Home | History | Annotate | Line # | Download | only in netipsec
ipsec.h revision 1.92
      1 /*	$NetBSD: ipsec.h,v 1.92 2022/10/28 05:18:39 ozaki-r Exp $	*/
      2 /*	$FreeBSD: 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 #ifndef _NETIPSEC_IPSEC_H_
     35 #define _NETIPSEC_IPSEC_H_
     36 
     37 #if defined(_KERNEL_OPT)
     38 #include "opt_inet.h"
     39 #include "opt_ipsec.h"
     40 #endif
     41 
     42 #include <net/pfkeyv2.h>
     43 
     44 #ifdef _KERNEL
     45 #include <sys/socketvar.h>
     46 #include <sys/localcount.h>
     47 
     48 #include <netinet/in_pcb_hdr.h>
     49 #include <netinet/in_pcb.h>
     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 };
     66 
     67 /* Security Policy Data Base */
     68 struct secpolicy {
     69 	struct pslist_entry pslist_entry;
     70 
     71 	struct localcount localcount;	/* reference count */
     72 	struct secpolicyindex spidx;	/* selector */
     73 	u_int32_t id;			/* It's unique number on the system. */
     74 	u_int state;			/* 0: dead, others: alive */
     75 #define IPSEC_SPSTATE_DEAD	0
     76 #define IPSEC_SPSTATE_ALIVE	1
     77 
     78 	u_int origin;			/* who generate this SP. */
     79 #define IPSEC_SPORIGIN_USER	0
     80 #define IPSEC_SPORIGIN_KERNEL	1
     81 
     82 	u_int policy;		/* DISCARD, NONE or IPSEC, see keyv2.h */
     83 	struct ipsecrequest *req;
     84 				/* pointer to the ipsec request tree, */
     85 				/* if policy == IPSEC else this value == NULL.*/
     86 
     87 	/*
     88 	 * lifetime handler.
     89 	 * the policy can be used without limitiation if both lifetime and
     90 	 * validtime are zero.
     91 	 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
     92 	 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
     93 	 */
     94 	time_t created;		/* time created the policy */
     95 	time_t lastused;	/* updated every when kernel sends a packet */
     96 	time_t lifetime;	/* duration of the lifetime of this policy */
     97 	time_t validtime;	/* duration this policy is valid without use */
     98 };
     99 
    100 /* Request for IPsec */
    101 struct ipsecrequest {
    102 	struct ipsecrequest *next;
    103 				/* pointer to next structure */
    104 				/* If NULL, it means the end of chain. */
    105 	struct secasindex saidx;/* hint for search proper SA */
    106 				/* if __ss_len == 0 then no address specified.*/
    107 	u_int level;		/* IPsec level defined below. */
    108 
    109 	struct secpolicy *sp;	/* back pointer to SP */
    110 };
    111 
    112 /* security policy in PCB */
    113 struct inpcbpolicy {
    114 	struct secpolicy *sp_in;
    115 	struct secpolicy *sp_out;
    116 	int priv;			/* privileged socket ? */
    117 
    118 	/* cached policy */
    119 	struct {
    120 		struct secpolicy *cachesp;
    121 		struct secpolicyindex cacheidx;
    122 		int cachehint;		/* processing requirement hint: */
    123 #define	IPSEC_PCBHINT_UNKNOWN	0	/* Unknown */
    124 #define	IPSEC_PCBHINT_YES	1	/* IPsec processing is required */
    125 #define	IPSEC_PCBHINT_NO	2	/* IPsec processing not required */
    126 		u_int cachegen;		/* spdgen when cache filled */
    127 	} sp_cache[3];			/* XXX 3 == IPSEC_DIR_MAX */
    128 	int sp_cacheflags;
    129 #define	IPSEC_PCBSP_CONNECTED	1
    130 	struct inpcb *sp_inp;		/* back pointer */
    131 };
    132 
    133 extern u_int ipsec_spdgen;
    134 
    135 static __inline bool
    136 ipsec_pcb_skip_ipsec(struct inpcbpolicy *pcbsp, int dir)
    137 {
    138 
    139 	KASSERT(inp_locked(pcbsp->sp_inp));
    140 
    141 	return pcbsp->sp_cache[(dir)].cachehint == IPSEC_PCBHINT_NO &&
    142 	    pcbsp->sp_cache[(dir)].cachegen == ipsec_spdgen;
    143 }
    144 
    145 /* SP acquiring list table. */
    146 struct secspacq {
    147 	LIST_ENTRY(secspacq) chain;
    148 
    149 	struct secpolicyindex spidx;
    150 
    151 	time_t created;		/* for lifetime */
    152 	int count;		/* for lifetime */
    153 	/* XXX: here is mbuf place holder to be sent ? */
    154 };
    155 #endif /* _KERNEL */
    156 
    157 /* buffer size for formatted output of ipsec address (addr + '%' + scope_id?) */
    158 #define	IPSEC_ADDRSTRLEN	(INET6_ADDRSTRLEN + 11)
    159 /* buffer size for ipsec_logsastr() */
    160 #define	IPSEC_LOGSASTRLEN	192
    161 
    162 /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
    163 #define IPSEC_PORT_ANY		0
    164 #define IPSEC_ULPROTO_ANY	255
    165 #define IPSEC_PROTO_ANY		255
    166 
    167 /* mode of security protocol */
    168 /* NOTE: DON'T use IPSEC_MODE_ANY at SPD.  It's only use in SAD */
    169 #define	IPSEC_MODE_ANY		0	/* i.e. wildcard. */
    170 #define	IPSEC_MODE_TRANSPORT	1
    171 #define	IPSEC_MODE_TUNNEL	2
    172 #define	IPSEC_MODE_TCPMD5	3	/* TCP MD5 mode */
    173 
    174 /*
    175  * Direction of security policy.
    176  * NOTE: Since INVALID is used just as flag.
    177  * The other are used for loop counter too.
    178  */
    179 #define IPSEC_DIR_ANY		0
    180 #define IPSEC_DIR_INBOUND	1
    181 #define IPSEC_DIR_OUTBOUND	2
    182 #define IPSEC_DIR_MAX		3
    183 #define IPSEC_DIR_INVALID	4
    184 
    185 #define IPSEC_DIR_IS_VALID(dir)		((dir) >= 0 && (dir) <= IPSEC_DIR_MAX)
    186 #define IPSEC_DIR_IS_INOROUT(dir)	((dir) == IPSEC_DIR_INBOUND || \
    187 					 (dir) == IPSEC_DIR_OUTBOUND)
    188 
    189 /* Policy level */
    190 /*
    191  * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
    192  * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
    193  * DISCARD and NONE are allowed for system default.
    194  */
    195 #define IPSEC_POLICY_DISCARD	0	/* discarding packet */
    196 #define IPSEC_POLICY_NONE	1	/* through IPsec engine */
    197 #define IPSEC_POLICY_IPSEC	2	/* do IPsec */
    198 #define IPSEC_POLICY_ENTRUST	3	/* consulting SPD if present. */
    199 #define IPSEC_POLICY_BYPASS	4	/* only for privileged socket. */
    200 
    201 /* Security protocol level */
    202 #define	IPSEC_LEVEL_DEFAULT	0	/* reference to system default */
    203 #define	IPSEC_LEVEL_USE		1	/* use SA if present. */
    204 #define	IPSEC_LEVEL_REQUIRE	2	/* require SA. */
    205 #define	IPSEC_LEVEL_UNIQUE	3	/* unique SA. */
    206 
    207 #define IPSEC_MANUAL_REQID_MAX	0x3fff
    208 				/*
    209 				 * if security policy level == unique, this id
    210 				 * indicate to a relative SA for use, else is
    211 				 * zero.
    212 				 * 1 - 0x3fff are reserved for manual keying.
    213 				 * 0 are reserved for above reason.  Others is
    214 				 * for kernel use.
    215 				 * Note that this id doesn't identify SA
    216 				 * by only itself.
    217 				 */
    218 #define IPSEC_REPLAYWSIZE  32
    219 
    220 #ifdef _KERNEL
    221 
    222 extern int ipsec_debug;
    223 #ifdef IPSEC_DEBUG
    224 extern int ipsec_replay;
    225 extern int ipsec_integrity;
    226 #endif
    227 
    228 extern struct secpolicy ip4_def_policy;
    229 extern int ip4_esp_trans_deflev;
    230 extern int ip4_esp_net_deflev;
    231 extern int ip4_ah_trans_deflev;
    232 extern int ip4_ah_net_deflev;
    233 extern int ip4_ah_cleartos;
    234 extern int ip4_ah_offsetmask;
    235 extern int ip4_ipsec_dfbit;
    236 extern int ip4_ipsec_ecn;
    237 extern int crypto_support;
    238 
    239 #include <sys/syslog.h>
    240 
    241 #define	DPRINTF(fmt, args...) 						\
    242 	do {								\
    243 		if (ipsec_debug)					\
    244 			log(LOG_DEBUG, "%s: " fmt, __func__, ##args);	\
    245 	} while (/*CONSTCOND*/0)
    246 
    247 #define IPSECLOG(level, fmt, args...) 					\
    248 	do {								\
    249 		if (ipsec_debug)					\
    250 			log(level, "%s: " fmt, __func__, ##args);	\
    251 	} while (/*CONSTCOND*/0)
    252 
    253 #define ipsec_indone(m)	\
    254 	((m->m_flags & M_AUTHIPHDR) || (m->m_flags & M_DECRYPTED))
    255 #define ipsec_outdone(m) \
    256 	(m_tag_find((m), PACKET_TAG_IPSEC_OUT_DONE) != NULL)
    257 
    258 static __inline bool
    259 ipsec_skip_pfil(struct mbuf *m)
    260 {
    261 	bool rv;
    262 
    263 	if (ipsec_indone(m) &&
    264 	    ((m->m_pkthdr.pkthdr_flags & PKTHDR_FLAG_IPSEC_SKIP_PFIL) != 0)) {
    265 		m->m_pkthdr.pkthdr_flags &= ~PKTHDR_FLAG_IPSEC_SKIP_PFIL;
    266 		rv = true;
    267 	} else {
    268 		rv = false;
    269 	}
    270 
    271 	return rv;
    272 }
    273 
    274 void ipsec_pcbconn(struct inpcbpolicy *);
    275 void ipsec_pcbdisconn(struct inpcbpolicy *);
    276 void ipsec_invalpcbcacheall(void);
    277 
    278 struct inpcb;
    279 int ipsec4_output(struct mbuf *, struct inpcb *, int, u_long *, bool *, bool *, bool *);
    280 
    281 int ipsec_ip_input_checkpolicy(struct mbuf *, bool);
    282 void ipsec_mtu(struct mbuf *, int *);
    283 #ifdef INET6
    284 void ipsec6_udp_cksum(struct mbuf *);
    285 #endif
    286 
    287 struct inpcb;
    288 int ipsec_init_pcbpolicy(struct socket *so, struct inpcbpolicy **);
    289 int ipsec_copy_policy(const struct inpcbpolicy *, struct inpcbpolicy *);
    290 u_int ipsec_get_reqlevel(const struct ipsecrequest *);
    291 
    292 int ipsec_set_policy(struct inpcb *, const void *, size_t, kauth_cred_t);
    293 int ipsec_get_policy(struct inpcb *, const void *, size_t, struct mbuf **);
    294 int ipsec_delete_pcbpolicy(struct inpcb *);
    295 int ipsec_in_reject(struct mbuf *, struct inpcb *);
    296 
    297 struct secasvar *ipsec_lookup_sa(const struct ipsecrequest *,
    298     const struct mbuf *);
    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 ipsec_hdrsiz(struct mbuf *, u_int, struct inpcb *);
    306 size_t ipsec4_hdrsiz_tcp(struct tcpcb *);
    307 
    308 union sockaddr_union;
    309 const char *ipsec_address(const union sockaddr_union* sa, char *, size_t);
    310 const char *ipsec_logsastr(const struct secasvar *, char *, size_t);
    311 
    312 /* NetBSD protosw ctlin entrypoint */
    313 void *esp4_ctlinput(int, const struct sockaddr *, void *);
    314 void *ah4_ctlinput(int, const struct sockaddr *, void *);
    315 
    316 void ipsec_output_init(void);
    317 struct m_tag;
    318 void ipsec4_common_input(struct mbuf *m, int, int);
    319 int ipsec4_common_input_cb(struct mbuf *, struct secasvar *, int, int);
    320 int ipsec4_process_packet(struct mbuf *, const struct ipsecrequest *, u_long *);
    321 int ipsec_process_done(struct mbuf *, const struct ipsecrequest *,
    322     struct secasvar *, int);
    323 
    324 struct mbuf *m_clone(struct mbuf *);
    325 struct mbuf *m_makespace(struct mbuf *, int, int, int *);
    326 void *m_pad(struct mbuf *, int);
    327 int m_striphdr(struct mbuf *, int, int);
    328 
    329 extern int ipsec_used __read_mostly;
    330 extern int ipsec_enabled __read_mostly;
    331 
    332 #endif /* _KERNEL */
    333 
    334 #ifndef _KERNEL
    335 char *ipsec_set_policy(const char *, int);
    336 int ipsec_get_policylen(char *);
    337 char *ipsec_dump_policy(char *, const char *);
    338 const char *ipsec_strerror(void);
    339 #endif /* !_KERNEL */
    340 
    341 #ifdef _KERNEL
    342 /* External declarations of per-file init functions */
    343 void ah_attach(void);
    344 void esp_attach(void);
    345 void ipcomp_attach(void);
    346 void ipe4_attach(void);
    347 void tcpsignature_attach(void);
    348 
    349 void ipsec_attach(void);
    350 
    351 void sysctl_net_inet_ipsec_setup(struct sysctllog **);
    352 #ifdef INET6
    353 void sysctl_net_inet6_ipsec6_setup(struct sysctllog **);
    354 #endif
    355 
    356 #endif /* _KERNEL */
    357 #endif /* !_NETIPSEC_IPSEC_H_ */
    358