Merge branch 'ikev1-clean' into ikev1-master
[strongswan.git] / src / libcharon / encoding / payloads / id_payload.c
1 /*
2 * Copyright (C) 2005-2011 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2007-2011 Tobias Brunner
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include <stddef.h>
20
21 #include "id_payload.h"
22
23 #include <daemon.h>
24 #include <encoding/payloads/encodings.h>
25
26 typedef struct private_id_payload_t private_id_payload_t;
27
28 /**
29 * Private data of an id_payload_t object.
30 */
31 struct private_id_payload_t {
32
33 /**
34 * Public id_payload_t interface.
35 */
36 id_payload_t public;
37
38 /**
39 * Next payload type.
40 */
41 u_int8_t next_payload;
42
43 /**
44 * Critical flag.
45 */
46 bool critical;
47
48 /**
49 * Reserved bits
50 */
51 bool reserved_bit[7];
52
53 /**
54 * Reserved bytes
55 */
56 u_int8_t reserved_byte[3];
57
58 /**
59 * Length of this payload.
60 */
61 u_int16_t payload_length;
62
63 /**
64 * Type of the ID Data.
65 */
66 u_int8_t id_type;
67
68 /**
69 * The contained id data value.
70 */
71 chunk_t id_data;
72
73 /**
74 * Tunneled protocol ID for IKEv1 quick modes.
75 */
76 u_int8_t protocol_id;
77
78 /**
79 * Tunneled port for IKEv1 quick modes.
80 */
81 u_int16_t port;
82
83 /**
84 * one of ID_INITIATOR, ID_RESPONDER, IDv1 and NAT_OA_V1
85 */
86 payload_type_t type;
87 };
88
89 /**
90 * Encoding rules for an IKEv2 ID payload
91 */
92 static encoding_rule_t encodings_v2[] = {
93 /* 1 Byte next payload type, stored in the field next_payload */
94 { U_INT_8, offsetof(private_id_payload_t, next_payload) },
95 /* the critical bit */
96 { FLAG, offsetof(private_id_payload_t, critical) },
97 /* 7 Bit reserved bits */
98 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[0]) },
99 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[1]) },
100 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[2]) },
101 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[3]) },
102 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[4]) },
103 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[5]) },
104 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[6]) },
105 /* Length of the whole payload*/
106 { PAYLOAD_LENGTH, offsetof(private_id_payload_t, payload_length) },
107 /* 1 Byte ID type*/
108 { U_INT_8, offsetof(private_id_payload_t, id_type) },
109 /* 3 reserved bytes */
110 { RESERVED_BYTE, offsetof(private_id_payload_t, reserved_byte[0])},
111 { RESERVED_BYTE, offsetof(private_id_payload_t, reserved_byte[1])},
112 { RESERVED_BYTE, offsetof(private_id_payload_t, reserved_byte[2])},
113 /* some id data bytes, length is defined in PAYLOAD_LENGTH */
114 { CHUNK_DATA, offsetof(private_id_payload_t, id_data) },
115 };
116
117 /*
118 1 2 3
119 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
120 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
121 ! Next Payload !C! RESERVED ! Payload Length !
122 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 ! ID Type ! RESERVED |
124 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
125 ! !
126 ~ Identification Data ~
127 ! !
128 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
129 */
130
131 /**
132 * Encoding rules for an IKEv1 ID payload
133 */
134 static encoding_rule_t encodings_v1[] = {
135 /* 1 Byte next payload type, stored in the field next_payload */
136 { U_INT_8, offsetof(private_id_payload_t, next_payload) },
137 /* Reserved Byte is skipped */
138 { RESERVED_BYTE, offsetof(private_id_payload_t, reserved_byte[0])},
139 /* Length of the whole payload*/
140 { PAYLOAD_LENGTH, offsetof(private_id_payload_t, payload_length) },
141 /* 1 Byte ID type*/
142 { U_INT_8, offsetof(private_id_payload_t, id_type) },
143 { U_INT_8, offsetof(private_id_payload_t, protocol_id) },
144 { U_INT_16, offsetof(private_id_payload_t, port) },
145 /* some id data bytes, length is defined in PAYLOAD_LENGTH */
146 { CHUNK_DATA, offsetof(private_id_payload_t, id_data) },
147 };
148
149 /*
150 1 2 3
151 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
152 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
153 ! Next Payload ! RESERVED ! Payload Length !
154 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
155 ! ID Type ! Protocol ID ! Port |
156 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
157 ! !
158 ~ Identification Data ~
159 ! !
160 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161 */
162
163 METHOD(payload_t, verify, status_t,
164 private_id_payload_t *this)
165 {
166 bool bad_length = FALSE;
167
168 if (this->type == NAT_OA_V1 &&
169 this->id_type != ID_IPV4_ADDR && this->id_type != ID_IPV6_ADDR)
170 {
171 DBG1(DBG_ENC, "invalid ID type %N for %N payload", id_type_names,
172 this->id_type, payload_type_short_names, this->type);
173 return FAILED;
174 }
175 switch (this->id_type)
176 {
177 case ID_IPV4_ADDR_RANGE:
178 case ID_IPV4_ADDR_SUBNET:
179 bad_length = this->id_data.len != 8;
180 break;
181 case ID_IPV6_ADDR_RANGE:
182 case ID_IPV6_ADDR_SUBNET:
183 bad_length = this->id_data.len != 32;
184 break;
185 }
186 if (bad_length)
187 {
188 DBG1(DBG_ENC, "invalid %N length (%d bytes)",
189 id_type_names, this->id_type, this->id_data.len);
190 return FAILED;
191 }
192 return SUCCESS;
193 }
194
195 METHOD(payload_t, get_encoding_rules, int,
196 private_id_payload_t *this, encoding_rule_t **rules)
197 {
198 if (this->type == ID_V1 || this->type == NAT_OA_V1)
199 {
200 *rules = encodings_v1;
201 return countof(encodings_v1);
202 }
203 *rules = encodings_v2;
204 return countof(encodings_v2);
205 }
206
207 METHOD(payload_t, get_header_length, int,
208 private_id_payload_t *this)
209 {
210 return 8;
211 }
212
213 METHOD(payload_t, get_type, payload_type_t,
214 private_id_payload_t *this)
215 {
216 return this->type;
217 }
218
219 METHOD(payload_t, get_next_type, payload_type_t,
220 private_id_payload_t *this)
221 {
222 return this->next_payload;
223 }
224
225 METHOD(payload_t, set_next_type, void,
226 private_id_payload_t *this, payload_type_t type)
227 {
228 this->next_payload = type;
229 }
230
231 METHOD(payload_t, get_length, size_t,
232 private_id_payload_t *this)
233 {
234 return this->payload_length;
235 }
236
237 METHOD(id_payload_t, get_identification, identification_t*,
238 private_id_payload_t *this)
239 {
240 return identification_create_from_encoding(this->id_type, this->id_data);
241 }
242
243 /**
244 * Create a traffic selector from an range ID
245 */
246 static traffic_selector_t *get_ts_from_range(private_id_payload_t *this,
247 ts_type_t type)
248 {
249 return traffic_selector_create_from_bytes(this->protocol_id, type,
250 chunk_create(this->id_data.ptr, this->id_data.len / 2), this->port,
251 chunk_skip(this->id_data, this->id_data.len / 2), this->port ?: 65535);
252 }
253
254 /**
255 * Create a traffic selector from an subnet ID
256 */
257 static traffic_selector_t *get_ts_from_subnet(private_id_payload_t *this,
258 ts_type_t type)
259 {
260 chunk_t net, netmask;
261 int i;
262
263 net = chunk_create(this->id_data.ptr, this->id_data.len / 2);
264 netmask = chunk_skip(this->id_data, this->id_data.len / 2);
265 for (i = 0; i < net.len; i++)
266 {
267 netmask.ptr[i] = (netmask.ptr[i] ^ 0xFF) | net.ptr[i];
268 }
269 return traffic_selector_create_from_bytes(this->protocol_id, type,
270 net, this->port, netmask, this->port ?: 65535);
271 }
272
273 /**
274 * Create a traffic selector from an IP ID
275 */
276 static traffic_selector_t *get_ts_from_ip(private_id_payload_t *this,
277 ts_type_t type)
278 {
279 return traffic_selector_create_from_bytes(this->protocol_id, type,
280 this->id_data, this->port, this->id_data, this->port ?: 65535);
281 }
282
283 METHOD(id_payload_t, get_ts, traffic_selector_t*,
284 private_id_payload_t *this)
285 {
286 switch (this->id_type)
287 {
288 case ID_IPV4_ADDR_SUBNET:
289 if (this->id_data.len == 8)
290 {
291 return get_ts_from_subnet(this, TS_IPV4_ADDR_RANGE);
292 }
293 break;
294 case ID_IPV6_ADDR_SUBNET:
295 if (this->id_data.len == 32)
296 {
297 return get_ts_from_subnet(this, TS_IPV6_ADDR_RANGE);
298 }
299 break;
300 case ID_IPV4_ADDR_RANGE:
301 if (this->id_data.len == 8)
302 {
303 return get_ts_from_range(this, TS_IPV4_ADDR_RANGE);
304 }
305 break;
306 case ID_IPV6_ADDR_RANGE:
307 if (this->id_data.len == 32)
308 {
309 return get_ts_from_range(this, TS_IPV6_ADDR_RANGE);
310 }
311 break;
312 case ID_IPV4_ADDR:
313 if (this->id_data.len == 4)
314 {
315 return get_ts_from_ip(this, TS_IPV4_ADDR_RANGE);
316 }
317 break;
318 case ID_IPV6_ADDR:
319 if (this->id_data.len == 16)
320 {
321 return get_ts_from_ip(this, TS_IPV6_ADDR_RANGE);
322 }
323 break;
324 default:
325 break;
326 }
327 return NULL;
328 }
329
330 METHOD(id_payload_t, get_encoded, chunk_t,
331 private_id_payload_t *this)
332 {
333 u_int16_t port = htons(this->port);
334 return chunk_cat("cccc", chunk_from_thing(this->id_type),
335 chunk_from_thing(this->protocol_id),
336 chunk_from_thing(port), this->id_data);
337 }
338
339 METHOD2(payload_t, id_payload_t, destroy, void,
340 private_id_payload_t *this)
341 {
342 free(this->id_data.ptr);
343 free(this);
344 }
345
346 /*
347 * Described in header.
348 */
349 id_payload_t *id_payload_create(payload_type_t type)
350 {
351 private_id_payload_t *this;
352
353 INIT(this,
354 .public = {
355 .payload_interface = {
356 .verify = _verify,
357 .get_encoding_rules = _get_encoding_rules,
358 .get_header_length = _get_header_length,
359 .get_length = _get_length,
360 .get_next_type = _get_next_type,
361 .set_next_type = _set_next_type,
362 .get_type = _get_type,
363 .destroy = _destroy,
364 },
365 .get_identification = _get_identification,
366 .get_encoded = _get_encoded,
367 .get_ts = _get_ts,
368 .destroy = _destroy,
369 },
370 .next_payload = NO_PAYLOAD,
371 .payload_length = get_header_length(this),
372 .type = type,
373 );
374 return &this->public;
375 }
376
377 /*
378 * Described in header.
379 */
380 id_payload_t *id_payload_create_from_identification(payload_type_t type,
381 identification_t *id)
382 {
383 private_id_payload_t *this;
384
385 this = (private_id_payload_t*)id_payload_create(type);
386 this->id_data = chunk_clone(id->get_encoding(id));
387 this->id_type = id->get_type(id);
388 this->payload_length += this->id_data.len;
389
390 return &this->public;
391 }
392
393 /*
394 * Described in header.
395 */
396 id_payload_t *id_payload_create_from_ts(traffic_selector_t *ts)
397 {
398 private_id_payload_t *this;
399 u_int8_t mask;
400 host_t *net;
401
402 this = (private_id_payload_t*)id_payload_create(ID_V1);
403
404 if (ts->is_host(ts, NULL))
405 {
406 if (ts->get_type(ts) == TS_IPV4_ADDR_RANGE)
407 {
408 this->id_type = ID_IPV4_ADDR;
409 }
410 else
411 {
412 this->id_type = ID_IPV6_ADDR;
413 }
414 this->id_data = chunk_clone(ts->get_from_address(ts));
415 }
416 else if (ts->to_subnet(ts, &net, &mask))
417 {
418 u_int8_t netmask[16], len, byte;
419
420 if (ts->get_type(ts) == TS_IPV4_ADDR_RANGE)
421 {
422 this->id_type = ID_IPV4_ADDR_SUBNET;
423 len = 4;
424 }
425 else
426 {
427 this->id_type = ID_IPV6_ADDR_SUBNET;
428 len = 16;
429 }
430 memset(netmask, 0, sizeof(netmask));
431 for (byte = 0; byte < sizeof(netmask); byte++)
432 {
433 if (mask < 8)
434 {
435 netmask[byte] = 0xFF << (8 - mask);
436 break;
437 }
438 netmask[byte] = 0xFF;
439 mask -= 8;
440 }
441 this->id_data = chunk_cat("cc", net->get_address(net),
442 chunk_create(netmask, len));
443 net->destroy(net);
444 }
445 else
446 {
447 if (ts->get_type(ts) == TS_IPV4_ADDR_RANGE)
448 {
449 this->id_type = ID_IPV4_ADDR_RANGE;
450 }
451 else
452 {
453 this->id_type = ID_IPV6_ADDR_RANGE;
454 }
455 this->id_data = chunk_cat("cc",
456 ts->get_from_address(ts), ts->get_to_address(ts));
457 net->destroy(net);
458 }
459 this->port = ts->get_from_port(ts);
460 this->protocol_id = ts->get_protocol(ts);
461 this->payload_length += this->id_data.len;
462
463 return &this->public;
464 }
465