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>
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 <netipx/ipx.h>
80 #include <netipx/ipx_if.h>
83 #define ARCNET_ALLOW_BROKEN_ARP
85 static struct mbuf *arc_defrag(struct ifnet *, struct mbuf *);
86 static int arc_resolvemulti(struct ifnet *, struct sockaddr **,
89 u_int8_t arcbroadcastaddr = 0;
91 #define ARC_LLADDR(ifp) (*(u_int8_t *)IF_LLADDR(ifp))
93 #define senderr(e) { error = (e); goto bad;}
94 #define SIN(s) ((struct sockaddr_in *)s)
95 #define SIPX(s) ((struct sockaddr_ipx *)s)
98 * ARCnet output routine.
99 * Encapsulate a packet of type family for the local net.
100 * Assumes that ifp is actually pointer to arccom structure.
103 arc_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
106 struct arc_header *ah;
108 u_int8_t atype, adst;
112 if (!((ifp->if_flags & IFF_UP) &&
113 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
114 return(ENETDOWN); /* m, m1 aren't initialized yet */
118 switch (dst->sa_family) {
123 * For now, use the simple IP addr -> ARCnet addr mapping
125 if (m->m_flags & (M_BCAST|M_MCAST))
126 adst = arcbroadcastaddr; /* ARCnet broadcast address */
127 else if (ifp->if_flags & IFF_NOARP)
128 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
130 error = arpresolve(ifp, rt0, m, dst, &adst);
132 return (error == EWOULDBLOCK ? 0 : error);
135 atype = (ifp->if_flags & IFF_LINK0) ?
136 ARCTYPE_IP_OLD : ARCTYPE_IP;
141 ah = mtod(m, struct arphdr *);
142 ah->ar_hrd = htons(ARPHRD_ARCNET);
144 loop_copy = -1; /* if this is for us, don't do it */
146 switch(ntohs(ah->ar_op)) {
147 case ARPOP_REVREQUEST:
149 atype = ARCTYPE_REVARP;
158 if (m->m_flags & M_BCAST)
159 bcopy(ifp->if_broadcastaddr, &adst, ARC_ADDR_LEN);
161 bcopy(ar_tha(ah), &adst, ARC_ADDR_LEN);
168 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)&adst);
171 atype = ARCTYPE_INET6;
176 adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
179 adst = arcbroadcastaddr;
185 ah = (struct arc_header *)dst->sa_data;
186 adst = ah->arc_dhost;
187 atype = ah->arc_type;
189 if (atype == ARCTYPE_ARP) {
190 atype = (ifp->if_flags & IFF_LINK0) ?
191 ARCTYPE_ARP_OLD: ARCTYPE_ARP;
193 #ifdef ARCNET_ALLOW_BROKEN_ARP
195 * XXX It's not clear per RFC826 if this is needed, but
196 * "assigned numbers" say this is wrong.
197 * However, e.g., AmiTCP 3.0Beta used it... we make this
198 * switchable for emergency cases. Not perfect, but...
200 if (ifp->if_flags & IFF_LINK2)
201 mtod(m, struct arphdr *)->ar_pro = atype - 1;
207 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
208 senderr(EAFNOSUPPORT);
211 isphds = arc_isphds(atype);
212 M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_DONTWAIT);
215 ah = mtod(m, struct arc_header *);
216 ah->arc_type = atype;
217 ah->arc_dhost = adst;
218 ah->arc_shost = ARC_LLADDR(ifp);
224 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
225 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
226 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
228 (void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
229 } else if (ah->arc_dhost == ah->arc_shost) {
230 (void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
231 return (0); /* XXX */
237 IFQ_HANDOFF(ifp, m, error);
248 arc_frag_init(struct ifnet *ifp)
252 ac = (struct arccom *)ifp->if_l2com;
257 arc_frag_next(struct ifnet *ifp)
261 struct arc_header *ah;
263 ac = (struct arccom *)ifp->if_l2com;
264 if ((m = ac->curr_frag) == 0) {
267 /* dequeue new packet */
268 IF_DEQUEUE(&ifp->if_snd, m);
272 ah = mtod(m, struct arc_header *);
273 if (!arc_isphds(ah->arc_type))
276 ++ac->ac_seqid; /* make the seqid unique */
277 tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
278 ac->fsflag = 2 * tfrags - 3;
280 ac->rsflag = ac->fsflag;
281 ac->arc_dhost = ah->arc_dhost;
282 ac->arc_shost = ah->arc_shost;
283 ac->arc_type = ah->arc_type;
285 m_adj(m, ARC_HDRNEWLEN);
289 /* split out next fragment and return it */
290 if (ac->sflag < ac->fsflag) {
291 /* we CAN'T have short packets here */
292 ac->curr_frag = m_split(m, ARC_MAX_DATA, M_DONTWAIT);
293 if (ac->curr_frag == 0) {
298 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
300 m_freem(ac->curr_frag);
305 ah = mtod(m, struct arc_header *);
306 ah->arc_flag = ac->rsflag;
307 ah->arc_seqid = ac->ac_seqid;
310 ac->rsflag = ac->sflag;
311 } else if ((m->m_pkthdr.len >=
312 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
314 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
317 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
321 ah = mtod(m, struct arc_header *);
323 ah->arc_seqid = 0xFFFF;
324 ah->arc_type2 = ac->arc_type;
325 ah->arc_flag2 = ac->sflag;
326 ah->arc_seqid2 = ac->ac_seqid;
330 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
334 ah = mtod(m, struct arc_header *);
335 ah->arc_flag = ac->sflag;
336 ah->arc_seqid = ac->ac_seqid;
339 ah->arc_dhost = ac->arc_dhost;
340 ah->arc_shost = ac->arc_shost;
341 ah->arc_type = ac->arc_type;
347 * Defragmenter. Returns mbuf if last packet found, else
348 * NULL. frees imcoming mbuf as necessary.
351 static __inline struct mbuf *
352 arc_defrag(struct ifnet *ifp, struct mbuf *m)
354 struct arc_header *ah, *ah1;
362 ac = (struct arccom *)ifp->if_l2com;
364 if (m->m_len < ARC_HDRNEWLEN) {
365 m = m_pullup(m, ARC_HDRNEWLEN);
372 ah = mtod(m, struct arc_header *);
375 if (!arc_isphds(typ))
381 if (ah->arc_flag == 0xff) {
384 if (m->m_len < ARC_HDRNEWLEN) {
385 m = m_pullup(m, ARC_HDRNEWLEN);
392 ah = mtod(m, struct arc_header *);
395 af = &ac->ac_fragtab[src];
397 s = "debug code error";
399 if (ah->arc_flag & 1) {
401 * first fragment. We always initialize, which is
402 * about the right thing to do, as we only want to
403 * accept one fragmented packet per src at a time.
410 af->af_maxflag = ah->arc_flag;
412 af->af_seqid = ah->arc_seqid;
417 /* check for unfragmented packet */
418 if (ah->arc_flag == 0)
421 /* do we have a first packet from that src? */
427 ah1 = mtod(m1, struct arc_header *);
429 if (ah->arc_seqid != ah1->arc_seqid) {
434 if (typ != ah1->arc_type) {
439 if (dst != ah1->arc_dhost) {
440 s = "dest host differs";
444 /* typ, seqid and dst are ok here. */
446 if (ah->arc_flag == af->af_lastseen) {
451 if (ah->arc_flag == af->af_lastseen + 2) {
452 /* ok, this is next fragment */
453 af->af_lastseen = ah->arc_flag;
454 m_adj(m,ARC_HDRNEWLEN);
457 * m_cat might free the first mbuf (with pkthdr)
458 * in 2nd chain; therefore:
461 newflen = m->m_pkthdr.len;
465 m1->m_pkthdr.len += newflen;
467 /* is it the last one? */
468 if (af->af_lastseen > af->af_maxflag) {
469 af->af_packet = NULL;
475 /* if all else fails, it is out of sequence, too */
480 af->af_packet = NULL;
486 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
493 * return 1 if Packet Header Definition Standard, else 0.
494 * For now: old IP, old ARP aren't obviously. Lacking correct information,
495 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
496 * (Apple and Novell corporations were involved, among others, in PHDS work).
497 * Easiest is to assume that everybody else uses that, too.
500 arc_isphds(u_int8_t type)
502 return (type != ARCTYPE_IP_OLD &&
503 type != ARCTYPE_ARP_OLD &&
504 type != ARCTYPE_DIAGNOSE);
508 * Process a received Arcnet packet;
509 * the packet is in the mbuf chain m with
513 arc_input(struct ifnet *ifp, struct mbuf *m)
515 struct arc_header *ah;
519 if ((ifp->if_flags & IFF_UP) == 0) {
524 /* possibly defragment: */
525 m = arc_defrag(ifp, m);
531 ah = mtod(m, struct arc_header *);
532 /* does this belong to us? */
533 if ((ifp->if_flags & IFF_PROMISC) == 0
534 && ah->arc_dhost != arcbroadcastaddr
535 && ah->arc_dhost != ARC_LLADDR(ifp)) {
540 ifp->if_ibytes += m->m_pkthdr.len;
542 if (ah->arc_dhost == arcbroadcastaddr) {
543 m->m_flags |= M_BCAST|M_MCAST;
547 atype = ah->arc_type;
551 m_adj(m, ARC_HDRNEWLEN);
552 if ((m = ip_fastforward(m)) == NULL)
558 m_adj(m, ARC_HDRLEN);
559 if ((m = ip_fastforward(m)) == NULL)
565 if (ifp->if_flags & IFF_NOARP) {
566 /* Discard packet if ARP is disabled on interface */
570 m_adj(m, ARC_HDRNEWLEN);
572 #ifdef ARCNET_ALLOW_BROKEN_ARP
573 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
577 case ARCTYPE_ARP_OLD:
578 if (ifp->if_flags & IFF_NOARP) {
579 /* Discard packet if ARP is disabled on interface */
583 m_adj(m, ARC_HDRLEN);
585 #ifdef ARCNET_ALLOW_BROKEN_ARP
586 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
592 m_adj(m, ARC_HDRNEWLEN);
598 m_adj(m, ARC_HDRNEWLEN);
606 netisr_dispatch(isr, m);
610 * Register (new) link level address.
613 arc_storelladdr(struct ifnet *ifp, u_int8_t lla)
615 ARC_LLADDR(ifp) = lla;
619 * Perform common duties while attaching to interface list
622 arc_ifattach(struct ifnet *ifp, u_int8_t lla)
625 struct sockaddr_dl *sdl;
630 ifp->if_hdrlen = ARC_HDRLEN;
632 ifp->if_resolvemulti = arc_resolvemulti;
633 if (ifp->if_baudrate == 0)
634 ifp->if_baudrate = 2500000;
635 #if __FreeBSD_version < 500000
636 ifa = ifnet_addrs[ifp->if_index - 1];
640 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
641 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
642 sdl->sdl_type = IFT_ARCNET;
643 sdl->sdl_alen = ifp->if_addrlen;
645 if (ifp->if_flags & IFF_BROADCAST)
646 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
648 ac = (struct arccom *)ifp->if_l2com;
649 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
651 /* XXX this message isn't entirely clear, to me -- cgd */
652 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n",
653 ifp->if_xname, ifp->if_xname);
655 arc_storelladdr(ifp, lla);
657 ifp->if_broadcastaddr = &arcbroadcastaddr;
659 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
663 arc_ifdetach(struct ifnet *ifp)
670 arc_ioctl(struct ifnet *ifp, int command, caddr_t data)
672 struct ifaddr *ifa = (struct ifaddr *) data;
673 struct ifreq *ifr = (struct ifreq *) data;
678 ifp->if_flags |= IFF_UP;
679 switch (ifa->ifa_addr->sa_family) {
682 ifp->if_init(ifp->if_softc); /* before arpwhohas */
683 arp_ifinit(ifp, ifa);
688 * XXX This code is probably wrong
692 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
694 if (ipx_nullhost(*ina))
695 ina->x_host.c_host[5] = ARC_LLADDR(ifp);
697 arc_storelladdr(ifp, ina->x_host.c_host[5]);
702 ifp->if_init(ifp->if_softc);
707 ifp->if_init(ifp->if_softc);
716 sa = (struct sockaddr *) &ifr->ifr_data;
717 *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
724 error = EAFNOSUPPORT;
726 switch (ifr->ifr_addr.sa_family) {
732 error = EAFNOSUPPORT;
740 * Set the interface MTU.
741 * mtu can't be larger than ARCMTU for RFC1051
742 * and can't be larger than ARC_PHDS_MTU
744 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
745 ifr->ifr_mtu > ARC_PHDS_MAXMTU)
748 ifp->if_mtu = ifr->ifr_mtu;
755 /* based on ether_resolvemulti() */
757 arc_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
760 struct sockaddr_dl *sdl;
761 struct sockaddr_in *sin;
763 struct sockaddr_in6 *sin6;
766 switch(sa->sa_family) {
769 * No mapping needed. Just check that it's a valid MC address.
771 sdl = (struct sockaddr_dl *)sa;
772 if (*LLADDR(sdl) != arcbroadcastaddr)
773 return EADDRNOTAVAIL;
778 sin = (struct sockaddr_in *)sa;
779 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
780 return EADDRNOTAVAIL;
781 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
785 sdl->sdl_len = sizeof *sdl;
786 sdl->sdl_family = AF_LINK;
787 sdl->sdl_index = ifp->if_index;
788 sdl->sdl_type = IFT_ARCNET;
789 sdl->sdl_alen = ARC_ADDR_LEN;
791 *llsa = (struct sockaddr *)sdl;
796 sin6 = (struct sockaddr_in6 *)sa;
797 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
799 * An IP6 address of 0 means listen to all
800 * of the Ethernet multicast address used for IP6.
801 * (This is used for multicast routers.)
803 ifp->if_flags |= IFF_ALLMULTI;
807 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
808 return EADDRNOTAVAIL;
809 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
813 sdl->sdl_len = sizeof *sdl;
814 sdl->sdl_family = AF_LINK;
815 sdl->sdl_index = ifp->if_index;
816 sdl->sdl_type = IFT_ARCNET;
817 sdl->sdl_alen = ARC_ADDR_LEN;
819 *llsa = (struct sockaddr *)sdl;
825 * Well, the text isn't quite right, but it's the name
832 MALLOC_DEFINE(M_ARCCOM, "arccom", "ARCNET interface internals");
835 arc_alloc(u_char type, struct ifnet *ifp)
839 ac = malloc(sizeof(struct arccom), M_ARCCOM, M_WAITOK | M_ZERO);
846 arc_free(void *com, u_char type)
853 arc_modevent(module_t mod, int type, void *data)
858 if_register_com_alloc(IFT_ARCNET, arc_alloc, arc_free);
861 if_deregister_com_alloc(IFT_ARCNET);
870 static moduledata_t arc_mod = {
876 DECLARE_MODULE(arcnet, arc_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
877 MODULE_VERSION(arcnet, 1);