if_arp.c revision 1.40 1 /* $NetBSD: if_arp.c,v 1.40 1997/08/29 16:02:41 gwr 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 #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, *maybe_ia = 0;
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 either the source or destination IP address is zero, then
483 * the packet is bogus and we can just ignore it. If we try to
484 * process such bogus packets normally, they cause odd problems,
485 * especially when we are using IP address zero (diskless boot).
486 */
487 if (in_nullhost(isaddr) || in_nullhost(itaddr)) {
488 /* On some networks, this happens a lot! (stupid PCs...) */
489 log(LOG_DEBUG, "arp: zero IP addr from link address %s\n",
490 lla_snprintf(ar_sha(ah), ah->ar_hln));
491 goto out;
492 }
493
494 /* Search for a matching interface address. */
495 for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
496 if (ia->ia_ifp == ifp) {
497 maybe_ia = ia;
498 if (in_hosteq(itaddr, ia->ia_addr.sin_addr) ||
499 in_hosteq(isaddr, ia->ia_addr.sin_addr))
500 break;
501 }
502 if (maybe_ia == 0)
503 goto out;
504
505 myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
506 if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
507 ifp->if_data.ifi_addrlen))
508 goto out; /* it's from me, ignore it. */
509 /*
510 * XXX
511 * if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)etherbroadcastaddr,
512 * sizeof (ea->arp_sha))) {
513 * log(LOG_ERR,
514 * "arp: ether address is broadcast for IP address %x!\n",
515 * ntohl(isaddr.s_addr));
516 * goto out;
517 * }
518 */
519 if (in_hosteq(isaddr, myaddr)) {
520 log(LOG_ERR,
521 "duplicate IP address %08x sent from link address %s\n",
522 ntohl(isaddr.s_addr), lla_snprintf(ar_sha(ah), ah->ar_hln));
523 itaddr = myaddr;
524 goto reply;
525 }
526 la = arplookup(&isaddr, in_hosteq(itaddr, myaddr), 0);
527 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
528 if (sdl->sdl_alen &&
529 bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen))
530 log(LOG_INFO, "arp info overwritten for %08x by %s\n",
531 ntohl(isaddr.s_addr),
532 lla_snprintf(ar_sha(ah), ah->ar_hln));
533 /*
534 * sanity check for the address length.
535 * XXX this does not work for protocols with variable address
536 * length. -is
537 */
538 if (sdl->sdl_alen &&
539 sdl->sdl_alen != ah->ar_hln) {
540 log(LOG_WARNING,
541 "arp from %08x: new addr len %d, was %d",
542 ntohl(isaddr.s_addr), ah->ar_hln, sdl->sdl_alen);
543 }
544 if (ifp->if_data.ifi_addrlen != ah->ar_hln) {
545 log(LOG_WARNING,
546 "arp from %08x: addr len: new %d, i/f %d (ignored)",
547 ntohl(isaddr.s_addr), ah->ar_hln,
548 ifp->if_data.ifi_addrlen);
549 goto reply;
550 }
551 bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
552 sdl->sdl_alen = ah->ar_hln);
553 if (rt->rt_expire)
554 rt->rt_expire = time.tv_sec + arpt_keep;
555 rt->rt_flags &= ~RTF_REJECT;
556 la->la_asked = 0;
557 if (la->la_hold) {
558 (*ifp->if_output)(ifp, la->la_hold,
559 rt_key(rt), rt);
560 la->la_hold = 0;
561 }
562 }
563 reply:
564 if (op != ARPOP_REQUEST) {
565 out:
566 m_freem(m);
567 return;
568 }
569 if (in_hosteq(itaddr, myaddr)) {
570 /* I am the target */
571 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
572 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
573 } else {
574 la = arplookup(&itaddr, 0, SIN_PROXY);
575 if (la == 0)
576 goto out;
577 rt = la->la_rt;
578 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
579 sdl = SDL(rt->rt_gateway);
580 bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
581 }
582
583 bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
584 bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
585 ah->ar_op = htons(ARPOP_REPLY);
586 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
587 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
588 sa.sa_family = AF_ARP;
589 sa.sa_len = 2;
590 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
591 return;
592 }
593
594 /*
595 * Free an arp entry.
596 */
597 static void
598 arptfree(la)
599 register struct llinfo_arp *la;
600 {
601 register struct rtentry *rt = la->la_rt;
602 register struct sockaddr_dl *sdl;
603
604 if (rt == 0)
605 panic("arptfree");
606 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
607 sdl->sdl_family == AF_LINK) {
608 sdl->sdl_alen = 0;
609 la->la_asked = 0;
610 rt->rt_flags &= ~RTF_REJECT;
611 return;
612 }
613 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
614 0, (struct rtentry **)0);
615 }
616
617 /*
618 * Lookup or enter a new address in arptab.
619 */
620 static struct llinfo_arp *
621 arplookup(addr, create, proxy)
622 struct in_addr *addr;
623 int create, proxy;
624 {
625 register struct rtentry *rt;
626 static struct sockaddr_inarp sin;
627
628 sin.sin_len = sizeof(sin);
629 sin.sin_family = AF_INET;
630 sin.sin_addr = *addr;
631 sin.sin_other = proxy ? SIN_PROXY : 0;
632 rt = rtalloc1(sintosa(&sin), create);
633 if (rt == 0)
634 return (0);
635 rt->rt_refcnt--;
636 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
637 rt->rt_gateway->sa_family != AF_LINK) {
638 if (create)
639 log(LOG_DEBUG, "arplookup: unable to enter address for %x\n",
640 ntohl(addr->s_addr));
641 return (0);
642 }
643 return ((struct llinfo_arp *)rt->rt_llinfo);
644 }
645
646 int
647 arpioctl(cmd, data)
648 u_long cmd;
649 caddr_t data;
650 {
651
652 return (EOPNOTSUPP);
653 }
654
655 void
656 arp_ifinit(ifp, ifa)
657 struct ifnet *ifp;
658 struct ifaddr *ifa;
659 {
660 struct in_addr *ip;
661
662 /*
663 * Warn the user if another station has this IP address,
664 * but only if the interface IP address is not zero.
665 */
666 ip = &IA_SIN(ifa)->sin_addr;
667 if (!in_nullhost(*ip))
668 arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl));
669
670 ifa->ifa_rtrequest = arp_rtrequest;
671 ifa->ifa_flags |= RTF_CLONING;
672 }
673
674 /*
675 * Called from 10 Mb/s Ethernet interrupt handlers
676 * when ether packet type ETHERTYPE_REVARP
677 * is received. Common length and type checks are done here,
678 * then the protocol-specific routine is called.
679 */
680 void
681 revarpinput(m)
682 struct mbuf *m;
683 {
684 struct arphdr *ar;
685
686 if (m->m_len < sizeof(struct arphdr))
687 goto out;
688 ar = mtod(m, struct arphdr *);
689 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
690 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
691 goto out;
692 #endif
693 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
694 goto out;
695 switch (ntohs(ar->ar_pro)) {
696
697 case ETHERTYPE_IP:
698 case ETHERTYPE_IPTRAILERS:
699 in_revarpinput(m);
700 return;
701
702 default:
703 break;
704 }
705 out:
706 m_freem(m);
707 }
708
709 /*
710 * RARP for Internet protocols on 10 Mb/s Ethernet.
711 * Algorithm is that given in RFC 903.
712 * We are only using for bootstrap purposes to get an ip address for one of
713 * our interfaces. Thus we support no user-interface.
714 *
715 * Since the contents of the RARP reply are specific to the interface that
716 * sent the request, this code must ensure that they are properly associated.
717 *
718 * Note: also supports ARP via RARP packets, per the RFC.
719 */
720 void
721 in_revarpinput(m)
722 struct mbuf *m;
723 {
724 struct ifnet *ifp;
725 struct arphdr *ah;
726 int op;
727
728 ah = mtod(m, struct arphdr *);
729 op = ntohs(ah->ar_op);
730 switch (op) {
731 case ARPOP_REQUEST:
732 case ARPOP_REPLY: /* per RFC */
733 in_arpinput(m);
734 return;
735 case ARPOP_REVREPLY:
736 break;
737 case ARPOP_REVREQUEST: /* handled by rarpd(8) */
738 default:
739 goto out;
740 }
741 if (!revarp_in_progress)
742 goto out;
743 ifp = m->m_pkthdr.rcvif;
744 if (ifp != myip_ifp) /* !same interface */
745 goto out;
746 if (myip_initialized)
747 goto wake;
748 if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
749 goto out;
750 bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
751 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
752 myip_initialized = 1;
753 wake: /* Do wakeup every time in case it was missed. */
754 wakeup((caddr_t)&myip);
755
756 out:
757 m_freem(m);
758 }
759
760 /*
761 * Send a RARP request for the ip address of the specified interface.
762 * The request should be RFC 903-compliant.
763 */
764 void
765 revarprequest(ifp)
766 struct ifnet *ifp;
767 {
768 struct sockaddr sa;
769 struct mbuf *m;
770 struct arphdr *ah;
771
772 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
773 return;
774 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
775 2*ifp->if_data.ifi_addrlen;
776 m->m_pkthdr.len = m->m_len;
777 MH_ALIGN(m, m->m_len);
778 ah = mtod(m, struct arphdr *);
779 bzero((caddr_t)ah, m->m_len);
780 ah->ar_pro = htons(ETHERTYPE_IP);
781 ah->ar_hln = ifp->if_data.ifi_addrlen; /* hardware address length */
782 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
783 ah->ar_op = htons(ARPOP_REVREQUEST);
784
785 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
786 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln);
787
788 sa.sa_family = AF_ARP;
789 sa.sa_len = 2;
790 m->m_flags |= M_BCAST;
791 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
792
793 }
794
795 /*
796 * RARP for the ip address of the specified interface, but also
797 * save the ip address of the server that sent the answer.
798 * Timeout if no response is received.
799 */
800 int
801 revarpwhoarewe(ifp, serv_in, clnt_in)
802 struct ifnet *ifp;
803 struct in_addr *serv_in;
804 struct in_addr *clnt_in;
805 {
806 int result, count = 20;
807
808 if (!myip_initialized) {
809 myip_ifp = ifp;
810 revarp_in_progress = 1;
811 while (count--) {
812 revarprequest(ifp);
813 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
814 if (result != EWOULDBLOCK)
815 break;
816 }
817 revarp_in_progress = 0;
818 }
819 if (!myip_initialized)
820 return ENETUNREACH;
821
822 bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
823 bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
824 return 0;
825 }
826
827 /* For compatibility: only saves interface address. */
828 int
829 revarpwhoami(in, ifp)
830 struct in_addr *in;
831 struct ifnet *ifp;
832 {
833 struct in_addr server;
834 return (revarpwhoarewe(ifp, &server, in));
835 }
836
837
839 #ifdef DDB
840
841 #include <machine/db_machdep.h>
842 #include <ddb/db_interface.h>
843 #include <ddb/db_output.h>
844 static void
845 db_print_sa(sa)
846 struct sockaddr *sa;
847 {
848 int len;
849 u_char *p;
850
851 if (sa == 0) {
852 db_printf("[NULL]");
853 return;
854 }
855
856 p = (u_char*)sa;
857 len = sa->sa_len;
858 db_printf("[");
859 while (len > 0) {
860 db_printf("%d", *p);
861 p++; len--;
862 if (len) db_printf(",");
863 }
864 db_printf("]\n");
865 }
866 static void
867 db_print_ifa(ifa)
868 struct ifaddr *ifa;
869 {
870 if (ifa == 0)
871 return;
872 db_printf(" ifa_addr=");
873 db_print_sa(ifa->ifa_addr);
874 db_printf(" ifa_dsta=");
875 db_print_sa(ifa->ifa_dstaddr);
876 db_printf(" ifa_mask=");
877 db_print_sa(ifa->ifa_netmask);
878 db_printf(" flags=0x%x,refcnt=%d,metric=%d\n",
879 ifa->ifa_flags,
880 ifa->ifa_refcnt,
881 ifa->ifa_metric);
882 }
883 static void
884 db_print_llinfo(li)
885 caddr_t li;
886 {
887 struct llinfo_arp *la;
888
889 if (li == 0)
890 return;
891 la = (struct llinfo_arp *)li;
892 db_printf(" la_rt=%p la_hold=%p, la_asked=0x%lx\n",
893 la->la_rt, la->la_hold, la->la_asked);
894 }
895 /*
896 * Function to pass to rn_walktree().
897 * Return non-zero error to abort walk.
898 */
899 static int
900 db_show_radix_node(rn, w)
901 struct radix_node *rn;
902 void *w;
903 {
904 struct rtentry *rt = (struct rtentry *)rn;
905
906 db_printf("rtentry=%p", rt);
907
908 db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
909 rt->rt_flags, rt->rt_refcnt,
910 rt->rt_use, rt->rt_expire);
911
912 db_printf(" key="); db_print_sa(rt_key(rt));
913 db_printf(" mask="); db_print_sa(rt_mask(rt));
914 db_printf(" gw="); db_print_sa(rt->rt_gateway);
915
916 db_printf(" ifp=%p ", rt->rt_ifp);
917 if (rt->rt_ifp)
918 db_printf("(%s)", rt->rt_ifp->if_xname);
919 else
920 db_printf("(NULL)");
921
922 db_printf(" ifa=%p\n", rt->rt_ifa);
923 db_print_ifa(rt->rt_ifa);
924
925 db_printf(" genmask="); db_print_sa(rt->rt_genmask);
926
927 db_printf(" gwroute=%p llinfo=%p\n",
928 rt->rt_gwroute, rt->rt_llinfo);
929 db_print_llinfo(rt->rt_llinfo);
930
931 return (0);
932 }
933 /*
934 * Function to print all the route trees.
935 * Use this from ddb: "call db_show_arptab"
936 */
937 int
938 db_show_arptab()
939 {
940 struct radix_node_head *rnh;
941 rnh = rt_tables[AF_INET];
942 db_printf("Route tree for AF_INET\n");
943 if (rnh == NULL) {
944 db_printf(" (not initialized)\n");
945 return (0);
946 }
947 rn_walktree(rnh, db_show_radix_node, NULL);
948 return (0);
949 }
950 #endif
951 #endif /* INET */
952