From patchwork Sat May 14 14:16:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 12849810 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B4B9C433FE for ; Sat, 14 May 2022 14:17:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233058AbiENORc (ORCPT ); Sat, 14 May 2022 10:17:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233072AbiENOR3 (ORCPT ); Sat, 14 May 2022 10:17:29 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A7CC13DE1; Sat, 14 May 2022 07:17:24 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id p12so10181020pfn.0; Sat, 14 May 2022 07:17:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pAzFdjEJParU598emBsl070uimancTgMuaJbOmtBt7k=; b=d8eqcu82P+nAhSF3059Qr9BeFLQsomJZ1TaqY20pGfAgXswcfXpKv0f2VocHWwtosV F9EWLZL+yyt/59pHZN/vhm2cEIvBuvqYruXeMHLrnqx7DmeOL9FKhjq3AmT5y2tk7pXE CVpvbrJzNF91a7uMGXwKeiLqLlFNzhDz/0a/6Jj9K4Rl0iVoPwZ/06rVjH3h1sK8c1rB kFAr3REwU/TpgqfkcqZRxXiqoefVkpiT1HWhotrvzDHcDe1Fyl5grXesaywBqOlgs7Gk TQnXdkLXg9AVxHRcLy9k1iMPC6LQhyte/3mq+jsf7/OTuQiFkeflc8g08MDbtHhTEN20 rmrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=pAzFdjEJParU598emBsl070uimancTgMuaJbOmtBt7k=; b=zraycUm5dmjnM9VX1HZXgjRwSoXxORbu2PBEQQ81xVnXoGjxQciN8WSTSTwTPBPB4+ qZyHqsf3KRCREm19MBnL9s336PbmqPoVHltBPZe6vplRxKZk3vkPl71e0dQLfJg4Yk/O Z1Scca8oTng9zt/D6ALdceSMFF9zOcKyJH8iA74F5RReKdjMWfa/nw0LBTDnaNeXIOb+ m81Fbx2tZieR52EH+VyaxEUumXRQZIOmu8HiVC129nFgXtMsyJeVtwWmK75LQOX5pmQ4 j9/zdK5pdzGN4faJjoNf4+39XJa0+KJWaWLbTqvMD24SBNP3lqNsJaIgmAbtrwcXi8eJ ZD7w== X-Gm-Message-State: AOAM531nEn6GWrGeb3IokdwG53IKOzZkjb0JxdPoeqvoOkJvaiDFGnCW 3J7QXdzlAW5nNg55muHz6heq10sKsl4mVmYL X-Google-Smtp-Source: ABdhPJxd/RDJzx7lKZe59yNSKHsumNxO0ixX84ZL8czVhN91aHTpIdUf2vl4nGO6EEVos1sTB8QCkQ== X-Received: by 2002:a05:6a00:170a:b0:50d:3e40:9e0 with SMTP id h10-20020a056a00170a00b0050d3e4009e0mr9258287pfc.48.1652537843910; Sat, 14 May 2022 07:17:23 -0700 (PDT) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id x8-20020a17090a530800b001cd4989feccsm5298541pjh.24.2022.05.14.07.17.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 May 2022 07:17:23 -0700 (PDT) Sender: Vincent Mailhol From: Vincent Mailhol To: Marc Kleine-Budde Cc: linux-can@vger.kernel.org, linux-kernel@vger.kernel.org, Max Staudt , Oliver Hartkopp , netdev@vger.kernel.org, Vincent Mailhol Subject: [PATCH v3 1/4] can: slcan: use can_dropped_invalid_skb() instead of manual check Date: Sat, 14 May 2022 23:16:47 +0900 Message-Id: <20220514141650.1109542-2-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220514141650.1109542-1-mailhol.vincent@wanadoo.fr> References: <20220513142355.250389-1-mailhol.vincent@wanadoo.fr> <20220514141650.1109542-1-mailhol.vincent@wanadoo.fr> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org slcan does a manual check in slc_xmit() to verify if the skb is valid. This check is incomplete, use instead can_dropped_invalid_skb(). Signed-off-by: Vincent Mailhol --- drivers/net/can/slcan.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/net/can/slcan.c b/drivers/net/can/slcan.c index 27783fbf011f..1879b50391ee 100644 --- a/drivers/net/can/slcan.c +++ b/drivers/net/can/slcan.c @@ -359,8 +359,8 @@ static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev) { struct slcan *sl = netdev_priv(dev); - if (skb->len != CAN_MTU) - goto out; + if (can_dropped_invalid_skb(dev, skb)) + return NETDEV_TX_OK; spin_lock(&sl->lock); if (!netif_running(dev)) { From patchwork Sat May 14 14:16:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 12849811 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 02A94C43219 for ; Sat, 14 May 2022 14:17:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233088AbiENORe (ORCPT ); Sat, 14 May 2022 10:17:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233139AbiENORd (ORCPT ); Sat, 14 May 2022 10:17:33 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2387E65E1; Sat, 14 May 2022 07:17:32 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id w17-20020a17090a529100b001db302efed6so10255781pjh.4; Sat, 14 May 2022 07:17:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HSCzIPZkOxbd67rvgI4t0yXSJkSPovXcOUQj8kg1kKM=; b=m1mqQs09ODnGkFJ2JeZTElntbNA5L0Y/sNeehpQQMOhaMCKdKccl4uTGgDp2sEHulg 1cCAkcPSrbyZ8KgnP4urcVFN6UQhMr1mzeauyfgGVvMUGu8DiEmVHuYIQZ21QYf9o6+l LIqcODAOPPqnMi5t/csL2oxvgOx79Txy89p+pS7h4mFpBZKxCDBItp61HIIP02jAip/V 1iso2r8ZBhkukozh78Mw68J/ZUZg6s8O+zSf+eBzzTpp9szZXp7zpQms30JdRkVkT7Rf PNaPle5E+ZkStXq8zX3ayYMJU2tDL7ewQxWIvq2WSlTJJ4kP8lQ752PadkRBa4vw7RQA Tdqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=HSCzIPZkOxbd67rvgI4t0yXSJkSPovXcOUQj8kg1kKM=; b=bP2RMeY/OS5aJlU9ufE/1gG1QMt9o08jTSLIKwhkfXxBQZHmtYj0toFC3oYNYW4t1j NsjzrTWJJZCjpMxLpvq1unvWD2EA8eKA1W2AGCLc2Mn5wbHonOzU7tMnSwwAm9V8MFNn LOE8FKJw0kTAuU1kFjZhc1wrE0qSZFkQkLldQVG1TOVi2vCYJgBeNqgTUsW4PNQnZwCu fJz8itWnWJpPwjP4U/vKp9YxKWIlw+MzSVlIY7m1Onsyo/nn7YT772VDA8HmZZU8PWM6 uGj3S8dCrwvyW25KBem5dHGKTjUA5Ucq8mhsinlmzjOFPP9/RecmBLmOXwY0k//tEQo6 QSyA== X-Gm-Message-State: AOAM532yM7g9OCbjokFnaXyzoRYn3oICmnIbemo9EounfdopZ46U37oc TSw9GzmQdlJM0Y0RZvP6MLA= X-Google-Smtp-Source: ABdhPJzo1AQqaSYYrdBV+tKqIdcFTE6whcQ2CsUdcqMHD6dBFSYcsMdHh9HRQQB7q172F+ZPWWEqqA== X-Received: by 2002:a17:90b:4f49:b0:1dc:1762:4e00 with SMTP id pj9-20020a17090b4f4900b001dc17624e00mr20941615pjb.87.1652537851522; Sat, 14 May 2022 07:17:31 -0700 (PDT) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id x8-20020a17090a530800b001cd4989feccsm5298541pjh.24.2022.05.14.07.17.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 May 2022 07:17:31 -0700 (PDT) Sender: Vincent Mailhol From: Vincent Mailhol To: Marc Kleine-Budde Cc: linux-can@vger.kernel.org, linux-kernel@vger.kernel.org, Max Staudt , Oliver Hartkopp , netdev@vger.kernel.org, Vincent Mailhol Subject: [PATCH v3 2/4] can: Kconfig: change CAN_DEV into a menuconfig Date: Sat, 14 May 2022 23:16:48 +0900 Message-Id: <20220514141650.1109542-3-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220514141650.1109542-1-mailhol.vincent@wanadoo.fr> References: <20220513142355.250389-1-mailhol.vincent@wanadoo.fr> <20220514141650.1109542-1-mailhol.vincent@wanadoo.fr> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Currently, only slcan, vcan and vxcan do not depend on CAN_DEV. This is going to change in the next patch when can_dropped_invalid_skb() will be moved into skb.c. In prevision to that, move CAN_DEV one rank up by making it a menuconfig. The description of CAN_DEV is updated accordingly. Signed-off-by: Vincent Mailhol --- drivers/net/can/Kconfig | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/drivers/net/can/Kconfig b/drivers/net/can/Kconfig index fff259247d52..9bddccfaa0c5 100644 --- a/drivers/net/can/Kconfig +++ b/drivers/net/can/Kconfig @@ -1,5 +1,22 @@ # SPDX-License-Identifier: GPL-2.0-only -menu "CAN Device Drivers" + +menuconfig CAN_DEV + tristate "CAN Device Drivers" + default y + help + This section contains all the CAN device drivers including + the virtual ones. + + Saying Y here enables the common framework for platform CAN + drivers with Netlink support. This is the standard library + for CAN drivers. + + To compile as a module, choose M here: the module will be + called can_dev. + + If unsure, say Y. + +if CAN_DEV config CAN_VCAN tristate "Virtual Local CAN Interface (vcan)" @@ -48,16 +65,6 @@ config CAN_SLCAN can be changed by the 'maxdev=xx' module option. This driver can also be built as a module. If so, the module will be called slcan. -config CAN_DEV - tristate "Platform CAN drivers with Netlink support" - default y - help - Enables the common framework for platform CAN drivers with Netlink - support. This is the standard library for CAN drivers. - If unsure, say Y. - -if CAN_DEV - config CAN_CALC_BITTIMING bool "CAN bit-timing calculation" default y @@ -180,8 +187,6 @@ source "drivers/net/can/softing/Kconfig" source "drivers/net/can/spi/Kconfig" source "drivers/net/can/usb/Kconfig" -endif - config CAN_DEBUG_DEVICES bool "CAN devices debugging messages" help @@ -190,4 +195,4 @@ config CAN_DEBUG_DEVICES a problem with CAN support and want to see more of what is going on. -endmenu +endif From patchwork Sat May 14 14:16:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 12849812 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5867CC433EF for ; Sat, 14 May 2022 14:17:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233174AbiENORm (ORCPT ); Sat, 14 May 2022 10:17:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56732 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233129AbiENORk (ORCPT ); Sat, 14 May 2022 10:17:40 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFE0A1116D; Sat, 14 May 2022 07:17:36 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id iq10so10495865pjb.0; Sat, 14 May 2022 07:17:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=N2k+Isl+sOcDD7NLNzf36x6iwDEAfomPn6Ceuobwoj8=; b=eKAd7Qjm1zOu0ZTJtm7WbmExZ4SlGb3O73IYgy91c2I0JSERoCr4RA+7RM23I4GXrb CQbcsDZXqzgMb5C6S2Cbe5alK6o+5IJKhIOEex1WB0vSCo5wMcKuWOj3+iWE4BvoL/9T t6lpStmBmOV3WswTsT3a2P7mgOisrouVd2TH1uJ3s6pZmxch2OQZVD/nw6WlUiV9DlJJ ruUY40ZaXh6BdF/W+DxBwgZiNNslyVqlFkbGYQpuh9D4v01QNd7FLBnOR8aMQV6cQGgm MmJ2g+Y7tmVuGuZN4zhvuMIoEqwnIa7PUggIGs3u2oZ5GICpX91VV3wGMan6SMtwPZyQ vO2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=N2k+Isl+sOcDD7NLNzf36x6iwDEAfomPn6Ceuobwoj8=; b=cNRbUI81/SHnsJ0yS3HXT5STnJoSL1HAMM+3EdBDhvzwCuBCnb47GY0itKzWBbDKMl oEZ5fyPIwuBAuzO5N9xMrUOolyIJWAMnRfCGZYLy/2aqUxtVuvB9wbQAsihaVMIf+QQH S+D+e3porJXZ/MWiFS6mH6kujTcfGewQkSRqiHv0tvbu9UprSKoNyYE9TB7G847+x8k3 ywAZGmN2ZGOzywqNfqZ/wVhWug0q4OX3crcYUgavDWoWj0PZ0lGyESjQSEYLMeVjXTYL O186Zb0xMoId98DYP7OTyahvnnORMS/Yi6Ij1CGEiXimnT3TptKzSHyFQ2omY4adm237 6QDQ== X-Gm-Message-State: AOAM530aZ4hDKKTpNokL6g1BDVc41VauW+JDSsXtcSW3EM+Xjr/Kd8DJ +2fjUaMrcjn9KnSW7Q1rPdJLfXy1eoImdNVP X-Google-Smtp-Source: ABdhPJwPzbHSNsj9dbC0cqBf7Ij3t+TWaBK7sYYngDWyEGhuHSTFFkys/A/7gn8Wj/3l4JEtQWQ/Eg== X-Received: by 2002:a17:90b:8c6:b0:1da:248b:2f95 with SMTP id ds6-20020a17090b08c600b001da248b2f95mr10093342pjb.125.1652537856310; Sat, 14 May 2022 07:17:36 -0700 (PDT) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id x8-20020a17090a530800b001cd4989feccsm5298541pjh.24.2022.05.14.07.17.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 May 2022 07:17:36 -0700 (PDT) Sender: Vincent Mailhol From: Vincent Mailhol To: Marc Kleine-Budde Cc: linux-can@vger.kernel.org, linux-kernel@vger.kernel.org, Max Staudt , Oliver Hartkopp , netdev@vger.kernel.org, Vincent Mailhol Subject: [PATCH v3 3/4] can: skb:: move can_dropped_invalid_skb and can_skb_headroom_valid to skb.c Date: Sat, 14 May 2022 23:16:49 +0900 Message-Id: <20220514141650.1109542-4-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220514141650.1109542-1-mailhol.vincent@wanadoo.fr> References: <20220513142355.250389-1-mailhol.vincent@wanadoo.fr> <20220514141650.1109542-1-mailhol.vincent@wanadoo.fr> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org The functions can_dropped_invalid_skb() and can_skb_headroom_valid() grew a lot over the years to a point which it does not make much sense to have them defined as static inline in header files. Move those two functions to the .c counterpart of skb.h. can_skb_headroom_valid() only caller being can_dropped_invalid_skb(), the declaration is removed from the header. Only can_dropped_invalid_skb() gets its symbol exported. While doing so, do a small cleanup: add brackets around the else block in can_dropped_invalid_skb(). Signed-off-by: Vincent Mailhol --- drivers/net/can/dev/skb.c | 58 ++++++++++++++++++++++++++++++++++++++ include/linux/can/skb.h | 59 +-------------------------------------- 2 files changed, 59 insertions(+), 58 deletions(-) diff --git a/drivers/net/can/dev/skb.c b/drivers/net/can/dev/skb.c index 61660248c69e..8b1991130de5 100644 --- a/drivers/net/can/dev/skb.c +++ b/drivers/net/can/dev/skb.c @@ -252,3 +252,61 @@ struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf) return skb; } EXPORT_SYMBOL_GPL(alloc_can_err_skb); + +/* Check for outgoing skbs that have not been created by the CAN subsystem */ +static bool can_skb_headroom_valid(struct net_device *dev, struct sk_buff *skb) +{ + /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */ + if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv))) + return false; + + /* af_packet does not apply CAN skb specific settings */ + if (skb->ip_summed == CHECKSUM_NONE) { + /* init headroom */ + can_skb_prv(skb)->ifindex = dev->ifindex; + can_skb_prv(skb)->skbcnt = 0; + + skb->ip_summed = CHECKSUM_UNNECESSARY; + + /* perform proper loopback on capable devices */ + if (dev->flags & IFF_ECHO) + skb->pkt_type = PACKET_LOOPBACK; + else + skb->pkt_type = PACKET_HOST; + + skb_reset_mac_header(skb); + skb_reset_network_header(skb); + skb_reset_transport_header(skb); + } + + return true; +} + +/* Drop a given socketbuffer if it does not contain a valid CAN frame. */ +bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb) +{ + const struct canfd_frame *cfd = (struct canfd_frame *)skb->data; + + if (skb->protocol == htons(ETH_P_CAN)) { + if (unlikely(skb->len != CAN_MTU || + cfd->len > CAN_MAX_DLEN)) + goto inval_skb; + } else if (skb->protocol == htons(ETH_P_CANFD)) { + if (unlikely(skb->len != CANFD_MTU || + cfd->len > CANFD_MAX_DLEN)) + goto inval_skb; + } else { + goto inval_skb; + } + + if (!can_skb_headroom_valid(dev, skb)) + goto inval_skb; + + return false; + +inval_skb: + kfree_skb(skb); + dev->stats.tx_dropped++; + return true; +} +EXPORT_SYMBOL_GPL(can_dropped_invalid_skb); diff --git a/include/linux/can/skb.h b/include/linux/can/skb.h index fdb22b00674a..182749e858b3 100644 --- a/include/linux/can/skb.h +++ b/include/linux/can/skb.h @@ -31,6 +31,7 @@ struct sk_buff *alloc_canfd_skb(struct net_device *dev, struct canfd_frame **cfd); struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf); +bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb); /* * The struct can_skb_priv is used to transport additional information along @@ -96,64 +97,6 @@ static inline struct sk_buff *can_create_echo_skb(struct sk_buff *skb) return nskb; } -/* Check for outgoing skbs that have not been created by the CAN subsystem */ -static inline bool can_skb_headroom_valid(struct net_device *dev, - struct sk_buff *skb) -{ - /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */ - if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv))) - return false; - - /* af_packet does not apply CAN skb specific settings */ - if (skb->ip_summed == CHECKSUM_NONE) { - /* init headroom */ - can_skb_prv(skb)->ifindex = dev->ifindex; - can_skb_prv(skb)->skbcnt = 0; - - skb->ip_summed = CHECKSUM_UNNECESSARY; - - /* perform proper loopback on capable devices */ - if (dev->flags & IFF_ECHO) - skb->pkt_type = PACKET_LOOPBACK; - else - skb->pkt_type = PACKET_HOST; - - skb_reset_mac_header(skb); - skb_reset_network_header(skb); - skb_reset_transport_header(skb); - } - - return true; -} - -/* Drop a given socketbuffer if it does not contain a valid CAN frame. */ -static inline bool can_dropped_invalid_skb(struct net_device *dev, - struct sk_buff *skb) -{ - const struct canfd_frame *cfd = (struct canfd_frame *)skb->data; - - if (skb->protocol == htons(ETH_P_CAN)) { - if (unlikely(skb->len != CAN_MTU || - cfd->len > CAN_MAX_DLEN)) - goto inval_skb; - } else if (skb->protocol == htons(ETH_P_CANFD)) { - if (unlikely(skb->len != CANFD_MTU || - cfd->len > CANFD_MAX_DLEN)) - goto inval_skb; - } else - goto inval_skb; - - if (!can_skb_headroom_valid(dev, skb)) - goto inval_skb; - - return false; - -inval_skb: - kfree_skb(skb); - dev->stats.tx_dropped++; - return true; -} - static inline bool can_is_canfd_skb(const struct sk_buff *skb) { /* the CAN specific type of skb is identified by its data length */ From patchwork Sat May 14 14:16:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 12849813 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3D01C433FE for ; Sat, 14 May 2022 14:18:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233193AbiENOSK (ORCPT ); Sat, 14 May 2022 10:18:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233188AbiENOR4 (ORCPT ); Sat, 14 May 2022 10:17:56 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F37E0DFE9; Sat, 14 May 2022 07:17:42 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id b12so1138365pju.3; Sat, 14 May 2022 07:17:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZEhSGoMoojYAD36SANNLcism31dWp4f4Nf5+aItJuTY=; b=jbklFPhslW+4+HkKyrtVXePxSFmMiySYB9NlkM82qwUxV8msgdLqyTMXdM5J1D4Sj3 ee47pqFiYwtIU5YThhtubn4Xepiuv3+7H1I83C3XZV6H1nFWvUm3RnPZ5TNjUj+hfoq9 G4W+V62LmFEBFcc8Qkn2M8/ai4vSycDl7R/rfvkYvg359NOM7aUrW7kYA6QVM8vebaH8 0c2kVrNGpB5WMhFGulffw2N8wwhSTwgZpJYTsgVVKocnLBinHyXR4PLcc1lc6rvbY+gp kc9tWTnBCojyNcT41fq6r6eRMfexRZ9Bqf+WdfcICVefWdyxaVwJNOis3qhjbbxMltva BN2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=ZEhSGoMoojYAD36SANNLcism31dWp4f4Nf5+aItJuTY=; b=PRZAyr4lvlZ9TTPN6qoNbCAL0dz9enMw0R7T9V2qzBi1ZVZJJDglZkBBaJKmz4KMQ7 fclA4oEbIAI/qfQ3E/CrE76TgazbaP7DbbXT649b5El/QaFXJ334S4ET1psw+EvkbKiv ujSR6mTykx55WoPCCQxnpLddK4NiZ+vvvssX9OrVyjkhbMsr/FijPAxRyZVP/prVZ38d Q4r6Z+gTSjZ7aiEWxqMjcRPAJyF5NXaBVtVsWpm4UxfcKPRbMxBbbRn3OUgfBcsMvMK/ G9YPtNZfVhUUXAI/HmcrF6JdWmJyNdJoNk0MRwfvfgGgrleDXZ1HWiSc7NRLqmCSctrR GmQQ== X-Gm-Message-State: AOAM531NPEa234VDX+YOWkNWHZbeOlGUHYYHm/vxFaMJKmiAVB2dEaO7 1KIfwNactg+GYttW5om0lGE= X-Google-Smtp-Source: ABdhPJzUSRBGx8S6BfJhi0bU2llugcyFbguZzUWUlbDGCnMW6DC9OHT0oX2pZFcdesE+mCKW/C6CxA== X-Received: by 2002:a17:90b:4a4a:b0:1dc:4731:31a4 with SMTP id lb10-20020a17090b4a4a00b001dc473131a4mr10171413pjb.19.1652537862493; Sat, 14 May 2022 07:17:42 -0700 (PDT) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id x8-20020a17090a530800b001cd4989feccsm5298541pjh.24.2022.05.14.07.17.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 May 2022 07:17:42 -0700 (PDT) Sender: Vincent Mailhol From: Vincent Mailhol To: Marc Kleine-Budde Cc: linux-can@vger.kernel.org, linux-kernel@vger.kernel.org, Max Staudt , Oliver Hartkopp , netdev@vger.kernel.org, Vincent Mailhol Subject: [PATCH v3 4/4] can: dev: drop tx skb if in listen only mode Date: Sat, 14 May 2022 23:16:50 +0900 Message-Id: <20220514141650.1109542-5-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220514141650.1109542-1-mailhol.vincent@wanadoo.fr> References: <20220513142355.250389-1-mailhol.vincent@wanadoo.fr> <20220514141650.1109542-1-mailhol.vincent@wanadoo.fr> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Frames can be directly injected to a can driver via the packet socket. By doing that, it is possible to reach the net_device_ops::ndo_start_xmit function even if the driver is configured in listen only mode. Add a check in can_dropped_invalid_skb() to discard the skb if CAN_CTRLMODE_LISTENONLY is set. Signed-off-by: Vincent Mailhol --- drivers/net/can/dev/skb.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/net/can/dev/skb.c b/drivers/net/can/dev/skb.c index 8b1991130de5..f7420fc43b99 100644 --- a/drivers/net/can/dev/skb.c +++ b/drivers/net/can/dev/skb.c @@ -5,6 +5,7 @@ */ #include +#include /* Local echo of CAN messages * @@ -286,6 +287,7 @@ static bool can_skb_headroom_valid(struct net_device *dev, struct sk_buff *skb) bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb) { const struct canfd_frame *cfd = (struct canfd_frame *)skb->data; + struct can_priv *priv = netdev_priv(dev); if (skb->protocol == htons(ETH_P_CAN)) { if (unlikely(skb->len != CAN_MTU || @@ -299,8 +301,13 @@ bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb) goto inval_skb; } - if (!can_skb_headroom_valid(dev, skb)) + if (!can_skb_headroom_valid(dev, skb)) { + goto inval_skb; + } else if (priv->ctrlmode & CAN_CTRLMODE_LISTENONLY) { + netdev_info_once(dev, + "interface in listen only mode, dropping skb\n"); goto inval_skb; + } return false;