]>
Commit | Line | Data |
---|---|---|
d3e51122 JHS |
1 | /* |
2 | * m_ife.c IFE actions module | |
3 | * | |
4 | * This program is free software; you can distribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: J Hadi Salim (jhs@mojatatu.com) | |
10 | * | |
11 | */ | |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <unistd.h> | |
16 | #include <syslog.h> | |
17 | #include <fcntl.h> | |
18 | #include <sys/socket.h> | |
19 | #include <netinet/in.h> | |
20 | #include <arpa/inet.h> | |
21 | #include <string.h> | |
22 | #include <linux/netdevice.h> | |
23 | ||
24 | #include "rt_names.h" | |
25 | #include "utils.h" | |
26 | #include "tc_util.h" | |
27 | #include <linux/tc_act/tc_ife.h> | |
28 | ||
29 | static void ife_explain(void) | |
30 | { | |
31 | fprintf(stderr, | |
32 | "Usage:... ife {decode|encode} {ALLOW|USE} [dst DMAC] [src SMAC] [type TYPE] [CONTROL] [index INDEX]\n"); | |
33 | fprintf(stderr, | |
34 | "\tALLOW := Encode direction. Allows encoding specified metadata\n" | |
35 | "\t\t e.g \"allow mark\"\n" | |
36 | "\tUSE := Encode direction. Enforce Static encoding of specified metadata\n" | |
37 | "\t\t e.g \"use mark 0x12\"\n" | |
38 | "\tDMAC := 6 byte Destination MAC address to encode\n" | |
39 | "\tSMAC := optional 6 byte Source MAC address to encode\n" | |
40 | "\tTYPE := optional 16 bit ethertype to encode\n" | |
41 | "\tCONTROL := reclassify|pipe|drop|continue|ok\n" | |
42 | "\tINDEX := optional IFE table index value used\n"); | |
43 | fprintf(stderr, "encode is used for sending IFE packets\n"); | |
44 | fprintf(stderr, "decode is used for receiving IFE packets\n"); | |
45 | } | |
46 | ||
47 | static void ife_usage(void) | |
48 | { | |
49 | ife_explain(); | |
50 | exit(-1); | |
51 | } | |
52 | ||
53 | static int parse_ife(struct action_util *a, int *argc_p, char ***argv_p, | |
54 | int tca_id, struct nlmsghdr *n) | |
55 | { | |
56 | int argc = *argc_p; | |
57 | char **argv = *argv_p; | |
58 | int ok = 0; | |
59 | struct tc_ife p; | |
60 | struct rtattr *tail; | |
61 | struct rtattr *tail2; | |
62 | char dbuf[ETH_ALEN]; | |
63 | char sbuf[ETH_ALEN]; | |
64 | __u16 ife_type = 0; | |
65 | __u32 ife_prio = 0; | |
66 | __u32 ife_prio_v = 0; | |
67 | __u32 ife_mark = 0; | |
68 | __u32 ife_mark_v = 0; | |
69 | char *daddr = NULL; | |
70 | char *saddr = NULL; | |
71 | ||
72 | memset(&p, 0, sizeof(p)); | |
73 | p.action = TC_ACT_PIPE; /* good default */ | |
74 | ||
75 | if (argc <= 0) | |
76 | return -1; | |
77 | ||
78 | while (argc > 0) { | |
79 | if (matches(*argv, "ife") == 0) { | |
80 | NEXT_ARG(); | |
81 | continue; | |
82 | } else if (matches(*argv, "decode") == 0) { | |
83 | p.flags = IFE_DECODE; /* readability aid */ | |
84 | ok++; | |
85 | } else if (matches(*argv, "encode") == 0) { | |
86 | p.flags = IFE_ENCODE; | |
87 | ok++; | |
88 | } else if (matches(*argv, "allow") == 0) { | |
89 | NEXT_ARG(); | |
90 | if (matches(*argv, "mark") == 0) { | |
91 | ife_mark = IFE_META_SKBMARK; | |
92 | } else if (matches(*argv, "prio") == 0) { | |
93 | ife_prio = IFE_META_PRIO; | |
94 | } else { | |
95 | fprintf(stderr, "Illegal meta define <%s>\n", | |
96 | *argv); | |
97 | return -1; | |
98 | } | |
99 | } else if (matches(*argv, "use") == 0) { | |
100 | NEXT_ARG(); | |
101 | if (matches(*argv, "mark") == 0) { | |
102 | NEXT_ARG(); | |
103 | if (get_u32(&ife_mark_v, *argv, 0)) | |
104 | invarg("ife mark val is invalid", | |
105 | *argv); | |
106 | } else if (matches(*argv, "prio") == 0) { | |
107 | NEXT_ARG(); | |
108 | if (get_u32(&ife_prio_v, *argv, 0)) | |
109 | invarg("ife prio val is invalid", | |
110 | *argv); | |
111 | } else { | |
112 | fprintf(stderr, "Illegal meta use type <%s>\n", | |
113 | *argv); | |
114 | return -1; | |
115 | } | |
116 | } else if (matches(*argv, "type") == 0) { | |
117 | NEXT_ARG(); | |
118 | if (get_u16(&ife_type, *argv, 0)) | |
119 | invarg("ife type is invalid", *argv); | |
120 | fprintf(stderr, "IFE type 0x%x\n", ife_type); | |
121 | } else if (matches(*argv, "dst") == 0) { | |
122 | NEXT_ARG(); | |
123 | daddr = *argv; | |
124 | if (sscanf(daddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | |
125 | dbuf, dbuf + 1, dbuf + 2, | |
126 | dbuf + 3, dbuf + 4, dbuf + 5) != 6) { | |
127 | fprintf(stderr, "Invalid mac address %s\n", | |
128 | daddr); | |
129 | } | |
130 | fprintf(stderr, "dst MAC address <%s>\n", daddr); | |
131 | ||
132 | } else if (matches(*argv, "src") == 0) { | |
133 | NEXT_ARG(); | |
134 | saddr = *argv; | |
135 | if (sscanf(saddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | |
136 | sbuf, sbuf + 1, sbuf + 2, | |
137 | sbuf + 3, sbuf + 4, sbuf + 5) != 6) { | |
138 | fprintf(stderr, "Invalid mac address %s\n", | |
139 | saddr); | |
140 | } | |
141 | fprintf(stderr, "src MAC address <%s>\n", saddr); | |
142 | } else if (matches(*argv, "help") == 0) { | |
143 | ife_usage(); | |
144 | } else { | |
145 | break; | |
146 | } | |
147 | ||
148 | argc--; | |
149 | argv++; | |
150 | } | |
151 | ||
152 | if (argc) { | |
153 | if (matches(*argv, "reclassify") == 0) { | |
154 | p.action = TC_ACT_RECLASSIFY; | |
155 | argc--; | |
156 | argv++; | |
157 | } else if (matches(*argv, "pipe") == 0) { | |
158 | p.action = TC_ACT_PIPE; | |
159 | argc--; | |
160 | argv++; | |
161 | } else if (matches(*argv, "drop") == 0 || | |
162 | matches(*argv, "shot") == 0) { | |
163 | p.action = TC_ACT_SHOT; | |
164 | argc--; | |
165 | argv++; | |
166 | } else if (matches(*argv, "continue") == 0) { | |
167 | p.action = TC_ACT_UNSPEC; | |
168 | argc--; | |
169 | argv++; | |
170 | } else if (matches(*argv, "pass") == 0) { | |
171 | p.action = TC_ACT_OK; | |
172 | argc--; | |
173 | argv++; | |
174 | } | |
175 | } | |
176 | ||
177 | if (argc) { | |
178 | if (matches(*argv, "index") == 0) { | |
179 | NEXT_ARG(); | |
180 | if (get_u32(&p.index, *argv, 10)) { | |
181 | fprintf(stderr, "ife: Illegal \"index\"\n"); | |
182 | return -1; | |
183 | } | |
184 | argc--; | |
185 | argv++; | |
186 | } | |
187 | } | |
188 | ||
189 | if (!ok) { | |
190 | fprintf(stderr, "IFE requires decode/encode specified\n"); | |
191 | ife_usage(); | |
192 | } | |
193 | ||
194 | tail = NLMSG_TAIL(n); | |
195 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); | |
196 | addattr_l(n, MAX_MSG, TCA_IFE_PARMS, &p, sizeof(p)); | |
197 | ||
198 | if (!(p.flags & IFE_ENCODE)) | |
199 | goto skip_encode; | |
200 | ||
201 | if (daddr) | |
202 | addattr_l(n, MAX_MSG, TCA_IFE_DMAC, dbuf, ETH_ALEN); | |
203 | if (ife_type) | |
204 | addattr_l(n, MAX_MSG, TCA_IFE_TYPE, &ife_type, 2); | |
205 | if (saddr) | |
206 | addattr_l(n, MAX_MSG, TCA_IFE_SMAC, sbuf, ETH_ALEN); | |
207 | ||
208 | tail2 = NLMSG_TAIL(n); | |
209 | addattr_l(n, MAX_MSG, TCA_IFE_METALST, NULL, 0); | |
210 | if (ife_mark || ife_mark_v) { | |
211 | if (ife_mark_v) | |
212 | addattr_l(n, MAX_MSG, IFE_META_SKBMARK, &ife_mark_v, 4); | |
213 | else | |
214 | addattr_l(n, MAX_MSG, IFE_META_SKBMARK, NULL, 0); | |
215 | } | |
216 | if (ife_prio || ife_prio_v) { | |
217 | if (ife_prio_v) | |
218 | addattr_l(n, MAX_MSG, IFE_META_PRIO, &ife_prio_v, 4); | |
219 | else | |
220 | addattr_l(n, MAX_MSG, IFE_META_PRIO, NULL, 0); | |
221 | } | |
222 | ||
223 | tail2->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail2; | |
224 | ||
225 | skip_encode: | |
226 | tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail; | |
227 | ||
228 | *argc_p = argc; | |
229 | *argv_p = argv; | |
230 | return 0; | |
231 | } | |
232 | ||
233 | static int print_ife(struct action_util *au, FILE *f, struct rtattr *arg) | |
234 | { | |
235 | struct tc_ife *p = NULL; | |
236 | struct rtattr *tb[TCA_IFE_MAX + 1]; | |
237 | __u16 ife_type = 0; | |
238 | __u32 mmark = 0; | |
239 | __u32 mhash = 0; | |
240 | __u32 mprio = 0; | |
241 | int has_optional = 0; | |
242 | SPRINT_BUF(b1); | |
243 | SPRINT_BUF(b2); | |
244 | ||
245 | if (arg == NULL) | |
246 | return -1; | |
247 | ||
248 | parse_rtattr_nested(tb, TCA_IFE_MAX, arg); | |
249 | ||
250 | if (tb[TCA_IFE_PARMS] == NULL) { | |
251 | fprintf(f, "[NULL ife parameters]"); | |
252 | return -1; | |
253 | } | |
254 | p = RTA_DATA(tb[TCA_IFE_PARMS]); | |
255 | ||
256 | fprintf(f, "ife %s action %s ", | |
257 | (p->flags & IFE_ENCODE) ? "encode" : "decode", | |
258 | action_n2a(p->action, b1, sizeof(b1))); | |
259 | ||
260 | if (tb[TCA_IFE_TYPE]) { | |
261 | ife_type = rta_getattr_u16(tb[TCA_IFE_TYPE]); | |
262 | has_optional = 1; | |
263 | fprintf(f, "type 0x%X ", ife_type); | |
264 | } | |
265 | ||
266 | if (has_optional) | |
267 | fprintf(f, "\n\t "); | |
268 | ||
269 | if (tb[TCA_IFE_METALST]) { | |
270 | struct rtattr *metalist[IFE_META_MAX + 1]; | |
271 | int len = 0; | |
272 | ||
273 | parse_rtattr_nested(metalist, IFE_META_MAX, | |
274 | tb[TCA_IFE_METALST]); | |
275 | ||
276 | if (metalist[IFE_META_SKBMARK]) { | |
277 | len = RTA_PAYLOAD(metalist[IFE_META_SKBMARK]); | |
278 | if (len) { | |
279 | mmark = rta_getattr_u32(metalist[IFE_META_SKBMARK]); | |
280 | fprintf(f, "use mark %d ", mmark); | |
281 | } else | |
282 | fprintf(f, "allow mark "); | |
283 | } | |
284 | ||
285 | if (metalist[IFE_META_HASHID]) { | |
286 | len = RTA_PAYLOAD(metalist[IFE_META_HASHID]); | |
287 | if (len) { | |
288 | mhash = rta_getattr_u32(metalist[IFE_META_HASHID]); | |
289 | fprintf(f, "use hash %d ", mhash); | |
290 | } else | |
291 | fprintf(f, "allow hash "); | |
292 | } | |
293 | ||
294 | if (metalist[IFE_META_PRIO]) { | |
295 | len = RTA_PAYLOAD(metalist[IFE_META_PRIO]); | |
296 | if (len) { | |
297 | mprio = rta_getattr_u32(metalist[IFE_META_PRIO]); | |
298 | fprintf(f, "use prio %d ", mprio); | |
299 | } else | |
300 | fprintf(f, "allow prio "); | |
301 | } | |
302 | ||
303 | } | |
304 | ||
305 | if (tb[TCA_IFE_DMAC]) { | |
306 | has_optional = 1; | |
307 | fprintf(f, "dst %s ", | |
308 | ll_addr_n2a(RTA_DATA(tb[TCA_IFE_DMAC]), | |
309 | RTA_PAYLOAD(tb[TCA_IFE_DMAC]), 0, b2, | |
310 | sizeof(b2))); | |
311 | ||
312 | } | |
313 | ||
314 | if (tb[TCA_IFE_SMAC]) { | |
315 | has_optional = 1; | |
316 | fprintf(f, "src %s ", | |
317 | ll_addr_n2a(RTA_DATA(tb[TCA_IFE_SMAC]), | |
318 | RTA_PAYLOAD(tb[TCA_IFE_SMAC]), 0, b2, | |
319 | sizeof(b2))); | |
320 | } | |
321 | ||
322 | fprintf(f, "\n\t index %d ref %d bind %d", p->index, p->refcnt, | |
323 | p->bindcnt); | |
324 | if (show_stats) { | |
325 | if (tb[TCA_IFE_TM]) { | |
326 | struct tcf_t *tm = RTA_DATA(tb[TCA_IFE_TM]); | |
327 | ||
328 | print_tm(f, tm); | |
329 | } | |
330 | } | |
331 | ||
332 | fprintf(f, "\n"); | |
333 | ||
334 | return 0; | |
335 | } | |
336 | ||
337 | struct action_util ife_action_util = { | |
338 | .id = "ife", | |
339 | .parse_aopt = parse_ife, | |
340 | .print_aopt = print_ife, | |
341 | }; |