Use vendor id payload for IKEv1 payloads, too
[strongswan.git] / src / libcharon / encoding / payloads / vendor_id_payload.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stddef.h>
19
20 #include "vendor_id_payload.h"
21
22 typedef struct private_vendor_id_payload_t private_vendor_id_payload_t;
23
24 /**
25 * Private data of an vendor_id_payload_t object.
26 */
27 struct private_vendor_id_payload_t {
28
29 /**
30 * Public vendor_id_payload_t interface.
31 */
32 vendor_id_payload_t public;
33
34 /**
35 * Next payload type.
36 */
37 u_int8_t next_payload;
38
39 /**
40 * Critical flag.
41 */
42 bool critical;
43
44 /**
45 * Reserved bits
46 */
47 bool reserved[7];
48
49 /**
50 * Length of this payload.
51 */
52 u_int16_t payload_length;
53
54 /**
55 * The contained data.
56 */
57 chunk_t data;
58
59 /**
60 * Either a IKEv1 or a IKEv2 vendor ID payload
61 */
62 payload_type_t type;
63 };
64
65 /**
66 * Encoding rules to parse or generate a VENDOR ID payload
67 *
68 * The defined offsets are the positions in a object of type
69 * private_vendor_id_payload_t.
70 */
71 encoding_rule_t vendor_id_payload_encodings[] = {
72 /* 1 Byte next payload type, stored in the field next_payload */
73 { U_INT_8, offsetof(private_vendor_id_payload_t, next_payload) },
74 /* the critical bit */
75 { FLAG, offsetof(private_vendor_id_payload_t, critical) },
76 /* 7 Bit reserved bits, nowhere stored */
77 { RESERVED_BIT, offsetof(private_vendor_id_payload_t, reserved[0]) },
78 { RESERVED_BIT, offsetof(private_vendor_id_payload_t, reserved[1]) },
79 { RESERVED_BIT, offsetof(private_vendor_id_payload_t, reserved[2]) },
80 { RESERVED_BIT, offsetof(private_vendor_id_payload_t, reserved[3]) },
81 { RESERVED_BIT, offsetof(private_vendor_id_payload_t, reserved[4]) },
82 { RESERVED_BIT, offsetof(private_vendor_id_payload_t, reserved[5]) },
83 { RESERVED_BIT, offsetof(private_vendor_id_payload_t, reserved[6]) },
84 /* Length of the whole payload*/
85 { PAYLOAD_LENGTH, offsetof(private_vendor_id_payload_t, payload_length)},
86 /* some vendor_id data bytes, length is defined in PAYLOAD_LENGTH */
87 { VID_DATA, offsetof(private_vendor_id_payload_t, data) }
88 };
89
90 /*
91 1 2 3
92 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
93 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 ! Next Payload !C! RESERVED ! Payload Length !
95 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 + !
97 ~ VID Data ~
98 ! !
99 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 */
101
102 METHOD(payload_t, verify, status_t,
103 private_vendor_id_payload_t *this)
104 {
105 return SUCCESS;
106 }
107
108 METHOD(payload_t, get_encoding_rules, void,
109 private_vendor_id_payload_t *this, encoding_rule_t **rules,
110 size_t *rule_count)
111 {
112 *rules = vendor_id_payload_encodings;
113 *rule_count = countof(vendor_id_payload_encodings);
114 }
115
116 METHOD(payload_t, get_type, payload_type_t,
117 private_vendor_id_payload_t *this)
118 {
119 return this->type;
120 }
121
122 METHOD(payload_t, get_next_type, payload_type_t,
123 private_vendor_id_payload_t *this)
124 {
125 return this->next_payload;
126 }
127
128 METHOD(payload_t, set_next_type, void,
129 private_vendor_id_payload_t *this, payload_type_t type)
130 {
131 this->next_payload = type;
132 }
133
134 METHOD(payload_t, get_length, size_t,
135 private_vendor_id_payload_t *this)
136 {
137 return this->payload_length;
138 }
139
140 METHOD(vendor_id_payload_t, get_data, chunk_t,
141 private_vendor_id_payload_t *this)
142 {
143 return this->data;
144 }
145
146 METHOD2(payload_t, vendor_id_payload_t, destroy, void,
147 private_vendor_id_payload_t *this)
148 {
149 free(this->data.ptr);
150 free(this);
151 }
152
153 /*
154 * Described in header
155 */
156 vendor_id_payload_t *vendor_id_payload_create_data(payload_type_t type,
157 chunk_t data)
158 {
159 private_vendor_id_payload_t *this;
160
161 INIT(this,
162 .public = {
163 .payload_interface = {
164 .verify = _verify,
165 .get_encoding_rules = _get_encoding_rules,
166 .get_length = _get_length,
167 .get_next_type = _get_next_type,
168 .set_next_type = _set_next_type,
169 .get_type = _get_type,
170 .destroy = _destroy,
171 },
172 .get_data = _get_data,
173 .destroy = _destroy,
174 },
175 .next_payload = NO_PAYLOAD,
176 .payload_length = VENDOR_ID_PAYLOAD_HEADER_LENGTH + data.len,
177 .data = data,
178 .type = type,
179 );
180 return &this->public;
181 }
182
183 /*
184 * Described in header
185 */
186 vendor_id_payload_t *vendor_id_payload_create(payload_type_t type)
187 {
188 return vendor_id_payload_create_data(type, chunk_empty);
189 }