]> Pileus Git - ~andy/linux/blob - net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
Merge tag 'v3.9-rc3' into next
[~andy/linux] / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4.c
1
2 /* (C) 1999-2001 Paul `Rusty' Russell
3  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/types.h>
11 #include <linux/ip.h>
12 #include <linux/netfilter.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/icmp.h>
16 #include <linux/sysctl.h>
17 #include <net/route.h>
18 #include <net/ip.h>
19
20 #include <linux/netfilter_ipv4.h>
21 #include <net/netfilter/nf_conntrack.h>
22 #include <net/netfilter/nf_conntrack_helper.h>
23 #include <net/netfilter/nf_conntrack_l4proto.h>
24 #include <net/netfilter/nf_conntrack_l3proto.h>
25 #include <net/netfilter/nf_conntrack_zones.h>
26 #include <net/netfilter/nf_conntrack_core.h>
27 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
28 #include <net/netfilter/nf_nat_helper.h>
29 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
30 #include <net/netfilter/nf_log.h>
31
32 static bool ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
33                               struct nf_conntrack_tuple *tuple)
34 {
35         const __be32 *ap;
36         __be32 _addrs[2];
37         ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr),
38                                 sizeof(u_int32_t) * 2, _addrs);
39         if (ap == NULL)
40                 return false;
41
42         tuple->src.u3.ip = ap[0];
43         tuple->dst.u3.ip = ap[1];
44
45         return true;
46 }
47
48 static bool 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 true;
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=%pI4 dst=%pI4 ",
61                           &tuple->src.u3.ip, &tuple->dst.u3.ip);
62 }
63
64 static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
65                             unsigned int *dataoff, u_int8_t *protonum)
66 {
67         const struct iphdr *iph;
68         struct iphdr _iph;
69
70         iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
71         if (iph == NULL)
72                 return -NF_ACCEPT;
73
74         /* Conntrack defragments packets, we might still see fragments
75          * inside ICMP packets though. */
76         if (iph->frag_off & htons(IP_OFFSET))
77                 return -NF_ACCEPT;
78
79         *dataoff = nhoff + (iph->ihl << 2);
80         *protonum = iph->protocol;
81
82         /* Check bogus IP headers */
83         if (*dataoff > skb->len) {
84                 pr_debug("nf_conntrack_ipv4: bogus IPv4 packet: "
85                          "nhoff %u, ihl %u, skblen %u\n",
86                          nhoff, iph->ihl << 2, skb->len);
87                 return -NF_ACCEPT;
88         }
89
90         return NF_ACCEPT;
91 }
92
93 static unsigned int ipv4_helper(unsigned int hooknum,
94                                 struct sk_buff *skb,
95                                 const struct net_device *in,
96                                 const struct net_device *out,
97                                 int (*okfn)(struct sk_buff *))
98 {
99         struct nf_conn *ct;
100         enum ip_conntrack_info ctinfo;
101         const struct nf_conn_help *help;
102         const struct nf_conntrack_helper *helper;
103
104         /* This is where we call the helper: as the packet goes out. */
105         ct = nf_ct_get(skb, &ctinfo);
106         if (!ct || ctinfo == IP_CT_RELATED_REPLY)
107                 return NF_ACCEPT;
108
109         help = nfct_help(ct);
110         if (!help)
111                 return NF_ACCEPT;
112
113         /* rcu_read_lock()ed by nf_hook_slow */
114         helper = rcu_dereference(help->helper);
115         if (!helper)
116                 return NF_ACCEPT;
117
118         return helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb),
119                             ct, ctinfo);
120 }
121
122 static unsigned int ipv4_confirm(unsigned int hooknum,
123                                  struct sk_buff *skb,
124                                  const struct net_device *in,
125                                  const struct net_device *out,
126                                  int (*okfn)(struct sk_buff *))
127 {
128         struct nf_conn *ct;
129         enum ip_conntrack_info ctinfo;
130
131         ct = nf_ct_get(skb, &ctinfo);
132         if (!ct || ctinfo == IP_CT_RELATED_REPLY)
133                 goto out;
134
135         /* adjust seqs for loopback traffic only in outgoing direction */
136         if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
137             !nf_is_loopback_packet(skb)) {
138                 typeof(nf_nat_seq_adjust_hook) seq_adjust;
139
140                 seq_adjust = rcu_dereference(nf_nat_seq_adjust_hook);
141                 if (!seq_adjust ||
142                     !seq_adjust(skb, ct, ctinfo, ip_hdrlen(skb))) {
143                         NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop);
144                         return NF_DROP;
145                 }
146         }
147 out:
148         /* We've seen it coming out the other side: confirm it */
149         return nf_conntrack_confirm(skb);
150 }
151
152 static unsigned int ipv4_conntrack_in(unsigned int hooknum,
153                                       struct sk_buff *skb,
154                                       const struct net_device *in,
155                                       const struct net_device *out,
156                                       int (*okfn)(struct sk_buff *))
157 {
158         return nf_conntrack_in(dev_net(in), PF_INET, hooknum, skb);
159 }
160
161 static unsigned int ipv4_conntrack_local(unsigned int hooknum,
162                                          struct sk_buff *skb,
163                                          const struct net_device *in,
164                                          const struct net_device *out,
165                                          int (*okfn)(struct sk_buff *))
166 {
167         /* root is playing with raw sockets. */
168         if (skb->len < sizeof(struct iphdr) ||
169             ip_hdrlen(skb) < sizeof(struct iphdr))
170                 return NF_ACCEPT;
171         return nf_conntrack_in(dev_net(out), PF_INET, hooknum, skb);
172 }
173
174 /* Connection tracking may drop packets, but never alters them, so
175    make it the first hook. */
176 static struct nf_hook_ops ipv4_conntrack_ops[] __read_mostly = {
177         {
178                 .hook           = ipv4_conntrack_in,
179                 .owner          = THIS_MODULE,
180                 .pf             = NFPROTO_IPV4,
181                 .hooknum        = NF_INET_PRE_ROUTING,
182                 .priority       = NF_IP_PRI_CONNTRACK,
183         },
184         {
185                 .hook           = ipv4_conntrack_local,
186                 .owner          = THIS_MODULE,
187                 .pf             = NFPROTO_IPV4,
188                 .hooknum        = NF_INET_LOCAL_OUT,
189                 .priority       = NF_IP_PRI_CONNTRACK,
190         },
191         {
192                 .hook           = ipv4_helper,
193                 .owner          = THIS_MODULE,
194                 .pf             = NFPROTO_IPV4,
195                 .hooknum        = NF_INET_POST_ROUTING,
196                 .priority       = NF_IP_PRI_CONNTRACK_HELPER,
197         },
198         {
199                 .hook           = ipv4_confirm,
200                 .owner          = THIS_MODULE,
201                 .pf             = NFPROTO_IPV4,
202                 .hooknum        = NF_INET_POST_ROUTING,
203                 .priority       = NF_IP_PRI_CONNTRACK_CONFIRM,
204         },
205         {
206                 .hook           = ipv4_helper,
207                 .owner          = THIS_MODULE,
208                 .pf             = NFPROTO_IPV4,
209                 .hooknum        = NF_INET_LOCAL_IN,
210                 .priority       = NF_IP_PRI_CONNTRACK_HELPER,
211         },
212         {
213                 .hook           = ipv4_confirm,
214                 .owner          = THIS_MODULE,
215                 .pf             = NFPROTO_IPV4,
216                 .hooknum        = NF_INET_LOCAL_IN,
217                 .priority       = NF_IP_PRI_CONNTRACK_CONFIRM,
218         },
219 };
220
221 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
222 static int log_invalid_proto_min = 0;
223 static int log_invalid_proto_max = 255;
224
225 static ctl_table ip_ct_sysctl_table[] = {
226         {
227                 .procname       = "ip_conntrack_max",
228                 .maxlen         = sizeof(int),
229                 .mode           = 0644,
230                 .proc_handler   = proc_dointvec,
231         },
232         {
233                 .procname       = "ip_conntrack_count",
234                 .maxlen         = sizeof(int),
235                 .mode           = 0444,
236                 .proc_handler   = proc_dointvec,
237         },
238         {
239                 .procname       = "ip_conntrack_buckets",
240                 .maxlen         = sizeof(unsigned int),
241                 .mode           = 0444,
242                 .proc_handler   = proc_dointvec,
243         },
244         {
245                 .procname       = "ip_conntrack_checksum",
246                 .maxlen         = sizeof(int),
247                 .mode           = 0644,
248                 .proc_handler   = proc_dointvec,
249         },
250         {
251                 .procname       = "ip_conntrack_log_invalid",
252                 .maxlen         = sizeof(unsigned int),
253                 .mode           = 0644,
254                 .proc_handler   = proc_dointvec_minmax,
255                 .extra1         = &log_invalid_proto_min,
256                 .extra2         = &log_invalid_proto_max,
257         },
258         { }
259 };
260 #endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
261
262 /* Fast function for those who don't want to parse /proc (and I don't
263    blame them). */
264 /* Reversing the socket's dst/src point of view gives us the reply
265    mapping. */
266 static int
267 getorigdst(struct sock *sk, int optval, void __user *user, int *len)
268 {
269         const struct inet_sock *inet = inet_sk(sk);
270         const struct nf_conntrack_tuple_hash *h;
271         struct nf_conntrack_tuple tuple;
272
273         memset(&tuple, 0, sizeof(tuple));
274         tuple.src.u3.ip = inet->inet_rcv_saddr;
275         tuple.src.u.tcp.port = inet->inet_sport;
276         tuple.dst.u3.ip = inet->inet_daddr;
277         tuple.dst.u.tcp.port = inet->inet_dport;
278         tuple.src.l3num = PF_INET;
279         tuple.dst.protonum = sk->sk_protocol;
280
281         /* We only do TCP and SCTP at the moment: is there a better way? */
282         if (sk->sk_protocol != IPPROTO_TCP && sk->sk_protocol != IPPROTO_SCTP) {
283                 pr_debug("SO_ORIGINAL_DST: Not a TCP/SCTP socket\n");
284                 return -ENOPROTOOPT;
285         }
286
287         if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
288                 pr_debug("SO_ORIGINAL_DST: len %d not %Zu\n",
289                          *len, sizeof(struct sockaddr_in));
290                 return -EINVAL;
291         }
292
293         h = nf_conntrack_find_get(sock_net(sk), NF_CT_DEFAULT_ZONE, &tuple);
294         if (h) {
295                 struct sockaddr_in sin;
296                 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
297
298                 sin.sin_family = AF_INET;
299                 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
300                         .tuple.dst.u.tcp.port;
301                 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
302                         .tuple.dst.u3.ip;
303                 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
304
305                 pr_debug("SO_ORIGINAL_DST: %pI4 %u\n",
306                          &sin.sin_addr.s_addr, ntohs(sin.sin_port));
307                 nf_ct_put(ct);
308                 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
309                         return -EFAULT;
310                 else
311                         return 0;
312         }
313         pr_debug("SO_ORIGINAL_DST: Can't find %pI4/%u-%pI4/%u.\n",
314                  &tuple.src.u3.ip, ntohs(tuple.src.u.tcp.port),
315                  &tuple.dst.u3.ip, ntohs(tuple.dst.u.tcp.port));
316         return -ENOENT;
317 }
318
319 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
320
321 #include <linux/netfilter/nfnetlink.h>
322 #include <linux/netfilter/nfnetlink_conntrack.h>
323
324 static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
325                                 const struct nf_conntrack_tuple *tuple)
326 {
327         if (nla_put_be32(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) ||
328             nla_put_be32(skb, CTA_IP_V4_DST, tuple->dst.u3.ip))
329                 goto nla_put_failure;
330         return 0;
331
332 nla_put_failure:
333         return -1;
334 }
335
336 static const struct nla_policy ipv4_nla_policy[CTA_IP_MAX+1] = {
337         [CTA_IP_V4_SRC] = { .type = NLA_U32 },
338         [CTA_IP_V4_DST] = { .type = NLA_U32 },
339 };
340
341 static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
342                                 struct nf_conntrack_tuple *t)
343 {
344         if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
345                 return -EINVAL;
346
347         t->src.u3.ip = nla_get_be32(tb[CTA_IP_V4_SRC]);
348         t->dst.u3.ip = nla_get_be32(tb[CTA_IP_V4_DST]);
349
350         return 0;
351 }
352
353 static int ipv4_nlattr_tuple_size(void)
354 {
355         return nla_policy_len(ipv4_nla_policy, CTA_IP_MAX + 1);
356 }
357 #endif
358
359 static struct nf_sockopt_ops so_getorigdst = {
360         .pf             = PF_INET,
361         .get_optmin     = SO_ORIGINAL_DST,
362         .get_optmax     = SO_ORIGINAL_DST+1,
363         .get            = &getorigdst,
364         .owner          = THIS_MODULE,
365 };
366
367 static int ipv4_init_net(struct net *net)
368 {
369 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
370         struct nf_ip_net *in = &net->ct.nf_ct_proto;
371         in->ctl_table = kmemdup(ip_ct_sysctl_table,
372                                 sizeof(ip_ct_sysctl_table),
373                                 GFP_KERNEL);
374         if (!in->ctl_table)
375                 return -ENOMEM;
376
377         in->ctl_table[0].data = &nf_conntrack_max;
378         in->ctl_table[1].data = &net->ct.count;
379         in->ctl_table[2].data = &net->ct.htable_size;
380         in->ctl_table[3].data = &net->ct.sysctl_checksum;
381         in->ctl_table[4].data = &net->ct.sysctl_log_invalid;
382 #endif
383         return 0;
384 }
385
386 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 __read_mostly = {
387         .l3proto         = PF_INET,
388         .name            = "ipv4",
389         .pkt_to_tuple    = ipv4_pkt_to_tuple,
390         .invert_tuple    = ipv4_invert_tuple,
391         .print_tuple     = ipv4_print_tuple,
392         .get_l4proto     = ipv4_get_l4proto,
393 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
394         .tuple_to_nlattr = ipv4_tuple_to_nlattr,
395         .nlattr_tuple_size = ipv4_nlattr_tuple_size,
396         .nlattr_to_tuple = ipv4_nlattr_to_tuple,
397         .nla_policy      = ipv4_nla_policy,
398 #endif
399 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
400         .ctl_table_path  = "net/ipv4/netfilter",
401 #endif
402         .init_net        = ipv4_init_net,
403         .me              = THIS_MODULE,
404 };
405
406 module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
407                   &nf_conntrack_htable_size, 0600);
408
409 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
410 MODULE_ALIAS("ip_conntrack");
411 MODULE_LICENSE("GPL");
412
413 static int ipv4_net_init(struct net *net)
414 {
415         int ret = 0;
416
417         ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_tcp4);
418         if (ret < 0) {
419                 pr_err("nf_conntrack_tcp4: pernet registration failed\n");
420                 goto out_tcp;
421         }
422         ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udp4);
423         if (ret < 0) {
424                 pr_err("nf_conntrack_udp4: pernet registration failed\n");
425                 goto out_udp;
426         }
427         ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_icmp);
428         if (ret < 0) {
429                 pr_err("nf_conntrack_icmp4: pernet registration failed\n");
430                 goto out_icmp;
431         }
432         ret = nf_ct_l3proto_pernet_register(net, &nf_conntrack_l3proto_ipv4);
433         if (ret < 0) {
434                 pr_err("nf_conntrack_ipv4: pernet registration failed\n");
435                 goto out_ipv4;
436         }
437         return 0;
438 out_ipv4:
439         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmp);
440 out_icmp:
441         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp4);
442 out_udp:
443         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp4);
444 out_tcp:
445         return ret;
446 }
447
448 static void ipv4_net_exit(struct net *net)
449 {
450         nf_ct_l3proto_pernet_unregister(net, &nf_conntrack_l3proto_ipv4);
451         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmp);
452         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp4);
453         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp4);
454 }
455
456 static struct pernet_operations ipv4_net_ops = {
457         .init = ipv4_net_init,
458         .exit = ipv4_net_exit,
459 };
460
461 static int __init nf_conntrack_l3proto_ipv4_init(void)
462 {
463         int ret = 0;
464
465         need_conntrack();
466         nf_defrag_ipv4_enable();
467
468         ret = nf_register_sockopt(&so_getorigdst);
469         if (ret < 0) {
470                 printk(KERN_ERR "Unable to register netfilter socket option\n");
471                 return ret;
472         }
473
474         ret = register_pernet_subsys(&ipv4_net_ops);
475         if (ret < 0) {
476                 pr_err("nf_conntrack_ipv4: can't register pernet ops\n");
477                 goto cleanup_sockopt;
478         }
479
480         ret = nf_register_hooks(ipv4_conntrack_ops,
481                                 ARRAY_SIZE(ipv4_conntrack_ops));
482         if (ret < 0) {
483                 pr_err("nf_conntrack_ipv4: can't register hooks.\n");
484                 goto cleanup_pernet;
485         }
486
487         ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_tcp4);
488         if (ret < 0) {
489                 pr_err("nf_conntrack_ipv4: can't register tcp4 proto.\n");
490                 goto cleanup_hooks;
491         }
492
493         ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udp4);
494         if (ret < 0) {
495                 pr_err("nf_conntrack_ipv4: can't register udp4 proto.\n");
496                 goto cleanup_tcp4;
497         }
498
499         ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_icmp);
500         if (ret < 0) {
501                 pr_err("nf_conntrack_ipv4: can't register icmpv4 proto.\n");
502                 goto cleanup_udp4;
503         }
504
505         ret = nf_ct_l3proto_register(&nf_conntrack_l3proto_ipv4);
506         if (ret < 0) {
507                 pr_err("nf_conntrack_ipv4: can't register ipv4 proto.\n");
508                 goto cleanup_icmpv4;
509         }
510
511 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
512         ret = nf_conntrack_ipv4_compat_init();
513         if (ret < 0)
514                 goto cleanup_proto;
515 #endif
516         return ret;
517 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
518  cleanup_proto:
519         nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
520 #endif
521  cleanup_icmpv4:
522         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmp);
523  cleanup_udp4:
524         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp4);
525  cleanup_tcp4:
526         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
527  cleanup_hooks:
528         nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
529  cleanup_pernet:
530         unregister_pernet_subsys(&ipv4_net_ops);
531  cleanup_sockopt:
532         nf_unregister_sockopt(&so_getorigdst);
533         return ret;
534 }
535
536 static void __exit nf_conntrack_l3proto_ipv4_fini(void)
537 {
538         synchronize_net();
539 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
540         nf_conntrack_ipv4_compat_fini();
541 #endif
542         nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
543         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmp);
544         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp4);
545         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
546         nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
547         unregister_pernet_subsys(&ipv4_net_ops);
548         nf_unregister_sockopt(&so_getorigdst);
549 }
550
551 module_init(nf_conntrack_l3proto_ipv4_init);
552 module_exit(nf_conntrack_l3proto_ipv4_fini);
553
554 void need_ipv4_conntrack(void)
555 {
556         return;
557 }
558 EXPORT_SYMBOL_GPL(need_ipv4_conntrack);