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
;
201 print_string(PRINT_ANY
, "kind", "%s ", "skbedit");
205 parse_rtattr_nested(tb
, TCA_SKBEDIT_MAX
, arg
);
207 if (tb
[TCA_SKBEDIT_PARMS
] == NULL
) {
208 fprintf(stderr
, "Missing skbedit parameters\n");
211 p
= RTA_DATA(tb
[TCA_SKBEDIT_PARMS
]);
213 if (tb
[TCA_SKBEDIT_QUEUE_MAPPING
] != NULL
) {
214 print_uint(PRINT_ANY
, "queue_mapping", "queue_mapping %u",
215 rta_getattr_u16(tb
[TCA_SKBEDIT_QUEUE_MAPPING
]));
217 if (tb
[TCA_SKBEDIT_PRIORITY
] != NULL
) {
218 priority
= rta_getattr_u32(tb
[TCA_SKBEDIT_PRIORITY
]);
219 print_string(PRINT_ANY
, "priority", " priority %s",
220 sprint_tc_classid(priority
, b1
));
222 if (tb
[TCA_SKBEDIT_MARK
] != NULL
) {
223 print_uint(PRINT_ANY
, "mark", " mark %u",
224 rta_getattr_u32(tb
[TCA_SKBEDIT_MARK
]));
226 if (tb
[TCA_SKBEDIT_MASK
]) {
227 print_hex(PRINT_ANY
, "mask", "/%#x",
228 rta_getattr_u32(tb
[TCA_SKBEDIT_MASK
]));
230 if (tb
[TCA_SKBEDIT_PTYPE
] != NULL
) {
231 ptype
= rta_getattr_u16(tb
[TCA_SKBEDIT_PTYPE
]);
232 if (ptype
== PACKET_HOST
)
233 print_string(PRINT_ANY
, "ptype", " ptype %s", "host");
234 else if (ptype
== PACKET_BROADCAST
)
235 print_string(PRINT_ANY
, "ptype", " ptype %s",
237 else if (ptype
== PACKET_MULTICAST
)
238 print_string(PRINT_ANY
, "ptype", " ptype %s",
240 else if (ptype
== PACKET_OTHERHOST
)
241 print_string(PRINT_ANY
, "ptype", " ptype %s",
244 print_uint(PRINT_ANY
, "ptype", " ptype %u", ptype
);
246 if (tb
[TCA_SKBEDIT_FLAGS
] != NULL
) {
247 __u64 flags
= rta_getattr_u64(tb
[TCA_SKBEDIT_FLAGS
]);
249 if (flags
& SKBEDIT_F_INHERITDSFIELD
)
250 print_null(PRINT_ANY
, "inheritdsfield", " %s",
254 print_action_control(f
, " ", p
->action
, "");
257 print_uint(PRINT_ANY
, "index", "\t index %u", p
->index
);
258 print_int(PRINT_ANY
, "ref", " ref %d", p
->refcnt
);
259 print_int(PRINT_ANY
, "bind", " bind %d", p
->bindcnt
);
262 if (tb
[TCA_SKBEDIT_TM
]) {
263 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_SKBEDIT_TM
]);
274 struct action_util skbedit_action_util
= {
276 .parse_aopt
= parse_skbedit
,
277 .print_aopt
= print_skbedit
,