1 /* $OpenBSD: if_ether.c,v 1.45 2002/06/09 16:26:10 itojun Exp $ */ 2 /* $NetBSD: if_ether.c,v 1.31 1996/05/11 12:59:58 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 37 */ 38 39 /* 40 * Ethernet address resolution protocol. 41 * TODO: 42 * add "inuse/lock" bit (or ref. count) along with valid bit 43 */ 44 45 #ifdef INET 46 47 #include "bridge.h" 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/mbuf.h> 52 #include <sys/socket.h> 53 #include <sys/kernel.h> 54 #include <sys/syslog.h> 55 #include <sys/proc.h> 56 57 #include <net/if.h> 58 #include <net/if_dl.h> 59 #include <net/route.h> 60 #include <net/if_fddi.h> 61 #include <net/if_types.h> 62 63 #include <netinet/in.h> 64 #include <netinet/in_var.h> 65 #include <netinet/if_ether.h> 66 67 #define SIN(s) ((struct sockaddr_in *)s) 68 #define SDL(s) ((struct sockaddr_dl *)s) 69 #define SRP(s) ((struct sockaddr_inarp *)s) 70 71 /* 72 * ARP trailer negotiation. Trailer protocol is not IP specific, 73 * but ARP request/response use IP addresses. 74 */ 75 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL 76 77 /* timer values */ 78 int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 79 int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 80 int arpt_down = 20; /* once declared down, don't send for 20 secs */ 81 #define rt_expire rt_rmx.rmx_expire 82 83 void arptfree(struct llinfo_arp *); 84 void arptimer(void *); 85 struct llinfo_arp *arplookup(u_int32_t, int, int); 86 void in_arpinput(struct mbuf *); 87 88 LIST_HEAD(, llinfo_arp) llinfo_arp; 89 struct ifqueue arpintrq = {0, 0, 0, 50}; 90 int arp_inuse, arp_allocated, arp_intimer; 91 int arp_maxtries = 5; 92 int useloopback = 1; /* use loopback interface for local traffic */ 93 int arpinit_done = 0; 94 95 /* revarp state */ 96 static struct in_addr myip, srv_ip; 97 static int myip_initialized = 0; 98 static int revarp_in_progress = 0; 99 struct ifnet *myip_ifp = NULL; 100 101 #ifdef DDB 102 #include <uvm/uvm_extern.h> 103 104 void db_print_sa(struct sockaddr *); 105 void db_print_ifa(struct ifaddr *); 106 void db_print_llinfo(caddr_t); 107 int db_show_radix_node(struct radix_node *, void *); 108 #endif 109 110 /* 111 * Timeout routine. Age arp_tab entries periodically. 112 */ 113 /* ARGSUSED */ 114 void 115 arptimer(arg) 116 void *arg; 117 { 118 struct timeout *to = (struct timeout *)arg; 119 int s; 120 struct llinfo_arp *la, *nla; 121 122 s = splsoftnet(); 123 timeout_add(to, arpt_prune * hz); 124 for (la = LIST_FIRST(&llinfo_arp); la != LIST_END(&llinfo_arp); 125 la = nla) { 126 register struct rtentry *rt = la->la_rt; 127 128 nla = LIST_NEXT(la, la_list); 129 if (rt->rt_expire && rt->rt_expire <= time.tv_sec) 130 arptfree(la); /* timer has expired; clear */ 131 } 132 splx(s); 133 } 134 135 /* 136 * Parallel to llc_rtrequest. 137 */ 138 void 139 arp_rtrequest(req, rt, info) 140 int req; 141 register struct rtentry *rt; 142 struct rt_addrinfo *info; 143 { 144 register struct sockaddr *gate = rt->rt_gateway; 145 register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 146 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 147 148 if (!arpinit_done) { 149 static struct timeout arptimer_to; 150 151 arpinit_done = 1; 152 /* 153 * We generate expiration times from time.tv_sec 154 * so avoid accidently creating permanent routes. 155 */ 156 if (time.tv_sec == 0) { 157 time.tv_sec++; 158 } 159 160 timeout_set(&arptimer_to, arptimer, &arptimer_to); 161 timeout_add(&arptimer_to, hz); 162 } 163 164 if (rt->rt_flags & RTF_GATEWAY) { 165 if (req != RTM_ADD) 166 return; 167 168 /* 169 * linklayers with particular link MTU limitation. it is a bit 170 * awkward to have FDDI handling here, we should split ARP from 171 * netinet/if_ether.c like NetBSD does. 172 */ 173 switch (rt->rt_ifp->if_type) { 174 case IFT_FDDI: 175 if (rt->rt_ifp->if_mtu > FDDIIPMTU) 176 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 177 break; 178 } 179 180 return; 181 } 182 183 switch (req) { 184 185 case RTM_ADD: 186 /* 187 * XXX: If this is a manually added route to interface 188 * such as older version of routed or gated might provide, 189 * restore cloning bit. 190 */ 191 if ((rt->rt_flags & RTF_HOST) == 0 && 192 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 193 rt->rt_flags |= RTF_CLONING; 194 if (rt->rt_flags & RTF_CLONING) { 195 /* 196 * Case 1: This route should come from a route to iface. 197 */ 198 rt_setgate(rt, rt_key(rt), 199 (struct sockaddr *)&null_sdl); 200 gate = rt->rt_gateway; 201 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 202 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 203 /* 204 * Give this route an expiration time, even though 205 * it's a "permanent" route, so that routes cloned 206 * from it do not need their expiration time set. 207 */ 208 rt->rt_expire = time.tv_sec; 209 /* 210 * linklayers with particular link MTU limitation. 211 */ 212 switch (rt->rt_ifp->if_type) { 213 case IFT_FDDI: 214 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 && 215 (rt->rt_rmx.rmx_mtu > FDDIIPMTU || 216 (rt->rt_rmx.rmx_mtu == 0 && 217 rt->rt_ifp->if_mtu > FDDIIPMTU))) 218 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 219 break; 220 } 221 break; 222 } 223 /* Announce a new entry if requested. */ 224 if (rt->rt_flags & RTF_ANNOUNCE) 225 arprequest(rt->rt_ifp, 226 &SIN(rt_key(rt))->sin_addr.s_addr, 227 &SIN(rt_key(rt))->sin_addr.s_addr, 228 (u_char *)LLADDR(SDL(gate))); 229 /*FALLTHROUGH*/ 230 case RTM_RESOLVE: 231 if (gate->sa_family != AF_LINK || 232 gate->sa_len < sizeof(null_sdl)) { 233 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 234 break; 235 } 236 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 237 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 238 if (la != 0) 239 break; /* This happens on a route change */ 240 /* 241 * Case 2: This route may come from cloning, or a manual route 242 * add with a LL address. 243 */ 244 R_Malloc(la, struct llinfo_arp *, sizeof(*la)); 245 rt->rt_llinfo = (caddr_t)la; 246 if (la == 0) { 247 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 248 break; 249 } 250 arp_inuse++, arp_allocated++; 251 Bzero(la, sizeof(*la)); 252 la->la_rt = rt; 253 rt->rt_flags |= RTF_LLINFO; 254 LIST_INSERT_HEAD(&llinfo_arp, la, la_list); 255 if (SIN(rt_key(rt))->sin_addr.s_addr == 256 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 257 /* 258 * This test used to be 259 * if (lo0ifp->if_flags & IFF_UP) 260 * It allowed local traffic to be forced through 261 * the hardware by configuring the loopback down. 262 * However, it causes problems during network 263 * configuration for boards that can't receive 264 * packets they send. It is now necessary to clear 265 * "useloopback" and remove the route to force 266 * traffic out to the hardware. 267 */ 268 rt->rt_expire = 0; 269 Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr, 270 LLADDR(SDL(gate)), 271 SDL(gate)->sdl_alen = ETHER_ADDR_LEN); 272 if (useloopback) 273 rt->rt_ifp = lo0ifp; 274 } 275 break; 276 277 case RTM_DELETE: 278 if (la == 0) 279 break; 280 arp_inuse--; 281 LIST_REMOVE(la, la_list); 282 rt->rt_llinfo = 0; 283 rt->rt_flags &= ~RTF_LLINFO; 284 if (la->la_hold) 285 m_freem(la->la_hold); 286 Free((caddr_t)la); 287 } 288 } 289 290 /* 291 * Broadcast an ARP request. Caller specifies: 292 * - arp header source ip address 293 * - arp header target ip address 294 * - arp header source ethernet address 295 */ 296 void 297 arprequest(ifp, sip, tip, enaddr) 298 register struct ifnet *ifp; 299 register u_int32_t *sip, *tip; 300 register u_int8_t *enaddr; 301 { 302 register struct mbuf *m; 303 register struct ether_header *eh; 304 register struct ether_arp *ea; 305 struct sockaddr sa; 306 307 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 308 return; 309 m->m_len = sizeof(*ea); 310 m->m_pkthdr.len = sizeof(*ea); 311 MH_ALIGN(m, sizeof(*ea)); 312 ea = mtod(m, struct ether_arp *); 313 eh = (struct ether_header *)sa.sa_data; 314 bzero((caddr_t)ea, sizeof (*ea)); 315 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 316 sizeof(eh->ether_dhost)); 317 eh->ether_type = htons(ETHERTYPE_ARP); /* if_output will not swap */ 318 ea->arp_hrd = htons(ARPHRD_ETHER); 319 ea->arp_pro = htons(ETHERTYPE_IP); 320 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 321 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 322 ea->arp_op = htons(ARPOP_REQUEST); 323 bcopy((caddr_t)enaddr, (caddr_t)eh->ether_shost, 324 sizeof(eh->ether_shost)); 325 bcopy((caddr_t)enaddr, (caddr_t)ea->arp_sha, sizeof(ea->arp_sha)); 326 bcopy((caddr_t)sip, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa)); 327 bcopy((caddr_t)tip, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa)); 328 sa.sa_family = AF_UNSPEC; 329 sa.sa_len = sizeof(sa); 330 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 331 } 332 333 /* 334 * Resolve an IP address into an ethernet address. If success, 335 * desten is filled in. If there is no entry in arptab, 336 * set one up and broadcast a request for the IP address. 337 * Hold onto this mbuf and resend it once the address 338 * is finally resolved. A return value of 1 indicates 339 * that desten has been filled in and the packet should be sent 340 * normally; a 0 return indicates that the packet has been 341 * taken over here, either now or for later transmission. 342 */ 343 int 344 arpresolve(ac, rt, m, dst, desten) 345 register struct arpcom *ac; 346 register struct rtentry *rt; 347 struct mbuf *m; 348 register struct sockaddr *dst; 349 register u_char *desten; 350 { 351 register struct llinfo_arp *la; 352 struct sockaddr_dl *sdl; 353 354 if (m->m_flags & M_BCAST) { /* broadcast */ 355 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten, 356 sizeof(etherbroadcastaddr)); 357 return (1); 358 } 359 if (m->m_flags & M_MCAST) { /* multicast */ 360 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 361 return (1); 362 } 363 if (rt) 364 la = (struct llinfo_arp *)rt->rt_llinfo; 365 else { 366 if ((la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0)) != NULL) 367 rt = la->la_rt; 368 } 369 if (la == 0 || rt == 0) { 370 log(LOG_DEBUG, "arpresolve: can't allocate llinfo\n"); 371 m_freem(m); 372 return (0); 373 } 374 sdl = SDL(rt->rt_gateway); 375 /* 376 * Check the address family and length is valid, the address 377 * is resolved; otherwise, try to resolve. 378 */ 379 if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) && 380 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 381 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 382 return 1; 383 } 384 if (((struct ifnet *)ac)->if_flags & IFF_NOARP) 385 return 0; 386 387 /* 388 * There is an arptab entry, but no ethernet address 389 * response yet. Replace the held mbuf with this 390 * latest one. 391 */ 392 if (la->la_hold) 393 m_freem(la->la_hold); 394 la->la_hold = m; 395 /* 396 * Re-send the ARP request when appropriate. 397 */ 398 #ifdef DIAGNOSTIC 399 if (rt->rt_expire == 0) { 400 /* This should never happen. (Should it? -gwr) */ 401 printf("arpresolve: unresolved and rt_expire == 0\n"); 402 /* Set expiration time to now (expired). */ 403 rt->rt_expire = time.tv_sec; 404 } 405 #endif 406 if (rt->rt_expire) { 407 rt->rt_flags &= ~RTF_REJECT; 408 if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) { 409 rt->rt_expire = time.tv_sec; 410 if (la->la_asked++ < arp_maxtries) 411 arprequest(&ac->ac_if, 412 &(SIN(rt->rt_ifa->ifa_addr)->sin_addr.s_addr), 413 &(SIN(dst)->sin_addr.s_addr), 414 ac->ac_enaddr); 415 else { 416 rt->rt_flags |= RTF_REJECT; 417 rt->rt_expire += arpt_down; 418 la->la_asked = 0; 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 void 430 arpintr() 431 { 432 register struct mbuf *m; 433 register struct arphdr *ar; 434 int s, len; 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 443 len = sizeof(struct arphdr); 444 if (m->m_len < len && (m = m_pullup(m, len)) == NULL) 445 continue; 446 447 ar = mtod(m, struct arphdr *); 448 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) 449 continue; 450 451 len += 2 * (ar->ar_hln + ar->ar_pln); 452 if (m->m_len < len && (m = m_pullup(m, len)) == NULL) 453 continue; 454 455 switch (ntohs(ar->ar_pro)) { 456 case ETHERTYPE_IP: 457 case ETHERTYPE_IPTRAILERS: 458 in_arpinput(m); 459 continue; 460 } 461 m_freem(m); 462 } 463 } 464 465 /* 466 * ARP for Internet protocols on 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 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 register struct llinfo_arp *la = 0; 487 register struct rtentry *rt; 488 struct in_ifaddr *ia; 489 #if NBRIDGE > 0 490 struct in_ifaddr *bridge_ia = NULL; 491 #endif 492 struct sockaddr_dl *sdl; 493 struct sockaddr sa; 494 struct in_addr isaddr, itaddr, myaddr; 495 int op; 496 497 ea = mtod(m, struct ether_arp *); 498 op = ntohs(ea->arp_op); 499 if ((op != ARPOP_REQUEST) && (op != ARPOP_REPLY)) 500 goto out; 501 #if notyet 502 if ((op == ARPOP_REPLY) && (m->m_flags & (M_BCAST|M_MCAST))) { 503 log(LOG_ERR, 504 "arp: received reply to broadcast or multicast address\n"); 505 goto out; 506 } 507 #endif 508 509 bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof(itaddr)); 510 bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof(isaddr)); 511 512 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 513 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr) 514 continue; 515 516 if (ia->ia_ifp == m->m_pkthdr.rcvif) 517 break; 518 #if NBRIDGE > 0 519 /* 520 * If the interface we received the packet on 521 * is part of a bridge, check to see if we need 522 * to "bridge" the packet to ourselves at this 523 * layer. Note we still prefer a perfect match, 524 * but allow this weaker match if necessary. 525 */ 526 if (m->m_pkthdr.rcvif->if_bridge != NULL && 527 m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge) 528 bridge_ia = ia; 529 #endif 530 } 531 532 #if NBRIDGE > 0 533 if (ia == NULL && bridge_ia != NULL) { 534 ia = bridge_ia; 535 ac = (struct arpcom *)bridge_ia->ia_ifp; 536 } 537 #endif 538 539 if (ia == NULL) { 540 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 541 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr) 542 continue; 543 if (ia->ia_ifp == m->m_pkthdr.rcvif) 544 break; 545 } 546 } 547 548 if (ia == NULL) { 549 struct ifaddr *ifa; 550 551 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) { 552 if (ifa->ifa_addr->sa_family == AF_INET) 553 break; 554 } 555 if (ifa) 556 ia = (struct in_ifaddr *)ifa; 557 } 558 559 if (ia == NULL) 560 goto out; 561 562 myaddr = ia->ia_addr.sin_addr; 563 564 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 565 sizeof (ea->arp_sha))) 566 goto out; /* it's from me, ignore it. */ 567 if (ETHER_IS_MULTICAST (&ea->arp_sha[0])) { 568 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 569 sizeof (ea->arp_sha))) 570 log(LOG_ERR, 571 "arp: ether address is broadcast for IP address %s!\n", 572 inet_ntoa(isaddr)); 573 else 574 log(LOG_ERR, 575 "arp: ether address is multicast for IP address %s!\n", 576 inet_ntoa(isaddr)); 577 goto out; 578 } 579 if (myaddr.s_addr && isaddr.s_addr == myaddr.s_addr) { 580 log(LOG_ERR, 581 "duplicate IP address %s sent from ethernet address %s\n", 582 inet_ntoa(isaddr), ether_sprintf(ea->arp_sha)); 583 itaddr = myaddr; 584 goto reply; 585 } 586 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 587 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 588 if (sdl->sdl_alen) { 589 if (bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) { 590 if (rt->rt_flags & RTF_PERMANENT_ARP) { 591 log(LOG_WARNING, 592 "arp: attempt to overwrite permanent " 593 "entry for %s by %s on %s\n", 594 inet_ntoa(isaddr), 595 ether_sprintf(ea->arp_sha), 596 ac->ac_if.if_xname); 597 goto out; 598 } else if (rt->rt_ifp != &ac->ac_if) { 599 log(LOG_WARNING, 600 "arp: attempt to overwrite entry for %s " 601 "on %s by %s on %s\n", 602 inet_ntoa(isaddr), rt->rt_ifp->if_xname, 603 ether_sprintf(ea->arp_sha), 604 ac->ac_if.if_xname); 605 goto out; 606 } else { 607 log(LOG_INFO, 608 "arp info overwritten for %s by %s on %s\n", 609 inet_ntoa(isaddr), 610 ether_sprintf(ea->arp_sha), 611 ac->ac_if.if_xname); 612 rt->rt_expire = 1; /* no longer static */ 613 } 614 } 615 } else if (rt->rt_ifp != &ac->ac_if && !(ac->ac_if.if_bridge && 616 (rt->rt_ifp->if_bridge == ac->ac_if.if_bridge))) { 617 log(LOG_WARNING, 618 "arp: attempt to add entry for %s " 619 "on %s by %s on %s\n", 620 inet_ntoa(isaddr), rt->rt_ifp->if_xname, 621 ether_sprintf(ea->arp_sha), 622 ac->ac_if.if_xname); 623 goto out; 624 } 625 bcopy((caddr_t)ea->arp_sha, LLADDR(sdl), 626 sdl->sdl_alen = sizeof(ea->arp_sha)); 627 if (rt->rt_expire) 628 rt->rt_expire = time.tv_sec + arpt_keep; 629 rt->rt_flags &= ~RTF_REJECT; 630 la->la_asked = 0; 631 if (la->la_hold) { 632 (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 633 rt_key(rt), rt); 634 la->la_hold = 0; 635 } 636 } 637 reply: 638 if (op != ARPOP_REQUEST) { 639 out: 640 m_freem(m); 641 return; 642 } 643 if (itaddr.s_addr == myaddr.s_addr) { 644 /* I am the target */ 645 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 646 sizeof(ea->arp_sha)); 647 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, 648 sizeof(ea->arp_sha)); 649 } else { 650 la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 651 if (la == 0) 652 goto out; 653 rt = la->la_rt; 654 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 655 sizeof(ea->arp_sha)); 656 sdl = SDL(rt->rt_gateway); 657 bcopy(LLADDR(sdl), (caddr_t)ea->arp_sha, sizeof(ea->arp_sha)); 658 } 659 660 bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, sizeof(ea->arp_spa)); 661 bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa)); 662 ea->arp_op = htons(ARPOP_REPLY); 663 ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 664 eh = (struct ether_header *)sa.sa_data; 665 bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost, 666 sizeof(eh->ether_dhost)); 667 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 668 sizeof(eh->ether_shost)); 669 eh->ether_type = htons(ETHERTYPE_ARP); 670 sa.sa_family = AF_UNSPEC; 671 sa.sa_len = sizeof(sa); 672 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 673 return; 674 } 675 676 /* 677 * Free an arp entry. 678 */ 679 void 680 arptfree(la) 681 register struct llinfo_arp *la; 682 { 683 register struct rtentry *rt = la->la_rt; 684 register struct sockaddr_dl *sdl; 685 686 if (rt == 0) 687 panic("arptfree"); 688 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 689 sdl->sdl_family == AF_LINK) { 690 sdl->sdl_alen = 0; 691 la->la_asked = 0; 692 rt->rt_flags &= ~RTF_REJECT; 693 return; 694 } 695 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 696 0, (struct rtentry **)0); 697 } 698 699 /* 700 * Lookup or enter a new address in arptab. 701 */ 702 struct llinfo_arp * 703 arplookup(addr, create, proxy) 704 u_int32_t addr; 705 int create, proxy; 706 { 707 register struct rtentry *rt; 708 static struct sockaddr_inarp sin; 709 710 sin.sin_len = sizeof(sin); 711 sin.sin_family = AF_INET; 712 sin.sin_addr.s_addr = addr; 713 sin.sin_other = proxy ? SIN_PROXY : 0; 714 rt = rtalloc1(sintosa(&sin), create); 715 if (rt == 0) 716 return (0); 717 rt->rt_refcnt--; 718 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 || 719 rt->rt_gateway->sa_family != AF_LINK) { 720 if (create) 721 log(LOG_DEBUG, 722 "arplookup: unable to enter address for %s\n", 723 inet_ntoa(sin.sin_addr)); 724 return (0); 725 } 726 return ((struct llinfo_arp *)rt->rt_llinfo); 727 } 728 729 int 730 arpioctl(cmd, data) 731 u_long cmd; 732 caddr_t data; 733 { 734 735 return (EOPNOTSUPP); 736 } 737 738 void 739 arp_ifinit(ac, ifa) 740 struct arpcom *ac; 741 struct ifaddr *ifa; 742 { 743 744 /* Warn the user if another station has this IP address. */ 745 arprequest(&ac->ac_if, 746 &(IA_SIN(ifa)->sin_addr.s_addr), 747 &(IA_SIN(ifa)->sin_addr.s_addr), 748 ac->ac_enaddr); 749 ifa->ifa_rtrequest = arp_rtrequest; 750 ifa->ifa_flags |= RTF_CLONING; 751 } 752 753 /* 754 * Called from Ethernet interrupt handlers 755 * when ether packet type ETHERTYPE_REVARP 756 * is received. Common length and type checks are done here, 757 * then the protocol-specific routine is called. 758 */ 759 void 760 revarpinput(m) 761 struct mbuf *m; 762 { 763 struct arphdr *ar; 764 765 if (m->m_len < sizeof(struct arphdr)) 766 goto out; 767 ar = mtod(m, struct arphdr *); 768 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) 769 goto out; 770 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln)) 771 goto out; 772 switch (ntohs(ar->ar_pro)) { 773 774 case ETHERTYPE_IP: 775 case ETHERTYPE_IPTRAILERS: 776 in_revarpinput(m); 777 return; 778 779 default: 780 break; 781 } 782 out: 783 m_freem(m); 784 } 785 786 /* 787 * RARP for Internet protocols on Ethernet. 788 * Algorithm is that given in RFC 903. 789 * We are only using for bootstrap purposes to get an ip address for one of 790 * our interfaces. Thus we support no user-interface. 791 * 792 * Since the contents of the RARP reply are specific to the interface that 793 * sent the request, this code must ensure that they are properly associated. 794 * 795 * Note: also supports ARP via RARP packets, per the RFC. 796 */ 797 void 798 in_revarpinput(m) 799 struct mbuf *m; 800 { 801 struct ifnet *ifp; 802 struct ether_arp *ar; 803 int op; 804 805 ar = mtod(m, struct ether_arp *); 806 op = ntohs(ar->arp_op); 807 switch (op) { 808 case ARPOP_REQUEST: 809 case ARPOP_REPLY: /* per RFC */ 810 in_arpinput(m); 811 return; 812 case ARPOP_REVREPLY: 813 break; 814 case ARPOP_REVREQUEST: /* handled by rarpd(8) */ 815 default: 816 goto out; 817 } 818 if (!revarp_in_progress) 819 goto out; 820 ifp = m->m_pkthdr.rcvif; 821 if (ifp != myip_ifp) /* !same interface */ 822 goto out; 823 if (myip_initialized) 824 goto wake; 825 if (bcmp(ar->arp_tha, ((struct arpcom *)ifp)->ac_enaddr, 826 sizeof(ar->arp_tha))) 827 goto out; 828 bcopy((caddr_t)ar->arp_spa, (caddr_t)&srv_ip, sizeof(srv_ip)); 829 bcopy((caddr_t)ar->arp_tpa, (caddr_t)&myip, sizeof(myip)); 830 myip_initialized = 1; 831 wake: /* Do wakeup every time in case it was missed. */ 832 wakeup((caddr_t)&myip); 833 834 out: 835 m_freem(m); 836 } 837 838 /* 839 * Send a RARP request for the ip address of the specified interface. 840 * The request should be RFC 903-compliant. 841 */ 842 void 843 revarprequest(ifp) 844 struct ifnet *ifp; 845 { 846 struct sockaddr sa; 847 struct mbuf *m; 848 struct ether_header *eh; 849 struct ether_arp *ea; 850 struct arpcom *ac = (struct arpcom *)ifp; 851 852 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 853 return; 854 m->m_len = sizeof(*ea); 855 m->m_pkthdr.len = sizeof(*ea); 856 MH_ALIGN(m, sizeof(*ea)); 857 ea = mtod(m, struct ether_arp *); 858 eh = (struct ether_header *)sa.sa_data; 859 bzero((caddr_t)ea, sizeof(*ea)); 860 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 861 sizeof(eh->ether_dhost)); 862 eh->ether_type = htons(ETHERTYPE_REVARP); 863 ea->arp_hrd = htons(ARPHRD_ETHER); 864 ea->arp_pro = htons(ETHERTYPE_IP); 865 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 866 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 867 ea->arp_op = htons(ARPOP_REVREQUEST); 868 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 869 sizeof(ea->arp_tha)); 870 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, 871 sizeof(ea->arp_sha)); 872 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha, 873 sizeof(ea->arp_tha)); 874 sa.sa_family = AF_UNSPEC; 875 sa.sa_len = sizeof(sa); 876 ifp->if_output(ifp, m, &sa, (struct rtentry *)0); 877 } 878 879 /* 880 * RARP for the ip address of the specified interface, but also 881 * save the ip address of the server that sent the answer. 882 * Timeout if no response is received. 883 */ 884 int 885 revarpwhoarewe(ifp, serv_in, clnt_in) 886 struct ifnet *ifp; 887 struct in_addr *serv_in; 888 struct in_addr *clnt_in; 889 { 890 int result, count = 20; 891 892 if (myip_initialized) 893 return EIO; 894 895 myip_ifp = ifp; 896 revarp_in_progress = 1; 897 while (count--) { 898 revarprequest(ifp); 899 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2); 900 if (result != EWOULDBLOCK) 901 break; 902 } 903 revarp_in_progress = 0; 904 if (!myip_initialized) 905 return ENETUNREACH; 906 907 bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in)); 908 bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in)); 909 return 0; 910 } 911 912 /* For compatibility: only saves interface address. */ 913 int 914 revarpwhoami(in, ifp) 915 struct in_addr *in; 916 struct ifnet *ifp; 917 { 918 struct in_addr server; 919 return (revarpwhoarewe(ifp, &server, in)); 920 } 921 922 923 #ifdef DDB 924 925 #include <machine/db_machdep.h> 926 #include <ddb/db_interface.h> 927 #include <ddb/db_output.h> 928 929 void 930 db_print_sa(sa) 931 struct sockaddr *sa; 932 { 933 int len; 934 u_char *p; 935 936 if (sa == 0) { 937 db_printf("[NULL]"); 938 return; 939 } 940 941 p = (u_char *)sa; 942 len = sa->sa_len; 943 db_printf("["); 944 while (len > 0) { 945 db_printf("%d", *p); 946 p++; 947 len--; 948 if (len) 949 db_printf(","); 950 } 951 db_printf("]\n"); 952 } 953 954 void 955 db_print_ifa(ifa) 956 struct ifaddr *ifa; 957 { 958 if (ifa == 0) 959 return; 960 db_printf(" ifa_addr="); 961 db_print_sa(ifa->ifa_addr); 962 db_printf(" ifa_dsta="); 963 db_print_sa(ifa->ifa_dstaddr); 964 db_printf(" ifa_mask="); 965 db_print_sa(ifa->ifa_netmask); 966 db_printf(" flags=0x%x, refcnt=%d, metric=%d\n", 967 ifa->ifa_flags, ifa->ifa_refcnt, ifa->ifa_metric); 968 } 969 970 void 971 db_print_llinfo(li) 972 caddr_t li; 973 { 974 struct llinfo_arp *la; 975 976 if (li == 0) 977 return; 978 la = (struct llinfo_arp *)li; 979 db_printf(" la_rt=%p la_hold=%p, la_asked=0x%lx\n", 980 la->la_rt, la->la_hold, la->la_asked); 981 } 982 983 /* 984 * Function to pass to rn_walktree(). 985 * Return non-zero error to abort walk. 986 */ 987 int 988 db_show_radix_node(rn, w) 989 struct radix_node *rn; 990 void *w; 991 { 992 struct rtentry *rt = (struct rtentry *)rn; 993 994 db_printf("rtentry=%p", rt); 995 996 db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n", 997 rt->rt_flags, rt->rt_refcnt, rt->rt_use, rt->rt_expire); 998 999 db_printf(" key="); db_print_sa(rt_key(rt)); 1000 db_printf(" mask="); db_print_sa(rt_mask(rt)); 1001 db_printf(" gw="); db_print_sa(rt->rt_gateway); 1002 1003 db_printf(" ifp=%p ", rt->rt_ifp); 1004 if (rt->rt_ifp) 1005 db_printf("(%s)", rt->rt_ifp->if_xname); 1006 else 1007 db_printf("(NULL)"); 1008 1009 db_printf(" ifa=%p\n", rt->rt_ifa); 1010 db_print_ifa(rt->rt_ifa); 1011 1012 db_printf(" genmask="); db_print_sa(rt->rt_genmask); 1013 1014 db_printf(" gwroute=%p llinfo=%p\n", rt->rt_gwroute, rt->rt_llinfo); 1015 db_print_llinfo(rt->rt_llinfo); 1016 return (0); 1017 } 1018 1019 /* 1020 * Function to print all the route trees. 1021 * Use this from ddb: "call db_show_arptab" 1022 */ 1023 int 1024 db_show_arptab() 1025 { 1026 struct radix_node_head *rnh; 1027 rnh = rt_tables[AF_INET]; 1028 db_printf("Route tree for AF_INET\n"); 1029 if (rnh == NULL) { 1030 db_printf(" (not initialized)\n"); 1031 return (0); 1032 } 1033 rn_walktree(rnh, db_show_radix_node, NULL); 1034 return (0); 1035 } 1036 #endif 1037 #endif /* INET */ 1038