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; | |
bce8032e | 40 | rcu_assign_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); |
bce8032e | 49 | rcu_assign_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); |
fd706d69 | 57 | static DEFINE_MUTEX(nf_hook_mutex); |
f6ebe77f HW |
58 | |
59 | int nf_register_hook(struct nf_hook_ops *reg) | |
60 | { | |
4c610979 | 61 | struct nf_hook_ops *elem; |
fd706d69 | 62 | int err; |
f6ebe77f | 63 | |
fd706d69 PM |
64 | err = mutex_lock_interruptible(&nf_hook_mutex); |
65 | if (err < 0) | |
66 | return err; | |
4c610979 LZ |
67 | list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) { |
68 | if (reg->priority < elem->priority) | |
f6ebe77f HW |
69 | break; |
70 | } | |
4c610979 | 71 | list_add_rcu(®->list, elem->list.prev); |
fd706d69 | 72 | mutex_unlock(&nf_hook_mutex); |
f6ebe77f HW |
73 | return 0; |
74 | } | |
75 | EXPORT_SYMBOL(nf_register_hook); | |
76 | ||
77 | void nf_unregister_hook(struct nf_hook_ops *reg) | |
78 | { | |
fd706d69 | 79 | mutex_lock(&nf_hook_mutex); |
f6ebe77f | 80 | list_del_rcu(®->list); |
fd706d69 | 81 | mutex_unlock(&nf_hook_mutex); |
f6ebe77f HW |
82 | |
83 | synchronize_net(); | |
84 | } | |
85 | EXPORT_SYMBOL(nf_unregister_hook); | |
86 | ||
972d1cb1 PM |
87 | int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n) |
88 | { | |
89 | unsigned int i; | |
90 | int err = 0; | |
91 | ||
92 | for (i = 0; i < n; i++) { | |
93 | err = nf_register_hook(®[i]); | |
94 | if (err) | |
95 | goto err; | |
96 | } | |
97 | return err; | |
98 | ||
99 | err: | |
100 | if (i > 0) | |
101 | nf_unregister_hooks(reg, i); | |
102 | return err; | |
103 | } | |
104 | EXPORT_SYMBOL(nf_register_hooks); | |
105 | ||
106 | void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n) | |
107 | { | |
f68c5301 CG |
108 | while (n-- > 0) |
109 | nf_unregister_hook(®[n]); | |
972d1cb1 PM |
110 | } |
111 | EXPORT_SYMBOL(nf_unregister_hooks); | |
112 | ||
f6ebe77f | 113 | unsigned int nf_iterate(struct list_head *head, |
3db05fea | 114 | struct sk_buff *skb, |
76108cea | 115 | unsigned int hook, |
f6ebe77f HW |
116 | const struct net_device *indev, |
117 | const struct net_device *outdev, | |
118 | struct list_head **i, | |
119 | int (*okfn)(struct sk_buff *), | |
120 | int hook_thresh) | |
121 | { | |
122 | unsigned int verdict; | |
123 | ||
124 | /* | |
125 | * The caller must not block between calls to this | |
126 | * function because of risk of continuing from deleted element. | |
127 | */ | |
128 | list_for_each_continue_rcu(*i, head) { | |
129 | struct nf_hook_ops *elem = (struct nf_hook_ops *)*i; | |
130 | ||
131 | if (hook_thresh > elem->priority) | |
132 | continue; | |
133 | ||
134 | /* Optimization: we don't need to hold module | |
601e68e1 | 135 | reference here, since function can't sleep. --RR */ |
f6ebe77f HW |
136 | verdict = elem->hook(hook, skb, indev, outdev, okfn); |
137 | if (verdict != NF_ACCEPT) { | |
138 | #ifdef CONFIG_NETFILTER_DEBUG | |
139 | if (unlikely((verdict & NF_VERDICT_MASK) | |
140 | > NF_MAX_VERDICT)) { | |
141 | NFDEBUG("Evil return from %p(%u).\n", | |
601e68e1 | 142 | elem->hook, hook); |
f6ebe77f HW |
143 | continue; |
144 | } | |
145 | #endif | |
146 | if (verdict != NF_REPEAT) | |
147 | return verdict; | |
148 | *i = (*i)->prev; | |
149 | } | |
150 | } | |
151 | return NF_ACCEPT; | |
152 | } | |
153 | ||
154 | ||
155 | /* Returns 1 if okfn() needs to be executed by the caller, | |
156 | * -EPERM for NF_DROP, 0 otherwise. */ | |
76108cea | 157 | int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb, |
f6ebe77f HW |
158 | struct net_device *indev, |
159 | struct net_device *outdev, | |
160 | int (*okfn)(struct sk_buff *), | |
161 | int hook_thresh) | |
162 | { | |
163 | struct list_head *elem; | |
164 | unsigned int verdict; | |
165 | int ret = 0; | |
166 | ||
167 | /* We may already have this, but read-locks nest anyway */ | |
168 | rcu_read_lock(); | |
169 | ||
170 | elem = &nf_hooks[pf][hook]; | |
171 | next_hook: | |
3db05fea | 172 | verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev, |
f6ebe77f HW |
173 | outdev, &elem, okfn, hook_thresh); |
174 | if (verdict == NF_ACCEPT || verdict == NF_STOP) { | |
175 | ret = 1; | |
f6ebe77f | 176 | } else if (verdict == NF_DROP) { |
3db05fea | 177 | kfree_skb(skb); |
f6ebe77f | 178 | ret = -EPERM; |
f9c63990 | 179 | } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) { |
3db05fea | 180 | if (!nf_queue(skb, elem, pf, hook, indev, outdev, okfn, |
f6ebe77f HW |
181 | verdict >> NF_VERDICT_BITS)) |
182 | goto next_hook; | |
183 | } | |
f6ebe77f HW |
184 | rcu_read_unlock(); |
185 | return ret; | |
186 | } | |
187 | EXPORT_SYMBOL(nf_hook_slow); | |
188 | ||
189 | ||
37d41879 | 190 | int skb_make_writable(struct sk_buff *skb, unsigned int writable_len) |
f6ebe77f | 191 | { |
37d41879 | 192 | if (writable_len > skb->len) |
f6ebe77f HW |
193 | return 0; |
194 | ||
195 | /* Not exclusive use of packet? Must copy. */ | |
37d41879 HX |
196 | if (!skb_cloned(skb)) { |
197 | if (writable_len <= skb_headlen(skb)) | |
198 | return 1; | |
199 | } else if (skb_clone_writable(skb, writable_len)) | |
200 | return 1; | |
201 | ||
202 | if (writable_len <= skb_headlen(skb)) | |
203 | writable_len = 0; | |
204 | else | |
205 | writable_len -= skb_headlen(skb); | |
206 | ||
207 | return !!__pskb_pull_tail(skb, writable_len); | |
f6ebe77f HW |
208 | } |
209 | EXPORT_SYMBOL(skb_make_writable); | |
210 | ||
5f79e0f9 | 211 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
f6ebe77f HW |
212 | /* This does not belong here, but locally generated errors need it if connection |
213 | tracking in use: without this, connection may not be in hash table, and hence | |
214 | manufactured ICMP or RST packets will not be associated with it. */ | |
215 | void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *); | |
216 | EXPORT_SYMBOL(ip_ct_attach); | |
217 | ||
218 | void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) | |
219 | { | |
220 | void (*attach)(struct sk_buff *, struct sk_buff *); | |
221 | ||
c3a47ab3 PM |
222 | if (skb->nfct) { |
223 | rcu_read_lock(); | |
224 | attach = rcu_dereference(ip_ct_attach); | |
225 | if (attach) | |
226 | attach(new, skb); | |
227 | rcu_read_unlock(); | |
f6ebe77f HW |
228 | } |
229 | } | |
230 | EXPORT_SYMBOL(nf_ct_attach); | |
de6e05c4 YK |
231 | |
232 | void (*nf_ct_destroy)(struct nf_conntrack *); | |
233 | EXPORT_SYMBOL(nf_ct_destroy); | |
234 | ||
235 | void nf_conntrack_destroy(struct nf_conntrack *nfct) | |
236 | { | |
237 | void (*destroy)(struct nf_conntrack *); | |
238 | ||
239 | rcu_read_lock(); | |
240 | destroy = rcu_dereference(nf_ct_destroy); | |
241 | BUG_ON(destroy == NULL); | |
242 | destroy(nfct); | |
243 | rcu_read_unlock(); | |
244 | } | |
245 | EXPORT_SYMBOL(nf_conntrack_destroy); | |
246 | #endif /* CONFIG_NF_CONNTRACK */ | |
f6ebe77f HW |
247 | |
248 | #ifdef CONFIG_PROC_FS | |
249 | struct proc_dir_entry *proc_net_netfilter; | |
250 | EXPORT_SYMBOL(proc_net_netfilter); | |
251 | #endif | |
252 | ||
253 | void __init netfilter_init(void) | |
254 | { | |
255 | int i, h; | |
7e9c6eeb | 256 | for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) { |
f6ebe77f HW |
257 | for (h = 0; h < NF_MAX_HOOKS; h++) |
258 | INIT_LIST_HEAD(&nf_hooks[i][h]); | |
259 | } | |
260 | ||
261 | #ifdef CONFIG_PROC_FS | |
457c4cbc | 262 | proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net); |
f6ebe77f HW |
263 | if (!proc_net_netfilter) |
264 | panic("cannot create netfilter proc entry"); | |
265 | #endif | |
266 | ||
267 | if (netfilter_queue_init() < 0) | |
268 | panic("cannot initialize nf_queue"); | |
269 | if (netfilter_log_init() < 0) | |
270 | panic("cannot initialize nf_log"); | |
271 | } | |
4f536522 PM |
272 | |
273 | #ifdef CONFIG_SYSCTL | |
274 | struct ctl_path nf_net_netfilter_sysctl_path[] = { | |
f8572d8f EB |
275 | { .procname = "net", }, |
276 | { .procname = "netfilter", }, | |
4f536522 PM |
277 | { } |
278 | }; | |
279 | EXPORT_SYMBOL_GPL(nf_net_netfilter_sysctl_path); | |
280 | #endif /* CONFIG_SYSCTL */ |