Fri Dec 1 06:34:14 2017 UTC ()
Don't touch an SP without a reference to it


(ozaki-r)
diff -r1.245 -r1.246 src/sys/netipsec/key.c

cvs diff -r1.245 -r1.246 src/sys/netipsec/key.c (switch to unified diff)

--- src/sys/netipsec/key.c 2017/11/30 02:45:12 1.245
+++ src/sys/netipsec/key.c 2017/12/01 06:34:14 1.246
@@ -1,1034 +1,1034 @@ @@ -1,1034 +1,1034 @@
1/* $NetBSD: key.c,v 1.245 2017/11/30 02:45:12 ozaki-r Exp $ */ 1/* $NetBSD: key.c,v 1.246 2017/12/01 06:34:14 ozaki-r Exp $ */
2/* $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.3 2004/02/14 22:23:23 bms Exp $ */ 2/* $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.3 2004/02/14 22:23:23 bms Exp $ */
3/* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */ 3/* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
4 4
5/* 5/*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved. 7 * All rights reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the 15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution. 16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors 17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software 18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission. 19 * without specific prior written permission.
20 * 20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE. 31 * SUCH DAMAGE.
32 */ 32 */
33 33
34#include <sys/cdefs.h> 34#include <sys/cdefs.h>
35__KERNEL_RCSID(0, "$NetBSD: key.c,v 1.245 2017/11/30 02:45:12 ozaki-r Exp $"); 35__KERNEL_RCSID(0, "$NetBSD: key.c,v 1.246 2017/12/01 06:34:14 ozaki-r Exp $");
36 36
37/* 37/*
38 * This code is referred to RFC 2367 38 * This code is referred to RFC 2367
39 */ 39 */
40 40
41#if defined(_KERNEL_OPT) 41#if defined(_KERNEL_OPT)
42#include "opt_inet.h" 42#include "opt_inet.h"
43#include "opt_ipsec.h" 43#include "opt_ipsec.h"
44#include "opt_gateway.h" 44#include "opt_gateway.h"
45#include "opt_net_mpsafe.h" 45#include "opt_net_mpsafe.h"
46#endif 46#endif
47 47
48#include <sys/types.h> 48#include <sys/types.h>
49#include <sys/param.h> 49#include <sys/param.h>
50#include <sys/systm.h> 50#include <sys/systm.h>
51#include <sys/callout.h> 51#include <sys/callout.h>
52#include <sys/kernel.h> 52#include <sys/kernel.h>
53#include <sys/mbuf.h> 53#include <sys/mbuf.h>
54#include <sys/domain.h> 54#include <sys/domain.h>
55#include <sys/socket.h> 55#include <sys/socket.h>
56#include <sys/socketvar.h> 56#include <sys/socketvar.h>
57#include <sys/sysctl.h> 57#include <sys/sysctl.h>
58#include <sys/errno.h> 58#include <sys/errno.h>
59#include <sys/proc.h> 59#include <sys/proc.h>
60#include <sys/queue.h> 60#include <sys/queue.h>
61#include <sys/syslog.h> 61#include <sys/syslog.h>
62#include <sys/once.h> 62#include <sys/once.h>
63#include <sys/cprng.h> 63#include <sys/cprng.h>
64#include <sys/psref.h> 64#include <sys/psref.h>
65#include <sys/lwp.h> 65#include <sys/lwp.h>
66#include <sys/workqueue.h> 66#include <sys/workqueue.h>
67#include <sys/kmem.h> 67#include <sys/kmem.h>
68#include <sys/cpu.h> 68#include <sys/cpu.h>
69#include <sys/atomic.h> 69#include <sys/atomic.h>
70#include <sys/pslist.h> 70#include <sys/pslist.h>
71#include <sys/mutex.h> 71#include <sys/mutex.h>
72#include <sys/condvar.h> 72#include <sys/condvar.h>
73#include <sys/localcount.h> 73#include <sys/localcount.h>
74#include <sys/pserialize.h> 74#include <sys/pserialize.h>
75 75
76#include <net/if.h> 76#include <net/if.h>
77#include <net/route.h> 77#include <net/route.h>
78 78
79#include <netinet/in.h> 79#include <netinet/in.h>
80#include <netinet/in_systm.h> 80#include <netinet/in_systm.h>
81#include <netinet/ip.h> 81#include <netinet/ip.h>
82#include <netinet/in_var.h> 82#include <netinet/in_var.h>
83#ifdef INET 83#ifdef INET
84#include <netinet/ip_var.h> 84#include <netinet/ip_var.h>
85#endif 85#endif
86 86
87#ifdef INET6 87#ifdef INET6
88#include <netinet/ip6.h> 88#include <netinet/ip6.h>
89#include <netinet6/in6_var.h> 89#include <netinet6/in6_var.h>
90#include <netinet6/ip6_var.h> 90#include <netinet6/ip6_var.h>
91#endif /* INET6 */ 91#endif /* INET6 */
92 92
93#ifdef INET 93#ifdef INET
94#include <netinet/in_pcb.h> 94#include <netinet/in_pcb.h>
95#endif 95#endif
96#ifdef INET6 96#ifdef INET6
97#include <netinet6/in6_pcb.h> 97#include <netinet6/in6_pcb.h>
98#endif /* INET6 */ 98#endif /* INET6 */
99 99
100#include <net/pfkeyv2.h> 100#include <net/pfkeyv2.h>
101#include <netipsec/keydb.h> 101#include <netipsec/keydb.h>
102#include <netipsec/key.h> 102#include <netipsec/key.h>
103#include <netipsec/keysock.h> 103#include <netipsec/keysock.h>
104#include <netipsec/key_debug.h> 104#include <netipsec/key_debug.h>
105 105
106#include <netipsec/ipsec.h> 106#include <netipsec/ipsec.h>
107#ifdef INET6 107#ifdef INET6
108#include <netipsec/ipsec6.h> 108#include <netipsec/ipsec6.h>
109#endif 109#endif
110#include <netipsec/ipsec_private.h> 110#include <netipsec/ipsec_private.h>
111 111
112#include <netipsec/xform.h> 112#include <netipsec/xform.h>
113#include <netipsec/ipcomp.h> 113#include <netipsec/ipcomp.h>
114 114
115 115
116#include <net/net_osdep.h> 116#include <net/net_osdep.h>
117 117
118#define FULLMASK 0xff 118#define FULLMASK 0xff
119#define _BITS(bytes) ((bytes) << 3) 119#define _BITS(bytes) ((bytes) << 3)
120 120
121#define PORT_NONE 0 121#define PORT_NONE 0
122#define PORT_LOOSE 1 122#define PORT_LOOSE 1
123#define PORT_STRICT 2 123#define PORT_STRICT 2
124 124
125percpu_t *pfkeystat_percpu; 125percpu_t *pfkeystat_percpu;
126 126
127/* 127/*
128 * Note on SA reference counting: 128 * Note on SA reference counting:
129 * - SAs that are not in DEAD state will have (total external reference + 1) 129 * - SAs that are not in DEAD state will have (total external reference + 1)
130 * following value in reference count field. they cannot be freed and are 130 * following value in reference count field. they cannot be freed and are
131 * referenced from SA header. 131 * referenced from SA header.
132 * - SAs that are in DEAD state will have (total external reference) 132 * - SAs that are in DEAD state will have (total external reference)
133 * in reference count field. they are ready to be freed. reference from 133 * in reference count field. they are ready to be freed. reference from
134 * SA header will be removed in key_delsav(), when the reference count 134 * SA header will be removed in key_delsav(), when the reference count
135 * field hits 0 (= no external reference other than from SA header. 135 * field hits 0 (= no external reference other than from SA header.
136 */ 136 */
137 137
138u_int32_t key_debug_level = 0; 138u_int32_t key_debug_level = 0;
139static u_int key_spi_trycnt = 1000; 139static u_int key_spi_trycnt = 1000;
140static u_int32_t key_spi_minval = 0x100; 140static u_int32_t key_spi_minval = 0x100;
141static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */ 141static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
142static u_int32_t policy_id = 0; 142static u_int32_t policy_id = 0;
143static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/ 143static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
144static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/ 144static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
145static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/ 145static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
146static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/ 146static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
147static int key_prefered_oldsa = 0; /* prefered old sa rather than new sa.*/ 147static int key_prefered_oldsa = 0; /* prefered old sa rather than new sa.*/
148 148
149static u_int32_t acq_seq = 0; 149static u_int32_t acq_seq = 0;
150 150
151/* 151/*
152 * Locking order: there is no order for now; it means that any locks aren't 152 * Locking order: there is no order for now; it means that any locks aren't
153 * overlapped. 153 * overlapped.
154 */ 154 */
155/* 155/*
156 * Locking notes on SPD: 156 * Locking notes on SPD:
157 * - Modifications to the key_spd.splist must be done with holding key_spd.lock 157 * - Modifications to the key_spd.splist must be done with holding key_spd.lock
158 * which is a adaptive mutex 158 * which is a adaptive mutex
159 * - Read accesses to the key_spd.splist must be in pserialize(9) read sections 159 * - Read accesses to the key_spd.splist must be in pserialize(9) read sections
160 * - SP's lifetime is managed by localcount(9) 160 * - SP's lifetime is managed by localcount(9)
161 * - An SP that has been inserted to the key_spd.splist is initially referenced 161 * - An SP that has been inserted to the key_spd.splist is initially referenced
162 * by none, i.e., a reference from the key_spd.splist isn't counted 162 * by none, i.e., a reference from the key_spd.splist isn't counted
163 * - When an SP is being destroyed, we change its state as DEAD, wait for 163 * - When an SP is being destroyed, we change its state as DEAD, wait for
164 * references to the SP to be released, and then deallocate the SP 164 * references to the SP to be released, and then deallocate the SP
165 * (see key_unlink_sp) 165 * (see key_unlink_sp)
166 * - Getting an SP 166 * - Getting an SP
167 * - Normally we get an SP from the key_spd.splist (see key_lookup_sp_byspidx) 167 * - Normally we get an SP from the key_spd.splist (see key_lookup_sp_byspidx)
168 * - Must iterate the list and increment the reference count of a found SP 168 * - Must iterate the list and increment the reference count of a found SP
169 * (by key_sp_ref) in a pserialize read section 169 * (by key_sp_ref) in a pserialize read section
170 * - We can gain another reference from a held SP only if we check its state 170 * - We can gain another reference from a held SP only if we check its state
171 * and take its reference in a pserialize read section 171 * and take its reference in a pserialize read section
172 * (see esp_output for example) 172 * (see esp_output for example)
173 * - We may get an SP from an SP cache. See below 173 * - We may get an SP from an SP cache. See below
174 * - A gotten SP must be released after use by KEY_SP_UNREF (key_sp_unref) 174 * - A gotten SP must be released after use by KEY_SP_UNREF (key_sp_unref)
175 * - Updating member variables of an SP 175 * - Updating member variables of an SP
176 * - Most member variables of an SP are immutable 176 * - Most member variables of an SP are immutable
177 * - Only sp->state and sp->lastused can be changed 177 * - Only sp->state and sp->lastused can be changed
178 * - sp->state of an SP is updated only when destroying it under key_spd.lock 178 * - sp->state of an SP is updated only when destroying it under key_spd.lock
179 * - SP caches 179 * - SP caches
180 * - SPs can be cached in PCBs 180 * - SPs can be cached in PCBs
181 * - The lifetime of the caches is controlled by the global generation counter 181 * - The lifetime of the caches is controlled by the global generation counter
182 * (ipsec_spdgen) 182 * (ipsec_spdgen)
183 * - The global counter value is stored when an SP is cached 183 * - The global counter value is stored when an SP is cached
184 * - If the stored value is different from the global counter then the cache 184 * - If the stored value is different from the global counter then the cache
185 * is considered invalidated 185 * is considered invalidated
186 * - The counter is incremented when an SP is being destroyed 186 * - The counter is incremented when an SP is being destroyed
187 * - So checking the generation and taking a reference to an SP should be 187 * - So checking the generation and taking a reference to an SP should be
188 * in a pserialize read section 188 * in a pserialize read section
189 * - Note that caching doesn't increment the reference counter of an SP 189 * - Note that caching doesn't increment the reference counter of an SP
190 * - SPs in sockets 190 * - SPs in sockets
191 * - Userland programs can set a policy to a socket by 191 * - Userland programs can set a policy to a socket by
192 * setsockopt(IP_IPSEC_POLICY) 192 * setsockopt(IP_IPSEC_POLICY)
193 * - Such policies (SPs) are set to a socket (PCB) and also inserted to 193 * - Such policies (SPs) are set to a socket (PCB) and also inserted to
194 * the key_spd.socksplist list (not the key_spd.splist) 194 * the key_spd.socksplist list (not the key_spd.splist)
195 * - Such a policy is destroyed when a corresponding socket is destroed, 195 * - Such a policy is destroyed when a corresponding socket is destroed,
196 * however, a socket can be destroyed in softint so we cannot destroy 196 * however, a socket can be destroyed in softint so we cannot destroy
197 * it directly instead we just mark it DEAD and delay the destruction 197 * it directly instead we just mark it DEAD and delay the destruction
198 * until GC by the timer 198 * until GC by the timer
199 */ 199 */
200/* 200/*
201 * Locking notes on SAD: 201 * Locking notes on SAD:
202 * - Data structures 202 * - Data structures
203 * - SAs are managed by the list called key_sad.sahlist and sav lists of sah 203 * - SAs are managed by the list called key_sad.sahlist and sav lists of sah
204 * entries 204 * entries
205 * - An sav is supposed to be an SA from a viewpoint of users 205 * - An sav is supposed to be an SA from a viewpoint of users
206 * - A sah has sav lists for each SA state 206 * - A sah has sav lists for each SA state
207 * - Multiple sahs with the same saidx can exist 207 * - Multiple sahs with the same saidx can exist
208 * - Only one entry has MATURE state and others should be DEAD 208 * - Only one entry has MATURE state and others should be DEAD
209 * - DEAD entries are just ignored from searching 209 * - DEAD entries are just ignored from searching
210 * - Modifications to the key_sad.sahlist and sah.savlist must be done with 210 * - Modifications to the key_sad.sahlist and sah.savlist must be done with
211 * holding key_sad.lock which is a adaptive mutex 211 * holding key_sad.lock which is a adaptive mutex
212 * - Read accesses to the key_sad.sahlist and sah.savlist must be in 212 * - Read accesses to the key_sad.sahlist and sah.savlist must be in
213 * pserialize(9) read sections 213 * pserialize(9) read sections
214 * - sah's lifetime is managed by localcount(9) 214 * - sah's lifetime is managed by localcount(9)
215 * - Getting an sah entry 215 * - Getting an sah entry
216 * - We get an sah from the key_sad.sahlist 216 * - We get an sah from the key_sad.sahlist
217 * - Must iterate the list and increment the reference count of a found sah 217 * - Must iterate the list and increment the reference count of a found sah
218 * (by key_sah_ref) in a pserialize read section 218 * (by key_sah_ref) in a pserialize read section
219 * - A gotten sah must be released after use by key_sah_unref 219 * - A gotten sah must be released after use by key_sah_unref
220 * - An sah is destroyed when its state become DEAD and no sav is 220 * - An sah is destroyed when its state become DEAD and no sav is
221 * listed to the sah 221 * listed to the sah
222 * - The destruction is done only in the timer (see key_timehandler_sad) 222 * - The destruction is done only in the timer (see key_timehandler_sad)
223 * - sav's lifetime is managed by localcount(9) 223 * - sav's lifetime is managed by localcount(9)
224 * - Getting an sav entry 224 * - Getting an sav entry
225 * - First get an sah by saidx and get an sav from either of sah's savlists 225 * - First get an sah by saidx and get an sav from either of sah's savlists
226 * - Must iterate the list and increment the reference count of a found sav 226 * - Must iterate the list and increment the reference count of a found sav
227 * (by key_sa_ref) in a pserialize read section 227 * (by key_sa_ref) in a pserialize read section
228 * - We can gain another reference from a held SA only if we check its state 228 * - We can gain another reference from a held SA only if we check its state
229 * and take its reference in a pserialize read section 229 * and take its reference in a pserialize read section
230 * (see esp_output for example) 230 * (see esp_output for example)
231 * - A gotten sav must be released after use by key_sa_unref 231 * - A gotten sav must be released after use by key_sa_unref
232 * - An sav is destroyed when its state become DEAD 232 * - An sav is destroyed when its state become DEAD
233 */ 233 */
234/* 234/*
235 * Locking notes on misc data: 235 * Locking notes on misc data:
236 * - All lists of key_misc are protected by key_misc.lock 236 * - All lists of key_misc are protected by key_misc.lock
237 * - key_misc.lock must be held even for read accesses 237 * - key_misc.lock must be held even for read accesses
238 */ 238 */
239 239
240/* SPD */ 240/* SPD */
241static struct { 241static struct {
242 kmutex_t lock; 242 kmutex_t lock;
243 kcondvar_t cv_lc; 243 kcondvar_t cv_lc;
244 struct pslist_head splist[IPSEC_DIR_MAX]; 244 struct pslist_head splist[IPSEC_DIR_MAX];
245 /* 245 /*
246 * The list has SPs that are set to a socket via 246 * The list has SPs that are set to a socket via
247 * setsockopt(IP_IPSEC_POLICY) from userland. See ipsec_set_policy. 247 * setsockopt(IP_IPSEC_POLICY) from userland. See ipsec_set_policy.
248 */ 248 */
249 struct pslist_head socksplist; 249 struct pslist_head socksplist;
250 250
251 pserialize_t psz; 251 pserialize_t psz;
252 kcondvar_t cv_psz; 252 kcondvar_t cv_psz;
253 bool psz_performing; 253 bool psz_performing;
254} key_spd __cacheline_aligned; 254} key_spd __cacheline_aligned;
255 255
256/* SAD */ 256/* SAD */
257static struct { 257static struct {
258 kmutex_t lock; 258 kmutex_t lock;
259 kcondvar_t cv_lc; 259 kcondvar_t cv_lc;
260 struct pslist_head sahlist; 260 struct pslist_head sahlist;
261 261
262 pserialize_t psz; 262 pserialize_t psz;
263 kcondvar_t cv_psz; 263 kcondvar_t cv_psz;
264 bool psz_performing; 264 bool psz_performing;
265} key_sad __cacheline_aligned; 265} key_sad __cacheline_aligned;
266 266
267/* Misc data */ 267/* Misc data */
268static struct { 268static struct {
269 kmutex_t lock; 269 kmutex_t lock;
270 /* registed list */ 270 /* registed list */
271 LIST_HEAD(_reglist, secreg) reglist[SADB_SATYPE_MAX + 1]; 271 LIST_HEAD(_reglist, secreg) reglist[SADB_SATYPE_MAX + 1];
272#ifndef IPSEC_NONBLOCK_ACQUIRE 272#ifndef IPSEC_NONBLOCK_ACQUIRE
273 /* acquiring list */ 273 /* acquiring list */
274 LIST_HEAD(_acqlist, secacq) acqlist; 274 LIST_HEAD(_acqlist, secacq) acqlist;
275#endif 275#endif
276#ifdef notyet 276#ifdef notyet
277 /* SP acquiring list */ 277 /* SP acquiring list */
278 LIST_HEAD(_spacqlist, secspacq) spacqlist; 278 LIST_HEAD(_spacqlist, secspacq) spacqlist;
279#endif 279#endif
280} key_misc __cacheline_aligned; 280} key_misc __cacheline_aligned;
281 281
282/* Macros for key_spd.splist */ 282/* Macros for key_spd.splist */
283#define SPLIST_ENTRY_INIT(sp) \ 283#define SPLIST_ENTRY_INIT(sp) \
284 PSLIST_ENTRY_INIT((sp), pslist_entry) 284 PSLIST_ENTRY_INIT((sp), pslist_entry)
285#define SPLIST_ENTRY_DESTROY(sp) \ 285#define SPLIST_ENTRY_DESTROY(sp) \
286 PSLIST_ENTRY_DESTROY((sp), pslist_entry) 286 PSLIST_ENTRY_DESTROY((sp), pslist_entry)
287#define SPLIST_WRITER_REMOVE(sp) \ 287#define SPLIST_WRITER_REMOVE(sp) \
288 PSLIST_WRITER_REMOVE((sp), pslist_entry) 288 PSLIST_WRITER_REMOVE((sp), pslist_entry)
289#define SPLIST_READER_EMPTY(dir) \ 289#define SPLIST_READER_EMPTY(dir) \
290 (PSLIST_READER_FIRST(&key_spd.splist[(dir)], struct secpolicy, \ 290 (PSLIST_READER_FIRST(&key_spd.splist[(dir)], struct secpolicy, \
291 pslist_entry) == NULL) 291 pslist_entry) == NULL)
292#define SPLIST_READER_FOREACH(sp, dir) \ 292#define SPLIST_READER_FOREACH(sp, dir) \
293 PSLIST_READER_FOREACH((sp), &key_spd.splist[(dir)], \ 293 PSLIST_READER_FOREACH((sp), &key_spd.splist[(dir)], \
294 struct secpolicy, pslist_entry) 294 struct secpolicy, pslist_entry)
295#define SPLIST_WRITER_FOREACH(sp, dir) \ 295#define SPLIST_WRITER_FOREACH(sp, dir) \
296 PSLIST_WRITER_FOREACH((sp), &key_spd.splist[(dir)], \ 296 PSLIST_WRITER_FOREACH((sp), &key_spd.splist[(dir)], \
297 struct secpolicy, pslist_entry) 297 struct secpolicy, pslist_entry)
298#define SPLIST_WRITER_INSERT_AFTER(sp, new) \ 298#define SPLIST_WRITER_INSERT_AFTER(sp, new) \
299 PSLIST_WRITER_INSERT_AFTER((sp), (new), pslist_entry) 299 PSLIST_WRITER_INSERT_AFTER((sp), (new), pslist_entry)
300#define SPLIST_WRITER_EMPTY(dir) \ 300#define SPLIST_WRITER_EMPTY(dir) \
301 (PSLIST_WRITER_FIRST(&key_spd.splist[(dir)], struct secpolicy, \ 301 (PSLIST_WRITER_FIRST(&key_spd.splist[(dir)], struct secpolicy, \
302 pslist_entry) == NULL) 302 pslist_entry) == NULL)
303#define SPLIST_WRITER_INSERT_HEAD(dir, sp) \ 303#define SPLIST_WRITER_INSERT_HEAD(dir, sp) \
304 PSLIST_WRITER_INSERT_HEAD(&key_spd.splist[(dir)], (sp), \ 304 PSLIST_WRITER_INSERT_HEAD(&key_spd.splist[(dir)], (sp), \
305 pslist_entry) 305 pslist_entry)
306#define SPLIST_WRITER_NEXT(sp) \ 306#define SPLIST_WRITER_NEXT(sp) \
307 PSLIST_WRITER_NEXT((sp), struct secpolicy, pslist_entry) 307 PSLIST_WRITER_NEXT((sp), struct secpolicy, pslist_entry)
308#define SPLIST_WRITER_INSERT_TAIL(dir, new) \ 308#define SPLIST_WRITER_INSERT_TAIL(dir, new) \
309 do { \ 309 do { \
310 if (SPLIST_WRITER_EMPTY((dir))) { \ 310 if (SPLIST_WRITER_EMPTY((dir))) { \
311 SPLIST_WRITER_INSERT_HEAD((dir), (new)); \ 311 SPLIST_WRITER_INSERT_HEAD((dir), (new)); \
312 } else { \ 312 } else { \
313 struct secpolicy *__sp; \ 313 struct secpolicy *__sp; \
314 SPLIST_WRITER_FOREACH(__sp, (dir)) { \ 314 SPLIST_WRITER_FOREACH(__sp, (dir)) { \
315 if (SPLIST_WRITER_NEXT(__sp) == NULL) { \ 315 if (SPLIST_WRITER_NEXT(__sp) == NULL) { \
316 SPLIST_WRITER_INSERT_AFTER(__sp,\ 316 SPLIST_WRITER_INSERT_AFTER(__sp,\
317 (new)); \ 317 (new)); \
318 break; \ 318 break; \
319 } \ 319 } \
320 } \ 320 } \
321 } \ 321 } \
322 } while (0) 322 } while (0)
323 323
324/* Macros for key_spd.socksplist */ 324/* Macros for key_spd.socksplist */
325#define SOCKSPLIST_WRITER_FOREACH(sp) \ 325#define SOCKSPLIST_WRITER_FOREACH(sp) \
326 PSLIST_WRITER_FOREACH((sp), &key_spd.socksplist, \ 326 PSLIST_WRITER_FOREACH((sp), &key_spd.socksplist, \
327 struct secpolicy, pslist_entry) 327 struct secpolicy, pslist_entry)
328#define SOCKSPLIST_READER_EMPTY() \ 328#define SOCKSPLIST_READER_EMPTY() \
329 (PSLIST_READER_FIRST(&key_spd.socksplist, struct secpolicy, \ 329 (PSLIST_READER_FIRST(&key_spd.socksplist, struct secpolicy, \
330 pslist_entry) == NULL) 330 pslist_entry) == NULL)
331 331
332/* Macros for key_sad.sahlist */ 332/* Macros for key_sad.sahlist */
333#define SAHLIST_ENTRY_INIT(sah) \ 333#define SAHLIST_ENTRY_INIT(sah) \
334 PSLIST_ENTRY_INIT((sah), pslist_entry) 334 PSLIST_ENTRY_INIT((sah), pslist_entry)
335#define SAHLIST_ENTRY_DESTROY(sah) \ 335#define SAHLIST_ENTRY_DESTROY(sah) \
336 PSLIST_ENTRY_DESTROY((sah), pslist_entry) 336 PSLIST_ENTRY_DESTROY((sah), pslist_entry)
337#define SAHLIST_WRITER_REMOVE(sah) \ 337#define SAHLIST_WRITER_REMOVE(sah) \
338 PSLIST_WRITER_REMOVE((sah), pslist_entry) 338 PSLIST_WRITER_REMOVE((sah), pslist_entry)
339#define SAHLIST_READER_FOREACH(sah) \ 339#define SAHLIST_READER_FOREACH(sah) \
340 PSLIST_READER_FOREACH((sah), &key_sad.sahlist, struct secashead,\ 340 PSLIST_READER_FOREACH((sah), &key_sad.sahlist, struct secashead,\
341 pslist_entry) 341 pslist_entry)
342#define SAHLIST_WRITER_FOREACH(sah) \ 342#define SAHLIST_WRITER_FOREACH(sah) \
343 PSLIST_WRITER_FOREACH((sah), &key_sad.sahlist, struct secashead,\ 343 PSLIST_WRITER_FOREACH((sah), &key_sad.sahlist, struct secashead,\
344 pslist_entry) 344 pslist_entry)
345#define SAHLIST_WRITER_INSERT_HEAD(sah) \ 345#define SAHLIST_WRITER_INSERT_HEAD(sah) \
346 PSLIST_WRITER_INSERT_HEAD(&key_sad.sahlist, (sah), pslist_entry) 346 PSLIST_WRITER_INSERT_HEAD(&key_sad.sahlist, (sah), pslist_entry)
347 347
348/* Macros for key_sad.sahlist#savlist */ 348/* Macros for key_sad.sahlist#savlist */
349#define SAVLIST_ENTRY_INIT(sav) \ 349#define SAVLIST_ENTRY_INIT(sav) \
350 PSLIST_ENTRY_INIT((sav), pslist_entry) 350 PSLIST_ENTRY_INIT((sav), pslist_entry)
351#define SAVLIST_ENTRY_DESTROY(sav) \ 351#define SAVLIST_ENTRY_DESTROY(sav) \
352 PSLIST_ENTRY_DESTROY((sav), pslist_entry) 352 PSLIST_ENTRY_DESTROY((sav), pslist_entry)
353#define SAVLIST_READER_FIRST(sah, state) \ 353#define SAVLIST_READER_FIRST(sah, state) \
354 PSLIST_READER_FIRST(&(sah)->savlist[(state)], struct secasvar, \ 354 PSLIST_READER_FIRST(&(sah)->savlist[(state)], struct secasvar, \
355 pslist_entry) 355 pslist_entry)
356#define SAVLIST_WRITER_REMOVE(sav) \ 356#define SAVLIST_WRITER_REMOVE(sav) \
357 PSLIST_WRITER_REMOVE((sav), pslist_entry) 357 PSLIST_WRITER_REMOVE((sav), pslist_entry)
358#define SAVLIST_READER_FOREACH(sav, sah, state) \ 358#define SAVLIST_READER_FOREACH(sav, sah, state) \
359 PSLIST_READER_FOREACH((sav), &(sah)->savlist[(state)], \ 359 PSLIST_READER_FOREACH((sav), &(sah)->savlist[(state)], \
360 struct secasvar, pslist_entry) 360 struct secasvar, pslist_entry)
361#define SAVLIST_WRITER_FOREACH(sav, sah, state) \ 361#define SAVLIST_WRITER_FOREACH(sav, sah, state) \
362 PSLIST_WRITER_FOREACH((sav), &(sah)->savlist[(state)], \ 362 PSLIST_WRITER_FOREACH((sav), &(sah)->savlist[(state)], \
363 struct secasvar, pslist_entry) 363 struct secasvar, pslist_entry)
364#define SAVLIST_WRITER_INSERT_BEFORE(sav, new) \ 364#define SAVLIST_WRITER_INSERT_BEFORE(sav, new) \
365 PSLIST_WRITER_INSERT_BEFORE((sav), (new), pslist_entry) 365 PSLIST_WRITER_INSERT_BEFORE((sav), (new), pslist_entry)
366#define SAVLIST_WRITER_INSERT_AFTER(sav, new) \ 366#define SAVLIST_WRITER_INSERT_AFTER(sav, new) \
367 PSLIST_WRITER_INSERT_AFTER((sav), (new), pslist_entry) 367 PSLIST_WRITER_INSERT_AFTER((sav), (new), pslist_entry)
368#define SAVLIST_WRITER_EMPTY(sah, state) \ 368#define SAVLIST_WRITER_EMPTY(sah, state) \
369 (PSLIST_WRITER_FIRST(&(sah)->savlist[(state)], struct secasvar, \ 369 (PSLIST_WRITER_FIRST(&(sah)->savlist[(state)], struct secasvar, \
370 pslist_entry) == NULL) 370 pslist_entry) == NULL)
371#define SAVLIST_WRITER_INSERT_HEAD(sah, state, sav) \ 371#define SAVLIST_WRITER_INSERT_HEAD(sah, state, sav) \
372 PSLIST_WRITER_INSERT_HEAD(&(sah)->savlist[(state)], (sav), \ 372 PSLIST_WRITER_INSERT_HEAD(&(sah)->savlist[(state)], (sav), \
373 pslist_entry) 373 pslist_entry)
374#define SAVLIST_WRITER_NEXT(sav) \ 374#define SAVLIST_WRITER_NEXT(sav) \
375 PSLIST_WRITER_NEXT((sav), struct secasvar, pslist_entry) 375 PSLIST_WRITER_NEXT((sav), struct secasvar, pslist_entry)
376#define SAVLIST_WRITER_INSERT_TAIL(sah, state, new) \ 376#define SAVLIST_WRITER_INSERT_TAIL(sah, state, new) \
377 do { \ 377 do { \
378 if (SAVLIST_WRITER_EMPTY((sah), (state))) { \ 378 if (SAVLIST_WRITER_EMPTY((sah), (state))) { \
379 SAVLIST_WRITER_INSERT_HEAD((sah), (state), (new));\ 379 SAVLIST_WRITER_INSERT_HEAD((sah), (state), (new));\
380 } else { \ 380 } else { \
381 struct secasvar *__sav; \ 381 struct secasvar *__sav; \
382 SAVLIST_WRITER_FOREACH(__sav, (sah), (state)) { \ 382 SAVLIST_WRITER_FOREACH(__sav, (sah), (state)) { \
383 if (SAVLIST_WRITER_NEXT(__sav) == NULL) {\ 383 if (SAVLIST_WRITER_NEXT(__sav) == NULL) {\
384 SAVLIST_WRITER_INSERT_AFTER(__sav,\ 384 SAVLIST_WRITER_INSERT_AFTER(__sav,\
385 (new)); \ 385 (new)); \
386 break; \ 386 break; \
387 } \ 387 } \
388 } \ 388 } \
389 } \ 389 } \
390 } while (0) 390 } while (0)
391#define SAVLIST_READER_NEXT(sav) \ 391#define SAVLIST_READER_NEXT(sav) \
392 PSLIST_READER_NEXT((sav), struct secasvar, pslist_entry) 392 PSLIST_READER_NEXT((sav), struct secasvar, pslist_entry)
393 393
394 394
395/* search order for SAs */ 395/* search order for SAs */
396 /* 396 /*
397 * This order is important because we must select the oldest SA 397 * This order is important because we must select the oldest SA
398 * for outbound processing. For inbound, This is not important. 398 * for outbound processing. For inbound, This is not important.
399 */ 399 */
400static const u_int saorder_state_valid_prefer_old[] = { 400static const u_int saorder_state_valid_prefer_old[] = {
401 SADB_SASTATE_DYING, SADB_SASTATE_MATURE, 401 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
402}; 402};
403static const u_int saorder_state_valid_prefer_new[] = { 403static const u_int saorder_state_valid_prefer_new[] = {
404 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, 404 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
405}; 405};
406 406
407static const u_int saorder_state_alive[] = { 407static const u_int saorder_state_alive[] = {
408 /* except DEAD */ 408 /* except DEAD */
409 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL 409 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
410}; 410};
411static const u_int saorder_state_any[] = { 411static const u_int saorder_state_any[] = {
412 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, 412 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
413 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD 413 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
414}; 414};
415 415
416#define SASTATE_ALIVE_FOREACH(s) \ 416#define SASTATE_ALIVE_FOREACH(s) \
417 for (int _i = 0; \ 417 for (int _i = 0; \
418 _i < __arraycount(saorder_state_alive) ? \ 418 _i < __arraycount(saorder_state_alive) ? \
419 (s) = saorder_state_alive[_i], true : false; \ 419 (s) = saorder_state_alive[_i], true : false; \
420 _i++) 420 _i++)
421#define SASTATE_ANY_FOREACH(s) \ 421#define SASTATE_ANY_FOREACH(s) \
422 for (int _i = 0; \ 422 for (int _i = 0; \
423 _i < __arraycount(saorder_state_any) ? \ 423 _i < __arraycount(saorder_state_any) ? \
424 (s) = saorder_state_any[_i], true : false; \ 424 (s) = saorder_state_any[_i], true : false; \
425 _i++) 425 _i++)
426 426
427static const int minsize[] = { 427static const int minsize[] = {
428 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */ 428 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
429 sizeof(struct sadb_sa), /* SADB_EXT_SA */ 429 sizeof(struct sadb_sa), /* SADB_EXT_SA */
430 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */ 430 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
431 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */ 431 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
432 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */ 432 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
433 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */ 433 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
434 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */ 434 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
435 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */ 435 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
436 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */ 436 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
437 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */ 437 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
438 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */ 438 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
439 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */ 439 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
440 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */ 440 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
441 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */ 441 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
442 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */ 442 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
443 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */ 443 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
444 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */ 444 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
445 0, /* SADB_X_EXT_KMPRIVATE */ 445 0, /* SADB_X_EXT_KMPRIVATE */
446 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */ 446 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
447 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */ 447 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
448 sizeof(struct sadb_x_nat_t_type), /* SADB_X_EXT_NAT_T_TYPE */ 448 sizeof(struct sadb_x_nat_t_type), /* SADB_X_EXT_NAT_T_TYPE */
449 sizeof(struct sadb_x_nat_t_port), /* SADB_X_EXT_NAT_T_SPORT */ 449 sizeof(struct sadb_x_nat_t_port), /* SADB_X_EXT_NAT_T_SPORT */
450 sizeof(struct sadb_x_nat_t_port), /* SADB_X_EXT_NAT_T_DPORT */ 450 sizeof(struct sadb_x_nat_t_port), /* SADB_X_EXT_NAT_T_DPORT */
451 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAI */ 451 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAI */
452 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAR */ 452 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAR */
453 sizeof(struct sadb_x_nat_t_frag), /* SADB_X_EXT_NAT_T_FRAG */ 453 sizeof(struct sadb_x_nat_t_frag), /* SADB_X_EXT_NAT_T_FRAG */
454}; 454};
455static const int maxsize[] = { 455static const int maxsize[] = {
456 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */ 456 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
457 sizeof(struct sadb_sa), /* SADB_EXT_SA */ 457 sizeof(struct sadb_sa), /* SADB_EXT_SA */
458 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */ 458 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
459 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */ 459 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
460 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */ 460 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
461 0, /* SADB_EXT_ADDRESS_SRC */ 461 0, /* SADB_EXT_ADDRESS_SRC */
462 0, /* SADB_EXT_ADDRESS_DST */ 462 0, /* SADB_EXT_ADDRESS_DST */
463 0, /* SADB_EXT_ADDRESS_PROXY */ 463 0, /* SADB_EXT_ADDRESS_PROXY */
464 0, /* SADB_EXT_KEY_AUTH */ 464 0, /* SADB_EXT_KEY_AUTH */
465 0, /* SADB_EXT_KEY_ENCRYPT */ 465 0, /* SADB_EXT_KEY_ENCRYPT */
466 0, /* SADB_EXT_IDENTITY_SRC */ 466 0, /* SADB_EXT_IDENTITY_SRC */
467 0, /* SADB_EXT_IDENTITY_DST */ 467 0, /* SADB_EXT_IDENTITY_DST */
468 0, /* SADB_EXT_SENSITIVITY */ 468 0, /* SADB_EXT_SENSITIVITY */
469 0, /* SADB_EXT_PROPOSAL */ 469 0, /* SADB_EXT_PROPOSAL */
470 0, /* SADB_EXT_SUPPORTED_AUTH */ 470 0, /* SADB_EXT_SUPPORTED_AUTH */
471 0, /* SADB_EXT_SUPPORTED_ENCRYPT */ 471 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
472 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */ 472 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
473 0, /* SADB_X_EXT_KMPRIVATE */ 473 0, /* SADB_X_EXT_KMPRIVATE */
474 0, /* SADB_X_EXT_POLICY */ 474 0, /* SADB_X_EXT_POLICY */
475 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */ 475 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
476 sizeof(struct sadb_x_nat_t_type), /* SADB_X_EXT_NAT_T_TYPE */ 476 sizeof(struct sadb_x_nat_t_type), /* SADB_X_EXT_NAT_T_TYPE */
477 sizeof(struct sadb_x_nat_t_port), /* SADB_X_EXT_NAT_T_SPORT */ 477 sizeof(struct sadb_x_nat_t_port), /* SADB_X_EXT_NAT_T_SPORT */
478 sizeof(struct sadb_x_nat_t_port), /* SADB_X_EXT_NAT_T_DPORT */ 478 sizeof(struct sadb_x_nat_t_port), /* SADB_X_EXT_NAT_T_DPORT */
479 0, /* SADB_X_EXT_NAT_T_OAI */ 479 0, /* SADB_X_EXT_NAT_T_OAI */
480 0, /* SADB_X_EXT_NAT_T_OAR */ 480 0, /* SADB_X_EXT_NAT_T_OAR */
481 sizeof(struct sadb_x_nat_t_frag), /* SADB_X_EXT_NAT_T_FRAG */ 481 sizeof(struct sadb_x_nat_t_frag), /* SADB_X_EXT_NAT_T_FRAG */
482}; 482};
483 483
484static int ipsec_esp_keymin = 256; 484static int ipsec_esp_keymin = 256;
485static int ipsec_esp_auth = 0; 485static int ipsec_esp_auth = 0;
486static int ipsec_ah_keymin = 128; 486static int ipsec_ah_keymin = 128;
487 487
488#ifdef SYSCTL_DECL 488#ifdef SYSCTL_DECL
489SYSCTL_DECL(_net_key); 489SYSCTL_DECL(_net_key);
490#endif 490#endif
491 491
492#ifdef SYSCTL_INT 492#ifdef SYSCTL_INT
493SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \ 493SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
494 &key_debug_level, 0, ""); 494 &key_debug_level, 0, "");
495 495
496/* max count of trial for the decision of spi value */ 496/* max count of trial for the decision of spi value */
497SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \ 497SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
498 &key_spi_trycnt, 0, ""); 498 &key_spi_trycnt, 0, "");
499 499
500/* minimum spi value to allocate automatically. */ 500/* minimum spi value to allocate automatically. */
501SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \ 501SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
502 &key_spi_minval, 0, ""); 502 &key_spi_minval, 0, "");
503 503
504/* maximun spi value to allocate automatically. */ 504/* maximun spi value to allocate automatically. */
505SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \ 505SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
506 &key_spi_maxval, 0, ""); 506 &key_spi_maxval, 0, "");
507 507
508/* interval to initialize randseed */ 508/* interval to initialize randseed */
509SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \ 509SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
510 &key_int_random, 0, ""); 510 &key_int_random, 0, "");
511 511
512/* lifetime for larval SA */ 512/* lifetime for larval SA */
513SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \ 513SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
514 &key_larval_lifetime, 0, ""); 514 &key_larval_lifetime, 0, "");
515 515
516/* counter for blocking to send SADB_ACQUIRE to IKEd */ 516/* counter for blocking to send SADB_ACQUIRE to IKEd */
517SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \ 517SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
518 &key_blockacq_count, 0, ""); 518 &key_blockacq_count, 0, "");
519 519
520/* lifetime for blocking to send SADB_ACQUIRE to IKEd */ 520/* lifetime for blocking to send SADB_ACQUIRE to IKEd */
521SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \ 521SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
522 &key_blockacq_lifetime, 0, ""); 522 &key_blockacq_lifetime, 0, "");
523 523
524/* ESP auth */ 524/* ESP auth */
525SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \ 525SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \
526 &ipsec_esp_auth, 0, ""); 526 &ipsec_esp_auth, 0, "");
527 527
528/* minimum ESP key length */ 528/* minimum ESP key length */
529SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \ 529SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \
530 &ipsec_esp_keymin, 0, ""); 530 &ipsec_esp_keymin, 0, "");
531 531
532/* minimum AH key length */ 532/* minimum AH key length */
533SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \ 533SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \
534 &ipsec_ah_keymin, 0, ""); 534 &ipsec_ah_keymin, 0, "");
535 535
536/* perfered old SA rather than new SA */ 536/* perfered old SA rather than new SA */
537SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW,\ 537SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW,\
538 &key_prefered_oldsa, 0, ""); 538 &key_prefered_oldsa, 0, "");
539#endif /* SYSCTL_INT */ 539#endif /* SYSCTL_INT */
540 540
541#define __LIST_CHAINED(elm) \ 541#define __LIST_CHAINED(elm) \
542 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL)) 542 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
543#define LIST_INSERT_TAIL(head, elm, type, field) \ 543#define LIST_INSERT_TAIL(head, elm, type, field) \
544do {\ 544do {\
545 struct type *curelm = LIST_FIRST(head); \ 545 struct type *curelm = LIST_FIRST(head); \
546 if (curelm == NULL) {\ 546 if (curelm == NULL) {\
547 LIST_INSERT_HEAD(head, elm, field); \ 547 LIST_INSERT_HEAD(head, elm, field); \
548 } else { \ 548 } else { \
549 while (LIST_NEXT(curelm, field)) \ 549 while (LIST_NEXT(curelm, field)) \
550 curelm = LIST_NEXT(curelm, field);\ 550 curelm = LIST_NEXT(curelm, field);\
551 LIST_INSERT_AFTER(curelm, elm, field);\ 551 LIST_INSERT_AFTER(curelm, elm, field);\
552 }\ 552 }\
553} while (0) 553} while (0)
554 554
555#define KEY_CHKSASTATE(head, sav) \ 555#define KEY_CHKSASTATE(head, sav) \
556/* do */ { \ 556/* do */ { \
557 if ((head) != (sav)) { \ 557 if ((head) != (sav)) { \
558 IPSECLOG(LOG_DEBUG, \ 558 IPSECLOG(LOG_DEBUG, \
559 "state mismatched (TREE=%d SA=%d)\n", \ 559 "state mismatched (TREE=%d SA=%d)\n", \
560 (head), (sav)); \ 560 (head), (sav)); \
561 continue; \ 561 continue; \
562 } \ 562 } \
563} /* while (0) */ 563} /* while (0) */
564 564
565#define KEY_CHKSPDIR(head, sp) \ 565#define KEY_CHKSPDIR(head, sp) \
566do { \ 566do { \
567 if ((head) != (sp)) { \ 567 if ((head) != (sp)) { \
568 IPSECLOG(LOG_DEBUG, \ 568 IPSECLOG(LOG_DEBUG, \
569 "direction mismatched (TREE=%d SP=%d), anyway continue.\n",\ 569 "direction mismatched (TREE=%d SP=%d), anyway continue.\n",\
570 (head), (sp)); \ 570 (head), (sp)); \
571 } \ 571 } \
572} while (0) 572} while (0)
573 573
574/* 574/*
575 * set parameters into secasindex buffer. 575 * set parameters into secasindex buffer.
576 * Must allocate secasindex buffer before calling this function. 576 * Must allocate secasindex buffer before calling this function.
577 */ 577 */
578static int 578static int
579key_setsecasidx(int, int, int, const struct sockaddr *, 579key_setsecasidx(int, int, int, const struct sockaddr *,
580 const struct sockaddr *, struct secasindex *); 580 const struct sockaddr *, struct secasindex *);
581 581
582/* key statistics */ 582/* key statistics */
583struct _keystat { 583struct _keystat {
584 u_long getspi_count; /* the avarage of count to try to get new SPI */ 584 u_long getspi_count; /* the avarage of count to try to get new SPI */
585} keystat; 585} keystat;
586 586
587struct sadb_msghdr { 587struct sadb_msghdr {
588 struct sadb_msg *msg; 588 struct sadb_msg *msg;
589 void *ext[SADB_EXT_MAX + 1]; 589 void *ext[SADB_EXT_MAX + 1];
590 int extoff[SADB_EXT_MAX + 1]; 590 int extoff[SADB_EXT_MAX + 1];
591 int extlen[SADB_EXT_MAX + 1]; 591 int extlen[SADB_EXT_MAX + 1];
592}; 592};
593 593
594static void 594static void
595key_init_spidx_bymsghdr(struct secpolicyindex *, const struct sadb_msghdr *); 595key_init_spidx_bymsghdr(struct secpolicyindex *, const struct sadb_msghdr *);
596 596
597static const struct sockaddr * 597static const struct sockaddr *
598key_msghdr_get_sockaddr(const struct sadb_msghdr *mhp, int idx) 598key_msghdr_get_sockaddr(const struct sadb_msghdr *mhp, int idx)
599{ 599{
600 600
601 return PFKEY_ADDR_SADDR(mhp->ext[idx]); 601 return PFKEY_ADDR_SADDR(mhp->ext[idx]);
602} 602}
603 603
604static void 604static void
605key_fill_replymsg(struct mbuf *m, int seq) 605key_fill_replymsg(struct mbuf *m, int seq)
606{ 606{
607 struct sadb_msg *msg; 607 struct sadb_msg *msg;
608 608
609 KASSERT(m->m_len >= sizeof(*msg)); 609 KASSERT(m->m_len >= sizeof(*msg));
610 610
611 msg = mtod(m, struct sadb_msg *); 611 msg = mtod(m, struct sadb_msg *);
612 msg->sadb_msg_errno = 0; 612 msg->sadb_msg_errno = 0;
613 msg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len); 613 msg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
614 if (seq != 0) 614 if (seq != 0)
615 msg->sadb_msg_seq = seq; 615 msg->sadb_msg_seq = seq;
616} 616}
617 617
618#if 0 618#if 0
619static void key_freeso(struct socket *); 619static void key_freeso(struct socket *);
620static void key_freesp_so(struct secpolicy **); 620static void key_freesp_so(struct secpolicy **);
621#endif 621#endif
622static struct secpolicy *key_getsp (const struct secpolicyindex *); 622static struct secpolicy *key_getsp (const struct secpolicyindex *);
623static struct secpolicy *key_getspbyid (u_int32_t); 623static struct secpolicy *key_getspbyid (u_int32_t);
624static struct secpolicy *key_lookup_and_remove_sp(const struct secpolicyindex *); 624static struct secpolicy *key_lookup_and_remove_sp(const struct secpolicyindex *);
625static struct secpolicy *key_lookupbyid_and_remove_sp(u_int32_t); 625static struct secpolicy *key_lookupbyid_and_remove_sp(u_int32_t);
626static void key_destroy_sp(struct secpolicy *); 626static void key_destroy_sp(struct secpolicy *);
627static u_int16_t key_newreqid (void); 627static u_int16_t key_newreqid (void);
628static struct mbuf *key_gather_mbuf (struct mbuf *, 628static struct mbuf *key_gather_mbuf (struct mbuf *,
629 const struct sadb_msghdr *, int, int, ...); 629 const struct sadb_msghdr *, int, int, ...);
630static int key_api_spdadd(struct socket *, struct mbuf *, 630static int key_api_spdadd(struct socket *, struct mbuf *,
631 const struct sadb_msghdr *); 631 const struct sadb_msghdr *);
632static u_int32_t key_getnewspid (void); 632static u_int32_t key_getnewspid (void);
633static int key_api_spddelete(struct socket *, struct mbuf *, 633static int key_api_spddelete(struct socket *, struct mbuf *,
634 const struct sadb_msghdr *); 634 const struct sadb_msghdr *);
635static int key_api_spddelete2(struct socket *, struct mbuf *, 635static int key_api_spddelete2(struct socket *, struct mbuf *,
636 const struct sadb_msghdr *); 636 const struct sadb_msghdr *);
637static int key_api_spdget(struct socket *, struct mbuf *, 637static int key_api_spdget(struct socket *, struct mbuf *,
638 const struct sadb_msghdr *); 638 const struct sadb_msghdr *);
639static int key_api_spdflush(struct socket *, struct mbuf *, 639static int key_api_spdflush(struct socket *, struct mbuf *,
640 const struct sadb_msghdr *); 640 const struct sadb_msghdr *);
641static int key_api_spddump(struct socket *, struct mbuf *, 641static int key_api_spddump(struct socket *, struct mbuf *,
642 const struct sadb_msghdr *); 642 const struct sadb_msghdr *);
643static struct mbuf * key_setspddump (int *errorp, pid_t); 643static struct mbuf * key_setspddump (int *errorp, pid_t);
644static struct mbuf * key_setspddump_chain (int *errorp, int *lenp, pid_t pid); 644static struct mbuf * key_setspddump_chain (int *errorp, int *lenp, pid_t pid);
645static int key_api_nat_map(struct socket *, struct mbuf *, 645static int key_api_nat_map(struct socket *, struct mbuf *,
646 const struct sadb_msghdr *); 646 const struct sadb_msghdr *);
647static struct mbuf *key_setdumpsp (struct secpolicy *, 647static struct mbuf *key_setdumpsp (struct secpolicy *,
648 u_int8_t, u_int32_t, pid_t); 648 u_int8_t, u_int32_t, pid_t);
649static u_int key_getspreqmsglen (const struct secpolicy *); 649static u_int key_getspreqmsglen (const struct secpolicy *);
650static int key_spdexpire (struct secpolicy *); 650static int key_spdexpire (struct secpolicy *);
651static struct secashead *key_newsah (const struct secasindex *); 651static struct secashead *key_newsah (const struct secasindex *);
652static void key_unlink_sah(struct secashead *); 652static void key_unlink_sah(struct secashead *);
653static void key_destroy_sah(struct secashead *); 653static void key_destroy_sah(struct secashead *);
654static bool key_sah_has_sav(struct secashead *); 654static bool key_sah_has_sav(struct secashead *);
655static void key_sah_ref(struct secashead *); 655static void key_sah_ref(struct secashead *);
656static void key_sah_unref(struct secashead *); 656static void key_sah_unref(struct secashead *);
657static void key_init_sav(struct secasvar *); 657static void key_init_sav(struct secasvar *);
658static void key_destroy_sav(struct secasvar *); 658static void key_destroy_sav(struct secasvar *);
659static void key_destroy_sav_with_ref(struct secasvar *); 659static void key_destroy_sav_with_ref(struct secasvar *);
660static struct secasvar *key_newsav(struct mbuf *, 660static struct secasvar *key_newsav(struct mbuf *,
661 const struct sadb_msghdr *, int *, const char*, int); 661 const struct sadb_msghdr *, int *, const char*, int);
662#define KEY_NEWSAV(m, sadb, e) \ 662#define KEY_NEWSAV(m, sadb, e) \
663 key_newsav(m, sadb, e, __func__, __LINE__) 663 key_newsav(m, sadb, e, __func__, __LINE__)
664static void key_delsav (struct secasvar *); 664static void key_delsav (struct secasvar *);
665static struct secashead *key_getsah(const struct secasindex *, int); 665static struct secashead *key_getsah(const struct secasindex *, int);
666static struct secashead *key_getsah_ref(const struct secasindex *, int); 666static struct secashead *key_getsah_ref(const struct secasindex *, int);
667static bool key_checkspidup(const struct secasindex *, u_int32_t); 667static bool key_checkspidup(const struct secasindex *, u_int32_t);
668static struct secasvar *key_getsavbyspi (struct secashead *, u_int32_t); 668static struct secasvar *key_getsavbyspi (struct secashead *, u_int32_t);
669static int key_setsaval (struct secasvar *, struct mbuf *, 669static int key_setsaval (struct secasvar *, struct mbuf *,
670 const struct sadb_msghdr *); 670 const struct sadb_msghdr *);
671static void key_freesaval(struct secasvar *); 671static void key_freesaval(struct secasvar *);
672static int key_init_xform(struct secasvar *); 672static int key_init_xform(struct secasvar *);
673static void key_clear_xform(struct secasvar *); 673static void key_clear_xform(struct secasvar *);
674static struct mbuf *key_setdumpsa (struct secasvar *, u_int8_t, 674static struct mbuf *key_setdumpsa (struct secasvar *, u_int8_t,
675 u_int8_t, u_int32_t, u_int32_t); 675 u_int8_t, u_int32_t, u_int32_t);
676static struct mbuf *key_setsadbxport (u_int16_t, u_int16_t); 676static struct mbuf *key_setsadbxport (u_int16_t, u_int16_t);
677static struct mbuf *key_setsadbxtype (u_int16_t); 677static struct mbuf *key_setsadbxtype (u_int16_t);
678static struct mbuf *key_setsadbxfrag (u_int16_t); 678static struct mbuf *key_setsadbxfrag (u_int16_t);
679static void key_porttosaddr (union sockaddr_union *, u_int16_t); 679static void key_porttosaddr (union sockaddr_union *, u_int16_t);
680static int key_checksalen (const union sockaddr_union *); 680static int key_checksalen (const union sockaddr_union *);
681static struct mbuf *key_setsadbmsg (u_int8_t, u_int16_t, u_int8_t, 681static struct mbuf *key_setsadbmsg (u_int8_t, u_int16_t, u_int8_t,
682 u_int32_t, pid_t, u_int16_t, int); 682 u_int32_t, pid_t, u_int16_t, int);
683static struct mbuf *key_setsadbsa (struct secasvar *); 683static struct mbuf *key_setsadbsa (struct secasvar *);
684static struct mbuf *key_setsadbaddr(u_int16_t, 684static struct mbuf *key_setsadbaddr(u_int16_t,
685 const struct sockaddr *, u_int8_t, u_int16_t, int); 685 const struct sockaddr *, u_int8_t, u_int16_t, int);
686#if 0 686#if 0
687static struct mbuf *key_setsadbident (u_int16_t, u_int16_t, void *, 687static struct mbuf *key_setsadbident (u_int16_t, u_int16_t, void *,
688 int, u_int64_t); 688 int, u_int64_t);
689#endif 689#endif
690static struct mbuf *key_setsadbxsa2 (u_int8_t, u_int32_t, u_int16_t); 690static struct mbuf *key_setsadbxsa2 (u_int8_t, u_int32_t, u_int16_t);
691static struct mbuf *key_setsadbxpolicy (u_int16_t, u_int8_t, 691static struct mbuf *key_setsadbxpolicy (u_int16_t, u_int8_t,
692 u_int32_t, int); 692 u_int32_t, int);
693static void *key_newbuf (const void *, u_int); 693static void *key_newbuf (const void *, u_int);
694#ifdef INET6 694#ifdef INET6
695static int key_ismyaddr6 (const struct sockaddr_in6 *); 695static int key_ismyaddr6 (const struct sockaddr_in6 *);
696#endif 696#endif
697 697
698static void sysctl_net_keyv2_setup(struct sysctllog **); 698static void sysctl_net_keyv2_setup(struct sysctllog **);
699static void sysctl_net_key_compat_setup(struct sysctllog **); 699static void sysctl_net_key_compat_setup(struct sysctllog **);
700 700
701/* flags for key_saidx_match() */ 701/* flags for key_saidx_match() */
702#define CMP_HEAD 1 /* protocol, addresses. */ 702#define CMP_HEAD 1 /* protocol, addresses. */
703#define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */ 703#define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
704#define CMP_REQID 3 /* additionally HEAD, reaid. */ 704#define CMP_REQID 3 /* additionally HEAD, reaid. */
705#define CMP_EXACTLY 4 /* all elements. */ 705#define CMP_EXACTLY 4 /* all elements. */
706static int key_saidx_match(const struct secasindex *, 706static int key_saidx_match(const struct secasindex *,
707 const struct secasindex *, int); 707 const struct secasindex *, int);
708 708
709static int key_sockaddr_match(const struct sockaddr *, 709static int key_sockaddr_match(const struct sockaddr *,
710 const struct sockaddr *, int); 710 const struct sockaddr *, int);
711static int key_bb_match_withmask(const void *, const void *, u_int); 711static int key_bb_match_withmask(const void *, const void *, u_int);
712static u_int16_t key_satype2proto (u_int8_t); 712static u_int16_t key_satype2proto (u_int8_t);
713static u_int8_t key_proto2satype (u_int16_t); 713static u_int8_t key_proto2satype (u_int16_t);
714 714
715static int key_spidx_match_exactly(const struct secpolicyindex *, 715static int key_spidx_match_exactly(const struct secpolicyindex *,
716 const struct secpolicyindex *); 716 const struct secpolicyindex *);
717static int key_spidx_match_withmask(const struct secpolicyindex *, 717static int key_spidx_match_withmask(const struct secpolicyindex *,
718 const struct secpolicyindex *); 718 const struct secpolicyindex *);
719 719
720static int key_api_getspi(struct socket *, struct mbuf *, 720static int key_api_getspi(struct socket *, struct mbuf *,
721 const struct sadb_msghdr *); 721 const struct sadb_msghdr *);
722static u_int32_t key_do_getnewspi (const struct sadb_spirange *, 722static u_int32_t key_do_getnewspi (const struct sadb_spirange *,
723 const struct secasindex *); 723 const struct secasindex *);
724static int key_handle_natt_info (struct secasvar *, 724static int key_handle_natt_info (struct secasvar *,
725 const struct sadb_msghdr *); 725 const struct sadb_msghdr *);
726static int key_set_natt_ports (union sockaddr_union *, 726static int key_set_natt_ports (union sockaddr_union *,
727 union sockaddr_union *, 727 union sockaddr_union *,
728 const struct sadb_msghdr *); 728 const struct sadb_msghdr *);
729static int key_api_update(struct socket *, struct mbuf *, 729static int key_api_update(struct socket *, struct mbuf *,
730 const struct sadb_msghdr *); 730 const struct sadb_msghdr *);
731#ifdef IPSEC_DOSEQCHECK 731#ifdef IPSEC_DOSEQCHECK
732static struct secasvar *key_getsavbyseq (struct secashead *, u_int32_t); 732static struct secasvar *key_getsavbyseq (struct secashead *, u_int32_t);
733#endif 733#endif
734static int key_api_add(struct socket *, struct mbuf *, 734static int key_api_add(struct socket *, struct mbuf *,
735 const struct sadb_msghdr *); 735 const struct sadb_msghdr *);
736static int key_setident (struct secashead *, struct mbuf *, 736static int key_setident (struct secashead *, struct mbuf *,
737 const struct sadb_msghdr *); 737 const struct sadb_msghdr *);
738static struct mbuf *key_getmsgbuf_x1 (struct mbuf *, 738static struct mbuf *key_getmsgbuf_x1 (struct mbuf *,
739 const struct sadb_msghdr *); 739 const struct sadb_msghdr *);
740static int key_api_delete(struct socket *, struct mbuf *, 740static int key_api_delete(struct socket *, struct mbuf *,
741 const struct sadb_msghdr *); 741 const struct sadb_msghdr *);
742static int key_api_get(struct socket *, struct mbuf *, 742static int key_api_get(struct socket *, struct mbuf *,
743 const struct sadb_msghdr *); 743 const struct sadb_msghdr *);
744 744
745static void key_getcomb_setlifetime (struct sadb_comb *); 745static void key_getcomb_setlifetime (struct sadb_comb *);
746static struct mbuf *key_getcomb_esp(int); 746static struct mbuf *key_getcomb_esp(int);
747static struct mbuf *key_getcomb_ah(int); 747static struct mbuf *key_getcomb_ah(int);
748static struct mbuf *key_getcomb_ipcomp(int); 748static struct mbuf *key_getcomb_ipcomp(int);
749static struct mbuf *key_getprop(const struct secasindex *, int); 749static struct mbuf *key_getprop(const struct secasindex *, int);
750 750
751static int key_acquire(const struct secasindex *, const struct secpolicy *, 751static int key_acquire(const struct secasindex *, const struct secpolicy *,
752 int); 752 int);
753static int key_acquire_sendup_mbuf_later(struct mbuf *); 753static int key_acquire_sendup_mbuf_later(struct mbuf *);
754static void key_acquire_sendup_pending_mbuf(void); 754static void key_acquire_sendup_pending_mbuf(void);
755#ifndef IPSEC_NONBLOCK_ACQUIRE 755#ifndef IPSEC_NONBLOCK_ACQUIRE
756static struct secacq *key_newacq (const struct secasindex *); 756static struct secacq *key_newacq (const struct secasindex *);
757static struct secacq *key_getacq (const struct secasindex *); 757static struct secacq *key_getacq (const struct secasindex *);
758static struct secacq *key_getacqbyseq (u_int32_t); 758static struct secacq *key_getacqbyseq (u_int32_t);
759#endif 759#endif
760#ifdef notyet 760#ifdef notyet
761static struct secspacq *key_newspacq (const struct secpolicyindex *); 761static struct secspacq *key_newspacq (const struct secpolicyindex *);
762static struct secspacq *key_getspacq (const struct secpolicyindex *); 762static struct secspacq *key_getspacq (const struct secpolicyindex *);
763#endif 763#endif
764static int key_api_acquire(struct socket *, struct mbuf *, 764static int key_api_acquire(struct socket *, struct mbuf *,
765 const struct sadb_msghdr *); 765 const struct sadb_msghdr *);
766static int key_api_register(struct socket *, struct mbuf *, 766static int key_api_register(struct socket *, struct mbuf *,
767 const struct sadb_msghdr *); 767 const struct sadb_msghdr *);
768static int key_expire (struct secasvar *); 768static int key_expire (struct secasvar *);
769static int key_api_flush(struct socket *, struct mbuf *, 769static int key_api_flush(struct socket *, struct mbuf *,
770 const struct sadb_msghdr *); 770 const struct sadb_msghdr *);
771static struct mbuf *key_setdump_chain (u_int8_t req_satype, int *errorp, 771static struct mbuf *key_setdump_chain (u_int8_t req_satype, int *errorp,
772 int *lenp, pid_t pid); 772 int *lenp, pid_t pid);
773static int key_api_dump(struct socket *, struct mbuf *, 773static int key_api_dump(struct socket *, struct mbuf *,
774 const struct sadb_msghdr *); 774 const struct sadb_msghdr *);
775static int key_api_promisc(struct socket *, struct mbuf *, 775static int key_api_promisc(struct socket *, struct mbuf *,
776 const struct sadb_msghdr *); 776 const struct sadb_msghdr *);
777static int key_senderror (struct socket *, struct mbuf *, int); 777static int key_senderror (struct socket *, struct mbuf *, int);
778static int key_validate_ext (const struct sadb_ext *, int); 778static int key_validate_ext (const struct sadb_ext *, int);
779static int key_align (struct mbuf *, struct sadb_msghdr *); 779static int key_align (struct mbuf *, struct sadb_msghdr *);
780#if 0 780#if 0
781static const char *key_getfqdn (void); 781static const char *key_getfqdn (void);
782static const char *key_getuserfqdn (void); 782static const char *key_getuserfqdn (void);
783#endif 783#endif
784static void key_sa_chgstate (struct secasvar *, u_int8_t); 784static void key_sa_chgstate (struct secasvar *, u_int8_t);
785 785
786static struct mbuf *key_alloc_mbuf(int, int); 786static struct mbuf *key_alloc_mbuf(int, int);
787static struct mbuf *key_alloc_mbuf_simple(int, int); 787static struct mbuf *key_alloc_mbuf_simple(int, int);
788 788
789static void key_timehandler(void *); 789static void key_timehandler(void *);
790static void key_timehandler_work(struct work *, void *); 790static void key_timehandler_work(struct work *, void *);
791static struct callout key_timehandler_ch; 791static struct callout key_timehandler_ch;
792static struct workqueue *key_timehandler_wq; 792static struct workqueue *key_timehandler_wq;
793static struct work key_timehandler_wk; 793static struct work key_timehandler_wk;
794 794
795u_int 795u_int
796key_sp_refcnt(const struct secpolicy *sp) 796key_sp_refcnt(const struct secpolicy *sp)
797{ 797{
798 798
799 /* FIXME */ 799 /* FIXME */
800 return 0; 800 return 0;
801} 801}
802 802
803static void 803static void
804key_spd_pserialize_perform(void) 804key_spd_pserialize_perform(void)
805{ 805{
806 806
807 KASSERT(mutex_owned(&key_spd.lock)); 807 KASSERT(mutex_owned(&key_spd.lock));
808 808
809 while (key_spd.psz_performing) 809 while (key_spd.psz_performing)
810 cv_wait(&key_spd.cv_psz, &key_spd.lock); 810 cv_wait(&key_spd.cv_psz, &key_spd.lock);
811 key_spd.psz_performing = true; 811 key_spd.psz_performing = true;
812 mutex_exit(&key_spd.lock); 812 mutex_exit(&key_spd.lock);
813 813
814 pserialize_perform(key_spd.psz); 814 pserialize_perform(key_spd.psz);
815 815
816 mutex_enter(&key_spd.lock); 816 mutex_enter(&key_spd.lock);
817 key_spd.psz_performing = false; 817 key_spd.psz_performing = false;
818 cv_broadcast(&key_spd.cv_psz); 818 cv_broadcast(&key_spd.cv_psz);
819} 819}
820 820
821/* 821/*
822 * Remove the sp from the key_spd.splist and wait for references to the sp 822 * Remove the sp from the key_spd.splist and wait for references to the sp
823 * to be released. key_spd.lock must be held. 823 * to be released. key_spd.lock must be held.
824 */ 824 */
825static void 825static void
826key_unlink_sp(struct secpolicy *sp) 826key_unlink_sp(struct secpolicy *sp)
827{ 827{
828 828
829 KASSERT(mutex_owned(&key_spd.lock)); 829 KASSERT(mutex_owned(&key_spd.lock));
830 830
831 sp->state = IPSEC_SPSTATE_DEAD; 831 sp->state = IPSEC_SPSTATE_DEAD;
832 SPLIST_WRITER_REMOVE(sp); 832 SPLIST_WRITER_REMOVE(sp);
833 833
834 /* Invalidate all cached SPD pointers in the PCBs. */ 834 /* Invalidate all cached SPD pointers in the PCBs. */
835 ipsec_invalpcbcacheall(); 835 ipsec_invalpcbcacheall();
836 836
837 KDASSERT(mutex_ownable(softnet_lock)); 837 KDASSERT(mutex_ownable(softnet_lock));
838 key_spd_pserialize_perform(); 838 key_spd_pserialize_perform();
839 839
840 localcount_drain(&sp->localcount, &key_spd.cv_lc, &key_spd.lock); 840 localcount_drain(&sp->localcount, &key_spd.cv_lc, &key_spd.lock);
841} 841}
842 842
843/* 843/*
844 * Return 0 when there are known to be no SP's for the specified 844 * Return 0 when there are known to be no SP's for the specified
845 * direction. Otherwise return 1. This is used by IPsec code 845 * direction. Otherwise return 1. This is used by IPsec code
846 * to optimize performance. 846 * to optimize performance.
847 */ 847 */
848int 848int
849key_havesp(u_int dir) 849key_havesp(u_int dir)
850{ 850{
851 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ? 851 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
852 !SPLIST_READER_EMPTY(dir) : 1); 852 !SPLIST_READER_EMPTY(dir) : 1);
853} 853}
854 854
855/* %%% IPsec policy management */ 855/* %%% IPsec policy management */
856/* 856/*
857 * allocating a SP for OUTBOUND or INBOUND packet. 857 * allocating a SP for OUTBOUND or INBOUND packet.
858 * Must call key_freesp() later. 858 * Must call key_freesp() later.
859 * OUT: NULL: not found 859 * OUT: NULL: not found
860 * others: found and return the pointer. 860 * others: found and return the pointer.
861 */ 861 */
862struct secpolicy * 862struct secpolicy *
863key_lookup_sp_byspidx(const struct secpolicyindex *spidx, 863key_lookup_sp_byspidx(const struct secpolicyindex *spidx,
864 u_int dir, const char* where, int tag) 864 u_int dir, const char* where, int tag)
865{ 865{
866 struct secpolicy *sp; 866 struct secpolicy *sp;
867 int s; 867 int s;
868 868
869 KASSERT(spidx != NULL); 869 KASSERT(spidx != NULL);
870 KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir); 870 KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
871 871
872 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag); 872 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag);
873 873
874 /* get a SP entry */ 874 /* get a SP entry */
875 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) { 875 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
876 kdebug_secpolicyindex("objects", spidx); 876 kdebug_secpolicyindex("objects", spidx);
877 } 877 }
878 878
879 s = pserialize_read_enter(); 879 s = pserialize_read_enter();
880 SPLIST_READER_FOREACH(sp, dir) { 880 SPLIST_READER_FOREACH(sp, dir) {
881 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) { 881 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
882 kdebug_secpolicyindex("in SPD", &sp->spidx); 882 kdebug_secpolicyindex("in SPD", &sp->spidx);
883 } 883 }
884 884
885 if (sp->state == IPSEC_SPSTATE_DEAD) 885 if (sp->state == IPSEC_SPSTATE_DEAD)
886 continue; 886 continue;
887 if (key_spidx_match_withmask(&sp->spidx, spidx)) 887 if (key_spidx_match_withmask(&sp->spidx, spidx))
888 goto found; 888 goto found;
889 } 889 }
890 sp = NULL; 890 sp = NULL;
891found: 891found:
892 if (sp) { 892 if (sp) {
893 /* sanity check */ 893 /* sanity check */
894 KEY_CHKSPDIR(sp->spidx.dir, dir); 894 KEY_CHKSPDIR(sp->spidx.dir, dir);
895 895
896 /* found a SPD entry */ 896 /* found a SPD entry */
897 sp->lastused = time_uptime; 897 sp->lastused = time_uptime;
898 key_sp_ref(sp, where, tag); 898 key_sp_ref(sp, where, tag);
899 } 899 }
900 pserialize_read_exit(s); 900 pserialize_read_exit(s);
901 901
902 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 902 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
903 "DP return SP:%p (ID=%u) refcnt %u\n", 903 "DP return SP:%p (ID=%u) refcnt %u\n",
904 sp, sp ? sp->id : 0, key_sp_refcnt(sp)); 904 sp, sp ? sp->id : 0, key_sp_refcnt(sp));
905 return sp; 905 return sp;
906} 906}
907 907
908/* 908/*
909 * return a policy that matches this particular inbound packet. 909 * return a policy that matches this particular inbound packet.
910 * XXX slow 910 * XXX slow
911 */ 911 */
912struct secpolicy * 912struct secpolicy *
913key_gettunnel(const struct sockaddr *osrc, 913key_gettunnel(const struct sockaddr *osrc,
914 const struct sockaddr *odst, 914 const struct sockaddr *odst,
915 const struct sockaddr *isrc, 915 const struct sockaddr *isrc,
916 const struct sockaddr *idst, 916 const struct sockaddr *idst,
917 const char* where, int tag) 917 const char* where, int tag)
918{ 918{
919 struct secpolicy *sp; 919 struct secpolicy *sp;
920 const int dir = IPSEC_DIR_INBOUND; 920 const int dir = IPSEC_DIR_INBOUND;
921 int s; 921 int s;
922 struct ipsecrequest *r1, *r2, *p; 922 struct ipsecrequest *r1, *r2, *p;
923 struct secpolicyindex spidx; 923 struct secpolicyindex spidx;
924 924
925 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag); 925 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag);
926 926
927 if (isrc->sa_family != idst->sa_family) { 927 if (isrc->sa_family != idst->sa_family) {
928 IPSECLOG(LOG_ERR, "protocol family mismatched %d != %d\n.", 928 IPSECLOG(LOG_ERR, "protocol family mismatched %d != %d\n.",
929 isrc->sa_family, idst->sa_family); 929 isrc->sa_family, idst->sa_family);
930 sp = NULL; 930 sp = NULL;
931 goto done; 931 goto done;
932 } 932 }
933 933
934 s = pserialize_read_enter(); 934 s = pserialize_read_enter();
935 SPLIST_READER_FOREACH(sp, dir) { 935 SPLIST_READER_FOREACH(sp, dir) {
936 if (sp->state == IPSEC_SPSTATE_DEAD) 936 if (sp->state == IPSEC_SPSTATE_DEAD)
937 continue; 937 continue;
938 938
939 r1 = r2 = NULL; 939 r1 = r2 = NULL;
940 for (p = sp->req; p; p = p->next) { 940 for (p = sp->req; p; p = p->next) {
941 if (p->saidx.mode != IPSEC_MODE_TUNNEL) 941 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
942 continue; 942 continue;
943 943
944 r1 = r2; 944 r1 = r2;
945 r2 = p; 945 r2 = p;
946 946
947 if (!r1) { 947 if (!r1) {
948 /* here we look at address matches only */ 948 /* here we look at address matches only */
949 spidx = sp->spidx; 949 spidx = sp->spidx;
950 if (isrc->sa_len > sizeof(spidx.src) || 950 if (isrc->sa_len > sizeof(spidx.src) ||
951 idst->sa_len > sizeof(spidx.dst)) 951 idst->sa_len > sizeof(spidx.dst))
952 continue; 952 continue;
953 memcpy(&spidx.src, isrc, isrc->sa_len); 953 memcpy(&spidx.src, isrc, isrc->sa_len);
954 memcpy(&spidx.dst, idst, idst->sa_len); 954 memcpy(&spidx.dst, idst, idst->sa_len);
955 if (!key_spidx_match_withmask(&sp->spidx, &spidx)) 955 if (!key_spidx_match_withmask(&sp->spidx, &spidx))
956 continue; 956 continue;
957 } else { 957 } else {
958 if (!key_sockaddr_match(&r1->saidx.src.sa, isrc, PORT_NONE) || 958 if (!key_sockaddr_match(&r1->saidx.src.sa, isrc, PORT_NONE) ||
959 !key_sockaddr_match(&r1->saidx.dst.sa, idst, PORT_NONE)) 959 !key_sockaddr_match(&r1->saidx.dst.sa, idst, PORT_NONE))
960 continue; 960 continue;
961 } 961 }
962 962
963 if (!key_sockaddr_match(&r2->saidx.src.sa, osrc, PORT_NONE) || 963 if (!key_sockaddr_match(&r2->saidx.src.sa, osrc, PORT_NONE) ||
964 !key_sockaddr_match(&r2->saidx.dst.sa, odst, PORT_NONE)) 964 !key_sockaddr_match(&r2->saidx.dst.sa, odst, PORT_NONE))
965 continue; 965 continue;
966 966
967 goto found; 967 goto found;
968 } 968 }
969 } 969 }
970 sp = NULL; 970 sp = NULL;
971found: 971found:
972 if (sp) { 972 if (sp) {
973 sp->lastused = time_uptime; 973 sp->lastused = time_uptime;
974 key_sp_ref(sp, where, tag); 974 key_sp_ref(sp, where, tag);
975 } 975 }
976 pserialize_read_exit(s); 976 pserialize_read_exit(s);
977done: 977done:
978 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 978 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
979 "DP return SP:%p (ID=%u) refcnt %u\n", 979 "DP return SP:%p (ID=%u) refcnt %u\n",
980 sp, sp ? sp->id : 0, key_sp_refcnt(sp)); 980 sp, sp ? sp->id : 0, key_sp_refcnt(sp));
981 return sp; 981 return sp;
982} 982}
983 983
984/* 984/*
985 * allocating an SA entry for an *OUTBOUND* packet. 985 * allocating an SA entry for an *OUTBOUND* packet.
986 * checking each request entries in SP, and acquire an SA if need. 986 * checking each request entries in SP, and acquire an SA if need.
987 * OUT: 0: there are valid requests. 987 * OUT: 0: there are valid requests.
988 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring. 988 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
989 */ 989 */
990int 990int
991key_checkrequest(const struct ipsecrequest *isr, const struct secasindex *saidx, 991key_checkrequest(const struct ipsecrequest *isr, const struct secasindex *saidx,
992 struct secasvar **ret) 992 struct secasvar **ret)
993{ 993{
994 u_int level; 994 u_int level;
995 int error; 995 int error;
996 struct secasvar *sav; 996 struct secasvar *sav;
997 997
998 KASSERT(isr != NULL); 998 KASSERT(isr != NULL);
999 KASSERTMSG(saidx->mode == IPSEC_MODE_TRANSPORT || 999 KASSERTMSG(saidx->mode == IPSEC_MODE_TRANSPORT ||
1000 saidx->mode == IPSEC_MODE_TUNNEL, 1000 saidx->mode == IPSEC_MODE_TUNNEL,
1001 "unexpected policy %u", saidx->mode); 1001 "unexpected policy %u", saidx->mode);
1002 1002
1003 /* get current level */ 1003 /* get current level */
1004 level = ipsec_get_reqlevel(isr); 1004 level = ipsec_get_reqlevel(isr);
1005 1005
1006 /* 1006 /*
1007 * XXX guard against protocol callbacks from the crypto 1007 * XXX guard against protocol callbacks from the crypto
1008 * thread as they reference ipsecrequest.sav which we 1008 * thread as they reference ipsecrequest.sav which we
1009 * temporarily null out below. Need to rethink how we 1009 * temporarily null out below. Need to rethink how we
1010 * handle bundled SA's in the callback thread. 1010 * handle bundled SA's in the callback thread.
1011 */ 1011 */
1012 IPSEC_SPLASSERT_SOFTNET("key_checkrequest"); 1012 IPSEC_SPLASSERT_SOFTNET("key_checkrequest");
1013 1013
1014 sav = key_lookup_sa_bysaidx(saidx); 1014 sav = key_lookup_sa_bysaidx(saidx);
1015 if (sav != NULL) { 1015 if (sav != NULL) {
1016 *ret = sav; 1016 *ret = sav;
1017 return 0; 1017 return 0;
1018 } 1018 }
1019 1019
1020 /* there is no SA */ 1020 /* there is no SA */
1021 error = key_acquire(saidx, isr->sp, M_NOWAIT); 1021 error = key_acquire(saidx, isr->sp, M_NOWAIT);
1022 if (error != 0) { 1022 if (error != 0) {
1023 /* XXX What should I do ? */ 1023 /* XXX What should I do ? */
1024 IPSECLOG(LOG_DEBUG, "error %d returned from key_acquire.\n", 1024 IPSECLOG(LOG_DEBUG, "error %d returned from key_acquire.\n",
1025 error); 1025 error);
1026 return error; 1026 return error;
1027 } 1027 }
1028 1028
1029 if (level != IPSEC_LEVEL_REQUIRE) { 1029 if (level != IPSEC_LEVEL_REQUIRE) {
1030 /* XXX sigh, the interface to this routine is botched */ 1030 /* XXX sigh, the interface to this routine is botched */
1031 *ret = NULL; 1031 *ret = NULL;
1032 return 0; 1032 return 0;
1033 } else { 1033 } else {
1034 return ENOENT; 1034 return ENOENT;
@@ -1116,2161 +1116,2169 @@ key_sendup_message_delete(struct secasva @@ -1116,2161 +1116,2169 @@ key_sendup_message_delete(struct secasva
1116 result = m; 1116 result = m;
1117 1117
1118 /* set sadb_address for saidx's. */ 1118 /* set sadb_address for saidx's. */
1119 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &sav->sah->saidx.src.sa, 1119 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &sav->sah->saidx.src.sa,
1120 sav->sah->saidx.src.sa.sa_len << 3, IPSEC_ULPROTO_ANY); 1120 sav->sah->saidx.src.sa.sa_len << 3, IPSEC_ULPROTO_ANY);
1121 if (m == NULL) 1121 if (m == NULL)
1122 goto msgfail; 1122 goto msgfail;
1123 m_cat(result, m); 1123 m_cat(result, m);
1124 1124
1125 /* set sadb_address for saidx's. */ 1125 /* set sadb_address for saidx's. */
1126 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &sav->sah->saidx.src.sa, 1126 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &sav->sah->saidx.src.sa,
1127 sav->sah->saidx.src.sa.sa_len << 3, IPSEC_ULPROTO_ANY); 1127 sav->sah->saidx.src.sa.sa_len << 3, IPSEC_ULPROTO_ANY);
1128 if (m == NULL) 1128 if (m == NULL)
1129 goto msgfail; 1129 goto msgfail;
1130 m_cat(result, m); 1130 m_cat(result, m);
1131 1131
1132 /* create SA extension */ 1132 /* create SA extension */
1133 m = key_setsadbsa(sav); 1133 m = key_setsadbsa(sav);
1134 if (m == NULL) 1134 if (m == NULL)
1135 goto msgfail; 1135 goto msgfail;
1136 m_cat(result, m); 1136 m_cat(result, m);
1137 1137
1138 if (result->m_len < sizeof(struct sadb_msg)) { 1138 if (result->m_len < sizeof(struct sadb_msg)) {
1139 result = m_pullup(result, sizeof(struct sadb_msg)); 1139 result = m_pullup(result, sizeof(struct sadb_msg));
1140 if (result == NULL) 1140 if (result == NULL)
1141 goto msgfail; 1141 goto msgfail;
1142 } 1142 }
1143 1143
1144 result->m_pkthdr.len = 0; 1144 result->m_pkthdr.len = 0;
1145 for (m = result; m; m = m->m_next) 1145 for (m = result; m; m = m->m_next)
1146 result->m_pkthdr.len += m->m_len; 1146 result->m_pkthdr.len += m->m_len;
1147 mtod(result, struct sadb_msg *)->sadb_msg_len = 1147 mtod(result, struct sadb_msg *)->sadb_msg_len =
1148 PFKEY_UNIT64(result->m_pkthdr.len); 1148 PFKEY_UNIT64(result->m_pkthdr.len);
1149 1149
1150 key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED); 1150 key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
1151 result = NULL; 1151 result = NULL;
1152msgfail: 1152msgfail:
1153 if (result) 1153 if (result)
1154 m_freem(result); 1154 m_freem(result);
1155} 1155}
1156#endif 1156#endif
1157 1157
1158/* 1158/*
1159 * allocating a usable SA entry for a *INBOUND* packet. 1159 * allocating a usable SA entry for a *INBOUND* packet.
1160 * Must call key_freesav() later. 1160 * Must call key_freesav() later.
1161 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state). 1161 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
1162 * NULL: not found, or error occurred. 1162 * NULL: not found, or error occurred.
1163 * 1163 *
1164 * In the comparison, no source address is used--for RFC2401 conformance. 1164 * In the comparison, no source address is used--for RFC2401 conformance.
1165 * To quote, from section 4.1: 1165 * To quote, from section 4.1:
1166 * A security association is uniquely identified by a triple consisting 1166 * A security association is uniquely identified by a triple consisting
1167 * of a Security Parameter Index (SPI), an IP Destination Address, and a 1167 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1168 * security protocol (AH or ESP) identifier. 1168 * security protocol (AH or ESP) identifier.
1169 * Note that, however, we do need to keep source address in IPsec SA. 1169 * Note that, however, we do need to keep source address in IPsec SA.
1170 * IKE specification and PF_KEY specification do assume that we 1170 * IKE specification and PF_KEY specification do assume that we
1171 * keep source address in IPsec SA. We see a tricky situation here. 1171 * keep source address in IPsec SA. We see a tricky situation here.
1172 * 1172 *
1173 * sport and dport are used for NAT-T. network order is always used. 1173 * sport and dport are used for NAT-T. network order is always used.
1174 */ 1174 */
1175struct secasvar * 1175struct secasvar *
1176key_lookup_sa( 1176key_lookup_sa(
1177 const union sockaddr_union *dst, 1177 const union sockaddr_union *dst,
1178 u_int proto, 1178 u_int proto,
1179 u_int32_t spi, 1179 u_int32_t spi,
1180 u_int16_t sport, 1180 u_int16_t sport,
1181 u_int16_t dport, 1181 u_int16_t dport,
1182 const char* where, int tag) 1182 const char* where, int tag)
1183{ 1183{
1184 struct secashead *sah; 1184 struct secashead *sah;
1185 struct secasvar *sav; 1185 struct secasvar *sav;
1186 u_int stateidx, state; 1186 u_int stateidx, state;
1187 const u_int *saorder_state_valid; 1187 const u_int *saorder_state_valid;
1188 int arraysize, chkport; 1188 int arraysize, chkport;
1189 int s; 1189 int s;
1190 1190
1191 int must_check_spi = 1; 1191 int must_check_spi = 1;
1192 int must_check_alg = 0; 1192 int must_check_alg = 0;
1193 u_int16_t cpi = 0; 1193 u_int16_t cpi = 0;
1194 u_int8_t algo = 0; 1194 u_int8_t algo = 0;
1195 1195
1196 if ((sport != 0) && (dport != 0)) 1196 if ((sport != 0) && (dport != 0))
1197 chkport = PORT_STRICT; 1197 chkport = PORT_STRICT;
1198 else 1198 else
1199 chkport = PORT_NONE; 1199 chkport = PORT_NONE;
1200 1200
1201 KASSERT(dst != NULL); 1201 KASSERT(dst != NULL);
1202 1202
1203 /* 1203 /*
1204 * XXX IPCOMP case 1204 * XXX IPCOMP case
1205 * We use cpi to define spi here. In the case where cpi <= 1205 * We use cpi to define spi here. In the case where cpi <=
1206 * IPCOMP_CPI_NEGOTIATE_MIN, cpi just define the algorithm used, not 1206 * IPCOMP_CPI_NEGOTIATE_MIN, cpi just define the algorithm used, not
1207 * the real spi. In this case, don't check the spi but check the 1207 * the real spi. In this case, don't check the spi but check the
1208 * algorithm 1208 * algorithm
1209 */ 1209 */
1210 1210
1211 if (proto == IPPROTO_IPCOMP) { 1211 if (proto == IPPROTO_IPCOMP) {
1212 u_int32_t tmp; 1212 u_int32_t tmp;
1213 tmp = ntohl(spi); 1213 tmp = ntohl(spi);
1214 cpi = (u_int16_t) tmp; 1214 cpi = (u_int16_t) tmp;
1215 if (cpi < IPCOMP_CPI_NEGOTIATE_MIN) { 1215 if (cpi < IPCOMP_CPI_NEGOTIATE_MIN) {
1216 algo = (u_int8_t) cpi; 1216 algo = (u_int8_t) cpi;
1217 must_check_spi = 0; 1217 must_check_spi = 0;
1218 must_check_alg = 1; 1218 must_check_alg = 1;
1219 } 1219 }
1220 } 1220 }
1221 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 1221 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1222 "DP from %s:%u check_spi=%d, check_alg=%d\n", 1222 "DP from %s:%u check_spi=%d, check_alg=%d\n",
1223 where, tag, must_check_spi, must_check_alg); 1223 where, tag, must_check_spi, must_check_alg);
1224 1224
1225 1225
1226 /* 1226 /*
1227 * searching SAD. 1227 * searching SAD.
1228 * XXX: to be checked internal IP header somewhere. Also when 1228 * XXX: to be checked internal IP header somewhere. Also when
1229 * IPsec tunnel packet is received. But ESP tunnel mode is 1229 * IPsec tunnel packet is received. But ESP tunnel mode is
1230 * encrypted so we can't check internal IP header. 1230 * encrypted so we can't check internal IP header.
1231 */ 1231 */
1232 if (key_prefered_oldsa) { 1232 if (key_prefered_oldsa) {
1233 saorder_state_valid = saorder_state_valid_prefer_old; 1233 saorder_state_valid = saorder_state_valid_prefer_old;
1234 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old); 1234 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
1235 } else { 1235 } else {
1236 saorder_state_valid = saorder_state_valid_prefer_new; 1236 saorder_state_valid = saorder_state_valid_prefer_new;
1237 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new); 1237 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
1238 } 1238 }
1239 s = pserialize_read_enter(); 1239 s = pserialize_read_enter();
1240 SAHLIST_READER_FOREACH(sah) { 1240 SAHLIST_READER_FOREACH(sah) {
1241 /* search valid state */ 1241 /* search valid state */
1242 for (stateidx = 0; stateidx < arraysize; stateidx++) { 1242 for (stateidx = 0; stateidx < arraysize; stateidx++) {
1243 state = saorder_state_valid[stateidx]; 1243 state = saorder_state_valid[stateidx];
1244 SAVLIST_READER_FOREACH(sav, sah, state) { 1244 SAVLIST_READER_FOREACH(sav, sah, state) {
1245 KEYDEBUG_PRINTF(KEYDEBUG_MATCH, 1245 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
1246 "try match spi %#x, %#x\n", 1246 "try match spi %#x, %#x\n",
1247 ntohl(spi), ntohl(sav->spi)); 1247 ntohl(spi), ntohl(sav->spi));
1248 /* sanity check */ 1248 /* sanity check */
1249 KEY_CHKSASTATE(sav->state, state); 1249 KEY_CHKSASTATE(sav->state, state);
1250 /* do not return entries w/ unusable state */ 1250 /* do not return entries w/ unusable state */
1251 if (!SADB_SASTATE_USABLE_P(sav)) { 1251 if (!SADB_SASTATE_USABLE_P(sav)) {
1252 KEYDEBUG_PRINTF(KEYDEBUG_MATCH, 1252 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
1253 "bad state %d\n", sav->state); 1253 "bad state %d\n", sav->state);
1254 continue; 1254 continue;
1255 } 1255 }
1256 if (proto != sav->sah->saidx.proto) { 1256 if (proto != sav->sah->saidx.proto) {
1257 KEYDEBUG_PRINTF(KEYDEBUG_MATCH, 1257 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
1258 "proto fail %d != %d\n", 1258 "proto fail %d != %d\n",
1259 proto, sav->sah->saidx.proto); 1259 proto, sav->sah->saidx.proto);
1260 continue; 1260 continue;
1261 } 1261 }
1262 if (must_check_spi && spi != sav->spi) { 1262 if (must_check_spi && spi != sav->spi) {
1263 KEYDEBUG_PRINTF(KEYDEBUG_MATCH, 1263 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
1264 "spi fail %#x != %#x\n", 1264 "spi fail %#x != %#x\n",
1265 ntohl(spi), ntohl(sav->spi)); 1265 ntohl(spi), ntohl(sav->spi));
1266 continue; 1266 continue;
1267 } 1267 }
1268 /* XXX only on the ipcomp case */ 1268 /* XXX only on the ipcomp case */
1269 if (must_check_alg && algo != sav->alg_comp) { 1269 if (must_check_alg && algo != sav->alg_comp) {
1270 KEYDEBUG_PRINTF(KEYDEBUG_MATCH, 1270 KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
1271 "algo fail %d != %d\n", 1271 "algo fail %d != %d\n",
1272 algo, sav->alg_comp); 1272 algo, sav->alg_comp);
1273 continue; 1273 continue;
1274 } 1274 }
1275 1275
1276#if 0 /* don't check src */ 1276#if 0 /* don't check src */
1277 /* Fix port in src->sa */ 1277 /* Fix port in src->sa */
1278 1278
1279 /* check src address */ 1279 /* check src address */
1280 if (!key_sockaddr_match(&src->sa, &sav->sah->saidx.src.sa, PORT_NONE)) 1280 if (!key_sockaddr_match(&src->sa, &sav->sah->saidx.src.sa, PORT_NONE))
1281 continue; 1281 continue;
1282#endif 1282#endif
1283 /* fix port of dst address XXX*/ 1283 /* fix port of dst address XXX*/
1284 key_porttosaddr(__UNCONST(dst), dport); 1284 key_porttosaddr(__UNCONST(dst), dport);
1285 /* check dst address */ 1285 /* check dst address */
1286 if (!key_sockaddr_match(&dst->sa, &sav->sah->saidx.dst.sa, chkport)) 1286 if (!key_sockaddr_match(&dst->sa, &sav->sah->saidx.dst.sa, chkport))
1287 continue; 1287 continue;
1288 key_sa_ref(sav, where, tag); 1288 key_sa_ref(sav, where, tag);
1289 goto done; 1289 goto done;
1290 } 1290 }
1291 } 1291 }
1292 } 1292 }
1293 sav = NULL; 1293 sav = NULL;
1294done: 1294done:
1295 pserialize_read_exit(s); 1295 pserialize_read_exit(s);
1296 1296
1297 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 1297 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1298 "DP return SA:%p; refcnt %u\n", sav, key_sa_refcnt(sav)); 1298 "DP return SA:%p; refcnt %u\n", sav, key_sa_refcnt(sav));
1299 return sav; 1299 return sav;
1300} 1300}
1301 1301
1302static void 1302static void
1303key_validate_savlist(const struct secashead *sah, const u_int state) 1303key_validate_savlist(const struct secashead *sah, const u_int state)
1304{ 1304{
1305#ifdef DEBUG 1305#ifdef DEBUG
1306 struct secasvar *sav, *next; 1306 struct secasvar *sav, *next;
1307 int s; 1307 int s;
1308 1308
1309 /* 1309 /*
1310 * The list should be sorted by lft_c->sadb_lifetime_addtime 1310 * The list should be sorted by lft_c->sadb_lifetime_addtime
1311 * in ascending order. 1311 * in ascending order.
1312 */ 1312 */
1313 s = pserialize_read_enter(); 1313 s = pserialize_read_enter();
1314 SAVLIST_READER_FOREACH(sav, sah, state) { 1314 SAVLIST_READER_FOREACH(sav, sah, state) {
1315 next = SAVLIST_READER_NEXT(sav); 1315 next = SAVLIST_READER_NEXT(sav);
1316 if (next != NULL && 1316 if (next != NULL &&
1317 sav->lft_c != NULL && next->lft_c != NULL) { 1317 sav->lft_c != NULL && next->lft_c != NULL) {
1318 KDASSERTMSG(sav->lft_c->sadb_lifetime_addtime <= 1318 KDASSERTMSG(sav->lft_c->sadb_lifetime_addtime <=
1319 next->lft_c->sadb_lifetime_addtime, 1319 next->lft_c->sadb_lifetime_addtime,
1320 "savlist is not sorted: sah=%p, state=%d, " 1320 "savlist is not sorted: sah=%p, state=%d, "
1321 "sav=%" PRIu64 ", next=%" PRIu64, sah, state, 1321 "sav=%" PRIu64 ", next=%" PRIu64, sah, state,
1322 sav->lft_c->sadb_lifetime_addtime, 1322 sav->lft_c->sadb_lifetime_addtime,
1323 next->lft_c->sadb_lifetime_addtime); 1323 next->lft_c->sadb_lifetime_addtime);
1324 } 1324 }
1325 } 1325 }
1326 pserialize_read_exit(s); 1326 pserialize_read_exit(s);
1327#endif 1327#endif
1328} 1328}
1329 1329
1330void 1330void
1331key_init_sp(struct secpolicy *sp) 1331key_init_sp(struct secpolicy *sp)
1332{ 1332{
1333 1333
1334 ASSERT_SLEEPABLE(); 1334 ASSERT_SLEEPABLE();
1335 1335
1336 sp->state = IPSEC_SPSTATE_ALIVE; 1336 sp->state = IPSEC_SPSTATE_ALIVE;
1337 if (sp->policy == IPSEC_POLICY_IPSEC) 1337 if (sp->policy == IPSEC_POLICY_IPSEC)
1338 KASSERT(sp->req != NULL); 1338 KASSERT(sp->req != NULL);
1339 localcount_init(&sp->localcount); 1339 localcount_init(&sp->localcount);
1340 SPLIST_ENTRY_INIT(sp); 1340 SPLIST_ENTRY_INIT(sp);
1341} 1341}
1342 1342
1343/* 1343/*
1344 * Must be called in a pserialize read section. A held SP 1344 * Must be called in a pserialize read section. A held SP
1345 * must be released by key_sp_unref after use. 1345 * must be released by key_sp_unref after use.
1346 */ 1346 */
1347void 1347void
1348key_sp_ref(struct secpolicy *sp, const char* where, int tag) 1348key_sp_ref(struct secpolicy *sp, const char* where, int tag)
1349{ 1349{
1350 1350
1351 localcount_acquire(&sp->localcount); 1351 localcount_acquire(&sp->localcount);
1352 1352
1353 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 1353 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1354 "DP SP:%p (ID=%u) from %s:%u; refcnt++ now %u\n", 1354 "DP SP:%p (ID=%u) from %s:%u; refcnt++ now %u\n",
1355 sp, sp->id, where, tag, key_sp_refcnt(sp)); 1355 sp, sp->id, where, tag, key_sp_refcnt(sp));
1356} 1356}
1357 1357
1358/* 1358/*
1359 * Must be called without holding key_spd.lock because the lock 1359 * Must be called without holding key_spd.lock because the lock
1360 * would be held in localcount_release. 1360 * would be held in localcount_release.
1361 */ 1361 */
1362void 1362void
1363key_sp_unref(struct secpolicy *sp, const char* where, int tag) 1363key_sp_unref(struct secpolicy *sp, const char* where, int tag)
1364{ 1364{
1365 1365
1366 KDASSERT(mutex_ownable(&key_spd.lock)); 1366 KDASSERT(mutex_ownable(&key_spd.lock));
1367 1367
1368 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 1368 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1369 "DP SP:%p (ID=%u) from %s:%u; refcnt-- now %u\n", 1369 "DP SP:%p (ID=%u) from %s:%u; refcnt-- now %u\n",
1370 sp, sp->id, where, tag, key_sp_refcnt(sp)); 1370 sp, sp->id, where, tag, key_sp_refcnt(sp));
1371 1371
1372 localcount_release(&sp->localcount, &key_spd.cv_lc, &key_spd.lock); 1372 localcount_release(&sp->localcount, &key_spd.cv_lc, &key_spd.lock);
1373} 1373}
1374 1374
1375static void 1375static void
1376key_init_sav(struct secasvar *sav) 1376key_init_sav(struct secasvar *sav)
1377{ 1377{
1378 1378
1379 ASSERT_SLEEPABLE(); 1379 ASSERT_SLEEPABLE();
1380 1380
1381 localcount_init(&sav->localcount); 1381 localcount_init(&sav->localcount);
1382 SAVLIST_ENTRY_INIT(sav); 1382 SAVLIST_ENTRY_INIT(sav);
1383} 1383}
1384 1384
1385u_int 1385u_int
1386key_sa_refcnt(const struct secasvar *sav) 1386key_sa_refcnt(const struct secasvar *sav)
1387{ 1387{
1388 1388
1389 /* FIXME */ 1389 /* FIXME */
1390 return 0; 1390 return 0;
1391} 1391}
1392 1392
1393void 1393void
1394key_sa_ref(struct secasvar *sav, const char* where, int tag) 1394key_sa_ref(struct secasvar *sav, const char* where, int tag)
1395{ 1395{
1396 1396
1397 localcount_acquire(&sav->localcount); 1397 localcount_acquire(&sav->localcount);
1398 1398
1399 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 1399 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1400 "DP cause refcnt++: SA:%p from %s:%u\n", 1400 "DP cause refcnt++: SA:%p from %s:%u\n",
1401 sav, where, tag); 1401 sav, where, tag);
1402} 1402}
1403 1403
1404void 1404void
1405key_sa_unref(struct secasvar *sav, const char* where, int tag) 1405key_sa_unref(struct secasvar *sav, const char* where, int tag)
1406{ 1406{
1407 1407
1408 KDASSERT(mutex_ownable(&key_sad.lock)); 1408 KDASSERT(mutex_ownable(&key_sad.lock));
1409 1409
1410 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 1410 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1411 "DP cause refcnt--: SA:%p from %s:%u\n", 1411 "DP cause refcnt--: SA:%p from %s:%u\n",
1412 sav, where, tag); 1412 sav, where, tag);
1413 1413
1414 localcount_release(&sav->localcount, &key_sad.cv_lc, &key_sad.lock); 1414 localcount_release(&sav->localcount, &key_sad.cv_lc, &key_sad.lock);
1415} 1415}
1416 1416
1417#if 0 1417#if 0
1418/* 1418/*
1419 * Must be called after calling key_lookup_sp*(). 1419 * Must be called after calling key_lookup_sp*().
1420 * For the packet with socket. 1420 * For the packet with socket.
1421 */ 1421 */
1422static void 1422static void
1423key_freeso(struct socket *so) 1423key_freeso(struct socket *so)
1424{ 1424{
1425 /* sanity check */ 1425 /* sanity check */
1426 KASSERT(so != NULL); 1426 KASSERT(so != NULL);
1427 1427
1428 switch (so->so_proto->pr_domain->dom_family) { 1428 switch (so->so_proto->pr_domain->dom_family) {
1429#ifdef INET 1429#ifdef INET
1430 case PF_INET: 1430 case PF_INET:
1431 { 1431 {
1432 struct inpcb *pcb = sotoinpcb(so); 1432 struct inpcb *pcb = sotoinpcb(so);
1433 1433
1434 /* Does it have a PCB ? */ 1434 /* Does it have a PCB ? */
1435 if (pcb == NULL) 1435 if (pcb == NULL)
1436 return; 1436 return;
1437 1437
1438 struct inpcbpolicy *sp = pcb->inp_sp; 1438 struct inpcbpolicy *sp = pcb->inp_sp;
1439 key_freesp_so(&sp->sp_in); 1439 key_freesp_so(&sp->sp_in);
1440 key_freesp_so(&sp->sp_out); 1440 key_freesp_so(&sp->sp_out);
1441 } 1441 }
1442 break; 1442 break;
1443#endif 1443#endif
1444#ifdef INET6 1444#ifdef INET6
1445 case PF_INET6: 1445 case PF_INET6:
1446 { 1446 {
1447#ifdef HAVE_NRL_INPCB 1447#ifdef HAVE_NRL_INPCB
1448 struct inpcb *pcb = sotoinpcb(so); 1448 struct inpcb *pcb = sotoinpcb(so);
1449 struct inpcbpolicy *sp = pcb->inp_sp; 1449 struct inpcbpolicy *sp = pcb->inp_sp;
1450 1450
1451 /* Does it have a PCB ? */ 1451 /* Does it have a PCB ? */
1452 if (pcb == NULL) 1452 if (pcb == NULL)
1453 return; 1453 return;
1454 key_freesp_so(&sp->sp_in); 1454 key_freesp_so(&sp->sp_in);
1455 key_freesp_so(&sp->sp_out); 1455 key_freesp_so(&sp->sp_out);
1456#else 1456#else
1457 struct in6pcb *pcb = sotoin6pcb(so); 1457 struct in6pcb *pcb = sotoin6pcb(so);
1458 1458
1459 /* Does it have a PCB ? */ 1459 /* Does it have a PCB ? */
1460 if (pcb == NULL) 1460 if (pcb == NULL)
1461 return; 1461 return;
1462 key_freesp_so(&pcb->in6p_sp->sp_in); 1462 key_freesp_so(&pcb->in6p_sp->sp_in);
1463 key_freesp_so(&pcb->in6p_sp->sp_out); 1463 key_freesp_so(&pcb->in6p_sp->sp_out);
1464#endif 1464#endif
1465 } 1465 }
1466 break; 1466 break;
1467#endif /* INET6 */ 1467#endif /* INET6 */
1468 default: 1468 default:
1469 IPSECLOG(LOG_DEBUG, "unknown address family=%d.\n", 1469 IPSECLOG(LOG_DEBUG, "unknown address family=%d.\n",
1470 so->so_proto->pr_domain->dom_family); 1470 so->so_proto->pr_domain->dom_family);
1471 return; 1471 return;
1472 } 1472 }
1473} 1473}
1474 1474
1475static void 1475static void
1476key_freesp_so(struct secpolicy **sp) 1476key_freesp_so(struct secpolicy **sp)
1477{ 1477{
1478 1478
1479 KASSERT(sp != NULL); 1479 KASSERT(sp != NULL);
1480 KASSERT(*sp != NULL); 1480 KASSERT(*sp != NULL);
1481 1481
1482 if ((*sp)->policy == IPSEC_POLICY_ENTRUST || 1482 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1483 (*sp)->policy == IPSEC_POLICY_BYPASS) 1483 (*sp)->policy == IPSEC_POLICY_BYPASS)
1484 return; 1484 return;
1485 1485
1486 KASSERTMSG((*sp)->policy == IPSEC_POLICY_IPSEC, 1486 KASSERTMSG((*sp)->policy == IPSEC_POLICY_IPSEC,
1487 "invalid policy %u", (*sp)->policy); 1487 "invalid policy %u", (*sp)->policy);
1488 KEY_SP_UNREF(&sp); 1488 KEY_SP_UNREF(&sp);
1489} 1489}
1490#endif 1490#endif
1491 1491
1492static void 1492static void
1493key_sad_pserialize_perform(void) 1493key_sad_pserialize_perform(void)
1494{ 1494{
1495 1495
1496 KASSERT(mutex_owned(&key_sad.lock)); 1496 KASSERT(mutex_owned(&key_sad.lock));
1497 1497
1498 while (key_sad.psz_performing) 1498 while (key_sad.psz_performing)
1499 cv_wait(&key_sad.cv_psz, &key_sad.lock); 1499 cv_wait(&key_sad.cv_psz, &key_sad.lock);
1500 key_sad.psz_performing = true; 1500 key_sad.psz_performing = true;
1501 mutex_exit(&key_sad.lock); 1501 mutex_exit(&key_sad.lock);
1502 1502
1503 pserialize_perform(key_sad.psz); 1503 pserialize_perform(key_sad.psz);
1504 1504
1505 mutex_enter(&key_sad.lock); 1505 mutex_enter(&key_sad.lock);
1506 key_sad.psz_performing = false; 1506 key_sad.psz_performing = false;
1507 cv_broadcast(&key_sad.cv_psz); 1507 cv_broadcast(&key_sad.cv_psz);
1508} 1508}
1509 1509
1510/* 1510/*
1511 * Remove the sav from the savlist of its sah and wait for references to the sav 1511 * Remove the sav from the savlist of its sah and wait for references to the sav
1512 * to be released. key_sad.lock must be held. 1512 * to be released. key_sad.lock must be held.
1513 */ 1513 */
1514static void 1514static void
1515key_unlink_sav(struct secasvar *sav) 1515key_unlink_sav(struct secasvar *sav)
1516{ 1516{
1517 1517
1518 KASSERT(mutex_owned(&key_sad.lock)); 1518 KASSERT(mutex_owned(&key_sad.lock));
1519 1519
1520 SAVLIST_WRITER_REMOVE(sav); 1520 SAVLIST_WRITER_REMOVE(sav);
1521 1521
1522 KDASSERT(mutex_ownable(softnet_lock)); 1522 KDASSERT(mutex_ownable(softnet_lock));
1523 key_sad_pserialize_perform(); 1523 key_sad_pserialize_perform();
1524 1524
1525 localcount_drain(&sav->localcount, &key_sad.cv_lc, &key_sad.lock); 1525 localcount_drain(&sav->localcount, &key_sad.cv_lc, &key_sad.lock);
1526} 1526}
1527 1527
1528/* 1528/*
1529 * Destroy an sav where the sav must be unlinked from an sah 1529 * Destroy an sav where the sav must be unlinked from an sah
1530 * by say key_unlink_sav. 1530 * by say key_unlink_sav.
1531 */ 1531 */
1532static void 1532static void
1533key_destroy_sav(struct secasvar *sav) 1533key_destroy_sav(struct secasvar *sav)
1534{ 1534{
1535 1535
1536 ASSERT_SLEEPABLE(); 1536 ASSERT_SLEEPABLE();
1537 1537
1538 localcount_fini(&sav->localcount); 1538 localcount_fini(&sav->localcount);
1539 SAVLIST_ENTRY_DESTROY(sav); 1539 SAVLIST_ENTRY_DESTROY(sav);
1540 1540
1541 key_delsav(sav); 1541 key_delsav(sav);
1542} 1542}
1543 1543
1544/* 1544/*
1545 * Destroy sav with holding its reference. 1545 * Destroy sav with holding its reference.
1546 */ 1546 */
1547static void 1547static void
1548key_destroy_sav_with_ref(struct secasvar *sav) 1548key_destroy_sav_with_ref(struct secasvar *sav)
1549{ 1549{
1550 1550
1551 ASSERT_SLEEPABLE(); 1551 ASSERT_SLEEPABLE();
1552 1552
1553 mutex_enter(&key_sad.lock); 1553 mutex_enter(&key_sad.lock);
1554 sav->state = SADB_SASTATE_DEAD; 1554 sav->state = SADB_SASTATE_DEAD;
1555 SAVLIST_WRITER_REMOVE(sav); 1555 SAVLIST_WRITER_REMOVE(sav);
1556 mutex_exit(&key_sad.lock); 1556 mutex_exit(&key_sad.lock);
1557 1557
1558 /* We cannot unref with holding key_sad.lock */ 1558 /* We cannot unref with holding key_sad.lock */
1559 KEY_SA_UNREF(&sav); 1559 KEY_SA_UNREF(&sav);
1560 1560
1561 mutex_enter(&key_sad.lock); 1561 mutex_enter(&key_sad.lock);
1562 KDASSERT(mutex_ownable(softnet_lock)); 1562 KDASSERT(mutex_ownable(softnet_lock));
1563 key_sad_pserialize_perform(); 1563 key_sad_pserialize_perform();
1564 localcount_drain(&sav->localcount, &key_sad.cv_lc, &key_sad.lock); 1564 localcount_drain(&sav->localcount, &key_sad.cv_lc, &key_sad.lock);
1565 mutex_exit(&key_sad.lock); 1565 mutex_exit(&key_sad.lock);
1566 1566
1567 key_destroy_sav(sav); 1567 key_destroy_sav(sav);
1568} 1568}
1569 1569
1570/* %%% SPD management */ 1570/* %%% SPD management */
1571/* 1571/*
1572 * free security policy entry. 1572 * free security policy entry.
1573 */ 1573 */
1574static void 1574static void
1575key_destroy_sp(struct secpolicy *sp) 1575key_destroy_sp(struct secpolicy *sp)
1576{ 1576{
1577 1577
1578 SPLIST_ENTRY_DESTROY(sp); 1578 SPLIST_ENTRY_DESTROY(sp);
1579 localcount_fini(&sp->localcount); 1579 localcount_fini(&sp->localcount);
1580 1580
1581 key_free_sp(sp); 1581 key_free_sp(sp);
1582 1582
1583 key_update_used(); 1583 key_update_used();
1584} 1584}
1585 1585
1586void 1586void
1587key_free_sp(struct secpolicy *sp) 1587key_free_sp(struct secpolicy *sp)
1588{ 1588{
1589 struct ipsecrequest *isr = sp->req, *nextisr; 1589 struct ipsecrequest *isr = sp->req, *nextisr;
1590 1590
1591 while (isr != NULL) { 1591 while (isr != NULL) {
1592 nextisr = isr->next; 1592 nextisr = isr->next;
1593 kmem_free(isr, sizeof(*isr)); 1593 kmem_free(isr, sizeof(*isr));
1594 isr = nextisr; 1594 isr = nextisr;
1595 } 1595 }
1596 1596
1597 kmem_free(sp, sizeof(*sp)); 1597 kmem_free(sp, sizeof(*sp));
1598} 1598}
1599 1599
1600void 1600void
1601key_socksplist_add(struct secpolicy *sp) 1601key_socksplist_add(struct secpolicy *sp)
1602{ 1602{
1603 1603
1604 mutex_enter(&key_spd.lock); 1604 mutex_enter(&key_spd.lock);
1605 PSLIST_WRITER_INSERT_HEAD(&key_spd.socksplist, sp, pslist_entry); 1605 PSLIST_WRITER_INSERT_HEAD(&key_spd.socksplist, sp, pslist_entry);
1606 mutex_exit(&key_spd.lock); 1606 mutex_exit(&key_spd.lock);
1607 1607
1608 key_update_used(); 1608 key_update_used();
1609} 1609}
1610 1610
1611/* 1611/*
1612 * search SPD 1612 * search SPD
1613 * OUT: NULL : not found 1613 * OUT: NULL : not found
1614 * others : found, pointer to a SP. 1614 * others : found, pointer to a SP.
1615 */ 1615 */
1616static struct secpolicy * 1616static struct secpolicy *
1617key_getsp(const struct secpolicyindex *spidx) 1617key_getsp(const struct secpolicyindex *spidx)
1618{ 1618{
1619 struct secpolicy *sp; 1619 struct secpolicy *sp;
1620 int s; 1620 int s;
1621 1621
1622 KASSERT(spidx != NULL); 1622 KASSERT(spidx != NULL);
1623 1623
1624 s = pserialize_read_enter(); 1624 s = pserialize_read_enter();
1625 SPLIST_READER_FOREACH(sp, spidx->dir) { 1625 SPLIST_READER_FOREACH(sp, spidx->dir) {
1626 if (sp->state == IPSEC_SPSTATE_DEAD) 1626 if (sp->state == IPSEC_SPSTATE_DEAD)
1627 continue; 1627 continue;
1628 if (key_spidx_match_exactly(spidx, &sp->spidx)) { 1628 if (key_spidx_match_exactly(spidx, &sp->spidx)) {
1629 KEY_SP_REF(sp); 1629 KEY_SP_REF(sp);
1630 pserialize_read_exit(s); 1630 pserialize_read_exit(s);
1631 return sp; 1631 return sp;
1632 } 1632 }
1633 } 1633 }
1634 pserialize_read_exit(s); 1634 pserialize_read_exit(s);
1635 1635
1636 return NULL; 1636 return NULL;
1637} 1637}
1638 1638
1639/* 1639/*
1640 * search SPD and remove found SP 1640 * search SPD and remove found SP
1641 * OUT: NULL : not found 1641 * OUT: NULL : not found
1642 * others : found, pointer to a SP. 1642 * others : found, pointer to a SP.
1643 */ 1643 */
1644static struct secpolicy * 1644static struct secpolicy *
1645key_lookup_and_remove_sp(const struct secpolicyindex *spidx) 1645key_lookup_and_remove_sp(const struct secpolicyindex *spidx)
1646{ 1646{
1647 struct secpolicy *sp = NULL; 1647 struct secpolicy *sp = NULL;
1648 1648
1649 mutex_enter(&key_spd.lock); 1649 mutex_enter(&key_spd.lock);
1650 SPLIST_WRITER_FOREACH(sp, spidx->dir) { 1650 SPLIST_WRITER_FOREACH(sp, spidx->dir) {
1651 KASSERT(sp->state != IPSEC_SPSTATE_DEAD); 1651 KASSERT(sp->state != IPSEC_SPSTATE_DEAD);
1652 1652
1653 if (key_spidx_match_exactly(spidx, &sp->spidx)) { 1653 if (key_spidx_match_exactly(spidx, &sp->spidx)) {
1654 key_unlink_sp(sp); 1654 key_unlink_sp(sp);
1655 goto out; 1655 goto out;
1656 } 1656 }
1657 } 1657 }
1658 sp = NULL; 1658 sp = NULL;
1659out: 1659out:
1660 mutex_exit(&key_spd.lock); 1660 mutex_exit(&key_spd.lock);
1661 1661
1662 return sp; 1662 return sp;
1663} 1663}
1664 1664
1665/* 1665/*
1666 * get SP by index. 1666 * get SP by index.
1667 * OUT: NULL : not found 1667 * OUT: NULL : not found
1668 * others : found, pointer to a SP. 1668 * others : found, pointer to a SP.
1669 */ 1669 */
1670static struct secpolicy * 1670static struct secpolicy *
1671key_getspbyid(u_int32_t id) 1671key_getspbyid(u_int32_t id)
1672{ 1672{
1673 struct secpolicy *sp; 1673 struct secpolicy *sp;
1674 int s; 1674 int s;
1675 1675
1676 s = pserialize_read_enter(); 1676 s = pserialize_read_enter();
1677 SPLIST_READER_FOREACH(sp, IPSEC_DIR_INBOUND) { 1677 SPLIST_READER_FOREACH(sp, IPSEC_DIR_INBOUND) {
1678 if (sp->state == IPSEC_SPSTATE_DEAD) 1678 if (sp->state == IPSEC_SPSTATE_DEAD)
1679 continue; 1679 continue;
1680 if (sp->id == id) { 1680 if (sp->id == id) {
1681 KEY_SP_REF(sp); 1681 KEY_SP_REF(sp);
1682 goto out; 1682 goto out;
1683 } 1683 }
1684 } 1684 }
1685 1685
1686 SPLIST_READER_FOREACH(sp, IPSEC_DIR_OUTBOUND) { 1686 SPLIST_READER_FOREACH(sp, IPSEC_DIR_OUTBOUND) {
1687 if (sp->state == IPSEC_SPSTATE_DEAD) 1687 if (sp->state == IPSEC_SPSTATE_DEAD)
1688 continue; 1688 continue;
1689 if (sp->id == id) { 1689 if (sp->id == id) {
1690 KEY_SP_REF(sp); 1690 KEY_SP_REF(sp);
1691 goto out; 1691 goto out;
1692 } 1692 }
1693 } 1693 }
1694out: 1694out:
1695 pserialize_read_exit(s); 1695 pserialize_read_exit(s);
1696 return sp; 1696 return sp;
1697} 1697}
1698 1698
1699/* 1699/*
1700 * get SP by index, remove and return it. 1700 * get SP by index, remove and return it.
1701 * OUT: NULL : not found 1701 * OUT: NULL : not found
1702 * others : found, pointer to a SP. 1702 * others : found, pointer to a SP.
1703 */ 1703 */
1704static struct secpolicy * 1704static struct secpolicy *
1705key_lookupbyid_and_remove_sp(u_int32_t id) 1705key_lookupbyid_and_remove_sp(u_int32_t id)
1706{ 1706{
1707 struct secpolicy *sp; 1707 struct secpolicy *sp;
1708 1708
1709 mutex_enter(&key_spd.lock); 1709 mutex_enter(&key_spd.lock);
1710 SPLIST_READER_FOREACH(sp, IPSEC_DIR_INBOUND) { 1710 SPLIST_READER_FOREACH(sp, IPSEC_DIR_INBOUND) {
1711 KASSERT(sp->state != IPSEC_SPSTATE_DEAD); 1711 KASSERT(sp->state != IPSEC_SPSTATE_DEAD);
1712 if (sp->id == id) 1712 if (sp->id == id)
1713 goto out; 1713 goto out;
1714 } 1714 }
1715 1715
1716 SPLIST_READER_FOREACH(sp, IPSEC_DIR_OUTBOUND) { 1716 SPLIST_READER_FOREACH(sp, IPSEC_DIR_OUTBOUND) {
1717 KASSERT(sp->state != IPSEC_SPSTATE_DEAD); 1717 KASSERT(sp->state != IPSEC_SPSTATE_DEAD);
1718 if (sp->id == id) 1718 if (sp->id == id)
1719 goto out; 1719 goto out;
1720 } 1720 }
1721out: 1721out:
1722 if (sp != NULL) 1722 if (sp != NULL)
1723 key_unlink_sp(sp); 1723 key_unlink_sp(sp);
1724 mutex_exit(&key_spd.lock); 1724 mutex_exit(&key_spd.lock);
1725 return sp; 1725 return sp;
1726} 1726}
1727 1727
1728struct secpolicy * 1728struct secpolicy *
1729key_newsp(const char* where, int tag) 1729key_newsp(const char* where, int tag)
1730{ 1730{
1731 struct secpolicy *newsp = NULL; 1731 struct secpolicy *newsp = NULL;
1732 1732
1733 newsp = kmem_zalloc(sizeof(struct secpolicy), KM_SLEEP); 1733 newsp = kmem_zalloc(sizeof(struct secpolicy), KM_SLEEP);
1734 1734
1735 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 1735 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
1736 "DP from %s:%u return SP:%p\n", where, tag, newsp); 1736 "DP from %s:%u return SP:%p\n", where, tag, newsp);
1737 return newsp; 1737 return newsp;
1738} 1738}
1739 1739
1740/* 1740/*
1741 * create secpolicy structure from sadb_x_policy structure. 1741 * create secpolicy structure from sadb_x_policy structure.
1742 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set, 1742 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1743 * so must be set properly later. 1743 * so must be set properly later.
1744 */ 1744 */
1745struct secpolicy * 1745struct secpolicy *
1746key_msg2sp(const struct sadb_x_policy *xpl0, size_t len, int *error) 1746key_msg2sp(const struct sadb_x_policy *xpl0, size_t len, int *error)
1747{ 1747{
1748 struct secpolicy *newsp; 1748 struct secpolicy *newsp;
1749 1749
1750 KASSERT(!cpu_softintr_p()); 1750 KASSERT(!cpu_softintr_p());
1751 KASSERT(xpl0 != NULL); 1751 KASSERT(xpl0 != NULL);
1752 KASSERT(len >= sizeof(*xpl0)); 1752 KASSERT(len >= sizeof(*xpl0));
1753 1753
1754 if (len != PFKEY_EXTLEN(xpl0)) { 1754 if (len != PFKEY_EXTLEN(xpl0)) {
1755 IPSECLOG(LOG_DEBUG, "Invalid msg length.\n"); 1755 IPSECLOG(LOG_DEBUG, "Invalid msg length.\n");
1756 *error = EINVAL; 1756 *error = EINVAL;
1757 return NULL; 1757 return NULL;
1758 } 1758 }
1759 1759
1760 newsp = KEY_NEWSP(); 1760 newsp = KEY_NEWSP();
1761 if (newsp == NULL) { 1761 if (newsp == NULL) {
1762 *error = ENOBUFS; 1762 *error = ENOBUFS;
1763 return NULL; 1763 return NULL;
1764 } 1764 }
1765 1765
1766 newsp->spidx.dir = xpl0->sadb_x_policy_dir; 1766 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1767 newsp->policy = xpl0->sadb_x_policy_type; 1767 newsp->policy = xpl0->sadb_x_policy_type;
1768 1768
1769 /* check policy */ 1769 /* check policy */
1770 switch (xpl0->sadb_x_policy_type) { 1770 switch (xpl0->sadb_x_policy_type) {
1771 case IPSEC_POLICY_DISCARD: 1771 case IPSEC_POLICY_DISCARD:
1772 case IPSEC_POLICY_NONE: 1772 case IPSEC_POLICY_NONE:
1773 case IPSEC_POLICY_ENTRUST: 1773 case IPSEC_POLICY_ENTRUST:
1774 case IPSEC_POLICY_BYPASS: 1774 case IPSEC_POLICY_BYPASS:
1775 newsp->req = NULL; 1775 newsp->req = NULL;
1776 *error = 0; 1776 *error = 0;
1777 return newsp; 1777 return newsp;
1778 1778
1779 case IPSEC_POLICY_IPSEC: 1779 case IPSEC_POLICY_IPSEC:
1780 /* Continued */ 1780 /* Continued */
1781 break; 1781 break;
1782 default: 1782 default:
1783 IPSECLOG(LOG_DEBUG, "invalid policy type.\n"); 1783 IPSECLOG(LOG_DEBUG, "invalid policy type.\n");
1784 key_free_sp(newsp); 1784 key_free_sp(newsp);
1785 *error = EINVAL; 1785 *error = EINVAL;
1786 return NULL; 1786 return NULL;
1787 } 1787 }
1788 1788
1789 /* IPSEC_POLICY_IPSEC */ 1789 /* IPSEC_POLICY_IPSEC */
1790 { 1790 {
1791 int tlen; 1791 int tlen;
1792 const struct sadb_x_ipsecrequest *xisr; 1792 const struct sadb_x_ipsecrequest *xisr;
1793 uint16_t xisr_reqid; 1793 uint16_t xisr_reqid;
1794 struct ipsecrequest **p_isr = &newsp->req; 1794 struct ipsecrequest **p_isr = &newsp->req;
1795 1795
1796 /* validity check */ 1796 /* validity check */
1797 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) { 1797 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1798 IPSECLOG(LOG_DEBUG, "Invalid msg length.\n"); 1798 IPSECLOG(LOG_DEBUG, "Invalid msg length.\n");
1799 *error = EINVAL; 1799 *error = EINVAL;
1800 goto free_exit; 1800 goto free_exit;
1801 } 1801 }
1802 1802
1803 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0); 1803 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1804 xisr = (const struct sadb_x_ipsecrequest *)(xpl0 + 1); 1804 xisr = (const struct sadb_x_ipsecrequest *)(xpl0 + 1);
1805 1805
1806 while (tlen > 0) { 1806 while (tlen > 0) {
1807 /* length check */ 1807 /* length check */
1808 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) { 1808 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1809 IPSECLOG(LOG_DEBUG, "invalid ipsecrequest length.\n"); 1809 IPSECLOG(LOG_DEBUG, "invalid ipsecrequest length.\n");
1810 *error = EINVAL; 1810 *error = EINVAL;
1811 goto free_exit; 1811 goto free_exit;
1812 } 1812 }
1813 1813
1814 /* allocate request buffer */ 1814 /* allocate request buffer */
1815 *p_isr = kmem_zalloc(sizeof(**p_isr), KM_SLEEP); 1815 *p_isr = kmem_zalloc(sizeof(**p_isr), KM_SLEEP);
1816 1816
1817 /* set values */ 1817 /* set values */
1818 (*p_isr)->next = NULL; 1818 (*p_isr)->next = NULL;
1819 1819
1820 switch (xisr->sadb_x_ipsecrequest_proto) { 1820 switch (xisr->sadb_x_ipsecrequest_proto) {
1821 case IPPROTO_ESP: 1821 case IPPROTO_ESP:
1822 case IPPROTO_AH: 1822 case IPPROTO_AH:
1823 case IPPROTO_IPCOMP: 1823 case IPPROTO_IPCOMP:
1824 break; 1824 break;
1825 default: 1825 default:
1826 IPSECLOG(LOG_DEBUG, "invalid proto type=%u\n", 1826 IPSECLOG(LOG_DEBUG, "invalid proto type=%u\n",
1827 xisr->sadb_x_ipsecrequest_proto); 1827 xisr->sadb_x_ipsecrequest_proto);
1828 *error = EPROTONOSUPPORT; 1828 *error = EPROTONOSUPPORT;
1829 goto free_exit; 1829 goto free_exit;
1830 } 1830 }
1831 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto; 1831 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1832 1832
1833 switch (xisr->sadb_x_ipsecrequest_mode) { 1833 switch (xisr->sadb_x_ipsecrequest_mode) {
1834 case IPSEC_MODE_TRANSPORT: 1834 case IPSEC_MODE_TRANSPORT:
1835 case IPSEC_MODE_TUNNEL: 1835 case IPSEC_MODE_TUNNEL:
1836 break; 1836 break;
1837 case IPSEC_MODE_ANY: 1837 case IPSEC_MODE_ANY:
1838 default: 1838 default:
1839 IPSECLOG(LOG_DEBUG, "invalid mode=%u\n", 1839 IPSECLOG(LOG_DEBUG, "invalid mode=%u\n",
1840 xisr->sadb_x_ipsecrequest_mode); 1840 xisr->sadb_x_ipsecrequest_mode);
1841 *error = EINVAL; 1841 *error = EINVAL;
1842 goto free_exit; 1842 goto free_exit;
1843 } 1843 }
1844 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode; 1844 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1845 1845
1846 switch (xisr->sadb_x_ipsecrequest_level) { 1846 switch (xisr->sadb_x_ipsecrequest_level) {
1847 case IPSEC_LEVEL_DEFAULT: 1847 case IPSEC_LEVEL_DEFAULT:
1848 case IPSEC_LEVEL_USE: 1848 case IPSEC_LEVEL_USE:
1849 case IPSEC_LEVEL_REQUIRE: 1849 case IPSEC_LEVEL_REQUIRE:
1850 break; 1850 break;
1851 case IPSEC_LEVEL_UNIQUE: 1851 case IPSEC_LEVEL_UNIQUE:
1852 xisr_reqid = xisr->sadb_x_ipsecrequest_reqid; 1852 xisr_reqid = xisr->sadb_x_ipsecrequest_reqid;
1853 /* validity check */ 1853 /* validity check */
1854 /* 1854 /*
1855 * If range violation of reqid, kernel will 1855 * If range violation of reqid, kernel will
1856 * update it, don't refuse it. 1856 * update it, don't refuse it.
1857 */ 1857 */
1858 if (xisr_reqid > IPSEC_MANUAL_REQID_MAX) { 1858 if (xisr_reqid > IPSEC_MANUAL_REQID_MAX) {
1859 IPSECLOG(LOG_DEBUG, 1859 IPSECLOG(LOG_DEBUG,
1860 "reqid=%d range " 1860 "reqid=%d range "
1861 "violation, updated by kernel.\n", 1861 "violation, updated by kernel.\n",
1862 xisr_reqid); 1862 xisr_reqid);
1863 xisr_reqid = 0; 1863 xisr_reqid = 0;
1864 } 1864 }
1865 1865
1866 /* allocate new reqid id if reqid is zero. */ 1866 /* allocate new reqid id if reqid is zero. */
1867 if (xisr_reqid == 0) { 1867 if (xisr_reqid == 0) {
1868 u_int16_t reqid = key_newreqid(); 1868 u_int16_t reqid = key_newreqid();
1869 if (reqid == 0) { 1869 if (reqid == 0) {
1870 *error = ENOBUFS; 1870 *error = ENOBUFS;
1871 goto free_exit; 1871 goto free_exit;
1872 } 1872 }
1873 (*p_isr)->saidx.reqid = reqid; 1873 (*p_isr)->saidx.reqid = reqid;
1874 } else { 1874 } else {
1875 /* set it for manual keying. */ 1875 /* set it for manual keying. */
1876 (*p_isr)->saidx.reqid = xisr_reqid; 1876 (*p_isr)->saidx.reqid = xisr_reqid;
1877 } 1877 }
1878 break; 1878 break;
1879 1879
1880 default: 1880 default:
1881 IPSECLOG(LOG_DEBUG, "invalid level=%u\n", 1881 IPSECLOG(LOG_DEBUG, "invalid level=%u\n",
1882 xisr->sadb_x_ipsecrequest_level); 1882 xisr->sadb_x_ipsecrequest_level);
1883 *error = EINVAL; 1883 *error = EINVAL;
1884 goto free_exit; 1884 goto free_exit;
1885 } 1885 }
1886 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level; 1886 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1887 1887
1888 /* set IP addresses if there */ 1888 /* set IP addresses if there */
1889 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) { 1889 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1890 const struct sockaddr *paddr; 1890 const struct sockaddr *paddr;
1891 1891
1892 paddr = (const struct sockaddr *)(xisr + 1); 1892 paddr = (const struct sockaddr *)(xisr + 1);
1893 1893
1894 /* validity check */ 1894 /* validity check */
1895 if (paddr->sa_len > sizeof((*p_isr)->saidx.src)) { 1895 if (paddr->sa_len > sizeof((*p_isr)->saidx.src)) {
1896 IPSECLOG(LOG_DEBUG, "invalid request " 1896 IPSECLOG(LOG_DEBUG, "invalid request "
1897 "address length.\n"); 1897 "address length.\n");
1898 *error = EINVAL; 1898 *error = EINVAL;
1899 goto free_exit; 1899 goto free_exit;
1900 } 1900 }
1901 memcpy(&(*p_isr)->saidx.src, paddr, paddr->sa_len); 1901 memcpy(&(*p_isr)->saidx.src, paddr, paddr->sa_len);
1902 1902
1903 paddr = (const struct sockaddr *)((const char *)paddr 1903 paddr = (const struct sockaddr *)((const char *)paddr
1904 + paddr->sa_len); 1904 + paddr->sa_len);
1905 1905
1906 /* validity check */ 1906 /* validity check */
1907 if (paddr->sa_len > sizeof((*p_isr)->saidx.dst)) { 1907 if (paddr->sa_len > sizeof((*p_isr)->saidx.dst)) {
1908 IPSECLOG(LOG_DEBUG, "invalid request " 1908 IPSECLOG(LOG_DEBUG, "invalid request "
1909 "address length.\n"); 1909 "address length.\n");
1910 *error = EINVAL; 1910 *error = EINVAL;
1911 goto free_exit; 1911 goto free_exit;
1912 } 1912 }
1913 memcpy(&(*p_isr)->saidx.dst, paddr, paddr->sa_len); 1913 memcpy(&(*p_isr)->saidx.dst, paddr, paddr->sa_len);
1914 } 1914 }
1915 1915
1916 (*p_isr)->sp = newsp; 1916 (*p_isr)->sp = newsp;
1917 1917
1918 /* initialization for the next. */ 1918 /* initialization for the next. */
1919 p_isr = &(*p_isr)->next; 1919 p_isr = &(*p_isr)->next;
1920 tlen -= xisr->sadb_x_ipsecrequest_len; 1920 tlen -= xisr->sadb_x_ipsecrequest_len;
1921 1921
1922 /* validity check */ 1922 /* validity check */
1923 if (tlen < 0) { 1923 if (tlen < 0) {
1924 IPSECLOG(LOG_DEBUG, "becoming tlen < 0.\n"); 1924 IPSECLOG(LOG_DEBUG, "becoming tlen < 0.\n");
1925 *error = EINVAL; 1925 *error = EINVAL;
1926 goto free_exit; 1926 goto free_exit;
1927 } 1927 }
1928 1928
1929 xisr = (const struct sadb_x_ipsecrequest *)((const char *)xisr + 1929 xisr = (const struct sadb_x_ipsecrequest *)((const char *)xisr +
1930 xisr->sadb_x_ipsecrequest_len); 1930 xisr->sadb_x_ipsecrequest_len);
1931 } 1931 }
1932 } 1932 }
1933 1933
1934 *error = 0; 1934 *error = 0;
1935 return newsp; 1935 return newsp;
1936 1936
1937free_exit: 1937free_exit:
1938 key_free_sp(newsp); 1938 key_free_sp(newsp);
1939 return NULL; 1939 return NULL;
1940} 1940}
1941 1941
1942static u_int16_t 1942static u_int16_t
1943key_newreqid(void) 1943key_newreqid(void)
1944{ 1944{
1945 static u_int16_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1; 1945 static u_int16_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1946 1946
1947 auto_reqid = (auto_reqid == 0xffff ? 1947 auto_reqid = (auto_reqid == 0xffff ?
1948 IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1); 1948 IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1949 1949
1950 /* XXX should be unique check */ 1950 /* XXX should be unique check */
1951 1951
1952 return auto_reqid; 1952 return auto_reqid;
1953} 1953}
1954 1954
1955/* 1955/*
1956 * copy secpolicy struct to sadb_x_policy structure indicated. 1956 * copy secpolicy struct to sadb_x_policy structure indicated.
1957 */ 1957 */
1958struct mbuf * 1958struct mbuf *
1959key_sp2msg(const struct secpolicy *sp, int mflag) 1959key_sp2msg(const struct secpolicy *sp, int mflag)
1960{ 1960{
1961 struct sadb_x_policy *xpl; 1961 struct sadb_x_policy *xpl;
1962 int tlen; 1962 int tlen;
1963 char *p; 1963 char *p;
1964 struct mbuf *m; 1964 struct mbuf *m;
1965 1965
1966 KASSERT(sp != NULL); 1966 KASSERT(sp != NULL);
1967 1967
1968 tlen = key_getspreqmsglen(sp); 1968 tlen = key_getspreqmsglen(sp);
1969 1969
1970 m = key_alloc_mbuf(tlen, mflag); 1970 m = key_alloc_mbuf(tlen, mflag);
1971 if (!m || m->m_next) { /*XXX*/ 1971 if (!m || m->m_next) { /*XXX*/
1972 if (m) 1972 if (m)
1973 m_freem(m); 1973 m_freem(m);
1974 return NULL; 1974 return NULL;
1975 } 1975 }
1976 1976
1977 m->m_len = tlen; 1977 m->m_len = tlen;
1978 m->m_next = NULL; 1978 m->m_next = NULL;
1979 xpl = mtod(m, struct sadb_x_policy *); 1979 xpl = mtod(m, struct sadb_x_policy *);
1980 memset(xpl, 0, tlen); 1980 memset(xpl, 0, tlen);
1981 1981
1982 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen); 1982 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1983 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY; 1983 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1984 xpl->sadb_x_policy_type = sp->policy; 1984 xpl->sadb_x_policy_type = sp->policy;
1985 xpl->sadb_x_policy_dir = sp->spidx.dir; 1985 xpl->sadb_x_policy_dir = sp->spidx.dir;
1986 xpl->sadb_x_policy_id = sp->id; 1986 xpl->sadb_x_policy_id = sp->id;
1987 p = (char *)xpl + sizeof(*xpl); 1987 p = (char *)xpl + sizeof(*xpl);
1988 1988
1989 /* if is the policy for ipsec ? */ 1989 /* if is the policy for ipsec ? */
1990 if (sp->policy == IPSEC_POLICY_IPSEC) { 1990 if (sp->policy == IPSEC_POLICY_IPSEC) {
1991 struct sadb_x_ipsecrequest *xisr; 1991 struct sadb_x_ipsecrequest *xisr;
1992 struct ipsecrequest *isr; 1992 struct ipsecrequest *isr;
1993 1993
1994 for (isr = sp->req; isr != NULL; isr = isr->next) { 1994 for (isr = sp->req; isr != NULL; isr = isr->next) {
1995 1995
1996 xisr = (struct sadb_x_ipsecrequest *)p; 1996 xisr = (struct sadb_x_ipsecrequest *)p;
1997 1997
1998 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto; 1998 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1999 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode; 1999 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
2000 xisr->sadb_x_ipsecrequest_level = isr->level; 2000 xisr->sadb_x_ipsecrequest_level = isr->level;
2001 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid; 2001 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
2002 2002
2003 p += sizeof(*xisr); 2003 p += sizeof(*xisr);
2004 memcpy(p, &isr->saidx.src, isr->saidx.src.sa.sa_len); 2004 memcpy(p, &isr->saidx.src, isr->saidx.src.sa.sa_len);
2005 p += isr->saidx.src.sa.sa_len; 2005 p += isr->saidx.src.sa.sa_len;
2006 memcpy(p, &isr->saidx.dst, isr->saidx.dst.sa.sa_len); 2006 memcpy(p, &isr->saidx.dst, isr->saidx.dst.sa.sa_len);
2007 p += isr->saidx.src.sa.sa_len; 2007 p += isr->saidx.src.sa.sa_len;
2008 2008
2009 xisr->sadb_x_ipsecrequest_len = 2009 xisr->sadb_x_ipsecrequest_len =
2010 PFKEY_ALIGN8(sizeof(*xisr) 2010 PFKEY_ALIGN8(sizeof(*xisr)
2011 + isr->saidx.src.sa.sa_len 2011 + isr->saidx.src.sa.sa_len
2012 + isr->saidx.dst.sa.sa_len); 2012 + isr->saidx.dst.sa.sa_len);
2013 } 2013 }
2014 } 2014 }
2015 2015
2016 return m; 2016 return m;
2017} 2017}
2018 2018
2019/* 2019/*
2020 * m will not be freed nor modified. It never return NULL. 2020 * m will not be freed nor modified. It never return NULL.
2021 * If it returns a mbuf of M_PKTHDR, the mbuf ensures to have 2021 * If it returns a mbuf of M_PKTHDR, the mbuf ensures to have
2022 * contiguous length at least sizeof(struct sadb_msg). 2022 * contiguous length at least sizeof(struct sadb_msg).
2023 */ 2023 */
2024static struct mbuf * 2024static struct mbuf *
2025key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp, 2025key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
2026 int ndeep, int nitem, ...) 2026 int ndeep, int nitem, ...)
2027{ 2027{
2028 va_list ap; 2028 va_list ap;
2029 int idx; 2029 int idx;
2030 int i; 2030 int i;
2031 struct mbuf *result = NULL, *n; 2031 struct mbuf *result = NULL, *n;
2032 int len; 2032 int len;
2033 2033
2034 KASSERT(m != NULL); 2034 KASSERT(m != NULL);
2035 KASSERT(mhp != NULL); 2035 KASSERT(mhp != NULL);
2036 KASSERT(!cpu_softintr_p()); 2036 KASSERT(!cpu_softintr_p());
2037 2037
2038 va_start(ap, nitem); 2038 va_start(ap, nitem);
2039 for (i = 0; i < nitem; i++) { 2039 for (i = 0; i < nitem; i++) {
2040 idx = va_arg(ap, int); 2040 idx = va_arg(ap, int);
2041 KASSERT(idx >= 0); 2041 KASSERT(idx >= 0);
2042 KASSERT(idx <= SADB_EXT_MAX); 2042 KASSERT(idx <= SADB_EXT_MAX);
2043 /* don't attempt to pull empty extension */ 2043 /* don't attempt to pull empty extension */
2044 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL) 2044 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
2045 continue; 2045 continue;
2046 if (idx != SADB_EXT_RESERVED && 2046 if (idx != SADB_EXT_RESERVED &&
2047 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0)) 2047 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
2048 continue; 2048 continue;
2049 2049
2050 if (idx == SADB_EXT_RESERVED) { 2050 if (idx == SADB_EXT_RESERVED) {
2051 CTASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg)) <= MHLEN); 2051 CTASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg)) <= MHLEN);
2052 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); 2052 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2053 MGETHDR(n, M_WAITOK, MT_DATA); 2053 MGETHDR(n, M_WAITOK, MT_DATA);
2054 n->m_len = len; 2054 n->m_len = len;
2055 n->m_next = NULL; 2055 n->m_next = NULL;
2056 m_copydata(m, 0, sizeof(struct sadb_msg), 2056 m_copydata(m, 0, sizeof(struct sadb_msg),
2057 mtod(n, void *)); 2057 mtod(n, void *));
2058 } else if (i < ndeep) { 2058 } else if (i < ndeep) {
2059 len = mhp->extlen[idx]; 2059 len = mhp->extlen[idx];
2060 n = key_alloc_mbuf(len, M_WAITOK); 2060 n = key_alloc_mbuf(len, M_WAITOK);
2061 KASSERT(n->m_next == NULL); 2061 KASSERT(n->m_next == NULL);
2062 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx], 2062 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
2063 mtod(n, void *)); 2063 mtod(n, void *));
2064 } else { 2064 } else {
2065 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx], 2065 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
2066 M_WAITOK); 2066 M_WAITOK);
2067 } 2067 }
2068 KASSERT(n != NULL); 2068 KASSERT(n != NULL);
2069 2069
2070 if (result) 2070 if (result)
2071 m_cat(result, n); 2071 m_cat(result, n);
2072 else 2072 else
2073 result = n; 2073 result = n;
2074 } 2074 }
2075 va_end(ap); 2075 va_end(ap);
2076 2076
2077 KASSERT(result != NULL); 2077 KASSERT(result != NULL);
2078 if ((result->m_flags & M_PKTHDR) != 0) { 2078 if ((result->m_flags & M_PKTHDR) != 0) {
2079 result->m_pkthdr.len = 0; 2079 result->m_pkthdr.len = 0;
2080 for (n = result; n; n = n->m_next) 2080 for (n = result; n; n = n->m_next)
2081 result->m_pkthdr.len += n->m_len; 2081 result->m_pkthdr.len += n->m_len;
2082 KASSERT(result->m_len >= sizeof(struct sadb_msg)); 2082 KASSERT(result->m_len >= sizeof(struct sadb_msg));
2083 } 2083 }
2084 2084
2085 return result; 2085 return result;
2086} 2086}
2087 2087
2088/* 2088/*
2089 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing 2089 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
2090 * add an entry to SP database, when received 2090 * add an entry to SP database, when received
2091 * <base, address(SD), (lifetime(H),) policy> 2091 * <base, address(SD), (lifetime(H),) policy>
2092 * from the user(?). 2092 * from the user(?).
2093 * Adding to SP database, 2093 * Adding to SP database,
2094 * and send 2094 * and send
2095 * <base, address(SD), (lifetime(H),) policy> 2095 * <base, address(SD), (lifetime(H),) policy>
2096 * to the socket which was send. 2096 * to the socket which was send.
2097 * 2097 *
2098 * SPDADD set a unique policy entry. 2098 * SPDADD set a unique policy entry.
2099 * SPDSETIDX like SPDADD without a part of policy requests. 2099 * SPDSETIDX like SPDADD without a part of policy requests.
2100 * SPDUPDATE replace a unique policy entry. 2100 * SPDUPDATE replace a unique policy entry.
2101 * 2101 *
2102 * m will always be freed. 2102 * m will always be freed.
2103 */ 2103 */
2104static int 2104static int
2105key_api_spdadd(struct socket *so, struct mbuf *m, 2105key_api_spdadd(struct socket *so, struct mbuf *m,
2106 const struct sadb_msghdr *mhp) 2106 const struct sadb_msghdr *mhp)
2107{ 2107{
2108 const struct sockaddr *src, *dst; 2108 const struct sockaddr *src, *dst;
2109 const struct sadb_x_policy *xpl0; 2109 const struct sadb_x_policy *xpl0;
2110 struct sadb_x_policy *xpl; 2110 struct sadb_x_policy *xpl;
2111 const struct sadb_lifetime *lft = NULL; 2111 const struct sadb_lifetime *lft = NULL;
2112 struct secpolicyindex spidx; 2112 struct secpolicyindex spidx;
2113 struct secpolicy *newsp; 2113 struct secpolicy *newsp;
2114 int error; 2114 int error;
 2115 uint32_t sadb_x_policy_id;
2115 2116
2116 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || 2117 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2117 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || 2118 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2118 mhp->ext[SADB_X_EXT_POLICY] == NULL) { 2119 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2119 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); 2120 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
2120 return key_senderror(so, m, EINVAL); 2121 return key_senderror(so, m, EINVAL);
2121 } 2122 }
2122 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || 2123 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2123 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) || 2124 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2124 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { 2125 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2125 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); 2126 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
2126 return key_senderror(so, m, EINVAL); 2127 return key_senderror(so, m, EINVAL);
2127 } 2128 }
2128 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) { 2129 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
2129 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < 2130 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] <
2130 sizeof(struct sadb_lifetime)) { 2131 sizeof(struct sadb_lifetime)) {
2131 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); 2132 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
2132 return key_senderror(so, m, EINVAL); 2133 return key_senderror(so, m, EINVAL);
2133 } 2134 }
2134 lft = mhp->ext[SADB_EXT_LIFETIME_HARD]; 2135 lft = mhp->ext[SADB_EXT_LIFETIME_HARD];
2135 } 2136 }
2136 2137
2137 xpl0 = mhp->ext[SADB_X_EXT_POLICY]; 2138 xpl0 = mhp->ext[SADB_X_EXT_POLICY];
2138 2139
2139 /* checking the direciton. */ 2140 /* checking the direciton. */
2140 switch (xpl0->sadb_x_policy_dir) { 2141 switch (xpl0->sadb_x_policy_dir) {
2141 case IPSEC_DIR_INBOUND: 2142 case IPSEC_DIR_INBOUND:
2142 case IPSEC_DIR_OUTBOUND: 2143 case IPSEC_DIR_OUTBOUND:
2143 break; 2144 break;
2144 default: 2145 default:
2145 IPSECLOG(LOG_DEBUG, "Invalid SP direction.\n"); 2146 IPSECLOG(LOG_DEBUG, "Invalid SP direction.\n");
2146 return key_senderror(so, m, EINVAL); 2147 return key_senderror(so, m, EINVAL);
2147 } 2148 }
2148 2149
2149 /* check policy */ 2150 /* check policy */
2150 /* key_api_spdadd() accepts DISCARD, NONE and IPSEC. */ 2151 /* key_api_spdadd() accepts DISCARD, NONE and IPSEC. */
2151 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST || 2152 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST ||
2152 xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) { 2153 xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
2153 IPSECLOG(LOG_DEBUG, "Invalid policy type.\n"); 2154 IPSECLOG(LOG_DEBUG, "Invalid policy type.\n");
2154 return key_senderror(so, m, EINVAL); 2155 return key_senderror(so, m, EINVAL);
2155 } 2156 }
2156 2157
2157 /* policy requests are mandatory when action is ipsec. */ 2158 /* policy requests are mandatory when action is ipsec. */
2158 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX && 2159 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX &&
2159 xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC && 2160 xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2160 mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) { 2161 mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
2161 IPSECLOG(LOG_DEBUG, "some policy requests part required.\n"); 2162 IPSECLOG(LOG_DEBUG, "some policy requests part required.\n");
2162 return key_senderror(so, m, EINVAL); 2163 return key_senderror(so, m, EINVAL);
2163 } 2164 }
2164 2165
2165 src = key_msghdr_get_sockaddr(mhp, SADB_EXT_ADDRESS_SRC); 2166 src = key_msghdr_get_sockaddr(mhp, SADB_EXT_ADDRESS_SRC);
2166 dst = key_msghdr_get_sockaddr(mhp, SADB_EXT_ADDRESS_DST); 2167 dst = key_msghdr_get_sockaddr(mhp, SADB_EXT_ADDRESS_DST);
2167 2168
2168 /* sanity check on addr pair */ 2169 /* sanity check on addr pair */
2169 if (src->sa_family != dst->sa_family) 2170 if (src->sa_family != dst->sa_family)
2170 return key_senderror(so, m, EINVAL); 2171 return key_senderror(so, m, EINVAL);
2171 if (src->sa_len != dst->sa_len) 2172 if (src->sa_len != dst->sa_len)
2172 return key_senderror(so, m, EINVAL); 2173 return key_senderror(so, m, EINVAL);
2173 2174
2174 key_init_spidx_bymsghdr(&spidx, mhp); 2175 key_init_spidx_bymsghdr(&spidx, mhp);
2175 2176
2176 /* 2177 /*
2177 * checking there is SP already or not. 2178 * checking there is SP already or not.
2178 * SPDUPDATE doesn't depend on whether there is a SP or not. 2179 * SPDUPDATE doesn't depend on whether there is a SP or not.
2179 * If the type is either SPDADD or SPDSETIDX AND a SP is found, 2180 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
2180 * then error. 2181 * then error.
2181 */ 2182 */
2182 { 2183 {
2183 struct secpolicy *sp; 2184 struct secpolicy *sp;
2184 2185
2185 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) { 2186 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
2186 sp = key_lookup_and_remove_sp(&spidx); 2187 sp = key_lookup_and_remove_sp(&spidx);
2187 if (sp != NULL) 2188 if (sp != NULL)
2188 key_destroy_sp(sp); 2189 key_destroy_sp(sp);
2189 } else { 2190 } else {
2190 sp = key_getsp(&spidx); 2191 sp = key_getsp(&spidx);
2191 if (sp != NULL) { 2192 if (sp != NULL) {
2192 KEY_SP_UNREF(&sp); 2193 KEY_SP_UNREF(&sp);
2193 IPSECLOG(LOG_DEBUG, "a SP entry exists already.\n"); 2194 IPSECLOG(LOG_DEBUG, "a SP entry exists already.\n");
2194 return key_senderror(so, m, EEXIST); 2195 return key_senderror(so, m, EEXIST);
2195 } 2196 }
2196 } 2197 }
2197 } 2198 }
2198 2199
2199 /* allocation new SP entry */ 2200 /* allocation new SP entry */
2200 newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error); 2201 newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error);
2201 if (newsp == NULL) { 2202 if (newsp == NULL) {
2202 return key_senderror(so, m, error); 2203 return key_senderror(so, m, error);
2203 } 2204 }
2204 2205
2205 newsp->id = key_getnewspid(); 2206 newsp->id = key_getnewspid();
2206 if (newsp->id == 0) { 2207 if (newsp->id == 0) {
2207 kmem_free(newsp, sizeof(*newsp)); 2208 kmem_free(newsp, sizeof(*newsp));
2208 return key_senderror(so, m, ENOBUFS); 2209 return key_senderror(so, m, ENOBUFS);
2209 } 2210 }
2210 2211
2211 newsp->spidx = spidx; 2212 newsp->spidx = spidx;
2212 newsp->created = time_uptime; 2213 newsp->created = time_uptime;
2213 newsp->lastused = newsp->created; 2214 newsp->lastused = newsp->created;
2214 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0; 2215 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
2215 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0; 2216 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
2216 2217
2217 key_init_sp(newsp); 2218 key_init_sp(newsp);
2218 2219
 2220 sadb_x_policy_id = newsp->id;
 2221
2219 mutex_enter(&key_spd.lock); 2222 mutex_enter(&key_spd.lock);
2220 SPLIST_WRITER_INSERT_TAIL(newsp->spidx.dir, newsp); 2223 SPLIST_WRITER_INSERT_TAIL(newsp->spidx.dir, newsp);
2221 mutex_exit(&key_spd.lock); 2224 mutex_exit(&key_spd.lock);
 2225 /*
 2226 * We don't have a reference to newsp, so we must not touch newsp from
 2227 * now on. If you want to do, you must take a reference beforehand.
 2228 */
 2229 newsp = NULL;
2222 2230
2223#ifdef notyet 2231#ifdef notyet
2224 /* delete the entry in key_misc.spacqlist */ 2232 /* delete the entry in key_misc.spacqlist */
2225 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) { 2233 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
2226 struct secspacq *spacq = key_getspacq(&spidx); 2234 struct secspacq *spacq = key_getspacq(&spidx);
2227 if (spacq != NULL) { 2235 if (spacq != NULL) {
2228 /* reset counter in order to deletion by timehandler. */ 2236 /* reset counter in order to deletion by timehandler. */
2229 spacq->created = time_uptime; 2237 spacq->created = time_uptime;
2230 spacq->count = 0; 2238 spacq->count = 0;
2231 } 2239 }
2232 } 2240 }
2233#endif 2241#endif
2234 2242
2235 /* Invalidate all cached SPD pointers in the PCBs. */ 2243 /* Invalidate all cached SPD pointers in the PCBs. */
2236 ipsec_invalpcbcacheall(); 2244 ipsec_invalpcbcacheall();
2237 2245
2238#if defined(GATEWAY) 2246#if defined(GATEWAY)
2239 /* Invalidate the ipflow cache, as well. */ 2247 /* Invalidate the ipflow cache, as well. */
2240 ipflow_invalidate_all(0); 2248 ipflow_invalidate_all(0);
2241#ifdef INET6 2249#ifdef INET6
2242 if (in6_present) 2250 if (in6_present)
2243 ip6flow_invalidate_all(0); 2251 ip6flow_invalidate_all(0);
2244#endif /* INET6 */ 2252#endif /* INET6 */
2245#endif /* GATEWAY */ 2253#endif /* GATEWAY */
2246 2254
2247 key_update_used(); 2255 key_update_used();
2248 2256
2249 { 2257 {
2250 struct mbuf *n, *mpolicy; 2258 struct mbuf *n, *mpolicy;
2251 int off; 2259 int off;
2252 2260
2253 /* create new sadb_msg to reply. */ 2261 /* create new sadb_msg to reply. */
2254 if (lft) { 2262 if (lft) {
2255 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED, 2263 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
2256 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD, 2264 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
2257 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); 2265 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2258 } else { 2266 } else {
2259 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED, 2267 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
2260 SADB_X_EXT_POLICY, 2268 SADB_X_EXT_POLICY,
2261 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); 2269 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2262 } 2270 }
2263 2271
2264 key_fill_replymsg(n, 0); 2272 key_fill_replymsg(n, 0);
2265 off = 0; 2273 off = 0;
2266 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)), 2274 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
2267 sizeof(*xpl), &off); 2275 sizeof(*xpl), &off);
2268 if (mpolicy == NULL) { 2276 if (mpolicy == NULL) {
2269 /* n is already freed */ 2277 /* n is already freed */
2270 return key_senderror(so, m, ENOBUFS); 2278 return key_senderror(so, m, ENOBUFS);
2271 } 2279 }
2272 xpl = (struct sadb_x_policy *)(mtod(mpolicy, char *) + off); 2280 xpl = (struct sadb_x_policy *)(mtod(mpolicy, char *) + off);
2273 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) { 2281 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
2274 m_freem(n); 2282 m_freem(n);
2275 return key_senderror(so, m, EINVAL); 2283 return key_senderror(so, m, EINVAL);
2276 } 2284 }
2277 xpl->sadb_x_policy_id = newsp->id; 2285 xpl->sadb_x_policy_id = sadb_x_policy_id;
2278 2286
2279 m_freem(m); 2287 m_freem(m);
2280 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); 2288 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2281 } 2289 }
2282} 2290}
2283 2291
2284/* 2292/*
2285 * get new policy id. 2293 * get new policy id.
2286 * OUT: 2294 * OUT:
2287 * 0: failure. 2295 * 0: failure.
2288 * others: success. 2296 * others: success.
2289 */ 2297 */
2290static u_int32_t 2298static u_int32_t
2291key_getnewspid(void) 2299key_getnewspid(void)
2292{ 2300{
2293 u_int32_t newid = 0; 2301 u_int32_t newid = 0;
2294 int count = key_spi_trycnt; /* XXX */ 2302 int count = key_spi_trycnt; /* XXX */
2295 struct secpolicy *sp; 2303 struct secpolicy *sp;
2296 2304
2297 /* when requesting to allocate spi ranged */ 2305 /* when requesting to allocate spi ranged */
2298 while (count--) { 2306 while (count--) {
2299 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1)); 2307 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
2300 2308
2301 sp = key_getspbyid(newid); 2309 sp = key_getspbyid(newid);
2302 if (sp == NULL) 2310 if (sp == NULL)
2303 break; 2311 break;
2304 2312
2305 KEY_SP_UNREF(&sp); 2313 KEY_SP_UNREF(&sp);
2306 } 2314 }
2307 2315
2308 if (count == 0 || newid == 0) { 2316 if (count == 0 || newid == 0) {
2309 IPSECLOG(LOG_DEBUG, "to allocate policy id is failed.\n"); 2317 IPSECLOG(LOG_DEBUG, "to allocate policy id is failed.\n");
2310 return 0; 2318 return 0;
2311 } 2319 }
2312 2320
2313 return newid; 2321 return newid;
2314} 2322}
2315 2323
2316/* 2324/*
2317 * SADB_SPDDELETE processing 2325 * SADB_SPDDELETE processing
2318 * receive 2326 * receive
2319 * <base, address(SD), policy(*)> 2327 * <base, address(SD), policy(*)>
2320 * from the user(?), and set SADB_SASTATE_DEAD, 2328 * from the user(?), and set SADB_SASTATE_DEAD,
2321 * and send, 2329 * and send,
2322 * <base, address(SD), policy(*)> 2330 * <base, address(SD), policy(*)>
2323 * to the ikmpd. 2331 * to the ikmpd.
2324 * policy(*) including direction of policy. 2332 * policy(*) including direction of policy.
2325 * 2333 *
2326 * m will always be freed. 2334 * m will always be freed.
2327 */ 2335 */
2328static int 2336static int
2329key_api_spddelete(struct socket *so, struct mbuf *m, 2337key_api_spddelete(struct socket *so, struct mbuf *m,
2330 const struct sadb_msghdr *mhp) 2338 const struct sadb_msghdr *mhp)
2331{ 2339{
2332 struct sadb_x_policy *xpl0; 2340 struct sadb_x_policy *xpl0;
2333 struct secpolicyindex spidx; 2341 struct secpolicyindex spidx;
2334 struct secpolicy *sp; 2342 struct secpolicy *sp;
2335 2343
2336 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || 2344 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2337 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || 2345 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2338 mhp->ext[SADB_X_EXT_POLICY] == NULL) { 2346 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2339 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); 2347 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
2340 return key_senderror(so, m, EINVAL); 2348 return key_senderror(so, m, EINVAL);
2341 } 2349 }
2342 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || 2350 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2343 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) || 2351 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2344 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { 2352 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2345 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); 2353 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
2346 return key_senderror(so, m, EINVAL); 2354 return key_senderror(so, m, EINVAL);
2347 } 2355 }
2348 2356
2349 xpl0 = mhp->ext[SADB_X_EXT_POLICY]; 2357 xpl0 = mhp->ext[SADB_X_EXT_POLICY];
2350 2358
2351 /* checking the directon. */ 2359 /* checking the directon. */
2352 switch (xpl0->sadb_x_policy_dir) { 2360 switch (xpl0->sadb_x_policy_dir) {
2353 case IPSEC_DIR_INBOUND: 2361 case IPSEC_DIR_INBOUND:
2354 case IPSEC_DIR_OUTBOUND: 2362 case IPSEC_DIR_OUTBOUND:
2355 break; 2363 break;
2356 default: 2364 default:
2357 IPSECLOG(LOG_DEBUG, "Invalid SP direction.\n"); 2365 IPSECLOG(LOG_DEBUG, "Invalid SP direction.\n");
2358 return key_senderror(so, m, EINVAL); 2366 return key_senderror(so, m, EINVAL);
2359 } 2367 }
2360 2368
2361 /* make secindex */ 2369 /* make secindex */
2362 key_init_spidx_bymsghdr(&spidx, mhp); 2370 key_init_spidx_bymsghdr(&spidx, mhp);
2363 2371
2364 /* Is there SP in SPD ? */ 2372 /* Is there SP in SPD ? */
2365 sp = key_lookup_and_remove_sp(&spidx); 2373 sp = key_lookup_and_remove_sp(&spidx);
2366 if (sp == NULL) { 2374 if (sp == NULL) {
2367 IPSECLOG(LOG_DEBUG, "no SP found.\n"); 2375 IPSECLOG(LOG_DEBUG, "no SP found.\n");
2368 return key_senderror(so, m, EINVAL); 2376 return key_senderror(so, m, EINVAL);
2369 } 2377 }
2370 2378
2371 /* save policy id to buffer to be returned. */ 2379 /* save policy id to buffer to be returned. */
2372 xpl0->sadb_x_policy_id = sp->id; 2380 xpl0->sadb_x_policy_id = sp->id;
2373 2381
2374 key_destroy_sp(sp); 2382 key_destroy_sp(sp);
2375 2383
2376 /* We're deleting policy; no need to invalidate the ipflow cache. */ 2384 /* We're deleting policy; no need to invalidate the ipflow cache. */
2377 2385
2378 { 2386 {
2379 struct mbuf *n; 2387 struct mbuf *n;
2380 2388
2381 /* create new sadb_msg to reply. */ 2389 /* create new sadb_msg to reply. */
2382 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED, 2390 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2383 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); 2391 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2384 key_fill_replymsg(n, 0); 2392 key_fill_replymsg(n, 0);
2385 m_freem(m); 2393 m_freem(m);
2386 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); 2394 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2387 } 2395 }
2388} 2396}
2389 2397
2390static struct mbuf * 2398static struct mbuf *
2391key_alloc_mbuf_simple(int len, int mflag) 2399key_alloc_mbuf_simple(int len, int mflag)
2392{ 2400{
2393 struct mbuf *n; 2401 struct mbuf *n;
2394 2402
2395 KASSERT(mflag == M_NOWAIT || (mflag == M_WAITOK && !cpu_softintr_p())); 2403 KASSERT(mflag == M_NOWAIT || (mflag == M_WAITOK && !cpu_softintr_p()));
2396 2404
2397 MGETHDR(n, mflag, MT_DATA); 2405 MGETHDR(n, mflag, MT_DATA);
2398 if (n && len > MHLEN) { 2406 if (n && len > MHLEN) {
2399 MCLGET(n, mflag); 2407 MCLGET(n, mflag);
2400 if ((n->m_flags & M_EXT) == 0) { 2408 if ((n->m_flags & M_EXT) == 0) {
2401 m_freem(n); 2409 m_freem(n);
2402 n = NULL; 2410 n = NULL;
2403 } 2411 }
2404 } 2412 }
2405 return n; 2413 return n;
2406} 2414}
2407 2415
2408/* 2416/*
2409 * SADB_SPDDELETE2 processing 2417 * SADB_SPDDELETE2 processing
2410 * receive 2418 * receive
2411 * <base, policy(*)> 2419 * <base, policy(*)>
2412 * from the user(?), and set SADB_SASTATE_DEAD, 2420 * from the user(?), and set SADB_SASTATE_DEAD,
2413 * and send, 2421 * and send,
2414 * <base, policy(*)> 2422 * <base, policy(*)>
2415 * to the ikmpd. 2423 * to the ikmpd.
2416 * policy(*) including direction of policy. 2424 * policy(*) including direction of policy.
2417 * 2425 *
2418 * m will always be freed. 2426 * m will always be freed.
2419 */ 2427 */
2420static int 2428static int
2421key_api_spddelete2(struct socket *so, struct mbuf *m, 2429key_api_spddelete2(struct socket *so, struct mbuf *m,
2422 const struct sadb_msghdr *mhp) 2430 const struct sadb_msghdr *mhp)
2423{ 2431{
2424 u_int32_t id; 2432 u_int32_t id;
2425 struct secpolicy *sp; 2433 struct secpolicy *sp;
2426 const struct sadb_x_policy *xpl; 2434 const struct sadb_x_policy *xpl;
2427 2435
2428 if (mhp->ext[SADB_X_EXT_POLICY] == NULL || 2436 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2429 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { 2437 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2430 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); 2438 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
2431 return key_senderror(so, m, EINVAL); 2439 return key_senderror(so, m, EINVAL);
2432 } 2440 }
2433 2441
2434 xpl = mhp->ext[SADB_X_EXT_POLICY]; 2442 xpl = mhp->ext[SADB_X_EXT_POLICY];
2435 id = xpl->sadb_x_policy_id; 2443 id = xpl->sadb_x_policy_id;
2436 2444
2437 /* Is there SP in SPD ? */ 2445 /* Is there SP in SPD ? */
2438 sp = key_lookupbyid_and_remove_sp(id); 2446 sp = key_lookupbyid_and_remove_sp(id);
2439 if (sp == NULL) { 2447 if (sp == NULL) {
2440 IPSECLOG(LOG_DEBUG, "no SP found id:%u.\n", id); 2448 IPSECLOG(LOG_DEBUG, "no SP found id:%u.\n", id);
2441 return key_senderror(so, m, EINVAL); 2449 return key_senderror(so, m, EINVAL);
2442 } 2450 }
2443 2451
2444 key_destroy_sp(sp); 2452 key_destroy_sp(sp);
2445 2453
2446 /* We're deleting policy; no need to invalidate the ipflow cache. */ 2454 /* We're deleting policy; no need to invalidate the ipflow cache. */
2447 2455
2448 { 2456 {
2449 struct mbuf *n, *nn; 2457 struct mbuf *n, *nn;
2450 int off, len; 2458 int off, len;
2451 2459
2452 CTASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg)) <= MCLBYTES); 2460 CTASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg)) <= MCLBYTES);
2453 2461
2454 /* create new sadb_msg to reply. */ 2462 /* create new sadb_msg to reply. */
2455 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); 2463 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2456 2464
2457 n = key_alloc_mbuf_simple(len, M_WAITOK); 2465 n = key_alloc_mbuf_simple(len, M_WAITOK);
2458 n->m_len = len; 2466 n->m_len = len;
2459 n->m_next = NULL; 2467 n->m_next = NULL;
2460 off = 0; 2468 off = 0;
2461 2469
2462 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, char *) + off); 2470 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, char *) + off);
2463 off += PFKEY_ALIGN8(sizeof(struct sadb_msg)); 2471 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2464 2472
2465 KASSERTMSG(off == len, "length inconsistency"); 2473 KASSERTMSG(off == len, "length inconsistency");
2466 2474
2467 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY], 2475 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2468 mhp->extlen[SADB_X_EXT_POLICY], M_WAITOK); 2476 mhp->extlen[SADB_X_EXT_POLICY], M_WAITOK);
2469 2477
2470 n->m_pkthdr.len = 0; 2478 n->m_pkthdr.len = 0;
2471 for (nn = n; nn; nn = nn->m_next) 2479 for (nn = n; nn; nn = nn->m_next)
2472 n->m_pkthdr.len += nn->m_len; 2480 n->m_pkthdr.len += nn->m_len;
2473 2481
2474 key_fill_replymsg(n, 0); 2482 key_fill_replymsg(n, 0);
2475 m_freem(m); 2483 m_freem(m);
2476 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); 2484 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2477 } 2485 }
2478} 2486}
2479 2487
2480/* 2488/*
2481 * SADB_X_GET processing 2489 * SADB_X_GET processing
2482 * receive 2490 * receive
2483 * <base, policy(*)> 2491 * <base, policy(*)>
2484 * from the user(?), 2492 * from the user(?),
2485 * and send, 2493 * and send,
2486 * <base, address(SD), policy> 2494 * <base, address(SD), policy>
2487 * to the ikmpd. 2495 * to the ikmpd.
2488 * policy(*) including direction of policy. 2496 * policy(*) including direction of policy.
2489 * 2497 *
2490 * m will always be freed. 2498 * m will always be freed.
2491 */ 2499 */
2492static int 2500static int
2493key_api_spdget(struct socket *so, struct mbuf *m, 2501key_api_spdget(struct socket *so, struct mbuf *m,
2494 const struct sadb_msghdr *mhp) 2502 const struct sadb_msghdr *mhp)
2495{ 2503{
2496 u_int32_t id; 2504 u_int32_t id;
2497 struct secpolicy *sp; 2505 struct secpolicy *sp;
2498 struct mbuf *n; 2506 struct mbuf *n;
2499 const struct sadb_x_policy *xpl; 2507 const struct sadb_x_policy *xpl;
2500 2508
2501 if (mhp->ext[SADB_X_EXT_POLICY] == NULL || 2509 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2502 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { 2510 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2503 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); 2511 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
2504 return key_senderror(so, m, EINVAL); 2512 return key_senderror(so, m, EINVAL);
2505 } 2513 }
2506 2514
2507 xpl = mhp->ext[SADB_X_EXT_POLICY]; 2515 xpl = mhp->ext[SADB_X_EXT_POLICY];
2508 id = xpl->sadb_x_policy_id; 2516 id = xpl->sadb_x_policy_id;
2509 2517
2510 /* Is there SP in SPD ? */ 2518 /* Is there SP in SPD ? */
2511 sp = key_getspbyid(id); 2519 sp = key_getspbyid(id);
2512 if (sp == NULL) { 2520 if (sp == NULL) {
2513 IPSECLOG(LOG_DEBUG, "no SP found id:%u.\n", id); 2521 IPSECLOG(LOG_DEBUG, "no SP found id:%u.\n", id);
2514 return key_senderror(so, m, ENOENT); 2522 return key_senderror(so, m, ENOENT);
2515 } 2523 }
2516 2524
2517 n = key_setdumpsp(sp, SADB_X_SPDGET, mhp->msg->sadb_msg_seq, 2525 n = key_setdumpsp(sp, SADB_X_SPDGET, mhp->msg->sadb_msg_seq,
2518 mhp->msg->sadb_msg_pid); 2526 mhp->msg->sadb_msg_pid);
2519 KEY_SP_UNREF(&sp); /* ref gained by key_getspbyid */ 2527 KEY_SP_UNREF(&sp); /* ref gained by key_getspbyid */
2520 m_freem(m); 2528 m_freem(m);
2521 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE); 2529 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2522} 2530}
2523 2531
2524#ifdef notyet 2532#ifdef notyet
2525/* 2533/*
2526 * SADB_X_SPDACQUIRE processing. 2534 * SADB_X_SPDACQUIRE processing.
2527 * Acquire policy and SA(s) for a *OUTBOUND* packet. 2535 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2528 * send 2536 * send
2529 * <base, policy(*)> 2537 * <base, policy(*)>
2530 * to KMD, and expect to receive 2538 * to KMD, and expect to receive
2531 * <base> with SADB_X_SPDACQUIRE if error occurred, 2539 * <base> with SADB_X_SPDACQUIRE if error occurred,
2532 * or 2540 * or
2533 * <base, policy> 2541 * <base, policy>
2534 * with SADB_X_SPDUPDATE from KMD by PF_KEY. 2542 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2535 * policy(*) is without policy requests. 2543 * policy(*) is without policy requests.
2536 * 2544 *
2537 * 0 : succeed 2545 * 0 : succeed
2538 * others: error number 2546 * others: error number
2539 */ 2547 */
2540int 2548int
2541key_spdacquire(const struct secpolicy *sp) 2549key_spdacquire(const struct secpolicy *sp)
2542{ 2550{
2543 struct mbuf *result = NULL, *m; 2551 struct mbuf *result = NULL, *m;
2544 struct secspacq *newspacq; 2552 struct secspacq *newspacq;
2545 int error; 2553 int error;
2546 2554
2547 KASSERT(sp != NULL); 2555 KASSERT(sp != NULL);
2548 KASSERTMSG(sp->req == NULL, "called but there is request"); 2556 KASSERTMSG(sp->req == NULL, "called but there is request");
2549 KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC, 2557 KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
2550 "policy mismathed. IPsec is expected"); 2558 "policy mismathed. IPsec is expected");
2551 2559
2552 /* Get an entry to check whether sent message or not. */ 2560 /* Get an entry to check whether sent message or not. */
2553 newspacq = key_getspacq(&sp->spidx); 2561 newspacq = key_getspacq(&sp->spidx);
2554 if (newspacq != NULL) { 2562 if (newspacq != NULL) {
2555 if (key_blockacq_count < newspacq->count) { 2563 if (key_blockacq_count < newspacq->count) {
2556 /* reset counter and do send message. */ 2564 /* reset counter and do send message. */
2557 newspacq->count = 0; 2565 newspacq->count = 0;
2558 } else { 2566 } else {
2559 /* increment counter and do nothing. */ 2567 /* increment counter and do nothing. */
2560 newspacq->count++; 2568 newspacq->count++;
2561 return 0; 2569 return 0;
2562 } 2570 }
2563 } else { 2571 } else {
2564 /* make new entry for blocking to send SADB_ACQUIRE. */ 2572 /* make new entry for blocking to send SADB_ACQUIRE. */
2565 newspacq = key_newspacq(&sp->spidx); 2573 newspacq = key_newspacq(&sp->spidx);
2566 if (newspacq == NULL) 2574 if (newspacq == NULL)
2567 return ENOBUFS; 2575 return ENOBUFS;
2568 2576
2569 /* add to key_misc.acqlist */ 2577 /* add to key_misc.acqlist */
2570 LIST_INSERT_HEAD(&key_misc.spacqlist, newspacq, chain); 2578 LIST_INSERT_HEAD(&key_misc.spacqlist, newspacq, chain);
2571 } 2579 }
2572 2580
2573 /* create new sadb_msg to reply. */ 2581 /* create new sadb_msg to reply. */
2574 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0); 2582 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2575 if (!m) { 2583 if (!m) {
2576 error = ENOBUFS; 2584 error = ENOBUFS;
2577 goto fail; 2585 goto fail;
2578 } 2586 }
2579 result = m; 2587 result = m;
2580 2588
2581 result->m_pkthdr.len = 0; 2589 result->m_pkthdr.len = 0;
2582 for (m = result; m; m = m->m_next) 2590 for (m = result; m; m = m->m_next)
2583 result->m_pkthdr.len += m->m_len; 2591 result->m_pkthdr.len += m->m_len;
2584 2592
2585 mtod(result, struct sadb_msg *)->sadb_msg_len = 2593 mtod(result, struct sadb_msg *)->sadb_msg_len =
2586 PFKEY_UNIT64(result->m_pkthdr.len); 2594 PFKEY_UNIT64(result->m_pkthdr.len);
2587 2595
2588 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED); 2596 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2589 2597
2590fail: 2598fail:
2591 if (result) 2599 if (result)
2592 m_freem(result); 2600 m_freem(result);
2593 return error; 2601 return error;
2594} 2602}
2595#endif /* notyet */ 2603#endif /* notyet */
2596 2604
2597/* 2605/*
2598 * SADB_SPDFLUSH processing 2606 * SADB_SPDFLUSH processing
2599 * receive 2607 * receive
2600 * <base> 2608 * <base>
2601 * from the user, and free all entries in secpctree. 2609 * from the user, and free all entries in secpctree.
2602 * and send, 2610 * and send,
2603 * <base> 2611 * <base>
2604 * to the user. 2612 * to the user.
2605 * NOTE: what to do is only marking SADB_SASTATE_DEAD. 2613 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2606 * 2614 *
2607 * m will always be freed. 2615 * m will always be freed.
2608 */ 2616 */
2609static int 2617static int
2610key_api_spdflush(struct socket *so, struct mbuf *m, 2618key_api_spdflush(struct socket *so, struct mbuf *m,
2611 const struct sadb_msghdr *mhp) 2619 const struct sadb_msghdr *mhp)
2612{ 2620{
2613 struct sadb_msg *newmsg; 2621 struct sadb_msg *newmsg;
2614 struct secpolicy *sp; 2622 struct secpolicy *sp;
2615 u_int dir; 2623 u_int dir;
2616 2624
2617 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg))) 2625 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2618 return key_senderror(so, m, EINVAL); 2626 return key_senderror(so, m, EINVAL);
2619 2627
2620 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { 2628 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2621 retry: 2629 retry:
2622 mutex_enter(&key_spd.lock); 2630 mutex_enter(&key_spd.lock);
2623 SPLIST_WRITER_FOREACH(sp, dir) { 2631 SPLIST_WRITER_FOREACH(sp, dir) {
2624 KASSERT(sp->state != IPSEC_SPSTATE_DEAD); 2632 KASSERT(sp->state != IPSEC_SPSTATE_DEAD);
2625 key_unlink_sp(sp); 2633 key_unlink_sp(sp);
2626 mutex_exit(&key_spd.lock); 2634 mutex_exit(&key_spd.lock);
2627 key_destroy_sp(sp); 2635 key_destroy_sp(sp);
2628 goto retry; 2636 goto retry;
2629 } 2637 }
2630 mutex_exit(&key_spd.lock); 2638 mutex_exit(&key_spd.lock);
2631 } 2639 }
2632 2640
2633 /* We're deleting policy; no need to invalidate the ipflow cache. */ 2641 /* We're deleting policy; no need to invalidate the ipflow cache. */
2634 2642
2635 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) { 2643 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2636 IPSECLOG(LOG_DEBUG, "No more memory.\n"); 2644 IPSECLOG(LOG_DEBUG, "No more memory.\n");
2637 return key_senderror(so, m, ENOBUFS); 2645 return key_senderror(so, m, ENOBUFS);
2638 } 2646 }
2639 2647
2640 if (m->m_next) 2648 if (m->m_next)
2641 m_freem(m->m_next); 2649 m_freem(m->m_next);
2642 m->m_next = NULL; 2650 m->m_next = NULL;
2643 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); 2651 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2644 newmsg = mtod(m, struct sadb_msg *); 2652 newmsg = mtod(m, struct sadb_msg *);
2645 newmsg->sadb_msg_errno = 0; 2653 newmsg->sadb_msg_errno = 0;
2646 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len); 2654 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2647 2655
2648 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL); 2656 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2649} 2657}
2650 2658
2651static struct sockaddr key_src = { 2659static struct sockaddr key_src = {
2652 .sa_len = 2, 2660 .sa_len = 2,
2653 .sa_family = PF_KEY, 2661 .sa_family = PF_KEY,
2654}; 2662};
2655 2663
2656static struct mbuf * 2664static struct mbuf *
2657key_setspddump_chain(int *errorp, int *lenp, pid_t pid) 2665key_setspddump_chain(int *errorp, int *lenp, pid_t pid)
2658{ 2666{
2659 struct secpolicy *sp; 2667 struct secpolicy *sp;
2660 int cnt; 2668 int cnt;
2661 u_int dir; 2669 u_int dir;
2662 struct mbuf *m, *n, *prev; 2670 struct mbuf *m, *n, *prev;
2663 int totlen; 2671 int totlen;
2664 2672
2665 KASSERT(mutex_owned(&key_spd.lock)); 2673 KASSERT(mutex_owned(&key_spd.lock));
2666 2674
2667 *lenp = 0; 2675 *lenp = 0;
2668 2676
2669 /* search SPD entry and get buffer size. */ 2677 /* search SPD entry and get buffer size. */
2670 cnt = 0; 2678 cnt = 0;
2671 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { 2679 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2672 SPLIST_WRITER_FOREACH(sp, dir) { 2680 SPLIST_WRITER_FOREACH(sp, dir) {
2673 cnt++; 2681 cnt++;
2674 } 2682 }
2675 } 2683 }
2676 2684
2677 if (cnt == 0) { 2685 if (cnt == 0) {
2678 *errorp = ENOENT; 2686 *errorp = ENOENT;
2679 return (NULL); 2687 return (NULL);
2680 } 2688 }
2681 2689
2682 m = NULL; 2690 m = NULL;
2683 prev = m; 2691 prev = m;
2684 totlen = 0; 2692 totlen = 0;
2685 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { 2693 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2686 SPLIST_WRITER_FOREACH(sp, dir) { 2694 SPLIST_WRITER_FOREACH(sp, dir) {
2687 --cnt; 2695 --cnt;
2688 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt, pid); 2696 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt, pid);
2689 2697
2690 totlen += n->m_pkthdr.len; 2698 totlen += n->m_pkthdr.len;
2691 if (!m) { 2699 if (!m) {
2692 m = n; 2700 m = n;
2693 } else { 2701 } else {
2694 prev->m_nextpkt = n; 2702 prev->m_nextpkt = n;
2695 } 2703 }
2696 prev = n; 2704 prev = n;
2697 } 2705 }
2698 } 2706 }
2699 2707
2700 *lenp = totlen; 2708 *lenp = totlen;
2701 *errorp = 0; 2709 *errorp = 0;
2702 return (m); 2710 return (m);
2703} 2711}
2704 2712
2705/* 2713/*
2706 * SADB_SPDDUMP processing 2714 * SADB_SPDDUMP processing
2707 * receive 2715 * receive
2708 * <base> 2716 * <base>
2709 * from the user, and dump all SP leaves 2717 * from the user, and dump all SP leaves
2710 * and send, 2718 * and send,
2711 * <base> ..... 2719 * <base> .....
2712 * to the ikmpd. 2720 * to the ikmpd.
2713 * 2721 *
2714 * m will always be freed. 2722 * m will always be freed.
2715 */ 2723 */
2716static int 2724static int
2717key_api_spddump(struct socket *so, struct mbuf *m0, 2725key_api_spddump(struct socket *so, struct mbuf *m0,
2718 const struct sadb_msghdr *mhp) 2726 const struct sadb_msghdr *mhp)
2719{ 2727{
2720 struct mbuf *n; 2728 struct mbuf *n;
2721 int error, len; 2729 int error, len;
2722 int ok; 2730 int ok;
2723 pid_t pid; 2731 pid_t pid;
2724 2732
2725 pid = mhp->msg->sadb_msg_pid; 2733 pid = mhp->msg->sadb_msg_pid;
2726 /* 2734 /*
2727 * If the requestor has insufficient socket-buffer space 2735 * If the requestor has insufficient socket-buffer space
2728 * for the entire chain, nobody gets any response to the DUMP. 2736 * for the entire chain, nobody gets any response to the DUMP.
2729 * XXX For now, only the requestor ever gets anything. 2737 * XXX For now, only the requestor ever gets anything.
2730 * Moreover, if the requestor has any space at all, they receive 2738 * Moreover, if the requestor has any space at all, they receive
2731 * the entire chain, otherwise the request is refused with ENOBUFS. 2739 * the entire chain, otherwise the request is refused with ENOBUFS.
2732 */ 2740 */
2733 if (sbspace(&so->so_rcv) <= 0) { 2741 if (sbspace(&so->so_rcv) <= 0) {
2734 return key_senderror(so, m0, ENOBUFS); 2742 return key_senderror(so, m0, ENOBUFS);
2735 } 2743 }
2736 2744
2737 mutex_enter(&key_spd.lock); 2745 mutex_enter(&key_spd.lock);
2738 n = key_setspddump_chain(&error, &len, pid); 2746 n = key_setspddump_chain(&error, &len, pid);
2739 mutex_exit(&key_spd.lock); 2747 mutex_exit(&key_spd.lock);
2740 2748
2741 if (n == NULL) { 2749 if (n == NULL) {
2742 return key_senderror(so, m0, ENOENT); 2750 return key_senderror(so, m0, ENOENT);
2743 } 2751 }
2744 { 2752 {
2745 uint64_t *ps = PFKEY_STAT_GETREF(); 2753 uint64_t *ps = PFKEY_STAT_GETREF();
2746 ps[PFKEY_STAT_IN_TOTAL]++; 2754 ps[PFKEY_STAT_IN_TOTAL]++;
2747 ps[PFKEY_STAT_IN_BYTES] += len; 2755 ps[PFKEY_STAT_IN_BYTES] += len;
2748 PFKEY_STAT_PUTREF(); 2756 PFKEY_STAT_PUTREF();
2749 } 2757 }
2750 2758
2751 /* 2759 /*
2752 * PF_KEY DUMP responses are no longer broadcast to all PF_KEY sockets. 2760 * PF_KEY DUMP responses are no longer broadcast to all PF_KEY sockets.
2753 * The requestor receives either the entire chain, or an 2761 * The requestor receives either the entire chain, or an
2754 * error message with ENOBUFS. 2762 * error message with ENOBUFS.
2755 */ 2763 */
2756 2764
2757 /* 2765 /*
2758 * sbappendchainwith record takes the chain of entries, one 2766 * sbappendchainwith record takes the chain of entries, one
2759 * packet-record per SPD entry, prepends the key_src sockaddr 2767 * packet-record per SPD entry, prepends the key_src sockaddr
2760 * to each packet-record, links the sockaddr mbufs into a new 2768 * to each packet-record, links the sockaddr mbufs into a new
2761 * list of records, then appends the entire resulting 2769 * list of records, then appends the entire resulting
2762 * list to the requesting socket. 2770 * list to the requesting socket.
2763 */ 2771 */
2764 ok = sbappendaddrchain(&so->so_rcv, (struct sockaddr *)&key_src, n, 2772 ok = sbappendaddrchain(&so->so_rcv, (struct sockaddr *)&key_src, n,
2765 SB_PRIO_ONESHOT_OVERFLOW); 2773 SB_PRIO_ONESHOT_OVERFLOW);
2766 2774
2767 if (!ok) { 2775 if (!ok) {
2768 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM); 2776 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
2769 m_freem(n); 2777 m_freem(n);
2770 return key_senderror(so, m0, ENOBUFS); 2778 return key_senderror(so, m0, ENOBUFS);
2771 } 2779 }
2772 2780
2773 m_freem(m0); 2781 m_freem(m0);
2774 return error; 2782 return error;
2775} 2783}
2776 2784
2777/* 2785/*
2778 * SADB_X_NAT_T_NEW_MAPPING. Unused by racoon as of 2005/04/23 2786 * SADB_X_NAT_T_NEW_MAPPING. Unused by racoon as of 2005/04/23
2779 */ 2787 */
2780static int 2788static int
2781key_api_nat_map(struct socket *so, struct mbuf *m, 2789key_api_nat_map(struct socket *so, struct mbuf *m,
2782 const struct sadb_msghdr *mhp) 2790 const struct sadb_msghdr *mhp)
2783{ 2791{
2784 struct sadb_x_nat_t_type *type; 2792 struct sadb_x_nat_t_type *type;
2785 struct sadb_x_nat_t_port *sport; 2793 struct sadb_x_nat_t_port *sport;
2786 struct sadb_x_nat_t_port *dport; 2794 struct sadb_x_nat_t_port *dport;
2787 struct sadb_address *iaddr, *raddr; 2795 struct sadb_address *iaddr, *raddr;
2788 struct sadb_x_nat_t_frag *frag; 2796 struct sadb_x_nat_t_frag *frag;
2789 2797
2790 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] == NULL || 2798 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] == NULL ||
2791 mhp->ext[SADB_X_EXT_NAT_T_SPORT] == NULL || 2799 mhp->ext[SADB_X_EXT_NAT_T_SPORT] == NULL ||
2792 mhp->ext[SADB_X_EXT_NAT_T_DPORT] == NULL) { 2800 mhp->ext[SADB_X_EXT_NAT_T_DPORT] == NULL) {
2793 IPSECLOG(LOG_DEBUG, "invalid message.\n"); 2801 IPSECLOG(LOG_DEBUG, "invalid message.\n");
2794 return key_senderror(so, m, EINVAL); 2802 return key_senderror(so, m, EINVAL);
2795 } 2803 }
2796 if ((mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type)) || 2804 if ((mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type)) ||
2797 (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport)) || 2805 (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport)) ||
2798 (mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport))) { 2806 (mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport))) {
2799 IPSECLOG(LOG_DEBUG, "invalid message.\n"); 2807 IPSECLOG(LOG_DEBUG, "invalid message.\n");
2800 return key_senderror(so, m, EINVAL); 2808 return key_senderror(so, m, EINVAL);
2801 } 2809 }
2802 2810
2803 if ((mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL) && 2811 if ((mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL) &&
2804 (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr))) { 2812 (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr))) {
2805 IPSECLOG(LOG_DEBUG, "invalid message\n"); 2813 IPSECLOG(LOG_DEBUG, "invalid message\n");
2806 return key_senderror(so, m, EINVAL); 2814 return key_senderror(so, m, EINVAL);
2807 } 2815 }
2808 2816
2809 if ((mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) && 2817 if ((mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) &&
2810 (mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr))) { 2818 (mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr))) {
2811 IPSECLOG(LOG_DEBUG, "invalid message\n"); 2819 IPSECLOG(LOG_DEBUG, "invalid message\n");
2812 return key_senderror(so, m, EINVAL); 2820 return key_senderror(so, m, EINVAL);
2813 } 2821 }
2814 2822
2815 if ((mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) && 2823 if ((mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) &&
2816 (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag))) { 2824 (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag))) {
2817 IPSECLOG(LOG_DEBUG, "invalid message\n"); 2825 IPSECLOG(LOG_DEBUG, "invalid message\n");
2818 return key_senderror(so, m, EINVAL); 2826 return key_senderror(so, m, EINVAL);
2819 } 2827 }
2820 2828
2821 type = mhp->ext[SADB_X_EXT_NAT_T_TYPE]; 2829 type = mhp->ext[SADB_X_EXT_NAT_T_TYPE];
2822 sport = mhp->ext[SADB_X_EXT_NAT_T_SPORT]; 2830 sport = mhp->ext[SADB_X_EXT_NAT_T_SPORT];
2823 dport = mhp->ext[SADB_X_EXT_NAT_T_DPORT]; 2831 dport = mhp->ext[SADB_X_EXT_NAT_T_DPORT];
2824 iaddr = mhp->ext[SADB_X_EXT_NAT_T_OAI]; 2832 iaddr = mhp->ext[SADB_X_EXT_NAT_T_OAI];
2825 raddr = mhp->ext[SADB_X_EXT_NAT_T_OAR]; 2833 raddr = mhp->ext[SADB_X_EXT_NAT_T_OAR];
2826 frag = mhp->ext[SADB_X_EXT_NAT_T_FRAG]; 2834 frag = mhp->ext[SADB_X_EXT_NAT_T_FRAG];
2827 2835
2828 /* 2836 /*
2829 * XXX handle that, it should also contain a SA, or anything 2837 * XXX handle that, it should also contain a SA, or anything
2830 * that enable to update the SA information. 2838 * that enable to update the SA information.
2831 */ 2839 */
2832 2840
2833 return 0; 2841 return 0;
2834} 2842}
2835 2843
2836/* 2844/*
2837 * Never return NULL. 2845 * Never return NULL.
2838 */ 2846 */
2839static struct mbuf * 2847static struct mbuf *
2840key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq, pid_t pid) 2848key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq, pid_t pid)
2841{ 2849{
2842 struct mbuf *result = NULL, *m; 2850 struct mbuf *result = NULL, *m;
2843 2851
2844 KASSERT(!cpu_softintr_p()); 2852 KASSERT(!cpu_softintr_p());
2845 2853
2846 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, 2854 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid,
2847 key_sp_refcnt(sp), M_WAITOK); 2855 key_sp_refcnt(sp), M_WAITOK);
2848 result = m; 2856 result = m;
2849 2857
2850 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, 2858 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2851 &sp->spidx.src.sa, sp->spidx.prefs, sp->spidx.ul_proto, M_WAITOK); 2859 &sp->spidx.src.sa, sp->spidx.prefs, sp->spidx.ul_proto, M_WAITOK);
2852 m_cat(result, m); 2860 m_cat(result, m);
2853 2861
2854 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, 2862 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2855 &sp->spidx.dst.sa, sp->spidx.prefd, sp->spidx.ul_proto, M_WAITOK); 2863 &sp->spidx.dst.sa, sp->spidx.prefd, sp->spidx.ul_proto, M_WAITOK);
2856 m_cat(result, m); 2864 m_cat(result, m);
2857 2865
2858 m = key_sp2msg(sp, M_WAITOK); 2866 m = key_sp2msg(sp, M_WAITOK);
2859 m_cat(result, m); 2867 m_cat(result, m);
2860 2868
2861 KASSERT(result->m_flags & M_PKTHDR); 2869 KASSERT(result->m_flags & M_PKTHDR);
2862 KASSERT(result->m_len >= sizeof(struct sadb_msg)); 2870 KASSERT(result->m_len >= sizeof(struct sadb_msg));
2863 2871
2864 result->m_pkthdr.len = 0; 2872 result->m_pkthdr.len = 0;
2865 for (m = result; m; m = m->m_next) 2873 for (m = result; m; m = m->m_next)
2866 result->m_pkthdr.len += m->m_len; 2874 result->m_pkthdr.len += m->m_len;
2867 2875
2868 mtod(result, struct sadb_msg *)->sadb_msg_len = 2876 mtod(result, struct sadb_msg *)->sadb_msg_len =
2869 PFKEY_UNIT64(result->m_pkthdr.len); 2877 PFKEY_UNIT64(result->m_pkthdr.len);
2870 2878
2871 return result; 2879 return result;
2872} 2880}
2873 2881
2874/* 2882/*
2875 * get PFKEY message length for security policy and request. 2883 * get PFKEY message length for security policy and request.
2876 */ 2884 */
2877static u_int 2885static u_int
2878key_getspreqmsglen(const struct secpolicy *sp) 2886key_getspreqmsglen(const struct secpolicy *sp)
2879{ 2887{
2880 u_int tlen; 2888 u_int tlen;
2881 2889
2882 tlen = sizeof(struct sadb_x_policy); 2890 tlen = sizeof(struct sadb_x_policy);
2883 2891
2884 /* if is the policy for ipsec ? */ 2892 /* if is the policy for ipsec ? */
2885 if (sp->policy != IPSEC_POLICY_IPSEC) 2893 if (sp->policy != IPSEC_POLICY_IPSEC)
2886 return tlen; 2894 return tlen;
2887 2895
2888 /* get length of ipsec requests */ 2896 /* get length of ipsec requests */
2889 { 2897 {
2890 const struct ipsecrequest *isr; 2898 const struct ipsecrequest *isr;
2891 int len; 2899 int len;
2892 2900
2893 for (isr = sp->req; isr != NULL; isr = isr->next) { 2901 for (isr = sp->req; isr != NULL; isr = isr->next) {
2894 len = sizeof(struct sadb_x_ipsecrequest) 2902 len = sizeof(struct sadb_x_ipsecrequest)
2895 + isr->saidx.src.sa.sa_len + isr->saidx.dst.sa.sa_len; 2903 + isr->saidx.src.sa.sa_len + isr->saidx.dst.sa.sa_len;
2896 2904
2897 tlen += PFKEY_ALIGN8(len); 2905 tlen += PFKEY_ALIGN8(len);
2898 } 2906 }
2899 } 2907 }
2900 2908
2901 return tlen; 2909 return tlen;
2902} 2910}
2903 2911
2904/* 2912/*
2905 * SADB_SPDEXPIRE processing 2913 * SADB_SPDEXPIRE processing
2906 * send 2914 * send
2907 * <base, address(SD), lifetime(CH), policy> 2915 * <base, address(SD), lifetime(CH), policy>
2908 * to KMD by PF_KEY. 2916 * to KMD by PF_KEY.
2909 * 2917 *
2910 * OUT: 0 : succeed 2918 * OUT: 0 : succeed
2911 * others : error number 2919 * others : error number
2912 */ 2920 */
2913static int 2921static int
2914key_spdexpire(struct secpolicy *sp) 2922key_spdexpire(struct secpolicy *sp)
2915{ 2923{
2916 int s; 2924 int s;
2917 struct mbuf *result = NULL, *m; 2925 struct mbuf *result = NULL, *m;
2918 int len; 2926 int len;
2919 int error = -1; 2927 int error = -1;
2920 struct sadb_lifetime *lt; 2928 struct sadb_lifetime *lt;
2921 2929
2922 /* XXX: Why do we lock ? */ 2930 /* XXX: Why do we lock ? */
2923 s = splsoftnet(); /*called from softclock()*/ 2931 s = splsoftnet(); /*called from softclock()*/
2924 2932
2925 KASSERT(sp != NULL); 2933 KASSERT(sp != NULL);
2926 2934
2927 /* set msg header */ 2935 /* set msg header */
2928 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0, M_WAITOK); 2936 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0, M_WAITOK);
2929 result = m; 2937 result = m;
2930 2938
2931 /* create lifetime extension (current and hard) */ 2939 /* create lifetime extension (current and hard) */
2932 len = PFKEY_ALIGN8(sizeof(*lt)) * 2; 2940 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2933 m = key_alloc_mbuf(len, M_WAITOK); 2941 m = key_alloc_mbuf(len, M_WAITOK);
2934 KASSERT(m->m_next == NULL); 2942 KASSERT(m->m_next == NULL);
2935 2943
2936 memset(mtod(m, void *), 0, len); 2944 memset(mtod(m, void *), 0, len);
2937 lt = mtod(m, struct sadb_lifetime *); 2945 lt = mtod(m, struct sadb_lifetime *);
2938 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); 2946 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2939 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; 2947 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2940 lt->sadb_lifetime_allocations = 0; 2948 lt->sadb_lifetime_allocations = 0;
2941 lt->sadb_lifetime_bytes = 0; 2949 lt->sadb_lifetime_bytes = 0;
2942 lt->sadb_lifetime_addtime = time_mono_to_wall(sp->created); 2950 lt->sadb_lifetime_addtime = time_mono_to_wall(sp->created);
2943 lt->sadb_lifetime_usetime = time_mono_to_wall(sp->lastused); 2951 lt->sadb_lifetime_usetime = time_mono_to_wall(sp->lastused);
2944 lt = (struct sadb_lifetime *)(mtod(m, char *) + len / 2); 2952 lt = (struct sadb_lifetime *)(mtod(m, char *) + len / 2);
2945 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); 2953 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2946 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD; 2954 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2947 lt->sadb_lifetime_allocations = 0; 2955 lt->sadb_lifetime_allocations = 0;
2948 lt->sadb_lifetime_bytes = 0; 2956 lt->sadb_lifetime_bytes = 0;
2949 lt->sadb_lifetime_addtime = sp->lifetime; 2957 lt->sadb_lifetime_addtime = sp->lifetime;
2950 lt->sadb_lifetime_usetime = sp->validtime; 2958 lt->sadb_lifetime_usetime = sp->validtime;
2951 m_cat(result, m); 2959 m_cat(result, m);
2952 2960
2953 /* set sadb_address for source */ 2961 /* set sadb_address for source */
2954 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &sp->spidx.src.sa, 2962 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &sp->spidx.src.sa,
2955 sp->spidx.prefs, sp->spidx.ul_proto, M_WAITOK); 2963 sp->spidx.prefs, sp->spidx.ul_proto, M_WAITOK);
2956 m_cat(result, m); 2964 m_cat(result, m);
2957 2965
2958 /* set sadb_address for destination */ 2966 /* set sadb_address for destination */
2959 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &sp->spidx.dst.sa, 2967 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &sp->spidx.dst.sa,
2960 sp->spidx.prefd, sp->spidx.ul_proto, M_WAITOK); 2968 sp->spidx.prefd, sp->spidx.ul_proto, M_WAITOK);
2961 m_cat(result, m); 2969 m_cat(result, m);
2962 2970
2963 /* set secpolicy */ 2971 /* set secpolicy */
2964 m = key_sp2msg(sp, M_WAITOK); 2972 m = key_sp2msg(sp, M_WAITOK);
2965 m_cat(result, m); 2973 m_cat(result, m);
2966 2974
2967 KASSERT(result->m_flags & M_PKTHDR); 2975 KASSERT(result->m_flags & M_PKTHDR);
2968 KASSERT(result->m_len >= sizeof(struct sadb_msg)); 2976 KASSERT(result->m_len >= sizeof(struct sadb_msg));
2969 2977
2970 result->m_pkthdr.len = 0; 2978 result->m_pkthdr.len = 0;
2971 for (m = result; m; m = m->m_next) 2979 for (m = result; m; m = m->m_next)
2972 result->m_pkthdr.len += m->m_len; 2980 result->m_pkthdr.len += m->m_len;
2973 2981
2974 mtod(result, struct sadb_msg *)->sadb_msg_len = 2982 mtod(result, struct sadb_msg *)->sadb_msg_len =
2975 PFKEY_UNIT64(result->m_pkthdr.len); 2983 PFKEY_UNIT64(result->m_pkthdr.len);
2976 2984
2977 error = key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED); 2985 error = key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2978 splx(s); 2986 splx(s);
2979 return error; 2987 return error;
2980} 2988}
2981 2989
2982/* %%% SAD management */ 2990/* %%% SAD management */
2983/* 2991/*
2984 * allocating a memory for new SA head, and copy from the values of mhp. 2992 * allocating a memory for new SA head, and copy from the values of mhp.
2985 * OUT: NULL : failure due to the lack of memory. 2993 * OUT: NULL : failure due to the lack of memory.
2986 * others : pointer to new SA head. 2994 * others : pointer to new SA head.
2987 */ 2995 */
2988static struct secashead * 2996static struct secashead *
2989key_newsah(const struct secasindex *saidx) 2997key_newsah(const struct secasindex *saidx)
2990{ 2998{
2991 struct secashead *newsah; 2999 struct secashead *newsah;
2992 int i; 3000 int i;
2993 3001
2994 KASSERT(saidx != NULL); 3002 KASSERT(saidx != NULL);
2995 3003
2996 newsah = kmem_zalloc(sizeof(struct secashead), KM_SLEEP); 3004 newsah = kmem_zalloc(sizeof(struct secashead), KM_SLEEP);
2997 for (i = 0; i < __arraycount(newsah->savlist); i++) 3005 for (i = 0; i < __arraycount(newsah->savlist); i++)
2998 PSLIST_INIT(&newsah->savlist[i]); 3006 PSLIST_INIT(&newsah->savlist[i]);
2999 newsah->saidx = *saidx; 3007 newsah->saidx = *saidx;
3000 3008
3001 localcount_init(&newsah->localcount); 3009 localcount_init(&newsah->localcount);
3002 /* Take a reference for the caller */ 3010 /* Take a reference for the caller */
3003 localcount_acquire(&newsah->localcount); 3011 localcount_acquire(&newsah->localcount);
3004 3012
3005 /* Add to the sah list */ 3013 /* Add to the sah list */
3006 SAHLIST_ENTRY_INIT(newsah); 3014 SAHLIST_ENTRY_INIT(newsah);
3007 newsah->state = SADB_SASTATE_MATURE; 3015 newsah->state = SADB_SASTATE_MATURE;
3008 mutex_enter(&key_sad.lock); 3016 mutex_enter(&key_sad.lock);
3009 SAHLIST_WRITER_INSERT_HEAD(newsah); 3017 SAHLIST_WRITER_INSERT_HEAD(newsah);
3010 mutex_exit(&key_sad.lock); 3018 mutex_exit(&key_sad.lock);
3011 3019
3012 return newsah; 3020 return newsah;
3013} 3021}
3014 3022
3015static bool 3023static bool
3016key_sah_has_sav(struct secashead *sah) 3024key_sah_has_sav(struct secashead *sah)
3017{ 3025{
3018 u_int state; 3026 u_int state;
3019 3027
3020 KASSERT(mutex_owned(&key_sad.lock)); 3028 KASSERT(mutex_owned(&key_sad.lock));
3021 3029
3022 SASTATE_ANY_FOREACH(state) { 3030 SASTATE_ANY_FOREACH(state) {
3023 if (!SAVLIST_WRITER_EMPTY(sah, state)) 3031 if (!SAVLIST_WRITER_EMPTY(sah, state))
3024 return true; 3032 return true;
3025 } 3033 }
3026 3034
3027 return false; 3035 return false;
3028} 3036}
3029 3037
3030static void 3038static void
3031key_unlink_sah(struct secashead *sah) 3039key_unlink_sah(struct secashead *sah)
3032{ 3040{
3033 3041
3034 KASSERT(!cpu_softintr_p()); 3042 KASSERT(!cpu_softintr_p());
3035 KASSERT(mutex_owned(&key_sad.lock)); 3043 KASSERT(mutex_owned(&key_sad.lock));
3036 KASSERT(sah->state == SADB_SASTATE_DEAD); 3044 KASSERT(sah->state == SADB_SASTATE_DEAD);
3037 3045
3038 /* Remove from the sah list */ 3046 /* Remove from the sah list */
3039 SAHLIST_WRITER_REMOVE(sah); 3047 SAHLIST_WRITER_REMOVE(sah);
3040 3048
3041 KDASSERT(mutex_ownable(softnet_lock)); 3049 KDASSERT(mutex_ownable(softnet_lock));
3042 key_sad_pserialize_perform(); 3050 key_sad_pserialize_perform();
3043 3051
3044 localcount_drain(&sah->localcount, &key_sad.cv_lc, &key_sad.lock); 3052 localcount_drain(&sah->localcount, &key_sad.cv_lc, &key_sad.lock);
3045} 3053}
3046 3054
3047static void 3055static void
3048key_destroy_sah(struct secashead *sah) 3056key_destroy_sah(struct secashead *sah)
3049{ 3057{
3050 3058
3051 rtcache_free(&sah->sa_route); 3059 rtcache_free(&sah->sa_route);
3052 3060
3053 SAHLIST_ENTRY_DESTROY(sah); 3061 SAHLIST_ENTRY_DESTROY(sah);
3054 localcount_fini(&sah->localcount); 3062 localcount_fini(&sah->localcount);
3055 3063
3056 if (sah->idents != NULL) 3064 if (sah->idents != NULL)
3057 kmem_free(sah->idents, sah->idents_len); 3065 kmem_free(sah->idents, sah->idents_len);
3058 if (sah->identd != NULL) 3066 if (sah->identd != NULL)
3059 kmem_free(sah->identd, sah->identd_len); 3067 kmem_free(sah->identd, sah->identd_len);
3060 3068
3061 kmem_free(sah, sizeof(*sah)); 3069 kmem_free(sah, sizeof(*sah));
3062} 3070}
3063 3071
3064/* 3072/*
3065 * allocating a new SA with LARVAL state. 3073 * allocating a new SA with LARVAL state.
3066 * key_api_add() and key_api_getspi() call, 3074 * key_api_add() and key_api_getspi() call,
3067 * and copy the values of mhp into new buffer. 3075 * and copy the values of mhp into new buffer.
3068 * When SAD message type is GETSPI: 3076 * When SAD message type is GETSPI:
3069 * to set sequence number from acq_seq++, 3077 * to set sequence number from acq_seq++,
3070 * to set zero to SPI. 3078 * to set zero to SPI.
3071 * not to call key_setsava(). 3079 * not to call key_setsava().
3072 * OUT: NULL : fail 3080 * OUT: NULL : fail
3073 * others : pointer to new secasvar. 3081 * others : pointer to new secasvar.
3074 * 3082 *
3075 * does not modify mbuf. does not free mbuf on error. 3083 * does not modify mbuf. does not free mbuf on error.
3076 */ 3084 */
3077static struct secasvar * 3085static struct secasvar *
3078key_newsav(struct mbuf *m, const struct sadb_msghdr *mhp, 3086key_newsav(struct mbuf *m, const struct sadb_msghdr *mhp,
3079 int *errp, const char* where, int tag) 3087 int *errp, const char* where, int tag)
3080{ 3088{
3081 struct secasvar *newsav; 3089 struct secasvar *newsav;
3082 const struct sadb_sa *xsa; 3090 const struct sadb_sa *xsa;
3083 3091
3084 KASSERT(!cpu_softintr_p()); 3092 KASSERT(!cpu_softintr_p());
3085 KASSERT(m != NULL); 3093 KASSERT(m != NULL);
3086 KASSERT(mhp != NULL); 3094 KASSERT(mhp != NULL);
3087 KASSERT(mhp->msg != NULL); 3095 KASSERT(mhp->msg != NULL);
3088 3096
3089 newsav = kmem_zalloc(sizeof(struct secasvar), KM_SLEEP); 3097 newsav = kmem_zalloc(sizeof(struct secasvar), KM_SLEEP);
3090 3098
3091 switch (mhp->msg->sadb_msg_type) { 3099 switch (mhp->msg->sadb_msg_type) {
3092 case SADB_GETSPI: 3100 case SADB_GETSPI:
3093 newsav->spi = 0; 3101 newsav->spi = 0;
3094 3102
3095#ifdef IPSEC_DOSEQCHECK 3103#ifdef IPSEC_DOSEQCHECK
3096 /* sync sequence number */ 3104 /* sync sequence number */
3097 if (mhp->msg->sadb_msg_seq == 0) 3105 if (mhp->msg->sadb_msg_seq == 0)
3098 newsav->seq = 3106 newsav->seq =
3099 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq)); 3107 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
3100 else 3108 else
3101#endif 3109#endif
3102 newsav->seq = mhp->msg->sadb_msg_seq; 3110 newsav->seq = mhp->msg->sadb_msg_seq;
3103 break; 3111 break;
3104 3112
3105 case SADB_ADD: 3113 case SADB_ADD:
3106 /* sanity check */ 3114 /* sanity check */
3107 if (mhp->ext[SADB_EXT_SA] == NULL) { 3115 if (mhp->ext[SADB_EXT_SA] == NULL) {
3108 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); 3116 IPSECLOG(LOG_DEBUG, "invalid message is passed.\n");
3109 *errp = EINVAL; 3117 *errp = EINVAL;
3110 goto error; 3118 goto error;
3111 } 3119 }
3112 xsa = mhp->ext[SADB_EXT_SA]; 3120 xsa = mhp->ext[SADB_EXT_SA];
3113 newsav->spi = xsa->sadb_sa_spi; 3121 newsav->spi = xsa->sadb_sa_spi;
3114 newsav->seq = mhp->msg->sadb_msg_seq; 3122 newsav->seq = mhp->msg->sadb_msg_seq;
3115 break; 3123 break;
3116 default: 3124 default:
3117 *errp = EINVAL; 3125 *errp = EINVAL;
3118 goto error; 3126 goto error;
3119 } 3127 }
3120 3128
3121 /* copy sav values */ 3129 /* copy sav values */
3122 if (mhp->msg->sadb_msg_type != SADB_GETSPI) { 3130 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
3123 *errp = key_setsaval(newsav, m, mhp); 3131 *errp = key_setsaval(newsav, m, mhp);
3124 if (*errp) 3132 if (*errp)
3125 goto error; 3133 goto error;
3126 } else { 3134 } else {
3127 /* We don't allow lft_c to be NULL */ 3135 /* We don't allow lft_c to be NULL */
3128 newsav->lft_c = kmem_zalloc(sizeof(struct sadb_lifetime), 3136 newsav->lft_c = kmem_zalloc(sizeof(struct sadb_lifetime),
3129 KM_SLEEP); 3137 KM_SLEEP);
3130 } 3138 }
3131 3139
3132 /* reset created */ 3140 /* reset created */
3133 newsav->created = time_uptime; 3141 newsav->created = time_uptime;
3134 newsav->pid = mhp->msg->sadb_msg_pid; 3142 newsav->pid = mhp->msg->sadb_msg_pid;
3135 3143
3136 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 3144 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
3137 "DP from %s:%u return SA:%p\n", where, tag, newsav); 3145 "DP from %s:%u return SA:%p\n", where, tag, newsav);
3138 return newsav; 3146 return newsav;
3139 3147
3140error: 3148error:
3141 KASSERT(*errp != 0); 3149 KASSERT(*errp != 0);
3142 kmem_free(newsav, sizeof(*newsav)); 3150 kmem_free(newsav, sizeof(*newsav));
3143 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 3151 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
3144 "DP from %s:%u return SA:NULL\n", where, tag); 3152 "DP from %s:%u return SA:NULL\n", where, tag);
3145 return NULL; 3153 return NULL;
3146} 3154}
3147 3155
3148 3156
3149static void 3157static void
3150key_clear_xform(struct secasvar *sav) 3158key_clear_xform(struct secasvar *sav)
3151{ 3159{
3152 3160
3153 /* 3161 /*
3154 * Cleanup xform state. Note that zeroize'ing causes the 3162 * Cleanup xform state. Note that zeroize'ing causes the
3155 * keys to be cleared; otherwise we must do it ourself. 3163 * keys to be cleared; otherwise we must do it ourself.
3156 */ 3164 */
3157 if (sav->tdb_xform != NULL) { 3165 if (sav->tdb_xform != NULL) {
3158 sav->tdb_xform->xf_zeroize(sav); 3166 sav->tdb_xform->xf_zeroize(sav);
3159 sav->tdb_xform = NULL; 3167 sav->tdb_xform = NULL;
3160 } else { 3168 } else {
3161 if (sav->key_auth != NULL) 3169 if (sav->key_auth != NULL)
3162 explicit_memset(_KEYBUF(sav->key_auth), 0, 3170 explicit_memset(_KEYBUF(sav->key_auth), 0,
3163 _KEYLEN(sav->key_auth)); 3171 _KEYLEN(sav->key_auth));
3164 if (sav->key_enc != NULL) 3172 if (sav->key_enc != NULL)
3165 explicit_memset(_KEYBUF(sav->key_enc), 0, 3173 explicit_memset(_KEYBUF(sav->key_enc), 0,
3166 _KEYLEN(sav->key_enc)); 3174 _KEYLEN(sav->key_enc));
3167 } 3175 }
3168} 3176}
3169 3177
3170/* 3178/*
3171 * free() SA variable entry. 3179 * free() SA variable entry.
3172 */ 3180 */
3173static void 3181static void
3174key_delsav(struct secasvar *sav) 3182key_delsav(struct secasvar *sav)
3175{ 3183{
3176 3184
3177 key_clear_xform(sav); 3185 key_clear_xform(sav);
3178 key_freesaval(sav); 3186 key_freesaval(sav);
3179 kmem_free(sav, sizeof(*sav)); 3187 kmem_free(sav, sizeof(*sav));
3180} 3188}
3181 3189
3182/* 3190/*
3183 * Must be called in a pserialize read section. A held sah 3191 * Must be called in a pserialize read section. A held sah
3184 * must be released by key_sah_unref after use. 3192 * must be released by key_sah_unref after use.
3185 */ 3193 */
3186static void 3194static void
3187key_sah_ref(struct secashead *sah) 3195key_sah_ref(struct secashead *sah)
3188{ 3196{
3189 3197
3190 localcount_acquire(&sah->localcount); 3198 localcount_acquire(&sah->localcount);
3191} 3199}
3192 3200
3193/* 3201/*
3194 * Must be called without holding key_sad.lock because the lock 3202 * Must be called without holding key_sad.lock because the lock
3195 * would be held in localcount_release. 3203 * would be held in localcount_release.
3196 */ 3204 */
3197static void 3205static void
3198key_sah_unref(struct secashead *sah) 3206key_sah_unref(struct secashead *sah)
3199{ 3207{
3200 3208
3201 KDASSERT(mutex_ownable(&key_sad.lock)); 3209 KDASSERT(mutex_ownable(&key_sad.lock));
3202 3210
3203 localcount_release(&sah->localcount, &key_sad.cv_lc, &key_sad.lock); 3211 localcount_release(&sah->localcount, &key_sad.cv_lc, &key_sad.lock);
3204} 3212}
3205 3213
3206/* 3214/*
3207 * Search SAD and return sah. Must be called in a pserialize 3215 * Search SAD and return sah. Must be called in a pserialize
3208 * read section. 3216 * read section.
3209 * OUT: 3217 * OUT:
3210 * NULL : not found 3218 * NULL : not found
3211 * others : found, pointer to a SA. 3219 * others : found, pointer to a SA.
3212 */ 3220 */
3213static struct secashead * 3221static struct secashead *
3214key_getsah(const struct secasindex *saidx, int flag) 3222key_getsah(const struct secasindex *saidx, int flag)
3215{ 3223{
3216 struct secashead *sah; 3224 struct secashead *sah;
3217 3225
3218 SAHLIST_READER_FOREACH(sah) { 3226 SAHLIST_READER_FOREACH(sah) {
3219 if (sah->state == SADB_SASTATE_DEAD) 3227 if (sah->state == SADB_SASTATE_DEAD)
3220 continue; 3228 continue;
3221 if (key_saidx_match(&sah->saidx, saidx, flag)) 3229 if (key_saidx_match(&sah->saidx, saidx, flag))
3222 return sah; 3230 return sah;
3223 } 3231 }
3224 3232
3225 return NULL; 3233 return NULL;
3226} 3234}
3227 3235
3228/* 3236/*
3229 * Search SAD and return sah. If sah is returned, the caller must call 3237 * Search SAD and return sah. If sah is returned, the caller must call
3230 * key_sah_unref to releaset a reference. 3238 * key_sah_unref to releaset a reference.
3231 * OUT: 3239 * OUT:
3232 * NULL : not found 3240 * NULL : not found
3233 * others : found, pointer to a SA. 3241 * others : found, pointer to a SA.
3234 */ 3242 */
3235static struct secashead * 3243static struct secashead *
3236key_getsah_ref(const struct secasindex *saidx, int flag) 3244key_getsah_ref(const struct secasindex *saidx, int flag)
3237{ 3245{
3238 struct secashead *sah; 3246 struct secashead *sah;
3239 int s; 3247 int s;
3240 3248
3241 s = pserialize_read_enter(); 3249 s = pserialize_read_enter();
3242 sah = key_getsah(saidx, flag); 3250 sah = key_getsah(saidx, flag);
3243 if (sah != NULL) 3251 if (sah != NULL)
3244 key_sah_ref(sah); 3252 key_sah_ref(sah);
3245 pserialize_read_exit(s); 3253 pserialize_read_exit(s);
3246 3254
3247 return sah; 3255 return sah;
3248} 3256}
3249 3257
3250/* 3258/*
3251 * check not to be duplicated SPI. 3259 * check not to be duplicated SPI.
3252 * NOTE: this function is too slow due to searching all SAD. 3260 * NOTE: this function is too slow due to searching all SAD.
3253 * OUT: 3261 * OUT:
3254 * NULL : not found 3262 * NULL : not found
3255 * others : found, pointer to a SA. 3263 * others : found, pointer to a SA.
3256 */ 3264 */
3257static bool 3265static bool
3258key_checkspidup(const struct secasindex *saidx, u_int32_t spi) 3266key_checkspidup(const struct secasindex *saidx, u_int32_t spi)
3259{ 3267{
3260 struct secashead *sah; 3268 struct secashead *sah;
3261 struct secasvar *sav; 3269 struct secasvar *sav;
3262 int s; 3270 int s;
3263 3271
3264 /* check address family */ 3272 /* check address family */
3265 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) { 3273 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
3266 IPSECLOG(LOG_DEBUG, "address family mismatched.\n"); 3274 IPSECLOG(LOG_DEBUG, "address family mismatched.\n");
3267 return false; 3275 return false;
3268 } 3276 }
3269 3277
3270 /* check all SAD */ 3278 /* check all SAD */
3271 s = pserialize_read_enter(); 3279 s = pserialize_read_enter();
3272 SAHLIST_READER_FOREACH(sah) { 3280 SAHLIST_READER_FOREACH(sah) {
3273 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst)) 3281 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
3274 continue; 3282 continue;
3275 sav = key_getsavbyspi(sah, spi); 3283 sav = key_getsavbyspi(sah, spi);
3276 if (sav != NULL) { 3284 if (sav != NULL) {