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.16 2005/02/11 22:25:57 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>
58 #include <net/ifq_var.h>
60 #if defined(INET) || defined(INET6)
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <netinet/if_ether.h>
64 #include <net/ethernet.h>
67 #include <netinet6/nd6.h>
69 #if defined(__DragonFly__) || defined(__FreeBSD__)
70 #include <netinet/if_fddi.h>
72 #include <net/if_fddi.h>
76 #include <netproto/ipx/ipx.h>
77 #include <netproto/ipx/ipx_if.h>
82 #include <netns/ns_if.h>
86 #include <netdnet/dn.h>
90 #include <netproto/atalk/at.h>
91 #include <netproto/atalk/at_var.h>
92 #include <netproto/atalk/at_extern.h>
94 #define llc_snap_org_code llc_un.type_snap.org_code
95 #define llc_snap_ether_type llc_un.type_snap.ether_type
97 extern u_char at_org_code[ 3 ];
98 extern u_char aarp_org_code[ 3 ];
101 static int fddi_resolvemulti (struct ifnet *, struct sockaddr **,
103 static void fddi_input(struct ifnet *, struct mbuf *);
104 static int fddi_output(struct ifnet *, struct mbuf *, struct sockaddr *,
107 #define senderr(e) { error = (e); goto bad;}
110 * This really should be defined in if_llc.h but in case it isn't.
113 #define llc_snap llc_un.type_snap
116 #if defined(__DragonFly__)
117 #define RTALLOC1(a, b) _rtlookup(a, b, b ? RTL_DOCLONE : RTL_DONTCLONE)
118 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e)
119 #elif defined(__FreeBSD__)
120 #define RTALLOC1(a, b) rtalloc1(a, b, 0UL)
121 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e, f)
124 * FDDI output routine.
125 * Encapsulate a packet of type family for the local net.
126 * Use trailer local net encapsulation if enough data in first
127 * packet leaves a multiple of 512 bytes of data in remainder.
128 * Assumes that ifp is actually pointer to arpcom structure.
131 fddi_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
134 struct arpcom *ac = (struct arpcom *)ifp;
136 u_char esrc[6], edst[6];
137 struct fddi_header *fh;
138 boolean_t hdrcmplt = FALSE;
139 int s, loop_copy = 0, error;
140 struct altq_pktattr pktattr;
142 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
146 * If the queueing discipline needs packet classification,
147 * do it before prepending link headers.
149 ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
151 switch (dst->sa_family) {
154 if (!arpresolve(ifp, rt, m, dst, edst))
155 return (0); /* if not yet resolved */
156 type = htons(ETHERTYPE_IP);
162 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
163 /* Something bad happened */
166 type = htons(ETHERTYPE_IPV6);
171 type = htons(ETHERTYPE_IPX);
172 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), edst,
178 struct at_ifaddr *aa;
180 if (!aarpresolve((struct arpcom *) ifp, m,
181 (struct sockaddr_at *)dst, edst))
185 * ifaddr is the first thing in at_ifaddr
187 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
193 * In the phase 2 case, we need to prepend an mbuf
194 * for the llc header. Since we must preserve the
195 * value of m, which is passed to us by value, we
196 * m_copy() the first mbuf, and use it for our llc
199 if (aa->aa_flags & AFA_PHASE2) {
202 M_PREPEND(m, sizeof(struct llc), MB_WAIT);
205 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
206 llc.llc_control = LLC_UI;
207 bcopy(at_org_code, llc.llc_snap_org_code,
209 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
210 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
213 type = htons(ETHERTYPE_AT);
217 #endif /* NETATALK */
220 type = htons(ETHERTYPE_NS);
221 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
226 case pseudo_AF_HDRCMPLT:
228 struct ether_header *eh;
231 eh = (struct ether_header *)dst->sa_data;
232 memcpy(esrc, eh->ether_shost, sizeof esrc);
238 struct ether_header *eh;
241 eh = (struct ether_header *)dst->sa_data;
242 memcpy(edst, eh->ether_dhost, sizeof edst);
244 m->m_flags |= (M_BCAST|M_MCAST);
245 type = eh->ether_type;
251 fh = mtod(m, struct fddi_header *);
252 error = EPROTONOSUPPORT;
253 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
254 case FDDIFC_LLC_ASYNC: {
255 /* legal priorities are 0 through 7 */
256 if ((fh->fddi_fc & FDDIFC_Z) > 7)
260 case FDDIFC_LLC_SYNC: {
261 /* FDDIFC_Z bits reserved, must be zero */
262 if (fh->fddi_fc & FDDIFC_Z)
267 /* FDDIFC_Z bits must be non zero */
268 if ((fh->fddi_fc & FDDIFC_Z) == 0)
273 /* anything else is too dangerous */
278 if (fh->fddi_dhost[0] & 1)
279 m->m_flags |= (M_BCAST | M_MCAST);
283 printf("%s: can't handle af%d\n", ifp->if_xname,
285 senderr(EAFNOSUPPORT);
291 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
294 l = mtod(m, struct llc *);
295 l->llc_control = LLC_UI;
296 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
297 l->llc_snap.org_code[0] = l->llc_snap.org_code[1] =
298 l->llc_snap.org_code[2] = 0;
299 memcpy(&l->llc_snap.ether_type, &type, sizeof(u_int16_t));
303 * Add local net header. If no space in first mbuf,
306 M_PREPEND(m, sizeof(struct fddi_header), MB_DONTWAIT);
309 fh = mtod(m, struct fddi_header *);
310 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
311 memcpy(fh->fddi_dhost, edst, sizeof edst);
315 memcpy(fh->fddi_shost, esrc, sizeof fh->fddi_shost);
317 memcpy(fh->fddi_shost, ac->ac_enaddr, sizeof fh->fddi_shost);
319 memcpy(fh->fddi_shost, ac->ac_enaddr, sizeof fh->fddi_shost);
320 * Ethernet address or a broadcast address, loopback a copy.
321 * XXX To make a simplex device behave exactly like a duplex
322 * device, we should copy in the case of sending to our own
323 * ethernet address (thus letting the original actually appear
324 * on the wire). However, we don't do that here for security
325 * reasons and compatibility with the original behavior.
327 if ((ifp->if_flags & IFF_SIMPLEX) &&
329 if ((m->m_flags & M_BCAST) || loop_copy) {
330 struct mbuf *n = m_copypacket(m, MB_DONTWAIT);
332 if_simloop(ifp, n, dst->sa_family,
333 sizeof(struct fddi_header));
334 } else if (bcmp(fh->fddi_dhost, fh->fddi_shost,
335 sizeof(fh->fddi_shost)) == 0) {
336 if_simloop(ifp, m, dst->sa_family,
337 sizeof(struct fddi_header));
338 senderr(0); /* XXX */
344 * Queue message on interface, and start output if interface
347 error = ifq_enqueue(&ifp->if_snd, m, &pktattr);
352 ifp->if_obytes += m->m_pkthdr.len;
353 if (m->m_flags & M_MCAST)
355 if ((ifp->if_flags & IFF_OACTIVE) == 0)
356 (*ifp->if_start)(ifp);
367 * Process a received FDDI packet;
368 * the packet is in the mbuf chain m without
369 * the fddi header, which is provided separately.
372 fddi_input(struct ifnet *ifp, struct mbuf *m)
376 struct fddi_header *fh = mtod(m, struct fddi_header *);
378 if (m->m_len < sizeof(struct fddi_header)) {
379 /* XXX error in the caller. */
383 m_adj(m, sizeof(struct fddi_header));
384 m->m_pkthdr.rcvif = ifp;
386 if (!(ifp->if_flags & IFF_UP)) {
390 getmicrotime(&ifp->if_lastchange);
391 ifp->if_ibytes += m->m_pkthdr.len + sizeof *fh;
392 if (fh->fddi_dhost[0] & 1) {
393 if (bcmp(ifp->if_broadcastaddr, fh->fddi_dhost,
394 ifp->if_addrlen) == 0)
395 m->m_flags |= M_BCAST;
397 m->m_flags |= M_MCAST;
399 } else if ((ifp->if_flags & IFF_PROMISC) &&
400 bcmp(((struct arpcom *)ifp)->ac_enaddr, fh->fddi_dhost,
401 sizeof fh->fddi_dhost) != 0) {
408 * If this has a LLC priority of 0, then mark it so upper
409 * layers have a hint that it really came via a FDDI/Ethernet
412 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
413 m->m_flags |= M_LINK0;
416 l = mtod(m, struct llc *);
417 switch (l->llc_dsap) {
418 #if defined(INET) || defined(INET6) || defined(NS) || defined(DECNET) || defined(IPX) || defined(NETATALK)
422 if (l->llc_control != LLC_UI || l->llc_ssap != LLC_SNAP_LSAP)
425 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
426 sizeof(at_org_code)) == 0 &&
427 sizeof(at_org_code) == 0 &&
428 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
429 m_adj(m, sizeof(struct llc));
434 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
435 sizeof(aarp_org_code)) == 0 &&
436 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
437 m_adj(m, sizeof(struct llc));
442 if (l->llc_snap.org_code[0] != 0 ||
443 l->llc_snap.org_code[1] != 0 ||
444 l->llc_snap.org_code[2] != 0)
446 type = ntohs(l->llc_snap.ether_type);
451 if (ipflow_fastforward(m))
457 if (ifp->if_flags & IFF_NOARP)
478 case ETHERTYPE_DECNET:
489 #endif /* NETATALK */
491 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
497 #endif /* INET || NS */
500 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
507 netisr_dispatch(isr, m);
511 * Perform common duties while attaching to interface list
514 #define ifa_next ifa_list.tqe_next
522 struct sockaddr_dl *sdl;
524 ifp->if_input = fddi_input;
525 ifp->if_output = fddi_output;
526 ifp->if_type = IFT_FDDI;
528 ifp->if_broadcastaddr = fddibroadcastaddr;
530 ifp->if_mtu = FDDIMTU;
531 ifp->if_resolvemulti = fddi_resolvemulti;
532 ifp->if_baudrate = 100000000;
533 #ifdef IFF_NOTRAILERS
534 ifp->if_flags |= IFF_NOTRAILERS;
537 #if defined(__DragonFly__) || defined(__FreeBSD__)
538 ifa = ifnet_addrs[ifp->if_index - 1];
539 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
540 sdl->sdl_type = IFT_FDDI;
541 sdl->sdl_alen = ifp->if_addrlen;
542 bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
543 #elif defined(__NetBSD__)
544 LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs);
545 for (ifa = ifp->if_addrlist.tqh_first; ifa != NULL; ifa = ifa->ifa_list.tqe_next)
547 for (ifa = ifp->if_addrlist; ifa != NULL; ifa = ifa->ifa_next)
549 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
550 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
551 sdl->sdl_family == AF_LINK) {
552 sdl->sdl_type = IFT_FDDI;
553 sdl->sdl_alen = ifp->if_addrlen;
554 bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl),
559 bpfattach(ifp, DLT_FDDI, sizeof(struct fddi_header));
563 fddi_resolvemulti(ifp, llsa, sa)
565 struct sockaddr **llsa;
568 struct sockaddr_dl *sdl;
569 struct sockaddr_in *sin;
571 struct sockaddr_in6 *sin6;
575 switch(sa->sa_family) {
578 * No mapping needed. Just check that it's a valid MC address.
580 sdl = (struct sockaddr_dl *)sa;
581 e_addr = LLADDR(sdl);
582 if ((e_addr[0] & 1) != 1)
583 return EADDRNOTAVAIL;
589 sin = (struct sockaddr_in *)sa;
590 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
591 return EADDRNOTAVAIL;
592 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
594 sdl->sdl_len = sizeof *sdl;
595 sdl->sdl_family = AF_LINK;
596 sdl->sdl_index = ifp->if_index;
597 sdl->sdl_type = IFT_FDDI;
599 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
601 e_addr = LLADDR(sdl);
602 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
603 *llsa = (struct sockaddr *)sdl;
608 sin6 = (struct sockaddr_in6 *)sa;
609 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
611 * An IP6 address of 0 means listen to all
612 * of the Ethernet multicast address used for IP6.
613 * (This is used for multicast routers.)
615 ifp->if_flags |= IFF_ALLMULTI;
619 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
620 return EADDRNOTAVAIL;
621 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
623 sdl->sdl_len = sizeof *sdl;
624 sdl->sdl_family = AF_LINK;
625 sdl->sdl_index = ifp->if_index;
626 sdl->sdl_type = IFT_FDDI;
628 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
630 e_addr = LLADDR(sdl);
631 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
632 *llsa = (struct sockaddr *)sdl;
638 * Well, the text isn't quite right, but it's the name