Home | History | Annotate | Line # | Download | only in netipsec
ipsec.h revision 1.79
      1  1.79      maxv /*	$NetBSD: ipsec.h,v 1.79 2018/04/29 11:51:08 maxv Exp $	*/
      2  1.73      maxv /*	$FreeBSD: ipsec.h,v 1.2.4.2 2004/02/14 22:23:23 bms Exp $	*/
      3   1.1  jonathan /*	$KAME: ipsec.h,v 1.53 2001/11/20 08:32:38 itojun Exp $	*/
      4   1.1  jonathan 
      5   1.1  jonathan /*
      6   1.1  jonathan  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      7   1.1  jonathan  * All rights reserved.
      8   1.1  jonathan  *
      9   1.1  jonathan  * Redistribution and use in source and binary forms, with or without
     10   1.1  jonathan  * modification, are permitted provided that the following conditions
     11   1.1  jonathan  * are met:
     12   1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     13   1.1  jonathan  *    notice, this list of conditions and the following disclaimer.
     14   1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1  jonathan  *    notice, this list of conditions and the following disclaimer in the
     16   1.1  jonathan  *    documentation and/or other materials provided with the distribution.
     17   1.1  jonathan  * 3. Neither the name of the project nor the names of its contributors
     18   1.1  jonathan  *    may be used to endorse or promote products derived from this software
     19   1.1  jonathan  *    without specific prior written permission.
     20   1.1  jonathan  *
     21   1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     22   1.1  jonathan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23   1.1  jonathan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24   1.1  jonathan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     25   1.1  jonathan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26   1.1  jonathan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27   1.1  jonathan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28   1.1  jonathan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29   1.1  jonathan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30   1.1  jonathan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31   1.1  jonathan  * SUCH DAMAGE.
     32   1.1  jonathan  */
     33   1.1  jonathan 
     34   1.1  jonathan #ifndef _NETIPSEC_IPSEC_H_
     35   1.1  jonathan #define _NETIPSEC_IPSEC_H_
     36   1.1  jonathan 
     37  1.23        ad #if defined(_KERNEL_OPT)
     38   1.1  jonathan #include "opt_inet.h"
     39   1.1  jonathan #include "opt_ipsec.h"
     40   1.1  jonathan #endif
     41   1.1  jonathan 
     42   1.1  jonathan #include <net/pfkeyv2.h>
     43   1.1  jonathan 
     44   1.1  jonathan #ifdef _KERNEL
     45  1.50     ozaki #include <sys/socketvar.h>
     46  1.58     ozaki #include <sys/localcount.h>
     47   1.1  jonathan 
     48  1.50     ozaki #include <netinet/in_pcb_hdr.h>
     49  1.31  drochner #include <netipsec/keydb.h>
     50  1.30  drochner 
     51   1.1  jonathan /*
     52   1.1  jonathan  * Security Policy Index
     53   1.1  jonathan  * Ensure that both address families in the "src" and "dst" are same.
     54   1.1  jonathan  * When the value of the ul_proto is ICMPv6, the port field in "src"
     55   1.1  jonathan  * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
     56   1.1  jonathan  */
     57   1.1  jonathan struct secpolicyindex {
     58   1.1  jonathan 	u_int8_t dir;			/* direction of packet flow, see blow */
     59   1.1  jonathan 	union sockaddr_union src;	/* IP src address for SP */
     60   1.1  jonathan 	union sockaddr_union dst;	/* IP dst address for SP */
     61   1.1  jonathan 	u_int8_t prefs;			/* prefix length in bits for src */
     62   1.1  jonathan 	u_int8_t prefd;			/* prefix length in bits for dst */
     63   1.1  jonathan 	u_int16_t ul_proto;		/* upper layer Protocol */
     64   1.1  jonathan #ifdef notyet
     65   1.1  jonathan 	uid_t uids;
     66   1.1  jonathan 	uid_t uidd;
     67   1.1  jonathan 	gid_t gids;
     68   1.1  jonathan 	gid_t gidd;
     69   1.1  jonathan #endif
     70   1.1  jonathan };
     71   1.1  jonathan 
     72   1.1  jonathan /* Security Policy Data Base */
     73   1.1  jonathan struct secpolicy {
     74  1.57     ozaki 	struct pslist_entry pslist_entry;
     75   1.1  jonathan 
     76  1.58     ozaki 	struct localcount localcount;	/* reference count */
     77   1.1  jonathan 	struct secpolicyindex spidx;	/* selector */
     78   1.1  jonathan 	u_int32_t id;			/* It's unique number on the system. */
     79   1.1  jonathan 	u_int state;			/* 0: dead, others: alive */
     80   1.1  jonathan #define IPSEC_SPSTATE_DEAD	0
     81   1.1  jonathan #define IPSEC_SPSTATE_ALIVE	1
     82   1.1  jonathan 
     83  1.62  knakahar 	u_int origin;			/* who generate this SP. */
     84  1.62  knakahar #define IPSEC_SPORIGIN_USER	0
     85  1.62  knakahar #define IPSEC_SPORIGIN_KERNEL	1
     86  1.62  knakahar 
     87   1.1  jonathan 	u_int policy;		/* DISCARD, NONE or IPSEC, see keyv2.h */
     88   1.1  jonathan 	struct ipsecrequest *req;
     89   1.1  jonathan 				/* pointer to the ipsec request tree, */
     90   1.1  jonathan 				/* if policy == IPSEC else this value == NULL.*/
     91   1.1  jonathan 
     92   1.1  jonathan 	/*
     93   1.1  jonathan 	 * lifetime handler.
     94   1.1  jonathan 	 * the policy can be used without limitiation if both lifetime and
     95   1.1  jonathan 	 * validtime are zero.
     96   1.1  jonathan 	 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
     97   1.1  jonathan 	 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
     98   1.1  jonathan 	 */
     99  1.26  drochner 	time_t created;		/* time created the policy */
    100  1.26  drochner 	time_t lastused;	/* updated every when kernel sends a packet */
    101  1.26  drochner 	time_t lifetime;	/* duration of the lifetime of this policy */
    102  1.26  drochner 	time_t validtime;	/* duration this policy is valid without use */
    103   1.1  jonathan };
    104   1.1  jonathan 
    105   1.1  jonathan /* Request for IPsec */
    106   1.1  jonathan struct ipsecrequest {
    107   1.1  jonathan 	struct ipsecrequest *next;
    108   1.1  jonathan 				/* pointer to next structure */
    109   1.1  jonathan 				/* If NULL, it means the end of chain. */
    110   1.1  jonathan 	struct secasindex saidx;/* hint for search proper SA */
    111   1.1  jonathan 				/* if __ss_len == 0 then no address specified.*/
    112   1.1  jonathan 	u_int level;		/* IPsec level defined below. */
    113   1.1  jonathan 
    114   1.1  jonathan 	struct secpolicy *sp;	/* back pointer to SP */
    115   1.1  jonathan };
    116   1.1  jonathan 
    117   1.1  jonathan /* security policy in PCB */
    118   1.1  jonathan struct inpcbpolicy {
    119   1.1  jonathan 	struct secpolicy *sp_in;
    120   1.1  jonathan 	struct secpolicy *sp_out;
    121   1.1  jonathan 	int priv;			/* privileged socket ? */
    122   1.7   thorpej 
    123   1.7   thorpej 	/* cached policy */
    124   1.7   thorpej 	struct {
    125   1.7   thorpej 		struct secpolicy *cachesp;
    126   1.7   thorpej 		struct secpolicyindex cacheidx;
    127   1.7   thorpej 		int cachehint;		/* processing requirement hint: */
    128  1.49     ozaki #define	IPSEC_PCBHINT_UNKNOWN	0	/* Unknown */
    129   1.7   thorpej #define	IPSEC_PCBHINT_YES	1	/* IPsec processing is required */
    130   1.7   thorpej #define	IPSEC_PCBHINT_NO	2	/* IPsec processing not required */
    131   1.7   thorpej 		u_int cachegen;		/* spdgen when cache filled */
    132   1.7   thorpej 	} sp_cache[3];			/* XXX 3 == IPSEC_DIR_MAX */
    133   1.7   thorpej 	int sp_cacheflags;
    134   1.7   thorpej #define	IPSEC_PCBSP_CONNECTED	1
    135  1.44     ozaki 	struct inpcb_hdr *sp_inph;	/* back pointer */
    136   1.1  jonathan };
    137   1.1  jonathan 
    138  1.50     ozaki extern u_int ipsec_spdgen;
    139  1.50     ozaki 
    140  1.74  christos static __inline bool
    141  1.50     ozaki ipsec_pcb_skip_ipsec(struct inpcbpolicy *pcbsp, int dir)
    142  1.50     ozaki {
    143  1.50     ozaki 
    144  1.50     ozaki 	KASSERT(inph_locked(pcbsp->sp_inph));
    145  1.50     ozaki 
    146  1.50     ozaki 	return pcbsp->sp_cache[(dir)].cachehint == IPSEC_PCBHINT_NO &&
    147  1.50     ozaki 	    pcbsp->sp_cache[(dir)].cachegen == ipsec_spdgen;
    148  1.50     ozaki }
    149   1.7   thorpej 
    150   1.1  jonathan /* SP acquiring list table. */
    151   1.1  jonathan struct secspacq {
    152   1.1  jonathan 	LIST_ENTRY(secspacq) chain;
    153   1.1  jonathan 
    154   1.1  jonathan 	struct secpolicyindex spidx;
    155   1.1  jonathan 
    156  1.26  drochner 	time_t created;		/* for lifetime */
    157   1.1  jonathan 	int count;		/* for lifetime */
    158   1.1  jonathan 	/* XXX: here is mbuf place holder to be sent ? */
    159   1.1  jonathan };
    160   1.1  jonathan #endif /* _KERNEL */
    161   1.1  jonathan 
    162  1.47       ryo /* buffer size for formatted output of ipsec address (addr + '%' + scope_id?) */
    163  1.47       ryo #define	IPSEC_ADDRSTRLEN	(INET6_ADDRSTRLEN + 11)
    164  1.47       ryo /* buffer size for ipsec_logsastr() */
    165  1.47       ryo #define	IPSEC_LOGSASTRLEN	192
    166  1.47       ryo 
    167   1.1  jonathan /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
    168   1.1  jonathan #define IPSEC_PORT_ANY		0
    169   1.1  jonathan #define IPSEC_ULPROTO_ANY	255
    170   1.1  jonathan #define IPSEC_PROTO_ANY		255
    171   1.1  jonathan 
    172   1.1  jonathan /* mode of security protocol */
    173   1.1  jonathan /* NOTE: DON'T use IPSEC_MODE_ANY at SPD.  It's only use in SAD */
    174   1.1  jonathan #define	IPSEC_MODE_ANY		0	/* i.e. wildcard. */
    175   1.1  jonathan #define	IPSEC_MODE_TRANSPORT	1
    176   1.1  jonathan #define	IPSEC_MODE_TUNNEL	2
    177   1.8  jonathan #define	IPSEC_MODE_TCPMD5	3	/* TCP MD5 mode */
    178   1.1  jonathan 
    179   1.1  jonathan /*
    180   1.1  jonathan  * Direction of security policy.
    181   1.1  jonathan  * NOTE: Since INVALID is used just as flag.
    182   1.1  jonathan  * The other are used for loop counter too.
    183   1.1  jonathan  */
    184   1.1  jonathan #define IPSEC_DIR_ANY		0
    185   1.1  jonathan #define IPSEC_DIR_INBOUND	1
    186   1.1  jonathan #define IPSEC_DIR_OUTBOUND	2
    187   1.1  jonathan #define IPSEC_DIR_MAX		3
    188   1.1  jonathan #define IPSEC_DIR_INVALID	4
    189   1.1  jonathan 
    190  1.42     ozaki #define IPSEC_DIR_IS_VALID(dir)		((dir) >= 0 && (dir) <= IPSEC_DIR_MAX)
    191  1.42     ozaki #define IPSEC_DIR_IS_INOROUT(dir)	((dir) == IPSEC_DIR_INBOUND || \
    192  1.42     ozaki 					 (dir) == IPSEC_DIR_OUTBOUND)
    193  1.42     ozaki 
    194   1.1  jonathan /* Policy level */
    195   1.1  jonathan /*
    196   1.1  jonathan  * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
    197   1.1  jonathan  * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
    198   1.1  jonathan  * DISCARD and NONE are allowed for system default.
    199   1.1  jonathan  */
    200   1.1  jonathan #define IPSEC_POLICY_DISCARD	0	/* discarding packet */
    201   1.1  jonathan #define IPSEC_POLICY_NONE	1	/* through IPsec engine */
    202   1.1  jonathan #define IPSEC_POLICY_IPSEC	2	/* do IPsec */
    203   1.1  jonathan #define IPSEC_POLICY_ENTRUST	3	/* consulting SPD if present. */
    204   1.1  jonathan #define IPSEC_POLICY_BYPASS	4	/* only for privileged socket. */
    205   1.1  jonathan 
    206   1.1  jonathan /* Security protocol level */
    207   1.1  jonathan #define	IPSEC_LEVEL_DEFAULT	0	/* reference to system default */
    208   1.1  jonathan #define	IPSEC_LEVEL_USE		1	/* use SA if present. */
    209   1.1  jonathan #define	IPSEC_LEVEL_REQUIRE	2	/* require SA. */
    210   1.1  jonathan #define	IPSEC_LEVEL_UNIQUE	3	/* unique SA. */
    211   1.1  jonathan 
    212   1.1  jonathan #define IPSEC_MANUAL_REQID_MAX	0x3fff
    213   1.1  jonathan 				/*
    214   1.1  jonathan 				 * if security policy level == unique, this id
    215   1.1  jonathan 				 * indicate to a relative SA for use, else is
    216   1.1  jonathan 				 * zero.
    217   1.1  jonathan 				 * 1 - 0x3fff are reserved for manual keying.
    218   1.1  jonathan 				 * 0 are reserved for above reason.  Others is
    219   1.1  jonathan 				 * for kernel use.
    220   1.1  jonathan 				 * Note that this id doesn't identify SA
    221   1.1  jonathan 				 * by only itself.
    222   1.1  jonathan 				 */
    223   1.1  jonathan #define IPSEC_REPLAYWSIZE  32
    224   1.1  jonathan 
    225   1.1  jonathan #ifdef _KERNEL
    226   1.1  jonathan 
    227   1.1  jonathan extern int ipsec_debug;
    228  1.15    rpaulo #ifdef IPSEC_DEBUG
    229  1.15    rpaulo extern int ipsec_replay;
    230  1.15    rpaulo extern int ipsec_integrity;
    231  1.15    rpaulo #endif
    232   1.1  jonathan 
    233   1.1  jonathan extern struct secpolicy ip4_def_policy;
    234   1.1  jonathan extern int ip4_esp_trans_deflev;
    235   1.1  jonathan extern int ip4_esp_net_deflev;
    236   1.1  jonathan extern int ip4_ah_trans_deflev;
    237   1.1  jonathan extern int ip4_ah_net_deflev;
    238   1.1  jonathan extern int ip4_ah_cleartos;
    239   1.1  jonathan extern int ip4_ah_offsetmask;
    240   1.1  jonathan extern int ip4_ipsec_dfbit;
    241   1.1  jonathan extern int ip4_ipsec_ecn;
    242   1.1  jonathan extern int crypto_support;
    243   1.1  jonathan 
    244  1.48     ozaki #include <sys/syslog.h>
    245  1.75      maxv 
    246   1.1  jonathan #define	DPRINTF(x)	do { if (ipsec_debug) printf x; } while (0)
    247   1.1  jonathan 
    248  1.48     ozaki #define IPSECLOG(level, fmt, args...) 					\
    249  1.48     ozaki 	do {								\
    250  1.48     ozaki 		if (ipsec_debug)					\
    251  1.48     ozaki 			log(level, "%s: " fmt, __func__, ##args);	\
    252  1.48     ozaki 	} while (0)
    253  1.48     ozaki 
    254  1.75      maxv #define ipsec_indone(m)	\
    255  1.75      maxv 	((m->m_flags & M_AUTHIPHDR) || (m->m_flags & M_DECRYPTED))
    256  1.75      maxv #define ipsec_outdone(m) \
    257  1.75      maxv 	(m_tag_find((m), PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
    258  1.75      maxv 
    259  1.64      maxv void ipsec_pcbconn(struct inpcbpolicy *);
    260  1.64      maxv void ipsec_pcbdisconn(struct inpcbpolicy *);
    261  1.64      maxv void ipsec_invalpcbcacheall(void);
    262   1.7   thorpej 
    263   1.1  jonathan struct inpcb;
    264  1.64      maxv int ipsec4_output(struct mbuf *, struct inpcb *, int, u_long *, bool *, bool *);
    265  1.34     rmind int ipsec4_input(struct mbuf *, int);
    266  1.34     rmind int ipsec4_forward(struct mbuf *, int *);
    267   1.1  jonathan 
    268   1.1  jonathan struct inpcb;
    269  1.76      maxv int ipsec_init_pcbpolicy(struct socket *so, struct inpcbpolicy **);
    270  1.64      maxv int ipsec_copy_policy(const struct inpcbpolicy *, struct inpcbpolicy *);
    271  1.64      maxv u_int ipsec_get_reqlevel(const struct ipsecrequest *);
    272  1.64      maxv 
    273  1.79      maxv int ipsec_set_policy(void *, const void *, size_t, kauth_cred_t);
    274  1.70      maxv int ipsec_get_policy(void *, const void *, size_t, struct mbuf **);
    275  1.70      maxv int ipsec_delete_pcbpolicy(void *);
    276  1.69      maxv int ipsec_in_reject(struct mbuf *, void *);
    277   1.1  jonathan 
    278  1.64      maxv struct secasvar *ipsec_lookup_sa(const struct ipsecrequest *,
    279  1.64      maxv     const struct mbuf *);
    280   1.1  jonathan 
    281   1.1  jonathan struct secas;
    282   1.1  jonathan struct tcpcb;
    283  1.64      maxv int ipsec_chkreplay(u_int32_t, const struct secasvar *);
    284  1.64      maxv int ipsec_updatereplay(u_int32_t, const struct secasvar *);
    285   1.1  jonathan 
    286  1.68      maxv size_t ipsec_hdrsiz(struct mbuf *, u_int, void *);
    287  1.64      maxv size_t ipsec4_hdrsiz_tcp(struct tcpcb *);
    288   1.1  jonathan 
    289   1.1  jonathan union sockaddr_union;
    290  1.47       ryo const char *ipsec_address(const union sockaddr_union* sa, char *, size_t);
    291  1.47       ryo const char *ipsec_logsastr(const struct secasvar *, char *, size_t);
    292   1.1  jonathan 
    293   1.1  jonathan /* NetBSD protosw ctlin entrypoint */
    294  1.19  degroote void *esp4_ctlinput(int, const struct sockaddr *, void *);
    295  1.19  degroote void *ah4_ctlinput(int, const struct sockaddr *, void *);
    296   1.1  jonathan 
    297  1.59     ozaki void ipsec_output_init(void);
    298   1.1  jonathan struct m_tag;
    299  1.19  degroote void ipsec4_common_input(struct mbuf *m, ...);
    300  1.64      maxv int ipsec4_common_input_cb(struct mbuf *, struct secasvar *, int, int);
    301  1.61     ozaki int ipsec4_process_packet(struct mbuf *, const struct ipsecrequest *, u_long *);
    302  1.61     ozaki int ipsec_process_done(struct mbuf *, const struct ipsecrequest *,
    303  1.61     ozaki     struct secasvar *);
    304  1.64      maxv 
    305  1.19  degroote struct mbuf *m_clone(struct mbuf *);
    306  1.19  degroote struct mbuf *m_makespace(struct mbuf *, int, int, int *);
    307  1.75      maxv void *m_pad(struct mbuf *, int);
    308  1.19  degroote int m_striphdr(struct mbuf *, int, int);
    309   1.1  jonathan 
    310  1.32  christos void nat_t_ports_get(struct mbuf *, u_int16_t *, u_int16_t *);
    311  1.32  christos 
    312  1.75      maxv extern int ipsec_used __read_mostly;
    313  1.75      maxv extern int ipsec_enabled __read_mostly;
    314   1.1  jonathan 
    315   1.1  jonathan #endif /* _KERNEL */
    316   1.1  jonathan 
    317   1.1  jonathan #ifndef _KERNEL
    318  1.64      maxv char *ipsec_set_policy(const char *, int);
    319  1.64      maxv int ipsec_get_policylen(char *);
    320  1.64      maxv char *ipsec_dump_policy(char *, const char *);
    321  1.64      maxv const char *ipsec_strerror(void);
    322   1.1  jonathan #endif /* !_KERNEL */
    323   1.1  jonathan 
    324   1.9  jonathan #ifdef _KERNEL
    325   1.1  jonathan /* External declarations of per-file init functions */
    326  1.41     ozaki void ah_attach(void);
    327  1.41     ozaki void esp_attach(void);
    328  1.41     ozaki void ipcomp_attach(void);
    329  1.41     ozaki void ipe4_attach(void);
    330  1.41     ozaki void tcpsignature_attach(void);
    331   1.1  jonathan 
    332  1.39     ozaki void ipsec_attach(void);
    333  1.39     ozaki 
    334  1.39     ozaki void sysctl_net_inet_ipsec_setup(struct sysctllog **);
    335  1.39     ozaki #ifdef INET6
    336  1.39     ozaki void sysctl_net_inet6_ipsec6_setup(struct sysctllog **);
    337  1.39     ozaki #endif
    338  1.39     ozaki 
    339   1.9  jonathan #endif /* _KERNEL */
    340  1.12      elad #endif /* !_NETIPSEC_IPSEC_H_ */
    341