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
39 #include "opt_atalk.h"
41 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
56 #include <net/if_dl.h>
57 #include <net/if_llc.h>
58 #include <net/if_types.h>
60 #include <net/netisr.h>
61 #include <net/route.h>
65 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/if_ether.h>
71 #include <netinet6/nd6.h>
75 #include <netipx/ipx.h>
76 #include <netipx/ipx_if.h>
80 #include <netdnet/dn.h>
84 #include <netatalk/at.h>
85 #include <netatalk/at_var.h>
86 #include <netatalk/at_extern.h>
88 extern u_char at_org_code[ 3 ];
89 extern u_char aarp_org_code[ 3 ];
92 static const u_char fddibroadcastaddr[FDDI_ADDR_LEN] =
93 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
95 static int fddi_resolvemulti(struct ifnet *, struct sockaddr **,
97 static int fddi_output(struct ifnet *, struct mbuf *, struct sockaddr *,
99 static void fddi_input(struct ifnet *ifp, struct mbuf *m);
101 #define senderr(e) do { error = (e); goto bad; } while (0)
104 * FDDI output routine.
105 * Encapsulate a packet of type family for the local net.
106 * Use trailer local net encapsulation if enough data in first
107 * packet leaves a multiple of 512 bytes of data in remainder.
108 * Assumes that ifp is actually pointer to arpcom structure.
111 fddi_output(ifp, m, dst, rt0)
114 struct sockaddr *dst;
118 int loop_copy = 0, error = 0, hdrcmplt = 0;
119 u_char esrc[FDDI_ADDR_LEN], edst[FDDI_ADDR_LEN];
120 struct fddi_header *fh;
123 error = mac_check_ifnet_transmit(ifp, m);
128 if (ifp->if_flags & IFF_MONITOR)
130 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
132 getmicrotime(&ifp->if_lastchange);
134 switch (dst->sa_family) {
137 error = arpresolve(ifp, rt0, m, dst, edst);
139 return (error == EWOULDBLOCK ? 0 : error);
140 type = htons(ETHERTYPE_IP);
146 ah = mtod(m, struct arphdr *);
147 ah->ar_hrd = htons(ARPHRD_ETHER);
149 loop_copy = -1; /* if this is for us, don't do it */
151 switch (ntohs(ah->ar_op)) {
152 case ARPOP_REVREQUEST:
154 type = htons(ETHERTYPE_REVARP);
159 type = htons(ETHERTYPE_ARP);
163 if (m->m_flags & M_BCAST)
164 bcopy(ifp->if_broadcastaddr, edst, FDDI_ADDR_LEN);
166 bcopy(ar_tha(ah), edst, FDDI_ADDR_LEN);
173 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
175 return (error); /* Something bad happened */
176 type = htons(ETHERTYPE_IPV6);
181 type = htons(ETHERTYPE_IPX);
182 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
183 (caddr_t)edst, FDDI_ADDR_LEN);
188 struct at_ifaddr *aa;
189 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
192 * ifaddr is the first thing in at_ifaddr
194 if ((aa = at_ifawithnet( (struct sockaddr_at *)dst)) == 0)
198 * In the phase 2 case, we need to prepend an mbuf for the llc header.
199 * Since we must preserve the value of m, which is passed to us by
200 * value, we m_copy() the first mbuf, and use it for our llc header.
202 if (aa->aa_flags & AFA_PHASE2) {
205 M_PREPEND(m, LLC_SNAPFRAMELEN, M_TRYWAIT);
208 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
209 llc.llc_control = LLC_UI;
210 bcopy(at_org_code, llc.llc_snap.org_code, sizeof(at_org_code));
211 llc.llc_snap.ether_type = htons(ETHERTYPE_AT);
212 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
215 type = htons(ETHERTYPE_AT);
219 #endif /* NETATALK */
221 case pseudo_AF_HDRCMPLT:
223 struct ether_header *eh;
225 eh = (struct ether_header *)dst->sa_data;
226 bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, FDDI_ADDR_LEN);
232 struct ether_header *eh;
234 eh = (struct ether_header *)dst->sa_data;
235 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, FDDI_ADDR_LEN);
237 m->m_flags |= (M_BCAST|M_MCAST);
238 type = eh->ether_type;
244 fh = mtod(m, struct fddi_header *);
245 error = EPROTONOSUPPORT;
246 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
247 case FDDIFC_LLC_ASYNC: {
248 /* legal priorities are 0 through 7 */
249 if ((fh->fddi_fc & FDDIFC_Z) > 7)
253 case FDDIFC_LLC_SYNC: {
254 /* FDDIFC_Z bits reserved, must be zero */
255 if (fh->fddi_fc & FDDIFC_Z)
260 /* FDDIFC_Z bits must be non zero */
261 if ((fh->fddi_fc & FDDIFC_Z) == 0)
266 /* anything else is too dangerous */
271 if (fh->fddi_dhost[0] & 1)
272 m->m_flags |= (M_BCAST|M_MCAST);
276 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
277 senderr(EAFNOSUPPORT);
285 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
288 l = mtod(m, struct llc *);
289 l->llc_control = LLC_UI;
290 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
291 l->llc_snap.org_code[0] =
292 l->llc_snap.org_code[1] =
293 l->llc_snap.org_code[2] = 0;
294 l->llc_snap.ether_type = htons(type);
298 * Add local net header. If no space in first mbuf,
301 M_PREPEND(m, FDDI_HDR_LEN, M_DONTWAIT);
304 fh = mtod(m, struct fddi_header *);
305 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
306 bcopy((caddr_t)edst, (caddr_t)fh->fddi_dhost, FDDI_ADDR_LEN);
309 bcopy((caddr_t)esrc, (caddr_t)fh->fddi_shost, FDDI_ADDR_LEN);
311 bcopy(IFP2AC(ifp)->ac_enaddr, (caddr_t)fh->fddi_shost,
315 * If a simplex interface, and the packet is being sent to our
316 * Ethernet address or a broadcast address, loopback a copy.
317 * XXX To make a simplex device behave exactly like a duplex
318 * device, we should copy in the case of sending to our own
319 * ethernet address (thus letting the original actually appear
320 * on the wire). However, we don't do that here for security
321 * reasons and compatibility with the original behavior.
323 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
324 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
326 n = m_copy(m, 0, (int)M_COPYALL);
327 (void) if_simloop(ifp, n, dst->sa_family,
329 } else if (bcmp(fh->fddi_dhost, fh->fddi_shost,
330 FDDI_ADDR_LEN) == 0) {
331 (void) if_simloop(ifp, m, dst->sa_family,
333 return (0); /* XXX */
337 IFQ_HANDOFF(ifp, m, error);
351 * Process a received FDDI packet.
360 struct fddi_header *fh;
363 * Do consistency checks to verify assumptions
364 * made by code past this point.
366 if ((m->m_flags & M_PKTHDR) == 0) {
367 if_printf(ifp, "discard frame w/o packet header\n");
372 if (m->m_pkthdr.rcvif == NULL) {
373 if_printf(ifp, "discard frame w/o interface pointer\n");
379 m = m_pullup(m, FDDI_HDR_LEN);
384 fh = mtod(m, struct fddi_header *);
385 m->m_pkthdr.header = (void *)fh;
388 * Discard packet if interface is not up.
390 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
394 * Give bpf a chance at the packet.
399 * Interface marked for monitoring; discard packet.
401 if (ifp->if_flags & IFF_MONITOR) {
407 mac_create_mbuf_from_ifnet(ifp, m);
411 * Update interface statistics.
413 ifp->if_ibytes += m->m_pkthdr.len;
414 getmicrotime(&ifp->if_lastchange);
417 * Discard non local unicast packets when interface
418 * is in promiscuous mode.
420 if ((ifp->if_flags & IFF_PROMISC) && ((fh->fddi_dhost[0] & 1) == 0) &&
421 (bcmp(IFP2AC(ifp)->ac_enaddr, (caddr_t)fh->fddi_dhost,
422 FDDI_ADDR_LEN) != 0))
426 * Set mbuf flags for bcast/mcast.
428 if (fh->fddi_dhost[0] & 1) {
429 if (bcmp(ifp->if_broadcastaddr, fh->fddi_dhost,
431 m->m_flags |= M_BCAST;
433 m->m_flags |= M_MCAST;
439 * If this has a LLC priority of 0, then mark it so upper
440 * layers have a hint that it really came via a FDDI/Ethernet
443 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
444 m->m_flags |= M_LINK0;
447 /* Strip off FDDI header. */
448 m_adj(m, FDDI_HDR_LEN);
450 m = m_pullup(m, LLC_SNAPFRAMELEN);
455 l = mtod(m, struct llc *);
457 switch (l->llc_dsap) {
461 if ((l->llc_control != LLC_UI) ||
462 (l->llc_ssap != LLC_SNAP_LSAP)) {
467 if (bcmp(&(l->llc_snap.org_code)[0], at_org_code,
468 sizeof(at_org_code)) == 0 &&
469 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AT) {
471 m_adj(m, LLC_SNAPFRAMELEN);
475 if (bcmp(&(l->llc_snap.org_code)[0], aarp_org_code,
476 sizeof(aarp_org_code)) == 0 &&
477 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AARP) {
478 m_adj(m, LLC_SNAPFRAMELEN);
482 #endif /* NETATALK */
483 if (l->llc_snap.org_code[0] != 0 ||
484 l->llc_snap.org_code[1] != 0 ||
485 l->llc_snap.org_code[2] != 0) {
490 type = ntohs(l->llc_snap.ether_type);
491 m_adj(m, LLC_SNAPFRAMELEN);
496 if (ip_fastforward(m))
502 if (ifp->if_flags & IFF_NOARP)
518 case ETHERTYPE_DECNET:
529 #endif /* NETATALK */
531 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
539 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
543 netisr_dispatch(isr, m);
554 * Perform common duties while attaching to interface list
557 fddi_ifattach(ifp, bpf)
562 struct sockaddr_dl *sdl;
564 ifp->if_type = IFT_FDDI;
565 ifp->if_addrlen = FDDI_ADDR_LEN;
568 if_attach(ifp); /* Must be called before additional assignments */
570 ifp->if_mtu = FDDIMTU;
571 ifp->if_output = fddi_output;
572 ifp->if_input = fddi_input;
573 ifp->if_resolvemulti = fddi_resolvemulti;
574 ifp->if_broadcastaddr = fddibroadcastaddr;
575 ifp->if_baudrate = 100000000;
576 #ifdef IFF_NOTRAILERS
577 ifp->if_flags |= IFF_NOTRAILERS;
579 ifa = ifaddr_byindex(ifp->if_index);
581 if_printf(ifp, "%s() no lladdr!\n", __func__);
585 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
586 sdl->sdl_type = IFT_FDDI;
587 sdl->sdl_alen = ifp->if_addrlen;
588 bcopy(IFP2AC(ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
591 bpfattach(ifp, DLT_FDDI, FDDI_HDR_LEN);
597 fddi_ifdetach(ifp, bpf)
611 fddi_ioctl (ifp, command, data)
620 ifa = (struct ifaddr *) data;
621 ifr = (struct ifreq *) data;
626 ifp->if_flags |= IFF_UP;
628 switch (ifa->ifa_addr->sa_family) {
630 case AF_INET: /* before arpwhohas */
631 ifp->if_init(ifp->if_softc);
632 arp_ifinit(ifp, ifa);
637 * XXX - This code is probably wrong
640 struct ipx_addr *ina;
643 ina = &(IA_SIPX(ifa)->sipx_addr);
646 if (ipx_nullhost(*ina)) {
647 ina->x_host = *(union ipx_host *)
650 bcopy((caddr_t) ina->x_host.c_host,
651 (caddr_t) ac->ac_enaddr,
652 sizeof(ac->ac_enaddr));
658 ifp->if_init(ifp->if_softc);
663 ifp->if_init(ifp->if_softc);
670 sa = (struct sockaddr *) & ifr->ifr_data;
671 bcopy(IFP2AC(ifp)->ac_enaddr,
672 (caddr_t) sa->sa_data, FDDI_ADDR_LEN);
678 * Set the interface MTU.
680 if (ifr->ifr_mtu > FDDIMTU) {
683 ifp->if_mtu = ifr->ifr_mtu;
695 fddi_resolvemulti(ifp, llsa, sa)
697 struct sockaddr **llsa;
700 struct sockaddr_dl *sdl;
701 struct sockaddr_in *sin;
703 struct sockaddr_in6 *sin6;
707 switch(sa->sa_family) {
710 * No mapping needed. Just check that it's a valid MC address.
712 sdl = (struct sockaddr_dl *)sa;
713 e_addr = LLADDR(sdl);
714 if ((e_addr[0] & 1) != 1)
715 return (EADDRNOTAVAIL);
721 sin = (struct sockaddr_in *)sa;
722 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
723 return (EADDRNOTAVAIL);
724 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
726 sdl->sdl_len = sizeof *sdl;
727 sdl->sdl_family = AF_LINK;
728 sdl->sdl_index = ifp->if_index;
729 sdl->sdl_type = IFT_FDDI;
731 sdl->sdl_alen = FDDI_ADDR_LEN;
733 e_addr = LLADDR(sdl);
734 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
735 *llsa = (struct sockaddr *)sdl;
740 sin6 = (struct sockaddr_in6 *)sa;
741 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
743 * An IP6 address of 0 means listen to all
744 * of the Ethernet multicast address used for IP6.
745 * (This is used for multicast routers.)
747 ifp->if_flags |= IFF_ALLMULTI;
751 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
752 return (EADDRNOTAVAIL);
753 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
755 sdl->sdl_len = sizeof *sdl;
756 sdl->sdl_family = AF_LINK;
757 sdl->sdl_index = ifp->if_index;
758 sdl->sdl_type = IFT_FDDI;
760 sdl->sdl_alen = FDDI_ADDR_LEN;
762 e_addr = LLADDR(sdl);
763 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
764 *llsa = (struct sockaddr *)sdl;
770 * Well, the text isn't quite right, but it's the name
773 return (EAFNOSUPPORT);
779 static moduledata_t fddi_mod = {
780 "fddi", /* module name */
781 NULL, /* event handler */
785 DECLARE_MODULE(fddi, fddi_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
786 MODULE_VERSION(fddi, 1);