Debian Codegraph @

tc/m_skbedit.c

raw
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * m_skbedit.c SKB Editing module
4 *
5 * Copyright (c) 2008, Intel Corporation.
6 *
7 * Authors: Alexander Duyck <[email protected]>
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include "utils.h"
15 #include "tc_util.h"
16 #include <linux/tc_act/tc_skbedit.h>
17 #include <linux/if_packet.h>
18
19 static void explain(void)
20 {
21 fprintf(stderr, "Usage: ... skbedit <[QM] [PM] [MM] [PT] [IF]>\n"
22 "QM = queue_mapping QUEUE_MAPPING\n"
23 "PM = priority PRIORITY\n"
24 "MM = mark MARK[/MASK]\n"
25 "PT = ptype PACKETYPE\n"
26 "IF = inheritdsfield\n"
27 "PACKETYPE = is one of:\n"
28 " host, otherhost, broadcast, multicast\n"
29 "QUEUE_MAPPING = device transmit queue to use\n"
30 "PRIORITY = classID to assign to priority field\n"
31 "MARK = firewall mark to set\n"
32 "MASK = mask applied to firewall mark (0xffffffff by default)\n"
33 "note: inheritdsfield maps DS field to skb->priority\n");
34 }
35
36 static void
37 usage(void)
38 {
39 explain();
40 exit(-1);
41 }
42
43 static int
44 parse_skbedit(const struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
45 struct nlmsghdr *n)
46 {
47 int argc = *argc_p;
48 char **argv = *argv_p;
49 int ok = 0;
50 struct rtattr *tail;
51 unsigned int tmp;
52 __u16 queue_mapping, ptype;
53 __u32 flags = 0, priority, mark, mask;
54 __u64 pure_flags = 0;
55 struct tc_skbedit sel = { 0 };
56
57 if (matches(*argv, "skbedit") != 0)
58 return -1;
59
60 NEXT_ARG();
61
62 while (argc > 0) {
63 if (matches(*argv, "queue_mapping") == 0) {
64 flags |= SKBEDIT_F_QUEUE_MAPPING;
65 NEXT_ARG();
66 if (get_unsigned(&tmp, *argv, 10) || tmp > 65535) {
67 fprintf(stderr, "Illegal queue_mapping\n");
68 return -1;
69 }
70 queue_mapping = tmp;
71 ok++;
72 } else if (matches(*argv, "priority") == 0) {
73 flags |= SKBEDIT_F_PRIORITY;
74 NEXT_ARG();
75 if (get_tc_classid(&priority, *argv)) {
76 fprintf(stderr, "Illegal priority\n");
77 return -1;
78 }
79 ok++;
80 } else if (matches(*argv, "mark") == 0) {
81 char *slash;
82
83 NEXT_ARG();
84 slash = strchr(*argv, '/');
85 if (slash)
86 *slash = '\0';
87
88 flags |= SKBEDIT_F_MARK;
89 if (get_u32(&mark, *argv, 0)) {
90 fprintf(stderr, "Illegal mark\n");
91 return -1;
92 }
93
94 if (slash) {
95 if (get_u32(&mask, slash + 1, 0)) {
96 fprintf(stderr, "Illegal mask\n");
97 return -1;
98 }
99 flags |= SKBEDIT_F_MASK;
100 }
101 ok++;
102 } else if (matches(*argv, "ptype") == 0) {
103
104 NEXT_ARG();
105 if (matches(*argv, "host") == 0) {
106 ptype = PACKET_HOST;
107 } else if (matches(*argv, "broadcast") == 0) {
108 ptype = PACKET_BROADCAST;
109 } else if (matches(*argv, "multicast") == 0) {
110 ptype = PACKET_MULTICAST;
111 } else if (matches(*argv, "otherhost") == 0) {
112 ptype = PACKET_OTHERHOST;
113 } else {
114 fprintf(stderr, "Illegal ptype (%s)\n",
115 *argv);
116 return -1;
117 }
118 flags |= SKBEDIT_F_PTYPE;
119 ok++;
120 } else if (matches(*argv, "inheritdsfield") == 0) {
121 pure_flags |= SKBEDIT_F_INHERITDSFIELD;
122 ok++;
123 } else if (matches(*argv, "help") == 0) {
124 usage();
125 } else {
126 break;
127 }
128 argc--;
129 argv++;
130 }
131
132 parse_action_control_dflt(&argc, &argv, &sel.action,
133 false, TC_ACT_PIPE);
134
135 if (argc) {
136 if (matches(*argv, "index") == 0) {
137 NEXT_ARG();
138 if (get_u32(&sel.index, *argv, 10)) {
139 fprintf(stderr, "skbedit: Illegal \"index\"\n");
140 return -1;
141 }
142 argc--;
143 argv++;
144 ok++;
145 }
146 }
147
148 if (!ok) {
149 explain();
150 return -1;
151 }
152
153
154 tail = addattr_nest(n, MAX_MSG, tca_id);
155 addattr_l(n, MAX_MSG, TCA_SKBEDIT_PARMS, &sel, sizeof(sel));
156 if (flags & SKBEDIT_F_QUEUE_MAPPING)
157 addattr_l(n, MAX_MSG, TCA_SKBEDIT_QUEUE_MAPPING,
158 &queue_mapping, sizeof(queue_mapping));
159 if (flags & SKBEDIT_F_PRIORITY)
160 addattr_l(n, MAX_MSG, TCA_SKBEDIT_PRIORITY,
161 &priority, sizeof(priority));
162 if (flags & SKBEDIT_F_MARK)
163 addattr_l(n, MAX_MSG, TCA_SKBEDIT_MARK,
164 &mark, sizeof(mark));
165 if (flags & SKBEDIT_F_MASK)
166 addattr_l(n, MAX_MSG, TCA_SKBEDIT_MASK,
167 &mask, sizeof(mask));
168 if (flags & SKBEDIT_F_PTYPE)
169 addattr_l(n, MAX_MSG, TCA_SKBEDIT_PTYPE,
170 &ptype, sizeof(ptype));
171 if (pure_flags != 0)
172 addattr64(n, MAX_MSG, TCA_SKBEDIT_FLAGS, pure_flags);
173 addattr_nest_end(n, tail);
174
175 *argc_p = argc;
176 *argv_p = argv;
177 return 0;
178 }
179
180 static int print_skbedit(const struct action_util *au, FILE *f, struct rtattr *arg)
181 {
182 struct rtattr *tb[TCA_SKBEDIT_MAX + 1];
183
184 SPRINT_BUF(b1);
185 __u32 priority;
186 __u16 ptype;
187 struct tc_skbedit *p;
188
189 print_string(PRINT_ANY, "kind", "%s ", "skbedit");
190 if (arg == NULL)
191 return 0;
192
193 parse_rtattr_nested(tb, TCA_SKBEDIT_MAX, arg);
194
195 if (tb[TCA_SKBEDIT_PARMS] == NULL) {
196 fprintf(stderr, "Missing skbedit parameters\n");
197 return -1;
198 }
199 p = RTA_DATA(tb[TCA_SKBEDIT_PARMS]);
200
201 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
202 print_uint(PRINT_ANY, "queue_mapping", "queue_mapping %u",
203 rta_getattr_u16(tb[TCA_SKBEDIT_QUEUE_MAPPING]));
204 }
205 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
206 priority = rta_getattr_u32(tb[TCA_SKBEDIT_PRIORITY]);
207 print_string(PRINT_ANY, "priority", " priority %s",
208 sprint_tc_classid(priority, b1));
209 }
210 if (tb[TCA_SKBEDIT_MARK] != NULL) {
211 print_uint(PRINT_ANY, "mark", " mark %u",
212 rta_getattr_u32(tb[TCA_SKBEDIT_MARK]));
213 }
214 if (tb[TCA_SKBEDIT_MASK]) {
215 print_hex(PRINT_ANY, "mask", "/%#x",
216 rta_getattr_u32(tb[TCA_SKBEDIT_MASK]));
217 }
218 if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
219 ptype = rta_getattr_u16(tb[TCA_SKBEDIT_PTYPE]);
220 if (ptype == PACKET_HOST)
221 print_string(PRINT_ANY, "ptype", " ptype %s", "host");
222 else if (ptype == PACKET_BROADCAST)
223 print_string(PRINT_ANY, "ptype", " ptype %s",
224 "broadcast");
225 else if (ptype == PACKET_MULTICAST)
226 print_string(PRINT_ANY, "ptype", " ptype %s",
227 "multicast");
228 else if (ptype == PACKET_OTHERHOST)
229 print_string(PRINT_ANY, "ptype", " ptype %s",
230 "otherhost");
231 else
232 print_uint(PRINT_ANY, "ptype", " ptype %u", ptype);
233 }
234 if (tb[TCA_SKBEDIT_FLAGS] != NULL) {
235 __u64 flags = rta_getattr_u64(tb[TCA_SKBEDIT_FLAGS]);
236
237 if (flags & SKBEDIT_F_INHERITDSFIELD)
238 print_null(PRINT_ANY, "inheritdsfield", " %s",
239 "inheritdsfield");
240 }
241
242 print_action_control(" ", p->action, "");
243
244 print_nl();
245 print_uint(PRINT_ANY, "index", "\t index %u", p->index);
246 print_int(PRINT_ANY, "ref", " ref %d", p->refcnt);
247 print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt);
248
249 if (show_stats) {
250 if (tb[TCA_SKBEDIT_TM]) {
251 struct tcf_t *tm = RTA_DATA(tb[TCA_SKBEDIT_TM]);
252
253 print_tm(tm);
254 }
255 }
256
257 print_nl();
258
259 return 0;
260 }
261
262 struct action_util skbedit_action_util = {
263 .id = "skbedit",
264 .parse_aopt = parse_skbedit,
265 .print_aopt = print_skbedit,
266 };