blob: f7aab5ab93a558bd7d60010e15ca15169d1d0ba7 [file] [log] [blame]
Balazs Scheidlere97c3e22010-10-21 16:03:43 +02001/* (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/ipv6.h>
11#include <linux/in6.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/ipv6.h>
18#include <net/inet_frag.h>
19
20#include <linux/netfilter_ipv6.h>
21#include <linux/netfilter_bridge.h>
Amerigo Wang07a93622012-10-29 16:23:10 +000022#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020023#include <net/netfilter/nf_conntrack.h>
24#include <net/netfilter/nf_conntrack_helper.h>
25#include <net/netfilter/nf_conntrack_l4proto.h>
26#include <net/netfilter/nf_conntrack_l3proto.h>
27#include <net/netfilter/nf_conntrack_core.h>
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020028#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +010029#endif
30#include <net/netfilter/nf_conntrack_zones.h>
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020031#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
32
33static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum,
34 struct sk_buff *skb)
35{
Daniel Borkmann308ac912015-08-08 21:40:01 +020036 u16 zone_id = NF_CT_DEFAULT_ZONE_ID;
Amerigo Wang07a93622012-10-29 16:23:10 +000037#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Daniel Borkmanndeedb592015-08-14 16:03:39 +020038 if (skb->nfct) {
39 enum ip_conntrack_info ctinfo;
40 const struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
41
42 zone_id = nf_ct_zone_id(nf_ct_zone(ct), CTINFO2DIR(ctinfo));
43 }
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +010044#endif
Florian Westphal72b1e5e2015-07-23 16:21:30 +020045 if (nf_bridge_in_prerouting(skb))
Daniel Borkmann308ac912015-08-08 21:40:01 +020046 return IP6_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id;
Florian Westphal72b1e5e2015-07-23 16:21:30 +020047
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020048 if (hooknum == NF_INET_PRE_ROUTING)
Daniel Borkmann308ac912015-08-08 21:40:01 +020049 return IP6_DEFRAG_CONNTRACK_IN + zone_id;
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020050 else
Daniel Borkmann308ac912015-08-08 21:40:01 +020051 return IP6_DEFRAG_CONNTRACK_OUT + zone_id;
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020052}
53
Eric W. Biederman06198b32015-09-18 14:33:06 -050054static unsigned int ipv6_defrag(void *priv,
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020055 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -040056 const struct nf_hook_state *state)
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020057{
Florian Westphaldaaa7d62015-11-18 23:32:40 +010058 int err;
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020059
Amerigo Wang07a93622012-10-29 16:23:10 +000060#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020061 /* Previously seen (loopback)? */
62 if (skb->nfct && !nf_ct_is_template((struct nf_conn *)skb->nfct))
63 return NF_ACCEPT;
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +010064#endif
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020065
Florian Westphaldaaa7d62015-11-18 23:32:40 +010066 err = nf_ct_frag6_gather(state->net, skb,
67 nf_ct6_defrag_user(state->hook, skb));
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020068 /* queued */
Florian Westphaldaaa7d62015-11-18 23:32:40 +010069 if (err == -EINPROGRESS)
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020070 return NF_STOLEN;
71
Florian Westphaldaaa7d62015-11-18 23:32:40 +010072 return NF_ACCEPT;
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020073}
74
75static struct nf_hook_ops ipv6_defrag_ops[] = {
76 {
77 .hook = ipv6_defrag,
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020078 .pf = NFPROTO_IPV6,
79 .hooknum = NF_INET_PRE_ROUTING,
80 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
81 },
82 {
83 .hook = ipv6_defrag,
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020084 .pf = NFPROTO_IPV6,
85 .hooknum = NF_INET_LOCAL_OUT,
86 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
87 },
88};
89
90static int __init nf_defrag_init(void)
91{
92 int ret = 0;
93
94 ret = nf_ct_frag6_init();
95 if (ret < 0) {
96 pr_err("nf_defrag_ipv6: can't initialize frag6.\n");
97 return ret;
98 }
99 ret = nf_register_hooks(ipv6_defrag_ops, ARRAY_SIZE(ipv6_defrag_ops));
100 if (ret < 0) {
101 pr_err("nf_defrag_ipv6: can't register hooks\n");
102 goto cleanup_frag6;
103 }
104 return ret;
105
106cleanup_frag6:
107 nf_ct_frag6_cleanup();
108 return ret;
109
110}
111
112static void __exit nf_defrag_fini(void)
113{
114 nf_unregister_hooks(ipv6_defrag_ops, ARRAY_SIZE(ipv6_defrag_ops));
115 nf_ct_frag6_cleanup();
116}
117
118void nf_defrag_ipv6_enable(void)
119{
120}
121EXPORT_SYMBOL_GPL(nf_defrag_ipv6_enable);
122
123module_init(nf_defrag_init);
124module_exit(nf_defrag_fini);
125
126MODULE_LICENSE("GPL");