]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_ematch.c
Merge branch 'iproute2-master' into iproute2-next
[mirror_iproute2.git] / tc / m_ematch.c
1 /*
2 * m_ematch.c Extended Matches
3 *
4 * This program is free software; you can distribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Thomas Graf <tgraf@suug.ch>
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <string.h>
20 #include <dlfcn.h>
21 #include <stdarg.h>
22 #include <errno.h>
23
24 #include "utils.h"
25 #include "tc_util.h"
26 #include "m_ematch.h"
27
28 #define EMATCH_MAP "/etc/iproute2/ematch_map"
29
30 static struct ematch_util *ematch_list;
31
32 /* export to bison parser */
33 int ematch_argc;
34 char **ematch_argv;
35 char *ematch_err;
36 struct ematch *ematch_root;
37
38 static int begin_argc;
39 static char **begin_argv;
40
41 static inline void map_warning(int num, char *kind)
42 {
43 fprintf(stderr,
44 "Error: Unable to find ematch \"%s\" in %s\n" \
45 "Please assign a unique ID to the ematch kind the suggested " \
46 "entry is:\n" \
47 "\t%d\t%s\n",
48 kind, EMATCH_MAP, num, kind);
49 }
50
51 static int lookup_map(__u16 num, char *dst, int len, const char *file)
52 {
53 int err = -EINVAL;
54 char buf[512];
55 FILE *fd = fopen(file, "r");
56
57 if (fd == NULL)
58 return -errno;
59
60 while (fgets(buf, sizeof(buf), fd)) {
61 char namebuf[512], *p = buf;
62 int id;
63
64 while (*p == ' ' || *p == '\t')
65 p++;
66 if (*p == '#' || *p == '\n' || *p == 0)
67 continue;
68
69 if (sscanf(p, "%d %s", &id, namebuf) != 2) {
70 fprintf(stderr, "ematch map %s corrupted at %s\n",
71 file, p);
72 goto out;
73 }
74
75 if (id == num) {
76 if (dst)
77 strncpy(dst, namebuf, len - 1);
78 err = 0;
79 goto out;
80 }
81 }
82
83 err = -ENOENT;
84 out:
85 fclose(fd);
86 return err;
87 }
88
89 static int lookup_map_id(char *kind, int *dst, const char *file)
90 {
91 int err = -EINVAL;
92 char buf[512];
93 FILE *fd = fopen(file, "r");
94
95 if (fd == NULL)
96 return -errno;
97
98 while (fgets(buf, sizeof(buf), fd)) {
99 char namebuf[512], *p = buf;
100 int id;
101
102 while (*p == ' ' || *p == '\t')
103 p++;
104 if (*p == '#' || *p == '\n' || *p == 0)
105 continue;
106
107 if (sscanf(p, "%d %s", &id, namebuf) != 2) {
108 fprintf(stderr, "ematch map %s corrupted at %s\n",
109 file, p);
110 goto out;
111 }
112
113 if (!strcasecmp(namebuf, kind)) {
114 if (dst)
115 *dst = id;
116 err = 0;
117 goto out;
118 }
119 }
120
121 err = -ENOENT;
122 *dst = 0;
123 out:
124 fclose(fd);
125 return err;
126 }
127
128 static struct ematch_util *get_ematch_kind(char *kind)
129 {
130 static void *body;
131 void *dlh;
132 char buf[256];
133 struct ematch_util *e;
134
135 for (e = ematch_list; e; e = e->next) {
136 if (strcmp(e->kind, kind) == 0)
137 return e;
138 }
139
140 snprintf(buf, sizeof(buf), "em_%s.so", kind);
141 dlh = dlopen(buf, RTLD_LAZY);
142 if (dlh == NULL) {
143 dlh = body;
144 if (dlh == NULL) {
145 dlh = body = dlopen(NULL, RTLD_LAZY);
146 if (dlh == NULL)
147 return NULL;
148 }
149 }
150
151 snprintf(buf, sizeof(buf), "%s_ematch_util", kind);
152 e = dlsym(dlh, buf);
153 if (e == NULL)
154 return NULL;
155
156 e->next = ematch_list;
157 ematch_list = e;
158
159 return e;
160 }
161
162 static struct ematch_util *get_ematch_kind_num(__u16 kind)
163 {
164 char name[32];
165
166 if (lookup_map(kind, name, sizeof(name), EMATCH_MAP) < 0)
167 return NULL;
168
169 return get_ematch_kind(name);
170 }
171
172 static int parse_tree(struct nlmsghdr *n, struct ematch *tree)
173 {
174 int index = 1;
175 struct ematch *t;
176
177 for (t = tree; t; t = t->next) {
178 struct rtattr *tail;
179 struct tcf_ematch_hdr hdr = { .flags = t->relation };
180
181 if (t->inverted)
182 hdr.flags |= TCF_EM_INVERT;
183
184 tail = addattr_nest(n, MAX_MSG, index++);
185
186 if (t->child) {
187 __u32 r = t->child_ref;
188
189 addraw_l(n, MAX_MSG, &hdr, sizeof(hdr));
190 addraw_l(n, MAX_MSG, &r, sizeof(r));
191 } else {
192 int num = 0, err;
193 char buf[64];
194 struct ematch_util *e;
195
196 if (t->args == NULL)
197 return -1;
198
199 strncpy(buf, (char *) t->args->data, sizeof(buf)-1);
200 e = get_ematch_kind(buf);
201 if (e == NULL) {
202 fprintf(stderr, "Unknown ematch \"%s\"\n",
203 buf);
204 return -1;
205 }
206
207 err = lookup_map_id(buf, &num, EMATCH_MAP);
208 if (err < 0) {
209 if (err == -ENOENT)
210 map_warning(e->kind_num, buf);
211 return err;
212 }
213
214 hdr.kind = num;
215 if (e->parse_eopt(n, &hdr, t->args->next) < 0)
216 return -1;
217 }
218
219 addattr_nest_end(n, tail);
220 }
221
222 return 0;
223 }
224
225 static int flatten_tree(struct ematch *head, struct ematch *tree)
226 {
227 int i, count = 0;
228 struct ematch *t;
229
230 for (;;) {
231 count++;
232
233 if (tree->child) {
234 for (t = head; t->next; t = t->next);
235 t->next = tree->child;
236 count += flatten_tree(head, tree->child);
237 }
238
239 if (tree->relation == 0)
240 break;
241
242 tree = tree->next;
243 }
244
245 for (i = 0, t = head; t; t = t->next, i++)
246 t->index = i;
247
248 for (t = head; t; t = t->next)
249 if (t->child)
250 t->child_ref = t->child->index;
251
252 return count;
253 }
254
255 int em_parse_error(int err, struct bstr *args, struct bstr *carg,
256 struct ematch_util *e, char *fmt, ...)
257 {
258 va_list a;
259
260 va_start(a, fmt);
261 vfprintf(stderr, fmt, a);
262 va_end(a);
263
264 if (ematch_err)
265 fprintf(stderr, ": %s\n... ", ematch_err);
266 else
267 fprintf(stderr, "\n... ");
268
269 while (ematch_argc < begin_argc) {
270 if (ematch_argc == (begin_argc - 1))
271 fprintf(stderr, ">>%s<< ", *begin_argv);
272 else
273 fprintf(stderr, "%s ", *begin_argv);
274 begin_argv++;
275 begin_argc--;
276 }
277
278 fprintf(stderr, "...\n");
279
280 if (args) {
281 fprintf(stderr, "... %s(", e->kind);
282 while (args) {
283 fprintf(stderr, "%s", args == carg ? ">>" : "");
284 bstr_print(stderr, args, 1);
285 fprintf(stderr, "%s%s", args == carg ? "<<" : "",
286 args->next ? " " : "");
287 args = args->next;
288 }
289 fprintf(stderr, ")...\n");
290
291 }
292
293 if (e == NULL) {
294 fprintf(stderr,
295 "Usage: EXPR\n" \
296 "where: EXPR := TERM [ { and | or } EXPR ]\n" \
297 " TERM := [ not ] { MATCH | '(' EXPR ')' }\n" \
298 " MATCH := module '(' ARGS ')'\n" \
299 " ARGS := ARG1 ARG2 ...\n" \
300 "\n" \
301 "Example: a(x y) and not (b(x) or c(x y z))\n");
302 } else
303 e->print_usage(stderr);
304
305 return -err;
306 }
307
308 static inline void free_ematch_err(void)
309 {
310 if (ematch_err) {
311 free(ematch_err);
312 ematch_err = NULL;
313 }
314 }
315
316 extern int ematch_parse(void);
317
318 int parse_ematch(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
319 {
320 begin_argc = ematch_argc = *argc_p;
321 begin_argv = ematch_argv = *argv_p;
322
323 if (ematch_parse()) {
324 int err = em_parse_error(EINVAL, NULL, NULL, NULL,
325 "Parse error");
326 free_ematch_err();
327 return err;
328 }
329
330 free_ematch_err();
331
332 /* undo look ahead by parser */
333 ematch_argc++;
334 ematch_argv--;
335
336 if (ematch_root) {
337 struct rtattr *tail, *tail_list;
338
339 struct tcf_ematch_tree_hdr hdr = {
340 .nmatches = flatten_tree(ematch_root, ematch_root),
341 .progid = TCF_EM_PROG_TC
342 };
343
344 tail = addattr_nest(n, MAX_MSG, tca_id);
345 addattr_l(n, MAX_MSG, TCA_EMATCH_TREE_HDR, &hdr, sizeof(hdr));
346
347 tail_list = addattr_nest(n, MAX_MSG, TCA_EMATCH_TREE_LIST);
348
349 if (parse_tree(n, ematch_root) < 0)
350 return -1;
351
352 addattr_nest_end(n, tail_list);
353 addattr_nest_end(n, tail);
354 }
355
356 *argc_p = ematch_argc;
357 *argv_p = ematch_argv;
358
359 return 0;
360 }
361
362 static int print_ematch_seq(FILE *fd, struct rtattr **tb, int start,
363 int prefix)
364 {
365 int n, i = start;
366 struct tcf_ematch_hdr *hdr;
367 int dlen;
368 void *data;
369
370 for (;;) {
371 if (tb[i] == NULL)
372 return -1;
373
374 dlen = RTA_PAYLOAD(tb[i]) - sizeof(*hdr);
375 data = (void *) RTA_DATA(tb[i]) + sizeof(*hdr);
376
377 if (dlen < 0)
378 return -1;
379
380 hdr = RTA_DATA(tb[i]);
381
382 if (hdr->flags & TCF_EM_INVERT)
383 fprintf(fd, "NOT ");
384
385 if (hdr->kind == 0) {
386 __u32 ref;
387
388 if (dlen < sizeof(__u32))
389 return -1;
390
391 ref = *(__u32 *) data;
392 fprintf(fd, "(\n");
393 for (n = 0; n <= prefix; n++)
394 fprintf(fd, " ");
395 if (print_ematch_seq(fd, tb, ref + 1, prefix + 1) < 0)
396 return -1;
397 for (n = 0; n < prefix; n++)
398 fprintf(fd, " ");
399 fprintf(fd, ") ");
400
401 } else {
402 struct ematch_util *e;
403
404 e = get_ematch_kind_num(hdr->kind);
405 if (e == NULL)
406 fprintf(fd, "[unknown ematch %d]\n",
407 hdr->kind);
408 else {
409 fprintf(fd, "%s(", e->kind);
410 if (e->print_eopt(fd, hdr, data, dlen) < 0)
411 return -1;
412 fprintf(fd, ")\n");
413 }
414 if (hdr->flags & TCF_EM_REL_MASK)
415 for (n = 0; n < prefix; n++)
416 fprintf(fd, " ");
417 }
418
419 switch (hdr->flags & TCF_EM_REL_MASK) {
420 case TCF_EM_REL_AND:
421 fprintf(fd, "AND ");
422 break;
423
424 case TCF_EM_REL_OR:
425 fprintf(fd, "OR ");
426 break;
427
428 default:
429 return 0;
430 }
431
432 i++;
433 }
434
435 return 0;
436 }
437
438 static int print_ematch_list(FILE *fd, struct tcf_ematch_tree_hdr *hdr,
439 struct rtattr *rta)
440 {
441 int err = -1;
442 struct rtattr **tb;
443
444 tb = malloc((hdr->nmatches + 1) * sizeof(struct rtattr *));
445 if (tb == NULL)
446 return -1;
447
448 if (hdr->nmatches > 0) {
449 if (parse_rtattr_nested(tb, hdr->nmatches, rta) < 0)
450 goto errout;
451
452 fprintf(fd, "\n ");
453 if (print_ematch_seq(fd, tb, 1, 1) < 0)
454 goto errout;
455 }
456
457 err = 0;
458 errout:
459 free(tb);
460 return err;
461 }
462
463 int print_ematch(FILE *fd, const struct rtattr *rta)
464 {
465 struct rtattr *tb[TCA_EMATCH_TREE_MAX+1];
466 struct tcf_ematch_tree_hdr *hdr;
467
468 if (parse_rtattr_nested(tb, TCA_EMATCH_TREE_MAX, rta) < 0)
469 return -1;
470
471 if (tb[TCA_EMATCH_TREE_HDR] == NULL) {
472 fprintf(stderr, "Missing ematch tree header\n");
473 return -1;
474 }
475
476 if (tb[TCA_EMATCH_TREE_LIST] == NULL) {
477 fprintf(stderr, "Missing ematch tree list\n");
478 return -1;
479 }
480
481 if (RTA_PAYLOAD(tb[TCA_EMATCH_TREE_HDR]) < sizeof(*hdr)) {
482 fprintf(stderr, "Ematch tree header size mismatch\n");
483 return -1;
484 }
485
486 hdr = RTA_DATA(tb[TCA_EMATCH_TREE_HDR]);
487
488 return print_ematch_list(fd, hdr, tb[TCA_EMATCH_TREE_LIST]);
489 }
490
491 struct bstr *bstr_alloc(const char *text)
492 {
493 struct bstr *b = calloc(1, sizeof(*b));
494
495 if (b == NULL)
496 return NULL;
497
498 b->data = strdup(text);
499 if (b->data == NULL) {
500 free(b);
501 return NULL;
502 }
503
504 b->len = strlen(text);
505
506 return b;
507 }
508
509 unsigned long bstrtoul(const struct bstr *b)
510 {
511 char *inv = NULL;
512 unsigned long l;
513 char buf[b->len+1];
514
515 memcpy(buf, b->data, b->len);
516 buf[b->len] = '\0';
517
518 l = strtoul(buf, &inv, 0);
519 if (l == ULONG_MAX || inv == buf)
520 return ULONG_MAX;
521
522 return l;
523 }
524
525 void bstr_print(FILE *fd, const struct bstr *b, int ascii)
526 {
527 int i;
528 char *s = b->data;
529
530 if (ascii)
531 for (i = 0; i < b->len; i++)
532 fprintf(fd, "%c", isprint(s[i]) ? s[i] : '.');
533 else {
534 for (i = 0; i < b->len; i++)
535 fprintf(fd, "%02x", s[i]);
536 fprintf(fd, "\"");
537 for (i = 0; i < b->len; i++)
538 fprintf(fd, "%c", isprint(s[i]) ? s[i] : '.');
539 fprintf(fd, "\"");
540 }
541 }
542
543 void print_ematch_tree(const struct ematch *tree)
544 {
545 const struct ematch *t;
546
547 for (t = tree; t; t = t->next) {
548 if (t->inverted)
549 printf("NOT ");
550
551 if (t->child) {
552 printf("(");
553 print_ematch_tree(t->child);
554 printf(")");
555 } else {
556 struct bstr *b;
557
558 for (b = t->args; b; b = b->next)
559 printf("%s%s", b->data, b->next ? " " : "");
560 }
561
562 if (t->relation == TCF_EM_REL_AND)
563 printf(" AND ");
564 else if (t->relation == TCF_EM_REL_OR)
565 printf(" OR ");
566 }
567 }