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.23 (Berkeley) 06/17/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 ea = mtod(m, struct ether_arp *); 353 proto = ntohs(ea->arp_pro); 354 op = ntohs(ea->arp_op); 355 bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr)); 356 bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr)); 357 for (ia = in_ifaddr; ia; ia = ia->ia_next) 358 if (ia->ia_ifp == &ac->ac_if) { 359 maybe_ia = ia; 360 if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || 361 (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) 362 break; 363 } 364 if (maybe_ia == 0) 365 goto out; 366 myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; 367 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 368 sizeof (ea->arp_sha))) 369 goto out; /* it's from me, ignore it. */ 370 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 371 sizeof (ea->arp_sha))) { 372 log(LOG_ERR, 373 "arp: ether address is broadcast for IP address %x!\n", 374 ntohl(isaddr.s_addr)); 375 goto out; 376 } 377 if (isaddr.s_addr == myaddr.s_addr) { 378 log(LOG_ERR, 379 "duplicate IP address %x!! sent from ethernet address: %s\n", 380 ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha)); 381 itaddr = myaddr; 382 if (op == ARPOP_REQUEST) 383 goto reply; 384 goto out; 385 } 386 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 387 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 388 if (sdl->sdl_alen && 389 bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) 390 log(LOG_INFO, "arp info overwritten for %x by %s\n", 391 isaddr.s_addr, ether_sprintf(ea->arp_sha)); 392 completed = 1; 393 bcopy((caddr_t)ea->arp_sha, LLADDR(sdl), 394 sdl->sdl_alen = sizeof(ea->arp_sha)); 395 if (rt->rt_expire) 396 rt->rt_expire = time.tv_sec + arpt_keep; 397 rt->rt_flags &= ~RTF_REJECT; 398 la->la_asked = 0; 399 if (la->la_hold) { 400 (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 401 rt_key(rt), rt); 402 la->la_hold = 0; 403 } 404 } 405 reply: 406 switch (proto) { 407 408 case ETHERTYPE_IPTRAILERS: 409 /* partner says trailers are OK */ 410 if (la) 411 la->la_rt->rt_flags |= RTF_USETRAILERS; 412 /* 413 * Reply to request iff we want trailers. 414 */ 415 if (op != ARPOP_REQUEST || ac->ac_if.if_flags & IFF_NOTRAILERS) 416 goto out; 417 break; 418 419 case ETHERTYPE_IP: 420 /* 421 * Reply if this is an IP request, 422 * or if we want to send a trailer response. 423 * Send the latter only to the IP response 424 * that completes the current ARP entry. 425 */ 426 if (op != ARPOP_REQUEST && 427 (completed == 0 || ac->ac_if.if_flags & IFF_NOTRAILERS)) 428 goto out; 429 } 430 if (itaddr.s_addr == myaddr.s_addr) { 431 /* I am the target */ 432 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 433 sizeof(ea->arp_sha)); 434 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, 435 sizeof(ea->arp_sha)); 436 sendtrailers = !(ac->ac_if.if_flags & IFF_NOTRAILERS); 437 } else { 438 la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 439 if (la == NULL) 440 goto out; 441 rt = la->la_rt; 442 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 443 sizeof(ea->arp_sha)); 444 sdl = SDL(rt->rt_gateway); 445 bcopy(LLADDR(sdl), (caddr_t)ea->arp_sha, sizeof(ea->arp_sha)); 446 sendtrailers = rt->rt_flags & RTF_USETRAILERS; 447 } 448 449 bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, 450 sizeof(ea->arp_spa)); 451 bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, 452 sizeof(ea->arp_spa)); 453 ea->arp_op = htons(ARPOP_REPLY); 454 /* 455 * If incoming packet was an IP reply, 456 * we are sending a reply for type IPTRAILERS. 457 * If we are sending a reply for type IP 458 * and we want to receive trailers, 459 * send a trailer reply as well. 460 */ 461 if (op == ARPOP_REPLY) 462 ea->arp_pro = htons(ETHERTYPE_IPTRAILERS); 463 else if (proto == ETHERTYPE_IP && sendtrailers) 464 mcopy = m_copy(m, 0, (int)M_COPYALL); 465 eh = (struct ether_header *)sa.sa_data; 466 bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost, 467 sizeof(eh->ether_dhost)); 468 eh->ether_type = ETHERTYPE_ARP; 469 sa.sa_family = AF_UNSPEC; 470 sa.sa_len = sizeof(sa); 471 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 472 if (mcopy) { 473 ea = mtod(mcopy, struct ether_arp *); 474 ea->arp_pro = htons(ETHERTYPE_IPTRAILERS); 475 (*ac->ac_if.if_output)(&ac->ac_if, 476 mcopy, &sa, (struct rtentry *)0); 477 } 478 return; 479 out: 480 m_freem(m); 481 return; 482 } 483 484 /* 485 * Free an arp entry. 486 */ 487 arptfree(la) 488 register struct llinfo_arp *la; 489 { 490 register struct rtentry *rt = la->la_rt; 491 register struct sockaddr_dl *sdl; 492 if (rt == 0) 493 panic("arptfree"); 494 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 495 sdl->sdl_family == AF_LINK) { 496 sdl->sdl_alen = 0; 497 la->la_asked = 0; 498 rt->rt_flags &= ~RTF_REJECT; 499 return; 500 } 501 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 502 0, (struct rtentry **)0); 503 } 504 int arpdebug = 0; 505 /* 506 * Lookup or enter a new address in arptab. 507 */ 508 struct llinfo_arp * 509 arplookup(addr, create, proxy) 510 u_long addr; 511 int create, proxy; 512 { 513 register struct rtentry *rt; 514 static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; 515 516 sin.sin_addr.s_addr = addr; 517 sin.sin_other = proxy ? SIN_PROXY : 0; 518 rt = rtalloc1((struct sockaddr *)&sin, create); 519 if (rt == 0) 520 return (0); 521 rt->rt_refcnt--; 522 if ((rt->rt_flags & RTF_GATEWAY) || !(rt->rt_flags & RTF_LLINFO) || 523 rt->rt_gateway->sa_family != AF_LINK) { 524 arpcatchme(); 525 if (arpdebug) 526 log(LOG_DEBUG, "arptnew failed on %x\n", ntohl(addr)); 527 return (0); 528 } 529 return ((struct llinfo_arp *)rt->rt_llinfo); 530 } 531 532 arpcatchme(){} 533 534 arpioctl(cmd, data) 535 int cmd; 536 caddr_t data; 537 { 538 return (EOPNOTSUPP); 539 } 540