1 /* $NetBSD: if_arcsubr.c,v 1.36 2001/06/14 05:44:23 itojun Exp $ */
2 /* $FreeBSD: src/sys/net/if_arcsubr.c,v 1.1.2.5 2003/02/05 18:42:15 fjoe Exp $ */
3 /* $DragonFly: src/sys/net/Attic/if_arcsubr.c,v 1.17 2005/06/05 16:05:33 joerg Exp $ */
6 * Copyright (c) 1994, 1995 Ignatios Souvatzis
7 * Copyright (c) 1982, 1989, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
39 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
43 #include "opt_inet6.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.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>
66 #include <net/ifq_var.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 <netproto/ipx/ipx.h>
81 #include <netproto/ipx/ipx_if.h>
84 MODULE_VERSION(arcnet, 1);
86 #define ARCNET_ALLOW_BROKEN_ARP
88 static struct mbuf *arc_defrag (struct ifnet *, struct mbuf *);
89 static int arc_resolvemulti (struct ifnet *, struct sockaddr **,
91 static void arc_input(struct ifnet *, struct mbuf *);
92 static int arc_output(struct ifnet *, struct mbuf *, struct sockaddr *,
95 #define ARC_LLADDR(ifp) (*(u_int8_t *)IF_LLADDR(ifp))
97 #define gotoerr(e) { error = (e); goto bad;}
98 #define SIN(s) ((struct sockaddr_in *)s)
99 #define SIPX(s) ((struct sockaddr_ipx *)s)
101 const uint8_t arcbroadcastaddr[1] = {0};
104 * ARCnet output routine.
105 * Encapsulate a packet of type family for the local net.
106 * Assumes that ifp is actually pointer to arccom structure.
109 arc_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
112 struct arc_header *ah;
114 u_int8_t atype, adst;
117 struct altq_pktattr pktattr;
119 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
120 return (ENETDOWN); /* m, m1 aren't initialized yet */
123 * If the queueing discipline needs packet classification,
124 * do it before prepending link headers.
126 ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
128 switch (dst->sa_family) {
133 * For now, use the simple IP addr -> ARCnet addr mapping
135 if (m->m_flags & (M_BCAST|M_MCAST))
136 adst = ifp->if_broadcastaddr[0];
137 else if (ifp->if_flags & IFF_NOARP)
138 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
139 else if (!arpresolve(ifp, rt, m, dst, &adst))
140 return 0; /* not resolved yet */
142 atype = (ifp->if_flags & IFF_LINK0) ?
143 ARCTYPE_IP_OLD : ARCTYPE_IP;
148 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)&adst))
149 return (0); /* it must be impossible, but... */
150 atype = ARCTYPE_INET6;
155 adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
158 adst = ifp->if_broadcastaddr[0];
164 ah = (struct arc_header *)dst->sa_data;
165 adst = ah->arc_dhost;
166 atype = ah->arc_type;
168 if (atype == ARCTYPE_ARP) {
169 atype = (ifp->if_flags & IFF_LINK0) ?
170 ARCTYPE_ARP_OLD: ARCTYPE_ARP;
172 #ifdef ARCNET_ALLOW_BROKEN_ARP
174 * XXX It's not clear per RFC826 if this is needed, but
175 * "assigned numbers" say this is wrong.
176 * However, e.g., AmiTCP 3.0Beta used it... we make this
177 * switchable for emergency cases. Not perfect, but...
179 if (ifp->if_flags & IFF_LINK2)
180 mtod(m, struct arphdr *)->ar_pro = atype - 1;
186 printf("%s: can't handle af%d\n", ifp->if_xname,
188 gotoerr(EAFNOSUPPORT);
191 isphds = arc_isphds(atype);
192 M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, MB_DONTWAIT);
195 ah = mtod(m, struct arc_header *);
196 ah->arc_type = atype;
197 ah->arc_dhost = adst;
198 ah->arc_shost = *IF_LLADDR(ifp);
199 ah->arc_shost = ARC_LLADDR(ifp);
205 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
206 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
207 struct mbuf *n = m_copypacket(m, MB_DONTWAIT);
209 if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
210 } else if (ah->arc_dhost == ah->arc_shost) {
211 if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
212 return (0); /* XXX */
218 error = ifq_handoff(ifp, m, &pktattr);
233 ac = (struct arccom *)ifp;
243 struct arc_header *ah;
245 ac = (struct arccom *)ifp;
246 if ((m = ac->curr_frag) == 0) {
249 /* dequeue new packet */
250 IF_DEQUEUE(&ifp->if_snd, m);
254 ah = mtod(m, struct arc_header *);
255 if (!arc_isphds(ah->arc_type))
258 ++ac->ac_seqid; /* make the seqid unique */
259 tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
260 ac->fsflag = 2 * tfrags - 3;
262 ac->rsflag = ac->fsflag;
263 ac->arc_dhost = ah->arc_dhost;
264 ac->arc_shost = ah->arc_shost;
265 ac->arc_type = ah->arc_type;
267 m_adj(m, ARC_HDRNEWLEN);
271 /* split out next fragment and return it */
272 if (ac->sflag < ac->fsflag) {
273 /* we CAN'T have short packets here */
274 ac->curr_frag = m_split(m, ARC_MAX_DATA, MB_DONTWAIT);
275 if (ac->curr_frag == 0) {
280 M_PREPEND(m, ARC_HDRNEWLEN, MB_DONTWAIT);
282 m_freem(ac->curr_frag);
287 ah = mtod(m, struct arc_header *);
288 ah->arc_flag = ac->rsflag;
289 ah->arc_seqid = ac->ac_seqid;
292 ac->rsflag = ac->sflag;
293 } else if ((m->m_pkthdr.len >=
294 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
296 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
299 M_PREPEND(m, ARC_HDRNEWLEN_EXC, MB_DONTWAIT);
303 ah = mtod(m, struct arc_header *);
305 ah->arc_seqid = 0xFFFF;
306 ah->arc_type2 = ac->arc_type;
307 ah->arc_flag2 = ac->sflag;
308 ah->arc_seqid2 = ac->ac_seqid;
312 M_PREPEND(m, ARC_HDRNEWLEN, MB_DONTWAIT);
316 ah = mtod(m, struct arc_header *);
317 ah->arc_flag = ac->sflag;
318 ah->arc_seqid = ac->ac_seqid;
321 ah->arc_dhost = ac->arc_dhost;
322 ah->arc_shost = ac->arc_shost;
323 ah->arc_type = ac->arc_type;
329 * Defragmenter. Returns mbuf if last packet found, else
330 * NULL. frees imcoming mbuf as necessary.
333 __inline struct mbuf *
338 struct arc_header *ah, *ah1;
346 ac = (struct arccom *)ifp;
348 if (m->m_len < ARC_HDRNEWLEN) {
349 m = m_pullup(m, ARC_HDRNEWLEN);
356 ah = mtod(m, struct arc_header *);
359 if (!arc_isphds(typ))
365 if (ah->arc_flag == 0xff) {
368 if (m->m_len < ARC_HDRNEWLEN) {
369 m = m_pullup(m, ARC_HDRNEWLEN);
376 ah = mtod(m, struct arc_header *);
379 af = &ac->ac_fragtab[src];
381 s = "debug code error";
383 if (ah->arc_flag & 1) {
385 * first fragment. We always initialize, which is
386 * about the right thing to do, as we only want to
387 * accept one fragmented packet per src at a time.
394 af->af_maxflag = ah->arc_flag;
396 af->af_seqid = ah->arc_seqid;
401 /* check for unfragmented packet */
402 if (ah->arc_flag == 0)
405 /* do we have a first packet from that src? */
411 ah1 = mtod(m1, struct arc_header *);
413 if (ah->arc_seqid != ah1->arc_seqid) {
418 if (typ != ah1->arc_type) {
423 if (dst != ah1->arc_dhost) {
424 s = "dest host differs";
428 /* typ, seqid and dst are ok here. */
430 if (ah->arc_flag == af->af_lastseen) {
435 if (ah->arc_flag == af->af_lastseen + 2) {
436 /* ok, this is next fragment */
437 af->af_lastseen = ah->arc_flag;
438 m_adj(m,ARC_HDRNEWLEN);
441 * m_cat might free the first mbuf (with pkthdr)
442 * in 2nd chain; therefore:
445 newflen = m->m_pkthdr.len;
449 m1->m_pkthdr.len += newflen;
451 /* is it the last one? */
452 if (af->af_lastseen > af->af_maxflag) {
453 af->af_packet = NULL;
459 /* if all else fails, it is out of sequence, too */
464 af->af_packet = NULL;
470 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
477 * return 1 if Packet Header Definition Standard, else 0.
478 * For now: old IP, old ARP aren't obviously. Lacking correct information,
479 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
480 * (Apple and Novell corporations were involved, among others, in PHDS work).
481 * Easiest is to assume that everybody else uses that, too.
487 return (type != ARCTYPE_IP_OLD &&
488 type != ARCTYPE_ARP_OLD &&
489 type != ARCTYPE_DIAGNOSE);
493 * Process a received Arcnet packet;
494 * the packet is in the mbuf chain m with
498 arc_input(struct ifnet *ifp, struct mbuf *m)
500 struct arc_header *ah;
504 if (!(ifp->if_flags & IFF_UP)) {
509 /* possibly defragment: */
510 m = arc_defrag(ifp, m);
516 ah = mtod(m, struct arc_header *);
517 /* does this belong to us? */
518 if (!(ifp->if_flags & IFF_PROMISC) &&
519 ah->arc_dhost != ifp->if_broadcastaddr[0] &&
520 ah->arc_dhost != ARC_LLADDR(ifp)) {
525 ifp->if_ibytes += m->m_pkthdr.len;
527 if (ah->arc_dhost == ifp->if_broadcastaddr[0]) {
528 m->m_flags |= M_BCAST|M_MCAST;
532 atype = ah->arc_type;
536 m_adj(m, ARC_HDRNEWLEN);
537 if (ipflow_fastforward(m))
543 m_adj(m, ARC_HDRLEN);
544 if (ipflow_fastforward(m))
550 if (ifp->if_flags & IFF_NOARP) {
551 /* Discard packet if ARP is disabled on interface */
555 m_adj(m, ARC_HDRNEWLEN);
557 #ifdef ARCNET_ALLOW_BROKEN_ARP
558 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
562 case ARCTYPE_ARP_OLD:
563 if (ifp->if_flags & IFF_NOARP) {
564 /* Discard packet if ARP is disabled on interface */
568 m_adj(m, ARC_HDRLEN);
570 #ifdef ARCNET_ALLOW_BROKEN_ARP
571 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
577 m_adj(m, ARC_HDRNEWLEN);
583 m_adj(m, ARC_HDRNEWLEN);
592 netisr_dispatch(isr, m);
596 * Register (new) link level address.
599 arc_storelladdr(ifp, lla)
603 ARC_LLADDR(ifp) = lla;
607 * Perform common duties while attaching to interface list
610 arc_ifattach(ifp, lla)
614 struct sockaddr_dl *sdl;
617 ifp->if_input = arc_input;
618 ifp->if_output = arc_output;
620 ifp->if_type = IFT_ARCNET;
622 ifp->if_broadcastaddr = arcbroadcastaddr;
623 ifp->if_hdrlen = ARC_HDRLEN;
625 ifp->if_resolvemulti = arc_resolvemulti;
626 if (ifp->if_baudrate == 0)
627 ifp->if_baudrate = 2500000;
628 sdl = IF_LLSOCKADDR(ifp);
629 sdl->sdl_type = IFT_ARCNET;
630 sdl->sdl_alen = ifp->if_addrlen;
632 if (ifp->if_flags & IFF_BROADCAST)
633 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
635 ac = (struct arccom *)ifp;
636 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
638 /* XXX this message isn't entirely clear, to me -- cgd */
639 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n",
640 ifp->if_xname, ifp->if_xname);
642 arc_storelladdr(ifp, lla);
644 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
656 arc_ioctl(ifp, command, data)
661 struct ifaddr *ifa = (struct ifaddr *) data;
662 struct ifreq *ifr = (struct ifreq *) data;
667 ifp->if_flags |= IFF_UP;
668 switch (ifa->ifa_addr->sa_family) {
671 ifp->if_init(ifp->if_softc); /* before arpwhohas */
672 arp_ifinit(ifp, ifa);
677 * XXX This code is probably wrong
681 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
683 if (ipx_nullhost(*ina))
684 ina->x_host.c_host[5] = ARC_LLADDR(ifp);
686 arc_storelladdr(ifp, ina->x_host.c_host[5]);
691 ifp->if_init(ifp->if_softc);
696 ifp->if_init(ifp->if_softc);
705 sa = (struct sockaddr *) &ifr->ifr_data;
706 *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
713 error = EAFNOSUPPORT;
715 switch (ifr->ifr_addr.sa_family) {
721 error = EAFNOSUPPORT;
729 * Set the interface MTU.
730 * mtu can't be larger than ARCMTU for RFC1051
731 * and can't be larger than ARC_PHDS_MTU
733 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
734 ifr->ifr_mtu > ARC_PHDS_MAXMTU)
737 ifp->if_mtu = ifr->ifr_mtu;
747 /* based on ether_resolvemulti() */
749 arc_resolvemulti(ifp, llsa, sa)
751 struct sockaddr **llsa;
754 struct sockaddr_dl *sdl;
755 struct sockaddr_in *sin;
757 struct sockaddr_in6 *sin6;
760 switch(sa->sa_family) {
763 * No mapping needed. Just check that it's a valid MC address.
765 sdl = (struct sockaddr_dl *)sa;
766 if (*LLADDR(sdl) != ifp->if_broadcastaddr[0])
767 return EADDRNOTAVAIL;
772 sin = (struct sockaddr_in *)sa;
773 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
774 return EADDRNOTAVAIL;
775 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
777 sdl->sdl_len = sizeof *sdl;
778 sdl->sdl_family = AF_LINK;
779 sdl->sdl_index = ifp->if_index;
780 sdl->sdl_type = IFT_ARCNET;
781 sdl->sdl_alen = ARC_ADDR_LEN;
783 *llsa = (struct sockaddr *)sdl;
788 sin6 = (struct sockaddr_in6 *)sa;
789 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
791 * An IP6 address of 0 means listen to all
792 * of the Ethernet multicast address used for IP6.
793 * (This is used for multicast routers.)
795 ifp->if_flags |= IFF_ALLMULTI;
799 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
800 return EADDRNOTAVAIL;
801 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
803 sdl->sdl_len = sizeof *sdl;
804 sdl->sdl_family = AF_LINK;
805 sdl->sdl_index = ifp->if_index;
806 sdl->sdl_type = IFT_ARCNET;
807 sdl->sdl_alen = ARC_ADDR_LEN;
809 *llsa = (struct sockaddr *)sdl;
815 * Well, the text isn't quite right, but it's the name