1 /*
2 * f_tcindex.c Traffic control index filter
3 *
4 * Written 1998,1999 by Werner Almesberger
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <syslog.h>
11 #include <fcntl.h>
12 #include <string.h>
13 #include <netinet/in.h>
14
15 #include "utils.h"
16 #include "tc_util.h"
17
explain(void)18 static void explain(void)
19 {
20 fprintf(stderr," Usage: ... tcindex [ hash SIZE ] [ mask MASK ]"
21 " [ shift SHIFT ]\n");
22 fprintf(stderr," [ pass_on | fall_through ]\n");
23 fprintf(stderr," [ classid CLASSID ] "
24 "[ action ACTION_SPEC ]\n");
25 }
26
tcindex_parse_opt(struct filter_util * qu,char * handle,int argc,char ** argv,struct nlmsghdr * n)27 static int tcindex_parse_opt(struct filter_util *qu, char *handle, int argc,
28 char **argv, struct nlmsghdr *n)
29 {
30 struct tcmsg *t = NLMSG_DATA(n);
31 struct rtattr *tail;
32 char *end;
33
34 if (handle) {
35 t->tcm_handle = strtoul(handle,&end,0);
36 if (*end) {
37 fprintf(stderr, "Illegal filter ID\n");
38 return -1;
39 }
40 }
41 if (!argc) return 0;
42 tail = NLMSG_TAIL(n);
43 addattr_l(n,4096,TCA_OPTIONS,NULL,0);
44 while (argc) {
45 if (!strcmp(*argv,"hash")) {
46 int hash;
47
48 NEXT_ARG();
49 hash = strtoul(*argv,&end,0);
50 if (*end || !hash || hash > 0x10000) {
51 explain();
52 return -1;
53 }
54 addattr_l(n,4096,TCA_TCINDEX_HASH,&hash,sizeof(hash));
55 }
56 else if (!strcmp(*argv,"mask")) {
57 __u16 mask;
58
59 NEXT_ARG();
60 mask = strtoul(*argv,&end,0);
61 if (*end) {
62 explain();
63 return -1;
64 }
65 addattr_l(n,4096,TCA_TCINDEX_MASK,&mask,sizeof(mask));
66 }
67 else if (!strcmp(*argv,"shift")) {
68 int shift;
69
70 NEXT_ARG();
71 shift = strtoul(*argv,&end,0);
72 if (*end) {
73 explain();
74 return -1;
75 }
76 addattr_l(n,4096,TCA_TCINDEX_SHIFT,&shift,
77 sizeof(shift));
78 }
79 else if (!strcmp(*argv,"fall_through")) {
80 int value = 1;
81
82 addattr_l(n,4096,TCA_TCINDEX_FALL_THROUGH,&value,
83 sizeof(value));
84 }
85 else if (!strcmp(*argv,"pass_on")) {
86 int value = 0;
87
88 addattr_l(n,4096,TCA_TCINDEX_FALL_THROUGH,&value,
89 sizeof(value));
90 }
91 else if (!strcmp(*argv,"classid")) {
92 __u32 handle;
93
94 NEXT_ARG();
95 if (get_tc_classid(&handle,*argv)) {
96 fprintf(stderr, "Illegal \"classid\"\n");
97 return -1;
98 }
99 addattr_l(n, 4096, TCA_TCINDEX_CLASSID, &handle, 4);
100 }
101 else if (!strcmp(*argv,"police")) {
102 NEXT_ARG();
103 if (parse_police(&argc, &argv, TCA_TCINDEX_POLICE, n)) {
104 fprintf(stderr, "Illegal \"police\"\n");
105 return -1;
106 }
107 continue;
108 }
109 else if (!strcmp(*argv,"action")) {
110 NEXT_ARG();
111 if (parse_police(&argc, &argv, TCA_TCINDEX_ACT, n)) {
112 fprintf(stderr, "Illegal \"action\"\n");
113 return -1;
114 }
115 continue;
116 }
117 else {
118 explain();
119 return -1;
120 }
121 argc--;
122 argv++;
123 }
124 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
125 return 0;
126 }
127
128
tcindex_print_opt(struct filter_util * qu,FILE * f,struct rtattr * opt,__u32 handle)129 static int tcindex_print_opt(struct filter_util *qu, FILE *f,
130 struct rtattr *opt, __u32 handle)
131 {
132 struct rtattr *tb[TCA_TCINDEX_MAX+1];
133
134 if (opt == NULL)
135 return 0;
136
137 parse_rtattr_nested(tb, TCA_TCINDEX_MAX, opt);
138
139 if (handle != ~0) fprintf(f,"handle 0x%04x ",handle);
140 if (tb[TCA_TCINDEX_HASH]) {
141 __u16 hash;
142
143 if (RTA_PAYLOAD(tb[TCA_TCINDEX_HASH]) < sizeof(hash))
144 return -1;
145 hash = rta_getattr_u16(tb[TCA_TCINDEX_HASH]);
146 fprintf(f,"hash %d ",hash);
147 }
148 if (tb[TCA_TCINDEX_MASK]) {
149 __u16 mask;
150
151 if (RTA_PAYLOAD(tb[TCA_TCINDEX_MASK]) < sizeof(mask))
152 return -1;
153 mask = rta_getattr_u16(tb[TCA_TCINDEX_MASK]);
154 fprintf(f,"mask 0x%04x ",mask);
155 }
156 if (tb[TCA_TCINDEX_SHIFT]) {
157 int shift;
158
159 if (RTA_PAYLOAD(tb[TCA_TCINDEX_SHIFT]) < sizeof(shift))
160 return -1;
161 shift = *(int *) RTA_DATA(tb[TCA_TCINDEX_SHIFT]);
162 fprintf(f,"shift %d ",shift);
163 }
164 if (tb[TCA_TCINDEX_FALL_THROUGH]) {
165 int fall_through;
166
167 if (RTA_PAYLOAD(tb[TCA_TCINDEX_FALL_THROUGH]) <
168 sizeof(fall_through))
169 return -1;
170 fall_through = *(int *) RTA_DATA(tb[TCA_TCINDEX_FALL_THROUGH]);
171 fprintf(f,fall_through ? "fall_through " : "pass_on ");
172 }
173 if (tb[TCA_TCINDEX_CLASSID]) {
174 SPRINT_BUF(b1);
175 fprintf(f, "classid %s ",sprint_tc_classid(*(__u32 *)
176 RTA_DATA(tb[TCA_TCINDEX_CLASSID]), b1));
177 }
178 if (tb[TCA_TCINDEX_POLICE]) {
179 fprintf(f, "\n");
180 tc_print_police(f, tb[TCA_TCINDEX_POLICE]);
181 }
182 if (tb[TCA_TCINDEX_ACT]) {
183 fprintf(f, "\n");
184 tc_print_police(f, tb[TCA_TCINDEX_ACT]);
185 }
186 return 0;
187 }
188
189 struct filter_util tcindex_filter_util = {
190 .id = "tcindex",
191 .parse_fopt = tcindex_parse_opt,
192 .print_fopt = tcindex_print_opt,
193 };
194