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