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