]>
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)); | |
90 | data = (unsigned char *)skb->nh.iph + dataoff; | |
91 | ||
92 | /* move post-replacement */ | |
93 | memmove(data + match_offset + rep_len, | |
94 | data + match_offset + match_len, | |
95 | skb->tail - (data + match_offset + match_len)); | |
96 | ||
97 | /* insert data from buffer */ | |
98 | memcpy(data + match_offset, rep_buffer, rep_len); | |
99 | ||
100 | /* update skb info */ | |
101 | if (rep_len > match_len) { | |
102 | DEBUGP("nf_nat_mangle_packet: Extending packet by " | |
103 | "%u from %u bytes\n", rep_len - match_len, | |
104 | skb->len); | |
105 | skb_put(skb, rep_len - match_len); | |
106 | } else { | |
107 | DEBUGP("nf_nat_mangle_packet: Shrinking packet from " | |
108 | "%u from %u bytes\n", match_len - rep_len, | |
109 | skb->len); | |
110 | __skb_trim(skb, skb->len + rep_len - match_len); | |
111 | } | |
112 | ||
113 | /* fix IP hdr checksum information */ | |
114 | skb->nh.iph->tot_len = htons(skb->len); | |
115 | ip_send_check(skb->nh.iph); | |
116 | } | |
117 | ||
118 | /* Unusual, but possible case. */ | |
119 | static int enlarge_skb(struct sk_buff **pskb, unsigned int extra) | |
120 | { | |
121 | struct sk_buff *nskb; | |
122 | ||
123 | if ((*pskb)->len + extra > 65535) | |
124 | return 0; | |
125 | ||
126 | nskb = skb_copy_expand(*pskb, skb_headroom(*pskb), extra, GFP_ATOMIC); | |
127 | if (!nskb) | |
128 | return 0; | |
129 | ||
130 | /* Transfer socket to new skb. */ | |
131 | if ((*pskb)->sk) | |
132 | skb_set_owner_w(nskb, (*pskb)->sk); | |
133 | kfree_skb(*pskb); | |
134 | *pskb = nskb; | |
135 | return 1; | |
136 | } | |
137 | ||
138 | /* Generic function for mangling variable-length address changes inside | |
139 | * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX | |
140 | * command in FTP). | |
141 | * | |
142 | * Takes care about all the nasty sequence number changes, checksumming, | |
143 | * skb enlargement, ... | |
144 | * | |
145 | * */ | |
146 | int | |
147 | nf_nat_mangle_tcp_packet(struct sk_buff **pskb, | |
148 | struct nf_conn *ct, | |
149 | enum ip_conntrack_info ctinfo, | |
150 | unsigned int match_offset, | |
151 | unsigned int match_len, | |
152 | const char *rep_buffer, | |
153 | unsigned int rep_len) | |
154 | { | |
155 | struct iphdr *iph; | |
156 | struct tcphdr *tcph; | |
157 | int oldlen, datalen; | |
158 | ||
159 | if (!skb_make_writable(pskb, (*pskb)->len)) | |
160 | return 0; | |
161 | ||
162 | if (rep_len > match_len && | |
163 | rep_len - match_len > skb_tailroom(*pskb) && | |
164 | !enlarge_skb(pskb, rep_len - match_len)) | |
165 | return 0; | |
166 | ||
167 | SKB_LINEAR_ASSERT(*pskb); | |
168 | ||
169 | iph = (*pskb)->nh.iph; | |
170 | tcph = (void *)iph + iph->ihl*4; | |
171 | ||
172 | oldlen = (*pskb)->len - iph->ihl*4; | |
173 | mangle_contents(*pskb, iph->ihl*4 + tcph->doff*4, | |
174 | match_offset, match_len, rep_buffer, rep_len); | |
175 | ||
176 | datalen = (*pskb)->len - iph->ihl*4; | |
177 | if ((*pskb)->ip_summed != CHECKSUM_PARTIAL) { | |
178 | tcph->check = 0; | |
179 | tcph->check = tcp_v4_check(tcph, datalen, | |
180 | iph->saddr, iph->daddr, | |
181 | csum_partial((char *)tcph, | |
182 | datalen, 0)); | |
183 | } else | |
184 | nf_proto_csum_replace2(&tcph->check, *pskb, | |
185 | htons(oldlen), htons(datalen), 1); | |
186 | ||
187 | if (rep_len != match_len) { | |
188 | set_bit(IPS_SEQ_ADJUST_BIT, &ct->status); | |
189 | adjust_tcp_sequence(ntohl(tcph->seq), | |
190 | (int)rep_len - (int)match_len, | |
191 | ct, ctinfo); | |
192 | /* Tell TCP window tracking about seq change */ | |
193 | nf_conntrack_tcp_update(*pskb, (*pskb)->nh.iph->ihl*4, | |
194 | ct, CTINFO2DIR(ctinfo)); | |
195 | } | |
196 | return 1; | |
197 | } | |
198 | EXPORT_SYMBOL(nf_nat_mangle_tcp_packet); | |
199 | ||
200 | /* Generic function for mangling variable-length address changes inside | |
201 | * NATed UDP connections (like the CONNECT DATA XXXXX MESG XXXXX INDEX XXXXX | |
202 | * command in the Amanda protocol) | |
203 | * | |
204 | * Takes care about all the nasty sequence number changes, checksumming, | |
205 | * skb enlargement, ... | |
206 | * | |
207 | * XXX - This function could be merged with nf_nat_mangle_tcp_packet which | |
208 | * should be fairly easy to do. | |
209 | */ | |
210 | int | |
211 | nf_nat_mangle_udp_packet(struct sk_buff **pskb, | |
212 | struct nf_conn *ct, | |
213 | enum ip_conntrack_info ctinfo, | |
214 | unsigned int match_offset, | |
215 | unsigned int match_len, | |
216 | const char *rep_buffer, | |
217 | unsigned int rep_len) | |
218 | { | |
219 | struct iphdr *iph; | |
220 | struct udphdr *udph; | |
221 | int datalen, oldlen; | |
222 | ||
223 | /* UDP helpers might accidentally mangle the wrong packet */ | |
224 | iph = (*pskb)->nh.iph; | |
225 | if ((*pskb)->len < iph->ihl*4 + sizeof(*udph) + | |
226 | match_offset + match_len) | |
227 | return 0; | |
228 | ||
229 | if (!skb_make_writable(pskb, (*pskb)->len)) | |
230 | return 0; | |
231 | ||
232 | if (rep_len > match_len && | |
233 | rep_len - match_len > skb_tailroom(*pskb) && | |
234 | !enlarge_skb(pskb, rep_len - match_len)) | |
235 | return 0; | |
236 | ||
237 | iph = (*pskb)->nh.iph; | |
238 | udph = (void *)iph + iph->ihl*4; | |
239 | ||
240 | oldlen = (*pskb)->len - iph->ihl*4; | |
241 | mangle_contents(*pskb, iph->ihl*4 + sizeof(*udph), | |
242 | match_offset, match_len, rep_buffer, rep_len); | |
243 | ||
244 | /* update the length of the UDP packet */ | |
245 | datalen = (*pskb)->len - iph->ihl*4; | |
246 | udph->len = htons(datalen); | |
247 | ||
248 | /* fix udp checksum if udp checksum was previously calculated */ | |
249 | if (!udph->check && (*pskb)->ip_summed != CHECKSUM_PARTIAL) | |
250 | return 1; | |
251 | ||
252 | if ((*pskb)->ip_summed != CHECKSUM_PARTIAL) { | |
253 | udph->check = 0; | |
254 | udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr, | |
255 | datalen, IPPROTO_UDP, | |
256 | csum_partial((char *)udph, | |
257 | datalen, 0)); | |
258 | if (!udph->check) | |
259 | udph->check = CSUM_MANGLED_0; | |
260 | } else | |
261 | nf_proto_csum_replace2(&udph->check, *pskb, | |
262 | htons(oldlen), htons(datalen), 1); | |
263 | ||
264 | return 1; | |
265 | } | |
266 | EXPORT_SYMBOL(nf_nat_mangle_udp_packet); | |
267 | ||
268 | /* Adjust one found SACK option including checksum correction */ | |
269 | static void | |
270 | sack_adjust(struct sk_buff *skb, | |
271 | struct tcphdr *tcph, | |
272 | unsigned int sackoff, | |
273 | unsigned int sackend, | |
274 | struct nf_nat_seq *natseq) | |
275 | { | |
276 | while (sackoff < sackend) { | |
277 | struct tcp_sack_block_wire *sack; | |
278 | __be32 new_start_seq, new_end_seq; | |
279 | ||
280 | sack = (void *)skb->data + sackoff; | |
281 | if (after(ntohl(sack->start_seq) - natseq->offset_before, | |
282 | natseq->correction_pos)) | |
283 | new_start_seq = htonl(ntohl(sack->start_seq) | |
284 | - natseq->offset_after); | |
285 | else | |
286 | new_start_seq = htonl(ntohl(sack->start_seq) | |
287 | - natseq->offset_before); | |
288 | ||
289 | if (after(ntohl(sack->end_seq) - natseq->offset_before, | |
290 | natseq->correction_pos)) | |
291 | new_end_seq = htonl(ntohl(sack->end_seq) | |
292 | - natseq->offset_after); | |
293 | else | |
294 | new_end_seq = htonl(ntohl(sack->end_seq) | |
295 | - natseq->offset_before); | |
296 | ||
297 | DEBUGP("sack_adjust: start_seq: %d->%d, end_seq: %d->%d\n", | |
298 | ntohl(sack->start_seq), new_start_seq, | |
299 | ntohl(sack->end_seq), new_end_seq); | |
300 | ||
301 | nf_proto_csum_replace4(&tcph->check, skb, | |
302 | sack->start_seq, new_start_seq, 0); | |
303 | nf_proto_csum_replace4(&tcph->check, skb, | |
304 | sack->end_seq, new_end_seq, 0); | |
305 | sack->start_seq = new_start_seq; | |
306 | sack->end_seq = new_end_seq; | |
307 | sackoff += sizeof(*sack); | |
308 | } | |
309 | } | |
310 | ||
311 | /* TCP SACK sequence number adjustment */ | |
312 | static inline unsigned int | |
313 | nf_nat_sack_adjust(struct sk_buff **pskb, | |
314 | struct tcphdr *tcph, | |
315 | struct nf_conn *ct, | |
316 | enum ip_conntrack_info ctinfo) | |
317 | { | |
318 | unsigned int dir, optoff, optend; | |
319 | struct nf_conn_nat *nat = nfct_nat(ct); | |
320 | ||
321 | optoff = (*pskb)->nh.iph->ihl*4 + sizeof(struct tcphdr); | |
322 | optend = (*pskb)->nh.iph->ihl*4 + tcph->doff*4; | |
323 | ||
324 | if (!skb_make_writable(pskb, optend)) | |
325 | return 0; | |
326 | ||
327 | dir = CTINFO2DIR(ctinfo); | |
328 | ||
329 | while (optoff < optend) { | |
330 | /* Usually: option, length. */ | |
331 | unsigned char *op = (*pskb)->data + optoff; | |
332 | ||
333 | switch (op[0]) { | |
334 | case TCPOPT_EOL: | |
335 | return 1; | |
336 | case TCPOPT_NOP: | |
337 | optoff++; | |
338 | continue; | |
339 | default: | |
340 | /* no partial options */ | |
341 | if (optoff + 1 == optend || | |
342 | optoff + op[1] > optend || | |
343 | op[1] < 2) | |
344 | return 0; | |
345 | if (op[0] == TCPOPT_SACK && | |
346 | op[1] >= 2+TCPOLEN_SACK_PERBLOCK && | |
347 | ((op[1] - 2) % TCPOLEN_SACK_PERBLOCK) == 0) | |
348 | sack_adjust(*pskb, tcph, optoff+2, | |
349 | optoff+op[1], | |
350 | &nat->info.seq[!dir]); | |
351 | optoff += op[1]; | |
352 | } | |
353 | } | |
354 | return 1; | |
355 | } | |
356 | ||
357 | /* TCP sequence number adjustment. Returns 1 on success, 0 on failure */ | |
358 | int | |
359 | nf_nat_seq_adjust(struct sk_buff **pskb, | |
360 | struct nf_conn *ct, | |
361 | enum ip_conntrack_info ctinfo) | |
362 | { | |
363 | struct tcphdr *tcph; | |
364 | int dir; | |
365 | __be32 newseq, newack; | |
366 | struct nf_conn_nat *nat = nfct_nat(ct); | |
367 | struct nf_nat_seq *this_way, *other_way; | |
368 | ||
369 | dir = CTINFO2DIR(ctinfo); | |
370 | ||
371 | this_way = &nat->info.seq[dir]; | |
372 | other_way = &nat->info.seq[!dir]; | |
373 | ||
374 | if (!skb_make_writable(pskb, (*pskb)->nh.iph->ihl*4+sizeof(*tcph))) | |
375 | return 0; | |
376 | ||
377 | tcph = (void *)(*pskb)->data + (*pskb)->nh.iph->ihl*4; | |
378 | if (after(ntohl(tcph->seq), this_way->correction_pos)) | |
379 | newseq = htonl(ntohl(tcph->seq) + this_way->offset_after); | |
380 | else | |
381 | newseq = htonl(ntohl(tcph->seq) + this_way->offset_before); | |
382 | ||
383 | if (after(ntohl(tcph->ack_seq) - other_way->offset_before, | |
384 | other_way->correction_pos)) | |
385 | newack = htonl(ntohl(tcph->ack_seq) - other_way->offset_after); | |
386 | else | |
387 | newack = htonl(ntohl(tcph->ack_seq) - other_way->offset_before); | |
388 | ||
389 | nf_proto_csum_replace4(&tcph->check, *pskb, tcph->seq, newseq, 0); | |
390 | nf_proto_csum_replace4(&tcph->check, *pskb, tcph->ack_seq, newack, 0); | |
391 | ||
392 | DEBUGP("Adjusting sequence number from %u->%u, ack from %u->%u\n", | |
393 | ntohl(tcph->seq), ntohl(newseq), ntohl(tcph->ack_seq), | |
394 | ntohl(newack)); | |
395 | ||
396 | tcph->seq = newseq; | |
397 | tcph->ack_seq = newack; | |
398 | ||
399 | if (!nf_nat_sack_adjust(pskb, tcph, ct, ctinfo)) | |
400 | return 0; | |
401 | ||
402 | nf_conntrack_tcp_update(*pskb, (*pskb)->nh.iph->ihl*4, ct, dir); | |
403 | ||
404 | return 1; | |
405 | } | |
406 | EXPORT_SYMBOL(nf_nat_seq_adjust); | |
407 | ||
408 | /* Setup NAT on this expected conntrack so it follows master. */ | |
409 | /* If we fail to get a free NAT slot, we'll get dropped on confirm */ | |
410 | void nf_nat_follow_master(struct nf_conn *ct, | |
411 | struct nf_conntrack_expect *exp) | |
412 | { | |
413 | struct nf_nat_range range; | |
414 | ||
415 | /* This must be a fresh one. */ | |
416 | BUG_ON(ct->status & IPS_NAT_DONE_MASK); | |
417 | ||
418 | /* Change src to where master sends to */ | |
419 | range.flags = IP_NAT_RANGE_MAP_IPS; | |
420 | range.min_ip = range.max_ip | |
421 | = ct->master->tuplehash[!exp->dir].tuple.dst.u3.ip; | |
422 | /* hook doesn't matter, but it has to do source manip */ | |
423 | nf_nat_setup_info(ct, &range, NF_IP_POST_ROUTING); | |
424 | ||
425 | /* For DST manip, map port here to where it's expected. */ | |
426 | range.flags = (IP_NAT_RANGE_MAP_IPS | IP_NAT_RANGE_PROTO_SPECIFIED); | |
427 | range.min = range.max = exp->saved_proto; | |
428 | range.min_ip = range.max_ip | |
429 | = ct->master->tuplehash[!exp->dir].tuple.src.u3.ip; | |
430 | /* hook doesn't matter, but it has to do destination manip */ | |
431 | nf_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING); | |
432 | } | |
433 | EXPORT_SYMBOL(nf_nat_follow_master); |