Home | History | Annotate | Line # | Download | only in netipsec
ipsec.h revision 1.41
      1 /*	$NetBSD: ipsec.h,v 1.41 2017/04/19 03:39:14 ozaki-r Exp $	*/
      2 /*	$FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/netipsec/ipsec.h,v 1.2.4.2 2004/02/14 22:23:23 bms Exp $	*/
      3 /*	$KAME: ipsec.h,v 1.53 2001/11/20 08:32:38 itojun 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 /*
     35  * IPsec controller part.
     36  */
     37 
     38 #ifndef _NETIPSEC_IPSEC_H_
     39 #define _NETIPSEC_IPSEC_H_
     40 
     41 #if defined(_KERNEL_OPT)
     42 #include "opt_inet.h"
     43 #include "opt_ipsec.h"
     44 #endif
     45 
     46 #include <net/pfkeyv2.h>
     47 
     48 #ifdef _KERNEL
     49 
     50 #include <netipsec/keydb.h>
     51 
     52 /*
     53  * Security Policy Index
     54  * Ensure that both address families in the "src" and "dst" are same.
     55  * When the value of the ul_proto is ICMPv6, the port field in "src"
     56  * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
     57  */
     58 struct secpolicyindex {
     59 	u_int8_t dir;			/* direction of packet flow, see blow */
     60 	union sockaddr_union src;	/* IP src address for SP */
     61 	union sockaddr_union dst;	/* IP dst address for SP */
     62 	u_int8_t prefs;			/* prefix length in bits for src */
     63 	u_int8_t prefd;			/* prefix length in bits for dst */
     64 	u_int16_t ul_proto;		/* upper layer Protocol */
     65 #ifdef notyet
     66 	uid_t uids;
     67 	uid_t uidd;
     68 	gid_t gids;
     69 	gid_t gidd;
     70 #endif
     71 };
     72 
     73 /* Security Policy Data Base */
     74 struct secpolicy {
     75 	LIST_ENTRY(secpolicy) chain;
     76 
     77 	u_int refcnt;			/* reference count */
     78 	struct secpolicyindex spidx;	/* selector */
     79 	u_int32_t id;			/* It's unique number on the system. */
     80 	u_int state;			/* 0: dead, others: alive */
     81 #define IPSEC_SPSTATE_DEAD	0
     82 #define IPSEC_SPSTATE_ALIVE	1
     83 
     84 	u_int policy;		/* DISCARD, NONE or IPSEC, see keyv2.h */
     85 	struct ipsecrequest *req;
     86 				/* pointer to the ipsec request tree, */
     87 				/* if policy == IPSEC else this value == NULL.*/
     88 
     89 	/*
     90 	 * lifetime handler.
     91 	 * the policy can be used without limitiation if both lifetime and
     92 	 * validtime are zero.
     93 	 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
     94 	 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
     95 	 */
     96 	time_t created;		/* time created the policy */
     97 	time_t lastused;	/* updated every when kernel sends a packet */
     98 	time_t lifetime;	/* duration of the lifetime of this policy */
     99 	time_t validtime;	/* duration this policy is valid without use */
    100 };
    101 
    102 /* Request for IPsec */
    103 struct ipsecrequest {
    104 	struct ipsecrequest *next;
    105 				/* pointer to next structure */
    106 				/* If NULL, it means the end of chain. */
    107 	struct secasindex saidx;/* hint for search proper SA */
    108 				/* if __ss_len == 0 then no address specified.*/
    109 	u_int level;		/* IPsec level defined below. */
    110 
    111 	struct secasvar *sav;	/* place holder of SA for use */
    112 	struct secpolicy *sp;	/* back pointer to SP */
    113 };
    114 
    115 /* security policy in PCB */
    116 struct inpcbpolicy {
    117 	struct secpolicy *sp_in;
    118 	struct secpolicy *sp_out;
    119 	int priv;			/* privileged socket ? */
    120 
    121 	/* cached policy */
    122 	struct {
    123 		struct secpolicy *cachesp;
    124 		struct secpolicyindex cacheidx;
    125 		int cachehint;		/* processing requirement hint: */
    126 #define	IPSEC_PCBHINT_MAYBE	0	/* IPsec processing maybe required */
    127 #define	IPSEC_PCBHINT_YES	1	/* IPsec processing is required */
    128 #define	IPSEC_PCBHINT_NO	2	/* IPsec processing not required */
    129 		u_int cachegen;		/* spdgen when cache filled */
    130 	} sp_cache[3];			/* XXX 3 == IPSEC_DIR_MAX */
    131 	int sp_cacheflags;
    132 #define	IPSEC_PCBSP_CONNECTED	1
    133 };
    134 
    135 #define	IPSEC_PCB_SKIP_IPSEC(inpp, dir)					\
    136 	((inpp)->sp_cache[(dir)].cachehint == IPSEC_PCBHINT_NO &&	\
    137 	 (inpp)->sp_cache[(dir)].cachegen == ipsec_spdgen)
    138 
    139 /* SP acquiring list table. */
    140 struct secspacq {
    141 	LIST_ENTRY(secspacq) chain;
    142 
    143 	struct secpolicyindex spidx;
    144 
    145 	time_t created;		/* for lifetime */
    146 	int count;		/* for lifetime */
    147 	/* XXX: here is mbuf place holder to be sent ? */
    148 };
    149 #endif /* _KERNEL */
    150 
    151 /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
    152 #define IPSEC_PORT_ANY		0
    153 #define IPSEC_ULPROTO_ANY	255
    154 #define IPSEC_PROTO_ANY		255
    155 
    156 /* mode of security protocol */
    157 /* NOTE: DON'T use IPSEC_MODE_ANY at SPD.  It's only use in SAD */
    158 #define	IPSEC_MODE_ANY		0	/* i.e. wildcard. */
    159 #define	IPSEC_MODE_TRANSPORT	1
    160 #define	IPSEC_MODE_TUNNEL	2
    161 #define	IPSEC_MODE_TCPMD5	3	/* TCP MD5 mode */
    162 
    163 /*
    164  * Direction of security policy.
    165  * NOTE: Since INVALID is used just as flag.
    166  * The other are used for loop counter too.
    167  */
    168 #define IPSEC_DIR_ANY		0
    169 #define IPSEC_DIR_INBOUND	1
    170 #define IPSEC_DIR_OUTBOUND	2
    171 #define IPSEC_DIR_MAX		3
    172 #define IPSEC_DIR_INVALID	4
    173 
    174 /* Policy level */
    175 /*
    176  * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
    177  * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
    178  * DISCARD and NONE are allowed for system default.
    179  */
    180 #define IPSEC_POLICY_DISCARD	0	/* discarding packet */
    181 #define IPSEC_POLICY_NONE	1	/* through IPsec engine */
    182 #define IPSEC_POLICY_IPSEC	2	/* do IPsec */
    183 #define IPSEC_POLICY_ENTRUST	3	/* consulting SPD if present. */
    184 #define IPSEC_POLICY_BYPASS	4	/* only for privileged socket. */
    185 
    186 /* Security protocol level */
    187 #define	IPSEC_LEVEL_DEFAULT	0	/* reference to system default */
    188 #define	IPSEC_LEVEL_USE		1	/* use SA if present. */
    189 #define	IPSEC_LEVEL_REQUIRE	2	/* require SA. */
    190 #define	IPSEC_LEVEL_UNIQUE	3	/* unique SA. */
    191 
    192 #define IPSEC_MANUAL_REQID_MAX	0x3fff
    193 				/*
    194 				 * if security policy level == unique, this id
    195 				 * indicate to a relative SA for use, else is
    196 				 * zero.
    197 				 * 1 - 0x3fff are reserved for manual keying.
    198 				 * 0 are reserved for above reason.  Others is
    199 				 * for kernel use.
    200 				 * Note that this id doesn't identify SA
    201 				 * by only itself.
    202 				 */
    203 #define IPSEC_REPLAYWSIZE  32
    204 
    205 #ifdef _KERNEL
    206 struct ipsec_output_state {
    207 	struct mbuf *m;
    208 	struct route *ro;
    209 	struct sockaddr *dst;
    210 };
    211 
    212 struct ipsec_history {
    213 	int ih_proto;
    214 	u_int32_t ih_spi;
    215 };
    216 
    217 extern int ipsec_debug;
    218 #ifdef IPSEC_DEBUG
    219 extern int ipsec_replay;
    220 extern int ipsec_integrity;
    221 #endif
    222 
    223 extern struct secpolicy ip4_def_policy;
    224 extern int ip4_esp_trans_deflev;
    225 extern int ip4_esp_net_deflev;
    226 extern int ip4_ah_trans_deflev;
    227 extern int ip4_ah_net_deflev;
    228 extern int ip4_ah_cleartos;
    229 extern int ip4_ah_offsetmask;
    230 extern int ip4_ipsec_dfbit;
    231 extern int ip4_ipsec_ecn;
    232 extern int ip4_esp_randpad;
    233 extern int crypto_support;
    234 
    235 #define ipseclog(x)	do { if (ipsec_debug) log x; } while (0)
    236 /* for openbsd compatibility */
    237 #define	DPRINTF(x)	do { if (ipsec_debug) printf x; } while (0)
    238 
    239 void ipsec_pcbconn (struct inpcbpolicy *);
    240 void ipsec_pcbdisconn (struct inpcbpolicy *);
    241 void ipsec_invalpcbcacheall (void);
    242 
    243 extern u_int ipsec_spdgen;
    244 
    245 struct tdb_ident;
    246 struct secpolicy *ipsec_getpolicy (const struct tdb_ident*, u_int);
    247 struct inpcb;
    248 struct secpolicy *ipsec4_checkpolicy (struct mbuf *, u_int, u_int,
    249 	int *, struct inpcb *);
    250 struct secpolicy * ipsec_getpolicybyaddr(struct mbuf *, u_int,
    251 	int, int *);
    252 int ipsec4_output(struct mbuf *, struct inpcb *, int,
    253 	struct secpolicy **, u_long *, bool *, bool *);
    254 int ipsec4_input(struct mbuf *, int);
    255 int ipsec4_forward(struct mbuf *, int *);
    256 #ifdef INET6
    257 int ipsec6_input(struct mbuf *);
    258 #endif
    259 
    260 static __inline struct secpolicy*
    261 ipsec4_getpolicybysock(
    262     struct mbuf *m,
    263     u_int dir,
    264     const struct socket *so,
    265     int *err
    266 )
    267 {
    268   panic("ipsec4_getpolicybysock");
    269 }
    270 
    271 static __inline int
    272 ipsec_copy_pcbpolicy(
    273     struct inpcbpolicy *oldp,
    274     struct inpcbpolicy *newp
    275 )
    276 {
    277   /*XXX do nothing */
    278   return (0);
    279 }
    280 
    281 struct inpcb;
    282 #define	ipsec_init_pcbpolicy ipsec_init_policy
    283 int ipsec_init_policy (struct socket *so, struct inpcbpolicy **);
    284 int ipsec_copy_policy
    285 	(const struct inpcbpolicy *, struct inpcbpolicy *);
    286 u_int ipsec_get_reqlevel (const struct ipsecrequest *);
    287 int ipsec_in_reject (const struct secpolicy *, const struct mbuf *);
    288 
    289 int ipsec4_set_policy (struct inpcb *, int, const void *, size_t, kauth_cred_t);
    290 int ipsec4_get_policy (struct inpcb *, const void *, size_t, struct mbuf **);
    291 int ipsec4_delete_pcbpolicy (struct inpcb *);
    292 int ipsec4_in_reject (struct mbuf *, struct inpcb *);
    293 /*
    294  * KAME ipsec4_in_reject_so(struct mbuf*, struct so)  compatibility shim
    295  */
    296 #define ipsec4_in_reject_so(m, _so) \
    297   ipsec4_in_reject(m, ((_so) == NULL? NULL : sotoinpcb(_so)))
    298 
    299 
    300 struct secas;
    301 struct tcpcb;
    302 int ipsec_chkreplay (u_int32_t, const struct secasvar *);
    303 int ipsec_updatereplay (u_int32_t, const struct secasvar *);
    304 
    305 size_t ipsec4_hdrsiz (struct mbuf *, u_int, struct inpcb *);
    306 size_t ipsec4_hdrsiz_tcp (struct tcpcb *);
    307 #define ipsec4_getpolicybyaddr ipsec_getpolicybyaddr
    308 
    309 union sockaddr_union;
    310 const char *ipsec_address(const union sockaddr_union* sa);
    311 const char *ipsec_logsastr (const struct secasvar *);
    312 
    313 void ipsec_dumpmbuf (struct mbuf *);
    314 
    315 /* NetBSD protosw ctlin entrypoint */
    316 void *esp4_ctlinput(int, const struct sockaddr *, void *);
    317 void *ah4_ctlinput(int, const struct sockaddr *, void *);
    318 
    319 struct m_tag;
    320 void ipsec4_common_input(struct mbuf *m, ...);
    321 int ipsec4_common_input_cb(struct mbuf *, struct secasvar *,
    322 			int, int, struct m_tag *);
    323 int ipsec4_process_packet (struct mbuf *, struct ipsecrequest *,
    324 			int, int);
    325 int ipsec_process_done (struct mbuf *, struct ipsecrequest *);
    326 #define ipsec_indone(m)	\
    327 	(m_tag_find((m), PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
    328 
    329 #define ipsec_outdone(m) \
    330 	(m_tag_find((m), PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
    331 
    332 struct mbuf *ipsec_copypkt (struct mbuf *);
    333 
    334 void m_checkalignment(const char* , struct mbuf *, int, int);
    335 struct mbuf *m_clone(struct mbuf *);
    336 struct mbuf *m_makespace(struct mbuf *, int, int, int *);
    337 void *m_pad(struct mbuf *, int );
    338 int m_striphdr(struct mbuf *, int, int);
    339 
    340 /* Per-socket caching of IPsec output policy */
    341 static __inline
    342 int ipsec_clear_socket_cache(struct mbuf *m)
    343 {
    344   return 0;
    345 }
    346 
    347 void nat_t_ports_get(struct mbuf *, u_int16_t *, u_int16_t *);
    348 
    349 extern int ipsec_used __read_mostly, ipsec_enabled __read_mostly;
    350 
    351 #endif /* _KERNEL */
    352 
    353 #ifndef _KERNEL
    354 char *ipsec_set_policy (const char *, int);
    355 int ipsec_get_policylen (char *);
    356 char *ipsec_dump_policy (char *, const char *);
    357 
    358 const char *ipsec_strerror (void);
    359 #endif /* !_KERNEL */
    360 
    361 #ifdef _KERNEL
    362 /* External declarations of per-file init functions */
    363 void ah_attach(void);
    364 void esp_attach(void);
    365 void ipcomp_attach(void);
    366 void ipe4_attach(void);
    367 void ipe4_attach(void);
    368 void tcpsignature_attach(void);
    369 
    370 void ipsec_attach(void);
    371 
    372 void sysctl_net_inet_ipsec_setup(struct sysctllog **);
    373 #ifdef INET6
    374 void sysctl_net_inet6_ipsec6_setup(struct sysctllog **);
    375 #endif
    376 
    377 #endif /* _KERNEL */
    378 #endif /* !_NETIPSEC_IPSEC_H_ */
    379