YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1 | /* netfilter.c: look after the filters for various protocols. |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 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. |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 8 | */ |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 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> |
Patrick McHardy | d486dd1 | 2007-02-12 11:09:55 -0800 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 23 | #include <net/net_namespace.h> |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 24 | #include <net/sock.h> |
| 25 | |
| 26 | #include "nf_internals.h" |
| 27 | |
Patrick McHardy | d486dd1 | 2007-02-12 11:09:55 -0800 | [diff] [blame] | 28 | static DEFINE_MUTEX(afinfo_mutex); |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 29 | |
Arnd Bergmann | 0906a37 | 2010-03-09 20:59:15 +0100 | [diff] [blame] | 30 | const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly; |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 31 | EXPORT_SYMBOL(nf_afinfo); |
| 32 | |
Patrick McHardy | 1e796fd | 2007-12-17 22:42:27 -0800 | [diff] [blame] | 33 | int nf_register_afinfo(const struct nf_afinfo *afinfo) |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 34 | { |
Patrick McHardy | d486dd1 | 2007-02-12 11:09:55 -0800 | [diff] [blame] | 35 | int err; |
| 36 | |
| 37 | err = mutex_lock_interruptible(&afinfo_mutex); |
| 38 | if (err < 0) |
| 39 | return err; |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 40 | RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo); |
Patrick McHardy | d486dd1 | 2007-02-12 11:09:55 -0800 | [diff] [blame] | 41 | mutex_unlock(&afinfo_mutex); |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | EXPORT_SYMBOL_GPL(nf_register_afinfo); |
| 45 | |
Patrick McHardy | 1e796fd | 2007-12-17 22:42:27 -0800 | [diff] [blame] | 46 | void nf_unregister_afinfo(const struct nf_afinfo *afinfo) |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 47 | { |
Patrick McHardy | d486dd1 | 2007-02-12 11:09:55 -0800 | [diff] [blame] | 48 | mutex_lock(&afinfo_mutex); |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 49 | RCU_INIT_POINTER(nf_afinfo[afinfo->family], NULL); |
Patrick McHardy | d486dd1 | 2007-02-12 11:09:55 -0800 | [diff] [blame] | 50 | mutex_unlock(&afinfo_mutex); |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 51 | synchronize_rcu(); |
| 52 | } |
| 53 | EXPORT_SYMBOL_GPL(nf_unregister_afinfo); |
| 54 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 55 | struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 56 | EXPORT_SYMBOL(nf_hooks); |
Patrick McHardy | fd706d6 | 2007-02-12 11:10:14 -0800 | [diff] [blame] | 57 | static DEFINE_MUTEX(nf_hook_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 58 | |
| 59 | int nf_register_hook(struct nf_hook_ops *reg) |
| 60 | { |
Li Zefan | 4c61097 | 2007-12-04 23:22:26 -0800 | [diff] [blame] | 61 | struct nf_hook_ops *elem; |
Patrick McHardy | fd706d6 | 2007-02-12 11:10:14 -0800 | [diff] [blame] | 62 | int err; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 63 | |
Patrick McHardy | fd706d6 | 2007-02-12 11:10:14 -0800 | [diff] [blame] | 64 | err = mutex_lock_interruptible(&nf_hook_mutex); |
| 65 | if (err < 0) |
| 66 | return err; |
Li Zefan | 4c61097 | 2007-12-04 23:22:26 -0800 | [diff] [blame] | 67 | list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) { |
| 68 | if (reg->priority < elem->priority) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 69 | break; |
| 70 | } |
Li Zefan | 4c61097 | 2007-12-04 23:22:26 -0800 | [diff] [blame] | 71 | list_add_rcu(®->list, elem->list.prev); |
Patrick McHardy | fd706d6 | 2007-02-12 11:10:14 -0800 | [diff] [blame] | 72 | mutex_unlock(&nf_hook_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | EXPORT_SYMBOL(nf_register_hook); |
| 76 | |
| 77 | void nf_unregister_hook(struct nf_hook_ops *reg) |
| 78 | { |
Patrick McHardy | fd706d6 | 2007-02-12 11:10:14 -0800 | [diff] [blame] | 79 | mutex_lock(&nf_hook_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 80 | list_del_rcu(®->list); |
Patrick McHardy | fd706d6 | 2007-02-12 11:10:14 -0800 | [diff] [blame] | 81 | mutex_unlock(&nf_hook_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 82 | |
| 83 | synchronize_net(); |
| 84 | } |
| 85 | EXPORT_SYMBOL(nf_unregister_hook); |
| 86 | |
Patrick McHardy | 972d1cb | 2006-04-06 14:09:12 -0700 | [diff] [blame] | 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 | { |
Changli Gao | f68c530 | 2010-10-04 22:24:12 +0200 | [diff] [blame] | 108 | while (n-- > 0) |
| 109 | nf_unregister_hook(®[n]); |
Patrick McHardy | 972d1cb | 2006-04-06 14:09:12 -0700 | [diff] [blame] | 110 | } |
| 111 | EXPORT_SYMBOL(nf_unregister_hooks); |
| 112 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 113 | unsigned int nf_iterate(struct list_head *head, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 114 | struct sk_buff *skb, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 115 | unsigned int hook, |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 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 |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 135 | reference here, since function can't sleep. --RR */ |
Patrick McHardy | de9963f | 2011-02-14 17:35:07 +0100 | [diff] [blame] | 136 | repeat: |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 137 | verdict = elem->hook(hook, skb, indev, outdev, okfn); |
| 138 | if (verdict != NF_ACCEPT) { |
| 139 | #ifdef CONFIG_NETFILTER_DEBUG |
| 140 | if (unlikely((verdict & NF_VERDICT_MASK) |
| 141 | > NF_MAX_VERDICT)) { |
| 142 | NFDEBUG("Evil return from %p(%u).\n", |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 143 | elem->hook, hook); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 144 | continue; |
| 145 | } |
| 146 | #endif |
| 147 | if (verdict != NF_REPEAT) |
| 148 | return verdict; |
Patrick McHardy | de9963f | 2011-02-14 17:35:07 +0100 | [diff] [blame] | 149 | goto repeat; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | return NF_ACCEPT; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /* Returns 1 if okfn() needs to be executed by the caller, |
| 157 | * -EPERM for NF_DROP, 0 otherwise. */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 158 | int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb, |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 159 | struct net_device *indev, |
| 160 | struct net_device *outdev, |
| 161 | int (*okfn)(struct sk_buff *), |
| 162 | int hook_thresh) |
| 163 | { |
| 164 | struct list_head *elem; |
| 165 | unsigned int verdict; |
| 166 | int ret = 0; |
| 167 | |
| 168 | /* We may already have this, but read-locks nest anyway */ |
| 169 | rcu_read_lock(); |
| 170 | |
| 171 | elem = &nf_hooks[pf][hook]; |
| 172 | next_hook: |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 173 | verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev, |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 174 | outdev, &elem, okfn, hook_thresh); |
| 175 | if (verdict == NF_ACCEPT || verdict == NF_STOP) { |
| 176 | ret = 1; |
Eric Paris | da68365 | 2010-11-16 11:52:38 +0000 | [diff] [blame] | 177 | } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 178 | kfree_skb(skb); |
Florian Westphal | f615df7 | 2011-01-18 15:52:14 +0100 | [diff] [blame] | 179 | ret = NF_DROP_GETERR(verdict); |
Eric Paris | da68365 | 2010-11-16 11:52:38 +0000 | [diff] [blame] | 180 | if (ret == 0) |
| 181 | ret = -EPERM; |
Patrick McHardy | f9c6399 | 2007-12-05 01:27:46 -0800 | [diff] [blame] | 182 | } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) { |
Florian Westphal | 563e123 | 2011-10-31 12:20:16 +0100 | [diff] [blame] | 183 | int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn, |
| 184 | verdict >> NF_VERDICT_QBITS); |
| 185 | if (err < 0) { |
| 186 | if (err == -ECANCELED) |
Florian Westphal | 06cdb63 | 2011-01-18 15:28:38 +0100 | [diff] [blame] | 187 | goto next_hook; |
Florian Westphal | 563e123 | 2011-10-31 12:20:16 +0100 | [diff] [blame] | 188 | if (err == -ESRCH && |
Florian Westphal | 94b27cc | 2011-01-18 16:08:30 +0100 | [diff] [blame] | 189 | (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS)) |
| 190 | goto next_hook; |
Florian Westphal | 06cdb63 | 2011-01-18 15:28:38 +0100 | [diff] [blame] | 191 | kfree_skb(skb); |
| 192 | } |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 193 | } |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 194 | rcu_read_unlock(); |
| 195 | return ret; |
| 196 | } |
| 197 | EXPORT_SYMBOL(nf_hook_slow); |
| 198 | |
| 199 | |
Herbert Xu | 37d4187 | 2007-10-14 00:39:18 -0700 | [diff] [blame] | 200 | int skb_make_writable(struct sk_buff *skb, unsigned int writable_len) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 201 | { |
Herbert Xu | 37d4187 | 2007-10-14 00:39:18 -0700 | [diff] [blame] | 202 | if (writable_len > skb->len) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 203 | return 0; |
| 204 | |
| 205 | /* Not exclusive use of packet? Must copy. */ |
Herbert Xu | 37d4187 | 2007-10-14 00:39:18 -0700 | [diff] [blame] | 206 | if (!skb_cloned(skb)) { |
| 207 | if (writable_len <= skb_headlen(skb)) |
| 208 | return 1; |
| 209 | } else if (skb_clone_writable(skb, writable_len)) |
| 210 | return 1; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 211 | |
Herbert Xu | 37d4187 | 2007-10-14 00:39:18 -0700 | [diff] [blame] | 212 | if (writable_len <= skb_headlen(skb)) |
| 213 | writable_len = 0; |
| 214 | else |
| 215 | writable_len -= skb_headlen(skb); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 216 | |
Herbert Xu | 37d4187 | 2007-10-14 00:39:18 -0700 | [diff] [blame] | 217 | return !!__pskb_pull_tail(skb, writable_len); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 218 | } |
| 219 | EXPORT_SYMBOL(skb_make_writable); |
| 220 | |
Yasuyuki Kozakai | 5f79e0f | 2007-03-23 11:17:07 -0700 | [diff] [blame] | 221 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 222 | /* This does not belong here, but locally generated errors need it if connection |
| 223 | tracking in use: without this, connection may not be in hash table, and hence |
| 224 | manufactured ICMP or RST packets will not be associated with it. */ |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 225 | void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu __read_mostly; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 226 | EXPORT_SYMBOL(ip_ct_attach); |
| 227 | |
| 228 | void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) |
| 229 | { |
| 230 | void (*attach)(struct sk_buff *, struct sk_buff *); |
| 231 | |
Patrick McHardy | c3a47ab | 2007-02-12 11:09:19 -0800 | [diff] [blame] | 232 | if (skb->nfct) { |
| 233 | rcu_read_lock(); |
| 234 | attach = rcu_dereference(ip_ct_attach); |
| 235 | if (attach) |
| 236 | attach(new, skb); |
| 237 | rcu_read_unlock(); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | EXPORT_SYMBOL(nf_ct_attach); |
Yasuyuki Kozakai | de6e05c | 2007-03-23 11:17:27 -0700 | [diff] [blame] | 241 | |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 242 | void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly; |
Yasuyuki Kozakai | de6e05c | 2007-03-23 11:17:27 -0700 | [diff] [blame] | 243 | EXPORT_SYMBOL(nf_ct_destroy); |
| 244 | |
| 245 | void nf_conntrack_destroy(struct nf_conntrack *nfct) |
| 246 | { |
| 247 | void (*destroy)(struct nf_conntrack *); |
| 248 | |
| 249 | rcu_read_lock(); |
| 250 | destroy = rcu_dereference(nf_ct_destroy); |
| 251 | BUG_ON(destroy == NULL); |
| 252 | destroy(nfct); |
| 253 | rcu_read_unlock(); |
| 254 | } |
| 255 | EXPORT_SYMBOL(nf_conntrack_destroy); |
| 256 | #endif /* CONFIG_NF_CONNTRACK */ |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 257 | |
| 258 | #ifdef CONFIG_PROC_FS |
| 259 | struct proc_dir_entry *proc_net_netfilter; |
| 260 | EXPORT_SYMBOL(proc_net_netfilter); |
| 261 | #endif |
| 262 | |
| 263 | void __init netfilter_init(void) |
| 264 | { |
| 265 | int i, h; |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 266 | for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) { |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 267 | for (h = 0; h < NF_MAX_HOOKS; h++) |
| 268 | INIT_LIST_HEAD(&nf_hooks[i][h]); |
| 269 | } |
| 270 | |
| 271 | #ifdef CONFIG_PROC_FS |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 272 | proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 273 | if (!proc_net_netfilter) |
| 274 | panic("cannot create netfilter proc entry"); |
| 275 | #endif |
| 276 | |
| 277 | if (netfilter_queue_init() < 0) |
| 278 | panic("cannot initialize nf_queue"); |
| 279 | if (netfilter_log_init() < 0) |
| 280 | panic("cannot initialize nf_log"); |
| 281 | } |
Patrick McHardy | 4f53652 | 2008-01-14 23:48:39 -0800 | [diff] [blame] | 282 | |
| 283 | #ifdef CONFIG_SYSCTL |
| 284 | struct ctl_path nf_net_netfilter_sysctl_path[] = { |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 285 | { .procname = "net", }, |
| 286 | { .procname = "netfilter", }, |
Patrick McHardy | 4f53652 | 2008-01-14 23:48:39 -0800 | [diff] [blame] | 287 | { } |
| 288 | }; |
| 289 | EXPORT_SYMBOL_GPL(nf_net_netfilter_sysctl_path); |
| 290 | #endif /* CONFIG_SYSCTL */ |