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