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