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