2 * Copyright (C) 2007-2013 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
18 #include <arpa/inet.h>
23 #include "traffic_selector.h"
25 #include <collections/linked_list.h>
26 #include <utils/identification.h>
27 #include <utils/debug.h>
29 #define NON_SUBNET_ADDRESS_RANGE 255
31 ENUM(ts_type_name
, TS_IPV4_ADDR_RANGE
, TS_IPV6_ADDR_RANGE
,
36 typedef struct private_traffic_selector_t private_traffic_selector_t
;
39 * Private data of an traffic_selector_t object
41 struct private_traffic_selector_t
{
46 traffic_selector_t
public;
54 * IP protocol (UDP, TCP, ICMP, ...)
59 * narrow this traffic selector to hosts external ip
60 * if set, from and to have no meaning until set_address() is called
65 * subnet size in CIDR notation, 255 means a non-subnet address range
70 * begin of address range, network order
73 /** dummy char for common address manipulation */
82 * end of address range, network order
85 /** dummy char for common address manipulation */
105 * calculate the "to"-address for the "from" address and a subnet size
107 static void calc_range(private_traffic_selector_t
*this, u_int8_t netbits
)
113 this->netbits
= netbits
;
115 len
= (this->type
== TS_IPV4_ADDR_RANGE
) ? 4 : 16;
116 bytes
= (netbits
+ 7)/8;
117 bits
= (bytes
* 8) - netbits
;
118 mask
= bits
? (1 << bits
) - 1 : 0;
120 memcpy(this->to
, this->from
, bytes
);
121 memset(this->from
+ bytes
, 0x00, len
- bytes
);
122 memset(this->to
+ bytes
, 0xff, len
- bytes
);
123 this->from
[bytes
-1] &= ~mask
;
124 this->to
[bytes
-1] |= mask
;
128 * calculate the subnet size from the "to" and "from" addresses
130 static u_int8_t
calc_netbits(private_traffic_selector_t
*this)
134 size_t size
= (this->type
== TS_IPV4_ADDR_RANGE
) ? 4 : 16;
137 /* a perfect match results in a single address with a /32 or /128 netmask */
138 netbits
= (size
* 8);
139 this->netbits
= netbits
;
141 /* go through all bits of the addresses, beginning in the front.
142 * as long as they are equal, the subnet gets larger
144 for (byte
= 0; byte
< size
; byte
++)
146 for (bit
= 7; bit
>= 0; bit
--)
148 u_int8_t bitmask
= 1 << bit
;
152 if ((bitmask
& this->from
[byte
]) != (bitmask
& this->to
[byte
]))
154 /* store the common prefix which might be a true subnet */
155 netbits
= (7 - bit
) + (byte
* 8);
156 this->netbits
= netbits
;
162 if ((bitmask
& this->from
[byte
]) || !(bitmask
& this->to
[byte
]))
164 this->netbits
= NON_SUBNET_ADDRESS_RANGE
;
165 return netbits
; /* return a pseudo subnet */
171 return netbits
; /* return a true subnet */
175 * internal generic constructor
177 static private_traffic_selector_t
*traffic_selector_create(u_int8_t protocol
,
178 ts_type_t type
, u_int16_t from_port
, u_int16_t to_port
);
181 * Check if TS contains "opaque" ports
183 static bool is_opaque(private_traffic_selector_t
*this)
185 return this->from_port
== 0xffff && this->to_port
== 0;
189 * Check if TS contains "any" ports
191 static bool is_any(private_traffic_selector_t
*this)
193 return this->from_port
== 0 && this->to_port
== 0xffff;
197 * Print ICMP/ICMPv6 type and code
199 static int print_icmp(printf_hook_data_t
*data
, u_int16_t port
)
203 type
= traffic_selector_icmp_type(port
);
204 code
= traffic_selector_icmp_code(port
);
207 return print_in_hook(data
, "%d(%d)", type
, code
);
209 return print_in_hook(data
, "%d", type
);
213 * Described in header.
215 int traffic_selector_printf_hook(printf_hook_data_t
*data
,
216 printf_hook_spec_t
*spec
, const void *const *args
)
218 private_traffic_selector_t
*this = *((private_traffic_selector_t
**)(args
[0]));
219 linked_list_t
*list
= *((linked_list_t
**)(args
[0]));
220 enumerator_t
*enumerator
;
221 char from_str
[INET6_ADDRSTRLEN
] = "";
222 char to_str
[INET6_ADDRSTRLEN
] = "";
223 char *serv_proto
= NULL
;
227 u_int32_t from
[4], to
[4];
231 return print_in_hook(data
, "(null)");
236 enumerator
= list
->create_enumerator(list
);
237 while (enumerator
->enumerate(enumerator
, (void**)&this))
239 /* call recursivly */
240 written
+= print_in_hook(data
, "%R ", this);
242 enumerator
->destroy(enumerator
);
246 memset(from
, 0, sizeof(from
));
247 memset(to
, 0xFF, sizeof(to
));
249 memeq(this->from
, from
, this->type
== TS_IPV4_ADDR_RANGE
? 4 : 16) &&
250 memeq(this->to
, to
, this->type
== TS_IPV4_ADDR_RANGE
? 4 : 16))
252 written
+= print_in_hook(data
, "dynamic");
256 if (this->type
== TS_IPV4_ADDR_RANGE
)
258 inet_ntop(AF_INET
, &this->from4
, from_str
, sizeof(from_str
));
262 inet_ntop(AF_INET6
, &this->from6
, from_str
, sizeof(from_str
));
264 if (this->netbits
== NON_SUBNET_ADDRESS_RANGE
)
266 if (this->type
== TS_IPV4_ADDR_RANGE
)
268 inet_ntop(AF_INET
, &this->to4
, to_str
, sizeof(to_str
));
272 inet_ntop(AF_INET6
, &this->to6
, to_str
, sizeof(to_str
));
274 written
+= print_in_hook(data
, "%s..%s", from_str
, to_str
);
278 written
+= print_in_hook(data
, "%s/%d", from_str
, this->netbits
);
282 /* check if we have protocol and/or port selectors */
283 has_proto
= this->protocol
!= 0;
284 has_ports
= !is_any(this);
286 if (!has_proto
&& !has_ports
)
291 written
+= print_in_hook(data
, "[");
293 /* build protocol string */
296 struct protoent
*proto
= getprotobynumber(this->protocol
);
300 written
+= print_in_hook(data
, "%s", proto
->p_name
);
301 serv_proto
= proto
->p_name
;
305 written
+= print_in_hook(data
, "%d", this->protocol
);
309 if (has_proto
&& has_ports
)
311 written
+= print_in_hook(data
, "/");
314 /* build port string */
317 if (this->from_port
== this->to_port
)
319 struct servent
*serv
;
321 if (this->protocol
== IPPROTO_ICMP
||
322 this->protocol
== IPPROTO_ICMPV6
)
324 written
+= print_icmp(data
, this->from_port
);
328 serv
= getservbyport(htons(this->from_port
), serv_proto
);
331 written
+= print_in_hook(data
, "%s", serv
->s_name
);
335 written
+= print_in_hook(data
, "%d", this->from_port
);
339 else if (is_opaque(this))
341 written
+= print_in_hook(data
, "OPAQUE");
343 else if (this->protocol
== IPPROTO_ICMP
||
344 this->protocol
== IPPROTO_ICMPV6
)
346 written
+= print_icmp(data
, this->from_port
);
347 written
+= print_in_hook(data
, "-");
348 written
+= print_icmp(data
, this->to_port
);
352 written
+= print_in_hook(data
, "%d-%d",
353 this->from_port
, this->to_port
);
357 written
+= print_in_hook(data
, "]");
362 METHOD(traffic_selector_t
, get_subset
, traffic_selector_t
*,
363 private_traffic_selector_t
*this, traffic_selector_t
*other_public
)
365 private_traffic_selector_t
*other
, *subset
;
366 u_int16_t from_port
, to_port
;
371 other
= (private_traffic_selector_t
*)other_public
;
373 if (this->dynamic
|| other
->dynamic
)
374 { /* no set_address() applied, TS has no subset */
378 if (this->type
!= other
->type
)
384 case TS_IPV4_ADDR_RANGE
:
385 size
= sizeof(this->from4
);
387 case TS_IPV6_ADDR_RANGE
:
388 size
= sizeof(this->from6
);
394 if (this->protocol
!= other
->protocol
&&
395 this->protocol
!= 0 && other
->protocol
!= 0)
399 /* select protocol, which is not zero */
400 protocol
= max(this->protocol
, other
->protocol
);
402 if ((is_opaque(this) && is_opaque(other
)) ||
403 (is_opaque(this) && is_any(other
)) ||
404 (is_opaque(other
) && is_any(this)))
411 /* calculate the maximum port range allowed for both */
412 from_port
= max(this->from_port
, other
->from_port
);
413 to_port
= min(this->to_port
, other
->to_port
);
414 if (from_port
> to_port
)
419 /* get higher from-address */
420 if (memcmp(this->from
, other
->from
, size
) > 0)
428 /* get lower to-address */
429 if (memcmp(this->to
, other
->to
, size
) > 0)
437 /* if "from" > "to", we don't have a match */
438 if (memcmp(from
, to
, size
) > 0)
443 /* we have a match in protocol, port, and address: return it... */
444 subset
= traffic_selector_create(protocol
, this->type
, from_port
, to_port
);
445 memcpy(subset
->from
, from
, size
);
446 memcpy(subset
->to
, to
, size
);
447 calc_netbits(subset
);
449 return &subset
->public;
452 METHOD(traffic_selector_t
, equals
, bool,
453 private_traffic_selector_t
*this, traffic_selector_t
*other_public
)
455 private_traffic_selector_t
*other
;
457 other
= (private_traffic_selector_t
*)other_public
;
458 if (this->type
!= other
->type
)
462 if (!(this->from_port
== other
->from_port
&&
463 this->to_port
== other
->to_port
&&
464 this->protocol
== other
->protocol
))
470 case TS_IPV4_ADDR_RANGE
:
471 if (memeq(this->from4
, other
->from4
, sizeof(this->from4
)) &&
472 memeq(this->to4
, other
->to4
, sizeof(this->to4
)))
477 case TS_IPV6_ADDR_RANGE
:
478 if (memeq(this->from6
, other
->from6
, sizeof(this->from6
)) &&
479 memeq(this->to6
, other
->to6
, sizeof(this->to6
)))
490 METHOD(traffic_selector_t
, get_from_address
, chunk_t
,
491 private_traffic_selector_t
*this)
495 case TS_IPV4_ADDR_RANGE
:
496 return chunk_create(this->from
, sizeof(this->from4
));
497 case TS_IPV6_ADDR_RANGE
:
498 return chunk_create(this->from
, sizeof(this->from6
));
504 METHOD(traffic_selector_t
, get_to_address
, chunk_t
,
505 private_traffic_selector_t
*this)
509 case TS_IPV4_ADDR_RANGE
:
510 return chunk_create(this->to
, sizeof(this->to4
));
511 case TS_IPV6_ADDR_RANGE
:
512 return chunk_create(this->to
, sizeof(this->to6
));
518 METHOD(traffic_selector_t
, get_from_port
, u_int16_t
,
519 private_traffic_selector_t
*this)
521 return this->from_port
;
524 METHOD(traffic_selector_t
, get_to_port
, u_int16_t
,
525 private_traffic_selector_t
*this)
527 return this->to_port
;
530 METHOD(traffic_selector_t
, get_type
, ts_type_t
,
531 private_traffic_selector_t
*this)
536 METHOD(traffic_selector_t
, get_protocol
, u_int8_t
,
537 private_traffic_selector_t
*this)
539 return this->protocol
;
542 METHOD(traffic_selector_t
, is_host
, bool,
543 private_traffic_selector_t
*this, host_t
*host
)
548 int family
= host
->get_family(host
);
550 if ((family
== AF_INET
&& this->type
== TS_IPV4_ADDR_RANGE
) ||
551 (family
== AF_INET6
&& this->type
== TS_IPV6_ADDR_RANGE
))
553 addr
= host
->get_address(host
);
554 if (memeq(addr
.ptr
, this->from
, addr
.len
) &&
555 memeq(addr
.ptr
, this->to
, addr
.len
))
563 size_t length
= (this->type
== TS_IPV4_ADDR_RANGE
) ? 4 : 16;
570 if (memeq(this->from
, this->to
, length
))
578 METHOD(traffic_selector_t
, is_dynamic
, bool,
579 private_traffic_selector_t
*this)
581 return this->dynamic
;
584 METHOD(traffic_selector_t
, set_address
, void,
585 private_traffic_selector_t
*this, host_t
*host
)
587 if (is_host(this, NULL
))
589 this->type
= host
->get_family(host
) == AF_INET
?
590 TS_IPV4_ADDR_RANGE
: TS_IPV6_ADDR_RANGE
;
592 if (host
->is_anyaddr(host
))
594 memset(this->from6
, 0x00, sizeof(this->from6
));
595 memset(this->to6
, 0xFF, sizeof(this->to6
));
600 chunk_t from
= host
->get_address(host
);
601 memcpy(this->from
, from
.ptr
, from
.len
);
602 memcpy(this->to
, from
.ptr
, from
.len
);
603 this->netbits
= from
.len
* 8;
605 this->dynamic
= FALSE
;
609 METHOD(traffic_selector_t
, is_contained_in
, bool,
610 private_traffic_selector_t
*this, traffic_selector_t
*other
)
612 private_traffic_selector_t
*subset
;
613 bool contained_in
= FALSE
;
615 subset
= (private_traffic_selector_t
*)get_subset(this, other
);
619 if (equals(subset
, &this->public))
628 METHOD(traffic_selector_t
, includes
, bool,
629 private_traffic_selector_t
*this, host_t
*host
)
632 int family
= host
->get_family(host
);
634 if ((family
== AF_INET
&& this->type
== TS_IPV4_ADDR_RANGE
) ||
635 (family
== AF_INET6
&& this->type
== TS_IPV6_ADDR_RANGE
))
637 addr
= host
->get_address(host
);
639 return memcmp(this->from
, addr
.ptr
, addr
.len
) <= 0 &&
640 memcmp(this->to
, addr
.ptr
, addr
.len
) >= 0;
646 METHOD(traffic_selector_t
, to_subnet
, bool,
647 private_traffic_selector_t
*this, host_t
**net
, u_int8_t
*mask
)
649 /* there is no way to do this cleanly, as the address range may
650 * be anything else but a subnet. We use from_addr as subnet
651 * and try to calculate a usable subnet mask.
653 int family
, non_zero_bytes
;
657 *mask
= (this->netbits
== NON_SUBNET_ADDRESS_RANGE
) ? calc_netbits(this)
662 case TS_IPV4_ADDR_RANGE
:
664 net_chunk
.len
= sizeof(this->from4
);
666 case TS_IPV6_ADDR_RANGE
:
668 net_chunk
.len
= sizeof(this->from6
);
675 net_chunk
.ptr
= malloc(net_chunk
.len
);
676 memset(net_chunk
.ptr
, 0x00, net_chunk
.len
);
679 non_zero_bytes
= (*mask
+ 7) / 8;
680 memcpy(net_chunk
.ptr
, this->from
, non_zero_bytes
);
681 net_chunk
.ptr
[non_zero_bytes
-1] &= 0xFF << (8 * non_zero_bytes
- *mask
);
684 if (this->to_port
== this->from_port
)
686 port
= this->to_port
;
689 *net
= host_create_from_chunk(family
, net_chunk
, port
);
690 chunk_free(&net_chunk
);
692 return this->netbits
!= NON_SUBNET_ADDRESS_RANGE
;
695 METHOD(traffic_selector_t
, clone_
, traffic_selector_t
*,
696 private_traffic_selector_t
*this)
698 private_traffic_selector_t
*clone
;
700 clone
= traffic_selector_create(this->protocol
, this->type
,
701 this->from_port
, this->to_port
);
702 clone
->netbits
= this->netbits
;
703 clone
->dynamic
= this->dynamic
;
707 case TS_IPV4_ADDR_RANGE
:
708 memcpy(clone
->from4
, this->from4
, sizeof(this->from4
));
709 memcpy(clone
->to4
, this->to4
, sizeof(this->to4
));
710 return &clone
->public;
711 case TS_IPV6_ADDR_RANGE
:
712 memcpy(clone
->from6
, this->from6
, sizeof(this->from6
));
713 memcpy(clone
->to6
, this->to6
, sizeof(this->to6
));
714 return &clone
->public;
717 return &clone
->public;
721 METHOD(traffic_selector_t
, destroy
, void,
722 private_traffic_selector_t
*this)
730 traffic_selector_t
*traffic_selector_create_from_bytes(u_int8_t protocol
,
732 chunk_t from
, u_int16_t from_port
,
733 chunk_t to
, u_int16_t to_port
)
735 private_traffic_selector_t
*this = traffic_selector_create(protocol
, type
,
740 case TS_IPV4_ADDR_RANGE
:
741 if (from
.len
!= 4 || to
.len
!= 4)
746 memcpy(this->from4
, from
.ptr
, from
.len
);
747 memcpy(this->to4
, to
.ptr
, to
.len
);
749 case TS_IPV6_ADDR_RANGE
:
750 if (from
.len
!= 16 || to
.len
!= 16)
755 memcpy(this->from6
, from
.ptr
, from
.len
);
756 memcpy(this->to6
, to
.ptr
, to
.len
);
763 return (&this->public);
769 traffic_selector_t
*traffic_selector_create_from_rfc3779_format(ts_type_t type
,
770 chunk_t from
, chunk_t to
)
773 private_traffic_selector_t
*this = traffic_selector_create(0, type
, 0, 65535);
777 case TS_IPV4_ADDR_RANGE
:
780 case TS_IPV6_ADDR_RANGE
:
787 memset(this->from
, 0x00, len
);
788 memset(this->to
, 0xff, len
);
792 memcpy(this->from
, from
.ptr
+1, from
.len
-1);
796 u_int8_t mask
= to
.ptr
[0] ? (1 << to
.ptr
[0]) - 1 : 0;
798 memcpy(this->to
, to
.ptr
+1, to
.len
-1);
799 this->to
[to
.len
-2] |= mask
;
801 this->netbits
= chunk_equals(from
, to
) ? (from
.len
-1)*8 - from
.ptr
[0]
802 : NON_SUBNET_ADDRESS_RANGE
;
803 return (&this->public);
809 traffic_selector_t
*traffic_selector_create_from_subnet(host_t
*net
,
810 u_int8_t netbits
, u_int8_t protocol
,
811 u_int16_t from_port
, u_int16_t to_port
)
813 private_traffic_selector_t
*this;
816 this = traffic_selector_create(protocol
, 0, from_port
, to_port
);
818 switch (net
->get_family(net
))
821 this->type
= TS_IPV4_ADDR_RANGE
;
824 this->type
= TS_IPV6_ADDR_RANGE
;
831 from
= net
->get_address(net
);
832 memcpy(this->from
, from
.ptr
, from
.len
);
833 netbits
= min(netbits
, this->type
== TS_IPV4_ADDR_RANGE
? 32 : 128);
834 calc_range(this, netbits
);
837 return &this->public;
843 traffic_selector_t
*traffic_selector_create_from_string(
844 u_int8_t protocol
, ts_type_t type
,
845 char *from_addr
, u_int16_t from_port
,
846 char *to_addr
, u_int16_t to_port
)
848 private_traffic_selector_t
*this;
853 case TS_IPV4_ADDR_RANGE
:
856 case TS_IPV6_ADDR_RANGE
:
863 this = traffic_selector_create(protocol
, type
, from_port
, to_port
);
865 if (inet_pton(family
, from_addr
, this->from
) != 1 ||
866 inet_pton(family
, to_addr
, this->to
) != 1)
873 return &this->public;
879 traffic_selector_t
*traffic_selector_create_from_cidr(
880 char *string
, u_int8_t protocol
,
881 u_int16_t from_port
, u_int16_t to_port
)
886 net
= host_create_from_subnet(string
, &bits
);
889 return traffic_selector_create_from_subnet(net
, bits
, protocol
,
898 traffic_selector_t
*traffic_selector_create_dynamic(u_int8_t protocol
,
899 u_int16_t from_port
, u_int16_t to_port
)
901 private_traffic_selector_t
*this = traffic_selector_create(
902 protocol
, TS_IPV4_ADDR_RANGE
, from_port
, to_port
);
904 memset(this->from6
, 0, sizeof(this->from6
));
905 memset(this->to6
, 0xFF, sizeof(this->to6
));
907 this->dynamic
= TRUE
;
909 return &this->public;
915 static private_traffic_selector_t
*traffic_selector_create(u_int8_t protocol
,
916 ts_type_t type
, u_int16_t from_port
, u_int16_t to_port
)
918 private_traffic_selector_t
*this;
922 .get_subset
= _get_subset
,
924 .get_from_address
= _get_from_address
,
925 .get_to_address
= _get_to_address
,
926 .get_from_port
= _get_from_port
,
927 .get_to_port
= _get_to_port
,
928 .get_type
= _get_type
,
929 .get_protocol
= _get_protocol
,
931 .is_dynamic
= _is_dynamic
,
932 .is_contained_in
= _is_contained_in
,
933 .includes
= _includes
,
934 .set_address
= _set_address
,
935 .to_subnet
= _to_subnet
,
939 .from_port
= from_port
,
941 .protocol
= protocol
,
944 if (protocol
== IPPROTO_ICMP
|| protocol
== IPPROTO_ICMPV6
)
946 this->from_port
= from_port
< 256 ? from_port
<< 8 : from_port
;
947 this->to_port
= to_port
< 256 ? to_port
<< 8 : to_port
;