if_ether.c revision 82893
1147191Sjkoshy/* 2147191Sjkoshy * Copyright (c) 1982, 1986, 1988, 1993 3147191Sjkoshy * The Regents of the University of California. All rights reserved. 4147191Sjkoshy * 5147191Sjkoshy * Redistribution and use in source and binary forms, with or without 6147191Sjkoshy * modification, are permitted provided that the following conditions 7147191Sjkoshy * are met: 8147191Sjkoshy * 1. Redistributions of source code must retain the above copyright 9147191Sjkoshy * notice, this list of conditions and the following disclaimer. 10147191Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright 11147191Sjkoshy * notice, this list of conditions and the following disclaimer in the 12147191Sjkoshy * documentation and/or other materials provided with the distribution. 13147191Sjkoshy * 3. All advertising materials mentioning features or use of this software 14147191Sjkoshy * must display the following acknowledgement: 15147191Sjkoshy * This product includes software developed by the University of 16147191Sjkoshy * California, Berkeley and its contributors. 17147191Sjkoshy * 4. Neither the name of the University nor the names of its contributors 18147191Sjkoshy * may be used to endorse or promote products derived from this software 19147191Sjkoshy * without specific prior written permission. 20147191Sjkoshy * 21147191Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22147191Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23147191Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24147191Sjkoshy * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25147191Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26147191Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27147191Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28147191Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29147191Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30147191Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31147191Sjkoshy * SUCH DAMAGE. 32147191Sjkoshy * 33147191Sjkoshy * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 34147191Sjkoshy * $FreeBSD: head/sys/netinet/if_ether.c 82893 2001-09-03 21:53:15Z alfred $ 35147191Sjkoshy */ 36147191Sjkoshy 37147191Sjkoshy/* 38147191Sjkoshy * Ethernet address resolution protocol. 39147191Sjkoshy * TODO: 40147191Sjkoshy * add "inuse/lock" bit (or ref. count) along with valid bit 41174410Sjkoshy */ 42185168Sjkoshy 43185168Sjkoshy#include "opt_inet.h" 44185168Sjkoshy#include "opt_bdg.h" 45185168Sjkoshy 46185168Sjkoshy#include <sys/param.h> 47185168Sjkoshy#include <sys/kernel.h> 48174410Sjkoshy#include <sys/queue.h> 49174410Sjkoshy#include <sys/sysctl.h> 50174410Sjkoshy#include <sys/systm.h> 51174410Sjkoshy#include <sys/mbuf.h> 52174410Sjkoshy#include <sys/malloc.h> 53174410Sjkoshy#include <sys/socket.h> 54174410Sjkoshy#include <sys/syslog.h> 55174410Sjkoshy 56174410Sjkoshy#include <net/if.h> 57174410Sjkoshy#include <net/if_dl.h> 58174410Sjkoshy#include <net/if_types.h> 59174410Sjkoshy#include <net/route.h> 60174410Sjkoshy#include <net/netisr.h> 61174410Sjkoshy#include <net/if_llc.h> 62174410Sjkoshy#ifdef BRIDGE 63174410Sjkoshy#include <net/ethernet.h> 64174410Sjkoshy#include <net/bridge.h> 65174410Sjkoshy#endif 66174410Sjkoshy 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 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 * If ARP is disabled on this interface, stop. 408 * XXX 409 * Probably should not allocate empty llinfo struct if we are 410 * not going to be sending out an arp request. 411 */ 412 if (ac->ac_if.if_flags & IFF_NOARP) 413 return (0); 414 /* 415 * There is an arptab entry, but no ethernet address 416 * response yet. Replace the held mbuf with this 417 * latest one. 418 */ 419 if (la->la_hold) 420 m_freem(la->la_hold); 421 la->la_hold = m; 422 if (rt->rt_expire) { 423 rt->rt_flags &= ~RTF_REJECT; 424 if (la->la_asked == 0 || rt->rt_expire != time_second) { 425 rt->rt_expire = time_second; 426 if (la->la_asked++ < arp_maxtries) 427 arprequest(ac, 428 &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 429 &SIN(dst)->sin_addr, ac->ac_enaddr); 430 else { 431 rt->rt_flags |= RTF_REJECT; 432 rt->rt_expire += arpt_down; 433 la->la_asked = 0; 434 } 435 436 } 437 } 438 return (0); 439} 440 441/* 442 * Common length and type checks are done here, 443 * then the protocol-specific routine is called. 444 */ 445static void 446arpintr() 447{ 448 register struct mbuf *m; 449 register struct arphdr *ar; 450 int s; 451 452 while (arpintrq.ifq_head) { 453 s = splimp(); 454 IF_DEQUEUE(&arpintrq, m); 455 splx(s); 456 if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 457 panic("arpintr"); 458 459 if (m->m_len < sizeof(struct arphdr) && 460 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 461 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 462 continue; 463 } 464 ar = mtod(m, struct arphdr *); 465 466 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER 467 && ntohs(ar->ar_hrd) != ARPHRD_IEEE802) { 468 log(LOG_ERR, 469 "arp: unknown hardware address format (0x%2D)\n", 470 (unsigned char *)&ar->ar_hrd, ""); 471 m_freem(m); 472 continue; 473 } 474 475 if (m->m_pkthdr.len < sizeof(struct arphdr) + 2 * ar->ar_hln 476 + 2 * ar->ar_pln) { 477 log(LOG_ERR, "arp: runt packet\n"); 478 m_freem(m); 479 continue; 480 } 481 482 switch (ntohs(ar->ar_pro)) { 483#ifdef INET 484 case ETHERTYPE_IP: 485 in_arpinput(m); 486 continue; 487#endif 488 } 489 m_freem(m); 490 } 491} 492 493#ifdef INET 494/* 495 * ARP for Internet protocols on 10 Mb/s Ethernet. 496 * Algorithm is that given in RFC 826. 497 * In addition, a sanity check is performed on the sender 498 * protocol address, to catch impersonators. 499 * We no longer handle negotiations for use of trailer protocol: 500 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 501 * along with IP replies if we wanted trailers sent to us, 502 * and also sent them in response to IP replies. 503 * This allowed either end to announce the desire to receive 504 * trailer packets. 505 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 506 * but formerly didn't normally send requests. 507 */ 508static int log_arp_wrong_iface = 1; 509static int log_arp_movements = 1; 510 511SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 512 &log_arp_wrong_iface, 0, 513 "log arp packets arriving on the wrong interface"); 514SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 515 &log_arp_movements, 0, 516 "log arp replies from MACs different the the one in the cache"); 517 518 519static void 520in_arpinput(m) 521 struct mbuf *m; 522{ 523 register struct ether_arp *ea; 524 register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; 525 struct ether_header *eh; 526 struct iso88025_header *th = (struct iso88025_header *)0; 527 register struct llinfo_arp *la = 0; 528 register struct rtentry *rt; 529 struct in_ifaddr *ia, *maybe_ia = 0; 530 struct sockaddr_dl *sdl; 531 struct sockaddr sa; 532 struct in_addr isaddr, itaddr, myaddr; 533 int op, rif_len; 534 535 if (m->m_len < sizeof(struct ether_arp) && 536 (m = m_pullup(m, sizeof(struct ether_arp))) == NULL) { 537 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 538 return; 539 } 540 541 ea = mtod(m, struct ether_arp *); 542 op = ntohs(ea->arp_op); 543 (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr)); 544 (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr)); 545 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 546 /* 547 * For a bridge, we want to check the address irrespective 548 * of the receive interface. (This will change slightly 549 * when we have clusters of interfaces). 550 */ 551#ifdef BRIDGE 552#define BRIDGE_TEST (do_bridge) 553#else 554#define BRIDGE_TEST (0) /* cc will optimise the test away */ 555#endif 556 if ((BRIDGE_TEST) || (ia->ia_ifp == &ac->ac_if)) { 557 maybe_ia = ia; 558 if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || 559 (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) { 560 break; 561 } 562 } 563 } 564 if (maybe_ia == 0) { 565 m_freem(m); 566 return; 567 } 568 myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; 569 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 570 sizeof (ea->arp_sha))) { 571 m_freem(m); /* it's from me, ignore it. */ 572 return; 573 } 574 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 575 sizeof (ea->arp_sha))) { 576 log(LOG_ERR, 577 "arp: ether address is broadcast for IP address %s!\n", 578 inet_ntoa(isaddr)); 579 m_freem(m); 580 return; 581 } 582 if (isaddr.s_addr == myaddr.s_addr) { 583 log(LOG_ERR, 584 "arp: %6D is using my IP address %s!\n", 585 ea->arp_sha, ":", inet_ntoa(isaddr)); 586 itaddr = myaddr; 587 goto reply; 588 } 589 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 590 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 591 /* the following is not an error when doing bridging */ 592 if (!BRIDGE_TEST && rt->rt_ifp != &ac->ac_if) { 593 if (log_arp_wrong_iface) 594 log(LOG_ERR, "arp: %s is on %s%d but got reply from %6D on %s%d\n", 595 inet_ntoa(isaddr), 596 rt->rt_ifp->if_name, rt->rt_ifp->if_unit, 597 ea->arp_sha, ":", 598 ac->ac_if.if_name, ac->ac_if.if_unit); 599 goto reply; 600 } 601 if (sdl->sdl_alen && 602 bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) { 603 if (rt->rt_expire) { 604 if (log_arp_movements) 605 log(LOG_INFO, "arp: %s moved from %6D to %6D on %s%d\n", 606 inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":", 607 ea->arp_sha, ":", 608 ac->ac_if.if_name, ac->ac_if.if_unit); 609 } else { 610 log(LOG_ERR, 611 "arp: %6D attempts to modify permanent entry for %s on %s%d\n", 612 ea->arp_sha, ":", inet_ntoa(isaddr), 613 ac->ac_if.if_name, ac->ac_if.if_unit); 614 goto reply; 615 } 616 } 617 (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha)); 618 sdl->sdl_alen = sizeof(ea->arp_sha); 619 sdl->sdl_rcf = (u_short)0; 620 /* 621 * If we receive an arp from a token-ring station over 622 * a token-ring nic then try to save the source 623 * routing info. 624 */ 625 if (ac->ac_if.if_type == IFT_ISO88025) { 626 th = (struct iso88025_header *)m->m_pkthdr.header; 627 rif_len = TR_RCF_RIFLEN(th->rcf); 628 if ((th->iso88025_shost[0] & TR_RII) && 629 (rif_len > 2)) { 630 sdl->sdl_rcf = th->rcf; 631 sdl->sdl_rcf ^= htons(TR_RCF_DIR); 632 memcpy(sdl->sdl_route, th->rd, rif_len - 2); 633 sdl->sdl_rcf &= ~htons(TR_RCF_BCST_MASK); 634 /* 635 * Set up source routing information for 636 * reply packet (XXX) 637 */ 638 m->m_data -= rif_len; 639 m->m_len += rif_len; 640 m->m_pkthdr.len += rif_len; 641 } else { 642 th->iso88025_shost[0] &= ~TR_RII; 643 } 644 m->m_data -= 8; 645 m->m_len += 8; 646 m->m_pkthdr.len += 8; 647 th->rcf = sdl->sdl_rcf; 648 } else { 649 sdl->sdl_rcf = (u_short)0; 650 } 651 if (rt->rt_expire) 652 rt->rt_expire = time_second + arpt_keep; 653 rt->rt_flags &= ~RTF_REJECT; 654 la->la_asked = 0; 655 if (la->la_hold) { 656 (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 657 rt_key(rt), rt); 658 la->la_hold = 0; 659 } 660 } 661reply: 662 if (op != ARPOP_REQUEST) { 663 m_freem(m); 664 return; 665 } 666 if (itaddr.s_addr == myaddr.s_addr) { 667 /* I am the target */ 668 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 669 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 670 } else { 671 la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 672 if (la == NULL) { 673 struct sockaddr_in sin; 674 675 if (!arp_proxyall) { 676 m_freem(m); 677 return; 678 } 679 680 bzero(&sin, sizeof sin); 681 sin.sin_family = AF_INET; 682 sin.sin_len = sizeof sin; 683 sin.sin_addr = itaddr; 684 685 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 686 if (!rt) { 687 m_freem(m); 688 return; 689 } 690 /* 691 * Don't send proxies for nodes on the same interface 692 * as this one came out of, or we'll get into a fight 693 * over who claims what Ether address. 694 */ 695 if (rt->rt_ifp == &ac->ac_if) { 696 rtfree(rt); 697 m_freem(m); 698 return; 699 } 700 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 701 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 702 rtfree(rt); 703 704 /* 705 * Also check that the node which sent the ARP packet 706 * is on the the interface we expect it to be on. This 707 * avoids ARP chaos if an interface is connected to the 708 * wrong network. 709 */ 710 sin.sin_addr = isaddr; 711 712 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 713 if (!rt) { 714 m_freem(m); 715 return; 716 } 717 if (rt->rt_ifp != &ac->ac_if) { 718 log(LOG_INFO, "arp_proxy: ignoring request" 719 " from %s via %s%d, expecting %s%d\n", 720 inet_ntoa(isaddr), ac->ac_if.if_name, 721 ac->ac_if.if_unit, rt->rt_ifp->if_name, 722 rt->rt_ifp->if_unit); 723 rtfree(rt); 724 m_freem(m); 725 return; 726 } 727 rtfree(rt); 728 729#ifdef DEBUG_PROXY 730 printf("arp: proxying for %s\n", 731 inet_ntoa(itaddr)); 732#endif 733 } else { 734 rt = la->la_rt; 735 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 736 sdl = SDL(rt->rt_gateway); 737 (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha)); 738 } 739 } 740 741 (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa)); 742 (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa)); 743 ea->arp_op = htons(ARPOP_REPLY); 744 ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 745 switch (ac->ac_if.if_type) { 746 case IFT_ISO88025: 747 /* Re-arrange the source/dest address */ 748 memcpy(th->iso88025_dhost, th->iso88025_shost, 749 sizeof(th->iso88025_dhost)); 750 memcpy(th->iso88025_shost, ac->ac_enaddr, 751 sizeof(th->iso88025_shost)); 752 /* Set the source routing bit if neccesary */ 753 if (th->iso88025_dhost[0] & TR_RII) { 754 th->iso88025_dhost[0] &= ~TR_RII; 755 if (TR_RCF_RIFLEN(th->rcf) > 2) 756 th->iso88025_shost[0] |= TR_RII; 757 } 758 /* Copy the addresses, ac and fc into sa_data */ 759 memcpy(sa.sa_data, th->iso88025_dhost, 760 sizeof(th->iso88025_dhost) * 2); 761 sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC; 762 sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME; 763 break; 764 case IFT_ETHER: 765 case IFT_FDDI: 766 /* 767 * May not be correct for types not explictly 768 * listed, but it is our best guess. 769 */ 770 default: 771 eh = (struct ether_header *)sa.sa_data; 772 (void)memcpy(eh->ether_dhost, ea->arp_tha, 773 sizeof(eh->ether_dhost)); 774 eh->ether_type = htons(ETHERTYPE_ARP); 775 break; 776 } 777 sa.sa_family = AF_UNSPEC; 778 sa.sa_len = sizeof(sa); 779 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 780 return; 781} 782#endif 783 784/* 785 * Free an arp entry. 786 */ 787static void 788arptfree(la) 789 register struct llinfo_arp *la; 790{ 791 register struct rtentry *rt = la->la_rt; 792 register struct sockaddr_dl *sdl; 793 if (rt == 0) 794 panic("arptfree"); 795 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 796 sdl->sdl_family == AF_LINK) { 797 sdl->sdl_alen = 0; 798 la->la_asked = 0; 799 rt->rt_flags &= ~RTF_REJECT; 800 return; 801 } 802 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 803 0, (struct rtentry **)0); 804} 805/* 806 * Lookup or enter a new address in arptab. 807 */ 808static struct llinfo_arp * 809arplookup(addr, create, proxy) 810 u_long addr; 811 int create, proxy; 812{ 813 register struct rtentry *rt; 814 static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; 815 const char *why = 0; 816 817 sin.sin_addr.s_addr = addr; 818 sin.sin_other = proxy ? SIN_PROXY : 0; 819 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 820 if (rt == 0) 821 return (0); 822 rt->rt_refcnt--; 823 824 if (rt->rt_flags & RTF_GATEWAY) 825 why = "host is not on local network"; 826 else if ((rt->rt_flags & RTF_LLINFO) == 0) 827 why = "could not allocate llinfo"; 828 else if (rt->rt_gateway->sa_family != AF_LINK) 829 why = "gateway route is not ours"; 830 831 if (why && create) { 832 log(LOG_DEBUG, "arplookup %s failed: %s\n", 833 inet_ntoa(sin.sin_addr), why); 834 return 0; 835 } else if (why) { 836 return 0; 837 } 838 return ((struct llinfo_arp *)rt->rt_llinfo); 839} 840 841void 842arp_ifinit(ac, ifa) 843 struct arpcom *ac; 844 struct ifaddr *ifa; 845{ 846 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 847 arprequest(ac, &IA_SIN(ifa)->sin_addr, 848 &IA_SIN(ifa)->sin_addr, ac->ac_enaddr); 849 ifa->ifa_rtrequest = arp_rtrequest; 850 ifa->ifa_flags |= RTF_CLONING; 851} 852 853static void 854arp_init(void) 855{ 856 857 arpintrq.ifq_maxlen = 50; 858 mtx_init(&arpintrq.ifq_mtx, "arp_inq", MTX_DEF); 859} 860 861SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 862