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) ((struct sockaddr_in *)s)
96 #define SIPX(s) ((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, 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 ah = (struct arc_header *)dst->sa_data;
191 adst = ah->arc_dhost;
192 atype = ah->arc_type;
194 if (atype == ARCTYPE_ARP) {
195 atype = (ifp->if_flags & IFF_LINK0) ?
196 ARCTYPE_ARP_OLD: ARCTYPE_ARP;
198 #ifdef ARCNET_ALLOW_BROKEN_ARP
200 * XXX It's not clear per RFC826 if this is needed, but
201 * "assigned numbers" say this is wrong.
202 * However, e.g., AmiTCP 3.0Beta used it... we make this
203 * switchable for emergency cases. Not perfect, but...
205 if (ifp->if_flags & IFF_LINK2)
206 mtod(m, struct arphdr *)->ar_pro = atype - 1;
212 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
213 senderr(EAFNOSUPPORT);
216 isphds = arc_isphds(atype);
217 M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_DONTWAIT);
220 ah = mtod(m, struct arc_header *);
221 ah->arc_type = atype;
222 ah->arc_dhost = adst;
223 ah->arc_shost = ARC_LLADDR(ifp);
229 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
230 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
231 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
233 (void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
234 } else if (ah->arc_dhost == ah->arc_shost) {
235 (void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
236 return (0); /* XXX */
242 error = ifp->if_transmit(ifp, m);
253 arc_frag_init(struct ifnet *ifp)
257 ac = (struct arccom *)ifp->if_l2com;
262 arc_frag_next(struct ifnet *ifp)
266 struct arc_header *ah;
268 ac = (struct arccom *)ifp->if_l2com;
269 if ((m = ac->curr_frag) == 0) {
272 /* dequeue new packet */
273 IF_DEQUEUE(&ifp->if_snd, m);
277 ah = mtod(m, struct arc_header *);
278 if (!arc_isphds(ah->arc_type))
281 ++ac->ac_seqid; /* make the seqid unique */
282 tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
283 ac->fsflag = 2 * tfrags - 3;
285 ac->rsflag = ac->fsflag;
286 ac->arc_dhost = ah->arc_dhost;
287 ac->arc_shost = ah->arc_shost;
288 ac->arc_type = ah->arc_type;
290 m_adj(m, ARC_HDRNEWLEN);
294 /* split out next fragment and return it */
295 if (ac->sflag < ac->fsflag) {
296 /* we CAN'T have short packets here */
297 ac->curr_frag = m_split(m, ARC_MAX_DATA, M_DONTWAIT);
298 if (ac->curr_frag == 0) {
303 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
305 m_freem(ac->curr_frag);
310 ah = mtod(m, struct arc_header *);
311 ah->arc_flag = ac->rsflag;
312 ah->arc_seqid = ac->ac_seqid;
315 ac->rsflag = ac->sflag;
316 } else if ((m->m_pkthdr.len >=
317 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
319 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
322 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
326 ah = mtod(m, struct arc_header *);
328 ah->arc_seqid = 0xFFFF;
329 ah->arc_type2 = ac->arc_type;
330 ah->arc_flag2 = ac->sflag;
331 ah->arc_seqid2 = ac->ac_seqid;
335 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
339 ah = mtod(m, struct arc_header *);
340 ah->arc_flag = ac->sflag;
341 ah->arc_seqid = ac->ac_seqid;
344 ah->arc_dhost = ac->arc_dhost;
345 ah->arc_shost = ac->arc_shost;
346 ah->arc_type = ac->arc_type;
352 * Defragmenter. Returns mbuf if last packet found, else
353 * NULL. frees imcoming mbuf as necessary.
356 static __inline struct mbuf *
357 arc_defrag(struct ifnet *ifp, struct mbuf *m)
359 struct arc_header *ah, *ah1;
367 ac = (struct arccom *)ifp->if_l2com;
369 if (m->m_len < ARC_HDRNEWLEN) {
370 m = m_pullup(m, ARC_HDRNEWLEN);
377 ah = mtod(m, struct arc_header *);
380 if (!arc_isphds(typ))
386 if (ah->arc_flag == 0xff) {
389 if (m->m_len < ARC_HDRNEWLEN) {
390 m = m_pullup(m, ARC_HDRNEWLEN);
397 ah = mtod(m, struct arc_header *);
400 af = &ac->ac_fragtab[src];
402 s = "debug code error";
404 if (ah->arc_flag & 1) {
406 * first fragment. We always initialize, which is
407 * about the right thing to do, as we only want to
408 * accept one fragmented packet per src at a time.
415 af->af_maxflag = ah->arc_flag;
417 af->af_seqid = ah->arc_seqid;
422 /* check for unfragmented packet */
423 if (ah->arc_flag == 0)
426 /* do we have a first packet from that src? */
432 ah1 = mtod(m1, struct arc_header *);
434 if (ah->arc_seqid != ah1->arc_seqid) {
439 if (typ != ah1->arc_type) {
444 if (dst != ah1->arc_dhost) {
445 s = "dest host differs";
449 /* typ, seqid and dst are ok here. */
451 if (ah->arc_flag == af->af_lastseen) {
456 if (ah->arc_flag == af->af_lastseen + 2) {
457 /* ok, this is next fragment */
458 af->af_lastseen = ah->arc_flag;
459 m_adj(m,ARC_HDRNEWLEN);
462 * m_cat might free the first mbuf (with pkthdr)
463 * in 2nd chain; therefore:
466 newflen = m->m_pkthdr.len;
470 m1->m_pkthdr.len += newflen;
472 /* is it the last one? */
473 if (af->af_lastseen > af->af_maxflag) {
474 af->af_packet = NULL;
480 /* if all else fails, it is out of sequence, too */
485 af->af_packet = NULL;
491 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
498 * return 1 if Packet Header Definition Standard, else 0.
499 * For now: old IP, old ARP aren't obviously. Lacking correct information,
500 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
501 * (Apple and Novell corporations were involved, among others, in PHDS work).
502 * Easiest is to assume that everybody else uses that, too.
505 arc_isphds(u_int8_t type)
507 return (type != ARCTYPE_IP_OLD &&
508 type != ARCTYPE_ARP_OLD &&
509 type != ARCTYPE_DIAGNOSE);
513 * Process a received Arcnet packet;
514 * the packet is in the mbuf chain m with
518 arc_input(struct ifnet *ifp, struct mbuf *m)
520 struct arc_header *ah;
524 if ((ifp->if_flags & IFF_UP) == 0) {
529 /* possibly defragment: */
530 m = arc_defrag(ifp, m);
536 ah = mtod(m, struct arc_header *);
537 /* does this belong to us? */
538 if ((ifp->if_flags & IFF_PROMISC) == 0
539 && ah->arc_dhost != arcbroadcastaddr
540 && ah->arc_dhost != ARC_LLADDR(ifp)) {
545 ifp->if_ibytes += m->m_pkthdr.len;
547 if (ah->arc_dhost == arcbroadcastaddr) {
548 m->m_flags |= M_BCAST|M_MCAST;
552 atype = ah->arc_type;
556 m_adj(m, ARC_HDRNEWLEN);
557 if ((m = ip_fastforward(m)) == NULL)
563 m_adj(m, ARC_HDRLEN);
564 if ((m = ip_fastforward(m)) == NULL)
570 if (ifp->if_flags & IFF_NOARP) {
571 /* Discard packet if ARP is disabled on interface */
575 m_adj(m, ARC_HDRNEWLEN);
577 #ifdef ARCNET_ALLOW_BROKEN_ARP
578 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
582 case ARCTYPE_ARP_OLD:
583 if (ifp->if_flags & IFF_NOARP) {
584 /* Discard packet if ARP is disabled on interface */
588 m_adj(m, ARC_HDRLEN);
590 #ifdef ARCNET_ALLOW_BROKEN_ARP
591 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
597 m_adj(m, ARC_HDRNEWLEN);
603 m_adj(m, ARC_HDRNEWLEN);
611 M_SETFIB(m, ifp->if_fib);
612 netisr_dispatch(isr, m);
616 * Register (new) link level address.
619 arc_storelladdr(struct ifnet *ifp, u_int8_t lla)
621 ARC_LLADDR(ifp) = lla;
625 * Perform common duties while attaching to interface list
628 arc_ifattach(struct ifnet *ifp, u_int8_t lla)
631 struct sockaddr_dl *sdl;
636 ifp->if_hdrlen = ARC_HDRLEN;
638 ifp->if_resolvemulti = arc_resolvemulti;
639 if (ifp->if_baudrate == 0)
640 ifp->if_baudrate = 2500000;
641 #if __FreeBSD_version < 500000
642 ifa = ifnet_addrs[ifp->if_index - 1];
646 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
647 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
648 sdl->sdl_type = IFT_ARCNET;
649 sdl->sdl_alen = ifp->if_addrlen;
651 if (ifp->if_flags & IFF_BROADCAST)
652 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
654 ac = (struct arccom *)ifp->if_l2com;
655 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
657 /* XXX this message isn't entirely clear, to me -- cgd */
658 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n",
659 ifp->if_xname, ifp->if_xname);
661 arc_storelladdr(ifp, lla);
663 ifp->if_broadcastaddr = &arcbroadcastaddr;
665 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
669 arc_ifdetach(struct ifnet *ifp)
676 arc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
678 struct ifaddr *ifa = (struct ifaddr *) data;
679 struct ifreq *ifr = (struct ifreq *) data;
684 ifp->if_flags |= IFF_UP;
685 switch (ifa->ifa_addr->sa_family) {
688 ifp->if_init(ifp->if_softc); /* before arpwhohas */
689 arp_ifinit(ifp, ifa);
694 * XXX This code is probably wrong
698 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
700 if (ipx_nullhost(*ina))
701 ina->x_host.c_host[5] = ARC_LLADDR(ifp);
703 arc_storelladdr(ifp, ina->x_host.c_host[5]);
708 ifp->if_init(ifp->if_softc);
713 ifp->if_init(ifp->if_softc);
722 sa = (struct sockaddr *) &ifr->ifr_data;
723 *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
730 error = EAFNOSUPPORT;
732 switch (ifr->ifr_addr.sa_family) {
738 error = EAFNOSUPPORT;
746 * Set the interface MTU.
747 * mtu can't be larger than ARCMTU for RFC1051
748 * and can't be larger than ARC_PHDS_MTU
750 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
751 ifr->ifr_mtu > ARC_PHDS_MAXMTU)
754 ifp->if_mtu = ifr->ifr_mtu;
761 /* based on ether_resolvemulti() */
763 arc_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
766 struct sockaddr_dl *sdl;
768 struct sockaddr_in *sin;
771 struct sockaddr_in6 *sin6;
774 switch(sa->sa_family) {
777 * No mapping needed. Just check that it's a valid MC address.
779 sdl = (struct sockaddr_dl *)sa;
780 if (*LLADDR(sdl) != arcbroadcastaddr)
781 return EADDRNOTAVAIL;
786 sin = (struct sockaddr_in *)sa;
787 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
788 return EADDRNOTAVAIL;
789 sdl = malloc(sizeof *sdl, M_IFMADDR,
793 sdl->sdl_len = sizeof *sdl;
794 sdl->sdl_family = AF_LINK;
795 sdl->sdl_index = ifp->if_index;
796 sdl->sdl_type = IFT_ARCNET;
797 sdl->sdl_alen = ARC_ADDR_LEN;
799 *llsa = (struct sockaddr *)sdl;
804 sin6 = (struct sockaddr_in6 *)sa;
805 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
807 * An IP6 address of 0 means listen to all
808 * of the Ethernet multicast address used for IP6.
809 * (This is used for multicast routers.)
811 ifp->if_flags |= IFF_ALLMULTI;
815 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
816 return EADDRNOTAVAIL;
817 sdl = malloc(sizeof *sdl, M_IFMADDR,
821 sdl->sdl_len = sizeof *sdl;
822 sdl->sdl_family = AF_LINK;
823 sdl->sdl_index = ifp->if_index;
824 sdl->sdl_type = IFT_ARCNET;
825 sdl->sdl_alen = ARC_ADDR_LEN;
827 *llsa = (struct sockaddr *)sdl;
833 * Well, the text isn't quite right, but it's the name
840 static MALLOC_DEFINE(M_ARCCOM, "arccom", "ARCNET interface internals");
843 arc_alloc(u_char type, struct ifnet *ifp)
847 ac = malloc(sizeof(struct arccom), M_ARCCOM, M_WAITOK | M_ZERO);
854 arc_free(void *com, u_char type)
861 arc_modevent(module_t mod, int type, void *data)
866 if_register_com_alloc(IFT_ARCNET, arc_alloc, arc_free);
869 if_deregister_com_alloc(IFT_ARCNET);
878 static moduledata_t arc_mod = {
884 DECLARE_MODULE(arcnet, arc_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
885 MODULE_VERSION(arcnet, 1);