netfilter: nf_conntrack: log packets dropped by helpers
[safe/jmp/linux-2.6] / net / ipv6 / netfilter / nf_conntrack_l3proto_ipv6.c
1 /*
2  * Copyright (C)2004 USAGI/WIDE Project
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  * Author:
9  *      Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
10  */
11
12 #include <linux/types.h>
13 #include <linux/ipv6.h>
14 #include <linux/in6.h>
15 #include <linux/netfilter.h>
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/icmp.h>
19 #include <linux/sysctl.h>
20 #include <net/ipv6.h>
21 #include <net/inet_frag.h>
22
23 #include <linux/netfilter_ipv6.h>
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_conntrack_helper.h>
26 #include <net/netfilter/nf_conntrack_l4proto.h>
27 #include <net/netfilter/nf_conntrack_l3proto.h>
28 #include <net/netfilter/nf_conntrack_core.h>
29 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
30 #include <net/netfilter/nf_log.h>
31
32 static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
33                               struct nf_conntrack_tuple *tuple)
34 {
35         const u_int32_t *ap;
36         u_int32_t _addrs[8];
37
38         ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
39                                 sizeof(_addrs), _addrs);
40         if (ap == NULL)
41                 return false;
42
43         memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
44         memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
45
46         return true;
47 }
48
49 static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
50                               const struct nf_conntrack_tuple *orig)
51 {
52         memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
53         memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
54
55         return true;
56 }
57
58 static int ipv6_print_tuple(struct seq_file *s,
59                             const struct nf_conntrack_tuple *tuple)
60 {
61         return seq_printf(s, "src=%pI6 dst=%pI6 ",
62                           tuple->src.u3.ip6, tuple->dst.u3.ip6);
63 }
64
65 /*
66  * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
67  *
68  * This function parses (probably truncated) exthdr set "hdr"
69  * of length "len". "nexthdrp" initially points to some place,
70  * where type of the first header can be found.
71  *
72  * It skips all well-known exthdrs, and returns pointer to the start
73  * of unparsable area i.e. the first header with unknown type.
74  * if success, *nexthdr is updated by type/protocol of this header.
75  *
76  * NOTES: - it may return pointer pointing beyond end of packet,
77  *          if the last recognized header is truncated in the middle.
78  *        - if packet is truncated, so that all parsed headers are skipped,
79  *          it returns -1.
80  *        - if packet is fragmented, return pointer of the fragment header.
81  *        - ESP is unparsable for now and considered like
82  *          normal payload protocol.
83  *        - Note also special handling of AUTH header. Thanks to IPsec wizards.
84  */
85
86 static int nf_ct_ipv6_skip_exthdr(const struct sk_buff *skb, int start,
87                                   u8 *nexthdrp, int len)
88 {
89         u8 nexthdr = *nexthdrp;
90
91         while (ipv6_ext_hdr(nexthdr)) {
92                 struct ipv6_opt_hdr hdr;
93                 int hdrlen;
94
95                 if (len < (int)sizeof(struct ipv6_opt_hdr))
96                         return -1;
97                 if (nexthdr == NEXTHDR_NONE)
98                         break;
99                 if (nexthdr == NEXTHDR_FRAGMENT)
100                         break;
101                 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
102                         BUG();
103                 if (nexthdr == NEXTHDR_AUTH)
104                         hdrlen = (hdr.hdrlen+2)<<2;
105                 else
106                         hdrlen = ipv6_optlen(&hdr);
107
108                 nexthdr = hdr.nexthdr;
109                 len -= hdrlen;
110                 start += hdrlen;
111         }
112
113         *nexthdrp = nexthdr;
114         return start;
115 }
116
117 static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
118                             unsigned int *dataoff, u_int8_t *protonum)
119 {
120         unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
121         unsigned char pnum;
122         int protoff;
123
124         if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
125                           &pnum, sizeof(pnum)) != 0) {
126                 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
127                 return -NF_ACCEPT;
128         }
129         protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum, skb->len - extoff);
130         /*
131          * (protoff == skb->len) mean that the packet doesn't have no data
132          * except of IPv6 & ext headers. but it's tracked anyway. - YK
133          */
134         if ((protoff < 0) || (protoff > skb->len)) {
135                 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
136                 return -NF_ACCEPT;
137         }
138
139         *dataoff = protoff;
140         *protonum = pnum;
141         return NF_ACCEPT;
142 }
143
144 static unsigned int ipv6_confirm(unsigned int hooknum,
145                                  struct sk_buff *skb,
146                                  const struct net_device *in,
147                                  const struct net_device *out,
148                                  int (*okfn)(struct sk_buff *))
149 {
150         struct nf_conn *ct;
151         const struct nf_conn_help *help;
152         const struct nf_conntrack_helper *helper;
153         enum ip_conntrack_info ctinfo;
154         unsigned int ret, protoff;
155         unsigned int extoff = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
156         unsigned char pnum = ipv6_hdr(skb)->nexthdr;
157
158
159         /* This is where we call the helper: as the packet goes out. */
160         ct = nf_ct_get(skb, &ctinfo);
161         if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
162                 goto out;
163
164         help = nfct_help(ct);
165         if (!help)
166                 goto out;
167         /* rcu_read_lock()ed by nf_hook_slow */
168         helper = rcu_dereference(help->helper);
169         if (!helper)
170                 goto out;
171
172         protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum,
173                                          skb->len - extoff);
174         if (protoff > skb->len || pnum == NEXTHDR_FRAGMENT) {
175                 pr_debug("proto header not found\n");
176                 return NF_ACCEPT;
177         }
178
179         ret = helper->help(skb, protoff, ct, ctinfo);
180         if (ret != NF_ACCEPT) {
181                 nf_log_packet(NFPROTO_IPV6, hooknum, skb, in, out, NULL,
182                               "nf_ct_%s: dropping packet", helper->name);
183                 return ret;
184         }
185 out:
186         /* We've seen it coming out the other side: confirm it */
187         return nf_conntrack_confirm(skb);
188 }
189
190 static unsigned int ipv6_defrag(unsigned int hooknum,
191                                 struct sk_buff *skb,
192                                 const struct net_device *in,
193                                 const struct net_device *out,
194                                 int (*okfn)(struct sk_buff *))
195 {
196         struct sk_buff *reasm;
197
198         /* Previously seen (loopback)?  */
199         if (skb->nfct)
200                 return NF_ACCEPT;
201
202         reasm = nf_ct_frag6_gather(skb);
203
204         /* queued */
205         if (reasm == NULL)
206                 return NF_STOLEN;
207
208         /* error occured or not fragmented */
209         if (reasm == skb)
210                 return NF_ACCEPT;
211
212         nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
213                            (struct net_device *)out, okfn);
214
215         return NF_STOLEN;
216 }
217
218 static unsigned int __ipv6_conntrack_in(struct net *net,
219                                         unsigned int hooknum,
220                                         struct sk_buff *skb,
221                                         int (*okfn)(struct sk_buff *))
222 {
223         struct sk_buff *reasm = skb->nfct_reasm;
224
225         /* This packet is fragmented and has reassembled packet. */
226         if (reasm) {
227                 /* Reassembled packet isn't parsed yet ? */
228                 if (!reasm->nfct) {
229                         unsigned int ret;
230
231                         ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm);
232                         if (ret != NF_ACCEPT)
233                                 return ret;
234                 }
235                 nf_conntrack_get(reasm->nfct);
236                 skb->nfct = reasm->nfct;
237                 skb->nfctinfo = reasm->nfctinfo;
238                 return NF_ACCEPT;
239         }
240
241         return nf_conntrack_in(net, PF_INET6, hooknum, skb);
242 }
243
244 static unsigned int ipv6_conntrack_in(unsigned int hooknum,
245                                       struct sk_buff *skb,
246                                       const struct net_device *in,
247                                       const struct net_device *out,
248                                       int (*okfn)(struct sk_buff *))
249 {
250         return __ipv6_conntrack_in(dev_net(in), hooknum, skb, okfn);
251 }
252
253 static unsigned int ipv6_conntrack_local(unsigned int hooknum,
254                                          struct sk_buff *skb,
255                                          const struct net_device *in,
256                                          const struct net_device *out,
257                                          int (*okfn)(struct sk_buff *))
258 {
259         /* root is playing with raw sockets. */
260         if (skb->len < sizeof(struct ipv6hdr)) {
261                 if (net_ratelimit())
262                         printk("ipv6_conntrack_local: packet too short\n");
263                 return NF_ACCEPT;
264         }
265         return __ipv6_conntrack_in(dev_net(out), hooknum, skb, okfn);
266 }
267
268 static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
269         {
270                 .hook           = ipv6_defrag,
271                 .owner          = THIS_MODULE,
272                 .pf             = NFPROTO_IPV6,
273                 .hooknum        = NF_INET_PRE_ROUTING,
274                 .priority       = NF_IP6_PRI_CONNTRACK_DEFRAG,
275         },
276         {
277                 .hook           = ipv6_conntrack_in,
278                 .owner          = THIS_MODULE,
279                 .pf             = NFPROTO_IPV6,
280                 .hooknum        = NF_INET_PRE_ROUTING,
281                 .priority       = NF_IP6_PRI_CONNTRACK,
282         },
283         {
284                 .hook           = ipv6_conntrack_local,
285                 .owner          = THIS_MODULE,
286                 .pf             = NFPROTO_IPV6,
287                 .hooknum        = NF_INET_LOCAL_OUT,
288                 .priority       = NF_IP6_PRI_CONNTRACK,
289         },
290         {
291                 .hook           = ipv6_defrag,
292                 .owner          = THIS_MODULE,
293                 .pf             = NFPROTO_IPV6,
294                 .hooknum        = NF_INET_LOCAL_OUT,
295                 .priority       = NF_IP6_PRI_CONNTRACK_DEFRAG,
296         },
297         {
298                 .hook           = ipv6_confirm,
299                 .owner          = THIS_MODULE,
300                 .pf             = NFPROTO_IPV6,
301                 .hooknum        = NF_INET_POST_ROUTING,
302                 .priority       = NF_IP6_PRI_LAST,
303         },
304         {
305                 .hook           = ipv6_confirm,
306                 .owner          = THIS_MODULE,
307                 .pf             = NFPROTO_IPV6,
308                 .hooknum        = NF_INET_LOCAL_IN,
309                 .priority       = NF_IP6_PRI_LAST-1,
310         },
311 };
312
313 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
314
315 #include <linux/netfilter/nfnetlink.h>
316 #include <linux/netfilter/nfnetlink_conntrack.h>
317
318 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
319                                 const struct nf_conntrack_tuple *tuple)
320 {
321         NLA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
322                 &tuple->src.u3.ip6);
323         NLA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
324                 &tuple->dst.u3.ip6);
325         return 0;
326
327 nla_put_failure:
328         return -1;
329 }
330
331 static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
332         [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
333         [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
334 };
335
336 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
337                                 struct nf_conntrack_tuple *t)
338 {
339         if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
340                 return -EINVAL;
341
342         memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
343                sizeof(u_int32_t) * 4);
344         memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
345                sizeof(u_int32_t) * 4);
346
347         return 0;
348 }
349
350 static int ipv6_nlattr_tuple_size(void)
351 {
352         return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1);
353 }
354 #endif
355
356 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
357         .l3proto                = PF_INET6,
358         .name                   = "ipv6",
359         .pkt_to_tuple           = ipv6_pkt_to_tuple,
360         .invert_tuple           = ipv6_invert_tuple,
361         .print_tuple            = ipv6_print_tuple,
362         .get_l4proto            = ipv6_get_l4proto,
363 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
364         .tuple_to_nlattr        = ipv6_tuple_to_nlattr,
365         .nlattr_tuple_size      = ipv6_nlattr_tuple_size,
366         .nlattr_to_tuple        = ipv6_nlattr_to_tuple,
367         .nla_policy             = ipv6_nla_policy,
368 #endif
369 #ifdef CONFIG_SYSCTL
370         .ctl_table_path         = nf_net_netfilter_sysctl_path,
371         .ctl_table              = nf_ct_ipv6_sysctl_table,
372 #endif
373         .me                     = THIS_MODULE,
374 };
375
376 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
377 MODULE_LICENSE("GPL");
378 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
379
380 static int __init nf_conntrack_l3proto_ipv6_init(void)
381 {
382         int ret = 0;
383
384         need_conntrack();
385
386         ret = nf_ct_frag6_init();
387         if (ret < 0) {
388                 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
389                 return ret;
390         }
391         ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
392         if (ret < 0) {
393                 printk("nf_conntrack_ipv6: can't register tcp.\n");
394                 goto cleanup_frag6;
395         }
396
397         ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
398         if (ret < 0) {
399                 printk("nf_conntrack_ipv6: can't register udp.\n");
400                 goto cleanup_tcp;
401         }
402
403         ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
404         if (ret < 0) {
405                 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
406                 goto cleanup_udp;
407         }
408
409         ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
410         if (ret < 0) {
411                 printk("nf_conntrack_ipv6: can't register ipv6\n");
412                 goto cleanup_icmpv6;
413         }
414
415         ret = nf_register_hooks(ipv6_conntrack_ops,
416                                 ARRAY_SIZE(ipv6_conntrack_ops));
417         if (ret < 0) {
418                 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
419                        "hook.\n");
420                 goto cleanup_ipv6;
421         }
422         return ret;
423
424  cleanup_ipv6:
425         nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
426  cleanup_icmpv6:
427         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
428  cleanup_udp:
429         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
430  cleanup_tcp:
431         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
432  cleanup_frag6:
433         nf_ct_frag6_cleanup();
434         return ret;
435 }
436
437 static void __exit nf_conntrack_l3proto_ipv6_fini(void)
438 {
439         synchronize_net();
440         nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
441         nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
442         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
443         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
444         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
445         nf_ct_frag6_cleanup();
446 }
447
448 module_init(nf_conntrack_l3proto_ipv6_init);
449 module_exit(nf_conntrack_l3proto_ipv6_fini);