Deleted Added
full compact
if_atmsubr.c (191148) if_atmsubr.c (193511)
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>
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 191148 2009-04-16 20:30:28Z kmacy $");
38__FBSDID("$FreeBSD: head/sys/net/if_atmsubr.c 193511 2009-06-05 14:55:22Z rwatson $");
39
40#include "opt_inet.h"
41#include "opt_inet6.h"
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/mbuf.h>
50#include <sys/socket.h>
51#include <sys/sockio.h>
52#include <sys/errno.h>
53#include <sys/sysctl.h>
54#include <sys/malloc.h>
55
56#include <net/if.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>
62
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>
68#endif
69#ifdef NATM
70#include <netnatm/natm.h>
71#endif
72
73#include <security/mac/mac_framework.h>
74
75/*
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
80 * pointers.
81 */
82void (*ng_atm_attach_p)(struct ifnet *);
83void (*ng_atm_detach_p)(struct ifnet *);
84int (*ng_atm_output_p)(struct ifnet *, struct mbuf **);
85void (*ng_atm_input_p)(struct ifnet *, struct mbuf **,
86 struct atm_pseudohdr *, void *);
87void (*ng_atm_input_orphan_p)(struct ifnet *, struct mbuf *,
88 struct atm_pseudohdr *, void *);
89void (*ng_atm_event_p)(struct ifnet *, uint32_t, void *);
90
91/*
92 * Harp pseudo interface hooks
93 */
94void (*atm_harp_input_p)(struct ifnet *ifp, struct mbuf **m,
95 struct atm_pseudohdr *ah, void *rxhand);
96void (*atm_harp_attach_p)(struct ifnet *);
97void (*atm_harp_detach_p)(struct ifnet *);
98void (*atm_harp_event_p)(struct ifnet *, uint32_t, void *);
99
100SYSCTL_NODE(_hw, OID_AUTO, atm, CTLFLAG_RW, 0, "ATM hardware");
101
102MALLOC_DEFINE(M_IFATM, "ifatm", "atm interface internals");
103
104#ifndef ETHERTYPE_IPV6
105#define ETHERTYPE_IPV6 0x86dd
106#endif
107
108#define senderr(e) do { error = (e); goto bad; } while (0)
109
110/*
111 * atm_output: ATM output routine
112 * inputs:
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]
118 *
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.
123 */
124int
125atm_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
126 struct route *ro)
127{
128 u_int16_t etype = 0; /* if using LLC/SNAP */
129 int error = 0, sz;
130 struct atm_pseudohdr atmdst, *ad;
131 struct mbuf *m = m0;
132 struct atmllc *atmllc;
133 struct atmllc *llc_hdr = NULL;
134 u_int32_t atm_flags;
135
136#ifdef MAC
137 error = mac_ifnet_check_transmit(ifp, m);
138 if (error)
139 senderr(error);
140#endif
141
142 if (!((ifp->if_flags & IFF_UP) &&
143 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
144 senderr(ENETDOWN);
145
146 /*
147 * check for non-native ATM traffic (dst != NULL)
148 */
149 if (dst) {
150 switch (dst->sa_family) {
151
152#if defined(INET) || defined(INET6)
153 case AF_INET:
154 case AF_INET6:
155 {
156 if (dst->sa_family == AF_INET6)
157 etype = ETHERTYPE_IPV6;
158 else
159 etype = ETHERTYPE_IP;
160 if (!atmresolve(ro->ro_rt, m, dst, &atmdst)) {
161 m = NULL;
162 /* XXX: atmresolve already free'd it */
163 senderr(EHOSTUNREACH);
164 /* XXX: put ATMARP stuff here */
165 /* XXX: watch who frees m on failure */
166 }
167 }
168 break;
169#endif /* INET || INET6 */
170
171 case AF_UNSPEC:
172 /*
173 * XXX: bpfwrite. assuming dst contains 12 bytes
174 * (atm pseudo header (4) + LLC/SNAP (8))
175 */
176 bcopy(dst->sa_data, &atmdst, sizeof(atmdst));
177 llc_hdr = (struct atmllc *)(dst->sa_data +
178 sizeof(atmdst));
179 break;
180
181 default:
182 printf("%s: can't handle af%d\n", ifp->if_xname,
183 dst->sa_family);
184 senderr(EAFNOSUPPORT);
185 }
186
187 /*
188 * must add atm_pseudohdr to data
189 */
190 sz = sizeof(atmdst);
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_DONTWAIT);
195 if (m == 0)
196 senderr(ENOBUFS);
197 ad = mtod(m, struct atm_pseudohdr *);
198 *ad = atmdst;
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);
206 }
207 else
208 bcopy(llc_hdr, atmllc, sizeof(struct atmllc));
209 }
210 }
211
212 if (ng_atm_output_p != NULL) {
213 if ((error = (*ng_atm_output_p)(ifp, &m)) != 0) {
214 if (m != NULL)
215 m_freem(m);
216 return (error);
217 }
218 if (m == NULL)
219 return (0);
220 }
221
222 /*
223 * Queue message on interface, and start output if interface
224 * not yet active.
225 */
226 if (!IF_HANDOFF_ADJ(&ifp->if_snd, m, ifp,
227 -(int)sizeof(struct atm_pseudohdr)))
228 return (ENOBUFS);
229 return (error);
230
231bad:
232 if (m)
233 m_freem(m);
234 return (error);
235}
236
237/*
238 * Process a received ATM packet;
239 * the packet is in the mbuf chain m.
240 */
241void
242atm_input(struct ifnet *ifp, struct atm_pseudohdr *ah, struct mbuf *m,
243 void *rxhand)
244{
245 int isr;
246 u_int16_t etype = ETHERTYPE_IP; /* default */
247
248 if ((ifp->if_flags & IFF_UP) == 0) {
249 m_freem(m);
250 return;
251 }
252#ifdef MAC
253 mac_ifnet_create_mbuf(ifp, m);
254#endif
255 ifp->if_ibytes += m->m_pkthdr.len;
256
257 if (ng_atm_input_p != NULL) {
258 (*ng_atm_input_p)(ifp, &m, ah, rxhand);
259 if (m == NULL)
260 return;
261 }
262
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);
266 if (m == NULL)
267 return;
268 }
269
270 if (rxhand) {
271#ifdef NATM
272 struct natmpcb *npcb;
273
274 /*
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
277 * of spl here.
278 */
279 NATM_LOCK();
280 npcb = rxhand;
281 npcb->npcb_inq++; /* count # in queue */
282 isr = NETISR_NATM;
283 m->m_pkthdr.rcvif = rxhand; /* XXX: overload */
284 NATM_UNLOCK();
285#else
286 printf("atm_input: NATM detected but not "
287 "configured in kernel\n");
288 goto dropit;
289#endif
290 } else {
291 /*
292 * handle LLC/SNAP header, if present
293 */
294 if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) {
295 struct atmllc *alc;
296
297 if (m->m_len < sizeof(*alc) &&
298 (m = m_pullup(m, sizeof(*alc))) == 0)
299 return; /* failed */
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));
305 m_freem(m);
306 return;
307 }
308 etype = ATM_LLC_TYPE(alc);
309 m_adj(m, sizeof(*alc));
310 }
311
312 switch (etype) {
313
314#ifdef INET
315 case ETHERTYPE_IP:
316 isr = NETISR_IP;
317 break;
318#endif
319
320#ifdef INET6
321 case ETHERTYPE_IPV6:
322 isr = NETISR_IPV6;
323 break;
324#endif
325 default:
326#ifndef NATM
327 dropit:
328#endif
329 if (ng_atm_input_orphan_p != NULL)
330 (*ng_atm_input_orphan_p)(ifp, m, ah, rxhand);
331 else
332 m_freem(m);
333 return;
334 }
335 }
336 netisr_dispatch(isr, m);
337}
338
339/*
340 * Perform common duties while attaching to interface list.
341 */
342void
343atm_ifattach(struct ifnet *ifp)
344{
345 struct ifaddr *ifa;
346 struct sockaddr_dl *sdl;
347 struct ifatm *ifatm = ifp->if_l2com;
348
349 ifp->if_addrlen = 0;
350 ifp->if_hdrlen = 0;
351 if_attach(ifp);
352 ifp->if_mtu = ATMMTU;
353 ifp->if_output = atm_output;
354#if 0
355 ifp->if_input = atm_input;
356#endif
357 ifp->if_snd.ifq_maxlen = 50; /* dummy */
358
359 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
360 if (ifa->ifa_addr->sa_family == AF_LINK) {
361 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
362 sdl->sdl_type = IFT_ATM;
363 sdl->sdl_alen = ifp->if_addrlen;
364#ifdef notyet /* if using ATMARP, store hardware address using the next line */
365 bcopy(ifp->hw_addr, LLADDR(sdl), ifp->if_addrlen);
366#endif
367 break;
368 }
369
370 ifp->if_linkmib = &ifatm->mib;
371 ifp->if_linkmiblen = sizeof(ifatm->mib);
372
373 if(ng_atm_attach_p)
374 (*ng_atm_attach_p)(ifp);
375 if (atm_harp_attach_p)
376 (*atm_harp_attach_p)(ifp);
377}
378
379/*
380 * Common stuff for detaching an ATM interface
381 */
382void
383atm_ifdetach(struct ifnet *ifp)
384{
385 if (atm_harp_detach_p)
386 (*atm_harp_detach_p)(ifp);
387 if(ng_atm_detach_p)
388 (*ng_atm_detach_p)(ifp);
389 if_detach(ifp);
390}
391
392/*
393 * Support routine for the SIOCATMGVCCS ioctl().
394 *
395 * This routine assumes, that the private VCC structures used by the driver
396 * begin with a struct atmio_vcc.
397 *
398 * Return a table of VCCs in a freshly allocated memory area.
399 * Here we have a problem: we first count, how many vccs we need
400 * to return. The we allocate the memory and finally fill it in.
401 * Because we cannot lock while calling malloc, the number of active
402 * vccs may change while we're in malloc. So we allocate a couple of
403 * vccs more and if space anyway is not enough re-iterate.
404 *
405 * We could use an sx lock for the vcc tables.
406 */
407struct atmio_vcctable *
408atm_getvccs(struct atmio_vcc **table, u_int size, u_int start,
409 struct mtx *lock, int waitok)
410{
411 u_int cid, alloc;
412 size_t len;
413 struct atmio_vcctable *vccs;
414 struct atmio_vcc *v;
415
416 alloc = start + 10;
417 vccs = NULL;
418
419 for (;;) {
420 len = sizeof(*vccs) + alloc * sizeof(vccs->vccs[0]);
421 vccs = reallocf(vccs, len, M_TEMP,
422 waitok ? M_WAITOK : M_NOWAIT);
423 if (vccs == NULL)
424 return (NULL);
425 bzero(vccs, len);
426
427 vccs->count = 0;
428 v = vccs->vccs;
429
430 mtx_lock(lock);
431 for (cid = 0; cid < size; cid++)
432 if (table[cid] != NULL) {
433 if (++vccs->count == alloc)
434 /* too many - try again */
435 break;
436 *v++ = *table[cid];
437 }
438 mtx_unlock(lock);
439
440 if (cid == size)
441 break;
442
443 alloc *= 2;
444 }
445 return (vccs);
446}
447
448/*
449 * Driver or channel state has changed. Inform whoever is interested
450 * in these events.
451 */
452void
453atm_event(struct ifnet *ifp, u_int event, void *arg)
454{
455 if (ng_atm_event_p != NULL)
456 (*ng_atm_event_p)(ifp, event, arg);
457 if (atm_harp_event_p != NULL)
458 (*atm_harp_event_p)(ifp, event, arg);
459}
460
461static void *
462atm_alloc(u_char type, struct ifnet *ifp)
463{
464 struct ifatm *ifatm;
465
466 ifatm = malloc(sizeof(struct ifatm), M_IFATM, M_WAITOK | M_ZERO);
467 ifatm->ifp = ifp;
468
469 return (ifatm);
470}
471
472static void
473atm_free(void *com, u_char type)
474{
475
476 free(com, M_IFATM);
477}
478
479static int
480atm_modevent(module_t mod, int type, void *data)
481{
482 switch (type) {
483 case MOD_LOAD:
484 if_register_com_alloc(IFT_ATM, atm_alloc, atm_free);
485 break;
486 case MOD_UNLOAD:
487 if_deregister_com_alloc(IFT_ATM);
488 break;
489 default:
490 return (EOPNOTSUPP);
491 }
492
493 return (0);
494}
495
496static moduledata_t atm_mod = {
497 "atm",
498 atm_modevent,
499 0
500};
501
502DECLARE_MODULE(atm, atm_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
503MODULE_VERSION(atm, 1);
42#include "opt_natm.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/module.h>
48#include <sys/mbuf.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>
54
55#include <net/if.h>
56#include <net/netisr.h>
57#include <net/route.h>
58#include <net/if_dl.h>
59#include <net/if_types.h>
60#include <net/if_atm.h>
61
62#include <netinet/in.h>
63#include <netinet/if_atm.h>
64#include <netinet/if_ether.h> /* XXX: for ETHERTYPE_* */
65#if defined(INET) || defined(INET6)
66#include <netinet/in_var.h>
67#endif
68#ifdef NATM
69#include <netnatm/natm.h>
70#endif
71
72#include <security/mac/mac_framework.h>
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_event_p)(struct ifnet *, uint32_t, void *);
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 *);
97void (*atm_harp_event_p)(struct ifnet *, uint32_t, void *);
98
99SYSCTL_NODE(_hw, OID_AUTO, atm, CTLFLAG_RW, 0, "ATM hardware");
100
101MALLOC_DEFINE(M_IFATM, "ifatm", "atm interface internals");
102
103#ifndef ETHERTYPE_IPV6
104#define ETHERTYPE_IPV6 0x86dd
105#endif
106
107#define senderr(e) do { error = (e); goto bad; } while (0)
108
109/*
110 * atm_output: ATM output routine
111 * inputs:
112 * "ifp" = ATM interface to output to
113 * "m0" = the packet to output
114 * "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI)
115 * "ro" = the route to use
116 * returns: error code [0 == ok]
117 *
118 * note: special semantic: if (dst == NULL) then we assume "m" already
119 * has an atm_pseudohdr on it and just send it directly.
120 * [for native mode ATM output] if dst is null, then
121 * ro->ro_rt must also be NULL.
122 */
123int
124atm_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
125 struct route *ro)
126{
127 u_int16_t etype = 0; /* if using LLC/SNAP */
128 int error = 0, sz;
129 struct atm_pseudohdr atmdst, *ad;
130 struct mbuf *m = m0;
131 struct atmllc *atmllc;
132 struct atmllc *llc_hdr = NULL;
133 u_int32_t atm_flags;
134
135#ifdef MAC
136 error = mac_ifnet_check_transmit(ifp, m);
137 if (error)
138 senderr(error);
139#endif
140
141 if (!((ifp->if_flags & IFF_UP) &&
142 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
143 senderr(ENETDOWN);
144
145 /*
146 * check for non-native ATM traffic (dst != NULL)
147 */
148 if (dst) {
149 switch (dst->sa_family) {
150
151#if defined(INET) || defined(INET6)
152 case AF_INET:
153 case AF_INET6:
154 {
155 if (dst->sa_family == AF_INET6)
156 etype = ETHERTYPE_IPV6;
157 else
158 etype = ETHERTYPE_IP;
159 if (!atmresolve(ro->ro_rt, m, dst, &atmdst)) {
160 m = NULL;
161 /* XXX: atmresolve already free'd it */
162 senderr(EHOSTUNREACH);
163 /* XXX: put ATMARP stuff here */
164 /* XXX: watch who frees m on failure */
165 }
166 }
167 break;
168#endif /* INET || INET6 */
169
170 case AF_UNSPEC:
171 /*
172 * XXX: bpfwrite. assuming dst contains 12 bytes
173 * (atm pseudo header (4) + LLC/SNAP (8))
174 */
175 bcopy(dst->sa_data, &atmdst, sizeof(atmdst));
176 llc_hdr = (struct atmllc *)(dst->sa_data +
177 sizeof(atmdst));
178 break;
179
180 default:
181 printf("%s: can't handle af%d\n", ifp->if_xname,
182 dst->sa_family);
183 senderr(EAFNOSUPPORT);
184 }
185
186 /*
187 * must add atm_pseudohdr to data
188 */
189 sz = sizeof(atmdst);
190 atm_flags = ATM_PH_FLAGS(&atmdst);
191 if (atm_flags & ATM_PH_LLCSNAP)
192 sz += 8; /* sizeof snap == 8 */
193 M_PREPEND(m, sz, M_DONTWAIT);
194 if (m == 0)
195 senderr(ENOBUFS);
196 ad = mtod(m, struct atm_pseudohdr *);
197 *ad = atmdst;
198 if (atm_flags & ATM_PH_LLCSNAP) {
199 atmllc = (struct atmllc *)(ad + 1);
200 if (llc_hdr == NULL) {
201 bcopy(ATMLLC_HDR, atmllc->llchdr,
202 sizeof(atmllc->llchdr));
203 /* note: in host order */
204 ATM_LLC_SETTYPE(atmllc, etype);
205 }
206 else
207 bcopy(llc_hdr, atmllc, sizeof(struct atmllc));
208 }
209 }
210
211 if (ng_atm_output_p != NULL) {
212 if ((error = (*ng_atm_output_p)(ifp, &m)) != 0) {
213 if (m != NULL)
214 m_freem(m);
215 return (error);
216 }
217 if (m == NULL)
218 return (0);
219 }
220
221 /*
222 * Queue message on interface, and start output if interface
223 * not yet active.
224 */
225 if (!IF_HANDOFF_ADJ(&ifp->if_snd, m, ifp,
226 -(int)sizeof(struct atm_pseudohdr)))
227 return (ENOBUFS);
228 return (error);
229
230bad:
231 if (m)
232 m_freem(m);
233 return (error);
234}
235
236/*
237 * Process a received ATM packet;
238 * the packet is in the mbuf chain m.
239 */
240void
241atm_input(struct ifnet *ifp, struct atm_pseudohdr *ah, struct mbuf *m,
242 void *rxhand)
243{
244 int isr;
245 u_int16_t etype = ETHERTYPE_IP; /* default */
246
247 if ((ifp->if_flags & IFF_UP) == 0) {
248 m_freem(m);
249 return;
250 }
251#ifdef MAC
252 mac_ifnet_create_mbuf(ifp, m);
253#endif
254 ifp->if_ibytes += m->m_pkthdr.len;
255
256 if (ng_atm_input_p != NULL) {
257 (*ng_atm_input_p)(ifp, &m, ah, rxhand);
258 if (m == NULL)
259 return;
260 }
261
262 /* not eaten by ng_atm. Maybe it's a pseudo-harp PDU? */
263 if (atm_harp_input_p != NULL) {
264 (*atm_harp_input_p)(ifp, &m, ah, rxhand);
265 if (m == NULL)
266 return;
267 }
268
269 if (rxhand) {
270#ifdef NATM
271 struct natmpcb *npcb;
272
273 /*
274 * XXXRW: this use of 'rxhand' is not a very good idea, and
275 * was subject to races even before SMPng due to the release
276 * of spl here.
277 */
278 NATM_LOCK();
279 npcb = rxhand;
280 npcb->npcb_inq++; /* count # in queue */
281 isr = NETISR_NATM;
282 m->m_pkthdr.rcvif = rxhand; /* XXX: overload */
283 NATM_UNLOCK();
284#else
285 printf("atm_input: NATM detected but not "
286 "configured in kernel\n");
287 goto dropit;
288#endif
289 } else {
290 /*
291 * handle LLC/SNAP header, if present
292 */
293 if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) {
294 struct atmllc *alc;
295
296 if (m->m_len < sizeof(*alc) &&
297 (m = m_pullup(m, sizeof(*alc))) == 0)
298 return; /* failed */
299 alc = mtod(m, struct atmllc *);
300 if (bcmp(alc, ATMLLC_HDR, 6)) {
301 printf("%s: recv'd invalid LLC/SNAP frame "
302 "[vp=%d,vc=%d]\n", ifp->if_xname,
303 ATM_PH_VPI(ah), ATM_PH_VCI(ah));
304 m_freem(m);
305 return;
306 }
307 etype = ATM_LLC_TYPE(alc);
308 m_adj(m, sizeof(*alc));
309 }
310
311 switch (etype) {
312
313#ifdef INET
314 case ETHERTYPE_IP:
315 isr = NETISR_IP;
316 break;
317#endif
318
319#ifdef INET6
320 case ETHERTYPE_IPV6:
321 isr = NETISR_IPV6;
322 break;
323#endif
324 default:
325#ifndef NATM
326 dropit:
327#endif
328 if (ng_atm_input_orphan_p != NULL)
329 (*ng_atm_input_orphan_p)(ifp, m, ah, rxhand);
330 else
331 m_freem(m);
332 return;
333 }
334 }
335 netisr_dispatch(isr, m);
336}
337
338/*
339 * Perform common duties while attaching to interface list.
340 */
341void
342atm_ifattach(struct ifnet *ifp)
343{
344 struct ifaddr *ifa;
345 struct sockaddr_dl *sdl;
346 struct ifatm *ifatm = ifp->if_l2com;
347
348 ifp->if_addrlen = 0;
349 ifp->if_hdrlen = 0;
350 if_attach(ifp);
351 ifp->if_mtu = ATMMTU;
352 ifp->if_output = atm_output;
353#if 0
354 ifp->if_input = atm_input;
355#endif
356 ifp->if_snd.ifq_maxlen = 50; /* dummy */
357
358 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
359 if (ifa->ifa_addr->sa_family == AF_LINK) {
360 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
361 sdl->sdl_type = IFT_ATM;
362 sdl->sdl_alen = ifp->if_addrlen;
363#ifdef notyet /* if using ATMARP, store hardware address using the next line */
364 bcopy(ifp->hw_addr, LLADDR(sdl), ifp->if_addrlen);
365#endif
366 break;
367 }
368
369 ifp->if_linkmib = &ifatm->mib;
370 ifp->if_linkmiblen = sizeof(ifatm->mib);
371
372 if(ng_atm_attach_p)
373 (*ng_atm_attach_p)(ifp);
374 if (atm_harp_attach_p)
375 (*atm_harp_attach_p)(ifp);
376}
377
378/*
379 * Common stuff for detaching an ATM interface
380 */
381void
382atm_ifdetach(struct ifnet *ifp)
383{
384 if (atm_harp_detach_p)
385 (*atm_harp_detach_p)(ifp);
386 if(ng_atm_detach_p)
387 (*ng_atm_detach_p)(ifp);
388 if_detach(ifp);
389}
390
391/*
392 * Support routine for the SIOCATMGVCCS ioctl().
393 *
394 * This routine assumes, that the private VCC structures used by the driver
395 * begin with a struct atmio_vcc.
396 *
397 * Return a table of VCCs in a freshly allocated memory area.
398 * Here we have a problem: we first count, how many vccs we need
399 * to return. The we allocate the memory and finally fill it in.
400 * Because we cannot lock while calling malloc, the number of active
401 * vccs may change while we're in malloc. So we allocate a couple of
402 * vccs more and if space anyway is not enough re-iterate.
403 *
404 * We could use an sx lock for the vcc tables.
405 */
406struct atmio_vcctable *
407atm_getvccs(struct atmio_vcc **table, u_int size, u_int start,
408 struct mtx *lock, int waitok)
409{
410 u_int cid, alloc;
411 size_t len;
412 struct atmio_vcctable *vccs;
413 struct atmio_vcc *v;
414
415 alloc = start + 10;
416 vccs = NULL;
417
418 for (;;) {
419 len = sizeof(*vccs) + alloc * sizeof(vccs->vccs[0]);
420 vccs = reallocf(vccs, len, M_TEMP,
421 waitok ? M_WAITOK : M_NOWAIT);
422 if (vccs == NULL)
423 return (NULL);
424 bzero(vccs, len);
425
426 vccs->count = 0;
427 v = vccs->vccs;
428
429 mtx_lock(lock);
430 for (cid = 0; cid < size; cid++)
431 if (table[cid] != NULL) {
432 if (++vccs->count == alloc)
433 /* too many - try again */
434 break;
435 *v++ = *table[cid];
436 }
437 mtx_unlock(lock);
438
439 if (cid == size)
440 break;
441
442 alloc *= 2;
443 }
444 return (vccs);
445}
446
447/*
448 * Driver or channel state has changed. Inform whoever is interested
449 * in these events.
450 */
451void
452atm_event(struct ifnet *ifp, u_int event, void *arg)
453{
454 if (ng_atm_event_p != NULL)
455 (*ng_atm_event_p)(ifp, event, arg);
456 if (atm_harp_event_p != NULL)
457 (*atm_harp_event_p)(ifp, event, arg);
458}
459
460static void *
461atm_alloc(u_char type, struct ifnet *ifp)
462{
463 struct ifatm *ifatm;
464
465 ifatm = malloc(sizeof(struct ifatm), M_IFATM, M_WAITOK | M_ZERO);
466 ifatm->ifp = ifp;
467
468 return (ifatm);
469}
470
471static void
472atm_free(void *com, u_char type)
473{
474
475 free(com, M_IFATM);
476}
477
478static int
479atm_modevent(module_t mod, int type, void *data)
480{
481 switch (type) {
482 case MOD_LOAD:
483 if_register_com_alloc(IFT_ATM, atm_alloc, atm_free);
484 break;
485 case MOD_UNLOAD:
486 if_deregister_com_alloc(IFT_ATM);
487 break;
488 default:
489 return (EOPNOTSUPP);
490 }
491
492 return (0);
493}
494
495static moduledata_t atm_mod = {
496 "atm",
497 atm_modevent,
498 0
499};
500
501DECLARE_MODULE(atm, atm_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
502MODULE_VERSION(atm, 1);