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