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