Home | History | Annotate | Line # | Download | only in altq
altq_afmap.c revision 1.9.12.3
      1 /*	$NetBSD: altq_afmap.c,v 1.9.12.3 2006/09/25 03:56:59 peter Exp $	*/
      2 /*	$KAME: altq_afmap.c,v 1.12 2005/04/13 03:44:24 suz Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1997-2002
      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 /*
     31  * experimental:
     32  * mapping an ip flow to atm vpi/vci.
     33  * this module is not related to queueing at all, but uses the altq
     34  * flowinfo mechanism.  it's just put in the altq framework since
     35  * it is easy to add devices to altq.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: altq_afmap.c,v 1.9.12.3 2006/09/25 03:56:59 peter Exp $");
     40 
     41 #ifdef _KERNEL_OPT
     42 #include "opt_altq.h"
     43 #include "opt_inet.h"
     44 #endif
     45 
     46 #ifdef ALTQ_AFMAP
     47 
     48 #include <sys/param.h>
     49 #include <sys/malloc.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/uio.h>
     52 #include <sys/socket.h>
     53 #include <sys/systm.h>
     54 #include <sys/proc.h>
     55 #include <sys/errno.h>
     56 #include <sys/time.h>
     57 #include <sys/kernel.h>
     58 #include <sys/kauth.h>
     59 
     60 #include <net/if.h>
     61 #include <net/if_types.h>
     62 #include <netinet/in.h>
     63 
     64 #include <altq/altq.h>
     65 #include <altq/altq_conf.h>
     66 #include <altq/altq_afmap.h>
     67 
     68 #ifdef ALTQ3_COMPAT
     69 
     70 LIST_HEAD(, afm_head) afhead_chain;
     71 
     72 static struct afm *afm_match4(struct afm_head *, struct flowinfo_in *);
     73 #ifdef INET6
     74 static struct afm *afm_match6(struct afm_head *, struct flowinfo_in6 *);
     75 #endif
     76 
     77 /*
     78  * rules to block interrupts: afm_match can be called from a net
     79  * level interrupt so that other routines handling the lists should
     80  * be called in splnet().
     81  */
     82 int
     83 afm_alloc(ifp)
     84 	struct ifnet *ifp;
     85 {
     86 	struct afm_head *head;
     87 
     88 	head = malloc(sizeof(struct afm_head), M_DEVBUF, M_WAITOK|M_ZERO);
     89 	if (head == NULL)
     90 		panic("afm_alloc: malloc failed!");
     91 
     92 	/* initialize per interface afmap list */
     93 	LIST_INIT(&head->afh_head);
     94 
     95 	head->afh_ifp = ifp;
     96 
     97 	/* add this afm_head to the chain */
     98 	LIST_INSERT_HEAD(&afhead_chain, head, afh_chain);
     99 
    100 	return (0);
    101 }
    102 
    103 int
    104 afm_dealloc(ifp)
    105 	struct ifnet *ifp;
    106 {
    107 	struct afm_head *head;
    108 
    109 	for (head = afhead_chain.lh_first; head != NULL;
    110 	     head = head->afh_chain.le_next)
    111 		if (head->afh_ifp == ifp)
    112 			break;
    113 	if (head == NULL)
    114 		return (-1);
    115 
    116 	afm_removeall(ifp);
    117 
    118 	LIST_REMOVE(head, afh_chain);
    119 
    120 	free(head, M_DEVBUF);
    121 	return 0;
    122 }
    123 
    124 struct afm *
    125 afm_top(ifp)
    126 	struct ifnet *ifp;
    127 {
    128 	struct afm_head *head;
    129 
    130 	for (head = afhead_chain.lh_first; head != NULL;
    131 	     head = head->afh_chain.le_next)
    132 		if (head->afh_ifp == ifp)
    133 			break;
    134 	if (head == NULL)
    135 		return NULL;
    136 
    137 	return (head->afh_head.lh_first);
    138 }
    139 
    140 int afm_add(ifp, flowmap)
    141 	struct ifnet *ifp;
    142 	struct atm_flowmap *flowmap;
    143 {
    144 	struct afm_head *head;
    145 	struct afm *afm;
    146 
    147 	for (head = afhead_chain.lh_first; head != NULL;
    148 	     head = head->afh_chain.le_next)
    149 		if (head->afh_ifp == ifp)
    150 			break;
    151 	if (head == NULL)
    152 		return (-1);
    153 
    154 	if (flowmap->af_flowinfo.fi_family == AF_INET) {
    155 		if (flowmap->af_flowinfo.fi_len != sizeof(struct flowinfo_in))
    156 			return (EINVAL);
    157 #ifdef INET6
    158 	} else if (flowmap->af_flowinfo.fi_family == AF_INET6) {
    159 		if (flowmap->af_flowinfo.fi_len != sizeof(struct flowinfo_in6))
    160 			return (EINVAL);
    161 #endif
    162 	} else
    163 		return (EINVAL);
    164 
    165 	afm = malloc(sizeof(struct afm), M_DEVBUF, M_WAITOK|M_ZERO);
    166 	if (afm == NULL)
    167 		return (ENOMEM);
    168 
    169 	afm->afm_vci = flowmap->af_vci;
    170 	afm->afm_vpi = flowmap->af_vpi;
    171 	(void)memcpy(&afm->afm_flowinfo, &flowmap->af_flowinfo,
    172 	      flowmap->af_flowinfo.fi_len);
    173 
    174 	LIST_INSERT_HEAD(&head->afh_head, afm, afm_list);
    175 	return 0;
    176 }
    177 
    178 int
    179 afm_remove(afm)
    180 	struct afm *afm;
    181 {
    182 	LIST_REMOVE(afm, afm_list);
    183 	free(afm, M_DEVBUF);
    184 	return (0);
    185 }
    186 
    187 int
    188 afm_removeall(ifp)
    189 	struct ifnet *ifp;
    190 {
    191 	struct afm_head *head;
    192 	struct afm *afm;
    193 
    194 	for (head = afhead_chain.lh_first; head != NULL;
    195 	     head = head->afh_chain.le_next)
    196 		if (head->afh_ifp == ifp)
    197 			break;
    198 	if (head == NULL)
    199 		return (-1);
    200 
    201 	while ((afm = head->afh_head.lh_first) != NULL)
    202 		afm_remove(afm);
    203 	return (0);
    204 }
    205 
    206 struct afm *
    207 afm_lookup(ifp, vpi, vci)
    208 	struct ifnet *ifp;
    209 	int vpi, vci;
    210 {
    211 	struct afm_head *head;
    212 	struct afm *afm;
    213 
    214 	for (head = afhead_chain.lh_first; head != NULL;
    215 	     head = head->afh_chain.le_next)
    216 		if (head->afh_ifp == ifp)
    217 			break;
    218 	if (head == NULL)
    219 		return NULL;
    220 
    221 	for (afm = head->afh_head.lh_first; afm != NULL;
    222 	     afm = afm->afm_list.le_next)
    223 		if (afm->afm_vpi == vpi && afm->afm_vci == vci)
    224 			break;
    225 	return afm;
    226 }
    227 
    228 static struct afm *
    229 afm_match4(head, fp)
    230 	struct afm_head *head;
    231 	struct flowinfo_in *fp;
    232 {
    233 	struct afm *afm;
    234 
    235 	for (afm = head->afh_head.lh_first; afm != NULL;
    236 	     afm = afm->afm_list.le_next) {
    237 		if (afm->afm_flowinfo4.fi_dst.s_addr != 0 &&
    238 		    afm->afm_flowinfo4.fi_dst.s_addr != fp->fi_dst.s_addr)
    239 			continue;
    240 		if (afm->afm_flowinfo4.fi_dport != 0 &&
    241 		    afm->afm_flowinfo4.fi_dport != fp->fi_dport)
    242 			continue;
    243 		if (afm->afm_flowinfo4.fi_src.s_addr != 0 &&
    244 		    afm->afm_flowinfo4.fi_src.s_addr != fp->fi_src.s_addr)
    245 			continue;
    246 		if (afm->afm_flowinfo4.fi_sport != 0 &&
    247 		    afm->afm_flowinfo4.fi_sport != fp->fi_sport)
    248 			continue;
    249 		if (afm->afm_flowinfo4.fi_proto != 0 &&
    250 		    afm->afm_flowinfo4.fi_proto != fp->fi_proto)
    251 			continue;
    252 		/* match found! */
    253 		return (afm);
    254 	}
    255 	return NULL;
    256 }
    257 
    258 #ifdef INET6
    259 static struct afm *
    260 afm_match6(head, fp)
    261 	struct afm_head *head;
    262 	struct flowinfo_in6 *fp;
    263 {
    264 	struct afm *afm;
    265 
    266 	for (afm = head->afh_head.lh_first; afm != NULL;
    267 	     afm = afm->afm_list.le_next) {
    268 		if (afm->afm_flowinfo6.fi6_flowlabel != 0 &&
    269 		    afm->afm_flowinfo6.fi6_flowlabel != fp->fi6_flowlabel)
    270 			continue;
    271 #ifdef notyet
    272 		if (!IN6_IS_ADDR_UNSPECIFIED(&afm->afm_flowinfo6.fi6_dst) &&
    273 		    !IN6_ARE_ADDR_EQUAL(&afm->afm_flowinfo6.fi6_dst,
    274 					&fp->fi6_dst))
    275 			continue;
    276 		if (afm->afm_flowinfo6.fi6_dport != 0 &&
    277 		    afm->afm_flowinfo6.fi6_dport != fp->fi6_dport)
    278 			continue;
    279 #endif
    280 		if (!IN6_IS_ADDR_UNSPECIFIED(&afm->afm_flowinfo6.fi6_src) &&
    281 		    !IN6_ARE_ADDR_EQUAL(&afm->afm_flowinfo6.fi6_src,
    282 					&fp->fi6_src))
    283 			continue;
    284 #ifdef notyet
    285 		if (afm->afm_flowinfo6.fi6_sport != 0 &&
    286 		    afm->afm_flowinfo6.fi6_sport != fp->fi6_sport)
    287 			continue;
    288 #endif
    289 		if (afm->afm_flowinfo6.fi6_proto != 0 &&
    290 		    afm->afm_flowinfo6.fi6_proto != fp->fi6_proto)
    291 			continue;
    292 		/* match found! */
    293 		return (afm);
    294 	}
    295 	return NULL;
    296 }
    297 #endif
    298 
    299 /* should be called in splnet() */
    300 struct afm *
    301 afm_match(ifp, flow)
    302 	struct ifnet *ifp;
    303 	struct flowinfo *flow;
    304 {
    305 	struct afm_head *head;
    306 
    307 	for (head = afhead_chain.lh_first; head != NULL;
    308 	     head = head->afh_chain.le_next)
    309 		if (head->afh_ifp == ifp)
    310 			break;
    311 	if (head == NULL)
    312 		return NULL;
    313 
    314 	switch (flow->fi_family) {
    315 	case AF_INET:
    316 		return (afm_match4(head, (struct flowinfo_in *)flow));
    317 
    318 #ifdef INET6
    319 	case AF_INET6:
    320 		return (afm_match6(head, (struct flowinfo_in6 *)flow));
    321 #endif
    322 
    323 	default:
    324 		return NULL;
    325 	}
    326 }
    327 
    328 /*
    329  * afm device interface
    330  */
    331 altqdev_decl(afm);
    332 
    333 int
    334 afmopen(dev, flag, fmt, l)
    335 	dev_t dev;
    336 	int flag, fmt;
    337 	struct lwp *l;
    338 {
    339 	return 0;
    340 }
    341 
    342 int
    343 afmclose(dev, flag, fmt, l)
    344 	dev_t dev;
    345 	int flag, fmt;
    346 	struct lwp *l;
    347 {
    348 	int err, error = 0;
    349 	struct atm_flowmap fmap;
    350 	struct afm_head *head;
    351 
    352 	for (head = afhead_chain.lh_first; head != NULL;
    353 	     head = head->afh_chain.le_next) {
    354 
    355 		/* call interface to clean up maps */
    356 		sprintf(fmap.af_ifname, "%s", head->afh_ifp->if_xname);
    357 		err = afmioctl(dev, AFM_CLEANFMAP, (caddr_t)&fmap, flag, l);
    358 		if (err && error == 0)
    359 			error = err;
    360 	}
    361 
    362 	return error;
    363 }
    364 
    365 int
    366 afmioctl(dev, cmd, addr, flag, l)
    367 	dev_t dev;
    368 	ioctlcmd_t cmd;
    369 	caddr_t addr;
    370 	int flag;
    371 	struct lwp *l;
    372 {
    373 	int	error = 0;
    374 	struct atm_flowmap *flowmap;
    375 	struct ifnet *ifp;
    376 
    377 	/* check cmd for superuser only */
    378 	switch (cmd) {
    379 	case AFM_GETFMAP:
    380 		break;
    381 	default:
    382 #if (__FreeBSD_version > 400000)
    383 		error = suser(p);
    384 #else
    385 		error = kauth_authorize_generic(l->l_cred,
    386 		    KAUTH_GENERIC_ISSUSER, &l->l_acflag);
    387 #endif
    388 		if (error)
    389 			return (error);
    390 		break;
    391 	}
    392 
    393 	/* lookup interface */
    394 	flowmap = (struct atm_flowmap *)addr;
    395 	flowmap->af_ifname[IFNAMSIZ-1] = '\0';
    396 	ifp = ifunit(flowmap->af_ifname);
    397 	if (ifp == NULL || ifp->if_ioctl == NULL ||
    398 	    (ifp->if_flags & IFF_RUNNING) == 0)
    399 		error = ENXIO;
    400 	else
    401 		error = ifp->if_ioctl(ifp, cmd, addr);
    402 
    403 	return error;
    404 }
    405 
    406 #endif /* ALTQ3_COMPAT */
    407 #endif /* ALTQ_AFMAP */
    408