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