4 * This program is free software; you can u32istribute 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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * Match mark added by Catalin(ux aka Dino) BOIE <catab at umbrella.ro> [5 nov 2004]
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 #include <linux/if_ether.h>
29 extern int show_pretty;
31 static void explain(void)
33 fprintf(stderr, "Usage: ... u32 [ match SELECTOR ... ] [ link HTID ]"
34 " [ classid CLASSID ]\n");
35 fprintf(stderr, " [ police POLICE_SPEC ]"
36 " [ offset OFFSET_SPEC ]\n");
37 fprintf(stderr, " [ ht HTID ] [ hashkey HASHKEY_SPEC ]\n");
38 fprintf(stderr, " [ sample SAMPLE ]\n");
39 fprintf(stderr, "or u32 divisor DIVISOR\n");
40 fprintf(stderr, "\n");
41 fprintf(stderr, "Where: SELECTOR := SAMPLE SAMPLE ...\n");
42 fprintf(stderr, " SAMPLE := { ip | ip6 | udp | tcp | icmp |"
43 " u{32|16|8} | mark } SAMPLE_ARGS [divisor DIVISOR]\n");
44 fprintf(stderr, " FILTERID := X:Y:Z\n");
45 fprintf(stderr, "\nNOTE: CLASSID is parsed at hexadecimal input.\n");
48 int get_u32_handle(__u32 *handle, const char *str)
50 __u32 htid=0, hash=0, nodeid=0;
51 char *tmp = strchr(str, ':');
54 if (memcmp("0x", str, 2) == 0)
55 return get_u32(handle, str, 16);
58 htid = strtoul(str, &tmp, 16);
59 if (tmp == str && *str != ':' && *str != 0)
65 hash = strtoul(str, &tmp, 16);
66 if (tmp == str && *str != ':' && *str != 0)
72 nodeid = strtoul(str, &tmp, 16);
73 if (tmp == str && *str != 0)
79 *handle = (htid<<20)|(hash<<12)|nodeid;
83 char * sprint_u32_handle(__u32 handle, char *buf)
85 int bsize = SPRINT_BSIZE-1;
86 __u32 htid = TC_U32_HTID(handle);
87 __u32 hash = TC_U32_HASH(handle);
88 __u32 nodeid = TC_U32_NODE(handle);
92 snprintf(b, bsize, "none");
96 int l = snprintf(b, bsize, "%x:", htid>>20);
102 int l = snprintf(b, bsize, "%x", hash);
107 int l = snprintf(b, bsize, ":%x", nodeid);
113 snprintf(b, bsize, "[%08x] ", handle);
117 static int pack_key(struct tc_u32_sel *sel, __u32 key, __u32 mask,
118 int off, int offmask)
121 int hwm = sel->nkeys;
125 for (i=0; i<hwm; i++) {
126 if (sel->keys[i].off == off && sel->keys[i].offmask == offmask) {
127 __u32 intersect = mask&sel->keys[i].mask;
129 if ((key^sel->keys[i].val) & intersect)
131 sel->keys[i].val |= key;
132 sel->keys[i].mask |= mask;
141 sel->keys[hwm].val = key;
142 sel->keys[hwm].mask = mask;
143 sel->keys[hwm].off = off;
144 sel->keys[hwm].offmask = offmask;
149 static int pack_key32(struct tc_u32_sel *sel, __u32 key, __u32 mask,
150 int off, int offmask)
154 return pack_key(sel, key, mask, off, offmask);
157 static int pack_key16(struct tc_u32_sel *sel, __u32 key, __u32 mask,
158 int off, int offmask)
160 if (key > 0xFFFF || mask > 0xFFFF)
163 if ((off & 3) == 0) {
171 return pack_key(sel, key, mask, off, offmask);
174 static int pack_key8(struct tc_u32_sel *sel, __u32 key, __u32 mask, int off, int offmask)
176 if (key > 0xFF || mask > 0xFF)
179 if ((off & 3) == 0) {
182 } else if ((off & 3) == 1) {
185 } else if ((off & 3) == 2) {
193 return pack_key(sel, key, mask, off, offmask);
197 int parse_at(int *argc_p, char ***argv_p, int *off, int *offmask)
200 char **argv = *argv_p;
206 if (strlen(p) > strlen("nexthdr+") &&
207 memcmp(p, "nexthdr+", strlen("nexthdr+")) == 0) {
209 p += strlen("nexthdr+");
210 } else if (matches(*argv, "nexthdr+") == 0) {
216 if (get_integer(off, p, 0))
226 static int parse_u32(int *argc_p, char ***argv_p, struct tc_u32_sel *sel,
227 int off, int offmask)
231 char **argv = *argv_p;
238 if (get_u32(&key, *argv, 0))
242 if (get_u32(&mask, *argv, 16))
246 if (argc > 0 && strcmp(argv[0], "at") == 0) {
248 if (parse_at(&argc, &argv, &off, &offmask))
252 res = pack_key32(sel, key, mask, off, offmask);
258 static int parse_u16(int *argc_p, char ***argv_p, struct tc_u32_sel *sel,
259 int off, int offmask)
263 char **argv = *argv_p;
270 if (get_u32(&key, *argv, 0))
274 if (get_u32(&mask, *argv, 16))
278 if (argc > 0 && strcmp(argv[0], "at") == 0) {
280 if (parse_at(&argc, &argv, &off, &offmask))
283 res = pack_key16(sel, key, mask, off, offmask);
289 static int parse_u8(int *argc_p, char ***argv_p, struct tc_u32_sel *sel,
290 int off, int offmask)
294 char **argv = *argv_p;
301 if (get_u32(&key, *argv, 0))
305 if (get_u32(&mask, *argv, 16))
309 if (key > 0xFF || mask > 0xFF)
312 if (argc > 0 && strcmp(argv[0], "at") == 0) {
314 if (parse_at(&argc, &argv, &off, &offmask))
318 res = pack_key8(sel, key, mask, off, offmask);
324 static int parse_ip_addr(int *argc_p, char ***argv_p, struct tc_u32_sel *sel,
329 char **argv = *argv_p;
337 if (get_prefix_1(&addr, *argv, AF_INET))
341 if (argc > 0 && strcmp(argv[0], "at") == 0) {
343 if (parse_at(&argc, &argv, &off, &offmask))
349 mask = htonl(0xFFFFFFFF<<(32-addr.bitlen));
350 if (pack_key(sel, addr.data[0], mask, off, offmask) < 0)
359 static int parse_ip6_addr(int *argc_p, char ***argv_p,
360 struct tc_u32_sel *sel, int off)
364 char **argv = *argv_p;
373 if (get_prefix_1(&addr, *argv, AF_INET6))
377 if (argc > 0 && strcmp(argv[0], "at") == 0) {
379 if (parse_at(&argc, &argv, &off, &offmask))
384 for (i=0; i<plen; i+=32) {
385 // if (((i+31)&~0x1F)<=plen) {
386 if (i + 31 <= plen) {
387 res = pack_key(sel, addr.data[i/32],
388 0xFFFFFFFF, off+4*(i/32), offmask);
391 } else if (i < plen) {
392 __u32 mask = htonl(0xFFFFFFFF << (32 - (plen -i )));
393 res = pack_key(sel, addr.data[i/32],
394 mask, off+4*(i/32), offmask);
406 static int parse_ip6_class(int *argc_p, char ***argv_p, struct tc_u32_sel *sel)
410 char **argv = *argv_p;
419 if (get_u32(&key, *argv, 0))
423 if (get_u32(&mask, *argv, 16))
427 if (key > 0xFF || mask > 0xFF)
435 res = pack_key(sel, key, mask, off, offmask);
444 static int parse_ether_addr(int *argc_p, char ***argv_p,
445 struct tc_u32_sel *sel, int off)
449 char **argv = *argv_p;
457 if (sscanf(*argv, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
458 addr + 0, addr + 1, addr + 2,
459 addr + 3, addr + 4, addr + 5) != 6) {
460 fprintf(stderr, "parse_ether_addr: improperly formed address '%s'\n",
466 if (argc > 0 && strcmp(argv[0], "at") == 0) {
468 if (parse_at(&argc, &argv, &off, &offmask))
472 for (i = 0; i < 6; i++) {
473 res = pack_key8(sel, addr[i], 0xFF, off + i, offmask);
483 static int parse_ip(int *argc_p, char ***argv_p, struct tc_u32_sel *sel)
487 char **argv = *argv_p;
492 if (strcmp(*argv, "src") == 0) {
494 res = parse_ip_addr(&argc, &argv, sel, 12);
495 } else if (strcmp(*argv, "dst") == 0) {
497 res = parse_ip_addr(&argc, &argv, sel, 16);
498 } else if (strcmp(*argv, "tos") == 0 ||
499 matches(*argv, "dsfield") == 0) {
501 res = parse_u8(&argc, &argv, sel, 1, 0);
502 } else if (strcmp(*argv, "ihl") == 0) {
504 res = parse_u8(&argc, &argv, sel, 0, 0);
505 } else if (strcmp(*argv, "protocol") == 0) {
507 res = parse_u8(&argc, &argv, sel, 9, 0);
508 } else if (matches(*argv, "precedence") == 0) {
510 res = parse_u8(&argc, &argv, sel, 1, 0);
511 } else if (strcmp(*argv, "nofrag") == 0) {
513 res = pack_key16(sel, 0, 0x3FFF, 6, 0);
514 } else if (strcmp(*argv, "firstfrag") == 0) {
516 res = pack_key16(sel, 0, 0x1FFF, 6, 0);
517 } else if (strcmp(*argv, "df") == 0) {
519 res = pack_key16(sel, 0x4000, 0x4000, 6, 0);
520 } else if (strcmp(*argv, "mf") == 0) {
522 res = pack_key16(sel, 0x2000, 0x2000, 6, 0);
523 } else if (strcmp(*argv, "dport") == 0) {
525 res = parse_u16(&argc, &argv, sel, 22, 0);
526 } else if (strcmp(*argv, "sport") == 0) {
528 res = parse_u16(&argc, &argv, sel, 20, 0);
529 } else if (strcmp(*argv, "icmp_type") == 0) {
531 res = parse_u8(&argc, &argv, sel, 20, 0);
532 } else if (strcmp(*argv, "icmp_code") == 0) {
534 res = parse_u8(&argc, &argv, sel, 20, 1);
543 static int parse_ip6(int *argc_p, char ***argv_p, struct tc_u32_sel *sel)
547 char **argv = *argv_p;
552 if (strcmp(*argv, "src") == 0) {
554 res = parse_ip6_addr(&argc, &argv, sel, 8);
555 } else if (strcmp(*argv, "dst") == 0) {
557 res = parse_ip6_addr(&argc, &argv, sel, 24);
558 } else if (strcmp(*argv, "priority") == 0) {
560 res = parse_ip6_class(&argc, &argv, sel);
561 } else if (strcmp(*argv, "protocol") == 0) {
563 res = parse_u8(&argc, &argv, sel, 6, 0);
564 } else if (strcmp(*argv, "flowlabel") == 0) {
566 res = parse_u32(&argc, &argv, sel, 0, 0);
567 } else if (strcmp(*argv, "dport") == 0) {
569 res = parse_u16(&argc, &argv, sel, 42, 0);
570 } else if (strcmp(*argv, "sport") == 0) {
572 res = parse_u16(&argc, &argv, sel, 40, 0);
573 } else if (strcmp(*argv, "icmp_type") == 0) {
575 res = parse_u8(&argc, &argv, sel, 40, 0);
576 } else if (strcmp(*argv, "icmp_code") == 0) {
578 res = parse_u8(&argc, &argv, sel, 41, 1);
587 static int parse_ether(int *argc_p, char ***argv_p, struct tc_u32_sel *sel)
591 char **argv = *argv_p;
596 if (strcmp(*argv, "src") == 0) {
598 res = parse_ether_addr(&argc, &argv, sel, -8);
599 } else if (strcmp(*argv, "dst") == 0) {
601 res = parse_ether_addr(&argc, &argv, sel, -14);
603 fprintf(stderr, "Unknown match: ether %s\n", *argv);
612 #define parse_tcp parse_udp
613 static int parse_udp(int *argc_p, char ***argv_p, struct tc_u32_sel *sel)
617 char **argv = *argv_p;
622 if (strcmp(*argv, "src") == 0) {
624 res = parse_u16(&argc, &argv, sel, 0, -1);
625 } else if (strcmp(*argv, "dst") == 0) {
627 res = parse_u16(&argc, &argv, sel, 2, -1);
637 static int parse_icmp(int *argc_p, char ***argv_p, struct tc_u32_sel *sel)
641 char **argv = *argv_p;
646 if (strcmp(*argv, "type") == 0) {
648 res = parse_u8(&argc, &argv, sel, 0, -1);
649 } else if (strcmp(*argv, "code") == 0) {
651 res = parse_u8(&argc, &argv, sel, 1, -1);
660 static int parse_mark(int *argc_p, char ***argv_p, struct nlmsghdr *n)
664 char **argv = *argv_p;
665 struct tc_u32_mark mark;
670 if (get_u32(&mark.val, *argv, 0)) {
671 fprintf(stderr, "Illegal \"mark\" value\n");
676 if (get_u32(&mark.mask, *argv, 0)) {
677 fprintf(stderr, "Illegal \"mark\" mask\n");
682 if ((mark.val & mark.mask) != mark.val) {
683 fprintf(stderr, "Illegal \"mark\" (impossible combination)\n");
687 addattr_l(n, MAX_MSG, TCA_U32_MARK, &mark, sizeof(mark));
695 static int parse_selector(int *argc_p, char ***argv_p,
696 struct tc_u32_sel *sel, struct nlmsghdr *n)
699 char **argv = *argv_p;
705 if (matches(*argv, "u32") == 0) {
707 res = parse_u32(&argc, &argv, sel, 0, 0);
708 } else if (matches(*argv, "u16") == 0) {
710 res = parse_u16(&argc, &argv, sel, 0, 0);
711 } else if (matches(*argv, "u8") == 0) {
713 res = parse_u8(&argc, &argv, sel, 0, 0);
714 } else if (matches(*argv, "ip") == 0) {
716 res = parse_ip(&argc, &argv, sel);
717 } else if (matches(*argv, "ip6") == 0) {
719 res = parse_ip6(&argc, &argv, sel);
720 } else if (matches(*argv, "udp") == 0) {
722 res = parse_udp(&argc, &argv, sel);
723 } else if (matches(*argv, "tcp") == 0) {
725 res = parse_tcp(&argc, &argv, sel);
726 } else if (matches(*argv, "icmp") == 0) {
728 res = parse_icmp(&argc, &argv, sel);
729 } else if (matches(*argv, "mark") == 0) {
731 res = parse_mark(&argc, &argv, n);
732 } else if (matches(*argv, "ether") == 0) {
734 res = parse_ether(&argc, &argv, sel);
743 static int parse_offset(int *argc_p, char ***argv_p, struct tc_u32_sel *sel)
746 char **argv = *argv_p;
749 if (matches(*argv, "plus") == 0) {
752 if (get_integer(&off, *argv, 0))
755 sel->flags |= TC_U32_OFFSET;
756 } else if (matches(*argv, "at") == 0) {
759 if (get_integer(&off, *argv, 0))
763 fprintf(stderr, "offset \"at\" must be even\n");
766 sel->flags |= TC_U32_VAROFFSET;
767 } else if (matches(*argv, "mask") == 0) {
770 if (get_u16(&mask, *argv, 16))
772 sel->offmask = htons(mask);
773 sel->flags |= TC_U32_VAROFFSET;
774 } else if (matches(*argv, "shift") == 0) {
777 if (get_integer(&shift, *argv, 0))
779 sel->offshift = shift;
780 sel->flags |= TC_U32_VAROFFSET;
781 } else if (matches(*argv, "eat") == 0) {
782 sel->flags |= TC_U32_EAT;
794 static int parse_hashkey(int *argc_p, char ***argv_p, struct tc_u32_sel *sel)
797 char **argv = *argv_p;
800 if (matches(*argv, "mask") == 0) {
803 if (get_u32(&mask, *argv, 16))
805 sel->hmask = htonl(mask);
806 } else if (matches(*argv, "at") == 0) {
809 if (get_integer(&num, *argv, 0))
825 static void print_ipv4(FILE *f, const struct tc_u32_key *key)
831 switch (ntohl(key->mask)) {
833 fprintf(f, "\n match IP ihl %u", ntohl(key->val) >> 24);
836 fprintf(f, "\n match IP dsfield %#x", ntohl(key->val) >> 16);
841 if (ntohl(key->mask) == 0x00ff0000) {
842 fprintf(f, "\n match IP protocol %d", ntohl(key->val) >> 16);
848 int bits = mask2bits(key->mask);
850 fprintf(f, "\n %s %s/%d",
851 key->off == 12 ? "match IP src" : "match IP dst",
852 inet_ntop(AF_INET, &key->val,
861 switch (ntohl(key->mask)) {
863 fprintf(f, "\n match dport %u",
864 ntohl(key->val) & 0xffff);
867 fprintf(f, "\n match sport %u",
868 ntohl(key->val) >> 16);
871 fprintf(f, "\n match dport %u, match sport %u",
872 ntohl(key->val) & 0xffff,
873 ntohl(key->val) >> 16);
877 /* XXX: Default print_raw */
881 static void print_ipv6(FILE *f, const struct tc_u32_key *key)
887 switch (ntohl(key->mask)) {
889 fprintf(f, "\n match IP ihl %u", ntohl(key->val) >> 24);
892 fprintf(f, "\n match IP dsfield %#x", ntohl(key->val) >> 16);
897 if (ntohl(key->mask) == 0x00ff0000) {
898 fprintf(f, "\n match IP protocol %d", ntohl(key->val) >> 16);
904 int bits = mask2bits(key->mask);
906 fprintf(f, "\n %s %s/%d",
907 key->off == 12 ? "match IP src" : "match IP dst",
908 inet_ntop(AF_INET, &key->val,
917 switch (ntohl(key->mask)) {
919 fprintf(f, "\n match sport %u",
920 ntohl(key->val) & 0xffff);
923 fprintf(f, "\n match dport %u",
924 ntohl(key->val) >> 16);
927 fprintf(f, "\n match sport %u, match dport %u",
928 ntohl(key->val) & 0xffff,
929 ntohl(key->val) >> 16);
933 /* XXX: Default print_raw */
937 static void print_raw(FILE *f, const struct tc_u32_key *key)
939 fprintf(f, "\n match %08x/%08x at %s%d",
940 (unsigned int)ntohl(key->val),
941 (unsigned int)ntohl(key->mask),
942 key->offmask ? "nexthdr+" : "",
946 static const struct {
949 void (*pprinter)(FILE *f, const struct tc_u32_key *key);
950 } u32_pprinters[] = {
952 {ETH_P_IP, 0, print_ipv4},
953 {ETH_P_IPV6, 0, print_ipv6},
956 static void show_keys(FILE *f, const struct tc_u32_key *key)
963 for (i = 0; i < sizeof(u32_pprinters) / sizeof(u32_pprinters[0]); i++) {
964 if (u32_pprinters[i].proto == ntohs(f_proto)) {
966 u32_pprinters[i].pprinter(f, key);
975 static int u32_parse_opt(struct filter_util *qu, char *handle,
976 int argc, char **argv, struct nlmsghdr *n)
979 struct tc_u32_sel sel;
980 struct tc_u32_key keys[128];
982 struct tcmsg *t = NLMSG_DATA(n);
984 int sel_ok = 0, terminal_ok = 0;
989 memset(&sel, 0, sizeof(sel));
991 if (handle && get_u32_handle(&t->tcm_handle, handle)) {
992 fprintf(stderr, "Illegal filter ID\n");
999 tail = NLMSG_TAIL(n);
1000 addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0);
1003 if (matches(*argv, "match") == 0) {
1005 if (parse_selector(&argc, &argv, &sel.sel, n)) {
1006 fprintf(stderr, "Illegal \"match\"\n");
1011 } else if (matches(*argv, "offset") == 0) {
1013 if (parse_offset(&argc, &argv, &sel.sel)) {
1014 fprintf(stderr, "Illegal \"offset\"\n");
1018 } else if (matches(*argv, "hashkey") == 0) {
1020 if (parse_hashkey(&argc, &argv, &sel.sel)) {
1021 fprintf(stderr, "Illegal \"hashkey\"\n");
1025 } else if (matches(*argv, "classid") == 0 ||
1026 strcmp(*argv, "flowid") == 0) {
1029 if (get_tc_classid(&handle, *argv)) {
1030 fprintf(stderr, "Illegal \"classid\"\n");
1033 addattr_l(n, MAX_MSG, TCA_U32_CLASSID, &handle, 4);
1034 sel.sel.flags |= TC_U32_TERMINAL;
1035 } else if (matches(*argv, "divisor") == 0) {
1038 if (get_unsigned(&divisor, *argv, 0) ||
1040 divisor > 0x100 || ((divisor - 1) & divisor)) {
1041 fprintf(stderr, "Illegal \"divisor\"\n");
1044 addattr_l(n, MAX_MSG, TCA_U32_DIVISOR, &divisor, 4);
1045 } else if (matches(*argv, "order") == 0) {
1047 if (get_u32(&order, *argv, 0)) {
1048 fprintf(stderr, "Illegal \"order\"\n");
1051 } else if (strcmp(*argv, "link") == 0) {
1054 if (get_u32_handle(&handle, *argv)) {
1055 fprintf(stderr, "Illegal \"link\"\n");
1058 if (handle && TC_U32_NODE(handle)) {
1059 fprintf(stderr, "\"link\" must be a hash table.\n");
1062 addattr_l(n, MAX_MSG, TCA_U32_LINK, &handle, 4);
1063 } else if (strcmp(*argv, "ht") == 0) {
1066 if (get_u32_handle(&handle, *argv)) {
1067 fprintf(stderr, "Illegal \"ht\"\n");
1070 if (handle && TC_U32_NODE(handle)) {
1071 fprintf(stderr, "\"ht\" must be a hash table.\n");
1075 htid = (htid&0xFF000)|(handle&0xFFF00000);
1077 htid = (handle&0xFFFFF000);
1078 } else if (strcmp(*argv, "sample") == 0) {
1080 unsigned divisor = 0x100;
1083 struct tc_u32_sel sel;
1084 struct tc_u32_key keys[4];
1086 memset(&sel2, 0, sizeof(sel2));
1088 if (parse_selector(&argc, &argv, &sel2.sel, n)) {
1089 fprintf(stderr, "Illegal \"sample\"\n");
1092 if (sel2.sel.nkeys != 1) {
1093 fprintf(stderr, "\"sample\" must contain"
1094 " exactly ONE key.\n");
1097 if (*argv != 0 && strcmp(*argv, "divisor") == 0) {
1099 if (get_unsigned(&divisor, *argv, 0) || divisor == 0 ||
1100 divisor > 0x100 || ((divisor - 1) & divisor)) {
1101 fprintf(stderr, "Illegal sample \"divisor\"\n");
1106 hash = sel2.sel.keys[0].val&sel2.sel.keys[0].mask;
1109 htid = ((hash%divisor)<<12)|(htid&0xFFF00000);
1112 } else if (strcmp(*argv, "indev") == 0) {
1113 char ind[IFNAMSIZ + 1];
1114 memset(ind, 0, sizeof (ind));
1118 fprintf(stderr, "Illegal indev\n");
1121 strncpy(ind, *argv, sizeof (ind) - 1);
1122 addattr_l(n, MAX_MSG, TCA_U32_INDEV, ind, strlen(ind) + 1);
1124 } else if (matches(*argv, "action") == 0) {
1126 if (parse_action(&argc, &argv, TCA_U32_ACT, n)) {
1127 fprintf(stderr, "Illegal \"action\"\n");
1133 } else if (matches(*argv, "police") == 0) {
1135 if (parse_police(&argc, &argv, TCA_U32_POLICE, n)) {
1136 fprintf(stderr, "Illegal \"police\"\n");
1141 } else if (strcmp(*argv, "help") == 0) {
1145 fprintf(stderr, "What is \"%s\"?\n", *argv);
1152 /* We dont necessarily need class/flowids */
1154 sel.sel.flags |= TC_U32_TERMINAL;
1157 if (TC_U32_NODE(t->tcm_handle) && order != TC_U32_NODE(t->tcm_handle)) {
1158 fprintf(stderr, "\"order\" contradicts \"handle\"\n");
1161 t->tcm_handle |= order;
1165 addattr_l(n, MAX_MSG, TCA_U32_HASH, &htid, 4);
1167 addattr_l(n, MAX_MSG, TCA_U32_SEL, &sel,
1168 sizeof(sel.sel)+sel.sel.nkeys*sizeof(struct tc_u32_key));
1169 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
1173 static int u32_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt,
1176 struct rtattr *tb[TCA_U32_MAX+1];
1177 struct tc_u32_sel *sel = NULL;
1178 struct tc_u32_pcnt *pf = NULL;
1183 parse_rtattr_nested(tb, TCA_U32_MAX, opt);
1187 fprintf(f, "fh %s ", sprint_u32_handle(handle, b1));
1189 if (TC_U32_NODE(handle)) {
1190 fprintf(f, "order %d ", TC_U32_NODE(handle));
1193 if (tb[TCA_U32_SEL]) {
1194 if (RTA_PAYLOAD(tb[TCA_U32_SEL]) < sizeof(*sel))
1197 sel = RTA_DATA(tb[TCA_U32_SEL]);
1200 if (tb[TCA_U32_DIVISOR]) {
1201 fprintf(f, "ht divisor %d ", rta_getattr_u32(tb[TCA_U32_DIVISOR]));
1202 } else if (tb[TCA_U32_HASH]) {
1203 __u32 htid = rta_getattr_u32(tb[TCA_U32_HASH]);
1204 fprintf(f, "key ht %x bkt %x ", TC_U32_USERHTID(htid),
1209 if (tb[TCA_U32_CLASSID]) {
1211 fprintf(f, "%sflowid %s ",
1212 !sel || !(sel->flags&TC_U32_TERMINAL) ? "*" : "",
1213 sprint_tc_classid(rta_getattr_u32(tb[TCA_U32_CLASSID]), b1));
1214 } else if (sel && sel->flags&TC_U32_TERMINAL) {
1215 fprintf(f, "terminal flowid ??? ");
1217 if (tb[TCA_U32_LINK]) {
1219 fprintf(f, "link %s ",
1220 sprint_u32_handle(rta_getattr_u32(tb[TCA_U32_LINK]), b1));
1223 if (tb[TCA_U32_PCNT]) {
1224 if (RTA_PAYLOAD(tb[TCA_U32_PCNT]) < sizeof(*pf)) {
1225 fprintf(f, "Broken perf counters \n");
1228 pf = RTA_DATA(tb[TCA_U32_PCNT]);
1231 if (sel && show_stats && NULL != pf)
1232 fprintf(f, " (rule hit %llu success %llu)",
1233 (unsigned long long) pf->rcnt,
1234 (unsigned long long) pf->rhit);
1236 if (tb[TCA_U32_MARK]) {
1237 struct tc_u32_mark *mark = RTA_DATA(tb[TCA_U32_MARK]);
1238 if (RTA_PAYLOAD(tb[TCA_U32_MARK]) < sizeof(*mark)) {
1239 fprintf(f, "\n Invalid mark (kernel&iproute2 mismatch)\n");
1241 fprintf(f, "\n mark 0x%04x 0x%04x (success %d)",
1242 mark->val, mark->mask, mark->success);
1249 for (i=0; i<sel->nkeys; i++) {
1250 show_keys(f, sel->keys + i);
1251 if (show_stats && NULL != pf)
1252 fprintf(f, " (success %llu ) ",
1253 (unsigned long long) pf->kcnts[i]);
1257 if (sel->flags&(TC_U32_VAROFFSET|TC_U32_OFFSET)) {
1258 fprintf(f, "\n offset ");
1259 if (sel->flags&TC_U32_VAROFFSET)
1260 fprintf(f, "%04x>>%d at %d ",
1261 ntohs(sel->offmask),
1262 sel->offshift, sel->offoff);
1264 fprintf(f, "plus %d ", sel->off);
1266 if (sel->flags&TC_U32_EAT)
1267 fprintf(f, " eat ");
1270 fprintf(f, "\n hash mask %08x at %d ",
1271 (unsigned int)htonl(sel->hmask), sel->hoff);
1275 if (tb[TCA_U32_POLICE]) {
1277 tc_print_police(f, tb[TCA_U32_POLICE]);
1279 if (tb[TCA_U32_INDEV]) {
1280 struct rtattr *idev = tb[TCA_U32_INDEV];
1281 fprintf(f, "\n input dev %s\n", rta_getattr_str(idev));
1283 if (tb[TCA_U32_ACT]) {
1284 tc_print_action(f, tb[TCA_U32_ACT]);
1290 struct filter_util u32_filter_util = {
1292 .parse_fopt = u32_parse_opt,
1293 .print_fopt = u32_print_opt,