if_arp.c revision 1.48 1 /* $NetBSD: if_arp.c,v 1.48 1998/07/02 11:39:56 is 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 }
545
546 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
547 NEXT_IA_WITH_SAME_ADDR(ia);
548
549 if (ia == NULL)
550 goto out;
551
552 myaddr = ia->ia_addr.sin_addr;
553
554 if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
555 ifp->if_data.ifi_addrlen))
556 goto out; /* it's from me, ignore it. */
557
558 if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)ifp->if_broadcastaddr,
559 ifp->if_data.ifi_addrlen)) {
560 log(LOG_ERR,
561 "%s: arp: link address is broadcast for IP address %s!\n",
562 ifp->if_xname, in_fmtaddr(isaddr));
563 goto out;
564 }
565
566 if (in_hosteq(isaddr, myaddr)) {
567 log(LOG_ERR,
568 "duplicate IP address %s sent from link address %s\n",
569 in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
570 itaddr = myaddr;
571 goto reply;
572 }
573 la = arplookup(&isaddr, in_hosteq(itaddr, myaddr), 0);
574 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
575 if (sdl->sdl_alen &&
576 bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen))
577 log(LOG_INFO, "arp info overwritten for %s by %s\n",
578 in_fmtaddr(isaddr),
579 lla_snprintf(ar_sha(ah), ah->ar_hln));
580 /*
581 * sanity check for the address length.
582 * XXX this does not work for protocols with variable address
583 * length. -is
584 */
585 if (sdl->sdl_alen &&
586 sdl->sdl_alen != ah->ar_hln) {
587 log(LOG_WARNING,
588 "arp from %s: new addr len %d, was %d",
589 in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
590 }
591 if (ifp->if_data.ifi_addrlen != ah->ar_hln) {
592 log(LOG_WARNING,
593 "arp from %s: addr len: new %d, i/f %d (ignored)",
594 in_fmtaddr(isaddr), ah->ar_hln,
595 ifp->if_data.ifi_addrlen);
596 goto reply;
597 }
598 bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
599 sdl->sdl_alen = ah->ar_hln);
600 if (rt->rt_expire)
601 rt->rt_expire = time.tv_sec + arpt_keep;
602 rt->rt_flags &= ~RTF_REJECT;
603 la->la_asked = 0;
604 if (la->la_hold) {
605 (*ifp->if_output)(ifp, la->la_hold,
606 rt_key(rt), rt);
607 la->la_hold = 0;
608 }
609 }
610 reply:
611 if (op != ARPOP_REQUEST) {
612 out:
613 m_freem(m);
614 return;
615 }
616 if (in_hosteq(itaddr, myaddr)) {
617 /* I am the target */
618 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
619 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
620 } else {
621 la = arplookup(&itaddr, 0, SIN_PROXY);
622 if (la == 0)
623 goto out;
624 rt = la->la_rt;
625 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
626 sdl = SDL(rt->rt_gateway);
627 bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
628 }
629
630 bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
631 bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
632 ah->ar_op = htons(ARPOP_REPLY);
633 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
634 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
635 sa.sa_family = AF_ARP;
636 sa.sa_len = 2;
637 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
638 return;
639 }
640
641 /*
642 * Free an arp entry.
643 */
644 static void
645 arptfree(la)
646 register struct llinfo_arp *la;
647 {
648 register struct rtentry *rt = la->la_rt;
649 register struct sockaddr_dl *sdl;
650
651 if (rt == 0)
652 panic("arptfree");
653 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
654 sdl->sdl_family == AF_LINK) {
655 sdl->sdl_alen = 0;
656 la->la_asked = 0;
657 rt->rt_flags &= ~RTF_REJECT;
658 return;
659 }
660 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
661 0, (struct rtentry **)0);
662 }
663
664 /*
665 * Lookup or enter a new address in arptab.
666 */
667 static struct llinfo_arp *
668 arplookup(addr, create, proxy)
669 struct in_addr *addr;
670 int create, proxy;
671 {
672 register struct rtentry *rt;
673 static struct sockaddr_inarp sin;
674
675 sin.sin_len = sizeof(sin);
676 sin.sin_family = AF_INET;
677 sin.sin_addr = *addr;
678 sin.sin_other = proxy ? SIN_PROXY : 0;
679 rt = rtalloc1(sintosa(&sin), create);
680 if (rt == 0)
681 return (0);
682 rt->rt_refcnt--;
683 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
684 rt->rt_gateway->sa_family != AF_LINK) {
685 if (create)
686 log(LOG_DEBUG, "arplookup: unable to enter address for %s\n",
687 in_fmtaddr(*addr));
688 return (0);
689 }
690 return ((struct llinfo_arp *)rt->rt_llinfo);
691 }
692
693 int
694 arpioctl(cmd, data)
695 u_long cmd;
696 caddr_t data;
697 {
698
699 return (EOPNOTSUPP);
700 }
701
702 void
703 arp_ifinit(ifp, ifa)
704 struct ifnet *ifp;
705 struct ifaddr *ifa;
706 {
707 struct in_addr *ip;
708
709 /*
710 * Warn the user if another station has this IP address,
711 * but only if the interface IP address is not zero.
712 */
713 ip = &IA_SIN(ifa)->sin_addr;
714 if (!in_nullhost(*ip))
715 arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl));
716
717 ifa->ifa_rtrequest = arp_rtrequest;
718 ifa->ifa_flags |= RTF_CLONING;
719 }
720
721 /*
722 * Called from 10 Mb/s Ethernet interrupt handlers
723 * when ether packet type ETHERTYPE_REVARP
724 * is received. Common length and type checks are done here,
725 * then the protocol-specific routine is called.
726 */
727 void
728 revarpinput(m)
729 struct mbuf *m;
730 {
731 struct arphdr *ar;
732
733 if (m->m_len < sizeof(struct arphdr))
734 goto out;
735 ar = mtod(m, struct arphdr *);
736 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
737 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
738 goto out;
739 #endif
740 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
741 goto out;
742 switch (ntohs(ar->ar_pro)) {
743
744 case ETHERTYPE_IP:
745 case ETHERTYPE_IPTRAILERS:
746 in_revarpinput(m);
747 return;
748
749 default:
750 break;
751 }
752 out:
753 m_freem(m);
754 }
755
756 /*
757 * RARP for Internet protocols on 10 Mb/s Ethernet.
758 * Algorithm is that given in RFC 903.
759 * We are only using for bootstrap purposes to get an ip address for one of
760 * our interfaces. Thus we support no user-interface.
761 *
762 * Since the contents of the RARP reply are specific to the interface that
763 * sent the request, this code must ensure that they are properly associated.
764 *
765 * Note: also supports ARP via RARP packets, per the RFC.
766 */
767 void
768 in_revarpinput(m)
769 struct mbuf *m;
770 {
771 struct ifnet *ifp;
772 struct arphdr *ah;
773 int op;
774
775 ah = mtod(m, struct arphdr *);
776 op = ntohs(ah->ar_op);
777 switch (op) {
778 case ARPOP_REQUEST:
779 case ARPOP_REPLY: /* per RFC */
780 in_arpinput(m);
781 return;
782 case ARPOP_REVREPLY:
783 break;
784 case ARPOP_REVREQUEST: /* handled by rarpd(8) */
785 default:
786 goto out;
787 }
788 if (!revarp_in_progress)
789 goto out;
790 ifp = m->m_pkthdr.rcvif;
791 if (ifp != myip_ifp) /* !same interface */
792 goto out;
793 if (myip_initialized)
794 goto wake;
795 if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
796 goto out;
797 bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
798 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
799 myip_initialized = 1;
800 wake: /* Do wakeup every time in case it was missed. */
801 wakeup((caddr_t)&myip);
802
803 out:
804 m_freem(m);
805 }
806
807 /*
808 * Send a RARP request for the ip address of the specified interface.
809 * The request should be RFC 903-compliant.
810 */
811 void
812 revarprequest(ifp)
813 struct ifnet *ifp;
814 {
815 struct sockaddr sa;
816 struct mbuf *m;
817 struct arphdr *ah;
818
819 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
820 return;
821 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
822 2*ifp->if_data.ifi_addrlen;
823 m->m_pkthdr.len = m->m_len;
824 MH_ALIGN(m, m->m_len);
825 ah = mtod(m, struct arphdr *);
826 bzero((caddr_t)ah, m->m_len);
827 ah->ar_pro = htons(ETHERTYPE_IP);
828 ah->ar_hln = ifp->if_data.ifi_addrlen; /* hardware address length */
829 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
830 ah->ar_op = htons(ARPOP_REVREQUEST);
831
832 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
833 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln);
834
835 sa.sa_family = AF_ARP;
836 sa.sa_len = 2;
837 m->m_flags |= M_BCAST;
838 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
839
840 }
841
842 /*
843 * RARP for the ip address of the specified interface, but also
844 * save the ip address of the server that sent the answer.
845 * Timeout if no response is received.
846 */
847 int
848 revarpwhoarewe(ifp, serv_in, clnt_in)
849 struct ifnet *ifp;
850 struct in_addr *serv_in;
851 struct in_addr *clnt_in;
852 {
853 int result, count = 20;
854
855 if (!myip_initialized) {
856 myip_ifp = ifp;
857 revarp_in_progress = 1;
858 while (count--) {
859 revarprequest(ifp);
860 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
861 if (result != EWOULDBLOCK)
862 break;
863 }
864 revarp_in_progress = 0;
865 }
866 if (!myip_initialized)
867 return ENETUNREACH;
868
869 bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
870 bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
871 return 0;
872 }
873
874 /* For compatibility: only saves interface address. */
875 int
876 revarpwhoami(in, ifp)
877 struct in_addr *in;
878 struct ifnet *ifp;
879 {
880 struct in_addr server;
881 return (revarpwhoarewe(ifp, &server, in));
882 }
883
884
886 #ifdef DDB
887
888 #include <machine/db_machdep.h>
889 #include <ddb/db_interface.h>
890 #include <ddb/db_output.h>
891 static void
892 db_print_sa(sa)
893 struct sockaddr *sa;
894 {
895 int len;
896 u_char *p;
897
898 if (sa == 0) {
899 db_printf("[NULL]");
900 return;
901 }
902
903 p = (u_char*)sa;
904 len = sa->sa_len;
905 db_printf("[");
906 while (len > 0) {
907 db_printf("%d", *p);
908 p++; len--;
909 if (len) db_printf(",");
910 }
911 db_printf("]\n");
912 }
913 static void
914 db_print_ifa(ifa)
915 struct ifaddr *ifa;
916 {
917 if (ifa == 0)
918 return;
919 db_printf(" ifa_addr=");
920 db_print_sa(ifa->ifa_addr);
921 db_printf(" ifa_dsta=");
922 db_print_sa(ifa->ifa_dstaddr);
923 db_printf(" ifa_mask=");
924 db_print_sa(ifa->ifa_netmask);
925 db_printf(" flags=0x%x,refcnt=%d,metric=%d\n",
926 ifa->ifa_flags,
927 ifa->ifa_refcnt,
928 ifa->ifa_metric);
929 }
930 static void
931 db_print_llinfo(li)
932 caddr_t li;
933 {
934 struct llinfo_arp *la;
935
936 if (li == 0)
937 return;
938 la = (struct llinfo_arp *)li;
939 db_printf(" la_rt=%p la_hold=%p, la_asked=0x%lx\n",
940 la->la_rt, la->la_hold, la->la_asked);
941 }
942 /*
943 * Function to pass to rn_walktree().
944 * Return non-zero error to abort walk.
945 */
946 static int
947 db_show_radix_node(rn, w)
948 struct radix_node *rn;
949 void *w;
950 {
951 struct rtentry *rt = (struct rtentry *)rn;
952
953 db_printf("rtentry=%p", rt);
954
955 db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
956 rt->rt_flags, rt->rt_refcnt,
957 rt->rt_use, rt->rt_expire);
958
959 db_printf(" key="); db_print_sa(rt_key(rt));
960 db_printf(" mask="); db_print_sa(rt_mask(rt));
961 db_printf(" gw="); db_print_sa(rt->rt_gateway);
962
963 db_printf(" ifp=%p ", rt->rt_ifp);
964 if (rt->rt_ifp)
965 db_printf("(%s)", rt->rt_ifp->if_xname);
966 else
967 db_printf("(NULL)");
968
969 db_printf(" ifa=%p\n", rt->rt_ifa);
970 db_print_ifa(rt->rt_ifa);
971
972 db_printf(" genmask="); db_print_sa(rt->rt_genmask);
973
974 db_printf(" gwroute=%p llinfo=%p\n",
975 rt->rt_gwroute, rt->rt_llinfo);
976 db_print_llinfo(rt->rt_llinfo);
977
978 return (0);
979 }
980 /*
981 * Function to print all the route trees.
982 * Use this from ddb: "call db_show_arptab"
983 */
984 int
985 db_show_arptab()
986 {
987 struct radix_node_head *rnh;
988 rnh = rt_tables[AF_INET];
989 db_printf("Route tree for AF_INET\n");
990 if (rnh == NULL) {
991 db_printf(" (not initialized)\n");
992 return (0);
993 }
994 rn_walktree(rnh, db_show_radix_node, NULL);
995 return (0);
996 }
997 #endif
998 #endif /* INET */
999