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/list.h> 32 #include <net/sock.h> 33 #include <net/tcp_states.h> 34 #include <net/netfilter/nf_queue.h> 35 #include <net/netns/generic.h> 36 37 #include <linux/atomic.h> 38 39 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 40 #include "../bridge/br_private.h" 41 #endif 42 43 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 44 #include <net/netfilter/nf_conntrack.h> 45 #endif 46 47 #define NFQNL_QMAX_DEFAULT 1024 48 49 /* We're using struct nlattr which has 16bit nla_len. Note that nla_len 50 * includes the header length. Thus, the maximum packet length that we 51 * support is 65531 bytes. We send truncated packets if the specified length 52 * is larger than that. Userspace can check for presence of NFQA_CAP_LEN 53 * attribute to detect truncation. 54 */ 55 #define NFQNL_MAX_COPY_RANGE (0xffff - NLA_HDRLEN) 56 57 struct nfqnl_instance { 58 struct hlist_node hlist; /* global list of queues */ 59 struct rcu_head rcu; 60 61 u32 peer_portid; 62 unsigned int queue_maxlen; 63 unsigned int copy_range; 64 unsigned int queue_dropped; 65 unsigned int queue_user_dropped; 66 67 68 u_int16_t queue_num; /* number of this queue */ 69 u_int8_t copy_mode; 70 u_int32_t flags; /* Set using NFQA_CFG_FLAGS */ 71 /* 72 * Following fields are dirtied for each queued packet, 73 * keep them in same cache line if possible. 74 */ 75 spinlock_t lock ____cacheline_aligned_in_smp; 76 unsigned int queue_total; 77 unsigned int id_sequence; /* 'sequence' of pkt ids */ 78 struct list_head queue_list; /* packets in queue */ 79 }; 80 81 typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long); 82 83 static unsigned int nfnl_queue_net_id __read_mostly; 84 85 #define INSTANCE_BUCKETS 16 86 struct nfnl_queue_net { 87 spinlock_t instances_lock; 88 struct hlist_head instance_table[INSTANCE_BUCKETS]; 89 }; 90 91 static struct nfnl_queue_net *nfnl_queue_pernet(struct net *net) 92 { 93 return net_generic(net, nfnl_queue_net_id); 94 } 95 96 static inline u_int8_t instance_hashfn(u_int16_t queue_num) 97 { 98 return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS; 99 } 100 101 static struct nfqnl_instance * 102 instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num) 103 { 104 struct hlist_head *head; 105 struct nfqnl_instance *inst; 106 107 head = &q->instance_table[instance_hashfn(queue_num)]; 108 hlist_for_each_entry_rcu(inst, head, hlist) { 109 if (inst->queue_num == queue_num) 110 return inst; 111 } 112 return NULL; 113 } 114 115 static struct nfqnl_instance * 116 instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 portid) 117 { 118 struct nfqnl_instance *inst; 119 unsigned int h; 120 int err; 121 122 spin_lock(&q->instances_lock); 123 if (instance_lookup(q, queue_num)) { 124 err = -EEXIST; 125 goto out_unlock; 126 } 127 128 inst = kzalloc(sizeof(*inst), GFP_ATOMIC); 129 if (!inst) { 130 err = -ENOMEM; 131 goto out_unlock; 132 } 133 134 inst->queue_num = queue_num; 135 inst->peer_portid = portid; 136 inst->queue_maxlen = NFQNL_QMAX_DEFAULT; 137 inst->copy_range = NFQNL_MAX_COPY_RANGE; 138 inst->copy_mode = NFQNL_COPY_NONE; 139 spin_lock_init(&inst->lock); 140 INIT_LIST_HEAD(&inst->queue_list); 141 142 if (!try_module_get(THIS_MODULE)) { 143 err = -EAGAIN; 144 goto out_free; 145 } 146 147 h = instance_hashfn(queue_num); 148 hlist_add_head_rcu(&inst->hlist, &q->instance_table[h]); 149 150 spin_unlock(&q->instances_lock); 151 152 return inst; 153 154 out_free: 155 kfree(inst); 156 out_unlock: 157 spin_unlock(&q->instances_lock); 158 return ERR_PTR(err); 159 } 160 161 static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, 162 unsigned long data); 163 164 static void 165 instance_destroy_rcu(struct rcu_head *head) 166 { 167 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance, 168 rcu); 169 170 nfqnl_flush(inst, NULL, 0); 171 kfree(inst); 172 module_put(THIS_MODULE); 173 } 174 175 static void 176 __instance_destroy(struct nfqnl_instance *inst) 177 { 178 hlist_del_rcu(&inst->hlist); 179 call_rcu(&inst->rcu, instance_destroy_rcu); 180 } 181 182 static void 183 instance_destroy(struct nfnl_queue_net *q, struct nfqnl_instance *inst) 184 { 185 spin_lock(&q->instances_lock); 186 __instance_destroy(inst); 187 spin_unlock(&q->instances_lock); 188 } 189 190 static inline void 191 __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry) 192 { 193 list_add_tail(&entry->list, &queue->queue_list); 194 queue->queue_total++; 195 } 196 197 static void 198 __dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry) 199 { 200 list_del(&entry->list); 201 queue->queue_total--; 202 } 203 204 static struct nf_queue_entry * 205 find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id) 206 { 207 struct nf_queue_entry *entry = NULL, *i; 208 209 spin_lock_bh(&queue->lock); 210 211 list_for_each_entry(i, &queue->queue_list, list) { 212 if (i->id == id) { 213 entry = i; 214 break; 215 } 216 } 217 218 if (entry) 219 __dequeue_entry(queue, entry); 220 221 spin_unlock_bh(&queue->lock); 222 223 return entry; 224 } 225 226 static void nfqnl_reinject(struct nf_queue_entry *entry, unsigned int verdict) 227 { 228 const struct nf_ct_hook *ct_hook; 229 int err; 230 231 if (verdict == NF_ACCEPT || 232 verdict == NF_REPEAT || 233 verdict == NF_STOP) { 234 rcu_read_lock(); 235 ct_hook = rcu_dereference(nf_ct_hook); 236 if (ct_hook) { 237 err = ct_hook->update(entry->state.net, entry->skb); 238 if (err < 0) 239 verdict = NF_DROP; 240 } 241 rcu_read_unlock(); 242 } 243 nf_reinject(entry, verdict); 244 } 245 246 static void 247 nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data) 248 { 249 struct nf_queue_entry *entry, *next; 250 251 spin_lock_bh(&queue->lock); 252 list_for_each_entry_safe(entry, next, &queue->queue_list, list) { 253 if (!cmpfn || cmpfn(entry, data)) { 254 list_del(&entry->list); 255 queue->queue_total--; 256 nfqnl_reinject(entry, NF_DROP); 257 } 258 } 259 spin_unlock_bh(&queue->lock); 260 } 261 262 static int 263 nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet, 264 bool csum_verify) 265 { 266 __u32 flags = 0; 267 268 if (packet->ip_summed == CHECKSUM_PARTIAL) 269 flags = NFQA_SKB_CSUMNOTREADY; 270 else if (csum_verify) 271 flags = NFQA_SKB_CSUM_NOTVERIFIED; 272 273 if (skb_is_gso(packet)) 274 flags |= NFQA_SKB_GSO; 275 276 return flags ? nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags)) : 0; 277 } 278 279 static int nfqnl_put_sk_uidgid(struct sk_buff *skb, struct sock *sk) 280 { 281 const struct cred *cred; 282 283 if (!sk_fullsock(sk)) 284 return 0; 285 286 read_lock_bh(&sk->sk_callback_lock); 287 if (sk->sk_socket && sk->sk_socket->file) { 288 cred = sk->sk_socket->file->f_cred; 289 if (nla_put_be32(skb, NFQA_UID, 290 htonl(from_kuid_munged(&init_user_ns, cred->fsuid)))) 291 goto nla_put_failure; 292 if (nla_put_be32(skb, NFQA_GID, 293 htonl(from_kgid_munged(&init_user_ns, cred->fsgid)))) 294 goto nla_put_failure; 295 } 296 read_unlock_bh(&sk->sk_callback_lock); 297 return 0; 298 299 nla_put_failure: 300 read_unlock_bh(&sk->sk_callback_lock); 301 return -1; 302 } 303 304 static u32 nfqnl_get_sk_secctx(struct sk_buff *skb, char **secdata) 305 { 306 u32 seclen = 0; 307 #if IS_ENABLED(CONFIG_NETWORK_SECMARK) 308 if (!skb || !sk_fullsock(skb->sk)) 309 return 0; 310 311 read_lock_bh(&skb->sk->sk_callback_lock); 312 313 if (skb->secmark) 314 security_secid_to_secctx(skb->secmark, secdata, &seclen); 315 316 read_unlock_bh(&skb->sk->sk_callback_lock); 317 #endif 318 return seclen; 319 } 320 321 static u32 nfqnl_get_bridge_size(struct nf_queue_entry *entry) 322 { 323 struct sk_buff *entskb = entry->skb; 324 u32 nlalen = 0; 325 326 if (entry->state.pf != PF_BRIDGE || !skb_mac_header_was_set(entskb)) 327 return 0; 328 329 if (skb_vlan_tag_present(entskb)) 330 nlalen += nla_total_size(nla_total_size(sizeof(__be16)) + 331 nla_total_size(sizeof(__be16))); 332 333 if (entskb->network_header > entskb->mac_header) 334 nlalen += nla_total_size((entskb->network_header - 335 entskb->mac_header)); 336 337 return nlalen; 338 } 339 340 static int nfqnl_put_bridge(struct nf_queue_entry *entry, struct sk_buff *skb) 341 { 342 struct sk_buff *entskb = entry->skb; 343 344 if (entry->state.pf != PF_BRIDGE || !skb_mac_header_was_set(entskb)) 345 return 0; 346 347 if (skb_vlan_tag_present(entskb)) { 348 struct nlattr *nest; 349 350 nest = nla_nest_start(skb, NFQA_VLAN); 351 if (!nest) 352 goto nla_put_failure; 353 354 if (nla_put_be16(skb, NFQA_VLAN_TCI, htons(entskb->vlan_tci)) || 355 nla_put_be16(skb, NFQA_VLAN_PROTO, entskb->vlan_proto)) 356 goto nla_put_failure; 357 358 nla_nest_end(skb, nest); 359 } 360 361 if (entskb->mac_header < entskb->network_header) { 362 int len = (int)(entskb->network_header - entskb->mac_header); 363 364 if (nla_put(skb, NFQA_L2HDR, len, skb_mac_header(entskb))) 365 goto nla_put_failure; 366 } 367 368 return 0; 369 370 nla_put_failure: 371 return -1; 372 } 373 374 static struct sk_buff * 375 nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue, 376 struct nf_queue_entry *entry, 377 __be32 **packet_id_ptr) 378 { 379 size_t size; 380 size_t data_len = 0, cap_len = 0; 381 unsigned int hlen = 0; 382 struct sk_buff *skb; 383 struct nlattr *nla; 384 struct nfqnl_msg_packet_hdr *pmsg; 385 struct nlmsghdr *nlh; 386 struct sk_buff *entskb = entry->skb; 387 struct net_device *indev; 388 struct net_device *outdev; 389 struct nf_conn *ct = NULL; 390 enum ip_conntrack_info ctinfo = 0; 391 const struct nfnl_ct_hook *nfnl_ct; 392 bool csum_verify; 393 char *secdata = NULL; 394 u32 seclen = 0; 395 396 size = nlmsg_total_size(sizeof(struct nfgenmsg)) 397 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr)) 398 + nla_total_size(sizeof(u_int32_t)) /* ifindex */ 399 + nla_total_size(sizeof(u_int32_t)) /* ifindex */ 400 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 401 + nla_total_size(sizeof(u_int32_t)) /* ifindex */ 402 + nla_total_size(sizeof(u_int32_t)) /* ifindex */ 403 #endif 404 + nla_total_size(sizeof(u_int32_t)) /* mark */ 405 + nla_total_size(sizeof(u_int32_t)) /* priority */ 406 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw)) 407 + nla_total_size(sizeof(u_int32_t)) /* skbinfo */ 408 + nla_total_size(sizeof(u_int32_t)); /* cap_len */ 409 410 if (entskb->tstamp) 411 size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp)); 412 413 size += nfqnl_get_bridge_size(entry); 414 415 if (entry->state.hook <= NF_INET_FORWARD || 416 (entry->state.hook == NF_INET_POST_ROUTING && entskb->sk == NULL)) 417 csum_verify = !skb_csum_unnecessary(entskb); 418 else 419 csum_verify = false; 420 421 outdev = entry->state.out; 422 423 switch ((enum nfqnl_config_mode)READ_ONCE(queue->copy_mode)) { 424 case NFQNL_COPY_META: 425 case NFQNL_COPY_NONE: 426 break; 427 428 case NFQNL_COPY_PACKET: 429 if (!(queue->flags & NFQA_CFG_F_GSO) && 430 entskb->ip_summed == CHECKSUM_PARTIAL && 431 skb_checksum_help(entskb)) 432 return NULL; 433 434 data_len = READ_ONCE(queue->copy_range); 435 if (data_len > entskb->len) 436 data_len = entskb->len; 437 438 hlen = skb_zerocopy_headlen(entskb); 439 hlen = min_t(unsigned int, hlen, data_len); 440 size += sizeof(struct nlattr) + hlen; 441 cap_len = entskb->len; 442 break; 443 } 444 445 nfnl_ct = rcu_dereference(nfnl_ct_hook); 446 447 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 448 if (queue->flags & NFQA_CFG_F_CONNTRACK) { 449 if (nfnl_ct != NULL) { 450 ct = nf_ct_get(entskb, &ctinfo); 451 if (ct != NULL) 452 size += nfnl_ct->build_size(ct); 453 } 454 } 455 #endif 456 457 if (queue->flags & NFQA_CFG_F_UID_GID) { 458 size += (nla_total_size(sizeof(u_int32_t)) /* uid */ 459 + nla_total_size(sizeof(u_int32_t))); /* gid */ 460 } 461 462 if ((queue->flags & NFQA_CFG_F_SECCTX) && entskb->sk) { 463 seclen = nfqnl_get_sk_secctx(entskb, &secdata); 464 if (seclen) 465 size += nla_total_size(seclen); 466 } 467 468 skb = alloc_skb(size, GFP_ATOMIC); 469 if (!skb) { 470 skb_tx_error(entskb); 471 goto nlmsg_failure; 472 } 473 474 nlh = nfnl_msg_put(skb, 0, 0, 475 nfnl_msg_type(NFNL_SUBSYS_QUEUE, NFQNL_MSG_PACKET), 476 0, entry->state.pf, NFNETLINK_V0, 477 htons(queue->queue_num)); 478 if (!nlh) { 479 skb_tx_error(entskb); 480 kfree_skb(skb); 481 goto nlmsg_failure; 482 } 483 484 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg)); 485 pmsg = nla_data(nla); 486 pmsg->hw_protocol = entskb->protocol; 487 pmsg->hook = entry->state.hook; 488 *packet_id_ptr = &pmsg->packet_id; 489 490 indev = entry->state.in; 491 if (indev) { 492 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 493 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex))) 494 goto nla_put_failure; 495 #else 496 if (entry->state.pf == PF_BRIDGE) { 497 /* Case 1: indev is physical input device, we need to 498 * look for bridge group (when called from 499 * netfilter_bridge) */ 500 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV, 501 htonl(indev->ifindex)) || 502 /* this is the bridge group "brX" */ 503 /* rcu_read_lock()ed by __nf_queue */ 504 nla_put_be32(skb, NFQA_IFINDEX_INDEV, 505 htonl(br_port_get_rcu(indev)->br->dev->ifindex))) 506 goto nla_put_failure; 507 } else { 508 int physinif; 509 510 /* Case 2: indev is bridge group, we need to look for 511 * physical device (when called from ipv4) */ 512 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, 513 htonl(indev->ifindex))) 514 goto nla_put_failure; 515 516 physinif = nf_bridge_get_physinif(entskb); 517 if (physinif && 518 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV, 519 htonl(physinif))) 520 goto nla_put_failure; 521 } 522 #endif 523 } 524 525 if (outdev) { 526 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 527 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex))) 528 goto nla_put_failure; 529 #else 530 if (entry->state.pf == PF_BRIDGE) { 531 /* Case 1: outdev is physical output device, we need to 532 * look for bridge group (when called from 533 * netfilter_bridge) */ 534 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV, 535 htonl(outdev->ifindex)) || 536 /* this is the bridge group "brX" */ 537 /* rcu_read_lock()ed by __nf_queue */ 538 nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, 539 htonl(br_port_get_rcu(outdev)->br->dev->ifindex))) 540 goto nla_put_failure; 541 } else { 542 int physoutif; 543 544 /* Case 2: outdev is bridge group, we need to look for 545 * physical output device (when called from ipv4) */ 546 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, 547 htonl(outdev->ifindex))) 548 goto nla_put_failure; 549 550 physoutif = nf_bridge_get_physoutif(entskb); 551 if (physoutif && 552 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV, 553 htonl(physoutif))) 554 goto nla_put_failure; 555 } 556 #endif 557 } 558 559 if (entskb->mark && 560 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark))) 561 goto nla_put_failure; 562 563 if (entskb->priority && 564 nla_put_be32(skb, NFQA_PRIORITY, htonl(entskb->priority))) 565 goto nla_put_failure; 566 567 if (indev && entskb->dev && 568 skb_mac_header_was_set(entskb) && 569 skb_mac_header_len(entskb) != 0) { 570 struct nfqnl_msg_packet_hw phw; 571 int len; 572 573 memset(&phw, 0, sizeof(phw)); 574 len = dev_parse_header(entskb, phw.hw_addr); 575 if (len) { 576 phw.hw_addrlen = htons(len); 577 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw)) 578 goto nla_put_failure; 579 } 580 } 581 582 if (nfqnl_put_bridge(entry, skb) < 0) 583 goto nla_put_failure; 584 585 if (entry->state.hook <= NF_INET_FORWARD && entskb->tstamp) { 586 struct nfqnl_msg_packet_timestamp ts; 587 struct timespec64 kts = ktime_to_timespec64(entskb->tstamp); 588 589 ts.sec = cpu_to_be64(kts.tv_sec); 590 ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC); 591 592 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts)) 593 goto nla_put_failure; 594 } 595 596 if ((queue->flags & NFQA_CFG_F_UID_GID) && entskb->sk && 597 nfqnl_put_sk_uidgid(skb, entskb->sk) < 0) 598 goto nla_put_failure; 599 600 if (seclen && nla_put(skb, NFQA_SECCTX, seclen, secdata)) 601 goto nla_put_failure; 602 603 if (ct && nfnl_ct->build(skb, ct, ctinfo, NFQA_CT, NFQA_CT_INFO) < 0) 604 goto nla_put_failure; 605 606 if (cap_len > data_len && 607 nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len))) 608 goto nla_put_failure; 609 610 if (nfqnl_put_packet_info(skb, entskb, csum_verify)) 611 goto nla_put_failure; 612 613 if (data_len) { 614 struct nlattr *nla; 615 616 if (skb_tailroom(skb) < sizeof(*nla) + hlen) 617 goto nla_put_failure; 618 619 nla = skb_put(skb, sizeof(*nla)); 620 nla->nla_type = NFQA_PAYLOAD; 621 nla->nla_len = nla_attr_size(data_len); 622 623 if (skb_zerocopy(skb, entskb, data_len, hlen)) 624 goto nla_put_failure; 625 } 626 627 nlh->nlmsg_len = skb->len; 628 if (seclen) 629 security_release_secctx(secdata, seclen); 630 return skb; 631 632 nla_put_failure: 633 skb_tx_error(entskb); 634 kfree_skb(skb); 635 net_err_ratelimited("nf_queue: error creating packet message\n"); 636 nlmsg_failure: 637 if (seclen) 638 security_release_secctx(secdata, seclen); 639 return NULL; 640 } 641 642 static bool nf_ct_drop_unconfirmed(const struct nf_queue_entry *entry) 643 { 644 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 645 static const unsigned long flags = IPS_CONFIRMED | IPS_DYING; 646 const struct nf_conn *ct = (void *)skb_nfct(entry->skb); 647 648 if (ct && ((ct->status & flags) == IPS_DYING)) 649 return true; 650 #endif 651 return false; 652 } 653 654 static int 655 __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue, 656 struct nf_queue_entry *entry) 657 { 658 struct sk_buff *nskb; 659 int err = -ENOBUFS; 660 __be32 *packet_id_ptr; 661 int failopen = 0; 662 663 nskb = nfqnl_build_packet_message(net, queue, entry, &packet_id_ptr); 664 if (nskb == NULL) { 665 err = -ENOMEM; 666 goto err_out; 667 } 668 spin_lock_bh(&queue->lock); 669 670 if (nf_ct_drop_unconfirmed(entry)) 671 goto err_out_free_nskb; 672 673 if (queue->queue_total >= queue->queue_maxlen) { 674 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) { 675 failopen = 1; 676 err = 0; 677 } else { 678 queue->queue_dropped++; 679 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n", 680 queue->queue_total); 681 } 682 goto err_out_free_nskb; 683 } 684 entry->id = ++queue->id_sequence; 685 *packet_id_ptr = htonl(entry->id); 686 687 /* nfnetlink_unicast will either free the nskb or add it to a socket */ 688 err = nfnetlink_unicast(nskb, net, queue->peer_portid); 689 if (err < 0) { 690 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) { 691 failopen = 1; 692 err = 0; 693 } else { 694 queue->queue_user_dropped++; 695 } 696 goto err_out_unlock; 697 } 698 699 __enqueue_entry(queue, entry); 700 701 spin_unlock_bh(&queue->lock); 702 return 0; 703 704 err_out_free_nskb: 705 kfree_skb(nskb); 706 err_out_unlock: 707 spin_unlock_bh(&queue->lock); 708 if (failopen) 709 nfqnl_reinject(entry, NF_ACCEPT); 710 err_out: 711 return err; 712 } 713 714 static struct nf_queue_entry * 715 nf_queue_entry_dup(struct nf_queue_entry *e) 716 { 717 struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC); 718 if (entry) 719 nf_queue_entry_get_refs(entry); 720 return entry; 721 } 722 723 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 724 /* When called from bridge netfilter, skb->data must point to MAC header 725 * before calling skb_gso_segment(). Else, original MAC header is lost 726 * and segmented skbs will be sent to wrong destination. 727 */ 728 static void nf_bridge_adjust_skb_data(struct sk_buff *skb) 729 { 730 if (nf_bridge_info_get(skb)) 731 __skb_push(skb, skb->network_header - skb->mac_header); 732 } 733 734 static void nf_bridge_adjust_segmented_data(struct sk_buff *skb) 735 { 736 if (nf_bridge_info_get(skb)) 737 __skb_pull(skb, skb->network_header - skb->mac_header); 738 } 739 #else 740 #define nf_bridge_adjust_skb_data(s) do {} while (0) 741 #define nf_bridge_adjust_segmented_data(s) do {} while (0) 742 #endif 743 744 static int 745 __nfqnl_enqueue_packet_gso(struct net *net, struct nfqnl_instance *queue, 746 struct sk_buff *skb, struct nf_queue_entry *entry) 747 { 748 int ret = -ENOMEM; 749 struct nf_queue_entry *entry_seg; 750 751 nf_bridge_adjust_segmented_data(skb); 752 753 if (skb->next == NULL) { /* last packet, no need to copy entry */ 754 struct sk_buff *gso_skb = entry->skb; 755 entry->skb = skb; 756 ret = __nfqnl_enqueue_packet(net, queue, entry); 757 if (ret) 758 entry->skb = gso_skb; 759 return ret; 760 } 761 762 skb_mark_not_on_list(skb); 763 764 entry_seg = nf_queue_entry_dup(entry); 765 if (entry_seg) { 766 entry_seg->skb = skb; 767 ret = __nfqnl_enqueue_packet(net, queue, entry_seg); 768 if (ret) 769 nf_queue_entry_free(entry_seg); 770 } 771 return ret; 772 } 773 774 static int 775 nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum) 776 { 777 unsigned int queued; 778 struct nfqnl_instance *queue; 779 struct sk_buff *skb, *segs, *nskb; 780 int err = -ENOBUFS; 781 struct net *net = entry->state.net; 782 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 783 784 /* rcu_read_lock()ed by nf_hook_thresh */ 785 queue = instance_lookup(q, queuenum); 786 if (!queue) 787 return -ESRCH; 788 789 if (queue->copy_mode == NFQNL_COPY_NONE) 790 return -EINVAL; 791 792 skb = entry->skb; 793 794 switch (entry->state.pf) { 795 case NFPROTO_IPV4: 796 skb->protocol = htons(ETH_P_IP); 797 break; 798 case NFPROTO_IPV6: 799 skb->protocol = htons(ETH_P_IPV6); 800 break; 801 } 802 803 if ((queue->flags & NFQA_CFG_F_GSO) || !skb_is_gso(skb)) 804 return __nfqnl_enqueue_packet(net, queue, entry); 805 806 nf_bridge_adjust_skb_data(skb); 807 segs = skb_gso_segment(skb, 0); 808 /* Does not use PTR_ERR to limit the number of error codes that can be 809 * returned by nf_queue. For instance, callers rely on -ESRCH to 810 * mean 'ignore this hook'. 811 */ 812 if (IS_ERR_OR_NULL(segs)) 813 goto out_err; 814 queued = 0; 815 err = 0; 816 skb_list_walk_safe(segs, segs, nskb) { 817 if (err == 0) 818 err = __nfqnl_enqueue_packet_gso(net, queue, 819 segs, entry); 820 if (err == 0) 821 queued++; 822 else 823 kfree_skb(segs); 824 } 825 826 if (queued) { 827 if (err) /* some segments are already queued */ 828 nf_queue_entry_free(entry); 829 kfree_skb(skb); 830 return 0; 831 } 832 out_err: 833 nf_bridge_adjust_segmented_data(skb); 834 return err; 835 } 836 837 static int 838 nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff) 839 { 840 struct sk_buff *nskb; 841 842 if (diff < 0) { 843 if (pskb_trim(e->skb, data_len)) 844 return -ENOMEM; 845 } else if (diff > 0) { 846 if (data_len > 0xFFFF) 847 return -EINVAL; 848 if (diff > skb_tailroom(e->skb)) { 849 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb), 850 diff, GFP_ATOMIC); 851 if (!nskb) 852 return -ENOMEM; 853 kfree_skb(e->skb); 854 e->skb = nskb; 855 } 856 skb_put(e->skb, diff); 857 } 858 if (skb_ensure_writable(e->skb, data_len)) 859 return -ENOMEM; 860 skb_copy_to_linear_data(e->skb, data, data_len); 861 e->skb->ip_summed = CHECKSUM_NONE; 862 return 0; 863 } 864 865 static int 866 nfqnl_set_mode(struct nfqnl_instance *queue, 867 unsigned char mode, unsigned int range) 868 { 869 int status = 0; 870 871 spin_lock_bh(&queue->lock); 872 switch (mode) { 873 case NFQNL_COPY_NONE: 874 case NFQNL_COPY_META: 875 queue->copy_mode = mode; 876 queue->copy_range = 0; 877 break; 878 879 case NFQNL_COPY_PACKET: 880 queue->copy_mode = mode; 881 if (range == 0 || range > NFQNL_MAX_COPY_RANGE) 882 queue->copy_range = NFQNL_MAX_COPY_RANGE; 883 else 884 queue->copy_range = range; 885 break; 886 887 default: 888 status = -EINVAL; 889 890 } 891 spin_unlock_bh(&queue->lock); 892 893 return status; 894 } 895 896 static int 897 dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex) 898 { 899 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 900 int physinif, physoutif; 901 902 physinif = nf_bridge_get_physinif(entry->skb); 903 physoutif = nf_bridge_get_physoutif(entry->skb); 904 905 if (physinif == ifindex || physoutif == ifindex) 906 return 1; 907 #endif 908 if (entry->state.in) 909 if (entry->state.in->ifindex == ifindex) 910 return 1; 911 if (entry->state.out) 912 if (entry->state.out->ifindex == ifindex) 913 return 1; 914 915 return 0; 916 } 917 918 /* drop all packets with either indev or outdev == ifindex from all queue 919 * instances */ 920 static void 921 nfqnl_dev_drop(struct net *net, int ifindex) 922 { 923 int i; 924 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 925 926 rcu_read_lock(); 927 928 for (i = 0; i < INSTANCE_BUCKETS; i++) { 929 struct nfqnl_instance *inst; 930 struct hlist_head *head = &q->instance_table[i]; 931 932 hlist_for_each_entry_rcu(inst, head, hlist) 933 nfqnl_flush(inst, dev_cmp, ifindex); 934 } 935 936 rcu_read_unlock(); 937 } 938 939 static int 940 nfqnl_rcv_dev_event(struct notifier_block *this, 941 unsigned long event, void *ptr) 942 { 943 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 944 945 /* Drop any packets associated with the downed device */ 946 if (event == NETDEV_DOWN) 947 nfqnl_dev_drop(dev_net(dev), dev->ifindex); 948 return NOTIFY_DONE; 949 } 950 951 static struct notifier_block nfqnl_dev_notifier = { 952 .notifier_call = nfqnl_rcv_dev_event, 953 }; 954 955 static void nfqnl_nf_hook_drop(struct net *net) 956 { 957 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 958 int i; 959 960 /* This function is also called on net namespace error unwind, 961 * when pernet_ops->init() failed and ->exit() functions of the 962 * previous pernet_ops gets called. 963 * 964 * This may result in a call to nfqnl_nf_hook_drop() before 965 * struct nfnl_queue_net was allocated. 966 */ 967 if (!q) 968 return; 969 970 for (i = 0; i < INSTANCE_BUCKETS; i++) { 971 struct nfqnl_instance *inst; 972 struct hlist_head *head = &q->instance_table[i]; 973 974 hlist_for_each_entry_rcu(inst, head, hlist) 975 nfqnl_flush(inst, NULL, 0); 976 } 977 } 978 979 static int 980 nfqnl_rcv_nl_event(struct notifier_block *this, 981 unsigned long event, void *ptr) 982 { 983 struct netlink_notify *n = ptr; 984 struct nfnl_queue_net *q = nfnl_queue_pernet(n->net); 985 986 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) { 987 int i; 988 989 /* destroy all instances for this portid */ 990 spin_lock(&q->instances_lock); 991 for (i = 0; i < INSTANCE_BUCKETS; i++) { 992 struct hlist_node *t2; 993 struct nfqnl_instance *inst; 994 struct hlist_head *head = &q->instance_table[i]; 995 996 hlist_for_each_entry_safe(inst, t2, head, hlist) { 997 if (n->portid == inst->peer_portid) 998 __instance_destroy(inst); 999 } 1000 } 1001 spin_unlock(&q->instances_lock); 1002 } 1003 return NOTIFY_DONE; 1004 } 1005 1006 static struct notifier_block nfqnl_rtnl_notifier = { 1007 .notifier_call = nfqnl_rcv_nl_event, 1008 }; 1009 1010 static const struct nla_policy nfqa_vlan_policy[NFQA_VLAN_MAX + 1] = { 1011 [NFQA_VLAN_TCI] = { .type = NLA_U16}, 1012 [NFQA_VLAN_PROTO] = { .type = NLA_U16}, 1013 }; 1014 1015 static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = { 1016 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, 1017 [NFQA_MARK] = { .type = NLA_U32 }, 1018 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC }, 1019 [NFQA_CT] = { .type = NLA_UNSPEC }, 1020 [NFQA_EXP] = { .type = NLA_UNSPEC }, 1021 [NFQA_VLAN] = { .type = NLA_NESTED }, 1022 }; 1023 1024 static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = { 1025 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, 1026 [NFQA_MARK] = { .type = NLA_U32 }, 1027 }; 1028 1029 static struct nfqnl_instance * 1030 verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, u32 nlportid) 1031 { 1032 struct nfqnl_instance *queue; 1033 1034 queue = instance_lookup(q, queue_num); 1035 if (!queue) 1036 return ERR_PTR(-ENODEV); 1037 1038 if (queue->peer_portid != nlportid) 1039 return ERR_PTR(-EPERM); 1040 1041 return queue; 1042 } 1043 1044 static struct nfqnl_msg_verdict_hdr* 1045 verdicthdr_get(const struct nlattr * const nfqa[]) 1046 { 1047 struct nfqnl_msg_verdict_hdr *vhdr; 1048 unsigned int verdict; 1049 1050 if (!nfqa[NFQA_VERDICT_HDR]) 1051 return NULL; 1052 1053 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]); 1054 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK; 1055 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN) 1056 return NULL; 1057 return vhdr; 1058 } 1059 1060 static int nfq_id_after(unsigned int id, unsigned int max) 1061 { 1062 return (int)(id - max) > 0; 1063 } 1064 1065 static int nfqnl_recv_verdict_batch(struct sk_buff *skb, 1066 const struct nfnl_info *info, 1067 const struct nlattr * const nfqa[]) 1068 { 1069 struct nfnl_queue_net *q = nfnl_queue_pernet(info->net); 1070 u16 queue_num = ntohs(info->nfmsg->res_id); 1071 struct nf_queue_entry *entry, *tmp; 1072 struct nfqnl_msg_verdict_hdr *vhdr; 1073 struct nfqnl_instance *queue; 1074 unsigned int verdict, maxid; 1075 LIST_HEAD(batch_list); 1076 1077 queue = verdict_instance_lookup(q, queue_num, 1078 NETLINK_CB(skb).portid); 1079 if (IS_ERR(queue)) 1080 return PTR_ERR(queue); 1081 1082 vhdr = verdicthdr_get(nfqa); 1083 if (!vhdr) 1084 return -EINVAL; 1085 1086 verdict = ntohl(vhdr->verdict); 1087 maxid = ntohl(vhdr->id); 1088 1089 spin_lock_bh(&queue->lock); 1090 1091 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) { 1092 if (nfq_id_after(entry->id, maxid)) 1093 break; 1094 __dequeue_entry(queue, entry); 1095 list_add_tail(&entry->list, &batch_list); 1096 } 1097 1098 spin_unlock_bh(&queue->lock); 1099 1100 if (list_empty(&batch_list)) 1101 return -ENOENT; 1102 1103 list_for_each_entry_safe(entry, tmp, &batch_list, list) { 1104 if (nfqa[NFQA_MARK]) 1105 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK])); 1106 1107 nfqnl_reinject(entry, verdict); 1108 } 1109 return 0; 1110 } 1111 1112 static struct nf_conn *nfqnl_ct_parse(const struct nfnl_ct_hook *nfnl_ct, 1113 const struct nlmsghdr *nlh, 1114 const struct nlattr * const nfqa[], 1115 struct nf_queue_entry *entry, 1116 enum ip_conntrack_info *ctinfo) 1117 { 1118 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 1119 struct nf_conn *ct; 1120 1121 ct = nf_ct_get(entry->skb, ctinfo); 1122 if (ct == NULL) 1123 return NULL; 1124 1125 if (nfnl_ct->parse(nfqa[NFQA_CT], ct) < 0) 1126 return NULL; 1127 1128 if (nfqa[NFQA_EXP]) 1129 nfnl_ct->attach_expect(nfqa[NFQA_EXP], ct, 1130 NETLINK_CB(entry->skb).portid, 1131 nlmsg_report(nlh)); 1132 return ct; 1133 #else 1134 return NULL; 1135 #endif 1136 } 1137 1138 static int nfqa_parse_bridge(struct nf_queue_entry *entry, 1139 const struct nlattr * const nfqa[]) 1140 { 1141 if (nfqa[NFQA_VLAN]) { 1142 struct nlattr *tb[NFQA_VLAN_MAX + 1]; 1143 int err; 1144 1145 err = nla_parse_nested_deprecated(tb, NFQA_VLAN_MAX, 1146 nfqa[NFQA_VLAN], 1147 nfqa_vlan_policy, NULL); 1148 if (err < 0) 1149 return err; 1150 1151 if (!tb[NFQA_VLAN_TCI] || !tb[NFQA_VLAN_PROTO]) 1152 return -EINVAL; 1153 1154 __vlan_hwaccel_put_tag(entry->skb, 1155 nla_get_be16(tb[NFQA_VLAN_PROTO]), 1156 ntohs(nla_get_be16(tb[NFQA_VLAN_TCI]))); 1157 } 1158 1159 if (nfqa[NFQA_L2HDR]) { 1160 int mac_header_len = entry->skb->network_header - 1161 entry->skb->mac_header; 1162 1163 if (mac_header_len != nla_len(nfqa[NFQA_L2HDR])) 1164 return -EINVAL; 1165 else if (mac_header_len > 0) 1166 memcpy(skb_mac_header(entry->skb), 1167 nla_data(nfqa[NFQA_L2HDR]), 1168 mac_header_len); 1169 } 1170 1171 return 0; 1172 } 1173 1174 static int nfqnl_recv_verdict(struct sk_buff *skb, const struct nfnl_info *info, 1175 const struct nlattr * const nfqa[]) 1176 { 1177 struct nfnl_queue_net *q = nfnl_queue_pernet(info->net); 1178 u_int16_t queue_num = ntohs(info->nfmsg->res_id); 1179 const struct nfnl_ct_hook *nfnl_ct; 1180 struct nfqnl_msg_verdict_hdr *vhdr; 1181 enum ip_conntrack_info ctinfo; 1182 struct nfqnl_instance *queue; 1183 struct nf_queue_entry *entry; 1184 struct nf_conn *ct = NULL; 1185 unsigned int verdict; 1186 int err; 1187 1188 queue = verdict_instance_lookup(q, queue_num, 1189 NETLINK_CB(skb).portid); 1190 if (IS_ERR(queue)) 1191 return PTR_ERR(queue); 1192 1193 vhdr = verdicthdr_get(nfqa); 1194 if (!vhdr) 1195 return -EINVAL; 1196 1197 verdict = ntohl(vhdr->verdict); 1198 1199 entry = find_dequeue_entry(queue, ntohl(vhdr->id)); 1200 if (entry == NULL) 1201 return -ENOENT; 1202 1203 /* rcu lock already held from nfnl->call_rcu. */ 1204 nfnl_ct = rcu_dereference(nfnl_ct_hook); 1205 1206 if (nfqa[NFQA_CT]) { 1207 if (nfnl_ct != NULL) 1208 ct = nfqnl_ct_parse(nfnl_ct, info->nlh, nfqa, entry, 1209 &ctinfo); 1210 } 1211 1212 if (entry->state.pf == PF_BRIDGE) { 1213 err = nfqa_parse_bridge(entry, nfqa); 1214 if (err < 0) 1215 return err; 1216 } 1217 1218 if (nfqa[NFQA_PAYLOAD]) { 1219 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]); 1220 int diff = payload_len - entry->skb->len; 1221 1222 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]), 1223 payload_len, entry, diff) < 0) 1224 verdict = NF_DROP; 1225 1226 if (ct && diff) 1227 nfnl_ct->seq_adjust(entry->skb, ct, ctinfo, diff); 1228 } 1229 1230 if (nfqa[NFQA_MARK]) 1231 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK])); 1232 1233 nfqnl_reinject(entry, verdict); 1234 return 0; 1235 } 1236 1237 static int nfqnl_recv_unsupp(struct sk_buff *skb, const struct nfnl_info *info, 1238 const struct nlattr * const cda[]) 1239 { 1240 return -ENOTSUPP; 1241 } 1242 1243 static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = { 1244 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) }, 1245 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) }, 1246 [NFQA_CFG_QUEUE_MAXLEN] = { .type = NLA_U32 }, 1247 [NFQA_CFG_MASK] = { .type = NLA_U32 }, 1248 [NFQA_CFG_FLAGS] = { .type = NLA_U32 }, 1249 }; 1250 1251 static const struct nf_queue_handler nfqh = { 1252 .outfn = nfqnl_enqueue_packet, 1253 .nf_hook_drop = nfqnl_nf_hook_drop, 1254 }; 1255 1256 static int nfqnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info, 1257 const struct nlattr * const nfqa[]) 1258 { 1259 struct nfnl_queue_net *q = nfnl_queue_pernet(info->net); 1260 u_int16_t queue_num = ntohs(info->nfmsg->res_id); 1261 struct nfqnl_msg_config_cmd *cmd = NULL; 1262 struct nfqnl_instance *queue; 1263 __u32 flags = 0, mask = 0; 1264 int ret = 0; 1265 1266 if (nfqa[NFQA_CFG_CMD]) { 1267 cmd = nla_data(nfqa[NFQA_CFG_CMD]); 1268 1269 /* Obsolete commands without queue context */ 1270 switch (cmd->command) { 1271 case NFQNL_CFG_CMD_PF_BIND: return 0; 1272 case NFQNL_CFG_CMD_PF_UNBIND: return 0; 1273 } 1274 } 1275 1276 /* Check if we support these flags in first place, dependencies should 1277 * be there too not to break atomicity. 1278 */ 1279 if (nfqa[NFQA_CFG_FLAGS]) { 1280 if (!nfqa[NFQA_CFG_MASK]) { 1281 /* A mask is needed to specify which flags are being 1282 * changed. 1283 */ 1284 return -EINVAL; 1285 } 1286 1287 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS])); 1288 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK])); 1289 1290 if (flags >= NFQA_CFG_F_MAX) 1291 return -EOPNOTSUPP; 1292 1293 #if !IS_ENABLED(CONFIG_NETWORK_SECMARK) 1294 if (flags & mask & NFQA_CFG_F_SECCTX) 1295 return -EOPNOTSUPP; 1296 #endif 1297 if ((flags & mask & NFQA_CFG_F_CONNTRACK) && 1298 !rcu_access_pointer(nfnl_ct_hook)) { 1299 #ifdef CONFIG_MODULES 1300 nfnl_unlock(NFNL_SUBSYS_QUEUE); 1301 request_module("ip_conntrack_netlink"); 1302 nfnl_lock(NFNL_SUBSYS_QUEUE); 1303 if (rcu_access_pointer(nfnl_ct_hook)) 1304 return -EAGAIN; 1305 #endif 1306 return -EOPNOTSUPP; 1307 } 1308 } 1309 1310 rcu_read_lock(); 1311 queue = instance_lookup(q, queue_num); 1312 if (queue && queue->peer_portid != NETLINK_CB(skb).portid) { 1313 ret = -EPERM; 1314 goto err_out_unlock; 1315 } 1316 1317 if (cmd != NULL) { 1318 switch (cmd->command) { 1319 case NFQNL_CFG_CMD_BIND: 1320 if (queue) { 1321 ret = -EBUSY; 1322 goto err_out_unlock; 1323 } 1324 queue = instance_create(q, queue_num, 1325 NETLINK_CB(skb).portid); 1326 if (IS_ERR(queue)) { 1327 ret = PTR_ERR(queue); 1328 goto err_out_unlock; 1329 } 1330 break; 1331 case NFQNL_CFG_CMD_UNBIND: 1332 if (!queue) { 1333 ret = -ENODEV; 1334 goto err_out_unlock; 1335 } 1336 instance_destroy(q, queue); 1337 goto err_out_unlock; 1338 case NFQNL_CFG_CMD_PF_BIND: 1339 case NFQNL_CFG_CMD_PF_UNBIND: 1340 break; 1341 default: 1342 ret = -ENOTSUPP; 1343 goto err_out_unlock; 1344 } 1345 } 1346 1347 if (!queue) { 1348 ret = -ENODEV; 1349 goto err_out_unlock; 1350 } 1351 1352 if (nfqa[NFQA_CFG_PARAMS]) { 1353 struct nfqnl_msg_config_params *params = 1354 nla_data(nfqa[NFQA_CFG_PARAMS]); 1355 1356 nfqnl_set_mode(queue, params->copy_mode, 1357 ntohl(params->copy_range)); 1358 } 1359 1360 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) { 1361 __be32 *queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]); 1362 1363 spin_lock_bh(&queue->lock); 1364 queue->queue_maxlen = ntohl(*queue_maxlen); 1365 spin_unlock_bh(&queue->lock); 1366 } 1367 1368 if (nfqa[NFQA_CFG_FLAGS]) { 1369 spin_lock_bh(&queue->lock); 1370 queue->flags &= ~mask; 1371 queue->flags |= flags & mask; 1372 spin_unlock_bh(&queue->lock); 1373 } 1374 1375 err_out_unlock: 1376 rcu_read_unlock(); 1377 return ret; 1378 } 1379 1380 static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = { 1381 [NFQNL_MSG_PACKET] = { 1382 .call = nfqnl_recv_unsupp, 1383 .type = NFNL_CB_RCU, 1384 .attr_count = NFQA_MAX, 1385 }, 1386 [NFQNL_MSG_VERDICT] = { 1387 .call = nfqnl_recv_verdict, 1388 .type = NFNL_CB_RCU, 1389 .attr_count = NFQA_MAX, 1390 .policy = nfqa_verdict_policy 1391 }, 1392 [NFQNL_MSG_CONFIG] = { 1393 .call = nfqnl_recv_config, 1394 .type = NFNL_CB_MUTEX, 1395 .attr_count = NFQA_CFG_MAX, 1396 .policy = nfqa_cfg_policy 1397 }, 1398 [NFQNL_MSG_VERDICT_BATCH] = { 1399 .call = nfqnl_recv_verdict_batch, 1400 .type = NFNL_CB_RCU, 1401 .attr_count = NFQA_MAX, 1402 .policy = nfqa_verdict_batch_policy 1403 }, 1404 }; 1405 1406 static const struct nfnetlink_subsystem nfqnl_subsys = { 1407 .name = "nf_queue", 1408 .subsys_id = NFNL_SUBSYS_QUEUE, 1409 .cb_count = NFQNL_MSG_MAX, 1410 .cb = nfqnl_cb, 1411 }; 1412 1413 #ifdef CONFIG_PROC_FS 1414 struct iter_state { 1415 struct seq_net_private p; 1416 unsigned int bucket; 1417 }; 1418 1419 static struct hlist_node *get_first(struct seq_file *seq) 1420 { 1421 struct iter_state *st = seq->private; 1422 struct net *net; 1423 struct nfnl_queue_net *q; 1424 1425 if (!st) 1426 return NULL; 1427 1428 net = seq_file_net(seq); 1429 q = nfnl_queue_pernet(net); 1430 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) { 1431 if (!hlist_empty(&q->instance_table[st->bucket])) 1432 return q->instance_table[st->bucket].first; 1433 } 1434 return NULL; 1435 } 1436 1437 static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h) 1438 { 1439 struct iter_state *st = seq->private; 1440 struct net *net = seq_file_net(seq); 1441 1442 h = h->next; 1443 while (!h) { 1444 struct nfnl_queue_net *q; 1445 1446 if (++st->bucket >= INSTANCE_BUCKETS) 1447 return NULL; 1448 1449 q = nfnl_queue_pernet(net); 1450 h = q->instance_table[st->bucket].first; 1451 } 1452 return h; 1453 } 1454 1455 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos) 1456 { 1457 struct hlist_node *head; 1458 head = get_first(seq); 1459 1460 if (head) 1461 while (pos && (head = get_next(seq, head))) 1462 pos--; 1463 return pos ? NULL : head; 1464 } 1465 1466 static void *seq_start(struct seq_file *s, loff_t *pos) 1467 __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock) 1468 { 1469 spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock); 1470 return get_idx(s, *pos); 1471 } 1472 1473 static void *seq_next(struct seq_file *s, void *v, loff_t *pos) 1474 { 1475 (*pos)++; 1476 return get_next(s, v); 1477 } 1478 1479 static void seq_stop(struct seq_file *s, void *v) 1480 __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock) 1481 { 1482 spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock); 1483 } 1484 1485 static int seq_show(struct seq_file *s, void *v) 1486 { 1487 const struct nfqnl_instance *inst = v; 1488 1489 seq_printf(s, "%5u %6u %5u %1u %5u %5u %5u %8u %2d\n", 1490 inst->queue_num, 1491 inst->peer_portid, inst->queue_total, 1492 inst->copy_mode, inst->copy_range, 1493 inst->queue_dropped, inst->queue_user_dropped, 1494 inst->id_sequence, 1); 1495 return 0; 1496 } 1497 1498 static const struct seq_operations nfqnl_seq_ops = { 1499 .start = seq_start, 1500 .next = seq_next, 1501 .stop = seq_stop, 1502 .show = seq_show, 1503 }; 1504 #endif /* PROC_FS */ 1505 1506 static int __net_init nfnl_queue_net_init(struct net *net) 1507 { 1508 unsigned int i; 1509 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 1510 1511 for (i = 0; i < INSTANCE_BUCKETS; i++) 1512 INIT_HLIST_HEAD(&q->instance_table[i]); 1513 1514 spin_lock_init(&q->instances_lock); 1515 1516 #ifdef CONFIG_PROC_FS 1517 if (!proc_create_net("nfnetlink_queue", 0440, net->nf.proc_netfilter, 1518 &nfqnl_seq_ops, sizeof(struct iter_state))) 1519 return -ENOMEM; 1520 #endif 1521 return 0; 1522 } 1523 1524 static void __net_exit nfnl_queue_net_exit(struct net *net) 1525 { 1526 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 1527 unsigned int i; 1528 1529 #ifdef CONFIG_PROC_FS 1530 remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter); 1531 #endif 1532 for (i = 0; i < INSTANCE_BUCKETS; i++) 1533 WARN_ON_ONCE(!hlist_empty(&q->instance_table[i])); 1534 } 1535 1536 static struct pernet_operations nfnl_queue_net_ops = { 1537 .init = nfnl_queue_net_init, 1538 .exit = nfnl_queue_net_exit, 1539 .id = &nfnl_queue_net_id, 1540 .size = sizeof(struct nfnl_queue_net), 1541 }; 1542 1543 static int __init nfnetlink_queue_init(void) 1544 { 1545 int status; 1546 1547 status = register_pernet_subsys(&nfnl_queue_net_ops); 1548 if (status < 0) { 1549 pr_err("failed to register pernet ops\n"); 1550 goto out; 1551 } 1552 1553 netlink_register_notifier(&nfqnl_rtnl_notifier); 1554 status = nfnetlink_subsys_register(&nfqnl_subsys); 1555 if (status < 0) { 1556 pr_err("failed to create netlink socket\n"); 1557 goto cleanup_netlink_notifier; 1558 } 1559 1560 status = register_netdevice_notifier(&nfqnl_dev_notifier); 1561 if (status < 0) { 1562 pr_err("failed to register netdevice notifier\n"); 1563 goto cleanup_netlink_subsys; 1564 } 1565 1566 nf_register_queue_handler(&nfqh); 1567 1568 return status; 1569 1570 cleanup_netlink_subsys: 1571 nfnetlink_subsys_unregister(&nfqnl_subsys); 1572 cleanup_netlink_notifier: 1573 netlink_unregister_notifier(&nfqnl_rtnl_notifier); 1574 unregister_pernet_subsys(&nfnl_queue_net_ops); 1575 out: 1576 return status; 1577 } 1578 1579 static void __exit nfnetlink_queue_fini(void) 1580 { 1581 nf_unregister_queue_handler(); 1582 unregister_netdevice_notifier(&nfqnl_dev_notifier); 1583 nfnetlink_subsys_unregister(&nfqnl_subsys); 1584 netlink_unregister_notifier(&nfqnl_rtnl_notifier); 1585 unregister_pernet_subsys(&nfnl_queue_net_ops); 1586 1587 rcu_barrier(); /* Wait for completion of call_rcu()'s */ 1588 } 1589 1590 MODULE_DESCRIPTION("netfilter packet queue handler"); 1591 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); 1592 MODULE_LICENSE("GPL"); 1593 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE); 1594 1595 module_init(nfnetlink_queue_init); 1596 module_exit(nfnetlink_queue_fini); 1597