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