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