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