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