~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/core.c

Version: ~ [ linux-5.16 ] ~ [ linux-5.15.13 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.90 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.170 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.224 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.261 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.296 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.298 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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  * Patrick McHardy (c) 2006-2012
  9  */
 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>
 22 #include <linux/mutex.h>
 23 #include <linux/slab.h>
 24 #include <net/net_namespace.h>
 25 #include <net/sock.h>
 26 
 27 #include "nf_internals.h"
 28 
 29 static DEFINE_MUTEX(afinfo_mutex);
 30 
 31 const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
 32 EXPORT_SYMBOL(nf_afinfo);
 33 const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly;
 34 EXPORT_SYMBOL_GPL(nf_ipv6_ops);
 35 
 36 int nf_register_afinfo(const struct nf_afinfo *afinfo)
 37 {
 38         int err;
 39 
 40         err = mutex_lock_interruptible(&afinfo_mutex);
 41         if (err < 0)
 42                 return err;
 43         RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo);
 44         mutex_unlock(&afinfo_mutex);
 45         return 0;
 46 }
 47 EXPORT_SYMBOL_GPL(nf_register_afinfo);
 48 
 49 void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
 50 {
 51         mutex_lock(&afinfo_mutex);
 52         RCU_INIT_POINTER(nf_afinfo[afinfo->family], NULL);
 53         mutex_unlock(&afinfo_mutex);
 54         synchronize_rcu();
 55 }
 56 EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
 57 
 58 struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly;
 59 EXPORT_SYMBOL(nf_hooks);
 60 
 61 #if defined(CONFIG_JUMP_LABEL)
 62 struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
 63 EXPORT_SYMBOL(nf_hooks_needed);
 64 #endif
 65 
 66 static DEFINE_MUTEX(nf_hook_mutex);
 67 
 68 int nf_register_hook(struct nf_hook_ops *reg)
 69 {
 70         struct nf_hook_ops *elem;
 71         int err;
 72 
 73         err = mutex_lock_interruptible(&nf_hook_mutex);
 74         if (err < 0)
 75                 return err;
 76         list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
 77                 if (reg->priority < elem->priority)
 78                         break;
 79         }
 80         list_add_rcu(&reg->list, elem->list.prev);
 81         mutex_unlock(&nf_hook_mutex);
 82 #if defined(CONFIG_JUMP_LABEL)
 83         static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
 84 #endif
 85         return 0;
 86 }
 87 EXPORT_SYMBOL(nf_register_hook);
 88 
 89 void nf_unregister_hook(struct nf_hook_ops *reg)
 90 {
 91         mutex_lock(&nf_hook_mutex);
 92         list_del_rcu(&reg->list);
 93         mutex_unlock(&nf_hook_mutex);
 94 #if defined(CONFIG_JUMP_LABEL)
 95         static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
 96 #endif
 97         synchronize_net();
 98         nf_queue_nf_hook_drop(reg);
 99 }
100 EXPORT_SYMBOL(nf_unregister_hook);
101 
102 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
103 {
104         unsigned int i;
105         int err = 0;
106 
107         for (i = 0; i < n; i++) {
108                 err = nf_register_hook(&reg[i]);
109                 if (err)
110                         goto err;
111         }
112         return err;
113 
114 err:
115         if (i > 0)
116                 nf_unregister_hooks(reg, i);
117         return err;
118 }
119 EXPORT_SYMBOL(nf_register_hooks);
120 
121 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
122 {
123         while (n-- > 0)
124                 nf_unregister_hook(&reg[n]);
125 }
126 EXPORT_SYMBOL(nf_unregister_hooks);
127 
128 unsigned int nf_iterate(struct list_head *head,
129                         struct sk_buff *skb,
130                         unsigned int hook,
131                         const struct net_device *indev,
132                         const struct net_device *outdev,
133                         struct nf_hook_ops **elemp,
134                         int (*okfn)(struct sk_buff *),
135                         int hook_thresh)
136 {
137         unsigned int verdict;
138 
139         /*
140          * The caller must not block between calls to this
141          * function because of risk of continuing from deleted element.
142          */
143         list_for_each_entry_continue_rcu((*elemp), head, list) {
144                 if (hook_thresh > (*elemp)->priority)
145                         continue;
146 
147                 /* Optimization: we don't need to hold module
148                    reference here, since function can't sleep. --RR */
149 repeat:
150                 verdict = (*elemp)->hook(*elemp, skb, indev, outdev, okfn);
151                 if (verdict != NF_ACCEPT) {
152 #ifdef CONFIG_NETFILTER_DEBUG
153                         if (unlikely((verdict & NF_VERDICT_MASK)
154                                                         > NF_MAX_VERDICT)) {
155                                 NFDEBUG("Evil return from %p(%u).\n",
156                                         (*elemp)->hook, hook);
157                                 continue;
158                         }
159 #endif
160                         if (verdict != NF_REPEAT)
161                                 return verdict;
162                         goto repeat;
163                 }
164         }
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. */
171 int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
172                  struct net_device *indev,
173                  struct net_device *outdev,
174                  int (*okfn)(struct sk_buff *),
175                  int hook_thresh)
176 {
177         struct nf_hook_ops *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 = list_entry_rcu(&nf_hooks[pf][hook], struct nf_hook_ops, list);
185 next_hook:
186         verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
187                              outdev, &elem, okfn, hook_thresh);
188         if (verdict == NF_ACCEPT || verdict == NF_STOP) {
189                 ret = 1;
190         } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
191                 kfree_skb(skb);
192                 ret = NF_DROP_GETERR(verdict);
193                 if (ret == 0)
194                         ret = -EPERM;
195         } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
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)
200                                 goto next_hook;
201                         if (err == -ESRCH &&
202                            (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
203                                 goto next_hook;
204                         kfree_skb(skb);
205                 }
206         }
207         rcu_read_unlock();
208         return ret;
209 }
210 EXPORT_SYMBOL(nf_hook_slow);
211 
212 
213 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
214 {
215         if (writable_len > skb->len)
216                 return 0;
217 
218         /* Not exclusive use of packet?  Must copy. */
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);
231 }
232 EXPORT_SYMBOL(skb_make_writable);
233 
234 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
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. */
238 void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *)
239                 __rcu __read_mostly;
240 EXPORT_SYMBOL(ip_ct_attach);
241 
242 void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb)
243 {
244         void (*attach)(struct sk_buff *, const struct sk_buff *);
245 
246         if (skb->nfct) {
247                 rcu_read_lock();
248                 attach = rcu_dereference(ip_ct_attach);
249                 if (attach)
250                         attach(new, skb);
251                 rcu_read_unlock();
252         }
253 }
254 EXPORT_SYMBOL(nf_ct_attach);
255 
256 void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
257 EXPORT_SYMBOL(nf_ct_destroy);
258 
259 void nf_conntrack_destroy(struct nf_conntrack *nfct)
260 {
261         void (*destroy)(struct nf_conntrack *);
262 
263         rcu_read_lock();
264         destroy = rcu_dereference(nf_ct_destroy);
265         BUG_ON(destroy == NULL);
266         destroy(nfct);
267         rcu_read_unlock();
268 }
269 EXPORT_SYMBOL(nf_conntrack_destroy);
270 
271 struct nfq_ct_hook __rcu *nfq_ct_hook __read_mostly;
272 EXPORT_SYMBOL_GPL(nfq_ct_hook);
273 
274 struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook __read_mostly;
275 EXPORT_SYMBOL_GPL(nfq_ct_nat_hook);
276 
277 #endif /* CONFIG_NF_CONNTRACK */
278 
279 #ifdef CONFIG_NF_NAT_NEEDED
280 void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
281 EXPORT_SYMBOL(nf_nat_decode_session_hook);
282 #endif
283 
284 static int __net_init netfilter_net_init(struct net *net)
285 {
286 #ifdef CONFIG_PROC_FS
287         net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
288                                                 net->proc_net);
289         if (!net->nf.proc_netfilter) {
290                 if (!net_eq(net, &init_net))
291                         pr_err("cannot create netfilter proc entry");
292 
293                 return -ENOMEM;
294         }
295 #endif
296         return 0;
297 }
298 
299 static void __net_exit netfilter_net_exit(struct net *net)
300 {
301         remove_proc_entry("netfilter", net->proc_net);
302 }
303 
304 static struct pernet_operations netfilter_net_ops = {
305         .init = netfilter_net_init,
306         .exit = netfilter_net_exit,
307 };
308 
309 int __init netfilter_init(void)
310 {
311         int i, h, ret;
312 
313         for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
314                 for (h = 0; h < NF_MAX_HOOKS; h++)
315                         INIT_LIST_HEAD(&nf_hooks[i][h]);
316         }
317 
318         ret = register_pernet_subsys(&netfilter_net_ops);
319         if (ret < 0)
320                 goto err;
321 
322         ret = netfilter_log_init();
323         if (ret < 0)
324                 goto err_pernet;
325 
326         return 0;
327 err_pernet:
328         unregister_pernet_subsys(&netfilter_net_ops);
329 err:
330         return ret;
331 }
332 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp