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