Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* (C) 1999-2001 Paul `Rusty' Russell |
2 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | */ | |
8 | ||
9 | #include <linux/types.h> | |
10 | #include <linux/init.h> | |
41f4689a | 11 | #include <linux/random.h> |
1da177e4 LT |
12 | #include <linux/netfilter.h> |
13 | #include <linux/ip.h> | |
14 | #include <linux/udp.h> | |
15 | #include <linux/if.h> | |
16 | ||
17 | #include <linux/netfilter_ipv4/ip_nat.h> | |
18 | #include <linux/netfilter_ipv4/ip_nat_core.h> | |
19 | #include <linux/netfilter_ipv4/ip_nat_rule.h> | |
20 | #include <linux/netfilter_ipv4/ip_nat_protocol.h> | |
21 | ||
22 | static int | |
23 | udp_in_range(const struct ip_conntrack_tuple *tuple, | |
24 | enum ip_nat_manip_type maniptype, | |
25 | const union ip_conntrack_manip_proto *min, | |
26 | const union ip_conntrack_manip_proto *max) | |
27 | { | |
a76b11dd | 28 | __be16 port; |
1da177e4 LT |
29 | |
30 | if (maniptype == IP_NAT_MANIP_SRC) | |
31 | port = tuple->src.u.udp.port; | |
32 | else | |
33 | port = tuple->dst.u.udp.port; | |
34 | ||
35 | return ntohs(port) >= ntohs(min->udp.port) | |
36 | && ntohs(port) <= ntohs(max->udp.port); | |
37 | } | |
38 | ||
39 | static int | |
40 | udp_unique_tuple(struct ip_conntrack_tuple *tuple, | |
41 | const struct ip_nat_range *range, | |
42 | enum ip_nat_manip_type maniptype, | |
43 | const struct ip_conntrack *conntrack) | |
44 | { | |
d04b4f8c | 45 | static u_int16_t port; |
a76b11dd | 46 | __be16 *portptr; |
1da177e4 LT |
47 | unsigned int range_size, min, i; |
48 | ||
49 | if (maniptype == IP_NAT_MANIP_SRC) | |
50 | portptr = &tuple->src.u.udp.port; | |
51 | else | |
52 | portptr = &tuple->dst.u.udp.port; | |
53 | ||
54 | /* If no range specified... */ | |
55 | if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) { | |
56 | /* If it's dst rewrite, can't change port */ | |
57 | if (maniptype == IP_NAT_MANIP_DST) | |
58 | return 0; | |
59 | ||
60 | if (ntohs(*portptr) < 1024) { | |
61 | /* Loose convention: >> 512 is credential passing */ | |
62 | if (ntohs(*portptr)<512) { | |
63 | min = 1; | |
64 | range_size = 511 - min + 1; | |
65 | } else { | |
66 | min = 600; | |
67 | range_size = 1023 - min + 1; | |
68 | } | |
69 | } else { | |
70 | min = 1024; | |
71 | range_size = 65535 - 1024 + 1; | |
72 | } | |
73 | } else { | |
74 | min = ntohs(range->min.udp.port); | |
75 | range_size = ntohs(range->max.udp.port) - min + 1; | |
76 | } | |
77 | ||
41f4689a EL |
78 | /* Start from random port to avoid prediction */ |
79 | if (range->flags & IP_NAT_RANGE_PROTO_RANDOM) | |
80 | port = net_random(); | |
81 | ||
1da177e4 LT |
82 | for (i = 0; i < range_size; i++, port++) { |
83 | *portptr = htons(min + port % range_size); | |
84 | if (!ip_nat_used_tuple(tuple, conntrack)) | |
85 | return 1; | |
86 | } | |
87 | return 0; | |
88 | } | |
89 | ||
90 | static int | |
91 | udp_manip_pkt(struct sk_buff **pskb, | |
92 | unsigned int iphdroff, | |
93 | const struct ip_conntrack_tuple *tuple, | |
94 | enum ip_nat_manip_type maniptype) | |
95 | { | |
96 | struct iphdr *iph = (struct iphdr *)((*pskb)->data + iphdroff); | |
97 | struct udphdr *hdr; | |
98 | unsigned int hdroff = iphdroff + iph->ihl*4; | |
a76b11dd AV |
99 | __be32 oldip, newip; |
100 | __be16 *portptr, newport; | |
1da177e4 | 101 | |
089af26c | 102 | if (!skb_make_writable(pskb, hdroff + sizeof(*hdr))) |
1da177e4 LT |
103 | return 0; |
104 | ||
105 | iph = (struct iphdr *)((*pskb)->data + iphdroff); | |
106 | hdr = (struct udphdr *)((*pskb)->data + hdroff); | |
107 | ||
108 | if (maniptype == IP_NAT_MANIP_SRC) { | |
109 | /* Get rid of src ip and src pt */ | |
110 | oldip = iph->saddr; | |
111 | newip = tuple->src.ip; | |
112 | newport = tuple->src.u.udp.port; | |
113 | portptr = &hdr->source; | |
114 | } else { | |
115 | /* Get rid of dst ip and dst pt */ | |
116 | oldip = iph->daddr; | |
117 | newip = tuple->dst.ip; | |
118 | newport = tuple->dst.u.udp.port; | |
119 | portptr = &hdr->dest; | |
120 | } | |
4cf411de PM |
121 | |
122 | if (hdr->check || (*pskb)->ip_summed == CHECKSUM_PARTIAL) { | |
43bc0ca7 AV |
123 | nf_proto_csum_replace4(&hdr->check, *pskb, oldip, newip, 1); |
124 | nf_proto_csum_replace2(&hdr->check, *pskb, *portptr, newport, 0); | |
4cf411de | 125 | if (!hdr->check) |
f6ab0288 | 126 | hdr->check = CSUM_MANGLED_0; |
4cf411de | 127 | } |
1da177e4 LT |
128 | *portptr = newport; |
129 | return 1; | |
130 | } | |
131 | ||
373ac735 PM |
132 | struct ip_nat_protocol ip_nat_protocol_udp = { |
133 | .name = "UDP", | |
134 | .protonum = IPPROTO_UDP, | |
135 | .me = THIS_MODULE, | |
136 | .manip_pkt = udp_manip_pkt, | |
137 | .in_range = udp_in_range, | |
138 | .unique_tuple = udp_unique_tuple, | |
080774a2 HW |
139 | #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \ |
140 | defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE) | |
373ac735 PM |
141 | .range_to_nfattr = ip_nat_port_range_to_nfattr, |
142 | .nfattr_to_range = ip_nat_port_nfattr_to_range, | |
080774a2 | 143 | #endif |
1da177e4 | 144 | }; |