File: kernel_ipsec_interface_netbsd.c

package info (click to toggle)
libreswan 5.2-2.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,644 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (212 lines) | stat: -rw-r--r-- 5,045 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* BSD's IPsec Interface, for libreswan
 *
 * Copyright (C) 2024 Andrew Cagney
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/gpl2.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 */

#include "ip_info.h"
#include "reqid.h"

#include "ipsec_interface.h"
#include "kernel_ipsec_interface.h"
#include "iface.h"
#include "server_run.h"

#include "log.h"

static reqid_t reqid_base;

static bool netbsd_ipsec_interface_has_cidr(const char *ipsec_if_name,
					      ip_cidr cidr,
					      struct verbose verbose)
{
	cidr_buf cb;
	vlog("%s() always true %s %s", __func__, ipsec_if_name,
	     str_cidr(&cidr, &cb));
	return true;
}

/*
 * ifconfig ipsec0 inet 172.16.100.1/32 172.16.200.1
 * where 172.16.200.1 is the interface-ip / sourceip.
 */

static bool netbsd_ipsec_interface_add_cidr(const char *ipsec_if_name,
					      ip_cidr cidr,
					      struct verbose verbose)
{
	/* same scope as add[] */
	cidr_buf cb;
	address_buf ab;
	ip_address address = cidr_address(cidr);
	const struct ip_info *afi = cidr_info(cidr);
	const char *add[] = {
		"ifconfig",
		ipsec_if_name,
		afi->inet_name, /* "inet" or "inet6" */
		str_cidr(&cidr, &cb),
		str_address(&address, &ab),
		NULL,
	};
	return server_runv(add, verbose);
}

static void netbsd_ipsec_interface_del_cidr(const char *ipsec_if_name,
					      ip_cidr cidr UNUSED,
					      struct verbose verbose)
{
	/* same scope as add[] */
	cidr_buf cb;
	address_buf ab;
	ip_address address = cidr_address(cidr);
	const struct ip_info *afi = cidr_info(cidr);
	const char *delete[] = {
		"ifconfig",
		ipsec_if_name,
		"delete",
		afi->inet_name, /* "inet" or "inet6" */
		str_cidr(&cidr, &cb),
		str_address(&address, &ab),
		NULL,
	};
	server_runv(delete, verbose);
}

static bool netbsd_ipsec_interface_add(const char *ipsec_if_name,
					 const ipsec_interface_id_t ipsec_if_id UNUSED,
					 const struct iface_device *iface UNUSED,
					 struct verbose verbose)
{
	const char *create[] = {
		"ifconfig",
		ipsec_if_name,
		"create",
		NULL,
	};
	return server_runv(create, verbose);
}

static bool netbsd_ipsec_interface_up(const char *ipsec_if_name UNUSED,
					struct verbose verbose UNUSED)
{
	const char *up[] = {
		"ifconfig",
		ipsec_if_name,
		"up",
		NULL,
	};
	return server_runv(up, verbose);
}

static bool netbsd_ipsec_interface_del(const char *ipsec_if_name,
					 struct verbose verbose)
{
	const char *destroy[] = {
		"ifconfig",
		ipsec_if_name,
		"destroy",
		NULL,
	};
	return server_runv(destroy, verbose);
}

static bool netbsd_ipsec_interface_match(struct ipsec_interface_match *match UNUSED,
					   struct verbose verbose UNUSED)
{
	const char *run[] = {
		"ifconfig",
		match->ipsec_if_name,
		NULL,
	};
	bool ok = server_runv(run, verbose);
	if (ok) {
		jam_str(match->found, sizeof(match->found), match->ipsec_if_name);
	} else {
		match->diag = diag("not found");
	}
	return ok;
}

static err_t read_sysctl(const char *ctl, uintmax_t *value, struct verbose verbose)
{
	const char *sysctl[] = {
		"sysctl",
		"-n",
		ctl,
		NULL,
	};
	chunk_t chunk = server_runv_chunk(sysctl, verbose);
	if (chunk.len == 0) {
		return "problem reading ..., no output";
	}
	shunk_t shunk = HUNK_AS_SHUNK(chunk);
	err_t e = shunk_to_uintmax(shunk, &shunk, 10, value);
	if (e != NULL) {
		return "problem reading ..., invalid number";
	}
	free_chunk_content(&chunk);
	return NULL;
}

static err_t netbsd_ipsec_interface_init(struct verbose verbose)
{
	err_t e;
	/* check net.ipsecif.use_fixed_reqid=1 */
	uintmax_t fixed;
	e = read_sysctl("net.ipsecif.use_fixed_reqid", &fixed, verbose);
	if (e != NULL) {
		return e;
	}
	if (fixed == 0) {
		return "net.ipsecif.use_fixed_reqid should be 1";
	}
	/* extract base */
	uintmax_t base;
	e = read_sysctl("net.ipsecif.reqid_base", &base, verbose);
	if (e != NULL) {
		return e;
	}
	if (base < 8192) {
		return "net.ipsecif.reqid_base is too small";
	}
	if (base >= 16384/*magic*/) {
		return "net.ipsecif.reqid_base is too big";
	}
	reqid_base = base;
	return NULL;
}

static reqid_t netbsd_ipsec_interface_reqid(ipsec_interface_id_t if_id,
					    struct verbose verbose)
{
	vdbg("()");
	return reqid_base + (if_id * 2);
}

const struct kernel_ipsec_interface kernel_ipsec_interface_ifconfig = {
	.name = "ipsec",

	.has_cidr = netbsd_ipsec_interface_has_cidr,
	.add_cidr = netbsd_ipsec_interface_add_cidr,
	.del_cidr = netbsd_ipsec_interface_del_cidr,

	.add = netbsd_ipsec_interface_add,
	.up = netbsd_ipsec_interface_up,
	.del = netbsd_ipsec_interface_del,

	.reqid = netbsd_ipsec_interface_reqid,

	.match = netbsd_ipsec_interface_match,
	.init = netbsd_ipsec_interface_init,

};