]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv6/netfilter/nf_defrag_ipv6_hooks.c
netfilter: nf_hook_ops structs can be const
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / netfilter / nf_defrag_ipv6_hooks.c
CommitLineData
e97c3e27
BS
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/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>
07a93626 22#if IS_ENABLED(CONFIG_NF_CONNTRACK)
e97c3e27
BS
23#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>
e97c3e27 28#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
2fc72c7b
KK
29#endif
30#include <net/netfilter/nf_conntrack_zones.h>
e97c3e27
BS
31#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
32
834184b1
FW
33static DEFINE_MUTEX(defrag6_mutex);
34
e97c3e27
BS
35static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum,
36 struct sk_buff *skb)
37{
308ac914 38 u16 zone_id = NF_CT_DEFAULT_ZONE_ID;
07a93626 39#if IS_ENABLED(CONFIG_NF_CONNTRACK)
cb9c6836 40 if (skb_nfct(skb)) {
deedb590
DB
41 enum ip_conntrack_info ctinfo;
42 const struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
43
44 zone_id = nf_ct_zone_id(nf_ct_zone(ct), CTINFO2DIR(ctinfo));
45 }
2fc72c7b 46#endif
72b1e5e4 47 if (nf_bridge_in_prerouting(skb))
308ac914 48 return IP6_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id;
72b1e5e4 49
e97c3e27 50 if (hooknum == NF_INET_PRE_ROUTING)
308ac914 51 return IP6_DEFRAG_CONNTRACK_IN + zone_id;
e97c3e27 52 else
308ac914 53 return IP6_DEFRAG_CONNTRACK_OUT + zone_id;
e97c3e27
BS
54}
55
06198b34 56static unsigned int ipv6_defrag(void *priv,
e97c3e27 57 struct sk_buff *skb,
238e54c9 58 const struct nf_hook_state *state)
e97c3e27 59{
daaa7d64 60 int err;
e97c3e27 61
07a93626 62#if IS_ENABLED(CONFIG_NF_CONNTRACK)
e97c3e27 63 /* Previously seen (loopback)? */
cb9c6836 64 if (skb_nfct(skb) && !nf_ct_is_template((struct nf_conn *)skb_nfct(skb)))
e97c3e27 65 return NF_ACCEPT;
2fc72c7b 66#endif
e97c3e27 67
daaa7d64
FW
68 err = nf_ct_frag6_gather(state->net, skb,
69 nf_ct6_defrag_user(state->hook, skb));
e97c3e27 70 /* queued */
daaa7d64 71 if (err == -EINPROGRESS)
e97c3e27
BS
72 return NF_STOLEN;
73
9b57da06 74 return err == 0 ? NF_ACCEPT : NF_DROP;
e97c3e27
BS
75}
76
591bb278 77static const struct nf_hook_ops ipv6_defrag_ops[] = {
e97c3e27
BS
78 {
79 .hook = ipv6_defrag,
e97c3e27
BS
80 .pf = NFPROTO_IPV6,
81 .hooknum = NF_INET_PRE_ROUTING,
82 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
83 },
84 {
85 .hook = ipv6_defrag,
e97c3e27
BS
86 .pf = NFPROTO_IPV6,
87 .hooknum = NF_INET_LOCAL_OUT,
88 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
89 },
90};
91
834184b1
FW
92static void __net_exit defrag6_net_exit(struct net *net)
93{
94 if (net->nf.defrag_ipv6) {
95 nf_unregister_net_hooks(net, ipv6_defrag_ops,
96 ARRAY_SIZE(ipv6_defrag_ops));
97 net->nf.defrag_ipv6 = false;
98 }
99}
100
101static struct pernet_operations defrag6_net_ops = {
102 .exit = defrag6_net_exit,
103};
104
e97c3e27
BS
105static int __init nf_defrag_init(void)
106{
107 int ret = 0;
108
109 ret = nf_ct_frag6_init();
110 if (ret < 0) {
111 pr_err("nf_defrag_ipv6: can't initialize frag6.\n");
112 return ret;
113 }
834184b1 114 ret = register_pernet_subsys(&defrag6_net_ops);
e97c3e27 115 if (ret < 0) {
834184b1 116 pr_err("nf_defrag_ipv6: can't register pernet ops\n");
e97c3e27
BS
117 goto cleanup_frag6;
118 }
119 return ret;
120
121cleanup_frag6:
122 nf_ct_frag6_cleanup();
123 return ret;
124
125}
126
127static void __exit nf_defrag_fini(void)
128{
834184b1 129 unregister_pernet_subsys(&defrag6_net_ops);
e97c3e27
BS
130 nf_ct_frag6_cleanup();
131}
132
834184b1 133int nf_defrag_ipv6_enable(struct net *net)
e97c3e27 134{
834184b1
FW
135 int err = 0;
136
137 might_sleep();
138
139 if (net->nf.defrag_ipv6)
140 return 0;
141
142 mutex_lock(&defrag6_mutex);
143 if (net->nf.defrag_ipv6)
144 goto out_unlock;
145
146 err = nf_register_net_hooks(net, ipv6_defrag_ops,
147 ARRAY_SIZE(ipv6_defrag_ops));
148 if (err == 0)
149 net->nf.defrag_ipv6 = true;
150
151 out_unlock:
152 mutex_unlock(&defrag6_mutex);
153 return err;
e97c3e27
BS
154}
155EXPORT_SYMBOL_GPL(nf_defrag_ipv6_enable);
156
157module_init(nf_defrag_init);
158module_exit(nf_defrag_fini);
159
160MODULE_LICENSE("GPL");