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