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