Received: by 2002:ab2:7903:0:b0:1fb:b500:807b with SMTP id a3csp1396824lqj; Mon, 3 Jun 2024 23:15:05 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWX3c3SrTvzRQWuCtQu+SnZZVCRpmkWPOHe1Fjazb/8j7eE5OKXZr/DGb4MRyBA2Jm+PGmcYDMUQC53mcI8Wo9iA7S0Gbujkb38TWfRQQ== X-Google-Smtp-Source: AGHT+IFWJfmfFIVqV40jFd4RqWIlGbyJUDxrqjoLXG7Gt6uU6gQZ/O5lyG4+zw7AZqMUzuVlT8xs X-Received: by 2002:a25:9744:0:b0:df4:efd9:c717 with SMTP id 3f1490d57ef6-dfa73dbc30cmr9768665276.47.1717481705357; Mon, 03 Jun 2024 23:15:05 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717481705; cv=pass; d=google.com; s=arc-20160816; b=gYdN1A8PTig9GlYZAavxy/fzYk2yDNPEPXkJKu+0ly2n7cqrHCIis3DOldS0Su3pMp hKaKRv9z3YCFyIY8zUmZMu75OskaSqryzFihTnqZsiITX0myrxk9UQCPKmMnCcfU1F5F U/klzBAihTBHGgMXZBRHpoIgyBz8I/tkBaYDbEyanUUipDE1RPvNyHomCKQvoQFDERZi pP8gHQZdPdg9BGhBNUM8QlSjDj1sOVnyFEDr3/QK/xNOC6kqOcZ+PRqM2PjZtxHL4joU ifjC0zAECaqQX/QGv1Jqa15QE7HkecRAHzLLCBve/CsUv7GCXpVPaY8iVI9Wyttqp5hr zWVA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=1WeR8EGjanFo/UpdB5WDNqel6tIBsDJjn/aZGqNsPZw=; fh=mme32WoFY5DHhq3KmpYLR3bi1SbTGUs043ffpaYssxI=; b=ZZ6YB+pTRzl8r3QSnsv/4ppdvcOaWyU53DlBxaeLFfductQiFf6AMNu9W6bqBzqSw1 mZeMsbP5lWdWG2CrJeVT+khjAlwlKSAbToOEExtKiat5eEvXLPncvQ+2cqO+RXcaXfBI YAo8YVGOsBziaFY6uTLH1C2/rQOTJWcUOLjCc40tKF/MLNmI7O00V5EBniDi94Y3Jzgj folOhKFp5KP3mafZMjitHLdFe38ztm9GMSfnsrbYc/ZwM1vX8eq3mU7x8/lE8i1+Y1i2 +BISmAQj0yUaSjehOalIFyzj2i2TKBze4jBM9sl8svuprrG1fufYXJvUcKznanW04QAB PxlA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=EHRjcEO0; arc=pass (i=1 spf=pass spfdomain=canonical.com dkim=pass dkdomain=canonical.com dmarc=pass fromdomain=canonical.com); spf=pass (google.com: domain of linux-kernel+bounces-200093-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200093-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 6a1803df08f44-6ae4b417017si443976d6.399.2024.06.03.23.15.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 23:15:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-200093-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=EHRjcEO0; arc=pass (i=1 spf=pass spfdomain=canonical.com dkim=pass dkdomain=canonical.com dmarc=pass fromdomain=canonical.com); spf=pass (google.com: domain of linux-kernel+bounces-200093-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200093-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 0DA091C20B59 for ; Tue, 4 Jun 2024 06:15:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8147013D511; Tue, 4 Jun 2024 06:14:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="EHRjcEO0" Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C99EA14294 for ; Tue, 4 Jun 2024 06:14:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.122 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717481698; cv=none; b=vE0IkNrcYvxdj2wMYRz+uIvwI+p6tdWm1pDbuAtdfyk8NiJzq5L5pi/pUJOHJlt0rocwBPCHEGVPROHRrLp2ctWCyjzZoleQ9+yI1HvsoK4nk0YLrjnkgLH2jVopt0HPLN3PLBkKLP3fKiO7mo8EVWOglNdLbPieV1Hkh6QpDqo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717481698; c=relaxed/simple; bh=4hIQQi3dXMlNsoeK8RqHMsi4O+6Mj+5pg/TWlF0xkts=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=TeCTm3RHPEAG4nXpEtWnwil7R+TJnsJNBsw0rRExDftwx5UlHNypqzqSqEr77XmNTxfe1We45WNlcSEj/khN36gATlqwf4Jev7dvbfqJzQkGzGjfgtgZ1Kp5BrEt/n6nmiRMAqdZu9OFcjXmdfSguYSJM+49HkAdD3DVH9rEUW4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=EHRjcEO0; arc=none smtp.client-ip=185.125.188.122 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 45E723F2A4 for ; Tue, 4 Jun 2024 06:14:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1717481690; bh=1WeR8EGjanFo/UpdB5WDNqel6tIBsDJjn/aZGqNsPZw=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=EHRjcEO0NVwpDZhwqTbuRj48Do9UiFqwvZndQ7PWRA0KoP/BtlFVFBURiNIdyn/eG 0Pys8boY3CzAmQs6z+BIOS/8Dh0TBrKc94JxxN+vvd/vXiwHzhIaRV4wNg+Z2Tuy+i aLFzXH1XQznEzURu29JiOyK3D5HIt7GwCG2ZmTna0b6rKKd0xEvDj8f3x/1cGAeNkx 9Vdp417o8cBiQ2jRocMLhb7PZiZnhlLzjRnIVQiD5JqESM4F1PETCk8Nfowrhe07MA PgK3JrtXcnPY5GdvEK0wpztFXIR4zI9CdTOLVtn6madMeDH5DpsfTcCuC5GgJO212L zYseik80brLMA== Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a68f2d64342so13344766b.1 for ; Mon, 03 Jun 2024 23:14:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717481689; x=1718086489; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1WeR8EGjanFo/UpdB5WDNqel6tIBsDJjn/aZGqNsPZw=; b=H2U95rACPBy+IE+7LWx8PbPe6mMtlqKct9sJEV69IltoIbOjDO9yvSjReUqgmSlRzf dP1gCg2Ha49LJX2ls5wHLngua32E0Qm8p1v/C5QJj/xWfuA9jyYY/Ms771DXqfb14RCX G6W6trjzjWwhCYPFb+UH5vjrKCBlxefYOF7ETLl0plVjOxOo7Cvlxpm8b2ClgPUSF4nH oEd3YX+fSZKcHcLvFsS5k4KIWox50bThOXMsOeDLfahWijG1oQnEBdslOy06tzpU8XNn IMdcO96gFaFiA6QV1oCUO5yT5DUGIe9K3PDxjSbzfYBepNtLewpc8RQu6JKHGDcsUx2c KQAA== X-Forwarded-Encrypted: i=1; AJvYcCVPR2+nAuFGLV0AWMI/TmJu019PsWcXN8XA2cv367kwmv8cFFT09yvyM27BnpkkKTJHtatypeyzL9yE9Iq4pHiY/pba3ANqPnmpJulY X-Gm-Message-State: AOJu0YySXDVdSgGHa9c6ldEoWWwYqOul7xM3Jq1oDQIcoI5GXuagjGci PJBLCfmjzp1eCNR/NqzB2+JFKvBvh8QnOi7rewOS/9ihsN5dMGbI+9H3aCyaHiF88fK4O8GQ1Oc mtyp3mgeqAXcf0TI0mY7aWmCYPhbKkEX8dmxxYQ60IPTsXpSESSvcHkfx+vL5NZr0devf76gy0h u/tvzQ4aPp2c5xWXvq6ZzKSm/mRMZIUMvzbj6VSCHKPkgYomfKvBDn X-Received: by 2002:a17:907:97c6:b0:a69:2553:5806 with SMTP id a640c23a62f3a-a6925535850mr262892666b.52.1717481689414; Mon, 03 Jun 2024 23:14:49 -0700 (PDT) X-Received: by 2002:a17:907:97c6:b0:a69:2553:5806 with SMTP id a640c23a62f3a-a6925535850mr262890266b.52.1717481688973; Mon, 03 Jun 2024 23:14:48 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240604054823.20649-1-chengen.du@canonical.com> In-Reply-To: <20240604054823.20649-1-chengen.du@canonical.com> From: Chengen Du Date: Tue, 4 Jun 2024 14:14:37 +0800 Message-ID: Subject: Re: [PATCH v5] af_packet: Handle outgoing VLAN packets without hardware offloading To: willemdebruijn.kernel@gmail.com Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kaber@trash.net, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Willem, Thank you for your comments on patch v4. I have made some modifications and would like to discuss some of your suggestions in detail. On Tue, Jun 4, 2024 at 1:50=E2=80=AFPM Chengen Du wrote: > > The issue initially stems from libpcap. The ethertype will be overwritten > as the VLAN TPID if the network interface lacks hardware VLAN offloading. > In the outbound packet path, if hardware VLAN offloading is unavailable, > the VLAN tag is inserted into the payload but then cleared from the sk_bu= ff > struct. Consequently, this can lead to a false negative when checking for > the presence of a VLAN tag, causing the packet sniffing outcome to lack > VLAN tag information (i.e., TCI-TPID). As a result, the packet capturing > tool may be unable to parse packets as expected. > > The TCI-TPID is missing because the prb_fill_vlan_info() function does no= t > modify the tp_vlan_tci/tp_vlan_tpid values, as the information is in the > payload and not in the sk_buff struct. The skb_vlan_tag_present() functio= n > only checks vlan_all in the sk_buff struct. In cooked mode, the L2 header > is stripped, preventing the packet capturing tool from determining the > correct TCI-TPID value. Additionally, the protocol in SLL is incorrect, > which means the packet capturing tool cannot parse the L3 header correctl= y. > > Link: https://github.com/the-tcpdump-group/libpcap/issues/1105 > Link: https://lore.kernel.org/netdev/20240520070348.26725-1-chengen.du@ca= nonical.com/T/#u > Fixes: 393e52e33c6c ("packet: deliver VLAN TCI to userspace") > Cc: stable@vger.kernel.org > Signed-off-by: Chengen Du > --- > net/packet/af_packet.c | 64 ++++++++++++++++++++++++++++++++++++++++-- > 1 file changed, 62 insertions(+), 2 deletions(-) > > diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c > index ea3ebc160e25..53d51ac87ac6 100644 > --- a/net/packet/af_packet.c > +++ b/net/packet/af_packet.c > @@ -538,6 +538,52 @@ static void *packet_current_frame(struct packet_sock= *po, > return packet_lookup_frame(po, rb, rb->head, status); > } > > +static u16 vlan_get_tci(struct sk_buff *skb) > +{ > + unsigned int vlan_depth =3D skb->mac_len; > + struct vlan_hdr vhdr, *vh; > + u8 *skb_head =3D skb->data; > + int skb_len =3D skb->len; > + > + if (vlan_depth) { > + if (WARN_ON(vlan_depth < VLAN_HLEN)) > + return 0; > + vlan_depth -=3D VLAN_HLEN; > + } else { > + vlan_depth =3D ETH_HLEN; > + } > + > + skb_push(skb, skb->data - skb_mac_header(skb)); > + vh =3D skb_header_pointer(skb, vlan_depth, sizeof(vhdr), &vhdr); > + if (skb_head !=3D skb->data) { > + skb->data =3D skb_head; > + skb->len =3D skb_len; > + } > + if (unlikely(!vh)) > + return 0; > + > + return ntohs(vh->h_vlan_TCI); > +} As you mentioned, we need the outermost VLAN tag to fit the protocol we referenced in tp_vlan_tpid. In if_vlan.h, __vlan_get_protocol() only provides the protocol and may affect other usage scenarios if we modify it to also provide TCI. There is a similar function called __vlan_get_tag(), which also aims to extract TCI from the L2 header, but it doesn't check the header size as __vlan_get_protocol() does. To prevent affecting other usage scenarios, I introduced a new function to achieve our purpose. Additionally, the starting point of skb->data differs in SOCK_RAW and SOCK_DGRAM cases. I accounted for this by using skb_push to ensure that skb->data starts from the L2 header. Would you recommend modifying __vlan_get_tag() to add the size check logic instead? If so, we would also need to consider how to integrate the size check logic into both __vlan_get_protocol() and __vlan_get_tag(). > + > +static __be16 vlan_get_protocol_dgram(struct sk_buff *skb) > +{ > + __be16 proto =3D skb->protocol; > + > + if (unlikely(eth_type_vlan(proto))) { > + u8 *skb_head =3D skb->data; > + int skb_len =3D skb->len; > + > + skb_push(skb, skb->data - skb_mac_header(skb)); > + proto =3D __vlan_get_protocol(skb, proto, NULL); > + if (skb_head !=3D skb->data) { > + skb->data =3D skb_head; > + skb->len =3D skb_len; > + } > + } > + > + return proto; > +} > + > static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc) > { > del_timer_sync(&pkc->retire_blk_timer); > @@ -1011,6 +1057,10 @@ static void prb_fill_vlan_info(struct tpacket_kbdq= _core *pkc, > ppd->hv1.tp_vlan_tci =3D skb_vlan_tag_get(pkc->skb); > ppd->hv1.tp_vlan_tpid =3D ntohs(pkc->skb->vlan_proto); > ppd->tp_status =3D TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_= TPID_VALID; > + } else if (unlikely(eth_type_vlan(pkc->skb->protocol))) { > + ppd->hv1.tp_vlan_tci =3D vlan_get_tci(pkc->skb); > + ppd->hv1.tp_vlan_tpid =3D ntohs(pkc->skb->protocol); > + ppd->tp_status =3D TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_= TPID_VALID; > } else { > ppd->hv1.tp_vlan_tci =3D 0; > ppd->hv1.tp_vlan_tpid =3D 0; > @@ -2428,6 +2478,10 @@ static int tpacket_rcv(struct sk_buff *skb, struct= net_device *dev, > h.h2->tp_vlan_tci =3D skb_vlan_tag_get(skb); > h.h2->tp_vlan_tpid =3D ntohs(skb->vlan_proto); > status |=3D TP_STATUS_VLAN_VALID | TP_STATUS_VLAN= _TPID_VALID; > + } else if (unlikely(eth_type_vlan(skb->protocol))) { > + h.h2->tp_vlan_tci =3D vlan_get_tci(skb); > + h.h2->tp_vlan_tpid =3D ntohs(skb->protocol); > + status |=3D TP_STATUS_VLAN_VALID | TP_STATUS_VLAN= _TPID_VALID; > } else { > h.h2->tp_vlan_tci =3D 0; > h.h2->tp_vlan_tpid =3D 0; > @@ -2457,7 +2511,8 @@ static int tpacket_rcv(struct sk_buff *skb, struct = net_device *dev, > sll->sll_halen =3D dev_parse_header(skb, sll->sll_addr); > sll->sll_family =3D AF_PACKET; > sll->sll_hatype =3D dev->type; > - sll->sll_protocol =3D skb->protocol; > + sll->sll_protocol =3D (sk->sk_type =3D=3D SOCK_DGRAM) ? > + vlan_get_protocol_dgram(skb) : skb->protocol; > sll->sll_pkttype =3D skb->pkt_type; > if (unlikely(packet_sock_flag(po, PACKET_SOCK_ORIGDEV))) > sll->sll_ifindex =3D orig_dev->ifindex; > @@ -3482,7 +3537,8 @@ static int packet_recvmsg(struct socket *sock, stru= ct msghdr *msg, size_t len, > /* Original length was stored in sockaddr_ll fields */ > origlen =3D PACKET_SKB_CB(skb)->sa.origlen; > sll->sll_family =3D AF_PACKET; > - sll->sll_protocol =3D skb->protocol; > + sll->sll_protocol =3D (sock->type =3D=3D SOCK_DGRAM) ? > + vlan_get_protocol_dgram(skb) : skb->protocol; > } > > sock_recv_cmsgs(msg, sk, skb); > @@ -3539,6 +3595,10 @@ static int packet_recvmsg(struct socket *sock, str= uct msghdr *msg, size_t len, > aux.tp_vlan_tci =3D skb_vlan_tag_get(skb); > aux.tp_vlan_tpid =3D ntohs(skb->vlan_proto); > aux.tp_status |=3D TP_STATUS_VLAN_VALID | TP_STAT= US_VLAN_TPID_VALID; > + } else if (unlikely(eth_type_vlan(skb->protocol))) { > + aux.tp_vlan_tci =3D vlan_get_tci(skb); > + aux.tp_vlan_tpid =3D ntohs(skb->protocol); > + aux.tp_status |=3D TP_STATUS_VLAN_VALID | TP_STAT= US_VLAN_TPID_VALID; > } else { > aux.tp_vlan_tci =3D 0; > aux.tp_vlan_tpid =3D 0; > -- > 2.43.0 >