| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* f_flow.c Flow filter |
| 4 |
* |
| 5 |
* Authors: Patrick McHardy <[email protected]> |
| 6 |
*/ |
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <unistd.h> |
| 10 |
#include <string.h> |
| 11 |
#include <errno.h> |
| 12 |
|
| 13 |
#include "utils.h" |
| 14 |
#include "tc_util.h" |
| 15 |
#include "m_ematch.h" |
| 16 |
|
| 17 |
static void explain(void) |
| 18 |
{ |
| 19 |
fprintf(stderr, |
| 20 |
"Usage: ... flow ...\n" |
| 21 |
"\n" |
| 22 |
" [mapping mode]: map key KEY [ OPS ] ...\n" |
| 23 |
" [hashing mode]: hash keys KEY-LIST ... [ perturb SECS ]\n" |
| 24 |
"\n" |
| 25 |
" [ divisor NUM ] [ baseclass ID ] [ match EMATCH_TREE ]\n" |
| 26 |
" [ action ACTION_SPEC ]\n" |
| 27 |
"\n" |
| 28 |
"KEY-LIST := [ KEY-LIST , ] KEY\n" |
| 29 |
"KEY := [ src | dst | proto | proto-src | proto-dst | iif | priority |\n" |
| 30 |
" mark | nfct | nfct-src | nfct-dst | nfct-proto-src |\n" |
| 31 |
" nfct-proto-dst | rt-classid | sk-uid | sk-gid |\n" |
| 32 |
" vlan-tag | rxhash ]\n" |
| 33 |
"OPS := [ or NUM | and NUM | xor NUM | rshift NUM | addend NUM ]\n" |
| 34 |
"ID := X:Y\n" |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
static const char *flow_keys[FLOW_KEY_MAX+1] = { |
| 39 |
[FLOW_KEY_SRC] = "src", |
| 40 |
[FLOW_KEY_DST] = "dst", |
| 41 |
[FLOW_KEY_PROTO] = "proto", |
| 42 |
[FLOW_KEY_PROTO_SRC] = "proto-src", |
| 43 |
[FLOW_KEY_PROTO_DST] = "proto-dst", |
| 44 |
[FLOW_KEY_IIF] = "iif", |
| 45 |
[FLOW_KEY_PRIORITY] = "priority", |
| 46 |
[FLOW_KEY_MARK] = "mark", |
| 47 |
[FLOW_KEY_NFCT] = "nfct", |
| 48 |
[FLOW_KEY_NFCT_SRC] = "nfct-src", |
| 49 |
[FLOW_KEY_NFCT_DST] = "nfct-dst", |
| 50 |
[FLOW_KEY_NFCT_PROTO_SRC] = "nfct-proto-src", |
| 51 |
[FLOW_KEY_NFCT_PROTO_DST] = "nfct-proto-dst", |
| 52 |
[FLOW_KEY_RTCLASSID] = "rt-classid", |
| 53 |
[FLOW_KEY_SKUID] = "sk-uid", |
| 54 |
[FLOW_KEY_SKGID] = "sk-gid", |
| 55 |
[FLOW_KEY_VLAN_TAG] = "vlan-tag", |
| 56 |
[FLOW_KEY_RXHASH] = "rxhash", |
| 57 |
}; |
| 58 |
|
| 59 |
static int flow_parse_keys(__u32 *keys, __u32 *nkeys, char *argv) |
| 60 |
{ |
| 61 |
char *s, *sep; |
| 62 |
unsigned int i; |
| 63 |
|
| 64 |
*keys = 0; |
| 65 |
*nkeys = 0; |
| 66 |
s = argv; |
| 67 |
while (s != NULL) { |
| 68 |
sep = strchr(s, ','); |
| 69 |
if (sep) |
| 70 |
*sep = '\0'; |
| 71 |
|
| 72 |
for (i = 0; i <= FLOW_KEY_MAX; i++) { |
| 73 |
if (matches(s, flow_keys[i]) == 0) { |
| 74 |
*keys |= 1 << i; |
| 75 |
(*nkeys)++; |
| 76 |
break; |
| 77 |
} |
| 78 |
} |
| 79 |
if (i > FLOW_KEY_MAX) { |
| 80 |
fprintf(stderr, "Unknown flow key \"%s\"\n", s); |
| 81 |
return -1; |
| 82 |
} |
| 83 |
s = sep ? sep + 1 : NULL; |
| 84 |
} |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
static void transfer_bitop(__u32 *mask, __u32 *xor, __u32 m, __u32 x) |
| 89 |
{ |
| 90 |
*xor = x ^ (*xor & m); |
| 91 |
*mask &= m; |
| 92 |
} |
| 93 |
|
| 94 |
static int get_addend(__u32 *addend, char *argv, __u32 keys) |
| 95 |
{ |
| 96 |
inet_prefix addr; |
| 97 |
int sign = 0; |
| 98 |
__u32 tmp; |
| 99 |
|
| 100 |
if (*argv == '-') { |
| 101 |
sign = 1; |
| 102 |
argv++; |
| 103 |
} |
| 104 |
|
| 105 |
if (get_u32(&tmp, argv, 0) == 0) |
| 106 |
goto out; |
| 107 |
|
| 108 |
if (keys & (FLOW_KEY_SRC | FLOW_KEY_DST | |
| 109 |
FLOW_KEY_NFCT_SRC | FLOW_KEY_NFCT_DST) && |
| 110 |
get_addr(&addr, argv, AF_UNSPEC) == 0) { |
| 111 |
switch (addr.family) { |
| 112 |
case AF_INET: |
| 113 |
tmp = ntohl(addr.data[0]); |
| 114 |
goto out; |
| 115 |
case AF_INET6: |
| 116 |
tmp = ntohl(addr.data[3]); |
| 117 |
goto out; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return -1; |
| 122 |
out: |
| 123 |
if (sign) |
| 124 |
tmp = -tmp; |
| 125 |
*addend = tmp; |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
static int flow_parse_opt(const struct filter_util *fu, char *handle, |
| 130 |
int argc, char **argv, struct nlmsghdr *n) |
| 131 |
{ |
| 132 |
struct tcmsg *t = NLMSG_DATA(n); |
| 133 |
struct rtattr *tail; |
| 134 |
__u32 mask = ~0U, xor = 0; |
| 135 |
__u32 keys = 0, nkeys = 0; |
| 136 |
__u32 mode = FLOW_MODE_MAP; |
| 137 |
__u32 tmp; |
| 138 |
|
| 139 |
if (handle) { |
| 140 |
if (get_u32(&t->tcm_handle, handle, 0)) { |
| 141 |
fprintf(stderr, "Illegal \"handle\"\n"); |
| 142 |
return -1; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
tail = addattr_nest(n, 4096, TCA_OPTIONS); |
| 147 |
|
| 148 |
while (argc > 0) { |
| 149 |
if (matches(*argv, "map") == 0) { |
| 150 |
mode = FLOW_MODE_MAP; |
| 151 |
} else if (matches(*argv, "hash") == 0) { |
| 152 |
mode = FLOW_MODE_HASH; |
| 153 |
} else if (matches(*argv, "keys") == 0) { |
| 154 |
NEXT_ARG(); |
| 155 |
if (flow_parse_keys(&keys, &nkeys, *argv)) |
| 156 |
return -1; |
| 157 |
addattr32(n, 4096, TCA_FLOW_KEYS, keys); |
| 158 |
} else if (matches(*argv, "and") == 0) { |
| 159 |
NEXT_ARG(); |
| 160 |
if (get_u32(&tmp, *argv, 0)) { |
| 161 |
fprintf(stderr, "Illegal \"mask\"\n"); |
| 162 |
return -1; |
| 163 |
} |
| 164 |
transfer_bitop(&mask, &xor, tmp, 0); |
| 165 |
} else if (matches(*argv, "or") == 0) { |
| 166 |
NEXT_ARG(); |
| 167 |
if (get_u32(&tmp, *argv, 0)) { |
| 168 |
fprintf(stderr, "Illegal \"or\"\n"); |
| 169 |
return -1; |
| 170 |
} |
| 171 |
transfer_bitop(&mask, &xor, ~tmp, tmp); |
| 172 |
} else if (matches(*argv, "xor") == 0) { |
| 173 |
NEXT_ARG(); |
| 174 |
if (get_u32(&tmp, *argv, 0)) { |
| 175 |
fprintf(stderr, "Illegal \"xor\"\n"); |
| 176 |
return -1; |
| 177 |
} |
| 178 |
transfer_bitop(&mask, &xor, ~0, tmp); |
| 179 |
} else if (matches(*argv, "rshift") == 0) { |
| 180 |
NEXT_ARG(); |
| 181 |
if (get_u32(&tmp, *argv, 0)) { |
| 182 |
fprintf(stderr, "Illegal \"rshift\"\n"); |
| 183 |
return -1; |
| 184 |
} |
| 185 |
addattr32(n, 4096, TCA_FLOW_RSHIFT, tmp); |
| 186 |
} else if (matches(*argv, "addend") == 0) { |
| 187 |
NEXT_ARG(); |
| 188 |
if (get_addend(&tmp, *argv, keys)) { |
| 189 |
fprintf(stderr, "Illegal \"addend\"\n"); |
| 190 |
return -1; |
| 191 |
} |
| 192 |
addattr32(n, 4096, TCA_FLOW_ADDEND, tmp); |
| 193 |
} else if (matches(*argv, "divisor") == 0) { |
| 194 |
NEXT_ARG(); |
| 195 |
if (get_u32(&tmp, *argv, 0)) { |
| 196 |
fprintf(stderr, "Illegal \"divisor\"\n"); |
| 197 |
return -1; |
| 198 |
} |
| 199 |
addattr32(n, 4096, TCA_FLOW_DIVISOR, tmp); |
| 200 |
} else if (matches(*argv, "baseclass") == 0) { |
| 201 |
NEXT_ARG(); |
| 202 |
if (get_tc_classid(&tmp, *argv) || TC_H_MIN(tmp) == 0) { |
| 203 |
fprintf(stderr, "Illegal \"baseclass\"\n"); |
| 204 |
return -1; |
| 205 |
} |
| 206 |
addattr32(n, 4096, TCA_FLOW_BASECLASS, tmp); |
| 207 |
} else if (matches(*argv, "perturb") == 0) { |
| 208 |
NEXT_ARG(); |
| 209 |
if (get_u32(&tmp, *argv, 0)) { |
| 210 |
fprintf(stderr, "Illegal \"perturb\"\n"); |
| 211 |
return -1; |
| 212 |
} |
| 213 |
addattr32(n, 4096, TCA_FLOW_PERTURB, tmp); |
| 214 |
} else if (matches(*argv, "police") == 0) { |
| 215 |
NEXT_ARG(); |
| 216 |
if (parse_police(&argc, &argv, TCA_FLOW_POLICE, n)) { |
| 217 |
fprintf(stderr, "Illegal \"police\"\n"); |
| 218 |
return -1; |
| 219 |
} |
| 220 |
continue; |
| 221 |
} else if (matches(*argv, "action") == 0) { |
| 222 |
NEXT_ARG(); |
| 223 |
if (parse_action(&argc, &argv, TCA_FLOW_ACT, n)) { |
| 224 |
fprintf(stderr, "Illegal \"action\"\n"); |
| 225 |
return -1; |
| 226 |
} |
| 227 |
continue; |
| 228 |
} else if (matches(*argv, "match") == 0) { |
| 229 |
NEXT_ARG(); |
| 230 |
if (parse_ematch(&argc, &argv, TCA_FLOW_EMATCHES, n)) { |
| 231 |
fprintf(stderr, "Illegal \"ematch\"\n"); |
| 232 |
return -1; |
| 233 |
} |
| 234 |
continue; |
| 235 |
} else if (matches(*argv, "help") == 0) { |
| 236 |
explain(); |
| 237 |
return -1; |
| 238 |
} else { |
| 239 |
fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 240 |
explain(); |
| 241 |
return -1; |
| 242 |
} |
| 243 |
argv++, argc--; |
| 244 |
} |
| 245 |
|
| 246 |
if (nkeys > 1 && mode != FLOW_MODE_HASH) { |
| 247 |
fprintf(stderr, "Invalid mode \"map\" for multiple keys\n"); |
| 248 |
return -1; |
| 249 |
} |
| 250 |
addattr32(n, 4096, TCA_FLOW_MODE, mode); |
| 251 |
|
| 252 |
if (mask != ~0 || xor != 0) { |
| 253 |
addattr32(n, 4096, TCA_FLOW_MASK, mask); |
| 254 |
addattr32(n, 4096, TCA_FLOW_XOR, xor); |
| 255 |
} |
| 256 |
|
| 257 |
addattr_nest_end(n, tail); |
| 258 |
return 0; |
| 259 |
} |
| 260 |
|
| 261 |
static const char *flow_mode2str(__u32 mode) |
| 262 |
{ |
| 263 |
static char buf[128]; |
| 264 |
|
| 265 |
switch (mode) { |
| 266 |
case FLOW_MODE_MAP: |
| 267 |
return "map"; |
| 268 |
case FLOW_MODE_HASH: |
| 269 |
return "hash"; |
| 270 |
default: |
| 271 |
snprintf(buf, sizeof(buf), "%#x", mode); |
| 272 |
return buf; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
static int flow_print_opt(const struct filter_util *fu, FILE *f, struct rtattr *opt, |
| 277 |
__u32 handle) |
| 278 |
{ |
| 279 |
struct rtattr *tb[TCA_FLOW_MAX+1]; |
| 280 |
|
| 281 |
SPRINT_BUF(b1); |
| 282 |
unsigned int i; |
| 283 |
__u32 mask = ~0, val = 0; |
| 284 |
|
| 285 |
if (opt == NULL) |
| 286 |
return -EINVAL; |
| 287 |
|
| 288 |
parse_rtattr_nested(tb, TCA_FLOW_MAX, opt); |
| 289 |
|
| 290 |
print_0xhex(PRINT_ANY, "handle", "handle %#llx ", handle); |
| 291 |
|
| 292 |
if (tb[TCA_FLOW_MODE]) { |
| 293 |
__u32 mode = rta_getattr_u32(tb[TCA_FLOW_MODE]); |
| 294 |
print_string(PRINT_ANY, "mode", "%s ", flow_mode2str(mode)); |
| 295 |
} |
| 296 |
|
| 297 |
if (tb[TCA_FLOW_KEYS]) { |
| 298 |
__u32 keymask = rta_getattr_u32(tb[TCA_FLOW_KEYS]); |
| 299 |
char *sep = " "; |
| 300 |
|
| 301 |
open_json_array(PRINT_ANY, "keys"); |
| 302 |
for (i = 0; i <= FLOW_KEY_MAX; i++) { |
| 303 |
if (keymask & (1 << i)) { |
| 304 |
print_string(PRINT_FP, NULL, "%s", sep); |
| 305 |
print_string(PRINT_ANY, NULL, "%s", flow_keys[i]); |
| 306 |
sep = ","; |
| 307 |
} |
| 308 |
} |
| 309 |
close_json_array(PRINT_ANY, " "); |
| 310 |
} |
| 311 |
|
| 312 |
if (tb[TCA_FLOW_MASK]) |
| 313 |
mask = rta_getattr_u32(tb[TCA_FLOW_MASK]); |
| 314 |
if (tb[TCA_FLOW_XOR]) |
| 315 |
val = rta_getattr_u32(tb[TCA_FLOW_XOR]); |
| 316 |
|
| 317 |
if (mask != ~0 || val != 0) { |
| 318 |
__u32 or = (mask & val) ^ val; |
| 319 |
__u32 xor = mask & val; |
| 320 |
|
| 321 |
if (mask != ~0) |
| 322 |
print_0xhex(PRINT_ANY, "and", "and 0x%.8x ", mask); |
| 323 |
if (xor != 0) |
| 324 |
print_0xhex(PRINT_ANY, "xor", "xor 0x%.8x ", xor); |
| 325 |
if (or != 0) |
| 326 |
print_0xhex(PRINT_ANY, "or", "or 0x%.8x ", or); |
| 327 |
} |
| 328 |
|
| 329 |
if (tb[TCA_FLOW_RSHIFT]) |
| 330 |
print_uint(PRINT_ANY, "rshift", "rshift %u ", |
| 331 |
rta_getattr_u32(tb[TCA_FLOW_RSHIFT])); |
| 332 |
if (tb[TCA_FLOW_ADDEND]) |
| 333 |
print_0xhex(PRINT_ANY, "addend", "addend 0x%x ", |
| 334 |
rta_getattr_u32(tb[TCA_FLOW_ADDEND])); |
| 335 |
|
| 336 |
if (tb[TCA_FLOW_DIVISOR]) |
| 337 |
print_uint(PRINT_ANY, "divisor", "divisor %u ", |
| 338 |
rta_getattr_u32(tb[TCA_FLOW_DIVISOR])); |
| 339 |
if (tb[TCA_FLOW_BASECLASS]) |
| 340 |
print_string(PRINT_ANY, "baseclass", "baseclass %s ", |
| 341 |
sprint_tc_classid(rta_getattr_u32(tb[TCA_FLOW_BASECLASS]), b1)); |
| 342 |
|
| 343 |
if (tb[TCA_FLOW_PERTURB]) |
| 344 |
print_uint(PRINT_ANY, "perturb", "perturb %usec ", |
| 345 |
rta_getattr_u32(tb[TCA_FLOW_PERTURB])); |
| 346 |
|
| 347 |
if (tb[TCA_FLOW_EMATCHES]) |
| 348 |
print_ematch(f, tb[TCA_FLOW_EMATCHES]); |
| 349 |
if (tb[TCA_FLOW_POLICE]) |
| 350 |
tc_print_police(tb[TCA_FLOW_POLICE]); |
| 351 |
if (tb[TCA_FLOW_ACT]) { |
| 352 |
print_nl(); |
| 353 |
tc_print_action(f, tb[TCA_FLOW_ACT], 0); |
| 354 |
} |
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
struct filter_util flow_filter_util = { |
| 359 |
.id = "flow", |
| 360 |
.parse_fopt = flow_parse_opt, |
| 361 |
.print_fopt = flow_print_opt, |
| 362 |
}; |