Home | History | Annotate | Line # | Download | only in altq
      1 /*	$NetBSD: altq_afmap.c,v 1.25 2025/01/14 13:51:49 joe 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.25 2025/01/14 13:51:49 joe 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(struct ifnet *ifp)
     84 {
     85 	struct afm_head *head;
     86 
     87 	head = malloc(sizeof(struct afm_head), M_DEVBUF, M_WAITOK|M_ZERO);
     88 	if (head == NULL)
     89 		panic("afm_alloc: malloc failed!");
     90 
     91 	/* initialize per interface afmap list */
     92 	LIST_INIT(&head->afh_head);
     93 
     94 	head->afh_ifp = ifp;
     95 
     96 	/* add this afm_head to the chain */
     97 	LIST_INSERT_HEAD(&afhead_chain, head, afh_chain);
     98 
     99 	return 0;
    100 }
    101 
    102 int
    103 afm_dealloc(struct ifnet *ifp)
    104 {
    105 	struct afm_head *head;
    106 
    107 	if ((head = afmhead_if(ifp)) == NULL)
    108 		return -1 ;
    109 
    110 	afm_removeall(ifp);
    111 
    112 	LIST_REMOVE(head, afh_chain);
    113 
    114 	free(head, M_DEVBUF);
    115 	return 0;
    116 }
    117 
    118 struct afm *
    119 afm_top(struct ifnet *ifp)
    120 {
    121 	struct afm_head *head;
    122 
    123 	if ((head = afmhead_if(ifp)) == NULL)
    124 		return NULL;
    125 
    126 	return (head->afh_head.lh_first);
    127 }
    128 
    129 int
    130 afm_add(struct ifnet *ifp, struct atm_flowmap *flowmap)
    131 {
    132 	struct afm_head *head;
    133 	struct afm *afm;
    134 
    135 	if ((head = afmhead_if(ifp)) == NULL)
    136 		return -1;
    137 
    138 	if (flowmap->af_flowinfo.fi_family == AF_INET) {
    139 		if (flowmap->af_flowinfo.fi_len != sizeof(struct flowinfo_in))
    140 			return EINVAL;
    141 #ifdef INET6
    142 	} else if (flowmap->af_flowinfo.fi_family == AF_INET6) {
    143 		if (flowmap->af_flowinfo.fi_len != sizeof(struct flowinfo_in6))
    144 			return EINVAL;
    145 #endif
    146 	} else
    147 		return EINVAL;
    148 
    149 	afm = malloc(sizeof(struct afm), M_DEVBUF, M_WAITOK|M_ZERO);
    150 	if (afm == NULL)
    151 		return ENOMEM;
    152 
    153 	afm->afm_vci = flowmap->af_vci;
    154 	afm->afm_vpi = flowmap->af_vpi;
    155 	(void)memcpy(&afm->afm_flowinfo, &flowmap->af_flowinfo,
    156 	      flowmap->af_flowinfo.fi_len);
    157 
    158 	LIST_INSERT_HEAD(&head->afh_head, afm, afm_list);
    159 	return 0;
    160 }
    161 
    162 int
    163 afm_remove(struct afm *afm)
    164 {
    165 	LIST_REMOVE(afm, afm_list);
    166 	free(afm, M_DEVBUF);
    167 	return 0;
    168 }
    169 
    170 int
    171 afm_removeall(struct ifnet *ifp)
    172 {
    173 	struct afm_head *head;
    174 	struct afm *afm;
    175 
    176 	if ((head = afmhead_if(ifp)) == NULL)
    177 		return -1;
    178 
    179 	while ((afm = head->afh_head.lh_first) != NULL)
    180 		afm_remove(afm);
    181 	return 0;
    182 }
    183 
    184 struct afm *
    185 afm_lookup(struct ifnet *ifp, int vpi, int vci)
    186 {
    187 	struct afm_head *head;
    188 	struct afm *afm;
    189 
    190 	if ((head = afmhead_if(ifp)) == NULL)
    191 		return NULL;
    192 
    193 	for (afm = head->afh_head.lh_first; afm != NULL;
    194 	     afm = afm->afm_list.le_next)
    195 		if (afm->afm_vpi == vpi && afm->afm_vci == vci)
    196 			break;
    197 	return afm;
    198 }
    199 
    200 static struct afm *
    201 afm_match4(struct afm_head *head, struct flowinfo_in *fp)
    202 {
    203 	struct afm *afm;
    204 
    205 	for (afm = head->afh_head.lh_first; afm != NULL;
    206 	     afm = afm->afm_list.le_next) {
    207 		if (afm->afm_flowinfo4.fi_dst.s_addr != 0 &&
    208 		    afm->afm_flowinfo4.fi_dst.s_addr != fp->fi_dst.s_addr)
    209 			continue;
    210 		if (afm->afm_flowinfo4.fi_dport != 0 &&
    211 		    afm->afm_flowinfo4.fi_dport != fp->fi_dport)
    212 			continue;
    213 		if (afm->afm_flowinfo4.fi_src.s_addr != 0 &&
    214 		    afm->afm_flowinfo4.fi_src.s_addr != fp->fi_src.s_addr)
    215 			continue;
    216 		if (afm->afm_flowinfo4.fi_sport != 0 &&
    217 		    afm->afm_flowinfo4.fi_sport != fp->fi_sport)
    218 			continue;
    219 		if (afm->afm_flowinfo4.fi_proto != 0 &&
    220 		    afm->afm_flowinfo4.fi_proto != fp->fi_proto)
    221 			continue;
    222 		/* match found! */
    223 		return afm;
    224 	}
    225 	return NULL;
    226 }
    227 
    228 #ifdef INET6
    229 static struct afm *
    230 afm_match6(struct afm_head *head, struct flowinfo_in6 *fp)
    231 {
    232 	struct afm *afm;
    233 
    234 	for (afm = head->afh_head.lh_first; afm != NULL;
    235 	     afm = afm->afm_list.le_next) {
    236 		if (afm->afm_flowinfo6.fi6_flowlabel != 0 &&
    237 		    afm->afm_flowinfo6.fi6_flowlabel != fp->fi6_flowlabel)
    238 			continue;
    239 #ifdef notyet
    240 		if (!IN6_IS_ADDR_UNSPECIFIED(&afm->afm_flowinfo6.fi6_dst) &&
    241 		    !IN6_ARE_ADDR_EQUAL(&afm->afm_flowinfo6.fi6_dst,
    242 					&fp->fi6_dst))
    243 			continue;
    244 		if (afm->afm_flowinfo6.fi6_dport != 0 &&
    245 		    afm->afm_flowinfo6.fi6_dport != fp->fi6_dport)
    246 			continue;
    247 #endif
    248 		if (!IN6_IS_ADDR_UNSPECIFIED(&afm->afm_flowinfo6.fi6_src) &&
    249 		    !IN6_ARE_ADDR_EQUAL(&afm->afm_flowinfo6.fi6_src,
    250 					&fp->fi6_src))
    251 			continue;
    252 #ifdef notyet
    253 		if (afm->afm_flowinfo6.fi6_sport != 0 &&
    254 		    afm->afm_flowinfo6.fi6_sport != fp->fi6_sport)
    255 			continue;
    256 #endif
    257 		if (afm->afm_flowinfo6.fi6_proto != 0 &&
    258 		    afm->afm_flowinfo6.fi6_proto != fp->fi6_proto)
    259 			continue;
    260 		/* match found! */
    261 		return (afm);
    262 	}
    263 	return NULL;
    264 }
    265 #endif
    266 
    267 /* should be called in splnet() */
    268 struct afm *
    269 afm_match(struct ifnet *ifp, struct flowinfo *flow)
    270 {
    271 	struct afm_head *head;
    272 
    273 	if ((head = afmhead_if(ifp)) == NULL)
    274 		return NULL;
    275 
    276 	switch (flow->fi_family) {
    277 	case AF_INET:
    278 		return (afm_match4(head, (struct flowinfo_in *)flow));
    279 
    280 #ifdef INET6
    281 	case AF_INET6:
    282 		return (afm_match6(head, (struct flowinfo_in6 *)flow));
    283 #endif
    284 
    285 	default:
    286 		return NULL;
    287 	}
    288 }
    289 
    290 /* find the address family node on the current interface */
    291 struct afm_head *
    292 afmhead_if(struct ifnet *ifp)
    293 {
    294 	struct afm_head *head;
    295 	for (head = afhead_chain.lh_first; head != NULL;
    296 	    head = head->afh_chain.le_next)
    297 		if (head->afh_ifp == ifp)
    298 			return head;
    299 	return NULL;
    300 }
    301 
    302 /*
    303  * afm device interface
    304  */
    305 altqdev_decl(afm);
    306 
    307 int
    308 afmopen(dev_t dev, int flag, int fmt,
    309     struct lwp *l)
    310 {
    311 	return 0;
    312 }
    313 
    314 int
    315 afmclose(dev_t dev, int flag, int fmt, struct lwp *l)
    316 {
    317 	int err, error = 0;
    318 	struct atm_flowmap fmap;
    319 	struct afm_head *head;
    320 
    321 	for (head = afhead_chain.lh_first; head != NULL;
    322 	     head = head->afh_chain.le_next) {
    323 
    324 		/* call interface to clean up maps */
    325 		snprintf(fmap.af_ifname, sizeof(fmap.af_ifname),
    326 		    "%s", head->afh_ifp->if_xname);
    327 		err = afmioctl(dev, AFM_CLEANFMAP, (void *)&fmap, flag, l);
    328 		if (err && error == 0)
    329 			error = err;
    330 	}
    331 
    332 	return error;
    333 }
    334 
    335 int
    336 afmioctl(dev_t dev, ioctlcmd_t cmd, void *addr, int flag,
    337     struct lwp *l)
    338 {
    339 	int	error = 0;
    340 	struct atm_flowmap *flowmap;
    341 	struct ifnet *ifp;
    342 
    343 	/* check cmd for superuser only */
    344 	switch (cmd) {
    345 	case AFM_GETFMAP:
    346 		break;
    347 	default:
    348 		error = kauth_authorize_network(l->l_cred,
    349 		    KAUTH_NETWORK_ALTQ, KAUTH_REQ_NETWORK_ALTQ_AFMAP, NULL,
    350 		    NULL, NULL);
    351 		if (error)
    352 			return error;
    353 		break;
    354 	}
    355 
    356 	/* lookup interface */
    357 	flowmap = (struct atm_flowmap *)addr;
    358 	flowmap->af_ifname[IFNAMSIZ-1] = '\0';
    359 	ifp = ifunit(flowmap->af_ifname);
    360 	if (ifp == NULL)
    361 		return ENXIO;
    362 
    363 	IFNET_LOCK(ifp);
    364 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    365 		error = ENXIO;
    366 	else
    367 		error = if_ioctl(ifp, cmd, addr);
    368 	IFNET_UNLOCK(ifp);
    369 
    370 	return error;
    371 }
    372 
    373 #endif /* ALTQ3_COMPAT */
    374 #endif /* ALTQ_AFMAP */
    375