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