[NETFILTER]: nf_conntrack: move registration of __nf_ct_attach
[safe/jmp/linux-2.6] / 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
145         /* This is where we call the helper: as the packet goes out. */
146         ct = nf_ct_get(*pskb, &ctinfo);
147         if (ct && ct->helper) {
148                 unsigned int ret;
149                 ret = ct->helper->help(pskb,
150                                        (*pskb)->nh.raw - (*pskb)->data
151                                                        + (*pskb)->nh.iph->ihl*4,
152                                        ct, ctinfo);
153                 if (ret != NF_ACCEPT)
154                         return ret;
155         }
156         return NF_ACCEPT;
157 }
158
159 static unsigned int ipv4_conntrack_defrag(unsigned int hooknum,
160                                           struct sk_buff **pskb,
161                                           const struct net_device *in,
162                                           const struct net_device *out,
163                                           int (*okfn)(struct sk_buff *))
164 {
165 #if !defined(CONFIG_IP_NF_NAT) && !defined(CONFIG_IP_NF_NAT_MODULE)
166         /* Previously seen (loopback)?  Ignore.  Do this before
167            fragment check. */
168         if ((*pskb)->nfct)
169                 return NF_ACCEPT;
170 #endif
171
172         /* Gather fragments. */
173         if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
174                 *pskb = nf_ct_ipv4_gather_frags(*pskb,
175                                                 hooknum == NF_IP_PRE_ROUTING ?
176                                                 IP_DEFRAG_CONNTRACK_IN :
177                                                 IP_DEFRAG_CONNTRACK_OUT);
178                 if (!*pskb)
179                         return NF_STOLEN;
180         }
181         return NF_ACCEPT;
182 }
183
184 static unsigned int ipv4_conntrack_in(unsigned int hooknum,
185                                       struct sk_buff **pskb,
186                                       const struct net_device *in,
187                                       const struct net_device *out,
188                                       int (*okfn)(struct sk_buff *))
189 {
190         return nf_conntrack_in(PF_INET, hooknum, pskb);
191 }
192
193 static unsigned int ipv4_conntrack_local(unsigned int hooknum,
194                                          struct sk_buff **pskb,
195                                          const struct net_device *in,
196                                          const struct net_device *out,
197                                          int (*okfn)(struct sk_buff *))
198 {
199         /* root is playing with raw sockets. */
200         if ((*pskb)->len < sizeof(struct iphdr)
201             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
202                 if (net_ratelimit())
203                         printk("ipt_hook: happy cracking.\n");
204                 return NF_ACCEPT;
205         }
206         return nf_conntrack_in(PF_INET, hooknum, pskb);
207 }
208
209 /* Connection tracking may drop packets, but never alters them, so
210    make it the first hook. */
211 static struct nf_hook_ops ipv4_conntrack_defrag_ops = {
212         .hook           = ipv4_conntrack_defrag,
213         .owner          = THIS_MODULE,
214         .pf             = PF_INET,
215         .hooknum        = NF_IP_PRE_ROUTING,
216         .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
217 };
218
219 static struct nf_hook_ops ipv4_conntrack_in_ops = {
220         .hook           = ipv4_conntrack_in,
221         .owner          = THIS_MODULE,
222         .pf             = PF_INET,
223         .hooknum        = NF_IP_PRE_ROUTING,
224         .priority       = NF_IP_PRI_CONNTRACK,
225 };
226
227 static struct nf_hook_ops ipv4_conntrack_defrag_local_out_ops = {
228         .hook           = ipv4_conntrack_defrag,
229         .owner          = THIS_MODULE,
230         .pf             = PF_INET,
231         .hooknum        = NF_IP_LOCAL_OUT,
232         .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
233 };
234
235 static struct nf_hook_ops ipv4_conntrack_local_out_ops = {
236         .hook           = ipv4_conntrack_local,
237         .owner          = THIS_MODULE,
238         .pf             = PF_INET,
239         .hooknum        = NF_IP_LOCAL_OUT,
240         .priority       = NF_IP_PRI_CONNTRACK,
241 };
242
243 /* helpers */
244 static struct nf_hook_ops ipv4_conntrack_helper_out_ops = {
245         .hook           = ipv4_conntrack_help,
246         .owner          = THIS_MODULE,
247         .pf             = PF_INET,
248         .hooknum        = NF_IP_POST_ROUTING,
249         .priority       = NF_IP_PRI_CONNTRACK_HELPER,
250 };
251
252 static struct nf_hook_ops ipv4_conntrack_helper_in_ops = {
253         .hook           = ipv4_conntrack_help,
254         .owner          = THIS_MODULE,
255         .pf             = PF_INET,
256         .hooknum        = NF_IP_LOCAL_IN,
257         .priority       = NF_IP_PRI_CONNTRACK_HELPER,
258 };
259
260
261 /* Refragmenter; last chance. */
262 static struct nf_hook_ops ipv4_conntrack_out_ops = {
263         .hook           = ipv4_confirm,
264         .owner          = THIS_MODULE,
265         .pf             = PF_INET,
266         .hooknum        = NF_IP_POST_ROUTING,
267         .priority       = NF_IP_PRI_CONNTRACK_CONFIRM,
268 };
269
270 static struct nf_hook_ops ipv4_conntrack_local_in_ops = {
271         .hook           = ipv4_confirm,
272         .owner          = THIS_MODULE,
273         .pf             = PF_INET,
274         .hooknum        = NF_IP_LOCAL_IN,
275         .priority       = NF_IP_PRI_CONNTRACK_CONFIRM,
276 };
277
278 #ifdef CONFIG_SYSCTL
279 /* From nf_conntrack_proto_icmp.c */
280 extern unsigned int nf_ct_icmp_timeout;
281 static struct ctl_table_header *nf_ct_ipv4_sysctl_header;
282
283 static ctl_table nf_ct_sysctl_table[] = {
284         {
285                 .ctl_name       = NET_NF_CONNTRACK_ICMP_TIMEOUT,
286                 .procname       = "nf_conntrack_icmp_timeout",
287                 .data           = &nf_ct_icmp_timeout,
288                 .maxlen         = sizeof(unsigned int),
289                 .mode           = 0644,
290                 .proc_handler   = &proc_dointvec_jiffies,
291         },
292         { .ctl_name = 0 }
293 };
294
295 static ctl_table nf_ct_netfilter_table[] = {
296         {
297                 .ctl_name       = NET_NETFILTER,
298                 .procname       = "netfilter",
299                 .mode           = 0555,
300                 .child          = nf_ct_sysctl_table,
301         },
302         { .ctl_name = 0 }
303 };
304
305 static ctl_table nf_ct_net_table[] = {
306         {
307                 .ctl_name       = CTL_NET,
308                 .procname       = "net",
309                 .mode           = 0555,
310                 .child          = nf_ct_netfilter_table,
311         },
312         { .ctl_name = 0 }
313 };
314 #endif
315
316 /* Fast function for those who don't want to parse /proc (and I don't
317    blame them). */
318 /* Reversing the socket's dst/src point of view gives us the reply
319    mapping. */
320 static int
321 getorigdst(struct sock *sk, int optval, void __user *user, int *len)
322 {
323         struct inet_sock *inet = inet_sk(sk);
324         struct nf_conntrack_tuple_hash *h;
325         struct nf_conntrack_tuple tuple;
326         
327         NF_CT_TUPLE_U_BLANK(&tuple);
328         tuple.src.u3.ip = inet->rcv_saddr;
329         tuple.src.u.tcp.port = inet->sport;
330         tuple.dst.u3.ip = inet->daddr;
331         tuple.dst.u.tcp.port = inet->dport;
332         tuple.src.l3num = PF_INET;
333         tuple.dst.protonum = IPPROTO_TCP;
334
335         /* We only do TCP at the moment: is there a better way? */
336         if (strcmp(sk->sk_prot->name, "TCP")) {
337                 DEBUGP("SO_ORIGINAL_DST: Not a TCP socket\n");
338                 return -ENOPROTOOPT;
339         }
340
341         if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
342                 DEBUGP("SO_ORIGINAL_DST: len %u not %u\n",
343                        *len, sizeof(struct sockaddr_in));
344                 return -EINVAL;
345         }
346
347         h = nf_conntrack_find_get(&tuple, NULL);
348         if (h) {
349                 struct sockaddr_in sin;
350                 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
351
352                 sin.sin_family = AF_INET;
353                 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
354                         .tuple.dst.u.tcp.port;
355                 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
356                         .tuple.dst.u3.ip;
357
358                 DEBUGP("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
359                        NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
360                 nf_ct_put(ct);
361                 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
362                         return -EFAULT;
363                 else
364                         return 0;
365         }
366         DEBUGP("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
367                NIPQUAD(tuple.src.u3.ip), ntohs(tuple.src.u.tcp.port),
368                NIPQUAD(tuple.dst.u3.ip), ntohs(tuple.dst.u.tcp.port));
369         return -ENOENT;
370 }
371
372 #if defined(CONFIG_NF_CT_NETLINK) || \
373     defined(CONFIG_NF_CT_NETLINK_MODULE)
374
375 #include <linux/netfilter/nfnetlink.h>
376 #include <linux/netfilter/nfnetlink_conntrack.h>
377
378 static int ipv4_tuple_to_nfattr(struct sk_buff *skb,
379                                 const struct nf_conntrack_tuple *tuple)
380 {
381         NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t),
382                 &tuple->src.u3.ip);
383         NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t),
384                 &tuple->dst.u3.ip);
385         return 0;
386
387 nfattr_failure:
388         return -1;
389 }
390
391 static const size_t cta_min_ip[CTA_IP_MAX] = {
392         [CTA_IP_V4_SRC-1]       = sizeof(u_int32_t),
393         [CTA_IP_V4_DST-1]       = sizeof(u_int32_t),
394 };
395
396 static int ipv4_nfattr_to_tuple(struct nfattr *tb[],
397                                 struct nf_conntrack_tuple *t)
398 {
399         if (!tb[CTA_IP_V4_SRC-1] || !tb[CTA_IP_V4_DST-1])
400                 return -EINVAL;
401
402         if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
403                 return -EINVAL;
404
405         t->src.u3.ip =
406                 *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
407         t->dst.u3.ip =
408                 *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
409
410         return 0;
411 }
412 #endif
413
414 static struct nf_sockopt_ops so_getorigdst = {
415         .pf             = PF_INET,
416         .get_optmin     = SO_ORIGINAL_DST,
417         .get_optmax     = SO_ORIGINAL_DST+1,
418         .get            = &getorigdst,
419 };
420
421 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 = {
422         .l3proto         = PF_INET,
423         .name            = "ipv4",
424         .pkt_to_tuple    = ipv4_pkt_to_tuple,
425         .invert_tuple    = ipv4_invert_tuple,
426         .print_tuple     = ipv4_print_tuple,
427         .print_conntrack = ipv4_print_conntrack,
428         .prepare         = ipv4_prepare,
429         .get_features    = ipv4_get_features,
430 #if defined(CONFIG_NF_CT_NETLINK) || \
431     defined(CONFIG_NF_CT_NETLINK_MODULE)
432         .tuple_to_nfattr = ipv4_tuple_to_nfattr,
433         .nfattr_to_tuple = ipv4_nfattr_to_tuple,
434 #endif
435         .me              = THIS_MODULE,
436 };
437
438 extern struct nf_conntrack_protocol nf_conntrack_protocol_tcp4;
439 extern struct nf_conntrack_protocol nf_conntrack_protocol_udp4;
440 extern struct nf_conntrack_protocol nf_conntrack_protocol_icmp;
441 static int init_or_cleanup(int init)
442 {
443         int ret = 0;
444
445         if (!init) goto cleanup;
446
447         ret = nf_register_sockopt(&so_getorigdst);
448         if (ret < 0) {
449                 printk(KERN_ERR "Unable to register netfilter socket option\n");
450                 goto cleanup_nothing;
451         }
452
453         ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_tcp4);
454         if (ret < 0) {
455                 printk("nf_conntrack_ipv4: can't register tcp.\n");
456                 goto cleanup_sockopt;
457         }
458
459         ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_udp4);
460         if (ret < 0) {
461                 printk("nf_conntrack_ipv4: can't register udp.\n");
462                 goto cleanup_tcp;
463         }
464
465         ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_icmp);
466         if (ret < 0) {
467                 printk("nf_conntrack_ipv4: can't register icmp.\n");
468                 goto cleanup_udp;
469         }
470
471         ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4);
472         if (ret < 0) {
473                 printk("nf_conntrack_ipv4: can't register ipv4\n");
474                 goto cleanup_icmp;
475         }
476
477         ret = nf_register_hook(&ipv4_conntrack_defrag_ops);
478         if (ret < 0) {
479                 printk("nf_conntrack_ipv4: can't register pre-routing defrag hook.\n");
480                 goto cleanup_ipv4;
481         }
482         ret = nf_register_hook(&ipv4_conntrack_defrag_local_out_ops);
483         if (ret < 0) {
484                 printk("nf_conntrack_ipv4: can't register local_out defrag hook.\n");
485                 goto cleanup_defragops;
486         }
487
488         ret = nf_register_hook(&ipv4_conntrack_in_ops);
489         if (ret < 0) {
490                 printk("nf_conntrack_ipv4: can't register pre-routing hook.\n");
491                 goto cleanup_defraglocalops;
492         }
493
494         ret = nf_register_hook(&ipv4_conntrack_local_out_ops);
495         if (ret < 0) {
496                 printk("nf_conntrack_ipv4: can't register local out hook.\n");
497                 goto cleanup_inops;
498         }
499
500         ret = nf_register_hook(&ipv4_conntrack_helper_in_ops);
501         if (ret < 0) {
502                 printk("nf_conntrack_ipv4: can't register local helper hook.\n");
503                 goto cleanup_inandlocalops;
504         }
505
506         ret = nf_register_hook(&ipv4_conntrack_helper_out_ops);
507         if (ret < 0) {
508                 printk("nf_conntrack_ipv4: can't register postrouting helper hook.\n");
509                 goto cleanup_helperinops;
510         }
511
512         ret = nf_register_hook(&ipv4_conntrack_out_ops);
513         if (ret < 0) {
514                 printk("nf_conntrack_ipv4: can't register post-routing hook.\n");
515                 goto cleanup_helperoutops;
516         }
517
518         ret = nf_register_hook(&ipv4_conntrack_local_in_ops);
519         if (ret < 0) {
520                 printk("nf_conntrack_ipv4: can't register local in hook.\n");
521                 goto cleanup_inoutandlocalops;
522         }
523
524 #ifdef CONFIG_SYSCTL
525         nf_ct_ipv4_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
526         if (nf_ct_ipv4_sysctl_header == NULL) {
527                 printk("nf_conntrack: can't register to sysctl.\n");
528                 ret = -ENOMEM;
529                 goto cleanup_localinops;
530         }
531 #endif
532         return ret;
533
534  cleanup:
535         synchronize_net();
536 #ifdef CONFIG_SYSCTL
537         unregister_sysctl_table(nf_ct_ipv4_sysctl_header);
538  cleanup_localinops:
539 #endif
540         nf_unregister_hook(&ipv4_conntrack_local_in_ops);
541  cleanup_inoutandlocalops:
542         nf_unregister_hook(&ipv4_conntrack_out_ops);
543  cleanup_helperoutops:
544         nf_unregister_hook(&ipv4_conntrack_helper_out_ops);
545  cleanup_helperinops:
546         nf_unregister_hook(&ipv4_conntrack_helper_in_ops);
547  cleanup_inandlocalops:
548         nf_unregister_hook(&ipv4_conntrack_local_out_ops);
549  cleanup_inops:
550         nf_unregister_hook(&ipv4_conntrack_in_ops);
551  cleanup_defraglocalops:
552         nf_unregister_hook(&ipv4_conntrack_defrag_local_out_ops);
553  cleanup_defragops:
554         nf_unregister_hook(&ipv4_conntrack_defrag_ops);
555  cleanup_ipv4:
556         nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
557  cleanup_icmp:
558         nf_conntrack_protocol_unregister(&nf_conntrack_protocol_icmp);
559  cleanup_udp:
560         nf_conntrack_protocol_unregister(&nf_conntrack_protocol_udp4);
561  cleanup_tcp:
562         nf_conntrack_protocol_unregister(&nf_conntrack_protocol_tcp4);
563  cleanup_sockopt:
564         nf_unregister_sockopt(&so_getorigdst);
565  cleanup_nothing:
566         return ret;
567 }
568
569 MODULE_LICENSE("GPL");
570
571 static int __init init(void)
572 {
573         need_conntrack();
574         return init_or_cleanup(1);
575 }
576
577 static void __exit fini(void)
578 {
579         init_or_cleanup(0);
580 }
581
582 module_init(init);
583 module_exit(fini);
584
585 EXPORT_SYMBOL(nf_ct_ipv4_gather_frags);