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