]> mj.ucw.cz Git - pciutils.git/blob - lib/filter.c
Filters: Allow leading "0x" for backward compatibility
[pciutils.git] / lib / filter.c
1 /*
2  *      The PCI Library -- Device Filtering
3  *
4  *      Copyright (c) 1998--2022 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "internal.h"
13
14 void pci_filter_init_v38(struct pci_access *a UNUSED, struct pci_filter *f) VERSIONED_ABI;
15 char *pci_filter_parse_slot_v38(struct pci_filter *f, char *str) VERSIONED_ABI;
16 char *pci_filter_parse_id_v38(struct pci_filter *f, char *str) VERSIONED_ABI;
17 int pci_filter_match_v38(struct pci_filter *f, struct pci_dev *d) VERSIONED_ABI;
18
19 void
20 pci_filter_init_v38(struct pci_access *a UNUSED, struct pci_filter *f)
21 {
22   memset((byte *) f, 0, sizeof(*f));
23   f->domain = f->bus = f->slot = f->func = -1;
24   f->vendor = f->device = -1;
25   f->device_class = -1;
26   f->device_class_mask = ~0U;
27   f->prog_if = -1;
28 }
29
30 #define BUF_SIZE 64
31
32 static char *
33 split_to_fields(char *str, char *buffer, int sep, char **fields, int num_fields)
34 {
35   if (buffer)
36     {
37       if (strlen(str) >= BUF_SIZE)
38         return "Expression too long";
39       strcpy(buffer, str);
40       str = buffer;
41     }
42
43   int i = 0;
44
45   for (;;)
46     {
47       if (i >= num_fields)
48         return "Too many fields";
49       fields[i++] = str;
50       while (*str && *str != sep)
51         str++;
52       if (!*str)
53         break;
54       *str++ = 0;
55     }
56
57   while (i < num_fields)
58     fields[i++] = NULL;
59
60   return NULL;
61 }
62
63 static int
64 field_defined(char *field)
65 {
66   return field && field[0] && strcmp(field, "*");
67 }
68
69 static int
70 parse_hex_field(char *str, int *outp, unsigned int *maskp, unsigned int max)
71 {
72   unsigned int out = 0;
73   unsigned int mask = ~0U;
74   unsigned int bound = 0;
75
76   if (!field_defined(str))
77     return 1;   // and keep the defaults
78
79   // Historically, filters allowed writing hexadecimal numbers with leading "0x".
80   // This was never intentional nor documented, but some people relied on it.
81   if (!maskp && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
82     str += 2;
83
84   while (*str)
85     {
86       int c = *str++;
87       int d;
88
89       if ((c == 'x' || c == 'X') && maskp)
90         {
91           out = out << 4;
92           bound = (bound << 4) | 1;
93           mask = mask << 4;
94         }
95       else
96         {
97           if (c >= '0' && c <= '9')
98             d = c - '0';
99           else if (c >= 'A' && c <= 'F')
100             d = c - 'A' + 10;
101           else if (c >= 'a' && c <= 'f')
102             d = c - 'a' + 10;
103           else
104             return 0;
105
106           out = (out << 4) | d;
107           bound = (bound << 4) | d;
108           mask = (mask << 4) | 0xf;
109         }
110
111       if (bound > max)
112         return 0;
113     }
114
115   *outp = out;
116   if (maskp)
117     *maskp = mask;
118   return 1;
119 }
120
121 /* Slot filter syntax: [[[domain]:][bus]:][slot][.[func]] */
122
123 char *
124 pci_filter_parse_slot_v38(struct pci_filter *f, char *str)
125 {
126   char buf[BUF_SIZE];
127   char *fields[3];
128   char *err;
129
130   if (err = split_to_fields(str, buf, ':', fields, 3))
131     return err;
132
133   int i = 0;
134   if (fields[2])
135     {
136       if (!parse_hex_field(fields[0], &f->domain, NULL, 0x7fffffff))
137         return "Invalid domain number";
138       i++;
139     }
140
141   if (fields[i+1])
142     {
143       if (!parse_hex_field(fields[i], &f->bus, NULL, 0xff))
144         return "Invalid bus number";
145       i++;
146     }
147
148   char *fdev = fields[i];
149   if (field_defined(fdev))
150     {
151       char *sfields[2];
152       if (split_to_fields(fdev, NULL, '.', sfields, 2))
153         return "Invalid slot/function number";
154
155       if (!parse_hex_field(sfields[0], &f->slot, NULL, 0x1f))
156         return "Invalid slot number";
157
158       if (!parse_hex_field(sfields[1], &f->func, NULL, 7))
159         return "Invalid function number";
160     }
161
162   return NULL;
163 }
164
165 /* ID filter syntax: [vendor]:[device][:class[:progif]] */
166
167 char *
168 pci_filter_parse_id_v38(struct pci_filter *f, char *str)
169 {
170   char buf[BUF_SIZE];
171   char *fields[4];
172   char *err;
173
174   if (err = split_to_fields(str, buf, ':', fields, 4))
175     return err;
176
177   if (!fields[1])
178     return "At least two fields must be given";
179
180   if (!parse_hex_field(fields[0], &f->vendor, NULL, 0xffff))
181     return "Invalid vendor ID";
182
183   if (!parse_hex_field(fields[1], &f->device, NULL, 0xffff))
184     return "Invalid device ID";
185
186   if (!parse_hex_field(fields[2], &f->device_class, &f->device_class_mask, 0xffff))
187     return "Invalid class code";
188
189   if (!parse_hex_field(fields[3], &f->prog_if, NULL, 0xff))
190     return "Invalid programming interface code";
191
192   return NULL;
193 }
194
195 int
196 pci_filter_match_v38(struct pci_filter *f, struct pci_dev *d)
197 {
198   if ((f->domain >= 0 && f->domain != d->domain) ||
199       (f->bus >= 0 && f->bus != d->bus) ||
200       (f->slot >= 0 && f->slot != d->dev) ||
201       (f->func >= 0 && f->func != d->func))
202     return 0;
203   if (f->device >= 0 || f->vendor >= 0)
204     {
205       pci_fill_info_v38(d, PCI_FILL_IDENT);
206       if ((f->device >= 0 && f->device != d->device_id) ||
207           (f->vendor >= 0 && f->vendor != d->vendor_id))
208         return 0;
209     }
210   if (f->device_class >= 0)
211     {
212       pci_fill_info_v38(d, PCI_FILL_CLASS);
213       if ((f->device_class ^ d->device_class) & f->device_class_mask)
214         return 0;
215     }
216   if (f->prog_if >= 0)
217     {
218       pci_fill_info_v38(d, PCI_FILL_CLASS_EXT);
219       if (f->prog_if != d->prog_if)
220         return 0;
221     }
222   return 1;
223 }
224
225 /*
226  * Before pciutils v3.3, struct pci_filter had fewer fields,
227  * so we have to provide compatibility wrappers.
228  */
229
230 struct pci_filter_v30 {
231   int domain, bus, slot, func;                  /* -1 = ANY */
232   int vendor, device;
233 };
234
235 void pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f) VERSIONED_ABI;
236 char *pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
237 char *pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
238 int pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d) VERSIONED_ABI;
239
240 static void
241 pci_filter_import_v30(struct pci_filter_v30 *old, struct pci_filter *new)
242 {
243   new->domain = old->domain;
244   new->bus = old->bus;
245   new->slot = old->slot;
246   new->func = old->func;
247   new->vendor = old->vendor;
248   new->device = old->device;
249   new->device_class = -1;
250   new->device_class_mask = ~0U;
251   new->prog_if = -1;
252 }
253
254 static void
255 pci_filter_export_v30(struct pci_filter *new, struct pci_filter_v30 *old)
256 {
257   old->domain = new->domain;
258   old->bus = new->bus;
259   old->slot = new->slot;
260   old->func = new->func;
261   old->vendor = new->vendor;
262   old->device = new->device;
263 }
264
265 void
266 pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f)
267 {
268   struct pci_filter new;
269   pci_filter_init_v38(a, &new);
270   pci_filter_export_v30(&new, f);
271 }
272
273 char *
274 pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str)
275 {
276   struct pci_filter new;
277   char *err;
278   pci_filter_import_v30(f, &new);
279   if (err = pci_filter_parse_slot_v38(&new, str))
280     return err;
281   pci_filter_export_v30(&new, f);
282   return NULL;
283 }
284
285 char *
286 pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str)
287 {
288   struct pci_filter new;
289   char *err;
290   pci_filter_import_v30(f, &new);
291   if (err = pci_filter_parse_id_v38(&new, str))
292     return err;
293   if (new.device_class >= 0 || new.prog_if >= 0)
294     return "Filtering by class or programming interface not supported in this program";
295   pci_filter_export_v30(&new, f);
296   return NULL;
297 }
298
299 int
300 pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d)
301 {
302   struct pci_filter new;
303   pci_filter_import_v30(f, &new);
304   return pci_filter_match_v38(&new, d);
305 }
306
307 // Version 3.3 is the same as version 3.8, only device_class_mask and prog_if were not implemented
308 // (their positions in struct pci_filter were declared as RFU).
309
310 STATIC_ALIAS(void pci_filter_init(struct pci_access *a, struct pci_filter *f), pci_filter_init_v38(a, f));
311 DEFINE_ALIAS(void pci_filter_init_v33(struct pci_access *a, struct pci_filter *f), pci_filter_init_v38);
312 SYMBOL_VERSION(pci_filter_init_v30, pci_filter_init@LIBPCI_3.0);
313 SYMBOL_VERSION(pci_filter_init_v33, pci_filter_init@LIBPCI_3.3);
314 SYMBOL_VERSION(pci_filter_init_v38, pci_filter_init@@LIBPCI_3.8);
315
316 STATIC_ALIAS(char *pci_filter_parse_slot(struct pci_filter *f, char *str), pci_filter_parse_slot_v38(f, str));
317 DEFINE_ALIAS(char *pci_filter_parse_slot_v33(struct pci_filter *f, char *str), pci_filter_parse_slot_v38);
318 SYMBOL_VERSION(pci_filter_parse_slot_v30, pci_filter_parse_slot@LIBPCI_3.0);
319 SYMBOL_VERSION(pci_filter_parse_slot_v33, pci_filter_parse_slot@LIBPCI_3.3);
320 SYMBOL_VERSION(pci_filter_parse_slot_v38, pci_filter_parse_slot@@LIBPCI_3.8);
321
322 STATIC_ALIAS(char *pci_filter_parse_id(struct pci_filter *f, char *str), pci_filter_parse_id_v38(f, str));
323 DEFINE_ALIAS(char *pci_filter_parse_id_v33(struct pci_filter *f, char *str), pci_filter_parse_id_v38);
324 SYMBOL_VERSION(pci_filter_parse_id_v30, pci_filter_parse_id@LIBPCI_3.0);
325 SYMBOL_VERSION(pci_filter_parse_id_v33, pci_filter_parse_id@LIBPCI_3.3);
326 SYMBOL_VERSION(pci_filter_parse_id_v38, pci_filter_parse_id@@LIBPCI_3.8);
327
328 STATIC_ALIAS(int pci_filter_match(struct pci_filter *f, struct pci_dev *d), pci_filter_match_v38(f, d));
329 DEFINE_ALIAS(int pci_filter_match_v33(struct pci_filter *f, struct pci_dev *d), pci_filter_match_v38);
330 SYMBOL_VERSION(pci_filter_match_v30, pci_filter_match@LIBPCI_3.0);
331 SYMBOL_VERSION(pci_filter_match_v33, pci_filter_match@LIBPCI_3.3);
332 SYMBOL_VERSION(pci_filter_match_v38, pci_filter_match@@LIBPCI_3.8);