if_atmsubr.c revision 148887
1/* $NetBSD: if_atmsubr.c,v 1.10 1997/03/11 23:19:51 chuck Exp $ */ 2 3/*- 4 * 5 * Copyright (c) 1996 Charles D. Cranor and Washington University. 6 * 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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Charles D. Cranor and 19 * Washington University. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * if_atmsubr.c 35 */ 36 37#include <sys/cdefs.h> 38__FBSDID("$FreeBSD: head/sys/net/if_atmsubr.c 148887 2005-08-09 10:20:02Z rwatson $"); 39 40#include "opt_inet.h" 41#include "opt_inet6.h" 42#include "opt_mac.h" 43#include "opt_natm.h" 44 45#include <sys/param.h> 46#include <sys/systm.h> 47#include <sys/kernel.h> 48#include <sys/module.h> 49#include <sys/mac.h> 50#include <sys/mbuf.h> 51#include <sys/socket.h> 52#include <sys/sockio.h> 53#include <sys/errno.h> 54#include <sys/sysctl.h> 55#include <sys/malloc.h> 56 57#include <net/if.h> 58#include <net/netisr.h> 59#include <net/route.h> 60#include <net/if_dl.h> 61#include <net/if_types.h> 62#include <net/if_atm.h> 63 64#include <netinet/in.h> 65#include <netinet/if_atm.h> 66#include <netinet/if_ether.h> /* XXX: for ETHERTYPE_* */ 67#if defined(INET) || defined(INET6) 68#include <netinet/in_var.h> 69#endif 70#ifdef NATM 71#include <netnatm/natm.h> 72#endif 73 74/* 75 * Netgraph interface functions. 76 * These need not be protected by a lock, because ng_atm nodes are persitent. 77 * The ng_atm module can be unloaded only if all ATM interfaces have been 78 * unloaded, so nobody should be in the code paths accessing these function 79 * pointers. 80 */ 81void (*ng_atm_attach_p)(struct ifnet *); 82void (*ng_atm_detach_p)(struct ifnet *); 83int (*ng_atm_output_p)(struct ifnet *, struct mbuf **); 84void (*ng_atm_input_p)(struct ifnet *, struct mbuf **, 85 struct atm_pseudohdr *, void *); 86void (*ng_atm_input_orphan_p)(struct ifnet *, struct mbuf *, 87 struct atm_pseudohdr *, void *); 88void (*ng_atm_event_p)(struct ifnet *, uint32_t, void *); 89 90/* 91 * Harp pseudo interface hooks 92 */ 93void (*atm_harp_input_p)(struct ifnet *ifp, struct mbuf **m, 94 struct atm_pseudohdr *ah, void *rxhand); 95void (*atm_harp_attach_p)(struct ifnet *); 96void (*atm_harp_detach_p)(struct ifnet *); 97void (*atm_harp_event_p)(struct ifnet *, uint32_t, void *); 98 99SYSCTL_NODE(_hw, OID_AUTO, atm, CTLFLAG_RW, 0, "ATM hardware"); 100 101MALLOC_DEFINE(M_IFATM, "ifatm", "atm interface internals"); 102 103#ifndef ETHERTYPE_IPV6 104#define ETHERTYPE_IPV6 0x86dd 105#endif 106 107#define senderr(e) do { error = (e); goto bad; } while (0) 108 109/* 110 * atm_output: ATM output routine 111 * inputs: 112 * "ifp" = ATM interface to output to 113 * "m0" = the packet to output 114 * "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI) 115 * "rt0" = the route to use 116 * returns: error code [0 == ok] 117 * 118 * note: special semantic: if (dst == NULL) then we assume "m" already 119 * has an atm_pseudohdr on it and just send it directly. 120 * [for native mode ATM output] if dst is null, then 121 * rt0 must also be NULL. 122 */ 123int 124atm_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst, 125 struct rtentry *rt0) 126{ 127 u_int16_t etype = 0; /* if using LLC/SNAP */ 128 int error = 0, sz; 129 struct atm_pseudohdr atmdst, *ad; 130 struct mbuf *m = m0; 131 struct atmllc *atmllc; 132 struct atmllc *llc_hdr = NULL; 133 u_int32_t atm_flags; 134 135#ifdef MAC 136 error = mac_check_ifnet_transmit(ifp, m); 137 if (error) 138 senderr(error); 139#endif 140 141 if (!((ifp->if_flags & IFF_UP) && 142 (ifp->if_drv_flags & IFF_DRV_RUNNING))) 143 senderr(ENETDOWN); 144 145 /* 146 * check for non-native ATM traffic (dst != NULL) 147 */ 148 if (dst) { 149 switch (dst->sa_family) { 150 151#if defined(INET) || defined(INET6) 152 case AF_INET: 153 case AF_INET6: 154 { 155 struct rtentry *rt; 156 /* 157 * check route 158 */ 159 error = rt_check(&rt, &rt0, dst); 160 if (error) 161 goto bad; 162 RT_UNLOCK(rt); 163 164 if (dst->sa_family == AF_INET6) 165 etype = ETHERTYPE_IPV6; 166 else 167 etype = ETHERTYPE_IP; 168 if (!atmresolve(rt, m, dst, &atmdst)) { 169 m = NULL; 170 /* XXX: atmresolve already free'd it */ 171 senderr(EHOSTUNREACH); 172 /* XXX: put ATMARP stuff here */ 173 /* XXX: watch who frees m on failure */ 174 } 175 } 176 break; 177#endif /* INET || INET6 */ 178 179 case AF_UNSPEC: 180 /* 181 * XXX: bpfwrite. assuming dst contains 12 bytes 182 * (atm pseudo header (4) + LLC/SNAP (8)) 183 */ 184 bcopy(dst->sa_data, &atmdst, sizeof(atmdst)); 185 llc_hdr = (struct atmllc *)(dst->sa_data + 186 sizeof(atmdst)); 187 break; 188 189 default: 190#if (defined(__FreeBSD__) && __FreeBSD_version >= 501113) || \ 191 defined(__NetBSD__) || defined(__OpenBSD__) 192 printf("%s: can't handle af%d\n", ifp->if_xname, 193 dst->sa_family); 194#elif defined(__FreeBSD__) || defined(__bsdi__) 195 printf("%s%d: can't handle af%d\n", ifp->if_name, 196 ifp->if_unit, dst->sa_family); 197#endif 198 senderr(EAFNOSUPPORT); 199 } 200 201 /* 202 * must add atm_pseudohdr to data 203 */ 204 sz = sizeof(atmdst); 205 atm_flags = ATM_PH_FLAGS(&atmdst); 206 if (atm_flags & ATM_PH_LLCSNAP) 207 sz += 8; /* sizeof snap == 8 */ 208 M_PREPEND(m, sz, M_DONTWAIT); 209 if (m == 0) 210 senderr(ENOBUFS); 211 ad = mtod(m, struct atm_pseudohdr *); 212 *ad = atmdst; 213 if (atm_flags & ATM_PH_LLCSNAP) { 214 atmllc = (struct atmllc *)(ad + 1); 215 if (llc_hdr == NULL) { 216 bcopy(ATMLLC_HDR, atmllc->llchdr, 217 sizeof(atmllc->llchdr)); 218 /* note: in host order */ 219 ATM_LLC_SETTYPE(atmllc, etype); 220 } 221 else 222 bcopy(llc_hdr, atmllc, sizeof(struct atmllc)); 223 } 224 } 225 226 if (ng_atm_output_p != NULL) { 227 if ((error = (*ng_atm_output_p)(ifp, &m)) != 0) { 228 if (m != NULL) 229 m_freem(m); 230 return (error); 231 } 232 if (m == NULL) 233 return (0); 234 } 235 236 /* 237 * Queue message on interface, and start output if interface 238 * not yet active. 239 */ 240 if (!IF_HANDOFF_ADJ(&ifp->if_snd, m, ifp, 241 -(int)sizeof(struct atm_pseudohdr))) 242 return (ENOBUFS); 243 return (error); 244 245bad: 246 if (m) 247 m_freem(m); 248 return (error); 249} 250 251/* 252 * Process a received ATM packet; 253 * the packet is in the mbuf chain m. 254 */ 255void 256atm_input(struct ifnet *ifp, struct atm_pseudohdr *ah, struct mbuf *m, 257 void *rxhand) 258{ 259 int isr; 260 u_int16_t etype = ETHERTYPE_IP; /* default */ 261 262 if ((ifp->if_flags & IFF_UP) == 0) { 263 m_freem(m); 264 return; 265 } 266#ifdef MAC 267 mac_create_mbuf_from_ifnet(ifp, m); 268#endif 269 ifp->if_ibytes += m->m_pkthdr.len; 270 271 if (ng_atm_input_p != NULL) { 272 (*ng_atm_input_p)(ifp, &m, ah, rxhand); 273 if (m == NULL) 274 return; 275 } 276 277 /* not eaten by ng_atm. Maybe it's a pseudo-harp PDU? */ 278 if (atm_harp_input_p != NULL) { 279 (*atm_harp_input_p)(ifp, &m, ah, rxhand); 280 if (m == NULL) 281 return; 282 } 283 284 if (rxhand) { 285#ifdef NATM 286 struct natmpcb *npcb; 287 288 /* 289 * XXXRW: this use of 'rxhand' is not a very good idea, and 290 * was subject to races even before SMPng due to the release 291 * of spl here. 292 */ 293 NATM_LOCK(); 294 npcb = rxhand; 295 npcb->npcb_inq++; /* count # in queue */ 296 isr = NETISR_NATM; 297 m->m_pkthdr.rcvif = rxhand; /* XXX: overload */ 298 NATM_UNLOCK(); 299#else 300 printf("atm_input: NATM detected but not " 301 "configured in kernel\n"); 302 goto dropit; 303#endif 304 } else { 305 /* 306 * handle LLC/SNAP header, if present 307 */ 308 if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) { 309 struct atmllc *alc; 310 311 if (m->m_len < sizeof(*alc) && 312 (m = m_pullup(m, sizeof(*alc))) == 0) 313 return; /* failed */ 314 alc = mtod(m, struct atmllc *); 315 if (bcmp(alc, ATMLLC_HDR, 6)) { 316#if (defined(__FreeBSD__) && __FreeBSD_version >= 501113) || \ 317 defined(__NetBSD__) || defined(__OpenBSD__) 318 printf("%s: recv'd invalid LLC/SNAP frame " 319 "[vp=%d,vc=%d]\n", ifp->if_xname, 320 ATM_PH_VPI(ah), ATM_PH_VCI(ah)); 321#elif defined(__FreeBSD__) || defined(__bsdi__) 322 printf("%s%d: recv'd invalid LLC/SNAP frame " 323 "[vp=%d,vc=%d]\n", ifp->if_name, 324 ifp->if_unit, ATM_PH_VPI(ah), 325 ATM_PH_VCI(ah)); 326#endif 327 m_freem(m); 328 return; 329 } 330 etype = ATM_LLC_TYPE(alc); 331 m_adj(m, sizeof(*alc)); 332 } 333 334 switch (etype) { 335 336#ifdef INET 337 case ETHERTYPE_IP: 338 isr = NETISR_IP; 339 break; 340#endif 341 342#ifdef INET6 343 case ETHERTYPE_IPV6: 344 isr = NETISR_IPV6; 345 break; 346#endif 347 default: 348#ifndef NATM 349 dropit: 350#endif 351 if (ng_atm_input_orphan_p != NULL) 352 (*ng_atm_input_orphan_p)(ifp, m, ah, rxhand); 353 else 354 m_freem(m); 355 return; 356 } 357 } 358 netisr_dispatch(isr, m); 359} 360 361/* 362 * Perform common duties while attaching to interface list. 363 */ 364void 365atm_ifattach(struct ifnet *ifp) 366{ 367 struct ifaddr *ifa; 368 struct sockaddr_dl *sdl; 369 struct ifatm *ifatm = ifp->if_l2com; 370 371 ifp->if_addrlen = 0; 372 ifp->if_hdrlen = 0; 373 if_attach(ifp); 374 ifp->if_mtu = ATMMTU; 375 ifp->if_output = atm_output; 376#if 0 377 ifp->if_input = atm_input; 378#endif 379 ifp->if_snd.ifq_maxlen = 50; /* dummy */ 380 381#if defined(__NetBSD__) || defined(__OpenBSD__) 382 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 383#elif defined(__FreeBSD__) && (__FreeBSD__ > 2) 384 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 385 ifa = TAILQ_NEXT(ifa, ifa_link)) 386#elif defined(__FreeBSD__) || defined(__bsdi__) 387 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 388#endif 389 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) && 390 sdl->sdl_family == AF_LINK) { 391 sdl->sdl_type = IFT_ATM; 392 sdl->sdl_alen = ifp->if_addrlen; 393#ifdef notyet /* if using ATMARP, store hardware address using the next line */ 394 bcopy(ifp->hw_addr, LLADDR(sdl), ifp->if_addrlen); 395#endif 396 break; 397 } 398 399 ifp->if_linkmib = &ifatm->mib; 400 ifp->if_linkmiblen = sizeof(ifatm->mib); 401 402 if(ng_atm_attach_p) 403 (*ng_atm_attach_p)(ifp); 404 if (atm_harp_attach_p) 405 (*atm_harp_attach_p)(ifp); 406} 407 408/* 409 * Common stuff for detaching an ATM interface 410 */ 411void 412atm_ifdetach(struct ifnet *ifp) 413{ 414 if (atm_harp_detach_p) 415 (*atm_harp_detach_p)(ifp); 416 if(ng_atm_detach_p) 417 (*ng_atm_detach_p)(ifp); 418 if_detach(ifp); 419} 420 421/* 422 * Support routine for the SIOCATMGVCCS ioctl(). 423 * 424 * This routine assumes, that the private VCC structures used by the driver 425 * begin with a struct atmio_vcc. 426 * 427 * Return a table of VCCs in a freshly allocated memory area. 428 * Here we have a problem: we first count, how many vccs we need 429 * to return. The we allocate the memory and finally fill it in. 430 * Because we cannot lock while calling malloc, the number of active 431 * vccs may change while we're in malloc. So we allocate a couple of 432 * vccs more and if space anyway is not enough re-iterate. 433 * 434 * We could use an sx lock for the vcc tables. 435 */ 436struct atmio_vcctable * 437atm_getvccs(struct atmio_vcc **table, u_int size, u_int start, 438 struct mtx *lock, int waitok) 439{ 440 u_int cid, alloc; 441 size_t len; 442 struct atmio_vcctable *vccs; 443 struct atmio_vcc *v; 444 445 alloc = start + 10; 446 vccs = NULL; 447 448 for (;;) { 449 len = sizeof(*vccs) + alloc * sizeof(vccs->vccs[0]); 450 vccs = reallocf(vccs, len, M_TEMP, 451 waitok ? M_WAITOK : M_NOWAIT); 452 if (vccs == NULL) 453 return (NULL); 454 bzero(vccs, len); 455 456 vccs->count = 0; 457 v = vccs->vccs; 458 459 mtx_lock(lock); 460 for (cid = 0; cid < size; cid++) 461 if (table[cid] != NULL) { 462 if (++vccs->count == alloc) 463 /* too many - try again */ 464 break; 465 *v++ = *table[cid]; 466 } 467 mtx_unlock(lock); 468 469 if (cid == size) 470 break; 471 472 alloc *= 2; 473 } 474 return (vccs); 475} 476 477/* 478 * Driver or channel state has changed. Inform whoever is interested 479 * in these events. 480 */ 481void 482atm_event(struct ifnet *ifp, u_int event, void *arg) 483{ 484 if (ng_atm_event_p != NULL) 485 (*ng_atm_event_p)(ifp, event, arg); 486 if (atm_harp_event_p != NULL) 487 (*atm_harp_event_p)(ifp, event, arg); 488} 489 490static void * 491atm_alloc(u_char type, struct ifnet *ifp) 492{ 493 struct ifatm *ifatm; 494 495 ifatm = malloc(sizeof(struct ifatm), M_IFATM, M_WAITOK | M_ZERO); 496 ifatm->ifp = ifp; 497 498 return (ifatm); 499} 500 501static void 502atm_free(void *com, u_char type) 503{ 504 505 free(com, M_IFATM); 506} 507 508static int 509atm_modevent(module_t mod, int type, void *data) 510{ 511 switch (type) { 512 case MOD_LOAD: 513 if_register_com_alloc(IFT_ATM, atm_alloc, atm_free); 514 break; 515 case MOD_UNLOAD: 516 if_deregister_com_alloc(IFT_ATM); 517 break; 518 default: 519 return (EOPNOTSUPP); 520 } 521 522 return (0); 523} 524 525static moduledata_t atm_mod = { 526 "atm", 527 atm_modevent, 528 0 529}; 530 531DECLARE_MODULE(atm, atm_mod, SI_SUB_INIT_IF, SI_ORDER_ANY); 532MODULE_VERSION(atm, 1); 533