2 * m_skbedit.c SKB Editing module
4 * Copyright (c) 2008, Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses>.
18 * Authors: Alexander Duyck <alexander.h.duyck@intel.com>
28 #include <linux/tc_act/tc_skbedit.h>
29 #include <linux/if_packet.h>
31 static void explain(void)
33 fprintf(stderr, "Usage: ... skbedit <[QM] [PM] [MM] [PT] [IF]>\n"
34 "QM = queue_mapping QUEUE_MAPPING\n"
35 "PM = priority PRIORITY\n"
36 "MM = mark MARK[/MASK]\n"
37 "PT = ptype PACKETYPE\n"
38 "IF = inheritdsfield\n"
39 "PACKETYPE = is one of:\n"
40 " host, otherhost, broadcast, multicast\n"
41 "QUEUE_MAPPING = device transmit queue to use\n"
42 "PRIORITY = classID to assign to priority field\n"
43 "MARK = firewall mark to set\n"
44 "MASK = mask applied to firewall mark (0xffffffff by default)\n"
45 "note: inheritdsfield maps DS field to skb->priority\n");
56 parse_skbedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
60 char **argv = *argv_p;
64 __u16 queue_mapping, ptype;
65 __u32 flags = 0, priority, mark, mask;
67 struct tc_skbedit sel = { 0 };
69 if (matches(*argv, "skbedit") != 0)
75 if (matches(*argv, "queue_mapping") == 0) {
76 flags |= SKBEDIT_F_QUEUE_MAPPING;
78 if (get_unsigned(&tmp, *argv, 10) || tmp > 65535) {
79 fprintf(stderr, "Illegal queue_mapping\n");
84 } else if (matches(*argv, "priority") == 0) {
85 flags |= SKBEDIT_F_PRIORITY;
87 if (get_tc_classid(&priority, *argv)) {
88 fprintf(stderr, "Illegal priority\n");
92 } else if (matches(*argv, "mark") == 0) {
96 slash = strchr(*argv, '/');
100 flags |= SKBEDIT_F_MARK;
101 if (get_u32(&mark, *argv, 0)) {
102 fprintf(stderr, "Illegal mark\n");
107 if (get_u32(&mask, slash + 1, 0)) {
108 fprintf(stderr, "Illegal mask\n");
111 flags |= SKBEDIT_F_MASK;
114 } else if (matches(*argv, "ptype") == 0) {
117 if (matches(*argv, "host") == 0) {
119 } else if (matches(*argv, "broadcast") == 0) {
120 ptype = PACKET_BROADCAST;
121 } else if (matches(*argv, "multicast") == 0) {
122 ptype = PACKET_MULTICAST;
123 } else if (matches(*argv, "otherhost") == 0) {
124 ptype = PACKET_OTHERHOST;
126 fprintf(stderr, "Illegal ptype (%s)\n",
130 flags |= SKBEDIT_F_PTYPE;
132 } else if (matches(*argv, "inheritdsfield") == 0) {
133 pure_flags |= SKBEDIT_F_INHERITDSFIELD;
135 } else if (matches(*argv, "help") == 0) {
144 parse_action_control_dflt(&argc, &argv, &sel.action,
148 if (matches(*argv, "index") == 0) {
150 if (get_u32(&sel.index, *argv, 10)) {
151 fprintf(stderr, "skbedit: Illegal \"index\"\n");
166 tail = addattr_nest(n, MAX_MSG, tca_id);
167 addattr_l(n, MAX_MSG, TCA_SKBEDIT_PARMS, &sel, sizeof(sel));
168 if (flags & SKBEDIT_F_QUEUE_MAPPING)
169 addattr_l(n, MAX_MSG, TCA_SKBEDIT_QUEUE_MAPPING,
170 &queue_mapping, sizeof(queue_mapping));
171 if (flags & SKBEDIT_F_PRIORITY)
172 addattr_l(n, MAX_MSG, TCA_SKBEDIT_PRIORITY,
173 &priority, sizeof(priority));
174 if (flags & SKBEDIT_F_MARK)
175 addattr_l(n, MAX_MSG, TCA_SKBEDIT_MARK,
176 &mark, sizeof(mark));
177 if (flags & SKBEDIT_F_MASK)
178 addattr_l(n, MAX_MSG, TCA_SKBEDIT_MASK,
179 &mask, sizeof(mask));
180 if (flags & SKBEDIT_F_PTYPE)
181 addattr_l(n, MAX_MSG, TCA_SKBEDIT_PTYPE,
182 &ptype, sizeof(ptype));
184 addattr64(n, MAX_MSG, TCA_SKBEDIT_FLAGS, pure_flags);
185 addattr_nest_end(n, tail);
192 static int print_skbedit(struct action_util *au, FILE *f, struct rtattr *arg)
194 struct rtattr *tb[TCA_SKBEDIT_MAX + 1];
199 struct tc_skbedit *p;
204 parse_rtattr_nested(tb, TCA_SKBEDIT_MAX, arg);
206 if (tb[TCA_SKBEDIT_PARMS] == NULL) {
207 fprintf(stderr, "Missing skbedit parameters\n");
210 p = RTA_DATA(tb[TCA_SKBEDIT_PARMS]);
212 print_string(PRINT_ANY, "kind", "%s ", "skbedit");
214 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
215 print_uint(PRINT_ANY, "queue_mapping", "queue_mapping %u",
216 rta_getattr_u16(tb[TCA_SKBEDIT_QUEUE_MAPPING]));
218 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
219 priority = rta_getattr_u32(tb[TCA_SKBEDIT_PRIORITY]);
220 print_string(PRINT_ANY, "priority", " priority %s",
221 sprint_tc_classid(priority, b1));
223 if (tb[TCA_SKBEDIT_MARK] != NULL) {
224 print_uint(PRINT_ANY, "mark", " mark %u",
225 rta_getattr_u32(tb[TCA_SKBEDIT_MARK]));
227 if (tb[TCA_SKBEDIT_MASK]) {
228 print_hex(PRINT_ANY, "mask", "/%#x",
229 rta_getattr_u32(tb[TCA_SKBEDIT_MASK]));
231 if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
232 ptype = rta_getattr_u16(tb[TCA_SKBEDIT_PTYPE]);
233 if (ptype == PACKET_HOST)
234 print_string(PRINT_ANY, "ptype", " ptype %s", "host");
235 else if (ptype == PACKET_BROADCAST)
236 print_string(PRINT_ANY, "ptype", " ptype %s",
238 else if (ptype == PACKET_MULTICAST)
239 print_string(PRINT_ANY, "ptype", " ptype %s",
241 else if (ptype == PACKET_OTHERHOST)
242 print_string(PRINT_ANY, "ptype", " ptype %s",
245 print_uint(PRINT_ANY, "ptype", " ptype %u", ptype);
247 if (tb[TCA_SKBEDIT_FLAGS] != NULL) {
248 __u64 flags = rta_getattr_u64(tb[TCA_SKBEDIT_FLAGS]);
250 if (flags & SKBEDIT_F_INHERITDSFIELD)
251 print_null(PRINT_ANY, "inheritdsfield", " %s",
255 print_action_control(f, " ", p->action, "");
257 print_string(PRINT_FP, NULL, "%s", _SL_);
258 print_uint(PRINT_ANY, "index", "\t index %u", p->index);
259 print_int(PRINT_ANY, "ref", " ref %d", p->refcnt);
260 print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt);
263 if (tb[TCA_SKBEDIT_TM]) {
264 struct tcf_t *tm = RTA_DATA(tb[TCA_SKBEDIT_TM]);
270 print_string(PRINT_FP, NULL, "%s", _SL_);
275 struct action_util skbedit_action_util = {
277 .parse_aopt = parse_skbedit,
278 .print_aopt = print_skbedit,