xref: /f-stack/tools/ifconfig/ifipsec.c (revision d4a07e70)
1 /*-
2  * Copyright (c) 2016 Yandex LLC
3  * Copyright (c) 2016 Andrey V. Elsukov <[email protected]>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <sys/sockio.h>
35 #include <sys/stdint.h>
36 
37 #include <stdlib.h>
38 #include <unistd.h>
39 
40 #include <net/ethernet.h>
41 #include <net/if.h>
42 #include <net/if_ipsec.h>
43 #include <net/route.h>
44 
45 #include <ctype.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <err.h>
49 #include <errno.h>
50 
51 #include "ifconfig.h"
52 
53 static void
ipsec_status(int s)54 ipsec_status(int s)
55 {
56 	uint32_t reqid;
57 
58 	ifr.ifr_data = (caddr_t)&reqid;
59 #ifndef FSTACK
60 	if (ioctl(s, IPSECGREQID, &ifr) == -1)
61 #else
62 	size_t offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
63 	size_t clen = sizeof(uint32_t);
64 	if (ioctl_va(s, IPSECGREQID, &ifr, 3, offset, ifr.ifr_data, clen) == -1)
65 #endif
66 		return;
67 	printf("\treqid: %u\n", reqid);
68 }
69 
70 static
DECL_CMD_FUNC(setreqid,val,arg)71 DECL_CMD_FUNC(setreqid, val, arg)
72 {
73 	char *ep;
74 	uint32_t v;
75 
76 	v = strtoul(val, &ep, 0);
77 	if (*ep != '\0') {
78 		warn("Invalid reqid value %s", val);
79 		return;
80 	}
81 	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
82 	ifr.ifr_data = (char *)&v;
83 #ifndef FSTACK
84 	if (ioctl(s, IPSECSREQID, &ifr) == -1) {
85 #else
86 	size_t offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
87 	size_t clen = sizeof(uint32_t);
88 	if (ioctl_va(s, IPSECGREQID, &ifr, 3, offset, ifr.ifr_data, clen) == -1) {
89 #endif
90 
91 		warn("ioctl(IPSECSREQID)");
92 		return;
93 	}
94 }
95 
96 static struct cmd ipsec_cmds[] = {
97 	DEF_CMD_ARG("reqid",		setreqid),
98 };
99 
100 static struct afswtch af_ipsec = {
101 	.af_name	= "af_ipsec",
102 	.af_af		= AF_UNSPEC,
103 	.af_other_status = ipsec_status,
104 };
105 
106 static __constructor void
107 ipsec_ctor(void)
108 {
109 	size_t i;
110 
111 	for (i = 0; i < nitems(ipsec_cmds); i++)
112 		cmd_register(&ipsec_cmds[i]);
113 	af_register(&af_ipsec);
114 #undef N
115 }
116