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