1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Tap functions for AF_VSOCK sockets.
5 * Code based on net/netlink/af_netlink.c tap functions.
8 #include <linux/module.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
))
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
);
30 EXPORT_SYMBOL_GPL(vsock_add_tap
);
32 int vsock_remove_tap(struct vsock_tap
*vt
)
34 struct vsock_tap
*tmp
;
37 spin_lock(&vsock_tap_lock
);
39 list_for_each_entry(tmp
, &vsock_tap_all
, list
) {
41 list_del_rcu(&vt
->list
);
47 pr_warn("vsock_remove_tap: %p not found\n", vt
);
49 spin_unlock(&vsock_tap_lock
);
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
)
64 struct sk_buff
*nskb
= skb_clone(skb
, GFP_ATOMIC
);
70 ret
= dev_queue_xmit(nskb
);
71 if (unlikely(ret
> 0))
72 ret
= net_xmit_errno(ret
);
80 static void __vsock_deliver_tap(struct sk_buff
*skb
)
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
);
92 void vsock_deliver_tap(struct sk_buff
*build_skb(void *opaque
), void *opaque
)
98 if (likely(list_empty(&vsock_tap_all
)))
101 skb
= build_skb(opaque
);
103 __vsock_deliver_tap(skb
);
110 EXPORT_SYMBOL_GPL(vsock_deliver_tap
);