1 /*
2 * f_matchall.c Match-all 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@mellanox.com>, Yotam Gigi <yotamg@mellanox.com>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22 #include <linux/if.h>
23
24 #include "utils.h"
25 #include "tc_util.h"
26
explain(void)27 static void explain(void)
28 {
29 fprintf(stderr, "Usage: ... matchall [skip_sw | skip_hw]\n");
30 fprintf(stderr, " [ action ACTION_SPEC ] [ classid CLASSID ]\n");
31 fprintf(stderr, "\n");
32 fprintf(stderr, "Where: SELECTOR := SAMPLE SAMPLE ...\n");
33 fprintf(stderr, " FILTERID := X:Y:Z\n");
34 fprintf(stderr, " ACTION_SPEC := ... look at individual actions\n");
35 fprintf(stderr, "\nNOTE: CLASSID is parsed as hexadecimal input.\n");
36 }
37
matchall_parse_opt(struct filter_util * qu,char * handle,int argc,char ** argv,struct nlmsghdr * n)38 static int matchall_parse_opt(struct filter_util *qu, char *handle,
39 int argc, char **argv, struct nlmsghdr *n)
40 {
41 struct tcmsg *t = NLMSG_DATA(n);
42 struct rtattr *tail;
43 __u32 flags = 0;
44 long h = 0;
45
46 if (handle) {
47 h = strtol(handle, NULL, 0);
48 if (h == LONG_MIN || h == LONG_MAX) {
49 fprintf(stderr, "Illegal handle \"%s\", must be numeric.\n",
50 handle);
51 return -1;
52 }
53 }
54 t->tcm_handle = h;
55
56 if (argc == 0)
57 return 0;
58
59 tail = (struct rtattr *)(((void *)n)+NLMSG_ALIGN(n->nlmsg_len));
60 addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0);
61
62 while (argc > 0) {
63 if (matches(*argv, "classid") == 0 ||
64 strcmp(*argv, "flowid") == 0) {
65 unsigned int handle;
66
67 NEXT_ARG();
68 if (get_tc_classid(&handle, *argv)) {
69 fprintf(stderr, "Illegal \"classid\"\n");
70 return -1;
71 }
72 addattr_l(n, MAX_MSG, TCA_MATCHALL_CLASSID, &handle, 4);
73 } else if (matches(*argv, "action") == 0) {
74 NEXT_ARG();
75 if (parse_action(&argc, &argv, TCA_MATCHALL_ACT, n)) {
76 fprintf(stderr, "Illegal \"action\"\n");
77 return -1;
78 }
79 continue;
80
81 } else if (strcmp(*argv, "skip_hw") == 0) {
82 NEXT_ARG();
83 flags |= TCA_CLS_FLAGS_SKIP_HW;
84 continue;
85 } else if (strcmp(*argv, "skip_sw") == 0) {
86 NEXT_ARG();
87 flags |= TCA_CLS_FLAGS_SKIP_SW;
88 continue;
89 } else if (strcmp(*argv, "help") == 0) {
90 explain();
91 return -1;
92 } else {
93 fprintf(stderr, "What is \"%s\"?\n", *argv);
94 explain();
95 return -1;
96 }
97 argc--; argv++;
98 }
99
100 if (flags) {
101 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW |
102 TCA_CLS_FLAGS_SKIP_SW))) {
103 fprintf(stderr,
104 "skip_hw and skip_sw are mutually exclusive\n");
105 return -1;
106 }
107 addattr_l(n, MAX_MSG, TCA_MATCHALL_FLAGS, &flags, 4);
108 }
109
110 tail->rta_len = (((void *)n)+n->nlmsg_len) - (void *)tail;
111 return 0;
112 }
113
matchall_print_opt(struct filter_util * qu,FILE * f,struct rtattr * opt,__u32 handle)114 static int matchall_print_opt(struct filter_util *qu, FILE *f,
115 struct rtattr *opt, __u32 handle)
116 {
117 struct rtattr *tb[TCA_MATCHALL_MAX+1];
118
119 if (opt == NULL)
120 return 0;
121
122 parse_rtattr_nested(tb, TCA_MATCHALL_MAX, opt);
123
124 if (handle)
125 fprintf(f, "handle 0x%x ", handle);
126
127 if (tb[TCA_MATCHALL_CLASSID]) {
128 SPRINT_BUF(b1);
129 fprintf(f, "flowid %s ",
130 sprint_tc_classid(rta_getattr_u32(tb[TCA_MATCHALL_CLASSID]), b1));
131 }
132
133 if (tb[TCA_MATCHALL_FLAGS]) {
134 __u32 flags = rta_getattr_u32(tb[TCA_MATCHALL_FLAGS]);
135
136 if (flags & TCA_CLS_FLAGS_SKIP_HW)
137 fprintf(f, "\n skip_hw");
138 if (flags & TCA_CLS_FLAGS_SKIP_SW)
139 fprintf(f, "\n skip_sw");
140
141 if (flags & TCA_CLS_FLAGS_IN_HW)
142 fprintf(f, "\n in_hw");
143 else if (flags & TCA_CLS_FLAGS_NOT_IN_HW)
144 fprintf(f, "\n not_in_hw");
145 }
146
147 if (tb[TCA_MATCHALL_ACT])
148 tc_print_action(f, tb[TCA_MATCHALL_ACT], 0);
149
150 return 0;
151 }
152
153 struct filter_util matchall_filter_util = {
154 .id = "matchall",
155 .parse_fopt = matchall_parse_opt,
156 .print_fopt = matchall_print_opt,
157 };
158