Home | History | Annotate | Line # | Download | only in altq
altq_conf.c revision 1.12.12.3
      1 /*	$NetBSD: altq_conf.c,v 1.12.12.3 2006/09/25 03:56:59 peter Exp $	*/
      2 /*	$KAME: altq_conf.c,v 1.24 2005/04/13 03:44:24 suz Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1997-2003
      6  *	Sony Computer Science Laboratories Inc.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: altq_conf.c,v 1.12.12.3 2006/09/25 03:56:59 peter Exp $");
     32 
     33 #ifdef _KERNEL_OPT
     34 #include "opt_altq.h"
     35 #include "opt_inet.h"
     36 #endif
     37 
     38 /*
     39  * altq device interface.
     40  */
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/socket.h>
     44 #include <sys/kernel.h>
     45 #include <sys/proc.h>
     46 #include <sys/errno.h>
     47 #include <sys/kauth.h>
     48 
     49 #include <net/if.h>
     50 
     51 #include <altq/altq.h>
     52 #include <altq/altqconf.h>
     53 #include <altq/altq_conf.h>
     54 
     55 #ifdef ALTQ3_COMPAT
     56 
     57 #ifdef ALTQ_CBQ
     58 altqdev_decl(cbq);
     59 #endif
     60 #ifdef ALTQ_WFQ
     61 altqdev_decl(wfq);
     62 #endif
     63 #ifdef ALTQ_AFMAP
     64 altqdev_decl(afm);
     65 #endif
     66 #ifdef ALTQ_FIFOQ
     67 altqdev_decl(fifoq);
     68 #endif
     69 #ifdef ALTQ_RED
     70 altqdev_decl(red);
     71 #endif
     72 #ifdef ALTQ_RIO
     73 altqdev_decl(rio);
     74 #endif
     75 #ifdef ALTQ_LOCALQ
     76 altqdev_decl(localq);
     77 #endif
     78 #ifdef ALTQ_HFSC
     79 altqdev_decl(hfsc);
     80 #endif
     81 #ifdef ALTQ_CDNR
     82 altqdev_decl(cdnr);
     83 #endif
     84 #ifdef ALTQ_BLUE
     85 altqdev_decl(blue);
     86 #endif
     87 #ifdef ALTQ_PRIQ
     88 altqdev_decl(priq);
     89 #endif
     90 #ifdef ALTQ_JOBS
     91 altqdev_decl(jobs);
     92 #endif
     93 
     94 /*
     95  * altq minor device (discipline) table
     96  */
     97 static struct altqsw altqsw[] = {				/* minor */
     98 	{"altq", noopen,	noclose,	noioctl},  /* 0 (reserved) */
     99 #ifdef ALTQ_CBQ
    100 	{"cbq",	cbqopen,	cbqclose,	cbqioctl},	/* 1 */
    101 #else
    102 	{"noq",	noopen,		noclose,	noioctl},	/* 1 */
    103 #endif
    104 #ifdef ALTQ_WFQ
    105 	{"wfq",	wfqopen,	wfqclose,	wfqioctl},	/* 2 */
    106 #else
    107 	{"noq",	noopen,		noclose,	noioctl},	/* 2 */
    108 #endif
    109 #ifdef ALTQ_AFMAP
    110 	{"afm",	afmopen,	afmclose,	afmioctl},	/* 3 */
    111 #else
    112 	{"noq",	noopen,		noclose,	noioctl},	/* 3 */
    113 #endif
    114 #ifdef ALTQ_FIFOQ
    115 	{"fifoq", fifoqopen,	fifoqclose,	fifoqioctl},	/* 4 */
    116 #else
    117 	{"noq",	noopen,		noclose,	noioctl},	/* 4 */
    118 #endif
    119 #ifdef ALTQ_RED
    120 	{"red", redopen,	redclose,	redioctl},	/* 5 */
    121 #else
    122 	{"noq",	noopen,		noclose,	noioctl},	/* 5 */
    123 #endif
    124 #ifdef ALTQ_RIO
    125 	{"rio", rioopen,	rioclose,	rioioctl},	/* 6 */
    126 #else
    127 	{"noq",	noopen,		noclose,	noioctl},	/* 6 */
    128 #endif
    129 #ifdef ALTQ_LOCALQ
    130 	{"localq",localqopen,	localqclose,	localqioctl}, /* 7 (local use) */
    131 #else
    132 	{"noq",	noopen,		noclose,	noioctl},  /* 7 (local use) */
    133 #endif
    134 #ifdef ALTQ_HFSC
    135 	{"hfsc",hfscopen,	hfscclose,	hfscioctl},	/* 8 */
    136 #else
    137 	{"noq",	noopen,		noclose,	noioctl},	/* 8 */
    138 #endif
    139 #ifdef ALTQ_CDNR
    140 	{"cdnr",cdnropen,	cdnrclose,	cdnrioctl},	/* 9 */
    141 #else
    142 	{"noq",	noopen,		noclose,	noioctl},	/* 9 */
    143 #endif
    144 #ifdef ALTQ_BLUE
    145 	{"blue",blueopen,	blueclose,	blueioctl},	/* 10 */
    146 #else
    147 	{"noq",	noopen,		noclose,	noioctl},	/* 10 */
    148 #endif
    149 #ifdef ALTQ_PRIQ
    150 	{"priq",priqopen,	priqclose,	priqioctl},	/* 11 */
    151 #else
    152 	{"noq",	noopen,		noclose,	noioctl},	/* 11 */
    153 #endif
    154 #ifdef ALTQ_JOBS
    155 	{"jobs",jobsopen,	jobsclose,	jobsioctl},	/* 12 */
    156 #else
    157 	{"noq", noopen,		noclose,	noioctl},	/* 12 */
    158 #endif
    159 };
    160 
    161 /*
    162  * altq major device support
    163  */
    164 int	naltqsw = sizeof (altqsw) / sizeof (altqsw[0]);
    165 
    166 dev_type_open(altqopen);
    167 dev_type_close(altqclose);
    168 dev_type_ioctl(altqioctl);
    169 
    170 const struct cdevsw altq_cdevsw = {
    171 	altqopen, altqclose, noread, nowrite, altqioctl,
    172 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
    173 };
    174 
    175 int
    176 altqopen(dev, flag, fmt, l)
    177 	dev_t dev;
    178 	int flag, fmt;
    179 	struct lwp *l;
    180 {
    181 	int unit = minor(dev);
    182 
    183 	if (unit == 0)
    184 		return (0);
    185 	if (unit < naltqsw)
    186 		return (*altqsw[unit].d_open)(dev, flag, fmt, l);
    187 
    188 	return ENXIO;
    189 }
    190 
    191 int
    192 altqclose(dev, flag, fmt, l)
    193 	dev_t dev;
    194 	int flag, fmt;
    195 	struct lwp *l;
    196 {
    197 	int unit = minor(dev);
    198 
    199 	if (unit == 0)
    200 		return (0);
    201 	if (unit < naltqsw)
    202 		return (*altqsw[unit].d_close)(dev, flag, fmt, l);
    203 
    204 	return ENXIO;
    205 }
    206 
    207 int
    208 altqioctl(dev, cmd, addr, flag, l)
    209 	dev_t dev;
    210 	ioctlcmd_t cmd;
    211 	caddr_t addr;
    212 	int flag;
    213 	struct lwp *l;
    214 {
    215 	int unit = minor(dev);
    216 
    217 	if (unit == 0) {
    218 		struct ifnet *ifp;
    219 		struct altqreq *typereq;
    220 		struct tbrreq *tbrreq;
    221 		int error;
    222 
    223 		switch (cmd) {
    224 		case ALTQGTYPE:
    225 		case ALTQTBRGET:
    226 			break;
    227 		default:
    228 #if (__FreeBSD_version > 400000)
    229 			if ((error = suser(p)) != 0)
    230 				return (error);
    231 #else
    232 			if ((error = kauth_authorize_generic(l->l_cred,
    233 			    KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
    234 				return (error);
    235 #endif
    236 			break;
    237 		}
    238 
    239 		switch (cmd) {
    240 		case ALTQGTYPE:
    241 			typereq = (struct altqreq *)addr;
    242 			if ((ifp = ifunit(typereq->ifname)) == NULL)
    243 				return (EINVAL);
    244 			typereq->arg = (u_long)ifp->if_snd.altq_type;
    245 			return (0);
    246 		case ALTQTBRSET:
    247 			tbrreq = (struct tbrreq *)addr;
    248 			if ((ifp = ifunit(tbrreq->ifname)) == NULL)
    249 				return (EINVAL);
    250 			return tbr_set(&ifp->if_snd, &tbrreq->tb_prof);
    251 		case ALTQTBRGET:
    252 			tbrreq = (struct tbrreq *)addr;
    253 			if ((ifp = ifunit(tbrreq->ifname)) == NULL)
    254 				return (EINVAL);
    255 			return tbr_get(&ifp->if_snd, &tbrreq->tb_prof);
    256 		default:
    257 			return (EINVAL);
    258 		}
    259 	}
    260 	if (unit < naltqsw)
    261 		return (*altqsw[unit].d_ioctl)(dev, cmd, addr, flag, l);
    262 
    263 	return ENXIO;
    264 }
    265 
    266 #ifdef __FreeBSD__
    267 static int altq_devsw_installed = 0;
    268 #endif
    269 
    270 #ifdef __FreeBSD__
    271 static void
    272 altq_drvinit(unused)
    273 	void *unused;
    274 {
    275 	int unit;
    276 
    277 #if 0
    278 	mtx_init(&altq_mtx, "altq global lock", MTX_DEF);
    279 #endif
    280 	altq_devsw_installed = 1;
    281 	printf("altq: attached. Major number assigned automatically.\n");
    282 
    283 	/* create minor devices */
    284 	for (unit = 0; unit < naltqsw; unit++) {
    285 		if (unit == 0 || altqsw[unit].d_open != NULL)
    286 			altqsw[unit].dev = make_dev(&altq_cdevsw, unit,
    287 			    UID_ROOT, GID_WHEEL, 0644, "altq/%s",
    288 			    altqsw[unit].d_name);
    289 	}
    290 }
    291 
    292 SYSINIT(altqdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,altq_drvinit,NULL)
    293 
    294 #endif /* FreeBSD */
    295 
    296 #ifdef ALTQ_KLD
    297 /*
    298  * KLD support
    299  */
    300 static int altq_module_register(struct altq_module_data *);
    301 static int altq_module_deregister(struct altq_module_data *);
    302 
    303 static struct altq_module_data *altq_modules[ALTQT_MAX];
    304 #if __FreeBSD_version < 502103
    305 static struct altqsw noqdisc = {"noq", noopen, noclose, noioctl};
    306 #else
    307 static struct altqsw noqdisc = {"noq"};
    308 #endif
    309 
    310 void altq_module_incref(type)
    311 	int type;
    312 {
    313 	if (type < 0 || type >= ALTQT_MAX || altq_modules[type] == NULL)
    314 		return;
    315 
    316 	altq_modules[type]->ref++;
    317 }
    318 
    319 void altq_module_declref(type)
    320 	int type;
    321 {
    322 	if (type < 0 || type >= ALTQT_MAX || altq_modules[type] == NULL)
    323 		return;
    324 
    325 	altq_modules[type]->ref--;
    326 }
    327 
    328 static int
    329 altq_module_register(mdata)
    330 	struct altq_module_data *mdata;
    331 {
    332 	int type = mdata->type;
    333 
    334 	if (type < 0 || type >= ALTQT_MAX)
    335 		return (EINVAL);
    336 #if (__FreeBSD_version < 502103)
    337 	if (altqsw[type].d_open != noopen)
    338 #else
    339 	if (altqsw[type].d_open != NULL)
    340 #endif
    341 		return (EBUSY);
    342 	altqsw[type] = *mdata->altqsw;	/* set discipline functions */
    343 	altq_modules[type] = mdata;	/* save module data pointer */
    344 #if (__FreeBSD_version < 502103)
    345 	make_dev(&altq_cdevsw, type, UID_ROOT, GID_WHEEL, 0644,
    346 		 "altq/%s", altqsw[type].d_name);
    347 #else
    348 	altqsw[type].dev = make_dev(&altq_cdevsw, type, UID_ROOT, GID_WHEEL,
    349 	    0644, "altq/%s", altqsw[type].d_name);
    350 #endif
    351 	return (0);
    352 }
    353 
    354 static int
    355 altq_module_deregister(mdata)
    356 	struct altq_module_data *mdata;
    357 {
    358 	int type = mdata->type;
    359 
    360 	if (type < 0 || type >= ALTQT_MAX)
    361 		return (EINVAL);
    362 	if (mdata != altq_modules[type])
    363 		return (EINVAL);
    364 	if (altq_modules[type]->ref > 0)
    365 		return (EBUSY);
    366 #if (__FreeBSD_version < 502103)
    367 	destroy_dev(makedev(CDEV_MAJOR, type));
    368 #else
    369 	destroy_dev(altqsw[type].dev);
    370 #endif
    371 	altqsw[type] = noqdisc;
    372 	altq_modules[type] = NULL;
    373 	return (0);
    374 }
    375 
    376 int
    377 altq_module_handler(mod, cmd, arg)
    378     module_t	mod;
    379     int cmd;
    380     void * arg;
    381 {
    382 	struct altq_module_data *data = (struct altq_module_data *)arg;
    383 	int	error = 0;
    384 
    385 	switch (cmd) {
    386 	case MOD_LOAD:
    387 		error = altq_module_register(data);
    388 		break;
    389 
    390 	case MOD_UNLOAD:
    391 		error = altq_module_deregister(data);
    392 		break;
    393 
    394 	default:
    395 		error = EINVAL;
    396 		break;
    397 	}
    398 
    399 	return(error);
    400 }
    401 
    402 #endif  /* ALTQ_KLD */
    403 #endif /* ALTQ3_COMPAT */
    404