Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / net / if_arcsubr.c
blobacba3df1319383976d60100e57217267f7905f56
1 /* $NetBSD: if_arcsubr.c,v 1.60 2008/11/07 00:20:13 dyoung Exp $ */
3 /*
4 * Copyright (c) 1994, 1995 Ignatios Souvatzis
5 * Copyright (c) 1982, 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: if_arcsubr.c,v 1.60 2008/11/07 00:20:13 dyoung Exp $");
40 #include "opt_inet.h"
42 #include "bpfilter.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/ioctl.h>
52 #include <sys/errno.h>
53 #include <sys/syslog.h>
55 #include <sys/cpu.h>
57 #include <net/if.h>
58 #include <net/netisr.h>
59 #include <net/route.h>
60 #include <net/if_dl.h>
61 #include <net/if_types.h>
62 #include <net/if_arc.h>
63 #include <net/if_arp.h>
64 #include <net/if_ether.h>
66 #if NBPFILTER > 0
67 #include <net/bpf.h>
68 #endif
70 #ifdef INET
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
73 #include <netinet/if_inarp.h>
74 #endif
76 #ifdef INET6
77 #ifndef INET
78 #include <netinet/in.h>
79 #endif
80 #include <netinet6/in6_var.h>
81 #include <netinet6/nd6.h>
82 #endif
84 #define ARCNET_ALLOW_BROKEN_ARP
86 #ifndef ARC_IPMTU
87 #define ARC_IPMTU 1500
88 #endif
90 static struct mbuf *arc_defrag(struct ifnet *, struct mbuf *);
93 * RC1201 requires us to have this configurable. We have it only per
94 * machine at the moment... there is no generic "set mtu" ioctl, AFAICS.
95 * Anyway, it is possible to binpatch this or set it per kernel config
96 * option.
98 #if ARC_IPMTU > 60480
99 ERROR: The arc_ipmtu is ARC_IPMTU, but must not exceed 60480.
100 #endif
101 int arc_ipmtu = ARC_IPMTU;
102 uint8_t arcbroadcastaddr = 0;
104 #define senderr(e) { error = (e); goto bad;}
106 static int arc_output(struct ifnet *, struct mbuf *,
107 const struct sockaddr *, struct rtentry *);
108 static void arc_input(struct ifnet *, struct mbuf *);
111 * ARCnet output routine.
112 * Encapsulate a packet of type family for the local net.
113 * Assumes that ifp is actually pointer to arccom structure.
115 static int
116 arc_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
117 struct rtentry *rt0)
119 struct mbuf *m, *m1, *mcopy;
120 struct rtentry *rt;
121 struct arccom *ac;
122 const struct arc_header *cah;
123 struct arc_header *ah;
124 struct arphdr *arph;
125 int error, newencoding;
126 uint8_t atype, adst, myself;
127 int tfrags, sflag, fsflag, rsflag;
128 ALTQ_DECL(struct altq_pktattr pktattr;)
130 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
131 return (ENETDOWN); /* m, m1 aren't initialized yet */
133 error = newencoding = 0;
134 ac = (struct arccom *)ifp;
135 m = m0;
136 mcopy = m1 = NULL;
138 myself = *CLLADDR(ifp->if_sadl);
140 if ((rt = rt0)) {
141 if ((rt->rt_flags & RTF_UP) == 0) {
142 if ((rt0 = rt = rtalloc1(dst, 1)))
143 rt->rt_refcnt--;
144 else
145 senderr(EHOSTUNREACH);
147 if (rt->rt_flags & RTF_GATEWAY) {
148 if (rt->rt_gwroute == 0)
149 goto lookup;
150 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
151 rtfree(rt); rt = rt0;
152 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
153 if ((rt = rt->rt_gwroute) == 0)
154 senderr(EHOSTUNREACH);
157 if (rt->rt_flags & RTF_REJECT)
158 if (rt->rt_rmx.rmx_expire == 0 ||
159 time_second < rt->rt_rmx.rmx_expire)
160 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
164 * if the queueing discipline needs packet classification,
165 * do it before prepending link headers.
167 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
169 switch (dst->sa_family) {
170 #ifdef INET
171 case AF_INET:
174 * For now, use the simple IP addr -> ARCnet addr mapping
176 if (m->m_flags & (M_BCAST|M_MCAST))
177 adst = arcbroadcastaddr; /* ARCnet broadcast address */
178 else if (ifp->if_flags & IFF_NOARP)
179 adst = ntohl(satocsin(dst)->sin_addr.s_addr) & 0xFF;
180 else if (!arpresolve(ifp, rt, m, dst, &adst))
181 return 0; /* not resolved yet */
183 /* If broadcasting on a simplex interface, loopback a copy */
184 if ((m->m_flags & (M_BCAST|M_MCAST)) &&
185 (ifp->if_flags & IFF_SIMPLEX))
186 mcopy = m_copy(m, 0, (int)M_COPYALL);
187 if (ifp->if_flags & IFF_LINK0) {
188 atype = ARCTYPE_IP;
189 newencoding = 1;
190 } else {
191 atype = ARCTYPE_IP_OLD;
192 newencoding = 0;
194 break;
196 case AF_ARP:
197 arph = mtod(m, struct arphdr *);
198 if (m->m_flags & M_BCAST)
199 adst = arcbroadcastaddr;
200 else {
201 uint8_t *tha = ar_tha(arph);
202 if (tha == NULL)
203 return 0;
204 adst = *tha;
207 arph->ar_hrd = htons(ARPHRD_ARCNET);
209 switch (ntohs(arph->ar_op)) {
210 case ARPOP_REVREQUEST:
211 case ARPOP_REVREPLY:
212 if (!(ifp->if_flags & IFF_LINK0)) {
213 printf("%s: can't handle af%d\n",
214 ifp->if_xname, dst->sa_family);
215 senderr(EAFNOSUPPORT);
218 atype = htons(ARCTYPE_REVARP);
219 newencoding = 1;
220 break;
222 case ARPOP_REQUEST:
223 case ARPOP_REPLY:
224 default:
225 if (ifp->if_flags & IFF_LINK0) {
226 atype = htons(ARCTYPE_ARP);
227 newencoding = 1;
228 } else {
229 atype = htons(ARCTYPE_ARP_OLD);
230 newencoding = 0;
233 #ifdef ARCNET_ALLOW_BROKEN_ARP
235 * XXX It's not clear per RFC826 if this is needed, but
236 * "assigned numbers" say this is wrong.
237 * However, e.g., AmiTCP 3.0Beta used it... we make this
238 * switchable for emergency cases. Not perfect, but...
240 if (ifp->if_flags & IFF_LINK2)
241 arph->ar_pro = atype - 1;
242 #endif
243 break;
244 #endif
245 #ifdef INET6
246 case AF_INET6:
247 if (!nd6_storelladdr(ifp, rt, m, dst, &adst, sizeof(adst)))
248 return (0); /* it must be impossible, but... */
249 atype = htons(ARCTYPE_INET6);
250 newencoding = 1;
251 break;
252 #endif
254 case AF_UNSPEC:
255 cah = (const struct arc_header *)dst->sa_data;
256 adst = cah->arc_dhost;
257 atype = cah->arc_type;
258 break;
260 default:
261 printf("%s: can't handle af%d\n", ifp->if_xname,
262 dst->sa_family);
263 senderr(EAFNOSUPPORT);
266 if (mcopy)
267 (void) looutput(ifp, mcopy, dst, rt);
270 * Add local net header. If no space in first mbuf,
271 * allocate another.
273 * For ARCnet, this is just symbolic. The header changes
274 * form and position on its way into the hardware and out of
275 * the wire. At this point, it contains source, destination and
276 * packet type.
278 if (newencoding) {
279 ++ac->ac_seqid; /* make the seqid unique */
281 tfrags = (m->m_pkthdr.len + 503) / 504;
282 fsflag = 2 * tfrags - 3;
283 sflag = 0;
284 rsflag = fsflag;
286 while (sflag < fsflag) {
287 /* we CAN'T have short packets here */
288 m1 = m_split(m, 504, M_DONTWAIT);
289 if (m1 == 0)
290 senderr(ENOBUFS);
292 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
293 if (m == 0)
294 senderr(ENOBUFS);
295 ah = mtod(m, struct arc_header *);
296 ah->arc_type = atype;
297 ah->arc_dhost = adst;
298 ah->arc_shost = myself;
299 ah->arc_flag = rsflag;
300 ah->arc_seqid = ac->ac_seqid;
302 if ((error = ifq_enqueue(ifp, m ALTQ_COMMA
303 ALTQ_DECL(&pktattr))) != 0)
304 return (error);
306 m = m1;
307 sflag += 2;
308 rsflag = sflag;
310 m1 = NULL;
313 /* here we can have small, especially forbidden packets */
315 if ((m->m_pkthdr.len >=
316 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
317 (m->m_pkthdr.len <=
318 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
320 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
321 if (m == 0)
322 senderr(ENOBUFS);
323 ah = mtod(m, struct arc_header *);
324 ah->arc_flag = 0xFF;
325 ah->arc_seqid = 0xFFFF;
326 ah->arc_type2 = atype;
327 ah->arc_flag2 = sflag;
328 ah->arc_seqid2 = ac->ac_seqid;
329 } else {
330 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
331 if (m == 0)
332 senderr(ENOBUFS);
333 ah = mtod(m, struct arc_header *);
334 ah->arc_flag = sflag;
335 ah->arc_seqid = ac->ac_seqid;
338 ah->arc_dhost = adst;
339 ah->arc_shost = myself;
340 ah->arc_type = atype;
341 } else {
342 M_PREPEND(m, ARC_HDRLEN, M_DONTWAIT);
343 if (m == 0)
344 senderr(ENOBUFS);
345 ah = mtod(m, struct arc_header *);
346 ah->arc_type = atype;
347 ah->arc_dhost = adst;
348 ah->arc_shost = myself;
351 return ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr));
353 bad:
354 if (m1)
355 m_freem(m1);
356 if (m)
357 m_freem(m);
358 return (error);
362 * Defragmenter. Returns mbuf if last packet found, else
363 * NULL. frees imcoming mbuf as necessary.
366 static struct mbuf *
367 arc_defrag(struct ifnet *ifp, struct mbuf *m)
369 struct arc_header *ah, *ah1;
370 struct arccom *ac;
371 struct ac_frag *af;
372 struct mbuf *m1;
373 const char *s;
374 int newflen;
375 u_char src, dst, typ;
377 ac = (struct arccom *)ifp;
379 if (m->m_len < ARC_HDRNEWLEN) {
380 m = m_pullup(m, ARC_HDRNEWLEN);
381 if (m == NULL) {
382 ++ifp->if_ierrors;
383 return NULL;
387 ah = mtod(m, struct arc_header *);
388 typ = ah->arc_type;
390 if (!arc_isphds(typ))
391 return m;
393 src = ah->arc_shost;
394 dst = ah->arc_dhost;
396 if (ah->arc_flag == 0xff) {
397 m_adj(m, 4);
399 if (m->m_len < ARC_HDRNEWLEN) {
400 m = m_pullup(m, ARC_HDRNEWLEN);
401 if (m == NULL) {
402 ++ifp->if_ierrors;
403 return NULL;
407 ah = mtod(m, struct arc_header *);
410 af = &ac->ac_fragtab[src];
411 m1 = af->af_packet;
412 s = "debug code error";
414 if (ah->arc_flag & 1) {
416 * first fragment. We always initialize, which is
417 * about the right thing to do, as we only want to
418 * accept one fragmented packet per src at a time.
420 if (m1 != NULL)
421 m_freem(m1);
423 af->af_packet = m;
424 m1 = m;
425 af->af_maxflag = ah->arc_flag;
426 af->af_lastseen = 0;
427 af->af_seqid = ah->arc_seqid;
429 return NULL;
430 /* notreached */
431 } else {
432 /* check for unfragmented packet */
433 if (ah->arc_flag == 0)
434 return m;
436 /* do we have a first packet from that src? */
437 if (m1 == NULL) {
438 s = "no first frag";
439 goto outofseq;
442 ah1 = mtod(m1, struct arc_header *);
444 if (ah->arc_seqid != ah1->arc_seqid) {
445 s = "seqid differs";
446 goto outofseq;
449 if (typ != ah1->arc_type) {
450 s = "type differs";
451 goto outofseq;
454 if (dst != ah1->arc_dhost) {
455 s = "dest host differs";
456 goto outofseq;
459 /* typ, seqid and dst are ok here. */
461 if (ah->arc_flag == af->af_lastseen) {
462 m_freem(m);
463 return NULL;
466 if (ah->arc_flag == af->af_lastseen + 2) {
467 /* ok, this is next fragment */
468 af->af_lastseen = ah->arc_flag;
469 m_adj(m, ARC_HDRNEWLEN);
472 * m_cat might free the first mbuf (with pkthdr)
473 * in 2nd chain; therefore:
476 newflen = m->m_pkthdr.len;
478 m_cat(m1, m);
480 m1->m_pkthdr.len += newflen;
482 /* is it the last one? */
483 if (af->af_lastseen > af->af_maxflag) {
484 af->af_packet = NULL;
485 return (m1);
486 } else
487 return NULL;
489 s = "other reason";
490 /* if all else fails, it is out of sequence, too */
492 outofseq:
493 if (m1) {
494 m_freem(m1);
495 af->af_packet = NULL;
498 if (m)
499 m_freem(m);
501 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
502 ifp->if_xname, s);
504 return NULL;
508 * return 1 if Packet Header Definition Standard, else 0.
509 * For now: old IP, old ARP aren't obviously. Lacking correct information,
510 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
511 * (Apple and Novell corporations were involved, among others, in PHDS work).
512 * Easiest is to assume that everybody else uses that, too.
515 arc_isphds(uint8_t type)
517 return (type != ARCTYPE_IP_OLD &&
518 type != ARCTYPE_ARP_OLD &&
519 type != ARCTYPE_DIAGNOSE);
523 * Process a received Arcnet packet;
524 * the packet is in the mbuf chain m with
525 * the ARCnet header.
527 static void
528 arc_input(struct ifnet *ifp, struct mbuf *m)
530 struct arc_header *ah;
531 struct ifqueue *inq;
532 uint8_t atype;
533 int s;
535 if ((ifp->if_flags & IFF_UP) == 0) {
536 m_freem(m);
537 return;
540 /* possibly defragment: */
541 m = arc_defrag(ifp, m);
542 if (m == NULL)
543 return;
545 ah = mtod(m, struct arc_header *);
547 ifp->if_ibytes += m->m_pkthdr.len;
549 if (arcbroadcastaddr == ah->arc_dhost) {
550 m->m_flags |= M_BCAST|M_MCAST;
551 ifp->if_imcasts++;
554 atype = ah->arc_type;
555 switch (atype) {
556 #ifdef INET
557 case ARCTYPE_IP:
558 m_adj(m, ARC_HDRNEWLEN);
559 schednetisr(NETISR_IP);
560 inq = &ipintrq;
561 break;
563 case ARCTYPE_IP_OLD:
564 m_adj(m, ARC_HDRLEN);
565 schednetisr(NETISR_IP);
566 inq = &ipintrq;
567 break;
569 case ARCTYPE_ARP:
570 m_adj(m, ARC_HDRNEWLEN);
571 schednetisr(NETISR_ARP);
572 inq = &arpintrq;
573 #ifdef ARCNET_ALLOW_BROKEN_ARP
574 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
575 #endif
576 break;
578 case ARCTYPE_ARP_OLD:
579 m_adj(m, ARC_HDRLEN);
580 schednetisr(NETISR_ARP);
581 inq = &arpintrq;
582 #ifdef ARCNET_ALLOW_BROKEN_ARP
583 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
584 #endif
585 break;
586 #endif
587 #ifdef INET6
588 case ARCTYPE_INET6:
589 m_adj(m, ARC_HDRNEWLEN);
590 schednetisr(NETISR_IPV6);
591 inq = &ip6intrq;
592 break;
593 #endif
594 default:
595 m_freem(m);
596 return;
599 s = splnet();
600 if (IF_QFULL(inq)) {
601 IF_DROP(inq);
602 m_freem(m);
603 } else
604 IF_ENQUEUE(inq, m);
605 splx(s);
609 * Convert Arcnet address to printable (loggable) representation.
611 char *
612 arc_sprintf(uint8_t *ap)
614 static char arcbuf[3];
615 char *cp = arcbuf;
617 *cp++ = hexdigits[*ap >> 4];
618 *cp++ = hexdigits[*ap++ & 0xf];
619 *cp = 0;
620 return (arcbuf);
624 * Perform common duties while attaching to interface list
626 void
627 arc_ifattach(struct ifnet *ifp, uint8_t lla)
629 struct arccom *ac;
631 ifp->if_type = IFT_ARCNET;
632 ifp->if_addrlen = 1;
633 ifp->if_hdrlen = ARC_HDRLEN;
634 ifp->if_dlt = DLT_ARCNET;
635 if (ifp->if_flags & IFF_BROADCAST)
636 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
637 if (ifp->if_flags & IFF_LINK0 && arc_ipmtu > ARC_PHDS_MAXMTU)
638 log(LOG_ERR,
639 "%s: arc_ipmtu is %d, but must not exceed %d",
640 ifp->if_xname, arc_ipmtu, ARC_PHDS_MAXMTU);
642 ifp->if_output = arc_output;
643 ifp->if_input = arc_input;
644 ac = (struct arccom *)ifp;
645 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
646 if (lla == 0) {
647 /* XXX this message isn't entirely clear, to me -- cgd */
648 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n",
649 ifp->if_xname, ifp->if_xname);
651 if_attach(ifp);
652 if_set_sadl(ifp, &lla, sizeof(lla), true);
654 ifp->if_broadcastaddr = &arcbroadcastaddr;
656 #if NBPFILTER > 0
657 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
658 #endif