-
Notifications
You must be signed in to change notification settings - Fork 0
/
pf_key_v2.c
3723 lines (3372 loc) · 97.1 KB
/
pf_key_v2.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: pf_key_v2.c,v 1.201 2019/11/29 22:06:19 tobhe Exp $ */
/* $EOM: pf_key_v2.c,v 1.79 2000/12/12 00:33:19 niklas Exp $ */
/*
* Copyright (c) 1999, 2000, 2001 Niklas Hallqvist. All rights reserved.
* Copyright (c) 1999, 2000, 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/ioctl.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <net/pfkeyv2.h>
#include <netinet/in.h>
#ifdef __OpenBSD__
#include <netinet/ip_ipsp.h>
#endif
#include <arpa/inet.h>
#include <stdlib.h>
#include <poll.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <errno.h>
#include <bitstring.h>
#include <inttypes.h>
#ifndef __OpenBSD__
#include <netinet/udp.h>
#endif
#include "cert.h"
#include "conf.h"
#include "connection.h"
#include "exchange.h"
#include "ipsec.h"
#include "ipsec_num.h"
#include "key.h"
#include "log.h"
#include "pf_key_v2.h"
#include "sa.h"
#include "timer.h"
#include "transport.h"
#include "ui.h"
#include "util.h"
#include "policy.h"
#include "udp_encap.h"
#include "openbsd-compat.h"
#define IN6_IS_ADDR_FULL(a) \
((*(u_int32_t *)(void *)(&(a)->s6_addr[0]) == 0xffffffff) && \
(*(u_int32_t *)(void *)(&(a)->s6_addr[4]) == 0xffffffff) && \
(*(u_int32_t *)(void *)(&(a)->s6_addr[8]) == 0xffffffff) && \
(*(u_int32_t *)(void *)(&(a)->s6_addr[12]) == 0xffffffff))
#define ADDRESS_MAX sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"
/*
* PF_KEY v2 always work with 64-bit entities and aligns on 64-bit boundaries.
*/
#define PF_KEY_V2_CHUNK 8
#define PF_KEY_V2_ROUND(x) \
(((x) + PF_KEY_V2_CHUNK - 1) & ~(PF_KEY_V2_CHUNK - 1))
/* How many microseconds we will wait for a reply from the PF_KEY socket. */
#define PF_KEY_REPLY_TIMEOUT 1000
/* Structure of two sockaddresses, used for NetBSD policy extension */
struct concat_sockaddr {
struct sockaddr src;
struct sockaddr dst;
};
struct pf_key_v2_node {
TAILQ_ENTRY(pf_key_v2_node) link;
void *seg;
size_t sz;
int cnt;
u_int16_t type;
u_int8_t flags;
};
TAILQ_HEAD(pf_key_v2_msg, pf_key_v2_node);
#define PF_KEY_V2_NODE_MALLOCED 1
#define PF_KEY_V2_NODE_MARK 2
/* Used to derive "unique" connection identifiers. */
int connection_seq = 0;
static u_int8_t *pf_key_v2_convert_id(u_int8_t *, int, size_t *, int *);
static struct pf_key_v2_msg *pf_key_v2_call(struct pf_key_v2_msg *);
static struct pf_key_v2_node *pf_key_v2_find_ext(struct pf_key_v2_msg *,
u_int16_t);
static void pf_key_v2_notify(struct pf_key_v2_msg *);
static struct pf_key_v2_msg *pf_key_v2_read(u_int32_t);
static u_int32_t pf_key_v2_seq(void);
static u_int32_t pf_key_v2_write(struct pf_key_v2_msg *);
static int pf_key_v2_remove_conf(char *);
static int pf_key_v2_conf_refhandle(int, char *);
static int pf_key_v2_conf_refinc(int, char *);
static uint8_t mask2prefix(struct sockaddr *);
static int
pf_key_v2_flow(struct sockaddr *laddr, struct sockaddr *lmask,
struct sockaddr *raddr, struct sockaddr *rmask,
u_int8_t tproto, u_int16_t sport, u_int16_t dport,
u_int8_t *spi, u_int8_t proto, struct sockaddr *dst,
struct sockaddr *src, int delete, int ingress,
u_int8_t srcid_type, u_int8_t *srcid, int srcid_len,
u_int8_t dstid_type, u_int8_t *dstid, int dstid_len,
struct ipsec_proto *iproto);
/* The socket to use for PF_KEY interactions. */
int pf_key_v2_socket;
static struct pf_key_v2_msg *
pf_key_v2_msg_new(struct sadb_msg *msg, int flags)
{
struct pf_key_v2_node *node;
struct pf_key_v2_msg *ret;
node = malloc(sizeof *node);
if (!node)
goto cleanup;
ret = malloc(sizeof *ret);
if (!ret)
goto cleanup;
TAILQ_INIT(ret);
node->seg = msg;
node->sz = sizeof *msg;
node->type = 0;
node->cnt = 1;
node->flags = flags;
TAILQ_INSERT_HEAD(ret, node, link);
return ret;
cleanup:
free(node);
return 0;
}
/* Add a SZ sized segment SEG to the PF_KEY message MSG. */
static int
pf_key_v2_msg_add(struct pf_key_v2_msg *msg, struct sadb_ext *ext, int flags)
{
struct pf_key_v2_node *node;
node = malloc(sizeof *node);
if (!node)
return -1;
node->seg = ext;
node->sz = ext->sadb_ext_len * PF_KEY_V2_CHUNK;
node->type = ext->sadb_ext_type;
node->flags = flags;
TAILQ_FIRST(msg)->cnt++;
TAILQ_INSERT_TAIL(msg, node, link);
return 0;
}
/* Deallocate the PF_KEY message MSG. */
static void
pf_key_v2_msg_free(struct pf_key_v2_msg *msg)
{
struct pf_key_v2_node *np;
np = TAILQ_FIRST(msg);
while (np) {
TAILQ_REMOVE(msg, np, link);
if (np->flags & PF_KEY_V2_NODE_MALLOCED)
free(np->seg);
free(np);
np = TAILQ_FIRST(msg);
}
free(msg);
}
/* Just return a new sequence number. */
static u_int32_t
pf_key_v2_seq(void)
{
static u_int32_t seq = 0;
return ++seq;
}
/*
* Read a PF_KEY packet with SEQ as the sequence number, looping if necessary.
* If SEQ is zero just read the first message we see, otherwise we queue
* messages up until both the PID and the sequence number match.
*/
static struct pf_key_v2_msg *
pf_key_v2_read(u_int32_t seq)
{
ssize_t n;
u_int8_t *buf = 0;
struct pf_key_v2_msg *ret = 0;
struct sadb_msg *msg;
struct sadb_msg hdr;
struct sadb_ext *ext;
struct timespec ts;
struct pollfd pfd[1];
pfd[0].fd = pf_key_v2_socket;
pfd[0].events = POLLIN;
while (1) {
/*
* If this is a read of a reply we should actually expect the
* reply to get lost as PF_KEY is an unreliable service per
* the specs. Currently we do this by setting a short timeout,
* and if it is not readable in that time, we fail the read.
*/
if (seq) {
n = poll(pfd, 1, PF_KEY_REPLY_TIMEOUT / 1000);
if (n == -1) {
log_error("pf_key_v2_read: poll() failed");
goto cleanup;
}
if (!n) {
log_print("pf_key_v2_read: "
"no reply from PF_KEY");
goto cleanup;
}
}
n = recv(pf_key_v2_socket, &hdr, sizeof hdr, MSG_PEEK);
if (n == -1) {
log_error("pf_key_v2_read: recv (%d, ...) failed",
pf_key_v2_socket);
goto cleanup;
}
if (n != sizeof hdr) {
log_error("pf_key_v2_read: recv (%d, ...) "
"returned short packet (%lu bytes)",
pf_key_v2_socket, (unsigned long) n);
goto cleanup;
}
buf = reallocarray(NULL, hdr.sadb_msg_len, PF_KEY_V2_CHUNK);
if (!buf) {
log_error("pf_key_v2_read: reallocarray (%d, %d) failed",
hdr.sadb_msg_len, PF_KEY_V2_CHUNK);
goto cleanup;
}
n = hdr.sadb_msg_len * PF_KEY_V2_CHUNK;
n = read(pf_key_v2_socket, buf, n);
if (n == -1) {
log_error("pf_key_v2_read: read (%d, ...) failed",
pf_key_v2_socket);
goto cleanup;
}
if (n != hdr.sadb_msg_len * PF_KEY_V2_CHUNK) {
log_print("pf_key_v2_read: read (%d, ...) "
"returned short packet (%lu bytes)",
pf_key_v2_socket, (unsigned long) n);
goto cleanup;
}
LOG_DBG_BUF((LOG_SYSDEP, 80, "pf_key_v2_read: msg", buf, n));
/* We drop all messages that is not what we expect. */
msg = (struct sadb_msg *) buf;
if (msg->sadb_msg_version != PF_KEY_V2 ||
(msg->sadb_msg_pid != 0 &&
msg->sadb_msg_pid != (u_int32_t) getpid())) {
if (seq) {
free(buf);
buf = 0;
continue;
} else {
LOG_DBG((LOG_SYSDEP, 90, "pf_key_v2_read:"
"bad version (%d) or PID (%d, mine is "
"%ld), ignored", msg->sadb_msg_version,
msg->sadb_msg_pid, (long) getpid()));
goto cleanup;
}
}
/* Parse the message. */
ret = pf_key_v2_msg_new(msg, PF_KEY_V2_NODE_MALLOCED);
if (!ret)
goto cleanup;
buf = 0;
for (ext = (struct sadb_ext *) (msg + 1);
(u_int8_t *) ext - (u_int8_t *) msg <
msg->sadb_msg_len * PF_KEY_V2_CHUNK;
ext = (struct sadb_ext *) ((u_int8_t *) ext +
ext->sadb_ext_len * PF_KEY_V2_CHUNK))
pf_key_v2_msg_add(ret, ext, 0);
/*
* If the message is not the one we are waiting for, queue it
* up.
*/
if (seq && (msg->sadb_msg_pid != (u_int32_t) getpid() ||
msg->sadb_msg_seq != seq)) {
clock_gettime(CLOCK_MONOTONIC, &ts);
timer_add_event("pf_key_v2_notify",
(void (*) (void *)) pf_key_v2_notify, ret, &ts);
ret = 0;
continue;
}
return ret;
}
cleanup:
free(buf);
if (ret)
pf_key_v2_msg_free(ret);
return 0;
}
/* Write the message in PMSG to the PF_KEY socket. */
u_int32_t
pf_key_v2_write(struct pf_key_v2_msg *pmsg)
{
struct iovec *iov = 0;
ssize_t n;
size_t len;
int i, cnt = TAILQ_FIRST(pmsg)->cnt;
char header[80];
struct sadb_msg *msg = TAILQ_FIRST(pmsg)->seg;
struct pf_key_v2_node *np = TAILQ_FIRST(pmsg);
iov = calloc(cnt, sizeof *iov);
if (!iov) {
log_error("pf_key_v2_write: malloc (%lu) failed",
cnt * (unsigned long) sizeof *iov);
return 0;
}
msg->sadb_msg_version = PF_KEY_V2;
msg->sadb_msg_errno = 0;
msg->sadb_msg_reserved = 0;
msg->sadb_msg_pid = getpid();
if (!msg->sadb_msg_seq)
msg->sadb_msg_seq = pf_key_v2_seq();
/* Compute the iovec segments as well as the message length. */
len = 0;
for (i = 0; i < cnt; i++) {
iov[i].iov_base = np->seg;
len += iov[i].iov_len = np->sz;
/*
* XXX One can envision setting specific extension fields,
* like *_reserved ones here. For now we require them to be
* set by the caller.
*/
np = TAILQ_NEXT(np, link);
}
msg->sadb_msg_len = len / PF_KEY_V2_CHUNK;
for (i = 0; i < cnt; i++) {
snprintf(header, sizeof header, "pf_key_v2_write: iov[%d]", i);
LOG_DBG_BUF((LOG_SYSDEP, 80, header,
(u_int8_t *) iov[i].iov_base, iov[i].iov_len));
}
do {
n = writev(pf_key_v2_socket, iov, cnt);
} while (n == -1 && (errno == EAGAIN || errno == EINTR));
if (n == -1) {
log_error("pf_key_v2_write: writev (%d, %p, %d) failed",
pf_key_v2_socket, iov, cnt);
goto cleanup;
}
if ((size_t) n != len) {
log_error("pf_key_v2_write: "
"writev (%d, ...) returned prematurely (%lu)",
pf_key_v2_socket, (unsigned long) n);
goto cleanup;
}
free(iov);
return msg->sadb_msg_seq;
cleanup:
free(iov);
return 0;
}
/*
* Do a PF_KEY "call", i.e. write a message MSG, read the reply and return
* it to the caller.
*/
static struct pf_key_v2_msg *
pf_key_v2_call(struct pf_key_v2_msg *msg)
{
u_int32_t seq;
seq = pf_key_v2_write(msg);
if (!seq)
return 0;
return pf_key_v2_read(seq);
}
/* Find the TYPE extension in MSG. Return zero if none found. */
static struct pf_key_v2_node *
pf_key_v2_find_ext(struct pf_key_v2_msg *msg, u_int16_t type)
{
struct pf_key_v2_node *ext;
for (ext = TAILQ_NEXT(TAILQ_FIRST(msg), link); ext;
ext = TAILQ_NEXT(ext, link))
if (ext->type == type)
return ext;
return 0;
}
/*
* Open the PF_KEYv2 sockets and return the descriptor used for notifies.
* Return -1 for failure and -2 if no notifies will show up.
*/
int
pf_key_v2_open(void)
{
int fd = -1, err;
struct sadb_msg msg;
struct pf_key_v2_msg *regmsg = 0, *ret = 0;
/* Open the socket we use to speak to IPsec. */
pf_key_v2_socket = -1;
fd = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
if (fd == -1) {
log_error("pf_key_v2_open: "
"socket (PF_KEY, SOCK_RAW, PF_KEY_V2) failed");
goto cleanup;
}
pf_key_v2_socket = fd;
/* Register it to get ESP and AH acquires from the kernel. */
msg.sadb_msg_seq = 0;
msg.sadb_msg_type = SADB_REGISTER;
msg.sadb_msg_satype = SADB_SATYPE_ESP;
regmsg = pf_key_v2_msg_new(&msg, 0);
if (!regmsg)
goto cleanup;
ret = pf_key_v2_call(regmsg);
pf_key_v2_msg_free(regmsg);
if (!ret)
goto cleanup;
err = ((struct sadb_msg *)TAILQ_FIRST(ret)->seg)->sadb_msg_errno;
if (err) {
log_print("pf_key_v2_open: REGISTER: %s", strerror(err));
goto cleanup;
}
/* XXX Register the accepted transforms. */
pf_key_v2_msg_free(ret);
ret = 0;
msg.sadb_msg_seq = 0;
msg.sadb_msg_type = SADB_REGISTER;
msg.sadb_msg_satype = SADB_SATYPE_AH;
regmsg = pf_key_v2_msg_new(&msg, 0);
if (!regmsg)
goto cleanup;
ret = pf_key_v2_call(regmsg);
pf_key_v2_msg_free(regmsg);
if (!ret)
goto cleanup;
err = ((struct sadb_msg *)TAILQ_FIRST(ret)->seg)->sadb_msg_errno;
if (err) {
log_print("pf_key_v2_open: REGISTER: %s", strerror(err));
goto cleanup;
}
/* XXX Register the accepted transforms. */
pf_key_v2_msg_free(ret);
ret = 0;
msg.sadb_msg_seq = 0;
msg.sadb_msg_type = SADB_REGISTER;
msg.sadb_msg_satype = SADB_X_SATYPE_IPCOMP;
regmsg = pf_key_v2_msg_new(&msg, 0);
if (!regmsg)
goto cleanup;
ret = pf_key_v2_call(regmsg);
pf_key_v2_msg_free(regmsg);
if (!ret)
goto cleanup;
err = ((struct sadb_msg *)TAILQ_FIRST(ret)->seg)->sadb_msg_errno;
if (err) {
log_print("pf_key_v2_open: REGISTER: %s", strerror(err));
goto cleanup;
}
/* XXX Register the accepted transforms. */
pf_key_v2_msg_free(ret);
return fd;
cleanup:
if (pf_key_v2_socket != -1) {
close(pf_key_v2_socket);
pf_key_v2_socket = -1;
}
if (ret)
pf_key_v2_msg_free(ret);
return -1;
}
/*
* Generate a SPI for protocol PROTO and the source/destination pair given by
* SRC, SRCLEN, DST & DSTLEN. Stash the SPI size in SZ.
*/
u_int8_t *
pf_key_v2_get_spi(size_t *sz, u_int8_t proto, struct sockaddr *src,
struct sockaddr *dst, u_int32_t seq)
{
struct sadb_msg msg;
struct sadb_sa *sa;
struct sadb_address *addr = 0;
struct sadb_spirange spirange;
struct pf_key_v2_msg *getspi = 0, *ret = 0;
struct pf_key_v2_node *ext;
u_int8_t *spi = 0;
int len, err;
msg.sadb_msg_type = SADB_GETSPI;
switch (proto) {
case IPSEC_PROTO_IPSEC_ESP:
msg.sadb_msg_satype = SADB_SATYPE_ESP;
break;
case IPSEC_PROTO_IPSEC_AH:
msg.sadb_msg_satype = SADB_SATYPE_AH;
break;
case IPSEC_PROTO_IPCOMP:
msg.sadb_msg_satype = SADB_X_SATYPE_IPCOMP;
break;
default:
log_print("pf_key_v2_get_spi: invalid proto %d", proto);
goto cleanup;
}
/* Set the sequence number from the ACQUIRE message. */
msg.sadb_msg_seq = seq;
getspi = pf_key_v2_msg_new(&msg, 0);
if (!getspi)
goto cleanup;
/* Setup the ADDRESS extensions. */
len =
sizeof(struct sadb_address) + PF_KEY_V2_ROUND(SA_LEN(src));
addr = calloc(1, len);
if (!addr)
goto cleanup;
addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
addr->sadb_address_len = len / PF_KEY_V2_CHUNK;
addr->sadb_address_reserved = 0;
memcpy(addr + 1, src, SA_LEN(src));
switch (((struct sockaddr *) (addr + 1))->sa_family) {
case AF_INET:
((struct sockaddr_in *) (addr + 1))->sin_port = 0;
break;
case AF_INET6:
((struct sockaddr_in6 *) (addr + 1))->sin6_port = 0;
break;
}
if (pf_key_v2_msg_add(getspi, (struct sadb_ext *) addr,
PF_KEY_V2_NODE_MALLOCED) == -1)
goto cleanup;
addr = 0;
len = sizeof(struct sadb_address) + PF_KEY_V2_ROUND(SA_LEN(dst));
addr = calloc(1, len);
if (!addr)
goto cleanup;
addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
addr->sadb_address_len = len / PF_KEY_V2_CHUNK;
addr->sadb_address_reserved = 0;
memcpy(addr + 1, dst, SA_LEN(dst));
switch (((struct sockaddr *) (addr + 1))->sa_family) {
case AF_INET:
((struct sockaddr_in *) (addr + 1))->sin_port = 0;
break;
case AF_INET6:
((struct sockaddr_in6 *) (addr + 1))->sin6_port = 0;
break;
}
if (pf_key_v2_msg_add(getspi, (struct sadb_ext *) addr,
PF_KEY_V2_NODE_MALLOCED) == -1)
goto cleanup;
addr = 0;
/* Setup the SPIRANGE extension. */
spirange.sadb_spirange_exttype = SADB_EXT_SPIRANGE;
spirange.sadb_spirange_len = sizeof spirange / PF_KEY_V2_CHUNK;
#ifdef CPI_RESERVED_MAX
if (proto == IPSEC_PROTO_IPCOMP) {
spirange.sadb_spirange_min = CPI_RESERVED_MAX + 1;
spirange.sadb_spirange_max = CPI_PRIVATE_MIN - 1;
} else
#endif
{
spirange.sadb_spirange_min = IPSEC_SPI_LOW;
spirange.sadb_spirange_max = 0xffffffff;
}
spirange.sadb_spirange_reserved = 0;
if (pf_key_v2_msg_add(getspi, (struct sadb_ext *)&spirange, 0) == -1)
goto cleanup;
ret = pf_key_v2_call(getspi);
pf_key_v2_msg_free(getspi);
getspi = 0;
if (!ret)
goto cleanup;
err = ((struct sadb_msg *)TAILQ_FIRST(ret)->seg)->sadb_msg_errno;
if (err) {
log_print("pf_key_v2_get_spi: GETSPI: %s", strerror(err));
goto cleanup;
}
ext = pf_key_v2_find_ext(ret, SADB_EXT_SA);
if (!ext) {
log_print("pf_key_v2_get_spi: no SA extension found");
goto cleanup;
}
sa = ext->seg;
/* IPCOMP CPIs are only 16 bits long. */
*sz = (proto == IPSEC_PROTO_IPCOMP) ? sizeof(u_int16_t)
: sizeof sa->sadb_sa_spi;
spi = malloc(*sz);
if (!spi)
goto cleanup;
/* XXX This is ugly. */
if (proto == IPSEC_PROTO_IPCOMP) {
u_int32_t tspi = ntohl(sa->sadb_sa_spi);
*(u_int16_t *) spi = htons((u_int16_t) tspi);
} else
memcpy(spi, &sa->sadb_sa_spi, *sz);
pf_key_v2_msg_free(ret);
LOG_DBG_BUF((LOG_SYSDEP, 50, "pf_key_v2_get_spi: spi", spi, *sz));
return spi;
cleanup:
free(spi);
free(addr);
if (getspi)
pf_key_v2_msg_free(getspi);
if (ret)
pf_key_v2_msg_free(ret);
return 0;
}
/* Fetch SA information from the kernel. XXX OpenBSD only? */
struct sa_kinfo *
pf_key_v2_get_kernel_sa(u_int8_t *spi, size_t spi_sz, u_int8_t proto,
struct sockaddr *dst)
{
struct sadb_msg msg;
struct sadb_sa *ssa;
struct sadb_address *addr = 0;
struct sockaddr *sa;
struct sadb_lifetime *life;
struct pf_key_v2_msg *gettdb = 0, *ret = 0;
struct pf_key_v2_node *ext;
static struct sa_kinfo ksa;
#ifdef SADB_X_EXT_UDPENCAP
struct sadb_x_udpencap *udpencap;
#endif
int len, err;
if (spi_sz != sizeof (ssa->sadb_sa_spi))
return 0;
msg.sadb_msg_type = SADB_GET;
switch (proto) {
case IPSEC_PROTO_IPSEC_ESP:
msg.sadb_msg_satype = SADB_SATYPE_ESP;
break;
case IPSEC_PROTO_IPSEC_AH:
msg.sadb_msg_satype = SADB_SATYPE_AH;
break;
case IPSEC_PROTO_IPCOMP:
msg.sadb_msg_satype = SADB_X_SATYPE_IPCOMP;
break;
default:
log_print("pf_key_v2_get_kernel_sa: invalid proto %d", proto);
goto cleanup;
}
gettdb = pf_key_v2_msg_new(&msg, 0);
if (!gettdb)
goto cleanup;
/* SPI */
ssa = calloc(1, sizeof *ssa);
if (!ssa) {
log_print("pf_key_v2_get_kernel_sa: calloc(1, %lu) failed",
(unsigned long)sizeof *ssa);
goto cleanup;
}
ssa->sadb_sa_exttype = SADB_EXT_SA;
ssa->sadb_sa_len = sizeof *ssa / PF_KEY_V2_CHUNK;
memcpy(&ssa->sadb_sa_spi, spi, sizeof ssa->sadb_sa_spi);
ssa->sadb_sa_state = SADB_SASTATE_MATURE;
if (pf_key_v2_msg_add(gettdb, (struct sadb_ext *)ssa,
PF_KEY_V2_NODE_MALLOCED) == -1)
goto cleanup;
ssa = 0;
/* Address */
len =
sizeof(struct sadb_address) + PF_KEY_V2_ROUND(SA_LEN(dst));
addr = calloc(1, len);
if (!addr)
goto cleanup;
addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
addr->sadb_address_len = len / PF_KEY_V2_CHUNK;
addr->sadb_address_reserved = 0;
memcpy(addr + 1, dst, SA_LEN(dst));
switch (((struct sockaddr *) (addr + 1))->sa_family) {
case AF_INET:
((struct sockaddr_in *) (addr + 1))->sin_port = 0;
break;
case AF_INET6:
((struct sockaddr_in6 *) (addr + 1))->sin6_port = 0;
break;
}
if (pf_key_v2_msg_add(gettdb, (struct sadb_ext *)addr,
PF_KEY_V2_NODE_MALLOCED) == -1)
goto cleanup;
addr = 0;
ret = pf_key_v2_call(gettdb);
pf_key_v2_msg_free(gettdb);
gettdb = 0;
if (!ret)
goto cleanup;
err = ((struct sadb_msg *)TAILQ_FIRST(ret)->seg)->sadb_msg_errno;
if (err) {
log_print("pf_key_v2_get_kernel_sa: SADB_GET: %s",
strerror(err));
goto cleanup;
}
/* Extract the data. */
bzero(&ksa, sizeof ksa);
ext = pf_key_v2_find_ext(ret, SADB_EXT_SA);
if (!ext)
goto cleanup;
ssa = (struct sadb_sa *)ext;
ksa.spi = ssa->sadb_sa_spi;
ksa.wnd = ssa->sadb_sa_replay;
ksa.flags = ssa->sadb_sa_flags;
ext = pf_key_v2_find_ext(ret, SADB_EXT_LIFETIME_CURRENT);
if (ext) {
life = (struct sadb_lifetime *)ext->seg;
ksa.cur_allocations = life->sadb_lifetime_allocations;
ksa.cur_bytes = life->sadb_lifetime_bytes;
ksa.first_use = life->sadb_lifetime_usetime;
ksa.established = life->sadb_lifetime_addtime;
}
ext = pf_key_v2_find_ext(ret, SADB_EXT_LIFETIME_SOFT);
if (ext) {
life = (struct sadb_lifetime *)ext->seg;
ksa.soft_allocations = life->sadb_lifetime_allocations;
ksa.soft_bytes = life->sadb_lifetime_bytes;
ksa.soft_timeout = life->sadb_lifetime_addtime;
ksa.soft_first_use = life->sadb_lifetime_usetime;
}
ext = pf_key_v2_find_ext(ret, SADB_EXT_LIFETIME_HARD);
if (ext) {
life = (struct sadb_lifetime *)ext->seg;
ksa.exp_allocations = life->sadb_lifetime_allocations;
ksa.exp_bytes = life->sadb_lifetime_bytes;
ksa.exp_timeout = life->sadb_lifetime_addtime;
ksa.exp_first_use = life->sadb_lifetime_usetime;
}
#ifdef SADB_X_EXT_LIFETIME_LASTUSE
ext = pf_key_v2_find_ext(ret, SADB_X_EXT_LIFETIME_LASTUSE);
if (ext) {
life = (struct sadb_lifetime *)ext->seg;
ksa.last_used = life->sadb_lifetime_usetime;
}
#endif
ext = pf_key_v2_find_ext(ret, SADB_EXT_ADDRESS_SRC);
if (ext) {
sa = (struct sockaddr *)ext->seg;
memcpy(&ksa.src, sa,
sa->sa_family == AF_INET ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6));
}
ext = pf_key_v2_find_ext(ret, SADB_EXT_ADDRESS_DST);
if (ext) {
sa = (struct sockaddr *)ext->seg;
memcpy(&ksa.dst, sa,
sa->sa_family == AF_INET ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6));
}
#ifdef SADB_X_EXT_UDPENCAP
ext = pf_key_v2_find_ext(ret, SADB_X_EXT_UDPENCAP);
if (ext) {
udpencap = (struct sadb_x_udpencap *)ext->seg;
ksa.udpencap_port = udpencap->sadb_x_udpencap_port;
}
#endif
pf_key_v2_msg_free(ret);
LOG_DBG_BUF((LOG_SYSDEP, 50, "pf_key_v2_get_kernel_sa: spi", spi,
spi_sz));
return &ksa;
cleanup:
free(addr);
if (gettdb)
pf_key_v2_msg_free(gettdb);
if (ret)
pf_key_v2_msg_free(ret);
return 0;
}
static void
pf_key_v2_setup_sockaddr(void *res, struct sockaddr *src,
struct sockaddr *dst, in_port_t port, int ingress)
{
struct sockaddr_in *ip4_sa;
struct sockaddr_in6 *ip6_sa;
u_int8_t *p;
switch (src->sa_family) {
case AF_INET:
ip4_sa = (struct sockaddr_in *) res;
ip4_sa->sin_family = AF_INET;
ip4_sa->sin_len = sizeof *ip4_sa;
ip4_sa->sin_port = port;
if (dst)
p = (u_int8_t *) (ingress ?
&((struct sockaddr_in *)src)->sin_addr.s_addr :
&((struct sockaddr_in *)dst)->sin_addr.s_addr);
else
p = (u_int8_t *)&((struct sockaddr_in *)src)->sin_addr.s_addr;
ip4_sa->sin_addr.s_addr = *((in_addr_t *) p);
break;
case AF_INET6:
ip6_sa = (struct sockaddr_in6 *) res;
ip6_sa->sin6_family = AF_INET6;
ip6_sa->sin6_len = sizeof *ip6_sa;
ip6_sa->sin6_port = port;
if (dst)
p = (u_int8_t *) (ingress ?
&((struct sockaddr_in6 *)src)->sin6_addr.s6_addr :
&((struct sockaddr_in6 *)dst)->sin6_addr.s6_addr);
else
p = (u_int8_t *)&((struct sockaddr_in6 *)src)->sin6_addr.s6_addr;
memcpy(ip6_sa->sin6_addr.s6_addr, p, sizeof(struct in6_addr));
break;
default:
log_print("pf_key_v2_setup_sockaddr: unknown family %d\n",
src->sa_family);
break;
}
}
/*
* Store/update a PF_KEY_V2 security association with full information from the
* IKE SA and PROTO into the kernel. INCOMING is set if we are setting the
* parameters for the incoming SA, and cleared otherwise.
*/
int
pf_key_v2_set_spi(struct sa *sa, struct proto *proto, int incoming,
struct sa *isakmp_sa)
{
struct sadb_msg msg;
struct sadb_sa ssa;
#ifdef SADB_X_EXT_TAG
struct sadb_x_tag *stag = NULL;
#endif
struct sadb_lifetime *life = 0;
struct sadb_address *addr = 0;
struct sadb_key *key = 0;
#ifdef __OpenBSD__
struct sadb_ident *sid = 0;
u_int8_t *pp;
int idtype;
#endif
struct sockaddr *src, *dst;
struct pf_key_v2_msg *update = 0, *ret = 0;
struct ipsec_proto *iproto = proto->data;
size_t len;
int keylen, hashlen, err;
struct ipsec_sa *isa = sa->data;
#ifdef SADB_X_EXT_FLOW_TYPE
struct sadb_protocol flowtype, tprotocol;
#endif
#ifdef SADB_X_EXT_UDPENCAP
struct sadb_x_udpencap udpencap;
#endif
#ifdef SADB_X_EXT_NAT_T_TYPE
struct sadb_x_nat_t_type nat_type;
struct sadb_x_nat_t_port nat_sport, nat_dport;
#endif
#ifndef __OpenBSD__
struct sadb_x_sa2 sa_2;
int error;
#endif
char *addr_str;
msg.sadb_msg_type = incoming ? SADB_UPDATE : SADB_ADD;
switch (proto->proto) {
case IPSEC_PROTO_IPSEC_ESP:
msg.sadb_msg_satype = SADB_SATYPE_ESP;
keylen = ipsec_esp_enckeylength(proto);
hashlen = ipsec_esp_authkeylength(proto);
switch (proto->id) {
case IPSEC_ESP_3DES:
ssa.sadb_sa_encrypt = SADB_EALG_3DESCBC;
break;
case IPSEC_ESP_AES:
ssa.sadb_sa_encrypt = SADB_X_EALG_AES;
break;
case IPSEC_ESP_AES_CTR:
ssa.sadb_sa_encrypt = SADB_X_EALG_AESCTR;
break;
case IPSEC_ESP_AES_GCM_16:
ssa.sadb_sa_encrypt = SADB_X_EALG_AESGCM16;
break;
case IPSEC_ESP_AES_GMAC:
ssa.sadb_sa_encrypt = SADB_X_EALG_AESGMAC;
break;
#ifdef SADB_X_EALG_CAST
case IPSEC_ESP_CAST:
ssa.sadb_sa_encrypt = SADB_X_EALG_CAST;
break;
#endif
#ifdef SADB_X_EALG_BLF
case IPSEC_ESP_BLOWFISH:
ssa.sadb_sa_encrypt = SADB_X_EALG_BLF;
break;
#endif
case IPSEC_ESP_NULL:
ssa.sadb_sa_encrypt = SADB_EALG_NULL;
break;
default:
LOG_DBG((LOG_SYSDEP, 50, "pf_key_v2_set_spi: "
"unknown encryption algorithm %d", proto->id));
return -1;
}
switch (iproto->auth) {
case IPSEC_AUTH_HMAC_MD5:
ssa.sadb_sa_auth = SADB_AALG_MD5HMAC;
break;
case IPSEC_AUTH_HMAC_SHA:
ssa.sadb_sa_auth = SADB_AALG_SHA1HMAC;
break;