| 1 | /* |
| 2 | * Copyright (c) 1982, 1986, 1988 Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms are permitted |
| 6 | * provided that the above copyright notice and this paragraph are |
| 7 | * duplicated in all such forms and that any documentation, |
| 8 | * advertising materials, and other materials related to such |
| 9 | * distribution and use acknowledge that the software was developed |
| 10 | * by the University of California, Berkeley. The name of the |
| 11 | * University may not be used to endorse or promote products derived |
| 12 | * from this software without specific prior written permission. |
| 13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 16 | * |
| 17 | * @(#)if_ether.c 7.8 (Berkeley) %G% |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Ethernet address resolution protocol. |
| 22 | * TODO: |
| 23 | * run at splnet (add ARP protocol intr.) |
| 24 | * link entries onto hash chains, keep free list |
| 25 | * add "inuse/lock" bit (or ref. count) along with valid bit |
| 26 | */ |
| 27 | |
| 28 | #include "param.h" |
| 29 | #include "systm.h" |
| 30 | #include "malloc.h" |
| 31 | #include "mbuf.h" |
| 32 | #include "socket.h" |
| 33 | #include "time.h" |
| 34 | #include "kernel.h" |
| 35 | #include "errno.h" |
| 36 | #include "ioctl.h" |
| 37 | #include "syslog.h" |
| 38 | |
| 39 | #include "../net/if.h" |
| 40 | #include "in.h" |
| 41 | #include "in_systm.h" |
| 42 | #include "ip.h" |
| 43 | #include "if_ether.h" |
| 44 | |
| 45 | #ifdef GATEWAY |
| 46 | #define ARPTAB_BSIZ 16 /* bucket size */ |
| 47 | #define ARPTAB_NB 37 /* number of buckets */ |
| 48 | #else |
| 49 | #define ARPTAB_BSIZ 9 /* bucket size */ |
| 50 | #define ARPTAB_NB 19 /* number of buckets */ |
| 51 | #endif |
| 52 | #define ARPTAB_SIZE (ARPTAB_BSIZ * ARPTAB_NB) |
| 53 | struct arptab arptab[ARPTAB_SIZE]; |
| 54 | int arptab_size = ARPTAB_SIZE; /* for arp command */ |
| 55 | |
| 56 | /* |
| 57 | * ARP trailer negotiation. Trailer protocol is not IP specific, |
| 58 | * but ARP request/response use IP addresses. |
| 59 | */ |
| 60 | #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL |
| 61 | |
| 62 | #define ARPTAB_HASH(a) \ |
| 63 | ((u_long)(a) % ARPTAB_NB) |
| 64 | |
| 65 | #define ARPTAB_LOOK(at,addr) { \ |
| 66 | register n; \ |
| 67 | at = &arptab[ARPTAB_HASH(addr) * ARPTAB_BSIZ]; \ |
| 68 | for (n = 0 ; n < ARPTAB_BSIZ ; n++,at++) \ |
| 69 | if (at->at_iaddr.s_addr == addr) \ |
| 70 | break; \ |
| 71 | if (n >= ARPTAB_BSIZ) \ |
| 72 | at = 0; \ |
| 73 | } |
| 74 | |
| 75 | /* timer values */ |
| 76 | #define ARPT_AGE (60*1) /* aging timer, 1 min. */ |
| 77 | #define ARPT_KILLC 20 /* kill completed entry in 20 mins. */ |
| 78 | #define ARPT_KILLI 3 /* kill incomplete entry in 3 minutes */ |
| 79 | |
| 80 | extern struct ifnet loif; |
| 81 | |
| 82 | /* |
| 83 | * Timeout routine. Age arp_tab entries once a minute. |
| 84 | */ |
| 85 | arptimer() |
| 86 | { |
| 87 | register struct arptab *at; |
| 88 | register i; |
| 89 | |
| 90 | timeout(arptimer, (caddr_t)0, ARPT_AGE * hz); |
| 91 | at = &arptab[0]; |
| 92 | for (i = 0; i < ARPTAB_SIZE; i++, at++) { |
| 93 | if (at->at_flags == 0 || (at->at_flags & ATF_PERM)) |
| 94 | continue; |
| 95 | if (++at->at_timer < ((at->at_flags&ATF_COM) ? |
| 96 | ARPT_KILLC : ARPT_KILLI)) |
| 97 | continue; |
| 98 | /* timer has expired, clear entry */ |
| 99 | arptfree(at); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Broadcast an ARP packet, asking who has addr on interface ac. |
| 105 | */ |
| 106 | arpwhohas(ac, addr) |
| 107 | register struct arpcom *ac; |
| 108 | struct in_addr *addr; |
| 109 | { |
| 110 | register struct mbuf *m; |
| 111 | register struct ether_header *eh; |
| 112 | register struct ether_arp *ea; |
| 113 | struct sockaddr sa; |
| 114 | |
| 115 | if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) |
| 116 | return; |
| 117 | m->m_len = sizeof(*ea); |
| 118 | m->m_pkthdr.len = sizeof(*ea); |
| 119 | MH_ALIGN(m, sizeof(*ea)); |
| 120 | ea = mtod(m, struct ether_arp *); |
| 121 | eh = (struct ether_header *)sa.sa_data; |
| 122 | bzero((caddr_t)ea, sizeof (*ea)); |
| 123 | bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, |
| 124 | sizeof(eh->ether_dhost)); |
| 125 | eh->ether_type = ETHERTYPE_ARP; /* if_output will swap */ |
| 126 | ea->arp_hrd = htons(ARPHRD_ETHER); |
| 127 | ea->arp_pro = htons(ETHERTYPE_IP); |
| 128 | ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ |
| 129 | ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ |
| 130 | ea->arp_op = htons(ARPOP_REQUEST); |
| 131 | bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, |
| 132 | sizeof(ea->arp_sha)); |
| 133 | bcopy((caddr_t)&ac->ac_ipaddr, (caddr_t)ea->arp_spa, |
| 134 | sizeof(ea->arp_spa)); |
| 135 | bcopy((caddr_t)addr, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa)); |
| 136 | sa.sa_family = AF_UNSPEC; |
| 137 | (*ac->ac_if.if_output)(&ac->ac_if, m, &sa); |
| 138 | } |
| 139 | |
| 140 | int useloopback = 1; /* use loopback interface for local traffic */ |
| 141 | |
| 142 | int useloopback = 1; /* use loopback interface for local traffic */ |
| 143 | |
| 144 | /* |
| 145 | * Resolve an IP address into an ethernet address. If success, |
| 146 | * desten is filled in. If there is no entry in arptab, |
| 147 | * set one up and broadcast a request for the IP address. |
| 148 | * Hold onto this mbuf and resend it once the address |
| 149 | * is finally resolved. A return value of 1 indicates |
| 150 | * that desten has been filled in and the packet should be sent |
| 151 | * normally; a 0 return indicates that the packet has been |
| 152 | * taken over here, either now or for later transmission. |
| 153 | * |
| 154 | * We do some (conservative) locking here at splimp, since |
| 155 | * arptab is also altered from input interrupt service (ecintr/ilintr |
| 156 | * calls arpinput when ETHERTYPE_ARP packets come in). |
| 157 | */ |
| 158 | arpresolve(ac, m, destip, desten, usetrailers) |
| 159 | register struct arpcom *ac; |
| 160 | struct mbuf *m; |
| 161 | register struct in_addr *destip; |
| 162 | register u_char *desten; |
| 163 | int *usetrailers; |
| 164 | { |
| 165 | register struct arptab *at; |
| 166 | struct sockaddr_in sin; |
| 167 | u_long lna; |
| 168 | int s; |
| 169 | |
| 170 | *usetrailers = 0; |
| 171 | if (m->m_flags & M_BCAST) { /* broadcast */ |
| 172 | bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten, |
| 173 | sizeof(etherbroadcastaddr)); |
| 174 | return (1); |
| 175 | } |
| 176 | lna = in_lnaof(*destip); |
| 177 | /* if for us, use software loopback driver if up */ |
| 178 | if (destip->s_addr == ac->ac_ipaddr.s_addr) { |
| 179 | /* |
| 180 | * This test used to be |
| 181 | * if (loif.if_flags & IFF_UP) |
| 182 | * It allowed local traffic to be forced |
| 183 | * through the hardware by configuring the loopback down. |
| 184 | * However, it causes problems during network configuration |
| 185 | * for boards that can't receive packets they send. |
| 186 | * It is now necessary to clear "useloopback" |
| 187 | * to force traffic out to the hardware. |
| 188 | */ |
| 189 | if (useloopback) { |
| 190 | sin.sin_family = AF_INET; |
| 191 | sin.sin_addr = *destip; |
| 192 | (void) looutput(&loif, m, (struct sockaddr *)&sin); |
| 193 | /* |
| 194 | * The packet has already been sent and freed. |
| 195 | */ |
| 196 | return (0); |
| 197 | } else { |
| 198 | bcopy((caddr_t)ac->ac_enaddr, (caddr_t)desten, |
| 199 | sizeof(ac->ac_enaddr)); |
| 200 | return (1); |
| 201 | } |
| 202 | } |
| 203 | s = splimp(); |
| 204 | ARPTAB_LOOK(at, destip->s_addr); |
| 205 | if (at == 0) { /* not found */ |
| 206 | if (ac->ac_if.if_flags & IFF_NOARP) { |
| 207 | bcopy((caddr_t)ac->ac_enaddr, (caddr_t)desten, 3); |
| 208 | desten[3] = (lna >> 16) & 0x7f; |
| 209 | desten[4] = (lna >> 8) & 0xff; |
| 210 | desten[5] = lna & 0xff; |
| 211 | splx(s); |
| 212 | return (1); |
| 213 | } else { |
| 214 | at = arptnew(destip); |
| 215 | if (at == 0) |
| 216 | panic("arpresolve: no free entry"); |
| 217 | at->at_hold = m; |
| 218 | arpwhohas(ac, destip); |
| 219 | splx(s); |
| 220 | return (0); |
| 221 | } |
| 222 | } |
| 223 | at->at_timer = 0; /* restart the timer */ |
| 224 | if (at->at_flags & ATF_COM) { /* entry IS complete */ |
| 225 | bcopy((caddr_t)at->at_enaddr, (caddr_t)desten, |
| 226 | sizeof(at->at_enaddr)); |
| 227 | if (at->at_flags & ATF_USETRAILERS) |
| 228 | *usetrailers = 1; |
| 229 | splx(s); |
| 230 | return (1); |
| 231 | } |
| 232 | /* |
| 233 | * There is an arptab entry, but no ethernet address |
| 234 | * response yet. Replace the held mbuf with this |
| 235 | * latest one. |
| 236 | */ |
| 237 | if (at->at_hold) |
| 238 | m_freem(at->at_hold); |
| 239 | at->at_hold = m; |
| 240 | arpwhohas(ac, destip); /* ask again */ |
| 241 | splx(s); |
| 242 | return (0); |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Called from 10 Mb/s Ethernet interrupt handlers |
| 247 | * when ether packet type ETHERTYPE_ARP |
| 248 | * is received. Common length and type checks are done here, |
| 249 | * then the protocol-specific routine is called. |
| 250 | */ |
| 251 | arpinput(ac, m) |
| 252 | struct arpcom *ac; |
| 253 | struct mbuf *m; |
| 254 | { |
| 255 | register struct arphdr *ar; |
| 256 | |
| 257 | if (ac->ac_if.if_flags & IFF_NOARP) |
| 258 | goto out; |
| 259 | if (m->m_len < sizeof(struct arphdr)) |
| 260 | goto out; |
| 261 | ar = mtod(m, struct arphdr *); |
| 262 | if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) |
| 263 | goto out; |
| 264 | if (m->m_len < sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln) |
| 265 | goto out; |
| 266 | |
| 267 | switch (ntohs(ar->ar_pro)) { |
| 268 | |
| 269 | case ETHERTYPE_IP: |
| 270 | case ETHERTYPE_IPTRAILERS: |
| 271 | in_arpinput(ac, m); |
| 272 | return; |
| 273 | |
| 274 | default: |
| 275 | break; |
| 276 | } |
| 277 | out: |
| 278 | m_freem(m); |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * ARP for Internet protocols on 10 Mb/s Ethernet. |
| 283 | * Algorithm is that given in RFC 826. |
| 284 | * In addition, a sanity check is performed on the sender |
| 285 | * protocol address, to catch impersonators. |
| 286 | * We also handle negotiations for use of trailer protocol: |
| 287 | * ARP replies for protocol type ETHERTYPE_TRAIL are sent |
| 288 | * along with IP replies if we want trailers sent to us, |
| 289 | * and also send them in response to IP replies. |
| 290 | * This allows either end to announce the desire to receive |
| 291 | * trailer packets. |
| 292 | * We reply to requests for ETHERTYPE_TRAIL protocol as well, |
| 293 | * but don't normally send requests. |
| 294 | */ |
| 295 | in_arpinput(ac, m) |
| 296 | register struct arpcom *ac; |
| 297 | struct mbuf *m; |
| 298 | { |
| 299 | register struct ether_arp *ea; |
| 300 | struct ether_header *eh; |
| 301 | register struct arptab *at; /* same as "merge" flag */ |
| 302 | struct mbuf *mcopy = 0; |
| 303 | struct sockaddr_in sin; |
| 304 | struct sockaddr sa; |
| 305 | struct in_addr isaddr, itaddr, myaddr; |
| 306 | int proto, op, s, completed = 0; |
| 307 | |
| 308 | myaddr = ac->ac_ipaddr; |
| 309 | ea = mtod(m, struct ether_arp *); |
| 310 | proto = ntohs(ea->arp_pro); |
| 311 | op = ntohs(ea->arp_op); |
| 312 | bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr)); |
| 313 | bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr)); |
| 314 | if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, |
| 315 | sizeof (ea->arp_sha))) |
| 316 | goto out; /* it's from me, ignore it. */ |
| 317 | if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, |
| 318 | sizeof (ea->arp_sha))) { |
| 319 | log(LOG_ERR, |
| 320 | "arp: ether address is broadcast for IP address %x!\n", |
| 321 | ntohl(isaddr.s_addr)); |
| 322 | goto out; |
| 323 | } |
| 324 | if (isaddr.s_addr == myaddr.s_addr) { |
| 325 | log(LOG_ERR, "%s: %s\n", |
| 326 | "duplicate IP address!! sent from ethernet address", |
| 327 | ether_sprintf(ea->arp_sha)); |
| 328 | itaddr = myaddr; |
| 329 | if (op == ARPOP_REQUEST) |
| 330 | goto reply; |
| 331 | goto out; |
| 332 | } |
| 333 | s = splimp(); |
| 334 | s = splimp(); |
| 335 | ARPTAB_LOOK(at, isaddr.s_addr); |
| 336 | if (at) { |
| 337 | bcopy((caddr_t)ea->arp_sha, (caddr_t)at->at_enaddr, |
| 338 | sizeof(ea->arp_sha)); |
| 339 | if ((at->at_flags & ATF_COM) == 0) |
| 340 | completed = 1; |
| 341 | at->at_flags |= ATF_COM; |
| 342 | if (at->at_hold) { |
| 343 | sin.sin_family = AF_INET; |
| 344 | sin.sin_addr = isaddr; |
| 345 | (*ac->ac_if.if_output)(&ac->ac_if, |
| 346 | at->at_hold, (struct sockaddr *)&sin); |
| 347 | at->at_hold = 0; |
| 348 | } |
| 349 | } |
| 350 | if (at == 0 && itaddr.s_addr == myaddr.s_addr) { |
| 351 | /* ensure we have a table entry */ |
| 352 | if (at = arptnew(&isaddr)) { |
| 353 | bcopy((caddr_t)ea->arp_sha, (caddr_t)at->at_enaddr, |
| 354 | sizeof(ea->arp_sha)); |
| 355 | completed = 1; |
| 356 | at->at_flags |= ATF_COM; |
| 357 | } |
| 358 | } |
| 359 | splx(s); |
| 360 | splx(s); |
| 361 | reply: |
| 362 | switch (proto) { |
| 363 | |
| 364 | case ETHERTYPE_IPTRAILERS: |
| 365 | /* partner says trailers are OK */ |
| 366 | if (at) |
| 367 | at->at_flags |= ATF_USETRAILERS; |
| 368 | /* |
| 369 | * Reply to request iff we want trailers. |
| 370 | */ |
| 371 | if (op != ARPOP_REQUEST || ac->ac_if.if_flags & IFF_NOTRAILERS) |
| 372 | goto out; |
| 373 | break; |
| 374 | |
| 375 | case ETHERTYPE_IP: |
| 376 | /* |
| 377 | * Reply if this is an IP request, |
| 378 | * or if we want to send a trailer response. |
| 379 | * Send the latter only to the IP response |
| 380 | * that completes the current ARP entry. |
| 381 | */ |
| 382 | if (op != ARPOP_REQUEST && |
| 383 | (completed == 0 || ac->ac_if.if_flags & IFF_NOTRAILERS)) |
| 384 | goto out; |
| 385 | } |
| 386 | if (itaddr.s_addr == myaddr.s_addr) { |
| 387 | /* I am the target */ |
| 388 | bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, |
| 389 | sizeof(ea->arp_sha)); |
| 390 | bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, |
| 391 | sizeof(ea->arp_sha)); |
| 392 | } else { |
| 393 | ARPTAB_LOOK(at, itaddr.s_addr); |
| 394 | if (at == NULL || (at->at_flags & ATF_PUBL) == 0) |
| 395 | goto out; |
| 396 | bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, |
| 397 | sizeof(ea->arp_sha)); |
| 398 | bcopy((caddr_t)at->at_enaddr, (caddr_t)ea->arp_sha, |
| 399 | sizeof(ea->arp_sha)); |
| 400 | } |
| 401 | |
| 402 | bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, |
| 403 | sizeof(ea->arp_spa)); |
| 404 | bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, |
| 405 | sizeof(ea->arp_spa)); |
| 406 | ea->arp_op = htons(ARPOP_REPLY); |
| 407 | /* |
| 408 | * If incoming packet was an IP reply, |
| 409 | * we are sending a reply for type IPTRAILERS. |
| 410 | * If we are sending a reply for type IP |
| 411 | * and we want to receive trailers, |
| 412 | * send a trailer reply as well. |
| 413 | */ |
| 414 | if (op == ARPOP_REPLY) |
| 415 | ea->arp_pro = htons(ETHERTYPE_IPTRAILERS); |
| 416 | else if (proto == ETHERTYPE_IP && |
| 417 | (ac->ac_if.if_flags & IFF_NOTRAILERS) == 0) |
| 418 | mcopy = m_copy(m, 0, (int)M_COPYALL); |
| 419 | eh = (struct ether_header *)sa.sa_data; |
| 420 | bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost, |
| 421 | sizeof(eh->ether_dhost)); |
| 422 | eh->ether_type = ETHERTYPE_ARP; |
| 423 | sa.sa_family = AF_UNSPEC; |
| 424 | (*ac->ac_if.if_output)(&ac->ac_if, m, &sa); |
| 425 | if (mcopy) { |
| 426 | ea = mtod(mcopy, struct ether_arp *); |
| 427 | ea->arp_pro = htons(ETHERTYPE_IPTRAILERS); |
| 428 | (*ac->ac_if.if_output)(&ac->ac_if, mcopy, &sa); |
| 429 | } |
| 430 | return; |
| 431 | out: |
| 432 | m_freem(m); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Free an arptab entry. |
| 438 | */ |
| 439 | arptfree(at) |
| 440 | register struct arptab *at; |
| 441 | { |
| 442 | int s = splimp(); |
| 443 | |
| 444 | if (at->at_hold) |
| 445 | m_freem(at->at_hold); |
| 446 | at->at_hold = 0; |
| 447 | at->at_timer = at->at_flags = 0; |
| 448 | at->at_iaddr.s_addr = 0; |
| 449 | splx(s); |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Enter a new address in arptab, pushing out the oldest entry |
| 454 | * from the bucket if there is no room. |
| 455 | * This always succeeds since no bucket can be completely filled |
| 456 | * with permanent entries (except from arpioctl when testing whether |
| 457 | * another permanent entry will fit). |
| 458 | * MUST BE CALLED AT SPLIMP. |
| 459 | */ |
| 460 | struct arptab * |
| 461 | arptnew(addr) |
| 462 | struct in_addr *addr; |
| 463 | { |
| 464 | register n; |
| 465 | int oldest = -1; |
| 466 | register struct arptab *at, *ato = NULL; |
| 467 | static int first = 1; |
| 468 | |
| 469 | if (first) { |
| 470 | first = 0; |
| 471 | timeout(arptimer, (caddr_t)0, hz); |
| 472 | } |
| 473 | at = &arptab[ARPTAB_HASH(addr->s_addr) * ARPTAB_BSIZ]; |
| 474 | for (n = 0; n < ARPTAB_BSIZ; n++,at++) { |
| 475 | if (at->at_flags == 0) |
| 476 | goto out; /* found an empty entry */ |
| 477 | if (at->at_flags & ATF_PERM) |
| 478 | continue; |
| 479 | if ((int) at->at_timer > oldest) { |
| 480 | oldest = at->at_timer; |
| 481 | ato = at; |
| 482 | } |
| 483 | } |
| 484 | if (ato == NULL) |
| 485 | return (NULL); |
| 486 | at = ato; |
| 487 | arptfree(at); |
| 488 | out: |
| 489 | at->at_iaddr = *addr; |
| 490 | at->at_flags = ATF_INUSE; |
| 491 | return (at); |
| 492 | } |
| 493 | |
| 494 | arpioctl(cmd, data) |
| 495 | int cmd; |
| 496 | caddr_t data; |
| 497 | { |
| 498 | register struct arpreq *ar = (struct arpreq *)data; |
| 499 | register struct arptab *at; |
| 500 | register struct sockaddr_in *sin; |
| 501 | int s; |
| 502 | |
| 503 | if (ar->arp_pa.sa_family != AF_INET || |
| 504 | ar->arp_ha.sa_family != AF_UNSPEC) |
| 505 | return (EAFNOSUPPORT); |
| 506 | sin = (struct sockaddr_in *)&ar->arp_pa; |
| 507 | s = splimp(); |
| 508 | ARPTAB_LOOK(at, sin->sin_addr.s_addr); |
| 509 | if (at == NULL) { /* not found */ |
| 510 | if (cmd != SIOCSARP) { |
| 511 | splx(s); |
| 512 | return (ENXIO); |
| 513 | } |
| 514 | if (ifa_ifwithnet(&ar->arp_pa) == NULL) { |
| 515 | splx(s); |
| 516 | return (ENETUNREACH); |
| 517 | } |
| 518 | } |
| 519 | switch (cmd) { |
| 520 | |
| 521 | case SIOCSARP: /* set entry */ |
| 522 | if (at == NULL) { |
| 523 | at = arptnew(&sin->sin_addr); |
| 524 | if (at == NULL) { |
| 525 | splx(s); |
| 526 | return (EADDRNOTAVAIL); |
| 527 | } |
| 528 | if (ar->arp_flags & ATF_PERM) { |
| 529 | /* never make all entries in a bucket permanent */ |
| 530 | register struct arptab *tat; |
| 531 | |
| 532 | /* try to re-allocate */ |
| 533 | tat = arptnew(&sin->sin_addr); |
| 534 | if (tat == NULL) { |
| 535 | arptfree(at); |
| 536 | splx(s); |
| 537 | return (EADDRNOTAVAIL); |
| 538 | } |
| 539 | arptfree(tat); |
| 540 | } |
| 541 | } |
| 542 | bcopy((caddr_t)ar->arp_ha.sa_data, (caddr_t)at->at_enaddr, |
| 543 | sizeof(at->at_enaddr)); |
| 544 | at->at_flags = ATF_COM | ATF_INUSE | |
| 545 | (ar->arp_flags & (ATF_PERM|ATF_PUBL|ATF_USETRAILERS)); |
| 546 | at->at_timer = 0; |
| 547 | break; |
| 548 | |
| 549 | case SIOCDARP: /* delete entry */ |
| 550 | arptfree(at); |
| 551 | break; |
| 552 | |
| 553 | case SIOCGARP: /* get entry */ |
| 554 | bcopy((caddr_t)at->at_enaddr, (caddr_t)ar->arp_ha.sa_data, |
| 555 | sizeof(at->at_enaddr)); |
| 556 | ar->arp_flags = at->at_flags; |
| 557 | break; |
| 558 | } |
| 559 | splx(s); |
| 560 | return (0); |
| 561 | } |