]>
Commit | Line | Data |
---|---|---|
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 | * | |
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>. | |
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> | |
29 | #include <linux/if_packet.h> | |
30 | ||
31 | static void explain(void) | |
32 | { | |
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\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 | "note: inheritdsfield maps DS field to skb->priority\n"); | |
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 | { | |
58 | int argc = *argc_p; | |
59 | char **argv = *argv_p; | |
60 | int ok = 0; | |
61 | struct rtattr *tail; | |
62 | unsigned int tmp; | |
63 | __u16 queue_mapping, ptype; | |
64 | __u32 flags = 0, priority, mark; | |
65 | __u64 pure_flags = 0; | |
66 | struct tc_skbedit sel = { 0 }; | |
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++; | |
91 | } else if (matches(*argv, "mark") == 0) { | |
92 | flags |= SKBEDIT_F_MARK; | |
93 | NEXT_ARG(); | |
94 | if (get_u32(&mark, *argv, 0)) { | |
95 | fprintf(stderr, "Illegal mark\n"); | |
96 | return -1; | |
97 | } | |
98 | ok++; | |
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++; | |
117 | } else if (matches(*argv, "inheritdsfield") == 0) { | |
118 | pure_flags |= SKBEDIT_F_INHERITDSFIELD; | |
119 | ok++; | |
120 | } else if (matches(*argv, "help") == 0) { | |
121 | usage(); | |
122 | } else { | |
123 | break; | |
124 | } | |
125 | argc--; | |
126 | argv++; | |
127 | } | |
128 | ||
129 | parse_action_control_dflt(&argc, &argv, &sel.action, | |
130 | false, TC_ACT_PIPE); | |
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 | ||
151 | tail = addattr_nest(n, MAX_MSG, tca_id); | |
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)); | |
159 | if (flags & SKBEDIT_F_MARK) | |
160 | addattr_l(n, MAX_MSG, TCA_SKBEDIT_MARK, | |
161 | &mark, sizeof(mark)); | |
162 | if (flags & SKBEDIT_F_PTYPE) | |
163 | addattr_l(n, MAX_MSG, TCA_SKBEDIT_PTYPE, | |
164 | &ptype, sizeof(ptype)); | |
165 | if (pure_flags != 0) | |
166 | addattr64(n, MAX_MSG, TCA_SKBEDIT_FLAGS, pure_flags); | |
167 | addattr_nest_end(n, tail); | |
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 | { | |
176 | struct rtattr *tb[TCA_SKBEDIT_MAX + 1]; | |
177 | ||
178 | SPRINT_BUF(b1); | |
179 | __u32 priority; | |
180 | __u16 ptype; | |
181 | struct tc_skbedit *p = NULL; | |
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) { | |
189 | print_string(PRINT_FP, NULL, "%s", "[NULL skbedit parameters]"); | |
190 | return -1; | |
191 | } | |
192 | p = RTA_DATA(tb[TCA_SKBEDIT_PARMS]); | |
193 | ||
194 | print_string(PRINT_ANY, "kind", "%s ", "skbedit"); | |
195 | ||
196 | if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { | |
197 | print_uint(PRINT_ANY, "queue_mapping", "queue_mapping %u", | |
198 | rta_getattr_u16(tb[TCA_SKBEDIT_QUEUE_MAPPING])); | |
199 | } | |
200 | if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { | |
201 | priority = rta_getattr_u32(tb[TCA_SKBEDIT_PRIORITY]); | |
202 | print_string(PRINT_ANY, "priority", " priority %s", | |
203 | sprint_tc_classid(priority, b1)); | |
204 | } | |
205 | if (tb[TCA_SKBEDIT_MARK] != NULL) { | |
206 | print_uint(PRINT_ANY, "mark", " mark %u", | |
207 | rta_getattr_u32(tb[TCA_SKBEDIT_MARK])); | |
208 | } | |
209 | if (tb[TCA_SKBEDIT_PTYPE] != NULL) { | |
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"); | |
222 | else | |
223 | print_uint(PRINT_ANY, "ptype", " ptype %u", ptype); | |
224 | } | |
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 | } | |
232 | ||
233 | print_action_control(f, " ", p->action, ""); | |
234 | ||
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); | |
239 | ||
240 | if (show_stats) { | |
241 | if (tb[TCA_SKBEDIT_TM]) { | |
242 | struct tcf_t *tm = RTA_DATA(tb[TCA_SKBEDIT_TM]); | |
243 | ||
244 | print_tm(f, tm); | |
245 | } | |
246 | } | |
247 | ||
248 | print_string(PRINT_FP, NULL, "%s", _SL_); | |
249 | ||
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 | }; |