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