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