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