Added missing break;s when converting ID_IP_ADDRESS types to ts, extracted function
[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 METHOD2(payload_t, id_payload_t, destroy, void,
331 private_id_payload_t *this)
332 {
333 free(this->id_data.ptr);
334 free(this);
335 }
336
337 /*
338 * Described in header.
339 */
340 id_payload_t *id_payload_create(payload_type_t type)
341 {
342 private_id_payload_t *this;
343
344 INIT(this,
345 .public = {
346 .payload_interface = {
347 .verify = _verify,
348 .get_encoding_rules = _get_encoding_rules,
349 .get_header_length = _get_header_length,
350 .get_length = _get_length,
351 .get_next_type = _get_next_type,
352 .set_next_type = _set_next_type,
353 .get_type = _get_type,
354 .destroy = _destroy,
355 },
356 .get_identification = _get_identification,
357 .get_ts = _get_ts,
358 .destroy = _destroy,
359 },
360 .next_payload = NO_PAYLOAD,
361 .payload_length = get_header_length(this),
362 .type = type,
363 );
364 return &this->public;
365 }
366
367 /*
368 * Described in header.
369 */
370 id_payload_t *id_payload_create_from_identification(payload_type_t type,
371 identification_t *id)
372 {
373 private_id_payload_t *this;
374
375 this = (private_id_payload_t*)id_payload_create(type);
376 this->id_data = chunk_clone(id->get_encoding(id));
377 this->id_type = id->get_type(id);
378 this->payload_length += this->id_data.len;
379
380 return &this->public;
381 }
382
383 /*
384 * Described in header.
385 */
386 id_payload_t *id_payload_create_from_ts(traffic_selector_t *ts)
387 {
388 private_id_payload_t *this;
389 u_int8_t mask;
390 host_t *net;
391
392 this = (private_id_payload_t*)id_payload_create(ID_V1);
393
394 if (ts->to_subnet(ts, &net, &mask))
395 {
396 u_int8_t netmask[16], len, byte;
397
398 if (ts->get_type(ts) == TS_IPV4_ADDR_RANGE)
399 {
400 this->id_type = ID_IPV4_ADDR_SUBNET;
401 len = 4;
402 }
403 else
404 {
405 this->id_type = ID_IPV6_ADDR_SUBNET;
406 len = 16;
407 }
408 memset(netmask, 0, sizeof(netmask));
409 for (byte = 0; byte < sizeof(netmask); byte++)
410 {
411 if (mask < 8)
412 {
413 netmask[byte] = 0xFF << (8 - mask);
414 break;
415 }
416 netmask[byte] = 0xFF;
417 mask -= 8;
418 }
419 this->id_data = chunk_cat("cc", net->get_address(net),
420 chunk_create(netmask, len));
421 }
422 else
423 {
424 if (ts->get_type(ts) == TS_IPV4_ADDR_RANGE)
425 {
426 this->id_type = ID_IPV4_ADDR_RANGE;
427 }
428 else
429 {
430 this->id_type = ID_IPV6_ADDR_RANGE;
431 }
432 this->id_data = chunk_cat("cc",
433 ts->get_from_address(ts), ts->get_to_address(ts));
434 }
435 this->port = ts->get_from_port(ts);
436 this->protocol_id = ts->get_protocol(ts);
437 this->payload_length += this->id_data.len;
438
439 net->destroy(net);
440
441 return &this->public;
442 }