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