Bug Summary

File:programs/pluto/kernel.c
Warning:line 2149, column 3
Value stored to 'encap_oneshot' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name kernel.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -pic-is-pie -mthread-model posix -mdisable-fp-elim -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -resource-dir /usr/lib64/clang/8.0.0 -D TimeZoneOffset=timezone -D linux -D PIE -D NSS_IPSEC_PROFILE -D XFRM_SUPPORT -D USE_XFRM_INTERFACE -D USE_DNSSEC -D DEFAULT_DNSSEC_ROOTKEY_FILE="/var/lib/unbound/root.key" -D HAVE_LABELED_IPSEC -D HAVE_SECCOMP -D LIBCURL -D USE_LINUX_AUDIT -D USE_SYSTEMD_WATCHDOG -D HAVE_NM -D XAUTH_HAVE_PAM -D USE_3DES -D USE_AES -D USE_CAMELLIA -D USE_CHACHA -D USE_DH31 -D USE_MD5 -D USE_SHA1 -D USE_SHA2 -D USE_PRF_AES_XCBC -D DEFAULT_RUNDIR="/run/pluto" -D IPSEC_CONF="/etc/ipsec.conf" -D IPSEC_CONFDDIR="/etc/ipsec.d" -D IPSEC_NSSDIR="/etc/ipsec.d" -D IPSEC_CONFDIR="/etc" -D IPSEC_EXECDIR="/usr/local/libexec/ipsec" -D IPSEC_SBINDIR="/usr/local/sbin" -D IPSEC_VARDIR="/var" -D POLICYGROUPSDIR="/etc/ipsec.d/policies" -D IPSEC_SECRETS_FILE="/etc/ipsec.secrets" -D FORCE_PR_ASSERT -D USE_FORK=1 -D USE_VFORK=0 -D USE_DAEMON=0 -D USE_PTHREAD_SETSCHEDPRIO=1 -D GCC_LINT -D HAVE_LIBCAP_NG -I . -I ../../OBJ.linux.x86_64/programs/pluto -I ../../include -I /usr/include/nss3 -I /usr/include/nspr4 -I /home/build/libreswan/programs/pluto/linux-copy -D HERE_BASENAME="kernel.c" -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/8.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-missing-field-initializers -std=gnu99 -fdebug-compilation-dir /home/build/libreswan/programs/pluto -ferror-limit 19 -fmessage-length 0 -stack-protector 3 -fobjc-runtime=gcc -fdiagnostics-show-option -analyzer-output=html -o /tmp/scan-build-2020-09-09-193337-25440-1 -x c /home/build/libreswan/programs/pluto/kernel.c -faddrsig
1/* routines that interface with the kernel's IPsec mechanism, for libreswan
2 *
3 * Copyright (C) 1997 Angelos D. Keromytis.
4 * Copyright (C) 1998-2010 D. Hugh Redelmeier.
5 * Copyright (C) 2003-2008 Michael Richardson <mcr@xelerance.com>
6 * Copyright (C) 2007-2010 Paul Wouters <paul@xelerance.com>
7 * Copyright (C) 2008-2010 David McCullough <david_mccullough@securecomputing.com>
8 * Copyright (C) 2010 Bart Trojanowski <bart@jukie.net>
9 * Copyright (C) 2009-2010 Tuomo Soini <tis@foobar.fi>
10 * Copyright (C) 2010 Avesh Agarwal <avagarwa@redhat.com>
11 * Copyright (C) 2010-2019 D. Hugh Redelmeier <hugh@mimosa.com>
12 * Copyright (C) 2012-2015 Paul Wouters <paul@libreswan.org>
13 * Copyright (C) 2013 Kim B. Heino <b@bbbs.net>
14 * Copyright (C) 2016-2019 Andrew Cagney <cagney@gnu.org>
15 * Copyright (C) 2019 Paul Wouters <pwouters@redhat.com>
16 * Copyright (C) 2017 Mayank Totale <mtotale@gmail.com>
17 *
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the
20 * Free Software Foundation; either version 2 of the License, or (at your
21 * option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * for more details.
27 */
28
29#include <stddef.h>
30#include <string.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <errno(*__errno_location ()).h>
34#include <sys/wait.h> /* for WIFEXITED() et.al. */
35#include <unistd.h>
36#include <fcntl.h>
37#include <sys/utsname.h>
38#include <sys/ioctl.h>
39
40#include <sys/stat.h>
41#include <sys/socket.h>
42#include <netinet/in.h>
43#include <arpa/inet.h>
44
45#include <event2/event.h>
46#include <event2/event_struct.h>
47#include <event2/thread.h>
48
49
50#include "sysdep.h"
51#include "constants.h"
52#include "lsw-pfkeyv2.h" /* for SADB_X_CALG_DEFLATE et.al., grrr */
53
54#include "defs.h"
55#include "rnd.h"
56#include "id.h"
57#include "connections.h" /* needs id.h */
58#include "state.h"
59#include "timer.h"
60#include "kernel.h"
61#include "kernel_xfrm.h"
62#include "packet.h"
63#include "x509.h"
64#include "pluto_x509.h"
65#include "certs.h"
66#include "secrets.h"
67#include "log.h"
68#include "server.h"
69#include "whack.h" /* for RC_LOG_SERIOUS */
70#include "keys.h"
71#include "ike_alg.h"
72#include "ike_alg_encrypt.h"
73#include "ike_alg_integ.h"
74
75#include "packet.h" /* for pb_stream in nat_traversal.h */
76#include "nat_traversal.h"
77#include "ip_address.h"
78#include "ip_info.h"
79#include "lswfips.h" /* for libreswan_fipsmode() */
80# include "kernel_xfrm_interface.h"
81#include "iface.h"
82#include "ip_selector.h"
83#include "ip_encap.h"
84#include "show.h"
85
86bool_Bool can_do_IPcomp = TRUE1; /* can system actually perform IPCOMP? */
87
88/* test if the routes required for two different connections agree
89 * It is assumed that the destination subnets agree; we are only
90 * testing that the interfaces and nexthops match.
91 */
92#define routes_agree(c, d)((c)->interface->ip_dev == (d)->interface->ip_dev
&& sameaddr(&(c)->spd.this.host_nexthop, &
(d)->spd.this.host_nexthop))
\
93 ((c)->interface->ip_dev == (d)->interface->ip_dev && \
94 sameaddr(&(c)->spd.this.host_nexthop, &(d)->spd.this.host_nexthop))
95
96const struct pfkey_proto_info null_proto_info[2] = {
97 {
98 .proto = IPPROTO_ESPIPPROTO_ESP,
99 .mode = ENCAPSULATION_MODE_TRANSPORT2,
100 .reqid = 0
101 },
102 {
103 .proto = 0,
104 .mode = 0,
105 .reqid = 0
106 }
107};
108
109struct bare_shunt {
110 policy_prio_t policy_prio;
111 ip_selector our_client;
112 ip_selector peer_client;
113 ip_said said;
114 int transport_proto; /* XXX: same value in local/remote */
115 unsigned long count;
116 monotime_t last_activity;
117
118 /*
119 * Note: "why" must be in stable storage (not auto, not heap)
120 * because we use it indefinitely without copying or pfreeing.
121 * Simple rule: use a string literal.
122 */
123 const char *why;
124 /* the connection from where it came - used to re-load /32 conns */
125 char *from_cn;
126
127 struct bare_shunt *next;
128};
129
130static struct bare_shunt *bare_shunts = NULL((void*)0);
131
132#ifdef IPSEC_CONNECTION_LIMIT
133static int num_ipsec_eroute = 0;
134#endif
135
136static void log_bare_shunt(lset_t rc_flags, const char *op, const struct bare_shunt *bs)
137{
138 said_buf sat;
139 selector_buf ourb;
140 selector_buf peerb;
141
142 char prio[POLICY_PRIO_BUF(3 + 1 + 3 + 1 + 10)];
143 fmt_policy_prio(bs->policy_prio, prio);
144
145 log_global(rc_flags, null_fd,{ struct logger log_ = (struct logger) { .where = (where_t) {
.func = __func__, .basename = "kernel.c" , .line = 152}, .global_whackfd
= ((struct fd *) ((void*)0)), .object = ((void*)0), .object_vec
= &logger_global_vec, }; log_message(rc_flags, &log_
, "%s bare shunt %p %s --%d--> %s => %s %s %s", op, (
const void *)bs, str_selector(&bs->our_client, &ourb
), bs->transport_proto, str_selector(&bs->peer_client
, &peerb), str_said(&bs->said, &sat), prio, bs
->why); }
146 "%s bare shunt %p %s --%d--> %s => %s %s %s",{ struct logger log_ = (struct logger) { .where = (where_t) {
.func = __func__, .basename = "kernel.c" , .line = 152}, .global_whackfd
= ((struct fd *) ((void*)0)), .object = ((void*)0), .object_vec
= &logger_global_vec, }; log_message(rc_flags, &log_
, "%s bare shunt %p %s --%d--> %s => %s %s %s", op, (
const void *)bs, str_selector(&bs->our_client, &ourb
), bs->transport_proto, str_selector(&bs->peer_client
, &peerb), str_said(&bs->said, &sat), prio, bs
->why); }
147 op, (const void *)bs,{ struct logger log_ = (struct logger) { .where = (where_t) {
.func = __func__, .basename = "kernel.c" , .line = 152}, .global_whackfd
= ((struct fd *) ((void*)0)), .object = ((void*)0), .object_vec
= &logger_global_vec, }; log_message(rc_flags, &log_
, "%s bare shunt %p %s --%d--> %s => %s %s %s", op, (
const void *)bs, str_selector(&bs->our_client, &ourb
), bs->transport_proto, str_selector(&bs->peer_client
, &peerb), str_said(&bs->said, &sat), prio, bs
->why); }
148 str_selector(&bs->our_client, &ourb),{ struct logger log_ = (struct logger) { .where = (where_t) {
.func = __func__, .basename = "kernel.c" , .line = 152}, .global_whackfd
= ((struct fd *) ((void*)0)), .object = ((void*)0), .object_vec
= &logger_global_vec, }; log_message(rc_flags, &log_
, "%s bare shunt %p %s --%d--> %s => %s %s %s", op, (
const void *)bs, str_selector(&bs->our_client, &ourb
), bs->transport_proto, str_selector(&bs->peer_client
, &peerb), str_said(&bs->said, &sat), prio, bs
->why); }
149 bs->transport_proto,{ struct logger log_ = (struct logger) { .where = (where_t) {
.func = __func__, .basename = "kernel.c" , .line = 152}, .global_whackfd
= ((struct fd *) ((void*)0)), .object = ((void*)0), .object_vec
= &logger_global_vec, }; log_message(rc_flags, &log_
, "%s bare shunt %p %s --%d--> %s => %s %s %s", op, (
const void *)bs, str_selector(&bs->our_client, &ourb
), bs->transport_proto, str_selector(&bs->peer_client
, &peerb), str_said(&bs->said, &sat), prio, bs
->why); }
150 str_selector(&bs->peer_client, &peerb),{ struct logger log_ = (struct logger) { .where = (where_t) {
.func = __func__, .basename = "kernel.c" , .line = 152}, .global_whackfd
= ((struct fd *) ((void*)0)), .object = ((void*)0), .object_vec
= &logger_global_vec, }; log_message(rc_flags, &log_
, "%s bare shunt %p %s --%d--> %s => %s %s %s", op, (
const void *)bs, str_selector(&bs->our_client, &ourb
), bs->transport_proto, str_selector(&bs->peer_client
, &peerb), str_said(&bs->said, &sat), prio, bs
->why); }
151 str_said(&bs->said, &sat),{ struct logger log_ = (struct logger) { .where = (where_t) {
.func = __func__, .basename = "kernel.c" , .line = 152}, .global_whackfd
= ((struct fd *) ((void*)0)), .object = ((void*)0), .object_vec
= &logger_global_vec, }; log_message(rc_flags, &log_
, "%s bare shunt %p %s --%d--> %s => %s %s %s", op, (
const void *)bs, str_selector(&bs->our_client, &ourb
), bs->transport_proto, str_selector(&bs->peer_client
, &peerb), str_said(&bs->said, &sat), prio, bs
->why); }
152 prio, bs->why){ struct logger log_ = (struct logger) { .where = (where_t) {
.func = __func__, .basename = "kernel.c" , .line = 152}, .global_whackfd
= ((struct fd *) ((void*)0)), .object = ((void*)0), .object_vec
= &logger_global_vec, }; log_message(rc_flags, &log_
, "%s bare shunt %p %s --%d--> %s => %s %s %s", op, (
const void *)bs, str_selector(&bs->our_client, &ourb
), bs->transport_proto, str_selector(&bs->peer_client
, &peerb), str_said(&bs->said, &sat), prio, bs
->why); }
;
153}
154
155static void dbg_bare_shunt(const char *op, const struct bare_shunt *bs)
156{
157 /* same as log_bare_shunt but goes to debug log */
158 if (DBGP(DBG_BASE)(cur_debugging & (((lset_t)1 << (DBG_BASE_IX))))) {
159 log_bare_shunt(DEBUG_STREAM, op, bs);
160 }
161}
162
163/*
164 * Note: "why" must be in stable storage (not auto, not heap)
165 * because we use it indefinitely without copying or pfreeing.
166 * Simple rule: use a string literal.
167 */
168void add_bare_shunt(const ip_subnet *our_client, const ip_subnet *peer_client,
169 int transport_proto, ipsec_spi_t shunt_spi,
170 const char *why)
171{
172 /* report any duplication; this should NOT happen */
173 struct bare_shunt **bspp = bare_shunt_ptr(our_client, peer_client, transport_proto);
174
175 if (bspp != NULL((void*)0)) {
176 /* maybe: passert(bsp == NULL); */
177 log_bare_shunt(RC_LOG, "CONFLICTING existing", *bspp);
178 }
179
180 struct bare_shunt *bs = alloc_thing(struct bare_shunt,((struct bare_shunt*) alloc_bytes(sizeof(struct bare_shunt), (
"bare shunt")))
181 "bare shunt")((struct bare_shunt*) alloc_bytes(sizeof(struct bare_shunt), (
"bare shunt")))
;
182
183 bs->why = why;
184 bs->from_cn = NULL((void*)0);
185 bs->our_client = *our_client;
186 bs->peer_client = *peer_client;
187 bs->transport_proto = transport_proto;
188 bs->policy_prio = BOTTOM_PRIO((policy_prio_t)0);
189
190 bs->said = said3(&subnet_type(our_client)->any_address, htonl(shunt_spi), &ip_protocol_internal);
191 bs->count = 0;
192 bs->last_activity = mononow();
193
194 bs->next = bare_shunts;
195 bare_shunts = bs;
196 dbg_bare_shunt("add", bs);
197
198 /* report duplication; this should NOT happen */
199 if (bspp != NULL((void*)0)) {
200 log_bare_shunt(RC_LOG, "CONFLICTING new", bs);
201 }
202}
203
204
205/*
206 * Note: "why" must be in stable storage (not auto, not heap)
207 * because we use it indefinitely without copying or pfreeing.
208 * Simple rule: use a string literal.
209 */
210
211void record_and_initiate_opportunistic(const ip_selector *our_client,
212 const ip_selector *peer_client,
213 unsigned transport_proto,
214 struct xfrm_user_sec_ctx_ike *uctx,
215 const char *why)
216{
217 passert(selector_type(our_client) == selector_type(peer_client)){ _Bool assertion__ = selector_type(our_client) == selector_type
(peer_client); if (!assertion__) { lsw_passert_fail((where_t)
{ .func = __func__, .basename = "kernel.c" , .line = 217}, "%s"
, "selector_type(our_client) == selector_type(peer_client)");
} }
;
218 passert(selector_ipproto(our_client) == transport_proto){ _Bool assertion__ = selector_ipproto(our_client) == transport_proto
; if (!assertion__) { lsw_passert_fail((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 218}, "%s", "selector_ipproto(our_client) == transport_proto"
); } }
;
219 passert(selector_ipproto(peer_client) == transport_proto){ _Bool assertion__ = selector_ipproto(peer_client) == transport_proto
; if (!assertion__) { lsw_passert_fail((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 219}, "%s", "selector_ipproto(peer_client) == transport_proto"
); } }
;
220 /* XXX: port may or may not be zero */
221
222 /*
223 * Add the kernel shunt to the pluto bare shunt list.
224 *
225 * We need to do this because the %hold shunt was installed by
226 * kernel and we want to keep track of it inside pluto.
227 */
228
229 /*const*/ struct bare_shunt **bspp = bare_shunt_ptr(our_client, peer_client,
230 transport_proto);
231 if (bspp != NULL((void*)0) &&
232 (*bspp)->said.proto == &ip_protocol_internal &&
233 (*bspp)->said.spi == htonl(SPI_HOLD259)) {
234 log_global(RC_LOG_SERIOUS, null_fd, "existing bare shunt found - refusing to add a duplicate"){ struct logger log_ = (struct logger) { .where = (where_t) {
.func = __func__, .basename = "kernel.c" , .line = 234}, .global_whackfd
= ((struct fd *) ((void*)0)), .object = ((void*)0), .object_vec
= &logger_global_vec, }; log_message(RC_LOG_SERIOUS, &
log_, "existing bare shunt found - refusing to add a duplicate"
); }
;
235 /* should we continue with initiate_ondemand() ? */
236 } else {
237 add_bare_shunt(our_client, peer_client, transport_proto, SPI_HOLD259, why);
238 }
239
240 /* XXX: missing transport_proto */
241 ip_address sp = subnet_prefix(our_client);
242 ip_address dp = subnet_prefix(peer_client);
243 ip_endpoint src = endpoint(&sp, subnet_hport(our_client));
244 ip_endpoint dst = endpoint(&dp, subnet_hport(peer_client));
245 passert(endpoint_type(&src) == endpoint_type(&dst)){ _Bool assertion__ = endpoint_type(&src) == endpoint_type
(&dst); if (!assertion__) { lsw_passert_fail((where_t) { .
func = __func__, .basename = "kernel.c" , .line = 245}, "%s",
"endpoint_type(&src) == endpoint_type(&dst)"); } }
; /* duh */
246
247 /* actually initiate opportunism / ondemand */
248 initiate_ondemand(&src, &dst, transport_proto,
249 TRUE1, null_fd((struct fd *) ((void*)0)), true1/*background*/,
250 uctx, "acquire");
251
252 if (kernel_ops->remove_orphaned_holds != NULL((void*)0)) {
253 dbg("record_and_initiate_opportunistic(): tell kernel to remove orphan hold for our bare shunt"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("record_and_initiate_opportunistic(): tell kernel to remove orphan hold for our bare shunt"
); } }
;
254 kernel_ops->remove_orphaned_holds(transport_proto,
255 our_client, peer_client);
256 }
257}
258
259static reqid_t get_proto_reqid(reqid_t base, const struct ip_protocol *proto)
260{
261 if (proto == &ip_protocol_comp)
262 return reqid_ipcomp(base);
263
264 if (proto == &ip_protocol_esp)
265 return reqid_esp(base);
266
267 if (proto == &ip_protocol_ah)
268 return reqid_ah(base);
269
270 PASSERT_FAIL("bad protocol %s", proto->name)lsw_passert_fail((where_t) { .func = __func__, .basename = "kernel.c"
, .line = 270}, "bad protocol %s", proto->name)
;
271}
272
273/* Generate Unique SPI numbers.
274 *
275 * The specs say that the number must not be less than IPSEC_DOI_SPI_MIN.
276 * Pluto generates numbers not less than IPSEC_DOI_SPI_OUR_MIN,
277 * reserving numbers in between for manual keying (but we cannot so
278 * restrict numbers generated by our peer).
279 * XXX This should be replaced by a call to the kernel when
280 * XXX we get an API.
281 * The returned SPI is in network byte order.
282 * We use a random number as the initial SPI so that there is
283 * a good chance that different Pluto instances will choose
284 * different SPIs. This is good for two reasons.
285 * - the keying material for the initiator and responder only
286 * differs if the SPIs differ.
287 * - if Pluto is restarted, it would otherwise recycle the SPI
288 * numbers and confuse everything. When the kernel generates
289 * SPIs, this will no longer matter.
290 * We then allocate numbers sequentially. Thus we don't have to
291 * check if the number was previously used (assuming that no
292 * SPI lives longer than 4G of its successors).
293 */
294ipsec_spi_t get_ipsec_spi(ipsec_spi_t avoid,
295 const struct ip_protocol *proto,
296 const struct spd_route *sr,
297 bool_Bool tunnel)
298{
299 passert(proto == &ip_protocol_ah || proto == &ip_protocol_esp){ _Bool assertion__ = proto == &ip_protocol_ah || proto ==
&ip_protocol_esp; if (!assertion__) { lsw_passert_fail((
where_t) { .func = __func__, .basename = "kernel.c" , .line =
299}, "%s", "proto == &ip_protocol_ah || proto == &ip_protocol_esp"
); } }
;
300
301 if (kernel_ops->get_spi != NULL((void*)0)) {
302 char text_said[SATOT_BUFsizeof(said_buf)];
303 set_text_said(text_said, &sr->this.host_addr, 0, proto);
304 return kernel_ops->get_spi(&sr->that.host_addr,
305 &sr->this.host_addr, proto, tunnel,
306 get_proto_reqid(sr->reqid, proto),
307 IPSEC_DOI_SPI_OUR_MIN0x1000, 0xffffffff,
308 text_said);
309 } else {
310 static ipsec_spi_t spi = 0; /* host order, so not returned directly! */
311
312 spi++;
313 while (spi < IPSEC_DOI_SPI_OUR_MIN0x1000 || spi == ntohl(avoid))
314 get_rnd_bytes((u_char *)&spi, sizeof(spi));
315
316 if (DBGP(DBG_BASE)(cur_debugging & (((lset_t)1 << (DBG_BASE_IX))))) {
317 ipsec_spi_t spi_net = htonl(spi);
318 DBG_dump("generate SPI:", (u_char *)&spi_net,
319 sizeof(spi_net));
320 }
321
322 return htonl(spi);
323 }
324}
325
326/* Generate Unique CPI numbers.
327 * The result is returned as an SPI (4 bytes) in network order!
328 * The real bits are in the nework-low-order 2 bytes.
329 * Modelled on get_ipsec_spi, but range is more limited:
330 * 256-61439.
331 * If we can't find one easily, return 0 (a bad SPI,
332 * no matter what order) indicating failure.
333 */
334ipsec_spi_t get_my_cpi(const struct spd_route *sr, bool_Bool tunnel)
335{
336 if (kernel_ops->get_spi != NULL((void*)0)) {
337 char text_said[SATOT_BUFsizeof(said_buf)];
338 set_text_said(text_said, &sr->this.host_addr, 0, &ip_protocol_comp);
339 return kernel_ops->get_spi(&sr->that.host_addr,
340 &sr->this.host_addr, &ip_protocol_comp,
341 tunnel,
342 get_proto_reqid(sr->reqid, &ip_protocol_comp),
343 IPCOMP_FIRST_NEGOTIATED256,
344 IPCOMP_LAST_NEGOTIATED61439,
345 text_said);
346 } else {
347 static cpi_t first_busy_cpi = 0;
348 static cpi_t latest_cpi = 0;
349
350 while (!(IPCOMP_FIRST_NEGOTIATED256 <= first_busy_cpi &&
351 first_busy_cpi < IPCOMP_LAST_NEGOTIATED61439)) {
352 get_rnd_bytes((u_char *)&first_busy_cpi,
353 sizeof(first_busy_cpi));
354 latest_cpi = first_busy_cpi;
355 }
356
357 latest_cpi++;
358
359 if (latest_cpi == first_busy_cpi)
360 find_my_cpi_gap(&latest_cpi, &first_busy_cpi);
361
362 if (latest_cpi > IPCOMP_LAST_NEGOTIATED61439)
363 latest_cpi = IPCOMP_FIRST_NEGOTIATED256;
364
365 return htonl((ipsec_spi_t)latest_cpi);
366 }
367}
368
369/*
370 * Remove all characters but [-_.0-9a-zA-Z] from a character string.
371 * Truncates the result if it would be too long.
372 */
373
374static void jam_clean_xauth_username(struct jambuf *buf, const char *src)
375{
376 bool_Bool changed = false0;
377 const char *dst = jambuf_cursor(buf);
378 while (*src != '\0') {
379 if ((*src >= '0' && *src <= '9') ||
380 (*src >= 'a' && *src <= 'z') ||
381 (*src >= 'A' && *src <= 'Z') ||
382 *src == '_' || *src == '-' || *src == '.') {
383 jam_char(buf, *src);
384 } else {
385 changed = true1;
386 }
387 src++;
388 }
389 if (changed || !jambuf_ok(buf)) {
390 libreswan_log("Warning: XAUTH username changed from '%s' to '%s'",loglog(RC_LOG, "Warning: XAUTH username changed from '%s' to '%s'"
, src, dst)
391 src, dst)loglog(RC_LOG, "Warning: XAUTH username changed from '%s' to '%s'"
, src, dst)
;
392 }
393}
394
395/*
396 * form the command string
397 *
398 * note: this mutates *st by calling get_sa_info().
399 */
400static void jam_common_shell_out(struct jambuf *buf, const struct connection *c,
401 const struct spd_route *sr, struct state *st,
402 bool_Bool inbytes, bool_Bool outbytes)
403{
404 ip_address ta;
405
406 char *id_vname = NULL((void*)0);
407
408 if (c->xfrmi != NULL((void*)0) && c->xfrmi->name != NULL((void*)0))
409 id_vname = c->xfrmi->name;
410 else
411 id_vname = "NULL";
412
413 /* change VERSION when interface spec changes */
414 jam(buf, "PLUTO_VERSION='2.0' ");
415 jam(buf, "PLUTO_CONNECTION='%s' ", c->name);
416 jam(buf, "PLUTO_VIRT_INTERFACE='%s' ", id_vname);
417 jam(buf, "PLUTO_INTERFACE='%s' ", c->interface == NULL((void*)0) ? "NULL" : c->interface->ip_dev->id_rname);
418 jam(buf, "PLUTO_XFRMI_ROUTE='%s' ", (c->xfrmi != NULL((void*)0) && c->xfrmi->if_id > 0) ? "yes" : "");
419
420 if (address_is_specified(&sr->this.host_nexthop)) {
421 jam(buf, "PLUTO_NEXT_HOP='");
422 jam_address(buf, &sr->this.host_nexthop);
423 jam(buf, "' ");
424 }
425
426 ipstr_buf bme;
427 jam(buf, "PLUTO_ME='%s' ", ipstr(&sr->this.host_addr, &bme));
428
429 jam(buf, "PLUTO_MY_ID='");
430 jam_id(buf, &sr->this.id, jam_meta_escaped_bytes);
431 jam(buf, "' ");
432
433 jam(buf, "PLUTO_MY_CLIENT='");
434 jam_subnet(buf, &sr->this.client);
435 jam(buf, "' ");
436
437 jam(buf, "PLUTO_MY_CLIENT_NET='");
438 ta = subnet_prefix(&sr->this.client);
439 jam_address(buf, &ta);
440 jam(buf, "' ");
441
442 jam(buf, "PLUTO_MY_CLIENT_MASK='");
443 ta = subnet_mask(&sr->this.client);
444 jam_address(buf, &ta);
445 jam(buf, "' ");
446
447 if (subnet_is_specified(&sr->this.host_vtiip)) {
448 jam(buf, "VTI_IP='");
449 jam_subnet(buf, &sr->this.host_vtiip);
450 jam(buf, "' ");
451 }
452
453 if (!isanyaddr(&sr->this.ifaceip.addr)) {
454 jam(buf, "INTERFACE_IP='");
455 jam_subnet(buf, &sr->this.ifaceip);
456 jam(buf, "' ");
457 }
458
459 jam(buf, "PLUTO_MY_PORT='%u' ", sr->this.port);
460 jam(buf, "PLUTO_MY_PROTOCOL='%u' ", sr->this.protocol);
461 jam(buf, "PLUTO_SA_REQID='%u' ", sr->reqid);
462 jam(buf, "PLUTO_SA_TYPE='%s' ", (st == NULL((void*)0) ? "none" :
463 st->st_esp.present ? "ESP" :
464 st->st_ah.present ? "AH" :
465 st->st_ipcomp.present ? "IPCOMP" :
466 "unknown?"));
467 ipstr_buf bpeer;
468 jam(buf, "PLUTO_PEER='%s' ", ipstr(&sr->that.host_addr, &bpeer));
469
470 jam(buf, "PLUTO_PEER_ID='");
471 jam_id(buf, &sr->that.id, jam_meta_escaped_bytes);
472 jam(buf, "' ");
473
474 jam(buf, "PLUTO_PEER_CLIENT='");
475 jam_subnet(buf, &sr->that.client);
476 jam(buf, "' ");
477
478 jam(buf, "PLUTO_PEER_CLIENT_NET='");
479 ta = subnet_prefix(&sr->that.client);
480 jam_address(buf, &ta);
481 jam(buf, "' ");
482
483 jam(buf, "PLUTO_PEER_CLIENT_MASK='");
484 ta = subnet_mask(&sr->that.client);
485 jam_address(buf, &ta);
486 jam(buf, "' ");
487
488 jam(buf, "PLUTO_PEER_PORT='%u' ", sr->that.port);
489 jam(buf, "PLUTO_PEER_PROTOCOL='%u' ", sr->that.protocol);
490
491 jam(buf, "PLUTO_PEER_CA='");
492 for (struct pubkey_list *p = pluto_pubkeys; p != NULL((void*)0); p = p->next) {
493 struct pubkey *key = p->key;
494 int pathlen; /* value ignored */
495 if (key->type == &pubkey_type_rsa &&
496 same_id(&sr->that.id, &key->id) &&
497 trusted_ca_nss(key->issuer, sr->that.ca, &pathlen)) {
498 jam_dn_or_null(buf, key->issuer, "", jam_meta_escaped_bytes);
499 break;
500 }
501 }
502 jam(buf, "' ");
503
504 jam(buf, "PLUTO_STACK='%s' ", kernel_ops->kern_name);
505
506 if (c->metric != 0) {
507 jam(buf, "PLUTO_METRIC=%d ", c->metric);
508 }
509
510 if (c->connmtu != 0) {
511 jam(buf, "PLUTO_MTU=%d ", c->connmtu);
512 }
513
514 jam(buf, "PLUTO_ADDTIME='%" PRIu64"l" "u" "' ", st == NULL((void*)0) ? (uint64_t)0 : st->st_esp.add_time);
515 jam(buf, "PLUTO_CONN_POLICY='%s%s' ", prettypolicy(c->policy), NEVER_NEGOTIATE(c->policy)(((((c->policy)) & (((lset_t)1 << (POLICY_ENCRYPT_IX
)) | ((lset_t)1 << (POLICY_AUTHENTICATE_IX)))) == ((lset_t
)0)))
? "+NEVER_NEGOTIATE" : "");
516 jam(buf, "PLUTO_CONN_KIND='%s' ", enum_show(&connection_kind_names, c->kind));
517 jam(buf, "PLUTO_CONN_ADDRFAMILY='ipv%d' ", address_type(&sr->this.host_addr)->ip_version);
518 jam(buf, "XAUTH_FAILED=%d ", (st != NULL((void*)0) && st->st_xauth_soft) ? 1 : 0);
519
520 if (st != NULL((void*)0) && st->st_xauth_username[0] != '\0') {
521 jam(buf, "PLUTO_USERNAME='");
522 jam_clean_xauth_username(buf, st->st_xauth_username);
523 jam(buf, "' ");
524 }
525
526 if (address_is_specified(&sr->this.host_srcip)) {
527 jam(buf, "PLUTO_MY_SOURCEIP='");
528 jam_address(buf, &sr->this.host_srcip);
529 jam(buf, "' ");
530 if (st != NULL((void*)0))
531 jam(buf, "PLUTO_MOBIKE_EVENT='%s' ",
532 st->st_mobike_del_src_ip ? "yes" : "");
533 }
534
535 jam(buf, "PLUTO_IS_PEER_CISCO='%u' ", c->remotepeertype /* ??? kind of odd printing an enum with %u */);
536 jam(buf, "PLUTO_PEER_DNS_INFO='%s' ", (st != NULL((void*)0) && st->st_seen_cfg_dns != NULL((void*)0)) ? st->st_seen_cfg_dns : "");
537 jam(buf, "PLUTO_PEER_DOMAIN_INFO='%s' ", (st != NULL((void*)0) && st->st_seen_cfg_domains != NULL((void*)0)) ? st->st_seen_cfg_domains : "");
538 jam(buf, "PLUTO_PEER_BANNER='%s' ", (st != NULL((void*)0) && st->st_seen_cfg_banner != NULL((void*)0)) ? st->st_seen_cfg_banner : "");
539 jam(buf, "PLUTO_CFG_SERVER='%u' ", sr->this.modecfg_server);
540 jam(buf, "PLUTO_CFG_CLIENT='%u' ", sr->this.modecfg_client);
541#ifdef HAVE_NM1
542 jam(buf, "PLUTO_NM_CONFIGURED='%u' ", c->nmconfigured);
543#endif
544
545 if (inbytes) {
546 jam(buf, "PLUTO_INBYTES='%" PRIu64"l" "u" "' ",
547 st->st_esp.present ? st->st_esp.our_bytes :
548 st->st_ah.present ? st->st_ah.our_bytes :
549 st->st_ipcomp.present ? st->st_ipcomp.our_bytes :
550 0);
551 }
552 if (outbytes) {
553 jam(buf, "PLUTO_OUTBYTES='%" PRIu64"l" "u" "' ",
554 st->st_esp.present ? st->st_esp.peer_bytes :
555 st->st_ah.present ? st->st_ah.peer_bytes :
556 st->st_ipcomp.present ? st->st_ipcomp.peer_bytes :
557 0);
558 }
559
560 if (c->nflog_group != 0) {
561 jam(buf, "NFLOG=%d ", c->nflog_group);
562 }
563
564 if (c->sa_marks.in.val != 0) {
565 jam(buf, "CONNMARK_IN=%" PRIu32"u" "/%#08" PRIx32"x" " ",
566 c->sa_marks.in.val, c->sa_marks.in.mask);
567 }
568 if (c->sa_marks.out.val != 0) {
569 jam(buf, "CONNMARK_OUT=%" PRIu32"u" "/%#08" PRIx32"x" " ",
570 c->sa_marks.out.val, c->sa_marks.out.mask);
571 }
572 if (c->xfrmi != NULL((void*)0) && c->xfrmi->if_id > 0) {
573 if (addrinsubnet(&sr->that.host_addr, &sr->that.client)) {
574 jam(buf, "PLUTO_XFRMI_FWMARK='%" PRIu32"u" "/0xffffffff' ",
575 c->xfrmi->if_id);
576 } else {
577 address_buf bpeer;
578 subnet_buf peerclient_str;
579 dbg("not adding PLUTO_XFRMI_FWMARK. PLUTO_PEER=%s is not inside PLUTO_PEER_CLIENT=%s",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("not adding PLUTO_XFRMI_FWMARK. PLUTO_PEER=%s is not inside PLUTO_PEER_CLIENT=%s"
, str_address(&sr->that.host_addr, &bpeer), str_subnet
(&sr->that.client, &peerclient_str)); } }
580 str_address(&sr->that.host_addr, &bpeer),{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("not adding PLUTO_XFRMI_FWMARK. PLUTO_PEER=%s is not inside PLUTO_PEER_CLIENT=%s"
, str_address(&sr->that.host_addr, &bpeer), str_subnet
(&sr->that.client, &peerclient_str)); } }
581 str_subnet(&sr->that.client, &peerclient_str)){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("not adding PLUTO_XFRMI_FWMARK. PLUTO_PEER=%s is not inside PLUTO_PEER_CLIENT=%s"
, str_address(&sr->that.host_addr, &bpeer), str_subnet
(&sr->that.client, &peerclient_str)); } }
;
582 jam(buf, "PLUTO_XFRMI_FWMARK='' ");
583 }
584 }
585 jam(buf, "VTI_IFACE='%s' ", c->vti_iface ? c->vti_iface : "");
586 jam(buf, "VTI_ROUTING='%s' ", bool_str(c->vti_routing));
587 jam(buf, "VTI_SHARED='%s' ", bool_str(c->vti_shared));
588
589 if (sr->this.has_cat) {
590 jam(buf, "CAT='YES' ");
591 }
592
593 jam(buf, "SPI_IN=0x%x SPI_OUT=0x%x " /* SPI_IN SPI_OUT */,
594 (st == NULL((void*)0) ? 0 : st->st_esp.present ? ntohl(st->st_esp.attrs.spi) :
595 st->st_ah.present ? ntohl(st->st_ah.attrs.spi) :
596 st->st_ipcomp.present ? ntohl(st->st_ipcomp.attrs.spi) : 0),
597 (st == NULL((void*)0) ? 0 : st->st_esp.present ? ntohl(st->st_esp.our_spi) :
598 st->st_ah.present ? ntohl(st->st_ah.our_spi) :
599 st->st_ipcomp.present ? ntohl(st->st_ipcomp.our_spi) : 0));
600}
601
602/*
603 * form the command string
604 *
605 * note: this mutates *st by calling fmt_traffic_str
606 */
607bool_Bool fmt_common_shell_out(char *buf, size_t blen, const struct connection *c,
608 const struct spd_route *sr, struct state *st)
609{
610 /*
611 * note: this mutates *st by calling get_sa_info
612 *
613 * XXX: does the get_sa_info() call order matter? Should this
614 * be a single "atomic" call?
615 *
616 * true==inbound: inbound updates OUR_BYTES; !inbound updates
617 * PEER_BYTES.
618 */
619 bool_Bool outbytes = st != NULL((void*)0) && get_sa_info(st, false0, NULL((void*)0));
620 bool_Bool inbytes = st != NULL((void*)0) && get_sa_info(st, true1, NULL((void*)0));
621 struct jambuf jambuf = array_as_jambuf(buf, blen);
622 jam_common_shell_out(&jambuf, c, sr, st, inbytes, outbytes);
623 return jambuf_ok(&jambuf);
624}
625
626bool_Bool do_command(const struct connection *c,
627 const struct spd_route *sr,
628 const char *verb,
629 struct state *st)
630{
631 const char *verb_suffix;
632
633 /*
634 * Support for skipping updown, eg leftupdown=""
635 * Useful on busy servers that do not need to use updown for anything
636 */
637 if (sr->this.updown == NULL((void*)0) || streq(sr->this.updown, "%disabled")(strcmp((sr->this.updown), ("%disabled")) == 0)) {
638 dbg("skipped updown %s command - disabled per policy", verb){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("skipped updown %s command - disabled per policy"
, verb); } }
;
639 return TRUE1;
640 }
641 dbg("running updown command \"%s\" for verb %s ", sr->this.updown, verb){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("running updown command \"%s\" for verb %s ", sr
->this.updown, verb); } }
;
642
643 /*
644 * Figure out which verb suffix applies.
645 * NOTE: this is a duplicate of code in mast_do_command_vs.
646 */
647 {
648 const char *hs, *cs;
649
650 switch (addrtypeof(&sr->this.host_addr)) {
651 case AF_INET2:
652 hs = "-host";
653 cs = "-client";
654 break;
655 case AF_INET610:
656 hs = "-host-v6";
657 cs = "-client-v6";
658 break;
659 default:
660 loglog(RC_LOG_SERIOUS, "unknown address family");
661 return FALSE0;
662 }
663 verb_suffix = subnetisaddr(&sr->this.client,(subnetishost(&sr->this.client) && addrinsubnet
((&sr->this.host_addr), (&sr->this.client)))
664 &sr->this.host_addr)(subnetishost(&sr->this.client) && addrinsubnet
((&sr->this.host_addr), (&sr->this.client)))
?
665 hs : cs;
666 }
667
668 dbg("command executing %s%s", verb, verb_suffix){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("command executing %s%s", verb, verb_suffix); }
}
;
669
670 if (kernel_ops->docommand == NULL((void*)0)) {
671 dbg("no do_command for method %s", kernel_ops->kern_name){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("no do_command for method %s", kernel_ops->
kern_name); } }
;
672 } else {
673 return (*kernel_ops->docommand)(c, sr, verb, verb_suffix, st);
674 }
675 return TRUE1;
676}
677
678bool_Bool invoke_command(const char *verb, const char *verb_suffix, const char *cmd)
679{
680# define CHUNK_WIDTH 80 /* units for cmd logging */
681 if (DBGP(DBG_BASE)(cur_debugging & (((lset_t)1 << (DBG_BASE_IX))))) {
682 int slen = strlen(cmd);
683 int i;
684
685 DBG_log("executing %s%s: %s",
686 verb, verb_suffix, cmd);
687 DBG_log("popen cmd is %d chars long", slen);
688 for (i = 0; i < slen; i += CHUNK_WIDTH)
689 DBG_log("cmd(%4d):%.*s:", i,
690 slen-i < CHUNK_WIDTH? slen-i : CHUNK_WIDTH,
691 &cmd[i]);
692 }
693# undef CHUNK_WIDTH
694
695
696 {
697 /*
698 * invoke the script, catching stderr and stdout
699 * It may be of concern that some file descriptors will
700 * be inherited. For the ones under our control, we
701 * have done fcntl(fd, F_SETFD, FD_CLOEXEC) to prevent this.
702 * Any used by library routines (perhaps the resolver or
703 * syslog) will remain.
704 */
705 FILE *f = popen(cmd, "r");
706
707 if (f == NULL((void*)0)) {
708#ifdef HAVE_BROKEN_POPEN
709 /*
710 * See bug #1067 Angstrom Linux on a arm7 has no
711 * popen()
712 */
713 if (errno(*__errno_location ()) == ENOSYS38) {
714 /*
715 * Try system(), though it will not give us
716 * output
717 */
718 DBG_log("unable to popen(), falling back to system()");
719 system(cmd);
720 return TRUE1;
721 }
722#endif
723 loglog(RC_LOG_SERIOUS, "unable to popen %s%s command",
724 verb, verb_suffix);
725 return FALSE0;
726 }
727
728 /* log any output */
729 for (;; ) {
730 /*
731 * if response doesn't fit in this buffer, it will
732 * be folded
733 */
734 char resp[256];
735
736 if (fgets(resp, sizeof(resp), f) == NULL((void*)0)) {
737 if (ferror(f)) {
738 LOG_ERRNO(errno, "fgets failed on output of %s%s command",{ int e_ = (*__errno_location ()); for (char lswbuf[((size_t)
1024)], *lswbuf_ = lswbuf; lswbuf_ != ((void*)0); lswbuf_ = (
(void*)0)) for (struct jambuf jambuf = array_as_jambuf((lswbuf
), sizeof(lswbuf)), *buf = &jambuf; buf != ((void*)0); buf
= ((void*)0)) { jam(buf, "ERROR: "); jam_cur_prefix(buf); jam
(buf, "fgets failed on output of %s%s command", verb, verb_suffix
); jam_string(buf, "."); jam(buf, " ""Errno %d: %s", (e_), strerror
(e_)); jambuf_to_error_stream(buf); } }
739 verb, verb_suffix){ int e_ = (*__errno_location ()); for (char lswbuf[((size_t)
1024)], *lswbuf_ = lswbuf; lswbuf_ != ((void*)0); lswbuf_ = (
(void*)0)) for (struct jambuf jambuf = array_as_jambuf((lswbuf
), sizeof(lswbuf)), *buf = &jambuf; buf != ((void*)0); buf
= ((void*)0)) { jam(buf, "ERROR: "); jam_cur_prefix(buf); jam
(buf, "fgets failed on output of %s%s command", verb, verb_suffix
); jam_string(buf, "."); jam(buf, " ""Errno %d: %s", (e_), strerror
(e_)); jambuf_to_error_stream(buf); } }
;
740 pclose(f);
741 return FALSE0;
742 } else {
743 passert(feof(f)){ _Bool assertion__ = feof(f); if (!assertion__) { lsw_passert_fail
((where_t) { .func = __func__, .basename = "kernel.c" , .line
= 743}, "%s", "feof(f)"); } }
;
744 break;
745 }
746 } else {
747 char *e = resp + strlen(resp);
748
749 if (e > resp && e[-1] == '\n')
750 e[-1] = '\0'; /* trim trailing '\n' */
751 libreswan_log("%s%s output: %s", verb,loglog(RC_LOG, "%s%s output: %s", verb, verb_suffix, resp)
752 verb_suffix, resp)loglog(RC_LOG, "%s%s output: %s", verb, verb_suffix, resp);
753 }
754 }
755
756 /* report on and react to return code */
757 {
758 int r = pclose(f);
759
760 if (r == -1) {
761 LOG_ERRNO(errno, "pclose failed for %s%s command",{ int e_ = (*__errno_location ()); for (char lswbuf[((size_t)
1024)], *lswbuf_ = lswbuf; lswbuf_ != ((void*)0); lswbuf_ = (
(void*)0)) for (struct jambuf jambuf = array_as_jambuf((lswbuf
), sizeof(lswbuf)), *buf = &jambuf; buf != ((void*)0); buf
= ((void*)0)) { jam(buf, "ERROR: "); jam_cur_prefix(buf); jam
(buf, "pclose failed for %s%s command", verb, verb_suffix); jam_string
(buf, "."); jam(buf, " ""Errno %d: %s", (e_), strerror(e_)); jambuf_to_error_stream
(buf); } }
762 verb, verb_suffix){ int e_ = (*__errno_location ()); for (char lswbuf[((size_t)
1024)], *lswbuf_ = lswbuf; lswbuf_ != ((void*)0); lswbuf_ = (
(void*)0)) for (struct jambuf jambuf = array_as_jambuf((lswbuf
), sizeof(lswbuf)), *buf = &jambuf; buf != ((void*)0); buf
= ((void*)0)) { jam(buf, "ERROR: "); jam_cur_prefix(buf); jam
(buf, "pclose failed for %s%s command", verb, verb_suffix); jam_string
(buf, "."); jam(buf, " ""Errno %d: %s", (e_), strerror(e_)); jambuf_to_error_stream
(buf); } }
;
763 return FALSE0;
764 } else if (WIFEXITED(r)(((r) & 0x7f) == 0)) {
765 if (WEXITSTATUS(r)(((r) & 0xff00) >> 8) != 0) {
766 loglog(RC_LOG_SERIOUS,
767 "%s%s command exited with status %d",
768 verb, verb_suffix,
769 WEXITSTATUS(r)(((r) & 0xff00) >> 8));
770 return FALSE0;
771 }
772 } else if (WIFSIGNALED(r)(((signed char) (((r) & 0x7f) + 1) >> 1) > 0)) {
773 loglog(RC_LOG_SERIOUS,
774 "%s%s command exited with signal %d",
775 verb, verb_suffix, WTERMSIG(r)((r) & 0x7f));
776 return FALSE0;
777 } else {
778 loglog(RC_LOG_SERIOUS,
779 "%s%s command exited with unknown status %d",
780 verb, verb_suffix, r);
781 return FALSE0;
782 }
783 }
784 }
785 return TRUE1;
786}
787
788/* Check that we can route (and eroute). Diagnose if we cannot. */
789
790enum routability {
791 route_impossible,
792 route_easy,
793 route_nearconflict,
794 route_farconflict,
795 route_unnecessary
796};
797
798/*
799 * handle co-terminal attempt of the "near" kind
800 *
801 * Note: it mutates both inside and outside
802 */
803static enum routability note_nearconflict(
804 struct connection *outside, /* CK_PERMANENT */
805 struct connection *inside) /* CK_TEMPLATE */
806{
807 char inst[CONN_INST_BUF(2 + 10 + 1 + sizeof(subnet_buf) + 7 + sizeof(address_reversed_buf
) + 3 + sizeof(subnet_buf) + 1 + 1)
];
808
809 /*
810 * this is a co-terminal attempt of the "near" kind.
811 * when chaining, we chain from inside to outside
812 *
813 * XXX permit multiple deep connections?
814 */
815 passert(inside->policy_next == NULL){ _Bool assertion__ = inside->policy_next == ((void*)0); if
(!assertion__) { lsw_passert_fail((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 815}, "%s", "inside->policy_next == NULL"
); } }
;
816
817 inside->policy_next = outside;
818
819 /*
820 * since we are going to steal the eroute from the secondary
821 * policy, we need to make sure that it no longer thinks that
822 * it owns the eroute.
823 */
824 outside->spd.eroute_owner = SOS_NOBODY0;
825 outside->spd.routing = RT_UNROUTED_KEYED;
826
827 /*
828 * set the priority of the new eroute owner to be higher
829 * than that of the current eroute owner
830 */
831 inside->prio = outside->prio + 1;
832
833 loglog(RC_LOG_SERIOUS,
834 "conflict on eroute (%s), switching eroute to %s and linking %s",
835 fmt_conn_instance(inside, inst),
836 inside->name, outside->name);
837
838 return route_nearconflict;
839}
840
841/*
842 * Note: this may mutate c
843 */
844static enum routability could_route(struct connection *c, struct logger *logger)
845{
846 dbg("could_route called for %s; kind=%s that.has_client=%s oppo=%s this.host_port=%u",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("could_route called for %s; kind=%s that.has_client=%s oppo=%s this.host_port=%u"
, c->name, enum_show(&connection_kind_names, c->kind
), bool_str(c->spd.that.has_client), bool_str(c->policy
& ((lset_t)1 << (POLICY_OPPORTUNISTIC_IX))), c->
spd.this.host_port); } }
847 c->name,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("could_route called for %s; kind=%s that.has_client=%s oppo=%s this.host_port=%u"
, c->name, enum_show(&connection_kind_names, c->kind
), bool_str(c->spd.that.has_client), bool_str(c->policy
& ((lset_t)1 << (POLICY_OPPORTUNISTIC_IX))), c->
spd.this.host_port); } }
848 enum_show(&connection_kind_names, c->kind),{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("could_route called for %s; kind=%s that.has_client=%s oppo=%s this.host_port=%u"
, c->name, enum_show(&connection_kind_names, c->kind
), bool_str(c->spd.that.has_client), bool_str(c->policy
& ((lset_t)1 << (POLICY_OPPORTUNISTIC_IX))), c->
spd.this.host_port); } }
849 bool_str(c->spd.that.has_client),{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("could_route called for %s; kind=%s that.has_client=%s oppo=%s this.host_port=%u"
, c->name, enum_show(&connection_kind_names, c->kind
), bool_str(c->spd.that.has_client), bool_str(c->policy
& ((lset_t)1 << (POLICY_OPPORTUNISTIC_IX))), c->
spd.this.host_port); } }
850 bool_str(c->policy & POLICY_OPPORTUNISTIC),{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("could_route called for %s; kind=%s that.has_client=%s oppo=%s this.host_port=%u"
, c->name, enum_show(&connection_kind_names, c->kind
), bool_str(c->spd.that.has_client), bool_str(c->policy
& ((lset_t)1 << (POLICY_OPPORTUNISTIC_IX))), c->
spd.this.host_port); } }
851 c->spd.this.host_port){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("could_route called for %s; kind=%s that.has_client=%s oppo=%s this.host_port=%u"
, c->name, enum_show(&connection_kind_names, c->kind
), bool_str(c->spd.that.has_client), bool_str(c->policy
& ((lset_t)1 << (POLICY_OPPORTUNISTIC_IX))), c->
spd.this.host_port); } }
;
852
853 /* it makes no sense to route a connection that is ISAKMP-only */
854 if (!NEVER_NEGOTIATE(c->policy)(((((c->policy)) & (((lset_t)1 << (POLICY_ENCRYPT_IX
)) | ((lset_t)1 << (POLICY_AUTHENTICATE_IX)))) == ((lset_t
)0)))
&& !HAS_IPSEC_POLICY(c->policy)(((c->policy) & (((lset_t)1 << (POLICY_NOPMTUDISC_IX
)) - ((lset_t)1 << (POLICY_ENCRYPT_IX)) + ((lset_t)1 <<
(POLICY_NOPMTUDISC_IX)))) != 0)
) {
855 log_message(RC_ROUTE, logger,
856 "cannot route an ISAKMP-only connection");
857 return route_impossible;
858 }
859
860 /*
861 * if this is a transport SA, and overlapping SAs are supported, then
862 * this route is not necessary at all.
863 */
864 if (kernel_ops->overlap_supported && !LIN(POLICY_TUNNEL, c->policy)(((((lset_t)1 << (POLICY_TUNNEL_IX))) & (c->policy
)) == (((lset_t)1 << (POLICY_TUNNEL_IX))))
)
865 return route_unnecessary;
866
867 /*
868 * if this is a Road Warrior template, we cannot route.
869 * Opportunistic template is OK.
870 */
871 if (!c->spd.that.has_client &&
872 c->kind == CK_TEMPLATE &&
873 !(c->policy & POLICY_OPPORTUNISTIC((lset_t)1 << (POLICY_OPPORTUNISTIC_IX)))) {
874 log_message(RC_ROUTE, logger,
875 "cannot route template policy of %s",
876 prettypolicy(c->policy));
877 return route_impossible;
878 }
879
880 /* if routing would affect IKE messages, reject */
881 if (c->spd.this.host_port != NAT_IKE_UDP_PORT4500 &&
882 c->spd.this.host_port != IKE_UDP_PORT500 &&
883 addrinsubnet(&c->spd.that.host_addr, &c->spd.that.client)) {
884 log_message(RC_LOG_SERIOUS, logger,
885 "cannot install route: peer is within its client");
886 return route_impossible;
887 }
888
889 struct spd_route *esr, *rosr;
890 struct connection *ero, /* who, if anyone, owns our eroute? */
891 *ro = route_owner(c, &c->spd, &rosr, &ero, &esr); /* who owns our route? */
892
893 /*
894 * If there is already a route for peer's client subnet
895 * and it disagrees about interface or nexthop, we cannot steal it.
896 * Note: if this connection is already routed (perhaps for another
897 * state object), the route will agree.
898 * This is as it should be -- it will arise during rekeying.
899 */
900 if (ro != NULL((void*)0) && !routes_agree(ro, c)((ro)->interface->ip_dev == (c)->interface->ip_dev
&& sameaddr(&(ro)->spd.this.host_nexthop, &
(c)->spd.this.host_nexthop))
) {
901
902 if (!compatible_overlapping_connections(c, ero)) {
903 /*
904 * Another connection is already using the eroute.
905 * TODO: XFRM can do this? For now excempt OE only
906 */
907 if ((c->policy & POLICY_OPPORTUNISTIC((lset_t)1 << (POLICY_OPPORTUNISTIC_IX))) == LEMPTY((lset_t)0)) {
908 connection_buf cib;
909 log_message(RC_LOG_SERIOUS, logger,
910 "cannot route -- route already in use for "PRI_CONNECTION"\"%s\"%s""",
911 pri_connection(ro, &cib)(ro)->name, str_connection_instance(ro, &cib));
912 return route_impossible;
913 } else {
914 connection_buf cib;
915 log_message(RC_LOG_SERIOUS, logger,
916 "cannot route -- route already in use for "PRI_CONNECTION"\"%s\"%s"" - but allowing anyway",
917 pri_connection(ro, &cib)(ro)->name, str_connection_instance(ro, &cib));
918 }
919 }
920 }
921
922
923 /* if there is an eroute for another connection, there is a problem */
924 if (ero != NULL((void*)0) && ero != c) {
925 /*
926 * note, wavesec (PERMANENT) goes *outside* and
927 * OE goes *inside* (TEMPLATE)
928 */
929 if (ero->kind == CK_PERMANENT &&
930 c->kind == CK_TEMPLATE) {
931 return note_nearconflict(ero, c);
932 } else if (c->kind == CK_PERMANENT &&
933 ero->kind == CK_TEMPLATE) {
934 return note_nearconflict(c, ero);
935 }
936
937 /* look along the chain of policies for one with the same name */
938
939 for (struct connection *ep = ero; ep != NULL((void*)0); ep = ero->policy_next) {
940 if (ep->kind == CK_TEMPLATE &&
941 streq(ep->name, c->name)(strcmp((ep->name), (c->name)) == 0))
942 return route_easy;
943 }
944
945 /*
946 * If we fell off the end of the list, then we found no
947 * TEMPLATE so there must be a conflict that we can't resolve.
948 * As the names are not equal, then we aren't
949 * replacing/rekeying.
950 *
951 * ??? should there not be a conflict if ANYTHING in the list,
952 * other than c, conflicts with c?
953 */
954
955 if (LDISJOINT(POLICY_OVERLAPIP, c->policy | ero->policy)(((((lset_t)1 << (POLICY_OVERLAPIP_IX))) & (c->policy
| ero->policy)) == ((lset_t)0))
) {
956 /*
957 * another connection is already using the eroute,
958 * TODO: XFRM apparently can do this though
959 */
960 connection_buf erob;
961 log_message(RC_LOG_SERIOUS, logger,
962 "cannot install eroute -- it is in use for "PRI_CONNECTION"\"%s\"%s"" #%lu",
963 pri_connection(ero, &erob)(ero)->name, str_connection_instance(ero, &erob), esr->eroute_owner);
964 return route_impossible;
965 }
966
967 connection_buf erob;
968 dbg("overlapping permitted with "PRI_CONNECTION" #%lu",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("overlapping permitted with ""\"%s\"%s"" #%lu"
, (ero)->name, str_connection_instance(ero, &erob), esr
->eroute_owner); } }
969 pri_connection(ero, &erob), esr->eroute_owner){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("overlapping permitted with ""\"%s\"%s"" #%lu"
, (ero)->name, str_connection_instance(ero, &erob), esr
->eroute_owner); } }
;
970 }
971 return route_easy;
972}
973
974bool_Bool trap_connection(struct connection *c, struct fd *whackfd)
975{
976 struct logger logger = CONNECTION_LOGGER(c, whackfd)(struct logger) { .where = (where_t) { .func = __func__, .basename
= "kernel.c" , .line = 976}, .global_whackfd = whackfd, .object
= c, .object_vec = &logger_connection_vec, }
;
977 enum routability r = could_route(c, &logger);
978
979 switch (r) {
980 case route_impossible:
981 return FALSE0;
982
983 case route_easy:
984 case route_nearconflict:
985 /*
986 * RT_ROUTED_TUNNEL is treated specially: we don't override
987 * because we don't want to lose track of the IPSEC_SAs etc.
988 * ??? The test treats RT_UNROUTED_KEYED specially too.
989 */
990 if (c->spd.routing < RT_ROUTED_TUNNEL)
991 return route_and_eroute(c, &c->spd, NULL((void*)0));
992
993 return TRUE1;
994
995 case route_farconflict:
996 return FALSE0;
997
998 case route_unnecessary:
999 return TRUE1;
1000 default:
1001 bad_case(r)libreswan_bad_case("r", (r), (where_t) { .func = __func__, .basename
= "kernel.c" , .line = 1001})
;
1002 }
1003}
1004
1005/*
1006 * Add/replace/delete a shunt eroute.
1007 *
1008 * Such an eroute determines the fate of packets without the use
1009 * of any SAs. These are defaults, in effect.
1010 * If a negotiation has not been attempted, use %trap.
1011 * If negotiation has failed, the choice between %trap/%pass/%drop/%reject
1012 * is specified in the policy of connection c.
1013 */
1014static bool_Bool shunt_eroute(const struct connection *c,
1015 const struct spd_route *sr,
1016 enum routing_t rt_kind,
1017 enum pluto_sadb_operations op,
1018 const char *opname)
1019{
1020 if (DBGP(DBG_BASE)(cur_debugging & (((lset_t)1 << (DBG_BASE_IX))))) {
1021 selector_buf thisb, thatb;
1022 DBG_log("shunt_eroute() called for connection '%s' to '%s' for rt_kind '%s' using protoports %s --%d->- %s",
1023 c->name, opname, enum_name(&routing_story, rt_kind),
1024 str_selector(&sr->this.client, &thisb),
1025 sr->this.protocol,
1026 str_selector(&sr->that.client, &thatb));
1027 }
1028
1029 if (kernel_ops->shunt_eroute != NULL((void*)0)) {
1030 return kernel_ops->shunt_eroute(c, sr, rt_kind, op, opname);
1031 }
1032
1033 loglog(RC_COMMENT, "no shunt_eroute implemented for %s interface",
1034 kernel_ops->kern_name);
1035 return TRUE1;
1036}
1037
1038static bool_Bool sag_eroute(const struct state *st,
1039 const struct spd_route *sr,
1040 enum pluto_sadb_operations op,
1041 const char *opname)
1042{
1043 pexpect(kernel_ops->sag_eroute != NULL)({ _Bool assertion__ = kernel_ops->sag_eroute != ((void*)0
); if (!assertion__) { log_pexpect((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 1043}, "%s", "kernel_ops->sag_eroute != NULL"
); } assertion__; })
;
1044 if (kernel_ops->sag_eroute != NULL((void*)0))
1045 return kernel_ops->sag_eroute(st, sr, op, opname);
1046
1047 return FALSE0;
1048}
1049
1050void migration_up(struct connection *c, struct state *st)
1051{
1052 for (struct spd_route *sr = &c->spd; sr != NULL((void*)0); sr = sr->spd_next) {
1053#ifdef IPSEC_CONNECTION_LIMIT
1054 num_ipsec_eroute++;
1055#endif
1056 sr->routing = RT_ROUTED_TUNNEL; /* do now so route_owner won't find us */
1057 (void) do_command(c, sr, "up", st);
1058 (void) do_command(c, sr, "route", st);
1059 }
1060}
1061
1062void migration_down(struct connection *c, struct state *st)
1063{
1064 for (struct spd_route *sr = &c->spd; sr != NULL((void*)0); sr = sr->spd_next) {
1065 enum routing_t cr = sr->routing;
1066
1067#ifdef IPSEC_CONNECTION_LIMIT
1068 if (erouted(cr)((cr) != RT_UNROUTED))
1069 num_ipsec_eroute--;
1070#endif
1071
1072 sr->routing = RT_UNROUTED; /* do now so route_owner won't find us */
1073
1074 /* only unroute if no other connection shares it */
1075 if (routed(cr)((cr) > RT_UNROUTED_HOLD) && route_owner(c, sr, NULL((void*)0), NULL((void*)0), NULL((void*)0)) == NULL((void*)0)) {
1076 (void) do_command(c, sr, "down", st);
1077 st->st_mobike_del_src_ip = true1;
1078 (void) do_command(c, sr, "unroute", st);
1079 st->st_mobike_del_src_ip = false0;
1080 }
1081 }
1082}
1083
1084
1085/* delete any eroute for a connection and unroute it if route isn't shared */
1086void unroute_connection(struct connection *c)
1087{
1088 for (struct spd_route *sr = &c->spd; sr != NULL((void*)0); sr = sr->spd_next) {
1089 enum routing_t cr = sr->routing;
1090
1091 if (erouted(cr)((cr) != RT_UNROUTED)) {
1092 /* cannot handle a live one */
1093 passert(cr != RT_ROUTED_TUNNEL){ _Bool assertion__ = cr != RT_ROUTED_TUNNEL; if (!assertion__
) { lsw_passert_fail((where_t) { .func = __func__, .basename =
"kernel.c" , .line = 1093}, "%s", "cr != RT_ROUTED_TUNNEL");
} }
;
1094 shunt_eroute(c, sr, RT_UNROUTED, ERO_DELETE, "delete");
1095#ifdef IPSEC_CONNECTION_LIMIT
1096 num_ipsec_eroute--;
1097#endif
1098 }
1099
1100 sr->routing = RT_UNROUTED; /* do now so route_owner won't find us */
1101
1102 /* only unroute if no other connection shares it */
1103 if (routed(cr)((cr) > RT_UNROUTED_HOLD) && route_owner(c, sr, NULL((void*)0), NULL((void*)0), NULL((void*)0)) == NULL((void*)0))
1104 (void) do_command(c, sr, "unroute", NULL((void*)0));
1105 }
1106}
1107
1108#include "kernel_alg.h"
1109
1110void set_text_said(char *text_said, const ip_address *dst,
1111 ipsec_spi_t spi, const struct ip_protocol *sa_proto)
1112{
1113 ip_said said = said3(dst, spi, sa_proto);
1114 struct jambuf jam = array_as_jambuf(text_said, SATOT_BUFsizeof(said_buf));
1115 jam_said(&jam, &said);
1116}
1117
1118/* find an entry in the bare_shunt table.
1119 * Trick: return a pointer to the pointer to the entry;
1120 * this allows the entry to be deleted.
1121 */
1122struct bare_shunt **bare_shunt_ptr(const ip_selector *our_client,
1123 const ip_selector *peer_client,
1124 int transport_proto)
1125
1126{
1127 struct bare_shunt *p, **pp;
1128
1129 for (pp = &bare_shunts; (p = *pp) != NULL((void*)0); pp = &p->next) {
1130 if (transport_proto == p->transport_proto &&
1131 selector_eq(our_client, &p->our_client) &&
1132 selector_eq(peer_client, &p->peer_client)) {
1133 return pp;
1134 }
1135 }
1136 return NULL((void*)0);
1137}
1138
1139/* free a bare_shunt entry, given a pointer to the pointer */
1140static void free_bare_shunt(struct bare_shunt **pp)
1141{
1142 struct bare_shunt *p;
1143
1144 passert(pp != NULL){ _Bool assertion__ = pp != ((void*)0); if (!assertion__) { lsw_passert_fail
((where_t) { .func = __func__, .basename = "kernel.c" , .line
= 1144}, "%s", "pp != NULL"); } }
;
1145
1146 p = *pp;
1147
1148 *pp = p->next;
1149 dbg_bare_shunt("delete", p);
1150 pfreeany(p->from_cn){ typeof(p->from_cn) *pp_ = &(p->from_cn); if (*pp_
!= ((void*)0)) { pfree(*pp_); *pp_ = ((void*)0); } }
;
1151 pfree(p);
1152}
1153
1154unsigned shunt_count(void)
1155{
1156 unsigned i = 0;
1157
1158 for (const struct bare_shunt *bs = bare_shunts; bs != NULL((void*)0); bs = bs->next)
1159 {
1160 i++;
1161 }
1162
1163 return i;
1164}
1165
1166void show_shunt_status(struct show *s)
1167{
1168 show_separator(s);
1169 show_comment(s, "Bare Shunt list:");
1170 show_separator(s);
1171
1172 for (const struct bare_shunt *bs = bare_shunts; bs != NULL((void*)0); bs = bs->next) {
1173 /* Print interesting fields. Ignore count and last_active. */
1174 selector_buf ourb;
1175 selector_buf peerb;
1176 said_buf sat;
1177
1178 char prio[POLICY_PRIO_BUF(3 + 1 + 3 + 1 + 10)];
1179 fmt_policy_prio(bs->policy_prio, prio);
1180
1181 show_comment(s, "%s -%d-> %s => %s %s %s",
1182 str_selector(&(bs)->our_client, &ourb),
1183 bs->transport_proto,
1184 str_selector(&(bs)->peer_client, &peerb),
1185 str_said(&(bs)->said, &sat),
1186 prio, bs->why);
1187 }
1188}
1189
1190/* Setup an IPsec route entry.
1191 * op is one of the ERO_* operators.
1192 */
1193
1194// should be made static again once we fix initiate.c calling this directly!
1195bool_Bool raw_eroute(const ip_address *this_host,
1196 const ip_subnet *this_client,
1197 const ip_address *that_host,
1198 const ip_subnet *that_client,
1199 ipsec_spi_t cur_spi,
1200 ipsec_spi_t new_spi,
1201 const struct ip_protocol *sa_proto,
1202 unsigned int transport_proto,
1203 enum eroute_type esatype,
1204 const struct pfkey_proto_info *proto_info,
1205 deltatime_t use_lifetime,
1206 uint32_t sa_priority,
1207 const struct sa_marks *sa_marks,
1208 const uint32_t xfrm_if_id,
1209 enum pluto_sadb_operations op,
1210 const char *opname,
1211 const char *policy_label)
1212{
1213 char text_said[SATOT_BUFsizeof(said_buf) + SATOT_BUFsizeof(said_buf)];
1214
1215 switch (op) {
1216 case ERO_ADD:
1217 case ERO_ADD_INBOUND:
1218 set_text_said(text_said, that_host, new_spi, sa_proto);
1219 break;
1220 case ERO_DELETE:
1221 case ERO_DEL_INBOUND:
1222 set_text_said(text_said, that_host, cur_spi, sa_proto);
1223 break;
1224 case ERO_REPLACE:
1225 case ERO_REPLACE_INBOUND:
1226 {
1227 size_t w;
1228
1229 set_text_said(text_said, that_host, cur_spi, sa_proto);
1230 w = strlen(text_said);
1231 text_said[w] = '>';
1232 set_text_said(text_said + w + 1, that_host, new_spi, sa_proto);
1233 break;
1234 }
1235 default:
1236 bad_case(op)libreswan_bad_case("op", (op), (where_t) { .func = __func__, .
basename = "kernel.c" , .line = 1236})
;
1237 }
1238
1239 if (DBGP(DBG_BASE)(cur_debugging & (((lset_t)1 << (DBG_BASE_IX))))) {
1240 selector_buf mybuf;
1241 selector_buf peerbuf;
1242 DBG_log("%s eroute %s --%d-> %s => %s using reqid %d (raw_eroute) proto=%d",
1243 opname,
1244 str_selector(this_client, &mybuf),
1245 transport_proto,
1246 str_selector(that_client, &peerbuf),
1247 text_said,
1248 proto_info->reqid,
1249 proto_info->proto);
1250
1251 if (policy_label != NULL((void*)0))
1252 DBG_log("policy security label %s",
1253 policy_label);
1254 }
1255
1256 bool_Bool result = kernel_ops->raw_eroute(this_host, this_client,
1257 that_host, that_client,
1258 cur_spi, new_spi, sa_proto,
1259 transport_proto,
1260 esatype, proto_info,
1261 use_lifetime, sa_priority, sa_marks,
1262 xfrm_if_id, op, text_said,
1263 policy_label);
1264 dbg("raw_eroute result=%s", result ? "success" : "failed"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("raw_eroute result=%s", result ? "success" : "failed"
); } }
;
1265
1266 return result;
1267}
1268
1269/*
1270 * Clear any bare shunt holds that overlap with the network we have
1271 * just routed. We only consider "narrow" holds: ones for a single
1272 * address to single address.
1273 */
1274static void clear_narrow_holds(const ip_selector *our_client,
1275 const ip_selector *peer_client,
1276 int transport_proto)
1277{
1278 struct bare_shunt *p, **pp;
1279
1280 for (pp = &bare_shunts; (p = *pp) != NULL((void*)0); ) {
1281 /*
1282 * is p->{local,remote} within {local,remote}.
1283 */
1284 if (p->said.spi == htonl(SPI_HOLD259) &&
1285 transport_proto == p->transport_proto &&
1286 selector_in_selector(&p->our_client, our_client) &&
1287 selector_in_selector(&p->peer_client, peer_client)) {
1288 if (!delete_bare_shunt(&p->our_client.addr, &p->peer_client.addr,
1289 transport_proto, SPI_HOLD259,
1290 "removing clashing narrow hold")) {
1291 /* ??? we could not delete a bare shunt */
1292 log_bare_shunt(RC_LOG, "failed to delete", p);
1293 break; /* unlikely to succeed a second time */
1294 } else if (*pp == p) {
1295 /*
1296 * ??? We deleted the wrong bare shunt!
1297 * This happened because more than one entry
1298 * matched and we happened to delete a
1299 * different one.
1300 * Log it! And keep deleting.
1301 */
1302 log_bare_shunt(RC_LOG, "UNEXPECTEDLY SURVIVING", p);
1303 pp = &bare_shunts; /* just in case, start over */
1304 }
1305 /*
1306 * ??? if we were sure that there could only be one
1307 * matching entry, we could break out of the FOR.
1308 * For an unknown reason this is not always the case,
1309 * so we will continue the loop, with pp unchanged.
1310 */
1311 } else {
1312 pp = &p->next;
1313 }
1314 }
1315}
1316
1317/*
1318 * Replace (or delete) a shunt that is in the bare_shunts table.
1319 * Issues the PF_KEY commands and updates the bare_shunts table.
1320 */
1321static bool_Bool fiddle_bare_shunt(const ip_address *src, const ip_address *dst,
1322 policy_prio_t policy_prio, /* of replacing shunt*/
1323 ipsec_spi_t cur_shunt_spi, /* in host order! */
1324 ipsec_spi_t new_shunt_spi, /* in host order! */
1325 bool_Bool repl, /* if TRUE, replace; if FALSE, delete */
1326 int transport_proto,
1327 const char *why)
1328{
1329 ip_subnet this_client, that_client;
1330 const ip_address null_host = address_any(address_type(src));
1331
1332 dbg("fiddle_bare_shunt called"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("fiddle_bare_shunt called"); } }
;
1333
1334 passert(addrtypeof(src) == addrtypeof(dst)){ _Bool assertion__ = addrtypeof(src) == addrtypeof(dst); if (
!assertion__) { lsw_passert_fail((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 1334}, "%s", "addrtypeof(src) == addrtypeof(dst)"
); } }
;
1335 happy(endtosubnet(src, &this_client, HERE)){ err_t ugh = endtosubnet(src, &this_client, (where_t) { .
func = __func__, .basename = "kernel.c" , .line = 1335}); if (
ugh != ((void*)0)) { lsw_passert_fail((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 1335}, "%s", ugh); } }
;
1336 happy(endtosubnet(dst, &that_client, HERE)){ err_t ugh = endtosubnet(dst, &that_client, (where_t) { .
func = __func__, .basename = "kernel.c" , .line = 1336}); if (
ugh != ((void*)0)) { lsw_passert_fail((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 1336}, "%s", ugh); } }
;
1337
1338 /*
1339 * ??? this comment might be obsolete.
1340 * If the transport protocol is not the wildcard (0), then we need
1341 * to look for a host<->host shunt, and replace that with the
1342 * shunt spi, and then we add a %HOLD for what was there before.
1343 *
1344 * This is at odds with !repl, which should delete things.
1345 *
1346 */
1347
1348 dbg("fiddle_bare_shunt with transport_proto %d", transport_proto){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("fiddle_bare_shunt with transport_proto %d", transport_proto
); } }
;
1349
1350 enum pluto_sadb_operations op = repl ? ERO_REPLACE : ERO_DELETE;
1351
1352 dbg("%s specific host-to-host bare shunt", repl ? "replacing" : "removing"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("%s specific host-to-host bare shunt", repl ? "replacing"
: "removing"); } }
;
1353 if (kernel_ops->type == USE_XFRM && strstr(why, "IGNORE_ON_XFRM:") != NULL((void*)0)) {
1354 dbg("skipping raw_eroute because IGNORE_ON_XFRM"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("skipping raw_eroute because IGNORE_ON_XFRM");
} }
;
1355 struct bare_shunt **bs_pp = bare_shunt_ptr(
1356 &this_client,
1357 &that_client,
1358 transport_proto);
1359
1360 free_bare_shunt(bs_pp);
1361 libreswan_log("raw_eroute() to op='%s' with transport_proto='%d' kernel shunt skipped - deleting from pluto shunt table",loglog(RC_LOG, "raw_eroute() to op='%s' with transport_proto='%d' kernel shunt skipped - deleting from pluto shunt table"
, repl ? "replace" : "delete", transport_proto)
1362 repl ? "replace" : "delete",loglog(RC_LOG, "raw_eroute() to op='%s' with transport_proto='%d' kernel shunt skipped - deleting from pluto shunt table"
, repl ? "replace" : "delete", transport_proto)
1363 transport_proto)loglog(RC_LOG, "raw_eroute() to op='%s' with transport_proto='%d' kernel shunt skipped - deleting from pluto shunt table"
, repl ? "replace" : "delete", transport_proto)
;
1364 return TRUE1;
1365 } else if (raw_eroute(&null_host, &this_client,
1366 &null_host, &that_client,
1367 htonl(cur_shunt_spi),
1368 htonl(new_shunt_spi),
1369 &ip_protocol_internal, transport_proto,
1370 ET_INT, null_proto_info,
1371 deltatime(SHUNT_PATIENCE((2 * 10) * 15 / 2)),
1372 0, /* we don't know connection for priority yet */
1373 NULL((void*)0), /* sa_marks */
1374 0 /* xfrm interface id */,
1375
1376 op, why, NULL((void*)0)))
1377 {
1378 struct bare_shunt **bs_pp = bare_shunt_ptr(
1379 &this_client,
1380 &that_client,
1381 transport_proto);
1382
1383 dbg("raw_eroute with op='%s' for transport_proto='%d' kernel shunt succeeded, bare shunt lookup %s",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("raw_eroute with op='%s' for transport_proto='%d' kernel shunt succeeded, bare shunt lookup %s"
, repl ? "replace" : "delete", transport_proto, (bs_pp == ((void
*)0)) ? "failed" : "succeeded"); } }
1384 repl ? "replace" : "delete", transport_proto,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("raw_eroute with op='%s' for transport_proto='%d' kernel shunt succeeded, bare shunt lookup %s"
, repl ? "replace" : "delete", transport_proto, (bs_pp == ((void
*)0)) ? "failed" : "succeeded"); } }
1385 (bs_pp == NULL) ? "failed" : "succeeded"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("raw_eroute with op='%s' for transport_proto='%d' kernel shunt succeeded, bare shunt lookup %s"
, repl ? "replace" : "delete", transport_proto, (bs_pp == ((void
*)0)) ? "failed" : "succeeded"); } }
;
1386
1387 /* we can have proto mismatching acquires with xfrm - this is a bad workaround */
1388 /* ??? what is the nature of those mismatching acquires? */
1389 /* passert(bs_pp != NULL); */
1390 if (bs_pp == NULL((void*)0)) {
1391 ipstr_buf srcb, dstb;
1392
1393 libreswan_log("can't find expected bare shunt to %s: %s->%s transport_proto='%d'",loglog(RC_LOG, "can't find expected bare shunt to %s: %s->%s transport_proto='%d'"
, repl ? "replace" : "delete", ipstr(src, &srcb), ipstr(dst
, &dstb), transport_proto)
1394 repl ? "replace" : "delete",loglog(RC_LOG, "can't find expected bare shunt to %s: %s->%s transport_proto='%d'"
, repl ? "replace" : "delete", ipstr(src, &srcb), ipstr(dst
, &dstb), transport_proto)
1395 ipstr(src, &srcb), ipstr(dst, &dstb),loglog(RC_LOG, "can't find expected bare shunt to %s: %s->%s transport_proto='%d'"
, repl ? "replace" : "delete", ipstr(src, &srcb), ipstr(dst
, &dstb), transport_proto)
1396 transport_proto)loglog(RC_LOG, "can't find expected bare shunt to %s: %s->%s transport_proto='%d'"
, repl ? "replace" : "delete", ipstr(src, &srcb), ipstr(dst
, &dstb), transport_proto)
;
1397 return TRUE1;
1398 }
1399
1400 if (repl) {
1401 /*
1402 * change over to new bare eroute
1403 * ours, peers, transport_proto are the same.
1404 */
1405 struct bare_shunt *bs = *bs_pp;
1406
1407 bs->why = why;
1408 bs->policy_prio = policy_prio;
1409 bs->said = said3(&null_host, htonl(new_shunt_spi), &ip_protocol_internal);
1410 bs->count = 0;
1411 bs->last_activity = mononow();
1412 dbg_bare_shunt("change", bs);
1413 } else {
1414 /* delete pluto bare shunt */
1415 free_bare_shunt(bs_pp);
1416 }
1417 return TRUE1;
1418 } else {
1419 struct bare_shunt **bs_pp = bare_shunt_ptr(
1420 &this_client,
1421 &that_client,
1422 transport_proto);
1423
1424 free_bare_shunt(bs_pp);
1425 libreswan_log("raw_eroute() to op='%s' with transport_proto='%d' kernel shunt failed - deleting from pluto shunt table",loglog(RC_LOG, "raw_eroute() to op='%s' with transport_proto='%d' kernel shunt failed - deleting from pluto shunt table"
, repl ? "replace" : "delete", transport_proto)
1426 repl ? "replace" : "delete",loglog(RC_LOG, "raw_eroute() to op='%s' with transport_proto='%d' kernel shunt failed - deleting from pluto shunt table"
, repl ? "replace" : "delete", transport_proto)
1427 transport_proto)loglog(RC_LOG, "raw_eroute() to op='%s' with transport_proto='%d' kernel shunt failed - deleting from pluto shunt table"
, repl ? "replace" : "delete", transport_proto)
;
1428
1429 return FALSE0;
1430 }
1431}
1432
1433bool_Bool replace_bare_shunt(const ip_address *src, const ip_address *dst,
1434 policy_prio_t policy_prio, /* of replacing shunt*/
1435 ipsec_spi_t cur_shunt_spi, /* in host order! */
1436 ipsec_spi_t new_shunt_spi, /* in host order! */
1437 int transport_proto,
1438 const char *why)
1439{
1440 return fiddle_bare_shunt(src, dst, policy_prio, cur_shunt_spi, new_shunt_spi, TRUE1, transport_proto, why);
1441}
1442
1443bool_Bool delete_bare_shunt(const ip_address *src, const ip_address *dst,
1444 int transport_proto, ipsec_spi_t cur_shunt_spi,
1445 const char *why)
1446{
1447 return fiddle_bare_shunt(src, dst, BOTTOM_PRIO((policy_prio_t)0), cur_shunt_spi, SPI_PASS256 /* unused */, FALSE0, transport_proto, why);
1448}
1449
1450bool_Bool eroute_connection(const struct spd_route *sr,
1451 ipsec_spi_t cur_spi,
1452 ipsec_spi_t new_spi,
1453 const struct ip_protocol *sa_proto,
1454 enum eroute_type esatype,
1455 const struct pfkey_proto_info *proto_info,
1456 uint32_t sa_priority,
1457 const struct sa_marks *sa_marks,
1458 const uint32_t xfrm_if_id,
1459 unsigned int op,
1460 const char *opname,
1461 const char *policy_label)
1462{
1463 ip_address peer = sr->that.host_addr;
1464 char buf2[256];
1465
1466 snprintf(buf2, sizeof(buf2),
1467 "eroute_connection %s", opname);
1468
1469 if (sa_proto == &ip_protocol_internal)
1470 peer = address_any(address_type(&peer));
1471
1472 if (sr->this.has_cat) {
1473 ip_subnet client;
1474
1475 endtosubnet(&sr->this.host_addr, &client, HERE(where_t) { .func = __func__, .basename = "kernel.c" , .line =
1475}
);
1476 bool_Bool t = raw_eroute(&sr->this.host_addr, &client,
1477 &peer, &sr->that.client,
1478 cur_spi,
1479 new_spi,
1480 sa_proto,
1481 sr->this.protocol,
1482 esatype,
1483 proto_info,
1484 deltatime(0),
1485 sa_priority, sa_marks,
1486 xfrm_if_id,
1487 op, buf2,
1488 policy_label);
1489 if (!t)
1490 libreswan_log("CAT: failed to eroute additional Client Address Translation policy")loglog(RC_LOG, "CAT: failed to eroute additional Client Address Translation policy"
)
;
1491
1492 dbg("%s CAT extra route added return=%d", __func__, t){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("%s CAT extra route added return=%d", __func__
, t); } }
;
1493 }
1494
1495 return raw_eroute(&sr->this.host_addr, &sr->this.client,
1496 &peer, &sr->that.client,
1497 cur_spi,
1498 new_spi,
1499 sa_proto,
1500 sr->this.protocol,
1501 esatype,
1502 proto_info,
1503 deltatime(0),
1504 sa_priority, sa_marks,
1505 xfrm_if_id,
1506 op, buf2,
1507 policy_label);
1508}
1509
1510/* assign a bare hold or pass to a connection */
1511
1512bool_Bool assign_holdpass(const struct connection *c,
1513 struct spd_route *sr,
1514 int transport_proto, ipsec_spi_t negotiation_shunt,
1515 const ip_address *src, const ip_address *dst)
1516{
1517 /*
1518 * either the automatically installed %hold eroute is broad enough
1519 * or we try to add a broader one and delete the automatic one.
1520 * Beware: this %hold might be already handled, but still squeak
1521 * through because of a race.
1522 */
1523 enum routing_t ro = sr->routing, /* routing, old */
1524 rn = ro; /* routing, new */
1525
1526 passert(LHAS(LELEM(CK_PERMANENT) | LELEM(CK_INSTANCE), c->kind)){ _Bool assertion__ = (((((lset_t)1 << (CK_PERMANENT)) |
((lset_t)1 << (CK_INSTANCE))) & ((lset_t)1 <<
(c->kind))) != ((lset_t)0)); if (!assertion__) { lsw_passert_fail
((where_t) { .func = __func__, .basename = "kernel.c" , .line
= 1526}, "%s", "LHAS(LELEM(CK_PERMANENT) | LELEM(CK_INSTANCE), c->kind)"
); } }
;
1527 /* figure out what routing should become */
1528 switch (ro) {
1529 case RT_UNROUTED:
1530 rn = RT_UNROUTED_HOLD;
1531 break;
1532 case RT_ROUTED_PROSPECTIVE:
1533 rn = RT_ROUTED_HOLD;
1534 break;
1535 default:
1536 /* no change: this %hold or %pass is old news */
1537 break;
1538 }
1539
1540 dbg("assign hold, routing was %s, needs to be %s",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("assign hold, routing was %s, needs to be %s",
enum_name(&routing_story, ro), enum_name(&routing_story
, rn)); } }
1541 enum_name(&routing_story, ro),{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("assign hold, routing was %s, needs to be %s",
enum_name(&routing_story, ro), enum_name(&routing_story
, rn)); } }
1542 enum_name(&routing_story, rn)){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("assign hold, routing was %s, needs to be %s",
enum_name(&routing_story, ro), enum_name(&routing_story
, rn)); } }
;
1543
1544 if (eclipsable(sr)(subnetishost(&(sr)->this.client) && subnetishost
(&(sr)->that.client))
) {
1545 /*
1546 * Although %hold or %pass is appropriately broad, it will
1547 * no longer be bare so we must ditch it from the bare table
1548 */
1549 struct bare_shunt **old = bare_shunt_ptr(&sr->this.client, &sr->that.client, sr->this.protocol);
1550
1551 if (old == NULL((void*)0)) {
1552 /* ??? should this happen? It does. */
1553 libreswan_log("assign_holdpass() no bare shunt to remove? - mismatch?")loglog(RC_LOG, "assign_holdpass() no bare shunt to remove? - mismatch?"
)
;
1554 } else {
1555 /* ??? should this happen? */
1556 dbg("assign_holdpass() removing bare shunt"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("assign_holdpass() removing bare shunt"); } }
;
1557 free_bare_shunt(old);
1558 }
1559 } else {
1560 dbg("assign_holdpass() need broad(er) shunt"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("assign_holdpass() need broad(er) shunt"); } }
;
1561 /*
1562 * we need a broad %hold, not the narrow one.
1563 * First we ensure that there is a broad %hold.
1564 * There may already be one (race condition): no need to
1565 * create one.
1566 * There may already be a %trap: replace it.
1567 * There may not be any broad eroute: add %hold.
1568 * Once the broad %hold is in place, delete the narrow one.
1569 */
1570 if (rn != ro) {
1571 int op;
1572 const char *reason;
1573
1574 if (erouted(ro)((ro) != RT_UNROUTED)) {
1575 op = ERO_REPLACE;
1576 reason = "replace %trap with broad %pass or %hold";
1577 } else {
1578 op = ERO_ADD;
1579 reason = "add broad %pass or %hold";
1580 }
1581
1582 if (eroute_connection(sr,
1583 htonl(SPI_HOLD259), /* kernel induced */
1584 htonl(negotiation_shunt),
1585 &ip_protocol_internal, ET_INT,
1586 null_proto_info,
1587 calculate_sa_prio(c, FALSE0),
1588 NULL((void*)0), 0 /* xfrm_if_id */,
1589 op,
1590 reason,
1591 c->policy_label))
1592 {
1593 dbg("assign_holdpass() eroute_connection() done"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("assign_holdpass() eroute_connection() done");
} }
;
1594 } else {
1595 libreswan_log("assign_holdpass() eroute_connection() failed")loglog(RC_LOG, "assign_holdpass() eroute_connection() failed"
)
;
1596 return FALSE0;
1597 }
1598 }
1599
1600 if (!delete_bare_shunt(src, dst,
1601 transport_proto,
1602 (c->policy & POLICY_NEGO_PASS((lset_t)1 << (POLICY_NEGO_PASS_IX))) ? SPI_PASS256 : SPI_HOLD259,
1603 (c->policy & POLICY_NEGO_PASS((lset_t)1 << (POLICY_NEGO_PASS_IX))) ? "delete narrow %pass" :
1604 "delete narrow %hold")) {
1605 dbg("assign_holdpass() delete_bare_shunt() succeeded"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("assign_holdpass() delete_bare_shunt() succeeded"
); } }
;
1606 } else {
1607 libreswan_log("assign_holdpass() delete_bare_shunt() failed")loglog(RC_LOG, "assign_holdpass() delete_bare_shunt() failed"
)
;
1608 return FALSE0;
1609 }
1610 }
1611 sr->routing = rn;
1612 dbg(" assign_holdpass() done - returning success"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log(" assign_holdpass() done - returning success")
; } }
;
1613 return TRUE1;
1614}
1615
1616/* compute a (host-order!) SPI to implement the policy in connection c */
1617ipsec_spi_t shunt_policy_spi(const struct connection *c, bool_Bool prospective)
1618{
1619 /* note: these are in host order :-( */
1620 static const ipsec_spi_t shunt_spi[] =
1621 {
1622 SPI_TRAP260, /* --initiateontraffic */
1623 SPI_PASS256, /* --pass */
1624 SPI_DROP257, /* --drop */
1625 SPI_REJECT258, /* --reject */
1626 };
1627
1628 static const ipsec_spi_t fail_spi[] =
1629 {
1630 0, /* --none*/
1631 SPI_PASS256, /* --failpass */
1632 SPI_DROP257, /* --faildrop */
1633 SPI_REJECT258, /* --failreject */
1634 };
1635
1636 return prospective ?
1637 shunt_spi[(c->policy & POLICY_SHUNT_MASK(((lset_t)1 << (POLICY_SHUNT1_IX)) - ((lset_t)1 <<
(POLICY_SHUNT0_IX)) + ((lset_t)1 << (POLICY_SHUNT1_IX)
))
) >>
1638 POLICY_SHUNT_SHIFTPOLICY_SHUNT0_IX] :
1639 fail_spi[(c->policy & POLICY_FAIL_MASK(((lset_t)1 << (POLICY_FAIL1_IX)) - ((lset_t)1 <<
(POLICY_FAIL0_IX)) + ((lset_t)1 << (POLICY_FAIL1_IX)))
) >> POLICY_FAIL_SHIFTPOLICY_FAIL0_IX];
1640}
1641
1642bool_Bool del_spi(ipsec_spi_t spi, const struct ip_protocol *proto,
1643 const ip_address *src, const ip_address *dest)
1644{
1645 char text_said[SATOT_BUFsizeof(said_buf)];
1646
1647 set_text_said(text_said, dest, spi, proto);
1648
1649 dbg("delete %s", text_said){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("delete %s", text_said); } }
;
1650
1651 struct kernel_sa sa = {
1652 .spi = spi,
1653 .proto = proto,
1654 .src.address = src,
1655 .dst.address = dest,
1656 .text_said = text_said,
1657 };
1658
1659 passert(kernel_ops->del_sa != NULL){ _Bool assertion__ = kernel_ops->del_sa != ((void*)0); if
(!assertion__) { lsw_passert_fail((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 1659}, "%s", "kernel_ops->del_sa != NULL"
); } }
;
1660 return kernel_ops->del_sa(&sa);
1661}
1662
1663static void setup_esp_nic_offload(struct kernel_sa *sa, struct connection *c,
1664 bool_Bool *nic_offload_fallback)
1665{
1666 if (c->nic_offload == yna_no ||
1667 c->interface == NULL((void*)0) || c->interface->ip_dev == NULL((void*)0) ||
1668 c->interface->ip_dev->id_rname == NULL((void*)0)) {
1669 dbg("NIC esp-hw-offload disabled for connection '%s'", c->name){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("NIC esp-hw-offload disabled for connection '%s'"
, c->name); } }
;
1670 return;
1671 }
1672
1673 if (c->nic_offload == yna_auto) {
1674 if (!c->interface->ip_dev->id_nic_offload) {
1675 dbg("NIC esp-hw-offload not for connection '%s' not available on interface %s",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("NIC esp-hw-offload not for connection '%s' not available on interface %s"
, c->name, c->interface->ip_dev->id_rname); } }
1676 c->name, c->interface->ip_dev->id_rname){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("NIC esp-hw-offload not for connection '%s' not available on interface %s"
, c->name, c->interface->ip_dev->id_rname); } }
;
1677 return;
1678 }
1679 *nic_offload_fallback = TRUE1;
1680 dbg("NIC esp-hw-offload offload for connection '%s' enabled on interface %s",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("NIC esp-hw-offload offload for connection '%s' enabled on interface %s"
, c->name, c->interface->ip_dev->id_rname); } }
1681 c->name, c->interface->ip_dev->id_rname){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("NIC esp-hw-offload offload for connection '%s' enabled on interface %s"
, c->name, c->interface->ip_dev->id_rname); } }
;
1682 }
1683 sa->nic_offload_dev = c->interface->ip_dev->id_rname;
1684}
1685
1686/*
1687 * Set up one direction of the SA bundle
1688 */
1689static bool_Bool setup_half_ipsec_sa(struct state *st, bool_Bool inbound)
1690{
1691 /* Build an inbound or outbound SA */
1692
1693 struct connection *c = st->st_connection;
1694 ipsec_spi_t inner_spi = 0;
1695 const struct ip_protocol *proto = NULL((void*)0);
1696 enum eroute_type esatype = ET_UNSPEC;
1697 bool_Bool replace = inbound && (kernel_ops->get_spi != NULL((void*)0));
1698 bool_Bool outgoing_ref_set = FALSE0;
1699 bool_Bool incoming_ref_set = FALSE0;
1700 IPsecSAref_t ref_peer = st->st_ref_peer;
1701 IPsecSAref_t new_ref_peer = IPSEC_SAREF_NULL((IPsecSAref_t)0u);
1702 bool_Bool nic_offload_fallback = FALSE0;
1703
1704 /* SPIs, saved for spigrouping or undoing, if necessary */
1705 struct kernel_sa said[EM_MAXRELSPIS4];
1706 struct kernel_sa *said_next = said;
1707
1708 char text_ipcomp[SATOT_BUFsizeof(said_buf)];
1709 char text_esp[SATOT_BUFsizeof(said_buf)];
1710 char text_ah[SATOT_BUFsizeof(said_buf)];
1711
1712 ip_address src, dst;
1713 ip_selector src_client, dst_client;
1714 if (inbound) {
1715 src = c->spd.that.host_addr;
1716 src_client = c->spd.that.client;
1717 dst = c->spd.this.host_addr;
1718 dst_client = c->spd.this.client;
1719 } else {
1720 src = c->spd.this.host_addr,
1721 src_client = c->spd.this.client;
1722 dst = c->spd.that.host_addr;
1723 dst_client = c->spd.that.client;
1724 }
1725 /* XXX: code is stuffing an endpoint in .host_addr */
1726 src = strip_endpoint(&src, HERE(where_t) { .func = __func__, .basename = "kernel.c" , .line =
1726}
);
1727 dst = strip_endpoint(&dst, HERE(where_t) { .func = __func__, .basename = "kernel.c" , .line =
1727}
);
1728
1729 /*
1730 * mode: encapsulation mode called for
1731 * encap_oneshot: copy of "encapsulation" but reset to
1732 * ENCAPSULATION_MODE_TRANSPORT after use.
1733 */
1734 int mode = ENCAPSULATION_MODE_TRANSPORT2;
1735 bool_Bool add_selector;
1736
1737 if (st->st_ah.attrs.mode == ENCAPSULATION_MODE_TUNNEL1 ||
1738 st->st_esp.attrs.mode == ENCAPSULATION_MODE_TUNNEL1 ||
1739 st->st_ipcomp.attrs.mode == ENCAPSULATION_MODE_TUNNEL1) {
1740 mode = ENCAPSULATION_MODE_TUNNEL1;
1741 add_selector = FALSE0; /* Don't add selectors for tunnel mode */
1742 } else {
1743 /*
1744 * RFC 4301, Section 5.2 Requires traffic selectors to be set
1745 * on transport mode
1746 */
1747 add_selector = TRUE1;
1748 }
1749 c->ipsec_mode = mode;
1750
1751 int encap_oneshot = mode;
1752
1753 struct kernel_sa said_boilerplate = {
1754 .src.address = &src,
1755 .dst.address = &dst,
1756 .src.client = &src_client,
1757 .dst.client = &dst_client,
1758 .inbound = inbound,
1759 .add_selector = add_selector,
1760 .transport_proto = c->spd.this.protocol,
1761 .sa_lifetime = c->sa_ipsec_life_seconds,
1762 .outif = -1,
1763 .sec_ctx = st->sec_ctx,
1764 };
1765
1766 inner_spi = SPI_PASS256;
1767 if (mode == ENCAPSULATION_MODE_TUNNEL1) {
1768 /* If we are tunnelling, set up IP in IP pseudo SA */
1769 proto = &ip_protocol_ipip;
1770 esatype = ET_IPIP;
1771 } else {
1772 /* For transport mode set ESP */
1773 /* ??? why are we sure that this isn't AH? */
1774 proto = &ip_protocol_esp;
1775 esatype = ET_ESP;
1776 }
1777
1778 /* set up IPCOMP SA, if any */
1779
1780 if (st->st_ipcomp.present) {
1781 ipsec_spi_t ipcomp_spi =
1782 inbound ? st->st_ipcomp.our_spi : st->st_ipcomp.attrs.spi;
1783 unsigned compalg;
1784
1785 switch (st->st_ipcomp.attrs.transattrs.ta_comp) {
1786 case IPCOMP_DEFLATE:
1787 compalg = SADB_X_CALG_DEFLATE2;
1788 break;
1789
1790 default:
1791 loglog(RC_LOG_SERIOUS,
1792 "IPCOMP transform %s not implemented",
1793 st->st_ipcomp.attrs.transattrs.ta_encrypt->common.fqn);
1794 goto fail;
1795 }
1796
1797 set_text_said(text_ipcomp, &dst, ipcomp_spi, &ip_protocol_comp);
1798
1799 *said_next = said_boilerplate;
1800 said_next->spi = ipcomp_spi;
1801 said_next->esatype = ET_IPCOMP;
1802 said_next->compalg = compalg;
1803 said_next->mode = encap_oneshot;
1804 said_next->reqid = reqid_ipcomp(c->spd.reqid);
1805 said_next->text_said = text_ipcomp;
1806
1807 if (inbound) {
1808 /*
1809 * set corresponding outbound SA. We can do this on
1810 * each SA in the bundle without harm.
1811 */
1812 said_next->ref_peer = ref_peer;
1813 } else if (!outgoing_ref_set) {
1814 /* on outbound, pick up the SAref if not already done */
1815 said_next->ref = ref_peer;
1816 outgoing_ref_set = TRUE1;
1817 }
1818
1819 if (!kernel_ops->add_sa(said_next, replace)) {
1820 libreswan_log("add_sa ipcomp failed")loglog(RC_LOG, "add_sa ipcomp failed");
1821 goto fail;
1822 }
1823
1824 /*
1825 * SA refs will have been allocated for this SA.
1826 * The inner most one is interesting for the outgoing SA,
1827 * since we refer to it in the policy that we instantiate.
1828 */
1829 if (new_ref_peer == IPSEC_SAREF_NULL((IPsecSAref_t)0u) && !inbound) {
1830 new_ref_peer = said_next->ref;
1831 if (kernel_ops->type != USE_XFRM && new_ref_peer == IPSEC_SAREF_NULL((IPsecSAref_t)0u))
1832 new_ref_peer = IPSEC_SAREF_NA((IPsecSAref_t)0xffff0001);
1833 }
1834 if (!incoming_ref_set && inbound) {
1835 st->st_ref = said_next->ref;
1836 incoming_ref_set = TRUE1;
1837 }
1838 said_next++;
1839
1840 encap_oneshot = ENCAPSULATION_MODE_TRANSPORT2;
1841 }
1842
1843 /* set up ESP SA, if any */
1844
1845 if (st->st_esp.present) {
1846 ipsec_spi_t esp_spi =
1847 inbound ? st->st_esp.our_spi : st->st_esp.attrs.spi;
1848 u_char *esp_dst_keymat =
1849 inbound ? st->st_esp.our_keymat : st->st_esp.
1850 peer_keymat;
1851 const struct trans_attrs *ta = &st->st_esp.attrs.transattrs;
1852
1853 const struct ip_encap *encap_type = NULL((void*)0);
1854 uint16_t encap_sport = 0, encap_dport = 0;
1855 ip_address natt_oa;
1856
1857 if (st->hidden_variables.st_nat_traversal & NAT_T_DETECTED( ((lset_t)1 << (NATED_HOST)) | ((lset_t)1 << (NATED_PEER
)) )
||
1858 st->st_interface->protocol == &ip_protocol_tcp) {
1859 encap_type = st->st_interface->protocol->encap_esp;
1860 if (inbound) {
1861 encap_sport = endpoint_hport(&st->st_remote_endpoint);
1862 encap_dport = endpoint_hport(&st->st_interface->local_endpoint);
1863 } else {
1864 encap_sport = endpoint_hport(&st->st_interface->local_endpoint);
1865 encap_dport = endpoint_hport(&st->st_remote_endpoint);
1866 }
1867 natt_oa = st->hidden_variables.st_nat_oa;
1868 dbg("natt/tcp sa encap_type="PRI_IP_ENCAP" sport=%d dport=%d",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("natt/tcp sa encap_type=""%u(%s)"" sport=%d dport=%d"
, (encap_type) == ((void*)0) ? 0 : (encap_type)->encap_type
, (encap_type) == ((void*)0) ? "none" : (encap_type)->name
, encap_sport, encap_dport); } }
1869 pri_ip_encap(encap_type), encap_sport, encap_dport){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("natt/tcp sa encap_type=""%u(%s)"" sport=%d dport=%d"
, (encap_type) == ((void*)0) ? 0 : (encap_type)->encap_type
, (encap_type) == ((void*)0) ? "none" : (encap_type)->name
, encap_sport, encap_dport); } }
;
1870 }
1871
1872 dbg("looking for alg with encrypt: %s keylen: %d integ: %s",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("looking for alg with encrypt: %s keylen: %d integ: %s"
, ta->ta_encrypt->common.fqn, ta->enckeylen, ta->
ta_integ->common.fqn); } }
1873 ta->ta_encrypt->common.fqn, ta->enckeylen, ta->ta_integ->common.fqn){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("looking for alg with encrypt: %s keylen: %d integ: %s"
, ta->ta_encrypt->common.fqn, ta->enckeylen, ta->
ta_integ->common.fqn); } }
;
1874
1875 /*
1876 * Check that both integrity and encryption are
1877 * supported by the kernel.
1878 *
1879 * Since the parser uses these exact same checks when
1880 * loading the connection, they should never fail (if
1881 * they do then strange things have been going on
1882 * since the connection was loaded).
1883 */
1884 if (!kernel_alg_integ_ok(ta->ta_integ)) {
1885 loglog(RC_LOG_SERIOUS,
1886 "ESP integrity algorithm %s is not implemented or allowed",
1887 ta->ta_integ->common.fqn);
1888 goto fail;
1889 }
1890 if (!kernel_alg_encrypt_ok(ta->ta_encrypt)) {
1891 loglog(RC_LOG_SERIOUS,
1892 "ESP encryption algorithm %s is not implemented or allowed",
1893 ta->ta_encrypt->common.fqn);
1894 goto fail;
1895 }
1896
1897 /*
1898 * Validate the encryption key size.
1899 */
1900 size_t encrypt_keymat_size;
1901 if (!kernel_alg_encrypt_key_size(ta->ta_encrypt, ta->enckeylen,
1902 &encrypt_keymat_size)) {
1903 loglog(RC_LOG_SERIOUS,
1904 "ESP encryption algorithm %s with key length %d not implemented or allowed",
1905 ta->ta_encrypt->common.fqn, ta->enckeylen);
1906 goto fail;
1907 }
1908
1909 /* Fixup key lengths for special cases */
1910#ifdef USE_3DES1
1911 if (ta->ta_encrypt == &ike_alg_encrypt_3des_cbc) {
1912 /* Grrrrr.... f*cking 7 bits jurassic algos */
1913 /* 168 bits in kernel, need 192 bits for keymat_len */
1914 if (encrypt_keymat_size == 21) {
1915 dbg("%s requires a 7-bit jurassic adjust",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("%s requires a 7-bit jurassic adjust", ta->
ta_encrypt->common.fqn); } }
1916 ta->ta_encrypt->common.fqn){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("%s requires a 7-bit jurassic adjust", ta->
ta_encrypt->common.fqn); } }
;
1917 encrypt_keymat_size = 24;
1918 }
1919 }
1920#endif
1921
1922 if (ta->ta_encrypt->salt_size > 0) {
1923 dbg("%s requires %zu salt bytes",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("%s requires %zu salt bytes", ta->ta_encrypt
->common.fqn, ta->ta_encrypt->salt_size); } }
1924 ta->ta_encrypt->common.fqn, ta->ta_encrypt->salt_size){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("%s requires %zu salt bytes", ta->ta_encrypt
->common.fqn, ta->ta_encrypt->salt_size); } }
;
1925 encrypt_keymat_size += ta->ta_encrypt->salt_size;
1926 }
1927
1928 size_t integ_keymat_size = ta->ta_integ->integ_keymat_size; /* BYTES */
1929
1930 dbg("st->st_esp.keymat_len=%" PRIu16 " is encrypt_keymat_size=%zu + integ_keymat_size=%zu",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("st->st_esp.keymat_len=%" "u" " is encrypt_keymat_size=%zu + integ_keymat_size=%zu"
, st->st_esp.keymat_len, encrypt_keymat_size, integ_keymat_size
); } }
1931 st->st_esp.keymat_len, encrypt_keymat_size, integ_keymat_size){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("st->st_esp.keymat_len=%" "u" " is encrypt_keymat_size=%zu + integ_keymat_size=%zu"
, st->st_esp.keymat_len, encrypt_keymat_size, integ_keymat_size
); } }
;
1932
1933 passert(st->st_esp.keymat_len == encrypt_keymat_size + integ_keymat_size){ _Bool assertion__ = st->st_esp.keymat_len == encrypt_keymat_size
+ integ_keymat_size; if (!assertion__) { lsw_passert_fail((where_t
) { .func = __func__, .basename = "kernel.c" , .line = 1933},
"%s", "st->st_esp.keymat_len == encrypt_keymat_size + integ_keymat_size"
); } }
;
1934
1935 set_text_said(text_esp, &dst, esp_spi, &ip_protocol_esp);
1936
1937 *said_next = said_boilerplate;
1938 said_next->spi = esp_spi;
1939 said_next->esatype = ET_ESP;
1940 said_next->replay_window = c->sa_replay_window;
1941 dbg("setting IPsec SA replay-window to %d", c->sa_replay_window){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("setting IPsec SA replay-window to %d", c->
sa_replay_window); } }
;
1942
1943 if (c->xfrmi != NULL((void*)0))
1944 said_next->xfrm_if_id = c->xfrmi->if_id;
1945
1946 if (!inbound && c->sa_tfcpad != 0 && !st->st_seen_no_tfc) {
1947 dbg("Enabling TFC at %d bytes (up to PMTU)", c->sa_tfcpad){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("Enabling TFC at %d bytes (up to PMTU)", c->
sa_tfcpad); } }
;
1948 said_next->tfcpad = c->sa_tfcpad;
1949 }
1950
1951 if (c->policy & POLICY_DECAP_DSCP((lset_t)1 << (POLICY_DECAP_DSCP_IX))) {
1952 dbg("Enabling Decap ToS/DSCP bits"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("Enabling Decap ToS/DSCP bits"); } }
;
1953 said_next->decap_dscp = TRUE1;
1954 }
1955 if (c->policy & POLICY_NOPMTUDISC((lset_t)1 << (POLICY_NOPMTUDISC_IX))) {
1956 dbg("Disabling Path MTU Discovery"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("Disabling Path MTU Discovery"); } }
;
1957 said_next->nopmtudisc = TRUE1;
1958 }
1959
1960 said_next->integ = ta->ta_integ;
1961#ifdef USE_SHA21
1962 if (said_next->integ == &ike_alg_integ_sha2_256 &&
1963 LIN(POLICY_SHA2_TRUNCBUG, c->policy)(((((lset_t)1 << (POLICY_SHA2_TRUNCBUG_IX))) & (c->
policy)) == (((lset_t)1 << (POLICY_SHA2_TRUNCBUG_IX))))
) {
1964 if (kernel_ops->sha2_truncbug_support) {
1965 if (libreswan_fipsmode() == 1) {
1966 loglog(RC_LOG_SERIOUS,
1967 "Error: sha2-truncbug=yes is not allowed in FIPS mode");
1968 goto fail;
1969 }
1970 dbg(" authalg converted for sha2 truncation at 96bits instead of IETF's mandated 128bits"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log(" authalg converted for sha2 truncation at 96bits instead of IETF's mandated 128bits"
); } }
;
1971 /*
1972 * We need to tell the kernel to mangle
1973 * the sha2_256, as instructed by the user
1974 */
1975 said_next->integ = &ike_alg_integ_hmac_sha2_256_truncbug;
1976 } else {
1977 loglog(RC_LOG_SERIOUS,
1978 "Error: %s stack does not support sha2_truncbug=yes",
1979 kernel_ops->kern_name);
1980 goto fail;
1981 }
1982 }
1983#endif
1984 said_next->authalg = said_next->integ->integ_ikev1_ah_transform;
1985
1986 if (st->st_esp.attrs.transattrs.esn_enabled) {
1987 dbg("Enabling ESN"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("Enabling ESN"); } }
;
1988 said_next->esn = TRUE1;
1989 }
1990
1991 /*
1992 * XXX: Assume SADB_ and ESP_ numbers match! Clearly
1993 * setting .compalg is wrong, don't yet trust
1994 * lower-level code to be right.
1995 */
1996 said_next->encrypt = ta->ta_encrypt;
1997 said_next->compalg = said_next->encrypt->common.id[IKEv1_ESP_ID];
1998
1999 /* divide up keying material */
2000 said_next->enckey = esp_dst_keymat;
2001 said_next->enckeylen = encrypt_keymat_size; /* BYTES */
2002 said_next->authkey = esp_dst_keymat + encrypt_keymat_size;
2003 said_next->authkeylen = integ_keymat_size; /* BYTES */
2004
2005 said_next->mode = encap_oneshot;
2006 said_next->reqid = reqid_esp(c->spd.reqid);
2007
2008 said_next->src.encap_port = encap_sport;
2009 said_next->dst.encap_port = encap_dport;
2010 said_next->encap_type = encap_type;
2011 said_next->natt_oa = &natt_oa;
2012 said_next->text_said = text_esp;
2013
2014 DBG(DBG_PRIVATE, {{ if ((cur_debugging & (((lset_t)1 << (DBG_PRIVATE_IX
))))) { { DBG_dump("ESP enckey:", said_next->enckey, said_next
->enckeylen); DBG_dump("ESP authkey:", said_next->authkey
, said_next->authkeylen); }; } }
2015 DBG_dump("ESP enckey:", said_next->enckey,{ if ((cur_debugging & (((lset_t)1 << (DBG_PRIVATE_IX
))))) { { DBG_dump("ESP enckey:", said_next->enckey, said_next
->enckeylen); DBG_dump("ESP authkey:", said_next->authkey
, said_next->authkeylen); }; } }
2016 said_next->enckeylen);{ if ((cur_debugging & (((lset_t)1 << (DBG_PRIVATE_IX
))))) { { DBG_dump("ESP enckey:", said_next->enckey, said_next
->enckeylen); DBG_dump("ESP authkey:", said_next->authkey
, said_next->authkeylen); }; } }
2017 DBG_dump("ESP authkey:", said_next->authkey,{ if ((cur_debugging & (((lset_t)1 << (DBG_PRIVATE_IX
))))) { { DBG_dump("ESP enckey:", said_next->enckey, said_next
->enckeylen); DBG_dump("ESP authkey:", said_next->authkey
, said_next->authkeylen); }; } }
2018 said_next->authkeylen);{ if ((cur_debugging & (((lset_t)1 << (DBG_PRIVATE_IX
))))) { { DBG_dump("ESP enckey:", said_next->enckey, said_next
->enckeylen); DBG_dump("ESP authkey:", said_next->authkey
, said_next->authkeylen); }; } }
2019 }){ if ((cur_debugging & (((lset_t)1 << (DBG_PRIVATE_IX
))))) { { DBG_dump("ESP enckey:", said_next->enckey, said_next
->enckeylen); DBG_dump("ESP authkey:", said_next->authkey
, said_next->authkeylen); }; } }
;
2020
2021 if (inbound) {
2022 /*
2023 * set corresponding outbound SA. We can do this on
2024 * each SA in the bundle without harm.
2025 */
2026 said_next->ref_peer = ref_peer;
2027 } else if (!outgoing_ref_set) {
2028 /* on outbound, pick up the SAref if not already done */
2029 said_next->ref = ref_peer;
2030 outgoing_ref_set = TRUE1;
2031 }
2032 setup_esp_nic_offload(said_next, c, &nic_offload_fallback);
2033
2034 bool_Bool ret = kernel_ops->add_sa(said_next, replace);
2035
2036 if (!ret && nic_offload_fallback &&
2037 said_next->nic_offload_dev != NULL((void*)0)) {
2038 /* Fallback to non-nic-offload crypto */
2039 said_next->nic_offload_dev = NULL((void*)0);
2040 ret = kernel_ops->add_sa(said_next, replace);
2041 }
2042
2043 /* scrub keys from memory */
2044 memset(said_next->enckey, 0, said_next->enckeylen);
2045 memset(said_next->authkey, 0, said_next->authkeylen);
2046
2047 if (!ret)
2048 goto fail;
2049
2050 /*
2051 * SA refs will have been allocated for this SA.
2052 * The inner most one is interesting for the outgoing SA,
2053 * since we refer to it in the policy that we instantiate.
2054 */
2055 if (new_ref_peer == IPSEC_SAREF_NULL((IPsecSAref_t)0u) && !inbound) {
2056 new_ref_peer = said_next->ref;
2057 if (kernel_ops->type != USE_XFRM && new_ref_peer == IPSEC_SAREF_NULL((IPsecSAref_t)0u))
2058 new_ref_peer = IPSEC_SAREF_NA((IPsecSAref_t)0xffff0001);
2059 }
2060 if (!incoming_ref_set && inbound) {
2061 st->st_ref = said_next->ref;
2062 incoming_ref_set = TRUE1;
2063 }
2064 said_next++;
2065
2066 encap_oneshot = ENCAPSULATION_MODE_TRANSPORT2;
2067 }
2068
2069 /* set up AH SA, if any */
2070
2071 if (st->st_ah.present) {
2072 ipsec_spi_t ah_spi =
2073 inbound ? st->st_ah.our_spi : st->st_ah.attrs.spi;
2074 u_char *ah_dst_keymat =
2075 inbound ? st->st_ah.our_keymat : st->st_ah.peer_keymat;
2076
2077 const struct integ_desc *integ = st->st_ah.attrs.transattrs.ta_integ;
2078 size_t keymat_size = integ->integ_keymat_size;
2079 int authalg = integ->integ_ikev1_ah_transform;
2080 if (authalg <= 0) {
2081 loglog(RC_LOG_SERIOUS, "%s not implemented",
2082 integ->common.fqn);
2083 goto fail;
2084 }
2085
2086 passert(st->st_ah.keymat_len == keymat_size){ _Bool assertion__ = st->st_ah.keymat_len == keymat_size;
if (!assertion__) { lsw_passert_fail((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 2086}, "%s", "st->st_ah.keymat_len == keymat_size"
); } }
;
2087
2088 set_text_said(text_ah, &dst, ah_spi, &ip_protocol_ah);
2089
2090 *said_next = said_boilerplate;
2091 said_next->spi = ah_spi;
2092 said_next->esatype = ET_AH;
2093 said_next->integ = integ;
2094 said_next->authalg = authalg;
2095 said_next->authkeylen = st->st_ah.keymat_len;
2096 said_next->authkey = ah_dst_keymat;
2097 said_next->mode = encap_oneshot;
2098 said_next->reqid = reqid_ah(c->spd.reqid);
2099 said_next->text_said = text_ah;
2100 said_next->replay_window = c->sa_replay_window;
2101 dbg("setting IPsec SA replay-window to %d", c->sa_replay_window){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("setting IPsec SA replay-window to %d", c->
sa_replay_window); } }
;
2102
2103 if (st->st_ah.attrs.transattrs.esn_enabled) {
2104 dbg("Enabling ESN"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("Enabling ESN"); } }
;
2105 said_next->esn = TRUE1;
2106 }
2107
2108 DBG(DBG_PRIVATE, {{ if ((cur_debugging & (((lset_t)1 << (DBG_PRIVATE_IX
))))) { { DBG_dump("AH authkey:", said_next->authkey, said_next
->authkeylen); }; } }
2109 DBG_dump("AH authkey:", said_next->authkey,{ if ((cur_debugging & (((lset_t)1 << (DBG_PRIVATE_IX
))))) { { DBG_dump("AH authkey:", said_next->authkey, said_next
->authkeylen); }; } }
2110 said_next->authkeylen);{ if ((cur_debugging & (((lset_t)1 << (DBG_PRIVATE_IX
))))) { { DBG_dump("AH authkey:", said_next->authkey, said_next
->authkeylen); }; } }
2111 }){ if ((cur_debugging & (((lset_t)1 << (DBG_PRIVATE_IX
))))) { { DBG_dump("AH authkey:", said_next->authkey, said_next
->authkeylen); }; } }
;
2112
2113 if (inbound) {
2114 /*
2115 * set corresponding outbound SA. We can do this on
2116 * each SA in the bundle without harm.
2117 */
2118 said_next->ref_peer = ref_peer;
2119 } else if (!outgoing_ref_set) {
2120 /* on outbound, pick up the SAref if not already done */
2121 said_next->ref = ref_peer;
2122 outgoing_ref_set = TRUE1; /* outgoing_ref_set not subsequently used */
2123 }
2124
2125 if (!kernel_ops->add_sa(said_next, replace)) {
2126 /* scrub key from memory */
2127 memset(said_next->authkey, 0, said_next->authkeylen);
2128 goto fail;
2129 }
2130 /* scrub key from memory */
2131 memset(said_next->authkey, 0, said_next->authkeylen);
2132
2133 /*
2134 * SA refs will have been allocated for this SA.
2135 * The inner most one is interesting for the outgoing SA,
2136 * since we refer to it in the policy that we instantiate.
2137 */
2138 if (new_ref_peer == IPSEC_SAREF_NULL((IPsecSAref_t)0u) && !inbound) {
2139 new_ref_peer = said_next->ref;
2140 if (kernel_ops->type != USE_XFRM && new_ref_peer == IPSEC_SAREF_NULL((IPsecSAref_t)0u))
2141 new_ref_peer = IPSEC_SAREF_NA((IPsecSAref_t)0xffff0001);
2142 }
2143 if (!incoming_ref_set && inbound) {
2144 st->st_ref = said_next->ref;
2145 incoming_ref_set = TRUE1; /* incoming_ref_set not subsequently used */
2146 }
2147 said_next++;
2148
2149 encap_oneshot = ENCAPSULATION_MODE_TRANSPORT2; /* encap_oneshot not subsequently used */
Value stored to 'encap_oneshot' is never read
2150 }
2151
2152 /*
2153 * Add an inbound eroute to enforce an arrival check.
2154 *
2155 * If inbound,
2156 * ??? and some more mysterious conditions,
2157 * Note reversed ends.
2158 * Not much to be done on failure.
2159 */
2160 dbg("%s() is installing inbound eroute? inbound=%d owner=#%lu mode=%d",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("%s() is installing inbound eroute? inbound=%d owner=#%lu mode=%d"
, __func__, inbound, c->spd.eroute_owner, mode); } }
2161 __func__, inbound, c->spd.eroute_owner, mode){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("%s() is installing inbound eroute? inbound=%d owner=#%lu mode=%d"
, __func__, inbound, c->spd.eroute_owner, mode); } }
;
2162 if (inbound && c->spd.eroute_owner == SOS_NOBODY0) {
2163 dbg("%s() is installing inbound eroute", __func__){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("%s() is installing inbound eroute", __func__)
; } }
;
2164 struct pfkey_proto_info proto_info[4];
2165 int i = 0;
2166
2167 /*
2168 * ??? why does this code care about
2169 * st->st_*.attrs.mode?
2170 * We have gone do some trouble to compute
2171 * "mode". And later code uses
2172 * "mode".
2173 */
2174 if (st->st_ipcomp.present) {
2175 proto_info[i].proto = ip_protocol_comp.ipproto;
2176 proto_info[i].mode =
2177 st->st_ipcomp.attrs.mode;
2178 proto_info[i].reqid = reqid_ipcomp(c->spd.reqid);
2179 i++;
2180 }
2181
2182 if (st->st_esp.present) {
2183 proto_info[i].proto = IPPROTO_ESPIPPROTO_ESP;
2184 proto_info[i].mode =
2185 st->st_esp.attrs.mode;
2186 proto_info[i].reqid = reqid_esp(c->spd.reqid);
2187 i++;
2188 }
2189
2190 if (st->st_ah.present) {
2191 proto_info[i].proto = IPPROTO_AHIPPROTO_AH;
2192 proto_info[i].mode =
2193 st->st_ah.attrs.mode;
2194 proto_info[i].reqid = reqid_ah(c->spd.reqid);
2195 i++;
2196 }
2197
2198 dbg("%s() before proto %d", __func__, proto_info[0].proto){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("%s() before proto %d", __func__, proto_info[0
].proto); } }
;
2199 proto_info[i].proto = 0;
2200
2201 /*
2202 * ??? why is mode overwritten ONLY if true
2203 * (kernel_ops->inbound_eroute)?
2204 */
2205 if (mode == ENCAPSULATION_MODE_TUNNEL1) {
2206 proto_info[0].mode =
2207 ENCAPSULATION_MODE_TUNNEL1;
2208 for (i = 1; proto_info[i].proto; i++)
2209 proto_info[i].mode =
2210 ENCAPSULATION_MODE_TRANSPORT2;
2211 }
2212 dbg("%s() after proto %d", __func__, proto_info[0].proto){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("%s() after proto %d", __func__, proto_info[0]
.proto); } }
;
2213
2214 uint32_t xfrm_if_id = c->xfrmi != NULL((void*)0) ?
2215 c->xfrmi->if_id : 0;
2216
2217 dbg("%s() calling raw_eroute backwards (i.e., inbound)", __func__){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("%s() calling raw_eroute backwards (i.e., inbound)"
, __func__); } }
;
2218 /* MCR - should be passed a spd_eroute structure here */
2219 /* note: this and that are intentionally reversed */
2220 if (!raw_eroute(&c->spd.that.host_addr, /* this_host */
2221 &c->spd.that.client, /* this_client */
2222 &c->spd.this.host_addr, /* that_host */
2223 &c->spd.this.client, /* that_client */
2224 inner_spi, /* current spi - might not be used? */
2225 inner_spi, /* new spi */
2226 proto, /* SA proto */
2227 c->spd.this.protocol, /* transport_proto */
2228 esatype, /* esatype */
2229 proto_info, /* " */
2230 deltatime(0), /* lifetime */
2231 calculate_sa_prio(c, FALSE0), /* priority */
2232 &c->sa_marks, /* IPsec SA marks */
2233 xfrm_if_id,
2234 ERO_ADD_INBOUND, /* op */
2235 "add inbound", /* opname */
2236 st->st_connection->policy_label))
2237 {
2238 libreswan_log("raw_eroute() in setup_half_ipsec_sa() failed to add inbound")loglog(RC_LOG, "raw_eroute() in setup_half_ipsec_sa() failed to add inbound"
)
;
2239 }
2240 }
2241
2242 /* If there are multiple SPIs, group them. */
2243
2244 if (kernel_ops->grp_sa != NULL((void*)0) && said_next > &said[1]) {
2245 struct kernel_sa *s;
2246
2247 /*
2248 * group SAs, two at a time, inner to outer (backwards in
2249 * said[])
2250 *
2251 * The grouping is by pairs. So if said[] contains
2252 * ah esp ipip,
2253 *
2254 * the grouping would be ipip:esp, esp:ah.
2255 */
2256 for (s = said; s < said_next - 1; s++) {
2257 dbg("grouping %s (ref=%u) and %s (ref=%u)",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("grouping %s (ref=%u) and %s (ref=%u)", s[0].text_said
, s[0].ref, s[1].text_said, s[1].ref); } }
2258 s[0].text_said, s[0].ref,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("grouping %s (ref=%u) and %s (ref=%u)", s[0].text_said
, s[0].ref, s[1].text_said, s[1].ref); } }
2259 s[1].text_said, s[1].ref){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("grouping %s (ref=%u) and %s (ref=%u)", s[0].text_said
, s[0].ref, s[1].text_said, s[1].ref); } }
;
2260 if (!kernel_ops->grp_sa(s + 1, s)) {
2261 libreswan_log("grp_sa failed")loglog(RC_LOG, "grp_sa failed");
2262 goto fail;
2263 }
2264 }
2265 /* could update said, but it will not be used */
2266 }
2267
2268 if (new_ref_peer != IPSEC_SAREF_NULL((IPsecSAref_t)0u))
2269 st->st_ref_peer = new_ref_peer;
2270
2271 /* if the impaired is set, pretend this fails */
2272 if (impair.sa_creation) {
2273 DBG_log("Impair SA creation is set, pretending to fail");
2274 goto fail;
2275 }
2276 return TRUE1;
2277
2278fail:
2279 {
2280 libreswan_log("setup_half_ipsec_sa() hit fail:")loglog(RC_LOG, "setup_half_ipsec_sa() hit fail:");
2281 /* undo the done SPIs */
2282 while (said_next-- != said) {
2283 if (said_next->proto != 0) {
2284 (void) del_spi(said_next->spi,
2285 said_next->proto,
2286 &src, said_next->dst.address);
2287 }
2288 }
2289 return FALSE0;
2290 }
2291}
2292
2293static bool_Bool teardown_half_ipsec_sa(struct state *st, bool_Bool inbound)
2294{
2295 /* Delete any AH, ESP, and IP in IP SPIs. */
2296
2297 struct connection *const c = st->st_connection;
2298
2299 /*
2300 * If we have a new address in c->spd.that.host_addr,
2301 * we are the initiator, have been redirected,
2302 * and yet this routine must use the old address.
2303 *
2304 * We point effective_that_host_address to the appropriate address.
2305 */
2306
2307 const ip_address *effective_that_host_addr = &c->spd.that.host_addr;
2308
2309 if (!sameaddr(&st->st_remote_endpoint, effective_that_host_addr) &&
2310 address_is_specified(&c->temp_vars.redirect_ip)) {
2311 effective_that_host_addr = &st->st_remote_endpoint;
2312 }
2313
2314 /* ??? CLANG 3.5 thinks that c might be NULL */
2315 if (inbound && c->spd.eroute_owner == SOS_NOBODY0 &&
2316 !raw_eroute(effective_that_host_addr,
2317 &c->spd.that.client,
2318 &c->spd.this.host_addr,
2319 &c->spd.this.client,
2320 SPI_PASS256, SPI_PASS256,
2321 c->ipsec_mode == ENCAPSULATION_MODE_TRANSPORT2 ?
2322 &ip_protocol_esp : NULL((void*)0),
2323 c->spd.this.protocol,
2324 c->ipsec_mode == ENCAPSULATION_MODE_TRANSPORT2 ?
2325 ET_ESP : ET_UNSPEC,
2326 null_proto_info,
2327 deltatime(0),
2328 calculate_sa_prio(c, FALSE0),
2329 &c->sa_marks,
2330 0, /* xfrm_if_id. needed to tear down? */
2331 ERO_DEL_INBOUND,
2332 "delete inbound",
2333 c->policy_label))
2334 {
2335 libreswan_log("raw_eroute in teardown_half_ipsec_sa() failed to delete inbound")loglog(RC_LOG, "raw_eroute in teardown_half_ipsec_sa() failed to delete inbound"
)
;
2336 }
2337
2338 /* collect each proto SA that needs deleting */
2339
2340 struct {
2341 const struct ip_protocol *proto;
2342 const struct ipsec_proto_info *info;
2343 } protos[4]; /* at most 3 entries + terminator */
2344 int i = 0;
2345
2346 if (st->st_ah.present) {
2347 protos[i].proto = &ip_protocol_ah;
2348 protos[i].info = &st->st_ah;
2349 i++;
2350 }
2351
2352 if (st->st_esp.present) {
2353 protos[i].proto = &ip_protocol_esp;
2354 protos[i].info = &st->st_esp;
2355 i++;
2356 }
2357
2358 if (st->st_ipcomp.present) {
2359 protos[i].proto = &ip_protocol_comp;
2360 protos[i].info = &st->st_ipcomp;
2361 i++;
2362 }
2363
2364 /*
2365 * If the SAs have been grouped, deleting any one will do:
2366 * we just delete the first one found (protos[0]).
2367 */
2368 if (kernel_ops->grp_sa != NULL((void*)0) && i > 0)
2369 i = 1;
2370
2371 protos[i].proto = NULL((void*)0);
2372
2373 /* delete each proto that needs deleting */
2374 bool_Bool result = TRUE1;
2375
2376 for (i = 0; protos[i].proto != NULL((void*)0); i++) {
2377 const struct ip_protocol *proto = protos[i].proto;
2378 ipsec_spi_t spi;
2379 const ip_address *src, *dst;
2380
2381 if (inbound) {
2382 spi = protos[i].info->our_spi;
2383 src = effective_that_host_addr;
2384 dst = &c->spd.this.host_addr;
2385 } else {
2386 spi = protos[i].info->attrs.spi;
2387 src = &c->spd.this.host_addr;
2388 dst = effective_that_host_addr;
2389 }
2390
2391 result &= del_spi(spi, proto, src, dst);
2392 }
2393
2394 return result;
2395}
2396
2397static event_callback_routine kernel_process_msg_cb;
2398
2399static void kernel_process_msg_cb(evutil_socket_tint fd,
2400 const short event UNUSED__attribute__ ((unused)), void *arg)
2401{
2402 const struct kernel_ops *kernel_ops = arg;
2403
2404 dbg(" %s process netlink message", __func__){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log(" %s process netlink message", __func__); } }
;
2405 threadtime_t start = threadtime_start();
2406 kernel_ops->process_msg(fd);
2407 threadtime_stop(&start, SOS_NOBODY0, "kernel message");
2408 pexpect_reset_globals()log_pexpect_reset_globals((where_t) { .func = __func__, .basename
= "kernel.c" , .line = 2408})
;
2409}
2410
2411static global_timer_cb kernel_process_queue_cb;
2412
2413static void kernel_process_queue_cb(struct fd *unused_whackfd UNUSED__attribute__ ((unused)))
2414{
2415 if (pexpect(kernel_ops->process_queue != NULL)({ _Bool assertion__ = kernel_ops->process_queue != ((void
*)0); if (!assertion__) { log_pexpect((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 2415}, "%s", "kernel_ops->process_queue != NULL"
); } assertion__; })
) {
2416 kernel_ops->process_queue();
2417 }
2418 pexpect_reset_globals()log_pexpect_reset_globals((where_t) { .func = __func__, .basename
= "kernel.c" , .line = 2418})
;
2419}
2420
2421/* keep track of kernel version */
2422static char kversion[256];
2423
2424const struct kernel_ops *kernel_ops =
2425#ifdef XFRM_SUPPORT1
2426 &xfrm_kernel_ops
2427#endif
2428#ifdef BSD_KAME
2429 &bsdkame_kernel_ops
2430#endif
2431 ;
2432
2433deltatime_t bare_shunt_interval = DELTATIME_INIT(SHUNT_SCAN_INTERVAL){ .dt = { .tv_sec = ((2 * 10)), } };
2434
2435static void kernel_scan_shunts(struct fd *unused_whackfd UNUSED__attribute__ ((unused)))
2436{
2437 kernel_ops->scan_shunts();
2438}
2439
2440void init_kernel(void)
2441{
2442 struct utsname un;
2443
2444 /* get kernel version */
2445 uname(&un);
2446 jam_str(kversion, sizeof(kversion), un.release);
2447
2448 switch (kernel_ops->type) {
2449#if defined(XFRM_SUPPORT1)
2450 case USE_XFRM:
2451 {
2452 struct stat buf;
2453 if (stat("/proc/sys/net/core/xfrm_acq_expires", &buf) != 0) {
2454 libreswan_log("No XFRM kernel support detected, missing /proc/sys/net/core/xfrm_acq_expires")loglog(RC_LOG, "No XFRM kernel support detected, missing /proc/sys/net/core/xfrm_acq_expires"
)
;
2455 exit_pluto(PLUTO_EXIT_KERNEL_FAIL);
2456 }
2457 libreswan_log("Using Linux XFRM/NETKEY IPsec kernel support code on %s",loglog(RC_LOG, "Using Linux XFRM/NETKEY IPsec kernel support code on %s"
, kversion)
2458 kversion)loglog(RC_LOG, "Using Linux XFRM/NETKEY IPsec kernel support code on %s"
, kversion)
;
2459 break;
2460 }
2461#endif
2462
2463#if defined(BSD_KAME)
2464 case USE_BSDKAME:
2465 libreswan_log("Using BSD/KAME IPsec interface code on %s",loglog(RC_LOG, "Using BSD/KAME IPsec interface code on %s", kversion
)
2466 kversion)loglog(RC_LOG, "Using BSD/KAME IPsec interface code on %s", kversion
)
;
2467 break;
2468#endif
2469
2470 default:
2471 libreswan_log("FATAL: kernel interface '%s' not available",loglog(RC_LOG, "FATAL: kernel interface '%s' not available", enum_name
(&kern_interface_names, kernel_ops->type))
2472 enum_name(&kern_interface_names,loglog(RC_LOG, "FATAL: kernel interface '%s' not available", enum_name
(&kern_interface_names, kernel_ops->type))
2473 kernel_ops->type))loglog(RC_LOG, "FATAL: kernel interface '%s' not available", enum_name
(&kern_interface_names, kernel_ops->type))
;
2474 exit_pluto(PLUTO_EXIT_KERNEL_FAIL);
2475 }
2476
2477 if (kernel_ops->init != NULL((void*)0))
2478 kernel_ops->init();
2479
2480 /* Add the port bypass polcies */
2481
2482 if (kernel_ops->v6holes != NULL((void*)0)) {
2483 if (!kernel_ops->v6holes()) {
2484 libreswan_log("Could not add the ICMP bypass policies")loglog(RC_LOG, "Could not add the ICMP bypass policies");
2485 exit_pluto(PLUTO_EXIT_KERNEL_FAIL);
2486 }
2487 }
2488
2489 /* register SA types that we can negotiate */
2490 if (kernel_ops->pfkey_register != NULL((void*)0))
2491 kernel_ops->pfkey_register();
2492
2493 enable_periodic_timer(EVENT_SHUNT_SCAN, kernel_scan_shunts,
2494 bare_shunt_interval);
2495
2496 dbg("setup kernel fd callback"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("setup kernel fd callback"); } }
;
2497
2498 if (kernel_ops->async_fdp != NULL((void*)0))
2499 /* Note: kernel_ops is const but pluto_event_add cannot know that */
2500 add_fd_read_event_handler(*kernel_ops->async_fdp, kernel_process_msg_cb,
2501 (void *)kernel_ops, "KERNEL_XRM_FD");
2502
2503 if (kernel_ops->route_fdp != NULL((void*)0) && *kernel_ops->route_fdp > NULL_FD(-1)) {
2504 add_fd_read_event_handler(*kernel_ops->route_fdp, kernel_process_msg_cb,
2505 (void *)kernel_ops, "KERNEL_ROUTE_FD");
2506 }
2507
2508 if (kernel_ops->process_queue != NULL((void*)0)) {
2509 /*
2510 * AA_2015 this is untested code. only for non xfrm ???
2511 * It seems in klips we should, besides kernel_process_msg,
2512 * call process_queue periodically. Does the order
2513 * matter?
2514 */
2515 enable_periodic_timer(EVENT_PROCESS_KERNEL_QUEUE,
2516 kernel_process_queue_cb,
2517 deltatime(KERNEL_PROCESS_Q_PERIOD1));
2518 }
2519}
2520
2521void show_kernel_interface(struct show *s)
2522{
2523 if (kernel_ops != NULL((void*)0)) {
2524 show_comment(s, "using kernel interface: %s",
2525 kernel_ops->kern_name);
2526 }
2527}
2528
2529/*
2530 * see if the attached connection refers to an older state.
2531 * if it does, then initiate this state with the appropriate outgoing
2532 * references, such that we won't break any userland applications
2533 * that are using the conn with REFINFO.
2534 */
2535static void look_for_replacement_state(struct state *st)
2536{
2537 struct connection *c = st->st_connection;
2538 struct state *ost = state_with_serialno(c->newest_ipsec_sa);
2539
2540 if (DBGP(DBG_BASE)(cur_debugging & (((lset_t)1 << (DBG_BASE_IX))))) {
2541 DBG_log("checking if this is a replacement state");
2542 DBG_log(" st=%p ost=%p st->serialno=#%lu ost->serialno=#%lu",
2543 st, ost, st->st_serialno,
2544 ost == NULL((void*)0) ? 0 : ost->st_serialno);
2545 }
2546
2547 if (ost != NULL((void*)0) && ost != st && ost->st_serialno != st->st_serialno) {
2548 /*
2549 * then there is an old state associated, and it is
2550 * different then the new one.
2551 */
2552 dbg("keeping ref_peer=%" PRIu32 " during rekey", ost->st_ref_peer){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("keeping ref_peer=%" "u" " during rekey", ost->
st_ref_peer); } }
;
2553 st->st_ref_peer = ost->st_ref_peer;
2554 }
2555}
2556
2557/*
2558 * Note: install_inbound_ipsec_sa is only used by the Responder.
2559 * The Responder will subsequently use install_ipsec_sa for the outbound.
2560 * The Initiator uses install_ipsec_sa to install both at once.
2561 */
2562bool_Bool install_inbound_ipsec_sa(struct state *st)
2563{
2564 struct connection *const c = st->st_connection;
2565
2566 /*
2567 * If our peer has a fixed-address client, check if we already
2568 * have a route for that client that conflicts. We will take this
2569 * as proof that that route and the connections using it are
2570 * obsolete and should be eliminated. Interestingly, this is
2571 * the only case in which we can tell that a connection is obsolete.
2572 */
2573 passert(c->kind == CK_PERMANENT || c->kind == CK_INSTANCE){ _Bool assertion__ = c->kind == CK_PERMANENT || c->kind
== CK_INSTANCE; if (!assertion__) { lsw_passert_fail((where_t
) { .func = __func__, .basename = "kernel.c" , .line = 2573},
"%s", "c->kind == CK_PERMANENT || c->kind == CK_INSTANCE"
); } }
;
2574 if (c->spd.that.has_client) {
2575 for (;; ) {
2576 struct spd_route *esr; /* value is ignored */
2577 struct connection *o = route_owner(c, &c->spd, &esr,
2578 NULL((void*)0), NULL((void*)0));
2579
2580 if (o == NULL((void*)0) || c == o)
2581 break; /* nobody interesting has a route */
2582
2583 /* note: we ignore the client addresses at this end */
2584 if (sameaddr(&o->spd.that.host_addr,
2585 &c->spd.that.host_addr) &&
2586 o->interface == c->interface)
2587 break; /* existing route is compatible */
2588
2589 if (kernel_ops->overlap_supported) {
2590 /*
2591 * Both are transport mode, allow overlapping.
2592 * [bart] not sure if this is actually
2593 * intended, but am leaving it in to make it
2594 * behave like before
2595 */
2596 if (!LIN(POLICY_TUNNEL, c->policy | o->policy)(((((lset_t)1 << (POLICY_TUNNEL_IX))) & (c->policy
| o->policy)) == (((lset_t)1 << (POLICY_TUNNEL_IX))
))
)
2597 break;
2598
2599 /* Both declared that overlapping is OK. */
2600 if (LIN(POLICY_OVERLAPIP, c->policy & o->policy)(((((lset_t)1 << (POLICY_OVERLAPIP_IX))) & (c->policy
& o->policy)) == (((lset_t)1 << (POLICY_OVERLAPIP_IX
))))
)
2601 break;
2602 }
2603
2604 address_buf b;
2605 connection_buf cib;
2606 log_state(RC_LOG_SERIOUS, st,
2607 "route to peer's client conflicts with "PRI_CONNECTION"\"%s\"%s"" %s; releasing old connection to free the route",
2608 pri_connection(o, &cib)(o)->name, str_connection_instance(o, &cib),
2609 str_address_sensitive(&o->spd.that.host_addr, &b));
2610 /*
2611 * XXX: Assume this call shouldn't log to
2612 * whack(?). While ST has an attached whack,
2613 * the global whack, which this code would
2614 * have been using, detached long-ago.
2615 */
2616 release_connection(o, false0, null_fd((struct fd *) ((void*)0)));
2617 }
2618 }
2619
2620 dbg("install_inbound_ipsec_sa() checking if we can route"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("install_inbound_ipsec_sa() checking if we can route"
); } }
;
2621 /* check that we will be able to route and eroute */
2622 switch (could_route(c, st->st_logger)) {
2623 case route_easy:
2624 case route_nearconflict:
2625 dbg(" routing is easy, or has resolvable near-conflict"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log(" routing is easy, or has resolvable near-conflict"
); } }
;
2626 break;
2627
2628 case route_unnecessary:
2629 /*
2630 * in this situation, we should look and see if there is
2631 * a state that our connection references, that we are
2632 * in fact replacing.
2633 */
2634 break;
2635
2636 default:
2637 return FALSE0;
2638 }
2639
2640 look_for_replacement_state(st);
2641
2642 /*
2643 * we now have to set up the outgoing SA first, so that
2644 * we can refer to it in the incoming SA.
2645 */
2646 if (st->st_ref_peer == IPSEC_SAREF_NULL((IPsecSAref_t)0u) && !st->st_outbound_done) {
2647 dbg("installing outgoing SA now as ref_peer=%u", st->st_ref_peer){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("installing outgoing SA now as ref_peer=%u", st
->st_ref_peer); } }
;
2648 if (!setup_half_ipsec_sa(st, FALSE0)) {
2649 DBG_log("failed to install outgoing SA: %u",
2650 st->st_ref_peer);
2651 return FALSE0;
2652 }
2653
2654 st->st_outbound_done = TRUE1;
2655 }
2656 dbg("outgoing SA has ref_peer=%u", st->st_ref_peer){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("outgoing SA has ref_peer=%u", st->st_ref_peer
); } }
;
2657
2658 /* (attempt to) actually set up the SAs */
2659
2660 return setup_half_ipsec_sa(st, TRUE1);
2661}
2662
2663/* Install a route and then a prospective shunt eroute or an SA group eroute.
2664 * Assumption: could_route gave a go-ahead.
2665 * Any SA Group must have already been created.
2666 * On failure, steps will be unwound.
2667 */
2668bool_Bool route_and_eroute(struct connection *c,
2669 struct spd_route *sr,
2670 struct state *st)
2671{
2672 dbg("route_and_eroute() for proto %d, and source port %d dest port %d",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute() for proto %d, and source port %d dest port %d"
, sr->this.protocol, sr->this.port, sr->that.port); }
}
2673 sr->this.protocol, sr->this.port, sr->that.port){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute() for proto %d, and source port %d dest port %d"
, sr->this.protocol, sr->this.port, sr->that.port); }
}
;
2674 setportof(htons(sr->this.port), &sr->this.client.addr){ *(&sr->this.client.addr) = set_endpoint_hport((&
sr->this.client.addr), ntohs(htons(sr->this.port))); }
;
2675 setportof(htons(sr->that.port), &sr->that.client.addr){ *(&sr->that.client.addr) = set_endpoint_hport((&
sr->that.client.addr), ntohs(htons(sr->that.port))); }
;
2676
2677 struct spd_route *esr, *rosr;
2678 struct connection *ero,
2679 *ro = route_owner(c, sr, &rosr, &ero, &esr); /* who, if anyone, owns our eroute? */
2680
2681 dbg("route_and_eroute with c: %s (next: %s) ero:%s esr:{%p} ro:%s rosr:{%p} and state: #%lu",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute with c: %s (next: %s) ero:%s esr:{%p} ro:%s rosr:{%p} and state: #%lu"
, c->name, (c->policy_next ? c->policy_next->name
: "none"), ero == ((void*)0) ? "null" : ero->name, esr, ro
== ((void*)0) ? "null" : ro->name, rosr, st == ((void*)0)
? 0 : st->st_serialno); } }
2682 c->name,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute with c: %s (next: %s) ero:%s esr:{%p} ro:%s rosr:{%p} and state: #%lu"
, c->name, (c->policy_next ? c->policy_next->name
: "none"), ero == ((void*)0) ? "null" : ero->name, esr, ro
== ((void*)0) ? "null" : ro->name, rosr, st == ((void*)0)
? 0 : st->st_serialno); } }
2683 (c->policy_next ? c->policy_next->name : "none"),{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute with c: %s (next: %s) ero:%s esr:{%p} ro:%s rosr:{%p} and state: #%lu"
, c->name, (c->policy_next ? c->policy_next->name
: "none"), ero == ((void*)0) ? "null" : ero->name, esr, ro
== ((void*)0) ? "null" : ro->name, rosr, st == ((void*)0)
? 0 : st->st_serialno); } }
2684 ero == NULL ? "null" : ero->name,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute with c: %s (next: %s) ero:%s esr:{%p} ro:%s rosr:{%p} and state: #%lu"
, c->name, (c->policy_next ? c->policy_next->name
: "none"), ero == ((void*)0) ? "null" : ero->name, esr, ro
== ((void*)0) ? "null" : ro->name, rosr, st == ((void*)0)
? 0 : st->st_serialno); } }
2685 esr,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute with c: %s (next: %s) ero:%s esr:{%p} ro:%s rosr:{%p} and state: #%lu"
, c->name, (c->policy_next ? c->policy_next->name
: "none"), ero == ((void*)0) ? "null" : ero->name, esr, ro
== ((void*)0) ? "null" : ro->name, rosr, st == ((void*)0)
? 0 : st->st_serialno); } }
2686 ro == NULL ? "null" : ro->name,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute with c: %s (next: %s) ero:%s esr:{%p} ro:%s rosr:{%p} and state: #%lu"
, c->name, (c->policy_next ? c->policy_next->name
: "none"), ero == ((void*)0) ? "null" : ero->name, esr, ro
== ((void*)0) ? "null" : ro->name, rosr, st == ((void*)0)
? 0 : st->st_serialno); } }
2687 rosr,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute with c: %s (next: %s) ero:%s esr:{%p} ro:%s rosr:{%p} and state: #%lu"
, c->name, (c->policy_next ? c->policy_next->name
: "none"), ero == ((void*)0) ? "null" : ero->name, esr, ro
== ((void*)0) ? "null" : ro->name, rosr, st == ((void*)0)
? 0 : st->st_serialno); } }
2688 st == NULL ? 0 : st->st_serialno){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute with c: %s (next: %s) ero:%s esr:{%p} ro:%s rosr:{%p} and state: #%lu"
, c->name, (c->policy_next ? c->policy_next->name
: "none"), ero == ((void*)0) ? "null" : ero->name, esr, ro
== ((void*)0) ? "null" : ro->name, rosr, st == ((void*)0)
? 0 : st->st_serialno); } }
;
2689
2690 /* look along the chain of policies for same one */
2691
2692 /* we should look for dest port as well? */
2693 /* ports are now switched to the ones in this.client / that.client ??????? */
2694 /* but port set is sr->this.port and sr.that.port ! */
2695 struct bare_shunt **bspp = (ero == NULL((void*)0)) ?
2696 bare_shunt_ptr(&sr->this.client, &sr->that.client, sr->this.protocol) :
2697 NULL((void*)0);
2698
2699 /* install the eroute */
2700
2701 bool_Bool eroute_installed = FALSE0;
2702
2703#ifdef IPSEC_CONNECTION_LIMIT
2704 bool_Bool new_eroute = FALSE0;
2705#endif
2706
2707 passert(bspp == NULL || ero == NULL){ _Bool assertion__ = bspp == ((void*)0) || ero == ((void*)0)
; if (!assertion__) { lsw_passert_fail((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 2707}, "%s", "bspp == NULL || ero == NULL"
); } }
; /* only one non-NULL */
2708
2709 if (bspp != NULL((void*)0) || ero != NULL((void*)0)) {
2710 dbg("we are replacing an eroute"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("we are replacing an eroute"); } }
;
2711 /* if no state provided, then install a shunt for later */
2712 if (st == NULL((void*)0)) {
2713 eroute_installed = shunt_eroute(c, sr,
2714 RT_ROUTED_PROSPECTIVE,
2715 ERO_REPLACE,
2716 "replace");
2717 } else {
2718 eroute_installed = sag_eroute(st, sr, ERO_REPLACE,
2719 "replace");
2720 }
2721
2722 /* remember to free bspp if we make it out of here alive */
2723 } else {
2724 /* we're adding an eroute */
2725#ifdef IPSEC_CONNECTION_LIMIT
2726 if (num_ipsec_eroute == IPSEC_CONNECTION_LIMIT) {
2727 loglog(RC_LOG_SERIOUS,
2728 "Maximum number of IPsec connections reached (%d)",
2729 IPSEC_CONNECTION_LIMIT);
2730 return FALSE0;
2731 }
2732 new_eroute = TRUE1;
2733#endif
2734
2735 /* if no state provided, then install a shunt for later */
2736 if (st == NULL((void*)0)) {
2737 eroute_installed = shunt_eroute(c, sr,
2738 RT_ROUTED_PROSPECTIVE,
2739 ERO_ADD, "add");
2740 } else {
2741 eroute_installed = sag_eroute(st, sr, ERO_ADD, "add");
2742 }
2743 }
2744
2745 /* notify the firewall of a new tunnel */
2746
2747 bool_Bool firewall_notified = FALSE0;
2748
2749 if (eroute_installed) {
2750 /*
2751 * do we have to notify the firewall?
2752 * Yes, if we are installing
2753 * a tunnel eroute and the firewall wasn't notified
2754 * for a previous tunnel with the same clients. Any Previous
2755 * tunnel would have to be for our connection, so the actual
2756 * test is simple.
2757 */
2758 firewall_notified = st == NULL((void*)0) || /* not a tunnel eroute */
2759 sr->eroute_owner != SOS_NOBODY0 || /* already notified */
2760 do_command(c, sr, "up", st); /* go ahead and notify */
2761 }
2762
2763 /* install the route */
2764
2765 bool_Bool route_installed = FALSE0;
2766
2767 dbg("route_and_eroute: firewall_notified: %s",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute: firewall_notified: %s", firewall_notified
? "true" : "false"); } }
2768 firewall_notified ? "true" : "false"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute: firewall_notified: %s", firewall_notified
? "true" : "false"); } }
;
2769 if (!firewall_notified) {
2770 /* we're in trouble -- don't do routing */
2771 } else if (ro == NULL((void*)0)) {
2772 /* a new route: no deletion required, but preparation is */
2773 if (!do_command(c, sr, "prepare", st))
2774 dbg("prepare command returned an error"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("prepare command returned an error"); } }
;
2775 route_installed = do_command(c, sr, "route", st);
2776 if (!route_installed)
2777 dbg("route command returned an error"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route command returned an error"); } }
;
2778 } else if (routed(sr->routing)((sr->routing) > RT_UNROUTED_HOLD) ||
2779 routes_agree(ro, c)((ro)->interface->ip_dev == (c)->interface->ip_dev
&& sameaddr(&(ro)->spd.this.host_nexthop, &
(c)->spd.this.host_nexthop))
) {
2780 route_installed = TRUE1; /* nothing to be done */
2781 } else {
2782 /*
2783 * Some other connection must own the route
2784 * and the route must disagree. But since could_route
2785 * must have allowed our stealing it, we'll do so.
2786 *
2787 * A feature of LINUX allows us to install the new route
2788 * before deleting the old if the nexthops differ.
2789 * This reduces the "window of vulnerability" when packets
2790 * might flow in the clear.
2791 */
2792 if (sameaddr(&sr->this.host_nexthop,
2793 &esr->this.host_nexthop)) {
2794 if (!do_command(ro, sr, "unroute", st)) {
2795 dbg("unroute command returned an error"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("unroute command returned an error"); } }
;
2796 }
2797 route_installed = do_command(c, sr, "route", st);
2798 if (!route_installed)
2799 dbg("route command returned an error"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route command returned an error"); } }
;
2800 } else {
2801 route_installed = do_command(c, sr, "route", st);
2802 if (!route_installed)
2803 dbg("route command returned an error"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route command returned an error"); } }
;
2804
2805 if (!do_command(ro, sr, "unroute", st)) {
2806 dbg("unroute command returned an error"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("unroute command returned an error"); } }
;
2807 }
2808 }
2809
2810 /* record unrouting */
2811 if (route_installed) {
2812 do {
2813 dbg("installed route: ro name=%s, rosr->routing=%d", ro->name,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("installed route: ro name=%s, rosr->routing=%d"
, ro->name, rosr->routing); } }
2814 rosr->routing){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("installed route: ro name=%s, rosr->routing=%d"
, ro->name, rosr->routing); } }
;
2815 pexpect(!erouted(rosr->routing))({ _Bool assertion__ = !((rosr->routing) != RT_UNROUTED); if
(!assertion__) { log_pexpect((where_t) { .func = __func__, .
basename = "kernel.c" , .line = 2815}, "%s", "!erouted(rosr->routing)"
); } assertion__; })
; /* warn for now - requires fixing */
2816 rosr->routing = RT_UNROUTED;
2817
2818 /* no need to keep old value */
2819 ro = route_owner(c, sr, &rosr, NULL((void*)0), NULL((void*)0));
2820 } while (ro != NULL((void*)0));
2821 }
2822 }
2823
2824 /* all done -- clean up */
2825 if (route_installed) {
2826 /* Success! */
2827
2828 if (bspp != NULL((void*)0)) {
2829 free_bare_shunt(bspp);
2830 } else if (ero != NULL((void*)0) && ero != c) {
2831 /* check if ero is an ancestor of c. */
2832 struct connection *ero2;
2833
2834 for (ero2 = c; ero2 != NULL((void*)0) && ero2 != c;
2835 ero2 = ero2->policy_next)
2836 ;
2837
2838 if (ero2 == NULL((void*)0)) {
2839 /*
2840 * By elimination, we must be eclipsing ero.
2841 * Checked above.
2842 */
2843 if (ero->spd.routing != RT_ROUTED_ECLIPSED) {
2844 ero->spd.routing = RT_ROUTED_ECLIPSED;
2845 eclipse_count++;
2846 }
2847 }
2848 }
2849
2850 if (st == NULL((void*)0)) {
2851 passert(sr->eroute_owner == SOS_NOBODY){ _Bool assertion__ = sr->eroute_owner == 0; if (!assertion__
) { lsw_passert_fail((where_t) { .func = __func__, .basename =
"kernel.c" , .line = 2851}, "%s", "sr->eroute_owner == SOS_NOBODY"
); } }
;
2852 sr->routing = RT_ROUTED_PROSPECTIVE;
2853 } else {
2854 sr->routing = RT_ROUTED_TUNNEL;
2855 connection_buf cib;
2856 dbg("route_and_eroute: instance "PRI_CONNECTION", setting eroute_owner {spd=%p,sr=%p} to #%lu (was #%lu) (newest_ipsec_sa=#%lu)",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute: instance ""\"%s\"%s"", setting eroute_owner {spd=%p,sr=%p} to #%lu (was #%lu) (newest_ipsec_sa=#%lu)"
, (st->st_connection)->name, str_connection_instance(st
->st_connection, &cib), &st->st_connection->
spd, sr, st->st_serialno, sr->eroute_owner, st->st_connection
->newest_ipsec_sa); } }
2857 pri_connection(st->st_connection, &cib),{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute: instance ""\"%s\"%s"", setting eroute_owner {spd=%p,sr=%p} to #%lu (was #%lu) (newest_ipsec_sa=#%lu)"
, (st->st_connection)->name, str_connection_instance(st
->st_connection, &cib), &st->st_connection->
spd, sr, st->st_serialno, sr->eroute_owner, st->st_connection
->newest_ipsec_sa); } }
2858 &st->st_connection->spd, sr,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute: instance ""\"%s\"%s"", setting eroute_owner {spd=%p,sr=%p} to #%lu (was #%lu) (newest_ipsec_sa=#%lu)"
, (st->st_connection)->name, str_connection_instance(st
->st_connection, &cib), &st->st_connection->
spd, sr, st->st_serialno, sr->eroute_owner, st->st_connection
->newest_ipsec_sa); } }
2859 st->st_serialno,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute: instance ""\"%s\"%s"", setting eroute_owner {spd=%p,sr=%p} to #%lu (was #%lu) (newest_ipsec_sa=#%lu)"
, (st->st_connection)->name, str_connection_instance(st
->st_connection, &cib), &st->st_connection->
spd, sr, st->st_serialno, sr->eroute_owner, st->st_connection
->newest_ipsec_sa); } }
2860 sr->eroute_owner,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute: instance ""\"%s\"%s"", setting eroute_owner {spd=%p,sr=%p} to #%lu (was #%lu) (newest_ipsec_sa=#%lu)"
, (st->st_connection)->name, str_connection_instance(st
->st_connection, &cib), &st->st_connection->
spd, sr, st->st_serialno, sr->eroute_owner, st->st_connection
->newest_ipsec_sa); } }
2861 st->st_connection->newest_ipsec_sa){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("route_and_eroute: instance ""\"%s\"%s"", setting eroute_owner {spd=%p,sr=%p} to #%lu (was #%lu) (newest_ipsec_sa=#%lu)"
, (st->st_connection)->name, str_connection_instance(st
->st_connection, &cib), &st->st_connection->
spd, sr, st->st_serialno, sr->eroute_owner, st->st_connection
->newest_ipsec_sa); } }
;
2862 sr->eroute_owner = st->st_serialno;
2863 /* clear host shunts that clash with freshly installed route */
2864 clear_narrow_holds(&sr->this.client, &sr->that.client,
2865 sr->this.protocol);
2866 }
2867
2868#ifdef IPSEC_CONNECTION_LIMIT
2869 if (new_eroute) {
2870 num_ipsec_eroute++;
2871 loglog(RC_COMMENT,
2872 "%d IPsec connections are currently being managed",
2873 num_ipsec_eroute);
2874 }
2875#endif
2876
2877 return TRUE1;
2878 } else {
2879 /* Failure! Unwind our work. */
2880 if (firewall_notified && sr->eroute_owner == SOS_NOBODY0) {
2881 if (!do_command(c, sr, "down", st))
2882 dbg("down command returned an error"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("down command returned an error"); } }
;
2883 }
2884
2885 if (eroute_installed) {
2886 /*
2887 * Restore original eroute, if we can.
2888 * Since there is nothing much to be done if
2889 * the restoration fails, ignore success or failure.
2890 */
2891 if (bspp != NULL((void*)0)) {
2892 /*
2893 * Restore old bare_shunt.
2894 * I don't think that this case is very likely.
2895 * Normally a bare shunt would have been
2896 * assigned to a connection before we've
2897 * gotten this far.
2898 */
2899 struct bare_shunt *bs = *bspp;
2900
2901 if (!raw_eroute(&bs->said.dst, /* should be useless */
2902 &bs->our_client,
2903 &bs->said.dst, /* should be useless */
2904 &bs->peer_client,
2905 bs->said.spi, /* unused? network order */
2906 bs->said.spi, /* network order */
2907 &ip_protocol_internal, /* proto */
2908 sr->this.protocol, /* transport_proto */
2909 ET_INT,
2910 null_proto_info,
2911 deltatime(SHUNT_PATIENCE((2 * 10) * 15 / 2)),
2912 calculate_sa_prio(c, FALSE0),
2913 NULL((void*)0),
2914 0,
2915 ERO_REPLACE,
2916 "restore",
2917 NULL((void*)0))) /* bare shunt are not associated with any connection so no security label */
2918 {
2919 libreswan_log("raw_eroute() in route_and_eroute() failed to restore/replace SA")loglog(RC_LOG, "raw_eroute() in route_and_eroute() failed to restore/replace SA"
)
;
2920 }
2921 } else if (ero != NULL((void*)0)) {
2922 passert(esr != NULL){ _Bool assertion__ = esr != ((void*)0); if (!assertion__) { lsw_passert_fail
((where_t) { .func = __func__, .basename = "kernel.c" , .line
= 2922}, "%s", "esr != NULL"); } }
;
2923 /* restore ero's former glory */
2924 if (esr->eroute_owner == SOS_NOBODY0) {
2925 /* note: normal or eclipse case */
2926 if (!shunt_eroute(ero, esr,
2927 esr->routing,
2928 ERO_REPLACE,
2929 "restore")) {
2930 libreswan_log("shunt_eroute() in route_and_eroute() failed restore/replace")loglog(RC_LOG, "shunt_eroute() in route_and_eroute() failed restore/replace"
)
;
2931 }
2932 } else {
2933 /*
2934 * Try to find state that owned eroute.
2935 * Don't do anything if it cannot be
2936 * found.
2937 * This case isn't likely since we
2938 * don't run the updown script when
2939 * replacing a SA group with its
2940 * successor (for the same conn).
2941 */
2942 struct state *ost =
2943 state_with_serialno(
2944 esr->eroute_owner);
2945
2946 if (ost != NULL((void*)0)) {
2947 if (!sag_eroute(ost, esr,
2948 ERO_REPLACE,
2949 "restore"))
2950 libreswan_log("sag_eroute() in route_and_eroute() failed restore/replace")loglog(RC_LOG, "sag_eroute() in route_and_eroute() failed restore/replace"
)
;
2951 }
2952 }
2953 } else {
2954 /* there was no previous eroute: delete whatever we installed */
2955 if (st == NULL((void*)0)) {
2956 if (!shunt_eroute(c, sr,
2957 sr->routing,
2958 ERO_DELETE,
2959 "delete")) {
2960 libreswan_log("shunt_eroute() in route_and_eroute() failed in !st case for delete")loglog(RC_LOG, "shunt_eroute() in route_and_eroute() failed in !st case for delete"
)
;
2961 }
2962 } else {
2963 if (!sag_eroute(st, sr,
2964 ERO_DELETE,
2965 "delete")) {
2966 libreswan_log("shunt_eroute() in route_and_eroute() failed in st case for delete")loglog(RC_LOG, "shunt_eroute() in route_and_eroute() failed in st case for delete"
)
;
2967 }
2968 }
2969 }
2970 }
2971
2972 return FALSE0;
2973 }
2974}
2975
2976bool_Bool install_ipsec_sa(struct state *st, bool_Bool inbound_also)
2977{
2978 dbg("install_ipsec_sa() for #%lu: %s", st->st_serialno,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("install_ipsec_sa() for #%lu: %s", st->st_serialno
, inbound_also ? "inbound and outbound" : "outbound only"); }
}
2979 inbound_also ? "inbound and outbound" : "outbound only"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("install_ipsec_sa() for #%lu: %s", st->st_serialno
, inbound_also ? "inbound and outbound" : "outbound only"); }
}
;
2980
2981 enum routability rb = could_route(st->st_connection, st->st_logger);
2982
2983 switch (rb) {
2984 case route_easy:
2985 case route_unnecessary:
2986 case route_nearconflict:
2987 break;
2988
2989 default:
2990 return false0;
2991 }
2992
2993 /* (attempt to) actually set up the SA group */
2994
2995 /* setup outgoing SA if we haven't already */
2996 if (!st->st_outbound_done) {
2997 if (!setup_half_ipsec_sa(st, FALSE0)) {
2998 return FALSE0;
2999 }
3000
3001 dbg("set up outgoing SA, ref=%u/%u", st->st_ref,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("set up outgoing SA, ref=%u/%u", st->st_ref
, st->st_ref_peer); } }
3002 st->st_ref_peer){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("set up outgoing SA, ref=%u/%u", st->st_ref
, st->st_ref_peer); } }
;
3003 st->st_outbound_done = TRUE1;
3004 }
3005
3006 /* now setup inbound SA */
3007 if (st->st_ref == IPSEC_SAREF_NULL((IPsecSAref_t)0u) && inbound_also) {
3008 if (!setup_half_ipsec_sa(st, TRUE1))
3009 return FALSE0;
3010
3011 dbg("set up incoming SA, ref=%u/%u", st->st_ref,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("set up incoming SA, ref=%u/%u", st->st_ref
, st->st_ref_peer); } }
3012 st->st_ref_peer){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("set up incoming SA, ref=%u/%u", st->st_ref
, st->st_ref_peer); } }
;
3013
3014 /*
3015 * We successfully installed an IPsec SA, meaning it is safe
3016 * to clear our revival back-off delay. This is based on the
3017 * assumption that an unwilling partner might complete an IKE
3018 * SA to us, but won't complete an IPsec SA to us.
3019 */
3020 st->st_connection->temp_vars.revive_delay = 0;
3021 }
3022
3023 if (rb == route_unnecessary)
3024 return TRUE1;
3025
3026 struct spd_route *sr = &st->st_connection->spd;
3027
3028 if (st->st_connection->remotepeertype == CISCO && sr->spd_next != NULL((void*)0))
3029 sr = sr->spd_next;
3030
3031 /* for (sr = &st->st_connection->spd; sr != NULL; sr = sr->next) */
3032 for (; sr != NULL((void*)0); sr = sr->spd_next) {
3033 dbg("sr for #%lu: %s", st->st_serialno,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("sr for #%lu: %s", st->st_serialno, enum_name
(&routing_story, sr->routing)); } }
3034 enum_name(&routing_story, sr->routing)){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("sr for #%lu: %s", st->st_serialno, enum_name
(&routing_story, sr->routing)); } }
;
3035
3036 /*
3037 * if the eroute owner is not us, then make it us.
3038 * See test co-terminal-02, pluto-rekey-01,
3039 * pluto-unit-02/oppo-twice
3040 */
3041 pexpect(sr->eroute_owner == SOS_NOBODY ||({ _Bool assertion__ = sr->eroute_owner == 0 || sr->routing
>= RT_ROUTED_TUNNEL; if (!assertion__) { log_pexpect((where_t
) { .func = __func__, .basename = "kernel.c" , .line = 3042},
"%s", "sr->eroute_owner == SOS_NOBODY || sr->routing >= RT_ROUTED_TUNNEL"
); } assertion__; })
3042 sr->routing >= RT_ROUTED_TUNNEL)({ _Bool assertion__ = sr->eroute_owner == 0 || sr->routing
>= RT_ROUTED_TUNNEL; if (!assertion__) { log_pexpect((where_t
) { .func = __func__, .basename = "kernel.c" , .line = 3042},
"%s", "sr->eroute_owner == SOS_NOBODY || sr->routing >= RT_ROUTED_TUNNEL"
); } assertion__; })
;
3043
3044 if (sr->eroute_owner != st->st_serialno &&
3045 sr->routing != RT_UNROUTED_KEYED) {
3046 if (!route_and_eroute(st->st_connection, sr, st)) {
3047 delete_ipsec_sa(st);
3048 /*
3049 * XXX go and unroute any SRs that were
3050 * successfully routed already.
3051 */
3052 return false0;
3053 }
3054 }
3055 }
3056
3057 /* XXX why is this needed? Skip the bogus original conn? */
3058 if (st->st_connection->remotepeertype == CISCO) {
3059 struct spd_route *srcisco = st->st_connection->spd.spd_next;
3060
3061 if (srcisco != NULL((void*)0)) {
3062 st->st_connection->spd.eroute_owner = srcisco->eroute_owner;
3063 st->st_connection->spd.routing = srcisco->routing;
3064 }
3065 }
3066
3067 if (inbound_also)
3068 linux_audit_conn(st, LAK_CHILD_START);
3069 return true1;
3070}
3071
3072bool_Bool migrate_ipsec_sa(struct state *st)
3073{
3074 switch (kernel_ops->type) {
3075 case USE_XFRM:
3076 /* support ah? if(!st->st_esp.present && !st->st_ah.present)) */
3077 if (!st->st_esp.present) {
3078 libreswan_log("mobike SA migration only support ESP SA")loglog(RC_LOG, "mobike SA migration only support ESP SA");
3079 return FALSE0;
3080 }
3081
3082 if (!kernel_ops->migrate_sa(st))
3083 return FALSE0;
3084
3085 return TRUE1;
3086
3087 default:
3088 dbg("Usupported kernel stack in migrate_ipsec_sa"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("Usupported kernel stack in migrate_ipsec_sa")
; } }
;
3089 return FALSE0;
3090 }
3091}
3092
3093/*
3094 * Delete an IPSEC SA.
3095 * we may not succeed, but we bull ahead anyway because
3096 * we cannot do anything better by recognizing failure
3097 * This used to have a parameter bool inbound_only, but
3098 * the saref code changed to always install inbound before
3099 * outbound so this it was always false, and thus removed
3100 *
3101 */
3102void delete_ipsec_sa(struct state *st)
3103{
3104 /* XXX in IKEv2 we get a spurious call with a parent st :( */
3105 if (IS_CHILD_SA(st)((st)->st_clonedfrom != 0)) {
3106 if (st->st_esp.present || st->st_ah.present) {
3107 /* ESP or AH means this was an established IPsec SA */
3108 linux_audit_conn(st, LAK_CHILD_DESTROY);
3109 }
3110 } else {
3111 libreswan_log("delete_ipsec_sa() called with (wrong?) parent state %s",loglog(RC_LOG, "delete_ipsec_sa() called with (wrong?) parent state %s"
, st->st_state->name)
3112 st->st_state->name)loglog(RC_LOG, "delete_ipsec_sa() called with (wrong?) parent state %s"
, st->st_state->name)
;
3113 }
3114
3115 switch (kernel_ops->type) {
3116 case USE_XFRM:
3117 {
3118 /*
3119 * If the state is the eroute owner, we must adjust
3120 * the routing for the connection.
3121 */
3122 struct connection *c = st->st_connection;
3123 struct spd_route *sr;
3124
3125 for (sr = &c->spd; sr; sr = sr->spd_next) {
3126 if (sr->eroute_owner == st->st_serialno &&
3127 sr->routing == RT_ROUTED_TUNNEL) {
3128 sr->eroute_owner = SOS_NOBODY0;
3129
3130 /*
3131 * Routing should become
3132 * RT_ROUTED_FAILURE,
3133 * but if POLICY_FAIL_NONE, then we
3134 * just go right back to
3135 * RT_ROUTED_PROSPECTIVE as if no
3136 * failure happened.
3137 */
3138 sr->routing =
3139 (c->policy &
3140 POLICY_FAIL_MASK(((lset_t)1 << (POLICY_FAIL1_IX)) - ((lset_t)1 <<
(POLICY_FAIL0_IX)) + ((lset_t)1 << (POLICY_FAIL1_IX)))
) ==
3141 POLICY_FAIL_NONE(0 * ((lset_t)1 << (POLICY_FAIL0_IX))) ?
3142 RT_ROUTED_PROSPECTIVE :
3143 RT_ROUTED_FAILURE;
3144
3145 if (sr == &c->spd &&
3146 c->remotepeertype == CISCO)
3147 continue;
3148
3149 (void) do_command(c, sr, "down", st);
3150 if ((c->policy & POLICY_OPPORTUNISTIC((lset_t)1 << (POLICY_OPPORTUNISTIC_IX))) &&
3151 c->kind == CK_INSTANCE) {
3152 /*
3153 * in this case we get rid of
3154 * the IPSEC SA
3155 */
3156 unroute_connection(c);
3157 } else if ((c->policy & POLICY_DONT_REKEY((lset_t)1 << (POLICY_DONT_REKEY_IX))) &&
3158 c->kind == CK_INSTANCE) {
3159 /*
3160 * in this special case,
3161 * even if the connection
3162 * is still alive (due to
3163 * an ISAKMP SA),
3164 * we get rid of routing.
3165 * Even though there is still
3166 * an eroute, the c->routing
3167 * setting will convince
3168 * unroute_connection to
3169 * delete it.
3170 * unroute_connection
3171 * would be upset
3172 * if c->routing ==
3173 * RT_ROUTED_TUNNEL
3174 */
3175 unroute_connection(c);
3176 } else {
3177 if (!shunt_eroute(c, sr,
3178 sr->routing,
3179 ERO_REPLACE,
3180 "replace with shunt")) {
3181 libreswan_log("shunt_eroute() failed replace with shunt in delete_ipsec_sa()")loglog(RC_LOG, "shunt_eroute() failed replace with shunt in delete_ipsec_sa()"
)
;
3182 }
3183 }
3184 }
3185 }
3186 (void) teardown_half_ipsec_sa(st, FALSE0);
3187 }
3188 (void) teardown_half_ipsec_sa(st, TRUE1);
3189
3190 break;
3191 default:
3192 dbg("unknown kernel stack in delete_ipsec_sa"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("unknown kernel stack in delete_ipsec_sa"); } }
;
3193 break;
3194 } /* switch kernel_ops->type */
3195}
3196
3197bool_Bool was_eroute_idle(struct state *st, deltatime_t since_when)
3198{
3199 if (kernel_ops->eroute_idle != NULL((void*)0))
3200 return kernel_ops->eroute_idle(st, since_when);
3201
3202 /* it is never idle if we can't check */
3203 return FALSE0;
3204}
3205
3206/*
3207 * get information about a given sa - needs merging with was_eroute_idle
3208 *
3209 * Note: this mutates *st.
3210 */
3211bool_Bool get_sa_info(struct state *st, bool_Bool inbound, deltatime_t *ago /* OUTPUT */)
3212{
3213 struct connection *const c = st->st_connection;
3214
3215 if (kernel_ops->get_sa == NULL((void*)0) || (!st->st_esp.present && !st->st_ah.present)) {
3216 return FALSE0;
3217 }
3218
3219 const struct ip_protocol *proto;
3220 struct ipsec_proto_info *p2;
3221
3222 if (st->st_esp.present) {
3223 proto = &ip_protocol_esp;
3224 p2 = &st->st_esp;
3225 } else if (st->st_ah.present) {
3226 proto = &ip_protocol_ah;
3227 p2 = &st->st_ah;
3228 } else {
3229 return FALSE0;
3230 }
3231
3232 const ip_address *src, *dst;
3233 ipsec_spi_t spi;
3234 bool_Bool redirected = FALSE0;
3235 ip_address tmp_ip;
3236
3237 /*
3238 * if we were redirected (using the REDIRECT
3239 * mechanism), change
3240 * spd.that.host_addr temporarily, we reset
3241 * it back later
3242 */
3243 if (!sameaddr(&st->st_remote_endpoint, &c->spd.that.host_addr) &&
3244 address_is_specified(&c->temp_vars.redirect_ip)) {
3245 redirected = TRUE1;
3246 tmp_ip = c->spd.that.host_addr;
3247 tmp_ip.version = c->spd.that.host_addr.version;
3248 tmp_ip.hport = c->spd.that.host_addr.hport;
3249 c->spd.that.host_addr = st->st_remote_endpoint;
3250 }
3251
3252 if (inbound) {
3253 src = &c->spd.that.host_addr;
3254 dst = &c->spd.this.host_addr;
3255 spi = p2->our_spi;
3256 } else {
3257 src = &c->spd.this.host_addr;
3258 dst = &c->spd.that.host_addr;
3259 spi = p2->attrs.spi;
3260 }
3261
3262 char text_said[SATOT_BUFsizeof(said_buf)];
3263
3264 set_text_said(text_said, dst, spi, proto);
3265
3266 struct kernel_sa sa = {
3267 .spi = spi,
3268 .proto = proto,
3269 .src.address = src,
3270 .dst.address = dst,
3271 .text_said = text_said,
3272 };
3273
3274 dbg("get_sa_info %s", text_said){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("get_sa_info %s", text_said); } }
;
3275
3276 uint64_t bytes;
3277 uint64_t add_time;
3278
3279 if (!kernel_ops->get_sa(&sa, &bytes, &add_time))
3280 return FALSE0;
3281
3282 p2->add_time = add_time;
3283
3284 /* field has been set? */
3285 passert(!is_monotime_epoch(p2->our_lastused)){ _Bool assertion__ = !is_monotime_epoch(p2->our_lastused)
; if (!assertion__) { lsw_passert_fail((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 3285}, "%s", "!is_monotime_epoch(p2->our_lastused)"
); } }
;
3286 passert(!is_monotime_epoch(p2->peer_lastused)){ _Bool assertion__ = !is_monotime_epoch(p2->peer_lastused
); if (!assertion__) { lsw_passert_fail((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 3286}, "%s", "!is_monotime_epoch(p2->peer_lastused)"
); } }
;
3287
3288 if (inbound) {
3289 if (bytes > p2->our_bytes) {
3290 p2->our_bytes = bytes;
3291 p2->our_lastused = mononow();
3292 }
3293 if (ago != NULL((void*)0))
3294 *ago = monotimediff(mononow(), p2->our_lastused);
3295 } else {
3296 if (bytes > p2->peer_bytes) {
3297 p2->peer_bytes = bytes;
3298 p2->peer_lastused = mononow();
3299 }
3300 if (ago != NULL((void*)0))
3301 *ago = monotimediff(mononow(), p2->peer_lastused);
3302 }
3303
3304 if (redirected)
3305 c->spd.that.host_addr = tmp_ip;
3306
3307 return TRUE1;
3308}
3309
3310bool_Bool orphan_holdpass(const struct connection *c, struct spd_route *sr,
3311 int transport_proto, ipsec_spi_t failure_shunt)
3312{
3313 enum routing_t ro = sr->routing, /* routing, old */
3314 rn = ro; /* routing, new */
3315 ipsec_spi_t negotiation_shunt = (c->policy & POLICY_NEGO_PASS((lset_t)1 << (POLICY_NEGO_PASS_IX))) ? SPI_PASS256 : SPI_DROP257;
3316
3317 if (negotiation_shunt != failure_shunt ) {
3318 dbg("failureshunt != negotiationshunt, needs replacing"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("failureshunt != negotiationshunt, needs replacing"
); } }
;
3319 } else {
3320 dbg("failureshunt == negotiationshunt, no replace needed"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("failureshunt == negotiationshunt, no replace needed"
); } }
;
3321 }
3322
3323 dbg("orphan_holdpass() called for %s with transport_proto '%d' and sport %d and dport %d",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("orphan_holdpass() called for %s with transport_proto '%d' and sport %d and dport %d"
, c->name, transport_proto, sr->this.port, sr->that.
port); } }
3324 c->name, transport_proto, sr->this.port, sr->that.port){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("orphan_holdpass() called for %s with transport_proto '%d' and sport %d and dport %d"
, c->name, transport_proto, sr->this.port, sr->that.
port); } }
;
3325
3326 passert(LHAS(LELEM(CK_PERMANENT) | LELEM(CK_INSTANCE) |{ _Bool assertion__ = (((((lset_t)1 << (CK_PERMANENT)) |
((lset_t)1 << (CK_INSTANCE)) | ((lset_t)1 << (CK_GOING_AWAY
))) & ((lset_t)1 << (c->kind))) != ((lset_t)0));
if (!assertion__) { lsw_passert_fail((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 3327}, "%s", "LHAS(LELEM(CK_PERMANENT) | LELEM(CK_INSTANCE) | LELEM(CK_GOING_AWAY), c->kind)"
); } }
3327 LELEM(CK_GOING_AWAY), c->kind)){ _Bool assertion__ = (((((lset_t)1 << (CK_PERMANENT)) |
((lset_t)1 << (CK_INSTANCE)) | ((lset_t)1 << (CK_GOING_AWAY
))) & ((lset_t)1 << (c->kind))) != ((lset_t)0));
if (!assertion__) { lsw_passert_fail((where_t) { .func = __func__
, .basename = "kernel.c" , .line = 3327}, "%s", "LHAS(LELEM(CK_PERMANENT) | LELEM(CK_INSTANCE) | LELEM(CK_GOING_AWAY), c->kind)"
); } }
;
3328
3329 switch (ro) {
3330 case RT_UNROUTED_HOLD:
3331 rn = RT_UNROUTED;
3332 dbg("orphan_holdpass unrouted: hold -> pass"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("orphan_holdpass unrouted: hold -> pass"); }
}
;
3333 break;
3334 case RT_UNROUTED:
3335 rn = RT_UNROUTED_HOLD;
3336 dbg("orphan_holdpass unrouted: pass -> hold"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("orphan_holdpass unrouted: pass -> hold"); }
}
;
3337 break;
3338 case RT_ROUTED_HOLD:
3339 rn = RT_ROUTED_PROSPECTIVE;
3340 dbg("orphan_holdpass routed: hold -> trap (?)"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("orphan_holdpass routed: hold -> trap (?)")
; } }
;
3341 break;
3342 default:
3343 dbg("no routing change needed for ro=%s - negotiation shunt matched failure shunt?",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("no routing change needed for ro=%s - negotiation shunt matched failure shunt?"
, enum_name(&routing_story, ro)); } }
3344 enum_name(&routing_story, ro)){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("no routing change needed for ro=%s - negotiation shunt matched failure shunt?"
, enum_name(&routing_story, ro)); } }
;
3345 break;
3346 }
3347
3348 dbg("orphaning holdpass for connection '%s', routing was %s, needs to be %s",{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("orphaning holdpass for connection '%s', routing was %s, needs to be %s"
, c->name, enum_name(&routing_story, ro), enum_name(&
routing_story, rn)); } }
3349 c->name,{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("orphaning holdpass for connection '%s', routing was %s, needs to be %s"
, c->name, enum_name(&routing_story, ro), enum_name(&
routing_story, rn)); } }
3350 enum_name(&routing_story, ro),{ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("orphaning holdpass for connection '%s', routing was %s, needs to be %s"
, c->name, enum_name(&routing_story, ro), enum_name(&
routing_story, rn)); } }
3351 enum_name(&routing_story, rn)){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("orphaning holdpass for connection '%s', routing was %s, needs to be %s"
, c->name, enum_name(&routing_story, ro), enum_name(&
routing_story, rn)); } }
;
3352
3353 {
3354 /* are we replacing a bare shunt ? */
3355 setportof(htons(sr->this.port), &sr->this.client.addr){ *(&sr->this.client.addr) = set_endpoint_hport((&
sr->this.client.addr), ntohs(htons(sr->this.port))); }
;
3356 setportof(htons(sr->that.port), &sr->that.client.addr){ *(&sr->that.client.addr) = set_endpoint_hport((&
sr->that.client.addr), ntohs(htons(sr->that.port))); }
;
3357 struct bare_shunt **old = bare_shunt_ptr(&sr->this.client, &sr->that.client, sr->this.protocol);
3358
3359 if (old != NULL((void*)0)) {
3360 free_bare_shunt(old);
3361 }
3362 }
3363
3364 /* create the bare shunt and update kernel policy if needed */
3365 {
3366 struct bare_shunt *bs = alloc_thing(struct bare_shunt, "orphan shunt")((struct bare_shunt*) alloc_bytes(sizeof(struct bare_shunt), (
"orphan shunt")))
;
3367
3368 bs->why = "oe-failing";
3369 bs->our_client = sr->this.client;
3370 bs->peer_client = sr->that.client;
3371 bs->transport_proto = sr->this.protocol;
3372 bs->policy_prio = BOTTOM_PRIO((policy_prio_t)0);
3373
3374 bs->said = said3(&subnet_type(&sr->this.client)->any_address,
3375 htonl(negotiation_shunt), &ip_protocol_internal);
3376
3377 bs->count = 0;
3378 bs->last_activity = mononow();
3379 if (strstr(c->name, "/32") != NULL((void*)0) || strstr(c->name, "/128") != NULL((void*)0)) {
3380 bs->from_cn = clone_str(c->name, "conn name in bare shunt")((c->name) == ((void*)0) ? ((void*)0) : clone_bytes((c->
name), strlen((c->name)) + 1, ("conn name in bare shunt"))
)
;
3381 }
3382
3383 bs->next = bare_shunts;
3384 bare_shunts = bs;
3385 dbg_bare_shunt("add", bs);
3386
3387 /* update kernel policy if needed */
3388 /* This really causes the name to remain "oe-failing", we should be able to update only only the name of the shunt */
3389 if (negotiation_shunt != failure_shunt ) {
3390 dbg("replacing negotiation_shunt with failure_shunt"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("replacing negotiation_shunt with failure_shunt"
); } }
;
3391 if (!replace_bare_shunt(&sr->this.host_addr, &sr->that.host_addr, bs->policy_prio,
3392 negotiation_shunt, failure_shunt, bs->transport_proto,
3393 "oe-failed")) {
3394 libreswan_log("assign_holdpass() failed to update shunt policy")loglog(RC_LOG, "assign_holdpass() failed to update shunt policy"
)
;
3395 }
3396 } else {
3397 dbg("No need to replace negotiation_shunt with failure_shunt - they are the same"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("No need to replace negotiation_shunt with failure_shunt - they are the same"
); } }
;
3398 }
3399 }
3400
3401 /* change routing so we don't get cleared out when state/connection dies */
3402 sr->routing = rn;
3403 dbg("orphan_holdpas() done - returning success"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("orphan_holdpas() done - returning success"); }
}
;
3404 return TRUE1;
3405}
3406
3407/* XXX move to proper kernel_ops in kernel_netlink */
3408void expire_bare_shunts(void)
3409{
3410 dbg("checking for aged bare shunts from shunt table to expire"){ if ((cur_debugging & (((lset_t)1 << (DBG_BASE_IX)
)))) { DBG_log("checking for aged bare shunts from shunt table to expire"
); } }
;
3411 for (struct bare_shunt **bspp = &bare_shunts; *bspp != NULL((void*)0); ) {
3412 struct bare_shunt *bsp = *bspp;
3413 time_t age = deltasecs(monotimediff(mononow(), bsp->last_activity));
3414 struct connection *c = NULL((void*)0);
3415
3416 if (age > deltasecs(pluto_shunt_lifetime)) {
3417 dbg_bare_shunt("expiring old", bsp);
3418 if (bsp->from_cn != NULL((void*)0)) {
3419 c = conn_by_name(bsp->from_cn, FALSE0);
3420 if (c != NULL((void*)0)) {
3421 if (!shunt_eroute(c, &c->spd, RT_ROUTED_PROSPECTIVE, ERO_ADD, "add")) {
3422 libreswan_log("trap shunt install failed ")loglog(RC_LOG, "trap shunt install failed ");
3423 }
3424 }
3425 }
3426 if (!delete_bare_shunt(&bsp->our_client.addr, &bsp->peer_client.addr,
3427 bsp->transport_proto,
3428 ntohl(bsp->said.spi),
3429 (bsp->from_cn == NULL((void*)0) ? "expire_bare_shunt" :
3430 "IGNORE_ON_XFRM: expire_bare_shunt"))) {
3431 log_global(RC_LOG_SERIOUS, null_fd, "failed to delete bare shunt"){ struct logger log_ = (struct logger) { .where = (where_t) {
.func = __func__, .basename = "kernel.c" , .line = 3431}, .global_whackfd
= ((struct fd *) ((void*)0)), .object = ((void*)0), .object_vec
= &logger_global_vec, }; log_message(RC_LOG_SERIOUS, &
log_, "failed to delete bare shunt"); }
;
3432 }
3433 passert(bsp != *bspp){ _Bool assertion__ = bsp != *bspp; if (!assertion__) { lsw_passert_fail
((where_t) { .func = __func__, .basename = "kernel.c" , .line
= 3433}, "%s", "bsp != *bspp"); } }
;
3434 } else {
3435 dbg_bare_shunt("keeping recent", bsp);
3436 bspp = &bsp->next;
3437 }
3438 }
3439}