parser.c revision 1.8 1 1.8 wiz /* $NetBSD: parser.c,v 1.8 2003/01/28 22:19:30 wiz Exp $ */
2 1.7 itojun /* $KAME: parser.c,v 1.16 2002/02/20 10:40:39 kjc Exp $ */
3 1.7 itojun /*
4 1.7 itojun * Copyright (C) 1999-2002
5 1.7 itojun * Sony Computer Science Laboratories, Inc. All rights reserved.
6 1.7 itojun *
7 1.7 itojun * Redistribution and use in source and binary forms, with or without
8 1.7 itojun * modification, are permitted provided that the following conditions
9 1.7 itojun * are met:
10 1.7 itojun * 1. Redistributions of source code must retain the above copyright
11 1.7 itojun * notice, this list of conditions and the following disclaimer.
12 1.7 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.7 itojun * notice, this list of conditions and the following disclaimer in the
14 1.7 itojun * documentation and/or other materials provided with the distribution.
15 1.7 itojun *
16 1.7 itojun * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
17 1.7 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.7 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.7 itojun * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
20 1.7 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.7 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.7 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.7 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.7 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.7 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.7 itojun * SUCH DAMAGE.
27 1.7 itojun */
28 1.1 thorpej
29 1.7 itojun #include <sys/param.h>
30 1.7 itojun #include <sys/socket.h>
31 1.7 itojun #include <net/if.h>
32 1.7 itojun #include <netinet/in.h>
33 1.7 itojun #include <arpa/inet.h>
34 1.1 thorpej
35 1.1 thorpej #include <stdio.h>
36 1.1 thorpej #include <stdlib.h>
37 1.1 thorpej #include <unistd.h>
38 1.1 thorpej #include <stddef.h>
39 1.1 thorpej #include <string.h>
40 1.1 thorpej #include <ctype.h>
41 1.1 thorpej #include <errno.h>
42 1.1 thorpej #include <syslog.h>
43 1.1 thorpej #include <netdb.h>
44 1.7 itojun #include <err.h>
45 1.1 thorpej
46 1.1 thorpej #include <altq/altq.h>
47 1.1 thorpej #include <altq/altq_cdnr.h>
48 1.1 thorpej #include <altq/altq_red.h>
49 1.1 thorpej #include <altq/altq_rio.h>
50 1.1 thorpej #include "altq_qop.h"
51 1.1 thorpej #include "qop_cdnr.h"
52 1.1 thorpej
53 1.4 itojun static int is_qdisc_name(const char *);
54 1.4 itojun static int qdisc_interface_parser(const char *, const char *, int, char **);
55 1.4 itojun static int qdisc_class_parser(const char *, const char *, const char *,
56 1.7 itojun const char *, int, char **);
57 1.4 itojun static int next_word(char **, char *);
58 1.4 itojun
59 1.4 itojun static int get_ifname(char **, char **);
60 1.4 itojun static int get_addr(char **, struct in_addr *, struct in_addr *);
61 1.4 itojun static int get_port(const char *, u_int16_t *);
62 1.4 itojun static int get_proto(const char *, int *);
63 1.4 itojun static int get_fltr_opts(char **, char *, size_t, int *);
64 1.4 itojun static int interface_parser(char *);
65 1.4 itojun static int class_parser(char *) ;
66 1.4 itojun static int filter_parser(char *);
67 1.1 thorpej #ifdef INET6
68 1.4 itojun static int filter6_parser(char *);
69 1.4 itojun static int get_ip6addr(char **, struct in6_addr *, struct in6_addr *);
70 1.1 thorpej #endif
71 1.4 itojun static int ctl_parser(char *);
72 1.4 itojun static int delete_parser(char *);
73 1.4 itojun static int red_parser(char *);
74 1.4 itojun static int rio_parser(char *);
75 1.4 itojun static int conditioner_parser(char *);
76 1.4 itojun static int tc_action_parser(char *, char **, struct tc_action *);
77 1.1 thorpej
78 1.7 itojun #define MAX_LINE 1024
79 1.7 itojun #define MAX_WORD 64
80 1.7 itojun #define MAX_ARGS 64
81 1.7 itojun #define MAX_ACTIONS 16
82 1.1 thorpej
83 1.1 thorpej #ifndef MAX
84 1.1 thorpej #define MAX(a,b) (((a)>(b))?(a):(b))
85 1.1 thorpej #endif
86 1.1 thorpej #ifndef MIN
87 1.1 thorpej #define MIN(a,b) (((a)<(b))?(a):(b))
88 1.1 thorpej #endif
89 1.7 itojun #define EQUAL(s1, s2) (strcmp((s1), (s2)) == 0)
90 1.7 itojun
91 1.7 itojun int line_no = 0;
92 1.7 itojun int filter_dontwarn;
93 1.1 thorpej
94 1.7 itojun static char curifname[IFNAMSIZ];
95 1.7 itojun static struct if_nameindex *if_namelist = NULL;
96 1.7 itojun
97 1.7 itojun struct cmd_tab {
98 1.7 itojun const char *cmd;
99 1.7 itojun int (*parser)(char *);
100 1.7 itojun const char *help;
101 1.7 itojun } cmd_tab[] = {
102 1.7 itojun {"help", NULL, "help | ?"},
103 1.7 itojun {"quit", NULL, "quit"},
104 1.7 itojun {"interface", interface_parser, "interface if_name [bandwidth bps] [cbq|hfsc]"},
105 1.7 itojun {"class", class_parser, "class discipline if_name class_name [parent]"},
106 1.7 itojun {"filter", filter_parser, "filter if_name class_name [name filt_name] dst [netmask #] dport src [netmask #] sport proto [tos # [tosmask #] [gpi #] [dontwarn]"},
107 1.7 itojun {"altq", ctl_parser, "altq if_name {enable|disable}"},
108 1.7 itojun {"delete", delete_parser, "delete if_name class_name [filter_name]"},
109 1.1 thorpej #ifdef INET6
110 1.7 itojun {"filter6", filter6_parser, "filter6 if_name class_name [name filt_name] dst[/prefix] dport src[/prefix] sport proto [flowlabel #][tclass # [tclassmask #]][gpi #] [dontwarn]"},
111 1.1 thorpej #endif
112 1.7 itojun {"red", red_parser, "red th_min th_max inv_pmax"},
113 1.7 itojun {"rio", rio_parser, "rio low_th_min low_th_max low_inv_pmax med_th_min med_th_max med_inv_pmax high_th_min high_th_max high_inv_pmax"},
114 1.7 itojun {"conditioner", conditioner_parser, "conditioner if_name cdnr_name <tc_action>"},
115 1.7 itojun {"debug", NULL, "debug"},
116 1.7 itojun {NULL, NULL, NULL} /* termination */
117 1.1 thorpej };
118 1.1 thorpej
119 1.7 itojun /*
120 1.7 itojun * read one line from the specified stream. if it's a command,
121 1.7 itojun * execute the command.
122 1.7 itojun * returns 1 if OK, 0 if error or EOF.
123 1.1 thorpej */
124 1.7 itojun int
125 1.7 itojun do_command(FILE *fp)
126 1.7 itojun {
127 1.7 itojun char cmd_line[MAX_LINE], cmd[MAX_WORD], *cp;
128 1.7 itojun struct cmd_tab *tp;
129 1.7 itojun int len, rval;
130 1.7 itojun
131 1.7 itojun /*
132 1.7 itojun * read a line from the stream and make it a null-terminated string
133 1.7 itojun */
134 1.7 itojun cp = cmd_line;
135 1.7 itojun read_line:
136 1.7 itojun if (fgets(cp, &cmd_line[MAX_LINE] - cp, fp) == NULL)
137 1.7 itojun /* EOF or error */
138 1.7 itojun return(0);
139 1.7 itojun line_no++;
140 1.7 itojun
141 1.7 itojun /* null-terminate the line */
142 1.7 itojun if ((len = strlen(cmd_line)) > 0) {
143 1.7 itojun cp = cmd_line + len - 1;
144 1.7 itojun if (*cp == '\n') {
145 1.7 itojun /* if escaped newline, read next line */
146 1.7 itojun if (len > 1 && *(cp - 1) == '\\')
147 1.7 itojun goto read_line;
148 1.7 itojun *cp = '\0';
149 1.7 itojun } else if (!feof(fp))
150 1.7 itojun err(1, "LINE %d too long!", line_no);
151 1.7 itojun }
152 1.7 itojun /* trim comments */
153 1.7 itojun if ((cp = strchr(cmd_line, '#')) != NULL)
154 1.7 itojun *cp = '\0';
155 1.7 itojun
156 1.7 itojun cp = cmd_line;
157 1.7 itojun if ((len = next_word(&cp, cmd)) == 0)
158 1.7 itojun /* no command in this line */
159 1.7 itojun return (1);
160 1.7 itojun
161 1.7 itojun /* fnind the corresponding parser */
162 1.7 itojun rval = 0;
163 1.7 itojun for (tp = cmd_tab; tp->cmd != NULL; tp++)
164 1.7 itojun if (strncmp(cmd, tp->cmd, len) == 0)
165 1.7 itojun break;
166 1.7 itojun
167 1.7 itojun if (tp->cmd == NULL) {
168 1.7 itojun if (fp == stdin) {
169 1.7 itojun printf(" ?? %s\n", cmd);
170 1.7 itojun rval = 1;
171 1.7 itojun } else
172 1.7 itojun LOG(LOG_ERR, 0, "unknown command: %s", cmd);
173 1.7 itojun return (rval);
174 1.7 itojun }
175 1.7 itojun
176 1.7 itojun if (tp->parser != NULL)
177 1.7 itojun rval = (*tp->parser)(cp);
178 1.7 itojun else {
179 1.7 itojun /* handle other commands */
180 1.7 itojun if (strcmp(tp->cmd, "quit") == 0)
181 1.7 itojun rval = 0;
182 1.7 itojun else if (strcmp(tp->cmd, "help") == 0 ||
183 1.7 itojun strcmp(tp->cmd, "?") == 0) {
184 1.7 itojun for (tp = cmd_tab; tp->cmd != NULL; tp++)
185 1.7 itojun printf("%s\n", tp->help);
186 1.7 itojun rval = 1;
187 1.7 itojun } else if (strcmp(tp->cmd, "debug") == 0) {
188 1.7 itojun if (m_debug & DEBUG_ALTQ) {
189 1.7 itojun /* turn off verbose */
190 1.7 itojun l_debug = LOG_INFO;
191 1.7 itojun m_debug &= ~DEBUG_ALTQ;
192 1.7 itojun } else {
193 1.7 itojun /* turn on verbose */
194 1.7 itojun l_debug = LOG_DEBUG;
195 1.7 itojun m_debug |= DEBUG_ALTQ;
196 1.7 itojun }
197 1.7 itojun rval = 1;
198 1.7 itojun }
199 1.7 itojun }
200 1.7 itojun return (rval);
201 1.7 itojun }
202 1.1 thorpej
203 1.7 itojun static int
204 1.1 thorpej is_qdisc_name(const char *qname)
205 1.1 thorpej {
206 1.1 thorpej struct qdisc_parser *qp;
207 1.7 itojun
208 1.1 thorpej for (qp = qdisc_parser; qp->qname != NULL; qp++)
209 1.1 thorpej if (strncmp(qp->qname, qname, strlen(qp->qname)) == 0)
210 1.1 thorpej return (1);
211 1.1 thorpej return (0);
212 1.1 thorpej }
213 1.1 thorpej
214 1.7 itojun static int
215 1.1 thorpej qdisc_interface_parser(const char * qname, const char *ifname,
216 1.1 thorpej int argc, char **argv)
217 1.1 thorpej {
218 1.1 thorpej struct qdisc_parser *qp;
219 1.7 itojun
220 1.1 thorpej for (qp = qdisc_parser; qp->qname != NULL; qp++)
221 1.1 thorpej if (strncmp(qp->qname, qname, strlen(qp->qname)) == 0)
222 1.1 thorpej return (*qp->interface_parser)(ifname, argc, argv);
223 1.1 thorpej return (0);
224 1.1 thorpej }
225 1.1 thorpej
226 1.7 itojun static int
227 1.1 thorpej qdisc_class_parser(const char *qname, const char *ifname,
228 1.1 thorpej const char *class_name, const char *parent_name,
229 1.1 thorpej int argc, char **argv)
230 1.1 thorpej {
231 1.1 thorpej struct qdisc_parser *qp;
232 1.4 itojun struct ifinfo *ifinfo;
233 1.4 itojun
234 1.1 thorpej for (qp = qdisc_parser; qp->qname != NULL; qp++)
235 1.1 thorpej if (strncmp(qp->qname, qname, strlen(qp->qname)) == 0) {
236 1.1 thorpej if (qp->class_parser == NULL) {
237 1.1 thorpej LOG(LOG_ERR, 0,
238 1.4 itojun "class can't be specified for %s", qp->qname);
239 1.4 itojun return (0);
240 1.4 itojun }
241 1.4 itojun if ((ifinfo = ifname2ifinfo(ifname)) == NULL) {
242 1.7 itojun LOG(LOG_ERR, 0, "no such interface");
243 1.4 itojun return (0);
244 1.4 itojun }
245 1.4 itojun if (strncmp(ifinfo->qdisc->qname, qname,
246 1.4 itojun strlen(ifinfo->qdisc->qname)) != 0) {
247 1.4 itojun LOG(LOG_ERR, 0,
248 1.7 itojun "qname doesn't match the interface");
249 1.1 thorpej return (0);
250 1.1 thorpej }
251 1.1 thorpej return (*qp->class_parser)(ifname, class_name,
252 1.1 thorpej parent_name, argc, argv);
253 1.1 thorpej }
254 1.1 thorpej return (0);
255 1.1 thorpej }
256 1.1 thorpej
257 1.1 thorpej /*
258 1.7 itojun * read the config file
259 1.1 thorpej */
260 1.1 thorpej int
261 1.1 thorpej qcmd_config(void)
262 1.1 thorpej {
263 1.7 itojun FILE *fp;
264 1.7 itojun int rval;
265 1.1 thorpej
266 1.1 thorpej if (if_namelist != NULL)
267 1.1 thorpej if_freenameindex(if_namelist);
268 1.1 thorpej if_namelist = if_nameindex();
269 1.7 itojun curifname[0] = '\0';
270 1.1 thorpej
271 1.6 itojun LOG(LOG_INFO, 0, "ALTQ config file is %s", altqconfigfile);
272 1.1 thorpej
273 1.7 itojun fp = fopen(altqconfigfile, "r");
274 1.7 itojun if (fp == NULL) {
275 1.6 itojun LOG(LOG_ERR, errno, "can't open %s", altqconfigfile, 0);
276 1.1 thorpej return (QOPERR_INVAL);
277 1.1 thorpej }
278 1.1 thorpej line_no = 0;
279 1.7 itojun rval = 1;
280 1.7 itojun while (rval)
281 1.7 itojun rval = do_command(fp);
282 1.1 thorpej
283 1.7 itojun if (!feof(fp)) {
284 1.7 itojun LOG(LOG_ERR, 0, "Error in %s, line %d. config failed.",
285 1.7 itojun altqconfigfile, line_no);
286 1.7 itojun (void) qcmd_destroyall();
287 1.7 itojun rval = QOPERR_INVAL;
288 1.7 itojun } else
289 1.7 itojun rval = 0;
290 1.7 itojun
291 1.7 itojun (void)fclose(fp);
292 1.1 thorpej line_no = 0;
293 1.7 itojun return (rval);
294 1.1 thorpej }
295 1.1 thorpej
296 1.1 thorpej static int
297 1.1 thorpej next_word(char **cpp, char *b)
298 1.1 thorpej {
299 1.7 itojun char *cp;
300 1.7 itojun int i;
301 1.1 thorpej
302 1.7 itojun cp = *cpp;
303 1.7 itojun while (*cp == ' ' || *cp == '\t')
304 1.7 itojun cp++;
305 1.7 itojun for (i = 0; i < MAX_WORD - 1; i++) {
306 1.7 itojun if (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\0')
307 1.7 itojun break;
308 1.7 itojun *b++ = *cp++;
309 1.1 thorpej }
310 1.7 itojun *b = '\0';
311 1.7 itojun *cpp = cp;
312 1.7 itojun return (i);
313 1.1 thorpej }
314 1.1 thorpej
315 1.7 itojun char *
316 1.7 itojun cur_ifname(void)
317 1.1 thorpej {
318 1.7 itojun return (curifname);
319 1.1 thorpej }
320 1.1 thorpej
321 1.1 thorpej u_int
322 1.1 thorpej get_ifindex(const char *ifname)
323 1.1 thorpej {
324 1.1 thorpej struct if_nameindex *ifnp;
325 1.1 thorpej
326 1.1 thorpej for (ifnp = if_namelist; ifnp->if_name != NULL; ifnp++)
327 1.1 thorpej if (strcmp(ifname, ifnp->if_name) == 0)
328 1.1 thorpej return (ifnp->if_index);
329 1.1 thorpej return (0);
330 1.1 thorpej }
331 1.1 thorpej
332 1.1 thorpej static int
333 1.1 thorpej get_ifname(char **cpp, char **ifnamep)
334 1.1 thorpej {
335 1.7 itojun char w[MAX_WORD], *ocp;
336 1.1 thorpej struct if_nameindex *ifnp;
337 1.1 thorpej
338 1.1 thorpej ocp = *cpp;
339 1.1 thorpej if (next_word(&ocp, w) && if_namelist != NULL)
340 1.1 thorpej for (ifnp = if_namelist; ifnp->if_name != NULL; ifnp++)
341 1.1 thorpej if (strcmp(w, ifnp->if_name) == 0) {
342 1.1 thorpej /* if_name found. advance the word pointer */
343 1.7 itojun *cpp = ocp;
344 1.7 itojun strlcpy(curifname, w, sizeof(curifname));
345 1.7 itojun *ifnamep = curifname;
346 1.1 thorpej return (1);
347 1.1 thorpej }
348 1.1 thorpej
349 1.1 thorpej /* this is not interface name. use one in the context. */
350 1.7 itojun if (curifname[0] == '\0')
351 1.1 thorpej return (0);
352 1.7 itojun *ifnamep = curifname;
353 1.1 thorpej return (1);
354 1.1 thorpej }
355 1.1 thorpej
356 1.1 thorpej /* set address and netmask in network byte order */
357 1.1 thorpej static int
358 1.1 thorpej get_addr(char **cpp, struct in_addr *addr, struct in_addr *mask)
359 1.1 thorpej {
360 1.7 itojun char w[MAX_WORD], *ocp;
361 1.3 itojun struct in_addr tmp;
362 1.7 itojun
363 1.1 thorpej addr->s_addr = 0;
364 1.1 thorpej mask->s_addr = 0xffffffff;
365 1.1 thorpej
366 1.1 thorpej if (!next_word(cpp, w))
367 1.1 thorpej return (0);
368 1.1 thorpej
369 1.3 itojun if (inet_aton((char *)w, &tmp) != 1) {
370 1.1 thorpej /* try gethostbyname */
371 1.1 thorpej struct hostent *h;
372 1.1 thorpej
373 1.7 itojun if ((h = gethostbyname(w)) == NULL ||
374 1.7 itojun h->h_addrtype != AF_INET || h->h_length != 4)
375 1.1 thorpej return (0);
376 1.1 thorpej bcopy(h->h_addr, &tmp, (size_t)h->h_length);
377 1.1 thorpej }
378 1.3 itojun addr->s_addr = tmp.s_addr;
379 1.1 thorpej
380 1.1 thorpej /* check if netmask option is present */
381 1.1 thorpej ocp = *cpp;
382 1.1 thorpej if (next_word(&ocp, w) && EQUAL(w, "netmask")) {
383 1.1 thorpej if (!next_word(&ocp, w))
384 1.1 thorpej return (0);
385 1.3 itojun if (inet_aton((char *)w, (struct in_addr *)&tmp) != 1)
386 1.1 thorpej return (0);
387 1.1 thorpej
388 1.3 itojun mask->s_addr = tmp.s_addr;
389 1.7 itojun *cpp = ocp;
390 1.1 thorpej return (1);
391 1.1 thorpej }
392 1.1 thorpej /* no netmask option */
393 1.1 thorpej return (1);
394 1.1 thorpej }
395 1.1 thorpej
396 1.1 thorpej /* returns service number in network byte order */
397 1.1 thorpej static int
398 1.1 thorpej get_port(const char *name, u_int16_t *port_no)
399 1.1 thorpej {
400 1.1 thorpej struct servent *s;
401 1.1 thorpej u_int16_t num;
402 1.7 itojun
403 1.1 thorpej if (isdigit(name[0])) {
404 1.1 thorpej num = (u_int16_t)strtol(name, NULL, 0);
405 1.1 thorpej *port_no = htons(num);
406 1.1 thorpej return (1);
407 1.1 thorpej }
408 1.1 thorpej
409 1.1 thorpej if ((s = getservbyname(name, 0)) == NULL)
410 1.1 thorpej return (0);
411 1.1 thorpej
412 1.1 thorpej *port_no = (u_int16_t)s->s_port;
413 1.1 thorpej return (1);
414 1.1 thorpej }
415 1.1 thorpej
416 1.1 thorpej static int
417 1.1 thorpej get_proto(const char *name, int *proto_no)
418 1.1 thorpej {
419 1.1 thorpej struct protoent *p;
420 1.7 itojun
421 1.1 thorpej if (isdigit(name[0])) {
422 1.1 thorpej *proto_no = (int)strtol(name, NULL, 0);
423 1.1 thorpej return (1);
424 1.1 thorpej }
425 1.1 thorpej
426 1.1 thorpej if ((p = getprotobyname(name)) == NULL)
427 1.1 thorpej return (0);
428 1.1 thorpej
429 1.1 thorpej *proto_no = p->p_proto;
430 1.1 thorpej return (1);
431 1.1 thorpej }
432 1.1 thorpej
433 1.1 thorpej static int
434 1.4 itojun get_fltr_opts(char **cpp, char *fltr_name, size_t len, int *ruleno)
435 1.1 thorpej {
436 1.7 itojun char w[MAX_WORD], *ocp;
437 1.1 thorpej
438 1.1 thorpej ocp = *cpp;
439 1.1 thorpej while (next_word(&ocp, w)) {
440 1.1 thorpej if (EQUAL(w, "name")) {
441 1.1 thorpej if (!next_word(&ocp, w))
442 1.1 thorpej return (0);
443 1.4 itojun strlcpy(fltr_name, w, len);
444 1.1 thorpej *cpp = ocp;
445 1.1 thorpej } else if (EQUAL(w, "ruleno")) {
446 1.1 thorpej if (!next_word(&ocp, w))
447 1.1 thorpej return (0);
448 1.1 thorpej *ruleno = (int)strtol(w, NULL, 0);
449 1.1 thorpej *cpp = ocp;
450 1.1 thorpej } else
451 1.1 thorpej break;
452 1.1 thorpej }
453 1.1 thorpej return (1);
454 1.1 thorpej }
455 1.1 thorpej
456 1.1 thorpej
457 1.1 thorpej #define DISCIPLINE_NONE 0
458 1.1 thorpej
459 1.1 thorpej static int
460 1.1 thorpej interface_parser(char *cmdbuf)
461 1.1 thorpej {
462 1.7 itojun char w[MAX_WORD], *ap, *cp = cmdbuf;
463 1.7 itojun char *ifname, *argv[MAX_ARGS], qdisc_name[MAX_WORD];
464 1.7 itojun int argc;
465 1.1 thorpej
466 1.1 thorpej if (!get_ifname(&cp, &ifname)) {
467 1.7 itojun LOG(LOG_ERR, 0, "missing interface name");
468 1.1 thorpej return (0);
469 1.1 thorpej }
470 1.1 thorpej
471 1.7 itojun /* create argment list & look for scheduling discipline options. */
472 1.7 itojun snprintf(qdisc_name, sizeof qdisc_name, "null");
473 1.1 thorpej argc = 0;
474 1.1 thorpej ap = w;
475 1.1 thorpej while (next_word(&cp, ap)) {
476 1.1 thorpej if (is_qdisc_name(ap))
477 1.7 itojun strlcpy(qdisc_name, ap, sizeof qdisc_name);
478 1.1 thorpej
479 1.1 thorpej argv[argc] = ap;
480 1.1 thorpej ap += strlen(ap) + 1;
481 1.1 thorpej argc++;
482 1.7 itojun if (argc >= MAX_ARGS) {
483 1.7 itojun LOG(LOG_ERR, 0, "too many args");
484 1.7 itojun return (0);
485 1.7 itojun }
486 1.1 thorpej }
487 1.1 thorpej
488 1.7 itojun return qdisc_interface_parser(qdisc_name, ifname, argc, argv);
489 1.1 thorpej }
490 1.1 thorpej
491 1.7 itojun
492 1.1 thorpej static int
493 1.7 itojun class_parser(char *cmdbuf)
494 1.1 thorpej {
495 1.7 itojun char w[MAX_WORD], *cp = cmdbuf;
496 1.7 itojun char *ifname, qdisc_name[MAX_WORD];
497 1.7 itojun char class_name[MAX_WORD], parent_name[MAX_WORD];
498 1.1 thorpej char *clname = class_name;
499 1.1 thorpej char *parent = NULL;
500 1.7 itojun char *argv[MAX_ARGS], *ap;
501 1.7 itojun int argc;
502 1.1 thorpej
503 1.1 thorpej /* get scheduling class */
504 1.1 thorpej if (!next_word(&cp, qdisc_name)) {
505 1.7 itojun LOG(LOG_ERR, 0, "missing discipline");
506 1.1 thorpej return (0);
507 1.1 thorpej }
508 1.1 thorpej if (!is_qdisc_name(qdisc_name)) {
509 1.7 itojun LOG(LOG_ERR, 0, "unknown discipline '%s'", qdisc_name);
510 1.1 thorpej return (0);
511 1.1 thorpej }
512 1.1 thorpej
513 1.1 thorpej /* get interface name */
514 1.1 thorpej if (!get_ifname(&cp, &ifname)) {
515 1.7 itojun LOG(LOG_ERR, 0, "missing interface name");
516 1.1 thorpej return (0);
517 1.1 thorpej }
518 1.1 thorpej
519 1.1 thorpej /* get class name */
520 1.1 thorpej if (!next_word(&cp, class_name)) {
521 1.7 itojun LOG(LOG_ERR, 0, "missing class name");
522 1.1 thorpej return (0);
523 1.1 thorpej }
524 1.1 thorpej
525 1.1 thorpej /* get parent name */
526 1.1 thorpej if (!next_word(&cp, parent_name)) {
527 1.7 itojun LOG(LOG_ERR, 0, "missing parent class");
528 1.1 thorpej return (0);
529 1.1 thorpej }
530 1.7 itojun if (!EQUAL(parent_name, "null") && !EQUAL(parent_name, "NULL"))
531 1.1 thorpej parent = parent_name;
532 1.7 itojun else
533 1.1 thorpej parent = NULL;
534 1.1 thorpej
535 1.1 thorpej ap = w;
536 1.1 thorpej argc = 0;
537 1.1 thorpej while (next_word(&cp, ap)) {
538 1.1 thorpej argv[argc] = ap;
539 1.1 thorpej ap += strlen(ap) + 1;
540 1.1 thorpej argc++;
541 1.7 itojun if (argc >= MAX_ARGS) {
542 1.7 itojun LOG(LOG_ERR, 0, "too many args");
543 1.7 itojun return (0);
544 1.7 itojun }
545 1.1 thorpej }
546 1.1 thorpej
547 1.7 itojun return qdisc_class_parser(qdisc_name, ifname, clname, parent,
548 1.7 itojun argc, argv);
549 1.1 thorpej }
550 1.1 thorpej
551 1.1 thorpej static int
552 1.7 itojun filter_parser(char *cmdbuf)
553 1.1 thorpej {
554 1.7 itojun char w[MAX_WORD], *cp = cmdbuf;
555 1.7 itojun char *ifname, class_name[MAX_WORD], fltr_name[MAX_WORD];
556 1.7 itojun char *flname = NULL;
557 1.1 thorpej struct flow_filter sfilt;
558 1.1 thorpej int protocol;
559 1.1 thorpej u_char tos, tosmask;
560 1.1 thorpej int ruleno;
561 1.1 thorpej int dontwarn = 0;
562 1.1 thorpej int error;
563 1.1 thorpej
564 1.1 thorpej memset(&sfilt, 0, sizeof(sfilt));
565 1.1 thorpej sfilt.ff_flow.fi_family = AF_INET;
566 1.1 thorpej
567 1.1 thorpej if (!get_ifname(&cp, &ifname)) {
568 1.7 itojun LOG(LOG_ERR, 0, "missing interface name in filter command");
569 1.1 thorpej return (0);
570 1.1 thorpej }
571 1.1 thorpej
572 1.1 thorpej if (!next_word(&cp, class_name)) {
573 1.7 itojun LOG(LOG_ERR, 0, "missing class name in filter command");
574 1.1 thorpej return (0);
575 1.1 thorpej }
576 1.1 thorpej
577 1.1 thorpej fltr_name[0] = '\0';
578 1.1 thorpej ruleno = 0;
579 1.4 itojun if (!get_fltr_opts(&cp, &fltr_name[0], sizeof(fltr_name), &ruleno)) {
580 1.7 itojun LOG(LOG_ERR, 0, "bad filter option");
581 1.1 thorpej return (0);
582 1.1 thorpej }
583 1.1 thorpej if (fltr_name[0] != '\0')
584 1.1 thorpej flname = fltr_name;
585 1.1 thorpej sfilt.ff_ruleno = ruleno;
586 1.1 thorpej
587 1.1 thorpej /* get filter destination Address */
588 1.1 thorpej if (!get_addr(&cp, &sfilt.ff_flow.fi_dst, &sfilt.ff_mask.mask_dst)) {
589 1.7 itojun LOG(LOG_ERR, 0, "bad filter destination address");
590 1.1 thorpej return (0);
591 1.1 thorpej }
592 1.1 thorpej
593 1.1 thorpej /* get filter destination port */
594 1.1 thorpej if (!next_word(&cp, w)) {
595 1.7 itojun LOG(LOG_ERR, 0, "missing filter destination port");
596 1.1 thorpej return (0);
597 1.1 thorpej }
598 1.1 thorpej if (!get_port(w, &sfilt.ff_flow.fi_dport)) {
599 1.7 itojun LOG(LOG_ERR, 0, "bad filter destination port");
600 1.1 thorpej return (0);
601 1.1 thorpej }
602 1.7 itojun
603 1.1 thorpej /* get filter source address */
604 1.1 thorpej if (!get_addr(&cp, &sfilt.ff_flow.fi_src, &sfilt.ff_mask.mask_src)) {
605 1.7 itojun LOG(LOG_ERR, 0, "bad filter source address");
606 1.1 thorpej return (0);
607 1.1 thorpej }
608 1.1 thorpej
609 1.1 thorpej /* get filter source port */
610 1.1 thorpej if (!next_word(&cp, w)) {
611 1.7 itojun LOG(LOG_ERR, 0, "missing filter source port");
612 1.1 thorpej return (0);
613 1.1 thorpej }
614 1.1 thorpej if (!get_port(w, &sfilt.ff_flow.fi_sport)) {
615 1.7 itojun LOG(LOG_ERR, 0, "bad filter source port");
616 1.1 thorpej return (0);
617 1.1 thorpej }
618 1.1 thorpej
619 1.1 thorpej /* get filter protocol id */
620 1.1 thorpej if (!next_word(&cp, w)) {
621 1.7 itojun LOG(LOG_ERR, 0, "missing filter protocol");
622 1.1 thorpej return (0);
623 1.1 thorpej }
624 1.1 thorpej if (!get_proto(w, &protocol)) {
625 1.7 itojun LOG(LOG_ERR, 0, "bad protocol");
626 1.1 thorpej return (0);
627 1.1 thorpej }
628 1.1 thorpej sfilt.ff_flow.fi_proto = protocol;
629 1.1 thorpej
630 1.1 thorpej while (next_word(&cp, w)) {
631 1.1 thorpej if (EQUAL(w, "tos")) {
632 1.1 thorpej tos = 0;
633 1.1 thorpej tosmask = 0xff;
634 1.7 itojun
635 1.1 thorpej if (next_word(&cp, w)) {
636 1.1 thorpej tos = (u_char)strtol(w, NULL, 0);
637 1.1 thorpej if (next_word(&cp, w)) {
638 1.1 thorpej if (EQUAL(w, "tosmask")) {
639 1.1 thorpej next_word(&cp, w);
640 1.1 thorpej tosmask = (u_char)strtol(w, NULL, 0);
641 1.1 thorpej }
642 1.1 thorpej }
643 1.1 thorpej }
644 1.7 itojun sfilt.ff_flow.fi_tos = tos;
645 1.7 itojun sfilt.ff_mask.mask_tos = tosmask;
646 1.1 thorpej } else if (EQUAL(w, "gpi")) {
647 1.1 thorpej if (next_word(&cp, w)) {
648 1.1 thorpej sfilt.ff_flow.fi_gpi =
649 1.1 thorpej (u_int32_t)strtoul(w, NULL, 0);
650 1.1 thorpej sfilt.ff_flow.fi_gpi =
651 1.1 thorpej htonl(sfilt.ff_flow.fi_gpi);
652 1.1 thorpej }
653 1.1 thorpej } else if (EQUAL(w, "dontwarn"))
654 1.1 thorpej dontwarn = 1;
655 1.1 thorpej }
656 1.1 thorpej
657 1.1 thorpej /*
658 1.1 thorpej * Add the filter.
659 1.1 thorpej */
660 1.1 thorpej filter_dontwarn = dontwarn; /* XXX */
661 1.1 thorpej error = qcmd_add_filter(ifname, class_name, flname, &sfilt);
662 1.1 thorpej filter_dontwarn = 0; /* XXX */
663 1.1 thorpej if (error) {
664 1.1 thorpej LOG(LOG_ERR, 0,
665 1.6 itojun "can't add filter to class '%s' on interface '%s'",
666 1.1 thorpej class_name, ifname);
667 1.1 thorpej return (0);
668 1.1 thorpej }
669 1.1 thorpej return (1);
670 1.1 thorpej }
671 1.1 thorpej
672 1.1 thorpej #ifdef INET6
673 1.1 thorpej static int
674 1.1 thorpej filter6_parser(char *cmdbuf)
675 1.1 thorpej {
676 1.7 itojun char w[MAX_WORD], *cp = cmdbuf;
677 1.7 itojun char *ifname, class_name[MAX_WORD], fltr_name[MAX_WORD];
678 1.7 itojun char *flname = NULL;
679 1.1 thorpej struct flow_filter6 sfilt;
680 1.1 thorpej int protocol;
681 1.1 thorpej u_char tclass, tclassmask;
682 1.1 thorpej int ruleno;
683 1.1 thorpej int dontwarn = 0;
684 1.1 thorpej int ret;
685 1.1 thorpej
686 1.1 thorpej memset(&sfilt, 0, sizeof(sfilt));
687 1.1 thorpej sfilt.ff_flow6.fi6_family = AF_INET6;
688 1.1 thorpej
689 1.1 thorpej if (!get_ifname(&cp, &ifname)) {
690 1.7 itojun LOG(LOG_ERR, 0, "missing interface name");
691 1.1 thorpej return (0);
692 1.1 thorpej }
693 1.1 thorpej
694 1.1 thorpej if (!next_word(&cp, class_name)) {
695 1.7 itojun LOG(LOG_ERR, 0, "missing class name");
696 1.1 thorpej return (0);
697 1.1 thorpej }
698 1.1 thorpej
699 1.1 thorpej fltr_name[0] = '\0';
700 1.1 thorpej ruleno = 0;
701 1.4 itojun if (!get_fltr_opts(&cp, &fltr_name[0], sizeof(fltr_name), &ruleno)) {
702 1.7 itojun LOG(LOG_ERR, 0, "bad filter option");
703 1.1 thorpej return (0);
704 1.1 thorpej }
705 1.1 thorpej if (fltr_name[0] != '\0')
706 1.1 thorpej flname = fltr_name;
707 1.1 thorpej sfilt.ff_ruleno = ruleno;
708 1.1 thorpej
709 1.1 thorpej /* get filter destination address */
710 1.1 thorpej if (!get_ip6addr(&cp, &sfilt.ff_flow6.fi6_dst,
711 1.1 thorpej &sfilt.ff_mask6.mask6_dst)) {
712 1.7 itojun LOG(LOG_ERR, 0, "bad destination address");
713 1.1 thorpej return (0);
714 1.1 thorpej }
715 1.6 itojun
716 1.1 thorpej /* get filter destination port */
717 1.1 thorpej if (!next_word(&cp, w)) {
718 1.7 itojun LOG(LOG_ERR, 0, "missing filter destination port");
719 1.1 thorpej return (0);
720 1.1 thorpej }
721 1.1 thorpej if (!get_port(w, &sfilt.ff_flow6.fi6_dport)) {
722 1.7 itojun LOG(LOG_ERR, 0, "bad filter destination port");
723 1.1 thorpej return (0);
724 1.1 thorpej }
725 1.6 itojun
726 1.1 thorpej /* get filter source address */
727 1.1 thorpej if (!get_ip6addr(&cp, &sfilt.ff_flow6.fi6_src,
728 1.1 thorpej &sfilt.ff_mask6.mask6_src)) {
729 1.7 itojun LOG(LOG_ERR, 0, "bad source address");
730 1.1 thorpej return (0);
731 1.1 thorpej }
732 1.1 thorpej
733 1.1 thorpej /* get filter source port */
734 1.1 thorpej if (!next_word(&cp, w)) {
735 1.7 itojun LOG(LOG_ERR, 0, "missing filter source port");
736 1.1 thorpej return (0);
737 1.1 thorpej }
738 1.1 thorpej if (!get_port(w, &sfilt.ff_flow6.fi6_sport)) {
739 1.7 itojun LOG(LOG_ERR, 0, "bad filter source port");
740 1.1 thorpej return (0);
741 1.1 thorpej }
742 1.1 thorpej
743 1.1 thorpej /* get filter protocol id */
744 1.1 thorpej if (!next_word(&cp, w)) {
745 1.7 itojun LOG(LOG_ERR, 0, "missing filter protocol");
746 1.1 thorpej return (0);
747 1.1 thorpej }
748 1.1 thorpej if (!get_proto(w, &protocol)) {
749 1.7 itojun LOG(LOG_ERR, 0, "bad protocol");
750 1.1 thorpej return (0);
751 1.1 thorpej }
752 1.7 itojun sfilt.ff_flow6.fi6_proto = protocol;
753 1.1 thorpej
754 1.1 thorpej while (next_word(&cp, w)) {
755 1.1 thorpej if (EQUAL(w, "tclass")) {
756 1.1 thorpej tclass = 0;
757 1.1 thorpej tclassmask = 0xff;
758 1.1 thorpej
759 1.1 thorpej if (next_word(&cp, w)) {
760 1.1 thorpej tclass = (u_char)strtol(w, NULL, 0);
761 1.1 thorpej if (next_word(&cp, w)) {
762 1.1 thorpej if (EQUAL(w, "tclassmask")) {
763 1.1 thorpej next_word(&cp, w);
764 1.1 thorpej tclassmask =
765 1.1 thorpej (u_char)strtol(w, NULL, 0);
766 1.1 thorpej }
767 1.1 thorpej }
768 1.1 thorpej }
769 1.6 itojun sfilt.ff_flow6.fi6_tclass = tclass;
770 1.6 itojun sfilt.ff_mask6.mask6_tclass = tclassmask;
771 1.1 thorpej } else if (EQUAL(w, "gpi")) {
772 1.1 thorpej if (next_word(&cp, w)) {
773 1.1 thorpej sfilt.ff_flow6.fi6_gpi =
774 1.1 thorpej (u_int32_t)strtoul(w, NULL, 0);
775 1.1 thorpej sfilt.ff_flow6.fi6_gpi =
776 1.1 thorpej htonl(sfilt.ff_flow6.fi6_gpi);
777 1.1 thorpej }
778 1.1 thorpej } else if (EQUAL(w, "flowlabel")) {
779 1.1 thorpej if (next_word(&cp, w)) {
780 1.1 thorpej sfilt.ff_flow6.fi6_flowlabel =
781 1.1 thorpej (u_int32_t)strtoul(w, NULL, 0) & 0x000fffff;
782 1.1 thorpej sfilt.ff_flow6.fi6_flowlabel =
783 1.1 thorpej htonl(sfilt.ff_flow6.fi6_flowlabel);
784 1.1 thorpej }
785 1.1 thorpej } else if (EQUAL(w, "dontwarn"))
786 1.1 thorpej dontwarn = 1;
787 1.1 thorpej }
788 1.1 thorpej
789 1.1 thorpej /*
790 1.1 thorpej * Add the filter.
791 1.1 thorpej */
792 1.1 thorpej filter_dontwarn = dontwarn; /* XXX */
793 1.1 thorpej ret = qcmd_add_filter(ifname, class_name, flname,
794 1.1 thorpej (struct flow_filter *)&sfilt);
795 1.1 thorpej filter_dontwarn = 0; /* XXX */
796 1.1 thorpej if (ret) {
797 1.1 thorpej LOG(LOG_ERR, 0,
798 1.6 itojun "can't add filter to class '%s' on interface '%s'",
799 1.1 thorpej class_name, ifname);
800 1.1 thorpej return (0);
801 1.1 thorpej }
802 1.1 thorpej
803 1.1 thorpej return (1);
804 1.1 thorpej }
805 1.1 thorpej
806 1.1 thorpej static int
807 1.1 thorpej get_ip6addr(char **cpp, struct in6_addr *addr, struct in6_addr *mask)
808 1.1 thorpej {
809 1.7 itojun char w[MAX_WORD], *prefix;
810 1.1 thorpej u_char *cp;
811 1.1 thorpej int len;
812 1.1 thorpej
813 1.1 thorpej *addr = in6addr_any; /* set all 0 */
814 1.1 thorpej *mask = in6addr_any; /* set all 0 */
815 1.1 thorpej
816 1.1 thorpej if (!next_word(cpp, w))
817 1.1 thorpej return (0);
818 1.1 thorpej
819 1.1 thorpej if (EQUAL(w, "0"))
820 1.1 thorpej /* abbreviation of a wildcard (::0) */
821 1.1 thorpej return (1);
822 1.1 thorpej
823 1.1 thorpej if ((prefix = strchr(w, '/')) != NULL) {
824 1.1 thorpej /* address has prefix length */
825 1.1 thorpej *prefix++ = '\0';
826 1.1 thorpej }
827 1.1 thorpej
828 1.3 itojun if (inet_pton(AF_INET6, w, addr) != 1)
829 1.1 thorpej return (0);
830 1.1 thorpej
831 1.1 thorpej if (IN6_IS_ADDR_UNSPECIFIED(addr) && prefix == NULL)
832 1.1 thorpej /* wildcard */
833 1.1 thorpej return (1);
834 1.1 thorpej
835 1.1 thorpej /* convert address prefix length to address mask */
836 1.1 thorpej if (prefix != NULL) {
837 1.1 thorpej len = (int)strtol(prefix, NULL, 0);
838 1.1 thorpej if ((len < 0) || (len > 128))
839 1.1 thorpej return (0);
840 1.1 thorpej for (cp = (u_char *)mask; len > 7; len -= 8)
841 1.1 thorpej *cp++ = 0xff;
842 1.1 thorpej if (len > 0)
843 1.1 thorpej *cp = (0xff << (8 - len)) & 0xff;
844 1.1 thorpej
845 1.1 thorpej IN6ADDR32(addr, 0) &= IN6ADDR32(mask, 0);
846 1.1 thorpej IN6ADDR32(addr, 1) &= IN6ADDR32(mask, 1);
847 1.1 thorpej IN6ADDR32(addr, 2) &= IN6ADDR32(mask, 2);
848 1.1 thorpej IN6ADDR32(addr, 3) &= IN6ADDR32(mask, 3);
849 1.1 thorpej } else
850 1.1 thorpej /* full mask */
851 1.1 thorpej memset(mask, 0xff, sizeof(struct in6_addr));
852 1.1 thorpej
853 1.1 thorpej return (1);
854 1.1 thorpej }
855 1.1 thorpej
856 1.1 thorpej #endif /* INET6 */
857 1.1 thorpej
858 1.1 thorpej static int
859 1.1 thorpej ctl_parser(char *cmdbuf)
860 1.1 thorpej {
861 1.7 itojun char w[MAX_WORD], *cp = cmdbuf;
862 1.1 thorpej char *ifname;
863 1.1 thorpej int state;
864 1.1 thorpej int rval;
865 1.7 itojun
866 1.1 thorpej if (!get_ifname(&cp, &ifname)) {
867 1.1 thorpej printf("missing interface name in %s, line %d",
868 1.1 thorpej altqconfigfile, line_no);
869 1.1 thorpej return (0);
870 1.1 thorpej }
871 1.1 thorpej
872 1.1 thorpej if (!next_word(&cp, w)) {
873 1.1 thorpej state = is_q_enabled(ifname);
874 1.1 thorpej printf("altq %s on %s\n",
875 1.1 thorpej state ? "enabled" : "disabled", ifname);
876 1.1 thorpej return (1);
877 1.1 thorpej }
878 1.6 itojun
879 1.1 thorpej if (EQUAL(w, "enable")) {
880 1.1 thorpej rval = qcmd_enable(ifname);
881 1.1 thorpej printf("altq %s on %s\n",
882 1.1 thorpej (rval == 0) ? "enabled" : "enable failed!", ifname);
883 1.1 thorpej } else if (EQUAL(w, "disable")) {
884 1.1 thorpej rval = qcmd_disable(ifname);
885 1.1 thorpej printf("altq %s on %s\n",
886 1.1 thorpej (rval == 0) ? "disabled" : "disable failed!", ifname);
887 1.1 thorpej } else if (EQUAL(w, "reload")) {
888 1.1 thorpej printf("reinitializing altq...\n");
889 1.1 thorpej qcmd_destroyall();
890 1.1 thorpej qcmd_init();
891 1.1 thorpej } else
892 1.1 thorpej return (0);
893 1.1 thorpej return (1);
894 1.1 thorpej }
895 1.1 thorpej
896 1.1 thorpej static int
897 1.1 thorpej delete_parser(char *cmdbuf)
898 1.1 thorpej {
899 1.1 thorpej char *cp = cmdbuf;
900 1.7 itojun char *ifname, class_name[MAX_WORD], filter_name[MAX_WORD];
901 1.1 thorpej int ret;
902 1.7 itojun
903 1.1 thorpej if (!get_ifname(&cp, &ifname)) {
904 1.7 itojun LOG(LOG_ERR, 0, "missing interface name");
905 1.1 thorpej return (0);
906 1.1 thorpej }
907 1.1 thorpej
908 1.1 thorpej if (!next_word(&cp, class_name)) {
909 1.7 itojun LOG(LOG_ERR, 0, "missing class name");
910 1.1 thorpej return (0);
911 1.1 thorpej }
912 1.1 thorpej
913 1.7 itojun /* check if filter is specified */
914 1.7 itojun if (next_word(&cp, filter_name)) {
915 1.7 itojun ret = qcmd_delete_filter(ifname, class_name, filter_name);
916 1.7 itojun if (ret) {
917 1.7 itojun LOG(LOG_ERR, 0,
918 1.7 itojun "can't delete filter '%s' on interface '%s'",
919 1.7 itojun filter_name, ifname);
920 1.7 itojun return (0);
921 1.7 itojun }
922 1.7 itojun return (1);
923 1.7 itojun }
924 1.7 itojun
925 1.1 thorpej ret = qcmd_delete_class(ifname, class_name);
926 1.1 thorpej if (ret) {
927 1.1 thorpej LOG(LOG_ERR, 0,
928 1.6 itojun "can't delete class '%s' on interface '%s'",
929 1.1 thorpej class_name, ifname);
930 1.1 thorpej return (0);
931 1.1 thorpej }
932 1.1 thorpej
933 1.1 thorpej return (1);
934 1.1 thorpej }
935 1.1 thorpej
936 1.1 thorpej static int
937 1.1 thorpej red_parser(char *cmdbuf)
938 1.1 thorpej {
939 1.7 itojun char w[MAX_WORD], *cp = cmdbuf;
940 1.1 thorpej int th_min, th_max, inv_pmax;
941 1.1 thorpej
942 1.1 thorpej if (!next_word(&cp, w))
943 1.1 thorpej goto bad;
944 1.1 thorpej th_min = (int)strtol(w, NULL, 0);
945 1.1 thorpej
946 1.1 thorpej if (!next_word(&cp, w))
947 1.1 thorpej goto bad;
948 1.1 thorpej th_max = (int)strtol(w, NULL, 0);
949 1.1 thorpej
950 1.1 thorpej if (!next_word(&cp, w))
951 1.1 thorpej goto bad;
952 1.1 thorpej inv_pmax = (int)strtol(w, NULL, 0);
953 1.1 thorpej
954 1.1 thorpej if (qop_red_set_defaults(th_min, th_max, inv_pmax) != 0) {
955 1.6 itojun LOG(LOG_ERR, 0, "can't set red default parameters");
956 1.1 thorpej return (0);
957 1.1 thorpej }
958 1.1 thorpej
959 1.1 thorpej return (1);
960 1.1 thorpej
961 1.1 thorpej bad:
962 1.7 itojun LOG(LOG_ERR, 0, "bad red parameter");
963 1.1 thorpej return (0);
964 1.1 thorpej }
965 1.1 thorpej
966 1.1 thorpej static int
967 1.1 thorpej rio_parser(char *cmdbuf)
968 1.1 thorpej {
969 1.7 itojun char w[MAX_WORD], *cp = cmdbuf;
970 1.1 thorpej int i;
971 1.1 thorpej struct redparams params[RIO_NDROPPREC];
972 1.1 thorpej
973 1.1 thorpej for (i = 0; i < RIO_NDROPPREC; i++) {
974 1.1 thorpej if (!next_word(&cp, w))
975 1.1 thorpej goto bad;
976 1.1 thorpej params[i].th_min = (int)strtol(w, NULL, 0);
977 1.1 thorpej
978 1.1 thorpej if (!next_word(&cp, w))
979 1.1 thorpej goto bad;
980 1.1 thorpej params[i].th_max = (int)strtol(w, NULL, 0);
981 1.1 thorpej
982 1.1 thorpej if (!next_word(&cp, w))
983 1.1 thorpej goto bad;
984 1.1 thorpej params[i].inv_pmax = (int)strtol(w, NULL, 0);
985 1.1 thorpej }
986 1.1 thorpej
987 1.1 thorpej if (qop_rio_set_defaults(¶ms[0]) != 0) {
988 1.6 itojun LOG(LOG_ERR, 0, "can't set rio default parameters");
989 1.1 thorpej return (0);
990 1.1 thorpej }
991 1.1 thorpej
992 1.1 thorpej return (1);
993 1.1 thorpej
994 1.1 thorpej bad:
995 1.7 itojun LOG(LOG_ERR, 0, "bad rio parameter");
996 1.1 thorpej return (0);
997 1.1 thorpej }
998 1.1 thorpej
999 1.1 thorpej static int
1000 1.1 thorpej conditioner_parser(char *cmdbuf)
1001 1.1 thorpej {
1002 1.7 itojun char cdnr_name[MAX_WORD], *cp = cmdbuf;
1003 1.1 thorpej char *ifname;
1004 1.7 itojun struct tc_action action[MAX_ACTIONS];
1005 1.7 itojun
1006 1.1 thorpej if (!get_ifname(&cp, &ifname)) {
1007 1.7 itojun LOG(LOG_ERR, 0, "missing interface name");
1008 1.1 thorpej return (0);
1009 1.1 thorpej }
1010 1.1 thorpej
1011 1.1 thorpej /* get conditioner name */
1012 1.1 thorpej if (!next_word(&cp, cdnr_name)) {
1013 1.7 itojun LOG(LOG_ERR, 0, "missing cdnr name");
1014 1.1 thorpej return (0);
1015 1.1 thorpej }
1016 1.1 thorpej
1017 1.1 thorpej if (tc_action_parser(ifname, &cp, &action[0]) == 0)
1018 1.1 thorpej return (0);
1019 1.1 thorpej
1020 1.1 thorpej if (qcmd_cdnr_add_element(NULL, ifname, cdnr_name, &action[0]) != 0)
1021 1.1 thorpej return (0);
1022 1.1 thorpej return (1);
1023 1.1 thorpej }
1024 1.1 thorpej
1025 1.1 thorpej /*
1026 1.1 thorpej * recursively parse '<'tc_action'>'
1027 1.1 thorpej * note that array "action" grows during recursive parse.
1028 1.1 thorpej */
1029 1.1 thorpej static int
1030 1.1 thorpej tc_action_parser(char *ifname, char **cpp, struct tc_action *action)
1031 1.1 thorpej {
1032 1.1 thorpej char *cp, *start, *end;
1033 1.7 itojun char type[MAX_WORD], w[MAX_WORD];
1034 1.1 thorpej int depth, i;
1035 1.1 thorpej struct tb_profile profile[2];
1036 1.6 itojun
1037 1.1 thorpej /*
1038 1.1 thorpej * find a possibly nested pair of '<' and '>',
1039 1.1 thorpej * make them pointed by 'start' and 'end'.
1040 1.1 thorpej */
1041 1.1 thorpej start = strchr(*cpp, '<');
1042 1.1 thorpej if (start == NULL) {
1043 1.7 itojun LOG(LOG_ERR, 0, "conditioner action missing");
1044 1.1 thorpej return (0);
1045 1.1 thorpej }
1046 1.1 thorpej depth = 1;
1047 1.1 thorpej cp = start + 1;
1048 1.1 thorpej do {
1049 1.1 thorpej end = strpbrk(cp, "<>");
1050 1.1 thorpej if (end == NULL) {
1051 1.1 thorpej LOG(LOG_ERR, 0,
1052 1.7 itojun "conditioner action delimiter mismatch");
1053 1.1 thorpej return (0);
1054 1.1 thorpej }
1055 1.1 thorpej if (*end == '<')
1056 1.1 thorpej depth++;
1057 1.1 thorpej else if (*end == '>')
1058 1.1 thorpej depth--;
1059 1.1 thorpej cp = end + 1;
1060 1.1 thorpej } while (depth > 0);
1061 1.1 thorpej *end = '\0';
1062 1.1 thorpej *cpp = end + 1;
1063 1.1 thorpej cp = start + 1;
1064 1.1 thorpej
1065 1.1 thorpej if (IsDebug(DEBUG_ALTQ)) {
1066 1.1 thorpej printf("tc_action_parser: [%s]\n", cp);
1067 1.1 thorpej }
1068 1.1 thorpej
1069 1.1 thorpej if (!next_word(&cp, type)) {
1070 1.7 itojun LOG(LOG_ERR, 0, "missing conditioner action type");
1071 1.1 thorpej return (0);
1072 1.1 thorpej }
1073 1.6 itojun
1074 1.1 thorpej /*
1075 1.1 thorpej * action type specific process
1076 1.1 thorpej */
1077 1.1 thorpej if (EQUAL(type, "conditioner")) {
1078 1.1 thorpej if (!next_word(&cp, w)) {
1079 1.1 thorpej LOG(LOG_ERR, 0,
1080 1.7 itojun "missing conditioner name");
1081 1.1 thorpej return (0);
1082 1.1 thorpej }
1083 1.1 thorpej action->tca_code = TCACODE_HANDLE;
1084 1.1 thorpej action->tca_handle = cdnr_name2handle(ifname, w);
1085 1.1 thorpej if (action->tca_handle == CDNR_NULL_HANDLE) {
1086 1.1 thorpej LOG(LOG_ERR, 0,
1087 1.7 itojun "wrong conditioner name %s", w);
1088 1.1 thorpej return (0);
1089 1.1 thorpej }
1090 1.1 thorpej } else if (EQUAL(type, "pass")) {
1091 1.1 thorpej action->tca_code = TCACODE_PASS;
1092 1.1 thorpej } else if (EQUAL(type, "drop")) {
1093 1.1 thorpej action->tca_code = TCACODE_DROP;
1094 1.1 thorpej } else if (EQUAL(type, "mark")) {
1095 1.1 thorpej if (!next_word(&cp, w)) {
1096 1.7 itojun LOG(LOG_ERR, 0, "missing dscp");
1097 1.1 thorpej return (0);
1098 1.1 thorpej }
1099 1.1 thorpej action->tca_code = TCACODE_MARK;
1100 1.1 thorpej action->tca_dscp = (u_int8_t)strtol(w, NULL, 0);
1101 1.1 thorpej } else if (EQUAL(type, "tbmeter")) {
1102 1.1 thorpej if (!next_word(&cp, w)) {
1103 1.7 itojun LOG(LOG_ERR, 0, "missing tb profile");
1104 1.1 thorpej return (0);
1105 1.1 thorpej }
1106 1.1 thorpej profile[0].rate = atobps(w);
1107 1.1 thorpej if (!next_word(&cp, w)) {
1108 1.7 itojun LOG(LOG_ERR, 0, "missing tb profile");
1109 1.1 thorpej return (0);
1110 1.1 thorpej }
1111 1.1 thorpej profile[0].depth = atobytes(w);
1112 1.1 thorpej if (tc_action_parser(ifname, &cp, &action[1]) == 0)
1113 1.1 thorpej return (0);
1114 1.1 thorpej if (tc_action_parser(ifname, &cp, &action[2]) == 0)
1115 1.1 thorpej return (0);
1116 1.1 thorpej
1117 1.1 thorpej if (qcmd_cdnr_add_tbmeter(action, ifname, NULL, &profile[0],
1118 1.1 thorpej &action[1], &action[2]) != 0)
1119 1.1 thorpej return (0);
1120 1.1 thorpej } else if (EQUAL(type, "trtcm")) {
1121 1.1 thorpej int coloraware = 0; /* default is color-blind */
1122 1.1 thorpej
1123 1.1 thorpej for (i=0; i<2; i++) {
1124 1.1 thorpej if (!next_word(&cp, w)) {
1125 1.7 itojun LOG(LOG_ERR, 0, "missing tb profile");
1126 1.1 thorpej return (0);
1127 1.1 thorpej }
1128 1.1 thorpej profile[i].rate = atobps(w);
1129 1.1 thorpej if (!next_word(&cp, w)) {
1130 1.7 itojun LOG(LOG_ERR, 0, "missing tb profile");
1131 1.1 thorpej return (0);
1132 1.1 thorpej }
1133 1.1 thorpej profile[i].depth = atobytes(w);
1134 1.1 thorpej }
1135 1.1 thorpej if (tc_action_parser(ifname, &cp, &action[1]) == 0)
1136 1.1 thorpej return (0);
1137 1.1 thorpej if (tc_action_parser(ifname, &cp, &action[2]) == 0)
1138 1.1 thorpej return (0);
1139 1.1 thorpej if (tc_action_parser(ifname, &cp, &action[3]) == 0)
1140 1.1 thorpej return (0);
1141 1.1 thorpej if (next_word(&cp, w)) {
1142 1.1 thorpej if (EQUAL(w, "coloraware"))
1143 1.1 thorpej coloraware = 1;
1144 1.1 thorpej else if (EQUAL(w, "colorblind"))
1145 1.1 thorpej coloraware = 0;
1146 1.1 thorpej }
1147 1.1 thorpej
1148 1.1 thorpej if (qcmd_cdnr_add_trtcm(action, ifname, NULL,
1149 1.1 thorpej &profile[0], &profile[1],
1150 1.1 thorpej &action[1], &action[2], &action[3],
1151 1.1 thorpej coloraware) != 0)
1152 1.1 thorpej return (0);
1153 1.1 thorpej } else if (EQUAL(type, "tswtcm")) {
1154 1.1 thorpej u_int32_t cmtd_rate, peak_rate, avg_interval;
1155 1.7 itojun
1156 1.1 thorpej if (!next_word(&cp, w)) {
1157 1.7 itojun LOG(LOG_ERR, 0, "missing cmtd rate");
1158 1.1 thorpej return (0);
1159 1.1 thorpej }
1160 1.1 thorpej cmtd_rate = atobps(w);
1161 1.1 thorpej
1162 1.1 thorpej if (!next_word(&cp, w)) {
1163 1.7 itojun LOG(LOG_ERR, 0, "missing peak rate");
1164 1.1 thorpej return (0);
1165 1.1 thorpej }
1166 1.1 thorpej peak_rate = atobps(w);
1167 1.1 thorpej
1168 1.1 thorpej if (!next_word(&cp, w)) {
1169 1.7 itojun LOG(LOG_ERR, 0, "missing avg interval");
1170 1.1 thorpej return (0);
1171 1.1 thorpej }
1172 1.1 thorpej avg_interval = (u_int32_t)strtoul(w, NULL, 0);
1173 1.1 thorpej
1174 1.1 thorpej if (tc_action_parser(ifname, &cp, &action[1]) == 0)
1175 1.1 thorpej return (0);
1176 1.1 thorpej if (tc_action_parser(ifname, &cp, &action[2]) == 0)
1177 1.1 thorpej return (0);
1178 1.1 thorpej if (tc_action_parser(ifname, &cp, &action[3]) == 0)
1179 1.1 thorpej return (0);
1180 1.1 thorpej
1181 1.1 thorpej if (qcmd_cdnr_add_tswtcm(action, ifname, NULL,
1182 1.1 thorpej cmtd_rate, peak_rate, avg_interval,
1183 1.1 thorpej &action[1], &action[2], &action[3])
1184 1.1 thorpej != 0)
1185 1.1 thorpej return (0);
1186 1.1 thorpej } else {
1187 1.8 wiz LOG(LOG_ERR, 0, "unknown action type %s");
1188 1.1 thorpej return (0);
1189 1.1 thorpej }
1190 1.6 itojun
1191 1.1 thorpej *end = '>'; /* restore the end delimiter */
1192 1.1 thorpej
1193 1.1 thorpej return (1);
1194 1.1 thorpej }
1195