Commit | Line | Data |
---|---|---|
601e68e1 | 1 | /* netfilter.c: look after the filters for various protocols. |
f6ebe77f HW |
2 | * Heavily influenced by the old firewall.c by David Bonn and Alan Cox. |
3 | * | |
4 | * Thanks to Rob `CmdrTaco' Malda for not influencing this code in any | |
5 | * way. | |
6 | * | |
7 | * Rusty Russell (C)2000 -- This code is GPL. | |
f229f6ce | 8 | * Patrick McHardy (c) 2006-2012 |
f6ebe77f | 9 | */ |
f6ebe77f HW |
10 | #include <linux/kernel.h> |
11 | #include <linux/netfilter.h> | |
12 | #include <net/protocol.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/skbuff.h> | |
15 | #include <linux/wait.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/interrupt.h> | |
18 | #include <linux/if.h> | |
19 | #include <linux/netdevice.h> | |
20 | #include <linux/inetdevice.h> | |
21 | #include <linux/proc_fs.h> | |
d486dd1f | 22 | #include <linux/mutex.h> |
5a0e3ad6 | 23 | #include <linux/slab.h> |
457c4cbc | 24 | #include <net/net_namespace.h> |
f6ebe77f HW |
25 | #include <net/sock.h> |
26 | ||
27 | #include "nf_internals.h" | |
28 | ||
d486dd1f | 29 | static DEFINE_MUTEX(afinfo_mutex); |
bce8032e | 30 | |
0906a372 | 31 | const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly; |
bce8032e | 32 | EXPORT_SYMBOL(nf_afinfo); |
2a7851bf FW |
33 | const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly; |
34 | EXPORT_SYMBOL_GPL(nf_ipv6_ops); | |
bce8032e | 35 | |
1e796fda | 36 | int nf_register_afinfo(const struct nf_afinfo *afinfo) |
bce8032e | 37 | { |
7926dbfa | 38 | mutex_lock(&afinfo_mutex); |
a9b3cd7f | 39 | RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo); |
d486dd1f | 40 | mutex_unlock(&afinfo_mutex); |
bce8032e PM |
41 | return 0; |
42 | } | |
43 | EXPORT_SYMBOL_GPL(nf_register_afinfo); | |
44 | ||
1e796fda | 45 | void nf_unregister_afinfo(const struct nf_afinfo *afinfo) |
bce8032e | 46 | { |
d486dd1f | 47 | mutex_lock(&afinfo_mutex); |
a9b3cd7f | 48 | RCU_INIT_POINTER(nf_afinfo[afinfo->family], NULL); |
d486dd1f | 49 | mutex_unlock(&afinfo_mutex); |
bce8032e PM |
50 | synchronize_rcu(); |
51 | } | |
52 | EXPORT_SYMBOL_GPL(nf_unregister_afinfo); | |
53 | ||
7e9c6eeb | 54 | struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly; |
f6ebe77f | 55 | EXPORT_SYMBOL(nf_hooks); |
a2d7ec58 | 56 | |
d1c85c2e | 57 | #ifdef HAVE_JUMP_LABEL |
c5905afb | 58 | struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; |
a2d7ec58 ED |
59 | EXPORT_SYMBOL(nf_hooks_needed); |
60 | #endif | |
61 | ||
fd706d69 | 62 | static DEFINE_MUTEX(nf_hook_mutex); |
f6ebe77f HW |
63 | |
64 | int nf_register_hook(struct nf_hook_ops *reg) | |
65 | { | |
4c610979 | 66 | struct nf_hook_ops *elem; |
f6ebe77f | 67 | |
7926dbfa | 68 | mutex_lock(&nf_hook_mutex); |
4c610979 LZ |
69 | list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) { |
70 | if (reg->priority < elem->priority) | |
f6ebe77f HW |
71 | break; |
72 | } | |
4c610979 | 73 | list_add_rcu(®->list, elem->list.prev); |
fd706d69 | 74 | mutex_unlock(&nf_hook_mutex); |
d1c85c2e | 75 | #ifdef HAVE_JUMP_LABEL |
c5905afb | 76 | static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]); |
a2d7ec58 | 77 | #endif |
f6ebe77f HW |
78 | return 0; |
79 | } | |
80 | EXPORT_SYMBOL(nf_register_hook); | |
81 | ||
82 | void nf_unregister_hook(struct nf_hook_ops *reg) | |
83 | { | |
fd706d69 | 84 | mutex_lock(&nf_hook_mutex); |
f6ebe77f | 85 | list_del_rcu(®->list); |
fd706d69 | 86 | mutex_unlock(&nf_hook_mutex); |
d1c85c2e | 87 | #ifdef HAVE_JUMP_LABEL |
c5905afb | 88 | static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]); |
a2d7ec58 | 89 | #endif |
f6ebe77f HW |
90 | synchronize_net(); |
91 | } | |
92 | EXPORT_SYMBOL(nf_unregister_hook); | |
93 | ||
972d1cb1 PM |
94 | int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n) |
95 | { | |
96 | unsigned int i; | |
97 | int err = 0; | |
98 | ||
99 | for (i = 0; i < n; i++) { | |
100 | err = nf_register_hook(®[i]); | |
101 | if (err) | |
102 | goto err; | |
103 | } | |
104 | return err; | |
105 | ||
106 | err: | |
107 | if (i > 0) | |
108 | nf_unregister_hooks(reg, i); | |
109 | return err; | |
110 | } | |
111 | EXPORT_SYMBOL(nf_register_hooks); | |
112 | ||
113 | void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n) | |
114 | { | |
f68c5301 CG |
115 | while (n-- > 0) |
116 | nf_unregister_hook(®[n]); | |
972d1cb1 PM |
117 | } |
118 | EXPORT_SYMBOL(nf_unregister_hooks); | |
119 | ||
f6ebe77f | 120 | unsigned int nf_iterate(struct list_head *head, |
3db05fea | 121 | struct sk_buff *skb, |
76108cea | 122 | unsigned int hook, |
f6ebe77f HW |
123 | const struct net_device *indev, |
124 | const struct net_device *outdev, | |
2a6decfd | 125 | struct nf_hook_ops **elemp, |
f6ebe77f HW |
126 | int (*okfn)(struct sk_buff *), |
127 | int hook_thresh) | |
128 | { | |
129 | unsigned int verdict; | |
130 | ||
131 | /* | |
132 | * The caller must not block between calls to this | |
133 | * function because of risk of continuing from deleted element. | |
134 | */ | |
2a6decfd MW |
135 | list_for_each_entry_continue_rcu((*elemp), head, list) { |
136 | if (hook_thresh > (*elemp)->priority) | |
f6ebe77f HW |
137 | continue; |
138 | ||
139 | /* Optimization: we don't need to hold module | |
601e68e1 | 140 | reference here, since function can't sleep. --RR */ |
de9963f0 | 141 | repeat: |
795aa6ef | 142 | verdict = (*elemp)->hook(*elemp, skb, indev, outdev, okfn); |
f6ebe77f HW |
143 | if (verdict != NF_ACCEPT) { |
144 | #ifdef CONFIG_NETFILTER_DEBUG | |
145 | if (unlikely((verdict & NF_VERDICT_MASK) | |
146 | > NF_MAX_VERDICT)) { | |
147 | NFDEBUG("Evil return from %p(%u).\n", | |
2a6decfd | 148 | (*elemp)->hook, hook); |
f6ebe77f HW |
149 | continue; |
150 | } | |
151 | #endif | |
2a6decfd | 152 | if (verdict != NF_REPEAT) |
f6ebe77f | 153 | return verdict; |
de9963f0 | 154 | goto repeat; |
f6ebe77f HW |
155 | } |
156 | } | |
157 | return NF_ACCEPT; | |
158 | } | |
159 | ||
160 | ||
161 | /* Returns 1 if okfn() needs to be executed by the caller, | |
162 | * -EPERM for NF_DROP, 0 otherwise. */ | |
76108cea | 163 | int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb, |
f6ebe77f HW |
164 | struct net_device *indev, |
165 | struct net_device *outdev, | |
166 | int (*okfn)(struct sk_buff *), | |
167 | int hook_thresh) | |
168 | { | |
2a6decfd | 169 | struct nf_hook_ops *elem; |
f6ebe77f HW |
170 | unsigned int verdict; |
171 | int ret = 0; | |
172 | ||
173 | /* We may already have this, but read-locks nest anyway */ | |
174 | rcu_read_lock(); | |
175 | ||
2a6decfd | 176 | elem = list_entry_rcu(&nf_hooks[pf][hook], struct nf_hook_ops, list); |
f6ebe77f | 177 | next_hook: |
3db05fea | 178 | verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev, |
f6ebe77f HW |
179 | outdev, &elem, okfn, hook_thresh); |
180 | if (verdict == NF_ACCEPT || verdict == NF_STOP) { | |
181 | ret = 1; | |
da683650 | 182 | } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) { |
3db05fea | 183 | kfree_skb(skb); |
f615df76 | 184 | ret = NF_DROP_GETERR(verdict); |
da683650 EP |
185 | if (ret == 0) |
186 | ret = -EPERM; | |
f9c63990 | 187 | } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) { |
1c15b677 MW |
188 | int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn, |
189 | verdict >> NF_VERDICT_QBITS); | |
563e1232 FW |
190 | if (err < 0) { |
191 | if (err == -ECANCELED) | |
06cdb634 | 192 | goto next_hook; |
563e1232 | 193 | if (err == -ESRCH && |
94b27cc3 FW |
194 | (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS)) |
195 | goto next_hook; | |
06cdb634 FW |
196 | kfree_skb(skb); |
197 | } | |
f6ebe77f | 198 | } |
f6ebe77f HW |
199 | rcu_read_unlock(); |
200 | return ret; | |
201 | } | |
202 | EXPORT_SYMBOL(nf_hook_slow); | |
203 | ||
204 | ||
37d41879 | 205 | int skb_make_writable(struct sk_buff *skb, unsigned int writable_len) |
f6ebe77f | 206 | { |
37d41879 | 207 | if (writable_len > skb->len) |
f6ebe77f HW |
208 | return 0; |
209 | ||
210 | /* Not exclusive use of packet? Must copy. */ | |
37d41879 HX |
211 | if (!skb_cloned(skb)) { |
212 | if (writable_len <= skb_headlen(skb)) | |
213 | return 1; | |
214 | } else if (skb_clone_writable(skb, writable_len)) | |
215 | return 1; | |
216 | ||
217 | if (writable_len <= skb_headlen(skb)) | |
218 | writable_len = 0; | |
219 | else | |
220 | writable_len -= skb_headlen(skb); | |
221 | ||
222 | return !!__pskb_pull_tail(skb, writable_len); | |
f6ebe77f HW |
223 | } |
224 | EXPORT_SYMBOL(skb_make_writable); | |
225 | ||
c0cd1156 | 226 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
f6ebe77f HW |
227 | /* This does not belong here, but locally generated errors need it if connection |
228 | tracking in use: without this, connection may not be in hash table, and hence | |
229 | manufactured ICMP or RST packets will not be associated with it. */ | |
312a0c16 PM |
230 | void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) |
231 | __rcu __read_mostly; | |
f6ebe77f HW |
232 | EXPORT_SYMBOL(ip_ct_attach); |
233 | ||
312a0c16 | 234 | void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb) |
f6ebe77f | 235 | { |
312a0c16 | 236 | void (*attach)(struct sk_buff *, const struct sk_buff *); |
f6ebe77f | 237 | |
c3a47ab3 PM |
238 | if (skb->nfct) { |
239 | rcu_read_lock(); | |
240 | attach = rcu_dereference(ip_ct_attach); | |
241 | if (attach) | |
242 | attach(new, skb); | |
243 | rcu_read_unlock(); | |
f6ebe77f HW |
244 | } |
245 | } | |
246 | EXPORT_SYMBOL(nf_ct_attach); | |
de6e05c4 | 247 | |
0e60ebe0 | 248 | void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly; |
de6e05c4 YK |
249 | EXPORT_SYMBOL(nf_ct_destroy); |
250 | ||
251 | void nf_conntrack_destroy(struct nf_conntrack *nfct) | |
252 | { | |
253 | void (*destroy)(struct nf_conntrack *); | |
254 | ||
255 | rcu_read_lock(); | |
256 | destroy = rcu_dereference(nf_ct_destroy); | |
257 | BUG_ON(destroy == NULL); | |
258 | destroy(nfct); | |
259 | rcu_read_unlock(); | |
260 | } | |
261 | EXPORT_SYMBOL(nf_conntrack_destroy); | |
9cb01766 | 262 | |
5a05fae5 | 263 | struct nfq_ct_hook __rcu *nfq_ct_hook __read_mostly; |
9cb01766 PNA |
264 | EXPORT_SYMBOL_GPL(nfq_ct_hook); |
265 | ||
d584a61a PNA |
266 | struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook __read_mostly; |
267 | EXPORT_SYMBOL_GPL(nfq_ct_nat_hook); | |
268 | ||
de6e05c4 | 269 | #endif /* CONFIG_NF_CONNTRACK */ |
f6ebe77f | 270 | |
c7232c99 PM |
271 | #ifdef CONFIG_NF_NAT_NEEDED |
272 | void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *); | |
273 | EXPORT_SYMBOL(nf_nat_decode_session_hook); | |
274 | #endif | |
275 | ||
f3c1a44a G |
276 | static int __net_init netfilter_net_init(struct net *net) |
277 | { | |
278 | #ifdef CONFIG_PROC_FS | |
279 | net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter", | |
280 | net->proc_net); | |
12202fa7 PNA |
281 | if (!net->nf.proc_netfilter) { |
282 | if (!net_eq(net, &init_net)) | |
283 | pr_err("cannot create netfilter proc entry"); | |
284 | ||
f3c1a44a G |
285 | return -ENOMEM; |
286 | } | |
287 | #endif | |
288 | return 0; | |
289 | } | |
290 | ||
291 | static void __net_exit netfilter_net_exit(struct net *net) | |
292 | { | |
293 | remove_proc_entry("netfilter", net->proc_net); | |
294 | } | |
295 | ||
296 | static struct pernet_operations netfilter_net_ops = { | |
297 | .init = netfilter_net_init, | |
298 | .exit = netfilter_net_exit, | |
299 | }; | |
300 | ||
6d11cfdb | 301 | int __init netfilter_init(void) |
f6ebe77f | 302 | { |
6d11cfdb PNA |
303 | int i, h, ret; |
304 | ||
7e9c6eeb | 305 | for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) { |
f6ebe77f HW |
306 | for (h = 0; h < NF_MAX_HOOKS; h++) |
307 | INIT_LIST_HEAD(&nf_hooks[i][h]); | |
308 | } | |
309 | ||
6d11cfdb PNA |
310 | ret = register_pernet_subsys(&netfilter_net_ops); |
311 | if (ret < 0) | |
312 | goto err; | |
313 | ||
314 | ret = netfilter_log_init(); | |
315 | if (ret < 0) | |
316 | goto err_pernet; | |
f6ebe77f | 317 | |
6d11cfdb PNA |
318 | return 0; |
319 | err_pernet: | |
320 | unregister_pernet_subsys(&netfilter_net_ops); | |
321 | err: | |
322 | return ret; | |
f6ebe77f | 323 | } |