| @@ -1,4051 +1,4051 @@ | | | @@ -1,4051 +1,4051 @@ |
1 | /* $NetBSD: key.c,v 1.243 2017/11/22 05:43:28 ozaki-r Exp $ */ | | 1 | /* $NetBSD: key.c,v 1.244 2017/11/30 02:43:49 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.243 2017/11/22 05:43:28 ozaki-r Exp $"); | | 35 | __KERNEL_RCSID(0, "$NetBSD: key.c,v 1.244 2017/11/30 02:43:49 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 | |
125 | percpu_t *pfkeystat_percpu; | | 125 | percpu_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 | |
138 | u_int32_t key_debug_level = 0; | | 138 | u_int32_t key_debug_level = 0; |
139 | static u_int key_spi_trycnt = 1000; | | 139 | static u_int key_spi_trycnt = 1000; |
140 | static u_int32_t key_spi_minval = 0x100; | | 140 | static u_int32_t key_spi_minval = 0x100; |
141 | static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */ | | 141 | static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */ |
142 | static u_int32_t policy_id = 0; | | 142 | static u_int32_t policy_id = 0; |
143 | static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/ | | 143 | static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/ |
144 | static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/ | | 144 | static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/ |
145 | static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/ | | 145 | static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/ |
146 | static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/ | | 146 | static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/ |
147 | static int key_prefered_oldsa = 0; /* prefered old sa rather than new sa.*/ | | 147 | static int key_prefered_oldsa = 0; /* prefered old sa rather than new sa.*/ |
148 | | | 148 | |
149 | static u_int32_t acq_seq = 0; | | 149 | static 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 */ |
241 | static struct { | | 241 | static 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 */ |
257 | static struct { | | 257 | static 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 */ |
268 | static struct { | | 268 | static 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 | */ |
400 | static const u_int saorder_state_valid_prefer_old[] = { | | 400 | static const u_int saorder_state_valid_prefer_old[] = { |
401 | SADB_SASTATE_DYING, SADB_SASTATE_MATURE, | | 401 | SADB_SASTATE_DYING, SADB_SASTATE_MATURE, |
402 | }; | | 402 | }; |
403 | static const u_int saorder_state_valid_prefer_new[] = { | | 403 | static 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 | |
407 | static const u_int saorder_state_alive[] = { | | 407 | static 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 | }; |
411 | static const u_int saorder_state_any[] = { | | 411 | static 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 | |
427 | static const int minsize[] = { | | 427 | static 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 | }; |
455 | static const int maxsize[] = { | | 455 | static 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 | |
484 | static int ipsec_esp_keymin = 256; | | 484 | static int ipsec_esp_keymin = 256; |
485 | static int ipsec_esp_auth = 0; | | 485 | static int ipsec_esp_auth = 0; |
486 | static int ipsec_ah_keymin = 128; | | 486 | static int ipsec_ah_keymin = 128; |
487 | | | 487 | |
488 | #ifdef SYSCTL_DECL | | 488 | #ifdef SYSCTL_DECL |
489 | SYSCTL_DECL(_net_key); | | 489 | SYSCTL_DECL(_net_key); |
490 | #endif | | 490 | #endif |
491 | | | 491 | |
492 | #ifdef SYSCTL_INT | | 492 | #ifdef SYSCTL_INT |
493 | SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \ | | 493 | SYSCTL_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 */ |
497 | SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \ | | 497 | SYSCTL_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. */ |
501 | SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \ | | 501 | SYSCTL_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. */ |
505 | SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \ | | 505 | SYSCTL_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 */ |
509 | SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \ | | 509 | SYSCTL_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 */ |
513 | SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \ | | 513 | SYSCTL_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 */ |
517 | SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \ | | 517 | SYSCTL_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 */ |
521 | SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \ | | 521 | SYSCTL_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 */ |
525 | SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \ | | 525 | SYSCTL_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 */ |
529 | SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \ | | 529 | SYSCTL_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 */ |
533 | SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \ | | 533 | SYSCTL_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 */ |
537 | SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, prefered_oldsa, CTLFLAG_RW,\ | | 537 | SYSCTL_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) \ |
544 | do {\ | | 544 | do {\ |
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) \ |
566 | do { \ | | 566 | do { \ |
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 | */ |
578 | static int | | 578 | static int |
579 | key_setsecasidx(int, int, int, const struct sockaddr *, | | 579 | key_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 */ |
583 | struct _keystat { | | 583 | struct _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 | |
587 | struct sadb_msghdr { | | 587 | struct 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 | |
594 | static void | | 594 | static void |
595 | key_init_spidx_bymsghdr(struct secpolicyindex *, const struct sadb_msghdr *); | | 595 | key_init_spidx_bymsghdr(struct secpolicyindex *, const struct sadb_msghdr *); |
596 | | | 596 | |
597 | static const struct sockaddr * | | 597 | static const struct sockaddr * |
598 | key_msghdr_get_sockaddr(const struct sadb_msghdr *mhp, int idx) | | 598 | key_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 | |
604 | static void | | 604 | static void |
605 | key_fill_replymsg(struct mbuf *m, int seq) | | 605 | key_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 |
619 | static void key_freeso(struct socket *); | | 619 | static void key_freeso(struct socket *); |
620 | static void key_freesp_so(struct secpolicy **); | | 620 | static void key_freesp_so(struct secpolicy **); |
621 | #endif | | 621 | #endif |
622 | static struct secpolicy *key_getsp (const struct secpolicyindex *); | | 622 | static struct secpolicy *key_getsp (const struct secpolicyindex *); |
623 | static struct secpolicy *key_getspbyid (u_int32_t); | | 623 | static struct secpolicy *key_getspbyid (u_int32_t); |
624 | static struct secpolicy *key_lookup_and_remove_sp(const struct secpolicyindex *); | | 624 | static struct secpolicy *key_lookup_and_remove_sp(const struct secpolicyindex *); |
625 | static struct secpolicy *key_lookupbyid_and_remove_sp(u_int32_t); | | 625 | static struct secpolicy *key_lookupbyid_and_remove_sp(u_int32_t); |
626 | static void key_destroy_sp(struct secpolicy *); | | 626 | static void key_destroy_sp(struct secpolicy *); |
627 | static u_int16_t key_newreqid (void); | | 627 | static u_int16_t key_newreqid (void); |
628 | static struct mbuf *key_gather_mbuf (struct mbuf *, | | 628 | static struct mbuf *key_gather_mbuf (struct mbuf *, |
629 | const struct sadb_msghdr *, int, int, ...); | | 629 | const struct sadb_msghdr *, int, int, ...); |
630 | static int key_api_spdadd(struct socket *, struct mbuf *, | | 630 | static int key_api_spdadd(struct socket *, struct mbuf *, |
631 | const struct sadb_msghdr *); | | 631 | const struct sadb_msghdr *); |
632 | static u_int32_t key_getnewspid (void); | | 632 | static u_int32_t key_getnewspid (void); |
633 | static int key_api_spddelete(struct socket *, struct mbuf *, | | 633 | static int key_api_spddelete(struct socket *, struct mbuf *, |
634 | const struct sadb_msghdr *); | | 634 | const struct sadb_msghdr *); |
635 | static int key_api_spddelete2(struct socket *, struct mbuf *, | | 635 | static int key_api_spddelete2(struct socket *, struct mbuf *, |
636 | const struct sadb_msghdr *); | | 636 | const struct sadb_msghdr *); |
637 | static int key_api_spdget(struct socket *, struct mbuf *, | | 637 | static int key_api_spdget(struct socket *, struct mbuf *, |
638 | const struct sadb_msghdr *); | | 638 | const struct sadb_msghdr *); |
639 | static int key_api_spdflush(struct socket *, struct mbuf *, | | 639 | static int key_api_spdflush(struct socket *, struct mbuf *, |
640 | const struct sadb_msghdr *); | | 640 | const struct sadb_msghdr *); |
641 | static int key_api_spddump(struct socket *, struct mbuf *, | | 641 | static int key_api_spddump(struct socket *, struct mbuf *, |
642 | const struct sadb_msghdr *); | | 642 | const struct sadb_msghdr *); |
643 | static struct mbuf * key_setspddump (int *errorp, pid_t); | | 643 | static struct mbuf * key_setspddump (int *errorp, pid_t); |
644 | static struct mbuf * key_setspddump_chain (int *errorp, int *lenp, pid_t pid); | | 644 | static struct mbuf * key_setspddump_chain (int *errorp, int *lenp, pid_t pid); |
645 | static int key_api_nat_map(struct socket *, struct mbuf *, | | 645 | static int key_api_nat_map(struct socket *, struct mbuf *, |
646 | const struct sadb_msghdr *); | | 646 | const struct sadb_msghdr *); |
647 | static struct mbuf *key_setdumpsp (struct secpolicy *, | | 647 | static struct mbuf *key_setdumpsp (struct secpolicy *, |
648 | u_int8_t, u_int32_t, pid_t); | | 648 | u_int8_t, u_int32_t, pid_t); |
649 | static u_int key_getspreqmsglen (const struct secpolicy *); | | 649 | static u_int key_getspreqmsglen (const struct secpolicy *); |
650 | static int key_spdexpire (struct secpolicy *); | | 650 | static int key_spdexpire (struct secpolicy *); |
651 | static struct secashead *key_newsah (const struct secasindex *); | | 651 | static struct secashead *key_newsah (const struct secasindex *); |
652 | static void key_unlink_sah(struct secashead *); | | 652 | static void key_unlink_sah(struct secashead *); |
653 | static void key_destroy_sah(struct secashead *); | | 653 | static void key_destroy_sah(struct secashead *); |
654 | static bool key_sah_has_sav(struct secashead *); | | 654 | static bool key_sah_has_sav(struct secashead *); |
655 | static void key_sah_ref(struct secashead *); | | 655 | static void key_sah_ref(struct secashead *); |
656 | static void key_sah_unref(struct secashead *); | | 656 | static void key_sah_unref(struct secashead *); |
657 | static void key_init_sav(struct secasvar *); | | 657 | static void key_init_sav(struct secasvar *); |
658 | static void key_destroy_sav(struct secasvar *); | | 658 | static void key_destroy_sav(struct secasvar *); |
659 | static void key_destroy_sav_with_ref(struct secasvar *); | | 659 | static void key_destroy_sav_with_ref(struct secasvar *); |
660 | static struct secasvar *key_newsav(struct mbuf *, | | 660 | static 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__) |
664 | static void key_delsav (struct secasvar *); | | 664 | static void key_delsav (struct secasvar *); |
665 | static struct secashead *key_getsah(const struct secasindex *, int); | | 665 | static struct secashead *key_getsah(const struct secasindex *, int); |
666 | static struct secashead *key_getsah_ref(const struct secasindex *, int); | | 666 | static struct secashead *key_getsah_ref(const struct secasindex *, int); |
667 | static bool key_checkspidup(const struct secasindex *, u_int32_t); | | 667 | static bool key_checkspidup(const struct secasindex *, u_int32_t); |
668 | static struct secasvar *key_getsavbyspi (struct secashead *, u_int32_t); | | 668 | static struct secasvar *key_getsavbyspi (struct secashead *, u_int32_t); |
669 | static int key_setsaval (struct secasvar *, struct mbuf *, | | 669 | static int key_setsaval (struct secasvar *, struct mbuf *, |
670 | const struct sadb_msghdr *); | | 670 | const struct sadb_msghdr *); |
671 | static void key_freesaval(struct secasvar *); | | 671 | static void key_freesaval(struct secasvar *); |
672 | static int key_init_xform(struct secasvar *); | | 672 | static int key_init_xform(struct secasvar *); |
673 | static void key_clear_xform(struct secasvar *); | | 673 | static void key_clear_xform(struct secasvar *); |
674 | static struct mbuf *key_setdumpsa (struct secasvar *, u_int8_t, | | 674 | static 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); |
676 | static struct mbuf *key_setsadbxport (u_int16_t, u_int16_t); | | 676 | static struct mbuf *key_setsadbxport (u_int16_t, u_int16_t); |
677 | static struct mbuf *key_setsadbxtype (u_int16_t); | | 677 | static struct mbuf *key_setsadbxtype (u_int16_t); |
678 | static struct mbuf *key_setsadbxfrag (u_int16_t); | | 678 | static struct mbuf *key_setsadbxfrag (u_int16_t); |
679 | static void key_porttosaddr (union sockaddr_union *, u_int16_t); | | 679 | static void key_porttosaddr (union sockaddr_union *, u_int16_t); |
680 | static int key_checksalen (const union sockaddr_union *); | | 680 | static int key_checksalen (const union sockaddr_union *); |
681 | static struct mbuf *key_setsadbmsg (u_int8_t, u_int16_t, u_int8_t, | | 681 | static 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); |
683 | static struct mbuf *key_setsadbsa (struct secasvar *); | | 683 | static struct mbuf *key_setsadbsa (struct secasvar *); |
684 | static struct mbuf *key_setsadbaddr(u_int16_t, | | 684 | static 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 |
687 | static struct mbuf *key_setsadbident (u_int16_t, u_int16_t, void *, | | 687 | static 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 |
690 | static struct mbuf *key_setsadbxsa2 (u_int8_t, u_int32_t, u_int16_t); | | 690 | static struct mbuf *key_setsadbxsa2 (u_int8_t, u_int32_t, u_int16_t); |
691 | static struct mbuf *key_setsadbxpolicy (u_int16_t, u_int8_t, | | 691 | static struct mbuf *key_setsadbxpolicy (u_int16_t, u_int8_t, |
692 | u_int32_t, int); | | 692 | u_int32_t, int); |
693 | static void *key_newbuf (const void *, u_int); | | 693 | static void *key_newbuf (const void *, u_int); |
694 | #ifdef INET6 | | 694 | #ifdef INET6 |
695 | static int key_ismyaddr6 (const struct sockaddr_in6 *); | | 695 | static int key_ismyaddr6 (const struct sockaddr_in6 *); |
696 | #endif | | 696 | #endif |
697 | | | 697 | |
698 | static void sysctl_net_keyv2_setup(struct sysctllog **); | | 698 | static void sysctl_net_keyv2_setup(struct sysctllog **); |
699 | static void sysctl_net_key_compat_setup(struct sysctllog **); | | 699 | static 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. */ |
706 | static int key_saidx_match(const struct secasindex *, | | 706 | static int key_saidx_match(const struct secasindex *, |
707 | const struct secasindex *, int); | | 707 | const struct secasindex *, int); |
708 | | | 708 | |
709 | static int key_sockaddr_match(const struct sockaddr *, | | 709 | static int key_sockaddr_match(const struct sockaddr *, |
710 | const struct sockaddr *, int); | | 710 | const struct sockaddr *, int); |
711 | static int key_bb_match_withmask(const void *, const void *, u_int); | | 711 | static int key_bb_match_withmask(const void *, const void *, u_int); |
712 | static u_int16_t key_satype2proto (u_int8_t); | | 712 | static u_int16_t key_satype2proto (u_int8_t); |
713 | static u_int8_t key_proto2satype (u_int16_t); | | 713 | static u_int8_t key_proto2satype (u_int16_t); |
714 | | | 714 | |
715 | static int key_spidx_match_exactly(const struct secpolicyindex *, | | 715 | static int key_spidx_match_exactly(const struct secpolicyindex *, |
716 | const struct secpolicyindex *); | | 716 | const struct secpolicyindex *); |
717 | static int key_spidx_match_withmask(const struct secpolicyindex *, | | 717 | static int key_spidx_match_withmask(const struct secpolicyindex *, |
718 | const struct secpolicyindex *); | | 718 | const struct secpolicyindex *); |
719 | | | 719 | |
720 | static int key_api_getspi(struct socket *, struct mbuf *, | | 720 | static int key_api_getspi(struct socket *, struct mbuf *, |
721 | const struct sadb_msghdr *); | | 721 | const struct sadb_msghdr *); |
722 | static u_int32_t key_do_getnewspi (const struct sadb_spirange *, | | 722 | static u_int32_t key_do_getnewspi (const struct sadb_spirange *, |
723 | const struct secasindex *); | | 723 | const struct secasindex *); |
724 | static int key_handle_natt_info (struct secasvar *, | | 724 | static int key_handle_natt_info (struct secasvar *, |
725 | const struct sadb_msghdr *); | | 725 | const struct sadb_msghdr *); |
726 | static int key_set_natt_ports (union sockaddr_union *, | | 726 | static 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 *); |
729 | static int key_api_update(struct socket *, struct mbuf *, | | 729 | static 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 |
732 | static struct secasvar *key_getsavbyseq (struct secashead *, u_int32_t); | | 732 | static struct secasvar *key_getsavbyseq (struct secashead *, u_int32_t); |
733 | #endif | | 733 | #endif |
734 | static int key_api_add(struct socket *, struct mbuf *, | | 734 | static int key_api_add(struct socket *, struct mbuf *, |
735 | const struct sadb_msghdr *); | | 735 | const struct sadb_msghdr *); |
736 | static int key_setident (struct secashead *, struct mbuf *, | | 736 | static int key_setident (struct secashead *, struct mbuf *, |
737 | const struct sadb_msghdr *); | | 737 | const struct sadb_msghdr *); |
738 | static struct mbuf *key_getmsgbuf_x1 (struct mbuf *, | | 738 | static struct mbuf *key_getmsgbuf_x1 (struct mbuf *, |
739 | const struct sadb_msghdr *); | | 739 | const struct sadb_msghdr *); |
740 | static int key_api_delete(struct socket *, struct mbuf *, | | 740 | static int key_api_delete(struct socket *, struct mbuf *, |
741 | const struct sadb_msghdr *); | | 741 | const struct sadb_msghdr *); |
742 | static int key_api_get(struct socket *, struct mbuf *, | | 742 | static int key_api_get(struct socket *, struct mbuf *, |
743 | const struct sadb_msghdr *); | | 743 | const struct sadb_msghdr *); |
744 | | | 744 | |
745 | static void key_getcomb_setlifetime (struct sadb_comb *); | | 745 | static void key_getcomb_setlifetime (struct sadb_comb *); |
746 | static struct mbuf *key_getcomb_esp(int); | | 746 | static struct mbuf *key_getcomb_esp(int); |
747 | static struct mbuf *key_getcomb_ah(int); | | 747 | static struct mbuf *key_getcomb_ah(int); |
748 | static struct mbuf *key_getcomb_ipcomp(int); | | 748 | static struct mbuf *key_getcomb_ipcomp(int); |
749 | static struct mbuf *key_getprop(const struct secasindex *, int); | | 749 | static struct mbuf *key_getprop(const struct secasindex *, int); |
750 | | | 750 | |
751 | static int key_acquire(const struct secasindex *, const struct secpolicy *, | | 751 | static int key_acquire(const struct secasindex *, const struct secpolicy *, |
752 | int); | | 752 | int); |
753 | static int key_acquire_sendup_mbuf_later(struct mbuf *); | | 753 | static int key_acquire_sendup_mbuf_later(struct mbuf *); |
754 | static void key_acquire_sendup_pending_mbuf(void); | | 754 | static void key_acquire_sendup_pending_mbuf(void); |
755 | #ifndef IPSEC_NONBLOCK_ACQUIRE | | 755 | #ifndef IPSEC_NONBLOCK_ACQUIRE |
756 | static struct secacq *key_newacq (const struct secasindex *); | | 756 | static struct secacq *key_newacq (const struct secasindex *); |
757 | static struct secacq *key_getacq (const struct secasindex *); | | 757 | static struct secacq *key_getacq (const struct secasindex *); |
758 | static struct secacq *key_getacqbyseq (u_int32_t); | | 758 | static struct secacq *key_getacqbyseq (u_int32_t); |
759 | #endif | | 759 | #endif |
760 | #ifdef notyet | | 760 | #ifdef notyet |
761 | static struct secspacq *key_newspacq (const struct secpolicyindex *); | | 761 | static struct secspacq *key_newspacq (const struct secpolicyindex *); |
762 | static struct secspacq *key_getspacq (const struct secpolicyindex *); | | 762 | static struct secspacq *key_getspacq (const struct secpolicyindex *); |
763 | #endif | | 763 | #endif |
764 | static int key_api_acquire(struct socket *, struct mbuf *, | | 764 | static int key_api_acquire(struct socket *, struct mbuf *, |
765 | const struct sadb_msghdr *); | | 765 | const struct sadb_msghdr *); |
766 | static int key_api_register(struct socket *, struct mbuf *, | | 766 | static int key_api_register(struct socket *, struct mbuf *, |
767 | const struct sadb_msghdr *); | | 767 | const struct sadb_msghdr *); |
768 | static int key_expire (struct secasvar *); | | 768 | static int key_expire (struct secasvar *); |
769 | static int key_api_flush(struct socket *, struct mbuf *, | | 769 | static int key_api_flush(struct socket *, struct mbuf *, |
770 | const struct sadb_msghdr *); | | 770 | const struct sadb_msghdr *); |
771 | static struct mbuf *key_setdump_chain (u_int8_t req_satype, int *errorp, | | 771 | static struct mbuf *key_setdump_chain (u_int8_t req_satype, int *errorp, |
772 | int *lenp, pid_t pid); | | 772 | int *lenp, pid_t pid); |
773 | static int key_api_dump(struct socket *, struct mbuf *, | | 773 | static int key_api_dump(struct socket *, struct mbuf *, |
774 | const struct sadb_msghdr *); | | 774 | const struct sadb_msghdr *); |
775 | static int key_api_promisc(struct socket *, struct mbuf *, | | 775 | static int key_api_promisc(struct socket *, struct mbuf *, |
776 | const struct sadb_msghdr *); | | 776 | const struct sadb_msghdr *); |
777 | static int key_senderror (struct socket *, struct mbuf *, int); | | 777 | static int key_senderror (struct socket *, struct mbuf *, int); |
778 | static int key_validate_ext (const struct sadb_ext *, int); | | 778 | static int key_validate_ext (const struct sadb_ext *, int); |
779 | static int key_align (struct mbuf *, struct sadb_msghdr *); | | 779 | static int key_align (struct mbuf *, struct sadb_msghdr *); |
780 | #if 0 | | 780 | #if 0 |
781 | static const char *key_getfqdn (void); | | 781 | static const char *key_getfqdn (void); |
782 | static const char *key_getuserfqdn (void); | | 782 | static const char *key_getuserfqdn (void); |
783 | #endif | | 783 | #endif |
784 | static void key_sa_chgstate (struct secasvar *, u_int8_t); | | 784 | static void key_sa_chgstate (struct secasvar *, u_int8_t); |
785 | | | 785 | |
786 | static struct mbuf *key_alloc_mbuf(int, int); | | 786 | static struct mbuf *key_alloc_mbuf(int, int); |
787 | static struct mbuf *key_alloc_mbuf_simple(int, int); | | 787 | static struct mbuf *key_alloc_mbuf_simple(int, int); |
788 | | | 788 | |
789 | static void key_timehandler(void *); | | 789 | static void key_timehandler(void *); |
790 | static void key_timehandler_work(struct work *, void *); | | 790 | static void key_timehandler_work(struct work *, void *); |
791 | static struct callout key_timehandler_ch; | | 791 | static struct callout key_timehandler_ch; |
792 | static struct workqueue *key_timehandler_wq; | | 792 | static struct workqueue *key_timehandler_wq; |
793 | static struct work key_timehandler_wk; | | 793 | static struct work key_timehandler_wk; |
794 | | | 794 | |
795 | u_int | | 795 | u_int |
796 | key_sp_refcnt(const struct secpolicy *sp) | | 796 | key_sp_refcnt(const struct secpolicy *sp) |
797 | { | | 797 | { |
798 | | | 798 | |
799 | /* FIXME */ | | 799 | /* FIXME */ |
800 | return 0; | | 800 | return 0; |
801 | } | | 801 | } |
802 | | | 802 | |
803 | #ifdef NET_MPSAFE | | 803 | #ifdef NET_MPSAFE |
804 | static void | | 804 | static void |
805 | key_spd_pserialize_perform(void) | | 805 | key_spd_pserialize_perform(void) |
806 | { | | 806 | { |
807 | | | 807 | |
808 | KASSERT(mutex_owned(&key_spd.lock)); | | 808 | KASSERT(mutex_owned(&key_spd.lock)); |
809 | | | 809 | |
810 | while (key_spd.psz_performing) | | 810 | while (key_spd.psz_performing) |
811 | cv_wait(&key_spd.cv_psz, &key_spd.lock); | | 811 | cv_wait(&key_spd.cv_psz, &key_spd.lock); |
812 | key_spd.psz_performing = true; | | 812 | key_spd.psz_performing = true; |
813 | mutex_exit(&key_spd.lock); | | 813 | mutex_exit(&key_spd.lock); |
814 | | | 814 | |
815 | pserialize_perform(key_spd.psz); | | 815 | pserialize_perform(key_spd.psz); |
816 | | | 816 | |
817 | mutex_enter(&key_spd.lock); | | 817 | mutex_enter(&key_spd.lock); |
818 | key_spd.psz_performing = false; | | 818 | key_spd.psz_performing = false; |
819 | cv_broadcast(&key_spd.cv_psz); | | 819 | cv_broadcast(&key_spd.cv_psz); |
820 | } | | 820 | } |
821 | #endif | | 821 | #endif |
822 | | | 822 | |
823 | /* | | 823 | /* |
824 | * Remove the sp from the key_spd.splist and wait for references to the sp | | 824 | * Remove the sp from the key_spd.splist and wait for references to the sp |
825 | * to be released. key_spd.lock must be held. | | 825 | * to be released. key_spd.lock must be held. |
826 | */ | | 826 | */ |
827 | static void | | 827 | static void |
828 | key_unlink_sp(struct secpolicy *sp) | | 828 | key_unlink_sp(struct secpolicy *sp) |
829 | { | | 829 | { |
830 | | | 830 | |
831 | KASSERT(mutex_owned(&key_spd.lock)); | | 831 | KASSERT(mutex_owned(&key_spd.lock)); |
832 | | | 832 | |
833 | sp->state = IPSEC_SPSTATE_DEAD; | | 833 | sp->state = IPSEC_SPSTATE_DEAD; |
834 | SPLIST_WRITER_REMOVE(sp); | | 834 | SPLIST_WRITER_REMOVE(sp); |
835 | | | 835 | |
836 | /* Invalidate all cached SPD pointers in the PCBs. */ | | 836 | /* Invalidate all cached SPD pointers in the PCBs. */ |
837 | ipsec_invalpcbcacheall(); | | 837 | ipsec_invalpcbcacheall(); |
838 | | | 838 | |
839 | #ifdef NET_MPSAFE | | 839 | #ifdef NET_MPSAFE |
840 | KASSERT(mutex_ownable(softnet_lock)); | | 840 | KDASSERT(mutex_ownable(softnet_lock)); |
841 | key_spd_pserialize_perform(); | | 841 | key_spd_pserialize_perform(); |
842 | #endif | | 842 | #endif |
843 | | | 843 | |
844 | localcount_drain(&sp->localcount, &key_spd.cv_lc, &key_spd.lock); | | 844 | localcount_drain(&sp->localcount, &key_spd.cv_lc, &key_spd.lock); |
845 | } | | 845 | } |
846 | | | 846 | |
847 | /* | | 847 | /* |
848 | * Return 0 when there are known to be no SP's for the specified | | 848 | * Return 0 when there are known to be no SP's for the specified |
849 | * direction. Otherwise return 1. This is used by IPsec code | | 849 | * direction. Otherwise return 1. This is used by IPsec code |
850 | * to optimize performance. | | 850 | * to optimize performance. |
851 | */ | | 851 | */ |
852 | int | | 852 | int |
853 | key_havesp(u_int dir) | | 853 | key_havesp(u_int dir) |
854 | { | | 854 | { |
855 | return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ? | | 855 | return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ? |
856 | !SPLIST_READER_EMPTY(dir) : 1); | | 856 | !SPLIST_READER_EMPTY(dir) : 1); |
857 | } | | 857 | } |
858 | | | 858 | |
859 | /* %%% IPsec policy management */ | | 859 | /* %%% IPsec policy management */ |
860 | /* | | 860 | /* |
861 | * allocating a SP for OUTBOUND or INBOUND packet. | | 861 | * allocating a SP for OUTBOUND or INBOUND packet. |
862 | * Must call key_freesp() later. | | 862 | * Must call key_freesp() later. |
863 | * OUT: NULL: not found | | 863 | * OUT: NULL: not found |
864 | * others: found and return the pointer. | | 864 | * others: found and return the pointer. |
865 | */ | | 865 | */ |
866 | struct secpolicy * | | 866 | struct secpolicy * |
867 | key_lookup_sp_byspidx(const struct secpolicyindex *spidx, | | 867 | key_lookup_sp_byspidx(const struct secpolicyindex *spidx, |
868 | u_int dir, const char* where, int tag) | | 868 | u_int dir, const char* where, int tag) |
869 | { | | 869 | { |
870 | struct secpolicy *sp; | | 870 | struct secpolicy *sp; |
871 | int s; | | 871 | int s; |
872 | | | 872 | |
873 | KASSERT(spidx != NULL); | | 873 | KASSERT(spidx != NULL); |
874 | KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir); | | 874 | KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir); |
875 | | | 875 | |
876 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag); | | 876 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag); |
877 | | | 877 | |
878 | /* get a SP entry */ | | 878 | /* get a SP entry */ |
879 | if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) { | | 879 | if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) { |
880 | kdebug_secpolicyindex("objects", spidx); | | 880 | kdebug_secpolicyindex("objects", spidx); |
881 | } | | 881 | } |
882 | | | 882 | |
883 | s = pserialize_read_enter(); | | 883 | s = pserialize_read_enter(); |
884 | SPLIST_READER_FOREACH(sp, dir) { | | 884 | SPLIST_READER_FOREACH(sp, dir) { |
885 | if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) { | | 885 | if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) { |
886 | kdebug_secpolicyindex("in SPD", &sp->spidx); | | 886 | kdebug_secpolicyindex("in SPD", &sp->spidx); |
887 | } | | 887 | } |
888 | | | 888 | |
889 | if (sp->state == IPSEC_SPSTATE_DEAD) | | 889 | if (sp->state == IPSEC_SPSTATE_DEAD) |
890 | continue; | | 890 | continue; |
891 | if (key_spidx_match_withmask(&sp->spidx, spidx)) | | 891 | if (key_spidx_match_withmask(&sp->spidx, spidx)) |
892 | goto found; | | 892 | goto found; |
893 | } | | 893 | } |
894 | sp = NULL; | | 894 | sp = NULL; |
895 | found: | | 895 | found: |
896 | if (sp) { | | 896 | if (sp) { |
897 | /* sanity check */ | | 897 | /* sanity check */ |
898 | KEY_CHKSPDIR(sp->spidx.dir, dir); | | 898 | KEY_CHKSPDIR(sp->spidx.dir, dir); |
899 | | | 899 | |
900 | /* found a SPD entry */ | | 900 | /* found a SPD entry */ |
901 | sp->lastused = time_uptime; | | 901 | sp->lastused = time_uptime; |
902 | key_sp_ref(sp, where, tag); | | 902 | key_sp_ref(sp, where, tag); |
903 | } | | 903 | } |
904 | pserialize_read_exit(s); | | 904 | pserialize_read_exit(s); |
905 | | | 905 | |
906 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, | | 906 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, |
907 | "DP return SP:%p (ID=%u) refcnt %u\n", | | 907 | "DP return SP:%p (ID=%u) refcnt %u\n", |
908 | sp, sp ? sp->id : 0, key_sp_refcnt(sp)); | | 908 | sp, sp ? sp->id : 0, key_sp_refcnt(sp)); |
909 | return sp; | | 909 | return sp; |
910 | } | | 910 | } |
911 | | | 911 | |
912 | /* | | 912 | /* |
913 | * return a policy that matches this particular inbound packet. | | 913 | * return a policy that matches this particular inbound packet. |
914 | * XXX slow | | 914 | * XXX slow |
915 | */ | | 915 | */ |
916 | struct secpolicy * | | 916 | struct secpolicy * |
917 | key_gettunnel(const struct sockaddr *osrc, | | 917 | key_gettunnel(const struct sockaddr *osrc, |
918 | const struct sockaddr *odst, | | 918 | const struct sockaddr *odst, |
919 | const struct sockaddr *isrc, | | 919 | const struct sockaddr *isrc, |
920 | const struct sockaddr *idst, | | 920 | const struct sockaddr *idst, |
921 | const char* where, int tag) | | 921 | const char* where, int tag) |
922 | { | | 922 | { |
923 | struct secpolicy *sp; | | 923 | struct secpolicy *sp; |
924 | const int dir = IPSEC_DIR_INBOUND; | | 924 | const int dir = IPSEC_DIR_INBOUND; |
925 | int s; | | 925 | int s; |
926 | struct ipsecrequest *r1, *r2, *p; | | 926 | struct ipsecrequest *r1, *r2, *p; |
927 | struct secpolicyindex spidx; | | 927 | struct secpolicyindex spidx; |
928 | | | 928 | |
929 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag); | | 929 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag); |
930 | | | 930 | |
931 | if (isrc->sa_family != idst->sa_family) { | | 931 | if (isrc->sa_family != idst->sa_family) { |
932 | IPSECLOG(LOG_ERR, "protocol family mismatched %d != %d\n.", | | 932 | IPSECLOG(LOG_ERR, "protocol family mismatched %d != %d\n.", |
933 | isrc->sa_family, idst->sa_family); | | 933 | isrc->sa_family, idst->sa_family); |
934 | sp = NULL; | | 934 | sp = NULL; |
935 | goto done; | | 935 | goto done; |
936 | } | | 936 | } |
937 | | | 937 | |
938 | s = pserialize_read_enter(); | | 938 | s = pserialize_read_enter(); |
939 | SPLIST_READER_FOREACH(sp, dir) { | | 939 | SPLIST_READER_FOREACH(sp, dir) { |
940 | if (sp->state == IPSEC_SPSTATE_DEAD) | | 940 | if (sp->state == IPSEC_SPSTATE_DEAD) |
941 | continue; | | 941 | continue; |
942 | | | 942 | |
943 | r1 = r2 = NULL; | | 943 | r1 = r2 = NULL; |
944 | for (p = sp->req; p; p = p->next) { | | 944 | for (p = sp->req; p; p = p->next) { |
945 | if (p->saidx.mode != IPSEC_MODE_TUNNEL) | | 945 | if (p->saidx.mode != IPSEC_MODE_TUNNEL) |
946 | continue; | | 946 | continue; |
947 | | | 947 | |
948 | r1 = r2; | | 948 | r1 = r2; |
949 | r2 = p; | | 949 | r2 = p; |
950 | | | 950 | |
951 | if (!r1) { | | 951 | if (!r1) { |
952 | /* here we look at address matches only */ | | 952 | /* here we look at address matches only */ |
953 | spidx = sp->spidx; | | 953 | spidx = sp->spidx; |
954 | if (isrc->sa_len > sizeof(spidx.src) || | | 954 | if (isrc->sa_len > sizeof(spidx.src) || |
955 | idst->sa_len > sizeof(spidx.dst)) | | 955 | idst->sa_len > sizeof(spidx.dst)) |
956 | continue; | | 956 | continue; |
957 | memcpy(&spidx.src, isrc, isrc->sa_len); | | 957 | memcpy(&spidx.src, isrc, isrc->sa_len); |
958 | memcpy(&spidx.dst, idst, idst->sa_len); | | 958 | memcpy(&spidx.dst, idst, idst->sa_len); |
959 | if (!key_spidx_match_withmask(&sp->spidx, &spidx)) | | 959 | if (!key_spidx_match_withmask(&sp->spidx, &spidx)) |
960 | continue; | | 960 | continue; |
961 | } else { | | 961 | } else { |
962 | if (!key_sockaddr_match(&r1->saidx.src.sa, isrc, PORT_NONE) || | | 962 | if (!key_sockaddr_match(&r1->saidx.src.sa, isrc, PORT_NONE) || |
963 | !key_sockaddr_match(&r1->saidx.dst.sa, idst, PORT_NONE)) | | 963 | !key_sockaddr_match(&r1->saidx.dst.sa, idst, PORT_NONE)) |
964 | continue; | | 964 | continue; |
965 | } | | 965 | } |
966 | | | 966 | |
967 | if (!key_sockaddr_match(&r2->saidx.src.sa, osrc, PORT_NONE) || | | 967 | if (!key_sockaddr_match(&r2->saidx.src.sa, osrc, PORT_NONE) || |
968 | !key_sockaddr_match(&r2->saidx.dst.sa, odst, PORT_NONE)) | | 968 | !key_sockaddr_match(&r2->saidx.dst.sa, odst, PORT_NONE)) |
969 | continue; | | 969 | continue; |
970 | | | 970 | |
971 | goto found; | | 971 | goto found; |
972 | } | | 972 | } |
973 | } | | 973 | } |
974 | sp = NULL; | | 974 | sp = NULL; |
975 | found: | | 975 | found: |
976 | if (sp) { | | 976 | if (sp) { |
977 | sp->lastused = time_uptime; | | 977 | sp->lastused = time_uptime; |
978 | key_sp_ref(sp, where, tag); | | 978 | key_sp_ref(sp, where, tag); |
979 | } | | 979 | } |
980 | pserialize_read_exit(s); | | 980 | pserialize_read_exit(s); |
981 | done: | | 981 | done: |
982 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, | | 982 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, |
983 | "DP return SP:%p (ID=%u) refcnt %u\n", | | 983 | "DP return SP:%p (ID=%u) refcnt %u\n", |
984 | sp, sp ? sp->id : 0, key_sp_refcnt(sp)); | | 984 | sp, sp ? sp->id : 0, key_sp_refcnt(sp)); |
985 | return sp; | | 985 | return sp; |
986 | } | | 986 | } |
987 | | | 987 | |
988 | /* | | 988 | /* |
989 | * allocating an SA entry for an *OUTBOUND* packet. | | 989 | * allocating an SA entry for an *OUTBOUND* packet. |
990 | * checking each request entries in SP, and acquire an SA if need. | | 990 | * checking each request entries in SP, and acquire an SA if need. |
991 | * OUT: 0: there are valid requests. | | 991 | * OUT: 0: there are valid requests. |
992 | * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring. | | 992 | * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring. |
993 | */ | | 993 | */ |
994 | int | | 994 | int |
995 | key_checkrequest(const struct ipsecrequest *isr, const struct secasindex *saidx, | | 995 | key_checkrequest(const struct ipsecrequest *isr, const struct secasindex *saidx, |
996 | struct secasvar **ret) | | 996 | struct secasvar **ret) |
997 | { | | 997 | { |
998 | u_int level; | | 998 | u_int level; |
999 | int error; | | 999 | int error; |
1000 | struct secasvar *sav; | | 1000 | struct secasvar *sav; |
1001 | | | 1001 | |
1002 | KASSERT(isr != NULL); | | 1002 | KASSERT(isr != NULL); |
1003 | KASSERTMSG(saidx->mode == IPSEC_MODE_TRANSPORT || | | 1003 | KASSERTMSG(saidx->mode == IPSEC_MODE_TRANSPORT || |
1004 | saidx->mode == IPSEC_MODE_TUNNEL, | | 1004 | saidx->mode == IPSEC_MODE_TUNNEL, |
1005 | "unexpected policy %u", saidx->mode); | | 1005 | "unexpected policy %u", saidx->mode); |
1006 | | | 1006 | |
1007 | /* get current level */ | | 1007 | /* get current level */ |
1008 | level = ipsec_get_reqlevel(isr); | | 1008 | level = ipsec_get_reqlevel(isr); |
1009 | | | 1009 | |
1010 | /* | | 1010 | /* |
1011 | * XXX guard against protocol callbacks from the crypto | | 1011 | * XXX guard against protocol callbacks from the crypto |
1012 | * thread as they reference ipsecrequest.sav which we | | 1012 | * thread as they reference ipsecrequest.sav which we |
1013 | * temporarily null out below. Need to rethink how we | | 1013 | * temporarily null out below. Need to rethink how we |
1014 | * handle bundled SA's in the callback thread. | | 1014 | * handle bundled SA's in the callback thread. |
1015 | */ | | 1015 | */ |
1016 | IPSEC_SPLASSERT_SOFTNET("key_checkrequest"); | | 1016 | IPSEC_SPLASSERT_SOFTNET("key_checkrequest"); |
1017 | | | 1017 | |
1018 | sav = key_lookup_sa_bysaidx(saidx); | | 1018 | sav = key_lookup_sa_bysaidx(saidx); |
1019 | if (sav != NULL) { | | 1019 | if (sav != NULL) { |
1020 | *ret = sav; | | 1020 | *ret = sav; |
1021 | return 0; | | 1021 | return 0; |
1022 | } | | 1022 | } |
1023 | | | 1023 | |
1024 | /* there is no SA */ | | 1024 | /* there is no SA */ |
1025 | error = key_acquire(saidx, isr->sp, M_NOWAIT); | | 1025 | error = key_acquire(saidx, isr->sp, M_NOWAIT); |
1026 | if (error != 0) { | | 1026 | if (error != 0) { |
1027 | /* XXX What should I do ? */ | | 1027 | /* XXX What should I do ? */ |
1028 | IPSECLOG(LOG_DEBUG, "error %d returned from key_acquire.\n", | | 1028 | IPSECLOG(LOG_DEBUG, "error %d returned from key_acquire.\n", |
1029 | error); | | 1029 | error); |
1030 | return error; | | 1030 | return error; |
1031 | } | | 1031 | } |
1032 | | | 1032 | |
1033 | if (level != IPSEC_LEVEL_REQUIRE) { | | 1033 | if (level != IPSEC_LEVEL_REQUIRE) { |
1034 | /* XXX sigh, the interface to this routine is botched */ | | 1034 | /* XXX sigh, the interface to this routine is botched */ |
1035 | *ret = NULL; | | 1035 | *ret = NULL; |
1036 | return 0; | | 1036 | return 0; |
1037 | } else { | | 1037 | } else { |
1038 | return ENOENT; | | 1038 | return ENOENT; |
1039 | } | | 1039 | } |
1040 | } | | 1040 | } |
1041 | | | 1041 | |
1042 | /* | | 1042 | /* |
1043 | * looking up a SA for policy entry from SAD. | | 1043 | * looking up a SA for policy entry from SAD. |
1044 | * NOTE: searching SAD of aliving state. | | 1044 | * NOTE: searching SAD of aliving state. |
1045 | * OUT: NULL: not found. | | 1045 | * OUT: NULL: not found. |
1046 | * others: found and return the pointer. | | 1046 | * others: found and return the pointer. |
1047 | */ | | 1047 | */ |
1048 | struct secasvar * | | 1048 | struct secasvar * |
1049 | key_lookup_sa_bysaidx(const struct secasindex *saidx) | | 1049 | key_lookup_sa_bysaidx(const struct secasindex *saidx) |
1050 | { | | 1050 | { |
1051 | struct secashead *sah; | | 1051 | struct secashead *sah; |
1052 | struct secasvar *sav = NULL; | | 1052 | struct secasvar *sav = NULL; |
1053 | u_int stateidx, state; | | 1053 | u_int stateidx, state; |
1054 | const u_int *saorder_state_valid; | | 1054 | const u_int *saorder_state_valid; |
1055 | int arraysize; | | 1055 | int arraysize; |
1056 | int s; | | 1056 | int s; |
1057 | | | 1057 | |
1058 | s = pserialize_read_enter(); | | 1058 | s = pserialize_read_enter(); |
1059 | sah = key_getsah(saidx, CMP_MODE_REQID); | | 1059 | sah = key_getsah(saidx, CMP_MODE_REQID); |
1060 | if (sah == NULL) | | 1060 | if (sah == NULL) |
1061 | goto out; | | 1061 | goto out; |
1062 | | | 1062 | |
1063 | /* | | 1063 | /* |
1064 | * search a valid state list for outbound packet. | | 1064 | * search a valid state list for outbound packet. |
1065 | * This search order is important. | | 1065 | * This search order is important. |
1066 | */ | | 1066 | */ |
1067 | if (key_prefered_oldsa) { | | 1067 | if (key_prefered_oldsa) { |
1068 | saorder_state_valid = saorder_state_valid_prefer_old; | | 1068 | saorder_state_valid = saorder_state_valid_prefer_old; |
1069 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_old); | | 1069 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_old); |
1070 | } else { | | 1070 | } else { |
1071 | saorder_state_valid = saorder_state_valid_prefer_new; | | 1071 | saorder_state_valid = saorder_state_valid_prefer_new; |
1072 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_new); | | 1072 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_new); |
1073 | } | | 1073 | } |
1074 | | | 1074 | |
1075 | /* search valid state */ | | 1075 | /* search valid state */ |
1076 | for (stateidx = 0; | | 1076 | for (stateidx = 0; |
1077 | stateidx < arraysize; | | 1077 | stateidx < arraysize; |
1078 | stateidx++) { | | 1078 | stateidx++) { |
1079 | | | 1079 | |
1080 | state = saorder_state_valid[stateidx]; | | 1080 | state = saorder_state_valid[stateidx]; |
1081 | | | 1081 | |
1082 | if (key_prefered_oldsa) | | 1082 | if (key_prefered_oldsa) |
1083 | sav = SAVLIST_READER_FIRST(sah, state); | | 1083 | sav = SAVLIST_READER_FIRST(sah, state); |
1084 | else { | | 1084 | else { |
1085 | /* XXX need O(1) lookup */ | | 1085 | /* XXX need O(1) lookup */ |
1086 | struct secasvar *last = NULL; | | 1086 | struct secasvar *last = NULL; |
1087 | | | 1087 | |
1088 | SAVLIST_READER_FOREACH(sav, sah, state) | | 1088 | SAVLIST_READER_FOREACH(sav, sah, state) |
1089 | last = sav; | | 1089 | last = sav; |
1090 | sav = last; | | 1090 | sav = last; |
1091 | } | | 1091 | } |
1092 | if (sav != NULL) { | | 1092 | if (sav != NULL) { |
1093 | KEY_SA_REF(sav); | | 1093 | KEY_SA_REF(sav); |
1094 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, | | 1094 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, |
1095 | "DP cause refcnt++:%d SA:%p\n", | | 1095 | "DP cause refcnt++:%d SA:%p\n", |
1096 | key_sa_refcnt(sav), sav); | | 1096 | key_sa_refcnt(sav), sav); |
1097 | break; | | 1097 | break; |
1098 | } | | 1098 | } |
1099 | } | | 1099 | } |
1100 | out: | | 1100 | out: |
1101 | pserialize_read_exit(s); | | 1101 | pserialize_read_exit(s); |
1102 | | | 1102 | |
1103 | return sav; | | 1103 | return sav; |
1104 | } | | 1104 | } |
1105 | | | 1105 | |
1106 | #if 0 | | 1106 | #if 0 |
1107 | static void | | 1107 | static void |
1108 | key_sendup_message_delete(struct secasvar *sav) | | 1108 | key_sendup_message_delete(struct secasvar *sav) |
1109 | { | | 1109 | { |
1110 | struct mbuf *m, *result = 0; | | 1110 | struct mbuf *m, *result = 0; |
1111 | uint8_t satype; | | 1111 | uint8_t satype; |
1112 | | | 1112 | |
1113 | satype = key_proto2satype(sav->sah->saidx.proto); | | 1113 | satype = key_proto2satype(sav->sah->saidx.proto); |
1114 | if (satype == 0) | | 1114 | if (satype == 0) |
1115 | goto msgfail; | | 1115 | goto msgfail; |
1116 | | | 1116 | |
1117 | m = key_setsadbmsg(SADB_DELETE, 0, satype, 0, 0, key_sa_refcnt(sav) - 1); | | 1117 | m = key_setsadbmsg(SADB_DELETE, 0, satype, 0, 0, key_sa_refcnt(sav) - 1); |
1118 | if (m == NULL) | | 1118 | if (m == NULL) |
1119 | goto msgfail; | | 1119 | goto msgfail; |
1120 | result = m; | | 1120 | result = m; |
1121 | | | 1121 | |
1122 | /* set sadb_address for saidx's. */ | | 1122 | /* set sadb_address for saidx's. */ |
1123 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &sav->sah->saidx.src.sa, | | 1123 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &sav->sah->saidx.src.sa, |
1124 | sav->sah->saidx.src.sa.sa_len << 3, IPSEC_ULPROTO_ANY); | | 1124 | sav->sah->saidx.src.sa.sa_len << 3, IPSEC_ULPROTO_ANY); |
1125 | if (m == NULL) | | 1125 | if (m == NULL) |
1126 | goto msgfail; | | 1126 | goto msgfail; |
1127 | m_cat(result, m); | | 1127 | m_cat(result, m); |
1128 | | | 1128 | |
1129 | /* set sadb_address for saidx's. */ | | 1129 | /* set sadb_address for saidx's. */ |
1130 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &sav->sah->saidx.src.sa, | | 1130 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &sav->sah->saidx.src.sa, |
1131 | sav->sah->saidx.src.sa.sa_len << 3, IPSEC_ULPROTO_ANY); | | 1131 | sav->sah->saidx.src.sa.sa_len << 3, IPSEC_ULPROTO_ANY); |
1132 | if (m == NULL) | | 1132 | if (m == NULL) |
1133 | goto msgfail; | | 1133 | goto msgfail; |
1134 | m_cat(result, m); | | 1134 | m_cat(result, m); |
1135 | | | 1135 | |
1136 | /* create SA extension */ | | 1136 | /* create SA extension */ |
1137 | m = key_setsadbsa(sav); | | 1137 | m = key_setsadbsa(sav); |
1138 | if (m == NULL) | | 1138 | if (m == NULL) |
1139 | goto msgfail; | | 1139 | goto msgfail; |
1140 | m_cat(result, m); | | 1140 | m_cat(result, m); |
1141 | | | 1141 | |
1142 | if (result->m_len < sizeof(struct sadb_msg)) { | | 1142 | if (result->m_len < sizeof(struct sadb_msg)) { |
1143 | result = m_pullup(result, sizeof(struct sadb_msg)); | | 1143 | result = m_pullup(result, sizeof(struct sadb_msg)); |
1144 | if (result == NULL) | | 1144 | if (result == NULL) |
1145 | goto msgfail; | | 1145 | goto msgfail; |
1146 | } | | 1146 | } |
1147 | | | 1147 | |
1148 | result->m_pkthdr.len = 0; | | 1148 | result->m_pkthdr.len = 0; |
1149 | for (m = result; m; m = m->m_next) | | 1149 | for (m = result; m; m = m->m_next) |
1150 | result->m_pkthdr.len += m->m_len; | | 1150 | result->m_pkthdr.len += m->m_len; |
1151 | mtod(result, struct sadb_msg *)->sadb_msg_len = | | 1151 | mtod(result, struct sadb_msg *)->sadb_msg_len = |
1152 | PFKEY_UNIT64(result->m_pkthdr.len); | | 1152 | PFKEY_UNIT64(result->m_pkthdr.len); |
1153 | | | 1153 | |
1154 | key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED); | | 1154 | key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED); |
1155 | result = NULL; | | 1155 | result = NULL; |
1156 | msgfail: | | 1156 | msgfail: |
1157 | if (result) | | 1157 | if (result) |
1158 | m_freem(result); | | 1158 | m_freem(result); |
1159 | } | | 1159 | } |
1160 | #endif | | 1160 | #endif |
1161 | | | 1161 | |
1162 | /* | | 1162 | /* |
1163 | * allocating a usable SA entry for a *INBOUND* packet. | | 1163 | * allocating a usable SA entry for a *INBOUND* packet. |
1164 | * Must call key_freesav() later. | | 1164 | * Must call key_freesav() later. |
1165 | * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state). | | 1165 | * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state). |
1166 | * NULL: not found, or error occurred. | | 1166 | * NULL: not found, or error occurred. |
1167 | * | | 1167 | * |
1168 | * In the comparison, no source address is used--for RFC2401 conformance. | | 1168 | * In the comparison, no source address is used--for RFC2401 conformance. |
1169 | * To quote, from section 4.1: | | 1169 | * To quote, from section 4.1: |
1170 | * A security association is uniquely identified by a triple consisting | | 1170 | * A security association is uniquely identified by a triple consisting |
1171 | * of a Security Parameter Index (SPI), an IP Destination Address, and a | | 1171 | * of a Security Parameter Index (SPI), an IP Destination Address, and a |
1172 | * security protocol (AH or ESP) identifier. | | 1172 | * security protocol (AH or ESP) identifier. |
1173 | * Note that, however, we do need to keep source address in IPsec SA. | | 1173 | * Note that, however, we do need to keep source address in IPsec SA. |
1174 | * IKE specification and PF_KEY specification do assume that we | | 1174 | * IKE specification and PF_KEY specification do assume that we |
1175 | * keep source address in IPsec SA. We see a tricky situation here. | | 1175 | * keep source address in IPsec SA. We see a tricky situation here. |
1176 | * | | 1176 | * |
1177 | * sport and dport are used for NAT-T. network order is always used. | | 1177 | * sport and dport are used for NAT-T. network order is always used. |
1178 | */ | | 1178 | */ |
1179 | struct secasvar * | | 1179 | struct secasvar * |
1180 | key_lookup_sa( | | 1180 | key_lookup_sa( |
1181 | const union sockaddr_union *dst, | | 1181 | const union sockaddr_union *dst, |
1182 | u_int proto, | | 1182 | u_int proto, |
1183 | u_int32_t spi, | | 1183 | u_int32_t spi, |
1184 | u_int16_t sport, | | 1184 | u_int16_t sport, |
1185 | u_int16_t dport, | | 1185 | u_int16_t dport, |
1186 | const char* where, int tag) | | 1186 | const char* where, int tag) |
1187 | { | | 1187 | { |
1188 | struct secashead *sah; | | 1188 | struct secashead *sah; |
1189 | struct secasvar *sav; | | 1189 | struct secasvar *sav; |
1190 | u_int stateidx, state; | | 1190 | u_int stateidx, state; |
1191 | const u_int *saorder_state_valid; | | 1191 | const u_int *saorder_state_valid; |
1192 | int arraysize, chkport; | | 1192 | int arraysize, chkport; |
1193 | int s; | | 1193 | int s; |
1194 | | | 1194 | |
1195 | int must_check_spi = 1; | | 1195 | int must_check_spi = 1; |
1196 | int must_check_alg = 0; | | 1196 | int must_check_alg = 0; |
1197 | u_int16_t cpi = 0; | | 1197 | u_int16_t cpi = 0; |
1198 | u_int8_t algo = 0; | | 1198 | u_int8_t algo = 0; |
1199 | | | 1199 | |
1200 | if ((sport != 0) && (dport != 0)) | | 1200 | if ((sport != 0) && (dport != 0)) |
1201 | chkport = PORT_STRICT; | | 1201 | chkport = PORT_STRICT; |
1202 | else | | 1202 | else |
1203 | chkport = PORT_NONE; | | 1203 | chkport = PORT_NONE; |
1204 | | | 1204 | |
1205 | KASSERT(dst != NULL); | | 1205 | KASSERT(dst != NULL); |
1206 | | | 1206 | |
1207 | /* | | 1207 | /* |
1208 | * XXX IPCOMP case | | 1208 | * XXX IPCOMP case |
1209 | * We use cpi to define spi here. In the case where cpi <= | | 1209 | * We use cpi to define spi here. In the case where cpi <= |
1210 | * IPCOMP_CPI_NEGOTIATE_MIN, cpi just define the algorithm used, not | | 1210 | * IPCOMP_CPI_NEGOTIATE_MIN, cpi just define the algorithm used, not |
1211 | * the real spi. In this case, don't check the spi but check the | | 1211 | * the real spi. In this case, don't check the spi but check the |
1212 | * algorithm | | 1212 | * algorithm |
1213 | */ | | 1213 | */ |
1214 | | | 1214 | |
1215 | if (proto == IPPROTO_IPCOMP) { | | 1215 | if (proto == IPPROTO_IPCOMP) { |
1216 | u_int32_t tmp; | | 1216 | u_int32_t tmp; |
1217 | tmp = ntohl(spi); | | 1217 | tmp = ntohl(spi); |
1218 | cpi = (u_int16_t) tmp; | | 1218 | cpi = (u_int16_t) tmp; |
1219 | if (cpi < IPCOMP_CPI_NEGOTIATE_MIN) { | | 1219 | if (cpi < IPCOMP_CPI_NEGOTIATE_MIN) { |
1220 | algo = (u_int8_t) cpi; | | 1220 | algo = (u_int8_t) cpi; |
1221 | must_check_spi = 0; | | 1221 | must_check_spi = 0; |
1222 | must_check_alg = 1; | | 1222 | must_check_alg = 1; |
1223 | } | | 1223 | } |
1224 | } | | 1224 | } |
1225 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, | | 1225 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, |
1226 | "DP from %s:%u check_spi=%d, check_alg=%d\n", | | 1226 | "DP from %s:%u check_spi=%d, check_alg=%d\n", |
1227 | where, tag, must_check_spi, must_check_alg); | | 1227 | where, tag, must_check_spi, must_check_alg); |
1228 | | | 1228 | |
1229 | | | 1229 | |
1230 | /* | | 1230 | /* |
1231 | * searching SAD. | | 1231 | * searching SAD. |
1232 | * XXX: to be checked internal IP header somewhere. Also when | | 1232 | * XXX: to be checked internal IP header somewhere. Also when |
1233 | * IPsec tunnel packet is received. But ESP tunnel mode is | | 1233 | * IPsec tunnel packet is received. But ESP tunnel mode is |
1234 | * encrypted so we can't check internal IP header. | | 1234 | * encrypted so we can't check internal IP header. |
1235 | */ | | 1235 | */ |
1236 | if (key_prefered_oldsa) { | | 1236 | if (key_prefered_oldsa) { |
1237 | saorder_state_valid = saorder_state_valid_prefer_old; | | 1237 | saorder_state_valid = saorder_state_valid_prefer_old; |
1238 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_old); | | 1238 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_old); |
1239 | } else { | | 1239 | } else { |
1240 | saorder_state_valid = saorder_state_valid_prefer_new; | | 1240 | saorder_state_valid = saorder_state_valid_prefer_new; |
1241 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_new); | | 1241 | arraysize = _ARRAYLEN(saorder_state_valid_prefer_new); |
1242 | } | | 1242 | } |
1243 | s = pserialize_read_enter(); | | 1243 | s = pserialize_read_enter(); |
1244 | SAHLIST_READER_FOREACH(sah) { | | 1244 | SAHLIST_READER_FOREACH(sah) { |
1245 | /* search valid state */ | | 1245 | /* search valid state */ |
1246 | for (stateidx = 0; stateidx < arraysize; stateidx++) { | | 1246 | for (stateidx = 0; stateidx < arraysize; stateidx++) { |
1247 | state = saorder_state_valid[stateidx]; | | 1247 | state = saorder_state_valid[stateidx]; |
1248 | SAVLIST_READER_FOREACH(sav, sah, state) { | | 1248 | SAVLIST_READER_FOREACH(sav, sah, state) { |
1249 | KEYDEBUG_PRINTF(KEYDEBUG_MATCH, | | 1249 | KEYDEBUG_PRINTF(KEYDEBUG_MATCH, |
1250 | "try match spi %#x, %#x\n", | | 1250 | "try match spi %#x, %#x\n", |
1251 | ntohl(spi), ntohl(sav->spi)); | | 1251 | ntohl(spi), ntohl(sav->spi)); |
1252 | /* sanity check */ | | 1252 | /* sanity check */ |
1253 | KEY_CHKSASTATE(sav->state, state); | | 1253 | KEY_CHKSASTATE(sav->state, state); |
1254 | /* do not return entries w/ unusable state */ | | 1254 | /* do not return entries w/ unusable state */ |
1255 | if (!SADB_SASTATE_USABLE_P(sav)) { | | 1255 | if (!SADB_SASTATE_USABLE_P(sav)) { |
1256 | KEYDEBUG_PRINTF(KEYDEBUG_MATCH, | | 1256 | KEYDEBUG_PRINTF(KEYDEBUG_MATCH, |
1257 | "bad state %d\n", sav->state); | | 1257 | "bad state %d\n", sav->state); |
1258 | continue; | | 1258 | continue; |
1259 | } | | 1259 | } |
1260 | if (proto != sav->sah->saidx.proto) { | | 1260 | if (proto != sav->sah->saidx.proto) { |
1261 | KEYDEBUG_PRINTF(KEYDEBUG_MATCH, | | 1261 | KEYDEBUG_PRINTF(KEYDEBUG_MATCH, |
1262 | "proto fail %d != %d\n", | | 1262 | "proto fail %d != %d\n", |
1263 | proto, sav->sah->saidx.proto); | | 1263 | proto, sav->sah->saidx.proto); |
1264 | continue; | | 1264 | continue; |
1265 | } | | 1265 | } |
1266 | if (must_check_spi && spi != sav->spi) { | | 1266 | if (must_check_spi && spi != sav->spi) { |
1267 | KEYDEBUG_PRINTF(KEYDEBUG_MATCH, | | 1267 | KEYDEBUG_PRINTF(KEYDEBUG_MATCH, |
1268 | "spi fail %#x != %#x\n", | | 1268 | "spi fail %#x != %#x\n", |
1269 | ntohl(spi), ntohl(sav->spi)); | | 1269 | ntohl(spi), ntohl(sav->spi)); |
1270 | continue; | | 1270 | continue; |
1271 | } | | 1271 | } |
1272 | /* XXX only on the ipcomp case */ | | 1272 | /* XXX only on the ipcomp case */ |
1273 | if (must_check_alg && algo != sav->alg_comp) { | | 1273 | if (must_check_alg && algo != sav->alg_comp) { |
1274 | KEYDEBUG_PRINTF(KEYDEBUG_MATCH, | | 1274 | KEYDEBUG_PRINTF(KEYDEBUG_MATCH, |
1275 | "algo fail %d != %d\n", | | 1275 | "algo fail %d != %d\n", |
1276 | algo, sav->alg_comp); | | 1276 | algo, sav->alg_comp); |
1277 | continue; | | 1277 | continue; |
1278 | } | | 1278 | } |
1279 | | | 1279 | |
1280 | #if 0 /* don't check src */ | | 1280 | #if 0 /* don't check src */ |
1281 | /* Fix port in src->sa */ | | 1281 | /* Fix port in src->sa */ |
1282 | | | 1282 | |
1283 | /* check src address */ | | 1283 | /* check src address */ |
1284 | if (!key_sockaddr_match(&src->sa, &sav->sah->saidx.src.sa, PORT_NONE)) | | 1284 | if (!key_sockaddr_match(&src->sa, &sav->sah->saidx.src.sa, PORT_NONE)) |
1285 | continue; | | 1285 | continue; |
1286 | #endif | | 1286 | #endif |
1287 | /* fix port of dst address XXX*/ | | 1287 | /* fix port of dst address XXX*/ |
1288 | key_porttosaddr(__UNCONST(dst), dport); | | 1288 | key_porttosaddr(__UNCONST(dst), dport); |
1289 | /* check dst address */ | | 1289 | /* check dst address */ |
1290 | if (!key_sockaddr_match(&dst->sa, &sav->sah->saidx.dst.sa, chkport)) | | 1290 | if (!key_sockaddr_match(&dst->sa, &sav->sah->saidx.dst.sa, chkport)) |
1291 | continue; | | 1291 | continue; |
1292 | key_sa_ref(sav, where, tag); | | 1292 | key_sa_ref(sav, where, tag); |
1293 | goto done; | | 1293 | goto done; |
1294 | } | | 1294 | } |
1295 | } | | 1295 | } |
1296 | } | | 1296 | } |
1297 | sav = NULL; | | 1297 | sav = NULL; |
1298 | done: | | 1298 | done: |
1299 | pserialize_read_exit(s); | | 1299 | pserialize_read_exit(s); |
1300 | | | 1300 | |
1301 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, | | 1301 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, |
1302 | "DP return SA:%p; refcnt %u\n", sav, key_sa_refcnt(sav)); | | 1302 | "DP return SA:%p; refcnt %u\n", sav, key_sa_refcnt(sav)); |
1303 | return sav; | | 1303 | return sav; |
1304 | } | | 1304 | } |
1305 | | | 1305 | |
1306 | static void | | 1306 | static void |
1307 | key_validate_savlist(const struct secashead *sah, const u_int state) | | 1307 | key_validate_savlist(const struct secashead *sah, const u_int state) |
1308 | { | | 1308 | { |
1309 | #ifdef DEBUG | | 1309 | #ifdef DEBUG |
1310 | struct secasvar *sav, *next; | | 1310 | struct secasvar *sav, *next; |
1311 | int s; | | 1311 | int s; |
1312 | | | 1312 | |
1313 | /* | | 1313 | /* |
1314 | * The list should be sorted by lft_c->sadb_lifetime_addtime | | 1314 | * The list should be sorted by lft_c->sadb_lifetime_addtime |
1315 | * in ascending order. | | 1315 | * in ascending order. |
1316 | */ | | 1316 | */ |
1317 | s = pserialize_read_enter(); | | 1317 | s = pserialize_read_enter(); |
1318 | SAVLIST_READER_FOREACH(sav, sah, state) { | | 1318 | SAVLIST_READER_FOREACH(sav, sah, state) { |
1319 | next = SAVLIST_READER_NEXT(sav); | | 1319 | next = SAVLIST_READER_NEXT(sav); |
1320 | if (next != NULL && | | 1320 | if (next != NULL && |
1321 | sav->lft_c != NULL && next->lft_c != NULL) { | | 1321 | sav->lft_c != NULL && next->lft_c != NULL) { |
1322 | KDASSERTMSG(sav->lft_c->sadb_lifetime_addtime <= | | 1322 | KDASSERTMSG(sav->lft_c->sadb_lifetime_addtime <= |
1323 | next->lft_c->sadb_lifetime_addtime, | | 1323 | next->lft_c->sadb_lifetime_addtime, |
1324 | "savlist is not sorted: sah=%p, state=%d, " | | 1324 | "savlist is not sorted: sah=%p, state=%d, " |
1325 | "sav=%" PRIu64 ", next=%" PRIu64, sah, state, | | 1325 | "sav=%" PRIu64 ", next=%" PRIu64, sah, state, |
1326 | sav->lft_c->sadb_lifetime_addtime, | | 1326 | sav->lft_c->sadb_lifetime_addtime, |
1327 | next->lft_c->sadb_lifetime_addtime); | | 1327 | next->lft_c->sadb_lifetime_addtime); |
1328 | } | | 1328 | } |
1329 | } | | 1329 | } |
1330 | pserialize_read_exit(s); | | 1330 | pserialize_read_exit(s); |
1331 | #endif | | 1331 | #endif |
1332 | } | | 1332 | } |
1333 | | | 1333 | |
1334 | void | | 1334 | void |
1335 | key_init_sp(struct secpolicy *sp) | | 1335 | key_init_sp(struct secpolicy *sp) |
1336 | { | | 1336 | { |
1337 | | | 1337 | |
1338 | ASSERT_SLEEPABLE(); | | 1338 | ASSERT_SLEEPABLE(); |
1339 | | | 1339 | |
1340 | sp->state = IPSEC_SPSTATE_ALIVE; | | 1340 | sp->state = IPSEC_SPSTATE_ALIVE; |
1341 | if (sp->policy == IPSEC_POLICY_IPSEC) | | 1341 | if (sp->policy == IPSEC_POLICY_IPSEC) |
1342 | KASSERT(sp->req != NULL); | | 1342 | KASSERT(sp->req != NULL); |
1343 | localcount_init(&sp->localcount); | | 1343 | localcount_init(&sp->localcount); |
1344 | SPLIST_ENTRY_INIT(sp); | | 1344 | SPLIST_ENTRY_INIT(sp); |
1345 | } | | 1345 | } |
1346 | | | 1346 | |
1347 | /* | | 1347 | /* |
1348 | * Must be called in a pserialize read section. A held SP | | 1348 | * Must be called in a pserialize read section. A held SP |
1349 | * must be released by key_sp_unref after use. | | 1349 | * must be released by key_sp_unref after use. |
1350 | */ | | 1350 | */ |
1351 | void | | 1351 | void |
1352 | key_sp_ref(struct secpolicy *sp, const char* where, int tag) | | 1352 | key_sp_ref(struct secpolicy *sp, const char* where, int tag) |
1353 | { | | 1353 | { |
1354 | | | 1354 | |
1355 | localcount_acquire(&sp->localcount); | | 1355 | localcount_acquire(&sp->localcount); |
1356 | | | 1356 | |
1357 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, | | 1357 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, |
1358 | "DP SP:%p (ID=%u) from %s:%u; refcnt++ now %u\n", | | 1358 | "DP SP:%p (ID=%u) from %s:%u; refcnt++ now %u\n", |
1359 | sp, sp->id, where, tag, key_sp_refcnt(sp)); | | 1359 | sp, sp->id, where, tag, key_sp_refcnt(sp)); |
1360 | } | | 1360 | } |
1361 | | | 1361 | |
1362 | /* | | 1362 | /* |
1363 | * Must be called without holding key_spd.lock because the lock | | 1363 | * Must be called without holding key_spd.lock because the lock |
1364 | * would be held in localcount_release. | | 1364 | * would be held in localcount_release. |
1365 | */ | | 1365 | */ |
1366 | void | | 1366 | void |
1367 | key_sp_unref(struct secpolicy *sp, const char* where, int tag) | | 1367 | key_sp_unref(struct secpolicy *sp, const char* where, int tag) |
1368 | { | | 1368 | { |
1369 | | | 1369 | |
1370 | KDASSERT(mutex_ownable(&key_spd.lock)); | | 1370 | KDASSERT(mutex_ownable(&key_spd.lock)); |
1371 | | | 1371 | |
1372 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, | | 1372 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, |
1373 | "DP SP:%p (ID=%u) from %s:%u; refcnt-- now %u\n", | | 1373 | "DP SP:%p (ID=%u) from %s:%u; refcnt-- now %u\n", |
1374 | sp, sp->id, where, tag, key_sp_refcnt(sp)); | | 1374 | sp, sp->id, where, tag, key_sp_refcnt(sp)); |
1375 | | | 1375 | |
1376 | localcount_release(&sp->localcount, &key_spd.cv_lc, &key_spd.lock); | | 1376 | localcount_release(&sp->localcount, &key_spd.cv_lc, &key_spd.lock); |
1377 | } | | 1377 | } |
1378 | | | 1378 | |
1379 | static void | | 1379 | static void |
1380 | key_init_sav(struct secasvar *sav) | | 1380 | key_init_sav(struct secasvar *sav) |
1381 | { | | 1381 | { |
1382 | | | 1382 | |
1383 | ASSERT_SLEEPABLE(); | | 1383 | ASSERT_SLEEPABLE(); |
1384 | | | 1384 | |
1385 | localcount_init(&sav->localcount); | | 1385 | localcount_init(&sav->localcount); |
1386 | SAVLIST_ENTRY_INIT(sav); | | 1386 | SAVLIST_ENTRY_INIT(sav); |
1387 | } | | 1387 | } |
1388 | | | 1388 | |
1389 | u_int | | 1389 | u_int |
1390 | key_sa_refcnt(const struct secasvar *sav) | | 1390 | key_sa_refcnt(const struct secasvar *sav) |
1391 | { | | 1391 | { |
1392 | | | 1392 | |
1393 | /* FIXME */ | | 1393 | /* FIXME */ |
1394 | return 0; | | 1394 | return 0; |
1395 | } | | 1395 | } |
1396 | | | 1396 | |
1397 | void | | 1397 | void |
1398 | key_sa_ref(struct secasvar *sav, const char* where, int tag) | | 1398 | key_sa_ref(struct secasvar *sav, const char* where, int tag) |
1399 | { | | 1399 | { |
1400 | | | 1400 | |
1401 | localcount_acquire(&sav->localcount); | | 1401 | localcount_acquire(&sav->localcount); |
1402 | | | 1402 | |
1403 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, | | 1403 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, |
1404 | "DP cause refcnt++: SA:%p from %s:%u\n", | | 1404 | "DP cause refcnt++: SA:%p from %s:%u\n", |
1405 | sav, where, tag); | | 1405 | sav, where, tag); |
1406 | } | | 1406 | } |
1407 | | | 1407 | |
1408 | void | | 1408 | void |
1409 | key_sa_unref(struct secasvar *sav, const char* where, int tag) | | 1409 | key_sa_unref(struct secasvar *sav, const char* where, int tag) |
1410 | { | | 1410 | { |
1411 | | | 1411 | |
1412 | KDASSERT(mutex_ownable(&key_sad.lock)); | | 1412 | KDASSERT(mutex_ownable(&key_sad.lock)); |
1413 | | | 1413 | |
1414 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, | | 1414 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, |
1415 | "DP cause refcnt--: SA:%p from %s:%u\n", | | 1415 | "DP cause refcnt--: SA:%p from %s:%u\n", |
1416 | sav, where, tag); | | 1416 | sav, where, tag); |
1417 | | | 1417 | |
1418 | localcount_release(&sav->localcount, &key_sad.cv_lc, &key_sad.lock); | | 1418 | localcount_release(&sav->localcount, &key_sad.cv_lc, &key_sad.lock); |
1419 | } | | 1419 | } |
1420 | | | 1420 | |
1421 | #if 0 | | 1421 | #if 0 |
1422 | /* | | 1422 | /* |
1423 | * Must be called after calling key_lookup_sp*(). | | 1423 | * Must be called after calling key_lookup_sp*(). |
1424 | * For the packet with socket. | | 1424 | * For the packet with socket. |
1425 | */ | | 1425 | */ |
1426 | static void | | 1426 | static void |
1427 | key_freeso(struct socket *so) | | 1427 | key_freeso(struct socket *so) |
1428 | { | | 1428 | { |
1429 | /* sanity check */ | | 1429 | /* sanity check */ |
1430 | KASSERT(so != NULL); | | 1430 | KASSERT(so != NULL); |
1431 | | | 1431 | |
1432 | switch (so->so_proto->pr_domain->dom_family) { | | 1432 | switch (so->so_proto->pr_domain->dom_family) { |
1433 | #ifdef INET | | 1433 | #ifdef INET |
1434 | case PF_INET: | | 1434 | case PF_INET: |
1435 | { | | 1435 | { |
1436 | struct inpcb *pcb = sotoinpcb(so); | | 1436 | struct inpcb *pcb = sotoinpcb(so); |
1437 | | | 1437 | |
1438 | /* Does it have a PCB ? */ | | 1438 | /* Does it have a PCB ? */ |
1439 | if (pcb == NULL) | | 1439 | if (pcb == NULL) |
1440 | return; | | 1440 | return; |
1441 | | | 1441 | |
1442 | struct inpcbpolicy *sp = pcb->inp_sp; | | 1442 | struct inpcbpolicy *sp = pcb->inp_sp; |
1443 | key_freesp_so(&sp->sp_in); | | 1443 | key_freesp_so(&sp->sp_in); |
1444 | key_freesp_so(&sp->sp_out); | | 1444 | key_freesp_so(&sp->sp_out); |
1445 | } | | 1445 | } |
1446 | break; | | 1446 | break; |
1447 | #endif | | 1447 | #endif |
1448 | #ifdef INET6 | | 1448 | #ifdef INET6 |
1449 | case PF_INET6: | | 1449 | case PF_INET6: |
1450 | { | | 1450 | { |
1451 | #ifdef HAVE_NRL_INPCB | | 1451 | #ifdef HAVE_NRL_INPCB |
1452 | struct inpcb *pcb = sotoinpcb(so); | | 1452 | struct inpcb *pcb = sotoinpcb(so); |
1453 | struct inpcbpolicy *sp = pcb->inp_sp; | | 1453 | struct inpcbpolicy *sp = pcb->inp_sp; |
1454 | | | 1454 | |
1455 | /* Does it have a PCB ? */ | | 1455 | /* Does it have a PCB ? */ |
1456 | if (pcb == NULL) | | 1456 | if (pcb == NULL) |
1457 | return; | | 1457 | return; |
1458 | key_freesp_so(&sp->sp_in); | | 1458 | key_freesp_so(&sp->sp_in); |
1459 | key_freesp_so(&sp->sp_out); | | 1459 | key_freesp_so(&sp->sp_out); |
1460 | #else | | 1460 | #else |
1461 | struct in6pcb *pcb = sotoin6pcb(so); | | 1461 | struct in6pcb *pcb = sotoin6pcb(so); |
1462 | | | 1462 | |
1463 | /* Does it have a PCB ? */ | | 1463 | /* Does it have a PCB ? */ |
1464 | if (pcb == NULL) | | 1464 | if (pcb == NULL) |
1465 | return; | | 1465 | return; |
1466 | key_freesp_so(&pcb->in6p_sp->sp_in); | | 1466 | key_freesp_so(&pcb->in6p_sp->sp_in); |
1467 | key_freesp_so(&pcb->in6p_sp->sp_out); | | 1467 | key_freesp_so(&pcb->in6p_sp->sp_out); |
1468 | #endif | | 1468 | #endif |
1469 | } | | 1469 | } |
1470 | break; | | 1470 | break; |
1471 | #endif /* INET6 */ | | 1471 | #endif /* INET6 */ |
1472 | default: | | 1472 | default: |
1473 | IPSECLOG(LOG_DEBUG, "unknown address family=%d.\n", | | 1473 | IPSECLOG(LOG_DEBUG, "unknown address family=%d.\n", |
1474 | so->so_proto->pr_domain->dom_family); | | 1474 | so->so_proto->pr_domain->dom_family); |
1475 | return; | | 1475 | return; |
1476 | } | | 1476 | } |
1477 | } | | 1477 | } |
1478 | | | 1478 | |
1479 | static void | | 1479 | static void |
1480 | key_freesp_so(struct secpolicy **sp) | | 1480 | key_freesp_so(struct secpolicy **sp) |
1481 | { | | 1481 | { |
1482 | | | 1482 | |
1483 | KASSERT(sp != NULL); | | 1483 | KASSERT(sp != NULL); |
1484 | KASSERT(*sp != NULL); | | 1484 | KASSERT(*sp != NULL); |
1485 | | | 1485 | |
1486 | if ((*sp)->policy == IPSEC_POLICY_ENTRUST || | | 1486 | if ((*sp)->policy == IPSEC_POLICY_ENTRUST || |
1487 | (*sp)->policy == IPSEC_POLICY_BYPASS) | | 1487 | (*sp)->policy == IPSEC_POLICY_BYPASS) |
1488 | return; | | 1488 | return; |
1489 | | | 1489 | |
1490 | KASSERTMSG((*sp)->policy == IPSEC_POLICY_IPSEC, | | 1490 | KASSERTMSG((*sp)->policy == IPSEC_POLICY_IPSEC, |
1491 | "invalid policy %u", (*sp)->policy); | | 1491 | "invalid policy %u", (*sp)->policy); |
1492 | KEY_SP_UNREF(&sp); | | 1492 | KEY_SP_UNREF(&sp); |
1493 | } | | 1493 | } |
1494 | #endif | | 1494 | #endif |
1495 | | | 1495 | |
1496 | #ifdef NET_MPSAFE | | 1496 | #ifdef NET_MPSAFE |
1497 | static void | | 1497 | static void |
1498 | key_sad_pserialize_perform(void) | | 1498 | key_sad_pserialize_perform(void) |
1499 | { | | 1499 | { |
1500 | | | 1500 | |
1501 | KASSERT(mutex_owned(&key_sad.lock)); | | 1501 | KASSERT(mutex_owned(&key_sad.lock)); |
1502 | | | 1502 | |
1503 | while (key_sad.psz_performing) | | 1503 | while (key_sad.psz_performing) |
1504 | cv_wait(&key_sad.cv_psz, &key_sad.lock); | | 1504 | cv_wait(&key_sad.cv_psz, &key_sad.lock); |
1505 | key_sad.psz_performing = true; | | 1505 | key_sad.psz_performing = true; |
1506 | mutex_exit(&key_sad.lock); | | 1506 | mutex_exit(&key_sad.lock); |
1507 | | | 1507 | |
1508 | pserialize_perform(key_sad.psz); | | 1508 | pserialize_perform(key_sad.psz); |
1509 | | | 1509 | |
1510 | mutex_enter(&key_sad.lock); | | 1510 | mutex_enter(&key_sad.lock); |
1511 | key_sad.psz_performing = false; | | 1511 | key_sad.psz_performing = false; |
1512 | cv_broadcast(&key_sad.cv_psz); | | 1512 | cv_broadcast(&key_sad.cv_psz); |
1513 | } | | 1513 | } |
1514 | #endif | | 1514 | #endif |
1515 | | | 1515 | |
1516 | /* | | 1516 | /* |
1517 | * Remove the sav from the savlist of its sah and wait for references to the sav | | 1517 | * Remove the sav from the savlist of its sah and wait for references to the sav |
1518 | * to be released. key_sad.lock must be held. | | 1518 | * to be released. key_sad.lock must be held. |
1519 | */ | | 1519 | */ |
1520 | static void | | 1520 | static void |
1521 | key_unlink_sav(struct secasvar *sav) | | 1521 | key_unlink_sav(struct secasvar *sav) |
1522 | { | | 1522 | { |
1523 | | | 1523 | |
1524 | KASSERT(mutex_owned(&key_sad.lock)); | | 1524 | KASSERT(mutex_owned(&key_sad.lock)); |
1525 | | | 1525 | |
1526 | SAVLIST_WRITER_REMOVE(sav); | | 1526 | SAVLIST_WRITER_REMOVE(sav); |
1527 | | | 1527 | |
1528 | #ifdef NET_MPSAFE | | 1528 | #ifdef NET_MPSAFE |
1529 | KASSERT(mutex_ownable(softnet_lock)); | | 1529 | KDASSERT(mutex_ownable(softnet_lock)); |
1530 | key_sad_pserialize_perform(); | | 1530 | key_sad_pserialize_perform(); |
1531 | #endif | | 1531 | #endif |
1532 | | | 1532 | |
1533 | localcount_drain(&sav->localcount, &key_sad.cv_lc, &key_sad.lock); | | 1533 | localcount_drain(&sav->localcount, &key_sad.cv_lc, &key_sad.lock); |
1534 | } | | 1534 | } |
1535 | | | 1535 | |
1536 | /* | | 1536 | /* |
1537 | * Destroy an sav where the sav must be unlinked from an sah | | 1537 | * Destroy an sav where the sav must be unlinked from an sah |
1538 | * by say key_unlink_sav. | | 1538 | * by say key_unlink_sav. |
1539 | */ | | 1539 | */ |
1540 | static void | | 1540 | static void |
1541 | key_destroy_sav(struct secasvar *sav) | | 1541 | key_destroy_sav(struct secasvar *sav) |
1542 | { | | 1542 | { |
1543 | | | 1543 | |
1544 | ASSERT_SLEEPABLE(); | | 1544 | ASSERT_SLEEPABLE(); |
1545 | | | 1545 | |
1546 | localcount_fini(&sav->localcount); | | 1546 | localcount_fini(&sav->localcount); |
1547 | SAVLIST_ENTRY_DESTROY(sav); | | 1547 | SAVLIST_ENTRY_DESTROY(sav); |
1548 | | | 1548 | |
1549 | key_delsav(sav); | | 1549 | key_delsav(sav); |
1550 | } | | 1550 | } |
1551 | | | 1551 | |
1552 | /* | | 1552 | /* |
1553 | * Destroy sav with holding its reference. | | 1553 | * Destroy sav with holding its reference. |
1554 | */ | | 1554 | */ |
1555 | static void | | 1555 | static void |
1556 | key_destroy_sav_with_ref(struct secasvar *sav) | | 1556 | key_destroy_sav_with_ref(struct secasvar *sav) |
1557 | { | | 1557 | { |
1558 | | | 1558 | |
1559 | ASSERT_SLEEPABLE(); | | 1559 | ASSERT_SLEEPABLE(); |
1560 | | | 1560 | |
1561 | mutex_enter(&key_sad.lock); | | 1561 | mutex_enter(&key_sad.lock); |
1562 | sav->state = SADB_SASTATE_DEAD; | | 1562 | sav->state = SADB_SASTATE_DEAD; |
1563 | SAVLIST_WRITER_REMOVE(sav); | | 1563 | SAVLIST_WRITER_REMOVE(sav); |
1564 | mutex_exit(&key_sad.lock); | | 1564 | mutex_exit(&key_sad.lock); |
1565 | | | 1565 | |
1566 | /* We cannot unref with holding key_sad.lock */ | | 1566 | /* We cannot unref with holding key_sad.lock */ |
1567 | KEY_SA_UNREF(&sav); | | 1567 | KEY_SA_UNREF(&sav); |
1568 | | | 1568 | |
1569 | mutex_enter(&key_sad.lock); | | 1569 | mutex_enter(&key_sad.lock); |
1570 | #ifdef NET_MPSAFE | | 1570 | #ifdef NET_MPSAFE |
1571 | KASSERT(mutex_ownable(softnet_lock)); | | 1571 | KDASSERT(mutex_ownable(softnet_lock)); |
1572 | key_sad_pserialize_perform(); | | 1572 | key_sad_pserialize_perform(); |
1573 | #endif | | 1573 | #endif |
1574 | localcount_drain(&sav->localcount, &key_sad.cv_lc, &key_sad.lock); | | 1574 | localcount_drain(&sav->localcount, &key_sad.cv_lc, &key_sad.lock); |
1575 | mutex_exit(&key_sad.lock); | | 1575 | mutex_exit(&key_sad.lock); |
1576 | | | 1576 | |
1577 | key_destroy_sav(sav); | | 1577 | key_destroy_sav(sav); |
1578 | } | | 1578 | } |
1579 | | | 1579 | |
1580 | /* %%% SPD management */ | | 1580 | /* %%% SPD management */ |
1581 | /* | | 1581 | /* |
1582 | * free security policy entry. | | 1582 | * free security policy entry. |
1583 | */ | | 1583 | */ |
1584 | static void | | 1584 | static void |
1585 | key_destroy_sp(struct secpolicy *sp) | | 1585 | key_destroy_sp(struct secpolicy *sp) |
1586 | { | | 1586 | { |
1587 | | | 1587 | |
1588 | SPLIST_ENTRY_DESTROY(sp); | | 1588 | SPLIST_ENTRY_DESTROY(sp); |
1589 | localcount_fini(&sp->localcount); | | 1589 | localcount_fini(&sp->localcount); |
1590 | | | 1590 | |
1591 | key_free_sp(sp); | | 1591 | key_free_sp(sp); |
1592 | | | 1592 | |
1593 | key_update_used(); | | 1593 | key_update_used(); |
1594 | } | | 1594 | } |
1595 | | | 1595 | |
1596 | void | | 1596 | void |
1597 | key_free_sp(struct secpolicy *sp) | | 1597 | key_free_sp(struct secpolicy *sp) |
1598 | { | | 1598 | { |
1599 | struct ipsecrequest *isr = sp->req, *nextisr; | | 1599 | struct ipsecrequest *isr = sp->req, *nextisr; |
1600 | | | 1600 | |
1601 | while (isr != NULL) { | | 1601 | while (isr != NULL) { |
1602 | nextisr = isr->next; | | 1602 | nextisr = isr->next; |
1603 | kmem_free(isr, sizeof(*isr)); | | 1603 | kmem_free(isr, sizeof(*isr)); |
1604 | isr = nextisr; | | 1604 | isr = nextisr; |
1605 | } | | 1605 | } |
1606 | | | 1606 | |
1607 | kmem_free(sp, sizeof(*sp)); | | 1607 | kmem_free(sp, sizeof(*sp)); |
1608 | } | | 1608 | } |
1609 | | | 1609 | |
1610 | void | | 1610 | void |
1611 | key_socksplist_add(struct secpolicy *sp) | | 1611 | key_socksplist_add(struct secpolicy *sp) |
1612 | { | | 1612 | { |
1613 | | | 1613 | |
1614 | mutex_enter(&key_spd.lock); | | 1614 | mutex_enter(&key_spd.lock); |
1615 | PSLIST_WRITER_INSERT_HEAD(&key_spd.socksplist, sp, pslist_entry); | | 1615 | PSLIST_WRITER_INSERT_HEAD(&key_spd.socksplist, sp, pslist_entry); |
1616 | mutex_exit(&key_spd.lock); | | 1616 | mutex_exit(&key_spd.lock); |
1617 | | | 1617 | |
1618 | key_update_used(); | | 1618 | key_update_used(); |
1619 | } | | 1619 | } |
1620 | | | 1620 | |
1621 | /* | | 1621 | /* |
1622 | * search SPD | | 1622 | * search SPD |
1623 | * OUT: NULL : not found | | 1623 | * OUT: NULL : not found |
1624 | * others : found, pointer to a SP. | | 1624 | * others : found, pointer to a SP. |
1625 | */ | | 1625 | */ |
1626 | static struct secpolicy * | | 1626 | static struct secpolicy * |
1627 | key_getsp(const struct secpolicyindex *spidx) | | 1627 | key_getsp(const struct secpolicyindex *spidx) |
1628 | { | | 1628 | { |
1629 | struct secpolicy *sp; | | 1629 | struct secpolicy *sp; |
1630 | int s; | | 1630 | int s; |
1631 | | | 1631 | |
1632 | KASSERT(spidx != NULL); | | 1632 | KASSERT(spidx != NULL); |
1633 | | | 1633 | |
1634 | s = pserialize_read_enter(); | | 1634 | s = pserialize_read_enter(); |
1635 | SPLIST_READER_FOREACH(sp, spidx->dir) { | | 1635 | SPLIST_READER_FOREACH(sp, spidx->dir) { |
1636 | if (sp->state == IPSEC_SPSTATE_DEAD) | | 1636 | if (sp->state == IPSEC_SPSTATE_DEAD) |
1637 | continue; | | 1637 | continue; |
1638 | if (key_spidx_match_exactly(spidx, &sp->spidx)) { | | 1638 | if (key_spidx_match_exactly(spidx, &sp->spidx)) { |
1639 | KEY_SP_REF(sp); | | 1639 | KEY_SP_REF(sp); |
1640 | pserialize_read_exit(s); | | 1640 | pserialize_read_exit(s); |
1641 | return sp; | | 1641 | return sp; |
1642 | } | | 1642 | } |
1643 | } | | 1643 | } |
1644 | pserialize_read_exit(s); | | 1644 | pserialize_read_exit(s); |
1645 | | | 1645 | |
1646 | return NULL; | | 1646 | return NULL; |
1647 | } | | 1647 | } |
1648 | | | 1648 | |
1649 | /* | | 1649 | /* |
1650 | * search SPD and remove found SP | | 1650 | * search SPD and remove found SP |
1651 | * OUT: NULL : not found | | 1651 | * OUT: NULL : not found |
1652 | * others : found, pointer to a SP. | | 1652 | * others : found, pointer to a SP. |
1653 | */ | | 1653 | */ |
1654 | static struct secpolicy * | | 1654 | static struct secpolicy * |
1655 | key_lookup_and_remove_sp(const struct secpolicyindex *spidx) | | 1655 | key_lookup_and_remove_sp(const struct secpolicyindex *spidx) |
1656 | { | | 1656 | { |
1657 | struct secpolicy *sp = NULL; | | 1657 | struct secpolicy *sp = NULL; |
1658 | | | 1658 | |
1659 | mutex_enter(&key_spd.lock); | | 1659 | mutex_enter(&key_spd.lock); |
1660 | SPLIST_WRITER_FOREACH(sp, spidx->dir) { | | 1660 | SPLIST_WRITER_FOREACH(sp, spidx->dir) { |
1661 | KASSERT(sp->state != IPSEC_SPSTATE_DEAD); | | 1661 | KASSERT(sp->state != IPSEC_SPSTATE_DEAD); |
1662 | | | 1662 | |
1663 | if (key_spidx_match_exactly(spidx, &sp->spidx)) { | | 1663 | if (key_spidx_match_exactly(spidx, &sp->spidx)) { |
1664 | key_unlink_sp(sp); | | 1664 | key_unlink_sp(sp); |
1665 | goto out; | | 1665 | goto out; |
1666 | } | | 1666 | } |
1667 | } | | 1667 | } |
1668 | sp = NULL; | | 1668 | sp = NULL; |
1669 | out: | | 1669 | out: |
1670 | mutex_exit(&key_spd.lock); | | 1670 | mutex_exit(&key_spd.lock); |
1671 | | | 1671 | |
1672 | return sp; | | 1672 | return sp; |
1673 | } | | 1673 | } |
1674 | | | 1674 | |
1675 | /* | | 1675 | /* |
1676 | * get SP by index. | | 1676 | * get SP by index. |
1677 | * OUT: NULL : not found | | 1677 | * OUT: NULL : not found |
1678 | * others : found, pointer to a SP. | | 1678 | * others : found, pointer to a SP. |
1679 | */ | | 1679 | */ |
1680 | static struct secpolicy * | | 1680 | static struct secpolicy * |
1681 | key_getspbyid(u_int32_t id) | | 1681 | key_getspbyid(u_int32_t id) |
1682 | { | | 1682 | { |
1683 | struct secpolicy *sp; | | 1683 | struct secpolicy *sp; |
1684 | int s; | | 1684 | int s; |
1685 | | | 1685 | |
1686 | s = pserialize_read_enter(); | | 1686 | s = pserialize_read_enter(); |
1687 | SPLIST_READER_FOREACH(sp, IPSEC_DIR_INBOUND) { | | 1687 | SPLIST_READER_FOREACH(sp, IPSEC_DIR_INBOUND) { |
1688 | if (sp->state == IPSEC_SPSTATE_DEAD) | | 1688 | if (sp->state == IPSEC_SPSTATE_DEAD) |
1689 | continue; | | 1689 | continue; |
1690 | if (sp->id == id) { | | 1690 | if (sp->id == id) { |
1691 | KEY_SP_REF(sp); | | 1691 | KEY_SP_REF(sp); |
1692 | goto out; | | 1692 | goto out; |
1693 | } | | 1693 | } |
1694 | } | | 1694 | } |
1695 | | | 1695 | |
1696 | SPLIST_READER_FOREACH(sp, IPSEC_DIR_OUTBOUND) { | | 1696 | SPLIST_READER_FOREACH(sp, IPSEC_DIR_OUTBOUND) { |
1697 | if (sp->state == IPSEC_SPSTATE_DEAD) | | 1697 | if (sp->state == IPSEC_SPSTATE_DEAD) |
1698 | continue; | | 1698 | continue; |
1699 | if (sp->id == id) { | | 1699 | if (sp->id == id) { |
1700 | KEY_SP_REF(sp); | | 1700 | KEY_SP_REF(sp); |
1701 | goto out; | | 1701 | goto out; |
1702 | } | | 1702 | } |
1703 | } | | 1703 | } |
1704 | out: | | 1704 | out: |
1705 | pserialize_read_exit(s); | | 1705 | pserialize_read_exit(s); |
1706 | return sp; | | 1706 | return sp; |
1707 | } | | 1707 | } |
1708 | | | 1708 | |
1709 | /* | | 1709 | /* |
1710 | * get SP by index, remove and return it. | | 1710 | * get SP by index, remove and return it. |
1711 | * OUT: NULL : not found | | 1711 | * OUT: NULL : not found |
1712 | * others : found, pointer to a SP. | | 1712 | * others : found, pointer to a SP. |
1713 | */ | | 1713 | */ |
1714 | static struct secpolicy * | | 1714 | static struct secpolicy * |
1715 | key_lookupbyid_and_remove_sp(u_int32_t id) | | 1715 | key_lookupbyid_and_remove_sp(u_int32_t id) |
1716 | { | | 1716 | { |
1717 | struct secpolicy *sp; | | 1717 | struct secpolicy *sp; |
1718 | | | 1718 | |
1719 | mutex_enter(&key_spd.lock); | | 1719 | mutex_enter(&key_spd.lock); |
1720 | SPLIST_READER_FOREACH(sp, IPSEC_DIR_INBOUND) { | | 1720 | SPLIST_READER_FOREACH(sp, IPSEC_DIR_INBOUND) { |
1721 | KASSERT(sp->state != IPSEC_SPSTATE_DEAD); | | 1721 | KASSERT(sp->state != IPSEC_SPSTATE_DEAD); |
1722 | if (sp->id == id) | | 1722 | if (sp->id == id) |
1723 | goto out; | | 1723 | goto out; |
1724 | } | | 1724 | } |
1725 | | | 1725 | |
1726 | SPLIST_READER_FOREACH(sp, IPSEC_DIR_OUTBOUND) { | | 1726 | SPLIST_READER_FOREACH(sp, IPSEC_DIR_OUTBOUND) { |
1727 | KASSERT(sp->state != IPSEC_SPSTATE_DEAD); | | 1727 | KASSERT(sp->state != IPSEC_SPSTATE_DEAD); |
1728 | if (sp->id == id) | | 1728 | if (sp->id == id) |
1729 | goto out; | | 1729 | goto out; |
1730 | } | | 1730 | } |
1731 | out: | | 1731 | out: |
1732 | if (sp != NULL) | | 1732 | if (sp != NULL) |
1733 | key_unlink_sp(sp); | | 1733 | key_unlink_sp(sp); |
1734 | mutex_exit(&key_spd.lock); | | 1734 | mutex_exit(&key_spd.lock); |
1735 | return sp; | | 1735 | return sp; |
1736 | } | | 1736 | } |
1737 | | | 1737 | |
1738 | struct secpolicy * | | 1738 | struct secpolicy * |
1739 | key_newsp(const char* where, int tag) | | 1739 | key_newsp(const char* where, int tag) |
1740 | { | | 1740 | { |
1741 | struct secpolicy *newsp = NULL; | | 1741 | struct secpolicy *newsp = NULL; |
1742 | | | 1742 | |
1743 | newsp = kmem_zalloc(sizeof(struct secpolicy), KM_SLEEP); | | 1743 | newsp = kmem_zalloc(sizeof(struct secpolicy), KM_SLEEP); |
1744 | | | 1744 | |
1745 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, | | 1745 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, |
1746 | "DP from %s:%u return SP:%p\n", where, tag, newsp); | | 1746 | "DP from %s:%u return SP:%p\n", where, tag, newsp); |
1747 | return newsp; | | 1747 | return newsp; |
1748 | } | | 1748 | } |
1749 | | | 1749 | |
1750 | /* | | 1750 | /* |
1751 | * create secpolicy structure from sadb_x_policy structure. | | 1751 | * create secpolicy structure from sadb_x_policy structure. |
1752 | * NOTE: `state', `secpolicyindex' in secpolicy structure are not set, | | 1752 | * NOTE: `state', `secpolicyindex' in secpolicy structure are not set, |
1753 | * so must be set properly later. | | 1753 | * so must be set properly later. |
1754 | */ | | 1754 | */ |
1755 | struct secpolicy * | | 1755 | struct secpolicy * |
1756 | key_msg2sp(const struct sadb_x_policy *xpl0, size_t len, int *error) | | 1756 | key_msg2sp(const struct sadb_x_policy *xpl0, size_t len, int *error) |
1757 | { | | 1757 | { |
1758 | struct secpolicy *newsp; | | 1758 | struct secpolicy *newsp; |
1759 | | | 1759 | |
1760 | KASSERT(!cpu_softintr_p()); | | 1760 | KASSERT(!cpu_softintr_p()); |
1761 | KASSERT(xpl0 != NULL); | | 1761 | KASSERT(xpl0 != NULL); |
1762 | KASSERT(len >= sizeof(*xpl0)); | | 1762 | KASSERT(len >= sizeof(*xpl0)); |
1763 | | | 1763 | |
1764 | if (len != PFKEY_EXTLEN(xpl0)) { | | 1764 | if (len != PFKEY_EXTLEN(xpl0)) { |
1765 | IPSECLOG(LOG_DEBUG, "Invalid msg length.\n"); | | 1765 | IPSECLOG(LOG_DEBUG, "Invalid msg length.\n"); |
1766 | *error = EINVAL; | | 1766 | *error = EINVAL; |
1767 | return NULL; | | 1767 | return NULL; |
1768 | } | | 1768 | } |
1769 | | | 1769 | |
1770 | newsp = KEY_NEWSP(); | | 1770 | newsp = KEY_NEWSP(); |
1771 | if (newsp == NULL) { | | 1771 | if (newsp == NULL) { |
1772 | *error = ENOBUFS; | | 1772 | *error = ENOBUFS; |
1773 | return NULL; | | 1773 | return NULL; |
1774 | } | | 1774 | } |
1775 | | | 1775 | |
1776 | newsp->spidx.dir = xpl0->sadb_x_policy_dir; | | 1776 | newsp->spidx.dir = xpl0->sadb_x_policy_dir; |
1777 | newsp->policy = xpl0->sadb_x_policy_type; | | 1777 | newsp->policy = xpl0->sadb_x_policy_type; |
1778 | | | 1778 | |
1779 | /* check policy */ | | 1779 | /* check policy */ |
1780 | switch (xpl0->sadb_x_policy_type) { | | 1780 | switch (xpl0->sadb_x_policy_type) { |
1781 | case IPSEC_POLICY_DISCARD: | | 1781 | case IPSEC_POLICY_DISCARD: |
1782 | case IPSEC_POLICY_NONE: | | 1782 | case IPSEC_POLICY_NONE: |
1783 | case IPSEC_POLICY_ENTRUST: | | 1783 | case IPSEC_POLICY_ENTRUST: |
1784 | case IPSEC_POLICY_BYPASS: | | 1784 | case IPSEC_POLICY_BYPASS: |
1785 | newsp->req = NULL; | | 1785 | newsp->req = NULL; |
1786 | *error = 0; | | 1786 | *error = 0; |
1787 | return newsp; | | 1787 | return newsp; |
1788 | | | 1788 | |
1789 | case IPSEC_POLICY_IPSEC: | | 1789 | case IPSEC_POLICY_IPSEC: |
1790 | /* Continued */ | | 1790 | /* Continued */ |
1791 | break; | | 1791 | break; |
1792 | default: | | 1792 | default: |
1793 | IPSECLOG(LOG_DEBUG, "invalid policy type.\n"); | | 1793 | IPSECLOG(LOG_DEBUG, "invalid policy type.\n"); |
1794 | key_free_sp(newsp); | | 1794 | key_free_sp(newsp); |
1795 | *error = EINVAL; | | 1795 | *error = EINVAL; |
1796 | return NULL; | | 1796 | return NULL; |
1797 | } | | 1797 | } |
1798 | | | 1798 | |
1799 | /* IPSEC_POLICY_IPSEC */ | | 1799 | /* IPSEC_POLICY_IPSEC */ |
1800 | { | | 1800 | { |
1801 | int tlen; | | 1801 | int tlen; |
1802 | const struct sadb_x_ipsecrequest *xisr; | | 1802 | const struct sadb_x_ipsecrequest *xisr; |
1803 | uint16_t xisr_reqid; | | 1803 | uint16_t xisr_reqid; |
1804 | struct ipsecrequest **p_isr = &newsp->req; | | 1804 | struct ipsecrequest **p_isr = &newsp->req; |
1805 | | | 1805 | |
1806 | /* validity check */ | | 1806 | /* validity check */ |
1807 | if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) { | | 1807 | if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) { |
1808 | IPSECLOG(LOG_DEBUG, "Invalid msg length.\n"); | | 1808 | IPSECLOG(LOG_DEBUG, "Invalid msg length.\n"); |
1809 | *error = EINVAL; | | 1809 | *error = EINVAL; |
1810 | goto free_exit; | | 1810 | goto free_exit; |
1811 | } | | 1811 | } |
1812 | | | 1812 | |
1813 | tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0); | | 1813 | tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0); |
1814 | xisr = (const struct sadb_x_ipsecrequest *)(xpl0 + 1); | | 1814 | xisr = (const struct sadb_x_ipsecrequest *)(xpl0 + 1); |
1815 | | | 1815 | |
1816 | while (tlen > 0) { | | 1816 | while (tlen > 0) { |
1817 | /* length check */ | | 1817 | /* length check */ |
1818 | if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) { | | 1818 | if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) { |
1819 | IPSECLOG(LOG_DEBUG, "invalid ipsecrequest length.\n"); | | 1819 | IPSECLOG(LOG_DEBUG, "invalid ipsecrequest length.\n"); |
1820 | *error = EINVAL; | | 1820 | *error = EINVAL; |
1821 | goto free_exit; | | 1821 | goto free_exit; |
1822 | } | | 1822 | } |
1823 | | | 1823 | |
1824 | /* allocate request buffer */ | | 1824 | /* allocate request buffer */ |
1825 | *p_isr = kmem_zalloc(sizeof(**p_isr), KM_SLEEP); | | 1825 | *p_isr = kmem_zalloc(sizeof(**p_isr), KM_SLEEP); |
1826 | | | 1826 | |
1827 | /* set values */ | | 1827 | /* set values */ |
1828 | (*p_isr)->next = NULL; | | 1828 | (*p_isr)->next = NULL; |
1829 | | | 1829 | |
1830 | switch (xisr->sadb_x_ipsecrequest_proto) { | | 1830 | switch (xisr->sadb_x_ipsecrequest_proto) { |
1831 | case IPPROTO_ESP: | | 1831 | case IPPROTO_ESP: |
1832 | case IPPROTO_AH: | | 1832 | case IPPROTO_AH: |
1833 | case IPPROTO_IPCOMP: | | 1833 | case IPPROTO_IPCOMP: |
1834 | break; | | 1834 | break; |
1835 | default: | | 1835 | default: |
1836 | IPSECLOG(LOG_DEBUG, "invalid proto type=%u\n", | | 1836 | IPSECLOG(LOG_DEBUG, "invalid proto type=%u\n", |
1837 | xisr->sadb_x_ipsecrequest_proto); | | 1837 | xisr->sadb_x_ipsecrequest_proto); |
1838 | *error = EPROTONOSUPPORT; | | 1838 | *error = EPROTONOSUPPORT; |
1839 | goto free_exit; | | 1839 | goto free_exit; |
1840 | } | | 1840 | } |
1841 | (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto; | | 1841 | (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto; |
1842 | | | 1842 | |
1843 | switch (xisr->sadb_x_ipsecrequest_mode) { | | 1843 | switch (xisr->sadb_x_ipsecrequest_mode) { |
1844 | case IPSEC_MODE_TRANSPORT: | | 1844 | case IPSEC_MODE_TRANSPORT: |
1845 | case IPSEC_MODE_TUNNEL: | | 1845 | case IPSEC_MODE_TUNNEL: |
1846 | break; | | 1846 | break; |
1847 | case IPSEC_MODE_ANY: | | 1847 | case IPSEC_MODE_ANY: |
1848 | default: | | 1848 | default: |
1849 | IPSECLOG(LOG_DEBUG, "invalid mode=%u\n", | | 1849 | IPSECLOG(LOG_DEBUG, "invalid mode=%u\n", |
1850 | xisr->sadb_x_ipsecrequest_mode); | | 1850 | xisr->sadb_x_ipsecrequest_mode); |
1851 | *error = EINVAL; | | 1851 | *error = EINVAL; |
1852 | goto free_exit; | | 1852 | goto free_exit; |
1853 | } | | 1853 | } |
1854 | (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode; | | 1854 | (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode; |
1855 | | | 1855 | |
1856 | switch (xisr->sadb_x_ipsecrequest_level) { | | 1856 | switch (xisr->sadb_x_ipsecrequest_level) { |
1857 | case IPSEC_LEVEL_DEFAULT: | | 1857 | case IPSEC_LEVEL_DEFAULT: |
1858 | case IPSEC_LEVEL_USE: | | 1858 | case IPSEC_LEVEL_USE: |
1859 | case IPSEC_LEVEL_REQUIRE: | | 1859 | case IPSEC_LEVEL_REQUIRE: |
1860 | break; | | 1860 | break; |
1861 | case IPSEC_LEVEL_UNIQUE: | | 1861 | case IPSEC_LEVEL_UNIQUE: |
1862 | xisr_reqid = xisr->sadb_x_ipsecrequest_reqid; | | 1862 | xisr_reqid = xisr->sadb_x_ipsecrequest_reqid; |
1863 | /* validity check */ | | 1863 | /* validity check */ |
1864 | /* | | 1864 | /* |
1865 | * If range violation of reqid, kernel will | | 1865 | * If range violation of reqid, kernel will |
1866 | * update it, don't refuse it. | | 1866 | * update it, don't refuse it. |
1867 | */ | | 1867 | */ |
1868 | if (xisr_reqid > IPSEC_MANUAL_REQID_MAX) { | | 1868 | if (xisr_reqid > IPSEC_MANUAL_REQID_MAX) { |
1869 | IPSECLOG(LOG_DEBUG, | | 1869 | IPSECLOG(LOG_DEBUG, |
1870 | "reqid=%d range " | | 1870 | "reqid=%d range " |
1871 | "violation, updated by kernel.\n", | | 1871 | "violation, updated by kernel.\n", |
1872 | xisr_reqid); | | 1872 | xisr_reqid); |
1873 | xisr_reqid = 0; | | 1873 | xisr_reqid = 0; |
1874 | } | | 1874 | } |
1875 | | | 1875 | |
1876 | /* allocate new reqid id if reqid is zero. */ | | 1876 | /* allocate new reqid id if reqid is zero. */ |
1877 | if (xisr_reqid == 0) { | | 1877 | if (xisr_reqid == 0) { |
1878 | u_int16_t reqid = key_newreqid(); | | 1878 | u_int16_t reqid = key_newreqid(); |
1879 | if (reqid == 0) { | | 1879 | if (reqid == 0) { |
1880 | *error = ENOBUFS; | | 1880 | *error = ENOBUFS; |
1881 | goto free_exit; | | 1881 | goto free_exit; |
1882 | } | | 1882 | } |
1883 | (*p_isr)->saidx.reqid = reqid; | | 1883 | (*p_isr)->saidx.reqid = reqid; |
1884 | } else { | | 1884 | } else { |
1885 | /* set it for manual keying. */ | | 1885 | /* set it for manual keying. */ |
1886 | (*p_isr)->saidx.reqid = xisr_reqid; | | 1886 | (*p_isr)->saidx.reqid = xisr_reqid; |
1887 | } | | 1887 | } |
1888 | break; | | 1888 | break; |
1889 | | | 1889 | |
1890 | default: | | 1890 | default: |
1891 | IPSECLOG(LOG_DEBUG, "invalid level=%u\n", | | 1891 | IPSECLOG(LOG_DEBUG, "invalid level=%u\n", |
1892 | xisr->sadb_x_ipsecrequest_level); | | 1892 | xisr->sadb_x_ipsecrequest_level); |
1893 | *error = EINVAL; | | 1893 | *error = EINVAL; |
1894 | goto free_exit; | | 1894 | goto free_exit; |
1895 | } | | 1895 | } |
1896 | (*p_isr)->level = xisr->sadb_x_ipsecrequest_level; | | 1896 | (*p_isr)->level = xisr->sadb_x_ipsecrequest_level; |
1897 | | | 1897 | |
1898 | /* set IP addresses if there */ | | 1898 | /* set IP addresses if there */ |
1899 | if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) { | | 1899 | if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) { |
1900 | const struct sockaddr *paddr; | | 1900 | const struct sockaddr *paddr; |
1901 | | | 1901 | |
1902 | paddr = (const struct sockaddr *)(xisr + 1); | | 1902 | paddr = (const struct sockaddr *)(xisr + 1); |
1903 | | | 1903 | |
1904 | /* validity check */ | | 1904 | /* validity check */ |
1905 | if (paddr->sa_len > sizeof((*p_isr)->saidx.src)) { | | 1905 | if (paddr->sa_len > sizeof((*p_isr)->saidx.src)) { |
1906 | IPSECLOG(LOG_DEBUG, "invalid request " | | 1906 | IPSECLOG(LOG_DEBUG, "invalid request " |
1907 | "address length.\n"); | | 1907 | "address length.\n"); |
1908 | *error = EINVAL; | | 1908 | *error = EINVAL; |
1909 | goto free_exit; | | 1909 | goto free_exit; |
1910 | } | | 1910 | } |
1911 | memcpy(&(*p_isr)->saidx.src, paddr, paddr->sa_len); | | 1911 | memcpy(&(*p_isr)->saidx.src, paddr, paddr->sa_len); |
1912 | | | 1912 | |
1913 | paddr = (const struct sockaddr *)((const char *)paddr | | 1913 | paddr = (const struct sockaddr *)((const char *)paddr |
1914 | + paddr->sa_len); | | 1914 | + paddr->sa_len); |
1915 | | | 1915 | |
1916 | /* validity check */ | | 1916 | /* validity check */ |
1917 | if (paddr->sa_len > sizeof((*p_isr)->saidx.dst)) { | | 1917 | if (paddr->sa_len > sizeof((*p_isr)->saidx.dst)) { |
1918 | IPSECLOG(LOG_DEBUG, "invalid request " | | 1918 | IPSECLOG(LOG_DEBUG, "invalid request " |
1919 | "address length.\n"); | | 1919 | "address length.\n"); |
1920 | *error = EINVAL; | | 1920 | *error = EINVAL; |
1921 | goto free_exit; | | 1921 | goto free_exit; |
1922 | } | | 1922 | } |
1923 | memcpy(&(*p_isr)->saidx.dst, paddr, paddr->sa_len); | | 1923 | memcpy(&(*p_isr)->saidx.dst, paddr, paddr->sa_len); |
1924 | } | | 1924 | } |
1925 | | | 1925 | |
1926 | (*p_isr)->sp = newsp; | | 1926 | (*p_isr)->sp = newsp; |
1927 | | | 1927 | |
1928 | /* initialization for the next. */ | | 1928 | /* initialization for the next. */ |
1929 | p_isr = &(*p_isr)->next; | | 1929 | p_isr = &(*p_isr)->next; |
1930 | tlen -= xisr->sadb_x_ipsecrequest_len; | | 1930 | tlen -= xisr->sadb_x_ipsecrequest_len; |
1931 | | | 1931 | |
1932 | /* validity check */ | | 1932 | /* validity check */ |
1933 | if (tlen < 0) { | | 1933 | if (tlen < 0) { |
1934 | IPSECLOG(LOG_DEBUG, "becoming tlen < 0.\n"); | | 1934 | IPSECLOG(LOG_DEBUG, "becoming tlen < 0.\n"); |
1935 | *error = EINVAL; | | 1935 | *error = EINVAL; |
1936 | goto free_exit; | | 1936 | goto free_exit; |
1937 | } | | 1937 | } |
1938 | | | 1938 | |
1939 | xisr = (const struct sadb_x_ipsecrequest *)((const char *)xisr + | | 1939 | xisr = (const struct sadb_x_ipsecrequest *)((const char *)xisr + |
1940 | xisr->sadb_x_ipsecrequest_len); | | 1940 | xisr->sadb_x_ipsecrequest_len); |
1941 | } | | 1941 | } |
1942 | } | | 1942 | } |
1943 | | | 1943 | |
1944 | *error = 0; | | 1944 | *error = 0; |
1945 | return newsp; | | 1945 | return newsp; |
1946 | | | 1946 | |
1947 | free_exit: | | 1947 | free_exit: |
1948 | key_free_sp(newsp); | | 1948 | key_free_sp(newsp); |
1949 | return NULL; | | 1949 | return NULL; |
1950 | } | | 1950 | } |
1951 | | | 1951 | |
1952 | static u_int16_t | | 1952 | static u_int16_t |
1953 | key_newreqid(void) | | 1953 | key_newreqid(void) |
1954 | { | | 1954 | { |
1955 | static u_int16_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1; | | 1955 | static u_int16_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1; |
1956 | | | 1956 | |
1957 | auto_reqid = (auto_reqid == 0xffff ? | | 1957 | auto_reqid = (auto_reqid == 0xffff ? |
1958 | IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1); | | 1958 | IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1); |
1959 | | | 1959 | |
1960 | /* XXX should be unique check */ | | 1960 | /* XXX should be unique check */ |
1961 | | | 1961 | |
1962 | return auto_reqid; | | 1962 | return auto_reqid; |
1963 | } | | 1963 | } |
1964 | | | 1964 | |
1965 | /* | | 1965 | /* |
1966 | * copy secpolicy struct to sadb_x_policy structure indicated. | | 1966 | * copy secpolicy struct to sadb_x_policy structure indicated. |
1967 | */ | | 1967 | */ |
1968 | struct mbuf * | | 1968 | struct mbuf * |
1969 | key_sp2msg(const struct secpolicy *sp, int mflag) | | 1969 | key_sp2msg(const struct secpolicy *sp, int mflag) |
1970 | { | | 1970 | { |
1971 | struct sadb_x_policy *xpl; | | 1971 | struct sadb_x_policy *xpl; |
1972 | int tlen; | | 1972 | int tlen; |
1973 | char *p; | | 1973 | char *p; |
1974 | struct mbuf *m; | | 1974 | struct mbuf *m; |
1975 | | | 1975 | |
1976 | KASSERT(sp != NULL); | | 1976 | KASSERT(sp != NULL); |
1977 | | | 1977 | |
1978 | tlen = key_getspreqmsglen(sp); | | 1978 | tlen = key_getspreqmsglen(sp); |
1979 | | | 1979 | |
1980 | m = key_alloc_mbuf(tlen, mflag); | | 1980 | m = key_alloc_mbuf(tlen, mflag); |
1981 | if (!m || m->m_next) { /*XXX*/ | | 1981 | if (!m || m->m_next) { /*XXX*/ |
1982 | if (m) | | 1982 | if (m) |
1983 | m_freem(m); | | 1983 | m_freem(m); |
1984 | return NULL; | | 1984 | return NULL; |
1985 | } | | 1985 | } |
1986 | | | 1986 | |
1987 | m->m_len = tlen; | | 1987 | m->m_len = tlen; |
1988 | m->m_next = NULL; | | 1988 | m->m_next = NULL; |
1989 | xpl = mtod(m, struct sadb_x_policy *); | | 1989 | xpl = mtod(m, struct sadb_x_policy *); |
1990 | memset(xpl, 0, tlen); | | 1990 | memset(xpl, 0, tlen); |
1991 | | | 1991 | |
1992 | xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen); | | 1992 | xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen); |
1993 | xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY; | | 1993 | xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY; |
1994 | xpl->sadb_x_policy_type = sp->policy; | | 1994 | xpl->sadb_x_policy_type = sp->policy; |
1995 | xpl->sadb_x_policy_dir = sp->spidx.dir; | | 1995 | xpl->sadb_x_policy_dir = sp->spidx.dir; |
1996 | xpl->sadb_x_policy_id = sp->id; | | 1996 | xpl->sadb_x_policy_id = sp->id; |
1997 | p = (char *)xpl + sizeof(*xpl); | | 1997 | p = (char *)xpl + sizeof(*xpl); |
1998 | | | 1998 | |
1999 | /* if is the policy for ipsec ? */ | | 1999 | /* if is the policy for ipsec ? */ |
2000 | if (sp->policy == IPSEC_POLICY_IPSEC) { | | 2000 | if (sp->policy == IPSEC_POLICY_IPSEC) { |
2001 | struct sadb_x_ipsecrequest *xisr; | | 2001 | struct sadb_x_ipsecrequest *xisr; |
2002 | struct ipsecrequest *isr; | | 2002 | struct ipsecrequest *isr; |
2003 | | | 2003 | |
2004 | for (isr = sp->req; isr != NULL; isr = isr->next) { | | 2004 | for (isr = sp->req; isr != NULL; isr = isr->next) { |
2005 | | | 2005 | |
2006 | xisr = (struct sadb_x_ipsecrequest *)p; | | 2006 | xisr = (struct sadb_x_ipsecrequest *)p; |
2007 | | | 2007 | |
2008 | xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto; | | 2008 | xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto; |
2009 | xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode; | | 2009 | xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode; |
2010 | xisr->sadb_x_ipsecrequest_level = isr->level; | | 2010 | xisr->sadb_x_ipsecrequest_level = isr->level; |
2011 | xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid; | | 2011 | xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid; |
2012 | | | 2012 | |
2013 | p += sizeof(*xisr); | | 2013 | p += sizeof(*xisr); |
2014 | memcpy(p, &isr->saidx.src, isr->saidx.src.sa.sa_len); | | 2014 | memcpy(p, &isr->saidx.src, isr->saidx.src.sa.sa_len); |
2015 | p += isr->saidx.src.sa.sa_len; | | 2015 | p += isr->saidx.src.sa.sa_len; |
2016 | memcpy(p, &isr->saidx.dst, isr->saidx.dst.sa.sa_len); | | 2016 | memcpy(p, &isr->saidx.dst, isr->saidx.dst.sa.sa_len); |
2017 | p += isr->saidx.src.sa.sa_len; | | 2017 | p += isr->saidx.src.sa.sa_len; |
2018 | | | 2018 | |
2019 | xisr->sadb_x_ipsecrequest_len = | | 2019 | xisr->sadb_x_ipsecrequest_len = |
2020 | PFKEY_ALIGN8(sizeof(*xisr) | | 2020 | PFKEY_ALIGN8(sizeof(*xisr) |
2021 | + isr->saidx.src.sa.sa_len | | 2021 | + isr->saidx.src.sa.sa_len |
2022 | + isr->saidx.dst.sa.sa_len); | | 2022 | + isr->saidx.dst.sa.sa_len); |
2023 | } | | 2023 | } |
2024 | } | | 2024 | } |
2025 | | | 2025 | |
2026 | return m; | | 2026 | return m; |
2027 | } | | 2027 | } |
2028 | | | 2028 | |
2029 | /* | | 2029 | /* |
2030 | * m will not be freed nor modified. It never return NULL. | | 2030 | * m will not be freed nor modified. It never return NULL. |
2031 | * If it returns a mbuf of M_PKTHDR, the mbuf ensures to have | | 2031 | * If it returns a mbuf of M_PKTHDR, the mbuf ensures to have |
2032 | * contiguous length at least sizeof(struct sadb_msg). | | 2032 | * contiguous length at least sizeof(struct sadb_msg). |
2033 | */ | | 2033 | */ |
2034 | static struct mbuf * | | 2034 | static struct mbuf * |
2035 | key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp, | | 2035 | key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp, |
2036 | int ndeep, int nitem, ...) | | 2036 | int ndeep, int nitem, ...) |
2037 | { | | 2037 | { |
2038 | va_list ap; | | 2038 | va_list ap; |
2039 | int idx; | | 2039 | int idx; |
2040 | int i; | | 2040 | int i; |
2041 | struct mbuf *result = NULL, *n; | | 2041 | struct mbuf *result = NULL, *n; |
2042 | int len; | | 2042 | int len; |
2043 | | | 2043 | |
2044 | KASSERT(m != NULL); | | 2044 | KASSERT(m != NULL); |
2045 | KASSERT(mhp != NULL); | | 2045 | KASSERT(mhp != NULL); |
2046 | KASSERT(!cpu_softintr_p()); | | 2046 | KASSERT(!cpu_softintr_p()); |
2047 | | | 2047 | |
2048 | va_start(ap, nitem); | | 2048 | va_start(ap, nitem); |
2049 | for (i = 0; i < nitem; i++) { | | 2049 | for (i = 0; i < nitem; i++) { |
2050 | idx = va_arg(ap, int); | | 2050 | idx = va_arg(ap, int); |
2051 | KASSERT(idx >= 0); | | 2051 | KASSERT(idx >= 0); |
2052 | KASSERT(idx <= SADB_EXT_MAX); | | 2052 | KASSERT(idx <= SADB_EXT_MAX); |
2053 | /* don't attempt to pull empty extension */ | | 2053 | /* don't attempt to pull empty extension */ |
2054 | if (idx == SADB_EXT_RESERVED && mhp->msg == NULL) | | 2054 | if (idx == SADB_EXT_RESERVED && mhp->msg == NULL) |
2055 | continue; | | 2055 | continue; |
2056 | if (idx != SADB_EXT_RESERVED && | | 2056 | if (idx != SADB_EXT_RESERVED && |
2057 | (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0)) | | 2057 | (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0)) |
2058 | continue; | | 2058 | continue; |
2059 | | | 2059 | |
2060 | if (idx == SADB_EXT_RESERVED) { | | 2060 | if (idx == SADB_EXT_RESERVED) { |
2061 | CTASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg)) <= MHLEN); | | 2061 | CTASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg)) <= MHLEN); |
2062 | len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); | | 2062 | len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); |
2063 | MGETHDR(n, M_WAITOK, MT_DATA); | | 2063 | MGETHDR(n, M_WAITOK, MT_DATA); |
2064 | n->m_len = len; | | 2064 | n->m_len = len; |
2065 | n->m_next = NULL; | | 2065 | n->m_next = NULL; |
2066 | m_copydata(m, 0, sizeof(struct sadb_msg), | | 2066 | m_copydata(m, 0, sizeof(struct sadb_msg), |
2067 | mtod(n, void *)); | | 2067 | mtod(n, void *)); |
2068 | } else if (i < ndeep) { | | 2068 | } else if (i < ndeep) { |
2069 | len = mhp->extlen[idx]; | | 2069 | len = mhp->extlen[idx]; |
2070 | n = key_alloc_mbuf(len, M_WAITOK); | | 2070 | n = key_alloc_mbuf(len, M_WAITOK); |
2071 | KASSERT(n->m_next == NULL); | | 2071 | KASSERT(n->m_next == NULL); |
2072 | m_copydata(m, mhp->extoff[idx], mhp->extlen[idx], | | 2072 | m_copydata(m, mhp->extoff[idx], mhp->extlen[idx], |
2073 | mtod(n, void *)); | | 2073 | mtod(n, void *)); |
2074 | } else { | | 2074 | } else { |
2075 | n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx], | | 2075 | n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx], |
2076 | M_WAITOK); | | 2076 | M_WAITOK); |
2077 | } | | 2077 | } |
2078 | KASSERT(n != NULL); | | 2078 | KASSERT(n != NULL); |
2079 | | | 2079 | |
2080 | if (result) | | 2080 | if (result) |
2081 | m_cat(result, n); | | 2081 | m_cat(result, n); |
2082 | else | | 2082 | else |
2083 | result = n; | | 2083 | result = n; |
2084 | } | | 2084 | } |
2085 | va_end(ap); | | 2085 | va_end(ap); |
2086 | | | 2086 | |
2087 | KASSERT(result != NULL); | | 2087 | KASSERT(result != NULL); |
2088 | if ((result->m_flags & M_PKTHDR) != 0) { | | 2088 | if ((result->m_flags & M_PKTHDR) != 0) { |
2089 | result->m_pkthdr.len = 0; | | 2089 | result->m_pkthdr.len = 0; |
2090 | for (n = result; n; n = n->m_next) | | 2090 | for (n = result; n; n = n->m_next) |
2091 | result->m_pkthdr.len += n->m_len; | | 2091 | result->m_pkthdr.len += n->m_len; |
2092 | KASSERT(result->m_len >= sizeof(struct sadb_msg)); | | 2092 | KASSERT(result->m_len >= sizeof(struct sadb_msg)); |
2093 | } | | 2093 | } |
2094 | | | 2094 | |
2095 | return result; | | 2095 | return result; |
2096 | } | | 2096 | } |
2097 | | | 2097 | |
2098 | /* | | 2098 | /* |
2099 | * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing | | 2099 | * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing |
2100 | * add an entry to SP database, when received | | 2100 | * add an entry to SP database, when received |
2101 | * <base, address(SD), (lifetime(H),) policy> | | 2101 | * <base, address(SD), (lifetime(H),) policy> |
2102 | * from the user(?). | | 2102 | * from the user(?). |
2103 | * Adding to SP database, | | 2103 | * Adding to SP database, |
2104 | * and send | | 2104 | * and send |
2105 | * <base, address(SD), (lifetime(H),) policy> | | 2105 | * <base, address(SD), (lifetime(H),) policy> |
2106 | * to the socket which was send. | | 2106 | * to the socket which was send. |
2107 | * | | 2107 | * |
2108 | * SPDADD set a unique policy entry. | | 2108 | * SPDADD set a unique policy entry. |
2109 | * SPDSETIDX like SPDADD without a part of policy requests. | | 2109 | * SPDSETIDX like SPDADD without a part of policy requests. |
2110 | * SPDUPDATE replace a unique policy entry. | | 2110 | * SPDUPDATE replace a unique policy entry. |
2111 | * | | 2111 | * |
2112 | * m will always be freed. | | 2112 | * m will always be freed. |
2113 | */ | | 2113 | */ |
2114 | static int | | 2114 | static int |
2115 | key_api_spdadd(struct socket *so, struct mbuf *m, | | 2115 | key_api_spdadd(struct socket *so, struct mbuf *m, |
2116 | const struct sadb_msghdr *mhp) | | 2116 | const struct sadb_msghdr *mhp) |
2117 | { | | 2117 | { |
2118 | const struct sockaddr *src, *dst; | | 2118 | const struct sockaddr *src, *dst; |
2119 | const struct sadb_x_policy *xpl0; | | 2119 | const struct sadb_x_policy *xpl0; |
2120 | struct sadb_x_policy *xpl; | | 2120 | struct sadb_x_policy *xpl; |
2121 | const struct sadb_lifetime *lft = NULL; | | 2121 | const struct sadb_lifetime *lft = NULL; |
2122 | struct secpolicyindex spidx; | | 2122 | struct secpolicyindex spidx; |
2123 | struct secpolicy *newsp; | | 2123 | struct secpolicy *newsp; |
2124 | int error; | | 2124 | int error; |
2125 | | | 2125 | |
2126 | if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || | | 2126 | if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || |
2127 | mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || | | 2127 | mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || |
2128 | mhp->ext[SADB_X_EXT_POLICY] == NULL) { | | 2128 | mhp->ext[SADB_X_EXT_POLICY] == NULL) { |
2129 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); | | 2129 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); |
2130 | return key_senderror(so, m, EINVAL); | | 2130 | return key_senderror(so, m, EINVAL); |
2131 | } | | 2131 | } |
2132 | if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || | | 2132 | if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || |
2133 | mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) || | | 2133 | mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) || |
2134 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { | | 2134 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { |
2135 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); | | 2135 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); |
2136 | return key_senderror(so, m, EINVAL); | | 2136 | return key_senderror(so, m, EINVAL); |
2137 | } | | 2137 | } |
2138 | if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) { | | 2138 | if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) { |
2139 | if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < | | 2139 | if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < |
2140 | sizeof(struct sadb_lifetime)) { | | 2140 | sizeof(struct sadb_lifetime)) { |
2141 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); | | 2141 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); |
2142 | return key_senderror(so, m, EINVAL); | | 2142 | return key_senderror(so, m, EINVAL); |
2143 | } | | 2143 | } |
2144 | lft = mhp->ext[SADB_EXT_LIFETIME_HARD]; | | 2144 | lft = mhp->ext[SADB_EXT_LIFETIME_HARD]; |
2145 | } | | 2145 | } |
2146 | | | 2146 | |
2147 | xpl0 = mhp->ext[SADB_X_EXT_POLICY]; | | 2147 | xpl0 = mhp->ext[SADB_X_EXT_POLICY]; |
2148 | | | 2148 | |
2149 | /* checking the direciton. */ | | 2149 | /* checking the direciton. */ |
2150 | switch (xpl0->sadb_x_policy_dir) { | | 2150 | switch (xpl0->sadb_x_policy_dir) { |
2151 | case IPSEC_DIR_INBOUND: | | 2151 | case IPSEC_DIR_INBOUND: |
2152 | case IPSEC_DIR_OUTBOUND: | | 2152 | case IPSEC_DIR_OUTBOUND: |
2153 | break; | | 2153 | break; |
2154 | default: | | 2154 | default: |
2155 | IPSECLOG(LOG_DEBUG, "Invalid SP direction.\n"); | | 2155 | IPSECLOG(LOG_DEBUG, "Invalid SP direction.\n"); |
2156 | return key_senderror(so, m, EINVAL); | | 2156 | return key_senderror(so, m, EINVAL); |
2157 | } | | 2157 | } |
2158 | | | 2158 | |
2159 | /* check policy */ | | 2159 | /* check policy */ |
2160 | /* key_api_spdadd() accepts DISCARD, NONE and IPSEC. */ | | 2160 | /* key_api_spdadd() accepts DISCARD, NONE and IPSEC. */ |
2161 | if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST || | | 2161 | if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST || |
2162 | xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) { | | 2162 | xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) { |
2163 | IPSECLOG(LOG_DEBUG, "Invalid policy type.\n"); | | 2163 | IPSECLOG(LOG_DEBUG, "Invalid policy type.\n"); |
2164 | return key_senderror(so, m, EINVAL); | | 2164 | return key_senderror(so, m, EINVAL); |
2165 | } | | 2165 | } |
2166 | | | 2166 | |
2167 | /* policy requests are mandatory when action is ipsec. */ | | 2167 | /* policy requests are mandatory when action is ipsec. */ |
2168 | if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX && | | 2168 | if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX && |
2169 | xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC && | | 2169 | xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC && |
2170 | mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) { | | 2170 | mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) { |
2171 | IPSECLOG(LOG_DEBUG, "some policy requests part required.\n"); | | 2171 | IPSECLOG(LOG_DEBUG, "some policy requests part required.\n"); |
2172 | return key_senderror(so, m, EINVAL); | | 2172 | return key_senderror(so, m, EINVAL); |
2173 | } | | 2173 | } |
2174 | | | 2174 | |
2175 | src = key_msghdr_get_sockaddr(mhp, SADB_EXT_ADDRESS_SRC); | | 2175 | src = key_msghdr_get_sockaddr(mhp, SADB_EXT_ADDRESS_SRC); |
2176 | dst = key_msghdr_get_sockaddr(mhp, SADB_EXT_ADDRESS_DST); | | 2176 | dst = key_msghdr_get_sockaddr(mhp, SADB_EXT_ADDRESS_DST); |
2177 | | | 2177 | |
2178 | /* sanity check on addr pair */ | | 2178 | /* sanity check on addr pair */ |
2179 | if (src->sa_family != dst->sa_family) | | 2179 | if (src->sa_family != dst->sa_family) |
2180 | return key_senderror(so, m, EINVAL); | | 2180 | return key_senderror(so, m, EINVAL); |
2181 | if (src->sa_len != dst->sa_len) | | 2181 | if (src->sa_len != dst->sa_len) |
2182 | return key_senderror(so, m, EINVAL); | | 2182 | return key_senderror(so, m, EINVAL); |
2183 | | | 2183 | |
2184 | key_init_spidx_bymsghdr(&spidx, mhp); | | 2184 | key_init_spidx_bymsghdr(&spidx, mhp); |
2185 | | | 2185 | |
2186 | /* | | 2186 | /* |
2187 | * checking there is SP already or not. | | 2187 | * checking there is SP already or not. |
2188 | * SPDUPDATE doesn't depend on whether there is a SP or not. | | 2188 | * SPDUPDATE doesn't depend on whether there is a SP or not. |
2189 | * If the type is either SPDADD or SPDSETIDX AND a SP is found, | | 2189 | * If the type is either SPDADD or SPDSETIDX AND a SP is found, |
2190 | * then error. | | 2190 | * then error. |
2191 | */ | | 2191 | */ |
2192 | { | | 2192 | { |
2193 | struct secpolicy *sp; | | 2193 | struct secpolicy *sp; |
2194 | | | 2194 | |
2195 | if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) { | | 2195 | if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) { |
2196 | sp = key_lookup_and_remove_sp(&spidx); | | 2196 | sp = key_lookup_and_remove_sp(&spidx); |
2197 | if (sp != NULL) | | 2197 | if (sp != NULL) |
2198 | key_destroy_sp(sp); | | 2198 | key_destroy_sp(sp); |
2199 | } else { | | 2199 | } else { |
2200 | sp = key_getsp(&spidx); | | 2200 | sp = key_getsp(&spidx); |
2201 | if (sp != NULL) { | | 2201 | if (sp != NULL) { |
2202 | KEY_SP_UNREF(&sp); | | 2202 | KEY_SP_UNREF(&sp); |
2203 | IPSECLOG(LOG_DEBUG, "a SP entry exists already.\n"); | | 2203 | IPSECLOG(LOG_DEBUG, "a SP entry exists already.\n"); |
2204 | return key_senderror(so, m, EEXIST); | | 2204 | return key_senderror(so, m, EEXIST); |
2205 | } | | 2205 | } |
2206 | } | | 2206 | } |
2207 | } | | 2207 | } |
2208 | | | 2208 | |
2209 | /* allocation new SP entry */ | | 2209 | /* allocation new SP entry */ |
2210 | newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error); | | 2210 | newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error); |
2211 | if (newsp == NULL) { | | 2211 | if (newsp == NULL) { |
2212 | return key_senderror(so, m, error); | | 2212 | return key_senderror(so, m, error); |
2213 | } | | 2213 | } |
2214 | | | 2214 | |
2215 | newsp->id = key_getnewspid(); | | 2215 | newsp->id = key_getnewspid(); |
2216 | if (newsp->id == 0) { | | 2216 | if (newsp->id == 0) { |
2217 | kmem_free(newsp, sizeof(*newsp)); | | 2217 | kmem_free(newsp, sizeof(*newsp)); |
2218 | return key_senderror(so, m, ENOBUFS); | | 2218 | return key_senderror(so, m, ENOBUFS); |
2219 | } | | 2219 | } |
2220 | | | 2220 | |
2221 | newsp->spidx = spidx; | | 2221 | newsp->spidx = spidx; |
2222 | newsp->created = time_uptime; | | 2222 | newsp->created = time_uptime; |
2223 | newsp->lastused = newsp->created; | | 2223 | newsp->lastused = newsp->created; |
2224 | newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0; | | 2224 | newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0; |
2225 | newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0; | | 2225 | newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0; |
2226 | | | 2226 | |
2227 | key_init_sp(newsp); | | 2227 | key_init_sp(newsp); |
2228 | | | 2228 | |
2229 | mutex_enter(&key_spd.lock); | | 2229 | mutex_enter(&key_spd.lock); |
2230 | SPLIST_WRITER_INSERT_TAIL(newsp->spidx.dir, newsp); | | 2230 | SPLIST_WRITER_INSERT_TAIL(newsp->spidx.dir, newsp); |
2231 | mutex_exit(&key_spd.lock); | | 2231 | mutex_exit(&key_spd.lock); |
2232 | | | 2232 | |
2233 | #ifdef notyet | | 2233 | #ifdef notyet |
2234 | /* delete the entry in key_misc.spacqlist */ | | 2234 | /* delete the entry in key_misc.spacqlist */ |
2235 | if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) { | | 2235 | if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) { |
2236 | struct secspacq *spacq = key_getspacq(&spidx); | | 2236 | struct secspacq *spacq = key_getspacq(&spidx); |
2237 | if (spacq != NULL) { | | 2237 | if (spacq != NULL) { |
2238 | /* reset counter in order to deletion by timehandler. */ | | 2238 | /* reset counter in order to deletion by timehandler. */ |
2239 | spacq->created = time_uptime; | | 2239 | spacq->created = time_uptime; |
2240 | spacq->count = 0; | | 2240 | spacq->count = 0; |
2241 | } | | 2241 | } |
2242 | } | | 2242 | } |
2243 | #endif | | 2243 | #endif |
2244 | | | 2244 | |
2245 | /* Invalidate all cached SPD pointers in the PCBs. */ | | 2245 | /* Invalidate all cached SPD pointers in the PCBs. */ |
2246 | ipsec_invalpcbcacheall(); | | 2246 | ipsec_invalpcbcacheall(); |
2247 | | | 2247 | |
2248 | #if defined(GATEWAY) | | 2248 | #if defined(GATEWAY) |
2249 | /* Invalidate the ipflow cache, as well. */ | | 2249 | /* Invalidate the ipflow cache, as well. */ |
2250 | ipflow_invalidate_all(0); | | 2250 | ipflow_invalidate_all(0); |
2251 | #ifdef INET6 | | 2251 | #ifdef INET6 |
2252 | if (in6_present) | | 2252 | if (in6_present) |
2253 | ip6flow_invalidate_all(0); | | 2253 | ip6flow_invalidate_all(0); |
2254 | #endif /* INET6 */ | | 2254 | #endif /* INET6 */ |
2255 | #endif /* GATEWAY */ | | 2255 | #endif /* GATEWAY */ |
2256 | | | 2256 | |
2257 | key_update_used(); | | 2257 | key_update_used(); |
2258 | | | 2258 | |
2259 | { | | 2259 | { |
2260 | struct mbuf *n, *mpolicy; | | 2260 | struct mbuf *n, *mpolicy; |
2261 | int off; | | 2261 | int off; |
2262 | | | 2262 | |
2263 | /* create new sadb_msg to reply. */ | | 2263 | /* create new sadb_msg to reply. */ |
2264 | if (lft) { | | 2264 | if (lft) { |
2265 | n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED, | | 2265 | n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED, |
2266 | SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD, | | 2266 | SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD, |
2267 | SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); | | 2267 | SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); |
2268 | } else { | | 2268 | } else { |
2269 | n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED, | | 2269 | n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED, |
2270 | SADB_X_EXT_POLICY, | | 2270 | SADB_X_EXT_POLICY, |
2271 | SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); | | 2271 | SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); |
2272 | } | | 2272 | } |
2273 | | | 2273 | |
2274 | key_fill_replymsg(n, 0); | | 2274 | key_fill_replymsg(n, 0); |
2275 | off = 0; | | 2275 | off = 0; |
2276 | mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)), | | 2276 | mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)), |
2277 | sizeof(*xpl), &off); | | 2277 | sizeof(*xpl), &off); |
2278 | if (mpolicy == NULL) { | | 2278 | if (mpolicy == NULL) { |
2279 | /* n is already freed */ | | 2279 | /* n is already freed */ |
2280 | return key_senderror(so, m, ENOBUFS); | | 2280 | return key_senderror(so, m, ENOBUFS); |
2281 | } | | 2281 | } |
2282 | xpl = (struct sadb_x_policy *)(mtod(mpolicy, char *) + off); | | 2282 | xpl = (struct sadb_x_policy *)(mtod(mpolicy, char *) + off); |
2283 | if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) { | | 2283 | if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) { |
2284 | m_freem(n); | | 2284 | m_freem(n); |
2285 | return key_senderror(so, m, EINVAL); | | 2285 | return key_senderror(so, m, EINVAL); |
2286 | } | | 2286 | } |
2287 | xpl->sadb_x_policy_id = newsp->id; | | 2287 | xpl->sadb_x_policy_id = newsp->id; |
2288 | | | 2288 | |
2289 | m_freem(m); | | 2289 | m_freem(m); |
2290 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); | | 2290 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); |
2291 | } | | 2291 | } |
2292 | } | | 2292 | } |
2293 | | | 2293 | |
2294 | /* | | 2294 | /* |
2295 | * get new policy id. | | 2295 | * get new policy id. |
2296 | * OUT: | | 2296 | * OUT: |
2297 | * 0: failure. | | 2297 | * 0: failure. |
2298 | * others: success. | | 2298 | * others: success. |
2299 | */ | | 2299 | */ |
2300 | static u_int32_t | | 2300 | static u_int32_t |
2301 | key_getnewspid(void) | | 2301 | key_getnewspid(void) |
2302 | { | | 2302 | { |
2303 | u_int32_t newid = 0; | | 2303 | u_int32_t newid = 0; |
2304 | int count = key_spi_trycnt; /* XXX */ | | 2304 | int count = key_spi_trycnt; /* XXX */ |
2305 | struct secpolicy *sp; | | 2305 | struct secpolicy *sp; |
2306 | | | 2306 | |
2307 | /* when requesting to allocate spi ranged */ | | 2307 | /* when requesting to allocate spi ranged */ |
2308 | while (count--) { | | 2308 | while (count--) { |
2309 | newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1)); | | 2309 | newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1)); |
2310 | | | 2310 | |
2311 | sp = key_getspbyid(newid); | | 2311 | sp = key_getspbyid(newid); |
2312 | if (sp == NULL) | | 2312 | if (sp == NULL) |
2313 | break; | | 2313 | break; |
2314 | | | 2314 | |
2315 | KEY_SP_UNREF(&sp); | | 2315 | KEY_SP_UNREF(&sp); |
2316 | } | | 2316 | } |
2317 | | | 2317 | |
2318 | if (count == 0 || newid == 0) { | | 2318 | if (count == 0 || newid == 0) { |
2319 | IPSECLOG(LOG_DEBUG, "to allocate policy id is failed.\n"); | | 2319 | IPSECLOG(LOG_DEBUG, "to allocate policy id is failed.\n"); |
2320 | return 0; | | 2320 | return 0; |
2321 | } | | 2321 | } |
2322 | | | 2322 | |
2323 | return newid; | | 2323 | return newid; |
2324 | } | | 2324 | } |
2325 | | | 2325 | |
2326 | /* | | 2326 | /* |
2327 | * SADB_SPDDELETE processing | | 2327 | * SADB_SPDDELETE processing |
2328 | * receive | | 2328 | * receive |
2329 | * <base, address(SD), policy(*)> | | 2329 | * <base, address(SD), policy(*)> |
2330 | * from the user(?), and set SADB_SASTATE_DEAD, | | 2330 | * from the user(?), and set SADB_SASTATE_DEAD, |
2331 | * and send, | | 2331 | * and send, |
2332 | * <base, address(SD), policy(*)> | | 2332 | * <base, address(SD), policy(*)> |
2333 | * to the ikmpd. | | 2333 | * to the ikmpd. |
2334 | * policy(*) including direction of policy. | | 2334 | * policy(*) including direction of policy. |
2335 | * | | 2335 | * |
2336 | * m will always be freed. | | 2336 | * m will always be freed. |
2337 | */ | | 2337 | */ |
2338 | static int | | 2338 | static int |
2339 | key_api_spddelete(struct socket *so, struct mbuf *m, | | 2339 | key_api_spddelete(struct socket *so, struct mbuf *m, |
2340 | const struct sadb_msghdr *mhp) | | 2340 | const struct sadb_msghdr *mhp) |
2341 | { | | 2341 | { |
2342 | struct sadb_x_policy *xpl0; | | 2342 | struct sadb_x_policy *xpl0; |
2343 | struct secpolicyindex spidx; | | 2343 | struct secpolicyindex spidx; |
2344 | struct secpolicy *sp; | | 2344 | struct secpolicy *sp; |
2345 | | | 2345 | |
2346 | if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || | | 2346 | if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL || |
2347 | mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || | | 2347 | mhp->ext[SADB_EXT_ADDRESS_DST] == NULL || |
2348 | mhp->ext[SADB_X_EXT_POLICY] == NULL) { | | 2348 | mhp->ext[SADB_X_EXT_POLICY] == NULL) { |
2349 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); | | 2349 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); |
2350 | return key_senderror(so, m, EINVAL); | | 2350 | return key_senderror(so, m, EINVAL); |
2351 | } | | 2351 | } |
2352 | if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || | | 2352 | if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) || |
2353 | mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) || | | 2353 | mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) || |
2354 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { | | 2354 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { |
2355 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); | | 2355 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); |
2356 | return key_senderror(so, m, EINVAL); | | 2356 | return key_senderror(so, m, EINVAL); |
2357 | } | | 2357 | } |
2358 | | | 2358 | |
2359 | xpl0 = mhp->ext[SADB_X_EXT_POLICY]; | | 2359 | xpl0 = mhp->ext[SADB_X_EXT_POLICY]; |
2360 | | | 2360 | |
2361 | /* checking the directon. */ | | 2361 | /* checking the directon. */ |
2362 | switch (xpl0->sadb_x_policy_dir) { | | 2362 | switch (xpl0->sadb_x_policy_dir) { |
2363 | case IPSEC_DIR_INBOUND: | | 2363 | case IPSEC_DIR_INBOUND: |
2364 | case IPSEC_DIR_OUTBOUND: | | 2364 | case IPSEC_DIR_OUTBOUND: |
2365 | break; | | 2365 | break; |
2366 | default: | | 2366 | default: |
2367 | IPSECLOG(LOG_DEBUG, "Invalid SP direction.\n"); | | 2367 | IPSECLOG(LOG_DEBUG, "Invalid SP direction.\n"); |
2368 | return key_senderror(so, m, EINVAL); | | 2368 | return key_senderror(so, m, EINVAL); |
2369 | } | | 2369 | } |
2370 | | | 2370 | |
2371 | /* make secindex */ | | 2371 | /* make secindex */ |
2372 | key_init_spidx_bymsghdr(&spidx, mhp); | | 2372 | key_init_spidx_bymsghdr(&spidx, mhp); |
2373 | | | 2373 | |
2374 | /* Is there SP in SPD ? */ | | 2374 | /* Is there SP in SPD ? */ |
2375 | sp = key_lookup_and_remove_sp(&spidx); | | 2375 | sp = key_lookup_and_remove_sp(&spidx); |
2376 | if (sp == NULL) { | | 2376 | if (sp == NULL) { |
2377 | IPSECLOG(LOG_DEBUG, "no SP found.\n"); | | 2377 | IPSECLOG(LOG_DEBUG, "no SP found.\n"); |
2378 | return key_senderror(so, m, EINVAL); | | 2378 | return key_senderror(so, m, EINVAL); |
2379 | } | | 2379 | } |
2380 | | | 2380 | |
2381 | /* save policy id to buffer to be returned. */ | | 2381 | /* save policy id to buffer to be returned. */ |
2382 | xpl0->sadb_x_policy_id = sp->id; | | 2382 | xpl0->sadb_x_policy_id = sp->id; |
2383 | | | 2383 | |
2384 | key_destroy_sp(sp); | | 2384 | key_destroy_sp(sp); |
2385 | | | 2385 | |
2386 | /* We're deleting policy; no need to invalidate the ipflow cache. */ | | 2386 | /* We're deleting policy; no need to invalidate the ipflow cache. */ |
2387 | | | 2387 | |
2388 | { | | 2388 | { |
2389 | struct mbuf *n; | | 2389 | struct mbuf *n; |
2390 | | | 2390 | |
2391 | /* create new sadb_msg to reply. */ | | 2391 | /* create new sadb_msg to reply. */ |
2392 | n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED, | | 2392 | n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED, |
2393 | SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); | | 2393 | SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST); |
2394 | key_fill_replymsg(n, 0); | | 2394 | key_fill_replymsg(n, 0); |
2395 | m_freem(m); | | 2395 | m_freem(m); |
2396 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); | | 2396 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); |
2397 | } | | 2397 | } |
2398 | } | | 2398 | } |
2399 | | | 2399 | |
2400 | static struct mbuf * | | 2400 | static struct mbuf * |
2401 | key_alloc_mbuf_simple(int len, int mflag) | | 2401 | key_alloc_mbuf_simple(int len, int mflag) |
2402 | { | | 2402 | { |
2403 | struct mbuf *n; | | 2403 | struct mbuf *n; |
2404 | | | 2404 | |
2405 | KASSERT(mflag == M_NOWAIT || (mflag == M_WAITOK && !cpu_softintr_p())); | | 2405 | KASSERT(mflag == M_NOWAIT || (mflag == M_WAITOK && !cpu_softintr_p())); |
2406 | | | 2406 | |
2407 | MGETHDR(n, mflag, MT_DATA); | | 2407 | MGETHDR(n, mflag, MT_DATA); |
2408 | if (n && len > MHLEN) { | | 2408 | if (n && len > MHLEN) { |
2409 | MCLGET(n, mflag); | | 2409 | MCLGET(n, mflag); |
2410 | if ((n->m_flags & M_EXT) == 0) { | | 2410 | if ((n->m_flags & M_EXT) == 0) { |
2411 | m_freem(n); | | 2411 | m_freem(n); |
2412 | n = NULL; | | 2412 | n = NULL; |
2413 | } | | 2413 | } |
2414 | } | | 2414 | } |
2415 | return n; | | 2415 | return n; |
2416 | } | | 2416 | } |
2417 | | | 2417 | |
2418 | /* | | 2418 | /* |
2419 | * SADB_SPDDELETE2 processing | | 2419 | * SADB_SPDDELETE2 processing |
2420 | * receive | | 2420 | * receive |
2421 | * <base, policy(*)> | | 2421 | * <base, policy(*)> |
2422 | * from the user(?), and set SADB_SASTATE_DEAD, | | 2422 | * from the user(?), and set SADB_SASTATE_DEAD, |
2423 | * and send, | | 2423 | * and send, |
2424 | * <base, policy(*)> | | 2424 | * <base, policy(*)> |
2425 | * to the ikmpd. | | 2425 | * to the ikmpd. |
2426 | * policy(*) including direction of policy. | | 2426 | * policy(*) including direction of policy. |
2427 | * | | 2427 | * |
2428 | * m will always be freed. | | 2428 | * m will always be freed. |
2429 | */ | | 2429 | */ |
2430 | static int | | 2430 | static int |
2431 | key_api_spddelete2(struct socket *so, struct mbuf *m, | | 2431 | key_api_spddelete2(struct socket *so, struct mbuf *m, |
2432 | const struct sadb_msghdr *mhp) | | 2432 | const struct sadb_msghdr *mhp) |
2433 | { | | 2433 | { |
2434 | u_int32_t id; | | 2434 | u_int32_t id; |
2435 | struct secpolicy *sp; | | 2435 | struct secpolicy *sp; |
2436 | const struct sadb_x_policy *xpl; | | 2436 | const struct sadb_x_policy *xpl; |
2437 | | | 2437 | |
2438 | if (mhp->ext[SADB_X_EXT_POLICY] == NULL || | | 2438 | if (mhp->ext[SADB_X_EXT_POLICY] == NULL || |
2439 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { | | 2439 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { |
2440 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); | | 2440 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); |
2441 | return key_senderror(so, m, EINVAL); | | 2441 | return key_senderror(so, m, EINVAL); |
2442 | } | | 2442 | } |
2443 | | | 2443 | |
2444 | xpl = mhp->ext[SADB_X_EXT_POLICY]; | | 2444 | xpl = mhp->ext[SADB_X_EXT_POLICY]; |
2445 | id = xpl->sadb_x_policy_id; | | 2445 | id = xpl->sadb_x_policy_id; |
2446 | | | 2446 | |
2447 | /* Is there SP in SPD ? */ | | 2447 | /* Is there SP in SPD ? */ |
2448 | sp = key_lookupbyid_and_remove_sp(id); | | 2448 | sp = key_lookupbyid_and_remove_sp(id); |
2449 | if (sp == NULL) { | | 2449 | if (sp == NULL) { |
2450 | IPSECLOG(LOG_DEBUG, "no SP found id:%u.\n", id); | | 2450 | IPSECLOG(LOG_DEBUG, "no SP found id:%u.\n", id); |
2451 | return key_senderror(so, m, EINVAL); | | 2451 | return key_senderror(so, m, EINVAL); |
2452 | } | | 2452 | } |
2453 | | | 2453 | |
2454 | key_destroy_sp(sp); | | 2454 | key_destroy_sp(sp); |
2455 | | | 2455 | |
2456 | /* We're deleting policy; no need to invalidate the ipflow cache. */ | | 2456 | /* We're deleting policy; no need to invalidate the ipflow cache. */ |
2457 | | | 2457 | |
2458 | { | | 2458 | { |
2459 | struct mbuf *n, *nn; | | 2459 | struct mbuf *n, *nn; |
2460 | int off, len; | | 2460 | int off, len; |
2461 | | | 2461 | |
2462 | CTASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg)) <= MCLBYTES); | | 2462 | CTASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg)) <= MCLBYTES); |
2463 | | | 2463 | |
2464 | /* create new sadb_msg to reply. */ | | 2464 | /* create new sadb_msg to reply. */ |
2465 | len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); | | 2465 | len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); |
2466 | | | 2466 | |
2467 | n = key_alloc_mbuf_simple(len, M_WAITOK); | | 2467 | n = key_alloc_mbuf_simple(len, M_WAITOK); |
2468 | n->m_len = len; | | 2468 | n->m_len = len; |
2469 | n->m_next = NULL; | | 2469 | n->m_next = NULL; |
2470 | off = 0; | | 2470 | off = 0; |
2471 | | | 2471 | |
2472 | m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, char *) + off); | | 2472 | m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, char *) + off); |
2473 | off += PFKEY_ALIGN8(sizeof(struct sadb_msg)); | | 2473 | off += PFKEY_ALIGN8(sizeof(struct sadb_msg)); |
2474 | | | 2474 | |
2475 | KASSERTMSG(off == len, "length inconsistency"); | | 2475 | KASSERTMSG(off == len, "length inconsistency"); |
2476 | | | 2476 | |
2477 | n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY], | | 2477 | n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY], |
2478 | mhp->extlen[SADB_X_EXT_POLICY], M_WAITOK); | | 2478 | mhp->extlen[SADB_X_EXT_POLICY], M_WAITOK); |
2479 | | | 2479 | |
2480 | n->m_pkthdr.len = 0; | | 2480 | n->m_pkthdr.len = 0; |
2481 | for (nn = n; nn; nn = nn->m_next) | | 2481 | for (nn = n; nn; nn = nn->m_next) |
2482 | n->m_pkthdr.len += nn->m_len; | | 2482 | n->m_pkthdr.len += nn->m_len; |
2483 | | | 2483 | |
2484 | key_fill_replymsg(n, 0); | | 2484 | key_fill_replymsg(n, 0); |
2485 | m_freem(m); | | 2485 | m_freem(m); |
2486 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); | | 2486 | return key_sendup_mbuf(so, n, KEY_SENDUP_ALL); |
2487 | } | | 2487 | } |
2488 | } | | 2488 | } |
2489 | | | 2489 | |
2490 | /* | | 2490 | /* |
2491 | * SADB_X_GET processing | | 2491 | * SADB_X_GET processing |
2492 | * receive | | 2492 | * receive |
2493 | * <base, policy(*)> | | 2493 | * <base, policy(*)> |
2494 | * from the user(?), | | 2494 | * from the user(?), |
2495 | * and send, | | 2495 | * and send, |
2496 | * <base, address(SD), policy> | | 2496 | * <base, address(SD), policy> |
2497 | * to the ikmpd. | | 2497 | * to the ikmpd. |
2498 | * policy(*) including direction of policy. | | 2498 | * policy(*) including direction of policy. |
2499 | * | | 2499 | * |
2500 | * m will always be freed. | | 2500 | * m will always be freed. |
2501 | */ | | 2501 | */ |
2502 | static int | | 2502 | static int |
2503 | key_api_spdget(struct socket *so, struct mbuf *m, | | 2503 | key_api_spdget(struct socket *so, struct mbuf *m, |
2504 | const struct sadb_msghdr *mhp) | | 2504 | const struct sadb_msghdr *mhp) |
2505 | { | | 2505 | { |
2506 | u_int32_t id; | | 2506 | u_int32_t id; |
2507 | struct secpolicy *sp; | | 2507 | struct secpolicy *sp; |
2508 | struct mbuf *n; | | 2508 | struct mbuf *n; |
2509 | const struct sadb_x_policy *xpl; | | 2509 | const struct sadb_x_policy *xpl; |
2510 | | | 2510 | |
2511 | if (mhp->ext[SADB_X_EXT_POLICY] == NULL || | | 2511 | if (mhp->ext[SADB_X_EXT_POLICY] == NULL || |
2512 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { | | 2512 | mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) { |
2513 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); | | 2513 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); |
2514 | return key_senderror(so, m, EINVAL); | | 2514 | return key_senderror(so, m, EINVAL); |
2515 | } | | 2515 | } |
2516 | | | 2516 | |
2517 | xpl = mhp->ext[SADB_X_EXT_POLICY]; | | 2517 | xpl = mhp->ext[SADB_X_EXT_POLICY]; |
2518 | id = xpl->sadb_x_policy_id; | | 2518 | id = xpl->sadb_x_policy_id; |
2519 | | | 2519 | |
2520 | /* Is there SP in SPD ? */ | | 2520 | /* Is there SP in SPD ? */ |
2521 | sp = key_getspbyid(id); | | 2521 | sp = key_getspbyid(id); |
2522 | if (sp == NULL) { | | 2522 | if (sp == NULL) { |
2523 | IPSECLOG(LOG_DEBUG, "no SP found id:%u.\n", id); | | 2523 | IPSECLOG(LOG_DEBUG, "no SP found id:%u.\n", id); |
2524 | return key_senderror(so, m, ENOENT); | | 2524 | return key_senderror(so, m, ENOENT); |
2525 | } | | 2525 | } |
2526 | | | 2526 | |
2527 | n = key_setdumpsp(sp, SADB_X_SPDGET, mhp->msg->sadb_msg_seq, | | 2527 | n = key_setdumpsp(sp, SADB_X_SPDGET, mhp->msg->sadb_msg_seq, |
2528 | mhp->msg->sadb_msg_pid); | | 2528 | mhp->msg->sadb_msg_pid); |
2529 | KEY_SP_UNREF(&sp); /* ref gained by key_getspbyid */ | | 2529 | KEY_SP_UNREF(&sp); /* ref gained by key_getspbyid */ |
2530 | m_freem(m); | | 2530 | m_freem(m); |
2531 | return key_sendup_mbuf(so, n, KEY_SENDUP_ONE); | | 2531 | return key_sendup_mbuf(so, n, KEY_SENDUP_ONE); |
2532 | } | | 2532 | } |
2533 | | | 2533 | |
2534 | #ifdef notyet | | 2534 | #ifdef notyet |
2535 | /* | | 2535 | /* |
2536 | * SADB_X_SPDACQUIRE processing. | | 2536 | * SADB_X_SPDACQUIRE processing. |
2537 | * Acquire policy and SA(s) for a *OUTBOUND* packet. | | 2537 | * Acquire policy and SA(s) for a *OUTBOUND* packet. |
2538 | * send | | 2538 | * send |
2539 | * <base, policy(*)> | | 2539 | * <base, policy(*)> |
2540 | * to KMD, and expect to receive | | 2540 | * to KMD, and expect to receive |
2541 | * <base> with SADB_X_SPDACQUIRE if error occurred, | | 2541 | * <base> with SADB_X_SPDACQUIRE if error occurred, |
2542 | * or | | 2542 | * or |
2543 | * <base, policy> | | 2543 | * <base, policy> |
2544 | * with SADB_X_SPDUPDATE from KMD by PF_KEY. | | 2544 | * with SADB_X_SPDUPDATE from KMD by PF_KEY. |
2545 | * policy(*) is without policy requests. | | 2545 | * policy(*) is without policy requests. |
2546 | * | | 2546 | * |
2547 | * 0 : succeed | | 2547 | * 0 : succeed |
2548 | * others: error number | | 2548 | * others: error number |
2549 | */ | | 2549 | */ |
2550 | int | | 2550 | int |
2551 | key_spdacquire(const struct secpolicy *sp) | | 2551 | key_spdacquire(const struct secpolicy *sp) |
2552 | { | | 2552 | { |
2553 | struct mbuf *result = NULL, *m; | | 2553 | struct mbuf *result = NULL, *m; |
2554 | struct secspacq *newspacq; | | 2554 | struct secspacq *newspacq; |
2555 | int error; | | 2555 | int error; |
2556 | | | 2556 | |
2557 | KASSERT(sp != NULL); | | 2557 | KASSERT(sp != NULL); |
2558 | KASSERTMSG(sp->req == NULL, "called but there is request"); | | 2558 | KASSERTMSG(sp->req == NULL, "called but there is request"); |
2559 | KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC, | | 2559 | KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC, |
2560 | "policy mismathed. IPsec is expected"); | | 2560 | "policy mismathed. IPsec is expected"); |
2561 | | | 2561 | |
2562 | /* Get an entry to check whether sent message or not. */ | | 2562 | /* Get an entry to check whether sent message or not. */ |
2563 | newspacq = key_getspacq(&sp->spidx); | | 2563 | newspacq = key_getspacq(&sp->spidx); |
2564 | if (newspacq != NULL) { | | 2564 | if (newspacq != NULL) { |
2565 | if (key_blockacq_count < newspacq->count) { | | 2565 | if (key_blockacq_count < newspacq->count) { |
2566 | /* reset counter and do send message. */ | | 2566 | /* reset counter and do send message. */ |
2567 | newspacq->count = 0; | | 2567 | newspacq->count = 0; |
2568 | } else { | | 2568 | } else { |
2569 | /* increment counter and do nothing. */ | | 2569 | /* increment counter and do nothing. */ |
2570 | newspacq->count++; | | 2570 | newspacq->count++; |
2571 | return 0; | | 2571 | return 0; |
2572 | } | | 2572 | } |
2573 | } else { | | 2573 | } else { |
2574 | /* make new entry for blocking to send SADB_ACQUIRE. */ | | 2574 | /* make new entry for blocking to send SADB_ACQUIRE. */ |
2575 | newspacq = key_newspacq(&sp->spidx); | | 2575 | newspacq = key_newspacq(&sp->spidx); |
2576 | if (newspacq == NULL) | | 2576 | if (newspacq == NULL) |
2577 | return ENOBUFS; | | 2577 | return ENOBUFS; |
2578 | | | 2578 | |
2579 | /* add to key_misc.acqlist */ | | 2579 | /* add to key_misc.acqlist */ |
2580 | LIST_INSERT_HEAD(&key_misc.spacqlist, newspacq, chain); | | 2580 | LIST_INSERT_HEAD(&key_misc.spacqlist, newspacq, chain); |
2581 | } | | 2581 | } |
2582 | | | 2582 | |
2583 | /* create new sadb_msg to reply. */ | | 2583 | /* create new sadb_msg to reply. */ |
2584 | m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0); | | 2584 | m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0); |
2585 | if (!m) { | | 2585 | if (!m) { |
2586 | error = ENOBUFS; | | 2586 | error = ENOBUFS; |
2587 | goto fail; | | 2587 | goto fail; |
2588 | } | | 2588 | } |
2589 | result = m; | | 2589 | result = m; |
2590 | | | 2590 | |
2591 | result->m_pkthdr.len = 0; | | 2591 | result->m_pkthdr.len = 0; |
2592 | for (m = result; m; m = m->m_next) | | 2592 | for (m = result; m; m = m->m_next) |
2593 | result->m_pkthdr.len += m->m_len; | | 2593 | result->m_pkthdr.len += m->m_len; |
2594 | | | 2594 | |
2595 | mtod(result, struct sadb_msg *)->sadb_msg_len = | | 2595 | mtod(result, struct sadb_msg *)->sadb_msg_len = |
2596 | PFKEY_UNIT64(result->m_pkthdr.len); | | 2596 | PFKEY_UNIT64(result->m_pkthdr.len); |
2597 | | | 2597 | |
2598 | return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED); | | 2598 | return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED); |
2599 | | | 2599 | |
2600 | fail: | | 2600 | fail: |
2601 | if (result) | | 2601 | if (result) |
2602 | m_freem(result); | | 2602 | m_freem(result); |
2603 | return error; | | 2603 | return error; |
2604 | } | | 2604 | } |
2605 | #endif /* notyet */ | | 2605 | #endif /* notyet */ |
2606 | | | 2606 | |
2607 | /* | | 2607 | /* |
2608 | * SADB_SPDFLUSH processing | | 2608 | * SADB_SPDFLUSH processing |
2609 | * receive | | 2609 | * receive |
2610 | * <base> | | 2610 | * <base> |
2611 | * from the user, and free all entries in secpctree. | | 2611 | * from the user, and free all entries in secpctree. |
2612 | * and send, | | 2612 | * and send, |
2613 | * <base> | | 2613 | * <base> |
2614 | * to the user. | | 2614 | * to the user. |
2615 | * NOTE: what to do is only marking SADB_SASTATE_DEAD. | | 2615 | * NOTE: what to do is only marking SADB_SASTATE_DEAD. |
2616 | * | | 2616 | * |
2617 | * m will always be freed. | | 2617 | * m will always be freed. |
2618 | */ | | 2618 | */ |
2619 | static int | | 2619 | static int |
2620 | key_api_spdflush(struct socket *so, struct mbuf *m, | | 2620 | key_api_spdflush(struct socket *so, struct mbuf *m, |
2621 | const struct sadb_msghdr *mhp) | | 2621 | const struct sadb_msghdr *mhp) |
2622 | { | | 2622 | { |
2623 | struct sadb_msg *newmsg; | | 2623 | struct sadb_msg *newmsg; |
2624 | struct secpolicy *sp; | | 2624 | struct secpolicy *sp; |
2625 | u_int dir; | | 2625 | u_int dir; |
2626 | | | 2626 | |
2627 | if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg))) | | 2627 | if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg))) |
2628 | return key_senderror(so, m, EINVAL); | | 2628 | return key_senderror(so, m, EINVAL); |
2629 | | | 2629 | |
2630 | for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { | | 2630 | for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { |
2631 | retry: | | 2631 | retry: |
2632 | mutex_enter(&key_spd.lock); | | 2632 | mutex_enter(&key_spd.lock); |
2633 | SPLIST_WRITER_FOREACH(sp, dir) { | | 2633 | SPLIST_WRITER_FOREACH(sp, dir) { |
2634 | KASSERT(sp->state != IPSEC_SPSTATE_DEAD); | | 2634 | KASSERT(sp->state != IPSEC_SPSTATE_DEAD); |
2635 | key_unlink_sp(sp); | | 2635 | key_unlink_sp(sp); |
2636 | mutex_exit(&key_spd.lock); | | 2636 | mutex_exit(&key_spd.lock); |
2637 | key_destroy_sp(sp); | | 2637 | key_destroy_sp(sp); |
2638 | goto retry; | | 2638 | goto retry; |
2639 | } | | 2639 | } |
2640 | mutex_exit(&key_spd.lock); | | 2640 | mutex_exit(&key_spd.lock); |
2641 | } | | 2641 | } |
2642 | | | 2642 | |
2643 | /* We're deleting policy; no need to invalidate the ipflow cache. */ | | 2643 | /* We're deleting policy; no need to invalidate the ipflow cache. */ |
2644 | | | 2644 | |
2645 | if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) { | | 2645 | if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) { |
2646 | IPSECLOG(LOG_DEBUG, "No more memory.\n"); | | 2646 | IPSECLOG(LOG_DEBUG, "No more memory.\n"); |
2647 | return key_senderror(so, m, ENOBUFS); | | 2647 | return key_senderror(so, m, ENOBUFS); |
2648 | } | | 2648 | } |
2649 | | | 2649 | |
2650 | if (m->m_next) | | 2650 | if (m->m_next) |
2651 | m_freem(m->m_next); | | 2651 | m_freem(m->m_next); |
2652 | m->m_next = NULL; | | 2652 | m->m_next = NULL; |
2653 | m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); | | 2653 | m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); |
2654 | newmsg = mtod(m, struct sadb_msg *); | | 2654 | newmsg = mtod(m, struct sadb_msg *); |
2655 | newmsg->sadb_msg_errno = 0; | | 2655 | newmsg->sadb_msg_errno = 0; |
2656 | newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len); | | 2656 | newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len); |
2657 | | | 2657 | |
2658 | return key_sendup_mbuf(so, m, KEY_SENDUP_ALL); | | 2658 | return key_sendup_mbuf(so, m, KEY_SENDUP_ALL); |
2659 | } | | 2659 | } |
2660 | | | 2660 | |
2661 | static struct sockaddr key_src = { | | 2661 | static struct sockaddr key_src = { |
2662 | .sa_len = 2, | | 2662 | .sa_len = 2, |
2663 | .sa_family = PF_KEY, | | 2663 | .sa_family = PF_KEY, |
2664 | }; | | 2664 | }; |
2665 | | | 2665 | |
2666 | static struct mbuf * | | 2666 | static struct mbuf * |
2667 | key_setspddump_chain(int *errorp, int *lenp, pid_t pid) | | 2667 | key_setspddump_chain(int *errorp, int *lenp, pid_t pid) |
2668 | { | | 2668 | { |
2669 | struct secpolicy *sp; | | 2669 | struct secpolicy *sp; |
2670 | int cnt; | | 2670 | int cnt; |
2671 | u_int dir; | | 2671 | u_int dir; |
2672 | struct mbuf *m, *n, *prev; | | 2672 | struct mbuf *m, *n, *prev; |
2673 | int totlen; | | 2673 | int totlen; |
2674 | | | 2674 | |
2675 | KASSERT(mutex_owned(&key_spd.lock)); | | 2675 | KASSERT(mutex_owned(&key_spd.lock)); |
2676 | | | 2676 | |
2677 | *lenp = 0; | | 2677 | *lenp = 0; |
2678 | | | 2678 | |
2679 | /* search SPD entry and get buffer size. */ | | 2679 | /* search SPD entry and get buffer size. */ |
2680 | cnt = 0; | | 2680 | cnt = 0; |
2681 | for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { | | 2681 | for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { |
2682 | SPLIST_WRITER_FOREACH(sp, dir) { | | 2682 | SPLIST_WRITER_FOREACH(sp, dir) { |
2683 | cnt++; | | 2683 | cnt++; |
2684 | } | | 2684 | } |
2685 | } | | 2685 | } |
2686 | | | 2686 | |
2687 | if (cnt == 0) { | | 2687 | if (cnt == 0) { |
2688 | *errorp = ENOENT; | | 2688 | *errorp = ENOENT; |
2689 | return (NULL); | | 2689 | return (NULL); |
2690 | } | | 2690 | } |
2691 | | | 2691 | |
2692 | m = NULL; | | 2692 | m = NULL; |
2693 | prev = m; | | 2693 | prev = m; |
2694 | totlen = 0; | | 2694 | totlen = 0; |
2695 | for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { | | 2695 | for (dir = 0; dir < IPSEC_DIR_MAX; dir++) { |
2696 | SPLIST_WRITER_FOREACH(sp, dir) { | | 2696 | SPLIST_WRITER_FOREACH(sp, dir) { |
2697 | --cnt; | | 2697 | --cnt; |
2698 | n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt, pid); | | 2698 | n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt, pid); |
2699 | | | 2699 | |
2700 | totlen += n->m_pkthdr.len; | | 2700 | totlen += n->m_pkthdr.len; |
2701 | if (!m) { | | 2701 | if (!m) { |
2702 | m = n; | | 2702 | m = n; |
2703 | } else { | | 2703 | } else { |
2704 | prev->m_nextpkt = n; | | 2704 | prev->m_nextpkt = n; |
2705 | } | | 2705 | } |
2706 | prev = n; | | 2706 | prev = n; |
2707 | } | | 2707 | } |
2708 | } | | 2708 | } |
2709 | | | 2709 | |
2710 | *lenp = totlen; | | 2710 | *lenp = totlen; |
2711 | *errorp = 0; | | 2711 | *errorp = 0; |
2712 | return (m); | | 2712 | return (m); |
2713 | } | | 2713 | } |
2714 | | | 2714 | |
2715 | /* | | 2715 | /* |
2716 | * SADB_SPDDUMP processing | | 2716 | * SADB_SPDDUMP processing |
2717 | * receive | | 2717 | * receive |
2718 | * <base> | | 2718 | * <base> |
2719 | * from the user, and dump all SP leaves | | 2719 | * from the user, and dump all SP leaves |
2720 | * and send, | | 2720 | * and send, |
2721 | * <base> ..... | | 2721 | * <base> ..... |
2722 | * to the ikmpd. | | 2722 | * to the ikmpd. |
2723 | * | | 2723 | * |
2724 | * m will always be freed. | | 2724 | * m will always be freed. |
2725 | */ | | 2725 | */ |
2726 | static int | | 2726 | static int |
2727 | key_api_spddump(struct socket *so, struct mbuf *m0, | | 2727 | key_api_spddump(struct socket *so, struct mbuf *m0, |
2728 | const struct sadb_msghdr *mhp) | | 2728 | const struct sadb_msghdr *mhp) |
2729 | { | | 2729 | { |
2730 | struct mbuf *n; | | 2730 | struct mbuf *n; |
2731 | int error, len; | | 2731 | int error, len; |
2732 | int ok; | | 2732 | int ok; |
2733 | pid_t pid; | | 2733 | pid_t pid; |
2734 | | | 2734 | |
2735 | pid = mhp->msg->sadb_msg_pid; | | 2735 | pid = mhp->msg->sadb_msg_pid; |
2736 | /* | | 2736 | /* |
2737 | * If the requestor has insufficient socket-buffer space | | 2737 | * If the requestor has insufficient socket-buffer space |
2738 | * for the entire chain, nobody gets any response to the DUMP. | | 2738 | * for the entire chain, nobody gets any response to the DUMP. |
2739 | * XXX For now, only the requestor ever gets anything. | | 2739 | * XXX For now, only the requestor ever gets anything. |
2740 | * Moreover, if the requestor has any space at all, they receive | | 2740 | * Moreover, if the requestor has any space at all, they receive |
2741 | * the entire chain, otherwise the request is refused with ENOBUFS. | | 2741 | * the entire chain, otherwise the request is refused with ENOBUFS. |
2742 | */ | | 2742 | */ |
2743 | if (sbspace(&so->so_rcv) <= 0) { | | 2743 | if (sbspace(&so->so_rcv) <= 0) { |
2744 | return key_senderror(so, m0, ENOBUFS); | | 2744 | return key_senderror(so, m0, ENOBUFS); |
2745 | } | | 2745 | } |
2746 | | | 2746 | |
2747 | mutex_enter(&key_spd.lock); | | 2747 | mutex_enter(&key_spd.lock); |
2748 | n = key_setspddump_chain(&error, &len, pid); | | 2748 | n = key_setspddump_chain(&error, &len, pid); |
2749 | mutex_exit(&key_spd.lock); | | 2749 | mutex_exit(&key_spd.lock); |
2750 | | | 2750 | |
2751 | if (n == NULL) { | | 2751 | if (n == NULL) { |
2752 | return key_senderror(so, m0, ENOENT); | | 2752 | return key_senderror(so, m0, ENOENT); |
2753 | } | | 2753 | } |
2754 | { | | 2754 | { |
2755 | uint64_t *ps = PFKEY_STAT_GETREF(); | | 2755 | uint64_t *ps = PFKEY_STAT_GETREF(); |
2756 | ps[PFKEY_STAT_IN_TOTAL]++; | | 2756 | ps[PFKEY_STAT_IN_TOTAL]++; |
2757 | ps[PFKEY_STAT_IN_BYTES] += len; | | 2757 | ps[PFKEY_STAT_IN_BYTES] += len; |
2758 | PFKEY_STAT_PUTREF(); | | 2758 | PFKEY_STAT_PUTREF(); |
2759 | } | | 2759 | } |
2760 | | | 2760 | |
2761 | /* | | 2761 | /* |
2762 | * PF_KEY DUMP responses are no longer broadcast to all PF_KEY sockets. | | 2762 | * PF_KEY DUMP responses are no longer broadcast to all PF_KEY sockets. |
2763 | * The requestor receives either the entire chain, or an | | 2763 | * The requestor receives either the entire chain, or an |
2764 | * error message with ENOBUFS. | | 2764 | * error message with ENOBUFS. |
2765 | */ | | 2765 | */ |
2766 | | | 2766 | |
2767 | /* | | 2767 | /* |
2768 | * sbappendchainwith record takes the chain of entries, one | | 2768 | * sbappendchainwith record takes the chain of entries, one |
2769 | * packet-record per SPD entry, prepends the key_src sockaddr | | 2769 | * packet-record per SPD entry, prepends the key_src sockaddr |
2770 | * to each packet-record, links the sockaddr mbufs into a new | | 2770 | * to each packet-record, links the sockaddr mbufs into a new |
2771 | * list of records, then appends the entire resulting | | 2771 | * list of records, then appends the entire resulting |
2772 | * list to the requesting socket. | | 2772 | * list to the requesting socket. |
2773 | */ | | 2773 | */ |
2774 | ok = sbappendaddrchain(&so->so_rcv, (struct sockaddr *)&key_src, n, | | 2774 | ok = sbappendaddrchain(&so->so_rcv, (struct sockaddr *)&key_src, n, |
2775 | SB_PRIO_ONESHOT_OVERFLOW); | | 2775 | SB_PRIO_ONESHOT_OVERFLOW); |
2776 | | | 2776 | |
2777 | if (!ok) { | | 2777 | if (!ok) { |
2778 | PFKEY_STATINC(PFKEY_STAT_IN_NOMEM); | | 2778 | PFKEY_STATINC(PFKEY_STAT_IN_NOMEM); |
2779 | m_freem(n); | | 2779 | m_freem(n); |
2780 | return key_senderror(so, m0, ENOBUFS); | | 2780 | return key_senderror(so, m0, ENOBUFS); |
2781 | } | | 2781 | } |
2782 | | | 2782 | |
2783 | m_freem(m0); | | 2783 | m_freem(m0); |
2784 | return error; | | 2784 | return error; |
2785 | } | | 2785 | } |
2786 | | | 2786 | |
2787 | /* | | 2787 | /* |
2788 | * SADB_X_NAT_T_NEW_MAPPING. Unused by racoon as of 2005/04/23 | | 2788 | * SADB_X_NAT_T_NEW_MAPPING. Unused by racoon as of 2005/04/23 |
2789 | */ | | 2789 | */ |
2790 | static int | | 2790 | static int |
2791 | key_api_nat_map(struct socket *so, struct mbuf *m, | | 2791 | key_api_nat_map(struct socket *so, struct mbuf *m, |
2792 | const struct sadb_msghdr *mhp) | | 2792 | const struct sadb_msghdr *mhp) |
2793 | { | | 2793 | { |
2794 | struct sadb_x_nat_t_type *type; | | 2794 | struct sadb_x_nat_t_type *type; |
2795 | struct sadb_x_nat_t_port *sport; | | 2795 | struct sadb_x_nat_t_port *sport; |
2796 | struct sadb_x_nat_t_port *dport; | | 2796 | struct sadb_x_nat_t_port *dport; |
2797 | struct sadb_address *iaddr, *raddr; | | 2797 | struct sadb_address *iaddr, *raddr; |
2798 | struct sadb_x_nat_t_frag *frag; | | 2798 | struct sadb_x_nat_t_frag *frag; |
2799 | | | 2799 | |
2800 | if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] == NULL || | | 2800 | if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] == NULL || |
2801 | mhp->ext[SADB_X_EXT_NAT_T_SPORT] == NULL || | | 2801 | mhp->ext[SADB_X_EXT_NAT_T_SPORT] == NULL || |
2802 | mhp->ext[SADB_X_EXT_NAT_T_DPORT] == NULL) { | | 2802 | mhp->ext[SADB_X_EXT_NAT_T_DPORT] == NULL) { |
2803 | IPSECLOG(LOG_DEBUG, "invalid message.\n"); | | 2803 | IPSECLOG(LOG_DEBUG, "invalid message.\n"); |
2804 | return key_senderror(so, m, EINVAL); | | 2804 | return key_senderror(so, m, EINVAL); |
2805 | } | | 2805 | } |
2806 | if ((mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type)) || | | 2806 | if ((mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type)) || |
2807 | (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport)) || | | 2807 | (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport)) || |
2808 | (mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport))) { | | 2808 | (mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport))) { |
2809 | IPSECLOG(LOG_DEBUG, "invalid message.\n"); | | 2809 | IPSECLOG(LOG_DEBUG, "invalid message.\n"); |
2810 | return key_senderror(so, m, EINVAL); | | 2810 | return key_senderror(so, m, EINVAL); |
2811 | } | | 2811 | } |
2812 | | | 2812 | |
2813 | if ((mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL) && | | 2813 | if ((mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL) && |
2814 | (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr))) { | | 2814 | (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr))) { |
2815 | IPSECLOG(LOG_DEBUG, "invalid message\n"); | | 2815 | IPSECLOG(LOG_DEBUG, "invalid message\n"); |
2816 | return key_senderror(so, m, EINVAL); | | 2816 | return key_senderror(so, m, EINVAL); |
2817 | } | | 2817 | } |
2818 | | | 2818 | |
2819 | if ((mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) && | | 2819 | if ((mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) && |
2820 | (mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr))) { | | 2820 | (mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr))) { |
2821 | IPSECLOG(LOG_DEBUG, "invalid message\n"); | | 2821 | IPSECLOG(LOG_DEBUG, "invalid message\n"); |
2822 | return key_senderror(so, m, EINVAL); | | 2822 | return key_senderror(so, m, EINVAL); |
2823 | } | | 2823 | } |
2824 | | | 2824 | |
2825 | if ((mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) && | | 2825 | if ((mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) && |
2826 | (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag))) { | | 2826 | (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag))) { |
2827 | IPSECLOG(LOG_DEBUG, "invalid message\n"); | | 2827 | IPSECLOG(LOG_DEBUG, "invalid message\n"); |
2828 | return key_senderror(so, m, EINVAL); | | 2828 | return key_senderror(so, m, EINVAL); |
2829 | } | | 2829 | } |
2830 | | | 2830 | |
2831 | type = mhp->ext[SADB_X_EXT_NAT_T_TYPE]; | | 2831 | type = mhp->ext[SADB_X_EXT_NAT_T_TYPE]; |
2832 | sport = mhp->ext[SADB_X_EXT_NAT_T_SPORT]; | | 2832 | sport = mhp->ext[SADB_X_EXT_NAT_T_SPORT]; |
2833 | dport = mhp->ext[SADB_X_EXT_NAT_T_DPORT]; | | 2833 | dport = mhp->ext[SADB_X_EXT_NAT_T_DPORT]; |
2834 | iaddr = mhp->ext[SADB_X_EXT_NAT_T_OAI]; | | 2834 | iaddr = mhp->ext[SADB_X_EXT_NAT_T_OAI]; |
2835 | raddr = mhp->ext[SADB_X_EXT_NAT_T_OAR]; | | 2835 | raddr = mhp->ext[SADB_X_EXT_NAT_T_OAR]; |
2836 | frag = mhp->ext[SADB_X_EXT_NAT_T_FRAG]; | | 2836 | frag = mhp->ext[SADB_X_EXT_NAT_T_FRAG]; |
2837 | | | 2837 | |
2838 | /* | | 2838 | /* |
2839 | * XXX handle that, it should also contain a SA, or anything | | 2839 | * XXX handle that, it should also contain a SA, or anything |
2840 | * that enable to update the SA information. | | 2840 | * that enable to update the SA information. |
2841 | */ | | 2841 | */ |
2842 | | | 2842 | |
2843 | return 0; | | 2843 | return 0; |
2844 | } | | 2844 | } |
2845 | | | 2845 | |
2846 | /* | | 2846 | /* |
2847 | * Never return NULL. | | 2847 | * Never return NULL. |
2848 | */ | | 2848 | */ |
2849 | static struct mbuf * | | 2849 | static struct mbuf * |
2850 | key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq, pid_t pid) | | 2850 | key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq, pid_t pid) |
2851 | { | | 2851 | { |
2852 | struct mbuf *result = NULL, *m; | | 2852 | struct mbuf *result = NULL, *m; |
2853 | | | 2853 | |
2854 | KASSERT(!cpu_softintr_p()); | | 2854 | KASSERT(!cpu_softintr_p()); |
2855 | | | 2855 | |
2856 | m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, | | 2856 | m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, |
2857 | key_sp_refcnt(sp), M_WAITOK); | | 2857 | key_sp_refcnt(sp), M_WAITOK); |
2858 | result = m; | | 2858 | result = m; |
2859 | | | 2859 | |
2860 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, | | 2860 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, |
2861 | &sp->spidx.src.sa, sp->spidx.prefs, sp->spidx.ul_proto, M_WAITOK); | | 2861 | &sp->spidx.src.sa, sp->spidx.prefs, sp->spidx.ul_proto, M_WAITOK); |
2862 | m_cat(result, m); | | 2862 | m_cat(result, m); |
2863 | | | 2863 | |
2864 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, | | 2864 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, |
2865 | &sp->spidx.dst.sa, sp->spidx.prefd, sp->spidx.ul_proto, M_WAITOK); | | 2865 | &sp->spidx.dst.sa, sp->spidx.prefd, sp->spidx.ul_proto, M_WAITOK); |
2866 | m_cat(result, m); | | 2866 | m_cat(result, m); |
2867 | | | 2867 | |
2868 | m = key_sp2msg(sp, M_WAITOK); | | 2868 | m = key_sp2msg(sp, M_WAITOK); |
2869 | m_cat(result, m); | | 2869 | m_cat(result, m); |
2870 | | | 2870 | |
2871 | KASSERT(result->m_flags & M_PKTHDR); | | 2871 | KASSERT(result->m_flags & M_PKTHDR); |
2872 | KASSERT(result->m_len >= sizeof(struct sadb_msg)); | | 2872 | KASSERT(result->m_len >= sizeof(struct sadb_msg)); |
2873 | | | 2873 | |
2874 | result->m_pkthdr.len = 0; | | 2874 | result->m_pkthdr.len = 0; |
2875 | for (m = result; m; m = m->m_next) | | 2875 | for (m = result; m; m = m->m_next) |
2876 | result->m_pkthdr.len += m->m_len; | | 2876 | result->m_pkthdr.len += m->m_len; |
2877 | | | 2877 | |
2878 | mtod(result, struct sadb_msg *)->sadb_msg_len = | | 2878 | mtod(result, struct sadb_msg *)->sadb_msg_len = |
2879 | PFKEY_UNIT64(result->m_pkthdr.len); | | 2879 | PFKEY_UNIT64(result->m_pkthdr.len); |
2880 | | | 2880 | |
2881 | return result; | | 2881 | return result; |
2882 | } | | 2882 | } |
2883 | | | 2883 | |
2884 | /* | | 2884 | /* |
2885 | * get PFKEY message length for security policy and request. | | 2885 | * get PFKEY message length for security policy and request. |
2886 | */ | | 2886 | */ |
2887 | static u_int | | 2887 | static u_int |
2888 | key_getspreqmsglen(const struct secpolicy *sp) | | 2888 | key_getspreqmsglen(const struct secpolicy *sp) |
2889 | { | | 2889 | { |
2890 | u_int tlen; | | 2890 | u_int tlen; |
2891 | | | 2891 | |
2892 | tlen = sizeof(struct sadb_x_policy); | | 2892 | tlen = sizeof(struct sadb_x_policy); |
2893 | | | 2893 | |
2894 | /* if is the policy for ipsec ? */ | | 2894 | /* if is the policy for ipsec ? */ |
2895 | if (sp->policy != IPSEC_POLICY_IPSEC) | | 2895 | if (sp->policy != IPSEC_POLICY_IPSEC) |
2896 | return tlen; | | 2896 | return tlen; |
2897 | | | 2897 | |
2898 | /* get length of ipsec requests */ | | 2898 | /* get length of ipsec requests */ |
2899 | { | | 2899 | { |
2900 | const struct ipsecrequest *isr; | | 2900 | const struct ipsecrequest *isr; |
2901 | int len; | | 2901 | int len; |
2902 | | | 2902 | |
2903 | for (isr = sp->req; isr != NULL; isr = isr->next) { | | 2903 | for (isr = sp->req; isr != NULL; isr = isr->next) { |
2904 | len = sizeof(struct sadb_x_ipsecrequest) | | 2904 | len = sizeof(struct sadb_x_ipsecrequest) |
2905 | + isr->saidx.src.sa.sa_len + isr->saidx.dst.sa.sa_len; | | 2905 | + isr->saidx.src.sa.sa_len + isr->saidx.dst.sa.sa_len; |
2906 | | | 2906 | |
2907 | tlen += PFKEY_ALIGN8(len); | | 2907 | tlen += PFKEY_ALIGN8(len); |
2908 | } | | 2908 | } |
2909 | } | | 2909 | } |
2910 | | | 2910 | |
2911 | return tlen; | | 2911 | return tlen; |
2912 | } | | 2912 | } |
2913 | | | 2913 | |
2914 | /* | | 2914 | /* |
2915 | * SADB_SPDEXPIRE processing | | 2915 | * SADB_SPDEXPIRE processing |
2916 | * send | | 2916 | * send |
2917 | * <base, address(SD), lifetime(CH), policy> | | 2917 | * <base, address(SD), lifetime(CH), policy> |
2918 | * to KMD by PF_KEY. | | 2918 | * to KMD by PF_KEY. |
2919 | * | | 2919 | * |
2920 | * OUT: 0 : succeed | | 2920 | * OUT: 0 : succeed |
2921 | * others : error number | | 2921 | * others : error number |
2922 | */ | | 2922 | */ |
2923 | static int | | 2923 | static int |
2924 | key_spdexpire(struct secpolicy *sp) | | 2924 | key_spdexpire(struct secpolicy *sp) |
2925 | { | | 2925 | { |
2926 | int s; | | 2926 | int s; |
2927 | struct mbuf *result = NULL, *m; | | 2927 | struct mbuf *result = NULL, *m; |
2928 | int len; | | 2928 | int len; |
2929 | int error = -1; | | 2929 | int error = -1; |
2930 | struct sadb_lifetime *lt; | | 2930 | struct sadb_lifetime *lt; |
2931 | | | 2931 | |
2932 | /* XXX: Why do we lock ? */ | | 2932 | /* XXX: Why do we lock ? */ |
2933 | s = splsoftnet(); /*called from softclock()*/ | | 2933 | s = splsoftnet(); /*called from softclock()*/ |
2934 | | | 2934 | |
2935 | KASSERT(sp != NULL); | | 2935 | KASSERT(sp != NULL); |
2936 | | | 2936 | |
2937 | /* set msg header */ | | 2937 | /* set msg header */ |
2938 | m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0, M_WAITOK); | | 2938 | m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0, M_WAITOK); |
2939 | result = m; | | 2939 | result = m; |
2940 | | | 2940 | |
2941 | /* create lifetime extension (current and hard) */ | | 2941 | /* create lifetime extension (current and hard) */ |
2942 | len = PFKEY_ALIGN8(sizeof(*lt)) * 2; | | 2942 | len = PFKEY_ALIGN8(sizeof(*lt)) * 2; |
2943 | m = key_alloc_mbuf(len, M_WAITOK); | | 2943 | m = key_alloc_mbuf(len, M_WAITOK); |
2944 | KASSERT(m->m_next == NULL); | | 2944 | KASSERT(m->m_next == NULL); |
2945 | | | 2945 | |
2946 | memset(mtod(m, void *), 0, len); | | 2946 | memset(mtod(m, void *), 0, len); |
2947 | lt = mtod(m, struct sadb_lifetime *); | | 2947 | lt = mtod(m, struct sadb_lifetime *); |
2948 | lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); | | 2948 | lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); |
2949 | lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; | | 2949 | lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; |
2950 | lt->sadb_lifetime_allocations = 0; | | 2950 | lt->sadb_lifetime_allocations = 0; |
2951 | lt->sadb_lifetime_bytes = 0; | | 2951 | lt->sadb_lifetime_bytes = 0; |
2952 | lt->sadb_lifetime_addtime = time_mono_to_wall(sp->created); | | 2952 | lt->sadb_lifetime_addtime = time_mono_to_wall(sp->created); |
2953 | lt->sadb_lifetime_usetime = time_mono_to_wall(sp->lastused); | | 2953 | lt->sadb_lifetime_usetime = time_mono_to_wall(sp->lastused); |
2954 | lt = (struct sadb_lifetime *)(mtod(m, char *) + len / 2); | | 2954 | lt = (struct sadb_lifetime *)(mtod(m, char *) + len / 2); |
2955 | lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); | | 2955 | lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime)); |
2956 | lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD; | | 2956 | lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD; |
2957 | lt->sadb_lifetime_allocations = 0; | | 2957 | lt->sadb_lifetime_allocations = 0; |
2958 | lt->sadb_lifetime_bytes = 0; | | 2958 | lt->sadb_lifetime_bytes = 0; |
2959 | lt->sadb_lifetime_addtime = sp->lifetime; | | 2959 | lt->sadb_lifetime_addtime = sp->lifetime; |
2960 | lt->sadb_lifetime_usetime = sp->validtime; | | 2960 | lt->sadb_lifetime_usetime = sp->validtime; |
2961 | m_cat(result, m); | | 2961 | m_cat(result, m); |
2962 | | | 2962 | |
2963 | /* set sadb_address for source */ | | 2963 | /* set sadb_address for source */ |
2964 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &sp->spidx.src.sa, | | 2964 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &sp->spidx.src.sa, |
2965 | sp->spidx.prefs, sp->spidx.ul_proto, M_WAITOK); | | 2965 | sp->spidx.prefs, sp->spidx.ul_proto, M_WAITOK); |
2966 | m_cat(result, m); | | 2966 | m_cat(result, m); |
2967 | | | 2967 | |
2968 | /* set sadb_address for destination */ | | 2968 | /* set sadb_address for destination */ |
2969 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &sp->spidx.dst.sa, | | 2969 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &sp->spidx.dst.sa, |
2970 | sp->spidx.prefd, sp->spidx.ul_proto, M_WAITOK); | | 2970 | sp->spidx.prefd, sp->spidx.ul_proto, M_WAITOK); |
2971 | m_cat(result, m); | | 2971 | m_cat(result, m); |
2972 | | | 2972 | |
2973 | /* set secpolicy */ | | 2973 | /* set secpolicy */ |
2974 | m = key_sp2msg(sp, M_WAITOK); | | 2974 | m = key_sp2msg(sp, M_WAITOK); |
2975 | m_cat(result, m); | | 2975 | m_cat(result, m); |
2976 | | | 2976 | |
2977 | KASSERT(result->m_flags & M_PKTHDR); | | 2977 | KASSERT(result->m_flags & M_PKTHDR); |
2978 | KASSERT(result->m_len >= sizeof(struct sadb_msg)); | | 2978 | KASSERT(result->m_len >= sizeof(struct sadb_msg)); |
2979 | | | 2979 | |
2980 | result->m_pkthdr.len = 0; | | 2980 | result->m_pkthdr.len = 0; |
2981 | for (m = result; m; m = m->m_next) | | 2981 | for (m = result; m; m = m->m_next) |
2982 | result->m_pkthdr.len += m->m_len; | | 2982 | result->m_pkthdr.len += m->m_len; |
2983 | | | 2983 | |
2984 | mtod(result, struct sadb_msg *)->sadb_msg_len = | | 2984 | mtod(result, struct sadb_msg *)->sadb_msg_len = |
2985 | PFKEY_UNIT64(result->m_pkthdr.len); | | 2985 | PFKEY_UNIT64(result->m_pkthdr.len); |
2986 | | | 2986 | |
2987 | error = key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED); | | 2987 | error = key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED); |
2988 | splx(s); | | 2988 | splx(s); |
2989 | return error; | | 2989 | return error; |
2990 | } | | 2990 | } |
2991 | | | 2991 | |
2992 | /* %%% SAD management */ | | 2992 | /* %%% SAD management */ |
2993 | /* | | 2993 | /* |
2994 | * allocating a memory for new SA head, and copy from the values of mhp. | | 2994 | * allocating a memory for new SA head, and copy from the values of mhp. |
2995 | * OUT: NULL : failure due to the lack of memory. | | 2995 | * OUT: NULL : failure due to the lack of memory. |
2996 | * others : pointer to new SA head. | | 2996 | * others : pointer to new SA head. |
2997 | */ | | 2997 | */ |
2998 | static struct secashead * | | 2998 | static struct secashead * |
2999 | key_newsah(const struct secasindex *saidx) | | 2999 | key_newsah(const struct secasindex *saidx) |
3000 | { | | 3000 | { |
3001 | struct secashead *newsah; | | 3001 | struct secashead *newsah; |
3002 | int i; | | 3002 | int i; |
3003 | | | 3003 | |
3004 | KASSERT(saidx != NULL); | | 3004 | KASSERT(saidx != NULL); |
3005 | | | 3005 | |
3006 | newsah = kmem_zalloc(sizeof(struct secashead), KM_SLEEP); | | 3006 | newsah = kmem_zalloc(sizeof(struct secashead), KM_SLEEP); |
3007 | for (i = 0; i < __arraycount(newsah->savlist); i++) | | 3007 | for (i = 0; i < __arraycount(newsah->savlist); i++) |
3008 | PSLIST_INIT(&newsah->savlist[i]); | | 3008 | PSLIST_INIT(&newsah->savlist[i]); |
3009 | newsah->saidx = *saidx; | | 3009 | newsah->saidx = *saidx; |
3010 | | | 3010 | |
3011 | localcount_init(&newsah->localcount); | | 3011 | localcount_init(&newsah->localcount); |
3012 | /* Take a reference for the caller */ | | 3012 | /* Take a reference for the caller */ |
3013 | localcount_acquire(&newsah->localcount); | | 3013 | localcount_acquire(&newsah->localcount); |
3014 | | | 3014 | |
3015 | /* Add to the sah list */ | | 3015 | /* Add to the sah list */ |
3016 | SAHLIST_ENTRY_INIT(newsah); | | 3016 | SAHLIST_ENTRY_INIT(newsah); |
3017 | newsah->state = SADB_SASTATE_MATURE; | | 3017 | newsah->state = SADB_SASTATE_MATURE; |
3018 | mutex_enter(&key_sad.lock); | | 3018 | mutex_enter(&key_sad.lock); |
3019 | SAHLIST_WRITER_INSERT_HEAD(newsah); | | 3019 | SAHLIST_WRITER_INSERT_HEAD(newsah); |
3020 | mutex_exit(&key_sad.lock); | | 3020 | mutex_exit(&key_sad.lock); |
3021 | | | 3021 | |
3022 | return newsah; | | 3022 | return newsah; |
3023 | } | | 3023 | } |
3024 | | | 3024 | |
3025 | static bool | | 3025 | static bool |
3026 | key_sah_has_sav(struct secashead *sah) | | 3026 | key_sah_has_sav(struct secashead *sah) |
3027 | { | | 3027 | { |
3028 | u_int state; | | 3028 | u_int state; |
3029 | | | 3029 | |
3030 | KASSERT(mutex_owned(&key_sad.lock)); | | 3030 | KASSERT(mutex_owned(&key_sad.lock)); |
3031 | | | 3031 | |
3032 | SASTATE_ANY_FOREACH(state) { | | 3032 | SASTATE_ANY_FOREACH(state) { |
3033 | if (!SAVLIST_WRITER_EMPTY(sah, state)) | | 3033 | if (!SAVLIST_WRITER_EMPTY(sah, state)) |
3034 | return true; | | 3034 | return true; |
3035 | } | | 3035 | } |
3036 | | | 3036 | |
3037 | return false; | | 3037 | return false; |
3038 | } | | 3038 | } |
3039 | | | 3039 | |
3040 | static void | | 3040 | static void |
3041 | key_unlink_sah(struct secashead *sah) | | 3041 | key_unlink_sah(struct secashead *sah) |
3042 | { | | 3042 | { |
3043 | | | 3043 | |
3044 | KASSERT(!cpu_softintr_p()); | | 3044 | KASSERT(!cpu_softintr_p()); |
3045 | KASSERT(mutex_owned(&key_sad.lock)); | | 3045 | KASSERT(mutex_owned(&key_sad.lock)); |
3046 | KASSERT(sah->state == SADB_SASTATE_DEAD); | | 3046 | KASSERT(sah->state == SADB_SASTATE_DEAD); |
3047 | | | 3047 | |
3048 | /* Remove from the sah list */ | | 3048 | /* Remove from the sah list */ |
3049 | SAHLIST_WRITER_REMOVE(sah); | | 3049 | SAHLIST_WRITER_REMOVE(sah); |
3050 | | | 3050 | |
3051 | #ifdef NET_MPSAFE | | 3051 | #ifdef NET_MPSAFE |
3052 | KASSERT(mutex_ownable(softnet_lock)); | | 3052 | KDASSERT(mutex_ownable(softnet_lock)); |
3053 | key_sad_pserialize_perform(); | | 3053 | key_sad_pserialize_perform(); |
3054 | #endif | | 3054 | #endif |
3055 | | | 3055 | |
3056 | localcount_drain(&sah->localcount, &key_sad.cv_lc, &key_sad.lock); | | 3056 | localcount_drain(&sah->localcount, &key_sad.cv_lc, &key_sad.lock); |
3057 | } | | 3057 | } |
3058 | | | 3058 | |
3059 | static void | | 3059 | static void |
3060 | key_destroy_sah(struct secashead *sah) | | 3060 | key_destroy_sah(struct secashead *sah) |
3061 | { | | 3061 | { |
3062 | | | 3062 | |
3063 | rtcache_free(&sah->sa_route); | | 3063 | rtcache_free(&sah->sa_route); |
3064 | | | 3064 | |
3065 | SAHLIST_ENTRY_DESTROY(sah); | | 3065 | SAHLIST_ENTRY_DESTROY(sah); |
3066 | localcount_fini(&sah->localcount); | | 3066 | localcount_fini(&sah->localcount); |
3067 | | | 3067 | |
3068 | if (sah->idents != NULL) | | 3068 | if (sah->idents != NULL) |
3069 | kmem_free(sah->idents, sah->idents_len); | | 3069 | kmem_free(sah->idents, sah->idents_len); |
3070 | if (sah->identd != NULL) | | 3070 | if (sah->identd != NULL) |
3071 | kmem_free(sah->identd, sah->identd_len); | | 3071 | kmem_free(sah->identd, sah->identd_len); |
3072 | | | 3072 | |
3073 | kmem_free(sah, sizeof(*sah)); | | 3073 | kmem_free(sah, sizeof(*sah)); |
3074 | } | | 3074 | } |
3075 | | | 3075 | |
3076 | /* | | 3076 | /* |
3077 | * allocating a new SA with LARVAL state. | | 3077 | * allocating a new SA with LARVAL state. |
3078 | * key_api_add() and key_api_getspi() call, | | 3078 | * key_api_add() and key_api_getspi() call, |
3079 | * and copy the values of mhp into new buffer. | | 3079 | * and copy the values of mhp into new buffer. |
3080 | * When SAD message type is GETSPI: | | 3080 | * When SAD message type is GETSPI: |
3081 | * to set sequence number from acq_seq++, | | 3081 | * to set sequence number from acq_seq++, |
3082 | * to set zero to SPI. | | 3082 | * to set zero to SPI. |
3083 | * not to call key_setsava(). | | 3083 | * not to call key_setsava(). |
3084 | * OUT: NULL : fail | | 3084 | * OUT: NULL : fail |
3085 | * others : pointer to new secasvar. | | 3085 | * others : pointer to new secasvar. |
3086 | * | | 3086 | * |
3087 | * does not modify mbuf. does not free mbuf on error. | | 3087 | * does not modify mbuf. does not free mbuf on error. |
3088 | */ | | 3088 | */ |
3089 | static struct secasvar * | | 3089 | static struct secasvar * |
3090 | key_newsav(struct mbuf *m, const struct sadb_msghdr *mhp, | | 3090 | key_newsav(struct mbuf *m, const struct sadb_msghdr *mhp, |
3091 | int *errp, const char* where, int tag) | | 3091 | int *errp, const char* where, int tag) |
3092 | { | | 3092 | { |
3093 | struct secasvar *newsav; | | 3093 | struct secasvar *newsav; |
3094 | const struct sadb_sa *xsa; | | 3094 | const struct sadb_sa *xsa; |
3095 | | | 3095 | |
3096 | KASSERT(!cpu_softintr_p()); | | 3096 | KASSERT(!cpu_softintr_p()); |
3097 | KASSERT(m != NULL); | | 3097 | KASSERT(m != NULL); |
3098 | KASSERT(mhp != NULL); | | 3098 | KASSERT(mhp != NULL); |
3099 | KASSERT(mhp->msg != NULL); | | 3099 | KASSERT(mhp->msg != NULL); |
3100 | | | 3100 | |
3101 | newsav = kmem_zalloc(sizeof(struct secasvar), KM_SLEEP); | | 3101 | newsav = kmem_zalloc(sizeof(struct secasvar), KM_SLEEP); |
3102 | | | 3102 | |
3103 | switch (mhp->msg->sadb_msg_type) { | | 3103 | switch (mhp->msg->sadb_msg_type) { |
3104 | case SADB_GETSPI: | | 3104 | case SADB_GETSPI: |
3105 | newsav->spi = 0; | | 3105 | newsav->spi = 0; |
3106 | | | 3106 | |
3107 | #ifdef IPSEC_DOSEQCHECK | | 3107 | #ifdef IPSEC_DOSEQCHECK |
3108 | /* sync sequence number */ | | 3108 | /* sync sequence number */ |
3109 | if (mhp->msg->sadb_msg_seq == 0) | | 3109 | if (mhp->msg->sadb_msg_seq == 0) |
3110 | newsav->seq = | | 3110 | newsav->seq = |
3111 | (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq)); | | 3111 | (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq)); |
3112 | else | | 3112 | else |
3113 | #endif | | 3113 | #endif |
3114 | newsav->seq = mhp->msg->sadb_msg_seq; | | 3114 | newsav->seq = mhp->msg->sadb_msg_seq; |
3115 | break; | | 3115 | break; |
3116 | | | 3116 | |
3117 | case SADB_ADD: | | 3117 | case SADB_ADD: |
3118 | /* sanity check */ | | 3118 | /* sanity check */ |
3119 | if (mhp->ext[SADB_EXT_SA] == NULL) { | | 3119 | if (mhp->ext[SADB_EXT_SA] == NULL) { |
3120 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); | | 3120 | IPSECLOG(LOG_DEBUG, "invalid message is passed.\n"); |
3121 | *errp = EINVAL; | | 3121 | *errp = EINVAL; |
3122 | goto error; | | 3122 | goto error; |
3123 | } | | 3123 | } |
3124 | xsa = mhp->ext[SADB_EXT_SA]; | | 3124 | xsa = mhp->ext[SADB_EXT_SA]; |
3125 | newsav->spi = xsa->sadb_sa_spi; | | 3125 | newsav->spi = xsa->sadb_sa_spi; |
3126 | newsav->seq = mhp->msg->sadb_msg_seq; | | 3126 | newsav->seq = mhp->msg->sadb_msg_seq; |
3127 | break; | | 3127 | break; |
3128 | default: | | 3128 | default: |
3129 | *errp = EINVAL; | | 3129 | *errp = EINVAL; |
3130 | goto error; | | 3130 | goto error; |
3131 | } | | 3131 | } |
3132 | | | 3132 | |
3133 | /* copy sav values */ | | 3133 | /* copy sav values */ |
3134 | if (mhp->msg->sadb_msg_type != SADB_GETSPI) { | | 3134 | if (mhp->msg->sadb_msg_type != SADB_GETSPI) { |
3135 | *errp = key_setsaval(newsav, m, mhp); | | 3135 | *errp = key_setsaval(newsav, m, mhp); |
3136 | if (*errp) | | 3136 | if (*errp) |
3137 | goto error; | | 3137 | goto error; |
3138 | } else { | | 3138 | } else { |
3139 | /* We don't allow lft_c to be NULL */ | | 3139 | /* We don't allow lft_c to be NULL */ |
3140 | newsav->lft_c = kmem_zalloc(sizeof(struct sadb_lifetime), | | 3140 | newsav->lft_c = kmem_zalloc(sizeof(struct sadb_lifetime), |
3141 | KM_SLEEP); | | 3141 | KM_SLEEP); |
3142 | } | | 3142 | } |
3143 | | | 3143 | |
3144 | /* reset created */ | | 3144 | /* reset created */ |
3145 | newsav->created = time_uptime; | | 3145 | newsav->created = time_uptime; |
3146 | newsav->pid = mhp->msg->sadb_msg_pid; | | 3146 | newsav->pid = mhp->msg->sadb_msg_pid; |
3147 | | | 3147 | |
3148 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, | | 3148 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, |
3149 | "DP from %s:%u return SA:%p\n", where, tag, newsav); | | 3149 | "DP from %s:%u return SA:%p\n", where, tag, newsav); |
3150 | return newsav; | | 3150 | return newsav; |
3151 | | | 3151 | |
3152 | error: | | 3152 | error: |
3153 | KASSERT(*errp != 0); | | 3153 | KASSERT(*errp != 0); |
3154 | kmem_free(newsav, sizeof(*newsav)); | | 3154 | kmem_free(newsav, sizeof(*newsav)); |
3155 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, | | 3155 | KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, |
3156 | "DP from %s:%u return SA:NULL\n", where, tag); | | 3156 | "DP from %s:%u return SA:NULL\n", where, tag); |
3157 | return NULL; | | 3157 | return NULL; |
3158 | } | | 3158 | } |
3159 | | | 3159 | |
3160 | | | 3160 | |
3161 | static void | | 3161 | static void |
3162 | key_clear_xform(struct secasvar *sav) | | 3162 | key_clear_xform(struct secasvar *sav) |
3163 | { | | 3163 | { |
3164 | | | 3164 | |
3165 | /* | | 3165 | /* |
3166 | * Cleanup xform state. Note that zeroize'ing causes the | | 3166 | * Cleanup xform state. Note that zeroize'ing causes the |
3167 | * keys to be cleared; otherwise we must do it ourself. | | 3167 | * keys to be cleared; otherwise we must do it ourself. |
3168 | */ | | 3168 | */ |
3169 | if (sav->tdb_xform != NULL) { | | 3169 | if (sav->tdb_xform != NULL) { |
3170 | sav->tdb_xform->xf_zeroize(sav); | | 3170 | sav->tdb_xform->xf_zeroize(sav); |
3171 | sav->tdb_xform = NULL; | | 3171 | sav->tdb_xform = NULL; |
3172 | } else { | | 3172 | } else { |
3173 | if (sav->key_auth != NULL) | | 3173 | if (sav->key_auth != NULL) |
3174 | explicit_memset(_KEYBUF(sav->key_auth), 0, | | 3174 | explicit_memset(_KEYBUF(sav->key_auth), 0, |
3175 | _KEYLEN(sav->key_auth)); | | 3175 | _KEYLEN(sav->key_auth)); |
3176 | if (sav->key_enc != NULL) | | 3176 | if (sav->key_enc != NULL) |
3177 | explicit_memset(_KEYBUF(sav->key_enc), 0, | | 3177 | explicit_memset(_KEYBUF(sav->key_enc), 0, |
3178 | _KEYLEN(sav->key_enc)); | | 3178 | _KEYLEN(sav->key_enc)); |
3179 | } | | 3179 | } |
3180 | } | | 3180 | } |
3181 | | | 3181 | |
3182 | /* | | 3182 | /* |
3183 | * free() SA variable entry. | | 3183 | * free() SA variable entry. |
3184 | */ | | 3184 | */ |
3185 | static void | | 3185 | static void |
3186 | key_delsav(struct secasvar *sav) | | 3186 | key_delsav(struct secasvar *sav) |
3187 | { | | 3187 | { |
3188 | | | 3188 | |
3189 | key_clear_xform(sav); | | 3189 | key_clear_xform(sav); |
3190 | key_freesaval(sav); | | 3190 | key_freesaval(sav); |
3191 | kmem_free(sav, sizeof(*sav)); | | 3191 | kmem_free(sav, sizeof(*sav)); |
3192 | } | | 3192 | } |
3193 | | | 3193 | |
3194 | /* | | 3194 | /* |
3195 | * Must be called in a pserialize read section. A held sah | | 3195 | * Must be called in a pserialize read section. A held sah |
3196 | * must be released by key_sah_unref after use. | | 3196 | * must be released by key_sah_unref after use. |
3197 | */ | | 3197 | */ |
3198 | static void | | 3198 | static void |
3199 | key_sah_ref(struct secashead *sah) | | 3199 | key_sah_ref(struct secashead *sah) |
3200 | { | | 3200 | { |
3201 | | | 3201 | |
3202 | localcount_acquire(&sah->localcount); | | 3202 | localcount_acquire(&sah->localcount); |
3203 | } | | 3203 | } |
3204 | | | 3204 | |
3205 | /* | | 3205 | /* |
3206 | * Must be called without holding key_sad.lock because the lock | | 3206 | * Must be called without holding key_sad.lock because the lock |
3207 | * would be held in localcount_release. | | 3207 | * would be held in localcount_release. |
3208 | */ | | 3208 | */ |
3209 | static void | | 3209 | static void |
3210 | key_sah_unref(struct secashead *sah) | | 3210 | key_sah_unref(struct secashead *sah) |
3211 | { | | 3211 | { |
3212 | | | 3212 | |
3213 | KDASSERT(mutex_ownable(&key_sad.lock)); | | 3213 | KDASSERT(mutex_ownable(&key_sad.lock)); |
3214 | | | 3214 | |
3215 | localcount_release(&sah->localcount, &key_sad.cv_lc, &key_sad.lock); | | 3215 | localcount_release(&sah->localcount, &key_sad.cv_lc, &key_sad.lock); |
3216 | } | | 3216 | } |
3217 | | | 3217 | |
3218 | /* | | 3218 | /* |
3219 | * Search SAD and return sah. Must be called in a pserialize | | 3219 | * Search SAD and return sah. Must be called in a pserialize |
3220 | * read section. | | 3220 | * read section. |
3221 | * OUT: | | 3221 | * OUT: |
3222 | * NULL : not found | | 3222 | * NULL : not found |
3223 | * others : found, pointer to a SA. | | 3223 | * others : found, pointer to a SA. |
3224 | */ | | 3224 | */ |
3225 | static struct secashead * | | 3225 | static struct secashead * |
3226 | key_getsah(const struct secasindex *saidx, int flag) | | 3226 | key_getsah(const struct secasindex *saidx, int flag) |
3227 | { | | 3227 | { |
3228 | struct secashead *sah; | | 3228 | struct secashead *sah; |
3229 | | | 3229 | |
3230 | SAHLIST_READER_FOREACH(sah) { | | 3230 | SAHLIST_READER_FOREACH(sah) { |
3231 | if (sah->state == SADB_SASTATE_DEAD) | | 3231 | if (sah->state == SADB_SASTATE_DEAD) |
3232 | continue; | | 3232 | continue; |
3233 | if (key_saidx_match(&sah->saidx, saidx, flag)) | | 3233 | if (key_saidx_match(&sah->saidx, saidx, flag)) |
3234 | return sah; | | 3234 | return sah; |
3235 | } | | 3235 | } |
3236 | | | 3236 | |
3237 | return NULL; | | 3237 | return NULL; |
3238 | } | | 3238 | } |
3239 | | | 3239 | |
3240 | /* | | 3240 | /* |
3241 | * Search SAD and return sah. If sah is returned, the caller must call | | 3241 | * Search SAD and return sah. If sah is returned, the caller must call |
3242 | * key_sah_unref to releaset a reference. | | 3242 | * key_sah_unref to releaset a reference. |
3243 | * OUT: | | 3243 | * OUT: |
3244 | * NULL : not found | | 3244 | * NULL : not found |
3245 | * others : found, pointer to a SA. | | 3245 | * others : found, pointer to a SA. |
3246 | */ | | 3246 | */ |
3247 | static struct secashead * | | 3247 | static struct secashead * |
3248 | key_getsah_ref(const struct secasindex *saidx, int flag) | | 3248 | key_getsah_ref(const struct secasindex *saidx, int flag) |
3249 | { | | 3249 | { |
3250 | struct secashead *sah; | | 3250 | struct secashead *sah; |
3251 | int s; | | 3251 | int s; |
3252 | | | 3252 | |
3253 | s = pserialize_read_enter(); | | 3253 | s = pserialize_read_enter(); |
3254 | sah = key_getsah(saidx, flag); | | 3254 | sah = key_getsah(saidx, flag); |
3255 | if (sah != NULL) | | 3255 | if (sah != NULL) |
3256 | key_sah_ref(sah); | | 3256 | key_sah_ref(sah); |
3257 | pserialize_read_exit(s); | | 3257 | pserialize_read_exit(s); |
3258 | | | 3258 | |
3259 | return sah; | | 3259 | return sah; |
3260 | } | | 3260 | } |
3261 | | | 3261 | |
3262 | /* | | 3262 | /* |
3263 | * check not to be duplicated SPI. | | 3263 | * check not to be duplicated SPI. |
3264 | * NOTE: this function is too slow due to searching all SAD. | | 3264 | * NOTE: this function is too slow due to searching all SAD. |
3265 | * OUT: | | 3265 | * OUT: |
3266 | * NULL : not found | | 3266 | * NULL : not found |
3267 | * others : found, pointer to a SA. | | 3267 | * others : found, pointer to a SA. |
3268 | */ | | 3268 | */ |
3269 | static bool | | 3269 | static bool |
3270 | key_checkspidup(const struct secasindex *saidx, u_int32_t spi) | | 3270 | key_checkspidup(const struct secasindex *saidx, u_int32_t spi) |
3271 | { | | 3271 | { |
3272 | struct secashead *sah; | | 3272 | struct secashead *sah; |
3273 | struct secasvar *sav; | | 3273 | struct secasvar *sav; |
3274 | int s; | | 3274 | int s; |
3275 | | | 3275 | |
3276 | /* check address family */ | | 3276 | /* check address family */ |
3277 | if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) { | | 3277 | if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) { |
3278 | IPSECLOG(LOG_DEBUG, "address family mismatched.\n"); | | 3278 | IPSECLOG(LOG_DEBUG, "address family mismatched.\n"); |
3279 | return false; | | 3279 | return false; |
3280 | } | | 3280 | } |
3281 | | | 3281 | |
3282 | /* check all SAD */ | | 3282 | /* check all SAD */ |
3283 | s = pserialize_read_enter(); | | 3283 | s = pserialize_read_enter(); |
3284 | SAHLIST_READER_FOREACH(sah) { | | 3284 | SAHLIST_READER_FOREACH(sah) { |
3285 | if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst)) | | 3285 | if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst)) |
3286 | continue; | | 3286 | continue; |
3287 | sav = key_getsavbyspi(sah, spi); | | 3287 | sav = key_getsavbyspi(sah, spi); |
3288 | if (sav != NULL) { | | 3288 | if (sav != NULL) { |
3289 | pserialize_read_exit(s); | | 3289 | pserialize_read_exit(s); |
3290 | KEY_SA_UNREF(&sav); | | 3290 | KEY_SA_UNREF(&sav); |
3291 | return true; | | 3291 | return true; |
3292 | } | | 3292 | } |
3293 | } | | 3293 | } |
3294 | pserialize_read_exit(s); | | 3294 | pserialize_read_exit(s); |
3295 | | | 3295 | |
3296 | return false; | | 3296 | return false; |
3297 | } | | 3297 | } |
3298 | | | 3298 | |
3299 | /* | | 3299 | /* |
3300 | * search SAD litmited alive SA, protocol, SPI. | | 3300 | * search SAD litmited alive SA, protocol, SPI. |
3301 | * OUT: | | 3301 | * OUT: |
3302 | * NULL : not found | | 3302 | * NULL : not found |
3303 | * others : found, pointer to a SA. | | 3303 | * others : found, pointer to a SA. |
3304 | */ | | 3304 | */ |
3305 | static struct secasvar * | | 3305 | static struct secasvar * |
3306 | key_getsavbyspi(struct secashead *sah, u_int32_t spi) | | 3306 | key_getsavbyspi(struct secashead *sah, u_int32_t spi) |
3307 | { | | 3307 | { |
3308 | struct secasvar *sav = NULL; | | 3308 | struct secasvar *sav = NULL; |
3309 | u_int state; | | 3309 | u_int state; |
3310 | int s; | | 3310 | int s; |
3311 | | | 3311 | |
3312 | /* search all status */ | | 3312 | /* search all status */ |
3313 | s = pserialize_read_enter(); | | 3313 | s = pserialize_read_enter(); |
3314 | SASTATE_ALIVE_FOREACH(state) { | | 3314 | SASTATE_ALIVE_FOREACH(state) { |
3315 | SAVLIST_READER_FOREACH(sav, sah, state) { | | 3315 | SAVLIST_READER_FOREACH(sav, sah, state) { |
3316 | /* sanity check */ | | 3316 | /* sanity check */ |
3317 | if (sav->state != state) { | | 3317 | if (sav->state != state) { |
3318 | IPSECLOG(LOG_DEBUG, | | 3318 | IPSECLOG(LOG_DEBUG, |
3319 | "invalid sav->state (queue: %d SA: %d)\n", | | 3319 | "invalid sav->state (queue: %d SA: %d)\n", |
3320 | state, sav->state); | | 3320 | state, sav->state); |
3321 | continue; | | 3321 | continue; |
3322 | } | | 3322 | } |
3323 | | | 3323 | |
3324 | if (sav->spi == spi) { | | 3324 | if (sav->spi == spi) { |
3325 | KEY_SA_REF(sav); | | 3325 | KEY_SA_REF(sav); |
3326 | goto out; | | 3326 | goto out; |
3327 | } | | 3327 | } |
3328 | } | | 3328 | } |
3329 | } | | 3329 | } |
3330 | out: | | 3330 | out: |
3331 | pserialize_read_exit(s); | | 3331 | pserialize_read_exit(s); |
3332 | | | 3332 | |
3333 | return sav; | | 3333 | return sav; |
3334 | } | | 3334 | } |
3335 | | | 3335 | |
3336 | /* | | 3336 | /* |
3337 | * Free allocated data to member variables of sav: | | 3337 | * Free allocated data to member variables of sav: |
3338 | * sav->replay, sav->key_* and sav->lft_*. | | 3338 | * sav->replay, sav->key_* and sav->lft_*. |
3339 | */ | | 3339 | */ |
3340 | static void | | 3340 | static void |
3341 | key_freesaval(struct secasvar *sav) | | 3341 | key_freesaval(struct secasvar *sav) |
3342 | { | | 3342 | { |
3343 | | | 3343 | |
3344 | KASSERT(key_sa_refcnt(sav) == 0); | | 3344 | KASSERT(key_sa_refcnt(sav) == 0); |
3345 | | | 3345 | |
3346 | if (sav->replay != NULL) | | 3346 | if (sav->replay != NULL) |
3347 | kmem_intr_free(sav->replay, sav->replay_len); | | 3347 | kmem_intr_free(sav->replay, sav->replay_len); |
3348 | if (sav->key_auth != NULL) | | 3348 | if (sav->key_auth != NULL) |
3349 | kmem_intr_free(sav->key_auth, sav->key_auth_len); | | 3349 | kmem_intr_free(sav->key_auth, sav->key_auth_len); |
3350 | if (sav->key_enc != NULL) | | 3350 | if (sav->key_enc != NULL) |
3351 | kmem_intr_free(sav->key_enc, sav->key_enc_len); | | 3351 | kmem_intr_free(sav->key_enc, sav->key_enc_len); |
3352 | if (sav->lft_c != NULL) | | 3352 | if (sav->lft_c != NULL) |
3353 | kmem_intr_free(sav->lft_c, sizeof(*(sav->lft_c))); | | 3353 | kmem_intr_free(sav->lft_c, sizeof(*(sav->lft_c))); |
3354 | if (sav->lft_h != NULL) | | 3354 | if (sav->lft_h != NULL) |
3355 | kmem_intr_free(sav->lft_h, sizeof(*(sav->lft_h))); | | 3355 | kmem_intr_free(sav->lft_h, sizeof(*(sav->lft_h))); |
3356 | if (sav->lft_s != NULL) | | 3356 | if (sav->lft_s != NULL) |
3357 | kmem_intr_free(sav->lft_s, sizeof(*(sav->lft_s))); | | 3357 | kmem_intr_free(sav->lft_s, sizeof(*(sav->lft_s))); |
3358 | } | | 3358 | } |
3359 | | | 3359 | |
3360 | /* | | 3360 | /* |
3361 | * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*. | | 3361 | * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*. |
3362 | * You must update these if need. | | 3362 | * You must update these if need. |
3363 | * OUT: 0: success. | | 3363 | * OUT: 0: success. |
3364 | * !0: failure. | | 3364 | * !0: failure. |
3365 | * | | 3365 | * |
3366 | * does not modify mbuf. does not free mbuf on error. | | 3366 | * does not modify mbuf. does not free mbuf on error. |
3367 | */ | | 3367 | */ |
3368 | static int | | 3368 | static int |
3369 | key_setsaval(struct secasvar *sav, struct mbuf *m, | | 3369 | key_setsaval(struct secasvar *sav, struct mbuf *m, |
3370 | const struct sadb_msghdr *mhp) | | 3370 | const struct sadb_msghdr *mhp) |
3371 | { | | 3371 | { |
3372 | int error = 0; | | 3372 | int error = 0; |
3373 | | | 3373 | |
3374 | KASSERT(!cpu_softintr_p()); | | 3374 | KASSERT(!cpu_softintr_p()); |
3375 | KASSERT(m != NULL); | | 3375 | KASSERT(m != NULL); |
3376 | KASSERT(mhp != NULL); | | 3376 | KASSERT(mhp != NULL); |
3377 | KASSERT(mhp->msg != NULL); | | 3377 | KASSERT(mhp->msg != NULL); |
3378 | | | 3378 | |
3379 | /* We shouldn't initialize sav variables while someone uses it. */ | | 3379 | /* We shouldn't initialize sav variables while someone uses it. */ |
3380 | KASSERT(key_sa_refcnt(sav) == 0); | | 3380 | KASSERT(key_sa_refcnt(sav) == 0); |
3381 | | | 3381 | |
3382 | /* SA */ | | 3382 | /* SA */ |
3383 | if (mhp->ext[SADB_EXT_SA] != NULL) { | | 3383 | if (mhp->ext[SADB_EXT_SA] != NULL) { |
3384 | const struct sadb_sa *sa0; | | 3384 | const struct sadb_sa *sa0; |
3385 | | | 3385 | |
3386 | sa0 = mhp->ext[SADB_EXT_SA]; | | 3386 | sa0 = mhp->ext[SADB_EXT_SA]; |
3387 | if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) { | | 3387 | if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) { |
3388 | error = EINVAL; | | 3388 | error = EINVAL; |
3389 | goto fail; | | 3389 | goto fail; |
3390 | } | | 3390 | } |
3391 | | | 3391 | |
3392 | sav->alg_auth = sa0->sadb_sa_auth; | | 3392 | sav->alg_auth = sa0->sadb_sa_auth; |
3393 | sav->alg_enc = sa0->sadb_sa_encrypt; | | 3393 | sav->alg_enc = sa0->sadb_sa_encrypt; |
3394 | sav->flags = sa0->sadb_sa_flags; | | 3394 | sav->flags = sa0->sadb_sa_flags; |
3395 | | | 3395 | |
3396 | /* replay window */ | | 3396 | /* replay window */ |
3397 | if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) { | | 3397 | if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) { |
3398 | size_t len = sizeof(struct secreplay) + | | 3398 | size_t len = sizeof(struct secreplay) + |
3399 | sa0->sadb_sa_replay; | | 3399 | sa0->sadb_sa_replay; |
3400 | sav->replay = kmem_zalloc(len, KM_SLEEP); | | 3400 | sav->replay = kmem_zalloc(len, KM_SLEEP); |
3401 | sav->replay_len = len; | | 3401 | sav->replay_len = len; |
3402 | if (sa0->sadb_sa_replay != 0) | | 3402 | if (sa0->sadb_sa_replay != 0) |
3403 | sav->replay->bitmap = (char*)(sav->replay+1); | | 3403 | sav->replay->bitmap = (char*)(sav->replay+1); |
3404 | sav->replay->wsize = sa0->sadb_sa_replay; | | 3404 | sav->replay->wsize = sa0->sadb_sa_replay; |
3405 | } | | 3405 | } |
3406 | } | | 3406 | } |
3407 | | | 3407 | |
3408 | /* Authentication keys */ | | 3408 | /* Authentication keys */ |
3409 | if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) { | | 3409 | if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) { |
3410 | const struct sadb_key *key0; | | 3410 | const struct sadb_key *key0; |
3411 | int len; | | 3411 | int len; |
3412 | | | 3412 | |
3413 | key0 = mhp->ext[SADB_EXT_KEY_AUTH]; | | 3413 | key0 = mhp->ext[SADB_EXT_KEY_AUTH]; |
3414 | len = mhp->extlen[SADB_EXT_KEY_AUTH]; | | 3414 | len = mhp->extlen[SADB_EXT_KEY_AUTH]; |
3415 | | | 3415 | |
3416 | error = 0; | | 3416 | error = 0; |
3417 | if (len < sizeof(*key0)) { | | 3417 | if (len < sizeof(*key0)) { |
3418 | error = EINVAL; | | 3418 | error = EINVAL; |
3419 | goto fail; | | 3419 | goto fail; |
3420 | } | | 3420 | } |
3421 | switch (mhp->msg->sadb_msg_satype) { | | 3421 | switch (mhp->msg->sadb_msg_satype) { |
3422 | case SADB_SATYPE_AH: | | 3422 | case SADB_SATYPE_AH: |
3423 | case SADB_SATYPE_ESP: | | 3423 | case SADB_SATYPE_ESP: |
3424 | case SADB_X_SATYPE_TCPSIGNATURE: | | 3424 | case SADB_X_SATYPE_TCPSIGNATURE: |
3425 | if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) && | | 3425 | if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) && |
3426 | sav->alg_auth != SADB_X_AALG_NULL) | | 3426 | sav->alg_auth != SADB_X_AALG_NULL) |
3427 | error = EINVAL; | | 3427 | error = EINVAL; |
3428 | break; | | 3428 | break; |
3429 | case SADB_X_SATYPE_IPCOMP: | | 3429 | case SADB_X_SATYPE_IPCOMP: |
3430 | default: | | 3430 | default: |
3431 | error = EINVAL; | | 3431 | error = EINVAL; |
3432 | break; | | 3432 | break; |
3433 | } | | 3433 | } |
3434 | if (error) { | | 3434 | if (error) { |
3435 | IPSECLOG(LOG_DEBUG, "invalid key_auth values.\n"); | | 3435 | IPSECLOG(LOG_DEBUG, "invalid key_auth values.\n"); |
3436 | goto fail; | | 3436 | goto fail; |
3437 | } | | 3437 | } |
3438 | | | 3438 | |
3439 | sav->key_auth = key_newbuf(key0, len); | | 3439 | sav->key_auth = key_newbuf(key0, len); |
3440 | sav->key_auth_len = len; | | 3440 | sav->key_auth_len = len; |
3441 | } | | 3441 | } |
3442 | | | 3442 | |
3443 | /* Encryption key */ | | 3443 | /* Encryption key */ |
3444 | if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) { | | 3444 | if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) { |
3445 | const struct sadb_key *key0; | | 3445 | const struct sadb_key *key0; |
3446 | int len; | | 3446 | int len; |
3447 | | | 3447 | |
3448 | key0 = mhp->ext[SADB_EXT_KEY_ENCRYPT]; | | 3448 | key0 = mhp->ext[SADB_EXT_KEY_ENCRYPT]; |
3449 | len = mhp->extlen[SADB_EXT_KEY_ENCRYPT]; | | 3449 | len = mhp->extlen[SADB_EXT_KEY_ENCRYPT]; |
3450 | | | 3450 | |
3451 | error = 0; | | 3451 | error = 0; |
3452 | if (len < sizeof(*key0)) { | | 3452 | if (len < sizeof(*key0)) { |
3453 | error = EINVAL; | | 3453 | error = EINVAL; |
3454 | goto fail; | | 3454 | goto fail; |
3455 | } | | 3455 | } |
3456 | switch (mhp->msg->sadb_msg_satype) { | | 3456 | switch (mhp->msg->sadb_msg_satype) { |
3457 | case SADB_SATYPE_ESP: | | 3457 | case SADB_SATYPE_ESP: |
3458 | if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) && | | 3458 | if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) && |
3459 | sav->alg_enc != SADB_EALG_NULL) { | | 3459 | sav->alg_enc != SADB_EALG_NULL) { |
3460 | error = EINVAL; | | 3460 | error = EINVAL; |
3461 | break; | | 3461 | break; |
3462 | } | | 3462 | } |
3463 | sav->key_enc = key_newbuf(key0, len); | | 3463 | sav->key_enc = key_newbuf(key0, len); |
3464 | sav->key_enc_len = len; | | 3464 | sav->key_enc_len = len; |
3465 | break; | | 3465 | break; |
3466 | case SADB_X_SATYPE_IPCOMP: | | 3466 | case SADB_X_SATYPE_IPCOMP: |
3467 | if (len != PFKEY_ALIGN8(sizeof(struct sadb_key))) | | 3467 | if (len != PFKEY_ALIGN8(sizeof(struct sadb_key))) |
3468 | error = EINVAL; | | 3468 | error = EINVAL; |
3469 | sav->key_enc = NULL; /*just in case*/ | | 3469 | sav->key_enc = NULL; /*just in case*/ |
3470 | break; | | 3470 | break; |
3471 | case SADB_SATYPE_AH: | | 3471 | case SADB_SATYPE_AH: |
3472 | case SADB_X_SATYPE_TCPSIGNATURE: | | 3472 | case SADB_X_SATYPE_TCPSIGNATURE: |
3473 | default: | | 3473 | default: |
3474 | error = EINVAL; | | 3474 | error = EINVAL; |
3475 | break; | | 3475 | break; |
3476 | } | | 3476 | } |
3477 | if (error) { | | 3477 | if (error) { |
3478 | IPSECLOG(LOG_DEBUG, "invalid key_enc value.\n"); | | 3478 | IPSECLOG(LOG_DEBUG, "invalid key_enc value.\n"); |
3479 | goto fail; | | 3479 | goto fail; |
3480 | } | | 3480 | } |
3481 | } | | 3481 | } |
3482 | | | 3482 | |
3483 | /* set iv */ | | 3483 | /* set iv */ |
3484 | sav->ivlen = 0; | | 3484 | sav->ivlen = 0; |
3485 | | | 3485 | |
3486 | switch (mhp->msg->sadb_msg_satype) { | | 3486 | switch (mhp->msg->sadb_msg_satype) { |
3487 | case SADB_SATYPE_AH: | | 3487 | case SADB_SATYPE_AH: |
3488 | error = xform_init(sav, XF_AH); | | 3488 | error = xform_init(sav, XF_AH); |
3489 | break; | | 3489 | break; |
3490 | case SADB_SATYPE_ESP: | | 3490 | case SADB_SATYPE_ESP: |
3491 | error = xform_init(sav, XF_ESP); | | 3491 | error = xform_init(sav, XF_ESP); |
3492 | break; | | 3492 | break; |
3493 | case SADB_X_SATYPE_IPCOMP: | | 3493 | case SADB_X_SATYPE_IPCOMP: |
3494 | error = xform_init(sav, XF_IPCOMP); | | 3494 | error = xform_init(sav, XF_IPCOMP); |
3495 | break; | | 3495 | break; |
3496 | case SADB_X_SATYPE_TCPSIGNATURE: | | 3496 | case SADB_X_SATYPE_TCPSIGNATURE: |
3497 | error = xform_init(sav, XF_TCPSIGNATURE); | | 3497 | error = xform_init(sav, XF_TCPSIGNATURE); |
3498 | break; | | 3498 | break; |
3499 | } | | 3499 | } |
3500 | if (error) { | | 3500 | if (error) { |
3501 | IPSECLOG(LOG_DEBUG, "unable to initialize SA type %u.\n", | | 3501 | IPSECLOG(LOG_DEBUG, "unable to initialize SA type %u.\n", |
3502 | mhp->msg->sadb_msg_satype); | | 3502 | mhp->msg->sadb_msg_satype); |
3503 | goto fail; | | 3503 | goto fail; |
3504 | } | | 3504 | } |
3505 | | | 3505 | |
3506 | /* reset created */ | | 3506 | /* reset created */ |
3507 | sav->created = time_uptime; | | 3507 | sav->created = time_uptime; |
3508 | | | 3508 | |
3509 | /* make lifetime for CURRENT */ | | 3509 | /* make lifetime for CURRENT */ |
3510 | sav->lft_c = kmem_alloc(sizeof(struct sadb_lifetime), KM_SLEEP); | | 3510 | sav->lft_c = kmem_alloc(sizeof(struct sadb_lifetime), KM_SLEEP); |
3511 | | | 3511 | |
3512 | sav->lft_c->sadb_lifetime_len = | | 3512 | sav->lft_c->sadb_lifetime_len = |
3513 | PFKEY_UNIT64(sizeof(struct sadb_lifetime)); | | 3513 | PFKEY_UNIT64(sizeof(struct sadb_lifetime)); |
3514 | sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; | | 3514 | sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; |
3515 | sav->lft_c->sadb_lifetime_allocations = 0; | | 3515 | sav->lft_c->sadb_lifetime_allocations = 0; |
3516 | sav->lft_c->sadb_lifetime_bytes = 0; | | 3516 | sav->lft_c->sadb_lifetime_bytes = 0; |
3517 | sav->lft_c->sadb_lifetime_addtime = time_uptime; | | 3517 | sav->lft_c->sadb_lifetime_addtime = time_uptime; |
3518 | sav->lft_c->sadb_lifetime_usetime = 0; | | 3518 | sav->lft_c->sadb_lifetime_usetime = 0; |
3519 | | | 3519 | |
3520 | /* lifetimes for HARD and SOFT */ | | 3520 | /* lifetimes for HARD and SOFT */ |
3521 | { | | 3521 | { |
3522 | const struct sadb_lifetime *lft0; | | 3522 | const struct sadb_lifetime *lft0; |
3523 | | | 3523 | |
3524 | lft0 = mhp->ext[SADB_EXT_LIFETIME_HARD]; | | 3524 | lft0 = mhp->ext[SADB_EXT_LIFETIME_HARD]; |
3525 | if (lft0 != NULL) { | | 3525 | if (lft0 != NULL) { |
3526 | if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) { | | 3526 | if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) { |
3527 | error = EINVAL; | | 3527 | error = EINVAL; |
3528 | goto fail; | | 3528 | goto fail; |
3529 | } | | 3529 | } |
3530 | sav->lft_h = key_newbuf(lft0, sizeof(*lft0)); | | 3530 | sav->lft_h = key_newbuf(lft0, sizeof(*lft0)); |
3531 | } | | 3531 | } |
3532 | | | 3532 | |
3533 | lft0 = mhp->ext[SADB_EXT_LIFETIME_SOFT]; | | 3533 | lft0 = mhp->ext[SADB_EXT_LIFETIME_SOFT]; |
3534 | if (lft0 != NULL) { | | 3534 | if (lft0 != NULL) { |
3535 | if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) { | | 3535 | if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) { |
3536 | error = EINVAL; | | 3536 | error = EINVAL; |
3537 | goto fail; | | 3537 | goto fail; |
3538 | } | | 3538 | } |
3539 | sav->lft_s = key_newbuf(lft0, sizeof(*lft0)); | | 3539 | sav->lft_s = key_newbuf(lft0, sizeof(*lft0)); |
3540 | /* to be initialize ? */ | | 3540 | /* to be initialize ? */ |
3541 | } | | 3541 | } |
3542 | } | | 3542 | } |
3543 | | | 3543 | |
3544 | return 0; | | 3544 | return 0; |
3545 | | | 3545 | |
3546 | fail: | | 3546 | fail: |
3547 | key_clear_xform(sav); | | 3547 | key_clear_xform(sav); |
3548 | key_freesaval(sav); | | 3548 | key_freesaval(sav); |
3549 | | | 3549 | |
3550 | return error; | | 3550 | return error; |
3551 | } | | 3551 | } |
3552 | | | 3552 | |
3553 | /* | | 3553 | /* |
3554 | * validation with a secasvar entry, and set SADB_SATYPE_MATURE. | | 3554 | * validation with a secasvar entry, and set SADB_SATYPE_MATURE. |
3555 | * OUT: 0: valid | | 3555 | * OUT: 0: valid |
3556 | * other: errno | | 3556 | * other: errno |
3557 | */ | | 3557 | */ |
3558 | static int | | 3558 | static int |
3559 | key_init_xform(struct secasvar *sav) | | 3559 | key_init_xform(struct secasvar *sav) |
3560 | { | | 3560 | { |
3561 | int error; | | 3561 | int error; |
3562 | | | 3562 | |
3563 | /* We shouldn't initialize sav variables while someone uses it. */ | | 3563 | /* We shouldn't initialize sav variables while someone uses it. */ |
3564 | KASSERT(key_sa_refcnt(sav) == 0); | | 3564 | KASSERT(key_sa_refcnt(sav) == 0); |
3565 | | | 3565 | |
3566 | /* check SPI value */ | | 3566 | /* check SPI value */ |
3567 | switch (sav->sah->saidx.proto) { | | 3567 | switch (sav->sah->saidx.proto) { |
3568 | case IPPROTO_ESP: | | 3568 | case IPPROTO_ESP: |
3569 | case IPPROTO_AH: | | 3569 | case IPPROTO_AH: |
3570 | if (ntohl(sav->spi) <= 255) { | | 3570 | if (ntohl(sav->spi) <= 255) { |
3571 | IPSECLOG(LOG_DEBUG, "illegal range of SPI %u.\n", | | 3571 | IPSECLOG(LOG_DEBUG, "illegal range of SPI %u.\n", |
3572 | (u_int32_t)ntohl(sav->spi)); | | 3572 | (u_int32_t)ntohl(sav->spi)); |
3573 | return EINVAL; | | 3573 | return EINVAL; |
3574 | } | | 3574 | } |
3575 | break; | | 3575 | break; |
3576 | } | | 3576 | } |
3577 | | | 3577 | |
3578 | /* check satype */ | | 3578 | /* check satype */ |
3579 | switch (sav->sah->saidx.proto) { | | 3579 | switch (sav->sah->saidx.proto) { |
3580 | case IPPROTO_ESP: | | 3580 | case IPPROTO_ESP: |
3581 | /* check flags */ | | 3581 | /* check flags */ |
3582 | if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) == | | 3582 | if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) == |
3583 | (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) { | | 3583 | (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) { |
3584 | IPSECLOG(LOG_DEBUG, | | 3584 | IPSECLOG(LOG_DEBUG, |
3585 | "invalid flag (derived) given to old-esp.\n"); | | 3585 | "invalid flag (derived) given to old-esp.\n"); |
3586 | return EINVAL; | | 3586 | return EINVAL; |
3587 | } | | 3587 | } |
3588 | error = xform_init(sav, XF_ESP); | | 3588 | error = xform_init(sav, XF_ESP); |
3589 | break; | | 3589 | break; |
3590 | case IPPROTO_AH: | | 3590 | case IPPROTO_AH: |
3591 | /* check flags */ | | 3591 | /* check flags */ |
3592 | if (sav->flags & SADB_X_EXT_DERIV) { | | 3592 | if (sav->flags & SADB_X_EXT_DERIV) { |
3593 | IPSECLOG(LOG_DEBUG, | | 3593 | IPSECLOG(LOG_DEBUG, |
3594 | "invalid flag (derived) given to AH SA.\n"); | | 3594 | "invalid flag (derived) given to AH SA.\n"); |
3595 | return EINVAL; | | 3595 | return EINVAL; |
3596 | } | | 3596 | } |
3597 | if (sav->alg_enc != SADB_EALG_NONE) { | | 3597 | if (sav->alg_enc != SADB_EALG_NONE) { |
3598 | IPSECLOG(LOG_DEBUG, | | 3598 | IPSECLOG(LOG_DEBUG, |
3599 | "protocol and algorithm mismated.\n"); | | 3599 | "protocol and algorithm mismated.\n"); |
3600 | return(EINVAL); | | 3600 | return(EINVAL); |
3601 | } | | 3601 | } |
3602 | error = xform_init(sav, XF_AH); | | 3602 | error = xform_init(sav, XF_AH); |
3603 | break; | | 3603 | break; |
3604 | case IPPROTO_IPCOMP: | | 3604 | case IPPROTO_IPCOMP: |
3605 | if (sav->alg_auth != SADB_AALG_NONE) { | | 3605 | if (sav->alg_auth != SADB_AALG_NONE) { |
3606 | IPSECLOG(LOG_DEBUG, | | 3606 | IPSECLOG(LOG_DEBUG, |
3607 | "protocol and algorithm mismated.\n"); | | 3607 | "protocol and algorithm mismated.\n"); |
3608 | return(EINVAL); | | 3608 | return(EINVAL); |
3609 | } | | 3609 | } |
3610 | if ((sav->flags & SADB_X_EXT_RAWCPI) == 0 | | 3610 | if ((sav->flags & SADB_X_EXT_RAWCPI) == 0 |
3611 | && ntohl(sav->spi) >= 0x10000) { | | 3611 | && ntohl(sav->spi) >= 0x10000) { |
3612 | IPSECLOG(LOG_DEBUG, "invalid cpi for IPComp.\n"); | | 3612 | IPSECLOG(LOG_DEBUG, "invalid cpi for IPComp.\n"); |
3613 | return(EINVAL); | | 3613 | return(EINVAL); |
3614 | } | | 3614 | } |
3615 | error = xform_init(sav, XF_IPCOMP); | | 3615 | error = xform_init(sav, XF_IPCOMP); |
3616 | break; | | 3616 | break; |
3617 | case IPPROTO_TCP: | | 3617 | case IPPROTO_TCP: |
3618 | if (sav->alg_enc != SADB_EALG_NONE) { | | 3618 | if (sav->alg_enc != SADB_EALG_NONE) { |
3619 | IPSECLOG(LOG_DEBUG, | | 3619 | IPSECLOG(LOG_DEBUG, |
3620 | "protocol and algorithm mismated.\n"); | | 3620 | "protocol and algorithm mismated.\n"); |
3621 | return(EINVAL); | | 3621 | return(EINVAL); |
3622 | } | | 3622 | } |
3623 | error = xform_init(sav, XF_TCPSIGNATURE); | | 3623 | error = xform_init(sav, XF_TCPSIGNATURE); |
3624 | break; | | 3624 | break; |
3625 | default: | | 3625 | default: |
3626 | IPSECLOG(LOG_DEBUG, "Invalid satype.\n"); | | 3626 | IPSECLOG(LOG_DEBUG, "Invalid satype.\n"); |
3627 | error = EPROTONOSUPPORT; | | 3627 | error = EPROTONOSUPPORT; |
3628 | break; | | 3628 | break; |
3629 | } | | 3629 | } |
3630 | | | 3630 | |
3631 | return error; | | 3631 | return error; |
3632 | } | | 3632 | } |
3633 | | | 3633 | |
3634 | /* | | 3634 | /* |
3635 | * subroutine for SADB_GET and SADB_DUMP. It never return NULL. | | 3635 | * subroutine for SADB_GET and SADB_DUMP. It never return NULL. |
3636 | */ | | 3636 | */ |
3637 | static struct mbuf * | | 3637 | static struct mbuf * |
3638 | key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype, | | 3638 | key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype, |
3639 | u_int32_t seq, u_int32_t pid) | | 3639 | u_int32_t seq, u_int32_t pid) |
3640 | { | | 3640 | { |
3641 | struct mbuf *result = NULL, *tres = NULL, *m; | | 3641 | struct mbuf *result = NULL, *tres = NULL, *m; |
3642 | int l = 0; | | 3642 | int l = 0; |
3643 | int i; | | 3643 | int i; |
3644 | void *p; | | 3644 | void *p; |
3645 | struct sadb_lifetime lt; | | 3645 | struct sadb_lifetime lt; |
3646 | int dumporder[] = { | | 3646 | int dumporder[] = { |
3647 | SADB_EXT_SA, SADB_X_EXT_SA2, | | 3647 | SADB_EXT_SA, SADB_X_EXT_SA2, |
3648 | SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT, | | 3648 | SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT, |
3649 | SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC, | | 3649 | SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC, |
3650 | SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH, | | 3650 | SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH, |
3651 | SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC, | | 3651 | SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC, |
3652 | SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY, | | 3652 | SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY, |
3653 | SADB_X_EXT_NAT_T_TYPE, | | 3653 | SADB_X_EXT_NAT_T_TYPE, |
3654 | SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT, | | 3654 | SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT, |
3655 | SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR, | | 3655 | SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR, |
3656 | SADB_X_EXT_NAT_T_FRAG, | | 3656 | SADB_X_EXT_NAT_T_FRAG, |
3657 | | | 3657 | |
3658 | }; | | 3658 | }; |
3659 | | | 3659 | |
3660 | m = key_setsadbmsg(type, 0, satype, seq, pid, key_sa_refcnt(sav), M_WAITOK); | | 3660 | m = key_setsadbmsg(type, 0, satype, seq, pid, key_sa_refcnt(sav), M_WAITOK); |
3661 | result = m; | | 3661 | result = m; |
3662 | | | 3662 | |
3663 | for (i = __arraycount(dumporder) - 1; i >= 0; i--) { | | 3663 | for (i = __arraycount(dumporder) - 1; i >= 0; i--) { |
3664 | m = NULL; | | 3664 | m = NULL; |
3665 | p = NULL; | | 3665 | p = NULL; |
3666 | switch (dumporder[i]) { | | 3666 | switch (dumporder[i]) { |
3667 | case SADB_EXT_SA: | | 3667 | case SADB_EXT_SA: |
3668 | m = key_setsadbsa(sav); | | 3668 | m = key_setsadbsa(sav); |
3669 | break; | | 3669 | break; |
3670 | | | 3670 | |
3671 | case SADB_X_EXT_SA2: | | 3671 | case SADB_X_EXT_SA2: |
3672 | m = key_setsadbxsa2(sav->sah->saidx.mode, | | 3672 | m = key_setsadbxsa2(sav->sah->saidx.mode, |
3673 | sav->replay ? sav->replay->count : 0, | | 3673 | sav->replay ? sav->replay->count : 0, |
3674 | sav->sah->saidx.reqid); | | 3674 | sav->sah->saidx.reqid); |
3675 | break; | | 3675 | break; |
3676 | | | 3676 | |
3677 | case SADB_EXT_ADDRESS_SRC: | | 3677 | case SADB_EXT_ADDRESS_SRC: |
3678 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, | | 3678 | m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, |
3679 | &sav->sah->saidx.src.sa, | | 3679 | &sav->sah->saidx.src.sa, |
3680 | FULLMASK, IPSEC_ULPROTO_ANY, M_WAITOK); | | 3680 | FULLMASK, IPSEC_ULPROTO_ANY, M_WAITOK); |
3681 | break; | | 3681 | break; |
3682 | | | 3682 | |
3683 | case SADB_EXT_ADDRESS_DST: | | 3683 | case SADB_EXT_ADDRESS_DST: |
3684 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, | | 3684 | m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, |
3685 | &sav->sah->saidx.dst.sa, | | 3685 | &sav->sah->saidx.dst.sa, |
3686 | FULLMASK, IPSEC_ULPROTO_ANY, M_WAITOK); | | 3686 | FULLMASK, IPSEC_ULPROTO_ANY, M_WAITOK); |
3687 | break; | | 3687 | break; |
3688 | | | 3688 | |
3689 | case SADB_EXT_KEY_AUTH: | | 3689 | case SADB_EXT_KEY_AUTH: |
3690 | if (!sav->key_auth) | | 3690 | if (!sav->key_auth) |
3691 | continue; | | 3691 | continue; |
3692 | l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len); | | 3692 | l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len); |
3693 | p = sav->key_auth; | | 3693 | p = sav->key_auth; |
3694 | break; | | 3694 | break; |
3695 | | | 3695 | |
3696 | case SADB_EXT_KEY_ENCRYPT: | | 3696 | case SADB_EXT_KEY_ENCRYPT: |
3697 | if (!sav->key_enc) | | 3697 | if (!sav->key_enc) |
3698 | continue; | | 3698 | continue; |
3699 | l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len); | | 3699 | l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len); |
3700 | p = sav->key_enc; | | 3700 | p = sav->key_enc; |
3701 | break; | | 3701 | break; |
3702 | | | 3702 | |
3703 | case SADB_EXT_LIFETIME_CURRENT: | | 3703 | case SADB_EXT_LIFETIME_CURRENT: |
3704 | KASSERT(sav->lft_c != NULL); | | 3704 | KASSERT(sav->lft_c != NULL); |
3705 | l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len); | | 3705 | l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len); |
3706 | memcpy(<, sav->lft_c, sizeof(struct sadb_lifetime)); | | 3706 | memcpy(<, sav->lft_c, sizeof(struct sadb_lifetime)); |
3707 | lt.sadb_lifetime_addtime = | | 3707 | lt.sadb_lifetime_addtime = |
3708 | time_mono_to_wall(lt.sadb_lifetime_addtime); | | 3708 | time_mono_to_wall(lt.sadb_lifetime_addtime); |
3709 | lt.sadb_lifetime_usetime = | | 3709 | lt.sadb_lifetime_usetime = |
3710 | time_mono_to_wall(lt.sadb_lifetime_usetime); | | 3710 | time_mono_to_wall(lt.sadb_lifetime_usetime); |
3711 | p = < | | 3711 | p = < |
3712 | break; | | 3712 | break; |
3713 | | | 3713 | |
3714 | case SADB_EXT_LIFETIME_HARD: | | 3714 | case SADB_EXT_LIFETIME_HARD: |
3715 | if (!sav->lft_h) | | 3715 | if (!sav->lft_h) |
3716 | continue; | | 3716 | continue; |
3717 | l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len); | | 3717 | l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len); |
3718 | p = sav->lft_h; | | 3718 | p = sav->lft_h; |
3719 | break; | | 3719 | break; |
3720 | | | 3720 | |
3721 | case SADB_EXT_LIFETIME_SOFT: | | 3721 | case SADB_EXT_LIFETIME_SOFT: |
3722 | if (!sav->lft_s) | | 3722 | if (!sav->lft_s) |
3723 | continue; | | 3723 | continue; |
3724 | l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len); | | 3724 | l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len); |
3725 | p = sav->lft_s; | | 3725 | p = sav->lft_s; |
3726 | break; | | 3726 | break; |
3727 | | | 3727 | |
3728 | case SADB_X_EXT_NAT_T_TYPE: | | 3728 | case SADB_X_EXT_NAT_T_TYPE: |
3729 | m = key_setsadbxtype(sav->natt_type); | | 3729 | m = key_setsadbxtype(sav->natt_type); |
3730 | break; | | 3730 | break; |
3731 | | | 3731 | |
3732 | case SADB_X_EXT_NAT_T_DPORT: | | 3732 | case SADB_X_EXT_NAT_T_DPORT: |
3733 | if (sav->natt_type == 0) | | 3733 | if (sav->natt_type == 0) |
3734 | continue; | | 3734 | continue; |
3735 | m = key_setsadbxport( | | 3735 | m = key_setsadbxport( |
3736 | key_portfromsaddr(&sav->sah->saidx.dst), | | 3736 | key_portfromsaddr(&sav->sah->saidx.dst), |
3737 | SADB_X_EXT_NAT_T_DPORT); | | 3737 | SADB_X_EXT_NAT_T_DPORT); |
3738 | break; | | 3738 | break; |
3739 | | | 3739 | |
3740 | case SADB_X_EXT_NAT_T_SPORT: | | 3740 | case SADB_X_EXT_NAT_T_SPORT: |
3741 | if (sav->natt_type == 0) | | 3741 | if (sav->natt_type == 0) |
3742 | continue; | | 3742 | continue; |
3743 | m = key_setsadbxport( | | 3743 | m = key_setsadbxport( |
3744 | key_portfromsaddr(&sav->sah->saidx.src), | | 3744 | key_portfromsaddr(&sav->sah->saidx.src), |
3745 | SADB_X_EXT_NAT_T_SPORT); | | 3745 | SADB_X_EXT_NAT_T_SPORT); |
3746 | break; | | 3746 | break; |
3747 | | | 3747 | |
3748 | case SADB_X_EXT_NAT_T_FRAG: | | 3748 | case SADB_X_EXT_NAT_T_FRAG: |
3749 | /* don't send frag info if not set */ | | 3749 | /* don't send frag info if not set */ |
3750 | if (sav->natt_type == 0 || sav->esp_frag == IP_MAXPACKET) | | 3750 | if (sav->natt_type == 0 || sav->esp_frag == IP_MAXPACKET) |
3751 | continue; | | 3751 | continue; |
3752 | m = key_setsadbxfrag(sav->esp_frag); | | 3752 | m = key_setsadbxfrag(sav->esp_frag); |
3753 | break; | | 3753 | break; |
3754 | | | 3754 | |
3755 | case SADB_X_EXT_NAT_T_OAI: | | 3755 | case SADB_X_EXT_NAT_T_OAI: |
3756 | case SADB_X_EXT_NAT_T_OAR: | | 3756 | case SADB_X_EXT_NAT_T_OAR: |
3757 | continue; | | 3757 | continue; |
3758 | | | 3758 | |
3759 | case SADB_EXT_ADDRESS_PROXY: | | 3759 | case SADB_EXT_ADDRESS_PROXY: |
3760 | case SADB_EXT_IDENTITY_SRC: | | 3760 | case SADB_EXT_IDENTITY_SRC: |
3761 | case SADB_EXT_IDENTITY_DST: | | 3761 | case SADB_EXT_IDENTITY_DST: |
3762 | /* XXX: should we brought from SPD ? */ | | 3762 | /* XXX: should we brought from SPD ? */ |
3763 | case SADB_EXT_SENSITIVITY: | | 3763 | case SADB_EXT_SENSITIVITY: |
3764 | default: | | 3764 | default: |
3765 | continue; | | 3765 | continue; |
3766 | } | | 3766 | } |
3767 | | | 3767 | |
3768 | KASSERT(!(m && p)); | | 3768 | KASSERT(!(m && p)); |
3769 | KASSERT(m != NULL || p != NULL); | | 3769 | KASSERT(m != NULL || p != NULL); |
3770 | if (p && tres) { | | 3770 | if (p && tres) { |
3771 | M_PREPEND(tres, l, M_WAITOK); | | 3771 | M_PREPEND(tres, l, M_WAITOK); |
3772 | memcpy(mtod(tres, void *), p, l); | | 3772 | memcpy(mtod(tres, void *), p, l); |
3773 | continue; | | 3773 | continue; |
3774 | } | | 3774 | } |
3775 | if (p) { | | 3775 | if (p) { |
3776 | m = key_alloc_mbuf(l, M_WAITOK); | | 3776 | m = key_alloc_mbuf(l, M_WAITOK); |
3777 | m_copyback(m, 0, l, p); | | 3777 | m_copyback(m, 0, l, p); |
3778 | } | | 3778 | } |
3779 | | | 3779 | |
3780 | if (tres) | | 3780 | if (tres) |
3781 | m_cat(m, tres); | | 3781 | m_cat(m, tres); |
3782 | tres = m; | | 3782 | tres = m; |
3783 | } | | 3783 | } |
3784 | | | 3784 | |
3785 | m_cat(result, tres); | | 3785 | m_cat(result, tres); |
3786 | tres = NULL; /* avoid free on error below */ | | 3786 | tres = NULL; /* avoid free on error below */ |
3787 | | | 3787 | |
3788 | KASSERT(result->m_len >= sizeof(struct sadb_msg)); | | 3788 | KASSERT(result->m_len >= sizeof(struct sadb_msg)); |
3789 | | | 3789 | |
3790 | result->m_pkthdr.len = 0; | | 3790 | result->m_pkthdr.len = 0; |
3791 | for (m = result; m; m = m->m_next) | | 3791 | for (m = result; m; m = m->m_next) |
3792 | result->m_pkthdr.len += m->m_len; | | 3792 | result->m_pkthdr.len += m->m_len; |
3793 | | | 3793 | |
3794 | mtod(result, struct sadb_msg *)->sadb_msg_len = | | 3794 | mtod(result, struct sadb_msg *)->sadb_msg_len = |
3795 | PFKEY_UNIT64(result->m_pkthdr.len); | | 3795 | PFKEY_UNIT64(result->m_pkthdr.len); |
3796 | | | 3796 | |
3797 | return result; | | 3797 | return result; |
3798 | } | | 3798 | } |
3799 | | | 3799 | |
3800 | | | 3800 | |
3801 | /* | | 3801 | /* |
3802 | * set a type in sadb_x_nat_t_type | | 3802 | * set a type in sadb_x_nat_t_type |
3803 | */ | | 3803 | */ |
3804 | static struct mbuf * | | 3804 | static struct mbuf * |
3805 | key_setsadbxtype(u_int16_t type) | | 3805 | key_setsadbxtype(u_int16_t type) |
3806 | { | | 3806 | { |
3807 | struct mbuf *m; | | 3807 | struct mbuf *m; |
3808 | size_t len; | | 3808 | size_t len; |
3809 | struct sadb_x_nat_t_type *p; | | 3809 | struct sadb_x_nat_t_type *p; |
3810 | | | 3810 | |
3811 | len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type)); | | 3811 | len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type)); |
3812 | | | 3812 | |
3813 | m = key_alloc_mbuf(len, M_WAITOK); | | 3813 | m = key_alloc_mbuf(len, M_WAITOK); |
3814 | KASSERT(m->m_next == NULL); | | 3814 | KASSERT(m->m_next == NULL); |
3815 | | | 3815 | |
3816 | p = mtod(m, struct sadb_x_nat_t_type *); | | 3816 | p = mtod(m, struct sadb_x_nat_t_type *); |
3817 | | | 3817 | |
3818 | memset(p, 0, len); | | 3818 | memset(p, 0, len); |
3819 | p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len); | | 3819 | p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len); |
3820 | p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE; | | 3820 | p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE; |
3821 | p->sadb_x_nat_t_type_type = type; | | 3821 | p->sadb_x_nat_t_type_type = type; |
3822 | | | 3822 | |
3823 | return m; | | 3823 | return m; |
3824 | } | | 3824 | } |
3825 | /* | | 3825 | /* |
3826 | * set a port in sadb_x_nat_t_port. port is in network order | | 3826 | * set a port in sadb_x_nat_t_port. port is in network order |
3827 | */ | | 3827 | */ |
3828 | static struct mbuf * | | 3828 | static struct mbuf * |
3829 | key_setsadbxport(u_int16_t port, u_int16_t type) | | 3829 | key_setsadbxport(u_int16_t port, u_int16_t type) |
3830 | { | | 3830 | { |
3831 | struct mbuf *m; | | 3831 | struct mbuf *m; |
3832 | size_t len; | | 3832 | size_t len; |
3833 | struct sadb_x_nat_t_port *p; | | 3833 | struct sadb_x_nat_t_port *p; |
3834 | | | 3834 | |
3835 | len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port)); | | 3835 | len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port)); |
3836 | | | 3836 | |
3837 | m = key_alloc_mbuf(len, M_WAITOK); | | 3837 | m = key_alloc_mbuf(len, M_WAITOK); |
3838 | KASSERT(m->m_next == NULL); | | 3838 | KASSERT(m->m_next == NULL); |
3839 | | | 3839 | |
3840 | p = mtod(m, struct sadb_x_nat_t_port *); | | 3840 | p = mtod(m, struct sadb_x_nat_t_port *); |
3841 | | | 3841 | |
3842 | memset(p, 0, len); | | 3842 | memset(p, 0, len); |
3843 | p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len); | | 3843 | p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len); |
3844 | p->sadb_x_nat_t_port_exttype = type; | | 3844 | p->sadb_x_nat_t_port_exttype = type; |
3845 | p->sadb_x_nat_t_port_port = port; | | 3845 | p->sadb_x_nat_t_port_port = port; |
3846 | | | 3846 | |
3847 | return m; | | 3847 | return m; |
3848 | } | | 3848 | } |
3849 | | | 3849 | |
3850 | /* | | 3850 | /* |
3851 | * set fragmentation info in sadb_x_nat_t_frag | | 3851 | * set fragmentation info in sadb_x_nat_t_frag |
3852 | */ | | 3852 | */ |
3853 | static struct mbuf * | | 3853 | static struct mbuf * |
3854 | key_setsadbxfrag(u_int16_t flen) | | 3854 | key_setsadbxfrag(u_int16_t flen) |
3855 | { | | 3855 | { |
3856 | struct mbuf *m; | | 3856 | struct mbuf *m; |
3857 | size_t len; | | 3857 | size_t len; |
3858 | struct sadb_x_nat_t_frag *p; | | 3858 | struct sadb_x_nat_t_frag *p; |
3859 | | | 3859 | |
3860 | len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_frag)); | | 3860 | len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_frag)); |
3861 | | | 3861 | |
3862 | m = key_alloc_mbuf(len, M_WAITOK); | | 3862 | m = key_alloc_mbuf(len, M_WAITOK); |
3863 | KASSERT(m->m_next == NULL); | | 3863 | KASSERT(m->m_next == NULL); |
3864 | | | 3864 | |
3865 | p = mtod(m, struct sadb_x_nat_t_frag *); | | 3865 | p = mtod(m, struct sadb_x_nat_t_frag *); |
3866 | | | 3866 | |
3867 | memset(p, 0, len); | | 3867 | memset(p, 0, len); |
3868 | p->sadb_x_nat_t_frag_len = PFKEY_UNIT64(len); | | 3868 | p->sadb_x_nat_t_frag_len = PFKEY_UNIT64(len); |
3869 | p->sadb_x_nat_t_frag_exttype = SADB_X_EXT_NAT_T_FRAG; | | 3869 | p->sadb_x_nat_t_frag_exttype = SADB_X_EXT_NAT_T_FRAG; |
3870 | p->sadb_x_nat_t_frag_fraglen = flen; | | 3870 | p->sadb_x_nat_t_frag_fraglen = flen; |
3871 | | | 3871 | |
3872 | return m; | | 3872 | return m; |
3873 | } | | 3873 | } |
3874 | | | 3874 | |
3875 | /* | | 3875 | /* |
3876 | * Get port from sockaddr, port is in network order | | 3876 | * Get port from sockaddr, port is in network order |
3877 | */ | | 3877 | */ |
3878 | u_int16_t | | 3878 | u_int16_t |
3879 | key_portfromsaddr(const union sockaddr_union *saddr) | | 3879 | key_portfromsaddr(const union sockaddr_union *saddr) |
3880 | { | | 3880 | { |
3881 | u_int16_t port; | | 3881 | u_int16_t port; |
3882 | | | 3882 | |
3883 | switch (saddr->sa.sa_family) { | | 3883 | switch (saddr->sa.sa_family) { |
3884 | case AF_INET: { | | 3884 | case AF_INET: { |
3885 | port = saddr->sin.sin_port; | | 3885 | port = saddr->sin.sin_port; |
3886 | break; | | 3886 | break; |
3887 | } | | 3887 | } |
3888 | #ifdef INET6 | | 3888 | #ifdef INET6 |
3889 | case AF_INET6: { | | 3889 | case AF_INET6: { |
3890 | port = saddr->sin6.sin6_port; | | 3890 | port = saddr->sin6.sin6_port; |
3891 | break; | | 3891 | break; |
3892 | } | | 3892 | } |
3893 | #endif | | 3893 | #endif |
3894 | default: | | 3894 | default: |
3895 | printf("%s: unexpected address family\n", __func__); | | 3895 | printf("%s: unexpected address family\n", __func__); |
3896 | port = 0; | | 3896 | port = 0; |
3897 | break; | | 3897 | break; |
3898 | } | | 3898 | } |
3899 | | | 3899 | |
3900 | return port; | | 3900 | return port; |
3901 | } | | 3901 | } |
3902 | | | 3902 | |
3903 | | | 3903 | |
3904 | /* | | 3904 | /* |
3905 | * Set port is struct sockaddr. port is in network order | | 3905 | * Set port is struct sockaddr. port is in network order |
3906 | */ | | 3906 | */ |
3907 | static void | | 3907 | static void |
3908 | key_porttosaddr(union sockaddr_union *saddr, u_int16_t port) | | 3908 | key_porttosaddr(union sockaddr_union *saddr, u_int16_t port) |
3909 | { | | 3909 | { |
3910 | switch (saddr->sa.sa_family) { | | 3910 | switch (saddr->sa.sa_family) { |
3911 | case AF_INET: { | | 3911 | case AF_INET: { |
3912 | saddr->sin.sin_port = port; | | 3912 | saddr->sin.sin_port = port; |
3913 | break; | | 3913 | break; |
3914 | } | | 3914 | } |
3915 | #ifdef INET6 | | 3915 | #ifdef INET6 |
3916 | case AF_INET6: { | | 3916 | case AF_INET6: { |
3917 | saddr->sin6.sin6_port = port; | | 3917 | saddr->sin6.sin6_port = port; |
3918 | break; | | 3918 | break; |
3919 | } | | 3919 | } |
3920 | #endif | | 3920 | #endif |
3921 | default: | | 3921 | default: |
3922 | printf("%s: unexpected address family %d\n", __func__, | | 3922 | printf("%s: unexpected address family %d\n", __func__, |
3923 | saddr->sa.sa_family); | | 3923 | saddr->sa.sa_family); |
3924 | break; | | 3924 | break; |
3925 | } | | 3925 | } |
3926 | | | 3926 | |
3927 | return; | | 3927 | return; |
3928 | } | | 3928 | } |
3929 | | | 3929 | |
3930 | /* | | 3930 | /* |
3931 | * Safety check sa_len | | 3931 | * Safety check sa_len |
3932 | */ | | 3932 | */ |
3933 | static int | | 3933 | static int |
3934 | key_checksalen(const union sockaddr_union *saddr) | | 3934 | key_checksalen(const union sockaddr_union *saddr) |
3935 | { | | 3935 | { |
3936 | switch (saddr->sa.sa_family) { | | 3936 | switch (saddr->sa.sa_family) { |
3937 | case AF_INET: | | 3937 | case AF_INET: |
3938 | if (saddr->sa.sa_len != sizeof(struct sockaddr_in)) | | 3938 | if (saddr->sa.sa_len != sizeof(struct sockaddr_in)) |
3939 | return -1; | | 3939 | return -1; |
3940 | break; | | 3940 | break; |
3941 | #ifdef INET6 | | 3941 | #ifdef INET6 |
3942 | case AF_INET6: | | 3942 | case AF_INET6: |
3943 | if (saddr->sa.sa_len != sizeof(struct sockaddr_in6)) | | 3943 | if (saddr->sa.sa_len != sizeof(struct sockaddr_in6)) |
3944 | return -1; | | 3944 | return -1; |
3945 | break; | | 3945 | break; |
3946 | #endif | | 3946 | #endif |
3947 | default: | | 3947 | default: |
3948 | printf("%s: unexpected sa_family %d\n", __func__, | | 3948 | printf("%s: unexpected sa_family %d\n", __func__, |
3949 | saddr->sa.sa_family); | | 3949 | saddr->sa.sa_family); |
3950 | return -1; | | 3950 | return -1; |
3951 | break; | | 3951 | break; |
3952 | } | | 3952 | } |
3953 | return 0; | | 3953 | return 0; |
3954 | } | | 3954 | } |
3955 | | | 3955 | |
3956 | | | 3956 | |
3957 | /* | | 3957 | /* |
3958 | * set data into sadb_msg. | | 3958 | * set data into sadb_msg. |
3959 | */ | | 3959 | */ |
3960 | static struct mbuf * | | 3960 | static struct mbuf * |
3961 | key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, | | 3961 | key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, |
3962 | u_int32_t seq, pid_t pid, u_int16_t reserved, int mflag) | | 3962 | u_int32_t seq, pid_t pid, u_int16_t reserved, int mflag) |
3963 | { | | 3963 | { |
3964 | struct mbuf *m; | | 3964 | struct mbuf *m; |
3965 | struct sadb_msg *p; | | 3965 | struct sadb_msg *p; |
3966 | int len; | | 3966 | int len; |
3967 | | | 3967 | |
3968 | CTASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg)) <= MCLBYTES); | | 3968 | CTASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg)) <= MCLBYTES); |
3969 | | | 3969 | |
3970 | len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); | | 3970 | len = PFKEY_ALIGN8(sizeof(struct sadb_msg)); |
3971 | | | 3971 | |
3972 | m = key_alloc_mbuf_simple(len, mflag); | | 3972 | m = key_alloc_mbuf_simple(len, mflag); |
3973 | if (!m) | | 3973 | if (!m) |
3974 | return NULL; | | 3974 | return NULL; |
3975 | m->m_pkthdr.len = m->m_len = len; | | 3975 | m->m_pkthdr.len = m->m_len = len; |
3976 | m->m_next = NULL; | | 3976 | m->m_next = NULL; |
3977 | | | 3977 | |
3978 | p = mtod(m, struct sadb_msg *); | | 3978 | p = mtod(m, struct sadb_msg *); |
3979 | | | 3979 | |
3980 | memset(p, 0, len); | | 3980 | memset(p, 0, len); |
3981 | p->sadb_msg_version = PF_KEY_V2; | | 3981 | p->sadb_msg_version = PF_KEY_V2; |
3982 | p->sadb_msg_type = type; | | 3982 | p->sadb_msg_type = type; |
3983 | p->sadb_msg_errno = 0; | | 3983 | p->sadb_msg_errno = 0; |
3984 | p->sadb_msg_satype = satype; | | 3984 | p->sadb_msg_satype = satype; |
3985 | p->sadb_msg_len = PFKEY_UNIT64(tlen); | | 3985 | p->sadb_msg_len = PFKEY_UNIT64(tlen); |
3986 | p->sadb_msg_reserved = reserved; | | 3986 | p->sadb_msg_reserved = reserved; |
3987 | p->sadb_msg_seq = seq; | | 3987 | p->sadb_msg_seq = seq; |
3988 | p->sadb_msg_pid = (u_int32_t)pid; | | 3988 | p->sadb_msg_pid = (u_int32_t)pid; |
3989 | | | 3989 | |
3990 | return m; | | 3990 | return m; |
3991 | } | | 3991 | } |
3992 | | | 3992 | |
3993 | /* | | 3993 | /* |
3994 | * copy secasvar data into sadb_address. | | 3994 | * copy secasvar data into sadb_address. |
3995 | */ | | 3995 | */ |
3996 | static struct mbuf * | | 3996 | static struct mbuf * |
3997 | key_setsadbsa(struct secasvar *sav) | | 3997 | key_setsadbsa(struct secasvar *sav) |
3998 | { | | 3998 | { |
3999 | struct mbuf *m; | | 3999 | struct mbuf *m; |
4000 | struct sadb_sa *p; | | 4000 | struct sadb_sa *p; |
4001 | int len; | | 4001 | int len; |
4002 | | | 4002 | |
4003 | len = PFKEY_ALIGN8(sizeof(struct sadb_sa)); | | 4003 | len = PFKEY_ALIGN8(sizeof(struct sadb_sa)); |
4004 | m = key_alloc_mbuf(len, M_WAITOK); | | 4004 | m = key_alloc_mbuf(len, M_WAITOK); |
4005 | KASSERT(m->m_next == NULL); | | 4005 | KASSERT(m->m_next == NULL); |
4006 | | | 4006 | |
4007 | p = mtod(m, struct sadb_sa *); | | 4007 | p = mtod(m, struct sadb_sa *); |
4008 | | | 4008 | |
4009 | memset(p, 0, len); | | 4009 | memset(p, 0, len); |
4010 | p->sadb_sa_len = PFKEY_UNIT64(len); | | 4010 | p->sadb_sa_len = PFKEY_UNIT64(len); |
4011 | p->sadb_sa_exttype = SADB_EXT_SA; | | 4011 | p->sadb_sa_exttype = SADB_EXT_SA; |
4012 | p->sadb_sa_spi = sav->spi; | | 4012 | p->sadb_sa_spi = sav->spi; |
4013 | p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0); | | 4013 | p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0); |
4014 | p->sadb_sa_state = sav->state; | | 4014 | p->sadb_sa_state = sav->state; |
4015 | p->sadb_sa_auth = sav->alg_auth; | | 4015 | p->sadb_sa_auth = sav->alg_auth; |
4016 | p->sadb_sa_encrypt = sav->alg_enc; | | 4016 | p->sadb_sa_encrypt = sav->alg_enc; |
4017 | p->sadb_sa_flags = sav->flags; | | 4017 | p->sadb_sa_flags = sav->flags; |
4018 | | | 4018 | |
4019 | return m; | | 4019 | return m; |
4020 | } | | 4020 | } |
4021 | | | 4021 | |
4022 | /* | | 4022 | /* |
4023 | * set data into sadb_address. | | 4023 | * set data into sadb_address. |
4024 | */ | | 4024 | */ |
4025 | static struct mbuf * | | 4025 | static struct mbuf * |
4026 | key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr, | | 4026 | key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr, |
4027 | u_int8_t prefixlen, u_int16_t ul_proto, int mflag) | | 4027 | u_int8_t prefixlen, u_int16_t ul_proto, int mflag) |
4028 | { | | 4028 | { |
4029 | struct mbuf *m; | | 4029 | struct mbuf *m; |
4030 | struct sadb_address *p; | | 4030 | struct sadb_address *p; |
4031 | size_t len; | | 4031 | size_t len; |
4032 | | | 4032 | |
4033 | len = PFKEY_ALIGN8(sizeof(struct sadb_address)) + | | 4033 | len = PFKEY_ALIGN8(sizeof(struct sadb_address)) + |
4034 | PFKEY_ALIGN8(saddr->sa_len); | | 4034 | PFKEY_ALIGN8(saddr->sa_len); |
4035 | m = key_alloc_mbuf(len, mflag); | | 4035 | m = key_alloc_mbuf(len, mflag); |
4036 | if (!m || m->m_next) { /*XXX*/ | | 4036 | if (!m || m->m_next) { /*XXX*/ |
4037 | if (m) | | 4037 | if (m) |
4038 | m_freem(m); | | 4038 | m_freem(m); |
4039 | return NULL; | | 4039 | return NULL; |
4040 | } | | 4040 | } |
4041 | | | 4041 | |
4042 | p = mtod(m, struct sadb_address *); | | 4042 | p = mtod(m, struct sadb_address *); |
4043 | | | 4043 | |
4044 | memset(p, 0, len); | | 4044 | memset(p, 0, len); |
4045 | p->sadb_address_len = PFKEY_UNIT64(len); | | 4045 | p->sadb_address_len = PFKEY_UNIT64(len); |
4046 | p->sadb_address_exttype = exttype; | | 4046 | p->sadb_address_exttype = exttype; |
4047 | p->sadb_address_proto = ul_proto; | | 4047 | p->sadb_address_proto = ul_proto; |
4048 | if (prefixlen == FULLMASK) { | | 4048 | if (prefixlen == FULLMASK) { |
4049 | switch (saddr->sa_family) { | | 4049 | switch (saddr->sa_family) { |
4050 | case AF_INET: | | 4050 | case AF_INET: |
4051 | prefixlen = sizeof(struct in_addr) << 3; | | 4051 | prefixlen = sizeof(struct in_addr) << 3; |