2 * Copyright (C) 2011-2014 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include "ietf_attr_port_filter.h"
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <bio/bio_writer.h>
20 #include <bio/bio_reader.h>
21 #include <collections/linked_list.h>
22 #include <utils/debug.h>
25 typedef struct private_ietf_attr_port_filter_t private_ietf_attr_port_filter_t
;
26 typedef struct port_entry_t port_entry_t
;
38 * PA-TNC Port Filter Type (see section 4.2.6 of RFC 5792)
41 * 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
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | Reserved |B| Protocol | Port Number |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | Reserved |B| Protocol | Port Number |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 #define PORT_FILTER_ENTRY_SIZE 4
52 * Private data of an ietf_attr_port_filter_t object.
54 struct private_ietf_attr_port_filter_t
{
57 * Public members of ietf_attr_port_filter_t
59 ietf_attr_port_filter_t
public;
62 * Vendor-specific attribute type
67 * Length of attribute value
72 * Attribute value or segment
82 * List of Port Filter entries
92 METHOD(pa_tnc_attr_t
, get_type
, pen_type_t
,
93 private_ietf_attr_port_filter_t
*this)
98 METHOD(pa_tnc_attr_t
, get_value
, chunk_t
,
99 private_ietf_attr_port_filter_t
*this)
104 METHOD(pa_tnc_attr_t
, get_noskip_flag
, bool,
105 private_ietf_attr_port_filter_t
*this)
107 return this->noskip_flag
;
110 METHOD(pa_tnc_attr_t
, set_noskip_flag
,void,
111 private_ietf_attr_port_filter_t
*this, bool noskip
)
113 this->noskip_flag
= noskip
;
116 METHOD(pa_tnc_attr_t
, build
, void,
117 private_ietf_attr_port_filter_t
*this)
119 bio_writer_t
*writer
;
120 enumerator_t
*enumerator
;
127 writer
= bio_writer_create(this->ports
->get_count(this->ports
) *
128 PORT_FILTER_ENTRY_SIZE
);
130 enumerator
= this->ports
->create_enumerator(this->ports
);
131 while (enumerator
->enumerate(enumerator
, &entry
))
133 writer
->write_uint8 (writer
, entry
->blocked ?
0x01 : 0x00);
134 writer
->write_uint8 (writer
, entry
->protocol
);
135 writer
->write_uint16(writer
, entry
->port
);
137 enumerator
->destroy(enumerator
);
139 this->value
= writer
->extract_buf(writer
);
140 this->length
= this->value
.len
;
141 writer
->destroy(writer
);
144 METHOD(pa_tnc_attr_t
, process
, status_t
,
145 private_ietf_attr_port_filter_t
*this, u_int32_t
*offset
)
147 bio_reader_t
*reader
;
153 if (this->value
.len
< this->length
)
157 if (this->value
.len
% PORT_FILTER_ENTRY_SIZE
)
159 DBG1(DBG_TNC
, "ietf port filter attribute value is not a multiple of %d",
160 PORT_FILTER_ENTRY_SIZE
);
163 reader
= bio_reader_create(this->value
);
165 while (reader
->remaining(reader
))
167 entry
= malloc_thing(port_entry_t
);
168 reader
->read_uint8 (reader
, &blocked
);
169 entry
->blocked
= blocked
& 0x01;
170 reader
->read_uint8 (reader
, &entry
->protocol
);
171 reader
->read_uint16(reader
, &entry
->port
);
172 this->ports
->insert_last(this->ports
, entry
);
174 reader
->destroy(reader
);
179 METHOD(pa_tnc_attr_t
, add_segment
, void,
180 private_ietf_attr_port_filter_t
*this, chunk_t segment
)
182 this->value
= chunk_cat("mc", this->value
, segment
);
185 METHOD(pa_tnc_attr_t
, get_ref
, pa_tnc_attr_t
*,
186 private_ietf_attr_port_filter_t
*this)
189 return &this->public.pa_tnc_attribute
;
192 METHOD(pa_tnc_attr_t
, destroy
, void,
193 private_ietf_attr_port_filter_t
*this)
195 if (ref_put(&this->ref
))
197 this->ports
->destroy_function(this->ports
, free
);
198 free(this->value
.ptr
);
203 METHOD(ietf_attr_port_filter_t
, add_port
, void,
204 private_ietf_attr_port_filter_t
*this, bool blocked
, u_int8_t protocol
,
209 entry
= malloc_thing(port_entry_t
);
210 entry
->blocked
= blocked
;
211 entry
->protocol
= protocol
;
213 this->ports
->insert_last(this->ports
, entry
);
217 * Enumerate port filter entries
219 static bool port_filter(void *null
, port_entry_t
**entry
,
220 bool *blocked
, void *i2
, u_int8_t
*protocol
, void *i3
,
223 *blocked
= (*entry
)->blocked
;
224 *protocol
= (*entry
)->protocol
;
225 *port
= (*entry
)->port
;
229 METHOD(ietf_attr_port_filter_t
, create_port_enumerator
, enumerator_t
*,
230 private_ietf_attr_port_filter_t
*this)
232 return enumerator_create_filter(this->ports
->create_enumerator(this->ports
),
233 (void*)port_filter
, NULL
, NULL
);
237 * Described in header.
239 pa_tnc_attr_t
*ietf_attr_port_filter_create(void)
241 private_ietf_attr_port_filter_t
*this;
245 .pa_tnc_attribute
= {
246 .get_type
= _get_type
,
247 .get_value
= _get_value
,
248 .get_noskip_flag
= _get_noskip_flag
,
249 .set_noskip_flag
= _set_noskip_flag
,
252 .add_segment
= _add_segment
,
256 .add_port
= _add_port
,
257 .create_port_enumerator
= _create_port_enumerator
,
259 .type
= { PEN_IETF
, IETF_ATTR_PORT_FILTER
},
260 .ports
= linked_list_create(),
264 return &this->public.pa_tnc_attribute
;
268 * Described in header.
270 pa_tnc_attr_t
*ietf_attr_port_filter_create_from_data(size_t length
,
273 private_ietf_attr_port_filter_t
*this;
277 .pa_tnc_attribute
= {
278 .get_type
= _get_type
,
279 .get_value
= _get_value
,
280 .get_noskip_flag
= _get_noskip_flag
,
281 .set_noskip_flag
= _set_noskip_flag
,
284 .add_segment
= _add_segment
,
288 .add_port
= _add_port
,
289 .create_port_enumerator
= _create_port_enumerator
,
291 .type
= {PEN_IETF
, IETF_ATTR_PORT_FILTER
},
293 .value
= chunk_clone(data
),
294 .ports
= linked_list_create(),
298 return &this->public.pa_tnc_attribute
;