]>
Commit | Line | Data |
---|---|---|
6054c1eb | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
aba5acdf SH |
2 | /* |
3 | * f_tcindex.c Traffic control index filter | |
4 | * | |
5 | * Written 1998,1999 by Werner Almesberger | |
6 | */ | |
7 | ||
8 | #include <stdio.h> | |
9 | #include <stdlib.h> | |
10 | #include <unistd.h> | |
aba5acdf SH |
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 | { | |
32a121cb SH |
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"); | |
aba5acdf SH |
23 | } |
24 | ||
aba5acdf | 25 | static int tcindex_parse_opt(struct filter_util *qu, char *handle, int argc, |
06be01f7 | 26 | char **argv, struct nlmsghdr *n) |
aba5acdf SH |
27 | { |
28 | struct tcmsg *t = NLMSG_DATA(n); | |
29 | struct rtattr *tail; | |
30 | char *end; | |
31 | ||
32 | if (handle) { | |
32a121cb | 33 | t->tcm_handle = strtoul(handle, &end, 0); |
aba5acdf SH |
34 | if (*end) { |
35 | fprintf(stderr, "Illegal filter ID\n"); | |
36 | return -1; | |
37 | } | |
38 | } | |
39 | if (!argc) return 0; | |
c14f9d92 | 40 | tail = addattr_nest(n, 4096, TCA_OPTIONS); |
aba5acdf | 41 | while (argc) { |
32a121cb | 42 | if (!strcmp(*argv, "hash")) { |
aba5acdf SH |
43 | int hash; |
44 | ||
45 | NEXT_ARG(); | |
32a121cb | 46 | hash = strtoul(*argv, &end, 0); |
aba5acdf SH |
47 | if (*end || !hash || hash > 0x10000) { |
48 | explain(); | |
49 | return -1; | |
50 | } | |
06be01f7 JHS |
51 | addattr_l(n, 4096, TCA_TCINDEX_HASH, &hash, |
52 | sizeof(hash)); | |
32a121cb | 53 | } else if (!strcmp(*argv,"mask")) { |
aba5acdf SH |
54 | __u16 mask; |
55 | ||
56 | NEXT_ARG(); | |
32a121cb | 57 | mask = strtoul(*argv, &end, 0); |
aba5acdf SH |
58 | if (*end) { |
59 | explain(); | |
60 | return -1; | |
61 | } | |
06be01f7 JHS |
62 | addattr_l(n, 4096, TCA_TCINDEX_MASK, &mask, |
63 | sizeof(mask)); | |
32a121cb | 64 | } else if (!strcmp(*argv,"shift")) { |
aba5acdf SH |
65 | int shift; |
66 | ||
67 | NEXT_ARG(); | |
32a121cb | 68 | shift = strtoul(*argv, &end, 0); |
aba5acdf SH |
69 | if (*end) { |
70 | explain(); | |
71 | return -1; | |
72 | } | |
32a121cb | 73 | addattr_l(n, 4096, TCA_TCINDEX_SHIFT, &shift, |
aba5acdf | 74 | sizeof(shift)); |
32a121cb | 75 | } else if (!strcmp(*argv,"fall_through")) { |
aba5acdf SH |
76 | int value = 1; |
77 | ||
32a121cb | 78 | addattr_l(n, 4096, TCA_TCINDEX_FALL_THROUGH, &value, |
aba5acdf | 79 | sizeof(value)); |
32a121cb | 80 | } else if (!strcmp(*argv,"pass_on")) { |
aba5acdf SH |
81 | int value = 0; |
82 | ||
32a121cb | 83 | addattr_l(n, 4096, TCA_TCINDEX_FALL_THROUGH, &value, |
aba5acdf | 84 | sizeof(value)); |
32a121cb | 85 | } else if (!strcmp(*argv,"classid")) { |
aba5acdf SH |
86 | __u32 handle; |
87 | ||
88 | NEXT_ARG(); | |
32a121cb | 89 | if (get_tc_classid(&handle, *argv)) { |
aba5acdf SH |
90 | fprintf(stderr, "Illegal \"classid\"\n"); |
91 | return -1; | |
92 | } | |
93 | addattr_l(n, 4096, TCA_TCINDEX_CLASSID, &handle, 4); | |
32a121cb | 94 | } else if (!strcmp(*argv,"police")) { |
aba5acdf SH |
95 | NEXT_ARG(); |
96 | if (parse_police(&argc, &argv, TCA_TCINDEX_POLICE, n)) { | |
97 | fprintf(stderr, "Illegal \"police\"\n"); | |
98 | return -1; | |
99 | } | |
100 | continue; | |
32a121cb | 101 | } else if (!strcmp(*argv,"action")) { |
08139c2f | 102 | NEXT_ARG(); |
06be01f7 | 103 | if (parse_action(&argc, &argv, TCA_TCINDEX_ACT, n)) { |
08139c2f JHS |
104 | fprintf(stderr, "Illegal \"action\"\n"); |
105 | return -1; | |
106 | } | |
107 | continue; | |
32a121cb | 108 | } else { |
aba5acdf SH |
109 | explain(); |
110 | return -1; | |
111 | } | |
112 | argc--; | |
113 | argv++; | |
114 | } | |
c14f9d92 | 115 | addattr_nest_end(n, tail); |
aba5acdf SH |
116 | return 0; |
117 | } | |
118 | ||
119 | ||
120 | static int tcindex_print_opt(struct filter_util *qu, FILE *f, | |
06be01f7 | 121 | struct rtattr *opt, __u32 handle) |
aba5acdf SH |
122 | { |
123 | struct rtattr *tb[TCA_TCINDEX_MAX+1]; | |
124 | ||
3b3ecd31 | 125 | if (opt == NULL) |
126 | return 0; | |
aba5acdf | 127 | |
3b3ecd31 | 128 | parse_rtattr_nested(tb, TCA_TCINDEX_MAX, opt); |
aba5acdf | 129 | |
32a121cb | 130 | if (handle != ~0) fprintf(f, "handle 0x%04x ", handle); |
aba5acdf SH |
131 | if (tb[TCA_TCINDEX_HASH]) { |
132 | __u16 hash; | |
133 | ||
134 | if (RTA_PAYLOAD(tb[TCA_TCINDEX_HASH]) < sizeof(hash)) | |
135 | return -1; | |
ff24746c | 136 | hash = rta_getattr_u16(tb[TCA_TCINDEX_HASH]); |
32a121cb | 137 | fprintf(f, "hash %d ", hash); |
aba5acdf SH |
138 | } |
139 | if (tb[TCA_TCINDEX_MASK]) { | |
140 | __u16 mask; | |
141 | ||
142 | if (RTA_PAYLOAD(tb[TCA_TCINDEX_MASK]) < sizeof(mask)) | |
143 | return -1; | |
ff24746c | 144 | mask = rta_getattr_u16(tb[TCA_TCINDEX_MASK]); |
32a121cb | 145 | fprintf(f, "mask 0x%04x ", mask); |
aba5acdf SH |
146 | } |
147 | if (tb[TCA_TCINDEX_SHIFT]) { | |
148 | int shift; | |
149 | ||
150 | if (RTA_PAYLOAD(tb[TCA_TCINDEX_SHIFT]) < sizeof(shift)) | |
151 | return -1; | |
a59b6162 | 152 | shift = rta_getattr_u32(tb[TCA_TCINDEX_SHIFT]); |
32a121cb | 153 | fprintf(f, "shift %d ", shift); |
aba5acdf SH |
154 | } |
155 | if (tb[TCA_TCINDEX_FALL_THROUGH]) { | |
156 | int fall_through; | |
157 | ||
158 | if (RTA_PAYLOAD(tb[TCA_TCINDEX_FALL_THROUGH]) < | |
159 | sizeof(fall_through)) | |
160 | return -1; | |
a59b6162 | 161 | fall_through = rta_getattr_u32(tb[TCA_TCINDEX_FALL_THROUGH]); |
32a121cb | 162 | fprintf(f, fall_through ? "fall_through " : "pass_on "); |
aba5acdf SH |
163 | } |
164 | if (tb[TCA_TCINDEX_CLASSID]) { | |
165 | SPRINT_BUF(b1); | |
32a121cb | 166 | fprintf(f, "classid %s ", sprint_tc_classid(*(__u32 *) |
aba5acdf SH |
167 | RTA_DATA(tb[TCA_TCINDEX_CLASSID]), b1)); |
168 | } | |
169 | if (tb[TCA_TCINDEX_POLICE]) { | |
170 | fprintf(f, "\n"); | |
171 | tc_print_police(f, tb[TCA_TCINDEX_POLICE]); | |
172 | } | |
08139c2f JHS |
173 | if (tb[TCA_TCINDEX_ACT]) { |
174 | fprintf(f, "\n"); | |
9e713525 | 175 | tc_print_action(f, tb[TCA_TCINDEX_ACT], 0); |
08139c2f | 176 | } |
aba5acdf SH |
177 | return 0; |
178 | } | |
179 | ||
6b7dff17 SH |
180 | struct filter_util tcindex_filter_util = { |
181 | .id = "tcindex", | |
182 | .parse_fopt = tcindex_parse_opt, | |
183 | .print_fopt = tcindex_print_opt, | |
aba5acdf | 184 | }; |