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