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