1 /* $NetBSD: if_arcsubr.c,v 1.45 2004/03/25 10:53:46 is 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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp 33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93 34 * 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: if_arcsubr.c,v 1.45 2004/03/25 10:53:46 is Exp $"); 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, len; 129 u_int8_t atype, adst, myself; 130 int tfrags, sflag, fsflag, rsflag; 131 ALTQ_DECL(struct altq_pktattr pktattr;) 132 133 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 134 return (ENETDOWN); /* m, m1 aren't initialized yet */ 135 136 error = newencoding = 0; 137 ac = (struct arccom *)ifp; 138 m = m0; 139 mcopy = m1 = NULL; 140 141 myself = *LLADDR(ifp->if_sadl); 142 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 /* 167 * if the queueing discipline needs packet classification, 168 * do it before prepending link headers. 169 */ 170 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr); 171 172 switch (dst->sa_family) { 173 #ifdef INET 174 case AF_INET: 175 176 /* 177 * For now, use the simple IP addr -> ARCnet addr mapping 178 */ 179 if (m->m_flags & (M_BCAST|M_MCAST)) 180 adst = arcbroadcastaddr; /* ARCnet broadcast address */ 181 else if (ifp->if_flags & IFF_NOARP) 182 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF; 183 else if (!arpresolve(ifp, rt, m, dst, &adst)) 184 return 0; /* not resolved yet */ 185 186 /* If broadcasting on a simplex interface, loopback a copy */ 187 if ((m->m_flags & (M_BCAST|M_MCAST)) && 188 (ifp->if_flags & IFF_SIMPLEX)) 189 mcopy = m_copy(m, 0, (int)M_COPYALL); 190 if (ifp->if_flags & IFF_LINK0) { 191 atype = ARCTYPE_IP; 192 newencoding = 1; 193 } else { 194 atype = ARCTYPE_IP_OLD; 195 newencoding = 0; 196 } 197 break; 198 199 case AF_ARP: 200 arph = mtod(m, struct arphdr *); 201 if (m->m_flags & M_BCAST) 202 adst = arcbroadcastaddr; 203 else 204 adst = *ar_tha(arph); 205 206 arph->ar_hrd = htons(ARPHRD_ARCNET); 207 208 switch (ntohs(arph->ar_op)) { 209 case ARPOP_REVREQUEST: 210 case ARPOP_REVREPLY: 211 if (!(ifp->if_flags & IFF_LINK0)) { 212 printf("%s: can't handle af%d\n", 213 ifp->if_xname, dst->sa_family); 214 senderr(EAFNOSUPPORT); 215 } 216 217 atype = htons(ARCTYPE_REVARP); 218 newencoding = 1; 219 break; 220 221 case ARPOP_REQUEST: 222 case ARPOP_REPLY: 223 default: 224 if (ifp->if_flags & IFF_LINK0) { 225 atype = htons(ARCTYPE_ARP); 226 newencoding = 1; 227 } else { 228 atype = htons(ARCTYPE_ARP_OLD); 229 newencoding = 0; 230 } 231 } 232 #ifdef ARCNET_ALLOW_BROKEN_ARP 233 /* 234 * XXX It's not clear per RFC826 if this is needed, but 235 * "assigned numbers" say this is wrong. 236 * However, e.g., AmiTCP 3.0Beta used it... we make this 237 * switchable for emergency cases. Not perfect, but... 238 */ 239 if (ifp->if_flags & IFF_LINK2) 240 arph->ar_pro = atype - 1; 241 #endif 242 break; 243 #endif 244 #ifdef INET6 245 case AF_INET6: 246 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)&adst)) 247 return (0); /* it must be impossible, but... */ 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 len = m->m_pkthdr.len; 302 s = splnet(); 303 /* 304 * Queue message on interface, and start output if 305 * interface not yet active. 306 */ 307 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error); 308 if (error) { 309 /* mbuf is already freed */ 310 splx(s); 311 return (error); 312 } 313 ifp->if_obytes += len; 314 if ((ifp->if_flags & IFF_OACTIVE) == 0) 315 (*ifp->if_start)(ifp); 316 splx(s); 317 318 m = m1; 319 sflag += 2; 320 rsflag = sflag; 321 } 322 m1 = NULL; 323 324 325 /* here we can have small, especially forbidden packets */ 326 327 if ((m->m_pkthdr.len >= 328 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) && 329 (m->m_pkthdr.len <= 330 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) { 331 332 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT); 333 if (m == 0) 334 senderr(ENOBUFS); 335 ah = mtod(m, struct arc_header *); 336 ah->arc_flag = 0xFF; 337 ah->arc_seqid = 0xFFFF; 338 ah->arc_type2 = atype; 339 ah->arc_flag2 = sflag; 340 ah->arc_seqid2 = ac->ac_seqid; 341 } else { 342 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT); 343 if (m == 0) 344 senderr(ENOBUFS); 345 ah = mtod(m, struct arc_header *); 346 ah->arc_flag = sflag; 347 ah->arc_seqid = ac->ac_seqid; 348 } 349 350 ah->arc_dhost = adst; 351 ah->arc_shost = myself; 352 ah->arc_type = atype; 353 } else { 354 M_PREPEND(m, ARC_HDRLEN, M_DONTWAIT); 355 if (m == 0) 356 senderr(ENOBUFS); 357 ah = mtod(m, struct arc_header *); 358 ah->arc_type = atype; 359 ah->arc_dhost = adst; 360 ah->arc_shost = myself; 361 } 362 363 len = m->m_pkthdr.len; 364 s = splnet(); 365 /* 366 * Queue message on interface, and start output if interface 367 * not yet active. 368 */ 369 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error); 370 if (error) { 371 /* mbuf is already freed */ 372 splx(s); 373 return (error); 374 } 375 ifp->if_obytes += len; 376 if ((ifp->if_flags & IFF_OACTIVE) == 0) 377 (*ifp->if_start)(ifp); 378 splx(s); 379 380 return (error); 381 382 bad: 383 if (m1) 384 m_freem(m1); 385 if (m) 386 m_freem(m); 387 return (error); 388 } 389 390 /* 391 * Defragmenter. Returns mbuf if last packet found, else 392 * NULL. frees imcoming mbuf as necessary. 393 */ 394 395 __inline struct mbuf * 396 arc_defrag(ifp, m) 397 struct ifnet *ifp; 398 struct mbuf *m; 399 { 400 struct arc_header *ah, *ah1; 401 struct arccom *ac; 402 struct ac_frag *af; 403 struct mbuf *m1; 404 char *s; 405 int newflen; 406 u_char src, dst, typ; 407 408 ac = (struct arccom *)ifp; 409 410 if (m->m_len < ARC_HDRNEWLEN) { 411 m = m_pullup(m, ARC_HDRNEWLEN); 412 if (m == NULL) { 413 ++ifp->if_ierrors; 414 return NULL; 415 } 416 } 417 418 ah = mtod(m, struct arc_header *); 419 typ = ah->arc_type; 420 421 if (!arc_isphds(typ)) 422 return m; 423 424 src = ah->arc_shost; 425 dst = ah->arc_dhost; 426 427 if (ah->arc_flag == 0xff) { 428 m_adj(m, 4); 429 430 if (m->m_len < ARC_HDRNEWLEN) { 431 m = m_pullup(m, ARC_HDRNEWLEN); 432 if (m == NULL) { 433 ++ifp->if_ierrors; 434 return NULL; 435 } 436 } 437 438 ah = mtod(m, struct arc_header *); 439 } 440 441 af = &ac->ac_fragtab[src]; 442 m1 = af->af_packet; 443 s = "debug code error"; 444 445 if (ah->arc_flag & 1) { 446 /* 447 * first fragment. We always initialize, which is 448 * about the right thing to do, as we only want to 449 * accept one fragmented packet per src at a time. 450 */ 451 if (m1 != NULL) 452 m_freem(m1); 453 454 af->af_packet = m; 455 m1 = m; 456 af->af_maxflag = ah->arc_flag; 457 af->af_lastseen = 0; 458 af->af_seqid = ah->arc_seqid; 459 460 return NULL; 461 /* notreached */ 462 } else { 463 /* check for unfragmented packet */ 464 if (ah->arc_flag == 0) 465 return m; 466 467 /* do we have a first packet from that src? */ 468 if (m1 == NULL) { 469 s = "no first frag"; 470 goto outofseq; 471 } 472 473 ah1 = mtod(m1, struct arc_header *); 474 475 if (ah->arc_seqid != ah1->arc_seqid) { 476 s = "seqid differs"; 477 goto outofseq; 478 } 479 480 if (typ != ah1->arc_type) { 481 s = "type differs"; 482 goto outofseq; 483 } 484 485 if (dst != ah1->arc_dhost) { 486 s = "dest host differs"; 487 goto outofseq; 488 } 489 490 /* typ, seqid and dst are ok here. */ 491 492 if (ah->arc_flag == af->af_lastseen) { 493 m_freem(m); 494 return NULL; 495 } 496 497 if (ah->arc_flag == af->af_lastseen + 2) { 498 /* ok, this is next fragment */ 499 af->af_lastseen = ah->arc_flag; 500 m_adj(m, ARC_HDRNEWLEN); 501 502 /* 503 * m_cat might free the first mbuf (with pkthdr) 504 * in 2nd chain; therefore: 505 */ 506 507 newflen = m->m_pkthdr.len; 508 509 m_cat(m1, m); 510 511 m1->m_pkthdr.len += newflen; 512 513 /* is it the last one? */ 514 if (af->af_lastseen > af->af_maxflag) { 515 af->af_packet = NULL; 516 return (m1); 517 } else 518 return NULL; 519 } 520 s = "other reason"; 521 /* if all else fails, it is out of sequence, too */ 522 } 523 outofseq: 524 if (m1) { 525 m_freem(m1); 526 af->af_packet = NULL; 527 } 528 529 if (m) 530 m_freem(m); 531 532 log(LOG_INFO,"%s: got out of seq. packet: %s\n", 533 ifp->if_xname, s); 534 535 return NULL; 536 } 537 538 /* 539 * return 1 if Packet Header Definition Standard, else 0. 540 * For now: old IP, old ARP aren't obviously. Lacking correct information, 541 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS. 542 * (Apple and Novell corporations were involved, among others, in PHDS work). 543 * Easiest is to assume that everybody else uses that, too. 544 */ 545 int 546 arc_isphds(type) 547 u_int8_t type; 548 { 549 return (type != ARCTYPE_IP_OLD && 550 type != ARCTYPE_ARP_OLD && 551 type != ARCTYPE_DIAGNOSE); 552 } 553 554 /* 555 * Process a received Arcnet packet; 556 * the packet is in the mbuf chain m with 557 * the ARCnet header. 558 */ 559 static void 560 arc_input(ifp, m) 561 struct ifnet *ifp; 562 struct mbuf *m; 563 { 564 struct arc_header *ah; 565 struct ifqueue *inq; 566 u_int8_t atype; 567 int s; 568 569 if ((ifp->if_flags & IFF_UP) == 0) { 570 m_freem(m); 571 return; 572 } 573 574 /* possibly defragment: */ 575 m = arc_defrag(ifp, m); 576 if (m == NULL) 577 return; 578 579 ah = mtod(m, struct arc_header *); 580 581 ifp->if_ibytes += m->m_pkthdr.len; 582 583 if (arcbroadcastaddr == ah->arc_dhost) { 584 m->m_flags |= M_BCAST|M_MCAST; 585 ifp->if_imcasts++; 586 } 587 588 atype = ah->arc_type; 589 switch (atype) { 590 #ifdef INET 591 case ARCTYPE_IP: 592 m_adj(m, ARC_HDRNEWLEN); 593 schednetisr(NETISR_IP); 594 inq = &ipintrq; 595 break; 596 597 case ARCTYPE_IP_OLD: 598 m_adj(m, ARC_HDRLEN); 599 schednetisr(NETISR_IP); 600 inq = &ipintrq; 601 break; 602 603 case ARCTYPE_ARP: 604 m_adj(m, ARC_HDRNEWLEN); 605 schednetisr(NETISR_ARP); 606 inq = &arpintrq; 607 #ifdef ARCNET_ALLOW_BROKEN_ARP 608 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP); 609 #endif 610 break; 611 612 case ARCTYPE_ARP_OLD: 613 m_adj(m, ARC_HDRLEN); 614 schednetisr(NETISR_ARP); 615 inq = &arpintrq; 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