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