Root/
1 | /* netfilter.c: look after the filters for various protocols. |
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. |
8 | */ |
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> |
21 | #include <linux/mutex.h> |
22 | #include <linux/slab.h> |
23 | #include <net/net_namespace.h> |
24 | #include <net/sock.h> |
25 | |
26 | #include "nf_internals.h" |
27 | |
28 | static DEFINE_MUTEX(afinfo_mutex); |
29 | |
30 | const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly; |
31 | EXPORT_SYMBOL(nf_afinfo); |
32 | |
33 | int nf_register_afinfo(const struct nf_afinfo *afinfo) |
34 | { |
35 | int err; |
36 | |
37 | err = mutex_lock_interruptible(&afinfo_mutex); |
38 | if (err < 0) |
39 | return err; |
40 | rcu_assign_pointer(nf_afinfo[afinfo->family], afinfo); |
41 | mutex_unlock(&afinfo_mutex); |
42 | return 0; |
43 | } |
44 | EXPORT_SYMBOL_GPL(nf_register_afinfo); |
45 | |
46 | void nf_unregister_afinfo(const struct nf_afinfo *afinfo) |
47 | { |
48 | mutex_lock(&afinfo_mutex); |
49 | rcu_assign_pointer(nf_afinfo[afinfo->family], NULL); |
50 | mutex_unlock(&afinfo_mutex); |
51 | synchronize_rcu(); |
52 | } |
53 | EXPORT_SYMBOL_GPL(nf_unregister_afinfo); |
54 | |
55 | struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly; |
56 | EXPORT_SYMBOL(nf_hooks); |
57 | static DEFINE_MUTEX(nf_hook_mutex); |
58 | |
59 | int nf_register_hook(struct nf_hook_ops *reg) |
60 | { |
61 | struct nf_hook_ops *elem; |
62 | int err; |
63 | |
64 | err = mutex_lock_interruptible(&nf_hook_mutex); |
65 | if (err < 0) |
66 | return err; |
67 | list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) { |
68 | if (reg->priority < elem->priority) |
69 | break; |
70 | } |
71 | list_add_rcu(®->list, elem->list.prev); |
72 | mutex_unlock(&nf_hook_mutex); |
73 | return 0; |
74 | } |
75 | EXPORT_SYMBOL(nf_register_hook); |
76 | |
77 | void nf_unregister_hook(struct nf_hook_ops *reg) |
78 | { |
79 | mutex_lock(&nf_hook_mutex); |
80 | list_del_rcu(®->list); |
81 | mutex_unlock(&nf_hook_mutex); |
82 | |
83 | synchronize_net(); |
84 | } |
85 | EXPORT_SYMBOL(nf_unregister_hook); |
86 | |
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 | { |
108 | unsigned int i; |
109 | |
110 | for (i = 0; i < n; i++) |
111 | nf_unregister_hook(®[i]); |
112 | } |
113 | EXPORT_SYMBOL(nf_unregister_hooks); |
114 | |
115 | unsigned int nf_iterate(struct list_head *head, |
116 | struct sk_buff *skb, |
117 | unsigned int hook, |
118 | const struct net_device *indev, |
119 | const struct net_device *outdev, |
120 | struct list_head **i, |
121 | int (*okfn)(struct sk_buff *), |
122 | int hook_thresh) |
123 | { |
124 | unsigned int verdict; |
125 | |
126 | /* |
127 | * The caller must not block between calls to this |
128 | * function because of risk of continuing from deleted element. |
129 | */ |
130 | list_for_each_continue_rcu(*i, head) { |
131 | struct nf_hook_ops *elem = (struct nf_hook_ops *)*i; |
132 | |
133 | if (hook_thresh > elem->priority) |
134 | continue; |
135 | |
136 | /* Optimization: we don't need to hold module |
137 | reference here, since function can't sleep. --RR */ |
138 | verdict = elem->hook(hook, skb, indev, outdev, okfn); |
139 | if (verdict != NF_ACCEPT) { |
140 | #ifdef CONFIG_NETFILTER_DEBUG |
141 | if (unlikely((verdict & NF_VERDICT_MASK) |
142 | > NF_MAX_VERDICT)) { |
143 | NFDEBUG("Evil return from %p(%u).\n", |
144 | elem->hook, hook); |
145 | continue; |
146 | } |
147 | #endif |
148 | if (verdict != NF_REPEAT) |
149 | return verdict; |
150 | *i = (*i)->prev; |
151 | } |
152 | } |
153 | return NF_ACCEPT; |
154 | } |
155 | |
156 | |
157 | /* Returns 1 if okfn() needs to be executed by the caller, |
158 | * -EPERM for NF_DROP, 0 otherwise. */ |
159 | int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb, |
160 | struct net_device *indev, |
161 | struct net_device *outdev, |
162 | int (*okfn)(struct sk_buff *), |
163 | int hook_thresh) |
164 | { |
165 | struct list_head *elem; |
166 | unsigned int verdict; |
167 | int ret = 0; |
168 | |
169 | /* We may already have this, but read-locks nest anyway */ |
170 | rcu_read_lock(); |
171 | |
172 | elem = &nf_hooks[pf][hook]; |
173 | next_hook: |
174 | verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev, |
175 | outdev, &elem, okfn, hook_thresh); |
176 | if (verdict == NF_ACCEPT || verdict == NF_STOP) { |
177 | ret = 1; |
178 | } else if (verdict == NF_DROP) { |
179 | kfree_skb(skb); |
180 | ret = -EPERM; |
181 | } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) { |
182 | if (!nf_queue(skb, elem, pf, hook, indev, outdev, okfn, |
183 | verdict >> NF_VERDICT_BITS)) |
184 | goto next_hook; |
185 | } |
186 | rcu_read_unlock(); |
187 | return ret; |
188 | } |
189 | EXPORT_SYMBOL(nf_hook_slow); |
190 | |
191 | |
192 | int skb_make_writable(struct sk_buff *skb, unsigned int writable_len) |
193 | { |
194 | if (writable_len > skb->len) |
195 | return 0; |
196 | |
197 | /* Not exclusive use of packet? Must copy. */ |
198 | if (!skb_cloned(skb)) { |
199 | if (writable_len <= skb_headlen(skb)) |
200 | return 1; |
201 | } else if (skb_clone_writable(skb, writable_len)) |
202 | return 1; |
203 | |
204 | if (writable_len <= skb_headlen(skb)) |
205 | writable_len = 0; |
206 | else |
207 | writable_len -= skb_headlen(skb); |
208 | |
209 | return !!__pskb_pull_tail(skb, writable_len); |
210 | } |
211 | EXPORT_SYMBOL(skb_make_writable); |
212 | |
213 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
214 | /* This does not belong here, but locally generated errors need it if connection |
215 | tracking in use: without this, connection may not be in hash table, and hence |
216 | manufactured ICMP or RST packets will not be associated with it. */ |
217 | void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *); |
218 | EXPORT_SYMBOL(ip_ct_attach); |
219 | |
220 | void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) |
221 | { |
222 | void (*attach)(struct sk_buff *, struct sk_buff *); |
223 | |
224 | if (skb->nfct) { |
225 | rcu_read_lock(); |
226 | attach = rcu_dereference(ip_ct_attach); |
227 | if (attach) |
228 | attach(new, skb); |
229 | rcu_read_unlock(); |
230 | } |
231 | } |
232 | EXPORT_SYMBOL(nf_ct_attach); |
233 | |
234 | void (*nf_ct_destroy)(struct nf_conntrack *); |
235 | EXPORT_SYMBOL(nf_ct_destroy); |
236 | |
237 | void nf_conntrack_destroy(struct nf_conntrack *nfct) |
238 | { |
239 | void (*destroy)(struct nf_conntrack *); |
240 | |
241 | rcu_read_lock(); |
242 | destroy = rcu_dereference(nf_ct_destroy); |
243 | BUG_ON(destroy == NULL); |
244 | destroy(nfct); |
245 | rcu_read_unlock(); |
246 | } |
247 | EXPORT_SYMBOL(nf_conntrack_destroy); |
248 | #endif /* CONFIG_NF_CONNTRACK */ |
249 | |
250 | #ifdef CONFIG_PROC_FS |
251 | struct proc_dir_entry *proc_net_netfilter; |
252 | EXPORT_SYMBOL(proc_net_netfilter); |
253 | #endif |
254 | |
255 | void __init netfilter_init(void) |
256 | { |
257 | int i, h; |
258 | for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) { |
259 | for (h = 0; h < NF_MAX_HOOKS; h++) |
260 | INIT_LIST_HEAD(&nf_hooks[i][h]); |
261 | } |
262 | |
263 | #ifdef CONFIG_PROC_FS |
264 | proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net); |
265 | if (!proc_net_netfilter) |
266 | panic("cannot create netfilter proc entry"); |
267 | #endif |
268 | |
269 | if (netfilter_queue_init() < 0) |
270 | panic("cannot initialize nf_queue"); |
271 | if (netfilter_log_init() < 0) |
272 | panic("cannot initialize nf_log"); |
273 | } |
274 | |
275 | #ifdef CONFIG_SYSCTL |
276 | struct ctl_path nf_net_netfilter_sysctl_path[] = { |
277 | { .procname = "net", }, |
278 | { .procname = "netfilter", }, |
279 | { } |
280 | }; |
281 | EXPORT_SYMBOL_GPL(nf_net_netfilter_sysctl_path); |
282 | #endif /* CONFIG_SYSCTL */ |
283 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9