1 /* 2 * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)if_ether.c 7.26 (Berkeley) 10/11/92 8 */ 9 10 /* 11 * Ethernet address resolution protocol. 12 * TODO: 13 * add "inuse/lock" bit (or ref. count) along with valid bit 14 */ 15 16 #include <sys/param.h> 17 #include <sys/systm.h> 18 #include <sys/malloc.h> 19 #include <sys/mbuf.h> 20 #include <sys/socket.h> 21 #include <sys/time.h> 22 #include <sys/kernel.h> 23 #include <sys/errno.h> 24 #include <sys/ioctl.h> 25 #include <sys/syslog.h> 26 27 #include <net/if.h> 28 #include <net/if_dl.h> 29 #include <net/route.h> 30 31 #include <netinet/in.h> 32 #include <netinet/in_systm.h> 33 #include <netinet/in_var.h> 34 #include <netinet/ip.h> 35 #include <netinet/if_ether.h> 36 37 #define SIN(s) ((struct sockaddr_in *)s) 38 #define SDL(s) ((struct sockaddr_dl *)s) 39 #define SRP(s) ((struct sockaddr_inarp *)s) 40 41 /* 42 * ARP trailer negotiation. Trailer protocol is not IP specific, 43 * but ARP request/response use IP addresses. 44 */ 45 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL 46 47 48 /* timer values */ 49 int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 50 int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 51 int arpt_down = 20; /* once declared down, don't send for 20 secs */ 52 #define rt_expire rt_rmx.rmx_expire 53 54 static void arprequest __P((struct arpcom *, u_long *, u_long *, u_char *)); 55 static void arptfree __P((struct llinfo_arp *)); 56 static struct llinfo_arp *arplookup __P((u_long, int, int)); 57 static void arpcatchme __P(()); 58 59 extern struct ifnet loif; 60 extern struct timeval time; 61 struct llinfo_arp llinfo_arp = {&llinfo_arp, &llinfo_arp}; 62 struct ifqueue arpintrq = {0, 0, 0, 50}; 63 int arp_inuse, arp_allocated, arp_intimer; 64 int arp_maxtries = 5; 65 int useloopback = 1; /* use loopback interface for local traffic */ 66 int arpinit_done = 0; 67 68 /* 69 * Timeout routine. Age arp_tab entries periodically. 70 */ 71 void 72 arptimer() 73 { 74 int s = splnet(); 75 register struct llinfo_arp *la = llinfo_arp.la_next; 76 77 timeout(arptimer, (caddr_t)0, arpt_prune * hz); 78 while (la != &llinfo_arp) { 79 register struct rtentry *rt = la->la_rt; 80 la = la->la_next; 81 if (rt->rt_expire && rt->rt_expire <= time.tv_sec) 82 arptfree(la->la_prev); /* timer has expired, clear */ 83 } 84 splx(s); 85 } 86 87 /* 88 * Parallel to llc_rtrequest. 89 */ 90 void 91 arp_rtrequest(req, rt, sa) 92 int req; 93 register struct rtentry *rt; 94 struct sockaddr *sa; 95 { 96 register struct sockaddr *gate = rt->rt_gateway; 97 register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 98 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 99 100 if (!arpinit_done) { 101 arpinit_done = 1; 102 timeout(arptimer, (caddr_t)0, hz); 103 } 104 if (rt->rt_flags & RTF_GATEWAY) 105 return; 106 switch (req) { 107 108 case RTM_ADD: 109 /* 110 * XXX: If this is a manually added route to interface 111 * such as older version of routed or gated might provide, 112 * restore cloning bit. 113 */ 114 if ((rt->rt_flags & RTF_HOST) == 0 && 115 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 116 rt->rt_flags |= RTF_CLONING; 117 if (rt->rt_flags & RTF_CLONING) { 118 /* 119 * Case 1: This route should come from a route to iface. 120 */ 121 rt_setgate(rt, rt_key(rt), &null_sdl); 122 gate = rt->rt_gateway; 123 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 124 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 125 rt->rt_expire = time.tv_sec; 126 break; 127 } 128 /* Announce a new entry if requested. */ 129 if (rt->rt_flags & RTF_ANNOUNCE) 130 arprequest((struct arpcom *)rt->rt_ifp, 131 &SIN(rt_key(rt))->sin_addr.s_addr, 132 &SIN(rt_key(rt))->sin_addr.s_addr, 133 (u_char *)LLADDR(SDL(gate))); 134 /*FALLTHROUGH*/ 135 case RTM_RESOLVE: 136 if (gate->sa_family != AF_LINK || 137 gate->sa_len < sizeof(null_sdl)) { 138 log(LOG_DEBUG, "arp_rtrequest: bad gateway value"); 139 break; 140 } 141 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 142 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 143 if (la != 0) 144 break; /* This happens on a route change */ 145 /* 146 * Case 2: This route may come from cloning, or a manual route 147 * add with a LL address. 148 */ 149 R_Malloc(la, struct llinfo_arp *, sizeof(*la)); 150 rt->rt_llinfo = (caddr_t)la; 151 if (la == 0) { 152 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 153 break; 154 } 155 arp_inuse++, arp_allocated++; 156 Bzero(la, sizeof(*la)); 157 la->la_rt = rt; 158 rt->rt_flags |= RTF_LLINFO; 159 insque(la, &llinfo_arp); 160 if (SIN(rt_key(rt))->sin_addr.s_addr == 161 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 162 /* 163 * This test used to be 164 * if (loif.if_flags & IFF_UP) 165 * It allowed local traffic to be forced 166 * through the hardware by configuring the loopback down. 167 * However, it causes problems during network configuration 168 * for boards that can't receive packets they send. 169 * It is now necessary to clear "useloopback" and remove 170 * the route to force traffic out to the hardware. 171 */ 172 rt->rt_expire = 0; 173 Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr, 174 LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6); 175 if (useloopback) 176 rt->rt_ifp = &loif; 177 178 } 179 break; 180 181 case RTM_DELETE: 182 if (la == 0) 183 break; 184 arp_inuse--; 185 remque(la); 186 rt->rt_llinfo = 0; 187 rt->rt_flags &= ~RTF_LLINFO; 188 if (la->la_hold) 189 m_freem(la->la_hold); 190 Free((caddr_t)la); 191 } 192 } 193 194 /* 195 * Broadcast an ARP packet, asking who has addr on interface ac. 196 */ 197 void 198 arpwhohas(ac, addr) 199 register struct arpcom *ac; 200 register struct in_addr *addr; 201 { 202 arprequest(ac, &ac->ac_ipaddr.s_addr, &addr->s_addr, ac->ac_enaddr); 203 } 204 205 /* 206 * Broadcast an ARP request. Caller specifies: 207 * - arp header source ip address 208 * - arp header target ip address 209 * - arp header source ethernet address 210 */ 211 static void 212 arprequest(ac, sip, tip, enaddr) 213 register struct arpcom *ac; 214 register u_long *sip, *tip; 215 register u_char *enaddr; 216 { 217 register struct mbuf *m; 218 register struct ether_header *eh; 219 register struct ether_arp *ea; 220 struct sockaddr sa; 221 222 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 223 return; 224 m->m_len = sizeof(*ea); 225 m->m_pkthdr.len = sizeof(*ea); 226 MH_ALIGN(m, sizeof(*ea)); 227 ea = mtod(m, struct ether_arp *); 228 eh = (struct ether_header *)sa.sa_data; 229 bzero((caddr_t)ea, sizeof (*ea)); 230 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 231 sizeof(eh->ether_dhost)); 232 eh->ether_type = ETHERTYPE_ARP; /* if_output will swap */ 233 ea->arp_hrd = htons(ARPHRD_ETHER); 234 ea->arp_pro = htons(ETHERTYPE_IP); 235 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 236 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 237 ea->arp_op = htons(ARPOP_REQUEST); 238 bcopy((caddr_t)enaddr, (caddr_t)ea->arp_sha, sizeof(ea->arp_sha)); 239 bcopy((caddr_t)sip, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa)); 240 bcopy((caddr_t)tip, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa)); 241 sa.sa_family = AF_UNSPEC; 242 sa.sa_len = sizeof(sa); 243 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 244 } 245 246 /* 247 * Resolve an IP address into an ethernet address. If success, 248 * desten is filled in. If there is no entry in arptab, 249 * set one up and broadcast a request for the IP address. 250 * Hold onto this mbuf and resend it once the address 251 * is finally resolved. A return value of 1 indicates 252 * that desten has been filled in and the packet should be sent 253 * normally; a 0 return indicates that the packet has been 254 * taken over here, either now or for later transmission. 255 */ 256 int 257 arpresolve(ac, rt, m, dst, desten) 258 register struct arpcom *ac; 259 register struct rtentry *rt; 260 struct mbuf *m; 261 register struct sockaddr *dst; 262 register u_char *desten; 263 { 264 register struct llinfo_arp *la; 265 struct sockaddr_dl *sdl; 266 267 if (m->m_flags & M_BCAST) { /* broadcast */ 268 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten, 269 sizeof(etherbroadcastaddr)); 270 return (1); 271 } 272 #ifdef MULTICAST 273 if (m->m_flags & M_MCAST) { /* multicast */ 274 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 275 return(1); 276 } 277 #endif 278 if (rt) 279 la = (struct llinfo_arp *)rt->rt_llinfo; 280 else { 281 if (la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0)) 282 rt = la->la_rt; 283 } 284 if (la == 0 || rt == 0) { 285 log(LOG_DEBUG, "arpresolve: can't allocate llinfo"); 286 m_freem(m); 287 return (0); 288 } 289 sdl = SDL(rt->rt_gateway); 290 /* 291 * Check the address family and length is valid, the address 292 * is resolved; otherwise, try to resolve. 293 */ 294 if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) && 295 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 296 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 297 return 1; 298 } 299 /* 300 * There is an arptab entry, but no ethernet address 301 * response yet. Replace the held mbuf with this 302 * latest one. 303 */ 304 if (la->la_hold) 305 m_freem(la->la_hold); 306 la->la_hold = m; 307 if (rt->rt_expire) { 308 rt->rt_flags &= ~RTF_REJECT; 309 if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) { 310 rt->rt_expire = time.tv_sec; 311 if (la->la_asked++ < arp_maxtries) 312 arpwhohas(ac, &(SIN(dst)->sin_addr)); 313 else { 314 rt->rt_flags |= RTF_REJECT; 315 rt->rt_expire += arpt_down; 316 la->la_asked = 0; 317 } 318 319 } 320 } 321 return (0); 322 } 323 324 /* 325 * Common length and type checks are done here, 326 * then the protocol-specific routine is called. 327 */ 328 void 329 arpintr() 330 { 331 register struct mbuf *m; 332 register struct arphdr *ar; 333 int s; 334 335 while (arpintrq.ifq_head) { 336 s = splimp(); 337 IF_DEQUEUE(&arpintrq, m); 338 splx(s); 339 if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 340 panic("arpintr"); 341 if (m->m_len >= sizeof(struct arphdr) && 342 (ar = mtod(m, struct arphdr *)) && 343 ntohs(ar->ar_hrd) == ARPHRD_ETHER && 344 m->m_len >= 345 sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln) 346 347 switch (ntohs(ar->ar_pro)) { 348 349 case ETHERTYPE_IP: 350 case ETHERTYPE_IPTRAILERS: 351 in_arpinput(m); 352 continue; 353 } 354 m_freem(m); 355 } 356 } 357 358 /* 359 * ARP for Internet protocols on 10 Mb/s Ethernet. 360 * Algorithm is that given in RFC 826. 361 * In addition, a sanity check is performed on the sender 362 * protocol address, to catch impersonators. 363 * We no longer handle negotiations for use of trailer protocol: 364 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 365 * along with IP replies if we wanted trailers sent to us, 366 * and also sent them in response to IP replies. 367 * This allowed either end to announce the desire to receive 368 * trailer packets. 369 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 370 * but formerly didn't normally send requests. 371 */ 372 void 373 in_arpinput(m) 374 struct mbuf *m; 375 { 376 register struct ether_arp *ea; 377 register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; 378 struct ether_header *eh; 379 register struct llinfo_arp *la = 0; 380 register struct rtentry *rt; 381 struct in_ifaddr *ia, *maybe_ia = 0; 382 struct sockaddr_dl *sdl; 383 struct sockaddr sa; 384 struct in_addr isaddr, itaddr, myaddr; 385 int op; 386 387 ea = mtod(m, struct ether_arp *); 388 op = ntohs(ea->arp_op); 389 bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr)); 390 bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr)); 391 for (ia = in_ifaddr; ia; ia = ia->ia_next) 392 if (ia->ia_ifp == &ac->ac_if) { 393 maybe_ia = ia; 394 if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || 395 (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) 396 break; 397 } 398 if (maybe_ia == 0) 399 goto out; 400 myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; 401 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 402 sizeof (ea->arp_sha))) 403 goto out; /* it's from me, ignore it. */ 404 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 405 sizeof (ea->arp_sha))) { 406 log(LOG_ERR, 407 "arp: ether address is broadcast for IP address %x!\n", 408 ntohl(isaddr.s_addr)); 409 goto out; 410 } 411 if (isaddr.s_addr == myaddr.s_addr) { 412 log(LOG_ERR, 413 "duplicate IP address %x!! sent from ethernet address: %s\n", 414 ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha)); 415 itaddr = myaddr; 416 goto reply; 417 } 418 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 419 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 420 if (sdl->sdl_alen && 421 bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) 422 log(LOG_INFO, "arp info overwritten for %x by %s\n", 423 isaddr.s_addr, ether_sprintf(ea->arp_sha)); 424 bcopy((caddr_t)ea->arp_sha, LLADDR(sdl), 425 sdl->sdl_alen = sizeof(ea->arp_sha)); 426 if (rt->rt_expire) 427 rt->rt_expire = time.tv_sec + arpt_keep; 428 rt->rt_flags &= ~RTF_REJECT; 429 la->la_asked = 0; 430 if (la->la_hold) { 431 (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 432 rt_key(rt), rt); 433 la->la_hold = 0; 434 } 435 } 436 reply: 437 if (op != ARPOP_REQUEST) { 438 out: 439 m_freem(m); 440 return; 441 } 442 if (itaddr.s_addr == myaddr.s_addr) { 443 /* I am the target */ 444 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 445 sizeof(ea->arp_sha)); 446 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, 447 sizeof(ea->arp_sha)); 448 } else { 449 la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 450 if (la == NULL) 451 goto out; 452 rt = la->la_rt; 453 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 454 sizeof(ea->arp_sha)); 455 sdl = SDL(rt->rt_gateway); 456 bcopy(LLADDR(sdl), (caddr_t)ea->arp_sha, sizeof(ea->arp_sha)); 457 } 458 459 bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, sizeof(ea->arp_spa)); 460 bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa)); 461 ea->arp_op = htons(ARPOP_REPLY); 462 ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 463 eh = (struct ether_header *)sa.sa_data; 464 bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost, 465 sizeof(eh->ether_dhost)); 466 eh->ether_type = ETHERTYPE_ARP; 467 sa.sa_family = AF_UNSPEC; 468 sa.sa_len = sizeof(sa); 469 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 470 return; 471 } 472 473 /* 474 * Free an arp entry. 475 */ 476 static void 477 arptfree(la) 478 register struct llinfo_arp *la; 479 { 480 register struct rtentry *rt = la->la_rt; 481 register struct sockaddr_dl *sdl; 482 if (rt == 0) 483 panic("arptfree"); 484 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 485 sdl->sdl_family == AF_LINK) { 486 sdl->sdl_alen = 0; 487 la->la_asked = 0; 488 rt->rt_flags &= ~RTF_REJECT; 489 return; 490 } 491 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 492 0, (struct rtentry **)0); 493 } 494 /* 495 * Lookup or enter a new address in arptab. 496 */ 497 static struct llinfo_arp * 498 arplookup(addr, create, proxy) 499 u_long addr; 500 int create, proxy; 501 { 502 register struct rtentry *rt; 503 static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; 504 505 sin.sin_addr.s_addr = addr; 506 sin.sin_other = proxy ? SIN_PROXY : 0; 507 rt = rtalloc1((struct sockaddr *)&sin, create); 508 if (rt == 0) 509 return (0); 510 rt->rt_refcnt--; 511 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 || 512 rt->rt_gateway->sa_family != AF_LINK) { 513 if (create) 514 log(LOG_DEBUG, "arptnew failed on %x\n", ntohl(addr)); 515 return (0); 516 } 517 return ((struct llinfo_arp *)rt->rt_llinfo); 518 } 519 520 int 521 arpioctl(cmd, data) 522 int cmd; 523 caddr_t data; 524 { 525 return (EOPNOTSUPP); 526 } 527