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>
47 #include <sys/socket.h>
48 #include <sys/malloc.h>
51 #include <net/netisr.h>
52 #include <net/route.h>
53 #include <net/if_llc.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
57 #if defined(INET) || defined(INET6)
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet/if_ether.h>
63 #include <netinet6/nd6.h>
65 #if defined(__FreeBSD__)
66 #include <netinet/if_fddi.h>
68 #include <net/if_fddi.h>
72 #include <netipx/ipx.h>
73 #include <netipx/ipx_if.h>
78 #include <netns/ns_if.h>
82 #include <netdnet/dn.h>
86 #include <netatalk/at.h>
87 #include <netatalk/at_var.h>
88 #include <netatalk/at_extern.h>
90 #define llc_snap_org_code llc_un.type_snap.org_code
91 #define llc_snap_ether_type llc_un.type_snap.ether_type
93 extern u_char at_org_code[ 3 ];
94 extern u_char aarp_org_code[ 3 ];
97 static int fddi_resolvemulti __P((struct ifnet *, struct sockaddr **,
100 #define senderr(e) { error = (e); goto bad;}
103 * This really should be defined in if_llc.h but in case it isn't.
106 #define llc_snap llc_un.type_snap
109 #if defined(__bsdi__) || defined(__NetBSD__)
110 #define RTALLOC1(a, b) rtalloc1(a, b)
111 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e)
112 #elif defined(__FreeBSD__)
113 #define RTALLOC1(a, b) rtalloc1(a, b, 0UL)
114 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e, f)
117 * FDDI output routine.
118 * Encapsulate a packet of type family for the local net.
119 * Use trailer local net encapsulation if enough data in first
120 * packet leaves a multiple of 512 bytes of data in remainder.
121 * Assumes that ifp is actually pointer to arpcom structure.
124 fddi_output(ifp, m, dst, rt0)
125 register struct ifnet *ifp;
127 struct sockaddr *dst;
131 int loop_copy = 0, error = 0, hdrcmplt = 0;
132 u_char esrc[6], edst[6];
133 register struct rtentry *rt;
134 register struct fddi_header *fh;
135 struct arpcom *ac = (struct arpcom *)ifp;
137 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
139 getmicrotime(&ifp->if_lastchange);
140 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
141 if ((rt = rt0) != NULL) {
142 if ((rt->rt_flags & RTF_UP) == 0) {
143 if ((rt0 = rt = RTALLOC1(dst, 1)) != NULL)
146 senderr(EHOSTUNREACH);
148 if (rt->rt_flags & RTF_GATEWAY) {
149 if (rt->rt_gwroute == 0)
151 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
152 rtfree(rt); rt = rt0;
153 lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 1);
154 if ((rt = rt->rt_gwroute) == 0)
155 senderr(EHOSTUNREACH);
158 if (rt->rt_flags & RTF_REJECT)
159 if (rt->rt_rmx.rmx_expire == 0 ||
160 time_second < rt->rt_rmx.rmx_expire)
161 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
164 switch (dst->sa_family) {
168 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
169 if (!ARPRESOLVE(ifp, rt, m, dst, edst, rt0))
170 return (0); /* if not yet resolved */
173 if (!arpresolve(ac, m, &((struct sockaddr_in *)dst)->sin_addr, edst, &usetrailers))
174 return (0); /* if not yet resolved */
176 type = htons(ETHERTYPE_IP);
182 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
183 /* 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, sizeof (edst));
198 struct at_ifaddr *aa;
199 if (!aarpresolve(ac, 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, sizeof(struct llc), M_TRYWAIT);
218 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
219 llc.llc_control = LLC_UI;
220 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
221 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
222 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
225 type = htons(ETHERTYPE_AT);
229 #endif /* NETATALK */
232 type = htons(ETHERTYPE_NS);
233 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
234 (caddr_t)edst, sizeof (edst));
238 case pseudo_AF_HDRCMPLT:
240 struct ether_header *eh;
242 eh = (struct ether_header *)dst->sa_data;
243 (void)memcpy((caddr_t)esrc, (caddr_t)eh->ether_shost, sizeof (esrc));
249 struct ether_header *eh;
251 eh = (struct ether_header *)dst->sa_data;
252 (void)memcpy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
254 m->m_flags |= (M_BCAST|M_MCAST);
255 type = eh->ether_type;
261 fh = mtod(m, struct fddi_header *);
262 error = EPROTONOSUPPORT;
263 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
264 case FDDIFC_LLC_ASYNC: {
265 /* legal priorities are 0 through 7 */
266 if ((fh->fddi_fc & FDDIFC_Z) > 7)
270 case FDDIFC_LLC_SYNC: {
271 /* FDDIFC_Z bits reserved, must be zero */
272 if (fh->fddi_fc & FDDIFC_Z)
277 /* FDDIFC_Z bits must be non zero */
278 if ((fh->fddi_fc & FDDIFC_Z) == 0)
283 /* anything else is too dangerous */
288 if (fh->fddi_dhost[0] & 1)
289 m->m_flags |= (M_BCAST|M_MCAST);
293 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
295 senderr(EAFNOSUPPORT);
299 register struct llc *l;
300 M_PREPEND(m, sizeof (struct llc), M_DONTWAIT);
303 l = mtod(m, struct llc *);
304 l->llc_control = LLC_UI;
305 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
306 l->llc_snap.org_code[0] = l->llc_snap.org_code[1] = l->llc_snap.org_code[2] = 0;
307 (void)memcpy((caddr_t) &l->llc_snap.ether_type, (caddr_t) &type,
312 * Add local net header. If no space in first mbuf,
315 M_PREPEND(m, sizeof (struct fddi_header), M_DONTWAIT);
318 fh = mtod(m, struct fddi_header *);
319 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
320 (void)memcpy((caddr_t)fh->fddi_dhost, (caddr_t)edst, sizeof (edst));
323 (void)memcpy((caddr_t)fh->fddi_shost, (caddr_t)esrc,
324 sizeof(fh->fddi_shost));
326 (void)memcpy((caddr_t)fh->fddi_shost, (caddr_t)ac->ac_enaddr,
327 sizeof(fh->fddi_shost));
329 * If a simplex interface, and the packet is being sent to our
330 * Ethernet address or a broadcast address, loopback a copy.
331 * XXX To make a simplex device behave exactly like a duplex
332 * device, we should copy in the case of sending to our own
333 * ethernet address (thus letting the original actually appear
334 * on the wire). However, we don't do that here for security
335 * reasons and compatibility with the original behavior.
337 if ((ifp->if_flags & IFF_SIMPLEX) &&
339 if ((m->m_flags & M_BCAST) || loop_copy) {
340 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
342 (void) if_simloop(ifp,
343 n, dst->sa_family, sizeof(struct fddi_header));
344 } else if (bcmp(fh->fddi_dhost,
345 fh->fddi_shost, sizeof(fh->fddi_shost)) == 0) {
346 (void) if_simloop(ifp,
347 m, dst->sa_family, sizeof(struct fddi_header));
352 if (! IF_HANDOFF(&ifp->if_snd, m, ifp))
363 * Process a received FDDI packet;
364 * the packet is in the mbuf chain m without
365 * the fddi header, which is provided separately.
368 fddi_input(ifp, fh, m)
370 register struct fddi_header *fh;
373 register struct ifqueue *inq;
374 register struct llc *l;
376 if ((ifp->if_flags & IFF_UP) == 0) {
380 getmicrotime(&ifp->if_lastchange);
381 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*fh);
382 if (fh->fddi_dhost[0] & 1) {
383 if (bcmp((caddr_t)fddibroadcastaddr, (caddr_t)fh->fddi_dhost,
384 sizeof(fddibroadcastaddr)) == 0)
385 m->m_flags |= M_BCAST;
387 m->m_flags |= M_MCAST;
389 } else if ((ifp->if_flags & IFF_PROMISC)
390 && bcmp(((struct arpcom *)ifp)->ac_enaddr, (caddr_t)fh->fddi_dhost,
391 sizeof(fh->fddi_dhost)) != 0) {
398 * If this has a LLC priority of 0, then mark it so upper
399 * layers have a hint that it really came via a FDDI/Ethernet
402 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
403 m->m_flags |= M_LINK0;
406 l = mtod(m, struct llc *);
407 switch (l->llc_dsap) {
408 #if defined(INET) || defined(INET6) || defined(NS) || defined(DECNET) || defined(IPX) || defined(NETATALK)
412 if (l->llc_control != LLC_UI || l->llc_ssap != LLC_SNAP_LSAP)
415 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
416 sizeof(at_org_code)) == 0 &&
417 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
419 m_adj( m, sizeof( struct llc ));
420 schednetisr(NETISR_ATALK);
424 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
425 sizeof(aarp_org_code)) == 0 &&
426 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
427 m_adj( m, sizeof( struct llc ));
428 aarpinput((struct arpcom *)ifp, m); /* XXX */
431 #endif /* NETATALK */
432 if (l->llc_snap.org_code[0] != 0 || l->llc_snap.org_code[1] != 0|| l->llc_snap.org_code[2] != 0)
434 type = ntohs(l->llc_snap.ether_type);
439 if (ipflow_fastforward(m))
441 schednetisr(NETISR_IP);
446 if (ifp->if_flags & IFF_NOARP)
448 schednetisr(NETISR_ARP);
454 schednetisr(NETISR_IPV6);
460 schednetisr(NETISR_IPX);
466 schednetisr(NETISR_NS);
471 case ETHERTYPE_DECNET:
472 schednetisr(NETISR_DECNET);
478 schednetisr(NETISR_ATALK);
482 /* probably this should be done with a NETISR as well */
483 aarpinput((struct arpcom *)ifp, m); /* XXX */
485 #endif /* NETATALK */
487 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
493 #endif /* INET || NS */
496 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
503 (void) IF_HANDOFF(inq, m, NULL);
506 * Perform common duties while attaching to interface list
509 #define ifa_next ifa_list.tqe_next
514 register struct ifnet *ifp;
516 register struct ifaddr *ifa;
517 register struct sockaddr_dl *sdl;
519 ifp->if_type = IFT_FDDI;
522 ifp->if_mtu = FDDIMTU;
523 ifp->if_resolvemulti = fddi_resolvemulti;
524 ifp->if_baudrate = 100000000;
525 #ifdef IFF_NOTRAILERS
526 ifp->if_flags |= IFF_NOTRAILERS;
528 ifp->if_broadcastaddr = fddibroadcastaddr;
529 #if defined(__FreeBSD__)
530 ifa = ifaddr_byindex(ifp->if_index);
531 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
532 sdl->sdl_type = IFT_FDDI;
533 sdl->sdl_alen = ifp->if_addrlen;
534 bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
535 #elif defined(__NetBSD__)
536 LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs);
537 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
539 for (ifa = ifp->if_addrlist; ifa != NULL; ifa = ifa->ifa_next)
541 #if !defined(__FreeBSD__)
542 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
543 sdl->sdl_family == AF_LINK) {
544 sdl->sdl_type = IFT_FDDI;
545 sdl->sdl_alen = ifp->if_addrlen;
546 bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
547 LLADDR(sdl), ifp->if_addrlen);
554 fddi_resolvemulti(ifp, llsa, sa)
556 struct sockaddr **llsa;
559 struct sockaddr_dl *sdl;
560 struct sockaddr_in *sin;
562 struct sockaddr_in6 *sin6;
566 switch(sa->sa_family) {
569 * No mapping needed. Just check that it's a valid MC address.
571 sdl = (struct sockaddr_dl *)sa;
572 e_addr = LLADDR(sdl);
573 if ((e_addr[0] & 1) != 1)
574 return EADDRNOTAVAIL;
580 sin = (struct sockaddr_in *)sa;
581 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
582 return EADDRNOTAVAIL;
583 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
585 sdl->sdl_len = sizeof *sdl;
586 sdl->sdl_family = AF_LINK;
587 sdl->sdl_index = ifp->if_index;
588 sdl->sdl_type = IFT_FDDI;
590 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
592 e_addr = LLADDR(sdl);
593 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
594 *llsa = (struct sockaddr *)sdl;
599 sin6 = (struct sockaddr_in6 *)sa;
600 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
602 * An IP6 address of 0 means listen to all
603 * of the Ethernet multicast address used for IP6.
604 * (This is used for multicast routers.)
606 ifp->if_flags |= IFF_ALLMULTI;
610 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
611 return EADDRNOTAVAIL;
612 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
614 sdl->sdl_len = sizeof *sdl;
615 sdl->sdl_family = AF_LINK;
616 sdl->sdl_index = ifp->if_index;
617 sdl->sdl_type = IFT_FDDI;
619 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
621 e_addr = LLADDR(sdl);
622 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
623 *llsa = (struct sockaddr *)sdl;
629 * Well, the text isn't quite right, but it's the name