]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/f_flower.c
update kernel headers (from 4.10-rc4)
[mirror_iproute2.git] / tc / f_flower.c
CommitLineData
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>
15#include <syslog.h>
16#include <string.h>
17#include <net/if.h>
18#include <linux/if_ether.h>
19#include <linux/ip.h>
745d9172 20#include <linux/tc_act/tc_vlan.h>
30eb304e
JP
21
22#include "utils.h"
23#include "tc_util.h"
24#include "rt_names.h"
25
6910d656
SH
26enum flower_endpoint {
27 FLOWER_ENDPOINT_SRC,
28 FLOWER_ENDPOINT_DST
29};
30
eb3b5696
SH
31enum flower_icmp_field {
32 FLOWER_ICMP_FIELD_TYPE,
33 FLOWER_ICMP_FIELD_CODE
34};
35
30eb304e
JP
36static void explain(void)
37{
512caeb2
SH
38 fprintf(stderr,
39 "Usage: ... flower [ MATCH-LIST ]\n"
40 " [ skip_sw | skip_hw ]\n"
41 " [ action ACTION-SPEC ] [ classid CLASSID ]\n"
42 "\n"
43 "Where: MATCH-LIST := [ MATCH-LIST ] MATCH\n"
44 " MATCH := { indev DEV-NAME |\n"
45 " vlan_id VID |\n"
46 " vlan_prio PRIORITY |\n"
47 " vlan_ethtype [ ipv4 | ipv6 | ETH-TYPE ] |\n"
8578bb73
SH
48 " dst_mac MAC-ADDR |\n"
49 " src_mac MAC-ADDR |\n"
eb3b5696 50 " ip_proto [tcp | udp | sctp | icmp | icmpv6 | IP-PROTO ] |\n"
10da5528
SH
51 " dst_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
52 " src_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
512caeb2 53 " dst_port PORT-NUMBER |\n"
bb9b63b1 54 " src_port PORT-NUMBER |\n"
eb3b5696 55 " type ICMP-TYPE |\n"
bf73c650 56 " code ICMP-CODE |\n"
10da5528
SH
57 " enc_dst_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
58 " enc_src_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
bf73c650
HHZ
59 " enc_key_id [ KEY-ID ] |\n"
60 " enc_dst_port [ UDP-PORT ] }\n"
512caeb2
SH
61 " FILTERID := X:Y:Z\n"
62 " ACTION-SPEC := ... look at individual actions\n"
63 "\n"
328374dc 64 "NOTE: CLASSID, IP-PROTO are parsed as hexadecimal input.\n"
512caeb2
SH
65 "NOTE: There can be only used one mask per one prio. If user needs\n"
66 " to specify different mask, he has to use different prio.\n");
30eb304e
JP
67}
68
69static int flower_parse_eth_addr(char *str, int addr_type, int mask_type,
70 struct nlmsghdr *n)
71{
8578bb73
SH
72 int ret;
73 char addr[ETH_ALEN];
30eb304e
JP
74
75 ret = ll_addr_a2n(addr, sizeof(addr), str);
76 if (ret < 0)
8578bb73 77 return -1;
30eb304e 78 addattr_l(n, MAX_MSG, addr_type, addr, sizeof(addr));
8578bb73 79 memset(addr, 0xff, ETH_ALEN);
30eb304e 80 addattr_l(n, MAX_MSG, mask_type, addr, sizeof(addr));
8578bb73 81 return 0;
30eb304e
JP
82}
83
745d9172 84static int flower_parse_vlan_eth_type(char *str, __be16 eth_type, int type,
512caeb2
SH
85 __be16 *p_vlan_eth_type,
86 struct nlmsghdr *n)
745d9172
HHZ
87{
88 __be16 vlan_eth_type;
89
90 if (eth_type != htons(ETH_P_8021Q)) {
512caeb2
SH
91 fprintf(stderr,
92 "Can't set \"vlan_ethtype\" if ethertype isn't 802.1Q\n");
745d9172
HHZ
93 return -1;
94 }
95
96 if (ll_proto_a2n(&vlan_eth_type, str))
97 invarg("invalid vlan_ethtype", str);
98 addattr16(n, MAX_MSG, type, vlan_eth_type);
99 *p_vlan_eth_type = vlan_eth_type;
100 return 0;
101}
102
30eb304e
JP
103static int flower_parse_ip_proto(char *str, __be16 eth_type, int type,
104 __u8 *p_ip_proto, struct nlmsghdr *n)
105{
106 int ret;
107 __u8 ip_proto;
108
eb3b5696
SH
109 if (eth_type != htons(ETH_P_IP) && eth_type != htons(ETH_P_IPV6))
110 goto err;
111
30eb304e
JP
112 if (matches(str, "tcp") == 0) {
113 ip_proto = IPPROTO_TCP;
114 } else if (matches(str, "udp") == 0) {
115 ip_proto = IPPROTO_UDP;
a1fb0d48
SH
116 } else if (matches(str, "sctp") == 0) {
117 ip_proto = IPPROTO_SCTP;
eb3b5696
SH
118 } else if (matches(str, "icmp") == 0) {
119 if (eth_type != htons(ETH_P_IP))
120 goto err;
121 ip_proto = IPPROTO_ICMP;
122 } else if (matches(str, "icmpv6") == 0) {
123 if (eth_type != htons(ETH_P_IPV6))
124 goto err;
125 ip_proto = IPPROTO_ICMPV6;
30eb304e
JP
126 } else {
127 ret = get_u8(&ip_proto, str, 16);
128 if (ret)
129 return -1;
130 }
131 addattr8(n, MAX_MSG, type, ip_proto);
132 *p_ip_proto = ip_proto;
133 return 0;
eb3b5696
SH
134
135err:
136 fprintf(stderr, "Illegal \"eth_type\" for ip proto\n");
137 return -1;
30eb304e
JP
138}
139
140static int flower_parse_ip_addr(char *str, __be16 eth_type,
141 int addr4_type, int mask4_type,
142 int addr6_type, int mask6_type,
143 struct nlmsghdr *n)
144{
145 int ret;
146 inet_prefix addr;
147 int family;
148 int bits;
149 int i;
150
151 if (eth_type == htons(ETH_P_IP)) {
152 family = AF_INET;
153 } else if (eth_type == htons(ETH_P_IPV6)) {
154 family = AF_INET6;
bb9b63b1
AV
155 } else if (!eth_type) {
156 family = AF_UNSPEC;
30eb304e 157 } else {
30eb304e
JP
158 return -1;
159 }
160
161 ret = get_prefix(&addr, str, family);
162 if (ret)
163 return -1;
164
bb9b63b1
AV
165 if (family && (addr.family != family)) {
166 fprintf(stderr, "Illegal \"eth_type\" for ip address\n");
30eb304e 167 return -1;
bb9b63b1 168 }
30eb304e
JP
169
170 addattr_l(n, MAX_MSG, addr.family == AF_INET ? addr4_type : addr6_type,
171 addr.data, addr.bytelen);
172
173 memset(addr.data, 0xff, addr.bytelen);
174 bits = addr.bitlen;
175 for (i = 0; i < addr.bytelen / 4; i++) {
176 if (!bits) {
177 addr.data[i] = 0;
178 } else if (bits / 32 >= 1) {
179 bits -= 32;
180 } else {
181 addr.data[i] <<= 32 - bits;
182 addr.data[i] = htonl(addr.data[i]);
183 bits = 0;
184 }
185 }
186
187 addattr_l(n, MAX_MSG, addr.family == AF_INET ? mask4_type : mask6_type,
188 addr.data, addr.bytelen);
189
190 return 0;
191}
192
eb3b5696
SH
193static int flower_icmp_attr_type(__be16 eth_type, __u8 ip_proto,
194 enum flower_icmp_field field)
195{
196 if (eth_type == htons(ETH_P_IP) && ip_proto == IPPROTO_ICMP)
197 return field == FLOWER_ICMP_FIELD_CODE ?
198 TCA_FLOWER_KEY_ICMPV4_CODE :
199 TCA_FLOWER_KEY_ICMPV4_TYPE;
200 else if (eth_type == htons(ETH_P_IPV6) && ip_proto == IPPROTO_ICMPV6)
201 return field == FLOWER_ICMP_FIELD_CODE ?
202 TCA_FLOWER_KEY_ICMPV6_CODE :
203 TCA_FLOWER_KEY_ICMPV6_TYPE;
204
205 return -1;
206}
207
208static int flower_parse_icmp(char *str, __u16 eth_type, __u8 ip_proto,
209 enum flower_icmp_field field, struct nlmsghdr *n)
210{
211 int ret;
212 int type;
213 uint8_t value;
214
215 type = flower_icmp_attr_type(eth_type, ip_proto, field);
216 if (type < 0)
217 return -1;
218
219 ret = get_u8(&value, str, 10);
220 if (ret)
221 return -1;
222
223 addattr8(n, MAX_MSG, type, value);
224
225 return 0;
226}
227
6910d656 228static int flower_port_attr_type(__u8 ip_proto, enum flower_endpoint endpoint)
30eb304e 229{
6bd5b80c 230 if (ip_proto == IPPROTO_TCP)
6910d656
SH
231 return endpoint == FLOWER_ENDPOINT_SRC ?
232 TCA_FLOWER_KEY_TCP_SRC :
a1fb0d48 233 TCA_FLOWER_KEY_TCP_DST;
6bd5b80c 234 else if (ip_proto == IPPROTO_UDP)
6910d656
SH
235 return endpoint == FLOWER_ENDPOINT_SRC ?
236 TCA_FLOWER_KEY_UDP_SRC :
a1fb0d48 237 TCA_FLOWER_KEY_UDP_DST;
6bd5b80c 238 else if (ip_proto == IPPROTO_SCTP)
6910d656
SH
239 return endpoint == FLOWER_ENDPOINT_SRC ?
240 TCA_FLOWER_KEY_SCTP_SRC :
a1fb0d48 241 TCA_FLOWER_KEY_SCTP_DST;
6bd5b80c 242 else
30eb304e 243 return -1;
a1fb0d48
SH
244}
245
6910d656
SH
246static int flower_parse_port(char *str, __u8 ip_proto,
247 enum flower_endpoint endpoint,
a1fb0d48
SH
248 struct nlmsghdr *n)
249{
250 int ret;
251 int type;
252 __be16 port;
253
6910d656 254 type = flower_port_attr_type(ip_proto, endpoint);
a1fb0d48
SH
255 if (type < 0)
256 return -1;
30eb304e 257
9f7401fa 258 ret = get_be16(&port, str, 10);
30eb304e
JP
259 if (ret)
260 return -1;
261
9f7401fa 262 addattr16(n, MAX_MSG, type, port);
30eb304e
JP
263
264 return 0;
265}
266
bb9b63b1
AV
267static int flower_parse_key_id(const char *str, int type, struct nlmsghdr *n)
268{
269 int ret;
270 __be32 key_id;
271
272 ret = get_be32(&key_id, str, 10);
273 if (!ret)
274 addattr32(n, MAX_MSG, type, key_id);
275
276 return ret;
277}
278
41aa17ff
HHZ
279static int flower_parse_enc_port(char *str, int type, struct nlmsghdr *n)
280{
281 int ret;
282 __be16 port;
283
284 ret = get_be16(&port, str, 10);
285 if (ret)
286 return -1;
287
288 addattr16(n, MAX_MSG, type, port);
289
290 return 0;
291}
292
30eb304e
JP
293static int flower_parse_opt(struct filter_util *qu, char *handle,
294 int argc, char **argv, struct nlmsghdr *n)
295{
296 int ret;
297 struct tcmsg *t = NLMSG_DATA(n);
298 struct rtattr *tail;
488b41d0 299 __be16 eth_type = TC_H_MIN(t->tcm_info);
745d9172 300 __be16 vlan_ethtype = 0;
30eb304e 301 __u8 ip_proto = 0xff;
cfcabf18 302 __u32 flags = 0;
30eb304e 303
30eb304e
JP
304 if (handle) {
305 ret = get_u32(&t->tcm_handle, handle, 0);
306 if (ret) {
307 fprintf(stderr, "Illegal \"handle\"\n");
308 return -1;
309 }
310 }
311
312 tail = (struct rtattr *) (((void *) n) + NLMSG_ALIGN(n->nlmsg_len));
313 addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0);
314
488b41d0
JHS
315 if (argc == 0) {
316 /*at minimal we will match all ethertype packets */
317 goto parse_done;
318 }
319
30eb304e
JP
320 while (argc > 0) {
321 if (matches(*argv, "classid") == 0 ||
322 matches(*argv, "flowid") == 0) {
32a121cb 323 unsigned int handle;
30eb304e
JP
324
325 NEXT_ARG();
326 ret = get_tc_classid(&handle, *argv);
327 if (ret) {
328 fprintf(stderr, "Illegal \"classid\"\n");
329 return -1;
330 }
331 addattr_l(n, MAX_MSG, TCA_FLOWER_CLASSID, &handle, 4);
cfcabf18
AV
332 } else if (matches(*argv, "skip_hw") == 0) {
333 flags |= TCA_CLS_FLAGS_SKIP_HW;
334 } else if (matches(*argv, "skip_sw") == 0) {
335 flags |= TCA_CLS_FLAGS_SKIP_SW;
30eb304e 336 } else if (matches(*argv, "indev") == 0) {
d17b136f 337 char ifname[IFNAMSIZ] = {};
30eb304e
JP
338
339 NEXT_ARG();
30eb304e
JP
340 strncpy(ifname, *argv, sizeof(ifname) - 1);
341 addattrstrz(n, MAX_MSG, TCA_FLOWER_INDEV, ifname);
745d9172
HHZ
342 } else if (matches(*argv, "vlan_id") == 0) {
343 __u16 vid;
344
345 NEXT_ARG();
346 if (eth_type != htons(ETH_P_8021Q)) {
512caeb2
SH
347 fprintf(stderr,
348 "Can't set \"vlan_id\" if ethertype isn't 802.1Q\n");
745d9172
HHZ
349 return -1;
350 }
351 ret = get_u16(&vid, *argv, 10);
352 if (ret < 0 || vid & ~0xfff) {
353 fprintf(stderr, "Illegal \"vlan_id\"\n");
354 return -1;
355 }
356 addattr16(n, MAX_MSG, TCA_FLOWER_KEY_VLAN_ID, vid);
357 } else if (matches(*argv, "vlan_prio") == 0) {
358 __u8 vlan_prio;
359
360 NEXT_ARG();
361 if (eth_type != htons(ETH_P_8021Q)) {
512caeb2
SH
362 fprintf(stderr,
363 "Can't set \"vlan_prio\" if ethertype isn't 802.1Q\n");
745d9172
HHZ
364 return -1;
365 }
366 ret = get_u8(&vlan_prio, *argv, 10);
367 if (ret < 0 || vlan_prio & ~0x7) {
368 fprintf(stderr, "Illegal \"vlan_prio\"\n");
369 return -1;
370 }
512caeb2
SH
371 addattr8(n, MAX_MSG,
372 TCA_FLOWER_KEY_VLAN_PRIO, vlan_prio);
745d9172
HHZ
373 } else if (matches(*argv, "vlan_ethtype") == 0) {
374 NEXT_ARG();
375 ret = flower_parse_vlan_eth_type(*argv, eth_type,
512caeb2
SH
376 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
377 &vlan_ethtype, n);
745d9172
HHZ
378 if (ret < 0)
379 return -1;
30eb304e
JP
380 } else if (matches(*argv, "dst_mac") == 0) {
381 NEXT_ARG();
382 ret = flower_parse_eth_addr(*argv,
383 TCA_FLOWER_KEY_ETH_DST,
384 TCA_FLOWER_KEY_ETH_DST_MASK,
385 n);
386 if (ret < 0) {
387 fprintf(stderr, "Illegal \"dst_mac\"\n");
388 return -1;
389 }
390 } else if (matches(*argv, "src_mac") == 0) {
391 NEXT_ARG();
392 ret = flower_parse_eth_addr(*argv,
393 TCA_FLOWER_KEY_ETH_SRC,
394 TCA_FLOWER_KEY_ETH_SRC_MASK,
395 n);
396 if (ret < 0) {
397 fprintf(stderr, "Illegal \"src_mac\"\n");
398 return -1;
399 }
30eb304e
JP
400 } else if (matches(*argv, "ip_proto") == 0) {
401 NEXT_ARG();
745d9172
HHZ
402 ret = flower_parse_ip_proto(*argv, vlan_ethtype ?
403 vlan_ethtype : eth_type,
30eb304e
JP
404 TCA_FLOWER_KEY_IP_PROTO,
405 &ip_proto, n);
406 if (ret < 0) {
407 fprintf(stderr, "Illegal \"ip_proto\"\n");
408 return -1;
409 }
410 } else if (matches(*argv, "dst_ip") == 0) {
411 NEXT_ARG();
745d9172
HHZ
412 ret = flower_parse_ip_addr(*argv, vlan_ethtype ?
413 vlan_ethtype : eth_type,
30eb304e
JP
414 TCA_FLOWER_KEY_IPV4_DST,
415 TCA_FLOWER_KEY_IPV4_DST_MASK,
416 TCA_FLOWER_KEY_IPV6_DST,
417 TCA_FLOWER_KEY_IPV6_DST_MASK,
418 n);
419 if (ret < 0) {
420 fprintf(stderr, "Illegal \"dst_ip\"\n");
421 return -1;
422 }
423 } else if (matches(*argv, "src_ip") == 0) {
424 NEXT_ARG();
745d9172
HHZ
425 ret = flower_parse_ip_addr(*argv, vlan_ethtype ?
426 vlan_ethtype : eth_type,
30eb304e
JP
427 TCA_FLOWER_KEY_IPV4_SRC,
428 TCA_FLOWER_KEY_IPV4_SRC_MASK,
429 TCA_FLOWER_KEY_IPV6_SRC,
430 TCA_FLOWER_KEY_IPV6_SRC_MASK,
431 n);
432 if (ret < 0) {
433 fprintf(stderr, "Illegal \"src_ip\"\n");
434 return -1;
435 }
436 } else if (matches(*argv, "dst_port") == 0) {
437 NEXT_ARG();
6910d656
SH
438 ret = flower_parse_port(*argv, ip_proto,
439 FLOWER_ENDPOINT_DST, n);
30eb304e
JP
440 if (ret < 0) {
441 fprintf(stderr, "Illegal \"dst_port\"\n");
442 return -1;
443 }
444 } else if (matches(*argv, "src_port") == 0) {
445 NEXT_ARG();
6910d656
SH
446 ret = flower_parse_port(*argv, ip_proto,
447 FLOWER_ENDPOINT_SRC, n);
30eb304e
JP
448 if (ret < 0) {
449 fprintf(stderr, "Illegal \"src_port\"\n");
450 return -1;
451 }
eb3b5696
SH
452 } else if (matches(*argv, "type") == 0) {
453 NEXT_ARG();
454 ret = flower_parse_icmp(*argv, eth_type, ip_proto,
455 FLOWER_ICMP_FIELD_TYPE, n);
456 if (ret < 0) {
457 fprintf(stderr, "Illegal \"icmp type\"\n");
458 return -1;
459 }
460 } else if (matches(*argv, "code") == 0) {
461 NEXT_ARG();
462 ret = flower_parse_icmp(*argv, eth_type, ip_proto,
463 FLOWER_ICMP_FIELD_CODE, n);
464 if (ret < 0) {
465 fprintf(stderr, "Illegal \"icmp code\"\n");
466 return -1;
467 }
bb9b63b1
AV
468 } else if (matches(*argv, "enc_dst_ip") == 0) {
469 NEXT_ARG();
470 ret = flower_parse_ip_addr(*argv, 0,
471 TCA_FLOWER_KEY_ENC_IPV4_DST,
472 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
473 TCA_FLOWER_KEY_ENC_IPV6_DST,
474 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
475 n);
476 if (ret < 0) {
477 fprintf(stderr, "Illegal \"enc_dst_ip\"\n");
478 return -1;
479 }
480 } else if (matches(*argv, "enc_src_ip") == 0) {
481 NEXT_ARG();
482 ret = flower_parse_ip_addr(*argv, 0,
483 TCA_FLOWER_KEY_ENC_IPV4_SRC,
484 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
485 TCA_FLOWER_KEY_ENC_IPV6_SRC,
486 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
487 n);
488 if (ret < 0) {
489 fprintf(stderr, "Illegal \"enc_src_ip\"\n");
490 return -1;
491 }
492 } else if (matches(*argv, "enc_key_id") == 0) {
493 NEXT_ARG();
494 ret = flower_parse_key_id(*argv,
495 TCA_FLOWER_KEY_ENC_KEY_ID, n);
496 if (ret < 0) {
497 fprintf(stderr, "Illegal \"enc_key_id\"\n");
498 return -1;
499 }
41aa17ff
HHZ
500 } else if (matches(*argv, "enc_dst_port") == 0) {
501 NEXT_ARG();
502 ret = flower_parse_enc_port(*argv,
503 TCA_FLOWER_KEY_ENC_UDP_DST_PORT, n);
504 if (ret < 0) {
505 fprintf(stderr, "Illegal \"enc_dst_port\"\n");
506 return -1;
507 }
30eb304e
JP
508 } else if (matches(*argv, "action") == 0) {
509 NEXT_ARG();
510 ret = parse_action(&argc, &argv, TCA_FLOWER_ACT, n);
511 if (ret) {
512 fprintf(stderr, "Illegal \"action\"\n");
513 return -1;
514 }
515 continue;
516 } else if (strcmp(*argv, "help") == 0) {
517 explain();
518 return -1;
519 } else {
520 fprintf(stderr, "What is \"%s\"?\n", *argv);
521 explain();
522 return -1;
523 }
524 argc--; argv++;
525 }
526
488b41d0 527parse_done:
cfcabf18
AV
528 addattr32(n, MAX_MSG, TCA_FLOWER_FLAGS, flags);
529
488b41d0
JHS
530 ret = addattr16(n, MAX_MSG, TCA_FLOWER_KEY_ETH_TYPE, eth_type);
531 if (ret) {
532 fprintf(stderr, "Illegal \"eth_type\"(0x%x)\n",
533 ntohs(eth_type));
534 return -1;
535 }
536
32a121cb 537 tail->rta_len = (((void *)n)+n->nlmsg_len) - (void *)tail;
30eb304e
JP
538
539 return 0;
540}
541
542static int __mask_bits(char *addr, size_t len)
543{
544 int bits = 0;
545 bool hole = false;
546 int i;
547 int j;
548
549 for (i = 0; i < len; i++, addr++) {
550 for (j = 7; j >= 0; j--) {
551 if (((*addr) >> j) & 0x1) {
552 if (hole)
553 return -1;
554 bits++;
555 } else if (bits) {
556 hole = true;
557 } else{
558 return -1;
559 }
560 }
561 }
562 return bits;
563}
564
565static void flower_print_eth_addr(FILE *f, char *name,
566 struct rtattr *addr_attr,
567 struct rtattr *mask_attr)
568{
569 SPRINT_BUF(b1);
570 int bits;
571
572 if (!addr_attr || RTA_PAYLOAD(addr_attr) != ETH_ALEN)
573 return;
574 fprintf(f, "\n %s %s", name, ll_addr_n2a(RTA_DATA(addr_attr), ETH_ALEN,
575 0, b1, sizeof(b1)));
576 if (!mask_attr || RTA_PAYLOAD(mask_attr) != ETH_ALEN)
577 return;
578 bits = __mask_bits(RTA_DATA(mask_attr), ETH_ALEN);
579 if (bits < 0)
580 fprintf(f, "/%s", ll_addr_n2a(RTA_DATA(mask_attr), ETH_ALEN,
581 0, b1, sizeof(b1)));
582 else if (bits < ETH_ALEN * 8)
583 fprintf(f, "/%d", bits);
584}
585
586static void flower_print_eth_type(FILE *f, __be16 *p_eth_type,
587 struct rtattr *eth_type_attr)
588{
589 __be16 eth_type;
590
591 if (!eth_type_attr)
592 return;
593
594 eth_type = rta_getattr_u16(eth_type_attr);
595 fprintf(f, "\n eth_type ");
596 if (eth_type == htons(ETH_P_IP))
597 fprintf(f, "ipv4");
598 else if (eth_type == htons(ETH_P_IPV6))
599 fprintf(f, "ipv6");
600 else
601 fprintf(f, "%04x", ntohs(eth_type));
602 *p_eth_type = eth_type;
603}
604
605static void flower_print_ip_proto(FILE *f, __u8 *p_ip_proto,
606 struct rtattr *ip_proto_attr)
607{
608 __u8 ip_proto;
609
610 if (!ip_proto_attr)
611 return;
612
613 ip_proto = rta_getattr_u8(ip_proto_attr);
614 fprintf(f, "\n ip_proto ");
615 if (ip_proto == IPPROTO_TCP)
616 fprintf(f, "tcp");
617 else if (ip_proto == IPPROTO_UDP)
618 fprintf(f, "udp");
a1fb0d48
SH
619 else if (ip_proto == IPPROTO_SCTP)
620 fprintf(f, "sctp");
eb3b5696
SH
621 else if (ip_proto == IPPROTO_ICMP)
622 fprintf(f, "icmp");
623 else if (ip_proto == IPPROTO_ICMPV6)
624 fprintf(f, "icmpv6");
30eb304e
JP
625 else
626 fprintf(f, "%02x", ip_proto);
627 *p_ip_proto = ip_proto;
628}
629
630static void flower_print_ip_addr(FILE *f, char *name, __be16 eth_type,
631 struct rtattr *addr4_attr,
632 struct rtattr *mask4_attr,
633 struct rtattr *addr6_attr,
634 struct rtattr *mask6_attr)
635{
30eb304e
JP
636 struct rtattr *addr_attr;
637 struct rtattr *mask_attr;
638 int family;
639 size_t len;
640 int bits;
641
642 if (eth_type == htons(ETH_P_IP)) {
643 family = AF_INET;
644 addr_attr = addr4_attr;
645 mask_attr = mask4_attr;
646 len = 4;
647 } else if (eth_type == htons(ETH_P_IPV6)) {
648 family = AF_INET6;
649 addr_attr = addr6_attr;
650 mask_attr = mask6_attr;
651 len = 16;
652 } else {
653 return;
654 }
655 if (!addr_attr || RTA_PAYLOAD(addr_attr) != len)
656 return;
7faf1588 657 fprintf(f, "\n %s %s", name, rt_addr_n2a_rta(family, addr_attr));
30eb304e
JP
658 if (!mask_attr || RTA_PAYLOAD(mask_attr) != len)
659 return;
660 bits = __mask_bits(RTA_DATA(mask_attr), len);
661 if (bits < 0)
7faf1588 662 fprintf(f, "/%s", rt_addr_n2a_rta(family, mask_attr));
30eb304e
JP
663 else if (bits < len * 8)
664 fprintf(f, "/%d", bits);
665}
666
a1fb0d48 667static void flower_print_port(FILE *f, char *name, struct rtattr *attr)
30eb304e 668{
6bd5b80c
SH
669 if (attr)
670 fprintf(f, "\n %s %d", name, rta_getattr_be16(attr));
30eb304e
JP
671}
672
bb9b63b1
AV
673static void flower_print_key_id(FILE *f, const char *name,
674 struct rtattr *attr)
675{
676 if (attr)
677 fprintf(f, "\n %s %d", name, rta_getattr_be32(attr));
678}
679
eb3b5696
SH
680static void flower_print_icmp(FILE *f, char *name, struct rtattr *attr)
681{
682 if (attr)
683 fprintf(f, "\n %s %d", name, rta_getattr_u8(attr));
684}
685
30eb304e
JP
686static int flower_print_opt(struct filter_util *qu, FILE *f,
687 struct rtattr *opt, __u32 handle)
688{
689 struct rtattr *tb[TCA_FLOWER_MAX + 1];
690 __be16 eth_type = 0;
691 __u8 ip_proto = 0xff;
6bd5b80c 692 int nl_type;
30eb304e
JP
693
694 if (!opt)
695 return 0;
696
697 parse_rtattr_nested(tb, TCA_FLOWER_MAX, opt);
698
699 if (handle)
700 fprintf(f, "handle 0x%x ", handle);
701
702 if (tb[TCA_FLOWER_CLASSID]) {
703 SPRINT_BUF(b1);
704 fprintf(f, "classid %s ",
488b41d0
JHS
705 sprint_tc_classid(rta_getattr_u32(tb[TCA_FLOWER_CLASSID]),
706 b1));
30eb304e
JP
707 }
708
709 if (tb[TCA_FLOWER_INDEV]) {
710 struct rtattr *attr = tb[TCA_FLOWER_INDEV];
711
712 fprintf(f, "\n indev %s", rta_getattr_str(attr));
713 }
714
745d9172
HHZ
715 if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
716 struct rtattr *attr = tb[TCA_FLOWER_KEY_VLAN_ID];
717
718 fprintf(f, "\n vlan_id %d", rta_getattr_u16(attr));
719 }
720
721 if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
722 struct rtattr *attr = tb[TCA_FLOWER_KEY_VLAN_PRIO];
723
724 fprintf(f, "\n vlan_prio %d", rta_getattr_u8(attr));
725 }
726
30eb304e
JP
727 flower_print_eth_addr(f, "dst_mac", tb[TCA_FLOWER_KEY_ETH_DST],
728 tb[TCA_FLOWER_KEY_ETH_DST_MASK]);
729 flower_print_eth_addr(f, "src_mac", tb[TCA_FLOWER_KEY_ETH_SRC],
730 tb[TCA_FLOWER_KEY_ETH_SRC_MASK]);
731
732 flower_print_eth_type(f, &eth_type, tb[TCA_FLOWER_KEY_ETH_TYPE]);
733 flower_print_ip_proto(f, &ip_proto, tb[TCA_FLOWER_KEY_IP_PROTO]);
734
735 flower_print_ip_addr(f, "dst_ip", eth_type,
736 tb[TCA_FLOWER_KEY_IPV4_DST],
737 tb[TCA_FLOWER_KEY_IPV4_DST_MASK],
738 tb[TCA_FLOWER_KEY_IPV6_DST],
739 tb[TCA_FLOWER_KEY_IPV6_DST_MASK]);
740
741 flower_print_ip_addr(f, "src_ip", eth_type,
742 tb[TCA_FLOWER_KEY_IPV4_SRC],
743 tb[TCA_FLOWER_KEY_IPV4_SRC_MASK],
744 tb[TCA_FLOWER_KEY_IPV6_SRC],
745 tb[TCA_FLOWER_KEY_IPV6_SRC_MASK]);
746
b2141de1 747 nl_type = flower_port_attr_type(ip_proto, FLOWER_ENDPOINT_DST);
6bd5b80c
SH
748 if (nl_type >= 0)
749 flower_print_port(f, "dst_port", tb[nl_type]);
b2141de1 750 nl_type = flower_port_attr_type(ip_proto, FLOWER_ENDPOINT_SRC);
6bd5b80c
SH
751 if (nl_type >= 0)
752 flower_print_port(f, "src_port", tb[nl_type]);
30eb304e 753
eb3b5696
SH
754 nl_type = flower_icmp_attr_type(eth_type, ip_proto, false);
755 if (nl_type >= 0)
756 flower_print_icmp(f, "icmp_type", tb[nl_type]);
757 nl_type = flower_icmp_attr_type(eth_type, ip_proto, true);
758 if (nl_type >= 0)
759 flower_print_icmp(f, "icmp_code", tb[nl_type]);
760
bb9b63b1
AV
761 flower_print_ip_addr(f, "enc_dst_ip",
762 tb[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] ?
763 htons(ETH_P_IP) : htons(ETH_P_IPV6),
764 tb[TCA_FLOWER_KEY_ENC_IPV4_DST],
765 tb[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK],
766 tb[TCA_FLOWER_KEY_ENC_IPV6_DST],
767 tb[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK]);
768
769 flower_print_ip_addr(f, "enc_src_ip",
770 tb[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] ?
771 htons(ETH_P_IP) : htons(ETH_P_IPV6),
772 tb[TCA_FLOWER_KEY_ENC_IPV4_SRC],
773 tb[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK],
774 tb[TCA_FLOWER_KEY_ENC_IPV6_SRC],
775 tb[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK]);
776
777 flower_print_key_id(f, "enc_key_id",
778 tb[TCA_FLOWER_KEY_ENC_KEY_ID]);
779
41aa17ff
HHZ
780 flower_print_port(f, "enc_dst_port",
781 tb[TCA_FLOWER_KEY_ENC_UDP_DST_PORT]);
782
512caeb2 783 if (tb[TCA_FLOWER_FLAGS]) {
cfcabf18
AV
784 __u32 flags = rta_getattr_u32(tb[TCA_FLOWER_FLAGS]);
785
786 if (flags & TCA_CLS_FLAGS_SKIP_HW)
787 fprintf(f, "\n skip_hw");
788 if (flags & TCA_CLS_FLAGS_SKIP_SW)
789 fprintf(f, "\n skip_sw");
790 }
791
512caeb2 792 if (tb[TCA_FLOWER_ACT])
30eb304e 793 tc_print_action(f, tb[TCA_FLOWER_ACT]);
30eb304e
JP
794
795 return 0;
796}
797
798struct filter_util flower_filter_util = {
799 .id = "flower",
800 .parse_fopt = flower_parse_opt,
801 .print_fopt = flower_print_opt,
802};