18c19aec0dc3bb069aa39e4b19de8f3e4f90086e
[strongswan.git] / src / libimcv / ietf / ietf_attr_fwd_enabled.c
1 /*
2 * Copyright (C) 2012-2014 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "ietf_attr_fwd_enabled.h"
17
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <bio/bio_writer.h>
20 #include <bio/bio_reader.h>
21 #include <utils/debug.h>
22
23 typedef struct private_ietf_attr_fwd_enabled_t private_ietf_attr_fwd_enabled_t;
24
25 /**
26 * PA-TNC Forwarding Enabled type (see section 4.2.11 of RFC 5792)
27 *
28 * 1 2 3
29 * 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
30 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31 * | Forwarding Enabled |
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 */
34
35 #define FORWARDING_ENABLED_SIZE 4
36
37 /**
38 * Private data of an ietf_attr_fwd_enabled_t object.
39 */
40 struct private_ietf_attr_fwd_enabled_t {
41
42 /**
43 * Public members of ietf_attr_fwd_enabled_t
44 */
45 ietf_attr_fwd_enabled_t public;
46
47 /**
48 * Vendor-specific attribute type
49 */
50 pen_type_t type;
51
52 /**
53 * Length of attribute value
54 */
55 size_t length;
56
57 /**
58 * Attribute value or segment
59 */
60 chunk_t value;
61
62 /**
63 * Noskip flag
64 */
65 bool noskip_flag;
66
67 /**
68 * Forwarding Enabled status
69 */
70 os_fwd_status_t fwd_status;
71
72 /**
73 * Reference count
74 */
75 refcount_t ref;
76 };
77
78 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
79 private_ietf_attr_fwd_enabled_t *this)
80 {
81 return this->type;
82 }
83
84 METHOD(pa_tnc_attr_t, get_value, chunk_t,
85 private_ietf_attr_fwd_enabled_t *this)
86 {
87 return this->value;
88 }
89
90 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
91 private_ietf_attr_fwd_enabled_t *this)
92 {
93 return this->noskip_flag;
94 }
95
96 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
97 private_ietf_attr_fwd_enabled_t *this, bool noskip)
98 {
99 this->noskip_flag = noskip;
100 }
101
102 METHOD(pa_tnc_attr_t, build, void,
103 private_ietf_attr_fwd_enabled_t *this)
104 {
105 bio_writer_t *writer;
106
107 if (this->value.ptr)
108 {
109 return;
110 }
111 writer = bio_writer_create(FORWARDING_ENABLED_SIZE);
112 writer->write_uint32(writer, this->fwd_status);
113
114 this->value = writer->extract_buf(writer);
115 this->length = this->value.len;
116 writer->destroy(writer);
117 }
118
119 METHOD(pa_tnc_attr_t, process, status_t,
120 private_ietf_attr_fwd_enabled_t *this, u_int32_t *offset)
121 {
122 bio_reader_t *reader;
123 u_int32_t fwd_status;
124
125 *offset = 0;
126
127 if (this->value.len < this->length)
128 {
129 return NEED_MORE;
130 }
131 if (this->value.len != FORWARDING_ENABLED_SIZE)
132 {
133 DBG1(DBG_TNC, "incorrect size for IETF forwarding enabled attribute");
134 return FAILED;
135 }
136 reader = bio_reader_create(this->value);
137 reader->read_uint32(reader, &fwd_status);
138 reader->destroy(reader);
139
140 if (fwd_status > OS_FWD_UNKNOWN)
141 {
142 DBG1(DBG_TNC, "IETF forwarding enabled field has unknown value %u",
143 fwd_status);
144 return FAILED;
145 }
146 this->fwd_status = fwd_status;
147
148 return SUCCESS;
149 }
150
151 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
152 private_ietf_attr_fwd_enabled_t *this)
153 {
154 ref_get(&this->ref);
155 return &this->public.pa_tnc_attribute;
156 }
157
158 METHOD(pa_tnc_attr_t, destroy, void,
159 private_ietf_attr_fwd_enabled_t *this)
160 {
161 if (ref_put(&this->ref))
162 {
163 free(this->value.ptr);
164 free(this);
165 }
166 }
167
168 METHOD(ietf_attr_fwd_enabled_t, get_status, os_fwd_status_t,
169 private_ietf_attr_fwd_enabled_t *this)
170 {
171 return this->fwd_status;
172 }
173
174 /**
175 * Described in header.
176 */
177 pa_tnc_attr_t *ietf_attr_fwd_enabled_create(os_fwd_status_t fwd_status)
178 {
179 private_ietf_attr_fwd_enabled_t *this;
180
181 INIT(this,
182 .public = {
183 .pa_tnc_attribute = {
184 .get_type = _get_type,
185 .get_value = _get_value,
186 .get_noskip_flag = _get_noskip_flag,
187 .set_noskip_flag = _set_noskip_flag,
188 .build = _build,
189 .process = _process,
190 .get_ref = _get_ref,
191 .destroy = _destroy,
192 },
193 .get_status = _get_status,
194 },
195 .type = { PEN_IETF, IETF_ATTR_FORWARDING_ENABLED },
196 .fwd_status = fwd_status,
197 .ref = 1,
198 );
199
200 return &this->public.pa_tnc_attribute;
201 }
202
203 /**
204 * Described in header.
205 */
206 pa_tnc_attr_t *ietf_attr_fwd_enabled_create_from_data(size_t length,
207 chunk_t data)
208 {
209 private_ietf_attr_fwd_enabled_t *this;
210
211 INIT(this,
212 .public = {
213 .pa_tnc_attribute = {
214 .get_type = _get_type,
215 .get_value = _get_value,
216 .get_noskip_flag = _get_noskip_flag,
217 .set_noskip_flag = _set_noskip_flag,
218 .build = _build,
219 .process = _process,
220 .get_ref = _get_ref,
221 .destroy = _destroy,
222 },
223 .get_status = _get_status,
224 },
225 .type = { PEN_IETF, IETF_ATTR_FORWARDING_ENABLED },
226 .length = length,
227 .value = chunk_clone(data),
228 .ref = 1,
229 );
230
231 return &this->public.pa_tnc_attribute;
232 }
233