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/if_var.h>
61 #include <net/netisr.h>
62 #include <net/route.h>
63 #include <net/if_dl.h>
64 #include <net/if_types.h>
65 #include <net/if_arc.h>
66 #include <net/if_arp.h>
68 #include <net/if_llatbl.h>
70 #if defined(INET) || defined(INET6)
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
73 #include <netinet/if_ether.h>
77 #include <netinet6/nd6.h>
81 #include <netipx/ipx.h>
82 #include <netipx/ipx_if.h>
85 #define ARCNET_ALLOW_BROKEN_ARP
87 static struct mbuf *arc_defrag(struct ifnet *, struct mbuf *);
88 static int arc_resolvemulti(struct ifnet *, struct sockaddr **,
91 u_int8_t arcbroadcastaddr = 0;
93 #define ARC_LLADDR(ifp) (*(u_int8_t *)IF_LLADDR(ifp))
95 #define senderr(e) { error = (e); goto bad;}
96 #define SIN(s) ((const struct sockaddr_in *)(s))
97 #define SIPX(s) ((const struct sockaddr_ipx *)(s))
100 * ARCnet output routine.
101 * Encapsulate a packet of type family for the local net.
102 * Assumes that ifp is actually pointer to arccom structure.
105 arc_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
108 struct arc_header *ah;
110 u_int8_t atype, adst;
113 #if defined(INET) || defined(INET6)
117 if (!((ifp->if_flags & IFF_UP) &&
118 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
119 return(ENETDOWN); /* m, m1 aren't initialized yet */
123 switch (dst->sa_family) {
128 * For now, use the simple IP addr -> ARCnet addr mapping
130 if (m->m_flags & (M_BCAST|M_MCAST))
131 adst = arcbroadcastaddr; /* ARCnet broadcast address */
132 else if (ifp->if_flags & IFF_NOARP)
133 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
135 error = arpresolve(ifp, ro ? ro->ro_rt : NULL,
136 m, dst, &adst, &lle);
138 return (error == EWOULDBLOCK ? 0 : error);
141 atype = (ifp->if_flags & IFF_LINK0) ?
142 ARCTYPE_IP_OLD : ARCTYPE_IP;
147 ah = mtod(m, struct arphdr *);
148 ah->ar_hrd = htons(ARPHRD_ARCNET);
150 loop_copy = -1; /* if this is for us, don't do it */
152 switch(ntohs(ah->ar_op)) {
153 case ARPOP_REVREQUEST:
155 atype = ARCTYPE_REVARP;
164 if (m->m_flags & M_BCAST)
165 bcopy(ifp->if_broadcastaddr, &adst, ARC_ADDR_LEN);
167 bcopy(ar_tha(ah), &adst, ARC_ADDR_LEN);
174 error = nd6_storelladdr(ifp, m, dst, (u_char *)&adst, &lle);
177 atype = ARCTYPE_INET6;
182 adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
185 adst = arcbroadcastaddr;
191 const struct arc_header *ah;
194 ah = (const struct arc_header *)dst->sa_data;
195 adst = ah->arc_dhost;
196 atype = ah->arc_type;
198 if (atype == ARCTYPE_ARP) {
199 atype = (ifp->if_flags & IFF_LINK0) ?
200 ARCTYPE_ARP_OLD: ARCTYPE_ARP;
202 #ifdef ARCNET_ALLOW_BROKEN_ARP
204 * XXX It's not clear per RFC826 if this is needed, but
205 * "assigned numbers" say this is wrong.
206 * However, e.g., AmiTCP 3.0Beta used it... we make this
207 * switchable for emergency cases. Not perfect, but...
209 if (ifp->if_flags & IFF_LINK2)
210 mtod(m, struct arphdr *)->ar_pro = atype - 1;
216 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
217 senderr(EAFNOSUPPORT);
220 isphds = arc_isphds(atype);
221 M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_NOWAIT);
224 ah = mtod(m, struct arc_header *);
225 ah->arc_type = atype;
226 ah->arc_dhost = adst;
227 ah->arc_shost = ARC_LLADDR(ifp);
233 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
234 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
235 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
237 (void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
238 } else if (ah->arc_dhost == ah->arc_shost) {
239 (void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
240 return (0); /* XXX */
246 error = ifp->if_transmit(ifp, m);
257 arc_frag_init(struct ifnet *ifp)
261 ac = (struct arccom *)ifp->if_l2com;
266 arc_frag_next(struct ifnet *ifp)
270 struct arc_header *ah;
272 ac = (struct arccom *)ifp->if_l2com;
273 if ((m = ac->curr_frag) == 0) {
276 /* dequeue new packet */
277 IF_DEQUEUE(&ifp->if_snd, m);
281 ah = mtod(m, struct arc_header *);
282 if (!arc_isphds(ah->arc_type))
285 ++ac->ac_seqid; /* make the seqid unique */
286 tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
287 ac->fsflag = 2 * tfrags - 3;
289 ac->rsflag = ac->fsflag;
290 ac->arc_dhost = ah->arc_dhost;
291 ac->arc_shost = ah->arc_shost;
292 ac->arc_type = ah->arc_type;
294 m_adj(m, ARC_HDRNEWLEN);
298 /* split out next fragment and return it */
299 if (ac->sflag < ac->fsflag) {
300 /* we CAN'T have short packets here */
301 ac->curr_frag = m_split(m, ARC_MAX_DATA, M_NOWAIT);
302 if (ac->curr_frag == 0) {
307 M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT);
309 m_freem(ac->curr_frag);
314 ah = mtod(m, struct arc_header *);
315 ah->arc_flag = ac->rsflag;
316 ah->arc_seqid = ac->ac_seqid;
319 ac->rsflag = ac->sflag;
320 } else if ((m->m_pkthdr.len >=
321 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
323 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
326 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_NOWAIT);
330 ah = mtod(m, struct arc_header *);
332 ah->arc_seqid = 0xFFFF;
333 ah->arc_type2 = ac->arc_type;
334 ah->arc_flag2 = ac->sflag;
335 ah->arc_seqid2 = ac->ac_seqid;
339 M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT);
343 ah = mtod(m, struct arc_header *);
344 ah->arc_flag = ac->sflag;
345 ah->arc_seqid = ac->ac_seqid;
348 ah->arc_dhost = ac->arc_dhost;
349 ah->arc_shost = ac->arc_shost;
350 ah->arc_type = ac->arc_type;
356 * Defragmenter. Returns mbuf if last packet found, else
357 * NULL. frees imcoming mbuf as necessary.
360 static __inline struct mbuf *
361 arc_defrag(struct ifnet *ifp, struct mbuf *m)
363 struct arc_header *ah, *ah1;
371 ac = (struct arccom *)ifp->if_l2com;
373 if (m->m_len < ARC_HDRNEWLEN) {
374 m = m_pullup(m, ARC_HDRNEWLEN);
381 ah = mtod(m, struct arc_header *);
384 if (!arc_isphds(typ))
390 if (ah->arc_flag == 0xff) {
393 if (m->m_len < ARC_HDRNEWLEN) {
394 m = m_pullup(m, ARC_HDRNEWLEN);
401 ah = mtod(m, struct arc_header *);
404 af = &ac->ac_fragtab[src];
406 s = "debug code error";
408 if (ah->arc_flag & 1) {
410 * first fragment. We always initialize, which is
411 * about the right thing to do, as we only want to
412 * accept one fragmented packet per src at a time.
419 af->af_maxflag = ah->arc_flag;
421 af->af_seqid = ah->arc_seqid;
426 /* check for unfragmented packet */
427 if (ah->arc_flag == 0)
430 /* do we have a first packet from that src? */
436 ah1 = mtod(m1, struct arc_header *);
438 if (ah->arc_seqid != ah1->arc_seqid) {
443 if (typ != ah1->arc_type) {
448 if (dst != ah1->arc_dhost) {
449 s = "dest host differs";
453 /* typ, seqid and dst are ok here. */
455 if (ah->arc_flag == af->af_lastseen) {
460 if (ah->arc_flag == af->af_lastseen + 2) {
461 /* ok, this is next fragment */
462 af->af_lastseen = ah->arc_flag;
463 m_adj(m,ARC_HDRNEWLEN);
466 * m_cat might free the first mbuf (with pkthdr)
467 * in 2nd chain; therefore:
470 newflen = m->m_pkthdr.len;
474 m1->m_pkthdr.len += newflen;
476 /* is it the last one? */
477 if (af->af_lastseen > af->af_maxflag) {
478 af->af_packet = NULL;
484 /* if all else fails, it is out of sequence, too */
489 af->af_packet = NULL;
495 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
502 * return 1 if Packet Header Definition Standard, else 0.
503 * For now: old IP, old ARP aren't obviously. Lacking correct information,
504 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
505 * (Apple and Novell corporations were involved, among others, in PHDS work).
506 * Easiest is to assume that everybody else uses that, too.
509 arc_isphds(u_int8_t type)
511 return (type != ARCTYPE_IP_OLD &&
512 type != ARCTYPE_ARP_OLD &&
513 type != ARCTYPE_DIAGNOSE);
517 * Process a received Arcnet packet;
518 * the packet is in the mbuf chain m with
522 arc_input(struct ifnet *ifp, struct mbuf *m)
524 struct arc_header *ah;
528 if ((ifp->if_flags & IFF_UP) == 0) {
533 /* possibly defragment: */
534 m = arc_defrag(ifp, m);
540 ah = mtod(m, struct arc_header *);
541 /* does this belong to us? */
542 if ((ifp->if_flags & IFF_PROMISC) == 0
543 && ah->arc_dhost != arcbroadcastaddr
544 && ah->arc_dhost != ARC_LLADDR(ifp)) {
549 ifp->if_ibytes += m->m_pkthdr.len;
551 if (ah->arc_dhost == arcbroadcastaddr) {
552 m->m_flags |= M_BCAST|M_MCAST;
556 atype = ah->arc_type;
560 m_adj(m, ARC_HDRNEWLEN);
561 if ((m = ip_fastforward(m)) == NULL)
567 m_adj(m, ARC_HDRLEN);
568 if ((m = ip_fastforward(m)) == NULL)
574 if (ifp->if_flags & IFF_NOARP) {
575 /* Discard packet if ARP is disabled on interface */
579 m_adj(m, ARC_HDRNEWLEN);
581 #ifdef ARCNET_ALLOW_BROKEN_ARP
582 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
586 case ARCTYPE_ARP_OLD:
587 if (ifp->if_flags & IFF_NOARP) {
588 /* Discard packet if ARP is disabled on interface */
592 m_adj(m, ARC_HDRLEN);
594 #ifdef ARCNET_ALLOW_BROKEN_ARP
595 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
601 m_adj(m, ARC_HDRNEWLEN);
607 m_adj(m, ARC_HDRNEWLEN);
615 M_SETFIB(m, ifp->if_fib);
616 netisr_dispatch(isr, m);
620 * Register (new) link level address.
623 arc_storelladdr(struct ifnet *ifp, u_int8_t lla)
625 ARC_LLADDR(ifp) = lla;
629 * Perform common duties while attaching to interface list
632 arc_ifattach(struct ifnet *ifp, u_int8_t lla)
635 struct sockaddr_dl *sdl;
640 ifp->if_hdrlen = ARC_HDRLEN;
642 ifp->if_resolvemulti = arc_resolvemulti;
643 if (ifp->if_baudrate == 0)
644 ifp->if_baudrate = 2500000;
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 = link_init_sdl(ifp, *llsa, IFT_ETHER);
790 sdl->sdl_alen = ARC_ADDR_LEN;
792 *llsa = (struct sockaddr *)sdl;
797 sin6 = (struct sockaddr_in6 *)sa;
798 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
800 * An IP6 address of 0 means listen to all
801 * of the Ethernet multicast address used for IP6.
802 * (This is used for multicast routers.)
804 ifp->if_flags |= IFF_ALLMULTI;
808 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
809 return EADDRNOTAVAIL;
810 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
811 sdl->sdl_alen = ARC_ADDR_LEN;
813 *llsa = (struct sockaddr *)sdl;
819 * Well, the text isn't quite right, but it's the name
826 static MALLOC_DEFINE(M_ARCCOM, "arccom", "ARCNET interface internals");
829 arc_alloc(u_char type, struct ifnet *ifp)
833 ac = malloc(sizeof(struct arccom), M_ARCCOM, M_WAITOK | M_ZERO);
840 arc_free(void *com, u_char type)
847 arc_modevent(module_t mod, int type, void *data)
852 if_register_com_alloc(IFT_ARCNET, arc_alloc, arc_free);
855 if_deregister_com_alloc(IFT_ARCNET);
864 static moduledata_t arc_mod = {
870 DECLARE_MODULE(arcnet, arc_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
871 MODULE_VERSION(arcnet, 1);