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.27 (Berkeley) 01/08/93 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 if (m->m_flags & M_MCAST) { /* multicast */ 273 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 274 return(1); 275 } 276 if (rt) 277 la = (struct llinfo_arp *)rt->rt_llinfo; 278 else { 279 if (la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0)) 280 rt = la->la_rt; 281 } 282 if (la == 0 || rt == 0) { 283 log(LOG_DEBUG, "arpresolve: can't allocate llinfo"); 284 m_freem(m); 285 return (0); 286 } 287 sdl = SDL(rt->rt_gateway); 288 /* 289 * Check the address family and length is valid, the address 290 * is resolved; otherwise, try to resolve. 291 */ 292 if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) && 293 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 294 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 295 return 1; 296 } 297 /* 298 * There is an arptab entry, but no ethernet address 299 * response yet. Replace the held mbuf with this 300 * latest one. 301 */ 302 if (la->la_hold) 303 m_freem(la->la_hold); 304 la->la_hold = m; 305 if (rt->rt_expire) { 306 rt->rt_flags &= ~RTF_REJECT; 307 if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) { 308 rt->rt_expire = time.tv_sec; 309 if (la->la_asked++ < arp_maxtries) 310 arpwhohas(ac, &(SIN(dst)->sin_addr)); 311 else { 312 rt->rt_flags |= RTF_REJECT; 313 rt->rt_expire += arpt_down; 314 la->la_asked = 0; 315 } 316 317 } 318 } 319 return (0); 320 } 321 322 /* 323 * Common length and type checks are done here, 324 * then the protocol-specific routine is called. 325 */ 326 void 327 arpintr() 328 { 329 register struct mbuf *m; 330 register struct arphdr *ar; 331 int s; 332 333 while (arpintrq.ifq_head) { 334 s = splimp(); 335 IF_DEQUEUE(&arpintrq, m); 336 splx(s); 337 if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 338 panic("arpintr"); 339 if (m->m_len >= sizeof(struct arphdr) && 340 (ar = mtod(m, struct arphdr *)) && 341 ntohs(ar->ar_hrd) == ARPHRD_ETHER && 342 m->m_len >= 343 sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln) 344 345 switch (ntohs(ar->ar_pro)) { 346 347 case ETHERTYPE_IP: 348 case ETHERTYPE_IPTRAILERS: 349 in_arpinput(m); 350 continue; 351 } 352 m_freem(m); 353 } 354 } 355 356 /* 357 * ARP for Internet protocols on 10 Mb/s Ethernet. 358 * Algorithm is that given in RFC 826. 359 * In addition, a sanity check is performed on the sender 360 * protocol address, to catch impersonators. 361 * We no longer handle negotiations for use of trailer protocol: 362 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 363 * along with IP replies if we wanted trailers sent to us, 364 * and also sent them in response to IP replies. 365 * This allowed either end to announce the desire to receive 366 * trailer packets. 367 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 368 * but formerly didn't normally send requests. 369 */ 370 void 371 in_arpinput(m) 372 struct mbuf *m; 373 { 374 register struct ether_arp *ea; 375 register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; 376 struct ether_header *eh; 377 register struct llinfo_arp *la = 0; 378 register struct rtentry *rt; 379 struct in_ifaddr *ia, *maybe_ia = 0; 380 struct sockaddr_dl *sdl; 381 struct sockaddr sa; 382 struct in_addr isaddr, itaddr, myaddr; 383 int op; 384 385 ea = mtod(m, struct ether_arp *); 386 op = ntohs(ea->arp_op); 387 bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr)); 388 bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr)); 389 for (ia = in_ifaddr; ia; ia = ia->ia_next) 390 if (ia->ia_ifp == &ac->ac_if) { 391 maybe_ia = ia; 392 if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || 393 (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) 394 break; 395 } 396 if (maybe_ia == 0) 397 goto out; 398 myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; 399 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 400 sizeof (ea->arp_sha))) 401 goto out; /* it's from me, ignore it. */ 402 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 403 sizeof (ea->arp_sha))) { 404 log(LOG_ERR, 405 "arp: ether address is broadcast for IP address %x!\n", 406 ntohl(isaddr.s_addr)); 407 goto out; 408 } 409 if (isaddr.s_addr == myaddr.s_addr) { 410 log(LOG_ERR, 411 "duplicate IP address %x!! sent from ethernet address: %s\n", 412 ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha)); 413 itaddr = myaddr; 414 goto reply; 415 } 416 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 417 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 418 if (sdl->sdl_alen && 419 bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) 420 log(LOG_INFO, "arp info overwritten for %x by %s\n", 421 isaddr.s_addr, ether_sprintf(ea->arp_sha)); 422 bcopy((caddr_t)ea->arp_sha, LLADDR(sdl), 423 sdl->sdl_alen = sizeof(ea->arp_sha)); 424 if (rt->rt_expire) 425 rt->rt_expire = time.tv_sec + arpt_keep; 426 rt->rt_flags &= ~RTF_REJECT; 427 la->la_asked = 0; 428 if (la->la_hold) { 429 (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 430 rt_key(rt), rt); 431 la->la_hold = 0; 432 } 433 } 434 reply: 435 if (op != ARPOP_REQUEST) { 436 out: 437 m_freem(m); 438 return; 439 } 440 if (itaddr.s_addr == myaddr.s_addr) { 441 /* I am the target */ 442 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 443 sizeof(ea->arp_sha)); 444 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, 445 sizeof(ea->arp_sha)); 446 } else { 447 la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 448 if (la == NULL) 449 goto out; 450 rt = la->la_rt; 451 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, 452 sizeof(ea->arp_sha)); 453 sdl = SDL(rt->rt_gateway); 454 bcopy(LLADDR(sdl), (caddr_t)ea->arp_sha, sizeof(ea->arp_sha)); 455 } 456 457 bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, sizeof(ea->arp_spa)); 458 bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa)); 459 ea->arp_op = htons(ARPOP_REPLY); 460 ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 461 eh = (struct ether_header *)sa.sa_data; 462 bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost, 463 sizeof(eh->ether_dhost)); 464 eh->ether_type = ETHERTYPE_ARP; 465 sa.sa_family = AF_UNSPEC; 466 sa.sa_len = sizeof(sa); 467 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 468 return; 469 } 470 471 /* 472 * Free an arp entry. 473 */ 474 static void 475 arptfree(la) 476 register struct llinfo_arp *la; 477 { 478 register struct rtentry *rt = la->la_rt; 479 register struct sockaddr_dl *sdl; 480 if (rt == 0) 481 panic("arptfree"); 482 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 483 sdl->sdl_family == AF_LINK) { 484 sdl->sdl_alen = 0; 485 la->la_asked = 0; 486 rt->rt_flags &= ~RTF_REJECT; 487 return; 488 } 489 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 490 0, (struct rtentry **)0); 491 } 492 /* 493 * Lookup or enter a new address in arptab. 494 */ 495 static struct llinfo_arp * 496 arplookup(addr, create, proxy) 497 u_long addr; 498 int create, proxy; 499 { 500 register struct rtentry *rt; 501 static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; 502 503 sin.sin_addr.s_addr = addr; 504 sin.sin_other = proxy ? SIN_PROXY : 0; 505 rt = rtalloc1((struct sockaddr *)&sin, create); 506 if (rt == 0) 507 return (0); 508 rt->rt_refcnt--; 509 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 || 510 rt->rt_gateway->sa_family != AF_LINK) { 511 if (create) 512 log(LOG_DEBUG, "arptnew failed on %x\n", ntohl(addr)); 513 return (0); 514 } 515 return ((struct llinfo_arp *)rt->rt_llinfo); 516 } 517 518 int 519 arpioctl(cmd, data) 520 int cmd; 521 caddr_t data; 522 { 523 return (EOPNOTSUPP); 524 } 525