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