Print this page
10687 Service routine cast changes need smatch fixes
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/inet/ip/spdsock.c
+++ new/usr/src/uts/common/inet/ip/spdsock.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 (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2012 Nexenta Systems, Inc. All rights reserved.
24 - * Copyright 2017 Joyent, Inc.
24 + * Copyright 2019, Joyent, Inc.
25 25 */
26 26
27 27 #include <sys/param.h>
28 28 #include <sys/types.h>
29 29 #include <sys/stream.h>
30 30 #include <sys/strsubr.h>
31 31 #include <sys/strsun.h>
32 32 #include <sys/stropts.h>
33 33 #include <sys/zone.h>
34 34 #include <sys/vnode.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/ddi.h>
40 40 #include <sys/sunddi.h>
41 41 #include <sys/mkdev.h>
42 42 #include <sys/debug.h>
43 43 #include <sys/kmem.h>
44 44 #include <sys/cmn_err.h>
45 45 #include <sys/suntpi.h>
46 46 #include <sys/policy.h>
47 47 #include <sys/dls.h>
48 48
49 49 #include <sys/socket.h>
50 50 #include <netinet/in.h>
51 51 #include <net/pfkeyv2.h>
52 52 #include <net/pfpolicy.h>
53 53
54 54 #include <inet/common.h>
55 55 #include <netinet/ip6.h>
56 56 #include <inet/ip.h>
57 57 #include <inet/ip6.h>
58 58 #include <inet/mi.h>
59 59 #include <inet/proto_set.h>
60 60 #include <inet/nd.h>
61 61 #include <inet/ip_if.h>
62 62 #include <inet/optcom.h>
63 63 #include <inet/ipsec_impl.h>
64 64 #include <inet/spdsock.h>
65 65 #include <inet/sadb.h>
66 66 #include <inet/iptun.h>
67 67 #include <inet/iptun/iptun_impl.h>
68 68
69 69 #include <sys/isa_defs.h>
70 70
71 71 #include <c2/audit.h>
72 72
73 73 /*
74 74 * This is a transport provider for the PF_POLICY IPsec policy
75 75 * management socket, which provides a management interface into the
76 76 * SPD, allowing policy rules to be added, deleted, and queried.
77 77 *
78 78 * This effectively replaces the old private SIOC*IPSECONFIG ioctls
79 79 * with an extensible interface which will hopefully be public some
80 80 * day.
81 81 *
82 82 * See <net/pfpolicy.h> for more details on the protocol.
83 83 *
84 84 * We link against drv/ip and call directly into it to manipulate the
85 85 * SPD; see ipsec_impl.h for the policy data structures and spd.c for
86 86 * the code which maintains them.
87 87 *
88 88 * The MT model of this is QPAIR with the addition of some explicit
89 89 * locking to protect system-wide policy data structures.
90 90 */
91 91
92 92 static vmem_t *spdsock_vmem; /* for minor numbers. */
93 93
94 94 #define ALIGNED64(x) IS_P2ALIGNED((x), sizeof (uint64_t))
95 95
96 96 /* Default structure copied into T_INFO_ACK messages (from rts.c...) */
97 97 static struct T_info_ack spdsock_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. spdsock allows maximum size messages. */
106 106 T_COTS, /* SERV_type. spdsock supports connection oriented. */
107 107 TS_UNBND, /* CURRENT_state. This is set from spdsock_state. */
108 108 (XPG4_1) /* Provider flags */
109 109 };
110 110
111 111 /* Named Dispatch Parameter Management Structure */
112 112 typedef struct spdsockparam_s {
113 113 uint_t spdsock_param_min;
114 114 uint_t spdsock_param_max;
115 115 uint_t spdsock_param_value;
116 116 char *spdsock_param_name;
117 117 } spdsockparam_t;
118 118
119 119 /*
120 120 * Table of NDD variables supported by spdsock. These are loaded into
121 121 * spdsock_g_nd in spdsock_init_nd.
122 122 * All of these are alterable, within the min/max values given, at run time.
123 123 */
124 124 static spdsockparam_t lcl_param_arr[] = {
125 125 /* min max value name */
126 126 { 4096, 65536, 8192, "spdsock_xmit_hiwat"},
127 127 { 0, 65536, 1024, "spdsock_xmit_lowat"},
128 128 { 4096, 65536, 8192, "spdsock_recv_hiwat"},
129 129 { 65536, 1024*1024*1024, 256*1024, "spdsock_max_buf"},
130 130 { 0, 3, 0, "spdsock_debug"},
131 131 };
132 132 #define spds_xmit_hiwat spds_params[0].spdsock_param_value
133 133 #define spds_xmit_lowat spds_params[1].spdsock_param_value
134 134 #define spds_recv_hiwat spds_params[2].spdsock_param_value
135 135 #define spds_max_buf spds_params[3].spdsock_param_value
136 136 #define spds_debug spds_params[4].spdsock_param_value
137 137
138 138 #define ss0dbg(a) printf a
139 139 /* NOTE: != 0 instead of > 0 so lint doesn't complain. */
140 140 #define ss1dbg(spds, a) if (spds->spds_debug != 0) printf a
141 141 #define ss2dbg(spds, a) if (spds->spds_debug > 1) printf a
142 142 #define ss3dbg(spds, a) if (spds->spds_debug > 2) printf a
143 143
144 144 #define RESET_SPDSOCK_DUMP_POLHEAD(ss, iph) { \
145 145 ASSERT(RW_READ_HELD(&(iph)->iph_lock)); \
146 146 (ss)->spdsock_dump_head = (iph); \
147 147 (ss)->spdsock_dump_gen = (iph)->iph_gen; \
148 148 (ss)->spdsock_dump_cur_type = 0; \
149 149 (ss)->spdsock_dump_cur_af = IPSEC_AF_V4; \
150 150 (ss)->spdsock_dump_cur_rule = NULL; \
151 151 (ss)->spdsock_dump_count = 0; \
152 152 (ss)->spdsock_dump_cur_chain = 0; \
153 153 }
154 154
155 155 static int spdsock_close(queue_t *, int, cred_t *);
156 156 static int spdsock_open(queue_t *, dev_t *, int, int, cred_t *);
157 157 static int spdsock_wput(queue_t *, mblk_t *);
158 158 static int spdsock_wsrv(queue_t *);
159 159 static int spdsock_rsrv(queue_t *);
160 160 static void *spdsock_stack_init(netstackid_t stackid, netstack_t *ns);
161 161 static void spdsock_stack_shutdown(netstackid_t stackid, void *arg);
162 162 static void spdsock_stack_fini(netstackid_t stackid, void *arg);
163 163 static void spdsock_loadcheck(void *);
164 164 static void spdsock_merge_algs(spd_stack_t *);
165 165 static void spdsock_flush_one(ipsec_policy_head_t *, netstack_t *);
166 166 static mblk_t *spdsock_dump_next_record(spdsock_t *);
167 167 static void update_iptun_policy(ipsec_tun_pol_t *);
168 168
169 169 static struct module_info info = {
170 170 5138, "spdsock", 1, INFPSZ, 512, 128
171 171 };
172 172
173 173 static struct qinit rinit = {
174 174 NULL, spdsock_rsrv, spdsock_open, spdsock_close,
175 175 NULL, &info
176 176 };
177 177
178 178 static struct qinit winit = {
179 179 spdsock_wput, spdsock_wsrv, NULL, NULL, NULL, &info
180 180 };
181 181
182 182 struct streamtab spdsockinfo = {
183 183 &rinit, &winit
184 184 };
185 185
186 186 /* mapping from alg type to protocol number, as per RFC 2407 */
187 187 static const uint_t algproto[] = {
188 188 PROTO_IPSEC_AH,
189 189 PROTO_IPSEC_ESP,
190 190 };
191 191
192 192 #define NALGPROTOS (sizeof (algproto) / sizeof (algproto[0]))
193 193
194 194 /* mapping from kernel exec mode to spdsock exec mode */
195 195 static const uint_t execmodes[] = {
196 196 SPD_ALG_EXEC_MODE_SYNC,
197 197 SPD_ALG_EXEC_MODE_ASYNC
198 198 };
199 199
200 200 #define NEXECMODES (sizeof (execmodes) / sizeof (execmodes[0]))
201 201
202 202 #define ALL_ACTIVE_POLHEADS ((ipsec_policy_head_t *)-1)
203 203 #define ALL_INACTIVE_POLHEADS ((ipsec_policy_head_t *)-2)
204 204
205 205 #define ITP_NAME(itp) (itp != NULL ? itp->itp_name : NULL)
206 206
207 207 /* ARGSUSED */
208 208 static int
209 209 spdsock_param_get(
210 210 queue_t *q,
211 211 mblk_t *mp,
212 212 caddr_t cp,
213 213 cred_t *cr)
214 214 {
215 215 spdsockparam_t *spdsockpa = (spdsockparam_t *)cp;
216 216 uint_t value;
217 217 spdsock_t *ss = (spdsock_t *)q->q_ptr;
218 218 spd_stack_t *spds = ss->spdsock_spds;
219 219
220 220 mutex_enter(&spds->spds_param_lock);
221 221 value = spdsockpa->spdsock_param_value;
222 222 mutex_exit(&spds->spds_param_lock);
223 223
224 224 (void) mi_mpprintf(mp, "%u", value);
225 225 return (0);
226 226 }
227 227
228 228 /* This routine sets an NDD variable in a spdsockparam_t structure. */
229 229 /* ARGSUSED */
230 230 static int
231 231 spdsock_param_set(
232 232 queue_t *q,
233 233 mblk_t *mp,
234 234 char *value,
235 235 caddr_t cp,
236 236 cred_t *cr)
237 237 {
238 238 ulong_t new_value;
239 239 spdsockparam_t *spdsockpa = (spdsockparam_t *)cp;
240 240 spdsock_t *ss = (spdsock_t *)q->q_ptr;
241 241 spd_stack_t *spds = ss->spdsock_spds;
242 242
243 243 /* Convert the value from a string into a long integer. */
244 244 if (ddi_strtoul(value, NULL, 10, &new_value) != 0)
245 245 return (EINVAL);
246 246
247 247 mutex_enter(&spds->spds_param_lock);
248 248 /*
249 249 * Fail the request if the new value does not lie within the
250 250 * required bounds.
251 251 */
252 252 if (new_value < spdsockpa->spdsock_param_min ||
253 253 new_value > spdsockpa->spdsock_param_max) {
254 254 mutex_exit(&spds->spds_param_lock);
255 255 return (EINVAL);
256 256 }
257 257
258 258 /* Set the new value */
259 259 spdsockpa->spdsock_param_value = new_value;
260 260 mutex_exit(&spds->spds_param_lock);
261 261
262 262 return (0);
263 263 }
264 264
265 265 /*
266 266 * Initialize at module load time
267 267 */
268 268 boolean_t
269 269 spdsock_ddi_init(void)
270 270 {
271 271 spdsock_max_optsize = optcom_max_optsize(
272 272 spdsock_opt_obj.odb_opt_des_arr, spdsock_opt_obj.odb_opt_arr_cnt);
273 273
274 274 spdsock_vmem = vmem_create("spdsock", (void *)1, MAXMIN, 1,
275 275 NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER);
276 276
277 277 /*
278 278 * We want to be informed each time a stack is created or
279 279 * destroyed in the kernel, so we can maintain the
280 280 * set of spd_stack_t's.
281 281 */
282 282 netstack_register(NS_SPDSOCK, spdsock_stack_init,
283 283 spdsock_stack_shutdown, spdsock_stack_fini);
284 284
285 285 return (B_TRUE);
286 286 }
287 287
288 288 /*
289 289 * Walk through the param array specified registering each element with the
290 290 * named dispatch handler.
291 291 */
292 292 static boolean_t
293 293 spdsock_param_register(IDP *ndp, spdsockparam_t *ssp, int cnt)
294 294 {
295 295 for (; cnt-- > 0; ssp++) {
296 296 if (ssp->spdsock_param_name != NULL &&
297 297 ssp->spdsock_param_name[0]) {
298 298 if (!nd_load(ndp,
299 299 ssp->spdsock_param_name,
300 300 spdsock_param_get, spdsock_param_set,
301 301 (caddr_t)ssp)) {
302 302 nd_free(ndp);
303 303 return (B_FALSE);
304 304 }
305 305 }
306 306 }
307 307 return (B_TRUE);
308 308 }
309 309
310 310 /*
311 311 * Initialize for each stack instance
312 312 */
313 313 /* ARGSUSED */
314 314 static void *
315 315 spdsock_stack_init(netstackid_t stackid, netstack_t *ns)
316 316 {
317 317 spd_stack_t *spds;
318 318 spdsockparam_t *ssp;
319 319
320 320 spds = (spd_stack_t *)kmem_zalloc(sizeof (*spds), KM_SLEEP);
321 321 spds->spds_netstack = ns;
322 322
323 323 ASSERT(spds->spds_g_nd == NULL);
324 324
325 325 ssp = (spdsockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
326 326 spds->spds_params = ssp;
327 327 bcopy(lcl_param_arr, ssp, sizeof (lcl_param_arr));
328 328
329 329 (void) spdsock_param_register(&spds->spds_g_nd, ssp,
330 330 A_CNT(lcl_param_arr));
331 331
332 332 mutex_init(&spds->spds_param_lock, NULL, MUTEX_DEFAULT, NULL);
333 333 mutex_init(&spds->spds_alg_lock, NULL, MUTEX_DEFAULT, NULL);
334 334
335 335 return (spds);
336 336 }
337 337
338 338 void
339 339 spdsock_ddi_destroy(void)
340 340 {
341 341 vmem_destroy(spdsock_vmem);
342 342
343 343 netstack_unregister(NS_SPDSOCK);
344 344 }
345 345
346 346 /*
347 347 * Do pre-removal cleanup.
348 348 */
349 349 /* ARGSUSED */
350 350 static void
351 351 spdsock_stack_shutdown(netstackid_t stackid, void *arg)
352 352 {
353 353 spd_stack_t *spds = (spd_stack_t *)arg;
354 354
355 355 if (spds->spds_mp_algs != NULL) {
356 356 freemsg(spds->spds_mp_algs);
357 357 spds->spds_mp_algs = NULL;
358 358 }
359 359 }
360 360
361 361 /* ARGSUSED */
362 362 static void
363 363 spdsock_stack_fini(netstackid_t stackid, void *arg)
364 364 {
365 365 spd_stack_t *spds = (spd_stack_t *)arg;
366 366
367 367 ASSERT(spds->spds_mp_algs == NULL);
368 368 mutex_destroy(&spds->spds_param_lock);
369 369 mutex_destroy(&spds->spds_alg_lock);
370 370 nd_free(&spds->spds_g_nd);
371 371 kmem_free(spds->spds_params, sizeof (lcl_param_arr));
372 372 spds->spds_params = NULL;
373 373
374 374 kmem_free(spds, sizeof (*spds));
375 375 }
376 376
377 377 /*
378 378 * NOTE: large quantities of this should be shared with keysock.
379 379 * Would be nice to combine some of this into a common module, but
380 380 * not possible given time pressures.
381 381 */
382 382
383 383 /*
384 384 * High-level reality checking of extensions.
385 385 */
386 386 /* ARGSUSED */ /* XXX */
387 387 static boolean_t
388 388 ext_check(spd_ext_t *ext)
389 389 {
390 390 spd_if_t *tunname = (spd_if_t *)ext;
391 391 int i;
392 392 char *idstr;
393 393
394 394 if (ext->spd_ext_type == SPD_EXT_TUN_NAME) {
395 395 /* (NOTE: Modified from SADB_EXT_IDENTITY..) */
396 396
397 397 /*
398 398 * Make sure the strings in these identities are
399 399 * null-terminated. Let's "proactively" null-terminate the
400 400 * string at the last byte if it's not terminated sooner.
401 401 */
402 402 i = SPD_64TO8(tunname->spd_if_len) - sizeof (spd_if_t);
403 403 idstr = (char *)(tunname + 1);
404 404 while (*idstr != '\0' && i > 0) {
405 405 i--;
406 406 idstr++;
407 407 }
408 408 if (i == 0) {
409 409 /*
410 410 * I.e., if the bozo user didn't NULL-terminate the
411 411 * string...
412 412 */
413 413 idstr--;
414 414 *idstr = '\0';
415 415 }
416 416 }
417 417 return (B_TRUE); /* For now... */
418 418 }
419 419
420 420
421 421
422 422 /* Return values for spdsock_get_ext(). */
423 423 #define KGE_OK 0
424 424 #define KGE_DUP 1
425 425 #define KGE_UNK 2
426 426 #define KGE_LEN 3
427 427 #define KGE_CHK 4
428 428
429 429 /*
430 430 * Parse basic extension headers and return in the passed-in pointer vector.
431 431 * Return values include:
432 432 *
433 433 * KGE_OK Everything's nice and parsed out.
434 434 * If there are no extensions, place NULL in extv[0].
435 435 * KGE_DUP There is a duplicate extension.
436 436 * First instance in appropriate bin. First duplicate in
437 437 * extv[0].
438 438 * KGE_UNK Unknown extension type encountered. extv[0] contains
439 439 * unknown header.
440 440 * KGE_LEN Extension length error.
441 441 * KGE_CHK High-level reality check failed on specific extension.
442 442 *
443 443 * My apologies for some of the pointer arithmetic in here. I'm thinking
444 444 * like an assembly programmer, yet trying to make the compiler happy.
445 445 */
446 446 static int
447 447 spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize)
448 448 {
449 449 bzero(extv, sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1));
450 450
451 451 /* Use extv[0] as the "current working pointer". */
452 452
453 453 extv[0] = (spd_ext_t *)(basehdr + 1);
454 454
455 455 while (extv[0] < (spd_ext_t *)(((uint8_t *)basehdr) + msgsize)) {
456 456 /* Check for unknown headers. */
457 457 if (extv[0]->spd_ext_type == 0 ||
458 458 extv[0]->spd_ext_type > SPD_EXT_MAX)
459 459 return (KGE_UNK);
460 460
461 461 /*
462 462 * Check length. Use uint64_t because extlen is in units
463 463 * of 64-bit words. If length goes beyond the msgsize,
464 464 * return an error. (Zero length also qualifies here.)
465 465 */
466 466 if (extv[0]->spd_ext_len == 0 ||
467 467 (void *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
468 468 (void *)((uint8_t *)basehdr + msgsize))
469 469 return (KGE_LEN);
470 470
471 471 /* Check for redundant headers. */
472 472 if (extv[extv[0]->spd_ext_type] != NULL)
473 473 return (KGE_DUP);
474 474
475 475 /*
476 476 * Reality check the extension if possible at the spdsock
477 477 * level.
478 478 */
479 479 if (!ext_check(extv[0]))
480 480 return (KGE_CHK);
481 481
482 482 /* If I make it here, assign the appropriate bin. */
483 483 extv[extv[0]->spd_ext_type] = extv[0];
484 484
485 485 /* Advance pointer (See above for uint64_t ptr reasoning.) */
486 486 extv[0] = (spd_ext_t *)
487 487 ((uint64_t *)extv[0] + extv[0]->spd_ext_len);
488 488 }
489 489
490 490 /* Everything's cool. */
491 491
492 492 /*
493 493 * If extv[0] == NULL, then there are no extension headers in this
494 494 * message. Ensure that this is the case.
495 495 */
496 496 if (extv[0] == (spd_ext_t *)(basehdr + 1))
497 497 extv[0] = NULL;
498 498
499 499 return (KGE_OK);
500 500 }
501 501
502 502 static const int bad_ext_diag[] = {
503 503 SPD_DIAGNOSTIC_MALFORMED_LCLPORT,
504 504 SPD_DIAGNOSTIC_MALFORMED_REMPORT,
505 505 SPD_DIAGNOSTIC_MALFORMED_PROTO,
506 506 SPD_DIAGNOSTIC_MALFORMED_LCLADDR,
507 507 SPD_DIAGNOSTIC_MALFORMED_REMADDR,
508 508 SPD_DIAGNOSTIC_MALFORMED_ACTION,
509 509 SPD_DIAGNOSTIC_MALFORMED_RULE,
510 510 SPD_DIAGNOSTIC_MALFORMED_RULESET,
511 511 SPD_DIAGNOSTIC_MALFORMED_ICMP_TYPECODE
512 512 };
513 513
514 514 static const int dup_ext_diag[] = {
515 515 SPD_DIAGNOSTIC_DUPLICATE_LCLPORT,
516 516 SPD_DIAGNOSTIC_DUPLICATE_REMPORT,
517 517 SPD_DIAGNOSTIC_DUPLICATE_PROTO,
518 518 SPD_DIAGNOSTIC_DUPLICATE_LCLADDR,
519 519 SPD_DIAGNOSTIC_DUPLICATE_REMADDR,
520 520 SPD_DIAGNOSTIC_DUPLICATE_ACTION,
521 521 SPD_DIAGNOSTIC_DUPLICATE_RULE,
522 522 SPD_DIAGNOSTIC_DUPLICATE_RULESET,
523 523 SPD_DIAGNOSTIC_DUPLICATE_ICMP_TYPECODE
524 524 };
525 525
526 526 /*
527 527 * Transmit a PF_POLICY error message to the instance either pointed to
528 528 * by ks, the instance with serial number serial, or more, depending.
529 529 *
530 530 * The faulty message (or a reasonable facsimile thereof) is in mp.
531 531 * This function will free mp or recycle it for delivery, thereby causing
532 532 * the stream head to free it.
533 533 */
534 534 static void
535 535 spdsock_error(queue_t *q, mblk_t *mp, int error, int diagnostic)
536 536 {
537 537 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
538 538
539 539 ASSERT(mp->b_datap->db_type == M_DATA);
540 540
541 541 if (spmsg->spd_msg_type < SPD_MIN ||
542 542 spmsg->spd_msg_type > SPD_MAX)
543 543 spmsg->spd_msg_type = SPD_RESERVED;
544 544
545 545 /*
546 546 * Strip out extension headers.
547 547 */
548 548 ASSERT(mp->b_rptr + sizeof (*spmsg) <= mp->b_datap->db_lim);
549 549 mp->b_wptr = mp->b_rptr + sizeof (*spmsg);
550 550 spmsg->spd_msg_len = SPD_8TO64(sizeof (spd_msg_t));
551 551 spmsg->spd_msg_errno = (uint8_t)error;
552 552 spmsg->spd_msg_diagnostic = (uint16_t)diagnostic;
553 553
554 554 qreply(q, mp);
555 555 }
556 556
557 557 static void
558 558 spdsock_diag(queue_t *q, mblk_t *mp, int diagnostic)
559 559 {
560 560 spdsock_error(q, mp, EINVAL, diagnostic);
561 561 }
562 562
563 563 static void
564 564 spd_echo(queue_t *q, mblk_t *mp)
565 565 {
566 566 qreply(q, mp);
567 567 }
568 568
569 569 /*
570 570 * Do NOT consume a reference to itp.
571 571 */
572 572 /*ARGSUSED*/
573 573 static void
574 574 spdsock_flush_node(ipsec_tun_pol_t *itp, void *cookie, netstack_t *ns)
575 575 {
576 576 boolean_t active = (boolean_t)cookie;
577 577 ipsec_policy_head_t *iph;
578 578
579 579 iph = active ? itp->itp_policy : itp->itp_inactive;
580 580 IPPH_REFHOLD(iph);
581 581 mutex_enter(&itp->itp_lock);
582 582 spdsock_flush_one(iph, ns); /* Releases iph refhold. */
583 583 if (active)
584 584 itp->itp_flags &= ~ITPF_PFLAGS;
585 585 else
586 586 itp->itp_flags &= ~ITPF_IFLAGS;
587 587 mutex_exit(&itp->itp_lock);
588 588 /* SPD_FLUSH is worth a tunnel MTU check. */
589 589 update_iptun_policy(itp);
590 590 }
591 591
592 592 /*
593 593 * Clear out one polhead.
594 594 */
595 595 static void
596 596 spdsock_flush_one(ipsec_policy_head_t *iph, netstack_t *ns)
597 597 {
598 598 rw_enter(&iph->iph_lock, RW_WRITER);
599 599 ipsec_polhead_flush(iph, ns);
600 600 rw_exit(&iph->iph_lock);
601 601 IPPH_REFRELE(iph, ns);
602 602 }
603 603
604 604 static void
605 605 spdsock_flush(queue_t *q, ipsec_policy_head_t *iph, ipsec_tun_pol_t *itp,
606 606 mblk_t *mp)
607 607 {
608 608 boolean_t active;
609 609 spdsock_t *ss = (spdsock_t *)q->q_ptr;
610 610 netstack_t *ns = ss->spdsock_spds->spds_netstack;
611 611 uint32_t auditing = AU_AUDITING();
612 612
613 613 if (iph != ALL_ACTIVE_POLHEADS && iph != ALL_INACTIVE_POLHEADS) {
614 614 spdsock_flush_one(iph, ns);
615 615 if (auditing) {
616 616 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
617 617 cred_t *cr;
618 618 pid_t cpid;
619 619
620 620 cr = msg_getcred(mp, &cpid);
621 621 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
622 622 audit_pf_policy(SPD_FLUSH, cr, ns,
623 623 ITP_NAME(itp), active, 0, cpid);
624 624 }
625 625 } else {
626 626 active = (iph == ALL_ACTIVE_POLHEADS);
627 627
628 628 /* First flush the global policy. */
629 629 spdsock_flush_one(active ? ipsec_system_policy(ns) :
630 630 ipsec_inactive_policy(ns), ns);
631 631 if (auditing) {
632 632 cred_t *cr;
633 633 pid_t cpid;
634 634
635 635 cr = msg_getcred(mp, &cpid);
636 636 audit_pf_policy(SPD_FLUSH, cr, ns, NULL,
637 637 active, 0, cpid);
638 638 }
639 639 /* Then flush every tunnel's appropriate one. */
640 640 itp_walk(spdsock_flush_node, (void *)active, ns);
641 641 if (auditing) {
642 642 cred_t *cr;
643 643 pid_t cpid;
644 644
645 645 cr = msg_getcred(mp, &cpid);
646 646 audit_pf_policy(SPD_FLUSH, cr, ns,
647 647 "all tunnels", active, 0, cpid);
648 648 }
649 649 }
650 650
651 651 spd_echo(q, mp);
652 652 }
653 653
654 654 static boolean_t
655 655 spdsock_ext_to_sel(spd_ext_t **extv, ipsec_selkey_t *sel, int *diag)
656 656 {
657 657 bzero(sel, sizeof (*sel));
658 658
659 659 if (extv[SPD_EXT_PROTO] != NULL) {
660 660 struct spd_proto *pr =
661 661 (struct spd_proto *)extv[SPD_EXT_PROTO];
662 662 sel->ipsl_proto = pr->spd_proto_number;
663 663 sel->ipsl_valid |= IPSL_PROTOCOL;
664 664 }
665 665 if (extv[SPD_EXT_LCLPORT] != NULL) {
666 666 struct spd_portrange *pr =
667 667 (struct spd_portrange *)extv[SPD_EXT_LCLPORT];
668 668 sel->ipsl_lport = pr->spd_ports_minport;
669 669 sel->ipsl_valid |= IPSL_LOCAL_PORT;
670 670 }
671 671 if (extv[SPD_EXT_REMPORT] != NULL) {
672 672 struct spd_portrange *pr =
673 673 (struct spd_portrange *)extv[SPD_EXT_REMPORT];
674 674 sel->ipsl_rport = pr->spd_ports_minport;
675 675 sel->ipsl_valid |= IPSL_REMOTE_PORT;
676 676 }
677 677
678 678 if (extv[SPD_EXT_ICMP_TYPECODE] != NULL) {
679 679 struct spd_typecode *tc=
680 680 (struct spd_typecode *)extv[SPD_EXT_ICMP_TYPECODE];
681 681
682 682 sel->ipsl_valid |= IPSL_ICMP_TYPE;
683 683 sel->ipsl_icmp_type = tc->spd_typecode_type;
684 684 if (tc->spd_typecode_type_end < tc->spd_typecode_type)
685 685 sel->ipsl_icmp_type_end = tc->spd_typecode_type;
686 686 else
687 687 sel->ipsl_icmp_type_end = tc->spd_typecode_type_end;
688 688
689 689 if (tc->spd_typecode_code != 255) {
690 690 sel->ipsl_valid |= IPSL_ICMP_CODE;
691 691 sel->ipsl_icmp_code = tc->spd_typecode_code;
692 692 if (tc->spd_typecode_code_end < tc->spd_typecode_code)
693 693 sel->ipsl_icmp_code_end = tc->spd_typecode_code;
694 694 else
695 695 sel->ipsl_icmp_code_end =
696 696 tc->spd_typecode_code_end;
697 697 }
698 698 }
699 699 #define ADDR2SEL(sel, extv, field, pfield, extn, bit) \
700 700 if ((extv)[(extn)] != NULL) { \
701 701 uint_t addrlen; \
702 702 struct spd_address *ap = \
703 703 (struct spd_address *)((extv)[(extn)]); \
704 704 addrlen = (ap->spd_address_af == AF_INET6) ? \
705 705 IPV6_ADDR_LEN : IP_ADDR_LEN; \
706 706 if (SPD_64TO8(ap->spd_address_len) < \
707 707 (addrlen + sizeof (*ap))) { \
708 708 *diag = SPD_DIAGNOSTIC_BAD_ADDR_LEN; \
709 709 return (B_FALSE); \
710 710 } \
711 711 bcopy((ap+1), &((sel)->field), addrlen); \
712 712 (sel)->pfield = ap->spd_address_prefixlen; \
713 713 (sel)->ipsl_valid |= (bit); \
714 714 (sel)->ipsl_valid |= (ap->spd_address_af == AF_INET6) ? \
715 715 IPSL_IPV6 : IPSL_IPV4; \
716 716 }
717 717
718 718 ADDR2SEL(sel, extv, ipsl_local, ipsl_local_pfxlen,
719 719 SPD_EXT_LCLADDR, IPSL_LOCAL_ADDR);
720 720 ADDR2SEL(sel, extv, ipsl_remote, ipsl_remote_pfxlen,
721 721 SPD_EXT_REMADDR, IPSL_REMOTE_ADDR);
722 722
723 723 if ((sel->ipsl_valid & (IPSL_IPV6|IPSL_IPV4)) ==
724 724 (IPSL_IPV6|IPSL_IPV4)) {
725 725 *diag = SPD_DIAGNOSTIC_MIXED_AF;
726 726 return (B_FALSE);
727 727 }
728 728
729 729 #undef ADDR2SEL
730 730
731 731 return (B_TRUE);
732 732 }
733 733
734 734 static boolean_t
735 735 spd_convert_type(uint32_t type, ipsec_act_t *act)
736 736 {
737 737 switch (type) {
738 738 case SPD_ACTTYPE_DROP:
739 739 act->ipa_type = IPSEC_ACT_DISCARD;
740 740 return (B_TRUE);
741 741
742 742 case SPD_ACTTYPE_PASS:
743 743 act->ipa_type = IPSEC_ACT_CLEAR;
744 744 return (B_TRUE);
745 745
746 746 case SPD_ACTTYPE_IPSEC:
747 747 act->ipa_type = IPSEC_ACT_APPLY;
748 748 return (B_TRUE);
749 749 }
750 750 return (B_FALSE);
751 751 }
752 752
753 753 static boolean_t
754 754 spd_convert_flags(uint32_t flags, ipsec_act_t *act)
755 755 {
756 756 /*
757 757 * Note use of !! for boolean canonicalization.
758 758 */
759 759 act->ipa_apply.ipp_use_ah = !!(flags & SPD_APPLY_AH);
760 760 act->ipa_apply.ipp_use_esp = !!(flags & SPD_APPLY_ESP);
761 761 act->ipa_apply.ipp_use_espa = !!(flags & SPD_APPLY_ESPA);
762 762 act->ipa_apply.ipp_use_se = !!(flags & SPD_APPLY_SE);
763 763 act->ipa_apply.ipp_use_unique = !!(flags & SPD_APPLY_UNIQUE);
764 764 return (B_TRUE);
765 765 }
766 766
767 767 static void
768 768 spdsock_reset_act(ipsec_act_t *act)
769 769 {
770 770 bzero(act, sizeof (*act));
771 771 act->ipa_apply.ipp_espe_maxbits = IPSEC_MAX_KEYBITS;
772 772 act->ipa_apply.ipp_espa_maxbits = IPSEC_MAX_KEYBITS;
773 773 act->ipa_apply.ipp_ah_maxbits = IPSEC_MAX_KEYBITS;
774 774 }
775 775
776 776 /*
777 777 * Sanity check action against reality, and shrink-wrap key sizes..
778 778 */
779 779 static boolean_t
780 780 spdsock_check_action(ipsec_act_t *act, boolean_t tunnel_polhead, int *diag,
781 781 spd_stack_t *spds)
782 782 {
783 783 if (tunnel_polhead && act->ipa_apply.ipp_use_unique) {
784 784 *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
785 785 return (B_FALSE);
786 786 }
787 787 if ((act->ipa_type != IPSEC_ACT_APPLY) &&
788 788 (act->ipa_apply.ipp_use_ah ||
789 789 act->ipa_apply.ipp_use_esp ||
790 790 act->ipa_apply.ipp_use_espa ||
791 791 act->ipa_apply.ipp_use_se ||
792 792 act->ipa_apply.ipp_use_unique)) {
793 793 *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
794 794 return (B_FALSE);
795 795 }
796 796 if ((act->ipa_type == IPSEC_ACT_APPLY) &&
797 797 !act->ipa_apply.ipp_use_ah &&
798 798 !act->ipa_apply.ipp_use_esp) {
799 799 *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
800 800 return (B_FALSE);
801 801 }
802 802 return (ipsec_check_action(act, diag, spds->spds_netstack));
803 803 }
804 804
805 805 /*
806 806 * We may be short a few error checks here..
807 807 */
808 808 static boolean_t
809 809 spdsock_ext_to_actvec(spd_ext_t **extv, ipsec_act_t **actpp, uint_t *nactp,
810 810 int *diag, spd_stack_t *spds)
811 811 {
812 812 struct spd_ext_actions *sactp =
813 813 (struct spd_ext_actions *)extv[SPD_EXT_ACTION];
814 814 ipsec_act_t act, *actp, *endactp;
815 815 struct spd_attribute *attrp, *endattrp;
816 816 uint64_t *endp;
817 817 int nact;
818 818 boolean_t tunnel_polhead;
819 819
820 820 tunnel_polhead = (extv[SPD_EXT_TUN_NAME] != NULL &&
821 821 (((struct spd_rule *)extv[SPD_EXT_RULE])->spd_rule_flags &
822 822 SPD_RULE_FLAG_TUNNEL));
823 823
824 824 *actpp = NULL;
825 825 *nactp = 0;
826 826
827 827 if (sactp == NULL) {
828 828 *diag = SPD_DIAGNOSTIC_NO_ACTION_EXT;
829 829 return (B_FALSE);
830 830 }
831 831
832 832 /*
833 833 * Parse the "action" extension and convert into an action chain.
834 834 */
835 835
836 836 nact = sactp->spd_actions_count;
837 837
838 838 endp = (uint64_t *)sactp;
839 839 endp += sactp->spd_actions_len;
840 840 endattrp = (struct spd_attribute *)endp;
841 841
842 842 actp = kmem_alloc(sizeof (*actp) * nact, KM_NOSLEEP);
843 843 if (actp == NULL) {
844 844 *diag = SPD_DIAGNOSTIC_ADD_NO_MEM;
845 845 return (B_FALSE);
846 846 }
847 847 *actpp = actp;
848 848 *nactp = nact;
849 849 endactp = actp + nact;
850 850
851 851 spdsock_reset_act(&act);
852 852 attrp = (struct spd_attribute *)(&sactp[1]);
853 853
854 854 for (; attrp < endattrp; attrp++) {
855 855 switch (attrp->spd_attr_tag) {
856 856 case SPD_ATTR_NOP:
857 857 break;
858 858
859 859 case SPD_ATTR_EMPTY:
860 860 spdsock_reset_act(&act);
861 861 break;
862 862
863 863 case SPD_ATTR_END:
864 864 attrp = endattrp;
865 865 /* FALLTHRU */
866 866 case SPD_ATTR_NEXT:
867 867 if (actp >= endactp) {
868 868 *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
869 869 goto fail;
870 870 }
871 871 if (!spdsock_check_action(&act, tunnel_polhead,
872 872 diag, spds))
873 873 goto fail;
874 874 *actp++ = act;
875 875 spdsock_reset_act(&act);
876 876 break;
877 877
878 878 case SPD_ATTR_TYPE:
879 879 if (!spd_convert_type(attrp->spd_attr_value, &act)) {
880 880 *diag = SPD_DIAGNOSTIC_ADD_BAD_TYPE;
881 881 goto fail;
882 882 }
883 883 break;
884 884
885 885 case SPD_ATTR_FLAGS:
886 886 if (!tunnel_polhead && extv[SPD_EXT_TUN_NAME] != NULL) {
887 887 /*
888 888 * Set "sa unique" for transport-mode
889 889 * tunnels whether we want to or not.
890 890 */
891 891 attrp->spd_attr_value |= SPD_APPLY_UNIQUE;
892 892 }
893 893 if (!spd_convert_flags(attrp->spd_attr_value, &act)) {
894 894 *diag = SPD_DIAGNOSTIC_ADD_BAD_FLAGS;
895 895 goto fail;
896 896 }
897 897 break;
898 898
899 899 case SPD_ATTR_AH_AUTH:
900 900 if (attrp->spd_attr_value == 0) {
901 901 *diag = SPD_DIAGNOSTIC_UNSUPP_AH_ALG;
902 902 goto fail;
903 903 }
904 904 act.ipa_apply.ipp_auth_alg = attrp->spd_attr_value;
905 905 break;
906 906
907 907 case SPD_ATTR_ESP_ENCR:
908 908 if (attrp->spd_attr_value == 0) {
909 909 *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG;
910 910 goto fail;
911 911 }
912 912 act.ipa_apply.ipp_encr_alg = attrp->spd_attr_value;
913 913 break;
914 914
915 915 case SPD_ATTR_ESP_AUTH:
916 916 if (attrp->spd_attr_value == 0) {
917 917 *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG;
918 918 goto fail;
919 919 }
920 920 act.ipa_apply.ipp_esp_auth_alg = attrp->spd_attr_value;
921 921 break;
922 922
923 923 case SPD_ATTR_ENCR_MINBITS:
924 924 act.ipa_apply.ipp_espe_minbits = attrp->spd_attr_value;
925 925 break;
926 926
927 927 case SPD_ATTR_ENCR_MAXBITS:
928 928 act.ipa_apply.ipp_espe_maxbits = attrp->spd_attr_value;
929 929 break;
930 930
931 931 case SPD_ATTR_AH_MINBITS:
932 932 act.ipa_apply.ipp_ah_minbits = attrp->spd_attr_value;
933 933 break;
934 934
935 935 case SPD_ATTR_AH_MAXBITS:
936 936 act.ipa_apply.ipp_ah_maxbits = attrp->spd_attr_value;
937 937 break;
938 938
939 939 case SPD_ATTR_ESPA_MINBITS:
940 940 act.ipa_apply.ipp_espa_minbits = attrp->spd_attr_value;
941 941 break;
942 942
943 943 case SPD_ATTR_ESPA_MAXBITS:
944 944 act.ipa_apply.ipp_espa_maxbits = attrp->spd_attr_value;
945 945 break;
946 946
947 947 case SPD_ATTR_LIFE_SOFT_TIME:
948 948 case SPD_ATTR_LIFE_HARD_TIME:
949 949 case SPD_ATTR_LIFE_SOFT_BYTES:
950 950 case SPD_ATTR_LIFE_HARD_BYTES:
951 951 break;
952 952
953 953 case SPD_ATTR_KM_PROTO:
954 954 act.ipa_apply.ipp_km_proto = attrp->spd_attr_value;
955 955 break;
956 956
957 957 case SPD_ATTR_KM_COOKIE:
958 958 act.ipa_apply.ipp_km_cookie = attrp->spd_attr_value;
959 959 break;
960 960
961 961 case SPD_ATTR_REPLAY_DEPTH:
962 962 act.ipa_apply.ipp_replay_depth = attrp->spd_attr_value;
963 963 break;
964 964 }
965 965 }
966 966 if (actp != endactp) {
967 967 *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
968 968 goto fail;
969 969 }
970 970
971 971 return (B_TRUE);
972 972 fail:
973 973 ipsec_actvec_free(*actpp, nact);
974 974 *actpp = NULL;
975 975 return (B_FALSE);
976 976 }
977 977
978 978 typedef struct
979 979 {
980 980 ipsec_policy_t *pol;
981 981 int dir;
982 982 } tmprule_t;
983 983
984 984 static int
985 985 mkrule(ipsec_policy_head_t *iph, struct spd_rule *rule,
986 986 ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t af,
987 987 tmprule_t **rp, uint64_t *index, spd_stack_t *spds)
988 988 {
989 989 ipsec_policy_t *pol;
990 990
991 991 sel->ipsl_valid &= ~(IPSL_IPV6|IPSL_IPV4);
992 992 sel->ipsl_valid |= af;
993 993
994 994 pol = ipsec_policy_create(sel, actp, nact, rule->spd_rule_priority,
995 995 index, spds->spds_netstack);
996 996 if (pol == NULL)
997 997 return (ENOMEM);
998 998
999 999 (*rp)->pol = pol;
1000 1000 (*rp)->dir = dir;
1001 1001 (*rp)++;
1002 1002
1003 1003 if (!ipsec_check_policy(iph, pol, dir))
1004 1004 return (EEXIST);
1005 1005
1006 1006 rule->spd_rule_index = pol->ipsp_index;
1007 1007 return (0);
1008 1008 }
1009 1009
1010 1010 static int
1011 1011 mkrulepair(ipsec_policy_head_t *iph, struct spd_rule *rule,
1012 1012 ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t afs,
1013 1013 tmprule_t **rp, uint64_t *index, spd_stack_t *spds)
1014 1014 {
1015 1015 int error;
1016 1016
1017 1017 if (afs & IPSL_IPV4) {
1018 1018 error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV4, rp,
1019 1019 index, spds);
1020 1020 if (error != 0)
1021 1021 return (error);
1022 1022 }
1023 1023 if (afs & IPSL_IPV6) {
1024 1024 error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV6, rp,
1025 1025 index, spds);
1026 1026 if (error != 0)
1027 1027 return (error);
1028 1028 }
1029 1029 return (0);
1030 1030 }
1031 1031
1032 1032
1033 1033 static void
1034 1034 spdsock_addrule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
1035 1035 spd_ext_t **extv, ipsec_tun_pol_t *itp)
1036 1036 {
1037 1037 ipsec_selkey_t sel;
1038 1038 ipsec_act_t *actp;
1039 1039 uint_t nact;
1040 1040 int diag = 0, error, afs;
1041 1041 struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE];
1042 1042 tmprule_t rules[4], *rulep = &rules[0];
1043 1043 boolean_t tunnel_mode, empty_itp, active;
1044 1044 uint64_t *index = (itp == NULL) ? NULL : &itp->itp_next_policy_index;
1045 1045 spdsock_t *ss = (spdsock_t *)q->q_ptr;
1046 1046 spd_stack_t *spds = ss->spdsock_spds;
1047 1047 uint32_t auditing = AU_AUDITING();
1048 1048
1049 1049 if (rule == NULL) {
1050 1050 spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT);
1051 1051 if (auditing) {
1052 1052 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1053 1053 cred_t *cr;
1054 1054 pid_t cpid;
1055 1055
1056 1056 cr = msg_getcred(mp, &cpid);
1057 1057 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1058 1058 audit_pf_policy(SPD_ADDRULE, cr,
1059 1059 spds->spds_netstack, ITP_NAME(itp), active,
1060 1060 SPD_DIAGNOSTIC_NO_RULE_EXT, cpid);
1061 1061 }
1062 1062 return;
1063 1063 }
1064 1064
1065 1065 tunnel_mode = (rule->spd_rule_flags & SPD_RULE_FLAG_TUNNEL);
1066 1066
1067 1067 if (itp != NULL) {
1068 1068 mutex_enter(&itp->itp_lock);
1069 1069 ASSERT(itp->itp_policy == iph || itp->itp_inactive == iph);
1070 1070 active = (itp->itp_policy == iph);
1071 1071 if (ITP_P_ISACTIVE(itp, iph)) {
1072 1072 /* Check for mix-and-match of tunnel/transport. */
1073 1073 if ((tunnel_mode && !ITP_P_ISTUNNEL(itp, iph)) ||
1074 1074 (!tunnel_mode && ITP_P_ISTUNNEL(itp, iph))) {
1075 1075 mutex_exit(&itp->itp_lock);
1076 1076 spdsock_error(q, mp, EBUSY, 0);
1077 1077 return;
1078 1078 }
1079 1079 empty_itp = B_FALSE;
1080 1080 } else {
1081 1081 empty_itp = B_TRUE;
1082 1082 itp->itp_flags = active ? ITPF_P_ACTIVE : ITPF_I_ACTIVE;
1083 1083 if (tunnel_mode)
1084 1084 itp->itp_flags |= active ? ITPF_P_TUNNEL :
1085 1085 ITPF_I_TUNNEL;
1086 1086 }
1087 1087 } else {
1088 1088 empty_itp = B_FALSE;
1089 1089 }
1090 1090
1091 1091 if (rule->spd_rule_index != 0) {
1092 1092 diag = SPD_DIAGNOSTIC_INVALID_RULE_INDEX;
1093 1093 error = EINVAL;
1094 1094 goto fail2;
1095 1095 }
1096 1096
1097 1097 if (!spdsock_ext_to_sel(extv, &sel, &diag)) {
1098 1098 error = EINVAL;
1099 1099 goto fail2;
1100 1100 }
1101 1101
1102 1102 if (itp != NULL) {
1103 1103 if (tunnel_mode) {
1104 1104 if (sel.ipsl_valid &
1105 1105 (IPSL_REMOTE_PORT | IPSL_LOCAL_PORT)) {
1106 1106 itp->itp_flags |= active ?
1107 1107 ITPF_P_PER_PORT_SECURITY :
1108 1108 ITPF_I_PER_PORT_SECURITY;
1109 1109 }
1110 1110 } else {
1111 1111 /*
1112 1112 * For now, we don't allow transport-mode on a tunnel
1113 1113 * with ANY specific selectors. Bail if we have such
1114 1114 * a request.
1115 1115 */
1116 1116 if (sel.ipsl_valid & IPSL_WILDCARD) {
1117 1117 diag = SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS;
1118 1118 error = EINVAL;
1119 1119 goto fail2;
1120 1120 }
1121 1121 }
1122 1122 }
1123 1123
1124 1124 if (!spdsock_ext_to_actvec(extv, &actp, &nact, &diag, spds)) {
1125 1125 error = EINVAL;
1126 1126 goto fail2;
1127 1127 }
1128 1128 /*
1129 1129 * If no addresses were specified, add both.
1130 1130 */
1131 1131 afs = sel.ipsl_valid & (IPSL_IPV6|IPSL_IPV4);
1132 1132 if (afs == 0)
1133 1133 afs = (IPSL_IPV6|IPSL_IPV4);
1134 1134
1135 1135 rw_enter(&iph->iph_lock, RW_WRITER);
1136 1136
1137 1137 if (rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) {
1138 1138 error = mkrulepair(iph, rule, &sel, actp, nact,
1139 1139 IPSEC_TYPE_OUTBOUND, afs, &rulep, index, spds);
1140 1140 if (error != 0)
1141 1141 goto fail;
1142 1142 }
1143 1143
1144 1144 if (rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) {
1145 1145 error = mkrulepair(iph, rule, &sel, actp, nact,
1146 1146 IPSEC_TYPE_INBOUND, afs, &rulep, index, spds);
1147 1147 if (error != 0)
1148 1148 goto fail;
1149 1149 }
1150 1150
1151 1151 while ((--rulep) >= &rules[0]) {
1152 1152 ipsec_enter_policy(iph, rulep->pol, rulep->dir,
1153 1153 spds->spds_netstack);
1154 1154 }
1155 1155 rw_exit(&iph->iph_lock);
1156 1156 if (itp != NULL)
1157 1157 mutex_exit(&itp->itp_lock);
1158 1158
1159 1159 ipsec_actvec_free(actp, nact);
1160 1160 spd_echo(q, mp);
1161 1161 if (auditing) {
1162 1162 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1163 1163 cred_t *cr;
1164 1164 pid_t cpid;
1165 1165
1166 1166 cr = msg_getcred(mp, &cpid);
1167 1167 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1168 1168 audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack,
1169 1169 ITP_NAME(itp), active, 0, cpid);
1170 1170 }
1171 1171 return;
1172 1172
1173 1173 fail:
1174 1174 rw_exit(&iph->iph_lock);
1175 1175 while ((--rulep) >= &rules[0])
1176 1176 IPPOL_REFRELE(rulep->pol);
1177 1177 ipsec_actvec_free(actp, nact);
1178 1178 fail2:
1179 1179 if (itp != NULL) {
1180 1180 if (empty_itp)
1181 1181 itp->itp_flags = 0;
1182 1182 mutex_exit(&itp->itp_lock);
1183 1183 }
1184 1184 spdsock_error(q, mp, error, diag);
1185 1185 if (auditing) {
1186 1186 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1187 1187 cred_t *cr;
1188 1188 pid_t cpid;
1189 1189
1190 1190 cr = msg_getcred(mp, &cpid);
1191 1191 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1192 1192 audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack,
1193 1193 ITP_NAME(itp), active, error, cpid);
1194 1194 }
1195 1195 }
1196 1196
1197 1197 void
1198 1198 spdsock_deleterule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
1199 1199 spd_ext_t **extv, ipsec_tun_pol_t *itp)
1200 1200 {
1201 1201 ipsec_selkey_t sel;
1202 1202 struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE];
1203 1203 int err, diag = 0;
1204 1204 spdsock_t *ss = (spdsock_t *)q->q_ptr;
1205 1205 netstack_t *ns = ss->spdsock_spds->spds_netstack;
1206 1206 uint32_t auditing = AU_AUDITING();
1207 1207
1208 1208 if (rule == NULL) {
1209 1209 spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT);
1210 1210 if (auditing) {
1211 1211 boolean_t active;
1212 1212 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1213 1213 cred_t *cr;
1214 1214 pid_t cpid;
1215 1215
1216 1216 cr = msg_getcred(mp, &cpid);
1217 1217 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1218 1218 audit_pf_policy(SPD_DELETERULE, cr, ns,
1219 1219 ITP_NAME(itp), active, SPD_DIAGNOSTIC_NO_RULE_EXT,
1220 1220 cpid);
1221 1221 }
1222 1222 return;
1223 1223 }
1224 1224
1225 1225 /*
1226 1226 * Must enter itp_lock first to avoid deadlock. See tun.c's
1227 1227 * set_sec_simple() for the other case of itp_lock and iph_lock.
1228 1228 */
1229 1229 if (itp != NULL)
1230 1230 mutex_enter(&itp->itp_lock);
1231 1231
1232 1232 if (rule->spd_rule_index != 0) {
1233 1233 if (ipsec_policy_delete_index(iph, rule->spd_rule_index, ns) !=
1234 1234 0) {
1235 1235 err = ESRCH;
1236 1236 goto fail;
1237 1237 }
1238 1238 } else {
1239 1239 if (!spdsock_ext_to_sel(extv, &sel, &diag)) {
1240 1240 err = EINVAL; /* diag already set... */
1241 1241 goto fail;
1242 1242 }
1243 1243
1244 1244 if ((rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) &&
1245 1245 !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_INBOUND, ns)) {
1246 1246 err = ESRCH;
1247 1247 goto fail;
1248 1248 }
1249 1249
1250 1250 if ((rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) &&
1251 1251 !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_OUTBOUND, ns)) {
1252 1252 err = ESRCH;
1253 1253 goto fail;
1254 1254 }
1255 1255 }
1256 1256
1257 1257 if (itp != NULL) {
1258 1258 ASSERT(iph == itp->itp_policy || iph == itp->itp_inactive);
1259 1259 rw_enter(&iph->iph_lock, RW_READER);
1260 1260 if (avl_numnodes(&iph->iph_rulebyid) == 0) {
1261 1261 if (iph == itp->itp_policy)
1262 1262 itp->itp_flags &= ~ITPF_PFLAGS;
1263 1263 else
1264 1264 itp->itp_flags &= ~ITPF_IFLAGS;
1265 1265 }
1266 1266 /* Can exit locks in any order. */
1267 1267 rw_exit(&iph->iph_lock);
1268 1268 mutex_exit(&itp->itp_lock);
1269 1269 }
1270 1270 spd_echo(q, mp);
1271 1271 if (auditing) {
1272 1272 boolean_t active;
1273 1273 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1274 1274 cred_t *cr;
1275 1275 pid_t cpid;
1276 1276
1277 1277 cr = msg_getcred(mp, &cpid);
1278 1278 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1279 1279 audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp),
1280 1280 active, 0, cpid);
1281 1281 }
1282 1282 return;
1283 1283 fail:
1284 1284 if (itp != NULL)
1285 1285 mutex_exit(&itp->itp_lock);
1286 1286 spdsock_error(q, mp, err, diag);
1287 1287 if (auditing) {
1288 1288 boolean_t active;
1289 1289 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1290 1290 cred_t *cr;
1291 1291 pid_t cpid;
1292 1292
1293 1293 cr = msg_getcred(mp, &cpid);
1294 1294 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1295 1295 audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp),
1296 1296 active, err, cpid);
1297 1297 }
1298 1298 }
1299 1299
1300 1300 /* Do NOT consume a reference to itp. */
1301 1301 /* ARGSUSED */
1302 1302 static void
1303 1303 spdsock_flip_node(ipsec_tun_pol_t *itp, void *ignoreme, netstack_t *ns)
1304 1304 {
1305 1305 mutex_enter(&itp->itp_lock);
1306 1306 ITPF_SWAP(itp->itp_flags);
1307 1307 ipsec_swap_policy(itp->itp_policy, itp->itp_inactive, ns);
1308 1308 mutex_exit(&itp->itp_lock);
1309 1309 /* SPD_FLIP is worth a tunnel MTU check. */
1310 1310 update_iptun_policy(itp);
1311 1311 }
1312 1312
1313 1313 void
1314 1314 spdsock_flip(queue_t *q, mblk_t *mp, spd_if_t *tunname)
1315 1315 {
1316 1316 char *tname;
1317 1317 ipsec_tun_pol_t *itp;
1318 1318 spdsock_t *ss = (spdsock_t *)q->q_ptr;
1319 1319 netstack_t *ns = ss->spdsock_spds->spds_netstack;
1320 1320 uint32_t auditing = AU_AUDITING();
1321 1321
1322 1322 if (tunname != NULL) {
1323 1323 tname = (char *)tunname->spd_if_name;
1324 1324 if (*tname == '\0') {
1325 1325 /* can't fail */
1326 1326 ipsec_swap_global_policy(ns);
1327 1327 if (auditing) {
1328 1328 boolean_t active;
1329 1329 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1330 1330 cred_t *cr;
1331 1331 pid_t cpid;
1332 1332
1333 1333 cr = msg_getcred(mp, &cpid);
1334 1334 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1335 1335 audit_pf_policy(SPD_FLIP, cr, ns,
1336 1336 NULL, active, 0, cpid);
1337 1337 }
1338 1338 itp_walk(spdsock_flip_node, NULL, ns);
1339 1339 if (auditing) {
1340 1340 boolean_t active;
1341 1341 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1342 1342 cred_t *cr;
1343 1343 pid_t cpid;
1344 1344
1345 1345 cr = msg_getcred(mp, &cpid);
1346 1346 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1347 1347 audit_pf_policy(SPD_FLIP, cr, ns,
1348 1348 "all tunnels", active, 0, cpid);
1349 1349 }
1350 1350 } else {
1351 1351 itp = get_tunnel_policy(tname, ns);
1352 1352 if (itp == NULL) {
1353 1353 /* Better idea for "tunnel not found"? */
1354 1354 spdsock_error(q, mp, ESRCH, 0);
1355 1355 if (auditing) {
1356 1356 boolean_t active;
1357 1357 spd_msg_t *spmsg =
1358 1358 (spd_msg_t *)mp->b_rptr;
1359 1359 cred_t *cr;
1360 1360 pid_t cpid;
1361 1361
1362 1362 cr = msg_getcred(mp, &cpid);
1363 1363 active = (spmsg->spd_msg_spdid ==
1364 1364 SPD_ACTIVE);
1365 1365 audit_pf_policy(SPD_FLIP, cr, ns,
1366 1366 ITP_NAME(itp), active,
1367 1367 ESRCH, cpid);
1368 1368 }
1369 1369 return;
1370 1370 }
1371 1371 spdsock_flip_node(itp, NULL, ns);
1372 1372 if (auditing) {
1373 1373 boolean_t active;
1374 1374 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1375 1375 cred_t *cr;
1376 1376 pid_t cpid;
1377 1377
1378 1378 cr = msg_getcred(mp, &cpid);
1379 1379 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1380 1380 audit_pf_policy(SPD_FLIP, cr, ns,
1381 1381 ITP_NAME(itp), active, 0, cpid);
1382 1382 }
1383 1383 ITP_REFRELE(itp, ns);
1384 1384 }
1385 1385 } else {
1386 1386 ipsec_swap_global_policy(ns); /* can't fail */
1387 1387 if (auditing) {
1388 1388 boolean_t active;
1389 1389 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1390 1390 cred_t *cr;
1391 1391 pid_t cpid;
1392 1392
1393 1393 cr = msg_getcred(mp, &cpid);
1394 1394 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1395 1395 audit_pf_policy(SPD_FLIP, cr,
1396 1396 ns, NULL, active, 0, cpid);
1397 1397 }
1398 1398 }
1399 1399 spd_echo(q, mp);
1400 1400 }
1401 1401
1402 1402 /*
1403 1403 * Unimplemented feature
1404 1404 */
1405 1405 /* ARGSUSED */
1406 1406 static void
1407 1407 spdsock_lookup(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
1408 1408 spd_ext_t **extv, ipsec_tun_pol_t *itp)
1409 1409 {
1410 1410 spdsock_error(q, mp, EINVAL, 0);
1411 1411 }
1412 1412
1413 1413
1414 1414 static mblk_t *
1415 1415 spdsock_dump_ruleset(mblk_t *req, ipsec_policy_head_t *iph,
1416 1416 uint32_t count, uint16_t error)
1417 1417 {
1418 1418 size_t len = sizeof (spd_ruleset_ext_t) + sizeof (spd_msg_t);
1419 1419 spd_msg_t *msg;
1420 1420 spd_ruleset_ext_t *ruleset;
1421 1421 mblk_t *m = allocb(len, BPRI_HI);
1422 1422
1423 1423 ASSERT(RW_READ_HELD(&iph->iph_lock));
1424 1424
1425 1425 if (m == NULL) {
1426 1426 return (NULL);
1427 1427 }
1428 1428 msg = (spd_msg_t *)m->b_rptr;
1429 1429 ruleset = (spd_ruleset_ext_t *)(&msg[1]);
1430 1430
1431 1431 m->b_wptr = (uint8_t *)&ruleset[1];
1432 1432
1433 1433 *msg = *(spd_msg_t *)(req->b_rptr);
1434 1434 msg->spd_msg_len = SPD_8TO64(len);
1435 1435 msg->spd_msg_errno = error;
1436 1436
1437 1437 ruleset->spd_ruleset_len = SPD_8TO64(sizeof (*ruleset));
1438 1438 ruleset->spd_ruleset_type = SPD_EXT_RULESET;
1439 1439 ruleset->spd_ruleset_count = count;
1440 1440 ruleset->spd_ruleset_version = iph->iph_gen;
1441 1441 return (m);
1442 1442 }
1443 1443
1444 1444 static mblk_t *
1445 1445 spdsock_dump_finish(spdsock_t *ss, int error)
1446 1446 {
1447 1447 mblk_t *m;
1448 1448 ipsec_policy_head_t *iph = ss->spdsock_dump_head;
1449 1449 mblk_t *req = ss->spdsock_dump_req;
1450 1450 netstack_t *ns = ss->spdsock_spds->spds_netstack;
1451 1451
1452 1452 rw_enter(&iph->iph_lock, RW_READER);
1453 1453 m = spdsock_dump_ruleset(req, iph, ss->spdsock_dump_count, error);
1454 1454 rw_exit(&iph->iph_lock);
1455 1455 IPPH_REFRELE(iph, ns);
1456 1456 if (ss->spdsock_itp != NULL) {
1457 1457 ITP_REFRELE(ss->spdsock_itp, ns);
1458 1458 ss->spdsock_itp = NULL;
1459 1459 }
1460 1460 ss->spdsock_dump_req = NULL;
1461 1461 freemsg(req);
1462 1462
1463 1463 return (m);
1464 1464 }
1465 1465
1466 1466 /*
1467 1467 * Rule encoding functions.
1468 1468 * We do a two-pass encode.
1469 1469 * If base != NULL, fill in encoded rule part starting at base+offset.
1470 1470 * Always return "offset" plus length of to-be-encoded data.
1471 1471 */
1472 1472 static uint_t
1473 1473 spdsock_encode_typecode(uint8_t *base, uint_t offset, uint8_t type,
1474 1474 uint8_t type_end, uint8_t code, uint8_t code_end)
1475 1475 {
1476 1476 struct spd_typecode *tcp;
1477 1477
1478 1478 ASSERT(ALIGNED64(offset));
1479 1479
1480 1480 if (base != NULL) {
1481 1481 tcp = (struct spd_typecode *)(base + offset);
1482 1482 tcp->spd_typecode_len = SPD_8TO64(sizeof (*tcp));
1483 1483 tcp->spd_typecode_exttype = SPD_EXT_ICMP_TYPECODE;
1484 1484 tcp->spd_typecode_code = code;
1485 1485 tcp->spd_typecode_type = type;
1486 1486 tcp->spd_typecode_type_end = type_end;
1487 1487 tcp->spd_typecode_code_end = code_end;
1488 1488 }
1489 1489 offset += sizeof (*tcp);
1490 1490
1491 1491 ASSERT(ALIGNED64(offset));
1492 1492
1493 1493 return (offset);
1494 1494 }
1495 1495
1496 1496 static uint_t
1497 1497 spdsock_encode_proto(uint8_t *base, uint_t offset, uint8_t proto)
1498 1498 {
1499 1499 struct spd_proto *spp;
1500 1500
1501 1501 ASSERT(ALIGNED64(offset));
1502 1502
1503 1503 if (base != NULL) {
1504 1504 spp = (struct spd_proto *)(base + offset);
1505 1505 spp->spd_proto_len = SPD_8TO64(sizeof (*spp));
1506 1506 spp->spd_proto_exttype = SPD_EXT_PROTO;
1507 1507 spp->spd_proto_number = proto;
1508 1508 spp->spd_proto_reserved1 = 0;
1509 1509 spp->spd_proto_reserved2 = 0;
1510 1510 }
1511 1511 offset += sizeof (*spp);
1512 1512
1513 1513 ASSERT(ALIGNED64(offset));
1514 1514
1515 1515 return (offset);
1516 1516 }
1517 1517
1518 1518 static uint_t
1519 1519 spdsock_encode_port(uint8_t *base, uint_t offset, uint16_t ext, uint16_t port)
1520 1520 {
1521 1521 struct spd_portrange *spp;
1522 1522
1523 1523 ASSERT(ALIGNED64(offset));
1524 1524
1525 1525 if (base != NULL) {
1526 1526 spp = (struct spd_portrange *)(base + offset);
1527 1527 spp->spd_ports_len = SPD_8TO64(sizeof (*spp));
1528 1528 spp->spd_ports_exttype = ext;
1529 1529 spp->spd_ports_minport = port;
1530 1530 spp->spd_ports_maxport = port;
1531 1531 }
1532 1532 offset += sizeof (*spp);
1533 1533
1534 1534 ASSERT(ALIGNED64(offset));
1535 1535
1536 1536 return (offset);
1537 1537 }
1538 1538
1539 1539 static uint_t
1540 1540 spdsock_encode_addr(uint8_t *base, uint_t offset, uint16_t ext,
1541 1541 const ipsec_selkey_t *sel, const ipsec_addr_t *addr, uint_t pfxlen)
1542 1542 {
1543 1543 struct spd_address *sae;
1544 1544 ipsec_addr_t *spdaddr;
1545 1545 uint_t start = offset;
1546 1546 uint_t addrlen;
1547 1547 uint_t af;
1548 1548
1549 1549 if (sel->ipsl_valid & IPSL_IPV4) {
1550 1550 af = AF_INET;
1551 1551 addrlen = IP_ADDR_LEN;
1552 1552 } else {
1553 1553 af = AF_INET6;
1554 1554 addrlen = IPV6_ADDR_LEN;
1555 1555 }
1556 1556
1557 1557 ASSERT(ALIGNED64(offset));
1558 1558
1559 1559 if (base != NULL) {
1560 1560 sae = (struct spd_address *)(base + offset);
1561 1561 sae->spd_address_exttype = ext;
1562 1562 sae->spd_address_af = af;
1563 1563 sae->spd_address_prefixlen = pfxlen;
1564 1564 sae->spd_address_reserved2 = 0;
1565 1565
1566 1566 spdaddr = (ipsec_addr_t *)(&sae[1]);
1567 1567 bcopy(addr, spdaddr, addrlen);
1568 1568 }
1569 1569 offset += sizeof (*sae);
1570 1570 addrlen = roundup(addrlen, sizeof (uint64_t));
1571 1571 offset += addrlen;
1572 1572
1573 1573 ASSERT(ALIGNED64(offset));
1574 1574
1575 1575 if (base != NULL)
1576 1576 sae->spd_address_len = SPD_8TO64(offset - start);
1577 1577 return (offset);
1578 1578 }
1579 1579
1580 1580 static uint_t
1581 1581 spdsock_encode_sel(uint8_t *base, uint_t offset, const ipsec_sel_t *sel)
1582 1582 {
1583 1583 const ipsec_selkey_t *selkey = &sel->ipsl_key;
1584 1584
1585 1585 if (selkey->ipsl_valid & IPSL_PROTOCOL)
1586 1586 offset = spdsock_encode_proto(base, offset, selkey->ipsl_proto);
1587 1587 if (selkey->ipsl_valid & IPSL_LOCAL_PORT)
1588 1588 offset = spdsock_encode_port(base, offset, SPD_EXT_LCLPORT,
1589 1589 selkey->ipsl_lport);
1590 1590 if (selkey->ipsl_valid & IPSL_REMOTE_PORT)
1591 1591 offset = spdsock_encode_port(base, offset, SPD_EXT_REMPORT,
1592 1592 selkey->ipsl_rport);
1593 1593 if (selkey->ipsl_valid & IPSL_REMOTE_ADDR)
1594 1594 offset = spdsock_encode_addr(base, offset, SPD_EXT_REMADDR,
1595 1595 selkey, &selkey->ipsl_remote, selkey->ipsl_remote_pfxlen);
1596 1596 if (selkey->ipsl_valid & IPSL_LOCAL_ADDR)
1597 1597 offset = spdsock_encode_addr(base, offset, SPD_EXT_LCLADDR,
1598 1598 selkey, &selkey->ipsl_local, selkey->ipsl_local_pfxlen);
1599 1599 if (selkey->ipsl_valid & IPSL_ICMP_TYPE) {
1600 1600 offset = spdsock_encode_typecode(base, offset,
1601 1601 selkey->ipsl_icmp_type, selkey->ipsl_icmp_type_end,
1602 1602 (selkey->ipsl_valid & IPSL_ICMP_CODE) ?
1603 1603 selkey->ipsl_icmp_code : 255,
1604 1604 (selkey->ipsl_valid & IPSL_ICMP_CODE) ?
1605 1605 selkey->ipsl_icmp_code_end : 255);
1606 1606 }
1607 1607 return (offset);
1608 1608 }
1609 1609
1610 1610 static uint_t
1611 1611 spdsock_encode_actattr(uint8_t *base, uint_t offset, uint32_t tag,
1612 1612 uint32_t value)
1613 1613 {
1614 1614 struct spd_attribute *attr;
1615 1615
1616 1616 ASSERT(ALIGNED64(offset));
1617 1617
1618 1618 if (base != NULL) {
1619 1619 attr = (struct spd_attribute *)(base + offset);
1620 1620 attr->spd_attr_tag = tag;
1621 1621 attr->spd_attr_value = value;
1622 1622 }
1623 1623 offset += sizeof (struct spd_attribute);
1624 1624
1625 1625 ASSERT(ALIGNED64(offset));
1626 1626
1627 1627 return (offset);
1628 1628 }
1629 1629
1630 1630
1631 1631 #define EMIT(t, v) offset = spdsock_encode_actattr(base, offset, (t), (v))
1632 1632
1633 1633 static uint_t
1634 1634 spdsock_encode_action(uint8_t *base, uint_t offset, const ipsec_action_t *ap)
1635 1635 {
1636 1636 const struct ipsec_act *act = &(ap->ipa_act);
1637 1637 uint_t flags;
1638 1638
1639 1639 EMIT(SPD_ATTR_EMPTY, 0);
1640 1640 switch (act->ipa_type) {
1641 1641 case IPSEC_ACT_DISCARD:
1642 1642 case IPSEC_ACT_REJECT:
1643 1643 EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_DROP);
1644 1644 break;
1645 1645 case IPSEC_ACT_BYPASS:
1646 1646 case IPSEC_ACT_CLEAR:
1647 1647 EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_PASS);
1648 1648 break;
1649 1649
1650 1650 case IPSEC_ACT_APPLY:
1651 1651 EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_IPSEC);
1652 1652 flags = 0;
1653 1653 if (act->ipa_apply.ipp_use_ah)
1654 1654 flags |= SPD_APPLY_AH;
1655 1655 if (act->ipa_apply.ipp_use_esp)
1656 1656 flags |= SPD_APPLY_ESP;
1657 1657 if (act->ipa_apply.ipp_use_espa)
1658 1658 flags |= SPD_APPLY_ESPA;
1659 1659 if (act->ipa_apply.ipp_use_se)
1660 1660 flags |= SPD_APPLY_SE;
1661 1661 if (act->ipa_apply.ipp_use_unique)
1662 1662 flags |= SPD_APPLY_UNIQUE;
1663 1663 EMIT(SPD_ATTR_FLAGS, flags);
1664 1664 if (flags & SPD_APPLY_AH) {
1665 1665 EMIT(SPD_ATTR_AH_AUTH, act->ipa_apply.ipp_auth_alg);
1666 1666 EMIT(SPD_ATTR_AH_MINBITS,
1667 1667 act->ipa_apply.ipp_ah_minbits);
1668 1668 EMIT(SPD_ATTR_AH_MAXBITS,
1669 1669 act->ipa_apply.ipp_ah_maxbits);
1670 1670 }
1671 1671 if (flags & SPD_APPLY_ESP) {
1672 1672 EMIT(SPD_ATTR_ESP_ENCR, act->ipa_apply.ipp_encr_alg);
1673 1673 EMIT(SPD_ATTR_ENCR_MINBITS,
1674 1674 act->ipa_apply.ipp_espe_minbits);
1675 1675 EMIT(SPD_ATTR_ENCR_MAXBITS,
1676 1676 act->ipa_apply.ipp_espe_maxbits);
1677 1677 if (flags & SPD_APPLY_ESPA) {
1678 1678 EMIT(SPD_ATTR_ESP_AUTH,
1679 1679 act->ipa_apply.ipp_esp_auth_alg);
1680 1680 EMIT(SPD_ATTR_ESPA_MINBITS,
1681 1681 act->ipa_apply.ipp_espa_minbits);
1682 1682 EMIT(SPD_ATTR_ESPA_MAXBITS,
1683 1683 act->ipa_apply.ipp_espa_maxbits);
1684 1684 }
1685 1685 }
1686 1686 if (act->ipa_apply.ipp_km_proto != 0)
1687 1687 EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_proto);
1688 1688 if (act->ipa_apply.ipp_km_cookie != 0)
1689 1689 EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_cookie);
1690 1690 if (act->ipa_apply.ipp_replay_depth != 0)
1691 1691 EMIT(SPD_ATTR_REPLAY_DEPTH,
1692 1692 act->ipa_apply.ipp_replay_depth);
1693 1693 /* Add more here */
1694 1694 break;
1695 1695 }
1696 1696
1697 1697 return (offset);
1698 1698 }
1699 1699
1700 1700 static uint_t
1701 1701 spdsock_encode_action_list(uint8_t *base, uint_t offset,
1702 1702 const ipsec_action_t *ap)
1703 1703 {
1704 1704 struct spd_ext_actions *act;
1705 1705 uint_t nact = 0;
1706 1706 uint_t start = offset;
1707 1707
1708 1708 ASSERT(ALIGNED64(offset));
1709 1709
1710 1710 if (base != NULL) {
1711 1711 act = (struct spd_ext_actions *)(base + offset);
1712 1712 act->spd_actions_len = 0;
1713 1713 act->spd_actions_exttype = SPD_EXT_ACTION;
1714 1714 act->spd_actions_count = 0;
1715 1715 act->spd_actions_reserved = 0;
1716 1716 }
1717 1717
1718 1718 offset += sizeof (*act);
1719 1719
1720 1720 ASSERT(ALIGNED64(offset));
1721 1721
1722 1722 while (ap != NULL) {
1723 1723 offset = spdsock_encode_action(base, offset, ap);
1724 1724 ap = ap->ipa_next;
1725 1725 nact++;
1726 1726 if (ap != NULL) {
1727 1727 EMIT(SPD_ATTR_NEXT, 0);
1728 1728 }
1729 1729 }
1730 1730 EMIT(SPD_ATTR_END, 0);
1731 1731
1732 1732 ASSERT(ALIGNED64(offset));
1733 1733
1734 1734 if (base != NULL) {
1735 1735 act->spd_actions_count = nact;
1736 1736 act->spd_actions_len = SPD_8TO64(offset - start);
1737 1737 }
1738 1738
1739 1739 return (offset);
1740 1740 }
1741 1741
1742 1742 #undef EMIT
1743 1743
1744 1744 /* ARGSUSED */
1745 1745 static uint_t
1746 1746 spdsock_rule_flags(uint_t dir, uint_t af)
1747 1747 {
1748 1748 uint_t flags = 0;
1749 1749
1750 1750 if (dir == IPSEC_TYPE_INBOUND)
1751 1751 flags |= SPD_RULE_FLAG_INBOUND;
1752 1752 if (dir == IPSEC_TYPE_OUTBOUND)
1753 1753 flags |= SPD_RULE_FLAG_OUTBOUND;
1754 1754
1755 1755 return (flags);
1756 1756 }
1757 1757
1758 1758
1759 1759 static uint_t
1760 1760 spdsock_encode_rule_head(uint8_t *base, uint_t offset, spd_msg_t *req,
1761 1761 const ipsec_policy_t *rule, uint_t dir, uint_t af, char *name,
1762 1762 boolean_t tunnel)
1763 1763 {
1764 1764 struct spd_msg *spmsg;
1765 1765 struct spd_rule *spr;
1766 1766 spd_if_t *sid;
1767 1767
1768 1768 uint_t start = offset;
1769 1769
1770 1770 ASSERT(ALIGNED64(offset));
1771 1771
1772 1772 if (base != NULL) {
1773 1773 spmsg = (struct spd_msg *)(base + offset);
1774 1774 bzero(spmsg, sizeof (*spmsg));
1775 1775 spmsg->spd_msg_version = PF_POLICY_V1;
1776 1776 spmsg->spd_msg_type = SPD_DUMP;
1777 1777 spmsg->spd_msg_seq = req->spd_msg_seq;
1778 1778 spmsg->spd_msg_pid = req->spd_msg_pid;
1779 1779 }
1780 1780 offset += sizeof (struct spd_msg);
1781 1781
1782 1782 ASSERT(ALIGNED64(offset));
1783 1783
1784 1784 if (base != NULL) {
1785 1785 spr = (struct spd_rule *)(base + offset);
1786 1786 spr->spd_rule_type = SPD_EXT_RULE;
1787 1787 spr->spd_rule_priority = rule->ipsp_prio;
1788 1788 spr->spd_rule_flags = spdsock_rule_flags(dir, af);
1789 1789 if (tunnel)
1790 1790 spr->spd_rule_flags |= SPD_RULE_FLAG_TUNNEL;
1791 1791 spr->spd_rule_unused = 0;
1792 1792 spr->spd_rule_len = SPD_8TO64(sizeof (*spr));
1793 1793 spr->spd_rule_index = rule->ipsp_index;
1794 1794 }
1795 1795 offset += sizeof (struct spd_rule);
1796 1796
1797 1797 /*
1798 1798 * If we have an interface name (i.e. if this policy head came from
1799 1799 * a tunnel), add the SPD_EXT_TUN_NAME extension.
1800 1800 */
1801 1801 if (name != NULL) {
1802 1802
1803 1803 ASSERT(ALIGNED64(offset));
1804 1804
1805 1805 if (base != NULL) {
1806 1806 sid = (spd_if_t *)(base + offset);
1807 1807 sid->spd_if_exttype = SPD_EXT_TUN_NAME;
1808 1808 sid->spd_if_len = SPD_8TO64(sizeof (spd_if_t) +
1809 1809 roundup((strlen(name) - 4), 8));
1810 1810 (void) strlcpy((char *)sid->spd_if_name, name,
1811 1811 LIFNAMSIZ);
1812 1812 }
1813 1813
1814 1814 offset += sizeof (spd_if_t) + roundup((strlen(name) - 4), 8);
1815 1815 }
1816 1816
1817 1817 offset = spdsock_encode_sel(base, offset, rule->ipsp_sel);
1818 1818 offset = spdsock_encode_action_list(base, offset, rule->ipsp_act);
1819 1819
1820 1820 ASSERT(ALIGNED64(offset));
1821 1821
1822 1822 if (base != NULL) {
1823 1823 spmsg->spd_msg_len = SPD_8TO64(offset - start);
1824 1824 }
1825 1825 return (offset);
1826 1826 }
1827 1827
1828 1828 /* ARGSUSED */
1829 1829 static mblk_t *
1830 1830 spdsock_encode_rule(mblk_t *req, const ipsec_policy_t *rule,
1831 1831 uint_t dir, uint_t af, char *name, boolean_t tunnel)
1832 1832 {
1833 1833 mblk_t *m;
1834 1834 uint_t len;
1835 1835 spd_msg_t *mreq = (spd_msg_t *)req->b_rptr;
1836 1836
1837 1837 /*
1838 1838 * Figure out how much space we'll need.
1839 1839 */
1840 1840 len = spdsock_encode_rule_head(NULL, 0, mreq, rule, dir, af, name,
1841 1841 tunnel);
1842 1842
1843 1843 /*
1844 1844 * Allocate mblk.
1845 1845 */
1846 1846 m = allocb(len, BPRI_HI);
1847 1847 if (m == NULL)
1848 1848 return (NULL);
1849 1849
1850 1850 /*
1851 1851 * Fill it in..
1852 1852 */
1853 1853 m->b_wptr = m->b_rptr + len;
1854 1854 bzero(m->b_rptr, len);
1855 1855 (void) spdsock_encode_rule_head(m->b_rptr, 0, mreq, rule, dir, af,
1856 1856 name, tunnel);
1857 1857 return (m);
1858 1858 }
1859 1859
1860 1860 static ipsec_policy_t *
1861 1861 spdsock_dump_next_in_chain(spdsock_t *ss, ipsec_policy_head_t *iph,
1862 1862 ipsec_policy_t *cur)
1863 1863 {
1864 1864 ASSERT(RW_READ_HELD(&iph->iph_lock));
1865 1865
1866 1866 ss->spdsock_dump_count++;
1867 1867 ss->spdsock_dump_cur_rule = cur->ipsp_hash.hash_next;
1868 1868 return (cur);
1869 1869 }
1870 1870
1871 1871 static ipsec_policy_t *
1872 1872 spdsock_dump_next_rule(spdsock_t *ss, ipsec_policy_head_t *iph)
1873 1873 {
1874 1874 ipsec_policy_t *cur;
1875 1875 ipsec_policy_root_t *ipr;
1876 1876 int chain, nchains, type, af;
1877 1877
1878 1878 ASSERT(RW_READ_HELD(&iph->iph_lock));
1879 1879
1880 1880 cur = ss->spdsock_dump_cur_rule;
1881 1881
1882 1882 if (cur != NULL)
1883 1883 return (spdsock_dump_next_in_chain(ss, iph, cur));
1884 1884
1885 1885 type = ss->spdsock_dump_cur_type;
1886 1886
1887 1887 next:
1888 1888 chain = ss->spdsock_dump_cur_chain;
1889 1889 ipr = &iph->iph_root[type];
1890 1890 nchains = ipr->ipr_nchains;
1891 1891
1892 1892 while (chain < nchains) {
1893 1893 cur = ipr->ipr_hash[chain].hash_head;
1894 1894 chain++;
1895 1895 if (cur != NULL) {
1896 1896 ss->spdsock_dump_cur_chain = chain;
1897 1897 return (spdsock_dump_next_in_chain(ss, iph, cur));
1898 1898 }
1899 1899 }
1900 1900 ss->spdsock_dump_cur_chain = nchains;
1901 1901
1902 1902 af = ss->spdsock_dump_cur_af;
1903 1903 while (af < IPSEC_NAF) {
1904 1904 cur = ipr->ipr_nonhash[af];
1905 1905 af++;
1906 1906 if (cur != NULL) {
1907 1907 ss->spdsock_dump_cur_af = af;
1908 1908 return (spdsock_dump_next_in_chain(ss, iph, cur));
1909 1909 }
1910 1910 }
1911 1911
1912 1912 type++;
1913 1913 if (type >= IPSEC_NTYPES)
1914 1914 return (NULL);
1915 1915
1916 1916 ss->spdsock_dump_cur_chain = 0;
1917 1917 ss->spdsock_dump_cur_type = type;
1918 1918 ss->spdsock_dump_cur_af = IPSEC_AF_V4;
1919 1919 goto next;
1920 1920
1921 1921 }
1922 1922
1923 1923 /*
1924 1924 * If we're done with one policy head, but have more to go, we iterate through
1925 1925 * another IPsec tunnel policy head (itp). Return NULL if it is an error
1926 1926 * worthy of returning EAGAIN via PF_POLICY.
1927 1927 */
1928 1928 static ipsec_tun_pol_t *
1929 1929 spdsock_dump_iterate_next_tunnel(spdsock_t *ss, ipsec_stack_t *ipss)
1930 1930 {
1931 1931 ipsec_tun_pol_t *itp;
1932 1932
1933 1933 ASSERT(RW_READ_HELD(&ipss->ipsec_tunnel_policy_lock));
1934 1934 if (ipss->ipsec_tunnel_policy_gen > ss->spdsock_dump_tun_gen) {
1935 1935 /* Oops, state of the tunnel polheads changed. */
1936 1936 itp = NULL;
1937 1937 } else if (ss->spdsock_itp == NULL) {
1938 1938 /* Just finished global, find first node. */
1939 1939 itp = avl_first(&ipss->ipsec_tunnel_policies);
1940 1940 } else {
1941 1941 /* We just finished current polhead, find the next one. */
1942 1942 itp = AVL_NEXT(&ipss->ipsec_tunnel_policies, ss->spdsock_itp);
1943 1943 }
1944 1944 if (itp != NULL) {
1945 1945 ITP_REFHOLD(itp);
1946 1946 }
1947 1947 if (ss->spdsock_itp != NULL) {
1948 1948 ITP_REFRELE(ss->spdsock_itp, ipss->ipsec_netstack);
1949 1949 }
1950 1950 ss->spdsock_itp = itp;
1951 1951 return (itp);
1952 1952 }
1953 1953
1954 1954 static mblk_t *
1955 1955 spdsock_dump_next_record(spdsock_t *ss)
1956 1956 {
1957 1957 ipsec_policy_head_t *iph;
1958 1958 ipsec_policy_t *rule;
1959 1959 mblk_t *m;
1960 1960 ipsec_tun_pol_t *itp;
1961 1961 netstack_t *ns = ss->spdsock_spds->spds_netstack;
1962 1962 ipsec_stack_t *ipss = ns->netstack_ipsec;
1963 1963
1964 1964 iph = ss->spdsock_dump_head;
1965 1965
1966 1966 ASSERT(iph != NULL);
1967 1967
1968 1968 rw_enter(&iph->iph_lock, RW_READER);
1969 1969
1970 1970 if (iph->iph_gen != ss->spdsock_dump_gen) {
1971 1971 rw_exit(&iph->iph_lock);
1972 1972 return (spdsock_dump_finish(ss, EAGAIN));
1973 1973 }
1974 1974
1975 1975 while ((rule = spdsock_dump_next_rule(ss, iph)) == NULL) {
1976 1976 rw_exit(&iph->iph_lock);
1977 1977 if (--(ss->spdsock_dump_remaining_polheads) == 0)
1978 1978 return (spdsock_dump_finish(ss, 0));
1979 1979
1980 1980
1981 1981 /*
1982 1982 * If we reach here, we have more policy heads (tunnel
1983 1983 * entries) to dump. Let's reset to a new policy head
1984 1984 * and get some more rules.
1985 1985 *
1986 1986 * An empty policy head will have spdsock_dump_next_rule()
1987 1987 * return NULL, and we loop (while dropping the number of
1988 1988 * remaining polheads). If we loop to 0, we finish. We
1989 1989 * keep looping until we hit 0 or until we have a rule to
1990 1990 * encode.
1991 1991 *
1992 1992 * NOTE: No need for ITP_REF*() macros here as we're only
1993 1993 * going after and refholding the policy head itself.
1994 1994 */
1995 1995 rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER);
1996 1996 itp = spdsock_dump_iterate_next_tunnel(ss, ipss);
1997 1997 if (itp == NULL) {
1998 1998 rw_exit(&ipss->ipsec_tunnel_policy_lock);
1999 1999 return (spdsock_dump_finish(ss, EAGAIN));
2000 2000 }
2001 2001
2002 2002 /* Reset other spdsock_dump thingies. */
2003 2003 IPPH_REFRELE(ss->spdsock_dump_head, ns);
2004 2004 if (ss->spdsock_dump_active) {
2005 2005 ss->spdsock_dump_tunnel =
2006 2006 itp->itp_flags & ITPF_P_TUNNEL;
2007 2007 iph = itp->itp_policy;
2008 2008 } else {
2009 2009 ss->spdsock_dump_tunnel =
2010 2010 itp->itp_flags & ITPF_I_TUNNEL;
2011 2011 iph = itp->itp_inactive;
2012 2012 }
2013 2013 IPPH_REFHOLD(iph);
2014 2014 rw_exit(&ipss->ipsec_tunnel_policy_lock);
2015 2015
2016 2016 rw_enter(&iph->iph_lock, RW_READER);
2017 2017 RESET_SPDSOCK_DUMP_POLHEAD(ss, iph);
2018 2018 }
2019 2019
2020 2020 m = spdsock_encode_rule(ss->spdsock_dump_req, rule,
2021 2021 ss->spdsock_dump_cur_type, ss->spdsock_dump_cur_af,
2022 2022 (ss->spdsock_itp == NULL) ? NULL : ss->spdsock_itp->itp_name,
2023 2023 ss->spdsock_dump_tunnel);
2024 2024 rw_exit(&iph->iph_lock);
2025 2025
2026 2026 if (m == NULL)
2027 2027 return (spdsock_dump_finish(ss, ENOMEM));
2028 2028 return (m);
2029 2029 }
2030 2030
2031 2031 /*
2032 2032 * Dump records until we run into flow-control back-pressure.
2033 2033 */
2034 2034 static void
2035 2035 spdsock_dump_some(queue_t *q, spdsock_t *ss)
2036 2036 {
2037 2037 mblk_t *m, *dataind;
2038 2038
2039 2039 while ((ss->spdsock_dump_req != NULL) && canputnext(q)) {
2040 2040 m = spdsock_dump_next_record(ss);
2041 2041 if (m == NULL)
2042 2042 return;
2043 2043 dataind = allocb(sizeof (struct T_data_req), BPRI_HI);
2044 2044 if (dataind == NULL) {
2045 2045 freemsg(m);
2046 2046 return;
2047 2047 }
2048 2048 dataind->b_cont = m;
2049 2049 dataind->b_wptr += sizeof (struct T_data_req);
2050 2050 ((struct T_data_ind *)dataind->b_rptr)->PRIM_type = T_DATA_IND;
2051 2051 ((struct T_data_ind *)dataind->b_rptr)->MORE_flag = 0;
2052 2052 dataind->b_datap->db_type = M_PROTO;
2053 2053 putnext(q, dataind);
2054 2054 }
2055 2055 }
2056 2056
2057 2057 /*
2058 2058 * Start dumping.
2059 2059 * Format a start-of-dump record, and set up the stream and kick the rsrv
2060 2060 * procedure to continue the job..
2061 2061 */
2062 2062 /* ARGSUSED */
2063 2063 static void
2064 2064 spdsock_dump(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp)
2065 2065 {
2066 2066 spdsock_t *ss = (spdsock_t *)q->q_ptr;
2067 2067 netstack_t *ns = ss->spdsock_spds->spds_netstack;
2068 2068 ipsec_stack_t *ipss = ns->netstack_ipsec;
2069 2069 mblk_t *mr;
2070 2070
2071 2071 /* spdsock_open() already set spdsock_itp to NULL. */
2072 2072 if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) {
2073 2073 rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER);
2074 2074 ss->spdsock_dump_remaining_polheads = 1 +
2075 2075 avl_numnodes(&ipss->ipsec_tunnel_policies);
2076 2076 ss->spdsock_dump_tun_gen = ipss->ipsec_tunnel_policy_gen;
2077 2077 rw_exit(&ipss->ipsec_tunnel_policy_lock);
2078 2078 if (iph == ALL_ACTIVE_POLHEADS) {
2079 2079 iph = ipsec_system_policy(ns);
2080 2080 ss->spdsock_dump_active = B_TRUE;
2081 2081 } else {
2082 2082 iph = ipsec_inactive_policy(ns);
2083 2083 ss->spdsock_dump_active = B_FALSE;
2084 2084 }
2085 2085 ASSERT(ss->spdsock_itp == NULL);
2086 2086 } else {
2087 2087 ss->spdsock_dump_remaining_polheads = 1;
2088 2088 }
2089 2089
2090 2090 rw_enter(&iph->iph_lock, RW_READER);
2091 2091
2092 2092 mr = spdsock_dump_ruleset(mp, iph, 0, 0);
2093 2093
2094 2094 if (!mr) {
2095 2095 rw_exit(&iph->iph_lock);
2096 2096 spdsock_error(q, mp, ENOMEM, 0);
2097 2097 return;
2098 2098 }
2099 2099
2100 2100 ss->spdsock_dump_req = mp;
2101 2101 RESET_SPDSOCK_DUMP_POLHEAD(ss, iph);
2102 2102
2103 2103 rw_exit(&iph->iph_lock);
2104 2104
2105 2105 qreply(q, mr);
2106 2106 qenable(OTHERQ(q));
2107 2107 }
2108 2108
2109 2109 /* Do NOT consume a reference to ITP. */
2110 2110 void
2111 2111 spdsock_clone_node(ipsec_tun_pol_t *itp, void *ep, netstack_t *ns)
2112 2112 {
2113 2113 int *errptr = (int *)ep;
2114 2114
2115 2115 if (*errptr != 0)
2116 2116 return; /* We've failed already for some reason. */
2117 2117 mutex_enter(&itp->itp_lock);
2118 2118 ITPF_CLONE(itp->itp_flags);
2119 2119 *errptr = ipsec_copy_polhead(itp->itp_policy, itp->itp_inactive, ns);
2120 2120 mutex_exit(&itp->itp_lock);
2121 2121 }
2122 2122
2123 2123 void
2124 2124 spdsock_clone(queue_t *q, mblk_t *mp, spd_if_t *tunname)
2125 2125 {
2126 2126 int error;
2127 2127 char *tname;
2128 2128 ipsec_tun_pol_t *itp;
2129 2129 spdsock_t *ss = (spdsock_t *)q->q_ptr;
2130 2130 netstack_t *ns = ss->spdsock_spds->spds_netstack;
2131 2131 uint32_t auditing = AU_AUDITING();
2132 2132
2133 2133 if (tunname != NULL) {
2134 2134 tname = (char *)tunname->spd_if_name;
2135 2135 if (*tname == '\0') {
2136 2136 error = ipsec_clone_system_policy(ns);
2137 2137 if (auditing) {
2138 2138 boolean_t active;
2139 2139 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2140 2140 cred_t *cr;
2141 2141 pid_t cpid;
2142 2142
2143 2143 cr = msg_getcred(mp, &cpid);
2144 2144 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2145 2145 audit_pf_policy(SPD_CLONE, cr, ns,
2146 2146 NULL, active, error, cpid);
2147 2147 }
2148 2148 if (error == 0) {
2149 2149 itp_walk(spdsock_clone_node, &error, ns);
2150 2150 if (auditing) {
2151 2151 boolean_t active;
2152 2152 spd_msg_t *spmsg =
2153 2153 (spd_msg_t *)mp->b_rptr;
2154 2154 cred_t *cr;
2155 2155 pid_t cpid;
2156 2156
2157 2157 cr = msg_getcred(mp, &cpid);
2158 2158 active = (spmsg->spd_msg_spdid ==
2159 2159 SPD_ACTIVE);
2160 2160 audit_pf_policy(SPD_CLONE, cr,
2161 2161 ns, "all tunnels", active, 0,
2162 2162 cpid);
2163 2163 }
2164 2164 }
2165 2165 } else {
2166 2166 itp = get_tunnel_policy(tname, ns);
2167 2167 if (itp == NULL) {
2168 2168 spdsock_error(q, mp, ENOENT, 0);
2169 2169 if (auditing) {
2170 2170 boolean_t active;
2171 2171 spd_msg_t *spmsg =
2172 2172 (spd_msg_t *)mp->b_rptr;
2173 2173 cred_t *cr;
2174 2174 pid_t cpid;
2175 2175
2176 2176 cr = msg_getcred(mp, &cpid);
2177 2177 active = (spmsg->spd_msg_spdid ==
2178 2178 SPD_ACTIVE);
2179 2179 audit_pf_policy(SPD_CLONE, cr,
2180 2180 ns, NULL, active, ENOENT, cpid);
2181 2181 }
2182 2182 return;
2183 2183 }
2184 2184 spdsock_clone_node(itp, &error, NULL);
2185 2185 if (auditing) {
2186 2186 boolean_t active;
2187 2187 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2188 2188 cred_t *cr;
2189 2189 pid_t cpid;
2190 2190
2191 2191 cr = msg_getcred(mp, &cpid);
2192 2192 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2193 2193 audit_pf_policy(SPD_CLONE, cr, ns,
2194 2194 ITP_NAME(itp), active, error, cpid);
2195 2195 }
2196 2196 ITP_REFRELE(itp, ns);
2197 2197 }
2198 2198 } else {
2199 2199 error = ipsec_clone_system_policy(ns);
2200 2200 if (auditing) {
2201 2201 boolean_t active;
2202 2202 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2203 2203 cred_t *cr;
2204 2204 pid_t cpid;
2205 2205
2206 2206 cr = msg_getcred(mp, &cpid);
2207 2207 active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2208 2208 audit_pf_policy(SPD_CLONE, cr, ns, NULL,
2209 2209 active, error, cpid);
2210 2210 }
2211 2211 }
2212 2212
2213 2213 if (error != 0)
2214 2214 spdsock_error(q, mp, error, 0);
2215 2215 else
2216 2216 spd_echo(q, mp);
2217 2217 }
2218 2218
2219 2219 /*
2220 2220 * Process a SPD_ALGLIST request. The caller expects separate alg entries
2221 2221 * for AH authentication, ESP authentication, and ESP encryption.
2222 2222 * The same distinction is then used when setting the min and max key
2223 2223 * sizes when defining policies.
2224 2224 */
2225 2225
2226 2226 #define SPDSOCK_AH_AUTH 0
2227 2227 #define SPDSOCK_ESP_AUTH 1
2228 2228 #define SPDSOCK_ESP_ENCR 2
2229 2229 #define SPDSOCK_NTYPES 3
2230 2230
2231 2231 static const uint_t algattr[SPDSOCK_NTYPES] = {
2232 2232 SPD_ATTR_AH_AUTH,
2233 2233 SPD_ATTR_ESP_AUTH,
2234 2234 SPD_ATTR_ESP_ENCR
2235 2235 };
2236 2236 static const uint_t minbitsattr[SPDSOCK_NTYPES] = {
2237 2237 SPD_ATTR_AH_MINBITS,
2238 2238 SPD_ATTR_ESPA_MINBITS,
2239 2239 SPD_ATTR_ENCR_MINBITS
2240 2240 };
2241 2241 static const uint_t maxbitsattr[SPDSOCK_NTYPES] = {
2242 2242 SPD_ATTR_AH_MAXBITS,
2243 2243 SPD_ATTR_ESPA_MAXBITS,
2244 2244 SPD_ATTR_ENCR_MAXBITS
2245 2245 };
2246 2246 static const uint_t defbitsattr[SPDSOCK_NTYPES] = {
2247 2247 SPD_ATTR_AH_DEFBITS,
2248 2248 SPD_ATTR_ESPA_DEFBITS,
2249 2249 SPD_ATTR_ENCR_DEFBITS
2250 2250 };
2251 2251 static const uint_t incrbitsattr[SPDSOCK_NTYPES] = {
2252 2252 SPD_ATTR_AH_INCRBITS,
2253 2253 SPD_ATTR_ESPA_INCRBITS,
2254 2254 SPD_ATTR_ENCR_INCRBITS
2255 2255 };
2256 2256
2257 2257 #define ATTRPERALG 6 /* fixed attributes per algs */
2258 2258
2259 2259 void
2260 2260 spdsock_alglist(queue_t *q, mblk_t *mp)
2261 2261 {
2262 2262 uint_t algtype;
2263 2263 uint_t algidx;
2264 2264 uint_t algcount;
2265 2265 uint_t size;
2266 2266 mblk_t *m;
2267 2267 uint8_t *cur;
2268 2268 spd_msg_t *msg;
2269 2269 struct spd_ext_actions *act;
2270 2270 struct spd_attribute *attr;
2271 2271 spdsock_t *ss = (spdsock_t *)q->q_ptr;
2272 2272 ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
2273 2273
2274 2274 rw_enter(&ipss->ipsec_alg_lock, RW_READER);
2275 2275 /*
2276 2276 * The SPD client expects to receive separate entries for
2277 2277 * AH authentication and ESP authentication supported algorithms.
2278 2278 *
2279 2279 * Don't return the "any" algorithms, if defined, as no
2280 2280 * kernel policies can be set for these algorithms.
2281 2281 */
2282 2282 algcount = 2 * ipss->ipsec_nalgs[IPSEC_ALG_AUTH] +
2283 2283 ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
2284 2284
2285 2285 if (ipss->ipsec_alglists[IPSEC_ALG_AUTH][SADB_AALG_NONE] != NULL)
2286 2286 algcount--;
2287 2287 if (ipss->ipsec_alglists[IPSEC_ALG_ENCR][SADB_EALG_NONE] != NULL)
2288 2288 algcount--;
2289 2289
2290 2290 /*
2291 2291 * For each algorithm, we encode:
2292 2292 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
2293 2293 */
2294 2294
2295 2295 size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions) +
2296 2296 ATTRPERALG * sizeof (struct spd_attribute) * algcount;
2297 2297
2298 2298 ASSERT(ALIGNED64(size));
2299 2299
2300 2300 m = allocb(size, BPRI_HI);
2301 2301 if (m == NULL) {
2302 2302 rw_exit(&ipss->ipsec_alg_lock);
2303 2303 spdsock_error(q, mp, ENOMEM, 0);
2304 2304 return;
2305 2305 }
2306 2306
2307 2307 m->b_wptr = m->b_rptr + size;
2308 2308 cur = m->b_rptr;
2309 2309
2310 2310 msg = (spd_msg_t *)cur;
2311 2311 bcopy(mp->b_rptr, cur, sizeof (*msg));
2312 2312
2313 2313 msg->spd_msg_len = SPD_8TO64(size);
2314 2314 msg->spd_msg_errno = 0;
2315 2315 msg->spd_msg_diagnostic = 0;
2316 2316
2317 2317 cur += sizeof (*msg);
2318 2318
2319 2319 act = (struct spd_ext_actions *)cur;
2320 2320 cur += sizeof (*act);
2321 2321
2322 2322 act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t));
2323 2323 act->spd_actions_exttype = SPD_EXT_ACTION;
2324 2324 act->spd_actions_count = algcount;
2325 2325 act->spd_actions_reserved = 0;
2326 2326
2327 2327 attr = (struct spd_attribute *)cur;
2328 2328
2329 2329 #define EMIT(tag, value) { \
2330 2330 attr->spd_attr_tag = (tag); \
2331 2331 attr->spd_attr_value = (value); \
2332 2332 attr++; \
2333 2333 }
2334 2334
2335 2335 /*
2336 2336 * If you change the number of EMIT's here, change
2337 2337 * ATTRPERALG above to match
2338 2338 */
2339 2339 #define EMITALGATTRS(_type) { \
2340 2340 EMIT(algattr[_type], algid); /* 1 */ \
2341 2341 EMIT(minbitsattr[_type], minbits); /* 2 */ \
2342 2342 EMIT(maxbitsattr[_type], maxbits); /* 3 */ \
2343 2343 EMIT(defbitsattr[_type], defbits); /* 4 */ \
2344 2344 EMIT(incrbitsattr[_type], incr); /* 5 */ \
2345 2345 EMIT(SPD_ATTR_NEXT, 0); /* 6 */ \
2346 2346 }
2347 2347
2348 2348 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
2349 2349 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
2350 2350 algidx++) {
2351 2351 int algid = ipss->ipsec_sortlist[algtype][algidx];
2352 2352 ipsec_alginfo_t *alg =
2353 2353 ipss->ipsec_alglists[algtype][algid];
2354 2354 uint_t minbits = alg->alg_minbits;
2355 2355 uint_t maxbits = alg->alg_maxbits;
2356 2356 uint_t defbits = alg->alg_default_bits;
2357 2357 uint_t incr = alg->alg_increment;
2358 2358
2359 2359 if (algtype == IPSEC_ALG_AUTH) {
2360 2360 if (algid == SADB_AALG_NONE)
2361 2361 continue;
2362 2362 EMITALGATTRS(SPDSOCK_AH_AUTH);
2363 2363 EMITALGATTRS(SPDSOCK_ESP_AUTH);
2364 2364 } else {
2365 2365 if (algid == SADB_EALG_NONE)
2366 2366 continue;
2367 2367 ASSERT(algtype == IPSEC_ALG_ENCR);
2368 2368 EMITALGATTRS(SPDSOCK_ESP_ENCR);
2369 2369 }
2370 2370 }
2371 2371 }
2372 2372
2373 2373 rw_exit(&ipss->ipsec_alg_lock);
2374 2374
2375 2375 #undef EMITALGATTRS
2376 2376 #undef EMIT
2377 2377 #undef ATTRPERALG
2378 2378
2379 2379 attr--;
2380 2380 attr->spd_attr_tag = SPD_ATTR_END;
2381 2381
2382 2382 freemsg(mp);
2383 2383 qreply(q, m);
2384 2384 }
2385 2385
2386 2386 /*
2387 2387 * Process a SPD_DUMPALGS request.
2388 2388 */
2389 2389
2390 2390 #define ATTRPERALG 9 /* fixed attributes per algs */
2391 2391
2392 2392 void
2393 2393 spdsock_dumpalgs(queue_t *q, mblk_t *mp)
2394 2394 {
2395 2395 uint_t algtype;
2396 2396 uint_t algidx;
2397 2397 uint_t size;
2398 2398 mblk_t *m;
2399 2399 uint8_t *cur;
2400 2400 spd_msg_t *msg;
2401 2401 struct spd_ext_actions *act;
2402 2402 struct spd_attribute *attr;
2403 2403 ipsec_alginfo_t *alg;
2404 2404 uint_t algid;
2405 2405 uint_t i;
2406 2406 uint_t alg_size;
2407 2407 spdsock_t *ss = (spdsock_t *)q->q_ptr;
2408 2408 ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
2409 2409
2410 2410 rw_enter(&ipss->ipsec_alg_lock, RW_READER);
2411 2411
2412 2412 /*
2413 2413 * For each algorithm, we encode:
2414 2414 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
2415 2415 *
2416 2416 * ALG_ID / ALG_PROTO / ALG_INCRBITS / ALG_NKEYSIZES / ALG_KEYSIZE*
2417 2417 * ALG_NBLOCKSIZES / ALG_BLOCKSIZE* / ALG_NPARAMS / ALG_PARAMS* /
2418 2418 * ALG_MECHNAME / ALG_FLAGS / {END, NEXT}
2419 2419 */
2420 2420
2421 2421 /*
2422 2422 * Compute the size of the SPD message.
2423 2423 */
2424 2424 size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions);
2425 2425
2426 2426 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
2427 2427 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
2428 2428 algidx++) {
2429 2429 algid = ipss->ipsec_sortlist[algtype][algidx];
2430 2430 alg = ipss->ipsec_alglists[algtype][algid];
2431 2431 alg_size = sizeof (struct spd_attribute) *
2432 2432 (ATTRPERALG + alg->alg_nkey_sizes +
2433 2433 alg->alg_nblock_sizes + alg->alg_nparams) +
2434 2434 CRYPTO_MAX_MECH_NAME;
2435 2435 size += alg_size;
2436 2436 }
2437 2437 }
2438 2438
2439 2439 ASSERT(ALIGNED64(size));
2440 2440
2441 2441 m = allocb(size, BPRI_HI);
2442 2442 if (m == NULL) {
2443 2443 rw_exit(&ipss->ipsec_alg_lock);
2444 2444 spdsock_error(q, mp, ENOMEM, 0);
2445 2445 return;
2446 2446 }
2447 2447
2448 2448 m->b_wptr = m->b_rptr + size;
2449 2449 cur = m->b_rptr;
2450 2450
2451 2451 msg = (spd_msg_t *)cur;
2452 2452 bcopy(mp->b_rptr, cur, sizeof (*msg));
2453 2453
2454 2454 msg->spd_msg_len = SPD_8TO64(size);
2455 2455 msg->spd_msg_errno = 0;
2456 2456 msg->spd_msg_type = SPD_ALGLIST;
2457 2457
2458 2458 msg->spd_msg_diagnostic = 0;
2459 2459
2460 2460 cur += sizeof (*msg);
2461 2461
2462 2462 act = (struct spd_ext_actions *)cur;
2463 2463 cur += sizeof (*act);
2464 2464
2465 2465 act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t));
2466 2466 act->spd_actions_exttype = SPD_EXT_ACTION;
2467 2467 act->spd_actions_count = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] +
2468 2468 ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
2469 2469 act->spd_actions_reserved = 0;
2470 2470
2471 2471 /*
2472 2472 * If there aren't any algorithms registered, return an empty message.
2473 2473 * spdsock_get_ext() knows how to deal with this.
2474 2474 */
2475 2475 if (act->spd_actions_count == 0) {
2476 2476 act->spd_actions_len = 0;
2477 2477 rw_exit(&ipss->ipsec_alg_lock);
2478 2478 goto error;
2479 2479 }
2480 2480
2481 2481 attr = (struct spd_attribute *)cur;
2482 2482
2483 2483 #define EMIT(tag, value) { \
2484 2484 attr->spd_attr_tag = (tag); \
2485 2485 attr->spd_attr_value = (value); \
2486 2486 attr++; \
2487 2487 }
2488 2488
2489 2489 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
2490 2490 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
2491 2491 algidx++) {
2492 2492
2493 2493 algid = ipss->ipsec_sortlist[algtype][algidx];
2494 2494 alg = ipss->ipsec_alglists[algtype][algid];
2495 2495
2496 2496 /*
2497 2497 * If you change the number of EMIT's here, change
2498 2498 * ATTRPERALG above to match
2499 2499 */
2500 2500 EMIT(SPD_ATTR_ALG_ID, algid);
2501 2501 EMIT(SPD_ATTR_ALG_PROTO, algproto[algtype]);
2502 2502 EMIT(SPD_ATTR_ALG_INCRBITS, alg->alg_increment);
2503 2503 EMIT(SPD_ATTR_ALG_NKEYSIZES, alg->alg_nkey_sizes);
2504 2504 for (i = 0; i < alg->alg_nkey_sizes; i++)
2505 2505 EMIT(SPD_ATTR_ALG_KEYSIZE,
2506 2506 alg->alg_key_sizes[i]);
2507 2507
2508 2508 EMIT(SPD_ATTR_ALG_NBLOCKSIZES, alg->alg_nblock_sizes);
2509 2509 for (i = 0; i < alg->alg_nblock_sizes; i++)
2510 2510 EMIT(SPD_ATTR_ALG_BLOCKSIZE,
2511 2511 alg->alg_block_sizes[i]);
2512 2512
2513 2513 EMIT(SPD_ATTR_ALG_NPARAMS, alg->alg_nparams);
2514 2514 for (i = 0; i < alg->alg_nparams; i++)
2515 2515 EMIT(SPD_ATTR_ALG_PARAMS,
2516 2516 alg->alg_params[i]);
2517 2517
2518 2518 EMIT(SPD_ATTR_ALG_FLAGS, alg->alg_flags);
2519 2519
2520 2520 EMIT(SPD_ATTR_ALG_MECHNAME, CRYPTO_MAX_MECH_NAME);
2521 2521 bcopy(alg->alg_mech_name, attr, CRYPTO_MAX_MECH_NAME);
2522 2522 attr = (struct spd_attribute *)((char *)attr +
2523 2523 CRYPTO_MAX_MECH_NAME);
2524 2524
2525 2525 EMIT(SPD_ATTR_NEXT, 0);
2526 2526 }
2527 2527 }
2528 2528
2529 2529 rw_exit(&ipss->ipsec_alg_lock);
2530 2530
2531 2531 #undef EMITALGATTRS
2532 2532 #undef EMIT
2533 2533 #undef ATTRPERALG
2534 2534
2535 2535 attr--;
2536 2536 attr->spd_attr_tag = SPD_ATTR_END;
2537 2537
2538 2538 error:
2539 2539 freemsg(mp);
2540 2540 qreply(q, m);
2541 2541 }
2542 2542
2543 2543 /*
2544 2544 * Do the actual work of processing an SPD_UPDATEALGS request. Can
2545 2545 * be invoked either once IPsec is loaded on a cached request, or
2546 2546 * when a request is received while IPsec is loaded.
2547 2547 */
2548 2548 static int
2549 2549 spdsock_do_updatealg(spd_ext_t *extv[], spd_stack_t *spds)
2550 2550 {
2551 2551 struct spd_ext_actions *actp;
2552 2552 struct spd_attribute *attr, *endattr;
2553 2553 uint64_t *start, *end;
2554 2554 ipsec_alginfo_t *alg = NULL;
2555 2555 ipsec_algtype_t alg_type = 0;
2556 2556 boolean_t skip_alg = B_TRUE, doing_proto = B_FALSE;
2557 2557 uint_t i, cur_key, cur_block, algid;
2558 2558 int diag = -1;
2559 2559
2560 2560 ASSERT(MUTEX_HELD(&spds->spds_alg_lock));
2561 2561
2562 2562 /* parse the message, building the list of algorithms */
2563 2563
2564 2564 actp = (struct spd_ext_actions *)extv[SPD_EXT_ACTION];
2565 2565 if (actp == NULL)
2566 2566 return (SPD_DIAGNOSTIC_NO_ACTION_EXT);
2567 2567
2568 2568 start = (uint64_t *)actp;
2569 2569 end = (start + actp->spd_actions_len);
2570 2570 endattr = (struct spd_attribute *)end;
2571 2571 attr = (struct spd_attribute *)&actp[1];
2572 2572
2573 2573 bzero(spds->spds_algs, IPSEC_NALGTYPES * IPSEC_MAX_ALGS *
2574 2574 sizeof (ipsec_alginfo_t *));
2575 2575
2576 2576 alg = kmem_zalloc(sizeof (*alg), KM_SLEEP);
2577 2577
2578 2578 #define ALG_KEY_SIZES(a) (((a)->alg_nkey_sizes + 1) * sizeof (uint16_t))
2579 2579 #define ALG_BLOCK_SIZES(a) (((a)->alg_nblock_sizes + 1) * sizeof (uint16_t))
2580 2580 #define ALG_PARAM_SIZES(a) (((a)->alg_nparams + 1) * sizeof (uint16_t))
2581 2581
2582 2582 while (attr < endattr) {
2583 2583 switch (attr->spd_attr_tag) {
2584 2584 case SPD_ATTR_NOP:
2585 2585 case SPD_ATTR_EMPTY:
2586 2586 break;
2587 2587 case SPD_ATTR_END:
2588 2588 attr = endattr;
2589 2589 /* FALLTHRU */
2590 2590 case SPD_ATTR_NEXT:
2591 2591 if (doing_proto) {
2592 2592 doing_proto = B_FALSE;
2593 2593 break;
2594 2594 }
2595 2595 if (skip_alg) {
2596 2596 ipsec_alg_free(alg);
2597 2597 } else {
2598 2598 ipsec_alg_free(
2599 2599 spds->spds_algs[alg_type][alg->alg_id]);
2600 2600 spds->spds_algs[alg_type][alg->alg_id] =
2601 2601 alg;
2602 2602 }
2603 2603 alg = kmem_zalloc(sizeof (*alg), KM_SLEEP);
2604 2604 break;
2605 2605
2606 2606 case SPD_ATTR_ALG_ID:
2607 2607 if (attr->spd_attr_value >= IPSEC_MAX_ALGS) {
2608 2608 ss1dbg(spds, ("spdsock_do_updatealg: "
2609 2609 "invalid alg id %d\n",
2610 2610 attr->spd_attr_value));
2611 2611 diag = SPD_DIAGNOSTIC_ALG_ID_RANGE;
2612 2612 goto bail;
2613 2613 }
2614 2614 alg->alg_id = attr->spd_attr_value;
2615 2615 break;
2616 2616
2617 2617 case SPD_ATTR_ALG_PROTO:
2618 2618 /* find the alg type */
2619 2619 for (i = 0; i < NALGPROTOS; i++)
2620 2620 if (algproto[i] == attr->spd_attr_value)
2621 2621 break;
2622 2622 skip_alg = (i == NALGPROTOS);
2623 2623 if (!skip_alg)
2624 2624 alg_type = i;
2625 2625 break;
2626 2626
2627 2627 case SPD_ATTR_ALG_INCRBITS:
2628 2628 alg->alg_increment = attr->spd_attr_value;
2629 2629 break;
2630 2630
2631 2631 case SPD_ATTR_ALG_NKEYSIZES:
2632 2632 if (alg->alg_key_sizes != NULL) {
2633 2633 kmem_free(alg->alg_key_sizes,
2634 2634 ALG_KEY_SIZES(alg));
2635 2635 }
2636 2636 alg->alg_nkey_sizes = attr->spd_attr_value;
2637 2637 /*
2638 2638 * Allocate room for the trailing zero key size
2639 2639 * value as well.
2640 2640 */
2641 2641 alg->alg_key_sizes = kmem_zalloc(ALG_KEY_SIZES(alg),
2642 2642 KM_SLEEP);
2643 2643 cur_key = 0;
2644 2644 break;
2645 2645
2646 2646 case SPD_ATTR_ALG_KEYSIZE:
2647 2647 if (alg->alg_key_sizes == NULL ||
2648 2648 cur_key >= alg->alg_nkey_sizes) {
2649 2649 ss1dbg(spds, ("spdsock_do_updatealg: "
2650 2650 "too many key sizes\n"));
2651 2651 diag = SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES;
2652 2652 goto bail;
2653 2653 }
2654 2654 alg->alg_key_sizes[cur_key++] = attr->spd_attr_value;
2655 2655 break;
2656 2656
2657 2657 case SPD_ATTR_ALG_FLAGS:
2658 2658 /*
2659 2659 * Flags (bit mask). The alg_flags element of
2660 2660 * ipsecalg_flags_t is only 8 bits wide. The
2661 2661 * user can set the VALID bit, but we will ignore it
2662 2662 * and make the decision is the algorithm is valid.
2663 2663 */
2664 2664 alg->alg_flags |= (uint8_t)attr->spd_attr_value;
2665 2665 break;
2666 2666
2667 2667 case SPD_ATTR_ALG_NBLOCKSIZES:
2668 2668 if (alg->alg_block_sizes != NULL) {
2669 2669 kmem_free(alg->alg_block_sizes,
2670 2670 ALG_BLOCK_SIZES(alg));
2671 2671 }
2672 2672 alg->alg_nblock_sizes = attr->spd_attr_value;
2673 2673 /*
2674 2674 * Allocate room for the trailing zero block size
2675 2675 * value as well.
2676 2676 */
2677 2677 alg->alg_block_sizes = kmem_zalloc(ALG_BLOCK_SIZES(alg),
2678 2678 KM_SLEEP);
2679 2679 cur_block = 0;
2680 2680 break;
2681 2681
2682 2682 case SPD_ATTR_ALG_BLOCKSIZE:
2683 2683 if (alg->alg_block_sizes == NULL ||
2684 2684 cur_block >= alg->alg_nblock_sizes) {
2685 2685 ss1dbg(spds, ("spdsock_do_updatealg: "
2686 2686 "too many block sizes\n"));
2687 2687 diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES;
2688 2688 goto bail;
2689 2689 }
2690 2690 alg->alg_block_sizes[cur_block++] =
2691 2691 attr->spd_attr_value;
2692 2692 break;
2693 2693
2694 2694 case SPD_ATTR_ALG_NPARAMS:
2695 2695 if (alg->alg_params != NULL) {
2696 2696 kmem_free(alg->alg_params,
2697 2697 ALG_PARAM_SIZES(alg));
2698 2698 }
2699 2699 alg->alg_nparams = attr->spd_attr_value;
2700 2700 /*
2701 2701 * Allocate room for the trailing zero block size
2702 2702 * value as well.
2703 2703 */
2704 2704 alg->alg_params = kmem_zalloc(ALG_PARAM_SIZES(alg),
2705 2705 KM_SLEEP);
2706 2706 cur_block = 0;
2707 2707 break;
2708 2708
2709 2709 case SPD_ATTR_ALG_PARAMS:
2710 2710 if (alg->alg_params == NULL ||
2711 2711 cur_block >= alg->alg_nparams) {
2712 2712 ss1dbg(spds, ("spdsock_do_updatealg: "
2713 2713 "too many params\n"));
2714 2714 diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES;
2715 2715 goto bail;
2716 2716 }
2717 2717 /*
2718 2718 * Array contains: iv_len, icv_len, salt_len
2719 2719 * Any additional parameters are currently ignored.
2720 2720 */
2721 2721 alg->alg_params[cur_block++] =
2722 2722 attr->spd_attr_value;
2723 2723 break;
2724 2724
2725 2725 case SPD_ATTR_ALG_MECHNAME: {
2726 2726 char *mech_name;
2727 2727
2728 2728 if (attr->spd_attr_value > CRYPTO_MAX_MECH_NAME) {
2729 2729 ss1dbg(spds, ("spdsock_do_updatealg: "
2730 2730 "mech name too long\n"));
2731 2731 diag = SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN;
2732 2732 goto bail;
2733 2733 }
2734 2734 mech_name = (char *)(attr + 1);
2735 2735 bcopy(mech_name, alg->alg_mech_name,
2736 2736 attr->spd_attr_value);
2737 2737 alg->alg_mech_name[CRYPTO_MAX_MECH_NAME-1] = '\0';
2738 2738 attr = (struct spd_attribute *)((char *)attr +
2739 2739 attr->spd_attr_value);
2740 2740 break;
2741 2741 }
2742 2742
2743 2743 case SPD_ATTR_PROTO_ID:
2744 2744 doing_proto = B_TRUE;
2745 2745 for (i = 0; i < NALGPROTOS; i++) {
2746 2746 if (algproto[i] == attr->spd_attr_value) {
2747 2747 alg_type = i;
2748 2748 break;
2749 2749 }
2750 2750 }
2751 2751 break;
2752 2752
2753 2753 case SPD_ATTR_PROTO_EXEC_MODE:
2754 2754 if (!doing_proto)
2755 2755 break;
2756 2756 for (i = 0; i < NEXECMODES; i++) {
2757 2757 if (execmodes[i] == attr->spd_attr_value) {
2758 2758 spds->spds_algs_exec_mode[alg_type] = i;
2759 2759 break;
2760 2760 }
2761 2761 }
2762 2762 break;
2763 2763 }
2764 2764 attr++;
2765 2765 }
2766 2766
2767 2767 #undef ALG_KEY_SIZES
2768 2768 #undef ALG_BLOCK_SIZES
2769 2769 #undef ALG_PARAM_SIZES
2770 2770
2771 2771 /* update the algorithm tables */
2772 2772 spdsock_merge_algs(spds);
2773 2773 bail:
2774 2774 /* cleanup */
2775 2775 ipsec_alg_free(alg);
2776 2776 for (alg_type = 0; alg_type < IPSEC_NALGTYPES; alg_type++)
2777 2777 for (algid = 0; algid < IPSEC_MAX_ALGS; algid++)
2778 2778 if (spds->spds_algs[alg_type][algid] != NULL)
2779 2779 ipsec_alg_free(spds->spds_algs[alg_type][algid]);
2780 2780 return (diag);
2781 2781 }
2782 2782
2783 2783 /*
2784 2784 * Process an SPD_UPDATEALGS request. If IPsec is not loaded, queue
2785 2785 * the request until IPsec loads. If IPsec is loaded, act on it
2786 2786 * immediately.
2787 2787 */
2788 2788
2789 2789 static void
2790 2790 spdsock_updatealg(queue_t *q, mblk_t *mp, spd_ext_t *extv[])
2791 2791 {
2792 2792 spdsock_t *ss = (spdsock_t *)q->q_ptr;
2793 2793 spd_stack_t *spds = ss->spdsock_spds;
2794 2794 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec;
2795 2795 uint32_t auditing = AU_AUDITING();
2796 2796
2797 2797 if (!ipsec_loaded(ipss)) {
2798 2798 /*
2799 2799 * IPsec is not loaded, save request and return nicely,
2800 2800 * the message will be processed once IPsec loads.
2801 2801 */
2802 2802 mblk_t *new_mp;
2803 2803
2804 2804 /* last update message wins */
2805 2805 if ((new_mp = copymsg(mp)) == NULL) {
2806 2806 spdsock_error(q, mp, ENOMEM, 0);
2807 2807 return;
2808 2808 }
2809 2809 mutex_enter(&spds->spds_alg_lock);
2810 2810 bcopy(extv, spds->spds_extv_algs,
2811 2811 sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1));
2812 2812 if (spds->spds_mp_algs != NULL)
2813 2813 freemsg(spds->spds_mp_algs);
2814 2814 spds->spds_mp_algs = mp;
2815 2815 mutex_exit(&spds->spds_alg_lock);
2816 2816 if (auditing) {
2817 2817 cred_t *cr;
2818 2818 pid_t cpid;
2819 2819
2820 2820 cr = msg_getcred(mp, &cpid);
2821 2821 audit_pf_policy(SPD_UPDATEALGS, cr,
2822 2822 spds->spds_netstack, NULL, B_TRUE, EAGAIN,
2823 2823 cpid);
2824 2824 }
2825 2825 spd_echo(q, new_mp);
2826 2826 } else {
2827 2827 /*
2828 2828 * IPsec is loaded, act on the message immediately.
2829 2829 */
2830 2830 int diag;
2831 2831
2832 2832 mutex_enter(&spds->spds_alg_lock);
2833 2833 diag = spdsock_do_updatealg(extv, spds);
2834 2834 if (diag == -1) {
2835 2835 /* Keep the lock held while we walk the SA tables. */
2836 2836 sadb_alg_update(IPSEC_ALG_ALL, 0, 0,
2837 2837 spds->spds_netstack);
2838 2838 mutex_exit(&spds->spds_alg_lock);
2839 2839 spd_echo(q, mp);
2840 2840 if (auditing) {
2841 2841 cred_t *cr;
2842 2842 pid_t cpid;
2843 2843
2844 2844 cr = msg_getcred(mp, &cpid);
2845 2845 audit_pf_policy(SPD_UPDATEALGS, cr,
2846 2846 spds->spds_netstack, NULL, B_TRUE, 0,
2847 2847 cpid);
2848 2848 }
2849 2849 } else {
2850 2850 mutex_exit(&spds->spds_alg_lock);
2851 2851 spdsock_diag(q, mp, diag);
2852 2852 if (auditing) {
2853 2853 cred_t *cr;
2854 2854 pid_t cpid;
2855 2855
2856 2856 cr = msg_getcred(mp, &cpid);
2857 2857 audit_pf_policy(SPD_UPDATEALGS, cr,
2858 2858 spds->spds_netstack, NULL, B_TRUE, diag,
2859 2859 cpid);
2860 2860 }
2861 2861 }
2862 2862 }
2863 2863 }
2864 2864
2865 2865 /*
2866 2866 * Find a tunnel instance (using the name to link ID mapping), and
2867 2867 * update it after an IPsec change. We need to do this always in case
2868 2868 * we add policy AFTER plumbing a tunnel. We also need to do this
2869 2869 * because, as a side-effect, the tunnel's MTU is updated to reflect
2870 2870 * any IPsec overhead in the itp's policy.
2871 2871 */
2872 2872 static void
2873 2873 update_iptun_policy(ipsec_tun_pol_t *itp)
2874 2874 {
2875 2875 datalink_id_t linkid;
2876 2876
2877 2877 if (dls_mgmt_get_linkid(itp->itp_name, &linkid) == 0)
2878 2878 iptun_set_policy(linkid, itp);
2879 2879 }
2880 2880
2881 2881 /*
2882 2882 * Sort through the mess of polhead options to retrieve an appropriate one.
2883 2883 * Returns NULL if we send an spdsock error. Returns a valid pointer if we
2884 2884 * found a valid polhead. Returns ALL_ACTIVE_POLHEADS (aka. -1) or
2885 2885 * ALL_INACTIVE_POLHEADS (aka. -2) if the operation calls for the operation to
2886 2886 * act on ALL policy heads.
2887 2887 */
2888 2888 static ipsec_policy_head_t *
2889 2889 get_appropriate_polhead(queue_t *q, mblk_t *mp, spd_if_t *tunname, int spdid,
2890 2890 int msgtype, ipsec_tun_pol_t **itpp)
2891 2891 {
2892 2892 ipsec_tun_pol_t *itp;
2893 2893 ipsec_policy_head_t *iph;
2894 2894 int errno;
2895 2895 char *tname;
2896 2896 boolean_t active;
2897 2897 spdsock_t *ss = (spdsock_t *)q->q_ptr;
2898 2898 netstack_t *ns = ss->spdsock_spds->spds_netstack;
2899 2899 uint64_t gen; /* Placeholder */
2900 2900
2901 2901 active = (spdid == SPD_ACTIVE);
2902 2902 *itpp = NULL;
2903 2903 if (!active && spdid != SPD_STANDBY) {
2904 2904 spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_SPDID);
2905 2905 return (NULL);
2906 2906 }
2907 2907
2908 2908 if (tunname != NULL) {
2909 2909 /* Acting on a tunnel's SPD. */
2910 2910 tname = (char *)tunname->spd_if_name;
2911 2911 if (*tname == '\0') {
2912 2912 /* Handle all-polhead cases here. */
2913 2913 if (msgtype != SPD_FLUSH && msgtype != SPD_DUMP) {
2914 2914 spdsock_diag(q, mp,
2915 2915 SPD_DIAGNOSTIC_NOT_GLOBAL_OP);
2916 2916 return (NULL);
2917 2917 }
2918 2918 return (active ? ALL_ACTIVE_POLHEADS :
2919 2919 ALL_INACTIVE_POLHEADS);
2920 2920 }
2921 2921
2922 2922 itp = get_tunnel_policy(tname, ns);
2923 2923 if (itp == NULL) {
2924 2924 if (msgtype != SPD_ADDRULE) {
2925 2925 /* "Tunnel not found" */
2926 2926 spdsock_error(q, mp, ENOENT, 0);
2927 2927 return (NULL);
2928 2928 }
2929 2929
2930 2930 errno = 0;
2931 2931 itp = create_tunnel_policy(tname, &errno, &gen, ns);
2932 2932 if (itp == NULL) {
2933 2933 /*
2934 2934 * Something very bad happened, most likely
2935 2935 * ENOMEM. Return an indicator.
2936 2936 */
2937 2937 spdsock_error(q, mp, errno, 0);
2938 2938 return (NULL);
2939 2939 }
2940 2940 }
2941 2941
2942 2942 /* Match up the itp to an iptun instance. */
2943 2943 update_iptun_policy(itp);
2944 2944
2945 2945 *itpp = itp;
2946 2946 /* For spdsock dump state, set the polhead's name. */
2947 2947 if (msgtype == SPD_DUMP) {
2948 2948 ITP_REFHOLD(itp);
2949 2949 ss->spdsock_itp = itp;
2950 2950 ss->spdsock_dump_tunnel = itp->itp_flags &
2951 2951 (active ? ITPF_P_TUNNEL : ITPF_I_TUNNEL);
2952 2952 }
2953 2953 } else {
2954 2954 itp = NULL;
2955 2955 /* For spdsock dump state, indicate it's global policy. */
2956 2956 if (msgtype == SPD_DUMP)
2957 2957 ss->spdsock_itp = NULL;
2958 2958 }
2959 2959
2960 2960 if (active)
2961 2961 iph = (itp == NULL) ? ipsec_system_policy(ns) : itp->itp_policy;
2962 2962 else
2963 2963 iph = (itp == NULL) ? ipsec_inactive_policy(ns) :
2964 2964 itp->itp_inactive;
2965 2965
2966 2966 ASSERT(iph != NULL);
2967 2967 if (itp != NULL) {
2968 2968 IPPH_REFHOLD(iph);
2969 2969 }
2970 2970
2971 2971 return (iph);
2972 2972 }
2973 2973
2974 2974 static void
2975 2975 spdsock_parse(queue_t *q, mblk_t *mp)
2976 2976 {
2977 2977 spd_msg_t *spmsg;
2978 2978 spd_ext_t *extv[SPD_EXT_MAX + 1];
2979 2979 uint_t msgsize;
2980 2980 ipsec_policy_head_t *iph;
2981 2981 ipsec_tun_pol_t *itp;
2982 2982 spd_if_t *tunname;
2983 2983 spdsock_t *ss = (spdsock_t *)q->q_ptr;
2984 2984 spd_stack_t *spds = ss->spdsock_spds;
2985 2985 netstack_t *ns = spds->spds_netstack;
2986 2986 ipsec_stack_t *ipss = ns->netstack_ipsec;
2987 2987
2988 2988 /* Make sure nothing's below me. */
2989 2989 ASSERT(WR(q)->q_next == NULL);
2990 2990
2991 2991 spmsg = (spd_msg_t *)mp->b_rptr;
2992 2992
2993 2993 msgsize = SPD_64TO8(spmsg->spd_msg_len);
2994 2994
2995 2995 if (msgdsize(mp) != msgsize) {
2996 2996 /*
2997 2997 * Message len incorrect w.r.t. actual size. Send an error
2998 2998 * (EMSGSIZE). It may be necessary to massage things a
2999 2999 * bit. For example, if the spd_msg_type is hosed,
3000 3000 * I need to set it to SPD_RESERVED to get delivery to
3001 3001 * do the right thing. Then again, maybe just letting
3002 3002 * the error delivery do the right thing.
3003 3003 */
3004 3004 ss2dbg(spds,
3005 3005 ("mblk (%lu) and base (%d) message sizes don't jibe.\n",
3006 3006 msgdsize(mp), msgsize));
3007 3007 spdsock_error(q, mp, EMSGSIZE, SPD_DIAGNOSTIC_NONE);
3008 3008 return;
3009 3009 }
3010 3010
3011 3011 if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) {
3012 3012 /* Get all message into one mblk. */
3013 3013 if (pullupmsg(mp, -1) == 0) {
3014 3014 /*
3015 3015 * Something screwy happened.
3016 3016 */
3017 3017 ss3dbg(spds, ("spdsock_parse: pullupmsg() failed.\n"));
3018 3018 return;
3019 3019 } else {
3020 3020 spmsg = (spd_msg_t *)mp->b_rptr;
3021 3021 }
3022 3022 }
3023 3023
3024 3024 switch (spdsock_get_ext(extv, spmsg, msgsize)) {
3025 3025 case KGE_DUP:
3026 3026 /* Handle duplicate extension. */
3027 3027 ss1dbg(spds, ("Got duplicate extension of type %d.\n",
3028 3028 extv[0]->spd_ext_type));
3029 3029 spdsock_diag(q, mp, dup_ext_diag[extv[0]->spd_ext_type]);
3030 3030 return;
3031 3031 case KGE_UNK:
3032 3032 /* Handle unknown extension. */
3033 3033 ss1dbg(spds, ("Got unknown extension of type %d.\n",
3034 3034 extv[0]->spd_ext_type));
3035 3035 spdsock_diag(q, mp, SPD_DIAGNOSTIC_UNKNOWN_EXT);
3036 3036 return;
3037 3037 case KGE_LEN:
3038 3038 /* Length error. */
3039 3039 ss1dbg(spds, ("Length %d on extension type %d overrun or 0.\n",
3040 3040 extv[0]->spd_ext_len, extv[0]->spd_ext_type));
3041 3041 spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_EXTLEN);
3042 3042 return;
3043 3043 case KGE_CHK:
3044 3044 /* Reality check failed. */
3045 3045 ss1dbg(spds, ("Reality check failed on extension type %d.\n",
3046 3046 extv[0]->spd_ext_type));
3047 3047 spdsock_diag(q, mp, bad_ext_diag[extv[0]->spd_ext_type]);
3048 3048 return;
3049 3049 default:
3050 3050 /* Default case is no errors. */
3051 3051 break;
3052 3052 }
3053 3053
3054 3054 /*
3055 3055 * Special-case SPD_UPDATEALGS so as not to load IPsec.
3056 3056 */
3057 3057 if (!ipsec_loaded(ipss) && spmsg->spd_msg_type != SPD_UPDATEALGS) {
3058 3058 spdsock_t *ss = (spdsock_t *)q->q_ptr;
3059 3059
3060 3060 ASSERT(ss != NULL);
3061 3061 ipsec_loader_loadnow(ipss);
3062 3062 ss->spdsock_timeout_arg = mp;
3063 3063 ss->spdsock_timeout = qtimeout(q, spdsock_loadcheck,
3064 3064 q, LOADCHECK_INTERVAL);
3065 3065 return;
3066 3066 }
3067 3067
3068 3068 /* First check for messages that need no polheads at all. */
3069 3069 switch (spmsg->spd_msg_type) {
3070 3070 case SPD_UPDATEALGS:
3071 3071 spdsock_updatealg(q, mp, extv);
3072 3072 return;
3073 3073 case SPD_ALGLIST:
3074 3074 spdsock_alglist(q, mp);
3075 3075 return;
3076 3076 case SPD_DUMPALGS:
3077 3077 spdsock_dumpalgs(q, mp);
3078 3078 return;
3079 3079 }
3080 3080
3081 3081 /*
3082 3082 * Then check for ones that need both primary/secondary polheads,
3083 3083 * finding the appropriate tunnel policy if need be.
3084 3084 */
3085 3085 tunname = (spd_if_t *)extv[SPD_EXT_TUN_NAME];
3086 3086 switch (spmsg->spd_msg_type) {
3087 3087 case SPD_FLIP:
3088 3088 spdsock_flip(q, mp, tunname);
3089 3089 return;
3090 3090 case SPD_CLONE:
3091 3091 spdsock_clone(q, mp, tunname);
3092 3092 return;
3093 3093 }
3094 3094
3095 3095 /*
3096 3096 * Finally, find ones that operate on exactly one polhead, or
3097 3097 * "all polheads" of a given type (active/inactive).
3098 3098 */
3099 3099 iph = get_appropriate_polhead(q, mp, tunname, spmsg->spd_msg_spdid,
3100 3100 spmsg->spd_msg_type, &itp);
3101 3101 if (iph == NULL)
3102 3102 return;
3103 3103
3104 3104 /* All-polheads-ready operations. */
3105 3105 switch (spmsg->spd_msg_type) {
3106 3106 case SPD_FLUSH:
3107 3107 if (itp != NULL) {
3108 3108 mutex_enter(&itp->itp_lock);
3109 3109 if (spmsg->spd_msg_spdid == SPD_ACTIVE)
3110 3110 itp->itp_flags &= ~ITPF_PFLAGS;
3111 3111 else
3112 3112 itp->itp_flags &= ~ITPF_IFLAGS;
3113 3113 mutex_exit(&itp->itp_lock);
3114 3114 }
3115 3115
3116 3116 spdsock_flush(q, iph, itp, mp);
3117 3117
3118 3118 if (itp != NULL) {
3119 3119 /* SPD_FLUSH is worth a tunnel MTU check. */
3120 3120 update_iptun_policy(itp);
3121 3121 ITP_REFRELE(itp, ns);
3122 3122 }
3123 3123 return;
3124 3124 case SPD_DUMP:
3125 3125 if (itp != NULL)
3126 3126 ITP_REFRELE(itp, ns);
3127 3127 spdsock_dump(q, iph, mp);
3128 3128 return;
3129 3129 }
3130 3130
3131 3131 if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) {
3132 3132 spdsock_diag(q, mp, SPD_DIAGNOSTIC_NOT_GLOBAL_OP);
3133 3133 return;
3134 3134 }
3135 3135
3136 3136 /* Single-polhead-only operations. */
3137 3137 switch (spmsg->spd_msg_type) {
3138 3138 case SPD_ADDRULE:
3139 3139 spdsock_addrule(q, iph, mp, extv, itp);
3140 3140 break;
3141 3141 case SPD_DELETERULE:
3142 3142 spdsock_deleterule(q, iph, mp, extv, itp);
3143 3143 break;
3144 3144 case SPD_LOOKUP:
3145 3145 spdsock_lookup(q, iph, mp, extv, itp);
3146 3146 break;
3147 3147 default:
3148 3148 spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_MSG_TYPE);
3149 3149 break;
3150 3150 }
3151 3151
3152 3152 IPPH_REFRELE(iph, ns);
3153 3153 if (itp != NULL) {
3154 3154 /* SPD_{ADD,DELETE}RULE are worth a tunnel MTU check. */
3155 3155 if (spmsg->spd_msg_type == SPD_ADDRULE ||
3156 3156 spmsg->spd_msg_type == SPD_DELETERULE)
3157 3157 update_iptun_policy(itp);
3158 3158 ITP_REFRELE(itp, ns);
3159 3159 }
3160 3160 }
3161 3161
3162 3162 /*
3163 3163 * If an algorithm mapping was received before IPsec was loaded, process it.
3164 3164 * Called from the IPsec loader.
3165 3165 */
3166 3166 void
3167 3167 spdsock_update_pending_algs(netstack_t *ns)
3168 3168 {
3169 3169 spd_stack_t *spds = ns->netstack_spdsock;
3170 3170
3171 3171 mutex_enter(&spds->spds_alg_lock);
3172 3172 if (spds->spds_mp_algs != NULL) {
3173 3173 (void) spdsock_do_updatealg(spds->spds_extv_algs, spds);
3174 3174 freemsg(spds->spds_mp_algs);
3175 3175 spds->spds_mp_algs = NULL;
3176 3176 }
3177 3177 mutex_exit(&spds->spds_alg_lock);
3178 3178 }
3179 3179
3180 3180 static void
3181 3181 spdsock_loadcheck(void *arg)
3182 3182 {
3183 3183 queue_t *q = (queue_t *)arg;
3184 3184 spdsock_t *ss = (spdsock_t *)q->q_ptr;
3185 3185 mblk_t *mp;
3186 3186 ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
3187 3187
3188 3188 ASSERT(ss != NULL);
3189 3189
3190 3190 ss->spdsock_timeout = 0;
3191 3191 mp = ss->spdsock_timeout_arg;
3192 3192 ASSERT(mp != NULL);
3193 3193 ss->spdsock_timeout_arg = NULL;
3194 3194 if (ipsec_failed(ipss))
3195 3195 spdsock_error(q, mp, EPROTONOSUPPORT, 0);
3196 3196 else
3197 3197 spdsock_parse(q, mp);
3198 3198 }
3199 3199
3200 3200 /*
3201 3201 * Copy relevant state bits.
3202 3202 */
3203 3203 static void
3204 3204 spdsock_copy_info(struct T_info_ack *tap, spdsock_t *ss)
3205 3205 {
3206 3206 *tap = spdsock_g_t_info_ack;
3207 3207 tap->CURRENT_state = ss->spdsock_state;
3208 3208 tap->OPT_size = spdsock_max_optsize;
3209 3209 }
3210 3210
3211 3211 /*
3212 3212 * This routine responds to T_CAPABILITY_REQ messages. It is called by
3213 3213 * spdsock_wput. Much of the T_CAPABILITY_ACK information is copied from
3214 3214 * spdsock_g_t_info_ack. The current state of the stream is copied from
3215 3215 * spdsock_state.
3216 3216 */
3217 3217 static void
3218 3218 spdsock_capability_req(queue_t *q, mblk_t *mp)
3219 3219 {
3220 3220 spdsock_t *ss = (spdsock_t *)q->q_ptr;
3221 3221 t_uscalar_t cap_bits1;
3222 3222 struct T_capability_ack *tcap;
3223 3223
3224 3224 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
3225 3225
3226 3226 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
3227 3227 mp->b_datap->db_type, T_CAPABILITY_ACK);
3228 3228 if (mp == NULL)
3229 3229 return;
3230 3230
3231 3231 tcap = (struct T_capability_ack *)mp->b_rptr;
3232 3232 tcap->CAP_bits1 = 0;
3233 3233
3234 3234 if (cap_bits1 & TC1_INFO) {
3235 3235 spdsock_copy_info(&tcap->INFO_ack, ss);
3236 3236 tcap->CAP_bits1 |= TC1_INFO;
3237 3237 }
3238 3238
3239 3239 qreply(q, mp);
3240 3240 }
3241 3241
3242 3242 /*
3243 3243 * This routine responds to T_INFO_REQ messages. It is called by
3244 3244 * spdsock_wput_other.
3245 3245 * Most of the T_INFO_ACK information is copied from spdsock_g_t_info_ack.
3246 3246 * The current state of the stream is copied from spdsock_state.
3247 3247 */
3248 3248 static void
3249 3249 spdsock_info_req(
3250 3250 queue_t *q,
3251 3251 mblk_t *mp)
3252 3252 {
3253 3253 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
3254 3254 T_INFO_ACK);
3255 3255 if (mp == NULL)
3256 3256 return;
3257 3257 spdsock_copy_info((struct T_info_ack *)mp->b_rptr,
3258 3258 (spdsock_t *)q->q_ptr);
3259 3259 qreply(q, mp);
3260 3260 }
3261 3261
3262 3262 /*
3263 3263 * spdsock_err_ack. This routine creates a
3264 3264 * T_ERROR_ACK message and passes it
3265 3265 * upstream.
3266 3266 */
3267 3267 static void
3268 3268 spdsock_err_ack(
3269 3269 queue_t *q,
3270 3270 mblk_t *mp,
3271 3271 int t_error,
3272 3272 int sys_error)
3273 3273 {
3274 3274 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
3275 3275 qreply(q, mp);
3276 3276 }
3277 3277
3278 3278 /*
3279 3279 * This routine retrieves the current status of socket options.
3280 3280 * It returns the size of the option retrieved.
3281 3281 */
3282 3282 /* ARGSUSED */
3283 3283 int
3284 3284 spdsock_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
3285 3285 {
3286 3286 int *i1 = (int *)ptr;
3287 3287
3288 3288 switch (level) {
3289 3289 case SOL_SOCKET:
3290 3290 switch (name) {
3291 3291 case SO_TYPE:
3292 3292 *i1 = SOCK_RAW;
3293 3293 break;
3294 3294 /*
3295 3295 * The following two items can be manipulated,
3296 3296 * but changing them should do nothing.
3297 3297 */
3298 3298 case SO_SNDBUF:
3299 3299 *i1 = (int)q->q_hiwat;
3300 3300 break;
3301 3301 case SO_RCVBUF:
3302 3302 *i1 = (int)(RD(q)->q_hiwat);
3303 3303 break;
3304 3304 }
3305 3305 break;
3306 3306 default:
3307 3307 return (0);
3308 3308 }
3309 3309 return (sizeof (int));
3310 3310 }
3311 3311
3312 3312 /*
3313 3313 * This routine sets socket options.
3314 3314 */
3315 3315 /* ARGSUSED */
3316 3316 int
3317 3317 spdsock_opt_set(queue_t *q, uint_t mgmt_flags, int level, int name,
3318 3318 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
3319 3319 void *thisdg_attrs, cred_t *cr)
3320 3320 {
3321 3321 int *i1 = (int *)invalp;
3322 3322 spdsock_t *ss = (spdsock_t *)q->q_ptr;
3323 3323 spd_stack_t *spds = ss->spdsock_spds;
3324 3324
3325 3325 switch (level) {
3326 3326 case SOL_SOCKET:
3327 3327 switch (name) {
3328 3328 case SO_SNDBUF:
3329 3329 if (*i1 > spds->spds_max_buf)
3330 3330 return (ENOBUFS);
3331 3331 q->q_hiwat = *i1;
3332 3332 break;
3333 3333 case SO_RCVBUF:
3334 3334 if (*i1 > spds->spds_max_buf)
3335 3335 return (ENOBUFS);
3336 3336 RD(q)->q_hiwat = *i1;
3337 3337 (void) proto_set_rx_hiwat(RD(q), NULL, *i1);
3338 3338 break;
3339 3339 }
3340 3340 break;
3341 3341 }
3342 3342 return (0);
3343 3343 }
3344 3344
3345 3345
3346 3346 /*
3347 3347 * Handle STREAMS messages.
3348 3348 */
3349 3349 static void
3350 3350 spdsock_wput_other(queue_t *q, mblk_t *mp)
3351 3351 {
3352 3352 struct iocblk *iocp;
3353 3353 int error;
3354 3354 spdsock_t *ss = (spdsock_t *)q->q_ptr;
3355 3355 spd_stack_t *spds = ss->spdsock_spds;
3356 3356 cred_t *cr;
3357 3357
3358 3358 switch (mp->b_datap->db_type) {
3359 3359 case M_PROTO:
3360 3360 case M_PCPROTO:
3361 3361 if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) {
3362 3362 ss3dbg(spds, (
3363 3363 "spdsock_wput_other: Not big enough M_PROTO\n"));
3364 3364 freemsg(mp);
3365 3365 return;
3366 3366 }
3367 3367 switch (((union T_primitives *)mp->b_rptr)->type) {
3368 3368 case T_CAPABILITY_REQ:
3369 3369 spdsock_capability_req(q, mp);
3370 3370 break;
3371 3371 case T_INFO_REQ:
3372 3372 spdsock_info_req(q, mp);
3373 3373 break;
3374 3374 case T_SVR4_OPTMGMT_REQ:
3375 3375 case T_OPTMGMT_REQ:
3376 3376 /*
3377 3377 * All Solaris components should pass a db_credp
3378 3378 * for this TPI message, hence we ASSERT.
3379 3379 * But in case there is some other M_PROTO that looks
3380 3380 * like a TPI message sent by some other kernel
3381 3381 * component, we check and return an error.
3382 3382 */
3383 3383 cr = msg_getcred(mp, NULL);
3384 3384 ASSERT(cr != NULL);
3385 3385 if (cr == NULL) {
3386 3386 spdsock_err_ack(q, mp, TSYSERR, EINVAL);
3387 3387 return;
3388 3388 }
3389 3389 if (((union T_primitives *)mp->b_rptr)->type ==
3390 3390 T_SVR4_OPTMGMT_REQ) {
3391 3391 svr4_optcom_req(q, mp, cr, &spdsock_opt_obj);
3392 3392 } else {
3393 3393 tpi_optcom_req(q, mp, cr, &spdsock_opt_obj);
3394 3394 }
3395 3395 break;
3396 3396 case T_DATA_REQ:
3397 3397 case T_EXDATA_REQ:
3398 3398 case T_ORDREL_REQ:
3399 3399 /* Illegal for spdsock. */
3400 3400 freemsg(mp);
3401 3401 (void) putnextctl1(RD(q), M_ERROR, EPROTO);
3402 3402 break;
3403 3403 default:
3404 3404 /* Not supported by spdsock. */
3405 3405 spdsock_err_ack(q, mp, TNOTSUPPORT, 0);
3406 3406 break;
3407 3407 }
3408 3408 return;
3409 3409 case M_IOCDATA:
3410 3410 keysock_spdsock_wput_iocdata(q, mp, PF_POLICY);
3411 3411 return;
3412 3412 case M_IOCTL:
3413 3413 iocp = (struct iocblk *)mp->b_rptr;
3414 3414 error = EINVAL;
3415 3415
3416 3416 switch (iocp->ioc_cmd) {
3417 3417 case TI_GETMYNAME:
3418 3418 case TI_GETPEERNAME:
3419 3419 /*
3420 3420 * For pfiles(1) observability with getsockname().
3421 3421 * See keysock_spdsock_wput_iocdata() for the rest of
3422 3422 * this.
3423 3423 */
3424 3424 mi_copyin(q, mp, NULL,
3425 3425 SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
3426 3426 return;
3427 3427 case ND_SET:
3428 3428 case ND_GET:
3429 3429 if (nd_getset(q, spds->spds_g_nd, mp)) {
3430 3430 qreply(q, mp);
3431 3431 return;
3432 3432 } else
3433 3433 error = ENOENT;
3434 3434 /* FALLTHRU */
3435 3435 default:
3436 3436 miocnak(q, mp, 0, error);
3437 3437 return;
3438 3438 }
3439 3439 case M_FLUSH:
3440 3440 if (*mp->b_rptr & FLUSHW) {
3441 3441 flushq(q, FLUSHALL);
3442 3442 *mp->b_rptr &= ~FLUSHW;
3443 3443 }
3444 3444 if (*mp->b_rptr & FLUSHR) {
3445 3445 qreply(q, mp);
3446 3446 return;
3447 3447 }
3448 3448 /* Else FALLTHRU */
3449 3449 }
3450 3450
3451 3451 /* If fell through, just black-hole the message. */
3452 3452 freemsg(mp);
3453 3453 }
3454 3454
3455 3455 static int
3456 3456 spdsock_wput(queue_t *q, mblk_t *mp)
3457 3457 {
3458 3458 uint8_t *rptr = mp->b_rptr;
3459 3459 mblk_t *mp1;
3460 3460 spdsock_t *ss = (spdsock_t *)q->q_ptr;
3461 3461 spd_stack_t *spds = ss->spdsock_spds;
3462 3462
3463 3463 /*
3464 3464 * If we're dumping, defer processing other messages until the
3465 3465 * dump completes.
3466 3466 */
3467 3467 if (ss->spdsock_dump_req != NULL) {
3468 3468 if (!putq(q, mp))
3469 3469 freemsg(mp);
3470 3470 return (0);
3471 3471 }
3472 3472
3473 3473 switch (mp->b_datap->db_type) {
3474 3474 case M_DATA:
3475 3475 /*
3476 3476 * Silently discard.
3477 3477 */
3478 3478 ss2dbg(spds, ("raw M_DATA in spdsock.\n"));
3479 3479 freemsg(mp);
3480 3480 return (0);
3481 3481 case M_PROTO:
3482 3482 case M_PCPROTO:
3483 3483 if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) {
3484 3484 if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
3485 3485 if ((mp1 = mp->b_cont) == NULL) {
3486 3486 /* No data after T_DATA_REQ. */
3487 3487 ss2dbg(spds,
3488 3488 ("No data after DATA_REQ.\n"));
3489 3489 freemsg(mp);
3490 3490 return (0);
3491 3491 }
3492 3492 freeb(mp);
3493 3493 mp = mp1;
3494 3494 ss2dbg(spds, ("T_DATA_REQ\n"));
3495 3495 break; /* Out of switch. */
3496 3496 }
3497 3497 }
3498 3498 /* FALLTHRU */
3499 3499 default:
3500 3500 ss3dbg(spds, ("In default wput case (%d %d).\n",
3501 3501 mp->b_datap->db_type, ((union T_primitives *)rptr)->type));
3502 3502 spdsock_wput_other(q, mp);
3503 3503 return (0);
3504 3504 }
3505 3505
3506 3506 /* I now have a PF_POLICY message in an M_DATA block. */
3507 3507 spdsock_parse(q, mp);
3508 3508 return (0);
3509 3509 }
3510 3510
3511 3511 /*
3512 3512 * Device open procedure, called when new queue pair created.
3513 3513 * We are passed the read-side queue.
3514 3514 */
3515 3515 /* ARGSUSED */
3516 3516 static int
3517 3517 spdsock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
3518 3518 {
3519 3519 spdsock_t *ss;
3520 3520 queue_t *oq = OTHERQ(q);
3521 3521 minor_t ssminor;
3522 3522 netstack_t *ns;
3523 3523 spd_stack_t *spds;
3524 3524
3525 3525 if (secpolicy_ip_config(credp, B_FALSE) != 0)
3526 3526 return (EPERM);
3527 3527
3528 3528 if (q->q_ptr != NULL)
3529 3529 return (0); /* Re-open of an already open instance. */
3530 3530
3531 3531 if (sflag & MODOPEN)
3532 3532 return (EINVAL);
3533 3533
3534 3534 ns = netstack_find_by_cred(credp);
3535 3535 ASSERT(ns != NULL);
3536 3536 spds = ns->netstack_spdsock;
3537 3537 ASSERT(spds != NULL);
3538 3538
3539 3539 ss2dbg(spds, ("Made it into PF_POLICY socket open.\n"));
3540 3540
3541 3541 ssminor = (minor_t)(uintptr_t)vmem_alloc(spdsock_vmem, 1, VM_NOSLEEP);
3542 3542 if (ssminor == 0) {
3543 3543 netstack_rele(spds->spds_netstack);
3544 3544 return (ENOMEM);
3545 3545 }
3546 3546 ss = kmem_zalloc(sizeof (spdsock_t), KM_NOSLEEP);
3547 3547 if (ss == NULL) {
3548 3548 vmem_free(spdsock_vmem, (void *)(uintptr_t)ssminor, 1);
3549 3549 netstack_rele(spds->spds_netstack);
3550 3550 return (ENOMEM);
3551 3551 }
3552 3552
3553 3553 ss->spdsock_minor = ssminor;
3554 3554 ss->spdsock_state = TS_UNBND;
3555 3555 ss->spdsock_dump_req = NULL;
3556 3556
3557 3557 ss->spdsock_spds = spds;
3558 3558
3559 3559 q->q_ptr = ss;
3560 3560 oq->q_ptr = ss;
3561 3561
3562 3562 q->q_hiwat = spds->spds_recv_hiwat;
3563 3563
3564 3564 oq->q_hiwat = spds->spds_xmit_hiwat;
3565 3565 oq->q_lowat = spds->spds_xmit_lowat;
3566 3566
3567 3567 qprocson(q);
3568 3568 (void) proto_set_rx_hiwat(q, NULL, spds->spds_recv_hiwat);
3569 3569
3570 3570 *devp = makedevice(getmajor(*devp), ss->spdsock_minor);
3571 3571 return (0);
3572 3572 }
3573 3573
3574 3574 /*
3575 3575 * Read-side service procedure, invoked when we get back-enabled
3576 3576 * when buffer space becomes available.
3577 3577 *
3578 3578 * Dump another chunk if we were dumping before; when we finish, kick
3579 3579 * the write-side queue in case it's waiting for read queue space.
3580 3580 */
3581 3581 int
3582 3582 spdsock_rsrv(queue_t *q)
3583 3583 {
3584 3584 spdsock_t *ss = q->q_ptr;
3585 3585
3586 3586 if (ss->spdsock_dump_req != NULL)
3587 3587 spdsock_dump_some(q, ss);
3588 3588
3589 3589 if (ss->spdsock_dump_req == NULL)
3590 3590 qenable(OTHERQ(q));
3591 3591 return (0);
3592 3592 }
3593 3593
3594 3594 /*
3595 3595 * Write-side service procedure, invoked when we defer processing
3596 3596 * if another message is received while a dump is in progress.
3597 3597 */
3598 3598 int
3599 3599 spdsock_wsrv(queue_t *q)
3600 3600 {
3601 3601 spdsock_t *ss = q->q_ptr;
↓ open down ↓ |
3567 lines elided |
↑ open up ↑ |
3602 3602 mblk_t *mp;
3603 3603 ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
3604 3604
3605 3605 if (ss->spdsock_dump_req != NULL) {
3606 3606 qenable(OTHERQ(q));
3607 3607 return (0);
3608 3608 }
3609 3609
3610 3610 while ((mp = getq(q)) != NULL) {
3611 3611 if (ipsec_loaded(ipss)) {
3612 - spdsock_wput(q, mp);
3612 + (void) spdsock_wput(q, mp);
3613 3613 if (ss->spdsock_dump_req != NULL)
3614 3614 return (0);
3615 3615 } else if (!ipsec_failed(ipss)) {
3616 3616 (void) putq(q, mp);
3617 3617 } else {
3618 3618 spdsock_error(q, mp, EPFNOSUPPORT, 0);
3619 3619 }
3620 3620 }
3621 3621 return (0);
3622 3622 }
3623 3623
3624 3624 /* ARGSUSED */
3625 3625 static int
3626 3626 spdsock_close(queue_t *q, int flags __unused, cred_t *credp __unused)
3627 3627 {
3628 3628 spdsock_t *ss = q->q_ptr;
3629 3629 spd_stack_t *spds = ss->spdsock_spds;
3630 3630
3631 3631 qprocsoff(q);
3632 3632
3633 3633 /* Safe assumption. */
3634 3634 ASSERT(ss != NULL);
3635 3635
3636 3636 if (ss->spdsock_timeout != 0)
3637 3637 (void) quntimeout(q, ss->spdsock_timeout);
3638 3638
3639 3639 ss3dbg(spds, ("Driver close, PF_POLICY socket is going away.\n"));
3640 3640
3641 3641 vmem_free(spdsock_vmem, (void *)(uintptr_t)ss->spdsock_minor, 1);
3642 3642 netstack_rele(ss->spdsock_spds->spds_netstack);
3643 3643
3644 3644 kmem_free(ss, sizeof (spdsock_t));
3645 3645 return (0);
3646 3646 }
3647 3647
3648 3648 /*
3649 3649 * Merge the IPsec algorithms tables with the received algorithm information.
3650 3650 */
3651 3651 void
3652 3652 spdsock_merge_algs(spd_stack_t *spds)
3653 3653 {
3654 3654 ipsec_alginfo_t *alg, *oalg;
3655 3655 ipsec_algtype_t algtype;
3656 3656 uint_t algidx, algid, nalgs;
3657 3657 crypto_mech_name_t *mechs;
3658 3658 uint_t mech_count, mech_idx;
3659 3659 netstack_t *ns = spds->spds_netstack;
3660 3660 ipsec_stack_t *ipss = ns->netstack_ipsec;
3661 3661
3662 3662 ASSERT(MUTEX_HELD(&spds->spds_alg_lock));
3663 3663
3664 3664 /*
3665 3665 * Get the list of supported mechanisms from the crypto framework.
3666 3666 * If a mechanism is supported by KCF, resolve its mechanism
3667 3667 * id and mark it as being valid. This operation must be done
3668 3668 * without holding alg_lock, since it can cause a provider
3669 3669 * module to be loaded and the provider notification callback to
3670 3670 * be invoked.
3671 3671 */
3672 3672 mechs = crypto_get_mech_list(&mech_count, KM_SLEEP);
3673 3673 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
3674 3674 for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) {
3675 3675 int algflags = 0;
3676 3676 crypto_mech_type_t mt = CRYPTO_MECHANISM_INVALID;
3677 3677
3678 3678 alg = spds->spds_algs[algtype][algid];
3679 3679 if (alg == NULL)
3680 3680 continue;
3681 3681
3682 3682 /*
3683 3683 * The NULL encryption algorithm is a special
3684 3684 * case because there are no mechanisms, yet
3685 3685 * the algorithm is still valid.
3686 3686 */
3687 3687 if (alg->alg_id == SADB_EALG_NULL) {
3688 3688 alg->alg_mech_type = CRYPTO_MECHANISM_INVALID;
3689 3689 alg->alg_flags |= ALG_FLAG_VALID;
3690 3690 continue;
3691 3691 }
3692 3692
3693 3693 for (mech_idx = 0; mech_idx < mech_count; mech_idx++) {
3694 3694 if (strncmp(alg->alg_mech_name, mechs[mech_idx],
3695 3695 CRYPTO_MAX_MECH_NAME) == 0) {
3696 3696 mt = crypto_mech2id(alg->alg_mech_name);
3697 3697 ASSERT(mt != CRYPTO_MECHANISM_INVALID);
3698 3698 algflags = ALG_FLAG_VALID;
3699 3699 break;
3700 3700 }
3701 3701 }
3702 3702 alg->alg_mech_type = mt;
3703 3703 alg->alg_flags |= algflags;
3704 3704 }
3705 3705 }
3706 3706
3707 3707 rw_enter(&ipss->ipsec_alg_lock, RW_WRITER);
3708 3708
3709 3709 /*
3710 3710 * For each algorithm currently defined, check if it is
3711 3711 * present in the new tables created from the SPD_UPDATEALGS
3712 3712 * message received from user-space.
3713 3713 * Delete the algorithm entries that are currently defined
3714 3714 * but not part of the new tables.
3715 3715 */
3716 3716 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
3717 3717 nalgs = ipss->ipsec_nalgs[algtype];
3718 3718 for (algidx = 0; algidx < nalgs; algidx++) {
3719 3719 algid = ipss->ipsec_sortlist[algtype][algidx];
3720 3720 if (spds->spds_algs[algtype][algid] == NULL)
3721 3721 ipsec_alg_unreg(algtype, algid, ns);
3722 3722 }
3723 3723 }
3724 3724
3725 3725 /*
3726 3726 * For each algorithm we just received, check if it is
3727 3727 * present in the currently defined tables. If it is, swap
3728 3728 * the entry with the one we just allocated.
3729 3729 * If the new algorithm is not in the current tables,
3730 3730 * add it.
3731 3731 */
3732 3732 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
3733 3733 for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) {
3734 3734 alg = spds->spds_algs[algtype][algid];
3735 3735 if (alg == NULL)
3736 3736 continue;
3737 3737
3738 3738 if ((oalg = ipss->ipsec_alglists[algtype][algid]) ==
3739 3739 NULL) {
3740 3740 /*
3741 3741 * New algorithm, add it to the algorithm
3742 3742 * table.
3743 3743 */
3744 3744 ipsec_alg_reg(algtype, alg, ns);
3745 3745 } else {
3746 3746 /*
3747 3747 * Algorithm is already in the table. Swap
3748 3748 * the existing entry with the new one.
3749 3749 */
3750 3750 ipsec_alg_fix_min_max(alg, algtype, ns);
3751 3751 ipss->ipsec_alglists[algtype][algid] = alg;
3752 3752 ipsec_alg_free(oalg);
3753 3753 }
3754 3754 spds->spds_algs[algtype][algid] = NULL;
3755 3755 }
3756 3756 }
3757 3757
3758 3758 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
3759 3759 ipss->ipsec_algs_exec_mode[algtype] =
3760 3760 spds->spds_algs_exec_mode[algtype];
3761 3761 }
3762 3762
3763 3763 rw_exit(&ipss->ipsec_alg_lock);
3764 3764
3765 3765 crypto_free_mech_list(mechs, mech_count);
3766 3766
3767 3767 ipsecah_algs_changed(ns);
3768 3768 ipsecesp_algs_changed(ns);
3769 3769 }
↓ open down ↓ |
147 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX