summaryrefslogtreecommitdiffstats
path: root/lib/filter.c
blob: 4221f570bd65f2b1b37d462a83ed73ea2af9a6aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 *	The PCI Library -- Device Filtering
 *
 *	Copyright (c) 1998--2022 Martin Mares <mj@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL v2+.
 *
 *	SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <stdlib.h>
#include <string.h>

#include "internal.h"

void pci_filter_init_v38(struct pci_access *a UNUSED, struct pci_filter *f) VERSIONED_ABI;
char *pci_filter_parse_slot_v38(struct pci_filter *f, char *str) VERSIONED_ABI;
char *pci_filter_parse_id_v38(struct pci_filter *f, char *str) VERSIONED_ABI;
int pci_filter_match_v38(struct pci_filter *f, struct pci_dev *d) VERSIONED_ABI;

void
pci_filter_init_v38(struct pci_access *a UNUSED, struct pci_filter *f)
{
  memset((byte *) f, 0, sizeof(*f));
  f->domain = f->bus = f->slot = f->func = -1;
  f->vendor = f->device = -1;
  f->device_class = -1;
  f->device_class_mask = ~0U;
  f->prog_if = -1;
}

#define BUF_SIZE 64

static char *
split_to_fields(char *str, char *buffer, int sep, char **fields, int num_fields)
{
  if (buffer)
    {
      if (strlen(str) >= BUF_SIZE)
	return "Expression too long";
      strcpy(buffer, str);
      str = buffer;
    }

  int i = 0;

  for (;;)
    {
      if (i >= num_fields)
	return "Too many fields";
      fields[i++] = str;
      while (*str && *str != sep)
	str++;
      if (!*str)
	break;
      *str++ = 0;
    }

  while (i < num_fields)
    fields[i++] = NULL;

  return NULL;
}

static int
field_defined(char *field)
{
  return field && field[0] && strcmp(field, "*");
}

static int
parse_hex_field(char *str, int *outp, unsigned int *maskp, unsigned int max)
{
  unsigned int out = 0;
  unsigned int mask = ~0U;
  unsigned int bound = 0;

  if (!field_defined(str))
    return 1;	// and keep the defaults

  // Historically, filters allowed writing hexadecimal numbers with leading "0x".
  // This was never intentional nor documented, but some people relied on it.
  if (!maskp && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
    str += 2;

  while (*str)
    {
      int c = *str++;
      int d;

      if ((c == 'x' || c == 'X') && maskp)
	{
	  out = out << 4;
	  bound = (bound << 4) | 1;
	  mask = mask << 4;
	}
      else
	{
	  if (c >= '0' && c <= '9')
	    d = c - '0';
	  else if (c >= 'A' && c <= 'F')
	    d = c - 'A' + 10;
	  else if (c >= 'a' && c <= 'f')
	    d = c - 'a' + 10;
	  else
	    return 0;

	  out = (out << 4) | d;
	  bound = (bound << 4) | d;
	  mask = (mask << 4) | 0xf;
	}

      if (bound > max)
	return 0;
    }

  *outp = out;
  if (maskp)
    *maskp = mask;
  return 1;
}

/* Slot filter syntax: [[[domain]:][bus]:][slot][.[func]] */

char *
pci_filter_parse_slot_v38(struct pci_filter *f, char *str)
{
  char buf[BUF_SIZE];
  char *fields[3];
  char *err;

  if (err = split_to_fields(str, buf, ':', fields, 3))
    return err;

  int i = 0;
  if (fields[2])
    {
      if (!parse_hex_field(fields[0], &f->domain, NULL, 0x7fffffff))
	return "Invalid domain number";
      i++;
    }

  if (fields[i+1])
    {
      if (!parse_hex_field(fields[i], &f->bus, NULL, 0xff))
	return "Invalid bus number";
      i++;
    }

  char *fdev = fields[i];
  if (field_defined(fdev))
    {
      char *sfields[2];
      if (split_to_fields(fdev, NULL, '.', sfields, 2))
	return "Invalid slot/function number";

      if (!parse_hex_field(sfields[0], &f->slot, NULL, 0x1f))
	return "Invalid slot number";

      if (!parse_hex_field(sfields[1], &f->func, NULL, 7))
	return "Invalid function number";
    }

  return NULL;
}

/* ID filter syntax: [vendor]:[device][:class[:progif]] */

char *
pci_filter_parse_id_v38(struct pci_filter *f, char *str)
{
  char buf[BUF_SIZE];
  char *fields[4];
  char *err;

  if (err = split_to_fields(str, buf, ':', fields, 4))
    return err;

  if (!fields[1])
    return "At least two fields must be given";

  if (!parse_hex_field(fields[0], &f->vendor, NULL, 0xffff))
    return "Invalid vendor ID";

  if (!parse_hex_field(fields[1], &f->device, NULL, 0xffff))
    return "Invalid device ID";

  if (!parse_hex_field(fields[2], &f->device_class, &f->device_class_mask, 0xffff))
    return "Invalid class code";

  if (!parse_hex_field(fields[3], &f->prog_if, NULL, 0xff))
    return "Invalid programming interface code";

  return NULL;
}

int
pci_filter_match_v38(struct pci_filter *f, struct pci_dev *d)
{
  if ((f->domain >= 0 && f->domain != d->domain) ||
      (f->bus >= 0 && f->bus != d->bus) ||
      (f->slot >= 0 && f->slot != d->dev) ||
      (f->func >= 0 && f->func != d->func))
    return 0;
  if (f->device >= 0 || f->vendor >= 0)
    {
      pci_fill_info_v313(d, PCI_FILL_IDENT);
      if ((f->device >= 0 && f->device != d->device_id) ||
	  (f->vendor >= 0 && f->vendor != d->vendor_id))
	return 0;
    }
  if (f->device_class >= 0)
    {
      pci_fill_info_v313(d, PCI_FILL_CLASS);
      if ((f->device_class ^ d->device_class) & f->device_class_mask)
	return 0;
    }
  if (f->prog_if >= 0)
    {
      pci_fill_info_v313(d, PCI_FILL_CLASS_EXT);
      if (f->prog_if != d->prog_if)
	return 0;
    }
  return 1;
}

/*
 * Before pciutils v3.3, struct pci_filter had fewer fields,
 * so we have to provide compatibility wrappers.
 */

struct pci_filter_v30 {
  int domain, bus, slot, func;			/* -1 = ANY */
  int vendor, device;
};

void pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f) VERSIONED_ABI;
char *pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
char *pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
int pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d) VERSIONED_ABI;

static void
pci_filter_import_v30(struct pci_filter_v30 *old, struct pci_filter *new)
{
  new->domain = old->domain;
  new->bus = old->bus;
  new->slot = old->slot;
  new->func = old->func;
  new->vendor = old->vendor;
  new->device = old->device;
  new->device_class = -1;
  new->device_class_mask = ~0U;
  new->prog_if = -1;
}

static void
pci_filter_export_v30(struct pci_filter *new, struct pci_filter_v30 *old)
{
  old->domain = new->domain;
  old->bus = new->bus;
  old->slot = new->slot;
  old->func = new->func;
  old->vendor = new->vendor;
  old->device = new->device;
}

void
pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f)
{
  struct pci_filter new;
  pci_filter_init_v38(a, &new);
  pci_filter_export_v30(&new, f);
}

char *
pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str)
{
  struct pci_filter new;
  char *err;
  pci_filter_import_v30(f, &new);
  if (err = pci_filter_parse_slot_v38(&new, str))
    return err;
  pci_filter_export_v30(&new, f);
  return NULL;
}

char *
pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str)
{
  struct pci_filter new;
  char *err;
  pci_filter_import_v30(f, &new);
  if (err = pci_filter_parse_id_v38(&new, str))
    return err;
  if (new.device_class >= 0 || new.prog_if >= 0)
    return "Filtering by class or programming interface not supported in this program";
  pci_filter_export_v30(&new, f);
  return NULL;
}

int
pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d)
{
  struct pci_filter new;
  pci_filter_import_v30(f, &new);
  return pci_filter_match_v38(&new, d);
}

// Version 3.3 is the same as version 3.8, only device_class_mask and prog_if were not implemented
// (their positions in struct pci_filter were declared as RFU).

STATIC_ALIAS(void pci_filter_init(struct pci_access *a, struct pci_filter *f), pci_filter_init_v38(a, f));
DEFINE_ALIAS(void pci_filter_init_v33(struct pci_access *a, struct pci_filter *f), pci_filter_init_v38);
SYMBOL_VERSION(pci_filter_init_v30, pci_filter_init@LIBPCI_3.0);
SYMBOL_VERSION(pci_filter_init_v33, pci_filter_init@LIBPCI_3.3);
SYMBOL_VERSION(pci_filter_init_v38, pci_filter_init@@LIBPCI_3.8);

STATIC_ALIAS(char *pci_filter_parse_slot(struct pci_filter *f, char *str), pci_filter_parse_slot_v38(f, str));
DEFINE_ALIAS(char *pci_filter_parse_slot_v33(struct pci_filter *f, char *str), pci_filter_parse_slot_v38);
SYMBOL_VERSION(pci_filter_parse_slot_v30, pci_filter_parse_slot@LIBPCI_3.0);
SYMBOL_VERSION(pci_filter_parse_slot_v33, pci_filter_parse_slot@LIBPCI_3.3);
SYMBOL_VERSION(pci_filter_parse_slot_v38, pci_filter_parse_slot@@LIBPCI_3.8);

STATIC_ALIAS(char *pci_filter_parse_id(struct pci_filter *f, char *str), pci_filter_parse_id_v38(f, str));
DEFINE_ALIAS(char *pci_filter_parse_id_v33(struct pci_filter *f, char *str), pci_filter_parse_id_v38);
SYMBOL_VERSION(pci_filter_parse_id_v30, pci_filter_parse_id@LIBPCI_3.0);
SYMBOL_VERSION(pci_filter_parse_id_v33, pci_filter_parse_id@LIBPCI_3.3);
SYMBOL_VERSION(pci_filter_parse_id_v38, pci_filter_parse_id@@LIBPCI_3.8);

STATIC_ALIAS(int pci_filter_match(struct pci_filter *f, struct pci_dev *d), pci_filter_match_v38(f, d));
DEFINE_ALIAS(int pci_filter_match_v33(struct pci_filter *f, struct pci_dev *d), pci_filter_match_v38);
SYMBOL_VERSION(pci_filter_match_v30, pci_filter_match@LIBPCI_3.0);
SYMBOL_VERSION(pci_filter_match_v33, pci_filter_match@LIBPCI_3.3);
SYMBOL_VERSION(pci_filter_match_v38, pci_filter_match@@LIBPCI_3.8);