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