Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3) #include <linux/if_tap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4) #include <linux/if_vlan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/nsproxy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/if_tun.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/virtio_net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/skb_array.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #define TAP_IFFEATURES (IFF_VNET_HDR | IFF_MULTI_QUEUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #define TAP_VNET_LE 0x80000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define TAP_VNET_BE 0x40000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #ifdef CONFIG_TUN_VNET_CROSS_LE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) static inline bool tap_legacy_is_little_endian(struct tap_queue *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	return q->flags & TAP_VNET_BE ? false :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 		virtio_legacy_is_little_endian();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) static long tap_get_vnet_be(struct tap_queue *q, int __user *sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	int s = !!(q->flags & TAP_VNET_BE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	if (put_user(s, sp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) static long tap_set_vnet_be(struct tap_queue *q, int __user *sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	int s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	if (get_user(s, sp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	if (s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 		q->flags |= TAP_VNET_BE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 		q->flags &= ~TAP_VNET_BE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static inline bool tap_legacy_is_little_endian(struct tap_queue *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	return virtio_legacy_is_little_endian();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) static long tap_get_vnet_be(struct tap_queue *q, int __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) static long tap_set_vnet_be(struct tap_queue *q, int __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #endif /* CONFIG_TUN_VNET_CROSS_LE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) static inline bool tap_is_little_endian(struct tap_queue *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	return q->flags & TAP_VNET_LE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 		tap_legacy_is_little_endian(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) static inline u16 tap16_to_cpu(struct tap_queue *q, __virtio16 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	return __virtio16_to_cpu(tap_is_little_endian(q), val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) static inline __virtio16 cpu_to_tap16(struct tap_queue *q, u16 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	return __cpu_to_virtio16(tap_is_little_endian(q), val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) static struct proto tap_proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	.name = "tap",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	.obj_size = sizeof(struct tap_queue),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) #define TAP_NUM_DEVS (1U << MINORBITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) static LIST_HEAD(major_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) struct major_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	dev_t major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	struct idr minor_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	spinlock_t minor_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	const char *device_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	struct list_head next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) #define GOODCOPY_LEN 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) static const struct proto_ops tap_socket_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) #define RX_OFFLOADS (NETIF_F_GRO | NETIF_F_LRO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) #define TAP_FEATURES (NETIF_F_GSO | NETIF_F_SG | NETIF_F_FRAGLIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) static struct tap_dev *tap_dev_get_rcu(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	return rcu_dereference(dev->rx_handler_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128)  * RCU usage:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129)  * The tap_queue and the macvlan_dev are loosely coupled, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130)  * pointers from one to the other can only be read while rcu_read_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131)  * or rtnl is held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133)  * Both the file and the macvlan_dev hold a reference on the tap_queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134)  * through sock_hold(&q->sk). When the macvlan_dev goes away first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135)  * q->vlan becomes inaccessible. When the files gets closed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136)  * tap_get_queue() fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138)  * There may still be references to the struct sock inside of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139)  * queue from outbound SKBs, but these never reference back to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140)  * file or the dev. The data structure is freed through __sk_free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141)  * when both our references and any pending SKBs are gone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) static int tap_enable_queue(struct tap_dev *tap, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 			    struct tap_queue *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	if (q->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	rcu_assign_pointer(tap->taps[tap->numvtaps], q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	q->queue_index = tap->numvtaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	q->enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	tap->numvtaps++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) /* Requires RTNL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) static int tap_set_queue(struct tap_dev *tap, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 			 struct tap_queue *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	if (tap->numqueues == MAX_TAP_QUEUES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	rcu_assign_pointer(q->tap, tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	rcu_assign_pointer(tap->taps[tap->numvtaps], q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	sock_hold(&q->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	q->file = file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	q->queue_index = tap->numvtaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	q->enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	file->private_data = q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	list_add_tail(&q->next, &tap->queue_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	tap->numvtaps++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	tap->numqueues++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) static int tap_disable_queue(struct tap_queue *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	struct tap_dev *tap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	struct tap_queue *nq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	if (!q->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	tap = rtnl_dereference(q->tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	if (tap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		int index = q->queue_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		BUG_ON(index >= tap->numvtaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		nq = rtnl_dereference(tap->taps[tap->numvtaps - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		nq->queue_index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		rcu_assign_pointer(tap->taps[index], nq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		RCU_INIT_POINTER(tap->taps[tap->numvtaps - 1], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		q->enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		tap->numvtaps--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215)  * The file owning the queue got closed, give up both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216)  * the reference that the files holds as well as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217)  * one from the macvlan_dev if that still exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219)  * Using the spinlock makes sure that we don't get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220)  * to the queue again after destroying it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) static void tap_put_queue(struct tap_queue *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	struct tap_dev *tap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	tap = rtnl_dereference(q->tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	if (tap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		if (q->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 			BUG_ON(tap_disable_queue(q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		tap->numqueues--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		RCU_INIT_POINTER(q->tap, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		sock_put(&q->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		list_del_init(&q->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	sock_put(&q->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246)  * Select a queue based on the rxq of the device on which this packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247)  * arrived. If the incoming device is not mq, calculate a flow hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248)  * to select a queue. If all fails, find the first available queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249)  * Cache vlan->numvtaps since it can become zero during the execution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250)  * of this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) static struct tap_queue *tap_get_queue(struct tap_dev *tap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 				       struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	struct tap_queue *queue = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	/* Access to taps array is protected by rcu, but access to numvtaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	 * isn't. Below we use it to lookup a queue, but treat it as a hint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	 * and validate that the result isn't NULL - in case we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	 * racing against queue removal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	int numvtaps = READ_ONCE(tap->numvtaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	__u32 rxq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	if (!numvtaps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	if (numvtaps == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		goto single;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	/* Check if we can use flow to select a queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	rxq = skb_get_hash(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	if (rxq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		queue = rcu_dereference(tap->taps[rxq % numvtaps]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	if (likely(skb_rx_queue_recorded(skb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		rxq = skb_get_rx_queue(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		while (unlikely(rxq >= numvtaps))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 			rxq -= numvtaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		queue = rcu_dereference(tap->taps[rxq]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) single:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	queue = rcu_dereference(tap->taps[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	return queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294)  * The net_device is going away, give up the reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295)  * that it holds on all queues and safely set the pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296)  * from the queues to NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) void tap_del_queues(struct tap_dev *tap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	struct tap_queue *q, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	list_for_each_entry_safe(q, tmp, &tap->queue_list, next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		list_del_init(&q->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		RCU_INIT_POINTER(q->tap, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		if (q->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 			tap->numvtaps--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		tap->numqueues--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		sock_put(&q->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	BUG_ON(tap->numvtaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	BUG_ON(tap->numqueues);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	/* guarantee that any future tap_set_queue will fail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	tap->numvtaps = MAX_TAP_QUEUES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) EXPORT_SYMBOL_GPL(tap_del_queues);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) rx_handler_result_t tap_handle_frame(struct sk_buff **pskb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	struct sk_buff *skb = *pskb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	struct net_device *dev = skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	struct tap_dev *tap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	struct tap_queue *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	netdev_features_t features = TAP_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	tap = tap_dev_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	if (!tap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		return RX_HANDLER_PASS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	q = tap_get_queue(tap, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	if (!q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		return RX_HANDLER_PASS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	skb_push(skb, ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	/* Apply the forward feature mask so that we perform segmentation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	 * according to users wishes.  This only works if VNET_HDR is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	 * enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	if (q->flags & IFF_VNET_HDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		features |= tap->tap_features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	if (netif_needs_gso(skb, features)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		struct sk_buff *segs = __skb_gso_segment(skb, features, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		struct sk_buff *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		if (IS_ERR(segs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		if (!segs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 			if (ptr_ring_produce(&q->ring, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 				goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 			goto wake_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		skb_list_walk_safe(segs, skb, next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 			skb_mark_not_on_list(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 			if (ptr_ring_produce(&q->ring, skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 				kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 				kfree_skb_list(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		/* If we receive a partial checksum and the tap side
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		 * doesn't support checksum offload, compute the checksum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		 * Note: it doesn't matter which checksum feature to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		 *	  check, we either support them all or none.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		if (skb->ip_summed == CHECKSUM_PARTIAL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		    !(features & NETIF_F_CSUM_MASK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		    skb_checksum_help(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		if (ptr_ring_produce(&q->ring, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) wake_up:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	wake_up_interruptible_poll(sk_sleep(&q->sk), EPOLLIN | EPOLLRDNORM | EPOLLRDBAND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	return RX_HANDLER_CONSUMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	/* Count errors/drops only here, thus don't care about args. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	if (tap->count_rx_dropped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		tap->count_rx_dropped(tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	return RX_HANDLER_CONSUMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) EXPORT_SYMBOL_GPL(tap_handle_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) static struct major_info *tap_get_major(int major)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	struct major_info *tap_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	list_for_each_entry_rcu(tap_major, &major_list, next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		if (tap_major->major == major)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 			return tap_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) int tap_get_minor(dev_t major, struct tap_dev *tap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	int retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	struct major_info *tap_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	tap_major = tap_get_major(MAJOR(major));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	if (!tap_major) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	spin_lock(&tap_major->minor_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	retval = idr_alloc(&tap_major->minor_idr, tap, 1, TAP_NUM_DEVS, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	if (retval >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		tap->minor = retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	} else if (retval == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		netdev_err(tap->dev, "Too many tap devices\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	spin_unlock(&tap_major->minor_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	return retval < 0 ? retval : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) EXPORT_SYMBOL_GPL(tap_get_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) void tap_free_minor(dev_t major, struct tap_dev *tap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	struct major_info *tap_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	tap_major = tap_get_major(MAJOR(major));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	if (!tap_major) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	spin_lock(&tap_major->minor_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	if (tap->minor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		idr_remove(&tap_major->minor_idr, tap->minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		tap->minor = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	spin_unlock(&tap_major->minor_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) EXPORT_SYMBOL_GPL(tap_free_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) static struct tap_dev *dev_get_by_tap_file(int major, int minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	struct net_device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	struct tap_dev *tap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	struct major_info *tap_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	tap_major = tap_get_major(major);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	if (!tap_major) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		tap = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	spin_lock(&tap_major->minor_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	tap = idr_find(&tap_major->minor_idr, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	if (tap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		dev = tap->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		dev_hold(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	spin_unlock(&tap_major->minor_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	return tap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) static void tap_sock_write_space(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	wait_queue_head_t *wqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	if (!sock_writeable(sk) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	    !test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	wqueue = sk_sleep(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	if (wqueue && waitqueue_active(wqueue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		wake_up_interruptible_poll(wqueue, EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) static void tap_sock_destruct(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	struct tap_queue *q = container_of(sk, struct tap_queue, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	ptr_ring_cleanup(&q->ring, __skb_array_destroy_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) static int tap_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	struct net *net = current->nsproxy->net_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	struct tap_dev *tap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	struct tap_queue *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	int err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	tap = dev_get_by_tap_file(imajor(inode), iminor(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	if (!tap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	q = (struct tap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 					     &tap_proto, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	if (!q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	if (ptr_ring_init(&q->ring, tap->dev->tx_queue_len, GFP_KERNEL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		sk_free(&q->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	init_waitqueue_head(&q->sock.wq.wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	q->sock.type = SOCK_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	q->sock.state = SS_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	q->sock.file = file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	q->sock.ops = &tap_socket_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	sock_init_data(&q->sock, &q->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	q->sk.sk_write_space = tap_sock_write_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	q->sk.sk_destruct = tap_sock_destruct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	 * so far only KVM virtio_net uses tap, enable zero copy between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	 * guest kernel and host kernel when lower device supports zerocopy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	 * The macvlan supports zerocopy iff the lower device supports zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	 * copy so we don't have to look at the lower device directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	if ((tap->dev->features & NETIF_F_HIGHDMA) && (tap->dev->features & NETIF_F_SG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		sock_set_flag(&q->sk, SOCK_ZEROCOPY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	err = tap_set_queue(tap, file, q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		/* tap_sock_destruct() will take care of freeing ptr_ring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	dev_put(tap->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) err_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	sock_put(&q->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	if (tap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		dev_put(tap->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) static int tap_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	struct tap_queue *q = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	tap_put_queue(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) static __poll_t tap_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	struct tap_queue *q = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	__poll_t mask = EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	if (!q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	poll_wait(file, &q->sock.wq.wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	if (!ptr_ring_empty(&q->ring))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	if (sock_writeable(&q->sk) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	    (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	     sock_writeable(&q->sk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		mask |= EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) static inline struct sk_buff *tap_alloc_skb(struct sock *sk, size_t prepad,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 					    size_t len, size_t linear,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 						int noblock, int *err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	/* Under a page?  Don't bother with paged skb. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	if (prepad + len < PAGE_SIZE || !linear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		linear = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 				   err, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	skb_reserve(skb, prepad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	skb_put(skb, linear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	skb->data_len = len - linear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	skb->len += len - linear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) /* Neighbour code has some assumptions on HH_DATA_MOD alignment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) #define TAP_RESERVE HH_DATA_OFF(ETH_HLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) /* Get packet from user space buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 			    struct iov_iter *from, int noblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	int good_linear = SKB_MAX_HEAD(TAP_RESERVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	struct tap_dev *tap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	unsigned long total_len = iov_iter_count(from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	unsigned long len = total_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	struct virtio_net_hdr vnet_hdr = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	int vnet_hdr_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	int copylen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	bool zerocopy = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	size_t linear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	if (q->flags & IFF_VNET_HDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		if (len < vnet_hdr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		len -= vnet_hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		if (!copy_from_iter_full(&vnet_hdr, sizeof(vnet_hdr), from))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		     tap16_to_cpu(q, vnet_hdr.csum_start) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		     tap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 			     tap16_to_cpu(q, vnet_hdr.hdr_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 			vnet_hdr.hdr_len = cpu_to_tap16(q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 				 tap16_to_cpu(q, vnet_hdr.csum_start) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 				 tap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		if (tap16_to_cpu(q, vnet_hdr.hdr_len) > len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	if (unlikely(len < ETH_HLEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	if (msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		struct iov_iter i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		copylen = vnet_hdr.hdr_len ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 			tap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		if (copylen > good_linear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 			copylen = good_linear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		else if (copylen < ETH_HLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 			copylen = ETH_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		linear = copylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		i = *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		iov_iter_advance(&i, copylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 			zerocopy = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	if (!zerocopy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		copylen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		linear = tap16_to_cpu(q, vnet_hdr.hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		if (linear > good_linear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 			linear = good_linear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		else if (linear < ETH_HLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 			linear = ETH_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	skb = tap_alloc_skb(&q->sk, TAP_RESERVE, copylen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 			    linear, noblock, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	if (zerocopy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		err = zerocopy_sg_from_iter(skb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		err = skb_copy_datagram_from_iter(skb, 0, from, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		goto err_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	skb_set_network_header(skb, ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	skb->protocol = eth_hdr(skb)->h_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	if (vnet_hdr_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		err = virtio_net_hdr_to_skb(skb, &vnet_hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 					    tap_is_little_endian(q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 			goto err_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	skb_probe_transport_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	/* Move network header to the right position for VLAN tagged packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	if ((skb->protocol == htons(ETH_P_8021Q) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	     skb->protocol == htons(ETH_P_8021AD)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	    __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		skb_set_network_header(skb, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	tap = rcu_dereference(q->tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	/* copy skb_ubuf_info for callback when skb has no error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	if (zerocopy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		skb_shinfo(skb)->destructor_arg = msg_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	} else if (msg_control) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		struct ubuf_info *uarg = msg_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		uarg->callback(uarg, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	if (tap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		skb->dev = tap->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		dev_queue_xmit(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	return total_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) err_kfree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	tap = rcu_dereference(q->tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	if (tap && tap->count_tx_dropped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		tap->count_tx_dropped(tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) static ssize_t tap_write_iter(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	struct tap_queue *q = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	return tap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) /* Put packet to the user space buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) static ssize_t tap_put_user(struct tap_queue *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 			    const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 			    struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	int vnet_hdr_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	int vlan_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	int total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	if (q->flags & IFF_VNET_HDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		struct virtio_net_hdr vnet_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		if (iov_iter_count(iter) < vnet_hdr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		if (virtio_net_hdr_from_skb(skb, &vnet_hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 					    tap_is_little_endian(q), true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 					    vlan_hlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		    sizeof(vnet_hdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	total = vnet_hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	total += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	if (skb_vlan_tag_present(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 			__be16 h_vlan_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 			__be16 h_vlan_TCI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		} veth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		veth.h_vlan_proto = skb->vlan_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		total += VLAN_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		if (ret || !iov_iter_count(iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		ret = copy_to_iter(&veth, sizeof(veth), iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		if (ret != sizeof(veth) || !iov_iter_count(iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 				     skb->len - vlan_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	return ret ? ret : total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) static ssize_t tap_do_read(struct tap_queue *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 			   struct iov_iter *to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 			   int noblock, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	ssize_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (!iov_iter_count(to)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	if (skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		goto put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		if (!noblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			prepare_to_wait(sk_sleep(&q->sk), &wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 					TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		/* Read frames from the queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		skb = ptr_ring_consume(&q->ring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		if (skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		if (noblock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 			ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 			ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		/* Nothing to read, let's sleep */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	if (!noblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		finish_wait(sk_sleep(&q->sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	if (skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		ret = tap_put_user(q, skb, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		if (unlikely(ret < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 			kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 			consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) static ssize_t tap_read_iter(struct kiocb *iocb, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	struct tap_queue *q = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	ssize_t len = iov_iter_count(to), ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	ret = tap_do_read(q, to, file->f_flags & O_NONBLOCK, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	ret = min_t(ssize_t, ret, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		iocb->ki_pos = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) static struct tap_dev *tap_get_tap_dev(struct tap_queue *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	struct tap_dev *tap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	tap = rtnl_dereference(q->tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	if (tap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		dev_hold(tap->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	return tap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) static void tap_put_tap_dev(struct tap_dev *tap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	dev_put(tap->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) static int tap_ioctl_set_queue(struct file *file, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	struct tap_queue *q = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	struct tap_dev *tap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	tap = tap_get_tap_dev(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	if (!tap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	if (flags & IFF_ATTACH_QUEUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		ret = tap_enable_queue(tap, file, q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	else if (flags & IFF_DETACH_QUEUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		ret = tap_disable_queue(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	tap_put_tap_dev(tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) static int set_offload(struct tap_queue *q, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	struct tap_dev *tap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	netdev_features_t features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	netdev_features_t feature_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	tap = rtnl_dereference(q->tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	if (!tap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		return -ENOLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	features = tap->dev->features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	if (arg & TUN_F_CSUM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		feature_mask = NETIF_F_HW_CSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 			if (arg & TUN_F_TSO_ECN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 				feature_mask |= NETIF_F_TSO_ECN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 			if (arg & TUN_F_TSO4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 				feature_mask |= NETIF_F_TSO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 			if (arg & TUN_F_TSO6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 				feature_mask |= NETIF_F_TSO6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	/* tun/tap driver inverts the usage for TSO offloads, where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	 * setting the TSO bit means that the userspace wants to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	 * accept TSO frames and turning it off means that user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	 * does not support TSO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	 * For tap, we have to invert it to mean the same thing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	 * When user space turns off TSO, we turn off GSO/LRO so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	 * user-space will not receive TSO frames.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		features |= RX_OFFLOADS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		features &= ~RX_OFFLOADS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	/* tap_features are the same as features on tun/tap and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	 * reflect user expectations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	tap->tap_features = feature_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	if (tap->update_features)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		tap->update_features(tap, features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972)  * provide compatibility with generic tun/tap interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) static long tap_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		      unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	struct tap_queue *q = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	struct tap_dev *tap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	struct ifreq __user *ifr = argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	unsigned int __user *up = argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	unsigned short u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	int __user *sp = argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	struct sockaddr sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	int s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	case TUNSETIFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		/* ignore the name, just look at flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		if (get_user(u, &ifr->ifr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		if ((u & ~TAP_IFFEATURES) != (IFF_NO_PI | IFF_TAP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 			q->flags = (q->flags & ~TAP_IFFEATURES) | u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	case TUNGETIFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		tap = tap_get_tap_dev(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		if (!tap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 			rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 			return -ENOLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		u = q->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		    put_user(u, &ifr->ifr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		tap_put_tap_dev(tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	case TUNSETQUEUE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		if (get_user(u, &ifr->ifr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		ret = tap_ioctl_set_queue(file, u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	case TUNGETFEATURES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		if (put_user(IFF_TAP | IFF_NO_PI | TAP_IFFEATURES, up))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	case TUNSETSNDBUF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		if (get_user(s, sp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		if (s <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		q->sk.sk_sndbuf = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	case TUNGETVNETHDRSZ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		s = q->vnet_hdr_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		if (put_user(s, sp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	case TUNSETVNETHDRSZ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		if (get_user(s, sp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		if (s < (int)sizeof(struct virtio_net_hdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		q->vnet_hdr_sz = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	case TUNGETVNETLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		s = !!(q->flags & TAP_VNET_LE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		if (put_user(s, sp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	case TUNSETVNETLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		if (get_user(s, sp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		if (s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 			q->flags |= TAP_VNET_LE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 			q->flags &= ~TAP_VNET_LE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	case TUNGETVNETBE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		return tap_get_vnet_be(q, sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	case TUNSETVNETBE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		return tap_set_vnet_be(q, sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	case TUNSETOFFLOAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		/* let the user check for future flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 			    TUN_F_TSO_ECN | TUN_F_UFO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		ret = set_offload(q, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	case SIOCGIFHWADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		tap = tap_get_tap_dev(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		if (!tap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 			rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			return -ENOLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		dev_get_mac_address(&sa, dev_net(tap->dev), tap->dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		    copy_to_user(&ifr->ifr_hwaddr, &sa, sizeof(sa)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		tap_put_tap_dev(tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	case SIOCSIFHWADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		tap = tap_get_tap_dev(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		if (!tap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 			rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 			return -ENOLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		ret = dev_set_mac_address_user(tap->dev, &sa, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		tap_put_tap_dev(tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) static const struct file_operations tap_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	.open		= tap_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	.release	= tap_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	.read_iter	= tap_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	.write_iter	= tap_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	.poll		= tap_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	.llseek		= no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	.unlocked_ioctl	= tap_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	.compat_ioctl	= compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	struct tun_xdp_hdr *hdr = xdp->data_hard_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	struct virtio_net_hdr *gso = &hdr->gso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	int buflen = hdr->buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	int vnet_hdr_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	struct tap_dev *tap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	int err, depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	if (q->flags & IFF_VNET_HDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	skb = build_skb(xdp->data_hard_start, buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	skb_reserve(skb, xdp->data - xdp->data_hard_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	skb_put(skb, xdp->data_end - xdp->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	skb_set_network_header(skb, ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	skb->protocol = eth_hdr(skb)->h_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	if (vnet_hdr_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		err = virtio_net_hdr_to_skb(skb, gso, tap_is_little_endian(q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 			goto err_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	/* Move network header to the right position for VLAN tagged packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	if ((skb->protocol == htons(ETH_P_8021Q) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	     skb->protocol == htons(ETH_P_8021AD)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	    __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		skb_set_network_header(skb, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	tap = rcu_dereference(q->tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	if (tap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		skb->dev = tap->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		skb_probe_transport_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		dev_queue_xmit(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) err_kfree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	tap = rcu_dereference(q->tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	if (tap && tap->count_tx_dropped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		tap->count_tx_dropped(tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) static int tap_sendmsg(struct socket *sock, struct msghdr *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		       size_t total_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	struct tap_queue *q = container_of(sock, struct tap_queue, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	struct tun_msg_ctl *ctl = m->msg_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	struct xdp_buff *xdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	if (ctl && (ctl->type == TUN_MSG_PTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		for (i = 0; i < ctl->num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 			xdp = &((struct xdp_buff *)ctl->ptr)[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 			tap_get_user_xdp(q, xdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	return tap_get_user(q, ctl ? ctl->ptr : NULL, &m->msg_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			    m->msg_flags & MSG_DONTWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) static int tap_recvmsg(struct socket *sock, struct msghdr *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		       size_t total_len, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	struct tap_queue *q = container_of(sock, struct tap_queue, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	struct sk_buff *skb = m->msg_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	if (ret > total_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		m->msg_flags |= MSG_TRUNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		ret = flags & MSG_TRUNC ? ret : total_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) static int tap_peek_len(struct socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	struct tap_queue *q = container_of(sock, struct tap_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 					       sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	return PTR_RING_PEEK_CALL(&q->ring, __skb_array_len_with_tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) /* Ops structure to mimic raw sockets with tun */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) static const struct proto_ops tap_socket_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	.sendmsg = tap_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	.recvmsg = tap_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	.peek_len = tap_peek_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) /* Get an underlying socket object from tun file.  Returns error unless file is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)  * attached to a device.  The returned object works like a packet socket, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)  * holding a reference to the file for as long as the socket is in use. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) struct socket *tap_get_socket(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	struct tap_queue *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	if (file->f_op != &tap_fops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	q = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	if (!q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		return ERR_PTR(-EBADFD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	return &q->sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) EXPORT_SYMBOL_GPL(tap_get_socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) struct ptr_ring *tap_get_ptr_ring(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	struct tap_queue *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	if (file->f_op != &tap_fops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	q = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	if (!q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		return ERR_PTR(-EBADFD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	return &q->ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) EXPORT_SYMBOL_GPL(tap_get_ptr_ring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) int tap_queue_resize(struct tap_dev *tap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	struct net_device *dev = tap->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	struct tap_queue *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	struct ptr_ring **rings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	int n = tap->numqueues;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	int ret, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	if (!rings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	list_for_each_entry(q, &tap->queue_list, next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		rings[i++] = &q->ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	ret = ptr_ring_resize_multiple(rings, n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 				       dev->tx_queue_len, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 				       __skb_array_destroy_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	kfree(rings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) EXPORT_SYMBOL_GPL(tap_queue_resize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) static int tap_list_add(dev_t major, const char *device_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	struct major_info *tap_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	tap_major = kzalloc(sizeof(*tap_major), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	if (!tap_major)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	tap_major->major = MAJOR(major);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	idr_init(&tap_major->minor_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	spin_lock_init(&tap_major->minor_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	tap_major->device_name = device_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	list_add_tail_rcu(&tap_major->next, &major_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) int tap_create_cdev(struct cdev *tap_cdev, dev_t *tap_major,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		    const char *device_name, struct module *module)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	err = alloc_chrdev_region(tap_major, 0, TAP_NUM_DEVS, device_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	cdev_init(tap_cdev, &tap_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	tap_cdev->owner = module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 		goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	err =  tap_list_add(*tap_major, device_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		goto out3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) out3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	cdev_del(tap_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	unregister_chrdev_region(*tap_major, TAP_NUM_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) EXPORT_SYMBOL_GPL(tap_create_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	struct major_info *tap_major, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	cdev_del(tap_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	unregister_chrdev_region(major, TAP_NUM_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	list_for_each_entry_safe(tap_major, tmp, &major_list, next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		if (tap_major->major == MAJOR(major)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 			idr_destroy(&tap_major->minor_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 			list_del_rcu(&tap_major->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 			kfree_rcu(tap_major, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) EXPORT_SYMBOL_GPL(tap_destroy_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) MODULE_AUTHOR("Sainath Grandhi <sainath.grandhi@intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) MODULE_LICENSE("GPL");