2 * Copyright (C) 2007 Tobias Brunner
3 * Copyright (C) 2005-2007 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
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>.
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
20 #include <arpa/inet.h>
26 #include "traffic_selector.h"
29 #include <utils/linked_list.h>
30 #include <utils/identification.h>
32 ENUM(ts_type_name
, TS_IPV4_ADDR_RANGE
, TS_IPV6_ADDR_RANGE
,
37 typedef struct private_traffic_selector_t private_traffic_selector_t
;
40 * Private data of an traffic_selector_t object
42 struct private_traffic_selector_t
{
47 traffic_selector_t
public;
55 * IP protocol (UDP, TCP, ICMP, ...)
60 * narrow this traffic selector to hosts external ip
61 * if set, from and to have no meaning until set_address() is called
66 * begin of address range, network order
69 /** dummy char for common address manipulation */
78 * end of address range, network order
81 /** dummy char for common address manipulation */
101 * calculate to "to"-address for the "from" address and a subnet size
103 static void calc_range(private_traffic_selector_t
*this, u_int8_t netbits
)
106 size_t size
= (this->type
== TS_IPV4_ADDR_RANGE
) ?
4 : 16;
108 /* go through the from address, starting at the tail. While we
109 * have not processed the bits belonging to the host, set them to 1 on
110 * the to address. If we reach the bits for the net, copy them from "from". */
111 for (byte
= size
- 1; byte
>=0; byte
--)
116 shift
= (byte
+1) * 8 - netbits
;
125 this->to
[byte
] = this->from
[byte
] | mask
;
130 * calculate to subnet size from "to"- and "from"-address
132 static u_int8_t
calc_netbits(private_traffic_selector_t
*this)
135 size_t size
= (this->type
== TS_IPV4_ADDR_RANGE
) ?
4 : 16;
137 /* go trough all bits of the addresses, begging in the front.
138 * As longer as they equal, the subnet gets larger */
139 for (byte
= 0; byte
< size
; byte
++)
141 for (bit
= 7; bit
>= 0; bit
--)
143 if ((1<<bit
& this->from
[byte
]) != (1<<bit
& this->to
[byte
]))
145 return ((7 - bit
) + (byte
* 8));
149 /* single host, netmask is 32/128 */
154 * internal generic constructor
156 static private_traffic_selector_t
*traffic_selector_create(u_int8_t protocol
, ts_type_t type
, u_int16_t from_port
, u_int16_t to_port
);
159 * output handler in printf()
161 static int print(FILE *stream
, const struct printf_info
*info
,
162 const void *const *args
)
164 private_traffic_selector_t
*this = *((private_traffic_selector_t
**)(args
[0]));
165 linked_list_t
*list
= *((linked_list_t
**)(args
[0]));
166 iterator_t
*iterator
;
167 char addr_str
[INET6_ADDRSTRLEN
] = "";
168 char *serv_proto
= NULL
;
173 u_int32_t from
[4], to
[4];
177 return fprintf(stream
, "(null)");
182 iterator
= list
->create_iterator(list
, TRUE
);
183 while (iterator
->iterate(iterator
, (void**)&this))
185 /* call recursivly */
186 written
+= fprintf(stream
, "%R ", this);
188 iterator
->destroy(iterator
);
192 memset(from
, 0, sizeof(from
));
193 memset(to
, 0xFF, sizeof(to
));
195 memeq(this->from
, from
, this->type
== TS_IPV4_ADDR_RANGE ?
4 : 16) &&
196 memeq(this->to
, to
, this->type
== TS_IPV4_ADDR_RANGE ?
4 : 16))
198 return fprintf(stream
, "dynamic/%d",
199 this->type
== TS_IPV4_ADDR_RANGE ?
32 : 128);
202 if (this->type
== TS_IPV4_ADDR_RANGE
)
204 inet_ntop(AF_INET
, &this->from4
, addr_str
, sizeof(addr_str
));
208 inet_ntop(AF_INET6
, &this->from6
, addr_str
, sizeof(addr_str
));
210 mask
= calc_netbits(this);
212 written
+= fprintf(stream
, "%s/%d", addr_str
, mask
);
214 /* check if we have protocol and/or port selectors */
215 has_proto
= this->protocol
!= 0;
216 has_ports
= !(this->from_port
== 0 && this->to_port
== 0xFFFF);
218 if (!has_proto
&& !has_ports
)
223 written
+= fprintf(stream
, "[");
225 /* build protocol string */
228 struct protoent
*proto
= getprotobynumber(this->protocol
);
232 written
+= fprintf(stream
, "%s", proto
->p_name
);
233 serv_proto
= proto
->p_name
;
237 written
+= fprintf(stream
, "%d", this->protocol
);
241 if (has_proto
&& has_ports
)
243 written
+= fprintf(stream
, "/");
246 /* build port string */
249 if (this->from_port
== this->to_port
)
251 struct servent
*serv
= getservbyport(htons(this->from_port
), serv_proto
);
255 written
+= fprintf(stream
, "%s", serv
->s_name
);
259 written
+= fprintf(stream
, "%d", this->from_port
);
264 written
+= fprintf(stream
, "%d-%d", this->from_port
, this->to_port
);
268 written
+= fprintf(stream
, "]");
274 * arginfo handler for printf() traffic selector
276 static int arginfo(const struct printf_info
*info
, size_t n
, int *argtypes
)
280 argtypes
[0] = PA_POINTER
;
286 * return printf hook functions for a chunk
288 printf_hook_functions_t
traffic_selector_get_printf_hooks()
290 printf_hook_functions_t hooks
= {print
, arginfo
};
296 * implements traffic_selector_t.get_subset
298 static traffic_selector_t
*get_subset(private_traffic_selector_t
*this, private_traffic_selector_t
*other
)
300 if (this->type
== other
->type
&& (this->protocol
== other
->protocol
||
301 this->protocol
== 0 || other
->protocol
== 0))
303 u_int16_t from_port
, to_port
;
307 private_traffic_selector_t
*new_ts
;
309 /* calculate the maximum port range allowed for both */
310 from_port
= max(this->from_port
, other
->from_port
);
311 to_port
= min(this->to_port
, other
->to_port
);
312 if (from_port
> to_port
)
316 /* select protocol, which is not zero */
317 protocol
= max(this->protocol
, other
->protocol
);
321 case TS_IPV4_ADDR_RANGE
:
322 size
= sizeof(this->from4
);
324 case TS_IPV6_ADDR_RANGE
:
325 size
= sizeof(this->from6
);
331 /* get higher from-address */
332 if (memcmp(this->from
, other
->from
, size
) > 0)
340 /* get lower to-address */
341 if (memcmp(this->to
, other
->to
, size
) > 0)
349 /* if "from" > "to", we don't have a match */
350 if (memcmp(from
, to
, size
) > 0)
355 /* we have a match in protocol, port, and address: return it... */
356 new_ts
= traffic_selector_create(protocol
, this->type
, from_port
, to_port
);
357 new_ts
->type
= this->type
;
358 new_ts
->dynamic
= this->dynamic
|| other
->dynamic
;
359 memcpy(new_ts
->from
, from
, size
);
360 memcpy(new_ts
->to
, to
, size
);
362 return &new_ts
->public;
368 * implements traffic_selector_t.equals
370 static bool equals(private_traffic_selector_t
*this, private_traffic_selector_t
*other
)
372 if (this->type
!= other
->type
)
376 if (!(this->from_port
== other
->from_port
&&
377 this->to_port
== other
->to_port
&&
378 this->protocol
== other
->protocol
))
384 case TS_IPV4_ADDR_RANGE
:
385 if (memeq(this->from4
, other
->from4
, sizeof(this->from4
)))
390 case TS_IPV6_ADDR_RANGE
:
391 if (memeq(this->from6
, other
->from6
, sizeof(this->from6
)))
403 * Implements traffic_selector_t.get_from_address.
405 static chunk_t
get_from_address(private_traffic_selector_t
*this)
407 chunk_t from
= chunk_empty
;
411 case TS_IPV4_ADDR_RANGE
:
413 from
.len
= sizeof(this->from4
);
414 from
.ptr
= malloc(from
.len
);
415 memcpy(from
.ptr
, this->from4
, from
.len
);
418 case TS_IPV6_ADDR_RANGE
:
420 from
.len
= sizeof(this->from6
);
421 from
.ptr
= malloc(from
.len
);
422 memcpy(from
.ptr
, this->from6
, from
.len
);
430 * Implements traffic_selector_t.get_to_address.
432 static chunk_t
get_to_address(private_traffic_selector_t
*this)
434 chunk_t to
= chunk_empty
;
438 case TS_IPV4_ADDR_RANGE
:
440 to
.len
= sizeof(this->to4
);
441 to
.ptr
= malloc(to
.len
);
442 memcpy(to
.ptr
, this->to4
, to
.len
);
445 case TS_IPV6_ADDR_RANGE
:
447 to
.len
= sizeof(this->to6
);
448 to
.ptr
= malloc(to
.len
);
449 memcpy(to
.ptr
, this->to6
, to
.len
);
457 * Implements traffic_selector_t.get_from_port.
459 static u_int16_t
get_from_port(private_traffic_selector_t
*this)
461 return this->from_port
;
465 * Implements traffic_selector_t.get_to_port.
467 static u_int16_t
get_to_port(private_traffic_selector_t
*this)
469 return this->to_port
;
473 * Implements traffic_selector_t.get_type.
475 static ts_type_t
get_type(private_traffic_selector_t
*this)
481 * Implements traffic_selector_t.get_protocol.
483 static u_int8_t
get_protocol(private_traffic_selector_t
*this)
485 return this->protocol
;
489 * Implements traffic_selector_t.is_host.
491 static bool is_host(private_traffic_selector_t
*this, host_t
*host
)
496 int family
= host
->get_family(host
);
498 if ((family
== AF_INET
&& this->type
== TS_IPV4_ADDR_RANGE
) ||
499 (family
== AF_INET6
&& this->type
== TS_IPV6_ADDR_RANGE
))
501 addr
= host
->get_address(host
);
502 if (memeq(addr
.ptr
, this->from
, addr
.len
) &&
503 memeq(addr
.ptr
, this->to
, addr
.len
))
511 size_t length
= (this->type
== TS_IPV4_ADDR_RANGE
) ?
4 : 16;
518 if (memeq(this->from
, this->to
, length
))
527 * Implements traffic_selector_t.set_address.
529 static void set_address(private_traffic_selector_t
*this, host_t
*host
)
533 this->type
= host
->get_family(host
) == AF_INET ?
534 TS_IPV4_ADDR_RANGE
: TS_IPV6_ADDR_RANGE
;
536 chunk_t from
= host
->get_address(host
);
537 memcpy(this->from
, from
.ptr
, from
.len
);
538 memcpy(this->to
, from
.ptr
, from
.len
);
543 * Implements traffic_selector_t.is_contained_in.
545 static bool is_contained_in(private_traffic_selector_t
*this,
546 private_traffic_selector_t
*other
)
548 private_traffic_selector_t
*subset
;
549 bool contained_in
= FALSE
;
551 subset
= (private_traffic_selector_t
*)get_subset(this, other
);
555 if (equals(subset
, this))
565 * Implements traffic_selector_t.includes.
567 static bool includes(private_traffic_selector_t
*this, host_t
*host
)
570 int family
= host
->get_family(host
);
572 if ((family
== AF_INET
&& this->type
== TS_IPV4_ADDR_RANGE
) ||
573 (family
== AF_INET6
&& this->type
== TS_IPV6_ADDR_RANGE
))
575 addr
= host
->get_address(host
);
577 return memcmp(this->from
, addr
.ptr
, addr
.len
) <= 0 &&
578 memcmp(this->to
, addr
.ptr
, addr
.len
) >= 0;
585 * Implements traffic_selector_t.clone.
587 static traffic_selector_t
*clone_(private_traffic_selector_t
*this)
589 private_traffic_selector_t
*clone
;
591 clone
= traffic_selector_create(this->protocol
, this->type
,
592 this->from_port
, this->to_port
);
594 clone
->dynamic
= this->dynamic
;
597 case TS_IPV4_ADDR_RANGE
:
599 memcpy(clone
->from4
, this->from4
, sizeof(this->from4
));
600 memcpy(clone
->to4
, this->to4
, sizeof(this->to4
));
601 return &clone
->public;
603 case TS_IPV6_ADDR_RANGE
:
605 memcpy(clone
->from6
, this->from6
, sizeof(this->from6
));
606 memcpy(clone
->to6
, this->to6
, sizeof(this->to6
));
607 return &clone
->public;
612 return &clone
->public;
618 * Implements traffic_selector_t.destroy.
620 static void destroy(private_traffic_selector_t
*this)
628 traffic_selector_t
*traffic_selector_create_from_bytes(u_int8_t protocol
,
630 chunk_t from
, u_int16_t from_port
,
631 chunk_t to
, u_int16_t to_port
)
633 private_traffic_selector_t
*this = traffic_selector_create(protocol
, type
,
638 case TS_IPV4_ADDR_RANGE
:
640 if (from
.len
!= 4 || to
.len
!= 4)
645 memcpy(this->from4
, from
.ptr
, from
.len
);
646 memcpy(this->to4
, to
.ptr
, to
.len
);
649 case TS_IPV6_ADDR_RANGE
:
651 if (from
.len
!= 16 || to
.len
!= 16)
656 memcpy(this->from6
, from
.ptr
, from
.len
);
657 memcpy(this->to6
, to
.ptr
, to
.len
);
666 return (&this->public);
672 traffic_selector_t
*traffic_selector_create_from_subnet(host_t
*net
,
673 u_int8_t netbits
, u_int8_t protocol
, u_int16_t port
)
675 private_traffic_selector_t
*this = traffic_selector_create(protocol
, 0, 0, 65535);
677 switch (net
->get_family(net
))
683 this->type
= TS_IPV4_ADDR_RANGE
;
684 from
= net
->get_address(net
);
685 memcpy(this->from4
, from
.ptr
, from
.len
);
686 if (this->from4
[0] == 0)
688 /* use /0 for 0.0.0.0 */
693 calc_range(this, netbits
);
701 this->type
= TS_IPV6_ADDR_RANGE
;
702 from
= net
->get_address(net
);
703 memcpy(this->from6
, from
.ptr
, from
.len
);
704 if (this->from6
[0] == 0 && this->from6
[1] == 0 &&
705 this->from6
[2] == 0 && this->from6
[3] == 0)
715 calc_range(this, netbits
);
728 this->from_port
= port
;
729 this->to_port
= port
;
732 return (&this->public);
738 traffic_selector_t
*traffic_selector_create_from_string(
739 u_int8_t protocol
, ts_type_t type
,
740 char *from_addr
, u_int16_t from_port
,
741 char *to_addr
, u_int16_t to_port
)
743 private_traffic_selector_t
*this = traffic_selector_create(protocol
, type
,
749 case TS_IPV4_ADDR_RANGE
:
751 if (inet_pton(AF_INET
, from_addr
, (struct in_addr
*)this->from4
) < 0)
756 if (inet_pton(AF_INET
, to_addr
, (struct in_addr
*)this->to4
) < 0)
763 case TS_IPV6_ADDR_RANGE
:
765 if (inet_pton(AF_INET6
, from_addr
, (struct in6_addr
*)this->from6
) < 0)
770 if (inet_pton(AF_INET6
, to_addr
, (struct in6_addr
*)this->to6
) < 0)
778 return (&this->public);
784 traffic_selector_t
*traffic_selector_create_dynamic(u_int8_t protocol
,
785 u_int16_t from_port
, u_int16_t to_port
)
787 private_traffic_selector_t
*this = traffic_selector_create(
788 protocol
, TS_IPV4_ADDR_RANGE
, from_port
, to_port
);
790 memset(this->from6
, 0, sizeof(this->from6
));
791 memset(this->to6
, 0xFF, sizeof(this->to6
));
793 this->dynamic
= TRUE
;
795 return &this->public;
801 static private_traffic_selector_t
*traffic_selector_create(u_int8_t protocol
,
802 ts_type_t type
, u_int16_t from_port
, u_int16_t to_port
)
804 private_traffic_selector_t
*this = malloc_thing(private_traffic_selector_t
);
806 /* public functions */
807 this->public.get_subset
= (traffic_selector_t
*(*)(traffic_selector_t
*,traffic_selector_t
*))get_subset
;
808 this->public.equals
= (bool(*)(traffic_selector_t
*,traffic_selector_t
*))equals
;
809 this->public.get_from_address
= (chunk_t(*)(traffic_selector_t
*))get_from_address
;
810 this->public.get_to_address
= (chunk_t(*)(traffic_selector_t
*))get_to_address
;
811 this->public.get_from_port
= (u_int16_t(*)(traffic_selector_t
*))get_from_port
;
812 this->public.get_to_port
= (u_int16_t(*)(traffic_selector_t
*))get_to_port
;
813 this->public.get_type
= (ts_type_t(*)(traffic_selector_t
*))get_type
;
814 this->public.get_protocol
= (u_int8_t(*)(traffic_selector_t
*))get_protocol
;
815 this->public.is_host
= (bool(*)(traffic_selector_t
*,host_t
*))is_host
;
816 this->public.is_contained_in
= (bool(*)(traffic_selector_t
*,traffic_selector_t
*))is_contained_in
;
817 this->public.includes
= (bool(*)(traffic_selector_t
*,host_t
*))includes
;
818 this->public.set_address
= (void(*)(traffic_selector_t
*,host_t
*))set_address
;
819 this->public.clone
= (traffic_selector_t
*(*)(traffic_selector_t
*))clone_
;
820 this->public.destroy
= (void(*)(traffic_selector_t
*))destroy
;
822 this->from_port
= from_port
;
823 this->to_port
= to_port
;
824 this->protocol
= protocol
;
826 this->dynamic
= FALSE
;