1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "metrics_allowlist.h"
18 
19 #include <cstdint>
20 #include <cstring>
21 
22 namespace bluetooth {
23 namespace metrics {
24 
25 namespace {
26 
27 static constexpr int device_info_allow_list_sig[][2] = {
28         /* Ericsson Technology Licensing */
29         {0, 52},
30         /* Motorola */
31         {8, 11905},
32         /* Qualcomm Technologies International, Ltd. (QTIL) */
33         {10, 65535},
34         /* Texas Instruments Inc. */
35         {13, 0},
36         /* Broadcom Corporation. */
37         {15, 4608},
38         /* Qualcomm */
39         {29, 4608},
40         /* Integrated System Solution Corp. */
41         {57, 5028},
42         {57, 5506},
43         /* MediaTek, Inc. */
44         {70, 4608},
45         /* Apple, Inc. */
46         {76, 8194},
47         {76, 8198},
48         {76, 8201},
49         {76, 8203},
50         {76, 8204},
51         {76, 8206},
52         {76, 8207},
53         {76, 8208},
54         {76, 8211},
55         /* Harman International Industries, Inc. */
56         {87, 35},
57         {87, 7977},
58         /* Realtek Semiconductor Corporation */
59         {93, 8763},
60         /* Samsung Electronics Co. Ltd. */
61         {117, 256},
62         {117, 40977},
63         {117, 40978},
64         {117, 40979},
65         /* Airoha Technology Corp. */
66         {148, 4},
67         {148, 291},
68         /* LG Electronics​ */
69         {196, 5025},
70         /* Google */
71         {224, 0},
72         {224, 4608},
73         {224, 12288},
74         {224, 12289},
75         {224, 12290},
76         {224, 12291},
77         {224, 12292},
78         {224, 12544},
79         {224, 50181},
80         /* Amazon.com Services, LLC */
81         {369, 384},
82         /* Bestechnic(Shanghai),Ltd */
83         {688, 0},
84         /* LEGO System A/S */
85         {919, 1},
86         /* Actions (Zhuhai) Technology Co., Limited */
87         {992, 12298},
88         /* STABILO International */
89         {1256, 32896},
90         /* GoerTek Dynaudio Co., Ltd. */
91         {1452, 544},
92         /* Zhuhai Jieli technology Co.,Ltd */
93         {1494, 10}};
94 
95 static constexpr int device_info_allow_list_usb[][2] = {
96         /* Unknown */
97         {14, 13330},
98         /* Unknown */
99         {97, 1},
100         /* Unknown  */
101         {125, 628},
102         /* HP, Inc */
103         {1008, 2124},
104         {1008, 588},
105         /* Unknown  */
106         {1014, 40961},
107         /* Microsoft Corp. */
108         {1118, 736},
109         {1118, 765},
110         {1118, 1954},
111         {1118, 2053},
112         {1118, 2054},
113         {1118, 2087},
114         {1118, 2095},
115         {1118, 2326},
116         {1118, 2354},
117         {1118, 2397},
118         {1118, 2835},
119         {1118, 2848},
120         /* Primax Electronics, Ltd */
121         {1121, 20206},
122         {1121, 20207},
123         /* Logitech, Inc. */
124         {1133, 45072},
125         {1133, 45076},
126         {1133, 45077},
127         {1133, 45078},
128         {1133, 45081},
129         {1133, 45082},
130         {1133, 45083},
131         {1133, 45089},
132         {1133, 45091},
133         {1133, 45093},
134         {1133, 45094},
135         {1133, 45095},
136         {1133, 45883},
137         {1133, 45885},
138         {1133, 45890},
139         {1133, 45901},
140         {1133, 45915},
141         {1133, 45917},
142         {1133, 45890},
143         /* Samsung Electronics Co., Ltd */
144         {1256, 28705},
145         /* Sony Corp. */
146         {1356, 1476},
147         {1356, 2508},
148         {1356, 3302},
149         /* Wacom Co., Ltd */
150         {1386, 887},
151         /* Nintendo Co., Ltd */
152         {1406, 8198},
153         {1406, 8199},
154         {1406, 8201},
155         /* Apple, Inc. */
156         {1452, 544},
157         {1452, 556},
158         {1452, 569},
159         {1452, 591},
160         {1452, 597},
161         {1452, 781},
162         {1452, 12850},
163         /* Zippy Technology Corp. */
164         {2458, 1280},
165         /* Broadcom Corp. */
166         {2652, 1},
167         {2652, 17667},
168         {2652, 63369},
169         /* Microdia */
170         {3141, 32270},
171         /* Focusrite-Novation */
172         {4661, 43554},
173         /* Razer USA, Ltd */
174         {5426, 130},
175         /* Nordic Semiconductor ASA */
176         {6421, 64},
177         /* Lab126, Inc. */
178         {6473, 1026},
179         /* Anker Innovations Limited */
180         {10522, 34050},
181         /* Unknown */
182         {12994, 1},
183         /* Fuji Yusoki Kogyo Co., Ltd. */
184         {44580, 34328},
185 };
186 
187 enum Transport {
188   USB = 1,
189   UART = 2,
190   SDIO = 3,
191 };
192 
193 /* Below chipset info allowlists are generated by the script:
194  * go/cros-chipset-allowlist-generator
195  * and feeds with the data in tsv format
196  * go/cros-chipset-info-0512
197  */
198 static constexpr int chipset_info_allow_list_vidpid[][3] = {
199         /* Intel */
200         {USB, 0x8086, 0x095a},  // INTEL-AC7265
201         {USB, 0x8086, 0x31dc},  // INTEL-AC9560
202         {USB, 0x8086, 0x4df0},  // INTEL-AX201
203         {USB, 0x8086, 0x095b},  // INTEL-AC7265
204         {USB, 0x8086, 0x02f0},  // INTEL-AX201
205         {USB, 0x8086, 0x51f0},
206         {USB, 0x8086, 0xa0f0},  // INTEL-AX201
207         {USB, 0x8086, 0x2526},  // INTEL-AC9260
208         {USB, 0x8086, 0x3165},
209         {USB, 0x8086, 0x2723},  // INTEL-AX200
210         {USB, 0x8086, 0x08b1},  // INTEL-AC7260
211         {USB, 0x8086, 0x24fd},
212         {USB, 0x8086, 0x9df0},  // INTEL-AC9560
213         {USB, 0x8086, 0x08b3},
214         {USB, 0x8086, 0x24fb},
215         {USB, 0x8086, 0x3166},
216         {USB, 0x8086, 0x24f3},
217         {USB, 0x8086, 0x08b2},  // INTEL-AC7260
218         {USB, 0x8086, 0x08b4},
219         {USB, 0x8086, 0x0887},
220         {USB, 0x8086, 0x4232},
221         {USB, 0x8086, 0x088e},
222         {USB, 0x8086, 0x0085},
223         {USB, 0x8086, 0x0082},
224         {USB, 0x8086, 0x4239},
225         {USB, 0x8086, 0x4238},
226         {USB, 0x8086, 0x4222},
227         {USB, 0x8086, 0x008a},
228         {USB, 0x8086, 0xa370},
229         {USB, 0x8086, 0x54f0},
230         {USB, 0x8086, 0x422c},
231         {USB, 0x8086, 0x4237},
232         {USB, 0x8086, 0x4229},
233         /* Qualcomm */
234         {USB, 0x168c, 0x003e},   // QCA-6174A-5
235         {UART, 0x0271, 0x050a},  // QCA-6174A-3
236         {USB, 0x168c, 0x0042},
237         {USB, 0x168c, 0x0036},
238         {USB, 0x168c, 0x0032},
239         {USB, 0x168c, 0x002b},
240         {USB, 0x168c, 0x0034},
241         {USB, 0x168c, 0x002a},
242         {USB, 0x168c, 0x0030},
243         /* Realtek */
244         {USB, 0x10ec, 0xc822},  // Realtek-RTL8822C-USB
245         {USB, 0x10ec, 0xb723},
246         {USB, 0x10ec, 0xc821},
247         {USB, 0x10ec, 0x8821},
248         {USB, 0x10ec, 0xb822},
249         {USB, 0x10ec, 0x8852},  // Realtek-RTL8852A-USB
250         {USB, 0x10ec, 0x8723},
251         {USB, 0x10ec, 0x8176},
252         {UART, 0x10ec, 0xc822},  // Realtek-RTL8822C-UART
253         {USB, 0x10ec, 0xc82f},
254         /* Marvell */
255         {SDIO, 0x02df, 0x912d},  // MVL-8897
256         {USB, 0x1b4b, 0x2b42},   // MVL-8997
257         /* Unknown */
258         {USB, 0x14e4, 0x4365},
259         {USB, 0x14e4, 0x43a0},
260         {USB, 0x1814, 0x3090},
261         /* MediaTek */
262         {USB, 0x14c3, 0x7961},   // Mediatek-MTK7921-USB
263         {SDIO, 0x037a, 0x7901},  // Mediatek-MTK7921-SDIO
264 };
265 
266 static constexpr struct chipstr_transport {
267   int transport;
268   uint64_t hash_value;
269   const char* str;
270 } chipset_info_allow_list_str[] = {
271         {USB, 0x676651522ac38489ULL, "usb:v8087p0AAAd0002dcE0dsc01dp01icE0isc01ip01in00"},
272         {UART, 0x0f7029c4e4bee5c2ULL, "of:NbluetoothT(null)Cqcom,wcn3991-bt"},
273         {USB, 0xc87adddb3473cef2ULL, "usb:v8087p0033d0000dcE0dsc01dp01icE0isc01ip01in00"},
274         {USB, 0xd4af1ceff203aa54ULL, "usb:v8087p0A2Ad0003dcE0dsc01dp01icE0isc01ip01in00"},
275         {USB, 0x67ff2e0860c6088cULL, "usb:v8087p0026d0002dcE0dsc01dp01icE0isc01ip01in00"},
276         {USB, 0x5ea49b43333b6757ULL, "usb:v0BDApB009d0200dcE0dsc01dp01icE0isc01ip01in00"},
277         {USB, 0xae4252633a7fc378ULL, "usb:v0A12p0001d8891dcE0dsc01dp01icE0isc01ip01in00"},
278         {USB, 0xe8b1d7dcab4a9180ULL, "usb:v05ACp821Fd0156dcFFdsc01dp01icFFisc01ip01in00"},
279         {USB, 0xf2fc524b34906492ULL, "usb:v413Cp8160d0173dcE0dsc01dp01icE0isc01ip01in00"},
280         {USB, 0xa16695ffe3ebcf8eULL, "usb:v0BDApB00Cd0000dcE0dsc01dp01icE0isc01ip01in00"},
281         {USB, 0x45fa5f31f455c161ULL, "usb:v05ACp821Ad0042dcFFdsc01dp01icFFisc01ip01in00"},
282         {USB, 0xed0707b5f055458dULL, "usb:v05ACp821Dd0156dcFFdsc01dp01icFFisc01ip01in00"},
283         {UART, 0x645c8874e1a27038ULL, "of:NbluetoothT(null)Cqcom,wcn6750-bt"},
284         {USB, 0xf8feae883f843f61ULL, "usb:v0A5Cp21BCd0761dcE0dsc01dp01icE0isc01ip01in00"},
285         {USB, 0x3db1b39b76523e6fULL, "usb:v0A5Cp219Cd0628dcE0dsc01dp01icE0isc01ip01in00"},
286         {USB, 0xa4fec7094cdb7bedULL, "usb:v05ACp8205d1965dcE0dsc01dp01icE0isc01ip01in00"},
287         {USB, 0x952ae80214a19dd9ULL, "usb:v05ACp828Ad0150dcFFdsc01dp01icFFisc01ip01in00"},
288         {USB, 0x40e84e9cf6844c32ULL, "usb:v8087p0025d0002dcE0dsc01dp01icE0isc01ip01in00"},
289         {USB, 0x6b037999f75bc6ceULL, "usb:v0A5Cp21F1d0112dcFFdsc01dp01icFFisc01ip01in00"},
290         {USB, 0x91be30aba4f17b7eULL, "usb:v8087p0A2Ad0001dcE0dsc01dp01icE0isc01ip01in00"},
291         {USB, 0xd393d6690ca2c212ULL, "usb:v0BDApB720d0200dcEFdsc02dp01icE0isc01ip01in00"},
292 };
293 
294 }  // namespace
295 
IsDeviceInfoInAllowlist(int vendor_id_source,int vendor_id,int product_id)296 bool IsDeviceInfoInAllowlist(int vendor_id_source, int vendor_id, int product_id) {
297   if (vendor_id_source == 1) {
298     for (int i = 0; i < sizeof(device_info_allow_list_sig) / sizeof(device_info_allow_list_sig[0]);
299          i++) {
300       if (vendor_id == device_info_allow_list_sig[i][0] &&
301           product_id == device_info_allow_list_sig[i][1]) {
302         return true;
303       }
304     }
305   } else if (vendor_id_source == 2) {
306     for (int i = 0; i < sizeof(device_info_allow_list_usb) / sizeof(device_info_allow_list_usb[0]);
307          i++) {
308       if (vendor_id == device_info_allow_list_usb[i][0] &&
309           product_id == device_info_allow_list_usb[i][1]) {
310         return true;
311       }
312     }
313   }
314 
315   return false;
316 }
317 
IsChipsetInfoInAllowList(int vendor_id,int product_id,int transport,const char * chipset_string,uint64_t * hval)318 bool IsChipsetInfoInAllowList(int vendor_id, int product_id, int transport,
319                               const char* chipset_string, uint64_t* hval) {
320   if (vendor_id != 0 && product_id != 0) {
321     for (int i = 0;
322          i < sizeof(chipset_info_allow_list_vidpid) / sizeof(chipset_info_allow_list_vidpid[0]);
323          i++) {
324       const int* record = chipset_info_allow_list_vidpid[i];
325 
326       if (transport == record[0] && vendor_id == record[1] && product_id == record[2]) {
327         return true;
328       }
329     }
330   } else if (!!chipset_string && !!strlen(chipset_string)) {
331     for (int i = 0;
332          i < sizeof(chipset_info_allow_list_str) / sizeof(chipset_info_allow_list_str[0]); i++) {
333       const struct chipstr_transport* record = &chipset_info_allow_list_str[i];
334 
335       if (strcmp(chipset_string, record->str) == 0 && transport == record->transport) {
336         *hval = record->hash_value;
337         return true;
338       }
339     }
340   }
341 
342   return false;
343 }
344 
345 }  // namespace metrics
346 }  // namespace bluetooth
347