4 * This program is free software; you can redistribute 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: Patrick McHardy <kaber@trash.net>
21 static void explain(void)
24 "Usage: ... flow ...\n"
26 " [mapping mode]: map key KEY [ OPS ] ...\n"
27 " [hashing mode]: hash keys KEY-LIST ... [ perturb SECS ]\n"
29 " [ divisor NUM ] [ baseclass ID ] [ match EMATCH_TREE ]\n"
30 " [ police POLICE_SPEC ] [ action ACTION_SPEC ]\n"
32 "KEY-LIST := [ KEY-LIST , ] KEY\n"
33 "KEY := [ src | dst | proto | proto-src | proto-dst | iif | priority | \n"
34 " mark | nfct | nfct-src | nfct-dst | nfct-proto-src | \n"
35 " nfct-proto-dst | rt-classid | sk-uid | sk-gid |\n"
37 "OPS := [ or NUM | and NUM | xor NUM | rshift NUM | addend NUM ]\n"
42 static const char *flow_keys[FLOW_KEY_MAX+1] = {
43 [FLOW_KEY_SRC] = "src",
44 [FLOW_KEY_DST] = "dst",
45 [FLOW_KEY_PROTO] = "proto",
46 [FLOW_KEY_PROTO_SRC] = "proto-src",
47 [FLOW_KEY_PROTO_DST] = "proto-dst",
48 [FLOW_KEY_IIF] = "iif",
49 [FLOW_KEY_PRIORITY] = "priority",
50 [FLOW_KEY_MARK] = "mark",
51 [FLOW_KEY_NFCT] = "nfct",
52 [FLOW_KEY_NFCT_SRC] = "nfct-src",
53 [FLOW_KEY_NFCT_DST] = "nfct-dst",
54 [FLOW_KEY_NFCT_PROTO_SRC] = "nfct-proto-src",
55 [FLOW_KEY_NFCT_PROTO_DST] = "nfct-proto-dst",
56 [FLOW_KEY_RTCLASSID] = "rt-classid",
57 [FLOW_KEY_SKUID] = "sk-uid",
58 [FLOW_KEY_SKGID] = "sk-gid",
59 [FLOW_KEY_VLAN_TAG] = "vlan-tag",
62 static int flow_parse_keys(__u32 *keys, __u32 *nkeys, char *argv)
75 for (i = 0; i <= FLOW_KEY_MAX; i++) {
76 if (matches(s, flow_keys[i]) == 0) {
82 if (i > FLOW_KEY_MAX) {
83 fprintf(stderr, "Unknown flow key \"%s\"\n", s);
86 s = sep ? sep + 1 : NULL;
91 static void transfer_bitop(__u32 *mask, __u32 *xor, __u32 m, __u32 x)
93 *xor = x ^ (*xor & m);
97 static int get_addend(__u32 *addend, char *argv, __u32 keys)
108 if (get_u32(&tmp, argv, 0) == 0)
111 if (keys & (FLOW_KEY_SRC | FLOW_KEY_DST |
112 FLOW_KEY_NFCT_SRC | FLOW_KEY_NFCT_DST) &&
113 get_addr(&addr, argv, AF_UNSPEC) == 0) {
114 switch (addr.family) {
116 tmp = ntohl(addr.data[0]);
119 tmp = ntohl(addr.data[3]);
132 static int flow_parse_opt(struct filter_util *fu, char *handle,
133 int argc, char **argv, struct nlmsghdr *n)
136 struct tcmsg *t = NLMSG_DATA(n);
138 __u32 mask = ~0U, xor = 0;
139 __u32 keys = 0, nkeys = 0;
140 __u32 mode = FLOW_MODE_MAP;
143 memset(&tp, 0, sizeof(tp));
146 if (get_u32(&t->tcm_handle, handle, 0)) {
147 fprintf(stderr, "Illegal \"handle\"\n");
152 tail = NLMSG_TAIL(n);
153 addattr_l(n, 4096, TCA_OPTIONS, NULL, 0);
156 if (matches(*argv, "map") == 0) {
157 mode = FLOW_MODE_MAP;
158 } else if (matches(*argv, "hash") == 0) {
159 mode = FLOW_MODE_HASH;
160 } else if (matches(*argv, "keys") == 0) {
162 if (flow_parse_keys(&keys, &nkeys, *argv))
164 addattr32(n, 4096, TCA_FLOW_KEYS, keys);
165 } else if (matches(*argv, "and") == 0) {
167 if (get_u32(&tmp, *argv, 0)) {
168 fprintf(stderr, "Illegal \"mask\"\n");
171 transfer_bitop(&mask, &xor, tmp, 0);
172 } else if (matches(*argv, "or") == 0) {
174 if (get_u32(&tmp, *argv, 0)) {
175 fprintf(stderr, "Illegal \"or\"\n");
178 transfer_bitop(&mask, &xor, ~tmp, tmp);
179 } else if (matches(*argv, "xor") == 0) {
181 if (get_u32(&tmp, *argv, 0)) {
182 fprintf(stderr, "Illegal \"xor\"\n");
185 transfer_bitop(&mask, &xor, ~0, tmp);
186 } else if (matches(*argv, "rshift") == 0) {
188 if (get_u32(&tmp, *argv, 0)) {
189 fprintf(stderr, "Illegal \"rshift\"\n");
192 addattr32(n, 4096, TCA_FLOW_RSHIFT, tmp);
193 } else if (matches(*argv, "addend") == 0) {
195 if (get_addend(&tmp, *argv, keys)) {
196 fprintf(stderr, "Illegal \"addend\"\n");
199 addattr32(n, 4096, TCA_FLOW_ADDEND, tmp);
200 } else if (matches(*argv, "divisor") == 0) {
202 if (get_u32(&tmp, *argv, 0)) {
203 fprintf(stderr, "Illegal \"divisor\"\n");
206 addattr32(n, 4096, TCA_FLOW_DIVISOR, tmp);
207 } else if (matches(*argv, "baseclass") == 0) {
209 if (get_tc_classid(&tmp, *argv) || TC_H_MIN(tmp) == 0) {
210 fprintf(stderr, "Illegal \"baseclass\"\n");
213 addattr32(n, 4096, TCA_FLOW_BASECLASS, tmp);
214 } else if (matches(*argv, "perturb") == 0) {
216 if (get_u32(&tmp, *argv, 0)) {
217 fprintf(stderr, "Illegal \"perturb\"\n");
220 addattr32(n, 4096, TCA_FLOW_PERTURB, tmp);
221 } else if (matches(*argv, "police") == 0) {
223 if (parse_police(&argc, &argv, TCA_FLOW_POLICE, n)) {
224 fprintf(stderr, "Illegal \"police\"\n");
228 } else if (matches(*argv, "action") == 0) {
230 if (parse_action(&argc, &argv, TCA_FLOW_ACT, n)) {
231 fprintf(stderr, "Illegal \"action\"\n");
235 } else if (matches(*argv, "match") == 0) {
237 if (parse_ematch(&argc, &argv, TCA_FLOW_EMATCHES, n)) {
238 fprintf(stderr, "Illegal \"ematch\"\n");
242 } else if (matches(*argv, "help") == 0) {
246 fprintf(stderr, "What is \"%s\"?\n", *argv);
253 if (nkeys > 1 && mode != FLOW_MODE_HASH) {
254 fprintf(stderr, "Invalid mode \"map\" for multiple keys\n");
257 addattr32(n, 4096, TCA_FLOW_MODE, mode);
259 if (mask != ~0 || xor != 0) {
260 addattr32(n, 4096, TCA_FLOW_MASK, mask);
261 addattr32(n, 4096, TCA_FLOW_XOR, xor);
264 tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail;
268 static int flow_print_opt(struct filter_util *fu, FILE *f, struct rtattr *opt,
271 struct rtattr *tb[TCA_FLOW_MAX+1];
274 __u32 mask = ~0, val = 0;
279 parse_rtattr_nested(tb, TCA_FLOW_MAX, opt);
281 fprintf(f, "handle 0x%x ", handle);
283 if (tb[TCA_FLOW_MODE]) {
284 __u32 mode = *(__u32 *)RTA_DATA(tb[TCA_FLOW_MODE]);
296 if (tb[TCA_FLOW_KEYS]) {
297 __u32 keymask = *(__u32 *)RTA_DATA(tb[TCA_FLOW_KEYS]);
301 for (i = 0; i <= FLOW_KEY_MAX; i++) {
302 if (keymask & (1 << i)) {
303 fprintf(f, "%s%s", sep, flow_keys[i]);
310 if (tb[TCA_FLOW_MASK])
311 mask = *(__u32 *)RTA_DATA(tb[TCA_FLOW_MASK]);
312 if (tb[TCA_FLOW_XOR])
313 val = *(__u32 *)RTA_DATA(tb[TCA_FLOW_XOR]);
315 if (mask != ~0 || val != 0) {
316 __u32 or = (mask & val) ^ val;
317 __u32 xor = mask & val;
320 fprintf(f, "and 0x%.8x ", mask);
322 fprintf(f, "xor 0x%.8x ", xor);
324 fprintf(f, "or 0x%.8x ", or);
327 if (tb[TCA_FLOW_RSHIFT])
328 fprintf(f, "rshift %u ",
329 *(__u32 *)RTA_DATA(tb[TCA_FLOW_RSHIFT]));
330 if (tb[TCA_FLOW_ADDEND])
331 fprintf(f, "addend 0x%x ",
332 *(__u32 *)RTA_DATA(tb[TCA_FLOW_ADDEND]));
334 if (tb[TCA_FLOW_DIVISOR])
335 fprintf(f, "divisor %u ",
336 *(__u32 *)RTA_DATA(tb[TCA_FLOW_DIVISOR]));
337 if (tb[TCA_FLOW_BASECLASS])
338 fprintf(f, "baseclass %s ",
339 sprint_tc_classid(*(__u32 *)RTA_DATA(tb[TCA_FLOW_BASECLASS]), b1));
341 if (tb[TCA_FLOW_PERTURB])
342 fprintf(f, "perturb %usec ",
343 *(__u32 *)RTA_DATA(tb[TCA_FLOW_PERTURB]));
345 if (tb[TCA_FLOW_EMATCHES])
346 print_ematch(f, tb[TCA_FLOW_EMATCHES]);
347 if (tb[TCA_FLOW_POLICE])
348 tc_print_police(f, tb[TCA_FLOW_POLICE]);
349 if (tb[TCA_FLOW_ACT]) {
351 tc_print_action(f, tb[TCA_FLOW_ACT]);
356 struct filter_util flow_filter_util = {
358 .parse_fopt = flow_parse_opt,
359 .print_fopt = flow_print_opt,