if_atmsubr.c revision 105576
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 * $FreeBSD: head/sys/net/if_atmsubr.c 105576 2002-10-20 22:20:48Z rwatson $ 35 */ 36 37/* 38 * if_atmsubr.c 39 */ 40 41#include "opt_inet.h" 42#include "opt_inet6.h" 43#include "opt_mac.h" 44#include "opt_natm.h" 45 46#include <sys/param.h> 47#include <sys/systm.h> 48#include <sys/mac.h> 49#include <sys/mbuf.h> 50#include <sys/socket.h> 51#include <sys/sockio.h> 52#include <sys/errno.h> 53 54#include <net/if.h> 55#include <net/netisr.h> 56#include <net/route.h> 57#include <net/if_dl.h> 58#include <net/if_types.h> 59#include <net/if_atm.h> 60 61#include <netinet/in.h> 62#include <netinet/if_atm.h> 63#include <netinet/if_ether.h> /* XXX: for ETHERTYPE_* */ 64#if defined(INET) || defined(INET6) 65#include <netinet/in_var.h> 66#endif 67#ifdef NATM 68#include <netnatm/natm.h> 69#endif 70 71#ifndef ETHERTYPE_IPV6 72#define ETHERTYPE_IPV6 0x86dd 73#endif 74 75#define senderr(e) { error = (e); goto bad;} 76 77/* 78 * atm_output: ATM output routine 79 * inputs: 80 * "ifp" = ATM interface to output to 81 * "m0" = the packet to output 82 * "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI) 83 * "rt0" = the route to use 84 * returns: error code [0 == ok] 85 * 86 * note: special semantic: if (dst == NULL) then we assume "m" already 87 * has an atm_pseudohdr on it and just send it directly. 88 * [for native mode ATM output] if dst is null, then 89 * rt0 must also be NULL. 90 */ 91 92int 93atm_output(ifp, m0, dst, rt0) 94 register struct ifnet *ifp; 95 struct mbuf *m0; 96 struct sockaddr *dst; 97 struct rtentry *rt0; 98{ 99 u_int16_t etype = 0; /* if using LLC/SNAP */ 100 int error = 0, sz; 101 struct atm_pseudohdr atmdst, *ad; 102 struct mbuf *m = m0; 103 struct rtentry *rt; 104 struct atmllc *atmllc; 105 struct atmllc *llc_hdr = NULL; 106 u_int32_t atm_flags; 107 108#ifdef MAC 109 error = mac_check_ifnet_transmit(ifp, m); 110 if (error) 111 senderr(error); 112#endif 113 114 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 115 senderr(ENETDOWN); 116 117 /* 118 * check route 119 */ 120 if ((rt = rt0) != NULL) { 121 122 if ((rt->rt_flags & RTF_UP) == 0) { /* route went down! */ 123 if ((rt0 = rt = RTALLOC1(dst, 0)) != NULL) 124 rt->rt_refcnt--; 125 else 126 senderr(EHOSTUNREACH); 127 } 128 129 if (rt->rt_flags & RTF_GATEWAY) { 130 if (rt->rt_gwroute == 0) 131 goto lookup; 132 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 133 rtfree(rt); rt = rt0; 134 lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 0); 135 if ((rt = rt->rt_gwroute) == 0) 136 senderr(EHOSTUNREACH); 137 } 138 } 139 140 /* XXX: put RTF_REJECT code here if doing ATMARP */ 141 142 } 143 144 /* 145 * check for non-native ATM traffic (dst != NULL) 146 */ 147 if (dst) { 148 switch (dst->sa_family) { 149#if defined(INET) || defined(INET6) 150 case AF_INET: 151 case AF_INET6: 152 if (dst->sa_family == AF_INET6) 153 etype = htons(ETHERTYPE_IPV6); 154 else 155 etype = htons(ETHERTYPE_IP); 156 if (!atmresolve(rt, m, dst, &atmdst)) { 157 m = NULL; 158 /* XXX: atmresolve already free'd it */ 159 senderr(EHOSTUNREACH); 160 /* XXX: put ATMARP stuff here */ 161 /* XXX: watch who frees m on failure */ 162 } 163 break; 164#endif /* INET || INET6 */ 165 166 case AF_UNSPEC: 167 /* 168 * XXX: bpfwrite. assuming dst contains 12 bytes 169 * (atm pseudo header (4) + LLC/SNAP (8)) 170 */ 171 bcopy(dst->sa_data, &atmdst, sizeof(atmdst)); 172 llc_hdr = (struct atmllc *)(dst->sa_data + sizeof(atmdst)); 173 break; 174 175 default: 176#if defined(__NetBSD__) || defined(__OpenBSD__) 177 printf("%s: can't handle af%d\n", ifp->if_xname, 178 dst->sa_family); 179#elif defined(__FreeBSD__) || defined(__bsdi__) 180 printf("%s%d: can't handle af%d\n", ifp->if_name, 181 ifp->if_unit, dst->sa_family); 182#endif 183 senderr(EAFNOSUPPORT); 184 } 185 186 /* 187 * must add atm_pseudohdr to data 188 */ 189 sz = sizeof(atmdst); 190 atm_flags = ATM_PH_FLAGS(&atmdst); 191 if (atm_flags & ATM_PH_LLCSNAP) sz += 8; /* sizeof snap == 8 */ 192 M_PREPEND(m, sz, M_DONTWAIT); 193 if (m == 0) 194 senderr(ENOBUFS); 195 ad = mtod(m, struct atm_pseudohdr *); 196 *ad = atmdst; 197 if (atm_flags & ATM_PH_LLCSNAP) { 198 atmllc = (struct atmllc *)(ad + 1); 199 if (llc_hdr == NULL) { 200 bcopy(ATMLLC_HDR, atmllc->llchdr, 201 sizeof(atmllc->llchdr)); 202 ATM_LLC_SETTYPE(atmllc, etype); 203 /* note: already in network order */ 204 } 205 else 206 bcopy(llc_hdr, atmllc, sizeof(struct atmllc)); 207 } 208 } 209 210 /* 211 * Queue message on interface, and start output if interface 212 * not yet active. 213 */ 214 if (! IF_HANDOFF(&ifp->if_snd, m, ifp)) 215 return (ENOBUFS); 216 return (error); 217 218bad: 219 if (m) 220 m_freem(m); 221 return (error); 222} 223 224/* 225 * Process a received ATM packet; 226 * the packet is in the mbuf chain m. 227 */ 228void 229atm_input(ifp, ah, m, rxhand) 230 struct ifnet *ifp; 231 register struct atm_pseudohdr *ah; 232 struct mbuf *m; 233 void *rxhand; 234{ 235 register struct ifqueue *inq; 236 u_int16_t etype = ETHERTYPE_IP; /* default */ 237 int s; 238 239 if ((ifp->if_flags & IFF_UP) == 0) { 240 m_freem(m); 241 return; 242 } 243#ifdef MAC 244 mac_create_mbuf_from_ifnet(ifp, m); 245#endif 246 ifp->if_ibytes += m->m_pkthdr.len; 247 248 if (rxhand) { 249#ifdef NATM 250 struct natmpcb *npcb = rxhand; 251 s = splimp(); /* in case 2 atm cards @ diff lvls */ 252 npcb->npcb_inq++; /* count # in queue */ 253 splx(s); 254 schednetisr(NETISR_NATM); 255 inq = &natmintrq; 256 m->m_pkthdr.rcvif = rxhand; /* XXX: overload */ 257#else 258 printf("atm_input: NATM detected but not configured in kernel\n"); 259 m_freem(m); 260 return; 261#endif 262 } else { 263 /* 264 * handle LLC/SNAP header, if present 265 */ 266 if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) { 267 struct atmllc *alc; 268 if (m->m_len < sizeof(*alc) && 269 (m = m_pullup(m, sizeof(*alc))) == 0) 270 return; /* failed */ 271 alc = mtod(m, struct atmllc *); 272 if (bcmp(alc, ATMLLC_HDR, 6)) { 273#if defined(__NetBSD__) || defined(__OpenBSD__) 274 printf("%s: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n", 275 ifp->if_xname, ATM_PH_VPI(ah), ATM_PH_VCI(ah)); 276#elif defined(__FreeBSD__) || defined(__bsdi__) 277 printf("%s%d: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n", 278 ifp->if_name, ifp->if_unit, ATM_PH_VPI(ah), ATM_PH_VCI(ah)); 279#endif 280 m_freem(m); 281 return; 282 } 283 etype = ATM_LLC_TYPE(alc); 284 m_adj(m, sizeof(*alc)); 285 } 286 287 switch (etype) { 288#ifdef INET 289 case ETHERTYPE_IP: 290 schednetisr(NETISR_IP); 291 inq = &ipintrq; 292 break; 293#endif 294#ifdef INET6 295 case ETHERTYPE_IPV6: 296 schednetisr(NETISR_IPV6); 297 inq = &ip6intrq; 298 break; 299#endif 300 default: 301 m_freem(m); 302 return; 303 } 304 } 305 306 (void) IF_HANDOFF(inq, m, NULL); 307} 308 309/* 310 * Perform common duties while attaching to interface list 311 */ 312void 313atm_ifattach(ifp) 314 register struct ifnet *ifp; 315{ 316 register struct ifaddr *ifa; 317 register struct sockaddr_dl *sdl; 318 319 ifp->if_type = IFT_ATM; 320 ifp->if_addrlen = 0; 321 ifp->if_hdrlen = 0; 322 ifp->if_mtu = ATMMTU; 323 ifp->if_output = atm_output; 324 ifp->if_snd.ifq_maxlen = 50; /* dummy */ 325 326#if defined(__NetBSD__) || defined(__OpenBSD__) 327 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 328#elif defined(__FreeBSD__) && (__FreeBSD__ > 2) 329 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 330 ifa = TAILQ_NEXT(ifa, ifa_link)) 331#elif defined(__FreeBSD__) || defined(__bsdi__) 332 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 333#endif 334 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) && 335 sdl->sdl_family == AF_LINK) { 336 sdl->sdl_type = IFT_ATM; 337 sdl->sdl_alen = ifp->if_addrlen; 338#ifdef notyet /* if using ATMARP, store hardware address using the next line */ 339 bcopy(ifp->hw_addr, LLADDR(sdl), ifp->if_addrlen); 340#endif 341 break; 342 } 343 344} 345