Skip to content

Commit 096d090

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nat: move nlattr parse and xfrm session decode to core
None of these functions calls any external functions, moving them allows to avoid both the indirection and a need to export these symbols. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent d1aca8a commit 096d090

4 files changed

Lines changed: 123 additions & 146 deletions

File tree

‎include/net/netfilter/nf_nat_l3proto.h‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@ struct nf_nat_l3proto {
1818
void (*csum_recalc)(struct sk_buff *skb, u8 proto,
1919
void *data, __sum16 *check,
2020
int datalen, int oldlen);
21-
22-
void (*decode_session)(struct sk_buff *skb,
23-
const struct nf_conn *ct,
24-
enum ip_conntrack_dir dir,
25-
unsigned long statusbit,
26-
struct flowi *fl);
27-
28-
int (*nlattr_to_range)(struct nlattr *tb[],
29-
struct nf_nat_range2 *range);
3021
};
3122

3223
int nf_nat_l3proto_register(const struct nf_nat_l3proto *);

‎net/ipv4/netfilter/nf_nat_l3proto_ipv4.c‎

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,6 @@
2828

2929
static const struct nf_nat_l3proto nf_nat_l3proto_ipv4;
3030

31-
#ifdef CONFIG_XFRM
32-
static void nf_nat_ipv4_decode_session(struct sk_buff *skb,
33-
const struct nf_conn *ct,
34-
enum ip_conntrack_dir dir,
35-
unsigned long statusbit,
36-
struct flowi *fl)
37-
{
38-
const struct nf_conntrack_tuple *t = &ct->tuplehash[dir].tuple;
39-
struct flowi4 *fl4 = &fl->u.ip4;
40-
41-
if (ct->status & statusbit) {
42-
fl4->daddr = t->dst.u3.ip;
43-
if (t->dst.protonum == IPPROTO_TCP ||
44-
t->dst.protonum == IPPROTO_UDP ||
45-
t->dst.protonum == IPPROTO_UDPLITE ||
46-
t->dst.protonum == IPPROTO_DCCP ||
47-
t->dst.protonum == IPPROTO_SCTP)
48-
fl4->fl4_dport = t->dst.u.all;
49-
}
50-
51-
statusbit ^= IPS_NAT_MASK;
52-
53-
if (ct->status & statusbit) {
54-
fl4->saddr = t->src.u3.ip;
55-
if (t->dst.protonum == IPPROTO_TCP ||
56-
t->dst.protonum == IPPROTO_UDP ||
57-
t->dst.protonum == IPPROTO_UDPLITE ||
58-
t->dst.protonum == IPPROTO_DCCP ||
59-
t->dst.protonum == IPPROTO_SCTP)
60-
fl4->fl4_sport = t->src.u.all;
61-
}
62-
}
63-
#endif /* CONFIG_XFRM */
64-
6531
static bool nf_nat_ipv4_manip_pkt(struct sk_buff *skb,
6632
unsigned int iphdroff,
6733
const struct nf_conntrack_tuple *target,
@@ -127,35 +93,11 @@ static void nf_nat_ipv4_csum_recalc(struct sk_buff *skb,
12793
htons(oldlen), htons(datalen), true);
12894
}
12995

130-
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
131-
static int nf_nat_ipv4_nlattr_to_range(struct nlattr *tb[],
132-
struct nf_nat_range2 *range)
133-
{
134-
if (tb[CTA_NAT_V4_MINIP]) {
135-
range->min_addr.ip = nla_get_be32(tb[CTA_NAT_V4_MINIP]);
136-
range->flags |= NF_NAT_RANGE_MAP_IPS;
137-
}
138-
139-
if (tb[CTA_NAT_V4_MAXIP])
140-
range->max_addr.ip = nla_get_be32(tb[CTA_NAT_V4_MAXIP]);
141-
else
142-
range->max_addr.ip = range->min_addr.ip;
143-
144-
return 0;
145-
}
146-
#endif
147-
14896
static const struct nf_nat_l3proto nf_nat_l3proto_ipv4 = {
14997
.l3proto = NFPROTO_IPV4,
15098
.manip_pkt = nf_nat_ipv4_manip_pkt,
15199
.csum_update = nf_nat_ipv4_csum_update,
152100
.csum_recalc = nf_nat_ipv4_csum_recalc,
153-
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
154-
.nlattr_to_range = nf_nat_ipv4_nlattr_to_range,
155-
#endif
156-
#ifdef CONFIG_XFRM
157-
.decode_session = nf_nat_ipv4_decode_session,
158-
#endif
159101
};
160102

161103
int nf_nat_icmp_reply_translation(struct sk_buff *skb,

‎net/ipv6/netfilter/nf_nat_l3proto_ipv6.c‎

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,6 @@
2828

2929
static const struct nf_nat_l3proto nf_nat_l3proto_ipv6;
3030

31-
#ifdef CONFIG_XFRM
32-
static void nf_nat_ipv6_decode_session(struct sk_buff *skb,
33-
const struct nf_conn *ct,
34-
enum ip_conntrack_dir dir,
35-
unsigned long statusbit,
36-
struct flowi *fl)
37-
{
38-
const struct nf_conntrack_tuple *t = &ct->tuplehash[dir].tuple;
39-
struct flowi6 *fl6 = &fl->u.ip6;
40-
41-
if (ct->status & statusbit) {
42-
fl6->daddr = t->dst.u3.in6;
43-
if (t->dst.protonum == IPPROTO_TCP ||
44-
t->dst.protonum == IPPROTO_UDP ||
45-
t->dst.protonum == IPPROTO_UDPLITE ||
46-
t->dst.protonum == IPPROTO_DCCP ||
47-
t->dst.protonum == IPPROTO_SCTP)
48-
fl6->fl6_dport = t->dst.u.all;
49-
}
50-
51-
statusbit ^= IPS_NAT_MASK;
52-
53-
if (ct->status & statusbit) {
54-
fl6->saddr = t->src.u3.in6;
55-
if (t->dst.protonum == IPPROTO_TCP ||
56-
t->dst.protonum == IPPROTO_UDP ||
57-
t->dst.protonum == IPPROTO_UDPLITE ||
58-
t->dst.protonum == IPPROTO_DCCP ||
59-
t->dst.protonum == IPPROTO_SCTP)
60-
fl6->fl6_sport = t->src.u.all;
61-
}
62-
}
63-
#endif
64-
6531
static bool nf_nat_ipv6_manip_pkt(struct sk_buff *skb,
6632
unsigned int iphdroff,
6733
const struct nf_conntrack_tuple *target,
@@ -136,37 +102,11 @@ static void nf_nat_ipv6_csum_recalc(struct sk_buff *skb,
136102
htons(oldlen), htons(datalen), true);
137103
}
138104

139-
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
140-
static int nf_nat_ipv6_nlattr_to_range(struct nlattr *tb[],
141-
struct nf_nat_range2 *range)
142-
{
143-
if (tb[CTA_NAT_V6_MINIP]) {
144-
nla_memcpy(&range->min_addr.ip6, tb[CTA_NAT_V6_MINIP],
145-
sizeof(struct in6_addr));
146-
range->flags |= NF_NAT_RANGE_MAP_IPS;
147-
}
148-
149-
if (tb[CTA_NAT_V6_MAXIP])
150-
nla_memcpy(&range->max_addr.ip6, tb[CTA_NAT_V6_MAXIP],
151-
sizeof(struct in6_addr));
152-
else
153-
range->max_addr = range->min_addr;
154-
155-
return 0;
156-
}
157-
#endif
158-
159105
static const struct nf_nat_l3proto nf_nat_l3proto_ipv6 = {
160106
.l3proto = NFPROTO_IPV6,
161107
.manip_pkt = nf_nat_ipv6_manip_pkt,
162108
.csum_update = nf_nat_ipv6_csum_update,
163109
.csum_recalc = nf_nat_ipv6_csum_recalc,
164-
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
165-
.nlattr_to_range = nf_nat_ipv6_nlattr_to_range,
166-
#endif
167-
#ifdef CONFIG_XFRM
168-
.decode_session = nf_nat_ipv6_decode_session,
169-
#endif
170110
};
171111

172112
int nf_nat_icmpv6_reply_translation(struct sk_buff *skb,

‎net/netfilter/nf_nat_core.c‎

Lines changed: 123 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,74 @@ __nf_nat_l3proto_find(u8 family)
6565
}
6666

6767
#ifdef CONFIG_XFRM
68+
static void nf_nat_ipv4_decode_session(struct sk_buff *skb,
69+
const struct nf_conn *ct,
70+
enum ip_conntrack_dir dir,
71+
unsigned long statusbit,
72+
struct flowi *fl)
73+
{
74+
const struct nf_conntrack_tuple *t = &ct->tuplehash[dir].tuple;
75+
struct flowi4 *fl4 = &fl->u.ip4;
76+
77+
if (ct->status & statusbit) {
78+
fl4->daddr = t->dst.u3.ip;
79+
if (t->dst.protonum == IPPROTO_TCP ||
80+
t->dst.protonum == IPPROTO_UDP ||
81+
t->dst.protonum == IPPROTO_UDPLITE ||
82+
t->dst.protonum == IPPROTO_DCCP ||
83+
t->dst.protonum == IPPROTO_SCTP)
84+
fl4->fl4_dport = t->dst.u.all;
85+
}
86+
87+
statusbit ^= IPS_NAT_MASK;
88+
89+
if (ct->status & statusbit) {
90+
fl4->saddr = t->src.u3.ip;
91+
if (t->dst.protonum == IPPROTO_TCP ||
92+
t->dst.protonum == IPPROTO_UDP ||
93+
t->dst.protonum == IPPROTO_UDPLITE ||
94+
t->dst.protonum == IPPROTO_DCCP ||
95+
t->dst.protonum == IPPROTO_SCTP)
96+
fl4->fl4_sport = t->src.u.all;
97+
}
98+
}
99+
100+
static void nf_nat_ipv6_decode_session(struct sk_buff *skb,
101+
const struct nf_conn *ct,
102+
enum ip_conntrack_dir dir,
103+
unsigned long statusbit,
104+
struct flowi *fl)
105+
{
106+
#if IS_ENABLED(CONFIG_IPV6)
107+
const struct nf_conntrack_tuple *t = &ct->tuplehash[dir].tuple;
108+
struct flowi6 *fl6 = &fl->u.ip6;
109+
110+
if (ct->status & statusbit) {
111+
fl6->daddr = t->dst.u3.in6;
112+
if (t->dst.protonum == IPPROTO_TCP ||
113+
t->dst.protonum == IPPROTO_UDP ||
114+
t->dst.protonum == IPPROTO_UDPLITE ||
115+
t->dst.protonum == IPPROTO_DCCP ||
116+
t->dst.protonum == IPPROTO_SCTP)
117+
fl6->fl6_dport = t->dst.u.all;
118+
}
119+
120+
statusbit ^= IPS_NAT_MASK;
121+
122+
if (ct->status & statusbit) {
123+
fl6->saddr = t->src.u3.in6;
124+
if (t->dst.protonum == IPPROTO_TCP ||
125+
t->dst.protonum == IPPROTO_UDP ||
126+
t->dst.protonum == IPPROTO_UDPLITE ||
127+
t->dst.protonum == IPPROTO_DCCP ||
128+
t->dst.protonum == IPPROTO_SCTP)
129+
fl6->fl6_sport = t->src.u.all;
130+
}
131+
#endif
132+
}
133+
68134
static void __nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl)
69135
{
70-
const struct nf_nat_l3proto *l3proto;
71136
const struct nf_conn *ct;
72137
enum ip_conntrack_info ctinfo;
73138
enum ip_conntrack_dir dir;
@@ -79,17 +144,20 @@ static void __nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl)
79144
return;
80145

81146
family = nf_ct_l3num(ct);
82-
l3proto = __nf_nat_l3proto_find(family);
83-
if (l3proto == NULL)
84-
return;
85-
86147
dir = CTINFO2DIR(ctinfo);
87148
if (dir == IP_CT_DIR_ORIGINAL)
88149
statusbit = IPS_DST_NAT;
89150
else
90151
statusbit = IPS_SRC_NAT;
91152

92-
l3proto->decode_session(skb, ct, dir, statusbit, fl);
153+
switch (family) {
154+
case NFPROTO_IPV4:
155+
nf_nat_ipv4_decode_session(skb, ct, dir, statusbit, fl);
156+
return;
157+
case NFPROTO_IPV6:
158+
nf_nat_ipv6_decode_session(skb, ct, dir, statusbit, fl);
159+
return;
160+
}
93161
}
94162

95163
int nf_xfrm_me_harder(struct net *net, struct sk_buff *skb, unsigned int family)
@@ -887,10 +955,43 @@ static const struct nla_policy nat_nla_policy[CTA_NAT_MAX+1] = {
887955
[CTA_NAT_PROTO] = { .type = NLA_NESTED },
888956
};
889957

958+
static int nf_nat_ipv4_nlattr_to_range(struct nlattr *tb[],
959+
struct nf_nat_range2 *range)
960+
{
961+
if (tb[CTA_NAT_V4_MINIP]) {
962+
range->min_addr.ip = nla_get_be32(tb[CTA_NAT_V4_MINIP]);
963+
range->flags |= NF_NAT_RANGE_MAP_IPS;
964+
}
965+
966+
if (tb[CTA_NAT_V4_MAXIP])
967+
range->max_addr.ip = nla_get_be32(tb[CTA_NAT_V4_MAXIP]);
968+
else
969+
range->max_addr.ip = range->min_addr.ip;
970+
971+
return 0;
972+
}
973+
974+
static int nf_nat_ipv6_nlattr_to_range(struct nlattr *tb[],
975+
struct nf_nat_range2 *range)
976+
{
977+
if (tb[CTA_NAT_V6_MINIP]) {
978+
nla_memcpy(&range->min_addr.ip6, tb[CTA_NAT_V6_MINIP],
979+
sizeof(struct in6_addr));
980+
range->flags |= NF_NAT_RANGE_MAP_IPS;
981+
}
982+
983+
if (tb[CTA_NAT_V6_MAXIP])
984+
nla_memcpy(&range->max_addr.ip6, tb[CTA_NAT_V6_MAXIP],
985+
sizeof(struct in6_addr));
986+
else
987+
range->max_addr = range->min_addr;
988+
989+
return 0;
990+
}
991+
890992
static int
891993
nfnetlink_parse_nat(const struct nlattr *nat,
892-
const struct nf_conn *ct, struct nf_nat_range2 *range,
893-
const struct nf_nat_l3proto *l3proto)
994+
const struct nf_conn *ct, struct nf_nat_range2 *range)
894995
{
895996
struct nlattr *tb[CTA_NAT_MAX+1];
896997
int err;
@@ -901,8 +1002,19 @@ nfnetlink_parse_nat(const struct nlattr *nat,
9011002
if (err < 0)
9021003
return err;
9031004

904-
err = l3proto->nlattr_to_range(tb, range);
905-
if (err < 0)
1005+
switch (nf_ct_l3num(ct)) {
1006+
case NFPROTO_IPV4:
1007+
err = nf_nat_ipv4_nlattr_to_range(tb, range);
1008+
break;
1009+
case NFPROTO_IPV6:
1010+
err = nf_nat_ipv6_nlattr_to_range(tb, range);
1011+
break;
1012+
default:
1013+
err = -EPROTONOSUPPORT;
1014+
break;
1015+
}
1016+
1017+
if (err)
9061018
return err;
9071019

9081020
if (!tb[CTA_NAT_PROTO])
@@ -918,7 +1030,6 @@ nfnetlink_parse_nat_setup(struct nf_conn *ct,
9181030
const struct nlattr *attr)
9191031
{
9201032
struct nf_nat_range2 range;
921-
const struct nf_nat_l3proto *l3proto;
9221033
int err;
9231034

9241035
/* Should not happen, restricted to creating new conntracks
@@ -927,18 +1038,11 @@ nfnetlink_parse_nat_setup(struct nf_conn *ct,
9271038
if (WARN_ON_ONCE(nf_nat_initialized(ct, manip)))
9281039
return -EEXIST;
9291040

930-
/* Make sure that L3 NAT is there by when we call nf_nat_setup_info to
931-
* attach the null binding, otherwise this may oops.
932-
*/
933-
l3proto = __nf_nat_l3proto_find(nf_ct_l3num(ct));
934-
if (l3proto == NULL)
935-
return -EAGAIN;
936-
9371041
/* No NAT information has been passed, allocate the null-binding */
9381042
if (attr == NULL)
9391043
return __nf_nat_alloc_null_binding(ct, manip) == NF_DROP ? -ENOMEM : 0;
9401044

941-
err = nfnetlink_parse_nat(attr, ct, &range, l3proto);
1045+
err = nfnetlink_parse_nat(attr, ct, &range);
9421046
if (err < 0)
9431047
return err;
9441048

0 commit comments

Comments
 (0)