1 /* 2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Jeffrey M. Hsu. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of The DragonFly Project nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific, prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1988, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 66 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $ 67 * $DragonFly: src/sys/netinet/if_ether.c,v 1.56 2008/10/04 11:26:09 sephe Exp $ 68 */ 69 70 /* 71 * Ethernet address resolution protocol. 72 * TODO: 73 * add "inuse/lock" bit (or ref. count) along with valid bit 74 */ 75 76 #include "opt_inet.h" 77 #include "opt_carp.h" 78 79 #include <sys/param.h> 80 #include <sys/kernel.h> 81 #include <sys/queue.h> 82 #include <sys/sysctl.h> 83 #include <sys/systm.h> 84 #include <sys/mbuf.h> 85 #include <sys/malloc.h> 86 #include <sys/socket.h> 87 #include <sys/syslog.h> 88 89 #include <net/if.h> 90 #include <net/if_dl.h> 91 #include <net/if_types.h> 92 #include <net/route.h> 93 #include <net/netisr.h> 94 #include <net/if_llc.h> 95 96 #include <netinet/in.h> 97 #include <netinet/in_var.h> 98 #include <netinet/if_ether.h> 99 100 #include <sys/thread2.h> 101 #include <sys/msgport2.h> 102 #include <net/netmsg2.h> 103 104 #ifdef CARP 105 #include <netinet/ip_carp.h> 106 #endif 107 108 #define SIN(s) ((struct sockaddr_in *)s) 109 #define SDL(s) ((struct sockaddr_dl *)s) 110 111 SYSCTL_DECL(_net_link_ether); 112 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 113 114 /* timer values */ 115 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 116 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 117 static int arpt_down = 20; /* once declared down, don't send for 20 sec */ 118 119 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, 120 &arpt_prune, 0, ""); 121 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 122 &arpt_keep, 0, ""); 123 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, 124 &arpt_down, 0, ""); 125 126 #define rt_expire rt_rmx.rmx_expire 127 128 struct llinfo_arp { 129 LIST_ENTRY(llinfo_arp) la_le; 130 struct rtentry *la_rt; 131 struct mbuf *la_hold; /* last packet until resolved/timeout */ 132 u_short la_preempt; /* countdown for pre-expiry arps */ 133 u_short la_asked; /* #times we QUERIED following expiration */ 134 }; 135 136 static LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU]; 137 138 static int arp_maxtries = 5; 139 static int useloopback = 1; /* use loopback interface for local traffic */ 140 static int arp_proxyall = 0; 141 142 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 143 &arp_maxtries, 0, ""); 144 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 145 &useloopback, 0, ""); 146 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 147 &arp_proxyall, 0, ""); 148 149 static int arp_mpsafe = 0; 150 TUNABLE_INT("net.link.ether.inet.arp_mpsafe", &arp_mpsafe); 151 152 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 153 static void arprequest(struct ifnet *, struct in_addr *, struct in_addr *, 154 const u_char *); 155 static void arprequest_async(struct ifnet *, struct in_addr *, 156 struct in_addr *, const u_char *); 157 static void arpintr(struct netmsg *); 158 static void arptfree(struct llinfo_arp *); 159 static void arptimer(void *); 160 static struct llinfo_arp * 161 arplookup(in_addr_t, boolean_t, boolean_t); 162 #ifdef INET 163 static void in_arpinput(struct mbuf *); 164 #endif 165 166 static struct callout arptimer_ch[MAXCPU]; 167 168 /* 169 * Timeout routine. Age arp_tab entries periodically. 170 */ 171 /* ARGSUSED */ 172 static void 173 arptimer(void *ignored_arg) 174 { 175 struct llinfo_arp *la, *nla; 176 177 crit_enter(); 178 LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) { 179 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second) 180 arptfree(la); 181 } 182 callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL); 183 crit_exit(); 184 } 185 186 /* 187 * Parallel to llc_rtrequest. 188 */ 189 static void 190 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info) 191 { 192 struct sockaddr *gate = rt->rt_gateway; 193 struct llinfo_arp *la = rt->rt_llinfo; 194 195 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK }; 196 static boolean_t arpinit_done[MAXCPU]; 197 198 if (!arpinit_done[mycpuid]) { 199 arpinit_done[mycpuid] = TRUE; 200 callout_init(&arptimer_ch[mycpuid]); 201 callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL); 202 } 203 if (rt->rt_flags & RTF_GATEWAY) 204 return; 205 206 switch (req) { 207 case RTM_ADD: 208 /* 209 * XXX: If this is a manually added route to interface 210 * such as older version of routed or gated might provide, 211 * restore cloning bit. 212 */ 213 if (!(rt->rt_flags & RTF_HOST) && 214 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 215 rt->rt_flags |= RTF_CLONING; 216 if (rt->rt_flags & RTF_CLONING) { 217 /* 218 * Case 1: This route should come from a route to iface. 219 */ 220 rt_setgate(rt, rt_key(rt), 221 (struct sockaddr *)&null_sdl); 222 gate = rt->rt_gateway; 223 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 224 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 225 rt->rt_expire = time_second; 226 break; 227 } 228 /* Announce a new entry if requested. */ 229 if (rt->rt_flags & RTF_ANNOUNCE) { 230 arprequest_async(rt->rt_ifp, 231 &SIN(rt_key(rt))->sin_addr, 232 &SIN(rt_key(rt))->sin_addr, 233 LLADDR(SDL(gate))); 234 } 235 /*FALLTHROUGH*/ 236 case RTM_RESOLVE: 237 if (gate->sa_family != AF_LINK || 238 gate->sa_len < sizeof(struct sockaddr_dl)) { 239 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 240 break; 241 } 242 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 243 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 244 if (la != NULL) 245 break; /* This happens on a route change */ 246 /* 247 * Case 2: This route may come from cloning, or a manual route 248 * add with a LL address. 249 */ 250 R_Malloc(la, struct llinfo_arp *, sizeof *la); 251 rt->rt_llinfo = la; 252 if (la == NULL) { 253 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 254 break; 255 } 256 bzero(la, sizeof *la); 257 la->la_rt = rt; 258 rt->rt_flags |= RTF_LLINFO; 259 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], 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 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 269 LLADDR(SDL(gate))); 270 SDL(gate)->sdl_alen = 6; 271 rt->rt_expire = 0; 272 } 273 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 274 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, 275 rt->rt_ifp->if_addrlen); 276 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; 277 rt->rt_expire = 0; 278 } 279 #endif 280 281 if (SIN(rt_key(rt))->sin_addr.s_addr == 282 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 283 /* 284 * This test used to be 285 * if (loif.if_flags & IFF_UP) 286 * It allowed local traffic to be forced 287 * through the hardware by configuring the 288 * loopback down. However, it causes problems 289 * during network configuration for boards 290 * that can't receive packets they send. It 291 * is now necessary to clear "useloopback" and 292 * remove the route to force traffic out to 293 * the hardware. 294 */ 295 rt->rt_expire = 0; 296 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 297 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 298 if (useloopback) 299 rt->rt_ifp = loif; 300 } 301 break; 302 303 case RTM_DELETE: 304 if (la == NULL) 305 break; 306 LIST_REMOVE(la, la_le); 307 rt->rt_llinfo = NULL; 308 rt->rt_flags &= ~RTF_LLINFO; 309 if (la->la_hold != NULL) 310 m_freem(la->la_hold); 311 Free(la); 312 break; 313 } 314 } 315 316 static struct mbuf * 317 arpreq_alloc(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip, 318 const u_char *enaddr) 319 { 320 struct mbuf *m; 321 struct arphdr *ah; 322 u_short ar_hrd; 323 324 if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL) 325 return NULL; 326 m->m_pkthdr.rcvif = NULL; 327 328 switch (ifp->if_type) { 329 case IFT_ETHER: 330 /* 331 * This may not be correct for types not explicitly 332 * listed, but this is our best guess 333 */ 334 default: 335 ar_hrd = htons(ARPHRD_ETHER); 336 337 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 338 m->m_pkthdr.len = m->m_len; 339 MH_ALIGN(m, m->m_len); 340 341 ah = mtod(m, struct arphdr *); 342 break; 343 } 344 345 ah->ar_hrd = ar_hrd; 346 ah->ar_pro = htons(ETHERTYPE_IP); 347 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 348 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 349 ah->ar_op = htons(ARPOP_REQUEST); 350 memcpy(ar_sha(ah), enaddr, ah->ar_hln); 351 memset(ar_tha(ah), 0, ah->ar_hln); 352 memcpy(ar_spa(ah), sip, ah->ar_pln); 353 memcpy(ar_tpa(ah), tip, ah->ar_pln); 354 355 return m; 356 } 357 358 static void 359 arpreq_send(struct ifnet *ifp, struct mbuf *m) 360 { 361 struct sockaddr sa; 362 struct ether_header *eh; 363 364 switch (ifp->if_type) { 365 case IFT_ETHER: 366 /* 367 * This may not be correct for types not explicitly 368 * listed, but this is our best guess 369 */ 370 default: 371 eh = (struct ether_header *)sa.sa_data; 372 /* if_output() will not swap */ 373 eh->ether_type = htons(ETHERTYPE_ARP); 374 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen); 375 break; 376 } 377 378 sa.sa_family = AF_UNSPEC; 379 sa.sa_len = sizeof(sa); 380 ifp->if_output(ifp, m, &sa, NULL); 381 } 382 383 static void 384 arpreq_send_handler(struct netmsg *nmsg) 385 { 386 struct mbuf *m = ((struct netmsg_packet *)nmsg)->nm_packet; 387 struct ifnet *ifp = nmsg->nm_lmsg.u.ms_resultp; 388 389 arpreq_send(ifp, m); 390 /* nmsg was embedded in the mbuf, do not reply! */ 391 } 392 393 /* 394 * Broadcast an ARP request. Caller specifies: 395 * - arp header source ip address 396 * - arp header target ip address 397 * - arp header source ethernet address 398 * 399 * NOTE: Caller MUST NOT hold ifp's serializer 400 */ 401 static void 402 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip, 403 const u_char *enaddr) 404 { 405 struct mbuf *m; 406 407 m = arpreq_alloc(ifp, sip, tip, enaddr); 408 if (m == NULL) 409 return; 410 arpreq_send(ifp, m); 411 } 412 413 /* 414 * Same as arprequest(), except: 415 * - Caller is allowed to hold ifp's serializer 416 * - Network output is done in TDF_NETWORK kernel thread 417 */ 418 static void 419 arprequest_async(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip, 420 const u_char *enaddr) 421 { 422 struct mbuf *m; 423 struct netmsg_packet *pmsg; 424 425 m = arpreq_alloc(ifp, sip, tip, enaddr); 426 if (m == NULL) 427 return; 428 429 pmsg = &m->m_hdr.mh_netmsg; 430 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, 0, 431 arpreq_send_handler); 432 pmsg->nm_packet = m; 433 pmsg->nm_netmsg.nm_lmsg.u.ms_resultp = ifp; 434 435 lwkt_sendmsg(cpu_portfn(mycpuid), &pmsg->nm_netmsg.nm_lmsg); 436 } 437 438 /* 439 * Resolve an IP address into an ethernet address. If success, 440 * desten is filled in. If there is no entry in arptab, 441 * set one up and broadcast a request for the IP address. 442 * Hold onto this mbuf and resend it once the address 443 * is finally resolved. A return value of 1 indicates 444 * that desten has been filled in and the packet should be sent 445 * normally; a 0 return indicates that the packet has been 446 * taken over here, either now or for later transmission. 447 */ 448 int 449 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 450 struct sockaddr *dst, u_char *desten) 451 { 452 struct rtentry *rt; 453 struct llinfo_arp *la = NULL; 454 struct sockaddr_dl *sdl; 455 456 if (m->m_flags & M_BCAST) { /* broadcast */ 457 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen); 458 return (1); 459 } 460 if (m->m_flags & M_MCAST) {/* multicast */ 461 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 462 return (1); 463 } 464 if (rt0 != NULL) { 465 if (rt_llroute(dst, rt0, &rt) != 0) { 466 m_freem(m); 467 return 0; 468 } 469 la = rt->rt_llinfo; 470 } 471 if (la == NULL) { 472 la = arplookup(SIN(dst)->sin_addr.s_addr, TRUE, FALSE); 473 if (la != NULL) 474 rt = la->la_rt; 475 } 476 if (la == NULL || rt == NULL) { 477 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", 478 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ", 479 rt ? "rt" : ""); 480 m_freem(m); 481 return (0); 482 } 483 sdl = SDL(rt->rt_gateway); 484 /* 485 * Check the address family and length is valid, the address 486 * is resolved; otherwise, try to resolve. 487 */ 488 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && 489 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 490 /* 491 * If entry has an expiry time and it is approaching, 492 * see if we need to send an ARP request within this 493 * arpt_down interval. 494 */ 495 if ((rt->rt_expire != 0) && 496 (time_second + la->la_preempt > rt->rt_expire)) { 497 arprequest(ifp, 498 &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 499 &SIN(dst)->sin_addr, 500 IF_LLADDR(ifp)); 501 la->la_preempt--; 502 } 503 504 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 505 return 1; 506 } 507 /* 508 * If ARP is disabled on this interface, stop. 509 * XXX 510 * Probably should not allocate empty llinfo struct if we are 511 * not going to be sending out an arp request. 512 */ 513 if (ifp->if_flags & IFF_NOARP) { 514 m_freem(m); 515 return (0); 516 } 517 /* 518 * There is an arptab entry, but no ethernet address 519 * response yet. Replace the held mbuf with this 520 * latest one. 521 */ 522 if (la->la_hold != NULL) 523 m_freem(la->la_hold); 524 la->la_hold = m; 525 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) { 526 rt->rt_flags &= ~RTF_REJECT; 527 if (la->la_asked == 0 || rt->rt_expire != time_second) { 528 rt->rt_expire = time_second; 529 if (la->la_asked++ < arp_maxtries) { 530 arprequest(ifp, 531 &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 532 &SIN(dst)->sin_addr, 533 IF_LLADDR(ifp)); 534 } else { 535 rt->rt_flags |= RTF_REJECT; 536 rt->rt_expire += arpt_down; 537 la->la_asked = 0; 538 la->la_preempt = arp_maxtries; 539 } 540 } 541 } 542 return (0); 543 } 544 545 /* 546 * Common length and type checks are done here, 547 * then the protocol-specific routine is called. 548 */ 549 static void 550 arpintr(struct netmsg *msg) 551 { 552 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet; 553 struct arphdr *ar; 554 u_short ar_hrd; 555 556 if (m->m_len < sizeof(struct arphdr) && 557 (m = m_pullup(m, sizeof(struct arphdr))) == NULL) { 558 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 559 return; 560 } 561 ar = mtod(m, struct arphdr *); 562 563 ar_hrd = ntohs(ar->ar_hrd); 564 if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) { 565 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 566 (unsigned char *)&ar->ar_hrd, ""); 567 m_freem(m); 568 return; 569 } 570 571 if (m->m_pkthdr.len < arphdr_len(ar)) { 572 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 573 log(LOG_ERR, "arp: runt packet\n"); 574 return; 575 } 576 ar = mtod(m, struct arphdr *); 577 } 578 579 switch (ntohs(ar->ar_pro)) { 580 #ifdef INET 581 case ETHERTYPE_IP: 582 in_arpinput(m); 583 return; 584 #endif 585 } 586 m_freem(m); 587 /* msg was embedded in the mbuf, do not reply! */ 588 } 589 590 #ifdef INET 591 /* 592 * ARP for Internet protocols on 10 Mb/s Ethernet. 593 * Algorithm is that given in RFC 826. 594 * In addition, a sanity check is performed on the sender 595 * protocol address, to catch impersonators. 596 * We no longer handle negotiations for use of trailer protocol: 597 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 598 * along with IP replies if we wanted trailers sent to us, 599 * and also sent them in response to IP replies. 600 * This allowed either end to announce the desire to receive 601 * trailer packets. 602 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 603 * but formerly didn't normally send requests. 604 */ 605 static int log_arp_wrong_iface = 1; 606 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 607 &log_arp_wrong_iface, 0, 608 "log arp packets arriving on the wrong interface"); 609 610 static void 611 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create, 612 boolean_t dologging) 613 { 614 struct arphdr *ah = mtod(m, struct arphdr *); 615 struct ifnet *ifp = m->m_pkthdr.rcvif; 616 struct llinfo_arp *la; 617 struct sockaddr_dl *sdl; 618 struct rtentry *rt; 619 620 la = arplookup(saddr, create, FALSE); 621 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 622 struct in_addr isaddr = { saddr }; 623 624 /* the following is not an error when doing bridging */ 625 if (rt->rt_ifp != ifp) { 626 if (dologging && log_arp_wrong_iface) { 627 log(LOG_ERR, 628 "arp: %s is on %s " 629 "but got reply from %*D on %s\n", 630 inet_ntoa(isaddr), 631 rt->rt_ifp->if_xname, 632 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 633 ifp->if_xname); 634 } 635 return; 636 } 637 if (sdl->sdl_alen && 638 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 639 if (rt->rt_expire != 0) { 640 if (dologging) { 641 log(LOG_INFO, 642 "arp: %s moved from %*D to %*D on %s\n", 643 inet_ntoa(isaddr), 644 ifp->if_addrlen, (u_char *)LLADDR(sdl), 645 ":", ifp->if_addrlen, 646 (u_char *)ar_sha(ah), ":", 647 ifp->if_xname); 648 } 649 } else { 650 if (dologging) { 651 log(LOG_ERR, 652 "arp: %*D attempts to modify " 653 "permanent entry for %s on %s\n", 654 ifp->if_addrlen, (u_char *)ar_sha(ah), 655 ":", inet_ntoa(isaddr), ifp->if_xname); 656 } 657 return; 658 } 659 } 660 /* 661 * sanity check for the address length. 662 * XXX this does not work for protocols with variable address 663 * length. -is 664 */ 665 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) { 666 log(LOG_WARNING, 667 "arp from %*D: new addr len %d, was %d", 668 ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 669 ah->ar_hln, sdl->sdl_alen); 670 } 671 if (ifp->if_addrlen != ah->ar_hln) { 672 if (dologging) { 673 log(LOG_WARNING, 674 "arp from %*D: addr len: new %d, i/f %d " 675 "(ignored)", 676 ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 677 ah->ar_hln, ifp->if_addrlen); 678 } 679 return; 680 } 681 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln); 682 if (rt->rt_expire != 0) 683 rt->rt_expire = time_second + arpt_keep; 684 rt->rt_flags &= ~RTF_REJECT; 685 la->la_asked = 0; 686 la->la_preempt = arp_maxtries; 687 688 /* 689 * This particular cpu might have been holding an mbuf 690 * pending ARP resolution. If so, transmit the mbuf now. 691 */ 692 if (la->la_hold != NULL) { 693 m_adj(la->la_hold, sizeof(struct ether_header)); 694 ifp->if_output(ifp, la->la_hold, rt_key(rt), rt); 695 la->la_hold = NULL; 696 } 697 } 698 } 699 700 #ifdef SMP 701 702 struct netmsg_arp_update { 703 struct netmsg netmsg; 704 struct mbuf *m; 705 in_addr_t saddr; 706 boolean_t create; 707 }; 708 709 static void arp_update_msghandler(struct netmsg *); 710 711 #endif 712 713 /* 714 * Called from arpintr() - this routine is run from a single cpu. 715 */ 716 static void 717 in_arpinput(struct mbuf *m) 718 { 719 struct arphdr *ah; 720 struct ifnet *ifp = m->m_pkthdr.rcvif; 721 struct ether_header *eh; 722 struct rtentry *rt; 723 struct ifaddr_container *ifac; 724 struct in_ifaddr_container *iac; 725 struct in_ifaddr *ia; 726 struct sockaddr sa; 727 struct in_addr isaddr, itaddr, myaddr; 728 #ifdef SMP 729 struct netmsg_arp_update msg; 730 #endif 731 u_int8_t *enaddr = NULL; 732 int op; 733 int req_len; 734 735 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 736 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 737 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 738 return; 739 } 740 741 ah = mtod(m, struct arphdr *); 742 op = ntohs(ah->ar_op); 743 memcpy(&isaddr, ar_spa(ah), sizeof isaddr); 744 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr); 745 /* 746 * Check both target and sender IP addresses: 747 * 748 * If we receive the packet on the interface owning the address, 749 * then accept the address. 750 * 751 * For a bridge, we accept the address if the receive interface and 752 * the interface owning the address are on the same bridge. 753 * (This will change slightly when we have clusters of interfaces). 754 */ 755 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) { 756 ia = iac->ia; 757 758 /* Skip all ia's which don't match */ 759 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr) 760 continue; 761 762 if (ia->ia_ifp == ifp) 763 goto match; 764 765 if (ifp->if_bridge && ia->ia_ifp && 766 ifp->if_bridge == ia->ia_ifp->if_bridge) 767 goto match; 768 769 #ifdef CARP 770 /* 771 * If the interface does not match, but the recieving interface 772 * is part of carp, we call carp_iamatch to see if this is a 773 * request for the virtual host ip. 774 * XXX: This is really ugly! 775 */ 776 if (ifp->if_carp != NULL) { 777 get_mplock(); 778 if (ifp->if_carp != NULL && 779 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr)) { 780 rel_mplock(); 781 goto match; 782 } 783 rel_mplock(); 784 } 785 #endif 786 } 787 LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) { 788 ia = iac->ia; 789 790 /* Skip all ia's which don't match */ 791 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr) 792 continue; 793 794 if (ia->ia_ifp == ifp) 795 goto match; 796 797 if (ifp->if_bridge && ia->ia_ifp && 798 ifp->if_bridge == ia->ia_ifp->if_bridge) 799 goto match; 800 } 801 /* 802 * No match, use the first inet address on the receive interface 803 * as a dummy address for the rest of the function. 804 */ 805 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 806 struct ifaddr *ifa = ifac->ifa; 807 808 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 809 ia = ifatoia(ifa); 810 goto match; 811 } 812 } 813 /* 814 * If we got here, we didn't find any suitable interface, 815 * so drop the packet. 816 */ 817 m_freem(m); 818 return; 819 820 match: 821 if (!enaddr) 822 enaddr = (u_int8_t *)IF_LLADDR(ifp); 823 myaddr = ia->ia_addr.sin_addr; 824 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) { 825 m_freem(m); /* it's from me, ignore it. */ 826 return; 827 } 828 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 829 log(LOG_ERR, 830 "arp: link address is broadcast for IP address %s!\n", 831 inet_ntoa(isaddr)); 832 m_freem(m); 833 return; 834 } 835 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { 836 log(LOG_ERR, 837 "arp: %*D is using my IP address %s!\n", 838 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 839 inet_ntoa(isaddr)); 840 itaddr = myaddr; 841 goto reply; 842 } 843 #ifdef SMP 844 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0, 845 arp_update_msghandler); 846 msg.m = m; 847 msg.saddr = isaddr.s_addr; 848 msg.create = (itaddr.s_addr == myaddr.s_addr); 849 lwkt_domsg(rtable_portfn(0), &msg.netmsg.nm_lmsg, 0); 850 #else 851 arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr), 852 TRUE); 853 #endif 854 reply: 855 if (op != ARPOP_REQUEST) { 856 m_freem(m); 857 return; 858 } 859 if (itaddr.s_addr == myaddr.s_addr) { 860 /* I am the target */ 861 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 862 memcpy(ar_sha(ah), enaddr, ah->ar_hln); 863 } else { 864 struct llinfo_arp *la; 865 866 la = arplookup(itaddr.s_addr, FALSE, SIN_PROXY); 867 if (la == NULL) { 868 struct sockaddr_in sin; 869 870 if (!arp_proxyall) { 871 m_freem(m); 872 return; 873 } 874 875 bzero(&sin, sizeof sin); 876 sin.sin_family = AF_INET; 877 sin.sin_len = sizeof sin; 878 sin.sin_addr = itaddr; 879 880 rt = rtpurelookup((struct sockaddr *)&sin); 881 if (rt == NULL) { 882 m_freem(m); 883 return; 884 } 885 --rt->rt_refcnt; 886 /* 887 * Don't send proxies for nodes on the same interface 888 * as this one came out of, or we'll get into a fight 889 * over who claims what Ether address. 890 */ 891 if (rt->rt_ifp == ifp) { 892 m_freem(m); 893 return; 894 } 895 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 896 memcpy(ar_sha(ah), enaddr, ah->ar_hln); 897 #ifdef DEBUG_PROXY 898 kprintf("arp: proxying for %s\n", inet_ntoa(itaddr)); 899 #endif 900 } else { 901 struct sockaddr_dl *sdl; 902 903 rt = la->la_rt; 904 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 905 sdl = SDL(rt->rt_gateway); 906 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 907 } 908 } 909 910 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 911 memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 912 ah->ar_op = htons(ARPOP_REPLY); 913 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 914 switch (ifp->if_type) { 915 case IFT_ETHER: 916 /* 917 * May not be correct for types not explictly 918 * listed, but it is our best guess. 919 */ 920 default: 921 eh = (struct ether_header *)sa.sa_data; 922 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost); 923 eh->ether_type = htons(ETHERTYPE_ARP); 924 break; 925 } 926 sa.sa_family = AF_UNSPEC; 927 sa.sa_len = sizeof sa; 928 ifp->if_output(ifp, m, &sa, NULL); 929 } 930 931 #ifdef SMP 932 933 static void 934 arp_update_msghandler(struct netmsg *netmsg) 935 { 936 struct netmsg_arp_update *msg = (struct netmsg_arp_update *)netmsg; 937 int nextcpu; 938 939 arp_update_oncpu(msg->m, msg->saddr, msg->create, mycpuid == 0); 940 941 nextcpu = mycpuid + 1; 942 if (nextcpu < ncpus) 943 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->netmsg.nm_lmsg); 944 else 945 lwkt_replymsg(&msg->netmsg.nm_lmsg, 0); 946 } 947 948 #endif /* SMP */ 949 950 #endif /* INET */ 951 952 /* 953 * Free an arp entry. If the arp entry is actively referenced or represents 954 * a static entry we only clear it back to an unresolved state, otherwise 955 * we destroy the entry entirely. 956 * 957 * Note that static entries are created when route add ... -interface is used 958 * to create an interface route to a (direct) destination. 959 */ 960 static void 961 arptfree(struct llinfo_arp *la) 962 { 963 struct rtentry *rt = la->la_rt; 964 struct sockaddr_dl *sdl; 965 966 if (rt == NULL) 967 panic("arptfree"); 968 sdl = SDL(rt->rt_gateway); 969 if (sdl != NULL && 970 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) || 971 (rt->rt_flags & RTF_STATIC))) { 972 sdl->sdl_alen = 0; 973 la->la_preempt = la->la_asked = 0; 974 rt->rt_flags &= ~RTF_REJECT; 975 return; 976 } 977 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL); 978 } 979 980 /* 981 * Lookup or enter a new address in arptab. 982 */ 983 static struct llinfo_arp * 984 arplookup(in_addr_t addr, boolean_t create, boolean_t proxy) 985 { 986 struct rtentry *rt; 987 struct sockaddr_inarp sin = { sizeof sin, AF_INET }; 988 const char *why = NULL; 989 990 sin.sin_addr.s_addr = addr; 991 sin.sin_other = proxy ? SIN_PROXY : 0; 992 if (create) 993 rt = rtlookup((struct sockaddr *)&sin); 994 else 995 rt = rtpurelookup((struct sockaddr *)&sin); 996 if (rt == NULL) 997 return (NULL); 998 rt->rt_refcnt--; 999 1000 if (rt->rt_flags & RTF_GATEWAY) 1001 why = "host is not on local network"; 1002 else if (!(rt->rt_flags & RTF_LLINFO)) 1003 why = "could not allocate llinfo"; 1004 else if (rt->rt_gateway->sa_family != AF_LINK) 1005 why = "gateway route is not ours"; 1006 1007 if (why) { 1008 if (create) { 1009 log(LOG_DEBUG, "arplookup %s failed: %s\n", 1010 inet_ntoa(sin.sin_addr), why); 1011 } 1012 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) { 1013 /* No references to this route. Purge it. */ 1014 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 1015 rt_mask(rt), rt->rt_flags, NULL); 1016 } 1017 return (NULL); 1018 } 1019 return (rt->rt_llinfo); 1020 } 1021 1022 void 1023 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 1024 { 1025 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) { 1026 arprequest_async(ifp, &IA_SIN(ifa)->sin_addr, 1027 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 1028 } 1029 ifa->ifa_rtrequest = arp_rtrequest; 1030 ifa->ifa_flags |= RTF_CLONING; 1031 } 1032 1033 void 1034 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr) 1035 { 1036 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) { 1037 arprequest_async(ifp, &IA_SIN(ifa)->sin_addr, 1038 &IA_SIN(ifa)->sin_addr, enaddr); 1039 } 1040 ifa->ifa_rtrequest = arp_rtrequest; 1041 ifa->ifa_flags |= RTF_CLONING; 1042 } 1043 1044 static void 1045 arp_init(void) 1046 { 1047 uint32_t flags; 1048 int cpu; 1049 1050 for (cpu = 0; cpu < ncpus2; cpu++) 1051 LIST_INIT(&llinfo_arp_list[cpu]); 1052 1053 if (arp_mpsafe) { 1054 flags = NETISR_FLAG_MPSAFE; 1055 kprintf("arp: MPSAFE\n"); 1056 } else { 1057 flags = NETISR_FLAG_NOTMPSAFE; 1058 } 1059 netisr_register(NETISR_ARP, cpu0_portfn, arpintr, flags); 1060 } 1061 1062 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 1063