1 /* 2 * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 3 * 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 * from: @(#)if_ether.c 7.13 (Berkeley) 10/31/90 34 * $Id: if_arp.c,v 1.2 1993/05/20 03:49:56 cgd Exp $ 35 */ 36 37 /* 38 * Ethernet address resolution protocol. 39 * TODO: 40 * run at splnet (add ARP protocol intr.) 41 * link entries onto hash chains, keep free list 42 * add "inuse/lock" bit (or ref. count) along with valid bit 43 */ 44 45 #include "param.h" 46 #include "systm.h" 47 #include "malloc.h" 48 #include "mbuf.h" 49 #include "socket.h" 50 #include "time.h" 51 #include "kernel.h" 52 #include "errno.h" 53 #include "ioctl.h" 54 #include "syslog.h" 55 56 #include "../net/if.h" 57 #include "in.h" 58 #include "in_systm.h" 59 #include "in_var.h" 60 #include "ip.h" 61 #include "if_ether.h" 62 63 #ifdef GATEWAY 64 #define ARPTAB_BSIZ 16 /* bucket size */ 65 #define ARPTAB_NB 37 /* number of buckets */ 66 #else 67 #define ARPTAB_BSIZ 9 /* bucket size */ 68 #define ARPTAB_NB 19 /* number of buckets */ 69 #endif 70 #define ARPTAB_SIZE (ARPTAB_BSIZ * ARPTAB_NB) 71 struct arptab arptab[ARPTAB_SIZE]; 72 int arptab_size = ARPTAB_SIZE; /* for arp command */ 73 74 /* 75 * ARP trailer negotiation. Trailer protocol is not IP specific, 76 * but ARP request/response use IP addresses. 77 */ 78 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL 79 80 #define ARPTAB_HASH(a) \ 81 ((u_long)(a) % ARPTAB_NB) 82 83 #define ARPTAB_LOOK(at,addr) { \ 84 register n; \ 85 at = &arptab[ARPTAB_HASH(addr) * ARPTAB_BSIZ]; \ 86 for (n = 0 ; n < ARPTAB_BSIZ ; n++,at++) \ 87 if (at->at_iaddr.s_addr == addr) \ 88 break; \ 89 if (n >= ARPTAB_BSIZ) \ 90 at = 0; \ 91 } 92 93 /* timer values */ 94 #define ARPT_AGE (60*1) /* aging timer, 1 min. */ 95 #define ARPT_KILLC 20 /* kill completed entry in 20 mins. */ 96 #define ARPT_KILLI 3 /* kill incomplete entry in 3 minutes */ 97 98 extern struct ifnet loif; 99 100 /* 101 * Timeout routine. Age arp_tab entries once a minute. 102 */ 103 arptimer() 104 { 105 register struct arptab *at; 106 register i; 107 108 timeout(arptimer, (caddr_t)0, ARPT_AGE * hz); 109 at = &arptab[0]; 110 for (i = 0; i < ARPTAB_SIZE; i++, at++) { 111 if (at->at_flags == 0 || (at->at_flags & ATF_PERM)) 112 continue; 113 if (++at->at_timer < ((at->at_flags&ATF_COM) ? 114 ARPT_KILLC : ARPT_KILLI)) 115 continue; 116 /* timer has expired, clear entry */ 117 arptfree(at); 118 } 119 } 120 121 /* 122 * Broadcast an ARP packet, asking who has addr on interface ac. 123 */ 124 arpwhohas(ac, addr) 125 register struct arpcom *ac; 126 struct in_addr *addr; 127 { 128 register struct mbuf *m; 129 register struct ether_header *eh; 130 register struct ether_arp *ea; 131 struct sockaddr sa; 132 133 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 134 return; 135 m->m_len = sizeof(*ea); 136 m->m_pkthdr.len = sizeof(*ea); 137 MH_ALIGN(m, sizeof(*ea)); 138 ea = mtod(m, struct ether_arp *); 139 eh = (struct ether_header *)sa.sa_data; 140 bzero((caddr_t)ea, sizeof (*ea)); 141 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 142 sizeof(eh->ether_dhost)); 143 eh->ether_type = ETHERTYPE_ARP; /* if_output will swap */ 144 ea->arp_hrd = htons(ARPHRD_ETHER); 145 ea->arp_pro = htons(ETHERTYPE_IP); 146 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 147 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 148 ea->arp_op = htons(ARPOP_REQUEST); 149 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, 150 sizeof(ea->arp_sha)); 151 bcopy((caddr_t)&ac->ac_ipaddr, (caddr_t)ea->arp_spa, 152 sizeof(ea->arp_spa)); 153 bcopy((caddr_t)addr, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa)); 154 sa.sa_family = AF_UNSPEC; 155 sa.sa_len = sizeof(sa); 156 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 157 } 158 159 int useloopback = 1; /* use loopback interface for local traffic */ 160 161 /* 162 * Resolve an IP address into an ethernet address. If success, 163 * desten is filled in. If there is no entry in arptab, 164 * set one up and broadcast a request for the IP address. 165 * Hold onto this mbuf and resend it once the address 166 * is finally resolved. A return value of 1 indicates 167 * that desten has been filled in and the packet should be sent 168 * normally; a 0 return indicates that the packet has been 169 * taken over here, either now or for later transmission. 170 * 171 * We do some (conservative) locking here at splimp, since 172 * arptab is also altered from input interrupt service (ecintr/ilintr 173 * calls arpinput when ETHERTYPE_ARP packets come in). 174 */ 175 arpresolve(ac, m, destip, desten, usetrailers) 176 register struct arpcom *ac; 177 struct mbuf *m; 178 register struct in_addr *destip; 179 register u_char *desten; 180 int *usetrailers; 181 { 182 register struct arptab *at; 183 struct sockaddr_in sin; 184 register struct in_ifaddr *ia; 185 u_long lna; 186 int s; 187 188 *usetrailers = 0; 189 if (m->m_flags & M_BCAST) { /* broadcast */ 190 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten, 191 sizeof(etherbroadcastaddr)); 192 return (1); 193 } 194 lna = in_lnaof(*destip); 195 /* if for us, use software loopback driver if up */ 196 for (ia = in_ifaddr; ia; ia = ia->ia_next) 197 if ((ia->ia_ifp == &ac->ac_if) && 198 (destip->s_addr == ia->ia_addr.sin_addr.s_addr)) { 199 /* 200 * This test used to be 201 * if (loif.if_flags & IFF_UP) 202 * It allowed local traffic to be forced 203 * through the hardware by configuring the loopback down. 204 * However, it causes problems during network configuration 205 * for boards that can't receive packets they send. 206 * It is now necessary to clear "useloopback" 207 * to force traffic out to the hardware. 208 */ 209 if (useloopback) { 210 sin.sin_family = AF_INET; 211 sin.sin_addr = *destip; 212 (void) looutput(&loif, m, (struct sockaddr *)&sin, 0); 213 /* 214 * The packet has already been sent and freed. 215 */ 216 return (0); 217 } else { 218 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)desten, 219 sizeof(ac->ac_enaddr)); 220 return (1); 221 } 222 } 223 s = splimp(); 224 ARPTAB_LOOK(at, destip->s_addr); 225 if (at == 0) { /* not found */ 226 if (ac->ac_if.if_flags & IFF_NOARP) { 227 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)desten, 3); 228 desten[3] = (lna >> 16) & 0x7f; 229 desten[4] = (lna >> 8) & 0xff; 230 desten[5] = lna & 0xff; 231 splx(s); 232 return (1); 233 } else { 234 at = arptnew(destip); 235 if (at == 0) 236 panic("arpresolve: no free entry"); 237 at->at_hold = m; 238 arpwhohas(ac, destip); 239 splx(s); 240 return (0); 241 } 242 } 243 at->at_timer = 0; /* restart the timer */ 244 if (at->at_flags & ATF_COM) { /* entry IS complete */ 245 bcopy((caddr_t)at->at_enaddr, (caddr_t)desten, 246 sizeof(at->at_enaddr)); 247 if (at->at_flags & ATF_USETRAILERS) 248 *usetrailers = 1; 249 splx(s); 250 return (1); 251 } 252 /* 253 * There is an arptab entry, but no ethernet address 254 * response yet. Replace the held mbuf with this 255 * latest one. 256 */ 257 if (at->at_hold) 258 m_freem(at->at_hold); 259 at->at_hold = m; 260 arpwhohas(ac, destip); /* ask again */ 261 splx(s); 262 return (0); 263 } 264 265 /* 266 * Called from 10 Mb/s Ethernet interrupt handlers 267 * when ether packet type ETHERTYPE_ARP 268 * is received. Common length and type checks are done here, 269 * then the protocol-specific routine is called. 270 */ 271 arpinput(ac, m) 272 struct arpcom *ac; 273 struct mbuf *m; 274 { 275 register struct arphdr *ar; 276 277 if (ac->ac_if.if_flags & IFF_NOARP) 278 goto out; 279 if (m->m_len < sizeof(struct arphdr)) 280 goto out; 281 ar = mtod(m, struct arphdr *); 282 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) 283 goto out; 284 if (m->m_len < sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln) 285 goto out; 286 287 switch (ntohs(ar->ar_pro)) { 288 289 case ETHERTYPE_IP: 290 case ETHERTYPE_IPTRAILERS: 291 in_arpinput(ac, m); 292 return; 293 294 default: 295 break; 296 } 297 out: 298 m_freem(m); 299 } 300 301 /* 302 * ARP for Internet protocols on 10 Mb/s Ethernet. 303 * Algorithm is that given in RFC 826. 304 * In addition, a sanity check is performed on the sender 305 * protocol address, to catch impersonators. 306 * We also handle negotiations for use of trailer protocol: 307 * ARP replies for protocol type ETHERTYPE_TRAIL are sent 308 * along with IP replies if we want trailers sent to us, 309 * and also send them in response to IP replies. 310 * This allows either end to announce the desire to receive 311 * trailer packets. 312 * We reply to requests for ETHERTYPE_TRAIL protocol as well, 313 * but don't normally send requests. 314 */ 315 in_arpinput(ac, m) 316 register struct arpcom *ac; 317 struct mbuf *m; 318 { 319 register struct ether_arp *ea; 320 struct ether_header *eh; 321 register struct arptab *at; /* same as "merge" flag */ 322 register struct in_ifaddr *ia; 323 struct in_ifaddr *maybe_ia = 0; 324 struct mbuf *mcopy = 0; 325 struct sockaddr_in sin; 326 struct sockaddr sa; 327 struct in_addr isaddr, itaddr, myaddr; 328 int proto, op, s, completed = 0; 329 330 ea = mtod(m, struct ether_arp *); 331 proto = ntohs(ea->arp_pro); 332 op = ntohs(ea->arp_op); 333 bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr)); 334 bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr)); 335 for (ia = in_ifaddr; ia; ia = ia->ia_next) 336 if (ia->ia_ifp == &ac->ac_if) { 337 maybe_ia = ia; 338 if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || 339 (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) 340 break; 341 } 342 if (maybe_ia == 0) 343 goto out; 344 myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; 345 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 346 sizeof (ea->arp_sha))) 347 goto out; /* it's from me, ignore it. */ 348 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 349 sizeof (ea->arp_sha))) { 350 log(LOG_ERR, 351 "arp: ether address is broadcast for IP address %x!\n", 352 ntohl(isaddr.s_addr)); 353 goto out; 354 } 355 if (isaddr.s_addr == myaddr.s_addr) { 356 log(LOG_ERR, 357 "duplicate IP address %x!! sent from ethernet address: %s\n", 358 ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha)); 359 itaddr = myaddr; 360 if (op == ARPOP_REQUEST) 361 goto reply; 362 goto out; 363 } 364 s = splimp(); 365 ARPTAB_LOOK(at, isaddr.s_addr); 366 if (at) { 367 bcopy((caddr_t)ea->arp_sha, (caddr_t)at->at_enaddr, 368 sizeof(ea->arp_sha)); 369 if ((at->at_flags & ATF_COM) == 0) 370 completed = 1; 371 at->at_flags |= ATF_COM; 372 if (at->at_hold) { 373 sin.sin_family = AF_INET; 374 sin.sin_addr = isaddr; 375 (*ac->ac_if.if_output)(&ac->ac_if, at->at_hold, 376 (struct sockaddr *)&sin, (struct rtentry *)0); 377 at->at_hold = 0; 378 } 379 } 380 if (at == 0 && itaddr.s_addr == myaddr.s_addr) { 381 /* ensure we have a table entry */ 382 if (at = arptnew(&isaddr)) { 383 bcopy((caddr_t)ea->arp_sha, (caddr_t)at->at_enaddr, 384 sizeof(ea->arp_sha)); 385 completed = 1; 386 at->at_flags |= ATF_COM; 387 } 388 } 389 splx(s); 390 reply: 391 switch (proto) { 392 393 case ETHERTYPE_IPTRAILERS: 394 /* partner says trailers are OK */ 395 if (at) 396 at->at_flags |= ATF_USETRAILERS; 397 /* 398 * Reply to request iff we want trailers. 399 */ 400 if (op != ARPOP_REQUEST || ac->ac_if.if_flags & IFF_NOTRAILERS) 401 goto out; 402 break; 403 404 case ETHERTYPE_IP: 405 /* 406 * Reply if this is an IP request, 407 * or if we want to send a trailer response. 408 * Send the latter only to the IP response 409 * that completes the current ARP entry. 410 */ 411 if (op != ARPOP_REQUEST && 412 (completed == 0 || ac->ac_if.if_flags & IFF_NOTRAILERS)) 413 goto out; 414 } 415 if (itaddr.s_addr == myaddr.s_addr) { 416 /* I am the target */ 417 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 418 sizeof(ea->arp_sha)); 419 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, 420 sizeof(ea->arp_sha)); 421 } else { 422 ARPTAB_LOOK(at, itaddr.s_addr); 423 if (at == NULL || (at->at_flags & ATF_PUBL) == 0) 424 goto out; 425 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 426 sizeof(ea->arp_sha)); 427 bcopy((caddr_t)at->at_enaddr, (caddr_t)ea->arp_sha, 428 sizeof(ea->arp_sha)); 429 } 430 431 bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, 432 sizeof(ea->arp_spa)); 433 bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, 434 sizeof(ea->arp_spa)); 435 ea->arp_op = htons(ARPOP_REPLY); 436 /* 437 * If incoming packet was an IP reply, 438 * we are sending a reply for type IPTRAILERS. 439 * If we are sending a reply for type IP 440 * and we want to receive trailers, 441 * send a trailer reply as well. 442 */ 443 if (op == ARPOP_REPLY) 444 ea->arp_pro = htons(ETHERTYPE_IPTRAILERS); 445 else if (proto == ETHERTYPE_IP && 446 (ac->ac_if.if_flags & IFF_NOTRAILERS) == 0) 447 mcopy = m_copy(m, 0, (int)M_COPYALL); 448 eh = (struct ether_header *)sa.sa_data; 449 bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost, 450 sizeof(eh->ether_dhost)); 451 eh->ether_type = ETHERTYPE_ARP; 452 sa.sa_family = AF_UNSPEC; 453 sa.sa_len = sizeof(sa); 454 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 455 if (mcopy) { 456 ea = mtod(mcopy, struct ether_arp *); 457 ea->arp_pro = htons(ETHERTYPE_IPTRAILERS); 458 (*ac->ac_if.if_output)(&ac->ac_if, 459 mcopy, &sa, (struct rtentry *)0); 460 } 461 return; 462 out: 463 m_freem(m); 464 return; 465 } 466 467 /* 468 * Free an arptab entry. 469 */ 470 arptfree(at) 471 register struct arptab *at; 472 { 473 int s = splimp(); 474 475 if (at->at_hold) 476 m_freem(at->at_hold); 477 at->at_hold = 0; 478 at->at_timer = at->at_flags = 0; 479 at->at_iaddr.s_addr = 0; 480 splx(s); 481 } 482 483 /* 484 * Enter a new address in arptab, pushing out the oldest entry 485 * from the bucket if there is no room. 486 * This always succeeds since no bucket can be completely filled 487 * with permanent entries (except from arpioctl when testing whether 488 * another permanent entry will fit). 489 * MUST BE CALLED AT SPLIMP. 490 */ 491 struct arptab * 492 arptnew(addr) 493 struct in_addr *addr; 494 { 495 register n; 496 int oldest = -1; 497 register struct arptab *at, *ato = NULL; 498 static int first = 1; 499 500 if (first) { 501 first = 0; 502 timeout(arptimer, (caddr_t)0, hz); 503 } 504 at = &arptab[ARPTAB_HASH(addr->s_addr) * ARPTAB_BSIZ]; 505 for (n = 0; n < ARPTAB_BSIZ; n++,at++) { 506 if (at->at_flags == 0) 507 goto out; /* found an empty entry */ 508 if (at->at_flags & ATF_PERM) 509 continue; 510 if ((int) at->at_timer > oldest) { 511 oldest = at->at_timer; 512 ato = at; 513 } 514 } 515 if (ato == NULL) 516 return (NULL); 517 at = ato; 518 arptfree(at); 519 out: 520 at->at_iaddr = *addr; 521 at->at_flags = ATF_INUSE; 522 return (at); 523 } 524 525 arpioctl(cmd, data) 526 int cmd; 527 caddr_t data; 528 { 529 register struct arpreq *ar = (struct arpreq *)data; 530 register struct arptab *at; 531 register struct sockaddr_in *sin; 532 int s; 533 534 sin = (struct sockaddr_in *)&ar->arp_ha; 535 #if defined(COMPAT_43) && BYTE_ORDER != BIG_ENDIAN 536 if (sin->sin_family == 0 && sin->sin_len < 16) 537 sin->sin_family = sin->sin_len; 538 #endif 539 sin->sin_len = sizeof(ar->arp_ha); 540 sin = (struct sockaddr_in *)&ar->arp_pa; 541 #if defined(COMPAT_43) && BYTE_ORDER != BIG_ENDIAN 542 if (sin->sin_family == 0 && sin->sin_len < 16) 543 sin->sin_family = sin->sin_len; 544 #endif 545 sin->sin_len = sizeof(ar->arp_pa); 546 if (ar->arp_pa.sa_family != AF_INET || 547 ar->arp_ha.sa_family != AF_UNSPEC) 548 return (EAFNOSUPPORT); 549 s = splimp(); 550 ARPTAB_LOOK(at, sin->sin_addr.s_addr); 551 if (at == NULL) { /* not found */ 552 if (cmd != SIOCSARP) { 553 splx(s); 554 return (ENXIO); 555 } 556 if (ifa_ifwithnet(&ar->arp_pa) == NULL) { 557 splx(s); 558 return (ENETUNREACH); 559 } 560 } 561 switch (cmd) { 562 563 case SIOCSARP: /* set entry */ 564 if (at == NULL) { 565 at = arptnew(&sin->sin_addr); 566 if (at == NULL) { 567 splx(s); 568 return (EADDRNOTAVAIL); 569 } 570 if (ar->arp_flags & ATF_PERM) { 571 /* never make all entries in a bucket permanent */ 572 register struct arptab *tat; 573 574 /* try to re-allocate */ 575 tat = arptnew(&sin->sin_addr); 576 if (tat == NULL) { 577 arptfree(at); 578 splx(s); 579 return (EADDRNOTAVAIL); 580 } 581 arptfree(tat); 582 } 583 } 584 bcopy((caddr_t)ar->arp_ha.sa_data, (caddr_t)at->at_enaddr, 585 sizeof(at->at_enaddr)); 586 at->at_flags = ATF_COM | ATF_INUSE | 587 (ar->arp_flags & (ATF_PERM|ATF_PUBL|ATF_USETRAILERS)); 588 at->at_timer = 0; 589 break; 590 591 case SIOCDARP: /* delete entry */ 592 arptfree(at); 593 break; 594 595 case SIOCGARP: /* get entry */ 596 case OSIOCGARP: 597 bcopy((caddr_t)at->at_enaddr, (caddr_t)ar->arp_ha.sa_data, 598 sizeof(at->at_enaddr)); 599 #ifdef COMPAT_43 600 if (cmd == OSIOCGARP) 601 *(u_short *)&ar->arp_ha = ar->arp_ha.sa_family; 602 #endif 603 ar->arp_flags = at->at_flags; 604 break; 605 } 606 splx(s); 607 return (0); 608 } 609