1 /* $NetBSD: if_arcsubr.c,v 1.37 2001/10/17 08:23:05 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 "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 if ((rt = rt0)) { 143 if ((rt->rt_flags & RTF_UP) == 0) { 144 if ((rt0 = rt = rtalloc1(dst, 1))) 145 rt->rt_refcnt--; 146 else 147 senderr(EHOSTUNREACH); 148 } 149 if (rt->rt_flags & RTF_GATEWAY) { 150 if (rt->rt_gwroute == 0) 151 goto lookup; 152 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 153 rtfree(rt); rt = rt0; 154 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1); 155 if ((rt = rt->rt_gwroute) == 0) 156 senderr(EHOSTUNREACH); 157 } 158 } 159 if (rt->rt_flags & RTF_REJECT) 160 if (rt->rt_rmx.rmx_expire == 0 || 161 time.tv_sec < rt->rt_rmx.rmx_expire) 162 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 163 } 164 165 switch (dst->sa_family) { 166 #ifdef INET 167 case AF_INET: 168 169 /* 170 * For now, use the simple IP addr -> ARCnet addr mapping 171 */ 172 if (m->m_flags & (M_BCAST|M_MCAST)) 173 adst = arcbroadcastaddr; /* ARCnet broadcast address */ 174 else if (ifp->if_flags & IFF_NOARP) 175 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF; 176 else if (!arpresolve(ifp, rt, m, dst, &adst)) 177 return 0; /* not resolved yet */ 178 179 /* If broadcasting on a simplex interface, loopback a copy */ 180 if ((m->m_flags & (M_BCAST|M_MCAST)) && 181 (ifp->if_flags & IFF_SIMPLEX)) 182 mcopy = m_copy(m, 0, (int)M_COPYALL); 183 if (ifp->if_flags & IFF_LINK0) { 184 atype = ARCTYPE_IP; 185 newencoding = 1; 186 } else { 187 atype = ARCTYPE_IP_OLD; 188 newencoding = 0; 189 } 190 break; 191 192 case AF_ARP: 193 arph = mtod(m, struct arphdr *); 194 if (m->m_flags & M_BCAST) 195 adst = arcbroadcastaddr; 196 else 197 adst = *ar_tha(arph); 198 199 arph->ar_hrd = htons(ARPHRD_ARCNET); 200 201 switch(ntohs(arph->ar_op)) { 202 203 case ARPOP_REVREQUEST: 204 case ARPOP_REVREPLY: 205 if (!(ifp->if_flags & IFF_LINK0)) { 206 printf("%s: can't handle af%d\n", 207 ifp->if_xname, dst->sa_family); 208 senderr(EAFNOSUPPORT); 209 } 210 211 atype = htons(ARCTYPE_REVARP); 212 newencoding = 1; 213 break; 214 215 case ARPOP_REQUEST: 216 case ARPOP_REPLY: 217 default: 218 if (ifp->if_flags & IFF_LINK0) { 219 atype = htons(ARCTYPE_ARP); 220 newencoding = 1; 221 } else { 222 atype = htons(ARCTYPE_ARP_OLD); 223 newencoding = 0; 224 } 225 } 226 #ifdef ARCNET_ALLOW_BROKEN_ARP 227 /* 228 * XXX It's not clear per RFC826 if this is needed, but 229 * "assigned numbers" say this is wrong. 230 * However, e.g., AmiTCP 3.0Beta used it... we make this 231 * switchable for emergency cases. Not perfect, but... 232 */ 233 if (ifp->if_flags & IFF_LINK2) 234 arph->ar_pro = atype - 1; 235 #endif 236 break; 237 #endif 238 #ifdef INET6 239 case AF_INET6: 240 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)&adst)) 241 return(0); /* it must be impossible, but... */ 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 = splnet(); 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 = splnet(); 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_ibytes += m->m_pkthdr.len; 574 575 if (arcbroadcastaddr == ah->arc_dhost) { 576 m->m_flags |= M_BCAST|M_MCAST; 577 ifp->if_imcasts++; 578 } 579 580 atype = ah->arc_type; 581 switch (atype) { 582 #ifdef INET 583 case ARCTYPE_IP: 584 m_adj(m, ARC_HDRNEWLEN); 585 schednetisr(NETISR_IP); 586 inq = &ipintrq; 587 break; 588 589 case ARCTYPE_IP_OLD: 590 m_adj(m, ARC_HDRLEN); 591 schednetisr(NETISR_IP); 592 inq = &ipintrq; 593 break; 594 595 case ARCTYPE_ARP: 596 m_adj(m, ARC_HDRNEWLEN); 597 schednetisr(NETISR_ARP); 598 inq = &arpintrq; 599 #ifdef ARCNET_ALLOW_BROKEN_ARP 600 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP); 601 #endif 602 break; 603 604 case ARCTYPE_ARP_OLD: 605 m_adj(m, ARC_HDRLEN); 606 schednetisr(NETISR_ARP); 607 inq = &arpintrq; 608 arph = mtod(m, struct arphdr *); 609 #ifdef ARCNET_ALLOW_BROKEN_ARP 610 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP); 611 #endif 612 break; 613 #endif 614 #ifdef INET6 615 case ARCTYPE_INET6: 616 m_adj(m, ARC_HDRNEWLEN); 617 schednetisr(NETISR_IPV6); 618 inq = &ip6intrq; 619 break; 620 #endif 621 default: 622 m_freem(m); 623 return; 624 } 625 626 s = splnet(); 627 if (IF_QFULL(inq)) { 628 IF_DROP(inq); 629 m_freem(m); 630 } else 631 IF_ENQUEUE(inq, m); 632 splx(s); 633 } 634 635 /* 636 * Convert Arcnet address to printable (loggable) representation. 637 */ 638 static char digits[] = "0123456789abcdef"; 639 char * 640 arc_sprintf(ap) 641 u_int8_t *ap; 642 { 643 static char arcbuf[3]; 644 char *cp = arcbuf; 645 646 *cp++ = digits[*ap >> 4]; 647 *cp++ = digits[*ap++ & 0xf]; 648 *cp = 0; 649 return (arcbuf); 650 } 651 652 /* 653 * Register (new) link level address. 654 */ 655 void 656 arc_storelladdr(ifp, lla) 657 struct ifnet *ifp; 658 u_int8_t lla; 659 { 660 661 *(LLADDR(ifp->if_sadl)) = lla; 662 ifp->if_mtu = ARC_PHDS_MAXMTU; 663 } 664 665 /* 666 * Perform common duties while attaching to interface list 667 */ 668 void 669 arc_ifattach(ifp, lla) 670 struct ifnet *ifp; 671 u_int8_t lla; 672 { 673 struct arccom *ac; 674 675 ifp->if_type = IFT_ARCNET; 676 ifp->if_addrlen = 1; 677 ifp->if_hdrlen = ARC_HDRLEN; 678 ifp->if_dlt = DLT_ARCNET; 679 if (ifp->if_flags & IFF_BROADCAST) 680 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI; 681 if (ifp->if_flags & IFF_LINK0 && arc_ipmtu > ARC_PHDS_MAXMTU) 682 log(LOG_ERR, 683 "%s: arc_ipmtu is %d, but must not exceed %d", 684 ifp->if_xname, arc_ipmtu, ARC_PHDS_MAXMTU); 685 686 ifp->if_output = arc_output; 687 ifp->if_input = arc_input; 688 ac = (struct arccom *)ifp; 689 ac->ac_seqid = (time.tv_sec) & 0xFFFF; /* try to make seqid unique */ 690 if (lla == 0) { 691 /* XXX this message isn't entirely clear, to me -- cgd */ 692 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n", 693 ifp->if_xname, ifp->if_xname); 694 } 695 if_attach(ifp); 696 if_alloc_sadl(ifp); 697 arc_storelladdr(ifp, lla); 698 699 ifp->if_broadcastaddr = &arcbroadcastaddr; 700 701 #if NBPFILTER > 0 702 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN); 703 #endif 704 } 705