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"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
54 #include <net/if_dl.h>
55 #include <net/if_llc.h>
56 #include <net/if_types.h>
57 #include <net/if_llatbl.h>
59 #include <net/ethernet.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 #include <security/mac/mac_framework.h>
94 static const u_char fddibroadcastaddr[FDDI_ADDR_LEN] =
95 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
97 static int fddi_resolvemulti(struct ifnet *, struct sockaddr **,
99 static int fddi_output(struct ifnet *, struct mbuf *, struct sockaddr *,
101 static void fddi_input(struct ifnet *ifp, struct mbuf *m);
103 #define senderr(e) do { error = (e); goto bad; } while (0)
106 * FDDI output routine.
107 * Encapsulate a packet of type family for the local net.
108 * Use trailer local net encapsulation if enough data in first
109 * packet leaves a multiple of 512 bytes of data in remainder.
110 * Assumes that ifp is actually pointer to arpcom structure.
113 fddi_output(ifp, m, dst, ro)
116 struct sockaddr *dst;
120 int loop_copy = 0, error = 0, hdrcmplt = 0;
121 u_char esrc[FDDI_ADDR_LEN], edst[FDDI_ADDR_LEN];
122 struct fddi_header *fh;
123 #if defined(INET) || defined(INET6)
128 error = mac_ifnet_check_transmit(ifp, m);
133 if (ifp->if_flags & IFF_MONITOR)
135 if (!((ifp->if_flags & IFF_UP) &&
136 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
138 getmicrotime(&ifp->if_lastchange);
140 switch (dst->sa_family) {
143 struct rtentry *rt0 = NULL;
147 error = arpresolve(ifp, rt0, m, dst, edst, &lle);
149 return (error == EWOULDBLOCK ? 0 : error);
150 type = htons(ETHERTYPE_IP);
156 ah = mtod(m, struct arphdr *);
157 ah->ar_hrd = htons(ARPHRD_ETHER);
159 loop_copy = -1; /* if this is for us, don't do it */
161 switch (ntohs(ah->ar_op)) {
162 case ARPOP_REVREQUEST:
164 type = htons(ETHERTYPE_REVARP);
169 type = htons(ETHERTYPE_ARP);
173 if (m->m_flags & M_BCAST)
174 bcopy(ifp->if_broadcastaddr, edst, FDDI_ADDR_LEN);
176 bcopy(ar_tha(ah), edst, FDDI_ADDR_LEN);
183 error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
185 return (error); /* Something bad happened */
186 type = htons(ETHERTYPE_IPV6);
191 type = htons(ETHERTYPE_IPX);
192 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
193 (caddr_t)edst, FDDI_ADDR_LEN);
198 struct at_ifaddr *aa;
199 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
202 * ifaddr is the first thing in at_ifaddr
204 if ((aa = at_ifawithnet( (struct sockaddr_at *)dst)) == 0)
208 * In the phase 2 case, we need to prepend an mbuf for the llc header.
209 * Since we must preserve the value of m, which is passed to us by
210 * value, we m_copy() the first mbuf, and use it for our llc header.
212 if (aa->aa_flags & AFA_PHASE2) {
215 M_PREPEND(m, LLC_SNAPFRAMELEN, M_WAIT);
216 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
217 llc.llc_control = LLC_UI;
218 bcopy(at_org_code, llc.llc_snap.org_code, sizeof(at_org_code));
219 llc.llc_snap.ether_type = htons(ETHERTYPE_AT);
220 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
223 type = htons(ETHERTYPE_AT);
225 ifa_free(&aa->aa_ifa);
228 #endif /* NETATALK */
230 case pseudo_AF_HDRCMPLT:
232 struct ether_header *eh;
234 eh = (struct ether_header *)dst->sa_data;
235 bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, FDDI_ADDR_LEN);
241 struct ether_header *eh;
243 eh = (struct ether_header *)dst->sa_data;
244 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, FDDI_ADDR_LEN);
246 m->m_flags |= (M_BCAST|M_MCAST);
247 type = eh->ether_type;
253 fh = mtod(m, struct fddi_header *);
254 error = EPROTONOSUPPORT;
255 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
256 case FDDIFC_LLC_ASYNC: {
257 /* legal priorities are 0 through 7 */
258 if ((fh->fddi_fc & FDDIFC_Z) > 7)
262 case FDDIFC_LLC_SYNC: {
263 /* FDDIFC_Z bits reserved, must be zero */
264 if (fh->fddi_fc & FDDIFC_Z)
269 /* FDDIFC_Z bits must be non zero */
270 if ((fh->fddi_fc & FDDIFC_Z) == 0)
275 /* anything else is too dangerous */
280 if (fh->fddi_dhost[0] & 1)
281 m->m_flags |= (M_BCAST|M_MCAST);
285 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
286 senderr(EAFNOSUPPORT);
294 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
297 l = mtod(m, struct llc *);
298 l->llc_control = LLC_UI;
299 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
300 l->llc_snap.org_code[0] =
301 l->llc_snap.org_code[1] =
302 l->llc_snap.org_code[2] = 0;
303 l->llc_snap.ether_type = htons(type);
307 * Add local net header. If no space in first mbuf,
310 M_PREPEND(m, FDDI_HDR_LEN, M_DONTWAIT);
313 fh = mtod(m, struct fddi_header *);
314 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
315 bcopy((caddr_t)edst, (caddr_t)fh->fddi_dhost, FDDI_ADDR_LEN);
318 bcopy((caddr_t)esrc, (caddr_t)fh->fddi_shost, FDDI_ADDR_LEN);
320 bcopy(IF_LLADDR(ifp), (caddr_t)fh->fddi_shost,
324 * If a simplex interface, and the packet is being sent to our
325 * Ethernet address or a broadcast address, loopback a copy.
326 * XXX To make a simplex device behave exactly like a duplex
327 * device, we should copy in the case of sending to our own
328 * ethernet address (thus letting the original actually appear
329 * on the wire). However, we don't do that here for security
330 * reasons and compatibility with the original behavior.
332 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
333 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
335 n = m_copy(m, 0, (int)M_COPYALL);
336 (void) if_simloop(ifp, n, dst->sa_family,
338 } else if (bcmp(fh->fddi_dhost, fh->fddi_shost,
339 FDDI_ADDR_LEN) == 0) {
340 (void) if_simloop(ifp, m, dst->sa_family,
342 return (0); /* XXX */
346 error = (ifp->if_transmit)(ifp, m);
360 * Process a received FDDI packet.
369 struct fddi_header *fh;
372 * Do consistency checks to verify assumptions
373 * made by code past this point.
375 if ((m->m_flags & M_PKTHDR) == 0) {
376 if_printf(ifp, "discard frame w/o packet header\n");
381 if (m->m_pkthdr.rcvif == NULL) {
382 if_printf(ifp, "discard frame w/o interface pointer\n");
388 m = m_pullup(m, FDDI_HDR_LEN);
393 fh = mtod(m, struct fddi_header *);
394 m->m_pkthdr.header = (void *)fh;
397 * Discard packet if interface is not up.
399 if (!((ifp->if_flags & IFF_UP) &&
400 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
404 * Give bpf a chance at the packet.
409 * Interface marked for monitoring; discard packet.
411 if (ifp->if_flags & IFF_MONITOR) {
417 mac_ifnet_create_mbuf(ifp, m);
421 * Update interface statistics.
423 ifp->if_ibytes += m->m_pkthdr.len;
424 getmicrotime(&ifp->if_lastchange);
427 * Discard non local unicast packets when interface
428 * is in promiscuous mode.
430 if ((ifp->if_flags & IFF_PROMISC) && ((fh->fddi_dhost[0] & 1) == 0) &&
431 (bcmp(IF_LLADDR(ifp), (caddr_t)fh->fddi_dhost,
432 FDDI_ADDR_LEN) != 0))
436 * Set mbuf flags for bcast/mcast.
438 if (fh->fddi_dhost[0] & 1) {
439 if (bcmp(ifp->if_broadcastaddr, fh->fddi_dhost,
441 m->m_flags |= M_BCAST;
443 m->m_flags |= M_MCAST;
449 * If this has a LLC priority of 0, then mark it so upper
450 * layers have a hint that it really came via a FDDI/Ethernet
453 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
454 m->m_flags |= M_LINK0;
457 /* Strip off FDDI header. */
458 m_adj(m, FDDI_HDR_LEN);
460 m = m_pullup(m, LLC_SNAPFRAMELEN);
465 l = mtod(m, struct llc *);
467 switch (l->llc_dsap) {
471 if ((l->llc_control != LLC_UI) ||
472 (l->llc_ssap != LLC_SNAP_LSAP)) {
477 if (bcmp(&(l->llc_snap.org_code)[0], at_org_code,
478 sizeof(at_org_code)) == 0 &&
479 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AT) {
481 m_adj(m, LLC_SNAPFRAMELEN);
485 if (bcmp(&(l->llc_snap.org_code)[0], aarp_org_code,
486 sizeof(aarp_org_code)) == 0 &&
487 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AARP) {
488 m_adj(m, LLC_SNAPFRAMELEN);
492 #endif /* NETATALK */
493 if (l->llc_snap.org_code[0] != 0 ||
494 l->llc_snap.org_code[1] != 0 ||
495 l->llc_snap.org_code[2] != 0) {
500 type = ntohs(l->llc_snap.ether_type);
501 m_adj(m, LLC_SNAPFRAMELEN);
506 if ((m = ip_fastforward(m)) == NULL)
512 if (ifp->if_flags & IFF_NOARP)
528 case ETHERTYPE_DECNET:
539 #endif /* NETATALK */
541 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
549 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
553 M_SETFIB(m, ifp->if_fib);
554 netisr_dispatch(isr, m);
565 * Perform common duties while attaching to interface list
568 fddi_ifattach(ifp, lla, bpf)
574 struct sockaddr_dl *sdl;
576 ifp->if_type = IFT_FDDI;
577 ifp->if_addrlen = FDDI_ADDR_LEN;
580 if_attach(ifp); /* Must be called before additional assignments */
582 ifp->if_mtu = FDDIMTU;
583 ifp->if_output = fddi_output;
584 ifp->if_input = fddi_input;
585 ifp->if_resolvemulti = fddi_resolvemulti;
586 ifp->if_broadcastaddr = fddibroadcastaddr;
587 ifp->if_baudrate = 100000000;
588 #ifdef IFF_NOTRAILERS
589 ifp->if_flags |= IFF_NOTRAILERS;
592 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
594 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
595 sdl->sdl_type = IFT_FDDI;
596 sdl->sdl_alen = ifp->if_addrlen;
597 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
600 bpfattach(ifp, DLT_FDDI, FDDI_HDR_LEN);
606 fddi_ifdetach(ifp, bpf)
620 fddi_ioctl (ifp, command, data)
629 ifa = (struct ifaddr *) data;
630 ifr = (struct ifreq *) data;
635 ifp->if_flags |= IFF_UP;
637 switch (ifa->ifa_addr->sa_family) {
639 case AF_INET: /* before arpwhohas */
640 ifp->if_init(ifp->if_softc);
641 arp_ifinit(ifp, ifa);
646 * XXX - This code is probably wrong
649 struct ipx_addr *ina;
651 ina = &(IA_SIPX(ifa)->sipx_addr);
653 if (ipx_nullhost(*ina)) {
654 ina->x_host = *(union ipx_host *)
657 bcopy((caddr_t) ina->x_host.c_host,
658 (caddr_t) IF_LLADDR(ifp),
665 ifp->if_init(ifp->if_softc);
670 ifp->if_init(ifp->if_softc);
677 sa = (struct sockaddr *) & ifr->ifr_data;
678 bcopy(IF_LLADDR(ifp),
679 (caddr_t) sa->sa_data, FDDI_ADDR_LEN);
685 * Set the interface MTU.
687 if (ifr->ifr_mtu > FDDIMTU) {
690 ifp->if_mtu = ifr->ifr_mtu;
702 fddi_resolvemulti(ifp, llsa, sa)
704 struct sockaddr **llsa;
707 struct sockaddr_dl *sdl;
709 struct sockaddr_in *sin;
712 struct sockaddr_in6 *sin6;
716 switch(sa->sa_family) {
719 * No mapping needed. Just check that it's a valid MC address.
721 sdl = (struct sockaddr_dl *)sa;
722 e_addr = LLADDR(sdl);
723 if ((e_addr[0] & 1) != 1)
724 return (EADDRNOTAVAIL);
730 sin = (struct sockaddr_in *)sa;
731 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
732 return (EADDRNOTAVAIL);
733 sdl = malloc(sizeof *sdl, M_IFMADDR,
737 sdl->sdl_len = sizeof *sdl;
738 sdl->sdl_family = AF_LINK;
739 sdl->sdl_index = ifp->if_index;
740 sdl->sdl_type = IFT_FDDI;
742 sdl->sdl_alen = FDDI_ADDR_LEN;
744 e_addr = LLADDR(sdl);
745 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
746 *llsa = (struct sockaddr *)sdl;
751 sin6 = (struct sockaddr_in6 *)sa;
752 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
754 * An IP6 address of 0 means listen to all
755 * of the Ethernet multicast address used for IP6.
756 * (This is used for multicast routers.)
758 ifp->if_flags |= IFF_ALLMULTI;
762 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
763 return (EADDRNOTAVAIL);
764 sdl = malloc(sizeof *sdl, M_IFMADDR,
768 sdl->sdl_len = sizeof *sdl;
769 sdl->sdl_family = AF_LINK;
770 sdl->sdl_index = ifp->if_index;
771 sdl->sdl_type = IFT_FDDI;
773 sdl->sdl_alen = FDDI_ADDR_LEN;
775 e_addr = LLADDR(sdl);
776 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
777 *llsa = (struct sockaddr *)sdl;
783 * Well, the text isn't quite right, but it's the name
786 return (EAFNOSUPPORT);
792 static moduledata_t fddi_mod = {
793 "fddi", /* module name */
794 NULL, /* event handler */
798 DECLARE_MODULE(fddi, fddi_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
799 MODULE_VERSION(fddi, 1);