ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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/config.h>
19 #include <linux/module.h>
20 #include <linux/kmod.h>
21 #include <linux/types.h>
22 #include <linux/timer.h>
23 #include <linux/skbuff.h>
24 #include <linux/netfilter_ipv4.h>
25 #include <net/checksum.h>
26 #include <net/icmp.h>
27 #include <net/ip.h>
28 #include <net/tcp.h>
29 #include <net/udp.h>
30
31 #define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_nat_lock)
32 #define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_nat_lock)
33
34 #include <linux/netfilter_ipv4/ip_conntrack.h>
35 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
36 #include <linux/netfilter_ipv4/ip_nat.h>
37 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
38 #include <linux/netfilter_ipv4/ip_nat_core.h>
39 #include <linux/netfilter_ipv4/ip_nat_helper.h>
40 #include <linux/netfilter_ipv4/listhelp.h>
41
42 #if 0
43 #define DEBUGP printk
44 #define DUMP_OFFSET(x)  printk("offset_before=%d, offset_after=%d, correction_pos=%u\n", x->offset_before, x->offset_after, x->correction_pos);
45 #else
46 #define DEBUGP(format, args...)
47 #define DUMP_OFFSET(x)
48 #endif
49
50 DECLARE_LOCK(ip_nat_seqofs_lock);
51
52 /* Setup TCP sequence correction given this change at this sequence */
53 static inline void 
54 adjust_tcp_sequence(u32 seq,
55                     int sizediff,
56                     struct ip_conntrack *ct, 
57                     enum ip_conntrack_info ctinfo)
58 {
59         int dir;
60         struct ip_nat_seq *this_way, *other_way;
61
62         DEBUGP("ip_nat_resize_packet: old_size = %u, new_size = %u\n",
63                 (*skb)->len, new_size);
64
65         dir = CTINFO2DIR(ctinfo);
66
67         this_way = &ct->nat.info.seq[dir];
68         other_way = &ct->nat.info.seq[!dir];
69
70         DEBUGP("ip_nat_resize_packet: Seq_offset before: ");
71         DUMP_OFFSET(this_way);
72
73         LOCK_BH(&ip_nat_seqofs_lock);
74
75         /* SYN adjust. If it's uninitialized, of this is after last
76          * correction, record it: we don't handle more than one
77          * adjustment in the window, but do deal with common case of a
78          * retransmit */
79         if (this_way->offset_before == this_way->offset_after
80             || before(this_way->correction_pos, seq)) {
81                     this_way->correction_pos = seq;
82                     this_way->offset_before = this_way->offset_after;
83                     this_way->offset_after += sizediff;
84         }
85         UNLOCK_BH(&ip_nat_seqofs_lock);
86
87         DEBUGP("ip_nat_resize_packet: Seq_offset after: ");
88         DUMP_OFFSET(this_way);
89 }
90
91 /* Frobs data inside this packet, which is linear. */
92 static void mangle_contents(struct sk_buff *skb,
93                             unsigned int dataoff,
94                             unsigned int match_offset,
95                             unsigned int match_len,
96                             const char *rep_buffer,
97                             unsigned int rep_len)
98 {
99         unsigned char *data;
100
101         BUG_ON(skb_is_nonlinear(skb));
102         data = (unsigned char *)skb->nh.iph + dataoff;
103
104         /* move post-replacement */
105         memmove(data + match_offset + rep_len,
106                 data + match_offset + match_len,
107                 skb->tail - (data + match_offset + match_len));
108
109         /* insert data from buffer */
110         memcpy(data + match_offset, rep_buffer, rep_len);
111
112         /* update skb info */
113         if (rep_len > match_len) {
114                 DEBUGP("ip_nat_mangle_packet: Extending packet by "
115                         "%u from %u bytes\n", rep_len - match_len,
116                        skb->len);
117                 skb_put(skb, rep_len - match_len);
118         } else {
119                 DEBUGP("ip_nat_mangle_packet: Shrinking packet from "
120                         "%u from %u bytes\n", match_len - rep_len,
121                        skb->len);
122                 __skb_trim(skb, skb->len + rep_len - match_len);
123         }
124
125         /* fix IP hdr checksum information */
126         skb->nh.iph->tot_len = htons(skb->len);
127         ip_send_check(skb->nh.iph);
128 }
129
130 /* Unusual, but possible case. */
131 static int enlarge_skb(struct sk_buff **pskb, unsigned int extra)
132 {
133         struct sk_buff *nskb;
134
135         if ((*pskb)->len + extra > 65535)
136                 return 0;
137
138         nskb = skb_copy_expand(*pskb, skb_headroom(*pskb), extra, GFP_ATOMIC);
139         if (!nskb)
140                 return 0;
141
142         /* Transfer socket to new skb. */
143         if ((*pskb)->sk)
144                 skb_set_owner_w(nskb, (*pskb)->sk);
145 #ifdef CONFIG_NETFILTER_DEBUG
146         nskb->nf_debug = (*pskb)->nf_debug;
147 #endif
148         kfree_skb(*pskb);
149         *pskb = nskb;
150         return 1;
151 }
152
153 /* Generic function for mangling variable-length address changes inside
154  * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX
155  * command in FTP).
156  *
157  * Takes care about all the nasty sequence number changes, checksumming,
158  * skb enlargement, ...
159  *
160  * */
161 int 
162 ip_nat_mangle_tcp_packet(struct sk_buff **pskb,
163                          struct ip_conntrack *ct,
164                          enum ip_conntrack_info ctinfo,
165                          unsigned int match_offset,
166                          unsigned int match_len,
167                          const char *rep_buffer,
168                          unsigned int rep_len)
169 {
170         struct iphdr *iph;
171         struct tcphdr *tcph;
172         int datalen;
173
174         if (!skb_ip_make_writable(pskb, (*pskb)->len))
175                 return 0;
176
177         if (rep_len > match_len
178             && rep_len - match_len > skb_tailroom(*pskb)
179             && !enlarge_skb(pskb, rep_len - match_len))
180                 return 0;
181
182         SKB_LINEAR_ASSERT(*pskb);
183
184         iph = (*pskb)->nh.iph;
185         tcph = (void *)iph + iph->ihl*4;
186
187         mangle_contents(*pskb, iph->ihl*4 + tcph->doff*4,
188                         match_offset, match_len, rep_buffer, rep_len);
189
190         datalen = (*pskb)->len - iph->ihl*4;
191         tcph->check = 0;
192         tcph->check = tcp_v4_check(tcph, datalen, iph->saddr, iph->daddr,
193                                    csum_partial((char *)tcph, datalen, 0));
194
195         adjust_tcp_sequence(ntohl(tcph->seq),
196                             (int)rep_len - (int)match_len,
197                             ct, ctinfo);
198         return 1;
199 }
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 ip_nat_mangle_tcp_packet which
209  *       should be fairly easy to do.
210  */
211 int 
212 ip_nat_mangle_udp_packet(struct sk_buff **pskb,
213                          struct ip_conntrack *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 {
220         struct iphdr *iph;
221         struct udphdr *udph;
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_ip_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         mangle_contents(*pskb, iph->ihl*4 + sizeof(*udph),
240                         match_offset, match_len, rep_buffer, rep_len);
241
242         /* update the length of the UDP packet */
243         udph->len = htons((*pskb)->len - iph->ihl*4);
244
245         /* fix udp checksum if udp checksum was previously calculated */
246         if (udph->check) {
247                 int datalen = (*pskb)->len - iph->ihl * 4;
248                 udph->check = 0;
249                 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
250                                                 datalen, IPPROTO_UDP,
251                                                 csum_partial((char *)udph,
252                                                              datalen, 0));
253         }
254
255         return 1;
256 }
257
258 /* Adjust one found SACK option including checksum correction */
259 static void
260 sack_adjust(struct sk_buff *skb,
261             struct tcphdr *tcph, 
262             unsigned int sackoff,
263             unsigned int sackend,
264             struct ip_nat_seq *natseq)
265 {
266         while (sackoff < sackend) {
267                 struct tcp_sack_block *sack;
268                 u_int32_t new_start_seq, new_end_seq;
269
270                 sack = (void *)skb->data + sackoff;
271                 if (after(ntohl(sack->start_seq) - natseq->offset_before,
272                           natseq->correction_pos))
273                         new_start_seq = ntohl(sack->start_seq) 
274                                         - natseq->offset_after;
275                 else
276                         new_start_seq = ntohl(sack->start_seq) 
277                                         - natseq->offset_before;
278                 new_start_seq = htonl(new_start_seq);
279
280                 if (after(ntohl(sack->end_seq) - natseq->offset_before,
281                           natseq->correction_pos))
282                         new_end_seq = ntohl(sack->end_seq)
283                                       - natseq->offset_after;
284                 else
285                         new_end_seq = ntohl(sack->end_seq)
286                                       - natseq->offset_before;
287                 new_end_seq = htonl(new_end_seq);
288
289                 DEBUGP("sack_adjust: start_seq: %d->%d, end_seq: %d->%d\n",
290                         ntohl(sack->start_seq), new_start_seq,
291                         ntohl(sack->end_seq), new_end_seq);
292
293                 tcph->check = 
294                         ip_nat_cheat_check(~sack->start_seq, new_start_seq,
295                                            ip_nat_cheat_check(~sack->end_seq, 
296                                                               new_end_seq,
297                                                               tcph->check));
298                 sack->start_seq = new_start_seq;
299                 sack->end_seq = new_end_seq;
300                 sackoff += sizeof(*sack);
301         }
302 }
303
304 /* TCP SACK sequence number adjustment */
305 static inline unsigned int
306 ip_nat_sack_adjust(struct sk_buff **pskb,
307                    struct tcphdr *tcph,
308                    struct ip_conntrack *ct,
309                    enum ip_conntrack_info ctinfo)
310 {
311         unsigned int dir, optoff, optend;
312
313         optoff = (*pskb)->nh.iph->ihl*4 + sizeof(struct tcphdr);
314         optend = (*pskb)->nh.iph->ihl*4 + tcph->doff*4;
315
316         if (!skb_ip_make_writable(pskb, optend))
317                 return 0;
318
319         dir = CTINFO2DIR(ctinfo);
320
321         while (optoff < optend) {
322                 /* Usually: option, length. */
323                 unsigned char *op = (*pskb)->data + optoff;
324
325                 switch (op[0]) {
326                 case TCPOPT_EOL:
327                         return 1;
328                 case TCPOPT_NOP:
329                         optoff++;
330                         continue;
331                 default:
332                         /* no partial options */
333                         if (optoff + 1 == optend
334                             || optoff + op[1] > optend
335                             || op[1] < 2)
336                                 return 0;
337                         if (op[0] == TCPOPT_SACK
338                             && op[1] >= 2+TCPOLEN_SACK_PERBLOCK
339                             && ((op[1] - 2) % TCPOLEN_SACK_PERBLOCK) == 0)
340                                 sack_adjust(*pskb, tcph, optoff+2,
341                                             optoff+op[1],
342                                             &ct->nat.info.seq[!dir]);
343                         optoff += op[1];
344                 }
345         }
346         return 1;
347 }
348
349 /* TCP sequence number adjustment.  Returns true or false.  */
350 int
351 ip_nat_seq_adjust(struct sk_buff **pskb, 
352                   struct ip_conntrack *ct, 
353                   enum ip_conntrack_info ctinfo)
354 {
355         struct tcphdr *tcph;
356         int dir, newseq, newack;
357         struct ip_nat_seq *this_way, *other_way;        
358
359         dir = CTINFO2DIR(ctinfo);
360
361         this_way = &ct->nat.info.seq[dir];
362         other_way = &ct->nat.info.seq[!dir];
363
364         /* No adjustments to make?  Very common case. */
365         if (!this_way->offset_before && !this_way->offset_after
366             && !other_way->offset_before && !other_way->offset_after)
367                 return 1;
368
369         if (!skb_ip_make_writable(pskb, (*pskb)->nh.iph->ihl*4+sizeof(*tcph)))
370                 return 0;
371
372         tcph = (void *)(*pskb)->data + (*pskb)->nh.iph->ihl*4;
373         if (after(ntohl(tcph->seq), this_way->correction_pos))
374                 newseq = ntohl(tcph->seq) + this_way->offset_after;
375         else
376                 newseq = ntohl(tcph->seq) + this_way->offset_before;
377         newseq = htonl(newseq);
378
379         if (after(ntohl(tcph->ack_seq) - other_way->offset_before,
380                   other_way->correction_pos))
381                 newack = ntohl(tcph->ack_seq) - other_way->offset_after;
382         else
383                 newack = ntohl(tcph->ack_seq) - other_way->offset_before;
384         newack = htonl(newack);
385
386         tcph->check = ip_nat_cheat_check(~tcph->seq, newseq,
387                                          ip_nat_cheat_check(~tcph->ack_seq, 
388                                                             newack, 
389                                                             tcph->check));
390
391         DEBUGP("Adjusting sequence number from %u->%u, ack from %u->%u\n",
392                 ntohl(tcph->seq), ntohl(newseq), ntohl(tcph->ack_seq),
393                 ntohl(newack));
394
395         tcph->seq = newseq;
396         tcph->ack_seq = newack;
397
398         return ip_nat_sack_adjust(pskb, tcph, ct, ctinfo);
399 }
400
401 static inline int
402 helper_cmp(const struct ip_nat_helper *helper,
403            const struct ip_conntrack_tuple *tuple)
404 {
405         return ip_ct_tuple_mask_cmp(tuple, &helper->tuple, &helper->mask);
406 }
407
408 int ip_nat_helper_register(struct ip_nat_helper *me)
409 {
410         int ret = 0;
411
412         WRITE_LOCK(&ip_nat_lock);
413         if (LIST_FIND(&helpers, helper_cmp, struct ip_nat_helper *,&me->tuple))
414                 ret = -EBUSY;
415         else
416                 list_prepend(&helpers, me);
417         WRITE_UNLOCK(&ip_nat_lock);
418
419         return ret;
420 }
421
422 static int
423 kill_helper(const struct ip_conntrack *i, void *helper)
424 {
425         int ret;
426
427         READ_LOCK(&ip_nat_lock);
428         ret = (i->nat.info.helper == helper);
429         READ_UNLOCK(&ip_nat_lock);
430
431         return ret;
432 }
433
434 void ip_nat_helper_unregister(struct ip_nat_helper *me)
435 {
436         WRITE_LOCK(&ip_nat_lock);
437         /* Autoloading conntrack helper might have failed */
438         if (LIST_FIND(&helpers, helper_cmp, struct ip_nat_helper *,&me->tuple)) {
439                 LIST_DELETE(&helpers, me);
440         }
441         WRITE_UNLOCK(&ip_nat_lock);
442
443         /* Someone could be still looking at the helper in a bh. */
444         synchronize_net();
445
446         /* Find anything using it, and umm, kill them.  We can't turn
447            them into normal connections: if we've adjusted SYNs, then
448            they'll ackstorm.  So we just drop it.  We used to just
449            bump module count when a connection existed, but that
450            forces admins to gen fake RSTs or bounce box, either of
451            which is just a long-winded way of making things
452            worse. --RR */
453         ip_ct_selective_cleanup(kill_helper, me);
454 }