1 /* $NetBSD: if_arcsubr.c,v 1.49 2005/06/05 22:31:40 he 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.49 2005/06/05 22:31:40 he 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 error, newencoding; 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 if ((error = ifq_enqueue(ifp, m ALTQ_COMMA 302 ALTQ_DECL(&pktattr))) != 0) 303 return (error); 304 305 m = m1; 306 sflag += 2; 307 rsflag = sflag; 308 } 309 m1 = NULL; 310 311 312 /* here we can have small, especially forbidden packets */ 313 314 if ((m->m_pkthdr.len >= 315 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) && 316 (m->m_pkthdr.len <= 317 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) { 318 319 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT); 320 if (m == 0) 321 senderr(ENOBUFS); 322 ah = mtod(m, struct arc_header *); 323 ah->arc_flag = 0xFF; 324 ah->arc_seqid = 0xFFFF; 325 ah->arc_type2 = atype; 326 ah->arc_flag2 = sflag; 327 ah->arc_seqid2 = ac->ac_seqid; 328 } else { 329 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT); 330 if (m == 0) 331 senderr(ENOBUFS); 332 ah = mtod(m, struct arc_header *); 333 ah->arc_flag = sflag; 334 ah->arc_seqid = ac->ac_seqid; 335 } 336 337 ah->arc_dhost = adst; 338 ah->arc_shost = myself; 339 ah->arc_type = atype; 340 } else { 341 M_PREPEND(m, ARC_HDRLEN, M_DONTWAIT); 342 if (m == 0) 343 senderr(ENOBUFS); 344 ah = mtod(m, struct arc_header *); 345 ah->arc_type = atype; 346 ah->arc_dhost = adst; 347 ah->arc_shost = myself; 348 } 349 350 return ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr)); 351 352 bad: 353 if (m1) 354 m_freem(m1); 355 if (m) 356 m_freem(m); 357 return (error); 358 } 359 360 /* 361 * Defragmenter. Returns mbuf if last packet found, else 362 * NULL. frees imcoming mbuf as necessary. 363 */ 364 365 __inline struct mbuf * 366 arc_defrag(ifp, m) 367 struct ifnet *ifp; 368 struct mbuf *m; 369 { 370 struct arc_header *ah, *ah1; 371 struct arccom *ac; 372 struct ac_frag *af; 373 struct mbuf *m1; 374 const char *s; 375 int newflen; 376 u_char src, dst, typ; 377 378 ac = (struct arccom *)ifp; 379 380 if (m->m_len < ARC_HDRNEWLEN) { 381 m = m_pullup(m, ARC_HDRNEWLEN); 382 if (m == NULL) { 383 ++ifp->if_ierrors; 384 return NULL; 385 } 386 } 387 388 ah = mtod(m, struct arc_header *); 389 typ = ah->arc_type; 390 391 if (!arc_isphds(typ)) 392 return m; 393 394 src = ah->arc_shost; 395 dst = ah->arc_dhost; 396 397 if (ah->arc_flag == 0xff) { 398 m_adj(m, 4); 399 400 if (m->m_len < ARC_HDRNEWLEN) { 401 m = m_pullup(m, ARC_HDRNEWLEN); 402 if (m == NULL) { 403 ++ifp->if_ierrors; 404 return NULL; 405 } 406 } 407 408 ah = mtod(m, struct arc_header *); 409 } 410 411 af = &ac->ac_fragtab[src]; 412 m1 = af->af_packet; 413 s = "debug code error"; 414 415 if (ah->arc_flag & 1) { 416 /* 417 * first fragment. We always initialize, which is 418 * about the right thing to do, as we only want to 419 * accept one fragmented packet per src at a time. 420 */ 421 if (m1 != NULL) 422 m_freem(m1); 423 424 af->af_packet = m; 425 m1 = m; 426 af->af_maxflag = ah->arc_flag; 427 af->af_lastseen = 0; 428 af->af_seqid = ah->arc_seqid; 429 430 return NULL; 431 /* notreached */ 432 } else { 433 /* check for unfragmented packet */ 434 if (ah->arc_flag == 0) 435 return m; 436 437 /* do we have a first packet from that src? */ 438 if (m1 == NULL) { 439 s = "no first frag"; 440 goto outofseq; 441 } 442 443 ah1 = mtod(m1, struct arc_header *); 444 445 if (ah->arc_seqid != ah1->arc_seqid) { 446 s = "seqid differs"; 447 goto outofseq; 448 } 449 450 if (typ != ah1->arc_type) { 451 s = "type differs"; 452 goto outofseq; 453 } 454 455 if (dst != ah1->arc_dhost) { 456 s = "dest host differs"; 457 goto outofseq; 458 } 459 460 /* typ, seqid and dst are ok here. */ 461 462 if (ah->arc_flag == af->af_lastseen) { 463 m_freem(m); 464 return NULL; 465 } 466 467 if (ah->arc_flag == af->af_lastseen + 2) { 468 /* ok, this is next fragment */ 469 af->af_lastseen = ah->arc_flag; 470 m_adj(m, ARC_HDRNEWLEN); 471 472 /* 473 * m_cat might free the first mbuf (with pkthdr) 474 * in 2nd chain; therefore: 475 */ 476 477 newflen = m->m_pkthdr.len; 478 479 m_cat(m1, m); 480 481 m1->m_pkthdr.len += newflen; 482 483 /* is it the last one? */ 484 if (af->af_lastseen > af->af_maxflag) { 485 af->af_packet = NULL; 486 return (m1); 487 } else 488 return NULL; 489 } 490 s = "other reason"; 491 /* if all else fails, it is out of sequence, too */ 492 } 493 outofseq: 494 if (m1) { 495 m_freem(m1); 496 af->af_packet = NULL; 497 } 498 499 if (m) 500 m_freem(m); 501 502 log(LOG_INFO,"%s: got out of seq. packet: %s\n", 503 ifp->if_xname, s); 504 505 return NULL; 506 } 507 508 /* 509 * return 1 if Packet Header Definition Standard, else 0. 510 * For now: old IP, old ARP aren't obviously. Lacking correct information, 511 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS. 512 * (Apple and Novell corporations were involved, among others, in PHDS work). 513 * Easiest is to assume that everybody else uses that, too. 514 */ 515 int 516 arc_isphds(type) 517 u_int8_t type; 518 { 519 return (type != ARCTYPE_IP_OLD && 520 type != ARCTYPE_ARP_OLD && 521 type != ARCTYPE_DIAGNOSE); 522 } 523 524 /* 525 * Process a received Arcnet packet; 526 * the packet is in the mbuf chain m with 527 * the ARCnet header. 528 */ 529 static void 530 arc_input(ifp, m) 531 struct ifnet *ifp; 532 struct mbuf *m; 533 { 534 struct arc_header *ah; 535 struct ifqueue *inq; 536 u_int8_t atype; 537 int s; 538 539 if ((ifp->if_flags & IFF_UP) == 0) { 540 m_freem(m); 541 return; 542 } 543 544 /* possibly defragment: */ 545 m = arc_defrag(ifp, m); 546 if (m == NULL) 547 return; 548 549 ah = mtod(m, struct arc_header *); 550 551 ifp->if_ibytes += m->m_pkthdr.len; 552 553 if (arcbroadcastaddr == ah->arc_dhost) { 554 m->m_flags |= M_BCAST|M_MCAST; 555 ifp->if_imcasts++; 556 } 557 558 atype = ah->arc_type; 559 switch (atype) { 560 #ifdef INET 561 case ARCTYPE_IP: 562 m_adj(m, ARC_HDRNEWLEN); 563 schednetisr(NETISR_IP); 564 inq = &ipintrq; 565 break; 566 567 case ARCTYPE_IP_OLD: 568 m_adj(m, ARC_HDRLEN); 569 schednetisr(NETISR_IP); 570 inq = &ipintrq; 571 break; 572 573 case ARCTYPE_ARP: 574 m_adj(m, ARC_HDRNEWLEN); 575 schednetisr(NETISR_ARP); 576 inq = &arpintrq; 577 #ifdef ARCNET_ALLOW_BROKEN_ARP 578 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP); 579 #endif 580 break; 581 582 case ARCTYPE_ARP_OLD: 583 m_adj(m, ARC_HDRLEN); 584 schednetisr(NETISR_ARP); 585 inq = &arpintrq; 586 #ifdef ARCNET_ALLOW_BROKEN_ARP 587 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP); 588 #endif 589 break; 590 #endif 591 #ifdef INET6 592 case ARCTYPE_INET6: 593 m_adj(m, ARC_HDRNEWLEN); 594 schednetisr(NETISR_IPV6); 595 inq = &ip6intrq; 596 break; 597 #endif 598 default: 599 m_freem(m); 600 return; 601 } 602 603 s = splnet(); 604 if (IF_QFULL(inq)) { 605 IF_DROP(inq); 606 m_freem(m); 607 } else 608 IF_ENQUEUE(inq, m); 609 splx(s); 610 } 611 612 /* 613 * Convert Arcnet address to printable (loggable) representation. 614 */ 615 char * 616 arc_sprintf(ap) 617 u_int8_t *ap; 618 { 619 static char arcbuf[3]; 620 char *cp = arcbuf; 621 622 *cp++ = hexdigits[*ap >> 4]; 623 *cp++ = hexdigits[*ap++ & 0xf]; 624 *cp = 0; 625 return (arcbuf); 626 } 627 628 /* 629 * Register (new) link level address. 630 */ 631 void 632 arc_storelladdr(ifp, lla) 633 struct ifnet *ifp; 634 u_int8_t lla; 635 { 636 637 *(LLADDR(ifp->if_sadl)) = lla; 638 ifp->if_mtu = ARC_PHDS_MAXMTU; 639 } 640 641 /* 642 * Perform common duties while attaching to interface list 643 */ 644 void 645 arc_ifattach(ifp, lla) 646 struct ifnet *ifp; 647 u_int8_t lla; 648 { 649 struct arccom *ac; 650 651 ifp->if_type = IFT_ARCNET; 652 ifp->if_addrlen = 1; 653 ifp->if_hdrlen = ARC_HDRLEN; 654 ifp->if_dlt = DLT_ARCNET; 655 if (ifp->if_flags & IFF_BROADCAST) 656 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI; 657 if (ifp->if_flags & IFF_LINK0 && arc_ipmtu > ARC_PHDS_MAXMTU) 658 log(LOG_ERR, 659 "%s: arc_ipmtu is %d, but must not exceed %d", 660 ifp->if_xname, arc_ipmtu, ARC_PHDS_MAXMTU); 661 662 ifp->if_output = arc_output; 663 ifp->if_input = arc_input; 664 ac = (struct arccom *)ifp; 665 ac->ac_seqid = (time.tv_sec) & 0xFFFF; /* try to make seqid unique */ 666 if (lla == 0) { 667 /* XXX this message isn't entirely clear, to me -- cgd */ 668 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n", 669 ifp->if_xname, ifp->if_xname); 670 } 671 if_attach(ifp); 672 if_alloc_sadl(ifp); 673 arc_storelladdr(ifp, lla); 674 675 ifp->if_broadcastaddr = &arcbroadcastaddr; 676 677 #if NBPFILTER > 0 678 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN); 679 #endif 680 } 681