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