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"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/malloc.h>
50 #include <sys/protosw.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/errno.h>
54 #include <sys/syslog.h>
56 #include <machine/cpu.h>
59 #include <net/if_var.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>
79 #define ARCNET_ALLOW_BROKEN_ARP
81 static struct mbuf *arc_defrag(struct ifnet *, struct mbuf *);
82 static int arc_resolvemulti(struct ifnet *, struct sockaddr **,
85 u_int8_t arcbroadcastaddr = 0;
87 #define ARC_LLADDR(ifp) (*(u_int8_t *)IF_LLADDR(ifp))
89 #define senderr(e) { error = (e); goto bad;}
90 #define SIN(s) ((const struct sockaddr_in *)(s))
93 * ARCnet output routine.
94 * Encapsulate a packet of type family for the local net.
95 * Assumes that ifp is actually pointer to arccom structure.
98 arc_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
101 struct arc_header *ah;
103 u_int8_t atype, adst;
106 #if defined(INET) || defined(INET6)
110 if (!((ifp->if_flags & IFF_UP) &&
111 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
112 return(ENETDOWN); /* m, m1 aren't initialized yet */
116 switch (dst->sa_family) {
121 * For now, use the simple IP addr -> ARCnet addr mapping
123 if (m->m_flags & (M_BCAST|M_MCAST))
124 adst = arcbroadcastaddr; /* ARCnet broadcast address */
125 else if (ifp->if_flags & IFF_NOARP)
126 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
128 error = arpresolve(ifp, ro ? ro->ro_rt : NULL,
129 m, dst, &adst, &lle);
131 return (error == EWOULDBLOCK ? 0 : error);
134 atype = (ifp->if_flags & IFF_LINK0) ?
135 ARCTYPE_IP_OLD : ARCTYPE_IP;
140 ah = mtod(m, struct arphdr *);
141 ah->ar_hrd = htons(ARPHRD_ARCNET);
143 loop_copy = -1; /* if this is for us, don't do it */
145 switch(ntohs(ah->ar_op)) {
146 case ARPOP_REVREQUEST:
148 atype = ARCTYPE_REVARP;
157 if (m->m_flags & M_BCAST)
158 bcopy(ifp->if_broadcastaddr, &adst, ARC_ADDR_LEN);
160 bcopy(ar_tha(ah), &adst, ARC_ADDR_LEN);
167 error = nd6_storelladdr(ifp, m, dst, (u_char *)&adst, &lle);
170 atype = ARCTYPE_INET6;
175 const struct arc_header *ah;
178 ah = (const struct arc_header *)dst->sa_data;
179 adst = ah->arc_dhost;
180 atype = ah->arc_type;
182 if (atype == ARCTYPE_ARP) {
183 atype = (ifp->if_flags & IFF_LINK0) ?
184 ARCTYPE_ARP_OLD: ARCTYPE_ARP;
186 #ifdef ARCNET_ALLOW_BROKEN_ARP
188 * XXX It's not clear per RFC826 if this is needed, but
189 * "assigned numbers" say this is wrong.
190 * However, e.g., AmiTCP 3.0Beta used it... we make this
191 * switchable for emergency cases. Not perfect, but...
193 if (ifp->if_flags & IFF_LINK2)
194 mtod(m, struct arphdr *)->ar_pro = atype - 1;
200 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
201 senderr(EAFNOSUPPORT);
204 isphds = arc_isphds(atype);
205 M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_NOWAIT);
208 ah = mtod(m, struct arc_header *);
209 ah->arc_type = atype;
210 ah->arc_dhost = adst;
211 ah->arc_shost = ARC_LLADDR(ifp);
217 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
218 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
219 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
221 (void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
222 } else if (ah->arc_dhost == ah->arc_shost) {
223 (void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
224 return (0); /* XXX */
230 error = ifp->if_transmit(ifp, m);
241 arc_frag_init(struct ifnet *ifp)
245 ac = (struct arccom *)ifp->if_l2com;
250 arc_frag_next(struct ifnet *ifp)
254 struct arc_header *ah;
256 ac = (struct arccom *)ifp->if_l2com;
257 if ((m = ac->curr_frag) == 0) {
260 /* dequeue new packet */
261 IF_DEQUEUE(&ifp->if_snd, m);
265 ah = mtod(m, struct arc_header *);
266 if (!arc_isphds(ah->arc_type))
269 ++ac->ac_seqid; /* make the seqid unique */
270 tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
271 ac->fsflag = 2 * tfrags - 3;
273 ac->rsflag = ac->fsflag;
274 ac->arc_dhost = ah->arc_dhost;
275 ac->arc_shost = ah->arc_shost;
276 ac->arc_type = ah->arc_type;
278 m_adj(m, ARC_HDRNEWLEN);
282 /* split out next fragment and return it */
283 if (ac->sflag < ac->fsflag) {
284 /* we CAN'T have short packets here */
285 ac->curr_frag = m_split(m, ARC_MAX_DATA, M_NOWAIT);
286 if (ac->curr_frag == 0) {
291 M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT);
293 m_freem(ac->curr_frag);
298 ah = mtod(m, struct arc_header *);
299 ah->arc_flag = ac->rsflag;
300 ah->arc_seqid = ac->ac_seqid;
303 ac->rsflag = ac->sflag;
304 } else if ((m->m_pkthdr.len >=
305 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
307 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
310 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_NOWAIT);
314 ah = mtod(m, struct arc_header *);
316 ah->arc_seqid = 0xFFFF;
317 ah->arc_type2 = ac->arc_type;
318 ah->arc_flag2 = ac->sflag;
319 ah->arc_seqid2 = ac->ac_seqid;
323 M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT);
327 ah = mtod(m, struct arc_header *);
328 ah->arc_flag = ac->sflag;
329 ah->arc_seqid = ac->ac_seqid;
332 ah->arc_dhost = ac->arc_dhost;
333 ah->arc_shost = ac->arc_shost;
334 ah->arc_type = ac->arc_type;
340 * Defragmenter. Returns mbuf if last packet found, else
341 * NULL. frees imcoming mbuf as necessary.
344 static __inline struct mbuf *
345 arc_defrag(struct ifnet *ifp, struct mbuf *m)
347 struct arc_header *ah, *ah1;
355 ac = (struct arccom *)ifp->if_l2com;
357 if (m->m_len < ARC_HDRNEWLEN) {
358 m = m_pullup(m, ARC_HDRNEWLEN);
365 ah = mtod(m, struct arc_header *);
368 if (!arc_isphds(typ))
374 if (ah->arc_flag == 0xff) {
377 if (m->m_len < ARC_HDRNEWLEN) {
378 m = m_pullup(m, ARC_HDRNEWLEN);
385 ah = mtod(m, struct arc_header *);
388 af = &ac->ac_fragtab[src];
390 s = "debug code error";
392 if (ah->arc_flag & 1) {
394 * first fragment. We always initialize, which is
395 * about the right thing to do, as we only want to
396 * accept one fragmented packet per src at a time.
403 af->af_maxflag = ah->arc_flag;
405 af->af_seqid = ah->arc_seqid;
410 /* check for unfragmented packet */
411 if (ah->arc_flag == 0)
414 /* do we have a first packet from that src? */
420 ah1 = mtod(m1, struct arc_header *);
422 if (ah->arc_seqid != ah1->arc_seqid) {
427 if (typ != ah1->arc_type) {
432 if (dst != ah1->arc_dhost) {
433 s = "dest host differs";
437 /* typ, seqid and dst are ok here. */
439 if (ah->arc_flag == af->af_lastseen) {
444 if (ah->arc_flag == af->af_lastseen + 2) {
445 /* ok, this is next fragment */
446 af->af_lastseen = ah->arc_flag;
447 m_adj(m,ARC_HDRNEWLEN);
450 * m_cat might free the first mbuf (with pkthdr)
451 * in 2nd chain; therefore:
454 newflen = m->m_pkthdr.len;
458 m1->m_pkthdr.len += newflen;
460 /* is it the last one? */
461 if (af->af_lastseen > af->af_maxflag) {
462 af->af_packet = NULL;
468 /* if all else fails, it is out of sequence, too */
473 af->af_packet = NULL;
479 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
486 * return 1 if Packet Header Definition Standard, else 0.
487 * For now: old IP, old ARP aren't obviously. Lacking correct information,
488 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
489 * (Apple and Novell corporations were involved, among others, in PHDS work).
490 * Easiest is to assume that everybody else uses that, too.
493 arc_isphds(u_int8_t type)
495 return (type != ARCTYPE_IP_OLD &&
496 type != ARCTYPE_ARP_OLD &&
497 type != ARCTYPE_DIAGNOSE);
501 * Process a received Arcnet packet;
502 * the packet is in the mbuf chain m with
506 arc_input(struct ifnet *ifp, struct mbuf *m)
508 struct arc_header *ah;
512 if ((ifp->if_flags & IFF_UP) == 0) {
517 /* possibly defragment: */
518 m = arc_defrag(ifp, m);
524 ah = mtod(m, struct arc_header *);
525 /* does this belong to us? */
526 if ((ifp->if_flags & IFF_PROMISC) == 0
527 && ah->arc_dhost != arcbroadcastaddr
528 && ah->arc_dhost != ARC_LLADDR(ifp)) {
533 ifp->if_ibytes += m->m_pkthdr.len;
535 if (ah->arc_dhost == arcbroadcastaddr) {
536 m->m_flags |= M_BCAST|M_MCAST;
540 atype = ah->arc_type;
544 m_adj(m, ARC_HDRNEWLEN);
545 if ((m = ip_fastforward(m)) == NULL)
551 m_adj(m, ARC_HDRLEN);
552 if ((m = ip_fastforward(m)) == NULL)
558 if (ifp->if_flags & IFF_NOARP) {
559 /* Discard packet if ARP is disabled on interface */
563 m_adj(m, ARC_HDRNEWLEN);
565 #ifdef ARCNET_ALLOW_BROKEN_ARP
566 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
570 case ARCTYPE_ARP_OLD:
571 if (ifp->if_flags & IFF_NOARP) {
572 /* Discard packet if ARP is disabled on interface */
576 m_adj(m, ARC_HDRLEN);
578 #ifdef ARCNET_ALLOW_BROKEN_ARP
579 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
585 m_adj(m, ARC_HDRNEWLEN);
593 M_SETFIB(m, ifp->if_fib);
594 netisr_dispatch(isr, m);
598 * Register (new) link level address.
601 arc_storelladdr(struct ifnet *ifp, u_int8_t lla)
603 ARC_LLADDR(ifp) = lla;
607 * Perform common duties while attaching to interface list
610 arc_ifattach(struct ifnet *ifp, u_int8_t lla)
613 struct sockaddr_dl *sdl;
618 ifp->if_hdrlen = ARC_HDRLEN;
620 ifp->if_resolvemulti = arc_resolvemulti;
621 if (ifp->if_baudrate == 0)
622 ifp->if_baudrate = 2500000;
624 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
625 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
626 sdl->sdl_type = IFT_ARCNET;
627 sdl->sdl_alen = ifp->if_addrlen;
629 if (ifp->if_flags & IFF_BROADCAST)
630 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
632 ac = (struct arccom *)ifp->if_l2com;
633 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
635 /* XXX this message isn't entirely clear, to me -- cgd */
636 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n",
637 ifp->if_xname, ifp->if_xname);
639 arc_storelladdr(ifp, lla);
641 ifp->if_broadcastaddr = &arcbroadcastaddr;
643 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
647 arc_ifdetach(struct ifnet *ifp)
654 arc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
656 struct ifaddr *ifa = (struct ifaddr *) data;
657 struct ifreq *ifr = (struct ifreq *) data;
662 ifp->if_flags |= IFF_UP;
663 switch (ifa->ifa_addr->sa_family) {
666 ifp->if_init(ifp->if_softc); /* before arpwhohas */
667 arp_ifinit(ifp, ifa);
671 ifp->if_init(ifp->if_softc);
680 sa = (struct sockaddr *) &ifr->ifr_data;
681 *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
688 error = EAFNOSUPPORT;
690 switch (ifr->ifr_addr.sa_family) {
696 error = EAFNOSUPPORT;
704 * Set the interface MTU.
705 * mtu can't be larger than ARCMTU for RFC1051
706 * and can't be larger than ARC_PHDS_MTU
708 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
709 ifr->ifr_mtu > ARC_PHDS_MAXMTU)
712 ifp->if_mtu = ifr->ifr_mtu;
719 /* based on ether_resolvemulti() */
721 arc_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
724 struct sockaddr_dl *sdl;
726 struct sockaddr_in *sin;
729 struct sockaddr_in6 *sin6;
732 switch(sa->sa_family) {
735 * No mapping needed. Just check that it's a valid MC address.
737 sdl = (struct sockaddr_dl *)sa;
738 if (*LLADDR(sdl) != arcbroadcastaddr)
739 return EADDRNOTAVAIL;
744 sin = (struct sockaddr_in *)sa;
745 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
746 return EADDRNOTAVAIL;
747 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
748 sdl->sdl_alen = ARC_ADDR_LEN;
750 *llsa = (struct sockaddr *)sdl;
755 sin6 = (struct sockaddr_in6 *)sa;
756 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
758 * An IP6 address of 0 means listen to all
759 * of the Ethernet multicast address used for IP6.
760 * (This is used for multicast routers.)
762 ifp->if_flags |= IFF_ALLMULTI;
766 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
767 return EADDRNOTAVAIL;
768 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
769 sdl->sdl_alen = ARC_ADDR_LEN;
771 *llsa = (struct sockaddr *)sdl;
777 * Well, the text isn't quite right, but it's the name
784 static MALLOC_DEFINE(M_ARCCOM, "arccom", "ARCNET interface internals");
787 arc_alloc(u_char type, struct ifnet *ifp)
791 ac = malloc(sizeof(struct arccom), M_ARCCOM, M_WAITOK | M_ZERO);
798 arc_free(void *com, u_char type)
805 arc_modevent(module_t mod, int type, void *data)
810 if_register_com_alloc(IFT_ARCNET, arc_alloc, arc_free);
813 if_deregister_com_alloc(IFT_ARCNET);
822 static moduledata_t arc_mod = {
828 DECLARE_MODULE(arcnet, arc_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
829 MODULE_VERSION(arcnet, 1);