]>
Commit | Line | Data |
---|---|---|
30eb304e JP |
1 | /* |
2 | * f_flower.c Flower Classifier | |
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: Jiri Pirko <jiri@resnulli.us> | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <unistd.h> | |
30eb304e JP |
15 | #include <string.h> |
16 | #include <net/if.h> | |
28747146 | 17 | #include <linux/limits.h> |
f888f4e2 | 18 | #include <linux/if_arp.h> |
30eb304e JP |
19 | #include <linux/if_ether.h> |
20 | #include <linux/ip.h> | |
745d9172 | 21 | #include <linux/tc_act/tc_vlan.h> |
7638ee13 | 22 | #include <linux/mpls.h> |
30eb304e JP |
23 | |
24 | #include "utils.h" | |
25 | #include "tc_util.h" | |
26 | #include "rt_names.h" | |
27 | ||
08f66c80 PB |
28 | enum flower_matching_flags { |
29 | FLOWER_IP_FLAGS, | |
30 | }; | |
31 | ||
6910d656 SH |
32 | enum flower_endpoint { |
33 | FLOWER_ENDPOINT_SRC, | |
34 | FLOWER_ENDPOINT_DST | |
35 | }; | |
36 | ||
eb3b5696 SH |
37 | enum flower_icmp_field { |
38 | FLOWER_ICMP_FIELD_TYPE, | |
39 | FLOWER_ICMP_FIELD_CODE | |
40 | }; | |
41 | ||
30eb304e JP |
42 | static void explain(void) |
43 | { | |
512caeb2 | 44 | fprintf(stderr, |
8589eb4e MC |
45 | "Usage: ... flower [ MATCH-LIST ] [ verbose ]\n" |
46 | " [ skip_sw | skip_hw ]\n" | |
47 | " [ action ACTION-SPEC ] [ classid CLASSID ]\n" | |
512caeb2 SH |
48 | "\n" |
49 | "Where: MATCH-LIST := [ MATCH-LIST ] MATCH\n" | |
8589eb4e MC |
50 | " MATCH := { indev DEV-NAME |\n" |
51 | " vlan_id VID |\n" | |
52 | " vlan_prio PRIORITY |\n" | |
53 | " vlan_ethtype [ ipv4 | ipv6 | ETH-TYPE ] |\n" | |
54 | " cvlan_id VID |\n" | |
55 | " cvlan_prio PRIORITY |\n" | |
56 | " cvlan_ethtype [ ipv4 | ipv6 | ETH-TYPE ] |\n" | |
57 | " dst_mac MASKED-LLADDR |\n" | |
58 | " src_mac MASKED-LLADDR |\n" | |
59 | " ip_proto [tcp | udp | sctp | icmp | icmpv6 | IP-PROTO ] |\n" | |
60 | " ip_tos MASKED-IP_TOS |\n" | |
61 | " ip_ttl MASKED-IP_TTL |\n" | |
62 | " mpls_label LABEL |\n" | |
63 | " mpls_tc TC |\n" | |
64 | " mpls_bos BOS |\n" | |
65 | " mpls_ttl TTL |\n" | |
66 | " dst_ip PREFIX |\n" | |
67 | " src_ip PREFIX |\n" | |
68 | " dst_port PORT-NUMBER |\n" | |
69 | " src_port PORT-NUMBER |\n" | |
70 | " tcp_flags MASKED-TCP_FLAGS |\n" | |
71 | " type MASKED-ICMP-TYPE |\n" | |
72 | " code MASKED-ICMP-CODE |\n" | |
73 | " arp_tip IPV4-PREFIX |\n" | |
74 | " arp_sip IPV4-PREFIX |\n" | |
75 | " arp_op [ request | reply | OP ] |\n" | |
76 | " arp_tha MASKED-LLADDR |\n" | |
77 | " arp_sha MASKED-LLADDR |\n" | |
78 | " enc_dst_ip [ IPV4-ADDR | IPV6-ADDR ] |\n" | |
79 | " enc_src_ip [ IPV4-ADDR | IPV6-ADDR ] |\n" | |
80 | " enc_key_id [ KEY-ID ] |\n" | |
81 | " enc_tos MASKED-IP_TOS |\n" | |
82 | " enc_ttl MASKED-IP_TTL |\n" | |
83 | " geneve_opts MASKED-OPTIONS |\n" | |
84 | " ip_flags IP-FLAGS | \n" | |
2fffb1c0 PB |
85 | " enc_dst_port [ port_number ] |\n" |
86 | " ct_state MASKED_CT_STATE |\n" | |
87 | " ct_label MASKED_CT_LABEL |\n" | |
88 | " ct_mark MASKED_CT_MARK |\n" | |
89 | " ct_zone MASKED_CT_ZONE }\n" | |
8589eb4e MC |
90 | " FILTERID := X:Y:Z\n" |
91 | " MASKED_LLADDR := { LLADDR | LLADDR/MASK | LLADDR/BITS }\n" | |
2fffb1c0 | 92 | " MASKED_CT_STATE := combination of {+|-} and flags trk,est,new\n" |
8589eb4e | 93 | " ACTION-SPEC := ... look at individual actions\n" |
512caeb2 | 94 | "\n" |
8589eb4e MC |
95 | "NOTE: CLASSID, IP-PROTO are parsed as hexadecimal input.\n" |
96 | "NOTE: There can be only used one mask per one prio. If user needs\n" | |
97 | " to specify different mask, he has to use different prio.\n"); | |
30eb304e JP |
98 | } |
99 | ||
100 | static int flower_parse_eth_addr(char *str, int addr_type, int mask_type, | |
101 | struct nlmsghdr *n) | |
102 | { | |
c2078f8d SH |
103 | int ret, err = -1; |
104 | char addr[ETH_ALEN], *slash; | |
105 | ||
106 | slash = strchr(str, '/'); | |
107 | if (slash) | |
108 | *slash = '\0'; | |
30eb304e JP |
109 | |
110 | ret = ll_addr_a2n(addr, sizeof(addr), str); | |
111 | if (ret < 0) | |
c2078f8d | 112 | goto err; |
30eb304e | 113 | addattr_l(n, MAX_MSG, addr_type, addr, sizeof(addr)); |
c2078f8d SH |
114 | |
115 | if (slash) { | |
116 | unsigned bits; | |
117 | ||
118 | if (!get_unsigned(&bits, slash + 1, 10)) { | |
119 | uint64_t mask; | |
120 | ||
121 | /* Extra 16 bit shift to push mac address into | |
122 | * high bits of uint64_t | |
123 | */ | |
124 | mask = htonll(0xffffffffffffULL << (16 + 48 - bits)); | |
125 | memcpy(addr, &mask, ETH_ALEN); | |
126 | } else { | |
127 | ret = ll_addr_a2n(addr, sizeof(addr), slash + 1); | |
128 | if (ret < 0) | |
129 | goto err; | |
130 | } | |
131 | } else { | |
132 | memset(addr, 0xff, ETH_ALEN); | |
133 | } | |
30eb304e | 134 | addattr_l(n, MAX_MSG, mask_type, addr, sizeof(addr)); |
c2078f8d SH |
135 | |
136 | err = 0; | |
137 | err: | |
138 | if (slash) | |
139 | *slash = '/'; | |
140 | return err; | |
30eb304e JP |
141 | } |
142 | ||
1f0a5dfd JL |
143 | static bool eth_type_vlan(__be16 ethertype) |
144 | { | |
145 | return ethertype == htons(ETH_P_8021Q) || | |
146 | ethertype == htons(ETH_P_8021AD); | |
147 | } | |
148 | ||
745d9172 | 149 | static int flower_parse_vlan_eth_type(char *str, __be16 eth_type, int type, |
512caeb2 SH |
150 | __be16 *p_vlan_eth_type, |
151 | struct nlmsghdr *n) | |
745d9172 HHZ |
152 | { |
153 | __be16 vlan_eth_type; | |
154 | ||
1f0a5dfd JL |
155 | if (!eth_type_vlan(eth_type)) { |
156 | fprintf(stderr, "Can't set \"%s\" if ethertype isn't 802.1Q or 802.1AD\n", | |
157 | type == TCA_FLOWER_KEY_VLAN_ETH_TYPE ? "vlan_ethtype" : "cvlan_ethtype"); | |
745d9172 HHZ |
158 | return -1; |
159 | } | |
160 | ||
161 | if (ll_proto_a2n(&vlan_eth_type, str)) | |
162 | invarg("invalid vlan_ethtype", str); | |
163 | addattr16(n, MAX_MSG, type, vlan_eth_type); | |
164 | *p_vlan_eth_type = vlan_eth_type; | |
165 | return 0; | |
166 | } | |
167 | ||
08f66c80 PB |
168 | struct flag_to_string { |
169 | int flag; | |
170 | enum flower_matching_flags type; | |
171 | char *string; | |
172 | }; | |
22a8f019 | 173 | |
08f66c80 PB |
174 | static struct flag_to_string flags_str[] = { |
175 | { TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOWER_IP_FLAGS, "frag" }, | |
fb4e6abf | 176 | { TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, FLOWER_IP_FLAGS, "firstfrag" }, |
08f66c80 | 177 | }; |
22a8f019 | 178 | |
08f66c80 PB |
179 | static int flower_parse_matching_flags(char *str, |
180 | enum flower_matching_flags type, | |
181 | __u32 *mtf, __u32 *mtf_mask) | |
182 | { | |
183 | char *token; | |
184 | bool no; | |
185 | bool found; | |
186 | int i; | |
22a8f019 | 187 | |
08f66c80 PB |
188 | token = strtok(str, "/"); |
189 | ||
190 | while (token) { | |
191 | if (!strncmp(token, "no", 2)) { | |
192 | no = true; | |
193 | token += 2; | |
194 | } else | |
195 | no = false; | |
196 | ||
197 | found = false; | |
198 | for (i = 0; i < ARRAY_SIZE(flags_str); i++) { | |
199 | if (type != flags_str[i].type) | |
200 | continue; | |
201 | ||
202 | if (!strcmp(token, flags_str[i].string)) { | |
203 | if (no) | |
204 | *mtf &= ~flags_str[i].flag; | |
205 | else | |
206 | *mtf |= flags_str[i].flag; | |
207 | ||
208 | *mtf_mask |= flags_str[i].flag; | |
209 | found = true; | |
210 | break; | |
211 | } | |
212 | } | |
213 | if (!found) | |
22a8f019 | 214 | return -1; |
08f66c80 PB |
215 | |
216 | token = strtok(NULL, "/"); | |
22a8f019 PB |
217 | } |
218 | ||
22a8f019 PB |
219 | return 0; |
220 | } | |
221 | ||
2fffb1c0 | 222 | static int flower_parse_u16(char *str, int value_type, int mask_type, |
b20dcd0b | 223 | struct nlmsghdr *n, bool be) |
2fffb1c0 PB |
224 | { |
225 | __u16 value, mask; | |
226 | char *slash; | |
227 | ||
228 | slash = strchr(str, '/'); | |
229 | if (slash) | |
230 | *slash = '\0'; | |
231 | ||
232 | if (get_u16(&value, str, 0)) | |
233 | return -1; | |
234 | ||
235 | if (slash) { | |
236 | if (get_u16(&mask, slash + 1, 0)) | |
237 | return -1; | |
238 | } else { | |
239 | mask = UINT16_MAX; | |
240 | } | |
241 | ||
b20dcd0b EB |
242 | if (be) { |
243 | value = htons(value); | |
244 | mask = htons(mask); | |
245 | } | |
2fffb1c0 PB |
246 | addattr16(n, MAX_MSG, value_type, value); |
247 | addattr16(n, MAX_MSG, mask_type, mask); | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | static int flower_parse_u32(char *str, int value_type, int mask_type, | |
253 | struct nlmsghdr *n) | |
254 | { | |
255 | __u32 value, mask; | |
256 | char *slash; | |
257 | ||
258 | slash = strchr(str, '/'); | |
259 | if (slash) | |
260 | *slash = '\0'; | |
261 | ||
262 | if (get_u32(&value, str, 0)) | |
263 | return -1; | |
264 | ||
265 | if (slash) { | |
266 | if (get_u32(&mask, slash + 1, 0)) | |
267 | return -1; | |
268 | } else { | |
269 | mask = UINT32_MAX; | |
270 | } | |
271 | ||
272 | addattr32(n, MAX_MSG, value_type, value); | |
273 | addattr32(n, MAX_MSG, mask_type, mask); | |
274 | ||
275 | return 0; | |
276 | } | |
277 | ||
278 | static int flower_parse_ct_mark(char *str, struct nlmsghdr *n) | |
279 | { | |
280 | return flower_parse_u32(str, | |
281 | TCA_FLOWER_KEY_CT_MARK, | |
282 | TCA_FLOWER_KEY_CT_MARK_MASK, | |
283 | n); | |
284 | } | |
285 | ||
286 | static int flower_parse_ct_zone(char *str, struct nlmsghdr *n) | |
287 | { | |
288 | return flower_parse_u16(str, | |
289 | TCA_FLOWER_KEY_CT_ZONE, | |
290 | TCA_FLOWER_KEY_CT_ZONE_MASK, | |
b20dcd0b EB |
291 | n, |
292 | false); | |
2fffb1c0 PB |
293 | } |
294 | ||
295 | static int flower_parse_ct_labels(char *str, struct nlmsghdr *n) | |
296 | { | |
297 | #define LABELS_SIZE 16 | |
298 | uint8_t labels[LABELS_SIZE], lmask[LABELS_SIZE]; | |
299 | char *slash, *mask = NULL; | |
300 | size_t slen, slen_mask = 0; | |
301 | ||
302 | slash = index(str, '/'); | |
303 | if (slash) { | |
304 | *slash = 0; | |
305 | mask = slash + 1; | |
306 | slen_mask = strlen(mask); | |
307 | } | |
308 | ||
309 | slen = strlen(str); | |
310 | if (slen > LABELS_SIZE * 2 || slen_mask > LABELS_SIZE * 2) { | |
311 | char errmsg[128]; | |
312 | ||
313 | snprintf(errmsg, sizeof(errmsg), | |
314 | "%zd Max allowed size %d", | |
315 | slen, LABELS_SIZE*2); | |
316 | invarg(errmsg, str); | |
317 | } | |
318 | ||
319 | if (hex2mem(str, labels, slen / 2) < 0) | |
320 | invarg("labels must be a hex string\n", str); | |
321 | addattr_l(n, MAX_MSG, TCA_FLOWER_KEY_CT_LABELS, labels, slen / 2); | |
322 | ||
323 | if (mask) { | |
324 | if (hex2mem(mask, lmask, slen_mask / 2) < 0) | |
325 | invarg("labels mask must be a hex string\n", mask); | |
326 | } else { | |
327 | memset(lmask, 0xff, sizeof(lmask)); | |
328 | slen_mask = sizeof(lmask) * 2; | |
329 | } | |
330 | addattr_l(n, MAX_MSG, TCA_FLOWER_KEY_CT_LABELS_MASK, lmask, | |
331 | slen_mask / 2); | |
332 | ||
333 | return 0; | |
334 | } | |
335 | ||
336 | static struct flower_ct_states { | |
337 | char *str; | |
338 | int flag; | |
339 | } flower_ct_states[] = { | |
340 | { "trk", TCA_FLOWER_KEY_CT_FLAGS_TRACKED }, | |
341 | { "new", TCA_FLOWER_KEY_CT_FLAGS_NEW }, | |
342 | { "est", TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED }, | |
343 | }; | |
344 | ||
345 | static int flower_parse_ct_state(char *str, struct nlmsghdr *n) | |
346 | { | |
347 | int flags = 0, mask = 0, len, i; | |
348 | bool p; | |
349 | ||
350 | while (*str != '\0') { | |
351 | if (*str == '+') | |
352 | p = true; | |
353 | else if (*str == '-') | |
354 | p = false; | |
355 | else | |
356 | return -1; | |
357 | ||
358 | for (i = 0; i < ARRAY_SIZE(flower_ct_states); i++) { | |
359 | len = strlen(flower_ct_states[i].str); | |
360 | if (strncmp(str + 1, flower_ct_states[i].str, len)) | |
361 | continue; | |
362 | ||
363 | if (p) | |
364 | flags |= flower_ct_states[i].flag; | |
365 | mask |= flower_ct_states[i].flag; | |
366 | break; | |
367 | } | |
368 | ||
369 | if (i == ARRAY_SIZE(flower_ct_states)) | |
370 | return -1; | |
371 | ||
372 | str += len + 1; | |
373 | } | |
374 | ||
375 | addattr16(n, MAX_MSG, TCA_FLOWER_KEY_CT_STATE, flags); | |
376 | addattr16(n, MAX_MSG, TCA_FLOWER_KEY_CT_STATE_MASK, mask); | |
377 | return 0; | |
378 | } | |
379 | ||
30eb304e JP |
380 | static int flower_parse_ip_proto(char *str, __be16 eth_type, int type, |
381 | __u8 *p_ip_proto, struct nlmsghdr *n) | |
382 | { | |
383 | int ret; | |
384 | __u8 ip_proto; | |
385 | ||
eb3b5696 SH |
386 | if (eth_type != htons(ETH_P_IP) && eth_type != htons(ETH_P_IPV6)) |
387 | goto err; | |
388 | ||
30eb304e JP |
389 | if (matches(str, "tcp") == 0) { |
390 | ip_proto = IPPROTO_TCP; | |
391 | } else if (matches(str, "udp") == 0) { | |
392 | ip_proto = IPPROTO_UDP; | |
a1fb0d48 SH |
393 | } else if (matches(str, "sctp") == 0) { |
394 | ip_proto = IPPROTO_SCTP; | |
eb3b5696 SH |
395 | } else if (matches(str, "icmp") == 0) { |
396 | if (eth_type != htons(ETH_P_IP)) | |
397 | goto err; | |
398 | ip_proto = IPPROTO_ICMP; | |
399 | } else if (matches(str, "icmpv6") == 0) { | |
400 | if (eth_type != htons(ETH_P_IPV6)) | |
401 | goto err; | |
402 | ip_proto = IPPROTO_ICMPV6; | |
30eb304e JP |
403 | } else { |
404 | ret = get_u8(&ip_proto, str, 16); | |
405 | if (ret) | |
406 | return -1; | |
407 | } | |
408 | addattr8(n, MAX_MSG, type, ip_proto); | |
409 | *p_ip_proto = ip_proto; | |
410 | return 0; | |
eb3b5696 SH |
411 | |
412 | err: | |
413 | fprintf(stderr, "Illegal \"eth_type\" for ip proto\n"); | |
414 | return -1; | |
30eb304e JP |
415 | } |
416 | ||
f888f4e2 SH |
417 | static int __flower_parse_ip_addr(char *str, int family, |
418 | int addr4_type, int mask4_type, | |
419 | int addr6_type, int mask6_type, | |
420 | struct nlmsghdr *n) | |
30eb304e JP |
421 | { |
422 | int ret; | |
423 | inet_prefix addr; | |
30eb304e JP |
424 | int bits; |
425 | int i; | |
426 | ||
30eb304e JP |
427 | ret = get_prefix(&addr, str, family); |
428 | if (ret) | |
429 | return -1; | |
430 | ||
bb9b63b1 AV |
431 | if (family && (addr.family != family)) { |
432 | fprintf(stderr, "Illegal \"eth_type\" for ip address\n"); | |
30eb304e | 433 | return -1; |
bb9b63b1 | 434 | } |
30eb304e JP |
435 | |
436 | addattr_l(n, MAX_MSG, addr.family == AF_INET ? addr4_type : addr6_type, | |
437 | addr.data, addr.bytelen); | |
438 | ||
439 | memset(addr.data, 0xff, addr.bytelen); | |
440 | bits = addr.bitlen; | |
441 | for (i = 0; i < addr.bytelen / 4; i++) { | |
442 | if (!bits) { | |
443 | addr.data[i] = 0; | |
444 | } else if (bits / 32 >= 1) { | |
445 | bits -= 32; | |
446 | } else { | |
447 | addr.data[i] <<= 32 - bits; | |
448 | addr.data[i] = htonl(addr.data[i]); | |
449 | bits = 0; | |
450 | } | |
451 | } | |
452 | ||
453 | addattr_l(n, MAX_MSG, addr.family == AF_INET ? mask4_type : mask6_type, | |
454 | addr.data, addr.bytelen); | |
455 | ||
456 | return 0; | |
457 | } | |
458 | ||
f888f4e2 SH |
459 | static int flower_parse_ip_addr(char *str, __be16 eth_type, |
460 | int addr4_type, int mask4_type, | |
461 | int addr6_type, int mask6_type, | |
462 | struct nlmsghdr *n) | |
463 | { | |
464 | int family; | |
465 | ||
466 | if (eth_type == htons(ETH_P_IP)) { | |
467 | family = AF_INET; | |
468 | } else if (eth_type == htons(ETH_P_IPV6)) { | |
469 | family = AF_INET6; | |
470 | } else if (!eth_type) { | |
471 | family = AF_UNSPEC; | |
472 | } else { | |
473 | return -1; | |
474 | } | |
475 | ||
164a9ff4 RD |
476 | return __flower_parse_ip_addr(str, family, addr4_type, mask4_type, |
477 | addr6_type, mask6_type, n); | |
f888f4e2 SH |
478 | } |
479 | ||
480 | static bool flower_eth_type_arp(__be16 eth_type) | |
481 | { | |
482 | return eth_type == htons(ETH_P_ARP) || eth_type == htons(ETH_P_RARP); | |
483 | } | |
484 | ||
485 | static int flower_parse_arp_ip_addr(char *str, __be16 eth_type, | |
486 | int addr_type, int mask_type, | |
487 | struct nlmsghdr *n) | |
488 | { | |
489 | if (!flower_eth_type_arp(eth_type)) | |
490 | return -1; | |
491 | ||
492 | return __flower_parse_ip_addr(str, AF_INET, addr_type, mask_type, | |
493 | TCA_FLOWER_UNSPEC, TCA_FLOWER_UNSPEC, n); | |
494 | } | |
495 | ||
180136e5 SH |
496 | static int flower_parse_u8(char *str, int value_type, int mask_type, |
497 | int (*value_from_name)(const char *str, | |
498 | __u8 *value), | |
499 | bool (*value_validate)(__u8 value), | |
500 | struct nlmsghdr *n) | |
f888f4e2 SH |
501 | { |
502 | char *slash; | |
503 | int ret, err = -1; | |
180136e5 | 504 | __u8 value, mask; |
f888f4e2 SH |
505 | |
506 | slash = strchr(str, '/'); | |
507 | if (slash) | |
508 | *slash = '\0'; | |
509 | ||
180136e5 SH |
510 | ret = value_from_name ? value_from_name(str, &value) : -1; |
511 | if (ret < 0) { | |
f888f4e2 SH |
512 | ret = get_u8(&value, str, 10); |
513 | if (ret) | |
514 | goto err; | |
f888f4e2 SH |
515 | } |
516 | ||
180136e5 SH |
517 | if (value_validate && !value_validate(value)) |
518 | goto err; | |
519 | ||
f888f4e2 SH |
520 | if (slash) { |
521 | ret = get_u8(&mask, slash + 1, 10); | |
522 | if (ret) | |
523 | goto err; | |
524 | } | |
525 | else { | |
526 | mask = UINT8_MAX; | |
527 | } | |
528 | ||
180136e5 | 529 | addattr8(n, MAX_MSG, value_type, value); |
f888f4e2 SH |
530 | addattr8(n, MAX_MSG, mask_type, mask); |
531 | ||
532 | err = 0; | |
533 | err: | |
534 | if (slash) | |
535 | *slash = '/'; | |
536 | return err; | |
537 | } | |
538 | ||
9d36e54f SH |
539 | static const char *flower_print_arp_op_to_name(__u8 op) |
540 | { | |
541 | switch (op) { | |
542 | case ARPOP_REQUEST: | |
543 | return "request"; | |
544 | case ARPOP_REPLY: | |
545 | return "reply"; | |
546 | default: | |
547 | return NULL; | |
548 | } | |
549 | } | |
550 | ||
180136e5 SH |
551 | static int flower_arp_op_from_name(const char *name, __u8 *op) |
552 | { | |
553 | if (!strcmp(name, "request")) | |
554 | *op = ARPOP_REQUEST; | |
555 | else if (!strcmp(name, "reply")) | |
556 | *op = ARPOP_REPLY; | |
557 | else | |
558 | return -1; | |
559 | ||
560 | return 0; | |
561 | } | |
562 | ||
563 | static bool flow_arp_op_validate(__u8 op) | |
564 | { | |
565 | return !op || op == ARPOP_REQUEST || op == ARPOP_REPLY; | |
566 | } | |
567 | ||
568 | static int flower_parse_arp_op(char *str, __be16 eth_type, | |
569 | int op_type, int mask_type, | |
570 | struct nlmsghdr *n) | |
571 | { | |
572 | if (!flower_eth_type_arp(eth_type)) | |
573 | return -1; | |
574 | ||
575 | return flower_parse_u8(str, op_type, mask_type, flower_arp_op_from_name, | |
576 | flow_arp_op_validate, n); | |
577 | } | |
578 | ||
eb3b5696 SH |
579 | static int flower_icmp_attr_type(__be16 eth_type, __u8 ip_proto, |
580 | enum flower_icmp_field field) | |
581 | { | |
582 | if (eth_type == htons(ETH_P_IP) && ip_proto == IPPROTO_ICMP) | |
583 | return field == FLOWER_ICMP_FIELD_CODE ? | |
584 | TCA_FLOWER_KEY_ICMPV4_CODE : | |
585 | TCA_FLOWER_KEY_ICMPV4_TYPE; | |
586 | else if (eth_type == htons(ETH_P_IPV6) && ip_proto == IPPROTO_ICMPV6) | |
587 | return field == FLOWER_ICMP_FIELD_CODE ? | |
588 | TCA_FLOWER_KEY_ICMPV6_CODE : | |
589 | TCA_FLOWER_KEY_ICMPV6_TYPE; | |
590 | ||
591 | return -1; | |
592 | } | |
593 | ||
6374961a SH |
594 | static int flower_icmp_attr_mask_type(__be16 eth_type, __u8 ip_proto, |
595 | enum flower_icmp_field field) | |
596 | { | |
597 | if (eth_type == htons(ETH_P_IP) && ip_proto == IPPROTO_ICMP) | |
598 | return field == FLOWER_ICMP_FIELD_CODE ? | |
599 | TCA_FLOWER_KEY_ICMPV4_CODE_MASK : | |
600 | TCA_FLOWER_KEY_ICMPV4_TYPE_MASK; | |
601 | else if (eth_type == htons(ETH_P_IPV6) && ip_proto == IPPROTO_ICMPV6) | |
602 | return field == FLOWER_ICMP_FIELD_CODE ? | |
603 | TCA_FLOWER_KEY_ICMPV6_CODE_MASK : | |
604 | TCA_FLOWER_KEY_ICMPV6_TYPE_MASK; | |
605 | ||
606 | return -1; | |
607 | } | |
608 | ||
eb3b5696 SH |
609 | static int flower_parse_icmp(char *str, __u16 eth_type, __u8 ip_proto, |
610 | enum flower_icmp_field field, struct nlmsghdr *n) | |
611 | { | |
6374961a | 612 | int value_type, mask_type; |
eb3b5696 | 613 | |
6374961a SH |
614 | value_type = flower_icmp_attr_type(eth_type, ip_proto, field); |
615 | mask_type = flower_icmp_attr_mask_type(eth_type, ip_proto, field); | |
616 | if (value_type < 0 || mask_type < 0) | |
eb3b5696 SH |
617 | return -1; |
618 | ||
6374961a | 619 | return flower_parse_u8(str, value_type, mask_type, NULL, NULL, n); |
eb3b5696 SH |
620 | } |
621 | ||
6910d656 | 622 | static int flower_port_attr_type(__u8 ip_proto, enum flower_endpoint endpoint) |
30eb304e | 623 | { |
6bd5b80c | 624 | if (ip_proto == IPPROTO_TCP) |
6910d656 SH |
625 | return endpoint == FLOWER_ENDPOINT_SRC ? |
626 | TCA_FLOWER_KEY_TCP_SRC : | |
a1fb0d48 | 627 | TCA_FLOWER_KEY_TCP_DST; |
6bd5b80c | 628 | else if (ip_proto == IPPROTO_UDP) |
6910d656 SH |
629 | return endpoint == FLOWER_ENDPOINT_SRC ? |
630 | TCA_FLOWER_KEY_UDP_SRC : | |
a1fb0d48 | 631 | TCA_FLOWER_KEY_UDP_DST; |
6bd5b80c | 632 | else if (ip_proto == IPPROTO_SCTP) |
6910d656 SH |
633 | return endpoint == FLOWER_ENDPOINT_SRC ? |
634 | TCA_FLOWER_KEY_SCTP_SRC : | |
a1fb0d48 | 635 | TCA_FLOWER_KEY_SCTP_DST; |
6bd5b80c | 636 | else |
30eb304e | 637 | return -1; |
a1fb0d48 SH |
638 | } |
639 | ||
482fd40a EB |
640 | static int flower_port_attr_mask_type(__u8 ip_proto, |
641 | enum flower_endpoint endpoint) | |
642 | { | |
643 | switch (ip_proto) { | |
644 | case IPPROTO_TCP: | |
645 | return endpoint == FLOWER_ENDPOINT_SRC ? | |
646 | TCA_FLOWER_KEY_TCP_SRC_MASK : | |
647 | TCA_FLOWER_KEY_TCP_DST_MASK; | |
648 | case IPPROTO_UDP: | |
649 | return endpoint == FLOWER_ENDPOINT_SRC ? | |
650 | TCA_FLOWER_KEY_UDP_SRC_MASK : | |
651 | TCA_FLOWER_KEY_UDP_DST_MASK; | |
652 | case IPPROTO_SCTP: | |
653 | return endpoint == FLOWER_ENDPOINT_SRC ? | |
654 | TCA_FLOWER_KEY_SCTP_SRC_MASK : | |
655 | TCA_FLOWER_KEY_SCTP_DST_MASK; | |
656 | default: | |
657 | return -1; | |
658 | } | |
659 | } | |
660 | ||
8930840e AN |
661 | static int flower_port_range_attr_type(__u8 ip_proto, enum flower_endpoint type, |
662 | __be16 *min_port_type, | |
663 | __be16 *max_port_type) | |
664 | { | |
665 | if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP || | |
666 | ip_proto == IPPROTO_SCTP) { | |
667 | if (type == FLOWER_ENDPOINT_SRC) { | |
668 | *min_port_type = TCA_FLOWER_KEY_PORT_SRC_MIN; | |
669 | *max_port_type = TCA_FLOWER_KEY_PORT_SRC_MAX; | |
670 | } else { | |
671 | *min_port_type = TCA_FLOWER_KEY_PORT_DST_MIN; | |
672 | *max_port_type = TCA_FLOWER_KEY_PORT_DST_MAX; | |
673 | } | |
674 | } else { | |
675 | return -1; | |
676 | } | |
677 | return 0; | |
678 | } | |
679 | ||
767b6fd6 LC |
680 | /* parse range args in format 10-20 */ |
681 | static int parse_range(char *str, __be16 *min, __be16 *max) | |
682 | { | |
683 | char *sep; | |
684 | ||
685 | sep = strchr(str, '-'); | |
686 | if (sep) { | |
687 | *sep = '\0'; | |
688 | ||
689 | if (get_be16(min, str, 10)) | |
690 | return -1; | |
691 | ||
692 | if (get_be16(max, sep + 1, 10)) | |
693 | return -1; | |
694 | } else { | |
695 | if (get_be16(min, str, 10)) | |
696 | return -1; | |
697 | } | |
698 | return 0; | |
699 | } | |
700 | ||
6910d656 SH |
701 | static int flower_parse_port(char *str, __u8 ip_proto, |
702 | enum flower_endpoint endpoint, | |
a1fb0d48 SH |
703 | struct nlmsghdr *n) |
704 | { | |
482fd40a | 705 | char *slash = NULL; |
767b6fd6 LC |
706 | __be16 min = 0; |
707 | __be16 max = 0; | |
a1fb0d48 | 708 | int ret; |
a1fb0d48 | 709 | |
767b6fd6 | 710 | ret = parse_range(str, &min, &max); |
482fd40a EB |
711 | if (ret) { |
712 | slash = strchr(str, '/'); | |
713 | if (!slash) | |
714 | return -1; | |
715 | } | |
30eb304e | 716 | |
767b6fd6 | 717 | if (min && max) { |
8930840e | 718 | __be16 min_port_type, max_port_type; |
30eb304e | 719 | |
8930840e AN |
720 | if (max <= min) { |
721 | fprintf(stderr, "max value should be greater than min value\n"); | |
722 | return -1; | |
723 | } | |
724 | if (flower_port_range_attr_type(ip_proto, endpoint, | |
725 | &min_port_type, &max_port_type)) | |
726 | return -1; | |
727 | ||
767b6fd6 LC |
728 | addattr16(n, MAX_MSG, min_port_type, min); |
729 | addattr16(n, MAX_MSG, max_port_type, max); | |
482fd40a | 730 | } else if (slash || (min && !max)) { |
767b6fd6 LC |
731 | int type; |
732 | ||
733 | type = flower_port_attr_type(ip_proto, endpoint); | |
734 | if (type < 0) | |
735 | return -1; | |
482fd40a EB |
736 | |
737 | if (!slash) { | |
738 | addattr16(n, MAX_MSG, type, min); | |
739 | } else { | |
740 | int mask_type; | |
741 | ||
742 | mask_type = flower_port_attr_mask_type(ip_proto, | |
743 | endpoint); | |
744 | if (mask_type < 0) | |
745 | return -1; | |
746 | return flower_parse_u16(str, type, mask_type, n, true); | |
747 | } | |
8930840e AN |
748 | } else { |
749 | return -1; | |
750 | } | |
30eb304e JP |
751 | return 0; |
752 | } | |
753 | ||
0c30d14d JP |
754 | #define TCP_FLAGS_MAX_MASK 0xfff |
755 | ||
756 | static int flower_parse_tcp_flags(char *str, int flags_type, int mask_type, | |
757 | struct nlmsghdr *n) | |
758 | { | |
759 | char *slash; | |
760 | int ret, err = -1; | |
761 | __u16 flags; | |
762 | ||
763 | slash = strchr(str, '/'); | |
764 | if (slash) | |
765 | *slash = '\0'; | |
766 | ||
767 | ret = get_u16(&flags, str, 16); | |
768 | if (ret < 0 || flags & ~TCP_FLAGS_MAX_MASK) | |
769 | goto err; | |
770 | ||
771 | addattr16(n, MAX_MSG, flags_type, htons(flags)); | |
772 | ||
773 | if (slash) { | |
774 | ret = get_u16(&flags, slash + 1, 16); | |
775 | if (ret < 0 || flags & ~TCP_FLAGS_MAX_MASK) | |
776 | goto err; | |
777 | } else { | |
778 | flags = TCP_FLAGS_MAX_MASK; | |
779 | } | |
780 | addattr16(n, MAX_MSG, mask_type, htons(flags)); | |
781 | ||
782 | err = 0; | |
783 | err: | |
784 | if (slash) | |
785 | *slash = '/'; | |
786 | return err; | |
787 | } | |
788 | ||
6ea2c2b1 OG |
789 | static int flower_parse_ip_tos_ttl(char *str, int key_type, int mask_type, |
790 | struct nlmsghdr *n) | |
791 | { | |
792 | char *slash; | |
793 | int ret, err = -1; | |
794 | __u8 tos_ttl; | |
795 | ||
796 | slash = strchr(str, '/'); | |
797 | if (slash) | |
798 | *slash = '\0'; | |
799 | ||
800 | ret = get_u8(&tos_ttl, str, 10); | |
801 | if (ret < 0) | |
802 | ret = get_u8(&tos_ttl, str, 16); | |
803 | if (ret < 0) | |
804 | goto err; | |
805 | ||
806 | addattr8(n, MAX_MSG, key_type, tos_ttl); | |
807 | ||
808 | if (slash) { | |
809 | ret = get_u8(&tos_ttl, slash + 1, 16); | |
810 | if (ret < 0) | |
811 | goto err; | |
812 | } else { | |
813 | tos_ttl = 0xff; | |
814 | } | |
815 | addattr8(n, MAX_MSG, mask_type, tos_ttl); | |
816 | ||
817 | err = 0; | |
818 | err: | |
819 | if (slash) | |
820 | *slash = '/'; | |
821 | return err; | |
822 | } | |
823 | ||
bb9b63b1 AV |
824 | static int flower_parse_key_id(const char *str, int type, struct nlmsghdr *n) |
825 | { | |
826 | int ret; | |
827 | __be32 key_id; | |
828 | ||
829 | ret = get_be32(&key_id, str, 10); | |
830 | if (!ret) | |
831 | addattr32(n, MAX_MSG, type, key_id); | |
832 | ||
833 | return ret; | |
834 | } | |
835 | ||
41aa17ff HHZ |
836 | static int flower_parse_enc_port(char *str, int type, struct nlmsghdr *n) |
837 | { | |
838 | int ret; | |
839 | __be16 port; | |
840 | ||
841 | ret = get_be16(&port, str, 10); | |
842 | if (ret) | |
843 | return -1; | |
844 | ||
845 | addattr16(n, MAX_MSG, type, port); | |
846 | ||
847 | return 0; | |
848 | } | |
849 | ||
56155d4d PJV |
850 | static int flower_parse_geneve_opts(char *str, struct nlmsghdr *n) |
851 | { | |
852 | struct rtattr *nest; | |
853 | char *token; | |
854 | int i, err; | |
855 | ||
856 | nest = addattr_nest(n, MAX_MSG, TCA_FLOWER_KEY_ENC_OPTS_GENEVE); | |
857 | ||
858 | i = 1; | |
859 | token = strsep(&str, ":"); | |
860 | while (token) { | |
861 | switch (i) { | |
862 | case TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS: | |
863 | { | |
864 | __be16 opt_class; | |
865 | ||
866 | if (!strlen(token)) | |
867 | break; | |
868 | err = get_be16(&opt_class, token, 16); | |
869 | if (err) | |
870 | return err; | |
871 | ||
872 | addattr16(n, MAX_MSG, i, opt_class); | |
873 | break; | |
874 | } | |
875 | case TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE: | |
876 | { | |
877 | __u8 opt_type; | |
878 | ||
879 | if (!strlen(token)) | |
880 | break; | |
881 | err = get_u8(&opt_type, token, 16); | |
882 | if (err) | |
883 | return err; | |
884 | ||
885 | addattr8(n, MAX_MSG, i, opt_type); | |
886 | break; | |
887 | } | |
888 | case TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA: | |
889 | { | |
890 | size_t token_len = strlen(token); | |
891 | __u8 *opts; | |
892 | ||
893 | if (!token_len) | |
894 | break; | |
895 | opts = malloc(token_len / 2); | |
896 | if (!opts) | |
897 | return -1; | |
898 | if (hex2mem(token, opts, token_len / 2) < 0) { | |
899 | free(opts); | |
900 | return -1; | |
901 | } | |
902 | addattr_l(n, MAX_MSG, i, opts, token_len / 2); | |
903 | free(opts); | |
904 | ||
905 | break; | |
906 | } | |
907 | default: | |
908 | fprintf(stderr, "Unknown \"geneve_opts\" type\n"); | |
909 | return -1; | |
910 | } | |
911 | ||
912 | token = strsep(&str, ":"); | |
913 | i++; | |
914 | } | |
915 | addattr_nest_end(n, nest); | |
916 | ||
917 | return 0; | |
918 | } | |
919 | ||
920 | static int flower_parse_enc_opt_part(char *str, struct nlmsghdr *n) | |
921 | { | |
922 | char *token; | |
923 | int err; | |
924 | ||
925 | token = strsep(&str, ","); | |
926 | while (token) { | |
927 | err = flower_parse_geneve_opts(token, n); | |
928 | if (err) | |
929 | return err; | |
930 | ||
931 | token = strsep(&str, ","); | |
932 | } | |
933 | ||
934 | return 0; | |
935 | } | |
936 | ||
937 | static int flower_check_enc_opt_key(char *key) | |
938 | { | |
939 | int key_len, col_cnt = 0; | |
940 | ||
941 | key_len = strlen(key); | |
942 | while ((key = strchr(key, ':'))) { | |
943 | if (strlen(key) == key_len) | |
944 | return -1; | |
945 | ||
946 | key_len = strlen(key) - 1; | |
947 | col_cnt++; | |
948 | key++; | |
949 | } | |
950 | ||
951 | if (col_cnt != 2 || !key_len) | |
952 | return -1; | |
953 | ||
954 | return 0; | |
955 | } | |
956 | ||
957 | static int flower_parse_enc_opts(char *str, struct nlmsghdr *n) | |
958 | { | |
959 | char key[XATTR_SIZE_MAX], mask[XATTR_SIZE_MAX]; | |
960 | int data_len, key_len, mask_len, err; | |
961 | char *token, *slash; | |
962 | struct rtattr *nest; | |
963 | ||
964 | key_len = 0; | |
965 | mask_len = 0; | |
966 | token = strsep(&str, ","); | |
967 | while (token) { | |
968 | slash = strchr(token, '/'); | |
969 | if (slash) | |
970 | *slash = '\0'; | |
971 | ||
972 | if ((key_len + strlen(token) > XATTR_SIZE_MAX) || | |
973 | flower_check_enc_opt_key(token)) | |
974 | return -1; | |
975 | ||
976 | strcpy(&key[key_len], token); | |
977 | key_len += strlen(token) + 1; | |
978 | key[key_len - 1] = ','; | |
979 | ||
980 | if (!slash) { | |
981 | /* Pad out mask when not provided */ | |
982 | if (mask_len + strlen(token) > XATTR_SIZE_MAX) | |
983 | return -1; | |
984 | ||
985 | data_len = strlen(rindex(token, ':')); | |
986 | sprintf(&mask[mask_len], "ffff:ff:"); | |
987 | mask_len += 8; | |
988 | memset(&mask[mask_len], 'f', data_len - 1); | |
989 | mask_len += data_len; | |
990 | mask[mask_len - 1] = ','; | |
991 | token = strsep(&str, ","); | |
992 | continue; | |
993 | } | |
994 | ||
995 | if (mask_len + strlen(slash + 1) > XATTR_SIZE_MAX) | |
996 | return -1; | |
997 | ||
998 | strcpy(&mask[mask_len], slash + 1); | |
999 | mask_len += strlen(slash + 1) + 1; | |
1000 | mask[mask_len - 1] = ','; | |
1001 | ||
1002 | *slash = '/'; | |
1003 | token = strsep(&str, ","); | |
1004 | } | |
1005 | key[key_len - 1] = '\0'; | |
1006 | mask[mask_len - 1] = '\0'; | |
1007 | ||
1008 | nest = addattr_nest(n, MAX_MSG, TCA_FLOWER_KEY_ENC_OPTS); | |
1009 | err = flower_parse_enc_opt_part(key, n); | |
1010 | if (err) | |
1011 | return err; | |
1012 | addattr_nest_end(n, nest); | |
1013 | ||
1014 | nest = addattr_nest(n, MAX_MSG, TCA_FLOWER_KEY_ENC_OPTS_MASK); | |
1015 | err = flower_parse_enc_opt_part(mask, n); | |
1016 | if (err) | |
1017 | return err; | |
1018 | addattr_nest_end(n, nest); | |
1019 | ||
1020 | return 0; | |
1021 | } | |
1022 | ||
30eb304e JP |
1023 | static int flower_parse_opt(struct filter_util *qu, char *handle, |
1024 | int argc, char **argv, struct nlmsghdr *n) | |
1025 | { | |
1026 | int ret; | |
1027 | struct tcmsg *t = NLMSG_DATA(n); | |
1028 | struct rtattr *tail; | |
488b41d0 | 1029 | __be16 eth_type = TC_H_MIN(t->tcm_info); |
745d9172 | 1030 | __be16 vlan_ethtype = 0; |
1f0a5dfd | 1031 | __be16 cvlan_ethtype = 0; |
30eb304e | 1032 | __u8 ip_proto = 0xff; |
cfcabf18 | 1033 | __u32 flags = 0; |
08f66c80 PB |
1034 | __u32 mtf = 0; |
1035 | __u32 mtf_mask = 0; | |
30eb304e | 1036 | |
30eb304e JP |
1037 | if (handle) { |
1038 | ret = get_u32(&t->tcm_handle, handle, 0); | |
1039 | if (ret) { | |
1040 | fprintf(stderr, "Illegal \"handle\"\n"); | |
1041 | return -1; | |
1042 | } | |
1043 | } | |
1044 | ||
1045 | tail = (struct rtattr *) (((void *) n) + NLMSG_ALIGN(n->nlmsg_len)); | |
1046 | addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0); | |
1047 | ||
488b41d0 JHS |
1048 | if (argc == 0) { |
1049 | /*at minimal we will match all ethertype packets */ | |
1050 | goto parse_done; | |
1051 | } | |
1052 | ||
30eb304e JP |
1053 | while (argc > 0) { |
1054 | if (matches(*argv, "classid") == 0 || | |
1055 | matches(*argv, "flowid") == 0) { | |
32a121cb | 1056 | unsigned int handle; |
30eb304e JP |
1057 | |
1058 | NEXT_ARG(); | |
1059 | ret = get_tc_classid(&handle, *argv); | |
1060 | if (ret) { | |
1061 | fprintf(stderr, "Illegal \"classid\"\n"); | |
1062 | return -1; | |
1063 | } | |
1064 | addattr_l(n, MAX_MSG, TCA_FLOWER_CLASSID, &handle, 4); | |
0d575c4d AN |
1065 | } else if (matches(*argv, "hw_tc") == 0) { |
1066 | unsigned int handle; | |
1067 | __u32 tc; | |
1068 | char *end; | |
1069 | ||
1070 | NEXT_ARG(); | |
1071 | tc = strtoul(*argv, &end, 0); | |
1072 | if (*end) { | |
1073 | fprintf(stderr, "Illegal TC index\n"); | |
1074 | return -1; | |
1075 | } | |
1076 | if (tc >= TC_QOPT_MAX_QUEUE) { | |
1077 | fprintf(stderr, "TC index exceeds max range\n"); | |
1078 | return -1; | |
1079 | } | |
1080 | handle = TC_H_MAKE(TC_H_MAJ(t->tcm_parent), | |
1081 | TC_H_MIN(tc + TC_H_MIN_PRIORITY)); | |
1082 | addattr_l(n, MAX_MSG, TCA_FLOWER_CLASSID, &handle, | |
1083 | sizeof(handle)); | |
08f66c80 | 1084 | } else if (matches(*argv, "ip_flags") == 0) { |
22a8f019 PB |
1085 | NEXT_ARG(); |
1086 | ret = flower_parse_matching_flags(*argv, | |
08f66c80 PB |
1087 | FLOWER_IP_FLAGS, |
1088 | &mtf, | |
1089 | &mtf_mask); | |
22a8f019 | 1090 | if (ret < 0) { |
08f66c80 | 1091 | fprintf(stderr, "Illegal \"ip_flags\"\n"); |
22a8f019 PB |
1092 | return -1; |
1093 | } | |
ac6a4c22 MRL |
1094 | } else if (matches(*argv, "verbose") == 0) { |
1095 | flags |= TCA_CLS_FLAGS_VERBOSE; | |
cfcabf18 AV |
1096 | } else if (matches(*argv, "skip_hw") == 0) { |
1097 | flags |= TCA_CLS_FLAGS_SKIP_HW; | |
1098 | } else if (matches(*argv, "skip_sw") == 0) { | |
1099 | flags |= TCA_CLS_FLAGS_SKIP_SW; | |
2fffb1c0 PB |
1100 | } else if (matches(*argv, "ct_state") == 0) { |
1101 | NEXT_ARG(); | |
1102 | ret = flower_parse_ct_state(*argv, n); | |
1103 | if (ret < 0) { | |
1104 | fprintf(stderr, "Illegal \"ct_state\"\n"); | |
1105 | return -1; | |
1106 | } | |
1107 | } else if (matches(*argv, "ct_zone") == 0) { | |
1108 | NEXT_ARG(); | |
1109 | ret = flower_parse_ct_zone(*argv, n); | |
1110 | if (ret < 0) { | |
1111 | fprintf(stderr, "Illegal \"ct_zone\"\n"); | |
1112 | return -1; | |
1113 | } | |
1114 | } else if (matches(*argv, "ct_mark") == 0) { | |
1115 | NEXT_ARG(); | |
1116 | ret = flower_parse_ct_mark(*argv, n); | |
1117 | if (ret < 0) { | |
1118 | fprintf(stderr, "Illegal \"ct_mark\"\n"); | |
1119 | return -1; | |
1120 | } | |
1121 | } else if (matches(*argv, "ct_label") == 0) { | |
1122 | NEXT_ARG(); | |
1123 | ret = flower_parse_ct_labels(*argv, n); | |
1124 | if (ret < 0) { | |
1125 | fprintf(stderr, "Illegal \"ct_label\"\n"); | |
1126 | return -1; | |
1127 | } | |
30eb304e | 1128 | } else if (matches(*argv, "indev") == 0) { |
30eb304e | 1129 | NEXT_ARG(); |
625df645 PS |
1130 | if (check_ifname(*argv)) |
1131 | invarg("\"indev\" not a valid ifname", *argv); | |
ee474849 | 1132 | addattrstrz(n, MAX_MSG, TCA_FLOWER_INDEV, *argv); |
745d9172 HHZ |
1133 | } else if (matches(*argv, "vlan_id") == 0) { |
1134 | __u16 vid; | |
1135 | ||
1136 | NEXT_ARG(); | |
1f0a5dfd JL |
1137 | if (!eth_type_vlan(eth_type)) { |
1138 | fprintf(stderr, "Can't set \"vlan_id\" if ethertype isn't 802.1Q or 802.1AD\n"); | |
745d9172 HHZ |
1139 | return -1; |
1140 | } | |
1141 | ret = get_u16(&vid, *argv, 10); | |
1142 | if (ret < 0 || vid & ~0xfff) { | |
1143 | fprintf(stderr, "Illegal \"vlan_id\"\n"); | |
1144 | return -1; | |
1145 | } | |
1146 | addattr16(n, MAX_MSG, TCA_FLOWER_KEY_VLAN_ID, vid); | |
1147 | } else if (matches(*argv, "vlan_prio") == 0) { | |
1148 | __u8 vlan_prio; | |
1149 | ||
1150 | NEXT_ARG(); | |
1f0a5dfd JL |
1151 | if (!eth_type_vlan(eth_type)) { |
1152 | fprintf(stderr, "Can't set \"vlan_prio\" if ethertype isn't 802.1Q or 802.1AD\n"); | |
745d9172 HHZ |
1153 | return -1; |
1154 | } | |
1155 | ret = get_u8(&vlan_prio, *argv, 10); | |
1156 | if (ret < 0 || vlan_prio & ~0x7) { | |
1157 | fprintf(stderr, "Illegal \"vlan_prio\"\n"); | |
1158 | return -1; | |
1159 | } | |
512caeb2 SH |
1160 | addattr8(n, MAX_MSG, |
1161 | TCA_FLOWER_KEY_VLAN_PRIO, vlan_prio); | |
745d9172 HHZ |
1162 | } else if (matches(*argv, "vlan_ethtype") == 0) { |
1163 | NEXT_ARG(); | |
1164 | ret = flower_parse_vlan_eth_type(*argv, eth_type, | |
512caeb2 SH |
1165 | TCA_FLOWER_KEY_VLAN_ETH_TYPE, |
1166 | &vlan_ethtype, n); | |
745d9172 HHZ |
1167 | if (ret < 0) |
1168 | return -1; | |
1f0a5dfd JL |
1169 | } else if (matches(*argv, "cvlan_id") == 0) { |
1170 | __u16 vid; | |
1171 | ||
1172 | NEXT_ARG(); | |
1173 | if (!eth_type_vlan(vlan_ethtype)) { | |
1174 | fprintf(stderr, "Can't set \"cvlan_id\" if inner vlan ethertype isn't 802.1Q or 802.1AD\n"); | |
1175 | return -1; | |
1176 | } | |
1177 | ret = get_u16(&vid, *argv, 10); | |
1178 | if (ret < 0 || vid & ~0xfff) { | |
1179 | fprintf(stderr, "Illegal \"cvlan_id\"\n"); | |
1180 | return -1; | |
1181 | } | |
1182 | addattr16(n, MAX_MSG, TCA_FLOWER_KEY_CVLAN_ID, vid); | |
1183 | } else if (matches(*argv, "cvlan_prio") == 0) { | |
1184 | __u8 cvlan_prio; | |
1185 | ||
1186 | NEXT_ARG(); | |
1187 | if (!eth_type_vlan(vlan_ethtype)) { | |
1188 | fprintf(stderr, "Can't set \"cvlan_prio\" if inner vlan ethertype isn't 802.1Q or 802.1AD\n"); | |
1189 | return -1; | |
1190 | } | |
1191 | ret = get_u8(&cvlan_prio, *argv, 10); | |
1192 | if (ret < 0 || cvlan_prio & ~0x7) { | |
1193 | fprintf(stderr, "Illegal \"cvlan_prio\"\n"); | |
1194 | return -1; | |
1195 | } | |
1196 | addattr8(n, MAX_MSG, | |
1197 | TCA_FLOWER_KEY_CVLAN_PRIO, cvlan_prio); | |
1198 | } else if (matches(*argv, "cvlan_ethtype") == 0) { | |
1199 | NEXT_ARG(); | |
1200 | ret = flower_parse_vlan_eth_type(*argv, vlan_ethtype, | |
1201 | TCA_FLOWER_KEY_CVLAN_ETH_TYPE, | |
1202 | &cvlan_ethtype, n); | |
1203 | if (ret < 0) | |
1204 | return -1; | |
7638ee13 BL |
1205 | } else if (matches(*argv, "mpls_label") == 0) { |
1206 | __u32 label; | |
1207 | ||
1208 | NEXT_ARG(); | |
1209 | if (eth_type != htons(ETH_P_MPLS_UC) && | |
1210 | eth_type != htons(ETH_P_MPLS_MC)) { | |
1211 | fprintf(stderr, | |
1212 | "Can't set \"mpls_label\" if ethertype isn't MPLS\n"); | |
1213 | return -1; | |
1214 | } | |
1215 | ret = get_u32(&label, *argv, 10); | |
1216 | if (ret < 0 || label & ~(MPLS_LS_LABEL_MASK >> MPLS_LS_LABEL_SHIFT)) { | |
1217 | fprintf(stderr, "Illegal \"mpls_label\"\n"); | |
1218 | return -1; | |
1219 | } | |
1220 | addattr32(n, MAX_MSG, TCA_FLOWER_KEY_MPLS_LABEL, label); | |
1221 | } else if (matches(*argv, "mpls_tc") == 0) { | |
1222 | __u8 tc; | |
1223 | ||
1224 | NEXT_ARG(); | |
1225 | if (eth_type != htons(ETH_P_MPLS_UC) && | |
1226 | eth_type != htons(ETH_P_MPLS_MC)) { | |
1227 | fprintf(stderr, | |
1228 | "Can't set \"mpls_tc\" if ethertype isn't MPLS\n"); | |
1229 | return -1; | |
1230 | } | |
1231 | ret = get_u8(&tc, *argv, 10); | |
1232 | if (ret < 0 || tc & ~(MPLS_LS_TC_MASK >> MPLS_LS_TC_SHIFT)) { | |
1233 | fprintf(stderr, "Illegal \"mpls_tc\"\n"); | |
1234 | return -1; | |
1235 | } | |
1236 | addattr8(n, MAX_MSG, TCA_FLOWER_KEY_MPLS_TC, tc); | |
1237 | } else if (matches(*argv, "mpls_bos") == 0) { | |
1238 | __u8 bos; | |
1239 | ||
1240 | NEXT_ARG(); | |
1241 | if (eth_type != htons(ETH_P_MPLS_UC) && | |
1242 | eth_type != htons(ETH_P_MPLS_MC)) { | |
1243 | fprintf(stderr, | |
1244 | "Can't set \"mpls_bos\" if ethertype isn't MPLS\n"); | |
1245 | return -1; | |
1246 | } | |
1247 | ret = get_u8(&bos, *argv, 10); | |
1248 | if (ret < 0 || bos & ~(MPLS_LS_S_MASK >> MPLS_LS_S_SHIFT)) { | |
1249 | fprintf(stderr, "Illegal \"mpls_bos\"\n"); | |
1250 | return -1; | |
1251 | } | |
1252 | addattr8(n, MAX_MSG, TCA_FLOWER_KEY_MPLS_BOS, bos); | |
1253 | } else if (matches(*argv, "mpls_ttl") == 0) { | |
1254 | __u8 ttl; | |
1255 | ||
1256 | NEXT_ARG(); | |
1257 | if (eth_type != htons(ETH_P_MPLS_UC) && | |
1258 | eth_type != htons(ETH_P_MPLS_MC)) { | |
1259 | fprintf(stderr, | |
1260 | "Can't set \"mpls_ttl\" if ethertype isn't MPLS\n"); | |
1261 | return -1; | |
1262 | } | |
1263 | ret = get_u8(&ttl, *argv, 10); | |
1264 | if (ret < 0 || ttl & ~(MPLS_LS_TTL_MASK >> MPLS_LS_TTL_SHIFT)) { | |
1265 | fprintf(stderr, "Illegal \"mpls_ttl\"\n"); | |
1266 | return -1; | |
1267 | } | |
1268 | addattr8(n, MAX_MSG, TCA_FLOWER_KEY_MPLS_TTL, ttl); | |
30eb304e JP |
1269 | } else if (matches(*argv, "dst_mac") == 0) { |
1270 | NEXT_ARG(); | |
1271 | ret = flower_parse_eth_addr(*argv, | |
1272 | TCA_FLOWER_KEY_ETH_DST, | |
1273 | TCA_FLOWER_KEY_ETH_DST_MASK, | |
1274 | n); | |
1275 | if (ret < 0) { | |
1276 | fprintf(stderr, "Illegal \"dst_mac\"\n"); | |
1277 | return -1; | |
1278 | } | |
1279 | } else if (matches(*argv, "src_mac") == 0) { | |
1280 | NEXT_ARG(); | |
1281 | ret = flower_parse_eth_addr(*argv, | |
1282 | TCA_FLOWER_KEY_ETH_SRC, | |
1283 | TCA_FLOWER_KEY_ETH_SRC_MASK, | |
1284 | n); | |
1285 | if (ret < 0) { | |
1286 | fprintf(stderr, "Illegal \"src_mac\"\n"); | |
1287 | return -1; | |
1288 | } | |
30eb304e JP |
1289 | } else if (matches(*argv, "ip_proto") == 0) { |
1290 | NEXT_ARG(); | |
1f0a5dfd JL |
1291 | ret = flower_parse_ip_proto(*argv, cvlan_ethtype ? |
1292 | cvlan_ethtype : vlan_ethtype ? | |
745d9172 | 1293 | vlan_ethtype : eth_type, |
30eb304e JP |
1294 | TCA_FLOWER_KEY_IP_PROTO, |
1295 | &ip_proto, n); | |
1296 | if (ret < 0) { | |
1297 | fprintf(stderr, "Illegal \"ip_proto\"\n"); | |
1298 | return -1; | |
1299 | } | |
6ea2c2b1 OG |
1300 | } else if (matches(*argv, "ip_tos") == 0) { |
1301 | NEXT_ARG(); | |
1302 | ret = flower_parse_ip_tos_ttl(*argv, | |
1303 | TCA_FLOWER_KEY_IP_TOS, | |
1304 | TCA_FLOWER_KEY_IP_TOS_MASK, | |
1305 | n); | |
1306 | if (ret < 0) { | |
1307 | fprintf(stderr, "Illegal \"ip_tos\"\n"); | |
1308 | return -1; | |
1309 | } | |
1310 | } else if (matches(*argv, "ip_ttl") == 0) { | |
1311 | NEXT_ARG(); | |
1312 | ret = flower_parse_ip_tos_ttl(*argv, | |
1313 | TCA_FLOWER_KEY_IP_TTL, | |
1314 | TCA_FLOWER_KEY_IP_TTL_MASK, | |
1315 | n); | |
1316 | if (ret < 0) { | |
1317 | fprintf(stderr, "Illegal \"ip_ttl\"\n"); | |
1318 | return -1; | |
1319 | } | |
30eb304e JP |
1320 | } else if (matches(*argv, "dst_ip") == 0) { |
1321 | NEXT_ARG(); | |
1f0a5dfd JL |
1322 | ret = flower_parse_ip_addr(*argv, cvlan_ethtype ? |
1323 | cvlan_ethtype : vlan_ethtype ? | |
745d9172 | 1324 | vlan_ethtype : eth_type, |
30eb304e JP |
1325 | TCA_FLOWER_KEY_IPV4_DST, |
1326 | TCA_FLOWER_KEY_IPV4_DST_MASK, | |
1327 | TCA_FLOWER_KEY_IPV6_DST, | |
1328 | TCA_FLOWER_KEY_IPV6_DST_MASK, | |
1329 | n); | |
1330 | if (ret < 0) { | |
1331 | fprintf(stderr, "Illegal \"dst_ip\"\n"); | |
1332 | return -1; | |
1333 | } | |
1334 | } else if (matches(*argv, "src_ip") == 0) { | |
1335 | NEXT_ARG(); | |
1f0a5dfd JL |
1336 | ret = flower_parse_ip_addr(*argv, cvlan_ethtype ? |
1337 | cvlan_ethtype : vlan_ethtype ? | |
745d9172 | 1338 | vlan_ethtype : eth_type, |
30eb304e JP |
1339 | TCA_FLOWER_KEY_IPV4_SRC, |
1340 | TCA_FLOWER_KEY_IPV4_SRC_MASK, | |
1341 | TCA_FLOWER_KEY_IPV6_SRC, | |
1342 | TCA_FLOWER_KEY_IPV6_SRC_MASK, | |
1343 | n); | |
1344 | if (ret < 0) { | |
1345 | fprintf(stderr, "Illegal \"src_ip\"\n"); | |
1346 | return -1; | |
1347 | } | |
1348 | } else if (matches(*argv, "dst_port") == 0) { | |
1349 | NEXT_ARG(); | |
dd7d522a DA |
1350 | ret = flower_parse_port(*argv, ip_proto, |
1351 | FLOWER_ENDPOINT_DST, n); | |
1352 | if (ret < 0) { | |
1353 | fprintf(stderr, "Illegal \"dst_port\"\n"); | |
1354 | return -1; | |
30eb304e JP |
1355 | } |
1356 | } else if (matches(*argv, "src_port") == 0) { | |
1357 | NEXT_ARG(); | |
dd7d522a DA |
1358 | ret = flower_parse_port(*argv, ip_proto, |
1359 | FLOWER_ENDPOINT_SRC, n); | |
1360 | if (ret < 0) { | |
1361 | fprintf(stderr, "Illegal \"src_port\"\n"); | |
1362 | return -1; | |
30eb304e | 1363 | } |
0c30d14d JP |
1364 | } else if (matches(*argv, "tcp_flags") == 0) { |
1365 | NEXT_ARG(); | |
1366 | ret = flower_parse_tcp_flags(*argv, | |
1367 | TCA_FLOWER_KEY_TCP_FLAGS, | |
1368 | TCA_FLOWER_KEY_TCP_FLAGS_MASK, | |
1369 | n); | |
1370 | if (ret < 0) { | |
1371 | fprintf(stderr, "Illegal \"tcp_flags\"\n"); | |
1372 | return -1; | |
1373 | } | |
eb3b5696 SH |
1374 | } else if (matches(*argv, "type") == 0) { |
1375 | NEXT_ARG(); | |
1376 | ret = flower_parse_icmp(*argv, eth_type, ip_proto, | |
1377 | FLOWER_ICMP_FIELD_TYPE, n); | |
1378 | if (ret < 0) { | |
1379 | fprintf(stderr, "Illegal \"icmp type\"\n"); | |
1380 | return -1; | |
1381 | } | |
1382 | } else if (matches(*argv, "code") == 0) { | |
1383 | NEXT_ARG(); | |
1384 | ret = flower_parse_icmp(*argv, eth_type, ip_proto, | |
1385 | FLOWER_ICMP_FIELD_CODE, n); | |
1386 | if (ret < 0) { | |
1387 | fprintf(stderr, "Illegal \"icmp code\"\n"); | |
1388 | return -1; | |
1389 | } | |
f888f4e2 SH |
1390 | } else if (matches(*argv, "arp_tip") == 0) { |
1391 | NEXT_ARG(); | |
1392 | ret = flower_parse_arp_ip_addr(*argv, vlan_ethtype ? | |
1393 | vlan_ethtype : eth_type, | |
1394 | TCA_FLOWER_KEY_ARP_TIP, | |
1395 | TCA_FLOWER_KEY_ARP_TIP_MASK, | |
1396 | n); | |
1397 | if (ret < 0) { | |
1398 | fprintf(stderr, "Illegal \"arp_tip\"\n"); | |
1399 | return -1; | |
1400 | } | |
1401 | } else if (matches(*argv, "arp_sip") == 0) { | |
1402 | NEXT_ARG(); | |
1403 | ret = flower_parse_arp_ip_addr(*argv, vlan_ethtype ? | |
1404 | vlan_ethtype : eth_type, | |
1405 | TCA_FLOWER_KEY_ARP_SIP, | |
1406 | TCA_FLOWER_KEY_ARP_SIP_MASK, | |
1407 | n); | |
1408 | if (ret < 0) { | |
1409 | fprintf(stderr, "Illegal \"arp_sip\"\n"); | |
1410 | return -1; | |
1411 | } | |
1412 | } else if (matches(*argv, "arp_op") == 0) { | |
1413 | NEXT_ARG(); | |
1414 | ret = flower_parse_arp_op(*argv, vlan_ethtype ? | |
1415 | vlan_ethtype : eth_type, | |
1416 | TCA_FLOWER_KEY_ARP_OP, | |
1417 | TCA_FLOWER_KEY_ARP_OP_MASK, | |
1418 | n); | |
1419 | if (ret < 0) { | |
1420 | fprintf(stderr, "Illegal \"arp_op\"\n"); | |
1421 | return -1; | |
1422 | } | |
1423 | } else if (matches(*argv, "arp_tha") == 0) { | |
1424 | NEXT_ARG(); | |
1425 | ret = flower_parse_eth_addr(*argv, | |
1426 | TCA_FLOWER_KEY_ARP_THA, | |
1427 | TCA_FLOWER_KEY_ARP_THA_MASK, | |
1428 | n); | |
1429 | if (ret < 0) { | |
1430 | fprintf(stderr, "Illegal \"arp_tha\"\n"); | |
1431 | return -1; | |
1432 | } | |
1433 | } else if (matches(*argv, "arp_sha") == 0) { | |
1434 | NEXT_ARG(); | |
1435 | ret = flower_parse_eth_addr(*argv, | |
1436 | TCA_FLOWER_KEY_ARP_SHA, | |
1437 | TCA_FLOWER_KEY_ARP_SHA_MASK, | |
1438 | n); | |
1439 | if (ret < 0) { | |
1440 | fprintf(stderr, "Illegal \"arp_sha\"\n"); | |
1441 | return -1; | |
1442 | } | |
bb9b63b1 AV |
1443 | } else if (matches(*argv, "enc_dst_ip") == 0) { |
1444 | NEXT_ARG(); | |
1445 | ret = flower_parse_ip_addr(*argv, 0, | |
1446 | TCA_FLOWER_KEY_ENC_IPV4_DST, | |
1447 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, | |
1448 | TCA_FLOWER_KEY_ENC_IPV6_DST, | |
1449 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, | |
1450 | n); | |
1451 | if (ret < 0) { | |
1452 | fprintf(stderr, "Illegal \"enc_dst_ip\"\n"); | |
1453 | return -1; | |
1454 | } | |
1455 | } else if (matches(*argv, "enc_src_ip") == 0) { | |
1456 | NEXT_ARG(); | |
1457 | ret = flower_parse_ip_addr(*argv, 0, | |
1458 | TCA_FLOWER_KEY_ENC_IPV4_SRC, | |
1459 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, | |
1460 | TCA_FLOWER_KEY_ENC_IPV6_SRC, | |
1461 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, | |
1462 | n); | |
1463 | if (ret < 0) { | |
1464 | fprintf(stderr, "Illegal \"enc_src_ip\"\n"); | |
1465 | return -1; | |
1466 | } | |
1467 | } else if (matches(*argv, "enc_key_id") == 0) { | |
1468 | NEXT_ARG(); | |
1469 | ret = flower_parse_key_id(*argv, | |
1470 | TCA_FLOWER_KEY_ENC_KEY_ID, n); | |
1471 | if (ret < 0) { | |
1472 | fprintf(stderr, "Illegal \"enc_key_id\"\n"); | |
1473 | return -1; | |
1474 | } | |
41aa17ff HHZ |
1475 | } else if (matches(*argv, "enc_dst_port") == 0) { |
1476 | NEXT_ARG(); | |
1477 | ret = flower_parse_enc_port(*argv, | |
1478 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT, n); | |
1479 | if (ret < 0) { | |
1480 | fprintf(stderr, "Illegal \"enc_dst_port\"\n"); | |
1481 | return -1; | |
1482 | } | |
761ec9e2 OG |
1483 | } else if (matches(*argv, "enc_tos") == 0) { |
1484 | NEXT_ARG(); | |
1485 | ret = flower_parse_ip_tos_ttl(*argv, | |
1486 | TCA_FLOWER_KEY_ENC_IP_TOS, | |
1487 | TCA_FLOWER_KEY_ENC_IP_TOS_MASK, | |
1488 | n); | |
1489 | if (ret < 0) { | |
1490 | fprintf(stderr, "Illegal \"enc_tos\"\n"); | |
1491 | return -1; | |
1492 | } | |
1493 | } else if (matches(*argv, "enc_ttl") == 0) { | |
1494 | NEXT_ARG(); | |
1495 | ret = flower_parse_ip_tos_ttl(*argv, | |
1496 | TCA_FLOWER_KEY_ENC_IP_TTL, | |
1497 | TCA_FLOWER_KEY_ENC_IP_TTL_MASK, | |
1498 | n); | |
1499 | if (ret < 0) { | |
1500 | fprintf(stderr, "Illegal \"enc_ttl\"\n"); | |
1501 | return -1; | |
1502 | } | |
56155d4d PJV |
1503 | } else if (matches(*argv, "geneve_opts") == 0) { |
1504 | NEXT_ARG(); | |
1505 | ret = flower_parse_enc_opts(*argv, n); | |
1506 | if (ret < 0) { | |
1507 | fprintf(stderr, "Illegal \"geneve_opts\"\n"); | |
1508 | return -1; | |
1509 | } | |
30eb304e JP |
1510 | } else if (matches(*argv, "action") == 0) { |
1511 | NEXT_ARG(); | |
1512 | ret = parse_action(&argc, &argv, TCA_FLOWER_ACT, n); | |
1513 | if (ret) { | |
1514 | fprintf(stderr, "Illegal \"action\"\n"); | |
1515 | return -1; | |
1516 | } | |
1517 | continue; | |
1518 | } else if (strcmp(*argv, "help") == 0) { | |
1519 | explain(); | |
1520 | return -1; | |
1521 | } else { | |
1522 | fprintf(stderr, "What is \"%s\"?\n", *argv); | |
1523 | explain(); | |
1524 | return -1; | |
1525 | } | |
1526 | argc--; argv++; | |
1527 | } | |
1528 | ||
488b41d0 | 1529 | parse_done: |
c85609b2 RD |
1530 | ret = addattr32(n, MAX_MSG, TCA_FLOWER_FLAGS, flags); |
1531 | if (ret) | |
1532 | return ret; | |
cfcabf18 | 1533 | |
08f66c80 PB |
1534 | if (mtf_mask) { |
1535 | ret = addattr32(n, MAX_MSG, TCA_FLOWER_KEY_FLAGS, htonl(mtf)); | |
1536 | if (ret) | |
1537 | return ret; | |
1538 | ||
1539 | ret = addattr32(n, MAX_MSG, TCA_FLOWER_KEY_FLAGS_MASK, htonl(mtf_mask)); | |
1540 | if (ret) | |
1541 | return ret; | |
1542 | } | |
1543 | ||
4f7d406f BL |
1544 | if (eth_type != htons(ETH_P_ALL)) { |
1545 | ret = addattr16(n, MAX_MSG, TCA_FLOWER_KEY_ETH_TYPE, eth_type); | |
1546 | if (ret) | |
1547 | return ret; | |
1548 | } | |
488b41d0 | 1549 | |
32a121cb | 1550 | tail->rta_len = (((void *)n)+n->nlmsg_len) - (void *)tail; |
30eb304e JP |
1551 | |
1552 | return 0; | |
1553 | } | |
1554 | ||
1555 | static int __mask_bits(char *addr, size_t len) | |
1556 | { | |
1557 | int bits = 0; | |
1558 | bool hole = false; | |
1559 | int i; | |
1560 | int j; | |
1561 | ||
1562 | for (i = 0; i < len; i++, addr++) { | |
1563 | for (j = 7; j >= 0; j--) { | |
1564 | if (((*addr) >> j) & 0x1) { | |
1565 | if (hole) | |
1566 | return -1; | |
1567 | bits++; | |
1568 | } else if (bits) { | |
1569 | hole = true; | |
1570 | } else{ | |
1571 | return -1; | |
1572 | } | |
1573 | } | |
1574 | } | |
1575 | return bits; | |
1576 | } | |
1577 | ||
e28b88a4 | 1578 | static void flower_print_eth_addr(char *name, struct rtattr *addr_attr, |
30eb304e JP |
1579 | struct rtattr *mask_attr) |
1580 | { | |
e28b88a4 JP |
1581 | SPRINT_BUF(namefrm); |
1582 | SPRINT_BUF(out); | |
30eb304e | 1583 | SPRINT_BUF(b1); |
e28b88a4 | 1584 | size_t done; |
30eb304e JP |
1585 | int bits; |
1586 | ||
1587 | if (!addr_attr || RTA_PAYLOAD(addr_attr) != ETH_ALEN) | |
1588 | return; | |
e28b88a4 JP |
1589 | done = sprintf(out, "%s", |
1590 | ll_addr_n2a(RTA_DATA(addr_attr), ETH_ALEN, | |
1591 | 0, b1, sizeof(b1))); | |
1592 | if (mask_attr && RTA_PAYLOAD(mask_attr) == ETH_ALEN) { | |
1593 | bits = __mask_bits(RTA_DATA(mask_attr), ETH_ALEN); | |
1594 | if (bits < 0) | |
1595 | sprintf(out + done, "/%s", | |
1596 | ll_addr_n2a(RTA_DATA(mask_attr), ETH_ALEN, | |
1597 | 0, b1, sizeof(b1))); | |
1598 | else if (bits < ETH_ALEN * 8) | |
1599 | sprintf(out + done, "/%d", bits); | |
1600 | } | |
1601 | ||
1602 | sprintf(namefrm, "\n %s %%s", name); | |
1603 | print_string(PRINT_ANY, name, namefrm, out); | |
30eb304e JP |
1604 | } |
1605 | ||
e28b88a4 | 1606 | static void flower_print_eth_type(__be16 *p_eth_type, |
30eb304e JP |
1607 | struct rtattr *eth_type_attr) |
1608 | { | |
e28b88a4 | 1609 | SPRINT_BUF(out); |
30eb304e JP |
1610 | __be16 eth_type; |
1611 | ||
1612 | if (!eth_type_attr) | |
1613 | return; | |
1614 | ||
1615 | eth_type = rta_getattr_u16(eth_type_attr); | |
30eb304e | 1616 | if (eth_type == htons(ETH_P_IP)) |
e28b88a4 | 1617 | sprintf(out, "ipv4"); |
30eb304e | 1618 | else if (eth_type == htons(ETH_P_IPV6)) |
e28b88a4 | 1619 | sprintf(out, "ipv6"); |
f888f4e2 | 1620 | else if (eth_type == htons(ETH_P_ARP)) |
e28b88a4 | 1621 | sprintf(out, "arp"); |
f888f4e2 | 1622 | else if (eth_type == htons(ETH_P_RARP)) |
e28b88a4 | 1623 | sprintf(out, "rarp"); |
30eb304e | 1624 | else |
e28b88a4 JP |
1625 | sprintf(out, "%04x", ntohs(eth_type)); |
1626 | ||
1627 | print_string(PRINT_ANY, "eth_type", "\n eth_type %s", out); | |
30eb304e JP |
1628 | *p_eth_type = eth_type; |
1629 | } | |
1630 | ||
e28b88a4 | 1631 | static void flower_print_ip_proto(__u8 *p_ip_proto, |
30eb304e JP |
1632 | struct rtattr *ip_proto_attr) |
1633 | { | |
e28b88a4 | 1634 | SPRINT_BUF(out); |
30eb304e JP |
1635 | __u8 ip_proto; |
1636 | ||
1637 | if (!ip_proto_attr) | |
1638 | return; | |
1639 | ||
1640 | ip_proto = rta_getattr_u8(ip_proto_attr); | |
30eb304e | 1641 | if (ip_proto == IPPROTO_TCP) |
e28b88a4 | 1642 | sprintf(out, "tcp"); |
30eb304e | 1643 | else if (ip_proto == IPPROTO_UDP) |
e28b88a4 | 1644 | sprintf(out, "udp"); |
a1fb0d48 | 1645 | else if (ip_proto == IPPROTO_SCTP) |
e28b88a4 | 1646 | sprintf(out, "sctp"); |
eb3b5696 | 1647 | else if (ip_proto == IPPROTO_ICMP) |
e28b88a4 | 1648 | sprintf(out, "icmp"); |
eb3b5696 | 1649 | else if (ip_proto == IPPROTO_ICMPV6) |
e28b88a4 | 1650 | sprintf(out, "icmpv6"); |
30eb304e | 1651 | else |
e28b88a4 JP |
1652 | sprintf(out, "%02x", ip_proto); |
1653 | ||
1654 | print_string(PRINT_ANY, "ip_proto", "\n ip_proto %s", out); | |
30eb304e JP |
1655 | *p_ip_proto = ip_proto; |
1656 | } | |
1657 | ||
e8bd3955 | 1658 | static void flower_print_ip_attr(const char *name, struct rtattr *key_attr, |
6ea2c2b1 OG |
1659 | struct rtattr *mask_attr) |
1660 | { | |
9479ec1e | 1661 | print_masked_u8(name, key_attr, mask_attr, true); |
6ea2c2b1 OG |
1662 | } |
1663 | ||
e28b88a4 | 1664 | static void flower_print_matching_flags(char *name, |
08f66c80 | 1665 | enum flower_matching_flags type, |
22a8f019 PB |
1666 | struct rtattr *attr, |
1667 | struct rtattr *mask_attr) | |
1668 | { | |
08f66c80 PB |
1669 | int i; |
1670 | int count = 0; | |
1671 | __u32 mtf; | |
1672 | __u32 mtf_mask; | |
1673 | ||
22a8f019 PB |
1674 | if (!mask_attr || RTA_PAYLOAD(mask_attr) != 4) |
1675 | return; | |
1676 | ||
08f66c80 PB |
1677 | mtf = ntohl(rta_getattr_u32(attr)); |
1678 | mtf_mask = ntohl(rta_getattr_u32(mask_attr)); | |
1679 | ||
1680 | for (i = 0; i < ARRAY_SIZE(flags_str); i++) { | |
1681 | if (type != flags_str[i].type) | |
1682 | continue; | |
1683 | if (mtf_mask & flags_str[i].flag) { | |
e28b88a4 JP |
1684 | if (++count == 1) { |
1685 | print_string(PRINT_FP, NULL, "\n %s ", name); | |
1686 | open_json_object(name); | |
1687 | } else { | |
1688 | print_string(PRINT_FP, NULL, "/", NULL); | |
1689 | } | |
08f66c80 | 1690 | |
e28b88a4 JP |
1691 | print_bool(PRINT_JSON, flags_str[i].string, NULL, |
1692 | mtf & flags_str[i].flag); | |
08f66c80 | 1693 | if (mtf & flags_str[i].flag) |
e28b88a4 JP |
1694 | print_string(PRINT_FP, NULL, "%s", |
1695 | flags_str[i].string); | |
08f66c80 | 1696 | else |
e28b88a4 JP |
1697 | print_string(PRINT_FP, NULL, "no%s", |
1698 | flags_str[i].string); | |
08f66c80 PB |
1699 | } |
1700 | } | |
e28b88a4 JP |
1701 | if (count) |
1702 | close_json_object(); | |
22a8f019 PB |
1703 | } |
1704 | ||
e28b88a4 | 1705 | static void flower_print_ip_addr(char *name, __be16 eth_type, |
30eb304e JP |
1706 | struct rtattr *addr4_attr, |
1707 | struct rtattr *mask4_attr, | |
1708 | struct rtattr *addr6_attr, | |
1709 | struct rtattr *mask6_attr) | |
1710 | { | |
30eb304e JP |
1711 | struct rtattr *addr_attr; |
1712 | struct rtattr *mask_attr; | |
e28b88a4 JP |
1713 | SPRINT_BUF(namefrm); |
1714 | SPRINT_BUF(out); | |
1715 | size_t done; | |
30eb304e JP |
1716 | int family; |
1717 | size_t len; | |
1718 | int bits; | |
1719 | ||
1720 | if (eth_type == htons(ETH_P_IP)) { | |
1721 | family = AF_INET; | |
1722 | addr_attr = addr4_attr; | |
1723 | mask_attr = mask4_attr; | |
1724 | len = 4; | |
1725 | } else if (eth_type == htons(ETH_P_IPV6)) { | |
1726 | family = AF_INET6; | |
1727 | addr_attr = addr6_attr; | |
1728 | mask_attr = mask6_attr; | |
1729 | len = 16; | |
1730 | } else { | |
1731 | return; | |
1732 | } | |
1733 | if (!addr_attr || RTA_PAYLOAD(addr_attr) != len) | |
1734 | return; | |
30eb304e JP |
1735 | if (!mask_attr || RTA_PAYLOAD(mask_attr) != len) |
1736 | return; | |
e28b88a4 | 1737 | done = sprintf(out, "%s", rt_addr_n2a_rta(family, addr_attr)); |
30eb304e JP |
1738 | bits = __mask_bits(RTA_DATA(mask_attr), len); |
1739 | if (bits < 0) | |
e28b88a4 | 1740 | sprintf(out + done, "/%s", rt_addr_n2a_rta(family, mask_attr)); |
30eb304e | 1741 | else if (bits < len * 8) |
e28b88a4 JP |
1742 | sprintf(out + done, "/%d", bits); |
1743 | ||
1744 | sprintf(namefrm, "\n %s %%s", name); | |
1745 | print_string(PRINT_ANY, name, namefrm, out); | |
30eb304e | 1746 | } |
e28b88a4 | 1747 | static void flower_print_ip4_addr(char *name, struct rtattr *addr_attr, |
f888f4e2 SH |
1748 | struct rtattr *mask_attr) |
1749 | { | |
e28b88a4 | 1750 | return flower_print_ip_addr(name, htons(ETH_P_IP), |
f888f4e2 SH |
1751 | addr_attr, mask_attr, 0, 0); |
1752 | } | |
30eb304e | 1753 | |
482fd40a EB |
1754 | static void flower_print_port(char *name, struct rtattr *attr, |
1755 | struct rtattr *mask_attr) | |
30eb304e | 1756 | { |
482fd40a | 1757 | print_masked_be16(name, attr, mask_attr, true); |
30eb304e JP |
1758 | } |
1759 | ||
8930840e AN |
1760 | static void flower_print_port_range(char *name, struct rtattr *min_attr, |
1761 | struct rtattr *max_attr) | |
1762 | { | |
1763 | if (!min_attr || !max_attr) | |
1764 | return; | |
1765 | ||
1766 | if (is_json_context()) { | |
1767 | open_json_object(name); | |
1768 | print_hu(PRINT_JSON, "start", NULL, rta_getattr_be16(min_attr)); | |
1769 | print_hu(PRINT_JSON, "end", NULL, rta_getattr_be16(max_attr)); | |
1770 | close_json_object(); | |
1771 | } else { | |
1772 | SPRINT_BUF(namefrm); | |
1773 | SPRINT_BUF(out); | |
1774 | size_t done; | |
1775 | ||
1776 | done = sprintf(out, "%u", rta_getattr_be16(min_attr)); | |
1777 | sprintf(out + done, "-%u", rta_getattr_be16(max_attr)); | |
1778 | sprintf(namefrm, "\n %s %%s", name); | |
1779 | print_string(PRINT_ANY, name, namefrm, out); | |
1780 | } | |
1781 | } | |
1782 | ||
e8bd3955 | 1783 | static void flower_print_tcp_flags(const char *name, struct rtattr *flags_attr, |
e28b88a4 | 1784 | struct rtattr *mask_attr) |
0c30d14d | 1785 | { |
e28b88a4 JP |
1786 | SPRINT_BUF(namefrm); |
1787 | SPRINT_BUF(out); | |
1788 | size_t done; | |
1789 | ||
0c30d14d JP |
1790 | if (!flags_attr) |
1791 | return; | |
e28b88a4 | 1792 | |
e8bd3955 | 1793 | done = sprintf(out, "0x%x", rta_getattr_be16(flags_attr)); |
e28b88a4 | 1794 | if (mask_attr) |
e8bd3955 | 1795 | sprintf(out + done, "/%x", rta_getattr_be16(mask_attr)); |
e28b88a4 | 1796 | |
e8bd3955 KL |
1797 | print_string(PRINT_FP, NULL, "%s ", _SL_); |
1798 | sprintf(namefrm, "%s %%s", name); | |
e28b88a4 | 1799 | print_string(PRINT_ANY, name, namefrm, out); |
0c30d14d JP |
1800 | } |
1801 | ||
2fffb1c0 PB |
1802 | static void flower_print_ct_state(struct rtattr *flags_attr, |
1803 | struct rtattr *mask_attr) | |
1804 | { | |
1805 | SPRINT_BUF(out); | |
1806 | uint16_t state; | |
1807 | uint16_t state_mask; | |
1808 | size_t done = 0; | |
1809 | int i; | |
1810 | ||
1811 | if (!flags_attr) | |
1812 | return; | |
1813 | ||
1814 | state = rta_getattr_u16(flags_attr); | |
1815 | if (mask_attr) | |
1816 | state_mask = rta_getattr_u16(mask_attr); | |
1817 | else | |
1818 | state_mask = UINT16_MAX; | |
1819 | ||
1820 | for (i = 0; i < ARRAY_SIZE(flower_ct_states); i++) { | |
1821 | if (!(state_mask & flower_ct_states[i].flag)) | |
1822 | continue; | |
1823 | ||
1824 | if (state & flower_ct_states[i].flag) | |
1825 | done += sprintf(out + done, "+%s", | |
1826 | flower_ct_states[i].str); | |
1827 | else | |
1828 | done += sprintf(out + done, "-%s", | |
1829 | flower_ct_states[i].str); | |
1830 | } | |
1831 | ||
1832 | print_string(PRINT_ANY, "ct_state", "\n ct_state %s", out); | |
1833 | } | |
1834 | ||
1835 | static void flower_print_ct_label(struct rtattr *attr, | |
1836 | struct rtattr *mask_attr) | |
1837 | { | |
1838 | const unsigned char *str; | |
1839 | bool print_mask = false; | |
1840 | int data_len, i; | |
1841 | SPRINT_BUF(out); | |
1842 | char *p; | |
1843 | ||
1844 | if (!attr) | |
1845 | return; | |
1846 | ||
1847 | data_len = RTA_PAYLOAD(attr); | |
1848 | hexstring_n2a(RTA_DATA(attr), data_len, out, sizeof(out)); | |
1849 | p = out + data_len*2; | |
1850 | ||
1851 | data_len = RTA_PAYLOAD(attr); | |
1852 | str = RTA_DATA(mask_attr); | |
1853 | if (data_len != 16) | |
1854 | print_mask = true; | |
1855 | for (i = 0; !print_mask && i < data_len; i++) { | |
1856 | if (str[i] != 0xff) | |
1857 | print_mask = true; | |
1858 | } | |
1859 | if (print_mask) { | |
1860 | *p++ = '/'; | |
1861 | hexstring_n2a(RTA_DATA(mask_attr), data_len, p, | |
1862 | sizeof(out)-(p-out)); | |
1863 | p += data_len*2; | |
1864 | } | |
1865 | *p = '\0'; | |
1866 | ||
1867 | print_string(PRINT_ANY, "ct_label", "\n ct_label %s", out); | |
1868 | } | |
1869 | ||
1870 | static void flower_print_ct_zone(struct rtattr *attr, | |
1871 | struct rtattr *mask_attr) | |
1872 | { | |
99d5ee83 | 1873 | print_masked_u16("ct_zone", attr, mask_attr, true); |
2fffb1c0 PB |
1874 | } |
1875 | ||
1876 | static void flower_print_ct_mark(struct rtattr *attr, | |
1877 | struct rtattr *mask_attr) | |
1878 | { | |
99d5ee83 | 1879 | print_masked_u32("ct_mark", attr, mask_attr, true); |
2fffb1c0 | 1880 | } |
0c30d14d | 1881 | |
e28b88a4 | 1882 | static void flower_print_key_id(const char *name, struct rtattr *attr) |
bb9b63b1 | 1883 | { |
e28b88a4 JP |
1884 | SPRINT_BUF(namefrm); |
1885 | ||
1886 | if (!attr) | |
1887 | return; | |
1888 | ||
1889 | sprintf(namefrm,"\n %s %%u", name); | |
1890 | print_uint(PRINT_ANY, name, namefrm, rta_getattr_be32(attr)); | |
bb9b63b1 AV |
1891 | } |
1892 | ||
56155d4d PJV |
1893 | static void flower_print_geneve_opts(const char *name, struct rtattr *attr, |
1894 | char *strbuf) | |
1895 | { | |
1896 | struct rtattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1]; | |
1897 | int ii, data_len, offset = 0, slen = 0; | |
1898 | struct rtattr *i = RTA_DATA(attr); | |
1899 | int rem = RTA_PAYLOAD(attr); | |
1900 | __u8 type, data_r[rem]; | |
1901 | char data[rem * 2 + 1]; | |
1902 | __u16 class; | |
1903 | ||
1904 | open_json_array(PRINT_JSON, name); | |
1905 | while (rem) { | |
1906 | parse_rtattr(tb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX, i, rem); | |
1907 | class = rta_getattr_be16(tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]); | |
1908 | type = rta_getattr_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]); | |
1909 | data_len = RTA_PAYLOAD(tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]); | |
1910 | hexstring_n2a(RTA_DATA(tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]), | |
1911 | data_len, data, sizeof(data)); | |
1912 | hex2mem(data, data_r, data_len); | |
1913 | offset += data_len + 20; | |
1914 | rem -= data_len + 20; | |
1915 | i = RTA_DATA(attr) + offset; | |
1916 | ||
1917 | open_json_object(NULL); | |
1918 | print_uint(PRINT_JSON, "class", NULL, class); | |
1919 | print_uint(PRINT_JSON, "type", NULL, type); | |
1920 | open_json_array(PRINT_JSON, "data"); | |
1921 | for (ii = 0; ii < data_len; ii++) | |
1922 | print_uint(PRINT_JSON, NULL, NULL, data_r[ii]); | |
1923 | close_json_array(PRINT_JSON, "data"); | |
1924 | close_json_object(); | |
1925 | ||
1926 | slen += sprintf(strbuf + slen, "%04x:%02x:%s", | |
1927 | class, type, data); | |
1928 | if (rem) | |
1929 | slen += sprintf(strbuf + slen, ","); | |
1930 | } | |
1931 | close_json_array(PRINT_JSON, name); | |
1932 | } | |
1933 | ||
1934 | static void flower_print_geneve_parts(const char *name, struct rtattr *attr, | |
1935 | char *key, char *mask) | |
1936 | { | |
1937 | char *namefrm = "\n geneve_opt %s"; | |
1938 | char *key_token, *mask_token, *out; | |
1939 | int len; | |
1940 | ||
1941 | out = malloc(RTA_PAYLOAD(attr) * 4 + 3); | |
1942 | if (!out) | |
1943 | return; | |
1944 | ||
1945 | len = 0; | |
1946 | key_token = strsep(&key, ","); | |
1947 | mask_token = strsep(&mask, ","); | |
1948 | while (key_token) { | |
1949 | len += sprintf(&out[len], "%s/%s,", key_token, mask_token); | |
1950 | mask_token = strsep(&mask, ","); | |
1951 | key_token = strsep(&key, ","); | |
1952 | } | |
1953 | ||
1954 | out[len - 1] = '\0'; | |
1955 | print_string(PRINT_FP, name, namefrm, out); | |
1956 | free(out); | |
1957 | } | |
1958 | ||
1959 | static void flower_print_enc_opts(const char *name, struct rtattr *attr, | |
1960 | struct rtattr *mask_attr) | |
1961 | { | |
1962 | struct rtattr *key_tb[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1]; | |
1963 | struct rtattr *msk_tb[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1]; | |
1964 | char *key, *msk; | |
1965 | ||
1966 | if (!attr) | |
1967 | return; | |
1968 | ||
1969 | key = malloc(RTA_PAYLOAD(attr) * 2 + 1); | |
1970 | if (!key) | |
1971 | return; | |
1972 | ||
1973 | msk = malloc(RTA_PAYLOAD(attr) * 2 + 1); | |
1974 | if (!msk) | |
1975 | goto err_key_free; | |
1976 | ||
1977 | parse_rtattr_nested(key_tb, TCA_FLOWER_KEY_ENC_OPTS_MAX, attr); | |
1978 | flower_print_geneve_opts("geneve_opt_key", | |
1979 | key_tb[TCA_FLOWER_KEY_ENC_OPTS_GENEVE], key); | |
1980 | ||
1981 | parse_rtattr_nested(msk_tb, TCA_FLOWER_KEY_ENC_OPTS_MAX, mask_attr); | |
1982 | flower_print_geneve_opts("geneve_opt_mask", | |
1983 | msk_tb[TCA_FLOWER_KEY_ENC_OPTS_GENEVE], msk); | |
1984 | ||
1985 | flower_print_geneve_parts(name, attr, key, msk); | |
1986 | ||
1987 | free(msk); | |
1988 | err_key_free: | |
1989 | free(key); | |
1990 | } | |
1991 | ||
e28b88a4 | 1992 | static void flower_print_masked_u8(const char *name, struct rtattr *attr, |
9d36e54f SH |
1993 | struct rtattr *mask_attr, |
1994 | const char *(*value_to_str)(__u8 value)) | |
f888f4e2 | 1995 | { |
9d36e54f SH |
1996 | const char *value_str = NULL; |
1997 | __u8 value, mask; | |
e28b88a4 JP |
1998 | SPRINT_BUF(namefrm); |
1999 | SPRINT_BUF(out); | |
2000 | size_t done; | |
f888f4e2 | 2001 | |
9d36e54f | 2002 | if (!attr) |
f888f4e2 SH |
2003 | return; |
2004 | ||
9d36e54f | 2005 | value = rta_getattr_u8(attr); |
f888f4e2 | 2006 | mask = mask_attr ? rta_getattr_u8(mask_attr) : UINT8_MAX; |
9d36e54f SH |
2007 | if (mask == UINT8_MAX && value_to_str) |
2008 | value_str = value_to_str(value); | |
f888f4e2 | 2009 | |
9d36e54f | 2010 | if (value_str) |
e28b88a4 | 2011 | done = sprintf(out, "%s", value_str); |
f888f4e2 | 2012 | else |
e28b88a4 | 2013 | done = sprintf(out, "%d", value); |
f888f4e2 SH |
2014 | |
2015 | if (mask != UINT8_MAX) | |
e28b88a4 JP |
2016 | sprintf(out + done, "/%d", mask); |
2017 | ||
2018 | sprintf(namefrm,"\n %s %%s", name); | |
2019 | print_string(PRINT_ANY, name, namefrm, out); | |
f888f4e2 SH |
2020 | } |
2021 | ||
e28b88a4 | 2022 | static void flower_print_u8(const char *name, struct rtattr *attr) |
7638ee13 | 2023 | { |
e28b88a4 | 2024 | flower_print_masked_u8(name, attr, NULL, NULL); |
7638ee13 BL |
2025 | } |
2026 | ||
e28b88a4 | 2027 | static void flower_print_u32(const char *name, struct rtattr *attr) |
7638ee13 | 2028 | { |
e28b88a4 | 2029 | SPRINT_BUF(namefrm); |
7638ee13 BL |
2030 | |
2031 | if (!attr) | |
2032 | return; | |
2033 | ||
e28b88a4 JP |
2034 | sprintf(namefrm,"\n %s %%u", name); |
2035 | print_uint(PRINT_ANY, name, namefrm, rta_getattr_u32(attr)); | |
7638ee13 BL |
2036 | } |
2037 | ||
e28b88a4 | 2038 | static void flower_print_arp_op(const char *name, |
9d36e54f SH |
2039 | struct rtattr *op_attr, |
2040 | struct rtattr *mask_attr) | |
2041 | { | |
e28b88a4 | 2042 | flower_print_masked_u8(name, op_attr, mask_attr, |
9d36e54f SH |
2043 | flower_print_arp_op_to_name); |
2044 | } | |
2045 | ||
30eb304e JP |
2046 | static int flower_print_opt(struct filter_util *qu, FILE *f, |
2047 | struct rtattr *opt, __u32 handle) | |
2048 | { | |
2049 | struct rtattr *tb[TCA_FLOWER_MAX + 1]; | |
8930840e | 2050 | __be16 min_port_type, max_port_type; |
6374961a | 2051 | int nl_type, nl_mask_type; |
30eb304e JP |
2052 | __be16 eth_type = 0; |
2053 | __u8 ip_proto = 0xff; | |
2054 | ||
2055 | if (!opt) | |
2056 | return 0; | |
2057 | ||
2058 | parse_rtattr_nested(tb, TCA_FLOWER_MAX, opt); | |
2059 | ||
2060 | if (handle) | |
e28b88a4 | 2061 | print_uint(PRINT_ANY, "handle", "handle 0x%x ", handle); |
30eb304e JP |
2062 | |
2063 | if (tb[TCA_FLOWER_CLASSID]) { | |
0d575c4d AN |
2064 | __u32 h = rta_getattr_u32(tb[TCA_FLOWER_CLASSID]); |
2065 | ||
2066 | if (TC_H_MIN(h) < TC_H_MIN_PRIORITY || | |
2067 | TC_H_MIN(h) > (TC_H_MIN_PRIORITY + TC_QOPT_MAX_QUEUE - 1)) { | |
2068 | SPRINT_BUF(b1); | |
e28b88a4 JP |
2069 | print_string(PRINT_ANY, "classid", "classid %s ", |
2070 | sprint_tc_classid(h, b1)); | |
0d575c4d | 2071 | } else { |
e28b88a4 JP |
2072 | print_uint(PRINT_ANY, "hw_tc", "hw_tc %u ", |
2073 | TC_H_MIN(h) - TC_H_MIN_PRIORITY); | |
0d575c4d | 2074 | } |
30eb304e JP |
2075 | } |
2076 | ||
2077 | if (tb[TCA_FLOWER_INDEV]) { | |
2078 | struct rtattr *attr = tb[TCA_FLOWER_INDEV]; | |
2079 | ||
e28b88a4 JP |
2080 | print_string(PRINT_ANY, "indev", "\n indev %s", |
2081 | rta_getattr_str(attr)); | |
30eb304e JP |
2082 | } |
2083 | ||
e28b88a4 JP |
2084 | open_json_object("keys"); |
2085 | ||
745d9172 HHZ |
2086 | if (tb[TCA_FLOWER_KEY_VLAN_ID]) { |
2087 | struct rtattr *attr = tb[TCA_FLOWER_KEY_VLAN_ID]; | |
2088 | ||
e28b88a4 JP |
2089 | print_uint(PRINT_ANY, "vlan_id", "\n vlan_id %u", |
2090 | rta_getattr_u16(attr)); | |
745d9172 HHZ |
2091 | } |
2092 | ||
2093 | if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) { | |
2094 | struct rtattr *attr = tb[TCA_FLOWER_KEY_VLAN_PRIO]; | |
2095 | ||
e28b88a4 JP |
2096 | print_uint(PRINT_ANY, "vlan_prio", "\n vlan_prio %d", |
2097 | rta_getattr_u8(attr)); | |
745d9172 HHZ |
2098 | } |
2099 | ||
1f0a5dfd JL |
2100 | if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) { |
2101 | SPRINT_BUF(buf); | |
2102 | struct rtattr *attr = tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]; | |
2103 | ||
2104 | print_string(PRINT_ANY, "vlan_ethtype", "\n vlan_ethtype %s", | |
2105 | ll_proto_n2a(rta_getattr_u16(attr), | |
2106 | buf, sizeof(buf))); | |
2107 | } | |
2108 | ||
2109 | if (tb[TCA_FLOWER_KEY_CVLAN_ID]) { | |
2110 | struct rtattr *attr = tb[TCA_FLOWER_KEY_CVLAN_ID]; | |
2111 | ||
2112 | print_uint(PRINT_ANY, "cvlan_id", "\n cvlan_id %u", | |
2113 | rta_getattr_u16(attr)); | |
2114 | } | |
2115 | ||
2116 | if (tb[TCA_FLOWER_KEY_CVLAN_PRIO]) { | |
2117 | struct rtattr *attr = tb[TCA_FLOWER_KEY_CVLAN_PRIO]; | |
2118 | ||
2119 | print_uint(PRINT_ANY, "cvlan_prio", "\n cvlan_prio %d", | |
2120 | rta_getattr_u8(attr)); | |
2121 | } | |
2122 | ||
2123 | if (tb[TCA_FLOWER_KEY_CVLAN_ETH_TYPE]) { | |
2124 | SPRINT_BUF(buf); | |
2125 | struct rtattr *attr = tb[TCA_FLOWER_KEY_CVLAN_ETH_TYPE]; | |
2126 | ||
2127 | print_string(PRINT_ANY, "cvlan_ethtype", "\n cvlan_ethtype %s", | |
2128 | ll_proto_n2a(rta_getattr_u16(attr), | |
2129 | buf, sizeof(buf))); | |
2130 | } | |
2131 | ||
e28b88a4 | 2132 | flower_print_eth_addr("dst_mac", tb[TCA_FLOWER_KEY_ETH_DST], |
30eb304e | 2133 | tb[TCA_FLOWER_KEY_ETH_DST_MASK]); |
e28b88a4 | 2134 | flower_print_eth_addr("src_mac", tb[TCA_FLOWER_KEY_ETH_SRC], |
30eb304e JP |
2135 | tb[TCA_FLOWER_KEY_ETH_SRC_MASK]); |
2136 | ||
e28b88a4 JP |
2137 | flower_print_eth_type(ð_type, tb[TCA_FLOWER_KEY_ETH_TYPE]); |
2138 | flower_print_ip_proto(&ip_proto, tb[TCA_FLOWER_KEY_IP_PROTO]); | |
30eb304e | 2139 | |
e28b88a4 | 2140 | flower_print_ip_attr("ip_tos", tb[TCA_FLOWER_KEY_IP_TOS], |
6ea2c2b1 | 2141 | tb[TCA_FLOWER_KEY_IP_TOS_MASK]); |
e28b88a4 | 2142 | flower_print_ip_attr("ip_ttl", tb[TCA_FLOWER_KEY_IP_TTL], |
6ea2c2b1 OG |
2143 | tb[TCA_FLOWER_KEY_IP_TTL_MASK]); |
2144 | ||
e28b88a4 JP |
2145 | flower_print_u32("mpls_label", tb[TCA_FLOWER_KEY_MPLS_LABEL]); |
2146 | flower_print_u8("mpls_tc", tb[TCA_FLOWER_KEY_MPLS_TC]); | |
2147 | flower_print_u8("mpls_bos", tb[TCA_FLOWER_KEY_MPLS_BOS]); | |
2148 | flower_print_u8("mpls_ttl", tb[TCA_FLOWER_KEY_MPLS_TTL]); | |
7638ee13 | 2149 | |
e28b88a4 | 2150 | flower_print_ip_addr("dst_ip", eth_type, |
30eb304e JP |
2151 | tb[TCA_FLOWER_KEY_IPV4_DST], |
2152 | tb[TCA_FLOWER_KEY_IPV4_DST_MASK], | |
2153 | tb[TCA_FLOWER_KEY_IPV6_DST], | |
2154 | tb[TCA_FLOWER_KEY_IPV6_DST_MASK]); | |
2155 | ||
e28b88a4 | 2156 | flower_print_ip_addr("src_ip", eth_type, |
30eb304e JP |
2157 | tb[TCA_FLOWER_KEY_IPV4_SRC], |
2158 | tb[TCA_FLOWER_KEY_IPV4_SRC_MASK], | |
2159 | tb[TCA_FLOWER_KEY_IPV6_SRC], | |
2160 | tb[TCA_FLOWER_KEY_IPV6_SRC_MASK]); | |
2161 | ||
b2141de1 | 2162 | nl_type = flower_port_attr_type(ip_proto, FLOWER_ENDPOINT_DST); |
482fd40a | 2163 | nl_mask_type = flower_port_attr_mask_type(ip_proto, FLOWER_ENDPOINT_DST); |
6bd5b80c | 2164 | if (nl_type >= 0) |
482fd40a | 2165 | flower_print_port("dst_port", tb[nl_type], tb[nl_mask_type]); |
b2141de1 | 2166 | nl_type = flower_port_attr_type(ip_proto, FLOWER_ENDPOINT_SRC); |
482fd40a | 2167 | nl_mask_type = flower_port_attr_mask_type(ip_proto, FLOWER_ENDPOINT_SRC); |
6bd5b80c | 2168 | if (nl_type >= 0) |
482fd40a | 2169 | flower_print_port("src_port", tb[nl_type], tb[nl_mask_type]); |
30eb304e | 2170 | |
8930840e AN |
2171 | if (!flower_port_range_attr_type(ip_proto, FLOWER_ENDPOINT_DST, |
2172 | &min_port_type, &max_port_type)) | |
2173 | flower_print_port_range("dst_port", | |
2174 | tb[min_port_type], tb[max_port_type]); | |
2175 | ||
2176 | if (!flower_port_range_attr_type(ip_proto, FLOWER_ENDPOINT_SRC, | |
2177 | &min_port_type, &max_port_type)) | |
2178 | flower_print_port_range("src_port", | |
2179 | tb[min_port_type], tb[max_port_type]); | |
2180 | ||
e28b88a4 | 2181 | flower_print_tcp_flags("tcp_flags", tb[TCA_FLOWER_KEY_TCP_FLAGS], |
0c30d14d JP |
2182 | tb[TCA_FLOWER_KEY_TCP_FLAGS_MASK]); |
2183 | ||
81f6e5a7 SH |
2184 | nl_type = flower_icmp_attr_type(eth_type, ip_proto, |
2185 | FLOWER_ICMP_FIELD_TYPE); | |
6374961a SH |
2186 | nl_mask_type = flower_icmp_attr_mask_type(eth_type, ip_proto, |
2187 | FLOWER_ICMP_FIELD_TYPE); | |
2188 | if (nl_type >= 0 && nl_mask_type >= 0) | |
e28b88a4 | 2189 | flower_print_masked_u8("icmp_type", tb[nl_type], |
6374961a SH |
2190 | tb[nl_mask_type], NULL); |
2191 | ||
81f6e5a7 SH |
2192 | nl_type = flower_icmp_attr_type(eth_type, ip_proto, |
2193 | FLOWER_ICMP_FIELD_CODE); | |
6374961a SH |
2194 | nl_mask_type = flower_icmp_attr_mask_type(eth_type, ip_proto, |
2195 | FLOWER_ICMP_FIELD_CODE); | |
2196 | if (nl_type >= 0 && nl_mask_type >= 0) | |
e28b88a4 | 2197 | flower_print_masked_u8("icmp_code", tb[nl_type], |
6374961a | 2198 | tb[nl_mask_type], NULL); |
eb3b5696 | 2199 | |
e28b88a4 | 2200 | flower_print_ip4_addr("arp_sip", tb[TCA_FLOWER_KEY_ARP_SIP], |
f888f4e2 | 2201 | tb[TCA_FLOWER_KEY_ARP_SIP_MASK]); |
e28b88a4 | 2202 | flower_print_ip4_addr("arp_tip", tb[TCA_FLOWER_KEY_ARP_TIP], |
f888f4e2 | 2203 | tb[TCA_FLOWER_KEY_ARP_TIP_MASK]); |
e28b88a4 | 2204 | flower_print_arp_op("arp_op", tb[TCA_FLOWER_KEY_ARP_OP], |
f888f4e2 | 2205 | tb[TCA_FLOWER_KEY_ARP_OP_MASK]); |
e28b88a4 | 2206 | flower_print_eth_addr("arp_sha", tb[TCA_FLOWER_KEY_ARP_SHA], |
f888f4e2 | 2207 | tb[TCA_FLOWER_KEY_ARP_SHA_MASK]); |
e28b88a4 | 2208 | flower_print_eth_addr("arp_tha", tb[TCA_FLOWER_KEY_ARP_THA], |
f888f4e2 SH |
2209 | tb[TCA_FLOWER_KEY_ARP_THA_MASK]); |
2210 | ||
e28b88a4 | 2211 | flower_print_ip_addr("enc_dst_ip", |
bb9b63b1 AV |
2212 | tb[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] ? |
2213 | htons(ETH_P_IP) : htons(ETH_P_IPV6), | |
2214 | tb[TCA_FLOWER_KEY_ENC_IPV4_DST], | |
2215 | tb[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK], | |
2216 | tb[TCA_FLOWER_KEY_ENC_IPV6_DST], | |
2217 | tb[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK]); | |
2218 | ||
e28b88a4 | 2219 | flower_print_ip_addr("enc_src_ip", |
bb9b63b1 AV |
2220 | tb[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] ? |
2221 | htons(ETH_P_IP) : htons(ETH_P_IPV6), | |
2222 | tb[TCA_FLOWER_KEY_ENC_IPV4_SRC], | |
2223 | tb[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK], | |
2224 | tb[TCA_FLOWER_KEY_ENC_IPV6_SRC], | |
2225 | tb[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK]); | |
2226 | ||
e28b88a4 | 2227 | flower_print_key_id("enc_key_id", tb[TCA_FLOWER_KEY_ENC_KEY_ID]); |
bb9b63b1 | 2228 | |
482fd40a EB |
2229 | flower_print_port("enc_dst_port", tb[TCA_FLOWER_KEY_ENC_UDP_DST_PORT], |
2230 | tb[TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK]); | |
41aa17ff | 2231 | |
761ec9e2 OG |
2232 | flower_print_ip_attr("enc_tos", tb[TCA_FLOWER_KEY_ENC_IP_TOS], |
2233 | tb[TCA_FLOWER_KEY_ENC_IP_TOS_MASK]); | |
2234 | flower_print_ip_attr("enc_ttl", tb[TCA_FLOWER_KEY_ENC_IP_TTL], | |
2235 | tb[TCA_FLOWER_KEY_ENC_IP_TTL_MASK]); | |
56155d4d PJV |
2236 | flower_print_enc_opts("enc_opt", tb[TCA_FLOWER_KEY_ENC_OPTS], |
2237 | tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); | |
761ec9e2 | 2238 | |
e28b88a4 | 2239 | flower_print_matching_flags("ip_flags", FLOWER_IP_FLAGS, |
22a8f019 PB |
2240 | tb[TCA_FLOWER_KEY_FLAGS], |
2241 | tb[TCA_FLOWER_KEY_FLAGS_MASK]); | |
2242 | ||
2fffb1c0 PB |
2243 | flower_print_ct_state(tb[TCA_FLOWER_KEY_CT_STATE], |
2244 | tb[TCA_FLOWER_KEY_CT_STATE_MASK]); | |
2245 | flower_print_ct_zone(tb[TCA_FLOWER_KEY_CT_ZONE], | |
2246 | tb[TCA_FLOWER_KEY_CT_ZONE_MASK]); | |
2247 | flower_print_ct_mark(tb[TCA_FLOWER_KEY_CT_MARK], | |
2248 | tb[TCA_FLOWER_KEY_CT_MARK_MASK]); | |
2249 | flower_print_ct_label(tb[TCA_FLOWER_KEY_CT_LABELS], | |
2250 | tb[TCA_FLOWER_KEY_CT_LABELS_MASK]); | |
2251 | ||
e28b88a4 JP |
2252 | close_json_object(); |
2253 | ||
512caeb2 | 2254 | if (tb[TCA_FLOWER_FLAGS]) { |
cfcabf18 AV |
2255 | __u32 flags = rta_getattr_u32(tb[TCA_FLOWER_FLAGS]); |
2256 | ||
2257 | if (flags & TCA_CLS_FLAGS_SKIP_HW) | |
e28b88a4 | 2258 | print_bool(PRINT_ANY, "skip_hw", "\n skip_hw", true); |
cfcabf18 | 2259 | if (flags & TCA_CLS_FLAGS_SKIP_SW) |
e28b88a4 | 2260 | print_bool(PRINT_ANY, "skip_sw", "\n skip_sw", true); |
e57285b8 | 2261 | |
f6b498f9 | 2262 | if (flags & TCA_CLS_FLAGS_IN_HW) { |
e28b88a4 | 2263 | print_bool(PRINT_ANY, "in_hw", "\n in_hw", true); |
f6b498f9 VB |
2264 | |
2265 | if (tb[TCA_FLOWER_IN_HW_COUNT]) { | |
2266 | __u32 count = rta_getattr_u32(tb[TCA_FLOWER_IN_HW_COUNT]); | |
2267 | ||
2268 | print_uint(PRINT_ANY, "in_hw_count", | |
2269 | " in_hw_count %u", count); | |
2270 | } | |
2271 | } | |
e57285b8 | 2272 | else if (flags & TCA_CLS_FLAGS_NOT_IN_HW) |
e28b88a4 | 2273 | print_bool(PRINT_ANY, "not_in_hw", "\n not_in_hw", true); |
cfcabf18 AV |
2274 | } |
2275 | ||
512caeb2 | 2276 | if (tb[TCA_FLOWER_ACT]) |
9e713525 | 2277 | tc_print_action(f, tb[TCA_FLOWER_ACT], 0); |
30eb304e JP |
2278 | |
2279 | return 0; | |
2280 | } | |
2281 | ||
2282 | struct filter_util flower_filter_util = { | |
2283 | .id = "flower", | |
2284 | .parse_fopt = flower_parse_opt, | |
2285 | .print_fopt = flower_print_opt, | |
2286 | }; |