if_atmsubr.c revision 78249
1215976Sjmallett/* $NetBSD: if_atmsubr.c,v 1.10 1997/03/11 23:19:51 chuck Exp $ */ 2215976Sjmallett 3215976Sjmallett/* 4215976Sjmallett * 5215976Sjmallett * Copyright (c) 1996 Charles D. Cranor and Washington University. 6215976Sjmallett * All rights reserved. 7215976Sjmallett * 8215976Sjmallett * Redistribution and use in source and binary forms, with or without 9215976Sjmallett * modification, are permitted provided that the following conditions 10215976Sjmallett * are met: 11215976Sjmallett * 1. Redistributions of source code must retain the above copyright 12215976Sjmallett * notice, this list of conditions and the following disclaimer. 13215976Sjmallett * 2. Redistributions in binary form must reproduce the above copyright 14215976Sjmallett * notice, this list of conditions and the following disclaimer in the 15215976Sjmallett * documentation and/or other materials provided with the distribution. 16215976Sjmallett * 3. All advertising materials mentioning features or use of this software 17215976Sjmallett * must display the following acknowledgement: 18215976Sjmallett * This product includes software developed by Charles D. Cranor and 19215976Sjmallett * Washington University. 20215976Sjmallett * 4. The name of the author may not be used to endorse or promote products 21215976Sjmallett * derived from this software without specific prior written permission. 22215976Sjmallett * 23215976Sjmallett * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24215976Sjmallett * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25215976Sjmallett * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26215976Sjmallett * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27215976Sjmallett * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28215976Sjmallett * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29215976Sjmallett * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30215976Sjmallett * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31215976Sjmallett * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32215976Sjmallett * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33215976Sjmallett * 34215976Sjmallett * $FreeBSD: head/sys/net/if_atmsubr.c 78249 2001-06-15 07:32:25Z peter $ 35215976Sjmallett */ 36215976Sjmallett 37215976Sjmallett/* 38215976Sjmallett * if_atmsubr.c 39215976Sjmallett */ 40215976Sjmallett 41215976Sjmallett#include "opt_inet.h" 42215976Sjmallett#include "opt_inet6.h" 43215976Sjmallett#include "opt_natm.h" 44215976Sjmallett 45215976Sjmallett#include <sys/param.h> 46215976Sjmallett#include <sys/systm.h> 47215976Sjmallett#include <sys/mbuf.h> 48215976Sjmallett#include <sys/socket.h> 49215976Sjmallett#include <sys/sockio.h> 50215976Sjmallett#include <sys/errno.h> 51215976Sjmallett 52215976Sjmallett#include <net/if.h> 53215976Sjmallett#include <net/netisr.h> 54215976Sjmallett#include <net/route.h> 55215976Sjmallett#include <net/if_dl.h> 56215976Sjmallett#include <net/if_types.h> 57215976Sjmallett#include <net/if_atm.h> 58215976Sjmallett 59215976Sjmallett#include <netinet/in.h> 60215976Sjmallett#include <netinet/if_atm.h> 61215976Sjmallett#include <netinet/if_ether.h> /* XXX: for ETHERTYPE_* */ 62215976Sjmallett#if defined(INET) || defined(INET6) 63215976Sjmallett#include <netinet/in_var.h> 64215976Sjmallett#endif 65215976Sjmallett#ifdef NATM 66215976Sjmallett#include <netnatm/natm.h> 67215976Sjmallett#endif 68215976Sjmallett 69215976Sjmallett#ifndef ETHERTYPE_IPV6 70215976Sjmallett#define ETHERTYPE_IPV6 0x86dd 71215976Sjmallett#endif 72215976Sjmallett 73215976Sjmallett#define senderr(e) { error = (e); goto bad;} 74215976Sjmallett 75215976Sjmallett/* 76215976Sjmallett * atm_output: ATM output routine 77215976Sjmallett * inputs: 78215976Sjmallett * "ifp" = ATM interface to output to 79215976Sjmallett * "m0" = the packet to output 80215976Sjmallett * "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI) 81215976Sjmallett * "rt0" = the route to use 82215976Sjmallett * returns: error code [0 == ok] 83215976Sjmallett * 84215976Sjmallett * note: special semantic: if (dst == NULL) then we assume "m" already 85215976Sjmallett * has an atm_pseudohdr on it and just send it directly. 86215976Sjmallett * [for native mode ATM output] if dst is null, then 87215976Sjmallett * rt0 must also be NULL. 88215976Sjmallett */ 89215976Sjmallett 90215976Sjmallettint 91215976Sjmallettatm_output(ifp, m0, dst, rt0) 92215976Sjmallett register struct ifnet *ifp; 93215976Sjmallett struct mbuf *m0; 94215976Sjmallett struct sockaddr *dst; 95215976Sjmallett struct rtentry *rt0; 96215976Sjmallett{ 97215976Sjmallett u_int16_t etype = 0; /* if using LLC/SNAP */ 98215976Sjmallett int error = 0, sz; 99215976Sjmallett struct atm_pseudohdr atmdst, *ad; 100215976Sjmallett struct mbuf *m = m0; 101215976Sjmallett struct rtentry *rt; 102215976Sjmallett struct atmllc *atmllc; 103215976Sjmallett struct atmllc *llc_hdr = NULL; 104215976Sjmallett u_int32_t atm_flags; 105215976Sjmallett 106215976Sjmallett if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 107215976Sjmallett senderr(ENETDOWN); 108 109 /* 110 * check route 111 */ 112 if ((rt = rt0) != NULL) { 113 114 if ((rt->rt_flags & RTF_UP) == 0) { /* route went down! */ 115 if ((rt0 = rt = RTALLOC1(dst, 0)) != NULL) 116 rt->rt_refcnt--; 117 else 118 senderr(EHOSTUNREACH); 119 } 120 121 if (rt->rt_flags & RTF_GATEWAY) { 122 if (rt->rt_gwroute == 0) 123 goto lookup; 124 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 125 rtfree(rt); rt = rt0; 126 lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 0); 127 if ((rt = rt->rt_gwroute) == 0) 128 senderr(EHOSTUNREACH); 129 } 130 } 131 132 /* XXX: put RTF_REJECT code here if doing ATMARP */ 133 134 } 135 136 /* 137 * check for non-native ATM traffic (dst != NULL) 138 */ 139 if (dst) { 140 switch (dst->sa_family) { 141#if defined(INET) || defined(INET6) 142 case AF_INET: 143 case AF_INET6: 144 if (dst->sa_family == AF_INET6) 145 etype = htons(ETHERTYPE_IPV6); 146 else 147 etype = htons(ETHERTYPE_IP); 148 if (!atmresolve(rt, m, dst, &atmdst)) { 149 m = NULL; 150 /* XXX: atmresolve already free'd it */ 151 senderr(EHOSTUNREACH); 152 /* XXX: put ATMARP stuff here */ 153 /* XXX: watch who frees m on failure */ 154 } 155 break; 156#endif /* INET || INET6 */ 157 158 case AF_UNSPEC: 159 /* 160 * XXX: bpfwrite. assuming dst contains 12 bytes 161 * (atm pseudo header (4) + LLC/SNAP (8)) 162 */ 163 bcopy(dst->sa_data, &atmdst, sizeof(atmdst)); 164 llc_hdr = (struct atmllc *)(dst->sa_data + sizeof(atmdst)); 165 break; 166 167 default: 168#if defined(__NetBSD__) || defined(__OpenBSD__) 169 printf("%s: can't handle af%d\n", ifp->if_xname, 170 dst->sa_family); 171#elif defined(__FreeBSD__) || defined(__bsdi__) 172 printf("%s%d: can't handle af%d\n", ifp->if_name, 173 ifp->if_unit, dst->sa_family); 174#endif 175 senderr(EAFNOSUPPORT); 176 } 177 178 /* 179 * must add atm_pseudohdr to data 180 */ 181 sz = sizeof(atmdst); 182 atm_flags = ATM_PH_FLAGS(&atmdst); 183 if (atm_flags & ATM_PH_LLCSNAP) sz += 8; /* sizeof snap == 8 */ 184 M_PREPEND(m, sz, M_DONTWAIT); 185 if (m == 0) 186 senderr(ENOBUFS); 187 ad = mtod(m, struct atm_pseudohdr *); 188 *ad = atmdst; 189 if (atm_flags & ATM_PH_LLCSNAP) { 190 atmllc = (struct atmllc *)(ad + 1); 191 if (llc_hdr == NULL) { 192 bcopy(ATMLLC_HDR, atmllc->llchdr, 193 sizeof(atmllc->llchdr)); 194 ATM_LLC_SETTYPE(atmllc, etype); 195 /* note: already in network order */ 196 } 197 else 198 bcopy(llc_hdr, atmllc, sizeof(struct atmllc)); 199 } 200 } 201 202 /* 203 * Queue message on interface, and start output if interface 204 * not yet active. 205 */ 206 if (! IF_HANDOFF(&ifp->if_snd, m, ifp)) 207 return (ENOBUFS); 208 return (error); 209 210bad: 211 if (m) 212 m_freem(m); 213 return (error); 214} 215 216/* 217 * Process a received ATM packet; 218 * the packet is in the mbuf chain m. 219 */ 220void 221atm_input(ifp, ah, m, rxhand) 222 struct ifnet *ifp; 223 register struct atm_pseudohdr *ah; 224 struct mbuf *m; 225 void *rxhand; 226{ 227 register struct ifqueue *inq; 228 u_int16_t etype = ETHERTYPE_IP; /* default */ 229 int s; 230 231 if ((ifp->if_flags & IFF_UP) == 0) { 232 m_freem(m); 233 return; 234 } 235 ifp->if_ibytes += m->m_pkthdr.len; 236 237 if (rxhand) { 238#ifdef NATM 239 struct natmpcb *npcb = rxhand; 240 s = splimp(); /* in case 2 atm cards @ diff lvls */ 241 npcb->npcb_inq++; /* count # in queue */ 242 splx(s); 243 schednetisr(NETISR_NATM); 244 inq = &natmintrq; 245 m->m_pkthdr.rcvif = rxhand; /* XXX: overload */ 246#else 247 printf("atm_input: NATM detected but not configured in kernel\n"); 248 m_freem(m); 249 return; 250#endif 251 } else { 252 /* 253 * handle LLC/SNAP header, if present 254 */ 255 if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) { 256 struct atmllc *alc; 257 if (m->m_len < sizeof(*alc) && 258 (m = m_pullup(m, sizeof(*alc))) == 0) 259 return; /* failed */ 260 alc = mtod(m, struct atmllc *); 261 if (bcmp(alc, ATMLLC_HDR, 6)) { 262#if defined(__NetBSD__) || defined(__OpenBSD__) 263 printf("%s: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n", 264 ifp->if_xname, ATM_PH_VPI(ah), ATM_PH_VCI(ah)); 265#elif defined(__FreeBSD__) || defined(__bsdi__) 266 printf("%s%d: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n", 267 ifp->if_name, ifp->if_unit, ATM_PH_VPI(ah), ATM_PH_VCI(ah)); 268#endif 269 m_freem(m); 270 return; 271 } 272 etype = ATM_LLC_TYPE(alc); 273 m_adj(m, sizeof(*alc)); 274 } 275 276 switch (etype) { 277#ifdef INET 278 case ETHERTYPE_IP: 279 schednetisr(NETISR_IP); 280 inq = &ipintrq; 281 break; 282#endif 283#ifdef INET6 284 case ETHERTYPE_IPV6: 285 schednetisr(NETISR_IPV6); 286 inq = &ip6intrq; 287 break; 288#endif 289 default: 290 m_freem(m); 291 return; 292 } 293 } 294 295 (void) IF_HANDOFF(inq, m, NULL); 296} 297 298/* 299 * Perform common duties while attaching to interface list 300 */ 301void 302atm_ifattach(ifp) 303 register struct ifnet *ifp; 304{ 305 register struct ifaddr *ifa; 306 register struct sockaddr_dl *sdl; 307 308 ifp->if_type = IFT_ATM; 309 ifp->if_addrlen = 0; 310 ifp->if_hdrlen = 0; 311 ifp->if_mtu = ATMMTU; 312 ifp->if_output = atm_output; 313 ifp->if_snd.ifq_maxlen = 50; /* dummy */ 314 315#if defined(__NetBSD__) || defined(__OpenBSD__) 316 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 317#elif defined(__FreeBSD__) && (__FreeBSD__ > 2) 318 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 319 ifa = TAILQ_NEXT(ifa, ifa_link)) 320#elif defined(__FreeBSD__) || defined(__bsdi__) 321 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 322#endif 323 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) && 324 sdl->sdl_family == AF_LINK) { 325 sdl->sdl_type = IFT_ATM; 326 sdl->sdl_alen = ifp->if_addrlen; 327#ifdef notyet /* if using ATMARP, store hardware address using the next line */ 328 bcopy(ifp->hw_addr, LLADDR(sdl), ifp->if_addrlen); 329#endif 330 break; 331 } 332 333} 334