1/*	$NetBSD: libpfkey.h,v 1.12.4.1 2007/08/01 11:52:18 vanhu Exp $	*/
2
3/* Id: libpfkey.h,v 1.13 2005/12/04 20:26:43 manubsd Exp */
4
5/*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef _LIBPFKEY_H
35#define _LIBPFKEY_H
36
37#ifndef KAME_LIBPFKEY_H
38#define KAME_LIBPFKEY_H
39
40#define PRIORITY_LOW        0xC0000000
41#define PRIORITY_DEFAULT    0x80000000
42#define PRIORITY_HIGH       0x40000000
43
44#define PRIORITY_OFFSET_POSITIVE_MAX	0x3fffffff
45#define PRIORITY_OFFSET_NEGATIVE_MAX	0x40000000
46
47struct sadb_msg;
48extern void pfkey_sadump __P((struct sadb_msg *));
49extern void pfkey_sadump_withports __P((struct sadb_msg *));
50extern void pfkey_spdump __P((struct sadb_msg *));
51extern void pfkey_spdump_withports __P((struct sadb_msg *));
52
53struct sockaddr;
54struct sadb_alg;
55
56/* Accomodate different prototypes in <netinet6/ipsec.h> */
57#include <sys/types.h>
58#include PATH_IPSEC_H
59
60#ifndef HAVE_IPSEC_POLICY_T
61typedef caddr_t ipsec_policy_t;
62#define __ipsec_const
63#else
64#define __ipsec_const const
65#endif
66
67struct pfkey_send_sa_args {
68	int 		so;			/* socket */
69	u_int		type;
70	u_int 		satype;
71	u_int		mode;
72	struct sockaddr *src;			/* IP src address for SA */
73	struct sockaddr *dst;			/* IP dst address for SA */
74	u_int32_t 	spi;			/* SA's spi */
75	u_int32_t 	reqid;
76	u_int		wsize;
77	caddr_t		keymat;
78	u_int		e_type, e_keylen;	/* Encryption alg and keylen */
79	u_int		a_type, a_keylen;	/* Authentication alg and key */
80	u_int		flags;
81	u_int32_t	l_alloc;
82	u_int32_t	l_bytes;
83	u_int32_t	l_addtime;
84	u_int32_t	l_usetime;
85	u_int32_t	seq;
86	u_int8_t	l_natt_type;
87	u_int16_t	l_natt_sport, l_natt_dport;
88	struct sockaddr *l_natt_oa;
89	u_int16_t	l_natt_frag;
90	u_int8_t ctxdoi, ctxalg;	/* Security context DOI and algorithm */
91	caddr_t ctxstr;			/* Security context string */
92	u_int16_t ctxstrlen;		/* length of security context string */
93};
94
95/* The options built into libipsec */
96extern int libipsec_opt;
97#define LIBIPSEC_OPT_NATT		0x01
98#define LIBIPSEC_OPT_FRAG		0x02
99#define LIBIPSEC_OPT_SEC_CTX		0x04
100
101/* IPsec Library Routines */
102
103int ipsec_check_keylen __P((u_int, u_int, u_int));
104int ipsec_check_keylen2 __P((u_int, u_int, u_int));
105int ipsec_get_keylen __P((u_int, u_int, struct sadb_alg *));
106char *ipsec_dump_policy_withports __P((void *, const char *));
107void ipsec_hexdump __P((const void *, int));
108const char *ipsec_strerror __P((void));
109void kdebug_sadb __P((struct sadb_msg *));
110ipsec_policy_t ipsec_set_policy __P((__ipsec_const char *, int));
111int  ipsec_get_policylen __P((ipsec_policy_t));
112char *ipsec_dump_policy __P((ipsec_policy_t, __ipsec_const char *));
113
114/* PFKey Routines */
115
116u_int pfkey_set_softrate __P((u_int, u_int));
117u_int pfkey_get_softrate __P((u_int));
118int pfkey_send_getspi __P((int, u_int, u_int, struct sockaddr *,
119	struct sockaddr *, u_int32_t, u_int32_t, u_int32_t, u_int32_t));
120int pfkey_send_update2 __P((struct pfkey_send_sa_args *));
121int pfkey_send_add2 __P((struct pfkey_send_sa_args *));
122int pfkey_send_delete __P((int, u_int, u_int,
123	struct sockaddr *, struct sockaddr *, u_int32_t));
124int pfkey_send_delete_all __P((int, u_int, u_int,
125	struct sockaddr *, struct sockaddr *));
126int pfkey_send_get __P((int, u_int, u_int,
127	struct sockaddr *, struct sockaddr *, u_int32_t));
128int pfkey_send_register __P((int, u_int));
129int pfkey_recv_register __P((int));
130int pfkey_set_supported __P((struct sadb_msg *, int));
131int pfkey_send_flush __P((int, u_int));
132int pfkey_send_dump __P((int, u_int));
133int pfkey_send_promisc_toggle __P((int, int));
134int pfkey_send_spdadd __P((int, struct sockaddr *, u_int,
135	struct sockaddr *, u_int, u_int, caddr_t, int, u_int32_t));
136int pfkey_send_spdadd2 __P((int, struct sockaddr *, u_int,
137	struct sockaddr *, u_int, u_int, u_int64_t, u_int64_t,
138	caddr_t, int, u_int32_t));
139int pfkey_send_spdupdate __P((int, struct sockaddr *, u_int,
140	struct sockaddr *, u_int, u_int, caddr_t, int, u_int32_t));
141int pfkey_send_spdupdate2 __P((int, struct sockaddr *, u_int,
142	struct sockaddr *, u_int, u_int, u_int64_t, u_int64_t,
143	caddr_t, int, u_int32_t));
144int pfkey_send_spddelete __P((int, struct sockaddr *, u_int,
145	struct sockaddr *, u_int, u_int, caddr_t, int, u_int32_t));
146int pfkey_send_spddelete2 __P((int, u_int32_t));
147int pfkey_send_spdget __P((int, u_int32_t));
148int pfkey_send_spdsetidx __P((int, struct sockaddr *, u_int,
149	struct sockaddr *, u_int, u_int, caddr_t, int, u_int32_t));
150int pfkey_send_spdflush __P((int));
151int pfkey_send_spddump __P((int));
152#ifdef SADB_X_MIGRATE
153int pfkey_send_migrate __P((int, struct sockaddr *, u_int,
154	struct sockaddr *, u_int, u_int, caddr_t, int, u_int32_t));
155#endif
156
157int pfkey_open __P((void));
158void pfkey_close __P((int));
159struct sadb_msg *pfkey_recv __P((int));
160int pfkey_send __P((int, struct sadb_msg *, int));
161int pfkey_align __P((struct sadb_msg *, caddr_t *));
162int pfkey_check __P((caddr_t *));
163
164/*
165 * Deprecated, available for backward compatibility with third party
166 * libipsec users. Please use pfkey_send_update2 and pfkey_send_add2 instead
167 */
168int pfkey_send_update __P((int, u_int, u_int, struct sockaddr *,
169	struct sockaddr *, u_int32_t, u_int32_t, u_int,
170	caddr_t, u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int64_t,
171	u_int64_t, u_int64_t, u_int32_t));
172int pfkey_send_update_nat __P((int, u_int, u_int, struct sockaddr *,
173	struct sockaddr *, u_int32_t, u_int32_t, u_int,
174	caddr_t, u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int64_t,
175	u_int64_t, u_int64_t, u_int32_t,
176	u_int8_t, u_int16_t, u_int16_t, struct sockaddr *, u_int16_t));
177int pfkey_send_add __P((int, u_int, u_int, struct sockaddr *,
178	struct sockaddr *, u_int32_t, u_int32_t, u_int,
179	caddr_t, u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int64_t,
180	u_int64_t, u_int64_t, u_int32_t));
181int pfkey_send_add_nat __P((int, u_int, u_int, struct sockaddr *,
182	struct sockaddr *, u_int32_t, u_int32_t, u_int,
183	caddr_t, u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int64_t,
184	u_int64_t, u_int64_t, u_int32_t,
185	u_int8_t, u_int16_t, u_int16_t, struct sockaddr *, u_int16_t));
186
187#ifndef __SYSDEP_SA_LEN__
188#define __SYSDEP_SA_LEN__
189#include <netinet/in.h>
190
191#ifndef IPPROTO_IPV4
192#define IPPROTO_IPV4 IPPROTO_IPIP
193#endif
194
195#ifndef IPPROTO_IPCOMP
196#define IPPROTO_IPCOMP IPPROTO_COMP
197#endif
198
199static __inline u_int8_t
200sysdep_sa_len (const struct sockaddr *sa)
201{
202#ifdef __linux__
203  switch (sa->sa_family)
204    {
205    case AF_INET:
206      return sizeof (struct sockaddr_in);
207    case AF_INET6:
208      return sizeof (struct sockaddr_in6);
209    }
210  // log_print ("sysdep_sa_len: unknown sa family %d", sa->sa_family);
211  return sizeof (struct sockaddr_in);
212#else
213  return sa->sa_len;
214#endif
215}
216#endif
217
218#endif /* KAME_LIBPFKEY_H */
219
220#endif /* _LIBPFKEY_H */
221