GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/ndp/ndp.c Lines: 0 424 0.0 %
Date: 2017-11-07 Branches: 0 279 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: ndp.c,v 1.86 2017/08/09 17:35:38 jmc Exp $	*/
2
/*	$KAME: ndp.c,v 1.101 2002/07/17 08:46:33 itojun Exp $	*/
3
4
/*
5
 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. Neither the name of the project nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
/*
33
 * Copyright (c) 1984, 1993
34
 *	The Regents of the University of California.  All rights reserved.
35
 *
36
 * This code is derived from software contributed to Berkeley by
37
 * Sun Microsystems, Inc.
38
 *
39
 * Redistribution and use in source and binary forms, with or without
40
 * modification, are permitted provided that the following conditions
41
 * are met:
42
 * 1. Redistributions of source code must retain the above copyright
43
 *    notice, this list of conditions and the following disclaimer.
44
 * 2. Redistributions in binary form must reproduce the above copyright
45
 *    notice, this list of conditions and the following disclaimer in the
46
 *    documentation and/or other materials provided with the distribution.
47
 * 3. Neither the name of the University nor the names of its contributors
48
 *    may be used to endorse or promote products derived from this software
49
 *    without specific prior written permission.
50
 *
51
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61
 * SUCH DAMAGE.
62
 */
63
64
/*
65
 * Based on:
66
 * "@(#) Copyright (c) 1984, 1993\n\
67
 *	The Regents of the University of California.  All rights reserved.\n";
68
 *
69
 * "@(#)arp.c	8.2 (Berkeley) 1/2/94";
70
 */
71
72
/*
73
 * ndp - display, set, delete and flush neighbor cache
74
 */
75
76
77
#include <sys/file.h>
78
#include <sys/ioctl.h>
79
#include <sys/socket.h>
80
#include <sys/sysctl.h>
81
#include <sys/time.h>
82
#include <sys/queue.h>
83
84
#include <net/if.h>
85
#include <net/if_dl.h>
86
#include <net/if_types.h>
87
#include <net/route.h>
88
89
#include <netinet/in.h>
90
91
#include <netinet/icmp6.h>
92
#include <netinet6/in6_var.h>
93
#include <netinet6/nd6.h>
94
95
#include <arpa/inet.h>
96
97
#include <stdio.h>
98
#include <errno.h>
99
#include <fcntl.h>
100
#include <netdb.h>
101
#include <paths.h>
102
#include <stdlib.h>
103
#include <string.h>
104
#include <unistd.h>
105
#include <limits.h>
106
#include <err.h>
107
108
#include "gmt2local.h"
109
110
/* packing rule for routing socket */
111
#define ROUNDUP(a) \
112
	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
113
114
static pid_t pid;
115
static int nflag;
116
static int tflag;
117
static int32_t thiszone;	/* time difference with gmt */
118
static int rtsock = -1;
119
static int repeat = 0;
120
121
char ntop_buf[INET6_ADDRSTRLEN];	/* inet_ntop() */
122
char host_buf[NI_MAXHOST];		/* getnameinfo() */
123
char ifix_buf[IFNAMSIZ];		/* if_indextoname() */
124
125
int file(char *);
126
void getsocket(void);
127
int set(int, char **);
128
void get(char *);
129
int delete(char *);
130
void dump(struct in6_addr *, int);
131
static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int);
132
static char *ether_str(struct sockaddr_dl *);
133
int ndp_ether_aton(char *, u_char *);
134
void usage(void);
135
int rtmsg(int);
136
int rtget(struct sockaddr_in6 **, struct sockaddr_dl **);
137
void ifinfo(char *);
138
void harmonize_rtr(void);
139
static char *sec2str(time_t);
140
static void ts_print(const struct timeval *);
141
static int rdomain;
142
143
static char *rtpref_str[] = {
144
	"medium",		/* 00 */
145
	"high",			/* 01 */
146
	"rsv",			/* 10 */
147
	"low"			/* 11 */
148
};
149
150
int
151
main(int argc, char *argv[])
152
{
153
	int		 ch;
154
	int		 mode = 0;
155
	char		*arg = NULL;
156
	const char	*errstr;
157
158
	pid = getpid();
159
	thiszone = gmt2local(0);
160
	rdomain = getrtable();
161
	while ((ch = getopt(argc, argv, "acd:f:i:nstA:V:")) != -1) {
162
		switch (ch) {
163
		case 'a':
164
		case 'c':
165
		case 'p':
166
		case 'r':
167
		case 'P':
168
		case 's':
169
			if (mode) {
170
				usage();
171
				/*NOTREACHED*/
172
			}
173
			mode = ch;
174
			arg = NULL;
175
			break;
176
		case 'd':
177
		case 'f':
178
		case 'i' :
179
			if (mode) {
180
				usage();
181
				/*NOTREACHED*/
182
			}
183
			mode = ch;
184
			arg = optarg;
185
			break;
186
		case 'n':
187
			nflag = 1;
188
			break;
189
		case 't':
190
			tflag = 1;
191
			break;
192
		case 'A':
193
			if (mode) {
194
				usage();
195
				/*NOTREACHED*/
196
			}
197
			mode = 'a';
198
			repeat = strtonum(optarg, 1, INT_MAX, &errstr);
199
			if (errstr) {
200
				usage();
201
				/*NOTREACHED*/
202
			}
203
			break;
204
		case 'V':
205
			rdomain = strtonum(optarg, 0, RT_TABLEID_MAX, &errstr);
206
			if (errstr != NULL) {
207
				warn("bad rdomain: %s", errstr);
208
				usage();
209
				/*NOTREACHED*/
210
			}
211
			break;
212
		default:
213
			usage();
214
		}
215
	}
216
	argc -= optind;
217
	argv += optind;
218
219
	switch (mode) {
220
	case 'a':
221
	case 'c':
222
		if (argc != 0) {
223
			usage();
224
			/*NOTREACHED*/
225
		}
226
		dump(0, mode == 'c');
227
		break;
228
	case 'd':
229
		if (argc != 0) {
230
			usage();
231
			/*NOTREACHED*/
232
		}
233
		delete(arg);
234
		break;
235
	case 'f':
236
		if (argc != 0)
237
			usage();
238
		file(arg);
239
		break;
240
	case 'i':
241
		if (argc != 0)
242
			usage();
243
		ifinfo(arg);
244
		break;
245
	case 's':
246
		if (argc < 2 || argc > 4)
247
			usage();
248
		exit(set(argc, argv) ? 1 : 0);
249
	case 0:
250
		if (argc != 1) {
251
			usage();
252
			/*NOTREACHED*/
253
		}
254
		get(argv[0]);
255
		break;
256
	}
257
	exit(0);
258
}
259
260
/*
261
 * Process a file to set standard ndp entries
262
 */
263
int
264
file(char *name)
265
{
266
	FILE *fp;
267
	int i, retval;
268
	char line[100], arg[5][50], *args[5];
269
270
	if ((fp = fopen(name, "r")) == NULL) {
271
		fprintf(stderr, "ndp: cannot open %s\n", name);
272
		exit(1);
273
	}
274
	args[0] = &arg[0][0];
275
	args[1] = &arg[1][0];
276
	args[2] = &arg[2][0];
277
	args[3] = &arg[3][0];
278
	args[4] = &arg[4][0];
279
	retval = 0;
280
	while (fgets(line, sizeof(line), fp) != NULL) {
281
		i = sscanf(line, "%49s %49s %49s %49s %49s",
282
		    arg[0], arg[1], arg[2], arg[3], arg[4]);
283
		if (i < 2) {
284
			fprintf(stderr, "ndp: bad line: %s\n", line);
285
			retval = 1;
286
			continue;
287
		}
288
		if (set(i, args))
289
			retval = 1;
290
	}
291
	fclose(fp);
292
	return (retval);
293
}
294
295
void
296
getsocket(void)
297
{
298
	socklen_t len = sizeof(rdomain);
299
300
	if (rtsock >= 0)
301
		return;
302
	rtsock = socket(PF_ROUTE, SOCK_RAW, 0);
303
	if (rtsock < 0)
304
		err(1, "routing socket");
305
	if (setsockopt(rtsock, PF_ROUTE, ROUTE_TABLEFILTER, &rdomain, len) < 0)
306
		err(1, "ROUTE_TABLEFILTER");
307
308
	if (pledge("stdio dns flock rpath cpath wpath", NULL) == -1)
309
		err(1, "pledge");
310
}
311
312
struct	sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
313
struct	sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
314
struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
315
struct	sockaddr_dl ifp_m = { sizeof(&ifp_m), AF_LINK };
316
time_t	expire_time;
317
int	flags, found_entry;
318
struct	{
319
	struct	rt_msghdr m_rtm;
320
	char	m_space[512];
321
}	m_rtmsg;
322
323
/*
324
 * Set an individual neighbor cache entry
325
 */
326
int
327
set(int argc, char **argv)
328
{
329
	struct sockaddr_in6 *sin = &sin_m;
330
	struct sockaddr_dl *sdl;
331
	struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
332
	struct addrinfo hints, *res;
333
	int gai_error;
334
	u_char *ea;
335
	char *host = argv[0], *eaddr = argv[1];
336
337
	getsocket();
338
	argc -= 2;
339
	argv += 2;
340
	sdl_m = blank_sdl;
341
	sin_m = blank_sin;
342
343
	bzero(&hints, sizeof(hints));
344
	hints.ai_family = AF_INET6;
345
	gai_error = getaddrinfo(host, NULL, &hints, &res);
346
	if (gai_error) {
347
		fprintf(stderr, "ndp: %s: %s\n", host,
348
			gai_strerror(gai_error));
349
		return 1;
350
	}
351
	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
352
#ifdef __KAME__
353
	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
354
		*(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
355
		    htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
356
	}
357
#endif
358
	ea = (u_char *)LLADDR(&sdl_m);
359
	if (ndp_ether_aton(eaddr, ea) == 0)
360
		sdl_m.sdl_alen = 6;
361
	expire_time = 0;
362
	flags = 0;
363
	while (argc-- > 0) {
364
		if (strncmp(argv[0], "temp", 4) == 0) {
365
			struct timeval now;
366
367
			gettimeofday(&now, 0);
368
			expire_time = now.tv_sec + 20 * 60;
369
		} else if (strncmp(argv[0], "proxy", 5) == 0)
370
			flags |= RTF_ANNOUNCE;
371
		argv++;
372
	}
373
374
	if (rtget(&sin, &sdl)) {
375
		errx(1, "RTM_GET(%s) failed", host);
376
		/* NOTREACHED */
377
	}
378
379
	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
380
		if (sdl->sdl_family == AF_LINK &&
381
		    (rtm->rtm_flags & RTF_LLINFO) &&
382
		    !(rtm->rtm_flags & RTF_GATEWAY)) {
383
			switch (sdl->sdl_type) {
384
			case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
385
			case IFT_ISO88024: case IFT_ISO88025:
386
				goto overwrite;
387
			}
388
		}
389
		/*
390
		 * IPv4 arp command retries with sin_other = SIN_PROXY here.
391
		 */
392
		fprintf(stderr, "set: cannot configure a new entry\n");
393
		return 1;
394
	}
395
396
overwrite:
397
	if (sdl->sdl_family != AF_LINK) {
398
		printf("cannot intuit interface index and type for %s\n", host);
399
		return (1);
400
	}
401
	sdl_m.sdl_type = sdl->sdl_type;
402
	sdl_m.sdl_index = sdl->sdl_index;
403
	return (rtmsg(RTM_ADD));
404
}
405
406
/*
407
 * Display an individual neighbor cache entry
408
 */
409
void
410
get(char *host)
411
{
412
	struct sockaddr_in6 *sin = &sin_m;
413
	struct addrinfo hints, *res;
414
	int gai_error;
415
416
	sin_m = blank_sin;
417
	bzero(&hints, sizeof(hints));
418
	hints.ai_family = AF_INET6;
419
	gai_error = getaddrinfo(host, NULL, &hints, &res);
420
	if (gai_error) {
421
		fprintf(stderr, "ndp: %s: %s\n", host,
422
		    gai_strerror(gai_error));
423
		return;
424
	}
425
	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
426
#ifdef __KAME__
427
	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
428
		*(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
429
		    htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
430
	}
431
#endif
432
	dump(&sin->sin6_addr, 0);
433
	if (found_entry == 0) {
434
		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
435
		    sizeof(host_buf), NULL ,0,
436
		    (nflag ? NI_NUMERICHOST : 0));
437
		printf("%s (%s) -- no entry\n", host, host_buf);
438
		exit(1);
439
	}
440
}
441
442
/*
443
 * Delete a neighbor cache entry
444
 */
445
int
446
delete(char *host)
447
{
448
	struct sockaddr_in6 *sin = &sin_m;
449
	struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
450
	struct sockaddr_dl *sdl;
451
	struct addrinfo hints, *res;
452
	int gai_error;
453
454
	getsocket();
455
	sin_m = blank_sin;
456
457
	bzero(&hints, sizeof(hints));
458
	hints.ai_family = AF_INET6;
459
	if (nflag)
460
		hints.ai_flags = AI_NUMERICHOST;
461
	gai_error = getaddrinfo(host, NULL, &hints, &res);
462
	if (gai_error) {
463
		fprintf(stderr, "ndp: %s: %s\n", host,
464
		    gai_strerror(gai_error));
465
		return 1;
466
	}
467
	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
468
#ifdef __KAME__
469
	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
470
		*(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
471
		    htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
472
	}
473
#endif
474
475
	if (rtget(&sin, &sdl)) {
476
		errx(1, "RTM_GET(%s) failed", host);
477
		/* NOTREACHED */
478
	}
479
480
	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
481
		if (sdl->sdl_family == AF_LINK && rtm->rtm_flags & RTF_LLINFO) {
482
			if (rtm->rtm_flags & RTF_LOCAL)
483
				return (0);
484
			if (!(rtm->rtm_flags & RTF_GATEWAY))
485
				goto delete;
486
		}
487
		/*
488
		 * IPv4 arp command retries with sin_other = SIN_PROXY here.
489
		 */
490
		fprintf(stderr, "delete: cannot delete non-NDP entry\n");
491
		return 1;
492
	}
493
494
delete:
495
	if (sdl->sdl_family != AF_LINK) {
496
		printf("cannot locate %s\n", host);
497
		return (1);
498
	}
499
	if (rtmsg(RTM_DELETE) == 0) {
500
		struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
501
502
#ifdef __KAME__
503
		if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
504
			s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
505
			*(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
506
		}
507
#endif
508
		getnameinfo((struct sockaddr *)&s6,
509
		    s6.sin6_len, host_buf,
510
		    sizeof(host_buf), NULL, 0,
511
		    (nflag ? NI_NUMERICHOST : 0));
512
		printf("%s (%s) deleted\n", host, host_buf);
513
	}
514
515
	return 0;
516
}
517
518
#define W_ADDR	36
519
#define W_LL	17
520
#define W_IF	7
521
522
/*
523
 * Dump the entire neighbor cache
524
 */
525
void
526
dump(struct in6_addr *addr, int cflag)
527
{
528
	int mib[7];
529
	size_t needed;
530
	char *lim, *buf = NULL, *next;
531
	struct rt_msghdr *rtm;
532
	struct sockaddr_in6 *sin;
533
	struct sockaddr_dl *sdl;
534
	struct in6_nbrinfo *nbi;
535
	struct timeval now;
536
	int addrwidth;
537
	int llwidth;
538
	int ifwidth;
539
	char *ifname;
540
541
	/* Print header */
542
	if (!tflag && !cflag)
543
		printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
544
		    W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
545
		    W_IF, W_IF, "Netif", "Expire", "S", "Flags");
546
547
again:;
548
	lim = NULL;
549
	mib[0] = CTL_NET;
550
	mib[1] = PF_ROUTE;
551
	mib[2] = 0;
552
	mib[3] = AF_INET6;
553
	mib[4] = NET_RT_FLAGS;
554
	mib[5] = RTF_LLINFO;
555
	mib[6] = rdomain;
556
	while (1) {
557
		if (sysctl(mib, 7, NULL, &needed, NULL, 0) == -1)
558
			err(1, "sysctl(PF_ROUTE estimate)");
559
		if (needed == 0)
560
			break;
561
		if ((buf = realloc(buf, needed)) == NULL)
562
			err(1, "realloc");
563
		if (sysctl(mib, 7, buf, &needed, NULL, 0) == -1) {
564
			if (errno == ENOMEM)
565
				continue;
566
			err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
567
		}
568
		lim = buf + needed;
569
		break;
570
	}
571
572
	for (next = buf; next && lim && next < lim; next += rtm->rtm_msglen) {
573
		int isrouter = 0, prbs = 0;
574
575
		rtm = (struct rt_msghdr *)next;
576
		if (rtm->rtm_version != RTM_VERSION)
577
			continue;
578
		sin = (struct sockaddr_in6 *)(next + rtm->rtm_hdrlen);
579
		sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
580
581
		/*
582
		 * Some OSes can produce a route that has the LINK flag but
583
		 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
584
		 * and BSD/OS, where xx is not the interface identifier on
585
		 * lo0).  Such routes entry would annoy getnbrinfo() below,
586
		 * so we skip them.
587
		 * XXX: such routes should have the GATEWAY flag, not the
588
		 * LINK flag.  However, there is rotten routing software
589
		 * that advertises all routes that have the GATEWAY flag.
590
		 * Thus, KAME kernel intentionally does not set the LINK flag.
591
		 * What is to be fixed is not ndp, but such routing software
592
		 * (and the kernel workaround)...
593
		 */
594
		if (sdl->sdl_family != AF_LINK)
595
			continue;
596
597
		if (!(rtm->rtm_flags & RTF_HOST))
598
			continue;
599
600
		if (addr) {
601
			if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
602
				continue;
603
			found_entry = 1;
604
		} else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
605
			continue;
606
		if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
607
		    IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
608
			/* XXX: should scope id be filled in the kernel? */
609
			if (sin->sin6_scope_id == 0)
610
				sin->sin6_scope_id = sdl->sdl_index;
611
#ifdef __KAME__
612
			/* KAME specific hack; removed the embedded id */
613
			*(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
614
#endif
615
		}
616
		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
617
		    sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
618
		if (cflag) {
619
			if (rtm->rtm_flags & RTF_CLONED)
620
				delete(host_buf);
621
			continue;
622
		}
623
		gettimeofday(&now, 0);
624
		if (tflag)
625
			ts_print(&now);
626
627
		addrwidth = strlen(host_buf);
628
		if (addrwidth < W_ADDR)
629
			addrwidth = W_ADDR;
630
		llwidth = strlen(ether_str(sdl));
631
		if (W_ADDR + W_LL - addrwidth > llwidth)
632
			llwidth = W_ADDR + W_LL - addrwidth;
633
		ifname = if_indextoname(sdl->sdl_index, ifix_buf);
634
		if (!ifname)
635
			ifname = "?";
636
		ifwidth = strlen(ifname);
637
		if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
638
			ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
639
640
		printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
641
		    llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
642
643
		/* Print neighbor discovery specific informations */
644
		nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
645
		if (nbi) {
646
			if (nbi->expire > now.tv_sec) {
647
				printf(" %-9.9s",
648
				    sec2str(nbi->expire - now.tv_sec));
649
			} else if (nbi->expire == 0)
650
				printf(" %-9.9s", "permanent");
651
			else
652
				printf(" %-9.9s", "expired");
653
654
			switch (nbi->state) {
655
			case ND6_LLINFO_NOSTATE:
656
				 printf(" N");
657
				 break;
658
			case ND6_LLINFO_INCOMPLETE:
659
				 printf(" I");
660
				 break;
661
			case ND6_LLINFO_REACHABLE:
662
				 printf(" R");
663
				 break;
664
			case ND6_LLINFO_STALE:
665
				 printf(" S");
666
				 break;
667
			case ND6_LLINFO_DELAY:
668
				 printf(" D");
669
				 break;
670
			case ND6_LLINFO_PROBE:
671
				 printf(" P");
672
				 break;
673
			default:
674
				 printf(" ?");
675
				 break;
676
			}
677
678
			isrouter = nbi->isrouter;
679
			prbs = nbi->asked;
680
		} else {
681
			warnx("failed to get neighbor information");
682
			printf("  ");
683
		}
684
685
		printf(" %s%s%s",
686
		    (rtm->rtm_flags & RTF_LOCAL) ? "l" : "",
687
		    isrouter ? "R" : "",
688
		    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
689
690
		if (prbs)
691
			printf(" %d", prbs);
692
693
		printf("\n");
694
	}
695
696
	if (repeat) {
697
		printf("\n");
698
		fflush(stdout);
699
		sleep(repeat);
700
		goto again;
701
	}
702
703
	free(buf);
704
}
705
706
static struct in6_nbrinfo *
707
getnbrinfo(struct in6_addr *addr, int ifindex, int warning)
708
{
709
	static struct in6_nbrinfo nbi;
710
	int s;
711
712
	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
713
		err(1, "socket");
714
715
	bzero(&nbi, sizeof(nbi));
716
	if_indextoname(ifindex, nbi.ifname);
717
	nbi.addr = *addr;
718
	if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
719
		if (warning)
720
			warn("ioctl(SIOCGNBRINFO_IN6)");
721
		close(s);
722
		return(NULL);
723
	}
724
725
	close(s);
726
	return(&nbi);
727
}
728
729
static char *
730
ether_str(struct sockaddr_dl *sdl)
731
{
732
	static char hbuf[NI_MAXHOST];
733
	u_char *cp;
734
735
	if (sdl->sdl_alen) {
736
		cp = (u_char *)LLADDR(sdl);
737
		snprintf(hbuf, sizeof(hbuf), "%02x:%02x:%02x:%02x:%02x:%02x",
738
		    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
739
	} else
740
		snprintf(hbuf, sizeof(hbuf), "(incomplete)");
741
742
	return(hbuf);
743
}
744
745
int
746
ndp_ether_aton(char *a, u_char *n)
747
{
748
	int i, o[6];
749
750
	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
751
	    &o[3], &o[4], &o[5]);
752
	if (i != 6) {
753
		fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
754
		return (1);
755
	}
756
	for (i = 0; i < 6; i++)
757
		n[i] = o[i];
758
	return (0);
759
}
760
761
void
762
usage(void)
763
{
764
	printf("usage: ndp [-acnt] ");
765
	printf("[-A wait] [-d hostname] [-f filename] [-i interface]\n");
766
	printf("\t[-s nodename ether_addr [temp] [proxy]] ");
767
	printf("[-V rdomain] [hostname]\n");
768
	exit(1);
769
}
770
771
int
772
rtmsg(int cmd)
773
{
774
	static int seq;
775
	int rlen;
776
	struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
777
	char *cp = m_rtmsg.m_space;
778
	int l;
779
780
	errno = 0;
781
	if (cmd == RTM_DELETE)
782
		goto doit;
783
	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
784
	rtm->rtm_flags = flags;
785
	rtm->rtm_version = RTM_VERSION;
786
	rtm->rtm_tableid = rdomain;
787
788
	switch (cmd) {
789
	default:
790
		fprintf(stderr, "ndp: internal wrong cmd\n");
791
		exit(1);
792
	case RTM_ADD:
793
		rtm->rtm_addrs |= RTA_GATEWAY;
794
		if (expire_time) {
795
			rtm->rtm_rmx.rmx_expire = expire_time;
796
			rtm->rtm_inits = RTV_EXPIRE;
797
		}
798
		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
799
#if 0	/* we don't support ipv6addr/128 type proxying. */
800
		if (rtm->rtm_flags & RTF_ANNOUNCE) {
801
			rtm->rtm_flags &= ~RTF_HOST;
802
			rtm->rtm_addrs |= RTA_NETMASK;
803
		}
804
#endif
805
		/* FALLTHROUGH */
806
	case RTM_GET:
807
		rtm->rtm_addrs |= (RTA_DST | RTA_IFP);
808
	}
809
#define NEXTADDR(w, s) \
810
	if (rtm->rtm_addrs & (w)) { \
811
		bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
812
813
	NEXTADDR(RTA_DST, sin_m);
814
	NEXTADDR(RTA_GATEWAY, sdl_m);
815
#if 0	/* we don't support ipv6addr/128 type proxying. */
816
	memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
817
	NEXTADDR(RTA_NETMASK, so_mask);
818
#endif
819
	NEXTADDR(RTA_IFP, ifp_m);
820
821
	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
822
doit:
823
	l = rtm->rtm_msglen;
824
	rtm->rtm_seq = ++seq;
825
	rtm->rtm_type = cmd;
826
	if ((rlen = write(rtsock, (char *)&m_rtmsg, l)) < 0) {
827
		if (errno != ESRCH || cmd != RTM_DELETE) {
828
			err(1, "writing to routing socket");
829
			/* NOTREACHED */
830
		}
831
	}
832
	do {
833
		l = read(rtsock, (char *)&m_rtmsg, sizeof(m_rtmsg));
834
	} while (l > 0 && (rtm->rtm_version != RTM_VERSION ||
835
	    rtm->rtm_seq != seq || rtm->rtm_pid != pid));
836
	if (l < 0)
837
		(void) fprintf(stderr, "ndp: read from routing socket: %s\n",
838
		    strerror(errno));
839
	return (0);
840
}
841
842
int
843
rtget(struct sockaddr_in6 **sinp, struct sockaddr_dl **sdlp)
844
{
845
	struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
846
	struct sockaddr_in6 *sin = NULL;
847
	struct sockaddr_dl *sdl = NULL;
848
	struct sockaddr *sa;
849
	char *cp;
850
	unsigned int i;
851
852
	if (rtmsg(RTM_GET) < 0)
853
		return (1);
854
855
	if (rtm->rtm_addrs) {
856
		cp = ((char *)rtm + rtm->rtm_hdrlen);
857
		for (i = 1; i; i <<= 1) {
858
			if (i & rtm->rtm_addrs) {
859
				sa = (struct sockaddr *)cp;
860
				switch (i) {
861
				case RTA_DST:
862
					sin = (struct sockaddr_in6 *)sa;
863
					break;
864
				case RTA_IFP:
865
					sdl = (struct sockaddr_dl *)sa;
866
					break;
867
				default:
868
					break;
869
				}
870
				cp += ROUNDUP(sa->sa_len);
871
			}
872
		}
873
	}
874
875
	if (sin == NULL || sdl == NULL)
876
		return (1);
877
878
	*sinp = sin;
879
	*sdlp = sdl;
880
881
	return (0);
882
}
883
884
void
885
ifinfo(char *ifname)
886
{
887
	struct in6_ndireq nd;
888
	int i, s;
889
890
	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
891
		err(1, "socket");
892
		/* NOTREACHED */
893
	}
894
	bzero(&nd, sizeof(nd));
895
	strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
896
	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0)
897
		err(1, "ioctl(SIOCGIFINFO_IN6)");
898
899
	if (!nd.ndi.initialized)
900
		errx(1, "%s: not initialized yet", ifname);
901
902
	printf("basereachable=%ds%dms",
903
	    nd.ndi.basereachable / 1000, nd.ndi.basereachable % 1000);
904
	printf(", reachable=%ds", nd.ndi.reachable);
905
	printf(", retrans=%ds%dms\n", nd.ndi.retrans / 1000,
906
	    nd.ndi.retrans % 1000);
907
908
	close(s);
909
}
910
911
void
912
harmonize_rtr(void)
913
{
914
	char dummyif[IFNAMSIZ+8];
915
	int s;
916
917
	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
918
		err(1, "socket");
919
	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
920
	if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
921
		err(1, "ioctl(SIOCSNDFLUSH_IN6)");
922
923
	close(s);
924
}
925
926
static char *
927
sec2str(time_t total)
928
{
929
	static char result[256];
930
	int days, hours, mins, secs;
931
	int first = 1;
932
	char *p = result;
933
	char *ep = &result[sizeof(result)];
934
	int n;
935
936
	days = total / 3600 / 24;
937
	hours = (total / 3600) % 24;
938
	mins = (total / 60) % 60;
939
	secs = total % 60;
940
941
	if (days) {
942
		first = 0;
943
		n = snprintf(p, ep - p, "%dd", days);
944
		if (n < 0 || n >= ep - p)
945
			return "?";
946
		p += n;
947
	}
948
	if (!first || hours) {
949
		first = 0;
950
		n = snprintf(p, ep - p, "%dh", hours);
951
		if (n < 0 || n >= ep - p)
952
			return "?";
953
		p += n;
954
	}
955
	if (!first || mins) {
956
		first = 0;
957
		n = snprintf(p, ep - p, "%dm", mins);
958
		if (n < 0 || n >= ep - p)
959
			return "?";
960
		p += n;
961
	}
962
	snprintf(p, ep - p, "%ds", secs);
963
964
	return(result);
965
}
966
967
/*
968
 * Print the timestamp
969
 * from tcpdump/util.c
970
 */
971
static void
972
ts_print(const struct timeval *tvp)
973
{
974
	int s;
975
976
	/* Default */
977
	s = (tvp->tv_sec + thiszone) % 86400;
978
	(void)printf("%02d:%02d:%02d.%06u ",
979
	    s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
980
}