qop.c revision 1.1 1 1.1 thorpej /* $KAME: qop.c,v 1.6 2000/10/18 09:15:18 kjc Exp $ */
2 1.1 thorpej /*
3 1.1 thorpej * Copyright (C) 1999-2000
4 1.1 thorpej * Sony Computer Science Laboratories, Inc. All rights reserved.
5 1.1 thorpej *
6 1.1 thorpej * Redistribution and use in source and binary forms, with or without
7 1.1 thorpej * modification, are permitted provided that the following conditions
8 1.1 thorpej * are met:
9 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
10 1.1 thorpej * notice, this list of conditions and the following disclaimer.
11 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
13 1.1 thorpej * documentation and/or other materials provided with the distribution.
14 1.1 thorpej *
15 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
16 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
19 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 thorpej * SUCH DAMAGE.
26 1.1 thorpej */
27 1.1 thorpej
28 1.1 thorpej #include <sys/param.h>
29 1.1 thorpej #include <sys/socket.h>
30 1.1 thorpej #include <sys/sockio.h>
31 1.1 thorpej #include <sys/ioctl.h>
32 1.1 thorpej #include <sys/fcntl.h>
33 1.1 thorpej #include <sys/stat.h>
34 1.1 thorpej #if defined(__FreeBSD__) && (__FreeBSD_version > 300000)
35 1.1 thorpej #include <sys/linker.h>
36 1.1 thorpej #endif
37 1.1 thorpej
38 1.1 thorpej #include <net/if.h>
39 1.1 thorpej #include <netinet/in.h>
40 1.1 thorpej #include <arpa/inet.h>
41 1.1 thorpej #include <stdio.h>
42 1.1 thorpej #include <stdlib.h>
43 1.1 thorpej #include <unistd.h>
44 1.1 thorpej #include <stddef.h>
45 1.1 thorpej #include <string.h>
46 1.1 thorpej #include <ctype.h>
47 1.1 thorpej #include <errno.h>
48 1.1 thorpej #include <err.h>
49 1.1 thorpej #include <syslog.h>
50 1.1 thorpej
51 1.1 thorpej #include <altq/altq.h>
52 1.1 thorpej #include <altq/altq_red.h>
53 1.1 thorpej #include <altq/altq_rio.h>
54 1.1 thorpej #include <altq/altq_cdnr.h>
55 1.1 thorpej #include "altq_qop.h"
56 1.1 thorpej #include "qop_cdnr.h"
57 1.1 thorpej
58 1.1 thorpej #define ALTQ_DEVICE "/dev/altq/altq"
59 1.1 thorpej #define RED_DEVICE "/dev/altq/red"
60 1.1 thorpej #define RIO_DEVICE "/dev/altq/rio"
61 1.1 thorpej #define CDNR_DEVICE "/dev/altq/cdnr"
62 1.1 thorpej
63 1.1 thorpej #ifndef LIST_HEAD_INITIALIZER
64 1.1 thorpej #define LIST_HEAD_INITIALIZER(head) { NULL }
65 1.1 thorpej #endif
66 1.1 thorpej
67 1.1 thorpej /*
68 1.1 thorpej * token bucket regulator information
69 1.1 thorpej */
70 1.1 thorpej struct tbrinfo {
71 1.1 thorpej LIST_ENTRY(tbrinfo) link;
72 1.1 thorpej char ifname[IFNAMSIZ]; /* if name, e.g. "en0" */
73 1.1 thorpej struct tb_profile tb_prof, otb_prof;
74 1.1 thorpej int installed;
75 1.1 thorpej };
76 1.1 thorpej
77 1.1 thorpej /*
78 1.1 thorpej * Static globals
79 1.1 thorpej */
80 1.1 thorpej /* a list of configured interfaces */
81 1.1 thorpej LIST_HEAD(qop_iflist, ifinfo) qop_iflist = LIST_HEAD_INITIALIZER(&iflist);
82 1.1 thorpej /* a list of configured token bucket regulators */
83 1.1 thorpej LIST_HEAD(tbr_list, tbrinfo) tbr_list = LIST_HEAD_INITIALIZER(&tbr_list);
84 1.1 thorpej int Debug_mode = 0; /* nosched (dummy mode) */
85 1.1 thorpej
86 1.1 thorpej /*
87 1.1 thorpej * internal functions
88 1.1 thorpej */
89 1.1 thorpej static int get_ifmtu(const char *ifname);
90 1.1 thorpej static void tbr_install(const char *ifname);
91 1.1 thorpej static void tbr_deinstall(const char *ifname);
92 1.1 thorpej static int add_filter_rule(struct ifinfo *ifinfo, struct fltrinfo *fltrinfo,
93 1.1 thorpej struct fltrinfo **conflict);
94 1.1 thorpej static int remove_filter_rule(struct ifinfo *ifinfo,
95 1.1 thorpej struct fltrinfo *fltrinfo);
96 1.1 thorpej static int filt_check_relation(struct flow_filter *front,
97 1.1 thorpej struct flow_filter *back);
98 1.1 thorpej static int filt_disjoint(struct flow_filter *front, struct flow_filter *back);
99 1.1 thorpej static int filt_subset(struct flow_filter *front, struct flow_filter *back);
100 1.1 thorpej
101 1.1 thorpej /*
102 1.1 thorpej * QCMD (Queue Command) API
103 1.1 thorpej */
104 1.1 thorpej int
105 1.1 thorpej qcmd_init(void)
106 1.1 thorpej {
107 1.1 thorpej int error;
108 1.1 thorpej
109 1.1 thorpej /* read config file and execute commands */
110 1.1 thorpej error = qcmd_config();
111 1.1 thorpej
112 1.1 thorpej if (error == 0)
113 1.1 thorpej error = qcmd_enableall();
114 1.1 thorpej
115 1.1 thorpej if (error != 0)
116 1.1 thorpej LOG(LOG_ERR, errno, "%s: qcmd_init failed.\n",
117 1.1 thorpej qoperror(error));
118 1.1 thorpej return (error);
119 1.1 thorpej }
120 1.1 thorpej
121 1.1 thorpej int
122 1.1 thorpej qcmd_enable(const char *ifname)
123 1.1 thorpej {
124 1.1 thorpej struct ifinfo *ifinfo;
125 1.1 thorpej int error = 0;
126 1.1 thorpej
127 1.1 thorpej if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
128 1.1 thorpej error = QOPERR_BADIF;
129 1.1 thorpej
130 1.1 thorpej if (error == 0)
131 1.1 thorpej error = qop_enable(ifinfo);
132 1.1 thorpej
133 1.1 thorpej if (error == 0) {
134 1.1 thorpej LOG(LOG_INFO, 0, "%s enabled on interface %s (mtu:%d)\n",
135 1.1 thorpej ifinfo->qdisc->qname, ifname, ifinfo->ifmtu);
136 1.1 thorpej } else
137 1.1 thorpej LOG(LOG_ERR, errno, "%s: enable failed!\n", qoperror(error));
138 1.1 thorpej return (error);
139 1.1 thorpej }
140 1.1 thorpej
141 1.1 thorpej int
142 1.1 thorpej qcmd_disable(const char *ifname)
143 1.1 thorpej {
144 1.1 thorpej struct ifinfo *ifinfo;
145 1.1 thorpej int error = 0;
146 1.1 thorpej
147 1.1 thorpej if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
148 1.1 thorpej error = QOPERR_BADIF;
149 1.1 thorpej
150 1.1 thorpej if (error == 0)
151 1.1 thorpej error = qop_disable(ifinfo);
152 1.1 thorpej
153 1.1 thorpej if (error != 0)
154 1.1 thorpej LOG(LOG_ERR, errno, "%s: disable failed!\n", qoperror(error));
155 1.1 thorpej return (error);
156 1.1 thorpej }
157 1.1 thorpej
158 1.1 thorpej int
159 1.1 thorpej qcmd_enableall()
160 1.1 thorpej {
161 1.1 thorpej struct ifinfo *ifinfo;
162 1.1 thorpej int error;
163 1.1 thorpej
164 1.1 thorpej LIST_FOREACH(ifinfo, &qop_iflist, next) {
165 1.1 thorpej if ((error = qop_enable(ifinfo)) != 0)
166 1.1 thorpej return (error);
167 1.1 thorpej LOG(LOG_INFO, 0, "%s enabled on interface %s (mtu:%d)\n",
168 1.1 thorpej ifinfo->qdisc->qname, ifinfo->ifname, ifinfo->ifmtu);
169 1.1 thorpej }
170 1.1 thorpej return (0);
171 1.1 thorpej }
172 1.1 thorpej
173 1.1 thorpej int
174 1.1 thorpej qcmd_disableall()
175 1.1 thorpej {
176 1.1 thorpej struct ifinfo *ifinfo;
177 1.1 thorpej int err, error = 0;
178 1.1 thorpej
179 1.1 thorpej LIST_FOREACH(ifinfo, &qop_iflist, next)
180 1.1 thorpej if ((err = qop_disable(ifinfo)) != 0)
181 1.1 thorpej if (error == 0)
182 1.1 thorpej error = err;
183 1.1 thorpej return (error);
184 1.1 thorpej }
185 1.1 thorpej
186 1.1 thorpej int
187 1.1 thorpej qcmd_clear(const char *ifname)
188 1.1 thorpej {
189 1.1 thorpej struct ifinfo *ifinfo;
190 1.1 thorpej int error = 0;
191 1.1 thorpej
192 1.1 thorpej if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
193 1.1 thorpej error = QOPERR_BADIF;
194 1.1 thorpej
195 1.1 thorpej if (error == 0)
196 1.1 thorpej error = qop_clear(ifinfo);
197 1.1 thorpej if (error != 0)
198 1.1 thorpej LOG(LOG_ERR, errno, "%s: clear failed!\n", qoperror(error));
199 1.1 thorpej return (error);
200 1.1 thorpej }
201 1.1 thorpej
202 1.1 thorpej int
203 1.1 thorpej qcmd_destroyall(void)
204 1.1 thorpej {
205 1.1 thorpej while (!LIST_EMPTY(&qop_iflist))
206 1.1 thorpej (void)qop_delete_if(LIST_FIRST(&qop_iflist));
207 1.1 thorpej return (0);
208 1.1 thorpej }
209 1.1 thorpej
210 1.1 thorpej int
211 1.1 thorpej qcmd_restart(void)
212 1.1 thorpej {
213 1.1 thorpej qcmd_destroyall();
214 1.1 thorpej return qcmd_init();
215 1.1 thorpej }
216 1.1 thorpej
217 1.1 thorpej int
218 1.1 thorpej qcmd_delete_class(const char *ifname, const char *clname)
219 1.1 thorpej {
220 1.1 thorpej struct ifinfo *ifinfo;
221 1.1 thorpej struct classinfo *clinfo;
222 1.1 thorpej int error = 0;
223 1.1 thorpej
224 1.1 thorpej if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
225 1.1 thorpej error = QOPERR_BADIF;
226 1.1 thorpej
227 1.1 thorpej if (error == 0 &&
228 1.1 thorpej (clinfo = clname2clinfo(ifinfo, clname)) == NULL)
229 1.1 thorpej error = QOPERR_BADCLASS;
230 1.1 thorpej
231 1.1 thorpej if (error == 0)
232 1.1 thorpej error = qop_delete_class(clinfo);
233 1.1 thorpej if (error != 0)
234 1.1 thorpej LOG(LOG_ERR, errno, "%s: delete_class failed\n",
235 1.1 thorpej qoperror(error));
236 1.1 thorpej return (error);
237 1.1 thorpej }
238 1.1 thorpej
239 1.1 thorpej int
240 1.1 thorpej qcmd_add_filter(const char *ifname, const char *clname, const char *flname,
241 1.1 thorpej const struct flow_filter *fltr)
242 1.1 thorpej {
243 1.1 thorpej struct ifinfo *ifinfo;
244 1.1 thorpej struct classinfo *clinfo;
245 1.1 thorpej int error = 0;
246 1.1 thorpej
247 1.1 thorpej if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
248 1.1 thorpej error = QOPERR_BADIF;
249 1.1 thorpej
250 1.1 thorpej if (error == 0 &&
251 1.1 thorpej (clinfo = clname2clinfo(ifinfo, clname)) == NULL) {
252 1.1 thorpej /*
253 1.1 thorpej * there is no matching class.
254 1.1 thorpej * check if it is for a traffic conditioner
255 1.1 thorpej */
256 1.1 thorpej if ((ifinfo = input_ifname2ifinfo(ifname)) == NULL ||
257 1.1 thorpej (clinfo = clname2clinfo(ifinfo, clname)) == NULL)
258 1.1 thorpej error = QOPERR_BADCLASS;
259 1.1 thorpej }
260 1.1 thorpej
261 1.1 thorpej if (error == 0)
262 1.1 thorpej error = qop_add_filter(NULL, clinfo, flname, fltr, NULL);
263 1.1 thorpej
264 1.1 thorpej if (error != 0)
265 1.1 thorpej LOG(LOG_ERR, errno, "%s: add filter failed!\n",
266 1.1 thorpej qoperror(error));
267 1.1 thorpej else if (IsDebug(DEBUG_ALTQ)) {
268 1.1 thorpej LOG(LOG_DEBUG, 0, "%s: add a filter %s to class %s\n",
269 1.1 thorpej ifname, flname ? flname : "(null)",
270 1.1 thorpej clname ? clname : "(null)");
271 1.1 thorpej print_filter(fltr);
272 1.1 thorpej }
273 1.1 thorpej return (error);
274 1.1 thorpej }
275 1.1 thorpej
276 1.1 thorpej int
277 1.1 thorpej qcmd_delete_filter(const char *ifname, const char *clname, const char *flname)
278 1.1 thorpej {
279 1.1 thorpej struct ifinfo *ifinfo;
280 1.1 thorpej struct classinfo *clinfo;
281 1.1 thorpej struct fltrinfo *fltrinfo;
282 1.1 thorpej int error = 0;
283 1.1 thorpej
284 1.1 thorpej if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
285 1.1 thorpej error = QOPERR_BADIF;
286 1.1 thorpej
287 1.1 thorpej if (error == 0 &&
288 1.1 thorpej (clinfo = clname2clinfo(ifinfo, clname)) == NULL) {
289 1.1 thorpej /*
290 1.1 thorpej * there is no matching class.
291 1.1 thorpej * check if it is for a traffic conditioner
292 1.1 thorpej */
293 1.1 thorpej if ((ifinfo = input_ifname2ifinfo(ifname)) == NULL ||
294 1.1 thorpej (clinfo = clname2clinfo(ifinfo, clname)) == NULL)
295 1.1 thorpej error = QOPERR_BADCLASS;
296 1.1 thorpej }
297 1.1 thorpej
298 1.1 thorpej if (error == 0 &&
299 1.1 thorpej (fltrinfo = flname2flinfo(clinfo, flname)) == NULL)
300 1.1 thorpej error = QOPERR_BADFILTER;
301 1.1 thorpej
302 1.1 thorpej if (error == 0)
303 1.1 thorpej error = qop_delete_filter(fltrinfo);
304 1.1 thorpej if (error != 0)
305 1.1 thorpej LOG(LOG_ERR, errno, "%s: delete filter failed!\n",
306 1.1 thorpej qoperror(error));
307 1.1 thorpej return (error);
308 1.1 thorpej }
309 1.1 thorpej
310 1.1 thorpej int
311 1.1 thorpej qcmd_tbr_register(const char *ifname, u_int rate, u_int size)
312 1.1 thorpej {
313 1.1 thorpej struct tbrinfo *info;
314 1.1 thorpej
315 1.1 thorpej if ((info = calloc(1, sizeof(struct tbrinfo))) == NULL)
316 1.1 thorpej return (QOPERR_NOMEM);
317 1.1 thorpej
318 1.1 thorpej strcpy(info->ifname, ifname);
319 1.1 thorpej info->tb_prof.rate = rate;
320 1.1 thorpej info->tb_prof.depth = size;
321 1.1 thorpej info->installed = 0;
322 1.1 thorpej LIST_INSERT_HEAD(&tbr_list, info, link);
323 1.1 thorpej return (0);
324 1.1 thorpej }
325 1.1 thorpej
326 1.1 thorpej /*
327 1.1 thorpej * QOP (Queue Operation) API
328 1.1 thorpej */
329 1.1 thorpej
330 1.1 thorpej int
331 1.1 thorpej qop_add_if(struct ifinfo **rp, const char *ifname, u_int bandwidth,
332 1.1 thorpej struct qdisc_ops *qdisc_ops, void *if_private)
333 1.1 thorpej {
334 1.1 thorpej struct ifinfo *ifinfo;
335 1.1 thorpej int error;
336 1.1 thorpej
337 1.1 thorpej if (ifname2ifinfo(ifname) != NULL) {
338 1.1 thorpej LOG(LOG_ERR, 0, "qop_add_if: %s already exists!\n", ifname);
339 1.1 thorpej return (QOPERR_BADIF);
340 1.1 thorpej }
341 1.1 thorpej
342 1.1 thorpej if ((ifinfo = calloc(1, sizeof(struct ifinfo))) == NULL)
343 1.1 thorpej return (QOPERR_NOMEM);
344 1.1 thorpej ifinfo->ifname = strdup(ifname);
345 1.1 thorpej ifinfo->bandwidth = bandwidth;
346 1.1 thorpej ifinfo->enabled = 0;
347 1.1 thorpej if (ifname[0] == '_')
348 1.1 thorpej /* input interface */
349 1.1 thorpej ifname += 1;
350 1.1 thorpej ifinfo->ifindex = get_ifindex(ifname);
351 1.1 thorpej ifinfo->ifmtu = get_ifmtu(ifname);
352 1.1 thorpej if (qdisc_ops == NULL || Debug_mode)
353 1.1 thorpej ifinfo->qdisc = &nop_qdisc; /* replace syscalls by nops */
354 1.1 thorpej else
355 1.1 thorpej ifinfo->qdisc = qdisc_ops;
356 1.1 thorpej ifinfo->private = if_private;
357 1.1 thorpej LIST_INIT(&ifinfo->cllist);
358 1.1 thorpej LIST_INIT(&ifinfo->fltr_rules);
359 1.1 thorpej
360 1.1 thorpej /* Link the interface info structure */
361 1.1 thorpej LIST_INSERT_HEAD(&qop_iflist, ifinfo, next);
362 1.1 thorpej
363 1.1 thorpej /* install token bucket regulator, if necessary */
364 1.1 thorpej tbr_install(ifname);
365 1.1 thorpej
366 1.1 thorpej /* attach the discipline to the interface */
367 1.1 thorpej if ((error = (*ifinfo->qdisc->attach)(ifinfo)) != 0)
368 1.1 thorpej goto err_ret;
369 1.1 thorpej
370 1.1 thorpej /* disable and clear the interface */
371 1.1 thorpej if (ifinfo->qdisc->disable != NULL)
372 1.1 thorpej if ((error = (*ifinfo->qdisc->disable)(ifinfo)) != 0)
373 1.1 thorpej goto err_ret;
374 1.1 thorpej if (ifinfo->qdisc->clear != NULL)
375 1.1 thorpej if ((error = (*ifinfo->qdisc->clear)(ifinfo)) != 0)
376 1.1 thorpej goto err_ret;
377 1.1 thorpej
378 1.1 thorpej if (rp != NULL)
379 1.1 thorpej *rp = ifinfo;
380 1.1 thorpej return (0);
381 1.1 thorpej
382 1.1 thorpej err_ret:
383 1.1 thorpej if (ifinfo != NULL) {
384 1.1 thorpej LIST_REMOVE(ifinfo, next);
385 1.1 thorpej if (ifinfo->ifname != NULL)
386 1.1 thorpej free(ifinfo->ifname);
387 1.1 thorpej free(ifinfo);
388 1.1 thorpej }
389 1.1 thorpej return (error);
390 1.1 thorpej }
391 1.1 thorpej
392 1.1 thorpej int
393 1.1 thorpej qop_delete_if(struct ifinfo *ifinfo)
394 1.1 thorpej {
395 1.1 thorpej (void)qop_disable(ifinfo);
396 1.1 thorpej (void)qop_clear(ifinfo);
397 1.1 thorpej
398 1.1 thorpej if (ifinfo->delete_hook != NULL)
399 1.1 thorpej (*ifinfo->delete_hook)(ifinfo);
400 1.1 thorpej
401 1.1 thorpej /* remove this entry from qop_iflist */
402 1.1 thorpej LIST_REMOVE(ifinfo, next);
403 1.1 thorpej
404 1.1 thorpej (void)(*ifinfo->qdisc->detach)(ifinfo);
405 1.1 thorpej
406 1.1 thorpej /* deinstall token bucket regulator, if necessary */
407 1.1 thorpej tbr_deinstall(ifinfo->ifname);
408 1.1 thorpej
409 1.1 thorpej if (ifinfo->private != NULL)
410 1.1 thorpej free(ifinfo->private);
411 1.1 thorpej if (ifinfo->ifname != NULL)
412 1.1 thorpej free(ifinfo->ifname);
413 1.1 thorpej free(ifinfo);
414 1.1 thorpej return (0);
415 1.1 thorpej }
416 1.1 thorpej
417 1.1 thorpej int
418 1.1 thorpej qop_enable(struct ifinfo *ifinfo)
419 1.1 thorpej {
420 1.1 thorpej int error;
421 1.1 thorpej
422 1.1 thorpej if (ifinfo->enable_hook != NULL)
423 1.1 thorpej if ((error = (*ifinfo->enable_hook)(ifinfo)) != 0)
424 1.1 thorpej return (error);
425 1.1 thorpej
426 1.1 thorpej if (ifinfo->qdisc->enable != NULL)
427 1.1 thorpej if ((error = (*ifinfo->qdisc->enable)(ifinfo)) != 0)
428 1.1 thorpej return (error);
429 1.1 thorpej ifinfo->enabled = 1;
430 1.1 thorpej return (0);
431 1.1 thorpej }
432 1.1 thorpej
433 1.1 thorpej int
434 1.1 thorpej qop_disable(struct ifinfo *ifinfo)
435 1.1 thorpej {
436 1.1 thorpej int error;
437 1.1 thorpej
438 1.1 thorpej if (ifinfo->qdisc->disable != NULL)
439 1.1 thorpej if ((error = (*ifinfo->qdisc->disable)(ifinfo)) != 0)
440 1.1 thorpej return (error);
441 1.1 thorpej ifinfo->enabled = 0;
442 1.1 thorpej return (0);
443 1.1 thorpej }
444 1.1 thorpej
445 1.1 thorpej int
446 1.1 thorpej qop_clear(struct ifinfo *ifinfo)
447 1.1 thorpej {
448 1.1 thorpej struct classinfo *clinfo;
449 1.1 thorpej
450 1.1 thorpej /* free all classes and filters */
451 1.1 thorpej if (ifinfo->ifname[0] != '_') {
452 1.1 thorpej /* output interface. delete from leaf classes */
453 1.1 thorpej while (!LIST_EMPTY(&ifinfo->cllist)) {
454 1.1 thorpej LIST_FOREACH(clinfo, &ifinfo->cllist, next) {
455 1.1 thorpej if (clinfo->child != NULL)
456 1.1 thorpej continue;
457 1.1 thorpej qop_delete_class(clinfo);
458 1.1 thorpej /*
459 1.1 thorpej * the list has been changed,
460 1.1 thorpej * restart from the head
461 1.1 thorpej */
462 1.1 thorpej break;
463 1.1 thorpej }
464 1.1 thorpej }
465 1.1 thorpej } else {
466 1.1 thorpej /* input interface. delete from parents */
467 1.1 thorpej struct classinfo *root = get_rootclass(ifinfo);
468 1.1 thorpej
469 1.1 thorpej while (!LIST_EMPTY(&ifinfo->cllist)) {
470 1.1 thorpej LIST_FOREACH(clinfo, &ifinfo->cllist, next)
471 1.1 thorpej if (clinfo->parent == root) {
472 1.1 thorpej qop_delete_cdnr(clinfo);
473 1.1 thorpej break;
474 1.1 thorpej }
475 1.1 thorpej if (root->child == NULL)
476 1.1 thorpej qop_delete_class(root);
477 1.1 thorpej }
478 1.1 thorpej }
479 1.1 thorpej
480 1.1 thorpej /* clear the interface */
481 1.1 thorpej if (ifinfo->qdisc->clear != NULL)
482 1.1 thorpej return (*ifinfo->qdisc->clear)(ifinfo);
483 1.1 thorpej return (0);
484 1.1 thorpej }
485 1.1 thorpej
486 1.1 thorpej int
487 1.1 thorpej qop_add_class(struct classinfo **rp, const char *clname,
488 1.1 thorpej struct ifinfo *ifinfo, struct classinfo *parent,
489 1.1 thorpej void *class_private)
490 1.1 thorpej {
491 1.1 thorpej struct classinfo *clinfo;
492 1.1 thorpej int error;
493 1.1 thorpej
494 1.1 thorpej if ((clinfo = calloc(1, sizeof(*clinfo))) == NULL)
495 1.1 thorpej return (QOPERR_NOMEM);
496 1.1 thorpej
497 1.1 thorpej if (clname != NULL)
498 1.1 thorpej clinfo->clname = strdup(clname);
499 1.1 thorpej else
500 1.1 thorpej clinfo->clname = strdup("(null)"); /* dummy name */
501 1.1 thorpej clinfo->ifinfo = ifinfo;
502 1.1 thorpej clinfo->private = class_private;
503 1.1 thorpej clinfo->parent = parent;
504 1.1 thorpej clinfo->child = NULL;
505 1.1 thorpej LIST_INIT(&clinfo->fltrlist);
506 1.1 thorpej
507 1.1 thorpej if ((error = (*ifinfo->qdisc->add_class)(clinfo)) != 0)
508 1.1 thorpej goto err_ret;
509 1.1 thorpej
510 1.1 thorpej /* link classinfo in lists */
511 1.1 thorpej LIST_INSERT_HEAD(&ifinfo->cllist, clinfo, next);
512 1.1 thorpej
513 1.1 thorpej if (parent != NULL) {
514 1.1 thorpej clinfo->sibling = parent->child;
515 1.1 thorpej clinfo->parent->child = clinfo;
516 1.1 thorpej }
517 1.1 thorpej
518 1.1 thorpej if (rp != NULL)
519 1.1 thorpej *rp = clinfo;
520 1.1 thorpej return (0);
521 1.1 thorpej
522 1.1 thorpej err_ret:
523 1.1 thorpej if (clinfo != NULL) {
524 1.1 thorpej if (clinfo->clname != NULL)
525 1.1 thorpej free(clinfo->clname);
526 1.1 thorpej free(clinfo);
527 1.1 thorpej }
528 1.1 thorpej return (error);
529 1.1 thorpej }
530 1.1 thorpej
531 1.1 thorpej int
532 1.1 thorpej qop_modify_class(struct classinfo *clinfo, void *arg)
533 1.1 thorpej {
534 1.1 thorpej return (*clinfo->ifinfo->qdisc->modify_class)(clinfo, arg);
535 1.1 thorpej }
536 1.1 thorpej
537 1.1 thorpej int
538 1.1 thorpej qop_delete_class(struct classinfo *clinfo)
539 1.1 thorpej {
540 1.1 thorpej struct ifinfo *ifinfo = clinfo->ifinfo;
541 1.1 thorpej struct classinfo *prev;
542 1.1 thorpej int error;
543 1.1 thorpej
544 1.1 thorpej /* a class to be removed should not have a child */
545 1.1 thorpej if (clinfo->child != NULL)
546 1.1 thorpej return (QOPERR_CLASS_PERM);
547 1.1 thorpej
548 1.1 thorpej /* remove filters associated to this class */
549 1.1 thorpej while (!LIST_EMPTY(&clinfo->fltrlist))
550 1.1 thorpej (void)qop_delete_filter(LIST_FIRST(&clinfo->fltrlist));
551 1.1 thorpej
552 1.1 thorpej if (clinfo->delete_hook != NULL)
553 1.1 thorpej (*clinfo->delete_hook)(clinfo);
554 1.1 thorpej
555 1.1 thorpej /* remove class info from the interface */
556 1.1 thorpej LIST_REMOVE(clinfo, next);
557 1.1 thorpej
558 1.1 thorpej /* remove this class from the child list */
559 1.1 thorpej if (clinfo->parent != NULL) {
560 1.1 thorpej if (clinfo->parent->child == clinfo)
561 1.1 thorpej clinfo->parent->child = clinfo->sibling;
562 1.1 thorpej else for (prev = clinfo->parent->child; prev->sibling != NULL;
563 1.1 thorpej prev = prev->sibling)
564 1.1 thorpej if (prev->sibling == clinfo) {
565 1.1 thorpej prev->sibling = clinfo->sibling;
566 1.1 thorpej break;
567 1.1 thorpej }
568 1.1 thorpej }
569 1.1 thorpej
570 1.1 thorpej /* delete class from kernel */
571 1.1 thorpej if ((error = (*ifinfo->qdisc->delete_class)(clinfo)) != 0)
572 1.1 thorpej return (error);
573 1.1 thorpej
574 1.1 thorpej if (clinfo->private != NULL)
575 1.1 thorpej free(clinfo->private);
576 1.1 thorpej if (clinfo->clname != NULL)
577 1.1 thorpej free(clinfo->clname);
578 1.1 thorpej free(clinfo);
579 1.1 thorpej return (0);
580 1.1 thorpej }
581 1.1 thorpej
582 1.1 thorpej int
583 1.1 thorpej qop_add_filter(struct fltrinfo **rp, struct classinfo *clinfo,
584 1.1 thorpej const char *flname, const struct flow_filter *fltr,
585 1.1 thorpej struct fltrinfo **conflict)
586 1.1 thorpej {
587 1.1 thorpej struct ifinfo *ifinfo;
588 1.1 thorpej struct fltrinfo *fltrinfo;
589 1.1 thorpej int error;
590 1.1 thorpej
591 1.1 thorpej if ((fltrinfo = calloc(1, sizeof(*fltrinfo))) == NULL)
592 1.1 thorpej return (QOPERR_NOMEM);
593 1.1 thorpej
594 1.1 thorpej fltrinfo->clinfo = clinfo;
595 1.1 thorpej fltrinfo->fltr = *fltr;
596 1.1 thorpej #if 1
597 1.1 thorpej /* fix this */
598 1.1 thorpej fltrinfo->line_no = line_no; /* XXX */
599 1.1 thorpej fltrinfo->dontwarn = filter_dontwarn; /* XXX */
600 1.1 thorpej #endif
601 1.1 thorpej if (flname != NULL)
602 1.1 thorpej fltrinfo->flname = strdup(flname);
603 1.1 thorpej else
604 1.1 thorpej fltrinfo->flname = strdup("(null)"); /* dummy name */
605 1.1 thorpej
606 1.1 thorpej /* check and save the filter */
607 1.1 thorpej ifinfo = clinfo->ifinfo;
608 1.1 thorpej if ((error = add_filter_rule(ifinfo, fltrinfo, conflict)) != 0)
609 1.1 thorpej goto err_ret;
610 1.1 thorpej
611 1.1 thorpej /* install the filter to the kernel */
612 1.1 thorpej if ((error = (*ifinfo->qdisc->add_filter)(fltrinfo)) != 0) {
613 1.1 thorpej remove_filter_rule(ifinfo, fltrinfo);
614 1.1 thorpej goto err_ret;
615 1.1 thorpej }
616 1.1 thorpej
617 1.1 thorpej /* link fltrinfo onto fltrlist of the class */
618 1.1 thorpej LIST_INSERT_HEAD(&clinfo->fltrlist, fltrinfo, next);
619 1.1 thorpej
620 1.1 thorpej if (rp != NULL)
621 1.1 thorpej *rp = fltrinfo;
622 1.1 thorpej return (0);
623 1.1 thorpej
624 1.1 thorpej err_ret:
625 1.1 thorpej if (fltrinfo != NULL) {
626 1.1 thorpej if (fltrinfo->flname != NULL)
627 1.1 thorpej free(fltrinfo->flname);
628 1.1 thorpej free(fltrinfo);
629 1.1 thorpej }
630 1.1 thorpej return (error);
631 1.1 thorpej }
632 1.1 thorpej
633 1.1 thorpej int
634 1.1 thorpej qop_delete_filter(struct fltrinfo *fltrinfo)
635 1.1 thorpej {
636 1.1 thorpej struct ifinfo *ifinfo;
637 1.1 thorpej struct classinfo *clinfo;
638 1.1 thorpej int error;
639 1.1 thorpej
640 1.1 thorpej /* remove filter info from the class */
641 1.1 thorpej clinfo = fltrinfo->clinfo;
642 1.1 thorpej ifinfo = clinfo->ifinfo;
643 1.1 thorpej
644 1.1 thorpej
645 1.1 thorpej /* remove the entry from fltrlist of the class */
646 1.1 thorpej LIST_REMOVE(fltrinfo, next);
647 1.1 thorpej
648 1.1 thorpej remove_filter_rule(ifinfo, fltrinfo);
649 1.1 thorpej
650 1.1 thorpej /* delete filter from kernel */
651 1.1 thorpej if ((error = (*ifinfo->qdisc->delete_filter)(fltrinfo)) != 0)
652 1.1 thorpej return (error);
653 1.1 thorpej
654 1.1 thorpej if (fltrinfo->flname)
655 1.1 thorpej free(fltrinfo->flname);
656 1.1 thorpej free(fltrinfo);
657 1.1 thorpej return (0);
658 1.1 thorpej }
659 1.1 thorpej
660 1.1 thorpej const char *
661 1.1 thorpej qoperror(int qoperrno)
662 1.1 thorpej {
663 1.1 thorpej static char buf[64];
664 1.1 thorpej
665 1.1 thorpej if (qoperrno <= QOPERR_MAX)
666 1.1 thorpej return (qop_errlist[qoperrno]);
667 1.1 thorpej sprintf(buf, "unknown error %d", qoperrno);
668 1.1 thorpej return (buf);
669 1.1 thorpej }
670 1.1 thorpej
671 1.1 thorpej /*
672 1.1 thorpej * misc functions
673 1.1 thorpej */
674 1.1 thorpej struct ifinfo *
675 1.1 thorpej ifname2ifinfo(const char *ifname)
676 1.1 thorpej {
677 1.1 thorpej struct ifinfo *ifinfo;
678 1.1 thorpej
679 1.1 thorpej LIST_FOREACH(ifinfo, &qop_iflist, next)
680 1.1 thorpej if (ifinfo->ifname != NULL &&
681 1.1 thorpej strcmp(ifinfo->ifname, ifname) == 0)
682 1.1 thorpej return (ifinfo);
683 1.1 thorpej return (NULL);
684 1.1 thorpej }
685 1.1 thorpej
686 1.1 thorpej struct ifinfo *
687 1.1 thorpej input_ifname2ifinfo(const char *ifname)
688 1.1 thorpej {
689 1.1 thorpej struct ifinfo *ifinfo;
690 1.1 thorpej
691 1.1 thorpej LIST_FOREACH(ifinfo, &qop_iflist, next)
692 1.1 thorpej if (ifinfo->ifname[0] == '_' &&
693 1.1 thorpej strcmp(ifinfo->ifname+1, ifname) == 0)
694 1.1 thorpej return (ifinfo);
695 1.1 thorpej return (NULL);
696 1.1 thorpej }
697 1.1 thorpej
698 1.1 thorpej struct classinfo *
699 1.1 thorpej clname2clinfo(const struct ifinfo *ifinfo, const char *clname)
700 1.1 thorpej {
701 1.1 thorpej struct classinfo *clinfo;
702 1.1 thorpej
703 1.1 thorpej LIST_FOREACH(clinfo, &ifinfo->cllist, next)
704 1.1 thorpej if (clinfo->clname != NULL &&
705 1.1 thorpej strcmp(clinfo->clname, clname) == 0)
706 1.1 thorpej return (clinfo);
707 1.1 thorpej return (NULL);
708 1.1 thorpej }
709 1.1 thorpej
710 1.1 thorpej struct classinfo *
711 1.1 thorpej clhandle2clinfo(struct ifinfo *ifinfo, u_long handle)
712 1.1 thorpej {
713 1.1 thorpej struct classinfo *clinfo;
714 1.1 thorpej
715 1.1 thorpej LIST_FOREACH(clinfo, &ifinfo->cllist, next)
716 1.1 thorpej if (clinfo->handle == handle)
717 1.1 thorpej return (clinfo);
718 1.1 thorpej return (NULL);
719 1.1 thorpej }
720 1.1 thorpej
721 1.1 thorpej struct fltrinfo *
722 1.1 thorpej flname2flinfo(const struct classinfo *clinfo, const char *flname)
723 1.1 thorpej {
724 1.1 thorpej struct fltrinfo *fltrinfo;
725 1.1 thorpej
726 1.1 thorpej LIST_FOREACH(fltrinfo, &clinfo->fltrlist, next)
727 1.1 thorpej if (fltrinfo->flname != NULL &&
728 1.1 thorpej strcmp(fltrinfo->flname, flname) == 0)
729 1.1 thorpej return (fltrinfo);
730 1.1 thorpej return (NULL);
731 1.1 thorpej }
732 1.1 thorpej
733 1.1 thorpej struct fltrinfo *
734 1.1 thorpej flhandle2fltrinfo(struct ifinfo *ifinfo, u_long handle)
735 1.1 thorpej {
736 1.1 thorpej struct fltrinfo *fltrinfo;
737 1.1 thorpej
738 1.1 thorpej LIST_FOREACH(fltrinfo, &ifinfo->fltr_rules, nextrule)
739 1.1 thorpej if (fltrinfo->handle == handle)
740 1.1 thorpej return (fltrinfo);
741 1.1 thorpej return (NULL);
742 1.1 thorpej }
743 1.1 thorpej
744 1.1 thorpej int
745 1.1 thorpej is_q_enabled(const char *ifname)
746 1.1 thorpej {
747 1.1 thorpej struct ifinfo *ifinfo;
748 1.1 thorpej
749 1.1 thorpej if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
750 1.1 thorpej return (0);
751 1.1 thorpej return (ifinfo->enabled);
752 1.1 thorpej }
753 1.1 thorpej
754 1.1 thorpej /*
755 1.1 thorpej * functions to walk through a class tree:
756 1.1 thorpej *
757 1.1 thorpej * for (clinfo = get_rootclass(ifinfo);
758 1.1 thorpej * clinfo != NULL; clinfo = get_nextclass(clinfo)) {
759 1.1 thorpej * do_something;
760 1.1 thorpej * }
761 1.1 thorpej */
762 1.1 thorpej struct classinfo *get_rootclass(struct ifinfo *ifinfo)
763 1.1 thorpej {
764 1.1 thorpej struct classinfo *clinfo;
765 1.1 thorpej
766 1.1 thorpej /* find a class without parent */
767 1.1 thorpej LIST_FOREACH(clinfo, &ifinfo->cllist, next)
768 1.1 thorpej if (clinfo->parent == NULL)
769 1.1 thorpej return (clinfo);
770 1.1 thorpej return (NULL);
771 1.1 thorpej }
772 1.1 thorpej
773 1.1 thorpej /* return next class in the tree */
774 1.1 thorpej struct classinfo *get_nextclass(struct classinfo *clinfo)
775 1.1 thorpej {
776 1.1 thorpej struct classinfo *next;
777 1.1 thorpej
778 1.1 thorpej if (clinfo->child != NULL)
779 1.1 thorpej next = clinfo->child;
780 1.1 thorpej else if (clinfo->sibling != NULL)
781 1.1 thorpej next = clinfo->sibling;
782 1.1 thorpej else {
783 1.1 thorpej next = clinfo;
784 1.1 thorpej while ((next = next->parent) != NULL)
785 1.1 thorpej if (next->sibling) {
786 1.1 thorpej next = next->sibling;
787 1.1 thorpej break;
788 1.1 thorpej }
789 1.1 thorpej }
790 1.1 thorpej return (next);
791 1.1 thorpej }
792 1.1 thorpej
793 1.1 thorpej u_long
794 1.1 thorpej atobps(const char *s)
795 1.1 thorpej {
796 1.1 thorpej u_long bandwidth;
797 1.1 thorpej char *cp;
798 1.1 thorpej
799 1.1 thorpej bandwidth = strtoul(s, &cp, 0);
800 1.1 thorpej if (cp != NULL) {
801 1.1 thorpej if (*cp == 'K' || *cp == 'k')
802 1.1 thorpej bandwidth *= 1000;
803 1.1 thorpej else if (*cp == 'M' || *cp == 'm')
804 1.1 thorpej bandwidth *= 1000000;
805 1.1 thorpej else if (*cp == 'G' || *cp == 'g')
806 1.1 thorpej bandwidth *= 1000000000;
807 1.1 thorpej }
808 1.1 thorpej return (bandwidth);
809 1.1 thorpej }
810 1.1 thorpej
811 1.1 thorpej u_long
812 1.1 thorpej atobytes(const char *s)
813 1.1 thorpej {
814 1.1 thorpej u_long bytes;
815 1.1 thorpej char *cp;
816 1.1 thorpej
817 1.1 thorpej bytes = strtoul(s, &cp, 0);
818 1.1 thorpej if (cp != NULL) {
819 1.1 thorpej if (*cp == 'K' || *cp == 'k')
820 1.1 thorpej bytes *= 1024;
821 1.1 thorpej else if (*cp == 'M' || *cp == 'm')
822 1.1 thorpej bytes *= 1024 * 1024;
823 1.1 thorpej else if (*cp == 'G' || *cp == 'g')
824 1.1 thorpej bytes *= 1024 * 1024 * 1024;
825 1.1 thorpej }
826 1.1 thorpej return (bytes);
827 1.1 thorpej }
828 1.1 thorpej
829 1.1 thorpej static int
830 1.1 thorpej get_ifmtu(const char *ifname)
831 1.1 thorpej {
832 1.1 thorpej int s, mtu;
833 1.1 thorpej struct ifreq ifr;
834 1.1 thorpej #ifdef __OpenBSD__
835 1.1 thorpej struct if_data ifdata;
836 1.1 thorpej #endif
837 1.1 thorpej
838 1.1 thorpej mtu = 512; /* default MTU */
839 1.1 thorpej
840 1.1 thorpej if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
841 1.1 thorpej return (mtu);
842 1.1 thorpej strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name);
843 1.1 thorpej #ifdef __OpenBSD__
844 1.1 thorpej ifr.ifr_data = (caddr_t)&ifdata;
845 1.1 thorpej if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == 0)
846 1.1 thorpej mtu = ifdata.ifi_mtu;
847 1.1 thorpej #else
848 1.1 thorpej if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == 0)
849 1.1 thorpej mtu = ifr.ifr_mtu;
850 1.1 thorpej #endif
851 1.1 thorpej close(s);
852 1.1 thorpej return (mtu);
853 1.1 thorpej }
854 1.1 thorpej
855 1.1 thorpej static void
856 1.1 thorpej tbr_install(const char *ifname)
857 1.1 thorpej {
858 1.1 thorpej struct tbrinfo *info;
859 1.1 thorpej struct tbrreq req;
860 1.1 thorpej int fd;
861 1.1 thorpej
862 1.1 thorpej LIST_FOREACH(info, &tbr_list, link)
863 1.1 thorpej if (strcmp(info->ifname, ifname) == 0)
864 1.1 thorpej break;
865 1.1 thorpej if (info == NULL)
866 1.1 thorpej return;
867 1.1 thorpej if (info->tb_prof.rate == 0 || info->installed)
868 1.1 thorpej return;
869 1.1 thorpej
870 1.1 thorpej /* get the current token bucket regulator */
871 1.1 thorpej if ((fd = open(ALTQ_DEVICE, O_RDWR)) < 0)
872 1.1 thorpej err(1, "can't open altq device");
873 1.1 thorpej strncpy(req.ifname, ifname, IFNAMSIZ-1);
874 1.1 thorpej if (ioctl(fd, ALTQTBRGET, &req) < 0)
875 1.1 thorpej err(1, "ALTQTBRGET for interface %s", req.ifname);
876 1.1 thorpej
877 1.1 thorpej /* save the current values */
878 1.1 thorpej info->otb_prof.rate = req.tb_prof.rate;
879 1.1 thorpej info->otb_prof.depth = req.tb_prof.depth;
880 1.1 thorpej
881 1.1 thorpej /*
882 1.1 thorpej * if tbr is not specified in the config file and tbr is already
883 1.1 thorpej * configured, do not change.
884 1.1 thorpej */
885 1.1 thorpej if (req.tb_prof.rate != 0) {
886 1.1 thorpej LOG(LOG_INFO, 0,
887 1.1 thorpej "tbr is already installed on %s,\n"
888 1.1 thorpej " using the current setting (rate:%.2fM size:%.2fK).\n",
889 1.1 thorpej info->ifname,
890 1.1 thorpej (double)req.tb_prof.rate/1000000.0,
891 1.1 thorpej (double)req.tb_prof.depth/1024.0);
892 1.1 thorpej close (fd);
893 1.1 thorpej return;
894 1.1 thorpej }
895 1.1 thorpej
896 1.1 thorpej /* if the new size is not specified, use heuristics */
897 1.1 thorpej if (info->tb_prof.depth == 0) {
898 1.1 thorpej u_int rate, size;
899 1.1 thorpej
900 1.1 thorpej rate = info->tb_prof.rate;
901 1.1 thorpej if (rate <= 1*1000*1000)
902 1.1 thorpej size = 1;
903 1.1 thorpej else if (rate <= 10*1000*1000)
904 1.1 thorpej size = 4;
905 1.1 thorpej else if (rate <= 200*1000*1000)
906 1.1 thorpej size = 8;
907 1.1 thorpej else
908 1.1 thorpej size = 24;
909 1.1 thorpej size = size * 1500; /* assume the default mtu is 1500 */
910 1.1 thorpej info->tb_prof.depth = size;
911 1.1 thorpej }
912 1.1 thorpej
913 1.1 thorpej /* install the new tbr */
914 1.1 thorpej strncpy(req.ifname, ifname, IFNAMSIZ-1);
915 1.1 thorpej req.tb_prof.rate = info->tb_prof.rate;
916 1.1 thorpej req.tb_prof.depth = info->tb_prof.depth;
917 1.1 thorpej if (ioctl(fd, ALTQTBRSET, &req) < 0)
918 1.1 thorpej err(1, "ALTQTBRSET for interface %s", req.ifname);
919 1.1 thorpej LOG(LOG_INFO, 0,
920 1.1 thorpej "tbr installed on %s (rate:%.2fM size:%.2fK)\n",
921 1.1 thorpej info->ifname,
922 1.1 thorpej (double)info->tb_prof.rate/1000000.0,
923 1.1 thorpej (double)info->tb_prof.depth/1024.0);
924 1.1 thorpej close(fd);
925 1.1 thorpej info->installed = 1;
926 1.1 thorpej }
927 1.1 thorpej
928 1.1 thorpej static void
929 1.1 thorpej tbr_deinstall(const char *ifname)
930 1.1 thorpej {
931 1.1 thorpej struct tbrinfo *info;
932 1.1 thorpej struct tbrreq req;
933 1.1 thorpej int fd;
934 1.1 thorpej
935 1.1 thorpej LIST_FOREACH(info, &tbr_list, link)
936 1.1 thorpej if (strcmp(info->ifname, ifname) == 0)
937 1.1 thorpej break;
938 1.1 thorpej if (info == NULL)
939 1.1 thorpej return;
940 1.1 thorpej
941 1.1 thorpej /* if we installed tbr, restore the old values */
942 1.1 thorpej if (info->installed != 0) {
943 1.1 thorpej strncpy(req.ifname, ifname, IFNAMSIZ-1);
944 1.1 thorpej req.tb_prof.rate = info->otb_prof.rate;
945 1.1 thorpej req.tb_prof.depth = info->otb_prof.depth;
946 1.1 thorpej if ((fd = open(ALTQ_DEVICE, O_RDWR)) < 0)
947 1.1 thorpej err(1, "can't open altq device");
948 1.1 thorpej if (ioctl(fd, ALTQTBRSET, &req) < 0)
949 1.1 thorpej err(1, "ALTQTBRSET for interface %s", req.ifname);
950 1.1 thorpej close(fd);
951 1.1 thorpej }
952 1.1 thorpej LIST_REMOVE(info, link);
953 1.1 thorpej free(info);
954 1.1 thorpej }
955 1.1 thorpej
956 1.1 thorpej void
957 1.1 thorpej print_filter(const struct flow_filter *filt)
958 1.1 thorpej {
959 1.1 thorpej if (filt->ff_flow.fi_family == AF_INET) {
960 1.1 thorpej struct in_addr in_addr;
961 1.1 thorpej
962 1.1 thorpej in_addr.s_addr = filt->ff_flow.fi_dst.s_addr;
963 1.1 thorpej LOG(LOG_DEBUG, 0,
964 1.1 thorpej " Filter Dest Addr: %s (mask %#x) Port: %d\n",
965 1.1 thorpej inet_ntoa(in_addr), ntoh32(filt->ff_mask.mask_dst.s_addr),
966 1.1 thorpej ntoh16(filt->ff_flow.fi_dport));
967 1.1 thorpej in_addr.s_addr = filt->ff_flow.fi_src.s_addr;
968 1.1 thorpej LOG(LOG_DEBUG, 0,
969 1.1 thorpej " Src Addr: %s (mask %#x) Port: %d\n",
970 1.1 thorpej inet_ntoa(in_addr), ntoh32(filt->ff_mask.mask_src.s_addr),
971 1.1 thorpej ntoh16(filt->ff_flow.fi_sport));
972 1.1 thorpej LOG(LOG_DEBUG, 0, " Protocol: %d TOS %#x (mask %#x)\n",
973 1.1 thorpej filt->ff_flow.fi_proto, filt->ff_flow.fi_tos,
974 1.1 thorpej filt->ff_mask.mask_tos);
975 1.1 thorpej }
976 1.1 thorpej #ifdef INET6
977 1.1 thorpej else if (filt->ff_flow.fi_family == AF_INET6) {
978 1.1 thorpej char str1[INET6_ADDRSTRLEN], str2[INET6_ADDRSTRLEN];
979 1.1 thorpej const struct flow_filter6 *sfilt6;
980 1.1 thorpej
981 1.1 thorpej sfilt6 = (const struct flow_filter6 *)filt;
982 1.1 thorpej LOG(LOG_DEBUG, 0, "Filter6 Dest Addr: %s (mask %s) Port: %d",
983 1.1 thorpej inet_ntop(AF_INET6, &sfilt6->ff_flow6.fi6_dst,
984 1.1 thorpej str1, sizeof(str1)),
985 1.1 thorpej inet_ntop(AF_INET6, &sfilt6->ff_mask6.mask6_dst,
986 1.1 thorpej str2, sizeof(str2)),
987 1.1 thorpej ntoh16(sfilt6->ff_flow6.fi6_dport));
988 1.1 thorpej LOG(LOG_DEBUG, 0, " Src Addr: %s (mask %s) Port: %d",
989 1.1 thorpej inet_ntop(AF_INET6, &sfilt6->ff_flow6.fi6_src,
990 1.1 thorpej str1, sizeof(str1)),
991 1.1 thorpej inet_ntop(AF_INET6, &sfilt6->ff_mask6.mask6_src,
992 1.1 thorpej str2, sizeof(str2)),
993 1.1 thorpej ntoh16(sfilt6->ff_flow6.fi6_sport));
994 1.1 thorpej LOG(LOG_DEBUG, 0, " Protocol: %d TCLASS %#x (mask %#x)\n",
995 1.1 thorpej sfilt6->ff_flow6.fi6_proto, sfilt6->ff_flow6.fi6_tclass,
996 1.1 thorpej sfilt6->ff_mask6.mask6_tclass);
997 1.1 thorpej }
998 1.1 thorpej #endif /* INET6 */
999 1.1 thorpej }
1000 1.1 thorpej
1001 1.1 thorpej /*
1002 1.1 thorpej * functions to check the filter-rules.
1003 1.1 thorpej * when a new filter is added, we check the relation to the existing filters
1004 1.1 thorpej * and if some inconsistency is found, produce an error or a warning message.
1005 1.1 thorpej *
1006 1.1 thorpej * filter matching is performed from the head of the list.
1007 1.1 thorpej * let
1008 1.1 thorpej * S: a set of packets that filter s matches
1009 1.1 thorpej * T: a set of packets that filter t matches
1010 1.1 thorpej * filter relations are:
1011 1.1 thorpej * disjoint: S ^ T = empty
1012 1.1 thorpej * subset: S <= T
1013 1.1 thorpej * intersect: S ^ T = not empty
1014 1.1 thorpej *
1015 1.1 thorpej * a new filter is disjoint or subset of the existing filters --> ok
1016 1.1 thorpej * a new filter is superset of an existing filter --> order problem
1017 1.1 thorpej * a new filter intersect an existing filter --> warning
1018 1.1 thorpej *
1019 1.1 thorpej * port-intersect: a special case we don't make warning
1020 1.1 thorpej * - intersection is only port numbers
1021 1.1 thorpej * - one specifies src port and the other specifies dst port
1022 1.1 thorpej * there must be no packet with well-known port numbers in
1023 1.1 thorpej * both src and dst ports. so this is ok.
1024 1.1 thorpej */
1025 1.1 thorpej
1026 1.1 thorpej #define FILT_DISJOINT 1
1027 1.1 thorpej #define FILT_SUBSET 2
1028 1.1 thorpej #define FILT_SUPERSET 3
1029 1.1 thorpej #define FILT_INTERSECT 4
1030 1.1 thorpej #define FILT_PORTINTERSECT 5
1031 1.1 thorpej
1032 1.1 thorpej static int
1033 1.1 thorpej add_filter_rule(struct ifinfo *ifinfo, struct fltrinfo *fltrinfo,
1034 1.1 thorpej struct fltrinfo **conflict)
1035 1.1 thorpej {
1036 1.1 thorpej struct fltrinfo *fp, *front, *back, *prev = NULL;
1037 1.1 thorpej int relation;
1038 1.1 thorpej
1039 1.1 thorpej LIST_FOREACH(fp, &ifinfo->fltr_rules, nextrule) {
1040 1.1 thorpej if (fp->fltr.ff_ruleno > fltrinfo->fltr.ff_ruleno) {
1041 1.1 thorpej front = fp;
1042 1.1 thorpej back = fltrinfo;
1043 1.1 thorpej prev = fp;
1044 1.1 thorpej } else {
1045 1.1 thorpej front = fltrinfo;
1046 1.1 thorpej back = fp;
1047 1.1 thorpej }
1048 1.1 thorpej
1049 1.1 thorpej relation = filt_check_relation(&front->fltr, &back->fltr);
1050 1.1 thorpej
1051 1.1 thorpej switch (relation) {
1052 1.1 thorpej case FILT_SUBSET:
1053 1.1 thorpej case FILT_DISJOINT:
1054 1.1 thorpej /* OK */
1055 1.1 thorpej break;
1056 1.1 thorpej case FILT_SUPERSET:
1057 1.1 thorpej if (front->dontwarn == 0 && back->dontwarn == 0)
1058 1.1 thorpej LOG(LOG_ERR, 0,
1059 1.1 thorpej "filters for \"%s\" at line %d and for \"%s\" at line %d has an order problem!\n",
1060 1.1 thorpej front->clinfo->clname, front->line_no,
1061 1.1 thorpej back->clinfo->clname, back->line_no);
1062 1.1 thorpej
1063 1.1 thorpej if (conflict != NULL)
1064 1.1 thorpej *conflict = fp;
1065 1.1 thorpej return (QOPERR_FILTER_SHADOW);
1066 1.1 thorpej case FILT_PORTINTERSECT:
1067 1.1 thorpej break;
1068 1.1 thorpej case FILT_INTERSECT:
1069 1.1 thorpej /*
1070 1.1 thorpej * if the intersecting two filters beloging to the
1071 1.1 thorpej * same class, it's ok.
1072 1.1 thorpej */
1073 1.1 thorpej if (front->clinfo == back->clinfo)
1074 1.1 thorpej break;
1075 1.1 thorpej if (front->dontwarn == 0 && back->dontwarn == 0)
1076 1.1 thorpej LOG(LOG_WARNING, 0,
1077 1.1 thorpej "warning: filter for \"%s\" at line %d could override filter for \"%s\" at line %d\n",
1078 1.1 thorpej front->clinfo->clname, front->line_no,
1079 1.1 thorpej back->clinfo->clname, back->line_no);
1080 1.1 thorpej break;
1081 1.1 thorpej }
1082 1.1 thorpej }
1083 1.1 thorpej
1084 1.1 thorpej if (prev == NULL)
1085 1.1 thorpej LIST_INSERT_HEAD(&ifinfo->fltr_rules, fltrinfo, nextrule);
1086 1.1 thorpej else
1087 1.1 thorpej LIST_INSERT_AFTER(prev, fltrinfo, nextrule);
1088 1.1 thorpej return (0);
1089 1.1 thorpej }
1090 1.1 thorpej
1091 1.1 thorpej static int
1092 1.1 thorpej remove_filter_rule(struct ifinfo *ifinfo, struct fltrinfo *fltrinfo)
1093 1.1 thorpej {
1094 1.1 thorpej LIST_REMOVE(fltrinfo, nextrule);
1095 1.1 thorpej return (0);
1096 1.1 thorpej }
1097 1.1 thorpej
1098 1.1 thorpej static int
1099 1.1 thorpej filt_check_relation(struct flow_filter *front, struct flow_filter *back)
1100 1.1 thorpej {
1101 1.1 thorpej int rval;
1102 1.1 thorpej
1103 1.1 thorpej if (front->ff_flow.fi_family != back->ff_flow.fi_family)
1104 1.1 thorpej return (FILT_DISJOINT);
1105 1.1 thorpej
1106 1.1 thorpej if (filt_disjoint(front, back))
1107 1.1 thorpej return (FILT_DISJOINT);
1108 1.1 thorpej
1109 1.1 thorpej if ((rval = filt_subset(front, back)) == 1)
1110 1.1 thorpej return (FILT_SUBSET);
1111 1.1 thorpej
1112 1.1 thorpej if (filt_subset(back, front) == 1)
1113 1.1 thorpej return (FILT_SUPERSET);
1114 1.1 thorpej
1115 1.1 thorpej if (rval == 2)
1116 1.1 thorpej return (FILT_PORTINTERSECT);
1117 1.1 thorpej
1118 1.1 thorpej return (FILT_INTERSECT);
1119 1.1 thorpej }
1120 1.1 thorpej
1121 1.1 thorpej static int
1122 1.1 thorpej filt_disjoint(struct flow_filter *front, struct flow_filter *back)
1123 1.1 thorpej {
1124 1.1 thorpej u_int32_t mask;
1125 1.1 thorpej u_int8_t tosmask;
1126 1.1 thorpej
1127 1.1 thorpej if (front->ff_flow.fi_family == AF_INET) {
1128 1.1 thorpej if (front->ff_flow.fi_proto != 0 && back->ff_flow.fi_proto != 0
1129 1.1 thorpej && front->ff_flow.fi_proto != back->ff_flow.fi_proto)
1130 1.1 thorpej return (1);
1131 1.1 thorpej if (front->ff_flow.fi_sport != 0 && back->ff_flow.fi_sport != 0
1132 1.1 thorpej && front->ff_flow.fi_sport != back->ff_flow.fi_sport)
1133 1.1 thorpej return (1);
1134 1.1 thorpej if (front->ff_flow.fi_dport != 0 && back->ff_flow.fi_dport != 0
1135 1.1 thorpej && front->ff_flow.fi_dport != back->ff_flow.fi_dport)
1136 1.1 thorpej return (1);
1137 1.1 thorpej if (front->ff_flow.fi_gpi != 0 && back->ff_flow.fi_gpi != 0
1138 1.1 thorpej && front->ff_flow.fi_gpi != back->ff_flow.fi_gpi)
1139 1.1 thorpej return (1);
1140 1.1 thorpej if (front->ff_flow.fi_src.s_addr != 0 &&
1141 1.1 thorpej back->ff_flow.fi_src.s_addr != 0) {
1142 1.1 thorpej mask = front->ff_mask.mask_src.s_addr &
1143 1.1 thorpej back->ff_mask.mask_src.s_addr;
1144 1.1 thorpej if ((front->ff_flow.fi_src.s_addr & mask) !=
1145 1.1 thorpej (back->ff_flow.fi_src.s_addr & mask))
1146 1.1 thorpej return (1);
1147 1.1 thorpej }
1148 1.1 thorpej if (front->ff_flow.fi_dst.s_addr != 0 &&
1149 1.1 thorpej back->ff_flow.fi_dst.s_addr != 0) {
1150 1.1 thorpej mask = front->ff_mask.mask_dst.s_addr &
1151 1.1 thorpej back->ff_mask.mask_dst.s_addr;
1152 1.1 thorpej if ((front->ff_flow.fi_dst.s_addr & mask) !=
1153 1.1 thorpej (back->ff_flow.fi_dst.s_addr & mask))
1154 1.1 thorpej return (1);
1155 1.1 thorpej }
1156 1.1 thorpej if (front->ff_flow.fi_tos != 0 && back->ff_flow.fi_tos != 0) {
1157 1.1 thorpej tosmask = front->ff_mask.mask_tos &
1158 1.1 thorpej back->ff_mask.mask_tos;
1159 1.1 thorpej if ((front->ff_flow.fi_tos & tosmask) !=
1160 1.1 thorpej (back->ff_flow.fi_tos & tosmask))
1161 1.1 thorpej return (1);
1162 1.1 thorpej }
1163 1.1 thorpej return (0);
1164 1.1 thorpej }
1165 1.1 thorpej #ifdef INET6
1166 1.1 thorpej else if (front->ff_flow.fi_family == AF_INET6) {
1167 1.1 thorpej struct flow_filter6 *front6, *back6;
1168 1.1 thorpej int i;
1169 1.1 thorpej
1170 1.1 thorpej front6 = (struct flow_filter6 *)front;
1171 1.1 thorpej back6 = (struct flow_filter6 *)back;
1172 1.1 thorpej
1173 1.1 thorpej if (front6->ff_flow6.fi6_proto != 0 &&
1174 1.1 thorpej back6->ff_flow6.fi6_proto != 0 &&
1175 1.1 thorpej front6->ff_flow6.fi6_proto != back6->ff_flow6.fi6_proto)
1176 1.1 thorpej return (1);
1177 1.1 thorpej if (front6->ff_flow6.fi6_flowlabel != 0 &&
1178 1.1 thorpej back6->ff_flow6.fi6_flowlabel != 0 &&
1179 1.1 thorpej front6->ff_flow6.fi6_flowlabel !=
1180 1.1 thorpej back6->ff_flow6.fi6_flowlabel)
1181 1.1 thorpej return (1);
1182 1.1 thorpej if (front6->ff_flow6.fi6_sport != 0 &&
1183 1.1 thorpej back6->ff_flow6.fi6_sport != 0 &&
1184 1.1 thorpej front6->ff_flow6.fi6_sport != back6->ff_flow6.fi6_sport)
1185 1.1 thorpej return (1);
1186 1.1 thorpej if (front6->ff_flow6.fi6_dport != 0 &&
1187 1.1 thorpej back6->ff_flow6.fi6_dport != 0 &&
1188 1.1 thorpej front6->ff_flow6.fi6_dport != back6->ff_flow6.fi6_dport)
1189 1.1 thorpej return (1);
1190 1.1 thorpej if (front6->ff_flow6.fi6_gpi != 0 &&
1191 1.1 thorpej back6->ff_flow6.fi6_gpi != 0 &&
1192 1.1 thorpej front6->ff_flow6.fi6_gpi != back6->ff_flow6.fi6_gpi)
1193 1.1 thorpej return (1);
1194 1.1 thorpej if (!IN6_IS_ADDR_UNSPECIFIED(&front6->ff_flow6.fi6_src) &&
1195 1.1 thorpej !IN6_IS_ADDR_UNSPECIFIED(&back6->ff_flow6.fi6_src)) {
1196 1.1 thorpej for (i=0; i<4; i++) {
1197 1.1 thorpej mask = IN6ADDR32(&front6->ff_mask6.mask6_src, i)
1198 1.1 thorpej & IN6ADDR32(&back6->ff_mask6.mask6_src, i);
1199 1.1 thorpej if ((IN6ADDR32(&front6->ff_flow6.fi6_src, i) & mask) !=
1200 1.1 thorpej (IN6ADDR32(&back6->ff_flow6.fi6_src, i) & mask))
1201 1.1 thorpej return (1);
1202 1.1 thorpej }
1203 1.1 thorpej }
1204 1.1 thorpej if (!IN6_IS_ADDR_UNSPECIFIED(&front6->ff_flow6.fi6_dst) &&
1205 1.1 thorpej !IN6_IS_ADDR_UNSPECIFIED(&back6->ff_flow6.fi6_dst)) {
1206 1.1 thorpej for (i=0; i<4; i++) {
1207 1.1 thorpej mask = IN6ADDR32(&front6->ff_mask6.mask6_dst, i)
1208 1.1 thorpej & IN6ADDR32(&back6->ff_mask6.mask6_dst, i);
1209 1.1 thorpej if ((IN6ADDR32(&front6->ff_flow6.fi6_dst, i) & mask) !=
1210 1.1 thorpej (IN6ADDR32(&back6->ff_flow6.fi6_dst, i) & mask))
1211 1.1 thorpej return (1);
1212 1.1 thorpej }
1213 1.1 thorpej }
1214 1.1 thorpej if (front6->ff_flow6.fi6_tclass != 0 &&
1215 1.1 thorpej back6->ff_flow6.fi6_tclass != 0) {
1216 1.1 thorpej tosmask = front6->ff_mask6.mask6_tclass &
1217 1.1 thorpej back6->ff_mask6.mask6_tclass;
1218 1.1 thorpej if ((front6->ff_flow6.fi6_tclass & tosmask) !=
1219 1.1 thorpej (back6->ff_flow6.fi6_tclass & tosmask))
1220 1.1 thorpej return (1);
1221 1.1 thorpej }
1222 1.1 thorpej return (0);
1223 1.1 thorpej }
1224 1.1 thorpej #endif /* INET6 */
1225 1.1 thorpej return (0);
1226 1.1 thorpej }
1227 1.1 thorpej
1228 1.1 thorpej /*
1229 1.1 thorpej * check if "front" is a subset of "back". assumes they are not disjoint
1230 1.1 thorpej * return value 0: not a subset
1231 1.1 thorpej * 1: subset
1232 1.1 thorpej * 2: subset except src & dst ports
1233 1.1 thorpej * (possible port-intersect)
1234 1.1 thorpej */
1235 1.1 thorpej static int
1236 1.1 thorpej filt_subset(struct flow_filter *front, struct flow_filter *back)
1237 1.1 thorpej {
1238 1.1 thorpej u_int16_t srcport, dstport;
1239 1.1 thorpej
1240 1.1 thorpej if (front->ff_flow.fi_family == AF_INET) {
1241 1.1 thorpej if (front->ff_flow.fi_proto == 0 &&
1242 1.1 thorpej back->ff_flow.fi_proto != 0)
1243 1.1 thorpej return (0);
1244 1.1 thorpej if (front->ff_flow.fi_gpi == 0 && back->ff_flow.fi_gpi != 0)
1245 1.1 thorpej return (0);
1246 1.1 thorpej if (front->ff_flow.fi_src.s_addr == 0) {
1247 1.1 thorpej if (back->ff_flow.fi_src.s_addr != 0)
1248 1.1 thorpej return (0);
1249 1.1 thorpej } else if (back->ff_flow.fi_src.s_addr != 0 &&
1250 1.1 thorpej (~front->ff_mask.mask_src.s_addr &
1251 1.1 thorpej back->ff_mask.mask_src.s_addr))
1252 1.1 thorpej return (0);
1253 1.1 thorpej if (front->ff_flow.fi_dst.s_addr == 0) {
1254 1.1 thorpej if (back->ff_flow.fi_dst.s_addr != 0)
1255 1.1 thorpej return (0);
1256 1.1 thorpej } else if (back->ff_flow.fi_dst.s_addr != 0 &&
1257 1.1 thorpej (~front->ff_mask.mask_dst.s_addr &
1258 1.1 thorpej back->ff_mask.mask_dst.s_addr))
1259 1.1 thorpej return (0);
1260 1.1 thorpej if (~front->ff_mask.mask_tos & back->ff_mask.mask_tos)
1261 1.1 thorpej return (0);
1262 1.1 thorpej
1263 1.1 thorpej if (front->ff_flow.fi_sport == 0 &&
1264 1.1 thorpej back->ff_flow.fi_sport != 0) {
1265 1.1 thorpej srcport = ntohs(back->ff_flow.fi_sport);
1266 1.1 thorpej dstport = ntohs(front->ff_flow.fi_dport);
1267 1.1 thorpej if (dstport > 0 /* && dstport < 1024 */ &&
1268 1.1 thorpej srcport > 0 /* && srcport < 1024 */)
1269 1.1 thorpej return (2);
1270 1.1 thorpej return (0);
1271 1.1 thorpej }
1272 1.1 thorpej if (front->ff_flow.fi_dport == 0 &&
1273 1.1 thorpej back->ff_flow.fi_dport != 0) {
1274 1.1 thorpej dstport = ntohs(back->ff_flow.fi_dport);
1275 1.1 thorpej srcport = ntohs(front->ff_flow.fi_sport);
1276 1.1 thorpej if (srcport > 0 /* && srcport < 1024 */ &&
1277 1.1 thorpej dstport > 0 /* && dstport < 1024 */)
1278 1.1 thorpej return (2);
1279 1.1 thorpej return (0);
1280 1.1 thorpej }
1281 1.1 thorpej
1282 1.1 thorpej return (1);
1283 1.1 thorpej }
1284 1.1 thorpej #ifdef INET6
1285 1.1 thorpej else if (front->ff_flow.fi_family == AF_INET6) {
1286 1.1 thorpej struct flow_filter6 *front6, *back6;
1287 1.1 thorpej int i;
1288 1.1 thorpej
1289 1.1 thorpej front6 = (struct flow_filter6 *)front;
1290 1.1 thorpej back6 = (struct flow_filter6 *)back;
1291 1.1 thorpej
1292 1.1 thorpej if (front6->ff_flow6.fi6_proto == 0 &&
1293 1.1 thorpej back6->ff_flow6.fi6_proto != 0)
1294 1.1 thorpej return (0);
1295 1.1 thorpej if (front6->ff_flow6.fi6_flowlabel == 0 &&
1296 1.1 thorpej back6->ff_flow6.fi6_flowlabel != 0)
1297 1.1 thorpej return (0);
1298 1.1 thorpej if (front6->ff_flow6.fi6_gpi == 0 &&
1299 1.1 thorpej back6->ff_flow6.fi6_gpi != 0)
1300 1.1 thorpej return (0);
1301 1.1 thorpej
1302 1.1 thorpej if (IN6_IS_ADDR_UNSPECIFIED(&front6->ff_flow6.fi6_src)) {
1303 1.1 thorpej if (!IN6_IS_ADDR_UNSPECIFIED(&back6->ff_flow6.fi6_src))
1304 1.1 thorpej return (0);
1305 1.1 thorpej } else if (!IN6_IS_ADDR_UNSPECIFIED(&back6->ff_flow6.fi6_src))
1306 1.1 thorpej for (i=0; i<4; i++)
1307 1.1 thorpej if (~IN6ADDR32(&front6->ff_mask6.mask6_src, i) &
1308 1.1 thorpej IN6ADDR32(&back6->ff_mask6.mask6_src, i))
1309 1.1 thorpej return (0);
1310 1.1 thorpej if (IN6_IS_ADDR_UNSPECIFIED(&front6->ff_flow6.fi6_dst)) {
1311 1.1 thorpej if (!IN6_IS_ADDR_UNSPECIFIED(&back6->ff_flow6.fi6_dst))
1312 1.1 thorpej return (0);
1313 1.1 thorpej } else if (!IN6_IS_ADDR_UNSPECIFIED(&back6->ff_flow6.fi6_dst))
1314 1.1 thorpej for (i=0; i<4; i++)
1315 1.1 thorpej if (~IN6ADDR32(&front6->ff_mask6.mask6_dst, i) &
1316 1.1 thorpej IN6ADDR32(&back6->ff_mask6.mask6_dst, i))
1317 1.1 thorpej return (0);
1318 1.1 thorpej
1319 1.1 thorpej if (~front6->ff_mask6.mask6_tclass &
1320 1.1 thorpej back6->ff_mask6.mask6_tclass)
1321 1.1 thorpej return (0);
1322 1.1 thorpej
1323 1.1 thorpej if (front6->ff_flow6.fi6_sport == 0 &&
1324 1.1 thorpej back6->ff_flow6.fi6_sport != 0) {
1325 1.1 thorpej srcport = ntohs(back6->ff_flow6.fi6_sport);
1326 1.1 thorpej dstport = ntohs(front6->ff_flow6.fi6_dport);
1327 1.1 thorpej if (dstport > 0 /* && dstport < 1024 */ &&
1328 1.1 thorpej srcport > 0 /* && srcport < 1024 */)
1329 1.1 thorpej return (2);
1330 1.1 thorpej return (0);
1331 1.1 thorpej }
1332 1.1 thorpej if (front6->ff_flow6.fi6_dport == 0 &&
1333 1.1 thorpej back6->ff_flow6.fi6_dport != 0) {
1334 1.1 thorpej dstport = ntohs(back6->ff_flow6.fi6_dport);
1335 1.1 thorpej srcport = ntohs(front6->ff_flow6.fi6_sport);
1336 1.1 thorpej if (srcport > 0 /* && srcport < 1024 */ &&
1337 1.1 thorpej dstport > 0 /* && dstport < 1024 */)
1338 1.1 thorpej return (2);
1339 1.1 thorpej return (0);
1340 1.1 thorpej }
1341 1.1 thorpej }
1342 1.1 thorpej #endif /* INET6 */
1343 1.1 thorpej return (1);
1344 1.1 thorpej }
1345 1.1 thorpej
1346 1.1 thorpej
1347 1.1 thorpej /*
1348 1.1 thorpej * setting RED or RIO default parameters
1349 1.1 thorpej */
1350 1.1 thorpej int
1351 1.1 thorpej qop_red_set_defaults(int th_min, int th_max, int inv_pmax)
1352 1.1 thorpej {
1353 1.1 thorpej struct redparams params;
1354 1.1 thorpej int fd;
1355 1.1 thorpej
1356 1.1 thorpej if ((fd = open(RED_DEVICE, O_RDWR)) < 0) {
1357 1.1 thorpej LOG(LOG_ERR, errno, "RED open\n");
1358 1.1 thorpej return (QOPERR_SYSCALL);
1359 1.1 thorpej }
1360 1.1 thorpej
1361 1.1 thorpej params.th_min = th_min;
1362 1.1 thorpej params.th_max = th_max;
1363 1.1 thorpej params.inv_pmax = inv_pmax;
1364 1.1 thorpej
1365 1.1 thorpej if (ioctl(fd, RED_SETDEFAULTS, ¶ms) < 0) {
1366 1.1 thorpej LOG(LOG_ERR, errno, "RED_SETDEFAULTS\n");
1367 1.1 thorpej return (QOPERR_SYSCALL);
1368 1.1 thorpej }
1369 1.1 thorpej
1370 1.1 thorpej (void)close(fd);
1371 1.1 thorpej return (0);
1372 1.1 thorpej }
1373 1.1 thorpej
1374 1.1 thorpej int
1375 1.1 thorpej qop_rio_set_defaults(struct redparams *params)
1376 1.1 thorpej {
1377 1.1 thorpej int i, fd;
1378 1.1 thorpej
1379 1.1 thorpej /* sanity check */
1380 1.1 thorpej for (i = 1; i < RIO_NDROPPREC; i++) {
1381 1.1 thorpej if (params[i].th_max > params[i-1].th_min)
1382 1.1 thorpej LOG(LOG_WARNING, 0,
1383 1.1 thorpej "warning: overlap found in RIO thresholds\n");
1384 1.1 thorpej }
1385 1.1 thorpej
1386 1.1 thorpej if ((fd = open(RIO_DEVICE, O_RDWR)) < 0) {
1387 1.1 thorpej LOG(LOG_ERR, errno, "RIO open\n");
1388 1.1 thorpej return (QOPERR_SYSCALL);
1389 1.1 thorpej }
1390 1.1 thorpej
1391 1.1 thorpej if (ioctl(fd, RIO_SETDEFAULTS, params) < 0) {
1392 1.1 thorpej LOG(LOG_ERR, errno, "RIO_SETDEFAULTS\n");
1393 1.1 thorpej return (QOPERR_SYSCALL);
1394 1.1 thorpej }
1395 1.1 thorpej
1396 1.1 thorpej (void)close(fd);
1397 1.1 thorpej return (0);
1398 1.1 thorpej }
1399 1.1 thorpej
1400 1.1 thorpej /*
1401 1.1 thorpej * try to load and open KLD module
1402 1.1 thorpej */
1403 1.1 thorpej int
1404 1.1 thorpej open_module(const char *devname, int flags)
1405 1.1 thorpej {
1406 1.1 thorpej #if defined(__FreeBSD__) && (__FreeBSD_version > 300000)
1407 1.1 thorpej char modname[64], filename[256], *cp;
1408 1.1 thorpej int fd;
1409 1.1 thorpej struct stat sbuf;
1410 1.1 thorpej
1411 1.1 thorpej /* turn discipline name into module name */
1412 1.1 thorpej strcpy(modname, "altq_");
1413 1.1 thorpej if ((cp = strrchr(devname, '/')) == NULL)
1414 1.1 thorpej return (-1);
1415 1.1 thorpej strcat(modname, cp+1);
1416 1.1 thorpej
1417 1.1 thorpej /* check if the kld module exists */
1418 1.1 thorpej sprintf(filename, "/modules/%s.ko", modname);
1419 1.1 thorpej if (stat(filename, &sbuf) < 0) {
1420 1.1 thorpej /* module file doesn't exist */
1421 1.1 thorpej return (-1);
1422 1.1 thorpej }
1423 1.1 thorpej
1424 1.1 thorpej if (kldload(modname) < 0) {
1425 1.1 thorpej LOG(LOG_ERR, errno, "kldload %s failed!\n", modname);
1426 1.1 thorpej return (-1);
1427 1.1 thorpej }
1428 1.1 thorpej
1429 1.1 thorpej /* successfully loaded, open the device */
1430 1.1 thorpej LOG(LOG_INFO, 0, "kld module %s loaded\n", modname);
1431 1.1 thorpej fd = open(devname, flags);
1432 1.1 thorpej return (fd);
1433 1.1 thorpej #else
1434 1.1 thorpej return (-1);
1435 1.1 thorpej #endif
1436 1.1 thorpej }
1437 1.1 thorpej
1438