if_ether.c revision 172467
138494Sobrien/*- 2174294Sobrien * Copyright (c) 1982, 1986, 1988, 1993 338494Sobrien * The Regents of the University of California. All rights reserved. 438494Sobrien * 538494Sobrien * Redistribution and use in source and binary forms, with or without 638494Sobrien * modification, are permitted provided that the following conditions 738494Sobrien * are met: 838494Sobrien * 1. Redistributions of source code must retain the above copyright 938494Sobrien * notice, this list of conditions and the following disclaimer. 1038494Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1138494Sobrien * notice, this list of conditions and the following disclaimer in the 1238494Sobrien * documentation and/or other materials provided with the distribution. 1338494Sobrien * 4. Neither the name of the University nor the names of its contributors 1438494Sobrien * may be used to endorse or promote products derived from this software 1538494Sobrien * without specific prior written permission. 1638494Sobrien * 1738494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1838494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1938494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2042629Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2138494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2238494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2338494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2438494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2538494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2638494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2738494Sobrien * SUCH DAMAGE. 2838494Sobrien * 2938494Sobrien * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 3038494Sobrien */ 3138494Sobrien 3238494Sobrien/* 3338494Sobrien * Ethernet address resolution protocol. 3438494Sobrien * TODO: 3538494Sobrien * add "inuse/lock" bit (or ref. count) along with valid bit 3638494Sobrien */ 3738494Sobrien 3838494Sobrien#include <sys/cdefs.h> 3938494Sobrien__FBSDID("$FreeBSD: head/sys/netinet/if_ether.c 172467 2007-10-07 20:44:24Z silby $"); 40174294Sobrien 4138494Sobrien#include "opt_inet.h" 4238494Sobrien#include "opt_mac.h" 4338494Sobrien#include "opt_carp.h" 4438494Sobrien 4538494Sobrien#include <sys/param.h> 4638494Sobrien#include <sys/kernel.h> 4738494Sobrien#include <sys/queue.h> 4838494Sobrien#include <sys/sysctl.h> 4938494Sobrien#include <sys/systm.h> 5038494Sobrien#include <sys/mbuf.h> 5138494Sobrien#include <sys/malloc.h> 5238494Sobrien#include <sys/socket.h> 5338494Sobrien#include <sys/syslog.h> 5438494Sobrien 5538494Sobrien#include <net/if.h> 5638494Sobrien#include <net/if_dl.h> 57174294Sobrien#include <net/if_types.h> 5838494Sobrien#include <net/route.h> 5938494Sobrien#include <net/netisr.h> 6038494Sobrien#include <net/if_llc.h> 6138494Sobrien#include <net/ethernet.h> 6238494Sobrien 6382794Sobrien#include <netinet/in.h> 6438494Sobrien#include <netinet/in_var.h> 6538494Sobrien#include <netinet/if_ether.h> 6638494Sobrien 6738494Sobrien#include <net/if_arc.h> 6838494Sobrien#include <net/iso88025.h> 6938494Sobrien 7038494Sobrien#ifdef DEV_CARP 7138494Sobrien#include <netinet/ip_carp.h> 7238494Sobrien#endif 7338494Sobrien 7438494Sobrien#include <security/mac/mac_framework.h> 7538494Sobrien 7638494Sobrien#define SIN(s) ((struct sockaddr_in *)s) 7738494Sobrien#define SDL(s) ((struct sockaddr_dl *)s) 7838494Sobrien 7938494SobrienSYSCTL_DECL(_net_link_ether); 8038494SobrienSYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 8138494Sobrien 8238494Sobrien/* timer values */ 8338494Sobrienstatic int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 8438494Sobrien 8538494SobrienSYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 8638494Sobrien &arpt_keep, 0, "ARP entry lifetime in seconds"); 8738494Sobrien 88174294Sobrien#define rt_expire rt_rmx.rmx_expire 8938494Sobrien 9038494Sobrienstruct llinfo_arp { 9138494Sobrien struct callout la_timer; 9238494Sobrien struct rtentry *la_rt; 9338494Sobrien struct mbuf *la_hold; /* last packet until resolved/timeout */ 9438494Sobrien u_short la_preempt; /* countdown for pre-expiry arps */ 9538494Sobrien u_short la_asked; /* # requests sent */ 9638494Sobrien}; 9738494Sobrien 98174294Sobrienstatic struct ifqueue arpintrq; 99174294Sobrienstatic int arp_allocated; 100174294Sobrien 101174294Sobrienstatic int arp_maxtries = 5; 102174294Sobrienstatic int useloopback = 1; /* use loopback interface for local traffic */ 103174294Sobrienstatic int arp_proxyall = 0; 10438494Sobrien 10538494SobrienSYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 10638494Sobrien &arp_maxtries, 0, "ARP resolution attempts before returning error"); 10738494SobrienSYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 10838494Sobrien &useloopback, 0, "Use the loopback interface for local traffic"); 10938494SobrienSYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 11038494Sobrien &arp_proxyall, 0, "Enable proxy ARP for all suitable requests"); 11138494Sobrien 11238494Sobrienstatic void arp_init(void); 11338494Sobrienstatic void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 11438494Sobrienstatic void arprequest(struct ifnet *, 11538494Sobrien struct in_addr *, struct in_addr *, u_char *); 11638494Sobrienstatic void arpintr(struct mbuf *); 11738494Sobrienstatic void arptimer(void *); 11838494Sobrienstatic struct rtentry 11938494Sobrien *arplookup(u_long, int, int); 12038494Sobrien#ifdef INET 12138494Sobrienstatic void in_arpinput(struct mbuf *); 12238494Sobrien#endif 12338494Sobrien 12438494Sobrien/* 12538494Sobrien * Timeout routine. 12638494Sobrien */ 12738494Sobrienstatic void 12838494Sobrienarptimer(void *arg) 12938494Sobrien{ 13038494Sobrien struct rtentry *rt = (struct rtentry *)arg; 13138494Sobrien 13238494Sobrien RT_LOCK_ASSERT(rt); 13338494Sobrien /* 13438494Sobrien * The lock is needed to close a theoretical race 13538494Sobrien * between spontaneous expiry and intentional removal. 13638494Sobrien * We still got an extra reference on rtentry, so can 13738494Sobrien * safely pass pointers to its contents. 13838494Sobrien */ 13938494Sobrien RT_UNLOCK(rt); 14038494Sobrien 14138494Sobrien rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL); 14238494Sobrien} 14338494Sobrien 14438494Sobrien/* 14538494Sobrien * Parallel to llc_rtrequest. 14638494Sobrien */ 14738494Sobrienstatic void 14838494Sobrienarp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info) 14938494Sobrien{ 15038494Sobrien struct sockaddr *gate; 15138494Sobrien struct llinfo_arp *la; 15238494Sobrien static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 15338494Sobrien struct in_ifaddr *ia; 15438494Sobrien struct ifaddr *ifa; 15538494Sobrien 15638494Sobrien RT_LOCK_ASSERT(rt); 15738494Sobrien 15838494Sobrien if (rt->rt_flags & RTF_GATEWAY) 15938494Sobrien return; 16038494Sobrien gate = rt->rt_gateway; 16138494Sobrien la = (struct llinfo_arp *)rt->rt_llinfo; 16238494Sobrien switch (req) { 16338494Sobrien 16438494Sobrien case RTM_ADD: 16538494Sobrien /* 16638494Sobrien * XXX: If this is a manually added route to interface 16738494Sobrien * such as older version of routed or gated might provide, 16838494Sobrien * restore cloning bit. 16938494Sobrien */ 17038494Sobrien if ((rt->rt_flags & RTF_HOST) == 0 && 17138494Sobrien rt_mask(rt) != NULL && 17238494Sobrien SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 17338494Sobrien rt->rt_flags |= RTF_CLONING; 17438494Sobrien if (rt->rt_flags & RTF_CLONING) { 17538494Sobrien /* 17638494Sobrien * Case 1: This route should come from a route to iface. 17738494Sobrien */ 17838494Sobrien rt_setgate(rt, rt_key(rt), 17938494Sobrien (struct sockaddr *)&null_sdl); 18038494Sobrien gate = rt->rt_gateway; 18138494Sobrien SDL(gate)->sdl_type = rt->rt_ifp->if_type; 18238494Sobrien SDL(gate)->sdl_index = rt->rt_ifp->if_index; 18338494Sobrien rt->rt_expire = time_uptime; 18438494Sobrien break; 18538494Sobrien } 18638494Sobrien /* Announce a new entry if requested. */ 18738494Sobrien if (rt->rt_flags & RTF_ANNOUNCE) 18838494Sobrien arprequest(rt->rt_ifp, 18938494Sobrien &SIN(rt_key(rt))->sin_addr, 19038494Sobrien &SIN(rt_key(rt))->sin_addr, 19138494Sobrien (u_char *)LLADDR(SDL(gate))); 19238494Sobrien /*FALLTHROUGH*/ 19338494Sobrien case RTM_RESOLVE: 19438494Sobrien if (gate->sa_family != AF_LINK || 19538494Sobrien gate->sa_len < sizeof(null_sdl)) { 19638494Sobrien log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__, 19738494Sobrien inet_ntoa(SIN(rt_key(rt))->sin_addr), 19838494Sobrien (gate->sa_family != AF_LINK) ? 19938494Sobrien " (!AF_LINK)": ""); 200174294Sobrien break; 20138494Sobrien } 20238494Sobrien SDL(gate)->sdl_type = rt->rt_ifp->if_type; 20338494Sobrien SDL(gate)->sdl_index = rt->rt_ifp->if_index; 20438494Sobrien if (la != 0) 20538494Sobrien break; /* This happens on a route change */ 20638494Sobrien /* 20738494Sobrien * Case 2: This route may come from cloning, or a manual route 20838494Sobrien * add with a LL address. 209174294Sobrien */ 21038494Sobrien R_Zalloc(la, struct llinfo_arp *, sizeof(*la)); 21138494Sobrien rt->rt_llinfo = (caddr_t)la; 21238494Sobrien if (la == 0) { 21338494Sobrien log(LOG_DEBUG, "%s: malloc failed\n", __func__); 21438494Sobrien break; 21538494Sobrien } 21638494Sobrien arp_allocated++; 21738494Sobrien /* 21838494Sobrien * We are storing a route entry outside of radix tree. So, 21938494Sobrien * it can be found and accessed by other means than radix 22038494Sobrien * lookup. The routing code assumes that any rtentry detached 22138494Sobrien * from radix can be destroyed safely. To prevent this, we 22238494Sobrien * add an additional reference. 22338494Sobrien */ 22438494Sobrien RT_ADDREF(rt); 22538494Sobrien la->la_rt = rt; 22638494Sobrien rt->rt_flags |= RTF_LLINFO; 227174294Sobrien callout_init_mtx(&la->la_timer, &rt->rt_mtx, 22838494Sobrien CALLOUT_RETURNUNLOCKED); 22938494Sobrien 23038494Sobrien#ifdef INET 23138494Sobrien /* 23238494Sobrien * This keeps the multicast addresses from showing up 23338494Sobrien * in `arp -a' listings as unresolved. It's not actually 23438494Sobrien * functional. Then the same for broadcast. 23538494Sobrien */ 23638494Sobrien if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) && 23738494Sobrien rt->rt_ifp->if_type != IFT_ARCNET) { 23838494Sobrien ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 23938494Sobrien LLADDR(SDL(gate))); 24038494Sobrien SDL(gate)->sdl_alen = 6; 24138494Sobrien rt->rt_expire = 0; 24238494Sobrien } 24338494Sobrien if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 24438494Sobrien memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, 24538494Sobrien rt->rt_ifp->if_addrlen); 24638494Sobrien SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; 247174294Sobrien rt->rt_expire = 0; 24838494Sobrien } 24938494Sobrien#endif 25038494Sobrien 25138494Sobrien TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 25238494Sobrien if (ia->ia_ifp == rt->rt_ifp && 25338494Sobrien SIN(rt_key(rt))->sin_addr.s_addr == 254 (IA_SIN(ia))->sin_addr.s_addr) 255 break; 256 } 257 if (ia) { 258 /* 259 * This test used to be 260 * if (loif.if_flags & IFF_UP) 261 * It allowed local traffic to be forced 262 * through the hardware by configuring the loopback down. 263 * However, it causes problems during network configuration 264 * for boards that can't receive packets they send. 265 * It is now necessary to clear "useloopback" and remove 266 * the route to force traffic out to the hardware. 267 */ 268 rt->rt_expire = 0; 269 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 270 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 271 if (useloopback) { 272 rt->rt_ifp = loif; 273 rt->rt_rmx.rmx_mtu = loif->if_mtu; 274 } 275 276 /* 277 * make sure to set rt->rt_ifa to the interface 278 * address we are using, otherwise we will have trouble 279 * with source address selection. 280 */ 281 ifa = &ia->ia_ifa; 282 if (ifa != rt->rt_ifa) { 283 IFAFREE(rt->rt_ifa); 284 IFAREF(ifa); 285 rt->rt_ifa = ifa; 286 } 287 } 288 break; 289 290 case RTM_DELETE: 291 if (la == NULL) /* XXX: at least CARP does this. */ 292 break; 293 callout_stop(&la->la_timer); 294 rt->rt_llinfo = NULL; 295 rt->rt_flags &= ~RTF_LLINFO; 296 RT_REMREF(rt); 297 if (la->la_hold) 298 m_freem(la->la_hold); 299 Free((caddr_t)la); 300 } 301} 302 303/* 304 * Broadcast an ARP request. Caller specifies: 305 * - arp header source ip address 306 * - arp header target ip address 307 * - arp header source ethernet address 308 */ 309static void 310arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip, 311 u_char *enaddr) 312{ 313 struct mbuf *m; 314 struct arphdr *ah; 315 struct sockaddr sa; 316 317 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 318 return; 319 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 320 2*ifp->if_data.ifi_addrlen; 321 m->m_pkthdr.len = m->m_len; 322 MH_ALIGN(m, m->m_len); 323 ah = mtod(m, struct arphdr *); 324 bzero((caddr_t)ah, m->m_len); 325#ifdef MAC 326 mac_create_mbuf_linklayer(ifp, m); 327#endif 328 ah->ar_pro = htons(ETHERTYPE_IP); 329 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 330 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 331 ah->ar_op = htons(ARPOP_REQUEST); 332 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 333 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 334 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 335 sa.sa_family = AF_ARP; 336 sa.sa_len = 2; 337 m->m_flags |= M_BCAST; 338 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 339 340 return; 341} 342 343/* 344 * Resolve an IP address into an ethernet address. 345 * On input: 346 * ifp is the interface we use 347 * dst is the next hop, 348 * rt0 is the route to the final destination (possibly useless) 349 * m is the mbuf 350 * desten is where we want the address. 351 * 352 * On success, desten is filled in and the function returns 0; 353 * If the packet must be held pending resolution, we return EWOULDBLOCK 354 * On other errors, we return the corresponding error code. 355 */ 356int 357arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 358 struct sockaddr *dst, u_char *desten) 359{ 360 struct llinfo_arp *la = NULL; 361 struct rtentry *rt = NULL; 362 struct sockaddr_dl *sdl; 363 int error; 364 365 if (m->m_flags & M_BCAST) { /* broadcast */ 366 (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen); 367 return (0); 368 } 369 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */ 370 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 371 return (0); 372 } 373 374 if (rt0 != NULL) { 375 error = rt_check(&rt, &rt0, dst); 376 if (error) { 377 m_freem(m); 378 return error; 379 } 380 la = (struct llinfo_arp *)rt->rt_llinfo; 381 if (la == NULL) 382 RT_UNLOCK(rt); 383 } 384 if (la == NULL) { 385 /* 386 * We enter this block in case if rt0 was NULL, 387 * or if rt found by rt_check() didn't have llinfo. 388 */ 389 rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 390 if (rt == NULL) { 391 log(LOG_DEBUG, 392 "arpresolve: can't allocate route for %s\n", 393 inet_ntoa(SIN(dst)->sin_addr)); 394 m_freem(m); 395 return (EINVAL); /* XXX */ 396 } 397 la = (struct llinfo_arp *)rt->rt_llinfo; 398 if (la == NULL) { 399 RT_UNLOCK(rt); 400 log(LOG_DEBUG, 401 "arpresolve: can't allocate llinfo for %s\n", 402 inet_ntoa(SIN(dst)->sin_addr)); 403 m_freem(m); 404 return (EINVAL); /* XXX */ 405 } 406 } 407 sdl = SDL(rt->rt_gateway); 408 /* 409 * Check the address family and length is valid, the address 410 * is resolved; otherwise, try to resolve. 411 */ 412 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) && 413 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 414 415 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 416 417 /* 418 * If entry has an expiry time and it is approaching, 419 * send an ARP request. 420 */ 421 if ((rt->rt_expire != 0) && 422 (time_uptime + la->la_preempt > rt->rt_expire)) { 423 struct in_addr sin = 424 SIN(rt->rt_ifa->ifa_addr)->sin_addr; 425 426 la->la_preempt--; 427 RT_UNLOCK(rt); 428 arprequest(ifp, &sin, &SIN(dst)->sin_addr, 429 IF_LLADDR(ifp)); 430 return (0); 431 } 432 433 RT_UNLOCK(rt); 434 return (0); 435 } 436 /* 437 * If ARP is disabled or static on this interface, stop. 438 * XXX 439 * Probably should not allocate empty llinfo struct if we are 440 * not going to be sending out an arp request. 441 */ 442 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) { 443 RT_UNLOCK(rt); 444 m_freem(m); 445 return (EINVAL); 446 } 447 /* 448 * There is an arptab entry, but no ethernet address 449 * response yet. Replace the held mbuf with this 450 * latest one. 451 */ 452 if (la->la_hold) 453 m_freem(la->la_hold); 454 la->la_hold = m; 455 456 KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry")); 457 458 /* 459 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 460 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 461 * if we have already sent arp_maxtries ARP requests. Retransmit the 462 * ARP request, but not faster than one request per second. 463 */ 464 if (la->la_asked < arp_maxtries) 465 error = EWOULDBLOCK; /* First request. */ 466 else 467 error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH; 468 469 if (la->la_asked == 0 || rt->rt_expire != time_uptime) { 470 struct in_addr sin = 471 SIN(rt->rt_ifa->ifa_addr)->sin_addr; 472 473 rt->rt_expire = time_uptime; 474 callout_reset(&la->la_timer, hz, arptimer, rt); 475 la->la_asked++; 476 RT_UNLOCK(rt); 477 478 arprequest(ifp, &sin, &SIN(dst)->sin_addr, 479 IF_LLADDR(ifp)); 480 } else 481 RT_UNLOCK(rt); 482 483 return (error); 484} 485 486/* 487 * Common length and type checks are done here, 488 * then the protocol-specific routine is called. 489 */ 490static void 491arpintr(struct mbuf *m) 492{ 493 struct arphdr *ar; 494 495 if (m->m_len < sizeof(struct arphdr) && 496 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 497 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 498 return; 499 } 500 ar = mtod(m, struct arphdr *); 501 502 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 503 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 504 ntohs(ar->ar_hrd) != ARPHRD_ARCNET && 505 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) { 506 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 507 (unsigned char *)&ar->ar_hrd, ""); 508 m_freem(m); 509 return; 510 } 511 512 if (m->m_len < arphdr_len(ar)) { 513 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 514 log(LOG_ERR, "arp: runt packet\n"); 515 m_freem(m); 516 return; 517 } 518 ar = mtod(m, struct arphdr *); 519 } 520 521 switch (ntohs(ar->ar_pro)) { 522#ifdef INET 523 case ETHERTYPE_IP: 524 in_arpinput(m); 525 return; 526#endif 527 } 528 m_freem(m); 529} 530 531#ifdef INET 532/* 533 * ARP for Internet protocols on 10 Mb/s Ethernet. 534 * Algorithm is that given in RFC 826. 535 * In addition, a sanity check is performed on the sender 536 * protocol address, to catch impersonators. 537 * We no longer handle negotiations for use of trailer protocol: 538 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 539 * along with IP replies if we wanted trailers sent to us, 540 * and also sent them in response to IP replies. 541 * This allowed either end to announce the desire to receive 542 * trailer packets. 543 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 544 * but formerly didn't normally send requests. 545 */ 546static int log_arp_wrong_iface = 1; 547static int log_arp_movements = 1; 548static int log_arp_permanent_modify = 1; 549 550SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 551 &log_arp_wrong_iface, 0, 552 "log arp packets arriving on the wrong interface"); 553SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 554 &log_arp_movements, 0, 555 "log arp replies from MACs different than the one in the cache"); 556SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW, 557 &log_arp_permanent_modify, 0, 558 "log arp replies from MACs different than the one in the permanent arp entry"); 559 560 561static void 562in_arpinput(struct mbuf *m) 563{ 564 struct arphdr *ah; 565 struct ifnet *ifp = m->m_pkthdr.rcvif; 566 struct llinfo_arp *la; 567 struct rtentry *rt; 568 struct ifaddr *ifa; 569 struct in_ifaddr *ia; 570 struct sockaddr_dl *sdl; 571 struct sockaddr sa; 572 struct in_addr isaddr, itaddr, myaddr; 573 struct mbuf *hold; 574 u_int8_t *enaddr = NULL; 575 int op, rif_len; 576 int req_len; 577 int bridged = 0; 578#ifdef DEV_CARP 579 int carp_match = 0; 580#endif 581 582 if (ifp->if_bridge) 583 bridged = 1; 584 585 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 586 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 587 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 588 return; 589 } 590 591 ah = mtod(m, struct arphdr *); 592 op = ntohs(ah->ar_op); 593 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 594 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 595 596 /* 597 * For a bridge, we want to check the address irrespective 598 * of the receive interface. (This will change slightly 599 * when we have clusters of interfaces). 600 * If the interface does not match, but the recieving interface 601 * is part of carp, we call carp_iamatch to see if this is a 602 * request for the virtual host ip. 603 * XXX: This is really ugly! 604 */ 605 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 606 if (((bridged && ia->ia_ifp->if_bridge != NULL) || 607 (ia->ia_ifp == ifp)) && 608 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 609 goto match; 610#ifdef DEV_CARP 611 if (ifp->if_carp != NULL && 612 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) && 613 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 614 carp_match = 1; 615 goto match; 616 } 617#endif 618 } 619 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 620 if (((bridged && ia->ia_ifp->if_bridge != NULL) || 621 (ia->ia_ifp == ifp)) && 622 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 623 goto match; 624 /* 625 * No match, use the first inet address on the receive interface 626 * as a dummy address for the rest of the function. 627 */ 628 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 629 if (ifa->ifa_addr->sa_family == AF_INET) { 630 ia = ifatoia(ifa); 631 goto match; 632 } 633 /* 634 * If bridging, fall back to using any inet address. 635 */ 636 if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) 637 goto drop; 638match: 639 if (!enaddr) 640 enaddr = (u_int8_t *)IF_LLADDR(ifp); 641 myaddr = ia->ia_addr.sin_addr; 642 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 643 goto drop; /* it's from me, ignore it. */ 644 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 645 log(LOG_ERR, 646 "arp: link address is broadcast for IP address %s!\n", 647 inet_ntoa(isaddr)); 648 goto drop; 649 } 650 /* 651 * Warn if another host is using the same IP address, but only if the 652 * IP address isn't 0.0.0.0, which is used for DHCP only, in which 653 * case we suppress the warning to avoid false positive complaints of 654 * potential misconfiguration. 655 */ 656 if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { 657 log(LOG_ERR, 658 "arp: %*D is using my IP address %s!\n", 659 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 660 inet_ntoa(isaddr)); 661 itaddr = myaddr; 662 goto reply; 663 } 664 if (ifp->if_flags & IFF_STATICARP) 665 goto reply; 666 rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 667 if (rt != NULL) { 668 la = (struct llinfo_arp *)rt->rt_llinfo; 669 if (la == NULL) { 670 RT_UNLOCK(rt); 671 goto reply; 672 } 673 } else 674 goto reply; 675 676 /* The following is not an error when doing bridging. */ 677 if (!bridged && rt->rt_ifp != ifp 678#ifdef DEV_CARP 679 && (ifp->if_type != IFT_CARP || !carp_match) 680#endif 681 ) { 682 if (log_arp_wrong_iface) 683 log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n", 684 inet_ntoa(isaddr), 685 rt->rt_ifp->if_xname, 686 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 687 ifp->if_xname); 688 RT_UNLOCK(rt); 689 goto reply; 690 } 691 sdl = SDL(rt->rt_gateway); 692 if (sdl->sdl_alen && 693 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 694 if (rt->rt_expire) { 695 if (log_arp_movements) 696 log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n", 697 inet_ntoa(isaddr), 698 ifp->if_addrlen, (u_char *)LLADDR(sdl), ":", 699 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 700 ifp->if_xname); 701 } else { 702 RT_UNLOCK(rt); 703 if (log_arp_permanent_modify) 704 log(LOG_ERR, "arp: %*D attempts to modify " 705 "permanent entry for %s on %s\n", 706 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 707 inet_ntoa(isaddr), ifp->if_xname); 708 goto reply; 709 } 710 } 711 /* 712 * sanity check for the address length. 713 * XXX this does not work for protocols with variable address 714 * length. -is 715 */ 716 if (sdl->sdl_alen && 717 sdl->sdl_alen != ah->ar_hln) { 718 log(LOG_WARNING, 719 "arp from %*D: new addr len %d, was %d", 720 ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 721 ah->ar_hln, sdl->sdl_alen); 722 } 723 if (ifp->if_addrlen != ah->ar_hln) { 724 log(LOG_WARNING, 725 "arp from %*D: addr len: new %d, i/f %d (ignored)", 726 ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 727 ah->ar_hln, ifp->if_addrlen); 728 RT_UNLOCK(rt); 729 goto reply; 730 } 731 (void)memcpy(LLADDR(sdl), ar_sha(ah), 732 sdl->sdl_alen = ah->ar_hln); 733 /* 734 * If we receive an arp from a token-ring station over 735 * a token-ring nic then try to save the source 736 * routing info. 737 */ 738 if (ifp->if_type == IFT_ISO88025) { 739 struct iso88025_header *th = NULL; 740 struct iso88025_sockaddr_dl_data *trld; 741 742 th = (struct iso88025_header *)m->m_pkthdr.header; 743 trld = SDL_ISO88025(sdl); 744 rif_len = TR_RCF_RIFLEN(th->rcf); 745 if ((th->iso88025_shost[0] & TR_RII) && 746 (rif_len > 2)) { 747 trld->trld_rcf = th->rcf; 748 trld->trld_rcf ^= htons(TR_RCF_DIR); 749 memcpy(trld->trld_route, th->rd, rif_len - 2); 750 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK); 751 /* 752 * Set up source routing information for 753 * reply packet (XXX) 754 */ 755 m->m_data -= rif_len; 756 m->m_len += rif_len; 757 m->m_pkthdr.len += rif_len; 758 } else { 759 th->iso88025_shost[0] &= ~TR_RII; 760 trld->trld_rcf = 0; 761 } 762 m->m_data -= 8; 763 m->m_len += 8; 764 m->m_pkthdr.len += 8; 765 th->rcf = trld->trld_rcf; 766 } 767 if (rt->rt_expire) { 768 rt->rt_expire = time_uptime + arpt_keep; 769 callout_reset(&la->la_timer, hz * arpt_keep, arptimer, rt); 770 } 771 la->la_asked = 0; 772 la->la_preempt = arp_maxtries; 773 hold = la->la_hold; 774 la->la_hold = NULL; 775 RT_UNLOCK(rt); 776 if (hold != NULL) 777 (*ifp->if_output)(ifp, hold, rt_key(rt), rt); 778 779reply: 780 if (op != ARPOP_REQUEST) 781 goto drop; 782 if (itaddr.s_addr == myaddr.s_addr) { 783 /* I am the target */ 784 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 785 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 786 } else { 787 rt = arplookup(itaddr.s_addr, 0, SIN_PROXY); 788 if (rt == NULL) { 789 struct sockaddr_in sin; 790 791 if (!arp_proxyall) 792 goto drop; 793 794 bzero(&sin, sizeof sin); 795 sin.sin_family = AF_INET; 796 sin.sin_len = sizeof sin; 797 sin.sin_addr = itaddr; 798 799 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 800 if (!rt) 801 goto drop; 802 /* 803 * Don't send proxies for nodes on the same interface 804 * as this one came out of, or we'll get into a fight 805 * over who claims what Ether address. 806 */ 807 if (rt->rt_ifp == ifp) { 808 rtfree(rt); 809 goto drop; 810 } 811 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 812 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 813 rtfree(rt); 814 815 /* 816 * Also check that the node which sent the ARP packet 817 * is on the the interface we expect it to be on. This 818 * avoids ARP chaos if an interface is connected to the 819 * wrong network. 820 */ 821 sin.sin_addr = isaddr; 822 823 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 824 if (!rt) 825 goto drop; 826 if (rt->rt_ifp != ifp) { 827 log(LOG_INFO, "arp_proxy: ignoring request" 828 " from %s via %s, expecting %s\n", 829 inet_ntoa(isaddr), ifp->if_xname, 830 rt->rt_ifp->if_xname); 831 rtfree(rt); 832 goto drop; 833 } 834 rtfree(rt); 835 836#ifdef DEBUG_PROXY 837 printf("arp: proxying for %s\n", 838 inet_ntoa(itaddr)); 839#endif 840 } else { 841 /* 842 * Return proxied ARP replies only on the interface 843 * or bridge cluster where this network resides. 844 * Otherwise we may conflict with the host we are 845 * proxying for. 846 */ 847 if (rt->rt_ifp != ifp && 848 (rt->rt_ifp->if_bridge != ifp->if_bridge || 849 ifp->if_bridge == NULL)) { 850 RT_UNLOCK(rt); 851 goto drop; 852 } 853 sdl = SDL(rt->rt_gateway); 854 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 855 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 856 RT_UNLOCK(rt); 857 } 858 } 859 860 if (itaddr.s_addr == myaddr.s_addr && 861 IN_LINKLOCAL(ntohl(itaddr.s_addr))) { 862 /* RFC 3927 link-local IPv4; always reply by broadcast. */ 863#ifdef DEBUG_LINKLOCAL 864 printf("arp: sending reply for link-local addr %s\n", 865 inet_ntoa(itaddr)); 866#endif 867 m->m_flags |= M_BCAST; 868 m->m_flags &= ~M_MCAST; 869 } else { 870 /* default behaviour; never reply by broadcast. */ 871 m->m_flags &= ~(M_BCAST|M_MCAST); 872 } 873 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 874 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 875 ah->ar_op = htons(ARPOP_REPLY); 876 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 877 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 878 m->m_pkthdr.len = m->m_len; 879 sa.sa_family = AF_ARP; 880 sa.sa_len = 2; 881 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 882 return; 883 884drop: 885 m_freem(m); 886} 887#endif 888 889/* 890 * Lookup or enter a new address in arptab. 891 */ 892static struct rtentry * 893arplookup(u_long addr, int create, int proxy) 894{ 895 struct rtentry *rt; 896 struct sockaddr_inarp sin; 897 const char *why = 0; 898 899 bzero(&sin, sizeof(sin)); 900 sin.sin_len = sizeof(sin); 901 sin.sin_family = AF_INET; 902 sin.sin_addr.s_addr = addr; 903 if (proxy) 904 sin.sin_other = SIN_PROXY; 905 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 906 if (rt == 0) 907 return (0); 908 909 if (rt->rt_flags & RTF_GATEWAY) 910 why = "host is not on local network"; 911 else if ((rt->rt_flags & RTF_LLINFO) == 0) 912 why = "could not allocate llinfo"; 913 else if (rt->rt_gateway->sa_family != AF_LINK) 914 why = "gateway route is not ours"; 915 916 if (why) { 917#define ISDYNCLONE(_rt) \ 918 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED) 919 if (create) 920 log(LOG_DEBUG, "arplookup %s failed: %s\n", 921 inet_ntoa(sin.sin_addr), why); 922 /* 923 * If there are no references to this Layer 2 route, 924 * and it is a cloned route, and not static, and 925 * arplookup() is creating the route, then purge 926 * it from the routing table as it is probably bogus. 927 */ 928 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt)) 929 rtexpunge(rt); 930 RTFREE_LOCKED(rt); 931 return (0); 932#undef ISDYNCLONE 933 } else { 934 RT_REMREF(rt); 935 return (rt); 936 } 937} 938 939void 940arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 941{ 942 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 943 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 944 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 945 ifa->ifa_rtrequest = arp_rtrequest; 946 ifa->ifa_flags |= RTF_CLONING; 947} 948 949void 950arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr) 951{ 952 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 953 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 954 &IA_SIN(ifa)->sin_addr, enaddr); 955 ifa->ifa_rtrequest = arp_rtrequest; 956 ifa->ifa_flags |= RTF_CLONING; 957} 958 959static void 960arp_init(void) 961{ 962 963 arpintrq.ifq_maxlen = 50; 964 mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF); 965 netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE); 966} 967SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 968