Added member fields for reserved bits and bytes in all payloads
[strongswan.git] / src / libcharon / encoding / payloads / id_payload.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2007 Tobias Brunner
5 * Copyright (C) 2005 Jan Hutter
6 *
7 * Hochschule fuer Technik Rapperswil
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 */
19
20 #include <stddef.h>
21
22 #include "id_payload.h"
23
24 #include <daemon.h>
25 #include <encoding/payloads/encodings.h>
26
27 typedef struct private_id_payload_t private_id_payload_t;
28
29 /**
30 * Private data of an id_payload_t object.
31 *
32 */
33 struct private_id_payload_t {
34 /**
35 * Public id_payload_t interface.
36 */
37 id_payload_t public;
38
39 /**
40 * one of ID_INITIATOR, ID_RESPONDER
41 */
42 payload_type_t payload_type;
43
44 /**
45 * Next payload type.
46 */
47 u_int8_t next_payload;
48
49 /**
50 * Critical flag.
51 */
52 bool critical;
53
54 /**
55 * Reserved bits
56 */
57 bool reserved_bit[7];
58
59 /**
60 * Reserved bytes
61 */
62 u_int8_t reserved_byte[3];
63
64 /**
65 * Length of this payload.
66 */
67 u_int16_t payload_length;
68
69 /**
70 * Type of the ID Data.
71 */
72 u_int8_t id_type;
73
74 /**
75 * The contained id data value.
76 */
77 chunk_t id_data;
78 };
79
80 /**
81 * Encoding rules to parse or generate a ID payload
82 *
83 * The defined offsets are the positions in a object of type
84 * private_id_payload_t.
85 */
86 encoding_rule_t id_payload_encodings[] = {
87 /* 1 Byte next payload type, stored in the field next_payload */
88 { U_INT_8, offsetof(private_id_payload_t, next_payload) },
89 /* the critical bit */
90 { FLAG, offsetof(private_id_payload_t, critical) },
91 /* 7 Bit reserved bits */
92 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[0]) },
93 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[1]) },
94 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[2]) },
95 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[3]) },
96 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[4]) },
97 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[5]) },
98 { RESERVED_BIT, offsetof(private_id_payload_t, reserved_bit[6]) },
99 /* Length of the whole payload*/
100 { PAYLOAD_LENGTH, offsetof(private_id_payload_t, payload_length) },
101 /* 1 Byte ID type*/
102 { U_INT_8, offsetof(private_id_payload_t, id_type) },
103 /* 3 reserved bytes */
104 { RESERVED_BYTE, offsetof(private_id_payload_t, reserved_byte[0])},
105 { RESERVED_BYTE, offsetof(private_id_payload_t, reserved_byte[1])},
106 { RESERVED_BYTE, offsetof(private_id_payload_t, reserved_byte[2])},
107 /* some id data bytes, length is defined in PAYLOAD_LENGTH */
108 { ID_DATA, offsetof(private_id_payload_t, id_data) }
109 };
110
111 /*
112 1 2 3
113 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
114 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 ! Next Payload !C! RESERVED ! Payload Length !
116 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117 ! ID Type ! RESERVED |
118 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119 ! !
120 ~ Identification Data ~
121 ! !
122 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 */
124
125 METHOD(payload_t, verify, status_t,
126 private_id_payload_t *this)
127 {
128 if (this->id_type == 0 || this->id_type == 4)
129 {
130 /* reserved IDs */
131 DBG1(DBG_ENC, "received ID with reserved type %d", this->id_type);
132 return FAILED;
133 }
134 return SUCCESS;
135 }
136
137 METHOD(payload_t, get_encoding_rules, void,
138 private_id_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
139 {
140 *rules = id_payload_encodings;
141 *rule_count = countof(id_payload_encodings);
142 }
143
144 METHOD(payload_t, get_type, payload_type_t,
145 private_id_payload_t *this)
146 {
147 return this->payload_type;
148 }
149
150 METHOD(payload_t, get_next_type, payload_type_t,
151 private_id_payload_t *this)
152 {
153 return this->next_payload;
154 }
155
156 METHOD(payload_t, set_next_type, void,
157 private_id_payload_t *this, payload_type_t type)
158 {
159 this->next_payload = type;
160 }
161
162 METHOD(payload_t, get_length, size_t,
163 private_id_payload_t *this)
164 {
165 return this->payload_length;
166 }
167
168 METHOD(id_payload_t, get_identification, identification_t*,
169 private_id_payload_t *this)
170 {
171 return identification_create_from_encoding(this->id_type, this->id_data);
172 }
173
174 METHOD2(payload_t, id_payload_t, destroy, void,
175 private_id_payload_t *this)
176 {
177 free(this->id_data.ptr);
178 free(this);
179 }
180
181 /*
182 * Described in header.
183 */
184 id_payload_t *id_payload_create(payload_type_t payload_type)
185 {
186 private_id_payload_t *this;
187
188 INIT(this,
189 .public = {
190 .payload_interface = {
191 .verify = _verify,
192 .get_encoding_rules = _get_encoding_rules,
193 .get_length = _get_length,
194 .get_next_type = _get_next_type,
195 .set_next_type = _set_next_type,
196 .get_type = _get_type,
197 .destroy = _destroy,
198 },
199 .get_identification = _get_identification,
200 .destroy = _destroy,
201 },
202 .next_payload = NO_PAYLOAD,
203 .payload_length = ID_PAYLOAD_HEADER_LENGTH,
204 .payload_type = payload_type,
205 );
206 return &this->public;
207 }
208
209 /*
210 * Described in header.
211 */
212 id_payload_t *id_payload_create_from_identification(payload_type_t payload_type,
213 identification_t *id)
214 {
215 private_id_payload_t *this;
216
217 this = (private_id_payload_t*)id_payload_create(payload_type);
218 this->id_data = chunk_clone(id->get_encoding(id));
219 this->id_type = id->get_type(id);
220 this->payload_length += this->id_data.len;
221
222 return &this->public;
223 }