WIP FPC-III support
[linux/fpc-iii.git] / net / vmw_vsock / af_vsock_tap.c
blob30ee7e4fe25115cef71b43f335862a09b8c51382
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Tap functions for AF_VSOCK sockets.
5 * Code based on net/netlink/af_netlink.c tap functions.
6 */
8 #include <linux/module.h>
9 #include <net/sock.h>
10 #include <net/af_vsock.h>
11 #include <linux/if_arp.h>
13 static DEFINE_SPINLOCK(vsock_tap_lock);
14 static struct list_head vsock_tap_all __read_mostly =
15 LIST_HEAD_INIT(vsock_tap_all);
17 int vsock_add_tap(struct vsock_tap *vt)
19 if (unlikely(vt->dev->type != ARPHRD_VSOCKMON))
20 return -EINVAL;
22 __module_get(vt->module);
24 spin_lock(&vsock_tap_lock);
25 list_add_rcu(&vt->list, &vsock_tap_all);
26 spin_unlock(&vsock_tap_lock);
28 return 0;
30 EXPORT_SYMBOL_GPL(vsock_add_tap);
32 int vsock_remove_tap(struct vsock_tap *vt)
34 struct vsock_tap *tmp;
35 bool found = false;
37 spin_lock(&vsock_tap_lock);
39 list_for_each_entry(tmp, &vsock_tap_all, list) {
40 if (vt == tmp) {
41 list_del_rcu(&vt->list);
42 found = true;
43 goto out;
47 pr_warn("vsock_remove_tap: %p not found\n", vt);
48 out:
49 spin_unlock(&vsock_tap_lock);
51 synchronize_net();
53 if (found)
54 module_put(vt->module);
56 return found ? 0 : -ENODEV;
58 EXPORT_SYMBOL_GPL(vsock_remove_tap);
60 static int __vsock_deliver_tap_skb(struct sk_buff *skb,
61 struct net_device *dev)
63 int ret = 0;
64 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
66 if (nskb) {
67 dev_hold(dev);
69 nskb->dev = dev;
70 ret = dev_queue_xmit(nskb);
71 if (unlikely(ret > 0))
72 ret = net_xmit_errno(ret);
74 dev_put(dev);
77 return ret;
80 static void __vsock_deliver_tap(struct sk_buff *skb)
82 int ret;
83 struct vsock_tap *tmp;
85 list_for_each_entry_rcu(tmp, &vsock_tap_all, list) {
86 ret = __vsock_deliver_tap_skb(skb, tmp->dev);
87 if (unlikely(ret))
88 break;
92 void vsock_deliver_tap(struct sk_buff *build_skb(void *opaque), void *opaque)
94 struct sk_buff *skb;
96 rcu_read_lock();
98 if (likely(list_empty(&vsock_tap_all)))
99 goto out;
101 skb = build_skb(opaque);
102 if (skb) {
103 __vsock_deliver_tap(skb);
104 consume_skb(skb);
107 out:
108 rcu_read_unlock();
110 EXPORT_SYMBOL_GPL(vsock_deliver_tap);