Skip to content

Commit 0fabd2a

Browse files
LorenzoBianconiAlexei Starovoitov
authored andcommitted
net: netfilter: add bpf_ct_set_nat_info kfunc helper
Introduce bpf_ct_set_nat_info kfunc helper in order to set source and destination nat addresses/ports in a new allocated ct entry not inserted in the connection tracking table yet. Signed-off-by: Lorenzo Bianconi <[email protected]> Link: https://lore.kernel.org/r/9567db2fdfa5bebe7b7cc5870f7a34549418b4fc.1663778601.git.lorenzo@kernel.org Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent eed807f commit 0fabd2a

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

net/netfilter/nf_conntrack_bpf.c

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <net/netfilter/nf_conntrack.h>
1818
#include <net/netfilter/nf_conntrack_bpf.h>
1919
#include <net/netfilter/nf_conntrack_core.h>
20+
#include <net/netfilter/nf_nat.h>
2021

2122
/* bpf_ct_opts - Options for CT lookup helpers
2223
*
@@ -137,7 +138,6 @@ __bpf_nf_ct_alloc_entry(struct net *net, struct bpf_sock_tuple *bpf_tuple,
137138

138139
memset(&ct->proto, 0, sizeof(ct->proto));
139140
__nf_ct_set_timeout(ct, timeout * HZ);
140-
ct->status |= IPS_CONFIRMED;
141141

142142
out:
143143
if (opts->netns_id >= 0)
@@ -390,6 +390,7 @@ struct nf_conn *bpf_ct_insert_entry(struct nf_conn___init *nfct_i)
390390
struct nf_conn *nfct = (struct nf_conn *)nfct_i;
391391
int err;
392392

393+
nfct->status |= IPS_CONFIRMED;
393394
err = nf_conntrack_hash_check_insert(nfct);
394395
if (err < 0) {
395396
nf_conntrack_free(nfct);
@@ -475,6 +476,49 @@ int bpf_ct_change_status(struct nf_conn *nfct, u32 status)
475476
return nf_ct_change_status_common(nfct, status);
476477
}
477478

479+
/* bpf_ct_set_nat_info - Set source or destination nat address
480+
*
481+
* Set source or destination nat address of the newly allocated
482+
* nf_conn before insertion. This must be invoked for referenced
483+
* PTR_TO_BTF_ID to nf_conn___init.
484+
*
485+
* Parameters:
486+
* @nfct - Pointer to referenced nf_conn object, obtained using
487+
* bpf_xdp_ct_alloc or bpf_skb_ct_alloc.
488+
* @addr - Nat source/destination address
489+
* @port - Nat source/destination port. Non-positive values are
490+
* interpreted as select a random port.
491+
* @manip - NF_NAT_MANIP_SRC or NF_NAT_MANIP_DST
492+
*/
493+
int bpf_ct_set_nat_info(struct nf_conn___init *nfct,
494+
union nf_inet_addr *addr, int port,
495+
enum nf_nat_manip_type manip)
496+
{
497+
#if ((IS_MODULE(CONFIG_NF_NAT) && IS_MODULE(CONFIG_NF_CONNTRACK)) || \
498+
IS_BUILTIN(CONFIG_NF_NAT))
499+
struct nf_conn *ct = (struct nf_conn *)nfct;
500+
u16 proto = nf_ct_l3num(ct);
501+
struct nf_nat_range2 range;
502+
503+
if (proto != NFPROTO_IPV4 && proto != NFPROTO_IPV6)
504+
return -EINVAL;
505+
506+
memset(&range, 0, sizeof(struct nf_nat_range2));
507+
range.flags = NF_NAT_RANGE_MAP_IPS;
508+
range.min_addr = *addr;
509+
range.max_addr = range.min_addr;
510+
if (port > 0) {
511+
range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
512+
range.min_proto.all = cpu_to_be16(port);
513+
range.max_proto.all = range.min_proto.all;
514+
}
515+
516+
return nf_nat_setup_info(ct, &range, manip) == NF_DROP ? -ENOMEM : 0;
517+
#else
518+
return -EOPNOTSUPP;
519+
#endif
520+
}
521+
478522
__diag_pop()
479523

480524
BTF_SET8_START(nf_ct_kfunc_set)
@@ -488,6 +532,7 @@ BTF_ID_FLAGS(func, bpf_ct_set_timeout, KF_TRUSTED_ARGS)
488532
BTF_ID_FLAGS(func, bpf_ct_change_timeout, KF_TRUSTED_ARGS)
489533
BTF_ID_FLAGS(func, bpf_ct_set_status, KF_TRUSTED_ARGS)
490534
BTF_ID_FLAGS(func, bpf_ct_change_status, KF_TRUSTED_ARGS)
535+
BTF_ID_FLAGS(func, bpf_ct_set_nat_info, KF_TRUSTED_ARGS)
491536
BTF_SET8_END(nf_ct_kfunc_set)
492537

493538
static const struct btf_kfunc_id_set nf_conntrack_kfunc_set = {

0 commit comments

Comments
 (0)