2 * Copyright (c) 2009-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. Neither the name of the project nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * Copyright (c) 1984, 1993
58 * The Regents of the University of California. All rights reserved.
60 * This code is derived from software contributed to Berkeley by
61 * Sun Microsystems, Inc.
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
66 * 1. Redistributions of source code must retain the above copyright
67 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in the
70 * documentation and/or other materials provided with the distribution.
71 * 3. All advertising materials mentioning features or use of this software
72 * must display the following acknowledgement:
73 * This product includes software developed by the University of
74 * California, Berkeley and its contributors.
75 * 4. Neither the name of the University nor the names of its contributors
76 * may be used to endorse or promote products derived from this software
77 * without specific prior written permission.
79 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
80 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
83 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94 * "@(#) Copyright (c) 1984, 1993\n\
95 * The Regents of the University of California. All rights reserved.\n";
97 * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
101 * ndp - display, set, delete and flush neighbor cache
105 #include <sys/param.h>
106 #include <sys/file.h>
107 #include <sys/ioctl.h>
108 #include <sys/socket.h>
109 #include <sys/sysctl.h>
110 #include <sys/time.h>
111 #include <sys/queue.h>
114 #include <net/if_var.h>
115 #include <net/if_dl.h>
116 #include <net/if_types.h>
117 #include <net/route.h>
119 #include <netinet/in.h>
120 #include <netinet/if_ether.h>
122 #include <netinet/icmp6.h>
123 #include <netinet6/in6_var.h>
124 #include <netinet6/nd6.h>
126 #include <arpa/inet.h>
139 /* packing rule for routing socket */
141 ((a) > 0 ? (1 + (((a) - 1) | (sizeof (uint32_t) - 1))) : \
143 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
149 static int32_t thiszone
= 0; /* time difference with gmt */
151 static int repeat
= 0;
153 static char host_buf
[NI_MAXHOST
]; /* getnameinfo() */
154 static char ifix_buf
[IFNAMSIZ
]; /* if_indextoname() */
156 static int file(char *);
157 static void getsocket(void);
158 static int set(int, char **);
159 static void get(char *);
160 static int delete(char *);
161 static void dump(struct in6_addr
*);
162 static void dump_ext(struct in6_addr
*, int);
163 static struct in6_nbrinfo
*getnbrinfo(struct in6_addr
*, int, int);
164 static char *ether_str(struct sockaddr_dl
*);
165 static int ndp_ether_aton(char *, u_char
*);
166 static void usage(void);
167 static int rtmsg(int);
168 static void ifinfo(int, char **);
169 static void rtrlist(void);
170 static void plist(void);
171 static void pfx_flush(void);
172 static void rtrlist(void);
173 static void rtr_flush(void);
174 static void harmonize_rtr(void);
175 static void getdefif(void);
176 static void setdefif(char *);
177 static char *sec2str(time_t);
178 static char *ether_str(struct sockaddr_dl
*);
179 static void ts_print(const struct timeval
*);
180 static void read_cga_parameters(void);
181 static void write_cga_parameters(const char[]);
183 static char *rtpref_str
[] = {
191 main(int argc
, char **argv
)
194 int aflag
= 0, dflag
= 0, sflag
= 0, Hflag
= 0, pflag
= 0, rflag
= 0,
195 Pflag
= 0, Rflag
= 0, lflag
= 0, xflag
= 0, wflag
= 0;
198 while ((ch
= getopt(argc
, argv
, "acndfIilprstA:HPRxwW")) != -1)
212 getdefif(); /* always call it to print the result */
246 repeat
= atoi(optarg
);
269 write_cga_parameters(argv
[2]);
278 if (aflag
|| cflag
) {
300 if (argc
< 2 || argc
> 4)
302 exit(set(argc
, argv
) ? 1 : 0);
317 read_cga_parameters();
328 * Process a file to set standard ndp entries
335 char line
[100], arg
[5][50], *args
[5];
337 if ((fp
= fopen(name
, "r")) == NULL
) {
338 fprintf(stderr
, "ndp: cannot open %s\n", name
);
341 args
[0] = &arg
[0][0];
342 args
[1] = &arg
[1][0];
343 args
[2] = &arg
[2][0];
344 args
[3] = &arg
[3][0];
345 args
[4] = &arg
[4][0];
347 while (fgets(line
, 100, fp
) != NULL
) {
348 i
= sscanf(line
, "%s %s %s %s %s", arg
[0], arg
[1], arg
[2],
351 fprintf(stderr
, "ndp: bad line: %s\n", line
);
366 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
368 perror("ndp: socket");
374 struct sockaddr_in6 so_mask
= {sizeof (so_mask
), AF_INET6
};
375 struct sockaddr_in6 blank_sin
= {sizeof (blank_sin
), AF_INET6
}, sin_m
;
376 struct sockaddr_dl blank_sdl
= {sizeof (blank_sdl
), AF_LINK
}, sdl_m
;
377 int expire_time
, flags
, found_entry
;
379 struct rt_msghdr m_rtm
;
384 * Set an individual neighbor cache entry
387 set(int argc
, char **argv
)
389 register struct sockaddr_in6
*sin
= &sin_m
;
390 register struct sockaddr_dl
*sdl
;
391 register struct rt_msghdr
*rtm
= &(m_rtmsg
.m_rtm
);
392 struct addrinfo hints
, *res
;
395 char *host
= argv
[0], *eaddr
= argv
[1];
404 bzero(&hints
, sizeof (hints
));
405 hints
.ai_family
= AF_INET6
;
406 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
408 fprintf(stderr
, "ndp: %s: %s\n", host
,
409 gai_strerror(gai_error
));
412 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
414 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
415 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
416 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
419 ea
= (u_char
*)LLADDR(&sdl_m
);
421 ealen
= ndp_ether_aton(eaddr
, ea
);
423 sdl_m
.sdl_alen
= ealen
;
424 flags
= expire_time
= 0;
426 if (strncmp(argv
[0], "temp", 4) == 0) {
428 gettimeofday(&time
, 0);
429 expire_time
= time
.tv_sec
+ 20 * 60;
430 } else if (strncmp(argv
[0], "proxy", 5) == 0)
431 flags
|= RTF_ANNOUNCE
;
434 if (rtmsg(RTM_GET
) < 0) {
438 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
439 sdl
= (struct sockaddr_dl
*)(ROUNDUP(sin
->sin6_len
) + (char *)sin
);
440 if (IN6_ARE_ADDR_EQUAL(&sin
->sin6_addr
, &sin_m
.sin6_addr
)) {
441 if (sdl
->sdl_family
== AF_LINK
&&
442 (rtm
->rtm_flags
& RTF_LLINFO
) &&
443 !(rtm
->rtm_flags
& RTF_GATEWAY
)) switch (sdl
->sdl_type
) {
444 case IFT_ETHER
: case IFT_FDDI
: case IFT_ISO88023
:
445 case IFT_ISO88024
: case IFT_ISO88025
:
446 #if defined(IFT_6LOWPAN)
452 * IPv4 arp command retries with sin_other = SIN_PROXY here.
454 fprintf(stderr
, "set: cannot configure a new entry\n");
459 if (sdl
->sdl_family
!= AF_LINK
) {
460 printf("cannot intuit interface index and type for %s\n", host
);
463 sdl_m
.sdl_type
= sdl
->sdl_type
;
464 sdl_m
.sdl_index
= sdl
->sdl_index
;
465 return (rtmsg(RTM_ADD
));
469 * Display an individual neighbor cache entry
474 struct sockaddr_in6
*sin
= &sin_m
;
475 struct addrinfo hints
, *res
;
479 bzero(&hints
, sizeof (hints
));
480 hints
.ai_family
= AF_INET6
;
481 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
483 fprintf(stderr
, "ndp: %s: %s\n", host
,
484 gai_strerror(gai_error
));
487 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
489 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
490 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
491 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
494 dump(&sin
->sin6_addr
);
495 if (found_entry
== 0) {
496 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
497 sizeof (host_buf
), NULL
, 0, NI_WITHSCOPEID
| (nflag
?
498 NI_NUMERICHOST
: 0));
499 printf("%s (%s) -- no entry\n", host
, host_buf
);
505 * Delete a neighbor cache entry
510 struct sockaddr_in6
*sin
= &sin_m
;
511 register struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
512 struct sockaddr_dl
*sdl
;
513 struct addrinfo hints
, *res
;
519 bzero(&hints
, sizeof (hints
));
520 hints
.ai_family
= AF_INET6
;
521 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
523 fprintf(stderr
, "ndp: %s: %s\n", host
,
524 gai_strerror(gai_error
));
527 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
529 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
530 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
531 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
534 if (rtmsg(RTM_GET
) < 0) {
538 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
539 sdl
= (struct sockaddr_dl
*)(ROUNDUP(sin
->sin6_len
) + (char *)sin
);
540 if (IN6_ARE_ADDR_EQUAL(&sin
->sin6_addr
, &sin_m
.sin6_addr
)) {
541 if (sdl
->sdl_family
== AF_LINK
&&
542 (rtm
->rtm_flags
& RTF_LLINFO
) &&
543 !(rtm
->rtm_flags
& RTF_GATEWAY
)) {
547 * IPv4 arp command retries with sin_other = SIN_PROXY here.
549 fprintf(stderr
, "delete: cannot delete non-NDP entry\n");
554 if (sdl
->sdl_family
!= AF_LINK
) {
555 printf("cannot locate %s\n", host
);
558 if (rtmsg(RTM_DELETE
) == 0) {
559 struct sockaddr_in6 s6
= *sin
;
562 if (IN6_IS_ADDR_LINKLOCAL(&s6
.sin6_addr
)) {
564 ntohs(*(u_int16_t
*)&s6
.sin6_addr
.s6_addr
[2]);
565 *(u_int16_t
*)&s6
.sin6_addr
.s6_addr
[2] = 0;
568 getnameinfo((struct sockaddr
*)&s6
,
569 s6
.sin6_len
, host_buf
,
570 sizeof (host_buf
), NULL
, 0,
571 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
572 printf("%s (%s) deleted\n", host
, host_buf
);
583 * Dump the entire neighbor cache
586 dump(struct in6_addr
*addr
)
590 char *lim
, *buf
, *next
;
591 struct rt_msghdr
*rtm
;
592 struct sockaddr_in6
*sin
;
593 struct sockaddr_dl
*sdl
;
594 struct in6_nbrinfo
*nbi
;
603 if (!tflag
&& !cflag
)
604 printf("%-*.*s %-*.*s %*.*s %-9.9s %2s %4s %4s\n",
605 W_ADDR
, W_ADDR
, "Neighbor", W_LL
, W_LL
, "Linklayer Address",
606 W_IF
, W_IF
, "Netif", "Expire", "St", "Flgs", "Prbs");
613 mib
[4] = NET_RT_FLAGS
;
615 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
616 err(1, "sysctl(PF_ROUTE estimate)");
618 if ((buf
= malloc(needed
)) == NULL
)
620 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
621 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
626 for (next
= buf
; next
&& next
< lim
; next
+= rtm
->rtm_msglen
) {
627 int isrouter
= 0, prbs
= 0;
629 rtm
= (struct rt_msghdr
*)next
;
630 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
631 sdl
= (struct sockaddr_dl
*)((char *)sin
+
632 ROUNDUP(sin
->sin6_len
));
635 * Some OSes can produce a route that has the LINK flag but
636 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
637 * and BSD/OS, where xx is not the interface identifier on
638 * lo0). Such routes entry would annoy getnbrinfo() below,
640 * XXX: such routes should have the GATEWAY flag, not the
641 * LINK flag. However, there are rotten routing software
642 * that advertises all routes that have the GATEWAY flag.
643 * Thus, KAME kernel intentionally does not set the LINK flag.
644 * What is to be fixed is not ndp, but such routing software
645 * (and the kernel workaround)...
647 if (sdl
->sdl_family
!= AF_LINK
)
651 if (!IN6_ARE_ADDR_EQUAL(addr
, &sin
->sin6_addr
))
654 } else if (IN6_IS_ADDR_MULTICAST(&sin
->sin6_addr
))
656 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
) ||
657 IN6_IS_ADDR_MC_NODELOCAL(&sin
->sin6_addr
) ||
658 IN6_IS_ADDR_MC_LINKLOCAL(&sin
->sin6_addr
)) {
659 /* should scope id be filled in the kernel? */
660 if (sin
->sin6_scope_id
== 0)
661 sin
->sin6_scope_id
= sdl
->sdl_index
;
663 /* KAME specific hack; removed the embedded id */
664 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] = 0;
667 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
668 sizeof (host_buf
), NULL
, 0,
669 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
671 if (rtm
->rtm_flags
& RTF_WASCLONED
)
675 gettimeofday(&time
, 0);
679 addrwidth
= strlen(host_buf
);
680 if (addrwidth
< W_ADDR
)
682 llwidth
= strlen(ether_str(sdl
));
683 if (W_ADDR
+ W_LL
- addrwidth
> llwidth
)
684 llwidth
= W_ADDR
+ W_LL
- addrwidth
;
685 ifname
= if_indextoname(sdl
->sdl_index
, ifix_buf
);
688 ifwidth
= strlen(ifname
);
689 if (W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
> ifwidth
)
690 ifwidth
= W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
;
692 printf("%-*.*s %-*.*s %*.*s", addrwidth
, addrwidth
, host_buf
,
693 llwidth
, llwidth
, ether_str(sdl
), ifwidth
, ifwidth
, ifname
);
695 /* Print neighbor discovery specific informations */
696 nbi
= getnbrinfo(&sin
->sin6_addr
, sdl
->sdl_index
, 1);
698 if (nbi
->expire
> time
.tv_sec
) {
699 printf(" %-9.9s", sec2str(nbi
->expire
-
701 } else if (nbi
->expire
== 0)
702 printf(" %-9.9s", "permanent");
704 printf(" %-9.9s", "expired");
706 switch (nbi
->state
) {
707 case ND6_LLINFO_NOSTATE
:
710 case ND6_LLINFO_INCOMPLETE
:
713 case ND6_LLINFO_REACHABLE
:
716 case ND6_LLINFO_STALE
:
719 case ND6_LLINFO_DELAY
:
722 case ND6_LLINFO_PROBE
:
730 isrouter
= nbi
->isrouter
;
733 warnx("failed to get neighbor information");
739 * other flags. R: router, P: proxy, W: ??
741 if ((rtm
->rtm_addrs
& RTA_NETMASK
) == 0) {
742 snprintf(flgbuf
, sizeof (flgbuf
), "%s%s",
744 (rtm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
746 sin
= (struct sockaddr_in6
*)
747 (sdl
->sdl_len
+ (char *)sdl
);
748 snprintf(flgbuf
, sizeof (flgbuf
), "%s%s%s%s",
750 !IN6_IS_ADDR_UNSPECIFIED(&sin
->sin6_addr
)
752 (sin
->sin6_len
!= sizeof (struct sockaddr_in6
))
754 (rtm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
756 printf(" %-4.4s", flgbuf
);
759 printf(" %4d", prbs
);
774 * Dump the entire neighbor cache (extended)
777 dump_ext(addr
, xflag
)
778 struct in6_addr
*addr
;
783 char *lim
, *buf
, *next
;
784 struct rt_msghdr_ext
*ertm
;
785 struct sockaddr_in6
*sin
;
786 struct sockaddr_dl
*sdl
;
787 struct in6_nbrinfo
*nbi
;
796 if (!tflag
&& !cflag
) {
797 printf("%-*.*s %-*.*s %*.*s %-9.9s %-9.9s %2s %4s %4s",
798 W_ADDR
, W_ADDR
, "Neighbor", W_LL
, W_LL
, "Linklayer Address",
799 W_IF
, W_IF
, "Netif", "Expire(O)", "Expire(I)", "St",
802 printf(" %-7.7s %-7.7s %-7.7s", "RSSI", "LQM", "NPM");
811 mib
[4] = NET_RT_DUMPX_FLAGS
;
813 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
814 err(1, "sysctl(PF_ROUTE estimate)");
816 if ((buf
= malloc(needed
)) == NULL
)
818 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
819 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
824 for (next
= buf
; next
&& next
< lim
; next
+= ertm
->rtm_msglen
) {
825 int isrouter
= 0, prbs
= 0;
827 ertm
= (struct rt_msghdr_ext
*)next
;
828 sin
= (struct sockaddr_in6
*)(ertm
+ 1);
829 sdl
= (struct sockaddr_dl
*)((char *)sin
+
830 ROUNDUP(sin
->sin6_len
));
833 * Some OSes can produce a route that has the LINK flag but
834 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
835 * and BSD/OS, where xx is not the interface identifier on
836 * lo0). Such routes entry would annoy getnbrinfo() below,
838 * XXX: such routes should have the GATEWAY flag, not the
839 * LINK flag. However, there are rotten routing software
840 * that advertises all routes that have the GATEWAY flag.
841 * Thus, KAME kernel intentionally does not set the LINK flag.
842 * What is to be fixed is not ndp, but such routing software
843 * (and the kernel workaround)...
845 if (sdl
->sdl_family
!= AF_LINK
)
849 if (!IN6_ARE_ADDR_EQUAL(addr
, &sin
->sin6_addr
))
852 } else if (IN6_IS_ADDR_MULTICAST(&sin
->sin6_addr
))
854 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
) ||
855 IN6_IS_ADDR_MC_NODELOCAL(&sin
->sin6_addr
) ||
856 IN6_IS_ADDR_MC_LINKLOCAL(&sin
->sin6_addr
)) {
857 /* should scope id be filled in the kernel? */
858 if (sin
->sin6_scope_id
== 0)
859 sin
->sin6_scope_id
= sdl
->sdl_index
;
861 /* KAME specific hack; removed the embedded id */
862 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] = 0;
865 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
866 sizeof (host_buf
), NULL
, 0,
867 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
869 if (ertm
->rtm_flags
& RTF_WASCLONED
)
873 gettimeofday(&time
, 0);
877 addrwidth
= strlen(host_buf
);
878 if (addrwidth
< W_ADDR
)
880 llwidth
= strlen(ether_str(sdl
));
881 if (W_ADDR
+ W_LL
- addrwidth
> llwidth
)
882 llwidth
= W_ADDR
+ W_LL
- addrwidth
;
883 ifname
= if_indextoname(sdl
->sdl_index
, ifix_buf
);
886 ifwidth
= strlen(ifname
);
887 if (W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
> ifwidth
)
888 ifwidth
= W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
;
890 printf("%-*.*s %-*.*s %*.*s", addrwidth
, addrwidth
, host_buf
,
891 llwidth
, llwidth
, ether_str(sdl
), ifwidth
, ifwidth
, ifname
);
893 if (ertm
->rtm_ri
.ri_refcnt
== 0 ||
894 ertm
->rtm_ri
.ri_snd_expire
== 0)
895 printf(" %-9.9s", "(none)");
896 else if (ertm
->rtm_ri
.ri_snd_expire
> time
.tv_sec
)
898 sec2str(ertm
->rtm_ri
.ri_snd_expire
- time
.tv_sec
));
900 printf(" %-9.9s", "expired");
902 if (ertm
->rtm_ri
.ri_refcnt
== 0 ||
903 ertm
->rtm_ri
.ri_rcv_expire
== 0)
904 printf(" %-9.9s", "(none)");
905 else if (ertm
->rtm_ri
.ri_rcv_expire
> time
.tv_sec
)
907 sec2str(ertm
->rtm_ri
.ri_rcv_expire
- time
.tv_sec
));
909 printf(" %-9.9s", "expired");
911 /* Print neighbor discovery specific informations */
912 nbi
= getnbrinfo(&sin
->sin6_addr
, sdl
->sdl_index
, 1);
914 switch (nbi
->state
) {
915 case ND6_LLINFO_NOSTATE
:
918 case ND6_LLINFO_INCOMPLETE
:
921 case ND6_LLINFO_REACHABLE
:
924 case ND6_LLINFO_STALE
:
927 case ND6_LLINFO_DELAY
:
930 case ND6_LLINFO_PROBE
:
938 isrouter
= nbi
->isrouter
;
941 warnx("failed to get neighbor information");
947 * other flags. R: router, P: proxy, W: ??
949 if ((ertm
->rtm_addrs
& RTA_NETMASK
) == 0) {
950 snprintf(flgbuf
, sizeof (flgbuf
), "%s%s",
952 (ertm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
954 sin
= (struct sockaddr_in6
*)
955 (sdl
->sdl_len
+ (char *)sdl
);
956 snprintf(flgbuf
, sizeof (flgbuf
), "%s%s%s%s",
958 !IN6_IS_ADDR_UNSPECIFIED(&sin
->sin6_addr
)
960 (sin
->sin6_len
!= sizeof (struct sockaddr_in6
))
962 (ertm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
964 printf(" %-4.4s", flgbuf
);
967 printf(" %4d", prbs
);
971 printf(" %-4.4s", "none");
973 if (ertm
->rtm_ri
.ri_rssi
!= IFNET_RSSI_UNKNOWN
)
974 printf(" %7d", ertm
->rtm_ri
.ri_rssi
);
976 printf(" %-7.7s", "unknown");
978 switch (ertm
->rtm_ri
.ri_lqm
)
980 case IFNET_LQM_THRESH_OFF
:
981 printf(" %-7.7s", "off");
983 case IFNET_LQM_THRESH_UNKNOWN
:
984 printf(" %-7.7s", "unknown");
986 case IFNET_LQM_THRESH_POOR
:
987 printf(" %-7.7s", "poor");
989 case IFNET_LQM_THRESH_GOOD
:
990 printf(" %-7.7s", "good");
993 printf(" %7d", ertm
->rtm_ri
.ri_lqm
);
997 switch (ertm
->rtm_ri
.ri_npm
)
999 case IFNET_NPM_THRESH_UNKNOWN
:
1000 printf(" %-7.7s", "unknown");
1002 case IFNET_NPM_THRESH_NEAR
:
1003 printf(" %-7.7s", "near");
1005 case IFNET_NPM_THRESH_GENERAL
:
1006 printf(" %-7.7s", "general");
1008 case IFNET_NPM_THRESH_FAR
:
1009 printf(" %-7.7s", "far");
1012 printf(" %7d", ertm
->rtm_ri
.ri_npm
);
1029 static struct in6_nbrinfo
*
1030 getnbrinfo(addr
, ifindex
, warning
)
1031 struct in6_addr
*addr
;
1035 static struct in6_nbrinfo nbi
;
1038 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1041 bzero(&nbi
, sizeof (nbi
));
1042 if_indextoname(ifindex
, nbi
.ifname
);
1044 if (ioctl(s
, SIOCGNBRINFO_IN6
, (caddr_t
)&nbi
) < 0) {
1046 warn("ioctl(SIOCGNBRINFO_IN6)");
1056 ether_str(struct sockaddr_dl
*sdl
)
1058 static char ebuf
[32];
1061 if (sdl
->sdl_alen
) {
1062 cp
= (u_char
*)LLADDR(sdl
);
1063 snprintf(ebuf
, sizeof (ebuf
), "%x:%x:%x:%x:%x:%x",
1064 cp
[0], cp
[1], cp
[2], cp
[3], cp
[4], cp
[5]);
1066 snprintf(ebuf
, sizeof (ebuf
), "(incomplete)");
1073 ndp_ether_aton(char *a
, u_char
*n
)
1078 len
= sscanf(a
, "%x:%x:%x:%x:%x:%x:%x:%x", &o
[0], &o
[1], &o
[2], &o
[3], &o
[4],
1079 &o
[5], &o
[6], &o
[7]);
1080 if (len
!= 6 && len
!= 8) {
1081 fprintf(stderr
, "ndp: invalid Ethernet address '%s'\n", a
);
1084 for (i
= 0; i
< len
; i
++)
1092 printf("usage: ndp hostname\n");
1093 printf(" ndp -a[lnt]\n");
1094 printf(" ndp [-nt] -A wait\n");
1095 printf(" ndp -c[nt]\n");
1096 printf(" ndp -d[nt] hostname\n");
1097 printf(" ndp -f[nt] filename\n");
1098 printf(" ndp -i interface [flags...]\n");
1099 printf(" ndp -I [interface|delete]\n");
1100 printf(" ndp -p\n");
1101 printf(" ndp -r\n");
1102 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
1103 printf(" ndp -H\n");
1104 printf(" ndp -P\n");
1105 printf(" ndp -R\n");
1106 printf(" ndp -w\n");
1107 printf(" ndp -W cfgfile\n");
1116 register struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
1117 register char *cp
= m_rtmsg
.m_space
;
1121 if (cmd
== RTM_DELETE
)
1123 bzero((char *)&m_rtmsg
, sizeof (m_rtmsg
));
1124 rtm
->rtm_flags
= flags
;
1125 rtm
->rtm_version
= RTM_VERSION
;
1129 fprintf(stderr
, "ndp: internal wrong cmd\n");
1132 rtm
->rtm_addrs
|= RTA_GATEWAY
;
1133 rtm
->rtm_rmx
.rmx_expire
= expire_time
;
1134 rtm
->rtm_inits
= RTV_EXPIRE
;
1135 rtm
->rtm_flags
|= (RTF_HOST
| RTF_STATIC
);
1136 if (rtm
->rtm_flags
& RTF_ANNOUNCE
) {
1137 rtm
->rtm_flags
&= ~RTF_HOST
;
1138 rtm
->rtm_flags
|= RTA_NETMASK
;
1142 rtm
->rtm_addrs
|= RTA_DST
;
1144 #define NEXTADDR(w, s) \
1145 if (rtm->rtm_addrs & (w)) { \
1146 bcopy((char *)&s, cp, sizeof (s)); cp += sizeof (s); \
1149 NEXTADDR(RTA_DST
, sin_m
);
1150 NEXTADDR(RTA_GATEWAY
, sdl_m
);
1151 memset(&so_mask
.sin6_addr
, 0xff, sizeof (so_mask
.sin6_addr
));
1152 NEXTADDR(RTA_NETMASK
, so_mask
);
1154 rtm
->rtm_msglen
= cp
- (char *)&m_rtmsg
;
1156 l
= rtm
->rtm_msglen
;
1157 rtm
->rtm_seq
= ++seq
;
1158 rtm
->rtm_type
= cmd
;
1159 if ((rlen
= write(s
, (char *)&m_rtmsg
, l
)) < 0) {
1160 if (errno
!= ESRCH
|| cmd
!= RTM_DELETE
) {
1161 perror("writing to routing socket");
1166 l
= read(s
, (char *)&m_rtmsg
, sizeof (m_rtmsg
));
1167 } while (l
> 0 && (rtm
->rtm_seq
!= seq
|| rtm
->rtm_pid
!= pid
));
1169 (void) fprintf(stderr
, "ndp: read from routing socket: %s\n",
1175 ifinfo(int argc
, char **argv
)
1177 struct in6_ndireq nd
;
1179 char *ifname
= argv
[0];
1181 u_int8_t nullbuf
[8];
1183 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
1184 perror("ndp: socket");
1187 bzero(&nd
, sizeof (nd
));
1188 strlcpy(nd
.ifname
, ifname
, sizeof (nd
.ifname
));
1189 if (ioctl(s
, SIOCGIFINFO_IN6
, (caddr_t
)&nd
) < 0) {
1190 perror("ioctl (SIOCGIFINFO_IN6)");
1194 newflags
= ND
.flags
;
1195 for (i
= 1; i
< argc
; i
++) {
1204 #define SETFLAG(s, f) \
1206 if (strcmp(cp, (s)) == 0) {\
1213 SETFLAG("nud", ND6_IFF_PERFORMNUD
);
1214 SETFLAG("proxy_prefixes", ND6_IFF_PROXY_PREFIXES
);
1215 SETFLAG("disabled", ND6_IFF_IFDISABLED
);
1216 SETFLAG("insecure", ND6_IFF_INSECURE
);
1217 SETFLAG("replicated", ND6_IFF_REPLICATED
);
1219 ND
.flags
= newflags
;
1220 if (ioctl(s
, SIOCSIFINFO_FLAGS
, (caddr_t
)&nd
) < 0) {
1221 perror("ioctl(SIOCSIFINFO_FLAGS)");
1227 printf("linkmtu=%d", ND
.linkmtu
);
1228 printf(", curhlim=%d", ND
.chlim
);
1229 printf(", basereachable=%ds%dms", ND
.basereachable
/ 1000,
1230 ND
.basereachable
% 1000);
1231 printf(", reachable=%ds", ND
.reachable
);
1232 printf(", retrans=%ds%dms", ND
.retrans
/ 1000, ND
.retrans
% 1000);
1233 memset(nullbuf
, 0, sizeof (nullbuf
));
1234 if (memcmp(nullbuf
, ND
.randomid
, sizeof (nullbuf
)) != 0) {
1236 u_int8_t
*rbuf
= NULL
;
1238 for (i
= 0; i
< 3; i
++) {
1241 printf("\nRandom seed(0): ");
1242 rbuf
= ND
.randomseed0
;
1245 printf("\nRandom seed(1): ");
1246 rbuf
= ND
.randomseed1
;
1249 printf("\nRandom ID: ");
1253 for (j
= 0; j
< 8; j
++)
1254 printf("%02x", rbuf
[j
]);
1258 printf("\nFlags: 0x%x ", ND
.flags
);
1259 if ((ND
.flags
& ND6_IFF_IFDISABLED
) != 0)
1260 printf("IFDISABLED ");
1261 if ((ND
.flags
& ND6_IFF_INSECURE
) != 0)
1262 printf("INSECURE ");
1263 if ((ND
.flags
& ND6_IFF_PERFORMNUD
) != 0)
1264 printf("PERFORMNUD ");
1265 if ((ND
.flags
& ND6_IFF_PROXY_PREFIXES
) != 0)
1266 printf("PROXY_PREFIXES ");
1267 if ((ND
.flags
& ND6_IFF_REPLICATED
) != 0)
1268 printf("REPLICATED ");
1269 if ((ND
.flags
& ND6_IFF_DAD
) != 0)
1278 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1279 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1285 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_ICMPV6
, ICMPV6CTL_ND6_DRLIST
};
1287 struct in6_defrouter
*p
, *ep
;
1289 struct timeval time
;
1291 if (sysctl(mib
, sizeof (mib
) / sizeof (mib
[0]), NULL
, &l
, NULL
, 0)
1293 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1298 errx(1, "not enough core");
1301 if (sysctl(mib
, sizeof (mib
) / sizeof (mib
[0]), buf
, &l
, NULL
, 0) < 0) {
1302 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1306 ep
= (struct in6_defrouter
*)(buf
+ l
);
1307 for (p
= (struct in6_defrouter
*)buf
; p
< ep
; p
++) {
1310 if (getnameinfo((struct sockaddr
*)&p
->rtaddr
,
1311 p
->rtaddr
.sin6_len
, host_buf
, sizeof (host_buf
), NULL
, 0,
1312 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0)) != 0)
1313 strlcpy(host_buf
, "?", sizeof (host_buf
));
1315 printf("%s if=%s", host_buf
, if_indextoname(p
->if_index
,
1317 printf(", flags=%s%s%s%s%s",
1318 p
->stateflags
& NDDRF_IFSCOPE
? "I" : "",
1319 p
->flags
& ND_RA_FLAG_MANAGED
? "M" : "",
1320 p
->flags
& ND_RA_FLAG_OTHER
? "O" : "",
1321 p
->stateflags
& NDDRF_STATIC
? "S" : "",
1322 p
->stateflags
& NDDRF_INSTALLED
? "T" : "");
1323 rtpref
= ((p
->flags
& ND_RA_FLAG_RTPREF_MASK
) >> 3) & 0xff;
1324 printf(", pref=%s", rtpref_str
[rtpref
]);
1326 gettimeofday(&time
, 0);
1328 printf(", expire=Never\n");
1330 printf(", expire=%s\n",
1331 sec2str(p
->expire
- time
.tv_sec
));
1339 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_ICMPV6
, ICMPV6CTL_ND6_PRLIST
};
1341 struct in6_prefix
*p
, *ep
, *n
;
1342 struct sockaddr_in6
*advrtr
;
1344 struct timeval time
;
1345 const int niflags
= NI_NUMERICHOST
| NI_WITHSCOPEID
;
1346 int ninflags
= (nflag
? NI_NUMERICHOST
: 0) | NI_WITHSCOPEID
;
1347 char namebuf
[NI_MAXHOST
];
1349 if (sysctl(mib
, sizeof (mib
) / sizeof (mib
[0]), NULL
, &l
, NULL
, 0)
1351 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1356 errx(1, "not enough core");
1359 if (sysctl(mib
, sizeof (mib
) / sizeof (mib
[0]), buf
, &l
, NULL
, 0)
1361 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1365 ep
= (struct in6_prefix
*)(buf
+ l
);
1366 for (p
= (struct in6_prefix
*)buf
; p
< ep
; p
= n
) {
1367 advrtr
= (struct sockaddr_in6
*)(p
+ 1);
1368 n
= (struct in6_prefix
*)&advrtr
[p
->advrtrs
];
1370 if (getnameinfo((struct sockaddr
*)&p
->prefix
,
1371 p
->prefix
.sin6_len
, namebuf
, sizeof (namebuf
),
1372 NULL
, 0, niflags
) != 0)
1373 strlcpy(namebuf
, "?", sizeof (namebuf
));
1374 printf("%s/%d if=%s\n", namebuf
, p
->prefixlen
,
1375 if_indextoname(p
->if_index
, ifix_buf
));
1377 gettimeofday(&time
, 0);
1379 * meaning of fields, especially flags, is very different
1380 * by origin. notify the difference to the users.
1382 printf("flags=%s%s%s%s%s%s%s",
1383 p
->raflags
.autonomous
? "A" : "",
1384 (p
->flags
& NDPRF_DETACHED
) != 0 ? "D" : "",
1385 (p
->flags
& NDPRF_IFSCOPE
) != 0 ? "I" : "",
1386 p
->raflags
.onlink
? "L" : "",
1387 (p
->flags
& NDPRF_STATIC
) != 0 ? "S" : "",
1388 (p
->flags
& NDPRF_ONLINK
) != 0 ? "O" : "",
1389 (p
->flags
& NDPRF_PRPROXY
) != 0 ? "Y" : "");
1390 if (p
->vltime
== ND6_INFINITE_LIFETIME
)
1391 printf(" vltime=infinity");
1393 printf(" vltime=%ld", (long)p
->vltime
);
1394 if (p
->pltime
== ND6_INFINITE_LIFETIME
)
1395 printf(", pltime=infinity");
1397 printf(", pltime=%ld", (long)p
->pltime
);
1399 printf(", expire=Never");
1400 else if (p
->expire
>= time
.tv_sec
)
1401 printf(", expire=%s",
1402 sec2str(p
->expire
- time
.tv_sec
));
1404 printf(", expired");
1405 printf(", ref=%d", p
->refcnt
);
1408 * "advertising router" list is meaningful only if the prefix
1409 * information is from RA.
1413 struct sockaddr_in6
*sin6
;
1415 sin6
= (struct sockaddr_in6
*)(p
+ 1);
1416 printf(" advertised by\n");
1417 for (j
= 0; j
< p
->advrtrs
; j
++) {
1418 struct in6_nbrinfo
*nbi
;
1420 if (getnameinfo((struct sockaddr
*)sin6
,
1421 sin6
->sin6_len
, namebuf
, sizeof (namebuf
),
1422 NULL
, 0, ninflags
) != 0)
1423 strlcpy(namebuf
, "?", sizeof (namebuf
));
1424 printf(" %s", namebuf
);
1426 nbi
= getnbrinfo(&sin6
->sin6_addr
, p
->if_index
,
1429 switch (nbi
->state
) {
1430 case ND6_LLINFO_REACHABLE
:
1431 case ND6_LLINFO_STALE
:
1432 case ND6_LLINFO_DELAY
:
1433 case ND6_LLINFO_PROBE
:
1434 printf(" (reachable)\n");
1437 printf(" (unreachable)\n");
1440 printf(" (no neighbor state)\n");
1444 printf(" No advertising router\n");
1452 char dummyif
[IFNAMSIZ
+8];
1455 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1457 strlcpy(dummyif
, "lo0", sizeof (dummyif
)); /* dummy */
1458 if (ioctl(s
, SIOCSPFXFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1459 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1465 char dummyif
[IFNAMSIZ
+8];
1468 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1470 strlcpy(dummyif
, "lo0", sizeof (dummyif
)); /* dummy */
1471 if (ioctl(s
, SIOCSRTRFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1472 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1480 char dummyif
[IFNAMSIZ
+8];
1483 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1485 strlcpy(dummyif
, "lo0", sizeof (dummyif
)); /* dummy */
1486 if (ioctl(s
, SIOCSNDFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1487 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1493 setdefif(char *ifname
)
1495 struct in6_ndifreq ndifreq
;
1496 unsigned int ifindex
;
1498 if (strcasecmp(ifname
, "delete") == 0)
1501 if ((ifindex
= if_nametoindex(ifname
)) == 0)
1502 err(1, "failed to resolve i/f index for %s", ifname
);
1505 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1508 strlcpy(ndifreq
.ifname
, "lo0", sizeof (ndifreq
.ifname
)); /* dummy */
1509 ndifreq
.ifindex
= ifindex
;
1511 if (ioctl(s
, SIOCSDEFIFACE_IN6
, (caddr_t
)&ndifreq
) < 0)
1512 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1520 struct in6_ndifreq ndifreq
;
1521 char ifname
[IFNAMSIZ
+8];
1523 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1526 memset(&ndifreq
, 0, sizeof (ndifreq
));
1527 strlcpy(ndifreq
.ifname
, "lo0", sizeof (ndifreq
.ifname
)); /* dummy */
1529 if (ioctl(s
, SIOCGDEFIFACE_IN6
, (caddr_t
)&ndifreq
) < 0)
1530 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1532 if (ndifreq
.ifindex
== 0)
1533 printf("No default interface.\n");
1535 if ((if_indextoname(ndifreq
.ifindex
, ifname
)) == NULL
)
1536 err(1, "failed to resolve ifname for index %lu",
1538 printf("ND default interface = %s\n", ifname
);
1545 sec2str(time_t total
)
1547 static char result
[256];
1548 int days
, hours
, mins
, secs
;
1552 days
= total
/ 3600 / 24;
1553 hours
= (total
/ 3600) % 24;
1554 mins
= (total
/ 60) % 60;
1559 p
+= snprintf(p
, sizeof (result
) - (p
- result
), "%dd", days
);
1561 if (!first
|| hours
) {
1563 p
+= snprintf(p
, sizeof (result
) - (p
- result
), "%dh", hours
);
1566 p
+= snprintf(p
, sizeof (result
) - (p
- result
), "%dm", mins
);
1567 snprintf(p
, sizeof (result
) - (p
- result
), "%ds", secs
);
1573 * Print the timestamp
1574 * from tcpdump/util.c
1577 ts_print(const struct timeval
*tvp
)
1582 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
1583 printf("%02d:%02d:%02d.%06u ", s
/ 3600, (s
% 3600) / 60, s
% 60,
1584 (u_int32_t
)tvp
->tv_usec
);
1587 #define SYSCTL_CGA_PARAMETERS_BUFFER_SIZE \
1588 2 * (sizeof (size_t) + IN6_CGA_KEY_MAXSIZE) + \
1589 sizeof (struct in6_cga_prepare)
1592 read_cga_parameters(void)
1594 static char oldb
[SYSCTL_CGA_PARAMETERS_BUFFER_SIZE
];
1597 struct in6_cga_nodecfg cfg
;
1602 unsigned int column
;
1606 error
= sysctlbyname("net.inet6.send.cga_parameters", oldb
, &oldn
,
1609 err(1, "sysctlbyname");
1612 printf("No CGA parameters.\n");
1618 memset(&cfg
, 0, sizeof (cfg
));
1620 if (oldp
+ sizeof (cfg
.cga_prepare
) > finp
)
1621 err(1, "format error[1]");
1623 memcpy(&cfg
.cga_prepare
, oldp
, sizeof (cfg
.cga_prepare
));
1624 oldp
+= sizeof (cfg
.cga_prepare
);
1626 iov
= &cfg
.cga_pubkey
;
1628 if (oldp
+ sizeof (u16
) > finp
)
1629 err(1, "format error[2]");
1631 memcpy(&u16
, oldp
, sizeof (u16
));
1632 oldp
+= sizeof (u16
);
1635 if (oldp
+ iov
->iov_len
> finp
)
1636 err(1, "format error[3]");
1638 iov
->iov_base
= (void *)oldp
;
1639 oldp
+= iov
->iov_len
;
1642 err(1, "format error[4]");
1644 puts("Public Key:");
1645 finp
= &iov
->iov_base
[iov
->iov_len
];
1647 oldp
= iov
->iov_base
;
1648 while (oldp
< finp
) {
1651 printf("%02x", (unsigned char) *oldp
++);
1661 oldp
= (const char*) cfg
.cga_prepare
.cga_modifier
.octets
;
1662 finp
= &oldp
[sizeof (cfg
.cga_prepare
.cga_modifier
.octets
)];
1664 while (oldp
< finp
) {
1667 printf("%02x", (unsigned char) *oldp
++);
1670 printf("Security Level: %u\n", cfg
.cga_prepare
.cga_security_level
);
1674 write_cga_parameters(const char filename
[])
1676 static char newb
[SYSCTL_CGA_PARAMETERS_BUFFER_SIZE
];
1682 fp
= fopen(filename
, "r");
1684 err(1, "opening '%s' for reading.", filename
);
1686 newn
= fread(newb
, 1, sizeof (newb
), fp
);
1688 err(1, "parameters too large");
1690 if (fclose(fp
) != 0)
1691 err(1, "closing file.");
1694 error
= sysctlbyname("net.inet6.send.cga_parameters", NULL
, NULL
, newb
,
1697 err(1, "sysctlbyname");