1 /* $OpenBSD: if_ether.c,v 1.82 2009/11/03 10:59:04 claudio 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/kernel.h> 51 #include <sys/syslog.h> 52 #include <sys/proc.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 67 #define SIN(s) ((struct sockaddr_in *)s) 68 #define SDL(s) ((struct sockaddr_dl *)s) 69 #define SRP(s) ((struct sockaddr_inarp *)s) 70 71 /* 72 * ARP trailer negotiation. Trailer protocol is not IP specific, 73 * but ARP request/response use IP addresses. 74 */ 75 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL 76 77 /* timer values */ 78 int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 79 int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 80 int arpt_down = 20; /* once declared down, don't send for 20 secs */ 81 #define rt_expire rt_rmx.rmx_expire 82 83 void arptfree(struct llinfo_arp *); 84 void arptimer(void *); 85 struct llinfo_arp *arplookup(u_int32_t, int, int, u_int); 86 void in_arpinput(struct mbuf *); 87 88 LIST_HEAD(, llinfo_arp) llinfo_arp; 89 struct ifqueue arpintrq = {0, 0, 0, 50}; 90 int arp_inuse, arp_allocated, arp_intimer; 91 int arp_maxtries = 5; 92 int useloopback = 1; /* use loopback interface for local traffic */ 93 int arpinit_done; 94 int la_hold_total; 95 96 /* revarp state */ 97 struct in_addr myip, srv_ip; 98 int myip_initialized; 99 int revarp_in_progress; 100 struct ifnet *myip_ifp; 101 102 #ifdef DDB 103 #include <uvm/uvm_extern.h> 104 105 void db_print_sa(struct sockaddr *); 106 void db_print_ifa(struct ifaddr *); 107 void db_print_llinfo(caddr_t); 108 int db_show_radix_node(struct radix_node *, void *); 109 #endif 110 111 /* 112 * Timeout routine. Age arp_tab entries periodically. 113 */ 114 /* ARGSUSED */ 115 void 116 arptimer(arg) 117 void *arg; 118 { 119 struct timeout *to = (struct timeout *)arg; 120 int s; 121 struct llinfo_arp *la, *nla; 122 123 s = splsoftnet(); 124 timeout_add_sec(to, arpt_prune); 125 for (la = LIST_FIRST(&llinfo_arp); la != LIST_END(&llinfo_arp); 126 la = nla) { 127 struct rtentry *rt = la->la_rt; 128 129 nla = LIST_NEXT(la, la_list); 130 if (rt->rt_expire && rt->rt_expire <= time_second) 131 arptfree(la); /* timer has expired; clear */ 132 } 133 splx(s); 134 } 135 136 /* 137 * Parallel to llc_rtrequest. 138 */ 139 void 140 arp_rtrequest(req, rt, info) 141 int req; 142 struct rtentry *rt; 143 struct rt_addrinfo *info; 144 { 145 struct sockaddr *gate = rt->rt_gateway; 146 struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 147 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 148 struct in_ifaddr *ia; 149 struct ifaddr *ifa; 150 struct mbuf *m; 151 152 if (!arpinit_done) { 153 static struct timeout arptimer_to; 154 155 arpinit_done = 1; 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(ifp, sip, tip, enaddr) 329 struct ifnet *ifp; 330 u_int32_t *sip, *tip; 331 u_int8_t *enaddr; 332 { 333 struct mbuf *m; 334 struct ether_header *eh; 335 struct ether_arp *ea; 336 struct sockaddr sa; 337 338 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 339 return; 340 m->m_len = sizeof(*ea); 341 m->m_pkthdr.len = sizeof(*ea); 342 m->m_pkthdr.rdomain = ifp->if_rdomain; 343 MH_ALIGN(m, sizeof(*ea)); 344 ea = mtod(m, struct ether_arp *); 345 eh = (struct ether_header *)sa.sa_data; 346 bzero((caddr_t)ea, sizeof (*ea)); 347 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 348 sizeof(eh->ether_dhost)); 349 eh->ether_type = htons(ETHERTYPE_ARP); /* if_output will not swap */ 350 ea->arp_hrd = htons(ARPHRD_ETHER); 351 ea->arp_pro = htons(ETHERTYPE_IP); 352 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 353 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 354 ea->arp_op = htons(ARPOP_REQUEST); 355 bcopy((caddr_t)enaddr, (caddr_t)eh->ether_shost, 356 sizeof(eh->ether_shost)); 357 bcopy((caddr_t)enaddr, (caddr_t)ea->arp_sha, sizeof(ea->arp_sha)); 358 bcopy((caddr_t)sip, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa)); 359 bcopy((caddr_t)tip, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa)); 360 sa.sa_family = pseudo_AF_HDRCMPLT; 361 sa.sa_len = sizeof(sa); 362 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 363 } 364 365 /* 366 * Resolve an IP address into an ethernet address. If success, 367 * desten is filled in. If there is no entry in arptab, 368 * set one up and broadcast a request for the IP address. 369 * Hold onto this mbuf and resend it once the address 370 * is finally resolved. A return value of 1 indicates 371 * that desten has been filled in and the packet should be sent 372 * normally; a 0 return indicates that the packet has been 373 * taken over here, either now or for later transmission. 374 */ 375 int 376 arpresolve(ac, rt, m, dst, desten) 377 struct arpcom *ac; 378 struct rtentry *rt; 379 struct mbuf *m; 380 struct sockaddr *dst; 381 u_char *desten; 382 { 383 struct llinfo_arp *la; 384 struct sockaddr_dl *sdl; 385 struct mbuf *mh; 386 387 if (m->m_flags & M_BCAST) { /* broadcast */ 388 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten, 389 sizeof(etherbroadcastaddr)); 390 return (1); 391 } 392 if (m->m_flags & M_MCAST) { /* multicast */ 393 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 394 return (1); 395 } 396 if (rt) { 397 la = (struct llinfo_arp *)rt->rt_llinfo; 398 if (la == NULL) 399 log(LOG_DEBUG, "arpresolve: %s: route without link " 400 "local address\n", inet_ntoa(SIN(dst)->sin_addr)); 401 } else { 402 if ((la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0, 403 ac->ac_if.if_rdomain)) != NULL) 404 rt = la->la_rt; 405 else 406 log(LOG_DEBUG, 407 "arpresolve: %s: can't allocate llinfo\n", 408 inet_ntoa(SIN(dst)->sin_addr)); 409 } 410 if (la == 0 || rt == 0) { 411 m_freem(m); 412 return (0); 413 } 414 sdl = SDL(rt->rt_gateway); 415 /* 416 * Check the address family and length is valid, the address 417 * is resolved; otherwise, try to resolve. 418 */ 419 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && 420 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 421 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 422 return 1; 423 } 424 if (((struct ifnet *)ac)->if_flags & IFF_NOARP) { 425 m_freem(m); 426 return 0; 427 } 428 429 /* 430 * There is an arptab entry, but no ethernet address 431 * response yet. Insert mbuf in hold queue if below limit 432 * if above the limit free the queue without queuing the new packet. 433 */ 434 if (la_hold_total < MAX_HOLD_TOTAL && la_hold_total < nmbclust / 64) { 435 if (la->la_hold_count >= MAX_HOLD_QUEUE) { 436 mh = la->la_hold_head; 437 la->la_hold_head = la->la_hold_head->m_nextpkt; 438 if (mh == la->la_hold_tail) 439 la->la_hold_tail = NULL; 440 la->la_hold_count--; 441 la_hold_total--; 442 m_freem(mh); 443 } 444 if (la->la_hold_tail == NULL) 445 la->la_hold_head = m; 446 else 447 la->la_hold_tail->m_nextpkt = m; 448 la->la_hold_tail = m; 449 la->la_hold_count++; 450 la_hold_total++; 451 } else { 452 while ((mh = la->la_hold_head) != NULL) { 453 la->la_hold_head = 454 la->la_hold_head->m_nextpkt; 455 la_hold_total--; 456 m_freem(mh); 457 } 458 la->la_hold_tail = NULL; 459 la->la_hold_count = 0; 460 m_freem(m); 461 } 462 463 /* 464 * Re-send the ARP request when appropriate. 465 */ 466 #ifdef DIAGNOSTIC 467 if (rt->rt_expire == 0) { 468 /* This should never happen. (Should it? -gwr) */ 469 printf("arpresolve: unresolved and rt_expire == 0\n"); 470 /* Set expiration time to now (expired). */ 471 rt->rt_expire = time_second; 472 } 473 #endif 474 if (rt->rt_expire) { 475 rt->rt_flags &= ~RTF_REJECT; 476 if (la->la_asked == 0 || rt->rt_expire != time_second) { 477 rt->rt_expire = time_second; 478 if (la->la_asked++ < arp_maxtries) 479 arprequest(&ac->ac_if, 480 &(SIN(rt->rt_ifa->ifa_addr)->sin_addr.s_addr), 481 &(SIN(dst)->sin_addr.s_addr), 482 #if NCARP > 0 483 (rt->rt_ifp->if_type == IFT_CARP) ? 484 ((struct arpcom *) rt->rt_ifp->if_softc 485 )->ac_enaddr : 486 #endif 487 ac->ac_enaddr); 488 else { 489 rt->rt_flags |= RTF_REJECT; 490 rt->rt_expire += arpt_down; 491 la->la_asked = 0; 492 while ((mh = la->la_hold_head) != NULL) { 493 la->la_hold_head = 494 la->la_hold_head->m_nextpkt; 495 la_hold_total--; 496 m_freem(mh); 497 } 498 la->la_hold_tail = NULL; 499 la->la_hold_count = 0; 500 } 501 } 502 } 503 return (0); 504 } 505 506 /* 507 * Common length and type checks are done here, 508 * then the protocol-specific routine is called. 509 */ 510 void 511 arpintr() 512 { 513 struct mbuf *m; 514 struct arphdr *ar; 515 int s, len; 516 517 for (;;) { 518 s = splnet(); 519 IF_DEQUEUE(&arpintrq, m); 520 splx(s); 521 if (m == NULL) 522 break; 523 #ifdef DIAGNOSTIC 524 if ((m->m_flags & M_PKTHDR) == 0) 525 panic("arpintr"); 526 #endif 527 528 len = sizeof(struct arphdr); 529 if (m->m_len < len && (m = m_pullup(m, len)) == NULL) 530 continue; 531 532 ar = mtod(m, struct arphdr *); 533 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) { 534 m_freem(m); 535 continue; 536 } 537 538 len += 2 * (ar->ar_hln + ar->ar_pln); 539 if (m->m_len < len && (m = m_pullup(m, len)) == NULL) 540 continue; 541 542 switch (ntohs(ar->ar_pro)) { 543 case ETHERTYPE_IP: 544 case ETHERTYPE_IPTRAILERS: 545 in_arpinput(m); 546 continue; 547 } 548 m_freem(m); 549 } 550 } 551 552 /* 553 * ARP for Internet protocols on Ethernet. 554 * Algorithm is that given in RFC 826. 555 * In addition, a sanity check is performed on the sender 556 * protocol address, to catch impersonators. 557 * We no longer handle negotiations for use of trailer protocol: 558 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 559 * along with IP replies if we wanted trailers sent to us, 560 * and also sent them in response to IP replies. 561 * This allowed either end to announce the desire to receive 562 * trailer packets. 563 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 564 * but formerly didn't normally send requests. 565 */ 566 void 567 in_arpinput(m) 568 struct mbuf *m; 569 { 570 struct ether_arp *ea; 571 struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; 572 struct ether_header *eh; 573 struct llinfo_arp *la = 0; 574 struct rtentry *rt; 575 struct in_ifaddr *ia; 576 #if NBRIDGE > 0 577 struct in_ifaddr *bridge_ia = NULL; 578 #endif 579 struct sockaddr_dl *sdl; 580 struct sockaddr sa; 581 struct in_addr isaddr, itaddr, myaddr; 582 struct mbuf *mh, *mt; 583 u_int8_t *enaddr = NULL; 584 #if NCARP > 0 585 u_int8_t *ether_shost = NULL; 586 #endif 587 int op; 588 589 ea = mtod(m, struct ether_arp *); 590 op = ntohs(ea->arp_op); 591 if ((op != ARPOP_REQUEST) && (op != ARPOP_REPLY)) 592 goto out; 593 #if notyet 594 if ((op == ARPOP_REPLY) && (m->m_flags & (M_BCAST|M_MCAST))) { 595 log(LOG_ERR, 596 "arp: received reply to broadcast or multicast address\n"); 597 goto out; 598 } 599 #endif 600 601 bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof(itaddr)); 602 bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof(isaddr)); 603 604 /* First try: check target against our addresses */ 605 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 606 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr) 607 continue; 608 609 #if NCARP > 0 610 if (ia->ia_ifp->if_type == IFT_CARP && 611 ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) == 612 (IFF_UP|IFF_RUNNING))) { 613 if (ia->ia_ifp == m->m_pkthdr.rcvif) { 614 if (op == ARPOP_REPLY) 615 break; 616 if (carp_iamatch(ia, ea->arp_sha, 617 &enaddr, ðer_shost)) 618 break; 619 else 620 goto out; 621 } 622 } else 623 #endif 624 if (ia->ia_ifp == m->m_pkthdr.rcvif) 625 break; 626 #if NBRIDGE > 0 627 /* 628 * If the interface we received the packet on 629 * is part of a bridge, check to see if we need 630 * to "bridge" the packet to ourselves at this 631 * layer. Note we still prefer a perfect match, 632 * but allow this weaker match if necessary. 633 */ 634 if (m->m_pkthdr.rcvif->if_bridge != NULL) { 635 if (m->m_pkthdr.rcvif->if_bridge == 636 ia->ia_ifp->if_bridge) 637 bridge_ia = ia; 638 #if NCARP > 0 639 else if (ia->ia_ifp->if_carpdev != NULL && 640 m->m_pkthdr.rcvif->if_bridge == 641 ia->ia_ifp->if_carpdev->if_bridge) { 642 if (carp_iamatch(ia, ea->arp_sha, 643 &enaddr, ðer_shost)) 644 bridge_ia = ia; 645 else 646 goto out; 647 } 648 #endif 649 } 650 #endif 651 } 652 653 #if NBRIDGE > 0 654 /* use bridge_ia if there was no direct match */ 655 if (ia == NULL && bridge_ia != NULL) { 656 ia = bridge_ia; 657 ac = (struct arpcom *)bridge_ia->ia_ifp; 658 } 659 #endif 660 661 /* Second try: check source against our addresses */ 662 if (ia == NULL) { 663 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 664 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr) 665 continue; 666 if (ia->ia_ifp == m->m_pkthdr.rcvif) 667 break; 668 } 669 } 670 671 /* Third try: not one of our addresses, just find an usable ia */ 672 if (ia == NULL) { 673 struct ifaddr *ifa; 674 675 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) { 676 if (ifa->ifa_addr->sa_family == AF_INET) 677 break; 678 } 679 if (ifa) 680 ia = (struct in_ifaddr *)ifa; 681 } 682 683 if (ia == NULL) 684 goto out; 685 686 if (!enaddr) 687 enaddr = ac->ac_enaddr; 688 myaddr = ia->ia_addr.sin_addr; 689 690 if (!bcmp((caddr_t)ea->arp_sha, enaddr, sizeof (ea->arp_sha))) 691 goto out; /* it's from me, ignore it. */ 692 if (ETHER_IS_MULTICAST (&ea->arp_sha[0])) 693 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 694 sizeof (ea->arp_sha))) { 695 log(LOG_ERR, "arp: ether address is broadcast for " 696 "IP address %s!\n", inet_ntoa(isaddr)); 697 goto out; 698 } 699 if (myaddr.s_addr && isaddr.s_addr == myaddr.s_addr) { 700 log(LOG_ERR, 701 "duplicate IP address %s sent from ethernet address %s\n", 702 inet_ntoa(isaddr), ether_sprintf(ea->arp_sha)); 703 itaddr = myaddr; 704 goto reply; 705 } 706 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0, 707 rtable_l2(m->m_pkthdr.rdomain)); 708 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 709 if (sdl->sdl_alen) { 710 if (bcmp(ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) { 711 if (rt->rt_flags & RTF_PERMANENT_ARP) { 712 log(LOG_WARNING, 713 "arp: attempt to overwrite permanent " 714 "entry for %s by %s on %s\n", 715 inet_ntoa(isaddr), 716 ether_sprintf(ea->arp_sha), 717 ac->ac_if.if_xname); 718 goto out; 719 } else if (rt->rt_ifp != &ac->ac_if) { 720 if (ac->ac_if.if_type != IFT_CARP) 721 log(LOG_WARNING, 722 "arp: attempt to overwrite entry for" 723 " %s on %s by %s on %s\n", 724 inet_ntoa(isaddr), 725 rt->rt_ifp->if_xname, 726 ether_sprintf(ea->arp_sha), 727 ac->ac_if.if_xname); 728 goto out; 729 } else { 730 log(LOG_INFO, 731 "arp info overwritten for %s by %s on %s\n", 732 inet_ntoa(isaddr), 733 ether_sprintf(ea->arp_sha), 734 ac->ac_if.if_xname); 735 rt->rt_expire = 1; /* no longer static */ 736 } 737 } 738 } else if (rt->rt_ifp != &ac->ac_if && !(ac->ac_if.if_bridge && 739 (rt->rt_ifp->if_bridge == ac->ac_if.if_bridge)) && 740 !(rt->rt_ifp->if_type == IFT_CARP && 741 rt->rt_ifp->if_carpdev == &ac->ac_if) && 742 !(ac->ac_if.if_type == IFT_CARP && 743 ac->ac_if.if_carpdev == rt->rt_ifp)) { 744 log(LOG_WARNING, 745 "arp: attempt to add entry for %s " 746 "on %s by %s on %s\n", 747 inet_ntoa(isaddr), rt->rt_ifp->if_xname, 748 ether_sprintf(ea->arp_sha), 749 ac->ac_if.if_xname); 750 goto out; 751 } 752 bcopy(ea->arp_sha, LLADDR(sdl), 753 sdl->sdl_alen = sizeof(ea->arp_sha)); 754 if (rt->rt_expire) 755 rt->rt_expire = time_second + arpt_keep; 756 rt->rt_flags &= ~RTF_REJECT; 757 la->la_asked = 0; 758 while ((mh = la->la_hold_head) != NULL) { 759 if ((la->la_hold_head = mh->m_nextpkt) == NULL) 760 la->la_hold_tail = NULL; 761 la->la_hold_count--; 762 la_hold_total--; 763 mt = la->la_hold_tail; 764 765 (*ac->ac_if.if_output)(&ac->ac_if, mh, rt_key(rt), rt); 766 767 if (la->la_hold_tail == mh) { 768 /* mbuf is back in queue. Discard. */ 769 la->la_hold_tail = mt; 770 if (la->la_hold_tail) 771 la->la_hold_tail->m_nextpkt = NULL; 772 else 773 la->la_hold_head = NULL; 774 la->la_hold_count--; 775 la_hold_total--; 776 m_freem(mh); 777 } 778 } 779 } 780 reply: 781 if (op != ARPOP_REQUEST) { 782 out: 783 m_freem(m); 784 return; 785 } 786 if (itaddr.s_addr == myaddr.s_addr) { 787 /* I am the target */ 788 bcopy(ea->arp_sha, ea->arp_tha, sizeof(ea->arp_sha)); 789 bcopy(enaddr, ea->arp_sha, sizeof(ea->arp_sha)); 790 } else { 791 la = arplookup(itaddr.s_addr, 0, SIN_PROXY, 792 rtable_l2(m->m_pkthdr.rdomain)); 793 if (la == 0) 794 goto out; 795 rt = la->la_rt; 796 if (rt->rt_ifp->if_type == IFT_CARP && 797 m->m_pkthdr.rcvif->if_type != IFT_CARP) 798 goto out; 799 bcopy(ea->arp_sha, ea->arp_tha, sizeof(ea->arp_sha)); 800 sdl = SDL(rt->rt_gateway); 801 bcopy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha)); 802 } 803 804 bcopy(ea->arp_spa, ea->arp_tpa, sizeof(ea->arp_spa)); 805 bcopy(&itaddr, ea->arp_spa, sizeof(ea->arp_spa)); 806 ea->arp_op = htons(ARPOP_REPLY); 807 ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 808 eh = (struct ether_header *)sa.sa_data; 809 bcopy(ea->arp_tha, eh->ether_dhost, sizeof(eh->ether_dhost)); 810 #if NCARP > 0 811 if (ether_shost) 812 enaddr = ether_shost; 813 #endif 814 bcopy(enaddr, eh->ether_shost, sizeof(eh->ether_shost)); 815 816 eh->ether_type = htons(ETHERTYPE_ARP); 817 sa.sa_family = pseudo_AF_HDRCMPLT; 818 sa.sa_len = sizeof(sa); 819 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 820 return; 821 } 822 823 /* 824 * Free an arp entry. 825 */ 826 void 827 arptfree(la) 828 struct llinfo_arp *la; 829 { 830 struct rtentry *rt = la->la_rt; 831 struct sockaddr_dl *sdl; 832 struct rt_addrinfo info; 833 u_int tid = 0; 834 835 if (rt == 0) 836 panic("arptfree"); 837 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 838 sdl->sdl_family == AF_LINK) { 839 sdl->sdl_alen = 0; 840 la->la_asked = 0; 841 rt->rt_flags &= ~RTF_REJECT; 842 return; 843 } 844 bzero(&info, sizeof(info)); 845 info.rti_info[RTAX_DST] = rt_key(rt); 846 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 847 848 if (rt->rt_ifp) 849 tid = rt->rt_ifp->if_rdomain; 850 851 rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL, tid); 852 } 853 854 /* 855 * Lookup or enter a new address in arptab. 856 */ 857 struct llinfo_arp * 858 arplookup(addr, create, proxy, tableid) 859 u_int32_t addr; 860 int create, proxy; 861 u_int tableid; 862 { 863 struct rtentry *rt; 864 static struct sockaddr_inarp sin; 865 866 sin.sin_len = sizeof(sin); 867 sin.sin_family = AF_INET; 868 sin.sin_addr.s_addr = addr; 869 sin.sin_other = proxy ? SIN_PROXY : 0; 870 rt = rtalloc1(sintosa(&sin), create, tableid); 871 if (rt == 0) 872 return (0); 873 rt->rt_refcnt--; 874 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 || 875 rt->rt_gateway->sa_family != AF_LINK) { 876 if (create) { 877 if (rt->rt_refcnt <= 0 && 878 (rt->rt_flags & RTF_CLONED) != 0) { 879 struct rt_addrinfo info; 880 881 bzero(&info, sizeof(info)); 882 info.rti_info[RTAX_DST] = rt_key(rt); 883 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 884 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 885 886 rtrequest1(RTM_DELETE, &info, rt->rt_priority, 887 NULL, tableid); 888 } 889 } 890 return (0); 891 } 892 return ((struct llinfo_arp *)rt->rt_llinfo); 893 } 894 895 int 896 arpioctl(cmd, data) 897 u_long cmd; 898 caddr_t data; 899 { 900 901 return (EOPNOTSUPP); 902 } 903 904 void 905 arp_ifinit(ac, ifa) 906 struct arpcom *ac; 907 struct ifaddr *ifa; 908 { 909 910 /* Warn the user if another station has this IP address. */ 911 arprequest(&ac->ac_if, 912 &(IA_SIN(ifa)->sin_addr.s_addr), 913 &(IA_SIN(ifa)->sin_addr.s_addr), 914 ac->ac_enaddr); 915 ifa->ifa_rtrequest = arp_rtrequest; 916 ifa->ifa_flags |= RTF_CLONING; 917 } 918 919 /* 920 * Called from Ethernet interrupt handlers 921 * when ether packet type ETHERTYPE_REVARP 922 * is received. Common length and type checks are done here, 923 * then the protocol-specific routine is called. 924 */ 925 void 926 revarpinput(m) 927 struct mbuf *m; 928 { 929 struct arphdr *ar; 930 931 if (m->m_len < sizeof(struct arphdr)) 932 goto out; 933 ar = mtod(m, struct arphdr *); 934 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) 935 goto out; 936 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln)) 937 goto out; 938 switch (ntohs(ar->ar_pro)) { 939 940 case ETHERTYPE_IP: 941 case ETHERTYPE_IPTRAILERS: 942 in_revarpinput(m); 943 return; 944 945 default: 946 break; 947 } 948 out: 949 m_freem(m); 950 } 951 952 /* 953 * RARP for Internet protocols on Ethernet. 954 * Algorithm is that given in RFC 903. 955 * We are only using for bootstrap purposes to get an ip address for one of 956 * our interfaces. Thus we support no user-interface. 957 * 958 * Since the contents of the RARP reply are specific to the interface that 959 * sent the request, this code must ensure that they are properly associated. 960 * 961 * Note: also supports ARP via RARP packets, per the RFC. 962 */ 963 void 964 in_revarpinput(m) 965 struct mbuf *m; 966 { 967 struct ifnet *ifp; 968 struct ether_arp *ar; 969 int op; 970 971 ar = mtod(m, struct ether_arp *); 972 op = ntohs(ar->arp_op); 973 switch (op) { 974 case ARPOP_REQUEST: 975 case ARPOP_REPLY: /* per RFC */ 976 in_arpinput(m); 977 return; 978 case ARPOP_REVREPLY: 979 break; 980 case ARPOP_REVREQUEST: /* handled by rarpd(8) */ 981 default: 982 goto out; 983 } 984 if (!revarp_in_progress) 985 goto out; 986 ifp = m->m_pkthdr.rcvif; 987 if (ifp != myip_ifp) /* !same interface */ 988 goto out; 989 if (myip_initialized) 990 goto wake; 991 if (bcmp(ar->arp_tha, ((struct arpcom *)ifp)->ac_enaddr, 992 sizeof(ar->arp_tha))) 993 goto out; 994 bcopy((caddr_t)ar->arp_spa, (caddr_t)&srv_ip, sizeof(srv_ip)); 995 bcopy((caddr_t)ar->arp_tpa, (caddr_t)&myip, sizeof(myip)); 996 myip_initialized = 1; 997 wake: /* Do wakeup every time in case it was missed. */ 998 wakeup((caddr_t)&myip); 999 1000 out: 1001 m_freem(m); 1002 } 1003 1004 /* 1005 * Send a RARP request for the ip address of the specified interface. 1006 * The request should be RFC 903-compliant. 1007 */ 1008 void 1009 revarprequest(ifp) 1010 struct ifnet *ifp; 1011 { 1012 struct sockaddr sa; 1013 struct mbuf *m; 1014 struct ether_header *eh; 1015 struct ether_arp *ea; 1016 struct arpcom *ac = (struct arpcom *)ifp; 1017 1018 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 1019 return; 1020 m->m_len = sizeof(*ea); 1021 m->m_pkthdr.len = sizeof(*ea); 1022 MH_ALIGN(m, sizeof(*ea)); 1023 ea = mtod(m, struct ether_arp *); 1024 eh = (struct ether_header *)sa.sa_data; 1025 bzero((caddr_t)ea, sizeof(*ea)); 1026 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 1027 sizeof(eh->ether_dhost)); 1028 eh->ether_type = htons(ETHERTYPE_REVARP); 1029 ea->arp_hrd = htons(ARPHRD_ETHER); 1030 ea->arp_pro = htons(ETHERTYPE_IP); 1031 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 1032 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 1033 ea->arp_op = htons(ARPOP_REVREQUEST); 1034 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 1035 sizeof(ea->arp_tha)); 1036 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, 1037 sizeof(ea->arp_sha)); 1038 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha, 1039 sizeof(ea->arp_tha)); 1040 sa.sa_family = pseudo_AF_HDRCMPLT; 1041 sa.sa_len = sizeof(sa); 1042 ifp->if_output(ifp, m, &sa, (struct rtentry *)0); 1043 } 1044 1045 /* 1046 * RARP for the ip address of the specified interface, but also 1047 * save the ip address of the server that sent the answer. 1048 * Timeout if no response is received. 1049 */ 1050 int 1051 revarpwhoarewe(ifp, serv_in, clnt_in) 1052 struct ifnet *ifp; 1053 struct in_addr *serv_in; 1054 struct in_addr *clnt_in; 1055 { 1056 int result, count = 20; 1057 1058 if (myip_initialized) 1059 return EIO; 1060 1061 myip_ifp = ifp; 1062 revarp_in_progress = 1; 1063 while (count--) { 1064 revarprequest(ifp); 1065 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2); 1066 if (result != EWOULDBLOCK) 1067 break; 1068 } 1069 revarp_in_progress = 0; 1070 if (!myip_initialized) 1071 return ENETUNREACH; 1072 1073 bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in)); 1074 bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in)); 1075 return 0; 1076 } 1077 1078 /* For compatibility: only saves interface address. */ 1079 int 1080 revarpwhoami(in, ifp) 1081 struct in_addr *in; 1082 struct ifnet *ifp; 1083 { 1084 struct in_addr server; 1085 return (revarpwhoarewe(ifp, &server, in)); 1086 } 1087 1088 1089 #ifdef DDB 1090 1091 #include <machine/db_machdep.h> 1092 #include <ddb/db_interface.h> 1093 #include <ddb/db_output.h> 1094 1095 void 1096 db_print_sa(sa) 1097 struct sockaddr *sa; 1098 { 1099 int len; 1100 u_char *p; 1101 1102 if (sa == 0) { 1103 db_printf("[NULL]"); 1104 return; 1105 } 1106 1107 p = (u_char *)sa; 1108 len = sa->sa_len; 1109 db_printf("["); 1110 while (len > 0) { 1111 db_printf("%d", *p); 1112 p++; 1113 len--; 1114 if (len) 1115 db_printf(","); 1116 } 1117 db_printf("]\n"); 1118 } 1119 1120 void 1121 db_print_ifa(ifa) 1122 struct ifaddr *ifa; 1123 { 1124 if (ifa == 0) 1125 return; 1126 db_printf(" ifa_addr="); 1127 db_print_sa(ifa->ifa_addr); 1128 db_printf(" ifa_dsta="); 1129 db_print_sa(ifa->ifa_dstaddr); 1130 db_printf(" ifa_mask="); 1131 db_print_sa(ifa->ifa_netmask); 1132 db_printf(" flags=0x%x, refcnt=%d, metric=%d\n", 1133 ifa->ifa_flags, ifa->ifa_refcnt, ifa->ifa_metric); 1134 } 1135 1136 void 1137 db_print_llinfo(li) 1138 caddr_t li; 1139 { 1140 struct llinfo_arp *la; 1141 1142 if (li == 0) 1143 return; 1144 la = (struct llinfo_arp *)li; 1145 db_printf(" la_rt=%p la_hold_head=%p, la_asked=0x%lx\n", 1146 la->la_rt, la->la_hold_head, la->la_asked); 1147 } 1148 1149 /* 1150 * Function to pass to rn_walktree(). 1151 * Return non-zero error to abort walk. 1152 */ 1153 int 1154 db_show_radix_node(rn, w) 1155 struct radix_node *rn; 1156 void *w; 1157 { 1158 struct rtentry *rt = (struct rtentry *)rn; 1159 1160 db_printf("rtentry=%p", rt); 1161 1162 db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n", 1163 rt->rt_flags, rt->rt_refcnt, rt->rt_use, rt->rt_expire); 1164 1165 db_printf(" key="); db_print_sa(rt_key(rt)); 1166 db_printf(" mask="); db_print_sa(rt_mask(rt)); 1167 db_printf(" gw="); db_print_sa(rt->rt_gateway); 1168 1169 db_printf(" ifp=%p ", rt->rt_ifp); 1170 if (rt->rt_ifp) 1171 db_printf("(%s)", rt->rt_ifp->if_xname); 1172 else 1173 db_printf("(NULL)"); 1174 1175 db_printf(" ifa=%p\n", rt->rt_ifa); 1176 db_print_ifa(rt->rt_ifa); 1177 1178 db_printf(" genmask="); db_print_sa(rt->rt_genmask); 1179 1180 db_printf(" gwroute=%p llinfo=%p\n", rt->rt_gwroute, rt->rt_llinfo); 1181 db_print_llinfo(rt->rt_llinfo); 1182 return (0); 1183 } 1184 1185 /* 1186 * Function to print all the route trees. 1187 * Use this from ddb: "call db_show_arptab" 1188 */ 1189 int 1190 db_show_arptab() 1191 { 1192 struct radix_node_head *rnh; 1193 rnh = rt_gettable(AF_INET, 0); 1194 db_printf("Route tree for AF_INET\n"); 1195 if (rnh == NULL) { 1196 db_printf(" (not initialized)\n"); 1197 return (0); 1198 } 1199 rn_walktree(rnh, db_show_radix_node, NULL); 1200 return (0); 1201 } 1202 #endif 1203 #endif /* INET */ 1204