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