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