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