| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* f_basic.c Basic Classifier |
| 4 |
* |
| 5 |
* Authors: Thomas Graf <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <sys/socket.h> |
| 13 |
#include <netinet/in.h> |
| 14 |
#include <arpa/inet.h> |
| 15 |
#include <string.h> |
| 16 |
#include <linux/if.h> |
| 17 |
|
| 18 |
#include "utils.h" |
| 19 |
#include "tc_util.h" |
| 20 |
#include "m_ematch.h" |
| 21 |
|
| 22 |
static void explain(void) |
| 23 |
{ |
| 24 |
fprintf(stderr, |
| 25 |
"Usage: ... basic [ match EMATCH_TREE ]\n" |
| 26 |
" [ action ACTION_SPEC ] [ classid CLASSID ]\n" |
| 27 |
"\n" |
| 28 |
"Where: SELECTOR := SAMPLE SAMPLE ...\n" |
| 29 |
" FILTERID := X:Y:Z\n" |
| 30 |
" ACTION_SPEC := ... look at individual actions\n" |
| 31 |
"\n" |
| 32 |
"NOTE: CLASSID is parsed as hexadecimal input.\n"); |
| 33 |
} |
| 34 |
|
| 35 |
static int basic_parse_opt(const struct filter_util *qu, char *handle, |
| 36 |
int argc, char **argv, struct nlmsghdr *n) |
| 37 |
{ |
| 38 |
struct tcmsg *t = NLMSG_DATA(n); |
| 39 |
struct rtattr *tail; |
| 40 |
long h = 0; |
| 41 |
|
| 42 |
if (handle) { |
| 43 |
h = strtol(handle, NULL, 0); |
| 44 |
if (h == LONG_MIN || h == LONG_MAX) { |
| 45 |
fprintf(stderr, "Illegal handle \"%s\", must be numeric.\n", |
| 46 |
handle); |
| 47 |
return -1; |
| 48 |
} |
| 49 |
} |
| 50 |
t->tcm_handle = h; |
| 51 |
|
| 52 |
if (argc == 0) |
| 53 |
return 0; |
| 54 |
|
| 55 |
tail = (struct rtattr *)(((void *)n)+NLMSG_ALIGN(n->nlmsg_len)); |
| 56 |
addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0); |
| 57 |
|
| 58 |
while (argc > 0) { |
| 59 |
if (matches(*argv, "match") == 0) { |
| 60 |
NEXT_ARG(); |
| 61 |
if (parse_ematch(&argc, &argv, TCA_BASIC_EMATCHES, n)) { |
| 62 |
fprintf(stderr, "Illegal \"ematch\"\n"); |
| 63 |
return -1; |
| 64 |
} |
| 65 |
continue; |
| 66 |
} else if (matches(*argv, "classid") == 0 || |
| 67 |
strcmp(*argv, "flowid") == 0) { |
| 68 |
unsigned int classid; |
| 69 |
|
| 70 |
NEXT_ARG(); |
| 71 |
if (get_tc_classid(&classid, *argv)) { |
| 72 |
fprintf(stderr, "Illegal \"classid\"\n"); |
| 73 |
return -1; |
| 74 |
} |
| 75 |
addattr_l(n, MAX_MSG, TCA_BASIC_CLASSID, &classid, 4); |
| 76 |
} else if (matches(*argv, "action") == 0) { |
| 77 |
NEXT_ARG(); |
| 78 |
if (parse_action(&argc, &argv, TCA_BASIC_ACT, n)) { |
| 79 |
fprintf(stderr, "Illegal \"action\"\n"); |
| 80 |
return -1; |
| 81 |
} |
| 82 |
continue; |
| 83 |
|
| 84 |
} else if (matches(*argv, "police") == 0) { |
| 85 |
NEXT_ARG(); |
| 86 |
if (parse_police(&argc, &argv, TCA_BASIC_POLICE, n)) { |
| 87 |
fprintf(stderr, "Illegal \"police\"\n"); |
| 88 |
return -1; |
| 89 |
} |
| 90 |
continue; |
| 91 |
} else if (strcmp(*argv, "help") == 0) { |
| 92 |
explain(); |
| 93 |
return -1; |
| 94 |
} else { |
| 95 |
fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 96 |
explain(); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
argc--; argv++; |
| 100 |
} |
| 101 |
|
| 102 |
tail->rta_len = (((void *)n)+n->nlmsg_len) - (void *)tail; |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
static int basic_print_opt(const struct filter_util *qu, FILE *f, |
| 107 |
struct rtattr *opt, __u32 handle) |
| 108 |
{ |
| 109 |
struct rtattr *tb[TCA_BASIC_MAX+1]; |
| 110 |
|
| 111 |
if (opt == NULL) |
| 112 |
return 0; |
| 113 |
|
| 114 |
parse_rtattr_nested(tb, TCA_BASIC_MAX, opt); |
| 115 |
|
| 116 |
if (handle) |
| 117 |
print_hex(PRINT_ANY, "handle", |
| 118 |
"handle 0x%x ", handle); |
| 119 |
|
| 120 |
if (tb[TCA_BASIC_CLASSID]) { |
| 121 |
uint32_t classid = rta_getattr_u32(tb[TCA_BASIC_CLASSID]); |
| 122 |
SPRINT_BUF(b1); |
| 123 |
|
| 124 |
print_string(PRINT_ANY, "flowid", "flowid %s ", |
| 125 |
sprint_tc_classid(classid, b1)); |
| 126 |
} |
| 127 |
|
| 128 |
if (tb[TCA_BASIC_EMATCHES]) |
| 129 |
print_ematch(f, tb[TCA_BASIC_EMATCHES]); |
| 130 |
|
| 131 |
if (tb[TCA_BASIC_POLICE]) { |
| 132 |
print_nl(); |
| 133 |
tc_print_police(tb[TCA_BASIC_POLICE]); |
| 134 |
} |
| 135 |
|
| 136 |
if (tb[TCA_BASIC_ACT]) { |
| 137 |
tc_print_action(f, tb[TCA_BASIC_ACT], 0); |
| 138 |
} |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
struct filter_util basic_filter_util = { |
| 144 |
.id = "basic", |
| 145 |
.parse_fopt = basic_parse_opt, |
| 146 |
.print_fopt = basic_print_opt, |
| 147 |
}; |