]> git.ipfire.org Git - thirdparty/strongswan.git/blob - Source/charon/payloads/notify_payload.c
- moved packet and socket in new network-package
[thirdparty/strongswan.git] / Source / charon / payloads / notify_payload.c
1 /**
2 * @file notify_payload.c
3 *
4 * @brief Declaration of the class notify_payload_t.
5 *
6 * An object of this type represents an IKEv2 Notify-Payload.
7 *
8 * See section 3.10 of Draft for details of this payload type.
9 *
10 */
11
12 /*
13 * Copyright (C) 2005 Jan Hutter, Martin Willi
14 * Hochschule fuer Technik Rapperswil
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2 of the License, or (at your
19 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
25 */
26
27 /* offsetof macro */
28 #include <stddef.h>
29
30 #include "notify_payload.h"
31
32 #include <payloads/encodings.h>
33 #include <utils/allocator.h>
34
35 /**
36 * Private data of an notify_payload_t Object
37 *
38 */
39 typedef struct private_notify_payload_s private_notify_payload_t;
40
41 struct private_notify_payload_s {
42 /**
43 * public notify_payload_t interface
44 */
45 notify_payload_t public;
46
47 /**
48 * next payload type
49 */
50 u_int8_t next_payload;
51
52 /**
53 * Critical flag
54 */
55 bool critical;
56
57 /**
58 * Length of this payload
59 */
60 u_int16_t payload_length;
61
62 /**
63 * protocol id
64 */
65 u_int8_t protocol_id;
66
67 /**
68 * spi size
69 */
70 u_int8_t spi_size;
71
72 /**
73 * notify message type
74 */
75 u_int16_t notify_message_type;
76
77 /**
78 * Security parameter index (spi)
79 */
80 chunk_t spi;
81
82 /**
83 * Notification data
84 */
85 chunk_t notification_data;
86
87 /**
88 * @brief Computes the length of this payload.
89 *
90 * @param this calling private_ke_payload_t object
91 * @return
92 * SUCCESS in any case
93 */
94 status_t (*compute_length) (private_notify_payload_t *this);
95 };
96
97 /**
98 * Encoding rules to parse or generate a IKEv2-Notify Payload
99 *
100 * The defined offsets are the positions in a object of type
101 * private_notify_payload_t.
102 *
103 */
104 encoding_rule_t notify_payload_encodings[] = {
105 /* 1 Byte next payload type, stored in the field next_payload */
106 { U_INT_8, offsetof(private_notify_payload_t, next_payload) },
107 /* the critical bit */
108 { FLAG, offsetof(private_notify_payload_t, critical) },
109 /* 7 Bit reserved bits, nowhere stored */
110 { RESERVED_BIT, 0 },
111 { RESERVED_BIT, 0 },
112 { RESERVED_BIT, 0 },
113 { RESERVED_BIT, 0 },
114 { RESERVED_BIT, 0 },
115 { RESERVED_BIT, 0 },
116 { RESERVED_BIT, 0 },
117 /* Length of the whole payload*/
118 { PAYLOAD_LENGTH, offsetof(private_notify_payload_t, payload_length) },
119 /* Protocol ID as 8 bit field*/
120 { U_INT_8, offsetof(private_notify_payload_t, protocol_id) },
121 /* SPI Size as 8 bit field*/
122 { SPI_SIZE, offsetof(private_notify_payload_t, spi_size) },
123 /* Notify message type as 16 bit field*/
124 { U_INT_16, offsetof(private_notify_payload_t, notify_message_type) },
125 /* SPI as variable length field*/
126 { SPI, offsetof(private_notify_payload_t, spi) },
127 /* Key Exchange Data is from variable size */
128 { NOTIFICATION_DATA, offsetof(private_notify_payload_t, notification_data) }
129 };
130
131 /*
132 1 2 3
133 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
134 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 ! Next Payload !C! RESERVED ! Payload Length !
136 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 ! Protocol ID ! SPI Size ! Notify Message Type !
138 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 ! !
140 ~ Security Parameter Index (SPI) ~
141 ! !
142 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 ! !
144 ~ Notification Data ~
145 ! !
146 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
147 */
148
149 /**
150 * Implements payload_t's verify function.
151 * See #payload_s.verify for description.
152 */
153 static status_t verify(private_notify_payload_t *this)
154 {
155 if (this->critical)
156 {
157 /* critical bit is set! */
158 return FAILED;
159 }
160 if (this->protocol_id > 3)
161 {
162 /* reserved for future use */
163 return FAILED;
164 }
165
166 /* notify message types and data is not getting checked in here */
167
168 return SUCCESS;
169 }
170
171 /**
172 * Implements payload_t's get_encoding_rules function.
173 * See #payload_s.get_encoding_rules for description.
174 */
175 static status_t get_encoding_rules(private_notify_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
176 {
177 *rules = notify_payload_encodings;
178 *rule_count = sizeof(notify_payload_encodings) / sizeof(encoding_rule_t);
179 return SUCCESS;
180 }
181
182 /**
183 * Implements payload_t's get_type function.
184 * See #payload_s.get_type for description.
185 */
186 static payload_type_t get_type(private_notify_payload_t *this)
187 {
188 return KEY_EXCHANGE;
189 }
190
191 /**
192 * Implements payload_t's get_next_type function.
193 * See #payload_s.get_next_type for description.
194 */
195 static payload_type_t get_next_type(private_notify_payload_t *this)
196 {
197 return (this->next_payload);
198 }
199
200 /**
201 * Implements payload_t's set_next_type function.
202 * See #payload_s.set_next_type for description.
203 */
204 static status_t set_next_type(private_notify_payload_t *this,payload_type_t type)
205 {
206 this->next_payload = type;
207 return SUCCESS;
208 }
209
210 /**
211 * Implements payload_t's get_length function.
212 * See #payload_s.get_length for description.
213 */
214 static size_t get_length(private_notify_payload_t *this)
215 {
216 this->compute_length(this);
217 return this->payload_length;
218 }
219
220 /**
221 * Implements private_ke_payload_t's compute_length function.
222 * See #private_ke_payload_s.compute_length for description.
223 */
224 static status_t compute_length (private_notify_payload_t *this)
225 {
226 size_t length = NOTIFY_PAYLOAD_HEADER_LENGTH;
227 if (this->notification_data.ptr != NULL)
228 {
229 length += this->notification_data.len;
230 }
231 if (this->spi.ptr != NULL)
232 {
233 length += this->spi.len;
234 }
235
236 this->payload_length = length;
237
238 return SUCCESS;
239 }
240
241
242 /**
243 * Implements notify_payload_t's get_protocol_id function.
244 * See #notify_payload_s.get_protocol_id for description.
245 */
246 u_int8_t get_protocol_id(private_notify_payload_t *this)
247 {
248 return this->protocol_id;
249 }
250
251 /**
252 * Implements notify_payload_t's set_protocol_id function.
253 * See #notify_payload_s.set_protocol_id for description.
254 */
255 status_t set_protocol_id(private_notify_payload_t *this, u_int8_t protocol_id)
256 {
257 this->protocol_id = protocol_id;
258 return SUCCESS;
259 }
260
261 /**
262 * Implements notify_payload_t's get_notification_data function.
263 * See #notify_payload_s.get_notification_data for description.
264 */
265 u_int16_t get_notify_message_type(private_notify_payload_t *this)
266 {
267 return this->notify_message_type;
268 }
269
270 /**
271 * Implements notify_payload_t's get_notification_data function.
272 * See #notify_payload_s.get_notification_data for description.
273 */
274 status_t set_notify_message_type(private_notify_payload_t *this, u_int16_t notify_message_type)
275 {
276 this->notify_message_type = notify_message_type;
277 return SUCCESS;
278 }
279
280 /**
281 * Implements notify_payload_t's get_spi function.
282 * See #notify_payload_s.get_spi for description.
283 */
284 chunk_t get_spi(private_notify_payload_t *this)
285 {
286 return (this->spi);
287 }
288
289 /**
290 * Implements notify_payload_t's set_spi function.
291 * See #notify_payload_s.set_spi for description.
292 */
293 status_t set_spi(private_notify_payload_t *this, chunk_t spi)
294 {
295 /* destroy existing data first */
296 if (this->spi.ptr != NULL)
297 {
298 /* free existing value */
299 allocator_free(this->spi.ptr);
300 this->spi.ptr = NULL;
301 this->spi.len = 0;
302
303 }
304
305 this->spi.ptr = allocator_clone_bytes(spi.ptr,spi.len);
306 if (this->spi.ptr == NULL)
307 {
308 return OUT_OF_RES;
309 }
310 this->spi.len = spi.len;
311 this->spi_size = spi.len;
312 this->compute_length(this);
313
314 return SUCCESS;
315 }
316
317
318 /**
319 * Implements notify_payload_t's get_notification_data function.
320 * See #notify_payload_s.get_notification_data for description.
321 */
322 chunk_t get_notification_data(private_notify_payload_t *this)
323 {
324 return (this->notification_data);
325 }
326
327 /**
328 * Implements notify_payload_t's get_notification_data function.
329 * See #notify_payload_s.get_notification_data for description.
330 */
331 status_t set_notification_data(private_notify_payload_t *this, chunk_t notification_data)
332 {
333 /* destroy existing data first */
334 if (this->notification_data.ptr != NULL)
335 {
336 /* free existing value */
337 allocator_free(this->notification_data.ptr);
338 this->notification_data.ptr = NULL;
339 this->notification_data.len = 0;
340
341 }
342
343 this->notification_data.ptr = allocator_clone_bytes(notification_data.ptr,notification_data.len);
344 if (this->notification_data.ptr == NULL)
345 {
346 return OUT_OF_RES;
347 }
348 this->notification_data.len = notification_data.len;
349 this->compute_length(this);
350
351 return SUCCESS;
352 }
353
354 /**
355 * Implements payload_t's and notify_payload_t's destroy function.
356 * See #payload_s.destroy or notify_payload_s.destroy for description.
357 */
358 static status_t destroy(private_notify_payload_t *this)
359 {
360 if (this->notification_data.ptr != NULL)
361 {
362 allocator_free(this->notification_data.ptr);
363 }
364 if (this->spi.ptr != NULL)
365 {
366 allocator_free(this->spi.ptr);
367 }
368
369 allocator_free(this);
370 return SUCCESS;
371 }
372
373 /*
374 * Described in header
375 */
376 notify_payload_t *notify_payload_create()
377 {
378 private_notify_payload_t *this = allocator_alloc_thing(private_notify_payload_t);
379 if (this == NULL)
380 {
381 return NULL;
382 }
383 /* interface functions */
384 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
385 this->public.payload_interface.get_encoding_rules = (status_t (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
386 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
387 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
388 this->public.payload_interface.set_next_type = (status_t (*) (payload_t *,payload_type_t)) set_next_type;
389 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
390 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
391
392 /* public functions */
393 this->public.get_protocol_id = (u_int8_t (*) (notify_payload_t *)) get_protocol_id;
394 this->public.set_protocol_id = (status_t (*) (notify_payload_t *,u_int8_t)) set_protocol_id;
395 this->public.get_notify_message_type = (u_int16_t (*) (notify_payload_t *)) get_notify_message_type;
396 this->public.set_notify_message_type = (status_t (*) (notify_payload_t *,u_int16_t)) set_notify_message_type;
397 this->public.get_spi = (chunk_t (*) (notify_payload_t *)) get_spi;
398 this->public.set_spi = (status_t (*) (notify_payload_t *,chunk_t)) set_spi;
399 this->public.get_notification_data = (chunk_t (*) (notify_payload_t *)) get_notification_data;
400 this->public.set_notification_data = (status_t (*) (notify_payload_t *,chunk_t)) set_notification_data;
401 this->public.destroy = (status_t (*) (notify_payload_t *)) destroy;
402
403 /* private functions */
404 this->compute_length = compute_length;
405
406 /* set default values of the fields */
407 this->critical = NOTIFY_PAYLOAD_CRITICAL_FLAG;
408 this->next_payload = NO_PAYLOAD;
409 this->payload_length = NOTIFY_PAYLOAD_HEADER_LENGTH;
410 this->protocol_id = 0;
411 this->notify_message_type = 0;
412 this->spi.ptr = NULL;
413 this->spi.len = 0;
414 this->notification_data.ptr = NULL;
415 this->notification_data.len = 0;
416
417 return (&(this->public));
418 }
419