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.11 2004/07/23 07:16:30 joerg 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>
53 #include <net/netisr.h>
54 #include <net/route.h>
55 #include <net/if_llc.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
59 #if defined(INET) || defined(INET6)
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <netinet/if_ether.h>
63 #include <net/ethernet.h>
66 #include <netinet6/nd6.h>
68 #if defined(__DragonFly__) || defined(__FreeBSD__)
69 #include <netinet/if_fddi.h>
71 #include <net/if_fddi.h>
75 #include <netproto/ipx/ipx.h>
76 #include <netproto/ipx/ipx_if.h>
81 #include <netns/ns_if.h>
85 #include <netdnet/dn.h>
89 #include <netproto/atalk/at.h>
90 #include <netproto/atalk/at_var.h>
91 #include <netproto/atalk/at_extern.h>
93 #define llc_snap_org_code llc_un.type_snap.org_code
94 #define llc_snap_ether_type llc_un.type_snap.ether_type
96 extern u_char at_org_code[ 3 ];
97 extern u_char aarp_org_code[ 3 ];
100 static int fddi_resolvemulti (struct ifnet *, struct sockaddr **,
102 static void fddi_input(struct ifnet *, struct mbuf *);
103 static int fddi_output(struct ifnet *, struct mbuf *, struct sockaddr *,
106 #define senderr(e) { error = (e); goto bad;}
109 * This really should be defined in if_llc.h but in case it isn't.
112 #define llc_snap llc_un.type_snap
115 #if defined(__bsdi__) || defined(__NetBSD__)
116 #define RTALLOC1(a, b) rtalloc1(a, b)
117 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e)
118 #elif defined(__DragonFly__) || defined(__FreeBSD__)
119 #define RTALLOC1(a, b) rtalloc1(a, b, 0UL)
120 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e, f)
123 * FDDI output routine.
124 * Encapsulate a packet of type family for the local net.
125 * Use trailer local net encapsulation if enough data in first
126 * packet leaves a multiple of 512 bytes of data in remainder.
127 * Assumes that ifp is actually pointer to arpcom structure.
130 fddi_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
134 int s, loop_copy = 0, error = 0, hdrcmplt = 0;
135 u_char esrc[6], edst[6];
137 struct fddi_header *fh;
138 struct arpcom *ac = (struct arpcom *)ifp;
140 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
142 getmicrotime(&ifp->if_lastchange);
143 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
144 if ((rt = rt0) != NULL) {
145 if ((rt->rt_flags & RTF_UP) == 0) {
146 if ((rt0 = rt = RTALLOC1(dst, 1)) != NULL)
149 senderr(EHOSTUNREACH);
151 if (rt->rt_flags & RTF_GATEWAY) {
152 if (rt->rt_gwroute == 0)
154 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
155 rtfree(rt); rt = rt0;
156 lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 1);
157 if ((rt = rt->rt_gwroute) == 0)
158 senderr(EHOSTUNREACH);
161 if (rt->rt_flags & RTF_REJECT)
162 if (rt->rt_rmx.rmx_expire == 0 ||
163 time_second < rt->rt_rmx.rmx_expire)
164 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
167 switch (dst->sa_family) {
171 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
172 if (!ARPRESOLVE(ifp, rt, m, dst, edst, rt0))
173 return (0); /* if not yet resolved */
176 if (!arpresolve(ac, m, &((struct sockaddr_in *)dst)->sin_addr, edst, &usetrailers))
177 return (0); /* if not yet resolved */
179 type = htons(ETHERTYPE_IP);
185 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
186 /* Something bad happened */
189 type = htons(ETHERTYPE_IPV6);
194 type = htons(ETHERTYPE_IPX);
195 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
196 (caddr_t)edst, sizeof (edst));
201 struct at_ifaddr *aa;
202 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
205 * ifaddr is the first thing in at_ifaddr
207 if ((aa = at_ifawithnet( (struct sockaddr_at *)dst)) == 0)
211 * In the phase 2 case, we need to prepend an mbuf for the llc header.
212 * Since we must preserve the value of m, which is passed to us by
213 * value, we m_copy() the first mbuf, and use it for our llc header.
215 if (aa->aa_flags & AFA_PHASE2) {
218 M_PREPEND(m, sizeof(struct llc), MB_WAIT);
221 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
222 llc.llc_control = LLC_UI;
223 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
224 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
225 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
228 type = htons(ETHERTYPE_AT);
232 #endif /* NETATALK */
235 type = htons(ETHERTYPE_NS);
236 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
237 (caddr_t)edst, sizeof (edst));
241 case pseudo_AF_HDRCMPLT:
243 struct ether_header *eh;
245 eh = (struct ether_header *)dst->sa_data;
246 (void)memcpy((caddr_t)esrc, (caddr_t)eh->ether_shost, sizeof (esrc));
252 struct ether_header *eh;
254 eh = (struct ether_header *)dst->sa_data;
255 (void)memcpy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
257 m->m_flags |= (M_BCAST|M_MCAST);
258 type = eh->ether_type;
264 fh = mtod(m, struct fddi_header *);
265 error = EPROTONOSUPPORT;
266 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
267 case FDDIFC_LLC_ASYNC: {
268 /* legal priorities are 0 through 7 */
269 if ((fh->fddi_fc & FDDIFC_Z) > 7)
273 case FDDIFC_LLC_SYNC: {
274 /* FDDIFC_Z bits reserved, must be zero */
275 if (fh->fddi_fc & FDDIFC_Z)
280 /* FDDIFC_Z bits must be non zero */
281 if ((fh->fddi_fc & FDDIFC_Z) == 0)
286 /* anything else is too dangerous */
291 if (fh->fddi_dhost[0] & 1)
292 m->m_flags |= (M_BCAST|M_MCAST);
296 printf("%s: can't handle af%d\n", ifp->if_xname,
298 senderr(EAFNOSUPPORT);
303 M_PREPEND(m, sizeof (struct llc), MB_DONTWAIT);
306 l = mtod(m, struct llc *);
307 l->llc_control = LLC_UI;
308 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
309 l->llc_snap.org_code[0] = l->llc_snap.org_code[1] = l->llc_snap.org_code[2] = 0;
310 (void)memcpy((caddr_t) &l->llc_snap.ether_type, (caddr_t) &type,
315 * Add local net header. If no space in first mbuf,
318 M_PREPEND(m, sizeof (struct fddi_header), MB_DONTWAIT);
321 fh = mtod(m, struct fddi_header *);
322 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
323 (void)memcpy((caddr_t)fh->fddi_dhost, (caddr_t)edst, sizeof (edst));
326 (void)memcpy((caddr_t)fh->fddi_shost, (caddr_t)esrc,
327 sizeof(fh->fddi_shost));
329 (void)memcpy((caddr_t)fh->fddi_shost, (caddr_t)ac->ac_enaddr,
330 sizeof(fh->fddi_shost));
332 * If a simplex interface, and the packet is being sent to our
333 * Ethernet address or a broadcast address, loopback a copy.
334 * XXX To make a simplex device behave exactly like a duplex
335 * device, we should copy in the case of sending to our own
336 * ethernet address (thus letting the original actually appear
337 * on the wire). However, we don't do that here for security
338 * reasons and compatibility with the original behavior.
340 if ((ifp->if_flags & IFF_SIMPLEX) &&
342 if ((m->m_flags & M_BCAST) || loop_copy) {
343 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
345 (void) if_simloop(ifp,
346 n, dst->sa_family, sizeof(struct fddi_header));
347 } else if (bcmp(fh->fddi_dhost,
348 fh->fddi_shost, sizeof(fh->fddi_shost)) == 0) {
349 (void) if_simloop(ifp,
350 m, dst->sa_family, sizeof(struct fddi_header));
357 * Queue message on interface, and start output if interface
360 if (IF_QFULL(&ifp->if_snd)) {
361 IF_DROP(&ifp->if_snd);
365 ifp->if_obytes += m->m_pkthdr.len;
366 if (m->m_flags & M_MCAST)
368 IF_ENQUEUE(&ifp->if_snd, m);
369 if ((ifp->if_flags & IFF_OACTIVE) == 0)
370 (*ifp->if_start)(ifp);
382 * Process a received FDDI packet;
383 * the packet is in the mbuf chain m without
384 * the fddi header, which is provided separately.
387 fddi_input(struct ifnet *ifp, struct mbuf *m)
391 struct fddi_header *fh = mtod(m, struct fddi_header *);
393 if (m->m_len < sizeof(struct fddi_header)) {
394 /* XXX error in the caller. */
398 m_adj(m, sizeof(struct fddi_header));
399 m->m_pkthdr.rcvif = ifp;
401 if ((ifp->if_flags & IFF_UP) == 0) {
405 getmicrotime(&ifp->if_lastchange);
406 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*fh);
407 if (fh->fddi_dhost[0] & 1) {
408 if (bcmp(ifp->if_broadcastaddr, fh->fddi_dhost,
409 ifp->if_addrlen) == 0)
410 m->m_flags |= M_BCAST;
412 m->m_flags |= M_MCAST;
414 } else if ((ifp->if_flags & IFF_PROMISC) &&
415 bcmp(((struct arpcom *)ifp)->ac_enaddr, (caddr_t)fh->fddi_dhost,
416 sizeof(fh->fddi_dhost)) != 0) {
423 * If this has a LLC priority of 0, then mark it so upper
424 * layers have a hint that it really came via a FDDI/Ethernet
427 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
428 m->m_flags |= M_LINK0;
431 l = mtod(m, struct llc *);
432 switch (l->llc_dsap) {
433 #if defined(INET) || defined(INET6) || defined(NS) || defined(DECNET) || defined(IPX) || defined(NETATALK)
437 if (l->llc_control != LLC_UI || l->llc_ssap != LLC_SNAP_LSAP)
440 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
441 sizeof(at_org_code)) == 0 &&
442 sizeof(at_org_code) == 0 &&
443 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
444 m_adj(m, sizeof(struct llc));
449 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
450 sizeof(aarp_org_code)) == 0 &&
451 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
452 m_adj( m, sizeof( struct llc ));
456 #endif /* NETATALK */
457 if (l->llc_snap.org_code[0] != 0 || l->llc_snap.org_code[1] != 0|| l->llc_snap.org_code[2] != 0)
459 type = ntohs(l->llc_snap.ether_type);
464 if (ipflow_fastforward(m))
470 if (ifp->if_flags & IFF_NOARP)
491 case ETHERTYPE_DECNET:
502 #endif /* NETATALK */
504 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
510 #endif /* INET || NS */
513 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
520 netisr_dispatch(isr, m);
524 * Perform common duties while attaching to interface list
527 #define ifa_next ifa_list.tqe_next
535 struct sockaddr_dl *sdl;
537 ifp->if_input = fddi_input;
538 ifp->if_output = fddi_output;
539 ifp->if_type = IFT_FDDI;
541 ifp->if_broadcastaddr = fddibroadcastaddr;
543 ifp->if_mtu = FDDIMTU;
544 ifp->if_resolvemulti = fddi_resolvemulti;
545 ifp->if_baudrate = 100000000;
546 #ifdef IFF_NOTRAILERS
547 ifp->if_flags |= IFF_NOTRAILERS;
550 #if defined(__DragonFly__) || defined(__FreeBSD__)
551 ifa = ifnet_addrs[ifp->if_index - 1];
552 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
553 sdl->sdl_type = IFT_FDDI;
554 sdl->sdl_alen = ifp->if_addrlen;
555 bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
556 #elif defined(__NetBSD__)
557 LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs);
558 for (ifa = ifp->if_addrlist.tqh_first; ifa != NULL; ifa = ifa->ifa_list.tqe_next)
560 for (ifa = ifp->if_addrlist; ifa != NULL; ifa = ifa->ifa_next)
562 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
563 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
564 sdl->sdl_family == AF_LINK) {
565 sdl->sdl_type = IFT_FDDI;
566 sdl->sdl_alen = ifp->if_addrlen;
567 bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
568 LLADDR(sdl), ifp->if_addrlen);
572 bpfattach(ifp, DLT_FDDI, sizeof(struct fddi_header));
576 fddi_resolvemulti(ifp, llsa, sa)
578 struct sockaddr **llsa;
581 struct sockaddr_dl *sdl;
582 struct sockaddr_in *sin;
584 struct sockaddr_in6 *sin6;
588 switch(sa->sa_family) {
591 * No mapping needed. Just check that it's a valid MC address.
593 sdl = (struct sockaddr_dl *)sa;
594 e_addr = LLADDR(sdl);
595 if ((e_addr[0] & 1) != 1)
596 return EADDRNOTAVAIL;
602 sin = (struct sockaddr_in *)sa;
603 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
604 return EADDRNOTAVAIL;
605 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
607 sdl->sdl_len = sizeof *sdl;
608 sdl->sdl_family = AF_LINK;
609 sdl->sdl_index = ifp->if_index;
610 sdl->sdl_type = IFT_FDDI;
612 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
614 e_addr = LLADDR(sdl);
615 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
616 *llsa = (struct sockaddr *)sdl;
621 sin6 = (struct sockaddr_in6 *)sa;
622 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
624 * An IP6 address of 0 means listen to all
625 * of the Ethernet multicast address used for IP6.
626 * (This is used for multicast routers.)
628 ifp->if_flags |= IFF_ALLMULTI;
632 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
633 return EADDRNOTAVAIL;
634 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
636 sdl->sdl_len = sizeof *sdl;
637 sdl->sdl_family = AF_LINK;
638 sdl->sdl_index = ifp->if_index;
639 sdl->sdl_type = IFT_FDDI;
641 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
643 e_addr = LLADDR(sdl);
644 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
645 *llsa = (struct sockaddr *)sdl;
651 * Well, the text isn't quite right, but it's the name