Merge branch 'e1000-fixes' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[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 #if 0
28 #define DEBUGP printk
29 #else
30 #define DEBUGP(format, args...)
31 #endif
32
33 static int ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
34                              struct nf_conntrack_tuple *tuple)
35 {
36         __be32 _addrs[2], *ap;
37         ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr),
38                                 sizeof(u_int32_t) * 2, _addrs);
39         if (ap == NULL)
40                 return 0;
41
42         tuple->src.u3.ip = ap[0];
43         tuple->dst.u3.ip = ap[1];
44
45         return 1;
46 }
47
48 static int ipv4_invert_tuple(struct nf_conntrack_tuple *tuple,
49                            const struct nf_conntrack_tuple *orig)
50 {
51         tuple->src.u3.ip = orig->dst.u3.ip;
52         tuple->dst.u3.ip = orig->src.u3.ip;
53
54         return 1;
55 }
56
57 static int ipv4_print_tuple(struct seq_file *s,
58                             const struct nf_conntrack_tuple *tuple)
59 {
60         return seq_printf(s, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
61                           NIPQUAD(tuple->src.u3.ip),
62                           NIPQUAD(tuple->dst.u3.ip));
63 }
64
65 static int ipv4_print_conntrack(struct seq_file *s,
66                                 const struct nf_conn *conntrack)
67 {
68         return 0;
69 }
70
71 /* Returns new sk_buff, or NULL */
72 static struct sk_buff *
73 nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user)
74 {
75         skb_orphan(skb);
76
77         local_bh_disable();
78         skb = ip_defrag(skb, user);
79         local_bh_enable();
80
81         if (skb)
82                 ip_send_check(ip_hdr(skb));
83
84         return skb;
85 }
86
87 static int
88 ipv4_prepare(struct sk_buff **pskb, unsigned int hooknum, unsigned int *dataoff,
89              u_int8_t *protonum)
90 {
91         /* Never happen */
92         if (ip_hdr(*pskb)->frag_off & htons(IP_OFFSET)) {
93                 if (net_ratelimit()) {
94                         printk(KERN_ERR "ipv4_prepare: Frag of proto %u (hook=%u)\n",
95                         ip_hdr(*pskb)->protocol, hooknum);
96                 }
97                 return -NF_DROP;
98         }
99
100         *dataoff = skb_network_offset(*pskb) + ip_hdrlen(*pskb);
101         *protonum = ip_hdr(*pskb)->protocol;
102
103         return NF_ACCEPT;
104 }
105
106 int nf_nat_module_is_loaded = 0;
107 EXPORT_SYMBOL_GPL(nf_nat_module_is_loaded);
108
109 static u_int32_t ipv4_get_features(const struct nf_conntrack_tuple *tuple)
110 {
111         if (nf_nat_module_is_loaded)
112                 return NF_CT_F_NAT;
113
114         return NF_CT_F_BASIC;
115 }
116
117 static unsigned int ipv4_confirm(unsigned int hooknum,
118                                  struct sk_buff **pskb,
119                                  const struct net_device *in,
120                                  const struct net_device *out,
121                                  int (*okfn)(struct sk_buff *))
122 {
123         /* We've seen it coming out the other side: confirm it */
124         return nf_conntrack_confirm(pskb);
125 }
126
127 static unsigned int ipv4_conntrack_help(unsigned int hooknum,
128                                       struct sk_buff **pskb,
129                                       const struct net_device *in,
130                                       const struct net_device *out,
131                                       int (*okfn)(struct sk_buff *))
132 {
133         struct nf_conn *ct;
134         enum ip_conntrack_info ctinfo;
135         struct nf_conn_help *help;
136
137         /* This is where we call the helper: as the packet goes out. */
138         ct = nf_ct_get(*pskb, &ctinfo);
139         if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
140                 return NF_ACCEPT;
141
142         help = nfct_help(ct);
143         if (!help || !help->helper)
144                 return NF_ACCEPT;
145
146         return help->helper->help(pskb,
147                                   skb_network_offset(*pskb) + ip_hdrlen(*pskb),
148                                   ct, ctinfo);
149 }
150
151 static unsigned int ipv4_conntrack_defrag(unsigned int hooknum,
152                                           struct sk_buff **pskb,
153                                           const struct net_device *in,
154                                           const struct net_device *out,
155                                           int (*okfn)(struct sk_buff *))
156 {
157 #if !defined(CONFIG_IP_NF_NAT) && !defined(CONFIG_IP_NF_NAT_MODULE)
158         /* Previously seen (loopback)?  Ignore.  Do this before
159            fragment check. */
160         if ((*pskb)->nfct)
161                 return NF_ACCEPT;
162 #endif
163
164         /* Gather fragments. */
165         if (ip_hdr(*pskb)->frag_off & htons(IP_MF | IP_OFFSET)) {
166                 *pskb = nf_ct_ipv4_gather_frags(*pskb,
167                                                 hooknum == NF_IP_PRE_ROUTING ?
168                                                 IP_DEFRAG_CONNTRACK_IN :
169                                                 IP_DEFRAG_CONNTRACK_OUT);
170                 if (!*pskb)
171                         return NF_STOLEN;
172         }
173         return NF_ACCEPT;
174 }
175
176 static unsigned int ipv4_conntrack_in(unsigned int hooknum,
177                                       struct sk_buff **pskb,
178                                       const struct net_device *in,
179                                       const struct net_device *out,
180                                       int (*okfn)(struct sk_buff *))
181 {
182         return nf_conntrack_in(PF_INET, hooknum, pskb);
183 }
184
185 static unsigned int ipv4_conntrack_local(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         /* root is playing with raw sockets. */
192         if ((*pskb)->len < sizeof(struct iphdr)
193             || ip_hdrlen(*pskb) < sizeof(struct iphdr)) {
194                 if (net_ratelimit())
195                         printk("ipt_hook: happy cracking.\n");
196                 return NF_ACCEPT;
197         }
198         return nf_conntrack_in(PF_INET, hooknum, pskb);
199 }
200
201 /* Connection tracking may drop packets, but never alters them, so
202    make it the first hook. */
203 static struct nf_hook_ops ipv4_conntrack_ops[] = {
204         {
205                 .hook           = ipv4_conntrack_defrag,
206                 .owner          = THIS_MODULE,
207                 .pf             = PF_INET,
208                 .hooknum        = NF_IP_PRE_ROUTING,
209                 .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
210         },
211         {
212                 .hook           = ipv4_conntrack_in,
213                 .owner          = THIS_MODULE,
214                 .pf             = PF_INET,
215                 .hooknum        = NF_IP_PRE_ROUTING,
216                 .priority       = NF_IP_PRI_CONNTRACK,
217         },
218         {
219                 .hook           = ipv4_conntrack_defrag,
220                 .owner          = THIS_MODULE,
221                 .pf             = PF_INET,
222                 .hooknum        = NF_IP_LOCAL_OUT,
223                 .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
224         },
225         {
226                 .hook           = ipv4_conntrack_local,
227                 .owner          = THIS_MODULE,
228                 .pf             = PF_INET,
229                 .hooknum        = NF_IP_LOCAL_OUT,
230                 .priority       = NF_IP_PRI_CONNTRACK,
231         },
232         {
233                 .hook           = ipv4_conntrack_help,
234                 .owner          = THIS_MODULE,
235                 .pf             = PF_INET,
236                 .hooknum        = NF_IP_POST_ROUTING,
237                 .priority       = NF_IP_PRI_CONNTRACK_HELPER,
238         },
239         {
240                 .hook           = ipv4_conntrack_help,
241                 .owner          = THIS_MODULE,
242                 .pf             = PF_INET,
243                 .hooknum        = NF_IP_LOCAL_IN,
244                 .priority       = NF_IP_PRI_CONNTRACK_HELPER,
245         },
246         {
247                 .hook           = ipv4_confirm,
248                 .owner          = THIS_MODULE,
249                 .pf             = PF_INET,
250                 .hooknum        = NF_IP_POST_ROUTING,
251                 .priority       = NF_IP_PRI_CONNTRACK_CONFIRM,
252         },
253         {
254                 .hook           = ipv4_confirm,
255                 .owner          = THIS_MODULE,
256                 .pf             = PF_INET,
257                 .hooknum        = NF_IP_LOCAL_IN,
258                 .priority       = NF_IP_PRI_CONNTRACK_CONFIRM,
259         },
260 };
261
262 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
263 static int log_invalid_proto_min = 0;
264 static int log_invalid_proto_max = 255;
265
266 static ctl_table ip_ct_sysctl_table[] = {
267         {
268                 .ctl_name       = NET_IPV4_NF_CONNTRACK_MAX,
269                 .procname       = "ip_conntrack_max",
270                 .data           = &nf_conntrack_max,
271                 .maxlen         = sizeof(int),
272                 .mode           = 0644,
273                 .proc_handler   = &proc_dointvec,
274         },
275         {
276                 .ctl_name       = NET_IPV4_NF_CONNTRACK_COUNT,
277                 .procname       = "ip_conntrack_count",
278                 .data           = &nf_conntrack_count,
279                 .maxlen         = sizeof(int),
280                 .mode           = 0444,
281                 .proc_handler   = &proc_dointvec,
282         },
283         {
284                 .ctl_name       = NET_IPV4_NF_CONNTRACK_BUCKETS,
285                 .procname       = "ip_conntrack_buckets",
286                 .data           = &nf_conntrack_htable_size,
287                 .maxlen         = sizeof(unsigned int),
288                 .mode           = 0444,
289                 .proc_handler   = &proc_dointvec,
290         },
291         {
292                 .ctl_name       = NET_IPV4_NF_CONNTRACK_CHECKSUM,
293                 .procname       = "ip_conntrack_checksum",
294                 .data           = &nf_conntrack_checksum,
295                 .maxlen         = sizeof(int),
296                 .mode           = 0644,
297                 .proc_handler   = &proc_dointvec,
298         },
299         {
300                 .ctl_name       = NET_IPV4_NF_CONNTRACK_LOG_INVALID,
301                 .procname       = "ip_conntrack_log_invalid",
302                 .data           = &nf_ct_log_invalid,
303                 .maxlen         = sizeof(unsigned int),
304                 .mode           = 0644,
305                 .proc_handler   = &proc_dointvec_minmax,
306                 .strategy       = &sysctl_intvec,
307                 .extra1         = &log_invalid_proto_min,
308                 .extra2         = &log_invalid_proto_max,
309         },
310         {
311                 .ctl_name       = 0
312         }
313 };
314 #endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
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                 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
358
359                 DEBUGP("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
360                        NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
361                 nf_ct_put(ct);
362                 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
363                         return -EFAULT;
364                 else
365                         return 0;
366         }
367         DEBUGP("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
368                NIPQUAD(tuple.src.u3.ip), ntohs(tuple.src.u.tcp.port),
369                NIPQUAD(tuple.dst.u3.ip), ntohs(tuple.dst.u.tcp.port));
370         return -ENOENT;
371 }
372
373 #if defined(CONFIG_NF_CT_NETLINK) || 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 = *(__be32 *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
406         t->dst.u3.ip = *(__be32 *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
407
408         return 0;
409 }
410 #endif
411
412 static struct nf_sockopt_ops so_getorigdst = {
413         .pf             = PF_INET,
414         .get_optmin     = SO_ORIGINAL_DST,
415         .get_optmax     = SO_ORIGINAL_DST+1,
416         .get            = &getorigdst,
417 };
418
419 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 = {
420         .l3proto         = PF_INET,
421         .name            = "ipv4",
422         .pkt_to_tuple    = ipv4_pkt_to_tuple,
423         .invert_tuple    = ipv4_invert_tuple,
424         .print_tuple     = ipv4_print_tuple,
425         .print_conntrack = ipv4_print_conntrack,
426         .prepare         = ipv4_prepare,
427         .get_features    = ipv4_get_features,
428 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
429         .tuple_to_nfattr = ipv4_tuple_to_nfattr,
430         .nfattr_to_tuple = ipv4_nfattr_to_tuple,
431 #endif
432 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
433         .ctl_table_path  = nf_net_ipv4_netfilter_sysctl_path,
434         .ctl_table       = ip_ct_sysctl_table,
435 #endif
436         .me              = THIS_MODULE,
437 };
438
439 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
440 MODULE_ALIAS("ip_conntrack");
441 MODULE_LICENSE("GPL");
442
443 static int __init nf_conntrack_l3proto_ipv4_init(void)
444 {
445         int ret = 0;
446
447         need_conntrack();
448
449         ret = nf_register_sockopt(&so_getorigdst);
450         if (ret < 0) {
451                 printk(KERN_ERR "Unable to register netfilter socket option\n");
452                 return ret;
453         }
454
455         ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_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_l4proto_register(&nf_conntrack_l4proto_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_l4proto_register(&nf_conntrack_l4proto_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_hooks(ipv4_conntrack_ops,
480                                 ARRAY_SIZE(ipv4_conntrack_ops));
481         if (ret < 0) {
482                 printk("nf_conntrack_ipv4: can't register hooks.\n");
483                 goto cleanup_ipv4;
484         }
485 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
486         ret = nf_conntrack_ipv4_compat_init();
487         if (ret < 0)
488                 goto cleanup_hooks;
489 #endif
490         return ret;
491 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
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_l4proto_unregister(&nf_conntrack_l4proto_icmp);
499  cleanup_udp:
500         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
501  cleanup_tcp:
502         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_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 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
512         nf_conntrack_ipv4_compat_fini();
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_l4proto_unregister(&nf_conntrack_l4proto_icmp);
517         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
518         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_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);