quip_server.c revision 1.3 1 /* $NetBSD: quip_server.c,v 1.3 2001/08/16 07:48:15 itojun Exp $ */
2 /* $KAME: quip_server.c,v 1.4 2001/08/15 12:51:58 kjc Exp $ */
3 /*
4 * Copyright (C) 1999-2000
5 * Sony Computer Science Laboratories, Inc. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <sys/queue.h>
32
33 #include <net/if.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <stddef.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <err.h>
44
45 #include <altq/altq.h>
46 #include <altq/altq_red.h>
47 #include <altq/altq_rio.h>
48
49 #include "altq_qop.h"
50 #include "quip_server.h"
51
52 extern LIST_HEAD(qop_iflist, ifinfo) qop_iflist;
53
54 #define EQUAL(s1, s2) (strcmp((s1), (s2)) == 0)
55
56 static int next_word(char **, char *);
57
58 static int query_list(const char *, const char *, char *, size_t);
59 static int query_handle2name(const char *, const char *, char *, size_t);
60 static int query_qdisc(const char *, const char *, char *, size_t);
61 static int query_filterspec(const char *, const char *, char *, size_t);
62
63 int
64 quip_input(FILE *fp)
65 {
66 char request[REQ_MAXSIZE], result[RES_MAXSIZE], body[BODY_MAXSIZE],
67 w[REQ_MAXSIZE], *cp, *query;
68 int n = 0;
69
70 while (1) {
71 if (fgets(request, REQ_MAXSIZE, fp) == NULL) /* EOF */
72 return (-1);
73 /* skip preceding blank lines */
74 if (request[0] == '\n')
75 continue;
76 break;
77 }
78
79 /* remove trailing newline and white space */
80 if ((cp = strrchr(request, '\n')) != NULL) {
81 *cp-- = '\0';
82 while (*cp == ' ' || *cp == '\t')
83 *cp-- = '\0';
84 }
85
86 body[0] = '\0';
87 cp = request;
88 if (!next_word(&cp, w)) {
89 snprintf(result, sizeof(result), "400 Bad request\n");
90 goto done;
91 }
92 if (EQUAL(w, "GET")) {
93 if (!next_word(&cp, w)) {
94 snprintf(result, sizeof(result), "400 Bad request\n");
95 goto done;
96 }
97 if ((query = strchr(w, '?')) != NULL) {
98 /* request has a query string */
99 *query = '\0';
100 query++;
101 }
102
103 if (EQUAL(w, "list")) {
104 n = query_list(w, query, body, BODY_MAXSIZE);
105 } else if (EQUAL(w, "handle-to-name")) {
106 n = query_handle2name(w, query, body, BODY_MAXSIZE);
107 } else if (EQUAL(w, "qdisc")) {
108 n = query_qdisc(w, query, body, BODY_MAXSIZE);
109 } else if (EQUAL(w, "filter")) {
110 n = query_filterspec(w, query, body, BODY_MAXSIZE);
111 } else {
112 snprintf(result, sizeof(result), "400 Bad request\n");
113 goto done;
114 }
115 } else {
116 snprintf(result, sizeof(result), "400 Bad request\n");
117 goto done;
118 }
119
120 if (n == 0) {
121 snprintf(result, sizeof(result), "204 No content\n");
122 } else if (n < 0) {
123 snprintf(result, sizeof(result), "400 Bad request\n");
124 } else {
125 snprintf(result, sizeof(result), "200 OK\nContent-Length:%d\n", n);
126 }
127
128 done:
129 /* send a result line and a blank line */
130 if (fputs ("QUIP/1.0 ", fp) != 0 ||
131 fputs(result, fp) != 0 || fputs("\n", fp) != 0)
132 return (-1);
133
134 /* send message body */
135 if (fputs(body, fp) != 0)
136 return (-1);
137 return (0);
138 }
139
140 /*
141 * Skip leading blanks, then copy next word (delimited by blank or zero, but
142 * no longer than 63 bytes) into buffer b, set scan pointer to following
143 * non-blank (or end of string), and return 1. If there is no non-blank text,
144 * set scan ptr to point to 0 byte and return 0.
145 */
146 static int
147 next_word(char **cpp, char *b)
148 {
149 char *tp;
150 int L;
151
152 *cpp += strspn(*cpp, " \t");
153 if (**cpp == '\0' || **cpp == '\n' || **cpp == '#')
154 return(0);
155
156 tp = strpbrk(*cpp, " \t\n#");
157 L = MIN((tp)?(tp-*cpp):strlen(*cpp), 63);
158 strncpy(b, *cpp, L);
159 *(b + L) = '\0';
160 *cpp += L;
161 *cpp += strspn(*cpp, " \t");
162 return (1);
163 }
164
165
166 /*
167 * expand_classname creates a long class name.
168 * <ifname>:/<root_name>/../<parent_name>/<class_name>
169 */
170 static int
171 expand_classname(struct classinfo *clinfo, char *name, size_t maxname)
172 {
173 struct classinfo *ci = clinfo;
174 #define CLASSNAMEMAX 256
175 char buf[2][CLASSNAMEMAX], *b0, *b1, *tmp;
176
177 b0 = buf[0]; b1 = buf[1];
178 b1[0] = '\0';
179 while (ci != NULL) {
180 strlcpy(b0, "/", CLASSNAMEMAX);
181 strlcat(b0, ci->clname, CLASSNAMEMAX);
182 strlcat(b0, b1, CLASSNAMEMAX);
183
184 ci = ci->parent;
185 tmp = b0; b0 = b1; b1 = tmp;
186 }
187 snprintf(b0, CLASSNAMEMAX, "%s:", clinfo->ifinfo->ifname);
188 strlcat(b0, b1, CLASSNAMEMAX);
189 strlcpy(name, b0, CLASSNAMEMAX);
190 return (strlen(name));
191 #undef CLASSNAMEMAX
192 }
193
194 /*
195 * expand_filtername creates a long filter name.
196 * <ifname>:/<root_name>/../<parent_name>/<class_name>:<fltr_name>
197 */
198 static int
199 expand_filtername(struct fltrinfo *fltrinfo, char *name, size_t maxname)
200 {
201 int len;
202
203 len = expand_classname(fltrinfo->clinfo, name, maxname);
204 len += snprintf(name + len, maxname - len, ":%s", fltrinfo->flname);
205 return (len);
206 }
207
208 static int
209 query_handle2name(const char *cmd, const char *arg, char *msg, size_t maxmsg)
210 {
211 struct ifinfo *ifinfo;
212 struct classinfo *clinfo;
213 struct fltrinfo *fltrinfo;
214 char *ifname, *class_field, *fltr_field, buf[256], *cp;
215 u_long handle;
216 int len, size;
217
218 strlcpy(buf, arg, sizeof(buf));
219 cp = buf;
220 ifname = strsep(&cp, ":");
221 class_field = strsep(&cp, ":");
222 fltr_field = cp;
223
224 if (fltr_field != NULL) {
225 if (sscanf(fltr_field, "%lx", &handle) != 1)
226 return (-1);
227 if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
228 return (-1);
229 if ((fltrinfo = flhandle2fltrinfo(ifinfo, handle)) == NULL)
230 return (-1);
231
232 len = expand_filtername(fltrinfo, msg, maxmsg);
233 } else {
234 if (sscanf(class_field, "%lx", &handle) != 1)
235 return (-1);
236 if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
237 return (-1);
238 if ((clinfo = clhandle2clinfo(ifinfo, handle)) == NULL)
239 return (-1);
240
241 len = expand_classname(clinfo, msg, maxmsg);
242 }
243 size = len + snprintf(msg + len, maxmsg - len, "\n");
244 return (size);
245 }
246
247 static int
248 query_qdisc(const char *cmd, const char *arg, char *msg, size_t maxmsg)
249 {
250 struct ifinfo *ifinfo;
251 int size;
252
253 if ((ifinfo = ifname2ifinfo(arg)) == NULL)
254 return (-1);
255
256 size = snprintf(msg, maxmsg, "%s\nbandwidth:%.2fMbps\nstatus:%s\n",
257 ifinfo->qdisc->qname, (double)ifinfo->bandwidth/1000000,
258 (ifinfo->enabled ? "enabled" : "disabled"));
259 return (size);
260 }
261
262 static int
263 query_filterspec(const char *cmd, const char *arg, char *msg, size_t maxmsg)
264 {
265 struct ifinfo *ifinfo;
266 struct fltrinfo *fltrinfo;
267 struct flow_filter *filt;
268 char *ifname, *class_field, *fltr_field, buf[256], *cp;
269 u_long handle;
270 int size;
271
272 strlcpy(buf, arg, sizeof(buf));
273 cp = buf;
274 ifname = strsep(&cp, ":");
275 class_field = strsep(&cp, ":");
276 fltr_field = cp;
277
278 if (fltr_field == NULL)
279 return (-1);
280 if (sscanf(fltr_field, "%lx", &handle) != 1)
281 return (-1);
282
283 if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
284 return (-1);
285 if ((fltrinfo = flhandle2fltrinfo(ifinfo, handle)) == NULL)
286 return (-1);
287
288 filt = &fltrinfo->fltr;
289
290 if (filt->ff_flow.fi_family == AF_INET) {
291 char src[128], dst[128], smask[128], dmask[128], tos[128];
292
293 if (filt->ff_flow.fi_dst.s_addr == 0) {
294 snprintf(dst, sizeof(dst), "0");
295 dmask[0] = '\0';
296 } else {
297 snprintf(dst, sizeof(dst), "%s",
298 inet_ntoa(filt->ff_flow.fi_dst));
299 if (filt->ff_mask.mask_dst.s_addr == 0xffffffff)
300 dmask[0] = '\0';
301 else
302 snprintf(dmask, sizeof(dmask), " mask %#x",
303 ntoh32(filt->ff_mask.mask_dst.s_addr));
304 }
305 if (filt->ff_flow.fi_src.s_addr == 0) {
306 snprintf(src, sizeof(src), "0");
307 smask[0] = '\0';
308 } else {
309 snprintf(src, sizeof(src), "%s",
310 inet_ntoa(filt->ff_flow.fi_src));
311 if (filt->ff_mask.mask_src.s_addr == 0xffffffff)
312 smask[0] = '\0';
313 else
314 snprintf(smask, sizeof(smask), " mask %#x",
315 ntoh32(filt->ff_mask.mask_src.s_addr));
316 }
317 if (filt->ff_flow.fi_tos == 0)
318 tos[0] = '\0';
319 else
320 snprintf(tos, sizeof(tos), " tos %#x tosmask %#x",
321 filt->ff_flow.fi_tos,
322 filt->ff_mask.mask_tos);
323
324 size = snprintf(msg, maxmsg, "inet %s%s %d %s%s %d %d%s\n",
325 dst, dmask,
326 ntoh16(filt->ff_flow.fi_dport),
327 src, smask,
328 ntoh16(filt->ff_flow.fi_sport),
329 filt->ff_flow.fi_proto, tos);
330 }
331 #ifdef INET6
332 else if (filt->ff_flow.fi_family == AF_INET6) {
333 struct flow_filter6 *filt6;
334 char dst6[INET6_ADDRSTRLEN], dmask6[INET6_ADDRSTRLEN];
335 char src6[INET6_ADDRSTRLEN], smask6[INET6_ADDRSTRLEN];
336 char tclass6[128];
337 const struct in6_addr mask128 =
338 {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
339 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}};
340
341 filt6 = (struct flow_filter6 *)&fltrinfo->fltr;
342 if (IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_dst)) {
343 snprintf(dst6, sizeof(dst6), "0");
344 dmask6[0] = '\0';
345 } else {
346 inet_ntop(AF_INET6, &filt6->ff_flow6.fi6_dst,
347 dst6, sizeof(dst6));
348 if (IN6_ARE_ADDR_EQUAL(&mask128,
349 &filt6->ff_mask6.mask6_dst))
350 dmask6[0] = '\0';
351 else {
352 snprintf(dmask6, sizeof(dmask6), " mask ");
353 inet_ntop(AF_INET6, &filt6->ff_mask6.mask6_dst,
354 dmask6 + 6, sizeof(dmask6) -6);
355 }
356 }
357
358 if (IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_src)) {
359 snprintf(src6, sizeof(src6), "0");
360 smask6[0] = '\0';
361 } else {
362 inet_ntop(AF_INET6, &filt6->ff_flow6.fi6_src,
363 src6, sizeof(src6));
364 if (IN6_ARE_ADDR_EQUAL(&mask128,
365 &filt6->ff_mask6.mask6_src))
366 smask6[0] = '\0';
367 else {
368 snprintf(smask6, sizeof(smask6), " mask ");
369 inet_ntop(AF_INET6, &filt6->ff_mask6.mask6_src,
370 smask6 + 6, sizeof(smask6) -6);
371 }
372 }
373 if (filt6->ff_flow6.fi6_tclass == 0)
374 tclass6[0] = '\0';
375 else
376 snprintf(tclass6, sizeof(tclass6),
377 " tclass %#x tclassmask %#x",
378 filt6->ff_flow6.fi6_tclass,
379 filt6->ff_mask6.mask6_tclass);
380
381 size = snprintf(msg, maxmsg, "inet6 %s%s %d %s%s %d %d%s\n",
382 dst6, dmask6,
383 ntoh16(filt6->ff_flow6.fi6_dport),
384 src6, smask6,
385 ntoh16(filt6->ff_flow6.fi6_sport),
386 filt6->ff_flow6.fi6_proto, tclass6);
387 }
388 #endif /* INET6 */
389
390 return (size);
391 }
392
393
394 /*
395 * string_match compares 2 strings and returns 1 when s1 matches s2.
396 * s1: possibly includes wildcards, "*".
397 * s2: must be a full string (should not include "*").
398 */
399 static int
400 string_match(const char *s1, const char *s2)
401 {
402 char *ap, *next, sub[256];
403 int prefixlen, sublen;
404
405 /* if there's no wild card, compare full string */
406 if ((ap = strchr(s1, '*')) == NULL)
407 return (strcmp(s1, s2) == 0);
408
409 /* compare string prefix */
410 prefixlen = ap - s1;
411 if (strncmp(s1, s2, prefixlen) != 0)
412 return (0);
413 s2 += prefixlen;
414
415 /*
416 * if there is another wildcard in the rest of the string,
417 * compare the substring between the 2 wildcards.
418 */
419 while ((next = strchr(ap + 1, '*')) != NULL) {
420 sublen = next - ap - 1;
421 strncpy(sub, ap+1, sublen);
422 sub[sublen] = '\0';
423 if ((s2 = strstr(s2, sub)) == NULL)
424 return (0);
425
426 s2 += sublen;
427 ap = next;
428 }
429
430 /* no more wildcard, compare the rest of the string */
431 return (strcmp(ap+1, s2+strlen(s2)-strlen(ap+1)) == 0);
432 }
433
434 static int
435 query_list(const char *cmd, const char *arg, char *msg, size_t maxmsg)
436 {
437 char tmp[256], *cp, *ep;
438 struct ifinfo *ifinfo;
439 struct classinfo *clinfo;
440 struct fltrinfo *fltrinfo;
441 int print_if, print_class, print_fltr, size = 0;
442
443 if (arg == NULL) {
444 /* no arg, print all */
445 print_if = print_class = print_fltr = 1;
446 } else {
447 print_if = print_class = print_fltr = 0;
448 if ((cp = strchr(arg, ':')) == NULL)
449 print_if = 1;
450 else if (strchr(cp+1, ':') == NULL)
451 print_class = 1;
452 else
453 print_fltr = 1;
454 }
455
456 cp = msg;
457 ep = msg + maxmsg;
458 LIST_FOREACH(ifinfo, &qop_iflist, next) {
459 if (print_if) {
460 strlcpy(tmp, ifinfo->ifname, sizeof(tmp));
461 if (arg == NULL || string_match(arg, tmp))
462 cp += snprintf(cp, ep - cp, "%#010x\t%s\n",
463 ifinfo->ifindex, tmp);
464 }
465 if (!print_class && !print_fltr)
466 continue;
467 for (clinfo = get_rootclass(ifinfo);
468 clinfo != NULL; clinfo = get_nextclass(clinfo)) {
469 if (print_class) {
470 expand_classname(clinfo, tmp, sizeof(tmp));
471 if (arg == NULL || string_match(arg, tmp))
472 cp += snprintf(cp, ep - cp,
473 "%#010lx\t%s\n",
474 clinfo->handle, tmp);
475 }
476 if (!print_fltr)
477 continue;
478 LIST_FOREACH(fltrinfo, &clinfo->fltrlist, next) {
479 expand_filtername(fltrinfo, tmp, sizeof(tmp));
480 if (arg == NULL || string_match(arg, tmp))
481 cp += snprintf(cp, ep - cp, "%#010lx\t%s\n",
482 fltrinfo->handle, tmp);
483 }
484 }
485 }
486 size = cp - msg;
487 return (size);
488 }
489
490