2 * Copyright (C) 2007-2017 Tobias Brunner
3 * Copyright (C) 2005-2007 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * HSR 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
21 #include "traffic_selector.h"
23 #include <utils/debug.h>
24 #include <utils/utils.h>
25 #include <utils/identification.h>
26 #include <collections/linked_list.h>
30 #define TS_IP_LEN(this) ({ ((this)->type == TS_IPV4_ADDR_RANGE) ? IPV4_LEN : IPV6_LEN; })
32 #define NON_SUBNET_ADDRESS_RANGE 255
34 ENUM(ts_type_name
, TS_IPV4_ADDR_RANGE
, TS_IPV6_ADDR_RANGE
,
39 typedef struct private_traffic_selector_t private_traffic_selector_t
;
42 * Private data of an traffic_selector_t object
44 struct private_traffic_selector_t
{
49 traffic_selector_t
public;
57 * IP protocol (UDP, TCP, ICMP, ...)
62 * narrow this traffic selector to hosts external ip
63 * if set, from and to have no meaning until set_address() is called
68 * subnet size in CIDR notation, 255 means a non-subnet address range
73 * begin of address range, network order
78 * end of address range, network order
94 * calculate the "to"-address for the "from" address and a subnet size
96 static void calc_range(private_traffic_selector_t
*this, uint8_t netbits
)
102 this->netbits
= netbits
;
104 len
= TS_IP_LEN(this);
105 bytes
= (netbits
+ 7)/8;
106 bits
= (bytes
* 8) - netbits
;
107 mask
= bits
? (1 << bits
) - 1 : 0;
109 memcpy(this->to
, this->from
, bytes
);
110 memset(this->from
+ bytes
, 0x00, len
- bytes
);
111 memset(this->to
+ bytes
, 0xff, len
- bytes
);
115 this->from
[bytes
-1] &= ~mask
;
116 this->to
[bytes
-1] |= mask
;
121 * calculate the subnet size from the "to" and "from" addresses
123 static uint8_t calc_netbits(private_traffic_selector_t
*this)
127 size_t size
= TS_IP_LEN(this);
130 /* a perfect match results in a single address with a /32 or /128 netmask */
131 netbits
= (size
* 8);
132 this->netbits
= netbits
;
134 /* go through all bits of the addresses, beginning in the front.
135 * as long as they are equal, the subnet gets larger
137 for (byte
= 0; byte
< size
; byte
++)
139 for (bit
= 7; bit
>= 0; bit
--)
141 uint8_t bitmask
= 1 << bit
;
145 if ((bitmask
& this->from
[byte
]) != (bitmask
& this->to
[byte
]))
147 /* store the common prefix which might be a true subnet */
148 netbits
= (7 - bit
) + (byte
* 8);
149 this->netbits
= netbits
;
155 if ((bitmask
& this->from
[byte
]) || !(bitmask
& this->to
[byte
]))
157 this->netbits
= NON_SUBNET_ADDRESS_RANGE
;
158 return netbits
; /* return a pseudo subnet */
164 return netbits
; /* return a true subnet */
168 * internal generic constructor
170 static private_traffic_selector_t
*traffic_selector_create(uint8_t protocol
,
171 ts_type_t type
, uint16_t from_port
, uint16_t to_port
);
174 * Check if TS contains "opaque" ports
176 static bool is_opaque(private_traffic_selector_t
*this)
178 return this->from_port
== 0xffff && this->to_port
== 0;
182 * Check if TS contains "any" ports
184 static bool is_any(private_traffic_selector_t
*this)
186 return this->from_port
== 0 && this->to_port
== 0xffff;
190 * Print ICMP/ICMPv6 type and code
192 static int print_icmp(printf_hook_data_t
*data
, uint16_t port
)
196 type
= traffic_selector_icmp_type(port
);
197 code
= traffic_selector_icmp_code(port
);
200 return print_in_hook(data
, "%d(%d)", type
, code
);
202 return print_in_hook(data
, "%d", type
);
206 * Described in header.
208 int traffic_selector_printf_hook(printf_hook_data_t
*data
,
209 printf_hook_spec_t
*spec
, const void *const *args
)
211 private_traffic_selector_t
*this = *((private_traffic_selector_t
**)(args
[0]));
212 linked_list_t
*list
= *((linked_list_t
**)(args
[0]));
213 enumerator_t
*enumerator
;
214 char from_str
[INET6_ADDRSTRLEN
] = "";
215 char to_str
[INET6_ADDRSTRLEN
] = "";
216 char *serv_proto
= NULL
, *sep
= "";
217 bool has_proto
, has_ports
;
218 size_t written
= 0, len
;
219 char from
[IPV6_LEN
], to
[IPV6_LEN
];
223 return print_in_hook(data
, "(null)");
228 enumerator
= list
->create_enumerator(list
);
229 while (enumerator
->enumerate(enumerator
, (void**)&this))
231 written
+= print_in_hook(data
, "%s%R", sep
, this);
234 enumerator
->destroy(enumerator
);
238 len
= TS_IP_LEN(this);
239 memset(from
, 0, len
);
240 memset(to
, 0xFF, len
);
242 memeq(this->from
, from
, len
) && memeq(this->to
, to
, len
))
244 written
+= print_in_hook(data
, "dynamic");
248 if (this->type
== TS_IPV4_ADDR_RANGE
)
250 inet_ntop(AF_INET
, &this->from
, from_str
, sizeof(from_str
));
254 inet_ntop(AF_INET6
, &this->from
, from_str
, sizeof(from_str
));
256 if (this->netbits
== NON_SUBNET_ADDRESS_RANGE
)
258 if (this->type
== TS_IPV4_ADDR_RANGE
)
260 inet_ntop(AF_INET
, &this->to
, to_str
, sizeof(to_str
));
264 inet_ntop(AF_INET6
, &this->to
, to_str
, sizeof(to_str
));
266 written
+= print_in_hook(data
, "%s..%s", from_str
, to_str
);
270 written
+= print_in_hook(data
, "%s/%d", from_str
, this->netbits
);
274 /* check if we have protocol and/or port selectors */
275 has_proto
= this->protocol
!= 0;
276 has_ports
= !is_any(this);
278 if (!has_proto
&& !has_ports
)
283 written
+= print_in_hook(data
, "[");
285 /* build protocol string */
288 struct protoent
*proto
= getprotobynumber(this->protocol
);
292 written
+= print_in_hook(data
, "%s", proto
->p_name
);
293 serv_proto
= proto
->p_name
;
297 written
+= print_in_hook(data
, "%d", this->protocol
);
302 written
+= print_in_hook(data
, "0");
305 /* build port string */
308 written
+= print_in_hook(data
, "/");
310 if (this->from_port
== this->to_port
)
312 struct servent
*serv
;
314 if (this->protocol
== IPPROTO_ICMP
||
315 this->protocol
== IPPROTO_ICMPV6
)
317 written
+= print_icmp(data
, this->from_port
);
321 serv
= getservbyport(htons(this->from_port
), serv_proto
);
324 written
+= print_in_hook(data
, "%s", serv
->s_name
);
328 written
+= print_in_hook(data
, "%d", this->from_port
);
332 else if (is_opaque(this))
334 written
+= print_in_hook(data
, "OPAQUE");
336 else if (this->protocol
== IPPROTO_ICMP
||
337 this->protocol
== IPPROTO_ICMPV6
)
339 written
+= print_icmp(data
, this->from_port
);
340 written
+= print_in_hook(data
, "-");
341 written
+= print_icmp(data
, this->to_port
);
345 written
+= print_in_hook(data
, "%d-%d",
346 this->from_port
, this->to_port
);
350 written
+= print_in_hook(data
, "]");
355 METHOD(traffic_selector_t
, get_subset
, traffic_selector_t
*,
356 private_traffic_selector_t
*this, traffic_selector_t
*other_public
)
358 private_traffic_selector_t
*other
, *subset
;
359 uint16_t from_port
, to_port
;
364 other
= (private_traffic_selector_t
*)other_public
;
366 if (this->dynamic
|| other
->dynamic
)
367 { /* no set_address() applied, TS has no subset */
371 if (this->type
!= other
->type
)
376 if (this->protocol
!= other
->protocol
&&
377 this->protocol
!= 0 && other
->protocol
!= 0)
381 /* select protocol, which is not zero */
382 protocol
= max(this->protocol
, other
->protocol
);
384 if ((is_opaque(this) && is_opaque(other
)) ||
385 (is_opaque(this) && is_any(other
)) ||
386 (is_opaque(other
) && is_any(this)))
393 /* calculate the maximum port range allowed for both */
394 from_port
= max(this->from_port
, other
->from_port
);
395 to_port
= min(this->to_port
, other
->to_port
);
396 if (from_port
> to_port
)
401 size
= TS_IP_LEN(this);
402 /* get higher from-address */
403 if (memcmp(this->from
, other
->from
, size
) > 0)
411 /* get lower to-address */
412 if (memcmp(this->to
, other
->to
, size
) > 0)
420 /* if "from" > "to", we don't have a match */
421 if (memcmp(from
, to
, size
) > 0)
426 /* we have a match in protocol, port, and address: return it... */
427 subset
= traffic_selector_create(protocol
, this->type
, from_port
, to_port
);
428 memcpy(subset
->from
, from
, size
);
429 memcpy(subset
->to
, to
, size
);
430 calc_netbits(subset
);
432 return &subset
->public;
435 METHOD(traffic_selector_t
, equals
, bool,
436 private_traffic_selector_t
*this, traffic_selector_t
*other
)
438 return traffic_selector_cmp(&this->public, other
, NULL
) == 0;
441 METHOD(traffic_selector_t
, get_from_address
, chunk_t
,
442 private_traffic_selector_t
*this)
444 return chunk_create(this->from
, TS_IP_LEN(this));
447 METHOD(traffic_selector_t
, get_to_address
, chunk_t
,
448 private_traffic_selector_t
*this)
450 return chunk_create(this->to
, TS_IP_LEN(this));
453 METHOD(traffic_selector_t
, get_from_port
, uint16_t,
454 private_traffic_selector_t
*this)
456 return this->from_port
;
459 METHOD(traffic_selector_t
, get_to_port
, uint16_t,
460 private_traffic_selector_t
*this)
462 return this->to_port
;
465 METHOD(traffic_selector_t
, get_type
, ts_type_t
,
466 private_traffic_selector_t
*this)
471 METHOD(traffic_selector_t
, get_protocol
, uint8_t,
472 private_traffic_selector_t
*this)
474 return this->protocol
;
477 METHOD(traffic_selector_t
, is_host
, bool,
478 private_traffic_selector_t
*this, host_t
*host
)
483 int family
= host
->get_family(host
);
485 if ((family
== AF_INET
&& this->type
== TS_IPV4_ADDR_RANGE
) ||
486 (family
== AF_INET6
&& this->type
== TS_IPV6_ADDR_RANGE
))
488 addr
= host
->get_address(host
);
489 if (memeq(addr
.ptr
, this->from
, addr
.len
) &&
490 memeq(addr
.ptr
, this->to
, addr
.len
))
498 size_t length
= TS_IP_LEN(this);
505 if (memeq(this->from
, this->to
, length
))
513 METHOD(traffic_selector_t
, is_dynamic
, bool,
514 private_traffic_selector_t
*this)
516 return this->dynamic
;
519 METHOD(traffic_selector_t
, set_address
, void,
520 private_traffic_selector_t
*this, host_t
*host
)
522 this->type
= host
->get_family(host
) == AF_INET
? TS_IPV4_ADDR_RANGE
523 : TS_IPV6_ADDR_RANGE
;
525 if (host
->is_anyaddr(host
))
527 memset(this->from
, 0x00, sizeof(this->from
));
528 memset(this->to
, 0xFF, sizeof(this->to
));
533 chunk_t from
= host
->get_address(host
);
534 memcpy(this->from
, from
.ptr
, from
.len
);
535 memcpy(this->to
, from
.ptr
, from
.len
);
536 this->netbits
= from
.len
* 8;
538 this->dynamic
= FALSE
;
541 METHOD(traffic_selector_t
, is_contained_in
, bool,
542 private_traffic_selector_t
*this, traffic_selector_t
*other
)
544 private_traffic_selector_t
*subset
;
545 bool contained_in
= FALSE
;
547 subset
= (private_traffic_selector_t
*)get_subset(this, other
);
551 if (equals(subset
, &this->public))
560 METHOD(traffic_selector_t
, includes
, bool,
561 private_traffic_selector_t
*this, host_t
*host
)
564 int family
= host
->get_family(host
);
566 if ((family
== AF_INET
&& this->type
== TS_IPV4_ADDR_RANGE
) ||
567 (family
== AF_INET6
&& this->type
== TS_IPV6_ADDR_RANGE
))
569 addr
= host
->get_address(host
);
571 return memcmp(this->from
, addr
.ptr
, addr
.len
) <= 0 &&
572 memcmp(this->to
, addr
.ptr
, addr
.len
) >= 0;
578 METHOD(traffic_selector_t
, to_subnet
, bool,
579 private_traffic_selector_t
*this, host_t
**net
, uint8_t *mask
)
581 /* there is no way to do this cleanly, as the address range may
582 * be anything else but a subnet. We use from_addr as subnet
583 * and try to calculate a usable subnet mask.
585 int family
, non_zero_bytes
;
589 *mask
= (this->netbits
== NON_SUBNET_ADDRESS_RANGE
) ? calc_netbits(this)
594 case TS_IPV4_ADDR_RANGE
:
596 net_chunk
.len
= IPV4_LEN
;
598 case TS_IPV6_ADDR_RANGE
:
600 net_chunk
.len
= IPV6_LEN
;
607 net_chunk
.ptr
= malloc(net_chunk
.len
);
608 memset(net_chunk
.ptr
, 0x00, net_chunk
.len
);
611 non_zero_bytes
= (*mask
+ 7) / 8;
612 memcpy(net_chunk
.ptr
, this->from
, non_zero_bytes
);
613 net_chunk
.ptr
[non_zero_bytes
-1] &= 0xFF << (8 * non_zero_bytes
- *mask
);
616 if (this->to_port
== this->from_port
)
618 port
= this->to_port
;
621 *net
= host_create_from_chunk(family
, net_chunk
, port
);
622 chunk_free(&net_chunk
);
624 return this->netbits
!= NON_SUBNET_ADDRESS_RANGE
;
627 METHOD(traffic_selector_t
, clone_
, traffic_selector_t
*,
628 private_traffic_selector_t
*this)
630 private_traffic_selector_t
*clone
;
631 size_t len
= TS_IP_LEN(this);
633 clone
= traffic_selector_create(this->protocol
, this->type
,
634 this->from_port
, this->to_port
);
635 clone
->netbits
= this->netbits
;
636 clone
->dynamic
= this->dynamic
;
638 memcpy(clone
->from
, this->from
, len
);
639 memcpy(clone
->to
, this->to
, len
);
640 return &clone
->public;
643 METHOD(traffic_selector_t
, hash
, u_int
,
644 private_traffic_selector_t
*this, u_int hash
)
646 return chunk_hash_inc(get_from_address(this),
647 chunk_hash_inc(get_to_address(this),
648 chunk_hash_inc(chunk_from_thing(this->from_port
),
649 chunk_hash_inc(chunk_from_thing(this->to_port
),
650 chunk_hash_inc(chunk_from_thing(this->protocol
),
654 METHOD(traffic_selector_t
, destroy
, void,
655 private_traffic_selector_t
*this)
661 * Compare two integers
663 static int compare_int(int a
, int b
)
671 int traffic_selector_cmp(traffic_selector_t
*a_pub
, traffic_selector_t
*b_pub
,
674 private_traffic_selector_t
*a
, *b
;
678 a
= (private_traffic_selector_t
*)a_pub
;
679 b
= (private_traffic_selector_t
*)b_pub
;
681 /* IPv4 before IPv6 */
682 res
= compare_int(a
->type
, b
->type
);
688 /* lower starting subnets first */
689 res
= memcmp(a
->from
, b
->from
, len
);
694 /* larger subnets first */
695 res
= memcmp(b
->to
, a
->to
, len
);
700 /* lower protocols first */
701 res
= compare_int(a
->protocol
, b
->protocol
);
706 /* lower starting ports first */
707 res
= compare_int(a
->from_port
, b
->from_port
);
712 /* larger port ranges first */
713 return compare_int(b
->to_port
, a
->to_port
);
719 traffic_selector_t
*traffic_selector_create_from_bytes(uint8_t protocol
,
721 chunk_t from
, uint16_t from_port
,
722 chunk_t to
, uint16_t to_port
)
724 private_traffic_selector_t
*this = traffic_selector_create(protocol
, type
,
731 if (from
.len
!= to
.len
|| from
.len
!= TS_IP_LEN(this))
736 memcpy(this->from
, from
.ptr
, from
.len
);
737 memcpy(this->to
, to
.ptr
, to
.len
);
739 return &this->public;
745 traffic_selector_t
*traffic_selector_create_from_rfc3779_format(ts_type_t type
,
746 chunk_t from
, chunk_t to
)
748 private_traffic_selector_t
*this = traffic_selector_create(0, type
, 0, 65535);
755 len
= TS_IP_LEN(this);
757 memset(this->from
, 0x00, len
);
758 memset(this->to
, 0xff, len
);
762 memcpy(this->from
, from
.ptr
+1, from
.len
-1);
766 uint8_t mask
= to
.ptr
[0] ? (1 << to
.ptr
[0]) - 1 : 0;
768 memcpy(this->to
, to
.ptr
+1, to
.len
-1);
769 this->to
[to
.len
-2] |= mask
;
772 return &this->public;
778 traffic_selector_t
*traffic_selector_create_from_subnet(host_t
*net
,
779 uint8_t netbits
, uint8_t protocol
,
780 uint16_t from_port
, uint16_t to_port
)
782 private_traffic_selector_t
*this;
786 switch (net
->get_family(net
))
789 type
= TS_IPV4_ADDR_RANGE
;
792 type
= TS_IPV6_ADDR_RANGE
;
799 this = traffic_selector_create(protocol
, type
, from_port
, to_port
);
801 from
= net
->get_address(net
);
802 memcpy(this->from
, from
.ptr
, from
.len
);
803 netbits
= min(netbits
, TS_IP_LEN(this) * 8);
804 calc_range(this, netbits
);
806 return &this->public;
812 traffic_selector_t
*traffic_selector_create_from_string(
813 uint8_t protocol
, ts_type_t type
,
814 char *from_addr
, uint16_t from_port
,
815 char *to_addr
, uint16_t to_port
)
817 private_traffic_selector_t
*this;
822 case TS_IPV4_ADDR_RANGE
:
825 case TS_IPV6_ADDR_RANGE
:
832 this = traffic_selector_create(protocol
, type
, from_port
, to_port
);
834 if (inet_pton(family
, from_addr
, this->from
) != 1 ||
835 inet_pton(family
, to_addr
, this->to
) != 1)
841 return &this->public;
847 traffic_selector_t
*traffic_selector_create_from_cidr(
848 char *string
, uint8_t protocol
,
849 uint16_t from_port
, uint16_t to_port
)
854 net
= host_create_from_subnet(string
, &bits
);
857 return traffic_selector_create_from_subnet(net
, bits
, protocol
,
866 traffic_selector_t
*traffic_selector_create_dynamic(uint8_t protocol
,
867 uint16_t from_port
, uint16_t to_port
)
869 private_traffic_selector_t
*this = traffic_selector_create(
870 protocol
, TS_IPV4_ADDR_RANGE
, from_port
, to_port
);
872 memset(this->from
, 0, sizeof(this->from
));
873 memset(this->to
, 0xFF, sizeof(this->to
));
875 this->dynamic
= TRUE
;
877 return &this->public;
883 static private_traffic_selector_t
*traffic_selector_create(uint8_t protocol
,
884 ts_type_t type
, uint16_t from_port
, uint16_t to_port
)
886 private_traffic_selector_t
*this;
889 if (type
!= TS_IPV4_ADDR_RANGE
&& type
!= TS_IPV6_ADDR_RANGE
)
896 .get_subset
= _get_subset
,
898 .get_from_address
= _get_from_address
,
899 .get_to_address
= _get_to_address
,
900 .get_from_port
= _get_from_port
,
901 .get_to_port
= _get_to_port
,
902 .get_type
= _get_type
,
903 .get_protocol
= _get_protocol
,
905 .is_dynamic
= _is_dynamic
,
906 .is_contained_in
= _is_contained_in
,
907 .includes
= _includes
,
908 .set_address
= _set_address
,
909 .to_subnet
= _to_subnet
,
914 .from_port
= from_port
,
916 .protocol
= protocol
,
919 if (protocol
== IPPROTO_ICMP
|| protocol
== IPPROTO_ICMPV6
)
921 this->from_port
= from_port
< 256 ? from_port
<< 8 : from_port
;
922 this->to_port
= to_port
< 256 ? to_port
<< 8 : to_port
;