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