Debian Codegraph @

tc/m_pedit.c

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