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