cb9c661f3f331c5fb64f805bbfdff5f8a7e70b3e
[linux-2.6.git] / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9  *      - move L3 protocol dependent part to this file.
10  * 23 Mar 2004: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
11  *      - add get_features() to support various size of conntrack
12  *        structures.
13  *
14  * Derived from net/ipv4/netfilter/ip_conntrack_standalone.c
15  */
16
17 #include <linux/config.h>
18 #include <linux/types.h>
19 #include <linux/ip.h>
20 #include <linux/netfilter.h>
21 #include <linux/module.h>
22 #include <linux/skbuff.h>
23 #include <linux/icmp.h>
24 #include <linux/sysctl.h>
25 #include <net/route.h>
26 #include <net/ip.h>
27
28 #include <linux/netfilter_ipv4.h>
29 #include <net/netfilter/nf_conntrack.h>
30 #include <net/netfilter/nf_conntrack_helper.h>
31 #include <net/netfilter/nf_conntrack_protocol.h>
32 #include <net/netfilter/nf_conntrack_l3proto.h>
33 #include <net/netfilter/nf_conntrack_core.h>
34 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
35
36 #if 0
37 #define DEBUGP printk
38 #else
39 #define DEBUGP(format, args...)
40 #endif
41
42 DECLARE_PER_CPU(struct nf_conntrack_stat, nf_conntrack_stat);
43
44 static int ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
45                              struct nf_conntrack_tuple *tuple)
46 {
47         u_int32_t _addrs[2], *ap;
48         ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr),
49                                 sizeof(u_int32_t) * 2, _addrs);
50         if (ap == NULL)
51                 return 0;
52
53         tuple->src.u3.ip = ap[0];
54         tuple->dst.u3.ip = ap[1];
55
56         return 1;
57 }
58
59 static int ipv4_invert_tuple(struct nf_conntrack_tuple *tuple,
60                            const struct nf_conntrack_tuple *orig)
61 {
62         tuple->src.u3.ip = orig->dst.u3.ip;
63         tuple->dst.u3.ip = orig->src.u3.ip;
64
65         return 1;
66 }
67
68 static int ipv4_print_tuple(struct seq_file *s,
69                             const struct nf_conntrack_tuple *tuple)
70 {
71         return seq_printf(s, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
72                           NIPQUAD(tuple->src.u3.ip),
73                           NIPQUAD(tuple->dst.u3.ip));
74 }
75
76 static int ipv4_print_conntrack(struct seq_file *s,
77                                 const struct nf_conn *conntrack)
78 {
79         return 0;
80 }
81
82 /* Returns new sk_buff, or NULL */
83 static struct sk_buff *
84 nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user)
85 {
86         skb_orphan(skb);
87
88         local_bh_disable();
89         skb = ip_defrag(skb, user);
90         local_bh_enable();
91
92         if (skb)
93                 ip_send_check(skb->nh.iph);
94
95         return skb;
96 }
97
98 static int
99 ipv4_prepare(struct sk_buff **pskb, unsigned int hooknum, unsigned int *dataoff,
100              u_int8_t *protonum)
101 {
102         /* Never happen */
103         if ((*pskb)->nh.iph->frag_off & htons(IP_OFFSET)) {
104                 if (net_ratelimit()) {
105                         printk(KERN_ERR "ipv4_prepare: Frag of proto %u (hook=%u)\n",
106                         (*pskb)->nh.iph->protocol, hooknum);
107                 }
108                 return -NF_DROP;
109         }
110
111         *dataoff = (*pskb)->nh.raw - (*pskb)->data + (*pskb)->nh.iph->ihl*4;
112         *protonum = (*pskb)->nh.iph->protocol;
113
114         return NF_ACCEPT;
115 }
116
117 int nat_module_is_loaded = 0;
118 static u_int32_t ipv4_get_features(const struct nf_conntrack_tuple *tuple)
119 {
120         if (nat_module_is_loaded)
121                 return NF_CT_F_NAT;
122
123         return NF_CT_F_BASIC;
124 }
125
126 static unsigned int ipv4_confirm(unsigned int hooknum,
127                                  struct sk_buff **pskb,
128                                  const struct net_device *in,
129                                  const struct net_device *out,
130                                  int (*okfn)(struct sk_buff *))
131 {
132         /* We've seen it coming out the other side: confirm it */
133         return nf_conntrack_confirm(pskb);
134 }
135
136 static unsigned int ipv4_conntrack_help(unsigned int hooknum,
137                                       struct sk_buff **pskb,
138                                       const struct net_device *in,
139                                       const struct net_device *out,
140                                       int (*okfn)(struct sk_buff *))
141 {
142         struct nf_conn *ct;
143         enum ip_conntrack_info ctinfo;
144         struct nf_conn_help *help;
145
146         /* This is where we call the helper: as the packet goes out. */
147         ct = nf_ct_get(*pskb, &ctinfo);
148         if (!ct)
149                 return NF_ACCEPT;
150
151         help = nfct_help(ct);
152         if (!help || !help->helper)
153                 return NF_ACCEPT;
154
155         return help->helper->help(pskb,
156                                (*pskb)->nh.raw - (*pskb)->data
157                                                + (*pskb)->nh.iph->ihl*4,
158                                ct, ctinfo);
159 }
160
161 static unsigned int ipv4_conntrack_defrag(unsigned int hooknum,
162                                           struct sk_buff **pskb,
163                                           const struct net_device *in,
164                                           const struct net_device *out,
165                                           int (*okfn)(struct sk_buff *))
166 {
167 #if !defined(CONFIG_IP_NF_NAT) && !defined(CONFIG_IP_NF_NAT_MODULE)
168         /* Previously seen (loopback)?  Ignore.  Do this before
169            fragment check. */
170         if ((*pskb)->nfct)
171                 return NF_ACCEPT;
172 #endif
173
174         /* Gather fragments. */
175         if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
176                 *pskb = nf_ct_ipv4_gather_frags(*pskb,
177                                                 hooknum == NF_IP_PRE_ROUTING ?
178                                                 IP_DEFRAG_CONNTRACK_IN :
179                                                 IP_DEFRAG_CONNTRACK_OUT);
180                 if (!*pskb)
181                         return NF_STOLEN;
182         }
183         return NF_ACCEPT;
184 }
185
186 static unsigned int ipv4_conntrack_in(unsigned int hooknum,
187                                       struct sk_buff **pskb,
188                                       const struct net_device *in,
189                                       const struct net_device *out,
190                                       int (*okfn)(struct sk_buff *))
191 {
192         return nf_conntrack_in(PF_INET, hooknum, pskb);
193 }
194
195 static unsigned int ipv4_conntrack_local(unsigned int hooknum,
196                                          struct sk_buff **pskb,
197                                          const struct net_device *in,
198                                          const struct net_device *out,
199                                          int (*okfn)(struct sk_buff *))
200 {
201         /* root is playing with raw sockets. */
202         if ((*pskb)->len < sizeof(struct iphdr)
203             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
204                 if (net_ratelimit())
205                         printk("ipt_hook: happy cracking.\n");
206                 return NF_ACCEPT;
207         }
208         return nf_conntrack_in(PF_INET, hooknum, pskb);
209 }
210
211 /* Connection tracking may drop packets, but never alters them, so
212    make it the first hook. */
213 static struct nf_hook_ops ipv4_conntrack_defrag_ops = {
214         .hook           = ipv4_conntrack_defrag,
215         .owner          = THIS_MODULE,
216         .pf             = PF_INET,
217         .hooknum        = NF_IP_PRE_ROUTING,
218         .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
219 };
220
221 static struct nf_hook_ops ipv4_conntrack_in_ops = {
222         .hook           = ipv4_conntrack_in,
223         .owner          = THIS_MODULE,
224         .pf             = PF_INET,
225         .hooknum        = NF_IP_PRE_ROUTING,
226         .priority       = NF_IP_PRI_CONNTRACK,
227 };
228
229 static struct nf_hook_ops ipv4_conntrack_defrag_local_out_ops = {
230         .hook           = ipv4_conntrack_defrag,
231         .owner          = THIS_MODULE,
232         .pf             = PF_INET,
233         .hooknum        = NF_IP_LOCAL_OUT,
234         .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
235 };
236
237 static struct nf_hook_ops ipv4_conntrack_local_out_ops = {
238         .hook           = ipv4_conntrack_local,
239         .owner          = THIS_MODULE,
240         .pf             = PF_INET,
241         .hooknum        = NF_IP_LOCAL_OUT,
242         .priority       = NF_IP_PRI_CONNTRACK,
243 };
244
245 /* helpers */
246 static struct nf_hook_ops ipv4_conntrack_helper_out_ops = {
247         .hook           = ipv4_conntrack_help,
248         .owner          = THIS_MODULE,
249         .pf             = PF_INET,
250         .hooknum        = NF_IP_POST_ROUTING,
251         .priority       = NF_IP_PRI_CONNTRACK_HELPER,
252 };
253
254 static struct nf_hook_ops ipv4_conntrack_helper_in_ops = {
255         .hook           = ipv4_conntrack_help,
256         .owner          = THIS_MODULE,
257         .pf             = PF_INET,
258         .hooknum        = NF_IP_LOCAL_IN,
259         .priority       = NF_IP_PRI_CONNTRACK_HELPER,
260 };
261
262
263 /* Refragmenter; last chance. */
264 static struct nf_hook_ops ipv4_conntrack_out_ops = {
265         .hook           = ipv4_confirm,
266         .owner          = THIS_MODULE,
267         .pf             = PF_INET,
268         .hooknum        = NF_IP_POST_ROUTING,
269         .priority       = NF_IP_PRI_CONNTRACK_CONFIRM,
270 };
271
272 static struct nf_hook_ops ipv4_conntrack_local_in_ops = {
273         .hook           = ipv4_confirm,
274         .owner          = THIS_MODULE,
275         .pf             = PF_INET,
276         .hooknum        = NF_IP_LOCAL_IN,
277         .priority       = NF_IP_PRI_CONNTRACK_CONFIRM,
278 };
279
280 #ifdef CONFIG_SYSCTL
281 /* From nf_conntrack_proto_icmp.c */
282 extern unsigned int nf_ct_icmp_timeout;
283 static struct ctl_table_header *nf_ct_ipv4_sysctl_header;
284
285 static ctl_table nf_ct_sysctl_table[] = {
286         {
287                 .ctl_name       = NET_NF_CONNTRACK_ICMP_TIMEOUT,
288                 .procname       = "nf_conntrack_icmp_timeout",
289                 .data           = &nf_ct_icmp_timeout,
290                 .maxlen         = sizeof(unsigned int),
291                 .mode           = 0644,
292                 .proc_handler   = &proc_dointvec_jiffies,
293         },
294         { .ctl_name = 0 }
295 };
296
297 static ctl_table nf_ct_netfilter_table[] = {
298         {
299                 .ctl_name       = NET_NETFILTER,
300                 .procname       = "netfilter",
301                 .mode           = 0555,
302                 .child          = nf_ct_sysctl_table,
303         },
304         { .ctl_name = 0 }
305 };
306
307 static ctl_table nf_ct_net_table[] = {
308         {
309                 .ctl_name       = CTL_NET,
310                 .procname       = "net",
311                 .mode           = 0555,
312                 .child          = nf_ct_netfilter_table,
313         },
314         { .ctl_name = 0 }
315 };
316 #endif
317
318 /* Fast function for those who don't want to parse /proc (and I don't
319    blame them). */
320 /* Reversing the socket's dst/src point of view gives us the reply
321    mapping. */
322 static int
323 getorigdst(struct sock *sk, int optval, void __user *user, int *len)
324 {
325         struct inet_sock *inet = inet_sk(sk);
326         struct nf_conntrack_tuple_hash *h;
327         struct nf_conntrack_tuple tuple;
328         
329         NF_CT_TUPLE_U_BLANK(&tuple);
330         tuple.src.u3.ip = inet->rcv_saddr;
331         tuple.src.u.tcp.port = inet->sport;
332         tuple.dst.u3.ip = inet->daddr;
333         tuple.dst.u.tcp.port = inet->dport;
334         tuple.src.l3num = PF_INET;
335         tuple.dst.protonum = IPPROTO_TCP;
336
337         /* We only do TCP at the moment: is there a better way? */
338         if (strcmp(sk->sk_prot->name, "TCP")) {
339                 DEBUGP("SO_ORIGINAL_DST: Not a TCP socket\n");
340                 return -ENOPROTOOPT;
341         }
342
343         if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
344                 DEBUGP("SO_ORIGINAL_DST: len %u not %u\n",
345                        *len, sizeof(struct sockaddr_in));
346                 return -EINVAL;
347         }
348
349         h = nf_conntrack_find_get(&tuple, NULL);
350         if (h) {
351                 struct sockaddr_in sin;
352                 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
353
354                 sin.sin_family = AF_INET;
355                 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
356                         .tuple.dst.u.tcp.port;
357                 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
358                         .tuple.dst.u3.ip;
359
360                 DEBUGP("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
361                        NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
362                 nf_ct_put(ct);
363                 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
364                         return -EFAULT;
365                 else
366                         return 0;
367         }
368         DEBUGP("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
369                NIPQUAD(tuple.src.u3.ip), ntohs(tuple.src.u.tcp.port),
370                NIPQUAD(tuple.dst.u3.ip), ntohs(tuple.dst.u.tcp.port));
371         return -ENOENT;
372 }
373
374 #if defined(CONFIG_NF_CT_NETLINK) || \
375     defined(CONFIG_NF_CT_NETLINK_MODULE)
376
377 #include <linux/netfilter/nfnetlink.h>
378 #include <linux/netfilter/nfnetlink_conntrack.h>
379
380 static int ipv4_tuple_to_nfattr(struct sk_buff *skb,
381                                 const struct nf_conntrack_tuple *tuple)
382 {
383         NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t),
384                 &tuple->src.u3.ip);
385         NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t),
386                 &tuple->dst.u3.ip);
387         return 0;
388
389 nfattr_failure:
390         return -1;
391 }
392
393 static const size_t cta_min_ip[CTA_IP_MAX] = {
394         [CTA_IP_V4_SRC-1]       = sizeof(u_int32_t),
395         [CTA_IP_V4_DST-1]       = sizeof(u_int32_t),
396 };
397
398 static int ipv4_nfattr_to_tuple(struct nfattr *tb[],
399                                 struct nf_conntrack_tuple *t)
400 {
401         if (!tb[CTA_IP_V4_SRC-1] || !tb[CTA_IP_V4_DST-1])
402                 return -EINVAL;
403
404         if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
405                 return -EINVAL;
406
407         t->src.u3.ip =
408                 *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
409         t->dst.u3.ip =
410                 *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
411
412         return 0;
413 }
414 #endif
415
416 static struct nf_sockopt_ops so_getorigdst = {
417         .pf             = PF_INET,
418         .get_optmin     = SO_ORIGINAL_DST,
419         .get_optmax     = SO_ORIGINAL_DST+1,
420         .get            = &getorigdst,
421 };
422
423 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 = {
424         .l3proto         = PF_INET,
425         .name            = "ipv4",
426         .pkt_to_tuple    = ipv4_pkt_to_tuple,
427         .invert_tuple    = ipv4_invert_tuple,
428         .print_tuple     = ipv4_print_tuple,
429         .print_conntrack = ipv4_print_conntrack,
430         .prepare         = ipv4_prepare,
431         .get_features    = ipv4_get_features,
432 #if defined(CONFIG_NF_CT_NETLINK) || \
433     defined(CONFIG_NF_CT_NETLINK_MODULE)
434         .tuple_to_nfattr = ipv4_tuple_to_nfattr,
435         .nfattr_to_tuple = ipv4_nfattr_to_tuple,
436 #endif
437         .me              = THIS_MODULE,
438 };
439
440 extern struct nf_conntrack_protocol nf_conntrack_protocol_tcp4;
441 extern struct nf_conntrack_protocol nf_conntrack_protocol_udp4;
442 extern struct nf_conntrack_protocol nf_conntrack_protocol_icmp;
443 static int init_or_cleanup(int init)
444 {
445         int ret = 0;
446
447         if (!init) goto cleanup;
448
449         ret = nf_register_sockopt(&so_getorigdst);
450         if (ret < 0) {
451                 printk(KERN_ERR "Unable to register netfilter socket option\n");
452                 goto cleanup_nothing;
453         }
454
455         ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_tcp4);
456         if (ret < 0) {
457                 printk("nf_conntrack_ipv4: can't register tcp.\n");
458                 goto cleanup_sockopt;
459         }
460
461         ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_udp4);
462         if (ret < 0) {
463                 printk("nf_conntrack_ipv4: can't register udp.\n");
464                 goto cleanup_tcp;
465         }
466
467         ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_icmp);
468         if (ret < 0) {
469                 printk("nf_conntrack_ipv4: can't register icmp.\n");
470                 goto cleanup_udp;
471         }
472
473         ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4);
474         if (ret < 0) {
475                 printk("nf_conntrack_ipv4: can't register ipv4\n");
476                 goto cleanup_icmp;
477         }
478
479         ret = nf_register_hook(&ipv4_conntrack_defrag_ops);
480         if (ret < 0) {
481                 printk("nf_conntrack_ipv4: can't register pre-routing defrag hook.\n");
482                 goto cleanup_ipv4;
483         }
484         ret = nf_register_hook(&ipv4_conntrack_defrag_local_out_ops);
485         if (ret < 0) {
486                 printk("nf_conntrack_ipv4: can't register local_out defrag hook.\n");
487                 goto cleanup_defragops;
488         }
489
490         ret = nf_register_hook(&ipv4_conntrack_in_ops);
491         if (ret < 0) {
492                 printk("nf_conntrack_ipv4: can't register pre-routing hook.\n");
493                 goto cleanup_defraglocalops;
494         }
495
496         ret = nf_register_hook(&ipv4_conntrack_local_out_ops);
497         if (ret < 0) {
498                 printk("nf_conntrack_ipv4: can't register local out hook.\n");
499                 goto cleanup_inops;
500         }
501
502         ret = nf_register_hook(&ipv4_conntrack_helper_in_ops);
503         if (ret < 0) {
504                 printk("nf_conntrack_ipv4: can't register local helper hook.\n");
505                 goto cleanup_inandlocalops;
506         }
507
508         ret = nf_register_hook(&ipv4_conntrack_helper_out_ops);
509         if (ret < 0) {
510                 printk("nf_conntrack_ipv4: can't register postrouting helper hook.\n");
511                 goto cleanup_helperinops;
512         }
513
514         ret = nf_register_hook(&ipv4_conntrack_out_ops);
515         if (ret < 0) {
516                 printk("nf_conntrack_ipv4: can't register post-routing hook.\n");
517                 goto cleanup_helperoutops;
518         }
519
520         ret = nf_register_hook(&ipv4_conntrack_local_in_ops);
521         if (ret < 0) {
522                 printk("nf_conntrack_ipv4: can't register local in hook.\n");
523                 goto cleanup_inoutandlocalops;
524         }
525
526 #ifdef CONFIG_SYSCTL
527         nf_ct_ipv4_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
528         if (nf_ct_ipv4_sysctl_header == NULL) {
529                 printk("nf_conntrack: can't register to sysctl.\n");
530                 ret = -ENOMEM;
531                 goto cleanup_localinops;
532         }
533 #endif
534         return ret;
535
536  cleanup:
537         synchronize_net();
538 #ifdef CONFIG_SYSCTL
539         unregister_sysctl_table(nf_ct_ipv4_sysctl_header);
540  cleanup_localinops:
541 #endif
542         nf_unregister_hook(&ipv4_conntrack_local_in_ops);
543  cleanup_inoutandlocalops:
544         nf_unregister_hook(&ipv4_conntrack_out_ops);
545  cleanup_helperoutops:
546         nf_unregister_hook(&ipv4_conntrack_helper_out_ops);
547  cleanup_helperinops:
548         nf_unregister_hook(&ipv4_conntrack_helper_in_ops);
549  cleanup_inandlocalops:
550         nf_unregister_hook(&ipv4_conntrack_local_out_ops);
551  cleanup_inops:
552         nf_unregister_hook(&ipv4_conntrack_in_ops);
553  cleanup_defraglocalops:
554         nf_unregister_hook(&ipv4_conntrack_defrag_local_out_ops);
555  cleanup_defragops:
556         nf_unregister_hook(&ipv4_conntrack_defrag_ops);
557  cleanup_ipv4:
558         nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
559  cleanup_icmp:
560         nf_conntrack_protocol_unregister(&nf_conntrack_protocol_icmp);
561  cleanup_udp:
562         nf_conntrack_protocol_unregister(&nf_conntrack_protocol_udp4);
563  cleanup_tcp:
564         nf_conntrack_protocol_unregister(&nf_conntrack_protocol_tcp4);
565  cleanup_sockopt:
566         nf_unregister_sockopt(&so_getorigdst);
567  cleanup_nothing:
568         return ret;
569 }
570
571 MODULE_LICENSE("GPL");
572
573 static int __init init(void)
574 {
575         need_conntrack();
576         return init_or_cleanup(1);
577 }
578
579 static void __exit fini(void)
580 {
581         init_or_cleanup(0);
582 }
583
584 module_init(init);
585 module_exit(fini);
586
587 EXPORT_SYMBOL(nf_ct_ipv4_gather_frags);