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 62 #include <netinet/in.h> 63 #include <netinet/in_var.h> 64 #include <netinet/if_ether.h> 65 66 #include <net/iso88025.h> 67 68 #define SIN(s) ((struct sockaddr_in *)s) 69 #define SDL(s) ((struct sockaddr_dl *)s) 70 71 SYSCTL_DECL(_net_link_ether); 72 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 73 74 /* timer values */ 75 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 76 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 77 static int arpt_down = 20; /* once declared down, don't send for 20 sec */ 78 79 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, 80 &arpt_prune, 0, ""); 81 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 82 &arpt_keep, 0, ""); 83 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, 84 &arpt_down, 0, ""); 85 86 #define rt_expire rt_rmx.rmx_expire 87 88 struct llinfo_arp { 89 LIST_ENTRY(llinfo_arp) la_le; 90 struct rtentry *la_rt; 91 struct mbuf *la_hold; /* last packet until resolved/timeout */ 92 long la_asked; /* last time we QUERIED for this addr */ 93 #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */ 94 }; 95 96 static LIST_HEAD(, llinfo_arp) llinfo_arp; 97 98 struct ifqueue arpintrq = {0, 0, 0, 50}; 99 static int arp_inuse, arp_allocated; 100 101 static int arp_maxtries = 5; 102 static int useloopback = 1; /* use loopback interface for local traffic */ 103 static int arp_proxyall = 0; 104 105 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 106 &arp_maxtries, 0, ""); 107 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 108 &useloopback, 0, ""); 109 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 110 &arp_proxyall, 0, ""); 111 112 static void arp_rtrequest __P((int, struct rtentry *, struct sockaddr *)); 113 static void arprequest __P((struct arpcom *, 114 struct in_addr *, struct in_addr *, u_char *)); 115 static void arpintr __P((void)); 116 static void arptfree __P((struct llinfo_arp *)); 117 static void arptimer __P((void *)); 118 static struct llinfo_arp 119 *arplookup __P((u_long, int, int)); 120 #ifdef INET 121 static void in_arpinput __P((struct mbuf *)); 122 #endif 123 124 /* 125 * Timeout routine. Age arp_tab entries periodically. 126 */ 127 /* ARGSUSED */ 128 static void 129 arptimer(ignored_arg) 130 void *ignored_arg; 131 { 132 int s = splnet(); 133 register struct llinfo_arp *la = llinfo_arp.lh_first; 134 struct llinfo_arp *ola; 135 136 timeout(arptimer, (caddr_t)0, arpt_prune * hz); 137 while ((ola = la) != 0) { 138 register struct rtentry *rt = la->la_rt; 139 la = la->la_le.le_next; 140 if (rt->rt_expire && rt->rt_expire <= time_second) 141 arptfree(ola); /* timer has expired, clear */ 142 } 143 splx(s); 144 } 145 146 /* 147 * Parallel to llc_rtrequest. 148 */ 149 static void 150 arp_rtrequest(req, rt, sa) 151 int req; 152 register struct rtentry *rt; 153 struct sockaddr *sa; 154 { 155 register struct sockaddr *gate = rt->rt_gateway; 156 register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 157 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 158 static int arpinit_done; 159 160 if (!arpinit_done) { 161 arpinit_done = 1; 162 LIST_INIT(&llinfo_arp); 163 timeout(arptimer, (caddr_t)0, hz); 164 } 165 if (rt->rt_flags & RTF_GATEWAY) 166 return; 167 switch (req) { 168 169 case RTM_ADD: 170 /* 171 * XXX: If this is a manually added route to interface 172 * such as older version of routed or gated might provide, 173 * restore cloning bit. 174 */ 175 if ((rt->rt_flags & RTF_HOST) == 0 && 176 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 177 rt->rt_flags |= RTF_CLONING; 178 if (rt->rt_flags & RTF_CLONING) { 179 /* 180 * Case 1: This route should come from a route to iface. 181 */ 182 rt_setgate(rt, rt_key(rt), 183 (struct sockaddr *)&null_sdl); 184 gate = rt->rt_gateway; 185 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 186 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 187 rt->rt_expire = time_second; 188 break; 189 } 190 /* Announce a new entry if requested. */ 191 if (rt->rt_flags & RTF_ANNOUNCE) 192 arprequest((struct arpcom *)rt->rt_ifp, 193 &SIN(rt_key(rt))->sin_addr, 194 &SIN(rt_key(rt))->sin_addr, 195 (u_char *)LLADDR(SDL(gate))); 196 /*FALLTHROUGH*/ 197 case RTM_RESOLVE: 198 if (gate->sa_family != AF_LINK || 199 gate->sa_len < sizeof(null_sdl)) { 200 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 201 break; 202 } 203 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 204 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 205 if (la != 0) 206 break; /* This happens on a route change */ 207 /* 208 * Case 2: This route may come from cloning, or a manual route 209 * add with a LL address. 210 */ 211 R_Malloc(la, struct llinfo_arp *, sizeof(*la)); 212 rt->rt_llinfo = (caddr_t)la; 213 if (la == 0) { 214 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 215 break; 216 } 217 arp_inuse++, arp_allocated++; 218 Bzero(la, sizeof(*la)); 219 la->la_rt = rt; 220 rt->rt_flags |= RTF_LLINFO; 221 LIST_INSERT_HEAD(&llinfo_arp, la, la_le); 222 223 #ifdef INET 224 /* 225 * This keeps the multicast addresses from showing up 226 * in `arp -a' listings as unresolved. It's not actually 227 * functional. Then the same for broadcast. 228 */ 229 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) { 230 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 231 LLADDR(SDL(gate))); 232 SDL(gate)->sdl_alen = 6; 233 rt->rt_expire = 0; 234 } 235 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 236 memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6); 237 SDL(gate)->sdl_alen = 6; 238 rt->rt_expire = 0; 239 } 240 #endif 241 242 if (SIN(rt_key(rt))->sin_addr.s_addr == 243 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 244 /* 245 * This test used to be 246 * if (loif.if_flags & IFF_UP) 247 * It allowed local traffic to be forced 248 * through the hardware by configuring the loopback down. 249 * However, it causes problems during network configuration 250 * for boards that can't receive packets they send. 251 * It is now necessary to clear "useloopback" and remove 252 * the route to force traffic out to the hardware. 253 */ 254 rt->rt_expire = 0; 255 Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr, 256 LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6); 257 if (useloopback) 258 rt->rt_ifp = loif; 259 260 } 261 break; 262 263 case RTM_DELETE: 264 if (la == 0) 265 break; 266 arp_inuse--; 267 LIST_REMOVE(la, la_le); 268 rt->rt_llinfo = 0; 269 rt->rt_flags &= ~RTF_LLINFO; 270 if (la->la_hold) 271 m_freem(la->la_hold); 272 Free((caddr_t)la); 273 } 274 } 275 276 /* 277 * Broadcast an ARP request. Caller specifies: 278 * - arp header source ip address 279 * - arp header target ip address 280 * - arp header source ethernet address 281 */ 282 static void 283 arprequest(ac, sip, tip, enaddr) 284 register struct arpcom *ac; 285 register struct in_addr *sip, *tip; 286 register u_char *enaddr; 287 { 288 register struct mbuf *m; 289 register struct ether_header *eh; 290 register struct ether_arp *ea; 291 struct sockaddr sa; 292 293 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 294 return; 295 m->m_pkthdr.rcvif = (struct ifnet *)0; 296 switch (ac->ac_if.if_type) { 297 case IFT_ISO88025: 298 m->m_len = sizeof(*ea) + 10; 299 m->m_pkthdr.len = sizeof(*ea) + 10; 300 MH_ALIGN(m, sizeof(*ea) + 10); 301 (void)memcpy(mtod(m, caddr_t), 302 "\x82\x40\xaa\xaa\x03\x00\x00\x00\x08\x06", 10); 303 (void)memcpy(sa.sa_data, etherbroadcastaddr, 6); 304 (void)memcpy(sa.sa_data + 6, enaddr, 6); 305 sa.sa_data[6] |= 0x80; 306 sa.sa_data[12] = 0x10; 307 sa.sa_data[13] = 0x40; 308 ea = (struct ether_arp *)(mtod(m, char *) + 10); 309 bzero((caddr_t)ea, sizeof (*ea)); 310 ea->arp_hrd = htons(ARPHRD_IEEE802); 311 break; 312 case IFT_FDDI: 313 case IFT_ETHER: 314 /* 315 * This may not be correct for types not explicitly 316 * listed, but this is our best guess 317 */ 318 default: 319 m->m_len = sizeof(*ea); 320 m->m_pkthdr.len = sizeof(*ea); 321 MH_ALIGN(m, sizeof(*ea)); 322 ea = mtod(m, struct ether_arp *); 323 eh = (struct ether_header *)sa.sa_data; 324 bzero((caddr_t)ea, sizeof (*ea)); 325 /* if_output will not swap */ 326 eh->ether_type = htons(ETHERTYPE_ARP); 327 (void)memcpy(eh->ether_dhost, etherbroadcastaddr, 328 sizeof(eh->ether_dhost)); 329 ea->arp_hrd = htons(ARPHRD_ETHER); 330 break; 331 } 332 ea->arp_pro = htons(ETHERTYPE_IP); 333 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 334 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 335 ea->arp_op = htons(ARPOP_REQUEST); 336 (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha)); 337 (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa)); 338 (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa)); 339 sa.sa_family = AF_UNSPEC; 340 sa.sa_len = sizeof(sa); 341 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 342 } 343 344 /* 345 * Resolve an IP address into an ethernet address. If success, 346 * desten is filled in. If there is no entry in arptab, 347 * set one up and broadcast a request for the IP address. 348 * Hold onto this mbuf and resend it once the address 349 * is finally resolved. A return value of 1 indicates 350 * that desten has been filled in and the packet should be sent 351 * normally; a 0 return indicates that the packet has been 352 * taken over here, either now or for later transmission. 353 */ 354 int 355 arpresolve(ac, rt, m, dst, desten, rt0) 356 register struct arpcom *ac; 357 register struct rtentry *rt; 358 struct mbuf *m; 359 register struct sockaddr *dst; 360 register u_char *desten; 361 struct rtentry *rt0; 362 { 363 register struct llinfo_arp *la = 0; 364 struct sockaddr_dl *sdl; 365 366 if (m->m_flags & M_BCAST) { /* broadcast */ 367 (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr)); 368 return (1); 369 } 370 if (m->m_flags & M_MCAST) { /* multicast */ 371 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 372 return(1); 373 } 374 if (rt) 375 la = (struct llinfo_arp *)rt->rt_llinfo; 376 if (la == 0) { 377 la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 378 if (la) 379 rt = la->la_rt; 380 } 381 if (la == 0 || rt == 0) { 382 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", 383 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "", 384 rt ? "rt" : ""); 385 m_freem(m); 386 return (0); 387 } 388 sdl = SDL(rt->rt_gateway); 389 /* 390 * Check the address family and length is valid, the address 391 * is resolved; otherwise, try to resolve. 392 */ 393 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && 394 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 395 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 396 return 1; 397 } 398 /* 399 * There is an arptab entry, but no ethernet address 400 * response yet. Replace the held mbuf with this 401 * latest one. 402 */ 403 if (la->la_hold) 404 m_freem(la->la_hold); 405 la->la_hold = m; 406 if (rt->rt_expire) { 407 rt->rt_flags &= ~RTF_REJECT; 408 if (la->la_asked == 0 || rt->rt_expire != time_second) { 409 rt->rt_expire = time_second; 410 if (la->la_asked++ < arp_maxtries) 411 arprequest(ac, 412 &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 413 &SIN(dst)->sin_addr, ac->ac_enaddr); 414 else { 415 rt->rt_flags |= RTF_REJECT; 416 rt->rt_expire += arpt_down; 417 la->la_asked = 0; 418 } 419 420 } 421 } 422 return (0); 423 } 424 425 /* 426 * Common length and type checks are done here, 427 * then the protocol-specific routine is called. 428 */ 429 static void 430 arpintr() 431 { 432 register struct mbuf *m; 433 register struct arphdr *ar; 434 int s; 435 436 while (arpintrq.ifq_head) { 437 s = splimp(); 438 IF_DEQUEUE(&arpintrq, m); 439 splx(s); 440 if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 441 panic("arpintr"); 442 if (m->m_len >= sizeof(struct arphdr) && 443 (ar = mtod(m, struct arphdr *)) && 444 (ntohs(ar->ar_hrd) == ARPHRD_ETHER || 445 ntohs(ar->ar_hrd) == ARPHRD_IEEE802) && 446 m->m_len >= 447 sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln) 448 449 switch (ntohs(ar->ar_pro)) { 450 451 #ifdef INET 452 case ETHERTYPE_IP: 453 in_arpinput(m); 454 continue; 455 #endif 456 } 457 m_freem(m); 458 } 459 } 460 461 NETISR_SET(NETISR_ARP, arpintr); 462 463 464 #ifdef INET 465 /* 466 * ARP for Internet protocols on 10 Mb/s Ethernet. 467 * Algorithm is that given in RFC 826. 468 * In addition, a sanity check is performed on the sender 469 * protocol address, to catch impersonators. 470 * We no longer handle negotiations for use of trailer protocol: 471 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 472 * along with IP replies if we wanted trailers sent to us, 473 * and also sent them in response to IP replies. 474 * This allowed either end to announce the desire to receive 475 * trailer packets. 476 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 477 * but formerly didn't normally send requests. 478 */ 479 static void 480 in_arpinput(m) 481 struct mbuf *m; 482 { 483 register struct ether_arp *ea; 484 register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; 485 struct ether_header *eh; 486 struct iso88025_header *th = (struct iso88025_header *)0; 487 register struct llinfo_arp *la = 0; 488 register struct rtentry *rt; 489 struct in_ifaddr *ia, *maybe_ia = 0; 490 struct sockaddr_dl *sdl; 491 struct sockaddr sa; 492 struct in_addr isaddr, itaddr, myaddr; 493 int op; 494 495 ea = mtod(m, struct ether_arp *); 496 op = ntohs(ea->arp_op); 497 (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr)); 498 (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr)); 499 for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next) 500 #ifdef BRIDGE 501 /* 502 * For a bridge, we want to check the address irrespective 503 * of the receive interface. (This will change slightly 504 * when we have clusters of interfaces). 505 */ 506 { 507 #else 508 if (ia->ia_ifp == &ac->ac_if) { 509 #endif 510 maybe_ia = ia; 511 if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || 512 (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) 513 break; 514 } 515 if (maybe_ia == 0) { 516 m_freem(m); 517 return; 518 } 519 myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; 520 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 521 sizeof (ea->arp_sha))) { 522 m_freem(m); /* it's from me, ignore it. */ 523 return; 524 } 525 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 526 sizeof (ea->arp_sha))) { 527 log(LOG_ERR, 528 "arp: ether address is broadcast for IP address %s!\n", 529 inet_ntoa(isaddr)); 530 m_freem(m); 531 return; 532 } 533 if (isaddr.s_addr == myaddr.s_addr) { 534 log(LOG_ERR, 535 "arp: %6D is using my IP address %s!\n", 536 ea->arp_sha, ":", inet_ntoa(isaddr)); 537 itaddr = myaddr; 538 goto reply; 539 } 540 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 541 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 542 #ifndef BRIDGE /* the following is not an error when doing bridging */ 543 if (rt->rt_ifp != &ac->ac_if) { 544 log(LOG_ERR, "arp: %s is on %s%d but got reply from %6D on %s%d\n", 545 inet_ntoa(isaddr), 546 rt->rt_ifp->if_name, rt->rt_ifp->if_unit, 547 ea->arp_sha, ":", 548 ac->ac_if.if_name, ac->ac_if.if_unit); 549 goto reply; 550 } 551 #endif 552 if (sdl->sdl_alen && 553 bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) { 554 if (rt->rt_expire) 555 log(LOG_INFO, "arp: %s moved from %6D to %6D on %s%d\n", 556 inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":", 557 ea->arp_sha, ":", 558 ac->ac_if.if_name, ac->ac_if.if_unit); 559 else { 560 log(LOG_ERR, 561 "arp: %6D attempts to modify permanent entry for %s on %s%d\n", 562 ea->arp_sha, ":", inet_ntoa(isaddr), 563 ac->ac_if.if_name, ac->ac_if.if_unit); 564 goto reply; 565 } 566 } 567 (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha)); 568 sdl->sdl_alen = sizeof(ea->arp_sha); 569 sdl->sdl_rcf = NULL; 570 /* 571 * If we receive an arp from a token-ring station over 572 * a token-ring nic then try to save the source 573 * routing info. 574 */ 575 if (ac->ac_if.if_type == IFT_ISO88025) { 576 th = (struct iso88025_header *)m->m_pkthdr.header; 577 if ((th->iso88025_shost[0] & 0x80) && 578 ((th->rcf & 0x001f) > 2)) { 579 sdl->sdl_rcf = (th->rcf & 0x8000) ? 580 (th->rcf & 0x7fff) : 581 (th->rcf | 0x8000); 582 memcpy(sdl->sdl_route, th->rseg, 583 (th->rcf & 0x001f) - 2); 584 sdl->sdl_rcf = sdl->sdl_rcf & 0xff1f; 585 /* 586 * Set up source routing information for 587 * reply packet (XXX) 588 */ 589 m->m_data -= (th->rcf & 0x001f); 590 m->m_len += (th->rcf & 0x001f); 591 m->m_pkthdr.len += (th->rcf & 0x001f); 592 } else { 593 th->iso88025_shost[0] &= 0x7f; 594 } 595 m->m_data -= 8; 596 m->m_len += 8; 597 m->m_pkthdr.len += 8; 598 th->rcf = sdl->sdl_rcf; 599 } else { 600 sdl->sdl_rcf = NULL; 601 } 602 if (rt->rt_expire) 603 rt->rt_expire = time_second + arpt_keep; 604 rt->rt_flags &= ~RTF_REJECT; 605 la->la_asked = 0; 606 if (la->la_hold) { 607 (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 608 rt_key(rt), rt); 609 la->la_hold = 0; 610 } 611 } 612 reply: 613 if (op != ARPOP_REQUEST) { 614 m_freem(m); 615 return; 616 } 617 if (itaddr.s_addr == myaddr.s_addr) { 618 /* I am the target */ 619 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 620 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 621 } else { 622 la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 623 if (la == NULL) { 624 struct sockaddr_in sin; 625 626 if (!arp_proxyall) { 627 m_freem(m); 628 return; 629 } 630 631 bzero(&sin, sizeof sin); 632 sin.sin_family = AF_INET; 633 sin.sin_len = sizeof sin; 634 sin.sin_addr = itaddr; 635 636 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 637 if (!rt) { 638 m_freem(m); 639 return; 640 } 641 /* 642 * Don't send proxies for nodes on the same interface 643 * as this one came out of, or we'll get into a fight 644 * over who claims what Ether address. 645 */ 646 if (rt->rt_ifp == &ac->ac_if) { 647 rtfree(rt); 648 m_freem(m); 649 return; 650 } 651 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 652 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 653 rtfree(rt); 654 #ifdef DEBUG_PROXY 655 printf("arp: proxying for %s\n", 656 inet_ntoa(itaddr)); 657 #endif 658 } else { 659 rt = la->la_rt; 660 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 661 sdl = SDL(rt->rt_gateway); 662 (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha)); 663 } 664 } 665 666 (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa)); 667 (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa)); 668 ea->arp_op = htons(ARPOP_REPLY); 669 ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 670 switch (ac->ac_if.if_type) { 671 case IFT_ISO88025: 672 /* Re-arrange the source/dest address */ 673 memcpy(th->iso88025_dhost, th->iso88025_shost, 674 sizeof(th->iso88025_dhost)); 675 memcpy(th->iso88025_shost, ac->ac_enaddr, 676 sizeof(th->iso88025_shost)); 677 /* Set the source routing bit if neccesary */ 678 if (th->iso88025_dhost[0] & 0x80) { 679 th->iso88025_dhost[0] &= 0x7f; 680 if ((th->rcf & 0x001f) - 2) 681 th->iso88025_shost[0] |= 0x80; 682 } 683 /* Copy the addresses, ac and fc into sa_data */ 684 memcpy(sa.sa_data, th->iso88025_dhost, 685 sizeof(th->iso88025_dhost) * 2); 686 sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = 0x10; 687 sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = 0x40; 688 break; 689 case IFT_ETHER: 690 case IFT_FDDI: 691 /* 692 * May not be correct for types not explictly 693 * listed, but it is our best guess. 694 */ 695 default: 696 eh = (struct ether_header *)sa.sa_data; 697 (void)memcpy(eh->ether_dhost, ea->arp_tha, 698 sizeof(eh->ether_dhost)); 699 eh->ether_type = htons(ETHERTYPE_ARP); 700 break; 701 } 702 sa.sa_family = AF_UNSPEC; 703 sa.sa_len = sizeof(sa); 704 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 705 return; 706 } 707 #endif 708 709 /* 710 * Free an arp entry. 711 */ 712 static void 713 arptfree(la) 714 register struct llinfo_arp *la; 715 { 716 register struct rtentry *rt = la->la_rt; 717 register struct sockaddr_dl *sdl; 718 if (rt == 0) 719 panic("arptfree"); 720 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 721 sdl->sdl_family == AF_LINK) { 722 sdl->sdl_alen = 0; 723 la->la_asked = 0; 724 rt->rt_flags &= ~RTF_REJECT; 725 return; 726 } 727 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 728 0, (struct rtentry **)0); 729 } 730 /* 731 * Lookup or enter a new address in arptab. 732 */ 733 static struct llinfo_arp * 734 arplookup(addr, create, proxy) 735 u_long addr; 736 int create, proxy; 737 { 738 register struct rtentry *rt; 739 static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; 740 const char *why = 0; 741 742 sin.sin_addr.s_addr = addr; 743 sin.sin_other = proxy ? SIN_PROXY : 0; 744 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 745 if (rt == 0) 746 return (0); 747 rt->rt_refcnt--; 748 749 if (rt->rt_flags & RTF_GATEWAY) 750 why = "host is not on local network"; 751 else if ((rt->rt_flags & RTF_LLINFO) == 0) 752 why = "could not allocate llinfo"; 753 else if (rt->rt_gateway->sa_family != AF_LINK) 754 why = "gateway route is not ours"; 755 756 if (why && create) { 757 log(LOG_DEBUG, "arplookup %s failed: %s\n", 758 inet_ntoa(sin.sin_addr), why); 759 return 0; 760 } else if (why) { 761 return 0; 762 } 763 return ((struct llinfo_arp *)rt->rt_llinfo); 764 } 765 766 void 767 arp_ifinit(ac, ifa) 768 struct arpcom *ac; 769 struct ifaddr *ifa; 770 { 771 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 772 arprequest(ac, &IA_SIN(ifa)->sin_addr, 773 &IA_SIN(ifa)->sin_addr, ac->ac_enaddr); 774 ifa->ifa_rtrequest = arp_rtrequest; 775 ifa->ifa_flags |= RTF_CLONING; 776 } 777