Print this page
8529 Extended and regular SADB_ACQUIREs should share address extension code
Portions contributed by: Bayard Bell <buffer.g.overflow@gmail.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/inet/ip/ipsecah.c
+++ new/usr/src/uts/common/inet/ip/ipsecah.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 * Copyright (c) 2012 Nexenta Systems, Inc. All rights reserved.
25 25 */
26 26
27 27 #include <sys/types.h>
28 28 #include <sys/stream.h>
29 29 #include <sys/stropts.h>
30 30 #include <sys/errno.h>
31 31 #include <sys/strlog.h>
32 32 #include <sys/tihdr.h>
33 33 #include <sys/socket.h>
34 34 #include <sys/ddi.h>
35 35 #include <sys/sunddi.h>
36 36 #include <sys/mkdev.h>
37 37 #include <sys/kmem.h>
38 38 #include <sys/zone.h>
39 39 #include <sys/sysmacros.h>
40 40 #include <sys/cmn_err.h>
41 41 #include <sys/vtrace.h>
42 42 #include <sys/debug.h>
43 43 #include <sys/atomic.h>
44 44 #include <sys/strsun.h>
45 45 #include <sys/random.h>
46 46 #include <netinet/in.h>
47 47 #include <net/if.h>
48 48 #include <netinet/ip6.h>
49 49 #include <netinet/icmp6.h>
50 50 #include <net/pfkeyv2.h>
51 51 #include <net/pfpolicy.h>
52 52
53 53 #include <inet/common.h>
54 54 #include <inet/mi.h>
55 55 #include <inet/ip.h>
56 56 #include <inet/ip6.h>
57 57 #include <inet/nd.h>
58 58 #include <inet/ip_if.h>
59 59 #include <inet/ip_ndp.h>
60 60 #include <inet/ipsec_info.h>
61 61 #include <inet/ipsec_impl.h>
62 62 #include <inet/sadb.h>
63 63 #include <inet/ipsecah.h>
64 64 #include <inet/ipsec_impl.h>
65 65 #include <inet/ipdrop.h>
66 66 #include <sys/taskq.h>
67 67 #include <sys/policy.h>
68 68 #include <sys/strsun.h>
69 69
70 70 #include <sys/crypto/common.h>
71 71 #include <sys/crypto/api.h>
72 72 #include <sys/kstat.h>
73 73 #include <sys/strsubr.h>
74 74
75 75 #include <sys/tsol/tnet.h>
76 76
77 77 /*
78 78 * Table of ND variables supported by ipsecah. These are loaded into
79 79 * ipsecah_g_nd in ipsecah_init_nd.
80 80 * All of these are alterable, within the min/max values given, at run time.
81 81 */
82 82 static ipsecahparam_t lcl_param_arr[] = {
83 83 /* min max value name */
84 84 { 0, 3, 0, "ipsecah_debug"},
85 85 { 125, 32000, SADB_AGE_INTERVAL_DEFAULT, "ipsecah_age_interval"},
86 86 { 1, 10, 1, "ipsecah_reap_delay"},
87 87 { 1, SADB_MAX_REPLAY, 64, "ipsecah_replay_size"},
88 88 { 1, 300, 15, "ipsecah_acquire_timeout"},
|
↓ open down ↓ |
88 lines elided |
↑ open up ↑ |
89 89 { 1, 1800, 90, "ipsecah_larval_timeout"},
90 90 /* Default lifetime values for ACQUIRE messages. */
91 91 { 0, 0xffffffffU, 0, "ipsecah_default_soft_bytes"},
92 92 { 0, 0xffffffffU, 0, "ipsecah_default_hard_bytes"},
93 93 { 0, 0xffffffffU, 24000, "ipsecah_default_soft_addtime"},
94 94 { 0, 0xffffffffU, 28800, "ipsecah_default_hard_addtime"},
95 95 { 0, 0xffffffffU, 0, "ipsecah_default_soft_usetime"},
96 96 { 0, 0xffffffffU, 0, "ipsecah_default_hard_usetime"},
97 97 { 0, 1, 0, "ipsecah_log_unknown_spi"},
98 98 };
99 -#define ipsecah_debug ipsecah_params[0].ipsecah_param_value
100 -#define ipsecah_age_interval ipsecah_params[1].ipsecah_param_value
101 -#define ipsecah_age_int_max ipsecah_params[1].ipsecah_param_max
102 -#define ipsecah_reap_delay ipsecah_params[2].ipsecah_param_value
103 -#define ipsecah_replay_size ipsecah_params[3].ipsecah_param_value
104 -#define ipsecah_acquire_timeout ipsecah_params[4].ipsecah_param_value
105 -#define ipsecah_larval_timeout ipsecah_params[5].ipsecah_param_value
106 -#define ipsecah_default_soft_bytes ipsecah_params[6].ipsecah_param_value
107 -#define ipsecah_default_hard_bytes ipsecah_params[7].ipsecah_param_value
108 -#define ipsecah_default_soft_addtime ipsecah_params[8].ipsecah_param_value
109 -#define ipsecah_default_hard_addtime ipsecah_params[9].ipsecah_param_value
110 -#define ipsecah_default_soft_usetime ipsecah_params[10].ipsecah_param_value
111 -#define ipsecah_default_hard_usetime ipsecah_params[11].ipsecah_param_value
112 -#define ipsecah_log_unknown_spi ipsecah_params[12].ipsecah_param_value
113 99
114 100 #define ah0dbg(a) printf a
115 101 /* NOTE: != 0 instead of > 0 so lint doesn't complain. */
116 102 #define ah1dbg(ahstack, a) if (ahstack->ipsecah_debug != 0) printf a
117 103 #define ah2dbg(ahstack, a) if (ahstack->ipsecah_debug > 1) printf a
118 104 #define ah3dbg(ahstack, a) if (ahstack->ipsecah_debug > 2) printf a
119 105
120 106 /*
121 107 * XXX This is broken. Padding should be determined dynamically
122 108 * depending on the ICV size and IP version number so that the
123 109 * total AH header size is a multiple of 32 bits or 64 bits
124 110 * for V4 and V6 respectively. For 96bit ICVs we have no problems.
125 111 * Anything different from that, we need to fix our code.
126 112 */
127 113 #define IPV4_PADDING_ALIGN 0x04 /* Multiple of 32 bits */
128 114 #define IPV6_PADDING_ALIGN 0x04 /* Multiple of 32 bits */
129 115
130 116 /*
131 117 * Helper macro. Avoids a call to msgdsize if there is only one
132 118 * mblk in the chain.
133 119 */
134 120 #define AH_MSGSIZE(mp) ((mp)->b_cont != NULL ? msgdsize(mp) : MBLKL(mp))
135 121
136 122
137 123 static mblk_t *ah_auth_out_done(mblk_t *, ip_xmit_attr_t *, ipsec_crypto_t *);
138 124 static mblk_t *ah_auth_in_done(mblk_t *, ip_recv_attr_t *, ipsec_crypto_t *);
139 125 static mblk_t *ah_process_ip_options_v4(mblk_t *, ipsa_t *, int *, uint_t,
140 126 boolean_t, ipsecah_stack_t *);
141 127 static mblk_t *ah_process_ip_options_v6(mblk_t *, ipsa_t *, int *, uint_t,
|
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
142 128 boolean_t, ipsecah_stack_t *);
143 129 static void ah_getspi(mblk_t *, keysock_in_t *, ipsecah_stack_t *);
144 130 static void ah_inbound_restart(mblk_t *, ip_recv_attr_t *);
145 131
146 132 static mblk_t *ah_outbound(mblk_t *, ip_xmit_attr_t *);
147 133 static void ah_outbound_finish(mblk_t *, ip_xmit_attr_t *);
148 134
149 135 static int ipsecah_open(queue_t *, dev_t *, int, int, cred_t *);
150 136 static int ipsecah_close(queue_t *);
151 137 static void ipsecah_wput(queue_t *, mblk_t *);
152 -static void ah_send_acquire(ipsacq_t *, mblk_t *, netstack_t *);
153 138 static boolean_t ah_register_out(uint32_t, uint32_t, uint_t, ipsecah_stack_t *,
154 139 cred_t *);
155 140 static void *ipsecah_stack_init(netstackid_t stackid, netstack_t *ns);
156 141 static void ipsecah_stack_fini(netstackid_t stackid, void *arg);
157 142
158 143 /* Setable in /etc/system */
159 144 uint32_t ah_hash_size = IPSEC_DEFAULT_HASH_SIZE;
160 145
161 146 static taskq_t *ah_taskq;
162 147
163 148 static struct module_info info = {
164 149 5136, "ipsecah", 0, INFPSZ, 65536, 1024
165 150 };
166 151
167 152 static struct qinit rinit = {
168 153 (pfi_t)putnext, NULL, ipsecah_open, ipsecah_close, NULL, &info,
169 154 NULL
170 155 };
171 156
172 157 static struct qinit winit = {
173 158 (pfi_t)ipsecah_wput, NULL, ipsecah_open, ipsecah_close, NULL, &info,
174 159 NULL
175 160 };
176 161
177 162 struct streamtab ipsecahinfo = {
178 163 &rinit, &winit, NULL, NULL
179 164 };
180 165
181 166 static int ah_kstat_update(kstat_t *, int);
182 167
183 168 uint64_t ipsacq_maxpackets = IPSACQ_MAXPACKETS;
184 169
185 170 static boolean_t
186 171 ah_kstat_init(ipsecah_stack_t *ahstack, netstackid_t stackid)
187 172 {
188 173 ipsec_stack_t *ipss = ahstack->ipsecah_netstack->netstack_ipsec;
189 174
190 175 ahstack->ah_ksp = kstat_create_netstack("ipsecah", 0, "ah_stat", "net",
191 176 KSTAT_TYPE_NAMED, sizeof (ah_kstats_t) / sizeof (kstat_named_t),
192 177 KSTAT_FLAG_PERSISTENT, stackid);
193 178
194 179 if (ahstack->ah_ksp == NULL || ahstack->ah_ksp->ks_data == NULL)
195 180 return (B_FALSE);
196 181
197 182 ahstack->ah_kstats = ahstack->ah_ksp->ks_data;
198 183
199 184 ahstack->ah_ksp->ks_update = ah_kstat_update;
200 185 ahstack->ah_ksp->ks_private = (void *)(uintptr_t)stackid;
201 186
202 187 #define K64 KSTAT_DATA_UINT64
203 188 #define KI(x) kstat_named_init(&(ahstack->ah_kstats->ah_stat_##x), #x, K64)
204 189
205 190 KI(num_aalgs);
206 191 KI(good_auth);
207 192 KI(bad_auth);
208 193 KI(replay_failures);
209 194 KI(replay_early_failures);
210 195 KI(keysock_in);
211 196 KI(out_requests);
212 197 KI(acquire_requests);
213 198 KI(bytes_expired);
214 199 KI(out_discards);
215 200 KI(crypto_sync);
216 201 KI(crypto_async);
217 202 KI(crypto_failures);
218 203
219 204 #undef KI
220 205 #undef K64
221 206
222 207 kstat_install(ahstack->ah_ksp);
223 208 IP_ACQUIRE_STAT(ipss, maxpackets, ipsacq_maxpackets);
224 209 return (B_TRUE);
225 210 }
226 211
227 212 static int
228 213 ah_kstat_update(kstat_t *kp, int rw)
229 214 {
230 215 ah_kstats_t *ekp;
231 216 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private;
232 217 netstack_t *ns;
233 218 ipsec_stack_t *ipss;
234 219
235 220 if ((kp == NULL) || (kp->ks_data == NULL))
236 221 return (EIO);
237 222
238 223 if (rw == KSTAT_WRITE)
239 224 return (EACCES);
240 225
241 226 ns = netstack_find_by_stackid(stackid);
242 227 if (ns == NULL)
243 228 return (-1);
244 229 ipss = ns->netstack_ipsec;
245 230 if (ipss == NULL) {
246 231 netstack_rele(ns);
247 232 return (-1);
248 233 }
249 234 ekp = (ah_kstats_t *)kp->ks_data;
250 235
251 236 rw_enter(&ipss->ipsec_alg_lock, RW_READER);
252 237 ekp->ah_stat_num_aalgs.value.ui64 = ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
253 238 rw_exit(&ipss->ipsec_alg_lock);
254 239
255 240 netstack_rele(ns);
256 241 return (0);
257 242 }
258 243
259 244 /*
260 245 * Don't have to lock ipsec_age_interval, as only one thread will access it at
261 246 * a time, because I control the one function that does a qtimeout() on
262 247 * ah_pfkey_q.
263 248 */
264 249 static void
265 250 ah_ager(void *arg)
266 251 {
267 252 ipsecah_stack_t *ahstack = (ipsecah_stack_t *)arg;
268 253 netstack_t *ns = ahstack->ipsecah_netstack;
269 254 hrtime_t begin = gethrtime();
270 255
271 256 sadb_ager(&ahstack->ah_sadb.s_v4, ahstack->ah_pfkey_q,
272 257 ahstack->ipsecah_reap_delay, ns);
273 258 sadb_ager(&ahstack->ah_sadb.s_v6, ahstack->ah_pfkey_q,
274 259 ahstack->ipsecah_reap_delay, ns);
275 260
276 261 ahstack->ah_event = sadb_retimeout(begin, ahstack->ah_pfkey_q,
277 262 ah_ager, ahstack,
278 263 &ahstack->ipsecah_age_interval, ahstack->ipsecah_age_int_max,
279 264 info.mi_idnum);
280 265 }
281 266
282 267 /*
283 268 * Get an AH NDD parameter.
284 269 */
285 270 /* ARGSUSED */
286 271 static int
287 272 ipsecah_param_get(
288 273 queue_t *q,
289 274 mblk_t *mp,
290 275 caddr_t cp,
291 276 cred_t *cr)
292 277 {
293 278 ipsecahparam_t *ipsecahpa = (ipsecahparam_t *)cp;
294 279 uint_t value;
295 280 ipsecah_stack_t *ahstack = (ipsecah_stack_t *)q->q_ptr;
296 281
297 282 mutex_enter(&ahstack->ipsecah_param_lock);
298 283 value = ipsecahpa->ipsecah_param_value;
299 284 mutex_exit(&ahstack->ipsecah_param_lock);
300 285
301 286 (void) mi_mpprintf(mp, "%u", value);
302 287 return (0);
303 288 }
304 289
305 290 /*
306 291 * This routine sets an NDD variable in a ipsecahparam_t structure.
307 292 */
308 293 /* ARGSUSED */
309 294 static int
310 295 ipsecah_param_set(
311 296 queue_t *q,
312 297 mblk_t *mp,
313 298 char *value,
314 299 caddr_t cp,
315 300 cred_t *cr)
316 301 {
317 302 ulong_t new_value;
318 303 ipsecahparam_t *ipsecahpa = (ipsecahparam_t *)cp;
319 304 ipsecah_stack_t *ahstack = (ipsecah_stack_t *)q->q_ptr;
320 305
321 306 /*
322 307 * Fail the request if the new value does not lie within the
323 308 * required bounds.
324 309 */
325 310 if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
326 311 new_value < ipsecahpa->ipsecah_param_min ||
327 312 new_value > ipsecahpa->ipsecah_param_max) {
328 313 return (EINVAL);
329 314 }
330 315
331 316 /* Set the new value */
332 317 mutex_enter(&ahstack->ipsecah_param_lock);
333 318 ipsecahpa->ipsecah_param_value = new_value;
334 319 mutex_exit(&ahstack->ipsecah_param_lock);
335 320 return (0);
336 321 }
337 322
338 323 /*
339 324 * Using lifetime NDD variables, fill in an extended combination's
340 325 * lifetime information.
341 326 */
342 327 void
343 328 ipsecah_fill_defs(sadb_x_ecomb_t *ecomb, netstack_t *ns)
344 329 {
345 330 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
346 331
347 332 ecomb->sadb_x_ecomb_soft_bytes = ahstack->ipsecah_default_soft_bytes;
348 333 ecomb->sadb_x_ecomb_hard_bytes = ahstack->ipsecah_default_hard_bytes;
349 334 ecomb->sadb_x_ecomb_soft_addtime =
350 335 ahstack->ipsecah_default_soft_addtime;
351 336 ecomb->sadb_x_ecomb_hard_addtime =
352 337 ahstack->ipsecah_default_hard_addtime;
353 338 ecomb->sadb_x_ecomb_soft_usetime =
354 339 ahstack->ipsecah_default_soft_usetime;
355 340 ecomb->sadb_x_ecomb_hard_usetime =
356 341 ahstack->ipsecah_default_hard_usetime;
357 342 }
358 343
359 344 /*
360 345 * Initialize things for AH at module load time.
361 346 */
362 347 boolean_t
363 348 ipsecah_ddi_init(void)
364 349 {
365 350 ah_taskq = taskq_create("ah_taskq", 1, minclsyspri,
366 351 IPSEC_TASKQ_MIN, IPSEC_TASKQ_MAX, 0);
367 352
368 353 /*
369 354 * We want to be informed each time a stack is created or
370 355 * destroyed in the kernel, so we can maintain the
371 356 * set of ipsecah_stack_t's.
372 357 */
373 358 netstack_register(NS_IPSECAH, ipsecah_stack_init, NULL,
374 359 ipsecah_stack_fini);
375 360
376 361 return (B_TRUE);
377 362 }
378 363
379 364 /*
380 365 * Walk through the param array specified registering each element with the
381 366 * named dispatch handler.
382 367 */
383 368 static boolean_t
384 369 ipsecah_param_register(IDP *ndp, ipsecahparam_t *ahp, int cnt)
385 370 {
386 371 for (; cnt-- > 0; ahp++) {
387 372 if (ahp->ipsecah_param_name != NULL &&
388 373 ahp->ipsecah_param_name[0]) {
389 374 if (!nd_load(ndp,
390 375 ahp->ipsecah_param_name,
391 376 ipsecah_param_get, ipsecah_param_set,
392 377 (caddr_t)ahp)) {
393 378 nd_free(ndp);
394 379 return (B_FALSE);
395 380 }
396 381 }
397 382 }
398 383 return (B_TRUE);
399 384 }
400 385
401 386 /*
402 387 * Initialize things for AH for each stack instance
403 388 */
404 389 static void *
405 390 ipsecah_stack_init(netstackid_t stackid, netstack_t *ns)
406 391 {
407 392 ipsecah_stack_t *ahstack;
408 393 ipsecahparam_t *ahp;
409 394
410 395 ahstack = (ipsecah_stack_t *)kmem_zalloc(sizeof (*ahstack), KM_SLEEP);
411 396 ahstack->ipsecah_netstack = ns;
412 397
|
↓ open down ↓ |
250 lines elided |
↑ open up ↑ |
413 398 ahp = (ipsecahparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
414 399 ahstack->ipsecah_params = ahp;
415 400 bcopy(lcl_param_arr, ahp, sizeof (lcl_param_arr));
416 401
417 402 (void) ipsecah_param_register(&ahstack->ipsecah_g_nd, ahp,
418 403 A_CNT(lcl_param_arr));
419 404
420 405 (void) ah_kstat_init(ahstack, stackid);
421 406
422 407 ahstack->ah_sadb.s_acquire_timeout = &ahstack->ipsecah_acquire_timeout;
423 - ahstack->ah_sadb.s_acqfn = ah_send_acquire;
424 408 sadbp_init("AH", &ahstack->ah_sadb, SADB_SATYPE_AH, ah_hash_size,
425 409 ahstack->ipsecah_netstack);
426 410
427 411 mutex_init(&ahstack->ipsecah_param_lock, NULL, MUTEX_DEFAULT, 0);
428 412
429 413 ip_drop_register(&ahstack->ah_dropper, "IPsec AH");
430 414 return (ahstack);
431 415 }
432 416
433 417 /*
434 418 * Destroy things for AH at module unload time.
435 419 */
436 420 void
437 421 ipsecah_ddi_destroy(void)
438 422 {
439 423 netstack_unregister(NS_IPSECAH);
440 424 taskq_destroy(ah_taskq);
441 425 }
442 426
443 427 /*
|
↓ open down ↓ |
10 lines elided |
↑ open up ↑ |
444 428 * Destroy things for AH for one stack... Never called?
445 429 */
446 430 static void
447 431 ipsecah_stack_fini(netstackid_t stackid, void *arg)
448 432 {
449 433 ipsecah_stack_t *ahstack = (ipsecah_stack_t *)arg;
450 434
451 435 if (ahstack->ah_pfkey_q != NULL) {
452 436 (void) quntimeout(ahstack->ah_pfkey_q, ahstack->ah_event);
453 437 }
454 - ahstack->ah_sadb.s_acqfn = NULL;
455 438 ahstack->ah_sadb.s_acquire_timeout = NULL;
456 439 sadbp_destroy(&ahstack->ah_sadb, ahstack->ipsecah_netstack);
457 440 ip_drop_unregister(&ahstack->ah_dropper);
458 441 mutex_destroy(&ahstack->ipsecah_param_lock);
459 442 nd_free(&ahstack->ipsecah_g_nd);
460 443
461 444 kmem_free(ahstack->ipsecah_params, sizeof (lcl_param_arr));
462 445 ahstack->ipsecah_params = NULL;
463 446 kstat_delete_netstack(ahstack->ah_ksp, stackid);
464 447 ahstack->ah_ksp = NULL;
465 448 ahstack->ah_kstats = NULL;
466 449
467 450 kmem_free(ahstack, sizeof (*ahstack));
468 451 }
469 452
470 453 /*
471 454 * AH module open routine, which is here for keysock plumbing.
472 455 * Keysock is pushed over {AH,ESP} which is an artifact from the Bad Old
473 456 * Days of export control, and fears that ESP would not be allowed
474 457 * to be shipped at all by default. Eventually, keysock should
475 458 * either access AH and ESP via modstubs or krtld dependencies, or
476 459 * perhaps be folded in with AH and ESP into a single IPsec/netsec
477 460 * module ("netsec" if PF_KEY provides more than AH/ESP keying tables).
478 461 */
479 462 /* ARGSUSED */
480 463 static int
481 464 ipsecah_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
482 465 {
483 466 netstack_t *ns;
484 467 ipsecah_stack_t *ahstack;
485 468
486 469 if (secpolicy_ip_config(credp, B_FALSE) != 0)
487 470 return (EPERM);
488 471
489 472 if (q->q_ptr != NULL)
490 473 return (0); /* Re-open of an already open instance. */
491 474
492 475 if (sflag != MODOPEN)
493 476 return (EINVAL);
494 477
495 478 ns = netstack_find_by_cred(credp);
496 479 ASSERT(ns != NULL);
497 480 ahstack = ns->netstack_ipsecah;
498 481 ASSERT(ahstack != NULL);
499 482
500 483 q->q_ptr = ahstack;
501 484 WR(q)->q_ptr = q->q_ptr;
502 485
503 486 qprocson(q);
504 487 return (0);
505 488 }
506 489
507 490 /*
508 491 * AH module close routine.
509 492 */
510 493 static int
511 494 ipsecah_close(queue_t *q)
512 495 {
513 496 ipsecah_stack_t *ahstack = (ipsecah_stack_t *)q->q_ptr;
514 497
515 498 /*
516 499 * Clean up q_ptr, if needed.
517 500 */
518 501 qprocsoff(q);
519 502
520 503 /* Keysock queue check is safe, because of OCEXCL perimeter. */
521 504
522 505 if (q == ahstack->ah_pfkey_q) {
523 506 ah1dbg(ahstack,
524 507 ("ipsecah_close: Ummm... keysock is closing AH.\n"));
525 508 ahstack->ah_pfkey_q = NULL;
526 509 /* Detach qtimeouts. */
527 510 (void) quntimeout(q, ahstack->ah_event);
528 511 }
529 512
530 513 netstack_rele(ahstack->ipsecah_netstack);
531 514 return (0);
532 515 }
533 516
534 517 /*
535 518 * Construct an SADB_REGISTER message with the current algorithms.
536 519 */
537 520 static boolean_t
538 521 ah_register_out(uint32_t sequence, uint32_t pid, uint_t serial,
539 522 ipsecah_stack_t *ahstack, cred_t *cr)
540 523 {
541 524 mblk_t *mp;
542 525 boolean_t rc = B_TRUE;
543 526 sadb_msg_t *samsg;
544 527 sadb_supported_t *sasupp;
545 528 sadb_alg_t *saalg;
546 529 uint_t allocsize = sizeof (*samsg);
547 530 uint_t i, numalgs_snap;
548 531 ipsec_alginfo_t **authalgs;
549 532 uint_t num_aalgs;
550 533 ipsec_stack_t *ipss = ahstack->ipsecah_netstack->netstack_ipsec;
551 534 sadb_sens_t *sens;
552 535 size_t sens_len = 0;
553 536 sadb_ext_t *nextext;
554 537 ts_label_t *sens_tsl = NULL;
555 538
556 539 /* Allocate the KEYSOCK_OUT. */
557 540 mp = sadb_keysock_out(serial);
558 541 if (mp == NULL) {
559 542 ah0dbg(("ah_register_out: couldn't allocate mblk.\n"));
560 543 return (B_FALSE);
561 544 }
562 545
563 546 if (is_system_labeled() && (cr != NULL)) {
564 547 sens_tsl = crgetlabel(cr);
565 548 if (sens_tsl != NULL) {
566 549 sens_len = sadb_sens_len_from_label(sens_tsl);
567 550 allocsize += sens_len;
568 551 }
569 552 }
570 553
571 554 /*
572 555 * Allocate the PF_KEY message that follows KEYSOCK_OUT.
573 556 * The alg reader lock needs to be held while allocating
574 557 * the variable part (i.e. the algorithms) of the message.
575 558 */
576 559
577 560 rw_enter(&ipss->ipsec_alg_lock, RW_READER);
578 561
579 562 /*
580 563 * Return only valid algorithms, so the number of algorithms
581 564 * to send up may be less than the number of algorithm entries
582 565 * in the table.
583 566 */
584 567 authalgs = ipss->ipsec_alglists[IPSEC_ALG_AUTH];
585 568 for (num_aalgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
586 569 if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
587 570 num_aalgs++;
588 571
589 572 /*
590 573 * Fill SADB_REGISTER message's algorithm descriptors. Hold
591 574 * down the lock while filling it.
592 575 */
593 576 if (num_aalgs != 0) {
594 577 allocsize += (num_aalgs * sizeof (*saalg));
595 578 allocsize += sizeof (*sasupp);
596 579 }
597 580 mp->b_cont = allocb(allocsize, BPRI_HI);
598 581 if (mp->b_cont == NULL) {
599 582 rw_exit(&ipss->ipsec_alg_lock);
600 583 freemsg(mp);
601 584 return (B_FALSE);
602 585 }
603 586
604 587 mp->b_cont->b_wptr += allocsize;
605 588 nextext = (sadb_ext_t *)(mp->b_cont->b_rptr + sizeof (*samsg));
606 589
607 590 if (num_aalgs != 0) {
608 591
609 592 saalg = (sadb_alg_t *)(((uint8_t *)nextext) + sizeof (*sasupp));
610 593 ASSERT(((ulong_t)saalg & 0x7) == 0);
611 594
612 595 numalgs_snap = 0;
613 596 for (i = 0;
614 597 ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_aalgs));
615 598 i++) {
616 599 if (authalgs[i] == NULL || !ALG_VALID(authalgs[i]))
617 600 continue;
618 601
619 602 saalg->sadb_alg_id = authalgs[i]->alg_id;
620 603 saalg->sadb_alg_ivlen = 0;
621 604 saalg->sadb_alg_minbits = authalgs[i]->alg_ef_minbits;
622 605 saalg->sadb_alg_maxbits = authalgs[i]->alg_ef_maxbits;
623 606 saalg->sadb_x_alg_increment =
624 607 authalgs[i]->alg_increment;
625 608 /* For now, salt is meaningless in AH. */
626 609 ASSERT(authalgs[i]->alg_saltlen == 0);
627 610 saalg->sadb_x_alg_saltbits =
628 611 SADB_8TO1(authalgs[i]->alg_saltlen);
629 612 numalgs_snap++;
630 613 saalg++;
631 614 }
632 615 ASSERT(numalgs_snap == num_aalgs);
633 616 #ifdef DEBUG
634 617 /*
635 618 * Reality check to make sure I snagged all of the
636 619 * algorithms.
637 620 */
638 621 for (; i < IPSEC_MAX_ALGS; i++)
639 622 if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
640 623 cmn_err(CE_PANIC,
641 624 "ah_register_out()! Missed #%d.\n", i);
642 625 #endif /* DEBUG */
643 626 nextext = (sadb_ext_t *)saalg;
644 627 }
645 628
646 629 rw_exit(&ipss->ipsec_alg_lock);
647 630
648 631 if (sens_tsl != NULL) {
649 632 sens = (sadb_sens_t *)nextext;
650 633 sadb_sens_from_label(sens, SADB_EXT_SENSITIVITY,
651 634 sens_tsl, sens_len);
652 635
653 636 nextext = (sadb_ext_t *)(((uint8_t *)sens) + sens_len);
654 637 }
655 638
656 639 /* Now fill the restof the SADB_REGISTER message. */
657 640
658 641 samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
659 642 samsg->sadb_msg_version = PF_KEY_V2;
660 643 samsg->sadb_msg_type = SADB_REGISTER;
661 644 samsg->sadb_msg_errno = 0;
662 645 samsg->sadb_msg_satype = SADB_SATYPE_AH;
663 646 samsg->sadb_msg_len = SADB_8TO64(allocsize);
664 647 samsg->sadb_msg_reserved = 0;
665 648 /*
666 649 * Assume caller has sufficient sequence/pid number info. If it's one
667 650 * from me over a new alg., I could give two hoots about sequence.
668 651 */
669 652 samsg->sadb_msg_seq = sequence;
670 653 samsg->sadb_msg_pid = pid;
671 654
672 655 if (num_aalgs != 0) {
673 656 sasupp = (sadb_supported_t *)(samsg + 1);
674 657 sasupp->sadb_supported_len = SADB_8TO64(
675 658 sizeof (*sasupp) + sizeof (*saalg) * num_aalgs);
676 659 sasupp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
677 660 sasupp->sadb_supported_reserved = 0;
678 661 }
679 662
680 663 if (ahstack->ah_pfkey_q != NULL)
681 664 putnext(ahstack->ah_pfkey_q, mp);
682 665 else {
683 666 rc = B_FALSE;
684 667 freemsg(mp);
685 668 }
686 669
687 670 return (rc);
688 671 }
689 672
690 673 /*
691 674 * Invoked when the algorithm table changes. Causes SADB_REGISTER
692 675 * messages continaining the current list of algorithms to be
693 676 * sent up to the AH listeners.
694 677 */
695 678 void
696 679 ipsecah_algs_changed(netstack_t *ns)
697 680 {
698 681 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
699 682
700 683 /*
701 684 * Time to send a PF_KEY SADB_REGISTER message to AH listeners
702 685 * everywhere. (The function itself checks for NULL ah_pfkey_q.)
703 686 */
704 687 (void) ah_register_out(0, 0, 0, ahstack, NULL);
705 688 }
706 689
707 690 /*
708 691 * Stub function that taskq_dispatch() invokes to take the mblk (in arg)
709 692 * and send it into AH and IP again.
710 693 */
711 694 static void
712 695 inbound_task(void *arg)
713 696 {
714 697 mblk_t *mp = (mblk_t *)arg;
715 698 mblk_t *async_mp;
716 699 ip_recv_attr_t iras;
717 700
718 701 async_mp = mp;
719 702 mp = async_mp->b_cont;
720 703 async_mp->b_cont = NULL;
721 704 if (!ip_recv_attr_from_mblk(async_mp, &iras)) {
722 705 /* The ill or ip_stack_t disappeared on us */
723 706 ip_drop_input("ip_recv_attr_from_mblk", mp, NULL);
724 707 freemsg(mp);
725 708 goto done;
726 709 }
727 710
728 711 ah_inbound_restart(mp, &iras);
729 712 done:
730 713 ira_cleanup(&iras, B_TRUE);
731 714 }
732 715
733 716 /*
734 717 * Restart ESP after the SA has been added.
735 718 */
736 719 static void
737 720 ah_inbound_restart(mblk_t *mp, ip_recv_attr_t *ira)
738 721 {
739 722 ah_t *ah;
740 723 netstack_t *ns;
741 724 ipsecah_stack_t *ahstack;
742 725
743 726 ns = ira->ira_ill->ill_ipst->ips_netstack;
744 727 ahstack = ns->netstack_ipsecah;
745 728
746 729 ASSERT(ahstack != NULL);
747 730 mp = ipsec_inbound_ah_sa(mp, ira, &ah);
748 731 if (mp == NULL)
749 732 return;
750 733
751 734 ASSERT(ah != NULL);
752 735 ASSERT(ira->ira_flags & IRAF_IPSEC_SECURE);
753 736 ASSERT(ira->ira_ipsec_ah_sa != NULL);
754 737
755 738 mp = ira->ira_ipsec_ah_sa->ipsa_input_func(mp, ah, ira);
756 739 if (mp == NULL) {
757 740 /*
758 741 * Either it failed or is pending. In the former case
759 742 * ipIfStatsInDiscards was increased.
760 743 */
761 744 return;
762 745 }
763 746 ip_input_post_ipsec(mp, ira);
764 747 }
765 748
766 749 /*
767 750 * Now that weak-key passed, actually ADD the security association, and
768 751 * send back a reply ADD message.
769 752 */
770 753 static int
771 754 ah_add_sa_finish(mblk_t *mp, sadb_msg_t *samsg, keysock_in_t *ksi,
772 755 int *diagnostic, ipsecah_stack_t *ahstack)
773 756 {
774 757 isaf_t *primary = NULL, *secondary;
775 758 boolean_t clone = B_FALSE, is_inbound = B_FALSE;
776 759 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
777 760 ipsa_t *larval;
778 761 ipsacq_t *acqrec;
779 762 iacqf_t *acq_bucket;
780 763 mblk_t *acq_msgs = NULL;
781 764 mblk_t *lpkt;
782 765 int rc;
783 766 ipsa_query_t sq;
784 767 int error;
785 768 netstack_t *ns = ahstack->ipsecah_netstack;
786 769 ipsec_stack_t *ipss = ns->netstack_ipsec;
787 770
788 771 /*
789 772 * Locate the appropriate table(s).
790 773 */
791 774
792 775 sq.spp = &ahstack->ah_sadb;
793 776 error = sadb_form_query(ksi, IPSA_Q_SA|IPSA_Q_DST,
794 777 IPSA_Q_SA|IPSA_Q_DST|IPSA_Q_INBOUND|IPSA_Q_OUTBOUND,
795 778 &sq, diagnostic);
796 779 if (error)
797 780 return (error);
798 781
799 782 /*
800 783 * Use the direction flags provided by the KMD to determine
801 784 * if the inbound or outbound table should be the primary
802 785 * for this SA. If these flags were absent then make this
803 786 * decision based on the addresses.
804 787 */
805 788 if (assoc->sadb_sa_flags & IPSA_F_INBOUND) {
806 789 primary = sq.inbound;
807 790 secondary = sq.outbound;
808 791 is_inbound = B_TRUE;
809 792 if (assoc->sadb_sa_flags & IPSA_F_OUTBOUND)
810 793 clone = B_TRUE;
811 794 } else {
812 795 if (assoc->sadb_sa_flags & IPSA_F_OUTBOUND) {
813 796 primary = sq.outbound;
814 797 secondary = sq.inbound;
815 798 }
816 799 }
817 800 if (primary == NULL) {
818 801 /*
819 802 * The KMD did not set a direction flag, determine which
820 803 * table to insert the SA into based on addresses.
821 804 */
822 805 switch (ksi->ks_in_dsttype) {
823 806 case KS_IN_ADDR_MBCAST:
824 807 clone = B_TRUE; /* All mcast SAs can be bidirectional */
825 808 assoc->sadb_sa_flags |= IPSA_F_OUTBOUND;
826 809 /* FALLTHRU */
827 810 /*
828 811 * If the source address is either one of mine, or unspecified
829 812 * (which is best summed up by saying "not 'not mine'"),
830 813 * then the association is potentially bi-directional,
831 814 * in that it can be used for inbound traffic and outbound
832 815 * traffic. The best example of such and SA is a multicast
833 816 * SA (which allows me to receive the outbound traffic).
834 817 */
835 818 case KS_IN_ADDR_ME:
836 819 assoc->sadb_sa_flags |= IPSA_F_INBOUND;
837 820 primary = sq.inbound;
838 821 secondary = sq.outbound;
839 822 if (ksi->ks_in_srctype != KS_IN_ADDR_NOTME)
840 823 clone = B_TRUE;
841 824 is_inbound = B_TRUE;
842 825 break;
843 826
844 827 /*
845 828 * If the source address literally not mine (either
846 829 * unspecified or not mine), then this SA may have an
847 830 * address that WILL be mine after some configuration.
848 831 * We pay the price for this by making it a bi-directional
849 832 * SA.
850 833 */
851 834 case KS_IN_ADDR_NOTME:
852 835 assoc->sadb_sa_flags |= IPSA_F_OUTBOUND;
853 836 primary = sq.outbound;
854 837 secondary = sq.inbound;
855 838 if (ksi->ks_in_srctype != KS_IN_ADDR_ME) {
856 839 assoc->sadb_sa_flags |= IPSA_F_INBOUND;
857 840 clone = B_TRUE;
858 841 }
859 842 break;
860 843 default:
861 844 *diagnostic = SADB_X_DIAGNOSTIC_BAD_DST;
862 845 return (EINVAL);
863 846 }
864 847 }
865 848
866 849 /*
867 850 * Find a ACQUIRE list entry if possible. If we've added an SA that
868 851 * suits the needs of an ACQUIRE list entry, we can eliminate the
869 852 * ACQUIRE list entry and transmit the enqueued packets. Use the
870 853 * high-bit of the sequence number to queue it. Key off destination
871 854 * addr, and change acqrec's state.
872 855 */
873 856
874 857 if (samsg->sadb_msg_seq & IACQF_LOWEST_SEQ) {
875 858 acq_bucket = &(sq.sp->sdb_acq[sq.outhash]);
876 859 mutex_enter(&acq_bucket->iacqf_lock);
877 860 for (acqrec = acq_bucket->iacqf_ipsacq; acqrec != NULL;
878 861 acqrec = acqrec->ipsacq_next) {
879 862 mutex_enter(&acqrec->ipsacq_lock);
880 863 /*
881 864 * Q: I only check sequence. Should I check dst?
882 865 * A: Yes, check dest because those are the packets
883 866 * that are queued up.
884 867 */
885 868 if (acqrec->ipsacq_seq == samsg->sadb_msg_seq &&
886 869 IPSA_ARE_ADDR_EQUAL(sq.dstaddr,
887 870 acqrec->ipsacq_dstaddr, acqrec->ipsacq_addrfam))
888 871 break;
889 872 mutex_exit(&acqrec->ipsacq_lock);
890 873 }
891 874 if (acqrec != NULL) {
892 875 /*
893 876 * AHA! I found an ACQUIRE record for this SA.
894 877 * Grab the msg list, and free the acquire record.
895 878 * I already am holding the lock for this record,
896 879 * so all I have to do is free it.
897 880 */
898 881 acq_msgs = acqrec->ipsacq_mp;
899 882 acqrec->ipsacq_mp = NULL;
900 883 mutex_exit(&acqrec->ipsacq_lock);
901 884 sadb_destroy_acquire(acqrec, ns);
902 885 }
903 886 mutex_exit(&acq_bucket->iacqf_lock);
904 887 }
905 888
906 889 /*
907 890 * Find PF_KEY message, and see if I'm an update. If so, find entry
908 891 * in larval list (if there).
909 892 */
910 893
911 894 larval = NULL;
912 895
913 896 if (samsg->sadb_msg_type == SADB_UPDATE) {
914 897 mutex_enter(&sq.inbound->isaf_lock);
915 898 larval = ipsec_getassocbyspi(sq.inbound, sq.assoc->sadb_sa_spi,
916 899 ALL_ZEROES_PTR, sq.dstaddr, sq.dst->sin_family);
917 900 mutex_exit(&sq.inbound->isaf_lock);
918 901
919 902 if ((larval == NULL) ||
920 903 (larval->ipsa_state != IPSA_STATE_LARVAL)) {
921 904 *diagnostic = SADB_X_DIAGNOSTIC_SA_NOTFOUND;
922 905 if (larval != NULL) {
923 906 IPSA_REFRELE(larval);
924 907 }
925 908 ah0dbg(("Larval update, but larval disappeared.\n"));
926 909 return (ESRCH);
927 910 } /* Else sadb_common_add unlinks it for me! */
928 911 }
929 912
930 913 if (larval != NULL) {
931 914 /*
932 915 * Hold again, because sadb_common_add() consumes a reference,
933 916 * and we don't want to clear_lpkt() without a reference.
934 917 */
935 918 IPSA_REFHOLD(larval);
936 919 }
937 920
938 921 rc = sadb_common_add(ahstack->ah_pfkey_q, mp,
939 922 samsg, ksi, primary, secondary, larval, clone, is_inbound,
940 923 diagnostic, ns, &ahstack->ah_sadb);
941 924
942 925 if (larval != NULL) {
943 926 if (rc == 0) {
944 927 lpkt = sadb_clear_lpkt(larval);
945 928 if (lpkt != NULL) {
946 929 rc = !taskq_dispatch(ah_taskq, inbound_task,
947 930 lpkt, TQ_NOSLEEP);
948 931 }
949 932 }
950 933 IPSA_REFRELE(larval);
951 934 }
952 935
953 936 /*
954 937 * How much more stack will I create with all of these
955 938 * ah_outbound_*() calls?
956 939 */
957 940
958 941 /* Handle the packets queued waiting for the SA */
959 942 while (acq_msgs != NULL) {
960 943 mblk_t *asyncmp;
961 944 mblk_t *data_mp;
962 945 ip_xmit_attr_t ixas;
963 946 ill_t *ill;
964 947
965 948 asyncmp = acq_msgs;
966 949 acq_msgs = acq_msgs->b_next;
967 950 asyncmp->b_next = NULL;
968 951
969 952 /*
970 953 * Extract the ip_xmit_attr_t from the first mblk.
971 954 * Verifies that the netstack and ill is still around; could
972 955 * have vanished while iked was doing its work.
973 956 * On succesful return we have a nce_t and the ill/ipst can't
974 957 * disappear until we do the nce_refrele in ixa_cleanup.
975 958 */
976 959 data_mp = asyncmp->b_cont;
977 960 asyncmp->b_cont = NULL;
978 961 if (!ip_xmit_attr_from_mblk(asyncmp, &ixas)) {
979 962 AH_BUMP_STAT(ahstack, out_discards);
980 963 ip_drop_packet(data_mp, B_FALSE, NULL,
981 964 DROPPER(ipss, ipds_sadb_acquire_timeout),
982 965 &ahstack->ah_dropper);
983 966 } else if (rc != 0) {
984 967 ill = ixas.ixa_nce->nce_ill;
985 968 AH_BUMP_STAT(ahstack, out_discards);
986 969 ip_drop_packet(data_mp, B_FALSE, ill,
987 970 DROPPER(ipss, ipds_sadb_acquire_timeout),
988 971 &ahstack->ah_dropper);
989 972 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
990 973 } else {
991 974 ah_outbound_finish(data_mp, &ixas);
992 975 }
993 976 ixa_cleanup(&ixas);
994 977 }
995 978
996 979 return (rc);
997 980 }
998 981
999 982
1000 983 /*
1001 984 * Process one of the queued messages (from ipsacq_mp) once the SA
1002 985 * has been added.
1003 986 */
1004 987 static void
1005 988 ah_outbound_finish(mblk_t *data_mp, ip_xmit_attr_t *ixa)
1006 989 {
1007 990 netstack_t *ns = ixa->ixa_ipst->ips_netstack;
1008 991 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
1009 992 ipsec_stack_t *ipss = ns->netstack_ipsec;
1010 993 ill_t *ill = ixa->ixa_nce->nce_ill;
1011 994
1012 995 if (!ipsec_outbound_sa(data_mp, ixa, IPPROTO_AH)) {
1013 996 AH_BUMP_STAT(ahstack, out_discards);
1014 997 ip_drop_packet(data_mp, B_FALSE, ill,
1015 998 DROPPER(ipss, ipds_sadb_acquire_timeout),
1016 999 &ahstack->ah_dropper);
1017 1000 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
1018 1001 return;
1019 1002 }
1020 1003
1021 1004 data_mp = ah_outbound(data_mp, ixa);
1022 1005 if (data_mp == NULL)
1023 1006 return;
1024 1007
1025 1008 (void) ip_output_post_ipsec(data_mp, ixa);
1026 1009 }
1027 1010
1028 1011 /*
1029 1012 * Add new AH security association. This may become a generic AH/ESP
1030 1013 * routine eventually.
1031 1014 */
1032 1015 static int
1033 1016 ah_add_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic, netstack_t *ns)
1034 1017 {
1035 1018 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
1036 1019 sadb_address_t *srcext =
1037 1020 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
1038 1021 sadb_address_t *dstext =
1039 1022 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
1040 1023 sadb_address_t *isrcext =
1041 1024 (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_SRC];
1042 1025 sadb_address_t *idstext =
1043 1026 (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_DST];
1044 1027 sadb_key_t *key = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH];
1045 1028 struct sockaddr_in *src, *dst;
1046 1029 /* We don't need sockaddr_in6 for now. */
1047 1030 sadb_lifetime_t *soft =
1048 1031 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT];
1049 1032 sadb_lifetime_t *hard =
1050 1033 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD];
1051 1034 sadb_lifetime_t *idle =
1052 1035 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_X_EXT_LIFETIME_IDLE];
1053 1036 ipsec_alginfo_t *aalg;
1054 1037 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
1055 1038 ipsec_stack_t *ipss = ns->netstack_ipsec;
1056 1039
1057 1040 /* I need certain extensions present for an ADD message. */
1058 1041 if (srcext == NULL) {
1059 1042 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC;
1060 1043 return (EINVAL);
1061 1044 }
1062 1045 if (dstext == NULL) {
1063 1046 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
1064 1047 return (EINVAL);
1065 1048 }
1066 1049 if (isrcext == NULL && idstext != NULL) {
1067 1050 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_SRC;
1068 1051 return (EINVAL);
1069 1052 }
1070 1053 if (isrcext != NULL && idstext == NULL) {
1071 1054 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_DST;
1072 1055 return (EINVAL);
1073 1056 }
1074 1057 if (assoc == NULL) {
1075 1058 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
1076 1059 return (EINVAL);
1077 1060 }
1078 1061 if (key == NULL) {
1079 1062 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_AKEY;
1080 1063 return (EINVAL);
1081 1064 }
1082 1065
1083 1066 src = (struct sockaddr_in *)(srcext + 1);
1084 1067 dst = (struct sockaddr_in *)(dstext + 1);
1085 1068
1086 1069 /* Sundry ADD-specific reality checks. */
1087 1070 /* XXX STATS : Logging/stats here? */
1088 1071
1089 1072 if ((assoc->sadb_sa_state != SADB_SASTATE_MATURE) &&
1090 1073 (assoc->sadb_sa_state != SADB_X_SASTATE_ACTIVE_ELSEWHERE)) {
1091 1074 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
1092 1075 return (EINVAL);
1093 1076 }
1094 1077 if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
1095 1078 *diagnostic = SADB_X_DIAGNOSTIC_ENCR_NOTSUPP;
1096 1079 return (EINVAL);
1097 1080 }
1098 1081 if (assoc->sadb_sa_flags & ~ahstack->ah_sadb.s_addflags) {
1099 1082 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS;
1100 1083 return (EINVAL);
1101 1084 }
1102 1085 if ((*diagnostic = sadb_hardsoftchk(hard, soft, idle)) != 0)
1103 1086 return (EINVAL);
1104 1087
1105 1088 ASSERT(src->sin_family == dst->sin_family);
1106 1089
1107 1090 /* Stuff I don't support, for now. XXX Diagnostic? */
1108 1091 if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL)
1109 1092 return (EOPNOTSUPP);
1110 1093
1111 1094 if (ksi->ks_in_extv[SADB_EXT_SENSITIVITY] != NULL) {
1112 1095 if (!is_system_labeled())
1113 1096 return (EOPNOTSUPP);
1114 1097 }
1115 1098
1116 1099 if (ksi->ks_in_extv[SADB_X_EXT_OUTER_SENS] != NULL) {
1117 1100 if (!is_system_labeled())
1118 1101 return (EOPNOTSUPP);
1119 1102 }
1120 1103 /*
1121 1104 * XXX Policy : I'm not checking identities at this time, but
1122 1105 * if I did, I'd do them here, before I sent the weak key
1123 1106 * check up to the algorithm.
1124 1107 */
1125 1108
1126 1109 /* verify that there is a mapping for the specified algorithm */
1127 1110 rw_enter(&ipss->ipsec_alg_lock, RW_READER);
1128 1111 aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH][assoc->sadb_sa_auth];
1129 1112 if (aalg == NULL || !ALG_VALID(aalg)) {
1130 1113 rw_exit(&ipss->ipsec_alg_lock);
1131 1114 ah1dbg(ahstack, ("Couldn't find auth alg #%d.\n",
1132 1115 assoc->sadb_sa_auth));
1133 1116 *diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
1134 1117 return (EINVAL);
1135 1118 }
1136 1119 ASSERT(aalg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
1137 1120
1138 1121 /* sanity check key sizes */
1139 1122 if (!ipsec_valid_key_size(key->sadb_key_bits, aalg)) {
1140 1123 rw_exit(&ipss->ipsec_alg_lock);
1141 1124 *diagnostic = SADB_X_DIAGNOSTIC_BAD_AKEYBITS;
1142 1125 return (EINVAL);
1143 1126 }
1144 1127
1145 1128 /* check key and fix parity if needed */
1146 1129 if (ipsec_check_key(aalg->alg_mech_type, key, B_TRUE,
1147 1130 diagnostic) != 0) {
1148 1131 rw_exit(&ipss->ipsec_alg_lock);
1149 1132 return (EINVAL);
1150 1133 }
1151 1134
1152 1135 rw_exit(&ipss->ipsec_alg_lock);
1153 1136
1154 1137 return (ah_add_sa_finish(mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi,
1155 1138 diagnostic, ahstack));
1156 1139 }
1157 1140
1158 1141 /* Refactor me */
1159 1142 /*
1160 1143 * Update a security association. Updates come in two varieties. The first
1161 1144 * is an update of lifetimes on a non-larval SA. The second is an update of
1162 1145 * a larval SA, which ends up looking a lot more like an add.
1163 1146 */
1164 1147 static int
1165 1148 ah_update_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
1166 1149 ipsecah_stack_t *ahstack, uint8_t sadb_msg_type)
1167 1150 {
1168 1151 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
1169 1152 sadb_address_t *dstext =
1170 1153 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
1171 1154 mblk_t *buf_pkt;
1172 1155 int rcode;
1173 1156
1174 1157 if (dstext == NULL) {
1175 1158 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
1176 1159 return (EINVAL);
1177 1160 }
1178 1161
1179 1162 rcode = sadb_update_sa(mp, ksi, &buf_pkt, &ahstack->ah_sadb,
1180 1163 diagnostic, ahstack->ah_pfkey_q, ah_add_sa,
1181 1164 ahstack->ipsecah_netstack, sadb_msg_type);
1182 1165
1183 1166 if ((assoc->sadb_sa_state != SADB_X_SASTATE_ACTIVE) ||
1184 1167 (rcode != 0)) {
1185 1168 return (rcode);
1186 1169 }
1187 1170
1188 1171 HANDLE_BUF_PKT(ah_taskq, ahstack->ipsecah_netstack->netstack_ipsec,
1189 1172 ahstack->ah_dropper, buf_pkt);
1190 1173
1191 1174 return (rcode);
1192 1175 }
1193 1176
1194 1177 /* Refactor me */
1195 1178 /*
1196 1179 * Delete a security association. This is REALLY likely to be code common to
1197 1180 * both AH and ESP. Find the association, then unlink it.
1198 1181 */
1199 1182 static int
1200 1183 ah_del_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
1201 1184 ipsecah_stack_t *ahstack, uint8_t sadb_msg_type)
1202 1185 {
1203 1186 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
1204 1187 sadb_address_t *dstext =
1205 1188 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
1206 1189 sadb_address_t *srcext =
1207 1190 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
1208 1191 struct sockaddr_in *sin;
1209 1192
1210 1193 if (assoc == NULL) {
1211 1194 if (dstext != NULL)
1212 1195 sin = (struct sockaddr_in *)(dstext + 1);
1213 1196 else if (srcext != NULL)
1214 1197 sin = (struct sockaddr_in *)(srcext + 1);
1215 1198 else {
1216 1199 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
1217 1200 return (EINVAL);
1218 1201 }
1219 1202 return (sadb_purge_sa(mp, ksi,
1220 1203 (sin->sin_family == AF_INET6) ? &ahstack->ah_sadb.s_v6 :
1221 1204 &ahstack->ah_sadb.s_v4, diagnostic, ahstack->ah_pfkey_q));
1222 1205 }
1223 1206
1224 1207 return (sadb_delget_sa(mp, ksi, &ahstack->ah_sadb, diagnostic,
1225 1208 ahstack->ah_pfkey_q, sadb_msg_type));
1226 1209 }
1227 1210
1228 1211 /* Refactor me */
1229 1212 /*
1230 1213 * Convert the entire contents of all of AH's SA tables into PF_KEY SADB_DUMP
1231 1214 * messages.
1232 1215 */
1233 1216 static void
1234 1217 ah_dump(mblk_t *mp, keysock_in_t *ksi, ipsecah_stack_t *ahstack)
1235 1218 {
1236 1219 int error;
1237 1220 sadb_msg_t *samsg;
1238 1221
1239 1222 /*
1240 1223 * Dump each fanout, bailing if error is non-zero.
1241 1224 */
1242 1225
1243 1226 error = sadb_dump(ahstack->ah_pfkey_q, mp, ksi, &ahstack->ah_sadb.s_v4);
1244 1227 if (error != 0)
1245 1228 goto bail;
1246 1229
1247 1230 error = sadb_dump(ahstack->ah_pfkey_q, mp, ksi, &ahstack->ah_sadb.s_v6);
1248 1231 bail:
1249 1232 ASSERT(mp->b_cont != NULL);
1250 1233 samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
1251 1234 samsg->sadb_msg_errno = (uint8_t)error;
1252 1235 sadb_pfkey_echo(ahstack->ah_pfkey_q, mp,
1253 1236 (sadb_msg_t *)mp->b_cont->b_rptr, ksi, NULL);
1254 1237 }
1255 1238
1256 1239 /*
1257 1240 * First-cut reality check for an inbound PF_KEY message.
1258 1241 */
1259 1242 static boolean_t
1260 1243 ah_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi,
1261 1244 ipsecah_stack_t *ahstack)
1262 1245 {
1263 1246 int diagnostic;
1264 1247
1265 1248 if (mp->b_cont == NULL) {
1266 1249 freemsg(mp);
1267 1250 return (B_TRUE);
1268 1251 }
1269 1252
1270 1253 if (ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT] != NULL) {
1271 1254 diagnostic = SADB_X_DIAGNOSTIC_EKEY_PRESENT;
1272 1255 goto badmsg;
1273 1256 }
1274 1257 if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) {
1275 1258 diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT;
1276 1259 goto badmsg;
1277 1260 }
1278 1261 if (ksi->ks_in_extv[SADB_EXT_SUPPORTED_AUTH] != NULL ||
1279 1262 ksi->ks_in_extv[SADB_EXT_SUPPORTED_ENCRYPT] != NULL) {
1280 1263 diagnostic = SADB_X_DIAGNOSTIC_SUPP_PRESENT;
1281 1264 goto badmsg;
1282 1265 }
1283 1266 return (B_FALSE); /* False ==> no failures */
1284 1267
1285 1268 badmsg:
1286 1269 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL,
1287 1270 diagnostic, ksi->ks_in_serial);
1288 1271 return (B_TRUE); /* True ==> failures */
1289 1272 }
1290 1273
1291 1274 /*
1292 1275 * AH parsing of PF_KEY messages. Keysock did most of the really silly
1293 1276 * error cases. What I receive is a fully-formed, syntactically legal
1294 1277 * PF_KEY message. I then need to check semantics...
1295 1278 *
1296 1279 * This code may become common to AH and ESP. Stay tuned.
1297 1280 *
1298 1281 * I also make the assumption that db_ref's are cool. If this assumption
1299 1282 * is wrong, this means that someone other than keysock or me has been
1300 1283 * mucking with PF_KEY messages.
1301 1284 */
1302 1285 static void
1303 1286 ah_parse_pfkey(mblk_t *mp, ipsecah_stack_t *ahstack)
1304 1287 {
1305 1288 mblk_t *msg = mp->b_cont;
1306 1289 sadb_msg_t *samsg;
1307 1290 keysock_in_t *ksi;
1308 1291 int error;
1309 1292 int diagnostic = SADB_X_DIAGNOSTIC_NONE;
1310 1293
1311 1294 ASSERT(msg != NULL);
1312 1295
1313 1296 samsg = (sadb_msg_t *)msg->b_rptr;
1314 1297 ksi = (keysock_in_t *)mp->b_rptr;
1315 1298
1316 1299 /*
1317 1300 * If applicable, convert unspecified AF_INET6 to unspecified
1318 1301 * AF_INET.
1319 1302 */
1320 1303 if (!sadb_addrfix(ksi, ahstack->ah_pfkey_q, mp,
1321 1304 ahstack->ipsecah_netstack) ||
1322 1305 ah_pfkey_reality_failures(mp, ksi, ahstack)) {
1323 1306 return;
1324 1307 }
1325 1308
1326 1309 switch (samsg->sadb_msg_type) {
1327 1310 case SADB_ADD:
1328 1311 error = ah_add_sa(mp, ksi, &diagnostic,
1329 1312 ahstack->ipsecah_netstack);
1330 1313 if (error != 0) {
1331 1314 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1332 1315 diagnostic, ksi->ks_in_serial);
1333 1316 }
1334 1317 /* else ah_add_sa() took care of things. */
1335 1318 break;
1336 1319 case SADB_DELETE:
1337 1320 case SADB_X_DELPAIR:
1338 1321 case SADB_X_DELPAIR_STATE:
1339 1322 error = ah_del_sa(mp, ksi, &diagnostic, ahstack,
1340 1323 samsg->sadb_msg_type);
1341 1324 if (error != 0) {
1342 1325 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1343 1326 diagnostic, ksi->ks_in_serial);
1344 1327 }
1345 1328 /* Else ah_del_sa() took care of things. */
1346 1329 break;
1347 1330 case SADB_GET:
1348 1331 error = sadb_delget_sa(mp, ksi, &ahstack->ah_sadb, &diagnostic,
1349 1332 ahstack->ah_pfkey_q, samsg->sadb_msg_type);
1350 1333 if (error != 0) {
1351 1334 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1352 1335 diagnostic, ksi->ks_in_serial);
1353 1336 }
1354 1337 /* Else sadb_get_sa() took care of things. */
1355 1338 break;
1356 1339 case SADB_FLUSH:
1357 1340 sadbp_flush(&ahstack->ah_sadb, ahstack->ipsecah_netstack);
1358 1341 sadb_pfkey_echo(ahstack->ah_pfkey_q, mp, samsg, ksi, NULL);
1359 1342 break;
1360 1343 case SADB_REGISTER:
1361 1344 /*
1362 1345 * Hmmm, let's do it! Check for extensions (there should
1363 1346 * be none), extract the fields, call ah_register_out(),
1364 1347 * then either free or report an error.
1365 1348 *
1366 1349 * Keysock takes care of the PF_KEY bookkeeping for this.
1367 1350 */
1368 1351 if (ah_register_out(samsg->sadb_msg_seq, samsg->sadb_msg_pid,
1369 1352 ksi->ks_in_serial, ahstack, msg_getcred(mp, NULL))) {
1370 1353 freemsg(mp);
1371 1354 } else {
1372 1355 /*
1373 1356 * Only way this path hits is if there is a memory
1374 1357 * failure. It will not return B_FALSE because of
1375 1358 * lack of ah_pfkey_q if I am in wput().
1376 1359 */
1377 1360 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, ENOMEM,
1378 1361 diagnostic, ksi->ks_in_serial);
1379 1362 }
1380 1363 break;
1381 1364 case SADB_UPDATE:
1382 1365 case SADB_X_UPDATEPAIR:
1383 1366 /*
1384 1367 * Find a larval, if not there, find a full one and get
1385 1368 * strict.
1386 1369 */
1387 1370 error = ah_update_sa(mp, ksi, &diagnostic, ahstack,
1388 1371 samsg->sadb_msg_type);
1389 1372 if (error != 0) {
1390 1373 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1391 1374 diagnostic, ksi->ks_in_serial);
1392 1375 }
1393 1376 /* else ah_update_sa() took care of things. */
1394 1377 break;
1395 1378 case SADB_GETSPI:
1396 1379 /*
1397 1380 * Reserve a new larval entry.
1398 1381 */
1399 1382 ah_getspi(mp, ksi, ahstack);
1400 1383 break;
1401 1384 case SADB_ACQUIRE:
1402 1385 /*
1403 1386 * Find larval and/or ACQUIRE record and kill it (them), I'm
1404 1387 * most likely an error. Inbound ACQUIRE messages should only
1405 1388 * have the base header.
1406 1389 */
1407 1390 sadb_in_acquire(samsg, &ahstack->ah_sadb, ahstack->ah_pfkey_q,
1408 1391 ahstack->ipsecah_netstack);
1409 1392 freemsg(mp);
1410 1393 break;
1411 1394 case SADB_DUMP:
1412 1395 /*
1413 1396 * Dump all entries.
1414 1397 */
1415 1398 ah_dump(mp, ksi, ahstack);
1416 1399 /* ah_dump will take care of the return message, etc. */
1417 1400 break;
1418 1401 case SADB_EXPIRE:
1419 1402 /* Should never reach me. */
1420 1403 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EOPNOTSUPP,
1421 1404 diagnostic, ksi->ks_in_serial);
1422 1405 break;
1423 1406 default:
1424 1407 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL,
1425 1408 SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial);
1426 1409 break;
1427 1410 }
1428 1411 }
1429 1412
1430 1413 /*
1431 1414 * Handle case where PF_KEY says it can't find a keysock for one of my
1432 1415 * ACQUIRE messages.
1433 1416 */
1434 1417 static void
1435 1418 ah_keysock_no_socket(mblk_t *mp, ipsecah_stack_t *ahstack)
1436 1419 {
1437 1420 sadb_msg_t *samsg;
1438 1421 keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr;
1439 1422
1440 1423 if (mp->b_cont == NULL) {
1441 1424 freemsg(mp);
1442 1425 return;
1443 1426 }
1444 1427 samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
1445 1428
1446 1429 /*
1447 1430 * If keysock can't find any registered, delete the acquire record
1448 1431 * immediately, and handle errors.
1449 1432 */
1450 1433 if (samsg->sadb_msg_type == SADB_ACQUIRE) {
1451 1434 samsg->sadb_msg_errno = kse->ks_err_errno;
1452 1435 samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg));
1453 1436 /*
1454 1437 * Use the write-side of the ah_pfkey_q
1455 1438 */
1456 1439 sadb_in_acquire(samsg, &ahstack->ah_sadb,
1457 1440 WR(ahstack->ah_pfkey_q), ahstack->ipsecah_netstack);
1458 1441 }
1459 1442
1460 1443 freemsg(mp);
1461 1444 }
1462 1445
1463 1446 /*
1464 1447 * AH module write put routine.
1465 1448 */
1466 1449 static void
1467 1450 ipsecah_wput(queue_t *q, mblk_t *mp)
1468 1451 {
1469 1452 ipsec_info_t *ii;
1470 1453 struct iocblk *iocp;
1471 1454 ipsecah_stack_t *ahstack = (ipsecah_stack_t *)q->q_ptr;
1472 1455
1473 1456 ah3dbg(ahstack, ("In ah_wput().\n"));
1474 1457
1475 1458 /* NOTE: Each case must take care of freeing or passing mp. */
1476 1459 switch (mp->b_datap->db_type) {
1477 1460 case M_CTL:
1478 1461 if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) {
1479 1462 /* Not big enough message. */
1480 1463 freemsg(mp);
1481 1464 break;
1482 1465 }
1483 1466 ii = (ipsec_info_t *)mp->b_rptr;
1484 1467
1485 1468 switch (ii->ipsec_info_type) {
1486 1469 case KEYSOCK_OUT_ERR:
1487 1470 ah1dbg(ahstack, ("Got KEYSOCK_OUT_ERR message.\n"));
1488 1471 ah_keysock_no_socket(mp, ahstack);
1489 1472 break;
1490 1473 case KEYSOCK_IN:
1491 1474 AH_BUMP_STAT(ahstack, keysock_in);
1492 1475 ah3dbg(ahstack, ("Got KEYSOCK_IN message.\n"));
1493 1476
1494 1477 /* Parse the message. */
1495 1478 ah_parse_pfkey(mp, ahstack);
1496 1479 break;
1497 1480 case KEYSOCK_HELLO:
1498 1481 sadb_keysock_hello(&ahstack->ah_pfkey_q, q, mp,
1499 1482 ah_ager, (void *)ahstack, &ahstack->ah_event,
1500 1483 SADB_SATYPE_AH);
1501 1484 break;
1502 1485 default:
1503 1486 ah1dbg(ahstack, ("Got M_CTL from above of 0x%x.\n",
1504 1487 ii->ipsec_info_type));
1505 1488 freemsg(mp);
1506 1489 break;
1507 1490 }
1508 1491 break;
1509 1492 case M_IOCTL:
1510 1493 iocp = (struct iocblk *)mp->b_rptr;
1511 1494 switch (iocp->ioc_cmd) {
1512 1495 case ND_SET:
1513 1496 case ND_GET:
1514 1497 if (nd_getset(q, ahstack->ipsecah_g_nd, mp)) {
1515 1498 qreply(q, mp);
1516 1499 return;
1517 1500 } else {
1518 1501 iocp->ioc_error = ENOENT;
1519 1502 }
1520 1503 /* FALLTHRU */
1521 1504 default:
1522 1505 /* We really don't support any other ioctls, do we? */
1523 1506
1524 1507 /* Return EINVAL */
1525 1508 if (iocp->ioc_error != ENOENT)
1526 1509 iocp->ioc_error = EINVAL;
1527 1510 iocp->ioc_count = 0;
1528 1511 mp->b_datap->db_type = M_IOCACK;
1529 1512 qreply(q, mp);
1530 1513 return;
1531 1514 }
1532 1515 default:
1533 1516 ah3dbg(ahstack,
1534 1517 ("Got default message, type %d, passing to IP.\n",
1535 1518 mp->b_datap->db_type));
1536 1519 putnext(q, mp);
1537 1520 }
1538 1521 }
1539 1522
1540 1523 /* Refactor me */
1541 1524 /*
1542 1525 * Updating use times can be tricky business if the ipsa_haspeer flag is
1543 1526 * set. This function is called once in an SA's lifetime.
1544 1527 *
1545 1528 * Caller has to REFRELE "assoc" which is passed in. This function has
1546 1529 * to REFRELE any peer SA that is obtained.
1547 1530 */
1548 1531 static void
1549 1532 ah_set_usetime(ipsa_t *assoc, boolean_t inbound)
1550 1533 {
1551 1534 ipsa_t *inassoc, *outassoc;
1552 1535 isaf_t *bucket;
1553 1536 sadb_t *sp;
1554 1537 int outhash;
1555 1538 boolean_t isv6;
1556 1539 netstack_t *ns = assoc->ipsa_netstack;
1557 1540 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
1558 1541
1559 1542 /* No peer? No problem! */
1560 1543 if (!assoc->ipsa_haspeer) {
1561 1544 sadb_set_usetime(assoc);
1562 1545 return;
1563 1546 }
1564 1547
1565 1548 /*
1566 1549 * Otherwise, we want to grab both the original assoc and its peer.
1567 1550 * There might be a race for this, but if it's a real race, the times
1568 1551 * will be out-of-synch by at most a second, and since our time
1569 1552 * granularity is a second, this won't be a problem.
1570 1553 *
1571 1554 * If we need tight synchronization on the peer SA, then we need to
1572 1555 * reconsider.
1573 1556 */
1574 1557
1575 1558 /* Use address family to select IPv6/IPv4 */
1576 1559 isv6 = (assoc->ipsa_addrfam == AF_INET6);
1577 1560 if (isv6) {
1578 1561 sp = &ahstack->ah_sadb.s_v6;
1579 1562 } else {
1580 1563 sp = &ahstack->ah_sadb.s_v4;
1581 1564 ASSERT(assoc->ipsa_addrfam == AF_INET);
1582 1565 }
1583 1566 if (inbound) {
1584 1567 inassoc = assoc;
1585 1568 if (isv6)
1586 1569 outhash = OUTBOUND_HASH_V6(sp,
1587 1570 *((in6_addr_t *)&inassoc->ipsa_dstaddr));
1588 1571 else
1589 1572 outhash = OUTBOUND_HASH_V4(sp,
1590 1573 *((ipaddr_t *)&inassoc->ipsa_dstaddr));
1591 1574 bucket = &sp->sdb_of[outhash];
1592 1575
1593 1576 mutex_enter(&bucket->isaf_lock);
1594 1577 outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
1595 1578 inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
1596 1579 inassoc->ipsa_addrfam);
1597 1580 mutex_exit(&bucket->isaf_lock);
1598 1581 if (outassoc == NULL) {
1599 1582 /* Q: Do we wish to set haspeer == B_FALSE? */
1600 1583 ah0dbg(("ah_set_usetime: "
1601 1584 "can't find peer for inbound.\n"));
1602 1585 sadb_set_usetime(inassoc);
1603 1586 return;
1604 1587 }
1605 1588 } else {
1606 1589 outassoc = assoc;
1607 1590 bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
1608 1591 mutex_enter(&bucket->isaf_lock);
1609 1592 inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
1610 1593 outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
1611 1594 outassoc->ipsa_addrfam);
1612 1595 mutex_exit(&bucket->isaf_lock);
1613 1596 if (inassoc == NULL) {
1614 1597 /* Q: Do we wish to set haspeer == B_FALSE? */
1615 1598 ah0dbg(("ah_set_usetime: "
1616 1599 "can't find peer for outbound.\n"));
1617 1600 sadb_set_usetime(outassoc);
1618 1601 return;
1619 1602 }
1620 1603 }
1621 1604
1622 1605 /* Update usetime on both. */
1623 1606 sadb_set_usetime(inassoc);
1624 1607 sadb_set_usetime(outassoc);
1625 1608
1626 1609 /*
1627 1610 * REFRELE any peer SA.
1628 1611 *
1629 1612 * Because of the multi-line macro nature of IPSA_REFRELE, keep
1630 1613 * them in { }.
1631 1614 */
1632 1615 if (inbound) {
1633 1616 IPSA_REFRELE(outassoc);
1634 1617 } else {
1635 1618 IPSA_REFRELE(inassoc);
1636 1619 }
1637 1620 }
1638 1621
1639 1622 /* Refactor me */
1640 1623 /*
1641 1624 * Add a number of bytes to what the SA has protected so far. Return
1642 1625 * B_TRUE if the SA can still protect that many bytes.
1643 1626 *
1644 1627 * Caller must REFRELE the passed-in assoc. This function must REFRELE
1645 1628 * any obtained peer SA.
1646 1629 */
1647 1630 static boolean_t
1648 1631 ah_age_bytes(ipsa_t *assoc, uint64_t bytes, boolean_t inbound)
1649 1632 {
1650 1633 ipsa_t *inassoc, *outassoc;
1651 1634 isaf_t *bucket;
1652 1635 boolean_t inrc, outrc, isv6;
1653 1636 sadb_t *sp;
1654 1637 int outhash;
1655 1638 netstack_t *ns = assoc->ipsa_netstack;
1656 1639 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
1657 1640
1658 1641 /* No peer? No problem! */
1659 1642 if (!assoc->ipsa_haspeer) {
1660 1643 return (sadb_age_bytes(ahstack->ah_pfkey_q, assoc, bytes,
1661 1644 B_TRUE));
1662 1645 }
1663 1646
1664 1647 /*
1665 1648 * Otherwise, we want to grab both the original assoc and its peer.
1666 1649 * There might be a race for this, but if it's a real race, two
1667 1650 * expire messages may occur. We limit this by only sending the
1668 1651 * expire message on one of the peers, we'll pick the inbound
1669 1652 * arbitrarily.
1670 1653 *
1671 1654 * If we need tight synchronization on the peer SA, then we need to
1672 1655 * reconsider.
1673 1656 */
1674 1657
1675 1658 /* Pick v4/v6 bucket based on addrfam. */
1676 1659 isv6 = (assoc->ipsa_addrfam == AF_INET6);
1677 1660 if (isv6) {
1678 1661 sp = &ahstack->ah_sadb.s_v6;
1679 1662 } else {
1680 1663 sp = &ahstack->ah_sadb.s_v4;
1681 1664 ASSERT(assoc->ipsa_addrfam == AF_INET);
1682 1665 }
1683 1666 if (inbound) {
1684 1667 inassoc = assoc;
1685 1668 if (isv6)
1686 1669 outhash = OUTBOUND_HASH_V6(sp,
1687 1670 *((in6_addr_t *)&inassoc->ipsa_dstaddr));
1688 1671 else
1689 1672 outhash = OUTBOUND_HASH_V4(sp,
1690 1673 *((ipaddr_t *)&inassoc->ipsa_dstaddr));
1691 1674 bucket = &sp->sdb_of[outhash];
1692 1675 mutex_enter(&bucket->isaf_lock);
1693 1676 outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
1694 1677 inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
1695 1678 inassoc->ipsa_addrfam);
1696 1679 mutex_exit(&bucket->isaf_lock);
1697 1680 if (outassoc == NULL) {
1698 1681 /* Q: Do we wish to set haspeer == B_FALSE? */
1699 1682 ah0dbg(("ah_age_bytes: "
1700 1683 "can't find peer for inbound.\n"));
1701 1684 return (sadb_age_bytes(ahstack->ah_pfkey_q, inassoc,
1702 1685 bytes, B_TRUE));
1703 1686 }
1704 1687 } else {
1705 1688 outassoc = assoc;
1706 1689 bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
1707 1690 mutex_enter(&bucket->isaf_lock);
1708 1691 inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
1709 1692 outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
1710 1693 outassoc->ipsa_addrfam);
1711 1694 mutex_exit(&bucket->isaf_lock);
1712 1695 if (inassoc == NULL) {
1713 1696 /* Q: Do we wish to set haspeer == B_FALSE? */
1714 1697 ah0dbg(("ah_age_bytes: "
1715 1698 "can't find peer for outbound.\n"));
1716 1699 return (sadb_age_bytes(ahstack->ah_pfkey_q, outassoc,
1717 1700 bytes, B_TRUE));
1718 1701 }
1719 1702 }
1720 1703
1721 1704 inrc = sadb_age_bytes(ahstack->ah_pfkey_q, inassoc, bytes, B_TRUE);
1722 1705 outrc = sadb_age_bytes(ahstack->ah_pfkey_q, outassoc, bytes, B_FALSE);
1723 1706
1724 1707 /*
1725 1708 * REFRELE any peer SA.
1726 1709 *
1727 1710 * Because of the multi-line macro nature of IPSA_REFRELE, keep
1728 1711 * them in { }.
|
↓ open down ↓ |
1264 lines elided |
↑ open up ↑ |
1729 1712 */
1730 1713 if (inbound) {
1731 1714 IPSA_REFRELE(outassoc);
1732 1715 } else {
1733 1716 IPSA_REFRELE(inassoc);
1734 1717 }
1735 1718
1736 1719 return (inrc && outrc);
1737 1720 }
1738 1721
1739 -/*
1740 - * Perform the really difficult work of inserting the proposed situation.
1741 - * Called while holding the algorithm lock.
1742 - */
1743 -static void
1744 -ah_insert_prop(sadb_prop_t *prop, ipsacq_t *acqrec, uint_t combs,
1745 - netstack_t *ns)
1746 -{
1747 - sadb_comb_t *comb = (sadb_comb_t *)(prop + 1);
1748 - ipsec_action_t *ap;
1749 - ipsec_prot_t *prot;
1750 - ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
1751 - ipsec_stack_t *ipss = ns->netstack_ipsec;
1752 -
1753 - ASSERT(RW_READ_HELD(&ipss->ipsec_alg_lock));
1754 -
1755 - prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
1756 - prop->sadb_prop_len = SADB_8TO64(sizeof (sadb_prop_t));
1757 - *(uint32_t *)(&prop->sadb_prop_replay) = 0; /* Quick zero-out! */
1758 -
1759 - prop->sadb_prop_replay = ahstack->ipsecah_replay_size;
1760 -
1761 - /*
1762 - * Based upon algorithm properties, and what-not, prioritize a
1763 - * proposal, based on the ordering of the AH algorithms in the
1764 - * alternatives in the policy rule or socket that was placed
1765 - * in the acquire record.
1766 - */
1767 -
1768 - for (ap = acqrec->ipsacq_act; ap != NULL;
1769 - ap = ap->ipa_next) {
1770 - ipsec_alginfo_t *aalg;
1771 -
1772 - if ((ap->ipa_act.ipa_type != IPSEC_POLICY_APPLY) ||
1773 - (!ap->ipa_act.ipa_apply.ipp_use_ah))
1774 - continue;
1775 -
1776 - prot = &ap->ipa_act.ipa_apply;
1777 -
1778 - ASSERT(prot->ipp_auth_alg > 0);
1779 -
1780 - aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
1781 - [prot->ipp_auth_alg];
1782 - if (aalg == NULL || !ALG_VALID(aalg))
1783 - continue;
1784 -
1785 - /* XXX check aalg for duplicates??.. */
1786 -
1787 - comb->sadb_comb_flags = 0;
1788 - comb->sadb_comb_reserved = 0;
1789 - comb->sadb_comb_encrypt = 0;
1790 - comb->sadb_comb_encrypt_minbits = 0;
1791 - comb->sadb_comb_encrypt_maxbits = 0;
1792 -
1793 - comb->sadb_comb_auth = aalg->alg_id;
1794 - comb->sadb_comb_auth_minbits =
1795 - MAX(prot->ipp_ah_minbits, aalg->alg_ef_minbits);
1796 - comb->sadb_comb_auth_maxbits =
1797 - MIN(prot->ipp_ah_maxbits, aalg->alg_ef_maxbits);
1798 -
1799 - /*
1800 - * The following may be based on algorithm
1801 - * properties, but in the meantime, we just pick
1802 - * some good, sensible numbers. Key mgmt. can
1803 - * (and perhaps should) be the place to finalize
1804 - * such decisions.
1805 - */
1806 -
1807 - /*
1808 - * No limits on allocations, since we really don't
1809 - * support that concept currently.
1810 - */
1811 - comb->sadb_comb_soft_allocations = 0;
1812 - comb->sadb_comb_hard_allocations = 0;
1813 -
1814 - /*
1815 - * These may want to come from policy rule..
1816 - */
1817 - comb->sadb_comb_soft_bytes =
1818 - ahstack->ipsecah_default_soft_bytes;
1819 - comb->sadb_comb_hard_bytes =
1820 - ahstack->ipsecah_default_hard_bytes;
1821 - comb->sadb_comb_soft_addtime =
1822 - ahstack->ipsecah_default_soft_addtime;
1823 - comb->sadb_comb_hard_addtime =
1824 - ahstack->ipsecah_default_hard_addtime;
1825 - comb->sadb_comb_soft_usetime =
1826 - ahstack->ipsecah_default_soft_usetime;
1827 - comb->sadb_comb_hard_usetime =
1828 - ahstack->ipsecah_default_hard_usetime;
1829 -
1830 - prop->sadb_prop_len += SADB_8TO64(sizeof (*comb));
1831 - if (--combs == 0)
1832 - return; /* out of space.. */
1833 - comb++;
1834 - }
1835 -}
1836 -
1837 -/*
1838 - * Prepare and actually send the SADB_ACQUIRE message to PF_KEY.
1839 - */
1840 -static void
1841 -ah_send_acquire(ipsacq_t *acqrec, mblk_t *extended, netstack_t *ns)
1842 -{
1843 - uint_t combs;
1844 - sadb_msg_t *samsg;
1845 - sadb_prop_t *prop;
1846 - mblk_t *pfkeymp, *msgmp;
1847 - ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
1848 - ipsec_stack_t *ipss = ns->netstack_ipsec;
1849 -
1850 - AH_BUMP_STAT(ahstack, acquire_requests);
1851 -
1852 - if (ahstack->ah_pfkey_q == NULL) {
1853 - mutex_exit(&acqrec->ipsacq_lock);
1854 - return;
1855 - }
1856 -
1857 - /* Set up ACQUIRE. */
1858 - pfkeymp = sadb_setup_acquire(acqrec, SADB_SATYPE_AH,
1859 - ns->netstack_ipsec);
1860 - if (pfkeymp == NULL) {
1861 - ah0dbg(("sadb_setup_acquire failed.\n"));
1862 - mutex_exit(&acqrec->ipsacq_lock);
1863 - return;
1864 - }
1865 - ASSERT(RW_READ_HELD(&ipss->ipsec_alg_lock));
1866 - combs = ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
1867 - msgmp = pfkeymp->b_cont;
1868 - samsg = (sadb_msg_t *)(msgmp->b_rptr);
1869 -
1870 - /* Insert proposal here. */
1871 -
1872 - prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len);
1873 - ah_insert_prop(prop, acqrec, combs, ns);
1874 - samsg->sadb_msg_len += prop->sadb_prop_len;
1875 - msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len);
1876 -
1877 - rw_exit(&ipss->ipsec_alg_lock);
1878 -
1879 - /*
1880 - * Must mutex_exit() before sending PF_KEY message up, in
1881 - * order to avoid recursive mutex_enter() if there are no registered
1882 - * listeners.
1883 - *
1884 - * Once I've sent the message, I'm cool anyway.
1885 - */
1886 - mutex_exit(&acqrec->ipsacq_lock);
1887 - if (extended != NULL) {
1888 - putnext(ahstack->ah_pfkey_q, extended);
1889 - }
1890 - putnext(ahstack->ah_pfkey_q, pfkeymp);
1891 -}
1892 -
1893 1722 /* Refactor me */
1894 1723 /*
1895 1724 * Handle the SADB_GETSPI message. Create a larval SA.
1896 1725 */
1897 1726 static void
1898 1727 ah_getspi(mblk_t *mp, keysock_in_t *ksi, ipsecah_stack_t *ahstack)
1899 1728 {
1900 1729 ipsa_t *newbie, *target;
1901 1730 isaf_t *outbound, *inbound;
1902 1731 int rc, diagnostic;
1903 1732 sadb_sa_t *assoc;
1904 1733 keysock_out_t *kso;
1905 1734 uint32_t newspi;
1906 1735
1907 1736 /*
1908 1737 * Randomly generate a proposed SPI value.
1909 1738 */
1910 1739 if (cl_inet_getspi != NULL) {
1911 1740 cl_inet_getspi(ahstack->ipsecah_netstack->netstack_stackid,
1912 1741 IPPROTO_AH, (uint8_t *)&newspi, sizeof (uint32_t), NULL);
1913 1742 } else {
1914 1743 (void) random_get_pseudo_bytes((uint8_t *)&newspi,
1915 1744 sizeof (uint32_t));
1916 1745 }
1917 1746 newbie = sadb_getspi(ksi, newspi, &diagnostic,
1918 1747 ahstack->ipsecah_netstack, IPPROTO_AH);
1919 1748
1920 1749 if (newbie == NULL) {
1921 1750 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, ENOMEM, diagnostic,
1922 1751 ksi->ks_in_serial);
1923 1752 return;
1924 1753 } else if (newbie == (ipsa_t *)-1) {
1925 1754 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL, diagnostic,
1926 1755 ksi->ks_in_serial);
1927 1756 return;
1928 1757 }
1929 1758
1930 1759 /*
1931 1760 * XXX - We may randomly collide. We really should recover from this.
1932 1761 * Unfortunately, that could require spending way-too-much-time
1933 1762 * in here. For now, let the user retry.
1934 1763 */
1935 1764
1936 1765 if (newbie->ipsa_addrfam == AF_INET6) {
1937 1766 outbound = OUTBOUND_BUCKET_V6(&ahstack->ah_sadb.s_v6,
1938 1767 *(uint32_t *)(newbie->ipsa_dstaddr));
1939 1768 inbound = INBOUND_BUCKET(&ahstack->ah_sadb.s_v6,
1940 1769 newbie->ipsa_spi);
1941 1770 } else {
1942 1771 outbound = OUTBOUND_BUCKET_V4(&ahstack->ah_sadb.s_v4,
1943 1772 *(uint32_t *)(newbie->ipsa_dstaddr));
1944 1773 inbound = INBOUND_BUCKET(&ahstack->ah_sadb.s_v4,
1945 1774 newbie->ipsa_spi);
1946 1775 }
1947 1776
1948 1777 mutex_enter(&outbound->isaf_lock);
1949 1778 mutex_enter(&inbound->isaf_lock);
1950 1779
1951 1780 /*
1952 1781 * Check for collisions (i.e. did sadb_getspi() return with something
1953 1782 * that already exists?).
1954 1783 *
1955 1784 * Try outbound first. Even though SADB_GETSPI is traditionally
1956 1785 * for inbound SAs, you never know what a user might do.
1957 1786 */
1958 1787 target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi,
1959 1788 newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam);
1960 1789 if (target == NULL) {
1961 1790 target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi,
1962 1791 newbie->ipsa_srcaddr, newbie->ipsa_dstaddr,
1963 1792 newbie->ipsa_addrfam);
1964 1793 }
1965 1794
1966 1795 /*
1967 1796 * I don't have collisions elsewhere!
1968 1797 * (Nor will I because I'm still holding inbound/outbound locks.)
1969 1798 */
1970 1799
1971 1800 if (target != NULL) {
1972 1801 rc = EEXIST;
1973 1802 IPSA_REFRELE(target);
1974 1803 } else {
1975 1804 /*
1976 1805 * sadb_insertassoc() also checks for collisions, so
1977 1806 * if there's a colliding larval entry, rc will be set
1978 1807 * to EEXIST.
1979 1808 */
1980 1809 rc = sadb_insertassoc(newbie, inbound);
1981 1810 newbie->ipsa_hardexpiretime = gethrestime_sec();
1982 1811 newbie->ipsa_hardexpiretime += ahstack->ipsecah_larval_timeout;
1983 1812 }
1984 1813
1985 1814 /*
1986 1815 * Can exit outbound mutex. Hold inbound until we're done with
1987 1816 * newbie.
1988 1817 */
1989 1818 mutex_exit(&outbound->isaf_lock);
1990 1819
1991 1820 if (rc != 0) {
1992 1821 mutex_exit(&inbound->isaf_lock);
1993 1822 IPSA_REFRELE(newbie);
1994 1823 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, rc,
1995 1824 SADB_X_DIAGNOSTIC_NONE, ksi->ks_in_serial);
1996 1825 return;
1997 1826 }
1998 1827
1999 1828 /* Can write here because I'm still holding the bucket lock. */
2000 1829 newbie->ipsa_type = SADB_SATYPE_AH;
2001 1830
2002 1831 /*
2003 1832 * Construct successful return message. We have one thing going
2004 1833 * for us in PF_KEY v2. That's the fact that
2005 1834 * sizeof (sadb_spirange_t) == sizeof (sadb_sa_t)
2006 1835 */
2007 1836 assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE];
2008 1837 assoc->sadb_sa_exttype = SADB_EXT_SA;
2009 1838 assoc->sadb_sa_spi = newbie->ipsa_spi;
2010 1839 *((uint64_t *)(&assoc->sadb_sa_replay)) = 0;
2011 1840 mutex_exit(&inbound->isaf_lock);
2012 1841
2013 1842 /* Convert KEYSOCK_IN to KEYSOCK_OUT. */
2014 1843 kso = (keysock_out_t *)ksi;
2015 1844 kso->ks_out_len = sizeof (*kso);
2016 1845 kso->ks_out_serial = ksi->ks_in_serial;
2017 1846 kso->ks_out_type = KEYSOCK_OUT;
2018 1847
2019 1848 /*
2020 1849 * Can safely putnext() to ah_pfkey_q, because this is a turnaround
2021 1850 * from the ah_pfkey_q.
2022 1851 */
2023 1852 putnext(ahstack->ah_pfkey_q, mp);
2024 1853 }
2025 1854
2026 1855 /*
2027 1856 * IPv6 sends up the ICMP errors for validation and the removal of the AH
2028 1857 * header.
2029 1858 * If succesful, the mp has been modified to not include the AH header so
2030 1859 * that the caller can fanout to the ULP's icmp error handler.
2031 1860 */
2032 1861 static mblk_t *
2033 1862 ah_icmp_error_v6(mblk_t *mp, ip_recv_attr_t *ira, ipsecah_stack_t *ahstack)
2034 1863 {
2035 1864 ip6_t *ip6h, *oip6h;
2036 1865 uint16_t hdr_length, ah_length;
2037 1866 uint8_t *nexthdrp;
2038 1867 ah_t *ah;
2039 1868 icmp6_t *icmp6;
2040 1869 isaf_t *isaf;
2041 1870 ipsa_t *assoc;
2042 1871 uint8_t *post_ah_ptr;
2043 1872 ipsec_stack_t *ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2044 1873
2045 1874 /*
2046 1875 * Eat the cost of a pullupmsg() for now. It makes the rest of this
2047 1876 * code far less convoluted.
2048 1877 */
2049 1878 if (!pullupmsg(mp, -1) ||
2050 1879 !ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, &hdr_length,
2051 1880 &nexthdrp) ||
2052 1881 mp->b_rptr + hdr_length + sizeof (icmp6_t) + sizeof (ip6_t) +
2053 1882 sizeof (ah_t) > mp->b_wptr) {
2054 1883 IP_AH_BUMP_STAT(ipss, in_discards);
2055 1884 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2056 1885 DROPPER(ipss, ipds_ah_nomem),
2057 1886 &ahstack->ah_dropper);
2058 1887 return (NULL);
2059 1888 }
2060 1889
2061 1890 oip6h = (ip6_t *)mp->b_rptr;
2062 1891 icmp6 = (icmp6_t *)((uint8_t *)oip6h + hdr_length);
2063 1892 ip6h = (ip6_t *)(icmp6 + 1);
2064 1893 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &hdr_length, &nexthdrp)) {
2065 1894 IP_AH_BUMP_STAT(ipss, in_discards);
2066 1895 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2067 1896 DROPPER(ipss, ipds_ah_bad_v6_hdrs),
2068 1897 &ahstack->ah_dropper);
2069 1898 return (NULL);
2070 1899 }
2071 1900 ah = (ah_t *)((uint8_t *)ip6h + hdr_length);
2072 1901
2073 1902 isaf = OUTBOUND_BUCKET_V6(&ahstack->ah_sadb.s_v6, ip6h->ip6_dst);
2074 1903 mutex_enter(&isaf->isaf_lock);
2075 1904 assoc = ipsec_getassocbyspi(isaf, ah->ah_spi,
2076 1905 (uint32_t *)&ip6h->ip6_src, (uint32_t *)&ip6h->ip6_dst, AF_INET6);
2077 1906 mutex_exit(&isaf->isaf_lock);
2078 1907
2079 1908 if (assoc == NULL) {
2080 1909 IP_AH_BUMP_STAT(ipss, lookup_failure);
2081 1910 IP_AH_BUMP_STAT(ipss, in_discards);
2082 1911 if (ahstack->ipsecah_log_unknown_spi) {
2083 1912 ipsec_assocfailure(info.mi_idnum, 0, 0,
2084 1913 SL_CONSOLE | SL_WARN | SL_ERROR,
2085 1914 "Bad ICMP message - No association for the "
2086 1915 "attached AH header whose spi is 0x%x, "
2087 1916 "sender is 0x%x\n",
2088 1917 ah->ah_spi, &oip6h->ip6_src, AF_INET6,
2089 1918 ahstack->ipsecah_netstack);
2090 1919 }
2091 1920 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2092 1921 DROPPER(ipss, ipds_ah_no_sa),
2093 1922 &ahstack->ah_dropper);
2094 1923 return (NULL);
2095 1924 }
2096 1925
2097 1926 IPSA_REFRELE(assoc);
2098 1927
2099 1928 /*
2100 1929 * There seems to be a valid association. If there is enough of AH
2101 1930 * header remove it, otherwise bail. One could check whether it has
2102 1931 * complete AH header plus 8 bytes but it does not make sense if an
2103 1932 * icmp error is returned for ICMP messages e.g ICMP time exceeded,
2104 1933 * that are being sent up. Let the caller figure out.
2105 1934 *
2106 1935 * NOTE: ah_length is the number of 32 bit words minus 2.
2107 1936 */
2108 1937 ah_length = (ah->ah_length << 2) + 8;
2109 1938 post_ah_ptr = (uint8_t *)ah + ah_length;
2110 1939
2111 1940 if (post_ah_ptr > mp->b_wptr) {
2112 1941 IP_AH_BUMP_STAT(ipss, in_discards);
2113 1942 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2114 1943 DROPPER(ipss, ipds_ah_bad_length),
2115 1944 &ahstack->ah_dropper);
2116 1945 return (NULL);
2117 1946 }
2118 1947
2119 1948 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - ah_length);
2120 1949 *nexthdrp = ah->ah_nexthdr;
2121 1950 ovbcopy(post_ah_ptr, ah,
2122 1951 (size_t)((uintptr_t)mp->b_wptr - (uintptr_t)post_ah_ptr));
2123 1952 mp->b_wptr -= ah_length;
2124 1953
2125 1954 return (mp);
2126 1955 }
2127 1956
2128 1957 /*
2129 1958 * IP sends up the ICMP errors for validation and the removal of
2130 1959 * the AH header.
2131 1960 * If succesful, the mp has been modified to not include the AH header so
2132 1961 * that the caller can fanout to the ULP's icmp error handler.
2133 1962 */
2134 1963 static mblk_t *
2135 1964 ah_icmp_error_v4(mblk_t *mp, ip_recv_attr_t *ira, ipsecah_stack_t *ahstack)
2136 1965 {
2137 1966 mblk_t *mp1;
2138 1967 icmph_t *icmph;
2139 1968 int iph_hdr_length;
2140 1969 int hdr_length;
2141 1970 isaf_t *hptr;
2142 1971 ipsa_t *assoc;
2143 1972 int ah_length;
2144 1973 ipha_t *ipha;
2145 1974 ipha_t *oipha;
2146 1975 ah_t *ah;
2147 1976 uint32_t length;
2148 1977 int alloc_size;
2149 1978 uint8_t nexthdr;
2150 1979 ipsec_stack_t *ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2151 1980
2152 1981 oipha = ipha = (ipha_t *)mp->b_rptr;
2153 1982 iph_hdr_length = IPH_HDR_LENGTH(ipha);
2154 1983 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2155 1984
2156 1985 ipha = (ipha_t *)&icmph[1];
2157 1986 hdr_length = IPH_HDR_LENGTH(ipha);
2158 1987
2159 1988 /*
2160 1989 * See if we have enough to locate the SPI
2161 1990 */
2162 1991 if ((uchar_t *)ipha + hdr_length + 8 > mp->b_wptr) {
2163 1992 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 8 -
2164 1993 mp->b_rptr)) {
2165 1994 ipsec_rl_strlog(ahstack->ipsecah_netstack,
2166 1995 info.mi_idnum, 0, 0,
2167 1996 SL_WARN | SL_ERROR,
2168 1997 "ICMP error: Small AH header\n");
2169 1998 IP_AH_BUMP_STAT(ipss, in_discards);
2170 1999 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2171 2000 DROPPER(ipss, ipds_ah_bad_length),
2172 2001 &ahstack->ah_dropper);
2173 2002 return (NULL);
2174 2003 }
2175 2004 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2176 2005 ipha = (ipha_t *)&icmph[1];
2177 2006 }
2178 2007
2179 2008 ah = (ah_t *)((uint8_t *)ipha + hdr_length);
2180 2009 nexthdr = ah->ah_nexthdr;
2181 2010
2182 2011 hptr = OUTBOUND_BUCKET_V4(&ahstack->ah_sadb.s_v4, ipha->ipha_dst);
2183 2012 mutex_enter(&hptr->isaf_lock);
2184 2013 assoc = ipsec_getassocbyspi(hptr, ah->ah_spi,
2185 2014 (uint32_t *)&ipha->ipha_src, (uint32_t *)&ipha->ipha_dst, AF_INET);
2186 2015 mutex_exit(&hptr->isaf_lock);
2187 2016
2188 2017 if (assoc == NULL) {
2189 2018 IP_AH_BUMP_STAT(ipss, lookup_failure);
2190 2019 IP_AH_BUMP_STAT(ipss, in_discards);
2191 2020 if (ahstack->ipsecah_log_unknown_spi) {
2192 2021 ipsec_assocfailure(info.mi_idnum, 0, 0,
2193 2022 SL_CONSOLE | SL_WARN | SL_ERROR,
2194 2023 "Bad ICMP message - No association for the "
2195 2024 "attached AH header whose spi is 0x%x, "
2196 2025 "sender is 0x%x\n",
2197 2026 ah->ah_spi, &oipha->ipha_src, AF_INET,
2198 2027 ahstack->ipsecah_netstack);
2199 2028 }
2200 2029 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2201 2030 DROPPER(ipss, ipds_ah_no_sa),
2202 2031 &ahstack->ah_dropper);
2203 2032 return (NULL);
2204 2033 }
2205 2034
2206 2035 IPSA_REFRELE(assoc);
2207 2036 /*
2208 2037 * There seems to be a valid association. If there
2209 2038 * is enough of AH header remove it, otherwise remove
2210 2039 * as much as possible and send it back. One could check
2211 2040 * whether it has complete AH header plus 8 bytes but it
2212 2041 * does not make sense if an icmp error is returned for
2213 2042 * ICMP messages e.g ICMP time exceeded, that are being
2214 2043 * sent up. Let the caller figure out.
2215 2044 *
2216 2045 * NOTE: ah_length is the number of 32 bit words minus 2.
2217 2046 */
2218 2047 ah_length = (ah->ah_length << 2) + 8;
2219 2048
2220 2049 if ((uchar_t *)ipha + hdr_length + ah_length > mp->b_wptr) {
2221 2050 if (mp->b_cont == NULL) {
2222 2051 /*
2223 2052 * There is nothing to pullup. Just remove as
2224 2053 * much as possible. This is a common case for
2225 2054 * IPV4.
2226 2055 */
2227 2056 ah_length = (mp->b_wptr - ((uchar_t *)ipha +
2228 2057 hdr_length));
2229 2058 goto done;
2230 2059 }
2231 2060 /* Pullup the full ah header */
2232 2061 if (!pullupmsg(mp, (uchar_t *)ah + ah_length - mp->b_rptr)) {
2233 2062 /*
2234 2063 * pullupmsg could have failed if there was not
2235 2064 * enough to pullup or memory allocation failed.
2236 2065 * We tried hard, give up now.
2237 2066 */
2238 2067 IP_AH_BUMP_STAT(ipss, in_discards);
2239 2068 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2240 2069 DROPPER(ipss, ipds_ah_nomem),
2241 2070 &ahstack->ah_dropper);
2242 2071 return (NULL);
2243 2072 }
2244 2073 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2245 2074 ipha = (ipha_t *)&icmph[1];
2246 2075 }
2247 2076 done:
2248 2077 /*
2249 2078 * Remove the AH header and change the protocol.
2250 2079 * Don't update the spi fields in the ip_recv_attr_t
2251 2080 * as we are called just to validate the
2252 2081 * message attached to the ICMP message.
2253 2082 *
2254 2083 * If we never pulled up since all of the message
2255 2084 * is in one single mblk, we can't remove the AH header
2256 2085 * by just setting the b_wptr to the beginning of the
2257 2086 * AH header. We need to allocate a mblk that can hold
2258 2087 * up until the inner IP header and copy them.
2259 2088 */
2260 2089 alloc_size = iph_hdr_length + sizeof (icmph_t) + hdr_length;
2261 2090
2262 2091 if ((mp1 = allocb(alloc_size, BPRI_LO)) == NULL) {
2263 2092 IP_AH_BUMP_STAT(ipss, in_discards);
2264 2093 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2265 2094 DROPPER(ipss, ipds_ah_nomem),
2266 2095 &ahstack->ah_dropper);
2267 2096 return (NULL);
2268 2097 }
2269 2098 bcopy(mp->b_rptr, mp1->b_rptr, alloc_size);
2270 2099 mp1->b_wptr += alloc_size;
2271 2100
2272 2101 /*
2273 2102 * Skip whatever we have copied and as much of AH header
2274 2103 * possible. If we still have something left in the original
2275 2104 * message, tag on.
2276 2105 */
2277 2106 mp->b_rptr = (uchar_t *)ipha + hdr_length + ah_length;
2278 2107
2279 2108 if (mp->b_rptr != mp->b_wptr) {
2280 2109 mp1->b_cont = mp;
2281 2110 } else {
2282 2111 if (mp->b_cont != NULL)
2283 2112 mp1->b_cont = mp->b_cont;
2284 2113 freeb(mp);
2285 2114 }
2286 2115
2287 2116 ipha = (ipha_t *)(mp1->b_rptr + iph_hdr_length + sizeof (icmph_t));
2288 2117 ipha->ipha_protocol = nexthdr;
2289 2118 length = ntohs(ipha->ipha_length);
2290 2119 length -= ah_length;
2291 2120 ipha->ipha_length = htons((uint16_t)length);
2292 2121 ipha->ipha_hdr_checksum = 0;
2293 2122 ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
2294 2123
2295 2124 return (mp1);
2296 2125 }
2297 2126
2298 2127 /*
2299 2128 * IP calls this to validate the ICMP errors that
2300 2129 * we got from the network.
2301 2130 */
2302 2131 mblk_t *
2303 2132 ipsecah_icmp_error(mblk_t *data_mp, ip_recv_attr_t *ira)
2304 2133 {
2305 2134 netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
2306 2135 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
2307 2136
2308 2137 if (ira->ira_flags & IRAF_IS_IPV4)
2309 2138 return (ah_icmp_error_v4(data_mp, ira, ahstack));
2310 2139 else
2311 2140 return (ah_icmp_error_v6(data_mp, ira, ahstack));
2312 2141 }
2313 2142
2314 2143 static int
2315 2144 ah_fix_tlv_options_v6(uint8_t *oi_opt, uint8_t *pi_opt, uint_t ehdrlen,
2316 2145 uint8_t hdr_type, boolean_t copy_always)
2317 2146 {
2318 2147 uint8_t opt_type;
2319 2148 uint_t optlen;
2320 2149
2321 2150 ASSERT(hdr_type == IPPROTO_DSTOPTS || hdr_type == IPPROTO_HOPOPTS);
2322 2151
2323 2152 /*
2324 2153 * Copy the next header and hdr ext. len of the HOP-by-HOP
2325 2154 * and Destination option.
2326 2155 */
2327 2156 *pi_opt++ = *oi_opt++;
2328 2157 *pi_opt++ = *oi_opt++;
2329 2158 ehdrlen -= 2;
2330 2159
2331 2160 /*
2332 2161 * Now handle all the TLV encoded options.
2333 2162 */
2334 2163 while (ehdrlen != 0) {
2335 2164 opt_type = *oi_opt;
2336 2165
2337 2166 if (opt_type == IP6OPT_PAD1) {
2338 2167 optlen = 1;
2339 2168 } else {
2340 2169 if (ehdrlen < 2)
2341 2170 goto bad_opt;
2342 2171 optlen = 2 + oi_opt[1];
2343 2172 if (optlen > ehdrlen)
2344 2173 goto bad_opt;
2345 2174 }
2346 2175 if (copy_always || !(opt_type & IP6OPT_MUTABLE)) {
2347 2176 bcopy(oi_opt, pi_opt, optlen);
2348 2177 } else {
2349 2178 if (optlen == 1) {
2350 2179 *pi_opt = 0;
2351 2180 } else {
2352 2181 /*
2353 2182 * Copy the type and data length fields.
2354 2183 * Zero the option data by skipping
2355 2184 * option type and option data len
2356 2185 * fields.
2357 2186 */
2358 2187 *pi_opt = *oi_opt;
2359 2188 *(pi_opt + 1) = *(oi_opt + 1);
2360 2189 bzero(pi_opt + 2, optlen - 2);
2361 2190 }
2362 2191 }
2363 2192 ehdrlen -= optlen;
2364 2193 oi_opt += optlen;
2365 2194 pi_opt += optlen;
2366 2195 }
2367 2196 return (0);
2368 2197 bad_opt:
2369 2198 return (-1);
2370 2199 }
2371 2200
2372 2201 /*
2373 2202 * Construct a pseudo header for AH, processing all the options.
2374 2203 *
2375 2204 * oip6h is the IPv6 header of the incoming or outgoing packet.
2376 2205 * ip6h is the pointer to the pseudo headers IPV6 header. All
2377 2206 * the space needed for the options have been allocated including
2378 2207 * the AH header.
2379 2208 *
2380 2209 * If copy_always is set, all the options that appear before AH are copied
2381 2210 * blindly without checking for IP6OPT_MUTABLE. This is used by
2382 2211 * ah_auth_out_done(). Please refer to that function for details.
2383 2212 *
2384 2213 * NOTE :
2385 2214 *
2386 2215 * * AH header is never copied in this function even if copy_always
2387 2216 * is set. It just returns the ah_offset - offset of the AH header
2388 2217 * and the caller needs to do the copying. This is done so that we
2389 2218 * don't have pass extra arguments e.g. SA etc. and also,
2390 2219 * it is not needed when ah_auth_out_done is calling this function.
2391 2220 */
2392 2221 static uint_t
2393 2222 ah_fix_phdr_v6(ip6_t *ip6h, ip6_t *oip6h, boolean_t outbound,
2394 2223 boolean_t copy_always)
2395 2224 {
2396 2225 uint8_t *oi_opt;
2397 2226 uint8_t *pi_opt;
2398 2227 uint8_t nexthdr;
2399 2228 uint8_t *prev_nexthdr;
2400 2229 ip6_hbh_t *hbhhdr;
2401 2230 ip6_dest_t *dsthdr = NULL;
2402 2231 ip6_rthdr0_t *rthdr;
2403 2232 int ehdrlen;
2404 2233 ah_t *ah;
2405 2234 int ret;
2406 2235
2407 2236 /*
2408 2237 * In the outbound case for source route, ULP has already moved
2409 2238 * the first hop, which is now in ip6_dst. We need to re-arrange
2410 2239 * the header to make it look like how it would appear in the
2411 2240 * receiver i.e
2412 2241 *
2413 2242 * Because of ip_massage_options_v6 the header looks like
2414 2243 * this :
2415 2244 *
2416 2245 * ip6_src = S, ip6_dst = I1. followed by I2,I3,D.
2417 2246 *
2418 2247 * When it reaches the receiver, it would look like
2419 2248 *
2420 2249 * ip6_src = S, ip6_dst = D. followed by I1,I2,I3.
2421 2250 *
2422 2251 * NOTE : We assume that there are no problems with the options
2423 2252 * as IP should have already checked this.
2424 2253 */
2425 2254
2426 2255 oi_opt = (uchar_t *)&oip6h[1];
2427 2256 pi_opt = (uchar_t *)&ip6h[1];
2428 2257
2429 2258 /*
2430 2259 * We set the prev_nexthdr properly in the pseudo header.
2431 2260 * After we finish authentication and come back from the
2432 2261 * algorithm module, pseudo header will become the real
2433 2262 * IP header.
2434 2263 */
2435 2264 prev_nexthdr = (uint8_t *)&ip6h->ip6_nxt;
2436 2265 nexthdr = oip6h->ip6_nxt;
2437 2266 /* Assume IP has already stripped it */
2438 2267 ASSERT(nexthdr != IPPROTO_FRAGMENT);
2439 2268 ah = NULL;
2440 2269 dsthdr = NULL;
2441 2270 for (;;) {
2442 2271 switch (nexthdr) {
2443 2272 case IPPROTO_HOPOPTS:
2444 2273 hbhhdr = (ip6_hbh_t *)oi_opt;
2445 2274 nexthdr = hbhhdr->ip6h_nxt;
2446 2275 ehdrlen = 8 * (hbhhdr->ip6h_len + 1);
2447 2276 ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen,
2448 2277 IPPROTO_HOPOPTS, copy_always);
2449 2278 /*
2450 2279 * Return a zero offset indicating error if there
2451 2280 * was error.
2452 2281 */
2453 2282 if (ret == -1)
2454 2283 return (0);
2455 2284 hbhhdr = (ip6_hbh_t *)pi_opt;
2456 2285 prev_nexthdr = (uint8_t *)&hbhhdr->ip6h_nxt;
2457 2286 break;
2458 2287 case IPPROTO_ROUTING:
2459 2288 rthdr = (ip6_rthdr0_t *)oi_opt;
2460 2289 nexthdr = rthdr->ip6r0_nxt;
2461 2290 ehdrlen = 8 * (rthdr->ip6r0_len + 1);
2462 2291 if (!copy_always && outbound) {
2463 2292 int i, left;
2464 2293 ip6_rthdr0_t *prthdr;
2465 2294 in6_addr_t *ap, *pap;
2466 2295
2467 2296 left = rthdr->ip6r0_segleft;
2468 2297 prthdr = (ip6_rthdr0_t *)pi_opt;
2469 2298 pap = (in6_addr_t *)(prthdr + 1);
2470 2299 ap = (in6_addr_t *)(rthdr + 1);
2471 2300 /*
2472 2301 * First eight bytes except seg_left
2473 2302 * does not change en route.
2474 2303 */
2475 2304 bcopy(oi_opt, pi_opt, 8);
2476 2305 prthdr->ip6r0_segleft = 0;
2477 2306 /*
2478 2307 * First address has been moved to
2479 2308 * the destination address of the
2480 2309 * ip header by ip_massage_options_v6.
2481 2310 * And the real destination address is
2482 2311 * in the last address part of the
2483 2312 * option.
2484 2313 */
2485 2314 *pap = oip6h->ip6_dst;
2486 2315 for (i = 1; i < left - 1; i++)
2487 2316 pap[i] = ap[i - 1];
2488 2317 ip6h->ip6_dst = *(ap + left - 1);
2489 2318 } else {
2490 2319 bcopy(oi_opt, pi_opt, ehdrlen);
2491 2320 }
2492 2321 rthdr = (ip6_rthdr0_t *)pi_opt;
2493 2322 prev_nexthdr = (uint8_t *)&rthdr->ip6r0_nxt;
2494 2323 break;
2495 2324 case IPPROTO_DSTOPTS:
2496 2325 /*
2497 2326 * Destination options are tricky. If there is
2498 2327 * a terminal (e.g. non-IPv6-extension) header
2499 2328 * following the destination options, don't
2500 2329 * reset prev_nexthdr or advance the AH insertion
2501 2330 * point and just treat this as a terminal header.
2502 2331 *
2503 2332 * If this is an inbound packet, just deal with
2504 2333 * it as is.
2505 2334 */
2506 2335 dsthdr = (ip6_dest_t *)oi_opt;
2507 2336 /*
2508 2337 * XXX I hope common-subexpression elimination
2509 2338 * saves us the double-evaluate.
2510 2339 */
2511 2340 if (outbound && dsthdr->ip6d_nxt != IPPROTO_ROUTING &&
2512 2341 dsthdr->ip6d_nxt != IPPROTO_HOPOPTS)
2513 2342 goto terminal_hdr;
2514 2343 nexthdr = dsthdr->ip6d_nxt;
2515 2344 ehdrlen = 8 * (dsthdr->ip6d_len + 1);
2516 2345 ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen,
2517 2346 IPPROTO_DSTOPTS, copy_always);
2518 2347 /*
2519 2348 * Return a zero offset indicating error if there
2520 2349 * was error.
2521 2350 */
2522 2351 if (ret == -1)
2523 2352 return (0);
2524 2353 break;
2525 2354 case IPPROTO_AH:
2526 2355 /*
2527 2356 * Be conservative in what you send. We shouldn't
2528 2357 * see two same-scoped AH's in one packet.
2529 2358 * (Inner-IP-scoped AH will be hit by terminal
2530 2359 * header of IP or IPv6.)
2531 2360 */
2532 2361 ASSERT(!outbound);
2533 2362 return ((uint_t)(pi_opt - (uint8_t *)ip6h));
2534 2363 default:
2535 2364 ASSERT(outbound);
2536 2365 terminal_hdr:
2537 2366 *prev_nexthdr = IPPROTO_AH;
2538 2367 ah = (ah_t *)pi_opt;
2539 2368 ah->ah_nexthdr = nexthdr;
2540 2369 return ((uint_t)(pi_opt - (uint8_t *)ip6h));
2541 2370 }
2542 2371 pi_opt += ehdrlen;
2543 2372 oi_opt += ehdrlen;
2544 2373 }
2545 2374 /* NOTREACHED */
2546 2375 }
2547 2376
2548 2377 static boolean_t
2549 2378 ah_finish_up(ah_t *phdr_ah, ah_t *inbound_ah, ipsa_t *assoc,
2550 2379 int ah_data_sz, int ah_align_sz, ipsecah_stack_t *ahstack)
2551 2380 {
2552 2381 int i;
2553 2382
2554 2383 /*
2555 2384 * Padding :
2556 2385 *
2557 2386 * 1) Authentication data may have to be padded
2558 2387 * before ICV calculation if ICV is not a multiple
2559 2388 * of 64 bits. This padding is arbitrary and transmitted
2560 2389 * with the packet at the end of the authentication data.
2561 2390 * Payload length should include the padding bytes.
2562 2391 *
2563 2392 * 2) Explicit padding of the whole datagram may be
2564 2393 * required by the algorithm which need not be
2565 2394 * transmitted. It is assumed that this will be taken
2566 2395 * care by the algorithm module.
2567 2396 */
2568 2397 bzero(phdr_ah + 1, ah_data_sz); /* Zero out ICV for pseudo-hdr. */
2569 2398
2570 2399 if (inbound_ah == NULL) {
2571 2400 /* Outbound AH datagram. */
2572 2401
2573 2402 phdr_ah->ah_length = (ah_align_sz >> 2) + 1;
2574 2403 phdr_ah->ah_reserved = 0;
2575 2404 phdr_ah->ah_spi = assoc->ipsa_spi;
2576 2405
2577 2406 phdr_ah->ah_replay =
2578 2407 htonl(atomic_inc_32_nv(&assoc->ipsa_replay));
2579 2408 if (phdr_ah->ah_replay == 0 && assoc->ipsa_replay_wsize != 0) {
2580 2409 /*
2581 2410 * XXX We have replay counter wrapping. We probably
2582 2411 * want to nuke this SA (and its peer).
2583 2412 */
2584 2413 ipsec_assocfailure(info.mi_idnum, 0, 0,
2585 2414 SL_ERROR | SL_CONSOLE | SL_WARN,
2586 2415 "Outbound AH SA (0x%x), dst %s has wrapped "
2587 2416 "sequence.\n", phdr_ah->ah_spi,
2588 2417 assoc->ipsa_dstaddr, assoc->ipsa_addrfam,
2589 2418 ahstack->ipsecah_netstack);
2590 2419
2591 2420 sadb_replay_delete(assoc);
2592 2421 /* Caller will free phdr_mp and return NULL. */
2593 2422 return (B_FALSE);
2594 2423 }
2595 2424
2596 2425 if (ah_data_sz != ah_align_sz) {
2597 2426 uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) +
2598 2427 ah_data_sz);
2599 2428
2600 2429 for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
2601 2430 pad[i] = (uchar_t)i; /* Fill the padding */
2602 2431 }
2603 2432 }
2604 2433 } else {
2605 2434 /* Inbound AH datagram. */
2606 2435 phdr_ah->ah_nexthdr = inbound_ah->ah_nexthdr;
2607 2436 phdr_ah->ah_length = inbound_ah->ah_length;
2608 2437 phdr_ah->ah_reserved = 0;
2609 2438 ASSERT(inbound_ah->ah_spi == assoc->ipsa_spi);
2610 2439 phdr_ah->ah_spi = inbound_ah->ah_spi;
2611 2440 phdr_ah->ah_replay = inbound_ah->ah_replay;
2612 2441
2613 2442 if (ah_data_sz != ah_align_sz) {
2614 2443 uchar_t *opad = ((uchar_t *)inbound_ah +
2615 2444 sizeof (ah_t) + ah_data_sz);
2616 2445 uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) +
2617 2446 ah_data_sz);
2618 2447
2619 2448 for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
2620 2449 pad[i] = opad[i]; /* Copy the padding */
2621 2450 }
2622 2451 }
2623 2452 }
2624 2453
2625 2454 return (B_TRUE);
2626 2455 }
2627 2456
2628 2457 /*
2629 2458 * Called upon failing the inbound ICV check. The message passed as
2630 2459 * argument is freed.
2631 2460 */
2632 2461 static void
2633 2462 ah_log_bad_auth(mblk_t *mp, ip_recv_attr_t *ira, ipsec_crypto_t *ic)
2634 2463 {
2635 2464 boolean_t isv4 = (ira->ira_flags & IRAF_IS_IPV4);
2636 2465 ipsa_t *assoc = ira->ira_ipsec_ah_sa;
2637 2466 int af;
2638 2467 void *addr;
2639 2468 netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
2640 2469 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
2641 2470 ipsec_stack_t *ipss = ns->netstack_ipsec;
2642 2471
2643 2472 ASSERT(mp->b_datap->db_type == M_DATA);
2644 2473
2645 2474 mp->b_rptr -= ic->ic_skip_len;
2646 2475
2647 2476 if (isv4) {
2648 2477 ipha_t *ipha = (ipha_t *)mp->b_rptr;
2649 2478 addr = &ipha->ipha_dst;
2650 2479 af = AF_INET;
2651 2480 } else {
2652 2481 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
2653 2482 addr = &ip6h->ip6_dst;
2654 2483 af = AF_INET6;
2655 2484 }
2656 2485
2657 2486 /*
2658 2487 * Log the event. Don't print to the console, block
2659 2488 * potential denial-of-service attack.
2660 2489 */
2661 2490 AH_BUMP_STAT(ahstack, bad_auth);
2662 2491
2663 2492 ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
2664 2493 "AH Authentication failed spi %x, dst_addr %s",
2665 2494 assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
2666 2495
2667 2496 IP_AH_BUMP_STAT(ipss, in_discards);
2668 2497 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2669 2498 DROPPER(ipss, ipds_ah_bad_auth),
2670 2499 &ahstack->ah_dropper);
2671 2500 }
2672 2501
2673 2502 /*
2674 2503 * Kernel crypto framework callback invoked after completion of async
2675 2504 * crypto requests for outbound packets.
2676 2505 */
2677 2506 static void
2678 2507 ah_kcf_callback_outbound(void *arg, int status)
2679 2508 {
2680 2509 mblk_t *mp = (mblk_t *)arg;
2681 2510 mblk_t *async_mp;
2682 2511 netstack_t *ns;
2683 2512 ipsec_stack_t *ipss;
2684 2513 ipsecah_stack_t *ahstack;
2685 2514 mblk_t *data_mp;
2686 2515 ip_xmit_attr_t ixas;
2687 2516 ipsec_crypto_t *ic;
2688 2517 ill_t *ill;
2689 2518
2690 2519 /*
2691 2520 * First remove the ipsec_crypto_t mblk
2692 2521 * Note that we need to ipsec_free_crypto_data(mp) once done with ic.
2693 2522 */
2694 2523 async_mp = ipsec_remove_crypto_data(mp, &ic);
2695 2524 ASSERT(async_mp != NULL);
2696 2525
2697 2526 /*
2698 2527 * Extract the ip_xmit_attr_t from the first mblk.
2699 2528 * Verifies that the netstack and ill is still around; could
2700 2529 * have vanished while kEf was doing its work.
2701 2530 * On succesful return we have a nce_t and the ill/ipst can't
2702 2531 * disappear until we do the nce_refrele in ixa_cleanup.
2703 2532 */
2704 2533 data_mp = async_mp->b_cont;
2705 2534 async_mp->b_cont = NULL;
2706 2535 if (!ip_xmit_attr_from_mblk(async_mp, &ixas)) {
2707 2536 /* Disappeared on us - no ill/ipst for MIB */
2708 2537 if (ixas.ixa_nce != NULL) {
2709 2538 ill = ixas.ixa_nce->nce_ill;
2710 2539 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
2711 2540 ip_drop_output("ipIfStatsOutDiscards", data_mp, ill);
2712 2541 }
2713 2542 freemsg(data_mp);
2714 2543 goto done;
2715 2544 }
2716 2545 ns = ixas.ixa_ipst->ips_netstack;
2717 2546 ahstack = ns->netstack_ipsecah;
2718 2547 ipss = ns->netstack_ipsec;
2719 2548 ill = ixas.ixa_nce->nce_ill;
2720 2549
2721 2550 if (status == CRYPTO_SUCCESS) {
2722 2551 data_mp = ah_auth_out_done(data_mp, &ixas, ic);
2723 2552 if (data_mp == NULL)
2724 2553 goto done;
2725 2554
2726 2555 (void) ip_output_post_ipsec(data_mp, &ixas);
2727 2556 } else {
2728 2557 /* Outbound shouldn't see invalid MAC */
2729 2558 ASSERT(status != CRYPTO_INVALID_MAC);
2730 2559
2731 2560 ah1dbg(ahstack,
2732 2561 ("ah_kcf_callback_outbound: crypto failed with 0x%x\n",
2733 2562 status));
2734 2563 AH_BUMP_STAT(ahstack, crypto_failures);
2735 2564 AH_BUMP_STAT(ahstack, out_discards);
2736 2565
2737 2566 ip_drop_packet(data_mp, B_FALSE, ill,
2738 2567 DROPPER(ipss, ipds_ah_crypto_failed),
2739 2568 &ahstack->ah_dropper);
2740 2569 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
2741 2570 }
2742 2571 done:
2743 2572 ixa_cleanup(&ixas);
2744 2573 (void) ipsec_free_crypto_data(mp);
2745 2574 }
2746 2575
2747 2576 /*
2748 2577 * Kernel crypto framework callback invoked after completion of async
2749 2578 * crypto requests for inbound packets.
2750 2579 */
2751 2580 static void
2752 2581 ah_kcf_callback_inbound(void *arg, int status)
2753 2582 {
2754 2583 mblk_t *mp = (mblk_t *)arg;
2755 2584 mblk_t *async_mp;
2756 2585 netstack_t *ns;
2757 2586 ipsec_stack_t *ipss;
2758 2587 ipsecah_stack_t *ahstack;
2759 2588 mblk_t *data_mp;
2760 2589 ip_recv_attr_t iras;
2761 2590 ipsec_crypto_t *ic;
2762 2591
2763 2592 /*
2764 2593 * First remove the ipsec_crypto_t mblk
2765 2594 * Note that we need to ipsec_free_crypto_data(mp) once done with ic.
2766 2595 */
2767 2596 async_mp = ipsec_remove_crypto_data(mp, &ic);
2768 2597 ASSERT(async_mp != NULL);
2769 2598
2770 2599 /*
2771 2600 * Extract the ip_xmit_attr_t from the first mblk.
2772 2601 * Verifies that the netstack and ill is still around; could
2773 2602 * have vanished while kEf was doing its work.
2774 2603 */
2775 2604 data_mp = async_mp->b_cont;
2776 2605 async_mp->b_cont = NULL;
2777 2606 if (!ip_recv_attr_from_mblk(async_mp, &iras)) {
2778 2607 /* The ill or ip_stack_t disappeared on us */
2779 2608 ip_drop_input("ip_recv_attr_from_mblk", data_mp, NULL);
2780 2609 freemsg(data_mp);
2781 2610 goto done;
2782 2611 }
2783 2612 ns = iras.ira_ill->ill_ipst->ips_netstack;
2784 2613 ahstack = ns->netstack_ipsecah;
2785 2614 ipss = ns->netstack_ipsec;
2786 2615
2787 2616 if (status == CRYPTO_SUCCESS) {
2788 2617 data_mp = ah_auth_in_done(data_mp, &iras, ic);
2789 2618 if (data_mp == NULL)
2790 2619 goto done;
2791 2620
2792 2621 /* finish IPsec processing */
2793 2622 ip_input_post_ipsec(data_mp, &iras);
2794 2623
2795 2624 } else if (status == CRYPTO_INVALID_MAC) {
2796 2625 ah_log_bad_auth(data_mp, &iras, ic);
2797 2626 } else {
2798 2627 ah1dbg(ahstack,
2799 2628 ("ah_kcf_callback_inbound: crypto failed with 0x%x\n",
2800 2629 status));
2801 2630 AH_BUMP_STAT(ahstack, crypto_failures);
2802 2631 IP_AH_BUMP_STAT(ipss, in_discards);
2803 2632 ip_drop_packet(data_mp, B_TRUE, iras.ira_ill,
2804 2633 DROPPER(ipss, ipds_ah_crypto_failed),
2805 2634 &ahstack->ah_dropper);
2806 2635 BUMP_MIB(iras.ira_ill->ill_ip_mib, ipIfStatsInDiscards);
2807 2636 }
2808 2637 done:
2809 2638 ira_cleanup(&iras, B_TRUE);
2810 2639 (void) ipsec_free_crypto_data(mp);
2811 2640 }
2812 2641
2813 2642 /*
2814 2643 * Invoked on kernel crypto failure during inbound and outbound processing.
2815 2644 */
2816 2645 static void
2817 2646 ah_crypto_failed(mblk_t *data_mp, boolean_t is_inbound, int kef_rc,
2818 2647 ill_t *ill, ipsecah_stack_t *ahstack)
2819 2648 {
2820 2649 ipsec_stack_t *ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2821 2650
2822 2651 ah1dbg(ahstack, ("crypto failed for %s AH with 0x%x\n",
2823 2652 is_inbound ? "inbound" : "outbound", kef_rc));
2824 2653 ip_drop_packet(data_mp, is_inbound, ill,
2825 2654 DROPPER(ipss, ipds_ah_crypto_failed),
2826 2655 &ahstack->ah_dropper);
2827 2656 AH_BUMP_STAT(ahstack, crypto_failures);
2828 2657 if (is_inbound)
2829 2658 IP_AH_BUMP_STAT(ipss, in_discards);
2830 2659 else
2831 2660 AH_BUMP_STAT(ahstack, out_discards);
2832 2661 }
2833 2662
2834 2663 /*
2835 2664 * Helper macros for the ah_submit_req_{inbound,outbound}() functions.
2836 2665 */
2837 2666
2838 2667 /*
2839 2668 * A statement-equivalent macro, _cr MUST point to a modifiable
2840 2669 * crypto_call_req_t.
2841 2670 */
2842 2671 #define AH_INIT_CALLREQ(_cr, _mp, _callback) \
2843 2672 (_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_ALWAYS_QUEUE; \
2844 2673 (_cr)->cr_callback_arg = (_mp); \
2845 2674 (_cr)->cr_callback_func = (_callback)
2846 2675
2847 2676 #define AH_INIT_CRYPTO_DATA(data, msglen, mblk) { \
2848 2677 (data)->cd_format = CRYPTO_DATA_MBLK; \
2849 2678 (data)->cd_mp = mblk; \
2850 2679 (data)->cd_offset = 0; \
2851 2680 (data)->cd_length = msglen; \
2852 2681 }
2853 2682
2854 2683 #define AH_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) { \
2855 2684 (mac)->cd_format = CRYPTO_DATA_RAW; \
2856 2685 (mac)->cd_offset = 0; \
2857 2686 (mac)->cd_length = icvlen; \
2858 2687 (mac)->cd_raw.iov_base = icvbuf; \
2859 2688 (mac)->cd_raw.iov_len = icvlen; \
2860 2689 }
2861 2690
2862 2691 /*
2863 2692 * Submit an inbound packet for processing by the crypto framework.
2864 2693 */
2865 2694 static mblk_t *
2866 2695 ah_submit_req_inbound(mblk_t *phdr_mp, ip_recv_attr_t *ira,
2867 2696 size_t skip_len, uint32_t ah_offset, ipsa_t *assoc)
2868 2697 {
2869 2698 int kef_rc;
2870 2699 mblk_t *mp;
2871 2700 crypto_call_req_t call_req, *callrp;
2872 2701 uint_t icv_len = assoc->ipsa_mac_len;
2873 2702 crypto_ctx_template_t ctx_tmpl;
2874 2703 ipsecah_stack_t *ahstack;
2875 2704 ipsec_crypto_t *ic, icstack;
2876 2705 boolean_t force = (assoc->ipsa_flags & IPSA_F_ASYNC);
2877 2706
2878 2707 ahstack = ira->ira_ill->ill_ipst->ips_netstack->netstack_ipsecah;
2879 2708
2880 2709 ASSERT(phdr_mp != NULL);
2881 2710 ASSERT(phdr_mp->b_datap->db_type == M_DATA);
2882 2711
2883 2712 if (force) {
2884 2713 /* We are doing asynch; allocate mblks to hold state */
2885 2714 if ((mp = ip_recv_attr_to_mblk(ira)) == NULL ||
2886 2715 (mp = ipsec_add_crypto_data(mp, &ic)) == NULL) {
2887 2716 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
2888 2717 ip_drop_input("ipIfStatsInDiscards", phdr_mp,
2889 2718 ira->ira_ill);
2890 2719 freemsg(phdr_mp);
2891 2720 return (NULL);
2892 2721 }
2893 2722
2894 2723 linkb(mp, phdr_mp);
2895 2724 callrp = &call_req;
2896 2725 AH_INIT_CALLREQ(callrp, mp, ah_kcf_callback_inbound);
2897 2726 } else {
2898 2727 /*
2899 2728 * If we know we are going to do sync then ipsec_crypto_t
2900 2729 * should be on the stack.
2901 2730 */
2902 2731 ic = &icstack;
2903 2732 bzero(ic, sizeof (*ic));
2904 2733 callrp = NULL;
2905 2734 }
2906 2735
2907 2736 /* init arguments for the crypto framework */
2908 2737 AH_INIT_CRYPTO_DATA(&ic->ic_crypto_data, AH_MSGSIZE(phdr_mp),
2909 2738 phdr_mp);
2910 2739
2911 2740 AH_INIT_CRYPTO_MAC(&ic->ic_crypto_mac, icv_len,
2912 2741 (char *)phdr_mp->b_cont->b_rptr - skip_len + ah_offset +
2913 2742 sizeof (ah_t));
2914 2743
2915 2744 ic->ic_skip_len = skip_len;
2916 2745
2917 2746 IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH, ctx_tmpl);
2918 2747
2919 2748 /* call KEF to do the MAC operation */
2920 2749 kef_rc = crypto_mac_verify(&assoc->ipsa_amech,
2921 2750 &ic->ic_crypto_data, &assoc->ipsa_kcfauthkey, ctx_tmpl,
2922 2751 &ic->ic_crypto_mac, callrp);
2923 2752
2924 2753 switch (kef_rc) {
2925 2754 case CRYPTO_SUCCESS:
2926 2755 AH_BUMP_STAT(ahstack, crypto_sync);
2927 2756 phdr_mp = ah_auth_in_done(phdr_mp, ira, ic);
2928 2757 if (force) {
2929 2758 /* Free mp after we are done with ic */
2930 2759 mp = ipsec_free_crypto_data(mp);
2931 2760 (void) ip_recv_attr_free_mblk(mp);
2932 2761 }
2933 2762 return (phdr_mp);
2934 2763 case CRYPTO_QUEUED:
2935 2764 /* ah_kcf_callback_inbound() will be invoked on completion */
2936 2765 AH_BUMP_STAT(ahstack, crypto_async);
2937 2766 return (NULL);
2938 2767 case CRYPTO_INVALID_MAC:
2939 2768 /* Free mp after we are done with ic */
2940 2769 AH_BUMP_STAT(ahstack, crypto_sync);
2941 2770 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
2942 2771 ah_log_bad_auth(phdr_mp, ira, ic);
2943 2772 /* phdr_mp was passed to ip_drop_packet */
2944 2773 if (force) {
2945 2774 mp = ipsec_free_crypto_data(mp);
2946 2775 (void) ip_recv_attr_free_mblk(mp);
2947 2776 }
2948 2777 return (NULL);
2949 2778 }
2950 2779
2951 2780 if (force) {
2952 2781 mp = ipsec_free_crypto_data(mp);
2953 2782 phdr_mp = ip_recv_attr_free_mblk(mp);
2954 2783 }
2955 2784 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
2956 2785 ah_crypto_failed(phdr_mp, B_TRUE, kef_rc, ira->ira_ill, ahstack);
2957 2786 /* phdr_mp was passed to ip_drop_packet */
2958 2787 return (NULL);
2959 2788 }
2960 2789
2961 2790 /*
2962 2791 * Submit an outbound packet for processing by the crypto framework.
2963 2792 */
2964 2793 static mblk_t *
2965 2794 ah_submit_req_outbound(mblk_t *phdr_mp, ip_xmit_attr_t *ixa,
2966 2795 size_t skip_len, ipsa_t *assoc)
2967 2796 {
2968 2797 int kef_rc;
2969 2798 mblk_t *mp;
2970 2799 crypto_call_req_t call_req, *callrp;
2971 2800 uint_t icv_len = assoc->ipsa_mac_len;
2972 2801 ipsecah_stack_t *ahstack;
2973 2802 ipsec_crypto_t *ic, icstack;
2974 2803 ill_t *ill = ixa->ixa_nce->nce_ill;
2975 2804 boolean_t force = (assoc->ipsa_flags & IPSA_F_ASYNC);
2976 2805
2977 2806 ahstack = ill->ill_ipst->ips_netstack->netstack_ipsecah;
2978 2807
2979 2808 ASSERT(phdr_mp != NULL);
2980 2809 ASSERT(phdr_mp->b_datap->db_type == M_DATA);
2981 2810
2982 2811 if (force) {
2983 2812 /* We are doing asynch; allocate mblks to hold state */
2984 2813 if ((mp = ip_xmit_attr_to_mblk(ixa)) == NULL ||
2985 2814 (mp = ipsec_add_crypto_data(mp, &ic)) == NULL) {
2986 2815 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
2987 2816 ip_drop_output("ipIfStatsOutDiscards", phdr_mp, ill);
2988 2817 freemsg(phdr_mp);
2989 2818 return (NULL);
2990 2819 }
2991 2820 linkb(mp, phdr_mp);
2992 2821 callrp = &call_req;
2993 2822 AH_INIT_CALLREQ(callrp, mp, ah_kcf_callback_outbound);
2994 2823 } else {
2995 2824 /*
2996 2825 * If we know we are going to do sync then ipsec_crypto_t
2997 2826 * should be on the stack.
2998 2827 */
2999 2828 ic = &icstack;
3000 2829 bzero(ic, sizeof (*ic));
3001 2830 callrp = NULL;
3002 2831 }
3003 2832
3004 2833 /* init arguments for the crypto framework */
3005 2834 AH_INIT_CRYPTO_DATA(&ic->ic_crypto_data, AH_MSGSIZE(phdr_mp),
3006 2835 phdr_mp);
3007 2836
3008 2837 AH_INIT_CRYPTO_MAC(&ic->ic_crypto_mac, icv_len,
3009 2838 (char *)phdr_mp->b_wptr);
3010 2839
3011 2840 ic->ic_skip_len = skip_len;
3012 2841
3013 2842 ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
3014 2843
3015 2844 /* call KEF to do the MAC operation */
3016 2845 kef_rc = crypto_mac(&assoc->ipsa_amech, &ic->ic_crypto_data,
3017 2846 &assoc->ipsa_kcfauthkey, assoc->ipsa_authtmpl,
3018 2847 &ic->ic_crypto_mac, callrp);
3019 2848
3020 2849 switch (kef_rc) {
3021 2850 case CRYPTO_SUCCESS:
3022 2851 AH_BUMP_STAT(ahstack, crypto_sync);
3023 2852 phdr_mp = ah_auth_out_done(phdr_mp, ixa, ic);
3024 2853 if (force) {
3025 2854 /* Free mp after we are done with ic */
3026 2855 mp = ipsec_free_crypto_data(mp);
3027 2856 (void) ip_xmit_attr_free_mblk(mp);
3028 2857 }
3029 2858 return (phdr_mp);
3030 2859 case CRYPTO_QUEUED:
3031 2860 /* ah_kcf_callback_outbound() will be invoked on completion */
3032 2861 AH_BUMP_STAT(ahstack, crypto_async);
3033 2862 return (NULL);
3034 2863 }
3035 2864
3036 2865 if (force) {
3037 2866 mp = ipsec_free_crypto_data(mp);
3038 2867 phdr_mp = ip_xmit_attr_free_mblk(mp);
3039 2868 }
3040 2869 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
3041 2870 ah_crypto_failed(phdr_mp, B_FALSE, kef_rc, NULL, ahstack);
3042 2871 /* phdr_mp was passed to ip_drop_packet */
3043 2872 return (NULL);
3044 2873 }
3045 2874
3046 2875 /*
3047 2876 * This function constructs a pseudo header by looking at the IP header
3048 2877 * and options if any. This is called for both outbound and inbound,
3049 2878 * before computing the ICV.
3050 2879 */
3051 2880 static mblk_t *
3052 2881 ah_process_ip_options_v6(mblk_t *mp, ipsa_t *assoc, int *length_to_skip,
3053 2882 uint_t ah_data_sz, boolean_t outbound, ipsecah_stack_t *ahstack)
3054 2883 {
3055 2884 ip6_t *ip6h;
3056 2885 ip6_t *oip6h;
3057 2886 mblk_t *phdr_mp;
3058 2887 int option_length;
3059 2888 uint_t ah_align_sz;
3060 2889 uint_t ah_offset;
3061 2890 int hdr_size;
3062 2891
3063 2892 /*
3064 2893 * Allocate space for the authentication data also. It is
3065 2894 * useful both during the ICV calculation where we need to
3066 2895 * feed in zeroes and while sending the datagram back to IP
3067 2896 * where we will be using the same space.
3068 2897 *
3069 2898 * We need to allocate space for padding bytes if it is not
3070 2899 * a multiple of IPV6_PADDING_ALIGN.
3071 2900 *
3072 2901 * In addition, we allocate space for the ICV computed by
3073 2902 * the kernel crypto framework, saving us a separate kmem
3074 2903 * allocation down the road.
3075 2904 */
3076 2905
3077 2906 ah_align_sz = P2ALIGN(ah_data_sz + IPV6_PADDING_ALIGN - 1,
3078 2907 IPV6_PADDING_ALIGN);
3079 2908
3080 2909 ASSERT(ah_align_sz >= ah_data_sz);
3081 2910
3082 2911 hdr_size = ipsec_ah_get_hdr_size_v6(mp, B_FALSE);
3083 2912 option_length = hdr_size - IPV6_HDR_LEN;
3084 2913
3085 2914 /* This was not included in ipsec_ah_get_hdr_size_v6() */
3086 2915 hdr_size += (sizeof (ah_t) + ah_align_sz);
3087 2916
3088 2917 if (!outbound && (MBLKL(mp) < hdr_size)) {
3089 2918 /*
3090 2919 * We have post-AH header options in a separate mblk,
3091 2920 * a pullup is required.
3092 2921 */
3093 2922 if (!pullupmsg(mp, hdr_size))
3094 2923 return (NULL);
3095 2924 }
3096 2925
3097 2926 if ((phdr_mp = allocb_tmpl(hdr_size + ah_data_sz, mp)) == NULL) {
3098 2927 return (NULL);
3099 2928 }
3100 2929
3101 2930 oip6h = (ip6_t *)mp->b_rptr;
3102 2931
3103 2932 /*
3104 2933 * Form the basic IP header first. Zero out the header
3105 2934 * so that the mutable fields are zeroed out.
3106 2935 */
3107 2936 ip6h = (ip6_t *)phdr_mp->b_rptr;
3108 2937 bzero(ip6h, sizeof (ip6_t));
3109 2938 ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
3110 2939
3111 2940 if (outbound) {
3112 2941 /*
3113 2942 * Include the size of AH and authentication data.
3114 2943 * This is how our recipient would compute the
3115 2944 * authentication data. Look at what we do in the
3116 2945 * inbound case below.
3117 2946 */
3118 2947 ip6h->ip6_plen = htons(ntohs(oip6h->ip6_plen) +
3119 2948 sizeof (ah_t) + ah_align_sz);
3120 2949 } else {
3121 2950 ip6h->ip6_plen = oip6h->ip6_plen;
3122 2951 }
3123 2952
3124 2953 ip6h->ip6_src = oip6h->ip6_src;
3125 2954 ip6h->ip6_dst = oip6h->ip6_dst;
3126 2955
3127 2956 *length_to_skip = IPV6_HDR_LEN;
3128 2957 if (option_length == 0) {
3129 2958 /* Form the AH header */
3130 2959 ip6h->ip6_nxt = IPPROTO_AH;
3131 2960 ((ah_t *)(ip6h + 1))->ah_nexthdr = oip6h->ip6_nxt;
3132 2961 ah_offset = *length_to_skip;
3133 2962 } else {
3134 2963 ip6h->ip6_nxt = oip6h->ip6_nxt;
3135 2964 /* option_length does not include the AH header's size */
3136 2965 *length_to_skip += option_length;
3137 2966
3138 2967 ah_offset = ah_fix_phdr_v6(ip6h, oip6h, outbound, B_FALSE);
3139 2968 if (ah_offset == 0) {
3140 2969 return (NULL);
3141 2970 }
3142 2971 }
3143 2972
3144 2973 if (!ah_finish_up(((ah_t *)((uint8_t *)ip6h + ah_offset)),
3145 2974 (outbound ? NULL : ((ah_t *)((uint8_t *)oip6h + ah_offset))),
3146 2975 assoc, ah_data_sz, ah_align_sz, ahstack)) {
3147 2976 freeb(phdr_mp);
3148 2977 /*
3149 2978 * Returning NULL will tell the caller to
3150 2979 * IPSA_REFELE(), free the memory, etc.
3151 2980 */
3152 2981 return (NULL);
3153 2982 }
3154 2983
3155 2984 phdr_mp->b_wptr = ((uint8_t *)ip6h + ah_offset + sizeof (ah_t) +
3156 2985 ah_align_sz);
3157 2986 if (!outbound)
3158 2987 *length_to_skip += sizeof (ah_t) + ah_align_sz;
3159 2988 return (phdr_mp);
3160 2989 }
3161 2990
3162 2991 /*
3163 2992 * This function constructs a pseudo header by looking at the IP header
3164 2993 * and options if any. This is called for both outbound and inbound,
3165 2994 * before computing the ICV.
3166 2995 */
3167 2996 static mblk_t *
3168 2997 ah_process_ip_options_v4(mblk_t *mp, ipsa_t *assoc, int *length_to_skip,
3169 2998 uint_t ah_data_sz, boolean_t outbound, ipsecah_stack_t *ahstack)
3170 2999 {
3171 3000 ipoptp_t opts;
3172 3001 uint32_t option_length;
3173 3002 ipha_t *ipha;
3174 3003 ipha_t *oipha;
3175 3004 mblk_t *phdr_mp;
3176 3005 int size;
3177 3006 uchar_t *optptr;
3178 3007 uint8_t optval;
3179 3008 uint8_t optlen;
3180 3009 ipaddr_t dst;
3181 3010 uint32_t v_hlen_tos_len;
3182 3011 int ip_hdr_length;
3183 3012 uint_t ah_align_sz;
3184 3013 uint32_t off;
3185 3014
3186 3015 #ifdef _BIG_ENDIAN
3187 3016 #define V_HLEN (v_hlen_tos_len >> 24)
3188 3017 #else
3189 3018 #define V_HLEN (v_hlen_tos_len & 0xFF)
3190 3019 #endif
3191 3020
3192 3021 oipha = (ipha_t *)mp->b_rptr;
3193 3022 v_hlen_tos_len = ((uint32_t *)oipha)[0];
3194 3023
3195 3024 /*
3196 3025 * Allocate space for the authentication data also. It is
3197 3026 * useful both during the ICV calculation where we need to
3198 3027 * feed in zeroes and while sending the datagram back to IP
3199 3028 * where we will be using the same space.
3200 3029 *
3201 3030 * We need to allocate space for padding bytes if it is not
3202 3031 * a multiple of IPV4_PADDING_ALIGN.
3203 3032 *
3204 3033 * In addition, we allocate space for the ICV computed by
3205 3034 * the kernel crypto framework, saving us a separate kmem
3206 3035 * allocation down the road.
3207 3036 */
3208 3037
3209 3038 ah_align_sz = P2ALIGN(ah_data_sz + IPV4_PADDING_ALIGN - 1,
3210 3039 IPV4_PADDING_ALIGN);
3211 3040
3212 3041 ASSERT(ah_align_sz >= ah_data_sz);
3213 3042
3214 3043 size = IP_SIMPLE_HDR_LENGTH + sizeof (ah_t) + ah_align_sz +
3215 3044 ah_data_sz;
3216 3045
3217 3046 if (V_HLEN != IP_SIMPLE_HDR_VERSION) {
3218 3047 option_length = oipha->ipha_version_and_hdr_length -
3219 3048 (uint8_t)((IP_VERSION << 4) +
3220 3049 IP_SIMPLE_HDR_LENGTH_IN_WORDS);
3221 3050 option_length <<= 2;
3222 3051 size += option_length;
3223 3052 }
3224 3053
3225 3054 if ((phdr_mp = allocb_tmpl(size, mp)) == NULL) {
3226 3055 return (NULL);
3227 3056 }
3228 3057
3229 3058 /*
3230 3059 * Form the basic IP header first.
3231 3060 */
3232 3061 ipha = (ipha_t *)phdr_mp->b_rptr;
3233 3062 ipha->ipha_version_and_hdr_length = oipha->ipha_version_and_hdr_length;
3234 3063 ipha->ipha_type_of_service = 0;
3235 3064
3236 3065 if (outbound) {
3237 3066 /*
3238 3067 * Include the size of AH and authentication data.
3239 3068 * This is how our recipient would compute the
3240 3069 * authentication data. Look at what we do in the
3241 3070 * inbound case below.
3242 3071 */
3243 3072 ipha->ipha_length = ntohs(htons(oipha->ipha_length) +
3244 3073 sizeof (ah_t) + ah_align_sz);
3245 3074 } else {
3246 3075 ipha->ipha_length = oipha->ipha_length;
3247 3076 }
3248 3077
3249 3078 ipha->ipha_ident = oipha->ipha_ident;
3250 3079 ipha->ipha_fragment_offset_and_flags = 0;
3251 3080 ipha->ipha_ttl = 0;
3252 3081 ipha->ipha_protocol = IPPROTO_AH;
3253 3082 ipha->ipha_hdr_checksum = 0;
3254 3083 ipha->ipha_src = oipha->ipha_src;
3255 3084 ipha->ipha_dst = dst = oipha->ipha_dst;
3256 3085
3257 3086 /*
3258 3087 * If there is no option to process return now.
3259 3088 */
3260 3089 ip_hdr_length = IP_SIMPLE_HDR_LENGTH;
3261 3090
3262 3091 if (V_HLEN == IP_SIMPLE_HDR_VERSION) {
3263 3092 /* Form the AH header */
3264 3093 goto ah_hdr;
3265 3094 }
3266 3095
3267 3096 ip_hdr_length += option_length;
3268 3097
3269 3098 /*
3270 3099 * We have options. In the outbound case for source route,
3271 3100 * ULP has already moved the first hop, which is now in
3272 3101 * ipha_dst. We need the final destination for the calculation
3273 3102 * of authentication data. And also make sure that mutable
3274 3103 * and experimental fields are zeroed out in the IP options.
3275 3104 */
3276 3105
3277 3106 bcopy(&oipha[1], &ipha[1], option_length);
3278 3107
3279 3108 for (optval = ipoptp_first(&opts, ipha);
3280 3109 optval != IPOPT_EOL;
3281 3110 optval = ipoptp_next(&opts)) {
3282 3111 optptr = opts.ipoptp_cur;
3283 3112 optlen = opts.ipoptp_len;
3284 3113 switch (optval) {
3285 3114 case IPOPT_EXTSEC:
3286 3115 case IPOPT_COMSEC:
3287 3116 case IPOPT_RA:
3288 3117 case IPOPT_SDMDD:
3289 3118 case IPOPT_SECURITY:
3290 3119 /*
3291 3120 * These options are Immutable, leave them as-is.
3292 3121 * Note that IPOPT_NOP is also Immutable, but it
3293 3122 * was skipped by ipoptp_next() and thus remains
3294 3123 * intact in the header.
3295 3124 */
3296 3125 break;
3297 3126 case IPOPT_SSRR:
3298 3127 case IPOPT_LSRR:
3299 3128 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0)
3300 3129 goto bad_ipv4opt;
3301 3130 /*
3302 3131 * These two are mutable and will be zeroed, but
3303 3132 * first get the final destination.
3304 3133 */
3305 3134 off = optptr[IPOPT_OFFSET];
3306 3135 /*
3307 3136 * If one of the conditions is true, it means
3308 3137 * end of options and dst already has the right
3309 3138 * value. So, just fall through.
3310 3139 */
3311 3140 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
3312 3141 off = optlen - IP_ADDR_LEN;
3313 3142 bcopy(&optptr[off], &dst, IP_ADDR_LEN);
3314 3143 }
3315 3144 /* FALLTHRU */
3316 3145 case IPOPT_RR:
3317 3146 case IPOPT_TS:
3318 3147 case IPOPT_SATID:
3319 3148 default:
3320 3149 /*
3321 3150 * optlen should include from the beginning of an
3322 3151 * option.
3323 3152 * NOTE : Stream Identifier Option (SID): RFC 791
3324 3153 * shows the bit pattern of optlen as 2 and documents
3325 3154 * the length as 4. We assume it to be 2 here.
3326 3155 */
3327 3156 bzero(optptr, optlen);
3328 3157 break;
3329 3158 }
3330 3159 }
3331 3160
3332 3161 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
3333 3162 bad_ipv4opt:
3334 3163 ah1dbg(ahstack, ("AH : bad IPv4 option"));
3335 3164 freeb(phdr_mp);
3336 3165 return (NULL);
3337 3166 }
3338 3167
3339 3168 /*
3340 3169 * Don't change ipha_dst for an inbound datagram as it points
3341 3170 * to the right value. Only for the outbound with LSRR/SSRR,
3342 3171 * because of ip_massage_options called by the ULP, ipha_dst
3343 3172 * points to the first hop and we need to use the final
3344 3173 * destination for computing the ICV.
3345 3174 */
3346 3175
3347 3176 if (outbound)
3348 3177 ipha->ipha_dst = dst;
3349 3178 ah_hdr:
3350 3179 ((ah_t *)((uint8_t *)ipha + ip_hdr_length))->ah_nexthdr =
3351 3180 oipha->ipha_protocol;
3352 3181 if (!ah_finish_up(((ah_t *)((uint8_t *)ipha + ip_hdr_length)),
3353 3182 (outbound ? NULL : ((ah_t *)((uint8_t *)oipha + ip_hdr_length))),
3354 3183 assoc, ah_data_sz, ah_align_sz, ahstack)) {
3355 3184 freeb(phdr_mp);
3356 3185 /*
3357 3186 * Returning NULL will tell the caller to IPSA_REFELE(), free
3358 3187 * the memory, etc.
3359 3188 */
3360 3189 return (NULL);
3361 3190 }
3362 3191
3363 3192 phdr_mp->b_wptr = ((uchar_t *)ipha + ip_hdr_length +
3364 3193 sizeof (ah_t) + ah_align_sz);
3365 3194
3366 3195 ASSERT(phdr_mp->b_wptr <= phdr_mp->b_datap->db_lim);
3367 3196 if (outbound)
3368 3197 *length_to_skip = ip_hdr_length;
3369 3198 else
3370 3199 *length_to_skip = ip_hdr_length + sizeof (ah_t) + ah_align_sz;
3371 3200 return (phdr_mp);
3372 3201 }
3373 3202
3374 3203 /*
3375 3204 * Authenticate an outbound datagram. This function is called
3376 3205 * whenever IP sends an outbound datagram that needs authentication.
3377 3206 * Returns a modified packet if done. Returns NULL if error or queued.
3378 3207 * If error return then ipIfStatsOutDiscards has been increased.
3379 3208 */
3380 3209 static mblk_t *
3381 3210 ah_outbound(mblk_t *data_mp, ip_xmit_attr_t *ixa)
3382 3211 {
3383 3212 mblk_t *phdr_mp;
3384 3213 ipsa_t *assoc;
3385 3214 int length_to_skip;
3386 3215 uint_t ah_align_sz;
3387 3216 uint_t age_bytes;
3388 3217 netstack_t *ns = ixa->ixa_ipst->ips_netstack;
3389 3218 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
3390 3219 ipsec_stack_t *ipss = ns->netstack_ipsec;
3391 3220 ill_t *ill = ixa->ixa_nce->nce_ill;
3392 3221 boolean_t need_refrele = B_FALSE;
3393 3222
3394 3223 /*
3395 3224 * Construct the chain of mblks
3396 3225 *
3397 3226 * PSEUDO_HDR->DATA
3398 3227 *
3399 3228 * one by one.
3400 3229 */
3401 3230
3402 3231 AH_BUMP_STAT(ahstack, out_requests);
3403 3232
3404 3233 ASSERT(data_mp->b_datap->db_type == M_DATA);
3405 3234
3406 3235 assoc = ixa->ixa_ipsec_ah_sa;
3407 3236 ASSERT(assoc != NULL);
3408 3237
3409 3238
3410 3239 /*
3411 3240 * Get the outer IP header in shape to escape this system..
3412 3241 */
3413 3242 if (is_system_labeled() && (assoc->ipsa_otsl != NULL)) {
3414 3243 /*
3415 3244 * Need to update packet with any CIPSO option and update
3416 3245 * ixa_tsl to capture the new label.
3417 3246 * We allocate a separate ixa for that purpose.
3418 3247 */
3419 3248 ixa = ip_xmit_attr_duplicate(ixa);
3420 3249 if (ixa == NULL) {
3421 3250 ip_drop_packet(data_mp, B_FALSE, ill,
3422 3251 DROPPER(ipss, ipds_ah_nomem),
3423 3252 &ahstack->ah_dropper);
3424 3253 return (NULL);
3425 3254 }
3426 3255 need_refrele = B_TRUE;
3427 3256
3428 3257 label_hold(assoc->ipsa_otsl);
3429 3258 ip_xmit_attr_replace_tsl(ixa, assoc->ipsa_otsl);
3430 3259
3431 3260 data_mp = sadb_whack_label(data_mp, assoc, ixa,
3432 3261 DROPPER(ipss, ipds_ah_nomem), &ahstack->ah_dropper);
3433 3262 if (data_mp == NULL) {
3434 3263 /* Packet dropped by sadb_whack_label */
3435 3264 ixa_refrele(ixa);
3436 3265 return (NULL);
3437 3266 }
3438 3267 }
3439 3268
3440 3269 /*
3441 3270 * Age SA according to number of bytes that will be sent after
3442 3271 * adding the AH header, ICV, and padding to the packet.
3443 3272 */
3444 3273
3445 3274 if (ixa->ixa_flags & IXAF_IS_IPV4) {
3446 3275 ipha_t *ipha = (ipha_t *)data_mp->b_rptr;
3447 3276 ah_align_sz = P2ALIGN(assoc->ipsa_mac_len +
3448 3277 IPV4_PADDING_ALIGN - 1, IPV4_PADDING_ALIGN);
3449 3278 age_bytes = ntohs(ipha->ipha_length) + sizeof (ah_t) +
3450 3279 ah_align_sz;
3451 3280 } else {
3452 3281 ip6_t *ip6h = (ip6_t *)data_mp->b_rptr;
3453 3282 ah_align_sz = P2ALIGN(assoc->ipsa_mac_len +
3454 3283 IPV6_PADDING_ALIGN - 1, IPV6_PADDING_ALIGN);
3455 3284 age_bytes = sizeof (ip6_t) + ntohs(ip6h->ip6_plen) +
3456 3285 sizeof (ah_t) + ah_align_sz;
3457 3286 }
3458 3287
3459 3288 if (!ah_age_bytes(assoc, age_bytes, B_FALSE)) {
3460 3289 /* rig things as if ipsec_getassocbyconn() failed */
3461 3290 ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
3462 3291 "AH association 0x%x, dst %s had bytes expire.\n",
3463 3292 ntohl(assoc->ipsa_spi), assoc->ipsa_dstaddr, AF_INET,
3464 3293 ahstack->ipsecah_netstack);
3465 3294 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
3466 3295 ip_drop_output("ipIfStatsOutDiscards", data_mp, ill);
3467 3296 freemsg(data_mp);
3468 3297 if (need_refrele)
3469 3298 ixa_refrele(ixa);
3470 3299 return (NULL);
3471 3300 }
3472 3301
3473 3302 /*
3474 3303 * XXX We need to have fixed up the outer label before we get here.
3475 3304 * (AH is computing the checksum over the outer label).
3476 3305 */
3477 3306
3478 3307 /*
3479 3308 * Insert pseudo header:
3480 3309 * [IP, ULP] => [IP, AH, ICV] -> ULP
3481 3310 */
3482 3311
3483 3312 if (ixa->ixa_flags & IXAF_IS_IPV4) {
3484 3313 phdr_mp = ah_process_ip_options_v4(data_mp, assoc,
3485 3314 &length_to_skip, assoc->ipsa_mac_len, B_TRUE, ahstack);
3486 3315 } else {
3487 3316 phdr_mp = ah_process_ip_options_v6(data_mp, assoc,
3488 3317 &length_to_skip, assoc->ipsa_mac_len, B_TRUE, ahstack);
3489 3318 }
3490 3319
3491 3320 if (phdr_mp == NULL) {
3492 3321 AH_BUMP_STAT(ahstack, out_discards);
3493 3322 ip_drop_packet(data_mp, B_FALSE, ixa->ixa_nce->nce_ill,
3494 3323 DROPPER(ipss, ipds_ah_bad_v4_opts),
3495 3324 &ahstack->ah_dropper);
3496 3325 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
3497 3326 if (need_refrele)
3498 3327 ixa_refrele(ixa);
3499 3328 return (NULL);
3500 3329 }
3501 3330
3502 3331 phdr_mp->b_cont = data_mp;
3503 3332 data_mp->b_rptr += length_to_skip;
3504 3333 data_mp = phdr_mp;
3505 3334
3506 3335 /*
3507 3336 * At this point data_mp points to
3508 3337 * an mblk containing the pseudo header (IP header,
3509 3338 * AH header, and ICV with mutable fields zero'ed out).
3510 3339 * mp points to the mblk containing the ULP data. The original
3511 3340 * IP header is kept before the ULP data in data_mp.
3512 3341 */
3513 3342
3514 3343 /* submit MAC request to KCF */
3515 3344 data_mp = ah_submit_req_outbound(data_mp, ixa, length_to_skip, assoc);
3516 3345 if (need_refrele)
3517 3346 ixa_refrele(ixa);
3518 3347 return (data_mp);
3519 3348 }
3520 3349
3521 3350 static mblk_t *
3522 3351 ah_inbound(mblk_t *data_mp, void *arg, ip_recv_attr_t *ira)
3523 3352 {
3524 3353 ah_t *ah = (ah_t *)arg;
3525 3354 ipsa_t *assoc = ira->ira_ipsec_ah_sa;
3526 3355 int length_to_skip;
3527 3356 int ah_length;
3528 3357 mblk_t *phdr_mp;
3529 3358 uint32_t ah_offset;
3530 3359 netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
3531 3360 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
3532 3361 ipsec_stack_t *ipss = ns->netstack_ipsec;
3533 3362
3534 3363 ASSERT(assoc != NULL);
3535 3364
3536 3365 /*
3537 3366 * We may wish to check replay in-range-only here as an optimization.
3538 3367 * Include the reality check of ipsa->ipsa_replay >
3539 3368 * ipsa->ipsa_replay_wsize for times when it's the first N packets,
3540 3369 * where N == ipsa->ipsa_replay_wsize.
3541 3370 *
3542 3371 * Another check that may come here later is the "collision" check.
3543 3372 * If legitimate packets flow quickly enough, this won't be a problem,
3544 3373 * but collisions may cause authentication algorithm crunching to
3545 3374 * take place when it doesn't need to.
3546 3375 */
3547 3376 if (!sadb_replay_peek(assoc, ah->ah_replay)) {
3548 3377 AH_BUMP_STAT(ahstack, replay_early_failures);
3549 3378 IP_AH_BUMP_STAT(ipss, in_discards);
3550 3379 ip_drop_packet(data_mp, B_TRUE, ira->ira_ill,
3551 3380 DROPPER(ipss, ipds_ah_early_replay),
3552 3381 &ahstack->ah_dropper);
3553 3382 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3554 3383 return (NULL);
3555 3384 }
3556 3385
3557 3386 /*
3558 3387 * The offset of the AH header can be computed from its pointer
3559 3388 * within the data mblk, which was pulled up until the AH header
3560 3389 * by ipsec_inbound_ah_sa() during SA selection.
3561 3390 */
3562 3391 ah_offset = (uchar_t *)ah - data_mp->b_rptr;
3563 3392
3564 3393 /*
3565 3394 * We need to pullup until the ICV before we call
3566 3395 * ah_process_ip_options_v6.
3567 3396 */
3568 3397 ah_length = (ah->ah_length << 2) + 8;
3569 3398
3570 3399 /*
3571 3400 * NOTE : If we want to use any field of IP/AH header, you need
3572 3401 * to re-assign following the pullup.
3573 3402 */
3574 3403 if (((uchar_t *)ah + ah_length) > data_mp->b_wptr) {
3575 3404 if (!pullupmsg(data_mp, (uchar_t *)ah + ah_length -
3576 3405 data_mp->b_rptr)) {
3577 3406 (void) ipsec_rl_strlog(ns, info.mi_idnum, 0, 0,
3578 3407 SL_WARN | SL_ERROR,
3579 3408 "ah_inbound: Small AH header\n");
3580 3409 IP_AH_BUMP_STAT(ipss, in_discards);
3581 3410 ip_drop_packet(data_mp, B_TRUE, ira->ira_ill,
3582 3411 DROPPER(ipss, ipds_ah_nomem),
3583 3412 &ahstack->ah_dropper);
3584 3413 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3585 3414 return (NULL);
3586 3415 }
3587 3416 }
3588 3417
3589 3418 /*
3590 3419 * Insert pseudo header:
3591 3420 * [IP, ULP] => [IP, AH, ICV] -> ULP
3592 3421 */
3593 3422 if (ira->ira_flags & IRAF_IS_IPV4) {
3594 3423 phdr_mp = ah_process_ip_options_v4(data_mp, assoc,
3595 3424 &length_to_skip, assoc->ipsa_mac_len, B_FALSE, ahstack);
3596 3425 } else {
3597 3426 phdr_mp = ah_process_ip_options_v6(data_mp, assoc,
3598 3427 &length_to_skip, assoc->ipsa_mac_len, B_FALSE, ahstack);
3599 3428 }
3600 3429
3601 3430 if (phdr_mp == NULL) {
3602 3431 IP_AH_BUMP_STAT(ipss, in_discards);
3603 3432 ip_drop_packet(data_mp, B_TRUE, ira->ira_ill,
3604 3433 ((ira->ira_flags & IRAF_IS_IPV4) ?
3605 3434 DROPPER(ipss, ipds_ah_bad_v4_opts) :
3606 3435 DROPPER(ipss, ipds_ah_bad_v6_hdrs)),
3607 3436 &ahstack->ah_dropper);
3608 3437 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3609 3438 return (NULL);
3610 3439 }
3611 3440
3612 3441 phdr_mp->b_cont = data_mp;
3613 3442 data_mp->b_rptr += length_to_skip;
3614 3443 data_mp = phdr_mp;
3615 3444
3616 3445 /* submit request to KCF */
3617 3446 return (ah_submit_req_inbound(data_mp, ira, length_to_skip, ah_offset,
3618 3447 assoc));
3619 3448 }
3620 3449
3621 3450 /*
3622 3451 * Invoked after processing of an inbound packet by the
3623 3452 * kernel crypto framework. Called by ah_submit_req() for a sync request,
3624 3453 * or by the kcf callback for an async request.
3625 3454 * Returns NULL if the mblk chain is consumed.
3626 3455 */
3627 3456 static mblk_t *
3628 3457 ah_auth_in_done(mblk_t *phdr_mp, ip_recv_attr_t *ira, ipsec_crypto_t *ic)
3629 3458 {
3630 3459 ipha_t *ipha;
3631 3460 uint_t ah_offset = 0;
3632 3461 mblk_t *mp;
3633 3462 int align_len, newpos;
3634 3463 ah_t *ah;
3635 3464 uint32_t length;
3636 3465 uint32_t *dest32;
3637 3466 uint8_t *dest;
3638 3467 boolean_t isv4;
3639 3468 ip6_t *ip6h;
3640 3469 uint_t icv_len;
3641 3470 ipsa_t *assoc;
3642 3471 kstat_named_t *counter;
3643 3472 netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
3644 3473 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
3645 3474 ipsec_stack_t *ipss = ns->netstack_ipsec;
3646 3475
3647 3476 isv4 = (ira->ira_flags & IRAF_IS_IPV4);
3648 3477 assoc = ira->ira_ipsec_ah_sa;
3649 3478 icv_len = (uint_t)ic->ic_crypto_mac.cd_raw.iov_len;
3650 3479
3651 3480 if (phdr_mp == NULL) {
3652 3481 ip_drop_packet(phdr_mp, B_TRUE, ira->ira_ill,
3653 3482 DROPPER(ipss, ipds_ah_nomem),
3654 3483 &ahstack->ah_dropper);
3655 3484 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3656 3485 return (NULL);
3657 3486 }
3658 3487
3659 3488 mp = phdr_mp->b_cont;
3660 3489 if (mp == NULL) {
3661 3490 ip_drop_packet(phdr_mp, B_TRUE, ira->ira_ill,
3662 3491 DROPPER(ipss, ipds_ah_nomem),
3663 3492 &ahstack->ah_dropper);
3664 3493 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3665 3494 return (NULL);
3666 3495 }
3667 3496 mp->b_rptr -= ic->ic_skip_len;
3668 3497
3669 3498 ah_set_usetime(assoc, B_TRUE);
3670 3499
3671 3500 if (isv4) {
3672 3501 ipha = (ipha_t *)mp->b_rptr;
3673 3502 ah_offset = ipha->ipha_version_and_hdr_length -
3674 3503 (uint8_t)((IP_VERSION << 4));
3675 3504 ah_offset <<= 2;
3676 3505 align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1,
3677 3506 IPV4_PADDING_ALIGN);
3678 3507 } else {
3679 3508 ip6h = (ip6_t *)mp->b_rptr;
3680 3509 ah_offset = ipsec_ah_get_hdr_size_v6(mp, B_TRUE);
3681 3510 ASSERT((mp->b_wptr - mp->b_rptr) >= ah_offset);
3682 3511 align_len = P2ALIGN(icv_len + IPV6_PADDING_ALIGN - 1,
3683 3512 IPV6_PADDING_ALIGN);
3684 3513 }
3685 3514
3686 3515 ah = (ah_t *)(mp->b_rptr + ah_offset);
3687 3516 newpos = sizeof (ah_t) + align_len;
3688 3517
3689 3518 /*
3690 3519 * We get here only when authentication passed.
3691 3520 */
3692 3521
3693 3522 ah3dbg(ahstack, ("AH succeeded, checking replay\n"));
3694 3523 AH_BUMP_STAT(ahstack, good_auth);
3695 3524
3696 3525 if (!sadb_replay_check(assoc, ah->ah_replay)) {
3697 3526 int af;
3698 3527 void *addr;
3699 3528
3700 3529 if (isv4) {
3701 3530 addr = &ipha->ipha_dst;
3702 3531 af = AF_INET;
3703 3532 } else {
3704 3533 addr = &ip6h->ip6_dst;
3705 3534 af = AF_INET6;
3706 3535 }
3707 3536
3708 3537 /*
3709 3538 * Log the event. As of now we print out an event.
3710 3539 * Do not print the replay failure number, or else
3711 3540 * syslog cannot collate the error messages. Printing
3712 3541 * the replay number that failed (or printing to the
3713 3542 * console) opens a denial-of-service attack.
3714 3543 */
3715 3544 AH_BUMP_STAT(ahstack, replay_failures);
3716 3545 ipsec_assocfailure(info.mi_idnum, 0, 0,
3717 3546 SL_ERROR | SL_WARN,
3718 3547 "Replay failed for AH spi %x, dst_addr %s",
3719 3548 assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
3720 3549 counter = DROPPER(ipss, ipds_ah_replay);
3721 3550 goto ah_in_discard;
3722 3551 }
3723 3552
3724 3553 /*
3725 3554 * We need to remove the AH header from the original
3726 3555 * datagram. Best way to do this is to move the pre-AH headers
3727 3556 * forward in the (relatively simple) IPv4 case. In IPv6, it's
3728 3557 * a bit more complicated because of IPv6's next-header chaining,
3729 3558 * but it's doable.
3730 3559 */
3731 3560 if (isv4) {
3732 3561 /*
3733 3562 * Assign the right protocol, adjust the length as we
3734 3563 * are removing the AH header and adjust the checksum to
3735 3564 * account for the protocol and length.
3736 3565 */
3737 3566 length = ntohs(ipha->ipha_length);
3738 3567 if (!ah_age_bytes(assoc, length, B_TRUE)) {
3739 3568 /* The ipsa has hit hard expiration, LOG and AUDIT. */
3740 3569 ipsec_assocfailure(info.mi_idnum, 0, 0,
3741 3570 SL_ERROR | SL_WARN,
3742 3571 "AH Association 0x%x, dst %s had bytes expire.\n",
3743 3572 assoc->ipsa_spi, assoc->ipsa_dstaddr,
3744 3573 AF_INET, ahstack->ipsecah_netstack);
3745 3574 AH_BUMP_STAT(ahstack, bytes_expired);
3746 3575 counter = DROPPER(ipss, ipds_ah_bytes_expire);
3747 3576 goto ah_in_discard;
3748 3577 }
3749 3578 ipha->ipha_protocol = ah->ah_nexthdr;
3750 3579 length -= newpos;
3751 3580
3752 3581 ipha->ipha_length = htons((uint16_t)length);
3753 3582 ipha->ipha_hdr_checksum = 0;
3754 3583 ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
3755 3584 } else {
3756 3585 uchar_t *whereptr;
3757 3586 int hdrlen;
3758 3587 uint8_t *nexthdr;
3759 3588 ip6_hbh_t *hbhhdr;
3760 3589 ip6_dest_t *dsthdr;
3761 3590 ip6_rthdr0_t *rthdr;
3762 3591
3763 3592 /*
3764 3593 * Make phdr_mp hold until the AH header and make
3765 3594 * mp hold everything past AH header.
3766 3595 */
3767 3596 length = ntohs(ip6h->ip6_plen);
3768 3597 if (!ah_age_bytes(assoc, length + sizeof (ip6_t), B_TRUE)) {
3769 3598 /* The ipsa has hit hard expiration, LOG and AUDIT. */
3770 3599 ipsec_assocfailure(info.mi_idnum, 0, 0,
3771 3600 SL_ERROR | SL_WARN,
3772 3601 "AH Association 0x%x, dst %s had bytes "
3773 3602 "expire.\n", assoc->ipsa_spi, &ip6h->ip6_dst,
3774 3603 AF_INET6, ahstack->ipsecah_netstack);
3775 3604 AH_BUMP_STAT(ahstack, bytes_expired);
3776 3605 counter = DROPPER(ipss, ipds_ah_bytes_expire);
3777 3606 goto ah_in_discard;
3778 3607 }
3779 3608
3780 3609 /*
3781 3610 * Update the next header field of the header preceding
3782 3611 * AH with the next header field of AH. Start with the
3783 3612 * IPv6 header and proceed with the extension headers
3784 3613 * until we find what we're looking for.
3785 3614 */
3786 3615 nexthdr = &ip6h->ip6_nxt;
3787 3616 whereptr = (uchar_t *)ip6h;
3788 3617 hdrlen = sizeof (ip6_t);
3789 3618
3790 3619 while (*nexthdr != IPPROTO_AH) {
3791 3620 whereptr += hdrlen;
3792 3621 /* Assume IP has already stripped it */
3793 3622 ASSERT(*nexthdr != IPPROTO_FRAGMENT);
3794 3623 switch (*nexthdr) {
3795 3624 case IPPROTO_HOPOPTS:
3796 3625 hbhhdr = (ip6_hbh_t *)whereptr;
3797 3626 nexthdr = &hbhhdr->ip6h_nxt;
3798 3627 hdrlen = 8 * (hbhhdr->ip6h_len + 1);
3799 3628 break;
3800 3629 case IPPROTO_DSTOPTS:
3801 3630 dsthdr = (ip6_dest_t *)whereptr;
3802 3631 nexthdr = &dsthdr->ip6d_nxt;
3803 3632 hdrlen = 8 * (dsthdr->ip6d_len + 1);
3804 3633 break;
3805 3634 case IPPROTO_ROUTING:
3806 3635 rthdr = (ip6_rthdr0_t *)whereptr;
3807 3636 nexthdr = &rthdr->ip6r0_nxt;
3808 3637 hdrlen = 8 * (rthdr->ip6r0_len + 1);
3809 3638 break;
3810 3639 }
3811 3640 }
3812 3641 *nexthdr = ah->ah_nexthdr;
3813 3642 length -= newpos;
3814 3643 ip6h->ip6_plen = htons((uint16_t)length);
3815 3644 }
3816 3645
3817 3646 /* Now that we've fixed the IP header, move it forward. */
3818 3647 mp->b_rptr += newpos;
3819 3648 if (IS_P2ALIGNED(mp->b_rptr, sizeof (uint32_t))) {
3820 3649 dest32 = (uint32_t *)(mp->b_rptr + ah_offset);
3821 3650 while (--dest32 >= (uint32_t *)mp->b_rptr)
3822 3651 *dest32 = *(dest32 - (newpos >> 2));
3823 3652 } else {
3824 3653 dest = mp->b_rptr + ah_offset;
3825 3654 while (--dest >= mp->b_rptr)
3826 3655 *dest = *(dest - newpos);
3827 3656 }
3828 3657 freeb(phdr_mp);
3829 3658
3830 3659 /*
3831 3660 * If SA is labelled, use its label, else inherit the label
3832 3661 */
3833 3662 if (is_system_labeled() && (assoc->ipsa_tsl != NULL)) {
3834 3663 if (!ip_recv_attr_replace_label(ira, assoc->ipsa_tsl)) {
3835 3664 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
3836 3665 DROPPER(ipss, ipds_ah_nomem), &ahstack->ah_dropper);
3837 3666 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3838 3667 return (NULL);
3839 3668 }
3840 3669 }
3841 3670
3842 3671 if (assoc->ipsa_state == IPSA_STATE_IDLE) {
3843 3672 /*
3844 3673 * Cluster buffering case. Tell caller that we're
3845 3674 * handling the packet.
3846 3675 */
3847 3676 sadb_buf_pkt(assoc, mp, ira);
3848 3677 return (NULL);
3849 3678 }
3850 3679
3851 3680 return (mp);
3852 3681
3853 3682 ah_in_discard:
3854 3683 IP_AH_BUMP_STAT(ipss, in_discards);
3855 3684 ip_drop_packet(phdr_mp, B_TRUE, ira->ira_ill, counter,
3856 3685 &ahstack->ah_dropper);
3857 3686 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3858 3687 return (NULL);
3859 3688 }
3860 3689
3861 3690 /*
3862 3691 * Invoked after processing of an outbound packet by the
3863 3692 * kernel crypto framework, either by ah_submit_req() for a request
3864 3693 * executed syncrhonously, or by the KEF callback for a request
3865 3694 * executed asynchronously.
3866 3695 */
3867 3696 static mblk_t *
3868 3697 ah_auth_out_done(mblk_t *phdr_mp, ip_xmit_attr_t *ixa, ipsec_crypto_t *ic)
3869 3698 {
3870 3699 mblk_t *mp;
3871 3700 int align_len;
3872 3701 uint32_t hdrs_length;
3873 3702 uchar_t *ptr;
3874 3703 uint32_t length;
3875 3704 boolean_t isv4;
3876 3705 size_t icv_len;
3877 3706 netstack_t *ns = ixa->ixa_ipst->ips_netstack;
3878 3707 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
3879 3708 ipsec_stack_t *ipss = ns->netstack_ipsec;
3880 3709 ill_t *ill = ixa->ixa_nce->nce_ill;
3881 3710
3882 3711 isv4 = (ixa->ixa_flags & IXAF_IS_IPV4);
3883 3712 icv_len = ic->ic_crypto_mac.cd_raw.iov_len;
3884 3713
3885 3714 mp = phdr_mp->b_cont;
3886 3715 if (mp == NULL) {
3887 3716 ip_drop_packet(phdr_mp, B_FALSE, ill,
3888 3717 DROPPER(ipss, ipds_ah_nomem),
3889 3718 &ahstack->ah_dropper);
3890 3719 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
3891 3720 return (NULL);
3892 3721 }
3893 3722 mp->b_rptr -= ic->ic_skip_len;
3894 3723
3895 3724 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
3896 3725 ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
3897 3726 ah_set_usetime(ixa->ixa_ipsec_ah_sa, B_FALSE);
3898 3727
3899 3728 if (isv4) {
3900 3729 ipha_t *ipha;
3901 3730 ipha_t *nipha;
3902 3731
3903 3732 ipha = (ipha_t *)mp->b_rptr;
3904 3733 hdrs_length = ipha->ipha_version_and_hdr_length -
3905 3734 (uint8_t)((IP_VERSION << 4));
3906 3735 hdrs_length <<= 2;
3907 3736 align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1,
3908 3737 IPV4_PADDING_ALIGN);
3909 3738 /*
3910 3739 * phdr_mp must have the right amount of space for the
3911 3740 * combined IP and AH header. Copy the IP header and
3912 3741 * the ack_data onto AH. Note that the AH header was
3913 3742 * already formed before the ICV calculation and hence
3914 3743 * you don't have to copy it here.
3915 3744 */
3916 3745 bcopy(mp->b_rptr, phdr_mp->b_rptr, hdrs_length);
3917 3746
3918 3747 ptr = phdr_mp->b_rptr + hdrs_length + sizeof (ah_t);
3919 3748 bcopy(phdr_mp->b_wptr, ptr, icv_len);
3920 3749
3921 3750 /*
3922 3751 * Compute the new header checksum as we are assigning
3923 3752 * IPPROTO_AH and adjusting the length here.
3924 3753 */
3925 3754 nipha = (ipha_t *)phdr_mp->b_rptr;
3926 3755
3927 3756 nipha->ipha_protocol = IPPROTO_AH;
3928 3757 length = ntohs(nipha->ipha_length);
3929 3758 length += (sizeof (ah_t) + align_len);
3930 3759 nipha->ipha_length = htons((uint16_t)length);
3931 3760 nipha->ipha_hdr_checksum = 0;
3932 3761 nipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(nipha);
3933 3762 } else {
3934 3763 ip6_t *ip6h;
3935 3764 ip6_t *nip6h;
3936 3765 uint_t ah_offset;
3937 3766
3938 3767 ip6h = (ip6_t *)mp->b_rptr;
3939 3768 nip6h = (ip6_t *)phdr_mp->b_rptr;
3940 3769 align_len = P2ALIGN(icv_len + IPV6_PADDING_ALIGN - 1,
3941 3770 IPV6_PADDING_ALIGN);
3942 3771 /*
3943 3772 * phdr_mp must have the right amount of space for the
3944 3773 * combined IP and AH header. Copy the IP header with
3945 3774 * options into the pseudo header. When we constructed
3946 3775 * a pseudo header, we did not copy some of the mutable
3947 3776 * fields. We do it now by calling ah_fix_phdr_v6()
3948 3777 * with the last argument B_TRUE. It returns the
3949 3778 * ah_offset into the pseudo header.
3950 3779 */
3951 3780
3952 3781 bcopy(ip6h, nip6h, IPV6_HDR_LEN);
3953 3782 ah_offset = ah_fix_phdr_v6(nip6h, ip6h, B_TRUE, B_TRUE);
3954 3783 ASSERT(ah_offset != 0);
3955 3784 /*
3956 3785 * phdr_mp can hold exactly the whole IP header with options
3957 3786 * plus the AH header also. Thus subtracting the AH header's
3958 3787 * size should give exactly how much of the original header
3959 3788 * should be skipped.
3960 3789 */
3961 3790 hdrs_length = (phdr_mp->b_wptr - phdr_mp->b_rptr) -
3962 3791 sizeof (ah_t) - icv_len;
3963 3792 bcopy(phdr_mp->b_wptr, ((uint8_t *)nip6h + ah_offset +
3964 3793 sizeof (ah_t)), icv_len);
3965 3794 length = ntohs(nip6h->ip6_plen);
3966 3795 length += (sizeof (ah_t) + align_len);
3967 3796 nip6h->ip6_plen = htons((uint16_t)length);
3968 3797 }
3969 3798
3970 3799 /* Skip the original IP header */
3971 3800 mp->b_rptr += hdrs_length;
3972 3801 if (mp->b_rptr == mp->b_wptr) {
3973 3802 phdr_mp->b_cont = mp->b_cont;
3974 3803 freeb(mp);
3975 3804 }
3976 3805
3977 3806 return (phdr_mp);
3978 3807 }
3979 3808
3980 3809 /* Refactor me */
3981 3810 /*
3982 3811 * Wrapper to allow IP to trigger an AH association failure message
3983 3812 * during SA inbound selection.
3984 3813 */
3985 3814 void
3986 3815 ipsecah_in_assocfailure(mblk_t *mp, char level, ushort_t sl, char *fmt,
3987 3816 uint32_t spi, void *addr, int af, ip_recv_attr_t *ira)
3988 3817 {
3989 3818 netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
3990 3819 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
3991 3820 ipsec_stack_t *ipss = ns->netstack_ipsec;
3992 3821
3993 3822 if (ahstack->ipsecah_log_unknown_spi) {
3994 3823 ipsec_assocfailure(info.mi_idnum, 0, level, sl, fmt, spi,
3995 3824 addr, af, ahstack->ipsecah_netstack);
3996 3825 }
3997 3826
3998 3827 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
3999 3828 DROPPER(ipss, ipds_ah_no_sa),
4000 3829 &ahstack->ah_dropper);
4001 3830 }
4002 3831
4003 3832 /*
4004 3833 * Initialize the AH input and output processing functions.
4005 3834 */
4006 3835 void
4007 3836 ipsecah_init_funcs(ipsa_t *sa)
4008 3837 {
4009 3838 if (sa->ipsa_output_func == NULL)
4010 3839 sa->ipsa_output_func = ah_outbound;
4011 3840 if (sa->ipsa_input_func == NULL)
4012 3841 sa->ipsa_input_func = ah_inbound;
4013 3842 }
|
↓ open down ↓ |
2111 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX