Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp1874106lqb; Sun, 26 May 2024 23:32:08 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVi46113ye1lxn4i2KJcX0yFRD+QqqVHy6dSFn+2oe+NRxT3srmAqzRDDqL6j2XurpPj4PpTktM5gJaf/dE28x9/UGueySSb+uDCdrO2g== X-Google-Smtp-Source: AGHT+IG2Gtg3iLRscJdgszjcZGq1EvOK9O0admYNWKtk6lpOZGsPkfOwwOH5Ec5cZOzHLfurHfjI X-Received: by 2002:a17:90a:ba0a:b0:2b9:e009:e47a with SMTP id 98e67ed59e1d1-2bddce7bbc7mr15836129a91.10.1716791528179; Sun, 26 May 2024 23:32:08 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716791528; cv=pass; d=google.com; s=arc-20160816; b=yvkop1ruiIaJkPDvFJ21CpQcUljmJxya8AT+0FSR30blm9qkfJ2QufbsI+ybPHttte g1bjmoPctX8ngprXBp8r7aaCgemHa0fSpU9JXVb78RzW8SjvdyqlRGPH38zfR7bY8YBA LFpWJQaqEVg0nPMgl3ShuWfZSx4GKAELJFJPGveyQop3ZUBPleLnl4ywxu3hDGc1oior ENeaOG7Y1fEAMBzK/YgRo8TQMaMBhBl9SABn5Ha29bVbXyw5XnV9+Jsy6iIDbOUc2zzW Hgeav8sRQolvO8/P59eXyVE+kK/EXpbKdDYRjE5KUcQOzsQ+2tfcqGM04KbhIGxUPx/H qEvA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=MbnK7btCma2THOpFcDrHSellO+iE5ad9pZfYFL7rGkU=; fh=H6TJmbcdsTsr17w/eC46McoMPKbMmkpSnTDPpENHeRM=; b=Q6q48ldKOXqfMms/AbWrySkrYF1NUQDT/5/MHlZ/oGeUjgxOYTyL3ahpOL5DBhaO7P 2qfefsBvDwATRJXIAZVOKCMR8B5Bfq2QX9gq07IJ+fZLZcl48KlnN7MYdDOPkjUhSOHY eanrzgay7PxZVAPNMcxMBS9RHdtSBARZuhl/HYLpzFUJIjQrUyBnIud0aY3pD86eizJn cQ5GGE9iz3B5+VDgCX/ZKqjR7tGfOvF65TbL93++CO2bHjXJLImCR1dWSd5mfBNpo3Vp CSg/Opkr7tPB8hXmCJqU2Xgv4JUcY/sERicUA4by/pyEJC/X69N+28zQoaZjpk3zVs4k 9wcQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=FZCvON5z; 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-190070-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190070-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 41be03b00d2f7-682288e7666si5676619a12.470.2024.05.26.23.32.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 23:32:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-190070-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=FZCvON5z; 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-190070-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190070-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id D427CB20AB4 for ; Mon, 27 May 2024 06:32:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2BDAC16415; Mon, 27 May 2024 06:31:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="FZCvON5z" 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 53650125BA for ; Mon, 27 May 2024 06:31:54 +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=1716791517; cv=none; b=qNUiDENE74HchGZk+lMsDSr5XBbswGnQzhqFIAwhd2RB/GJrUWpBgeJ7bUeYT23mXKyiTBpa+ZWAIgyfl1RNVr85NWxyljSG1xrtwsnLvHxAroL9e4C+KN1122E+BIkIWAhkPNH4z3+dDZpiT3OfgLIu/qUL8EeE1yNkh2zN7tA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716791517; c=relaxed/simple; bh=GiX/F6U+4AhQxOl6Y7hnIeCCOl0QPSSAqz7DQ9FqL6k=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=ACccpnKupLrbh/rAA5KyBgeBsekExPTij5TEysWQY6/q4iOH8SylmOGM7d39i/QN0TkZadVk5EECixLnJtUrGd7MxQI3bQnLu753G1hEfZcgbbgtMQyLAqrA5s9664EpBLyWn3XUuHdtBa0wodcv74419qcFcQeJhBl1rf08cZg= 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=FZCvON5z; 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-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (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 ED82141203 for ; Mon, 27 May 2024 06:31:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1716791511; bh=MbnK7btCma2THOpFcDrHSellO+iE5ad9pZfYFL7rGkU=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=FZCvON5zGdX1O61DfHue+fp4tvnVGxUiYe86rxSls6BF36m4Ndlkd6pLaW+7i3AdG HKAx/ltGkSVzCGe3zPtLcThaLBnXH0HzkCVYdgtUviTFzYt6lOORIte6ki19Rgaf4B jVUcMulGwEYJIlkOUg3XyMKrF/zn5DYEXLd1/LMCBjrk2+LWZ3qK3lIu4SM26ie68m Zv9BxCHiIr/PNr02TnWQSUCx+MtgpewHDWAxRvjKJuGOAZYWtMhOTren+oh+QRsVW5 Rj3gJG0JVb7rXDwLToptnPFT12GdjksGE6LlQHnga2G2pVf/X1PyJihboPvqjGevR6 ZiSWwu0jt/xaA== Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-1f2fbeba024so21329045ad.1 for ; Sun, 26 May 2024 23:31:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716791510; x=1717396310; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MbnK7btCma2THOpFcDrHSellO+iE5ad9pZfYFL7rGkU=; b=gcNsLzONpRcku4AOcBpjeaEit4sLuQmzOX7OlRaLIUXtB54IN53RxyglYzYdkekDmt Jlu+fy8RiPBp+36DRyeqiLwFxq8gGQlAyTP36ALT+14PoJCdx/v56wtyWecIuD1C/LW5 XGbZaHVPlOdVyfQWrAkkHZ1SLNb2wbrXfTMEnohBV4Kp44fJwdHG/iBoAse3lHc7cCw6 XFnQ0r1/56SNIoTu4XwOUX2WMw8EF9OST30GiA7rHgPbUfCIVM4E5oOApyLE4uyeNmHs 0oYL1RFD+g/VZHor4QRZM/Th4du1W0i39XlK7rCDMkaTxWbO8nRzYT21E9HMTwT/rh7W NPzg== X-Forwarded-Encrypted: i=1; AJvYcCWCi+QwQZjfWtaYhTcY5yQbQzZNIgfmZAir1UiICCMsCvhUnVI5Cd5VBER3ZTPQ+0kEkpEZKsY8RZYFntfIKHtaGTsKC3l1SXLEGqIB X-Gm-Message-State: AOJu0Yy94SkfQJX/VR3x32al+9uc17UUmQqumW0wZC5x64qOxefPhqb2 xJmm+de+KtwEh0B25dfaVNVzIJBSXwXoEc+asZyTlwWW0qP0g4mW6PiiFkRraQNrXLkY3olBt5y Ze36zYQ8Tj2KxZV4fHCfOjs6btN+iRch4vx1H3Ts4jBEzf4OynN22nJeMvylAtxgXt8NYMtQDaZ ZOuA== X-Received: by 2002:a17:902:f684:b0:1f3:5ca:4200 with SMTP id d9443c01a7336-1f448126212mr124732225ad.2.1716791510241; Sun, 26 May 2024 23:31:50 -0700 (PDT) X-Received: by 2002:a17:902:f684:b0:1f3:5ca:4200 with SMTP id d9443c01a7336-1f448126212mr124731835ad.2.1716791509744; Sun, 26 May 2024 23:31:49 -0700 (PDT) Received: from chengendu.. (36-227-176-221.dynamic-ip.hinet.net. [36.227.176.221]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f4aa742215sm5895275ad.289.2024.05.26.23.31.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 23:31:49 -0700 (PDT) From: Chengen Du To: willemdebruijn.kernel@gmail.com Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, loke.chetan@gmail.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Chengen Du Subject: [PATCH v2] af_packet: Handle outgoing VLAN packets without hardware offloading Date: Mon, 27 May 2024 14:31:36 +0800 Message-Id: <20240527063136.159616-1-chengen.du@canonical.com> X-Mailer: git-send-email 2.40.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The issue initially stems from libpcap [1]. 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_buff 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 not 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() function 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 correctly. [1] https://github.com/the-tcpdump-group/libpcap/issues/1105 Fixes: f6fb8f100b80 ("af-packet: TPACKET_V3 flexible buffer implementation.") Signed-off-by: Chengen Du --- net/packet/af_packet.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index ea3ebc160e25..82b36e90d73b 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -1011,6 +1011,10 @@ static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc, ppd->hv1.tp_vlan_tci = skb_vlan_tag_get(pkc->skb); ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->vlan_proto); ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID; + } else if (eth_type_vlan(pkc->skb->protocol)) { + ppd->hv1.tp_vlan_tci = ntohs(vlan_eth_hdr(pkc->skb)->h_vlan_TCI); + ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->protocol); + ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID; } else { ppd->hv1.tp_vlan_tci = 0; ppd->hv1.tp_vlan_tpid = 0; @@ -2428,6 +2432,10 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, h.h2->tp_vlan_tci = skb_vlan_tag_get(skb); h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto); status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID; + } else if (eth_type_vlan(skb->protocol)) { + h.h2->tp_vlan_tci = ntohs(vlan_eth_hdr(skb)->h_vlan_TCI); + h.h2->tp_vlan_tpid = ntohs(skb->protocol); + status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID; } else { h.h2->tp_vlan_tci = 0; h.h2->tp_vlan_tpid = 0; @@ -2457,7 +2465,8 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, sll->sll_halen = dev_parse_header(skb, sll->sll_addr); sll->sll_family = AF_PACKET; sll->sll_hatype = dev->type; - sll->sll_protocol = skb->protocol; + sll->sll_protocol = (skb->protocol == htons(ETH_P_8021Q)) ? + vlan_eth_hdr(skb)->h_vlan_encapsulated_proto : skb->protocol; sll->sll_pkttype = skb->pkt_type; if (unlikely(packet_sock_flag(po, PACKET_SOCK_ORIGDEV))) sll->sll_ifindex = orig_dev->ifindex; @@ -3482,7 +3491,8 @@ static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, /* Original length was stored in sockaddr_ll fields */ origlen = PACKET_SKB_CB(skb)->sa.origlen; sll->sll_family = AF_PACKET; - sll->sll_protocol = skb->protocol; + sll->sll_protocol = (skb->protocol == htons(ETH_P_8021Q)) ? + vlan_eth_hdr(skb)->h_vlan_encapsulated_proto : skb->protocol; } sock_recv_cmsgs(msg, sk, skb); @@ -3539,6 +3549,10 @@ static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, aux.tp_vlan_tci = skb_vlan_tag_get(skb); aux.tp_vlan_tpid = ntohs(skb->vlan_proto); aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID; + } else if (eth_type_vlan(skb->protocol)) { + aux.tp_vlan_tci = ntohs(vlan_eth_hdr(skb)->h_vlan_TCI); + aux.tp_vlan_tpid = ntohs(skb->protocol); + aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID; } else { aux.tp_vlan_tci = 0; aux.tp_vlan_tpid = 0; -- 2.40.1