if_arp.c revision 1.63 1 /* $NetBSD: if_arp.c,v 1.63 1999/08/05 04:04:29 sommerfeld Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Public Access Networks Corporation ("Panix"). It was developed under
9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1988, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)if_ether.c 8.2 (Berkeley) 9/26/94
73 */
74
75 /*
76 * Ethernet address resolution protocol.
77 * TODO:
78 * add "inuse/lock" bit (or ref. count) along with valid bit
79 */
80
81 #include "opt_ddb.h"
82 #include "opt_inet.h"
83
84 #ifdef INET
85
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/malloc.h>
89 #include <sys/mbuf.h>
90 #include <sys/socket.h>
91 #include <sys/time.h>
92 #include <sys/kernel.h>
93 #include <sys/errno.h>
94 #include <sys/ioctl.h>
95 #include <sys/syslog.h>
96 #include <sys/proc.h>
97 #include <sys/protosw.h>
98 #include <sys/domain.h>
99
100 #include <net/ethertypes.h>
101 #include <net/if.h>
102 #include <net/if_dl.h>
103 #include <net/if_token.h>
104 #include <net/if_types.h>
105 #include <net/route.h>
106
107
108 #include <netinet/in.h>
109 #include <netinet/in_systm.h>
110 #include <netinet/in_var.h>
111 #include <netinet/ip.h>
112 #include <netinet/if_inarp.h>
113
114 #include "loop.h"
115 #include "token.h"
116
117 #define SIN(s) ((struct sockaddr_in *)s)
118 #define SDL(s) ((struct sockaddr_dl *)s)
119 #define SRP(s) ((struct sockaddr_inarp *)s)
120
121 /*
122 * ARP trailer negotiation. Trailer protocol is not IP specific,
123 * but ARP request/response use IP addresses.
124 */
125 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
126
127 /* timer values */
128 int arpt_prune = (5*60*1); /* walk list every 5 minutes */
129 int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
130 int arpt_down = 20; /* once declared down, don't send for 20 secs */
131 #define rt_expire rt_rmx.rmx_expire
132
133 static void arprequest __P((struct ifnet *,
134 struct in_addr *, struct in_addr *, u_int8_t *));
135 static void arptfree __P((struct llinfo_arp *));
136 static void arptimer __P((void *));
137 static struct llinfo_arp *arplookup __P((struct in_addr *, int, int));
138 static void in_arpinput __P((struct mbuf *));
139
140 #if NLOOP > 0
141 extern struct ifnet loif[NLOOP];
142 #endif
143 LIST_HEAD(, llinfo_arp) llinfo_arp;
144 struct ifqueue arpintrq = {0, 0, 0, 50};
145 int arp_inuse, arp_allocated, arp_intimer;
146 int arp_maxtries = 5;
147 int useloopback = 1; /* use loopback interface for local traffic */
148 int arpinit_done = 0;
149
150 /* revarp state */
151 static struct in_addr myip, srv_ip;
152 static int myip_initialized = 0;
153 static int revarp_in_progress = 0;
154 static struct ifnet *myip_ifp = NULL;
155
156 #ifdef DDB
157 static void db_print_sa __P((struct sockaddr *));
158 static void db_print_ifa __P((struct ifaddr *));
159 static void db_print_llinfo __P((caddr_t));
160 static int db_show_radix_node __P((struct radix_node *, void *));
161 #endif
162
163 /*
164 * this should be elsewhere.
165 */
166
167 static char *
168 lla_snprintf __P((u_int8_t *, int));
169
170 static char *
171 lla_snprintf(adrp, len)
172 u_int8_t *adrp;
173 int len;
174 {
175 static char buf[16*3];
176 static const char hexdigits[] = {
177 '0','1','2','3','4','5','6','7',
178 '8','9','a','b','c','d','e','f'
179 };
180
181 int i;
182 char *p;
183
184 p = buf;
185
186 *p++ = hexdigits[(*adrp)>>4];
187 *p++ = hexdigits[(*adrp++)&0xf];
188
189 for (i=1; i<len && i<16; i++) {
190 *p++ = ':';
191 *p++ = hexdigits[(*adrp)>>4];
192 *p++ = hexdigits[(*adrp++)&0xf];
193 }
194
195 *p = 0;
196 return buf;
197 }
198
199 struct protosw arpsw[] = {
200 { 0, 0, 0, 0,
201 0, 0, 0, 0,
202 0,
203 0, 0, 0, arp_drain,
204 }
205 };
206
207
208 struct domain arpdomain =
209 { PF_ARP, "arp", 0, 0, 0,
210 arpsw, &arpsw[sizeof(arpsw)/sizeof(arpsw[0])]
211 };
212
213 /*
214 * ARP table locking.
215 *
216 * to prevent lossage vs. the arp_drain routine (which may be called at
217 * any time, including in a device driver context), we do two things:
218 *
219 * 1) manipulation of la->la_hold is done at splimp() (for all of
220 * about two instructions).
221 *
222 * 2) manipulation of the arp table's linked list is done under the
223 * protection of the ARP_LOCK; if arp_drain() or arptimer is called
224 * while the arp table is locked, we punt and try again later.
225 */
226
227 int arp_locked;
228
229 static __inline int arp_lock_try __P((int));
230 static __inline void arp_unlock __P((void));
231
232 static __inline int
233 arp_lock_try(int recurse)
234 {
235 int s;
236
237 s = splimp();
238 if (!recurse && arp_locked) {
239 splx(s);
240 return (0);
241 }
242 arp_locked++;
243 splx(s);
244 return (1);
245 }
246
247 static __inline void
248 arp_unlock()
249 {
250 int s;
251
252 s = splimp();
253 arp_locked--;
254 splx(s);
255 }
256
257 #ifdef DIAGNOSTIC
258 #define ARP_LOCK(recurse) \
259 do { \
260 if (arp_lock_try(recurse) == 0) { \
261 printf("%s:%d: arp already locked\n", __FILE__, __LINE__); \
262 panic("arp_lock"); \
263 } \
264 } while (0)
265 #define ARP_LOCK_CHECK() \
266 do { \
267 if (arp_locked == 0) { \
268 printf("%s:%d: arp lock not held\n", __FILE__, __LINE__); \
269 panic("arp lock check"); \
270 } \
271 } while (0)
272 #else
273 #define ARP_LOCK(x) (void) arp_lock_try(x)
274 #define ARP_LOCK_CHECK() /* nothing */
275 #endif
276
277 #define ARP_UNLOCK() arp_unlock()
278
279 /*
280 * ARP protocol drain routine. Called when memory is in short supply.
281 * Called at splimp();
282 */
283
284 void
285 arp_drain()
286 {
287 register struct llinfo_arp *la, *nla;
288 int count = 0;
289 struct mbuf *mold;
290
291 printf("arp_drain: entered\n");
292
293 if (arp_lock_try(0) == 0) {
294 printf("arp_drain: locked; punting\n");
295 return;
296 }
297
298
299 for (la = llinfo_arp.lh_first; la != 0; la = nla) {
300 nla = la->la_list.le_next;
301
302 mold = la->la_hold;
303 la->la_hold = 0;
304
305 if (mold) {
306 m_freem(mold);
307 count++;
308 }
309 }
310 ARP_UNLOCK();
311
312 printf("arp_drain: freed %d\n", count);
313 }
314
315
316 /*
317 * Timeout routine. Age arp_tab entries periodically.
318 */
319 /* ARGSUSED */
320 static void
321 arptimer(arg)
322 void *arg;
323 {
324 int s;
325 register struct llinfo_arp *la, *nla;
326
327 s = splsoftnet();
328
329 if (arp_lock_try(0) == 0) {
330 /* get it later.. */
331 splx(s);
332 return;
333 }
334
335 timeout(arptimer, NULL, arpt_prune * hz);
336 for (la = llinfo_arp.lh_first; la != 0; la = nla) {
337 register struct rtentry *rt = la->la_rt;
338
339 nla = la->la_list.le_next;
340 if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
341 arptfree(la); /* timer has expired; clear */
342 }
343
344 ARP_UNLOCK();
345
346 splx(s);
347 }
348
349 /*
350 * Parallel to llc_rtrequest.
351 */
352 void
353 arp_rtrequest(req, rt, sa)
354 int req;
355 register struct rtentry *rt;
356 struct sockaddr *sa;
357 {
358 register struct sockaddr *gate = rt->rt_gateway;
359 register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
360 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
361 size_t allocsize;
362 struct mbuf *mold;
363 int s;
364
365 if (!arpinit_done) {
366 arpinit_done = 1;
367 /*
368 * We generate expiration times from time.tv_sec
369 * so avoid accidently creating permanent routes.
370 */
371 if (time.tv_sec == 0) {
372 time.tv_sec++;
373 }
374 timeout(arptimer, (caddr_t)0, hz);
375 }
376 if (rt->rt_flags & RTF_GATEWAY)
377 return;
378
379 ARP_LOCK(1); /* we may already be locked here. */
380
381 switch (req) {
382
383 case RTM_ADD:
384 /*
385 * XXX: If this is a manually added route to interface
386 * such as older version of routed or gated might provide,
387 * restore cloning bit.
388 */
389 if ((rt->rt_flags & RTF_HOST) == 0 &&
390 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
391 rt->rt_flags |= RTF_CLONING;
392 if (rt->rt_flags & RTF_CLONING) {
393 /*
394 * Case 1: This route should come from a route to iface.
395 */
396 rt_setgate(rt, rt_key(rt),
397 (struct sockaddr *)&null_sdl);
398 gate = rt->rt_gateway;
399 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
400 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
401 /*
402 * Give this route an expiration time, even though
403 * it's a "permanent" route, so that routes cloned
404 * from it do not need their expiration time set.
405 */
406 rt->rt_expire = time.tv_sec;
407 break;
408 }
409 /* Announce a new entry if requested. */
410 if (rt->rt_flags & RTF_ANNOUNCE)
411 arprequest(rt->rt_ifp,
412 &SIN(rt_key(rt))->sin_addr,
413 &SIN(rt_key(rt))->sin_addr,
414 (u_char *)LLADDR(SDL(gate)));
415 /*FALLTHROUGH*/
416 case RTM_RESOLVE:
417 if (gate->sa_family != AF_LINK ||
418 gate->sa_len < sizeof(null_sdl)) {
419 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
420 break;
421 }
422 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
423 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
424 if (la != 0)
425 break; /* This happens on a route change */
426 /*
427 * Case 2: This route may come from cloning, or a manual route
428 * add with a LL address.
429 */
430 switch (SDL(gate)->sdl_type) {
431 #if NTOKEN > 0
432 case IFT_ISO88025:
433 allocsize = sizeof(*la) + sizeof(struct token_rif);
434 break;
435 #endif /* NTOKEN > 0 */
436 default:
437 allocsize = sizeof(*la);
438 }
439 R_Malloc(la, struct llinfo_arp *, allocsize);
440 rt->rt_llinfo = (caddr_t)la;
441 if (la == 0) {
442 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
443 break;
444 }
445 arp_inuse++, arp_allocated++;
446 Bzero(la, allocsize);
447 la->la_rt = rt;
448 rt->rt_flags |= RTF_LLINFO;
449 LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
450 if (in_hosteq(SIN(rt_key(rt))->sin_addr,
451 (IA_SIN(rt->rt_ifa))->sin_addr)) {
452 /*
453 * This test used to be
454 * if (loif.if_flags & IFF_UP)
455 * It allowed local traffic to be forced through
456 * the hardware by configuring the loopback down.
457 * However, it causes problems during network
458 * configuration for boards that can't receive
459 * packets they send. It is now necessary to clear
460 * "useloopback" and remove the route to force
461 * traffic out to the hardware.
462 */
463 rt->rt_expire = 0;
464 Bcopy(LLADDR(rt->rt_ifp->if_sadl),
465 LLADDR(SDL(gate)),
466 SDL(gate)->sdl_alen =
467 rt->rt_ifp->if_data.ifi_addrlen);
468 #if NLOOP > 0
469 if (useloopback)
470 rt->rt_ifp = &loif[0];
471 #endif
472 }
473 break;
474
475 case RTM_DELETE:
476 if (la == 0)
477 break;
478 arp_inuse--;
479 LIST_REMOVE(la, la_list);
480 rt->rt_llinfo = 0;
481 rt->rt_flags &= ~RTF_LLINFO;
482
483 s = splimp();
484 mold = la->la_hold;
485 la->la_hold = 0;
486 splx(s);
487
488 if (mold)
489 m_freem(mold);
490
491 Free((caddr_t)la);
492 }
493 ARP_UNLOCK();
494 }
495
496 /*
497 * Broadcast an ARP request. Caller specifies:
498 * - arp header source ip address
499 * - arp header target ip address
500 * - arp header source ethernet address
501 */
502 static void
503 arprequest(ifp, sip, tip, enaddr)
504 register struct ifnet *ifp;
505 register struct in_addr *sip, *tip;
506 register u_int8_t *enaddr;
507 {
508 register struct mbuf *m;
509 struct arphdr *ah;
510 struct sockaddr sa;
511
512 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
513 return;
514 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
515 2*ifp->if_data.ifi_addrlen;
516 m->m_pkthdr.len = m->m_len;
517 MH_ALIGN(m, m->m_len);
518 ah = mtod(m, struct arphdr *);
519 bzero((caddr_t)ah, m->m_len);
520 ah->ar_pro = htons(ETHERTYPE_IP);
521 ah->ar_hln = ifp->if_data.ifi_addrlen; /* hardware address length */
522 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
523 ah->ar_op = htons(ARPOP_REQUEST);
524 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
525 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
526 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
527 sa.sa_family = AF_ARP;
528 sa.sa_len = 2;
529 m->m_flags |= M_BCAST;
530 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
531 }
532
533 /*
534 * Resolve an IP address into an ethernet address. If success,
535 * desten is filled in. If there is no entry in arptab,
536 * set one up and broadcast a request for the IP address.
537 * Hold onto this mbuf and resend it once the address
538 * is finally resolved. A return value of 1 indicates
539 * that desten has been filled in and the packet should be sent
540 * normally; a 0 return indicates that the packet has been
541 * taken over here, either now or for later transmission.
542 */
543 int
544 arpresolve(ifp, rt, m, dst, desten)
545 register struct ifnet *ifp;
546 register struct rtentry *rt;
547 struct mbuf *m;
548 register struct sockaddr *dst;
549 register u_char *desten;
550 {
551 register struct llinfo_arp *la;
552 struct sockaddr_dl *sdl;
553 struct mbuf *mold;
554 int s;
555
556 if (rt)
557 la = (struct llinfo_arp *)rt->rt_llinfo;
558 else {
559 if ((la = arplookup(&SIN(dst)->sin_addr, 1, 0)) != NULL)
560 rt = la->la_rt;
561 }
562 if (la == 0 || rt == 0) {
563 log(LOG_DEBUG, "arpresolve: can't allocate llinfo\n");
564 m_freem(m);
565 return (0);
566 }
567 sdl = SDL(rt->rt_gateway);
568 /*
569 * Check the address family and length is valid, the address
570 * is resolved; otherwise, try to resolve.
571 */
572 if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
573 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
574 bcopy(LLADDR(sdl), desten,
575 min(sdl->sdl_alen, ifp->if_data.ifi_addrlen));
576 return 1;
577 }
578 /*
579 * There is an arptab entry, but no ethernet address
580 * response yet. Replace the held mbuf with this
581 * latest one.
582 */
583
584 s = splimp();
585 mold = la->la_hold;
586 la->la_hold = m;
587 splx(s);
588
589 if (mold)
590 m_freem(mold);
591
592
593 /*
594 * Re-send the ARP request when appropriate.
595 */
596 #ifdef DIAGNOSTIC
597 if (rt->rt_expire == 0) {
598 /* This should never happen. (Should it? -gwr) */
599 printf("arpresolve: unresolved and rt_expire == 0\n");
600 /* Set expiration time to now (expired). */
601 rt->rt_expire = time.tv_sec;
602 }
603 #endif
604 if (rt->rt_expire) {
605 rt->rt_flags &= ~RTF_REJECT;
606 if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
607 rt->rt_expire = time.tv_sec;
608 if (la->la_asked++ < arp_maxtries)
609 arprequest(ifp,
610 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
611 &SIN(dst)->sin_addr,
612 LLADDR(ifp->if_sadl));
613 else {
614 rt->rt_flags |= RTF_REJECT;
615 rt->rt_expire += arpt_down;
616 la->la_asked = 0;
617 }
618 }
619 }
620 return (0);
621 }
622
623 /*
624 * Common length and type checks are done here,
625 * then the protocol-specific routine is called.
626 */
627 void
628 arpintr()
629 {
630 register struct mbuf *m;
631 register struct arphdr *ar;
632 int s;
633
634 while (arpintrq.ifq_head) {
635 s = splimp();
636 IF_DEQUEUE(&arpintrq, m);
637 splx(s);
638 if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
639 panic("arpintr");
640
641 if (m->m_len >= sizeof(struct arphdr) &&
642 (ar = mtod(m, struct arphdr *)) &&
643 /* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
644 m->m_len >=
645 sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
646 switch (ntohs(ar->ar_pro)) {
647
648 case ETHERTYPE_IP:
649 case ETHERTYPE_IPTRAILERS:
650 in_arpinput(m);
651 continue;
652 }
653 m_freem(m);
654 }
655 }
656
657 /*
658 * ARP for Internet protocols on 10 Mb/s Ethernet.
659 * Algorithm is that given in RFC 826.
660 * In addition, a sanity check is performed on the sender
661 * protocol address, to catch impersonators.
662 * We no longer handle negotiations for use of trailer protocol:
663 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
664 * along with IP replies if we wanted trailers sent to us,
665 * and also sent them in response to IP replies.
666 * This allowed either end to announce the desire to receive
667 * trailer packets.
668 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
669 * but formerly didn't normally send requests.
670 */
671 static void
672 in_arpinput(m)
673 struct mbuf *m;
674 {
675 struct arphdr *ah;
676 register struct ifnet *ifp = m->m_pkthdr.rcvif;
677 register struct llinfo_arp *la = 0;
678 register struct rtentry *rt;
679 struct in_ifaddr *ia;
680 struct sockaddr_dl *sdl;
681 struct sockaddr sa;
682 struct in_addr isaddr, itaddr, myaddr;
683 int op;
684 struct mbuf *mold;
685 int s;
686
687
688 ah = mtod(m, struct arphdr *);
689 op = ntohs(ah->ar_op);
690 bcopy((caddr_t)ar_spa(ah), (caddr_t)&isaddr, sizeof (isaddr));
691 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&itaddr, sizeof (itaddr));
692
693 /*
694 * If the target IP address is zero, ignore the packet.
695 * This prevents the code below from tring to answer
696 * when we are using IP address zero (booting).
697 */
698 if (in_nullhost(itaddr))
699 goto out;
700
701 /*
702 * If the source IP address is zero, this is most likely a
703 * confused host trying to use IP address zero. (Windoze?)
704 * XXX: Should we bother trying to reply to these?
705 */
706 if (in_nullhost(isaddr))
707 goto out;
708
709 /*
710 * Search for a matching interface address
711 * or any address on the interface to use
712 * as a dummy address in the rest of this function
713 */
714 INADDR_TO_IA(itaddr, ia);
715 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
716 NEXT_IA_WITH_SAME_ADDR(ia);
717
718 if (ia == NULL) {
719 INADDR_TO_IA(isaddr, ia);
720 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
721 NEXT_IA_WITH_SAME_ADDR(ia);
722
723 if (ia == NULL) {
724 IFP_TO_IA(ifp, ia);
725 if (ia == NULL)
726 goto out;
727 }
728 }
729
730 myaddr = ia->ia_addr.sin_addr;
731
732 if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
733 ifp->if_data.ifi_addrlen))
734 goto out; /* it's from me, ignore it. */
735
736 if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)ifp->if_broadcastaddr,
737 ifp->if_data.ifi_addrlen)) {
738 log(LOG_ERR,
739 "%s: arp: link address is broadcast for IP address %s!\n",
740 ifp->if_xname, in_fmtaddr(isaddr));
741 goto out;
742 }
743
744 if (in_hosteq(isaddr, myaddr)) {
745 log(LOG_ERR,
746 "duplicate IP address %s sent from link address %s\n",
747 in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
748 itaddr = myaddr;
749 goto reply;
750 }
751 la = arplookup(&isaddr, in_hosteq(itaddr, myaddr), 0);
752 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
753 if (sdl->sdl_alen &&
754 bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
755 if (rt->rt_flags & RTF_STATIC) {
756 log(LOG_INFO,
757 "%s tried to overwrite permanent arp info"
758 " for %s\n",
759 lla_snprintf(ar_sha(ah), ah->ar_hln),
760 in_fmtaddr(isaddr));
761 goto out;
762 } else if (rt->rt_ifp != ifp) {
763 log(LOG_INFO,
764 "%s on %s tried to overwrite "
765 "arp info for %s on %s\n",
766 lla_snprintf(ar_sha(ah), ah->ar_hln),
767 ifp->if_xname, in_fmtaddr(isaddr),
768 rt->rt_ifp->if_xname);
769 goto out;
770 } else {
771 log(LOG_INFO,
772 "arp info overwritten for %s by %s\n",
773 in_fmtaddr(isaddr),
774 lla_snprintf(ar_sha(ah), ah->ar_hln));
775 }
776 }
777 /*
778 * sanity check for the address length.
779 * XXX this does not work for protocols with variable address
780 * length. -is
781 */
782 if (sdl->sdl_alen &&
783 sdl->sdl_alen != ah->ar_hln) {
784 log(LOG_WARNING,
785 "arp from %s: new addr len %d, was %d",
786 in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
787 }
788 if (ifp->if_data.ifi_addrlen != ah->ar_hln) {
789 log(LOG_WARNING,
790 "arp from %s: addr len: new %d, i/f %d (ignored)",
791 in_fmtaddr(isaddr), ah->ar_hln,
792 ifp->if_data.ifi_addrlen);
793 goto reply;
794 }
795 #if NTOKEN > 0
796 /*
797 * XXX uses m_data and assumes the complete answer including
798 * XXX token-ring headers is in the same buf
799 */
800 if (ifp->if_type == IFT_ISO88025) {
801 struct token_header *trh;
802
803 trh = (struct token_header *)M_TRHSTART(m);
804 if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
805 struct token_rif *rif;
806 size_t riflen;
807
808 rif = TOKEN_RIF(trh);
809 riflen = (ntohs(rif->tr_rcf) &
810 TOKEN_RCF_LEN_MASK) >> 8;
811
812 if (riflen > 2 &&
813 riflen < sizeof(struct token_rif) &&
814 (riflen & 1) == 0) {
815 rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
816 rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
817 bcopy(rif, TOKEN_RIF(la), riflen);
818 }
819 }
820 }
821 #endif /* NTOKEN > 0 */
822 bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
823 sdl->sdl_alen = ah->ar_hln);
824 if (rt->rt_expire)
825 rt->rt_expire = time.tv_sec + arpt_keep;
826 rt->rt_flags &= ~RTF_REJECT;
827 la->la_asked = 0;
828
829 s = splimp();
830 mold = la->la_hold;
831 la->la_hold = 0;
832 splx(s);
833
834 if (mold)
835 (*ifp->if_output)(ifp, mold, rt_key(rt), rt);
836 }
837 reply:
838 if (op != ARPOP_REQUEST) {
839 out:
840 m_freem(m);
841 return;
842 }
843 if (in_hosteq(itaddr, myaddr)) {
844 /* I am the target */
845 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
846 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
847 } else {
848 la = arplookup(&itaddr, 0, SIN_PROXY);
849 if (la == 0)
850 goto out;
851 rt = la->la_rt;
852 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
853 sdl = SDL(rt->rt_gateway);
854 bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
855 }
856
857 bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
858 bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
859 ah->ar_op = htons(ARPOP_REPLY);
860 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
861 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
862 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
863 m->m_pkthdr.len = m->m_len;
864 sa.sa_family = AF_ARP;
865 sa.sa_len = 2;
866 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
867 return;
868 }
869
870 /*
871 * Free an arp entry.
872 */
873 static void
874 arptfree(la)
875 register struct llinfo_arp *la;
876 {
877 register struct rtentry *rt = la->la_rt;
878 register struct sockaddr_dl *sdl;
879
880 ARP_LOCK_CHECK();
881
882 if (rt == 0)
883 panic("arptfree");
884 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
885 sdl->sdl_family == AF_LINK) {
886 sdl->sdl_alen = 0;
887 la->la_asked = 0;
888 rt->rt_flags &= ~RTF_REJECT;
889 return;
890 }
891 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
892 0, (struct rtentry **)0);
893 }
894
895 /*
896 * Lookup or enter a new address in arptab.
897 */
898 static struct llinfo_arp *
899 arplookup(addr, create, proxy)
900 struct in_addr *addr;
901 int create, proxy;
902 {
903 register struct rtentry *rt;
904 static struct sockaddr_inarp sin;
905 const char *why = 0;
906
907 sin.sin_len = sizeof(sin);
908 sin.sin_family = AF_INET;
909 sin.sin_addr = *addr;
910 sin.sin_other = proxy ? SIN_PROXY : 0;
911 rt = rtalloc1(sintosa(&sin), create);
912 if (rt == 0)
913 return (0);
914 rt->rt_refcnt--;
915
916 if (rt->rt_flags & RTF_GATEWAY)
917 why = "host is not on local network";
918 else if ((rt->rt_flags & RTF_LLINFO) == 0)
919 why = "could not allocate llinfo";
920 else if (rt->rt_gateway->sa_family != AF_LINK)
921 why = "gateway route is not ours";
922 else
923 return ((struct llinfo_arp *)rt->rt_llinfo);
924
925 if (create)
926 log(LOG_DEBUG, "arplookup: unable to enter address"
927 " for %s (%s)\n",
928 in_fmtaddr(*addr), why);
929 return (0);
930 }
931
932 int
933 arpioctl(cmd, data)
934 u_long cmd;
935 caddr_t data;
936 {
937
938 return (EOPNOTSUPP);
939 }
940
941 void
942 arp_ifinit(ifp, ifa)
943 struct ifnet *ifp;
944 struct ifaddr *ifa;
945 {
946 struct in_addr *ip;
947
948 /*
949 * Warn the user if another station has this IP address,
950 * but only if the interface IP address is not zero.
951 */
952 ip = &IA_SIN(ifa)->sin_addr;
953 if (!in_nullhost(*ip))
954 arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl));
955
956 ifa->ifa_rtrequest = arp_rtrequest;
957 ifa->ifa_flags |= RTF_CLONING;
958 }
959
960 /*
961 * Called from 10 Mb/s Ethernet interrupt handlers
962 * when ether packet type ETHERTYPE_REVARP
963 * is received. Common length and type checks are done here,
964 * then the protocol-specific routine is called.
965 */
966 void
967 revarpinput(m)
968 struct mbuf *m;
969 {
970 struct arphdr *ar;
971
972 if (m->m_len < sizeof(struct arphdr))
973 goto out;
974 ar = mtod(m, struct arphdr *);
975 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
976 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
977 goto out;
978 #endif
979 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
980 goto out;
981 switch (ntohs(ar->ar_pro)) {
982
983 case ETHERTYPE_IP:
984 case ETHERTYPE_IPTRAILERS:
985 in_revarpinput(m);
986 return;
987
988 default:
989 break;
990 }
991 out:
992 m_freem(m);
993 }
994
995 /*
996 * RARP for Internet protocols on 10 Mb/s Ethernet.
997 * Algorithm is that given in RFC 903.
998 * We are only using for bootstrap purposes to get an ip address for one of
999 * our interfaces. Thus we support no user-interface.
1000 *
1001 * Since the contents of the RARP reply are specific to the interface that
1002 * sent the request, this code must ensure that they are properly associated.
1003 *
1004 * Note: also supports ARP via RARP packets, per the RFC.
1005 */
1006 void
1007 in_revarpinput(m)
1008 struct mbuf *m;
1009 {
1010 struct ifnet *ifp;
1011 struct arphdr *ah;
1012 int op;
1013
1014 ah = mtod(m, struct arphdr *);
1015 op = ntohs(ah->ar_op);
1016 switch (op) {
1017 case ARPOP_REQUEST:
1018 case ARPOP_REPLY: /* per RFC */
1019 in_arpinput(m);
1020 return;
1021 case ARPOP_REVREPLY:
1022 break;
1023 case ARPOP_REVREQUEST: /* handled by rarpd(8) */
1024 default:
1025 goto out;
1026 }
1027 if (!revarp_in_progress)
1028 goto out;
1029 ifp = m->m_pkthdr.rcvif;
1030 if (ifp != myip_ifp) /* !same interface */
1031 goto out;
1032 if (myip_initialized)
1033 goto wake;
1034 if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
1035 goto out;
1036 bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
1037 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
1038 myip_initialized = 1;
1039 wake: /* Do wakeup every time in case it was missed. */
1040 wakeup((caddr_t)&myip);
1041
1042 out:
1043 m_freem(m);
1044 }
1045
1046 /*
1047 * Send a RARP request for the ip address of the specified interface.
1048 * The request should be RFC 903-compliant.
1049 */
1050 void
1051 revarprequest(ifp)
1052 struct ifnet *ifp;
1053 {
1054 struct sockaddr sa;
1055 struct mbuf *m;
1056 struct arphdr *ah;
1057
1058 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
1059 return;
1060 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
1061 2*ifp->if_data.ifi_addrlen;
1062 m->m_pkthdr.len = m->m_len;
1063 MH_ALIGN(m, m->m_len);
1064 ah = mtod(m, struct arphdr *);
1065 bzero((caddr_t)ah, m->m_len);
1066 ah->ar_pro = htons(ETHERTYPE_IP);
1067 ah->ar_hln = ifp->if_data.ifi_addrlen; /* hardware address length */
1068 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
1069 ah->ar_op = htons(ARPOP_REVREQUEST);
1070
1071 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
1072 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln);
1073
1074 sa.sa_family = AF_ARP;
1075 sa.sa_len = 2;
1076 m->m_flags |= M_BCAST;
1077 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
1078
1079 }
1080
1081 /*
1082 * RARP for the ip address of the specified interface, but also
1083 * save the ip address of the server that sent the answer.
1084 * Timeout if no response is received.
1085 */
1086 int
1087 revarpwhoarewe(ifp, serv_in, clnt_in)
1088 struct ifnet *ifp;
1089 struct in_addr *serv_in;
1090 struct in_addr *clnt_in;
1091 {
1092 int result, count = 20;
1093
1094 myip_initialized = 0;
1095 myip_ifp = ifp;
1096
1097 revarp_in_progress = 1;
1098 while (count--) {
1099 revarprequest(ifp);
1100 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
1101 if (result != EWOULDBLOCK)
1102 break;
1103 }
1104 revarp_in_progress = 0;
1105
1106 if (!myip_initialized)
1107 return ENETUNREACH;
1108
1109 bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
1110 bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
1111 return 0;
1112 }
1113
1114
1115
1117 #ifdef DDB
1118
1119 #include <machine/db_machdep.h>
1120 #include <ddb/db_interface.h>
1121 #include <ddb/db_output.h>
1122 static void
1123 db_print_sa(sa)
1124 struct sockaddr *sa;
1125 {
1126 int len;
1127 u_char *p;
1128
1129 if (sa == 0) {
1130 db_printf("[NULL]");
1131 return;
1132 }
1133
1134 p = (u_char*)sa;
1135 len = sa->sa_len;
1136 db_printf("[");
1137 while (len > 0) {
1138 db_printf("%d", *p);
1139 p++; len--;
1140 if (len) db_printf(",");
1141 }
1142 db_printf("]\n");
1143 }
1144 static void
1145 db_print_ifa(ifa)
1146 struct ifaddr *ifa;
1147 {
1148 if (ifa == 0)
1149 return;
1150 db_printf(" ifa_addr=");
1151 db_print_sa(ifa->ifa_addr);
1152 db_printf(" ifa_dsta=");
1153 db_print_sa(ifa->ifa_dstaddr);
1154 db_printf(" ifa_mask=");
1155 db_print_sa(ifa->ifa_netmask);
1156 db_printf(" flags=0x%x,refcnt=%d,metric=%d\n",
1157 ifa->ifa_flags,
1158 ifa->ifa_refcnt,
1159 ifa->ifa_metric);
1160 }
1161 static void
1162 db_print_llinfo(li)
1163 caddr_t li;
1164 {
1165 struct llinfo_arp *la;
1166
1167 if (li == 0)
1168 return;
1169 la = (struct llinfo_arp *)li;
1170 db_printf(" la_rt=%p la_hold=%p, la_asked=0x%lx\n",
1171 la->la_rt, la->la_hold, la->la_asked);
1172 }
1173 /*
1174 * Function to pass to rn_walktree().
1175 * Return non-zero error to abort walk.
1176 */
1177 static int
1178 db_show_radix_node(rn, w)
1179 struct radix_node *rn;
1180 void *w;
1181 {
1182 struct rtentry *rt = (struct rtentry *)rn;
1183
1184 db_printf("rtentry=%p", rt);
1185
1186 db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
1187 rt->rt_flags, rt->rt_refcnt,
1188 rt->rt_use, rt->rt_expire);
1189
1190 db_printf(" key="); db_print_sa(rt_key(rt));
1191 db_printf(" mask="); db_print_sa(rt_mask(rt));
1192 db_printf(" gw="); db_print_sa(rt->rt_gateway);
1193
1194 db_printf(" ifp=%p ", rt->rt_ifp);
1195 if (rt->rt_ifp)
1196 db_printf("(%s)", rt->rt_ifp->if_xname);
1197 else
1198 db_printf("(NULL)");
1199
1200 db_printf(" ifa=%p\n", rt->rt_ifa);
1201 db_print_ifa(rt->rt_ifa);
1202
1203 db_printf(" genmask="); db_print_sa(rt->rt_genmask);
1204
1205 db_printf(" gwroute=%p llinfo=%p\n",
1206 rt->rt_gwroute, rt->rt_llinfo);
1207 db_print_llinfo(rt->rt_llinfo);
1208
1209 return (0);
1210 }
1211 /*
1212 * Function to print all the route trees.
1213 * Use this from ddb: "call db_show_arptab"
1214 */
1215 int
1216 db_show_arptab()
1217 {
1218 struct radix_node_head *rnh;
1219 rnh = rt_tables[AF_INET];
1220 db_printf("Route tree for AF_INET\n");
1221 if (rnh == NULL) {
1222 db_printf(" (not initialized)\n");
1223 return (0);
1224 }
1225 rn_walktree(rnh, db_show_radix_node, NULL);
1226 return (0);
1227 }
1228 #endif
1229 #endif /* INET */
1230
1231