if_atmsubr.c revision 27845
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
35/*
36 * if_atmsubr.c
37 */
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/mbuf.h>
43#include <sys/socket.h>
44
45#include <net/if.h>
46#include <net/netisr.h>
47#include <net/route.h>
48#include <net/if_dl.h>
49#include <net/if_types.h>
50#include <net/if_atm.h>
51
52#include <netinet/in.h>
53#include <netinet/if_atm.h>
54#include <netinet/if_ether.h> /* XXX: for ETHERTYPE_* */
55#ifdef INET
56#include <netinet/in_var.h>
57#endif
58#ifdef NATM
59#include <netnatm/natm.h>
60#endif
61
62#include "bpfilter.h"
63#if NBPFILTER > 0
64/*
65 * bpf support.
66 * the code is derived from if_loop.c.
67 * bpf support should belong to the driver but it's easier to implement
68 * it here since we can call bpf_mtap before atm_output adds a pseudo
69 * header to the mbuf.
70 *			--kjc
71 */
72#include <net/bpf.h>
73#endif /* NBPFILTER > 0 */
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 s, error = 0, sz;
101	struct atm_pseudohdr atmdst, *ad;
102	register struct mbuf *m = m0;
103	register struct rtentry *rt;
104	struct atmllc *atmllc;
105	u_int32_t atm_flags;
106
107	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
108		senderr(ENETDOWN);
109	ifp->if_lastchange = time;
110
111	/*
112	 * check route
113	 */
114	if ((rt = rt0) != NULL) {
115
116		if ((rt->rt_flags & RTF_UP) == 0) { /* route went down! */
117			if ((rt0 = rt = RTALLOC1(dst, 0)) != NULL)
118				rt->rt_refcnt--;
119			else
120				senderr(EHOSTUNREACH);
121		}
122
123		if (rt->rt_flags & RTF_GATEWAY) {
124			if (rt->rt_gwroute == 0)
125				goto lookup;
126			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
127				rtfree(rt); rt = rt0;
128			lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 0);
129				if ((rt = rt->rt_gwroute) == 0)
130					senderr(EHOSTUNREACH);
131			}
132		}
133
134		/* XXX: put RTF_REJECT code here if doing ATMARP */
135
136	}
137
138	/*
139	 * check for non-native ATM traffic   (dst != NULL)
140	 */
141	if (dst) {
142		switch (dst->sa_family) {
143#ifdef INET
144		case AF_INET:
145			if (!atmresolve(rt, m, dst, &atmdst)) {
146				m = NULL;
147				/* XXX: atmresolve already free'd it */
148				senderr(EHOSTUNREACH);
149				/* XXX: put ATMARP stuff here */
150				/* XXX: watch who frees m on failure */
151			}
152			etype = htons(ETHERTYPE_IP);
153			break;
154#endif
155
156		default:
157#if defined(__NetBSD__) || defined(__OpenBSD__)
158			printf("%s: can't handle af%d\n", ifp->if_xname,
159			    dst->sa_family);
160#elif defined(__FreeBSD__) || defined(__bsdi__)
161			printf("%s%d: can't handle af%d\n", ifp->if_name,
162			    ifp->if_unit, dst->sa_family);
163#endif
164			senderr(EAFNOSUPPORT);
165		}
166
167#if NBPFILTER > 0
168		/* BPF write needs to be handled specially */
169		if (dst && dst->sa_family == AF_UNSPEC) {
170		    dst->sa_family = *(mtod(m, int *));
171		    m->m_len -= sizeof(int);
172		    m->m_pkthdr.len -= sizeof(int);
173		    m->m_data += sizeof(int);
174		}
175
176		if (ifp->if_bpf) {
177		    /*
178		     * We need to prepend the address family as
179		     * a four byte field.  Cons up a dummy header
180		     * to pacify bpf.  This is safe because bpf
181		     * will only read from the mbuf (i.e., it won't
182		     * try to free it or keep a pointer a to it).
183		     */
184		    struct mbuf m1;
185		    u_int af = dst->sa_family;
186
187		    m1.m_next = m;
188		    m1.m_len = 4;
189		    m1.m_data = (char *)&af;
190
191		    s = splimp();
192#if defined(__NetBSD__) || defined(__OpenBSD__)
193		bpf_mtap(&ifp->if_bpf, &m0);
194#elif defined(__FreeBSD__)
195		    bpf_mtap(ifp, &m1);
196#endif
197		    splx(s);
198		}
199#endif /* NBPFILTER > 0 */
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) sz += 8; /* sizeof snap == 8 */
207		M_PREPEND(m, sz, M_DONTWAIT);
208		if (m == 0)
209			senderr(ENOBUFS);
210		ad = mtod(m, struct atm_pseudohdr *);
211		*ad = atmdst;
212		if (atm_flags & ATM_PH_LLCSNAP) {
213			atmllc = (struct atmllc *)(ad + 1);
214			bcopy(ATMLLC_HDR, atmllc->llchdr,
215						sizeof(atmllc->llchdr));
216			ATM_LLC_SETTYPE(atmllc, etype);
217					/* note: already in network order */
218		}
219	}
220
221	/*
222	 * Queue message on interface, and start output if interface
223	 * not yet active.
224	 */
225
226	s = splimp();
227	if (IF_QFULL(&ifp->if_snd)) {
228		IF_DROP(&ifp->if_snd);
229		splx(s);
230		senderr(ENOBUFS);
231	}
232	ifp->if_obytes += m->m_pkthdr.len;
233	IF_ENQUEUE(&ifp->if_snd, m);
234	if ((ifp->if_flags & IFF_OACTIVE) == 0)
235		(*ifp->if_start)(ifp);
236	splx(s);
237	return (error);
238
239bad:
240	if (m)
241		m_freem(m);
242	return (error);
243}
244
245/*
246 * Process a received ATM packet;
247 * the packet is in the mbuf chain m.
248 */
249void
250atm_input(ifp, ah, m, rxhand)
251	struct ifnet *ifp;
252	register struct atm_pseudohdr *ah;
253	struct mbuf *m;
254	void *rxhand;
255{
256	register struct ifqueue *inq;
257	u_int16_t etype = ETHERTYPE_IP; /* default */
258	int s;
259
260	if ((ifp->if_flags & IFF_UP) == 0) {
261		m_freem(m);
262		return;
263	}
264	ifp->if_lastchange = time;
265	ifp->if_ibytes += m->m_pkthdr.len;
266
267#if NBPFILTER > 0
268	if (ifp->if_bpf) {
269		/*
270		 * We need to prepend the address family as
271		 * a four byte field.  Cons up a dummy header
272		 * to pacify bpf.  This is safe because bpf
273		 * will only read from the mbuf (i.e., it won't
274		 * try to free it or keep a pointer to it).
275		 */
276		struct mbuf m0;
277		u_int af = AF_INET;
278
279		m0.m_next = m;
280		m0.m_len = 4;
281		m0.m_data = (char *)&af;
282
283#if defined(__NetBSD__) || defined(__OpenBSD__)
284		bpf_mtap(&ifp->if_bpf, &m0);
285#elif defined(__FreeBSD__)
286		bpf_mtap(ifp, &m0);
287#endif
288	}
289#endif /* NBPFILTER > 0 */
290
291	if (rxhand) {
292#ifdef NATM
293	  struct natmpcb *npcb = rxhand;
294	  s = splimp();			/* in case 2 atm cards @ diff lvls */
295	  npcb->npcb_inq++;			/* count # in queue */
296	  splx(s);
297	  schednetisr(NETISR_NATM);
298	  inq = &natmintrq;
299	  m->m_pkthdr.rcvif = rxhand; /* XXX: overload */
300#else
301	  printf("atm_input: NATM detected but not configured in kernel\n");
302	  m_freem(m);
303	  return;
304#endif
305	} else {
306	  /*
307	   * handle LLC/SNAP header, if present
308	   */
309	  if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) {
310	    struct atmllc *alc;
311	    if (m->m_len < sizeof(*alc) && (m = m_pullup(m, sizeof(*alc))) == 0)
312		  return; /* failed */
313	    alc = mtod(m, struct atmllc *);
314	    if (bcmp(alc, ATMLLC_HDR, 6)) {
315#if defined(__NetBSD__) || defined(__OpenBSD__)
316	      printf("%s: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
317		  ifp->if_xname, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
318#elif defined(__FreeBSD__) || defined(__bsdi__)
319	      printf("%s%d: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
320		  ifp->if_name, ifp->if_unit, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
321#endif
322	      m_freem(m);
323              return;
324	    }
325	    etype = ATM_LLC_TYPE(alc);
326	    m_adj(m, sizeof(*alc));
327	  }
328
329	  switch (etype) {
330#ifdef INET
331	  case ETHERTYPE_IP:
332		  schednetisr(NETISR_IP);
333		  inq = &ipintrq;
334		  break;
335#endif
336	  default:
337	      m_freem(m);
338	      return;
339	  }
340	}
341
342	s = splimp();
343	if (IF_QFULL(inq)) {
344		IF_DROP(inq);
345		m_freem(m);
346	} else
347		IF_ENQUEUE(inq, m);
348	splx(s);
349}
350
351/*
352 * Perform common duties while attaching to interface list
353 */
354void
355atm_ifattach(ifp)
356	register struct ifnet *ifp;
357{
358	register struct ifaddr *ifa;
359	register struct sockaddr_dl *sdl;
360
361	ifp->if_type = IFT_ATM;
362	ifp->if_addrlen = 0;
363	ifp->if_hdrlen = 0;
364	ifp->if_mtu = ATMMTU;
365	ifp->if_output = atm_output;
366
367#if defined(__NetBSD__) || defined(__OpenBSD__)
368	for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
369	    ifa = ifa->ifa_list.tqe_next)
370#elif defined(__FreeBSD__) && ((__FreeBSD__ > 2) || defined(_NET_IF_VAR_H_))
371/*
372 * for FreeBSD-3.0.  3.0-SNAP-970124 still sets -D__FreeBSD__=2!
373 * XXX -- for now, use newly-introduced "net/if_var.h" as an identifier.
374 * need a better way to identify 3.0.  -- kjc
375 */
376	for (ifa = ifp->if_addrhead.tqh_first; ifa;
377	    ifa = ifa->ifa_link.tqe_next)
378#elif defined(__FreeBSD__) || defined(__bsdi__)
379	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
380#endif
381
382		if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
383		    sdl->sdl_family == AF_LINK) {
384			sdl->sdl_type = IFT_ATM;
385			sdl->sdl_alen = ifp->if_addrlen;
386#ifdef notyet /* if using ATMARP, store hardware address using the next line */
387			bcopy(ifp->hw_addr, LLADDR(sdl), ifp->if_addrlen);
388#endif
389			break;
390		}
391#if NBPFILTER > 0
392#if defined(__NetBSD__) || defined(__OpenBSD__)
393	bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
394#elif defined(__FreeBSD__)
395	bpfattach(ifp, DLT_NULL, sizeof(u_int));
396#endif
397#endif /* NBPFILTER > 0 */
398}
399