nf_dup_netdev.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015 Pablo Neira Ayuso <pablo@netfilter.org>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/netlink.h>
  9. #include <linux/netfilter.h>
  10. #include <linux/netfilter/nf_tables.h>
  11. #include <net/netfilter/nf_tables.h>
  12. #include <net/netfilter/nf_tables_offload.h>
  13. #include <net/netfilter/nf_dup_netdev.h>
  14. static void nf_do_netdev_egress(struct sk_buff *skb, struct net_device *dev)
  15. {
  16. if (skb_mac_header_was_set(skb))
  17. skb_push(skb, skb->mac_len);
  18. skb->dev = dev;
  19. skb->tstamp = 0;
  20. dev_queue_xmit(skb);
  21. }
  22. void nf_fwd_netdev_egress(const struct nft_pktinfo *pkt, int oif)
  23. {
  24. struct net_device *dev;
  25. dev = dev_get_by_index_rcu(nft_net(pkt), oif);
  26. if (!dev) {
  27. kfree_skb(pkt->skb);
  28. return;
  29. }
  30. nf_do_netdev_egress(pkt->skb, dev);
  31. }
  32. EXPORT_SYMBOL_GPL(nf_fwd_netdev_egress);
  33. void nf_dup_netdev_egress(const struct nft_pktinfo *pkt, int oif)
  34. {
  35. struct net_device *dev;
  36. struct sk_buff *skb;
  37. dev = dev_get_by_index_rcu(nft_net(pkt), oif);
  38. if (dev == NULL)
  39. return;
  40. skb = skb_clone(pkt->skb, GFP_ATOMIC);
  41. if (skb)
  42. nf_do_netdev_egress(skb, dev);
  43. }
  44. EXPORT_SYMBOL_GPL(nf_dup_netdev_egress);
  45. int nft_fwd_dup_netdev_offload(struct nft_offload_ctx *ctx,
  46. struct nft_flow_rule *flow,
  47. enum flow_action_id id, int oif)
  48. {
  49. struct flow_action_entry *entry;
  50. struct net_device *dev;
  51. /* nft_flow_rule_destroy() releases the reference on this device. */
  52. dev = dev_get_by_index(ctx->net, oif);
  53. if (!dev)
  54. return -EOPNOTSUPP;
  55. entry = &flow->rule->action.entries[ctx->num_actions++];
  56. entry->id = id;
  57. entry->dev = dev;
  58. return 0;
  59. }
  60. EXPORT_SYMBOL_GPL(nft_fwd_dup_netdev_offload);
  61. MODULE_LICENSE("GPL");
  62. MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
  63. MODULE_DESCRIPTION("Netfilter packet duplication support");