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