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