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