Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4229369pxy; Mon, 26 Apr 2021 23:09:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxR6+jS/9+NW3tAettEohTROGEGKbYL1WKLMEro+qeUHegkBcqiYHoheIb11Z4YqLbP4N39 X-Received: by 2002:a63:1914:: with SMTP id z20mr20124180pgl.250.1619503741067; Mon, 26 Apr 2021 23:09:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619503741; cv=none; d=google.com; s=arc-20160816; b=Q7Tq/x4k/TSmPgSoZv0kJe5FxnjbLPPjbXlpmz077Q5i2v2lZenHDVl2GgMF2d2dVU BgkjwLDlNc6qfmaxLxioVO/PavV9o1/i3gnFhy5XONbtjxPPp+/2OD+kCLCrvB0YXLVM E1Yj0OlsbZrUL/54EnzLOhHQZ76gGPM2Hf9855U4pFRw9hcTMZ3fjRPYZPkcpOFExjCV U9R6ZbmGQI96XEhnFZnLTEZFl80D2LrYQfm9a91MVHRawbk5FM4Wc+jLYhYrbDVD9amp QpiRZOX0y15hZEob8BKCu/xzHbl5QadqZdmfoyI/gJzEfYiuDaegO4NPL7HT+1ZPc19r HWZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version; bh=8EEk1GFgxo0auYokEFcAdC1T+aptF8cmjHepE7druB4=; b=m7p8X0OHX8orDS+RJFCO8NJIeM7fYlx5nY8FRCsVy9L9Syn3mumU1DP/Ys5U/401Nw GmvLqjZKGdq7LnkAwoTHeTY44hDEkb3Jse1A+5tYMDXIANOhQUw1UJtAwIvpujg6/5ua nbCtz694eodwsd/TqSlVpSvsiAfJHj8cnpsfOlRsiiPq8PMTjODvNkT+wq5v9nOSKGZf riEqrAUllJwoeyy+ONbSyPY3ccu3XWynbVhw0vX3AWFedAzCv1EoTVf9Y5NDOxjF3E6I bziWImiXwO6irXeilrq7GevwrxLMEwPdM+lF5Byumo9xl1Ir0nQmjfAfvv29GchSTtz6 RR+A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m9si21073221plx.67.2021.04.26.23.08.22; Mon, 26 Apr 2021 23:09:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229578AbhD0GHy convert rfc822-to-8bit (ORCPT + 99 others); Tue, 27 Apr 2021 02:07:54 -0400 Received: from coyote.holtmann.net ([212.227.132.17]:54889 "EHLO mail.holtmann.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229535AbhD0GHy (ORCPT ); Tue, 27 Apr 2021 02:07:54 -0400 Received: from marcel-macbook.holtmann.net (p4fefc624.dip0.t-ipconnect.de [79.239.198.36]) by mail.holtmann.org (Postfix) with ESMTPSA id 30AEECECF7; Tue, 27 Apr 2021 08:14:59 +0200 (CEST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.60.0.2.21\)) Subject: Re: [PATCH v2 01/10] Bluetooth: HCI: Use skb_pull to parse BR/EDR events From: Marcel Holtmann In-Reply-To: Date: Tue, 27 Apr 2021 08:07:10 +0200 Cc: "linux-bluetooth@vger.kernel.org" Content-Transfer-Encoding: 8BIT Message-Id: References: <20210419171257.3865181-1-luiz.dentz@gmail.com> <20210419171257.3865181-2-luiz.dentz@gmail.com> <25D86C94-89CE-44CC-BB9C-724486444C12@holtmann.org> To: Luiz Augusto von Dentz X-Mailer: Apple Mail (2.3654.60.0.2.21) Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Hi Luiz, >>> This uses skb_pull to check the BR/EDR events received have the minimum >>> required length. >>> >>> Signed-off-by: Luiz Augusto von Dentz >>> --- >>> include/net/bluetooth/hci.h | 4 + >>> net/bluetooth/hci_event.c | 272 +++++++++++++++++++++++++++++------- >>> 2 files changed, 229 insertions(+), 47 deletions(-) >>> >>> diff --git a/include/net/bluetooth/hci.h b/include/net/bluetooth/hci.h >>> index ea4ae551c426..f1f505355e81 100644 >>> --- a/include/net/bluetooth/hci.h >>> +++ b/include/net/bluetooth/hci.h >>> @@ -1894,6 +1894,10 @@ struct hci_cp_le_reject_cis { >>> } __packed; >>> >>> /* ---- HCI Events ---- */ >>> +struct hci_ev_status { >>> + __u8 status; >>> +} __packed; >>> + >>> #define HCI_EV_INQUIRY_COMPLETE 0x01 >>> >>> #define HCI_EV_INQUIRY_RESULT 0x02 >>> diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c >>> index 5e99968939ce..077541fcba41 100644 >>> --- a/net/bluetooth/hci_event.c >>> +++ b/net/bluetooth/hci_event.c >>> @@ -42,6 +42,30 @@ >>> >>> /* Handle HCI Event packets */ >>> >>> +static void *hci_skb_pull(struct sk_buff *skb, size_t len) >>> +{ >>> + void *data = skb->data; >>> + >>> + if (skb->len < len) >>> + return NULL; >>> + >>> + skb_pull(skb, len); >>> + >>> + return data; >>> +} >>> + >>> +static void *hci_ev_skb_pull(struct hci_dev *hdev, struct sk_buff *skb, >>> + u8 ev, size_t len) >>> +{ >>> + void *data; >>> + >>> + data = hci_skb_pull(skb, len); >>> + if (!data) >>> + bt_dev_err(hdev, "Malformed Event: 0x%2.2x", ev); >>> + >>> + return data; >>> +} >>> + >> >> so if you do it in one function, this will look like this: >> >> static void *hci_ev_skb_pull(..) >> { >> void *data = skb->data; >> >> if (skb->len < len) { >> bt_dev_err(hdev, “Malformed ..”); >> return NULL; >> } >> >> skb_pull(skb, len); >> return data; >> } >> >> static void *hci_cc_skb_pull(..) >> { >> void *data = skb->data; >> >> if (skb->len < len) { >> bt_dev_err(..); >> return NULL; >> } >> >> skb_pull(..); >> return data; >> } >> >> I realize that you want to optimize the code with hci_skb_pull, but I don’t see how that makes it simpler or cleaner. In the end you just have spaghetti code and don’t win anything in size reduction or complexity. > > Right, I can either do that or perhaps bite the bullet and convert the > whole hci_event.c to use a function table: > > #define HCI_EVENT(_op, _len, _func)... > > struct hci_event { > __u8 op; > __u16 len; > func... > } ev_table[] = { > HCI_EVENT(...), > } > > But that would pack a lot more changes since we would need to convert > the whole switch to a for loop or alternatively if we don't want do a > lookup we could omit the op and access the table by index if we want > to trade speed over code size, with that we can have just one call to > the likes of hci_ev_skb_pull. looping through a table is not ideal. There are too many events that you can receive and if we end up looping for every LE advertising report it would be rather silly. And of course a static table is possible since event numbers are assigned in order, but it also means some sort of overhead since we don’t parse each event. In addition to that dilemma, you have the problem that not all events are fixed size. So you end up indicating that similar to how we do it in btmon which will increase the table size again. Maybe that is actually ok since two giant switch statements also take time and code size. So our currently max event code is 0x57 for Authenticated Payload Timeout and the max LE event code is 0x3e for BIG Info Advertising Report. Meaning table one would have 87 entries and table would have two 63 entries. If we do min_len,max_len as uint8 then we have 2 octets and a function pointer. Maybe that is not too bad since that is all static const data. Regards Marcel