if_arp.c revision 1.35 1 /* $NetBSD: if_arp.c,v 1.35 1997/03/15 18:12:31 is Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
36 */
37
38 /*
39 * Ethernet address resolution protocol.
40 * TODO:
41 * add "inuse/lock" bit (or ref. count) along with valid bit
42 */
43
44 #ifdef INET
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/socket.h>
51 #include <sys/time.h>
52 #include <sys/kernel.h>
53 #include <sys/errno.h>
54 #include <sys/ioctl.h>
55 #include <sys/syslog.h>
56 #include <sys/proc.h>
57
58 #include <net/ethertypes.h>
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/route.h>
62
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
67 #include <netinet/if_inarp.h>
68
69 #define SIN(s) ((struct sockaddr_in *)s)
70 #define SDL(s) ((struct sockaddr_dl *)s)
71 #define SRP(s) ((struct sockaddr_inarp *)s)
72
73 /*
74 * ARP trailer negotiation. Trailer protocol is not IP specific,
75 * but ARP request/response use IP addresses.
76 */
77 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
78
79 /* timer values */
80 int arpt_prune = (5*60*1); /* walk list every 5 minutes */
81 int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
82 int arpt_down = 20; /* once declared down, don't send for 20 secs */
83 #define rt_expire rt_rmx.rmx_expire
84
85 static void arprequest __P((struct ifnet *,
86 struct in_addr *, struct in_addr *, u_int8_t *));
87 static void arptfree __P((struct llinfo_arp *));
88 static void arptimer __P((void *));
89 static struct llinfo_arp *arplookup __P((struct in_addr *, int, int));
90 static void in_arpinput __P((struct mbuf *));
91
92 extern struct ifnet loif;
93 LIST_HEAD(, llinfo_arp) llinfo_arp;
94 struct ifqueue arpintrq = {0, 0, 0, 50};
95 int arp_inuse, arp_allocated, arp_intimer;
96 int arp_maxtries = 5;
97 int useloopback = 1; /* use loopback interface for local traffic */
98 int arpinit_done = 0;
99
100 /* revarp state */
101 static struct in_addr myip, srv_ip;
102 static int myip_initialized = 0;
103 static int revarp_in_progress = 0;
104 static struct ifnet *myip_ifp = NULL;
105
106 #ifdef DDB
107 static void db_print_sa __P((struct sockaddr *));
108 static void db_print_ifa __P((struct ifaddr *));
109 static void db_print_llinfo __P((caddr_t));
110 static int db_show_radix_node __P((struct radix_node *, void *));
111 #endif
112
113 /*
114 * this should be elsewhere.
115 */
116
117 static char *
118 lla_snprintf __P((u_int8_t *, int));
119
120 static char *
121 lla_snprintf(adrp, len)
122 u_int8_t *adrp;
123 int len;
124 {
125 static char buf[16*3];
126 static const char hexdigits[] = {
127 '0','1','2','3','4','5','6','7',
128 '8','9','a','b','c','d','e','f'
129 };
130
131 int i;
132 char *p;
133
134 p = buf;
135
136 *p++ = hexdigits[(*adrp)>>4];
137 *p++ = hexdigits[(*adrp++)&0xf];
138
139 for (i=1; i<len && i<16; i++) {
140 *p++ = ':';
141 *p++ = hexdigits[(*adrp)>>4];
142 *p++ = hexdigits[(*adrp++)&0xf];
143 }
144
145 *p = 0;
146 return buf;
147 }
148
149 /*
150 * Timeout routine. Age arp_tab entries periodically.
151 */
152 /* ARGSUSED */
153 static void
154 arptimer(arg)
155 void *arg;
156 {
157 int s;
158 register struct llinfo_arp *la, *nla;
159
160 s = splsoftnet();
161 timeout(arptimer, NULL, arpt_prune * hz);
162 for (la = llinfo_arp.lh_first; la != 0; la = nla) {
163 register struct rtentry *rt = la->la_rt;
164
165 nla = la->la_list.le_next;
166 if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
167 arptfree(la); /* timer has expired; clear */
168 }
169 splx(s);
170 }
171
172 /*
173 * Parallel to llc_rtrequest.
174 */
175 void
176 arp_rtrequest(req, rt, sa)
177 int req;
178 register struct rtentry *rt;
179 struct sockaddr *sa;
180 {
181 register struct sockaddr *gate = rt->rt_gateway;
182 register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
183 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
184
185 if (!arpinit_done) {
186 arpinit_done = 1;
187 /*
188 * We generate expiration times from time.tv_sec
189 * so avoid accidently creating permanent routes.
190 */
191 if (time.tv_sec == 0) {
192 time.tv_sec++;
193 }
194 timeout(arptimer, (caddr_t)0, hz);
195 }
196 if (rt->rt_flags & RTF_GATEWAY)
197 return;
198 switch (req) {
199
200 case RTM_ADD:
201 /*
202 * XXX: If this is a manually added route to interface
203 * such as older version of routed or gated might provide,
204 * restore cloning bit.
205 */
206 if ((rt->rt_flags & RTF_HOST) == 0 &&
207 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
208 rt->rt_flags |= RTF_CLONING;
209 if (rt->rt_flags & RTF_CLONING) {
210 /*
211 * Case 1: This route should come from a route to iface.
212 */
213 rt_setgate(rt, rt_key(rt),
214 (struct sockaddr *)&null_sdl);
215 gate = rt->rt_gateway;
216 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
217 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
218 /*
219 * Give this route an expiration time, even though
220 * it's a "permanent" route, so that routes cloned
221 * from it do not need their expiration time set.
222 */
223 rt->rt_expire = time.tv_sec;
224 break;
225 }
226 /* Announce a new entry if requested. */
227 if (rt->rt_flags & RTF_ANNOUNCE)
228 arprequest(rt->rt_ifp,
229 &SIN(rt_key(rt))->sin_addr,
230 &SIN(rt_key(rt))->sin_addr,
231 (u_char *)LLADDR(SDL(gate)));
232 /*FALLTHROUGH*/
233 case RTM_RESOLVE:
234 if (gate->sa_family != AF_LINK ||
235 gate->sa_len < sizeof(null_sdl)) {
236 log(LOG_DEBUG, "arp_rtrequest: bad gateway value");
237 break;
238 }
239 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
240 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
241 if (la != 0)
242 break; /* This happens on a route change */
243 /*
244 * Case 2: This route may come from cloning, or a manual route
245 * add with a LL address.
246 */
247 R_Malloc(la, struct llinfo_arp *, sizeof(*la));
248 rt->rt_llinfo = (caddr_t)la;
249 if (la == 0) {
250 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
251 break;
252 }
253 arp_inuse++, arp_allocated++;
254 Bzero(la, sizeof(*la));
255 la->la_rt = rt;
256 rt->rt_flags |= RTF_LLINFO;
257 LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
258 if (in_hosteq(SIN(rt_key(rt))->sin_addr,
259 (IA_SIN(rt->rt_ifa))->sin_addr)) {
260 /*
261 * This test used to be
262 * if (loif.if_flags & IFF_UP)
263 * It allowed local traffic to be forced through
264 * the hardware by configuring the loopback down.
265 * However, it causes problems during network
266 * configuration for boards that can't receive
267 * packets they send. It is now necessary to clear
268 * "useloopback" and remove the route to force
269 * traffic out to the hardware.
270 */
271 rt->rt_expire = 0;
272 Bcopy(LLADDR(rt->rt_ifp->if_sadl),
273 LLADDR(SDL(gate)),
274 SDL(gate)->sdl_alen =
275 rt->rt_ifp->if_data.ifi_addrlen);
276 if (useloopback)
277 rt->rt_ifp = &loif;
278 }
279 break;
280
281 case RTM_DELETE:
282 if (la == 0)
283 break;
284 arp_inuse--;
285 LIST_REMOVE(la, la_list);
286 rt->rt_llinfo = 0;
287 rt->rt_flags &= ~RTF_LLINFO;
288 if (la->la_hold)
289 m_freem(la->la_hold);
290 Free((caddr_t)la);
291 }
292 }
293
294 /*
295 * Broadcast an ARP request. Caller specifies:
296 * - arp header source ip address
297 * - arp header target ip address
298 * - arp header source ethernet address
299 */
300 static void
301 arprequest(ifp, sip, tip, enaddr)
302 register struct ifnet *ifp;
303 register struct in_addr *sip, *tip;
304 register u_int8_t *enaddr;
305 {
306 register struct mbuf *m;
307 struct arphdr *ah;
308 struct sockaddr sa;
309
310 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
311 return;
312 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
313 2*ifp->if_data.ifi_addrlen;
314 m->m_pkthdr.len = m->m_len;
315 MH_ALIGN(m, m->m_len);
316 ah = mtod(m, struct arphdr *);
317 bzero((caddr_t)ah, m->m_len);
318 ah->ar_pro = htons(ETHERTYPE_IP);
319 ah->ar_hln = ifp->if_data.ifi_addrlen; /* hardware address length */
320 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
321 ah->ar_op = htons(ARPOP_REQUEST);
322 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
323 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
324 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
325 sa.sa_family = AF_ARP;
326 sa.sa_len = 2;
327 m->m_flags |= M_BCAST;
328 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
329 }
330
331 /*
332 * Resolve an IP address into an ethernet address. If success,
333 * desten is filled in. If there is no entry in arptab,
334 * set one up and broadcast a request for the IP address.
335 * Hold onto this mbuf and resend it once the address
336 * is finally resolved. A return value of 1 indicates
337 * that desten has been filled in and the packet should be sent
338 * normally; a 0 return indicates that the packet has been
339 * taken over here, either now or for later transmission.
340 */
341 int
342 arpresolve(ifp, rt, m, dst, desten)
343 register struct ifnet *ifp;
344 register struct rtentry *rt;
345 struct mbuf *m;
346 register struct sockaddr *dst;
347 register u_char *desten;
348 {
349 register struct llinfo_arp *la;
350 struct sockaddr_dl *sdl;
351
352 if (rt)
353 la = (struct llinfo_arp *)rt->rt_llinfo;
354 else {
355 if ((la = arplookup(&SIN(dst)->sin_addr, 1, 0)) != NULL)
356 rt = la->la_rt;
357 }
358 if (la == 0 || rt == 0) {
359 log(LOG_DEBUG, "arpresolve: can't allocate llinfo");
360 m_freem(m);
361 return (0);
362 }
363 sdl = SDL(rt->rt_gateway);
364 /*
365 * Check the address family and length is valid, the address
366 * is resolved; otherwise, try to resolve.
367 */
368 if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
369 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
370 bcopy(LLADDR(sdl), desten,
371 min(sdl->sdl_alen, ifp->if_data.ifi_addrlen));
372 return 1;
373 }
374 /*
375 * There is an arptab entry, but no ethernet address
376 * response yet. Replace the held mbuf with this
377 * latest one.
378 */
379 if (la->la_hold)
380 m_freem(la->la_hold);
381 la->la_hold = m;
382 /*
383 * Re-send the ARP request when appropriate.
384 */
385 #ifdef DIAGNOSTIC
386 if (rt->rt_expire == 0) {
387 /* This should never happen. (Should it? -gwr) */
388 printf("arpresolve: unresolved and rt_expire == 0\n");
389 /* Set expiration time to now (expired). */
390 rt->rt_expire = time.tv_sec;
391 }
392 #endif
393 if (rt->rt_expire) {
394 rt->rt_flags &= ~RTF_REJECT;
395 if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
396 rt->rt_expire = time.tv_sec;
397 if (la->la_asked++ < arp_maxtries)
398 arprequest(ifp,
399 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
400 &SIN(dst)->sin_addr,
401 LLADDR(ifp->if_sadl));
402 else {
403 rt->rt_flags |= RTF_REJECT;
404 rt->rt_expire += arpt_down;
405 la->la_asked = 0;
406 }
407 }
408 }
409 return (0);
410 }
411
412 /*
413 * Common length and type checks are done here,
414 * then the protocol-specific routine is called.
415 */
416 void
417 arpintr()
418 {
419 register struct mbuf *m;
420 register struct arphdr *ar;
421 int s;
422
423 while (arpintrq.ifq_head) {
424 s = splimp();
425 IF_DEQUEUE(&arpintrq, m);
426 splx(s);
427 if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
428 panic("arpintr");
429 if (m->m_len >= sizeof(struct arphdr) &&
430 (ar = mtod(m, struct arphdr *)) &&
431 /* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
432 m->m_len >=
433 sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
434 switch (ntohs(ar->ar_pro)) {
435
436 case ETHERTYPE_IP:
437 case ETHERTYPE_IPTRAILERS:
438 in_arpinput(m);
439 continue;
440 }
441 m_freem(m);
442 }
443 }
444
445 /*
446 * ARP for Internet protocols on 10 Mb/s Ethernet.
447 * Algorithm is that given in RFC 826.
448 * In addition, a sanity check is performed on the sender
449 * protocol address, to catch impersonators.
450 * We no longer handle negotiations for use of trailer protocol:
451 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
452 * along with IP replies if we wanted trailers sent to us,
453 * and also sent them in response to IP replies.
454 * This allowed either end to announce the desire to receive
455 * trailer packets.
456 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
457 * but formerly didn't normally send requests.
458 */
459 static void
460 in_arpinput(m)
461 struct mbuf *m;
462 {
463 struct arphdr *ah;
464 register struct ifnet *ifp = m->m_pkthdr.rcvif;
465 register struct llinfo_arp *la = 0;
466 register struct rtentry *rt;
467 struct in_ifaddr *ia, *maybe_ia = 0;
468 struct sockaddr_dl *sdl;
469 struct sockaddr sa;
470 struct in_addr isaddr, itaddr, myaddr;
471 int op;
472
473 ah = mtod(m, struct arphdr *);
474 op = ntohs(ah->ar_op);
475 bcopy((caddr_t)ar_spa(ah), (caddr_t)&isaddr, sizeof (isaddr));
476 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&itaddr, sizeof (itaddr));
477 for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
478 if (ia->ia_ifp == ifp) {
479 maybe_ia = ia;
480 if (in_hosteq(itaddr, ia->ia_addr.sin_addr) ||
481 in_hosteq(isaddr, ia->ia_addr.sin_addr))
482 break;
483 }
484 if (maybe_ia == 0)
485 goto out;
486 myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
487 if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
488 ifp->if_data.ifi_addrlen))
489 goto out; /* it's from me, ignore it. */
490 /*
491 * XXX
492 * if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)etherbroadcastaddr,
493 * sizeof (ea->arp_sha))) {
494 * log(LOG_ERR,
495 * "arp: ether address is broadcast for IP address %x!\n",
496 * ntohl(isaddr.s_addr));
497 * goto out;
498 * }
499 */
500 if (in_hosteq(isaddr, myaddr)) {
501 log(LOG_ERR,
502 "duplicate IP address %08x sent from ethernet address %s\n",
503 ntohl(isaddr.s_addr), lla_snprintf(ar_sha(ah), ah->ar_hln));
504 itaddr = myaddr;
505 goto reply;
506 }
507 la = arplookup(&isaddr, in_hosteq(itaddr, myaddr), 0);
508 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
509 if (sdl->sdl_alen &&
510 bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen))
511 log(LOG_INFO, "arp info overwritten for %08x by %s\n",
512 ntohl(isaddr.s_addr),
513 lla_snprintf(ar_sha(ah), ah->ar_hln));
514 /*
515 * sanity check for the address length.
516 * XXX this does not work for protocols with variable address
517 * length. -is
518 */
519 if (sdl->sdl_alen &&
520 sdl->sdl_alen != ah->ar_hln) {
521 log(LOG_WARNING,
522 "arp from %08x: new addr len %d, was %d",
523 ntohl(isaddr.s_addr), ah->ar_hln, sdl->sdl_alen);
524 }
525 if (ifp->if_data.ifi_addrlen != ah->ar_hln) {
526 log(LOG_WARNING,
527 "arp from %08x: addr len: new %d, i/f %d (ignored)",
528 ntohl(isaddr.s_addr), ah->ar_hln,
529 ifp->if_data.ifi_addrlen);
530 goto reply;
531 }
532 bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
533 sdl->sdl_alen = ah->ar_hln);
534 if (rt->rt_expire)
535 rt->rt_expire = time.tv_sec + arpt_keep;
536 rt->rt_flags &= ~RTF_REJECT;
537 la->la_asked = 0;
538 if (la->la_hold) {
539 (*ifp->if_output)(ifp, la->la_hold,
540 rt_key(rt), rt);
541 la->la_hold = 0;
542 }
543 }
544 reply:
545 if (op != ARPOP_REQUEST) {
546 out:
547 m_freem(m);
548 return;
549 }
550 if (in_hosteq(itaddr, myaddr)) {
551 /* I am the target */
552 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
553 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
554 } else {
555 la = arplookup(&itaddr, 0, SIN_PROXY);
556 if (la == 0)
557 goto out;
558 rt = la->la_rt;
559 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
560 sdl = SDL(rt->rt_gateway);
561 bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
562 }
563
564 bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
565 bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
566 ah->ar_op = htons(ARPOP_REPLY);
567 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
568 sa.sa_family = AF_ARP;
569 sa.sa_len = 2;
570 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
571 return;
572 }
573
574 /*
575 * Free an arp entry.
576 */
577 static void
578 arptfree(la)
579 register struct llinfo_arp *la;
580 {
581 register struct rtentry *rt = la->la_rt;
582 register struct sockaddr_dl *sdl;
583
584 if (rt == 0)
585 panic("arptfree");
586 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
587 sdl->sdl_family == AF_LINK) {
588 sdl->sdl_alen = 0;
589 la->la_asked = 0;
590 rt->rt_flags &= ~RTF_REJECT;
591 return;
592 }
593 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
594 0, (struct rtentry **)0);
595 }
596
597 /*
598 * Lookup or enter a new address in arptab.
599 */
600 static struct llinfo_arp *
601 arplookup(addr, create, proxy)
602 struct in_addr *addr;
603 int create, proxy;
604 {
605 register struct rtentry *rt;
606 static struct sockaddr_inarp sin;
607
608 sin.sin_len = sizeof(sin);
609 sin.sin_family = AF_INET;
610 sin.sin_addr = *addr;
611 sin.sin_other = proxy ? SIN_PROXY : 0;
612 rt = rtalloc1(sintosa(&sin), create);
613 if (rt == 0)
614 return (0);
615 rt->rt_refcnt--;
616 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
617 rt->rt_gateway->sa_family != AF_LINK) {
618 if (create)
619 log(LOG_DEBUG, "arplookup: unable to enter address for %x\n",
620 ntohl(addr->s_addr));
621 return (0);
622 }
623 return ((struct llinfo_arp *)rt->rt_llinfo);
624 }
625
626 int
627 arpioctl(cmd, data)
628 u_long cmd;
629 caddr_t data;
630 {
631
632 return (EOPNOTSUPP);
633 }
634
635 void
636 arp_ifinit(ifp, ifa)
637 struct ifnet *ifp;
638 struct ifaddr *ifa;
639 {
640
641 /* Warn the user if another station has this IP address. */
642 arprequest(ifp,
643 &IA_SIN(ifa)->sin_addr,
644 &IA_SIN(ifa)->sin_addr,
645 LLADDR(ifp->if_sadl));
646 ifa->ifa_rtrequest = arp_rtrequest;
647 ifa->ifa_flags |= RTF_CLONING;
648 }
649
650 /*
651 * Called from 10 Mb/s Ethernet interrupt handlers
652 * when ether packet type ETHERTYPE_REVARP
653 * is received. Common length and type checks are done here,
654 * then the protocol-specific routine is called.
655 */
656 void
657 revarpinput(m)
658 struct mbuf *m;
659 {
660 struct arphdr *ar;
661
662 if (m->m_len < sizeof(struct arphdr))
663 goto out;
664 ar = mtod(m, struct arphdr *);
665 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
666 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
667 goto out;
668 #endif
669 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
670 goto out;
671 switch (ntohs(ar->ar_pro)) {
672
673 case ETHERTYPE_IP:
674 case ETHERTYPE_IPTRAILERS:
675 in_revarpinput(m);
676 return;
677
678 default:
679 break;
680 }
681 out:
682 m_freem(m);
683 }
684
685 /*
686 * RARP for Internet protocols on 10 Mb/s Ethernet.
687 * Algorithm is that given in RFC 903.
688 * We are only using for bootstrap purposes to get an ip address for one of
689 * our interfaces. Thus we support no user-interface.
690 *
691 * Since the contents of the RARP reply are specific to the interface that
692 * sent the request, this code must ensure that they are properly associated.
693 *
694 * Note: also supports ARP via RARP packets, per the RFC.
695 */
696 void
697 in_revarpinput(m)
698 struct mbuf *m;
699 {
700 struct ifnet *ifp;
701 struct arphdr *ah;
702 int op;
703
704 ah = mtod(m, struct arphdr *);
705 op = ntohs(ah->ar_op);
706 switch (op) {
707 case ARPOP_REQUEST:
708 case ARPOP_REPLY: /* per RFC */
709 in_arpinput(m);
710 return;
711 case ARPOP_REVREPLY:
712 break;
713 case ARPOP_REVREQUEST: /* handled by rarpd(8) */
714 default:
715 goto out;
716 }
717 if (!revarp_in_progress)
718 goto out;
719 ifp = m->m_pkthdr.rcvif;
720 if (ifp != myip_ifp) /* !same interface */
721 goto out;
722 if (myip_initialized)
723 goto wake;
724 if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
725 goto out;
726 bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
727 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
728 myip_initialized = 1;
729 wake: /* Do wakeup every time in case it was missed. */
730 wakeup((caddr_t)&myip);
731
732 out:
733 m_freem(m);
734 }
735
736 /*
737 * Send a RARP request for the ip address of the specified interface.
738 * The request should be RFC 903-compliant.
739 */
740 void
741 revarprequest(ifp)
742 struct ifnet *ifp;
743 {
744 struct sockaddr sa;
745 struct mbuf *m;
746 struct arphdr *ah;
747
748 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
749 return;
750 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
751 2*ifp->if_data.ifi_addrlen;
752 m->m_pkthdr.len = m->m_len;
753 MH_ALIGN(m, m->m_len);
754 ah = mtod(m, struct arphdr *);
755 bzero((caddr_t)ah, m->m_len);
756 ah->ar_pro = htons(ETHERTYPE_IP);
757 ah->ar_hln = ifp->if_data.ifi_addrlen; /* hardware address length */
758 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
759 ah->ar_op = htons(ARPOP_REVREQUEST);
760
761 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
762 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln);
763
764 sa.sa_family = AF_ARP;
765 sa.sa_len = 2;
766 m->m_flags |= M_BCAST;
767 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
768
769 }
770
771 /*
772 * RARP for the ip address of the specified interface, but also
773 * save the ip address of the server that sent the answer.
774 * Timeout if no response is received.
775 */
776 int
777 revarpwhoarewe(ifp, serv_in, clnt_in)
778 struct ifnet *ifp;
779 struct in_addr *serv_in;
780 struct in_addr *clnt_in;
781 {
782 int result, count = 20;
783
784 if (myip_initialized)
785 return EIO;
786
787 myip_ifp = ifp;
788 revarp_in_progress = 1;
789 while (count--) {
790 revarprequest(ifp);
791 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
792 if (result != EWOULDBLOCK)
793 break;
794 }
795 revarp_in_progress = 0;
796 if (!myip_initialized)
797 return ENETUNREACH;
798
799 bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
800 bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
801 return 0;
802 }
803
804 /* For compatibility: only saves interface address. */
805 int
806 revarpwhoami(in, ifp)
807 struct in_addr *in;
808 struct ifnet *ifp;
809 {
810 struct in_addr server;
811 return (revarpwhoarewe(ifp, &server, in));
812 }
813
814
816 #ifdef DDB
817
818 #include <machine/db_machdep.h>
819 #include <ddb/db_interface.h>
820 #include <ddb/db_output.h>
821 static void
822 db_print_sa(sa)
823 struct sockaddr *sa;
824 {
825 int len;
826 u_char *p;
827
828 if (sa == 0) {
829 db_printf("[NULL]");
830 return;
831 }
832
833 p = (u_char*)sa;
834 len = sa->sa_len;
835 db_printf("[");
836 while (len > 0) {
837 db_printf("%d", *p);
838 p++; len--;
839 if (len) db_printf(",");
840 }
841 db_printf("]\n");
842 }
843 static void
844 db_print_ifa(ifa)
845 struct ifaddr *ifa;
846 {
847 if (ifa == 0)
848 return;
849 db_printf(" ifa_addr=");
850 db_print_sa(ifa->ifa_addr);
851 db_printf(" ifa_dsta=");
852 db_print_sa(ifa->ifa_dstaddr);
853 db_printf(" ifa_mask=");
854 db_print_sa(ifa->ifa_netmask);
855 db_printf(" flags=0x%x,refcnt=%d,metric=%d\n",
856 ifa->ifa_flags,
857 ifa->ifa_refcnt,
858 ifa->ifa_metric);
859 }
860 static void
861 db_print_llinfo(li)
862 caddr_t li;
863 {
864 struct llinfo_arp *la;
865
866 if (li == 0)
867 return;
868 la = (struct llinfo_arp *)li;
869 db_printf(" la_rt=%p la_hold=%p, la_asked=0x%lx\n",
870 la->la_rt, la->la_hold, la->la_asked);
871 }
872 /*
873 * Function to pass to rn_walktree().
874 * Return non-zero error to abort walk.
875 */
876 static int
877 db_show_radix_node(rn, w)
878 struct radix_node *rn;
879 void *w;
880 {
881 struct rtentry *rt = (struct rtentry *)rn;
882
883 db_printf("rtentry=%p", rt);
884
885 db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
886 rt->rt_flags, rt->rt_refcnt,
887 rt->rt_use, rt->rt_expire);
888
889 db_printf(" key="); db_print_sa(rt_key(rt));
890 db_printf(" mask="); db_print_sa(rt_mask(rt));
891 db_printf(" gw="); db_print_sa(rt->rt_gateway);
892
893 db_printf(" ifp=%p ", rt->rt_ifp);
894 if (rt->rt_ifp)
895 db_printf("(%s)", rt->rt_ifp->if_xname);
896 else
897 db_printf("(NULL)");
898
899 db_printf(" ifa=%p\n", rt->rt_ifa);
900 db_print_ifa(rt->rt_ifa);
901
902 db_printf(" genmask="); db_print_sa(rt->rt_genmask);
903
904 db_printf(" gwroute=%p llinfo=%p\n",
905 rt->rt_gwroute, rt->rt_llinfo);
906 db_print_llinfo(rt->rt_llinfo);
907
908 return (0);
909 }
910 /*
911 * Function to print all the route trees.
912 * Use this from ddb: "call db_show_arptab"
913 */
914 int
915 db_show_arptab()
916 {
917 struct radix_node_head *rnh;
918 rnh = rt_tables[AF_INET];
919 db_printf("Route tree for AF_INET\n");
920 if (rnh == NULL) {
921 db_printf(" (not initialized)\n");
922 return (0);
923 }
924 rn_walktree(rnh, db_show_radix_node, NULL);
925 return (0);
926 }
927 #endif
928 #endif /* INET */
929