1 /* $NetBSD: if_arcsubr.c,v 1.36 2001/06/14 05:44:23 itojun Exp $ */
5 * SPDX-License-Identifier: BSD-4-Clause
7 * Copyright (c) 1994, 1995 Ignatios Souvatzis
8 * Copyright (c) 1982, 1989, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
40 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
44 #include "opt_inet6.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/module.h>
50 #include <sys/malloc.h>
52 #include <sys/protosw.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/errno.h>
56 #include <sys/syslog.h>
58 #include <machine/cpu.h>
61 #include <net/if_var.h>
62 #include <net/netisr.h>
63 #include <net/route.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/if_arc.h>
67 #include <net/if_arp.h>
69 #include <net/if_llatbl.h>
71 #if defined(INET) || defined(INET6)
72 #include <netinet/in.h>
73 #include <netinet/in_var.h>
74 #include <netinet/if_ether.h>
78 #include <netinet6/nd6.h>
81 #define ARCNET_ALLOW_BROKEN_ARP
83 static struct mbuf *arc_defrag(struct ifnet *, struct mbuf *);
84 static int arc_resolvemulti(struct ifnet *, struct sockaddr **,
87 u_int8_t arcbroadcastaddr = 0;
89 #define ARC_LLADDR(ifp) (*(u_int8_t *)IF_LLADDR(ifp))
91 #define senderr(e) { error = (e); goto bad;}
92 #define SIN(s) ((const struct sockaddr_in *)(s))
95 * ARCnet output routine.
96 * Encapsulate a packet of type family for the local net.
97 * Assumes that ifp is actually pointer to arccom structure.
100 arc_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
103 struct arc_header *ah;
105 u_int8_t atype, adst;
108 #if defined(INET) || defined(INET6)
112 if (!((ifp->if_flags & IFF_UP) &&
113 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
114 return(ENETDOWN); /* m, m1 aren't initialized yet */
117 #if defined(INET) || defined(INET6)
119 is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
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, is_gw, m, dst, &adst, NULL,
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 if ((m->m_flags & M_MCAST) != 0)
174 adst = arcbroadcastaddr; /* ARCnet broadcast address */
176 error = nd6_resolve(ifp, is_gw, m, dst, &adst, NULL,
179 return (error == EWOULDBLOCK ? 0 : error);
181 atype = ARCTYPE_INET6;
186 const struct arc_header *ah;
189 ah = (const struct arc_header *)dst->sa_data;
190 adst = ah->arc_dhost;
191 atype = ah->arc_type;
193 if (atype == ARCTYPE_ARP) {
194 atype = (ifp->if_flags & IFF_LINK0) ?
195 ARCTYPE_ARP_OLD: ARCTYPE_ARP;
197 #ifdef ARCNET_ALLOW_BROKEN_ARP
199 * XXX It's not clear per RFC826 if this is needed, but
200 * "assigned numbers" say this is wrong.
201 * However, e.g., AmiTCP 3.0Beta used it... we make this
202 * switchable for emergency cases. Not perfect, but...
204 if (ifp->if_flags & IFF_LINK2)
205 mtod(m, struct arphdr *)->ar_pro = atype - 1;
211 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
212 senderr(EAFNOSUPPORT);
215 isphds = arc_isphds(atype);
216 M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_NOWAIT);
219 ah = mtod(m, struct arc_header *);
220 ah->arc_type = atype;
221 ah->arc_dhost = adst;
222 ah->arc_shost = ARC_LLADDR(ifp);
228 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
229 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
230 struct mbuf *n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
232 (void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
233 } else if (ah->arc_dhost == ah->arc_shost) {
234 (void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
235 return (0); /* XXX */
241 error = ifp->if_transmit(ifp, m);
252 arc_frag_init(struct ifnet *ifp)
256 ac = (struct arccom *)ifp->if_l2com;
261 arc_frag_next(struct ifnet *ifp)
265 struct arc_header *ah;
267 ac = (struct arccom *)ifp->if_l2com;
268 if ((m = ac->curr_frag) == NULL) {
271 /* dequeue new packet */
272 IF_DEQUEUE(&ifp->if_snd, m);
276 ah = mtod(m, struct arc_header *);
277 if (!arc_isphds(ah->arc_type))
280 ++ac->ac_seqid; /* make the seqid unique */
281 tfrags = howmany(m->m_pkthdr.len, ARC_MAX_DATA);
282 ac->fsflag = 2 * tfrags - 3;
284 ac->rsflag = ac->fsflag;
285 ac->arc_dhost = ah->arc_dhost;
286 ac->arc_shost = ah->arc_shost;
287 ac->arc_type = ah->arc_type;
289 m_adj(m, ARC_HDRNEWLEN);
293 /* split out next fragment and return it */
294 if (ac->sflag < ac->fsflag) {
295 /* we CAN'T have short packets here */
296 ac->curr_frag = m_split(m, ARC_MAX_DATA, M_NOWAIT);
297 if (ac->curr_frag == 0) {
302 M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT);
304 m_freem(ac->curr_frag);
309 ah = mtod(m, struct arc_header *);
310 ah->arc_flag = ac->rsflag;
311 ah->arc_seqid = ac->ac_seqid;
314 ac->rsflag = ac->sflag;
315 } else if ((m->m_pkthdr.len >=
316 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
318 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
321 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_NOWAIT);
325 ah = mtod(m, struct arc_header *);
327 ah->arc_seqid = 0xFFFF;
328 ah->arc_type2 = ac->arc_type;
329 ah->arc_flag2 = ac->sflag;
330 ah->arc_seqid2 = ac->ac_seqid;
334 M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT);
338 ah = mtod(m, struct arc_header *);
339 ah->arc_flag = ac->sflag;
340 ah->arc_seqid = ac->ac_seqid;
343 ah->arc_dhost = ac->arc_dhost;
344 ah->arc_shost = ac->arc_shost;
345 ah->arc_type = ac->arc_type;
351 * Defragmenter. Returns mbuf if last packet found, else
352 * NULL. frees incoming mbuf as necessary.
355 static __inline struct mbuf *
356 arc_defrag(struct ifnet *ifp, struct mbuf *m)
358 struct arc_header *ah, *ah1;
366 ac = (struct arccom *)ifp->if_l2com;
368 if (m->m_len < ARC_HDRNEWLEN) {
369 m = m_pullup(m, ARC_HDRNEWLEN);
371 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
376 ah = mtod(m, struct arc_header *);
379 if (!arc_isphds(typ))
385 if (ah->arc_flag == 0xff) {
388 if (m->m_len < ARC_HDRNEWLEN) {
389 m = m_pullup(m, ARC_HDRNEWLEN);
391 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
396 ah = mtod(m, struct arc_header *);
399 af = &ac->ac_fragtab[src];
401 s = "debug code error";
403 if (ah->arc_flag & 1) {
405 * first fragment. We always initialize, which is
406 * about the right thing to do, as we only want to
407 * accept one fragmented packet per src at a time.
414 af->af_maxflag = ah->arc_flag;
416 af->af_seqid = ah->arc_seqid;
421 /* check for unfragmented packet */
422 if (ah->arc_flag == 0)
425 /* do we have a first packet from that src? */
431 ah1 = mtod(m1, struct arc_header *);
433 if (ah->arc_seqid != ah1->arc_seqid) {
438 if (typ != ah1->arc_type) {
443 if (dst != ah1->arc_dhost) {
444 s = "dest host differs";
448 /* typ, seqid and dst are ok here. */
450 if (ah->arc_flag == af->af_lastseen) {
455 if (ah->arc_flag == af->af_lastseen + 2) {
456 /* ok, this is next fragment */
457 af->af_lastseen = ah->arc_flag;
458 m_adj(m,ARC_HDRNEWLEN);
461 * m_cat might free the first mbuf (with pkthdr)
462 * in 2nd chain; therefore:
465 newflen = m->m_pkthdr.len;
469 m1->m_pkthdr.len += newflen;
471 /* is it the last one? */
472 if (af->af_lastseen > af->af_maxflag) {
473 af->af_packet = NULL;
479 /* if all else fails, it is out of sequence, too */
484 af->af_packet = NULL;
490 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
497 * return 1 if Packet Header Definition Standard, else 0.
498 * For now: old IP, old ARP aren't obviously. Lacking correct information,
499 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
500 * (Apple and Novell corporations were involved, among others, in PHDS work).
501 * Easiest is to assume that everybody else uses that, too.
504 arc_isphds(u_int8_t type)
506 return (type != ARCTYPE_IP_OLD &&
507 type != ARCTYPE_ARP_OLD &&
508 type != ARCTYPE_DIAGNOSE);
512 * Process a received Arcnet packet;
513 * the packet is in the mbuf chain m with
517 arc_input(struct ifnet *ifp, struct mbuf *m)
519 struct arc_header *ah;
523 if ((ifp->if_flags & IFF_UP) == 0) {
528 /* possibly defragment: */
529 m = arc_defrag(ifp, m);
535 ah = mtod(m, struct arc_header *);
536 /* does this belong to us? */
537 if ((ifp->if_flags & IFF_PROMISC) == 0
538 && ah->arc_dhost != arcbroadcastaddr
539 && ah->arc_dhost != ARC_LLADDR(ifp)) {
544 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
546 if (ah->arc_dhost == arcbroadcastaddr) {
547 m->m_flags |= M_BCAST|M_MCAST;
548 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
551 atype = ah->arc_type;
555 m_adj(m, ARC_HDRNEWLEN);
560 m_adj(m, ARC_HDRLEN);
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);
600 M_SETFIB(m, ifp->if_fib);
601 netisr_dispatch(isr, m);
605 * Register (new) link level address.
608 arc_storelladdr(struct ifnet *ifp, u_int8_t lla)
610 ARC_LLADDR(ifp) = lla;
614 * Perform common duties while attaching to interface list
617 arc_ifattach(struct ifnet *ifp, u_int8_t lla)
620 struct sockaddr_dl *sdl;
625 ifp->if_hdrlen = ARC_HDRLEN;
627 ifp->if_resolvemulti = arc_resolvemulti;
628 if (ifp->if_baudrate == 0)
629 ifp->if_baudrate = 2500000;
631 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
632 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
633 sdl->sdl_type = IFT_ARCNET;
634 sdl->sdl_alen = ifp->if_addrlen;
636 if (ifp->if_flags & IFF_BROADCAST)
637 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
639 ac = (struct arccom *)ifp->if_l2com;
640 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
642 /* XXX this message isn't entirely clear, to me -- cgd */
643 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n",
644 ifp->if_xname, ifp->if_xname);
646 arc_storelladdr(ifp, lla);
648 ifp->if_broadcastaddr = &arcbroadcastaddr;
650 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
654 arc_ifdetach(struct ifnet *ifp)
661 arc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
663 struct ifaddr *ifa = (struct ifaddr *) data;
664 struct ifreq *ifr = (struct ifreq *) data;
669 ifp->if_flags |= IFF_UP;
670 switch (ifa->ifa_addr->sa_family) {
673 ifp->if_init(ifp->if_softc); /* before arpwhohas */
674 arp_ifinit(ifp, ifa);
678 ifp->if_init(ifp->if_softc);
687 sa = (struct sockaddr *) &ifr->ifr_data;
688 *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
695 error = EAFNOSUPPORT;
697 switch (ifr->ifr_addr.sa_family) {
703 error = EAFNOSUPPORT;
711 * Set the interface MTU.
712 * mtu can't be larger than ARCMTU for RFC1051
713 * and can't be larger than ARC_PHDS_MTU
715 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
716 ifr->ifr_mtu > ARC_PHDS_MAXMTU)
719 ifp->if_mtu = ifr->ifr_mtu;
726 /* based on ether_resolvemulti() */
728 arc_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
731 struct sockaddr_dl *sdl;
733 struct sockaddr_in *sin;
736 struct sockaddr_in6 *sin6;
739 switch(sa->sa_family) {
742 * No mapping needed. Just check that it's a valid MC address.
744 sdl = (struct sockaddr_dl *)sa;
745 if (*LLADDR(sdl) != arcbroadcastaddr)
746 return EADDRNOTAVAIL;
751 sin = (struct sockaddr_in *)sa;
752 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
753 return EADDRNOTAVAIL;
754 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
755 sdl->sdl_alen = ARC_ADDR_LEN;
757 *llsa = (struct sockaddr *)sdl;
762 sin6 = (struct sockaddr_in6 *)sa;
763 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
765 * An IP6 address of 0 means listen to all
766 * of the Ethernet multicast address used for IP6.
767 * (This is used for multicast routers.)
769 ifp->if_flags |= IFF_ALLMULTI;
773 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
774 return EADDRNOTAVAIL;
775 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
776 sdl->sdl_alen = ARC_ADDR_LEN;
778 *llsa = (struct sockaddr *)sdl;
784 * Well, the text isn't quite right, but it's the name
791 static MALLOC_DEFINE(M_ARCCOM, "arccom", "ARCNET interface internals");
794 arc_alloc(u_char type, struct ifnet *ifp)
798 ac = malloc(sizeof(struct arccom), M_ARCCOM, M_WAITOK | M_ZERO);
805 arc_free(void *com, u_char type)
812 arc_modevent(module_t mod, int type, void *data)
817 if_register_com_alloc(IFT_ARCNET, arc_alloc, arc_free);
820 if_deregister_com_alloc(IFT_ARCNET);
829 static moduledata_t arc_mod = {
835 DECLARE_MODULE(arcnet, arc_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
836 MODULE_VERSION(arcnet, 1);