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