a002a8f21ab28acec79644c93a0acd08df2d2395
[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 PLV2_ID_INITIATOR, PLV2_ID_RESPONDER, IDv1 and PLV1_NAT_OA
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 == PLV1_NAT_OA || this->type == PLV1_NAT_OA_DRAFT_00_03) &&
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 == PLV1_ID ||
199 this->type == PLV1_NAT_OA || this->type == PLV1_NAT_OA_DRAFT_00_03)
200 {
201 *rules = encodings_v1;
202 return countof(encodings_v1);
203 }
204 *rules = encodings_v2;
205 return countof(encodings_v2);
206 }
207
208 METHOD(payload_t, get_header_length, int,
209 private_id_payload_t *this)
210 {
211 return 8;
212 }
213
214 METHOD(payload_t, get_type, payload_type_t,
215 private_id_payload_t *this)
216 {
217 return this->type;
218 }
219
220 METHOD(payload_t, get_next_type, payload_type_t,
221 private_id_payload_t *this)
222 {
223 return this->next_payload;
224 }
225
226 METHOD(payload_t, set_next_type, void,
227 private_id_payload_t *this, payload_type_t type)
228 {
229 this->next_payload = type;
230 }
231
232 METHOD(payload_t, get_length, size_t,
233 private_id_payload_t *this)
234 {
235 return this->payload_length;
236 }
237
238 METHOD(id_payload_t, get_identification, identification_t*,
239 private_id_payload_t *this)
240 {
241 return identification_create_from_encoding(this->id_type, this->id_data);
242 }
243
244 /**
245 * Create a traffic selector from an range ID
246 */
247 static traffic_selector_t *get_ts_from_range(private_id_payload_t *this,
248 ts_type_t type)
249 {
250 return traffic_selector_create_from_bytes(this->protocol_id, type,
251 chunk_create(this->id_data.ptr, this->id_data.len / 2), this->port,
252 chunk_skip(this->id_data, this->id_data.len / 2), this->port ?: 65535);
253 }
254
255 /**
256 * Create a traffic selector from an subnet ID
257 */
258 static traffic_selector_t *get_ts_from_subnet(private_id_payload_t *this,
259 ts_type_t type)
260 {
261 chunk_t net, netmask;
262 int i;
263
264 net = chunk_create(this->id_data.ptr, this->id_data.len / 2);
265 netmask = chunk_skip(this->id_data, this->id_data.len / 2);
266 for (i = 0; i < net.len; i++)
267 {
268 netmask.ptr[i] = (netmask.ptr[i] ^ 0xFF) | net.ptr[i];
269 }
270 return traffic_selector_create_from_bytes(this->protocol_id, type,
271 net, this->port, netmask, this->port ?: 65535);
272 }
273
274 /**
275 * Create a traffic selector from an IP ID
276 */
277 static traffic_selector_t *get_ts_from_ip(private_id_payload_t *this,
278 ts_type_t type)
279 {
280 return traffic_selector_create_from_bytes(this->protocol_id, type,
281 this->id_data, this->port, this->id_data, this->port ?: 65535);
282 }
283
284 METHOD(id_payload_t, get_ts, traffic_selector_t*,
285 private_id_payload_t *this)
286 {
287 switch (this->id_type)
288 {
289 case ID_IPV4_ADDR_SUBNET:
290 if (this->id_data.len == 8)
291 {
292 return get_ts_from_subnet(this, TS_IPV4_ADDR_RANGE);
293 }
294 break;
295 case ID_IPV6_ADDR_SUBNET:
296 if (this->id_data.len == 32)
297 {
298 return get_ts_from_subnet(this, TS_IPV6_ADDR_RANGE);
299 }
300 break;
301 case ID_IPV4_ADDR_RANGE:
302 if (this->id_data.len == 8)
303 {
304 return get_ts_from_range(this, TS_IPV4_ADDR_RANGE);
305 }
306 break;
307 case ID_IPV6_ADDR_RANGE:
308 if (this->id_data.len == 32)
309 {
310 return get_ts_from_range(this, TS_IPV6_ADDR_RANGE);
311 }
312 break;
313 case ID_IPV4_ADDR:
314 if (this->id_data.len == 4)
315 {
316 return get_ts_from_ip(this, TS_IPV4_ADDR_RANGE);
317 }
318 break;
319 case ID_IPV6_ADDR:
320 if (this->id_data.len == 16)
321 {
322 return get_ts_from_ip(this, TS_IPV6_ADDR_RANGE);
323 }
324 break;
325 default:
326 break;
327 }
328 return NULL;
329 }
330
331 METHOD(id_payload_t, get_encoded, chunk_t,
332 private_id_payload_t *this)
333 {
334 u_int16_t port = htons(this->port);
335 return chunk_cat("cccc", chunk_from_thing(this->id_type),
336 chunk_from_thing(this->protocol_id),
337 chunk_from_thing(port), this->id_data);
338 }
339
340 METHOD2(payload_t, id_payload_t, destroy, void,
341 private_id_payload_t *this)
342 {
343 free(this->id_data.ptr);
344 free(this);
345 }
346
347 /*
348 * Described in header.
349 */
350 id_payload_t *id_payload_create(payload_type_t type)
351 {
352 private_id_payload_t *this;
353
354 INIT(this,
355 .public = {
356 .payload_interface = {
357 .verify = _verify,
358 .get_encoding_rules = _get_encoding_rules,
359 .get_header_length = _get_header_length,
360 .get_length = _get_length,
361 .get_next_type = _get_next_type,
362 .set_next_type = _set_next_type,
363 .get_type = _get_type,
364 .destroy = _destroy,
365 },
366 .get_identification = _get_identification,
367 .get_encoded = _get_encoded,
368 .get_ts = _get_ts,
369 .destroy = _destroy,
370 },
371 .next_payload = PL_NONE,
372 .payload_length = get_header_length(this),
373 .type = type,
374 );
375 return &this->public;
376 }
377
378 /*
379 * Described in header.
380 */
381 id_payload_t *id_payload_create_from_identification(payload_type_t type,
382 identification_t *id)
383 {
384 private_id_payload_t *this;
385
386 this = (private_id_payload_t*)id_payload_create(type);
387 this->id_data = chunk_clone(id->get_encoding(id));
388 this->id_type = id->get_type(id);
389 this->payload_length += this->id_data.len;
390
391 return &this->public;
392 }
393
394 /*
395 * Described in header.
396 */
397 id_payload_t *id_payload_create_from_ts(traffic_selector_t *ts)
398 {
399 private_id_payload_t *this;
400 u_int8_t mask;
401 host_t *net;
402
403 this = (private_id_payload_t*)id_payload_create(PLV1_ID);
404
405 if (ts->is_host(ts, NULL))
406 {
407 if (ts->get_type(ts) == TS_IPV4_ADDR_RANGE)
408 {
409 this->id_type = ID_IPV4_ADDR;
410 }
411 else
412 {
413 this->id_type = ID_IPV6_ADDR;
414 }
415 this->id_data = chunk_clone(ts->get_from_address(ts));
416 }
417 else if (ts->to_subnet(ts, &net, &mask))
418 {
419 u_int8_t netmask[16], len, byte;
420
421 if (ts->get_type(ts) == TS_IPV4_ADDR_RANGE)
422 {
423 this->id_type = ID_IPV4_ADDR_SUBNET;
424 len = 4;
425 }
426 else
427 {
428 this->id_type = ID_IPV6_ADDR_SUBNET;
429 len = 16;
430 }
431 memset(netmask, 0, sizeof(netmask));
432 for (byte = 0; byte < sizeof(netmask); byte++)
433 {
434 if (mask < 8)
435 {
436 netmask[byte] = 0xFF << (8 - mask);
437 break;
438 }
439 netmask[byte] = 0xFF;
440 mask -= 8;
441 }
442 this->id_data = chunk_cat("cc", net->get_address(net),
443 chunk_create(netmask, len));
444 net->destroy(net);
445 }
446 else
447 {
448 if (ts->get_type(ts) == TS_IPV4_ADDR_RANGE)
449 {
450 this->id_type = ID_IPV4_ADDR_RANGE;
451 }
452 else
453 {
454 this->id_type = ID_IPV6_ADDR_RANGE;
455 }
456 this->id_data = chunk_cat("cc",
457 ts->get_from_address(ts), ts->get_to_address(ts));
458 net->destroy(net);
459 }
460 this->port = ts->get_from_port(ts);
461 this->protocol_id = ts->get_protocol(ts);
462 this->payload_length += this->id_data.len;
463
464 return &this->public;
465 }
466