Debian Codegraph @

tc/f_route.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * f_route.c ROUTE 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
17 #include "utils.h"
18 #include "rt_names.h"
19 #include "tc_common.h"
20 #include "tc_util.h"
21
22 static void explain(void)
23 {
24 fprintf(stderr,
25 "Usage: ... route [ from REALM | fromif TAG ] [ to REALM ]\n"
26 " [ classid CLASSID ] [ action ACTION_SPEC ]\n"
27 " ACTION_SPEC := ... look at individual actions\n"
28 " CLASSID := X:Y\n"
29 "\n"
30 "NOTE: CLASSID is parsed as hexadecimal input.\n");
31 }
32
33 static int route_parse_opt(const struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n)
34 {
35 struct tcmsg *t = NLMSG_DATA(n);
36 struct rtattr *tail;
37 __u32 fh = 0xFFFF8000;
38 __u32 order = 0;
39
40 if (handle) {
41 if (get_u32(&t->tcm_handle, handle, 0)) {
42 fprintf(stderr, "Illegal \"handle\"\n");
43 return -1;
44 }
45 }
46
47 if (argc == 0)
48 return 0;
49
50 tail = addattr_nest(n, 4096, TCA_OPTIONS);
51
52 while (argc > 0) {
53 if (matches(*argv, "to") == 0) {
54 __u32 id;
55
56 NEXT_ARG();
57 if (rtnl_rtrealm_a2n(&id, *argv)) {
58 fprintf(stderr, "Illegal \"to\"\n");
59 return -1;
60 }
61 addattr_l(n, 4096, TCA_ROUTE4_TO, &id, 4);
62 fh &= ~0x80FF;
63 fh |= id&0xFF;
64 } else if (matches(*argv, "from") == 0) {
65 __u32 id;
66
67 NEXT_ARG();
68 if (rtnl_rtrealm_a2n(&id, *argv)) {
69 fprintf(stderr, "Illegal \"from\"\n");
70 return -1;
71 }
72 addattr_l(n, 4096, TCA_ROUTE4_FROM, &id, 4);
73 fh &= 0xFFFF;
74 fh |= id<<16;
75 } else if (matches(*argv, "fromif") == 0) {
76 __u32 id;
77
78 NEXT_ARG();
79 ll_init_map(&rth);
80 if ((id = ll_name_to_index(*argv)) <= 0) {
81 fprintf(stderr, "Illegal \"fromif\"\n");
82 return -1;
83 }
84 addattr_l(n, 4096, TCA_ROUTE4_IIF, &id, 4);
85 fh &= 0xFFFF;
86 fh |= (0x8000|id)<<16;
87 } else if (matches(*argv, "classid") == 0 ||
88 strcmp(*argv, "flowid") == 0) {
89 unsigned int classid;
90
91 NEXT_ARG();
92 if (get_tc_classid(&classid, *argv)) {
93 fprintf(stderr, "Illegal \"classid\"\n");
94 return -1;
95 }
96 addattr_l(n, 4096, TCA_ROUTE4_CLASSID, &classid, 4);
97 } else if (matches(*argv, "police") == 0) {
98 NEXT_ARG();
99 if (parse_police(&argc, &argv, TCA_ROUTE4_POLICE, n)) {
100 fprintf(stderr, "Illegal \"police\"\n");
101 return -1;
102 }
103 continue;
104 } else if (matches(*argv, "action") == 0) {
105 NEXT_ARG();
106 if (parse_action(&argc, &argv, TCA_ROUTE4_ACT, n)) {
107 fprintf(stderr, "Illegal \"action\"\n");
108 return -1;
109 }
110 continue;
111 } else if (matches(*argv, "order") == 0) {
112 NEXT_ARG();
113 if (get_u32(&order, *argv, 0)) {
114 fprintf(stderr, "Illegal \"order\"\n");
115 return -1;
116 }
117 } else if (strcmp(*argv, "help") == 0) {
118 explain();
119 return -1;
120 } else {
121 fprintf(stderr, "What is \"%s\"?\n", *argv);
122 explain();
123 return -1;
124 }
125 argc--; argv++;
126 }
127 addattr_nest_end(n, tail);
128 if (order) {
129 fh &= ~0x7F00;
130 fh |= (order<<8)&0x7F00;
131 }
132 if (!t->tcm_handle)
133 t->tcm_handle = fh;
134 return 0;
135 }
136
137 static int route_print_opt(const struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
138 {
139 struct rtattr *tb[TCA_ROUTE4_MAX+1];
140
141 SPRINT_BUF(b1);
142
143 if (opt == NULL)
144 return 0;
145
146 parse_rtattr_nested(tb, TCA_ROUTE4_MAX, opt);
147
148 if (handle)
149 print_0xhex(PRINT_ANY, "fh", "fh 0x%08x ", handle);
150 if (handle&0x7F00)
151 print_uint(PRINT_ANY, "order", "order %d ", (handle >> 8) & 0x7F);
152
153 if (tb[TCA_ROUTE4_CLASSID]) {
154 SPRINT_BUF(b1);
155 print_string(PRINT_ANY, "flowid", "flowid %s ",
156 sprint_tc_classid(rta_getattr_u32(tb[TCA_ROUTE4_CLASSID]), b1));
157 }
158 if (tb[TCA_ROUTE4_TO])
159 print_string(PRINT_ANY, "name", "to %s ",
160 rtnl_rtrealm_n2a(rta_getattr_u32(tb[TCA_ROUTE4_TO]), b1, sizeof(b1)));
161 if (tb[TCA_ROUTE4_FROM])
162 print_string(PRINT_ANY, "name", "from %s ",
163 rtnl_rtrealm_n2a(rta_getattr_u32(tb[TCA_ROUTE4_FROM]), b1, sizeof(b1)));
164 if (tb[TCA_ROUTE4_IIF])
165 print_color_string(PRINT_ANY, COLOR_IFNAME, "fromif", "fromif %s",
166 ll_index_to_name(rta_getattr_u32(tb[TCA_ROUTE4_IIF])));
167 if (tb[TCA_ROUTE4_POLICE])
168 tc_print_police(tb[TCA_ROUTE4_POLICE]);
169 if (tb[TCA_ROUTE4_ACT])
170 tc_print_action(f, tb[TCA_ROUTE4_ACT], 0);
171 return 0;
172 }
173
174 struct filter_util route_filter_util = {
175 .id = "route",
176 .parse_fopt = route_parse_opt,
177 .print_fopt = route_print_opt,
178 };