if_arcsubr.c revision 1.36 1 /* $NetBSD: if_arcsubr.c,v 1.36 2001/06/14 05:44:23 itojun Exp $ */
2
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.
7 *
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
37 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
38 *
39 */
40 #include "opt_inet.h"
41
42 #include "bpfilter.h"
43
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>
54
55 #include <machine/cpu.h>
56
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>
65
66 #if NBPFILTER > 0
67 #include <net/bpf.h>
68 #endif
69
70 #ifdef INET
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
73 #include <netinet/if_inarp.h>
74 #endif
75
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
83
84 #define ARCNET_ALLOW_BROKEN_ARP
85
86 #ifndef ARC_IPMTU
87 #define ARC_IPMTU 1500
88 #endif
89
90 static struct mbuf *arc_defrag __P((struct ifnet *, struct mbuf *));
91
92 /*
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.
97 */
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 u_int8_t arcbroadcastaddr = 0;
103
104 #define senderr(e) { error = (e); goto bad;}
105 #define SIN(s) ((struct sockaddr_in *)s)
106
107 static int arc_output __P((struct ifnet *, struct mbuf *,
108 struct sockaddr *, struct rtentry *));
109 static void arc_input __P((struct ifnet *, struct mbuf *));
110
111 /*
112 * ARCnet output routine.
113 * Encapsulate a packet of type family for the local net.
114 * Assumes that ifp is actually pointer to arccom structure.
115 */
116 static int
117 arc_output(ifp, m0, dst, rt0)
118 struct ifnet *ifp;
119 struct mbuf *m0;
120 struct sockaddr *dst;
121 struct rtentry *rt0;
122 {
123 struct mbuf *m, *m1, *mcopy;
124 struct rtentry *rt;
125 struct arccom *ac;
126 struct arc_header *ah;
127 struct arphdr *arph;
128 int s, error, newencoding;
129 u_int8_t atype, adst, myself;
130 int tfrags, sflag, fsflag, rsflag;
131
132 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
133 return(ENETDOWN); /* m, m1 aren't initialized yet */
134
135 error = newencoding = 0;
136 ac = (struct arccom *)ifp;
137 m = m0;
138 mcopy = m1 = NULL;
139
140 myself = *LLADDR(ifp->if_sadl);
141
142 if ((rt = rt0)) {
143 if ((rt->rt_flags & RTF_UP) == 0) {
144 if ((rt0 = rt = rtalloc1(dst, 1)))
145 rt->rt_refcnt--;
146 else
147 senderr(EHOSTUNREACH);
148 }
149 if (rt->rt_flags & RTF_GATEWAY) {
150 if (rt->rt_gwroute == 0)
151 goto lookup;
152 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
153 rtfree(rt); rt = rt0;
154 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
155 if ((rt = rt->rt_gwroute) == 0)
156 senderr(EHOSTUNREACH);
157 }
158 }
159 if (rt->rt_flags & RTF_REJECT)
160 if (rt->rt_rmx.rmx_expire == 0 ||
161 time.tv_sec < rt->rt_rmx.rmx_expire)
162 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
163 }
164
165 switch (dst->sa_family) {
166 #ifdef INET
167 case AF_INET:
168
169 /*
170 * For now, use the simple IP addr -> ARCnet addr mapping
171 */
172 if (m->m_flags & (M_BCAST|M_MCAST))
173 adst = arcbroadcastaddr; /* ARCnet broadcast address */
174 else if (ifp->if_flags & IFF_NOARP)
175 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
176 else if (!arpresolve(ifp, rt, m, dst, &adst))
177 return 0; /* not resolved yet */
178
179 /* If broadcasting on a simplex interface, loopback a copy */
180 if ((m->m_flags & (M_BCAST|M_MCAST)) &&
181 (ifp->if_flags & IFF_SIMPLEX))
182 mcopy = m_copy(m, 0, (int)M_COPYALL);
183 if (ifp->if_flags & IFF_LINK0) {
184 atype = ARCTYPE_IP;
185 newencoding = 1;
186 } else {
187 atype = ARCTYPE_IP_OLD;
188 newencoding = 0;
189 }
190 break;
191
192 case AF_ARP:
193 arph = mtod(m, struct arphdr *);
194 if (m->m_flags & M_BCAST)
195 adst = arcbroadcastaddr;
196 else
197 adst = *ar_tha(arph);
198
199 arph->ar_hrd = htons(ARPHRD_ARCNET);
200
201 switch(ntohs(arph->ar_op)) {
202
203 case ARPOP_REVREQUEST:
204 case ARPOP_REVREPLY:
205 if (!(ifp->if_flags & IFF_LINK0)) {
206 printf("%s: can't handle af%d\n",
207 ifp->if_xname, dst->sa_family);
208 senderr(EAFNOSUPPORT);
209 }
210
211 atype = htons(ARCTYPE_REVARP);
212 newencoding = 1;
213 break;
214
215 case ARPOP_REQUEST:
216 case ARPOP_REPLY:
217 default:
218 if (ifp->if_flags & IFF_LINK0) {
219 atype = htons(ARCTYPE_ARP);
220 newencoding = 1;
221 } else {
222 atype = htons(ARCTYPE_ARP_OLD);
223 newencoding = 0;
224 }
225 }
226 #ifdef ARCNET_ALLOW_BROKEN_ARP
227 /*
228 * XXX It's not clear per RFC826 if this is needed, but
229 * "assigned numbers" say this is wrong.
230 * However, e.g., AmiTCP 3.0Beta used it... we make this
231 * switchable for emergency cases. Not perfect, but...
232 */
233 if (ifp->if_flags & IFF_LINK2)
234 arph->ar_pro = atype - 1;
235 #endif
236 break;
237 #endif
238 #ifdef INET6
239 case AF_INET6:
240 #ifdef OLDIP6OUTPUT
241 if (!nd6_resolve(ifp, rt, m, dst, (u_char *)&adst))
242 return(0); /* if not yet resolves */
243 #else
244 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)&adst))
245 return(0); /* it must be impossible, but... */
246 #endif /* OLDIP6OUTPUT */
247 atype = htons(ARCTYPE_INET6);
248 newencoding = 1;
249 break;
250 #endif
251
252 case AF_UNSPEC:
253 ah = (struct arc_header *)dst->sa_data;
254 adst = ah->arc_dhost;
255 atype = ah->arc_type;
256 break;
257
258 default:
259 printf("%s: can't handle af%d\n", ifp->if_xname,
260 dst->sa_family);
261 senderr(EAFNOSUPPORT);
262 }
263
264 if (mcopy)
265 (void) looutput(ifp, mcopy, dst, rt);
266
267 /*
268 * Add local net header. If no space in first mbuf,
269 * allocate another.
270 *
271 * For ARCnet, this is just symbolic. The header changes
272 * form and position on its way into the hardware and out of
273 * the wire. At this point, it contains source, destination and
274 * packet type.
275 */
276 if (newencoding) {
277 ++ac->ac_seqid; /* make the seqid unique */
278
279 tfrags = (m->m_pkthdr.len + 503) / 504;
280 fsflag = 2 * tfrags - 3;
281 sflag = 0;
282 rsflag = fsflag;
283
284 while (sflag < fsflag) {
285 /* we CAN'T have short packets here */
286 m1 = m_split(m, 504, M_DONTWAIT);
287 if (m1 == 0)
288 senderr(ENOBUFS);
289
290 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
291 if (m == 0)
292 senderr(ENOBUFS);
293 ah = mtod(m, struct arc_header *);
294 ah->arc_type = atype;
295 ah->arc_dhost = adst;
296 ah->arc_shost = myself;
297 ah->arc_flag = rsflag;
298 ah->arc_seqid = ac->ac_seqid;
299
300 s = splnet();
301 /*
302 * Queue message on interface, and start output if
303 * interface not yet active.
304 */
305 if (IF_QFULL(&ifp->if_snd)) {
306 IF_DROP(&ifp->if_snd);
307 splx(s);
308 senderr(ENOBUFS);
309 }
310 ifp->if_obytes += m->m_pkthdr.len;
311 IF_ENQUEUE(&ifp->if_snd, m);
312 if ((ifp->if_flags & IFF_OACTIVE) == 0)
313 (*ifp->if_start)(ifp);
314 splx(s);
315
316 m = m1;
317 sflag += 2;
318 rsflag = sflag;
319 }
320 m1 = NULL;
321
322
323 /* here we can have small, especially forbidden packets */
324
325 if ((m->m_pkthdr.len >=
326 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
327 (m->m_pkthdr.len <=
328 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
329
330 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
331 if (m == 0)
332 senderr(ENOBUFS);
333 ah = mtod(m, struct arc_header *);
334 ah->arc_flag = 0xFF;
335 ah->arc_seqid = 0xFFFF;
336 ah->arc_type2 = atype;
337 ah->arc_flag2 = sflag;
338 ah->arc_seqid2 = ac->ac_seqid;
339 } else {
340 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
341 if (m == 0)
342 senderr(ENOBUFS);
343 ah = mtod(m, struct arc_header *);
344 ah->arc_flag = sflag;
345 ah->arc_seqid = ac->ac_seqid;
346 }
347
348 ah->arc_dhost = adst;
349 ah->arc_shost = myself;
350 ah->arc_type = atype;
351 } else {
352 M_PREPEND(m, ARC_HDRLEN, M_DONTWAIT);
353 if (m == 0)
354 senderr(ENOBUFS);
355 ah = mtod(m, struct arc_header *);
356 ah->arc_type = atype;
357 ah->arc_dhost = adst;
358 ah->arc_shost = myself;
359 }
360 s = splnet();
361 /*
362 * Queue message on interface, and start output if interface
363 * not yet active.
364 */
365 if (IF_QFULL(&ifp->if_snd)) {
366 IF_DROP(&ifp->if_snd);
367 splx(s);
368 senderr(ENOBUFS);
369 }
370 ifp->if_obytes += m->m_pkthdr.len;
371 IF_ENQUEUE(&ifp->if_snd, m);
372 if ((ifp->if_flags & IFF_OACTIVE) == 0)
373 (*ifp->if_start)(ifp);
374 splx(s);
375
376 return (error);
377
378 bad:
379 if (m1)
380 m_freem(m1);
381 if (m)
382 m_freem(m);
383 return (error);
384 }
385
386 /*
387 * Defragmenter. Returns mbuf if last packet found, else
388 * NULL. frees imcoming mbuf as necessary.
389 */
390
391 __inline struct mbuf *
392 arc_defrag(ifp, m)
393 struct ifnet *ifp;
394 struct mbuf *m;
395 {
396 struct arc_header *ah, *ah1;
397 struct arccom *ac;
398 struct ac_frag *af;
399 struct mbuf *m1;
400 char *s;
401 int newflen;
402 u_char src,dst,typ;
403
404 ac = (struct arccom *)ifp;
405
406 if (m->m_len < ARC_HDRNEWLEN) {
407 m = m_pullup(m, ARC_HDRNEWLEN);
408 if (m == NULL) {
409 ++ifp->if_ierrors;
410 return NULL;
411 }
412 }
413
414 ah = mtod(m, struct arc_header *);
415 typ = ah->arc_type;
416
417 if (!arc_isphds(typ))
418 return m;
419
420 src = ah->arc_shost;
421 dst = ah->arc_dhost;
422
423 if (ah->arc_flag == 0xff) {
424 m_adj(m, 4);
425
426 if (m->m_len < ARC_HDRNEWLEN) {
427 m = m_pullup(m, ARC_HDRNEWLEN);
428 if (m == NULL) {
429 ++ifp->if_ierrors;
430 return NULL;
431 }
432 }
433
434 ah = mtod(m, struct arc_header *);
435 }
436
437 af = &ac->ac_fragtab[src];
438 m1 = af->af_packet;
439 s = "debug code error";
440
441 if (ah->arc_flag & 1) {
442 /*
443 * first fragment. We always initialize, which is
444 * about the right thing to do, as we only want to
445 * accept one fragmented packet per src at a time.
446 */
447 if (m1 != NULL)
448 m_freem(m1);
449
450 af->af_packet = m;
451 m1 = m;
452 af->af_maxflag = ah->arc_flag;
453 af->af_lastseen = 0;
454 af->af_seqid = ah->arc_seqid;
455
456 return NULL;
457 /* notreached */
458 } else {
459 /* check for unfragmented packet */
460 if (ah->arc_flag == 0)
461 return m;
462
463 /* do we have a first packet from that src? */
464 if (m1 == NULL) {
465 s = "no first frag";
466 goto outofseq;
467 }
468
469 ah1 = mtod(m1, struct arc_header *);
470
471 if (ah->arc_seqid != ah1->arc_seqid) {
472 s = "seqid differs";
473 goto outofseq;
474 }
475
476 if (typ != ah1->arc_type) {
477 s = "type differs";
478 goto outofseq;
479 }
480
481 if (dst != ah1->arc_dhost) {
482 s = "dest host differs";
483 goto outofseq;
484 }
485
486 /* typ, seqid and dst are ok here. */
487
488 if (ah->arc_flag == af->af_lastseen) {
489 m_freem(m);
490 return NULL;
491 }
492
493 if (ah->arc_flag == af->af_lastseen + 2) {
494 /* ok, this is next fragment */
495 af->af_lastseen = ah->arc_flag;
496 m_adj(m,ARC_HDRNEWLEN);
497
498 /*
499 * m_cat might free the first mbuf (with pkthdr)
500 * in 2nd chain; therefore:
501 */
502
503 newflen = m->m_pkthdr.len;
504
505 m_cat(m1,m);
506
507 m1->m_pkthdr.len += newflen;
508
509 /* is it the last one? */
510 if (af->af_lastseen > af->af_maxflag) {
511 af->af_packet = NULL;
512 return(m1);
513 } else
514 return NULL;
515 }
516 s = "other reason";
517 /* if all else fails, it is out of sequence, too */
518 }
519 outofseq:
520 if (m1) {
521 m_freem(m1);
522 af->af_packet = NULL;
523 }
524
525 if (m)
526 m_freem(m);
527
528 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
529 ifp->if_xname, s);
530
531 return NULL;
532 }
533
534 /*
535 * return 1 if Packet Header Definition Standard, else 0.
536 * For now: old IP, old ARP aren't obviously. Lacking correct information,
537 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
538 * (Apple and Novell corporations were involved, among others, in PHDS work).
539 * Easiest is to assume that everybody else uses that, too.
540 */
541 int
542 arc_isphds(type)
543 u_int8_t type;
544 {
545 return (type != ARCTYPE_IP_OLD &&
546 type != ARCTYPE_ARP_OLD &&
547 type != ARCTYPE_DIAGNOSE);
548 }
549
550 /*
551 * Process a received Arcnet packet;
552 * the packet is in the mbuf chain m with
553 * the ARCnet header.
554 */
555 static void
556 arc_input(ifp, m)
557 struct ifnet *ifp;
558 struct mbuf *m;
559 {
560 struct arc_header *ah;
561 struct ifqueue *inq;
562 u_int8_t atype;
563 int s;
564 struct arphdr *arph;
565
566 if ((ifp->if_flags & IFF_UP) == 0) {
567 m_freem(m);
568 return;
569 }
570
571 /* possibly defragment: */
572 m = arc_defrag(ifp, m);
573 if (m == NULL)
574 return;
575
576 ah = mtod(m, struct arc_header *);
577
578 ifp->if_ibytes += m->m_pkthdr.len;
579
580 if (arcbroadcastaddr == ah->arc_dhost) {
581 m->m_flags |= M_BCAST|M_MCAST;
582 ifp->if_imcasts++;
583 }
584
585 atype = ah->arc_type;
586 switch (atype) {
587 #ifdef INET
588 case ARCTYPE_IP:
589 m_adj(m, ARC_HDRNEWLEN);
590 schednetisr(NETISR_IP);
591 inq = &ipintrq;
592 break;
593
594 case ARCTYPE_IP_OLD:
595 m_adj(m, ARC_HDRLEN);
596 schednetisr(NETISR_IP);
597 inq = &ipintrq;
598 break;
599
600 case ARCTYPE_ARP:
601 m_adj(m, ARC_HDRNEWLEN);
602 schednetisr(NETISR_ARP);
603 inq = &arpintrq;
604 #ifdef ARCNET_ALLOW_BROKEN_ARP
605 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
606 #endif
607 break;
608
609 case ARCTYPE_ARP_OLD:
610 m_adj(m, ARC_HDRLEN);
611 schednetisr(NETISR_ARP);
612 inq = &arpintrq;
613 arph = mtod(m, struct arphdr *);
614 #ifdef ARCNET_ALLOW_BROKEN_ARP
615 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
616 #endif
617 break;
618 #endif
619 #ifdef INET6
620 case ARCTYPE_INET6:
621 m_adj(m, ARC_HDRNEWLEN);
622 schednetisr(NETISR_IPV6);
623 inq = &ip6intrq;
624 break;
625 #endif
626 default:
627 m_freem(m);
628 return;
629 }
630
631 s = splnet();
632 if (IF_QFULL(inq)) {
633 IF_DROP(inq);
634 m_freem(m);
635 } else
636 IF_ENQUEUE(inq, m);
637 splx(s);
638 }
639
640 /*
641 * Convert Arcnet address to printable (loggable) representation.
642 */
643 static char digits[] = "0123456789abcdef";
644 char *
645 arc_sprintf(ap)
646 u_int8_t *ap;
647 {
648 static char arcbuf[3];
649 char *cp = arcbuf;
650
651 *cp++ = digits[*ap >> 4];
652 *cp++ = digits[*ap++ & 0xf];
653 *cp = 0;
654 return (arcbuf);
655 }
656
657 /*
658 * Register (new) link level address.
659 */
660 void
661 arc_storelladdr(ifp, lla)
662 struct ifnet *ifp;
663 u_int8_t lla;
664 {
665
666 *(LLADDR(ifp->if_sadl)) = lla;
667 ifp->if_mtu = ARC_PHDS_MAXMTU;
668 }
669
670 /*
671 * Perform common duties while attaching to interface list
672 */
673 void
674 arc_ifattach(ifp, lla)
675 struct ifnet *ifp;
676 u_int8_t lla;
677 {
678 struct arccom *ac;
679
680 ifp->if_type = IFT_ARCNET;
681 ifp->if_addrlen = 1;
682 ifp->if_hdrlen = ARC_HDRLEN;
683 ifp->if_dlt = DLT_ARCNET;
684 if (ifp->if_flags & IFF_BROADCAST)
685 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
686 if (ifp->if_flags & IFF_LINK0 && arc_ipmtu > ARC_PHDS_MAXMTU)
687 log(LOG_ERR,
688 "%s: arc_ipmtu is %d, but must not exceed %d",
689 ifp->if_xname, arc_ipmtu, ARC_PHDS_MAXMTU);
690
691 ifp->if_output = arc_output;
692 ifp->if_input = arc_input;
693 ac = (struct arccom *)ifp;
694 ac->ac_seqid = (time.tv_sec) & 0xFFFF; /* try to make seqid unique */
695 if (lla == 0) {
696 /* XXX this message isn't entirely clear, to me -- cgd */
697 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n",
698 ifp->if_xname, ifp->if_xname);
699 }
700 if_attach(ifp);
701 if_alloc_sadl(ifp);
702 arc_storelladdr(ifp, lla);
703
704 ifp->if_broadcastaddr = &arcbroadcastaddr;
705
706 #if NBPFILTER > 0
707 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
708 #endif
709 }
710