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