1 /* $NetBSD: if_arcsubr.c,v 1.36 2001/06/14 05:44:23 itojun Exp $ */
5 * Copyright (c) 1994, 1995 Ignatios Souvatzis
6 * Copyright (c) 1982, 1989, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
38 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
42 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/module.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>
67 #include <net/if_llatbl.h>
69 #if defined(INET) || defined(INET6)
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
72 #include <netinet/if_ether.h>
76 #include <netinet6/nd6.h>
80 #include <netipx/ipx.h>
81 #include <netipx/ipx_if.h>
84 #define ARCNET_ALLOW_BROKEN_ARP
86 static struct mbuf *arc_defrag(struct ifnet *, struct mbuf *);
87 static int arc_resolvemulti(struct ifnet *, struct sockaddr **,
90 u_int8_t arcbroadcastaddr = 0;
92 #define ARC_LLADDR(ifp) (*(u_int8_t *)IF_LLADDR(ifp))
94 #define senderr(e) { error = (e); goto bad;}
95 #define SIN(s) ((const struct sockaddr_in *)(s))
96 #define SIPX(s) ((const struct sockaddr_ipx *)(s))
99 * ARCnet output routine.
100 * Encapsulate a packet of type family for the local net.
101 * Assumes that ifp is actually pointer to arccom structure.
104 arc_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
107 struct arc_header *ah;
109 u_int8_t atype, adst;
112 #if defined(INET) || defined(INET6)
116 if (!((ifp->if_flags & IFF_UP) &&
117 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
118 return(ENETDOWN); /* m, m1 aren't initialized yet */
122 switch (dst->sa_family) {
127 * For now, use the simple IP addr -> ARCnet addr mapping
129 if (m->m_flags & (M_BCAST|M_MCAST))
130 adst = arcbroadcastaddr; /* ARCnet broadcast address */
131 else if (ifp->if_flags & IFF_NOARP)
132 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
134 error = arpresolve(ifp, ro ? ro->ro_rt : NULL,
135 m, dst, &adst, &lle);
137 return (error == EWOULDBLOCK ? 0 : error);
140 atype = (ifp->if_flags & IFF_LINK0) ?
141 ARCTYPE_IP_OLD : ARCTYPE_IP;
146 ah = mtod(m, struct arphdr *);
147 ah->ar_hrd = htons(ARPHRD_ARCNET);
149 loop_copy = -1; /* if this is for us, don't do it */
151 switch(ntohs(ah->ar_op)) {
152 case ARPOP_REVREQUEST:
154 atype = ARCTYPE_REVARP;
163 if (m->m_flags & M_BCAST)
164 bcopy(ifp->if_broadcastaddr, &adst, ARC_ADDR_LEN);
166 bcopy(ar_tha(ah), &adst, ARC_ADDR_LEN);
173 error = nd6_storelladdr(ifp, m, dst, (u_char *)&adst, &lle);
176 atype = ARCTYPE_INET6;
181 adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
184 adst = arcbroadcastaddr;
190 const struct arc_header *ah;
193 ah = (const struct arc_header *)dst->sa_data;
194 adst = ah->arc_dhost;
195 atype = ah->arc_type;
197 if (atype == ARCTYPE_ARP) {
198 atype = (ifp->if_flags & IFF_LINK0) ?
199 ARCTYPE_ARP_OLD: ARCTYPE_ARP;
201 #ifdef ARCNET_ALLOW_BROKEN_ARP
203 * XXX It's not clear per RFC826 if this is needed, but
204 * "assigned numbers" say this is wrong.
205 * However, e.g., AmiTCP 3.0Beta used it... we make this
206 * switchable for emergency cases. Not perfect, but...
208 if (ifp->if_flags & IFF_LINK2)
209 mtod(m, struct arphdr *)->ar_pro = atype - 1;
215 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
216 senderr(EAFNOSUPPORT);
219 isphds = arc_isphds(atype);
220 M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_NOWAIT);
223 ah = mtod(m, struct arc_header *);
224 ah->arc_type = atype;
225 ah->arc_dhost = adst;
226 ah->arc_shost = ARC_LLADDR(ifp);
232 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
233 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
234 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
236 (void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
237 } else if (ah->arc_dhost == ah->arc_shost) {
238 (void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
239 return (0); /* XXX */
245 error = ifp->if_transmit(ifp, m);
256 arc_frag_init(struct ifnet *ifp)
260 ac = (struct arccom *)ifp->if_l2com;
265 arc_frag_next(struct ifnet *ifp)
269 struct arc_header *ah;
271 ac = (struct arccom *)ifp->if_l2com;
272 if ((m = ac->curr_frag) == 0) {
275 /* dequeue new packet */
276 IF_DEQUEUE(&ifp->if_snd, m);
280 ah = mtod(m, struct arc_header *);
281 if (!arc_isphds(ah->arc_type))
284 ++ac->ac_seqid; /* make the seqid unique */
285 tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
286 ac->fsflag = 2 * tfrags - 3;
288 ac->rsflag = ac->fsflag;
289 ac->arc_dhost = ah->arc_dhost;
290 ac->arc_shost = ah->arc_shost;
291 ac->arc_type = ah->arc_type;
293 m_adj(m, ARC_HDRNEWLEN);
297 /* split out next fragment and return it */
298 if (ac->sflag < ac->fsflag) {
299 /* we CAN'T have short packets here */
300 ac->curr_frag = m_split(m, ARC_MAX_DATA, M_NOWAIT);
301 if (ac->curr_frag == 0) {
306 M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT);
308 m_freem(ac->curr_frag);
313 ah = mtod(m, struct arc_header *);
314 ah->arc_flag = ac->rsflag;
315 ah->arc_seqid = ac->ac_seqid;
318 ac->rsflag = ac->sflag;
319 } else if ((m->m_pkthdr.len >=
320 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
322 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
325 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_NOWAIT);
329 ah = mtod(m, struct arc_header *);
331 ah->arc_seqid = 0xFFFF;
332 ah->arc_type2 = ac->arc_type;
333 ah->arc_flag2 = ac->sflag;
334 ah->arc_seqid2 = ac->ac_seqid;
338 M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT);
342 ah = mtod(m, struct arc_header *);
343 ah->arc_flag = ac->sflag;
344 ah->arc_seqid = ac->ac_seqid;
347 ah->arc_dhost = ac->arc_dhost;
348 ah->arc_shost = ac->arc_shost;
349 ah->arc_type = ac->arc_type;
355 * Defragmenter. Returns mbuf if last packet found, else
356 * NULL. frees imcoming mbuf as necessary.
359 static __inline struct mbuf *
360 arc_defrag(struct ifnet *ifp, struct mbuf *m)
362 struct arc_header *ah, *ah1;
370 ac = (struct arccom *)ifp->if_l2com;
372 if (m->m_len < ARC_HDRNEWLEN) {
373 m = m_pullup(m, ARC_HDRNEWLEN);
380 ah = mtod(m, struct arc_header *);
383 if (!arc_isphds(typ))
389 if (ah->arc_flag == 0xff) {
392 if (m->m_len < ARC_HDRNEWLEN) {
393 m = m_pullup(m, ARC_HDRNEWLEN);
400 ah = mtod(m, struct arc_header *);
403 af = &ac->ac_fragtab[src];
405 s = "debug code error";
407 if (ah->arc_flag & 1) {
409 * first fragment. We always initialize, which is
410 * about the right thing to do, as we only want to
411 * accept one fragmented packet per src at a time.
418 af->af_maxflag = ah->arc_flag;
420 af->af_seqid = ah->arc_seqid;
425 /* check for unfragmented packet */
426 if (ah->arc_flag == 0)
429 /* do we have a first packet from that src? */
435 ah1 = mtod(m1, struct arc_header *);
437 if (ah->arc_seqid != ah1->arc_seqid) {
442 if (typ != ah1->arc_type) {
447 if (dst != ah1->arc_dhost) {
448 s = "dest host differs";
452 /* typ, seqid and dst are ok here. */
454 if (ah->arc_flag == af->af_lastseen) {
459 if (ah->arc_flag == af->af_lastseen + 2) {
460 /* ok, this is next fragment */
461 af->af_lastseen = ah->arc_flag;
462 m_adj(m,ARC_HDRNEWLEN);
465 * m_cat might free the first mbuf (with pkthdr)
466 * in 2nd chain; therefore:
469 newflen = m->m_pkthdr.len;
473 m1->m_pkthdr.len += newflen;
475 /* is it the last one? */
476 if (af->af_lastseen > af->af_maxflag) {
477 af->af_packet = NULL;
483 /* if all else fails, it is out of sequence, too */
488 af->af_packet = NULL;
494 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
501 * return 1 if Packet Header Definition Standard, else 0.
502 * For now: old IP, old ARP aren't obviously. Lacking correct information,
503 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
504 * (Apple and Novell corporations were involved, among others, in PHDS work).
505 * Easiest is to assume that everybody else uses that, too.
508 arc_isphds(u_int8_t type)
510 return (type != ARCTYPE_IP_OLD &&
511 type != ARCTYPE_ARP_OLD &&
512 type != ARCTYPE_DIAGNOSE);
516 * Process a received Arcnet packet;
517 * the packet is in the mbuf chain m with
521 arc_input(struct ifnet *ifp, struct mbuf *m)
523 struct arc_header *ah;
527 if ((ifp->if_flags & IFF_UP) == 0) {
532 /* possibly defragment: */
533 m = arc_defrag(ifp, m);
539 ah = mtod(m, struct arc_header *);
540 /* does this belong to us? */
541 if ((ifp->if_flags & IFF_PROMISC) == 0
542 && ah->arc_dhost != arcbroadcastaddr
543 && ah->arc_dhost != ARC_LLADDR(ifp)) {
548 ifp->if_ibytes += m->m_pkthdr.len;
550 if (ah->arc_dhost == arcbroadcastaddr) {
551 m->m_flags |= M_BCAST|M_MCAST;
555 atype = ah->arc_type;
559 m_adj(m, ARC_HDRNEWLEN);
560 if ((m = ip_fastforward(m)) == NULL)
566 m_adj(m, ARC_HDRLEN);
567 if ((m = ip_fastforward(m)) == NULL)
573 if (ifp->if_flags & IFF_NOARP) {
574 /* Discard packet if ARP is disabled on interface */
578 m_adj(m, ARC_HDRNEWLEN);
580 #ifdef ARCNET_ALLOW_BROKEN_ARP
581 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
585 case ARCTYPE_ARP_OLD:
586 if (ifp->if_flags & IFF_NOARP) {
587 /* Discard packet if ARP is disabled on interface */
591 m_adj(m, ARC_HDRLEN);
593 #ifdef ARCNET_ALLOW_BROKEN_ARP
594 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
600 m_adj(m, ARC_HDRNEWLEN);
606 m_adj(m, ARC_HDRNEWLEN);
614 M_SETFIB(m, ifp->if_fib);
615 netisr_dispatch(isr, m);
619 * Register (new) link level address.
622 arc_storelladdr(struct ifnet *ifp, u_int8_t lla)
624 ARC_LLADDR(ifp) = lla;
628 * Perform common duties while attaching to interface list
631 arc_ifattach(struct ifnet *ifp, u_int8_t lla)
634 struct sockaddr_dl *sdl;
639 ifp->if_hdrlen = ARC_HDRLEN;
641 ifp->if_resolvemulti = arc_resolvemulti;
642 if (ifp->if_baudrate == 0)
643 ifp->if_baudrate = 2500000;
645 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
646 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
647 sdl->sdl_type = IFT_ARCNET;
648 sdl->sdl_alen = ifp->if_addrlen;
650 if (ifp->if_flags & IFF_BROADCAST)
651 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
653 ac = (struct arccom *)ifp->if_l2com;
654 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
656 /* XXX this message isn't entirely clear, to me -- cgd */
657 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n",
658 ifp->if_xname, ifp->if_xname);
660 arc_storelladdr(ifp, lla);
662 ifp->if_broadcastaddr = &arcbroadcastaddr;
664 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
668 arc_ifdetach(struct ifnet *ifp)
675 arc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
677 struct ifaddr *ifa = (struct ifaddr *) data;
678 struct ifreq *ifr = (struct ifreq *) data;
683 ifp->if_flags |= IFF_UP;
684 switch (ifa->ifa_addr->sa_family) {
687 ifp->if_init(ifp->if_softc); /* before arpwhohas */
688 arp_ifinit(ifp, ifa);
693 * XXX This code is probably wrong
697 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
699 if (ipx_nullhost(*ina))
700 ina->x_host.c_host[5] = ARC_LLADDR(ifp);
702 arc_storelladdr(ifp, ina->x_host.c_host[5]);
707 ifp->if_init(ifp->if_softc);
712 ifp->if_init(ifp->if_softc);
721 sa = (struct sockaddr *) &ifr->ifr_data;
722 *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
729 error = EAFNOSUPPORT;
731 switch (ifr->ifr_addr.sa_family) {
737 error = EAFNOSUPPORT;
745 * Set the interface MTU.
746 * mtu can't be larger than ARCMTU for RFC1051
747 * and can't be larger than ARC_PHDS_MTU
749 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
750 ifr->ifr_mtu > ARC_PHDS_MAXMTU)
753 ifp->if_mtu = ifr->ifr_mtu;
760 /* based on ether_resolvemulti() */
762 arc_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
765 struct sockaddr_dl *sdl;
767 struct sockaddr_in *sin;
770 struct sockaddr_in6 *sin6;
773 switch(sa->sa_family) {
776 * No mapping needed. Just check that it's a valid MC address.
778 sdl = (struct sockaddr_dl *)sa;
779 if (*LLADDR(sdl) != arcbroadcastaddr)
780 return EADDRNOTAVAIL;
785 sin = (struct sockaddr_in *)sa;
786 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
787 return EADDRNOTAVAIL;
788 sdl = malloc(sizeof *sdl, M_IFMADDR,
792 sdl->sdl_len = sizeof *sdl;
793 sdl->sdl_family = AF_LINK;
794 sdl->sdl_index = ifp->if_index;
795 sdl->sdl_type = IFT_ARCNET;
796 sdl->sdl_alen = ARC_ADDR_LEN;
798 *llsa = (struct sockaddr *)sdl;
803 sin6 = (struct sockaddr_in6 *)sa;
804 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
806 * An IP6 address of 0 means listen to all
807 * of the Ethernet multicast address used for IP6.
808 * (This is used for multicast routers.)
810 ifp->if_flags |= IFF_ALLMULTI;
814 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
815 return EADDRNOTAVAIL;
816 sdl = malloc(sizeof *sdl, M_IFMADDR,
820 sdl->sdl_len = sizeof *sdl;
821 sdl->sdl_family = AF_LINK;
822 sdl->sdl_index = ifp->if_index;
823 sdl->sdl_type = IFT_ARCNET;
824 sdl->sdl_alen = ARC_ADDR_LEN;
826 *llsa = (struct sockaddr *)sdl;
832 * Well, the text isn't quite right, but it's the name
839 static MALLOC_DEFINE(M_ARCCOM, "arccom", "ARCNET interface internals");
842 arc_alloc(u_char type, struct ifnet *ifp)
846 ac = malloc(sizeof(struct arccom), M_ARCCOM, M_WAITOK | M_ZERO);
853 arc_free(void *com, u_char type)
860 arc_modevent(module_t mod, int type, void *data)
865 if_register_com_alloc(IFT_ARCNET, arc_alloc, arc_free);
868 if_deregister_com_alloc(IFT_ARCNET);
877 static moduledata_t arc_mod = {
883 DECLARE_MODULE(arcnet, arc_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
884 MODULE_VERSION(arcnet, 1);