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.14 2005/01/06 09:14:13 hsu 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(__DragonFly__)
116 #define RTALLOC1(a, b) rtlookup(a, b, 0UL)
117 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e)
118 #elif 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,
133 struct arpcom *ac = (struct arpcom *)ifp;
135 u_char esrc[6], edst[6];
136 struct fddi_header *fh;
137 boolean_t hdrcmplt = FALSE;
138 int s, loop_copy = 0, error;
140 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
143 switch (dst->sa_family) {
146 if (!arpresolve(ifp, rt, m, dst, edst))
147 return (0); /* if not yet resolved */
148 type = htons(ETHERTYPE_IP);
154 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
155 /* Something bad happened */
158 type = htons(ETHERTYPE_IPV6);
163 type = htons(ETHERTYPE_IPX);
164 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), edst,
170 struct at_ifaddr *aa;
172 if (!aarpresolve((struct arpcom *) ifp, m,
173 (struct sockaddr_at *)dst, edst))
177 * ifaddr is the first thing in at_ifaddr
179 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
185 * In the phase 2 case, we need to prepend an mbuf
186 * for the llc header. Since we must preserve the
187 * value of m, which is passed to us by value, we
188 * m_copy() the first mbuf, and use it for our llc
191 if (aa->aa_flags & AFA_PHASE2) {
194 M_PREPEND(m, sizeof(struct llc), MB_WAIT);
197 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
198 llc.llc_control = LLC_UI;
199 bcopy(at_org_code, llc.llc_snap_org_code,
201 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
202 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
205 type = htons(ETHERTYPE_AT);
209 #endif /* NETATALK */
212 type = htons(ETHERTYPE_NS);
213 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
218 case pseudo_AF_HDRCMPLT:
220 struct ether_header *eh;
223 eh = (struct ether_header *)dst->sa_data;
224 memcpy(esrc, eh->ether_shost, sizeof esrc);
230 struct ether_header *eh;
233 eh = (struct ether_header *)dst->sa_data;
234 memcpy(edst, eh->ether_dhost, sizeof edst);
236 m->m_flags |= (M_BCAST|M_MCAST);
237 type = eh->ether_type;
243 fh = mtod(m, struct fddi_header *);
244 error = EPROTONOSUPPORT;
245 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
246 case FDDIFC_LLC_ASYNC: {
247 /* legal priorities are 0 through 7 */
248 if ((fh->fddi_fc & FDDIFC_Z) > 7)
252 case FDDIFC_LLC_SYNC: {
253 /* FDDIFC_Z bits reserved, must be zero */
254 if (fh->fddi_fc & FDDIFC_Z)
259 /* FDDIFC_Z bits must be non zero */
260 if ((fh->fddi_fc & FDDIFC_Z) == 0)
265 /* anything else is too dangerous */
270 if (fh->fddi_dhost[0] & 1)
271 m->m_flags |= (M_BCAST | M_MCAST);
275 printf("%s: can't handle af%d\n", ifp->if_xname,
277 senderr(EAFNOSUPPORT);
283 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
286 l = mtod(m, struct llc *);
287 l->llc_control = LLC_UI;
288 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
289 l->llc_snap.org_code[0] = l->llc_snap.org_code[1] =
290 l->llc_snap.org_code[2] = 0;
291 memcpy(&l->llc_snap.ether_type, &type, sizeof(u_int16_t));
295 * Add local net header. If no space in first mbuf,
298 M_PREPEND(m, sizeof(struct fddi_header), MB_DONTWAIT);
301 fh = mtod(m, struct fddi_header *);
302 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
303 memcpy(fh->fddi_dhost, edst, sizeof edst);
307 memcpy(fh->fddi_shost, esrc, sizeof fh->fddi_shost);
309 memcpy(fh->fddi_shost, ac->ac_enaddr, sizeof fh->fddi_shost);
311 memcpy(fh->fddi_shost, ac->ac_enaddr, sizeof fh->fddi_shost);
312 * Ethernet address or a broadcast address, loopback a copy.
313 * XXX To make a simplex device behave exactly like a duplex
314 * device, we should copy in the case of sending to our own
315 * ethernet address (thus letting the original actually appear
316 * on the wire). However, we don't do that here for security
317 * reasons and compatibility with the original behavior.
319 if ((ifp->if_flags & IFF_SIMPLEX) &&
321 if ((m->m_flags & M_BCAST) || loop_copy) {
322 struct mbuf *n = m_copypacket(m, MB_DONTWAIT);
324 if_simloop(ifp, n, dst->sa_family,
325 sizeof(struct fddi_header));
326 } else if (bcmp(fh->fddi_dhost, fh->fddi_shost,
327 sizeof(fh->fddi_shost)) == 0) {
328 if_simloop(ifp, m, dst->sa_family,
329 sizeof(struct fddi_header));
330 senderr(0); /* XXX */
336 * Queue message on interface, and start output if interface
339 if (IF_QFULL(&ifp->if_snd)) {
340 IF_DROP(&ifp->if_snd);
344 ifp->if_obytes += m->m_pkthdr.len;
345 if (m->m_flags & M_MCAST)
347 IF_ENQUEUE(&ifp->if_snd, m);
348 if ((ifp->if_flags & IFF_OACTIVE) == 0)
349 (*ifp->if_start)(ifp);
360 * Process a received FDDI packet;
361 * the packet is in the mbuf chain m without
362 * the fddi header, which is provided separately.
365 fddi_input(struct ifnet *ifp, struct mbuf *m)
369 struct fddi_header *fh = mtod(m, struct fddi_header *);
371 if (m->m_len < sizeof(struct fddi_header)) {
372 /* XXX error in the caller. */
376 m_adj(m, sizeof(struct fddi_header));
377 m->m_pkthdr.rcvif = ifp;
379 if (!(ifp->if_flags & IFF_UP)) {
383 getmicrotime(&ifp->if_lastchange);
384 ifp->if_ibytes += m->m_pkthdr.len + sizeof *fh;
385 if (fh->fddi_dhost[0] & 1) {
386 if (bcmp(ifp->if_broadcastaddr, fh->fddi_dhost,
387 ifp->if_addrlen) == 0)
388 m->m_flags |= M_BCAST;
390 m->m_flags |= M_MCAST;
392 } else if ((ifp->if_flags & IFF_PROMISC) &&
393 bcmp(((struct arpcom *)ifp)->ac_enaddr, fh->fddi_dhost,
394 sizeof fh->fddi_dhost) != 0) {
401 * If this has a LLC priority of 0, then mark it so upper
402 * layers have a hint that it really came via a FDDI/Ethernet
405 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
406 m->m_flags |= M_LINK0;
409 l = mtod(m, struct llc *);
410 switch (l->llc_dsap) {
411 #if defined(INET) || defined(INET6) || defined(NS) || defined(DECNET) || defined(IPX) || defined(NETATALK)
415 if (l->llc_control != LLC_UI || l->llc_ssap != LLC_SNAP_LSAP)
418 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
419 sizeof(at_org_code)) == 0 &&
420 sizeof(at_org_code) == 0 &&
421 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
422 m_adj(m, sizeof(struct llc));
427 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
428 sizeof(aarp_org_code)) == 0 &&
429 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
430 m_adj(m, sizeof(struct llc));
435 if (l->llc_snap.org_code[0] != 0 ||
436 l->llc_snap.org_code[1] != 0 ||
437 l->llc_snap.org_code[2] != 0)
439 type = ntohs(l->llc_snap.ether_type);
444 if (ipflow_fastforward(m))
450 if (ifp->if_flags & IFF_NOARP)
471 case ETHERTYPE_DECNET:
482 #endif /* NETATALK */
484 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
490 #endif /* INET || NS */
493 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
500 netisr_dispatch(isr, m);
504 * Perform common duties while attaching to interface list
507 #define ifa_next ifa_list.tqe_next
515 struct sockaddr_dl *sdl;
517 ifp->if_input = fddi_input;
518 ifp->if_output = fddi_output;
519 ifp->if_type = IFT_FDDI;
521 ifp->if_broadcastaddr = fddibroadcastaddr;
523 ifp->if_mtu = FDDIMTU;
524 ifp->if_resolvemulti = fddi_resolvemulti;
525 ifp->if_baudrate = 100000000;
526 #ifdef IFF_NOTRAILERS
527 ifp->if_flags |= IFF_NOTRAILERS;
530 #if defined(__DragonFly__) || defined(__FreeBSD__)
531 ifa = ifnet_addrs[ifp->if_index - 1];
532 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
533 sdl->sdl_type = IFT_FDDI;
534 sdl->sdl_alen = ifp->if_addrlen;
535 bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
536 #elif defined(__NetBSD__)
537 LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs);
538 for (ifa = ifp->if_addrlist.tqh_first; ifa != NULL; ifa = ifa->ifa_list.tqe_next)
540 for (ifa = ifp->if_addrlist; ifa != NULL; ifa = ifa->ifa_next)
542 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
543 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
544 sdl->sdl_family == AF_LINK) {
545 sdl->sdl_type = IFT_FDDI;
546 sdl->sdl_alen = ifp->if_addrlen;
547 bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl),
552 bpfattach(ifp, DLT_FDDI, sizeof(struct fddi_header));
556 fddi_resolvemulti(ifp, llsa, sa)
558 struct sockaddr **llsa;
561 struct sockaddr_dl *sdl;
562 struct sockaddr_in *sin;
564 struct sockaddr_in6 *sin6;
568 switch(sa->sa_family) {
571 * No mapping needed. Just check that it's a valid MC address.
573 sdl = (struct sockaddr_dl *)sa;
574 e_addr = LLADDR(sdl);
575 if ((e_addr[0] & 1) != 1)
576 return EADDRNOTAVAIL;
582 sin = (struct sockaddr_in *)sa;
583 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
584 return EADDRNOTAVAIL;
585 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
587 sdl->sdl_len = sizeof *sdl;
588 sdl->sdl_family = AF_LINK;
589 sdl->sdl_index = ifp->if_index;
590 sdl->sdl_type = IFT_FDDI;
592 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
594 e_addr = LLADDR(sdl);
595 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
596 *llsa = (struct sockaddr *)sdl;
601 sin6 = (struct sockaddr_in6 *)sa;
602 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
604 * An IP6 address of 0 means listen to all
605 * of the Ethernet multicast address used for IP6.
606 * (This is used for multicast routers.)
608 ifp->if_flags |= IFF_ALLMULTI;
612 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
613 return EADDRNOTAVAIL;
614 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
616 sdl->sdl_len = sizeof *sdl;
617 sdl->sdl_family = AF_LINK;
618 sdl->sdl_index = ifp->if_index;
619 sdl->sdl_type = IFT_FDDI;
621 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
623 e_addr = LLADDR(sdl);
624 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
625 *llsa = (struct sockaddr *)sdl;
631 * Well, the text isn't quite right, but it's the name