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