/* $NetBSD: qop.c,v 1.13 2024/12/24 08:35:28 ozaki-r Exp $ */
/* $KAME: qop.c,v 1.11 2001/10/26 04:57:59 kjc Exp $ */
/*
* Copyright (C) 1999-2000
* Sony Computer Science Laboratories, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
error = QOPERR_BADIF;
if (error == 0 &&
(clinfo = clname2clinfo(ifinfo, clname)) == NULL) {
/*
* there is no matching class.
* check if it is for a traffic conditioner
*/
if ((ifinfo = input_ifname2ifinfo(ifname)) == NULL ||
(clinfo = clname2clinfo(ifinfo, clname)) == NULL)
error = QOPERR_BADCLASS;
}
if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
error = QOPERR_BADIF;
if (error == 0 &&
(clinfo = clname2clinfo(ifinfo, clname)) == NULL) {
/*
* there is no matching class.
* check if it is for a traffic conditioner
*/
if ((ifinfo = input_ifname2ifinfo(ifname)) == NULL ||
(clinfo = clname2clinfo(ifinfo, clname)) == NULL)
error = QOPERR_BADCLASS;
}
/* Link the interface info structure */
LIST_INSERT_HEAD(&qop_iflist, ifinfo, next);
/* install token bucket regulator, if necessary */
tbr_install(ifname);
/* attach the discipline to the interface */
if ((error = (*ifinfo->qdisc->attach)(ifinfo)) != 0)
goto err_ret;
/* disable and clear the interface */
if (ifinfo->qdisc->disable != NULL)
if ((error = (*ifinfo->qdisc->disable)(ifinfo)) != 0)
goto err_ret;
if (ifinfo->qdisc->clear != NULL)
if ((error = (*ifinfo->qdisc->clear)(ifinfo)) != 0)
goto err_ret;
if (rp != NULL)
*rp = ifinfo;
return (0);
err_ret:
if (ifinfo != NULL) {
LIST_REMOVE(ifinfo, next);
if (ifinfo->ifname != NULL)
free(ifinfo->ifname);
free(ifinfo);
}
return (error);
}
int
qop_delete_if(struct ifinfo *ifinfo)
{
(void)qop_disable(ifinfo);
(void)qop_clear(ifinfo);
if (ifinfo->delete_hook != NULL)
(*ifinfo->delete_hook)(ifinfo);
/* remove this entry from qop_iflist */
LIST_REMOVE(ifinfo, next);
(void)(*ifinfo->qdisc->detach)(ifinfo);
/* deinstall token bucket regulator, if necessary */
tbr_deinstall(ifinfo->ifname);
if (ifinfo->private != NULL)
free(ifinfo->private);
if (ifinfo->ifname != NULL)
free(ifinfo->ifname);
free(ifinfo);
return (0);
}
int
qop_enable(struct ifinfo *ifinfo)
{
int error;
if (ifinfo->enable_hook != NULL)
if ((error = (*ifinfo->enable_hook)(ifinfo)) != 0)
return (error);
if (ifinfo->qdisc->enable != NULL)
if ((error = (*ifinfo->qdisc->enable)(ifinfo)) != 0)
return (error);
ifinfo->enabled = 1;
return (0);
}
int
qop_disable(struct ifinfo *ifinfo)
{
int error;
if (ifinfo->qdisc->disable != NULL)
if ((error = (*ifinfo->qdisc->disable)(ifinfo)) != 0)
return (error);
ifinfo->enabled = 0;
return (0);
}
int
qop_clear(struct ifinfo *ifinfo)
{
struct classinfo *clinfo;
/* free all classes and filters */
if (ifinfo->ifname[0] != '_') {
/* output interface. delete from leaf classes */
while (!LIST_EMPTY(&ifinfo->cllist)) {
LIST_FOREACH(clinfo, &ifinfo->cllist, next) {
if (clinfo->child != NULL)
continue;
qop_delete_class(clinfo);
/*
* the list has been changed,
* restart from the head
*/
break;
}
}
} else {
/* input interface. delete from parents */
struct classinfo *root = get_rootclass(ifinfo);
while (!LIST_EMPTY(&ifinfo->cllist)) {
LIST_FOREACH(clinfo, &ifinfo->cllist, next)
if (clinfo->parent == root) {
qop_delete_cdnr(clinfo);
break;
}
if (root->child != NULL)
qop_delete_class(root);
}
}
/* clear the interface */
if (ifinfo->qdisc->clear != NULL)
return (*ifinfo->qdisc->clear)(ifinfo);
return (0);
}
int
qop_delete_class(struct classinfo *clinfo)
{
struct ifinfo *ifinfo = clinfo->ifinfo;
struct classinfo *prev;
int error;
/* a class to be removed should not have a child */
if (clinfo->child != NULL)
return (QOPERR_CLASS_PERM);
/* remove filters associated to this class */
while (!LIST_EMPTY(&clinfo->fltrlist))
(void)qop_delete_filter(LIST_FIRST(&clinfo->fltrlist));
if (clinfo->delete_hook != NULL)
(*clinfo->delete_hook)(clinfo);
/* remove class info from the interface */
LIST_REMOVE(clinfo, next);
/* remove this class from the child list */
if (clinfo->parent != NULL) {
if (clinfo->parent->child == clinfo)
clinfo->parent->child = clinfo->sibling;
else for (prev = clinfo->parent->child; prev->sibling != NULL;
prev = prev->sibling)
if (prev->sibling == clinfo) {
prev->sibling = clinfo->sibling;
break;
}
}
/* delete class from kernel */
if ((error = (*ifinfo->qdisc->delete_class)(clinfo)) != 0)
return (error);
if (clinfo->private != NULL)
free(clinfo->private);
if (clinfo->clname != NULL)
free(clinfo->clname);
free(clinfo);
return (0);
}
/* check and save the filter */
ifinfo = clinfo->ifinfo;
if ((error = add_filter_rule(ifinfo, fltrinfo, conflict)) != 0)
goto err_ret;
/* install the filter to the kernel */
if ((error = (*ifinfo->qdisc->add_filter)(fltrinfo)) != 0) {
remove_filter_rule(ifinfo, fltrinfo);
goto err_ret;
}
/* link fltrinfo onto fltrlist of the class */
LIST_INSERT_HEAD(&clinfo->fltrlist, fltrinfo, next);
if (rp != NULL)
*rp = fltrinfo;
return (0);
err_ret:
if (fltrinfo != NULL) {
if (fltrinfo->flname != NULL)
free(fltrinfo->flname);
free(fltrinfo);
}
return (error);
}
int
qop_delete_filter(struct fltrinfo *fltrinfo)
{
struct ifinfo *ifinfo;
struct classinfo *clinfo;
int error;
/* remove filter info from the class */
clinfo = fltrinfo->clinfo;
ifinfo = clinfo->ifinfo;
/* remove the entry from fltrlist of the class */
LIST_REMOVE(fltrinfo, next);
remove_filter_rule(ifinfo, fltrinfo);
/* delete filter from kernel */
if ((error = (*ifinfo->qdisc->delete_filter)(fltrinfo)) != 0)
return (error);
if (fltrinfo->flname)
free(fltrinfo->flname);
free(fltrinfo);
return (0);
}
/*
* functions to walk through a class tree:
*
* for (clinfo = get_rootclass(ifinfo);
* clinfo != NULL; clinfo = get_nextclass(clinfo)) {
* do_something;
* }
*/
struct classinfo *get_rootclass(struct ifinfo *ifinfo)
{
struct classinfo *clinfo;
/* find a class without parent */
LIST_FOREACH(clinfo, &ifinfo->cllist, next)
if (clinfo->parent == NULL)
return (clinfo);
return (NULL);
}
/* return next class in the tree */
struct classinfo *get_nextclass(struct classinfo *clinfo)
{
struct classinfo *next;
if (clinfo->child != NULL)
next = clinfo->child;
else if (clinfo->sibling != NULL)
next = clinfo->sibling;
else {
next = clinfo;
while ((next = next->parent) != NULL)
if (next->sibling) {
next = next->sibling;
break;
}
}
return (next);
}
LIST_FOREACH(info, &tbr_list, link)
if (strcmp(info->ifname, ifname) == 0)
break;
if (info == NULL)
return;
if (info->tb_prof.rate == 0 || info->installed)
return;
/* get the current token bucket regulator */
if ((fd = open(ALTQ_DEVICE, O_RDWR)) < 0)
err(1, "can't open altq device");
strncpy(req.ifname, ifname, IFNAMSIZ-1);
if (ioctl(fd, ALTQTBRGET, &req) < 0)
err(1, "ALTQTBRGET for interface %s", req.ifname);
/* save the current values */
info->otb_prof.rate = req.tb_prof.rate;
info->otb_prof.depth = req.tb_prof.depth;
/*
* if tbr is not specified in the config file and tbr is already
* configured, do not change.
*/
if (req.tb_prof.rate != 0) {
LOG(LOG_INFO, 0,
"tbr is already installed on %s,\n"
" using the current setting (rate:%.2fM size:%.2fK).",
info->ifname,
(double)req.tb_prof.rate/1000000.0,
(double)req.tb_prof.depth/1024.0);
close (fd);
return;
}
/* if the new size is not specified, use heuristics */
if (info->tb_prof.depth == 0) {
u_int rate, size;
rate = info->tb_prof.rate;
if (rate <= 1*1000*1000)
size = 1;
else if (rate <= 10*1000*1000)
size = 4;
else if (rate <= 200*1000*1000)
size = 8;
else
size = 24;
size = size * 1500; /* assume the default mtu is 1500 */
info->tb_prof.depth = size;
}
/* install the new tbr */
strncpy(req.ifname, ifname, IFNAMSIZ-1);
req.tb_prof.rate = info->tb_prof.rate;
req.tb_prof.depth = info->tb_prof.depth;
if (ioctl(fd, ALTQTBRSET, &req) < 0)
err(1, "ALTQTBRSET for interface %s", req.ifname);
LOG(LOG_INFO, 0,
"tbr installed on %s (rate:%.2fM size:%.2fK)",
info->ifname,
(double)info->tb_prof.rate/1000000.0,
(double)info->tb_prof.depth/1024.0);
close(fd);
info->installed = 1;
}
/*
* functions to check the filter-rules.
* when a new filter is added, we check the relation to the existing filters
* and if some inconsistency is found, produce an error or a warning message.
*
* filter matching is performed from the head of the list.
* let
* S: a set of packets that filter s matches
* T: a set of packets that filter t matches
* filter relations are:
* disjoint: S ^ T = empty
* subset: S <= T
* intersect: S ^ T = not empty
*
* a new filter is disjoint or subset of the existing filters --> ok
* a new filter is superset of an existing filter --> order problem
* a new filter intersect an existing filter --> warning
*
* port-intersect: a special case we don't make warning
* - intersection is only port numbers
* - one specifies src port and the other specifies dst port
* there must be no packet with well-known port numbers in
* both src and dst ports. so this is ok.
*/
switch (relation) {
case FILT_SUBSET:
case FILT_DISJOINT:
/* OK */
break;
case FILT_SUPERSET:
if (front->dontwarn == 0 && back->dontwarn == 0)
LOG(LOG_ERR, 0,
"filters for \"%s\" at line %d and for \"%s\" at line %d has an order problem!",
front->clinfo->clname, front->line_no,
back->clinfo->clname, back->line_no);
if (conflict != NULL)
*conflict = fp;
return (QOPERR_FILTER_SHADOW);
case FILT_PORTINTERSECT:
break;
case FILT_INTERSECT:
/*
* if the intersecting two filters belonging to the
* same class, it's ok.
*/
if (front->clinfo == back->clinfo)
break;
if (front->dontwarn == 0 && back->dontwarn == 0)
LOG(LOG_WARNING, 0,
"warning: filter for \"%s\" at line %d could override filter for \"%s\" at line %d",
front->clinfo->clname, front->line_no,
back->clinfo->clname, back->line_no);
break;
}
}
/*
* check if "front" is a subset of "back". assumes they are not disjoint
* return value 0: not a subset
* 1: subset
* 2: subset except src & dst ports
* (possible port-intersect)
*/
static int
filt_subset(struct flow_filter *front, struct flow_filter *back)
{
u_int16_t srcport, dstport;
if (front->ff_flow.fi_family == AF_INET) {
if (front->ff_flow.fi_proto == 0 &&
back->ff_flow.fi_proto != 0)
return (0);
if (front->ff_flow.fi_gpi == 0 && back->ff_flow.fi_gpi != 0)
return (0);
if (front->ff_flow.fi_src.s_addr == 0) {
if (back->ff_flow.fi_src.s_addr != 0)
return (0);
} else if (back->ff_flow.fi_src.s_addr != 0 &&
(~front->ff_mask.mask_src.s_addr &
back->ff_mask.mask_src.s_addr))
return (0);
if (front->ff_flow.fi_dst.s_addr == 0) {
if (back->ff_flow.fi_dst.s_addr != 0)
return (0);
} else if (back->ff_flow.fi_dst.s_addr != 0 &&
(~front->ff_mask.mask_dst.s_addr &
back->ff_mask.mask_dst.s_addr))
return (0);
if (~front->ff_mask.mask_tos & back->ff_mask.mask_tos)
return (0);
if (front6->ff_flow6.fi6_proto == 0 &&
back6->ff_flow6.fi6_proto != 0)
return (0);
if (front6->ff_flow6.fi6_flowlabel == 0 &&
back6->ff_flow6.fi6_flowlabel != 0)
return (0);
if (front6->ff_flow6.fi6_gpi == 0 &&
back6->ff_flow6.fi6_gpi != 0)
return (0);
if (IN6_IS_ADDR_UNSPECIFIED(&front6->ff_flow6.fi6_src)) {
if (!IN6_IS_ADDR_UNSPECIFIED(&back6->ff_flow6.fi6_src))
return (0);
} else if (!IN6_IS_ADDR_UNSPECIFIED(&back6->ff_flow6.fi6_src))
for (i=0; i<4; i++)
if (~IN6ADDR32_GET(&front6->ff_mask6.mask6_src, i) &
IN6ADDR32_GET(&back6->ff_mask6.mask6_src, i))
return (0);
if (IN6_IS_ADDR_UNSPECIFIED(&front6->ff_flow6.fi6_dst)) {
if (!IN6_IS_ADDR_UNSPECIFIED(&back6->ff_flow6.fi6_dst))
return (0);
} else if (!IN6_IS_ADDR_UNSPECIFIED(&back6->ff_flow6.fi6_dst))
for (i=0; i<4; i++)
if (~IN6ADDR32_GET(&front6->ff_mask6.mask6_dst, i) &
IN6ADDR32_GET(&back6->ff_mask6.mask6_dst, i))
return (0);
if (~front6->ff_mask6.mask6_tclass &
back6->ff_mask6.mask6_tclass)
return (0);
int
qop_rio_set_defaults(struct redparams *params)
{
int i, fd;
/* sanity check */
for (i = 1; i < RIO_NDROPPREC; i++) {
if (params[i].th_max > params[i-1].th_min)
LOG(LOG_WARNING, 0,
"warning: overlap found in RIO thresholds");
}
/*
* try to load and open KLD module
* (also check the altq device file)
*/
int
open_module(const char *dvname, int flags)
{
#if defined(__FreeBSD__) && (__FreeBSD_version > 300000)
char modname[64], filename[MAXPATHLEN], *cp;
int fd;
#endif
struct stat sbuf;
/* check if the altq device exists */
if (stat(dvname, &sbuf) < 0) {
LOG(LOG_ERR, errno, "can't access %s!", dvname);
return (-1);
}
#if defined(__FreeBSD__) && (__FreeBSD_version > 300000)
/* turn discipline name into module name */
strlcpy(modname, "altq_", sizeof(modname));
if ((cp = strrchr(devname, '/')) == NULL)
return (-1);
strlcat(modname, cp + 1, sizeof(modname));
/* check if the kld module exists */
snprintf(filename, sizeof(filename), "/modules/%s.ko", modname);
if (stat(filename, &sbuf) < 0) {
/* module file doesn't exist */
return (-1);
}