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 int carp_match = 0; 557 558 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 559 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 560 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 561 return; 562 } 563 564 ah = mtod(m, struct arphdr *); 565 op = ntohs(ah->ar_op); 566 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 567 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 568 569 /* 570 * For a bridge, we want to check the address irrespective 571 * of the receive interface. (This will change slightly 572 * when we have clusters of interfaces). 573 * If the interface does not match, but the recieving interface 574 * is part of carp, we call carp_iamatch to see if this is a 575 * request for the virtual host ip. 576 * XXX: This is really ugly! 577 */ 578 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 579 if ((do_bridge || (ia->ia_ifp == ifp)) && 580 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 581 goto match; 582 #ifdef DEV_CARP 583 if (ifp->if_carp != NULL && 584 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) && 585 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 586 carp_match = 1; 587 goto match; 588 } 589 #endif 590 } 591 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 592 if ((do_bridge || (ia->ia_ifp == ifp)) && 593 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 594 goto match; 595 /* 596 * No match, use the first inet address on the receive interface 597 * as a dummy address for the rest of the function. 598 */ 599 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 600 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 601 ia = ifatoia(ifa); 602 goto match; 603 } 604 /* 605 * If bridging, fall back to using any inet address. 606 */ 607 if (!do_bridge || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) 608 goto drop; 609 match: 610 if (!enaddr) 611 enaddr = (u_int8_t *)IF_LLADDR(ifp); 612 myaddr = ia->ia_addr.sin_addr; 613 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 614 goto drop; /* it's from me, ignore it. */ 615 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 616 log(LOG_ERR, 617 "arp: link address is broadcast for IP address %s!\n", 618 inet_ntoa(isaddr)); 619 goto drop; 620 } 621 /* 622 * Warn if another host is using the same IP address, but only if the 623 * IP address isn't 0.0.0.0, which is used for DHCP only, in which 624 * case we suppress the warning to avoid false positive complaints of 625 * potential misconfiguration. 626 */ 627 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { 628 log(LOG_ERR, 629 "arp: %*D is using my IP address %s!\n", 630 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 631 inet_ntoa(isaddr)); 632 itaddr = myaddr; 633 goto reply; 634 } 635 if (ifp->if_flags & IFF_STATICARP) 636 goto reply; 637 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 638 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 639 /* the following is not an error when doing bridging */ 640 if (!do_bridge && rt->rt_ifp != ifp && 641 !(ifp->if_type == IFT_CARP && carp_match)) { 642 if (log_arp_wrong_iface) 643 log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n", 644 inet_ntoa(isaddr), 645 rt->rt_ifp->if_xname, 646 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 647 ifp->if_xname); 648 goto reply; 649 } 650 if (sdl->sdl_alen && 651 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 652 if (rt->rt_expire) { 653 if (log_arp_movements) 654 log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n", 655 inet_ntoa(isaddr), 656 ifp->if_addrlen, (u_char *)LLADDR(sdl), ":", 657 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 658 ifp->if_xname); 659 } else { 660 log(LOG_ERR, 661 "arp: %*D attempts to modify permanent entry for %s on %s\n", 662 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 663 inet_ntoa(isaddr), ifp->if_xname); 664 goto reply; 665 } 666 } 667 /* 668 * sanity check for the address length. 669 * XXX this does not work for protocols with variable address 670 * length. -is 671 */ 672 if (sdl->sdl_alen && 673 sdl->sdl_alen != ah->ar_hln) { 674 log(LOG_WARNING, 675 "arp from %*D: new addr len %d, was %d", 676 ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 677 ah->ar_hln, sdl->sdl_alen); 678 } 679 if (ifp->if_addrlen != ah->ar_hln) { 680 log(LOG_WARNING, 681 "arp from %*D: addr len: new %d, i/f %d (ignored)", 682 ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 683 ah->ar_hln, ifp->if_addrlen); 684 goto reply; 685 } 686 (void)memcpy(LLADDR(sdl), ar_sha(ah), 687 sdl->sdl_alen = ah->ar_hln); 688 /* 689 * If we receive an arp from a token-ring station over 690 * a token-ring nic then try to save the source 691 * routing info. 692 */ 693 if (ifp->if_type == IFT_ISO88025) { 694 th = (struct iso88025_header *)m->m_pkthdr.header; 695 trld = SDL_ISO88025(sdl); 696 rif_len = TR_RCF_RIFLEN(th->rcf); 697 if ((th->iso88025_shost[0] & TR_RII) && 698 (rif_len > 2)) { 699 trld->trld_rcf = th->rcf; 700 trld->trld_rcf ^= htons(TR_RCF_DIR); 701 memcpy(trld->trld_route, th->rd, rif_len - 2); 702 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK); 703 /* 704 * Set up source routing information for 705 * reply packet (XXX) 706 */ 707 m->m_data -= rif_len; 708 m->m_len += rif_len; 709 m->m_pkthdr.len += rif_len; 710 } else { 711 th->iso88025_shost[0] &= ~TR_RII; 712 trld->trld_rcf = 0; 713 } 714 m->m_data -= 8; 715 m->m_len += 8; 716 m->m_pkthdr.len += 8; 717 th->rcf = trld->trld_rcf; 718 } 719 RT_LOCK(rt); 720 if (rt->rt_expire) 721 rt->rt_expire = time_second + arpt_keep; 722 rt->rt_flags &= ~RTF_REJECT; 723 RT_UNLOCK(rt); 724 la->la_asked = 0; 725 la->la_preempt = arp_maxtries; 726 if (la->la_hold) { 727 (*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt); 728 la->la_hold = 0; 729 } 730 } 731 reply: 732 if (op != ARPOP_REQUEST) 733 goto drop; 734 if (itaddr.s_addr == myaddr.s_addr) { 735 /* I am the target */ 736 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 737 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 738 } else { 739 la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 740 if (la == NULL) { 741 struct sockaddr_in sin; 742 743 if (!arp_proxyall) 744 goto drop; 745 746 bzero(&sin, sizeof sin); 747 sin.sin_family = AF_INET; 748 sin.sin_len = sizeof sin; 749 sin.sin_addr = itaddr; 750 751 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 752 if (!rt) 753 goto drop; 754 /* 755 * Don't send proxies for nodes on the same interface 756 * as this one came out of, or we'll get into a fight 757 * over who claims what Ether address. 758 */ 759 if (rt->rt_ifp == ifp) { 760 rtfree(rt); 761 goto drop; 762 } 763 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 764 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 765 rtfree(rt); 766 767 /* 768 * Also check that the node which sent the ARP packet 769 * is on the the interface we expect it to be on. This 770 * avoids ARP chaos if an interface is connected to the 771 * wrong network. 772 */ 773 sin.sin_addr = isaddr; 774 775 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 776 if (!rt) 777 goto drop; 778 if (rt->rt_ifp != ifp) { 779 log(LOG_INFO, "arp_proxy: ignoring request" 780 " from %s via %s, expecting %s\n", 781 inet_ntoa(isaddr), ifp->if_xname, 782 rt->rt_ifp->if_xname); 783 rtfree(rt); 784 goto drop; 785 } 786 rtfree(rt); 787 788 #ifdef DEBUG_PROXY 789 printf("arp: proxying for %s\n", 790 inet_ntoa(itaddr)); 791 #endif 792 } else { 793 rt = la->la_rt; 794 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 795 sdl = SDL(rt->rt_gateway); 796 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 797 } 798 } 799 800 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 801 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 802 ah->ar_op = htons(ARPOP_REPLY); 803 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 804 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */ 805 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 806 m->m_pkthdr.len = m->m_len; 807 sa.sa_family = AF_ARP; 808 sa.sa_len = 2; 809 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 810 return; 811 812 drop: 813 m_freem(m); 814 } 815 #endif 816 817 /* 818 * Free an arp entry. 819 */ 820 static void 821 arptfree(la) 822 struct llinfo_arp *la; 823 { 824 struct rtentry *rt = la->la_rt; 825 struct sockaddr_dl *sdl; 826 827 if (rt == 0) 828 panic("arptfree"); 829 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 830 sdl->sdl_family == AF_LINK) { 831 sdl->sdl_alen = 0; 832 la->la_preempt = la->la_asked = 0; 833 RT_LOCK(rt); /* XXX needed or move higher? */ 834 rt->rt_flags &= ~RTF_REJECT; 835 RT_UNLOCK(rt); 836 return; 837 } 838 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 839 0, (struct rtentry **)0); 840 } 841 /* 842 * Lookup or enter a new address in arptab. 843 */ 844 static struct llinfo_arp * 845 arplookup(addr, create, proxy) 846 u_long addr; 847 int create, proxy; 848 { 849 struct rtentry *rt; 850 struct sockaddr_inarp sin; 851 const char *why = 0; 852 853 bzero(&sin, sizeof(sin)); 854 sin.sin_len = sizeof(sin); 855 sin.sin_family = AF_INET; 856 sin.sin_addr.s_addr = addr; 857 if (proxy) 858 sin.sin_other = SIN_PROXY; 859 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 860 if (rt == 0) 861 return (0); 862 863 if (rt->rt_flags & RTF_GATEWAY) 864 why = "host is not on local network"; 865 else if ((rt->rt_flags & RTF_LLINFO) == 0) 866 why = "could not allocate llinfo"; 867 else if (rt->rt_gateway->sa_family != AF_LINK) 868 why = "gateway route is not ours"; 869 870 if (why) { 871 #define ISDYNCLONE(_rt) \ 872 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED) 873 if (create) 874 log(LOG_DEBUG, "arplookup %s failed: %s\n", 875 inet_ntoa(sin.sin_addr), why); 876 /* 877 * If there are no references to this Layer 2 route, 878 * and it is a cloned route, and not static, and 879 * arplookup() is creating the route, then purge 880 * it from the routing table as it is probably bogus. 881 */ 882 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt)) 883 rtexpunge(rt); 884 RTFREE_LOCKED(rt); 885 return (0); 886 #undef ISDYNCLONE 887 } else { 888 RT_REMREF(rt); 889 RT_UNLOCK(rt); 890 return ((struct llinfo_arp *)rt->rt_llinfo); 891 } 892 } 893 894 void 895 arp_ifinit(ifp, ifa) 896 struct ifnet *ifp; 897 struct ifaddr *ifa; 898 { 899 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 900 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 901 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 902 ifa->ifa_rtrequest = arp_rtrequest; 903 ifa->ifa_flags |= RTF_CLONING; 904 } 905 906 void 907 arp_ifinit2(ifp, ifa, enaddr) 908 struct ifnet *ifp; 909 struct ifaddr *ifa; 910 u_char *enaddr; 911 { 912 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 913 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 914 &IA_SIN(ifa)->sin_addr, enaddr); 915 ifa->ifa_rtrequest = arp_rtrequest; 916 ifa->ifa_flags |= RTF_CLONING; 917 } 918 919 static void 920 arp_init(void) 921 { 922 923 arpintrq.ifq_maxlen = 50; 924 mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF); 925 LIST_INIT(&llinfo_arp); 926 callout_init(&arp_callout, CALLOUT_MPSAFE); 927 netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE); 928 callout_reset(&arp_callout, hz, arptimer, NULL); 929 } 930 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 931