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