2 * Copyright (c) 1995, 1996
3 * Matt Thomas <matt@3am-software.com>. All rights reserved.
4 * Copyright (c) 1982, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * from: if_ethersubr.c,v 1.5 1994/12/13 22:31:45 wollman Exp
36 * $FreeBSD: src/sys/net/if_fddisubr.c,v 1.41.2.8 2002/02/20 23:34:09 fjoe Exp $
37 * $DragonFly: src/sys/net/Attic/if_fddisubr.c,v 1.7 2004/01/06 03:17:25 dillon Exp $
40 #include "opt_atalk.h"
42 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/socket.h>
49 #include <sys/malloc.h>
52 #include <net/netisr.h>
53 #include <net/route.h>
54 #include <net/if_llc.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
58 #if defined(INET) || defined(INET6)
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/if_ether.h>
64 #include <netinet6/nd6.h>
66 #if defined(__FreeBSD__)
67 #include <netinet/if_fddi.h>
69 #include <net/if_fddi.h>
73 #include <netproto/ipx/ipx.h>
74 #include <netproto/ipx/ipx_if.h>
79 #include <netns/ns_if.h>
83 #include <netdnet/dn.h>
87 #include <netproto/atalk/at.h>
88 #include <netproto/atalk/at_var.h>
89 #include <netproto/atalk/at_extern.h>
91 #define llc_snap_org_code llc_un.type_snap.org_code
92 #define llc_snap_ether_type llc_un.type_snap.ether_type
94 extern u_char at_org_code[ 3 ];
95 extern u_char aarp_org_code[ 3 ];
98 static int fddi_resolvemulti (struct ifnet *, struct sockaddr **,
101 #define senderr(e) { error = (e); goto bad;}
104 * This really should be defined in if_llc.h but in case it isn't.
107 #define llc_snap llc_un.type_snap
110 #if defined(__bsdi__) || defined(__NetBSD__)
111 #define RTALLOC1(a, b) rtalloc1(a, b)
112 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e)
113 #elif defined(__FreeBSD__)
114 #define RTALLOC1(a, b) rtalloc1(a, b, 0UL)
115 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e, f)
118 * FDDI output routine.
119 * Encapsulate a packet of type family for the local net.
120 * Use trailer local net encapsulation if enough data in first
121 * packet leaves a multiple of 512 bytes of data in remainder.
122 * Assumes that ifp is actually pointer to arpcom structure.
125 fddi_output(ifp, m, dst, rt0)
128 struct sockaddr *dst;
132 int s, loop_copy = 0, error = 0, hdrcmplt = 0;
133 u_char esrc[6], edst[6];
135 struct fddi_header *fh;
136 struct arpcom *ac = (struct arpcom *)ifp;
138 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
140 getmicrotime(&ifp->if_lastchange);
141 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
142 if ((rt = rt0) != NULL) {
143 if ((rt->rt_flags & RTF_UP) == 0) {
144 if ((rt0 = rt = RTALLOC1(dst, 1)) != NULL)
147 senderr(EHOSTUNREACH);
149 if (rt->rt_flags & RTF_GATEWAY) {
150 if (rt->rt_gwroute == 0)
152 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
153 rtfree(rt); rt = rt0;
154 lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 1);
155 if ((rt = rt->rt_gwroute) == 0)
156 senderr(EHOSTUNREACH);
159 if (rt->rt_flags & RTF_REJECT)
160 if (rt->rt_rmx.rmx_expire == 0 ||
161 time_second < rt->rt_rmx.rmx_expire)
162 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
165 switch (dst->sa_family) {
169 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
170 if (!ARPRESOLVE(ifp, rt, m, dst, edst, rt0))
171 return (0); /* if not yet resolved */
174 if (!arpresolve(ac, m, &((struct sockaddr_in *)dst)->sin_addr, edst, &usetrailers))
175 return (0); /* if not yet resolved */
177 type = htons(ETHERTYPE_IP);
183 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
184 /* Something bad happened */
187 type = htons(ETHERTYPE_IPV6);
192 type = htons(ETHERTYPE_IPX);
193 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
194 (caddr_t)edst, sizeof (edst));
199 struct at_ifaddr *aa;
200 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
203 * ifaddr is the first thing in at_ifaddr
205 if ((aa = at_ifawithnet( (struct sockaddr_at *)dst)) == 0)
209 * In the phase 2 case, we need to prepend an mbuf for the llc header.
210 * Since we must preserve the value of m, which is passed to us by
211 * value, we m_copy() the first mbuf, and use it for our llc header.
213 if (aa->aa_flags & AFA_PHASE2) {
216 M_PREPEND(m, sizeof(struct llc), M_WAIT);
219 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
220 llc.llc_control = LLC_UI;
221 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
222 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
223 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
226 type = htons(ETHERTYPE_AT);
230 #endif /* NETATALK */
233 type = htons(ETHERTYPE_NS);
234 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
235 (caddr_t)edst, sizeof (edst));
239 case pseudo_AF_HDRCMPLT:
241 struct ether_header *eh;
243 eh = (struct ether_header *)dst->sa_data;
244 (void)memcpy((caddr_t)esrc, (caddr_t)eh->ether_shost, sizeof (esrc));
250 struct ether_header *eh;
252 eh = (struct ether_header *)dst->sa_data;
253 (void)memcpy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
255 m->m_flags |= (M_BCAST|M_MCAST);
256 type = eh->ether_type;
262 fh = mtod(m, struct fddi_header *);
263 error = EPROTONOSUPPORT;
264 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
265 case FDDIFC_LLC_ASYNC: {
266 /* legal priorities are 0 through 7 */
267 if ((fh->fddi_fc & FDDIFC_Z) > 7)
271 case FDDIFC_LLC_SYNC: {
272 /* FDDIFC_Z bits reserved, must be zero */
273 if (fh->fddi_fc & FDDIFC_Z)
278 /* FDDIFC_Z bits must be non zero */
279 if ((fh->fddi_fc & FDDIFC_Z) == 0)
284 /* anything else is too dangerous */
289 if (fh->fddi_dhost[0] & 1)
290 m->m_flags |= (M_BCAST|M_MCAST);
294 printf("%s: can't handle af%d\n", ifp->if_xname,
296 senderr(EAFNOSUPPORT);
301 M_PREPEND(m, sizeof (struct llc), M_DONTWAIT);
304 l = mtod(m, struct llc *);
305 l->llc_control = LLC_UI;
306 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
307 l->llc_snap.org_code[0] = l->llc_snap.org_code[1] = l->llc_snap.org_code[2] = 0;
308 (void)memcpy((caddr_t) &l->llc_snap.ether_type, (caddr_t) &type,
313 * Add local net header. If no space in first mbuf,
316 M_PREPEND(m, sizeof (struct fddi_header), M_DONTWAIT);
319 fh = mtod(m, struct fddi_header *);
320 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
321 (void)memcpy((caddr_t)fh->fddi_dhost, (caddr_t)edst, sizeof (edst));
324 (void)memcpy((caddr_t)fh->fddi_shost, (caddr_t)esrc,
325 sizeof(fh->fddi_shost));
327 (void)memcpy((caddr_t)fh->fddi_shost, (caddr_t)ac->ac_enaddr,
328 sizeof(fh->fddi_shost));
330 * If a simplex interface, and the packet is being sent to our
331 * Ethernet address or a broadcast address, loopback a copy.
332 * XXX To make a simplex device behave exactly like a duplex
333 * device, we should copy in the case of sending to our own
334 * ethernet address (thus letting the original actually appear
335 * on the wire). However, we don't do that here for security
336 * reasons and compatibility with the original behavior.
338 if ((ifp->if_flags & IFF_SIMPLEX) &&
340 if ((m->m_flags & M_BCAST) || loop_copy) {
341 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
343 (void) if_simloop(ifp,
344 n, dst->sa_family, sizeof(struct fddi_header));
345 } else if (bcmp(fh->fddi_dhost,
346 fh->fddi_shost, sizeof(fh->fddi_shost)) == 0) {
347 (void) if_simloop(ifp,
348 m, dst->sa_family, sizeof(struct fddi_header));
355 * Queue message on interface, and start output if interface
358 if (IF_QFULL(&ifp->if_snd)) {
359 IF_DROP(&ifp->if_snd);
363 ifp->if_obytes += m->m_pkthdr.len;
364 if (m->m_flags & M_MCAST)
366 IF_ENQUEUE(&ifp->if_snd, m);
367 if ((ifp->if_flags & IFF_OACTIVE) == 0)
368 (*ifp->if_start)(ifp);
380 * Process a received FDDI packet;
381 * the packet is in the mbuf chain m without
382 * the fddi header, which is provided separately.
385 fddi_input(ifp, fh, m)
387 struct fddi_header *fh;
393 if ((ifp->if_flags & IFF_UP) == 0) {
397 getmicrotime(&ifp->if_lastchange);
398 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*fh);
399 if (fh->fddi_dhost[0] & 1) {
400 if (bcmp((caddr_t)fddibroadcastaddr, (caddr_t)fh->fddi_dhost,
401 sizeof(fddibroadcastaddr)) == 0)
402 m->m_flags |= M_BCAST;
404 m->m_flags |= M_MCAST;
406 } else if ((ifp->if_flags & IFF_PROMISC) &&
407 bcmp(((struct arpcom *)ifp)->ac_enaddr, (caddr_t)fh->fddi_dhost,
408 sizeof(fh->fddi_dhost)) != 0) {
415 * If this has a LLC priority of 0, then mark it so upper
416 * layers have a hint that it really came via a FDDI/Ethernet
419 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
420 m->m_flags |= M_LINK0;
423 l = mtod(m, struct llc *);
424 switch (l->llc_dsap) {
425 #if defined(INET) || defined(INET6) || defined(NS) || defined(DECNET) || defined(IPX) || defined(NETATALK)
429 if (l->llc_control != LLC_UI || l->llc_ssap != LLC_SNAP_LSAP)
432 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
433 sizeof(at_org_code)) == 0 &&
434 sizeof(at_org_code) == 0 &&
435 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
436 m_adj(m, sizeof(struct llc));
441 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
442 sizeof(aarp_org_code)) == 0 &&
443 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
444 m_adj( m, sizeof( struct llc ));
448 #endif /* NETATALK */
449 if (l->llc_snap.org_code[0] != 0 || l->llc_snap.org_code[1] != 0|| l->llc_snap.org_code[2] != 0)
451 type = ntohs(l->llc_snap.ether_type);
456 if (ipflow_fastforward(m))
462 if (ifp->if_flags & IFF_NOARP)
483 case ETHERTYPE_DECNET:
494 #endif /* NETATALK */
496 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
502 #endif /* INET || NS */
505 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
512 netisr_dispatch(isr, m);
516 * Perform common duties while attaching to interface list
519 #define ifa_next ifa_list.tqe_next
527 struct sockaddr_dl *sdl;
529 ifp->if_type = IFT_FDDI;
532 ifp->if_mtu = FDDIMTU;
533 ifp->if_resolvemulti = fddi_resolvemulti;
534 ifp->if_baudrate = 100000000;
535 #ifdef IFF_NOTRAILERS
536 ifp->if_flags |= IFF_NOTRAILERS;
538 #if defined(__FreeBSD__)
539 ifa = ifnet_addrs[ifp->if_index - 1];
540 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
541 sdl->sdl_type = IFT_FDDI;
542 sdl->sdl_alen = ifp->if_addrlen;
543 bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
544 #elif defined(__NetBSD__)
545 LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs);
546 for (ifa = ifp->if_addrlist.tqh_first; ifa != NULL; ifa = ifa->ifa_list.tqe_next)
548 for (ifa = ifp->if_addrlist; ifa != NULL; ifa = ifa->ifa_next)
550 #if !defined(__FreeBSD__)
551 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
552 sdl->sdl_family == AF_LINK) {
553 sdl->sdl_type = IFT_FDDI;
554 sdl->sdl_alen = ifp->if_addrlen;
555 bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
556 LLADDR(sdl), ifp->if_addrlen);
563 fddi_resolvemulti(ifp, llsa, sa)
565 struct sockaddr **llsa;
568 struct sockaddr_dl *sdl;
569 struct sockaddr_in *sin;
571 struct sockaddr_in6 *sin6;
575 switch(sa->sa_family) {
578 * No mapping needed. Just check that it's a valid MC address.
580 sdl = (struct sockaddr_dl *)sa;
581 e_addr = LLADDR(sdl);
582 if ((e_addr[0] & 1) != 1)
583 return EADDRNOTAVAIL;
589 sin = (struct sockaddr_in *)sa;
590 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
591 return EADDRNOTAVAIL;
592 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
594 sdl->sdl_len = sizeof *sdl;
595 sdl->sdl_family = AF_LINK;
596 sdl->sdl_index = ifp->if_index;
597 sdl->sdl_type = IFT_FDDI;
599 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
601 e_addr = LLADDR(sdl);
602 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
603 *llsa = (struct sockaddr *)sdl;
608 sin6 = (struct sockaddr_in6 *)sa;
609 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
611 * An IP6 address of 0 means listen to all
612 * of the Ethernet multicast address used for IP6.
613 * (This is used for multicast routers.)
615 ifp->if_flags |= IFF_ALLMULTI;
619 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
620 return EADDRNOTAVAIL;
621 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
623 sdl->sdl_len = sizeof *sdl;
624 sdl->sdl_family = AF_LINK;
625 sdl->sdl_index = ifp->if_index;
626 sdl->sdl_type = IFT_FDDI;
628 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
630 e_addr = LLADDR(sdl);
631 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
632 *llsa = (struct sockaddr *)sdl;
638 * Well, the text isn't quite right, but it's the name