1 /* $NetBSD: if_arp.c,v 1.70 2000/08/15 20:24:57 jhawk Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Public Access Networks Corporation ("Panix"). It was developed under 9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Copyright (c) 1982, 1986, 1988, 1993 42 * The Regents of the University of California. All rights reserved. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * @(#)if_ether.c 8.2 (Berkeley) 9/26/94 73 */ 74 75 /* 76 * Ethernet address resolution protocol. 77 * TODO: 78 * add "inuse/lock" bit (or ref. count) along with valid bit 79 */ 80 81 #include "opt_ddb.h" 82 #include "opt_inet.h" 83 84 #ifdef INET 85 86 #include <sys/param.h> 87 #include <sys/systm.h> 88 #include <sys/callout.h> 89 #include <sys/malloc.h> 90 #include <sys/mbuf.h> 91 #include <sys/socket.h> 92 #include <sys/time.h> 93 #include <sys/kernel.h> 94 #include <sys/errno.h> 95 #include <sys/ioctl.h> 96 #include <sys/syslog.h> 97 #include <sys/proc.h> 98 #include <sys/protosw.h> 99 #include <sys/domain.h> 100 101 #include <net/ethertypes.h> 102 #include <net/if.h> 103 #include <net/if_dl.h> 104 #include <net/if_token.h> 105 #include <net/if_types.h> 106 #include <net/route.h> 107 108 109 #include <netinet/in.h> 110 #include <netinet/in_systm.h> 111 #include <netinet/in_var.h> 112 #include <netinet/ip.h> 113 #include <netinet/if_inarp.h> 114 115 #include "loop.h" 116 #include "arc.h" 117 #if NARC > 0 118 #include <net/if_arc.h> 119 #endif 120 #include "fddi.h" 121 #if NFDDI > 0 122 #include <net/if_fddi.h> 123 #endif 124 #include "token.h" 125 #include "token.h" 126 127 #define SIN(s) ((struct sockaddr_in *)s) 128 #define SDL(s) ((struct sockaddr_dl *)s) 129 #define SRP(s) ((struct sockaddr_inarp *)s) 130 131 /* 132 * ARP trailer negotiation. Trailer protocol is not IP specific, 133 * but ARP request/response use IP addresses. 134 */ 135 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL 136 137 /* timer values */ 138 int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 139 int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 140 int arpt_down = 20; /* once declared down, don't send for 20 secs */ 141 #define rt_expire rt_rmx.rmx_expire 142 143 static void arprequest __P((struct ifnet *, 144 struct in_addr *, struct in_addr *, u_int8_t *)); 145 static void arptfree __P((struct llinfo_arp *)); 146 static void arptimer __P((void *)); 147 static struct llinfo_arp *arplookup __P((struct in_addr *, int, int)); 148 static void in_arpinput __P((struct mbuf *)); 149 150 #if NLOOP > 0 151 extern struct ifnet loif[NLOOP]; 152 #endif 153 LIST_HEAD(, llinfo_arp) llinfo_arp; 154 struct ifqueue arpintrq = {0, 0, 0, 50}; 155 int arp_inuse, arp_allocated, arp_intimer; 156 int arp_maxtries = 5; 157 int useloopback = 1; /* use loopback interface for local traffic */ 158 int arpinit_done = 0; 159 160 struct arpstat arpstat; 161 struct callout arptimer_ch; 162 163 164 /* revarp state */ 165 static struct in_addr myip, srv_ip; 166 static int myip_initialized = 0; 167 static int revarp_in_progress = 0; 168 static struct ifnet *myip_ifp = NULL; 169 170 #ifdef DDB 171 static void db_print_sa __P((struct sockaddr *)); 172 static void db_print_ifa __P((struct ifaddr *)); 173 static void db_print_llinfo __P((caddr_t)); 174 static int db_show_radix_node __P((struct radix_node *, void *)); 175 #endif 176 177 /* 178 * this should be elsewhere. 179 */ 180 181 static char * 182 lla_snprintf __P((u_int8_t *, int)); 183 184 static char * 185 lla_snprintf(adrp, len) 186 u_int8_t *adrp; 187 int len; 188 { 189 static char buf[16*3]; 190 static const char hexdigits[] = { 191 '0','1','2','3','4','5','6','7', 192 '8','9','a','b','c','d','e','f' 193 }; 194 195 int i; 196 char *p; 197 198 p = buf; 199 200 *p++ = hexdigits[(*adrp)>>4]; 201 *p++ = hexdigits[(*adrp++)&0xf]; 202 203 for (i=1; i<len && i<16; i++) { 204 *p++ = ':'; 205 *p++ = hexdigits[(*adrp)>>4]; 206 *p++ = hexdigits[(*adrp++)&0xf]; 207 } 208 209 *p = 0; 210 return buf; 211 } 212 213 struct protosw arpsw[] = { 214 { 0, 0, 0, 0, 215 0, 0, 0, 0, 216 0, 217 0, 0, 0, arp_drain, 218 } 219 }; 220 221 222 struct domain arpdomain = 223 { PF_ARP, "arp", 0, 0, 0, 224 arpsw, &arpsw[sizeof(arpsw)/sizeof(arpsw[0])] 225 }; 226 227 /* 228 * ARP table locking. 229 * 230 * to prevent lossage vs. the arp_drain routine (which may be called at 231 * any time, including in a device driver context), we do two things: 232 * 233 * 1) manipulation of la->la_hold is done at splimp() (for all of 234 * about two instructions). 235 * 236 * 2) manipulation of the arp table's linked list is done under the 237 * protection of the ARP_LOCK; if arp_drain() or arptimer is called 238 * while the arp table is locked, we punt and try again later. 239 */ 240 241 int arp_locked; 242 243 static __inline int arp_lock_try __P((int)); 244 static __inline void arp_unlock __P((void)); 245 246 static __inline int 247 arp_lock_try(int recurse) 248 { 249 int s; 250 251 s = splimp(); 252 if (!recurse && arp_locked) { 253 splx(s); 254 return (0); 255 } 256 arp_locked++; 257 splx(s); 258 return (1); 259 } 260 261 static __inline void 262 arp_unlock() 263 { 264 int s; 265 266 s = splimp(); 267 arp_locked--; 268 splx(s); 269 } 270 271 #ifdef DIAGNOSTIC 272 #define ARP_LOCK(recurse) \ 273 do { \ 274 if (arp_lock_try(recurse) == 0) { \ 275 printf("%s:%d: arp already locked\n", __FILE__, __LINE__); \ 276 panic("arp_lock"); \ 277 } \ 278 } while (0) 279 #define ARP_LOCK_CHECK() \ 280 do { \ 281 if (arp_locked == 0) { \ 282 printf("%s:%d: arp lock not held\n", __FILE__, __LINE__); \ 283 panic("arp lock check"); \ 284 } \ 285 } while (0) 286 #else 287 #define ARP_LOCK(x) (void) arp_lock_try(x) 288 #define ARP_LOCK_CHECK() /* nothing */ 289 #endif 290 291 #define ARP_UNLOCK() arp_unlock() 292 293 /* 294 * ARP protocol drain routine. Called when memory is in short supply. 295 * Called at splimp(); 296 */ 297 298 void 299 arp_drain() 300 { 301 struct llinfo_arp *la, *nla; 302 int count = 0; 303 struct mbuf *mold; 304 305 if (arp_lock_try(0) == 0) { 306 printf("arp_drain: locked; punting\n"); 307 return; 308 } 309 310 for (la = llinfo_arp.lh_first; la != 0; la = nla) { 311 nla = la->la_list.le_next; 312 313 mold = la->la_hold; 314 la->la_hold = 0; 315 316 if (mold) { 317 m_freem(mold); 318 count++; 319 } 320 } 321 ARP_UNLOCK(); 322 arpstat.as_dfrdropped += count; 323 } 324 325 326 /* 327 * Timeout routine. Age arp_tab entries periodically. 328 */ 329 /* ARGSUSED */ 330 static void 331 arptimer(arg) 332 void *arg; 333 { 334 int s; 335 struct llinfo_arp *la, *nla; 336 337 s = splsoftnet(); 338 339 if (arp_lock_try(0) == 0) { 340 /* get it later.. */ 341 splx(s); 342 return; 343 } 344 345 callout_reset(&arptimer_ch, arpt_prune * hz, arptimer, NULL); 346 for (la = llinfo_arp.lh_first; la != 0; la = nla) { 347 struct rtentry *rt = la->la_rt; 348 349 nla = la->la_list.le_next; 350 if (rt->rt_expire && rt->rt_expire <= time.tv_sec) 351 arptfree(la); /* timer has expired; clear */ 352 } 353 354 ARP_UNLOCK(); 355 356 splx(s); 357 } 358 359 /* 360 * Parallel to llc_rtrequest. 361 */ 362 void 363 arp_rtrequest(req, rt, sa) 364 int req; 365 struct rtentry *rt; 366 struct sockaddr *sa; 367 { 368 struct sockaddr *gate = rt->rt_gateway; 369 struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 370 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 371 size_t allocsize; 372 struct mbuf *mold; 373 int s; 374 375 if (!arpinit_done) { 376 arpinit_done = 1; 377 /* 378 * We generate expiration times from time.tv_sec 379 * so avoid accidently creating permanent routes. 380 */ 381 if (time.tv_sec == 0) { 382 time.tv_sec++; 383 } 384 callout_init(&arptimer_ch); 385 callout_reset(&arptimer_ch, hz, arptimer, NULL); 386 } 387 if (rt->rt_flags & RTF_GATEWAY) 388 return; 389 390 ARP_LOCK(1); /* we may already be locked here. */ 391 392 switch (req) { 393 394 case RTM_ADD: 395 /* 396 * XXX: If this is a manually added route to interface 397 * such as older version of routed or gated might provide, 398 * restore cloning bit. 399 */ 400 if ((rt->rt_flags & RTF_HOST) == 0 && 401 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 402 rt->rt_flags |= RTF_CLONING; 403 if (rt->rt_flags & RTF_CLONING) { 404 /* 405 * Case 1: This route should come from a route to iface. 406 */ 407 rt_setgate(rt, rt_key(rt), 408 (struct sockaddr *)&null_sdl); 409 gate = rt->rt_gateway; 410 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 411 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 412 /* 413 * Give this route an expiration time, even though 414 * it's a "permanent" route, so that routes cloned 415 * from it do not need their expiration time set. 416 */ 417 rt->rt_expire = time.tv_sec; 418 #if NFDDI > 0 419 if (rt->rt_ifp->if_type == IFT_FDDI 420 && (rt->rt_rmx.rmx_mtu > FDDIIPMTU 421 || (rt->rt_rmx.rmx_mtu == 0 422 && rt->rt_ifp->if_mtu > FDDIIPMTU))) { 423 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 424 } 425 #endif 426 #if NARC > 0 427 if (rt->rt_ifp->if_type == IFT_ARCNET) { 428 int arcipifmtu; 429 430 if (rt->rt_ifp->if_flags & IFF_LINK0) 431 arcipifmtu = arc_ipmtu; 432 else 433 arcipifmtu = ARCMTU; 434 435 if (rt->rt_rmx.rmx_mtu > arcipifmtu || 436 (rt->rt_rmx.rmx_mtu == 0 && 437 rt->rt_ifp->if_mtu > arcipifmtu)) 438 439 rt->rt_rmx.rmx_mtu = arcipifmtu; 440 } 441 #endif 442 break; 443 } 444 /* Announce a new entry if requested. */ 445 if (rt->rt_flags & RTF_ANNOUNCE) 446 arprequest(rt->rt_ifp, 447 &SIN(rt_key(rt))->sin_addr, 448 &SIN(rt_key(rt))->sin_addr, 449 (u_char *)LLADDR(SDL(gate))); 450 /*FALLTHROUGH*/ 451 case RTM_RESOLVE: 452 if (gate->sa_family != AF_LINK || 453 gate->sa_len < sizeof(null_sdl)) { 454 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 455 break; 456 } 457 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 458 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 459 if (la != 0) 460 break; /* This happens on a route change */ 461 /* 462 * Case 2: This route may come from cloning, or a manual route 463 * add with a LL address. 464 */ 465 switch (SDL(gate)->sdl_type) { 466 #if NTOKEN > 0 467 case IFT_ISO88025: 468 allocsize = sizeof(*la) + sizeof(struct token_rif); 469 break; 470 #endif /* NTOKEN > 0 */ 471 default: 472 allocsize = sizeof(*la); 473 } 474 R_Malloc(la, struct llinfo_arp *, allocsize); 475 rt->rt_llinfo = (caddr_t)la; 476 if (la == 0) { 477 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 478 break; 479 } 480 arp_inuse++, arp_allocated++; 481 Bzero(la, allocsize); 482 la->la_rt = rt; 483 rt->rt_flags |= RTF_LLINFO; 484 LIST_INSERT_HEAD(&llinfo_arp, la, la_list); 485 if (in_hosteq(SIN(rt_key(rt))->sin_addr, 486 (IA_SIN(rt->rt_ifa))->sin_addr)) { 487 /* 488 * This test used to be 489 * if (loif.if_flags & IFF_UP) 490 * It allowed local traffic to be forced through 491 * the hardware by configuring the loopback down. 492 * However, it causes problems during network 493 * configuration for boards that can't receive 494 * packets they send. It is now necessary to clear 495 * "useloopback" and remove the route to force 496 * traffic out to the hardware. 497 */ 498 rt->rt_expire = 0; 499 Bcopy(LLADDR(rt->rt_ifp->if_sadl), 500 LLADDR(SDL(gate)), 501 SDL(gate)->sdl_alen = 502 rt->rt_ifp->if_data.ifi_addrlen); 503 #if NLOOP > 0 504 if (useloopback) 505 rt->rt_ifp = &loif[0]; 506 #endif 507 } 508 break; 509 510 case RTM_DELETE: 511 if (la == 0) 512 break; 513 arp_inuse--; 514 LIST_REMOVE(la, la_list); 515 rt->rt_llinfo = 0; 516 rt->rt_flags &= ~RTF_LLINFO; 517 518 s = splimp(); 519 mold = la->la_hold; 520 la->la_hold = 0; 521 splx(s); 522 523 if (mold) 524 m_freem(mold); 525 526 Free((caddr_t)la); 527 } 528 ARP_UNLOCK(); 529 } 530 531 /* 532 * Broadcast an ARP request. Caller specifies: 533 * - arp header source ip address 534 * - arp header target ip address 535 * - arp header source ethernet address 536 */ 537 static void 538 arprequest(ifp, sip, tip, enaddr) 539 struct ifnet *ifp; 540 struct in_addr *sip, *tip; 541 u_int8_t *enaddr; 542 { 543 struct mbuf *m; 544 struct arphdr *ah; 545 struct sockaddr sa; 546 547 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 548 return; 549 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 550 2*ifp->if_data.ifi_addrlen; 551 m->m_pkthdr.len = m->m_len; 552 MH_ALIGN(m, m->m_len); 553 ah = mtod(m, struct arphdr *); 554 bzero((caddr_t)ah, m->m_len); 555 ah->ar_pro = htons(ETHERTYPE_IP); 556 ah->ar_hln = ifp->if_data.ifi_addrlen; /* hardware address length */ 557 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 558 ah->ar_op = htons(ARPOP_REQUEST); 559 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 560 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 561 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 562 sa.sa_family = AF_ARP; 563 sa.sa_len = 2; 564 m->m_flags |= M_BCAST; 565 arpstat.as_sndtotal++; 566 arpstat.as_sndrequest++; 567 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 568 } 569 570 /* 571 * Resolve an IP address into an ethernet address. If success, 572 * desten is filled in. If there is no entry in arptab, 573 * set one up and broadcast a request for the IP address. 574 * Hold onto this mbuf and resend it once the address 575 * is finally resolved. A return value of 1 indicates 576 * that desten has been filled in and the packet should be sent 577 * normally; a 0 return indicates that the packet has been 578 * taken over here, either now or for later transmission. 579 */ 580 int 581 arpresolve(ifp, rt, m, dst, desten) 582 struct ifnet *ifp; 583 struct rtentry *rt; 584 struct mbuf *m; 585 struct sockaddr *dst; 586 u_char *desten; 587 { 588 struct llinfo_arp *la; 589 struct sockaddr_dl *sdl; 590 struct mbuf *mold; 591 int s; 592 593 if (rt) 594 la = (struct llinfo_arp *)rt->rt_llinfo; 595 else { 596 if ((la = arplookup(&SIN(dst)->sin_addr, 1, 0)) != NULL) 597 rt = la->la_rt; 598 } 599 if (la == 0 || rt == 0) { 600 arpstat.as_allocfail++; 601 log(LOG_DEBUG, "arpresolve: can't allocate llinfo\n"); 602 m_freem(m); 603 return (0); 604 } 605 sdl = SDL(rt->rt_gateway); 606 /* 607 * Check the address family and length is valid, the address 608 * is resolved; otherwise, try to resolve. 609 */ 610 if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) && 611 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 612 bcopy(LLADDR(sdl), desten, 613 min(sdl->sdl_alen, ifp->if_data.ifi_addrlen)); 614 return 1; 615 } 616 /* 617 * There is an arptab entry, but no ethernet address 618 * response yet. Replace the held mbuf with this 619 * latest one. 620 */ 621 622 arpstat.as_dfrtotal++; 623 s = splimp(); 624 mold = la->la_hold; 625 la->la_hold = m; 626 splx(s); 627 628 if (mold) { 629 arpstat.as_dfrdropped++; 630 m_freem(mold); 631 } 632 633 /* 634 * Re-send the ARP request when appropriate. 635 */ 636 #ifdef DIAGNOSTIC 637 if (rt->rt_expire == 0) { 638 /* This should never happen. (Should it? -gwr) */ 639 printf("arpresolve: unresolved and rt_expire == 0\n"); 640 /* Set expiration time to now (expired). */ 641 rt->rt_expire = time.tv_sec; 642 } 643 #endif 644 if (rt->rt_expire) { 645 rt->rt_flags &= ~RTF_REJECT; 646 if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) { 647 rt->rt_expire = time.tv_sec; 648 if (la->la_asked++ < arp_maxtries) 649 arprequest(ifp, 650 &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 651 &SIN(dst)->sin_addr, 652 LLADDR(ifp->if_sadl)); 653 else { 654 rt->rt_flags |= RTF_REJECT; 655 rt->rt_expire += arpt_down; 656 la->la_asked = 0; 657 } 658 } 659 } 660 return (0); 661 } 662 663 /* 664 * Common length and type checks are done here, 665 * then the protocol-specific routine is called. 666 */ 667 void 668 arpintr() 669 { 670 struct mbuf *m; 671 struct arphdr *ar; 672 int s; 673 674 while (arpintrq.ifq_head) { 675 s = splimp(); 676 IF_DEQUEUE(&arpintrq, m); 677 splx(s); 678 if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 679 panic("arpintr"); 680 681 arpstat.as_rcvtotal++; 682 683 if (m->m_len >= sizeof(struct arphdr) && 684 (ar = mtod(m, struct arphdr *)) && 685 /* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */ 686 m->m_len >= 687 sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln)) 688 switch (ntohs(ar->ar_pro)) { 689 690 case ETHERTYPE_IP: 691 case ETHERTYPE_IPTRAILERS: 692 in_arpinput(m); 693 continue; 694 default: 695 arpstat.as_rcvbadproto++; 696 } 697 else 698 arpstat.as_rcvbadlen++; 699 m_freem(m); 700 } 701 } 702 703 /* 704 * ARP for Internet protocols on 10 Mb/s Ethernet. 705 * Algorithm is that given in RFC 826. 706 * In addition, a sanity check is performed on the sender 707 * protocol address, to catch impersonators. 708 * We no longer handle negotiations for use of trailer protocol: 709 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 710 * along with IP replies if we wanted trailers sent to us, 711 * and also sent them in response to IP replies. 712 * This allowed either end to announce the desire to receive 713 * trailer packets. 714 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 715 * but formerly didn't normally send requests. 716 */ 717 static void 718 in_arpinput(m) 719 struct mbuf *m; 720 { 721 struct arphdr *ah; 722 struct ifnet *ifp = m->m_pkthdr.rcvif; 723 struct llinfo_arp *la = 0; 724 struct rtentry *rt; 725 struct in_ifaddr *ia; 726 struct sockaddr_dl *sdl; 727 struct sockaddr sa; 728 struct in_addr isaddr, itaddr, myaddr; 729 int op; 730 struct mbuf *mold; 731 int s; 732 733 ah = mtod(m, struct arphdr *); 734 op = ntohs(ah->ar_op); 735 bcopy((caddr_t)ar_spa(ah), (caddr_t)&isaddr, sizeof (isaddr)); 736 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&itaddr, sizeof (itaddr)); 737 738 if (m->m_flags & (M_BCAST|M_MCAST)) 739 arpstat.as_rcvmcast++; 740 741 /* 742 * If the target IP address is zero, ignore the packet. 743 * This prevents the code below from tring to answer 744 * when we are using IP address zero (booting). 745 */ 746 if (in_nullhost(itaddr)) { 747 arpstat.as_rcvzerotpa++; 748 goto out; 749 } 750 751 /* 752 * If the source IP address is zero, this is most likely a 753 * confused host trying to use IP address zero. (Windoze?) 754 * XXX: Should we bother trying to reply to these? 755 */ 756 if (in_nullhost(isaddr)) { 757 arpstat.as_rcvzerospa++; 758 goto out; 759 } 760 761 /* 762 * Search for a matching interface address 763 * or any address on the interface to use 764 * as a dummy address in the rest of this function 765 */ 766 INADDR_TO_IA(itaddr, ia); 767 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif) 768 NEXT_IA_WITH_SAME_ADDR(ia); 769 770 if (ia == NULL) { 771 INADDR_TO_IA(isaddr, ia); 772 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif) 773 NEXT_IA_WITH_SAME_ADDR(ia); 774 775 if (ia == NULL) { 776 IFP_TO_IA(ifp, ia); 777 if (ia == NULL) { 778 arpstat.as_rcvnoint++; 779 goto out; 780 } 781 } 782 } 783 784 myaddr = ia->ia_addr.sin_addr; 785 786 if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl), 787 ifp->if_data.ifi_addrlen)) { 788 arpstat.as_rcvlocalsha++; 789 goto out; /* it's from me, ignore it. */ 790 } 791 792 if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)ifp->if_broadcastaddr, 793 ifp->if_data.ifi_addrlen)) { 794 arpstat.as_rcvbcastsha++; 795 log(LOG_ERR, 796 "%s: arp: link address is broadcast for IP address %s!\n", 797 ifp->if_xname, in_fmtaddr(isaddr)); 798 goto out; 799 } 800 801 if (in_hosteq(isaddr, myaddr)) { 802 arpstat.as_rcvlocalspa++; 803 log(LOG_ERR, 804 "duplicate IP address %s sent from link address %s\n", 805 in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln)); 806 itaddr = myaddr; 807 goto reply; 808 } 809 la = arplookup(&isaddr, in_hosteq(itaddr, myaddr), 0); 810 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 811 if (sdl->sdl_alen && 812 bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 813 if (rt->rt_flags & RTF_STATIC) { 814 arpstat.as_rcvoverperm++; 815 log(LOG_INFO, 816 "%s tried to overwrite permanent arp info" 817 " for %s\n", 818 lla_snprintf(ar_sha(ah), ah->ar_hln), 819 in_fmtaddr(isaddr)); 820 goto out; 821 } else if (rt->rt_ifp != ifp) { 822 arpstat.as_rcvoverint++; 823 log(LOG_INFO, 824 "%s on %s tried to overwrite " 825 "arp info for %s on %s\n", 826 lla_snprintf(ar_sha(ah), ah->ar_hln), 827 ifp->if_xname, in_fmtaddr(isaddr), 828 rt->rt_ifp->if_xname); 829 goto out; 830 } else { 831 arpstat.as_rcvover++; 832 log(LOG_INFO, 833 "arp info overwritten for %s by %s\n", 834 in_fmtaddr(isaddr), 835 lla_snprintf(ar_sha(ah), ah->ar_hln)); 836 } 837 } 838 /* 839 * sanity check for the address length. 840 * XXX this does not work for protocols with variable address 841 * length. -is 842 */ 843 if (sdl->sdl_alen && 844 sdl->sdl_alen != ah->ar_hln) { 845 arpstat.as_rcvlenchg++; 846 log(LOG_WARNING, 847 "arp from %s: new addr len %d, was %d", 848 in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen); 849 } 850 if (ifp->if_data.ifi_addrlen != ah->ar_hln) { 851 arpstat.as_rcvbadlen++; 852 log(LOG_WARNING, 853 "arp from %s: addr len: new %d, i/f %d (ignored)", 854 in_fmtaddr(isaddr), ah->ar_hln, 855 ifp->if_data.ifi_addrlen); 856 goto reply; 857 } 858 #if NTOKEN > 0 859 /* 860 * XXX uses m_data and assumes the complete answer including 861 * XXX token-ring headers is in the same buf 862 */ 863 if (ifp->if_type == IFT_ISO88025) { 864 struct token_header *trh; 865 866 trh = (struct token_header *)M_TRHSTART(m); 867 if (trh->token_shost[0] & TOKEN_RI_PRESENT) { 868 struct token_rif *rif; 869 size_t riflen; 870 871 rif = TOKEN_RIF(trh); 872 riflen = (ntohs(rif->tr_rcf) & 873 TOKEN_RCF_LEN_MASK) >> 8; 874 875 if (riflen > 2 && 876 riflen < sizeof(struct token_rif) && 877 (riflen & 1) == 0) { 878 rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION); 879 rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK); 880 bcopy(rif, TOKEN_RIF(la), riflen); 881 } 882 } 883 } 884 #endif /* NTOKEN > 0 */ 885 bcopy((caddr_t)ar_sha(ah), LLADDR(sdl), 886 sdl->sdl_alen = ah->ar_hln); 887 if (rt->rt_expire) 888 rt->rt_expire = time.tv_sec + arpt_keep; 889 rt->rt_flags &= ~RTF_REJECT; 890 la->la_asked = 0; 891 892 s = splimp(); 893 mold = la->la_hold; 894 la->la_hold = 0; 895 splx(s); 896 897 if (mold) { 898 arpstat.as_dfrsent++; 899 (*ifp->if_output)(ifp, mold, rt_key(rt), rt); 900 } 901 } 902 reply: 903 if (op != ARPOP_REQUEST) { 904 if (op == ARPOP_REPLY) 905 arpstat.as_rcvreply++; 906 out: 907 m_freem(m); 908 return; 909 } 910 arpstat.as_rcvrequest++; 911 if (in_hosteq(itaddr, myaddr)) { 912 /* I am the target */ 913 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln); 914 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln); 915 } else { 916 la = arplookup(&itaddr, 0, SIN_PROXY); 917 if (la == 0) 918 goto out; 919 rt = la->la_rt; 920 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln); 921 sdl = SDL(rt->rt_gateway); 922 bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln); 923 } 924 925 bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln); 926 bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln); 927 ah->ar_op = htons(ARPOP_REPLY); 928 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 929 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */ 930 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 931 m->m_pkthdr.len = m->m_len; 932 sa.sa_family = AF_ARP; 933 sa.sa_len = 2; 934 arpstat.as_sndtotal++; 935 arpstat.as_sndreply++; 936 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 937 return; 938 } 939 940 /* 941 * Free an arp entry. 942 */ 943 static void 944 arptfree(la) 945 struct llinfo_arp *la; 946 { 947 struct rtentry *rt = la->la_rt; 948 struct sockaddr_dl *sdl; 949 950 ARP_LOCK_CHECK(); 951 952 if (rt == 0) 953 panic("arptfree"); 954 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 955 sdl->sdl_family == AF_LINK) { 956 sdl->sdl_alen = 0; 957 la->la_asked = 0; 958 rt->rt_flags &= ~RTF_REJECT; 959 return; 960 } 961 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 962 0, (struct rtentry **)0); 963 } 964 965 /* 966 * Lookup or enter a new address in arptab. 967 */ 968 static struct llinfo_arp * 969 arplookup(addr, create, proxy) 970 struct in_addr *addr; 971 int create, proxy; 972 { 973 struct rtentry *rt; 974 static struct sockaddr_inarp sin; 975 const char *why = 0; 976 977 sin.sin_len = sizeof(sin); 978 sin.sin_family = AF_INET; 979 sin.sin_addr = *addr; 980 sin.sin_other = proxy ? SIN_PROXY : 0; 981 rt = rtalloc1(sintosa(&sin), create); 982 if (rt == 0) 983 return (0); 984 rt->rt_refcnt--; 985 986 if (rt->rt_flags & RTF_GATEWAY) 987 why = "host is not on local network"; 988 else if ((rt->rt_flags & RTF_LLINFO) == 0) { 989 arpstat.as_allocfail++; 990 why = "could not allocate llinfo"; 991 } else if (rt->rt_gateway->sa_family != AF_LINK) 992 why = "gateway route is not ours"; 993 else 994 return ((struct llinfo_arp *)rt->rt_llinfo); 995 996 if (create) 997 log(LOG_DEBUG, "arplookup: unable to enter address" 998 " for %s (%s)\n", 999 in_fmtaddr(*addr), why); 1000 return (0); 1001 } 1002 1003 int 1004 arpioctl(cmd, data) 1005 u_long cmd; 1006 caddr_t data; 1007 { 1008 1009 return (EOPNOTSUPP); 1010 } 1011 1012 void 1013 arp_ifinit(ifp, ifa) 1014 struct ifnet *ifp; 1015 struct ifaddr *ifa; 1016 { 1017 struct in_addr *ip; 1018 1019 /* 1020 * Warn the user if another station has this IP address, 1021 * but only if the interface IP address is not zero. 1022 */ 1023 ip = &IA_SIN(ifa)->sin_addr; 1024 if (!in_nullhost(*ip)) 1025 arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl)); 1026 1027 ifa->ifa_rtrequest = arp_rtrequest; 1028 ifa->ifa_flags |= RTF_CLONING; 1029 } 1030 1031 /* 1032 * Called from 10 Mb/s Ethernet interrupt handlers 1033 * when ether packet type ETHERTYPE_REVARP 1034 * is received. Common length and type checks are done here, 1035 * then the protocol-specific routine is called. 1036 */ 1037 void 1038 revarpinput(m) 1039 struct mbuf *m; 1040 { 1041 struct arphdr *ar; 1042 1043 if (m->m_len < sizeof(struct arphdr)) 1044 goto out; 1045 ar = mtod(m, struct arphdr *); 1046 #if 0 /* XXX I don't think we need this... and it will prevent other LL */ 1047 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) 1048 goto out; 1049 #endif 1050 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln)) 1051 goto out; 1052 switch (ntohs(ar->ar_pro)) { 1053 1054 case ETHERTYPE_IP: 1055 case ETHERTYPE_IPTRAILERS: 1056 in_revarpinput(m); 1057 return; 1058 1059 default: 1060 break; 1061 } 1062 out: 1063 m_freem(m); 1064 } 1065 1066 /* 1067 * RARP for Internet protocols on 10 Mb/s Ethernet. 1068 * Algorithm is that given in RFC 903. 1069 * We are only using for bootstrap purposes to get an ip address for one of 1070 * our interfaces. Thus we support no user-interface. 1071 * 1072 * Since the contents of the RARP reply are specific to the interface that 1073 * sent the request, this code must ensure that they are properly associated. 1074 * 1075 * Note: also supports ARP via RARP packets, per the RFC. 1076 */ 1077 void 1078 in_revarpinput(m) 1079 struct mbuf *m; 1080 { 1081 struct ifnet *ifp; 1082 struct arphdr *ah; 1083 int op; 1084 1085 ah = mtod(m, struct arphdr *); 1086 op = ntohs(ah->ar_op); 1087 switch (op) { 1088 case ARPOP_REQUEST: 1089 case ARPOP_REPLY: /* per RFC */ 1090 in_arpinput(m); 1091 return; 1092 case ARPOP_REVREPLY: 1093 break; 1094 case ARPOP_REVREQUEST: /* handled by rarpd(8) */ 1095 default: 1096 goto out; 1097 } 1098 if (!revarp_in_progress) 1099 goto out; 1100 ifp = m->m_pkthdr.rcvif; 1101 if (ifp != myip_ifp) /* !same interface */ 1102 goto out; 1103 if (myip_initialized) 1104 goto wake; 1105 if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen)) 1106 goto out; 1107 bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip)); 1108 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip)); 1109 myip_initialized = 1; 1110 wake: /* Do wakeup every time in case it was missed. */ 1111 wakeup((caddr_t)&myip); 1112 1113 out: 1114 m_freem(m); 1115 } 1116 1117 /* 1118 * Send a RARP request for the ip address of the specified interface. 1119 * The request should be RFC 903-compliant. 1120 */ 1121 void 1122 revarprequest(ifp) 1123 struct ifnet *ifp; 1124 { 1125 struct sockaddr sa; 1126 struct mbuf *m; 1127 struct arphdr *ah; 1128 1129 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 1130 return; 1131 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 1132 2*ifp->if_data.ifi_addrlen; 1133 m->m_pkthdr.len = m->m_len; 1134 MH_ALIGN(m, m->m_len); 1135 ah = mtod(m, struct arphdr *); 1136 bzero((caddr_t)ah, m->m_len); 1137 ah->ar_pro = htons(ETHERTYPE_IP); 1138 ah->ar_hln = ifp->if_data.ifi_addrlen; /* hardware address length */ 1139 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 1140 ah->ar_op = htons(ARPOP_REVREQUEST); 1141 1142 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln); 1143 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln); 1144 1145 sa.sa_family = AF_ARP; 1146 sa.sa_len = 2; 1147 m->m_flags |= M_BCAST; 1148 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 1149 1150 } 1151 1152 /* 1153 * RARP for the ip address of the specified interface, but also 1154 * save the ip address of the server that sent the answer. 1155 * Timeout if no response is received. 1156 */ 1157 int 1158 revarpwhoarewe(ifp, serv_in, clnt_in) 1159 struct ifnet *ifp; 1160 struct in_addr *serv_in; 1161 struct in_addr *clnt_in; 1162 { 1163 int result, count = 20; 1164 1165 myip_initialized = 0; 1166 myip_ifp = ifp; 1167 1168 revarp_in_progress = 1; 1169 while (count--) { 1170 revarprequest(ifp); 1171 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2); 1172 if (result != EWOULDBLOCK) 1173 break; 1174 } 1175 revarp_in_progress = 0; 1176 1177 if (!myip_initialized) 1178 return ENETUNREACH; 1179 1180 bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in)); 1181 bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in)); 1182 return 0; 1183 } 1184 1185 1186 1187 #ifdef DDB 1188 1189 #include <machine/db_machdep.h> 1190 #include <ddb/db_interface.h> 1191 #include <ddb/db_output.h> 1192 static void 1193 db_print_sa(sa) 1194 struct sockaddr *sa; 1195 { 1196 int len; 1197 u_char *p; 1198 1199 if (sa == 0) { 1200 db_printf("[NULL]"); 1201 return; 1202 } 1203 1204 p = (u_char*)sa; 1205 len = sa->sa_len; 1206 db_printf("["); 1207 while (len > 0) { 1208 db_printf("%d", *p); 1209 p++; len--; 1210 if (len) db_printf(","); 1211 } 1212 db_printf("]\n"); 1213 } 1214 static void 1215 db_print_ifa(ifa) 1216 struct ifaddr *ifa; 1217 { 1218 if (ifa == 0) 1219 return; 1220 db_printf(" ifa_addr="); 1221 db_print_sa(ifa->ifa_addr); 1222 db_printf(" ifa_dsta="); 1223 db_print_sa(ifa->ifa_dstaddr); 1224 db_printf(" ifa_mask="); 1225 db_print_sa(ifa->ifa_netmask); 1226 db_printf(" flags=0x%x,refcnt=%d,metric=%d\n", 1227 ifa->ifa_flags, 1228 ifa->ifa_refcnt, 1229 ifa->ifa_metric); 1230 } 1231 static void 1232 db_print_llinfo(li) 1233 caddr_t li; 1234 { 1235 struct llinfo_arp *la; 1236 1237 if (li == 0) 1238 return; 1239 la = (struct llinfo_arp *)li; 1240 db_printf(" la_rt=%p la_hold=%p, la_asked=0x%lx\n", 1241 la->la_rt, la->la_hold, la->la_asked); 1242 } 1243 /* 1244 * Function to pass to rn_walktree(). 1245 * Return non-zero error to abort walk. 1246 */ 1247 static int 1248 db_show_radix_node(rn, w) 1249 struct radix_node *rn; 1250 void *w; 1251 { 1252 struct rtentry *rt = (struct rtentry *)rn; 1253 1254 db_printf("rtentry=%p", rt); 1255 1256 db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n", 1257 rt->rt_flags, rt->rt_refcnt, 1258 rt->rt_use, rt->rt_expire); 1259 1260 db_printf(" key="); db_print_sa(rt_key(rt)); 1261 db_printf(" mask="); db_print_sa(rt_mask(rt)); 1262 db_printf(" gw="); db_print_sa(rt->rt_gateway); 1263 1264 db_printf(" ifp=%p ", rt->rt_ifp); 1265 if (rt->rt_ifp) 1266 db_printf("(%s)", rt->rt_ifp->if_xname); 1267 else 1268 db_printf("(NULL)"); 1269 1270 db_printf(" ifa=%p\n", rt->rt_ifa); 1271 db_print_ifa(rt->rt_ifa); 1272 1273 db_printf(" genmask="); db_print_sa(rt->rt_genmask); 1274 1275 db_printf(" gwroute=%p llinfo=%p\n", 1276 rt->rt_gwroute, rt->rt_llinfo); 1277 db_print_llinfo(rt->rt_llinfo); 1278 1279 return (0); 1280 } 1281 /* 1282 * Function to print all the route trees. 1283 * Use this from ddb: "show arptab" 1284 */ 1285 void 1286 db_show_arptab(addr, have_addr, count, modif) 1287 db_expr_t addr; 1288 int have_addr; 1289 db_expr_t count; 1290 char * modif; 1291 { 1292 struct radix_node_head *rnh; 1293 rnh = rt_tables[AF_INET]; 1294 db_printf("Route tree for AF_INET\n"); 1295 if (rnh == NULL) { 1296 db_printf(" (not initialized)\n"); 1297 return; 1298 } 1299 rn_walktree(rnh, db_show_radix_node, NULL); 1300 return; 1301 } 1302 #endif 1303 #endif /* INET */ 1304 1305