hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/net/packet/af_packet.c
....@@ -269,7 +269,8 @@
269269
270270 static bool packet_use_direct_xmit(const struct packet_sock *po)
271271 {
272
- return po->xmit == packet_direct_xmit;
272
+ /* Paired with WRITE_ONCE() in packet_setsockopt() */
273
+ return READ_ONCE(po->xmit) == packet_direct_xmit;
273274 }
274275
275276 static u16 packet_pick_tx_queue(struct sk_buff *skb)
....@@ -365,18 +366,20 @@
365366 {
366367 union tpacket_uhdr h;
367368
369
+ /* WRITE_ONCE() are paired with READ_ONCE() in __packet_get_status */
370
+
368371 h.raw = frame;
369372 switch (po->tp_version) {
370373 case TPACKET_V1:
371
- h.h1->tp_status = status;
374
+ WRITE_ONCE(h.h1->tp_status, status);
372375 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
373376 break;
374377 case TPACKET_V2:
375
- h.h2->tp_status = status;
378
+ WRITE_ONCE(h.h2->tp_status, status);
376379 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
377380 break;
378381 case TPACKET_V3:
379
- h.h3->tp_status = status;
382
+ WRITE_ONCE(h.h3->tp_status, status);
380383 flush_dcache_page(pgv_to_page(&h.h3->tp_status));
381384 break;
382385 default:
....@@ -393,17 +396,19 @@
393396
394397 smp_rmb();
395398
399
+ /* READ_ONCE() are paired with WRITE_ONCE() in __packet_set_status */
400
+
396401 h.raw = frame;
397402 switch (po->tp_version) {
398403 case TPACKET_V1:
399404 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
400
- return h.h1->tp_status;
405
+ return READ_ONCE(h.h1->tp_status);
401406 case TPACKET_V2:
402407 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
403
- return h.h2->tp_status;
408
+ return READ_ONCE(h.h2->tp_status);
404409 case TPACKET_V3:
405410 flush_dcache_page(pgv_to_page(&h.h3->tp_status));
406
- return h.h3->tp_status;
411
+ return READ_ONCE(h.h3->tp_status);
407412 default:
408413 WARN(1, "TPACKET version not supported.\n");
409414 BUG();
....@@ -1885,11 +1890,19 @@
18851890
18861891 static void packet_parse_headers(struct sk_buff *skb, struct socket *sock)
18871892 {
1893
+ int depth;
1894
+
18881895 if ((!skb->protocol || skb->protocol == htons(ETH_P_ALL)) &&
18891896 sock->type == SOCK_RAW) {
18901897 skb_reset_mac_header(skb);
18911898 skb->protocol = dev_parse_header_protocol(skb);
18921899 }
1900
+
1901
+ /* Move network header to the right position for VLAN tagged packets */
1902
+ if (likely(skb->dev->type == ARPHRD_ETHER) &&
1903
+ eth_type_vlan(skb->protocol) &&
1904
+ vlan_get_protocol_and_depth(skb, skb->protocol, &depth) != 0)
1905
+ skb_set_network_header(skb, depth);
18931906
18941907 skb_probe_transport_header(skb);
18951908 }
....@@ -1985,7 +1998,7 @@
19851998 goto retry;
19861999 }
19872000
1988
- if (!dev_validate_header(dev, skb->data, len)) {
2001
+ if (!dev_validate_header(dev, skb->data, len) || !skb->len) {
19892002 err = -EINVAL;
19902003 goto out_unlock;
19912004 }
....@@ -2135,7 +2148,7 @@
21352148 sll = &PACKET_SKB_CB(skb)->sa.ll;
21362149 sll->sll_hatype = dev->type;
21372150 sll->sll_pkttype = skb->pkt_type;
2138
- if (unlikely(po->origdev))
2151
+ if (unlikely(packet_sock_flag(po, PACKET_SOCK_ORIGDEV)))
21392152 sll->sll_ifindex = orig_dev->ifindex;
21402153 else
21412154 sll->sll_ifindex = dev->ifindex;
....@@ -2408,7 +2421,7 @@
24082421 sll->sll_hatype = dev->type;
24092422 sll->sll_protocol = skb->protocol;
24102423 sll->sll_pkttype = skb->pkt_type;
2411
- if (unlikely(po->origdev))
2424
+ if (unlikely(packet_sock_flag(po, PACKET_SOCK_ORIGDEV)))
24122425 sll->sll_ifindex = orig_dev->ifindex;
24132426 else
24142427 sll->sll_ifindex = dev->ifindex;
....@@ -2815,7 +2828,8 @@
28152828 packet_inc_pending(&po->tx_ring);
28162829
28172830 status = TP_STATUS_SEND_REQUEST;
2818
- err = po->xmit(skb);
2831
+ /* Paired with WRITE_ONCE() in packet_setsockopt() */
2832
+ err = READ_ONCE(po->xmit)(skb);
28192833 if (unlikely(err != 0)) {
28202834 if (err > 0)
28212835 err = net_xmit_errno(err);
....@@ -3005,6 +3019,11 @@
30053019 skb->mark = sockc.mark;
30063020 skb->tstamp = sockc.transmit_time;
30073021
3022
+ if (unlikely(extra_len == 4))
3023
+ skb->no_fcs = 1;
3024
+
3025
+ packet_parse_headers(skb, sock);
3026
+
30083027 if (has_vnet_hdr) {
30093028 err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
30103029 if (err)
....@@ -3013,12 +3032,8 @@
30133032 virtio_net_hdr_set_proto(skb, &vnet_hdr);
30143033 }
30153034
3016
- packet_parse_headers(skb, sock);
3017
-
3018
- if (unlikely(extra_len == 4))
3019
- skb->no_fcs = 1;
3020
-
3021
- err = po->xmit(skb);
3035
+ /* Paired with WRITE_ONCE() in packet_setsockopt() */
3036
+ err = READ_ONCE(po->xmit)(skb);
30223037 if (unlikely(err != 0)) {
30233038 if (err > 0)
30243039 err = net_xmit_errno(err);
....@@ -3146,6 +3161,9 @@
31463161
31473162 lock_sock(sk);
31483163 spin_lock(&po->bind_lock);
3164
+ if (!proto)
3165
+ proto = po->num;
3166
+
31493167 rcu_read_lock();
31503168
31513169 if (po->fanout) {
....@@ -3248,7 +3266,7 @@
32483266 memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data));
32493267 name[sizeof(uaddr->sa_data)] = 0;
32503268
3251
- return packet_do_bind(sk, name, 0, pkt_sk(sk)->num);
3269
+ return packet_do_bind(sk, name, 0, 0);
32523270 }
32533271
32543272 static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
....@@ -3265,8 +3283,7 @@
32653283 if (sll->sll_family != AF_PACKET)
32663284 return -EINVAL;
32673285
3268
- return packet_do_bind(sk, NULL, sll->sll_ifindex,
3269
- sll->sll_protocol ? : pkt_sk(sk)->num);
3286
+ return packet_do_bind(sk, NULL, sll->sll_ifindex, sll->sll_protocol);
32703287 }
32713288
32723289 static struct proto packet_proto = {
....@@ -3472,7 +3489,7 @@
34723489 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, copy_len);
34733490 }
34743491
3475
- if (pkt_sk(sk)->auxdata) {
3492
+ if (packet_sock_flag(pkt_sk(sk), PACKET_SOCK_AUXDATA)) {
34763493 struct tpacket_auxdata aux;
34773494
34783495 aux.tp_status = TP_STATUS_USER;
....@@ -3856,9 +3873,7 @@
38563873 if (copy_from_sockptr(&val, optval, sizeof(val)))
38573874 return -EFAULT;
38583875
3859
- lock_sock(sk);
3860
- po->auxdata = !!val;
3861
- release_sock(sk);
3876
+ packet_sock_flag_set(po, PACKET_SOCK_AUXDATA, val);
38623877 return 0;
38633878 }
38643879 case PACKET_ORIGDEV:
....@@ -3870,9 +3885,7 @@
38703885 if (copy_from_sockptr(&val, optval, sizeof(val)))
38713886 return -EFAULT;
38723887
3873
- lock_sock(sk);
3874
- po->origdev = !!val;
3875
- release_sock(sk);
3888
+ packet_sock_flag_set(po, PACKET_SOCK_ORIGDEV, val);
38763889 return 0;
38773890 }
38783891 case PACKET_VNET_HDR:
....@@ -3969,7 +3982,8 @@
39693982 if (copy_from_sockptr(&val, optval, sizeof(val)))
39703983 return -EFAULT;
39713984
3972
- po->xmit = val ? packet_direct_xmit : dev_queue_xmit;
3985
+ /* Paired with all lockless reads of po->xmit */
3986
+ WRITE_ONCE(po->xmit, val ? packet_direct_xmit : dev_queue_xmit);
39733987 return 0;
39743988 }
39753989 default:
....@@ -4020,10 +4034,10 @@
40204034
40214035 break;
40224036 case PACKET_AUXDATA:
4023
- val = po->auxdata;
4037
+ val = packet_sock_flag(po, PACKET_SOCK_AUXDATA);
40244038 break;
40254039 case PACKET_ORIGDEV:
4026
- val = po->origdev;
4040
+ val = packet_sock_flag(po, PACKET_SOCK_ORIGDEV);
40274041 break;
40284042 case PACKET_VNET_HDR:
40294043 val = po->has_vnet_hdr;