Don't use unportable htobe64 macro directly
[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 METHOD(id_payload_t, get_ts, traffic_selector_t*,
274 private_id_payload_t *this)
275 {
276 switch (this->id_type)
277 {
278 case ID_IPV4_ADDR_SUBNET:
279 if (this->id_data.len == 8)
280 {
281 return get_ts_from_subnet(this, TS_IPV4_ADDR_RANGE);
282 }
283 break;
284 case ID_IPV6_ADDR_SUBNET:
285 if (this->id_data.len == 32)
286 {
287 return get_ts_from_subnet(this, TS_IPV6_ADDR_RANGE);
288 }
289 break;
290 case ID_IPV4_ADDR_RANGE:
291 if (this->id_data.len == 8)
292 {
293 return get_ts_from_range(this, TS_IPV4_ADDR_RANGE);
294 }
295 break;
296 case ID_IPV6_ADDR_RANGE:
297 if (this->id_data.len == 32)
298 {
299 return get_ts_from_range(this, TS_IPV6_ADDR_RANGE);
300 }
301 break;
302 case ID_IPV4_ADDR:
303 {
304 if (this->id_data.len == 4)
305 {
306 return traffic_selector_create_from_bytes(this->protocol_id,
307 TS_IPV4_ADDR_RANGE, this->id_data, this->port,
308 this->id_data, this->port ?: 65535);
309 }
310 }
311 case ID_IPV6_ADDR:
312 {
313 if (this->id_data.len == 16)
314 {
315 return traffic_selector_create_from_bytes(this->protocol_id,
316 TS_IPV4_ADDR_RANGE, this->id_data, this->port,
317 this->id_data, this->port ?: 65535);
318 }
319 }
320 default:
321 break;
322 }
323 return NULL;
324 }
325
326 METHOD2(payload_t, id_payload_t, destroy, void,
327 private_id_payload_t *this)
328 {
329 free(this->id_data.ptr);
330 free(this);
331 }
332
333 /*
334 * Described in header.
335 */
336 id_payload_t *id_payload_create(payload_type_t type)
337 {
338 private_id_payload_t *this;
339
340 INIT(this,
341 .public = {
342 .payload_interface = {
343 .verify = _verify,
344 .get_encoding_rules = _get_encoding_rules,
345 .get_header_length = _get_header_length,
346 .get_length = _get_length,
347 .get_next_type = _get_next_type,
348 .set_next_type = _set_next_type,
349 .get_type = _get_type,
350 .destroy = _destroy,
351 },
352 .get_identification = _get_identification,
353 .get_ts = _get_ts,
354 .destroy = _destroy,
355 },
356 .next_payload = NO_PAYLOAD,
357 .payload_length = get_header_length(this),
358 .type = type,
359 );
360 return &this->public;
361 }
362
363 /*
364 * Described in header.
365 */
366 id_payload_t *id_payload_create_from_identification(payload_type_t type,
367 identification_t *id)
368 {
369 private_id_payload_t *this;
370
371 this = (private_id_payload_t*)id_payload_create(type);
372 this->id_data = chunk_clone(id->get_encoding(id));
373 this->id_type = id->get_type(id);
374 this->payload_length += this->id_data.len;
375
376 return &this->public;
377 }
378
379 /*
380 * Described in header.
381 */
382 id_payload_t *id_payload_create_from_ts(traffic_selector_t *ts)
383 {
384 private_id_payload_t *this;
385 u_int8_t mask;
386 host_t *net;
387
388 this = (private_id_payload_t*)id_payload_create(ID_V1);
389
390 if (ts->to_subnet(ts, &net, &mask))
391 {
392 u_int8_t netmask[16], len, byte;
393
394 if (ts->get_type(ts) == TS_IPV4_ADDR_RANGE)
395 {
396 this->id_type = ID_IPV4_ADDR_SUBNET;
397 len = 4;
398 }
399 else
400 {
401 this->id_type = ID_IPV6_ADDR_SUBNET;
402 len = 16;
403 }
404 memset(netmask, 0, sizeof(netmask));
405 for (byte = 0; byte < sizeof(netmask); byte++)
406 {
407 if (mask < 8)
408 {
409 netmask[byte] = 0xFF << (8 - mask);
410 break;
411 }
412 netmask[byte] = 0xFF;
413 mask -= 8;
414 }
415 this->id_data = chunk_cat("cc", net->get_address(net),
416 chunk_create(netmask, len));
417 }
418 else
419 {
420 if (ts->get_type(ts) == TS_IPV4_ADDR_RANGE)
421 {
422 this->id_type = ID_IPV4_ADDR_RANGE;
423 }
424 else
425 {
426 this->id_type = ID_IPV6_ADDR_RANGE;
427 }
428 this->id_data = chunk_cat("cc",
429 ts->get_from_address(ts), ts->get_to_address(ts));
430 }
431 this->port = ts->get_from_port(ts);
432 this->protocol_id = ts->get_protocol(ts);
433 this->payload_length += this->id_data.len;
434
435 net->destroy(net);
436
437 return &this->public;
438 }