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