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