Print this page
Test of DTrace assert probes
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/inet/ip/keysock.c
+++ new/usr/src/uts/common/inet/ip/keysock.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 #include <sys/param.h>
27 27 #include <sys/types.h>
28 28 #include <sys/stream.h>
29 29 #include <sys/strsubr.h>
30 30 #include <sys/strsun.h>
31 31 #include <sys/stropts.h>
32 32 #include <sys/vnode.h>
33 33 #include <sys/zone.h>
34 34 #include <sys/strlog.h>
35 35 #include <sys/sysmacros.h>
36 36 #define _SUN_TPI_VERSION 2
37 37 #include <sys/tihdr.h>
38 38 #include <sys/timod.h>
39 39 #include <sys/tiuser.h>
40 40 #include <sys/ddi.h>
41 41 #include <sys/sunddi.h>
42 42 #include <sys/sunldi.h>
43 43 #include <sys/file.h>
44 44 #include <sys/modctl.h>
45 45 #include <sys/debug.h>
46 46 #include <sys/kmem.h>
47 47 #include <sys/cmn_err.h>
48 48 #include <sys/proc.h>
49 49 #include <sys/suntpi.h>
50 50 #include <sys/atomic.h>
51 51 #include <sys/mkdev.h>
52 52 #include <sys/policy.h>
53 53 #include <sys/disp.h>
54 54
55 55 #include <sys/socket.h>
56 56 #include <netinet/in.h>
57 57 #include <net/pfkeyv2.h>
58 58
59 59 #include <inet/common.h>
60 60 #include <netinet/ip6.h>
61 61 #include <inet/ip.h>
62 62 #include <inet/proto_set.h>
63 63 #include <inet/nd.h>
64 64 #include <inet/optcom.h>
65 65 #include <inet/ipsec_info.h>
66 66 #include <inet/ipsec_impl.h>
67 67 #include <inet/keysock.h>
68 68
69 69 #include <sys/isa_defs.h>
70 70
71 71 /*
72 72 * This is a transport provider for the PF_KEY key mangement socket.
73 73 * (See RFC 2367 for details.)
74 74 * Downstream messages are wrapped in a keysock consumer interface KEYSOCK_IN
75 75 * messages (see ipsec_info.h), and passed to the appropriate consumer.
76 76 * Upstream messages are generated for all open PF_KEY sockets, when
77 77 * appropriate, as well as the sender (as long as SO_USELOOPBACK is enabled)
78 78 * in reply to downstream messages.
79 79 *
80 80 * Upstream messages must be created asynchronously for the following
81 81 * situations:
82 82 *
83 83 * 1.) A keysock consumer requires an SA, and there is currently none.
84 84 * 2.) An SA expires, either hard or soft lifetime.
85 85 * 3.) Other events a consumer deems fit.
86 86 *
87 87 * The MT model of this is PERMOD, with shared put procedures. Two types of
88 88 * messages, SADB_FLUSH and SADB_DUMP, need to lock down the perimeter to send
89 89 * down the *multiple* messages they create.
90 90 */
91 91
92 92 static vmem_t *keysock_vmem; /* for minor numbers. */
93 93
94 94 #define KEYSOCK_MAX_CONSUMERS 256
95 95
96 96 /* Default structure copied into T_INFO_ACK messages (from rts.c...) */
97 97 static struct T_info_ack keysock_g_t_info_ack = {
98 98 T_INFO_ACK,
99 99 T_INFINITE, /* TSDU_size. Maximum size messages. */
100 100 T_INVALID, /* ETSDU_size. No expedited data. */
101 101 T_INVALID, /* CDATA_size. No connect data. */
102 102 T_INVALID, /* DDATA_size. No disconnect data. */
103 103 0, /* ADDR_size. */
104 104 0, /* OPT_size. No user-settable options */
105 105 64 * 1024, /* TIDU_size. keysock allows maximum size messages. */
106 106 T_COTS, /* SERV_type. keysock supports connection oriented. */
107 107 TS_UNBND, /* CURRENT_state. This is set from keysock_state. */
108 108 (XPG4_1) /* Provider flags */
109 109 };
110 110
111 111 /* Named Dispatch Parameter Management Structure */
112 112 typedef struct keysockparam_s {
113 113 uint_t keysock_param_min;
114 114 uint_t keysock_param_max;
115 115 uint_t keysock_param_value;
116 116 char *keysock_param_name;
117 117 } keysockparam_t;
118 118
119 119 /*
120 120 * Table of NDD variables supported by keysock. These are loaded into
121 121 * keysock_g_nd in keysock_init_nd.
122 122 * All of these are alterable, within the min/max values given, at run time.
123 123 */
124 124 static keysockparam_t lcl_param_arr[] = {
125 125 /* min max value name */
126 126 { 4096, 65536, 8192, "keysock_xmit_hiwat"},
127 127 { 0, 65536, 1024, "keysock_xmit_lowat"},
128 128 { 4096, 65536, 8192, "keysock_recv_hiwat"},
129 129 { 65536, 1024*1024*1024, 256*1024, "keysock_max_buf"},
130 130 { 0, 3, 0, "keysock_debug"},
131 131 };
132 132 #define keystack_xmit_hiwat keystack_params[0].keysock_param_value
133 133 #define keystack_xmit_lowat keystack_params[1].keysock_param_value
134 134 #define keystack_recv_hiwat keystack_params[2].keysock_param_value
135 135 #define keystack_max_buf keystack_params[3].keysock_param_value
136 136 #define keystack_debug keystack_params[4].keysock_param_value
137 137
138 138 #define ks0dbg(a) printf a
139 139 /* NOTE: != 0 instead of > 0 so lint doesn't complain. */
140 140 #define ks1dbg(keystack, a) if (keystack->keystack_debug != 0) printf a
141 141 #define ks2dbg(keystack, a) if (keystack->keystack_debug > 1) printf a
142 142 #define ks3dbg(keystack, a) if (keystack->keystack_debug > 2) printf a
143 143
144 144 static int keysock_close(queue_t *);
145 145 static int keysock_open(queue_t *, dev_t *, int, int, cred_t *);
146 146 static void keysock_wput(queue_t *, mblk_t *);
147 147 static void keysock_rput(queue_t *, mblk_t *);
148 148 static void keysock_rsrv(queue_t *);
149 149 static void keysock_passup(mblk_t *, sadb_msg_t *, minor_t,
150 150 keysock_consumer_t *, boolean_t, keysock_stack_t *);
151 151 static void *keysock_stack_init(netstackid_t stackid, netstack_t *ns);
152 152 static void keysock_stack_fini(netstackid_t stackid, void *arg);
153 153
154 154 static struct module_info info = {
155 155 5138, "keysock", 1, INFPSZ, 512, 128
156 156 };
157 157
158 158 static struct qinit rinit = {
159 159 (pfi_t)keysock_rput, (pfi_t)keysock_rsrv, keysock_open, keysock_close,
160 160 NULL, &info
161 161 };
162 162
163 163 static struct qinit winit = {
164 164 (pfi_t)keysock_wput, NULL, NULL, NULL, NULL, &info
165 165 };
166 166
167 167 struct streamtab keysockinfo = {
168 168 &rinit, &winit
169 169 };
170 170
171 171 extern struct modlinkage *keysock_modlp;
172 172
173 173 /*
174 174 * Plumb IPsec.
175 175 *
176 176 * NOTE: New "default" modules will need to be loaded here if needed before
177 177 * boot time.
178 178 */
179 179
180 180 /* Keep these in global space to keep the lint from complaining. */
181 181 static char *IPSECESP = "ipsecesp";
182 182 static char *IPSECESPDEV = "/devices/pseudo/ipsecesp@0:ipsecesp";
183 183 static char *IPSECAH = "ipsecah";
184 184 static char *IPSECAHDEV = "/devices/pseudo/ipsecah@0:ipsecah";
185 185 static char *IP6DEV = "/devices/pseudo/ip6@0:ip6";
186 186 static char *KEYSOCK = "keysock";
187 187 static char *STRMOD = "strmod";
188 188
189 189 /*
190 190 * Load the other ipsec modules and plumb them together.
191 191 */
192 192 int
193 193 keysock_plumb_ipsec(netstack_t *ns)
194 194 {
195 195 ldi_handle_t lh, ip6_lh = NULL;
196 196 ldi_ident_t li = NULL;
197 197 int err = 0;
198 198 int muxid, rval;
199 199 boolean_t esp_present = B_TRUE;
200 200 cred_t *cr;
201 201 keysock_stack_t *keystack = ns->netstack_keysock;
202 202
203 203 #ifdef NS_DEBUG
204 204 (void) printf("keysock_plumb_ipsec(%d)\n",
205 205 ns->netstack_stackid);
206 206 #endif
207 207
208 208 keystack->keystack_plumbed = 0; /* we're trying again.. */
209 209
210 210 cr = zone_get_kcred(netstackid_to_zoneid(
211 211 keystack->keystack_netstack->netstack_stackid));
212 212 ASSERT(cr != NULL);
213 213 /*
214 214 * Load up the drivers (AH/ESP).
215 215 *
216 216 * I do this separately from the actual plumbing in case this function
217 217 * ever gets called from a diskless boot before the root filesystem is
218 218 * up. I don't have to worry about "keysock" because, well, if I'm
219 219 * here, keysock must've loaded successfully.
220 220 */
221 221 if (i_ddi_attach_pseudo_node(IPSECAH) == NULL) {
222 222 ks0dbg(("IPsec: AH failed to attach.\n"));
223 223 goto bail;
224 224 }
225 225 if (i_ddi_attach_pseudo_node(IPSECESP) == NULL) {
226 226 ks0dbg(("IPsec: ESP failed to attach.\n"));
227 227 esp_present = B_FALSE;
228 228 }
229 229
230 230 /*
231 231 * Set up the IP streams for AH and ESP, as well as tacking keysock
232 232 * on top of them. Assume keysock has set the autopushes up already.
233 233 */
234 234
235 235 /* Open IP. */
236 236 err = ldi_ident_from_mod(keysock_modlp, &li);
237 237 if (err) {
238 238 ks0dbg(("IPsec: lid_ident_from_mod failed (err %d).\n",
239 239 err));
240 240 goto bail;
241 241 }
242 242
243 243 err = ldi_open_by_name(IP6DEV, FREAD|FWRITE, cr, &ip6_lh, li);
244 244 if (err) {
245 245 ks0dbg(("IPsec: Open of IP6 failed (err %d).\n", err));
246 246 goto bail;
247 247 }
248 248
249 249 /* PLINK KEYSOCK/AH */
250 250 err = ldi_open_by_name(IPSECAHDEV, FREAD|FWRITE, cr, &lh, li);
251 251 if (err) {
252 252 ks0dbg(("IPsec: Open of AH failed (err %d).\n", err));
253 253 goto bail;
254 254 }
255 255 err = ldi_ioctl(lh,
256 256 I_PUSH, (intptr_t)KEYSOCK, FKIOCTL, cr, &rval);
257 257 if (err) {
258 258 ks0dbg(("IPsec: Push of KEYSOCK onto AH failed (err %d).\n",
259 259 err));
260 260 (void) ldi_close(lh, FREAD|FWRITE, cr);
261 261 goto bail;
262 262 }
263 263 err = ldi_ioctl(ip6_lh, I_PLINK, (intptr_t)lh,
264 264 FREAD+FWRITE+FNOCTTY+FKIOCTL, cr, &muxid);
265 265 if (err) {
266 266 ks0dbg(("IPsec: PLINK of KEYSOCK/AH failed (err %d).\n", err));
267 267 (void) ldi_close(lh, FREAD|FWRITE, cr);
268 268 goto bail;
269 269 }
270 270 (void) ldi_close(lh, FREAD|FWRITE, cr);
271 271
272 272 /* PLINK KEYSOCK/ESP */
273 273 if (esp_present) {
274 274 err = ldi_open_by_name(IPSECESPDEV,
275 275 FREAD|FWRITE, cr, &lh, li);
276 276 if (err) {
277 277 ks0dbg(("IPsec: Open of ESP failed (err %d).\n", err));
278 278 goto bail;
279 279 }
280 280 err = ldi_ioctl(lh,
281 281 I_PUSH, (intptr_t)KEYSOCK, FKIOCTL, cr, &rval);
282 282 if (err) {
283 283 ks0dbg(("IPsec: "
284 284 "Push of KEYSOCK onto ESP failed (err %d).\n",
285 285 err));
286 286 (void) ldi_close(lh, FREAD|FWRITE, cr);
287 287 goto bail;
288 288 }
289 289 err = ldi_ioctl(ip6_lh, I_PLINK, (intptr_t)lh,
290 290 FREAD+FWRITE+FNOCTTY+FKIOCTL, cr, &muxid);
291 291 if (err) {
292 292 ks0dbg(("IPsec: "
293 293 "PLINK of KEYSOCK/ESP failed (err %d).\n", err));
294 294 (void) ldi_close(lh, FREAD|FWRITE, cr);
295 295 goto bail;
296 296 }
297 297 (void) ldi_close(lh, FREAD|FWRITE, cr);
298 298 }
299 299
300 300 bail:
301 301 keystack->keystack_plumbed = (err == 0) ? 1 : -1;
302 302 if (ip6_lh != NULL) {
303 303 (void) ldi_close(ip6_lh, FREAD|FWRITE, cr);
304 304 }
305 305 if (li != NULL)
306 306 ldi_ident_release(li);
307 307 #ifdef NS_DEBUG
308 308 (void) printf("keysock_plumb_ipsec -> %d\n",
309 309 keystack->keystack_plumbed);
310 310 #endif
311 311 crfree(cr);
312 312 return (err);
313 313 }
314 314
315 315 /* ARGSUSED */
316 316 static int
317 317 keysock_param_get(q, mp, cp, cr)
318 318 queue_t *q;
319 319 mblk_t *mp;
320 320 caddr_t cp;
321 321 cred_t *cr;
322 322 {
323 323 keysockparam_t *keysockpa = (keysockparam_t *)cp;
324 324 uint_t value;
325 325 keysock_t *ks = (keysock_t *)q->q_ptr;
326 326 keysock_stack_t *keystack = ks->keysock_keystack;
327 327
328 328 mutex_enter(&keystack->keystack_param_lock);
329 329 value = keysockpa->keysock_param_value;
330 330 mutex_exit(&keystack->keystack_param_lock);
331 331
332 332 (void) mi_mpprintf(mp, "%u", value);
333 333 return (0);
334 334 }
335 335
336 336 /* This routine sets an NDD variable in a keysockparam_t structure. */
337 337 /* ARGSUSED */
338 338 static int
339 339 keysock_param_set(q, mp, value, cp, cr)
340 340 queue_t *q;
341 341 mblk_t *mp;
342 342 char *value;
343 343 caddr_t cp;
344 344 cred_t *cr;
345 345 {
346 346 ulong_t new_value;
347 347 keysockparam_t *keysockpa = (keysockparam_t *)cp;
348 348 keysock_t *ks = (keysock_t *)q->q_ptr;
349 349 keysock_stack_t *keystack = ks->keysock_keystack;
350 350
351 351 /* Convert the value from a string into a long integer. */
352 352 if (ddi_strtoul(value, NULL, 10, &new_value) != 0)
353 353 return (EINVAL);
354 354
355 355 mutex_enter(&keystack->keystack_param_lock);
356 356 /*
357 357 * Fail the request if the new value does not lie within the
358 358 * required bounds.
359 359 */
360 360 if (new_value < keysockpa->keysock_param_min ||
361 361 new_value > keysockpa->keysock_param_max) {
362 362 mutex_exit(&keystack->keystack_param_lock);
363 363 return (EINVAL);
364 364 }
365 365
366 366 /* Set the new value */
367 367 keysockpa->keysock_param_value = new_value;
368 368 mutex_exit(&keystack->keystack_param_lock);
369 369
370 370 return (0);
371 371 }
372 372
373 373 /*
374 374 * Initialize keysock at module load time
375 375 */
376 376 boolean_t
377 377 keysock_ddi_init(void)
378 378 {
379 379 keysock_max_optsize = optcom_max_optsize(
380 380 keysock_opt_obj.odb_opt_des_arr, keysock_opt_obj.odb_opt_arr_cnt);
381 381
382 382 keysock_vmem = vmem_create("keysock", (void *)1, MAXMIN, 1,
383 383 NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER);
384 384
385 385 /*
386 386 * We want to be informed each time a stack is created or
387 387 * destroyed in the kernel, so we can maintain the
388 388 * set of keysock_stack_t's.
389 389 */
390 390 netstack_register(NS_KEYSOCK, keysock_stack_init, NULL,
391 391 keysock_stack_fini);
392 392
393 393 return (B_TRUE);
394 394 }
395 395
396 396 /*
397 397 * Walk through the param array specified registering each element with the
398 398 * named dispatch handler.
399 399 */
400 400 static boolean_t
401 401 keysock_param_register(IDP *ndp, keysockparam_t *ksp, int cnt)
402 402 {
403 403 for (; cnt-- > 0; ksp++) {
404 404 if (ksp->keysock_param_name != NULL &&
405 405 ksp->keysock_param_name[0]) {
406 406 if (!nd_load(ndp,
407 407 ksp->keysock_param_name,
408 408 keysock_param_get, keysock_param_set,
409 409 (caddr_t)ksp)) {
410 410 nd_free(ndp);
411 411 return (B_FALSE);
412 412 }
413 413 }
414 414 }
415 415 return (B_TRUE);
416 416 }
417 417
418 418 /*
419 419 * Initialize keysock for one stack instance
420 420 */
421 421 /* ARGSUSED */
422 422 static void *
423 423 keysock_stack_init(netstackid_t stackid, netstack_t *ns)
424 424 {
425 425 keysock_stack_t *keystack;
426 426 keysockparam_t *ksp;
427 427
428 428 keystack = (keysock_stack_t *)kmem_zalloc(sizeof (*keystack), KM_SLEEP);
429 429 keystack->keystack_netstack = ns;
430 430
431 431 keystack->keystack_acquire_seq = 0xffffffff;
432 432
433 433 ksp = (keysockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
434 434 keystack->keystack_params = ksp;
435 435 bcopy(lcl_param_arr, ksp, sizeof (lcl_param_arr));
436 436
437 437 (void) keysock_param_register(&keystack->keystack_g_nd, ksp,
438 438 A_CNT(lcl_param_arr));
439 439
440 440 mutex_init(&keystack->keystack_list_lock, NULL, MUTEX_DEFAULT, NULL);
441 441 mutex_init(&keystack->keystack_consumers_lock,
442 442 NULL, MUTEX_DEFAULT, NULL);
443 443 mutex_init(&keystack->keystack_param_lock, NULL, MUTEX_DEFAULT, NULL);
444 444 return (keystack);
445 445 }
446 446
447 447 /*
448 448 * Free NDD variable space, and other destructors, for keysock.
449 449 */
450 450 void
451 451 keysock_ddi_destroy(void)
452 452 {
453 453 netstack_unregister(NS_KEYSOCK);
454 454 vmem_destroy(keysock_vmem);
455 455 }
456 456
457 457 /*
458 458 * Remove one stack instance from keysock
459 459 */
460 460 /* ARGSUSED */
461 461 static void
462 462 keysock_stack_fini(netstackid_t stackid, void *arg)
463 463 {
464 464 keysock_stack_t *keystack = (keysock_stack_t *)arg;
465 465
466 466 nd_free(&keystack->keystack_g_nd);
467 467 kmem_free(keystack->keystack_params, sizeof (lcl_param_arr));
468 468 keystack->keystack_params = NULL;
469 469
470 470 mutex_destroy(&keystack->keystack_list_lock);
471 471 mutex_destroy(&keystack->keystack_consumers_lock);
472 472 mutex_destroy(&keystack->keystack_param_lock);
473 473
474 474 kmem_free(keystack, sizeof (*keystack));
475 475 }
476 476
477 477 /*
478 478 * Close routine for keysock.
479 479 */
480 480 static int
481 481 keysock_close(queue_t *q)
482 482 {
483 483 keysock_t *ks;
484 484 keysock_consumer_t *kc;
485 485 void *ptr = q->q_ptr;
486 486 int size;
487 487 keysock_stack_t *keystack;
488 488
489 489
490 490 qprocsoff(q);
491 491
492 492 /* Safe assumption. */
493 493 ASSERT(ptr != NULL);
494 494
495 495 if (WR(q)->q_next) {
496 496 kc = (keysock_consumer_t *)ptr;
497 497 keystack = kc->kc_keystack;
498 498
499 499 ks1dbg(keystack, ("Module close, removing a consumer (%d).\n",
500 500 kc->kc_sa_type));
501 501 /*
502 502 * Because of PERMOD open/close exclusive perimeter, I
503 503 * can inspect KC_FLUSHING w/o locking down kc->kc_lock.
504 504 */
505 505 if (kc->kc_flags & KC_FLUSHING) {
506 506 /*
507 507 * If this decrement was the last one, send
508 508 * down the next pending one, if any.
509 509 *
510 510 * With a PERMOD perimeter, the mutexes ops aren't
511 511 * really necessary, but if we ever loosen up, we will
512 512 * have this bit covered already.
513 513 */
514 514 keystack->keystack_flushdump--;
515 515 if (keystack->keystack_flushdump == 0) {
516 516 /*
517 517 * The flush/dump terminated by having a
518 518 * consumer go away. I need to send up to the
519 519 * appropriate keysock all of the relevant
520 520 * information. Unfortunately, I don't
521 521 * have that handy.
522 522 */
523 523 ks0dbg(("Consumer went away while flushing or"
524 524 " dumping.\n"));
525 525 }
526 526 }
527 527 size = sizeof (keysock_consumer_t);
528 528 mutex_enter(&keystack->keystack_consumers_lock);
529 529 keystack->keystack_consumers[kc->kc_sa_type] = NULL;
530 530 mutex_exit(&keystack->keystack_consumers_lock);
531 531 mutex_destroy(&kc->kc_lock);
532 532 netstack_rele(kc->kc_keystack->keystack_netstack);
533 533 } else {
534 534 ks = (keysock_t *)ptr;
535 535 keystack = ks->keysock_keystack;
536 536
537 537 ks3dbg(keystack,
538 538 ("Driver close, PF_KEY socket is going away.\n"));
539 539 if ((ks->keysock_flags & KEYSOCK_EXTENDED) != 0)
540 540 atomic_dec_32(&keystack->keystack_num_extended);
541 541 size = sizeof (keysock_t);
542 542 mutex_enter(&keystack->keystack_list_lock);
543 543 *(ks->keysock_ptpn) = ks->keysock_next;
544 544 if (ks->keysock_next != NULL)
545 545 ks->keysock_next->keysock_ptpn = ks->keysock_ptpn;
546 546 mutex_exit(&keystack->keystack_list_lock);
547 547 mutex_destroy(&ks->keysock_lock);
548 548 vmem_free(keysock_vmem, (void *)(uintptr_t)ks->keysock_serial,
549 549 1);
550 550 netstack_rele(ks->keysock_keystack->keystack_netstack);
551 551 }
552 552
553 553 /* Now I'm free. */
554 554 kmem_free(ptr, size);
555 555 return (0);
556 556 }
557 557 /*
558 558 * Open routine for keysock.
559 559 */
560 560 /* ARGSUSED */
|
↓ open down ↓ |
560 lines elided |
↑ open up ↑ |
561 561 static int
562 562 keysock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
563 563 {
564 564 keysock_t *ks;
565 565 keysock_consumer_t *kc;
566 566 mblk_t *mp;
567 567 ipsec_info_t *ii;
568 568 netstack_t *ns;
569 569 keysock_stack_t *keystack;
570 570
571 +#ifdef KEBE
572 + /*
573 + * Some test ASSERT()s that only work with the new dtrace-style assert.
574 + */
575 + ASSERT(q == NULL); /* Will likely fail. (575) */
576 + ASSERT3P(q, ==, NULL); /* Same here. (576) */
577 +#endif
578 +
571 579 if (secpolicy_ip_config(credp, B_FALSE) != 0) {
572 580 /* Privilege debugging will log the error */
573 581 return (EPERM);
574 582 }
575 583
576 584 if (q->q_ptr != NULL)
577 585 return (0); /* Re-open of an already open instance. */
578 586
579 587 ns = netstack_find_by_cred(credp);
580 588 ASSERT(ns != NULL);
581 589 keystack = ns->netstack_keysock;
582 590 ASSERT(keystack != NULL);
583 591
584 592 ks3dbg(keystack, ("Entering keysock open.\n"));
585 593
586 594 if (keystack->keystack_plumbed < 1) {
587 595 netstack_t *ns = keystack->keystack_netstack;
588 596
589 597 keystack->keystack_plumbed = 0;
590 598 #ifdef NS_DEBUG
591 599 printf("keysock_open(%d) - plumb\n",
592 600 keystack->keystack_netstack->netstack_stackid);
593 601 #endif
594 602 /*
595 603 * Don't worry about ipsec_failure being true here.
596 604 * (See ip.c). An open of keysock should try and force
597 605 * the issue. Maybe it was a transient failure.
598 606 */
599 607 ipsec_loader_loadnow(ns->netstack_ipsec);
600 608 }
601 609
602 610 if (sflag & MODOPEN) {
603 611 /* Initialize keysock_consumer state here. */
604 612 kc = kmem_zalloc(sizeof (keysock_consumer_t), KM_NOSLEEP);
605 613 if (kc == NULL) {
606 614 netstack_rele(keystack->keystack_netstack);
607 615 return (ENOMEM);
608 616 }
609 617 mutex_init(&kc->kc_lock, NULL, MUTEX_DEFAULT, 0);
610 618 kc->kc_rq = q;
611 619 kc->kc_wq = WR(q);
612 620
613 621 q->q_ptr = kc;
614 622 WR(q)->q_ptr = kc;
615 623
616 624 kc->kc_keystack = keystack;
617 625 qprocson(q);
618 626
619 627 /*
620 628 * Send down initial message to whatever I was pushed on top
621 629 * of asking for its consumer type. The reply will set it.
622 630 */
623 631
624 632 /* Allocate it. */
625 633 mp = allocb(sizeof (ipsec_info_t), BPRI_HI);
626 634 if (mp == NULL) {
627 635 ks1dbg(keystack, (
628 636 "keysock_open: Cannot allocate KEYSOCK_HELLO.\n"));
629 637 /* Do I need to set these to null? */
630 638 q->q_ptr = NULL;
631 639 WR(q)->q_ptr = NULL;
632 640 mutex_destroy(&kc->kc_lock);
633 641 kmem_free(kc, sizeof (*kc));
634 642 netstack_rele(keystack->keystack_netstack);
635 643 return (ENOMEM);
636 644 }
637 645
638 646 /* If I allocated okay, putnext to what I was pushed atop. */
639 647 mp->b_wptr += sizeof (ipsec_info_t);
640 648 mp->b_datap->db_type = M_CTL;
641 649 ii = (ipsec_info_t *)mp->b_rptr;
642 650 ii->ipsec_info_type = KEYSOCK_HELLO;
643 651 /* Length only of type/len. */
644 652 ii->ipsec_info_len = sizeof (ii->ipsec_allu);
645 653 ks2dbg(keystack, ("Ready to putnext KEYSOCK_HELLO.\n"));
646 654 putnext(kc->kc_wq, mp);
647 655 } else {
648 656 minor_t ksminor;
649 657
650 658 /* Initialize keysock state. */
651 659
652 660 ks2dbg(keystack, ("Made it into PF_KEY socket open.\n"));
653 661
654 662 ksminor = (minor_t)(uintptr_t)
655 663 vmem_alloc(keysock_vmem, 1, VM_NOSLEEP);
656 664 if (ksminor == 0) {
657 665 netstack_rele(keystack->keystack_netstack);
658 666 return (ENOMEM);
659 667 }
660 668 ks = kmem_zalloc(sizeof (keysock_t), KM_NOSLEEP);
661 669 if (ks == NULL) {
662 670 vmem_free(keysock_vmem, (void *)(uintptr_t)ksminor, 1);
663 671 netstack_rele(keystack->keystack_netstack);
664 672 return (ENOMEM);
665 673 }
666 674
667 675 mutex_init(&ks->keysock_lock, NULL, MUTEX_DEFAULT, 0);
668 676 ks->keysock_rq = q;
669 677 ks->keysock_wq = WR(q);
670 678 ks->keysock_state = TS_UNBND;
671 679 ks->keysock_serial = ksminor;
672 680
673 681 q->q_ptr = ks;
674 682 WR(q)->q_ptr = ks;
675 683 ks->keysock_keystack = keystack;
676 684
677 685 /*
678 686 * The receive hiwat is only looked at on the stream head
679 687 * queue. Store in q_hiwat in order to return on SO_RCVBUF
680 688 * getsockopts.
681 689 */
682 690
683 691 q->q_hiwat = keystack->keystack_recv_hiwat;
684 692
685 693 /*
686 694 * The transmit hiwat/lowat is only looked at on IP's queue.
687 695 * Store in q_hiwat/q_lowat in order to return on
688 696 * SO_SNDBUF/SO_SNDLOWAT getsockopts.
689 697 */
690 698
691 699 WR(q)->q_hiwat = keystack->keystack_xmit_hiwat;
692 700 WR(q)->q_lowat = keystack->keystack_xmit_lowat;
693 701
694 702 *devp = makedevice(getmajor(*devp), ksminor);
695 703
696 704 /*
697 705 * Thread keysock into the global keysock list.
698 706 */
699 707 mutex_enter(&keystack->keystack_list_lock);
700 708 ks->keysock_next = keystack->keystack_list;
701 709 ks->keysock_ptpn = &keystack->keystack_list;
702 710 if (keystack->keystack_list != NULL) {
703 711 keystack->keystack_list->keysock_ptpn =
704 712 &ks->keysock_next;
705 713 }
706 714 keystack->keystack_list = ks;
707 715 mutex_exit(&keystack->keystack_list_lock);
708 716
709 717 qprocson(q);
710 718 (void) proto_set_rx_hiwat(q, NULL,
711 719 keystack->keystack_recv_hiwat);
712 720 /*
713 721 * Wait outside the keysock module perimeter for IPsec
714 722 * plumbing to be completed. If it fails, keysock_close()
715 723 * undoes everything we just did.
716 724 */
717 725 if (!ipsec_loader_wait(q,
718 726 keystack->keystack_netstack->netstack_ipsec)) {
719 727 (void) keysock_close(q);
720 728 return (EPFNOSUPPORT);
721 729 }
722 730 }
723 731
724 732 return (0);
725 733 }
726 734
727 735 /* BELOW THIS LINE ARE ROUTINES INCLUDING AND RELATED TO keysock_wput(). */
728 736
729 737 /*
730 738 * Copy relevant state bits.
731 739 */
732 740 static void
733 741 keysock_copy_info(struct T_info_ack *tap, keysock_t *ks)
734 742 {
735 743 *tap = keysock_g_t_info_ack;
736 744 tap->CURRENT_state = ks->keysock_state;
737 745 tap->OPT_size = keysock_max_optsize;
738 746 }
739 747
740 748 /*
741 749 * This routine responds to T_CAPABILITY_REQ messages. It is called by
742 750 * keysock_wput. Much of the T_CAPABILITY_ACK information is copied from
743 751 * keysock_g_t_info_ack. The current state of the stream is copied from
744 752 * keysock_state.
745 753 */
746 754 static void
747 755 keysock_capability_req(queue_t *q, mblk_t *mp)
748 756 {
749 757 keysock_t *ks = (keysock_t *)q->q_ptr;
750 758 t_uscalar_t cap_bits1;
751 759 struct T_capability_ack *tcap;
752 760
753 761 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
754 762
755 763 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
756 764 mp->b_datap->db_type, T_CAPABILITY_ACK);
757 765 if (mp == NULL)
758 766 return;
759 767
760 768 tcap = (struct T_capability_ack *)mp->b_rptr;
761 769 tcap->CAP_bits1 = 0;
762 770
763 771 if (cap_bits1 & TC1_INFO) {
764 772 keysock_copy_info(&tcap->INFO_ack, ks);
765 773 tcap->CAP_bits1 |= TC1_INFO;
766 774 }
767 775
768 776 qreply(q, mp);
769 777 }
770 778
771 779 /*
772 780 * This routine responds to T_INFO_REQ messages. It is called by
773 781 * keysock_wput_other.
774 782 * Most of the T_INFO_ACK information is copied from keysock_g_t_info_ack.
775 783 * The current state of the stream is copied from keysock_state.
776 784 */
777 785 static void
778 786 keysock_info_req(q, mp)
779 787 queue_t *q;
780 788 mblk_t *mp;
781 789 {
782 790 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
783 791 T_INFO_ACK);
784 792 if (mp == NULL)
785 793 return;
786 794 keysock_copy_info((struct T_info_ack *)mp->b_rptr,
787 795 (keysock_t *)q->q_ptr);
788 796 qreply(q, mp);
789 797 }
790 798
791 799 /*
792 800 * keysock_err_ack. This routine creates a
793 801 * T_ERROR_ACK message and passes it
794 802 * upstream.
795 803 */
796 804 static void
797 805 keysock_err_ack(q, mp, t_error, sys_error)
798 806 queue_t *q;
799 807 mblk_t *mp;
800 808 int t_error;
801 809 int sys_error;
802 810 {
803 811 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
804 812 qreply(q, mp);
805 813 }
806 814
807 815 /*
808 816 * This routine retrieves the current status of socket options.
809 817 * It returns the size of the option retrieved.
810 818 */
811 819 /* ARGSUSED */
812 820 int
813 821 keysock_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
814 822 {
815 823 int *i1 = (int *)ptr;
816 824 keysock_t *ks = (keysock_t *)q->q_ptr;
817 825
818 826 switch (level) {
819 827 case SOL_SOCKET:
820 828 mutex_enter(&ks->keysock_lock);
821 829 switch (name) {
822 830 case SO_TYPE:
823 831 *i1 = SOCK_RAW;
824 832 break;
825 833 case SO_USELOOPBACK:
826 834 *i1 = (int)(!((ks->keysock_flags & KEYSOCK_NOLOOP) ==
827 835 KEYSOCK_NOLOOP));
828 836 break;
829 837 /*
830 838 * The following two items can be manipulated,
831 839 * but changing them should do nothing.
832 840 */
833 841 case SO_SNDBUF:
834 842 *i1 = (int)q->q_hiwat;
835 843 break;
836 844 case SO_RCVBUF:
837 845 *i1 = (int)(RD(q)->q_hiwat);
838 846 break;
839 847 }
840 848 mutex_exit(&ks->keysock_lock);
841 849 break;
842 850 default:
843 851 return (0);
844 852 }
845 853 return (sizeof (int));
846 854 }
847 855
848 856 /*
849 857 * This routine sets socket options.
850 858 */
851 859 /* ARGSUSED */
852 860 int
853 861 keysock_opt_set(queue_t *q, uint_t mgmt_flags, int level,
854 862 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp,
855 863 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr)
856 864 {
857 865 int *i1 = (int *)invalp, errno = 0;
858 866 keysock_t *ks = (keysock_t *)q->q_ptr;
859 867 keysock_stack_t *keystack = ks->keysock_keystack;
860 868
861 869 switch (level) {
862 870 case SOL_SOCKET:
863 871 mutex_enter(&ks->keysock_lock);
864 872 switch (name) {
865 873 case SO_USELOOPBACK:
866 874 if (!(*i1))
867 875 ks->keysock_flags |= KEYSOCK_NOLOOP;
868 876 else ks->keysock_flags &= ~KEYSOCK_NOLOOP;
869 877 break;
870 878 case SO_SNDBUF:
871 879 if (*i1 > keystack->keystack_max_buf)
872 880 errno = ENOBUFS;
873 881 else q->q_hiwat = *i1;
874 882 break;
875 883 case SO_RCVBUF:
876 884 if (*i1 > keystack->keystack_max_buf) {
877 885 errno = ENOBUFS;
878 886 } else {
879 887 RD(q)->q_hiwat = *i1;
880 888 (void) proto_set_rx_hiwat(RD(q), NULL, *i1);
881 889 }
882 890 break;
883 891 default:
884 892 errno = EINVAL;
885 893 }
886 894 mutex_exit(&ks->keysock_lock);
887 895 break;
888 896 default:
889 897 errno = EINVAL;
890 898 }
891 899 return (errno);
892 900 }
893 901
894 902 /*
895 903 * Handle STREAMS messages.
896 904 */
897 905 static void
898 906 keysock_wput_other(queue_t *q, mblk_t *mp)
899 907 {
900 908 struct iocblk *iocp;
901 909 int error;
902 910 keysock_t *ks = (keysock_t *)q->q_ptr;
903 911 keysock_stack_t *keystack = ks->keysock_keystack;
904 912 cred_t *cr;
905 913
906 914 switch (mp->b_datap->db_type) {
907 915 case M_PROTO:
908 916 case M_PCPROTO:
909 917 if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) {
910 918 ks3dbg(keystack, (
911 919 "keysock_wput_other: Not big enough M_PROTO\n"));
912 920 freemsg(mp);
913 921 return;
914 922 }
915 923 switch (((union T_primitives *)mp->b_rptr)->type) {
916 924 case T_CAPABILITY_REQ:
917 925 keysock_capability_req(q, mp);
918 926 break;
919 927 case T_INFO_REQ:
920 928 keysock_info_req(q, mp);
921 929 break;
922 930 case T_SVR4_OPTMGMT_REQ:
923 931 case T_OPTMGMT_REQ:
924 932 /*
925 933 * All Solaris components should pass a db_credp
926 934 * for this TPI message, hence we ASSERT.
927 935 * But in case there is some other M_PROTO that looks
928 936 * like a TPI message sent by some other kernel
929 937 * component, we check and return an error.
930 938 */
931 939 cr = msg_getcred(mp, NULL);
932 940 ASSERT(cr != NULL);
933 941 if (cr == NULL) {
934 942 keysock_err_ack(q, mp, TSYSERR, EINVAL);
935 943 return;
936 944 }
937 945 if (((union T_primitives *)mp->b_rptr)->type ==
938 946 T_SVR4_OPTMGMT_REQ) {
939 947 svr4_optcom_req(q, mp, cr, &keysock_opt_obj);
940 948 } else {
941 949 tpi_optcom_req(q, mp, cr, &keysock_opt_obj);
942 950 }
943 951 break;
944 952 case T_DATA_REQ:
945 953 case T_EXDATA_REQ:
946 954 case T_ORDREL_REQ:
947 955 /* Illegal for keysock. */
948 956 freemsg(mp);
949 957 (void) putnextctl1(RD(q), M_ERROR, EPROTO);
950 958 break;
951 959 default:
952 960 /* Not supported by keysock. */
953 961 keysock_err_ack(q, mp, TNOTSUPPORT, 0);
954 962 break;
955 963 }
956 964 return;
957 965 case M_IOCTL:
958 966 iocp = (struct iocblk *)mp->b_rptr;
959 967 error = EINVAL;
960 968
961 969 switch (iocp->ioc_cmd) {
962 970 case ND_SET:
963 971 case ND_GET:
964 972 if (nd_getset(q, keystack->keystack_g_nd, mp)) {
965 973 qreply(q, mp);
966 974 return;
967 975 } else
968 976 error = ENOENT;
969 977 /* FALLTHRU */
970 978 default:
971 979 miocnak(q, mp, 0, error);
972 980 return;
973 981 }
974 982 case M_FLUSH:
975 983 if (*mp->b_rptr & FLUSHW) {
976 984 flushq(q, FLUSHALL);
977 985 *mp->b_rptr &= ~FLUSHW;
978 986 }
979 987 if (*mp->b_rptr & FLUSHR) {
980 988 qreply(q, mp);
981 989 return;
982 990 }
983 991 /* Else FALLTHRU */
984 992 }
985 993
986 994 /* If fell through, just black-hole the message. */
987 995 freemsg(mp);
988 996 }
989 997
990 998 /*
991 999 * Transmit a PF_KEY error message to the instance either pointed to
992 1000 * by ks, the instance with serial number serial, or more, depending.
993 1001 *
994 1002 * The faulty message (or a reasonable facsimile thereof) is in mp.
995 1003 * This function will free mp or recycle it for delivery, thereby causing
996 1004 * the stream head to free it.
997 1005 */
998 1006 static void
999 1007 keysock_error(keysock_t *ks, mblk_t *mp, int error, int diagnostic)
1000 1008 {
1001 1009 sadb_msg_t *samsg = (sadb_msg_t *)mp->b_rptr;
1002 1010 keysock_stack_t *keystack = ks->keysock_keystack;
1003 1011
1004 1012 ASSERT(mp->b_datap->db_type == M_DATA);
1005 1013
1006 1014 if (samsg->sadb_msg_type < SADB_GETSPI ||
1007 1015 samsg->sadb_msg_type > SADB_MAX)
1008 1016 samsg->sadb_msg_type = SADB_RESERVED;
1009 1017
1010 1018 /*
1011 1019 * Strip out extension headers.
1012 1020 */
1013 1021 ASSERT(mp->b_rptr + sizeof (*samsg) <= mp->b_datap->db_lim);
1014 1022 mp->b_wptr = mp->b_rptr + sizeof (*samsg);
1015 1023 samsg->sadb_msg_len = SADB_8TO64(sizeof (sadb_msg_t));
1016 1024 samsg->sadb_msg_errno = (uint8_t)error;
1017 1025 samsg->sadb_x_msg_diagnostic = (uint16_t)diagnostic;
1018 1026
1019 1027 keysock_passup(mp, samsg, ks->keysock_serial, NULL, B_FALSE, keystack);
1020 1028 }
1021 1029
1022 1030 /*
1023 1031 * Pass down a message to a consumer. Wrap it in KEYSOCK_IN, and copy
1024 1032 * in the extv if passed in.
1025 1033 */
1026 1034 static void
1027 1035 keysock_passdown(keysock_t *ks, mblk_t *mp, uint8_t satype, sadb_ext_t *extv[],
1028 1036 boolean_t flushmsg)
1029 1037 {
1030 1038 keysock_consumer_t *kc;
1031 1039 mblk_t *wrapper;
1032 1040 keysock_in_t *ksi;
1033 1041 int i;
1034 1042 keysock_stack_t *keystack = ks->keysock_keystack;
1035 1043
1036 1044 wrapper = allocb(sizeof (ipsec_info_t), BPRI_HI);
1037 1045 if (wrapper == NULL) {
1038 1046 ks3dbg(keystack, ("keysock_passdown: allocb failed.\n"));
1039 1047 if (extv[SADB_EXT_KEY_ENCRYPT] != NULL)
1040 1048 bzero(extv[SADB_EXT_KEY_ENCRYPT],
1041 1049 SADB_64TO8(
1042 1050 extv[SADB_EXT_KEY_ENCRYPT]->sadb_ext_len));
1043 1051 if (extv[SADB_EXT_KEY_AUTH] != NULL)
1044 1052 bzero(extv[SADB_EXT_KEY_AUTH],
1045 1053 SADB_64TO8(
1046 1054 extv[SADB_EXT_KEY_AUTH]->sadb_ext_len));
1047 1055 if (flushmsg) {
1048 1056 ks0dbg((
1049 1057 "keysock: Downwards flush/dump message failed!\n"));
1050 1058 /* If this is true, I hold the perimeter. */
1051 1059 keystack->keystack_flushdump--;
1052 1060 }
1053 1061 freemsg(mp);
1054 1062 return;
1055 1063 }
1056 1064
1057 1065 wrapper->b_datap->db_type = M_CTL;
1058 1066 ksi = (keysock_in_t *)wrapper->b_rptr;
1059 1067 ksi->ks_in_type = KEYSOCK_IN;
1060 1068 ksi->ks_in_len = sizeof (keysock_in_t);
1061 1069 if (extv[SADB_EXT_ADDRESS_SRC] != NULL)
1062 1070 ksi->ks_in_srctype = KS_IN_ADDR_UNKNOWN;
1063 1071 else ksi->ks_in_srctype = KS_IN_ADDR_NOTTHERE;
1064 1072 if (extv[SADB_EXT_ADDRESS_DST] != NULL)
1065 1073 ksi->ks_in_dsttype = KS_IN_ADDR_UNKNOWN;
1066 1074 else ksi->ks_in_dsttype = KS_IN_ADDR_NOTTHERE;
1067 1075 for (i = 0; i <= SADB_EXT_MAX; i++)
1068 1076 ksi->ks_in_extv[i] = extv[i];
1069 1077 ksi->ks_in_serial = ks->keysock_serial;
1070 1078 wrapper->b_wptr += sizeof (ipsec_info_t);
1071 1079 wrapper->b_cont = mp;
1072 1080
1073 1081 /*
1074 1082 * Find the appropriate consumer where the message is passed down.
1075 1083 */
1076 1084 kc = keystack->keystack_consumers[satype];
1077 1085 if (kc == NULL) {
1078 1086 freeb(wrapper);
1079 1087 keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE);
1080 1088 if (flushmsg) {
1081 1089 ks0dbg((
1082 1090 "keysock: Downwards flush/dump message failed!\n"));
1083 1091 /* If this is true, I hold the perimeter. */
1084 1092 keystack->keystack_flushdump--;
1085 1093 }
1086 1094 return;
1087 1095 }
1088 1096
1089 1097 /*
1090 1098 * NOTE: There used to be code in here to spin while a flush or
1091 1099 * dump finished. Keysock now assumes that consumers have enough
1092 1100 * MT-savviness to deal with that.
1093 1101 */
1094 1102
1095 1103 /*
1096 1104 * Current consumers (AH and ESP) are guaranteed to return a
1097 1105 * FLUSH or DUMP message back, so when we reach here, we don't
1098 1106 * have to worry about keysock_flushdumps.
1099 1107 */
1100 1108
1101 1109 putnext(kc->kc_wq, wrapper);
1102 1110 }
1103 1111
1104 1112 /*
1105 1113 * High-level reality checking of extensions.
1106 1114 */
1107 1115 static boolean_t
1108 1116 ext_check(sadb_ext_t *ext, keysock_stack_t *keystack)
1109 1117 {
1110 1118 int i;
1111 1119 uint64_t *lp;
1112 1120 sadb_ident_t *id;
1113 1121 char *idstr;
1114 1122
1115 1123 switch (ext->sadb_ext_type) {
1116 1124 case SADB_EXT_ADDRESS_SRC:
1117 1125 case SADB_EXT_ADDRESS_DST:
1118 1126 case SADB_X_EXT_ADDRESS_INNER_SRC:
1119 1127 case SADB_X_EXT_ADDRESS_INNER_DST:
1120 1128 /* Check for at least enough addtl length for a sockaddr. */
1121 1129 if (ext->sadb_ext_len <= SADB_8TO64(sizeof (sadb_address_t)))
1122 1130 return (B_FALSE);
1123 1131 break;
1124 1132 case SADB_EXT_LIFETIME_HARD:
1125 1133 case SADB_EXT_LIFETIME_SOFT:
1126 1134 case SADB_EXT_LIFETIME_CURRENT:
1127 1135 if (ext->sadb_ext_len != SADB_8TO64(sizeof (sadb_lifetime_t)))
1128 1136 return (B_FALSE);
1129 1137 break;
1130 1138 case SADB_EXT_SPIRANGE:
1131 1139 /* See if the SPI range is legit. */
1132 1140 if (htonl(((sadb_spirange_t *)ext)->sadb_spirange_min) >
1133 1141 htonl(((sadb_spirange_t *)ext)->sadb_spirange_max))
1134 1142 return (B_FALSE);
1135 1143 break;
1136 1144 case SADB_EXT_KEY_AUTH:
1137 1145 case SADB_EXT_KEY_ENCRYPT:
1138 1146 /* Key length check. */
1139 1147 if (((sadb_key_t *)ext)->sadb_key_bits == 0)
1140 1148 return (B_FALSE);
1141 1149 /*
1142 1150 * Check to see if the key length (in bits) is less than the
1143 1151 * extension length (in 8-bits words).
1144 1152 */
1145 1153 if ((roundup(SADB_1TO8(((sadb_key_t *)ext)->sadb_key_bits), 8) +
1146 1154 sizeof (sadb_key_t)) != SADB_64TO8(ext->sadb_ext_len)) {
1147 1155 ks1dbg(keystack, (
1148 1156 "ext_check: Key bits/length inconsistent.\n"));
1149 1157 ks1dbg(keystack, ("%d bits, len is %d bytes.\n",
1150 1158 ((sadb_key_t *)ext)->sadb_key_bits,
1151 1159 SADB_64TO8(ext->sadb_ext_len)));
1152 1160 return (B_FALSE);
1153 1161 }
1154 1162
1155 1163 /* All-zeroes key check. */
1156 1164 lp = (uint64_t *)(((char *)ext) + sizeof (sadb_key_t));
1157 1165 for (i = 0;
1158 1166 i < (ext->sadb_ext_len - SADB_8TO64(sizeof (sadb_key_t)));
1159 1167 i++)
1160 1168 if (lp[i] != 0)
1161 1169 break; /* Out of for loop. */
1162 1170 /* If finished the loop naturally, it's an all zero key. */
1163 1171 if (lp[i] == 0)
1164 1172 return (B_FALSE);
1165 1173 break;
1166 1174 case SADB_EXT_IDENTITY_SRC:
1167 1175 case SADB_EXT_IDENTITY_DST:
1168 1176 /*
1169 1177 * Make sure the strings in these identities are
1170 1178 * null-terminated. RFC 2367 underspecified how to handle
1171 1179 * such a case. I "proactively" null-terminate the string
1172 1180 * at the last byte if it's not terminated sooner.
1173 1181 */
1174 1182 id = (sadb_ident_t *)ext;
1175 1183 i = SADB_64TO8(id->sadb_ident_len);
1176 1184 i -= sizeof (sadb_ident_t);
1177 1185 idstr = (char *)(id + 1);
1178 1186 while (*idstr != '\0' && i > 0) {
1179 1187 i--;
1180 1188 idstr++;
1181 1189 }
1182 1190 if (i == 0) {
1183 1191 /*
1184 1192 * I.e., if the bozo user didn't NULL-terminate the
1185 1193 * string...
1186 1194 */
1187 1195 idstr--;
1188 1196 *idstr = '\0';
1189 1197 }
1190 1198 break;
1191 1199 }
1192 1200 return (B_TRUE); /* For now... */
1193 1201 }
1194 1202
1195 1203 /* Return values for keysock_get_ext(). */
1196 1204 #define KGE_OK 0
1197 1205 #define KGE_DUP 1
1198 1206 #define KGE_UNK 2
1199 1207 #define KGE_LEN 3
1200 1208 #define KGE_CHK 4
1201 1209
1202 1210 /*
1203 1211 * Parse basic extension headers and return in the passed-in pointer vector.
1204 1212 * Return values include:
1205 1213 *
1206 1214 * KGE_OK Everything's nice and parsed out.
1207 1215 * If there are no extensions, place NULL in extv[0].
1208 1216 * KGE_DUP There is a duplicate extension.
1209 1217 * First instance in appropriate bin. First duplicate in
1210 1218 * extv[0].
1211 1219 * KGE_UNK Unknown extension type encountered. extv[0] contains
1212 1220 * unknown header.
1213 1221 * KGE_LEN Extension length error.
1214 1222 * KGE_CHK High-level reality check failed on specific extension.
1215 1223 *
1216 1224 * My apologies for some of the pointer arithmetic in here. I'm thinking
1217 1225 * like an assembly programmer, yet trying to make the compiler happy.
1218 1226 */
1219 1227 static int
1220 1228 keysock_get_ext(sadb_ext_t *extv[], sadb_msg_t *basehdr, uint_t msgsize,
1221 1229 keysock_stack_t *keystack)
1222 1230 {
1223 1231 bzero(extv, sizeof (sadb_ext_t *) * (SADB_EXT_MAX + 1));
1224 1232
1225 1233 /* Use extv[0] as the "current working pointer". */
1226 1234
1227 1235 extv[0] = (sadb_ext_t *)(basehdr + 1);
1228 1236
1229 1237 while (extv[0] < (sadb_ext_t *)(((uint8_t *)basehdr) + msgsize)) {
1230 1238 /* Check for unknown headers. */
1231 1239 if (extv[0]->sadb_ext_type == 0 ||
1232 1240 extv[0]->sadb_ext_type > SADB_EXT_MAX)
1233 1241 return (KGE_UNK);
1234 1242
1235 1243 /*
1236 1244 * Check length. Use uint64_t because extlen is in units
1237 1245 * of 64-bit words. If length goes beyond the msgsize,
1238 1246 * return an error. (Zero length also qualifies here.)
1239 1247 */
1240 1248 if (extv[0]->sadb_ext_len == 0 ||
1241 1249 (void *)((uint64_t *)extv[0] + extv[0]->sadb_ext_len) >
1242 1250 (void *)((uint8_t *)basehdr + msgsize))
1243 1251 return (KGE_LEN);
1244 1252
1245 1253 /* Check for redundant headers. */
1246 1254 if (extv[extv[0]->sadb_ext_type] != NULL)
1247 1255 return (KGE_DUP);
1248 1256
1249 1257 /*
1250 1258 * Reality check the extension if possible at the keysock
1251 1259 * level.
1252 1260 */
1253 1261 if (!ext_check(extv[0], keystack))
1254 1262 return (KGE_CHK);
1255 1263
1256 1264 /* If I make it here, assign the appropriate bin. */
1257 1265 extv[extv[0]->sadb_ext_type] = extv[0];
1258 1266
1259 1267 /* Advance pointer (See above for uint64_t ptr reasoning.) */
1260 1268 extv[0] = (sadb_ext_t *)
1261 1269 ((uint64_t *)extv[0] + extv[0]->sadb_ext_len);
1262 1270 }
1263 1271
1264 1272 /* Everything's cool. */
1265 1273
1266 1274 /*
1267 1275 * If extv[0] == NULL, then there are no extension headers in this
1268 1276 * message. Ensure that this is the case.
1269 1277 */
1270 1278 if (extv[0] == (sadb_ext_t *)(basehdr + 1))
1271 1279 extv[0] = NULL;
1272 1280
1273 1281 return (KGE_OK);
1274 1282 }
1275 1283
1276 1284 /*
1277 1285 * qwriter() callback to handle flushes and dumps. This routine will hold
1278 1286 * the inner perimeter.
1279 1287 */
1280 1288 void
1281 1289 keysock_do_flushdump(queue_t *q, mblk_t *mp)
1282 1290 {
1283 1291 int i, start, finish;
1284 1292 mblk_t *mp1 = NULL;
1285 1293 keysock_t *ks = (keysock_t *)q->q_ptr;
1286 1294 sadb_ext_t *extv[SADB_EXT_MAX + 1];
1287 1295 sadb_msg_t *samsg = (sadb_msg_t *)mp->b_rptr;
1288 1296 keysock_stack_t *keystack = ks->keysock_keystack;
1289 1297
1290 1298 /*
1291 1299 * I am guaranteed this will work. I did the work in keysock_parse()
1292 1300 * already.
1293 1301 */
1294 1302 (void) keysock_get_ext(extv, samsg, SADB_64TO8(samsg->sadb_msg_len),
1295 1303 keystack);
1296 1304
1297 1305 /*
1298 1306 * I hold the perimeter, therefore I don't need to use atomic ops.
1299 1307 */
1300 1308 if (keystack->keystack_flushdump != 0) {
1301 1309 /* XXX Should I instead use EBUSY? */
1302 1310 /* XXX Or is there a way to queue these up? */
1303 1311 keysock_error(ks, mp, ENOMEM, SADB_X_DIAGNOSTIC_NONE);
1304 1312 return;
1305 1313 }
1306 1314
1307 1315 if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) {
1308 1316 start = 0;
1309 1317 finish = KEYSOCK_MAX_CONSUMERS - 1;
1310 1318 } else {
1311 1319 start = samsg->sadb_msg_satype;
1312 1320 finish = samsg->sadb_msg_satype;
1313 1321 }
1314 1322
1315 1323 /*
1316 1324 * Fill up keysock_flushdump with the number of outstanding dumps
1317 1325 * and/or flushes.
1318 1326 */
1319 1327
1320 1328 keystack->keystack_flushdump_errno = 0;
1321 1329
1322 1330 /*
1323 1331 * Okay, I hold the perimeter. Eventually keysock_flushdump will
1324 1332 * contain the number of consumers with outstanding flush operations.
1325 1333 *
1326 1334 * SO, here's the plan:
1327 1335 * * For each relevant consumer (Might be one, might be all)
1328 1336 * * Twiddle on the FLUSHING flag.
1329 1337 * * Pass down the FLUSH/DUMP message.
1330 1338 *
1331 1339 * When I see upbound FLUSH/DUMP messages, I will decrement the
1332 1340 * keysock_flushdump. When I decrement it to 0, I will pass the
1333 1341 * FLUSH/DUMP message back up to the PF_KEY sockets. Because I will
1334 1342 * pass down the right SA type to the consumer (either its own, or
1335 1343 * that of UNSPEC), the right one will be reflected from each consumer,
1336 1344 * and accordingly back to the socket.
1337 1345 */
1338 1346
1339 1347 mutex_enter(&keystack->keystack_consumers_lock);
1340 1348 for (i = start; i <= finish; i++) {
1341 1349 if (keystack->keystack_consumers[i] != NULL) {
1342 1350 mp1 = copymsg(mp);
1343 1351 if (mp1 == NULL) {
1344 1352 ks0dbg(("SADB_FLUSH copymsg() failed.\n"));
1345 1353 /*
1346 1354 * Error? And what about outstanding
1347 1355 * flushes? Oh, yeah, they get sucked up and
1348 1356 * the counter is decremented. Consumers
1349 1357 * (see keysock_passdown()) are guaranteed
1350 1358 * to deliver back a flush request, even if
1351 1359 * it's an error.
1352 1360 */
1353 1361 keysock_error(ks, mp, ENOMEM,
1354 1362 SADB_X_DIAGNOSTIC_NONE);
1355 1363 return;
1356 1364 }
1357 1365 /*
1358 1366 * Because my entry conditions are met above, the
1359 1367 * following assertion should hold true.
1360 1368 */
1361 1369 mutex_enter(&keystack->keystack_consumers[i]->kc_lock);
1362 1370 ASSERT((keystack->keystack_consumers[i]->kc_flags &
1363 1371 KC_FLUSHING) == 0);
1364 1372 keystack->keystack_consumers[i]->kc_flags |=
1365 1373 KC_FLUSHING;
1366 1374 mutex_exit(&(keystack->keystack_consumers[i]->kc_lock));
1367 1375 /* Always increment the number of flushes... */
1368 1376 keystack->keystack_flushdump++;
1369 1377 /* Guaranteed to return a message. */
1370 1378 keysock_passdown(ks, mp1, i, extv, B_TRUE);
1371 1379 } else if (start == finish) {
1372 1380 /*
1373 1381 * In case where start == finish, and there's no
1374 1382 * consumer, should we force an error? Yes.
1375 1383 */
1376 1384 mutex_exit(&keystack->keystack_consumers_lock);
1377 1385 keysock_error(ks, mp, EINVAL,
1378 1386 SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE);
1379 1387 return;
1380 1388 }
1381 1389 }
1382 1390 mutex_exit(&keystack->keystack_consumers_lock);
1383 1391
1384 1392 if (keystack->keystack_flushdump == 0) {
1385 1393 /*
1386 1394 * There were no consumers at all for this message.
1387 1395 * XXX For now return ESRCH.
1388 1396 */
1389 1397 keysock_error(ks, mp, ESRCH, SADB_X_DIAGNOSTIC_NO_SADBS);
1390 1398 } else {
1391 1399 /* Otherwise, free the original message. */
1392 1400 freemsg(mp);
1393 1401 }
1394 1402 }
1395 1403
1396 1404 /*
1397 1405 * Get the right diagnostic for a duplicate. Should probably use a static
1398 1406 * table lookup.
1399 1407 */
1400 1408 int
1401 1409 keysock_duplicate(int ext_type)
1402 1410 {
1403 1411 int rc = 0;
1404 1412
1405 1413 switch (ext_type) {
1406 1414 case SADB_EXT_ADDRESS_SRC:
1407 1415 rc = SADB_X_DIAGNOSTIC_DUPLICATE_SRC;
1408 1416 break;
1409 1417 case SADB_EXT_ADDRESS_DST:
1410 1418 rc = SADB_X_DIAGNOSTIC_DUPLICATE_DST;
1411 1419 break;
1412 1420 case SADB_X_EXT_ADDRESS_INNER_SRC:
1413 1421 rc = SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC;
1414 1422 break;
1415 1423 case SADB_X_EXT_ADDRESS_INNER_DST:
1416 1424 rc = SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST;
1417 1425 break;
1418 1426 case SADB_EXT_SA:
1419 1427 rc = SADB_X_DIAGNOSTIC_DUPLICATE_SA;
1420 1428 break;
1421 1429 case SADB_EXT_SPIRANGE:
1422 1430 rc = SADB_X_DIAGNOSTIC_DUPLICATE_RANGE;
1423 1431 break;
1424 1432 case SADB_EXT_KEY_AUTH:
1425 1433 rc = SADB_X_DIAGNOSTIC_DUPLICATE_AKEY;
1426 1434 break;
1427 1435 case SADB_EXT_KEY_ENCRYPT:
1428 1436 rc = SADB_X_DIAGNOSTIC_DUPLICATE_EKEY;
1429 1437 break;
1430 1438 }
1431 1439 return (rc);
1432 1440 }
1433 1441
1434 1442 /*
1435 1443 * Get the right diagnostic for a reality check failure. Should probably use
1436 1444 * a static table lookup.
1437 1445 */
1438 1446 int
1439 1447 keysock_malformed(int ext_type)
1440 1448 {
1441 1449 int rc = 0;
1442 1450
1443 1451 switch (ext_type) {
1444 1452 case SADB_EXT_ADDRESS_SRC:
1445 1453 rc = SADB_X_DIAGNOSTIC_MALFORMED_SRC;
1446 1454 break;
1447 1455 case SADB_EXT_ADDRESS_DST:
1448 1456 rc = SADB_X_DIAGNOSTIC_MALFORMED_DST;
1449 1457 break;
1450 1458 case SADB_X_EXT_ADDRESS_INNER_SRC:
1451 1459 rc = SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC;
1452 1460 break;
1453 1461 case SADB_X_EXT_ADDRESS_INNER_DST:
1454 1462 rc = SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST;
1455 1463 break;
1456 1464 case SADB_EXT_SA:
1457 1465 rc = SADB_X_DIAGNOSTIC_MALFORMED_SA;
1458 1466 break;
1459 1467 case SADB_EXT_SPIRANGE:
1460 1468 rc = SADB_X_DIAGNOSTIC_MALFORMED_RANGE;
1461 1469 break;
1462 1470 case SADB_EXT_KEY_AUTH:
1463 1471 rc = SADB_X_DIAGNOSTIC_MALFORMED_AKEY;
1464 1472 break;
1465 1473 case SADB_EXT_KEY_ENCRYPT:
1466 1474 rc = SADB_X_DIAGNOSTIC_MALFORMED_EKEY;
1467 1475 break;
1468 1476 }
1469 1477 return (rc);
1470 1478 }
1471 1479
1472 1480 /*
1473 1481 * Keysock massaging of an inverse ACQUIRE. Consult policy,
1474 1482 * and construct an appropriate response.
1475 1483 */
1476 1484 static void
1477 1485 keysock_inverse_acquire(mblk_t *mp, sadb_msg_t *samsg, sadb_ext_t *extv[],
1478 1486 keysock_t *ks)
1479 1487 {
1480 1488 mblk_t *reply_mp;
1481 1489 keysock_stack_t *keystack = ks->keysock_keystack;
1482 1490
1483 1491 /*
1484 1492 * Reality check things...
1485 1493 */
1486 1494 if (extv[SADB_EXT_ADDRESS_SRC] == NULL) {
1487 1495 keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_MISSING_SRC);
1488 1496 return;
1489 1497 }
1490 1498 if (extv[SADB_EXT_ADDRESS_DST] == NULL) {
1491 1499 keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_MISSING_DST);
1492 1500 return;
1493 1501 }
1494 1502
1495 1503 if (extv[SADB_X_EXT_ADDRESS_INNER_SRC] != NULL &&
1496 1504 extv[SADB_X_EXT_ADDRESS_INNER_DST] == NULL) {
1497 1505 keysock_error(ks, mp, EINVAL,
1498 1506 SADB_X_DIAGNOSTIC_MISSING_INNER_DST);
1499 1507 return;
1500 1508 }
1501 1509
1502 1510 if (extv[SADB_X_EXT_ADDRESS_INNER_SRC] == NULL &&
1503 1511 extv[SADB_X_EXT_ADDRESS_INNER_DST] != NULL) {
1504 1512 keysock_error(ks, mp, EINVAL,
1505 1513 SADB_X_DIAGNOSTIC_MISSING_INNER_SRC);
1506 1514 return;
1507 1515 }
1508 1516
1509 1517 reply_mp = ipsec_construct_inverse_acquire(samsg, extv,
1510 1518 keystack->keystack_netstack);
1511 1519
1512 1520 if (reply_mp != NULL) {
1513 1521 freemsg(mp);
1514 1522 keysock_passup(reply_mp, (sadb_msg_t *)reply_mp->b_rptr,
1515 1523 ks->keysock_serial, NULL, B_FALSE, keystack);
1516 1524 } else {
1517 1525 keysock_error(ks, mp, samsg->sadb_msg_errno,
1518 1526 samsg->sadb_x_msg_diagnostic);
1519 1527 }
1520 1528 }
1521 1529
1522 1530 /*
1523 1531 * Spew an extended REGISTER down to the relevant consumers.
1524 1532 */
1525 1533 static void
1526 1534 keysock_extended_register(keysock_t *ks, mblk_t *mp, sadb_ext_t *extv[])
1527 1535 {
1528 1536 sadb_x_ereg_t *ereg = (sadb_x_ereg_t *)extv[SADB_X_EXT_EREG];
1529 1537 uint8_t *satypes, *fencepost;
1530 1538 mblk_t *downmp;
1531 1539 sadb_ext_t *downextv[SADB_EXT_MAX + 1];
1532 1540 keysock_stack_t *keystack = ks->keysock_keystack;
1533 1541
1534 1542 if (ks->keysock_registered[0] != 0 || ks->keysock_registered[1] != 0 ||
1535 1543 ks->keysock_registered[2] != 0 || ks->keysock_registered[3] != 0) {
1536 1544 keysock_error(ks, mp, EBUSY, 0);
1537 1545 }
1538 1546
1539 1547 ks->keysock_flags |= KEYSOCK_EXTENDED;
1540 1548 if (ereg == NULL) {
1541 1549 keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_SATYPE_NEEDED);
1542 1550 } else {
1543 1551 ASSERT(mp->b_rptr + msgdsize(mp) == mp->b_wptr);
1544 1552 fencepost = (uint8_t *)mp->b_wptr;
1545 1553 satypes = ereg->sadb_x_ereg_satypes;
1546 1554 while (*satypes != SADB_SATYPE_UNSPEC && satypes != fencepost) {
1547 1555 downmp = copymsg(mp);
1548 1556 if (downmp == NULL) {
1549 1557 keysock_error(ks, mp, ENOMEM, 0);
1550 1558 return;
1551 1559 }
1552 1560 /*
1553 1561 * Since we've made it here, keysock_get_ext will work!
1554 1562 */
1555 1563 (void) keysock_get_ext(downextv,
1556 1564 (sadb_msg_t *)downmp->b_rptr, msgdsize(downmp),
1557 1565 keystack);
1558 1566 keysock_passdown(ks, downmp, *satypes, downextv,
1559 1567 B_FALSE);
1560 1568 ++satypes;
1561 1569 }
1562 1570 freemsg(mp);
1563 1571 }
1564 1572
1565 1573 /*
1566 1574 * Set global to indicate we prefer an extended ACQUIRE.
1567 1575 */
1568 1576 atomic_inc_32(&keystack->keystack_num_extended);
1569 1577 }
1570 1578
1571 1579 static void
1572 1580 keysock_delpair_all(keysock_t *ks, mblk_t *mp, sadb_ext_t *extv[])
1573 1581 {
1574 1582 int i, start, finish;
1575 1583 mblk_t *mp1 = NULL;
1576 1584 keysock_stack_t *keystack = ks->keysock_keystack;
1577 1585
1578 1586 start = 0;
1579 1587 finish = KEYSOCK_MAX_CONSUMERS - 1;
1580 1588
1581 1589 for (i = start; i <= finish; i++) {
1582 1590 if (keystack->keystack_consumers[i] != NULL) {
1583 1591 mp1 = copymsg(mp);
1584 1592 if (mp1 == NULL) {
1585 1593 keysock_error(ks, mp, ENOMEM,
1586 1594 SADB_X_DIAGNOSTIC_NONE);
1587 1595 return;
1588 1596 }
1589 1597 keysock_passdown(ks, mp1, i, extv, B_FALSE);
1590 1598 }
1591 1599 }
1592 1600 }
1593 1601
1594 1602 /*
1595 1603 * Handle PF_KEY messages.
1596 1604 */
1597 1605 static void
1598 1606 keysock_parse(queue_t *q, mblk_t *mp)
1599 1607 {
1600 1608 sadb_msg_t *samsg;
1601 1609 sadb_ext_t *extv[SADB_EXT_MAX + 1];
1602 1610 keysock_t *ks = (keysock_t *)q->q_ptr;
1603 1611 uint_t msgsize;
1604 1612 uint8_t satype;
1605 1613 keysock_stack_t *keystack = ks->keysock_keystack;
1606 1614
1607 1615 /* Make sure I'm a PF_KEY socket. (i.e. nothing's below me) */
1608 1616 ASSERT(WR(q)->q_next == NULL);
1609 1617
1610 1618 samsg = (sadb_msg_t *)mp->b_rptr;
1611 1619 ks2dbg(keystack, ("Received possible PF_KEY message, type %d.\n",
1612 1620 samsg->sadb_msg_type));
1613 1621
1614 1622 msgsize = SADB_64TO8(samsg->sadb_msg_len);
1615 1623
1616 1624 if (msgdsize(mp) != msgsize) {
1617 1625 /*
1618 1626 * Message len incorrect w.r.t. actual size. Send an error
1619 1627 * (EMSGSIZE). It may be necessary to massage things a
1620 1628 * bit. For example, if the sadb_msg_type is hosed,
1621 1629 * I need to set it to SADB_RESERVED to get delivery to
1622 1630 * do the right thing. Then again, maybe just letting
1623 1631 * the error delivery do the right thing.
1624 1632 */
1625 1633 ks2dbg(keystack,
1626 1634 ("mblk (%lu) and base (%d) message sizes don't jibe.\n",
1627 1635 msgdsize(mp), msgsize));
1628 1636 keysock_error(ks, mp, EMSGSIZE, SADB_X_DIAGNOSTIC_NONE);
1629 1637 return;
1630 1638 }
1631 1639
1632 1640 if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) {
1633 1641 /* Get all message into one mblk. */
1634 1642 if (pullupmsg(mp, -1) == 0) {
1635 1643 /*
1636 1644 * Something screwy happened.
1637 1645 */
1638 1646 ks3dbg(keystack,
1639 1647 ("keysock_parse: pullupmsg() failed.\n"));
1640 1648 return;
1641 1649 } else {
1642 1650 samsg = (sadb_msg_t *)mp->b_rptr;
1643 1651 }
1644 1652 }
1645 1653
1646 1654 switch (keysock_get_ext(extv, samsg, msgsize, keystack)) {
1647 1655 case KGE_DUP:
1648 1656 /* Handle duplicate extension. */
1649 1657 ks1dbg(keystack, ("Got duplicate extension of type %d.\n",
1650 1658 extv[0]->sadb_ext_type));
1651 1659 keysock_error(ks, mp, EINVAL,
1652 1660 keysock_duplicate(extv[0]->sadb_ext_type));
1653 1661 return;
1654 1662 case KGE_UNK:
1655 1663 /* Handle unknown extension. */
1656 1664 ks1dbg(keystack, ("Got unknown extension of type %d.\n",
1657 1665 extv[0]->sadb_ext_type));
1658 1666 keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_UNKNOWN_EXT);
1659 1667 return;
1660 1668 case KGE_LEN:
1661 1669 /* Length error. */
1662 1670 ks1dbg(keystack,
1663 1671 ("Length %d on extension type %d overrun or 0.\n",
1664 1672 extv[0]->sadb_ext_len, extv[0]->sadb_ext_type));
1665 1673 keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_BAD_EXTLEN);
1666 1674 return;
1667 1675 case KGE_CHK:
1668 1676 /* Reality check failed. */
1669 1677 ks1dbg(keystack,
1670 1678 ("Reality check failed on extension type %d.\n",
1671 1679 extv[0]->sadb_ext_type));
1672 1680 keysock_error(ks, mp, EINVAL,
1673 1681 keysock_malformed(extv[0]->sadb_ext_type));
1674 1682 return;
1675 1683 default:
1676 1684 /* Default case is no errors. */
1677 1685 break;
1678 1686 }
1679 1687
1680 1688 switch (samsg->sadb_msg_type) {
1681 1689 case SADB_REGISTER:
1682 1690 /*
1683 1691 * There's a semantic weirdness in that a message OTHER than
1684 1692 * the return REGISTER message may be passed up if I set the
1685 1693 * registered bit BEFORE I pass it down.
1686 1694 *
1687 1695 * SOOOO, I'll not twiddle any registered bits until I see
1688 1696 * the upbound REGISTER (with a serial number in it).
1689 1697 */
1690 1698 if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) {
1691 1699 /* Handle extended register here. */
1692 1700 keysock_extended_register(ks, mp, extv);
1693 1701 return;
1694 1702 } else if (ks->keysock_flags & KEYSOCK_EXTENDED) {
1695 1703 keysock_error(ks, mp, EBUSY, 0);
1696 1704 return;
1697 1705 }
1698 1706 /* FALLTHRU */
1699 1707 case SADB_GETSPI:
1700 1708 case SADB_ADD:
1701 1709 case SADB_UPDATE:
1702 1710 case SADB_X_UPDATEPAIR:
1703 1711 case SADB_DELETE:
1704 1712 case SADB_X_DELPAIR:
1705 1713 case SADB_GET:
1706 1714 /*
1707 1715 * Pass down to appropriate consumer.
1708 1716 */
1709 1717 if (samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1710 1718 keysock_passdown(ks, mp, samsg->sadb_msg_satype, extv,
1711 1719 B_FALSE);
1712 1720 else keysock_error(ks, mp, EINVAL,
1713 1721 SADB_X_DIAGNOSTIC_SATYPE_NEEDED);
1714 1722 return;
1715 1723 case SADB_X_DELPAIR_STATE:
1716 1724 if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) {
1717 1725 keysock_delpair_all(ks, mp, extv);
1718 1726 } else {
1719 1727 keysock_passdown(ks, mp, samsg->sadb_msg_satype, extv,
1720 1728 B_FALSE);
1721 1729 }
1722 1730 return;
1723 1731 case SADB_ACQUIRE:
1724 1732 /*
1725 1733 * If I _receive_ an acquire, this means I should spread it
1726 1734 * out to registered sockets. Unless there's an errno...
1727 1735 *
1728 1736 * Need ADDRESS, may have ID, SENS, and PROP, unless errno,
1729 1737 * in which case there should be NO extensions.
1730 1738 *
1731 1739 * Return to registered.
1732 1740 */
1733 1741 if (samsg->sadb_msg_errno != 0) {
1734 1742 satype = samsg->sadb_msg_satype;
1735 1743 if (satype == SADB_SATYPE_UNSPEC) {
1736 1744 if (!(ks->keysock_flags & KEYSOCK_EXTENDED)) {
1737 1745 keysock_error(ks, mp, EINVAL,
1738 1746 SADB_X_DIAGNOSTIC_SATYPE_NEEDED);
1739 1747 return;
1740 1748 }
1741 1749 /*
1742 1750 * Reassign satype based on the first
1743 1751 * flags that KEYSOCK_SETREG says.
1744 1752 */
1745 1753 while (satype <= SADB_SATYPE_MAX) {
1746 1754 if (KEYSOCK_ISREG(ks, satype))
1747 1755 break;
1748 1756 satype++;
1749 1757 }
1750 1758 if (satype > SADB_SATYPE_MAX) {
1751 1759 keysock_error(ks, mp, EBUSY, 0);
1752 1760 return;
1753 1761 }
1754 1762 }
1755 1763 keysock_passdown(ks, mp, satype, extv, B_FALSE);
1756 1764 } else {
1757 1765 if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) {
1758 1766 keysock_error(ks, mp, EINVAL,
1759 1767 SADB_X_DIAGNOSTIC_SATYPE_NEEDED);
1760 1768 } else {
1761 1769 keysock_passup(mp, samsg, 0, NULL, B_FALSE,
1762 1770 keystack);
1763 1771 }
1764 1772 }
1765 1773 return;
1766 1774 case SADB_EXPIRE:
1767 1775 /*
1768 1776 * If someone sends this in, then send out to all senders.
1769 1777 * (Save maybe ESP or AH, I have to be careful here.)
1770 1778 *
1771 1779 * Need ADDRESS, may have ID and SENS.
1772 1780 *
1773 1781 * XXX for now this is unsupported.
1774 1782 */
1775 1783 break;
1776 1784 case SADB_FLUSH:
1777 1785 /*
1778 1786 * Nuke all SAs.
1779 1787 *
1780 1788 * No extensions at all. Return to all listeners.
1781 1789 *
1782 1790 * Question: Should I hold a lock here to prevent
1783 1791 * additions/deletions while flushing?
1784 1792 * Answer: No. (See keysock_passdown() for details.)
1785 1793 */
1786 1794 if (extv[0] != NULL) {
1787 1795 /*
1788 1796 * FLUSH messages shouldn't have extensions.
1789 1797 * Return EINVAL.
1790 1798 */
1791 1799 ks2dbg(keystack, ("FLUSH message with extension.\n"));
1792 1800 keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_NO_EXT);
1793 1801 return;
1794 1802 }
1795 1803
1796 1804 /* Passing down of DUMP/FLUSH messages are special. */
1797 1805 qwriter(q, mp, keysock_do_flushdump, PERIM_INNER);
1798 1806 return;
1799 1807 case SADB_DUMP: /* not used by normal applications */
1800 1808 if ((extv[0] != NULL) &&
1801 1809 ((msgsize >
1802 1810 (sizeof (sadb_msg_t) + sizeof (sadb_x_edump_t))) ||
1803 1811 (extv[SADB_X_EXT_EDUMP] == NULL))) {
1804 1812 keysock_error(ks, mp, EINVAL,
1805 1813 SADB_X_DIAGNOSTIC_NO_EXT);
1806 1814 return;
1807 1815 }
1808 1816 qwriter(q, mp, keysock_do_flushdump, PERIM_INNER);
1809 1817 return;
1810 1818 case SADB_X_PROMISC:
1811 1819 /*
1812 1820 * Promiscuous processing message.
1813 1821 */
1814 1822 if (samsg->sadb_msg_satype == 0)
1815 1823 ks->keysock_flags &= ~KEYSOCK_PROMISC;
1816 1824 else
1817 1825 ks->keysock_flags |= KEYSOCK_PROMISC;
1818 1826 keysock_passup(mp, samsg, ks->keysock_serial, NULL, B_FALSE,
1819 1827 keystack);
1820 1828 return;
1821 1829 case SADB_X_INVERSE_ACQUIRE:
1822 1830 keysock_inverse_acquire(mp, samsg, extv, ks);
1823 1831 return;
1824 1832 default:
1825 1833 ks2dbg(keystack, ("Got unknown message type %d.\n",
1826 1834 samsg->sadb_msg_type));
1827 1835 keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_UNKNOWN_MSG);
1828 1836 return;
1829 1837 }
1830 1838
1831 1839 /* As a placeholder... */
1832 1840 ks0dbg(("keysock_parse(): Hit EOPNOTSUPP\n"));
1833 1841 keysock_error(ks, mp, EOPNOTSUPP, SADB_X_DIAGNOSTIC_NONE);
1834 1842 }
1835 1843
1836 1844 /*
1837 1845 * wput routing for PF_KEY/keysock/whatever. Unlike the routing socket,
1838 1846 * I don't convert to ioctl()'s for IP. I am the end-all driver as far
1839 1847 * as PF_KEY sockets are concerned. I do some conversion, but not as much
1840 1848 * as IP/rts does.
1841 1849 */
1842 1850 static void
1843 1851 keysock_wput(queue_t *q, mblk_t *mp)
1844 1852 {
1845 1853 uchar_t *rptr = mp->b_rptr;
1846 1854 mblk_t *mp1;
1847 1855 keysock_t *ks;
1848 1856 keysock_stack_t *keystack;
1849 1857
1850 1858 if (WR(q)->q_next) {
1851 1859 keysock_consumer_t *kc = (keysock_consumer_t *)q->q_ptr;
1852 1860 keystack = kc->kc_keystack;
1853 1861
1854 1862 ks3dbg(keystack, ("In keysock_wput\n"));
1855 1863
1856 1864 /*
1857 1865 * We shouldn't get writes on a consumer instance.
1858 1866 * But for now, just passthru.
1859 1867 */
1860 1868 ks1dbg(keystack, ("Huh? wput for an consumer instance (%d)?\n",
1861 1869 kc->kc_sa_type));
1862 1870 putnext(q, mp);
1863 1871 return;
1864 1872 }
1865 1873 ks = (keysock_t *)q->q_ptr;
1866 1874 keystack = ks->keysock_keystack;
1867 1875
1868 1876 ks3dbg(keystack, ("In keysock_wput\n"));
1869 1877
1870 1878 switch (mp->b_datap->db_type) {
1871 1879 case M_DATA:
1872 1880 /*
1873 1881 * Silently discard.
1874 1882 */
1875 1883 ks2dbg(keystack, ("raw M_DATA in keysock.\n"));
1876 1884 freemsg(mp);
1877 1885 return;
1878 1886 case M_PROTO:
1879 1887 case M_PCPROTO:
1880 1888 if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) {
1881 1889 if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
1882 1890 if ((mp1 = mp->b_cont) == NULL) {
1883 1891 /* No data after T_DATA_REQ. */
1884 1892 ks2dbg(keystack,
1885 1893 ("No data after DATA_REQ.\n"));
1886 1894 freemsg(mp);
1887 1895 return;
1888 1896 }
1889 1897 freeb(mp);
1890 1898 mp = mp1;
1891 1899 ks2dbg(keystack, ("T_DATA_REQ\n"));
1892 1900 break; /* Out of switch. */
1893 1901 }
1894 1902 }
1895 1903 /* FALLTHRU */
1896 1904 default:
1897 1905 ks3dbg(keystack, ("In default wput case (%d %d).\n",
1898 1906 mp->b_datap->db_type, ((union T_primitives *)rptr)->type));
1899 1907 keysock_wput_other(q, mp);
1900 1908 return;
1901 1909 }
1902 1910
1903 1911 /* I now have a PF_KEY message in an M_DATA block, pointed to by mp. */
1904 1912 keysock_parse(q, mp);
1905 1913 }
1906 1914
1907 1915 /* BELOW THIS LINE ARE ROUTINES INCLUDING AND RELATED TO keysock_rput(). */
1908 1916
1909 1917 /*
1910 1918 * Called upon receipt of a KEYSOCK_HELLO_ACK to set up the appropriate
1911 1919 * state vectors.
1912 1920 */
1913 1921 static void
1914 1922 keysock_link_consumer(uint8_t satype, keysock_consumer_t *kc)
1915 1923 {
1916 1924 keysock_t *ks;
1917 1925 keysock_stack_t *keystack = kc->kc_keystack;
1918 1926
1919 1927 mutex_enter(&keystack->keystack_consumers_lock);
1920 1928 mutex_enter(&kc->kc_lock);
1921 1929 if (keystack->keystack_consumers[satype] != NULL) {
1922 1930 ks0dbg((
1923 1931 "Hmmmm, someone closed %d before the HELLO_ACK happened.\n",
1924 1932 satype));
1925 1933 /*
1926 1934 * Perhaps updating the new below-me consumer with what I have
1927 1935 * so far would work too?
1928 1936 */
1929 1937 mutex_exit(&kc->kc_lock);
1930 1938 mutex_exit(&keystack->keystack_consumers_lock);
1931 1939 } else {
1932 1940 /* Add new below-me consumer. */
1933 1941 keystack->keystack_consumers[satype] = kc;
1934 1942
1935 1943 kc->kc_flags = 0;
1936 1944 kc->kc_sa_type = satype;
1937 1945 mutex_exit(&kc->kc_lock);
1938 1946 mutex_exit(&keystack->keystack_consumers_lock);
1939 1947
1940 1948 /* Scan the keysock list. */
1941 1949 mutex_enter(&keystack->keystack_list_lock);
1942 1950 for (ks = keystack->keystack_list; ks != NULL;
1943 1951 ks = ks->keysock_next) {
1944 1952 if (KEYSOCK_ISREG(ks, satype)) {
1945 1953 /*
1946 1954 * XXX Perhaps send an SADB_REGISTER down on
1947 1955 * the socket's behalf.
1948 1956 */
1949 1957 ks1dbg(keystack,
1950 1958 ("Socket %u registered already for "
1951 1959 "new consumer.\n", ks->keysock_serial));
1952 1960 }
1953 1961 }
1954 1962 mutex_exit(&keystack->keystack_list_lock);
1955 1963 }
1956 1964 }
1957 1965
1958 1966 /*
1959 1967 * Generate a KEYSOCK_OUT_ERR message for my consumer.
1960 1968 */
1961 1969 static void
1962 1970 keysock_out_err(keysock_consumer_t *kc, int ks_errno, mblk_t *mp)
1963 1971 {
1964 1972 keysock_out_err_t *kse;
1965 1973 mblk_t *imp;
1966 1974 keysock_stack_t *keystack = kc->kc_keystack;
1967 1975
1968 1976 imp = allocb(sizeof (ipsec_info_t), BPRI_HI);
1969 1977 if (imp == NULL) {
1970 1978 ks1dbg(keystack, ("keysock_out_err: Can't alloc message.\n"));
1971 1979 return;
1972 1980 }
1973 1981
1974 1982 imp->b_datap->db_type = M_CTL;
1975 1983 imp->b_wptr += sizeof (ipsec_info_t);
1976 1984
1977 1985 kse = (keysock_out_err_t *)imp->b_rptr;
1978 1986 imp->b_cont = mp;
1979 1987 kse->ks_err_type = KEYSOCK_OUT_ERR;
1980 1988 kse->ks_err_len = sizeof (*kse);
1981 1989 /* Is serial necessary? */
1982 1990 kse->ks_err_serial = 0;
1983 1991 kse->ks_err_errno = ks_errno;
1984 1992
1985 1993 /*
1986 1994 * XXX What else do I need to do here w.r.t. information
1987 1995 * to tell the consumer what caused this error?
1988 1996 *
1989 1997 * I believe the answer is the PF_KEY ACQUIRE (or other) message
1990 1998 * attached in mp, which is appended at the end. I believe the
1991 1999 * db_ref won't matter here, because the PF_KEY message is only read
1992 2000 * for KEYSOCK_OUT_ERR.
1993 2001 */
1994 2002
1995 2003 putnext(kc->kc_wq, imp);
1996 2004 }
1997 2005
1998 2006 /* XXX this is a hack errno. */
1999 2007 #define EIPSECNOSA 255
2000 2008
2001 2009 /*
2002 2010 * Route message (pointed by mp, header in samsg) toward appropriate
2003 2011 * sockets. Assume the message's creator did its job correctly.
2004 2012 *
2005 2013 * This should be a function that is followed by a return in its caller.
2006 2014 * The compiler _should_ be able to use tail-call optimizations to make the
2007 2015 * large ## of parameters not a huge deal.
2008 2016 */
2009 2017 static void
2010 2018 keysock_passup(mblk_t *mp, sadb_msg_t *samsg, minor_t serial,
2011 2019 keysock_consumer_t *kc, boolean_t persistent, keysock_stack_t *keystack)
2012 2020 {
2013 2021 keysock_t *ks;
2014 2022 uint8_t satype = samsg->sadb_msg_satype;
2015 2023 boolean_t toall = B_FALSE, allreg = B_FALSE, allereg = B_FALSE,
2016 2024 setalg = B_FALSE;
2017 2025 mblk_t *mp1;
2018 2026 int err = EIPSECNOSA;
2019 2027
2020 2028 /* Convert mp, which is M_DATA, into an M_PROTO of type T_DATA_IND */
2021 2029 mp1 = allocb(sizeof (struct T_data_req), BPRI_HI);
2022 2030 if (mp1 == NULL) {
2023 2031 err = ENOMEM;
2024 2032 goto error;
2025 2033 }
2026 2034 mp1->b_wptr += sizeof (struct T_data_req);
2027 2035 ((struct T_data_ind *)mp1->b_rptr)->PRIM_type = T_DATA_IND;
2028 2036 ((struct T_data_ind *)mp1->b_rptr)->MORE_flag = 0;
2029 2037 mp1->b_datap->db_type = M_PROTO;
2030 2038 mp1->b_cont = mp;
2031 2039 mp = mp1;
2032 2040
2033 2041 switch (samsg->sadb_msg_type) {
2034 2042 case SADB_FLUSH:
2035 2043 case SADB_GETSPI:
2036 2044 case SADB_UPDATE:
2037 2045 case SADB_X_UPDATEPAIR:
2038 2046 case SADB_ADD:
2039 2047 case SADB_DELETE:
2040 2048 case SADB_X_DELPAIR:
2041 2049 case SADB_EXPIRE:
2042 2050 /*
2043 2051 * These are most likely replies. Don't worry about
2044 2052 * KEYSOCK_OUT_ERR handling. Deliver to all sockets.
2045 2053 */
2046 2054 ks3dbg(keystack,
2047 2055 ("Delivering normal message (%d) to all sockets.\n",
2048 2056 samsg->sadb_msg_type));
2049 2057 toall = B_TRUE;
2050 2058 break;
2051 2059 case SADB_REGISTER:
2052 2060 /*
2053 2061 * REGISTERs come up for one of three reasons:
2054 2062 *
2055 2063 * 1.) In response to a normal SADB_REGISTER
2056 2064 * (samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
2057 2065 * serial != 0)
2058 2066 * Deliver to normal SADB_REGISTERed sockets.
2059 2067 * 2.) In response to an extended REGISTER
2060 2068 * (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
2061 2069 * Deliver to extended REGISTERed socket.
2062 2070 * 3.) Spontaneous algorithm changes
2063 2071 * (samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
2064 2072 * serial == 0)
2065 2073 * Deliver to REGISTERed sockets of all sorts.
2066 2074 */
2067 2075 if (kc == NULL) {
2068 2076 /* Here because of keysock_error() call. */
2069 2077 ASSERT(samsg->sadb_msg_errno != 0);
2070 2078 break; /* Out of switch. */
2071 2079 }
2072 2080 ks3dbg(keystack, ("Delivering REGISTER.\n"));
2073 2081 if (satype == SADB_SATYPE_UNSPEC) {
2074 2082 /* REGISTER Reason #2 */
2075 2083 allereg = B_TRUE;
2076 2084 /*
2077 2085 * Rewhack SA type so PF_KEY socket holder knows what
2078 2086 * consumer generated this algorithm list.
2079 2087 */
2080 2088 satype = kc->kc_sa_type;
2081 2089 samsg->sadb_msg_satype = satype;
2082 2090 setalg = B_TRUE;
2083 2091 } else if (serial == 0) {
2084 2092 /* REGISTER Reason #3 */
2085 2093 allreg = B_TRUE;
2086 2094 allereg = B_TRUE;
2087 2095 } else {
2088 2096 /* REGISTER Reason #1 */
2089 2097 allreg = B_TRUE;
2090 2098 setalg = B_TRUE;
2091 2099 }
2092 2100 break;
2093 2101 case SADB_ACQUIRE:
2094 2102 /*
2095 2103 * ACQUIREs are either extended (sadb_msg_satype == 0) or
2096 2104 * regular (sadb_msg_satype != 0). And we're guaranteed
2097 2105 * that serial == 0 for an ACQUIRE.
2098 2106 */
2099 2107 ks3dbg(keystack, ("Delivering ACQUIRE.\n"));
2100 2108 allereg = (satype == SADB_SATYPE_UNSPEC);
2101 2109 allreg = !allereg;
2102 2110 /*
2103 2111 * Corner case - if we send a regular ACQUIRE and there's
2104 2112 * extended ones registered, don't send an error down to
2105 2113 * consumers if nobody's listening and prematurely destroy
2106 2114 * their ACQUIRE record. This might be too hackish of a
2107 2115 * solution.
2108 2116 */
2109 2117 if (allreg && keystack->keystack_num_extended > 0)
2110 2118 err = 0;
2111 2119 break;
2112 2120 case SADB_X_PROMISC:
2113 2121 case SADB_X_INVERSE_ACQUIRE:
2114 2122 case SADB_DUMP:
2115 2123 case SADB_GET:
2116 2124 default:
2117 2125 /*
2118 2126 * Deliver to the sender and promiscuous only.
2119 2127 */
2120 2128 ks3dbg(keystack, ("Delivering sender/promisc only (%d).\n",
2121 2129 samsg->sadb_msg_type));
2122 2130 break;
2123 2131 }
2124 2132
2125 2133 mutex_enter(&keystack->keystack_list_lock);
2126 2134 for (ks = keystack->keystack_list; ks != NULL; ks = ks->keysock_next) {
2127 2135 /* Delivery loop. */
2128 2136
2129 2137 /*
2130 2138 * Check special keysock-setting cases (REGISTER replies)
2131 2139 * here.
2132 2140 */
2133 2141 if (setalg && serial == ks->keysock_serial) {
2134 2142 ASSERT(kc != NULL);
2135 2143 ASSERT(kc->kc_sa_type == satype);
2136 2144 KEYSOCK_SETREG(ks, satype);
2137 2145 }
2138 2146
2139 2147 /*
2140 2148 * NOLOOP takes precedence over PROMISC. So if you've set
2141 2149 * !SO_USELOOPBACK, don't expect to see any data...
2142 2150 */
2143 2151 if (ks->keysock_flags & KEYSOCK_NOLOOP)
2144 2152 continue;
2145 2153
2146 2154 /*
2147 2155 * Messages to all, or promiscuous sockets just GET the
2148 2156 * message. Perform rules-type checking iff it's not for all
2149 2157 * listeners or the socket is in promiscuous mode.
2150 2158 *
2151 2159 * NOTE:Because of the (kc != NULL && ISREG()), make sure
2152 2160 * extended ACQUIREs arrive off a consumer that is
2153 2161 * part of the extended REGISTER set of consumers.
2154 2162 */
2155 2163 if (serial != ks->keysock_serial &&
2156 2164 !toall &&
2157 2165 !(ks->keysock_flags & KEYSOCK_PROMISC) &&
2158 2166 !((ks->keysock_flags & KEYSOCK_EXTENDED) ?
2159 2167 allereg : allreg && kc != NULL &&
2160 2168 KEYSOCK_ISREG(ks, kc->kc_sa_type)))
2161 2169 continue;
2162 2170
2163 2171 mp1 = dupmsg(mp);
2164 2172 if (mp1 == NULL) {
2165 2173 ks2dbg(keystack, (
2166 2174 "keysock_passup(): dupmsg() failed.\n"));
2167 2175 mp1 = mp;
2168 2176 mp = NULL;
2169 2177 err = ENOMEM;
2170 2178 }
2171 2179
2172 2180 /*
2173 2181 * At this point, we can deliver or attempt to deliver
2174 2182 * this message. We're free of obligation to report
2175 2183 * no listening PF_KEY sockets. So set err to 0.
2176 2184 */
2177 2185 err = 0;
2178 2186
2179 2187 /*
2180 2188 * See if we canputnext(), as well as see if the message
2181 2189 * needs to be queued if we can't.
2182 2190 */
2183 2191 if (!canputnext(ks->keysock_rq)) {
2184 2192 if (persistent) {
2185 2193 if (putq(ks->keysock_rq, mp1) == 0) {
2186 2194 ks1dbg(keystack, (
2187 2195 "keysock_passup: putq failed.\n"));
2188 2196 } else {
2189 2197 continue;
2190 2198 }
2191 2199 }
2192 2200 freemsg(mp1);
2193 2201 continue;
2194 2202 }
2195 2203
2196 2204 ks3dbg(keystack,
2197 2205 ("Putting to serial %d.\n", ks->keysock_serial));
2198 2206 /*
2199 2207 * Unlike the specific keysock instance case, this
2200 2208 * will only hit for listeners, so we will only
2201 2209 * putnext() if we can.
2202 2210 */
2203 2211 putnext(ks->keysock_rq, mp1);
2204 2212 if (mp == NULL)
2205 2213 break; /* out of for loop. */
2206 2214 }
2207 2215 mutex_exit(&keystack->keystack_list_lock);
2208 2216
2209 2217 error:
2210 2218 if ((err != 0) && (kc != NULL)) {
2211 2219 /*
2212 2220 * Generate KEYSOCK_OUT_ERR for consumer.
2213 2221 * Basically, I send this back if I have not been able to
2214 2222 * transmit (for whatever reason)
2215 2223 */
2216 2224 ks1dbg(keystack,
2217 2225 ("keysock_passup(): No registered of type %d.\n",
2218 2226 satype));
2219 2227 if (mp != NULL) {
2220 2228 if (mp->b_datap->db_type == M_PROTO) {
2221 2229 mp1 = mp;
2222 2230 mp = mp->b_cont;
2223 2231 freeb(mp1);
2224 2232 }
2225 2233 /*
2226 2234 * Do a copymsg() because people who get
2227 2235 * KEYSOCK_OUT_ERR may alter the message contents.
2228 2236 */
2229 2237 mp1 = copymsg(mp);
2230 2238 if (mp1 == NULL) {
2231 2239 ks2dbg(keystack,
2232 2240 ("keysock_passup: copymsg() failed.\n"));
2233 2241 mp1 = mp;
2234 2242 mp = NULL;
2235 2243 }
2236 2244 keysock_out_err(kc, err, mp1);
2237 2245 }
2238 2246 }
2239 2247
2240 2248 /*
2241 2249 * XXX Blank the message somehow. This is difficult because we don't
2242 2250 * know at this point if the message has db_ref > 1, etc.
2243 2251 *
2244 2252 * Optimally, keysock messages containing actual keying material would
2245 2253 * be allocated with esballoc(), with a zeroing free function.
2246 2254 */
2247 2255 if (mp != NULL)
2248 2256 freemsg(mp);
2249 2257 }
2250 2258
2251 2259 /*
2252 2260 * Keysock's read service procedure is there only for PF_KEY reply
2253 2261 * messages that really need to reach the top.
2254 2262 */
2255 2263 static void
2256 2264 keysock_rsrv(queue_t *q)
2257 2265 {
2258 2266 mblk_t *mp;
2259 2267
2260 2268 while ((mp = getq(q)) != NULL) {
2261 2269 if (canputnext(q)) {
2262 2270 putnext(q, mp);
2263 2271 } else {
2264 2272 (void) putbq(q, mp);
2265 2273 return;
2266 2274 }
2267 2275 }
2268 2276 }
2269 2277
2270 2278 /*
2271 2279 * The read procedure should only be invoked by a keysock consumer, like
2272 2280 * ESP, AH, etc. I should only see KEYSOCK_OUT and KEYSOCK_HELLO_ACK
2273 2281 * messages on my read queues.
2274 2282 */
2275 2283 static void
2276 2284 keysock_rput(queue_t *q, mblk_t *mp)
2277 2285 {
2278 2286 keysock_consumer_t *kc = (keysock_consumer_t *)q->q_ptr;
2279 2287 ipsec_info_t *ii;
2280 2288 keysock_hello_ack_t *ksa;
2281 2289 minor_t serial;
2282 2290 mblk_t *mp1;
2283 2291 sadb_msg_t *samsg;
2284 2292 keysock_stack_t *keystack = kc->kc_keystack;
2285 2293
2286 2294 /* Make sure I'm a consumer instance. (i.e. something's below me) */
2287 2295 ASSERT(WR(q)->q_next != NULL);
2288 2296
2289 2297 if (mp->b_datap->db_type != M_CTL) {
2290 2298 /*
2291 2299 * Keysock should only see keysock consumer interface
2292 2300 * messages (see ipsec_info.h) on its read procedure.
2293 2301 * To be robust, however, putnext() up so the STREAM head can
2294 2302 * deal with it appropriately.
2295 2303 */
2296 2304 ks1dbg(keystack,
2297 2305 ("Hmmm, a non M_CTL (%d, 0x%x) on keysock_rput.\n",
2298 2306 mp->b_datap->db_type, mp->b_datap->db_type));
2299 2307 putnext(q, mp);
2300 2308 return;
2301 2309 }
2302 2310
2303 2311 ii = (ipsec_info_t *)mp->b_rptr;
2304 2312
2305 2313 switch (ii->ipsec_info_type) {
2306 2314 case KEYSOCK_OUT:
2307 2315 /*
2308 2316 * A consumer needs to pass a response message or an ACQUIRE
2309 2317 * UP. I assume that the consumer has done the right
2310 2318 * thing w.r.t. message creation, etc.
2311 2319 */
2312 2320 serial = ((keysock_out_t *)mp->b_rptr)->ks_out_serial;
2313 2321 mp1 = mp->b_cont; /* Get M_DATA portion. */
2314 2322 freeb(mp);
2315 2323 samsg = (sadb_msg_t *)mp1->b_rptr;
2316 2324 if (samsg->sadb_msg_type == SADB_FLUSH ||
2317 2325 (samsg->sadb_msg_type == SADB_DUMP &&
2318 2326 samsg->sadb_msg_len == SADB_8TO64(sizeof (*samsg)))) {
2319 2327 /*
2320 2328 * If I'm an end-of-FLUSH or an end-of-DUMP marker...
2321 2329 */
2322 2330 ASSERT(keystack->keystack_flushdump != 0);
2323 2331 /* Am I flushing? */
2324 2332
2325 2333 mutex_enter(&kc->kc_lock);
2326 2334 kc->kc_flags &= ~KC_FLUSHING;
2327 2335 mutex_exit(&kc->kc_lock);
2328 2336
2329 2337 if (samsg->sadb_msg_errno != 0)
2330 2338 keystack->keystack_flushdump_errno =
2331 2339 samsg->sadb_msg_errno;
2332 2340
2333 2341 /*
2334 2342 * Lower the atomic "flushing" count. If it's
2335 2343 * the last one, send up the end-of-{FLUSH,DUMP} to
2336 2344 * the appropriate PF_KEY socket.
2337 2345 */
2338 2346 if (atomic_dec_32_nv(&keystack->keystack_flushdump) !=
2339 2347 0) {
2340 2348 ks1dbg(keystack,
2341 2349 ("One flush/dump message back from %d,"
2342 2350 " more to go.\n", samsg->sadb_msg_satype));
2343 2351 freemsg(mp1);
2344 2352 return;
2345 2353 }
2346 2354
2347 2355 samsg->sadb_msg_errno =
2348 2356 (uint8_t)keystack->keystack_flushdump_errno;
2349 2357 if (samsg->sadb_msg_type == SADB_DUMP) {
2350 2358 samsg->sadb_msg_seq = 0;
2351 2359 }
2352 2360 }
2353 2361 keysock_passup(mp1, samsg, serial, kc,
2354 2362 (samsg->sadb_msg_type == SADB_DUMP), keystack);
2355 2363 return;
2356 2364 case KEYSOCK_HELLO_ACK:
2357 2365 /* Aha, now we can link in the consumer! */
2358 2366 ksa = (keysock_hello_ack_t *)ii;
2359 2367 keysock_link_consumer(ksa->ks_hello_satype, kc);
2360 2368 freemsg(mp);
2361 2369 return;
2362 2370 default:
2363 2371 ks1dbg(keystack, ("Hmmm, an IPsec info I'm not used to, 0x%x\n",
2364 2372 ii->ipsec_info_type));
2365 2373 putnext(q, mp);
2366 2374 }
2367 2375 }
2368 2376
2369 2377 /*
2370 2378 * So we can avoid external linking problems....
2371 2379 */
2372 2380 boolean_t
2373 2381 keysock_extended_reg(netstack_t *ns)
2374 2382 {
2375 2383 keysock_stack_t *keystack = ns->netstack_keysock;
2376 2384
2377 2385 return (keystack->keystack_num_extended != 0);
2378 2386 }
2379 2387
2380 2388 uint32_t
2381 2389 keysock_next_seq(netstack_t *ns)
2382 2390 {
2383 2391 keysock_stack_t *keystack = ns->netstack_keysock;
2384 2392
2385 2393 return (atomic_dec_32_nv(&keystack->keystack_acquire_seq));
2386 2394 }
|
↓ open down ↓ |
1806 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX