1 /* $OpenBSD: if_ether.c,v 1.48 2003/06/02 23:28:13 millert Exp $ */ 2 /* $NetBSD: if_ether.c,v 1.31 1996/05/11 12:59:58 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 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 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 33 */ 34 35 /* 36 * Ethernet address resolution protocol. 37 * TODO: 38 * add "inuse/lock" bit (or ref. count) along with valid bit 39 */ 40 41 #ifdef INET 42 43 #include "bridge.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/mbuf.h> 48 #include <sys/socket.h> 49 #include <sys/kernel.h> 50 #include <sys/syslog.h> 51 #include <sys/proc.h> 52 53 #include <net/if.h> 54 #include <net/if_dl.h> 55 #include <net/route.h> 56 #include <net/if_fddi.h> 57 #include <net/if_types.h> 58 59 #include <netinet/in.h> 60 #include <netinet/in_var.h> 61 #include <netinet/if_ether.h> 62 63 #define SIN(s) ((struct sockaddr_in *)s) 64 #define SDL(s) ((struct sockaddr_dl *)s) 65 #define SRP(s) ((struct sockaddr_inarp *)s) 66 67 /* 68 * ARP trailer negotiation. Trailer protocol is not IP specific, 69 * but ARP request/response use IP addresses. 70 */ 71 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL 72 73 /* timer values */ 74 int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 75 int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 76 int arpt_down = 20; /* once declared down, don't send for 20 secs */ 77 #define rt_expire rt_rmx.rmx_expire 78 79 void arptfree(struct llinfo_arp *); 80 void arptimer(void *); 81 struct llinfo_arp *arplookup(u_int32_t, int, int); 82 void in_arpinput(struct mbuf *); 83 84 LIST_HEAD(, llinfo_arp) llinfo_arp; 85 struct ifqueue arpintrq = {0, 0, 0, 50}; 86 int arp_inuse, arp_allocated, arp_intimer; 87 int arp_maxtries = 5; 88 int useloopback = 1; /* use loopback interface for local traffic */ 89 int arpinit_done = 0; 90 91 /* revarp state */ 92 static struct in_addr myip, srv_ip; 93 static int myip_initialized = 0; 94 static int revarp_in_progress = 0; 95 struct ifnet *myip_ifp = NULL; 96 97 #ifdef DDB 98 #include <uvm/uvm_extern.h> 99 100 void db_print_sa(struct sockaddr *); 101 void db_print_ifa(struct ifaddr *); 102 void db_print_llinfo(caddr_t); 103 int db_show_radix_node(struct radix_node *, void *); 104 #endif 105 106 /* 107 * Timeout routine. Age arp_tab entries periodically. 108 */ 109 /* ARGSUSED */ 110 void 111 arptimer(arg) 112 void *arg; 113 { 114 struct timeout *to = (struct timeout *)arg; 115 int s; 116 struct llinfo_arp *la, *nla; 117 118 s = splsoftnet(); 119 timeout_add(to, arpt_prune * hz); 120 for (la = LIST_FIRST(&llinfo_arp); la != LIST_END(&llinfo_arp); 121 la = nla) { 122 register struct rtentry *rt = la->la_rt; 123 124 nla = LIST_NEXT(la, la_list); 125 if (rt->rt_expire && rt->rt_expire <= time.tv_sec) 126 arptfree(la); /* timer has expired; clear */ 127 } 128 splx(s); 129 } 130 131 /* 132 * Parallel to llc_rtrequest. 133 */ 134 void 135 arp_rtrequest(req, rt, info) 136 int req; 137 register struct rtentry *rt; 138 struct rt_addrinfo *info; 139 { 140 register struct sockaddr *gate = rt->rt_gateway; 141 register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 142 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 143 struct in_ifaddr *ia; 144 struct ifaddr *ifa; 145 146 if (!arpinit_done) { 147 static struct timeout arptimer_to; 148 149 arpinit_done = 1; 150 /* 151 * We generate expiration times from time.tv_sec 152 * so avoid accidently creating permanent routes. 153 */ 154 if (time.tv_sec == 0) { 155 time.tv_sec++; 156 } 157 158 timeout_set(&arptimer_to, arptimer, &arptimer_to); 159 timeout_add(&arptimer_to, hz); 160 } 161 162 if (rt->rt_flags & RTF_GATEWAY) { 163 if (req != RTM_ADD) 164 return; 165 166 /* 167 * linklayers with particular link MTU limitation. it is a bit 168 * awkward to have FDDI handling here, we should split ARP from 169 * netinet/if_ether.c like NetBSD does. 170 */ 171 switch (rt->rt_ifp->if_type) { 172 case IFT_FDDI: 173 if (rt->rt_ifp->if_mtu > FDDIIPMTU) 174 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 175 break; 176 } 177 178 return; 179 } 180 181 switch (req) { 182 183 case RTM_ADD: 184 /* 185 * XXX: If this is a manually added route to interface 186 * such as older version of routed or gated might provide, 187 * restore cloning bit. 188 */ 189 if ((rt->rt_flags & RTF_HOST) == 0 && 190 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 191 rt->rt_flags |= RTF_CLONING; 192 if (rt->rt_flags & RTF_CLONING) { 193 /* 194 * Case 1: This route should come from a route to iface. 195 */ 196 rt_setgate(rt, rt_key(rt), 197 (struct sockaddr *)&null_sdl); 198 gate = rt->rt_gateway; 199 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 200 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 201 /* 202 * Give this route an expiration time, even though 203 * it's a "permanent" route, so that routes cloned 204 * from it do not need their expiration time set. 205 */ 206 rt->rt_expire = time.tv_sec; 207 /* 208 * linklayers with particular link MTU limitation. 209 */ 210 switch (rt->rt_ifp->if_type) { 211 case IFT_FDDI: 212 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 && 213 (rt->rt_rmx.rmx_mtu > FDDIIPMTU || 214 (rt->rt_rmx.rmx_mtu == 0 && 215 rt->rt_ifp->if_mtu > FDDIIPMTU))) 216 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 217 break; 218 } 219 break; 220 } 221 /* Announce a new entry if requested. */ 222 if (rt->rt_flags & RTF_ANNOUNCE) 223 arprequest(rt->rt_ifp, 224 &SIN(rt_key(rt))->sin_addr.s_addr, 225 &SIN(rt_key(rt))->sin_addr.s_addr, 226 (u_char *)LLADDR(SDL(gate))); 227 /*FALLTHROUGH*/ 228 case RTM_RESOLVE: 229 if (gate->sa_family != AF_LINK || 230 gate->sa_len < sizeof(null_sdl)) { 231 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 232 break; 233 } 234 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 235 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 236 if (la != 0) 237 break; /* This happens on a route change */ 238 /* 239 * Case 2: This route may come from cloning, or a manual route 240 * add with a LL address. 241 */ 242 R_Malloc(la, struct llinfo_arp *, sizeof(*la)); 243 rt->rt_llinfo = (caddr_t)la; 244 if (la == 0) { 245 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 246 break; 247 } 248 arp_inuse++, arp_allocated++; 249 Bzero(la, sizeof(*la)); 250 la->la_rt = rt; 251 rt->rt_flags |= RTF_LLINFO; 252 LIST_INSERT_HEAD(&llinfo_arp, la, la_list); 253 254 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 255 if (ia->ia_ifp == rt->rt_ifp && 256 SIN(rt_key(rt))->sin_addr.s_addr == 257 (IA_SIN(ia))->sin_addr.s_addr) 258 break; 259 } 260 if (ia) { 261 /* 262 * This test used to be 263 * if (lo0ifp->if_flags & IFF_UP) 264 * It allowed local traffic to be forced through 265 * the hardware by configuring the loopback down. 266 * However, it causes problems during network 267 * configuration for boards that can't receive 268 * packets they send. It is now necessary to clear 269 * "useloopback" and remove the route to force 270 * traffic out to the hardware. 271 * 272 * In 4.4BSD, the above "if" statement checked 273 * rt->rt_ifa against rt_key(rt). It was changed 274 * to the current form so that we can provide a 275 * better support for multiple IPv4 addresses on a 276 * interface. 277 */ 278 rt->rt_expire = 0; 279 Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr, 280 LLADDR(SDL(gate)), 281 SDL(gate)->sdl_alen = ETHER_ADDR_LEN); 282 if (useloopback) 283 rt->rt_ifp = lo0ifp; 284 /* 285 * make sure to set rt->rt_ifa to the interface 286 * address we are using, otherwise we will have trouble 287 * with source address selection. 288 */ 289 ifa = &ia->ia_ifa; 290 if (ifa != rt->rt_ifa) { 291 IFAFREE(rt->rt_ifa); 292 ifa->ifa_refcnt++; 293 rt->rt_ifa = ifa; 294 } 295 } 296 break; 297 298 case RTM_DELETE: 299 if (la == 0) 300 break; 301 arp_inuse--; 302 LIST_REMOVE(la, la_list); 303 rt->rt_llinfo = 0; 304 rt->rt_flags &= ~RTF_LLINFO; 305 if (la->la_hold) 306 m_freem(la->la_hold); 307 Free((caddr_t)la); 308 } 309 } 310 311 /* 312 * Broadcast an ARP request. Caller specifies: 313 * - arp header source ip address 314 * - arp header target ip address 315 * - arp header source ethernet address 316 */ 317 void 318 arprequest(ifp, sip, tip, enaddr) 319 register struct ifnet *ifp; 320 register u_int32_t *sip, *tip; 321 register u_int8_t *enaddr; 322 { 323 register struct mbuf *m; 324 register struct ether_header *eh; 325 register struct ether_arp *ea; 326 struct sockaddr sa; 327 328 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 329 return; 330 m->m_len = sizeof(*ea); 331 m->m_pkthdr.len = sizeof(*ea); 332 MH_ALIGN(m, sizeof(*ea)); 333 ea = mtod(m, struct ether_arp *); 334 eh = (struct ether_header *)sa.sa_data; 335 bzero((caddr_t)ea, sizeof (*ea)); 336 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 337 sizeof(eh->ether_dhost)); 338 eh->ether_type = htons(ETHERTYPE_ARP); /* if_output will not swap */ 339 ea->arp_hrd = htons(ARPHRD_ETHER); 340 ea->arp_pro = htons(ETHERTYPE_IP); 341 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 342 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 343 ea->arp_op = htons(ARPOP_REQUEST); 344 bcopy((caddr_t)enaddr, (caddr_t)eh->ether_shost, 345 sizeof(eh->ether_shost)); 346 bcopy((caddr_t)enaddr, (caddr_t)ea->arp_sha, sizeof(ea->arp_sha)); 347 bcopy((caddr_t)sip, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa)); 348 bcopy((caddr_t)tip, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa)); 349 sa.sa_family = AF_UNSPEC; 350 sa.sa_len = sizeof(sa); 351 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 352 } 353 354 /* 355 * Resolve an IP address into an ethernet address. If success, 356 * desten is filled in. If there is no entry in arptab, 357 * set one up and broadcast a request for the IP address. 358 * Hold onto this mbuf and resend it once the address 359 * is finally resolved. A return value of 1 indicates 360 * that desten has been filled in and the packet should be sent 361 * normally; a 0 return indicates that the packet has been 362 * taken over here, either now or for later transmission. 363 */ 364 int 365 arpresolve(ac, rt, m, dst, desten) 366 register struct arpcom *ac; 367 register struct rtentry *rt; 368 struct mbuf *m; 369 register struct sockaddr *dst; 370 register u_char *desten; 371 { 372 register struct llinfo_arp *la; 373 struct sockaddr_dl *sdl; 374 375 if (m->m_flags & M_BCAST) { /* broadcast */ 376 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten, 377 sizeof(etherbroadcastaddr)); 378 return (1); 379 } 380 if (m->m_flags & M_MCAST) { /* multicast */ 381 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 382 return (1); 383 } 384 if (rt) 385 la = (struct llinfo_arp *)rt->rt_llinfo; 386 else { 387 if ((la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0)) != NULL) 388 rt = la->la_rt; 389 } 390 if (la == 0 || rt == 0) { 391 log(LOG_DEBUG, "arpresolve: can't allocate llinfo\n"); 392 m_freem(m); 393 return (0); 394 } 395 sdl = SDL(rt->rt_gateway); 396 /* 397 * Check the address family and length is valid, the address 398 * is resolved; otherwise, try to resolve. 399 */ 400 if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) && 401 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 402 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 403 return 1; 404 } 405 if (((struct ifnet *)ac)->if_flags & IFF_NOARP) 406 return 0; 407 408 /* 409 * There is an arptab entry, but no ethernet address 410 * response yet. Replace the held mbuf with this 411 * latest one. 412 */ 413 if (la->la_hold) 414 m_freem(la->la_hold); 415 la->la_hold = m; 416 /* 417 * Re-send the ARP request when appropriate. 418 */ 419 #ifdef DIAGNOSTIC 420 if (rt->rt_expire == 0) { 421 /* This should never happen. (Should it? -gwr) */ 422 printf("arpresolve: unresolved and rt_expire == 0\n"); 423 /* Set expiration time to now (expired). */ 424 rt->rt_expire = time.tv_sec; 425 } 426 #endif 427 if (rt->rt_expire) { 428 rt->rt_flags &= ~RTF_REJECT; 429 if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) { 430 rt->rt_expire = time.tv_sec; 431 if (la->la_asked++ < arp_maxtries) 432 arprequest(&ac->ac_if, 433 &(SIN(rt->rt_ifa->ifa_addr)->sin_addr.s_addr), 434 &(SIN(dst)->sin_addr.s_addr), 435 ac->ac_enaddr); 436 else { 437 rt->rt_flags |= RTF_REJECT; 438 rt->rt_expire += arpt_down; 439 la->la_asked = 0; 440 } 441 } 442 } 443 return (0); 444 } 445 446 /* 447 * Common length and type checks are done here, 448 * then the protocol-specific routine is called. 449 */ 450 void 451 arpintr() 452 { 453 register struct mbuf *m; 454 register struct arphdr *ar; 455 int s, len; 456 457 while (arpintrq.ifq_head) { 458 s = splimp(); 459 IF_DEQUEUE(&arpintrq, m); 460 splx(s); 461 if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 462 panic("arpintr"); 463 464 len = sizeof(struct arphdr); 465 if (m->m_len < len && (m = m_pullup(m, len)) == NULL) 466 continue; 467 468 ar = mtod(m, struct arphdr *); 469 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) { 470 m_freem(m); 471 continue; 472 } 473 474 len += 2 * (ar->ar_hln + ar->ar_pln); 475 if (m->m_len < len && (m = m_pullup(m, len)) == NULL) 476 continue; 477 478 switch (ntohs(ar->ar_pro)) { 479 case ETHERTYPE_IP: 480 case ETHERTYPE_IPTRAILERS: 481 in_arpinput(m); 482 continue; 483 } 484 m_freem(m); 485 } 486 } 487 488 /* 489 * ARP for Internet protocols on Ethernet. 490 * Algorithm is that given in RFC 826. 491 * In addition, a sanity check is performed on the sender 492 * protocol address, to catch impersonators. 493 * We no longer handle negotiations for use of trailer protocol: 494 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 495 * along with IP replies if we wanted trailers sent to us, 496 * and also sent them in response to IP replies. 497 * This allowed either end to announce the desire to receive 498 * trailer packets. 499 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 500 * but formerly didn't normally send requests. 501 */ 502 void 503 in_arpinput(m) 504 struct mbuf *m; 505 { 506 register struct ether_arp *ea; 507 register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; 508 struct ether_header *eh; 509 register struct llinfo_arp *la = 0; 510 register struct rtentry *rt; 511 struct in_ifaddr *ia; 512 #if NBRIDGE > 0 513 struct in_ifaddr *bridge_ia = NULL; 514 #endif 515 struct sockaddr_dl *sdl; 516 struct sockaddr sa; 517 struct in_addr isaddr, itaddr, myaddr; 518 int op; 519 520 ea = mtod(m, struct ether_arp *); 521 op = ntohs(ea->arp_op); 522 if ((op != ARPOP_REQUEST) && (op != ARPOP_REPLY)) 523 goto out; 524 #if notyet 525 if ((op == ARPOP_REPLY) && (m->m_flags & (M_BCAST|M_MCAST))) { 526 log(LOG_ERR, 527 "arp: received reply to broadcast or multicast address\n"); 528 goto out; 529 } 530 #endif 531 532 bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof(itaddr)); 533 bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof(isaddr)); 534 535 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 536 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr) 537 continue; 538 539 if (ia->ia_ifp == m->m_pkthdr.rcvif) 540 break; 541 #if NBRIDGE > 0 542 /* 543 * If the interface we received the packet on 544 * is part of a bridge, check to see if we need 545 * to "bridge" the packet to ourselves at this 546 * layer. Note we still prefer a perfect match, 547 * but allow this weaker match if necessary. 548 */ 549 if (m->m_pkthdr.rcvif->if_bridge != NULL && 550 m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge) 551 bridge_ia = ia; 552 #endif 553 } 554 555 #if NBRIDGE > 0 556 if (ia == NULL && bridge_ia != NULL) { 557 ia = bridge_ia; 558 ac = (struct arpcom *)bridge_ia->ia_ifp; 559 } 560 #endif 561 562 if (ia == NULL) { 563 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 564 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr) 565 continue; 566 if (ia->ia_ifp == m->m_pkthdr.rcvif) 567 break; 568 } 569 } 570 571 if (ia == NULL) { 572 struct ifaddr *ifa; 573 574 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) { 575 if (ifa->ifa_addr->sa_family == AF_INET) 576 break; 577 } 578 if (ifa) 579 ia = (struct in_ifaddr *)ifa; 580 } 581 582 if (ia == NULL) 583 goto out; 584 585 myaddr = ia->ia_addr.sin_addr; 586 587 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 588 sizeof (ea->arp_sha))) 589 goto out; /* it's from me, ignore it. */ 590 if (ETHER_IS_MULTICAST (&ea->arp_sha[0])) { 591 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 592 sizeof (ea->arp_sha))) 593 log(LOG_ERR, 594 "arp: ether address is broadcast for IP address %s!\n", 595 inet_ntoa(isaddr)); 596 else 597 log(LOG_ERR, 598 "arp: ether address is multicast for IP address %s!\n", 599 inet_ntoa(isaddr)); 600 goto out; 601 } 602 if (myaddr.s_addr && isaddr.s_addr == myaddr.s_addr) { 603 log(LOG_ERR, 604 "duplicate IP address %s sent from ethernet address %s\n", 605 inet_ntoa(isaddr), ether_sprintf(ea->arp_sha)); 606 itaddr = myaddr; 607 goto reply; 608 } 609 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 610 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 611 if (sdl->sdl_alen) { 612 if (bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) { 613 if (rt->rt_flags & RTF_PERMANENT_ARP) { 614 log(LOG_WARNING, 615 "arp: attempt to overwrite permanent " 616 "entry for %s by %s on %s\n", 617 inet_ntoa(isaddr), 618 ether_sprintf(ea->arp_sha), 619 ac->ac_if.if_xname); 620 goto out; 621 } else if (rt->rt_ifp != &ac->ac_if) { 622 log(LOG_WARNING, 623 "arp: attempt to overwrite entry for %s " 624 "on %s by %s on %s\n", 625 inet_ntoa(isaddr), rt->rt_ifp->if_xname, 626 ether_sprintf(ea->arp_sha), 627 ac->ac_if.if_xname); 628 goto out; 629 } else { 630 log(LOG_INFO, 631 "arp info overwritten for %s by %s on %s\n", 632 inet_ntoa(isaddr), 633 ether_sprintf(ea->arp_sha), 634 ac->ac_if.if_xname); 635 rt->rt_expire = 1; /* no longer static */ 636 } 637 } 638 } else if (rt->rt_ifp != &ac->ac_if && !(ac->ac_if.if_bridge && 639 (rt->rt_ifp->if_bridge == ac->ac_if.if_bridge))) { 640 log(LOG_WARNING, 641 "arp: attempt to add entry for %s " 642 "on %s by %s on %s\n", 643 inet_ntoa(isaddr), rt->rt_ifp->if_xname, 644 ether_sprintf(ea->arp_sha), 645 ac->ac_if.if_xname); 646 goto out; 647 } 648 bcopy((caddr_t)ea->arp_sha, LLADDR(sdl), 649 sdl->sdl_alen = sizeof(ea->arp_sha)); 650 if (rt->rt_expire) 651 rt->rt_expire = time.tv_sec + arpt_keep; 652 rt->rt_flags &= ~RTF_REJECT; 653 la->la_asked = 0; 654 if (la->la_hold) { 655 (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 656 rt_key(rt), rt); 657 la->la_hold = 0; 658 } 659 } 660 reply: 661 if (op != ARPOP_REQUEST) { 662 out: 663 m_freem(m); 664 return; 665 } 666 if (itaddr.s_addr == myaddr.s_addr) { 667 /* I am the target */ 668 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 669 sizeof(ea->arp_sha)); 670 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, 671 sizeof(ea->arp_sha)); 672 } else { 673 la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 674 if (la == 0) 675 goto out; 676 rt = la->la_rt; 677 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 678 sizeof(ea->arp_sha)); 679 sdl = SDL(rt->rt_gateway); 680 bcopy(LLADDR(sdl), (caddr_t)ea->arp_sha, sizeof(ea->arp_sha)); 681 } 682 683 bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, sizeof(ea->arp_spa)); 684 bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa)); 685 ea->arp_op = htons(ARPOP_REPLY); 686 ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 687 eh = (struct ether_header *)sa.sa_data; 688 bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost, 689 sizeof(eh->ether_dhost)); 690 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 691 sizeof(eh->ether_shost)); 692 eh->ether_type = htons(ETHERTYPE_ARP); 693 sa.sa_family = AF_UNSPEC; 694 sa.sa_len = sizeof(sa); 695 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 696 return; 697 } 698 699 /* 700 * Free an arp entry. 701 */ 702 void 703 arptfree(la) 704 register struct llinfo_arp *la; 705 { 706 register struct rtentry *rt = la->la_rt; 707 register struct sockaddr_dl *sdl; 708 709 if (rt == 0) 710 panic("arptfree"); 711 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 712 sdl->sdl_family == AF_LINK) { 713 sdl->sdl_alen = 0; 714 la->la_asked = 0; 715 rt->rt_flags &= ~RTF_REJECT; 716 return; 717 } 718 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 719 0, (struct rtentry **)0); 720 } 721 722 /* 723 * Lookup or enter a new address in arptab. 724 */ 725 struct llinfo_arp * 726 arplookup(addr, create, proxy) 727 u_int32_t addr; 728 int create, proxy; 729 { 730 register struct rtentry *rt; 731 static struct sockaddr_inarp sin; 732 733 sin.sin_len = sizeof(sin); 734 sin.sin_family = AF_INET; 735 sin.sin_addr.s_addr = addr; 736 sin.sin_other = proxy ? SIN_PROXY : 0; 737 rt = rtalloc1(sintosa(&sin), create); 738 if (rt == 0) 739 return (0); 740 rt->rt_refcnt--; 741 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 || 742 rt->rt_gateway->sa_family != AF_LINK) { 743 if (create) 744 log(LOG_DEBUG, 745 "arplookup: unable to enter address for %s\n", 746 inet_ntoa(sin.sin_addr)); 747 return (0); 748 } 749 return ((struct llinfo_arp *)rt->rt_llinfo); 750 } 751 752 int 753 arpioctl(cmd, data) 754 u_long cmd; 755 caddr_t data; 756 { 757 758 return (EOPNOTSUPP); 759 } 760 761 void 762 arp_ifinit(ac, ifa) 763 struct arpcom *ac; 764 struct ifaddr *ifa; 765 { 766 767 /* Warn the user if another station has this IP address. */ 768 arprequest(&ac->ac_if, 769 &(IA_SIN(ifa)->sin_addr.s_addr), 770 &(IA_SIN(ifa)->sin_addr.s_addr), 771 ac->ac_enaddr); 772 ifa->ifa_rtrequest = arp_rtrequest; 773 ifa->ifa_flags |= RTF_CLONING; 774 } 775 776 /* 777 * Called from Ethernet interrupt handlers 778 * when ether packet type ETHERTYPE_REVARP 779 * is received. Common length and type checks are done here, 780 * then the protocol-specific routine is called. 781 */ 782 void 783 revarpinput(m) 784 struct mbuf *m; 785 { 786 struct arphdr *ar; 787 788 if (m->m_len < sizeof(struct arphdr)) 789 goto out; 790 ar = mtod(m, struct arphdr *); 791 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) 792 goto out; 793 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln)) 794 goto out; 795 switch (ntohs(ar->ar_pro)) { 796 797 case ETHERTYPE_IP: 798 case ETHERTYPE_IPTRAILERS: 799 in_revarpinput(m); 800 return; 801 802 default: 803 break; 804 } 805 out: 806 m_freem(m); 807 } 808 809 /* 810 * RARP for Internet protocols on Ethernet. 811 * Algorithm is that given in RFC 903. 812 * We are only using for bootstrap purposes to get an ip address for one of 813 * our interfaces. Thus we support no user-interface. 814 * 815 * Since the contents of the RARP reply are specific to the interface that 816 * sent the request, this code must ensure that they are properly associated. 817 * 818 * Note: also supports ARP via RARP packets, per the RFC. 819 */ 820 void 821 in_revarpinput(m) 822 struct mbuf *m; 823 { 824 struct ifnet *ifp; 825 struct ether_arp *ar; 826 int op; 827 828 ar = mtod(m, struct ether_arp *); 829 op = ntohs(ar->arp_op); 830 switch (op) { 831 case ARPOP_REQUEST: 832 case ARPOP_REPLY: /* per RFC */ 833 in_arpinput(m); 834 return; 835 case ARPOP_REVREPLY: 836 break; 837 case ARPOP_REVREQUEST: /* handled by rarpd(8) */ 838 default: 839 goto out; 840 } 841 if (!revarp_in_progress) 842 goto out; 843 ifp = m->m_pkthdr.rcvif; 844 if (ifp != myip_ifp) /* !same interface */ 845 goto out; 846 if (myip_initialized) 847 goto wake; 848 if (bcmp(ar->arp_tha, ((struct arpcom *)ifp)->ac_enaddr, 849 sizeof(ar->arp_tha))) 850 goto out; 851 bcopy((caddr_t)ar->arp_spa, (caddr_t)&srv_ip, sizeof(srv_ip)); 852 bcopy((caddr_t)ar->arp_tpa, (caddr_t)&myip, sizeof(myip)); 853 myip_initialized = 1; 854 wake: /* Do wakeup every time in case it was missed. */ 855 wakeup((caddr_t)&myip); 856 857 out: 858 m_freem(m); 859 } 860 861 /* 862 * Send a RARP request for the ip address of the specified interface. 863 * The request should be RFC 903-compliant. 864 */ 865 void 866 revarprequest(ifp) 867 struct ifnet *ifp; 868 { 869 struct sockaddr sa; 870 struct mbuf *m; 871 struct ether_header *eh; 872 struct ether_arp *ea; 873 struct arpcom *ac = (struct arpcom *)ifp; 874 875 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 876 return; 877 m->m_len = sizeof(*ea); 878 m->m_pkthdr.len = sizeof(*ea); 879 MH_ALIGN(m, sizeof(*ea)); 880 ea = mtod(m, struct ether_arp *); 881 eh = (struct ether_header *)sa.sa_data; 882 bzero((caddr_t)ea, sizeof(*ea)); 883 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 884 sizeof(eh->ether_dhost)); 885 eh->ether_type = htons(ETHERTYPE_REVARP); 886 ea->arp_hrd = htons(ARPHRD_ETHER); 887 ea->arp_pro = htons(ETHERTYPE_IP); 888 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 889 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 890 ea->arp_op = htons(ARPOP_REVREQUEST); 891 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 892 sizeof(ea->arp_tha)); 893 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, 894 sizeof(ea->arp_sha)); 895 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha, 896 sizeof(ea->arp_tha)); 897 sa.sa_family = AF_UNSPEC; 898 sa.sa_len = sizeof(sa); 899 ifp->if_output(ifp, m, &sa, (struct rtentry *)0); 900 } 901 902 /* 903 * RARP for the ip address of the specified interface, but also 904 * save the ip address of the server that sent the answer. 905 * Timeout if no response is received. 906 */ 907 int 908 revarpwhoarewe(ifp, serv_in, clnt_in) 909 struct ifnet *ifp; 910 struct in_addr *serv_in; 911 struct in_addr *clnt_in; 912 { 913 int result, count = 20; 914 915 if (myip_initialized) 916 return EIO; 917 918 myip_ifp = ifp; 919 revarp_in_progress = 1; 920 while (count--) { 921 revarprequest(ifp); 922 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2); 923 if (result != EWOULDBLOCK) 924 break; 925 } 926 revarp_in_progress = 0; 927 if (!myip_initialized) 928 return ENETUNREACH; 929 930 bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in)); 931 bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in)); 932 return 0; 933 } 934 935 /* For compatibility: only saves interface address. */ 936 int 937 revarpwhoami(in, ifp) 938 struct in_addr *in; 939 struct ifnet *ifp; 940 { 941 struct in_addr server; 942 return (revarpwhoarewe(ifp, &server, in)); 943 } 944 945 946 #ifdef DDB 947 948 #include <machine/db_machdep.h> 949 #include <ddb/db_interface.h> 950 #include <ddb/db_output.h> 951 952 void 953 db_print_sa(sa) 954 struct sockaddr *sa; 955 { 956 int len; 957 u_char *p; 958 959 if (sa == 0) { 960 db_printf("[NULL]"); 961 return; 962 } 963 964 p = (u_char *)sa; 965 len = sa->sa_len; 966 db_printf("["); 967 while (len > 0) { 968 db_printf("%d", *p); 969 p++; 970 len--; 971 if (len) 972 db_printf(","); 973 } 974 db_printf("]\n"); 975 } 976 977 void 978 db_print_ifa(ifa) 979 struct ifaddr *ifa; 980 { 981 if (ifa == 0) 982 return; 983 db_printf(" ifa_addr="); 984 db_print_sa(ifa->ifa_addr); 985 db_printf(" ifa_dsta="); 986 db_print_sa(ifa->ifa_dstaddr); 987 db_printf(" ifa_mask="); 988 db_print_sa(ifa->ifa_netmask); 989 db_printf(" flags=0x%x, refcnt=%d, metric=%d\n", 990 ifa->ifa_flags, ifa->ifa_refcnt, ifa->ifa_metric); 991 } 992 993 void 994 db_print_llinfo(li) 995 caddr_t li; 996 { 997 struct llinfo_arp *la; 998 999 if (li == 0) 1000 return; 1001 la = (struct llinfo_arp *)li; 1002 db_printf(" la_rt=%p la_hold=%p, la_asked=0x%lx\n", 1003 la->la_rt, la->la_hold, la->la_asked); 1004 } 1005 1006 /* 1007 * Function to pass to rn_walktree(). 1008 * Return non-zero error to abort walk. 1009 */ 1010 int 1011 db_show_radix_node(rn, w) 1012 struct radix_node *rn; 1013 void *w; 1014 { 1015 struct rtentry *rt = (struct rtentry *)rn; 1016 1017 db_printf("rtentry=%p", rt); 1018 1019 db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n", 1020 rt->rt_flags, rt->rt_refcnt, rt->rt_use, rt->rt_expire); 1021 1022 db_printf(" key="); db_print_sa(rt_key(rt)); 1023 db_printf(" mask="); db_print_sa(rt_mask(rt)); 1024 db_printf(" gw="); db_print_sa(rt->rt_gateway); 1025 1026 db_printf(" ifp=%p ", rt->rt_ifp); 1027 if (rt->rt_ifp) 1028 db_printf("(%s)", rt->rt_ifp->if_xname); 1029 else 1030 db_printf("(NULL)"); 1031 1032 db_printf(" ifa=%p\n", rt->rt_ifa); 1033 db_print_ifa(rt->rt_ifa); 1034 1035 db_printf(" genmask="); db_print_sa(rt->rt_genmask); 1036 1037 db_printf(" gwroute=%p llinfo=%p\n", rt->rt_gwroute, rt->rt_llinfo); 1038 db_print_llinfo(rt->rt_llinfo); 1039 return (0); 1040 } 1041 1042 /* 1043 * Function to print all the route trees. 1044 * Use this from ddb: "call db_show_arptab" 1045 */ 1046 int 1047 db_show_arptab() 1048 { 1049 struct radix_node_head *rnh; 1050 rnh = rt_tables[AF_INET]; 1051 db_printf("Route tree for AF_INET\n"); 1052 if (rnh == NULL) { 1053 db_printf(" (not initialized)\n"); 1054 return (0); 1055 } 1056 rn_walktree(rnh, db_show_radix_node, NULL); 1057 return (0); 1058 } 1059 #endif 1060 #endif /* INET */ 1061