Debian Codegraph @

tc/f_fw.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * f_fw.c FW filter.
4 *
5 * Authors: Alexey Kuznetsov, <[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> /* IFNAMSIZ */
17 #include "utils.h"
18 #include "tc_util.h"
19
20 static void explain(void)
21 {
22 fprintf(stderr,
23 "Usage: ... fw [ classid CLASSID ] [ indev DEV ] [ action ACTION_SPEC ]\n"
24 " CLASSID := Push matching packets to the class identified by CLASSID with format X:Y\n"
25 " CLASSID is parsed as hexadecimal input.\n"
26 " DEV := specify device for incoming device classification.\n"
27 " ACTION_SPEC := Apply an action on matching packets.\n"
28 " NOTE: handle is represented as HANDLE[/FWMASK].\n"
29 " FWMASK is 0xffffffff by default.\n");
30 }
31
32 static int fw_parse_opt(const struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n)
33 {
34 struct tcmsg *t = NLMSG_DATA(n);
35 struct rtattr *tail;
36 __u32 mask = 0;
37 int mask_set = 0;
38
39 if (handle) {
40 char *slash;
41
42 if ((slash = strchr(handle, '/')) != NULL)
43 *slash = '\0';
44 if (get_u32(&t->tcm_handle, handle, 0)) {
45 fprintf(stderr, "Illegal \"handle\"\n");
46 return -1;
47 }
48 if (slash) {
49 if (get_u32(&mask, slash+1, 0)) {
50 fprintf(stderr, "Illegal \"handle\" mask\n");
51 return -1;
52 }
53 mask_set = 1;
54 }
55 }
56
57 if (argc == 0)
58 return 0;
59
60 tail = addattr_nest(n, 4096, TCA_OPTIONS);
61
62 if (mask_set)
63 addattr32(n, MAX_MSG, TCA_FW_MASK, mask);
64
65 while (argc > 0) {
66 if (matches(*argv, "classid") == 0 ||
67 matches(*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, 4096, TCA_FW_CLASSID, &classid, 4);
76 } else if (matches(*argv, "police") == 0) {
77 NEXT_ARG();
78 if (parse_police(&argc, &argv, TCA_FW_POLICE, n)) {
79 fprintf(stderr, "Illegal \"police\"\n");
80 return -1;
81 }
82 continue;
83 } else if (matches(*argv, "action") == 0) {
84 NEXT_ARG();
85 if (parse_action(&argc, &argv, TCA_FW_ACT, n)) {
86 fprintf(stderr, "Illegal fw \"action\"\n");
87 return -1;
88 }
89 continue;
90 } else if (strcmp(*argv, "indev") == 0) {
91 char d[IFNAMSIZ+1] = {};
92
93 argc--;
94 argv++;
95 if (argc < 1) {
96 fprintf(stderr, "Illegal indev\n");
97 return -1;
98 }
99 strncpy(d, *argv, sizeof(d) - 1);
100 addattr_l(n, MAX_MSG, TCA_FW_INDEV, d, strlen(d) + 1);
101 } else if (strcmp(*argv, "help") == 0) {
102 explain();
103 return -1;
104 } else {
105 fprintf(stderr, "What is \"%s\"?\n", *argv);
106 explain();
107 return -1;
108 }
109 argc--; argv++;
110 }
111 addattr_nest_end(n, tail);
112 return 0;
113 }
114
115 static int fw_print_opt(const struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
116 {
117 struct rtattr *tb[TCA_FW_MAX+1];
118
119 if (opt == NULL)
120 return 0;
121
122 parse_rtattr_nested(tb, TCA_FW_MAX, opt);
123
124 if (handle || tb[TCA_FW_MASK]) {
125 __u32 mark = 0, mask = 0;
126
127 open_json_object("fw");
128 if (handle)
129 mark = handle;
130 if (tb[TCA_FW_MASK] &&
131 (mask = rta_getattr_u32(tb[TCA_FW_MASK])) != 0xFFFFFFFF) {
132 print_0xhex(PRINT_ANY, "mark", "handle 0x%x", mark);
133 print_0xhex(PRINT_ANY, "mask", "/0x%x ", mask);
134 } else {
135 print_0xhex(PRINT_ANY, "mark", "handle 0x%x ", mark);
136 print_0xhex(PRINT_JSON, "mask", NULL, 0xFFFFFFFF);
137 }
138 close_json_object();
139 }
140
141 if (tb[TCA_FW_CLASSID]) {
142 SPRINT_BUF(b1);
143 print_string(PRINT_ANY, "classid", "classid %s ",
144 sprint_tc_classid(
145 rta_getattr_u32(tb[TCA_FW_CLASSID]), b1));
146 }
147
148 if (tb[TCA_FW_POLICE])
149 tc_print_police(tb[TCA_FW_POLICE]);
150 if (tb[TCA_FW_INDEV]) {
151 struct rtattr *idev = tb[TCA_FW_INDEV];
152
153 print_string(PRINT_ANY, "indev", "input dev %s ",
154 rta_getattr_str(idev));
155 }
156
157 if (tb[TCA_FW_ACT]) {
158 print_nl();
159 tc_print_action(f, tb[TCA_FW_ACT], 0);
160 }
161 return 0;
162 }
163
164 struct filter_util fw_filter_util = {
165 .id = "fw",
166 .parse_fopt = fw_parse_opt,
167 .print_fopt = fw_print_opt,
168 };