http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / userapps / opensource / ipsec-tools / src / libipsec / pfkey.c
1 /*      $KAME: pfkey.c,v 1.47 2003/10/02 19:52:12 itojun Exp $  */
2
3 /*
4  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <net/pfkeyv2.h>
40 #include <netinet/in.h>
41 #ifdef HAVE_NETINET6_IPSEC
42 #  include <netinet6/ipsec.h>
43 #else
44 #  include <netinet/ipsec.h>
45 #endif
46
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <string.h>
50 #include <errno.h>
51 #include <stdio.h>
52
53 #include "ipsec_strerror.h"
54 #include "libpfkey.h"
55
56 #define CALLOC(size, cast) (cast)calloc(1, (size))
57
58 static int findsupportedmap __P((int));
59 static int setsupportedmap __P((struct sadb_supported *));
60 static struct sadb_alg *findsupportedalg __P((u_int, u_int));
61 static int pfkey_send_x1 __P((int, u_int, u_int, u_int, struct sockaddr *,
62         struct sockaddr *, u_int32_t, u_int32_t, u_int, caddr_t,
63         u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int32_t,
64         u_int32_t, u_int32_t, u_int32_t,
65         u_int8_t, u_int16_t, u_int16_t, struct sockaddr *));
66 static int pfkey_send_x2 __P((int, u_int, u_int, u_int,
67         struct sockaddr *, struct sockaddr *, u_int32_t));
68 static int pfkey_send_x3 __P((int, u_int, u_int));
69 static int pfkey_send_x4 __P((int, u_int, struct sockaddr *, u_int,
70         struct sockaddr *, u_int, u_int, u_int64_t, u_int64_t,
71         char *, int, u_int32_t));
72 static int pfkey_send_x5 __P((int, u_int, u_int32_t));
73
74 static caddr_t pfkey_setsadbmsg __P((caddr_t, caddr_t, u_int, u_int,
75         u_int, u_int32_t, pid_t));
76 static caddr_t pfkey_setsadbsa __P((caddr_t, caddr_t, u_int32_t, u_int,
77         u_int, u_int, u_int32_t));
78 static caddr_t pfkey_setsadbaddr __P((caddr_t, caddr_t, u_int,
79         struct sockaddr *, u_int, u_int));
80 static caddr_t pfkey_setsadbkey __P((caddr_t, caddr_t, u_int, caddr_t, u_int));
81 static caddr_t pfkey_setsadblifetime __P((caddr_t, caddr_t, u_int, u_int32_t,
82         u_int32_t, u_int32_t, u_int32_t));
83 static caddr_t pfkey_setsadbxsa2 __P((caddr_t, caddr_t, u_int32_t, u_int32_t));
84
85 #ifdef SADB_X_EXT_NAT_T_TYPE
86 static caddr_t pfkey_set_natt_type __P((caddr_t, caddr_t, u_int, u_int8_t));
87 static caddr_t pfkey_set_natt_port __P((caddr_t, caddr_t, u_int, u_int16_t));
88 #endif
89
90 /*
91  * make and search supported algorithm structure.
92  */
93 static struct sadb_supported *ipsec_supported[] = { NULL, NULL, NULL, };
94
95 static int supported_map[] = {
96         SADB_SATYPE_AH,
97         SADB_SATYPE_ESP,
98         SADB_X_SATYPE_IPCOMP,
99 };
100
101 static int
102 findsupportedmap(satype)
103         int satype;
104 {
105         int i;
106
107         for (i = 0; i < sizeof(supported_map)/sizeof(supported_map[0]); i++)
108                 if (supported_map[i] == satype)
109                         return i;
110         return -1;
111 }
112
113 static struct sadb_alg *
114 findsupportedalg(satype, alg_id)
115         u_int satype, alg_id;
116 {
117         int algno;
118         int tlen;
119         caddr_t p;
120
121         /* validity check */
122         algno = findsupportedmap(satype);
123         if (algno == -1) {
124                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
125                 return NULL;
126         }
127         if (ipsec_supported[algno] == NULL) {
128                 __ipsec_errcode = EIPSEC_DO_GET_SUPP_LIST;
129                 return NULL;
130         }
131
132         tlen = ipsec_supported[algno]->sadb_supported_len
133                 - sizeof(struct sadb_supported);
134         p = (caddr_t)(ipsec_supported[algno] + 1);
135         while (tlen > 0) {
136                 if (tlen < sizeof(struct sadb_alg)) {
137                         /* invalid format */
138                         break;
139                 }
140                 if (((struct sadb_alg *)p)->sadb_alg_id == alg_id)
141                         return (struct sadb_alg *)p;
142
143                 tlen -= sizeof(struct sadb_alg);
144                 p += sizeof(struct sadb_alg);
145         }
146
147         __ipsec_errcode = EIPSEC_NOT_SUPPORTED;
148         return NULL;
149 }
150
151 static int
152 setsupportedmap(sup)
153         struct sadb_supported *sup;
154 {
155         struct sadb_supported **ipsup;
156
157         switch (sup->sadb_supported_exttype) {
158         case SADB_EXT_SUPPORTED_AUTH:
159                 ipsup = &ipsec_supported[findsupportedmap(SADB_SATYPE_AH)];
160                 break;
161         case SADB_EXT_SUPPORTED_ENCRYPT:
162                 ipsup = &ipsec_supported[findsupportedmap(SADB_SATYPE_ESP)];
163                 break;
164         default:
165                 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
166                 return -1;
167         }
168
169         if (*ipsup)
170                 free(*ipsup);
171
172         *ipsup = malloc(sup->sadb_supported_len);
173         if (!*ipsup) {
174                 __ipsec_set_strerror(strerror(errno));
175                 return -1;
176         }
177         memcpy(*ipsup, sup, sup->sadb_supported_len);
178
179         return 0;
180 }
181
182 /*
183  * check key length against algorithm specified.
184  * This function is called with SADB_EXT_SUPPORTED_{AUTH,ENCRYPT} as the
185  * augument, and only calls to ipsec_check_keylen2();
186  * keylen is the unit of bit.
187  * OUT:
188  *      -1: invalid.
189  *       0: valid.
190  */
191 int
192 ipsec_check_keylen(supported, alg_id, keylen)
193         u_int supported;
194         u_int alg_id;
195         u_int keylen;
196 {
197         int satype;
198
199         /* validity check */
200         switch (supported) {
201         case SADB_EXT_SUPPORTED_AUTH:
202                 satype = SADB_SATYPE_AH;
203                 break;
204         case SADB_EXT_SUPPORTED_ENCRYPT:
205                 satype = SADB_SATYPE_ESP;
206                 break;
207         default:
208                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
209                 return -1;
210         }
211
212         return ipsec_check_keylen2(satype, alg_id, keylen);
213 }
214
215 /*
216  * check key length against algorithm specified.
217  * satype is one of satype defined at pfkeyv2.h.
218  * keylen is the unit of bit.
219  * OUT:
220  *      -1: invalid.
221  *       0: valid.
222  */
223 int
224 ipsec_check_keylen2(satype, alg_id, keylen)
225         u_int satype;
226         u_int alg_id;
227         u_int keylen;
228 {
229         struct sadb_alg *alg;
230
231         alg = findsupportedalg(satype, alg_id);
232         if (!alg)
233                 return -1;
234
235         if (keylen < alg->sadb_alg_minbits || keylen > alg->sadb_alg_maxbits) {
236                 fprintf(stderr, "%d %d %d\n", keylen, alg->sadb_alg_minbits,
237                         alg->sadb_alg_maxbits);
238                 __ipsec_errcode = EIPSEC_INVAL_KEYLEN;
239                 return -1;
240         }
241
242         __ipsec_errcode = EIPSEC_NO_ERROR;
243         return 0;
244 }
245
246 /*
247  * get max/min key length against algorithm specified.
248  * satype is one of satype defined at pfkeyv2.h.
249  * keylen is the unit of bit.
250  * OUT:
251  *      -1: invalid.
252  *       0: valid.
253  */
254 int
255 ipsec_get_keylen(supported, alg_id, alg0)
256         u_int supported, alg_id;
257         struct sadb_alg *alg0;
258 {
259         struct sadb_alg *alg;
260         u_int satype;
261
262         /* validity check */
263         if (!alg0) {
264                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
265                 return -1;
266         }
267
268         switch (supported) {
269         case SADB_EXT_SUPPORTED_AUTH:
270                 satype = SADB_SATYPE_AH;
271                 break;
272         case SADB_EXT_SUPPORTED_ENCRYPT:
273                 satype = SADB_SATYPE_ESP;
274                 break;
275         default:
276                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
277                 return -1;
278         }
279
280         alg = findsupportedalg(satype, alg_id);
281         if (!alg)
282                 return -1;
283
284         memcpy(alg0, alg, sizeof(*alg0));
285
286         __ipsec_errcode = EIPSEC_NO_ERROR;
287         return 0;
288 }
289
290 /*
291  * set the rate for SOFT lifetime against HARD one.
292  * If rate is more than 100 or equal to zero, then set to 100.
293  */
294 static u_int soft_lifetime_allocations_rate = PFKEY_SOFT_LIFETIME_RATE;
295 static u_int soft_lifetime_bytes_rate = PFKEY_SOFT_LIFETIME_RATE;
296 static u_int soft_lifetime_addtime_rate = PFKEY_SOFT_LIFETIME_RATE;
297 static u_int soft_lifetime_usetime_rate = PFKEY_SOFT_LIFETIME_RATE;
298
299 u_int
300 pfkey_set_softrate(type, rate)
301         u_int type, rate;
302 {
303         __ipsec_errcode = EIPSEC_NO_ERROR;
304
305         if (rate > 100 || rate == 0)
306                 rate = 100;
307
308         switch (type) {
309         case SADB_X_LIFETIME_ALLOCATIONS:
310                 soft_lifetime_allocations_rate = rate;
311                 return 0;
312         case SADB_X_LIFETIME_BYTES:
313                 soft_lifetime_bytes_rate = rate;
314                 return 0;
315         case SADB_X_LIFETIME_ADDTIME:
316                 soft_lifetime_addtime_rate = rate;
317                 return 0;
318         case SADB_X_LIFETIME_USETIME:
319                 soft_lifetime_usetime_rate = rate;
320                 return 0;
321         }
322
323         __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
324         return 1;
325 }
326
327 /*
328  * get current rate for SOFT lifetime against HARD one.
329  * ATTENTION: ~0 is returned if invalid type was passed.
330  */
331 u_int
332 pfkey_get_softrate(type)
333         u_int type;
334 {
335         switch (type) {
336         case SADB_X_LIFETIME_ALLOCATIONS:
337                 return soft_lifetime_allocations_rate;
338         case SADB_X_LIFETIME_BYTES:
339                 return soft_lifetime_bytes_rate;
340         case SADB_X_LIFETIME_ADDTIME:
341                 return soft_lifetime_addtime_rate;
342         case SADB_X_LIFETIME_USETIME:
343                 return soft_lifetime_usetime_rate;
344         }
345
346         return ~0;
347 }
348
349 /*
350  * sending SADB_GETSPI message to the kernel.
351  * OUT:
352  *      positive: success and return length sent.
353  *      -1      : error occured, and set errno.
354  */
355 int
356 pfkey_send_getspi(so, satype, mode, src, dst, min, max, reqid, seq)
357         int so;
358         u_int satype, mode;
359         struct sockaddr *src, *dst;
360         u_int32_t min, max, reqid, seq;
361 {
362         struct sadb_msg *newmsg;
363         caddr_t ep;
364         int len;
365         int need_spirange = 0;
366         caddr_t p;
367         int plen;
368
369         /* validity check */
370         if (src == NULL || dst == NULL) {
371                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
372                 return -1;
373         }
374         if (src->sa_family != dst->sa_family) {
375                 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
376                 return -1;
377         }
378         if (min > max || (min > 0 && min <= 255)) {
379                 __ipsec_errcode = EIPSEC_INVAL_SPI;
380                 return -1;
381         }
382         switch (src->sa_family) {
383         case AF_INET:
384                 plen = sizeof(struct in_addr) << 3;
385                 break;
386         case AF_INET6:
387                 plen = sizeof(struct in6_addr) << 3;
388                 break;
389         default:
390                 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
391                 return -1;
392         }
393
394         /* create new sadb_msg to send. */
395         len = sizeof(struct sadb_msg)
396                 + sizeof(struct sadb_x_sa2)
397                 + sizeof(struct sadb_address)
398                 + PFKEY_ALIGN8(sysdep_sa_len(src))
399                 + sizeof(struct sadb_address)
400                 + PFKEY_ALIGN8(sysdep_sa_len(dst));
401
402         if (min > 255 && max < ~0) {
403                 need_spirange++;
404                 len += sizeof(struct sadb_spirange);
405         }
406
407         if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
408                 __ipsec_set_strerror(strerror(errno));
409                 return -1;
410         }
411         ep = ((caddr_t)newmsg) + len;
412
413         p = pfkey_setsadbmsg((caddr_t)newmsg, ep, SADB_GETSPI,
414             len, satype, seq, getpid());
415         if (!p) {
416                 free(newmsg);
417                 return -1;
418         }
419
420         p = pfkey_setsadbxsa2(p, ep, mode, reqid);
421         if (!p) {
422                 free(newmsg);
423                 return -1;
424         }
425
426         /* set sadb_address for source */
427         p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
428             IPSEC_ULPROTO_ANY);
429         if (!p) {
430                 free(newmsg);
431                 return -1;
432         }
433
434         /* set sadb_address for destination */
435         p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
436             IPSEC_ULPROTO_ANY);
437         if (!p) {
438                 free(newmsg);
439                 return -1;
440         }
441
442         /* proccessing spi range */
443         if (need_spirange) {
444                 struct sadb_spirange spirange;
445
446                 if (p + sizeof(spirange) > ep) {
447                         free(newmsg);
448                         return -1;
449                 }
450
451                 memset(&spirange, 0, sizeof(spirange));
452                 spirange.sadb_spirange_len = PFKEY_UNIT64(sizeof(spirange));
453                 spirange.sadb_spirange_exttype = SADB_EXT_SPIRANGE;
454                 spirange.sadb_spirange_min = min;
455                 spirange.sadb_spirange_max = max;
456
457                 memcpy(p, &spirange, sizeof(spirange));
458
459                 p += sizeof(spirange);
460         }
461         if (p != ep) {
462                 free(newmsg);
463                 return -1;
464         }
465
466         /* send message */
467         len = pfkey_send(so, newmsg, len);
468         free(newmsg);
469
470         if (len < 0)
471                 return -1;
472
473         __ipsec_errcode = EIPSEC_NO_ERROR;
474         return len;
475 }
476
477 /*
478  * sending SADB_UPDATE message to the kernel.
479  * The length of key material is a_keylen + e_keylen.
480  * OUT:
481  *      positive: success and return length sent.
482  *      -1      : error occured, and set errno.
483  */
484 int
485 pfkey_send_update(so, satype, mode, src, dst, spi, reqid, wsize,
486                 keymat, e_type, e_keylen, a_type, a_keylen, flags,
487                 l_alloc, l_bytes, l_addtime, l_usetime, seq)
488         int so;
489         u_int satype, mode, wsize;
490         struct sockaddr *src, *dst;
491         u_int32_t spi, reqid;
492         caddr_t keymat;
493         u_int e_type, e_keylen, a_type, a_keylen, flags;
494         u_int32_t l_alloc;
495         u_int64_t l_bytes, l_addtime, l_usetime;
496         u_int32_t seq;
497 {
498         int len;
499         if ((len = pfkey_send_x1(so, SADB_UPDATE, satype, mode, src, dst, spi,
500                         reqid, wsize,
501                         keymat, e_type, e_keylen, a_type, a_keylen, flags,
502                         l_alloc, l_bytes, l_addtime, l_usetime, seq,
503                         0, 0, 0, NULL)) < 0)
504                 return -1;
505
506         return len;
507 }
508
509 #ifdef SADB_X_EXT_NAT_T_TYPE
510 int
511 pfkey_send_update_nat(so, satype, mode, src, dst, spi, reqid, wsize,
512                       keymat, e_type, e_keylen, a_type, a_keylen, flags,
513                       l_alloc, l_bytes, l_addtime, l_usetime, seq,
514                       l_natt_type, l_natt_sport, l_natt_dport, l_natt_oa)
515         int so;
516         u_int satype, mode, wsize;
517         struct sockaddr *src, *dst;
518         u_int32_t spi, reqid;
519         caddr_t keymat;
520         u_int e_type, e_keylen, a_type, a_keylen, flags;
521         u_int32_t l_alloc;
522         u_int64_t l_bytes, l_addtime, l_usetime;
523         u_int32_t seq;
524         u_int8_t l_natt_type;
525         u_int16_t l_natt_sport, l_natt_dport;
526         struct sockaddr *l_natt_oa;
527 {
528         int len;
529         if ((len = pfkey_send_x1(so, SADB_UPDATE, satype, mode, src, dst, spi,
530                         reqid, wsize,
531                         keymat, e_type, e_keylen, a_type, a_keylen, flags,
532                         l_alloc, l_bytes, l_addtime, l_usetime, seq,
533                         l_natt_type, l_natt_sport, l_natt_dport, l_natt_oa)) < 0)
534                 return -1;
535
536         return len;
537 }
538 #endif
539
540 /*
541  * sending SADB_ADD message to the kernel.
542  * The length of key material is a_keylen + e_keylen.
543  * OUT:
544  *      positive: success and return length sent.
545  *      -1      : error occured, and set errno.
546  */
547 int
548 pfkey_send_add(so, satype, mode, src, dst, spi, reqid, wsize,
549                 keymat, e_type, e_keylen, a_type, a_keylen, flags,
550                 l_alloc, l_bytes, l_addtime, l_usetime, seq)
551         int so;
552         u_int satype, mode, wsize;
553         struct sockaddr *src, *dst;
554         u_int32_t spi, reqid;
555         caddr_t keymat;
556         u_int e_type, e_keylen, a_type, a_keylen, flags;
557         u_int32_t l_alloc;
558         u_int64_t l_bytes, l_addtime, l_usetime;
559         u_int32_t seq;
560 {
561         int len;
562         if ((len = pfkey_send_x1(so, SADB_ADD, satype, mode, src, dst, spi,
563                         reqid, wsize,
564                         keymat, e_type, e_keylen, a_type, a_keylen, flags,
565                         l_alloc, l_bytes, l_addtime, l_usetime, seq,
566                         0, 0, 0, NULL)) < 0)
567                 return -1;
568
569         return len;
570 }
571
572 #ifdef SADB_X_EXT_NAT_T_TYPE
573 int
574 pfkey_send_add_nat(so, satype, mode, src, dst, spi, reqid, wsize,
575                    keymat, e_type, e_keylen, a_type, a_keylen, flags,
576                    l_alloc, l_bytes, l_addtime, l_usetime, seq,
577                    l_natt_type, l_natt_sport, l_natt_dport, l_natt_oa)
578         int so;
579         u_int satype, mode, wsize;
580         struct sockaddr *src, *dst;
581         u_int32_t spi, reqid;
582         caddr_t keymat;
583         u_int e_type, e_keylen, a_type, a_keylen, flags;
584         u_int32_t l_alloc;
585         u_int64_t l_bytes, l_addtime, l_usetime;
586         u_int32_t seq;
587         u_int8_t l_natt_type;
588         u_int16_t l_natt_sport, l_natt_dport;
589         struct sockaddr *l_natt_oa;
590 {
591         int len;
592         if ((len = pfkey_send_x1(so, SADB_ADD, satype, mode, src, dst, spi,
593                         reqid, wsize,
594                         keymat, e_type, e_keylen, a_type, a_keylen, flags,
595                         l_alloc, l_bytes, l_addtime, l_usetime, seq,
596                         l_natt_type, l_natt_sport, l_natt_dport, l_natt_oa)) < 0)
597                 return -1;
598
599         return len;
600 }
601 #endif
602
603 /*
604  * sending SADB_DELETE message to the kernel.
605  * OUT:
606  *      positive: success and return length sent.
607  *      -1      : error occured, and set errno.
608  */
609 int
610 pfkey_send_delete(so, satype, mode, src, dst, spi)
611         int so;
612         u_int satype, mode;
613         struct sockaddr *src, *dst;
614         u_int32_t spi;
615 {
616         int len;
617         if ((len = pfkey_send_x2(so, SADB_DELETE, satype, mode, src, dst, spi)) < 0)
618                 return -1;
619
620         return len;
621 }
622
623 /*
624  * sending SADB_DELETE without spi to the kernel.  This is
625  * the "delete all" request (an extension also present in
626  * Solaris).
627  *
628  * OUT:
629  *      positive: success and return length sent
630  *      -1      : error occured, and set errno
631  */
632 int
633 pfkey_send_delete_all(so, satype, mode, src, dst)
634         int so;
635         u_int satype, mode;
636         struct sockaddr *src, *dst;
637 {
638         struct sadb_msg *newmsg;
639         int len;
640         caddr_t p;
641         int plen;
642         caddr_t ep;
643
644         /* validity check */
645         if (src == NULL || dst == NULL) {
646                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
647                 return -1;
648         }
649         if (src->sa_family != dst->sa_family) {
650                 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
651                 return -1;
652         }
653         switch (src->sa_family) {
654         case AF_INET:
655                 plen = sizeof(struct in_addr) << 3;
656                 break;
657         case AF_INET6:
658                 plen = sizeof(struct in6_addr) << 3;
659                 break;
660         default:
661                 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
662                 return -1;
663         }
664
665         /* create new sadb_msg to reply. */
666         len = sizeof(struct sadb_msg)
667                 + sizeof(struct sadb_address)
668                 + PFKEY_ALIGN8(sysdep_sa_len(src))
669                 + sizeof(struct sadb_address)
670                 + PFKEY_ALIGN8(sysdep_sa_len(dst));
671
672         if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
673                 __ipsec_set_strerror(strerror(errno));
674                 return -1;
675         }
676         ep = ((caddr_t)newmsg) + len;
677
678         p = pfkey_setsadbmsg((caddr_t)newmsg, ep, SADB_DELETE, len, satype, 0,
679             getpid());
680         if (!p) {
681                 free(newmsg);
682                 return -1;
683         }
684         p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
685             IPSEC_ULPROTO_ANY);
686         if (!p) {
687                 free(newmsg);
688                 return -1;
689         }
690         p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
691             IPSEC_ULPROTO_ANY);
692         if (!p || p != ep) {
693                 free(newmsg);
694                 return -1;
695         }
696
697         /* send message */
698         len = pfkey_send(so, newmsg, len);
699         free(newmsg);
700
701         if (len < 0)
702                 return -1;
703
704         __ipsec_errcode = EIPSEC_NO_ERROR;
705         return len;
706 }
707
708 /*
709  * sending SADB_GET message to the kernel.
710  * OUT:
711  *      positive: success and return length sent.
712  *      -1      : error occured, and set errno.
713  */
714 int
715 pfkey_send_get(so, satype, mode, src, dst, spi)
716         int so;
717         u_int satype, mode;
718         struct sockaddr *src, *dst;
719         u_int32_t spi;
720 {
721         int len;
722         if ((len = pfkey_send_x2(so, SADB_GET, satype, mode, src, dst, spi)) < 0)
723                 return -1;
724
725         return len;
726 }
727
728 /*
729  * sending SADB_REGISTER message to the kernel.
730  * OUT:
731  *      positive: success and return length sent.
732  *      -1      : error occured, and set errno.
733  */
734 int
735 pfkey_send_register(so, satype)
736         int so;
737         u_int satype;
738 {
739         int len, algno;
740
741         if (satype == PF_UNSPEC) {
742                 for (algno = 0;
743                      algno < sizeof(supported_map)/sizeof(supported_map[0]);
744                      algno++) {
745                         if (ipsec_supported[algno]) {
746                                 free(ipsec_supported[algno]);
747                                 ipsec_supported[algno] = NULL;
748                         }
749                 }
750         } else {
751                 algno = findsupportedmap(satype);
752                 if (algno == -1) {
753                         __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
754                         return -1;
755                 }
756
757                 if (ipsec_supported[algno]) {
758                         free(ipsec_supported[algno]);
759                         ipsec_supported[algno] = NULL;
760                 }
761         }
762
763         if ((len = pfkey_send_x3(so, SADB_REGISTER, satype)) < 0)
764                 return -1;
765
766         return len;
767 }
768
769 /*
770  * receiving SADB_REGISTER message from the kernel, and copy buffer for
771  * sadb_supported returned into ipsec_supported.
772  * OUT:
773  *       0: success and return length sent.
774  *      -1: error occured, and set errno.
775  */
776 int
777 pfkey_recv_register(so)
778         int so;
779 {
780         pid_t pid = getpid();
781         struct sadb_msg *newmsg;
782         int error = -1;
783
784         /* receive message */
785         for (;;) {
786                 if ((newmsg = pfkey_recv(so)) == NULL)
787                         return -1;
788                 if (newmsg->sadb_msg_type == SADB_REGISTER &&
789                     newmsg->sadb_msg_pid == pid)
790                         break;
791                 free(newmsg);
792         }
793
794         /* check and fix */
795         newmsg->sadb_msg_len = PFKEY_UNUNIT64(newmsg->sadb_msg_len);
796
797         error = pfkey_set_supported(newmsg, newmsg->sadb_msg_len);
798         free(newmsg);
799
800         if (error == 0)
801                 __ipsec_errcode = EIPSEC_NO_ERROR;
802
803         return error;
804 }
805
806 /*
807  * receiving SADB_REGISTER message from the kernel, and copy buffer for
808  * sadb_supported returned into ipsec_supported.
809  * NOTE: sadb_msg_len must be host order.
810  * IN:
811  *      tlen: msg length, it's to makeing sure.
812  * OUT:
813  *       0: success and return length sent.
814  *      -1: error occured, and set errno.
815  */
816 int
817 pfkey_set_supported(msg, tlen)
818         struct sadb_msg *msg;
819         int tlen;
820 {
821         struct sadb_supported *sup;
822         caddr_t p;
823         caddr_t ep;
824
825         /* validity */
826         if (msg->sadb_msg_len != tlen) {
827                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
828                 return -1;
829         }
830
831         p = (caddr_t)msg;
832         ep = p + tlen;
833
834         p += sizeof(struct sadb_msg);
835
836         while (p < ep) {
837                 sup = (struct sadb_supported *)p;
838                 if (ep < p + sizeof(*sup) ||
839                     PFKEY_EXTLEN(sup) < sizeof(*sup) ||
840                     ep < p + sup->sadb_supported_len) {
841                         /* invalid format */
842                         break;
843                 }
844
845                 switch (sup->sadb_supported_exttype) {
846                 case SADB_EXT_SUPPORTED_AUTH:
847                 case SADB_EXT_SUPPORTED_ENCRYPT:
848                         break;
849                 default:
850                         __ipsec_errcode = EIPSEC_INVAL_SATYPE;
851                         return -1;
852                 }
853
854                 /* fixed length */
855                 sup->sadb_supported_len = PFKEY_EXTLEN(sup);
856
857                 /* set supported map */
858                 if (setsupportedmap(sup) != 0)
859                         return -1;
860
861                 p += sup->sadb_supported_len;
862         }
863
864         if (p != ep) {
865                 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
866                 return -1;
867         }
868
869         __ipsec_errcode = EIPSEC_NO_ERROR;
870
871         return 0;
872 }
873
874 /*
875  * sending SADB_FLUSH message to the kernel.
876  * OUT:
877  *      positive: success and return length sent.
878  *      -1      : error occured, and set errno.
879  */
880 int
881 pfkey_send_flush(so, satype)
882         int so;
883         u_int satype;
884 {
885         int len;
886
887         if ((len = pfkey_send_x3(so, SADB_FLUSH, satype)) < 0)
888                 return -1;
889
890         return len;
891 }
892
893 /*
894  * sending SADB_DUMP message to the kernel.
895  * OUT:
896  *      positive: success and return length sent.
897  *      -1      : error occured, and set errno.
898  */
899 int
900 pfkey_send_dump(so, satype)
901         int so;
902         u_int satype;
903 {
904         int len;
905
906         if ((len = pfkey_send_x3(so, SADB_DUMP, satype)) < 0)
907                 return -1;
908
909         return len;
910 }
911
912 /*
913  * sending SADB_X_PROMISC message to the kernel.
914  * NOTE that this function handles promisc mode toggle only.
915  * IN:
916  *      flag:   set promisc off if zero, set promisc on if non-zero.
917  * OUT:
918  *      positive: success and return length sent.
919  *      -1      : error occured, and set errno.
920  *      0     : error occured, and set errno.
921  *      others: a pointer to new allocated buffer in which supported
922  *              algorithms is.
923  */
924 int
925 pfkey_send_promisc_toggle(so, flag)
926         int so;
927         int flag;
928 {
929         int len;
930
931         if ((len = pfkey_send_x3(so, SADB_X_PROMISC, (flag ? 1 : 0))) < 0)
932                 return -1;
933
934         return len;
935 }
936
937 /*
938  * sending SADB_X_SPDADD message to the kernel.
939  * OUT:
940  *      positive: success and return length sent.
941  *      -1      : error occured, and set errno.
942  */
943 int
944 pfkey_send_spdadd(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
945         int so;
946         struct sockaddr *src, *dst;
947         u_int prefs, prefd, proto;
948         caddr_t policy;
949         int policylen;
950         u_int32_t seq;
951 {
952         int len;
953
954         if ((len = pfkey_send_x4(so, SADB_X_SPDADD,
955                                 src, prefs, dst, prefd, proto,
956                                 0, 0,
957                                 policy, policylen, seq)) < 0)
958                 return -1;
959
960         return len;
961 }
962
963 /*
964  * sending SADB_X_SPDADD message to the kernel.
965  * OUT:
966  *      positive: success and return length sent.
967  *      -1      : error occured, and set errno.
968  */
969 int
970 pfkey_send_spdadd2(so, src, prefs, dst, prefd, proto, ltime, vtime,
971                 policy, policylen, seq)
972         int so;
973         struct sockaddr *src, *dst;
974         u_int prefs, prefd, proto;
975         u_int64_t ltime, vtime;
976         caddr_t policy;
977         int policylen;
978         u_int32_t seq;
979 {
980         int len;
981
982         if ((len = pfkey_send_x4(so, SADB_X_SPDADD,
983                                 src, prefs, dst, prefd, proto,
984                                 ltime, vtime,
985                                 policy, policylen, seq)) < 0)
986                 return -1;
987
988         return len;
989 }
990
991 /*
992  * sending SADB_X_SPDUPDATE message to the kernel.
993  * OUT:
994  *      positive: success and return length sent.
995  *      -1      : error occured, and set errno.
996  */
997 int
998 pfkey_send_spdupdate(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
999         int so;
1000         struct sockaddr *src, *dst;
1001         u_int prefs, prefd, proto;
1002         caddr_t policy;
1003         int policylen;
1004         u_int32_t seq;
1005 {
1006         int len;
1007
1008         if ((len = pfkey_send_x4(so, SADB_X_SPDUPDATE,
1009                                 src, prefs, dst, prefd, proto,
1010                                 0, 0,
1011                                 policy, policylen, seq)) < 0)
1012                 return -1;
1013
1014         return len;
1015 }
1016
1017 /*
1018  * sending SADB_X_SPDUPDATE message to the kernel.
1019  * OUT:
1020  *      positive: success and return length sent.
1021  *      -1      : error occured, and set errno.
1022  */
1023 int
1024 pfkey_send_spdupdate2(so, src, prefs, dst, prefd, proto, ltime, vtime,
1025                 policy, policylen, seq)
1026         int so;
1027         struct sockaddr *src, *dst;
1028         u_int prefs, prefd, proto;
1029         u_int64_t ltime, vtime;
1030         caddr_t policy;
1031         int policylen;
1032         u_int32_t seq;
1033 {
1034         int len;
1035
1036         if ((len = pfkey_send_x4(so, SADB_X_SPDUPDATE,
1037                                 src, prefs, dst, prefd, proto,
1038                                 ltime, vtime,
1039                                 policy, policylen, seq)) < 0)
1040                 return -1;
1041
1042         return len;
1043 }
1044
1045 /*
1046  * sending SADB_X_SPDDELETE message to the kernel.
1047  * OUT:
1048  *      positive: success and return length sent.
1049  *      -1      : error occured, and set errno.
1050  */
1051 int
1052 pfkey_send_spddelete(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
1053         int so;
1054         struct sockaddr *src, *dst;
1055         u_int prefs, prefd, proto;
1056         caddr_t policy;
1057         int policylen;
1058         u_int32_t seq;
1059 {
1060         int len;
1061
1062         if (policylen != sizeof(struct sadb_x_policy)) {
1063                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1064                 return -1;
1065         }
1066
1067         if ((len = pfkey_send_x4(so, SADB_X_SPDDELETE,
1068                                 src, prefs, dst, prefd, proto,
1069                                 0, 0,
1070                                 policy, policylen, seq)) < 0)
1071                 return -1;
1072
1073         return len;
1074 }
1075
1076 /*
1077  * sending SADB_X_SPDDELETE message to the kernel.
1078  * OUT:
1079  *      positive: success and return length sent.
1080  *      -1      : error occured, and set errno.
1081  */
1082 int
1083 pfkey_send_spddelete2(so, spid)
1084         int so;
1085         u_int32_t spid;
1086 {
1087         int len;
1088
1089         if ((len = pfkey_send_x5(so, SADB_X_SPDDELETE2, spid)) < 0)
1090                 return -1;
1091
1092         return len;
1093 }
1094
1095 /*
1096  * sending SADB_X_SPDGET message to the kernel.
1097  * OUT:
1098  *      positive: success and return length sent.
1099  *      -1      : error occured, and set errno.
1100  */
1101 int
1102 pfkey_send_spdget(so, spid)
1103         int so;
1104         u_int32_t spid;
1105 {
1106         int len;
1107
1108         if ((len = pfkey_send_x5(so, SADB_X_SPDGET, spid)) < 0)
1109                 return -1;
1110
1111         return len;
1112 }
1113
1114 /*
1115  * sending SADB_X_SPDSETIDX message to the kernel.
1116  * OUT:
1117  *      positive: success and return length sent.
1118  *      -1      : error occured, and set errno.
1119  */
1120 int
1121 pfkey_send_spdsetidx(so, src, prefs, dst, prefd, proto, policy, policylen, seq)
1122         int so;
1123         struct sockaddr *src, *dst;
1124         u_int prefs, prefd, proto;
1125         caddr_t policy;
1126         int policylen;
1127         u_int32_t seq;
1128 {
1129         int len;
1130
1131         if (policylen != sizeof(struct sadb_x_policy)) {
1132                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1133                 return -1;
1134         }
1135
1136         if ((len = pfkey_send_x4(so, SADB_X_SPDSETIDX,
1137                                 src, prefs, dst, prefd, proto,
1138                                 0, 0,
1139                                 policy, policylen, seq)) < 0)
1140                 return -1;
1141
1142         return len;
1143 }
1144
1145 /*
1146  * sending SADB_SPDFLUSH message to the kernel.
1147  * OUT:
1148  *      positive: success and return length sent.
1149  *      -1      : error occured, and set errno.
1150  */
1151 int
1152 pfkey_send_spdflush(so)
1153         int so;
1154 {
1155         int len;
1156
1157         if ((len = pfkey_send_x3(so, SADB_X_SPDFLUSH, SADB_SATYPE_UNSPEC)) < 0)
1158                 return -1;
1159
1160         return len;
1161 }
1162
1163 /*
1164  * sending SADB_SPDDUMP message to the kernel.
1165  * OUT:
1166  *      positive: success and return length sent.
1167  *      -1      : error occured, and set errno.
1168  */
1169 int
1170 pfkey_send_spddump(so)
1171         int so;
1172 {
1173         int len;
1174
1175         if ((len = pfkey_send_x3(so, SADB_X_SPDDUMP, SADB_SATYPE_UNSPEC)) < 0)
1176                 return -1;
1177
1178         return len;
1179 }
1180
1181 /* sending SADB_ADD or SADB_UPDATE message to the kernel */
1182 static int
1183 pfkey_send_x1(so, type, satype, mode, src, dst, spi, reqid, wsize,
1184                 keymat, e_type, e_keylen, a_type, a_keylen, flags,
1185                 l_alloc, l_bytes, l_addtime, l_usetime, seq,
1186                 l_natt_type, l_natt_sport, l_natt_dport, l_natt_oa)
1187         int so;
1188         u_int type, satype, mode;
1189         struct sockaddr *src, *dst, *l_natt_oa;
1190         u_int32_t spi, reqid;
1191         u_int wsize;
1192         caddr_t keymat;
1193         u_int e_type, e_keylen, a_type, a_keylen, flags;
1194         u_int32_t l_alloc, l_bytes, l_addtime, l_usetime, seq;
1195         u_int16_t l_natt_sport, l_natt_dport;
1196         u_int8_t l_natt_type;
1197 {
1198         struct sadb_msg *newmsg;
1199         int len;
1200         caddr_t p;
1201         int plen;
1202         caddr_t ep;
1203
1204         /* validity check */
1205         if (src == NULL || dst == NULL) {
1206                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1207                 return -1;
1208         }
1209         if (src->sa_family != dst->sa_family) {
1210                 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1211                 return -1;
1212         }
1213         switch (src->sa_family) {
1214         case AF_INET:
1215                 plen = sizeof(struct in_addr) << 3;
1216                 break;
1217         case AF_INET6:
1218                 plen = sizeof(struct in6_addr) << 3;
1219                 break;
1220         default:
1221                 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1222                 return -1;
1223         }
1224
1225         switch (satype) {
1226         case SADB_SATYPE_ESP:
1227                 if (e_type == SADB_EALG_NONE) {
1228                         __ipsec_errcode = EIPSEC_NO_ALGS;
1229                         return -1;
1230                 }
1231                 break;
1232         case SADB_SATYPE_AH:
1233                 if (e_type != SADB_EALG_NONE) {
1234                         __ipsec_errcode = EIPSEC_INVAL_ALGS;
1235                         return -1;
1236                 }
1237                 if (a_type == SADB_AALG_NONE) {
1238                         __ipsec_errcode = EIPSEC_NO_ALGS;
1239                         return -1;
1240                 }
1241                 break;
1242         case SADB_X_SATYPE_IPCOMP:
1243                 if (e_type == SADB_X_CALG_NONE) {
1244                         __ipsec_errcode = EIPSEC_INVAL_ALGS;
1245                         return -1;
1246                 }
1247                 if (a_type != SADB_AALG_NONE) {
1248                         __ipsec_errcode = EIPSEC_NO_ALGS;
1249                         return -1;
1250                 }
1251                 break;
1252         default:
1253                 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1254                 return -1;
1255         }
1256
1257         /* create new sadb_msg to reply. */
1258         len = sizeof(struct sadb_msg)
1259                 + sizeof(struct sadb_sa)
1260                 + sizeof(struct sadb_x_sa2)
1261                 + sizeof(struct sadb_address)
1262                 + PFKEY_ALIGN8(sysdep_sa_len(src))
1263                 + sizeof(struct sadb_address)
1264                 + PFKEY_ALIGN8(sysdep_sa_len(dst))
1265                 + sizeof(struct sadb_lifetime)
1266                 + sizeof(struct sadb_lifetime);
1267
1268         if (e_type != SADB_EALG_NONE && satype != SADB_X_SATYPE_IPCOMP)
1269                 len += (sizeof(struct sadb_key) + PFKEY_ALIGN8(e_keylen));
1270         if (a_type != SADB_AALG_NONE)
1271                 len += (sizeof(struct sadb_key) + PFKEY_ALIGN8(a_keylen));
1272
1273 #ifdef SADB_X_EXT_NAT_T_TYPE
1274         /* add nat-t packets */
1275         if (l_natt_type) {
1276                 if (satype != SADB_SATYPE_ESP) {
1277                         __ipsec_errcode = EIPSEC_NO_ALGS;
1278                         return -1;
1279                 }
1280
1281                 len += sizeof(struct sadb_x_nat_t_type);
1282                 len += sizeof(struct sadb_x_nat_t_port);
1283                 len += sizeof(struct sadb_x_nat_t_port);
1284                 if (l_natt_oa)
1285                         len += sizeof(struct sadb_address) +
1286                           PFKEY_ALIGN8(sysdep_sa_len(l_natt_oa));
1287         }
1288 #endif
1289
1290         if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1291                 __ipsec_set_strerror(strerror(errno));
1292                 return -1;
1293         }
1294         ep = ((caddr_t)newmsg) + len;
1295
1296         p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len,
1297                              satype, seq, getpid());
1298         if (!p) {
1299                 free(newmsg);
1300                 return -1;
1301         }
1302         p = pfkey_setsadbsa(p, ep, spi, wsize, a_type, e_type, flags);
1303         if (!p) {
1304                 free(newmsg);
1305                 return -1;
1306         }
1307         p = pfkey_setsadbxsa2(p, ep, mode, reqid);
1308         if (!p) {
1309                 free(newmsg);
1310                 return -1;
1311         }
1312         p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
1313             IPSEC_ULPROTO_ANY);
1314         if (!p) {
1315                 free(newmsg);
1316                 return -1;
1317         }
1318         p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
1319             IPSEC_ULPROTO_ANY);
1320         if (!p) {
1321                 free(newmsg);
1322                 return -1;
1323         }
1324
1325         if (e_type != SADB_EALG_NONE && satype != SADB_X_SATYPE_IPCOMP) {
1326                 p = pfkey_setsadbkey(p, ep, SADB_EXT_KEY_ENCRYPT,
1327                                    keymat, e_keylen);
1328                 if (!p) {
1329                         free(newmsg);
1330                         return -1;
1331                 }
1332         }
1333         if (a_type != SADB_AALG_NONE) {
1334                 p = pfkey_setsadbkey(p, ep, SADB_EXT_KEY_AUTH,
1335                                    keymat + e_keylen, a_keylen);
1336                 if (!p) {
1337                         free(newmsg);
1338                         return -1;
1339                 }
1340         }
1341
1342         /* set sadb_lifetime for destination */
1343         p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_HARD,
1344                         l_alloc, l_bytes, l_addtime, l_usetime);
1345         if (!p) {
1346                 free(newmsg);
1347                 return -1;
1348         }
1349         p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_SOFT,
1350                         l_alloc, l_bytes, l_addtime, l_usetime);
1351         if (!p) {
1352                 free(newmsg);
1353                 return -1;
1354         }
1355
1356 #ifdef SADB_X_EXT_NAT_T_TYPE
1357         /* Add nat-t messages */
1358         if (l_natt_type) {
1359                 p = pfkey_set_natt_type(p, ep, SADB_X_EXT_NAT_T_TYPE, l_natt_type);
1360                 if (!p) {
1361                         free(newmsg);
1362                         return -1;
1363                 }
1364
1365                 p = pfkey_set_natt_port(p, ep, SADB_X_EXT_NAT_T_SPORT,
1366                                         l_natt_sport);
1367                 if (!p) {
1368                         free(newmsg);
1369                         return -1;
1370                 }
1371
1372                 p = pfkey_set_natt_port(p, ep, SADB_X_EXT_NAT_T_DPORT,
1373                                         l_natt_dport);
1374                 if (!p) {
1375                         free(newmsg);
1376                         return -1;
1377                 }
1378
1379                 if (l_natt_oa) {
1380                         p = pfkey_setsadbaddr(p, ep, SADB_X_EXT_NAT_T_OA,
1381                                               l_natt_oa,
1382                                               PFKEY_ALIGN8(sysdep_sa_len(l_natt_oa)),
1383                                               IPSEC_ULPROTO_ANY);
1384                         if (!p) {
1385                                 free(newmsg);
1386                                 return -1;
1387                         }
1388                 }
1389         }
1390 #endif
1391
1392         if (p != ep) {
1393                 free(newmsg);
1394                 return -1;
1395         }
1396
1397         /* send message */
1398         len = pfkey_send(so, newmsg, len);
1399         free(newmsg);
1400
1401         if (len < 0)
1402                 return -1;
1403
1404         __ipsec_errcode = EIPSEC_NO_ERROR;
1405         return len;
1406 }
1407
1408 /* sending SADB_DELETE or SADB_GET message to the kernel */
1409 static int
1410 pfkey_send_x2(so, type, satype, mode, src, dst, spi)
1411         int so;
1412         u_int type, satype, mode;
1413         struct sockaddr *src, *dst;
1414         u_int32_t spi;
1415 {
1416         struct sadb_msg *newmsg;
1417         int len;
1418         caddr_t p;
1419         int plen;
1420         caddr_t ep;
1421
1422         /* validity check */
1423         if (src == NULL || dst == NULL) {
1424                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1425                 return -1;
1426         }
1427         if (src->sa_family != dst->sa_family) {
1428                 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1429                 return -1;
1430         }
1431         switch (src->sa_family) {
1432         case AF_INET:
1433                 plen = sizeof(struct in_addr) << 3;
1434                 break;
1435         case AF_INET6:
1436                 plen = sizeof(struct in6_addr) << 3;
1437                 break;
1438         default:
1439                 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1440                 return -1;
1441         }
1442
1443         /* create new sadb_msg to reply. */
1444         len = sizeof(struct sadb_msg)
1445                 + sizeof(struct sadb_sa)
1446                 + sizeof(struct sadb_address)
1447                 + PFKEY_ALIGN8(sysdep_sa_len(src))
1448                 + sizeof(struct sadb_address)
1449                 + PFKEY_ALIGN8(sysdep_sa_len(dst));
1450
1451         if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1452                 __ipsec_set_strerror(strerror(errno));
1453                 return -1;
1454         }
1455         ep = ((caddr_t)newmsg) + len;
1456
1457         p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len, satype, 0,
1458             getpid());
1459         if (!p) {
1460                 free(newmsg);
1461                 return -1;
1462         }
1463         p = pfkey_setsadbsa(p, ep, spi, 0, 0, 0, 0);
1464         if (!p) {
1465                 free(newmsg);
1466                 return -1;
1467         }
1468         p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen,
1469             IPSEC_ULPROTO_ANY);
1470         if (!p) {
1471                 free(newmsg);
1472                 return -1;
1473         }
1474         p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen,
1475             IPSEC_ULPROTO_ANY);
1476         if (!p || p != ep) {
1477                 free(newmsg);
1478                 return -1;
1479         }
1480
1481         /* send message */
1482         len = pfkey_send(so, newmsg, len);
1483         free(newmsg);
1484
1485         if (len < 0)
1486                 return -1;
1487
1488         __ipsec_errcode = EIPSEC_NO_ERROR;
1489         return len;
1490 }
1491
1492 /*
1493  * sending SADB_REGISTER, SADB_FLUSH, SADB_DUMP or SADB_X_PROMISC message
1494  * to the kernel
1495  */
1496 static int
1497 pfkey_send_x3(so, type, satype)
1498         int so;
1499         u_int type, satype;
1500 {
1501         struct sadb_msg *newmsg;
1502         int len;
1503         caddr_t p;
1504         caddr_t ep;
1505
1506         /* validity check */
1507         switch (type) {
1508         case SADB_X_PROMISC:
1509                 if (satype != 0 && satype != 1) {
1510                         __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1511                         return -1;
1512                 }
1513                 break;
1514         default:
1515                 switch (satype) {
1516                 case SADB_SATYPE_UNSPEC:
1517                 case SADB_SATYPE_AH:
1518                 case SADB_SATYPE_ESP:
1519                 case SADB_X_SATYPE_IPCOMP:
1520                         break;
1521                 default:
1522                         __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1523                         return -1;
1524                 }
1525         }
1526
1527         /* create new sadb_msg to send. */
1528         len = sizeof(struct sadb_msg);
1529
1530         if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1531                 __ipsec_set_strerror(strerror(errno));
1532                 return -1;
1533         }
1534         ep = ((caddr_t)newmsg) + len;
1535
1536         p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len, satype, 0,
1537             getpid());
1538         if (!p || p != ep) {
1539                 free(newmsg);
1540                 return -1;
1541         }
1542
1543         /* send message */
1544         len = pfkey_send(so, newmsg, len);
1545         free(newmsg);
1546
1547         if (len < 0)
1548                 return -1;
1549
1550         __ipsec_errcode = EIPSEC_NO_ERROR;
1551         return len;
1552 }
1553
1554 /* sending SADB_X_SPDADD message to the kernel */
1555 static int
1556 pfkey_send_x4(so, type, src, prefs, dst, prefd, proto,
1557                 ltime, vtime, policy, policylen, seq)
1558         int so;
1559         struct sockaddr *src, *dst;
1560         u_int type, prefs, prefd, proto;
1561         u_int64_t ltime, vtime;
1562         char *policy;
1563         int policylen;
1564         u_int32_t seq;
1565 {
1566         struct sadb_msg *newmsg;
1567         int len;
1568         caddr_t p;
1569         int plen;
1570         caddr_t ep;
1571
1572         /* validity check */
1573         if (src == NULL || dst == NULL) {
1574                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1575                 return -1;
1576         }
1577         if (src->sa_family != dst->sa_family) {
1578                 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1579                 return -1;
1580         }
1581
1582         switch (src->sa_family) {
1583         case AF_INET:
1584                 plen = sizeof(struct in_addr) << 3;
1585                 break;
1586         case AF_INET6:
1587                 plen = sizeof(struct in6_addr) << 3;
1588                 break;
1589         default:
1590                 __ipsec_errcode = EIPSEC_INVAL_FAMILY;
1591                 return -1;
1592         }
1593         if (prefs > plen || prefd > plen) {
1594                 __ipsec_errcode = EIPSEC_INVAL_PREFIXLEN;
1595                 return -1;
1596         }
1597
1598         /* create new sadb_msg to reply. */
1599         len = sizeof(struct sadb_msg)
1600                 + sizeof(struct sadb_address)
1601                 + PFKEY_ALIGN8(sysdep_sa_len(src))
1602                 + sizeof(struct sadb_address)
1603                 + PFKEY_ALIGN8(sysdep_sa_len(src))
1604                 + sizeof(struct sadb_lifetime)
1605                 + policylen;
1606
1607         if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1608                 __ipsec_set_strerror(strerror(errno));
1609                 return -1;
1610         }
1611         ep = ((caddr_t)newmsg) + len;
1612
1613         p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len,
1614             SADB_SATYPE_UNSPEC, seq, getpid());
1615         if (!p) {
1616                 free(newmsg);
1617                 return -1;
1618         }
1619         p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, prefs, proto);
1620         if (!p) {
1621                 free(newmsg);
1622                 return -1;
1623         }
1624         p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, prefd, proto);
1625         if (!p) {
1626                 free(newmsg);
1627                 return -1;
1628         }
1629         p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_HARD,
1630                         0, 0, ltime, vtime);
1631         if (!p || p + policylen != ep) {
1632                 free(newmsg);
1633                 return -1;
1634         }
1635         memcpy(p, policy, policylen);
1636
1637         /* send message */
1638         len = pfkey_send(so, newmsg, len);
1639         free(newmsg);
1640
1641         if (len < 0)
1642                 return -1;
1643
1644         __ipsec_errcode = EIPSEC_NO_ERROR;
1645         return len;
1646 }
1647
1648 /* sending SADB_X_SPDGET or SADB_X_SPDDELETE message to the kernel */
1649 static int
1650 pfkey_send_x5(so, type, spid)
1651         int so;
1652         u_int type;
1653         u_int32_t spid;
1654 {
1655         struct sadb_msg *newmsg;
1656         struct sadb_x_policy xpl;
1657         int len;
1658         caddr_t p;
1659         caddr_t ep;
1660
1661         /* create new sadb_msg to reply. */
1662         len = sizeof(struct sadb_msg)
1663                 + sizeof(xpl);
1664
1665         if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) {
1666                 __ipsec_set_strerror(strerror(errno));
1667                 return -1;
1668         }
1669         ep = ((caddr_t)newmsg) + len;
1670
1671         p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len,
1672             SADB_SATYPE_UNSPEC, 0, getpid());
1673         if (!p) {
1674                 free(newmsg);
1675                 return -1;
1676         }
1677
1678         if (p + sizeof(xpl) != ep) {
1679                 free(newmsg);
1680                 return -1;
1681         }
1682         memset(&xpl, 0, sizeof(xpl));
1683         xpl.sadb_x_policy_len = PFKEY_UNIT64(sizeof(xpl));
1684         xpl.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1685         xpl.sadb_x_policy_id = spid;
1686         memcpy(p, &xpl, sizeof(xpl));
1687
1688         /* send message */
1689         len = pfkey_send(so, newmsg, len);
1690         free(newmsg);
1691
1692         if (len < 0)
1693                 return -1;
1694
1695         __ipsec_errcode = EIPSEC_NO_ERROR;
1696         return len;
1697 }
1698
1699 /*
1700  * open a socket.
1701  * OUT:
1702  *      -1: fail.
1703  *      others : success and return value of socket.
1704  */
1705 int
1706 pfkey_open()
1707 {
1708         int so;
1709         const int bufsiz = 128 * 1024;  /*is 128K enough?*/
1710
1711         if ((so = socket(PF_KEY, SOCK_RAW, PF_KEY_V2)) < 0) {
1712                 __ipsec_set_strerror(strerror(errno));
1713                 return -1;
1714         }
1715
1716         /*
1717          * This is a temporary workaround for KAME PR 154.
1718          * Don't really care even if it fails.
1719          */
1720         (void)setsockopt(so, SOL_SOCKET, SO_SNDBUF, &bufsiz, sizeof(bufsiz));
1721         (void)setsockopt(so, SOL_SOCKET, SO_RCVBUF, &bufsiz, sizeof(bufsiz));
1722
1723         __ipsec_errcode = EIPSEC_NO_ERROR;
1724         return so;
1725 }
1726
1727 /*
1728  * close a socket.
1729  * OUT:
1730  *       0: success.
1731  *      -1: fail.
1732  */
1733 void
1734 pfkey_close(so)
1735         int so;
1736 {
1737         (void)close(so);
1738
1739         __ipsec_errcode = EIPSEC_NO_ERROR;
1740         return;
1741 }
1742
1743 /*
1744  * receive sadb_msg data, and return pointer to new buffer allocated.
1745  * Must free this buffer later.
1746  * OUT:
1747  *      NULL    : error occured.
1748  *      others  : a pointer to sadb_msg structure.
1749  *
1750  * XXX should be rewritten to pass length explicitly
1751  */
1752 struct sadb_msg *
1753 pfkey_recv(so)
1754         int so;
1755 {
1756         struct sadb_msg buf, *newmsg;
1757         int len, reallen;
1758
1759         while ((len = recv(so, (caddr_t)&buf, sizeof(buf), MSG_PEEK)) < 0) {
1760                 if (errno == EINTR)
1761                         continue;
1762                 __ipsec_set_strerror(strerror(errno));
1763                 return NULL;
1764         }
1765
1766         if (len < sizeof(buf)) {
1767                 recv(so, (caddr_t)&buf, sizeof(buf), 0);
1768                 __ipsec_errcode = EIPSEC_MAX;
1769                 return NULL;
1770         }
1771
1772         /* read real message */
1773         reallen = PFKEY_UNUNIT64(buf.sadb_msg_len);
1774         if ((newmsg = CALLOC(reallen, struct sadb_msg *)) == 0) {
1775                 __ipsec_set_strerror(strerror(errno));
1776                 return NULL;
1777         }
1778
1779         while ((len = recv(so, (caddr_t)newmsg, reallen, 0)) < 0) {
1780                 if (errno == EINTR)
1781                         continue;
1782                 __ipsec_set_strerror(strerror(errno));
1783                 free(newmsg);
1784                 return NULL;
1785         }
1786
1787         if (len != reallen) {
1788                 __ipsec_errcode = EIPSEC_SYSTEM_ERROR;
1789                 free(newmsg);
1790                 return NULL;
1791         }
1792
1793         /* don't trust what the kernel says, validate! */
1794         if (PFKEY_UNUNIT64(newmsg->sadb_msg_len) != len) {
1795                 __ipsec_errcode = EIPSEC_SYSTEM_ERROR;
1796                 free(newmsg);
1797                 return NULL;
1798         }
1799
1800         __ipsec_errcode = EIPSEC_NO_ERROR;
1801         return newmsg;
1802 }
1803
1804 /*
1805  * send message to a socket.
1806  * OUT:
1807  *       others: success and return length sent.
1808  *      -1     : fail.
1809  */
1810 int
1811 pfkey_send(so, msg, len)
1812         int so;
1813         struct sadb_msg *msg;
1814         int len;
1815 {
1816         if ((len = send(so, (caddr_t)msg, len, 0)) < 0) {
1817                 __ipsec_set_strerror(strerror(errno));
1818                 return -1;
1819         }
1820
1821         __ipsec_errcode = EIPSEC_NO_ERROR;
1822         return len;
1823 }
1824
1825 /*
1826  * %%% Utilities
1827  * NOTE: These functions are derived from netkey/key.c in KAME.
1828  */
1829 /*
1830  * set the pointer to each header in this message buffer.
1831  * IN:  msg: pointer to message buffer.
1832  *      mhp: pointer to the buffer initialized like below:
1833  *              caddr_t mhp[SADB_EXT_MAX + 1];
1834  * OUT: -1: invalid.
1835  *       0: valid.
1836  *
1837  * XXX should be rewritten to obtain length explicitly
1838  */
1839 int
1840 pfkey_align(msg, mhp)
1841         struct sadb_msg *msg;
1842         caddr_t *mhp;
1843 {
1844         struct sadb_ext *ext;
1845         int i;
1846         caddr_t p;
1847         caddr_t ep;     /* XXX should be passed from upper layer */
1848
1849         /* validity check */
1850         if (msg == NULL || mhp == NULL) {
1851                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1852                 return -1;
1853         }
1854
1855         /* initialize */
1856         for (i = 0; i < SADB_EXT_MAX + 1; i++)
1857                 mhp[i] = NULL;
1858
1859         mhp[0] = (caddr_t)msg;
1860
1861         /* initialize */
1862         p = (caddr_t) msg;
1863         ep = p + PFKEY_UNUNIT64(msg->sadb_msg_len);
1864
1865         /* skip base header */
1866         p += sizeof(struct sadb_msg);
1867
1868         while (p < ep) {
1869                 ext = (struct sadb_ext *)p;
1870                 if (ep < p + sizeof(*ext) || PFKEY_EXTLEN(ext) < sizeof(*ext) ||
1871                     ep < p + PFKEY_EXTLEN(ext)) {
1872                         /* invalid format */
1873                         break;
1874                 }
1875
1876                 /* duplicate check */
1877                 /* XXX Are there duplication either KEY_AUTH or KEY_ENCRYPT ?*/
1878                 if (mhp[ext->sadb_ext_type] != NULL) {
1879                         __ipsec_errcode = EIPSEC_INVAL_EXTTYPE;
1880                         return -1;
1881                 }
1882
1883                 /* set pointer */
1884                 switch (ext->sadb_ext_type) {
1885                 case SADB_EXT_SA:
1886                 case SADB_EXT_LIFETIME_CURRENT:
1887                 case SADB_EXT_LIFETIME_HARD:
1888                 case SADB_EXT_LIFETIME_SOFT:
1889                 case SADB_EXT_ADDRESS_SRC:
1890                 case SADB_EXT_ADDRESS_DST:
1891                 case SADB_EXT_ADDRESS_PROXY:
1892                 case SADB_EXT_KEY_AUTH:
1893                         /* XXX should to be check weak keys. */
1894                 case SADB_EXT_KEY_ENCRYPT:
1895                         /* XXX should to be check weak keys. */
1896                 case SADB_EXT_IDENTITY_SRC:
1897                 case SADB_EXT_IDENTITY_DST:
1898                 case SADB_EXT_SENSITIVITY:
1899                 case SADB_EXT_PROPOSAL:
1900                 case SADB_EXT_SUPPORTED_AUTH:
1901                 case SADB_EXT_SUPPORTED_ENCRYPT:
1902                 case SADB_EXT_SPIRANGE:
1903                 case SADB_X_EXT_POLICY:
1904                 case SADB_X_EXT_SA2:
1905 #ifdef SADB_X_EXT_NAT_T_TYPE
1906                 case SADB_X_EXT_NAT_T_TYPE:
1907                 case SADB_X_EXT_NAT_T_SPORT:
1908                 case SADB_X_EXT_NAT_T_DPORT:
1909                 case SADB_X_EXT_NAT_T_OA:
1910 #endif
1911 #ifdef SADB_X_EXT_TAG
1912                 case SADB_X_EXT_TAG:
1913 #endif
1914                         mhp[ext->sadb_ext_type] = (caddr_t)ext;
1915                         break;
1916                 default:
1917                         __ipsec_errcode = EIPSEC_INVAL_EXTTYPE;
1918                         return -1;
1919                 }
1920
1921                 p += PFKEY_EXTLEN(ext);
1922         }
1923
1924         if (p != ep) {
1925                 __ipsec_errcode = EIPSEC_INVAL_SADBMSG;
1926                 return -1;
1927         }
1928
1929         __ipsec_errcode = EIPSEC_NO_ERROR;
1930         return 0;
1931 }
1932
1933 /*
1934  * check basic usage for sadb_msg,
1935  * NOTE: This routine is derived from netkey/key.c in KAME.
1936  * IN:  msg: pointer to message buffer.
1937  *      mhp: pointer to the buffer initialized like below:
1938  *
1939  *              caddr_t mhp[SADB_EXT_MAX + 1];
1940  *
1941  * OUT: -1: invalid.
1942  *       0: valid.
1943  */
1944 int
1945 pfkey_check(mhp)
1946         caddr_t *mhp;
1947 {
1948         struct sadb_msg *msg;
1949
1950         /* validity check */
1951         if (mhp == NULL || mhp[0] == NULL) {
1952                 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1953                 return -1;
1954         }
1955
1956         msg = (struct sadb_msg *)mhp[0];
1957
1958         /* check version */
1959         if (msg->sadb_msg_version != PF_KEY_V2) {
1960                 __ipsec_errcode = EIPSEC_INVAL_VERSION;
1961                 return -1;
1962         }
1963
1964         /* check type */
1965         if (msg->sadb_msg_type > SADB_MAX) {
1966                 __ipsec_errcode = EIPSEC_INVAL_MSGTYPE;
1967                 return -1;
1968         }
1969
1970         /* check SA type */
1971         switch (msg->sadb_msg_satype) {
1972         case SADB_SATYPE_UNSPEC:
1973                 switch (msg->sadb_msg_type) {
1974                 case SADB_GETSPI:
1975                 case SADB_UPDATE:
1976                 case SADB_ADD:
1977                 case SADB_DELETE:
1978                 case SADB_GET:
1979                 case SADB_ACQUIRE:
1980                 case SADB_EXPIRE:
1981 #ifdef SADB_X_NAT_T_NEW_MAPPING
1982                 case SADB_X_NAT_T_NEW_MAPPING:
1983 #endif
1984                         __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1985                         return -1;
1986                 }
1987                 break;
1988         case SADB_SATYPE_ESP:
1989         case SADB_SATYPE_AH:
1990         case SADB_X_SATYPE_IPCOMP:
1991                 switch (msg->sadb_msg_type) {
1992                 case SADB_X_SPDADD:
1993                 case SADB_X_SPDDELETE:
1994                 case SADB_X_SPDGET:
1995                 case SADB_X_SPDDUMP:
1996                 case SADB_X_SPDFLUSH:
1997                         __ipsec_errcode = EIPSEC_INVAL_SATYPE;
1998                         return -1;
1999                 }
2000 #ifdef SADB_X_NAT_T_NEW_MAPPING
2001                 if (msg->sadb_msg_type == SADB_X_NAT_T_NEW_MAPPING &&
2002                     msg->sadb_msg_satype != SADB_SATYPE_ESP) {
2003                         __ipsec_errcode = EIPSEC_INVAL_SATYPE;
2004                         return -1;
2005                 }
2006 #endif
2007                 break;
2008         case SADB_SATYPE_RSVP:
2009         case SADB_SATYPE_OSPFV2:
2010         case SADB_SATYPE_RIPV2:
2011         case SADB_SATYPE_MIP:
2012                 __ipsec_errcode = EIPSEC_NOT_SUPPORTED;
2013                 return -1;
2014         case 1: /* XXX: What does it do ? */
2015                 if (msg->sadb_msg_type == SADB_X_PROMISC)
2016                         break;
2017                 /*FALLTHROUGH*/
2018         default:
2019                 __ipsec_errcode = EIPSEC_INVAL_SATYPE;
2020                 return -1;
2021         }
2022
2023         /* check field of upper layer protocol and address family */
2024         if (mhp[SADB_EXT_ADDRESS_SRC] != NULL
2025          && mhp[SADB_EXT_ADDRESS_DST] != NULL) {
2026                 struct sadb_address *src0, *dst0;
2027
2028                 src0 = (struct sadb_address *)(mhp[SADB_EXT_ADDRESS_SRC]);
2029                 dst0 = (struct sadb_address *)(mhp[SADB_EXT_ADDRESS_DST]);
2030
2031                 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
2032                         __ipsec_errcode = EIPSEC_PROTO_MISMATCH;
2033                         return -1;
2034                 }
2035
2036                 if (PFKEY_ADDR_SADDR(src0)->sa_family
2037                  != PFKEY_ADDR_SADDR(dst0)->sa_family) {
2038                         __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
2039                         return -1;
2040                 }
2041
2042                 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
2043                 case AF_INET:
2044                 case AF_INET6:
2045                         break;
2046                 default:
2047                         __ipsec_errcode = EIPSEC_INVAL_FAMILY;
2048                         return -1;
2049                 }
2050
2051                 /*
2052                  * prefixlen == 0 is valid because there must be the case
2053                  * all addresses are matched.
2054                  */
2055         }
2056
2057         __ipsec_errcode = EIPSEC_NO_ERROR;
2058         return 0;
2059 }
2060
2061 /*
2062  * set data into sadb_msg.
2063  * `buf' must has been allocated sufficiently.
2064  */
2065 static caddr_t
2066 pfkey_setsadbmsg(buf, lim, type, tlen, satype, seq, pid)
2067         caddr_t buf;
2068         caddr_t lim;
2069         u_int type, satype;
2070         u_int tlen;
2071         u_int32_t seq;
2072         pid_t pid;
2073 {
2074         struct sadb_msg *p;
2075         u_int len;
2076
2077         p = (struct sadb_msg *)buf;
2078         len = sizeof(struct sadb_msg);
2079
2080         if (buf + len > lim)
2081                 return NULL;
2082
2083         memset(p, 0, len);
2084         p->sadb_msg_version = PF_KEY_V2;
2085         p->sadb_msg_type = type;
2086         p->sadb_msg_errno = 0;
2087         p->sadb_msg_satype = satype;
2088         p->sadb_msg_len = PFKEY_UNIT64(tlen);
2089         p->sadb_msg_reserved = 0;
2090         p->sadb_msg_seq = seq;
2091         p->sadb_msg_pid = (u_int32_t)pid;
2092
2093         return(buf + len);
2094 }
2095
2096 /*
2097  * copy secasvar data into sadb_address.
2098  * `buf' must has been allocated sufficiently.
2099  */
2100 static caddr_t
2101 pfkey_setsadbsa(buf, lim, spi, wsize, auth, enc, flags)
2102         caddr_t buf;
2103         caddr_t lim;
2104         u_int32_t spi, flags;
2105         u_int wsize, auth, enc;
2106 {
2107         struct sadb_sa *p;
2108         u_int len;
2109
2110         p = (struct sadb_sa *)buf;
2111         len = sizeof(struct sadb_sa);
2112
2113         if (buf + len > lim)
2114                 return NULL;
2115
2116         memset(p, 0, len);
2117         p->sadb_sa_len = PFKEY_UNIT64(len);
2118         p->sadb_sa_exttype = SADB_EXT_SA;
2119         p->sadb_sa_spi = spi;
2120         p->sadb_sa_replay = wsize;
2121         p->sadb_sa_state = SADB_SASTATE_LARVAL;
2122         p->sadb_sa_auth = auth;
2123         p->sadb_sa_encrypt = enc;
2124         p->sadb_sa_flags = flags;
2125
2126         return(buf + len);
2127 }
2128
2129 /*
2130  * set data into sadb_address.
2131  * `buf' must has been allocated sufficiently.
2132  * prefixlen is in bits.
2133  */
2134 static caddr_t
2135 pfkey_setsadbaddr(buf, lim, exttype, saddr, prefixlen, ul_proto)
2136         caddr_t buf;
2137         caddr_t lim;
2138         u_int exttype;
2139         struct sockaddr *saddr;
2140         u_int prefixlen;
2141         u_int ul_proto;
2142 {
2143         struct sadb_address *p;
2144         u_int len;
2145
2146         p = (struct sadb_address *)buf;
2147         len = sizeof(struct sadb_address) + PFKEY_ALIGN8(sysdep_sa_len(saddr));
2148
2149         if (buf + len > lim)
2150                 return NULL;
2151
2152         memset(p, 0, len);
2153         p->sadb_address_len = PFKEY_UNIT64(len);
2154         p->sadb_address_exttype = exttype & 0xffff;
2155         p->sadb_address_proto = ul_proto & 0xff;
2156         p->sadb_address_prefixlen = prefixlen;
2157         p->sadb_address_reserved = 0;
2158
2159         memcpy(p + 1, saddr, sysdep_sa_len(saddr));
2160
2161         return(buf + len);
2162 }
2163
2164 /*
2165  * set sadb_key structure after clearing buffer with zero.
2166  * OUT: the pointer of buf + len.
2167  */
2168 static caddr_t
2169 pfkey_setsadbkey(buf, lim, type, key, keylen)
2170         caddr_t buf;
2171         caddr_t lim;
2172         caddr_t key;
2173         u_int type, keylen;
2174 {
2175         struct sadb_key *p;
2176         u_int len;
2177
2178         p = (struct sadb_key *)buf;
2179         len = sizeof(struct sadb_key) + PFKEY_ALIGN8(keylen);
2180
2181         if (buf + len > lim)
2182                 return NULL;
2183
2184         memset(p, 0, len);
2185         p->sadb_key_len = PFKEY_UNIT64(len);
2186         p->sadb_key_exttype = type;
2187         p->sadb_key_bits = keylen << 3;
2188         p->sadb_key_reserved = 0;
2189
2190         memcpy(p + 1, key, keylen);
2191
2192         return buf + len;
2193 }
2194
2195 /*
2196  * set sadb_lifetime structure after clearing buffer with zero.
2197  * OUT: the pointer of buf + len.
2198  */
2199 static caddr_t
2200 pfkey_setsadblifetime(buf, lim, type, l_alloc, l_bytes, l_addtime, l_usetime)
2201         caddr_t buf;
2202         caddr_t lim;
2203         u_int type;
2204         u_int32_t l_alloc, l_bytes, l_addtime, l_usetime;
2205 {
2206         struct sadb_lifetime *p;
2207         u_int len;
2208
2209         p = (struct sadb_lifetime *)buf;
2210         len = sizeof(struct sadb_lifetime);
2211
2212         if (buf + len > lim)
2213                 return NULL;
2214
2215         memset(p, 0, len);
2216         p->sadb_lifetime_len = PFKEY_UNIT64(len);
2217         p->sadb_lifetime_exttype = type;
2218
2219         switch (type) {
2220         case SADB_EXT_LIFETIME_SOFT:
2221                 p->sadb_lifetime_allocations
2222                         = (l_alloc * soft_lifetime_allocations_rate) /100;
2223                 p->sadb_lifetime_bytes
2224                         = (l_bytes * soft_lifetime_bytes_rate) /100;
2225                 p->sadb_lifetime_addtime
2226                         = (l_addtime * soft_lifetime_addtime_rate) /100;
2227                 p->sadb_lifetime_usetime
2228                         = (l_usetime * soft_lifetime_usetime_rate) /100;
2229                 break;
2230         case SADB_EXT_LIFETIME_HARD:
2231                 p->sadb_lifetime_allocations = l_alloc;
2232                 p->sadb_lifetime_bytes = l_bytes;
2233                 p->sadb_lifetime_addtime = l_addtime;
2234                 p->sadb_lifetime_usetime = l_usetime;
2235                 break;
2236         }
2237
2238         return buf + len;
2239 }
2240
2241 /*
2242  * copy secasvar data into sadb_address.
2243  * `buf' must has been allocated sufficiently.
2244  */
2245 static caddr_t
2246 pfkey_setsadbxsa2(buf, lim, mode0, reqid)
2247         caddr_t buf;
2248         caddr_t lim;
2249         u_int32_t mode0;
2250         u_int32_t reqid;
2251 {
2252         struct sadb_x_sa2 *p;
2253         u_int8_t mode = mode0 & 0xff;
2254         u_int len;
2255
2256         p = (struct sadb_x_sa2 *)buf;
2257         len = sizeof(struct sadb_x_sa2);
2258
2259         if (buf + len > lim)
2260                 return NULL;
2261
2262         memset(p, 0, len);
2263         p->sadb_x_sa2_len = PFKEY_UNIT64(len);
2264         p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
2265         p->sadb_x_sa2_mode = mode;
2266         p->sadb_x_sa2_reqid = reqid;
2267
2268         return(buf + len);
2269 }
2270
2271 #ifdef SADB_X_EXT_NAT_T_TYPE
2272 static caddr_t
2273 pfkey_set_natt_type(buf, lim, type, l_natt_type)
2274         caddr_t buf;
2275         caddr_t lim;
2276         u_int type;
2277         u_int8_t l_natt_type;
2278 {
2279         struct sadb_x_nat_t_type *p;
2280         u_int len;
2281
2282         p = (struct sadb_x_nat_t_type *)buf;
2283         len = sizeof(struct sadb_x_nat_t_type);
2284
2285         if (buf + len > lim)
2286                 return NULL;
2287
2288         memset(p, 0, len);
2289         p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
2290         p->sadb_x_nat_t_type_exttype = type;
2291         p->sadb_x_nat_t_type_type = l_natt_type;
2292
2293         return(buf + len);
2294 }
2295
2296 static caddr_t
2297 pfkey_set_natt_port(buf, lim, type, l_natt_port)
2298         caddr_t buf;
2299         caddr_t lim;
2300         u_int type;
2301         u_int16_t l_natt_port;
2302 {
2303         struct sadb_x_nat_t_port *p;
2304         u_int len;
2305
2306         p = (struct sadb_x_nat_t_port *)buf;
2307         len = sizeof(struct sadb_x_nat_t_port);
2308
2309         if (buf + len > lim)
2310                 return NULL;
2311
2312         memset(p, 0, len);
2313         p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
2314         p->sadb_x_nat_t_port_exttype = type;
2315         p->sadb_x_nat_t_port_port = htons(l_natt_port);
2316
2317         return(buf + len);
2318 }
2319 #endif