Skip to content

Commit 203f2e7

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nat: remove l4proto->unique_tuple
fold remaining users (icmp, icmpv6, gre) into nf_nat_l4proto_unique_tuple. The static-save of old incarnation of resolved key in gre and icmp is removed as well, just use the prandom based offset like the others. Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 716b23c commit 203f2e7

File tree

6 files changed

+56
-134
lines changed

6 files changed

+56
-134
lines changed

include/net/netfilter/nf_nat_l4proto.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,6 @@ struct nf_nat_l4proto {
2727
const union nf_conntrack_man_proto *min,
2828
const union nf_conntrack_man_proto *max);
2929

30-
/* Alter the per-proto part of the tuple (depending on
31-
* maniptype), to give a unique tuple in the given range if
32-
* possible. Per-protocol part of tuple is initialized to the
33-
* incoming packet.
34-
*/
35-
void (*unique_tuple)(const struct nf_nat_l3proto *l3proto,
36-
struct nf_conntrack_tuple *tuple,
37-
const struct nf_nat_range2 *range,
38-
enum nf_nat_manip_type maniptype,
39-
const struct nf_conn *ct);
40-
4130
int (*nlattr_to_range)(struct nlattr *tb[],
4231
struct nf_nat_range2 *range);
4332
};

net/ipv4/netfilter/nf_nat_proto_gre.c

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,49 +37,6 @@ MODULE_LICENSE("GPL");
3737
MODULE_AUTHOR("Harald Welte <[email protected]>");
3838
MODULE_DESCRIPTION("Netfilter NAT protocol helper module for GRE");
3939

40-
/* generate unique tuple ... */
41-
static void
42-
gre_unique_tuple(const struct nf_nat_l3proto *l3proto,
43-
struct nf_conntrack_tuple *tuple,
44-
const struct nf_nat_range2 *range,
45-
enum nf_nat_manip_type maniptype,
46-
const struct nf_conn *ct)
47-
{
48-
static u_int16_t key;
49-
__be16 *keyptr;
50-
unsigned int min, i, range_size;
51-
52-
/* If there is no master conntrack we are not PPTP,
53-
do not change tuples */
54-
if (!ct->master)
55-
return;
56-
57-
if (maniptype == NF_NAT_MANIP_SRC)
58-
keyptr = &tuple->src.u.gre.key;
59-
else
60-
keyptr = &tuple->dst.u.gre.key;
61-
62-
if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
63-
pr_debug("%p: NATing GRE PPTP\n", ct);
64-
min = 1;
65-
range_size = 0xffff;
66-
} else {
67-
min = ntohs(range->min_proto.gre.key);
68-
range_size = ntohs(range->max_proto.gre.key) - min + 1;
69-
}
70-
71-
pr_debug("min = %u, range_size = %u\n", min, range_size);
72-
73-
for (i = 0; ; ++key) {
74-
*keyptr = htons(min + key % range_size);
75-
if (++i == range_size || !nf_nat_used_tuple(tuple, ct))
76-
return;
77-
}
78-
79-
pr_debug("%p: no NAT mapping\n", ct);
80-
return;
81-
}
82-
8340
/* manipulate a GRE packet according to maniptype */
8441
static bool
8542
gre_manip_pkt(struct sk_buff *skb,
@@ -124,7 +81,6 @@ static const struct nf_nat_l4proto gre = {
12481
.l4proto = IPPROTO_GRE,
12582
.manip_pkt = gre_manip_pkt,
12683
.in_range = nf_nat_l4proto_in_range,
127-
.unique_tuple = gre_unique_tuple,
12884
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
12985
.nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
13086
#endif

net/ipv4/netfilter/nf_nat_proto_icmp.c

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,6 @@ icmp_in_range(const struct nf_conntrack_tuple *tuple,
2727
ntohs(tuple->src.u.icmp.id) <= ntohs(max->icmp.id);
2828
}
2929

30-
static void
31-
icmp_unique_tuple(const struct nf_nat_l3proto *l3proto,
32-
struct nf_conntrack_tuple *tuple,
33-
const struct nf_nat_range2 *range,
34-
enum nf_nat_manip_type maniptype,
35-
const struct nf_conn *ct)
36-
{
37-
static u_int16_t id;
38-
unsigned int range_size;
39-
unsigned int i;
40-
41-
range_size = ntohs(range->max_proto.icmp.id) -
42-
ntohs(range->min_proto.icmp.id) + 1;
43-
/* If no range specified... */
44-
if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED))
45-
range_size = 0xFFFF;
46-
47-
for (i = 0; ; ++id) {
48-
tuple->src.u.icmp.id = htons(ntohs(range->min_proto.icmp.id) +
49-
(id % range_size));
50-
if (++i == range_size || !nf_nat_used_tuple(tuple, ct))
51-
return;
52-
}
53-
return;
54-
}
55-
5630
static bool
5731
icmp_manip_pkt(struct sk_buff *skb,
5832
const struct nf_nat_l3proto *l3proto,
@@ -76,7 +50,6 @@ const struct nf_nat_l4proto nf_nat_l4proto_icmp = {
7650
.l4proto = IPPROTO_ICMP,
7751
.manip_pkt = icmp_manip_pkt,
7852
.in_range = icmp_in_range,
79-
.unique_tuple = icmp_unique_tuple,
8053
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
8154
.nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
8255
#endif

net/ipv6/netfilter/nf_nat_proto_icmpv6.c

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,6 @@ icmpv6_in_range(const struct nf_conntrack_tuple *tuple,
2929
ntohs(tuple->src.u.icmp.id) <= ntohs(max->icmp.id);
3030
}
3131

32-
static void
33-
icmpv6_unique_tuple(const struct nf_nat_l3proto *l3proto,
34-
struct nf_conntrack_tuple *tuple,
35-
const struct nf_nat_range2 *range,
36-
enum nf_nat_manip_type maniptype,
37-
const struct nf_conn *ct)
38-
{
39-
static u16 id;
40-
unsigned int range_size;
41-
unsigned int i;
42-
43-
range_size = ntohs(range->max_proto.icmp.id) -
44-
ntohs(range->min_proto.icmp.id) + 1;
45-
46-
if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED))
47-
range_size = 0xffff;
48-
49-
for (i = 0; ; ++id) {
50-
tuple->src.u.icmp.id = htons(ntohs(range->min_proto.icmp.id) +
51-
(id % range_size));
52-
if (++i == range_size || !nf_nat_used_tuple(tuple, ct))
53-
return;
54-
}
55-
}
56-
5732
static bool
5833
icmpv6_manip_pkt(struct sk_buff *skb,
5934
const struct nf_nat_l3proto *l3proto,
@@ -83,7 +58,6 @@ const struct nf_nat_l4proto nf_nat_l4proto_icmpv6 = {
8358
.l4proto = IPPROTO_ICMPV6,
8459
.manip_pkt = icmpv6_manip_pkt,
8560
.in_range = icmpv6_in_range,
86-
.unique_tuple = icmpv6_unique_tuple,
8761
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
8862
.nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
8963
#endif

net/netfilter/nf_nat_core.c

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,30 +310,75 @@ find_best_ips_proto(const struct nf_conntrack_zone *zone,
310310
}
311311
}
312312

313+
/* Alter the per-proto part of the tuple (depending on maniptype), to
314+
* give a unique tuple in the given range if possible.
315+
*
316+
* Per-protocol part of tuple is initialized to the incoming packet.
317+
*/
313318
static void nf_nat_l4proto_unique_tuple(struct nf_conntrack_tuple *tuple,
314319
const struct nf_nat_range2 *range,
315320
enum nf_nat_manip_type maniptype,
316321
const struct nf_conn *ct)
317322
{
318323
unsigned int range_size, min, max, i, attempts;
319-
__be16 *portptr;
324+
__be16 *keyptr;
320325
u16 off;
321326
static const unsigned int max_attempts = 128;
322327

323-
if (maniptype == NF_NAT_MANIP_SRC)
324-
portptr = &tuple->src.u.all;
325-
else
326-
portptr = &tuple->dst.u.all;
328+
switch (tuple->dst.protonum) {
329+
case IPPROTO_ICMP: /* fallthrough */
330+
case IPPROTO_ICMPV6:
331+
/* id is same for either direction... */
332+
keyptr = &tuple->src.u.icmp.id;
333+
min = range->min_proto.icmp.id;
334+
range_size = ntohs(range->max_proto.icmp.id) -
335+
ntohs(range->min_proto.icmp.id) + 1;
336+
goto find_free_id;
337+
#if IS_ENABLED(CONFIG_NF_CT_PROTO_GRE)
338+
case IPPROTO_GRE:
339+
/* If there is no master conntrack we are not PPTP,
340+
do not change tuples */
341+
if (!ct->master)
342+
return;
343+
344+
if (maniptype == NF_NAT_MANIP_SRC)
345+
keyptr = &tuple->src.u.gre.key;
346+
else
347+
keyptr = &tuple->dst.u.gre.key;
348+
349+
if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
350+
min = 1;
351+
range_size = 65535;
352+
} else {
353+
min = ntohs(range->min_proto.gre.key);
354+
range_size = ntohs(range->max_proto.gre.key) - min + 1;
355+
}
356+
goto find_free_id;
357+
#endif
358+
case IPPROTO_UDP: /* fallthrough */
359+
case IPPROTO_UDPLITE: /* fallthrough */
360+
case IPPROTO_TCP: /* fallthrough */
361+
case IPPROTO_SCTP: /* fallthrough */
362+
case IPPROTO_DCCP: /* fallthrough */
363+
if (maniptype == NF_NAT_MANIP_SRC)
364+
keyptr = &tuple->src.u.all;
365+
else
366+
keyptr = &tuple->dst.u.all;
367+
368+
break;
369+
default:
370+
return;
371+
}
327372

328373
/* If no range specified... */
329374
if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
330375
/* If it's dst rewrite, can't change port */
331376
if (maniptype == NF_NAT_MANIP_DST)
332377
return;
333378

334-
if (ntohs(*portptr) < 1024) {
379+
if (ntohs(*keyptr) < 1024) {
335380
/* Loose convention: >> 512 is credential passing */
336-
if (ntohs(*portptr) < 512) {
381+
if (ntohs(*keyptr) < 512) {
337382
min = 1;
338383
range_size = 511 - min + 1;
339384
} else {
@@ -352,8 +397,9 @@ static void nf_nat_l4proto_unique_tuple(struct nf_conntrack_tuple *tuple,
352397
range_size = max - min + 1;
353398
}
354399

400+
find_free_id:
355401
if (range->flags & NF_NAT_RANGE_PROTO_OFFSET)
356-
off = (ntohs(*portptr) - ntohs(range->base_proto.all));
402+
off = (ntohs(*keyptr) - ntohs(range->base_proto.all));
357403
else
358404
off = prandom_u32();
359405

@@ -369,7 +415,7 @@ static void nf_nat_l4proto_unique_tuple(struct nf_conntrack_tuple *tuple,
369415
*/
370416
another_round:
371417
for (i = 0; i < attempts; i++, off++) {
372-
*portptr = htons(min + off % range_size);
418+
*keyptr = htons(min + off % range_size);
373419
if (!nf_nat_used_tuple(tuple, ct))
374420
return;
375421
}
@@ -454,10 +500,7 @@ get_unique_tuple(struct nf_conntrack_tuple *tuple,
454500
}
455501

456502
/* Last chance: get protocol to try to obtain unique tuple. */
457-
if (l4proto->unique_tuple)
458-
l4proto->unique_tuple(l3proto, tuple, range, maniptype, ct);
459-
else
460-
nf_nat_l4proto_unique_tuple(tuple, range, maniptype, ct);
503+
nf_nat_l4proto_unique_tuple(tuple, range, maniptype, ct);
461504
out:
462505
rcu_read_unlock();
463506
}

net/netfilter/nf_nat_proto_unknown.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,6 @@ static bool unknown_in_range(const struct nf_conntrack_tuple *tuple,
2525
return true;
2626
}
2727

28-
static void unknown_unique_tuple(const struct nf_nat_l3proto *l3proto,
29-
struct nf_conntrack_tuple *tuple,
30-
const struct nf_nat_range2 *range,
31-
enum nf_nat_manip_type maniptype,
32-
const struct nf_conn *ct)
33-
{
34-
/* Sorry: we can't help you; if it's not unique, we can't frob
35-
* anything.
36-
*/
37-
return;
38-
}
39-
4028
static bool
4129
unknown_manip_pkt(struct sk_buff *skb,
4230
const struct nf_nat_l3proto *l3proto,
@@ -50,5 +38,4 @@ unknown_manip_pkt(struct sk_buff *skb,
5038
const struct nf_nat_l4proto nf_nat_l4proto_unknown = {
5139
.manip_pkt = unknown_manip_pkt,
5240
.in_range = unknown_in_range,
53-
.unique_tuple = unknown_unique_tuple,
5441
};

0 commit comments

Comments
 (0)