if_atmsubr.c revision 116720
1151497Sru/*      $NetBSD: if_atmsubr.c,v 1.10 1997/03/11 23:19:51 chuck Exp $       */
2151497Sru
3151497Sru/*
4151497Sru *
5151497Sru * Copyright (c) 1996 Charles D. Cranor and Washington University.
6151497Sru * All rights reserved.
7151497Sru *
8151497Sru * Redistribution and use in source and binary forms, with or without
9151497Sru * modification, are permitted provided that the following conditions
10151497Sru * are met:
11151497Sru * 1. Redistributions of source code must retain the above copyright
12151497Sru *    notice, this list of conditions and the following disclaimer.
13151497Sru * 2. Redistributions in binary form must reproduce the above copyright
14151497Sru *    notice, this list of conditions and the following disclaimer in the
15151497Sru *    documentation and/or other materials provided with the distribution.
16151497Sru * 3. All advertising materials mentioning features or use of this software
17151497Sru *    must display the following acknowledgement:
18151497Sru *      This product includes software developed by Charles D. Cranor and
19151497Sru *	Washington University.
20151497Sru * 4. The name of the author may not be used to endorse or promote products
21151497Sru *    derived from this software without specific prior written permission.
22151497Sru *
23151497Sru * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24151497Sru * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25151497Sru * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26151497Sru * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27151497Sru * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28151497Sru * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29151497Sru * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30151497Sru * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31151497Sru * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32151497Sru * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33151497Sru *
34151497Sru * if_atmsubr.c
35151497Sru */
36151497Sru
37151497Sru#include <sys/cdefs.h>
38151497Sru__FBSDID("$FreeBSD: head/sys/net/if_atmsubr.c 116720 2003-06-23 10:32:13Z harti $");
39151497Sru
40151497Sru#include "opt_inet.h"
41151497Sru#include "opt_inet6.h"
42151497Sru#include "opt_mac.h"
43151497Sru#include "opt_natm.h"
44151497Sru
45151497Sru#include <sys/param.h>
46151497Sru#include <sys/systm.h>
47151497Sru#include <sys/kernel.h>
48151497Sru#include <sys/module.h>
49151497Sru#include <sys/mac.h>
50151497Sru#include <sys/mbuf.h>
51151497Sru#include <sys/socket.h>
52151497Sru#include <sys/sockio.h>
53151497Sru#include <sys/errno.h>
54151497Sru#include <sys/sysctl.h>
55151497Sru
56151497Sru#include <net/if.h>
57151497Sru#include <net/netisr.h>
58151497Sru#include <net/route.h>
59151497Sru#include <net/if_dl.h>
60151497Sru#include <net/if_types.h>
61151497Sru#include <net/if_atm.h>
62151497Sru
63151497Sru#include <netinet/in.h>
64151497Sru#include <netinet/if_atm.h>
65151497Sru#include <netinet/if_ether.h> /* XXX: for ETHERTYPE_* */
66151497Sru#if defined(INET) || defined(INET6)
67151497Sru#include <netinet/in_var.h>
68151497Sru#endif
69151497Sru#ifdef NATM
70151497Sru#include <netnatm/natm.h>
71151497Sru#endif
72151497Sru
73151497SruSYSCTL_NODE(_hw, OID_AUTO, atm, CTLFLAG_RW, 0, "ATM hardware");
74151497Sru
75151497Sru#ifndef ETHERTYPE_IPV6
76151497Sru#define	ETHERTYPE_IPV6	0x86dd
77151497Sru#endif
78151497Sru
79151497Sru#define	senderr(e) do { error = (e); goto bad; } while (0)
80151497Sru
81151497Sru/*
82151497Sru * atm_output: ATM output routine
83151497Sru *   inputs:
84151497Sru *     "ifp" = ATM interface to output to
85151497Sru *     "m0" = the packet to output
86151497Sru *     "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI)
87151497Sru *     "rt0" = the route to use
88151497Sru *   returns: error code   [0 == ok]
89151497Sru *
90151497Sru *   note: special semantic: if (dst == NULL) then we assume "m" already
91151497Sru *		has an atm_pseudohdr on it and just send it directly.
92151497Sru *		[for native mode ATM output]   if dst is null, then
93151497Sru *		rt0 must also be NULL.
94151497Sru */
95151497Sruint
96151497Sruatm_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
97151497Sru    struct rtentry *rt0)
98151497Sru{
99151497Sru	u_int16_t etype = 0;			/* if using LLC/SNAP */
100151497Sru	int error = 0, sz;
101151497Sru	struct atm_pseudohdr atmdst, *ad;
102151497Sru	struct mbuf *m = m0;
103151497Sru	struct rtentry *rt;
104151497Sru	struct atmllc *atmllc;
105151497Sru	struct atmllc *llc_hdr = NULL;
106151497Sru	u_int32_t atm_flags;
107151497Sru
108151497Sru#ifdef MAC
109151497Sru	error = mac_check_ifnet_transmit(ifp, m);
110151497Sru	if (error)
111151497Sru		senderr(error);
112151497Sru#endif
113151497Sru
114151497Sru	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
115151497Sru		senderr(ENETDOWN);
116151497Sru
117151497Sru	/*
118151497Sru	 * check route
119151497Sru	 */
120151497Sru	error = rt_check(&rt, &rt0, dst);
121151497Sru	if (error)
122151497Sru		goto bad;
123151497Sru
124151497Sru	/*
125151497Sru	 * check for non-native ATM traffic   (dst != NULL)
126151497Sru	 */
127151497Sru	if (dst) {
128151497Sru		switch (dst->sa_family) {
129151497Sru
130151497Sru#if defined(INET) || defined(INET6)
131151497Sru		case AF_INET:
132151497Sru		case AF_INET6:
133151497Sru			if (dst->sa_family == AF_INET6)
134151497Sru			        etype = ETHERTYPE_IPV6;
135151497Sru			else
136151497Sru			        etype = ETHERTYPE_IP;
137151497Sru			if (!atmresolve(rt, m, dst, &atmdst)) {
138151497Sru				m = NULL;
139151497Sru				/* XXX: atmresolve already free'd it */
140151497Sru				senderr(EHOSTUNREACH);
141151497Sru				/* XXX: put ATMARP stuff here */
142151497Sru				/* XXX: watch who frees m on failure */
143151497Sru			}
144151497Sru			break;
145151497Sru#endif /* INET || INET6 */
146151497Sru
147151497Sru		case AF_UNSPEC:
148151497Sru			/*
149151497Sru			 * XXX: bpfwrite. assuming dst contains 12 bytes
150151497Sru			 * (atm pseudo header (4) + LLC/SNAP (8))
151151497Sru			 */
152151497Sru			bcopy(dst->sa_data, &atmdst, sizeof(atmdst));
153151497Sru			llc_hdr = (struct atmllc *)(dst->sa_data +
154151497Sru			    sizeof(atmdst));
155151497Sru			break;
156151497Sru
157151497Sru		default:
158151497Sru#if defined(__NetBSD__) || defined(__OpenBSD__)
159151497Sru			printf("%s: can't handle af%d\n", ifp->if_xname,
160151497Sru			    dst->sa_family);
161151497Sru#elif defined(__FreeBSD__) || defined(__bsdi__)
162151497Sru			printf("%s%d: can't handle af%d\n", ifp->if_name,
163151497Sru			    ifp->if_unit, dst->sa_family);
164151497Sru#endif
165151497Sru			senderr(EAFNOSUPPORT);
166151497Sru		}
167151497Sru
168151497Sru		/*
169151497Sru		 * must add atm_pseudohdr to data
170151497Sru		 */
171151497Sru		sz = sizeof(atmdst);
172151497Sru		atm_flags = ATM_PH_FLAGS(&atmdst);
173151497Sru		if (atm_flags & ATM_PH_LLCSNAP)
174151497Sru			sz += 8;	/* sizeof snap == 8 */
175151497Sru		M_PREPEND(m, sz, M_DONTWAIT);
176151497Sru		if (m == 0)
177151497Sru			senderr(ENOBUFS);
178151497Sru		ad = mtod(m, struct atm_pseudohdr *);
179151497Sru		*ad = atmdst;
180151497Sru		if (atm_flags & ATM_PH_LLCSNAP) {
181151497Sru			atmllc = (struct atmllc *)(ad + 1);
182151497Sru			if (llc_hdr == NULL) {
183151497Sru			        bcopy(ATMLLC_HDR, atmllc->llchdr,
184151497Sru				      sizeof(atmllc->llchdr));
185151497Sru				/* note: in host order */
186151497Sru				ATM_LLC_SETTYPE(atmllc, etype);
187151497Sru			}
188151497Sru			else
189151497Sru			        bcopy(llc_hdr, atmllc, sizeof(struct atmllc));
190151497Sru		}
191151497Sru	}
192151497Sru
193151497Sru	/*
194151497Sru	 * Queue message on interface, and start output if interface
195151497Sru	 * not yet active.
196151497Sru	 */
197151497Sru	if (!IF_HANDOFF(&ifp->if_snd, m, ifp))
198151497Sru		return (ENOBUFS);
199151497Sru	return (error);
200151497Sru
201151497Srubad:
202151497Sru	if (m)
203151497Sru		m_freem(m);
204151497Sru	return (error);
205151497Sru}
206151497Sru
207151497Sru/*
208151497Sru * Process a received ATM packet;
209151497Sru * the packet is in the mbuf chain m.
210151497Sru */
211151497Sruvoid
212151497Sruatm_input(struct ifnet *ifp, struct atm_pseudohdr *ah, struct mbuf *m,
213151497Sru    void *rxhand)
214151497Sru{
215151497Sru	int isr;
216151497Sru	u_int16_t etype = ETHERTYPE_IP;		/* default */
217151497Sru	int s;
218151497Sru
219151497Sru	if ((ifp->if_flags & IFF_UP) == 0) {
220151497Sru		m_freem(m);
221151497Sru		return;
222151497Sru	}
223151497Sru#ifdef MAC
224151497Sru	mac_create_mbuf_from_ifnet(ifp, m);
225151497Sru#endif
226151497Sru	ifp->if_ibytes += m->m_pkthdr.len;
227151497Sru
228151497Sru	if (rxhand) {
229151497Sru#ifdef NATM
230151497Sru		struct natmpcb *npcb = rxhand;
231151497Sru
232151497Sru		s = splimp();		/* in case 2 atm cards @ diff lvls */
233151497Sru		npcb->npcb_inq++;	/* count # in queue */
234151497Sru		splx(s);
235151497Sru		isr = NETISR_NATM;
236151497Sru		m->m_pkthdr.rcvif = rxhand; /* XXX: overload */
237151497Sru#else
238151497Sru		printf("atm_input: NATM detected but not "
239151497Sru		    "configured in kernel\n");
240151497Sru		m_freem(m);
241151497Sru		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