xref: /openbsd/sbin/isakmpd/message.h (revision 20c653eb)
1 /* $OpenBSD: message.h,v 1.27 2016/03/10 07:32:16 yasuoka Exp $	 */
2 /* $EOM: message.h,v 1.51 2000/10/10 12:36:39 provos Exp $	 */
3 
4 /*
5  * Copyright (c) 1998, 1999 Niklas Hallqvist.  All rights reserved.
6  * Copyright (c) 1999 Angelos D. Keromytis.  All rights reserved.
7  * Copyright (c) 2001, 2004 H�kan Olsson.  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  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * This code was written under funding by Ericsson Radio Systems.
32  */
33 
34 #ifndef _MESSAGE_H_
35 #define _MESSAGE_H_
36 
37 #include <sys/queue.h>
38 #include <sys/socket.h>
39 #include <sys/uio.h>
40 
41 #include "isakmp.h"
42 
43 struct event;
44 struct message;
45 struct proto;
46 struct sa;
47 struct transport;
48 
49 struct payload {
50 	/* Link all payloads of the same type through here.  */
51 	TAILQ_ENTRY(payload) link;
52 
53 	/* The pointer to the actual payload data.  */
54 	u_int8_t       *p;
55 
56 	/*
57 	 * A pointer to the parent payload, used for proposal and transform
58 	 * payloads.
59          */
60 	struct payload *context;
61 
62 	/* Payload flags described below.  */
63 	int             flags;
64 };
65 
66 /* Payload flags.  */
67 
68 /*
69  * Set this when a payload has been handled, so we later can sweep over
70  * unhandled ones.
71  */
72 #define PL_MARK 1
73 
74 /* A post-send chain of functions to be called.  */
75 struct post_send {
76 	/* Link to the next function in the chain.  */
77 	TAILQ_ENTRY(post_send) link;
78 
79 	/* The actual function.  */
80 	void            (*func) (struct message *);
81 };
82 
83 struct message {
84 	/* Link message in send queues via this link.  */
85 	TAILQ_ENTRY(message) link;
86 
87 	/* Message flags described below.  */
88 	u_int           flags;
89 
90 	/*
91 	 * This is the transport the message either arrived on or will be sent
92 	 * to.
93          */
94 	struct transport *transport;
95 
96 	/*
97 	 * This is the ISAKMP SA protecting this message.
98 	 * XXX Needs to be redone to some keystate pointer or something.
99          */
100 	struct sa      *isakmp_sa;
101 
102 	/* This is the exchange where this message appears.  */
103 	struct exchange *exchange;
104 
105 	/*
106 	 * A segmented buffer structure holding the messages raw contents.  On
107 	 * input only segment 0 will be filled, holding all of the message.
108 	 * On output, as long as the message body is unencrypted each segment
109 	 * will be one payload, after encryption segment 0 will be the
110 	 * unencrypted header, and segment 1 will be the encrypted payloads,
111 	 * all of them.
112          */
113 	struct iovec   *iov;
114 
115 	/* The segment count.  */
116 	u_int           iovlen;
117 
118 	/* Pointer to the last "next payload" field.  */
119 	u_int8_t       *nextp;
120 
121 	/* "Smart" pointers to each payload, sorted by type.  */
122 	TAILQ_HEAD(payload_head, payload) *payload;
123 
124 	/* Number of times this message has been sent.  */
125 	int             xmits;
126 
127 	/* The timeout event causing retransmission of this message.  */
128 	struct event   *retrans;
129 
130 	/* The (possibly encrypted) message text, used for duplicate testing. */
131 	u_int8_t       *orig;
132 	size_t          orig_sz;
133 
134 	/*
135 	 * Extra baggage needed to travel with the message.  Used transiently
136 	 * in context sensitive ways.
137          */
138 	void           *extra;
139 
140 	/*
141 	 * Hooks for stuff needed to be done after the message has gone out to
142 	 * the wire.
143          */
144 	TAILQ_HEAD(post_send_head, post_send) post_send;
145 };
146 
147 /* Message flags.  */
148 
149 /*
150  * This is the last message of an exchange, meaning it should not be
151  * retransmitted other than if we see duplicates from our peer's last
152  * message.
153  */
154 #define MSG_LAST	0x01
155 
156 /* The message has already been encrypted.  */
157 #define MSG_ENCRYPTED	0x02
158 
159 /* The message is on the send queue.  */
160 #define MSG_IN_TRANSIT	0x04
161 
162 /* This message should be kept on the prioritized sendq.  */
163 #define MSG_PRIORITIZED	0x08
164 
165 /* This message has successfully been authenticated. */
166 #define MSG_AUTHENTICATED	0x10
167 
168 /* The message was received on the NAT-T port. */
169 #define MSG_NATT	0x20
170 
171 /* The message must not be retransmitted. */
172 #define	MSG_DONTRETRANSMIT	0x40
173 
174 TAILQ_HEAD(msg_head, message);
175 
176 /* The number of different ISAKMP payloads supported.  */
177 extern u_int8_t payload_index_max;
178 
179 extern int	message_add_payload(struct message *, u_int8_t, u_int8_t *,
180 		    size_t, int);
181 extern int      message_add_sa_payload(struct message *);
182 extern struct message *message_alloc(struct transport *, u_int8_t *, size_t);
183 extern struct message *message_alloc_reply(struct message *);
184 extern u_int8_t *message_copy(struct message *, size_t, size_t *);
185 extern void     message_drop(struct message *, int, struct proto *, int, int);
186 extern void     message_dump_raw(char *, struct message *, int);
187 extern void     message_free(struct message *);
188 extern void	message_init(void);
189 extern int	message_negotiate_sa(struct message *,
190 		    int (*)(struct exchange *, struct sa *, struct sa *));
191 extern int      message_recv(struct message *);
192 extern int      message_register_post_send(struct message *,
193 		    void (*) (struct message *));
194 extern void     message_post_send(struct message *);
195 extern void     message_send(struct message *);
196 extern void     message_send_expire(struct message *);
197 extern void     message_send_delete(struct sa *);
198 extern int      message_send_info(struct message *);
199 extern void     message_send_notification(struct message *, struct sa *,
200 		    u_int16_t, struct proto *, int);
201 extern void     message_setup_header(struct message *, u_int8_t, u_int8_t,
202 		    u_int8_t *);
203 struct payload *payload_first(struct message *, u_int8_t);
204 
205 extern void	message_send_dpd_notify(struct sa*, u_int16_t, u_int32_t);
206 
207 #endif				/* _MESSAGE_H_ */
208