1 /* $NetBSD: if_arcsubr.c,v 1.36 2001/06/14 05:44:23 itojun Exp $ */
2 /* $FreeBSD: src/sys/net/if_arcsubr.c,v 1.1.2.5 2003/02/05 18:42:15 fjoe Exp $ */
3 /* $DragonFly: src/sys/net/Attic/if_arcsubr.c,v 1.11 2004/12/21 02:54:14 hsu Exp $ */
6 * Copyright (c) 1994, 1995 Ignatios Souvatzis
7 * Copyright (c) 1982, 1989, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
39 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
43 #include "opt_inet6.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
51 #include <sys/protosw.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/errno.h>
55 #include <sys/syslog.h>
57 #include <machine/cpu.h>
60 #include <net/netisr.h>
61 #include <net/route.h>
62 #include <net/if_dl.h>
63 #include <net/if_types.h>
64 #include <net/if_arc.h>
65 #include <net/if_arp.h>
68 #if defined(INET) || defined(INET6)
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/if_ether.h>
75 #include <netinet6/nd6.h>
79 #include <netproto/ipx/ipx.h>
80 #include <netproto/ipx/ipx_if.h>
83 MODULE_VERSION(arcnet, 1);
85 #define ARCNET_ALLOW_BROKEN_ARP
87 static struct mbuf *arc_defrag (struct ifnet *, struct mbuf *);
88 static int arc_resolvemulti (struct ifnet *, struct sockaddr **,
90 static void arc_input(struct ifnet *, struct mbuf *);
91 static int arc_output(struct ifnet *, struct mbuf *, struct sockaddr *,
94 #define ARC_LLADDR(ifp) (*(u_int8_t *)IF_LLADDR(ifp))
96 #define senderr(e) { error = (e); goto bad;}
97 #define SIN(s) ((struct sockaddr_in *)s)
98 #define SIPX(s) ((struct sockaddr_ipx *)s)
100 const uint8_t arcbroadcastaddr[1] = {0};
103 * ARCnet output routine.
104 * Encapsulate a packet of type family for the local net.
105 * Assumes that ifp is actually pointer to arccom structure.
108 arc_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
111 struct arc_header *ah;
113 u_int8_t atype, adst;
117 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
118 return (ENETDOWN); /* m, m1 aren't initialized yet */
120 switch (dst->sa_family) {
125 * For now, use the simple IP addr -> ARCnet addr mapping
127 if (m->m_flags & (M_BCAST|M_MCAST))
128 adst = ifp->if_broadcastaddr[0];
129 else if (ifp->if_flags & IFF_NOARP)
130 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
131 else if (!arpresolve(ifp, rt, m, dst, &adst))
132 return 0; /* not resolved yet */
134 atype = (ifp->if_flags & IFF_LINK0) ?
135 ARCTYPE_IP_OLD : ARCTYPE_IP;
140 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)&adst))
141 return (0); /* it must be impossible, but... */
142 atype = ARCTYPE_INET6;
147 adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
150 adst = ifp->if_broadcastaddr[0];
156 ah = (struct arc_header *)dst->sa_data;
157 adst = ah->arc_dhost;
158 atype = ah->arc_type;
160 if (atype == ARCTYPE_ARP) {
161 atype = (ifp->if_flags & IFF_LINK0) ?
162 ARCTYPE_ARP_OLD: ARCTYPE_ARP;
164 #ifdef ARCNET_ALLOW_BROKEN_ARP
166 * XXX It's not clear per RFC826 if this is needed, but
167 * "assigned numbers" say this is wrong.
168 * However, e.g., AmiTCP 3.0Beta used it... we make this
169 * switchable for emergency cases. Not perfect, but...
171 if (ifp->if_flags & IFF_LINK2)
172 mtod(m, struct arphdr *)->ar_pro = atype - 1;
178 printf("%s: can't handle af%d\n", ifp->if_xname,
180 senderr(EAFNOSUPPORT);
183 isphds = arc_isphds(atype);
184 M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, MB_DONTWAIT);
187 ah = mtod(m, struct arc_header *);
188 ah->arc_type = atype;
189 ah->arc_dhost = adst;
190 ah->arc_shost = *IF_LLADDR(ifp);
191 ah->arc_shost = ARC_LLADDR(ifp);
197 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
198 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
199 struct mbuf *n = m_copypacket(m, MB_DONTWAIT);
201 if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
202 } else if (ah->arc_dhost == ah->arc_shost) {
203 if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
204 return (0); /* XXX */
211 if (!IF_HANDOFF(&ifp->if_snd, m, ifp)) {
230 ac = (struct arccom *)ifp;
240 struct arc_header *ah;
242 ac = (struct arccom *)ifp;
243 if ((m = ac->curr_frag) == 0) {
246 /* dequeue new packet */
247 IF_DEQUEUE(&ifp->if_snd, m);
251 ah = mtod(m, struct arc_header *);
252 if (!arc_isphds(ah->arc_type))
255 ++ac->ac_seqid; /* make the seqid unique */
256 tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
257 ac->fsflag = 2 * tfrags - 3;
259 ac->rsflag = ac->fsflag;
260 ac->arc_dhost = ah->arc_dhost;
261 ac->arc_shost = ah->arc_shost;
262 ac->arc_type = ah->arc_type;
264 m_adj(m, ARC_HDRNEWLEN);
268 /* split out next fragment and return it */
269 if (ac->sflag < ac->fsflag) {
270 /* we CAN'T have short packets here */
271 ac->curr_frag = m_split(m, ARC_MAX_DATA, MB_DONTWAIT);
272 if (ac->curr_frag == 0) {
277 M_PREPEND(m, ARC_HDRNEWLEN, MB_DONTWAIT);
279 m_freem(ac->curr_frag);
284 ah = mtod(m, struct arc_header *);
285 ah->arc_flag = ac->rsflag;
286 ah->arc_seqid = ac->ac_seqid;
289 ac->rsflag = ac->sflag;
290 } else if ((m->m_pkthdr.len >=
291 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
293 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
296 M_PREPEND(m, ARC_HDRNEWLEN_EXC, MB_DONTWAIT);
300 ah = mtod(m, struct arc_header *);
302 ah->arc_seqid = 0xFFFF;
303 ah->arc_type2 = ac->arc_type;
304 ah->arc_flag2 = ac->sflag;
305 ah->arc_seqid2 = ac->ac_seqid;
309 M_PREPEND(m, ARC_HDRNEWLEN, MB_DONTWAIT);
313 ah = mtod(m, struct arc_header *);
314 ah->arc_flag = ac->sflag;
315 ah->arc_seqid = ac->ac_seqid;
318 ah->arc_dhost = ac->arc_dhost;
319 ah->arc_shost = ac->arc_shost;
320 ah->arc_type = ac->arc_type;
326 * Defragmenter. Returns mbuf if last packet found, else
327 * NULL. frees imcoming mbuf as necessary.
330 __inline struct mbuf *
335 struct arc_header *ah, *ah1;
343 ac = (struct arccom *)ifp;
345 if (m->m_len < ARC_HDRNEWLEN) {
346 m = m_pullup(m, ARC_HDRNEWLEN);
353 ah = mtod(m, struct arc_header *);
356 if (!arc_isphds(typ))
362 if (ah->arc_flag == 0xff) {
365 if (m->m_len < ARC_HDRNEWLEN) {
366 m = m_pullup(m, ARC_HDRNEWLEN);
373 ah = mtod(m, struct arc_header *);
376 af = &ac->ac_fragtab[src];
378 s = "debug code error";
380 if (ah->arc_flag & 1) {
382 * first fragment. We always initialize, which is
383 * about the right thing to do, as we only want to
384 * accept one fragmented packet per src at a time.
391 af->af_maxflag = ah->arc_flag;
393 af->af_seqid = ah->arc_seqid;
398 /* check for unfragmented packet */
399 if (ah->arc_flag == 0)
402 /* do we have a first packet from that src? */
408 ah1 = mtod(m1, struct arc_header *);
410 if (ah->arc_seqid != ah1->arc_seqid) {
415 if (typ != ah1->arc_type) {
420 if (dst != ah1->arc_dhost) {
421 s = "dest host differs";
425 /* typ, seqid and dst are ok here. */
427 if (ah->arc_flag == af->af_lastseen) {
432 if (ah->arc_flag == af->af_lastseen + 2) {
433 /* ok, this is next fragment */
434 af->af_lastseen = ah->arc_flag;
435 m_adj(m,ARC_HDRNEWLEN);
438 * m_cat might free the first mbuf (with pkthdr)
439 * in 2nd chain; therefore:
442 newflen = m->m_pkthdr.len;
446 m1->m_pkthdr.len += newflen;
448 /* is it the last one? */
449 if (af->af_lastseen > af->af_maxflag) {
450 af->af_packet = NULL;
456 /* if all else fails, it is out of sequence, too */
461 af->af_packet = NULL;
467 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
474 * return 1 if Packet Header Definition Standard, else 0.
475 * For now: old IP, old ARP aren't obviously. Lacking correct information,
476 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
477 * (Apple and Novell corporations were involved, among others, in PHDS work).
478 * Easiest is to assume that everybody else uses that, too.
484 return (type != ARCTYPE_IP_OLD &&
485 type != ARCTYPE_ARP_OLD &&
486 type != ARCTYPE_DIAGNOSE);
490 * Process a received Arcnet packet;
491 * the packet is in the mbuf chain m with
495 arc_input(struct ifnet *ifp, struct mbuf *m)
497 struct arc_header *ah;
501 if ((ifp->if_flags & IFF_UP) == 0) {
506 /* possibly defragment: */
507 m = arc_defrag(ifp, m);
514 ah = mtod(m, struct arc_header *);
515 /* does this belong to us? */
516 if ((ifp->if_flags & IFF_PROMISC) == 0
517 && ah->arc_dhost != ifp->if_broadcastaddr[0]
518 && ah->arc_dhost != ARC_LLADDR(ifp)) {
523 ifp->if_ibytes += m->m_pkthdr.len;
525 if (ah->arc_dhost == ifp->if_broadcastaddr[0]) {
526 m->m_flags |= M_BCAST|M_MCAST;
530 atype = ah->arc_type;
534 m_adj(m, ARC_HDRNEWLEN);
535 if (ipflow_fastforward(m))
541 m_adj(m, ARC_HDRLEN);
542 if (ipflow_fastforward(m))
548 if (ifp->if_flags & IFF_NOARP) {
549 /* Discard packet if ARP is disabled on interface */
553 m_adj(m, ARC_HDRNEWLEN);
555 #ifdef ARCNET_ALLOW_BROKEN_ARP
556 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
560 case ARCTYPE_ARP_OLD:
561 if (ifp->if_flags & IFF_NOARP) {
562 /* Discard packet if ARP is disabled on interface */
566 m_adj(m, ARC_HDRLEN);
568 #ifdef ARCNET_ALLOW_BROKEN_ARP
569 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
575 m_adj(m, ARC_HDRNEWLEN);
581 m_adj(m, ARC_HDRNEWLEN);
590 netisr_dispatch(isr, m);
594 * Register (new) link level address.
597 arc_storelladdr(ifp, lla)
601 ARC_LLADDR(ifp) = lla;
605 * Perform common duties while attaching to interface list
608 arc_ifattach(ifp, lla)
613 struct sockaddr_dl *sdl;
616 ifp->if_input = arc_input;
617 ifp->if_output = arc_output;
619 ifp->if_type = IFT_ARCNET;
621 ifp->if_broadcastaddr = arcbroadcastaddr;
622 ifp->if_hdrlen = ARC_HDRLEN;
624 ifp->if_resolvemulti = arc_resolvemulti;
625 if (ifp->if_baudrate == 0)
626 ifp->if_baudrate = 2500000;
627 #if defined(__DragonFly__) || __FreeBSD_version < 500000
628 ifa = ifnet_addrs[ifp->if_index - 1];
630 ifa = ifaddr_byindex(ifp->if_index);
632 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
633 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
634 sdl->sdl_type = IFT_ARCNET;
635 sdl->sdl_alen = ifp->if_addrlen;
637 if (ifp->if_flags & IFF_BROADCAST)
638 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
640 ac = (struct arccom *)ifp;
641 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
643 /* XXX this message isn't entirely clear, to me -- cgd */
644 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n",
645 ifp->if_xname, ifp->if_xname);
647 arc_storelladdr(ifp, lla);
649 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
661 arc_ioctl(ifp, command, data)
666 struct ifaddr *ifa = (struct ifaddr *) data;
667 struct ifreq *ifr = (struct ifreq *) data;
672 ifp->if_flags |= IFF_UP;
673 switch (ifa->ifa_addr->sa_family) {
676 ifp->if_init(ifp->if_softc); /* before arpwhohas */
677 arp_ifinit(ifp, ifa);
682 * XXX This code is probably wrong
686 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
688 if (ipx_nullhost(*ina))
689 ina->x_host.c_host[5] = ARC_LLADDR(ifp);
691 arc_storelladdr(ifp, ina->x_host.c_host[5]);
696 ifp->if_init(ifp->if_softc);
701 ifp->if_init(ifp->if_softc);
710 sa = (struct sockaddr *) &ifr->ifr_data;
711 *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
718 error = EAFNOSUPPORT;
720 switch (ifr->ifr_addr.sa_family) {
726 error = EAFNOSUPPORT;
734 * Set the interface MTU.
735 * mtu can't be larger than ARCMTU for RFC1051
736 * and can't be larger than ARC_PHDS_MTU
738 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
739 ifr->ifr_mtu > ARC_PHDS_MAXMTU)
742 ifp->if_mtu = ifr->ifr_mtu;
749 /* based on ether_resolvemulti() */
751 arc_resolvemulti(ifp, llsa, sa)
753 struct sockaddr **llsa;
756 struct sockaddr_dl *sdl;
757 struct sockaddr_in *sin;
759 struct sockaddr_in6 *sin6;
762 switch(sa->sa_family) {
765 * No mapping needed. Just check that it's a valid MC address.
767 sdl = (struct sockaddr_dl *)sa;
768 if (*LLADDR(sdl) != ifp->if_broadcastaddr[0])
769 return EADDRNOTAVAIL;
774 sin = (struct sockaddr_in *)sa;
775 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
776 return EADDRNOTAVAIL;
777 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
779 sdl->sdl_len = sizeof *sdl;
780 sdl->sdl_family = AF_LINK;
781 sdl->sdl_index = ifp->if_index;
782 sdl->sdl_type = IFT_ARCNET;
783 sdl->sdl_alen = ARC_ADDR_LEN;
785 *llsa = (struct sockaddr *)sdl;
790 sin6 = (struct sockaddr_in6 *)sa;
791 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
793 * An IP6 address of 0 means listen to all
794 * of the Ethernet multicast address used for IP6.
795 * (This is used for multicast routers.)
797 ifp->if_flags |= IFF_ALLMULTI;
801 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
802 return EADDRNOTAVAIL;
803 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
805 sdl->sdl_len = sizeof *sdl;
806 sdl->sdl_family = AF_LINK;
807 sdl->sdl_index = ifp->if_index;
808 sdl->sdl_type = IFT_ARCNET;
809 sdl->sdl_alen = ARC_ADDR_LEN;
811 *llsa = (struct sockaddr *)sdl;
817 * Well, the text isn't quite right, but it's the name