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