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