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