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 * $FreeBSD$ 31 */ 32 33 /* 34 * Ethernet address resolution protocol. 35 * TODO: 36 * add "inuse/lock" bit (or ref. count) along with valid bit 37 */ 38 39 #include "opt_inet.h" 40 #include "opt_bdg.h" 41 #include "opt_mac.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/mac.h> 50 #include <sys/mbuf.h> 51 #include <sys/malloc.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/route.h> 59 #include <net/netisr.h> 60 #include <net/if_llc.h> 61 #include <net/ethernet.h> 62 #include <net/bridge.h> 63 64 #include <netinet/in.h> 65 #include <netinet/in_var.h> 66 #include <netinet/if_ether.h> 67 68 #include <net/if_arc.h> 69 #include <net/iso88025.h> 70 71 #ifdef DEV_CARP 72 #include <netinet/ip_carp.h> 73 #endif 74 75 #define SIN(s) ((struct sockaddr_in *)s) 76 #define SDL(s) ((struct sockaddr_dl *)s) 77 78 SYSCTL_DECL(_net_link_ether); 79 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 80 81 /* timer values */ 82 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 83 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 84 static int arpt_down = 20; /* once declared down, don't send for 20 sec */ 85 86 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, 87 &arpt_prune, 0, ""); 88 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 89 &arpt_keep, 0, ""); 90 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, 91 &arpt_down, 0, ""); 92 93 #define rt_expire rt_rmx.rmx_expire 94 95 struct llinfo_arp { 96 LIST_ENTRY(llinfo_arp) la_le; 97 struct rtentry *la_rt; 98 struct mbuf *la_hold; /* last packet until resolved/timeout */ 99 u_short la_preempt; /* countdown for pre-expiry arps */ 100 u_short la_asked; /* #times we QUERIED following expiration */ 101 #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */ 102 }; 103 104 static LIST_HEAD(, llinfo_arp) llinfo_arp; 105 106 static struct ifqueue arpintrq; 107 static int arp_allocated; 108 109 static int arp_maxtries = 5; 110 static int useloopback = 1; /* use loopback interface for local traffic */ 111 static int arp_proxyall = 0; 112 static struct callout arp_callout; 113 114 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 115 &arp_maxtries, 0, ""); 116 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 117 &useloopback, 0, ""); 118 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 119 &arp_proxyall, 0, ""); 120 121 static void arp_init(void); 122 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 123 static void arprequest(struct ifnet *, 124 struct in_addr *, struct in_addr *, u_char *); 125 static void arpintr(struct mbuf *); 126 static void arptfree(struct llinfo_arp *); 127 static void arptimer(void *); 128 static struct llinfo_arp 129 *arplookup(u_long, int, int); 130 #ifdef INET 131 static void in_arpinput(struct mbuf *); 132 #endif 133 134 /* 135 * Timeout routine. Age arp_tab entries periodically. 136 */ 137 /* ARGSUSED */ 138 static void 139 arptimer(ignored_arg) 140 void *ignored_arg; 141 { 142 struct llinfo_arp *la, *ola; 143 144 RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]); 145 la = LIST_FIRST(&llinfo_arp); 146 while (la != NULL) { 147 struct rtentry *rt = la->la_rt; 148 ola = la; 149 la = LIST_NEXT(la, la_le); 150 if (rt->rt_expire && rt->rt_expire <= time_second) 151 arptfree(ola); /* timer has expired, clear */ 152 } 153 RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]); 154 155 callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL); 156 } 157 158 /* 159 * Parallel to llc_rtrequest. 160 */ 161 static void 162 arp_rtrequest(req, rt, info) 163 int req; 164 struct rtentry *rt; 165 struct rt_addrinfo *info; 166 { 167 struct sockaddr *gate; 168 struct llinfo_arp *la; 169 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 170 struct in_ifaddr *ia; 171 struct ifaddr *ifa; 172 173 RT_LOCK_ASSERT(rt); 174 175 if (rt->rt_flags & RTF_GATEWAY) 176 return; 177 gate = rt->rt_gateway; 178 la = (struct llinfo_arp *)rt->rt_llinfo; 179 switch (req) { 180 181 case RTM_ADD: 182 /* 183 * XXX: If this is a manually added route to interface 184 * such as older version of routed or gated might provide, 185 * restore cloning bit. 186 */ 187 if ((rt->rt_flags & RTF_HOST) == 0 && 188 rt_mask(rt) != NULL && 189 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 190 rt->rt_flags |= RTF_CLONING; 191 if (rt->rt_flags & RTF_CLONING) { 192 /* 193 * Case 1: This route should come from a route to iface. 194 */ 195 rt_setgate(rt, rt_key(rt), 196 (struct sockaddr *)&null_sdl); 197 gate = rt->rt_gateway; 198 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 199 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 200 rt->rt_expire = time_second; 201 break; 202 } 203 /* Announce a new entry if requested. */ 204 if (rt->rt_flags & RTF_ANNOUNCE) 205 arprequest(rt->rt_ifp, 206 &SIN(rt_key(rt))->sin_addr, 207 &SIN(rt_key(rt))->sin_addr, 208 (u_char *)LLADDR(SDL(gate))); 209 /*FALLTHROUGH*/ 210 case RTM_RESOLVE: 211 if (gate->sa_family != AF_LINK || 212 gate->sa_len < sizeof(null_sdl)) { 213 log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__, 214 inet_ntoa(SIN(rt_key(rt))->sin_addr), 215 (gate->sa_family != AF_LINK) ? 216 " (!AF_LINK)": ""); 217 break; 218 } 219 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 220 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 221 if (la != 0) 222 break; /* This happens on a route change */ 223 /* 224 * Case 2: This route may come from cloning, or a manual route 225 * add with a LL address. 226 */ 227 R_Zalloc(la, struct llinfo_arp *, sizeof(*la)); 228 rt->rt_llinfo = (caddr_t)la; 229 if (la == 0) { 230 log(LOG_DEBUG, "%s: malloc failed\n", __func__); 231 break; 232 } 233 arp_allocated++; 234 la->la_rt = rt; 235 rt->rt_flags |= RTF_LLINFO; 236 RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]); 237 LIST_INSERT_HEAD(&llinfo_arp, la, la_le); 238 239 #ifdef INET 240 /* 241 * This keeps the multicast addresses from showing up 242 * in `arp -a' listings as unresolved. It's not actually 243 * functional. Then the same for broadcast. 244 */ 245 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) && 246 rt->rt_ifp->if_type != IFT_ARCNET) { 247 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 248 LLADDR(SDL(gate))); 249 SDL(gate)->sdl_alen = 6; 250 rt->rt_expire = 0; 251 } 252 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 253 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, 254 rt->rt_ifp->if_addrlen); 255 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; 256 rt->rt_expire = 0; 257 } 258 #endif 259 260 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 261 if (ia->ia_ifp == rt->rt_ifp && 262 SIN(rt_key(rt))->sin_addr.s_addr == 263 (IA_SIN(ia))->sin_addr.s_addr) 264 break; 265 } 266 if (ia) { 267 /* 268 * This test used to be 269 * if (loif.if_flags & IFF_UP) 270 * It allowed local traffic to be forced 271 * through the hardware by configuring the loopback down. 272 * However, it causes problems during network configuration 273 * for boards that can't receive packets they send. 274 * It is now necessary to clear "useloopback" and remove 275 * the route to force traffic out to the hardware. 276 */ 277 rt->rt_expire = 0; 278 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 279 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 280 if (useloopback) 281 rt->rt_ifp = loif; 282 283 /* 284 * make sure to set rt->rt_ifa to the interface 285 * address we are using, otherwise we will have trouble 286 * with source address selection. 287 */ 288 ifa = &ia->ia_ifa; 289 if (ifa != rt->rt_ifa) { 290 IFAFREE(rt->rt_ifa); 291 IFAREF(ifa); 292 rt->rt_ifa = ifa; 293 } 294 } 295 break; 296 297 case RTM_DELETE: 298 if (la == 0) 299 break; 300 RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]); 301 LIST_REMOVE(la, la_le); 302 rt->rt_llinfo = 0; 303 rt->rt_flags &= ~RTF_LLINFO; 304 if (la->la_hold) 305 m_freem(la->la_hold); 306 Free((caddr_t)la); 307 } 308 } 309 310 /* 311 * Broadcast an ARP request. Caller specifies: 312 * - arp header source ip address 313 * - arp header target ip address 314 * - arp header source ethernet address 315 */ 316 static void 317 arprequest(ifp, sip, tip, enaddr) 318 struct ifnet *ifp; 319 struct in_addr *sip, *tip; 320 u_char *enaddr; 321 { 322 struct mbuf *m; 323 struct arphdr *ah; 324 struct sockaddr sa; 325 326 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 327 return; 328 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 329 2*ifp->if_data.ifi_addrlen; 330 m->m_pkthdr.len = m->m_len; 331 MH_ALIGN(m, m->m_len); 332 ah = mtod(m, struct arphdr *); 333 bzero((caddr_t)ah, m->m_len); 334 #ifdef MAC 335 mac_create_mbuf_linklayer(ifp, m); 336 #endif 337 ah->ar_pro = htons(ETHERTYPE_IP); 338 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 339 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 340 ah->ar_op = htons(ARPOP_REQUEST); 341 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 342 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 343 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 344 sa.sa_family = AF_ARP; 345 sa.sa_len = 2; 346 m->m_flags |= M_BCAST; 347 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 348 349 return; 350 } 351 352 /* 353 * Resolve an IP address into an ethernet address. 354 * On input: 355 * ifp is the interface we use 356 * dst is the next hop, 357 * rt0 is the route to the final destination (possibly useless) 358 * m is the mbuf 359 * desten is where we want the address. 360 * 361 * On success, desten is filled in and the function returns 0; 362 * If the packet must be held pending resolution, we return EWOULDBLOCK 363 * On other errors, we return the corresponding error code. 364 */ 365 int 366 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 367 struct sockaddr *dst, u_char *desten) 368 { 369 struct llinfo_arp *la = 0; 370 struct sockaddr_dl *sdl; 371 int error; 372 struct rtentry *rt; 373 374 error = rt_check(&rt, &rt0, dst); 375 if (error) { 376 m_freem(m); 377 return error; 378 } 379 380 if (m->m_flags & M_BCAST) { /* broadcast */ 381 (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen); 382 return (0); 383 } 384 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */ 385 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 386 return (0); 387 } 388 if (rt) 389 la = (struct llinfo_arp *)rt->rt_llinfo; 390 if (la == 0) { 391 la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 392 if (la) 393 rt = la->la_rt; 394 } 395 if (la == 0 || rt == 0) { 396 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", 397 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "", 398 rt ? "rt" : ""); 399 m_freem(m); 400 return (EINVAL); /* XXX */ 401 } 402 sdl = SDL(rt->rt_gateway); 403 /* 404 * Check the address family and length is valid, the address 405 * is resolved; otherwise, try to resolve. 406 */ 407 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && 408 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 409 /* 410 * If entry has an expiry time and it is approaching, 411 * see if we need to send an ARP request within this 412 * arpt_down interval. 413 */ 414 if ((rt->rt_expire != 0) && 415 (time_second + la->la_preempt > rt->rt_expire)) { 416 arprequest(ifp, 417 &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 418 &SIN(dst)->sin_addr, 419 IF_LLADDR(ifp)); 420 la->la_preempt--; 421 } 422 423 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 424 return (0); 425 } 426 /* 427 * If ARP is disabled or static on this interface, stop. 428 * XXX 429 * Probably should not allocate empty llinfo struct if we are 430 * not going to be sending out an arp request. 431 */ 432 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) { 433 m_freem(m); 434 return (EINVAL); 435 } 436 /* 437 * There is an arptab entry, but no ethernet address 438 * response yet. Replace the held mbuf with this 439 * latest one. 440 */ 441 if (la->la_hold) 442 m_freem(la->la_hold); 443 la->la_hold = m; 444 if (rt->rt_expire) { 445 RT_LOCK(rt); 446 rt->rt_flags &= ~RTF_REJECT; 447 if (la->la_asked == 0 || rt->rt_expire != time_second) { 448 rt->rt_expire = time_second; 449 if (la->la_asked++ < arp_maxtries) { 450 arprequest(ifp, 451 &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 452 &SIN(dst)->sin_addr, 453 IF_LLADDR(ifp)); 454 } else { 455 rt->rt_flags |= RTF_REJECT; 456 rt->rt_expire += arpt_down; 457 la->la_asked = 0; 458 la->la_preempt = arp_maxtries; 459 } 460 461 } 462 RT_UNLOCK(rt); 463 } 464 return (EWOULDBLOCK); 465 } 466 467 /* 468 * Common length and type checks are done here, 469 * then the protocol-specific routine is called. 470 */ 471 static void 472 arpintr(struct mbuf *m) 473 { 474 struct arphdr *ar; 475 476 if (m->m_len < sizeof(struct arphdr) && 477 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 478 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 479 return; 480 } 481 ar = mtod(m, struct arphdr *); 482 483 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 484 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 485 ntohs(ar->ar_hrd) != ARPHRD_ARCNET && 486 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) { 487 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 488 (unsigned char *)&ar->ar_hrd, ""); 489 m_freem(m); 490 return; 491 } 492 493 if (m->m_len < arphdr_len(ar)) { 494 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 495 log(LOG_ERR, "arp: runt packet\n"); 496 m_freem(m); 497 return; 498 } 499 ar = mtod(m, struct arphdr *); 500 } 501 502 switch (ntohs(ar->ar_pro)) { 503 #ifdef INET 504 case ETHERTYPE_IP: 505 in_arpinput(m); 506 return; 507 #endif 508 } 509 m_freem(m); 510 } 511 512 #ifdef INET 513 /* 514 * ARP for Internet protocols on 10 Mb/s 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 static int log_arp_wrong_iface = 1; 528 static int log_arp_movements = 1; 529 530 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 531 &log_arp_wrong_iface, 0, 532 "log arp packets arriving on the wrong interface"); 533 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 534 &log_arp_movements, 0, 535 "log arp replies from MACs different than the one in the cache"); 536 537 538 static void 539 in_arpinput(m) 540 struct mbuf *m; 541 { 542 struct arphdr *ah; 543 struct ifnet *ifp = m->m_pkthdr.rcvif; 544 struct iso88025_header *th = (struct iso88025_header *)0; 545 struct iso88025_sockaddr_dl_data *trld; 546 struct llinfo_arp *la = 0; 547 struct rtentry *rt; 548 struct ifaddr *ifa; 549 struct in_ifaddr *ia; 550 struct sockaddr_dl *sdl; 551 struct sockaddr sa; 552 struct in_addr isaddr, itaddr, myaddr; 553 u_int8_t *enaddr = NULL; 554 int op, rif_len; 555 int req_len; 556 557 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 558 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 559 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 560 return; 561 } 562 563 ah = mtod(m, struct arphdr *); 564 op = ntohs(ah->ar_op); 565 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 566 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 567 568 /* 569 * For a bridge, we want to check the address irrespective 570 * of the receive interface. (This will change slightly 571 * when we have clusters of interfaces). 572 * If the interface does not match, but the recieving interface 573 * is part of carp, we call carp_iamatch to see if this is a 574 * request for the virtual host ip. 575 * XXX: This is really ugly! 576 */ 577 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) 578 if ((do_bridge || (ia->ia_ifp == ifp) 579 #ifdef DEV_CARP 580 || (ifp->if_carp 581 && carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr)) 582 #endif 583 ) && itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 584 goto match; 585 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 586 if ((do_bridge || (ia->ia_ifp == ifp)) && 587 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 588 goto match; 589 /* 590 * No match, use the first inet address on the receive interface 591 * as a dummy address for the rest of the function. 592 */ 593 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 594 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 595 ia = ifatoia(ifa); 596 goto match; 597 } 598 /* 599 * If bridging, fall back to using any inet address. 600 */ 601 if (!do_bridge || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) 602 goto drop; 603 match: 604 if (!enaddr) 605 enaddr = (u_int8_t *)IF_LLADDR(ifp); 606 myaddr = ia->ia_addr.sin_addr; 607 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 608 goto drop; /* it's from me, ignore it. */ 609 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 610 log(LOG_ERR, 611 "arp: link address is broadcast for IP address %s!\n", 612 inet_ntoa(isaddr)); 613 goto drop; 614 } 615 /* 616 * Warn if another host is using the same IP address, but only if the 617 * IP address isn't 0.0.0.0, which is used for DHCP only, in which 618 * case we suppress the warning to avoid false positive complaints of 619 * potential misconfiguration. 620 */ 621 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { 622 log(LOG_ERR, 623 "arp: %*D is using my IP address %s!\n", 624 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 625 inet_ntoa(isaddr)); 626 itaddr = myaddr; 627 goto reply; 628 } 629 if (ifp->if_flags & IFF_STATICARP) 630 goto reply; 631 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 632 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 633 /* the following is not an error when doing bridging */ 634 if (!do_bridge && rt->rt_ifp != ifp) { 635 if (log_arp_wrong_iface) 636 log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n", 637 inet_ntoa(isaddr), 638 rt->rt_ifp->if_xname, 639 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 640 ifp->if_xname); 641 goto reply; 642 } 643 if (sdl->sdl_alen && 644 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 645 if (rt->rt_expire) { 646 if (log_arp_movements) 647 log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n", 648 inet_ntoa(isaddr), 649 ifp->if_addrlen, (u_char *)LLADDR(sdl), ":", 650 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 651 ifp->if_xname); 652 } else { 653 log(LOG_ERR, 654 "arp: %*D attempts to modify permanent entry for %s on %s\n", 655 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 656 inet_ntoa(isaddr), ifp->if_xname); 657 goto reply; 658 } 659 } 660 /* 661 * sanity check for the address length. 662 * XXX this does not work for protocols with variable address 663 * length. -is 664 */ 665 if (sdl->sdl_alen && 666 sdl->sdl_alen != ah->ar_hln) { 667 log(LOG_WARNING, 668 "arp from %*D: new addr len %d, was %d", 669 ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 670 ah->ar_hln, sdl->sdl_alen); 671 } 672 if (ifp->if_addrlen != ah->ar_hln) { 673 log(LOG_WARNING, 674 "arp from %*D: addr len: new %d, i/f %d (ignored)", 675 ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 676 ah->ar_hln, ifp->if_addrlen); 677 goto reply; 678 } 679 (void)memcpy(LLADDR(sdl), ar_sha(ah), 680 sdl->sdl_alen = ah->ar_hln); 681 /* 682 * If we receive an arp from a token-ring station over 683 * a token-ring nic then try to save the source 684 * routing info. 685 */ 686 if (ifp->if_type == IFT_ISO88025) { 687 th = (struct iso88025_header *)m->m_pkthdr.header; 688 trld = SDL_ISO88025(sdl); 689 rif_len = TR_RCF_RIFLEN(th->rcf); 690 if ((th->iso88025_shost[0] & TR_RII) && 691 (rif_len > 2)) { 692 trld->trld_rcf = th->rcf; 693 trld->trld_rcf ^= htons(TR_RCF_DIR); 694 memcpy(trld->trld_route, th->rd, rif_len - 2); 695 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK); 696 /* 697 * Set up source routing information for 698 * reply packet (XXX) 699 */ 700 m->m_data -= rif_len; 701 m->m_len += rif_len; 702 m->m_pkthdr.len += rif_len; 703 } else { 704 th->iso88025_shost[0] &= ~TR_RII; 705 trld->trld_rcf = 0; 706 } 707 m->m_data -= 8; 708 m->m_len += 8; 709 m->m_pkthdr.len += 8; 710 th->rcf = trld->trld_rcf; 711 } 712 RT_LOCK(rt); 713 if (rt->rt_expire) 714 rt->rt_expire = time_second + arpt_keep; 715 rt->rt_flags &= ~RTF_REJECT; 716 RT_UNLOCK(rt); 717 la->la_asked = 0; 718 la->la_preempt = arp_maxtries; 719 if (la->la_hold) { 720 (*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt); 721 la->la_hold = 0; 722 } 723 } 724 reply: 725 if (op != ARPOP_REQUEST) 726 goto drop; 727 if (itaddr.s_addr == myaddr.s_addr) { 728 /* I am the target */ 729 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 730 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 731 } else { 732 la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 733 if (la == NULL) { 734 struct sockaddr_in sin; 735 736 if (!arp_proxyall) 737 goto drop; 738 739 bzero(&sin, sizeof sin); 740 sin.sin_family = AF_INET; 741 sin.sin_len = sizeof sin; 742 sin.sin_addr = itaddr; 743 744 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 745 if (!rt) 746 goto drop; 747 /* 748 * Don't send proxies for nodes on the same interface 749 * as this one came out of, or we'll get into a fight 750 * over who claims what Ether address. 751 */ 752 if (rt->rt_ifp == ifp) { 753 rtfree(rt); 754 goto drop; 755 } 756 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 757 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 758 rtfree(rt); 759 760 /* 761 * Also check that the node which sent the ARP packet 762 * is on the the interface we expect it to be on. This 763 * avoids ARP chaos if an interface is connected to the 764 * wrong network. 765 */ 766 sin.sin_addr = isaddr; 767 768 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 769 if (!rt) 770 goto drop; 771 if (rt->rt_ifp != ifp) { 772 log(LOG_INFO, "arp_proxy: ignoring request" 773 " from %s via %s, expecting %s\n", 774 inet_ntoa(isaddr), ifp->if_xname, 775 rt->rt_ifp->if_xname); 776 rtfree(rt); 777 goto drop; 778 } 779 rtfree(rt); 780 781 #ifdef DEBUG_PROXY 782 printf("arp: proxying for %s\n", 783 inet_ntoa(itaddr)); 784 #endif 785 } else { 786 rt = la->la_rt; 787 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 788 sdl = SDL(rt->rt_gateway); 789 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 790 } 791 } 792 793 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 794 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 795 ah->ar_op = htons(ARPOP_REPLY); 796 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 797 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */ 798 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 799 m->m_pkthdr.len = m->m_len; 800 sa.sa_family = AF_ARP; 801 sa.sa_len = 2; 802 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 803 return; 804 805 drop: 806 m_freem(m); 807 } 808 #endif 809 810 /* 811 * Free an arp entry. 812 */ 813 static void 814 arptfree(la) 815 struct llinfo_arp *la; 816 { 817 struct rtentry *rt = la->la_rt; 818 struct sockaddr_dl *sdl; 819 820 if (rt == 0) 821 panic("arptfree"); 822 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 823 sdl->sdl_family == AF_LINK) { 824 sdl->sdl_alen = 0; 825 la->la_preempt = la->la_asked = 0; 826 RT_LOCK(rt); /* XXX needed or move higher? */ 827 rt->rt_flags &= ~RTF_REJECT; 828 RT_UNLOCK(rt); 829 return; 830 } 831 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 832 0, (struct rtentry **)0); 833 } 834 /* 835 * Lookup or enter a new address in arptab. 836 */ 837 static struct llinfo_arp * 838 arplookup(addr, create, proxy) 839 u_long addr; 840 int create, proxy; 841 { 842 struct rtentry *rt; 843 struct sockaddr_inarp sin; 844 const char *why = 0; 845 846 bzero(&sin, sizeof(sin)); 847 sin.sin_len = sizeof(sin); 848 sin.sin_family = AF_INET; 849 sin.sin_addr.s_addr = addr; 850 if (proxy) 851 sin.sin_other = SIN_PROXY; 852 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 853 if (rt == 0) 854 return (0); 855 856 if (rt->rt_flags & RTF_GATEWAY) 857 why = "host is not on local network"; 858 else if ((rt->rt_flags & RTF_LLINFO) == 0) 859 why = "could not allocate llinfo"; 860 else if (rt->rt_gateway->sa_family != AF_LINK) 861 why = "gateway route is not ours"; 862 863 if (why) { 864 #define ISDYNCLONE(_rt) \ 865 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED) 866 if (create) 867 log(LOG_DEBUG, "arplookup %s failed: %s\n", 868 inet_ntoa(sin.sin_addr), why); 869 /* 870 * If there are no references to this Layer 2 route, 871 * and it is a cloned route, and not static, and 872 * arplookup() is creating the route, then purge 873 * it from the routing table as it is probably bogus. 874 */ 875 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt)) 876 rtexpunge(rt); 877 RTFREE_LOCKED(rt); 878 return (0); 879 #undef ISDYNCLONE 880 } else { 881 RT_REMREF(rt); 882 RT_UNLOCK(rt); 883 return ((struct llinfo_arp *)rt->rt_llinfo); 884 } 885 } 886 887 void 888 arp_ifinit(ifp, ifa) 889 struct ifnet *ifp; 890 struct ifaddr *ifa; 891 { 892 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 893 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 894 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 895 ifa->ifa_rtrequest = arp_rtrequest; 896 ifa->ifa_flags |= RTF_CLONING; 897 } 898 899 void 900 arp_ifinit2(ifp, ifa, enaddr) 901 struct ifnet *ifp; 902 struct ifaddr *ifa; 903 u_char *enaddr; 904 { 905 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 906 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 907 &IA_SIN(ifa)->sin_addr, enaddr); 908 ifa->ifa_rtrequest = arp_rtrequest; 909 ifa->ifa_flags |= RTF_CLONING; 910 } 911 912 static void 913 arp_init(void) 914 { 915 916 arpintrq.ifq_maxlen = 50; 917 mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF); 918 LIST_INIT(&llinfo_arp); 919 callout_init(&arp_callout, CALLOUT_MPSAFE); 920 netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE); 921 callout_reset(&arp_callout, hz, arptimer, NULL); 922 } 923 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 924