if_ether.c revision 183550
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: head/sys/netinet/if_ether.c 183550 2008-10-02 15:37:58Z zec $"); 40 41#include "opt_inet.h" 42#include "opt_mac.h" 43#include "opt_carp.h" 44 45#include <sys/param.h> 46#include <sys/kernel.h> 47#include <sys/queue.h> 48#include <sys/sysctl.h> 49#include <sys/systm.h> 50#include <sys/mbuf.h> 51#include <sys/malloc.h> 52#include <sys/proc.h> 53#include <sys/socket.h> 54#include <sys/syslog.h> 55#include <sys/vimage.h> 56 57#include <net/if.h> 58#include <net/if_dl.h> 59#include <net/if_types.h> 60#include <net/route.h> 61#include <net/netisr.h> 62#include <net/if_llc.h> 63#include <net/ethernet.h> 64 65#include <netinet/in.h> 66#include <netinet/in_var.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 81SYSCTL_DECL(_net_link_ether); 82SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 83 84/* timer values */ 85static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 86 87SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 88 &arpt_keep, 0, "ARP entry lifetime in seconds"); 89 90#define rt_expire rt_rmx.rmx_expire 91 92struct llinfo_arp { 93 struct callout la_timer; 94 struct rtentry *la_rt; 95 struct mbuf *la_hold; /* last packet until resolved/timeout */ 96 u_short la_preempt; /* countdown for pre-expiry arps */ 97 u_short la_asked; /* # requests sent */ 98}; 99 100static struct ifqueue arpintrq; 101static int arp_allocated; 102 103static int arp_maxtries = 5; 104static int useloopback = 1; /* use loopback interface for local traffic */ 105static int arp_proxyall = 0; 106 107SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, maxtries, 108 CTLFLAG_RW, arp_maxtries, 0, 109 "ARP resolution attempts before returning error"); 110SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, useloopback, 111 CTLFLAG_RW, useloopback, 0, 112 "Use the loopback interface for local traffic"); 113SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, proxyall, 114 CTLFLAG_RW, arp_proxyall, 0, 115 "Enable proxy ARP for all suitable requests"); 116 117static void arp_init(void); 118static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 119static void arprequest(struct ifnet *, 120 struct in_addr *, struct in_addr *, u_char *); 121static void arpintr(struct mbuf *); 122static void arptimer(void *); 123static struct rtentry 124 *arplookup(u_long, int, int, int); 125#ifdef INET 126static void in_arpinput(struct mbuf *); 127#endif 128 129/* 130 * Timeout routine. 131 */ 132static void 133arptimer(void *arg) 134{ 135 struct rtentry *rt = (struct rtentry *)arg; 136 137 RT_LOCK_ASSERT(rt); 138 /* 139 * The lock is needed to close a theoretical race 140 * between spontaneous expiry and intentional removal. 141 * We still got an extra reference on rtentry, so can 142 * safely pass pointers to its contents. 143 */ 144 RT_UNLOCK(rt); 145 146 in_rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL, 147 rt->rt_fibnum); 148} 149 150/* 151 * Parallel to llc_rtrequest. 152 */ 153static void 154arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info) 155{ 156 INIT_VNET_NET(curvnet); 157 INIT_VNET_INET(curvnet); 158 struct sockaddr *gate; 159 struct llinfo_arp *la; 160 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 161 struct in_ifaddr *ia; 162 struct ifaddr *ifa; 163 164 RT_LOCK_ASSERT(rt); 165 166 if (rt->rt_flags & RTF_GATEWAY) 167 return; 168 gate = rt->rt_gateway; 169 la = (struct llinfo_arp *)rt->rt_llinfo; 170 switch (req) { 171 172 case RTM_ADD: 173 /* 174 * XXX: If this is a manually added route to interface 175 * such as older version of routed or gated might provide, 176 * restore cloning bit. 177 */ 178 if ((rt->rt_flags & RTF_HOST) == 0 && 179 rt_mask(rt) != NULL && 180 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 181 rt->rt_flags |= RTF_CLONING; 182 if (rt->rt_flags & RTF_CLONING) { 183 /* 184 * Case 1: This route should come from a route to iface. 185 */ 186 rt_setgate(rt, rt_key(rt), 187 (struct sockaddr *)&null_sdl); 188 gate = rt->rt_gateway; 189 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 190 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 191 rt->rt_expire = time_uptime; 192 break; 193 } 194 /* Announce a new entry if requested. */ 195 if (rt->rt_flags & RTF_ANNOUNCE) 196 arprequest(rt->rt_ifp, 197 &SIN(rt_key(rt))->sin_addr, 198 &SIN(rt_key(rt))->sin_addr, 199 (u_char *)LLADDR(SDL(gate))); 200 /*FALLTHROUGH*/ 201 case RTM_RESOLVE: 202 if (gate->sa_family != AF_LINK || 203 gate->sa_len < sizeof(null_sdl)) { 204 log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__, 205 inet_ntoa(SIN(rt_key(rt))->sin_addr), 206 (gate->sa_family != AF_LINK) ? 207 " (!AF_LINK)": ""); 208 break; 209 } 210 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 211 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 212 if (la != 0) 213 break; /* This happens on a route change */ 214 /* 215 * Case 2: This route may come from cloning, or a manual route 216 * add with a LL address. 217 */ 218 R_Zalloc(la, struct llinfo_arp *, sizeof(*la)); 219 rt->rt_llinfo = (caddr_t)la; 220 if (la == 0) { 221 log(LOG_DEBUG, "%s: malloc failed\n", __func__); 222 break; 223 } 224 arp_allocated++; 225 /* 226 * We are storing a route entry outside of radix tree. So, 227 * it can be found and accessed by other means than radix 228 * lookup. The routing code assumes that any rtentry detached 229 * from radix can be destroyed safely. To prevent this, we 230 * add an additional reference. 231 */ 232 RT_ADDREF(rt); 233 la->la_rt = rt; 234 rt->rt_flags |= RTF_LLINFO; 235 callout_init_mtx(&la->la_timer, &rt->rt_mtx, 236 CALLOUT_RETURNUNLOCKED); 237 238#ifdef INET 239 /* 240 * This keeps the multicast addresses from showing up 241 * in `arp -a' listings as unresolved. It's not actually 242 * functional. Then the same for broadcast. 243 */ 244 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) && 245 rt->rt_ifp->if_type != IFT_ARCNET) { 246 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 247 LLADDR(SDL(gate))); 248 SDL(gate)->sdl_alen = 6; 249 rt->rt_expire = 0; 250 } 251 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 252 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, 253 rt->rt_ifp->if_addrlen); 254 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; 255 rt->rt_expire = 0; 256 } 257#endif 258 259 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 260 if (ia->ia_ifp == rt->rt_ifp && 261 SIN(rt_key(rt))->sin_addr.s_addr == 262 (IA_SIN(ia))->sin_addr.s_addr) 263 break; 264 } 265 if (ia) { 266 /* 267 * This test used to be 268 * if (loif.if_flags & IFF_UP) 269 * It allowed local traffic to be forced 270 * through the hardware by configuring the loopback down. 271 * However, it causes problems during network configuration 272 * for boards that can't receive packets they send. 273 * It is now necessary to clear "useloopback" and remove 274 * the route to force traffic out to the hardware. 275 */ 276 rt->rt_expire = 0; 277 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 278 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 279 if (V_useloopback) { 280 rt->rt_ifp = V_loif; 281 rt->rt_rmx.rmx_mtu = V_loif->if_mtu; 282 } 283 284 /* 285 * make sure to set rt->rt_ifa to the interface 286 * address we are using, otherwise we will have trouble 287 * with source address selection. 288 */ 289 ifa = &ia->ia_ifa; 290 if (ifa != rt->rt_ifa) { 291 IFAFREE(rt->rt_ifa); 292 IFAREF(ifa); 293 rt->rt_ifa = ifa; 294 } 295 } 296 break; 297 298 case RTM_DELETE: 299 if (la == NULL) /* XXX: at least CARP does this. */ 300 break; 301 callout_stop(&la->la_timer); 302 rt->rt_llinfo = NULL; 303 rt->rt_flags &= ~RTF_LLINFO; 304 RT_REMREF(rt); 305 if (la->la_hold) 306 m_freem(la->la_hold); 307 Free((caddr_t)la); 308 } 309} 310 311/* 312 * Broadcast an ARP request. Caller specifies: 313 * - arp header source ip address 314 * - arp header target ip address 315 * - arp header source ethernet address 316 */ 317static void 318arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip, 319 u_char *enaddr) 320{ 321 struct mbuf *m; 322 struct arphdr *ah; 323 struct sockaddr sa; 324 325 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 326 return; 327 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 328 2*ifp->if_data.ifi_addrlen; 329 m->m_pkthdr.len = m->m_len; 330 MH_ALIGN(m, m->m_len); 331 ah = mtod(m, struct arphdr *); 332 bzero((caddr_t)ah, m->m_len); 333#ifdef MAC 334 mac_netinet_arp_send(ifp, m); 335#endif 336 ah->ar_pro = htons(ETHERTYPE_IP); 337 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 338 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 339 ah->ar_op = htons(ARPOP_REQUEST); 340 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 341 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 342 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 343 sa.sa_family = AF_ARP; 344 sa.sa_len = 2; 345 m->m_flags |= M_BCAST; 346 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 347 348 return; 349} 350 351/* 352 * Resolve an IP address into an ethernet address. 353 * On input: 354 * ifp is the interface we use 355 * rt0 is the route to the final destination (possibly useless) 356 * m is the mbuf. May be NULL if we don't have a packet. 357 * dst is the next hop, 358 * desten is where we want the address. 359 * 360 * On success, desten is filled in and the function returns 0; 361 * If the packet must be held pending resolution, we return EWOULDBLOCK 362 * On other errors, we return the corresponding error code. 363 * Note that m_freem() handles NULL. 364 */ 365int 366arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 367 struct sockaddr *dst, u_char *desten) 368{ 369 INIT_VNET_INET(ifp->if_vnet); 370 struct llinfo_arp *la = NULL; 371 struct rtentry *rt = NULL; 372 struct sockaddr_dl *sdl; 373 int error; 374 int fibnum = -1; 375 376 if (m) { 377 if (m->m_flags & M_BCAST) { 378 /* broadcast */ 379 (void)memcpy(desten, 380 ifp->if_broadcastaddr, ifp->if_addrlen); 381 return (0); 382 } 383 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) { 384 /* multicast */ 385 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 386 return (0); 387 } 388 fibnum = M_GETFIB(m); 389 } 390 391 if (rt0 != NULL) { 392 /* Look for a cached arp (ll) entry. */ 393 if (m == NULL) 394 fibnum = rt0->rt_fibnum; 395 error = rt_check(&rt, &rt0, dst); 396 if (error) { 397 m_freem(m); 398 return error; 399 } 400 la = (struct llinfo_arp *)rt->rt_llinfo; 401 if (la == NULL) 402 RT_UNLOCK(rt); 403 } 404 405 /* 406 * If we had no mbuf and no route, then hope the caller 407 * has a fib in mind because we are running out of ideas. 408 * I think this should not happen in current code. 409 * (kmacy would know). 410 */ 411 if (fibnum == -1) 412 fibnum = curthread->td_proc->p_fibnum; /* last gasp */ 413 414 if (la == NULL) { 415 /* 416 * We enter this block if rt0 was NULL, 417 * or if rt found by rt_check() didn't have llinfo. 418 * we should get a cloned route, which since it should 419 * come from the local interface should have a ll entry. 420 * It may be incomplete but that's ok. 421 */ 422 rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0, fibnum); 423 if (rt == NULL) { 424 log(LOG_DEBUG, 425 "arpresolve: can't allocate route for %s\n", 426 inet_ntoa(SIN(dst)->sin_addr)); 427 m_freem(m); 428 return (EINVAL); /* XXX */ 429 } 430 la = (struct llinfo_arp *)rt->rt_llinfo; 431 if (la == NULL) { 432 RT_UNLOCK(rt); 433 log(LOG_DEBUG, 434 "arpresolve: can't allocate llinfo for %s\n", 435 inet_ntoa(SIN(dst)->sin_addr)); 436 m_freem(m); 437 return (EINVAL); /* XXX */ 438 } 439 } 440 sdl = SDL(rt->rt_gateway); 441 /* 442 * Check the address family and length is valid, the address 443 * is resolved; otherwise, try to resolve. 444 */ 445 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) && 446 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 447 448 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 449 450 /* 451 * If entry has an expiry time and it is approaching, 452 * send an ARP request. 453 */ 454 if ((rt->rt_expire != 0) && 455 (time_uptime + la->la_preempt > rt->rt_expire)) { 456 struct in_addr sin = 457 SIN(rt->rt_ifa->ifa_addr)->sin_addr; 458 459 la->la_preempt--; 460 RT_UNLOCK(rt); 461 arprequest(ifp, &sin, &SIN(dst)->sin_addr, 462 IF_LLADDR(ifp)); 463 return (0); 464 } 465 466 RT_UNLOCK(rt); 467 return (0); 468 } 469 /* 470 * If ARP is disabled or static on this interface, stop. 471 * XXX 472 * Probably should not allocate empty llinfo struct if we are 473 * not going to be sending out an arp request. 474 */ 475 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) { 476 RT_UNLOCK(rt); 477 m_freem(m); 478 return (EINVAL); 479 } 480 /* 481 * There is an arptab entry, but no ethernet address 482 * response yet. Replace the held mbuf with this 483 * latest one. 484 */ 485 if (m) { 486 if (la->la_hold) 487 m_freem(la->la_hold); 488 la->la_hold = m; 489 } 490 KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry")); 491 492 /* 493 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 494 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 495 * if we have already sent arp_maxtries ARP requests. Retransmit the 496 * ARP request, but not faster than one request per second. 497 */ 498 if (la->la_asked < V_arp_maxtries) 499 error = EWOULDBLOCK; /* First request. */ 500 else 501 error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH; 502 503 if (la->la_asked == 0 || rt->rt_expire != time_uptime) { 504 struct in_addr sin = 505 SIN(rt->rt_ifa->ifa_addr)->sin_addr; 506 507 rt->rt_expire = time_uptime; 508 callout_reset(&la->la_timer, hz, arptimer, rt); 509 la->la_asked++; 510 RT_UNLOCK(rt); 511 512 arprequest(ifp, &sin, &SIN(dst)->sin_addr, 513 IF_LLADDR(ifp)); 514 } else 515 RT_UNLOCK(rt); 516 517 return (error); 518} 519 520/* 521 * Common length and type checks are done here, 522 * then the protocol-specific routine is called. 523 */ 524static void 525arpintr(struct mbuf *m) 526{ 527 struct arphdr *ar; 528 529 if (m->m_len < sizeof(struct arphdr) && 530 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 531 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 532 return; 533 } 534 ar = mtod(m, struct arphdr *); 535 536 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 537 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 538 ntohs(ar->ar_hrd) != ARPHRD_ARCNET && 539 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) { 540 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 541 (unsigned char *)&ar->ar_hrd, ""); 542 m_freem(m); 543 return; 544 } 545 546 if (m->m_len < arphdr_len(ar)) { 547 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 548 log(LOG_ERR, "arp: runt packet\n"); 549 m_freem(m); 550 return; 551 } 552 ar = mtod(m, struct arphdr *); 553 } 554 555 switch (ntohs(ar->ar_pro)) { 556#ifdef INET 557 case ETHERTYPE_IP: 558 in_arpinput(m); 559 return; 560#endif 561 } 562 m_freem(m); 563} 564 565#ifdef INET 566/* 567 * ARP for Internet protocols on 10 Mb/s Ethernet. 568 * Algorithm is that given in RFC 826. 569 * In addition, a sanity check is performed on the sender 570 * protocol address, to catch impersonators. 571 * We no longer handle negotiations for use of trailer protocol: 572 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 573 * along with IP replies if we wanted trailers sent to us, 574 * and also sent them in response to IP replies. 575 * This allowed either end to announce the desire to receive 576 * trailer packets. 577 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 578 * but formerly didn't normally send requests. 579 */ 580static int log_arp_wrong_iface = 1; 581static int log_arp_movements = 1; 582static int log_arp_permanent_modify = 1; 583 584SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 585 &log_arp_wrong_iface, 0, 586 "log arp packets arriving on the wrong interface"); 587SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 588 &log_arp_movements, 0, 589 "log arp replies from MACs different than the one in the cache"); 590SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW, 591 &log_arp_permanent_modify, 0, 592 "log arp replies from MACs different than the one in the permanent arp entry"); 593 594 595static void 596in_arpinput(struct mbuf *m) 597{ 598 struct arphdr *ah; 599 struct ifnet *ifp = m->m_pkthdr.rcvif; 600 struct llinfo_arp *la; 601 struct rtentry *rt; 602 struct ifaddr *ifa; 603 struct in_ifaddr *ia; 604 struct sockaddr_dl *sdl; 605 struct sockaddr sa; 606 struct in_addr isaddr, itaddr, myaddr; 607 struct mbuf *hold; 608 u_int8_t *enaddr = NULL; 609 int op, rif_len; 610 int req_len; 611 int bridged = 0, is_bridge = 0; 612 u_int fibnum; 613 u_int goodfib = 0; 614 int firstpass = 1; 615#ifdef DEV_CARP 616 int carp_match = 0; 617#endif 618 struct sockaddr_in sin; 619 sin.sin_len = sizeof(struct sockaddr_in); 620 sin.sin_family = AF_INET; 621 sin.sin_addr.s_addr = 0; 622 INIT_VNET_INET(ifp->if_vnet); 623 624 if (ifp->if_bridge) 625 bridged = 1; 626 if (ifp->if_type == IFT_BRIDGE) 627 is_bridge = 1; 628 629 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 630 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 631 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 632 return; 633 } 634 635 ah = mtod(m, struct arphdr *); 636 op = ntohs(ah->ar_op); 637 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 638 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 639 640 /* 641 * For a bridge, we want to check the address irrespective 642 * of the receive interface. (This will change slightly 643 * when we have clusters of interfaces). 644 * If the interface does not match, but the recieving interface 645 * is part of carp, we call carp_iamatch to see if this is a 646 * request for the virtual host ip. 647 * XXX: This is really ugly! 648 */ 649 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 650 if (((bridged && ia->ia_ifp->if_bridge != NULL) || 651 (ia->ia_ifp == ifp)) && 652 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 653 goto match; 654#ifdef DEV_CARP 655 if (ifp->if_carp != NULL && 656 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) && 657 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 658 carp_match = 1; 659 goto match; 660 } 661#endif 662 } 663 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 664 if (((bridged && ia->ia_ifp->if_bridge != NULL) || 665 (ia->ia_ifp == ifp)) && 666 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 667 goto match; 668 669#define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \ 670 (ia->ia_ifp->if_bridge == ifp->if_softc && \ 671 !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \ 672 addr == ia->ia_addr.sin_addr.s_addr) 673 /* 674 * Check the case when bridge shares its MAC address with 675 * some of its children, so packets are claimed by bridge 676 * itself (bridge_input() does it first), but they are really 677 * meant to be destined to the bridge member. 678 */ 679 if (is_bridge) { 680 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 681 if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) { 682 ifp = ia->ia_ifp; 683 goto match; 684 } 685 } 686 } 687#undef BDG_MEMBER_MATCHES_ARP 688 689 /* 690 * No match, use the first inet address on the receive interface 691 * as a dummy address for the rest of the function. 692 */ 693 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 694 if (ifa->ifa_addr->sa_family == AF_INET) { 695 ia = ifatoia(ifa); 696 goto match; 697 } 698 /* 699 * If bridging, fall back to using any inet address. 700 */ 701 if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) 702 goto drop; 703match: 704 if (!enaddr) 705 enaddr = (u_int8_t *)IF_LLADDR(ifp); 706 myaddr = ia->ia_addr.sin_addr; 707 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 708 goto drop; /* it's from me, ignore it. */ 709 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 710 log(LOG_ERR, 711 "arp: link address is broadcast for IP address %s!\n", 712 inet_ntoa(isaddr)); 713 goto drop; 714 } 715 /* 716 * Warn if another host is using the same IP address, but only if the 717 * IP address isn't 0.0.0.0, which is used for DHCP only, in which 718 * case we suppress the warning to avoid false positive complaints of 719 * potential misconfiguration. 720 */ 721 if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { 722 log(LOG_ERR, 723 "arp: %*D is using my IP address %s on %s!\n", 724 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 725 inet_ntoa(isaddr), ifp->if_xname); 726 itaddr = myaddr; 727 goto reply; 728 } 729 if (ifp->if_flags & IFF_STATICARP) 730 goto reply; 731 /* 732 * We look for any FIB that has this address to find 733 * the interface etc. 734 * For sanity checks that are FIB independent we abort the loop. 735 */ 736 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 737 rt = arplookup(isaddr.s_addr, 738 itaddr.s_addr == myaddr.s_addr, 0, fibnum); 739 if (rt == NULL) 740 continue; 741 742 sdl = SDL(rt->rt_gateway); 743 /* Only call this once */ 744 if (firstpass) { 745 sin.sin_addr.s_addr = isaddr.s_addr; 746 EVENTHANDLER_INVOKE(route_arp_update_event, rt, 747 ar_sha(ah), (struct sockaddr *)&sin); 748 } 749 750 la = (struct llinfo_arp *)rt->rt_llinfo; 751 if (la == NULL) { 752 RT_UNLOCK(rt); 753 continue; 754 } 755 756 if (firstpass) { 757 /* The following is not an error when doing bridging. */ 758 if (!bridged && rt->rt_ifp != ifp 759#ifdef DEV_CARP 760 && (ifp->if_type != IFT_CARP || !carp_match) 761#endif 762 ) { 763 if (log_arp_wrong_iface) 764 log(LOG_ERR, "arp: %s is on %s " 765 "but got reply from %*D " 766 "on %s\n", 767 inet_ntoa(isaddr), 768 rt->rt_ifp->if_xname, 769 ifp->if_addrlen, 770 (u_char *)ar_sha(ah), ":", 771 ifp->if_xname); 772 RT_UNLOCK(rt); 773 break; 774 } 775 if (sdl->sdl_alen && 776 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 777 if (rt->rt_expire) { 778 if (log_arp_movements) 779 log(LOG_INFO, 780 "arp: %s moved from %*D to %*D " 781 "on %s\n", 782 inet_ntoa(isaddr), 783 ifp->if_addrlen, 784 (u_char *)LLADDR(sdl), ":", 785 ifp->if_addrlen, 786 (u_char *)ar_sha(ah), ":", 787 ifp->if_xname); 788 } else { 789 RT_UNLOCK(rt); 790 if (log_arp_permanent_modify) 791 log(LOG_ERR, 792 "arp: %*D attempts to " 793 "modify permanent entry " 794 "for %s on %s\n", 795 ifp->if_addrlen, 796 (u_char *)ar_sha(ah), ":", 797 inet_ntoa(isaddr), 798 ifp->if_xname); 799 break; 800 } 801 } 802 /* 803 * sanity check for the address length. 804 * XXX this does not work for protocols 805 * with variable address length. -is 806 */ 807 if (sdl->sdl_alen && 808 sdl->sdl_alen != ah->ar_hln) { 809 log(LOG_WARNING, 810 "arp from %*D: new addr len %d, was %d", 811 ifp->if_addrlen, (u_char *) ar_sha(ah), 812 ":", ah->ar_hln, sdl->sdl_alen); 813 } 814 if (ifp->if_addrlen != ah->ar_hln) { 815 log(LOG_WARNING, 816 "arp from %*D: addr len: " 817 "new %d, i/f %d (ignored)", 818 ifp->if_addrlen, (u_char *) ar_sha(ah), 819 ":", ah->ar_hln, ifp->if_addrlen); 820 RT_UNLOCK(rt); 821 break; 822 } 823 firstpass = 0; 824 goodfib = fibnum; 825 } 826 827 /* Copy in the information received. */ 828 (void)memcpy(LLADDR(sdl), ar_sha(ah), 829 sdl->sdl_alen = ah->ar_hln); 830 /* 831 * If we receive an arp from a token-ring station over 832 * a token-ring nic then try to save the source routing info. 833 * XXXMRT Only minimal Token Ring support for MRT. 834 * Only do this on the first pass as if modifies the mbuf. 835 */ 836 if (ifp->if_type == IFT_ISO88025) { 837 struct iso88025_header *th = NULL; 838 struct iso88025_sockaddr_dl_data *trld; 839 840 /* force the fib loop to end after this pass */ 841 fibnum = rt_numfibs - 1; 842 843 th = (struct iso88025_header *)m->m_pkthdr.header; 844 trld = SDL_ISO88025(sdl); 845 rif_len = TR_RCF_RIFLEN(th->rcf); 846 if ((th->iso88025_shost[0] & TR_RII) && 847 (rif_len > 2)) { 848 trld->trld_rcf = th->rcf; 849 trld->trld_rcf ^= htons(TR_RCF_DIR); 850 memcpy(trld->trld_route, th->rd, rif_len - 2); 851 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK); 852 /* 853 * Set up source routing information for 854 * reply packet (XXX) 855 */ 856 m->m_data -= rif_len; 857 m->m_len += rif_len; 858 m->m_pkthdr.len += rif_len; 859 } else { 860 th->iso88025_shost[0] &= ~TR_RII; 861 trld->trld_rcf = 0; 862 } 863 m->m_data -= 8; 864 m->m_len += 8; 865 m->m_pkthdr.len += 8; 866 th->rcf = trld->trld_rcf; 867 } 868 869 if (rt->rt_expire) { 870 rt->rt_expire = time_uptime + V_arpt_keep; 871 callout_reset(&la->la_timer, hz * V_arpt_keep, 872 arptimer, rt); 873 } 874 la->la_asked = 0; 875 la->la_preempt = V_arp_maxtries; 876 hold = la->la_hold; 877 la->la_hold = NULL; 878 RT_UNLOCK(rt); 879 if (hold != NULL) 880 (*ifp->if_output)(ifp, hold, rt_key(rt), rt); 881 } /* end of FIB loop */ 882reply: 883 884 /* 885 * Decide if we have to respond to something. 886 */ 887 if (op != ARPOP_REQUEST) 888 goto drop; 889 if (itaddr.s_addr == myaddr.s_addr) { 890 /* Shortcut.. the receiving interface is the target. */ 891 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 892 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 893 } else { 894 /* It's not asking for our address. But it still may 895 * be something we should answer. 896 * 897 * XXX MRT 898 * We assume that link level info is independent of 899 * the table used and so we use whichever we can and don't 900 * have a better option. 901 */ 902 /* Have we been asked to proxy for the target. */ 903 rt = arplookup(itaddr.s_addr, 0, SIN_PROXY, goodfib); 904 if (rt == NULL) { 905 /* Nope, only intersted now if proxying everything. */ 906 struct sockaddr_in sin; 907 908 if (!V_arp_proxyall) 909 goto drop; 910 911 bzero(&sin, sizeof sin); 912 sin.sin_family = AF_INET; 913 sin.sin_len = sizeof sin; 914 sin.sin_addr = itaddr; 915 916 /* XXX MRT use table 0 for arp reply */ 917 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 918 if (!rt) 919 goto drop; 920 /* 921 * Don't send proxies for nodes on the same interface 922 * as this one came out of, or we'll get into a fight 923 * over who claims what Ether address. 924 */ 925 if (rt->rt_ifp == ifp) { 926 rtfree(rt); 927 goto drop; 928 } 929 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 930 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 931 rtfree(rt); 932 933 /* 934 * Also check that the node which sent the ARP packet 935 * is on the the interface we expect it to be on. This 936 * avoids ARP chaos if an interface is connected to the 937 * wrong network. 938 */ 939 sin.sin_addr = isaddr; 940 941 /* XXX MRT use table 0 for arp checks */ 942 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 943 if (!rt) 944 goto drop; 945 if (rt->rt_ifp != ifp) { 946 log(LOG_INFO, "arp_proxy: ignoring request" 947 " from %s via %s, expecting %s\n", 948 inet_ntoa(isaddr), ifp->if_xname, 949 rt->rt_ifp->if_xname); 950 rtfree(rt); 951 goto drop; 952 } 953 rtfree(rt); 954 955#ifdef DEBUG_PROXY 956 printf("arp: proxying for %s\n", 957 inet_ntoa(itaddr)); 958#endif 959 } else { 960 /* 961 * Return proxied ARP replies only on the interface 962 * or bridge cluster where this network resides. 963 * Otherwise we may conflict with the host we are 964 * proxying for. 965 */ 966 if (rt->rt_ifp != ifp && 967 (rt->rt_ifp->if_bridge != ifp->if_bridge || 968 ifp->if_bridge == NULL)) { 969 RT_UNLOCK(rt); 970 goto drop; 971 } 972 sdl = SDL(rt->rt_gateway); 973 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 974 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 975 RT_UNLOCK(rt); 976 } 977 } 978 979 if (itaddr.s_addr == myaddr.s_addr && 980 IN_LINKLOCAL(ntohl(itaddr.s_addr))) { 981 /* RFC 3927 link-local IPv4; always reply by broadcast. */ 982#ifdef DEBUG_LINKLOCAL 983 printf("arp: sending reply for link-local addr %s\n", 984 inet_ntoa(itaddr)); 985#endif 986 m->m_flags |= M_BCAST; 987 m->m_flags &= ~M_MCAST; 988 } else { 989 /* default behaviour; never reply by broadcast. */ 990 m->m_flags &= ~(M_BCAST|M_MCAST); 991 } 992 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 993 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 994 ah->ar_op = htons(ARPOP_REPLY); 995 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 996 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 997 m->m_pkthdr.len = m->m_len; 998 sa.sa_family = AF_ARP; 999 sa.sa_len = 2; 1000 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 1001 return; 1002 1003drop: 1004 m_freem(m); 1005} 1006#endif 1007 1008/* 1009 * Lookup or enter a new address in arptab. 1010 */ 1011static struct rtentry * 1012arplookup(u_long addr, int create, int proxy, int fibnum) 1013{ 1014 struct rtentry *rt; 1015 struct sockaddr_inarp sin; 1016 const char *why = 0; 1017 1018 bzero(&sin, sizeof(sin)); 1019 sin.sin_len = sizeof(sin); 1020 sin.sin_family = AF_INET; 1021 sin.sin_addr.s_addr = addr; 1022 if (proxy) 1023 sin.sin_other = SIN_PROXY; 1024 rt = in_rtalloc1((struct sockaddr *)&sin, create, 0UL, fibnum); 1025 if (rt == 0) 1026 return (0); 1027 1028 if (rt->rt_flags & RTF_GATEWAY) 1029 why = "host is not on local network"; 1030 else if ((rt->rt_flags & RTF_LLINFO) == 0) 1031 why = "could not allocate llinfo"; 1032 else if (rt->rt_gateway->sa_family != AF_LINK) 1033 why = "gateway route is not ours"; 1034 1035 if (why) { 1036#define ISDYNCLONE(_rt) \ 1037 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED) 1038 if (create) 1039 log(LOG_DEBUG, "arplookup %s failed: %s\n", 1040 inet_ntoa(sin.sin_addr), why); 1041 /* 1042 * If there are no references to this Layer 2 route, 1043 * and it is a cloned route, and not static, and 1044 * arplookup() is creating the route, then purge 1045 * it from the routing table as it is probably bogus. 1046 */ 1047 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt)) 1048 rtexpunge(rt); 1049 RTFREE_LOCKED(rt); 1050 return (0); 1051#undef ISDYNCLONE 1052 } else { 1053 RT_REMREF(rt); 1054 return (rt); 1055 } 1056} 1057 1058void 1059arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 1060{ 1061 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 1062 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 1063 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 1064 ifa->ifa_rtrequest = arp_rtrequest; 1065 ifa->ifa_flags |= RTF_CLONING; 1066} 1067 1068void 1069arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr) 1070{ 1071 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 1072 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 1073 &IA_SIN(ifa)->sin_addr, enaddr); 1074 ifa->ifa_rtrequest = arp_rtrequest; 1075 ifa->ifa_flags |= RTF_CLONING; 1076} 1077 1078static void 1079arp_init(void) 1080{ 1081 1082 arpintrq.ifq_maxlen = 50; 1083 mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF); 1084 netisr_register(NETISR_ARP, arpintr, &arpintrq, 0); 1085} 1086SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 1087