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