ip_ipsp.h revision 1.26
1/*	$OpenBSD: ip_ipsp.h,v 1.26 1999/02/24 23:45:52 angelos Exp $	*/
2
3/*
4 * The authors of this code are John Ioannidis (ji@tla.org),
5 * Angelos D. Keromytis (kermit@csd.uch.gr) and
6 * Niels Provos (provos@physnet.uni-hamburg.de).
7 *
8 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
9 * in November 1995.
10 *
11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12 * by Angelos D. Keromytis.
13 *
14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15 * and Niels Provos.
16 *
17 * Additional features in 1999 by Angelos D. Keromytis.
18 *
19 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
20 * Angelos D. Keromytis and Niels Provos.
21 *
22 * Permission to use, copy, and modify this software without fee
23 * is hereby granted, provided that this entire notice is included in
24 * all copies of any software which is or includes a copy or
25 * modification of this software.
26 * You may use this code under the GNU public license if you so wish. Please
27 * contribute changes back to the authors under this freer than GPL license
28 * so that we may further the use of strong encryption without limitations to
29 * all.
30 *
31 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
32 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
33 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
34 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
35 * PURPOSE.
36 */
37
38/*
39 * IPSP global definitions.
40 */
41
42#include <sys/md5k.h>
43#include <netinet/ip_sha1.h>
44#include <netinet/ip_rmd160.h>
45#include <netinet/ip_blf.h>
46#include <netinet/ip_cast.h>
47#include <netinet/ip_skipjack.h>
48#include <sys/socket.h>
49
50/* HMAC key sizes */
51#define MD5HMAC96_KEYSIZE       16
52#define SHA1HMAC96_KEYSIZE      20
53#define RIPEMD160HMAC96_KEYSIZE 20
54
55/* IV lengths */
56#define ESP_DES_IVS		8
57#define ESP_3DES_IVS		8
58#define ESP_BLF_IVS             8
59#define ESP_CAST_IVS            8
60#define ESP_SKIPJACK_IVS	8
61#define ESP_MAX_IVS		8       /* Keep updated */
62
63/* Block sizes -- it is assumed that they're powers of 2 */
64#define ESP_DES_BLKS		8
65#define ESP_3DES_BLKS		8
66#define ESP_BLF_BLKS            8
67#define ESP_CAST_BLKS           8
68#define ESP_SKIPJACK_BLKS	8
69#define ESP_MAX_BLKS            8       /* Keep updated */
70
71#define HMAC_BLOCK_LEN		64
72
73#define AH_HMAC_HASHLEN		12	/* 96 bits of authenticator */
74#define AH_HMAC_RPLENGTH        4	/* 32 bits of replay counter */
75#define AH_HMAC_INITIAL_RPL	1	/* Replay counter initial value */
76
77/* HMAC definitions */
78#define HMAC_IPAD_VAL           0x36
79#define HMAC_OPAD_VAL           0x5C
80#define HMAC_BLOCK_LEN          64
81
82/* Authenticator lengths */
83#define AH_MD5_ALEN		16
84#define AH_SHA1_ALEN		20
85#define AH_RMD160_ALEN		20
86#define AH_ALEN_MAX		20 	/* Keep updated */
87
88struct sockaddr_encap
89{
90    u_int8_t	sen_len;		/* length */
91    u_int8_t	sen_family;		/* PF_KEY */
92    u_int16_t	sen_type;		/* see SENT_* */
93    union
94    {
95	u_int8_t	Data[16];	/* other stuff mapped here */
96
97	struct				/* SENT_IP4 */
98	{
99	    struct in_addr Src;
100	    struct in_addr Dst;
101	    u_int16_t Sport;
102	    u_int16_t Dport;
103	    u_int8_t Proto;
104	    u_int8_t Filler[3];
105	} Sip4;
106
107	struct				/* SENT_IPSP */
108	{
109	    struct in_addr Dst;
110	    u_int32_t Spi;
111	    u_int8_t Sproto;
112	    u_int8_t Filler[7];
113	} Sipsp;
114    } Sen;
115};
116
117#define sen_data	Sen.Data
118#define sen_ip_src	Sen.Sip4.Src
119#define sen_ip_dst	Sen.Sip4.Dst
120#define sen_proto	Sen.Sip4.Proto
121#define sen_sport	Sen.Sip4.Sport
122#define sen_dport	Sen.Sip4.Dport
123#define sen_ipsp_dst	Sen.Sipsp.Dst
124#define sen_ipsp_spi	Sen.Sipsp.Spi
125#define sen_ipsp_sproto	Sen.Sipsp.Sproto
126
127/*
128 * The "type" is really part of the address as far as the routing
129 * system is concerned. By using only one bit in the type field
130 * for each type, we sort-of make sure that different types of
131 * encapsulation addresses won't be matched against the wrong type.
132 *
133 */
134
135#define SENT_IP4	0x0001		/* data is two struct in_addr */
136#define SENT_IPSP	0x0002		/* data as in IP4 plus SPI */
137
138/*
139 * SENT_HDRLEN is the length of the "header"
140 * SENT_*_LEN are the lengths of various forms of sen_data
141 * SENT_*_OFF are the offsets in the sen_data array of various fields
142 */
143
144#define SENT_HDRLEN	(2 * sizeof(u_int8_t) + sizeof(u_int16_t))
145
146#define SENT_IP4_SRCOFF	(0)
147#define SENT_IP4_DSTOFF (sizeof (struct in_addr))
148
149#define SENT_IP4_LEN	20
150#define SENT_IPSP_LEN	20
151
152#define NOTIFY_SOFT_EXPIRE      0       /* Soft expiration of SA */
153#define NOTIFY_HARD_EXPIRE      1       /* Hard expiration of SA */
154#define NOTIFY_REQUEST_SA       2       /* Establish an SA */
155
156#define NOTIFY_SATYPE_CONF      1       /* SA should do encryption */
157#define NOTIFY_SATYPE_AUTH      2       /* SA should do authentication */
158#define NOTIFY_SATYPE_TUNNEL    4       /* SA should use tunneling */
159
160/*
161 * For encapsulation routes are possible not only for the destination
162 * address but also for the protocol, source and destination ports
163 * if available
164 */
165
166struct route_enc {
167    struct rtentry *re_rt;
168    struct sockaddr_encap re_dst;
169};
170
171struct expiration
172{
173    u_int32_t             exp_timeout;
174    union sockaddr_union  exp_dst;
175    u_int32_t             exp_spi;
176    u_int8_t              exp_sproto;
177    struct expiration    *exp_next;
178    struct expiration    *exp_prev;
179};
180
181struct flow
182{
183    struct flow           *flow_next;	/* Next in flow chain */
184    struct flow           *flow_prev;	/* Previous in flow chain */
185    struct tdb            *flow_sa;	/* Pointer to the SA */
186    union sockaddr_union  flow_src;   	/* Source address */
187    union sockaddr_union  flow_srcmask; /* Source netmask */
188    union sockaddr_union  flow_dst;	/* Destination address */
189    union sockaddr_union  flow_dstmask;	/* Destination netmask */
190    u_int8_t	          flow_proto;	/* Transport protocol, if applicable */
191    u_int8_t	          foo[3];	/* Alignment */
192};
193
194struct tdb				/* tunnel descriptor block */
195{
196    struct tdb	     *tdb_hnext;  	/* Next in hash chain */
197    struct tdb	     *tdb_onext;        /* Next in output */
198    struct tdb	     *tdb_inext;        /* Previous in output */
199
200    struct xformsw   *tdb_xform;	/* Transformation to use */
201    struct enc_xform *tdb_encalgxform;  /* Encryption algorithm xform */
202    struct auth_hash *tdb_authalgxform; /* Authentication algorithm xform */
203
204#define TDBF_UNIQUE	      0x00001	/* This should not be used by others */
205#define TDBF_TIMER            0x00002	/* Absolute expiration timer in use */
206#define TDBF_BYTES            0x00004	/* Check the byte counters */
207#define TDBF_ALLOCATIONS      0x00008	/* Check the flows counters */
208#define TDBF_INVALID          0x00010	/* This SPI is not valid yet/anymore */
209#define TDBF_FIRSTUSE         0x00020	/* Expire after first use */
210#define TDBF_HALFIV           0x00040   /* Use half-length IV (ESP old only) */
211#define TDBF_SOFT_TIMER       0x00080	/* Soft expiration */
212#define TDBF_SOFT_BYTES       0x00100	/* Soft expiration */
213#define TDBF_SOFT_ALLOCATIONS 0x00200	/* Soft expiration */
214#define TDBF_SOFT_FIRSTUSE    0x00400	/* Soft expiration */
215#define TDBF_PFS              0x00800	/* Ask for PFS from Key Mgmt. */
216#define TDBF_TUNNELING        0x01000	/* Force IP-IP encapsulation */
217    u_int32_t	      tdb_flags;  	/* Flags related to this TDB */
218
219    u_int32_t         tdb_exp_allocations;  /* Expire after so many flows */
220    u_int32_t         tdb_soft_allocations; /* Expiration warning */
221    u_int32_t         tdb_cur_allocations;  /* Total number of allocations */
222
223    u_int64_t         tdb_exp_bytes;    /* Expire after so many bytes passed */
224    u_int64_t         tdb_soft_bytes;	/* Expiration warning */
225    u_int64_t         tdb_cur_bytes;	/* Current count of bytes */
226
227    u_int64_t         tdb_exp_timeout;	/* When does the SPI expire */
228    u_int64_t         tdb_soft_timeout;	/* Send a soft-expire warning */
229    u_int64_t         tdb_established;	/* When was the SPI established */
230
231    u_int64_t	      tdb_first_use;	  /* When was it first used */
232    u_int64_t         tdb_soft_first_use; /* Soft warning */
233    u_int64_t         tdb_exp_first_use;  /* Expire if tdb_first_use +
234					   * tdb_exp_first_use <= curtime */
235    u_int32_t	      tdb_spi;    	/* SPI */
236    u_int16_t         tdb_amxkeylen;    /* AH-old only */
237    u_int16_t         tdb_ivlen;        /* IV length */
238    u_int8_t	      tdb_sproto;	/* IPsec protocol */
239    u_int8_t          tdb_wnd;          /* Replay window */
240    u_int16_t         tdb_FILLER;       /* Padding */
241
242    union sockaddr_union tdb_dst;	/* Destination address for this SA */
243    union sockaddr_union tdb_src;	/* Source address for this SA */
244    union sockaddr_union tdb_proxy;
245
246    u_int8_t         *tdb_key;          /* Key material (schedules) */
247    u_int8_t         *tdb_ictx;         /* Authentication contexts */
248    u_int8_t         *tdb_octx;
249    u_int8_t         *tdb_srcid;        /* Source ID for this SA */
250    u_int8_t         *tdb_dstid;        /* Destination ID for this SA */
251    u_int8_t         *tdb_amxkey;       /* AH-old only */
252
253    union
254    {
255	u_int8_t  Iv[ESP_3DES_IVS];     /* That's enough space */
256	u_int32_t Ivl;        	        /* Make sure this is 4 bytes */
257	u_int64_t Ivq; 		        /* Make sure this is 8 bytes! */
258    }IV;
259#define tdb_iv  IV.Iv
260#define tdb_ivl IV.Ivl
261#define tdb_ivq IV.Ivq
262
263    u_int32_t         tdb_rpl;	        /* Replay counter */
264    u_int32_t         tdb_bitmap;       /* Used for replay sliding window */
265    u_int32_t         tdb_initial;	/* Initial replay value */
266
267    u_int32_t         tdb_epoch;	/* Used by the kernfs interface */
268    u_int16_t         tdb_srcid_len;
269    u_int16_t         tdb_dstid_len;
270    u_int16_t         tdb_srcid_type;
271    u_int16_t         tdb_dstid_type;
272
273    struct flow	     *tdb_flow; 	/* Which flows use this SA */
274};
275
276#define TDB_HASHMOD	257
277
278struct auth_hash {
279    int type;
280    char *name;
281    u_int16_t keysize;
282    u_int16_t hashsize;
283    u_int16_t ctxsize;
284    void (*Init)(void *);
285    void (*Update)(void *, u_int8_t *, u_int16_t);
286    void (*Final)(u_int8_t *, void *);
287};
288
289struct enc_xform {
290    int type;
291    char *name;
292    u_int16_t blocksize, ivsize;
293    u_int16_t minkey, maxkey;
294    u_int32_t ivmask;           /* Or all possible modes, zero iv = 1 */
295    void (*encrypt)(struct tdb *, u_int8_t *);
296    void (*decrypt)(struct tdb *, u_int8_t *);
297};
298
299struct ipsecinit
300{
301    u_int8_t       *ii_enckey;
302    u_int8_t       *ii_authkey;
303    u_int16_t       ii_enckeylen;
304    u_int16_t       ii_authkeylen;
305    u_int8_t        ii_encalg;
306    u_int8_t        ii_authalg;
307};
308
309struct xformsw
310{
311    u_short		xf_type;	/* Unique ID of xform */
312    u_short		xf_flags;	/* flags (see below) */
313    char		*xf_name;	/* human-readable name */
314    int		(*xf_attach)(void);	/* called at config time */
315    int		(*xf_init)(struct tdb *, struct xformsw *, struct ipsecinit *);
316    int		(*xf_zeroize)(struct tdb *); /* termination */
317    struct mbuf 	*(*xf_input)(struct mbuf *, struct tdb *); /* input */
318    int		(*xf_output)(struct mbuf *, struct sockaddr_encap *,
319			     struct tdb *, struct mbuf **);        /* output */
320};
321
322/* xform IDs */
323#define XF_IP4		1	/* IP inside IP */
324#define XF_OLD_AH	2	/* RFCs 1828 & 1852 */
325#define XF_OLD_ESP	3	/* RFCs 1829 & 1851 */
326#define XF_NEW_AH	4	/* AH HMAC 96bits */
327#define XF_NEW_ESP	5	/* ESP + auth 96bits + replay counter */
328
329/* xform attributes */
330#define XFT_AUTH	0x0001
331#define XFT_CONF	0x0100
332
333#define IPSEC_ZEROES_SIZE	64
334#define IPSEC_KERNFS_BUFSIZE    4096
335
336#if BYTE_ORDER == LITTLE_ENDIAN
337static __inline u_int64_t
338htonq(u_int64_t q)
339{
340    register u_int32_t u, l;
341    u = q >> 32;
342    l = (u_int32_t) q;
343
344    return htonl(u) | ((u_int64_t)htonl(l) << 32);
345}
346
347#define ntohq(_x) htonq(_x)
348
349#elif BYTE_ORDER == BIG_ENDIAN
350
351#define htonq(_x) (_x)
352#define ntohq(_x) htonq(_x)
353
354#else
355#error  "Please fix <machine/endian.h>"
356#endif
357
358/*
359 * Names for IPsec sysctl objects
360 */
361#define IPSECCTL_PFKEY  		0
362#define IPSECCTL_MAXID			1
363
364#define CTL_IPSEC_NAMES {\
365	{ "pfkey", CTLTYPE_NODE }, \
366}
367
368#define PFKEYCTL_ENCDEBUG	1
369#define PFKEYCTL_MAXID		2
370
371#define PFKEYCTL_NAMES {\
372	{ 0, 0 }, \
373	{ "encdebug", CTLTYPE_INT }, \
374}
375
376#ifdef _KERNEL
377extern int encdebug;
378extern int ipsec_in_use;
379extern u_int8_t hmac_ipad_buffer[64];
380extern u_int8_t hmac_opad_buffer[64];
381
382struct tdb *tdbh[TDB_HASHMOD];
383struct expiration *explist;
384extern struct xformsw xformsw[], *xformswNXFORMSW;
385
386/* Check if a given tdb has encryption, authentication and/or tunneling */
387#define TDB_ATTRIB(x) (((x)->tdb_encalgxform ? NOTIFY_SATYPE_CONF : 0)| \
388		       ((x)->tdb_authalgxform ? NOTIFY_SATYPE_AUTH : 0))
389
390/* Traverse spi chain and get attributes */
391
392#define SPI_CHAIN_ATTRIB(have, TDB_DIR, TDBP) {\
393	struct tdb *tmptdb = (TDBP); \
394	(have) = 0; \
395	\
396	while (tmptdb && tmptdb->tdb_xform) { \
397	        if (tmptdb == NULL || tmptdb->tdb_flags & TDBF_INVALID) \
398	                break; \
399                (have) |= TDB_ATTRIB(tmptdb); \
400                tmptdb = tmptdb->TDB_DIR; \
401        } \
402}
403
404/* Misc. */
405extern char *inet_ntoa4(struct in_addr);
406extern char *ipsp_address(union sockaddr_union);
407
408/* TDB management routines */
409extern u_int32_t reserve_spi(u_int32_t, u_int32_t, union sockaddr_union *,
410			     union sockaddr_union *, u_int8_t, int *);
411extern struct tdb *gettdb(u_int32_t, union sockaddr_union *, u_int8_t);
412extern void puttdb(struct tdb *);
413extern int tdb_delete(struct tdb *, int);
414extern int tdb_init (struct tdb *, u_int16_t, struct ipsecinit *);
415
416/* Expiration management routines */
417extern struct expiration *get_expiration(void);
418extern void put_expiration(struct expiration *);
419extern void handle_expirations(void *);
420extern void cleanup_expirations(union sockaddr_union *, u_int32_t, u_int8_t);
421
422/* Flow management routines */
423extern struct flow *get_flow(void);
424extern void put_flow(struct flow *, struct tdb *);
425extern void delete_flow(struct flow *, struct tdb *);
426extern struct flow *find_flow(union sockaddr_union *, union sockaddr_union *,
427			      union sockaddr_union *, union sockaddr_union *,
428			      u_int8_t, struct tdb *);
429extern struct flow *find_global_flow(union sockaddr_union *,
430				     union sockaddr_union *,
431				     union sockaddr_union *,
432				     union sockaddr_union *, u_int8_t);
433
434/* XF_IP4 */
435extern int ipe4_attach(void);
436extern int ipe4_init(struct tdb *, struct xformsw *, struct ipsecinit *);
437extern int ipe4_zeroize(struct tdb *);
438extern int ipe4_output(struct mbuf *, struct sockaddr_encap *, struct tdb *,
439		       struct mbuf **);
440extern void ipe4_input __P((struct mbuf *, ...));
441extern void ip4_input __P((struct mbuf *, ...));
442
443/* XF_OLD_AH */
444extern int ah_old_attach(void);
445extern int ah_old_init(struct tdb *, struct xformsw *, struct ipsecinit *);
446extern int ah_old_zeroize(struct tdb *);
447extern int ah_old_output(struct mbuf *, struct sockaddr_encap *, struct tdb *,
448			 struct mbuf **);
449extern struct mbuf *ah_old_input(struct mbuf *, struct tdb *);
450
451/* XF_NEW_AH */
452extern int ah_new_attach(void);
453extern int ah_new_init(struct tdb *, struct xformsw *, struct ipsecinit *);
454extern int ah_new_zeroize(struct tdb *);
455extern int ah_new_output(struct mbuf *, struct sockaddr_encap *, struct tdb *,
456			 struct mbuf **);
457extern struct mbuf *ah_new_input(struct mbuf *, struct tdb *);
458
459/* XF_OLD_ESP */
460extern int esp_old_attach(void);
461extern int esp_old_init(struct tdb *, struct xformsw *, struct ipsecinit *);
462extern int esp_old_zeroize(struct tdb *);
463extern int esp_old_output(struct mbuf *, struct sockaddr_encap *, struct tdb *,
464			  struct mbuf **);
465extern struct mbuf *esp_old_input(struct mbuf *, struct tdb *);
466
467/* XF_NEW_ESP */
468extern int esp_new_attach(void);
469extern int esp_new_init(struct tdb *, struct xformsw *, struct ipsecinit *);
470extern int esp_new_zeroize(struct tdb *);
471extern int esp_new_output(struct mbuf *, struct sockaddr_encap *, struct tdb *,
472			  struct mbuf **);
473extern struct mbuf *esp_new_input(struct mbuf *, struct tdb *);
474
475/* Padding */
476extern caddr_t m_pad(struct mbuf *, int, int);
477
478/* Replay window */
479extern int checkreplaywindow32(u_int32_t, u_int32_t, u_int32_t *, u_int32_t,
480                               u_int32_t *);
481
482extern unsigned char ipseczeroes[];
483#endif
484