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"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/errno.h>
52 #include <sys/sysctl.h>
53 #include <sys/malloc.h>
56 #include <net/if_var.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>
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>
70 #include <netnatm/natm.h>
73 #include <security/mac/mac_framework.h>
76 * Netgraph interface functions.
77 * These need not be protected by a lock, because ng_atm nodes are persitent.
78 * The ng_atm module can be unloaded only if all ATM interfaces have been
79 * unloaded, so nobody should be in the code paths accessing these function
82 void (*ng_atm_attach_p
)(struct ifnet
*);
83 void (*ng_atm_detach_p
)(struct ifnet
*);
84 int (*ng_atm_output_p
)(struct ifnet
*, struct mbuf
**);
85 void (*ng_atm_input_p
)(struct ifnet
*, struct mbuf
**,
86 struct atm_pseudohdr
*, void *);
87 void (*ng_atm_input_orphan_p
)(struct ifnet
*, struct mbuf
*,
88 struct atm_pseudohdr
*, void *);
89 void (*ng_atm_event_p
)(struct ifnet
*, uint32_t, void *);
92 * Harp pseudo interface hooks
94 void (*atm_harp_input_p
)(struct ifnet
*ifp
, struct mbuf
**m
,
95 struct atm_pseudohdr
*ah
, void *rxhand
);
96 void (*atm_harp_attach_p
)(struct ifnet
*);
97 void (*atm_harp_detach_p
)(struct ifnet
*);
98 void (*atm_harp_event_p
)(struct ifnet
*, uint32_t, void *);
100 SYSCTL_NODE(_hw
, OID_AUTO
, atm
, CTLFLAG_RW
, 0, "ATM hardware");
102 static MALLOC_DEFINE(M_IFATM
, "ifatm", "atm interface internals");
104 #ifndef ETHERTYPE_IPV6
105 #define ETHERTYPE_IPV6 0x86dd
108 #define senderr(e) do { error = (e); goto bad; } while (0)
111 * atm_output: ATM output routine
113 * "ifp" = ATM interface to output to
114 * "m0" = the packet to output
115 * "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI)
116 * "ro" = the route to use
117 * returns: error code [0 == ok]
119 * note: special semantic: if (dst == NULL) then we assume "m" already
120 * has an atm_pseudohdr on it and just send it directly.
121 * [for native mode ATM output] if dst is null, then
122 * ro->ro_rt must also be NULL.
125 atm_output(struct ifnet
*ifp
, struct mbuf
*m0
, const struct sockaddr
*dst
,
128 u_int16_t etype
= 0; /* if using LLC/SNAP */
130 struct atm_pseudohdr atmdst
, *ad
;
132 struct atmllc
*atmllc
;
133 const struct atmllc
*llc_hdr
= NULL
;
137 error
= mac_ifnet_check_transmit(ifp
, m
);
142 if (!((ifp
->if_flags
& IFF_UP
) &&
143 (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)))
147 * check for non-native ATM traffic (dst != NULL)
150 switch (dst
->sa_family
) {
152 #if defined(INET) || defined(INET6)
156 if (dst
->sa_family
== AF_INET6
)
157 etype
= ETHERTYPE_IPV6
;
159 etype
= ETHERTYPE_IP
;
160 if (!atmresolve(ro
->ro_rt
, m
, dst
, &atmdst
)) {
162 /* XXX: atmresolve already free'd it */
163 senderr(EHOSTUNREACH
);
164 /* XXX: put ATMARP stuff here */
165 /* XXX: watch who frees m on failure */
169 #endif /* INET || INET6 */
173 * XXX: bpfwrite. assuming dst contains 12 bytes
174 * (atm pseudo header (4) + LLC/SNAP (8))
176 bcopy(dst
->sa_data
, &atmdst
, sizeof(atmdst
));
177 llc_hdr
= (const struct atmllc
*)(dst
->sa_data
+
182 printf("%s: can't handle af%d\n", ifp
->if_xname
,
184 senderr(EAFNOSUPPORT
);
188 * must add atm_pseudohdr to data
191 atm_flags
= ATM_PH_FLAGS(&atmdst
);
192 if (atm_flags
& ATM_PH_LLCSNAP
)
193 sz
+= 8; /* sizeof snap == 8 */
194 M_PREPEND(m
, sz
, M_NOWAIT
);
197 ad
= mtod(m
, struct atm_pseudohdr
*);
199 if (atm_flags
& ATM_PH_LLCSNAP
) {
200 atmllc
= (struct atmllc
*)(ad
+ 1);
201 if (llc_hdr
== NULL
) {
202 bcopy(ATMLLC_HDR
, atmllc
->llchdr
,
203 sizeof(atmllc
->llchdr
));
204 /* note: in host order */
205 ATM_LLC_SETTYPE(atmllc
, etype
);
208 bcopy(llc_hdr
, atmllc
, sizeof(struct atmllc
));
212 if (ng_atm_output_p
!= NULL
) {
213 if ((error
= (*ng_atm_output_p
)(ifp
, &m
)) != 0) {
223 * Queue message on interface, and start output if interface
226 if (!IF_HANDOFF_ADJ(&ifp
->if_snd
, m
, ifp
,
227 -(int)sizeof(struct atm_pseudohdr
)))
238 * Process a received ATM packet;
239 * the packet is in the mbuf chain m.
242 atm_input(struct ifnet
*ifp
, struct atm_pseudohdr
*ah
, struct mbuf
*m
,
246 u_int16_t etype
= ETHERTYPE_IP
; /* default */
248 if ((ifp
->if_flags
& IFF_UP
) == 0) {
253 mac_ifnet_create_mbuf(ifp
, m
);
255 if_inc_counter(ifp
, IFCOUNTER_IBYTES
, m
->m_pkthdr
.len
);
257 if (ng_atm_input_p
!= NULL
) {
258 (*ng_atm_input_p
)(ifp
, &m
, ah
, rxhand
);
263 /* not eaten by ng_atm. Maybe it's a pseudo-harp PDU? */
264 if (atm_harp_input_p
!= NULL
) {
265 (*atm_harp_input_p
)(ifp
, &m
, ah
, rxhand
);
272 struct natmpcb
*npcb
;
275 * XXXRW: this use of 'rxhand' is not a very good idea, and
276 * was subject to races even before SMPng due to the release
281 npcb
->npcb_inq
++; /* count # in queue */
283 m
->m_pkthdr
.rcvif
= rxhand
; /* XXX: overload */
286 printf("atm_input: NATM detected but not "
287 "configured in kernel\n");
292 * handle LLC/SNAP header, if present
294 if (ATM_PH_FLAGS(ah
) & ATM_PH_LLCSNAP
) {
297 if (m
->m_len
< sizeof(*alc
) &&
298 (m
= m_pullup(m
, sizeof(*alc
))) == NULL
)
300 alc
= mtod(m
, struct atmllc
*);
301 if (bcmp(alc
, ATMLLC_HDR
, 6)) {
302 printf("%s: recv'd invalid LLC/SNAP frame "
303 "[vp=%d,vc=%d]\n", ifp
->if_xname
,
304 ATM_PH_VPI(ah
), ATM_PH_VCI(ah
));
308 etype
= ATM_LLC_TYPE(alc
);
309 m_adj(m
, sizeof(*alc
));
329 if (ng_atm_input_orphan_p
!= NULL
)
330 (*ng_atm_input_orphan_p
)(ifp
, m
, ah
, rxhand
);
336 M_SETFIB(m
, ifp
->if_fib
);
337 netisr_dispatch(isr
, m
);
341 * Perform common duties while attaching to interface list.
344 atm_ifattach(struct ifnet
*ifp
)
347 struct sockaddr_dl
*sdl
;
348 struct ifatm
*ifatm
= ifp
->if_l2com
;
353 ifp
->if_mtu
= ATMMTU
;
354 ifp
->if_output
= atm_output
;
356 ifp
->if_input
= atm_input
;
358 ifp
->if_snd
.ifq_maxlen
= 50; /* dummy */
360 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
361 if (ifa
->ifa_addr
->sa_family
== AF_LINK
) {
362 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
363 sdl
->sdl_type
= IFT_ATM
;
364 sdl
->sdl_alen
= ifp
->if_addrlen
;
365 #ifdef notyet /* if using ATMARP, store hardware address using the next line */
366 bcopy(ifp
->hw_addr
, LLADDR(sdl
), ifp
->if_addrlen
);
371 ifp
->if_linkmib
= &ifatm
->mib
;
372 ifp
->if_linkmiblen
= sizeof(ifatm
->mib
);
375 (*ng_atm_attach_p
)(ifp
);
376 if (atm_harp_attach_p
)
377 (*atm_harp_attach_p
)(ifp
);
381 * Common stuff for detaching an ATM interface
384 atm_ifdetach(struct ifnet
*ifp
)
386 if (atm_harp_detach_p
)
387 (*atm_harp_detach_p
)(ifp
);
389 (*ng_atm_detach_p
)(ifp
);
394 * Support routine for the SIOCATMGVCCS ioctl().
396 * This routine assumes, that the private VCC structures used by the driver
397 * begin with a struct atmio_vcc.
399 * Return a table of VCCs in a freshly allocated memory area.
400 * Here we have a problem: we first count, how many vccs we need
401 * to return. The we allocate the memory and finally fill it in.
402 * Because we cannot lock while calling malloc, the number of active
403 * vccs may change while we're in malloc. So we allocate a couple of
404 * vccs more and if space anyway is not enough re-iterate.
406 * We could use an sx lock for the vcc tables.
408 struct atmio_vcctable
*
409 atm_getvccs(struct atmio_vcc
**table
, u_int size
, u_int start
,
410 struct mtx
*lock
, int waitok
)
414 struct atmio_vcctable
*vccs
;
421 len
= sizeof(*vccs
) + alloc
* sizeof(vccs
->vccs
[0]);
422 vccs
= reallocf(vccs
, len
, M_TEMP
,
423 waitok
? M_WAITOK
: M_NOWAIT
);
432 for (cid
= 0; cid
< size
; cid
++)
433 if (table
[cid
] != NULL
) {
434 if (++vccs
->count
== alloc
)
435 /* too many - try again */
450 * Driver or channel state has changed. Inform whoever is interested
454 atm_event(struct ifnet
*ifp
, u_int event
, void *arg
)
456 if (ng_atm_event_p
!= NULL
)
457 (*ng_atm_event_p
)(ifp
, event
, arg
);
458 if (atm_harp_event_p
!= NULL
)
459 (*atm_harp_event_p
)(ifp
, event
, arg
);
463 atm_alloc(u_char type
, struct ifnet
*ifp
)
467 ifatm
= malloc(sizeof(struct ifatm
), M_IFATM
, M_WAITOK
| M_ZERO
);
474 atm_free(void *com
, u_char type
)
481 atm_modevent(module_t mod
, int type
, void *data
)
485 if_register_com_alloc(IFT_ATM
, atm_alloc
, atm_free
);
488 if_deregister_com_alloc(IFT_ATM
);
497 static moduledata_t atm_mod
= {
503 DECLARE_MODULE(atm
, atm_mod
, SI_SUB_INIT_IF
, SI_ORDER_ANY
);
504 MODULE_VERSION(atm
, 1);