1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 30 */ 31 32 /* 33 * Ethernet address resolution protocol. 34 * TODO: 35 * add "inuse/lock" bit (or ref. count) along with valid bit 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include "opt_inet.h" 42 #include "opt_carp.h" 43 44 #include <sys/param.h> 45 #include <sys/kernel.h> 46 #include <sys/queue.h> 47 #include <sys/sysctl.h> 48 #include <sys/systm.h> 49 #include <sys/mbuf.h> 50 #include <sys/malloc.h> 51 #include <sys/proc.h> 52 #include <sys/socket.h> 53 #include <sys/syslog.h> 54 55 #include <net/if.h> 56 #include <net/if_dl.h> 57 #include <net/if_types.h> 58 #include <net/netisr.h> 59 #include <net/if_llc.h> 60 #include <net/ethernet.h> 61 #include <net/route.h> 62 #include <net/vnet.h> 63 64 #include <netinet/in.h> 65 #include <netinet/in_var.h> 66 #include <net/if_llatbl.h> 67 #include <netinet/if_ether.h> 68 69 #include <net/if_arc.h> 70 #include <net/iso88025.h> 71 72 #ifdef DEV_CARP 73 #include <netinet/ip_carp.h> 74 #endif 75 76 #include <security/mac/mac_framework.h> 77 78 #define SIN(s) ((struct sockaddr_in *)s) 79 #define SDL(s) ((struct sockaddr_dl *)s) 80 81 SYSCTL_DECL(_net_link_ether); 82 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 83 84 VNET_DEFINE(int, useloopback) = 1; /* use loopback interface for 85 * local traffic */ 86 87 /* timer values */ 88 static VNET_DEFINE(int, arpt_keep) = (20*60); /* once resolved, good for 20 89 * minutes */ 90 static VNET_DEFINE(int, arp_maxtries) = 5; 91 static VNET_DEFINE(int, arp_proxyall); 92 93 #define V_arpt_keep VNET(arpt_keep) 94 #define V_arp_maxtries VNET(arp_maxtries) 95 #define V_arp_proxyall VNET(arp_proxyall) 96 97 SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 98 &VNET_NAME(arpt_keep), 0, 99 "ARP entry lifetime in seconds"); 100 101 SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 102 &VNET_NAME(arp_maxtries), 0, 103 "ARP resolution attempts before returning error"); 104 SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 105 &VNET_NAME(useloopback), 0, 106 "Use the loopback interface for local traffic"); 107 SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 108 &VNET_NAME(arp_proxyall), 0, 109 "Enable proxy ARP for all suitable requests"); 110 111 static void arp_init(void); 112 void arprequest(struct ifnet *, 113 struct in_addr *, struct in_addr *, u_char *); 114 static void arpintr(struct mbuf *); 115 static void arptimer(void *); 116 #ifdef INET 117 static void in_arpinput(struct mbuf *); 118 #endif 119 120 static const struct netisr_handler arp_nh = { 121 .nh_name = "arp", 122 .nh_handler = arpintr, 123 .nh_proto = NETISR_ARP, 124 .nh_policy = NETISR_POLICY_SOURCE, 125 }; 126 127 #ifdef AF_INET 128 void arp_ifscrub(struct ifnet *ifp, uint32_t addr); 129 130 /* 131 * called by in_ifscrub to remove entry from the table when 132 * the interface goes away 133 */ 134 void 135 arp_ifscrub(struct ifnet *ifp, uint32_t addr) 136 { 137 struct sockaddr_in addr4; 138 139 bzero((void *)&addr4, sizeof(addr4)); 140 addr4.sin_len = sizeof(addr4); 141 addr4.sin_family = AF_INET; 142 addr4.sin_addr.s_addr = addr; 143 CURVNET_SET(ifp->if_vnet); 144 IF_AFDATA_LOCK(ifp); 145 lla_lookup(LLTABLE(ifp), (LLE_DELETE | LLE_IFADDR), 146 (struct sockaddr *)&addr4); 147 IF_AFDATA_UNLOCK(ifp); 148 CURVNET_RESTORE(); 149 } 150 #endif 151 152 /* 153 * Timeout routine. Age arp_tab entries periodically. 154 */ 155 static void 156 arptimer(void *arg) 157 { 158 struct ifnet *ifp; 159 struct llentry *lle = (struct llentry *)arg; 160 161 if (lle == NULL) { 162 panic("%s: NULL entry!\n", __func__); 163 return; 164 } 165 ifp = lle->lle_tbl->llt_ifp; 166 IF_AFDATA_LOCK(ifp); 167 LLE_WLOCK(lle); 168 if (((lle->la_flags & LLE_DELETED) 169 || (time_second >= lle->la_expire)) 170 && (!callout_pending(&lle->la_timer) && 171 callout_active(&lle->la_timer))) 172 (void) llentry_free(lle); 173 else { 174 /* 175 * Still valid, just drop our reference 176 */ 177 LLE_FREE_LOCKED(lle); 178 } 179 IF_AFDATA_UNLOCK(ifp); 180 } 181 182 /* 183 * Broadcast an ARP request. Caller specifies: 184 * - arp header source ip address 185 * - arp header target ip address 186 * - arp header source ethernet address 187 */ 188 void 189 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip, 190 u_char *enaddr) 191 { 192 struct mbuf *m; 193 struct arphdr *ah; 194 struct sockaddr sa; 195 196 if (sip == NULL) { 197 /* XXX don't believe this can happen (or explain why) */ 198 /* 199 * The caller did not supply a source address, try to find 200 * a compatible one among those assigned to this interface. 201 */ 202 struct ifaddr *ifa; 203 204 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 205 if (!ifa->ifa_addr || 206 ifa->ifa_addr->sa_family != AF_INET) 207 continue; 208 sip = &SIN(ifa->ifa_addr)->sin_addr; 209 if (0 == ((sip->s_addr ^ tip->s_addr) & 210 SIN(ifa->ifa_netmask)->sin_addr.s_addr) ) 211 break; /* found it. */ 212 } 213 if (sip == NULL) { 214 printf("%s: cannot find matching address\n", __func__); 215 return; 216 } 217 } 218 219 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 220 return; 221 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 222 2*ifp->if_data.ifi_addrlen; 223 m->m_pkthdr.len = m->m_len; 224 MH_ALIGN(m, m->m_len); 225 ah = mtod(m, struct arphdr *); 226 bzero((caddr_t)ah, m->m_len); 227 #ifdef MAC 228 mac_netinet_arp_send(ifp, m); 229 #endif 230 ah->ar_pro = htons(ETHERTYPE_IP); 231 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 232 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 233 ah->ar_op = htons(ARPOP_REQUEST); 234 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 235 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 236 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 237 sa.sa_family = AF_ARP; 238 sa.sa_len = 2; 239 m->m_flags |= M_BCAST; 240 (*ifp->if_output)(ifp, m, &sa, NULL); 241 } 242 243 /* 244 * Resolve an IP address into an ethernet address. 245 * On input: 246 * ifp is the interface we use 247 * rt0 is the route to the final destination (possibly useless) 248 * m is the mbuf. May be NULL if we don't have a packet. 249 * dst is the next hop, 250 * desten is where we want the address. 251 * 252 * On success, desten is filled in and the function returns 0; 253 * If the packet must be held pending resolution, we return EWOULDBLOCK 254 * On other errors, we return the corresponding error code. 255 * Note that m_freem() handles NULL. 256 */ 257 int 258 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 259 struct sockaddr *dst, u_char *desten, struct llentry **lle) 260 { 261 struct llentry *la = 0; 262 u_int flags = 0; 263 int error, renew; 264 265 *lle = NULL; 266 if (m != NULL) { 267 if (m->m_flags & M_BCAST) { 268 /* broadcast */ 269 (void)memcpy(desten, 270 ifp->if_broadcastaddr, ifp->if_addrlen); 271 return (0); 272 } 273 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) { 274 /* multicast */ 275 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 276 return (0); 277 } 278 } 279 /* XXXXX 280 */ 281 retry: 282 IF_AFDATA_RLOCK(ifp); 283 la = lla_lookup(LLTABLE(ifp), flags, dst); 284 IF_AFDATA_RUNLOCK(ifp); 285 if ((la == NULL) && ((flags & LLE_EXCLUSIVE) == 0) 286 && ((ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0)) { 287 flags |= (LLE_CREATE | LLE_EXCLUSIVE); 288 IF_AFDATA_WLOCK(ifp); 289 la = lla_lookup(LLTABLE(ifp), flags, dst); 290 IF_AFDATA_WUNLOCK(ifp); 291 } 292 if (la == NULL) { 293 if (flags & LLE_CREATE) 294 log(LOG_DEBUG, 295 "arpresolve: can't allocate llinfo for %s\n", 296 inet_ntoa(SIN(dst)->sin_addr)); 297 m_freem(m); 298 return (EINVAL); 299 } 300 301 if ((la->la_flags & LLE_VALID) && 302 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) { 303 bcopy(&la->ll_addr, desten, ifp->if_addrlen); 304 /* 305 * If entry has an expiry time and it is approaching, 306 * see if we need to send an ARP request within this 307 * arpt_down interval. 308 */ 309 if (!(la->la_flags & LLE_STATIC) && 310 time_uptime + la->la_preempt > la->la_expire) { 311 arprequest(ifp, NULL, 312 &SIN(dst)->sin_addr, IF_LLADDR(ifp)); 313 314 la->la_preempt--; 315 } 316 317 *lle = la; 318 error = 0; 319 goto done; 320 } 321 322 if (la->la_flags & LLE_STATIC) { /* should not happen! */ 323 log(LOG_DEBUG, "arpresolve: ouch, empty static llinfo for %s\n", 324 inet_ntoa(SIN(dst)->sin_addr)); 325 m_freem(m); 326 error = EINVAL; 327 goto done; 328 } 329 330 renew = (la->la_asked == 0 || la->la_expire != time_uptime); 331 if ((renew || m != NULL) && (flags & LLE_EXCLUSIVE) == 0) { 332 flags |= LLE_EXCLUSIVE; 333 LLE_RUNLOCK(la); 334 goto retry; 335 } 336 /* 337 * There is an arptab entry, but no ethernet address 338 * response yet. Replace the held mbuf with this 339 * latest one. 340 */ 341 if (m != NULL) { 342 if (la->la_hold != NULL) 343 m_freem(la->la_hold); 344 la->la_hold = m; 345 if (renew == 0 && (flags & LLE_EXCLUSIVE)) { 346 flags &= ~LLE_EXCLUSIVE; 347 LLE_DOWNGRADE(la); 348 } 349 350 } 351 /* 352 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 353 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 354 * if we have already sent arp_maxtries ARP requests. Retransmit the 355 * ARP request, but not faster than one request per second. 356 */ 357 if (la->la_asked < V_arp_maxtries) 358 error = EWOULDBLOCK; /* First request. */ 359 else 360 error = 361 (rt0->rt_flags & RTF_GATEWAY) ? EHOSTDOWN : EHOSTUNREACH; 362 363 if (renew) { 364 LLE_ADDREF(la); 365 la->la_expire = time_uptime; 366 callout_reset(&la->la_timer, hz, arptimer, la); 367 la->la_asked++; 368 LLE_WUNLOCK(la); 369 arprequest(ifp, NULL, &SIN(dst)->sin_addr, 370 IF_LLADDR(ifp)); 371 return (error); 372 } 373 done: 374 if (flags & LLE_EXCLUSIVE) 375 LLE_WUNLOCK(la); 376 else 377 LLE_RUNLOCK(la); 378 return (error); 379 } 380 381 /* 382 * Common length and type checks are done here, 383 * then the protocol-specific routine is called. 384 */ 385 static void 386 arpintr(struct mbuf *m) 387 { 388 struct arphdr *ar; 389 390 if (m->m_len < sizeof(struct arphdr) && 391 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 392 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 393 return; 394 } 395 ar = mtod(m, struct arphdr *); 396 397 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 398 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 399 ntohs(ar->ar_hrd) != ARPHRD_ARCNET && 400 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) { 401 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 402 (unsigned char *)&ar->ar_hrd, ""); 403 m_freem(m); 404 return; 405 } 406 407 if (m->m_len < arphdr_len(ar)) { 408 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 409 log(LOG_ERR, "arp: runt packet\n"); 410 m_freem(m); 411 return; 412 } 413 ar = mtod(m, struct arphdr *); 414 } 415 416 switch (ntohs(ar->ar_pro)) { 417 #ifdef INET 418 case ETHERTYPE_IP: 419 in_arpinput(m); 420 return; 421 #endif 422 } 423 m_freem(m); 424 } 425 426 #ifdef INET 427 /* 428 * ARP for Internet protocols on 10 Mb/s Ethernet. 429 * Algorithm is that given in RFC 826. 430 * In addition, a sanity check is performed on the sender 431 * protocol address, to catch impersonators. 432 * We no longer handle negotiations for use of trailer protocol: 433 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 434 * along with IP replies if we wanted trailers sent to us, 435 * and also sent them in response to IP replies. 436 * This allowed either end to announce the desire to receive 437 * trailer packets. 438 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 439 * but formerly didn't normally send requests. 440 */ 441 static int log_arp_wrong_iface = 1; 442 static int log_arp_movements = 1; 443 static int log_arp_permanent_modify = 1; 444 445 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 446 &log_arp_wrong_iface, 0, 447 "log arp packets arriving on the wrong interface"); 448 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 449 &log_arp_movements, 0, 450 "log arp replies from MACs different than the one in the cache"); 451 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW, 452 &log_arp_permanent_modify, 0, 453 "log arp replies from MACs different than the one in the permanent arp entry"); 454 455 456 static void 457 in_arpinput(struct mbuf *m) 458 { 459 struct arphdr *ah; 460 struct ifnet *ifp = m->m_pkthdr.rcvif; 461 struct llentry *la = NULL; 462 struct rtentry *rt; 463 struct ifaddr *ifa; 464 struct in_ifaddr *ia; 465 struct mbuf *hold; 466 struct sockaddr sa; 467 struct in_addr isaddr, itaddr, myaddr; 468 u_int8_t *enaddr = NULL; 469 int op, flags; 470 int req_len; 471 int bridged = 0, is_bridge = 0; 472 #ifdef DEV_CARP 473 int carp_match = 0; 474 #endif 475 struct sockaddr_in sin; 476 sin.sin_len = sizeof(struct sockaddr_in); 477 sin.sin_family = AF_INET; 478 sin.sin_addr.s_addr = 0; 479 480 if (ifp->if_bridge) 481 bridged = 1; 482 if (ifp->if_type == IFT_BRIDGE) 483 is_bridge = 1; 484 485 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 486 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 487 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 488 return; 489 } 490 491 ah = mtod(m, struct arphdr *); 492 op = ntohs(ah->ar_op); 493 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 494 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 495 496 /* 497 * For a bridge, we want to check the address irrespective 498 * of the receive interface. (This will change slightly 499 * when we have clusters of interfaces). 500 * If the interface does not match, but the recieving interface 501 * is part of carp, we call carp_iamatch to see if this is a 502 * request for the virtual host ip. 503 * XXX: This is really ugly! 504 */ 505 IN_IFADDR_RLOCK(); 506 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 507 if (((bridged && ia->ia_ifp->if_bridge != NULL) || 508 ia->ia_ifp == ifp) && 509 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 510 ifa_ref(&ia->ia_ifa); 511 IN_IFADDR_RUNLOCK(); 512 goto match; 513 } 514 #ifdef DEV_CARP 515 if (ifp->if_carp != NULL && 516 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) && 517 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 518 carp_match = 1; 519 ifa_ref(&ia->ia_ifa); 520 IN_IFADDR_RUNLOCK(); 521 goto match; 522 } 523 #endif 524 } 525 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 526 if (((bridged && ia->ia_ifp->if_bridge != NULL) || 527 ia->ia_ifp == ifp) && 528 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 529 ifa_ref(&ia->ia_ifa); 530 IN_IFADDR_RUNLOCK(); 531 goto match; 532 } 533 534 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \ 535 (ia->ia_ifp->if_bridge == ifp->if_softc && \ 536 !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \ 537 addr == ia->ia_addr.sin_addr.s_addr) 538 /* 539 * Check the case when bridge shares its MAC address with 540 * some of its children, so packets are claimed by bridge 541 * itself (bridge_input() does it first), but they are really 542 * meant to be destined to the bridge member. 543 */ 544 if (is_bridge) { 545 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 546 if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) { 547 ifa_ref(&ia->ia_ifa); 548 ifp = ia->ia_ifp; 549 IN_IFADDR_RUNLOCK(); 550 goto match; 551 } 552 } 553 } 554 #undef BDG_MEMBER_MATCHES_ARP 555 IN_IFADDR_RUNLOCK(); 556 557 /* 558 * No match, use the first inet address on the receive interface 559 * as a dummy address for the rest of the function. 560 */ 561 IF_ADDR_LOCK(ifp); 562 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 563 if (ifa->ifa_addr->sa_family == AF_INET) { 564 ia = ifatoia(ifa); 565 ifa_ref(ifa); 566 IF_ADDR_UNLOCK(ifp); 567 goto match; 568 } 569 IF_ADDR_UNLOCK(ifp); 570 571 /* 572 * If bridging, fall back to using any inet address. 573 */ 574 IN_IFADDR_RLOCK(); 575 if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) { 576 IN_IFADDR_RUNLOCK(); 577 goto drop; 578 } 579 ifa_ref(&ia->ia_ifa); 580 IN_IFADDR_RUNLOCK(); 581 match: 582 if (!enaddr) 583 enaddr = (u_int8_t *)IF_LLADDR(ifp); 584 myaddr = ia->ia_addr.sin_addr; 585 ifa_free(&ia->ia_ifa); 586 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 587 goto drop; /* it's from me, ignore it. */ 588 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 589 log(LOG_ERR, 590 "arp: link address is broadcast for IP address %s!\n", 591 inet_ntoa(isaddr)); 592 goto drop; 593 } 594 /* 595 * Warn if another host is using the same IP address, but only if the 596 * IP address isn't 0.0.0.0, which is used for DHCP only, in which 597 * case we suppress the warning to avoid false positive complaints of 598 * potential misconfiguration. 599 */ 600 if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { 601 log(LOG_ERR, 602 "arp: %*D is using my IP address %s on %s!\n", 603 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 604 inet_ntoa(isaddr), ifp->if_xname); 605 itaddr = myaddr; 606 goto reply; 607 } 608 if (ifp->if_flags & IFF_STATICARP) 609 goto reply; 610 611 bzero(&sin, sizeof(sin)); 612 sin.sin_len = sizeof(struct sockaddr_in); 613 sin.sin_family = AF_INET; 614 sin.sin_addr = isaddr; 615 flags = (itaddr.s_addr == myaddr.s_addr) ? LLE_CREATE : 0; 616 flags |= LLE_EXCLUSIVE; 617 IF_AFDATA_LOCK(ifp); 618 la = lla_lookup(LLTABLE(ifp), flags, (struct sockaddr *)&sin); 619 IF_AFDATA_UNLOCK(ifp); 620 if (la != NULL) { 621 /* the following is not an error when doing bridging */ 622 if (!bridged && la->lle_tbl->llt_ifp != ifp 623 #ifdef DEV_CARP 624 && (ifp->if_type != IFT_CARP || !carp_match) 625 #endif 626 ) { 627 if (log_arp_wrong_iface) 628 log(LOG_ERR, "arp: %s is on %s " 629 "but got reply from %*D on %s\n", 630 inet_ntoa(isaddr), 631 la->lle_tbl->llt_ifp->if_xname, 632 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 633 ifp->if_xname); 634 LLE_WUNLOCK(la); 635 goto reply; 636 } 637 if ((la->la_flags & LLE_VALID) && 638 bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) { 639 if (la->la_flags & LLE_STATIC) { 640 LLE_WUNLOCK(la); 641 log(LOG_ERR, 642 "arp: %*D attempts to modify permanent " 643 "entry for %s on %s\n", 644 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 645 inet_ntoa(isaddr), ifp->if_xname); 646 goto reply; 647 } 648 if (log_arp_movements) { 649 log(LOG_INFO, "arp: %s moved from %*D " 650 "to %*D on %s\n", 651 inet_ntoa(isaddr), 652 ifp->if_addrlen, 653 (u_char *)&la->ll_addr, ":", 654 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 655 ifp->if_xname); 656 } 657 } 658 659 if (ifp->if_addrlen != ah->ar_hln) { 660 LLE_WUNLOCK(la); 661 log(LOG_WARNING, 662 "arp from %*D: addr len: new %d, i/f %d (ignored)", 663 ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 664 ah->ar_hln, ifp->if_addrlen); 665 goto reply; 666 } 667 (void)memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen); 668 la->la_flags |= LLE_VALID; 669 670 if (!(la->la_flags & LLE_STATIC)) { 671 la->la_expire = time_uptime + V_arpt_keep; 672 callout_reset(&la->la_timer, hz * V_arpt_keep, 673 arptimer, la); 674 } 675 la->la_asked = 0; 676 la->la_preempt = V_arp_maxtries; 677 hold = la->la_hold; 678 if (hold != NULL) { 679 la->la_hold = NULL; 680 memcpy(&sa, L3_ADDR(la), sizeof(sa)); 681 } 682 LLE_WUNLOCK(la); 683 if (hold != NULL) 684 (*ifp->if_output)(ifp, hold, &sa, NULL); 685 } 686 reply: 687 if (op != ARPOP_REQUEST) 688 goto drop; 689 690 if (itaddr.s_addr == myaddr.s_addr) { 691 /* Shortcut.. the receiving interface is the target. */ 692 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 693 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 694 } else { 695 struct llentry *lle = NULL; 696 697 if (!V_arp_proxyall) 698 goto drop; 699 700 sin.sin_addr = itaddr; 701 /* XXX MRT use table 0 for arp reply */ 702 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 703 if (!rt) 704 goto drop; 705 706 /* 707 * Don't send proxies for nodes on the same interface 708 * as this one came out of, or we'll get into a fight 709 * over who claims what Ether address. 710 */ 711 if (!rt->rt_ifp || rt->rt_ifp == ifp) { 712 RTFREE_LOCKED(rt); 713 goto drop; 714 } 715 IF_AFDATA_LOCK(rt->rt_ifp); 716 lle = lla_lookup(LLTABLE(rt->rt_ifp), 0, (struct sockaddr *)&sin); 717 IF_AFDATA_UNLOCK(rt->rt_ifp); 718 RTFREE_LOCKED(rt); 719 720 if (lle != NULL) { 721 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 722 (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln); 723 LLE_RUNLOCK(lle); 724 } else 725 goto drop; 726 727 /* 728 * Also check that the node which sent the ARP packet 729 * is on the the interface we expect it to be on. This 730 * avoids ARP chaos if an interface is connected to the 731 * wrong network. 732 */ 733 sin.sin_addr = isaddr; 734 735 /* XXX MRT use table 0 for arp checks */ 736 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 737 if (!rt) 738 goto drop; 739 if (rt->rt_ifp != ifp) { 740 log(LOG_INFO, "arp_proxy: ignoring request" 741 " from %s via %s, expecting %s\n", 742 inet_ntoa(isaddr), ifp->if_xname, 743 rt->rt_ifp->if_xname); 744 RTFREE_LOCKED(rt); 745 goto drop; 746 } 747 RTFREE_LOCKED(rt); 748 749 #ifdef DEBUG_PROXY 750 printf("arp: proxying for %s\n", 751 inet_ntoa(itaddr)); 752 #endif 753 } 754 755 if (itaddr.s_addr == myaddr.s_addr && 756 IN_LINKLOCAL(ntohl(itaddr.s_addr))) { 757 /* RFC 3927 link-local IPv4; always reply by broadcast. */ 758 #ifdef DEBUG_LINKLOCAL 759 printf("arp: sending reply for link-local addr %s\n", 760 inet_ntoa(itaddr)); 761 #endif 762 m->m_flags |= M_BCAST; 763 m->m_flags &= ~M_MCAST; 764 } else { 765 /* default behaviour; never reply by broadcast. */ 766 m->m_flags &= ~(M_BCAST|M_MCAST); 767 } 768 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 769 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 770 ah->ar_op = htons(ARPOP_REPLY); 771 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 772 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 773 m->m_pkthdr.len = m->m_len; 774 sa.sa_family = AF_ARP; 775 sa.sa_len = 2; 776 (*ifp->if_output)(ifp, m, &sa, NULL); 777 return; 778 779 drop: 780 m_freem(m); 781 } 782 #endif 783 784 void 785 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 786 { 787 struct llentry *lle; 788 789 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) { 790 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 791 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 792 /* 793 * interface address is considered static entry 794 * because the output of the arp utility shows 795 * that L2 entry as permanent 796 */ 797 IF_AFDATA_LOCK(ifp); 798 lle = lla_lookup(LLTABLE(ifp), (LLE_CREATE | LLE_IFADDR | LLE_STATIC), 799 (struct sockaddr *)IA_SIN(ifa)); 800 IF_AFDATA_UNLOCK(ifp); 801 if (lle == NULL) 802 log(LOG_INFO, "arp_ifinit: cannot create arp " 803 "entry for interface address\n"); 804 else 805 LLE_RUNLOCK(lle); 806 } 807 ifa->ifa_rtrequest = NULL; 808 } 809 810 void 811 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr) 812 { 813 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 814 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 815 &IA_SIN(ifa)->sin_addr, enaddr); 816 ifa->ifa_rtrequest = NULL; 817 } 818 819 static void 820 arp_init(void) 821 { 822 823 netisr_register(&arp_nh); 824 } 825 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 826