-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipsec.c
2595 lines (2342 loc) · 66.6 KB
/
ipsec.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
/* $OpenBSD: ipsec.c,v 1.150 2018/01/15 09:54:48 mpi Exp $ */
/* $EOM: ipsec.c,v 1.143 2000/12/11 23:57:42 niklas Exp $ */
/*
* Copyright (c) 1998, 1999, 2000, 2001 Niklas Hallqvist. All rights reserved.
* Copyright (c) 2001 Angelos D. Keromytis. All rights reserved.
* Copyright (c) 2001 Håkan Olsson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This code was written under funding by Ericsson Radio Systems.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <net/if.h>
#include <net/pfvar.h>
#include "attribute.h"
#include "conf.h"
#include "connection.h"
#include "constants.h"
#include "crypto.h"
#include "dh.h"
#include "doi.h"
#include "dpd.h"
#include "exchange.h"
#include "hash.h"
#include "ike_aggressive.h"
#include "ike_auth.h"
#include "ike_main_mode.h"
#include "ike_quick_mode.h"
#include "ipsec.h"
#include "ipsec_doi.h"
#include "isakmp.h"
#include "isakmp_cfg.h"
#include "isakmp_fld.h"
#include "isakmp_num.h"
#include "log.h"
#include "message.h"
#include "nat_traversal.h"
#include "pf_key_v2.h"
#include "prf.h"
#include "sa.h"
#include "timer.h"
#include "transport.h"
#include "util.h"
#include "x509.h"
extern int acquire_only;
#define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
/* The replay window size used for all IPsec protocols if not overridden. */
#define DEFAULT_REPLAY_WINDOW 16
struct ipsec_decode_arg {
struct message *msg;
struct sa *sa;
struct proto *proto;
};
/* These variables hold the contacted peers ADT state. */
struct contact {
struct sockaddr *addr;
socklen_t len;
} *contacts = 0;
int contact_cnt = 0, contact_limit = 0;
static int addr_cmp(const void *, const void *);
static int ipsec_add_contact(struct message *);
static int ipsec_contacted(struct message *);
static int ipsec_debug_attribute(u_int16_t, u_int8_t *, u_int16_t,
void *);
static void ipsec_delete_spi(struct sa *, struct proto *, int);
static int16_t *ipsec_exchange_script(u_int8_t);
static void ipsec_finalize_exchange(struct message *);
static void ipsec_free_exchange_data(void *);
static void ipsec_free_proto_data(void *);
static void ipsec_free_sa_data(void *);
static struct keystate *ipsec_get_keystate(struct message *);
static u_int8_t *ipsec_get_spi(size_t *, u_int8_t, struct message *);
static int ipsec_handle_leftover_payload(struct message *, u_int8_t,
struct payload *);
static int ipsec_informational_post_hook(struct message *);
static int ipsec_informational_pre_hook(struct message *);
static int ipsec_initiator(struct message *);
static void ipsec_proto_init(struct proto *, char *);
static int ipsec_responder(struct message *);
static void ipsec_setup_situation(u_int8_t *);
static int ipsec_set_network(u_int8_t *, u_int8_t *, struct sa *);
static size_t ipsec_situation_size(void);
static u_int8_t ipsec_spi_size(u_int8_t);
static int ipsec_validate_attribute(u_int16_t, u_int8_t *, u_int16_t,
void *);
static int ipsec_validate_exchange(u_int8_t);
static int ipsec_validate_id_information(u_int8_t, u_int8_t *, u_int8_t *,
size_t, struct exchange *);
static int ipsec_validate_key_information(u_int8_t *, size_t);
static int ipsec_validate_notification(u_int16_t);
static int ipsec_validate_proto(u_int8_t);
static int ipsec_validate_situation(u_int8_t *, size_t *, size_t);
static int ipsec_validate_transform_id(u_int8_t, u_int8_t);
static int ipsec_sa_check_flow(struct sa *, void *);
static int ipsec_sa_check_flow_any(struct sa *, void *);
static int ipsec_sa_tag(struct exchange *, struct sa *, struct sa *);
static struct doi ipsec_doi = {
{0}, IPSEC_DOI_IPSEC,
sizeof(struct ipsec_exch), sizeof(struct ipsec_sa),
sizeof(struct ipsec_proto),
ipsec_debug_attribute,
ipsec_delete_spi,
ipsec_exchange_script,
ipsec_finalize_exchange,
ipsec_free_exchange_data,
ipsec_free_proto_data,
ipsec_free_sa_data,
ipsec_get_keystate,
ipsec_get_spi,
ipsec_handle_leftover_payload,
ipsec_informational_post_hook,
ipsec_informational_pre_hook,
ipsec_is_attribute_incompatible,
ipsec_proto_init,
ipsec_setup_situation,
ipsec_situation_size,
ipsec_spi_size,
ipsec_validate_attribute,
ipsec_validate_exchange,
ipsec_validate_id_information,
ipsec_validate_key_information,
ipsec_validate_notification,
ipsec_validate_proto,
ipsec_validate_situation,
ipsec_validate_transform_id,
ipsec_initiator,
ipsec_responder,
ipsec_decode_ids
};
int16_t script_quick_mode[] = {
ISAKMP_PAYLOAD_HASH, /* Initiator -> responder. */
ISAKMP_PAYLOAD_SA,
ISAKMP_PAYLOAD_NONCE,
EXCHANGE_SCRIPT_SWITCH,
ISAKMP_PAYLOAD_HASH, /* Responder -> initiator. */
ISAKMP_PAYLOAD_SA,
ISAKMP_PAYLOAD_NONCE,
EXCHANGE_SCRIPT_SWITCH,
ISAKMP_PAYLOAD_HASH, /* Initiator -> responder. */
EXCHANGE_SCRIPT_END
};
int16_t script_new_group_mode[] = {
ISAKMP_PAYLOAD_HASH, /* Initiator -> responder. */
ISAKMP_PAYLOAD_SA,
EXCHANGE_SCRIPT_SWITCH,
ISAKMP_PAYLOAD_HASH, /* Responder -> initiator. */
ISAKMP_PAYLOAD_SA,
EXCHANGE_SCRIPT_END
};
struct dst_spi_proto_arg {
struct sockaddr *dst;
u_int32_t spi;
u_int8_t proto;
};
/*
* Check if SA matches what we are asking for through V_ARG. It has to
* be a finished phase 2 SA.
* if "proto" arg is 0, match any proto
*/
static int
ipsec_sa_check(struct sa *sa, void *v_arg)
{
struct dst_spi_proto_arg *arg = v_arg;
struct proto *proto;
struct sockaddr *dst, *src;
int incoming;
if (sa->phase != 2 || !(sa->flags & SA_FLAG_READY))
return 0;
sa->transport->vtbl->get_dst(sa->transport, &dst);
if (memcmp(sockaddr_addrdata(dst), sockaddr_addrdata(arg->dst),
sockaddr_addrlen(dst)) == 0)
incoming = 0;
else {
sa->transport->vtbl->get_src(sa->transport, &src);
if (memcmp(sockaddr_addrdata(src), sockaddr_addrdata(arg->dst),
sockaddr_addrlen(src)) == 0)
incoming = 1;
else
return 0;
}
for (proto = TAILQ_FIRST(&sa->protos); proto;
proto = TAILQ_NEXT(proto, link))
if ((arg->proto == 0 || proto->proto == arg->proto) &&
memcmp(proto->spi[incoming], &arg->spi, sizeof arg->spi)
== 0)
return 1;
return 0;
}
/* Find an SA with a "name" of DST, SPI & PROTO. */
struct sa *
ipsec_sa_lookup(struct sockaddr *dst, u_int32_t spi, u_int8_t proto)
{
struct dst_spi_proto_arg arg;
arg.dst = dst;
arg.spi = spi;
arg.proto = proto;
return sa_find(ipsec_sa_check, &arg);
}
/*
* Check if SA matches the flow of another SA in V_ARG. It has to
* be a finished non-replaced phase 2 SA.
* XXX At some point other selectors will matter here too.
*/
static int
ipsec_sa_check_flow(struct sa *sa, void *v_arg)
{
if ((sa->flags & (SA_FLAG_READY | SA_FLAG_REPLACED)) != SA_FLAG_READY)
return 0;
return ipsec_sa_check_flow_any(sa, v_arg);
}
static int
ipsec_sa_check_flow_any(struct sa *sa, void *v_arg)
{
struct sa *sa2 = v_arg;
struct ipsec_sa *isa = sa->data, *isa2 = sa2->data;
if (sa == sa2 || sa->phase != 2 ||
(sa->flags & SA_FLAG_READY) != SA_FLAG_READY)
return 0;
if (isa->tproto != isa2->tproto || isa->sport != isa2->sport ||
isa->dport != isa2->dport)
return 0;
/*
* If at least one of the IPsec SAs is incomplete, we're done.
*/
if (isa->src_net == NULL || isa2->src_net == NULL ||
isa->dst_net == NULL || isa2->dst_net == NULL ||
isa->src_mask == NULL || isa2->src_mask == NULL ||
isa->dst_mask == NULL || isa2->dst_mask == NULL)
return 0;
return isa->src_net->sa_family == isa2->src_net->sa_family &&
memcmp(sockaddr_addrdata(isa->src_net),
sockaddr_addrdata(isa2->src_net),
sockaddr_addrlen(isa->src_net)) == 0 &&
memcmp(sockaddr_addrdata(isa->src_mask),
sockaddr_addrdata(isa2->src_mask),
sockaddr_addrlen(isa->src_mask)) == 0 &&
memcmp(sockaddr_addrdata(isa->dst_net),
sockaddr_addrdata(isa2->dst_net),
sockaddr_addrlen(isa->dst_net)) == 0 &&
memcmp(sockaddr_addrdata(isa->dst_mask),
sockaddr_addrdata(isa2->dst_mask),
sockaddr_addrlen(isa->dst_mask)) == 0;
}
/*
* Construct a PF tag if specified in the configuration.
* It is possible to use variables to expand the tag:
* $id The string representation of the remote ID
* $domain The stripped domain part of the ID (for FQDN and UFQDN)
*/
static int
ipsec_sa_tag(struct exchange *exchange, struct sa *sa, struct sa *isakmp_sa)
{
char *format, *section;
char *id_string = NULL, *domain = NULL;
int error = -1;
size_t len;
sa->tag = NULL;
if (exchange->name == NULL ||
(section = exchange->name) == NULL ||
(format = conf_get_str(section, "PF-Tag")) == NULL)
return (0); /* ignore if not present */
len = PF_TAG_NAME_SIZE;
if ((sa->tag = calloc(1, len)) == NULL) {
log_error("ipsec_sa_tag: calloc");
goto fail;
}
if (strlcpy(sa->tag, format, len) >= len) {
log_print("ipsec_sa_tag: tag too long");
goto fail;
}
if (isakmp_sa->initiator)
id_string = ipsec_id_string(isakmp_sa->id_r,
isakmp_sa->id_r_len);
else
id_string = ipsec_id_string(isakmp_sa->id_i,
isakmp_sa->id_i_len);
if (strstr(format, "$id") != NULL) {
if (id_string == NULL) {
log_print("ipsec_sa_tag: cannot get ID");
goto fail;
}
if (expand_string(sa->tag, len, "$id", id_string) != 0) {
log_print("ipsec_sa_tag: failed to expand tag");
goto fail;
}
}
if (strstr(format, "$domain") != NULL) {
if (id_string == NULL) {
log_print("ipsec_sa_tag: cannot get ID");
goto fail;
}
if (strncmp(id_string, "fqdn/", strlen("fqdn/")) == 0)
domain = strchr(id_string, '.');
else if (strncmp(id_string, "ufqdn/", strlen("ufqdn/")) == 0)
domain = strchr(id_string, '@');
if (domain == NULL || strlen(domain) < 2) {
log_print("ipsec_sa_tag: no valid domain in ID %s",
id_string);
goto fail;
}
domain++;
if (expand_string(sa->tag, len, "$domain", domain) != 0) {
log_print("ipsec_sa_tag: failed to expand tag");
goto fail;
}
}
LOG_DBG((LOG_SA, 10, "ipsec_sa_tag: tag_len %ld tag \"%s\"",
strlen(sa->tag), sa->tag));
error = 0;
fail:
free(id_string);
if (error != 0) {
free(sa->tag);
sa->tag = NULL;
}
return (error);
}
/*
* Do IPsec DOI specific finalizations task for the exchange where MSG was
* the final message.
*/
static void
ipsec_finalize_exchange(struct message *msg)
{
struct sa *isakmp_sa = msg->isakmp_sa;
struct ipsec_sa *isa;
struct exchange *exchange = msg->exchange;
struct ipsec_exch *ie = exchange->data;
struct sa *sa = 0, *old_sa;
struct proto *proto, *last_proto = 0;
char *addr1, *addr2, *mask1, *mask2;
switch (exchange->phase) {
case 1:
switch (exchange->type) {
case ISAKMP_EXCH_ID_PROT:
case ISAKMP_EXCH_AGGRESSIVE:
isa = isakmp_sa->data;
isa->hash = ie->hash->type;
isa->prf_type = ie->prf_type;
isa->skeyid_len = ie->skeyid_len;
isa->skeyid_d = ie->skeyid_d;
isa->skeyid_a = ie->skeyid_a;
/* Prevents early free of SKEYID_*. */
ie->skeyid_a = ie->skeyid_d = 0;
/*
* If a lifetime was negotiated setup the expiration
* timers.
*/
if (isakmp_sa->seconds)
sa_setup_expirations(isakmp_sa);
if (isakmp_sa->flags & SA_FLAG_NAT_T_KEEPALIVE)
nat_t_setup_keepalive(isakmp_sa);
break;
}
break;
case 2:
switch (exchange->type) {
case IKE_EXCH_QUICK_MODE:
/*
* Tell the application(s) about the SPIs and key
* material.
*/
for (sa = TAILQ_FIRST(&exchange->sa_list); sa;
sa = TAILQ_NEXT(sa, next)) {
isa = sa->data;
if (exchange->initiator) {
/*
* Initiator is source, responder is
* destination.
*/
if (ipsec_set_network(ie->id_ci,
ie->id_cr, sa)) {
log_print(
"ipsec_finalize_exchange: "
"ipsec_set_network "
"failed");
return;
}
} else {
/*
* Responder is source, initiator is
* destination.
*/
if (ipsec_set_network(ie->id_cr,
ie->id_ci, sa)) {
log_print(
"ipsec_finalize_exchange: "
"ipsec_set_network "
"failed");
return;
}
}
if (ipsec_sa_tag(exchange, sa, isakmp_sa) == -1)
return;
for (proto = TAILQ_FIRST(&sa->protos),
last_proto = 0; proto;
proto = TAILQ_NEXT(proto, link)) {
if (pf_key_v2_set_spi(sa, proto,
0, isakmp_sa) ||
(last_proto &&
pf_key_v2_group_spis(sa,
last_proto, proto, 0)) ||
pf_key_v2_set_spi(sa, proto,
1, isakmp_sa) ||
(last_proto &&
pf_key_v2_group_spis(sa,
last_proto, proto, 1)))
/*
* XXX Tear down this
* exchange.
*/
return;
last_proto = proto;
}
if (sockaddr2text(isa->src_net, &addr1, 0))
addr1 = 0;
if (sockaddr2text(isa->src_mask, &mask1, 0))
mask1 = 0;
if (sockaddr2text(isa->dst_net, &addr2, 0))
addr2 = 0;
if (sockaddr2text(isa->dst_mask, &mask2, 0))
mask2 = 0;
LOG_DBG((LOG_EXCHANGE, 50,
"ipsec_finalize_exchange: src %s %s "
"dst %s %s tproto %u sport %u dport %u",
addr1 ? addr1 : "<??\?>",
mask1 ? mask1 : "<??\?>",
addr2 ? addr2 : "<??\?>",
mask2 ? mask2 : "<??\?>",
isa->tproto, ntohs(isa->sport),
ntohs(isa->dport)));
free(addr1);
free(mask1);
free(addr2);
free(mask2);
/*
* If this is not an SA acquired by the
* kernel, it needs to have a SPD entry
* (a.k.a. flow) set up.
*/
if (!(sa->flags & SA_FLAG_ONDEMAND ||
conf_get_str("General", "Acquire-Only") ||
acquire_only) &&
pf_key_v2_enable_sa(sa, isakmp_sa))
/* XXX Tear down this exchange. */
return;
/*
* Mark elder SAs with the same flow
* information as replaced.
*/
while ((old_sa = sa_find(ipsec_sa_check_flow,
sa)) != 0)
sa_mark_replaced(old_sa);
}
break;
}
}
}
/* Set the client addresses in ISA from SRC_ID and DST_ID. */
static int
ipsec_set_network(u_int8_t *src_id, u_int8_t *dst_id, struct sa *sa)
{
void *src_net, *dst_net;
void *src_mask = NULL, *dst_mask = NULL;
struct sockaddr *addr;
struct proto *proto;
struct ipsec_proto *iproto;
struct ipsec_sa *isa = sa->data;
int src_af, dst_af;
int id;
char *name, *nat = NULL;
u_int8_t *nat_id = NULL;
size_t nat_sz;
if ((name = connection_passive_lookup_by_ids(src_id, dst_id)))
nat = conf_get_str(name, "NAT-ID");
if (nat) {
if ((nat_id = ipsec_build_id(nat, &nat_sz))) {
LOG_DBG((LOG_EXCHANGE, 50, "ipsec_set_network: SRC-NAT:"
" src: %s -> %s", name, nat));
src_id = nat_id;
} else
log_print("ipsec_set_network: ipsec_build_id"
" failed for NAT-ID: %s", nat);
}
if (((proto = TAILQ_FIRST(&sa->protos)) != NULL) &&
((iproto = proto->data) != NULL) &&
(iproto->encap_mode == IPSEC_ENCAP_UDP_ENCAP_TRANSPORT ||
iproto->encap_mode == IPSEC_ENCAP_UDP_ENCAP_TRANSPORT_DRAFT)) {
/*
* For NAT-T with transport mode, we need to use the ISAKMP's
* SA addresses for the flow.
*/
sa->transport->vtbl->get_src(sa->transport, &addr);
src_af = addr->sa_family;
src_net = sockaddr_addrdata(addr);
sa->transport->vtbl->get_dst(sa->transport, &addr);
dst_af = addr->sa_family;
dst_net = sockaddr_addrdata(addr);
} else {
id = GET_ISAKMP_ID_TYPE(src_id);
src_net = src_id + ISAKMP_ID_DATA_OFF;
switch (id) {
case IPSEC_ID_IPV4_ADDR_SUBNET:
src_mask = (u_int8_t *)src_net + sizeof(struct in_addr);
/* FALLTHROUGH */
case IPSEC_ID_IPV4_ADDR:
src_af = AF_INET;
break;
case IPSEC_ID_IPV6_ADDR_SUBNET:
src_mask = (u_int8_t *)src_net +
sizeof(struct in6_addr);
/* FALLTHROUGH */
case IPSEC_ID_IPV6_ADDR:
src_af = AF_INET6;
break;
default:
log_print(
"ipsec_set_network: ID type %d (%s) not supported",
id, constant_name(ipsec_id_cst, id));
return -1;
}
id = GET_ISAKMP_ID_TYPE(dst_id);
dst_net = dst_id + ISAKMP_ID_DATA_OFF;
switch (id) {
case IPSEC_ID_IPV4_ADDR_SUBNET:
dst_mask = (u_int8_t *)dst_net + sizeof(struct in_addr);
/* FALLTHROUGH */
case IPSEC_ID_IPV4_ADDR:
dst_af = AF_INET;
break;
case IPSEC_ID_IPV6_ADDR_SUBNET:
dst_mask = (u_int8_t *)dst_net +
sizeof(struct in6_addr);
/* FALLTHROUGH */
case IPSEC_ID_IPV6_ADDR:
dst_af = AF_INET6;
break;
default:
log_print(
"ipsec_set_network: ID type %d (%s) not supported",
id, constant_name(ipsec_id_cst, id));
return -1;
}
}
/* Set source address/mask. */
switch (src_af) {
case AF_INET:
isa->src_net = calloc(1, sizeof(struct sockaddr_in));
if (!isa->src_net)
goto memfail;
isa->src_net->sa_family = AF_INET;
isa->src_net->sa_len = sizeof(struct sockaddr_in);
isa->src_mask = calloc(1, sizeof(struct sockaddr_in));
if (!isa->src_mask)
goto memfail;
isa->src_mask->sa_family = AF_INET;
isa->src_mask->sa_len = sizeof(struct sockaddr_in);
break;
case AF_INET6:
isa->src_net = calloc(1, sizeof(struct sockaddr_in6));
if (!isa->src_net)
goto memfail;
isa->src_net->sa_family = AF_INET6;
isa->src_net->sa_len = sizeof(struct sockaddr_in6);
isa->src_mask = calloc(1, sizeof(struct sockaddr_in6));
if (!isa->src_mask)
goto memfail;
isa->src_mask->sa_family = AF_INET6;
isa->src_mask->sa_len = sizeof(struct sockaddr_in6);
break;
}
/* Net */
memcpy(sockaddr_addrdata(isa->src_net), src_net,
sockaddr_addrlen(isa->src_net));
/* Mask */
if (src_mask == NULL)
memset(sockaddr_addrdata(isa->src_mask), 0xff,
sockaddr_addrlen(isa->src_mask));
else
memcpy(sockaddr_addrdata(isa->src_mask), src_mask,
sockaddr_addrlen(isa->src_mask));
memcpy(&isa->sport,
src_id + ISAKMP_ID_DOI_DATA_OFF + IPSEC_ID_PORT_OFF,
IPSEC_ID_PORT_LEN);
free(nat_id);
/* Set destination address. */
switch (dst_af) {
case AF_INET:
isa->dst_net = calloc(1, sizeof(struct sockaddr_in));
if (!isa->dst_net)
goto memfail;
isa->dst_net->sa_family = AF_INET;
isa->dst_net->sa_len = sizeof(struct sockaddr_in);
isa->dst_mask = calloc(1, sizeof(struct sockaddr_in));
if (!isa->dst_mask)
goto memfail;
isa->dst_mask->sa_family = AF_INET;
isa->dst_mask->sa_len = sizeof(struct sockaddr_in);
break;
case AF_INET6:
isa->dst_net = calloc(1, sizeof(struct sockaddr_in6));
if (!isa->dst_net)
goto memfail;
isa->dst_net->sa_family = AF_INET6;
isa->dst_net->sa_len = sizeof(struct sockaddr_in6);
isa->dst_mask = calloc(1, sizeof(struct sockaddr_in6));
if (!isa->dst_mask)
goto memfail;
isa->dst_mask->sa_family = AF_INET6;
isa->dst_mask->sa_len = sizeof(struct sockaddr_in6);
break;
}
/* Net */
memcpy(sockaddr_addrdata(isa->dst_net), dst_net,
sockaddr_addrlen(isa->dst_net));
/* Mask */
if (dst_mask == NULL)
memset(sockaddr_addrdata(isa->dst_mask), 0xff,
sockaddr_addrlen(isa->dst_mask));
else
memcpy(sockaddr_addrdata(isa->dst_mask), dst_mask,
sockaddr_addrlen(isa->dst_mask));
memcpy(&isa->tproto, dst_id + ISAKMP_ID_DOI_DATA_OFF +
IPSEC_ID_PROTO_OFF, IPSEC_ID_PROTO_LEN);
memcpy(&isa->dport,
dst_id + ISAKMP_ID_DOI_DATA_OFF + IPSEC_ID_PORT_OFF,
IPSEC_ID_PORT_LEN);
return 0;
memfail:
log_error("ipsec_set_network: calloc () failed");
return -1;
}
/* Free the DOI-specific exchange data pointed to by VIE. */
static void
ipsec_free_exchange_data(void *vie)
{
struct ipsec_exch *ie = vie;
struct isakmp_cfg_attr *attr;
free(ie->sa_i_b);
free(ie->id_ci);
free(ie->id_cr);
free(ie->g_xi);
free(ie->g_xr);
free(ie->g_xy);
free(ie->skeyid);
free(ie->skeyid_d);
free(ie->skeyid_a);
free(ie->skeyid_e);
free(ie->hash_i);
free(ie->hash_r);
if (ie->group)
group_free(ie->group);
for (attr = LIST_FIRST(&ie->attrs); attr;
attr = LIST_FIRST(&ie->attrs)) {
LIST_REMOVE(attr, link);
if (attr->length)
free(attr->value);
free(attr);
}
}
/* Free the DOI-specific SA data pointed to by VISA. */
static void
ipsec_free_sa_data(void *visa)
{
struct ipsec_sa *isa = visa;
free(isa->src_net);
free(isa->src_mask);
free(isa->dst_net);
free(isa->dst_mask);
free(isa->skeyid_a);
free(isa->skeyid_d);
}
/* Free the DOI-specific protocol data of an SA pointed to by VIPROTO. */
static void
ipsec_free_proto_data(void *viproto)
{
struct ipsec_proto *iproto = viproto;
int i;
for (i = 0; i < 2; i++)
free(iproto->keymat[i]);
}
/* Return exchange script based on TYPE. */
static int16_t *
ipsec_exchange_script(u_int8_t type)
{
switch (type) {
case ISAKMP_EXCH_TRANSACTION:
return script_transaction;
case IKE_EXCH_QUICK_MODE:
return script_quick_mode;
case IKE_EXCH_NEW_GROUP_MODE:
return script_new_group_mode;
}
return 0;
}
/* Initialize this DOI, requires doi_init to already have been called. */
void
ipsec_init(void)
{
doi_register(&ipsec_doi);
}
/* Given a message MSG, return a suitable IV (or rather keystate). */
static struct keystate *
ipsec_get_keystate(struct message *msg)
{
struct keystate *ks;
struct hash *hash;
/* If we have already have an IV, use it. */
if (msg->exchange && msg->exchange->keystate) {
ks = malloc(sizeof *ks);
if (!ks) {
log_error("ipsec_get_keystate: malloc (%lu) failed",
(unsigned long) sizeof *ks);
return 0;
}
memcpy(ks, msg->exchange->keystate, sizeof *ks);
return ks;
}
/*
* For phase 2 when no SA yet is setup we need to hash the IV used by
* the ISAKMP SA concatenated with the message ID, and use that as an
* IV for further cryptographic operations.
*/
if (!msg->isakmp_sa->keystate) {
log_print("ipsec_get_keystate: no keystate in ISAKMP SA %p",
msg->isakmp_sa);
return 0;
}
ks = crypto_clone_keystate(msg->isakmp_sa->keystate);
if (!ks)
return 0;
hash = hash_get(((struct ipsec_sa *)msg->isakmp_sa->data)->hash);
hash->Init(hash->ctx);
LOG_DBG_BUF((LOG_CRYPTO, 80, "ipsec_get_keystate: final phase 1 IV",
ks->riv, ks->xf->blocksize));
hash->Update(hash->ctx, ks->riv, ks->xf->blocksize);
LOG_DBG_BUF((LOG_CRYPTO, 80, "ipsec_get_keystate: message ID",
((u_int8_t *) msg->iov[0].iov_base) + ISAKMP_HDR_MESSAGE_ID_OFF,
ISAKMP_HDR_MESSAGE_ID_LEN));
hash->Update(hash->ctx, ((u_int8_t *) msg->iov[0].iov_base) +
ISAKMP_HDR_MESSAGE_ID_OFF, ISAKMP_HDR_MESSAGE_ID_LEN);
hash->Final(hash->digest, hash->ctx);
crypto_init_iv(ks, hash->digest, ks->xf->blocksize);
LOG_DBG_BUF((LOG_CRYPTO, 80, "ipsec_get_keystate: phase 2 IV",
hash->digest, ks->xf->blocksize));
return ks;
}
static void
ipsec_setup_situation(u_int8_t *buf)
{
SET_IPSEC_SIT_SIT(buf + ISAKMP_SA_SIT_OFF, IPSEC_SIT_IDENTITY_ONLY);
}
static size_t
ipsec_situation_size(void)
{
return IPSEC_SIT_SIT_LEN;
}
static u_int8_t
ipsec_spi_size(u_int8_t proto)
{
return IPSEC_SPI_SIZE;
}
static int
ipsec_validate_attribute(u_int16_t type, u_int8_t * value, u_int16_t len,
void *vmsg)
{
struct message *msg = vmsg;
if (msg->exchange->phase == 1 &&
(type < IKE_ATTR_ENCRYPTION_ALGORITHM || type > IKE_ATTR_GROUP_ORDER))
return -1;
if (msg->exchange->phase == 2 &&
(type < IPSEC_ATTR_SA_LIFE_TYPE || type > IPSEC_ATTR_ECN_TUNNEL))
return -1;
return 0;
}
static int
ipsec_validate_exchange(u_int8_t exch)
{
return exch != IKE_EXCH_QUICK_MODE && exch != IKE_EXCH_NEW_GROUP_MODE;
}
static int
ipsec_validate_id_information(u_int8_t type, u_int8_t *extra, u_int8_t *buf,
size_t sz, struct exchange *exchange)
{
u_int8_t proto = GET_IPSEC_ID_PROTO(extra);
u_int16_t port = GET_IPSEC_ID_PORT(extra);
LOG_DBG((LOG_MESSAGE, 40,
"ipsec_validate_id_information: proto %d port %d type %d",
proto, port, type));
if (type < IPSEC_ID_IPV4_ADDR || type > IPSEC_ID_KEY_ID)
return -1;
switch (type) {
case IPSEC_ID_IPV4_ADDR:
LOG_DBG_BUF((LOG_MESSAGE, 40,
"ipsec_validate_id_information: IPv4", buf,
sizeof(struct in_addr)));
break;
case IPSEC_ID_IPV6_ADDR:
LOG_DBG_BUF((LOG_MESSAGE, 40,
"ipsec_validate_id_information: IPv6", buf,
sizeof(struct in6_addr)));
break;
case IPSEC_ID_IPV4_ADDR_SUBNET:
LOG_DBG_BUF((LOG_MESSAGE, 40,
"ipsec_validate_id_information: IPv4 network/netmask",
buf, 2 * sizeof(struct in_addr)));
break;
case IPSEC_ID_IPV6_ADDR_SUBNET:
LOG_DBG_BUF((LOG_MESSAGE, 40,
"ipsec_validate_id_information: IPv6 network/netmask",
buf, 2 * sizeof(struct in6_addr)));
break;
default:
break;
}
if (exchange->phase == 1 &&
(proto != IPPROTO_UDP || port != UDP_DEFAULT_PORT) &&
(proto != 0 || port != 0)) {
/*
* XXX SSH's ISAKMP tester fails this test (proto 17 - port
* 0).
*/
#ifdef notyet
return -1;
#else
log_print("ipsec_validate_id_information: dubious ID "
"information accepted");
#endif
}
/* XXX More checks? */
return 0;
}
static int
ipsec_validate_key_information(u_int8_t *buf, size_t sz)
{
/* XXX Not implemented yet. */
return 0;
}
static int
ipsec_validate_notification(u_int16_t type)
{
return type < IPSEC_NOTIFY_RESPONDER_LIFETIME ||
type > IPSEC_NOTIFY_INITIAL_CONTACT ? -1 : 0;
}
static int
ipsec_validate_proto(u_int8_t proto)
{
return proto < IPSEC_PROTO_IPSEC_AH ||
proto > IPSEC_PROTO_IPCOMP ? -1 : 0;
}
static int
ipsec_validate_situation(u_int8_t *buf, size_t *sz, size_t len)
{
if (len < IPSEC_SIT_SIT_OFF + IPSEC_SIT_SIT_LEN) {
log_print("ipsec_validate_situation: payload too short: %u",
(unsigned int) len);
return -1;
}
/* Currently only "identity only" situations are supported. */
if (GET_IPSEC_SIT_SIT(buf) != IPSEC_SIT_IDENTITY_ONLY)
return 1;
*sz = IPSEC_SIT_SIT_LEN;
return 0;
}
static int