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>
48 #include <sys/malloc.h>
50 #include <sys/module.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
55 #include <net/if_dl.h>
56 #include <net/if_llc.h>
57 #include <net/if_types.h>
59 #include <net/netisr.h>
60 #include <net/route.h>
64 #if defined(INET) || defined(INET6)
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/if_ether.h>
70 #include <netinet6/nd6.h>
74 #include <netipx/ipx.h>
75 #include <netipx/ipx_if.h>
79 #include <netdnet/dn.h>
83 #include <netatalk/at.h>
84 #include <netatalk/at_var.h>
85 #include <netatalk/at_extern.h>
87 extern u_char at_org_code[ 3 ];
88 extern u_char aarp_org_code[ 3 ];
91 #include <security/mac/mac_framework.h>
93 static const u_char fddibroadcastaddr[FDDI_ADDR_LEN] =
94 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
96 static int fddi_resolvemulti(struct ifnet *, struct sockaddr **,
98 static int fddi_output(struct ifnet *, struct mbuf *, struct sockaddr *,
100 static void fddi_input(struct ifnet *ifp, struct mbuf *m);
102 #define senderr(e) do { error = (e); goto bad; } while (0)
105 * FDDI output routine.
106 * Encapsulate a packet of type family for the local net.
107 * Use trailer local net encapsulation if enough data in first
108 * packet leaves a multiple of 512 bytes of data in remainder.
109 * Assumes that ifp is actually pointer to arpcom structure.
112 fddi_output(ifp, m, dst, rt0)
115 struct sockaddr *dst;
119 int loop_copy = 0, error = 0, hdrcmplt = 0;
120 u_char esrc[FDDI_ADDR_LEN], edst[FDDI_ADDR_LEN];
121 struct fddi_header *fh;
124 error = mac_check_ifnet_transmit(ifp, m);
129 if (ifp->if_flags & IFF_MONITOR)
131 if (!((ifp->if_flags & IFF_UP) &&
132 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
134 getmicrotime(&ifp->if_lastchange);
136 switch (dst->sa_family) {
139 error = arpresolve(ifp, rt0, m, dst, edst);
141 return (error == EWOULDBLOCK ? 0 : error);
142 type = htons(ETHERTYPE_IP);
148 ah = mtod(m, struct arphdr *);
149 ah->ar_hrd = htons(ARPHRD_ETHER);
151 loop_copy = -1; /* if this is for us, don't do it */
153 switch (ntohs(ah->ar_op)) {
154 case ARPOP_REVREQUEST:
156 type = htons(ETHERTYPE_REVARP);
161 type = htons(ETHERTYPE_ARP);
165 if (m->m_flags & M_BCAST)
166 bcopy(ifp->if_broadcastaddr, edst, FDDI_ADDR_LEN);
168 bcopy(ar_tha(ah), edst, FDDI_ADDR_LEN);
175 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
177 return (error); /* Something bad happened */
178 type = htons(ETHERTYPE_IPV6);
183 type = htons(ETHERTYPE_IPX);
184 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
185 (caddr_t)edst, FDDI_ADDR_LEN);
190 struct at_ifaddr *aa;
191 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
194 * ifaddr is the first thing in at_ifaddr
196 if ((aa = at_ifawithnet( (struct sockaddr_at *)dst)) == 0)
200 * In the phase 2 case, we need to prepend an mbuf for the llc header.
201 * Since we must preserve the value of m, which is passed to us by
202 * value, we m_copy() the first mbuf, and use it for our llc header.
204 if (aa->aa_flags & AFA_PHASE2) {
207 M_PREPEND(m, LLC_SNAPFRAMELEN, M_TRYWAIT);
210 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
211 llc.llc_control = LLC_UI;
212 bcopy(at_org_code, llc.llc_snap.org_code, sizeof(at_org_code));
213 llc.llc_snap.ether_type = htons(ETHERTYPE_AT);
214 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
217 type = htons(ETHERTYPE_AT);
221 #endif /* NETATALK */
223 case pseudo_AF_HDRCMPLT:
225 struct ether_header *eh;
227 eh = (struct ether_header *)dst->sa_data;
228 bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, FDDI_ADDR_LEN);
234 struct ether_header *eh;
236 eh = (struct ether_header *)dst->sa_data;
237 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, FDDI_ADDR_LEN);
239 m->m_flags |= (M_BCAST|M_MCAST);
240 type = eh->ether_type;
246 fh = mtod(m, struct fddi_header *);
247 error = EPROTONOSUPPORT;
248 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
249 case FDDIFC_LLC_ASYNC: {
250 /* legal priorities are 0 through 7 */
251 if ((fh->fddi_fc & FDDIFC_Z) > 7)
255 case FDDIFC_LLC_SYNC: {
256 /* FDDIFC_Z bits reserved, must be zero */
257 if (fh->fddi_fc & FDDIFC_Z)
262 /* FDDIFC_Z bits must be non zero */
263 if ((fh->fddi_fc & FDDIFC_Z) == 0)
268 /* anything else is too dangerous */
273 if (fh->fddi_dhost[0] & 1)
274 m->m_flags |= (M_BCAST|M_MCAST);
278 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
279 senderr(EAFNOSUPPORT);
287 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
290 l = mtod(m, struct llc *);
291 l->llc_control = LLC_UI;
292 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
293 l->llc_snap.org_code[0] =
294 l->llc_snap.org_code[1] =
295 l->llc_snap.org_code[2] = 0;
296 l->llc_snap.ether_type = htons(type);
300 * Add local net header. If no space in first mbuf,
303 M_PREPEND(m, FDDI_HDR_LEN, M_DONTWAIT);
306 fh = mtod(m, struct fddi_header *);
307 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
308 bcopy((caddr_t)edst, (caddr_t)fh->fddi_dhost, FDDI_ADDR_LEN);
311 bcopy((caddr_t)esrc, (caddr_t)fh->fddi_shost, FDDI_ADDR_LEN);
313 bcopy(IF_LLADDR(ifp), (caddr_t)fh->fddi_shost,
317 * If a simplex interface, and the packet is being sent to our
318 * Ethernet address or a broadcast address, loopback a copy.
319 * XXX To make a simplex device behave exactly like a duplex
320 * device, we should copy in the case of sending to our own
321 * ethernet address (thus letting the original actually appear
322 * on the wire). However, we don't do that here for security
323 * reasons and compatibility with the original behavior.
325 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
326 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
328 n = m_copy(m, 0, (int)M_COPYALL);
329 (void) if_simloop(ifp, n, dst->sa_family,
331 } else if (bcmp(fh->fddi_dhost, fh->fddi_shost,
332 FDDI_ADDR_LEN) == 0) {
333 (void) if_simloop(ifp, m, dst->sa_family,
335 return (0); /* XXX */
339 IFQ_HANDOFF(ifp, m, error);
353 * Process a received FDDI packet.
362 struct fddi_header *fh;
365 * Do consistency checks to verify assumptions
366 * made by code past this point.
368 if ((m->m_flags & M_PKTHDR) == 0) {
369 if_printf(ifp, "discard frame w/o packet header\n");
374 if (m->m_pkthdr.rcvif == NULL) {
375 if_printf(ifp, "discard frame w/o interface pointer\n");
381 m = m_pullup(m, FDDI_HDR_LEN);
386 fh = mtod(m, struct fddi_header *);
387 m->m_pkthdr.header = (void *)fh;
390 * Discard packet if interface is not up.
392 if (!((ifp->if_flags & IFF_UP) &&
393 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
397 * Give bpf a chance at the packet.
402 * Interface marked for monitoring; discard packet.
404 if (ifp->if_flags & IFF_MONITOR) {
410 mac_create_mbuf_from_ifnet(ifp, m);
414 * Update interface statistics.
416 ifp->if_ibytes += m->m_pkthdr.len;
417 getmicrotime(&ifp->if_lastchange);
420 * Discard non local unicast packets when interface
421 * is in promiscuous mode.
423 if ((ifp->if_flags & IFF_PROMISC) && ((fh->fddi_dhost[0] & 1) == 0) &&
424 (bcmp(IF_LLADDR(ifp), (caddr_t)fh->fddi_dhost,
425 FDDI_ADDR_LEN) != 0))
429 * Set mbuf flags for bcast/mcast.
431 if (fh->fddi_dhost[0] & 1) {
432 if (bcmp(ifp->if_broadcastaddr, fh->fddi_dhost,
434 m->m_flags |= M_BCAST;
436 m->m_flags |= M_MCAST;
442 * If this has a LLC priority of 0, then mark it so upper
443 * layers have a hint that it really came via a FDDI/Ethernet
446 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
447 m->m_flags |= M_LINK0;
450 /* Strip off FDDI header. */
451 m_adj(m, FDDI_HDR_LEN);
453 m = m_pullup(m, LLC_SNAPFRAMELEN);
458 l = mtod(m, struct llc *);
460 switch (l->llc_dsap) {
464 if ((l->llc_control != LLC_UI) ||
465 (l->llc_ssap != LLC_SNAP_LSAP)) {
470 if (bcmp(&(l->llc_snap.org_code)[0], at_org_code,
471 sizeof(at_org_code)) == 0 &&
472 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AT) {
474 m_adj(m, LLC_SNAPFRAMELEN);
478 if (bcmp(&(l->llc_snap.org_code)[0], aarp_org_code,
479 sizeof(aarp_org_code)) == 0 &&
480 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AARP) {
481 m_adj(m, LLC_SNAPFRAMELEN);
485 #endif /* NETATALK */
486 if (l->llc_snap.org_code[0] != 0 ||
487 l->llc_snap.org_code[1] != 0 ||
488 l->llc_snap.org_code[2] != 0) {
493 type = ntohs(l->llc_snap.ether_type);
494 m_adj(m, LLC_SNAPFRAMELEN);
499 if ((m = ip_fastforward(m)) == NULL)
505 if (ifp->if_flags & IFF_NOARP)
521 case ETHERTYPE_DECNET:
532 #endif /* NETATALK */
534 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
542 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
546 netisr_dispatch(isr, m);
557 * Perform common duties while attaching to interface list
560 fddi_ifattach(ifp, lla, bpf)
566 struct sockaddr_dl *sdl;
568 ifp->if_type = IFT_FDDI;
569 ifp->if_addrlen = FDDI_ADDR_LEN;
572 if_attach(ifp); /* Must be called before additional assignments */
574 ifp->if_mtu = FDDIMTU;
575 ifp->if_output = fddi_output;
576 ifp->if_input = fddi_input;
577 ifp->if_resolvemulti = fddi_resolvemulti;
578 ifp->if_broadcastaddr = fddibroadcastaddr;
579 ifp->if_baudrate = 100000000;
580 #ifdef IFF_NOTRAILERS
581 ifp->if_flags |= IFF_NOTRAILERS;
584 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
586 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
587 sdl->sdl_type = IFT_FDDI;
588 sdl->sdl_alen = ifp->if_addrlen;
589 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
592 bpfattach(ifp, DLT_FDDI, FDDI_HDR_LEN);
598 fddi_ifdetach(ifp, bpf)
612 fddi_ioctl (ifp, command, data)
621 ifa = (struct ifaddr *) data;
622 ifr = (struct ifreq *) data;
627 ifp->if_flags |= IFF_UP;
629 switch (ifa->ifa_addr->sa_family) {
631 case AF_INET: /* before arpwhohas */
632 ifp->if_init(ifp->if_softc);
633 arp_ifinit(ifp, ifa);
638 * XXX - This code is probably wrong
641 struct ipx_addr *ina;
643 ina = &(IA_SIPX(ifa)->sipx_addr);
645 if (ipx_nullhost(*ina)) {
646 ina->x_host = *(union ipx_host *)
649 bcopy((caddr_t) ina->x_host.c_host,
650 (caddr_t) IF_LLADDR(ifp),
657 ifp->if_init(ifp->if_softc);
662 ifp->if_init(ifp->if_softc);
669 sa = (struct sockaddr *) & ifr->ifr_data;
670 bcopy(IF_LLADDR(ifp),
671 (caddr_t) sa->sa_data, FDDI_ADDR_LEN);
677 * Set the interface MTU.
679 if (ifr->ifr_mtu > FDDIMTU) {
682 ifp->if_mtu = ifr->ifr_mtu;
694 fddi_resolvemulti(ifp, llsa, sa)
696 struct sockaddr **llsa;
699 struct sockaddr_dl *sdl;
700 struct sockaddr_in *sin;
702 struct sockaddr_in6 *sin6;
706 switch(sa->sa_family) {
709 * No mapping needed. Just check that it's a valid MC address.
711 sdl = (struct sockaddr_dl *)sa;
712 e_addr = LLADDR(sdl);
713 if ((e_addr[0] & 1) != 1)
714 return (EADDRNOTAVAIL);
720 sin = (struct sockaddr_in *)sa;
721 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
722 return (EADDRNOTAVAIL);
723 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
727 sdl->sdl_len = sizeof *sdl;
728 sdl->sdl_family = AF_LINK;
729 sdl->sdl_index = ifp->if_index;
730 sdl->sdl_type = IFT_FDDI;
732 sdl->sdl_alen = FDDI_ADDR_LEN;
734 e_addr = LLADDR(sdl);
735 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
736 *llsa = (struct sockaddr *)sdl;
741 sin6 = (struct sockaddr_in6 *)sa;
742 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
744 * An IP6 address of 0 means listen to all
745 * of the Ethernet multicast address used for IP6.
746 * (This is used for multicast routers.)
748 ifp->if_flags |= IFF_ALLMULTI;
752 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
753 return (EADDRNOTAVAIL);
754 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
758 sdl->sdl_len = sizeof *sdl;
759 sdl->sdl_family = AF_LINK;
760 sdl->sdl_index = ifp->if_index;
761 sdl->sdl_type = IFT_FDDI;
763 sdl->sdl_alen = FDDI_ADDR_LEN;
765 e_addr = LLADDR(sdl);
766 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
767 *llsa = (struct sockaddr *)sdl;
773 * Well, the text isn't quite right, but it's the name
776 return (EAFNOSUPPORT);
782 static moduledata_t fddi_mod = {
783 "fddi", /* module name */
784 NULL, /* event handler */
788 DECLARE_MODULE(fddi, fddi_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
789 MODULE_VERSION(fddi, 1);