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 <[email protected]> 7 * (C) 2007 by Patrick McHardy <[email protected]> 8 * 9 * Based on the old ipv4-only ip_queue.c: 10 * (C) 2000-2002 James Morris <[email protected]> 11 * (C) 2003-2005 Netfilter Core Team <[email protected]> 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 u32 nfqnl_get_sk_secctx(struct sk_buff *skb, char **secdata) 474 { 475 u32 seclen = 0; 476 #if IS_ENABLED(CONFIG_NETWORK_SECMARK) 477 if (!skb || !sk_fullsock(skb->sk)) 478 return 0; 479 480 read_lock_bh(&skb->sk->sk_callback_lock); 481 482 if (skb->secmark) 483 security_secid_to_secctx(skb->secmark, secdata, &seclen); 484 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 struct sk_buff * 544 nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue, 545 struct nf_queue_entry *entry, 546 __be32 **packet_id_ptr) 547 { 548 size_t size; 549 size_t data_len = 0, cap_len = 0; 550 unsigned int hlen = 0; 551 struct sk_buff *skb; 552 struct nlattr *nla; 553 struct nfqnl_msg_packet_hdr *pmsg; 554 struct nlmsghdr *nlh; 555 struct sk_buff *entskb = entry->skb; 556 struct net_device *indev; 557 struct net_device *outdev; 558 struct nf_conn *ct = NULL; 559 enum ip_conntrack_info ctinfo = 0; 560 const struct nfnl_ct_hook *nfnl_ct; 561 bool csum_verify; 562 char *secdata = NULL; 563 u32 seclen = 0; 564 ktime_t tstamp; 565 566 size = nlmsg_total_size(sizeof(struct nfgenmsg)) 567 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr)) 568 + nla_total_size(sizeof(u_int32_t)) /* ifindex */ 569 + nla_total_size(sizeof(u_int32_t)) /* ifindex */ 570 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 571 + nla_total_size(sizeof(u_int32_t)) /* ifindex */ 572 + nla_total_size(sizeof(u_int32_t)) /* ifindex */ 573 #endif 574 + nla_total_size(sizeof(u_int32_t)) /* mark */ 575 + nla_total_size(sizeof(u_int32_t)) /* priority */ 576 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw)) 577 + nla_total_size(sizeof(u_int32_t)) /* skbinfo */ 578 #if IS_ENABLED(CONFIG_CGROUP_NET_CLASSID) 579 + nla_total_size(sizeof(u_int32_t)) /* classid */ 580 #endif 581 + nla_total_size(sizeof(u_int32_t)); /* cap_len */ 582 583 tstamp = skb_tstamp_cond(entskb, false); 584 if (tstamp) 585 size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp)); 586 587 size += nfqnl_get_bridge_size(entry); 588 589 if (entry->state.hook <= NF_INET_FORWARD || 590 (entry->state.hook == NF_INET_POST_ROUTING && entskb->sk == NULL)) 591 csum_verify = !skb_csum_unnecessary(entskb); 592 else 593 csum_verify = false; 594 595 outdev = entry->state.out; 596 597 switch ((enum nfqnl_config_mode)READ_ONCE(queue->copy_mode)) { 598 case NFQNL_COPY_META: 599 case NFQNL_COPY_NONE: 600 break; 601 602 case NFQNL_COPY_PACKET: 603 if (!(queue->flags & NFQA_CFG_F_GSO) && 604 entskb->ip_summed == CHECKSUM_PARTIAL && 605 skb_checksum_help(entskb)) 606 return NULL; 607 608 data_len = READ_ONCE(queue->copy_range); 609 if (data_len > entskb->len) 610 data_len = entskb->len; 611 612 hlen = skb_zerocopy_headlen(entskb); 613 hlen = min_t(unsigned int, hlen, data_len); 614 size += sizeof(struct nlattr) + hlen; 615 cap_len = entskb->len; 616 break; 617 } 618 619 nfnl_ct = rcu_dereference(nfnl_ct_hook); 620 621 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 622 if (queue->flags & NFQA_CFG_F_CONNTRACK) { 623 if (nfnl_ct != NULL) { 624 ct = nf_ct_get(entskb, &ctinfo); 625 if (ct != NULL) 626 size += nfnl_ct->build_size(ct); 627 } 628 } 629 #endif 630 631 if (queue->flags & NFQA_CFG_F_UID_GID) { 632 size += (nla_total_size(sizeof(u_int32_t)) /* uid */ 633 + nla_total_size(sizeof(u_int32_t))); /* gid */ 634 } 635 636 if ((queue->flags & NFQA_CFG_F_SECCTX) && entskb->sk) { 637 seclen = nfqnl_get_sk_secctx(entskb, &secdata); 638 if (seclen) 639 size += nla_total_size(seclen); 640 } 641 642 skb = alloc_skb(size, GFP_ATOMIC); 643 if (!skb) { 644 skb_tx_error(entskb); 645 goto nlmsg_failure; 646 } 647 648 nlh = nfnl_msg_put(skb, 0, 0, 649 nfnl_msg_type(NFNL_SUBSYS_QUEUE, NFQNL_MSG_PACKET), 650 0, entry->state.pf, NFNETLINK_V0, 651 htons(queue->queue_num)); 652 if (!nlh) { 653 skb_tx_error(entskb); 654 kfree_skb(skb); 655 goto nlmsg_failure; 656 } 657 658 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg)); 659 pmsg = nla_data(nla); 660 pmsg->hw_protocol = entskb->protocol; 661 pmsg->hook = entry->state.hook; 662 *packet_id_ptr = &pmsg->packet_id; 663 664 indev = entry->state.in; 665 if (indev) { 666 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 667 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex))) 668 goto nla_put_failure; 669 #else 670 if (entry->state.pf == PF_BRIDGE) { 671 /* Case 1: indev is physical input device, we need to 672 * look for bridge group (when called from 673 * netfilter_bridge) */ 674 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV, 675 htonl(indev->ifindex)) || 676 /* this is the bridge group "brX" */ 677 /* rcu_read_lock()ed by __nf_queue */ 678 nla_put_be32(skb, NFQA_IFINDEX_INDEV, 679 htonl(br_port_get_rcu(indev)->br->dev->ifindex))) 680 goto nla_put_failure; 681 } else { 682 int physinif; 683 684 /* Case 2: indev is bridge group, we need to look for 685 * physical device (when called from ipv4) */ 686 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, 687 htonl(indev->ifindex))) 688 goto nla_put_failure; 689 690 physinif = nf_bridge_get_physinif(entskb); 691 if (physinif && 692 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV, 693 htonl(physinif))) 694 goto nla_put_failure; 695 } 696 #endif 697 } 698 699 if (outdev) { 700 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 701 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex))) 702 goto nla_put_failure; 703 #else 704 if (entry->state.pf == PF_BRIDGE) { 705 /* Case 1: outdev is physical output device, we need to 706 * look for bridge group (when called from 707 * netfilter_bridge) */ 708 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV, 709 htonl(outdev->ifindex)) || 710 /* this is the bridge group "brX" */ 711 /* rcu_read_lock()ed by __nf_queue */ 712 nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, 713 htonl(br_port_get_rcu(outdev)->br->dev->ifindex))) 714 goto nla_put_failure; 715 } else { 716 int physoutif; 717 718 /* Case 2: outdev is bridge group, we need to look for 719 * physical output device (when called from ipv4) */ 720 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, 721 htonl(outdev->ifindex))) 722 goto nla_put_failure; 723 724 physoutif = nf_bridge_get_physoutif(entskb); 725 if (physoutif && 726 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV, 727 htonl(physoutif))) 728 goto nla_put_failure; 729 } 730 #endif 731 } 732 733 if (entskb->mark && 734 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark))) 735 goto nla_put_failure; 736 737 if (entskb->priority && 738 nla_put_be32(skb, NFQA_PRIORITY, htonl(entskb->priority))) 739 goto nla_put_failure; 740 741 if (indev && entskb->dev && 742 skb_mac_header_was_set(entskb) && 743 skb_mac_header_len(entskb) != 0) { 744 struct nfqnl_msg_packet_hw phw; 745 int len; 746 747 memset(&phw, 0, sizeof(phw)); 748 len = dev_parse_header(entskb, phw.hw_addr); 749 if (len) { 750 phw.hw_addrlen = htons(len); 751 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw)) 752 goto nla_put_failure; 753 } 754 } 755 756 if (nfqnl_put_bridge(entry, skb) < 0) 757 goto nla_put_failure; 758 759 if (entry->state.hook <= NF_INET_FORWARD && tstamp) { 760 struct nfqnl_msg_packet_timestamp ts; 761 struct timespec64 kts = ktime_to_timespec64(tstamp); 762 763 ts.sec = cpu_to_be64(kts.tv_sec); 764 ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC); 765 766 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts)) 767 goto nla_put_failure; 768 } 769 770 if ((queue->flags & NFQA_CFG_F_UID_GID) && entskb->sk && 771 nfqnl_put_sk_uidgid(skb, entskb->sk) < 0) 772 goto nla_put_failure; 773 774 if (nfqnl_put_sk_classid(skb, entskb->sk) < 0) 775 goto nla_put_failure; 776 777 if (seclen && nla_put(skb, NFQA_SECCTX, seclen, secdata)) 778 goto nla_put_failure; 779 780 if (ct && nfnl_ct->build(skb, ct, ctinfo, NFQA_CT, NFQA_CT_INFO) < 0) 781 goto nla_put_failure; 782 783 if (cap_len > data_len && 784 nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len))) 785 goto nla_put_failure; 786 787 if (nfqnl_put_packet_info(skb, entskb, csum_verify)) 788 goto nla_put_failure; 789 790 if (data_len) { 791 struct nlattr *nla; 792 793 if (skb_tailroom(skb) < sizeof(*nla) + hlen) 794 goto nla_put_failure; 795 796 nla = skb_put(skb, sizeof(*nla)); 797 nla->nla_type = NFQA_PAYLOAD; 798 nla->nla_len = nla_attr_size(data_len); 799 800 if (skb_zerocopy(skb, entskb, data_len, hlen)) 801 goto nla_put_failure; 802 } 803 804 nlh->nlmsg_len = skb->len; 805 if (seclen) 806 security_release_secctx(secdata, seclen); 807 return skb; 808 809 nla_put_failure: 810 skb_tx_error(entskb); 811 kfree_skb(skb); 812 net_err_ratelimited("nf_queue: error creating packet message\n"); 813 nlmsg_failure: 814 if (seclen) 815 security_release_secctx(secdata, seclen); 816 return NULL; 817 } 818 819 static bool nf_ct_drop_unconfirmed(const struct nf_queue_entry *entry) 820 { 821 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 822 static const unsigned long flags = IPS_CONFIRMED | IPS_DYING; 823 struct nf_conn *ct = (void *)skb_nfct(entry->skb); 824 unsigned long status; 825 unsigned int use; 826 827 if (!ct) 828 return false; 829 830 status = READ_ONCE(ct->status); 831 if ((status & flags) == IPS_DYING) 832 return true; 833 834 if (status & IPS_CONFIRMED) 835 return false; 836 837 /* in some cases skb_clone() can occur after initial conntrack 838 * pickup, but conntrack assumes exclusive skb->_nfct ownership for 839 * unconfirmed entries. 840 * 841 * This happens for br_netfilter and with ip multicast routing. 842 * We can't be solved with serialization here because one clone could 843 * have been queued for local delivery. 844 */ 845 use = refcount_read(&ct->ct_general.use); 846 if (likely(use == 1)) 847 return false; 848 849 /* Can't decrement further? Exclusive ownership. */ 850 if (!refcount_dec_not_one(&ct->ct_general.use)) 851 return false; 852 853 skb_set_nfct(entry->skb, 0); 854 /* No nf_ct_put(): we already decremented .use and it cannot 855 * drop down to 0. 856 */ 857 return true; 858 #endif 859 return false; 860 } 861 862 static int 863 __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue, 864 struct nf_queue_entry *entry) 865 { 866 struct sk_buff *nskb; 867 int err = -ENOBUFS; 868 __be32 *packet_id_ptr; 869 int failopen = 0; 870 871 nskb = nfqnl_build_packet_message(net, queue, entry, &packet_id_ptr); 872 if (nskb == NULL) { 873 err = -ENOMEM; 874 goto err_out; 875 } 876 spin_lock_bh(&queue->lock); 877 878 if (nf_ct_drop_unconfirmed(entry)) 879 goto err_out_free_nskb; 880 881 if (queue->queue_total >= queue->queue_maxlen) { 882 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) { 883 failopen = 1; 884 err = 0; 885 } else { 886 queue->queue_dropped++; 887 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n", 888 queue->queue_total); 889 } 890 goto err_out_free_nskb; 891 } 892 entry->id = ++queue->id_sequence; 893 *packet_id_ptr = htonl(entry->id); 894 895 /* nfnetlink_unicast will either free the nskb or add it to a socket */ 896 err = nfnetlink_unicast(nskb, net, queue->peer_portid); 897 if (err < 0) { 898 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) { 899 failopen = 1; 900 err = 0; 901 } else { 902 queue->queue_user_dropped++; 903 } 904 goto err_out_unlock; 905 } 906 907 __enqueue_entry(queue, entry); 908 909 spin_unlock_bh(&queue->lock); 910 return 0; 911 912 err_out_free_nskb: 913 kfree_skb(nskb); 914 err_out_unlock: 915 spin_unlock_bh(&queue->lock); 916 if (failopen) 917 nfqnl_reinject(entry, NF_ACCEPT); 918 err_out: 919 return err; 920 } 921 922 static struct nf_queue_entry * 923 nf_queue_entry_dup(struct nf_queue_entry *e) 924 { 925 struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC); 926 927 if (!entry) 928 return NULL; 929 930 if (nf_queue_entry_get_refs(entry)) 931 return entry; 932 933 kfree(entry); 934 return NULL; 935 } 936 937 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 938 /* When called from bridge netfilter, skb->data must point to MAC header 939 * before calling skb_gso_segment(). Else, original MAC header is lost 940 * and segmented skbs will be sent to wrong destination. 941 */ 942 static void nf_bridge_adjust_skb_data(struct sk_buff *skb) 943 { 944 if (nf_bridge_info_get(skb)) 945 __skb_push(skb, skb->network_header - skb->mac_header); 946 } 947 948 static void nf_bridge_adjust_segmented_data(struct sk_buff *skb) 949 { 950 if (nf_bridge_info_get(skb)) 951 __skb_pull(skb, skb->network_header - skb->mac_header); 952 } 953 #else 954 #define nf_bridge_adjust_skb_data(s) do {} while (0) 955 #define nf_bridge_adjust_segmented_data(s) do {} while (0) 956 #endif 957 958 static int 959 __nfqnl_enqueue_packet_gso(struct net *net, struct nfqnl_instance *queue, 960 struct sk_buff *skb, struct nf_queue_entry *entry) 961 { 962 int ret = -ENOMEM; 963 struct nf_queue_entry *entry_seg; 964 965 nf_bridge_adjust_segmented_data(skb); 966 967 if (skb->next == NULL) { /* last packet, no need to copy entry */ 968 struct sk_buff *gso_skb = entry->skb; 969 entry->skb = skb; 970 ret = __nfqnl_enqueue_packet(net, queue, entry); 971 if (ret) 972 entry->skb = gso_skb; 973 return ret; 974 } 975 976 skb_mark_not_on_list(skb); 977 978 entry_seg = nf_queue_entry_dup(entry); 979 if (entry_seg) { 980 entry_seg->skb = skb; 981 ret = __nfqnl_enqueue_packet(net, queue, entry_seg); 982 if (ret) 983 nf_queue_entry_free(entry_seg); 984 } 985 return ret; 986 } 987 988 static int 989 nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum) 990 { 991 unsigned int queued; 992 struct nfqnl_instance *queue; 993 struct sk_buff *skb, *segs, *nskb; 994 int err = -ENOBUFS; 995 struct net *net = entry->state.net; 996 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 997 998 /* rcu_read_lock()ed by nf_hook_thresh */ 999 queue = instance_lookup(q, queuenum); 1000 if (!queue) 1001 return -ESRCH; 1002 1003 if (queue->copy_mode == NFQNL_COPY_NONE) 1004 return -EINVAL; 1005 1006 skb = entry->skb; 1007 1008 switch (entry->state.pf) { 1009 case NFPROTO_IPV4: 1010 skb->protocol = htons(ETH_P_IP); 1011 break; 1012 case NFPROTO_IPV6: 1013 skb->protocol = htons(ETH_P_IPV6); 1014 break; 1015 } 1016 1017 if ((queue->flags & NFQA_CFG_F_GSO) || !skb_is_gso(skb)) 1018 return __nfqnl_enqueue_packet(net, queue, entry); 1019 1020 nf_bridge_adjust_skb_data(skb); 1021 segs = skb_gso_segment(skb, 0); 1022 /* Does not use PTR_ERR to limit the number of error codes that can be 1023 * returned by nf_queue. For instance, callers rely on -ESRCH to 1024 * mean 'ignore this hook'. 1025 */ 1026 if (IS_ERR_OR_NULL(segs)) 1027 goto out_err; 1028 queued = 0; 1029 err = 0; 1030 skb_list_walk_safe(segs, segs, nskb) { 1031 if (err == 0) 1032 err = __nfqnl_enqueue_packet_gso(net, queue, 1033 segs, entry); 1034 if (err == 0) 1035 queued++; 1036 else 1037 kfree_skb(segs); 1038 } 1039 1040 if (queued) { 1041 if (err) /* some segments are already queued */ 1042 nf_queue_entry_free(entry); 1043 kfree_skb(skb); 1044 return 0; 1045 } 1046 out_err: 1047 nf_bridge_adjust_segmented_data(skb); 1048 return err; 1049 } 1050 1051 static int 1052 nfqnl_mangle(void *data, unsigned int data_len, struct nf_queue_entry *e, int diff) 1053 { 1054 struct sk_buff *nskb; 1055 1056 if (diff < 0) { 1057 unsigned int min_len = skb_transport_offset(e->skb); 1058 1059 if (data_len < min_len) 1060 return -EINVAL; 1061 1062 if (pskb_trim(e->skb, data_len)) 1063 return -ENOMEM; 1064 } else if (diff > 0) { 1065 if (data_len > 0xFFFF) 1066 return -EINVAL; 1067 if (diff > skb_tailroom(e->skb)) { 1068 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb), 1069 diff, GFP_ATOMIC); 1070 if (!nskb) 1071 return -ENOMEM; 1072 kfree_skb(e->skb); 1073 e->skb = nskb; 1074 } 1075 skb_put(e->skb, diff); 1076 } 1077 if (skb_ensure_writable(e->skb, data_len)) 1078 return -ENOMEM; 1079 skb_copy_to_linear_data(e->skb, data, data_len); 1080 e->skb->ip_summed = CHECKSUM_NONE; 1081 return 0; 1082 } 1083 1084 static int 1085 nfqnl_set_mode(struct nfqnl_instance *queue, 1086 unsigned char mode, unsigned int range) 1087 { 1088 int status = 0; 1089 1090 spin_lock_bh(&queue->lock); 1091 switch (mode) { 1092 case NFQNL_COPY_NONE: 1093 case NFQNL_COPY_META: 1094 queue->copy_mode = mode; 1095 queue->copy_range = 0; 1096 break; 1097 1098 case NFQNL_COPY_PACKET: 1099 queue->copy_mode = mode; 1100 if (range == 0 || range > NFQNL_MAX_COPY_RANGE) 1101 queue->copy_range = NFQNL_MAX_COPY_RANGE; 1102 else 1103 queue->copy_range = range; 1104 break; 1105 1106 default: 1107 status = -EINVAL; 1108 1109 } 1110 spin_unlock_bh(&queue->lock); 1111 1112 return status; 1113 } 1114 1115 static int 1116 dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex) 1117 { 1118 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 1119 int physinif, physoutif; 1120 1121 physinif = nf_bridge_get_physinif(entry->skb); 1122 physoutif = nf_bridge_get_physoutif(entry->skb); 1123 1124 if (physinif == ifindex || physoutif == ifindex) 1125 return 1; 1126 #endif 1127 if (entry->state.in) 1128 if (entry->state.in->ifindex == ifindex) 1129 return 1; 1130 if (entry->state.out) 1131 if (entry->state.out->ifindex == ifindex) 1132 return 1; 1133 1134 return 0; 1135 } 1136 1137 /* drop all packets with either indev or outdev == ifindex from all queue 1138 * instances */ 1139 static void 1140 nfqnl_dev_drop(struct net *net, int ifindex) 1141 { 1142 int i; 1143 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 1144 1145 rcu_read_lock(); 1146 1147 for (i = 0; i < INSTANCE_BUCKETS; i++) { 1148 struct nfqnl_instance *inst; 1149 struct hlist_head *head = &q->instance_table[i]; 1150 1151 hlist_for_each_entry_rcu(inst, head, hlist) 1152 nfqnl_flush(inst, dev_cmp, ifindex); 1153 } 1154 1155 rcu_read_unlock(); 1156 } 1157 1158 static int 1159 nfqnl_rcv_dev_event(struct notifier_block *this, 1160 unsigned long event, void *ptr) 1161 { 1162 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1163 1164 /* Drop any packets associated with the downed device */ 1165 if (event == NETDEV_DOWN) 1166 nfqnl_dev_drop(dev_net(dev), dev->ifindex); 1167 return NOTIFY_DONE; 1168 } 1169 1170 static struct notifier_block nfqnl_dev_notifier = { 1171 .notifier_call = nfqnl_rcv_dev_event, 1172 }; 1173 1174 static void nfqnl_nf_hook_drop(struct net *net) 1175 { 1176 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 1177 int i; 1178 1179 /* This function is also called on net namespace error unwind, 1180 * when pernet_ops->init() failed and ->exit() functions of the 1181 * previous pernet_ops gets called. 1182 * 1183 * This may result in a call to nfqnl_nf_hook_drop() before 1184 * struct nfnl_queue_net was allocated. 1185 */ 1186 if (!q) 1187 return; 1188 1189 for (i = 0; i < INSTANCE_BUCKETS; i++) { 1190 struct nfqnl_instance *inst; 1191 struct hlist_head *head = &q->instance_table[i]; 1192 1193 hlist_for_each_entry_rcu(inst, head, hlist) 1194 nfqnl_flush(inst, NULL, 0); 1195 } 1196 } 1197 1198 static int 1199 nfqnl_rcv_nl_event(struct notifier_block *this, 1200 unsigned long event, void *ptr) 1201 { 1202 struct netlink_notify *n = ptr; 1203 struct nfnl_queue_net *q = nfnl_queue_pernet(n->net); 1204 1205 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) { 1206 int i; 1207 1208 /* destroy all instances for this portid */ 1209 spin_lock(&q->instances_lock); 1210 for (i = 0; i < INSTANCE_BUCKETS; i++) { 1211 struct hlist_node *t2; 1212 struct nfqnl_instance *inst; 1213 struct hlist_head *head = &q->instance_table[i]; 1214 1215 hlist_for_each_entry_safe(inst, t2, head, hlist) { 1216 if (n->portid == inst->peer_portid) 1217 __instance_destroy(inst); 1218 } 1219 } 1220 spin_unlock(&q->instances_lock); 1221 } 1222 return NOTIFY_DONE; 1223 } 1224 1225 static struct notifier_block nfqnl_rtnl_notifier = { 1226 .notifier_call = nfqnl_rcv_nl_event, 1227 }; 1228 1229 static const struct nla_policy nfqa_vlan_policy[NFQA_VLAN_MAX + 1] = { 1230 [NFQA_VLAN_TCI] = { .type = NLA_U16}, 1231 [NFQA_VLAN_PROTO] = { .type = NLA_U16}, 1232 }; 1233 1234 static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = { 1235 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, 1236 [NFQA_MARK] = { .type = NLA_U32 }, 1237 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC }, 1238 [NFQA_CT] = { .type = NLA_UNSPEC }, 1239 [NFQA_EXP] = { .type = NLA_UNSPEC }, 1240 [NFQA_VLAN] = { .type = NLA_NESTED }, 1241 [NFQA_PRIORITY] = { .type = NLA_U32 }, 1242 }; 1243 1244 static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = { 1245 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, 1246 [NFQA_MARK] = { .type = NLA_U32 }, 1247 [NFQA_PRIORITY] = { .type = NLA_U32 }, 1248 }; 1249 1250 static struct nfqnl_instance * 1251 verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, u32 nlportid) 1252 { 1253 struct nfqnl_instance *queue; 1254 1255 queue = instance_lookup(q, queue_num); 1256 if (!queue) 1257 return ERR_PTR(-ENODEV); 1258 1259 if (queue->peer_portid != nlportid) 1260 return ERR_PTR(-EPERM); 1261 1262 return queue; 1263 } 1264 1265 static struct nfqnl_msg_verdict_hdr* 1266 verdicthdr_get(const struct nlattr * const nfqa[]) 1267 { 1268 struct nfqnl_msg_verdict_hdr *vhdr; 1269 unsigned int verdict; 1270 1271 if (!nfqa[NFQA_VERDICT_HDR]) 1272 return NULL; 1273 1274 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]); 1275 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK; 1276 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN) 1277 return NULL; 1278 return vhdr; 1279 } 1280 1281 static int nfq_id_after(unsigned int id, unsigned int max) 1282 { 1283 return (int)(id - max) > 0; 1284 } 1285 1286 static int nfqnl_recv_verdict_batch(struct sk_buff *skb, 1287 const struct nfnl_info *info, 1288 const struct nlattr * const nfqa[]) 1289 { 1290 struct nfnl_queue_net *q = nfnl_queue_pernet(info->net); 1291 u16 queue_num = ntohs(info->nfmsg->res_id); 1292 struct nf_queue_entry *entry, *tmp; 1293 struct nfqnl_msg_verdict_hdr *vhdr; 1294 struct nfqnl_instance *queue; 1295 unsigned int verdict, maxid; 1296 LIST_HEAD(batch_list); 1297 1298 queue = verdict_instance_lookup(q, queue_num, 1299 NETLINK_CB(skb).portid); 1300 if (IS_ERR(queue)) 1301 return PTR_ERR(queue); 1302 1303 vhdr = verdicthdr_get(nfqa); 1304 if (!vhdr) 1305 return -EINVAL; 1306 1307 verdict = ntohl(vhdr->verdict); 1308 maxid = ntohl(vhdr->id); 1309 1310 spin_lock_bh(&queue->lock); 1311 1312 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) { 1313 if (nfq_id_after(entry->id, maxid)) 1314 break; 1315 __dequeue_entry(queue, entry); 1316 list_add_tail(&entry->list, &batch_list); 1317 } 1318 1319 spin_unlock_bh(&queue->lock); 1320 1321 if (list_empty(&batch_list)) 1322 return -ENOENT; 1323 1324 list_for_each_entry_safe(entry, tmp, &batch_list, list) { 1325 if (nfqa[NFQA_MARK]) 1326 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK])); 1327 1328 if (nfqa[NFQA_PRIORITY]) 1329 entry->skb->priority = ntohl(nla_get_be32(nfqa[NFQA_PRIORITY])); 1330 1331 nfqnl_reinject(entry, verdict); 1332 } 1333 return 0; 1334 } 1335 1336 static struct nf_conn *nfqnl_ct_parse(const struct nfnl_ct_hook *nfnl_ct, 1337 const struct nlmsghdr *nlh, 1338 const struct nlattr * const nfqa[], 1339 struct nf_queue_entry *entry, 1340 enum ip_conntrack_info *ctinfo) 1341 { 1342 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 1343 struct nf_conn *ct; 1344 1345 ct = nf_ct_get(entry->skb, ctinfo); 1346 if (ct == NULL) 1347 return NULL; 1348 1349 if (nfnl_ct->parse(nfqa[NFQA_CT], ct) < 0) 1350 return NULL; 1351 1352 if (nfqa[NFQA_EXP]) 1353 nfnl_ct->attach_expect(nfqa[NFQA_EXP], ct, 1354 NETLINK_CB(entry->skb).portid, 1355 nlmsg_report(nlh)); 1356 return ct; 1357 #else 1358 return NULL; 1359 #endif 1360 } 1361 1362 static int nfqa_parse_bridge(struct nf_queue_entry *entry, 1363 const struct nlattr * const nfqa[]) 1364 { 1365 if (nfqa[NFQA_VLAN]) { 1366 struct nlattr *tb[NFQA_VLAN_MAX + 1]; 1367 int err; 1368 1369 err = nla_parse_nested_deprecated(tb, NFQA_VLAN_MAX, 1370 nfqa[NFQA_VLAN], 1371 nfqa_vlan_policy, NULL); 1372 if (err < 0) 1373 return err; 1374 1375 if (!tb[NFQA_VLAN_TCI] || !tb[NFQA_VLAN_PROTO]) 1376 return -EINVAL; 1377 1378 __vlan_hwaccel_put_tag(entry->skb, 1379 nla_get_be16(tb[NFQA_VLAN_PROTO]), 1380 ntohs(nla_get_be16(tb[NFQA_VLAN_TCI]))); 1381 } 1382 1383 if (nfqa[NFQA_L2HDR]) { 1384 int mac_header_len = entry->skb->network_header - 1385 entry->skb->mac_header; 1386 1387 if (mac_header_len != nla_len(nfqa[NFQA_L2HDR])) 1388 return -EINVAL; 1389 else if (mac_header_len > 0) 1390 memcpy(skb_mac_header(entry->skb), 1391 nla_data(nfqa[NFQA_L2HDR]), 1392 mac_header_len); 1393 } 1394 1395 return 0; 1396 } 1397 1398 static int nfqnl_recv_verdict(struct sk_buff *skb, const struct nfnl_info *info, 1399 const struct nlattr * const nfqa[]) 1400 { 1401 struct nfnl_queue_net *q = nfnl_queue_pernet(info->net); 1402 u_int16_t queue_num = ntohs(info->nfmsg->res_id); 1403 const struct nfnl_ct_hook *nfnl_ct; 1404 struct nfqnl_msg_verdict_hdr *vhdr; 1405 enum ip_conntrack_info ctinfo; 1406 struct nfqnl_instance *queue; 1407 struct nf_queue_entry *entry; 1408 struct nf_conn *ct = NULL; 1409 unsigned int verdict; 1410 int err; 1411 1412 queue = verdict_instance_lookup(q, queue_num, 1413 NETLINK_CB(skb).portid); 1414 if (IS_ERR(queue)) 1415 return PTR_ERR(queue); 1416 1417 vhdr = verdicthdr_get(nfqa); 1418 if (!vhdr) 1419 return -EINVAL; 1420 1421 verdict = ntohl(vhdr->verdict); 1422 1423 entry = find_dequeue_entry(queue, ntohl(vhdr->id)); 1424 if (entry == NULL) 1425 return -ENOENT; 1426 1427 /* rcu lock already held from nfnl->call_rcu. */ 1428 nfnl_ct = rcu_dereference(nfnl_ct_hook); 1429 1430 if (nfqa[NFQA_CT]) { 1431 if (nfnl_ct != NULL) 1432 ct = nfqnl_ct_parse(nfnl_ct, info->nlh, nfqa, entry, 1433 &ctinfo); 1434 } 1435 1436 if (entry->state.pf == PF_BRIDGE) { 1437 err = nfqa_parse_bridge(entry, nfqa); 1438 if (err < 0) 1439 return err; 1440 } 1441 1442 if (nfqa[NFQA_PAYLOAD]) { 1443 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]); 1444 int diff = payload_len - entry->skb->len; 1445 1446 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]), 1447 payload_len, entry, diff) < 0) 1448 verdict = NF_DROP; 1449 1450 if (ct && diff) 1451 nfnl_ct->seq_adjust(entry->skb, ct, ctinfo, diff); 1452 } 1453 1454 if (nfqa[NFQA_MARK]) 1455 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK])); 1456 1457 if (nfqa[NFQA_PRIORITY]) 1458 entry->skb->priority = ntohl(nla_get_be32(nfqa[NFQA_PRIORITY])); 1459 1460 nfqnl_reinject(entry, verdict); 1461 return 0; 1462 } 1463 1464 static int nfqnl_recv_unsupp(struct sk_buff *skb, const struct nfnl_info *info, 1465 const struct nlattr * const cda[]) 1466 { 1467 return -ENOTSUPP; 1468 } 1469 1470 static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = { 1471 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) }, 1472 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) }, 1473 [NFQA_CFG_QUEUE_MAXLEN] = { .type = NLA_U32 }, 1474 [NFQA_CFG_MASK] = { .type = NLA_U32 }, 1475 [NFQA_CFG_FLAGS] = { .type = NLA_U32 }, 1476 }; 1477 1478 static const struct nf_queue_handler nfqh = { 1479 .outfn = nfqnl_enqueue_packet, 1480 .nf_hook_drop = nfqnl_nf_hook_drop, 1481 }; 1482 1483 static int nfqnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info, 1484 const struct nlattr * const nfqa[]) 1485 { 1486 struct nfnl_queue_net *q = nfnl_queue_pernet(info->net); 1487 u_int16_t queue_num = ntohs(info->nfmsg->res_id); 1488 struct nfqnl_msg_config_cmd *cmd = NULL; 1489 struct nfqnl_instance *queue; 1490 __u32 flags = 0, mask = 0; 1491 int ret = 0; 1492 1493 if (nfqa[NFQA_CFG_CMD]) { 1494 cmd = nla_data(nfqa[NFQA_CFG_CMD]); 1495 1496 /* Obsolete commands without queue context */ 1497 switch (cmd->command) { 1498 case NFQNL_CFG_CMD_PF_BIND: return 0; 1499 case NFQNL_CFG_CMD_PF_UNBIND: return 0; 1500 } 1501 } 1502 1503 /* Check if we support these flags in first place, dependencies should 1504 * be there too not to break atomicity. 1505 */ 1506 if (nfqa[NFQA_CFG_FLAGS]) { 1507 if (!nfqa[NFQA_CFG_MASK]) { 1508 /* A mask is needed to specify which flags are being 1509 * changed. 1510 */ 1511 return -EINVAL; 1512 } 1513 1514 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS])); 1515 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK])); 1516 1517 if (flags >= NFQA_CFG_F_MAX) 1518 return -EOPNOTSUPP; 1519 1520 #if !IS_ENABLED(CONFIG_NETWORK_SECMARK) 1521 if (flags & mask & NFQA_CFG_F_SECCTX) 1522 return -EOPNOTSUPP; 1523 #endif 1524 if ((flags & mask & NFQA_CFG_F_CONNTRACK) && 1525 !rcu_access_pointer(nfnl_ct_hook)) { 1526 #ifdef CONFIG_MODULES 1527 nfnl_unlock(NFNL_SUBSYS_QUEUE); 1528 request_module("ip_conntrack_netlink"); 1529 nfnl_lock(NFNL_SUBSYS_QUEUE); 1530 if (rcu_access_pointer(nfnl_ct_hook)) 1531 return -EAGAIN; 1532 #endif 1533 return -EOPNOTSUPP; 1534 } 1535 } 1536 1537 rcu_read_lock(); 1538 queue = instance_lookup(q, queue_num); 1539 if (queue && queue->peer_portid != NETLINK_CB(skb).portid) { 1540 ret = -EPERM; 1541 goto err_out_unlock; 1542 } 1543 1544 if (cmd != NULL) { 1545 switch (cmd->command) { 1546 case NFQNL_CFG_CMD_BIND: 1547 if (queue) { 1548 ret = -EBUSY; 1549 goto err_out_unlock; 1550 } 1551 queue = instance_create(q, queue_num, 1552 NETLINK_CB(skb).portid); 1553 if (IS_ERR(queue)) { 1554 ret = PTR_ERR(queue); 1555 goto err_out_unlock; 1556 } 1557 break; 1558 case NFQNL_CFG_CMD_UNBIND: 1559 if (!queue) { 1560 ret = -ENODEV; 1561 goto err_out_unlock; 1562 } 1563 instance_destroy(q, queue); 1564 goto err_out_unlock; 1565 case NFQNL_CFG_CMD_PF_BIND: 1566 case NFQNL_CFG_CMD_PF_UNBIND: 1567 break; 1568 default: 1569 ret = -ENOTSUPP; 1570 goto err_out_unlock; 1571 } 1572 } 1573 1574 if (!queue) { 1575 ret = -ENODEV; 1576 goto err_out_unlock; 1577 } 1578 1579 if (nfqa[NFQA_CFG_PARAMS]) { 1580 struct nfqnl_msg_config_params *params = 1581 nla_data(nfqa[NFQA_CFG_PARAMS]); 1582 1583 nfqnl_set_mode(queue, params->copy_mode, 1584 ntohl(params->copy_range)); 1585 } 1586 1587 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) { 1588 __be32 *queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]); 1589 1590 spin_lock_bh(&queue->lock); 1591 queue->queue_maxlen = ntohl(*queue_maxlen); 1592 spin_unlock_bh(&queue->lock); 1593 } 1594 1595 if (nfqa[NFQA_CFG_FLAGS]) { 1596 spin_lock_bh(&queue->lock); 1597 queue->flags &= ~mask; 1598 queue->flags |= flags & mask; 1599 spin_unlock_bh(&queue->lock); 1600 } 1601 1602 err_out_unlock: 1603 rcu_read_unlock(); 1604 return ret; 1605 } 1606 1607 static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = { 1608 [NFQNL_MSG_PACKET] = { 1609 .call = nfqnl_recv_unsupp, 1610 .type = NFNL_CB_RCU, 1611 .attr_count = NFQA_MAX, 1612 }, 1613 [NFQNL_MSG_VERDICT] = { 1614 .call = nfqnl_recv_verdict, 1615 .type = NFNL_CB_RCU, 1616 .attr_count = NFQA_MAX, 1617 .policy = nfqa_verdict_policy 1618 }, 1619 [NFQNL_MSG_CONFIG] = { 1620 .call = nfqnl_recv_config, 1621 .type = NFNL_CB_MUTEX, 1622 .attr_count = NFQA_CFG_MAX, 1623 .policy = nfqa_cfg_policy 1624 }, 1625 [NFQNL_MSG_VERDICT_BATCH] = { 1626 .call = nfqnl_recv_verdict_batch, 1627 .type = NFNL_CB_RCU, 1628 .attr_count = NFQA_MAX, 1629 .policy = nfqa_verdict_batch_policy 1630 }, 1631 }; 1632 1633 static const struct nfnetlink_subsystem nfqnl_subsys = { 1634 .name = "nf_queue", 1635 .subsys_id = NFNL_SUBSYS_QUEUE, 1636 .cb_count = NFQNL_MSG_MAX, 1637 .cb = nfqnl_cb, 1638 }; 1639 1640 #ifdef CONFIG_PROC_FS 1641 struct iter_state { 1642 struct seq_net_private p; 1643 unsigned int bucket; 1644 }; 1645 1646 static struct hlist_node *get_first(struct seq_file *seq) 1647 { 1648 struct iter_state *st = seq->private; 1649 struct net *net; 1650 struct nfnl_queue_net *q; 1651 1652 if (!st) 1653 return NULL; 1654 1655 net = seq_file_net(seq); 1656 q = nfnl_queue_pernet(net); 1657 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) { 1658 if (!hlist_empty(&q->instance_table[st->bucket])) 1659 return q->instance_table[st->bucket].first; 1660 } 1661 return NULL; 1662 } 1663 1664 static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h) 1665 { 1666 struct iter_state *st = seq->private; 1667 struct net *net = seq_file_net(seq); 1668 1669 h = h->next; 1670 while (!h) { 1671 struct nfnl_queue_net *q; 1672 1673 if (++st->bucket >= INSTANCE_BUCKETS) 1674 return NULL; 1675 1676 q = nfnl_queue_pernet(net); 1677 h = q->instance_table[st->bucket].first; 1678 } 1679 return h; 1680 } 1681 1682 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos) 1683 { 1684 struct hlist_node *head; 1685 head = get_first(seq); 1686 1687 if (head) 1688 while (pos && (head = get_next(seq, head))) 1689 pos--; 1690 return pos ? NULL : head; 1691 } 1692 1693 static void *seq_start(struct seq_file *s, loff_t *pos) 1694 __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock) 1695 { 1696 spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock); 1697 return get_idx(s, *pos); 1698 } 1699 1700 static void *seq_next(struct seq_file *s, void *v, loff_t *pos) 1701 { 1702 (*pos)++; 1703 return get_next(s, v); 1704 } 1705 1706 static void seq_stop(struct seq_file *s, void *v) 1707 __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock) 1708 { 1709 spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock); 1710 } 1711 1712 static int seq_show(struct seq_file *s, void *v) 1713 { 1714 const struct nfqnl_instance *inst = v; 1715 1716 seq_printf(s, "%5u %6u %5u %1u %5u %5u %5u %8u %2d\n", 1717 inst->queue_num, 1718 inst->peer_portid, inst->queue_total, 1719 inst->copy_mode, inst->copy_range, 1720 inst->queue_dropped, inst->queue_user_dropped, 1721 inst->id_sequence, 1); 1722 return 0; 1723 } 1724 1725 static const struct seq_operations nfqnl_seq_ops = { 1726 .start = seq_start, 1727 .next = seq_next, 1728 .stop = seq_stop, 1729 .show = seq_show, 1730 }; 1731 #endif /* PROC_FS */ 1732 1733 static int __net_init nfnl_queue_net_init(struct net *net) 1734 { 1735 unsigned int i; 1736 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 1737 1738 for (i = 0; i < INSTANCE_BUCKETS; i++) 1739 INIT_HLIST_HEAD(&q->instance_table[i]); 1740 1741 spin_lock_init(&q->instances_lock); 1742 1743 #ifdef CONFIG_PROC_FS 1744 if (!proc_create_net("nfnetlink_queue", 0440, net->nf.proc_netfilter, 1745 &nfqnl_seq_ops, sizeof(struct iter_state))) 1746 return -ENOMEM; 1747 #endif 1748 return 0; 1749 } 1750 1751 static void __net_exit nfnl_queue_net_exit(struct net *net) 1752 { 1753 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 1754 unsigned int i; 1755 1756 #ifdef CONFIG_PROC_FS 1757 remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter); 1758 #endif 1759 for (i = 0; i < INSTANCE_BUCKETS; i++) 1760 WARN_ON_ONCE(!hlist_empty(&q->instance_table[i])); 1761 } 1762 1763 static struct pernet_operations nfnl_queue_net_ops = { 1764 .init = nfnl_queue_net_init, 1765 .exit = nfnl_queue_net_exit, 1766 .id = &nfnl_queue_net_id, 1767 .size = sizeof(struct nfnl_queue_net), 1768 }; 1769 1770 static int __init nfnetlink_queue_init(void) 1771 { 1772 int status; 1773 1774 status = register_pernet_subsys(&nfnl_queue_net_ops); 1775 if (status < 0) { 1776 pr_err("failed to register pernet ops\n"); 1777 goto out; 1778 } 1779 1780 netlink_register_notifier(&nfqnl_rtnl_notifier); 1781 status = nfnetlink_subsys_register(&nfqnl_subsys); 1782 if (status < 0) { 1783 pr_err("failed to create netlink socket\n"); 1784 goto cleanup_netlink_notifier; 1785 } 1786 1787 status = register_netdevice_notifier(&nfqnl_dev_notifier); 1788 if (status < 0) { 1789 pr_err("failed to register netdevice notifier\n"); 1790 goto cleanup_netlink_subsys; 1791 } 1792 1793 nf_register_queue_handler(&nfqh); 1794 1795 return status; 1796 1797 cleanup_netlink_subsys: 1798 nfnetlink_subsys_unregister(&nfqnl_subsys); 1799 cleanup_netlink_notifier: 1800 netlink_unregister_notifier(&nfqnl_rtnl_notifier); 1801 unregister_pernet_subsys(&nfnl_queue_net_ops); 1802 out: 1803 return status; 1804 } 1805 1806 static void __exit nfnetlink_queue_fini(void) 1807 { 1808 nf_unregister_queue_handler(); 1809 unregister_netdevice_notifier(&nfqnl_dev_notifier); 1810 nfnetlink_subsys_unregister(&nfqnl_subsys); 1811 netlink_unregister_notifier(&nfqnl_rtnl_notifier); 1812 unregister_pernet_subsys(&nfnl_queue_net_ops); 1813 1814 rcu_barrier(); /* Wait for completion of call_rcu()'s */ 1815 } 1816 1817 MODULE_DESCRIPTION("netfilter packet queue handler"); 1818 MODULE_AUTHOR("Harald Welte <[email protected]>"); 1819 MODULE_LICENSE("GPL"); 1820 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE); 1821 1822 module_init(nfnetlink_queue_init); 1823 module_exit(nfnetlink_queue_fini); 1824