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