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 <linux/rcupdate.h> 26 #include <net/net_namespace.h> 27 #include <net/sock.h> 28 29 #include "nf_internals.h" 30 31 static DEFINE_MUTEX(afinfo_mutex); 32 33 const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly; 34 EXPORT_SYMBOL(nf_afinfo); 35 const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly; 36 EXPORT_SYMBOL_GPL(nf_ipv6_ops); 37 38 DEFINE_PER_CPU(bool, nf_skb_duplicated); 39 EXPORT_SYMBOL_GPL(nf_skb_duplicated); 40 41 int nf_register_afinfo(const struct nf_afinfo *afinfo) 42 { 43 mutex_lock(&afinfo_mutex); 44 RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo); 45 mutex_unlock(&afinfo_mutex); 46 return 0; 47 } 48 EXPORT_SYMBOL_GPL(nf_register_afinfo); 49 50 void nf_unregister_afinfo(const struct nf_afinfo *afinfo) 51 { 52 mutex_lock(&afinfo_mutex); 53 RCU_INIT_POINTER(nf_afinfo[afinfo->family], NULL); 54 mutex_unlock(&afinfo_mutex); 55 synchronize_rcu(); 56 } 57 EXPORT_SYMBOL_GPL(nf_unregister_afinfo); 58 59 #ifdef HAVE_JUMP_LABEL 60 struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; 61 EXPORT_SYMBOL(nf_hooks_needed); 62 #endif 63 64 static DEFINE_MUTEX(nf_hook_mutex); 65 #define nf_entry_dereference(e) \ 66 rcu_dereference_protected(e, lockdep_is_held(&nf_hook_mutex)) 67 68 static struct nf_hook_entry __rcu **nf_hook_entry_head(struct net *net, const struct nf_hook_ops *reg) 69 { 70 if (reg->pf != NFPROTO_NETDEV) 71 return net->nf.hooks[reg->pf]+reg->hooknum; 72 73 #ifdef CONFIG_NETFILTER_INGRESS 74 if (reg->hooknum == NF_NETDEV_INGRESS) { 75 if (reg->dev && dev_net(reg->dev) == net) 76 return ®->dev->nf_hooks_ingress; 77 } 78 #endif 79 return NULL; 80 } 81 82 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg) 83 { 84 struct nf_hook_entry __rcu **pp; 85 struct nf_hook_entry *entry, *p; 86 87 if (reg->pf == NFPROTO_NETDEV) { 88 #ifndef CONFIG_NETFILTER_INGRESS 89 if (reg->hooknum == NF_NETDEV_INGRESS) 90 return -EOPNOTSUPP; 91 #endif 92 if (reg->hooknum != NF_NETDEV_INGRESS || 93 !reg->dev || dev_net(reg->dev) != net) 94 return -EINVAL; 95 } 96 97 pp = nf_hook_entry_head(net, reg); 98 if (!pp) 99 return -EINVAL; 100 101 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 102 if (!entry) 103 return -ENOMEM; 104 105 entry->orig_ops = reg; 106 entry->ops = *reg; 107 entry->next = NULL; 108 109 mutex_lock(&nf_hook_mutex); 110 111 /* Find the spot in the list */ 112 while ((p = nf_entry_dereference(*pp)) != NULL) { 113 if (reg->priority < p->orig_ops->priority) 114 break; 115 pp = &p->next; 116 } 117 rcu_assign_pointer(entry->next, p); 118 rcu_assign_pointer(*pp, entry); 119 120 mutex_unlock(&nf_hook_mutex); 121 #ifdef CONFIG_NETFILTER_INGRESS 122 if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS) 123 net_inc_ingress_queue(); 124 #endif 125 #ifdef HAVE_JUMP_LABEL 126 static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]); 127 #endif 128 return 0; 129 } 130 EXPORT_SYMBOL(nf_register_net_hook); 131 132 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg) 133 { 134 struct nf_hook_entry __rcu **pp; 135 struct nf_hook_entry *p; 136 137 pp = nf_hook_entry_head(net, reg); 138 if (WARN_ON_ONCE(!pp)) 139 return; 140 141 mutex_lock(&nf_hook_mutex); 142 while ((p = nf_entry_dereference(*pp)) != NULL) { 143 if (p->orig_ops == reg) { 144 rcu_assign_pointer(*pp, p->next); 145 break; 146 } 147 pp = &p->next; 148 } 149 mutex_unlock(&nf_hook_mutex); 150 if (!p) { 151 WARN(1, "nf_unregister_net_hook: hook not found!\n"); 152 return; 153 } 154 #ifdef CONFIG_NETFILTER_INGRESS 155 if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS) 156 net_dec_ingress_queue(); 157 #endif 158 #ifdef HAVE_JUMP_LABEL 159 static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]); 160 #endif 161 synchronize_net(); 162 nf_queue_nf_hook_drop(net, p); 163 /* other cpu might still process nfqueue verdict that used reg */ 164 synchronize_net(); 165 kfree(p); 166 } 167 EXPORT_SYMBOL(nf_unregister_net_hook); 168 169 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg, 170 unsigned int n) 171 { 172 unsigned int i; 173 int err = 0; 174 175 for (i = 0; i < n; i++) { 176 err = nf_register_net_hook(net, ®[i]); 177 if (err) 178 goto err; 179 } 180 return err; 181 182 err: 183 if (i > 0) 184 nf_unregister_net_hooks(net, reg, i); 185 return err; 186 } 187 EXPORT_SYMBOL(nf_register_net_hooks); 188 189 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg, 190 unsigned int n) 191 { 192 while (n-- > 0) 193 nf_unregister_net_hook(net, ®[n]); 194 } 195 EXPORT_SYMBOL(nf_unregister_net_hooks); 196 197 static LIST_HEAD(nf_hook_list); 198 199 static int _nf_register_hook(struct nf_hook_ops *reg) 200 { 201 struct net *net, *last; 202 int ret; 203 204 for_each_net(net) { 205 ret = nf_register_net_hook(net, reg); 206 if (ret && ret != -ENOENT) 207 goto rollback; 208 } 209 list_add_tail(®->list, &nf_hook_list); 210 211 return 0; 212 rollback: 213 last = net; 214 for_each_net(net) { 215 if (net == last) 216 break; 217 nf_unregister_net_hook(net, reg); 218 } 219 return ret; 220 } 221 222 int nf_register_hook(struct nf_hook_ops *reg) 223 { 224 int ret; 225 226 rtnl_lock(); 227 ret = _nf_register_hook(reg); 228 rtnl_unlock(); 229 230 return ret; 231 } 232 EXPORT_SYMBOL(nf_register_hook); 233 234 static void _nf_unregister_hook(struct nf_hook_ops *reg) 235 { 236 struct net *net; 237 238 list_del(®->list); 239 for_each_net(net) 240 nf_unregister_net_hook(net, reg); 241 } 242 243 void nf_unregister_hook(struct nf_hook_ops *reg) 244 { 245 rtnl_lock(); 246 _nf_unregister_hook(reg); 247 rtnl_unlock(); 248 } 249 EXPORT_SYMBOL(nf_unregister_hook); 250 251 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n) 252 { 253 unsigned int i; 254 int err = 0; 255 256 for (i = 0; i < n; i++) { 257 err = nf_register_hook(®[i]); 258 if (err) 259 goto err; 260 } 261 return err; 262 263 err: 264 if (i > 0) 265 nf_unregister_hooks(reg, i); 266 return err; 267 } 268 EXPORT_SYMBOL(nf_register_hooks); 269 270 /* Caller MUST take rtnl_lock() */ 271 int _nf_register_hooks(struct nf_hook_ops *reg, unsigned int n) 272 { 273 unsigned int i; 274 int err = 0; 275 276 for (i = 0; i < n; i++) { 277 err = _nf_register_hook(®[i]); 278 if (err) 279 goto err; 280 } 281 return err; 282 283 err: 284 if (i > 0) 285 _nf_unregister_hooks(reg, i); 286 return err; 287 } 288 EXPORT_SYMBOL(_nf_register_hooks); 289 290 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n) 291 { 292 while (n-- > 0) 293 nf_unregister_hook(®[n]); 294 } 295 EXPORT_SYMBOL(nf_unregister_hooks); 296 297 /* Caller MUST take rtnl_lock */ 298 void _nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n) 299 { 300 while (n-- > 0) 301 _nf_unregister_hook(®[n]); 302 } 303 EXPORT_SYMBOL(_nf_unregister_hooks); 304 305 unsigned int nf_iterate(struct sk_buff *skb, 306 struct nf_hook_state *state, 307 struct nf_hook_entry **entryp) 308 { 309 unsigned int verdict; 310 311 while (*entryp) { 312 repeat: 313 verdict = (*entryp)->ops.hook((*entryp)->ops.priv, skb, state); 314 if (verdict != NF_ACCEPT) { 315 if (verdict != NF_REPEAT) 316 return verdict; 317 goto repeat; 318 } 319 *entryp = rcu_dereference((*entryp)->next); 320 } 321 return NF_ACCEPT; 322 } 323 324 325 /* Returns 1 if okfn() needs to be executed by the caller, 326 * -EPERM for NF_DROP, 0 otherwise. Caller must hold rcu_read_lock. */ 327 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state) 328 { 329 struct nf_hook_entry *entry; 330 unsigned int verdict; 331 int ret = 0; 332 333 entry = rcu_dereference(state->hook_entries); 334 next_hook: 335 verdict = nf_iterate(skb, state, &entry); 336 if (verdict == NF_ACCEPT) { 337 ret = 1; 338 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) { 339 kfree_skb(skb); 340 ret = NF_DROP_GETERR(verdict); 341 if (ret == 0) 342 ret = -EPERM; 343 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) { 344 ret = nf_queue(skb, state, &entry, verdict); 345 if (ret == 1 && entry) 346 goto next_hook; 347 } 348 return ret; 349 } 350 EXPORT_SYMBOL(nf_hook_slow); 351 352 353 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len) 354 { 355 if (writable_len > skb->len) 356 return 0; 357 358 /* Not exclusive use of packet? Must copy. */ 359 if (!skb_cloned(skb)) { 360 if (writable_len <= skb_headlen(skb)) 361 return 1; 362 } else if (skb_clone_writable(skb, writable_len)) 363 return 1; 364 365 if (writable_len <= skb_headlen(skb)) 366 writable_len = 0; 367 else 368 writable_len -= skb_headlen(skb); 369 370 return !!__pskb_pull_tail(skb, writable_len); 371 } 372 EXPORT_SYMBOL(skb_make_writable); 373 374 /* This needs to be compiled in any case to avoid dependencies between the 375 * nfnetlink_queue code and nf_conntrack. 376 */ 377 struct nfnl_ct_hook __rcu *nfnl_ct_hook __read_mostly; 378 EXPORT_SYMBOL_GPL(nfnl_ct_hook); 379 380 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 381 /* This does not belong here, but locally generated errors need it if connection 382 tracking in use: without this, connection may not be in hash table, and hence 383 manufactured ICMP or RST packets will not be associated with it. */ 384 void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) 385 __rcu __read_mostly; 386 EXPORT_SYMBOL(ip_ct_attach); 387 388 void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb) 389 { 390 void (*attach)(struct sk_buff *, const struct sk_buff *); 391 392 if (skb->nfct) { 393 rcu_read_lock(); 394 attach = rcu_dereference(ip_ct_attach); 395 if (attach) 396 attach(new, skb); 397 rcu_read_unlock(); 398 } 399 } 400 EXPORT_SYMBOL(nf_ct_attach); 401 402 void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly; 403 EXPORT_SYMBOL(nf_ct_destroy); 404 405 void nf_conntrack_destroy(struct nf_conntrack *nfct) 406 { 407 void (*destroy)(struct nf_conntrack *); 408 409 rcu_read_lock(); 410 destroy = rcu_dereference(nf_ct_destroy); 411 BUG_ON(destroy == NULL); 412 destroy(nfct); 413 rcu_read_unlock(); 414 } 415 EXPORT_SYMBOL(nf_conntrack_destroy); 416 417 /* Built-in default zone used e.g. by modules. */ 418 const struct nf_conntrack_zone nf_ct_zone_dflt = { 419 .id = NF_CT_DEFAULT_ZONE_ID, 420 .dir = NF_CT_DEFAULT_ZONE_DIR, 421 }; 422 EXPORT_SYMBOL_GPL(nf_ct_zone_dflt); 423 #endif /* CONFIG_NF_CONNTRACK */ 424 425 #ifdef CONFIG_NF_NAT_NEEDED 426 void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *); 427 EXPORT_SYMBOL(nf_nat_decode_session_hook); 428 #endif 429 430 static int nf_register_hook_list(struct net *net) 431 { 432 struct nf_hook_ops *elem; 433 int ret; 434 435 rtnl_lock(); 436 list_for_each_entry(elem, &nf_hook_list, list) { 437 ret = nf_register_net_hook(net, elem); 438 if (ret && ret != -ENOENT) 439 goto out_undo; 440 } 441 rtnl_unlock(); 442 return 0; 443 444 out_undo: 445 list_for_each_entry_continue_reverse(elem, &nf_hook_list, list) 446 nf_unregister_net_hook(net, elem); 447 rtnl_unlock(); 448 return ret; 449 } 450 451 static void nf_unregister_hook_list(struct net *net) 452 { 453 struct nf_hook_ops *elem; 454 455 rtnl_lock(); 456 list_for_each_entry(elem, &nf_hook_list, list) 457 nf_unregister_net_hook(net, elem); 458 rtnl_unlock(); 459 } 460 461 static int __net_init netfilter_net_init(struct net *net) 462 { 463 int i, h, ret; 464 465 for (i = 0; i < ARRAY_SIZE(net->nf.hooks); i++) { 466 for (h = 0; h < NF_MAX_HOOKS; h++) 467 RCU_INIT_POINTER(net->nf.hooks[i][h], NULL); 468 } 469 470 #ifdef CONFIG_PROC_FS 471 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter", 472 net->proc_net); 473 if (!net->nf.proc_netfilter) { 474 if (!net_eq(net, &init_net)) 475 pr_err("cannot create netfilter proc entry"); 476 477 return -ENOMEM; 478 } 479 #endif 480 ret = nf_register_hook_list(net); 481 if (ret) 482 remove_proc_entry("netfilter", net->proc_net); 483 484 return ret; 485 } 486 487 static void __net_exit netfilter_net_exit(struct net *net) 488 { 489 nf_unregister_hook_list(net); 490 remove_proc_entry("netfilter", net->proc_net); 491 } 492 493 static struct pernet_operations netfilter_net_ops = { 494 .init = netfilter_net_init, 495 .exit = netfilter_net_exit, 496 }; 497 498 int __init netfilter_init(void) 499 { 500 int ret; 501 502 ret = register_pernet_subsys(&netfilter_net_ops); 503 if (ret < 0) 504 goto err; 505 506 ret = netfilter_log_init(); 507 if (ret < 0) 508 goto err_pernet; 509 510 return 0; 511 err_pernet: 512 unregister_pernet_subsys(&netfilter_net_ops); 513 err: 514 return ret; 515 } 516