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.21 2006/01/14 11:05:17 swildner 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>
50 #include <sys/serialize.h>
54 #include <net/netisr.h>
55 #include <net/route.h>
56 #include <net/if_llc.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/ifq_var.h>
61 #if defined(INET) || defined(INET6)
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet/if_ether.h>
65 #include <net/ethernet.h>
68 #include <netinet6/nd6.h>
70 #if defined(__DragonFly__) || defined(__FreeBSD__)
71 #include <netinet/if_fddi.h>
73 #include <net/if_fddi.h>
77 #include <netproto/ipx/ipx.h>
78 #include <netproto/ipx/ipx_if.h>
83 #include <netns/ns_if.h>
87 #include <netdnet/dn.h>
91 #include <netproto/atalk/at.h>
92 #include <netproto/atalk/at_var.h>
93 #include <netproto/atalk/at_extern.h>
95 #define llc_snap_org_code llc_un.type_snap.org_code
96 #define llc_snap_ether_type llc_un.type_snap.ether_type
98 extern u_char at_org_code[ 3 ];
99 extern u_char aarp_org_code[ 3 ];
100 #endif /* NETATALK */
102 static int fddi_resolvemulti (struct ifnet *, struct sockaddr **,
104 static void fddi_input(struct ifnet *, struct mbuf *);
105 static int fddi_output(struct ifnet *, struct mbuf *, struct sockaddr *,
108 #define senderr(e) { error = (e); goto bad;}
111 * This really should be defined in if_llc.h but in case it isn't.
114 #define llc_snap llc_un.type_snap
117 #if defined(__DragonFly__)
118 #define RTALLOC1(a, b) _rtlookup(a, b, b ? RTL_DOCLONE : RTL_DONTCLONE)
119 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e)
120 #elif defined(__FreeBSD__)
121 #define RTALLOC1(a, b) rtalloc1(a, b, 0UL)
122 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e, f)
125 * FDDI output routine.
126 * Encapsulate a packet of type family for the local net.
127 * Use trailer local net encapsulation if enough data in first
128 * packet leaves a multiple of 512 bytes of data in remainder.
129 * Assumes that ifp is actually pointer to arpcom structure.
132 fddi_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
135 struct arpcom *ac = (struct arpcom *)ifp;
137 u_char esrc[6], edst[6];
138 struct fddi_header *fh;
139 boolean_t hdrcmplt = FALSE;
140 int loop_copy = 0, error;
141 struct altq_pktattr pktattr;
143 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
147 * If the queueing discipline needs packet classification,
148 * do it before prepending link headers.
150 ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
152 switch (dst->sa_family) {
155 if (!arpresolve(ifp, rt, m, dst, edst))
156 return (0); /* if not yet resolved */
157 type = htons(ETHERTYPE_IP);
163 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
164 /* Something bad happened */
167 type = htons(ETHERTYPE_IPV6);
172 type = htons(ETHERTYPE_IPX);
173 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), edst,
179 struct at_ifaddr *aa;
181 if (!aarpresolve((struct arpcom *) ifp, m,
182 (struct sockaddr_at *)dst, edst))
186 * ifaddr is the first thing in at_ifaddr
188 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
194 * In the phase 2 case, we need to prepend an mbuf
195 * for the llc header. Since we must preserve the
196 * value of m, which is passed to us by value, we
197 * m_copy() the first mbuf, and use it for our llc
200 if (aa->aa_flags & AFA_PHASE2) {
203 M_PREPEND(m, sizeof(struct llc), MB_WAIT);
206 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
207 llc.llc_control = LLC_UI;
208 bcopy(at_org_code, llc.llc_snap_org_code,
210 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
211 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
214 type = htons(ETHERTYPE_AT);
218 #endif /* NETATALK */
221 type = htons(ETHERTYPE_NS);
222 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
227 case pseudo_AF_HDRCMPLT:
229 struct ether_header *eh;
232 eh = (struct ether_header *)dst->sa_data;
233 memcpy(esrc, eh->ether_shost, sizeof esrc);
239 struct ether_header *eh;
242 eh = (struct ether_header *)dst->sa_data;
243 memcpy(edst, eh->ether_dhost, sizeof edst);
245 m->m_flags |= (M_BCAST|M_MCAST);
246 type = eh->ether_type;
252 fh = mtod(m, struct fddi_header *);
253 error = EPROTONOSUPPORT;
254 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
255 case FDDIFC_LLC_ASYNC: {
256 /* legal priorities are 0 through 7 */
257 if ((fh->fddi_fc & FDDIFC_Z) > 7)
261 case FDDIFC_LLC_SYNC: {
262 /* FDDIFC_Z bits reserved, must be zero */
263 if (fh->fddi_fc & FDDIFC_Z)
268 /* FDDIFC_Z bits must be non zero */
269 if ((fh->fddi_fc & FDDIFC_Z) == 0)
274 /* anything else is too dangerous */
279 if (fh->fddi_dhost[0] & 1)
280 m->m_flags |= (M_BCAST | M_MCAST);
284 printf("%s: can't handle af%d\n", ifp->if_xname,
286 senderr(EAFNOSUPPORT);
292 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
295 l = mtod(m, struct llc *);
296 l->llc_control = LLC_UI;
297 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
298 l->llc_snap.org_code[0] = l->llc_snap.org_code[1] =
299 l->llc_snap.org_code[2] = 0;
300 memcpy(&l->llc_snap.ether_type, &type, sizeof(u_int16_t));
304 * Add local net header. If no space in first mbuf,
307 M_PREPEND(m, sizeof(struct fddi_header), MB_DONTWAIT);
310 fh = mtod(m, struct fddi_header *);
311 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
312 memcpy(fh->fddi_dhost, edst, sizeof edst);
316 memcpy(fh->fddi_shost, esrc, sizeof fh->fddi_shost);
318 memcpy(fh->fddi_shost, ac->ac_enaddr, sizeof fh->fddi_shost);
320 memcpy(fh->fddi_shost, ac->ac_enaddr, sizeof fh->fddi_shost);
321 * Ethernet address or a broadcast address, loopback a copy.
322 * XXX To make a simplex device behave exactly like a duplex
323 * device, we should copy in the case of sending to our own
324 * ethernet address (thus letting the original actually appear
325 * on the wire). However, we don't do that here for security
326 * reasons and compatibility with the original behavior.
328 if ((ifp->if_flags & IFF_SIMPLEX) &&
330 if ((m->m_flags & M_BCAST) || loop_copy) {
331 struct mbuf *n = m_copypacket(m, MB_DONTWAIT);
333 if_simloop(ifp, n, dst->sa_family,
334 sizeof(struct fddi_header));
335 } else if (bcmp(fh->fddi_dhost, fh->fddi_shost,
336 sizeof(fh->fddi_shost)) == 0) {
337 if_simloop(ifp, m, dst->sa_family,
338 sizeof(struct fddi_header));
339 senderr(0); /* XXX */
344 * Dispatch the message to the interface.
346 return (ifq_handoff(ifp, m, &pktattr));
354 * Process a received FDDI packet;
355 * the packet is in the mbuf chain m without
356 * the fddi header, which is provided separately.
359 fddi_input(struct ifnet *ifp, struct mbuf *m)
363 struct fddi_header *fh = mtod(m, struct fddi_header *);
365 ASSERT_SERIALIZED(ifp->if_serializer);
367 if (m->m_len < sizeof(struct fddi_header)) {
368 /* XXX error in the caller. */
372 m_adj(m, sizeof(struct fddi_header));
373 m->m_pkthdr.rcvif = ifp;
375 if (!(ifp->if_flags & IFF_UP)) {
379 getmicrotime(&ifp->if_lastchange);
380 ifp->if_ibytes += m->m_pkthdr.len + sizeof *fh;
381 if (fh->fddi_dhost[0] & 1) {
382 if (bcmp(ifp->if_broadcastaddr, fh->fddi_dhost,
383 ifp->if_addrlen) == 0)
384 m->m_flags |= M_BCAST;
386 m->m_flags |= M_MCAST;
388 } else if ((ifp->if_flags & IFF_PROMISC) &&
389 bcmp(((struct arpcom *)ifp)->ac_enaddr, fh->fddi_dhost,
390 sizeof fh->fddi_dhost) != 0) {
397 * If this has a LLC priority of 0, then mark it so upper
398 * layers have a hint that it really came via a FDDI/Ethernet
401 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
402 m->m_flags |= M_LINK0;
405 l = mtod(m, struct llc *);
406 switch (l->llc_dsap) {
407 #if defined(INET) || defined(INET6) || defined(NS) || defined(DECNET) || defined(IPX) || defined(NETATALK)
411 if (l->llc_control != LLC_UI || l->llc_ssap != LLC_SNAP_LSAP)
414 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
415 sizeof(at_org_code)) == 0 &&
416 sizeof(at_org_code) == 0 &&
417 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
418 m_adj(m, sizeof(struct llc));
423 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
424 sizeof(aarp_org_code)) == 0 &&
425 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
426 m_adj(m, sizeof(struct llc));
431 if (l->llc_snap.org_code[0] != 0 ||
432 l->llc_snap.org_code[1] != 0 ||
433 l->llc_snap.org_code[2] != 0)
435 type = ntohs(l->llc_snap.ether_type);
440 if (ipflow_fastforward(m, ifp->if_serializer))
446 if (ifp->if_flags & IFF_NOARP)
467 case ETHERTYPE_DECNET:
478 #endif /* NETATALK */
480 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
486 #endif /* INET || NS */
489 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
496 netisr_dispatch(isr, m);
500 * Perform common duties while attaching to interface list
503 #define ifa_next ifa_list.tqe_next
507 fddi_ifattach(struct ifnet *ifp, lwkt_serialize_t serializer)
509 struct sockaddr_dl *sdl;
511 ifp->if_input = fddi_input;
512 ifp->if_output = fddi_output;
513 ifp->if_type = IFT_FDDI;
515 ifp->if_broadcastaddr = fddibroadcastaddr;
517 ifp->if_mtu = FDDIMTU;
518 ifp->if_resolvemulti = fddi_resolvemulti;
519 ifp->if_baudrate = 100000000;
520 #ifdef IFF_NOTRAILERS
521 ifp->if_flags |= IFF_NOTRAILERS;
523 if_attach(ifp, serializer);
524 #if defined(__DragonFly__) || defined(__FreeBSD__)
525 sdl = IF_LLSOCKADDR(ifp);
526 sdl->sdl_type = IFT_FDDI;
527 sdl->sdl_alen = ifp->if_addrlen;
528 bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
529 #elif defined(__NetBSD__)
530 LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs);
531 for (ifa = ifp->if_addrlist.tqh_first; ifa != NULL; ifa = ifa->ifa_list.tqe_next)
533 for (ifa = ifp->if_addrlist; ifa != NULL; ifa = ifa->ifa_next)
535 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
536 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
537 sdl->sdl_family == AF_LINK) {
538 sdl->sdl_type = IFT_FDDI;
539 sdl->sdl_alen = ifp->if_addrlen;
540 bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl),
545 bpfattach(ifp, DLT_FDDI, sizeof(struct fddi_header));
549 fddi_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, struct sockaddr *sa)
551 struct sockaddr_dl *sdl;
552 struct sockaddr_in *sin;
554 struct sockaddr_in6 *sin6;
558 switch(sa->sa_family) {
561 * No mapping needed. Just check that it's a valid MC address.
563 sdl = (struct sockaddr_dl *)sa;
564 e_addr = LLADDR(sdl);
565 if ((e_addr[0] & 1) != 1)
566 return EADDRNOTAVAIL;
572 sin = (struct sockaddr_in *)sa;
573 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
574 return EADDRNOTAVAIL;
575 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
577 sdl->sdl_len = sizeof *sdl;
578 sdl->sdl_family = AF_LINK;
579 sdl->sdl_index = ifp->if_index;
580 sdl->sdl_type = IFT_FDDI;
582 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
584 e_addr = LLADDR(sdl);
585 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
586 *llsa = (struct sockaddr *)sdl;
591 sin6 = (struct sockaddr_in6 *)sa;
592 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
594 * An IP6 address of 0 means listen to all
595 * of the Ethernet multicast address used for IP6.
596 * (This is used for multicast routers.)
598 ifp->if_flags |= IFF_ALLMULTI;
602 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
603 return EADDRNOTAVAIL;
604 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
606 sdl->sdl_len = sizeof *sdl;
607 sdl->sdl_family = AF_LINK;
608 sdl->sdl_index = ifp->if_index;
609 sdl->sdl_type = IFT_FDDI;
611 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
613 e_addr = LLADDR(sdl);
614 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
615 *llsa = (struct sockaddr *)sdl;
621 * Well, the text isn't quite right, but it's the name