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 
188 enum Transport {
189   USB = 1,
190   UART = 2,
191   SDIO = 3,
192 };
193 
194 /* Below chipset info allowlists are generated by the script:
195  * go/cros-chipset-allowlist-generator
196  * and feeds with the data in tsv format
197  * go/cros-chipset-info-0512
198  */
199 static constexpr int chipset_info_allow_list_vidpid[][3] = {
200     /* Intel */
201     {USB, 0x8086, 0x095a},  // INTEL-AC7265
202     {USB, 0x8086, 0x31dc},  // INTEL-AC9560
203     {USB, 0x8086, 0x4df0},  // INTEL-AX201
204     {USB, 0x8086, 0x095b},  // INTEL-AC7265
205     {USB, 0x8086, 0x02f0},  // INTEL-AX201
206     {USB, 0x8086, 0x51f0},
207     {USB, 0x8086, 0xa0f0},  // INTEL-AX201
208     {USB, 0x8086, 0x2526},  // INTEL-AC9260
209     {USB, 0x8086, 0x3165},
210     {USB, 0x8086, 0x2723},  // INTEL-AX200
211     {USB, 0x8086, 0x08b1},  // INTEL-AC7260
212     {USB, 0x8086, 0x24fd},
213     {USB, 0x8086, 0x9df0},  // INTEL-AC9560
214     {USB, 0x8086, 0x08b3},
215     {USB, 0x8086, 0x24fb},
216     {USB, 0x8086, 0x3166},
217     {USB, 0x8086, 0x24f3},
218     {USB, 0x8086, 0x08b2},  // INTEL-AC7260
219     {USB, 0x8086, 0x08b4},
220     {USB, 0x8086, 0x0887},
221     {USB, 0x8086, 0x4232},
222     {USB, 0x8086, 0x088e},
223     {USB, 0x8086, 0x0085},
224     {USB, 0x8086, 0x0082},
225     {USB, 0x8086, 0x4239},
226     {USB, 0x8086, 0x4238},
227     {USB, 0x8086, 0x4222},
228     {USB, 0x8086, 0x008a},
229     {USB, 0x8086, 0xa370},
230     {USB, 0x8086, 0x54f0},
231     {USB, 0x8086, 0x422c},
232     {USB, 0x8086, 0x4237},
233     {USB, 0x8086, 0x4229},
234     /* Qualcomm */
235     {USB, 0x168c, 0x003e},   // QCA-6174A-5
236     {UART, 0x0271, 0x050a},  // QCA-6174A-3
237     {USB, 0x168c, 0x0042},
238     {USB, 0x168c, 0x0036},
239     {USB, 0x168c, 0x0032},
240     {USB, 0x168c, 0x002b},
241     {USB, 0x168c, 0x0034},
242     {USB, 0x168c, 0x002a},
243     {USB, 0x168c, 0x0030},
244     /* Realtek */
245     {USB, 0x10ec, 0xc822},  // Realtek-RTL8822C-USB
246     {USB, 0x10ec, 0xb723},
247     {USB, 0x10ec, 0xc821},
248     {USB, 0x10ec, 0x8821},
249     {USB, 0x10ec, 0xb822},
250     {USB, 0x10ec, 0x8852},  // Realtek-RTL8852A-USB
251     {USB, 0x10ec, 0x8723},
252     {USB, 0x10ec, 0x8176},
253     {UART, 0x10ec, 0xc822},  // Realtek-RTL8822C-UART
254     {USB, 0x10ec, 0xc82f},
255     /* Marvell */
256     {SDIO, 0x02df, 0x912d},  // MVL-8897
257     {USB, 0x1b4b, 0x2b42},   // MVL-8997
258     /* Unknown */
259     {USB, 0x14e4, 0x4365},
260     {USB, 0x14e4, 0x43a0},
261     {USB, 0x1814, 0x3090},
262     /* MediaTek */
263     {USB, 0x14c3, 0x7961},   // Mediatek-MTK7921-USB
264     {SDIO, 0x037a, 0x7901},  // Mediatek-MTK7921-SDIO
265 };
266 
267 static constexpr struct chipstr_transport {
268   int transport;
269   uint64_t hash_value;
270   const char* str;
271 } chipset_info_allow_list_str[] = {
272     {USB, 0x676651522ac38489ULL, "usb:v8087p0AAAd0002dcE0dsc01dp01icE0isc01ip01in00"},
273     {UART, 0x0f7029c4e4bee5c2ULL, "of:NbluetoothT(null)Cqcom,wcn3991-bt"},
274     {USB, 0xc87adddb3473cef2ULL, "usb:v8087p0033d0000dcE0dsc01dp01icE0isc01ip01in00"},
275     {USB, 0xd4af1ceff203aa54ULL, "usb:v8087p0A2Ad0003dcE0dsc01dp01icE0isc01ip01in00"},
276     {USB, 0x67ff2e0860c6088cULL, "usb:v8087p0026d0002dcE0dsc01dp01icE0isc01ip01in00"},
277     {USB, 0x5ea49b43333b6757ULL, "usb:v0BDApB009d0200dcE0dsc01dp01icE0isc01ip01in00"},
278     {USB, 0xae4252633a7fc378ULL, "usb:v0A12p0001d8891dcE0dsc01dp01icE0isc01ip01in00"},
279     {USB, 0xe8b1d7dcab4a9180ULL, "usb:v05ACp821Fd0156dcFFdsc01dp01icFFisc01ip01in00"},
280     {USB, 0xf2fc524b34906492ULL, "usb:v413Cp8160d0173dcE0dsc01dp01icE0isc01ip01in00"},
281     {USB, 0xa16695ffe3ebcf8eULL, "usb:v0BDApB00Cd0000dcE0dsc01dp01icE0isc01ip01in00"},
282     {USB, 0x45fa5f31f455c161ULL, "usb:v05ACp821Ad0042dcFFdsc01dp01icFFisc01ip01in00"},
283     {USB, 0xed0707b5f055458dULL, "usb:v05ACp821Dd0156dcFFdsc01dp01icFFisc01ip01in00"},
284     {UART, 0x645c8874e1a27038ULL, "of:NbluetoothT(null)Cqcom,wcn6750-bt"},
285     {USB, 0xf8feae883f843f61ULL, "usb:v0A5Cp21BCd0761dcE0dsc01dp01icE0isc01ip01in00"},
286     {USB, 0x3db1b39b76523e6fULL, "usb:v0A5Cp219Cd0628dcE0dsc01dp01icE0isc01ip01in00"},
287     {USB, 0xa4fec7094cdb7bedULL, "usb:v05ACp8205d1965dcE0dsc01dp01icE0isc01ip01in00"},
288     {USB, 0x952ae80214a19dd9ULL, "usb:v05ACp828Ad0150dcFFdsc01dp01icFFisc01ip01in00"},
289     {USB, 0x40e84e9cf6844c32ULL, "usb:v8087p0025d0002dcE0dsc01dp01icE0isc01ip01in00"},
290     {USB, 0x6b037999f75bc6ceULL, "usb:v0A5Cp21F1d0112dcFFdsc01dp01icFFisc01ip01in00"},
291     {USB, 0x91be30aba4f17b7eULL, "usb:v8087p0A2Ad0001dcE0dsc01dp01icE0isc01ip01in00"},
292     {USB, 0xd393d6690ca2c212ULL, "usb:v0BDApB720d0200dcEFdsc02dp01icE0isc01ip01in00"},
293 };
294 
295 }  // namespace
296 
IsDeviceInfoInAllowlist(int vendor_id_source,int vendor_id,int product_id)297 bool IsDeviceInfoInAllowlist(int vendor_id_source, int vendor_id, int product_id) {
298   if (vendor_id_source == 1) {
299     for (int i = 0; i < sizeof(device_info_allow_list_sig) / sizeof(device_info_allow_list_sig[0]); i++) {
300       if (vendor_id == device_info_allow_list_sig[i][0] && product_id == device_info_allow_list_sig[i][1]) {
301         return true;
302       }
303     }
304   } else if (vendor_id_source == 2) {
305     for (int i = 0; i < sizeof(device_info_allow_list_usb) / sizeof(device_info_allow_list_usb[0]); i++) {
306       if (vendor_id == device_info_allow_list_usb[i][0] && product_id == device_info_allow_list_usb[i][1]) {
307         return true;
308       }
309     }
310   }
311 
312   return false;
313 }
314 
IsChipsetInfoInAllowList(int vendor_id,int product_id,int transport,const char * chipset_string,uint64_t * hval)315 bool IsChipsetInfoInAllowList(
316     int vendor_id, int product_id, int transport, const char* chipset_string, uint64_t* hval) {
317   if (vendor_id != 0 && product_id != 0) {
318     for (int i = 0; i < sizeof(chipset_info_allow_list_vidpid) / sizeof(chipset_info_allow_list_vidpid[0]); i++) {
319       const int* record = chipset_info_allow_list_vidpid[i];
320 
321       if (transport == record[0] && vendor_id == record[1] && product_id == record[2]) {
322         return true;
323       }
324     }
325   } else if (!!chipset_string && !!strlen(chipset_string)) {
326     for (int i = 0; i < sizeof(chipset_info_allow_list_str) / sizeof(chipset_info_allow_list_str[0]); i++) {
327       const struct chipstr_transport* record = &chipset_info_allow_list_str[i];
328 
329       if (strcmp(chipset_string, record->str) == 0 && transport == record->transport) {
330         *hval = record->hash_value;
331         return true;
332       }
333     }
334   }
335 
336   return false;
337 }
338 
339 }  // namespace metrics
340 }  // namespace bluetooth