hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/net/tap.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 #include <linux/etherdevice.h>
23 #include <linux/if_tap.h>
34 #include <linux/if_vlan.h>
....@@ -340,6 +341,7 @@
340341 features |= tap->tap_features;
341342 if (netif_needs_gso(skb, features)) {
342343 struct sk_buff *segs = __skb_gso_segment(skb, features, false);
344
+ struct sk_buff *next;
343345
344346 if (IS_ERR(segs))
345347 goto drop;
....@@ -351,16 +353,13 @@
351353 }
352354
353355 consume_skb(skb);
354
- while (segs) {
355
- struct sk_buff *nskb = segs->next;
356
-
357
- segs->next = NULL;
358
- if (ptr_ring_produce(&q->ring, segs)) {
359
- kfree_skb(segs);
360
- kfree_skb_list(nskb);
356
+ skb_list_walk_safe(segs, skb, next) {
357
+ skb_mark_not_on_list(skb);
358
+ if (ptr_ring_produce(&q->ring, skb)) {
359
+ kfree_skb(skb);
360
+ kfree_skb_list(next);
361361 break;
362362 }
363
- segs = nskb;
364363 }
365364 } else {
366365 /* If we receive a partial checksum and the tap side
....@@ -519,8 +518,7 @@
519518 goto err;
520519 }
521520
522
- RCU_INIT_POINTER(q->sock.wq, &q->wq);
523
- init_waitqueue_head(&q->wq.wait);
521
+ init_waitqueue_head(&q->sock.wq.wait);
524522 q->sock.type = SOCK_RAW;
525523 q->sock.state = SS_CONNECTED;
526524 q->sock.file = file;
....@@ -578,7 +576,7 @@
578576 goto out;
579577
580578 mask = 0;
581
- poll_wait(file, &q->wq.wait, wait);
579
+ poll_wait(file, &q->sock.wq.wait, wait);
582580
583581 if (!ptr_ring_empty(&q->ring))
584582 mask |= EPOLLIN | EPOLLRDNORM;
....@@ -619,7 +617,7 @@
619617 #define TAP_RESERVE HH_DATA_OFF(ETH_HLEN)
620618
621619 /* Get packet from user space buffer */
622
-static ssize_t tap_get_user(struct tap_queue *q, struct msghdr *m,
620
+static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
623621 struct iov_iter *from, int noblock)
624622 {
625623 int good_linear = SKB_MAX_HEAD(TAP_RESERVE);
....@@ -663,7 +661,7 @@
663661 if (unlikely(len < ETH_HLEN))
664662 goto err;
665663
666
- if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
664
+ if (msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
667665 struct iov_iter i;
668666
669667 copylen = vnet_hdr.hdr_len ?
....@@ -712,7 +710,7 @@
712710 goto err_kfree;
713711 }
714712
715
- skb_probe_transport_header(skb, ETH_HLEN);
713
+ skb_probe_transport_header(skb);
716714
717715 /* Move network header to the right position for VLAN tagged packets */
718716 if ((skb->protocol == htons(ETH_P_8021Q) ||
....@@ -724,11 +722,11 @@
724722 tap = rcu_dereference(q->tap);
725723 /* copy skb_ubuf_info for callback when skb has no error */
726724 if (zerocopy) {
727
- skb_shinfo(skb)->destructor_arg = m->msg_control;
725
+ skb_shinfo(skb)->destructor_arg = msg_control;
728726 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
729727 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
730
- } else if (m && m->msg_control) {
731
- struct ubuf_info *uarg = m->msg_control;
728
+ } else if (msg_control) {
729
+ struct ubuf_info *uarg = msg_control;
732730 uarg->callback(uarg, false);
733731 }
734732
....@@ -830,8 +828,7 @@
830828 ssize_t ret = 0;
831829
832830 if (!iov_iter_count(to)) {
833
- if (skb)
834
- kfree_skb(skb);
831
+ kfree_skb(skb);
835832 return 0;
836833 }
837834
....@@ -1096,10 +1093,9 @@
10961093 return -ENOLINK;
10971094 }
10981095 ret = 0;
1099
- u = tap->dev->type;
1096
+ dev_get_mac_address(&sa, dev_net(tap->dev), tap->dev->name);
11001097 if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
1101
- copy_to_user(&ifr->ifr_hwaddr.sa_data, tap->dev->dev_addr, ETH_ALEN) ||
1102
- put_user(u, &ifr->ifr_hwaddr.sa_family))
1098
+ copy_to_user(&ifr->ifr_hwaddr, &sa, sizeof(sa)))
11031099 ret = -EFAULT;
11041100 tap_put_tap_dev(tap);
11051101 rtnl_unlock();
....@@ -1114,7 +1110,7 @@
11141110 rtnl_unlock();
11151111 return -ENOLINK;
11161112 }
1117
- ret = dev_set_mac_address(tap->dev, &sa);
1113
+ ret = dev_set_mac_address_user(tap->dev, &sa, NULL);
11181114 tap_put_tap_dev(tap);
11191115 rtnl_unlock();
11201116 return ret;
....@@ -1123,14 +1119,6 @@
11231119 return -EINVAL;
11241120 }
11251121 }
1126
-
1127
-#ifdef CONFIG_COMPAT
1128
-static long tap_compat_ioctl(struct file *file, unsigned int cmd,
1129
- unsigned long arg)
1130
-{
1131
- return tap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1132
-}
1133
-#endif
11341122
11351123 static const struct file_operations tap_fops = {
11361124 .owner = THIS_MODULE,
....@@ -1141,16 +1129,90 @@
11411129 .poll = tap_poll,
11421130 .llseek = no_llseek,
11431131 .unlocked_ioctl = tap_ioctl,
1144
-#ifdef CONFIG_COMPAT
1145
- .compat_ioctl = tap_compat_ioctl,
1146
-#endif
1132
+ .compat_ioctl = compat_ptr_ioctl,
11471133 };
1134
+
1135
+static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)
1136
+{
1137
+ struct tun_xdp_hdr *hdr = xdp->data_hard_start;
1138
+ struct virtio_net_hdr *gso = &hdr->gso;
1139
+ int buflen = hdr->buflen;
1140
+ int vnet_hdr_len = 0;
1141
+ struct tap_dev *tap;
1142
+ struct sk_buff *skb;
1143
+ int err, depth;
1144
+
1145
+ if (q->flags & IFF_VNET_HDR)
1146
+ vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
1147
+
1148
+ skb = build_skb(xdp->data_hard_start, buflen);
1149
+ if (!skb) {
1150
+ err = -ENOMEM;
1151
+ goto err;
1152
+ }
1153
+
1154
+ skb_reserve(skb, xdp->data - xdp->data_hard_start);
1155
+ skb_put(skb, xdp->data_end - xdp->data);
1156
+
1157
+ skb_set_network_header(skb, ETH_HLEN);
1158
+ skb_reset_mac_header(skb);
1159
+ skb->protocol = eth_hdr(skb)->h_proto;
1160
+
1161
+ if (vnet_hdr_len) {
1162
+ err = virtio_net_hdr_to_skb(skb, gso, tap_is_little_endian(q));
1163
+ if (err)
1164
+ goto err_kfree;
1165
+ }
1166
+
1167
+ /* Move network header to the right position for VLAN tagged packets */
1168
+ if ((skb->protocol == htons(ETH_P_8021Q) ||
1169
+ skb->protocol == htons(ETH_P_8021AD)) &&
1170
+ __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
1171
+ skb_set_network_header(skb, depth);
1172
+
1173
+ rcu_read_lock();
1174
+ tap = rcu_dereference(q->tap);
1175
+ if (tap) {
1176
+ skb->dev = tap->dev;
1177
+ skb_probe_transport_header(skb);
1178
+ dev_queue_xmit(skb);
1179
+ } else {
1180
+ kfree_skb(skb);
1181
+ }
1182
+ rcu_read_unlock();
1183
+
1184
+ return 0;
1185
+
1186
+err_kfree:
1187
+ kfree_skb(skb);
1188
+err:
1189
+ rcu_read_lock();
1190
+ tap = rcu_dereference(q->tap);
1191
+ if (tap && tap->count_tx_dropped)
1192
+ tap->count_tx_dropped(tap);
1193
+ rcu_read_unlock();
1194
+ return err;
1195
+}
11481196
11491197 static int tap_sendmsg(struct socket *sock, struct msghdr *m,
11501198 size_t total_len)
11511199 {
11521200 struct tap_queue *q = container_of(sock, struct tap_queue, sock);
1153
- return tap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT);
1201
+ struct tun_msg_ctl *ctl = m->msg_control;
1202
+ struct xdp_buff *xdp;
1203
+ int i;
1204
+
1205
+ if (m->msg_controllen == sizeof(struct tun_msg_ctl) &&
1206
+ ctl && ctl->type == TUN_MSG_PTR) {
1207
+ for (i = 0; i < ctl->num; i++) {
1208
+ xdp = &((struct xdp_buff *)ctl->ptr)[i];
1209
+ tap_get_user_xdp(q, xdp);
1210
+ }
1211
+ return 0;
1212
+ }
1213
+
1214
+ return tap_get_user(q, ctl ? ctl->ptr : NULL, &m->msg_iter,
1215
+ m->msg_flags & MSG_DONTWAIT);
11541216 }
11551217
11561218 static int tap_recvmsg(struct socket *sock, struct msghdr *m,
....@@ -1160,8 +1222,7 @@
11601222 struct sk_buff *skb = m->msg_control;
11611223 int ret;
11621224 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
1163
- if (skb)
1164
- kfree_skb(skb);
1225
+ kfree_skb(skb);
11651226 return -EINVAL;
11661227 }
11671228 ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT, skb);