if_atmsubr.c revision 117630
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 117630 2003-07-15 10:37:09Z 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#include <sys/malloc.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
74/*
75 * Netgraph interface functions.
76 * These need not be protected by a lock, because ng_atm nodes are persitent.
77 * The ng_atm module can be unloaded only if all ATM interfaces have been
78 * unloaded, so nobody should be in the code paths accessing these function
79 * pointers.
80 */
81void	(*ng_atm_attach_p)(struct ifnet *);
82void	(*ng_atm_detach_p)(struct ifnet *);
83int	(*ng_atm_output_p)(struct ifnet *, struct mbuf **);
84void	(*ng_atm_input_p)(struct ifnet *, struct mbuf **,
85	    struct atm_pseudohdr *, void *);
86void	(*ng_atm_input_orphan_p)(struct ifnet *, struct mbuf *,
87	    struct atm_pseudohdr *, void *);
88void	(*ng_atm_message_p)(struct ifnet *, u_int32_t, u_int32_t);
89
90/*
91 * Harp pseudo interface hooks
92 */
93void	(*atm_harp_input_p)(struct ifnet *ifp, struct mbuf **m,
94	    struct atm_pseudohdr *ah, void *rxhand);
95void	(*atm_harp_attach_p)(struct ifnet *);
96void	(*atm_harp_detach_p)(struct ifnet *);
97
98SYSCTL_NODE(_hw, OID_AUTO, atm, CTLFLAG_RW, 0, "ATM hardware");
99
100#ifndef ETHERTYPE_IPV6
101#define	ETHERTYPE_IPV6	0x86dd
102#endif
103
104#define	senderr(e) do { error = (e); goto bad; } while (0)
105
106/*
107 * atm_output: ATM output routine
108 *   inputs:
109 *     "ifp" = ATM interface to output to
110 *     "m0" = the packet to output
111 *     "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI)
112 *     "rt0" = the route to use
113 *   returns: error code   [0 == ok]
114 *
115 *   note: special semantic: if (dst == NULL) then we assume "m" already
116 *		has an atm_pseudohdr on it and just send it directly.
117 *		[for native mode ATM output]   if dst is null, then
118 *		rt0 must also be NULL.
119 */
120int
121atm_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
122    struct rtentry *rt0)
123{
124	u_int16_t etype = 0;			/* if using LLC/SNAP */
125	int error = 0, sz;
126	struct atm_pseudohdr atmdst, *ad;
127	struct mbuf *m = m0;
128	struct rtentry *rt;
129	struct atmllc *atmllc;
130	struct atmllc *llc_hdr = NULL;
131	u_int32_t atm_flags;
132
133#ifdef MAC
134	error = mac_check_ifnet_transmit(ifp, m);
135	if (error)
136		senderr(error);
137#endif
138
139	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
140		senderr(ENETDOWN);
141
142	/*
143	 * check route
144	 */
145	error = rt_check(&rt, &rt0, dst);
146	if (error)
147		goto bad;
148
149	/*
150	 * check for non-native ATM traffic   (dst != NULL)
151	 */
152	if (dst) {
153		switch (dst->sa_family) {
154
155#if defined(INET) || defined(INET6)
156		case AF_INET:
157		case AF_INET6:
158			if (dst->sa_family == AF_INET6)
159			        etype = ETHERTYPE_IPV6;
160			else
161			        etype = ETHERTYPE_IP;
162			if (!atmresolve(rt, m, dst, &atmdst)) {
163				m = NULL;
164				/* XXX: atmresolve already free'd it */
165				senderr(EHOSTUNREACH);
166				/* XXX: put ATMARP stuff here */
167				/* XXX: watch who frees m on failure */
168			}
169			break;
170#endif /* INET || INET6 */
171
172		case AF_UNSPEC:
173			/*
174			 * XXX: bpfwrite. assuming dst contains 12 bytes
175			 * (atm pseudo header (4) + LLC/SNAP (8))
176			 */
177			bcopy(dst->sa_data, &atmdst, sizeof(atmdst));
178			llc_hdr = (struct atmllc *)(dst->sa_data +
179			    sizeof(atmdst));
180			break;
181
182		default:
183#if defined(__NetBSD__) || defined(__OpenBSD__)
184			printf("%s: can't handle af%d\n", ifp->if_xname,
185			    dst->sa_family);
186#elif defined(__FreeBSD__) || defined(__bsdi__)
187			printf("%s%d: can't handle af%d\n", ifp->if_name,
188			    ifp->if_unit, dst->sa_family);
189#endif
190			senderr(EAFNOSUPPORT);
191		}
192
193		/*
194		 * must add atm_pseudohdr to data
195		 */
196		sz = sizeof(atmdst);
197		atm_flags = ATM_PH_FLAGS(&atmdst);
198		if (atm_flags & ATM_PH_LLCSNAP)
199			sz += 8;	/* sizeof snap == 8 */
200		M_PREPEND(m, sz, M_DONTWAIT);
201		if (m == 0)
202			senderr(ENOBUFS);
203		ad = mtod(m, struct atm_pseudohdr *);
204		*ad = atmdst;
205		if (atm_flags & ATM_PH_LLCSNAP) {
206			atmllc = (struct atmllc *)(ad + 1);
207			if (llc_hdr == NULL) {
208			        bcopy(ATMLLC_HDR, atmllc->llchdr,
209				      sizeof(atmllc->llchdr));
210				/* note: in host order */
211				ATM_LLC_SETTYPE(atmllc, etype);
212			}
213			else
214			        bcopy(llc_hdr, atmllc, sizeof(struct atmllc));
215		}
216	}
217
218	if (ng_atm_output_p != NULL) {
219		if ((error = (*ng_atm_output_p)(ifp, &m)) != 0) {
220			if (m != NULL)
221				m_freem(m);
222			return (error);
223		}
224		if (m == NULL)
225			return (0);
226	}
227
228	/*
229	 * Queue message on interface, and start output if interface
230	 * not yet active.
231	 */
232	if (!IF_HANDOFF_ADJ(&ifp->if_snd, m, ifp,
233	    -(int)sizeof(struct atm_pseudohdr)))
234		return (ENOBUFS);
235	return (error);
236
237bad:
238	if (m)
239		m_freem(m);
240	return (error);
241}
242
243/*
244 * Process a received ATM packet;
245 * the packet is in the mbuf chain m.
246 */
247void
248atm_input(struct ifnet *ifp, struct atm_pseudohdr *ah, struct mbuf *m,
249    void *rxhand)
250{
251	int isr;
252	u_int16_t etype = ETHERTYPE_IP;		/* default */
253	int s;
254
255	if ((ifp->if_flags & IFF_UP) == 0) {
256		m_freem(m);
257		return;
258	}
259#ifdef MAC
260	mac_create_mbuf_from_ifnet(ifp, m);
261#endif
262	ifp->if_ibytes += m->m_pkthdr.len;
263
264	if (ng_atm_input_p != NULL) {
265		(*ng_atm_input_p)(ifp, &m, ah, rxhand);
266		if (m == NULL)
267			return;
268	}
269
270	/* not eaten by ng_atm. Maybe it's a pseudo-harp PDU? */
271	if (atm_harp_input_p != NULL) {
272		(*atm_harp_input_p)(ifp, &m, ah, rxhand);
273		if (m == NULL)
274			return;
275	}
276
277	if (rxhand) {
278#ifdef NATM
279		struct natmpcb *npcb = rxhand;
280
281		s = splimp();		/* in case 2 atm cards @ diff lvls */
282		npcb->npcb_inq++;	/* count # in queue */
283		splx(s);
284		isr = NETISR_NATM;
285		m->m_pkthdr.rcvif = rxhand; /* XXX: overload */
286#else
287		printf("atm_input: NATM detected but not "
288		    "configured in kernel\n");
289		goto dropit;
290#endif
291	} else {
292		/*
293		 * handle LLC/SNAP header, if present
294		 */
295		if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) {
296			struct atmllc *alc;
297
298			if (m->m_len < sizeof(*alc) &&
299			    (m = m_pullup(m, sizeof(*alc))) == 0)
300				return; /* failed */
301			alc = mtod(m, struct atmllc *);
302			if (bcmp(alc, ATMLLC_HDR, 6)) {
303#if defined(__NetBSD__) || defined(__OpenBSD__)
304				printf("%s: recv'd invalid LLC/SNAP frame "
305				    "[vp=%d,vc=%d]\n", ifp->if_xname,
306				    ATM_PH_VPI(ah), ATM_PH_VCI(ah));
307#elif defined(__FreeBSD__) || defined(__bsdi__)
308				printf("%s%d: recv'd invalid LLC/SNAP frame "
309				    "[vp=%d,vc=%d]\n", ifp->if_name,
310				    ifp->if_unit, ATM_PH_VPI(ah),
311				    ATM_PH_VCI(ah));
312#endif
313				m_freem(m);
314				return;
315			}
316			etype = ATM_LLC_TYPE(alc);
317			m_adj(m, sizeof(*alc));
318		}
319
320		switch (etype) {
321
322#ifdef INET
323		case ETHERTYPE_IP:
324			isr = NETISR_IP;
325			break;
326#endif
327
328#ifdef INET6
329		case ETHERTYPE_IPV6:
330			isr = NETISR_IPV6;
331			break;
332#endif
333		default:
334#ifndef NATM
335  dropit:
336#endif
337			if (ng_atm_input_orphan_p != NULL)
338				(*ng_atm_input_orphan_p)(ifp, m, ah, rxhand);
339			else
340				m_freem(m);
341			return;
342		}
343	}
344	netisr_dispatch(isr, m);
345}
346
347/*
348 * Perform common duties while attaching to interface list.
349 */
350void
351atm_ifattach(struct ifnet *ifp)
352{
353	struct ifaddr *ifa;
354	struct sockaddr_dl *sdl;
355	struct ifatm *ifatm = ifp->if_softc;
356
357	ifp->if_type = IFT_ATM;
358	ifp->if_addrlen = 0;
359	ifp->if_hdrlen = 0;
360	if_attach(ifp);
361	ifp->if_mtu = ATMMTU;
362	ifp->if_output = atm_output;
363#if 0
364	ifp->if_input = atm_input;
365#endif
366	ifp->if_snd.ifq_maxlen = 50;	/* dummy */
367
368#if defined(__NetBSD__) || defined(__OpenBSD__)
369	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
370#elif defined(__FreeBSD__) && (__FreeBSD__ > 2)
371	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
372	    ifa = TAILQ_NEXT(ifa, ifa_link))
373#elif defined(__FreeBSD__) || defined(__bsdi__)
374	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
375#endif
376		if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
377		    sdl->sdl_family == AF_LINK) {
378			sdl->sdl_type = IFT_ATM;
379			sdl->sdl_alen = ifp->if_addrlen;
380#ifdef notyet /* if using ATMARP, store hardware address using the next line */
381			bcopy(ifp->hw_addr, LLADDR(sdl), ifp->if_addrlen);
382#endif
383			break;
384		}
385
386	ifp->if_linkmib = &ifatm->mib;
387	ifp->if_linkmiblen = sizeof(ifatm->mib);
388
389	if(ng_atm_attach_p)
390		(*ng_atm_attach_p)(ifp);
391	if (atm_harp_attach_p)
392		(*atm_harp_attach_p)(ifp);
393}
394
395/*
396 * Common stuff for detaching an ATM interface
397 */
398void
399atm_ifdetach(struct ifnet *ifp)
400{
401	if (atm_harp_detach_p)
402		(*atm_harp_detach_p)(ifp);
403	if(ng_atm_detach_p)
404		(*ng_atm_detach_p)(ifp);
405	if_detach(ifp);
406}
407
408/*
409 * Support routine for the SIOCATMGVCCS ioctl().
410 *
411 * This routine assumes, that the private VCC structures used by the driver
412 * begin with a struct atmio_vcc.
413 *
414 * Return a table of VCCs in a freshly allocated memory area.
415 * Here we have a problem: we first count, how many vccs we need
416 * to return. The we allocate the memory and finally fill it in.
417 * Because we cannot lock while calling malloc, the number of active
418 * vccs may change while we're in malloc. So we allocate a couple of
419 * vccs more and if space anyway is not enough re-iterate.
420 *
421 * We could use an sx lock for the vcc tables.
422 */
423struct atmio_vcctable *
424atm_getvccs(struct atmio_vcc **table, u_int size, u_int start,
425    struct mtx *lock, int waitok)
426{
427	u_int cid, alloc;
428	size_t len;
429	struct atmio_vcctable *vccs;
430	struct atmio_vcc *v;
431
432	alloc = start + 10;
433	vccs = NULL;
434
435	for (;;) {
436		len = sizeof(*vccs) + alloc * sizeof(vccs->vccs[0]);
437		vccs = reallocf(vccs, len, M_TEMP,
438		    waitok ? M_WAITOK : M_NOWAIT);
439		if (vccs == NULL)
440			return (NULL);
441		bzero(vccs, len);
442
443		vccs->count = 0;
444		v = vccs->vccs;
445
446		mtx_lock(lock);
447		for (cid = 0; cid < size; cid++)
448			if (table[cid] != NULL) {
449				if (++vccs->count == alloc)
450					/* too many - try again */
451					break;
452				*v++ = *table[cid];
453			}
454		mtx_unlock(lock);
455
456		if (cid == size)
457			break;
458
459		alloc *= 2;
460	}
461	return (vccs);
462}
463
464static moduledata_t atm_mod = {
465        "atm",
466        NULL,
467        0
468};
469
470DECLARE_MODULE(atm, atm_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
471MODULE_VERSION(atm, 1);
472