1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * This is a module which is used for queueing packets and communicating with 4 * userspace via nfnetlink. 5 * 6 * (C) 2005 by Harald Welte <laforge@netfilter.org> 7 * (C) 2007 by Patrick McHardy <kaber@trash.net> 8 * 9 * Based on the old ipv4-only ip_queue.c: 10 * (C) 2000-2002 James Morris <jmorris@intercode.com.au> 11 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org> 12 */ 13 14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15 16 #include <linux/module.h> 17 #include <linux/skbuff.h> 18 #include <linux/init.h> 19 #include <linux/spinlock.h> 20 #include <linux/slab.h> 21 #include <linux/notifier.h> 22 #include <linux/netdevice.h> 23 #include <linux/netfilter.h> 24 #include <linux/proc_fs.h> 25 #include <linux/netfilter_ipv4.h> 26 #include <linux/netfilter_ipv6.h> 27 #include <linux/netfilter_bridge.h> 28 #include <linux/netfilter/nfnetlink.h> 29 #include <linux/netfilter/nfnetlink_queue.h> 30 #include <linux/netfilter/nf_conntrack_common.h> 31 #include <linux/icmp.h> 32 #include <linux/icmpv6.h> 33 #include <linux/ip.h> 34 #include <linux/list.h> 35 #include <linux/sctp.h> 36 #include <linux/cgroup-defs.h> 37 #include <linux/rhashtable.h> 38 #include <linux/jhash.h> 39 #include <linux/tcp.h> 40 #include <linux/udp.h> 41 #include <net/gre.h> 42 #include <net/gso.h> 43 #include <net/ip.h> 44 #include <net/sock.h> 45 #include <net/tcp_states.h> 46 #include <net/netfilter/nf_queue.h> 47 #include <net/net_debug.h> 48 #include <net/netns/generic.h> 49 50 #include <linux/atomic.h> 51 52 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 53 #include "../bridge/br_private.h" 54 #endif 55 56 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 57 #include <net/netfilter/nf_conntrack.h> 58 #endif 59 60 #define NFQNL_QMAX_DEFAULT 1024 61 #define NFQNL_HASH_MIN 8 62 #define NFQNL_HASH_MAX 32768 63 64 /* We're using struct nlattr which has 16bit nla_len. Note that nla_len 65 * includes the header length. Thus, the maximum packet length that we 66 * support is 65531 bytes. We send truncated packets if the specified length 67 * is larger than that. Userspace can check for presence of NFQA_CAP_LEN 68 * attribute to detect truncation. 69 */ 70 #define NFQNL_MAX_COPY_RANGE (0xffff - NLA_HDRLEN) 71 72 struct nfqnl_instance { 73 struct hlist_node hlist; /* global list of queues */ 74 struct rhashtable nfqnl_packet_map; 75 struct rcu_work rwork; 76 77 u32 peer_portid; 78 unsigned int queue_maxlen; 79 unsigned int copy_range; 80 unsigned int queue_dropped; 81 unsigned int queue_user_dropped; 82 83 84 u_int16_t queue_num; /* number of this queue */ 85 u_int8_t copy_mode; 86 u_int32_t flags; /* Set using NFQA_CFG_FLAGS */ 87 /* 88 * Following fields are dirtied for each queued packet, 89 * keep them in same cache line if possible. 90 */ 91 spinlock_t lock ____cacheline_aligned_in_smp; 92 unsigned int queue_total; 93 unsigned int id_sequence; /* 'sequence' of pkt ids */ 94 struct list_head queue_list; /* packets in queue */ 95 }; 96 97 typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long); 98 99 static struct workqueue_struct *nfq_cleanup_wq __read_mostly; 100 static unsigned int nfnl_queue_net_id __read_mostly; 101 102 #define INSTANCE_BUCKETS 16 103 struct nfnl_queue_net { 104 spinlock_t instances_lock; 105 struct hlist_head instance_table[INSTANCE_BUCKETS]; 106 }; 107 108 static struct nfnl_queue_net *nfnl_queue_pernet(struct net *net) 109 { 110 return net_generic(net, nfnl_queue_net_id); 111 } 112 113 static inline u_int8_t instance_hashfn(u_int16_t queue_num) 114 { 115 return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS; 116 } 117 118 static const struct rhashtable_params nfqnl_rhashtable_params = { 119 .head_offset = offsetof(struct nf_queue_entry, hash_node), 120 .key_offset = offsetof(struct nf_queue_entry, id), 121 .key_len = sizeof(u32), 122 .automatic_shrinking = true, 123 .min_size = NFQNL_HASH_MIN, 124 .max_size = NFQNL_HASH_MAX, 125 }; 126 127 static struct nfqnl_instance * 128 instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num) 129 { 130 struct hlist_head *head; 131 struct nfqnl_instance *inst; 132 133 head = &q->instance_table[instance_hashfn(queue_num)]; 134 hlist_for_each_entry_rcu(inst, head, hlist) { 135 if (inst->queue_num == queue_num) 136 return inst; 137 } 138 return NULL; 139 } 140 141 static struct nfqnl_instance * 142 instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 portid) 143 { 144 struct nfqnl_instance *inst; 145 unsigned int h; 146 int err; 147 148 inst = kzalloc_obj(*inst, GFP_KERNEL_ACCOUNT); 149 if (!inst) 150 return ERR_PTR(-ENOMEM); 151 152 inst->queue_num = queue_num; 153 inst->peer_portid = portid; 154 inst->queue_maxlen = NFQNL_QMAX_DEFAULT; 155 inst->copy_range = NFQNL_MAX_COPY_RANGE; 156 inst->copy_mode = NFQNL_COPY_NONE; 157 spin_lock_init(&inst->lock); 158 INIT_LIST_HEAD(&inst->queue_list); 159 160 err = rhashtable_init(&inst->nfqnl_packet_map, &nfqnl_rhashtable_params); 161 if (err < 0) 162 goto out_free; 163 164 spin_lock(&q->instances_lock); 165 if (instance_lookup(q, queue_num)) { 166 err = -EEXIST; 167 goto out_unlock; 168 } 169 170 if (!try_module_get(THIS_MODULE)) { 171 err = -EAGAIN; 172 goto out_unlock; 173 } 174 175 h = instance_hashfn(queue_num); 176 hlist_add_head_rcu(&inst->hlist, &q->instance_table[h]); 177 178 spin_unlock(&q->instances_lock); 179 180 return inst; 181 182 out_unlock: 183 spin_unlock(&q->instances_lock); 184 rhashtable_destroy(&inst->nfqnl_packet_map); 185 out_free: 186 kfree(inst); 187 return ERR_PTR(err); 188 } 189 190 static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, 191 unsigned long data); 192 193 static void instance_destroy_work(struct work_struct *work) 194 { 195 struct nfqnl_instance *inst; 196 197 inst = container_of(to_rcu_work(work), struct nfqnl_instance, 198 rwork); 199 rcu_read_lock(); 200 nfqnl_flush(inst, NULL, 0); 201 rcu_read_unlock(); 202 203 rhashtable_destroy(&inst->nfqnl_packet_map); 204 205 kfree(inst); 206 module_put(THIS_MODULE); 207 } 208 209 static void 210 __instance_destroy(struct nfqnl_instance *inst) 211 { 212 hlist_del_rcu(&inst->hlist); 213 214 INIT_RCU_WORK(&inst->rwork, instance_destroy_work); 215 queue_rcu_work(nfq_cleanup_wq, &inst->rwork); 216 } 217 218 static void 219 instance_destroy(struct nfnl_queue_net *q, struct nfqnl_instance *inst) 220 { 221 spin_lock(&q->instances_lock); 222 __instance_destroy(inst); 223 spin_unlock(&q->instances_lock); 224 } 225 226 static int 227 __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry) 228 { 229 int err; 230 231 err = rhashtable_insert_fast(&queue->nfqnl_packet_map, &entry->hash_node, 232 nfqnl_rhashtable_params); 233 if (unlikely(err)) 234 return err; 235 236 list_add_tail(&entry->list, &queue->queue_list); 237 queue->queue_total++; 238 239 return 0; 240 } 241 242 static void 243 __dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry) 244 { 245 rhashtable_remove_fast(&queue->nfqnl_packet_map, &entry->hash_node, 246 nfqnl_rhashtable_params); 247 list_del(&entry->list); 248 queue->queue_total--; 249 } 250 251 static struct nf_queue_entry * 252 find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id) 253 { 254 struct nf_queue_entry *entry; 255 256 spin_lock_bh(&queue->lock); 257 entry = rhashtable_lookup_fast(&queue->nfqnl_packet_map, &id, 258 nfqnl_rhashtable_params); 259 260 if (entry) 261 __dequeue_entry(queue, entry); 262 263 spin_unlock_bh(&queue->lock); 264 265 return entry; 266 } 267 268 static unsigned int nf_iterate(struct sk_buff *skb, 269 struct nf_hook_state *state, 270 const struct nf_hook_entries *hooks, 271 unsigned int *index) 272 { 273 const struct nf_hook_entry *hook; 274 unsigned int verdict, i = *index; 275 276 while (i < hooks->num_hook_entries) { 277 hook = &hooks->hooks[i]; 278 repeat: 279 verdict = nf_hook_entry_hookfn(hook, skb, state); 280 if (verdict != NF_ACCEPT) { 281 *index = i; 282 if (verdict != NF_REPEAT) 283 return verdict; 284 goto repeat; 285 } 286 i++; 287 } 288 289 *index = i; 290 return NF_ACCEPT; 291 } 292 293 static struct nf_hook_entries *nf_hook_entries_head(const struct net *net, u8 pf, u8 hooknum) 294 { 295 switch (pf) { 296 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE 297 case NFPROTO_BRIDGE: 298 return rcu_dereference(net->nf.hooks_bridge[hooknum]); 299 #endif 300 case NFPROTO_IPV4: 301 return rcu_dereference(net->nf.hooks_ipv4[hooknum]); 302 case NFPROTO_IPV6: 303 return rcu_dereference(net->nf.hooks_ipv6[hooknum]); 304 default: 305 WARN_ON_ONCE(1); 306 return NULL; 307 } 308 309 return NULL; 310 } 311 312 static int nf_ip_reroute(struct sk_buff *skb, const struct nf_queue_entry *entry) 313 { 314 #ifdef CONFIG_INET 315 const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry); 316 317 if (entry->state.hook == NF_INET_LOCAL_OUT) { 318 const struct iphdr *iph = ip_hdr(skb); 319 320 if (!(iph->tos == rt_info->tos && 321 skb->mark == rt_info->mark && 322 iph->daddr == rt_info->daddr && 323 iph->saddr == rt_info->saddr)) 324 return ip_route_me_harder(entry->state.net, entry->state.sk, 325 skb, RTN_UNSPEC); 326 } 327 #endif 328 return 0; 329 } 330 331 static int nf_ip6_reroute(struct sk_buff *skb, 332 const struct nf_queue_entry *entry) 333 { 334 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry); 335 336 if (entry->state.hook == NF_INET_LOCAL_OUT) { 337 const struct ipv6hdr *iph = ipv6_hdr(skb); 338 339 if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) || 340 !ipv6_addr_equal(&iph->saddr, &rt_info->saddr) || 341 skb->mark != rt_info->mark) 342 return nf_ip6_route_me_harder(entry->state.net, 343 entry->state.sk, skb); 344 } 345 return 0; 346 } 347 348 static int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry) 349 { 350 int ret = 0; 351 352 switch (entry->state.pf) { 353 case AF_INET: 354 ret = nf_ip_reroute(skb, entry); 355 break; 356 case AF_INET6: 357 ret = nf_ip6_reroute(skb, entry); 358 break; 359 } 360 return ret; 361 } 362 363 /* caller must hold rcu read-side lock */ 364 static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) 365 { 366 const struct nf_hook_entry *hook_entry; 367 const struct nf_hook_entries *hooks; 368 struct sk_buff *skb = entry->skb; 369 bool old_in_nf_duplicate; 370 const struct net *net; 371 unsigned int i; 372 int err; 373 u8 pf; 374 375 DEBUG_NET_WARN_ONCE(current->in_nf_duplicate && 376 !entry->in_nf_duplicate, 377 "%s called with unexpected duplication guard", 378 __func__); 379 local_bh_disable(); 380 old_in_nf_duplicate = current->in_nf_duplicate; 381 382 net = entry->state.net; 383 pf = entry->state.pf; 384 385 current->in_nf_duplicate = entry->in_nf_duplicate; 386 hooks = nf_hook_entries_head(net, pf, entry->state.hook); 387 388 i = entry->hook_index; 389 if (!hooks || i >= hooks->num_hook_entries) { 390 kfree_skb_reason(skb, SKB_DROP_REASON_NETFILTER_DROP); 391 nf_queue_entry_free(entry); 392 goto out; 393 } 394 395 hook_entry = &hooks->hooks[i]; 396 397 /* Continue traversal iff userspace said ok... */ 398 if (verdict == NF_REPEAT) 399 verdict = nf_hook_entry_hookfn(hook_entry, skb, &entry->state); 400 401 if (verdict == NF_ACCEPT) { 402 if (nf_reroute(skb, entry) < 0) 403 verdict = NF_DROP; 404 } 405 406 if (verdict == NF_ACCEPT) { 407 next_hook: 408 ++i; 409 verdict = nf_iterate(skb, &entry->state, hooks, &i); 410 } 411 412 switch (verdict & NF_VERDICT_MASK) { 413 case NF_ACCEPT: 414 case NF_STOP: 415 entry->state.okfn(entry->state.net, entry->state.sk, skb); 416 break; 417 case NF_QUEUE: 418 err = nf_queue(skb, &entry->state, i, verdict); 419 if (err == 1) 420 goto next_hook; 421 break; 422 case NF_STOLEN: 423 break; 424 default: 425 kfree_skb(skb); 426 } 427 428 nf_queue_entry_free(entry); 429 out: 430 current->in_nf_duplicate = old_in_nf_duplicate; 431 local_bh_enable(); 432 } 433 434 /* return true if the entry has an unconfirmed conntrack attached that isn't owned by us 435 * exclusively. 436 */ 437 static bool nf_ct_drop_unconfirmed(const struct nf_queue_entry *entry, bool *is_unconfirmed) 438 { 439 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 440 struct nf_conn *ct = (void *)skb_nfct(entry->skb); 441 442 if (!ct || nf_ct_is_confirmed(ct)) 443 return false; 444 445 if (is_unconfirmed) 446 *is_unconfirmed = true; 447 448 /* in some cases skb_clone() can occur after initial conntrack 449 * pickup, but conntrack assumes exclusive skb->_nfct ownership for 450 * unconfirmed entries. 451 * 452 * This happens for br_netfilter and with ip multicast routing. 453 * This can't be solved with serialization here because one clone 454 * could have been queued for local delivery or could be transmitted 455 * in parallel on another CPU. 456 */ 457 return refcount_read(&ct->ct_general.use) > 1; 458 #endif 459 return false; 460 } 461 462 static bool nf_bridge_port_valid(const struct net_device *dev) 463 { 464 if (!dev) 465 return true; 466 467 return netif_is_bridge_port(dev); 468 } 469 470 /* queued skbs leave rcu protection. We bump device refcount so that 471 * the device cannot go away. However, while packet was out the port 472 * could have been removed from the bridge. 473 * 474 * Ensure in+outdev are still part of a bridge at reinject time. 475 * 476 * The device rx_handler_data could even be pointing at data that is 477 * not a net_bridge_port structure. 478 */ 479 static bool nf_bridge_ports_valid(const struct nf_queue_entry *entry) 480 { 481 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 482 if (!nf_bridge_port_valid(entry->physin) || 483 !nf_bridge_port_valid(entry->physout)) 484 return false; 485 #endif 486 if (entry->state.pf != PF_BRIDGE) 487 return true; 488 489 if (!nf_bridge_port_valid(entry->state.in) || 490 !nf_bridge_port_valid(entry->state.out)) 491 return false; 492 493 return true; 494 } 495 496 static void nfqnl_reinject(struct nf_queue_entry *entry, unsigned int verdict) 497 { 498 const struct nf_ct_hook *ct_hook; 499 500 if (!nf_bridge_ports_valid(entry)) 501 verdict = NF_DROP; 502 503 if (verdict == NF_ACCEPT || 504 verdict == NF_REPEAT || 505 verdict == NF_STOP) { 506 unsigned int ct_verdict = verdict; 507 508 rcu_read_lock(); 509 ct_hook = rcu_dereference(nf_ct_hook); 510 if (ct_hook) 511 ct_verdict = ct_hook->update(entry->state.net, entry->skb); 512 rcu_read_unlock(); 513 514 switch (ct_verdict & NF_VERDICT_MASK) { 515 case NF_ACCEPT: 516 /* follow userspace verdict, could be REPEAT */ 517 break; 518 case NF_STOLEN: 519 nf_queue_entry_free(entry); 520 return; 521 default: 522 verdict = ct_verdict & NF_VERDICT_MASK; 523 break; 524 } 525 } 526 527 if (verdict != NF_DROP && entry->nf_ct_is_unconfirmed) { 528 /* If first queued segment was already reinjected then 529 * there is a good chance the ct entry is now confirmed. 530 * 531 * Handle the rare cases: 532 * - out-of-order verdict 533 * - threaded userspace reinjecting in parallel 534 * - first segment was dropped 535 * 536 * In all of those cases we can't handle this packet 537 * because we can't be sure that another CPU won't modify 538 * nf_conn->ext in parallel which isn't allowed. 539 */ 540 if (nf_ct_drop_unconfirmed(entry, NULL)) 541 verdict = NF_DROP; 542 } 543 544 nf_reinject(entry, verdict); 545 } 546 547 static void 548 nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data) 549 { 550 struct nf_queue_entry *entry, *next; 551 552 spin_lock_bh(&queue->lock); 553 list_for_each_entry_safe(entry, next, &queue->queue_list, list) { 554 if (!cmpfn || cmpfn(entry, data)) { 555 __dequeue_entry(queue, entry); 556 nfqnl_reinject(entry, NF_DROP); 557 } 558 } 559 spin_unlock_bh(&queue->lock); 560 } 561 562 static int 563 nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet, 564 bool csum_verify) 565 { 566 __u32 flags = 0; 567 568 if (packet->ip_summed == CHECKSUM_PARTIAL) 569 flags = NFQA_SKB_CSUMNOTREADY; 570 else if (csum_verify) 571 flags = NFQA_SKB_CSUM_NOTVERIFIED; 572 573 if (skb_is_gso(packet)) 574 flags |= NFQA_SKB_GSO; 575 576 return flags ? nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags)) : 0; 577 } 578 579 static int nfqnl_put_sk_uidgid(struct sk_buff *skb, struct sock *sk) 580 { 581 const struct socket *sock; 582 const struct file *file; 583 const struct cred *cred; 584 585 if (!sk_fullsock(sk)) 586 return 0; 587 588 /* The sk pointer remains valid as long as the skb is. 589 * The sk_socket and file pointer may become NULL 590 * if the socket is closed. 591 * Both structures (including file->cred) are RCU freed 592 * which means they can be accessed within a RCU read section. 593 */ 594 sock = READ_ONCE(sk->sk_socket); 595 file = sock ? READ_ONCE(sock->file) : NULL; 596 if (file) { 597 cred = file->f_cred; 598 if (nla_put_be32(skb, NFQA_UID, 599 htonl(from_kuid_munged(&init_user_ns, cred->fsuid)))) 600 goto nla_put_failure; 601 if (nla_put_be32(skb, NFQA_GID, 602 htonl(from_kgid_munged(&init_user_ns, cred->fsgid)))) 603 goto nla_put_failure; 604 } 605 return 0; 606 607 nla_put_failure: 608 return -1; 609 } 610 611 static int nfqnl_put_sk_classid(struct sk_buff *skb, struct sock *sk) 612 { 613 #if IS_ENABLED(CONFIG_CGROUP_NET_CLASSID) 614 if (sk && sk_fullsock(sk)) { 615 u32 classid = sock_cgroup_classid(&sk->sk_cgrp_data); 616 617 if (classid && nla_put_be32(skb, NFQA_CGROUP_CLASSID, htonl(classid))) 618 return -1; 619 } 620 #endif 621 return 0; 622 } 623 624 static int nfqnl_get_sk_secctx(struct sk_buff *skb, struct lsm_context *ctx) 625 { 626 int seclen = 0; 627 #if IS_ENABLED(CONFIG_NETWORK_SECMARK) 628 if (skb->secmark) 629 seclen = security_secid_to_secctx(skb->secmark, ctx); 630 #endif 631 return seclen; 632 } 633 634 static u32 nfqnl_get_bridge_size(struct nf_queue_entry *entry) 635 { 636 struct sk_buff *entskb = entry->skb; 637 u32 nlalen = 0; 638 u32 mac_len; 639 640 if (entry->state.pf != PF_BRIDGE || !skb_mac_header_was_set(entskb)) 641 return 0; 642 643 if (skb_vlan_tag_present(entskb)) 644 nlalen += nla_total_size(nla_total_size(sizeof(__be16)) + 645 nla_total_size(sizeof(__be16))); 646 647 mac_len = skb_mac_header_len(entskb); 648 if (mac_len > 0) 649 nlalen += nla_total_size(mac_len); 650 651 return nlalen; 652 } 653 654 static int nfqnl_put_bridge(struct nf_queue_entry *entry, struct sk_buff *skb) 655 { 656 struct sk_buff *entskb = entry->skb; 657 u32 mac_len; 658 659 if (entry->state.pf != PF_BRIDGE || !skb_mac_header_was_set(entskb)) 660 return 0; 661 662 if (skb_vlan_tag_present(entskb)) { 663 struct nlattr *nest; 664 665 nest = nla_nest_start(skb, NFQA_VLAN); 666 if (!nest) 667 goto nla_put_failure; 668 669 if (nla_put_be16(skb, NFQA_VLAN_TCI, htons(entskb->vlan_tci)) || 670 nla_put_be16(skb, NFQA_VLAN_PROTO, entskb->vlan_proto)) 671 goto nla_put_failure; 672 673 nla_nest_end(skb, nest); 674 } 675 676 mac_len = skb_mac_header_len(entskb); 677 if (mac_len > 0 && 678 nla_put(skb, NFQA_L2HDR, mac_len, skb_mac_header(entskb))) 679 goto nla_put_failure; 680 681 return 0; 682 683 nla_put_failure: 684 return -1; 685 } 686 687 static int nf_queue_checksum_help(struct sk_buff *entskb) 688 { 689 if (entskb->protocol == htons(ETH_P_IP)) { 690 if (!pskb_network_may_pull(entskb, sizeof(struct iphdr)) || 691 ip_check_csum_start(entskb)) 692 return -EINVAL; 693 } 694 695 if (skb_csum_is_sctp(entskb)) 696 return skb_crc32c_csum_help(entskb); 697 698 return skb_checksum_help(entskb); 699 } 700 701 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 702 static int nfqnl_put_master_ifindex(struct sk_buff *nlskb, int attr, 703 const struct net_device *dev) 704 { 705 const struct net_device *upper; 706 707 if (dev && !netif_is_bridge_port(dev)) 708 return 0; 709 710 upper = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 711 if (upper && nla_put_be32(nlskb, attr, htonl(upper->ifindex))) 712 return -EMSGSIZE; 713 714 return 0; 715 } 716 #endif 717 718 static unsigned int nfqnl_get_data_len(const struct sk_buff *entskb, 719 unsigned int copy_range) 720 { 721 unsigned int data_len = entskb->len; 722 723 if (!skb_frags_readable(entskb)) 724 data_len = skb_headlen(entskb); 725 726 return min(data_len, copy_range); 727 } 728 729 static struct sk_buff * 730 nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue, 731 struct nf_queue_entry *entry, 732 __be32 **packet_id_ptr) 733 { 734 size_t size; 735 size_t data_len = 0, cap_len = 0; 736 unsigned int hlen = 0; 737 struct sk_buff *skb; 738 struct nlattr *nla; 739 struct nfqnl_msg_packet_hdr *pmsg; 740 struct nlmsghdr *nlh; 741 struct sk_buff *entskb = entry->skb; 742 struct net_device *indev; 743 struct net_device *outdev; 744 struct nf_conn *ct = NULL; 745 enum ip_conntrack_info ctinfo = 0; 746 const struct nfnl_ct_hook *nfnl_ct; 747 bool csum_verify; 748 struct lsm_context ctx = { NULL, 0, 0 }; 749 int seclen = 0; 750 ktime_t tstamp; 751 752 size = nlmsg_total_size(sizeof(struct nfgenmsg)) 753 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr)) 754 + nla_total_size(sizeof(u_int32_t)) /* ifindex */ 755 + nla_total_size(sizeof(u_int32_t)) /* ifindex */ 756 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 757 + nla_total_size(sizeof(u_int32_t)) /* ifindex */ 758 + nla_total_size(sizeof(u_int32_t)) /* ifindex */ 759 #endif 760 + nla_total_size(sizeof(u_int32_t)) /* mark */ 761 + nla_total_size(sizeof(u_int32_t)) /* priority */ 762 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw)) 763 + nla_total_size(sizeof(u_int32_t)) /* skbinfo */ 764 #if IS_ENABLED(CONFIG_CGROUP_NET_CLASSID) 765 + nla_total_size(sizeof(u_int32_t)) /* classid */ 766 #endif 767 + nla_total_size(sizeof(u_int32_t)); /* cap_len */ 768 769 tstamp = skb_tstamp_cond(entskb, false); 770 if (tstamp) 771 size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp)); 772 773 size += nfqnl_get_bridge_size(entry); 774 775 if (entry->state.hook <= NF_INET_FORWARD || 776 (entry->state.hook == NF_INET_POST_ROUTING && entskb->sk == NULL)) 777 csum_verify = !skb_csum_unnecessary(entskb); 778 else 779 csum_verify = false; 780 781 outdev = entry->state.out; 782 783 switch ((enum nfqnl_config_mode)READ_ONCE(queue->copy_mode)) { 784 case NFQNL_COPY_META: 785 case NFQNL_COPY_NONE: 786 break; 787 788 case NFQNL_COPY_PACKET: 789 if (!(queue->flags & NFQA_CFG_F_GSO) && 790 entskb->ip_summed == CHECKSUM_PARTIAL && 791 nf_queue_checksum_help(entskb)) 792 return NULL; 793 794 data_len = nfqnl_get_data_len(entskb, READ_ONCE(queue->copy_range)); 795 hlen = skb_zerocopy_headlen(entskb); 796 hlen = min_t(unsigned int, hlen, data_len); 797 size += sizeof(struct nlattr) + hlen; 798 cap_len = entskb->len; 799 break; 800 } 801 802 nfnl_ct = rcu_dereference(nfnl_ct_hook); 803 804 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 805 if (queue->flags & NFQA_CFG_F_CONNTRACK) { 806 if (nfnl_ct != NULL) { 807 ct = nf_ct_get(entskb, &ctinfo); 808 if (ct != NULL) 809 size += nfnl_ct->build_size(ct); 810 } 811 } 812 #endif 813 814 if (queue->flags & NFQA_CFG_F_UID_GID) { 815 size += (nla_total_size(sizeof(u_int32_t)) /* uid */ 816 + nla_total_size(sizeof(u_int32_t))); /* gid */ 817 } 818 819 if ((queue->flags & NFQA_CFG_F_SECCTX) && entskb->sk) { 820 seclen = nfqnl_get_sk_secctx(entskb, &ctx); 821 if (seclen < 0) 822 return NULL; 823 if (seclen) 824 size += nla_total_size(seclen); 825 } 826 827 skb = alloc_skb(size, GFP_ATOMIC); 828 if (!skb) { 829 skb_tx_error(entskb); 830 goto nlmsg_failure; 831 } 832 833 nlh = nfnl_msg_put(skb, 0, 0, 834 nfnl_msg_type(NFNL_SUBSYS_QUEUE, NFQNL_MSG_PACKET), 835 0, entry->state.pf, NFNETLINK_V0, 836 htons(queue->queue_num)); 837 if (!nlh) { 838 skb_tx_error(entskb); 839 kfree_skb(skb); 840 goto nlmsg_failure; 841 } 842 843 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg)); 844 pmsg = nla_data(nla); 845 pmsg->hw_protocol = entskb->protocol; 846 pmsg->hook = entry->state.hook; 847 *packet_id_ptr = &pmsg->packet_id; 848 849 indev = entry->state.in; 850 if (indev) { 851 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 852 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex))) 853 goto nla_put_failure; 854 #else 855 if (entry->state.pf == PF_BRIDGE) { 856 /* Case 1: indev is physical input device, we need to 857 * look for bridge group (when called from 858 * netfilter_bridge) */ 859 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV, 860 htonl(indev->ifindex)) || 861 nfqnl_put_master_ifindex(skb, NFQA_IFINDEX_INDEV, indev)) 862 goto nla_put_failure; 863 } else { 864 int physinif; 865 866 /* Case 2: indev is bridge group, we need to look for 867 * physical device (when called from ipv4) */ 868 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, 869 htonl(indev->ifindex))) 870 goto nla_put_failure; 871 872 physinif = nf_bridge_get_physinif(entskb); 873 if (physinif && 874 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV, 875 htonl(physinif))) 876 goto nla_put_failure; 877 } 878 #endif 879 } 880 881 if (outdev) { 882 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 883 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex))) 884 goto nla_put_failure; 885 #else 886 if (entry->state.pf == PF_BRIDGE) { 887 /* Case 1: outdev is physical output device, we need to 888 * look for bridge group (when called from 889 * netfilter_bridge) */ 890 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV, 891 htonl(outdev->ifindex)) || 892 nfqnl_put_master_ifindex(skb, NFQA_IFINDEX_OUTDEV, outdev)) 893 goto nla_put_failure; 894 } else { 895 int physoutif; 896 897 /* Case 2: outdev is bridge group, we need to look for 898 * physical output device (when called from ipv4) */ 899 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, 900 htonl(outdev->ifindex))) 901 goto nla_put_failure; 902 903 physoutif = nf_bridge_get_physoutif(entskb); 904 if (physoutif && 905 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV, 906 htonl(physoutif))) 907 goto nla_put_failure; 908 } 909 #endif 910 } 911 912 if (entskb->mark && 913 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark))) 914 goto nla_put_failure; 915 916 if (entskb->priority && 917 nla_put_be32(skb, NFQA_PRIORITY, htonl(entskb->priority))) 918 goto nla_put_failure; 919 920 if (indev && entskb->dev && 921 skb_mac_header_was_set(entskb) && 922 skb_mac_header_len(entskb) != 0) { 923 struct nfqnl_msg_packet_hw phw; 924 int len; 925 926 memset(&phw, 0, sizeof(phw)); 927 len = dev_parse_header(entskb, phw.hw_addr); 928 if (len) { 929 phw.hw_addrlen = htons(len); 930 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw)) 931 goto nla_put_failure; 932 } 933 } 934 935 if (nfqnl_put_bridge(entry, skb) < 0) 936 goto nla_put_failure; 937 938 if (entry->state.hook <= NF_INET_FORWARD && tstamp) { 939 struct nfqnl_msg_packet_timestamp ts; 940 struct timespec64 kts = ktime_to_timespec64(tstamp); 941 942 ts.sec = cpu_to_be64(kts.tv_sec); 943 ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC); 944 945 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts)) 946 goto nla_put_failure; 947 } 948 949 if ((queue->flags & NFQA_CFG_F_UID_GID) && entskb->sk && 950 nfqnl_put_sk_uidgid(skb, entskb->sk) < 0) 951 goto nla_put_failure; 952 953 if (nfqnl_put_sk_classid(skb, entskb->sk) < 0) 954 goto nla_put_failure; 955 956 if (seclen > 0 && nla_put(skb, NFQA_SECCTX, ctx.len, ctx.context)) 957 goto nla_put_failure; 958 959 if (ct && nfnl_ct->build(skb, ct, ctinfo, NFQA_CT, NFQA_CT_INFO) < 0) 960 goto nla_put_failure; 961 962 if (cap_len > data_len && 963 nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len))) 964 goto nla_put_failure; 965 966 if (nfqnl_put_packet_info(skb, entskb, csum_verify)) 967 goto nla_put_failure; 968 969 if (data_len) { 970 struct nlattr *nla; 971 972 if (skb_tailroom(skb) < sizeof(*nla) + hlen) 973 goto nla_put_failure; 974 975 nla = skb_put(skb, sizeof(*nla)); 976 nla->nla_type = NFQA_PAYLOAD; 977 nla->nla_len = nla_attr_size(data_len); 978 979 if (skb_zerocopy(skb, entskb, data_len, hlen)) 980 goto nla_put_failure; 981 } 982 983 nlh->nlmsg_len = skb->len; 984 if (seclen >= 0) 985 security_release_secctx(&ctx); 986 return skb; 987 988 nla_put_failure: 989 skb_tx_error(entskb); 990 kfree_skb(skb); 991 net_err_ratelimited("nf_queue: error creating packet message\n"); 992 nlmsg_failure: 993 if (seclen >= 0) 994 security_release_secctx(&ctx); 995 return NULL; 996 } 997 998 static int 999 __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue, 1000 struct nf_queue_entry *entry) 1001 { 1002 struct sk_buff *nskb; 1003 int err = -ENOBUFS; 1004 __be32 *packet_id_ptr; 1005 int failopen = 0; 1006 1007 nskb = nfqnl_build_packet_message(net, queue, entry, &packet_id_ptr); 1008 if (nskb == NULL) { 1009 err = -ENOMEM; 1010 goto err_out; 1011 } 1012 spin_lock_bh(&queue->lock); 1013 1014 if (queue->queue_total >= queue->queue_maxlen) 1015 goto err_out_queue_drop; 1016 1017 entry->id = ++queue->id_sequence; 1018 *packet_id_ptr = htonl(entry->id); 1019 1020 /* Insert into hash BEFORE unicast. If failure don't send to userspace. */ 1021 err = __enqueue_entry(queue, entry); 1022 if (unlikely(err)) 1023 goto err_out_queue_drop; 1024 1025 /* nfnetlink_unicast will either free the nskb or add it to a socket */ 1026 err = nfnetlink_unicast(nskb, net, queue->peer_portid); 1027 if (err < 0) { 1028 /* Unicast failed - remove entry we just inserted */ 1029 __dequeue_entry(queue, entry); 1030 1031 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) { 1032 failopen = 1; 1033 err = 0; 1034 } else { 1035 queue->queue_user_dropped++; 1036 } 1037 goto err_out_unlock; 1038 } 1039 1040 spin_unlock_bh(&queue->lock); 1041 return 0; 1042 1043 err_out_queue_drop: 1044 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) { 1045 failopen = 1; 1046 err = 0; 1047 } else { 1048 queue->queue_dropped++; 1049 1050 if (queue->queue_total >= queue->queue_maxlen) 1051 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n", 1052 queue->queue_total); 1053 else 1054 net_warn_ratelimited("nf_queue: hash insert failed: %d\n", err); 1055 } 1056 kfree_skb(nskb); 1057 err_out_unlock: 1058 spin_unlock_bh(&queue->lock); 1059 if (failopen) 1060 nfqnl_reinject(entry, NF_ACCEPT); 1061 err_out: 1062 return err; 1063 } 1064 1065 static struct nf_queue_entry * 1066 nf_queue_entry_dup(struct nf_queue_entry *e) 1067 { 1068 struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC); 1069 1070 if (!entry) 1071 return NULL; 1072 1073 if (nf_queue_entry_get_refs(entry)) 1074 return entry; 1075 1076 kfree(entry); 1077 return NULL; 1078 } 1079 1080 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 1081 /* When called from bridge netfilter, skb->data must point to MAC header 1082 * before calling skb_gso_segment(). Else, original MAC header is lost 1083 * and segmented skbs will be sent to wrong destination. 1084 */ 1085 static void nf_bridge_adjust_skb_data(struct sk_buff *skb) 1086 { 1087 if (nf_bridge_info_get(skb)) 1088 __skb_push(skb, skb_mac_header_len(skb)); 1089 } 1090 1091 static void nf_bridge_adjust_segmented_data(struct sk_buff *skb) 1092 { 1093 if (nf_bridge_info_get(skb)) 1094 __skb_pull(skb, skb_mac_header_len(skb)); 1095 } 1096 #else 1097 #define nf_bridge_adjust_skb_data(s) do {} while (0) 1098 #define nf_bridge_adjust_segmented_data(s) do {} while (0) 1099 #endif 1100 1101 static int 1102 __nfqnl_enqueue_packet_gso(struct net *net, struct nfqnl_instance *queue, 1103 struct sk_buff *skb, struct nf_queue_entry *entry) 1104 { 1105 int ret = -ENOMEM; 1106 struct nf_queue_entry *entry_seg; 1107 1108 nf_bridge_adjust_segmented_data(skb); 1109 1110 if (skb->next == NULL) { /* last packet, no need to copy entry */ 1111 struct sk_buff *gso_skb = entry->skb; 1112 entry->skb = skb; 1113 ret = __nfqnl_enqueue_packet(net, queue, entry); 1114 if (ret) 1115 entry->skb = gso_skb; 1116 return ret; 1117 } 1118 1119 skb_mark_not_on_list(skb); 1120 1121 entry_seg = nf_queue_entry_dup(entry); 1122 if (entry_seg) { 1123 entry_seg->skb = skb; 1124 ret = __nfqnl_enqueue_packet(net, queue, entry_seg); 1125 if (ret) 1126 nf_queue_entry_free(entry_seg); 1127 } 1128 return ret; 1129 } 1130 1131 static int 1132 nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum) 1133 { 1134 struct sk_buff *skb, *segs, *nskb; 1135 bool ct_is_unconfirmed = false; 1136 struct nfqnl_instance *queue; 1137 unsigned int queued; 1138 int err = -ENOBUFS; 1139 struct net *net = entry->state.net; 1140 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 1141 1142 /* rcu_read_lock()ed by nf_hook_thresh */ 1143 queue = instance_lookup(q, queuenum); 1144 if (!queue) 1145 return -ESRCH; 1146 1147 if (queue->copy_mode == NFQNL_COPY_NONE) 1148 return -EINVAL; 1149 1150 skb = entry->skb; 1151 1152 switch (entry->state.pf) { 1153 case NFPROTO_IPV4: 1154 skb->protocol = htons(ETH_P_IP); 1155 break; 1156 case NFPROTO_IPV6: 1157 skb->protocol = htons(ETH_P_IPV6); 1158 break; 1159 } 1160 1161 /* Check if someone already holds another reference to 1162 * unconfirmed ct. If so, we cannot queue the skb: 1163 * concurrent modifications of nf_conn->ext are not 1164 * allowed and we can't know if another CPU isn't 1165 * processing the same nf_conn entry in parallel. 1166 */ 1167 if (nf_ct_drop_unconfirmed(entry, &ct_is_unconfirmed)) 1168 return -EINVAL; 1169 1170 if (!skb_is_gso(skb) || ((queue->flags & NFQA_CFG_F_GSO) && !skb_is_gso_sctp(skb))) 1171 return __nfqnl_enqueue_packet(net, queue, entry); 1172 1173 nf_bridge_adjust_skb_data(skb); 1174 segs = skb_gso_segment(skb, 0); 1175 /* Does not use PTR_ERR to limit the number of error codes that can be 1176 * returned by nf_queue. For instance, callers rely on -ESRCH to 1177 * mean 'ignore this hook'. 1178 */ 1179 if (IS_ERR_OR_NULL(segs)) 1180 goto out_err; 1181 queued = 0; 1182 err = 0; 1183 1184 skb_list_walk_safe(segs, segs, nskb) { 1185 if (ct_is_unconfirmed && queued > 0) { 1186 /* skb_gso_segment() increments the ct refcount. 1187 * This is a problem for unconfirmed (not in hash) 1188 * entries, those can race when reinjections happen 1189 * in parallel. 1190 * 1191 * Annotate this for all queued entries except the 1192 * first one. 1193 * 1194 * As long as the first one is reinjected first it 1195 * will do the confirmation for us. 1196 */ 1197 entry->nf_ct_is_unconfirmed = ct_is_unconfirmed; 1198 } 1199 1200 if (err == 0) 1201 err = __nfqnl_enqueue_packet_gso(net, queue, 1202 segs, entry); 1203 if (err == 0) 1204 queued++; 1205 else 1206 kfree_skb(segs); 1207 } 1208 1209 if (queued) { 1210 if (err) /* some segments are already queued */ 1211 nf_queue_entry_free(entry); 1212 kfree_skb(skb); 1213 return 0; 1214 } 1215 out_err: 1216 nf_bridge_adjust_segmented_data(skb); 1217 return err; 1218 } 1219 1220 static bool nfqnl_validate_ipopts(const struct iphdr *iph_new, 1221 const struct nf_queue_entry *e) 1222 { 1223 const struct iphdr *iph_orig = ip_hdr(e->skb); 1224 unsigned int ihl = iph_new->ihl * 4; 1225 1226 if (iph_new->ihl != iph_orig->ihl) 1227 return false; 1228 if (ihl == sizeof(*iph_orig)) 1229 return true; 1230 1231 return memcmp(iph_new + 1, ip_hdr(e->skb) + 1, ihl - sizeof(*iph_orig)) == 0; 1232 } 1233 1234 static bool nfqnl_validate_l4(const u8 *data, unsigned int data_len, 1235 const struct nf_queue_entry *e, u8 proto, 1236 bool fragment) 1237 { 1238 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 1239 enum ip_conntrack_info ctinfo; 1240 const struct nf_conn *ct; 1241 1242 ct = nf_ct_get(e->skb, &ctinfo); 1243 if (ct && !nf_ct_is_template(ct)) { 1244 if (fragment || nf_ct_protonum(ct) != proto) 1245 return false; 1246 } 1247 #endif 1248 1249 if (fragment) 1250 return true; 1251 1252 switch (proto) { 1253 case IPPROTO_TCP: { 1254 const struct tcphdr *th = (const struct tcphdr *)data; 1255 unsigned int thlen; 1256 1257 if (data_len < sizeof(*th)) 1258 return false; 1259 1260 thlen = __tcp_hdrlen(th); 1261 if (thlen < sizeof(*th) || data_len < thlen) 1262 return false; 1263 1264 return true; 1265 } 1266 case IPPROTO_UDP: 1267 return data_len >= sizeof(struct udphdr); 1268 case IPPROTO_ICMP: 1269 return data_len >= sizeof(struct icmphdr); 1270 case IPPROTO_ICMPV6: 1271 return data_len >= sizeof(struct icmp6hdr); 1272 case IPPROTO_SCTP: 1273 return data_len >= sizeof(struct sctphdr); 1274 case IPPROTO_GRE: 1275 return data_len >= sizeof(struct gre_base_hdr); 1276 case IPPROTO_AH: 1277 return data_len >= sizeof(struct ip_auth_hdr); 1278 case IPPROTO_ESP: 1279 return data_len >= sizeof(struct ip_esp_hdr); 1280 } 1281 1282 return true; 1283 } 1284 1285 static bool nfqnl_validate_ip4(const struct iphdr *iph, unsigned int data_len, 1286 const struct nf_queue_entry *e) 1287 { 1288 unsigned int ihl; 1289 bool fragment; 1290 1291 if (data_len < sizeof(*iph)) 1292 return false; 1293 1294 ihl = iph->ihl * 4u; 1295 if (ihl < sizeof(*iph) || data_len < ihl) 1296 return false; 1297 1298 if (iph->version != 4 || 1299 ((iph->frag_off ^ ip_hdr(e->skb)->frag_off) & ~htons(IP_DF)) != 0) 1300 return false; 1301 1302 /* BIG TCP won't work; netlink attr len is u16 */ 1303 if (ntohs(iph->tot_len) != data_len) 1304 return false; 1305 1306 fragment = iph->frag_off & htons(IP_MF | IP_OFFSET); 1307 1308 /* support for ipopts mangling would require 1309 * recompile + skb transport header update. 1310 */ 1311 return nfqnl_validate_ipopts(iph, e) && 1312 nfqnl_validate_l4((const u8 *)iph + ihl, data_len - ihl, e, 1313 iph->protocol, fragment); 1314 } 1315 1316 static bool nfqnl_validate_one_exthdr(const u8 *data, 1317 unsigned int data_len, 1318 const struct nf_queue_entry *e, 1319 int start, int hdrlen) 1320 { 1321 u16 octets; 1322 1323 if (data_len < hdrlen || hdrlen < 2) 1324 return false; 1325 1326 while (hdrlen > 0) { 1327 if (data_len < sizeof(octets)) 1328 return false; 1329 data_len -= sizeof(octets); 1330 1331 if (skb_copy_bits(e->skb, start, &octets, sizeof(octets))) 1332 return false; 1333 1334 if (hdrlen < sizeof(octets)) 1335 return false; 1336 1337 hdrlen -= sizeof(octets); 1338 if (memcmp(data, &octets, sizeof(octets))) 1339 return false; 1340 1341 start += sizeof(octets); 1342 data += sizeof(octets); 1343 } 1344 1345 return true; 1346 } 1347 1348 static bool nfqnl_validate_exthdr(const struct ipv6hdr *ip6_new, 1349 unsigned int data_len, 1350 const struct nf_queue_entry *e) 1351 { 1352 const struct ipv6hdr *ip6_orig = ipv6_hdr(e->skb); 1353 int exthdr_cnt = 0, start = sizeof(*ip6_orig); 1354 const u8 *data = (const u8 *)ip6_new; 1355 u8 orig_nexthdr = ip6_orig->nexthdr; 1356 u8 new_nexthdr = ip6_new->nexthdr; 1357 bool fragment = false; 1358 1359 if (new_nexthdr != orig_nexthdr) 1360 return false; 1361 1362 data += sizeof(*ip6_new); 1363 data_len -= sizeof(*ip6_new); 1364 1365 while (ipv6_ext_hdr(orig_nexthdr)) { 1366 const struct ipv6_opt_hdr *hp; 1367 struct ipv6_opt_hdr _hdr; 1368 int hdrlen; 1369 1370 if (orig_nexthdr == NEXTHDR_NONE) 1371 return nfqnl_validate_l4(data, data_len, e, 1372 new_nexthdr, fragment); 1373 1374 if (unlikely(exthdr_cnt++ >= IP6_MAX_EXT_HDRS_CNT)) 1375 return false; 1376 1377 hp = skb_header_pointer(e->skb, start, sizeof(_hdr), &_hdr); 1378 if (!hp) 1379 return false; 1380 1381 switch (orig_nexthdr) { 1382 case NEXTHDR_FRAGMENT: 1383 fragment = true; 1384 hdrlen = sizeof(struct frag_hdr); 1385 break; 1386 case NEXTHDR_AUTH: 1387 hdrlen = ipv6_authlen(hp); 1388 break; 1389 default: 1390 hdrlen = ipv6_optlen(hp); 1391 break; 1392 } 1393 1394 if (!nfqnl_validate_one_exthdr(data, data_len, e, 1395 start, hdrlen)) 1396 return false; 1397 1398 orig_nexthdr = hp->nexthdr; 1399 hp = (const void *)data; 1400 new_nexthdr = hp->nexthdr; 1401 1402 if (new_nexthdr != orig_nexthdr) 1403 return false; 1404 1405 data_len -= hdrlen; 1406 start += hdrlen; 1407 data += hdrlen; 1408 } 1409 1410 return nfqnl_validate_l4(data, data_len, e, new_nexthdr, fragment); 1411 } 1412 1413 static bool nfqnl_validate_ip6(const struct ipv6hdr *ip6, unsigned int data_len, 1414 const struct nf_queue_entry *e) 1415 { 1416 if (data_len < sizeof(*ip6)) 1417 return false; 1418 1419 /* BIG TCP/jumbograms won't work; netlink attr len is u16 */ 1420 if (ntohs(ip6->payload_len) != data_len - sizeof(*ip6)) 1421 return false; 1422 1423 if (ip6->version != 6) 1424 return false; 1425 1426 return nfqnl_validate_exthdr(ip6, data_len, e); 1427 } 1428 1429 static bool nfqnl_validate_write(const void *data, unsigned int data_len, 1430 const struct nf_queue_entry *e) 1431 { 1432 switch (e->state.pf) { 1433 case NFPROTO_IPV4: 1434 return nfqnl_validate_ip4(data, data_len, e); 1435 case NFPROTO_IPV6: 1436 return nfqnl_validate_ip6(data, data_len, e) && 1437 !(IP6CB(e->skb)->flags & IP6SKB_JUMBOGRAM); 1438 case NFPROTO_BRIDGE: 1439 /* No write support. Bridge is dubious: userspace doesn't even see L2 header */ 1440 return false; 1441 } 1442 1443 return false; 1444 } 1445 1446 static int 1447 nfqnl_mangle(void *data, unsigned int data_len, struct nf_queue_entry *e, int diff) 1448 { 1449 struct sk_buff *nskb; 1450 1451 if (e->state.net->user_ns != &init_user_ns) 1452 return -EPERM; 1453 1454 if (!nfqnl_validate_write(data, data_len, e)) 1455 return -EINVAL; 1456 1457 if (diff < 0) { 1458 unsigned int min_len = skb_transport_offset(e->skb); 1459 1460 if (data_len < min_len) 1461 return -EINVAL; 1462 1463 if (pskb_trim(e->skb, data_len)) 1464 return -ENOMEM; 1465 } else if (diff > 0) { 1466 if (data_len > 0xFFFF) 1467 return -EINVAL; 1468 if (diff > skb_tailroom(e->skb)) { 1469 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb), 1470 diff, GFP_ATOMIC); 1471 if (!nskb) 1472 return -ENOMEM; 1473 kfree_skb(e->skb); 1474 e->skb = nskb; 1475 } 1476 skb_put(e->skb, diff); 1477 } 1478 if (skb_ensure_writable(e->skb, data_len)) 1479 return -ENOMEM; 1480 skb_copy_to_linear_data(e->skb, data, data_len); 1481 e->skb->ip_summed = CHECKSUM_NONE; 1482 return 0; 1483 } 1484 1485 static int 1486 nfqnl_set_mode(struct nfqnl_instance *queue, 1487 unsigned char mode, unsigned int range) 1488 { 1489 int status = 0; 1490 1491 spin_lock_bh(&queue->lock); 1492 switch (mode) { 1493 case NFQNL_COPY_NONE: 1494 case NFQNL_COPY_META: 1495 queue->copy_mode = mode; 1496 queue->copy_range = 0; 1497 break; 1498 1499 case NFQNL_COPY_PACKET: 1500 queue->copy_mode = mode; 1501 if (range == 0 || range > NFQNL_MAX_COPY_RANGE) 1502 queue->copy_range = NFQNL_MAX_COPY_RANGE; 1503 else 1504 queue->copy_range = range; 1505 break; 1506 1507 default: 1508 status = -EINVAL; 1509 1510 } 1511 spin_unlock_bh(&queue->lock); 1512 1513 return status; 1514 } 1515 1516 static int 1517 dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex) 1518 { 1519 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 1520 int physinif, physoutif; 1521 1522 physinif = nf_bridge_get_physinif(entry->skb); 1523 physoutif = nf_bridge_get_physoutif(entry->skb); 1524 1525 if (physinif == ifindex || physoutif == ifindex) 1526 return 1; 1527 1528 if (entry->bridge_dev && entry->bridge_dev->ifindex == ifindex) 1529 return 1; 1530 #endif 1531 if (entry->skb_dev && entry->skb_dev->ifindex == ifindex) 1532 return 1; 1533 if (entry->state.in) 1534 if (entry->state.in->ifindex == ifindex) 1535 return 1; 1536 if (entry->state.out) 1537 if (entry->state.out->ifindex == ifindex) 1538 return 1; 1539 1540 return 0; 1541 } 1542 1543 /* drop all packets with either indev or outdev == ifindex from all queue 1544 * instances */ 1545 static void 1546 nfqnl_dev_drop(struct net *net, int ifindex) 1547 { 1548 int i; 1549 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 1550 1551 rcu_read_lock(); 1552 1553 for (i = 0; i < INSTANCE_BUCKETS; i++) { 1554 struct nfqnl_instance *inst; 1555 struct hlist_head *head = &q->instance_table[i]; 1556 1557 hlist_for_each_entry_rcu(inst, head, hlist) 1558 nfqnl_flush(inst, dev_cmp, ifindex); 1559 } 1560 1561 rcu_read_unlock(); 1562 } 1563 1564 static int 1565 nfqnl_rcv_dev_event(struct notifier_block *this, 1566 unsigned long event, void *ptr) 1567 { 1568 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1569 1570 /* Drop any packets associated with the downed device */ 1571 if (event == NETDEV_DOWN) 1572 nfqnl_dev_drop(dev_net(dev), dev->ifindex); 1573 return NOTIFY_DONE; 1574 } 1575 1576 static struct notifier_block nfqnl_dev_notifier = { 1577 .notifier_call = nfqnl_rcv_dev_event, 1578 }; 1579 1580 /* Match the entries whose hook_index points into the hook array that 1581 * __nf_unregister_net_hook() replaces when it removes @ops. 1582 * 1583 * ops->pf/hooknum name the hook as registered; entry->state.pf/hook 1584 * name the array actually traversed - they differ only for NFPROTO_INET. 1585 * Every other family uses the array of its own name. 1586 */ 1587 static int nf_hook_cmp(struct nf_queue_entry *entry, unsigned long ops_ptr) 1588 { 1589 const struct nf_hook_ops *ops = (const struct nf_hook_ops *)ops_ptr; 1590 unsigned int hooknum = ops->hooknum; 1591 u8 pf = ops->pf; 1592 1593 /* same translation as nf_static_key_inc() */ 1594 if (pf == NFPROTO_INET && hooknum == NF_INET_INGRESS) { 1595 pf = NFPROTO_NETDEV; 1596 hooknum = NF_NETDEV_INGRESS; 1597 } 1598 1599 if (entry->state.hook != hooknum) 1600 return 0; 1601 1602 if (pf == NFPROTO_INET) 1603 return entry->state.pf == NFPROTO_IPV4 || 1604 entry->state.pf == NFPROTO_IPV6; 1605 1606 return entry->state.pf == pf; 1607 } 1608 1609 static void nfqnl_nf_hook_drop(struct net *net, const struct nf_hook_ops *ops) 1610 { 1611 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 1612 nfqnl_cmpfn cmpfn = NULL; 1613 int i; 1614 1615 /* This function is also called on net namespace error unwind, 1616 * when pernet_ops->init() failed and ->exit() functions of the 1617 * previous pernet_ops gets called. 1618 * 1619 * This may result in a call to nfqnl_nf_hook_drop() before 1620 * struct nfnl_queue_net was allocated. 1621 */ 1622 if (!q) 1623 return; 1624 1625 /* ops is NULL when the caller is not unregistering a hook but 1626 * removing an object queued packets may reference, every entry 1627 * has to go 1628 */ 1629 if (ops) 1630 cmpfn = nf_hook_cmp; 1631 1632 for (i = 0; i < INSTANCE_BUCKETS; i++) { 1633 struct nfqnl_instance *inst; 1634 struct hlist_head *head = &q->instance_table[i]; 1635 1636 hlist_for_each_entry_rcu(inst, head, hlist) 1637 nfqnl_flush(inst, cmpfn, (unsigned long)ops); 1638 } 1639 } 1640 1641 static int 1642 nfqnl_rcv_nl_event(struct notifier_block *this, 1643 unsigned long event, void *ptr) 1644 { 1645 struct netlink_notify *n = ptr; 1646 struct nfnl_queue_net *q = nfnl_queue_pernet(n->net); 1647 1648 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) { 1649 int i; 1650 1651 nfnl_lock(NFNL_SUBSYS_QUEUE); 1652 /* destroy all instances for this portid */ 1653 spin_lock(&q->instances_lock); 1654 for (i = 0; i < INSTANCE_BUCKETS; i++) { 1655 struct hlist_node *t2; 1656 struct nfqnl_instance *inst; 1657 struct hlist_head *head = &q->instance_table[i]; 1658 1659 hlist_for_each_entry_safe(inst, t2, head, hlist) { 1660 if (n->portid == inst->peer_portid) 1661 __instance_destroy(inst); 1662 } 1663 } 1664 spin_unlock(&q->instances_lock); 1665 nfnl_unlock(NFNL_SUBSYS_QUEUE); 1666 } 1667 return NOTIFY_DONE; 1668 } 1669 1670 static struct notifier_block nfqnl_rtnl_notifier = { 1671 .notifier_call = nfqnl_rcv_nl_event, 1672 }; 1673 1674 static const struct nla_policy nfqa_vlan_policy[NFQA_VLAN_MAX + 1] = { 1675 [NFQA_VLAN_TCI] = { .type = NLA_U16}, 1676 [NFQA_VLAN_PROTO] = { .type = NLA_U16}, 1677 }; 1678 1679 static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = { 1680 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, 1681 [NFQA_MARK] = { .type = NLA_U32 }, 1682 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC }, 1683 [NFQA_CT] = { .type = NLA_UNSPEC }, 1684 [NFQA_EXP] = { .type = NLA_UNSPEC }, 1685 [NFQA_VLAN] = { .type = NLA_NESTED }, 1686 [NFQA_PRIORITY] = { .type = NLA_U32 }, 1687 }; 1688 1689 static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = { 1690 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, 1691 [NFQA_MARK] = { .type = NLA_U32 }, 1692 [NFQA_PRIORITY] = { .type = NLA_U32 }, 1693 }; 1694 1695 static struct nfqnl_instance * 1696 verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, u32 nlportid) 1697 { 1698 struct nfqnl_instance *queue; 1699 1700 queue = instance_lookup(q, queue_num); 1701 if (!queue) 1702 return ERR_PTR(-ENODEV); 1703 1704 if (queue->peer_portid != nlportid) 1705 return ERR_PTR(-EPERM); 1706 1707 return queue; 1708 } 1709 1710 static struct nfqnl_msg_verdict_hdr* 1711 verdicthdr_get(const struct nlattr * const nfqa[]) 1712 { 1713 struct nfqnl_msg_verdict_hdr *vhdr; 1714 unsigned int verdict; 1715 1716 if (!nfqa[NFQA_VERDICT_HDR]) 1717 return NULL; 1718 1719 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]); 1720 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK; 1721 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN) 1722 return NULL; 1723 return vhdr; 1724 } 1725 1726 static int nfq_id_after(unsigned int id, unsigned int max) 1727 { 1728 return (int)(id - max) > 0; 1729 } 1730 1731 static int nfqnl_recv_verdict_batch(struct sk_buff *skb, 1732 const struct nfnl_info *info, 1733 const struct nlattr * const nfqa[]) 1734 { 1735 struct nfnl_queue_net *q = nfnl_queue_pernet(info->net); 1736 u16 queue_num = ntohs(info->nfmsg->res_id); 1737 struct nf_queue_entry *entry, *tmp; 1738 struct nfqnl_msg_verdict_hdr *vhdr; 1739 struct nfqnl_instance *queue; 1740 unsigned int verdict, maxid; 1741 LIST_HEAD(batch_list); 1742 1743 queue = verdict_instance_lookup(q, queue_num, 1744 NETLINK_CB(skb).portid); 1745 if (IS_ERR(queue)) 1746 return PTR_ERR(queue); 1747 1748 vhdr = verdicthdr_get(nfqa); 1749 if (!vhdr) 1750 return -EINVAL; 1751 1752 verdict = ntohl(vhdr->verdict); 1753 maxid = ntohl(vhdr->id); 1754 1755 spin_lock_bh(&queue->lock); 1756 1757 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) { 1758 if (nfq_id_after(entry->id, maxid)) 1759 break; 1760 __dequeue_entry(queue, entry); 1761 list_add_tail(&entry->list, &batch_list); 1762 } 1763 1764 spin_unlock_bh(&queue->lock); 1765 1766 if (list_empty(&batch_list)) 1767 return -ENOENT; 1768 1769 list_for_each_entry_safe(entry, tmp, &batch_list, list) { 1770 if (nfqa[NFQA_MARK]) 1771 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK])); 1772 1773 if (nfqa[NFQA_PRIORITY]) 1774 entry->skb->priority = ntohl(nla_get_be32(nfqa[NFQA_PRIORITY])); 1775 1776 nfqnl_reinject(entry, verdict); 1777 } 1778 return 0; 1779 } 1780 1781 static struct nf_conn *nfqnl_ct_parse(const struct nfnl_ct_hook *nfnl_ct, 1782 const struct nlmsghdr *nlh, 1783 const struct nlattr * const nfqa[], 1784 struct nf_queue_entry *entry, 1785 enum ip_conntrack_info *ctinfo) 1786 { 1787 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 1788 struct nf_conn *ct; 1789 1790 ct = nf_ct_get(entry->skb, ctinfo); 1791 if (ct == NULL) 1792 return NULL; 1793 1794 if (nfnl_ct->parse(nfqa[NFQA_CT], ct) < 0) 1795 return NULL; 1796 1797 if (nfqa[NFQA_EXP]) 1798 nfnl_ct->attach_expect(nfqa[NFQA_EXP], ct, 1799 NETLINK_CB(entry->skb).portid, 1800 nlmsg_report(nlh)); 1801 return ct; 1802 #else 1803 return NULL; 1804 #endif 1805 } 1806 1807 static int nfqa_parse_bridge(struct nf_queue_entry *entry, 1808 const struct nlattr * const nfqa[]) 1809 { 1810 if (nfqa[NFQA_VLAN]) { 1811 struct nlattr *tb[NFQA_VLAN_MAX + 1]; 1812 int err; 1813 1814 err = nla_parse_nested_deprecated(tb, NFQA_VLAN_MAX, 1815 nfqa[NFQA_VLAN], 1816 nfqa_vlan_policy, NULL); 1817 if (err < 0) 1818 return err; 1819 1820 if (!tb[NFQA_VLAN_TCI] || !tb[NFQA_VLAN_PROTO]) 1821 return -EINVAL; 1822 1823 __vlan_hwaccel_put_tag(entry->skb, 1824 nla_get_be16(tb[NFQA_VLAN_PROTO]), 1825 ntohs(nla_get_be16(tb[NFQA_VLAN_TCI]))); 1826 } 1827 1828 if (nfqa[NFQA_L2HDR]) { 1829 u32 mac_header_len = skb_mac_header_len(entry->skb); 1830 1831 if (mac_header_len != nla_len(nfqa[NFQA_L2HDR])) 1832 return -EINVAL; 1833 else if (mac_header_len > 0) 1834 memcpy(skb_mac_header(entry->skb), 1835 nla_data(nfqa[NFQA_L2HDR]), 1836 mac_header_len); 1837 } 1838 1839 return 0; 1840 } 1841 1842 static int nfqnl_recv_verdict(struct sk_buff *skb, const struct nfnl_info *info, 1843 const struct nlattr * const nfqa[]) 1844 { 1845 struct nfnl_queue_net *q = nfnl_queue_pernet(info->net); 1846 u_int16_t queue_num = ntohs(info->nfmsg->res_id); 1847 const struct nfnl_ct_hook *nfnl_ct; 1848 struct nfqnl_msg_verdict_hdr *vhdr; 1849 enum ip_conntrack_info ctinfo; 1850 struct nfqnl_instance *queue; 1851 struct nf_queue_entry *entry; 1852 struct nf_conn *ct = NULL; 1853 unsigned int verdict; 1854 int err; 1855 1856 queue = verdict_instance_lookup(q, queue_num, 1857 NETLINK_CB(skb).portid); 1858 if (IS_ERR(queue)) 1859 return PTR_ERR(queue); 1860 1861 vhdr = verdicthdr_get(nfqa); 1862 if (!vhdr) 1863 return -EINVAL; 1864 1865 verdict = ntohl(vhdr->verdict); 1866 1867 entry = find_dequeue_entry(queue, ntohl(vhdr->id)); 1868 if (entry == NULL) 1869 return -ENOENT; 1870 1871 /* rcu lock already held from nfnl->call_rcu. */ 1872 nfnl_ct = rcu_dereference(nfnl_ct_hook); 1873 1874 if (nfqa[NFQA_CT]) { 1875 if (nfnl_ct != NULL) 1876 ct = nfqnl_ct_parse(nfnl_ct, info->nlh, nfqa, entry, 1877 &ctinfo); 1878 } 1879 1880 if (entry->state.pf == PF_BRIDGE) { 1881 err = nfqa_parse_bridge(entry, nfqa); 1882 if (err < 0) { 1883 nfqnl_reinject(entry, NF_DROP); 1884 return err; 1885 } 1886 } 1887 1888 if (nfqa[NFQA_PAYLOAD]) { 1889 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]); 1890 int diff = payload_len - entry->skb->len; 1891 1892 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]), 1893 payload_len, entry, diff) < 0) 1894 verdict = NF_DROP; 1895 else if (ct && diff) 1896 nfnl_ct->seq_adjust(entry->skb, ct, ctinfo, diff); 1897 } 1898 1899 if (nfqa[NFQA_MARK]) 1900 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK])); 1901 1902 if (nfqa[NFQA_PRIORITY]) 1903 entry->skb->priority = ntohl(nla_get_be32(nfqa[NFQA_PRIORITY])); 1904 1905 nfqnl_reinject(entry, verdict); 1906 return 0; 1907 } 1908 1909 static int nfqnl_recv_unsupp(struct sk_buff *skb, const struct nfnl_info *info, 1910 const struct nlattr * const cda[]) 1911 { 1912 return -ENOTSUPP; 1913 } 1914 1915 static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = { 1916 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) }, 1917 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) }, 1918 [NFQA_CFG_QUEUE_MAXLEN] = { .type = NLA_U32 }, 1919 [NFQA_CFG_MASK] = { .type = NLA_U32 }, 1920 [NFQA_CFG_FLAGS] = NLA_POLICY_MASK(NLA_BE32, NFQA_CFG_F_MAX - 1), 1921 }; 1922 1923 static const struct nf_queue_handler nfqh = { 1924 .outfn = nfqnl_enqueue_packet, 1925 .nf_hook_drop = nfqnl_nf_hook_drop, 1926 }; 1927 1928 static int nfqnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info, 1929 const struct nlattr * const nfqa[]) 1930 { 1931 struct nfnl_queue_net *q = nfnl_queue_pernet(info->net); 1932 u_int16_t queue_num = ntohs(info->nfmsg->res_id); 1933 struct nfqnl_msg_config_cmd *cmd = NULL; 1934 struct nfqnl_instance *queue; 1935 __u32 flags = 0, mask = 0; 1936 1937 WARN_ON_ONCE(!lockdep_nfnl_is_held(NFNL_SUBSYS_QUEUE)); 1938 1939 if (nfqa[NFQA_CFG_CMD]) { 1940 cmd = nla_data(nfqa[NFQA_CFG_CMD]); 1941 1942 /* Obsolete commands without queue context */ 1943 switch (cmd->command) { 1944 case NFQNL_CFG_CMD_PF_BIND: return 0; 1945 case NFQNL_CFG_CMD_PF_UNBIND: return 0; 1946 } 1947 } 1948 1949 /* Check if we support these flags in first place, dependencies should 1950 * be there too not to break atomicity. 1951 */ 1952 if (nfqa[NFQA_CFG_FLAGS]) { 1953 if (!nfqa[NFQA_CFG_MASK]) { 1954 /* A mask is needed to specify which flags are being 1955 * changed. 1956 */ 1957 return -EINVAL; 1958 } 1959 1960 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS])); 1961 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK])); 1962 1963 if (flags >= NFQA_CFG_F_MAX) 1964 return -EOPNOTSUPP; 1965 1966 #if !IS_ENABLED(CONFIG_NETWORK_SECMARK) 1967 if (flags & mask & NFQA_CFG_F_SECCTX) 1968 return -EOPNOTSUPP; 1969 #endif 1970 if ((flags & mask & NFQA_CFG_F_CONNTRACK) && 1971 !rcu_access_pointer(nfnl_ct_hook)) { 1972 #ifdef CONFIG_MODULES 1973 nfnl_unlock(NFNL_SUBSYS_QUEUE); 1974 request_module("ip_conntrack_netlink"); 1975 nfnl_lock(NFNL_SUBSYS_QUEUE); 1976 if (rcu_access_pointer(nfnl_ct_hook)) 1977 return -EAGAIN; 1978 #endif 1979 return -EOPNOTSUPP; 1980 } 1981 } 1982 1983 /* Lookup queue under RCU. After peer_portid check (or for new queue 1984 * in BIND case), the queue is owned by the socket sending this message. 1985 * nfqnl_rcv_nl_event() will block on the nfnl subsys mutex that is 1986 * held by the caller, so the queue cannot be destroyed in parallel, 1987 * even after we drop the RCU read lock. 1988 */ 1989 rcu_read_lock(); 1990 queue = instance_lookup(q, queue_num); 1991 if (queue && queue->peer_portid != NETLINK_CB(skb).portid) { 1992 rcu_read_unlock(); 1993 return -EPERM; 1994 } 1995 rcu_read_unlock(); 1996 1997 if (cmd != NULL) { 1998 switch (cmd->command) { 1999 case NFQNL_CFG_CMD_BIND: 2000 if (queue) 2001 return -EBUSY; 2002 queue = instance_create(q, queue_num, NETLINK_CB(skb).portid); 2003 if (IS_ERR(queue)) 2004 return PTR_ERR(queue); 2005 break; 2006 case NFQNL_CFG_CMD_UNBIND: 2007 if (!queue) 2008 return -ENODEV; 2009 instance_destroy(q, queue); 2010 return 0; 2011 case NFQNL_CFG_CMD_PF_BIND: 2012 case NFQNL_CFG_CMD_PF_UNBIND: 2013 break; 2014 default: 2015 return -EOPNOTSUPP; 2016 } 2017 } 2018 2019 if (!queue) 2020 return -ENODEV; 2021 2022 if (nfqa[NFQA_CFG_PARAMS]) { 2023 struct nfqnl_msg_config_params *params = 2024 nla_data(nfqa[NFQA_CFG_PARAMS]); 2025 2026 nfqnl_set_mode(queue, params->copy_mode, 2027 ntohl(params->copy_range)); 2028 } 2029 2030 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) { 2031 __be32 *queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]); 2032 2033 spin_lock_bh(&queue->lock); 2034 queue->queue_maxlen = ntohl(*queue_maxlen); 2035 spin_unlock_bh(&queue->lock); 2036 } 2037 2038 if (nfqa[NFQA_CFG_FLAGS]) { 2039 spin_lock_bh(&queue->lock); 2040 queue->flags &= ~mask; 2041 queue->flags |= flags & mask; 2042 spin_unlock_bh(&queue->lock); 2043 } 2044 2045 return 0; 2046 } 2047 2048 static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = { 2049 [NFQNL_MSG_PACKET] = { 2050 .call = nfqnl_recv_unsupp, 2051 .type = NFNL_CB_RCU, 2052 .attr_count = NFQA_MAX, 2053 }, 2054 [NFQNL_MSG_VERDICT] = { 2055 .call = nfqnl_recv_verdict, 2056 .type = NFNL_CB_RCU, 2057 .attr_count = NFQA_MAX, 2058 .policy = nfqa_verdict_policy 2059 }, 2060 [NFQNL_MSG_CONFIG] = { 2061 .call = nfqnl_recv_config, 2062 .type = NFNL_CB_MUTEX, 2063 .attr_count = NFQA_CFG_MAX, 2064 .policy = nfqa_cfg_policy 2065 }, 2066 [NFQNL_MSG_VERDICT_BATCH] = { 2067 .call = nfqnl_recv_verdict_batch, 2068 .type = NFNL_CB_RCU, 2069 .attr_count = NFQA_MAX, 2070 .policy = nfqa_verdict_batch_policy 2071 }, 2072 }; 2073 2074 static const struct nfnetlink_subsystem nfqnl_subsys = { 2075 .name = "nf_queue", 2076 .subsys_id = NFNL_SUBSYS_QUEUE, 2077 .cb_count = NFQNL_MSG_MAX, 2078 .cb = nfqnl_cb, 2079 }; 2080 2081 #ifdef CONFIG_PROC_FS 2082 struct iter_state { 2083 struct seq_net_private p; 2084 unsigned int bucket; 2085 }; 2086 2087 static struct hlist_node *get_first(struct seq_file *seq) 2088 { 2089 struct iter_state *st = seq->private; 2090 struct net *net; 2091 struct nfnl_queue_net *q; 2092 2093 if (!st) 2094 return NULL; 2095 2096 net = seq_file_net(seq); 2097 q = nfnl_queue_pernet(net); 2098 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) { 2099 if (!hlist_empty(&q->instance_table[st->bucket])) 2100 return q->instance_table[st->bucket].first; 2101 } 2102 return NULL; 2103 } 2104 2105 static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h) 2106 { 2107 struct iter_state *st = seq->private; 2108 struct net *net = seq_file_net(seq); 2109 2110 h = h->next; 2111 while (!h) { 2112 struct nfnl_queue_net *q; 2113 2114 if (++st->bucket >= INSTANCE_BUCKETS) 2115 return NULL; 2116 2117 q = nfnl_queue_pernet(net); 2118 h = q->instance_table[st->bucket].first; 2119 } 2120 return h; 2121 } 2122 2123 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos) 2124 { 2125 struct hlist_node *head; 2126 head = get_first(seq); 2127 2128 if (head) 2129 while (pos && (head = get_next(seq, head))) 2130 pos--; 2131 return pos ? NULL : head; 2132 } 2133 2134 static void *seq_start(struct seq_file *s, loff_t *pos) 2135 __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock) 2136 { 2137 spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock); 2138 return get_idx(s, *pos); 2139 } 2140 2141 static void *seq_next(struct seq_file *s, void *v, loff_t *pos) 2142 { 2143 (*pos)++; 2144 return get_next(s, v); 2145 } 2146 2147 static void seq_stop(struct seq_file *s, void *v) 2148 __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock) 2149 { 2150 spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock); 2151 } 2152 2153 static int seq_show(struct seq_file *s, void *v) 2154 { 2155 const struct nfqnl_instance *inst = v; 2156 2157 seq_printf(s, "%5u %6u %5u %1u %5u %5u %5u %8u %2d\n", 2158 inst->queue_num, 2159 inst->peer_portid, inst->queue_total, 2160 inst->copy_mode, inst->copy_range, 2161 inst->queue_dropped, inst->queue_user_dropped, 2162 inst->id_sequence, 1); 2163 return 0; 2164 } 2165 2166 static const struct seq_operations nfqnl_seq_ops = { 2167 .start = seq_start, 2168 .next = seq_next, 2169 .stop = seq_stop, 2170 .show = seq_show, 2171 }; 2172 #endif /* PROC_FS */ 2173 2174 static int __net_init nfnl_queue_net_init(struct net *net) 2175 { 2176 unsigned int i; 2177 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 2178 2179 for (i = 0; i < INSTANCE_BUCKETS; i++) 2180 INIT_HLIST_HEAD(&q->instance_table[i]); 2181 2182 spin_lock_init(&q->instances_lock); 2183 2184 #ifdef CONFIG_PROC_FS 2185 if (!proc_create_net("nfnetlink_queue", 0440, net->nf.proc_netfilter, 2186 &nfqnl_seq_ops, sizeof(struct iter_state))) 2187 return -ENOMEM; 2188 #endif 2189 return 0; 2190 } 2191 2192 static void __net_exit nfnl_queue_net_exit(struct net *net) 2193 { 2194 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 2195 unsigned int i; 2196 2197 #ifdef CONFIG_PROC_FS 2198 remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter); 2199 #endif 2200 for (i = 0; i < INSTANCE_BUCKETS; i++) 2201 WARN_ON_ONCE(!hlist_empty(&q->instance_table[i])); 2202 } 2203 2204 static struct pernet_operations nfnl_queue_net_ops = { 2205 .init = nfnl_queue_net_init, 2206 .exit = nfnl_queue_net_exit, 2207 .id = &nfnl_queue_net_id, 2208 .size = sizeof(struct nfnl_queue_net), 2209 }; 2210 2211 static int __init nfnetlink_queue_init(void) 2212 { 2213 int status; 2214 2215 nfq_cleanup_wq = alloc_ordered_workqueue("nfq_workqueue", 0); 2216 if (!nfq_cleanup_wq) 2217 return -ENOMEM; 2218 2219 status = register_pernet_subsys(&nfnl_queue_net_ops); 2220 if (status < 0) 2221 goto cleanup_pernet_subsys; 2222 2223 status = netlink_register_notifier(&nfqnl_rtnl_notifier); 2224 if (status < 0) 2225 goto cleanup_rtnl_notifier; 2226 2227 status = register_netdevice_notifier(&nfqnl_dev_notifier); 2228 if (status < 0) 2229 goto cleanup_dev_notifier; 2230 2231 status = nfnetlink_subsys_register(&nfqnl_subsys); 2232 if (status < 0) 2233 goto cleanup_nfqnl_subsys; 2234 2235 nf_register_queue_handler(&nfqh); 2236 2237 return status; 2238 2239 cleanup_nfqnl_subsys: 2240 unregister_netdevice_notifier(&nfqnl_dev_notifier); 2241 cleanup_dev_notifier: 2242 netlink_unregister_notifier(&nfqnl_rtnl_notifier); 2243 cleanup_rtnl_notifier: 2244 unregister_pernet_subsys(&nfnl_queue_net_ops); 2245 cleanup_pernet_subsys: 2246 destroy_workqueue(nfq_cleanup_wq); 2247 return status; 2248 } 2249 2250 static void __exit nfnetlink_queue_fini(void) 2251 { 2252 nf_unregister_queue_handler(); 2253 unregister_netdevice_notifier(&nfqnl_dev_notifier); 2254 nfnetlink_subsys_unregister(&nfqnl_subsys); 2255 netlink_unregister_notifier(&nfqnl_rtnl_notifier); 2256 unregister_pernet_subsys(&nfnl_queue_net_ops); 2257 destroy_workqueue(nfq_cleanup_wq); 2258 rcu_barrier(); /* Wait for completion of call_rcu()'s */ 2259 } 2260 2261 MODULE_DESCRIPTION("netfilter packet queue handler"); 2262 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); 2263 MODULE_LICENSE("GPL"); 2264 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE); 2265 2266 module_init(nfnetlink_queue_init); 2267 module_exit(nfnetlink_queue_fini); 2268