1 /* $NetBSD: racoonctl.c,v 1.7 2006/10/02 07:12:26 manu Exp $ */
3 /* Id: racoonctl.c,v 1.11 2006/04/06 17:06:25 manubsd Exp */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
45 #include <System/net/pfkeyv2.h>
47 #include <net/pfkeyv2.h>
54 #if TIME_WITH_SYS_TIME
55 # include <sys/time.h>
59 # include <sys/time.h>
69 #include <sys/ioctl.h>
77 #include "racoonctl.h"
84 #include "isakmp_var.h"
86 #include "isakmp_xauth.h"
87 #include "isakmp_cfg.h"
88 #include "isakmp_unity.h"
89 #include "ipsec_doi.h"
92 char *adminsock_path
= ADMINSOCK_PATH
;
94 static void usage
__P((void));
95 static vchar_t
*get_combuf
__P((int, char **));
96 static int handle_recv
__P((vchar_t
*));
97 static vchar_t
*f_reload
__P((int, char **));
98 static vchar_t
*f_getsched
__P((int, char **));
99 static vchar_t
*f_getsa
__P((int, char **));
100 static vchar_t
*f_flushsa
__P((int, char **));
101 static vchar_t
*f_deletesa
__P((int, char **));
102 static vchar_t
*f_exchangesa
__P((int, char **));
103 static vchar_t
*f_vpnc
__P((int, char **));
104 static vchar_t
*f_exchangesatest
__P((int, char **));
105 static vchar_t
*f_vpntest
__P((int, char **));
106 static vchar_t
*f_vpnd
__P((int, char **));
107 static vchar_t
*f_getevt
__P((int, char **));
109 static vchar_t
*f_logoutusr
__P((int, char **));
113 vchar_t
*(*func
) __P((int, char **));
117 { f_reload
, ADMIN_RELOAD_CONF
, "reload-config" },
118 { f_reload
, ADMIN_RELOAD_CONF
, "rc" },
119 { f_getsched
, ADMIN_SHOW_SCHED
, "show-schedule" },
120 { f_getsched
, ADMIN_SHOW_SCHED
, "sc" },
121 { f_getsa
, ADMIN_SHOW_SA
, "show-sa" },
122 { f_getsa
, ADMIN_SHOW_SA
, "ss" },
123 { f_flushsa
, ADMIN_FLUSH_SA
, "flush-sa" },
124 { f_flushsa
, ADMIN_FLUSH_SA
, "fs" },
125 { f_deletesa
, ADMIN_DELETE_SA
, "delete-sa" },
126 { f_deletesa
, ADMIN_DELETE_SA
, "ds" },
127 { f_exchangesa
, ADMIN_ESTABLISH_SA
, "establish-sa" },
128 { f_exchangesa
, ADMIN_ESTABLISH_SA
, "es" },
129 { f_vpnc
, ADMIN_ESTABLISH_SA
, "vpn-connect" },
130 { f_vpnc
, ADMIN_ESTABLISH_SA
, "vc" },
131 { f_vpntest
, ADMIN_ESTABLISH_SA_VPNCONTROL
, "vpntest" },
132 { f_vpnd
, ADMIN_DELETE_ALL_SA_DST
,"vpn-disconnect" },
133 { f_vpnd
, ADMIN_DELETE_ALL_SA_DST
,"vd" },
134 { f_getevt
, ADMIN_SHOW_EVT
, "show-event" },
135 { f_getevt
, ADMIN_SHOW_EVT
, "se" },
137 { f_logoutusr
, ADMIN_LOGOUT_USER
, "logout-user" },
138 { f_logoutusr
, ADMIN_LOGOUT_USER
, "lu" },
146 enum { UNSPEC
, ERROR
, INFO
} level
;
148 { EVTT_PHASE1_UP
, "Phase 1 established", INFO
},
149 { EVTT_PHASE1_DOWN
, "Phase 1 deleted", INFO
},
150 { EVTT_XAUTH_SUCCESS
, "Xauth exchange passed", INFO
},
151 { EVTT_ISAKMP_CFG_DONE
, "ISAKMP mode config done", INFO
},
152 { EVTT_PHASE2_UP
, "Phase 2 established", INFO
},
153 { EVTT_PHASE2_DOWN
, "Phase 2 deleted", INFO
},
154 { EVTT_DPD_TIMEOUT
, "Peer not reachable anymore", ERROR
},
155 { EVTT_PEER_NO_RESPONSE
, "Peer not responding", ERROR
},
156 { EVTT_PEER_DELETE
, "Peer terminated security association", ERROR
},
157 { EVTT_RACOON_QUIT
, "Raccon terminated", ERROR
},
158 { EVTT_OVERFLOW
, "Event queue overflow", ERROR
},
159 { EVTT_XAUTH_FAILED
, "Xauth exchange failed", ERROR
},
160 { EVTT_PEERPH1AUTH_FAILED
, "Peer failed phase 1 authentication "
161 "(certificate problem?)", ERROR
},
162 { EVTT_PEERPH1_NOPROP
, "Peer failed phase 1 initiation "
163 "(proposal problem?)", ERROR
},
165 { EVTT_NO_ISAKMP_CFG
, "No need for ISAKMP mode config ", INFO
},
168 static int get_proto
__P((char *));
169 static vchar_t
*get_index
__P((int, char **));
170 static int get_family
__P((char *));
171 static vchar_t
*get_comindexes
__P((int, int, char **));
172 static int get_comindex
__P((char *, char **, char **, char **));
173 static int get_ulproto
__P((char *));
179 { ADMIN_PROTO_ISAKMP
, "isakmp" },
180 { ADMIN_PROTO_IPSEC
, "ipsec" },
181 { ADMIN_PROTO_AH
, "ah" },
182 { ADMIN_PROTO_ESP
, "esp" },
183 { ADMIN_PROTO_INTERNAL
, "internal" },
192 { IPPROTO_ICMP
, "icmp" },
193 { IPPROTO_TCP
, "tcp" },
194 { IPPROTO_UDP
, "udp" },
200 static char _addr1_
[NI_MAXHOST
], _addr2_
[NI_MAXHOST
];
205 #define EVTF_NONE 0x0000 /* Ignore any events */
206 #define EVTF_LOOP 0x0001 /* Loop awaiting for new events */
207 #define EVTF_CFG_STOP 0x0002 /* Stop after ISAKMP mode config */
208 #define EVTF_CFG 0x0004 /* Print ISAKMP mode config info */
209 #define EVTF_ALL 0x0008 /* Print any events */
210 #define EVTF_PURGE 0x0010 /* Print all available events */
211 #define EVTF_PH1DOWN_STOP 0x0020 /* Stop when phase 1 SA gets down */
212 #define EVTF_PH1DOWN 0x0040 /* Print that phase 1 SA got down */
213 #define EVTF_ERR 0x0080 /* Print any error */
214 #define EVTF_ERR_STOP 0x0100 /* Stop on any error */
216 int evt_filter
= EVTF_NONE
;
219 void dump_isakmp_sa
__P((char *, int));
220 void dump_internal
__P((char *, int));
221 char *pindex_isakmp
__P((isakmp_index
*));
222 void print_schedule
__P((caddr_t
, int));
223 void print_evt
__P((caddr_t
, int));
224 void print_cfg
__P((caddr_t
, int));
225 void print_err
__P((caddr_t
, int));
226 void print_ph1down
__P((caddr_t
, int));
227 void print_ph1up
__P((caddr_t
, int));
228 int evt_poll
__P((void));
229 char * fixed_addr
__P((char *, char *, int));
236 " %s reload-config\n"
237 " %s [-l [-l]] show-sa [protocol]\n"
238 " %s flush-sa [protocol]\n"
239 " %s delete-sa <saopts>\n"
240 " %s establish-sa [-u identity] <saopts>\n"
241 " %s vpn-connect [-u identity] vpn_gateway\n"
242 " %s vpn-disconnect vpn_gateway\n"
244 " <protocol>: \"isakmp\", \"esp\" or \"ah\".\n"
245 " In the case of \"show-sa\" or \"flush-sa\", you can use \"ipsec\".\n"
247 " <saopts>: \"isakmp\" <family> <src> <dst>\n"
248 " : {\"esp\",\"ah\"} <family> <src/prefixlen/port> <dst/prefixlen/port>\n"
250 " <family>: \"inet\" or \"inet6\"\n"
251 " <ul_proto>: \"icmp\", \"tcp\", \"udp\" or \"any\"\n",
252 pname
, pname
, pname
, pname
, pname
, pname
, pname
);
256 * Check for proper racoonctl interface
258 #if ((RACOONCTL_INTERFACE_MAJOR != 1) || (RACOONCTL_INTERFACE < 20041230))
259 #error "Incompatible racoonctl interface"
273 * Check for proper racoonctl interface
275 if ((racoonctl_interface_major
!= RACOONCTL_INTERFACE_MAJOR
) ||
276 (racoonctl_interface
< RACOONCTL_INTERFACE
))
277 errx(1, "Incompatible racoonctl interface");
281 * Disable GNU extensions that will prevent racoonct vc -u login
282 * from working (GNU getopt(3) does not like options after vc)
284 setenv("POSIXLY_CORRECT", "1", 0);
286 while ((c
= getopt(ac
, av
, "lds:")) != -1) {
297 adminsock_path
= optarg
;
309 combuf
= get_combuf(ac
, av
);
314 hexdump(combuf
, ((struct admin_com
*)combuf
)->ac_len
);
318 if (com_send(combuf
) != 0)
323 if (com_recv(&combuf
) != 0)
325 if (handle_recv(combuf
) != 0)
330 if (evt_filter
!= EVTF_NONE
)
346 if ((sendbuf
= f_getevt(0, NULL
)) == NULL
)
347 errx(1, "Cannot make combuf");
349 while (evt_filter
& (EVTF_LOOP
|EVTF_PURGE
)) {
350 /* handle_recv closes the socket time, so open it each time */
352 if (com_send(sendbuf
) != 0)
353 errx(1, "Cannot send combuf");
355 if (com_recv(&recvbuf
) == 0) {
356 handle_recv(recvbuf
);
362 (void)select(0, NULL
, NULL
, NULL
, &tv
);
371 * return command buffer.
385 /* checking the string of command. */
386 for (cp
= &cmdtab
[0]; cp
->str
; cp
++) {
387 if (strcmp(*av
, cp
->str
) == 0) {
392 printf("Invalid command [%s]\n", *av
);
399 return (cp
->func
)(ac
, av
);
408 struct admin_com
*head
;
410 buf
= vmalloc(sizeof(*head
));
412 errx(1, "not enough core");
414 head
= (struct admin_com
*)buf
->v
;
415 head
->ac_len
= buf
->l
;
416 head
->ac_cmd
= ADMIN_RELOAD_CONF
;
429 struct admin_com
*head
;
432 * There are 3 ways of getting here
433 * 1) racoonctl vc => evt_filter = (EVTF_LOOP|EVTF_CFG| ... )
434 * 2) racoonctl es => evt_filter = EVTF_NONE
435 * 3) racoonctl es -l => evt_filter = EVTF_LOOP
436 * Catch the second case: show-event is here to purge all
438 if (evt_filter
== EVTF_NONE
)
439 evt_filter
= (EVTF_ALL
|EVTF_PURGE
);
441 if ((ac
>= 1) && (strcmp(av
[0], "-l") == 0))
442 evt_filter
|= EVTF_LOOP
;
445 errx(1, "too many arguments");
447 buf
= vmalloc(sizeof(*head
));
449 errx(1, "not enough core");
451 head
= (struct admin_com
*)buf
->v
;
452 head
->ac_len
= buf
->l
;
453 head
->ac_cmd
= ADMIN_SHOW_EVT
;
466 struct admin_com
*head
;
468 buf
= vmalloc(sizeof(*head
));
470 errx(1, "not enough core");
472 head
= (struct admin_com
*)buf
->v
;
473 head
->ac_len
= buf
->l
;
474 head
->ac_cmd
= ADMIN_SHOW_SCHED
;
487 struct admin_com
*head
;
492 errx(1, "insufficient arguments");
493 proto
= get_proto(*av
);
495 errx(1, "unknown protocol %s", *av
);
497 buf
= vmalloc(sizeof(*head
));
499 errx(1, "not enough core");
501 head
= (struct admin_com
*)buf
->v
;
502 head
->ac_len
= buf
->l
;
503 head
->ac_cmd
= ADMIN_SHOW_SA
;
505 head
->ac_proto
= proto
;
516 struct admin_com
*head
;
521 errx(1, "insufficient arguments");
522 proto
= get_proto(*av
);
524 errx(1, "unknown protocol %s", *av
);
526 buf
= vmalloc(sizeof(*head
));
528 errx(1, "not enough core");
530 head
= (struct admin_com
*)buf
->v
;
531 head
->ac_len
= buf
->l
;
532 head
->ac_cmd
= ADMIN_FLUSH_SA
;
534 head
->ac_proto
= proto
;
544 vchar_t
*buf
, *index
;
545 struct admin_com
*head
;
550 errx(1, "insufficient arguments");
551 proto
= get_proto(*av
);
553 errx(1, "unknown protocol %s", *av
);
559 case ADMIN_PROTO_ISAKMP
:
560 index
= get_index(ac
, av
);
565 case ADMIN_PROTO_ESP
:
566 index
= get_index(ac
, av
);
571 errno
= EPROTONOSUPPORT
;
575 buf
= vmalloc(sizeof(*head
) + index
->l
);
579 head
= (struct admin_com
*)buf
->v
;
580 head
->ac_len
= buf
->l
+ index
->l
;
581 head
->ac_cmd
= ADMIN_DELETE_SA
;
583 head
->ac_proto
= proto
;
585 memcpy(buf
->v
+sizeof(*head
), index
->v
, index
->l
);
595 f_deleteallsadst(ac
, av
)
599 vchar_t
*buf
, *index
;
600 struct admin_com
*head
;
605 errx(1, "insufficient arguments");
606 proto
= get_proto(*av
);
608 errx(1, "unknown protocol %s", *av
);
614 case ADMIN_PROTO_ISAKMP
:
615 index
= get_index(ac
, av
);
620 case ADMIN_PROTO_ESP
:
621 index
= get_index(ac
, av
);
626 errno
= EPROTONOSUPPORT
;
630 buf
= vmalloc(sizeof(*head
) + index
->l
);
634 head
= (struct admin_com
*)buf
->v
;
635 head
->ac_len
= buf
->l
+ index
->l
;
636 head
->ac_cmd
= ADMIN_DELETE_ALL_SA_DST
;
638 head
->ac_proto
= proto
;
640 memcpy(buf
->v
+sizeof(*head
), index
->v
, index
->l
);
654 vchar_t
*buf
, *index
;
655 struct admin_com
*head
;
657 int cmd
= ADMIN_ESTABLISH_SA
;
661 struct admin_com_psk
*acp
;
664 errx(1, "insufficient arguments");
666 /* Optional -u identity */
667 if (strcmp(av
[0], "-u") == 0) {
669 errx(1, "-u require an argument");
672 if ((key
= getpass("Password: ")) == NULL
)
673 errx(1, "getpass() failed: %s", strerror(errno
));
675 com_len
+= sizeof(*acp
) + strlen(id
) + 1 + strlen(key
) + 1;
676 cmd
= ADMIN_ESTABLISH_SA_PSK
;
684 errx(1, "insufficient arguments");
685 if ((proto
= get_proto(*av
)) == -1)
686 errx(1, "unknown protocol %s", *av
);
692 case ADMIN_PROTO_ISAKMP
:
693 index
= get_index(ac
, av
);
698 case ADMIN_PROTO_ESP
:
699 index
= get_index(ac
, av
);
704 errno
= EPROTONOSUPPORT
;
708 com_len
+= sizeof(*head
) + index
->l
;
709 if ((buf
= vmalloc(com_len
)) == NULL
)
710 errx(1, "Cannot allocate buffer");
712 head
= (struct admin_com
*)buf
->v
;
713 head
->ac_len
= buf
->l
;
716 head
->ac_proto
= proto
;
718 memcpy(buf
->v
+sizeof(*head
), index
->v
, index
->l
);
721 // overload com_len to track the number of unused bytes in buf->v
723 acp
= (struct admin_com_psk
*)
724 (buf
->v
+ sizeof(*head
) + index
->l
);
725 com_len
-= sizeof(*head
) + index
->l
;
727 acp
->id_type
= IDTYPE_USERFQDN
;
728 acp
->id_len
= strlen(id
) + 1;
729 acp
->key_len
= strlen(key
) + 1;
731 data
= (char *)(acp
+ 1);
732 com_len
-= sizeof(*acp
);
733 strlcpy(data
, id
, com_len
);
735 data
= (char *)(data
+ acp
->id_len
);
736 com_len
-= acp
->id_len
;
737 strlcpy(data
, key
, com_len
);
747 f_exchangesatest(ac
, av
)
751 vchar_t
*buf
, *index
;
752 struct admin_com
*head
;
754 int cmd
= ADMIN_ESTABLISH_SA_VPNCONTROL
;
758 struct admin_com_psk
*acp
;
761 errx(1, "insufficient arguments");
763 /* Optional -u identity */
764 if (strcmp(av
[0], "-u") == 0) {
766 errx(1, "-u require an argument");
769 if ((key
= getpass("Password: ")) == NULL
)
770 errx(1, "getpass() failed: %s", strerror(errno
));
772 com_len
+= sizeof(*acp
) + strlen(id
) + 1 + strlen(key
) + 1;
773 cmd
= ADMIN_ESTABLISH_SA_VPNCONTROL
;
781 errx(1, "insufficient arguments");
782 if ((proto
= get_proto(*av
)) == -1)
783 errx(1, "unknown protocol %s", *av
);
789 case ADMIN_PROTO_ISAKMP
:
790 index
= get_index(ac
, av
);
795 case ADMIN_PROTO_ESP
:
796 index
= get_index(ac
, av
);
801 errno
= EPROTONOSUPPORT
;
805 com_len
+= sizeof(*head
) + index
->l
;
806 if ((buf
= vmalloc(com_len
)) == NULL
)
807 errx(1, "Cannot allocate buffer");
809 head
= (struct admin_com
*)buf
->v
;
810 head
->ac_len
= buf
->l
;
813 head
->ac_proto
= proto
;
815 memcpy(buf
->v
+sizeof(*head
), index
->v
, index
->l
);
818 // overload com_len to track the number of unused bytes in buf->v
820 acp
= (struct admin_com_psk
*)
821 (buf
->v
+ sizeof(*head
) + index
->l
);
822 com_len
-= sizeof(*head
) + index
->l
;
824 acp
->id_type
= IDTYPE_USERFQDN
;
825 acp
->id_len
= strlen(id
) + 1;
826 acp
->key_len
= strlen(key
) + 1;
828 data
= (char *)(acp
+ 1);
829 strlcpy(data
, id
, com_len
);
831 data
= (char *)(data
+ acp
->id_len
);
832 com_len
-= acp
->id_len
;
833 strlcpy(data
, key
, com_len
);
846 char *nav
[] = {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
848 char *isakmp
= "isakmp";
851 struct addrinfo hints
, *res
;
852 struct sockaddr
*src
;
856 errx(1, "insufficient arguments");
858 evt_filter
= (EVTF_LOOP
|EVTF_CFG
|EVTF_CFG_STOP
|EVTF_ERR
|EVTF_ERR_STOP
);
861 /* Optional -u identity */
862 if (strcmp(av
[0], "-u") == 0) {
864 errx(1, "-u require an argument");
874 errx(1, "VPN gateway required");
876 warnx("Extra arguments");
879 * Find the source address
881 memset(&hints
, 0, sizeof(hints
));
882 hints
.ai_family
= PF_UNSPEC
;
883 hints
.ai_socktype
= SOCK_DGRAM
;
884 if (getaddrinfo(av
[0], "4500", &hints
, &res
) != 0)
885 errx(1, "Cannot resolve destination address");
887 if ((src
= getlocaladdr(res
->ai_addr
)) == NULL
)
888 errx(1, "cannot find source address");
890 if ((srcaddr
= saddr2str(src
)) == NULL
)
891 errx(1, "cannot read source address");
893 /* We get "ip[port]" strip the port */
894 if ((idx
= index(srcaddr
, '[')) == NULL
)
895 errx(1, "unexpected source address format");
900 nav
[nac
++] = srcaddr
;
903 return f_exchangesa(nac
, nav
);
912 char *nav
[] = {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
914 char *isakmp
= "isakmp";
917 struct addrinfo hints
, *res
;
918 struct sockaddr
*src
;
922 errx(1, "insufficient arguments");
924 evt_filter
= (EVTF_LOOP
|EVTF_CFG
|EVTF_CFG_STOP
|EVTF_ERR
|EVTF_ERR_STOP
);
927 /* Optional -u identity */
928 if (strcmp(av
[0], "-u") == 0) {
930 errx(1, "-u require an argument");
940 errx(1, "VPN gateway required");
942 warnx("Extra arguments");
945 * Find the source address
947 memset(&hints
, 0, sizeof(hints
));
948 hints
.ai_family
= PF_UNSPEC
;
949 hints
.ai_socktype
= SOCK_DGRAM
;
950 if (getaddrinfo(av
[0], "4500", &hints
, &res
) != 0)
951 errx(1, "Cannot resolve destination address");
953 if ((src
= getlocaladdr(res
->ai_addr
)) == NULL
)
954 errx(1, "cannot find source address");
956 if ((srcaddr
= saddr2str(src
)) == NULL
)
957 errx(1, "cannot read source address");
959 /* We get "ip[port]" strip the port */
960 if ((idx
= index(srcaddr
, '[')) == NULL
)
961 errx(1, "unexpected source address format");
966 nav
[nac
++] = srcaddr
;
969 return f_exchangesa(nac
, nav
);
977 char *nav
[] = {NULL
, NULL
, NULL
, NULL
};
979 char *isakmp
= "isakmp";
981 char *anyaddr
= "0.0.0.0";
985 errx(1, "VPN gateway required");
987 warnx("Extra arguments");
990 (EVTF_PH1DOWN
|EVTF_PH1DOWN_STOP
|EVTF_LOOP
|EVTF_ERR
|EVTF_ERR_STOP
);
994 nav
[nac
++] = anyaddr
;
997 return f_deleteallsadst(nac
, nav
);
1000 #ifdef ENABLE_HYBRID
1007 struct admin_com
*head
;
1012 errx(1, "insufficient arguments");
1014 if ((user
== NULL
) || ((strlen(user
) + 1) > LOGINLEN
))
1015 errx(1, "bad login (too long?)");
1017 buf
= vmalloc(sizeof(*head
) + LOGINLEN
);
1021 head
= (struct admin_com
*)buf
->v
;
1022 head
->ac_len
= buf
->l
;
1023 head
->ac_cmd
= ADMIN_LOGOUT_USER
;
1027 strlcpy((char *)(head
+ 1), user
, LOGINLEN
);
1031 #endif /* ENABLE_HYBRID */
1038 struct proto_tag
*cp
;
1045 /* checking the string of command. */
1046 for (cp
= &prototab
[0]; cp
->str
; cp
++) {
1047 if (strcmp(str
, cp
->str
) == 0)
1062 if (ac
!= 3 && ac
!= 4) {
1067 /* checking the string of family */
1068 family
= get_family(*av
);
1074 return get_comindexes(family
, ac
, av
);
1081 if (strcmp("inet", str
) == 0)
1084 else if (strcmp("inet6", str
) == 0)
1087 errno
= EAFNOSUPPORT
;
1092 get_comindexes(family
, ac
, av
)
1098 struct admin_com_indexes
*ci
;
1099 char *p_name
= NULL
, *p_port
= NULL
;
1100 char *p_prefs
= NULL
, *p_prefd
= NULL
;
1101 struct sockaddr
*src
= NULL
, *dst
= NULL
;
1104 if (ac
!= 2 && ac
!= 3) {
1109 if (get_comindex(*av
, &p_name
, &p_port
, &p_prefs
) == -1)
1111 src
= get_sockaddr(family
, p_name
, p_port
);
1113 racoon_free(p_name
);
1117 racoon_free(p_port
);
1124 if (get_comindex(*av
, &p_name
, &p_port
, &p_prefd
) == -1)
1126 dst
= get_sockaddr(family
, p_name
, p_port
);
1128 racoon_free(p_name
);
1132 racoon_free(p_port
);
1138 buf
= vmalloc(sizeof(*ci
));
1145 ulproto
= get_ulproto(*av
);
1151 ci
= (struct admin_com_indexes
*)buf
->v
;
1153 ci
->prefs
= (u_int8_t
)atoi(p_prefs
); /* XXX should be handled error. */
1157 ci
->prefd
= (u_int8_t
)atoi(p_prefd
); /* XXX should be handled error. */
1160 ci
->ul_proto
= ulproto
;
1161 memcpy(&ci
->src
, src
, sysdep_sa_len(src
));
1162 memcpy(&ci
->dst
, dst
, sysdep_sa_len(dst
));
1165 racoon_free(p_name
);
1171 racoon_free(p_name
);
1173 racoon_free(p_port
);
1175 racoon_free(p_prefs
);
1177 racoon_free(p_prefd
);
1182 get_comindex(str
, name
, port
, pref
)
1183 char *str
, **name
, **port
, **pref
;
1187 *name
= *port
= *pref
= NULL
;
1189 *name
= racoon_strdup(str
);
1190 STRDUP_FATAL(*name
);
1191 p
= strpbrk(*name
, "/[");
1193 if (*(p
+ 1) == '\0')
1197 *pref
= racoon_strdup(p
+ 1);
1198 STRDUP_FATAL(*pref
);
1199 p
= strchr(*pref
, '[');
1201 if (*(p
+ 1) == '\0')
1204 *port
= racoon_strdup(p
+ 1);
1205 STRDUP_FATAL(*port
);
1206 p
= strchr(*pref
, ']');
1211 } else if (*p
== '[') {
1215 *port
= racoon_strdup(p
+ 1);
1216 STRDUP_FATAL(*port
);
1217 p
= strchr(*pref
, ']');
1236 *name
= *port
= *pref
= NULL
;
1244 struct ulproto_tag
*cp
;
1251 /* checking the string of upper layer protocol. */
1252 for (cp
= &ulprototab
[0]; cp
->str
; cp
++) {
1253 if (strcmp(str
, cp
->str
) == 0)
1254 return cp
->ul_proto
;
1263 dump_isakmp_sa(buf
, len
)
1272 /* isakmp status header */
1274 1234567890123456789012 0000000000000000:0000000000000000 000000000000
1277 "Destination Cookies Created";
1279 /* semi long header;
1280 1234567890123456789012 0000000000000000:0000000000000000 00 X 00 X 0000-00-00 00:00:00 000000
1283 "Destination Cookies ST S V E Created Phase2";
1286 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
1289 "Source Destination Cookies ST S V E Created Phase2";
1291 /* phase status header */
1293 side stats source address destination address
1294 xxx xxxxx 1234567890123456789012 1234567890123456789012
1297 static char *estr
[] = { "", "B", "M", "U", "A", "I", };
1299 switch (long_format
) {
1301 printf("%s\n", header1
);
1304 printf("%s\n", header2
);
1308 printf("%s\n", header3
);
1312 if (len
% sizeof(*pd
))
1313 printf("invalid length %d\n", len
);
1316 pd
= (struct ph1dump
*)buf
;
1319 /* source address */
1320 if (long_format
>= 2) {
1321 GETNAMEINFO((struct sockaddr
*)&pd
->local
, _addr1_
, _addr2_
);
1322 switch (long_format
) {
1326 p
= fixed_addr(_addr1_
, _addr2_
, 22);
1330 p
= fixed_addr(_addr1_
, _addr2_
, 45);
1336 /* destination address */
1337 GETNAMEINFO((struct sockaddr
*)&pd
->remote
, _addr1_
, _addr2_
);
1338 switch (long_format
) {
1341 p
= fixed_addr(_addr1_
, _addr2_
, 22);
1345 p
= fixed_addr(_addr1_
, _addr2_
, 45);
1350 printf("%s ", pindex_isakmp(&pd
->index
));
1352 /* statuc, side and version */
1353 if (long_format
>= 1) {
1354 printf("%2d %c %2x ",
1356 pd
->side
== INITIATOR
? 'I' : 'R',
1358 if (ARRAYLEN(estr
) > pd
->etype
)
1359 printf("%s ", estr
[pd
->etype
]);
1364 tm
= localtime(&pd
->created
);
1365 strftime(tbuf
, sizeof(tbuf
), "%Y-%m-%d %T", tm
);
1367 snprintf(tbuf
, sizeof(tbuf
), " ");
1368 printf("%s ", tbuf
);
1370 /* counter of phase 2 */
1371 if (long_format
>= 1)
1372 printf("%6d ", pd
->ph2cnt
);
1384 dump_internal(buf
, tlen
)
1388 struct ph2handle
*iph2
;
1389 struct sockaddr
*addr
;
1393 source address destination address
1394 1234567890123456789012 1234567890123456789012
1397 "Source Destination ";
1401 source address destination address
1402 123456789012345678901234567890123456789012345 123456789012345678901234567890123456789012345
1403 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
1406 "Source Destination ";
1408 printf("%s\n", long_format
? long_h1
: short_h1
);
1411 iph2
= (struct ph2handle
*)buf
;
1412 addr
= (struct sockaddr
*)(++iph2
);
1414 GETNAMEINFO(addr
, _addr1_
, _addr2_
);
1415 printf("%s ", long_format
?
1416 fixed_addr(_addr1_
, _addr2_
, 45)
1417 : fixed_addr(_addr1_
, _addr2_
, 22));
1419 tlen
-= sysdep_sa_len(addr
);
1421 GETNAMEINFO(addr
, _addr1_
, _addr2_
);
1422 printf("%s ", long_format
?
1423 fixed_addr(_addr1_
, _addr2_
, 45)
1424 : fixed_addr(_addr1_
, _addr2_
, 22));
1426 tlen
-= sysdep_sa_len(addr
);
1436 pindex_isakmp(index
)
1437 isakmp_index
*index
;
1439 static char buf
[64];
1443 memset(buf
, 0, sizeof(buf
));
1446 p
= (u_char
*)index
;
1447 for (j
= 0, i
= 0; i
< sizeof(isakmp_index
); i
++) {
1448 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, "%02x", p
[i
]);
1462 /* print schedule */
1463 char *str_sched_stat
[] = {
1469 char *str_sched_id
[] = {
1478 print_schedule(buf
, len
)
1482 struct scheddump
*sc
= (struct scheddump
*)buf
;
1486 if (len
% sizeof(*sc
))
1487 printf("invalid length %d\n", len
);
1490 /* 00000000 00000000 00000000 xxx........*/
1491 printf("index tick xtime created\n");
1494 tm
= localtime(&sc
->created
);
1495 strftime(tbuf
, sizeof(tbuf
), "%Y-%m-%d %T", tm
);
1497 printf("%-8ld %-8ld %-8ld %s\n",
1514 struct evtdump
*evtdump
= (struct evtdump
*)buf
;
1519 for (i
= 0; evtmsg
[i
].msg
; i
++)
1520 if (evtmsg
[i
].type
== evtdump
->type
)
1523 if (evtmsg
[i
].msg
== NULL
)
1524 printf("Event %d: ", evtdump
->type
);
1526 printf("%s : ", evtmsg
[i
].msg
);
1528 if ((srcstr
= saddr2str((struct sockaddr
*)&evtdump
->src
)) == NULL
)
1531 printf("%s", srcstr
);
1533 if ((dststr
= saddr2str((struct sockaddr
*)&evtdump
->dst
)) == NULL
)
1536 printf("%s", dststr
);
1547 struct evtdump
*evtdump
= (struct evtdump
*)buf
;
1551 for (i
= 0; evtmsg
[i
].msg
; i
++)
1552 if (evtmsg
[i
].type
== evtdump
->type
)
1555 if (evtmsg
[i
].level
!= ERROR
)
1558 if (evtmsg
[i
].msg
== NULL
)
1559 printf("Error: Event %d\n", evtdump
->type
);
1561 printf("Error: %s\n", evtmsg
[i
].msg
);
1563 if (evt_filter
& EVTF_ERR_STOP
)
1564 evt_filter
&= ~EVTF_LOOP
;
1570 * Print a message when phase 1 SA goes down
1573 print_ph1down(buf
, len
)
1577 struct evtdump
*evtdump
= (struct evtdump
*)buf
;
1579 if (evtdump
->type
!= EVTT_PHASE1_DOWN
)
1582 printf("VPN connexion terminated\n");
1584 if (evt_filter
& EVTF_PH1DOWN_STOP
)
1585 evt_filter
&= ~EVTF_LOOP
;
1591 * Print ISAKMP mode config info (IP and banner)
1598 struct evtdump
*evtdump
= (struct evtdump
*)buf
;
1599 struct isakmp_data
*attr
;
1600 char *banner
= NULL
;
1601 struct in_addr addr4
;
1603 memset(&addr4
, 0, sizeof(addr4
));
1605 if (evtdump
->type
!= EVTT_ISAKMP_CFG_DONE
&&
1606 evtdump
->type
!= EVTT_NO_ISAKMP_CFG
)
1609 len
-= sizeof(*evtdump
);
1610 attr
= (struct isakmp_data
*)(evtdump
+ 1);
1613 if (len
< sizeof(*attr
)) {
1614 printf("short attribute too short\n");
1618 if ((ntohs(attr
->type
) & ISAKMP_GEN_MASK
) == ISAKMP_GEN_TV
) {
1619 /* Short attribute, skip */
1620 len
-= sizeof(*attr
);
1622 } else { /* Long attribute */
1625 if (len
< (sizeof(*attr
) + ntohs(attr
->lorv
))) {
1626 printf("long attribute too long\n");
1630 switch (ntohs(attr
->type
) & ~ISAKMP_GEN_MASK
) {
1631 case INTERNAL_IP4_ADDRESS
:
1632 if (ntohs(attr
->lorv
) < sizeof(addr4
)) {
1633 printf("addr4 attribute too short\n");
1636 memcpy(&addr4
, attr
+ 1, sizeof(addr4
));
1640 banner
= racoon_malloc(ntohs(attr
->lorv
) + 1);
1641 if (banner
== NULL
) {
1642 printf("malloc failed\n");
1645 memcpy(banner
, attr
+ 1, ntohs(attr
->lorv
));
1646 banner
[ntohs(attr
->lorv
)] = '\0';
1653 len
-= (sizeof(*attr
) + ntohs(attr
->lorv
));
1655 attr
= (struct isakmp_data
*)
1656 (n
+ sizeof(*attr
) + ntohs(attr
->lorv
));
1660 if (evtdump
->type
== EVTT_ISAKMP_CFG_DONE
)
1661 printf("Bound to address %s\n", inet_ntoa(addr4
));
1663 printf("VPN connexion established\n");
1670 if (ioctl(1, TIOCGWINSZ
, &win
) != 1)
1673 for (i
= 0; i
< col
; i
++)
1675 printf("\n%s\n", banner
);
1676 for (i
= 0; i
< col
; i
++)
1679 racoon_free(banner
);
1682 if (evt_filter
& EVTF_CFG_STOP
)
1683 evt_filter
&= ~EVTF_LOOP
;
1690 fixed_addr(addr
, port
, len
)
1694 static char _addr_buf_
[BUFSIZ
];
1699 memset(_addr_buf_
, ' ', sizeof(_addr_buf_
));
1701 plen
= strlen(port
);
1706 for (i
= 0; i
< len
- plen
- 1 && addr
[i
] != '\0'; /*noting*/)
1710 for (i
= 0; i
< plen
&& port
[i
] != '\0'; /*noting*/)
1713 _addr_buf_
[len
] = '\0';
1722 struct admin_com h
, *com
;
1726 com
= (struct admin_com
*)combuf
->v
;
1727 len
= com
->ac_len
- sizeof(*com
);
1728 buf
= combuf
->v
+ sizeof(*com
);
1730 switch (com
->ac_cmd
) {
1731 case ADMIN_SHOW_SCHED
:
1732 print_schedule(buf
, len
);
1735 case ADMIN_SHOW_EVT
: {
1736 struct evtdump
*evtdump
;
1738 /* We got no event */
1740 /* If we were purging the queue, it is now done */
1741 if (evt_filter
& EVTF_PURGE
)
1742 evt_filter
&= ~EVTF_PURGE
;
1746 if (len
< sizeof(struct evtdump
))
1747 errx(1, "Short buffer\n");
1749 /* Toss outdated events */
1750 evtdump
= (struct evtdump
*)buf
;
1751 if (evtdump
->timestamp
< evt_start
)
1754 if (evt_filter
& EVTF_ALL
)
1755 print_evt(buf
, len
);
1756 if (evt_filter
& EVTF_ERR
)
1757 print_err(buf
, len
);
1758 if (evt_filter
& EVTF_CFG
)
1759 print_cfg(buf
, len
);
1760 if (evt_filter
& EVTF_PH1DOWN
)
1761 print_ph1down(buf
, len
);
1767 switch (com
->ac_proto
) {
1768 case ADMIN_PROTO_ISAKMP
:
1769 dump_isakmp_sa(buf
, len
);
1771 case ADMIN_PROTO_IPSEC
:
1772 case ADMIN_PROTO_AH
:
1773 case ADMIN_PROTO_ESP
:
1775 struct sadb_msg
*msg
= (struct sadb_msg
*)buf
;
1777 switch (msg
->sadb_msg_errno
) {
1779 switch (msg
->sadb_msg_type
) {
1782 printf("No entry.\n");
1785 printf("No SAD entries.\n");
1792 if (msg
->sadb_msg_seq
== 0)
1794 msg
= (struct sadb_msg
*)((caddr_t
)msg
+
1795 PFKEY_UNUNIT64(msg
->sadb_msg_len
));
1799 printf("%s.\n", strerror(msg
->sadb_msg_errno
));
1803 case ADMIN_PROTO_INTERNAL
:
1804 dump_internal(buf
, len
);
1807 printf("Invalid proto [%d]\n", com
->ac_proto
);