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