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