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