2 * Copyright (c) 2009-2013 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];
403 bzero(&hints
, sizeof (hints
));
404 hints
.ai_family
= AF_INET6
;
405 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
407 fprintf(stderr
, "ndp: %s: %s\n", host
,
408 gai_strerror(gai_error
));
411 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
413 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
414 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
415 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
418 ea
= (u_char
*)LLADDR(&sdl_m
);
419 if (ndp_ether_aton(eaddr
, ea
) == 0)
421 flags
= expire_time
= 0;
423 if (strncmp(argv
[0], "temp", 4) == 0) {
425 gettimeofday(&time
, 0);
426 expire_time
= time
.tv_sec
+ 20 * 60;
427 } else if (strncmp(argv
[0], "proxy", 5) == 0)
428 flags
|= RTF_ANNOUNCE
;
431 if (rtmsg(RTM_GET
) < 0) {
435 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
436 sdl
= (struct sockaddr_dl
*)(ROUNDUP(sin
->sin6_len
) + (char *)sin
);
437 if (IN6_ARE_ADDR_EQUAL(&sin
->sin6_addr
, &sin_m
.sin6_addr
)) {
438 if (sdl
->sdl_family
== AF_LINK
&&
439 (rtm
->rtm_flags
& RTF_LLINFO
) &&
440 !(rtm
->rtm_flags
& RTF_GATEWAY
)) switch (sdl
->sdl_type
) {
441 case IFT_ETHER
: case IFT_FDDI
: case IFT_ISO88023
:
442 case IFT_ISO88024
: case IFT_ISO88025
:
446 * IPv4 arp command retries with sin_other = SIN_PROXY here.
448 fprintf(stderr
, "set: cannot configure a new entry\n");
453 if (sdl
->sdl_family
!= AF_LINK
) {
454 printf("cannot intuit interface index and type for %s\n", host
);
457 sdl_m
.sdl_type
= sdl
->sdl_type
;
458 sdl_m
.sdl_index
= sdl
->sdl_index
;
459 return (rtmsg(RTM_ADD
));
463 * Display an individual neighbor cache entry
468 struct sockaddr_in6
*sin
= &sin_m
;
469 struct addrinfo hints
, *res
;
473 bzero(&hints
, sizeof (hints
));
474 hints
.ai_family
= AF_INET6
;
475 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
477 fprintf(stderr
, "ndp: %s: %s\n", host
,
478 gai_strerror(gai_error
));
481 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
483 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
484 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
485 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
488 dump(&sin
->sin6_addr
);
489 if (found_entry
== 0) {
490 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
491 sizeof (host_buf
), NULL
, 0, NI_WITHSCOPEID
| (nflag
?
492 NI_NUMERICHOST
: 0));
493 printf("%s (%s) -- no entry\n", host
, host_buf
);
499 * Delete a neighbor cache entry
504 struct sockaddr_in6
*sin
= &sin_m
;
505 register struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
506 struct sockaddr_dl
*sdl
;
507 struct addrinfo hints
, *res
;
513 bzero(&hints
, sizeof (hints
));
514 hints
.ai_family
= AF_INET6
;
515 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
517 fprintf(stderr
, "ndp: %s: %s\n", host
,
518 gai_strerror(gai_error
));
521 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
523 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
524 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
525 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
528 if (rtmsg(RTM_GET
) < 0) {
532 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
533 sdl
= (struct sockaddr_dl
*)(ROUNDUP(sin
->sin6_len
) + (char *)sin
);
534 if (IN6_ARE_ADDR_EQUAL(&sin
->sin6_addr
, &sin_m
.sin6_addr
)) {
535 if (sdl
->sdl_family
== AF_LINK
&&
536 (rtm
->rtm_flags
& RTF_LLINFO
) &&
537 !(rtm
->rtm_flags
& RTF_GATEWAY
)) {
541 * IPv4 arp command retries with sin_other = SIN_PROXY here.
543 fprintf(stderr
, "delete: cannot delete non-NDP entry\n");
548 if (sdl
->sdl_family
!= AF_LINK
) {
549 printf("cannot locate %s\n", host
);
552 if (rtmsg(RTM_DELETE
) == 0) {
553 struct sockaddr_in6 s6
= *sin
;
556 if (IN6_IS_ADDR_LINKLOCAL(&s6
.sin6_addr
)) {
558 ntohs(*(u_int16_t
*)&s6
.sin6_addr
.s6_addr
[2]);
559 *(u_int16_t
*)&s6
.sin6_addr
.s6_addr
[2] = 0;
562 getnameinfo((struct sockaddr
*)&s6
,
563 s6
.sin6_len
, host_buf
,
564 sizeof (host_buf
), NULL
, 0,
565 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
566 printf("%s (%s) deleted\n", host
, host_buf
);
577 * Dump the entire neighbor cache
580 dump(struct in6_addr
*addr
)
584 char *lim
, *buf
, *next
;
585 struct rt_msghdr
*rtm
;
586 struct sockaddr_in6
*sin
;
587 struct sockaddr_dl
*sdl
;
588 struct in6_nbrinfo
*nbi
;
597 if (!tflag
&& !cflag
)
598 printf("%-*.*s %-*.*s %*.*s %-9.9s %2s %4s %4s\n",
599 W_ADDR
, W_ADDR
, "Neighbor", W_LL
, W_LL
, "Linklayer Address",
600 W_IF
, W_IF
, "Netif", "Expire", "St", "Flgs", "Prbs");
607 mib
[4] = NET_RT_FLAGS
;
609 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
610 err(1, "sysctl(PF_ROUTE estimate)");
612 if ((buf
= malloc(needed
)) == NULL
)
614 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
615 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
620 for (next
= buf
; next
&& next
< lim
; next
+= rtm
->rtm_msglen
) {
621 int isrouter
= 0, prbs
= 0;
623 rtm
= (struct rt_msghdr
*)next
;
624 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
625 sdl
= (struct sockaddr_dl
*)((char *)sin
+
626 ROUNDUP(sin
->sin6_len
));
629 * Some OSes can produce a route that has the LINK flag but
630 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
631 * and BSD/OS, where xx is not the interface identifier on
632 * lo0). Such routes entry would annoy getnbrinfo() below,
634 * XXX: such routes should have the GATEWAY flag, not the
635 * LINK flag. However, there are rotten routing software
636 * that advertises all routes that have the GATEWAY flag.
637 * Thus, KAME kernel intentionally does not set the LINK flag.
638 * What is to be fixed is not ndp, but such routing software
639 * (and the kernel workaround)...
641 if (sdl
->sdl_family
!= AF_LINK
)
645 if (!IN6_ARE_ADDR_EQUAL(addr
, &sin
->sin6_addr
))
648 } else if (IN6_IS_ADDR_MULTICAST(&sin
->sin6_addr
))
650 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
) ||
651 IN6_IS_ADDR_MC_NODELOCAL(&sin
->sin6_addr
) ||
652 IN6_IS_ADDR_MC_LINKLOCAL(&sin
->sin6_addr
)) {
653 /* should scope id be filled in the kernel? */
654 if (sin
->sin6_scope_id
== 0)
655 sin
->sin6_scope_id
= sdl
->sdl_index
;
657 /* KAME specific hack; removed the embedded id */
658 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] = 0;
661 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
662 sizeof (host_buf
), NULL
, 0,
663 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
665 if (rtm
->rtm_flags
& RTF_WASCLONED
)
669 gettimeofday(&time
, 0);
673 addrwidth
= strlen(host_buf
);
674 if (addrwidth
< W_ADDR
)
676 llwidth
= strlen(ether_str(sdl
));
677 if (W_ADDR
+ W_LL
- addrwidth
> llwidth
)
678 llwidth
= W_ADDR
+ W_LL
- addrwidth
;
679 ifname
= if_indextoname(sdl
->sdl_index
, ifix_buf
);
682 ifwidth
= strlen(ifname
);
683 if (W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
> ifwidth
)
684 ifwidth
= W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
;
686 printf("%-*.*s %-*.*s %*.*s", addrwidth
, addrwidth
, host_buf
,
687 llwidth
, llwidth
, ether_str(sdl
), ifwidth
, ifwidth
, ifname
);
689 /* Print neighbor discovery specific informations */
690 nbi
= getnbrinfo(&sin
->sin6_addr
, sdl
->sdl_index
, 1);
692 if (nbi
->expire
> time
.tv_sec
) {
693 printf(" %-9.9s", sec2str(nbi
->expire
-
695 } else if (nbi
->expire
== 0)
696 printf(" %-9.9s", "permanent");
698 printf(" %-9.9s", "expired");
700 switch (nbi
->state
) {
701 case ND6_LLINFO_NOSTATE
:
704 case ND6_LLINFO_INCOMPLETE
:
707 case ND6_LLINFO_REACHABLE
:
710 case ND6_LLINFO_STALE
:
713 case ND6_LLINFO_DELAY
:
716 case ND6_LLINFO_PROBE
:
724 isrouter
= nbi
->isrouter
;
727 warnx("failed to get neighbor information");
733 * other flags. R: router, P: proxy, W: ??
735 if ((rtm
->rtm_addrs
& RTA_NETMASK
) == 0) {
736 snprintf(flgbuf
, sizeof (flgbuf
), "%s%s",
738 (rtm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
740 sin
= (struct sockaddr_in6
*)
741 (sdl
->sdl_len
+ (char *)sdl
);
742 snprintf(flgbuf
, sizeof (flgbuf
), "%s%s%s%s",
744 !IN6_IS_ADDR_UNSPECIFIED(&sin
->sin6_addr
)
746 (sin
->sin6_len
!= sizeof (struct sockaddr_in6
))
748 (rtm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
750 printf(" %-4.4s", flgbuf
);
753 printf(" %4d", prbs
);
768 * Dump the entire neighbor cache (extended)
771 dump_ext(addr
, xflag
)
772 struct in6_addr
*addr
;
777 char *lim
, *buf
, *next
;
778 struct rt_msghdr_ext
*ertm
;
779 struct sockaddr_in6
*sin
;
780 struct sockaddr_dl
*sdl
;
781 struct in6_nbrinfo
*nbi
;
790 if (!tflag
&& !cflag
) {
791 printf("%-*.*s %-*.*s %*.*s %-9.9s %-9.9s %2s %4s %4s",
792 W_ADDR
, W_ADDR
, "Neighbor", W_LL
, W_LL
, "Linklayer Address",
793 W_IF
, W_IF
, "Netif", "Expire(O)", "Expire(I)", "St",
796 printf(" %-7.7s %-7.7s %-7.7s", "RSSI", "LQM", "NPM");
805 mib
[4] = NET_RT_DUMPX_FLAGS
;
807 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
808 err(1, "sysctl(PF_ROUTE estimate)");
810 if ((buf
= malloc(needed
)) == NULL
)
812 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
813 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
818 for (next
= buf
; next
&& next
< lim
; next
+= ertm
->rtm_msglen
) {
819 int isrouter
= 0, prbs
= 0;
821 ertm
= (struct rt_msghdr_ext
*)next
;
822 sin
= (struct sockaddr_in6
*)(ertm
+ 1);
823 sdl
= (struct sockaddr_dl
*)((char *)sin
+
824 ROUNDUP(sin
->sin6_len
));
827 * Some OSes can produce a route that has the LINK flag but
828 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
829 * and BSD/OS, where xx is not the interface identifier on
830 * lo0). Such routes entry would annoy getnbrinfo() below,
832 * XXX: such routes should have the GATEWAY flag, not the
833 * LINK flag. However, there are rotten routing software
834 * that advertises all routes that have the GATEWAY flag.
835 * Thus, KAME kernel intentionally does not set the LINK flag.
836 * What is to be fixed is not ndp, but such routing software
837 * (and the kernel workaround)...
839 if (sdl
->sdl_family
!= AF_LINK
)
843 if (!IN6_ARE_ADDR_EQUAL(addr
, &sin
->sin6_addr
))
846 } else if (IN6_IS_ADDR_MULTICAST(&sin
->sin6_addr
))
848 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
) ||
849 IN6_IS_ADDR_MC_NODELOCAL(&sin
->sin6_addr
) ||
850 IN6_IS_ADDR_MC_LINKLOCAL(&sin
->sin6_addr
)) {
851 /* should scope id be filled in the kernel? */
852 if (sin
->sin6_scope_id
== 0)
853 sin
->sin6_scope_id
= sdl
->sdl_index
;
855 /* KAME specific hack; removed the embedded id */
856 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] = 0;
859 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
860 sizeof (host_buf
), NULL
, 0,
861 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
863 if (ertm
->rtm_flags
& RTF_WASCLONED
)
867 gettimeofday(&time
, 0);
871 addrwidth
= strlen(host_buf
);
872 if (addrwidth
< W_ADDR
)
874 llwidth
= strlen(ether_str(sdl
));
875 if (W_ADDR
+ W_LL
- addrwidth
> llwidth
)
876 llwidth
= W_ADDR
+ W_LL
- addrwidth
;
877 ifname
= if_indextoname(sdl
->sdl_index
, ifix_buf
);
880 ifwidth
= strlen(ifname
);
881 if (W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
> ifwidth
)
882 ifwidth
= W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
;
884 printf("%-*.*s %-*.*s %*.*s", addrwidth
, addrwidth
, host_buf
,
885 llwidth
, llwidth
, ether_str(sdl
), ifwidth
, ifwidth
, ifname
);
887 if (ertm
->rtm_ri
.ri_refcnt
== 0 ||
888 ertm
->rtm_ri
.ri_snd_expire
== 0)
889 printf(" %-9.9s", "(none)");
890 else if (ertm
->rtm_ri
.ri_snd_expire
> time
.tv_sec
)
892 sec2str(ertm
->rtm_ri
.ri_snd_expire
- time
.tv_sec
));
894 printf(" %-9.9s", "expired");
896 if (ertm
->rtm_ri
.ri_refcnt
== 0 ||
897 ertm
->rtm_ri
.ri_rcv_expire
== 0)
898 printf(" %-9.9s", "(none)");
899 else if (ertm
->rtm_ri
.ri_rcv_expire
> time
.tv_sec
)
901 sec2str(ertm
->rtm_ri
.ri_rcv_expire
- time
.tv_sec
));
903 printf(" %-9.9s", "expired");
905 /* Print neighbor discovery specific informations */
906 nbi
= getnbrinfo(&sin
->sin6_addr
, sdl
->sdl_index
, 1);
908 switch (nbi
->state
) {
909 case ND6_LLINFO_NOSTATE
:
912 case ND6_LLINFO_INCOMPLETE
:
915 case ND6_LLINFO_REACHABLE
:
918 case ND6_LLINFO_STALE
:
921 case ND6_LLINFO_DELAY
:
924 case ND6_LLINFO_PROBE
:
932 isrouter
= nbi
->isrouter
;
935 warnx("failed to get neighbor information");
941 * other flags. R: router, P: proxy, W: ??
943 if ((ertm
->rtm_addrs
& RTA_NETMASK
) == 0) {
944 snprintf(flgbuf
, sizeof (flgbuf
), "%s%s",
946 (ertm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
948 sin
= (struct sockaddr_in6
*)
949 (sdl
->sdl_len
+ (char *)sdl
);
950 snprintf(flgbuf
, sizeof (flgbuf
), "%s%s%s%s",
952 !IN6_IS_ADDR_UNSPECIFIED(&sin
->sin6_addr
)
954 (sin
->sin6_len
!= sizeof (struct sockaddr_in6
))
956 (ertm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
958 printf(" %-4.4s", flgbuf
);
961 printf(" %4d", prbs
);
965 printf(" %-4.4s", "none");
967 if (ertm
->rtm_ri
.ri_rssi
!= IFNET_RSSI_UNKNOWN
)
968 printf(" %7d", ertm
->rtm_ri
.ri_rssi
);
970 printf(" %-7.7s", "unknown");
972 switch (ertm
->rtm_ri
.ri_lqm
)
974 case IFNET_LQM_THRESH_OFF
:
975 printf(" %-7.7s", "off");
977 case IFNET_LQM_THRESH_UNKNOWN
:
978 printf(" %-7.7s", "unknown");
980 case IFNET_LQM_THRESH_POOR
:
981 printf(" %-7.7s", "poor");
983 case IFNET_LQM_THRESH_GOOD
:
984 printf(" %-7.7s", "good");
987 printf(" %7d", ertm
->rtm_ri
.ri_lqm
);
991 switch (ertm
->rtm_ri
.ri_npm
)
993 case IFNET_NPM_THRESH_UNKNOWN
:
994 printf(" %-7.7s", "unknown");
996 case IFNET_NPM_THRESH_NEAR
:
997 printf(" %-7.7s", "near");
999 case IFNET_NPM_THRESH_GENERAL
:
1000 printf(" %-7.7s", "general");
1002 case IFNET_NPM_THRESH_FAR
:
1003 printf(" %-7.7s", "far");
1006 printf(" %7d", ertm
->rtm_ri
.ri_npm
);
1023 static struct in6_nbrinfo
*
1024 getnbrinfo(addr
, ifindex
, warning
)
1025 struct in6_addr
*addr
;
1029 static struct in6_nbrinfo nbi
;
1032 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1035 bzero(&nbi
, sizeof (nbi
));
1036 if_indextoname(ifindex
, nbi
.ifname
);
1038 if (ioctl(s
, SIOCGNBRINFO_IN6
, (caddr_t
)&nbi
) < 0) {
1040 warn("ioctl(SIOCGNBRINFO_IN6)");
1050 ether_str(struct sockaddr_dl
*sdl
)
1052 static char ebuf
[32];
1055 if (sdl
->sdl_alen
) {
1056 cp
= (u_char
*)LLADDR(sdl
);
1057 snprintf(ebuf
, sizeof (ebuf
), "%x:%x:%x:%x:%x:%x",
1058 cp
[0], cp
[1], cp
[2], cp
[3], cp
[4], cp
[5]);
1060 snprintf(ebuf
, sizeof (ebuf
), "(incomplete)");
1067 ndp_ether_aton(char *a
, u_char
*n
)
1071 i
= sscanf(a
, "%x:%x:%x:%x:%x:%x", &o
[0], &o
[1], &o
[2], &o
[3], &o
[4],
1074 fprintf(stderr
, "ndp: invalid Ethernet address '%s'\n", a
);
1077 for (i
= 0; i
< 6; i
++)
1085 printf("usage: ndp hostname\n");
1086 printf(" ndp -a[lnt]\n");
1087 printf(" ndp [-nt] -A wait\n");
1088 printf(" ndp -c[nt]\n");
1089 printf(" ndp -d[nt] hostname\n");
1090 printf(" ndp -f[nt] filename\n");
1091 printf(" ndp -i interface [flags...]\n");
1092 printf(" ndp -I [interface|delete]\n");
1093 printf(" ndp -p\n");
1094 printf(" ndp -r\n");
1095 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
1096 printf(" ndp -H\n");
1097 printf(" ndp -P\n");
1098 printf(" ndp -R\n");
1099 printf(" ndp -w\n");
1100 printf(" ndp -W cfgfile\n");
1109 register struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
1110 register char *cp
= m_rtmsg
.m_space
;
1114 if (cmd
== RTM_DELETE
)
1116 bzero((char *)&m_rtmsg
, sizeof (m_rtmsg
));
1117 rtm
->rtm_flags
= flags
;
1118 rtm
->rtm_version
= RTM_VERSION
;
1122 fprintf(stderr
, "ndp: internal wrong cmd\n");
1125 rtm
->rtm_addrs
|= RTA_GATEWAY
;
1126 rtm
->rtm_rmx
.rmx_expire
= expire_time
;
1127 rtm
->rtm_inits
= RTV_EXPIRE
;
1128 rtm
->rtm_flags
|= (RTF_HOST
| RTF_STATIC
);
1129 if (rtm
->rtm_flags
& RTF_ANNOUNCE
) {
1130 rtm
->rtm_flags
&= ~RTF_HOST
;
1131 rtm
->rtm_flags
|= RTA_NETMASK
;
1135 rtm
->rtm_addrs
|= RTA_DST
;
1137 #define NEXTADDR(w, s) \
1138 if (rtm->rtm_addrs & (w)) { \
1139 bcopy((char *)&s, cp, sizeof (s)); cp += sizeof (s); \
1142 NEXTADDR(RTA_DST
, sin_m
);
1143 NEXTADDR(RTA_GATEWAY
, sdl_m
);
1144 memset(&so_mask
.sin6_addr
, 0xff, sizeof (so_mask
.sin6_addr
));
1145 NEXTADDR(RTA_NETMASK
, so_mask
);
1147 rtm
->rtm_msglen
= cp
- (char *)&m_rtmsg
;
1149 l
= rtm
->rtm_msglen
;
1150 rtm
->rtm_seq
= ++seq
;
1151 rtm
->rtm_type
= cmd
;
1152 if ((rlen
= write(s
, (char *)&m_rtmsg
, l
)) < 0) {
1153 if (errno
!= ESRCH
|| cmd
!= RTM_DELETE
) {
1154 perror("writing to routing socket");
1159 l
= read(s
, (char *)&m_rtmsg
, sizeof (m_rtmsg
));
1160 } while (l
> 0 && (rtm
->rtm_seq
!= seq
|| rtm
->rtm_pid
!= pid
));
1162 (void) fprintf(stderr
, "ndp: read from routing socket: %s\n",
1168 ifinfo(int argc
, char **argv
)
1170 struct in6_ndireq nd
;
1172 char *ifname
= argv
[0];
1174 u_int8_t nullbuf
[8];
1176 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
1177 perror("ndp: socket");
1180 bzero(&nd
, sizeof (nd
));
1181 strlcpy(nd
.ifname
, ifname
, sizeof (nd
.ifname
));
1182 if (ioctl(s
, SIOCGIFINFO_IN6
, (caddr_t
)&nd
) < 0) {
1183 perror("ioctl (SIOCGIFINFO_IN6)");
1187 newflags
= ND
.flags
;
1188 for (i
= 1; i
< argc
; i
++) {
1197 #define SETFLAG(s, f) \
1199 if (strcmp(cp, (s)) == 0) {\
1206 SETFLAG("nud", ND6_IFF_PERFORMNUD
);
1207 SETFLAG("proxy_prefixes", ND6_IFF_PROXY_PREFIXES
);
1208 SETFLAG("ignore_na", ND6_IFF_IGNORE_NA
);
1209 SETFLAG("disabled", ND6_IFF_IFDISABLED
);
1210 SETFLAG("insecure", ND6_IFF_INSECURE
);
1212 ND
.flags
= newflags
;
1213 if (ioctl(s
, SIOCSIFINFO_FLAGS
, (caddr_t
)&nd
) < 0) {
1214 perror("ioctl(SIOCSIFINFO_FLAGS)");
1220 printf("linkmtu=%d", ND
.linkmtu
);
1221 printf(", curhlim=%d", ND
.chlim
);
1222 printf(", basereachable=%ds%dms", ND
.basereachable
/ 1000,
1223 ND
.basereachable
% 1000);
1224 printf(", reachable=%ds", ND
.reachable
);
1225 printf(", retrans=%ds%dms", ND
.retrans
/ 1000, ND
.retrans
% 1000);
1226 memset(nullbuf
, 0, sizeof (nullbuf
));
1227 if (memcmp(nullbuf
, ND
.randomid
, sizeof (nullbuf
)) != 0) {
1229 u_int8_t
*rbuf
= NULL
;
1231 for (i
= 0; i
< 3; i
++) {
1234 printf("\nRandom seed(0): ");
1235 rbuf
= ND
.randomseed0
;
1238 printf("\nRandom seed(1): ");
1239 rbuf
= ND
.randomseed1
;
1242 printf("\nRandom ID: ");
1246 for (j
= 0; j
< 8; j
++)
1247 printf("%02x", rbuf
[j
]);
1251 printf("\nFlags: ");
1252 if ((ND
.flags
& ND6_IFF_IFDISABLED
) != 0)
1253 printf("IFDISABLED ");
1254 if ((ND
.flags
& ND6_IFF_IGNORE_NA
) != 0)
1255 printf("IGNORE_NA ");
1256 if ((ND
.flags
& ND6_IFF_INSECURE
) != 0)
1257 printf("INSECURE ");
1258 if ((ND
.flags
& ND6_IFF_PERFORMNUD
) != 0)
1259 printf("PERFORMNUD ");
1260 if ((ND
.flags
& ND6_IFF_PROXY_PREFIXES
) != 0)
1261 printf("PROXY_PREFIXES ");
1269 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1270 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1276 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_ICMPV6
, ICMPV6CTL_ND6_DRLIST
};
1278 struct in6_defrouter
*p
, *ep
;
1280 struct timeval time
;
1282 if (sysctl(mib
, sizeof (mib
) / sizeof (mib
[0]), NULL
, &l
, NULL
, 0)
1284 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1289 errx(1, "not enough core");
1292 if (sysctl(mib
, sizeof (mib
) / sizeof (mib
[0]), buf
, &l
, NULL
, 0) < 0) {
1293 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1297 ep
= (struct in6_defrouter
*)(buf
+ l
);
1298 for (p
= (struct in6_defrouter
*)buf
; p
< ep
; p
++) {
1301 if (getnameinfo((struct sockaddr
*)&p
->rtaddr
,
1302 p
->rtaddr
.sin6_len
, host_buf
, sizeof (host_buf
), NULL
, 0,
1303 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0)) != 0)
1304 strlcpy(host_buf
, "?", sizeof (host_buf
));
1306 printf("%s if=%s", host_buf
, if_indextoname(p
->if_index
,
1308 printf(", flags=%s%s%s%s%s",
1309 p
->stateflags
& NDDRF_IFSCOPE
? "I" : "",
1310 p
->flags
& ND_RA_FLAG_MANAGED
? "M" : "",
1311 p
->flags
& ND_RA_FLAG_OTHER
? "O" : "",
1312 p
->stateflags
& NDDRF_STATIC
? "S" : "",
1313 p
->stateflags
& NDDRF_INSTALLED
? "T" : "");
1314 rtpref
= ((p
->flags
& ND_RA_FLAG_RTPREF_MASK
) >> 3) & 0xff;
1315 printf(", pref=%s", rtpref_str
[rtpref
]);
1317 gettimeofday(&time
, 0);
1319 printf(", expire=Never\n");
1321 printf(", expire=%s\n",
1322 sec2str(p
->expire
- time
.tv_sec
));
1330 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_ICMPV6
, ICMPV6CTL_ND6_PRLIST
};
1332 struct in6_prefix
*p
, *ep
, *n
;
1333 struct sockaddr_in6
*advrtr
;
1335 struct timeval time
;
1336 const int niflags
= NI_NUMERICHOST
| NI_WITHSCOPEID
;
1337 int ninflags
= (nflag
? NI_NUMERICHOST
: 0) | NI_WITHSCOPEID
;
1338 char namebuf
[NI_MAXHOST
];
1340 if (sysctl(mib
, sizeof (mib
) / sizeof (mib
[0]), NULL
, &l
, NULL
, 0)
1342 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1347 errx(1, "not enough core");
1350 if (sysctl(mib
, sizeof (mib
) / sizeof (mib
[0]), buf
, &l
, NULL
, 0)
1352 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1356 ep
= (struct in6_prefix
*)(buf
+ l
);
1357 for (p
= (struct in6_prefix
*)buf
; p
< ep
; p
= n
) {
1358 advrtr
= (struct sockaddr_in6
*)(p
+ 1);
1359 n
= (struct in6_prefix
*)&advrtr
[p
->advrtrs
];
1361 if (getnameinfo((struct sockaddr
*)&p
->prefix
,
1362 p
->prefix
.sin6_len
, namebuf
, sizeof (namebuf
),
1363 NULL
, 0, niflags
) != 0)
1364 strlcpy(namebuf
, "?", sizeof (namebuf
));
1365 printf("%s/%d if=%s\n", namebuf
, p
->prefixlen
,
1366 if_indextoname(p
->if_index
, ifix_buf
));
1368 gettimeofday(&time
, 0);
1370 * meaning of fields, especially flags, is very different
1371 * by origin. notify the difference to the users.
1373 printf("flags=%s%s%s%s%s%s%s",
1374 p
->raflags
.autonomous
? "A" : "",
1375 (p
->flags
& NDPRF_DETACHED
) != 0 ? "D" : "",
1376 (p
->flags
& NDPRF_IFSCOPE
) != 0 ? "I" : "",
1377 p
->raflags
.onlink
? "L" : "",
1378 (p
->flags
& NDPRF_STATIC
) != 0 ? "S" : "",
1379 (p
->flags
& NDPRF_ONLINK
) != 0 ? "O" : "",
1380 (p
->flags
& NDPRF_PRPROXY
) != 0 ? "Y" : "");
1381 if (p
->vltime
== ND6_INFINITE_LIFETIME
)
1382 printf(" vltime=infinity");
1384 printf(" vltime=%ld", (long)p
->vltime
);
1385 if (p
->pltime
== ND6_INFINITE_LIFETIME
)
1386 printf(", pltime=infinity");
1388 printf(", pltime=%ld", (long)p
->pltime
);
1390 printf(", expire=Never");
1391 else if (p
->expire
>= time
.tv_sec
)
1392 printf(", expire=%s",
1393 sec2str(p
->expire
- time
.tv_sec
));
1395 printf(", expired");
1396 printf(", ref=%d", p
->refcnt
);
1399 * "advertising router" list is meaningful only if the prefix
1400 * information is from RA.
1404 struct sockaddr_in6
*sin6
;
1406 sin6
= (struct sockaddr_in6
*)(p
+ 1);
1407 printf(" advertised by\n");
1408 for (j
= 0; j
< p
->advrtrs
; j
++) {
1409 struct in6_nbrinfo
*nbi
;
1411 if (getnameinfo((struct sockaddr
*)sin6
,
1412 sin6
->sin6_len
, namebuf
, sizeof (namebuf
),
1413 NULL
, 0, ninflags
) != 0)
1414 strlcpy(namebuf
, "?", sizeof (namebuf
));
1415 printf(" %s", namebuf
);
1417 nbi
= getnbrinfo(&sin6
->sin6_addr
, p
->if_index
,
1420 switch (nbi
->state
) {
1421 case ND6_LLINFO_REACHABLE
:
1422 case ND6_LLINFO_STALE
:
1423 case ND6_LLINFO_DELAY
:
1424 case ND6_LLINFO_PROBE
:
1425 printf(" (reachable)\n");
1428 printf(" (unreachable)\n");
1431 printf(" (no neighbor state)\n");
1435 printf(" No advertising router\n");
1443 char dummyif
[IFNAMSIZ
+8];
1446 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1448 strlcpy(dummyif
, "lo0", sizeof (dummyif
)); /* dummy */
1449 if (ioctl(s
, SIOCSPFXFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1450 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1456 char dummyif
[IFNAMSIZ
+8];
1459 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1461 strlcpy(dummyif
, "lo0", sizeof (dummyif
)); /* dummy */
1462 if (ioctl(s
, SIOCSRTRFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1463 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1471 char dummyif
[IFNAMSIZ
+8];
1474 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1476 strlcpy(dummyif
, "lo0", sizeof (dummyif
)); /* dummy */
1477 if (ioctl(s
, SIOCSNDFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1478 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1484 setdefif(char *ifname
)
1486 struct in6_ndifreq ndifreq
;
1487 unsigned int ifindex
;
1489 if (strcasecmp(ifname
, "delete") == 0)
1492 if ((ifindex
= if_nametoindex(ifname
)) == 0)
1493 err(1, "failed to resolve i/f index for %s", ifname
);
1496 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1499 strlcpy(ndifreq
.ifname
, "lo0", sizeof (ndifreq
.ifname
)); /* dummy */
1500 ndifreq
.ifindex
= ifindex
;
1502 if (ioctl(s
, SIOCSDEFIFACE_IN6
, (caddr_t
)&ndifreq
) < 0)
1503 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1511 struct in6_ndifreq ndifreq
;
1512 char ifname
[IFNAMSIZ
+8];
1514 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1517 memset(&ndifreq
, 0, sizeof (ndifreq
));
1518 strlcpy(ndifreq
.ifname
, "lo0", sizeof (ndifreq
.ifname
)); /* dummy */
1520 if (ioctl(s
, SIOCGDEFIFACE_IN6
, (caddr_t
)&ndifreq
) < 0)
1521 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1523 if (ndifreq
.ifindex
== 0)
1524 printf("No default interface.\n");
1526 if ((if_indextoname(ndifreq
.ifindex
, ifname
)) == NULL
)
1527 err(1, "failed to resolve ifname for index %lu",
1529 printf("ND default interface = %s\n", ifname
);
1536 sec2str(time_t total
)
1538 static char result
[256];
1539 int days
, hours
, mins
, secs
;
1543 days
= total
/ 3600 / 24;
1544 hours
= (total
/ 3600) % 24;
1545 mins
= (total
/ 60) % 60;
1550 p
+= snprintf(p
, sizeof (result
) - (p
- result
), "%dd", days
);
1552 if (!first
|| hours
) {
1554 p
+= snprintf(p
, sizeof (result
) - (p
- result
), "%dh", hours
);
1557 p
+= snprintf(p
, sizeof (result
) - (p
- result
), "%dm", mins
);
1558 snprintf(p
, sizeof (result
) - (p
- result
), "%ds", secs
);
1564 * Print the timestamp
1565 * from tcpdump/util.c
1568 ts_print(const struct timeval
*tvp
)
1573 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
1574 printf("%02d:%02d:%02d.%06u ", s
/ 3600, (s
% 3600) / 60, s
% 60,
1575 (u_int32_t
)tvp
->tv_usec
);
1578 #define SYSCTL_CGA_PARAMETERS_BUFFER_SIZE \
1579 2 * (sizeof (size_t) + IN6_CGA_KEY_MAXSIZE) + \
1580 sizeof (struct in6_cga_prepare)
1583 read_cga_parameters(void)
1585 static char oldb
[SYSCTL_CGA_PARAMETERS_BUFFER_SIZE
];
1588 struct in6_cga_nodecfg cfg
;
1593 unsigned int column
;
1597 error
= sysctlbyname("net.inet6.send.cga_parameters", oldb
, &oldn
,
1600 err(1, "sysctlbyname");
1603 printf("No CGA parameters.\n");
1609 memset(&cfg
, 0, sizeof (cfg
));
1611 if (oldp
+ sizeof (cfg
.cga_prepare
) > finp
)
1612 err(1, "format error[1]");
1614 memcpy(&cfg
.cga_prepare
, oldp
, sizeof (cfg
.cga_prepare
));
1615 oldp
+= sizeof (cfg
.cga_prepare
);
1617 iov
= &cfg
.cga_pubkey
;
1619 if (oldp
+ sizeof (u16
) > finp
)
1620 err(1, "format error[2]");
1622 memcpy(&u16
, oldp
, sizeof (u16
));
1623 oldp
+= sizeof (u16
);
1626 if (oldp
+ iov
->iov_len
> finp
)
1627 err(1, "format error[3]");
1629 iov
->iov_base
= (void *)oldp
;
1630 oldp
+= iov
->iov_len
;
1633 err(1, "format error[4]");
1635 puts("Public Key:");
1636 finp
= &iov
->iov_base
[iov
->iov_len
];
1638 oldp
= iov
->iov_base
;
1639 while (oldp
< finp
) {
1642 printf("%02x", (unsigned char) *oldp
++);
1652 oldp
= (const char*) cfg
.cga_prepare
.cga_modifier
.octets
;
1653 finp
= &oldp
[sizeof (cfg
.cga_prepare
.cga_modifier
.octets
)];
1655 while (oldp
< finp
) {
1658 printf("%02x", (unsigned char) *oldp
++);
1661 printf("Security Level: %u\n", cfg
.cga_prepare
.cga_security_level
);
1665 write_cga_parameters(const char filename
[])
1667 static char newb
[SYSCTL_CGA_PARAMETERS_BUFFER_SIZE
];
1673 fp
= fopen(filename
, "r");
1675 err(1, "opening '%s' for reading.", filename
);
1677 newn
= fread(newb
, 1, sizeof (newb
), fp
);
1679 err(1, "parameters too large");
1681 if (fclose(fp
) != 0)
1682 err(1, "closing file.");
1685 error
= sysctlbyname("net.inet6.send.cga_parameters", NULL
, NULL
, newb
,
1688 err(1, "sysctlbyname");