1 /*
2 * m_pedit.c generic packet editor actions module
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: J Hadi Salim (hadi@cyberus.ca)
10 *
11 * TODO:
12 * 1) Big endian broken in some spots
13 * 2) A lot of this stuff was added on the fly; get a big double-double
14 * and clean it up at some point.
15 *
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <syslog.h>
22 #include <fcntl.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <string.h>
27 #include <dlfcn.h>
28 #include "utils.h"
29 #include "tc_util.h"
30 #include "m_pedit.h"
31 #include "rt_names.h"
32
33 static struct m_pedit_util *pedit_list;
34 static int pedit_debug;
35
explain(void)36 static void explain(void)
37 {
38 fprintf(stderr, "Usage: ... pedit munge [ex] <MUNGE> [CONTROL]\n");
39 fprintf(stderr,
40 "Where: MUNGE := <RAW>|<LAYERED>\n"
41 "\t<RAW>:= <OFFSETC>[ATC]<CMD>\n \t\tOFFSETC:= offset <offval> <u8|u16|u32>\n"
42 "\t\tATC:= at <atval> offmask <maskval> shift <shiftval>\n"
43 "\t\tNOTE: offval is byte offset, must be multiple of 4\n"
44 "\t\tNOTE: maskval is a 32 bit hex number\n \t\tNOTE: shiftval is a shift value\n"
45 "\t\tCMD:= clear | invert | set <setval>| add <addval> | retain\n"
46 "\t<LAYERED>:= ip <ipdata> | ip6 <ip6data>\n"
47 " \t\t| udp <udpdata> | tcp <tcpdata> | icmp <icmpdata>\n"
48 "\tCONTROL:= reclassify | pipe | drop | continue | pass |\n"
49 "\t goto chain <CHAIN_INDEX>\n"
50 "\tNOTE: if 'ex' is set, extended functionality will be supported (kernel >= 4.11)\n"
51 "For Example usage look at the examples directory\n");
52
53 }
54
usage(void)55 static void usage(void)
56 {
57 explain();
58 exit(-1);
59 }
60
pedit_parse_nopopt(int * argc_p,char *** argv_p,struct m_pedit_sel * sel,struct m_pedit_key * tkey)61 static int pedit_parse_nopopt(int *argc_p, char ***argv_p,
62 struct m_pedit_sel *sel,
63 struct m_pedit_key *tkey)
64 {
65 int argc = *argc_p;
66 char **argv = *argv_p;
67
68 if (argc) {
69 fprintf(stderr,
70 "Unknown action hence option \"%s\" is unparsable\n",
71 *argv);
72 return -1;
73 }
74
75 return 0;
76
77 }
78
get_pedit_kind(const char * str)79 static struct m_pedit_util *get_pedit_kind(const char *str)
80 {
81 static void *pBODY;
82 void *dlh;
83 char buf[256];
84 struct m_pedit_util *p;
85
86 for (p = pedit_list; p; p = p->next) {
87 if (strcmp(p->id, str) == 0)
88 return p;
89 }
90
91 snprintf(buf, sizeof(buf), "p_%s.so", str);
92 dlh = dlopen(buf, RTLD_LAZY);
93 if (dlh == NULL) {
94 dlh = pBODY;
95 if (dlh == NULL) {
96 dlh = pBODY = dlopen(NULL, RTLD_LAZY);
97 if (dlh == NULL)
98 goto noexist;
99 }
100 }
101
102 snprintf(buf, sizeof(buf), "p_pedit_%s", str);
103 p = dlsym(dlh, buf);
104 if (p == NULL)
105 goto noexist;
106
107 reg:
108 p->next = pedit_list;
109 pedit_list = p;
110 return p;
111
112 noexist:
113 p = calloc(1, sizeof(*p));
114 if (p) {
115 strncpy(p->id, str, sizeof(p->id) - 1);
116 p->parse_peopt = pedit_parse_nopopt;
117 goto reg;
118 }
119 return p;
120 }
121
pack_key(struct m_pedit_sel * _sel,struct m_pedit_key * tkey)122 int pack_key(struct m_pedit_sel *_sel, struct m_pedit_key *tkey)
123 {
124 struct tc_pedit_sel *sel = &_sel->sel;
125 struct m_pedit_key_ex *keys_ex = _sel->keys_ex;
126 int hwm = sel->nkeys;
127
128 if (hwm >= MAX_OFFS)
129 return -1;
130
131 if (tkey->off % 4) {
132 fprintf(stderr, "offsets MUST be in 32 bit boundaries\n");
133 return -1;
134 }
135
136 sel->keys[hwm].val = tkey->val;
137 sel->keys[hwm].mask = tkey->mask;
138 sel->keys[hwm].off = tkey->off;
139 sel->keys[hwm].at = tkey->at;
140 sel->keys[hwm].offmask = tkey->offmask;
141 sel->keys[hwm].shift = tkey->shift;
142
143 if (_sel->extended) {
144 keys_ex[hwm].htype = tkey->htype;
145 keys_ex[hwm].cmd = tkey->cmd;
146 } else {
147 if (tkey->htype != TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK ||
148 tkey->cmd != TCA_PEDIT_KEY_EX_CMD_SET) {
149 fprintf(stderr,
150 "Munge parameters not supported. Use 'pedit ex munge ...'.\n");
151 return -1;
152 }
153 }
154
155 sel->nkeys++;
156 return 0;
157 }
158
pack_key32(__u32 retain,struct m_pedit_sel * sel,struct m_pedit_key * tkey)159 int pack_key32(__u32 retain, struct m_pedit_sel *sel,
160 struct m_pedit_key *tkey)
161 {
162 if (tkey->off > (tkey->off & ~3)) {
163 fprintf(stderr,
164 "pack_key32: 32 bit offsets must begin in 32bit boundaries\n");
165 return -1;
166 }
167
168 tkey->val = htonl(tkey->val & retain);
169 tkey->mask = htonl(tkey->mask | ~retain);
170 return pack_key(sel, tkey);
171 }
172
pack_key16(__u32 retain,struct m_pedit_sel * sel,struct m_pedit_key * tkey)173 int pack_key16(__u32 retain, struct m_pedit_sel *sel,
174 struct m_pedit_key *tkey)
175 {
176 int ind, stride;
177 __u32 m[4] = { 0x0000FFFF, 0xFF0000FF, 0xFFFF0000 };
178
179 if (tkey->val > 0xFFFF || tkey->mask > 0xFFFF) {
180 fprintf(stderr, "pack_key16 bad value\n");
181 return -1;
182 }
183
184 ind = tkey->off & 3;
185
186 if (ind == 3) {
187 fprintf(stderr, "pack_key16 bad index value %d\n", ind);
188 return -1;
189 }
190
191 stride = 8 * (2 - ind);
192 tkey->val = htonl((tkey->val & retain) << stride);
193 tkey->mask = htonl(((tkey->mask | ~retain) << stride) | m[ind]);
194
195 tkey->off &= ~3;
196
197 if (pedit_debug)
198 printf("pack_key16: Final val %08x mask %08x\n",
199 tkey->val, tkey->mask);
200 return pack_key(sel, tkey);
201
202 }
203
pack_key8(__u32 retain,struct m_pedit_sel * sel,struct m_pedit_key * tkey)204 int pack_key8(__u32 retain, struct m_pedit_sel *sel, struct m_pedit_key *tkey)
205 {
206 int ind, stride;
207 __u32 m[4] = { 0x00FFFFFF, 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00 };
208
209 if (tkey->val > 0xFF || tkey->mask > 0xFF) {
210 fprintf(stderr, "pack_key8 bad value (val %x mask %x\n",
211 tkey->val, tkey->mask);
212 return -1;
213 }
214
215 ind = tkey->off & 3;
216
217 stride = 8 * (3 - ind);
218 tkey->val = htonl((tkey->val & retain) << stride);
219 tkey->mask = htonl(((tkey->mask | ~retain) << stride) | m[ind]);
220
221 tkey->off &= ~3;
222
223 if (pedit_debug)
224 printf("pack_key8: Final word off %d val %08x mask %08x\n",
225 tkey->off, tkey->val, tkey->mask);
226 return pack_key(sel, tkey);
227 }
228
pack_mac(struct m_pedit_sel * sel,struct m_pedit_key * tkey,__u8 * mac)229 static int pack_mac(struct m_pedit_sel *sel, struct m_pedit_key *tkey,
230 __u8 *mac)
231 {
232 int ret = 0;
233
234 if (!(tkey->off & 0x3)) {
235 tkey->mask = 0;
236 tkey->val = ntohl(*((__u32 *)mac));
237 ret |= pack_key32(~0, sel, tkey);
238
239 tkey->off += 4;
240 tkey->mask = 0;
241 tkey->val = ntohs(*((__u16 *)&mac[4]));
242 ret |= pack_key16(~0, sel, tkey);
243 } else if (!(tkey->off & 0x1)) {
244 tkey->mask = 0;
245 tkey->val = ntohs(*((__u16 *)mac));
246 ret |= pack_key16(~0, sel, tkey);
247
248 tkey->off += 4;
249 tkey->mask = 0;
250 tkey->val = ntohl(*((__u32 *)(mac + 2)));
251 ret |= pack_key32(~0, sel, tkey);
252 } else {
253 fprintf(stderr,
254 "pack_mac: mac offsets must begin in 32bit or 16bit boundaries\n");
255 return -1;
256 }
257
258 return ret;
259 }
260
pack_ipv6(struct m_pedit_sel * sel,struct m_pedit_key * tkey,__u32 * ipv6)261 static int pack_ipv6(struct m_pedit_sel *sel, struct m_pedit_key *tkey,
262 __u32 *ipv6)
263 {
264 int ret = 0;
265 int i;
266
267 if (tkey->off & 0x3) {
268 fprintf(stderr,
269 "pack_ipv6: IPv6 offsets must begin in 32bit boundaries\n");
270 return -1;
271 }
272
273 for (i = 0; i < 4; i++) {
274 tkey->mask = 0;
275 tkey->val = ntohl(ipv6[i]);
276
277 ret = pack_key32(~0, sel, tkey);
278 if (ret)
279 return ret;
280
281 tkey->off += 4;
282 }
283
284 return 0;
285 }
286
parse_val(int * argc_p,char *** argv_p,__u32 * val,int type)287 int parse_val(int *argc_p, char ***argv_p, __u32 *val, int type)
288 {
289 int argc = *argc_p;
290 char **argv = *argv_p;
291
292 if (argc <= 0)
293 return -1;
294
295 if (type == TINT)
296 return get_integer((int *)val, *argv, 0);
297
298 if (type == TU32)
299 return get_u32(val, *argv, 0);
300
301 if (type == TIPV4) {
302 inet_prefix addr;
303
304 if (get_prefix_1(&addr, *argv, AF_INET))
305 return -1;
306
307 *val = addr.data[0];
308 return 0;
309 }
310
311 if (type == TIPV6) {
312 inet_prefix addr;
313
314 if (get_prefix_1(&addr, *argv, AF_INET6))
315 return -1;
316
317 memcpy(val, addr.data, addr.bytelen);
318
319 return 0;
320 }
321
322 if (type == TMAC) {
323 #define MAC_ALEN 6
324 int ret = ll_addr_a2n((char *)val, MAC_ALEN, *argv);
325
326 if (ret == MAC_ALEN)
327 return 0;
328 }
329
330 return -1;
331 }
332
parse_cmd(int * argc_p,char *** argv_p,__u32 len,int type,__u32 retain,struct m_pedit_sel * sel,struct m_pedit_key * tkey)333 int parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain,
334 struct m_pedit_sel *sel, struct m_pedit_key *tkey)
335 {
336 __u32 mask[4] = { 0 };
337 __u32 val[4] = { 0 };
338 __u32 *m = &mask[0];
339 __u32 *v = &val[0];
340 __u32 o = 0xFF;
341 int res = -1;
342 int argc = *argc_p;
343 char **argv = *argv_p;
344
345 if (argc <= 0)
346 return -1;
347
348 if (pedit_debug)
349 printf("parse_cmd argc %d %s offset %d length %d\n",
350 argc, *argv, tkey->off, len);
351
352 if (len == 2)
353 o = 0xFFFF;
354 if (len == 4)
355 o = 0xFFFFFFFF;
356
357 if (matches(*argv, "invert") == 0) {
358 *v = *m = o;
359 } else if (matches(*argv, "set") == 0 ||
360 matches(*argv, "add") == 0) {
361 if (matches(*argv, "add") == 0)
362 tkey->cmd = TCA_PEDIT_KEY_EX_CMD_ADD;
363
364 if (!sel->extended && tkey->cmd) {
365 fprintf(stderr,
366 "Non extended mode. only 'set' command is supported\n");
367 return -1;
368 }
369
370 NEXT_ARG();
371 if (parse_val(&argc, &argv, val, type))
372 return -1;
373 } else if (matches(*argv, "preserve") == 0) {
374 retain = 0;
375 } else {
376 if (matches(*argv, "clear") != 0)
377 return -1;
378 }
379
380 argc--;
381 argv++;
382
383 if (argc && matches(*argv, "retain") == 0) {
384 NEXT_ARG();
385 if (parse_val(&argc, &argv, &retain, TU32))
386 return -1;
387 argc--;
388 argv++;
389 }
390
391 if (len > 4 && retain != ~0) {
392 fprintf(stderr,
393 "retain is not supported for fields longer the 32 bits\n");
394 return -1;
395 }
396
397 if (type == TMAC) {
398 res = pack_mac(sel, tkey, (__u8 *)val);
399 goto done;
400 }
401
402 if (type == TIPV6) {
403 res = pack_ipv6(sel, tkey, val);
404 goto done;
405 }
406
407 tkey->val = *v;
408 tkey->mask = *m;
409
410 if (type == TIPV4)
411 tkey->val = ntohl(tkey->val);
412
413 if (len == 1) {
414 res = pack_key8(retain, sel, tkey);
415 goto done;
416 }
417 if (len == 2) {
418 res = pack_key16(retain, sel, tkey);
419 goto done;
420 }
421 if (len == 4) {
422 res = pack_key32(retain, sel, tkey);
423 goto done;
424 }
425
426 return -1;
427 done:
428 if (pedit_debug)
429 printf("parse_cmd done argc %d %s offset %d length %d\n",
430 argc, *argv, tkey->off, len);
431 *argc_p = argc;
432 *argv_p = argv;
433 return res;
434
435 }
436
parse_offset(int * argc_p,char *** argv_p,struct m_pedit_sel * sel,struct m_pedit_key * tkey)437 int parse_offset(int *argc_p, char ***argv_p, struct m_pedit_sel *sel,
438 struct m_pedit_key *tkey)
439 {
440 int off;
441 __u32 len, retain;
442 int argc = *argc_p;
443 char **argv = *argv_p;
444 int res = -1;
445
446 if (argc <= 0)
447 return -1;
448
449 if (get_integer(&off, *argv, 0))
450 return -1;
451 tkey->off = off;
452
453 argc--;
454 argv++;
455
456 if (argc <= 0)
457 return -1;
458
459 if (matches(*argv, "u32") == 0) {
460 len = 4;
461 retain = 0xFFFFFFFF;
462 goto done;
463 }
464 if (matches(*argv, "u16") == 0) {
465 len = 2;
466 retain = 0xffff;
467 goto done;
468 }
469 if (matches(*argv, "u8") == 0) {
470 len = 1;
471 retain = 0xff;
472 goto done;
473 }
474
475 return -1;
476
477 done:
478
479 NEXT_ARG();
480
481 /* [at <someval> offmask <maskval> shift <shiftval>] */
482 if (matches(*argv, "at") == 0) {
483
484 __u32 atv = 0, offmask = 0x0, shift = 0;
485
486 NEXT_ARG();
487 if (get_u32(&atv, *argv, 0))
488 return -1;
489 tkey->at = atv;
490
491 NEXT_ARG();
492
493 if (get_u32(&offmask, *argv, 16))
494 return -1;
495 tkey->offmask = offmask;
496
497 NEXT_ARG();
498
499 if (get_u32(&shift, *argv, 0))
500 return -1;
501 tkey->shift = shift;
502
503 NEXT_ARG();
504 }
505
506 res = parse_cmd(&argc, &argv, len, TU32, retain, sel, tkey);
507
508 *argc_p = argc;
509 *argv_p = argv;
510 return res;
511 }
512
parse_munge(int * argc_p,char *** argv_p,struct m_pedit_sel * sel)513 static int parse_munge(int *argc_p, char ***argv_p, struct m_pedit_sel *sel)
514 {
515 struct m_pedit_key tkey = {};
516 int argc = *argc_p;
517 char **argv = *argv_p;
518 int res = -1;
519
520 if (argc <= 0)
521 return -1;
522
523 if (matches(*argv, "offset") == 0) {
524 NEXT_ARG();
525 res = parse_offset(&argc, &argv, sel, &tkey);
526 goto done;
527 } else {
528 char k[16];
529 struct m_pedit_util *p = NULL;
530
531 strncpy(k, *argv, sizeof(k) - 1);
532
533 if (argc > 0) {
534 p = get_pedit_kind(k);
535 if (p == NULL)
536 goto bad_val;
537 NEXT_ARG();
538 res = p->parse_peopt(&argc, &argv, sel, &tkey);
539 if (res < 0) {
540 fprintf(stderr, "bad pedit parsing\n");
541 goto bad_val;
542 }
543 goto done;
544 }
545 }
546
547 bad_val:
548 return -1;
549
550 done:
551
552 *argc_p = argc;
553 *argv_p = argv;
554 return res;
555 }
556
pedit_keys_ex_getattr(struct rtattr * attr,struct m_pedit_key_ex * keys_ex,int n)557 static int pedit_keys_ex_getattr(struct rtattr *attr,
558 struct m_pedit_key_ex *keys_ex, int n)
559 {
560 struct rtattr *i;
561 int rem = RTA_PAYLOAD(attr);
562 struct rtattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
563 struct m_pedit_key_ex *k = keys_ex;
564
565 for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
566 if (!n)
567 return -1;
568
569 if (i->rta_type != TCA_PEDIT_KEY_EX)
570 return -1;
571
572 parse_rtattr_nested(tb, TCA_PEDIT_KEY_EX_MAX, i);
573
574 k->htype = rta_getattr_u16(tb[TCA_PEDIT_KEY_EX_HTYPE]);
575 k->cmd = rta_getattr_u16(tb[TCA_PEDIT_KEY_EX_CMD]);
576
577 k++;
578 n--;
579 }
580
581 return !!n;
582 }
583
pedit_keys_ex_addattr(struct m_pedit_sel * sel,struct nlmsghdr * n)584 static int pedit_keys_ex_addattr(struct m_pedit_sel *sel, struct nlmsghdr *n)
585 {
586 struct m_pedit_key_ex *k = sel->keys_ex;
587 struct rtattr *keys_start;
588 int i;
589
590 if (!sel->extended)
591 return 0;
592
593 keys_start = addattr_nest(n, MAX_MSG, TCA_PEDIT_KEYS_EX | NLA_F_NESTED);
594
595 for (i = 0; i < sel->sel.nkeys; i++) {
596 struct rtattr *key_start;
597
598 key_start = addattr_nest(n, MAX_MSG,
599 TCA_PEDIT_KEY_EX | NLA_F_NESTED);
600
601 if (addattr16(n, MAX_MSG, TCA_PEDIT_KEY_EX_HTYPE, k->htype) ||
602 addattr16(n, MAX_MSG, TCA_PEDIT_KEY_EX_CMD, k->cmd)) {
603 return -1;
604 }
605
606 addattr_nest_end(n, key_start);
607
608 k++;
609 }
610
611 addattr_nest_end(n, keys_start);
612
613 return 0;
614 }
615
parse_pedit(struct action_util * a,int * argc_p,char *** argv_p,int tca_id,struct nlmsghdr * n)616 int parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
617 struct nlmsghdr *n)
618 {
619 struct m_pedit_sel sel = {};
620
621 int argc = *argc_p;
622 char **argv = *argv_p;
623 int ok = 0, iok = 0;
624 struct rtattr *tail;
625
626 while (argc > 0) {
627 if (pedit_debug > 1)
628 fprintf(stderr, "while pedit (%d:%s)\n", argc, *argv);
629 if (matches(*argv, "pedit") == 0) {
630 NEXT_ARG();
631 ok++;
632
633 if (matches(*argv, "ex") == 0) {
634 if (ok > 1) {
635 fprintf(stderr,
636 "'ex' must be before first 'munge'\n");
637 explain();
638 return -1;
639 }
640 sel.extended = true;
641 NEXT_ARG();
642 }
643
644 continue;
645 } else if (matches(*argv, "help") == 0) {
646 usage();
647 } else if (matches(*argv, "munge") == 0) {
648 if (!ok) {
649 fprintf(stderr, "Bad pedit construct (%s)\n",
650 *argv);
651 explain();
652 return -1;
653 }
654 NEXT_ARG();
655
656 if (parse_munge(&argc, &argv, &sel)) {
657 fprintf(stderr, "Bad pedit construct (%s)\n",
658 *argv);
659 explain();
660 return -1;
661 }
662 ok++;
663 } else {
664 break;
665 }
666
667 }
668
669 if (!ok) {
670 explain();
671 return -1;
672 }
673
674 parse_action_control_dflt(&argc, &argv, &sel.sel.action, false, TC_ACT_OK);
675
676 if (argc) {
677 if (matches(*argv, "index") == 0) {
678 NEXT_ARG();
679 if (get_u32(&sel.sel.index, *argv, 10)) {
680 fprintf(stderr, "Pedit: Illegal \"index\"\n");
681 return -1;
682 }
683 argc--;
684 argv++;
685 iok++;
686 }
687 }
688
689 tail = NLMSG_TAIL(n);
690 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
691 if (!sel.extended) {
692 addattr_l(n, MAX_MSG, TCA_PEDIT_PARMS, &sel,
693 sizeof(sel.sel) +
694 sel.sel.nkeys * sizeof(struct tc_pedit_key));
695 } else {
696 addattr_l(n, MAX_MSG, TCA_PEDIT_PARMS_EX, &sel,
697 sizeof(sel.sel) +
698 sel.sel.nkeys * sizeof(struct tc_pedit_key));
699
700 pedit_keys_ex_addattr(&sel, n);
701 }
702
703 tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail;
704
705 *argc_p = argc;
706 *argv_p = argv;
707 return 0;
708 }
709
710 const char *pedit_htype_str[] = {
711 [TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK] = "",
712 [TCA_PEDIT_KEY_EX_HDR_TYPE_ETH] = "eth",
713 [TCA_PEDIT_KEY_EX_HDR_TYPE_IP4] = "ipv4",
714 [TCA_PEDIT_KEY_EX_HDR_TYPE_IP6] = "ipv6",
715 [TCA_PEDIT_KEY_EX_HDR_TYPE_TCP] = "tcp",
716 [TCA_PEDIT_KEY_EX_HDR_TYPE_UDP] = "udp",
717 };
718
print_pedit_location(FILE * f,enum pedit_header_type htype,__u32 off)719 static void print_pedit_location(FILE *f,
720 enum pedit_header_type htype, __u32 off)
721 {
722 if (htype == TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK) {
723 fprintf(f, "%d", (unsigned int)off);
724 return;
725 }
726
727 if (htype < ARRAY_SIZE(pedit_htype_str))
728 fprintf(f, "%s", pedit_htype_str[htype]);
729 else
730 fprintf(f, "unknown(%d)", htype);
731
732 fprintf(f, "%c%d", (int)off >= 0 ? '+' : '-', abs((int)off));
733 }
734
print_pedit(struct action_util * au,FILE * f,struct rtattr * arg)735 int print_pedit(struct action_util *au, FILE *f, struct rtattr *arg)
736 {
737 struct tc_pedit_sel *sel;
738 struct rtattr *tb[TCA_PEDIT_MAX + 1];
739 struct m_pedit_key_ex *keys_ex = NULL;
740
741 if (arg == NULL)
742 return -1;
743
744 parse_rtattr_nested(tb, TCA_PEDIT_MAX, arg);
745
746 if (!tb[TCA_PEDIT_PARMS] && !tb[TCA_PEDIT_PARMS_EX]) {
747 fprintf(f, "[NULL pedit parameters]");
748 return -1;
749 }
750
751 if (tb[TCA_PEDIT_PARMS]) {
752 sel = RTA_DATA(tb[TCA_PEDIT_PARMS]);
753 } else {
754 int err;
755
756 sel = RTA_DATA(tb[TCA_PEDIT_PARMS_EX]);
757
758 if (!tb[TCA_PEDIT_KEYS_EX]) {
759 fprintf(f, "Netlink error\n");
760 return -1;
761 }
762
763 keys_ex = calloc(sel->nkeys, sizeof(*keys_ex));
764 if (!keys_ex) {
765 fprintf(f, "Out of memory\n");
766 return -1;
767 }
768
769 err = pedit_keys_ex_getattr(tb[TCA_PEDIT_KEYS_EX], keys_ex,
770 sel->nkeys);
771 if (err) {
772 fprintf(f, "Netlink error\n");
773
774 free(keys_ex);
775 return -1;
776 }
777 }
778
779 fprintf(f, " pedit ");
780 print_action_control(f, "action ", sel->action, " ");
781 fprintf(f,"keys %d\n ", sel->nkeys);
782 fprintf(f, "\t index %u ref %d bind %d", sel->index, sel->refcnt,
783 sel->bindcnt);
784
785 if (show_stats) {
786 if (tb[TCA_PEDIT_TM]) {
787 struct tcf_t *tm = RTA_DATA(tb[TCA_PEDIT_TM]);
788
789 print_tm(f, tm);
790 }
791 }
792 if (sel->nkeys) {
793 int i;
794 struct tc_pedit_key *key = sel->keys;
795 struct m_pedit_key_ex *key_ex = keys_ex;
796
797 for (i = 0; i < sel->nkeys; i++, key++) {
798 enum pedit_header_type htype =
799 TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
800 enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
801
802 if (keys_ex) {
803 htype = key_ex->htype;
804 cmd = key_ex->cmd;
805
806 key_ex++;
807 }
808
809 fprintf(f, "\n\t key #%d", i);
810
811 fprintf(f, " at ");
812
813 print_pedit_location(f, htype, key->off);
814
815 fprintf(f, ": %s %08x mask %08x",
816 cmd ? "add" : "val",
817 (unsigned int)ntohl(key->val),
818 (unsigned int)ntohl(key->mask));
819 }
820 } else {
821 fprintf(f, "\npedit %x keys %d is not LEGIT", sel->index,
822 sel->nkeys);
823 }
824
825 fprintf(f, "\n ");
826
827 free(keys_ex);
828 return 0;
829 }
830
pedit_print_xstats(struct action_util * au,FILE * f,struct rtattr * xstats)831 int pedit_print_xstats(struct action_util *au, FILE *f, struct rtattr *xstats)
832 {
833 return 0;
834 }
835
836 struct action_util pedit_action_util = {
837 .id = "pedit",
838 .parse_aopt = parse_pedit,
839 .print_aopt = print_pedit,
840 };
841