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