| 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/netfilter_ipv6.h> |
| 21 | #include <linux/inetdevice.h> |
| 22 | #include <linux/proc_fs.h> |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <net/net_namespace.h> |
| 26 | #include <net/sock.h> |
| 27 | |
| 28 | #include "nf_internals.h" |
| 29 | |
| 30 | static DEFINE_MUTEX(afinfo_mutex); |
| 31 | |
| 32 | const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly; |
| 33 | EXPORT_SYMBOL(nf_afinfo); |
| 34 | const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly; |
| 35 | EXPORT_SYMBOL_GPL(nf_ipv6_ops); |
| 36 | |
| 37 | int nf_register_afinfo(const struct nf_afinfo *afinfo) |
| 38 | { |
| 39 | mutex_lock(&afinfo_mutex); |
| 40 | RCU_INIT_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_INIT_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 | |
| 58 | #ifdef HAVE_JUMP_LABEL |
| 59 | struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; |
| 60 | EXPORT_SYMBOL(nf_hooks_needed); |
| 61 | #endif |
| 62 | |
| 63 | static DEFINE_MUTEX(nf_hook_mutex); |
| 64 | |
| 65 | int nf_register_hook(struct nf_hook_ops *reg) |
| 66 | { |
| 67 | struct nf_hook_ops *elem; |
| 68 | |
| 69 | mutex_lock(&nf_hook_mutex); |
| 70 | list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) { |
| 71 | if (reg->priority < elem->priority) |
| 72 | break; |
| 73 | } |
| 74 | list_add_rcu(®->list, elem->list.prev); |
| 75 | mutex_unlock(&nf_hook_mutex); |
| 76 | #ifdef HAVE_JUMP_LABEL |
| 77 | static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]); |
| 78 | #endif |
| 79 | return 0; |
| 80 | } |
| 81 | EXPORT_SYMBOL(nf_register_hook); |
| 82 | |
| 83 | void nf_unregister_hook(struct nf_hook_ops *reg) |
| 84 | { |
| 85 | mutex_lock(&nf_hook_mutex); |
| 86 | list_del_rcu(®->list); |
| 87 | mutex_unlock(&nf_hook_mutex); |
| 88 | #ifdef HAVE_JUMP_LABEL |
| 89 | static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]); |
| 90 | #endif |
| 91 | synchronize_net(); |
| 92 | } |
| 93 | EXPORT_SYMBOL(nf_unregister_hook); |
| 94 | |
| 95 | int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n) |
| 96 | { |
| 97 | unsigned int i; |
| 98 | int err = 0; |
| 99 | |
| 100 | for (i = 0; i < n; i++) { |
| 101 | err = nf_register_hook(®[i]); |
| 102 | if (err) |
| 103 | goto err; |
| 104 | } |
| 105 | return err; |
| 106 | |
| 107 | err: |
| 108 | if (i > 0) |
| 109 | nf_unregister_hooks(reg, i); |
| 110 | return err; |
| 111 | } |
| 112 | EXPORT_SYMBOL(nf_register_hooks); |
| 113 | |
| 114 | void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n) |
| 115 | { |
| 116 | while (n-- > 0) |
| 117 | nf_unregister_hook(®[n]); |
| 118 | } |
| 119 | EXPORT_SYMBOL(nf_unregister_hooks); |
| 120 | |
| 121 | unsigned int nf_iterate(struct list_head *head, |
| 122 | struct sk_buff *skb, |
| 123 | struct nf_hook_state *state, |
| 124 | struct nf_hook_ops **elemp) |
| 125 | { |
| 126 | unsigned int verdict; |
| 127 | |
| 128 | /* |
| 129 | * The caller must not block between calls to this |
| 130 | * function because of risk of continuing from deleted element. |
| 131 | */ |
| 132 | list_for_each_entry_continue_rcu((*elemp), head, list) { |
| 133 | if (state->thresh > (*elemp)->priority) |
| 134 | continue; |
| 135 | |
| 136 | /* Optimization: we don't need to hold module |
| 137 | reference here, since function can't sleep. --RR */ |
| 138 | repeat: |
| 139 | verdict = (*elemp)->hook(*elemp, skb, state); |
| 140 | if (verdict != NF_ACCEPT) { |
| 141 | #ifdef CONFIG_NETFILTER_DEBUG |
| 142 | if (unlikely((verdict & NF_VERDICT_MASK) |
| 143 | > NF_MAX_VERDICT)) { |
| 144 | NFDEBUG("Evil return from %p(%u).\n", |
| 145 | (*elemp)->hook, state->hook); |
| 146 | continue; |
| 147 | } |
| 148 | #endif |
| 149 | if (verdict != NF_REPEAT) |
| 150 | return verdict; |
| 151 | goto repeat; |
| 152 | } |
| 153 | } |
| 154 | return NF_ACCEPT; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /* Returns 1 if okfn() needs to be executed by the caller, |
| 159 | * -EPERM for NF_DROP, 0 otherwise. */ |
| 160 | int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state) |
| 161 | { |
| 162 | struct nf_hook_ops *elem; |
| 163 | unsigned int verdict; |
| 164 | int ret = 0; |
| 165 | |
| 166 | /* We may already have this, but read-locks nest anyway */ |
| 167 | rcu_read_lock(); |
| 168 | |
| 169 | elem = list_entry_rcu(state->hook_list, struct nf_hook_ops, list); |
| 170 | next_hook: |
| 171 | verdict = nf_iterate(state->hook_list, skb, state, &elem); |
| 172 | if (verdict == NF_ACCEPT || verdict == NF_STOP) { |
| 173 | ret = 1; |
| 174 | } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) { |
| 175 | kfree_skb(skb); |
| 176 | ret = NF_DROP_GETERR(verdict); |
| 177 | if (ret == 0) |
| 178 | ret = -EPERM; |
| 179 | } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) { |
| 180 | int err = nf_queue(skb, elem, state, |
| 181 | verdict >> NF_VERDICT_QBITS); |
| 182 | if (err < 0) { |
| 183 | if (err == -ECANCELED) |
| 184 | goto next_hook; |
| 185 | if (err == -ESRCH && |
| 186 | (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS)) |
| 187 | goto next_hook; |
| 188 | kfree_skb(skb); |
| 189 | } |
| 190 | } |
| 191 | rcu_read_unlock(); |
| 192 | return ret; |
| 193 | } |
| 194 | EXPORT_SYMBOL(nf_hook_slow); |
| 195 | |
| 196 | |
| 197 | int skb_make_writable(struct sk_buff *skb, unsigned int writable_len) |
| 198 | { |
| 199 | if (writable_len > skb->len) |
| 200 | return 0; |
| 201 | |
| 202 | /* Not exclusive use of packet? Must copy. */ |
| 203 | if (!skb_cloned(skb)) { |
| 204 | if (writable_len <= skb_headlen(skb)) |
| 205 | return 1; |
| 206 | } else if (skb_clone_writable(skb, writable_len)) |
| 207 | return 1; |
| 208 | |
| 209 | if (writable_len <= skb_headlen(skb)) |
| 210 | writable_len = 0; |
| 211 | else |
| 212 | writable_len -= skb_headlen(skb); |
| 213 | |
| 214 | return !!__pskb_pull_tail(skb, writable_len); |
| 215 | } |
| 216 | EXPORT_SYMBOL(skb_make_writable); |
| 217 | |
| 218 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
| 219 | /* This does not belong here, but locally generated errors need it if connection |
| 220 | tracking in use: without this, connection may not be in hash table, and hence |
| 221 | manufactured ICMP or RST packets will not be associated with it. */ |
| 222 | void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) |
| 223 | __rcu __read_mostly; |
| 224 | EXPORT_SYMBOL(ip_ct_attach); |
| 225 | |
| 226 | void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb) |
| 227 | { |
| 228 | void (*attach)(struct sk_buff *, const struct sk_buff *); |
| 229 | |
| 230 | if (skb->nfct) { |
| 231 | rcu_read_lock(); |
| 232 | attach = rcu_dereference(ip_ct_attach); |
| 233 | if (attach) |
| 234 | attach(new, skb); |
| 235 | rcu_read_unlock(); |
| 236 | } |
| 237 | } |
| 238 | EXPORT_SYMBOL(nf_ct_attach); |
| 239 | |
| 240 | void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly; |
| 241 | EXPORT_SYMBOL(nf_ct_destroy); |
| 242 | |
| 243 | void nf_conntrack_destroy(struct nf_conntrack *nfct) |
| 244 | { |
| 245 | void (*destroy)(struct nf_conntrack *); |
| 246 | |
| 247 | rcu_read_lock(); |
| 248 | destroy = rcu_dereference(nf_ct_destroy); |
| 249 | BUG_ON(destroy == NULL); |
| 250 | destroy(nfct); |
| 251 | rcu_read_unlock(); |
| 252 | } |
| 253 | EXPORT_SYMBOL(nf_conntrack_destroy); |
| 254 | |
| 255 | struct nfq_ct_hook __rcu *nfq_ct_hook __read_mostly; |
| 256 | EXPORT_SYMBOL_GPL(nfq_ct_hook); |
| 257 | |
| 258 | struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook __read_mostly; |
| 259 | EXPORT_SYMBOL_GPL(nfq_ct_nat_hook); |
| 260 | |
| 261 | #endif /* CONFIG_NF_CONNTRACK */ |
| 262 | |
| 263 | #ifdef CONFIG_NF_NAT_NEEDED |
| 264 | void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *); |
| 265 | EXPORT_SYMBOL(nf_nat_decode_session_hook); |
| 266 | #endif |
| 267 | |
| 268 | static int __net_init netfilter_net_init(struct net *net) |
| 269 | { |
| 270 | #ifdef CONFIG_PROC_FS |
| 271 | net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter", |
| 272 | net->proc_net); |
| 273 | if (!net->nf.proc_netfilter) { |
| 274 | if (!net_eq(net, &init_net)) |
| 275 | pr_err("cannot create netfilter proc entry"); |
| 276 | |
| 277 | return -ENOMEM; |
| 278 | } |
| 279 | #endif |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static void __net_exit netfilter_net_exit(struct net *net) |
| 284 | { |
| 285 | remove_proc_entry("netfilter", net->proc_net); |
| 286 | } |
| 287 | |
| 288 | static struct pernet_operations netfilter_net_ops = { |
| 289 | .init = netfilter_net_init, |
| 290 | .exit = netfilter_net_exit, |
| 291 | }; |
| 292 | |
| 293 | int __init netfilter_init(void) |
| 294 | { |
| 295 | int i, h, ret; |
| 296 | |
| 297 | for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) { |
| 298 | for (h = 0; h < NF_MAX_HOOKS; h++) |
| 299 | INIT_LIST_HEAD(&nf_hooks[i][h]); |
| 300 | } |
| 301 | |
| 302 | ret = register_pernet_subsys(&netfilter_net_ops); |
| 303 | if (ret < 0) |
| 304 | goto err; |
| 305 | |
| 306 | ret = netfilter_log_init(); |
| 307 | if (ret < 0) |
| 308 | goto err_pernet; |
| 309 | |
| 310 | return 0; |
| 311 | err_pernet: |
| 312 | unregister_pernet_subsys(&netfilter_net_ops); |
| 313 | err: |
| 314 | return ret; |
| 315 | } |