]>
Commit | Line | Data |
---|---|---|
e905a9ed | 1 | /* ip_nat_helper.c - generic support functions for NAT helpers |
1da177e4 LT |
2 | * |
3 | * (C) 2000-2002 Harald Welte <laforge@netfilter.org> | |
4 | * (C) 2003-2004 Netfilter Core Team <coreteam@netfilter.org> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * 14 Jan 2002 Harald Welte <laforge@gnumonks.org>: | |
e905a9ed | 11 | * - add support for SACK adjustment |
1da177e4 LT |
12 | * 14 Mar 2002 Harald Welte <laforge@gnumonks.org>: |
13 | * - merge SACK support into newnat API | |
14 | * 16 Aug 2002 Brian J. Murrell <netfilter@interlinx.bc.ca>: | |
15 | * - make ip_nat_resize_packet more generic (TCP and UDP) | |
16 | * - add ip_nat_mangle_udp_packet | |
17 | */ | |
1da177e4 LT |
18 | #include <linux/module.h> |
19 | #include <linux/kmod.h> | |
20 | #include <linux/types.h> | |
21 | #include <linux/timer.h> | |
22 | #include <linux/skbuff.h> | |
23 | #include <linux/netfilter_ipv4.h> | |
24 | #include <net/checksum.h> | |
25 | #include <net/icmp.h> | |
26 | #include <net/ip.h> | |
27 | #include <net/tcp.h> | |
28 | #include <net/udp.h> | |
29 | ||
1da177e4 LT |
30 | #include <linux/netfilter_ipv4/ip_conntrack.h> |
31 | #include <linux/netfilter_ipv4/ip_conntrack_helper.h> | |
32 | #include <linux/netfilter_ipv4/ip_nat.h> | |
33 | #include <linux/netfilter_ipv4/ip_nat_protocol.h> | |
34 | #include <linux/netfilter_ipv4/ip_nat_core.h> | |
35 | #include <linux/netfilter_ipv4/ip_nat_helper.h> | |
1da177e4 LT |
36 | |
37 | #if 0 | |
38 | #define DEBUGP printk | |
39 | #define DUMP_OFFSET(x) printk("offset_before=%d, offset_after=%d, correction_pos=%u\n", x->offset_before, x->offset_after, x->correction_pos); | |
40 | #else | |
41 | #define DEBUGP(format, args...) | |
42 | #define DUMP_OFFSET(x) | |
43 | #endif | |
44 | ||
e45b1be8 | 45 | static DEFINE_SPINLOCK(ip_nat_seqofs_lock); |
1da177e4 LT |
46 | |
47 | /* Setup TCP sequence correction given this change at this sequence */ | |
e905a9ed | 48 | static inline void |
1da177e4 LT |
49 | adjust_tcp_sequence(u32 seq, |
50 | int sizediff, | |
e905a9ed | 51 | struct ip_conntrack *ct, |
1da177e4 LT |
52 | enum ip_conntrack_info ctinfo) |
53 | { | |
54 | int dir; | |
55 | struct ip_nat_seq *this_way, *other_way; | |
56 | ||
57 | DEBUGP("ip_nat_resize_packet: old_size = %u, new_size = %u\n", | |
58 | (*skb)->len, new_size); | |
59 | ||
60 | dir = CTINFO2DIR(ctinfo); | |
61 | ||
62 | this_way = &ct->nat.info.seq[dir]; | |
63 | other_way = &ct->nat.info.seq[!dir]; | |
64 | ||
65 | DEBUGP("ip_nat_resize_packet: Seq_offset before: "); | |
66 | DUMP_OFFSET(this_way); | |
67 | ||
e45b1be8 | 68 | spin_lock_bh(&ip_nat_seqofs_lock); |
1da177e4 LT |
69 | |
70 | /* SYN adjust. If it's uninitialized, or this is after last | |
71 | * correction, record it: we don't handle more than one | |
72 | * adjustment in the window, but do deal with common case of a | |
73 | * retransmit */ | |
74 | if (this_way->offset_before == this_way->offset_after | |
75 | || before(this_way->correction_pos, seq)) { | |
76 | this_way->correction_pos = seq; | |
77 | this_way->offset_before = this_way->offset_after; | |
78 | this_way->offset_after += sizediff; | |
79 | } | |
e45b1be8 | 80 | spin_unlock_bh(&ip_nat_seqofs_lock); |
1da177e4 LT |
81 | |
82 | DEBUGP("ip_nat_resize_packet: Seq_offset after: "); | |
83 | DUMP_OFFSET(this_way); | |
84 | } | |
85 | ||
86 | /* Frobs data inside this packet, which is linear. */ | |
87 | static void mangle_contents(struct sk_buff *skb, | |
88 | unsigned int dataoff, | |
89 | unsigned int match_offset, | |
90 | unsigned int match_len, | |
91 | const char *rep_buffer, | |
92 | unsigned int rep_len) | |
93 | { | |
94 | unsigned char *data; | |
95 | ||
96 | BUG_ON(skb_is_nonlinear(skb)); | |
97 | data = (unsigned char *)skb->nh.iph + dataoff; | |
98 | ||
99 | /* move post-replacement */ | |
100 | memmove(data + match_offset + rep_len, | |
101 | data + match_offset + match_len, | |
102 | skb->tail - (data + match_offset + match_len)); | |
103 | ||
104 | /* insert data from buffer */ | |
105 | memcpy(data + match_offset, rep_buffer, rep_len); | |
106 | ||
107 | /* update skb info */ | |
108 | if (rep_len > match_len) { | |
109 | DEBUGP("ip_nat_mangle_packet: Extending packet by " | |
110 | "%u from %u bytes\n", rep_len - match_len, | |
111 | skb->len); | |
112 | skb_put(skb, rep_len - match_len); | |
113 | } else { | |
114 | DEBUGP("ip_nat_mangle_packet: Shrinking packet from " | |
115 | "%u from %u bytes\n", match_len - rep_len, | |
116 | skb->len); | |
117 | __skb_trim(skb, skb->len + rep_len - match_len); | |
118 | } | |
119 | ||
120 | /* fix IP hdr checksum information */ | |
121 | skb->nh.iph->tot_len = htons(skb->len); | |
122 | ip_send_check(skb->nh.iph); | |
123 | } | |
124 | ||
125 | /* Unusual, but possible case. */ | |
126 | static int enlarge_skb(struct sk_buff **pskb, unsigned int extra) | |
127 | { | |
128 | struct sk_buff *nskb; | |
129 | ||
130 | if ((*pskb)->len + extra > 65535) | |
131 | return 0; | |
132 | ||
133 | nskb = skb_copy_expand(*pskb, skb_headroom(*pskb), extra, GFP_ATOMIC); | |
134 | if (!nskb) | |
135 | return 0; | |
136 | ||
137 | /* Transfer socket to new skb. */ | |
138 | if ((*pskb)->sk) | |
139 | skb_set_owner_w(nskb, (*pskb)->sk); | |
1da177e4 LT |
140 | kfree_skb(*pskb); |
141 | *pskb = nskb; | |
142 | return 1; | |
143 | } | |
144 | ||
145 | /* Generic function for mangling variable-length address changes inside | |
146 | * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX | |
147 | * command in FTP). | |
148 | * | |
149 | * Takes care about all the nasty sequence number changes, checksumming, | |
150 | * skb enlargement, ... | |
151 | * | |
152 | * */ | |
e905a9ed | 153 | int |
1da177e4 LT |
154 | ip_nat_mangle_tcp_packet(struct sk_buff **pskb, |
155 | struct ip_conntrack *ct, | |
156 | enum ip_conntrack_info ctinfo, | |
157 | unsigned int match_offset, | |
158 | unsigned int match_len, | |
159 | const char *rep_buffer, | |
160 | unsigned int rep_len) | |
161 | { | |
162 | struct iphdr *iph; | |
163 | struct tcphdr *tcph; | |
4cf411de | 164 | int oldlen, datalen; |
1da177e4 | 165 | |
089af26c | 166 | if (!skb_make_writable(pskb, (*pskb)->len)) |
1da177e4 LT |
167 | return 0; |
168 | ||
169 | if (rep_len > match_len | |
170 | && rep_len - match_len > skb_tailroom(*pskb) | |
171 | && !enlarge_skb(pskb, rep_len - match_len)) | |
172 | return 0; | |
173 | ||
174 | SKB_LINEAR_ASSERT(*pskb); | |
175 | ||
176 | iph = (*pskb)->nh.iph; | |
177 | tcph = (void *)iph + iph->ihl*4; | |
178 | ||
4cf411de | 179 | oldlen = (*pskb)->len - iph->ihl*4; |
1da177e4 LT |
180 | mangle_contents(*pskb, iph->ihl*4 + tcph->doff*4, |
181 | match_offset, match_len, rep_buffer, rep_len); | |
182 | ||
183 | datalen = (*pskb)->len - iph->ihl*4; | |
4cf411de PM |
184 | if ((*pskb)->ip_summed != CHECKSUM_PARTIAL) { |
185 | tcph->check = 0; | |
ba7808ea | 186 | tcph->check = tcp_v4_check(datalen, |
4cf411de PM |
187 | iph->saddr, iph->daddr, |
188 | csum_partial((char *)tcph, | |
e905a9ed | 189 | datalen, 0)); |
4cf411de | 190 | } else |
43bc0ca7 AV |
191 | nf_proto_csum_replace2(&tcph->check, *pskb, |
192 | htons(oldlen), htons(datalen), 1); | |
1da177e4 LT |
193 | |
194 | if (rep_len != match_len) { | |
195 | set_bit(IPS_SEQ_ADJUST_BIT, &ct->status); | |
196 | adjust_tcp_sequence(ntohl(tcph->seq), | |
197 | (int)rep_len - (int)match_len, | |
198 | ct, ctinfo); | |
199 | /* Tell TCP window tracking about seq change */ | |
200 | ip_conntrack_tcp_update(*pskb, ct, CTINFO2DIR(ctinfo)); | |
201 | } | |
202 | return 1; | |
203 | } | |
188bab3a | 204 | EXPORT_SYMBOL(ip_nat_mangle_tcp_packet); |
e905a9ed | 205 | |
1da177e4 LT |
206 | /* Generic function for mangling variable-length address changes inside |
207 | * NATed UDP connections (like the CONNECT DATA XXXXX MESG XXXXX INDEX XXXXX | |
208 | * command in the Amanda protocol) | |
209 | * | |
210 | * Takes care about all the nasty sequence number changes, checksumming, | |
211 | * skb enlargement, ... | |
212 | * | |
213 | * XXX - This function could be merged with ip_nat_mangle_tcp_packet which | |
214 | * should be fairly easy to do. | |
215 | */ | |
e905a9ed | 216 | int |
1da177e4 LT |
217 | ip_nat_mangle_udp_packet(struct sk_buff **pskb, |
218 | struct ip_conntrack *ct, | |
219 | enum ip_conntrack_info ctinfo, | |
220 | unsigned int match_offset, | |
221 | unsigned int match_len, | |
222 | const char *rep_buffer, | |
223 | unsigned int rep_len) | |
224 | { | |
225 | struct iphdr *iph; | |
226 | struct udphdr *udph; | |
4cf411de | 227 | int datalen, oldlen; |
1da177e4 LT |
228 | |
229 | /* UDP helpers might accidentally mangle the wrong packet */ | |
230 | iph = (*pskb)->nh.iph; | |
e905a9ed YH |
231 | if ((*pskb)->len < iph->ihl*4 + sizeof(*udph) + |
232 | match_offset + match_len) | |
1da177e4 LT |
233 | return 0; |
234 | ||
089af26c | 235 | if (!skb_make_writable(pskb, (*pskb)->len)) |
1da177e4 LT |
236 | return 0; |
237 | ||
238 | if (rep_len > match_len | |
239 | && rep_len - match_len > skb_tailroom(*pskb) | |
240 | && !enlarge_skb(pskb, rep_len - match_len)) | |
241 | return 0; | |
242 | ||
243 | iph = (*pskb)->nh.iph; | |
244 | udph = (void *)iph + iph->ihl*4; | |
4cf411de PM |
245 | |
246 | oldlen = (*pskb)->len - iph->ihl*4; | |
1da177e4 LT |
247 | mangle_contents(*pskb, iph->ihl*4 + sizeof(*udph), |
248 | match_offset, match_len, rep_buffer, rep_len); | |
249 | ||
250 | /* update the length of the UDP packet */ | |
4cf411de PM |
251 | datalen = (*pskb)->len - iph->ihl*4; |
252 | udph->len = htons(datalen); | |
1da177e4 LT |
253 | |
254 | /* fix udp checksum if udp checksum was previously calculated */ | |
4cf411de PM |
255 | if (!udph->check && (*pskb)->ip_summed != CHECKSUM_PARTIAL) |
256 | return 1; | |
257 | ||
258 | if ((*pskb)->ip_summed != CHECKSUM_PARTIAL) { | |
1da177e4 LT |
259 | udph->check = 0; |
260 | udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr, | |
e905a9ed YH |
261 | datalen, IPPROTO_UDP, |
262 | csum_partial((char *)udph, | |
263 | datalen, 0)); | |
4cf411de | 264 | if (!udph->check) |
f6ab0288 | 265 | udph->check = CSUM_MANGLED_0; |
4cf411de | 266 | } else |
43bc0ca7 AV |
267 | nf_proto_csum_replace2(&udph->check, *pskb, |
268 | htons(oldlen), htons(datalen), 1); | |
1da177e4 LT |
269 | return 1; |
270 | } | |
188bab3a | 271 | EXPORT_SYMBOL(ip_nat_mangle_udp_packet); |
1da177e4 LT |
272 | |
273 | /* Adjust one found SACK option including checksum correction */ | |
274 | static void | |
275 | sack_adjust(struct sk_buff *skb, | |
e905a9ed | 276 | struct tcphdr *tcph, |
1da177e4 LT |
277 | unsigned int sackoff, |
278 | unsigned int sackend, | |
279 | struct ip_nat_seq *natseq) | |
280 | { | |
281 | while (sackoff < sackend) { | |
269bd27e | 282 | struct tcp_sack_block_wire *sack; |
a76b11dd | 283 | __be32 new_start_seq, new_end_seq; |
1da177e4 LT |
284 | |
285 | sack = (void *)skb->data + sackoff; | |
286 | if (after(ntohl(sack->start_seq) - natseq->offset_before, | |
287 | natseq->correction_pos)) | |
a76b11dd AV |
288 | new_start_seq = htonl(ntohl(sack->start_seq) |
289 | - natseq->offset_after); | |
1da177e4 | 290 | else |
a76b11dd AV |
291 | new_start_seq = htonl(ntohl(sack->start_seq) |
292 | - natseq->offset_before); | |
1da177e4 LT |
293 | |
294 | if (after(ntohl(sack->end_seq) - natseq->offset_before, | |
295 | natseq->correction_pos)) | |
a76b11dd AV |
296 | new_end_seq = htonl(ntohl(sack->end_seq) |
297 | - natseq->offset_after); | |
1da177e4 | 298 | else |
a76b11dd AV |
299 | new_end_seq = htonl(ntohl(sack->end_seq) |
300 | - natseq->offset_before); | |
1da177e4 LT |
301 | |
302 | DEBUGP("sack_adjust: start_seq: %d->%d, end_seq: %d->%d\n", | |
303 | ntohl(sack->start_seq), new_start_seq, | |
304 | ntohl(sack->end_seq), new_end_seq); | |
305 | ||
43bc0ca7 AV |
306 | nf_proto_csum_replace4(&tcph->check, skb, |
307 | sack->start_seq, new_start_seq, 0); | |
308 | nf_proto_csum_replace4(&tcph->check, skb, | |
309 | sack->end_seq, new_end_seq, 0); | |
1da177e4 LT |
310 | sack->start_seq = new_start_seq; |
311 | sack->end_seq = new_end_seq; | |
312 | sackoff += sizeof(*sack); | |
313 | } | |
314 | } | |
315 | ||
316 | /* TCP SACK sequence number adjustment */ | |
317 | static inline unsigned int | |
318 | ip_nat_sack_adjust(struct sk_buff **pskb, | |
319 | struct tcphdr *tcph, | |
320 | struct ip_conntrack *ct, | |
321 | enum ip_conntrack_info ctinfo) | |
322 | { | |
323 | unsigned int dir, optoff, optend; | |
324 | ||
c9bdd4b5 ACM |
325 | optoff = ip_hdrlen(*pskb) + sizeof(struct tcphdr); |
326 | optend = ip_hdrlen(*pskb) + tcph->doff * 4; | |
1da177e4 | 327 | |
089af26c | 328 | if (!skb_make_writable(pskb, optend)) |
1da177e4 LT |
329 | return 0; |
330 | ||
331 | dir = CTINFO2DIR(ctinfo); | |
332 | ||
333 | while (optoff < optend) { | |
334 | /* Usually: option, length. */ | |
335 | unsigned char *op = (*pskb)->data + optoff; | |
336 | ||
337 | switch (op[0]) { | |
338 | case TCPOPT_EOL: | |
339 | return 1; | |
340 | case TCPOPT_NOP: | |
341 | optoff++; | |
342 | continue; | |
343 | default: | |
344 | /* no partial options */ | |
345 | if (optoff + 1 == optend | |
346 | || optoff + op[1] > optend | |
347 | || op[1] < 2) | |
348 | return 0; | |
349 | if (op[0] == TCPOPT_SACK | |
350 | && op[1] >= 2+TCPOLEN_SACK_PERBLOCK | |
351 | && ((op[1] - 2) % TCPOLEN_SACK_PERBLOCK) == 0) | |
352 | sack_adjust(*pskb, tcph, optoff+2, | |
353 | optoff+op[1], | |
354 | &ct->nat.info.seq[!dir]); | |
355 | optoff += op[1]; | |
356 | } | |
357 | } | |
358 | return 1; | |
359 | } | |
360 | ||
361 | /* TCP sequence number adjustment. Returns 1 on success, 0 on failure */ | |
362 | int | |
e905a9ed YH |
363 | ip_nat_seq_adjust(struct sk_buff **pskb, |
364 | struct ip_conntrack *ct, | |
1da177e4 LT |
365 | enum ip_conntrack_info ctinfo) |
366 | { | |
367 | struct tcphdr *tcph; | |
a76b11dd AV |
368 | int dir; |
369 | __be32 newseq, newack; | |
e905a9ed | 370 | struct ip_nat_seq *this_way, *other_way; |
1da177e4 LT |
371 | |
372 | dir = CTINFO2DIR(ctinfo); | |
373 | ||
374 | this_way = &ct->nat.info.seq[dir]; | |
375 | other_way = &ct->nat.info.seq[!dir]; | |
376 | ||
c9bdd4b5 | 377 | if (!skb_make_writable(pskb, ip_hdrlen(*pskb) + sizeof(*tcph))) |
1da177e4 LT |
378 | return 0; |
379 | ||
c9bdd4b5 | 380 | tcph = (void *)(*pskb)->data + ip_hdrlen(*pskb); |
1da177e4 | 381 | if (after(ntohl(tcph->seq), this_way->correction_pos)) |
a76b11dd | 382 | newseq = htonl(ntohl(tcph->seq) + this_way->offset_after); |
1da177e4 | 383 | else |
a76b11dd | 384 | newseq = htonl(ntohl(tcph->seq) + this_way->offset_before); |
1da177e4 LT |
385 | |
386 | if (after(ntohl(tcph->ack_seq) - other_way->offset_before, | |
387 | other_way->correction_pos)) | |
a76b11dd | 388 | newack = htonl(ntohl(tcph->ack_seq) - other_way->offset_after); |
1da177e4 | 389 | else |
a76b11dd | 390 | newack = htonl(ntohl(tcph->ack_seq) - other_way->offset_before); |
1da177e4 | 391 | |
43bc0ca7 AV |
392 | nf_proto_csum_replace4(&tcph->check, *pskb, tcph->seq, newseq, 0); |
393 | nf_proto_csum_replace4(&tcph->check, *pskb, tcph->ack_seq, newack, 0); | |
1da177e4 LT |
394 | |
395 | DEBUGP("Adjusting sequence number from %u->%u, ack from %u->%u\n", | |
396 | ntohl(tcph->seq), ntohl(newseq), ntohl(tcph->ack_seq), | |
397 | ntohl(newack)); | |
398 | ||
399 | tcph->seq = newseq; | |
400 | tcph->ack_seq = newack; | |
401 | ||
402 | if (!ip_nat_sack_adjust(pskb, tcph, ct, ctinfo)) | |
403 | return 0; | |
404 | ||
405 | ip_conntrack_tcp_update(*pskb, ct, dir); | |
406 | ||
407 | return 1; | |
408 | } | |
188bab3a | 409 | EXPORT_SYMBOL(ip_nat_seq_adjust); |
1da177e4 LT |
410 | |
411 | /* Setup NAT on this expected conntrack so it follows master. */ | |
412 | /* If we fail to get a free NAT slot, we'll get dropped on confirm */ | |
413 | void ip_nat_follow_master(struct ip_conntrack *ct, | |
414 | struct ip_conntrack_expect *exp) | |
415 | { | |
416 | struct ip_nat_range range; | |
417 | ||
418 | /* This must be a fresh one. */ | |
419 | BUG_ON(ct->status & IPS_NAT_DONE_MASK); | |
420 | ||
421 | /* Change src to where master sends to */ | |
422 | range.flags = IP_NAT_RANGE_MAP_IPS; | |
423 | range.min_ip = range.max_ip | |
424 | = ct->master->tuplehash[!exp->dir].tuple.dst.ip; | |
425 | /* hook doesn't matter, but it has to do source manip */ | |
426 | ip_nat_setup_info(ct, &range, NF_IP_POST_ROUTING); | |
427 | ||
428 | /* For DST manip, map port here to where it's expected. */ | |
429 | range.flags = (IP_NAT_RANGE_MAP_IPS | IP_NAT_RANGE_PROTO_SPECIFIED); | |
430 | range.min = range.max = exp->saved_proto; | |
431 | range.min_ip = range.max_ip | |
432 | = ct->master->tuplehash[!exp->dir].tuple.src.ip; | |
433 | /* hook doesn't matter, but it has to do destination manip */ | |
434 | ip_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING); | |
435 | } | |
188bab3a | 436 | EXPORT_SYMBOL(ip_nat_follow_master); |