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