xref: /freebsd/sbin/ifconfig/ifipsec.c (revision 069ac184)
1 /*-
2  * Copyright (c) 2016 Yandex LLC
3  * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
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/param.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <sys/sockio.h>
32 #include <sys/stdint.h>
33 
34 #include <stdlib.h>
35 #include <unistd.h>
36 
37 #include <net/ethernet.h>
38 #include <net/if.h>
39 #include <net/if_ipsec.h>
40 #include <net/route.h>
41 
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <err.h>
46 #include <errno.h>
47 
48 #include "ifconfig.h"
49 
50 static void
51 ipsec_status(if_ctx *ctx)
52 {
53 	uint32_t reqid;
54 	struct ifreq ifr = { .ifr_data = (caddr_t)&reqid };
55 
56 	if (ioctl_ctx_ifr(ctx, IPSECGREQID, &ifr) == -1)
57 		return;
58 	printf("\treqid: %u\n", reqid);
59 }
60 
61 static void
62 setreqid(if_ctx *ctx, const char *val, int dummy __unused)
63 {
64 	char *ep;
65 	uint32_t v;
66 	struct ifreq ifr = { .ifr_data = (caddr_t)&v };
67 
68 	v = strtoul(val, &ep, 0);
69 	if (*ep != '\0') {
70 		warn("Invalid reqid value %s", val);
71 		return;
72 	}
73 	if (ioctl_ctx_ifr(ctx, IPSECSREQID, &ifr) == -1) {
74 		warn("ioctl(IPSECSREQID)");
75 		return;
76 	}
77 }
78 
79 static struct cmd ipsec_cmds[] = {
80 	DEF_CMD_ARG("reqid",		setreqid),
81 };
82 
83 static struct afswtch af_ipsec = {
84 	.af_name	= "af_ipsec",
85 	.af_af		= AF_UNSPEC,
86 	.af_other_status = ipsec_status,
87 };
88 
89 static __constructor void
90 ipsec_ctor(void)
91 {
92 	size_t i;
93 
94 	for (i = 0; i < nitems(ipsec_cmds); i++)
95 		cmd_register(&ipsec_cmds[i]);
96 	af_register(&af_ipsec);
97 #undef N
98 }
99