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