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