1 /* $OpenBSD: if_ether.c,v 1.40 2002/03/27 17:13:47 ian 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 61 #include <netinet/in.h> 62 #include <netinet/in_var.h> 63 #include <netinet/if_ether.h> 64 65 #define SIN(s) ((struct sockaddr_in *)s) 66 #define SDL(s) ((struct sockaddr_dl *)s) 67 #define SRP(s) ((struct sockaddr_inarp *)s) 68 69 /* 70 * ARP trailer negotiation. Trailer protocol is not IP specific, 71 * but ARP request/response use IP addresses. 72 */ 73 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL 74 75 /* timer values */ 76 int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 77 int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 78 int arpt_down = 20; /* once declared down, don't send for 20 secs */ 79 #define rt_expire rt_rmx.rmx_expire 80 81 void arptfree(struct llinfo_arp *); 82 void arptimer(void *); 83 struct llinfo_arp *arplookup(u_int32_t, int, int); 84 void in_arpinput(struct mbuf *); 85 86 LIST_HEAD(, llinfo_arp) llinfo_arp; 87 struct ifqueue arpintrq = {0, 0, 0, 50}; 88 int arp_inuse, arp_allocated, arp_intimer; 89 int arp_maxtries = 5; 90 int useloopback = 1; /* use loopback interface for local traffic */ 91 int arpinit_done = 0; 92 93 /* revarp state */ 94 static struct in_addr myip, srv_ip; 95 static int myip_initialized = 0; 96 static int revarp_in_progress = 0; 97 struct ifnet *myip_ifp = NULL; 98 99 #ifdef DDB 100 #include <uvm/uvm_extern.h> 101 102 void db_print_sa(struct sockaddr *); 103 void db_print_ifa(struct ifaddr *); 104 void db_print_llinfo(caddr_t); 105 int db_show_radix_node(struct radix_node *, void *); 106 #endif 107 108 /* 109 * Timeout routine. Age arp_tab entries periodically. 110 */ 111 /* ARGSUSED */ 112 void 113 arptimer(arg) 114 void *arg; 115 { 116 struct timeout *to = (struct timeout *)arg; 117 int s; 118 struct llinfo_arp *la, *nla; 119 120 s = splsoftnet(); 121 timeout_add(to, arpt_prune * hz); 122 for (la = llinfo_arp.lh_first; la != 0; la = nla) { 123 register struct rtentry *rt = la->la_rt; 124 125 nla = la->la_list.le_next; 126 if (rt->rt_expire && rt->rt_expire <= time.tv_sec) 127 arptfree(la); /* timer has expired; clear */ 128 } 129 splx(s); 130 } 131 132 /* 133 * Parallel to llc_rtrequest. 134 */ 135 void 136 arp_rtrequest(req, rt, info) 137 int req; 138 register struct rtentry *rt; 139 struct rt_addrinfo *info; 140 { 141 register struct sockaddr *gate = rt->rt_gateway; 142 register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 143 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 144 145 if (!arpinit_done) { 146 static struct timeout arptimer_to; 147 148 arpinit_done = 1; 149 /* 150 * We generate expiration times from time.tv_sec 151 * so avoid accidently creating permanent routes. 152 */ 153 if (time.tv_sec == 0) { 154 time.tv_sec++; 155 } 156 157 timeout_set(&arptimer_to, arptimer, &arptimer_to); 158 timeout_add(&arptimer_to, hz); 159 } 160 if (rt->rt_flags & RTF_GATEWAY) 161 return; 162 switch (req) { 163 164 case RTM_ADD: 165 /* 166 * XXX: If this is a manually added route to interface 167 * such as older version of routed or gated might provide, 168 * restore cloning bit. 169 */ 170 if ((rt->rt_flags & RTF_HOST) == 0 && 171 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 172 rt->rt_flags |= RTF_CLONING; 173 if (rt->rt_flags & RTF_CLONING) { 174 /* 175 * Case 1: This route should come from a route to iface. 176 */ 177 rt_setgate(rt, rt_key(rt), 178 (struct sockaddr *)&null_sdl); 179 gate = rt->rt_gateway; 180 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 181 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 182 /* 183 * Give this route an expiration time, even though 184 * it's a "permanent" route, so that routes cloned 185 * from it do not need their expiration time set. 186 */ 187 rt->rt_expire = time.tv_sec; 188 break; 189 } 190 /* Announce a new entry if requested. */ 191 if (rt->rt_flags & RTF_ANNOUNCE) 192 arprequest(rt->rt_ifp, 193 &SIN(rt_key(rt))->sin_addr.s_addr, 194 &SIN(rt_key(rt))->sin_addr.s_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_list); 222 if (SIN(rt_key(rt))->sin_addr.s_addr == 223 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 224 /* 225 * This test used to be 226 * if (lo0ifp->if_flags & IFF_UP) 227 * It allowed local traffic to be forced through 228 * the hardware by configuring the loopback down. 229 * However, it causes problems during network 230 * configuration for boards that can't receive 231 * packets they send. It is now necessary to clear 232 * "useloopback" and remove the route to force 233 * traffic out to the hardware. 234 */ 235 rt->rt_expire = 0; 236 Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr, 237 LLADDR(SDL(gate)), 238 SDL(gate)->sdl_alen = ETHER_ADDR_LEN); 239 if (useloopback) 240 rt->rt_ifp = lo0ifp; 241 } 242 break; 243 244 case RTM_DELETE: 245 if (la == 0) 246 break; 247 arp_inuse--; 248 LIST_REMOVE(la, la_list); 249 rt->rt_llinfo = 0; 250 rt->rt_flags &= ~RTF_LLINFO; 251 if (la->la_hold) 252 m_freem(la->la_hold); 253 Free((caddr_t)la); 254 } 255 } 256 257 /* 258 * Broadcast an ARP request. Caller specifies: 259 * - arp header source ip address 260 * - arp header target ip address 261 * - arp header source ethernet address 262 */ 263 void 264 arprequest(ifp, sip, tip, enaddr) 265 register struct ifnet *ifp; 266 register u_int32_t *sip, *tip; 267 register u_int8_t *enaddr; 268 { 269 register struct mbuf *m; 270 register struct ether_header *eh; 271 register struct ether_arp *ea; 272 struct sockaddr sa; 273 274 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 275 return; 276 m->m_len = sizeof(*ea); 277 m->m_pkthdr.len = sizeof(*ea); 278 MH_ALIGN(m, sizeof(*ea)); 279 ea = mtod(m, struct ether_arp *); 280 eh = (struct ether_header *)sa.sa_data; 281 bzero((caddr_t)ea, sizeof (*ea)); 282 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 283 sizeof(eh->ether_dhost)); 284 eh->ether_type = htons(ETHERTYPE_ARP); /* if_output will not swap */ 285 ea->arp_hrd = htons(ARPHRD_ETHER); 286 ea->arp_pro = htons(ETHERTYPE_IP); 287 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 288 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 289 ea->arp_op = htons(ARPOP_REQUEST); 290 bcopy((caddr_t)enaddr, (caddr_t)eh->ether_shost, 291 sizeof(eh->ether_shost)); 292 bcopy((caddr_t)enaddr, (caddr_t)ea->arp_sha, sizeof(ea->arp_sha)); 293 bcopy((caddr_t)sip, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa)); 294 bcopy((caddr_t)tip, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa)); 295 sa.sa_family = AF_UNSPEC; 296 sa.sa_len = sizeof(sa); 297 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 298 } 299 300 /* 301 * Resolve an IP address into an ethernet address. If success, 302 * desten is filled in. If there is no entry in arptab, 303 * set one up and broadcast a request for the IP address. 304 * Hold onto this mbuf and resend it once the address 305 * is finally resolved. A return value of 1 indicates 306 * that desten has been filled in and the packet should be sent 307 * normally; a 0 return indicates that the packet has been 308 * taken over here, either now or for later transmission. 309 */ 310 int 311 arpresolve(ac, rt, m, dst, desten) 312 register struct arpcom *ac; 313 register struct rtentry *rt; 314 struct mbuf *m; 315 register struct sockaddr *dst; 316 register u_char *desten; 317 { 318 register struct llinfo_arp *la; 319 struct sockaddr_dl *sdl; 320 321 if (m->m_flags & M_BCAST) { /* broadcast */ 322 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten, 323 sizeof(etherbroadcastaddr)); 324 return (1); 325 } 326 if (m->m_flags & M_MCAST) { /* multicast */ 327 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 328 return (1); 329 } 330 if (rt) 331 la = (struct llinfo_arp *)rt->rt_llinfo; 332 else { 333 if ((la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0)) != NULL) 334 rt = la->la_rt; 335 } 336 if (la == 0 || rt == 0) { 337 log(LOG_DEBUG, "arpresolve: can't allocate llinfo\n"); 338 m_freem(m); 339 return (0); 340 } 341 sdl = SDL(rt->rt_gateway); 342 /* 343 * Check the address family and length is valid, the address 344 * is resolved; otherwise, try to resolve. 345 */ 346 if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) && 347 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 348 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 349 return 1; 350 } 351 if (((struct ifnet *)ac)->if_flags & IFF_NOARP) 352 return 0; 353 354 /* 355 * There is an arptab entry, but no ethernet address 356 * response yet. Replace the held mbuf with this 357 * latest one. 358 */ 359 if (la->la_hold) 360 m_freem(la->la_hold); 361 la->la_hold = m; 362 /* 363 * Re-send the ARP request when appropriate. 364 */ 365 #ifdef DIAGNOSTIC 366 if (rt->rt_expire == 0) { 367 /* This should never happen. (Should it? -gwr) */ 368 printf("arpresolve: unresolved and rt_expire == 0\n"); 369 /* Set expiration time to now (expired). */ 370 rt->rt_expire = time.tv_sec; 371 } 372 #endif 373 if (rt->rt_expire) { 374 rt->rt_flags &= ~RTF_REJECT; 375 if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) { 376 rt->rt_expire = time.tv_sec; 377 if (la->la_asked++ < arp_maxtries) 378 arprequest(&ac->ac_if, 379 &(SIN(rt->rt_ifa->ifa_addr)->sin_addr.s_addr), 380 &(SIN(dst)->sin_addr.s_addr), 381 ac->ac_enaddr); 382 else { 383 rt->rt_flags |= RTF_REJECT; 384 rt->rt_expire += arpt_down; 385 la->la_asked = 0; 386 } 387 } 388 } 389 return (0); 390 } 391 392 /* 393 * Common length and type checks are done here, 394 * then the protocol-specific routine is called. 395 */ 396 void 397 arpintr() 398 { 399 register struct mbuf *m; 400 register struct arphdr *ar; 401 int s, len; 402 403 while (arpintrq.ifq_head) { 404 s = splimp(); 405 IF_DEQUEUE(&arpintrq, m); 406 splx(s); 407 if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 408 panic("arpintr"); 409 410 len = sizeof(struct arphdr); 411 if (m->m_len < len && (m = m_pullup(m, len)) == NULL) 412 continue; 413 414 ar = mtod(m, struct arphdr *); 415 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) 416 continue; 417 418 len += 2 * (ar->ar_hln + ar->ar_pln); 419 if (m->m_len < len && (m = m_pullup(m, len)) == NULL) 420 continue; 421 422 switch (ntohs(ar->ar_pro)) { 423 case ETHERTYPE_IP: 424 case ETHERTYPE_IPTRAILERS: 425 in_arpinput(m); 426 continue; 427 } 428 m_freem(m); 429 } 430 } 431 432 /* 433 * ARP for Internet protocols on Ethernet. 434 * Algorithm is that given in RFC 826. 435 * In addition, a sanity check is performed on the sender 436 * protocol address, to catch impersonators. 437 * We no longer handle negotiations for use of trailer protocol: 438 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 439 * along with IP replies if we wanted trailers sent to us, 440 * and also sent them in response to IP replies. 441 * This allowed either end to announce the desire to receive 442 * trailer packets. 443 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 444 * but formerly didn't normally send requests. 445 */ 446 void 447 in_arpinput(m) 448 struct mbuf *m; 449 { 450 register struct ether_arp *ea; 451 register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; 452 struct ether_header *eh; 453 register struct llinfo_arp *la = 0; 454 register struct rtentry *rt; 455 struct in_ifaddr *ia; 456 #if NBRIDGE > 0 457 struct in_ifaddr *bridge_ia = NULL; 458 #endif 459 struct sockaddr_dl *sdl; 460 struct sockaddr sa; 461 struct in_addr isaddr, itaddr, myaddr; 462 int op; 463 464 ea = mtod(m, struct ether_arp *); 465 op = ntohs(ea->arp_op); 466 if ((op != ARPOP_REQUEST) && (op != ARPOP_REPLY)) 467 goto out; 468 #if notyet 469 if ((op == ARPOP_REPLY) && (m->m_flags & (M_BCAST|M_MCAST))) { 470 log(LOG_ERR, 471 "arp: received reply to broadcast or multicast address\n"); 472 goto out; 473 } 474 #endif 475 476 bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof(itaddr)); 477 bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof(isaddr)); 478 479 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 480 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr) 481 continue; 482 483 if (ia->ia_ifp == m->m_pkthdr.rcvif) 484 break; 485 #if NBRIDGE > 0 486 /* 487 * If the interface we received the packet on 488 * is part of a bridge, check to see if we need 489 * to "bridge" the packet to ourselves at this 490 * layer. Note we still prefer a perfect match, 491 * but allow this weaker match if necessary. 492 */ 493 if (m->m_pkthdr.rcvif->if_bridge != NULL && 494 m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge) 495 bridge_ia = ia; 496 #endif 497 } 498 499 #if NBRIDGE > 0 500 if (ia == NULL && bridge_ia != NULL) { 501 ia = bridge_ia; 502 ac = (struct arpcom *)bridge_ia->ia_ifp; 503 } 504 #endif 505 506 if (ia == NULL) { 507 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 508 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr) 509 continue; 510 if (ia->ia_ifp == m->m_pkthdr.rcvif) 511 break; 512 } 513 } 514 515 if (ia == NULL) 516 goto out; 517 518 myaddr = ia->ia_addr.sin_addr; 519 520 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 521 sizeof (ea->arp_sha))) 522 goto out; /* it's from me, ignore it. */ 523 if (ETHER_IS_MULTICAST (&ea->arp_sha[0])) { 524 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 525 sizeof (ea->arp_sha))) 526 log(LOG_ERR, 527 "arp: ether address is broadcast for IP address %s!\n", 528 inet_ntoa(isaddr)); 529 else 530 log(LOG_ERR, 531 "arp: ether address is multicast for IP address %s!\n", 532 inet_ntoa(isaddr)); 533 goto out; 534 } 535 if (myaddr.s_addr && isaddr.s_addr == myaddr.s_addr) { 536 log(LOG_ERR, 537 "duplicate IP address %s sent from ethernet address %s\n", 538 inet_ntoa(isaddr), ether_sprintf(ea->arp_sha)); 539 itaddr = myaddr; 540 goto reply; 541 } 542 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 543 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 544 if (sdl->sdl_alen) { 545 if (bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) { 546 if (rt->rt_flags & RTF_PERMANENT_ARP) { 547 log(LOG_WARNING, 548 "arp: attempt to overwrite permanent " 549 "entry for %s by %s on %s\n", 550 inet_ntoa(isaddr), 551 ether_sprintf(ea->arp_sha), 552 ac->ac_if.if_xname); 553 goto out; 554 } else if (rt->rt_ifp != &ac->ac_if) { 555 log(LOG_WARNING, 556 "arp: attempt to overwrite entry for %s " 557 "on %s by %s on %s\n", 558 inet_ntoa(isaddr), rt->rt_ifp->if_xname, 559 ether_sprintf(ea->arp_sha), 560 ac->ac_if.if_xname); 561 goto out; 562 } else { 563 log(LOG_INFO, 564 "arp info overwritten for %s by %s on %s\n", 565 inet_ntoa(isaddr), 566 ether_sprintf(ea->arp_sha), 567 ac->ac_if.if_xname); 568 rt->rt_expire = 1; /* no longer static */ 569 } 570 } 571 } else if (rt->rt_ifp != &ac->ac_if && !(ac->ac_if.if_bridge && 572 (rt->rt_ifp->if_bridge == ac->ac_if.if_bridge))) { 573 log(LOG_WARNING, 574 "arp: attempt to add entry for %s " 575 "on %s by %s on %s\n", 576 inet_ntoa(isaddr), rt->rt_ifp->if_xname, 577 ether_sprintf(ea->arp_sha), 578 ac->ac_if.if_xname); 579 goto out; 580 } 581 bcopy((caddr_t)ea->arp_sha, LLADDR(sdl), 582 sdl->sdl_alen = sizeof(ea->arp_sha)); 583 if (rt->rt_expire) 584 rt->rt_expire = time.tv_sec + arpt_keep; 585 rt->rt_flags &= ~RTF_REJECT; 586 la->la_asked = 0; 587 if (la->la_hold) { 588 (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 589 rt_key(rt), rt); 590 la->la_hold = 0; 591 } 592 } 593 reply: 594 if (op != ARPOP_REQUEST) { 595 out: 596 m_freem(m); 597 return; 598 } 599 if (itaddr.s_addr == myaddr.s_addr) { 600 /* I am the target */ 601 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 602 sizeof(ea->arp_sha)); 603 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, 604 sizeof(ea->arp_sha)); 605 } else { 606 la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 607 if (la == 0) 608 goto out; 609 rt = la->la_rt; 610 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 611 sizeof(ea->arp_sha)); 612 sdl = SDL(rt->rt_gateway); 613 bcopy(LLADDR(sdl), (caddr_t)ea->arp_sha, sizeof(ea->arp_sha)); 614 } 615 616 bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, sizeof(ea->arp_spa)); 617 bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa)); 618 ea->arp_op = htons(ARPOP_REPLY); 619 ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 620 eh = (struct ether_header *)sa.sa_data; 621 bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost, 622 sizeof(eh->ether_dhost)); 623 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 624 sizeof(eh->ether_shost)); 625 eh->ether_type = htons(ETHERTYPE_ARP); 626 sa.sa_family = AF_UNSPEC; 627 sa.sa_len = sizeof(sa); 628 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 629 return; 630 } 631 632 /* 633 * Free an arp entry. 634 */ 635 void 636 arptfree(la) 637 register struct llinfo_arp *la; 638 { 639 register struct rtentry *rt = la->la_rt; 640 register struct sockaddr_dl *sdl; 641 642 if (rt == 0) 643 panic("arptfree"); 644 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 645 sdl->sdl_family == AF_LINK) { 646 sdl->sdl_alen = 0; 647 la->la_asked = 0; 648 rt->rt_flags &= ~RTF_REJECT; 649 return; 650 } 651 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 652 0, (struct rtentry **)0); 653 } 654 655 /* 656 * Lookup or enter a new address in arptab. 657 */ 658 struct llinfo_arp * 659 arplookup(addr, create, proxy) 660 u_int32_t addr; 661 int create, proxy; 662 { 663 register struct rtentry *rt; 664 static struct sockaddr_inarp sin; 665 666 sin.sin_len = sizeof(sin); 667 sin.sin_family = AF_INET; 668 sin.sin_addr.s_addr = addr; 669 sin.sin_other = proxy ? SIN_PROXY : 0; 670 rt = rtalloc1(sintosa(&sin), create); 671 if (rt == 0) 672 return (0); 673 rt->rt_refcnt--; 674 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 || 675 rt->rt_gateway->sa_family != AF_LINK) { 676 if (create) 677 log(LOG_DEBUG, 678 "arplookup: unable to enter address for %s\n", 679 inet_ntoa(sin.sin_addr)); 680 return (0); 681 } 682 return ((struct llinfo_arp *)rt->rt_llinfo); 683 } 684 685 int 686 arpioctl(cmd, data) 687 u_long cmd; 688 caddr_t data; 689 { 690 691 return (EOPNOTSUPP); 692 } 693 694 void 695 arp_ifinit(ac, ifa) 696 struct arpcom *ac; 697 struct ifaddr *ifa; 698 { 699 700 /* Warn the user if another station has this IP address. */ 701 arprequest(&ac->ac_if, 702 &(IA_SIN(ifa)->sin_addr.s_addr), 703 &(IA_SIN(ifa)->sin_addr.s_addr), 704 ac->ac_enaddr); 705 ifa->ifa_rtrequest = arp_rtrequest; 706 ifa->ifa_flags |= RTF_CLONING; 707 } 708 709 /* 710 * Called from Ethernet interrupt handlers 711 * when ether packet type ETHERTYPE_REVARP 712 * is received. Common length and type checks are done here, 713 * then the protocol-specific routine is called. 714 */ 715 void 716 revarpinput(m) 717 struct mbuf *m; 718 { 719 struct arphdr *ar; 720 721 if (m->m_len < sizeof(struct arphdr)) 722 goto out; 723 ar = mtod(m, struct arphdr *); 724 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) 725 goto out; 726 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln)) 727 goto out; 728 switch (ntohs(ar->ar_pro)) { 729 730 case ETHERTYPE_IP: 731 case ETHERTYPE_IPTRAILERS: 732 in_revarpinput(m); 733 return; 734 735 default: 736 break; 737 } 738 out: 739 m_freem(m); 740 } 741 742 /* 743 * RARP for Internet protocols on Ethernet. 744 * Algorithm is that given in RFC 903. 745 * We are only using for bootstrap purposes to get an ip address for one of 746 * our interfaces. Thus we support no user-interface. 747 * 748 * Since the contents of the RARP reply are specific to the interface that 749 * sent the request, this code must ensure that they are properly associated. 750 * 751 * Note: also supports ARP via RARP packets, per the RFC. 752 */ 753 void 754 in_revarpinput(m) 755 struct mbuf *m; 756 { 757 struct ifnet *ifp; 758 struct ether_arp *ar; 759 int op; 760 761 ar = mtod(m, struct ether_arp *); 762 op = ntohs(ar->arp_op); 763 switch (op) { 764 case ARPOP_REQUEST: 765 case ARPOP_REPLY: /* per RFC */ 766 in_arpinput(m); 767 return; 768 case ARPOP_REVREPLY: 769 break; 770 case ARPOP_REVREQUEST: /* handled by rarpd(8) */ 771 default: 772 goto out; 773 } 774 if (!revarp_in_progress) 775 goto out; 776 ifp = m->m_pkthdr.rcvif; 777 if (ifp != myip_ifp) /* !same interface */ 778 goto out; 779 if (myip_initialized) 780 goto wake; 781 if (bcmp(ar->arp_tha, ((struct arpcom *)ifp)->ac_enaddr, 782 sizeof(ar->arp_tha))) 783 goto out; 784 bcopy((caddr_t)ar->arp_spa, (caddr_t)&srv_ip, sizeof(srv_ip)); 785 bcopy((caddr_t)ar->arp_tpa, (caddr_t)&myip, sizeof(myip)); 786 myip_initialized = 1; 787 wake: /* Do wakeup every time in case it was missed. */ 788 wakeup((caddr_t)&myip); 789 790 out: 791 m_freem(m); 792 } 793 794 /* 795 * Send a RARP request for the ip address of the specified interface. 796 * The request should be RFC 903-compliant. 797 */ 798 void 799 revarprequest(ifp) 800 struct ifnet *ifp; 801 { 802 struct sockaddr sa; 803 struct mbuf *m; 804 struct ether_header *eh; 805 struct ether_arp *ea; 806 struct arpcom *ac = (struct arpcom *)ifp; 807 808 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 809 return; 810 m->m_len = sizeof(*ea); 811 m->m_pkthdr.len = sizeof(*ea); 812 MH_ALIGN(m, sizeof(*ea)); 813 ea = mtod(m, struct ether_arp *); 814 eh = (struct ether_header *)sa.sa_data; 815 bzero((caddr_t)ea, sizeof(*ea)); 816 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 817 sizeof(eh->ether_dhost)); 818 eh->ether_type = htons(ETHERTYPE_REVARP); 819 ea->arp_hrd = htons(ARPHRD_ETHER); 820 ea->arp_pro = htons(ETHERTYPE_IP); 821 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 822 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 823 ea->arp_op = htons(ARPOP_REVREQUEST); 824 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 825 sizeof(ea->arp_tha)); 826 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, 827 sizeof(ea->arp_sha)); 828 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha, 829 sizeof(ea->arp_tha)); 830 sa.sa_family = AF_UNSPEC; 831 sa.sa_len = sizeof(sa); 832 ifp->if_output(ifp, m, &sa, (struct rtentry *)0); 833 } 834 835 /* 836 * RARP for the ip address of the specified interface, but also 837 * save the ip address of the server that sent the answer. 838 * Timeout if no response is received. 839 */ 840 int 841 revarpwhoarewe(ifp, serv_in, clnt_in) 842 struct ifnet *ifp; 843 struct in_addr *serv_in; 844 struct in_addr *clnt_in; 845 { 846 int result, count = 20; 847 848 if (myip_initialized) 849 return EIO; 850 851 myip_ifp = ifp; 852 revarp_in_progress = 1; 853 while (count--) { 854 revarprequest(ifp); 855 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2); 856 if (result != EWOULDBLOCK) 857 break; 858 } 859 revarp_in_progress = 0; 860 if (!myip_initialized) 861 return ENETUNREACH; 862 863 bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in)); 864 bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in)); 865 return 0; 866 } 867 868 /* For compatibility: only saves interface address. */ 869 int 870 revarpwhoami(in, ifp) 871 struct in_addr *in; 872 struct ifnet *ifp; 873 { 874 struct in_addr server; 875 return (revarpwhoarewe(ifp, &server, in)); 876 } 877 878 879 #ifdef DDB 880 881 #include <machine/db_machdep.h> 882 #include <ddb/db_interface.h> 883 #include <ddb/db_output.h> 884 885 void 886 db_print_sa(sa) 887 struct sockaddr *sa; 888 { 889 int len; 890 u_char *p; 891 892 if (sa == 0) { 893 db_printf("[NULL]"); 894 return; 895 } 896 897 p = (u_char *)sa; 898 len = sa->sa_len; 899 db_printf("["); 900 while (len > 0) { 901 db_printf("%d", *p); 902 p++; 903 len--; 904 if (len) 905 db_printf(","); 906 } 907 db_printf("]\n"); 908 } 909 910 void 911 db_print_ifa(ifa) 912 struct ifaddr *ifa; 913 { 914 if (ifa == 0) 915 return; 916 db_printf(" ifa_addr="); 917 db_print_sa(ifa->ifa_addr); 918 db_printf(" ifa_dsta="); 919 db_print_sa(ifa->ifa_dstaddr); 920 db_printf(" ifa_mask="); 921 db_print_sa(ifa->ifa_netmask); 922 db_printf(" flags=0x%x, refcnt=%d, metric=%d\n", 923 ifa->ifa_flags, ifa->ifa_refcnt, ifa->ifa_metric); 924 } 925 926 void 927 db_print_llinfo(li) 928 caddr_t li; 929 { 930 struct llinfo_arp *la; 931 932 if (li == 0) 933 return; 934 la = (struct llinfo_arp *)li; 935 db_printf(" la_rt=%p la_hold=%p, la_asked=0x%lx\n", 936 la->la_rt, la->la_hold, la->la_asked); 937 } 938 939 /* 940 * Function to pass to rn_walktree(). 941 * Return non-zero error to abort walk. 942 */ 943 int 944 db_show_radix_node(rn, w) 945 struct radix_node *rn; 946 void *w; 947 { 948 struct rtentry *rt = (struct rtentry *)rn; 949 950 db_printf("rtentry=%p", rt); 951 952 db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n", 953 rt->rt_flags, rt->rt_refcnt, rt->rt_use, rt->rt_expire); 954 955 db_printf(" key="); db_print_sa(rt_key(rt)); 956 db_printf(" mask="); db_print_sa(rt_mask(rt)); 957 db_printf(" gw="); db_print_sa(rt->rt_gateway); 958 959 db_printf(" ifp=%p ", rt->rt_ifp); 960 if (rt->rt_ifp) 961 db_printf("(%s)", rt->rt_ifp->if_xname); 962 else 963 db_printf("(NULL)"); 964 965 db_printf(" ifa=%p\n", rt->rt_ifa); 966 db_print_ifa(rt->rt_ifa); 967 968 db_printf(" genmask="); db_print_sa(rt->rt_genmask); 969 970 db_printf(" gwroute=%p llinfo=%p\n", rt->rt_gwroute, rt->rt_llinfo); 971 db_print_llinfo(rt->rt_llinfo); 972 return (0); 973 } 974 975 /* 976 * Function to print all the route trees. 977 * Use this from ddb: "call db_show_arptab" 978 */ 979 int 980 db_show_arptab() 981 { 982 struct radix_node_head *rnh; 983 rnh = rt_tables[AF_INET]; 984 db_printf("Route tree for AF_INET\n"); 985 if (rnh == NULL) { 986 db_printf(" (not initialized)\n"); 987 return (0); 988 } 989 rn_walktree(rnh, db_show_radix_node, NULL); 990 return (0); 991 } 992 #endif 993 #endif /* INET */ 994