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