removed NAT_TRAVERSAL compile option
[strongswan.git] / src / libfreeswan / pfkey_v2_build.c
1 /*
2 * RFC2367 PF_KEYv2 Key management API message parser
3 * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * RCSID $Id: pfkey_v2_build.c,v 1.4 2005/04/07 19:43:52 as Exp $
16 */
17
18 /*
19 * Template from klips/net/ipsec/ipsec/ipsec_parser.c.
20 */
21
22 char pfkey_v2_build_c_version[] = "$Id: pfkey_v2_build.c,v 1.4 2005/04/07 19:43:52 as Exp $";
23
24 /*
25 * Some ugly stuff to allow consistent debugging code for use in the
26 * kernel and in user space
27 */
28
29 #ifdef __KERNEL__
30
31 # include <linux/kernel.h> /* for printk */
32
33 # include "freeswan/ipsec_kversion.h" /* for malloc switch */
34 # ifdef MALLOC_SLAB
35 # include <linux/slab.h> /* kmalloc() */
36 # else /* MALLOC_SLAB */
37 # include <linux/malloc.h> /* kmalloc() */
38 # endif /* MALLOC_SLAB */
39 # include <linux/errno.h> /* error codes */
40 # include <linux/types.h> /* size_t */
41 # include <linux/interrupt.h> /* mark_bh */
42
43 # include <linux/netdevice.h> /* struct device, and other headers */
44 # include <linux/etherdevice.h> /* eth_type_trans */
45 # include <linux/ip.h> /* struct iphdr */
46 # if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
47 # include <linux/ipv6.h> /* struct ipv6hdr */
48 # endif /* if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
49
50 # define MALLOC(size) kmalloc(size, GFP_ATOMIC)
51 # define FREE(obj) kfree(obj)
52 # include <freeswan.h>
53 #else /* __KERNEL__ */
54
55 # include <sys/types.h>
56 # include <linux/types.h>
57 # include <linux/errno.h>
58 # include <malloc.h>
59 # include <string.h> /* memset */
60
61 # include <freeswan.h>
62 unsigned int pfkey_lib_debug = 0;
63
64 void (*pfkey_debug_func)(const char *message, ...) PRINTF_LIKE(1);
65
66 /* #define PLUTO */
67
68 #define DEBUGGING(args...) if(pfkey_lib_debug) { \
69 if(pfkey_debug_func != NULL) { \
70 (*pfkey_debug_func)("pfkey_lib_debug:" args); \
71 } else { \
72 printf("pfkey_lib_debug:" args); \
73 } }
74 # define MALLOC(size) malloc(size)
75 # define FREE(obj) free(obj)
76 #endif /* __KERNEL__ */
77
78 #include <pfkeyv2.h>
79 #include <pfkey.h>
80
81 #ifdef __KERNEL__
82
83 #include "freeswan/radij.h" /* rd_nodes */
84 #include "freeswan/ipsec_encap.h" /* sockaddr_encap */
85
86 # define DEBUGGING(args...) \
87 KLIPS_PRINT(debug_pfkey, "klips_debug:" args)
88 #endif /* __KERNEL__ */
89
90 #include "ipsec_sa.h" /* IPSEC_SAREF_NULL, IPSEC_SA_REF_TABLE_IDX_WIDTH */
91
92 #define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
93
94 void
95 pfkey_extensions_init(struct sadb_ext *extensions[SADB_EXT_MAX + 1])
96 {
97 int i;
98
99 for (i = 0; i != SADB_EXT_MAX + 1; i++) {
100 extensions[i] = NULL;
101 }
102 }
103
104 void
105 pfkey_extensions_free(struct sadb_ext *extensions[SADB_EXT_MAX + 1])
106 {
107 int i;
108
109 if (!extensions) {
110 return;
111 }
112
113 if (extensions[0]) {
114 memset(extensions[0], 0, sizeof(struct sadb_msg));
115 FREE(extensions[0]);
116 extensions[0] = NULL;
117 }
118
119 for (i = 1; i != SADB_EXT_MAX + 1; i++) {
120 if(extensions[i]) {
121 memset(extensions[i], 0, extensions[i]->sadb_ext_len * IPSEC_PFKEYv2_ALIGN);
122 FREE(extensions[i]);
123 extensions[i] = NULL;
124 }
125 }
126 }
127
128 void
129 pfkey_msg_free(struct sadb_msg **pfkey_msg)
130 {
131 if (*pfkey_msg) {
132 memset(*pfkey_msg, 0, (*pfkey_msg)->sadb_msg_len * IPSEC_PFKEYv2_ALIGN);
133 FREE(*pfkey_msg);
134 *pfkey_msg = NULL;
135 }
136 }
137
138 /* Default extension builders taken from the KLIPS code */
139
140 int
141 pfkey_msg_hdr_build(struct sadb_ext** pfkey_ext,
142 uint8_t msg_type,
143 uint8_t satype,
144 uint8_t msg_errno,
145 uint32_t seq,
146 uint32_t pid)
147 {
148 int error = 0;
149 struct sadb_msg *pfkey_msg = (struct sadb_msg *)*pfkey_ext;
150
151 DEBUGGING(
152 "pfkey_msg_hdr_build:\n");
153 DEBUGGING(
154 "pfkey_msg_hdr_build: "
155 "on_entry &pfkey_ext=0p%p pfkey_ext=0p%p *pfkey_ext=0p%p.\n",
156 &pfkey_ext,
157 pfkey_ext,
158 *pfkey_ext);
159 /* sanity checks... */
160 if (pfkey_msg) {
161 DEBUGGING(
162 "pfkey_msg_hdr_build: "
163 "why is pfkey_msg already pointing to something?\n");
164 SENDERR(EINVAL);
165 }
166
167 if (!msg_type) {
168 DEBUGGING(
169 "pfkey_msg_hdr_build: "
170 "msg type not set, must be non-zero..\n");
171 SENDERR(EINVAL);
172 }
173
174 if (msg_type > SADB_MAX) {
175 DEBUGGING(
176 "pfkey_msg_hdr_build: "
177 "msg type too large:%d.\n",
178 msg_type);
179 SENDERR(EINVAL);
180 }
181
182 if (satype > SADB_SATYPE_MAX) {
183 DEBUGGING(
184 "pfkey_msg_hdr_build: "
185 "satype %d > max %d\n",
186 satype, SADB_SATYPE_MAX);
187 SENDERR(EINVAL);
188 }
189
190 pfkey_msg = (struct sadb_msg*)MALLOC(sizeof(struct sadb_msg));
191 *pfkey_ext = (struct sadb_ext*)pfkey_msg;
192
193 if (pfkey_msg == NULL) {
194 DEBUGGING(
195 "pfkey_msg_hdr_build: "
196 "memory allocation failed\n");
197 SENDERR(ENOMEM);
198 }
199 memset(pfkey_msg, 0, sizeof(struct sadb_msg));
200
201 pfkey_msg->sadb_msg_len = sizeof(struct sadb_msg) / IPSEC_PFKEYv2_ALIGN;
202
203 pfkey_msg->sadb_msg_type = msg_type;
204 pfkey_msg->sadb_msg_satype = satype;
205
206 pfkey_msg->sadb_msg_version = PF_KEY_V2;
207 pfkey_msg->sadb_msg_errno = msg_errno;
208 pfkey_msg->sadb_msg_reserved = 0;
209 pfkey_msg->sadb_msg_seq = seq;
210 pfkey_msg->sadb_msg_pid = pid;
211 DEBUGGING(
212 "pfkey_msg_hdr_build: "
213 "on_exit &pfkey_ext=0p%p pfkey_ext=0p%p *pfkey_ext=0p%p.\n",
214 &pfkey_ext,
215 pfkey_ext,
216 *pfkey_ext);
217 errlab:
218 return error;
219 }
220
221 int
222 pfkey_sa_ref_build(struct sadb_ext ** pfkey_ext,
223 uint16_t exttype,
224 uint32_t spi,
225 uint8_t replay_window,
226 uint8_t sa_state,
227 uint8_t auth,
228 uint8_t encrypt,
229 uint32_t flags,
230 uint32_t/*IPsecSAref_t*/ ref)
231 {
232 int error = 0;
233 struct sadb_sa *pfkey_sa = (struct sadb_sa *)*pfkey_ext;
234
235 DEBUGGING(
236 "pfkey_sa_build: "
237 "spi=%08x replay=%d sa_state=%d auth=%d encrypt=%d flags=%d\n",
238 ntohl(spi), /* in network order */
239 replay_window,
240 sa_state,
241 auth,
242 encrypt,
243 flags);
244 /* sanity checks... */
245 if (pfkey_sa) {
246 DEBUGGING(
247 "pfkey_sa_build: "
248 "why is pfkey_sa already pointing to something?\n");
249 SENDERR(EINVAL);
250 }
251
252 if (exttype != SADB_EXT_SA
253 && exttype != SADB_X_EXT_SA2) {
254 DEBUGGING(
255 "pfkey_sa_build: "
256 "invalid exttype=%d.\n",
257 exttype);
258 SENDERR(EINVAL);
259 }
260
261 if (replay_window > 64) {
262 DEBUGGING(
263 "pfkey_sa_build: "
264 "replay window size: %d -- must be 0 <= size <= 64\n",
265 replay_window);
266 SENDERR(EINVAL);
267 }
268
269 if (auth > SADB_AALG_MAX) {
270 DEBUGGING(
271 "pfkey_sa_build: "
272 "auth=%d > SADB_AALG_MAX=%d.\n",
273 auth,
274 SADB_AALG_MAX);
275 SENDERR(EINVAL);
276 }
277
278 if (encrypt > SADB_EALG_MAX) {
279 DEBUGGING(
280 "pfkey_sa_build: "
281 "encrypt=%d > SADB_EALG_MAX=%d.\n",
282 encrypt,
283 SADB_EALG_MAX);
284 SENDERR(EINVAL);
285 }
286
287 if (sa_state > SADB_SASTATE_MAX) {
288 DEBUGGING(
289 "pfkey_sa_build: "
290 "sa_state=%d exceeds MAX=%d.\n",
291 sa_state,
292 SADB_SASTATE_MAX);
293 SENDERR(EINVAL);
294 }
295
296 if (sa_state == SADB_SASTATE_DEAD) {
297 DEBUGGING(
298 "pfkey_sa_build: "
299 "sa_state=%d is DEAD=%d is not allowed.\n",
300 sa_state,
301 SADB_SASTATE_DEAD);
302 SENDERR(EINVAL);
303 }
304
305 if ((IPSEC_SAREF_NULL != ref) && (ref >= (1 << IPSEC_SA_REF_TABLE_IDX_WIDTH))) {
306 DEBUGGING(
307 "pfkey_sa_build: "
308 "SAref=%d must be (SAref == IPSEC_SAREF_NULL(%d) || SAref < IPSEC_SA_REF_TABLE_NUM_ENTRIES(%d)).\n",
309 ref,
310 IPSEC_SAREF_NULL,
311 IPSEC_SA_REF_TABLE_NUM_ENTRIES);
312 SENDERR(EINVAL);
313 }
314
315 pfkey_sa = (struct sadb_sa*)MALLOC(sizeof(struct sadb_sa));
316 *pfkey_ext = (struct sadb_ext*)pfkey_sa;
317
318 if (pfkey_sa == NULL) {
319 DEBUGGING(
320 "pfkey_sa_build: "
321 "memory allocation failed\n");
322 SENDERR(ENOMEM);
323 }
324 memset(pfkey_sa, 0, sizeof(struct sadb_sa));
325
326 pfkey_sa->sadb_sa_len = sizeof(*pfkey_sa) / IPSEC_PFKEYv2_ALIGN;
327 pfkey_sa->sadb_sa_exttype = exttype;
328 pfkey_sa->sadb_sa_spi = spi;
329 pfkey_sa->sadb_sa_replay = replay_window;
330 pfkey_sa->sadb_sa_state = sa_state;
331 pfkey_sa->sadb_sa_auth = auth;
332 pfkey_sa->sadb_sa_encrypt = encrypt;
333 pfkey_sa->sadb_sa_flags = flags;
334 pfkey_sa->sadb_x_sa_ref = ref;
335
336 errlab:
337 return error;
338 }
339
340 int
341 pfkey_sa_build(struct sadb_ext ** pfkey_ext,
342 uint16_t exttype,
343 uint32_t spi,
344 uint8_t replay_window,
345 uint8_t sa_state,
346 uint8_t auth,
347 uint8_t encrypt,
348 uint32_t flags)
349 {
350 return pfkey_sa_ref_build(pfkey_ext,
351 exttype,
352 spi,
353 replay_window,
354 sa_state,
355 auth,
356 encrypt,
357 flags,
358 IPSEC_SAREF_NULL);
359 }
360
361 int
362 pfkey_lifetime_build(struct sadb_ext ** pfkey_ext,
363 uint16_t exttype,
364 uint32_t allocations,
365 uint64_t bytes,
366 uint64_t addtime,
367 uint64_t usetime,
368 uint32_t packets)
369 {
370 int error = 0;
371 struct sadb_lifetime *pfkey_lifetime = (struct sadb_lifetime *)*pfkey_ext;
372
373 DEBUGGING(
374 "pfkey_lifetime_build:\n");
375 /* sanity checks... */
376 if (pfkey_lifetime) {
377 DEBUGGING(
378 "pfkey_lifetime_build: "
379 "why is pfkey_lifetime already pointing to something?\n");
380 SENDERR(EINVAL);
381 }
382
383 if (exttype != SADB_EXT_LIFETIME_CURRENT
384 && exttype != SADB_EXT_LIFETIME_HARD
385 && exttype != SADB_EXT_LIFETIME_SOFT) {
386 DEBUGGING(
387 "pfkey_lifetime_build: "
388 "invalid exttype=%d.\n",
389 exttype);
390 SENDERR(EINVAL);
391 }
392
393 pfkey_lifetime = (struct sadb_lifetime*)MALLOC(sizeof(struct sadb_lifetime));
394 *pfkey_ext = (struct sadb_ext*)pfkey_lifetime;
395
396 if (pfkey_lifetime == NULL) {
397 DEBUGGING(
398 "pfkey_lifetime_build: "
399 "memory allocation failed\n");
400 SENDERR(ENOMEM);
401 }
402 memset(pfkey_lifetime, 0, sizeof(struct sadb_lifetime));
403
404 pfkey_lifetime->sadb_lifetime_len = sizeof(struct sadb_lifetime) / IPSEC_PFKEYv2_ALIGN;
405 pfkey_lifetime->sadb_lifetime_exttype = exttype;
406 pfkey_lifetime->sadb_lifetime_allocations = allocations;
407 pfkey_lifetime->sadb_lifetime_bytes = bytes;
408 pfkey_lifetime->sadb_lifetime_addtime = addtime;
409 pfkey_lifetime->sadb_lifetime_usetime = usetime;
410 pfkey_lifetime->sadb_x_lifetime_packets = packets;
411
412 errlab:
413 return error;
414 }
415
416 int
417 pfkey_address_build(struct sadb_ext** pfkey_ext,
418 uint16_t exttype,
419 uint8_t proto,
420 uint8_t prefixlen,
421 struct sockaddr* address)
422 {
423 int error = 0;
424 int saddr_len = 0;
425 char ipaddr_txt[ADDRTOT_BUF + 6/*extra for port number*/];
426 struct sadb_address *pfkey_address = (struct sadb_address *)*pfkey_ext;
427
428 DEBUGGING(
429 "pfkey_address_build: "
430 "exttype=%d proto=%d prefixlen=%d\n",
431 exttype,
432 proto,
433 prefixlen);
434 /* sanity checks... */
435 if (pfkey_address) {
436 DEBUGGING(
437 "pfkey_address_build: "
438 "why is pfkey_address already pointing to something?\n");
439 SENDERR(EINVAL);
440 }
441
442 if (!address) {
443 DEBUGGING("pfkey_address_build: "
444 "address is NULL\n");
445 SENDERR(EINVAL);
446 }
447
448 switch(exttype) {
449 case SADB_EXT_ADDRESS_SRC:
450 case SADB_EXT_ADDRESS_DST:
451 case SADB_EXT_ADDRESS_PROXY:
452 case SADB_X_EXT_ADDRESS_DST2:
453 case SADB_X_EXT_ADDRESS_SRC_FLOW:
454 case SADB_X_EXT_ADDRESS_DST_FLOW:
455 case SADB_X_EXT_ADDRESS_SRC_MASK:
456 case SADB_X_EXT_ADDRESS_DST_MASK:
457 case SADB_X_EXT_NAT_T_OA:
458 break;
459 default:
460 DEBUGGING(
461 "pfkey_address_build: "
462 "unrecognised ext_type=%d.\n",
463 exttype);
464 SENDERR(EINVAL);
465 }
466
467 switch (address->sa_family) {
468 case AF_INET:
469 DEBUGGING(
470 "pfkey_address_build: "
471 "found address family AF_INET.\n");
472 saddr_len = sizeof(struct sockaddr_in);
473 sprintf(ipaddr_txt, "%d.%d.%d.%d:%d"
474 , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 0) & 0xFF
475 , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 8) & 0xFF
476 , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 16) & 0xFF
477 , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 24) & 0xFF
478 , ntohs(((struct sockaddr_in*)address)->sin_port));
479 break;
480 case AF_INET6:
481 DEBUGGING(
482 "pfkey_address_build: "
483 "found address family AF_INET6.\n");
484 saddr_len = sizeof(struct sockaddr_in6);
485 sprintf(ipaddr_txt, "%x:%x:%x:%x:%x:%x:%x:%x-%x"
486 , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[0])
487 , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[1])
488 , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[2])
489 , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[3])
490 , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[4])
491 , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[5])
492 , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[6])
493 , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[7])
494 , ntohs(((struct sockaddr_in6*)address)->sin6_port));
495 break;
496 default:
497 DEBUGGING(
498 "pfkey_address_build: "
499 "address->sa_family=%d not supported.\n",
500 address->sa_family);
501 SENDERR(EPFNOSUPPORT);
502 }
503
504 DEBUGGING(
505 "pfkey_address_build: "
506 "found address=%s.\n",
507 ipaddr_txt);
508 if (prefixlen != 0) {
509 DEBUGGING(
510 "pfkey_address_build: "
511 "address prefixes not supported yet.\n");
512 SENDERR(EAFNOSUPPORT); /* not supported yet */
513 }
514
515 pfkey_address = (struct sadb_address*)
516 MALLOC(ALIGN_N(sizeof(struct sadb_address) + saddr_len, IPSEC_PFKEYv2_ALIGN));
517 *pfkey_ext = (struct sadb_ext*)pfkey_address;
518
519 if (pfkey_address == NULL) {
520 DEBUGGING(
521 "pfkey_lifetime_build: "
522 "memory allocation failed\n");
523 SENDERR(ENOMEM);
524 }
525 memset(pfkey_address,
526 0,
527 ALIGN_N(sizeof(struct sadb_address) + saddr_len,
528 IPSEC_PFKEYv2_ALIGN));
529
530 pfkey_address->sadb_address_len = DIVUP(sizeof(struct sadb_address) + saddr_len,
531 IPSEC_PFKEYv2_ALIGN);
532
533 pfkey_address->sadb_address_exttype = exttype;
534 pfkey_address->sadb_address_proto = proto;
535 pfkey_address->sadb_address_prefixlen = prefixlen;
536 pfkey_address->sadb_address_reserved = 0;
537
538 memcpy((char*)pfkey_address + sizeof(struct sadb_address),
539 address,
540 saddr_len);
541
542 #if 0
543 for (i = 0; i < sizeof(struct sockaddr_in) - offsetof(struct sockaddr_in, sin_zero); i++) {
544 pfkey_address_s_ska.sin_zero[i] = 0;
545 }
546 #endif
547 DEBUGGING(
548 "pfkey_address_build: "
549 "successful.\n");
550
551 errlab:
552 return error;
553 }
554
555 int
556 pfkey_key_build(struct sadb_ext** pfkey_ext,
557 uint16_t exttype,
558 uint16_t key_bits,
559 char* key)
560 {
561 int error = 0;
562 struct sadb_key *pfkey_key = (struct sadb_key *)*pfkey_ext;
563
564 DEBUGGING(
565 "pfkey_key_build:\n");
566 /* sanity checks... */
567 if (pfkey_key) {
568 DEBUGGING(
569 "pfkey_key_build: "
570 "why is pfkey_key already pointing to something?\n");
571 SENDERR(EINVAL);
572 }
573
574 if (!key_bits) {
575 DEBUGGING(
576 "pfkey_key_build: "
577 "key_bits is zero, it must be non-zero.\n");
578 SENDERR(EINVAL);
579 }
580
581 if ( !((exttype == SADB_EXT_KEY_AUTH) || (exttype == SADB_EXT_KEY_ENCRYPT))) {
582 DEBUGGING(
583 "pfkey_key_build: "
584 "unsupported extension type=%d.\n",
585 exttype);
586 SENDERR(EINVAL);
587 }
588
589 pfkey_key = (struct sadb_key*)
590 MALLOC(sizeof(struct sadb_key) +
591 DIVUP(key_bits, 64) * IPSEC_PFKEYv2_ALIGN);
592 *pfkey_ext = (struct sadb_ext*)pfkey_key;
593
594 if (pfkey_key == NULL) {
595 DEBUGGING(
596 "pfkey_key_build: "
597 "memory allocation failed\n");
598 SENDERR(ENOMEM);
599 }
600 memset(pfkey_key,
601 0,
602 sizeof(struct sadb_key) +
603 DIVUP(key_bits, 64) * IPSEC_PFKEYv2_ALIGN);
604
605 pfkey_key->sadb_key_len = DIVUP(sizeof(struct sadb_key) * IPSEC_PFKEYv2_ALIGN + key_bits,
606 64);
607 pfkey_key->sadb_key_exttype = exttype;
608 pfkey_key->sadb_key_bits = key_bits;
609 pfkey_key->sadb_key_reserved = 0;
610 memcpy((char*)pfkey_key + sizeof(struct sadb_key),
611 key,
612 DIVUP(key_bits, 8));
613
614 errlab:
615 return error;
616 }
617
618 int
619 pfkey_ident_build(struct sadb_ext** pfkey_ext,
620 uint16_t exttype,
621 uint16_t ident_type,
622 uint64_t ident_id,
623 uint8_t ident_len,
624 char* ident_string)
625 {
626 int error = 0;
627 struct sadb_ident *pfkey_ident = (struct sadb_ident *)*pfkey_ext;
628 int data_len = ident_len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
629
630 DEBUGGING(
631 "pfkey_ident_build:\n");
632 /* sanity checks... */
633 if (pfkey_ident) {
634 DEBUGGING(
635 "pfkey_ident_build: "
636 "why is pfkey_ident already pointing to something?\n");
637 SENDERR(EINVAL);
638 }
639
640 if ( !((exttype == SADB_EXT_IDENTITY_SRC) ||
641 (exttype == SADB_EXT_IDENTITY_DST))) {
642 DEBUGGING(
643 "pfkey_ident_build: "
644 "unsupported extension type=%d.\n",
645 exttype);
646 SENDERR(EINVAL);
647 }
648
649 if (ident_type == SADB_IDENTTYPE_RESERVED) {
650 DEBUGGING(
651 "pfkey_ident_build: "
652 "ident_type must be non-zero.\n");
653 SENDERR(EINVAL);
654 }
655
656 if (ident_type > SADB_IDENTTYPE_MAX) {
657 DEBUGGING(
658 "pfkey_ident_build: "
659 "identtype=%d out of range.\n",
660 ident_type);
661 SENDERR(EINVAL);
662 }
663
664 if ((ident_type == SADB_IDENTTYPE_PREFIX ||
665 ident_type == SADB_IDENTTYPE_FQDN) &&
666 !ident_string) {
667 DEBUGGING(
668 "pfkey_ident_build: "
669 "string required to allocate size of extension.\n");
670 SENDERR(EINVAL);
671 }
672
673 #if 0
674 if (ident_type == SADB_IDENTTYPE_USERFQDN) {
675 }
676 #endif
677
678 pfkey_ident = (struct sadb_ident*)
679 MALLOC(ident_len * IPSEC_PFKEYv2_ALIGN);
680 *pfkey_ext = (struct sadb_ext*)pfkey_ident;
681
682 if (pfkey_ident == NULL) {
683 DEBUGGING(
684 "pfkey_ident_build: "
685 "memory allocation failed\n");
686 SENDERR(ENOMEM);
687 }
688 memset(pfkey_ident, 0, ident_len * IPSEC_PFKEYv2_ALIGN);
689
690 pfkey_ident->sadb_ident_len = ident_len;
691 pfkey_ident->sadb_ident_exttype = exttype;
692 pfkey_ident->sadb_ident_type = ident_type;
693 pfkey_ident->sadb_ident_reserved = 0;
694 pfkey_ident->sadb_ident_id = ident_id;
695 memcpy((char*)pfkey_ident + sizeof(struct sadb_ident),
696 ident_string,
697 data_len);
698
699 errlab:
700 return error;
701 }
702
703 int
704 pfkey_sens_build(struct sadb_ext** pfkey_ext,
705 uint32_t dpd,
706 uint8_t sens_level,
707 uint8_t sens_len,
708 uint64_t* sens_bitmap,
709 uint8_t integ_level,
710 uint8_t integ_len,
711 uint64_t* integ_bitmap)
712 {
713 int error = 0;
714 struct sadb_sens *pfkey_sens = (struct sadb_sens *)*pfkey_ext;
715 int i;
716 uint64_t* bitmap;
717
718 DEBUGGING(
719 "pfkey_sens_build:\n");
720 /* sanity checks... */
721 if (pfkey_sens) {
722 DEBUGGING(
723 "pfkey_sens_build: "
724 "why is pfkey_sens already pointing to something?\n");
725 SENDERR(EINVAL);
726 }
727
728 DEBUGGING(
729 "pfkey_sens_build: "
730 "Sorry, I can't build exttype=%d yet.\n",
731 (*pfkey_ext)->sadb_ext_type);
732 SENDERR(EINVAL); /* don't process these yet */
733
734 pfkey_sens = (struct sadb_sens*)
735 MALLOC(sizeof(struct sadb_sens) +
736 (sens_len + integ_len) * sizeof(uint64_t));
737 *pfkey_ext = (struct sadb_ext*)pfkey_sens;
738
739 if (pfkey_sens == NULL) {
740 DEBUGGING(
741 "pfkey_sens_build: "
742 "memory allocation failed\n");
743 SENDERR(ENOMEM);
744 }
745 memset(pfkey_sens,
746 0,
747 sizeof(struct sadb_sens) +
748 (sens_len + integ_len) * sizeof(uint64_t));
749
750 pfkey_sens->sadb_sens_len = (sizeof(struct sadb_sens) +
751 (sens_len + integ_len) * sizeof(uint64_t)) / IPSEC_PFKEYv2_ALIGN;
752 pfkey_sens->sadb_sens_exttype = SADB_EXT_SENSITIVITY;
753 pfkey_sens->sadb_sens_dpd = dpd;
754 pfkey_sens->sadb_sens_sens_level = sens_level;
755 pfkey_sens->sadb_sens_sens_len = sens_len;
756 pfkey_sens->sadb_sens_integ_level = integ_level;
757 pfkey_sens->sadb_sens_integ_len = integ_len;
758 pfkey_sens->sadb_sens_reserved = 0;
759
760 bitmap = (uint64_t*)((char*)pfkey_ext + sizeof(struct sadb_sens));
761 for (i = 0; i < sens_len; i++) {
762 *bitmap = sens_bitmap[i];
763 bitmap++;
764 }
765 for (i = 0; i < integ_len; i++) {
766 *bitmap = integ_bitmap[i];
767 bitmap++;
768 }
769
770 errlab:
771 return error;
772 }
773
774 int
775 pfkey_prop_build(struct sadb_ext** pfkey_ext,
776 uint8_t replay,
777 unsigned int comb_num,
778 struct sadb_comb* comb)
779 {
780 int error = 0;
781 int i;
782 struct sadb_prop *pfkey_prop = (struct sadb_prop *)*pfkey_ext;
783 struct sadb_comb *combp;
784
785 DEBUGGING(
786 "pfkey_prop_build:\n");
787 /* sanity checks... */
788 if (pfkey_prop) {
789 DEBUGGING(
790 "pfkey_prop_build: "
791 "why is pfkey_prop already pointing to something?\n");
792 SENDERR(EINVAL);
793 }
794
795 pfkey_prop = (struct sadb_prop*)
796 MALLOC(sizeof(struct sadb_prop) +
797 comb_num * sizeof(struct sadb_comb));
798
799 *pfkey_ext = (struct sadb_ext*)pfkey_prop;
800
801 if (pfkey_prop == NULL) {
802 DEBUGGING(
803 "pfkey_prop_build: "
804 "memory allocation failed\n");
805 SENDERR(ENOMEM);
806 }
807 memset(pfkey_prop,
808 0,
809 sizeof(struct sadb_prop) +
810 comb_num * sizeof(struct sadb_comb));
811
812 pfkey_prop->sadb_prop_len = (sizeof(struct sadb_prop) +
813 comb_num * sizeof(struct sadb_comb)) / IPSEC_PFKEYv2_ALIGN;
814
815 pfkey_prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
816 pfkey_prop->sadb_prop_replay = replay;
817
818 for (i=0; i<3; i++) {
819 pfkey_prop->sadb_prop_reserved[i] = 0;
820 }
821
822 combp = (struct sadb_comb*)((char*)*pfkey_ext + sizeof(struct sadb_prop));
823 for (i = 0; i < comb_num; i++) {
824 memcpy (combp, &(comb[i]), sizeof(struct sadb_comb));
825 combp++;
826 }
827
828 #if 0
829 uint8_t sadb_comb_auth;
830 uint8_t sadb_comb_encrypt;
831 uint16_t sadb_comb_flags;
832 uint16_t sadb_comb_auth_minbits;
833 uint16_t sadb_comb_auth_maxbits;
834 uint16_t sadb_comb_encrypt_minbits;
835 uint16_t sadb_comb_encrypt_maxbits;
836 uint32_t sadb_comb_reserved;
837 uint32_t sadb_comb_soft_allocations;
838 uint32_t sadb_comb_hard_allocations;
839 uint64_t sadb_comb_soft_bytes;
840 uint64_t sadb_comb_hard_bytes;
841 uint64_t sadb_comb_soft_addtime;
842 uint64_t sadb_comb_hard_addtime;
843 uint64_t sadb_comb_soft_usetime;
844 uint64_t sadb_comb_hard_usetime;
845 uint32_t sadb_comb_soft_packets;
846 uint32_t sadb_comb_hard_packets;
847 #endif
848 errlab:
849 return error;
850 }
851
852 int
853 pfkey_supported_build(struct sadb_ext** pfkey_ext,
854 uint16_t exttype,
855 unsigned int alg_num,
856 struct sadb_alg* alg)
857 {
858 int error = 0;
859 unsigned int i;
860 struct sadb_supported *pfkey_supported = (struct sadb_supported *)*pfkey_ext;
861 struct sadb_alg *pfkey_alg;
862
863 /* sanity checks... */
864 if (pfkey_supported) {
865 DEBUGGING(
866 "pfkey_supported_build: "
867 "why is pfkey_supported already pointing to something?\n");
868 SENDERR(EINVAL);
869 }
870
871 if ( !((exttype == SADB_EXT_SUPPORTED_AUTH) || (exttype == SADB_EXT_SUPPORTED_ENCRYPT))) {
872 DEBUGGING(
873 "pfkey_supported_build: "
874 "unsupported extension type=%d.\n",
875 exttype);
876 SENDERR(EINVAL);
877 }
878
879 pfkey_supported = (struct sadb_supported*)
880 MALLOC(sizeof(struct sadb_supported) +
881 alg_num * sizeof(struct sadb_alg));
882
883 *pfkey_ext = (struct sadb_ext*)pfkey_supported;
884
885 if (pfkey_supported == NULL) {
886 DEBUGGING(
887 "pfkey_supported_build: "
888 "memory allocation failed\n");
889 SENDERR(ENOMEM);
890 }
891 memset(pfkey_supported,
892 0,
893 sizeof(struct sadb_supported) +
894 alg_num *
895 sizeof(struct sadb_alg));
896
897 pfkey_supported->sadb_supported_len = (sizeof(struct sadb_supported) +
898 alg_num *
899 sizeof(struct sadb_alg)) /
900 IPSEC_PFKEYv2_ALIGN;
901 pfkey_supported->sadb_supported_exttype = exttype;
902 pfkey_supported->sadb_supported_reserved = 0;
903
904 pfkey_alg = (struct sadb_alg*)((char*)pfkey_supported + sizeof(struct sadb_supported));
905 for(i = 0; i < alg_num; i++) {
906 memcpy (pfkey_alg, &(alg[i]), sizeof(struct sadb_alg));
907 pfkey_alg->sadb_alg_reserved = 0;
908 pfkey_alg++;
909 }
910
911 #if 0
912 DEBUGGING(
913 "pfkey_supported_build: "
914 "Sorry, I can't build exttype=%d yet.\n",
915 (*pfkey_ext)->sadb_ext_type);
916 SENDERR(EINVAL); /* don't process these yet */
917
918 uint8_t sadb_alg_id;
919 uint8_t sadb_alg_ivlen;
920 uint16_t sadb_alg_minbits;
921 uint16_t sadb_alg_maxbits;
922 uint16_t sadb_alg_reserved;
923 #endif
924 errlab:
925 return error;
926 }
927
928 int
929 pfkey_spirange_build(struct sadb_ext** pfkey_ext,
930 uint16_t exttype,
931 uint32_t min, /* in network order */
932 uint32_t max) /* in network order */
933 {
934 int error = 0;
935 struct sadb_spirange *pfkey_spirange = (struct sadb_spirange *)*pfkey_ext;
936
937 /* sanity checks... */
938 if (pfkey_spirange) {
939 DEBUGGING(
940 "pfkey_spirange_build: "
941 "why is pfkey_spirange already pointing to something?\n");
942 SENDERR(EINVAL);
943 }
944
945 if (ntohl(max) < ntohl(min)) {
946 DEBUGGING(
947 "pfkey_spirange_build: "
948 "minspi=%08x must be < maxspi=%08x.\n",
949 ntohl(min),
950 ntohl(max));
951 SENDERR(EINVAL);
952 }
953
954 if (ntohl(min) <= 255) {
955 DEBUGGING(
956 "pfkey_spirange_build: "
957 "minspi=%08x must be > 255.\n",
958 ntohl(min));
959 SENDERR(EEXIST);
960 }
961
962 pfkey_spirange = (struct sadb_spirange*)
963 MALLOC(sizeof(struct sadb_spirange));
964 *pfkey_ext = (struct sadb_ext*)pfkey_spirange;
965
966 if (pfkey_spirange == NULL) {
967 DEBUGGING(
968 "pfkey_spirange_build: "
969 "memory allocation failed\n");
970 SENDERR(ENOMEM);
971 }
972 memset(pfkey_spirange,
973 0,
974 sizeof(struct sadb_spirange));
975
976 pfkey_spirange->sadb_spirange_len = sizeof(struct sadb_spirange) / IPSEC_PFKEYv2_ALIGN;
977
978 pfkey_spirange->sadb_spirange_exttype = SADB_EXT_SPIRANGE;
979 pfkey_spirange->sadb_spirange_min = min;
980 pfkey_spirange->sadb_spirange_max = max;
981 pfkey_spirange->sadb_spirange_reserved = 0;
982 errlab:
983 return error;
984 }
985
986 int
987 pfkey_x_kmprivate_build(struct sadb_ext** pfkey_ext)
988 {
989 int error = 0;
990 struct sadb_x_kmprivate *pfkey_x_kmprivate = (struct sadb_x_kmprivate *)*pfkey_ext;
991
992 /* sanity checks... */
993 if (pfkey_x_kmprivate) {
994 DEBUGGING(
995 "pfkey_x_kmprivate_build: "
996 "why is pfkey_x_kmprivate already pointing to something?\n");
997 SENDERR(EINVAL);
998 }
999
1000 pfkey_x_kmprivate->sadb_x_kmprivate_reserved = 0;
1001
1002 DEBUGGING(
1003 "pfkey_x_kmprivate_build: "
1004 "Sorry, I can't build exttype=%d yet.\n",
1005 (*pfkey_ext)->sadb_ext_type);
1006 SENDERR(EINVAL); /* don't process these yet */
1007
1008 pfkey_x_kmprivate = (struct sadb_x_kmprivate*)
1009 MALLOC(sizeof(struct sadb_x_kmprivate));
1010 *pfkey_ext = (struct sadb_ext*)pfkey_x_kmprivate;
1011
1012 if (pfkey_x_kmprivate == NULL) {
1013 DEBUGGING(
1014 "pfkey_x_kmprivate_build: "
1015 "memory allocation failed\n");
1016 SENDERR(ENOMEM);
1017 }
1018 memset(pfkey_x_kmprivate,
1019 0,
1020 sizeof(struct sadb_x_kmprivate));
1021
1022 pfkey_x_kmprivate->sadb_x_kmprivate_len =
1023 sizeof(struct sadb_x_kmprivate) / IPSEC_PFKEYv2_ALIGN;
1024
1025 pfkey_x_kmprivate->sadb_x_kmprivate_exttype = SADB_X_EXT_KMPRIVATE;
1026 pfkey_x_kmprivate->sadb_x_kmprivate_reserved = 0;
1027 errlab:
1028 return error;
1029 }
1030
1031 int
1032 pfkey_x_satype_build(struct sadb_ext** pfkey_ext,
1033 uint8_t satype)
1034 {
1035 int error = 0;
1036 int i;
1037 struct sadb_x_satype *pfkey_x_satype = (struct sadb_x_satype *)*pfkey_ext;
1038
1039 DEBUGGING(
1040 "pfkey_x_satype_build:\n");
1041 /* sanity checks... */
1042 if (pfkey_x_satype) {
1043 DEBUGGING(
1044 "pfkey_x_satype_build: "
1045 "why is pfkey_x_satype already pointing to something?\n");
1046 SENDERR(EINVAL);
1047 }
1048
1049 if (!satype) {
1050 DEBUGGING(
1051 "pfkey_x_satype_build: "
1052 "SA type not set, must be non-zero.\n");
1053 SENDERR(EINVAL);
1054 }
1055
1056 if (satype > SADB_SATYPE_MAX) {
1057 DEBUGGING(
1058 "pfkey_x_satype_build: "
1059 "satype %d > max %d\n",
1060 satype, SADB_SATYPE_MAX);
1061 SENDERR(EINVAL);
1062 }
1063
1064 pfkey_x_satype = (struct sadb_x_satype*)
1065 MALLOC(sizeof(struct sadb_x_satype));
1066
1067 *pfkey_ext = (struct sadb_ext*)pfkey_x_satype;
1068
1069 if (pfkey_x_satype == NULL) {
1070 DEBUGGING(
1071 "pfkey_x_satype_build: "
1072 "memory allocation failed\n");
1073 SENDERR(ENOMEM);
1074 }
1075 memset(pfkey_x_satype,
1076 0,
1077 sizeof(struct sadb_x_satype));
1078
1079 pfkey_x_satype->sadb_x_satype_len = sizeof(struct sadb_x_satype) / IPSEC_PFKEYv2_ALIGN;
1080
1081 pfkey_x_satype->sadb_x_satype_exttype = SADB_X_EXT_SATYPE2;
1082 pfkey_x_satype->sadb_x_satype_satype = satype;
1083 for (i=0; i<3; i++) {
1084 pfkey_x_satype->sadb_x_satype_reserved[i] = 0;
1085 }
1086
1087 errlab:
1088 return error;
1089 }
1090
1091 int
1092 pfkey_x_debug_build(struct sadb_ext** pfkey_ext,
1093 uint32_t tunnel,
1094 uint32_t netlink,
1095 uint32_t xform,
1096 uint32_t eroute,
1097 uint32_t spi,
1098 uint32_t radij,
1099 uint32_t esp,
1100 uint32_t ah,
1101 uint32_t rcv,
1102 uint32_t pfkey,
1103 uint32_t ipcomp,
1104 uint32_t verbose)
1105 {
1106 int error = 0;
1107 int i;
1108 struct sadb_x_debug *pfkey_x_debug = (struct sadb_x_debug *)*pfkey_ext;
1109
1110 DEBUGGING(
1111 "pfkey_x_debug_build:\n");
1112 /* sanity checks... */
1113 if (pfkey_x_debug) {
1114 DEBUGGING(
1115 "pfkey_x_debug_build: "
1116 "why is pfkey_x_debug already pointing to something?\n");
1117 SENDERR(EINVAL);
1118 }
1119
1120 DEBUGGING(
1121 "pfkey_x_debug_build: "
1122 "tunnel=%x netlink=%x xform=%x eroute=%x spi=%x radij=%x esp=%x ah=%x rcv=%x pfkey=%x ipcomp=%x verbose=%x?\n",
1123 tunnel, netlink, xform, eroute, spi, radij, esp, ah, rcv, pfkey, ipcomp, verbose);
1124
1125 pfkey_x_debug = (struct sadb_x_debug*)
1126 MALLOC(sizeof(struct sadb_x_debug));
1127 *pfkey_ext = (struct sadb_ext*)pfkey_x_debug;
1128
1129 if (pfkey_x_debug == NULL) {
1130 DEBUGGING(
1131 "pfkey_x_debug_build: "
1132 "memory allocation failed\n");
1133 SENDERR(ENOMEM);
1134 }
1135 #if 0
1136 memset(pfkey_x_debug,
1137 0,
1138 sizeof(struct sadb_x_debug));
1139 #endif
1140
1141 pfkey_x_debug->sadb_x_debug_len = sizeof(struct sadb_x_debug) / IPSEC_PFKEYv2_ALIGN;
1142 pfkey_x_debug->sadb_x_debug_exttype = SADB_X_EXT_DEBUG;
1143
1144 pfkey_x_debug->sadb_x_debug_tunnel = tunnel;
1145 pfkey_x_debug->sadb_x_debug_netlink = netlink;
1146 pfkey_x_debug->sadb_x_debug_xform = xform;
1147 pfkey_x_debug->sadb_x_debug_eroute = eroute;
1148 pfkey_x_debug->sadb_x_debug_spi = spi;
1149 pfkey_x_debug->sadb_x_debug_radij = radij;
1150 pfkey_x_debug->sadb_x_debug_esp = esp;
1151 pfkey_x_debug->sadb_x_debug_ah = ah;
1152 pfkey_x_debug->sadb_x_debug_rcv = rcv;
1153 pfkey_x_debug->sadb_x_debug_pfkey = pfkey;
1154 pfkey_x_debug->sadb_x_debug_ipcomp = ipcomp;
1155 pfkey_x_debug->sadb_x_debug_verbose = verbose;
1156
1157 for (i=0; i<4; i++) {
1158 pfkey_x_debug->sadb_x_debug_reserved[i] = 0;
1159 }
1160
1161 errlab:
1162 return error;
1163 }
1164
1165 int
1166 pfkey_x_nat_t_type_build(struct sadb_ext** pfkey_ext,
1167 uint8_t type)
1168 {
1169 int error = 0;
1170 int i;
1171 struct sadb_x_nat_t_type *pfkey_x_nat_t_type = (struct sadb_x_nat_t_type *)*pfkey_ext;
1172
1173 DEBUGGING(
1174 "pfkey_x_nat_t_type_build:\n");
1175 /* sanity checks... */
1176 if (pfkey_x_nat_t_type) {
1177 DEBUGGING(
1178 "pfkey_x_nat_t_type_build: "
1179 "why is pfkey_x_nat_t_type already pointing to something?\n");
1180 SENDERR(EINVAL);
1181 }
1182
1183 DEBUGGING(
1184 "pfkey_x_nat_t_type_build: "
1185 "type=%d\n", type);
1186
1187 pfkey_x_nat_t_type = (struct sadb_x_nat_t_type*)
1188 MALLOC(sizeof(struct sadb_x_nat_t_type));
1189
1190 *pfkey_ext = (struct sadb_ext*)pfkey_x_nat_t_type;
1191 if (pfkey_x_nat_t_type == NULL) {
1192 DEBUGGING(
1193 "pfkey_x_nat_t_type_build: "
1194 "memory allocation failed\n");
1195 SENDERR(ENOMEM);
1196 }
1197
1198 pfkey_x_nat_t_type->sadb_x_nat_t_type_len = sizeof(struct sadb_x_nat_t_type) / IPSEC_PFKEYv2_ALIGN;
1199 pfkey_x_nat_t_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
1200 pfkey_x_nat_t_type->sadb_x_nat_t_type_type = type;
1201 for (i=0; i<3; i++) {
1202 pfkey_x_nat_t_type->sadb_x_nat_t_type_reserved[i] = 0;
1203 }
1204
1205 errlab:
1206 return error;
1207 }
1208
1209 int
1210 pfkey_x_nat_t_port_build(struct sadb_ext** pfkey_ext,
1211 uint16_t exttype,
1212 uint16_t port)
1213 {
1214 int error = 0;
1215 struct sadb_x_nat_t_port *pfkey_x_nat_t_port = (struct sadb_x_nat_t_port *)*pfkey_ext;
1216
1217 DEBUGGING(
1218 "pfkey_x_nat_t_port_build:\n");
1219 /* sanity checks... */
1220 if (pfkey_x_nat_t_port) {
1221 DEBUGGING(
1222 "pfkey_x_nat_t_port_build: "
1223 "why is pfkey_x_nat_t_port already pointing to something?\n");
1224 SENDERR(EINVAL);
1225 }
1226
1227 switch (exttype) {
1228 case SADB_X_EXT_NAT_T_SPORT:
1229 case SADB_X_EXT_NAT_T_DPORT:
1230 break;
1231 default:
1232 DEBUGGING(
1233 "pfkey_nat_t_port_build: "
1234 "unrecognised ext_type=%d.\n",
1235 exttype);
1236 SENDERR(EINVAL);
1237 }
1238
1239 DEBUGGING(
1240 "pfkey_x_nat_t_port_build: "
1241 "ext=%d, port=%d\n", exttype, port);
1242
1243 pfkey_x_nat_t_port = (struct sadb_x_nat_t_port*)
1244 MALLOC(sizeof(struct sadb_x_nat_t_port));
1245 *pfkey_ext = (struct sadb_ext*)pfkey_x_nat_t_port;
1246
1247 if (pfkey_x_nat_t_port == NULL) {
1248 DEBUGGING(
1249 "pfkey_x_nat_t_port_build: "
1250 "memory allocation failed\n");
1251 SENDERR(ENOMEM);
1252 }
1253
1254 pfkey_x_nat_t_port->sadb_x_nat_t_port_len = sizeof(struct sadb_x_nat_t_port) / IPSEC_PFKEYv2_ALIGN;
1255 pfkey_x_nat_t_port->sadb_x_nat_t_port_exttype = exttype;
1256 pfkey_x_nat_t_port->sadb_x_nat_t_port_port = port;
1257 pfkey_x_nat_t_port->sadb_x_nat_t_port_reserved = 0;
1258
1259 errlab:
1260 return error;
1261 }
1262
1263 int pfkey_x_protocol_build(struct sadb_ext **pfkey_ext,
1264 uint8_t protocol)
1265 {
1266 int error = 0;
1267 struct sadb_protocol * p = (struct sadb_protocol *)*pfkey_ext;
1268 DEBUGGING("pfkey_x_protocol_build: protocol=%u\n", protocol);
1269 /* sanity checks... */
1270 if (p != 0) {
1271 DEBUGGING("pfkey_x_protocol_build: bogus protocol pointer\n");
1272 SENDERR(EINVAL);
1273 }
1274 if ((p = (struct sadb_protocol*)MALLOC(sizeof(*p))) == 0) {
1275 DEBUGGING("pfkey_build: memory allocation failed\n");
1276 SENDERR(ENOMEM);
1277 }
1278 *pfkey_ext = (struct sadb_ext *)p;
1279 p->sadb_protocol_len = sizeof(*p) / sizeof(uint64_t);
1280 p->sadb_protocol_exttype = SADB_X_EXT_PROTOCOL;
1281 p->sadb_protocol_proto = protocol;
1282 p->sadb_protocol_flags = 0;
1283 p->sadb_protocol_reserved2 = 0;
1284 errlab:
1285 return error;
1286 }
1287
1288
1289 #if I_DONT_THINK_THIS_WILL_BE_USEFUL
1290 int (*ext_default_builders[SADB_EXT_MAX +1])(struct sadb_msg*, struct sadb_ext*)
1291 =
1292 {
1293 NULL, /* pfkey_msg_build, */
1294 pfkey_sa_build,
1295 pfkey_lifetime_build,
1296 pfkey_lifetime_build,
1297 pfkey_lifetime_build,
1298 pfkey_address_build,
1299 pfkey_address_build,
1300 pfkey_address_build,
1301 pfkey_key_build,
1302 pfkey_key_build,
1303 pfkey_ident_build,
1304 pfkey_ident_build,
1305 pfkey_sens_build,
1306 pfkey_prop_build,
1307 pfkey_supported_build,
1308 pfkey_supported_build,
1309 pfkey_spirange_build,
1310 pfkey_x_kmprivate_build,
1311 pfkey_x_satype_build,
1312 pfkey_sa_build,
1313 pfkey_address_build,
1314 pfkey_address_build,
1315 pfkey_address_build,
1316 pfkey_address_build,
1317 pfkey_address_build,
1318 pfkey_x_ext_debug_build
1319 };
1320 #endif
1321
1322 int
1323 pfkey_msg_build(struct sadb_msg **pfkey_msg, struct sadb_ext *extensions[], int dir)
1324 {
1325 int error = 0;
1326 unsigned ext;
1327 unsigned total_size;
1328 struct sadb_ext *pfkey_ext;
1329 int extensions_seen = 0;
1330 struct sadb_ext *extensions_check[SADB_EXT_MAX + 1];
1331
1332 if (!extensions[0]) {
1333 DEBUGGING(
1334 "pfkey_msg_build: "
1335 "extensions[0] must be specified (struct sadb_msg).\n");
1336 SENDERR(EINVAL);
1337 }
1338
1339 total_size = sizeof(struct sadb_msg) / IPSEC_PFKEYv2_ALIGN;
1340 for (ext = 1; ext <= SADB_EXT_MAX; ext++) {
1341 if(extensions[ext]) {
1342 total_size += (extensions[ext])->sadb_ext_len;
1343 }
1344 }
1345
1346 if (!(*pfkey_msg = (struct sadb_msg*)MALLOC(total_size * IPSEC_PFKEYv2_ALIGN))) {
1347 DEBUGGING(
1348 "pfkey_msg_build: "
1349 "memory allocation failed\n");
1350 SENDERR(ENOMEM);
1351 }
1352
1353 DEBUGGING(
1354 "pfkey_msg_build: "
1355 "pfkey_msg=0p%p allocated %lu bytes, &(extensions[0])=0p%p\n",
1356 *pfkey_msg,
1357 (unsigned long)(total_size * IPSEC_PFKEYv2_ALIGN),
1358 &(extensions[0]));
1359 memcpy(*pfkey_msg,
1360 extensions[0],
1361 sizeof(struct sadb_msg));
1362 (*pfkey_msg)->sadb_msg_len = total_size;
1363 (*pfkey_msg)->sadb_msg_reserved = 0;
1364 extensions_seen = 1 ;
1365
1366 pfkey_ext = (struct sadb_ext*)(((char*)(*pfkey_msg)) + sizeof(struct sadb_msg));
1367
1368 for (ext = 1; ext <= SADB_EXT_MAX; ext++) {
1369 /* copy from extension[ext] to buffer */
1370 if (extensions[ext]) {
1371 /* Is this type of extension permitted for this type of message? */
1372 if (!(extensions_bitmaps[dir][EXT_BITS_PERM][(*pfkey_msg)->sadb_msg_type] &
1373 1<<ext)) {
1374 DEBUGGING(
1375 "pfkey_msg_build: "
1376 "ext type %d not permitted, exts_perm=%08x, 1<<type=%08x\n",
1377 ext,
1378 extensions_bitmaps[dir][EXT_BITS_PERM][(*pfkey_msg)->sadb_msg_type],
1379 1<<ext);
1380 SENDERR(EINVAL);
1381 }
1382 DEBUGGING(
1383 "pfkey_msg_build: "
1384 "copying %lu bytes from extensions[%u]=0p%p to=0p%p\n",
1385 (unsigned long)(extensions[ext]->sadb_ext_len * IPSEC_PFKEYv2_ALIGN),
1386 ext,
1387 extensions[ext],
1388 pfkey_ext);
1389 memcpy(pfkey_ext,
1390 extensions[ext],
1391 (extensions[ext])->sadb_ext_len * IPSEC_PFKEYv2_ALIGN);
1392 {
1393 char *pfkey_ext_c = (char *)pfkey_ext;
1394
1395 pfkey_ext_c += (extensions[ext])->sadb_ext_len * IPSEC_PFKEYv2_ALIGN;
1396 pfkey_ext = (struct sadb_ext *)pfkey_ext_c;
1397 }
1398 /* Mark that we have seen this extension and remember the header location */
1399 extensions_seen |= ( 1 << ext );
1400 }
1401 }
1402
1403 /* check required extensions */
1404 DEBUGGING(
1405 "pfkey_msg_build: "
1406 "extensions permitted=%08x, seen=%08x, required=%08x.\n",
1407 extensions_bitmaps[dir][EXT_BITS_PERM][(*pfkey_msg)->sadb_msg_type],
1408 extensions_seen,
1409 extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]);
1410
1411 if ((extensions_seen &
1412 extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]) !=
1413 extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]) {
1414 DEBUGGING(
1415 "pfkey_msg_build: "
1416 "required extensions missing:%08x.\n",
1417 extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type] -
1418 (extensions_seen &
1419 extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]) );
1420 SENDERR(EINVAL);
1421 }
1422
1423 error = pfkey_msg_parse(*pfkey_msg, NULL, extensions_check, dir);
1424 if (error) {
1425 DEBUGGING(
1426 "pfkey_msg_build: "
1427 "Trouble parsing newly built pfkey message, error=%d.\n",
1428 error);
1429 SENDERR(-error);
1430 }
1431
1432 errlab:
1433
1434 return error;
1435 }