osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * f_tcindex.c Traffic control index filter |
| 3 | * |
| 4 | * Written 1998,1999 by Werner Almesberger |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <unistd.h> |
| 10 | #include <syslog.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <string.h> |
| 13 | #include <netinet/in.h> |
| 14 | |
| 15 | #include "utils.h" |
| 16 | #include "tc_util.h" |
| 17 | |
| 18 | static void explain(void) |
| 19 | { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 20 | fprintf(stderr," Usage: ... tcindex [ hash SIZE ] [ mask MASK ] [ shift SHIFT ]\n"); |
| 21 | fprintf(stderr, " [ pass_on | fall_through ]\n"); |
| 22 | fprintf(stderr," [ classid CLASSID ] [ action ACTION_SPEC ]\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 23 | } |
| 24 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 25 | static int tcindex_parse_opt(struct filter_util *qu, char *handle, int argc, |
Jamal Hadi Salim | 06be01f | 2016-08-22 07:17:33 -0400 | [diff] [blame] | 26 | char **argv, struct nlmsghdr *n) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 27 | { |
| 28 | struct tcmsg *t = NLMSG_DATA(n); |
| 29 | struct rtattr *tail; |
| 30 | char *end; |
| 31 | |
| 32 | if (handle) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 33 | t->tcm_handle = strtoul(handle, &end, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 34 | if (*end) { |
| 35 | fprintf(stderr, "Illegal filter ID\n"); |
| 36 | return -1; |
| 37 | } |
| 38 | } |
| 39 | if (!argc) return 0; |
4!tgraf | 3b3ecd3 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 40 | tail = NLMSG_TAIL(n); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 41 | addattr_l(n, 4096, TCA_OPTIONS, NULL, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 42 | while (argc) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 43 | if (!strcmp(*argv, "hash")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 44 | int hash; |
| 45 | |
| 46 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 47 | hash = strtoul(*argv, &end, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 48 | if (*end || !hash || hash > 0x10000) { |
| 49 | explain(); |
| 50 | return -1; |
| 51 | } |
Jamal Hadi Salim | 06be01f | 2016-08-22 07:17:33 -0400 | [diff] [blame] | 52 | addattr_l(n, 4096, TCA_TCINDEX_HASH, &hash, |
| 53 | sizeof(hash)); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 54 | } else if (!strcmp(*argv,"mask")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 55 | __u16 mask; |
| 56 | |
| 57 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 58 | mask = strtoul(*argv, &end, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 59 | if (*end) { |
| 60 | explain(); |
| 61 | return -1; |
| 62 | } |
Jamal Hadi Salim | 06be01f | 2016-08-22 07:17:33 -0400 | [diff] [blame] | 63 | addattr_l(n, 4096, TCA_TCINDEX_MASK, &mask, |
| 64 | sizeof(mask)); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 65 | } else if (!strcmp(*argv,"shift")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 66 | int shift; |
| 67 | |
| 68 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 69 | shift = strtoul(*argv, &end, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 70 | if (*end) { |
| 71 | explain(); |
| 72 | return -1; |
| 73 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 74 | addattr_l(n, 4096, TCA_TCINDEX_SHIFT, &shift, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 75 | sizeof(shift)); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 76 | } else if (!strcmp(*argv,"fall_through")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 77 | int value = 1; |
| 78 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 79 | addattr_l(n, 4096, TCA_TCINDEX_FALL_THROUGH, &value, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 80 | sizeof(value)); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 81 | } else if (!strcmp(*argv,"pass_on")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 82 | int value = 0; |
| 83 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 84 | addattr_l(n, 4096, TCA_TCINDEX_FALL_THROUGH, &value, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 85 | sizeof(value)); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 86 | } else if (!strcmp(*argv,"classid")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 87 | __u32 handle; |
| 88 | |
| 89 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 90 | if (get_tc_classid(&handle, *argv)) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 91 | fprintf(stderr, "Illegal \"classid\"\n"); |
| 92 | return -1; |
| 93 | } |
| 94 | addattr_l(n, 4096, TCA_TCINDEX_CLASSID, &handle, 4); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 95 | } else if (!strcmp(*argv,"police")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 96 | NEXT_ARG(); |
| 97 | if (parse_police(&argc, &argv, TCA_TCINDEX_POLICE, n)) { |
| 98 | fprintf(stderr, "Illegal \"police\"\n"); |
| 99 | return -1; |
| 100 | } |
| 101 | continue; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 102 | } else if (!strcmp(*argv,"action")) { |
Jamal Hadi Salim | 08139c2 | 2014-10-06 07:30:16 -0400 | [diff] [blame] | 103 | NEXT_ARG(); |
Jamal Hadi Salim | 06be01f | 2016-08-22 07:17:33 -0400 | [diff] [blame] | 104 | if (parse_action(&argc, &argv, TCA_TCINDEX_ACT, n)) { |
Jamal Hadi Salim | 08139c2 | 2014-10-06 07:30:16 -0400 | [diff] [blame] | 105 | fprintf(stderr, "Illegal \"action\"\n"); |
| 106 | return -1; |
| 107 | } |
| 108 | continue; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 109 | } else { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 110 | explain(); |
| 111 | return -1; |
| 112 | } |
| 113 | argc--; |
| 114 | argv++; |
| 115 | } |
4!tgraf | 3b3ecd3 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 116 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | static int tcindex_print_opt(struct filter_util *qu, FILE *f, |
Jamal Hadi Salim | 06be01f | 2016-08-22 07:17:33 -0400 | [diff] [blame] | 122 | struct rtattr *opt, __u32 handle) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 123 | { |
| 124 | struct rtattr *tb[TCA_TCINDEX_MAX+1]; |
| 125 | |
4!tgraf | 3b3ecd3 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 126 | if (opt == NULL) |
| 127 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 128 | |
4!tgraf | 3b3ecd3 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 129 | parse_rtattr_nested(tb, TCA_TCINDEX_MAX, opt); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 130 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 131 | if (handle != ~0) fprintf(f, "handle 0x%04x ", handle); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 132 | if (tb[TCA_TCINDEX_HASH]) { |
| 133 | __u16 hash; |
| 134 | |
| 135 | if (RTA_PAYLOAD(tb[TCA_TCINDEX_HASH]) < sizeof(hash)) |
| 136 | return -1; |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 137 | hash = rta_getattr_u16(tb[TCA_TCINDEX_HASH]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 138 | fprintf(f, "hash %d ", hash); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 139 | } |
| 140 | if (tb[TCA_TCINDEX_MASK]) { |
| 141 | __u16 mask; |
| 142 | |
| 143 | if (RTA_PAYLOAD(tb[TCA_TCINDEX_MASK]) < sizeof(mask)) |
| 144 | return -1; |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 145 | mask = rta_getattr_u16(tb[TCA_TCINDEX_MASK]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 146 | fprintf(f, "mask 0x%04x ", mask); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 147 | } |
| 148 | if (tb[TCA_TCINDEX_SHIFT]) { |
| 149 | int shift; |
| 150 | |
| 151 | if (RTA_PAYLOAD(tb[TCA_TCINDEX_SHIFT]) < sizeof(shift)) |
| 152 | return -1; |
| 153 | shift = *(int *) RTA_DATA(tb[TCA_TCINDEX_SHIFT]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 154 | fprintf(f, "shift %d ", shift); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 155 | } |
| 156 | if (tb[TCA_TCINDEX_FALL_THROUGH]) { |
| 157 | int fall_through; |
| 158 | |
| 159 | if (RTA_PAYLOAD(tb[TCA_TCINDEX_FALL_THROUGH]) < |
| 160 | sizeof(fall_through)) |
| 161 | return -1; |
| 162 | fall_through = *(int *) RTA_DATA(tb[TCA_TCINDEX_FALL_THROUGH]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 163 | fprintf(f, fall_through ? "fall_through " : "pass_on "); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 164 | } |
| 165 | if (tb[TCA_TCINDEX_CLASSID]) { |
| 166 | SPRINT_BUF(b1); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 167 | fprintf(f, "classid %s ", sprint_tc_classid(*(__u32 *) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 168 | RTA_DATA(tb[TCA_TCINDEX_CLASSID]), b1)); |
| 169 | } |
| 170 | if (tb[TCA_TCINDEX_POLICE]) { |
| 171 | fprintf(f, "\n"); |
| 172 | tc_print_police(f, tb[TCA_TCINDEX_POLICE]); |
| 173 | } |
Jamal Hadi Salim | 08139c2 | 2014-10-06 07:30:16 -0400 | [diff] [blame] | 174 | if (tb[TCA_TCINDEX_ACT]) { |
| 175 | fprintf(f, "\n"); |
Jamal Hadi Salim | 06be01f | 2016-08-22 07:17:33 -0400 | [diff] [blame] | 176 | tc_print_action(f, tb[TCA_TCINDEX_ACT]); |
Jamal Hadi Salim | 08139c2 | 2014-10-06 07:30:16 -0400 | [diff] [blame] | 177 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
osdl.net!shemminger | 6b7dff1 | 2004-09-28 18:35:49 +0000 | [diff] [blame] | 181 | struct filter_util tcindex_filter_util = { |
| 182 | .id = "tcindex", |
| 183 | .parse_fopt = tcindex_parse_opt, |
| 184 | .print_fopt = tcindex_print_opt, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 185 | }; |