1 /*      $OpenBSD: if_atmsubr.c,v 1.24 2004/04/17 04:19:33 henning 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 
35 /*
36  *	@(#)COPYRIGHT	1.1 (NRL) January 1995
37  *
38  * NRL grants permission for redistribution and use in source and binary
39  * forms, with or without modification, of the software and documentation
40  * created at NRL provided that the following conditions are met:
41  *
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgements:
49  * 	This product includes software developed by the University of
50  * 	California, Berkeley and its contributors.
51  * 	This product includes software developed at the Information
52  * 	Technology Division, US Naval Research Laboratory.
53  * 4. Neither the name of the NRL nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
58  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
59  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
60  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
61  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
62  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
63  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
64  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
65  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
66  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
67  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68  *
69  * The views and conclusions contained in the software and documentation
70  * are those of the authors and should not be interpreted as representing
71  * official policies, either expressed or implied, of the US Naval
72  * Research Laboratory (NRL).
73  */
74 
75 /*
76  * if_atmsubr.c
77  */
78 
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/malloc.h>
83 #include <sys/mbuf.h>
84 #include <sys/protosw.h>
85 #include <sys/socket.h>
86 #include <sys/ioctl.h>
87 #include <sys/errno.h>
88 #include <sys/syslog.h>
89 
90 #include <machine/cpu.h>
91 
92 #include <net/if.h>
93 #include <net/netisr.h>
94 #include <net/route.h>
95 #include <net/if_dl.h>
96 #include <net/if_types.h>
97 #include <net/if_atm.h>
98 
99 #include <netinet/in.h>
100 #include <netinet/if_atm.h>
101 #include <netinet/if_ether.h> /* XXX: for ETHERTYPE_* */
102 #if defined(INET) || defined(INET6)
103 #include <netinet/in_var.h>
104 #endif
105 #ifdef NATM
106 #include <netnatm/natm.h>
107 #endif
108 
109 #ifdef INET6
110 #include <netinet6/in6_var.h>
111 #endif /* INET6 */
112 
113 #define senderr(e) { error = (e); goto bad;}
114 
115 /*
116  * atm_output: ATM output routine
117  *   inputs:
118  *     "ifp" = ATM interface to output to
119  *     "m0" = the packet to output
120  *     "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI)
121  *     "rt0" = the route to use
122  *   returns: error code   [0 == ok]
123  *
124  *   note: special semantic: if (dst == NULL) then we assume "m" already
125  *		has an atm_pseudohdr on it and just send it directly.
126  *		[for native mode ATM output]   if dst is null, then
127  *		rt0 must also be NULL.
128  */
129 
130 int
atm_output(ifp,m0,dst,rt0)131 atm_output(ifp, m0, dst, rt0)
132 	struct ifnet *ifp;
133 	struct mbuf *m0;
134 	struct sockaddr *dst;
135 	struct rtentry *rt0;
136 {
137 	u_int16_t etype = 0;			/* if using LLC/SNAP */
138 	int s, error = 0, sz, len;
139 	struct atm_pseudohdr atmdst, *ad;
140 	struct mbuf *m = m0;
141 	struct rtentry *rt;
142 	struct atmllc *atmllc;
143 	u_int32_t atm_flags;
144 
145 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
146 		senderr(ENETDOWN);
147 
148 	/*
149 	 * check route
150 	 */
151 	if ((rt = rt0) != NULL) {
152 
153 		if ((rt->rt_flags & RTF_UP) == 0) { /* route went down! */
154 			if ((rt0 = rt = RTALLOC1(dst, 0)) != NULL)
155 				rt->rt_refcnt--;
156 			else
157 				senderr(EHOSTUNREACH);
158 		}
159 
160 		if (rt->rt_flags & RTF_GATEWAY) {
161 			if (rt->rt_gwroute == 0)
162 				goto lookup;
163 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
164 				rtfree(rt); rt = rt0;
165 			lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 0);
166 				if ((rt = rt->rt_gwroute) == 0)
167 					senderr(EHOSTUNREACH);
168 			}
169 		}
170 
171 		/* XXX: put RTF_REJECT code here if doing ATMARP */
172 
173 	}
174 
175 	/*
176 	 * check for non-native ATM traffic   (dst != NULL)
177 	 */
178 	if (dst) {
179 		switch (dst->sa_family) {
180 #ifdef INET
181 		case AF_INET:
182 #endif
183 #ifdef INET6
184 		case AF_INET6:
185 #endif
186 #if defined(INET) || defined(INET6)
187 			if (dst->sa_family == AF_INET)
188 				etype = ETHERTYPE_IP;
189 			else
190 				etype = ETHERTYPE_IPV6;
191 			if (!atmresolve(rt, m, dst, &atmdst)) {
192 				m = NULL;
193 				/* XXX: atmresolve already free'd it */
194 				senderr(EHOSTUNREACH);
195 				/* XXX: put ATMARP stuff here */
196 				/* XXX: watch who frees m on failure */
197 			}
198 			break;
199 #endif
200 #if 0	/*NRL INET6*/
201 		case AF_INET6:
202 			/*
203 			 * The bottom line here is to either queue the
204 			 * outgoing packet in the discovery engine, or fill
205 			 * in edst with something that'll work.
206 			 */
207 			if (m->m_flags & M_MCAST) {
208 				/*
209 				 * If multicast dest., then use IPv6 -> Ethernet
210 				 * mcast mapping.  Really simple.
211 				 */
212 				ETHER_MAP_IN6_MULTICAST(
213 				    ((struct sockaddr_in6 *)dst)->sin6_addr,
214 				    edst);
215 			} else {
216 				/* Do unicast neighbor discovery stuff. */
217 				if (!ipv6_discov_resolve(ifp, rt, m, dst, edst))
218 	 				return 0;
219 			}
220 			type = htons(ETHERTYPE_IPV6);
221 			break;
222 #endif /* INET6 */
223 
224 		default:
225 #if defined(__NetBSD__) || defined(__OpenBSD__)
226 			printf("%s: can't handle af%d\n", ifp->if_xname,
227 			       dst->sa_family);
228 #elif defined(__FreeBSD__) || defined(__bsdi__)
229 			printf("%s%d: can't handle af%d\n", ifp->if_name,
230 			       ifp->if_unit, dst->sa_family);
231 #endif
232 			senderr(EAFNOSUPPORT);
233 		}
234 
235 		/*
236 		 * must add atm_pseudohdr to data
237 		 */
238 		sz = sizeof(atmdst);
239 		atm_flags = ATM_PH_FLAGS(&atmdst);
240 		if (atm_flags & ATM_PH_LLCSNAP) sz += 8; /* sizeof snap == 8 */
241 		M_PREPEND(m, sz, M_DONTWAIT);
242 		if (m == 0)
243 			senderr(ENOBUFS);
244 		ad = mtod(m, struct atm_pseudohdr *);
245 		*ad = atmdst;
246 		if (atm_flags & ATM_PH_LLCSNAP) {
247 			atmllc = (struct atmllc *)(ad + 1);
248 			bcopy(ATMLLC_HDR, atmllc->llchdr,
249 						sizeof(atmllc->llchdr));
250 			ATM_LLC_SETTYPE(atmllc, etype);
251 		}
252 	}
253 
254 	/*
255 	 * Queue message on interface, and start output if interface
256 	 * not yet active.
257 	 */
258 	len = m->m_pkthdr.len;
259 	s = splimp();
260 	IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
261 	if (error) {
262 		splx(s);
263 		return (error);
264 	}
265 	ifp->if_obytes += len;
266 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
267 		(*ifp->if_start)(ifp);
268 	splx(s);
269 	return (error);
270 
271 bad:
272 	if (m)
273 		m_freem(m);
274 	return (error);
275 }
276 
277 /*
278  * Process a received ATM packet;
279  * the packet is in the mbuf chain m.
280  */
281 void
atm_input(ifp,ah,m,rxhand)282 atm_input(ifp, ah, m, rxhand)
283 	struct ifnet *ifp;
284 	struct atm_pseudohdr *ah;
285 	struct mbuf *m;
286 	void *rxhand;
287 {
288 	struct ifqueue *inq;
289 	u_int16_t etype = ETHERTYPE_IP; /* default */
290 	int s;
291 
292 	if ((ifp->if_flags & IFF_UP) == 0) {
293 		m_freem(m);
294 		return;
295 	}
296 	ifp->if_ibytes += m->m_pkthdr.len;
297 
298 	if (rxhand) {
299 #ifdef NATM
300 	  struct natmpcb *npcb = rxhand;
301 	  s = splimp();			/* in case 2 atm cards @ diff lvls */
302 	  npcb->npcb_inq++;			/* count # in queue */
303 	  splx(s);
304 	  schednetisr(NETISR_NATM);
305 	  inq = &natmintrq;
306 	  m->m_pkthdr.rcvif = rxhand; /* XXX: overload */
307 #else
308 	  printf("atm_input: NATM detected but not configured in kernel\n");
309 	  m_freem(m);
310 	  return;
311 #endif
312 	} else {
313 	  /*
314 	   * handle LLC/SNAP header, if present
315 	   */
316 	  if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) {
317 	    struct atmllc *alc;
318 	    if (m->m_len < sizeof(*alc) &&
319 		(m = m_pullup(m, sizeof(*alc))) == NULL)
320 		  return; /* failed */
321 	    alc = mtod(m, struct atmllc *);
322 	    if (bcmp(alc, ATMLLC_HDR, 6)) {
323 #if defined(__NetBSD__) || defined(__OpenBSD__)
324 	      printf("%s: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
325 		  ifp->if_xname, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
326 #elif defined(__FreeBSD__) || defined(__bsdi__)
327 	      printf("%s%d: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
328 		  ifp->if_name, ifp->if_unit, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
329 #endif
330 	      m_freem(m);
331               return;
332 	    }
333 	    etype = ATM_LLC_TYPE(alc);
334 	    m_adj(m, sizeof(*alc));
335 	  }
336 
337 	  switch (etype) {
338 #ifdef INET
339 	  case ETHERTYPE_IP:
340 		  schednetisr(NETISR_IP);
341 		  inq = &ipintrq;
342 		  break;
343 #endif /* INET */
344 #ifdef INET6
345 	  case ETHERTYPE_IPV6:
346 		  schednetisr(NETISR_IPV6);
347 		  inq = &ip6intrq;
348 		  break;
349 #endif
350 	  default:
351 	      m_freem(m);
352 	      return;
353 	  }
354 	}
355 
356 	s = splimp();
357 	IF_INPUT_ENQUEUE(inq, m);
358 	splx(s);
359 }
360 
361 /*
362  * Perform common duties while attaching to interface list
363  */
364 void
atm_ifattach(ifp)365 atm_ifattach(ifp)
366 	struct ifnet *ifp;
367 {
368 
369 	ifp->if_type = IFT_ATM;
370 	ifp->if_addrlen = 0;
371 	ifp->if_hdrlen = 0;
372 	ifp->if_mtu = ATMMTU;
373 	ifp->if_output = atm_output;
374 
375 	if_alloc_sadl(ifp);
376 #ifdef notyet /* if using ATMARP, store hardware address using the next line */
377 	bcopy(ifp->hw_addr, LLADDR(ifp->if_sadl), ifp->if_addrlen);
378 #endif
379 }
380