1 /* $NetBSD: if_atmsubr.c,v 1.10 1997/03/11 23:19:51 chuck Exp $ */
5 * Copyright (c) 1996 Charles D. Cranor and Washington University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/module.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>
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>
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>
71 #include <netnatm/natm.h>
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
81 void (*ng_atm_attach_p)(struct ifnet *);
82 void (*ng_atm_detach_p)(struct ifnet *);
83 int (*ng_atm_output_p)(struct ifnet *, struct mbuf **);
84 void (*ng_atm_input_p)(struct ifnet *, struct mbuf **,
85 struct atm_pseudohdr *, void *);
86 void (*ng_atm_input_orphan_p)(struct ifnet *, struct mbuf *,
87 struct atm_pseudohdr *, void *);
88 void (*ng_atm_event_p)(struct ifnet *, uint32_t, void *);
91 * Harp pseudo interface hooks
93 void (*atm_harp_input_p)(struct ifnet *ifp, struct mbuf **m,
94 struct atm_pseudohdr *ah, void *rxhand);
95 void (*atm_harp_attach_p)(struct ifnet *);
96 void (*atm_harp_detach_p)(struct ifnet *);
97 void (*atm_harp_event_p)(struct ifnet *, uint32_t, void *);
99 SYSCTL_NODE(_hw, OID_AUTO, atm, CTLFLAG_RW, 0, "ATM hardware");
101 #ifndef ETHERTYPE_IPV6
102 #define ETHERTYPE_IPV6 0x86dd
105 #define senderr(e) do { error = (e); goto bad; } while (0)
108 * atm_output: ATM output routine
110 * "ifp" = ATM interface to output to
111 * "m0" = the packet to output
112 * "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI)
113 * "rt0" = the route to use
114 * returns: error code [0 == ok]
116 * note: special semantic: if (dst == NULL) then we assume "m" already
117 * has an atm_pseudohdr on it and just send it directly.
118 * [for native mode ATM output] if dst is null, then
119 * rt0 must also be NULL.
122 atm_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
125 u_int16_t etype = 0; /* if using LLC/SNAP */
127 struct atm_pseudohdr atmdst, *ad;
129 struct atmllc *atmllc;
130 struct atmllc *llc_hdr = NULL;
134 error = mac_check_ifnet_transmit(ifp, m);
139 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
143 * check for non-native ATM traffic (dst != NULL)
146 switch (dst->sa_family) {
148 #if defined(INET) || defined(INET6)
156 error = rt_check(&rt, &rt0, dst);
160 if (dst->sa_family == AF_INET6)
161 etype = ETHERTYPE_IPV6;
163 etype = ETHERTYPE_IP;
164 if (!atmresolve(rt, m, dst, &atmdst)) {
166 /* XXX: atmresolve already free'd it */
167 senderr(EHOSTUNREACH);
168 /* XXX: put ATMARP stuff here */
169 /* XXX: watch who frees m on failure */
173 #endif /* INET || INET6 */
177 * XXX: bpfwrite. assuming dst contains 12 bytes
178 * (atm pseudo header (4) + LLC/SNAP (8))
180 bcopy(dst->sa_data, &atmdst, sizeof(atmdst));
181 llc_hdr = (struct atmllc *)(dst->sa_data +
186 #if (defined(__FreeBSD__) && __FreeBSD_version >= 501113) || \
187 defined(__NetBSD__) || defined(__OpenBSD__)
188 printf("%s: can't handle af%d\n", ifp->if_xname,
190 #elif defined(__FreeBSD__) || defined(__bsdi__)
191 printf("%s%d: can't handle af%d\n", ifp->if_name,
192 ifp->if_unit, dst->sa_family);
194 senderr(EAFNOSUPPORT);
198 * must add atm_pseudohdr to data
201 atm_flags = ATM_PH_FLAGS(&atmdst);
202 if (atm_flags & ATM_PH_LLCSNAP)
203 sz += 8; /* sizeof snap == 8 */
204 M_PREPEND(m, sz, M_DONTWAIT);
207 ad = mtod(m, struct atm_pseudohdr *);
209 if (atm_flags & ATM_PH_LLCSNAP) {
210 atmllc = (struct atmllc *)(ad + 1);
211 if (llc_hdr == NULL) {
212 bcopy(ATMLLC_HDR, atmllc->llchdr,
213 sizeof(atmllc->llchdr));
214 /* note: in host order */
215 ATM_LLC_SETTYPE(atmllc, etype);
218 bcopy(llc_hdr, atmllc, sizeof(struct atmllc));
222 if (ng_atm_output_p != NULL) {
223 if ((error = (*ng_atm_output_p)(ifp, &m)) != 0) {
233 * Queue message on interface, and start output if interface
236 if (!IF_HANDOFF_ADJ(&ifp->if_snd, m, ifp,
237 -(int)sizeof(struct atm_pseudohdr)))
248 * Process a received ATM packet;
249 * the packet is in the mbuf chain m.
252 atm_input(struct ifnet *ifp, struct atm_pseudohdr *ah, struct mbuf *m,
256 u_int16_t etype = ETHERTYPE_IP; /* default */
261 if ((ifp->if_flags & IFF_UP) == 0) {
266 mac_create_mbuf_from_ifnet(ifp, m);
268 ifp->if_ibytes += m->m_pkthdr.len;
270 if (ng_atm_input_p != NULL) {
271 (*ng_atm_input_p)(ifp, &m, ah, rxhand);
276 /* not eaten by ng_atm. Maybe it's a pseudo-harp PDU? */
277 if (atm_harp_input_p != NULL) {
278 (*atm_harp_input_p)(ifp, &m, ah, rxhand);
285 struct natmpcb *npcb = rxhand;
287 s = splimp(); /* in case 2 atm cards @ diff lvls */
288 npcb->npcb_inq++; /* count # in queue */
291 m->m_pkthdr.rcvif = rxhand; /* XXX: overload */
293 printf("atm_input: NATM detected but not "
294 "configured in kernel\n");
299 * handle LLC/SNAP header, if present
301 if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) {
304 if (m->m_len < sizeof(*alc) &&
305 (m = m_pullup(m, sizeof(*alc))) == 0)
307 alc = mtod(m, struct atmllc *);
308 if (bcmp(alc, ATMLLC_HDR, 6)) {
309 #if (defined(__FreeBSD__) && __FreeBSD_version >= 501113) || \
310 defined(__NetBSD__) || defined(__OpenBSD__)
311 printf("%s: recv'd invalid LLC/SNAP frame "
312 "[vp=%d,vc=%d]\n", ifp->if_xname,
313 ATM_PH_VPI(ah), ATM_PH_VCI(ah));
314 #elif defined(__FreeBSD__) || defined(__bsdi__)
315 printf("%s%d: recv'd invalid LLC/SNAP frame "
316 "[vp=%d,vc=%d]\n", ifp->if_name,
317 ifp->if_unit, ATM_PH_VPI(ah),
323 etype = ATM_LLC_TYPE(alc);
324 m_adj(m, sizeof(*alc));
344 if (ng_atm_input_orphan_p != NULL)
345 (*ng_atm_input_orphan_p)(ifp, m, ah, rxhand);
351 netisr_dispatch(isr, m);
355 * Perform common duties while attaching to interface list.
358 atm_ifattach(struct ifnet *ifp)
361 struct sockaddr_dl *sdl;
362 struct ifatm *ifatm = ifp->if_softc;
364 ifp->if_type = IFT_ATM;
368 ifp->if_mtu = ATMMTU;
369 ifp->if_output = atm_output;
371 ifp->if_input = atm_input;
373 ifp->if_snd.ifq_maxlen = 50; /* dummy */
375 #if defined(__NetBSD__) || defined(__OpenBSD__)
376 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
377 #elif defined(__FreeBSD__) && (__FreeBSD__ > 2)
378 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
379 ifa = TAILQ_NEXT(ifa, ifa_link))
380 #elif defined(__FreeBSD__) || defined(__bsdi__)
381 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
383 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
384 sdl->sdl_family == AF_LINK) {
385 sdl->sdl_type = IFT_ATM;
386 sdl->sdl_alen = ifp->if_addrlen;
387 #ifdef notyet /* if using ATMARP, store hardware address using the next line */
388 bcopy(ifp->hw_addr, LLADDR(sdl), ifp->if_addrlen);
393 ifp->if_linkmib = &ifatm->mib;
394 ifp->if_linkmiblen = sizeof(ifatm->mib);
397 (*ng_atm_attach_p)(ifp);
398 if (atm_harp_attach_p)
399 (*atm_harp_attach_p)(ifp);
403 * Common stuff for detaching an ATM interface
406 atm_ifdetach(struct ifnet *ifp)
408 if (atm_harp_detach_p)
409 (*atm_harp_detach_p)(ifp);
411 (*ng_atm_detach_p)(ifp);
416 * Support routine for the SIOCATMGVCCS ioctl().
418 * This routine assumes, that the private VCC structures used by the driver
419 * begin with a struct atmio_vcc.
421 * Return a table of VCCs in a freshly allocated memory area.
422 * Here we have a problem: we first count, how many vccs we need
423 * to return. The we allocate the memory and finally fill it in.
424 * Because we cannot lock while calling malloc, the number of active
425 * vccs may change while we're in malloc. So we allocate a couple of
426 * vccs more and if space anyway is not enough re-iterate.
428 * We could use an sx lock for the vcc tables.
430 struct atmio_vcctable *
431 atm_getvccs(struct atmio_vcc **table, u_int size, u_int start,
432 struct mtx *lock, int waitok)
436 struct atmio_vcctable *vccs;
443 len = sizeof(*vccs) + alloc * sizeof(vccs->vccs[0]);
444 vccs = reallocf(vccs, len, M_TEMP,
445 waitok ? M_WAITOK : M_NOWAIT);
454 for (cid = 0; cid < size; cid++)
455 if (table[cid] != NULL) {
456 if (++vccs->count == alloc)
457 /* too many - try again */
472 * Driver or channel state has changed. Inform whoever is interested
476 atm_event(struct ifnet *ifp, u_int event, void *arg)
478 if (ng_atm_event_p != NULL)
479 (*ng_atm_event_p)(ifp, event, arg);
480 if (atm_harp_event_p != NULL)
481 (*atm_harp_event_p)(ifp, event, arg);
484 static moduledata_t atm_mod = {
490 DECLARE_MODULE(atm, atm_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
491 MODULE_VERSION(atm, 1);