1 /* $Id: racoonctl.c,v 1.2.2.1 2005/04/21 09:07:20 monas Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
43 #include <System/net/pfkeyv2.h>
45 #include <net/pfkeyv2.h>
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
57 # include <sys/time.h>
67 #include <sys/ioctl.h>
74 #include "racoonctl.h"
80 #include "isakmp_var.h"
82 #include "isakmp_xauth.h"
83 #include "isakmp_cfg.h"
84 #include "isakmp_unity.h"
85 #include "ipsec_doi.h"
88 char *adminsock_path
= ADMINSOCK_PATH
;
90 static void usage
__P((void));
91 static vchar_t
*get_combuf
__P((int, char **));
92 static int handle_recv
__P((vchar_t
*));
93 static vchar_t
*f_reload
__P((int, char **));
94 static vchar_t
*f_getsched
__P((int, char **));
95 static vchar_t
*f_getsa
__P((int, char **));
96 static vchar_t
*f_flushsa
__P((int, char **));
97 static vchar_t
*f_deletesa
__P((int, char **));
98 static vchar_t
*f_exchangesa
__P((int, char **));
99 static vchar_t
*f_vpnc
__P((int, char **));
100 static vchar_t
*f_vpnd
__P((int, char **));
101 static vchar_t
*f_getevt
__P((int, char **));
104 vchar_t
*(*func
) __P((int, char **));
108 { f_reload
, ADMIN_RELOAD_CONF
, "reload-config" },
109 { f_reload
, ADMIN_RELOAD_CONF
, "rc" },
110 { f_getsched
, ADMIN_SHOW_SCHED
, "show-schedule" },
111 { f_getsched
, ADMIN_SHOW_SCHED
, "sc" },
112 { f_getsa
, ADMIN_SHOW_SA
, "show-sa" },
113 { f_getsa
, ADMIN_SHOW_SA
, "ss" },
114 { f_flushsa
, ADMIN_FLUSH_SA
, "flush-sa" },
115 { f_flushsa
, ADMIN_FLUSH_SA
, "fs" },
116 { f_deletesa
, ADMIN_DELETE_SA
, "delete-sa" },
117 { f_deletesa
, ADMIN_DELETE_SA
, "ds" },
118 { f_exchangesa
, ADMIN_ESTABLISH_SA
, "establish-sa" },
119 { f_exchangesa
, ADMIN_ESTABLISH_SA
, "es" },
120 { f_vpnc
, ADMIN_ESTABLISH_SA
, "vpn-connect" },
121 { f_vpnc
, ADMIN_ESTABLISH_SA
, "vc" },
122 { f_vpnd
, ADMIN_DELETE_ALL_SA_DST
,"vpn-disconnect" },
123 { f_vpnd
, ADMIN_DELETE_ALL_SA_DST
,"vd" },
124 { f_getevt
, ADMIN_SHOW_EVT
, "show-event" },
125 { f_getevt
, ADMIN_SHOW_EVT
, "se" },
132 enum { UNSPEC
, ERROR
, INFO
} level
;
134 { EVTT_PHASE1_UP
, "Phase 1 established", INFO
},
135 { EVTT_PHASE1_DOWN
, "Phase 1 deleted", INFO
},
136 { EVTT_XAUTH_SUCCESS
, "Xauth exchange passed", INFO
},
137 { EVTT_ISAKMP_CFG_DONE
, "ISAKMP mode config done", INFO
},
138 { EVTT_PHASE2_UP
, "Phase 2 established", INFO
},
139 { EVTT_PHASE2_DOWN
, "Phase 2 deleted", INFO
},
140 { EVTT_DPD_TIMEOUT
, "Peer not reachable anymore", ERROR
},
141 { EVTT_PEER_NO_RESPONSE
, "Peer not responding", ERROR
},
142 { EVTT_PEER_DELETE
, "Peer terminated security association", ERROR
},
143 { EVTT_RACOON_QUIT
, "Raccon terminated", ERROR
},
144 { EVTT_OVERFLOW
, "Event queue overflow", ERROR
},
145 { EVTT_XAUTH_FAILED
, "Xauth exchange failed", ERROR
},
146 { EVTT_PEERPH1AUTH_FAILED
, "Peer failed phase 1 authentication "
147 "(certificate problem?)", ERROR
},
151 static int get_proto
__P((char *));
152 static vchar_t
*get_index
__P((int, char **));
153 static int get_family
__P((char *));
154 static vchar_t
*get_comindexes
__P((int, int, char **));
155 static int get_comindex
__P((char *, char **, char **, char **));
156 static int get_ulproto
__P((char *));
162 { ADMIN_PROTO_ISAKMP
, "isakmp" },
163 { ADMIN_PROTO_IPSEC
, "ipsec" },
164 { ADMIN_PROTO_AH
, "ah" },
165 { ADMIN_PROTO_ESP
, "esp" },
166 { ADMIN_PROTO_INTERNAL
, "internal" },
175 { IPPROTO_ICMP
, "icmp" },
176 { IPPROTO_TCP
, "tcp" },
177 { IPPROTO_UDP
, "udp" },
183 static char _addr1_
[NI_MAXHOST
], _addr2_
[NI_MAXHOST
];
188 #define EVTF_NONE 0x0000 /* Ignore any events */
189 #define EVTF_LOOP 0x0001 /* Loop awaiting for new events */
190 #define EVTF_CFG_STOP 0x0002 /* Stop after ISAKMP mode config */
191 #define EVTF_CFG 0x0004 /* Print ISAKMP mode config info */
192 #define EVTF_ALL 0x0008 /* Print any events */
193 #define EVTF_PURGE 0x0010 /* Print all available events */
194 #define EVTF_PH1DOWN_STOP 0x0020 /* Stop when phase 1 SA gets down */
195 #define EVTF_PH1DOWN 0x0040 /* Print that phase 1 SA got down */
196 #define EVTF_ERR 0x0080 /* Print any error */
197 #define EVTF_ERR_STOP 0x0100 /* Stop on any error */
199 int evt_filter
= EVTF_NONE
;
202 void dump_isakmp_sa
__P((char *, int));
203 void dump_internal
__P((char *, int));
204 char *pindex_isakmp
__P((isakmp_index
*));
205 void print_schedule
__P((caddr_t
, int));
206 void print_evt
__P((caddr_t
, int));
207 void print_cfg
__P((caddr_t
, int));
208 void print_err
__P((caddr_t
, int));
209 void print_ph1down
__P((caddr_t
, int));
210 int evt_poll
__P((void));
211 char * fixed_addr
__P((char *, char *, int));
218 " %s reload-config\n"
219 " %s [-l [-l]] show-sa [protocol]\n"
220 " %s flush-sa [protocol]\n"
221 " %s delete-sa <saopts>\n"
222 " %s establish-sa [-u identity] <saopts>\n"
223 " %s vpn-connect [-u identity] vpn_gateway\n"
224 " %s vpn-disconnect vpn_gateway\n"
226 " <protocol>: \"isakmp\", \"esp\" or \"ah\".\n"
227 " In the case of \"show-sa\" or \"flush-sa\", you can use \"ipsec\".\n"
229 " <saopts>: \"isakmp\" <family> <src> <dst>\n"
230 " : {\"esp\",\"ah\"} <family> <src/prefixlen/port> <dst/prefixlen/port>\n"
232 " <family>: \"inet\" or \"inet6\"\n"
233 " <ul_proto>: \"icmp\", \"tcp\", \"udp\" or \"any\"\n",
234 pname
, pname
, pname
, pname
, pname
, pname
, pname
);
238 * Check for proper racoonctl interface
240 #if ((RACOONCTL_INTERFACE_MAJOR != 1) || (RACOONCTL_INTERFACE < 20041230))
241 #error "Incompatible racoonctl interface"
255 * Check for proper racoonctl interface
257 if ((racoonctl_interface_major
!= RACOONCTL_INTERFACE_MAJOR
) ||
258 (racoonctl_interface
< RACOONCTL_INTERFACE
))
259 errx(1, "Incompatible racoonctl interface");
263 * Disable GNU extensions that will prevent racoonct vc -u login
264 * from working (GNU getopt(3) does not like options after vc)
266 setenv("POSIXLY_CORRECT", "1", 0);
268 while ((c
= getopt(ac
, av
, "lds:")) != -1) {
279 adminsock_path
= optarg
;
291 combuf
= get_combuf(ac
, av
);
296 hexdump(combuf
, ((struct admin_com
*)combuf
)->ac_len
);
300 if (com_send(combuf
) != 0)
305 if (com_recv(&combuf
) != 0)
307 if (handle_recv(combuf
) != 0)
312 if (evt_filter
!= EVTF_NONE
)
328 if ((sendbuf
= f_getevt(0, NULL
)) == NULL
)
329 errx(1, "Cannot make combuf");
331 while (evt_filter
& (EVTF_LOOP
|EVTF_PURGE
)) {
333 if (com_send(sendbuf
) != 0)
334 errx(1, "Cannot send combuf");
336 if (com_recv(&recvbuf
) == 0) {
337 handle_recv(recvbuf
);
343 (void)select(0, NULL
, NULL
, NULL
, &tv
);
352 * return command buffer.
366 /* checking the string of command. */
367 for (cp
= &cmdtab
[0]; cp
->str
; cp
++) {
368 if (strcmp(*av
, cp
->str
) == 0) {
373 printf("Invalid command [%s]\n", *av
);
380 return (cp
->func
)(ac
, av
);
389 struct admin_com
*head
;
391 buf
= vmalloc(sizeof(*head
));
393 errx(1, "not enough core");
395 head
= (struct admin_com
*)buf
->v
;
396 head
->ac_len
= buf
->l
;
397 head
->ac_cmd
= ADMIN_RELOAD_CONF
;
410 struct admin_com
*head
;
413 * There are 3 ways of getting here
414 * 1) racoonctl vc => evt_filter = (EVTF_LOOP|EVTF_CFG| ... )
415 * 2) racoonctl es => evt_filter = EVTF_NONE
416 * 3) racoonctl es -l => evt_filter = EVTF_LOOP
417 * Catch the second case: show-event is here to purge all
419 if (evt_filter
== EVTF_NONE
)
420 evt_filter
= (EVTF_ALL
|EVTF_PURGE
);
422 if ((ac
>= 1) && (strcmp(av
[0], "-l") == 0))
423 evt_filter
|= EVTF_LOOP
;
426 errx(1, "too many arguments");
428 buf
= vmalloc(sizeof(*head
));
430 errx(1, "not enough core");
432 head
= (struct admin_com
*)buf
->v
;
433 head
->ac_len
= buf
->l
;
434 head
->ac_cmd
= ADMIN_SHOW_EVT
;
447 struct admin_com
*head
;
449 buf
= vmalloc(sizeof(*head
));
451 errx(1, "not enough core");
453 head
= (struct admin_com
*)buf
->v
;
454 head
->ac_len
= buf
->l
;
455 head
->ac_cmd
= ADMIN_SHOW_SCHED
;
468 struct admin_com
*head
;
473 errx(1, "insufficient arguments");
474 proto
= get_proto(*av
);
476 errx(1, "unknown protocol %s", *av
);
478 buf
= vmalloc(sizeof(*head
));
480 errx(1, "not enough core");
482 head
= (struct admin_com
*)buf
->v
;
483 head
->ac_len
= buf
->l
;
484 head
->ac_cmd
= ADMIN_SHOW_SA
;
486 head
->ac_proto
= proto
;
497 struct admin_com
*head
;
502 errx(1, "insufficient arguments");
503 proto
= get_proto(*av
);
505 errx(1, "unknown protocol %s", *av
);
507 buf
= vmalloc(sizeof(*head
));
509 errx(1, "not enough core");
511 head
= (struct admin_com
*)buf
->v
;
512 head
->ac_len
= buf
->l
;
513 head
->ac_cmd
= ADMIN_FLUSH_SA
;
515 head
->ac_proto
= proto
;
525 vchar_t
*buf
, *index
;
526 struct admin_com
*head
;
531 errx(1, "insufficient arguments");
532 proto
= get_proto(*av
);
534 errx(1, "unknown protocol %s", *av
);
540 case ADMIN_PROTO_ISAKMP
:
541 index
= get_index(ac
, av
);
546 case ADMIN_PROTO_ESP
:
547 index
= get_index(ac
, av
);
552 errno
= EPROTONOSUPPORT
;
556 buf
= vmalloc(sizeof(*head
) + index
->l
);
560 head
= (struct admin_com
*)buf
->v
;
561 head
->ac_len
= buf
->l
+ index
->l
;
562 head
->ac_cmd
= ADMIN_DELETE_SA
;
564 head
->ac_proto
= proto
;
566 memcpy(buf
->v
+sizeof(*head
), index
->v
, index
->l
);
572 f_deleteallsadst(ac
, av
)
576 vchar_t
*buf
, *index
;
577 struct admin_com
*head
;
582 errx(1, "insufficient arguments");
583 proto
= get_proto(*av
);
585 errx(1, "unknown protocol %s", *av
);
591 case ADMIN_PROTO_ISAKMP
:
592 index
= get_index(ac
, av
);
597 case ADMIN_PROTO_ESP
:
598 index
= get_index(ac
, av
);
603 errno
= EPROTONOSUPPORT
;
607 buf
= vmalloc(sizeof(*head
) + index
->l
);
611 head
= (struct admin_com
*)buf
->v
;
612 head
->ac_len
= buf
->l
+ index
->l
;
613 head
->ac_cmd
= ADMIN_DELETE_ALL_SA_DST
;
615 head
->ac_proto
= proto
;
617 memcpy(buf
->v
+sizeof(*head
), index
->v
, index
->l
);
627 vchar_t
*buf
, *index
;
628 struct admin_com
*head
;
630 int cmd
= ADMIN_ESTABLISH_SA
;
634 struct admin_com_psk
*acp
;
637 errx(1, "insufficient arguments");
639 /* Optional -u identity */
640 if (strcmp(av
[0], "-u") == 0) {
642 errx(1, "-u require an argument");
645 if ((key
= getpass("Password: ")) == NULL
)
646 errx(1, "getpass() failed: %s", strerror(errno
));
648 com_len
+= sizeof(*acp
) + strlen(id
) + 1 + strlen(key
) + 1;
649 cmd
= ADMIN_ESTABLISH_SA_PSK
;
657 errx(1, "insufficient arguments");
658 if ((proto
= get_proto(*av
)) == -1)
659 errx(1, "unknown protocol %s", *av
);
665 case ADMIN_PROTO_ISAKMP
:
666 index
= get_index(ac
, av
);
671 case ADMIN_PROTO_ESP
:
672 index
= get_index(ac
, av
);
677 errno
= EPROTONOSUPPORT
;
681 com_len
+= sizeof(*head
) + index
->l
;
682 if ((buf
= vmalloc(com_len
)) == NULL
)
683 errx(1, "Cannot allocate buffer");
685 head
= (struct admin_com
*)buf
->v
;
686 head
->ac_len
= buf
->l
;
689 head
->ac_proto
= proto
;
691 memcpy(buf
->v
+sizeof(*head
), index
->v
, index
->l
);
695 acp
= (struct admin_com_psk
*)
696 (buf
->v
+ sizeof(*head
) + index
->l
);
698 acp
->id_type
= IDTYPE_LOGIN
;
699 acp
->id_len
= strlen(id
) + 1;
700 acp
->key_len
= strlen(key
) + 1;
702 data
= (char *)(acp
+ 1);
705 data
= (char *)(data
+ acp
->id_len
);
717 char *nav
[] = {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
719 char *isakmp
= "isakmp";
722 struct addrinfo hints
, *res
;
723 struct sockaddr
*src
;
727 errx(1, "insufficient arguments");
729 evt_filter
= (EVTF_LOOP
|EVTF_CFG
|EVTF_CFG_STOP
|EVTF_ERR
|EVTF_ERR_STOP
);
732 /* Optional -u identity */
733 if (strcmp(av
[0], "-u") == 0) {
735 errx(1, "-u require an argument");
745 errx(1, "VPN gateway required");
747 warnx("Extra arguments");
750 * Find the source address
752 memset(&hints
, 0, sizeof(hints
));
753 hints
.ai_family
= PF_UNSPEC
;
754 hints
.ai_socktype
= SOCK_DGRAM
;
755 if (getaddrinfo(av
[0], "4500", &hints
, &res
) != 0)
756 errx(1, "Cannot resolve destination address");
758 if ((src
= getlocaladdr(res
->ai_addr
)) == NULL
)
759 errx(1, "cannot find source address");
761 if ((srcaddr
= saddr2str(src
)) == NULL
)
762 errx(1, "cannot read source address");
764 /* We get "ip[port]" strip the port */
765 if ((idx
= index(srcaddr
, '[')) == NULL
)
766 errx(1, "unexpected source address format");
771 nav
[nac
++] = srcaddr
;
774 return f_exchangesa(nac
, nav
);
782 char *nav
[] = {NULL
, NULL
, NULL
, NULL
};
784 char *isakmp
= "isakmp";
786 char *anyaddr
= "0.0.0.0";
788 vchar_t
*buf
, *index
;
791 errx(1, "VPN gateway required");
793 warnx("Extra arguments");
796 (EVTF_PH1DOWN
|EVTF_PH1DOWN_STOP
|EVTF_LOOP
|EVTF_ERR
|EVTF_ERR_STOP
);
800 nav
[nac
++] = anyaddr
;
803 return f_deleteallsadst(nac
, nav
);
811 struct proto_tag
*cp
;
818 /* checking the string of command. */
819 for (cp
= &prototab
[0]; cp
->str
; cp
++) {
820 if (strcmp(str
, cp
->str
) == 0)
835 if (ac
!= 3 && ac
!= 4) {
840 /* checking the string of family */
841 family
= get_family(*av
);
847 return get_comindexes(family
, ac
, av
);
854 if (strcmp("inet", str
) == 0)
857 else if (strcmp("inet6", str
) == 0)
860 errno
= EAFNOSUPPORT
;
865 get_comindexes(family
, ac
, av
)
871 struct admin_com_indexes
*ci
;
872 char *p_name
= NULL
, *p_port
= NULL
;
873 char *p_prefs
= NULL
, *p_prefd
= NULL
;
874 struct sockaddr
*src
= NULL
, *dst
= NULL
;
877 if (ac
!= 2 && ac
!= 3) {
882 if (get_comindex(*av
, &p_name
, &p_port
, &p_prefs
) == -1)
884 src
= get_sockaddr(family
, p_name
, p_port
);
897 if (get_comindex(*av
, &p_name
, &p_port
, &p_prefd
) == -1)
899 dst
= get_sockaddr(family
, p_name
, p_port
);
911 buf
= vmalloc(sizeof(*ci
));
918 ulproto
= get_ulproto(*av
);
924 ci
= (struct admin_com_indexes
*)buf
->v
;
926 ci
->prefs
= (u_int8_t
)atoi(p_prefs
); /* XXX should be handled error. */
930 ci
->prefd
= (u_int8_t
)atoi(p_prefd
); /* XXX should be handled error. */
933 ci
->ul_proto
= ulproto
;
934 memcpy(&ci
->src
, src
, sysdep_sa_len(src
));
935 memcpy(&ci
->dst
, dst
, sysdep_sa_len(dst
));
948 racoon_free(p_prefs
);
950 racoon_free(p_prefd
);
955 get_comindex(str
, name
, port
, pref
)
956 char *str
, **name
, **port
, **pref
;
960 *name
= *port
= *pref
= NULL
;
963 p
= strpbrk(*name
, "/[");
965 if (*(p
+ 1) == '\0')
969 *pref
= strdup(p
+ 1);
970 p
= strchr(*pref
, '[');
972 if (*(p
+ 1) == '\0')
975 *port
= strdup(p
+ 1);
976 p
= strchr(*pref
, ']');
981 } else if (*p
== '[') {
983 *port
= strdup(p
+ 1);
984 p
= strchr(*pref
, ']');
1003 *name
= *port
= *pref
= NULL
;
1011 struct ulproto_tag
*cp
;
1018 /* checking the string of upper layer protocol. */
1019 for (cp
= &ulprototab
[0]; cp
->str
; cp
++) {
1020 if (strcmp(str
, cp
->str
) == 0)
1021 return cp
->ul_proto
;
1030 dump_isakmp_sa(buf
, len
)
1039 /* isakmp status header */
1041 1234567890123456789012 0000000000000000:0000000000000000 000000000000
1044 "Destination Cookies Created";
1046 /* semi long header;
1047 1234567890123456789012 0000000000000000:0000000000000000 00 X 00 X 0000-00-00 00:00:00 000000
1050 "Destination Cookies ST S V E Created Phase2";
1053 0000:0000:0000:0000:0000:0000:0000:0000.00000 0000:0000:0000:0000:0000:0000:0000:0000.00000 0000000000000000:0000000000000000 00 X 00 X 0000-00-00 00:00:00 000000
1056 "Source Destination Cookies ST S V E Created Phase2";
1058 /* phase status header */
1060 side stats source address destination address
1061 xxx xxxxx 1234567890123456789012 1234567890123456789012
1064 static char *estr
[] = { "", "B", "M", "U", "A", "I", };
1066 switch (long_format
) {
1068 printf("%s\n", header1
);
1071 printf("%s\n", header2
);
1075 printf("%s\n", header3
);
1079 if (len
% sizeof(*pd
))
1080 printf("invalid length %d\n", len
);
1083 pd
= (struct ph1dump
*)buf
;
1086 /* source address */
1087 if (long_format
>= 2) {
1088 GETNAMEINFO((struct sockaddr
*)&pd
->local
, _addr1_
, _addr2_
);
1089 switch (long_format
) {
1093 p
= fixed_addr(_addr1_
, _addr2_
, 22);
1097 p
= fixed_addr(_addr1_
, _addr2_
, 45);
1103 /* destination address */
1104 GETNAMEINFO((struct sockaddr
*)&pd
->remote
, _addr1_
, _addr2_
);
1105 switch (long_format
) {
1108 p
= fixed_addr(_addr1_
, _addr2_
, 22);
1112 p
= fixed_addr(_addr1_
, _addr2_
, 45);
1117 printf("%s ", pindex_isakmp(&pd
->index
));
1119 /* statuc, side and version */
1120 if (long_format
>= 1) {
1121 printf("%2d %c %2x ",
1123 pd
->side
== INITIATOR
? 'I' : 'R',
1125 if (ARRAYLEN(estr
) > pd
->etype
)
1126 printf("%s ", estr
[pd
->etype
]);
1131 tm
= localtime(&pd
->created
);
1132 strftime(tbuf
, sizeof(tbuf
), "%Y-%m-%d %T", tm
);
1134 snprintf(tbuf
, sizeof(tbuf
), " ");
1135 printf("%s ", tbuf
);
1137 /* counter of phase 2 */
1138 if (long_format
>= 1)
1139 printf("%6d ", pd
->ph2cnt
);
1151 dump_internal(buf
, tlen
)
1155 struct ph2handle
*iph2
;
1156 struct sockaddr
*addr
;
1160 source address destination address
1161 1234567890123456789012 1234567890123456789012
1164 "Source Destination ";
1168 source address destination address
1169 123456789012345678901234567890123456789012345 123456789012345678901234567890123456789012345
1170 0000:0000:0000:0000:0000:0000:0000:0000.00000 0000:0000:0000:0000:0000:0000:0000:0000.00000 0000:0000:0000:0000:0000:0000:0000:0000.00000
1173 "Source Destination ";
1175 printf("%s\n", long_format
? long_h1
: short_h1
);
1178 iph2
= (struct ph2handle
*)buf
;
1179 addr
= (struct sockaddr
*)(++iph2
);
1181 GETNAMEINFO(addr
, _addr1_
, _addr2_
);
1182 printf("%s ", long_format
?
1183 fixed_addr(_addr1_
, _addr2_
, 45)
1184 : fixed_addr(_addr1_
, _addr2_
, 22));
1186 tlen
-= sysdep_sa_len(addr
);
1188 GETNAMEINFO(addr
, _addr1_
, _addr2_
);
1189 printf("%s ", long_format
?
1190 fixed_addr(_addr1_
, _addr2_
, 45)
1191 : fixed_addr(_addr1_
, _addr2_
, 22));
1193 tlen
-= sysdep_sa_len(addr
);
1203 pindex_isakmp(index
)
1204 isakmp_index
*index
;
1206 static char buf
[64];
1210 memset(buf
, 0, sizeof(buf
));
1213 p
= (u_char
*)index
;
1214 for (j
= 0, i
= 0; i
< sizeof(isakmp_index
); i
++) {
1215 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, "%02x", p
[i
]);
1229 /* print schedule */
1230 char *str_sched_stat
[] = {
1236 char *str_sched_id
[] = {
1245 print_schedule(buf
, len
)
1249 struct scheddump
*sc
= (struct scheddump
*)buf
;
1253 if (len
% sizeof(*sc
))
1254 printf("invalid length %d\n", len
);
1257 /* 00000000 00000000 00000000 xxx........*/
1258 printf("index tick xtime created\n");
1261 tm
= localtime(&sc
->created
);
1262 strftime(tbuf
, sizeof(tbuf
), "%Y-%m-%d %T", tm
);
1264 printf("%-8ld %-8ld %-8ld %s\n",
1281 struct evtdump
*evtdump
= (struct evtdump
*)buf
;
1286 for (i
= 0; evtmsg
[i
].msg
; i
++)
1287 if (evtmsg
[i
].type
== evtdump
->type
)
1290 if (evtmsg
[i
].msg
== NULL
)
1291 printf("Event %d: ", evtdump
->type
);
1293 printf("%s : ", evtmsg
[i
].msg
);
1295 if ((srcstr
= saddr2str((struct sockaddr
*)&evtdump
->src
)) == NULL
)
1298 printf("%s", srcstr
);
1300 if ((dststr
= saddr2str((struct sockaddr
*)&evtdump
->dst
)) == NULL
)
1303 printf("%s", dststr
);
1314 struct evtdump
*evtdump
= (struct evtdump
*)buf
;
1318 for (i
= 0; evtmsg
[i
].msg
; i
++)
1319 if (evtmsg
[i
].type
== evtdump
->type
)
1322 if (evtmsg
[i
].level
!= ERROR
)
1325 if (evtmsg
[i
].msg
== NULL
)
1326 printf("Error: Event %d\n", evtdump
->type
);
1328 printf("Error: %s\n", evtmsg
[i
].msg
);
1330 if (evt_filter
& EVTF_ERR_STOP
)
1331 evt_filter
&= ~EVTF_LOOP
;
1337 * Print a message when phase 1 SA goes down
1340 print_ph1down(buf
, len
)
1344 struct evtdump
*evtdump
= (struct evtdump
*)buf
;
1346 if (evtdump
->type
!= EVTT_PHASE1_DOWN
)
1349 printf("VPN connexion terminated\n");
1351 if (evt_filter
& EVTF_PH1DOWN_STOP
)
1352 evt_filter
&= ~EVTF_LOOP
;
1358 * Print ISAKMP mode config info (IP and banner)
1365 struct evtdump
*evtdump
= (struct evtdump
*)buf
;
1366 struct isakmp_data
*attr
;
1367 char *banner
= NULL
;
1368 struct in_addr addr4
;
1370 memset(&addr4
, 0, sizeof(addr4
));
1372 if (evtdump
->type
!= EVTT_ISAKMP_CFG_DONE
)
1375 len
-= sizeof(*evtdump
);
1376 attr
= (struct isakmp_data
*)(evtdump
+ 1);
1379 if (len
< sizeof(*attr
)) {
1380 printf("short attribute too short\n");
1384 if ((ntohs(attr
->type
) & ISAKMP_GEN_MASK
) == ISAKMP_GEN_TV
) {
1385 /* Short attribute, skip */
1386 len
-= sizeof(*attr
);
1388 } else { /* Long attribute */
1391 if (len
< (sizeof(*attr
) + ntohs(attr
->lorv
))) {
1392 printf("long attribute too long\n");
1396 switch (ntohs(attr
->type
) & ~ISAKMP_GEN_MASK
) {
1397 case INTERNAL_IP4_ADDRESS
:
1398 if (ntohs(attr
->lorv
) < sizeof(addr4
)) {
1399 printf("addr4 attribute too short\n");
1402 memcpy(&addr4
, attr
+ 1, sizeof(addr4
));
1406 banner
= racoon_malloc(ntohs(attr
->lorv
) + 1);
1407 if (banner
== NULL
) {
1408 printf("malloc failed\n");
1411 memcpy(banner
, attr
+ 1, ntohs(attr
->lorv
));
1412 banner
[ntohs(attr
->lorv
)] = '\0';
1419 len
-= (sizeof(*attr
) + ntohs(attr
->lorv
));
1421 attr
= (struct isakmp_data
*)
1422 (n
+ sizeof(*attr
) + ntohs(attr
->lorv
));
1426 printf("Bound to address %s\n", inet_ntoa(addr4
));
1432 if (ioctl(1, TIOCGWINSZ
, &win
) != 1)
1435 for (i
= 0; i
< col
; i
++)
1437 printf("\n%s\n", banner
);
1438 for (i
= 0; i
< col
; i
++)
1443 if (evt_filter
& EVTF_CFG_STOP
)
1444 evt_filter
&= ~EVTF_LOOP
;
1451 fixed_addr(addr
, port
, len
)
1455 static char _addr_buf_
[BUFSIZ
];
1460 memset(_addr_buf_
, ' ', sizeof(_addr_buf_
));
1462 plen
= strlen(port
);
1467 for (i
= 0; i
< len
- plen
- 1 && addr
[i
] != '\0'; /*noting*/)
1471 for (i
= 0; i
< plen
&& port
[i
] != '\0'; /*noting*/)
1474 _addr_buf_
[len
] = '\0';
1483 struct admin_com h
, *com
;
1487 com
= (struct admin_com
*)combuf
->v
;
1488 len
= com
->ac_len
- sizeof(*com
);
1489 buf
= combuf
->v
+ sizeof(*com
);
1491 switch (com
->ac_cmd
) {
1492 case ADMIN_SHOW_SCHED
:
1493 print_schedule(buf
, len
);
1496 case ADMIN_SHOW_EVT
: {
1497 struct evtdump
*evtdump
;
1499 /* We got no event */
1501 /* If we were purging the queue, it is now done */
1502 if (evt_filter
& EVTF_PURGE
)
1503 evt_filter
&= ~EVTF_PURGE
;
1507 if (len
< sizeof(struct evtdump
))
1508 errx(1, "Short buffer\n");
1510 /* Toss outdated events */
1511 evtdump
= (struct evtdump
*)buf
;
1512 if (evtdump
->timestamp
< evt_start
)
1515 if (evt_filter
& EVTF_ALL
)
1516 print_evt(buf
, len
);
1517 if (evt_filter
& EVTF_ERR
)
1518 print_err(buf
, len
);
1519 if (evt_filter
& EVTF_CFG
)
1520 print_cfg(buf
, len
);
1521 if (evt_filter
& EVTF_PH1DOWN
)
1522 print_ph1down(buf
, len
);
1528 switch (com
->ac_proto
) {
1529 case ADMIN_PROTO_ISAKMP
:
1530 dump_isakmp_sa(buf
, len
);
1532 case ADMIN_PROTO_IPSEC
:
1533 case ADMIN_PROTO_AH
:
1534 case ADMIN_PROTO_ESP
:
1536 struct sadb_msg
*msg
= (struct sadb_msg
*)buf
;
1538 switch (msg
->sadb_msg_errno
) {
1540 switch (msg
->sadb_msg_type
) {
1543 printf("No entry.\n");
1546 printf("No SAD entries.\n");
1553 if (msg
->sadb_msg_seq
== 0)
1555 msg
= (struct sadb_msg
*)((caddr_t
)msg
+
1556 PFKEY_UNUNIT64(msg
->sadb_msg_len
));
1560 printf("%s.\n", strerror(msg
->sadb_msg_errno
));
1564 case ADMIN_PROTO_INTERNAL
:
1565 dump_internal(buf
, len
);
1568 printf("Invalid proto [%d]\n", com
->ac_proto
);