/
spdsock.c
3765 lines (3276 loc) · 92.7 KB
/
spdsock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Nexenta Systems, Inc. All rights reserved.
* Copyright 2017 Joyent, Inc.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/strsun.h>
#include <sys/stropts.h>
#include <sys/zone.h>
#include <sys/vnode.h>
#include <sys/sysmacros.h>
#define _SUN_TPI_VERSION 2
#include <sys/tihdr.h>
#include <sys/timod.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/mkdev.h>
#include <sys/debug.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/suntpi.h>
#include <sys/policy.h>
#include <sys/dls.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/pfkeyv2.h>
#include <net/pfpolicy.h>
#include <inet/common.h>
#include <netinet/ip6.h>
#include <inet/ip.h>
#include <inet/ip6.h>
#include <inet/mi.h>
#include <inet/proto_set.h>
#include <inet/nd.h>
#include <inet/ip_if.h>
#include <inet/optcom.h>
#include <inet/ipsec_impl.h>
#include <inet/spdsock.h>
#include <inet/sadb.h>
#include <inet/iptun.h>
#include <inet/iptun/iptun_impl.h>
#include <sys/isa_defs.h>
#include <c2/audit.h>
/*
* This is a transport provider for the PF_POLICY IPsec policy
* management socket, which provides a management interface into the
* SPD, allowing policy rules to be added, deleted, and queried.
*
* This effectively replaces the old private SIOC*IPSECONFIG ioctls
* with an extensible interface which will hopefully be public some
* day.
*
* See <net/pfpolicy.h> for more details on the protocol.
*
* We link against drv/ip and call directly into it to manipulate the
* SPD; see ipsec_impl.h for the policy data structures and spd.c for
* the code which maintains them.
*
* The MT model of this is QPAIR with the addition of some explicit
* locking to protect system-wide policy data structures.
*/
static vmem_t *spdsock_vmem; /* for minor numbers. */
#define ALIGNED64(x) IS_P2ALIGNED((x), sizeof (uint64_t))
/* Default structure copied into T_INFO_ACK messages (from rts.c...) */
static struct T_info_ack spdsock_g_t_info_ack = {
T_INFO_ACK,
T_INFINITE, /* TSDU_size. Maximum size messages. */
T_INVALID, /* ETSDU_size. No expedited data. */
T_INVALID, /* CDATA_size. No connect data. */
T_INVALID, /* DDATA_size. No disconnect data. */
0, /* ADDR_size. */
0, /* OPT_size. No user-settable options */
64 * 1024, /* TIDU_size. spdsock allows maximum size messages. */
T_COTS, /* SERV_type. spdsock supports connection oriented. */
TS_UNBND, /* CURRENT_state. This is set from spdsock_state. */
(XPG4_1) /* Provider flags */
};
/* Named Dispatch Parameter Management Structure */
typedef struct spdsockparam_s {
uint_t spdsock_param_min;
uint_t spdsock_param_max;
uint_t spdsock_param_value;
char *spdsock_param_name;
} spdsockparam_t;
/*
* Table of NDD variables supported by spdsock. These are loaded into
* spdsock_g_nd in spdsock_init_nd.
* All of these are alterable, within the min/max values given, at run time.
*/
static spdsockparam_t lcl_param_arr[] = {
/* min max value name */
{ 4096, 65536, 8192, "spdsock_xmit_hiwat"},
{ 0, 65536, 1024, "spdsock_xmit_lowat"},
{ 4096, 65536, 8192, "spdsock_recv_hiwat"},
{ 65536, 1024*1024*1024, 256*1024, "spdsock_max_buf"},
{ 0, 3, 0, "spdsock_debug"},
};
#define spds_xmit_hiwat spds_params[0].spdsock_param_value
#define spds_xmit_lowat spds_params[1].spdsock_param_value
#define spds_recv_hiwat spds_params[2].spdsock_param_value
#define spds_max_buf spds_params[3].spdsock_param_value
#define spds_debug spds_params[4].spdsock_param_value
#define ss0dbg(a) printf a
/* NOTE: != 0 instead of > 0 so lint doesn't complain. */
#define ss1dbg(spds, a) if (spds->spds_debug != 0) printf a
#define ss2dbg(spds, a) if (spds->spds_debug > 1) printf a
#define ss3dbg(spds, a) if (spds->spds_debug > 2) printf a
#define RESET_SPDSOCK_DUMP_POLHEAD(ss, iph) { \
ASSERT(RW_READ_HELD(&(iph)->iph_lock)); \
(ss)->spdsock_dump_head = (iph); \
(ss)->spdsock_dump_gen = (iph)->iph_gen; \
(ss)->spdsock_dump_cur_type = 0; \
(ss)->spdsock_dump_cur_af = IPSEC_AF_V4; \
(ss)->spdsock_dump_cur_rule = NULL; \
(ss)->spdsock_dump_count = 0; \
(ss)->spdsock_dump_cur_chain = 0; \
}
static int spdsock_close(queue_t *);
static int spdsock_open(queue_t *, dev_t *, int, int, cred_t *);
static void spdsock_wput(queue_t *, mblk_t *);
static void spdsock_wsrv(queue_t *);
static void spdsock_rsrv(queue_t *);
static void *spdsock_stack_init(netstackid_t stackid, netstack_t *ns);
static void spdsock_stack_shutdown(netstackid_t stackid, void *arg);
static void spdsock_stack_fini(netstackid_t stackid, void *arg);
static void spdsock_loadcheck(void *);
static void spdsock_merge_algs(spd_stack_t *);
static void spdsock_flush_one(ipsec_policy_head_t *, netstack_t *);
static mblk_t *spdsock_dump_next_record(spdsock_t *);
static void update_iptun_policy(ipsec_tun_pol_t *);
static struct module_info info = {
5138, "spdsock", 1, INFPSZ, 512, 128
};
static struct qinit rinit = {
NULL, (pfi_t)spdsock_rsrv, spdsock_open, spdsock_close,
NULL, &info
};
static struct qinit winit = {
(pfi_t)spdsock_wput, (pfi_t)spdsock_wsrv, NULL, NULL, NULL, &info
};
struct streamtab spdsockinfo = {
&rinit, &winit
};
/* mapping from alg type to protocol number, as per RFC 2407 */
static const uint_t algproto[] = {
PROTO_IPSEC_AH,
PROTO_IPSEC_ESP,
};
#define NALGPROTOS (sizeof (algproto) / sizeof (algproto[0]))
/* mapping from kernel exec mode to spdsock exec mode */
static const uint_t execmodes[] = {
SPD_ALG_EXEC_MODE_SYNC,
SPD_ALG_EXEC_MODE_ASYNC
};
#define NEXECMODES (sizeof (execmodes) / sizeof (execmodes[0]))
#define ALL_ACTIVE_POLHEADS ((ipsec_policy_head_t *)-1)
#define ALL_INACTIVE_POLHEADS ((ipsec_policy_head_t *)-2)
#define ITP_NAME(itp) (itp != NULL ? itp->itp_name : NULL)
/* ARGSUSED */
static int
spdsock_param_get(
queue_t *q,
mblk_t *mp,
caddr_t cp,
cred_t *cr)
{
spdsockparam_t *spdsockpa = (spdsockparam_t *)cp;
uint_t value;
spdsock_t *ss = (spdsock_t *)q->q_ptr;
spd_stack_t *spds = ss->spdsock_spds;
mutex_enter(&spds->spds_param_lock);
value = spdsockpa->spdsock_param_value;
mutex_exit(&spds->spds_param_lock);
(void) mi_mpprintf(mp, "%u", value);
return (0);
}
/* This routine sets an NDD variable in a spdsockparam_t structure. */
/* ARGSUSED */
static int
spdsock_param_set(
queue_t *q,
mblk_t *mp,
char *value,
caddr_t cp,
cred_t *cr)
{
ulong_t new_value;
spdsockparam_t *spdsockpa = (spdsockparam_t *)cp;
spdsock_t *ss = (spdsock_t *)q->q_ptr;
spd_stack_t *spds = ss->spdsock_spds;
/* Convert the value from a string into a long integer. */
if (ddi_strtoul(value, NULL, 10, &new_value) != 0)
return (EINVAL);
mutex_enter(&spds->spds_param_lock);
/*
* Fail the request if the new value does not lie within the
* required bounds.
*/
if (new_value < spdsockpa->spdsock_param_min ||
new_value > spdsockpa->spdsock_param_max) {
mutex_exit(&spds->spds_param_lock);
return (EINVAL);
}
/* Set the new value */
spdsockpa->spdsock_param_value = new_value;
mutex_exit(&spds->spds_param_lock);
return (0);
}
/*
* Initialize at module load time
*/
boolean_t
spdsock_ddi_init(void)
{
spdsock_max_optsize = optcom_max_optsize(
spdsock_opt_obj.odb_opt_des_arr, spdsock_opt_obj.odb_opt_arr_cnt);
spdsock_vmem = vmem_create("spdsock", (void *)1, MAXMIN, 1,
NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER);
/*
* We want to be informed each time a stack is created or
* destroyed in the kernel, so we can maintain the
* set of spd_stack_t's.
*/
netstack_register(NS_SPDSOCK, spdsock_stack_init,
spdsock_stack_shutdown, spdsock_stack_fini);
return (B_TRUE);
}
/*
* Walk through the param array specified registering each element with the
* named dispatch handler.
*/
static boolean_t
spdsock_param_register(IDP *ndp, spdsockparam_t *ssp, int cnt)
{
for (; cnt-- > 0; ssp++) {
if (ssp->spdsock_param_name != NULL &&
ssp->spdsock_param_name[0]) {
if (!nd_load(ndp,
ssp->spdsock_param_name,
spdsock_param_get, spdsock_param_set,
(caddr_t)ssp)) {
nd_free(ndp);
return (B_FALSE);
}
}
}
return (B_TRUE);
}
/*
* Initialize for each stack instance
*/
/* ARGSUSED */
static void *
spdsock_stack_init(netstackid_t stackid, netstack_t *ns)
{
spd_stack_t *spds;
spdsockparam_t *ssp;
spds = (spd_stack_t *)kmem_zalloc(sizeof (*spds), KM_SLEEP);
spds->spds_netstack = ns;
ASSERT(spds->spds_g_nd == NULL);
ssp = (spdsockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
spds->spds_params = ssp;
bcopy(lcl_param_arr, ssp, sizeof (lcl_param_arr));
(void) spdsock_param_register(&spds->spds_g_nd, ssp,
A_CNT(lcl_param_arr));
mutex_init(&spds->spds_param_lock, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&spds->spds_alg_lock, NULL, MUTEX_DEFAULT, NULL);
return (spds);
}
void
spdsock_ddi_destroy(void)
{
vmem_destroy(spdsock_vmem);
netstack_unregister(NS_SPDSOCK);
}
/*
* Do pre-removal cleanup.
*/
/* ARGSUSED */
static void
spdsock_stack_shutdown(netstackid_t stackid, void *arg)
{
spd_stack_t *spds = (spd_stack_t *)arg;
if (spds->spds_mp_algs != NULL) {
freemsg(spds->spds_mp_algs);
spds->spds_mp_algs = NULL;
}
}
/* ARGSUSED */
static void
spdsock_stack_fini(netstackid_t stackid, void *arg)
{
spd_stack_t *spds = (spd_stack_t *)arg;
ASSERT(spds->spds_mp_algs == NULL);
mutex_destroy(&spds->spds_param_lock);
mutex_destroy(&spds->spds_alg_lock);
nd_free(&spds->spds_g_nd);
kmem_free(spds->spds_params, sizeof (lcl_param_arr));
spds->spds_params = NULL;
kmem_free(spds, sizeof (*spds));
}
/*
* NOTE: large quantities of this should be shared with keysock.
* Would be nice to combine some of this into a common module, but
* not possible given time pressures.
*/
/*
* High-level reality checking of extensions.
*/
/* ARGSUSED */ /* XXX */
static boolean_t
ext_check(spd_ext_t *ext)
{
spd_if_t *tunname = (spd_if_t *)ext;
int i;
char *idstr;
if (ext->spd_ext_type == SPD_EXT_TUN_NAME) {
/* (NOTE: Modified from SADB_EXT_IDENTITY..) */
/*
* Make sure the strings in these identities are
* null-terminated. Let's "proactively" null-terminate the
* string at the last byte if it's not terminated sooner.
*/
i = SPD_64TO8(tunname->spd_if_len) - sizeof (spd_if_t);
idstr = (char *)(tunname + 1);
while (*idstr != '\0' && i > 0) {
i--;
idstr++;
}
if (i == 0) {
/*
* I.e., if the bozo user didn't NULL-terminate the
* string...
*/
idstr--;
*idstr = '\0';
}
}
return (B_TRUE); /* For now... */
}
/* Return values for spdsock_get_ext(). */
#define KGE_OK 0
#define KGE_DUP 1
#define KGE_UNK 2
#define KGE_LEN 3
#define KGE_CHK 4
/*
* Parse basic extension headers and return in the passed-in pointer vector.
* Return values include:
*
* KGE_OK Everything's nice and parsed out.
* If there are no extensions, place NULL in extv[0].
* KGE_DUP There is a duplicate extension.
* First instance in appropriate bin. First duplicate in
* extv[0].
* KGE_UNK Unknown extension type encountered. extv[0] contains
* unknown header.
* KGE_LEN Extension length error.
* KGE_CHK High-level reality check failed on specific extension.
*
* My apologies for some of the pointer arithmetic in here. I'm thinking
* like an assembly programmer, yet trying to make the compiler happy.
*/
static int
spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize)
{
bzero(extv, sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1));
/* Use extv[0] as the "current working pointer". */
extv[0] = (spd_ext_t *)(basehdr + 1);
while (extv[0] < (spd_ext_t *)(((uint8_t *)basehdr) + msgsize)) {
/* Check for unknown headers. */
if (extv[0]->spd_ext_type == 0 ||
extv[0]->spd_ext_type > SPD_EXT_MAX)
return (KGE_UNK);
/*
* Check length. Use uint64_t because extlen is in units
* of 64-bit words. If length goes beyond the msgsize,
* return an error. (Zero length also qualifies here.)
*/
if (extv[0]->spd_ext_len == 0 ||
(void *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
(void *)((uint8_t *)basehdr + msgsize))
return (KGE_LEN);
/* Check for redundant headers. */
if (extv[extv[0]->spd_ext_type] != NULL)
return (KGE_DUP);
/*
* Reality check the extension if possible at the spdsock
* level.
*/
if (!ext_check(extv[0]))
return (KGE_CHK);
/* If I make it here, assign the appropriate bin. */
extv[extv[0]->spd_ext_type] = extv[0];
/* Advance pointer (See above for uint64_t ptr reasoning.) */
extv[0] = (spd_ext_t *)
((uint64_t *)extv[0] + extv[0]->spd_ext_len);
}
/* Everything's cool. */
/*
* If extv[0] == NULL, then there are no extension headers in this
* message. Ensure that this is the case.
*/
if (extv[0] == (spd_ext_t *)(basehdr + 1))
extv[0] = NULL;
return (KGE_OK);
}
static const int bad_ext_diag[] = {
SPD_DIAGNOSTIC_MALFORMED_LCLPORT,
SPD_DIAGNOSTIC_MALFORMED_REMPORT,
SPD_DIAGNOSTIC_MALFORMED_PROTO,
SPD_DIAGNOSTIC_MALFORMED_LCLADDR,
SPD_DIAGNOSTIC_MALFORMED_REMADDR,
SPD_DIAGNOSTIC_MALFORMED_ACTION,
SPD_DIAGNOSTIC_MALFORMED_RULE,
SPD_DIAGNOSTIC_MALFORMED_RULESET,
SPD_DIAGNOSTIC_MALFORMED_ICMP_TYPECODE
};
static const int dup_ext_diag[] = {
SPD_DIAGNOSTIC_DUPLICATE_LCLPORT,
SPD_DIAGNOSTIC_DUPLICATE_REMPORT,
SPD_DIAGNOSTIC_DUPLICATE_PROTO,
SPD_DIAGNOSTIC_DUPLICATE_LCLADDR,
SPD_DIAGNOSTIC_DUPLICATE_REMADDR,
SPD_DIAGNOSTIC_DUPLICATE_ACTION,
SPD_DIAGNOSTIC_DUPLICATE_RULE,
SPD_DIAGNOSTIC_DUPLICATE_RULESET,
SPD_DIAGNOSTIC_DUPLICATE_ICMP_TYPECODE
};
/*
* Transmit a PF_POLICY error message to the instance either pointed to
* by ks, the instance with serial number serial, or more, depending.
*
* The faulty message (or a reasonable facsimile thereof) is in mp.
* This function will free mp or recycle it for delivery, thereby causing
* the stream head to free it.
*/
static void
spdsock_error(queue_t *q, mblk_t *mp, int error, int diagnostic)
{
spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
ASSERT(mp->b_datap->db_type == M_DATA);
if (spmsg->spd_msg_type < SPD_MIN ||
spmsg->spd_msg_type > SPD_MAX)
spmsg->spd_msg_type = SPD_RESERVED;
/*
* Strip out extension headers.
*/
ASSERT(mp->b_rptr + sizeof (*spmsg) <= mp->b_datap->db_lim);
mp->b_wptr = mp->b_rptr + sizeof (*spmsg);
spmsg->spd_msg_len = SPD_8TO64(sizeof (spd_msg_t));
spmsg->spd_msg_errno = (uint8_t)error;
spmsg->spd_msg_diagnostic = (uint16_t)diagnostic;
qreply(q, mp);
}
static void
spdsock_diag(queue_t *q, mblk_t *mp, int diagnostic)
{
spdsock_error(q, mp, EINVAL, diagnostic);
}
static void
spd_echo(queue_t *q, mblk_t *mp)
{
qreply(q, mp);
}
/*
* Do NOT consume a reference to itp.
*/
/*ARGSUSED*/
static void
spdsock_flush_node(ipsec_tun_pol_t *itp, void *cookie, netstack_t *ns)
{
boolean_t active = (boolean_t)cookie;
ipsec_policy_head_t *iph;
iph = active ? itp->itp_policy : itp->itp_inactive;
IPPH_REFHOLD(iph);
mutex_enter(&itp->itp_lock);
spdsock_flush_one(iph, ns); /* Releases iph refhold. */
if (active)
itp->itp_flags &= ~ITPF_PFLAGS;
else
itp->itp_flags &= ~ITPF_IFLAGS;
mutex_exit(&itp->itp_lock);
/* SPD_FLUSH is worth a tunnel MTU check. */
update_iptun_policy(itp);
}
/*
* Clear out one polhead.
*/
static void
spdsock_flush_one(ipsec_policy_head_t *iph, netstack_t *ns)
{
rw_enter(&iph->iph_lock, RW_WRITER);
ipsec_polhead_flush(iph, ns);
rw_exit(&iph->iph_lock);
IPPH_REFRELE(iph, ns);
}
static void
spdsock_flush(queue_t *q, ipsec_policy_head_t *iph, ipsec_tun_pol_t *itp,
mblk_t *mp)
{
boolean_t active;
spdsock_t *ss = (spdsock_t *)q->q_ptr;
netstack_t *ns = ss->spdsock_spds->spds_netstack;
uint32_t auditing = AU_AUDITING();
if (iph != ALL_ACTIVE_POLHEADS && iph != ALL_INACTIVE_POLHEADS) {
spdsock_flush_one(iph, ns);
if (auditing) {
spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
cred_t *cr;
pid_t cpid;
cr = msg_getcred(mp, &cpid);
active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
audit_pf_policy(SPD_FLUSH, cr, ns,
ITP_NAME(itp), active, 0, cpid);
}
} else {
active = (iph == ALL_ACTIVE_POLHEADS);
/* First flush the global policy. */
spdsock_flush_one(active ? ipsec_system_policy(ns) :
ipsec_inactive_policy(ns), ns);
if (auditing) {
cred_t *cr;
pid_t cpid;
cr = msg_getcred(mp, &cpid);
audit_pf_policy(SPD_FLUSH, cr, ns, NULL,
active, 0, cpid);
}
/* Then flush every tunnel's appropriate one. */
itp_walk(spdsock_flush_node, (void *)active, ns);
if (auditing) {
cred_t *cr;
pid_t cpid;
cr = msg_getcred(mp, &cpid);
audit_pf_policy(SPD_FLUSH, cr, ns,
"all tunnels", active, 0, cpid);
}
}
spd_echo(q, mp);
}
static boolean_t
spdsock_ext_to_sel(spd_ext_t **extv, ipsec_selkey_t *sel, int *diag)
{
bzero(sel, sizeof (*sel));
if (extv[SPD_EXT_PROTO] != NULL) {
struct spd_proto *pr =
(struct spd_proto *)extv[SPD_EXT_PROTO];
sel->ipsl_proto = pr->spd_proto_number;
sel->ipsl_valid |= IPSL_PROTOCOL;
}
if (extv[SPD_EXT_LCLPORT] != NULL) {
struct spd_portrange *pr =
(struct spd_portrange *)extv[SPD_EXT_LCLPORT];
sel->ipsl_lport = pr->spd_ports_minport;
sel->ipsl_valid |= IPSL_LOCAL_PORT;
}
if (extv[SPD_EXT_REMPORT] != NULL) {
struct spd_portrange *pr =
(struct spd_portrange *)extv[SPD_EXT_REMPORT];
sel->ipsl_rport = pr->spd_ports_minport;
sel->ipsl_valid |= IPSL_REMOTE_PORT;
}
if (extv[SPD_EXT_ICMP_TYPECODE] != NULL) {
struct spd_typecode *tc=
(struct spd_typecode *)extv[SPD_EXT_ICMP_TYPECODE];
sel->ipsl_valid |= IPSL_ICMP_TYPE;
sel->ipsl_icmp_type = tc->spd_typecode_type;
if (tc->spd_typecode_type_end < tc->spd_typecode_type)
sel->ipsl_icmp_type_end = tc->spd_typecode_type;
else
sel->ipsl_icmp_type_end = tc->spd_typecode_type_end;
if (tc->spd_typecode_code != 255) {
sel->ipsl_valid |= IPSL_ICMP_CODE;
sel->ipsl_icmp_code = tc->spd_typecode_code;
if (tc->spd_typecode_code_end < tc->spd_typecode_code)
sel->ipsl_icmp_code_end = tc->spd_typecode_code;
else
sel->ipsl_icmp_code_end =
tc->spd_typecode_code_end;
}
}
#define ADDR2SEL(sel, extv, field, pfield, extn, bit) \
if ((extv)[(extn)] != NULL) { \
uint_t addrlen; \
struct spd_address *ap = \
(struct spd_address *)((extv)[(extn)]); \
addrlen = (ap->spd_address_af == AF_INET6) ? \
IPV6_ADDR_LEN : IP_ADDR_LEN; \
if (SPD_64TO8(ap->spd_address_len) < \
(addrlen + sizeof (*ap))) { \
*diag = SPD_DIAGNOSTIC_BAD_ADDR_LEN; \
return (B_FALSE); \
} \
bcopy((ap+1), &((sel)->field), addrlen); \
(sel)->pfield = ap->spd_address_prefixlen; \
(sel)->ipsl_valid |= (bit); \
(sel)->ipsl_valid |= (ap->spd_address_af == AF_INET6) ? \
IPSL_IPV6 : IPSL_IPV4; \
}
ADDR2SEL(sel, extv, ipsl_local, ipsl_local_pfxlen,
SPD_EXT_LCLADDR, IPSL_LOCAL_ADDR);
ADDR2SEL(sel, extv, ipsl_remote, ipsl_remote_pfxlen,
SPD_EXT_REMADDR, IPSL_REMOTE_ADDR);
if ((sel->ipsl_valid & (IPSL_IPV6|IPSL_IPV4)) ==
(IPSL_IPV6|IPSL_IPV4)) {
*diag = SPD_DIAGNOSTIC_MIXED_AF;
return (B_FALSE);
}
#undef ADDR2SEL
return (B_TRUE);
}
static boolean_t
spd_convert_type(uint32_t type, ipsec_act_t *act)
{
switch (type) {
case SPD_ACTTYPE_DROP:
act->ipa_type = IPSEC_ACT_DISCARD;
return (B_TRUE);
case SPD_ACTTYPE_PASS:
act->ipa_type = IPSEC_ACT_CLEAR;
return (B_TRUE);
case SPD_ACTTYPE_IPSEC:
act->ipa_type = IPSEC_ACT_APPLY;
return (B_TRUE);
}
return (B_FALSE);
}
static boolean_t
spd_convert_flags(uint32_t flags, ipsec_act_t *act)
{
/*
* Note use of !! for boolean canonicalization.
*/
act->ipa_apply.ipp_use_ah = !!(flags & SPD_APPLY_AH);
act->ipa_apply.ipp_use_esp = !!(flags & SPD_APPLY_ESP);
act->ipa_apply.ipp_use_espa = !!(flags & SPD_APPLY_ESPA);
act->ipa_apply.ipp_use_se = !!(flags & SPD_APPLY_SE);
act->ipa_apply.ipp_use_unique = !!(flags & SPD_APPLY_UNIQUE);
return (B_TRUE);
}
static void
spdsock_reset_act(ipsec_act_t *act)
{
bzero(act, sizeof (*act));
act->ipa_apply.ipp_espe_maxbits = IPSEC_MAX_KEYBITS;
act->ipa_apply.ipp_espa_maxbits = IPSEC_MAX_KEYBITS;
act->ipa_apply.ipp_ah_maxbits = IPSEC_MAX_KEYBITS;
}
/*
* Sanity check action against reality, and shrink-wrap key sizes..
*/
static boolean_t
spdsock_check_action(ipsec_act_t *act, boolean_t tunnel_polhead, int *diag,
spd_stack_t *spds)
{
if (tunnel_polhead && act->ipa_apply.ipp_use_unique) {
*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
return (B_FALSE);
}
if ((act->ipa_type != IPSEC_ACT_APPLY) &&
(act->ipa_apply.ipp_use_ah ||
act->ipa_apply.ipp_use_esp ||
act->ipa_apply.ipp_use_espa ||
act->ipa_apply.ipp_use_se ||
act->ipa_apply.ipp_use_unique)) {
*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
return (B_FALSE);
}
if ((act->ipa_type == IPSEC_ACT_APPLY) &&
!act->ipa_apply.ipp_use_ah &&
!act->ipa_apply.ipp_use_esp) {
*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
return (B_FALSE);
}
return (ipsec_check_action(act, diag, spds->spds_netstack));
}
/*
* We may be short a few error checks here..
*/
static boolean_t
spdsock_ext_to_actvec(spd_ext_t **extv, ipsec_act_t **actpp, uint_t *nactp,
int *diag, spd_stack_t *spds)
{
struct spd_ext_actions *sactp =
(struct spd_ext_actions *)extv[SPD_EXT_ACTION];
ipsec_act_t act, *actp, *endactp;
struct spd_attribute *attrp, *endattrp;
uint64_t *endp;
int nact;
boolean_t tunnel_polhead;
tunnel_polhead = (extv[SPD_EXT_TUN_NAME] != NULL &&
(((struct spd_rule *)extv[SPD_EXT_RULE])->spd_rule_flags &
SPD_RULE_FLAG_TUNNEL));
*actpp = NULL;
*nactp = 0;
if (sactp == NULL) {
*diag = SPD_DIAGNOSTIC_NO_ACTION_EXT;
return (B_FALSE);
}
/*
* Parse the "action" extension and convert into an action chain.
*/
nact = sactp->spd_actions_count;
endp = (uint64_t *)sactp;
endp += sactp->spd_actions_len;
endattrp = (struct spd_attribute *)endp;
actp = kmem_alloc(sizeof (*actp) * nact, KM_NOSLEEP);
if (actp == NULL) {
*diag = SPD_DIAGNOSTIC_ADD_NO_MEM;
return (B_FALSE);
}
*actpp = actp;
*nactp = nact;
endactp = actp + nact;
spdsock_reset_act(&act);
attrp = (struct spd_attribute *)(&sactp[1]);
for (; attrp < endattrp; attrp++) {
switch (attrp->spd_attr_tag) {
case SPD_ATTR_NOP:
break;
case SPD_ATTR_EMPTY:
spdsock_reset_act(&act);
break;
case SPD_ATTR_END:
attrp = endattrp;
/* FALLTHRU */
case SPD_ATTR_NEXT:
if (actp >= endactp) {
*diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
goto fail;
}
if (!spdsock_check_action(&act, tunnel_polhead,
diag, spds))
goto fail;
*actp++ = act;
spdsock_reset_act(&act);
break;
case SPD_ATTR_TYPE:
if (!spd_convert_type(attrp->spd_attr_value, &act)) {
*diag = SPD_DIAGNOSTIC_ADD_BAD_TYPE;
goto fail;
}
break;
case SPD_ATTR_FLAGS:
if (!tunnel_polhead && extv[SPD_EXT_TUN_NAME] != NULL) {
/*
* Set "sa unique" for transport-mode
* tunnels whether we want to or not.
*/
attrp->spd_attr_value |= SPD_APPLY_UNIQUE;
}
if (!spd_convert_flags(attrp->spd_attr_value, &act)) {
*diag = SPD_DIAGNOSTIC_ADD_BAD_FLAGS;
goto fail;
}
break;
case SPD_ATTR_AH_AUTH:
if (attrp->spd_attr_value == 0) {
*diag = SPD_DIAGNOSTIC_UNSUPP_AH_ALG;
goto fail;
}
act.ipa_apply.ipp_auth_alg = attrp->spd_attr_value;
break;
case SPD_ATTR_ESP_ENCR:
if (attrp->spd_attr_value == 0) {
*diag = SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG;
goto fail;
}
act.ipa_apply.ipp_encr_alg = attrp->spd_attr_value;
break;
case SPD_ATTR_ESP_AUTH:
if (attrp->spd_attr_value == 0) {
*diag = SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG;
goto fail;
}
act.ipa_apply.ipp_esp_auth_alg = attrp->spd_attr_value;
break;
case SPD_ATTR_ENCR_MINBITS:
act.ipa_apply.ipp_espe_minbits = attrp->spd_attr_value;
break;
case SPD_ATTR_ENCR_MAXBITS:
act.ipa_apply.ipp_espe_maxbits = attrp->spd_attr_value;
break;
case SPD_ATTR_AH_MINBITS:
act.ipa_apply.ipp_ah_minbits = attrp->spd_attr_value;
break;
case SPD_ATTR_AH_MAXBITS:
act.ipa_apply.ipp_ah_maxbits = attrp->spd_attr_value;
break;
case SPD_ATTR_ESPA_MINBITS:
act.ipa_apply.ipp_espa_minbits = attrp->spd_attr_value;
break;
case SPD_ATTR_ESPA_MAXBITS:
act.ipa_apply.ipp_espa_maxbits = attrp->spd_attr_value;
break;
case SPD_ATTR_LIFE_SOFT_TIME:
case SPD_ATTR_LIFE_HARD_TIME:
case SPD_ATTR_LIFE_SOFT_BYTES:
case SPD_ATTR_LIFE_HARD_BYTES:
break;
case SPD_ATTR_KM_PROTO:
act.ipa_apply.ipp_km_proto = attrp->spd_attr_value;
break;
case SPD_ATTR_KM_COOKIE:
act.ipa_apply.ipp_km_cookie = attrp->spd_attr_value;
break;
case SPD_ATTR_REPLAY_DEPTH:
act.ipa_apply.ipp_replay_depth = attrp->spd_attr_value;
break;
}
}
if (actp != endactp) {
*diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
goto fail;
}
return (B_TRUE);
fail:
ipsec_actvec_free(*actpp, nact);
*actpp = NULL;
return (B_FALSE);
}
typedef struct
{
ipsec_policy_t *pol;
int dir;
} tmprule_t;
static int
mkrule(ipsec_policy_head_t *iph, struct spd_rule *rule,
ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t af,
tmprule_t **rp, uint64_t *index, spd_stack_t *spds)
{
ipsec_policy_t *pol;
sel->ipsl_valid &= ~(IPSL_IPV6|IPSL_IPV4);
sel->ipsl_valid |= af;
pol = ipsec_policy_create(sel, actp, nact, rule->spd_rule_priority,
index, spds->spds_netstack);
if (pol == NULL)
return (ENOMEM);
(*rp)->pol = pol;
(*rp)->dir = dir;