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