2 * Copyright (C) 2007-2017 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
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
);
112 this->from
[bytes
-1] &= ~mask
;
113 this->to
[bytes
-1] |= mask
;
117 * calculate the subnet size from the "to" and "from" addresses
119 static uint8_t calc_netbits(private_traffic_selector_t
*this)
123 size_t size
= TS_IP_LEN(this);
126 /* a perfect match results in a single address with a /32 or /128 netmask */
127 netbits
= (size
* 8);
128 this->netbits
= netbits
;
130 /* go through all bits of the addresses, beginning in the front.
131 * as long as they are equal, the subnet gets larger
133 for (byte
= 0; byte
< size
; byte
++)
135 for (bit
= 7; bit
>= 0; bit
--)
137 uint8_t bitmask
= 1 << bit
;
141 if ((bitmask
& this->from
[byte
]) != (bitmask
& this->to
[byte
]))
143 /* store the common prefix which might be a true subnet */
144 netbits
= (7 - bit
) + (byte
* 8);
145 this->netbits
= netbits
;
151 if ((bitmask
& this->from
[byte
]) || !(bitmask
& this->to
[byte
]))
153 this->netbits
= NON_SUBNET_ADDRESS_RANGE
;
154 return netbits
; /* return a pseudo subnet */
160 return netbits
; /* return a true subnet */
164 * internal generic constructor
166 static private_traffic_selector_t
*traffic_selector_create(uint8_t protocol
,
167 ts_type_t type
, uint16_t from_port
, uint16_t to_port
);
170 * Check if TS contains "opaque" ports
172 static bool is_opaque(private_traffic_selector_t
*this)
174 return this->from_port
== 0xffff && this->to_port
== 0;
178 * Check if TS contains "any" ports
180 static bool is_any(private_traffic_selector_t
*this)
182 return this->from_port
== 0 && this->to_port
== 0xffff;
186 * Print ICMP/ICMPv6 type and code
188 static int print_icmp(printf_hook_data_t
*data
, uint16_t port
)
192 type
= traffic_selector_icmp_type(port
);
193 code
= traffic_selector_icmp_code(port
);
196 return print_in_hook(data
, "%d(%d)", type
, code
);
198 return print_in_hook(data
, "%d", type
);
202 * Described in header.
204 int traffic_selector_printf_hook(printf_hook_data_t
*data
,
205 printf_hook_spec_t
*spec
, const void *const *args
)
207 private_traffic_selector_t
*this = *((private_traffic_selector_t
**)(args
[0]));
208 linked_list_t
*list
= *((linked_list_t
**)(args
[0]));
209 enumerator_t
*enumerator
;
210 char from_str
[INET6_ADDRSTRLEN
] = "";
211 char to_str
[INET6_ADDRSTRLEN
] = "";
212 char *serv_proto
= NULL
, *sep
= "";
213 bool has_proto
, has_ports
;
214 size_t written
= 0, len
;
215 char from
[IPV6_LEN
], to
[IPV6_LEN
];
219 return print_in_hook(data
, "(null)");
224 enumerator
= list
->create_enumerator(list
);
225 while (enumerator
->enumerate(enumerator
, (void**)&this))
227 written
+= print_in_hook(data
, "%s%R", sep
, this);
230 enumerator
->destroy(enumerator
);
234 len
= TS_IP_LEN(this);
235 memset(from
, 0, len
);
236 memset(to
, 0xFF, len
);
238 memeq(this->from
, from
, len
) && memeq(this->to
, to
, len
))
240 written
+= print_in_hook(data
, "dynamic");
244 if (this->type
== TS_IPV4_ADDR_RANGE
)
246 inet_ntop(AF_INET
, &this->from
, from_str
, sizeof(from_str
));
250 inet_ntop(AF_INET6
, &this->from
, from_str
, sizeof(from_str
));
252 if (this->netbits
== NON_SUBNET_ADDRESS_RANGE
)
254 if (this->type
== TS_IPV4_ADDR_RANGE
)
256 inet_ntop(AF_INET
, &this->to
, to_str
, sizeof(to_str
));
260 inet_ntop(AF_INET6
, &this->to
, to_str
, sizeof(to_str
));
262 written
+= print_in_hook(data
, "%s..%s", from_str
, to_str
);
266 written
+= print_in_hook(data
, "%s/%d", from_str
, this->netbits
);
270 /* check if we have protocol and/or port selectors */
271 has_proto
= this->protocol
!= 0;
272 has_ports
= !is_any(this);
274 if (!has_proto
&& !has_ports
)
279 written
+= print_in_hook(data
, "[");
281 /* build protocol string */
284 struct protoent
*proto
= getprotobynumber(this->protocol
);
288 written
+= print_in_hook(data
, "%s", proto
->p_name
);
289 serv_proto
= proto
->p_name
;
293 written
+= print_in_hook(data
, "%d", this->protocol
);
297 if (has_proto
&& has_ports
)
299 written
+= print_in_hook(data
, "/");
302 /* build port string */
305 if (this->from_port
== this->to_port
)
307 struct servent
*serv
;
309 if (this->protocol
== IPPROTO_ICMP
||
310 this->protocol
== IPPROTO_ICMPV6
)
312 written
+= print_icmp(data
, this->from_port
);
316 serv
= getservbyport(htons(this->from_port
), serv_proto
);
319 written
+= print_in_hook(data
, "%s", serv
->s_name
);
323 written
+= print_in_hook(data
, "%d", this->from_port
);
327 else if (is_opaque(this))
329 written
+= print_in_hook(data
, "OPAQUE");
331 else if (this->protocol
== IPPROTO_ICMP
||
332 this->protocol
== IPPROTO_ICMPV6
)
334 written
+= print_icmp(data
, this->from_port
);
335 written
+= print_in_hook(data
, "-");
336 written
+= print_icmp(data
, this->to_port
);
340 written
+= print_in_hook(data
, "%d-%d",
341 this->from_port
, this->to_port
);
345 written
+= print_in_hook(data
, "]");
350 METHOD(traffic_selector_t
, get_subset
, traffic_selector_t
*,
351 private_traffic_selector_t
*this, traffic_selector_t
*other_public
)
353 private_traffic_selector_t
*other
, *subset
;
354 uint16_t from_port
, to_port
;
359 other
= (private_traffic_selector_t
*)other_public
;
361 if (this->dynamic
|| other
->dynamic
)
362 { /* no set_address() applied, TS has no subset */
366 if (this->type
!= other
->type
)
371 if (this->protocol
!= other
->protocol
&&
372 this->protocol
!= 0 && other
->protocol
!= 0)
376 /* select protocol, which is not zero */
377 protocol
= max(this->protocol
, other
->protocol
);
379 if ((is_opaque(this) && is_opaque(other
)) ||
380 (is_opaque(this) && is_any(other
)) ||
381 (is_opaque(other
) && is_any(this)))
388 /* calculate the maximum port range allowed for both */
389 from_port
= max(this->from_port
, other
->from_port
);
390 to_port
= min(this->to_port
, other
->to_port
);
391 if (from_port
> to_port
)
396 size
= TS_IP_LEN(this);
397 /* get higher from-address */
398 if (memcmp(this->from
, other
->from
, size
) > 0)
406 /* get lower to-address */
407 if (memcmp(this->to
, other
->to
, size
) > 0)
415 /* if "from" > "to", we don't have a match */
416 if (memcmp(from
, to
, size
) > 0)
421 /* we have a match in protocol, port, and address: return it... */
422 subset
= traffic_selector_create(protocol
, this->type
, from_port
, to_port
);
423 memcpy(subset
->from
, from
, size
);
424 memcpy(subset
->to
, to
, size
);
425 calc_netbits(subset
);
427 return &subset
->public;
430 METHOD(traffic_selector_t
, equals
, bool,
431 private_traffic_selector_t
*this, traffic_selector_t
*other
)
433 return traffic_selector_cmp(&this->public, other
, NULL
) == 0;
436 METHOD(traffic_selector_t
, get_from_address
, chunk_t
,
437 private_traffic_selector_t
*this)
439 return chunk_create(this->from
, TS_IP_LEN(this));
442 METHOD(traffic_selector_t
, get_to_address
, chunk_t
,
443 private_traffic_selector_t
*this)
445 return chunk_create(this->to
, TS_IP_LEN(this));
448 METHOD(traffic_selector_t
, get_from_port
, uint16_t,
449 private_traffic_selector_t
*this)
451 return this->from_port
;
454 METHOD(traffic_selector_t
, get_to_port
, uint16_t,
455 private_traffic_selector_t
*this)
457 return this->to_port
;
460 METHOD(traffic_selector_t
, get_type
, ts_type_t
,
461 private_traffic_selector_t
*this)
466 METHOD(traffic_selector_t
, get_protocol
, uint8_t,
467 private_traffic_selector_t
*this)
469 return this->protocol
;
472 METHOD(traffic_selector_t
, is_host
, bool,
473 private_traffic_selector_t
*this, host_t
*host
)
478 int family
= host
->get_family(host
);
480 if ((family
== AF_INET
&& this->type
== TS_IPV4_ADDR_RANGE
) ||
481 (family
== AF_INET6
&& this->type
== TS_IPV6_ADDR_RANGE
))
483 addr
= host
->get_address(host
);
484 if (memeq(addr
.ptr
, this->from
, addr
.len
) &&
485 memeq(addr
.ptr
, this->to
, addr
.len
))
493 size_t length
= TS_IP_LEN(this);
500 if (memeq(this->from
, this->to
, length
))
508 METHOD(traffic_selector_t
, is_dynamic
, bool,
509 private_traffic_selector_t
*this)
511 return this->dynamic
;
514 METHOD(traffic_selector_t
, set_address
, void,
515 private_traffic_selector_t
*this, host_t
*host
)
517 this->type
= host
->get_family(host
) == AF_INET
? TS_IPV4_ADDR_RANGE
518 : TS_IPV6_ADDR_RANGE
;
520 if (host
->is_anyaddr(host
))
522 memset(this->from
, 0x00, sizeof(this->from
));
523 memset(this->to
, 0xFF, sizeof(this->to
));
528 chunk_t from
= host
->get_address(host
);
529 memcpy(this->from
, from
.ptr
, from
.len
);
530 memcpy(this->to
, from
.ptr
, from
.len
);
531 this->netbits
= from
.len
* 8;
533 this->dynamic
= FALSE
;
536 METHOD(traffic_selector_t
, is_contained_in
, bool,
537 private_traffic_selector_t
*this, traffic_selector_t
*other
)
539 private_traffic_selector_t
*subset
;
540 bool contained_in
= FALSE
;
542 subset
= (private_traffic_selector_t
*)get_subset(this, other
);
546 if (equals(subset
, &this->public))
555 METHOD(traffic_selector_t
, includes
, bool,
556 private_traffic_selector_t
*this, host_t
*host
)
559 int family
= host
->get_family(host
);
561 if ((family
== AF_INET
&& this->type
== TS_IPV4_ADDR_RANGE
) ||
562 (family
== AF_INET6
&& this->type
== TS_IPV6_ADDR_RANGE
))
564 addr
= host
->get_address(host
);
566 return memcmp(this->from
, addr
.ptr
, addr
.len
) <= 0 &&
567 memcmp(this->to
, addr
.ptr
, addr
.len
) >= 0;
573 METHOD(traffic_selector_t
, to_subnet
, bool,
574 private_traffic_selector_t
*this, host_t
**net
, uint8_t *mask
)
576 /* there is no way to do this cleanly, as the address range may
577 * be anything else but a subnet. We use from_addr as subnet
578 * and try to calculate a usable subnet mask.
580 int family
, non_zero_bytes
;
584 *mask
= (this->netbits
== NON_SUBNET_ADDRESS_RANGE
) ? calc_netbits(this)
589 case TS_IPV4_ADDR_RANGE
:
591 net_chunk
.len
= IPV4_LEN
;
593 case TS_IPV6_ADDR_RANGE
:
595 net_chunk
.len
= IPV6_LEN
;
602 net_chunk
.ptr
= malloc(net_chunk
.len
);
603 memset(net_chunk
.ptr
, 0x00, net_chunk
.len
);
606 non_zero_bytes
= (*mask
+ 7) / 8;
607 memcpy(net_chunk
.ptr
, this->from
, non_zero_bytes
);
608 net_chunk
.ptr
[non_zero_bytes
-1] &= 0xFF << (8 * non_zero_bytes
- *mask
);
611 if (this->to_port
== this->from_port
)
613 port
= this->to_port
;
616 *net
= host_create_from_chunk(family
, net_chunk
, port
);
617 chunk_free(&net_chunk
);
619 return this->netbits
!= NON_SUBNET_ADDRESS_RANGE
;
622 METHOD(traffic_selector_t
, clone_
, traffic_selector_t
*,
623 private_traffic_selector_t
*this)
625 private_traffic_selector_t
*clone
;
626 size_t len
= TS_IP_LEN(this);
628 clone
= traffic_selector_create(this->protocol
, this->type
,
629 this->from_port
, this->to_port
);
630 clone
->netbits
= this->netbits
;
631 clone
->dynamic
= this->dynamic
;
633 memcpy(clone
->from
, this->from
, len
);
634 memcpy(clone
->to
, this->to
, len
);
635 return &clone
->public;
638 METHOD(traffic_selector_t
, hash
, u_int
,
639 private_traffic_selector_t
*this, u_int hash
)
641 return chunk_hash_inc(get_from_address(this),
642 chunk_hash_inc(get_to_address(this),
643 chunk_hash_inc(chunk_from_thing(this->from_port
),
644 chunk_hash_inc(chunk_from_thing(this->to_port
),
645 chunk_hash_inc(chunk_from_thing(this->protocol
),
649 METHOD(traffic_selector_t
, destroy
, void,
650 private_traffic_selector_t
*this)
656 * Compare two integers
658 static int compare_int(int a
, int b
)
666 int traffic_selector_cmp(traffic_selector_t
*a_pub
, traffic_selector_t
*b_pub
,
669 private_traffic_selector_t
*a
, *b
;
673 a
= (private_traffic_selector_t
*)a_pub
;
674 b
= (private_traffic_selector_t
*)b_pub
;
676 /* IPv4 before IPv6 */
677 res
= compare_int(a
->type
, b
->type
);
683 /* lower starting subnets first */
684 res
= memcmp(a
->from
, b
->from
, len
);
689 /* larger subnets first */
690 res
= memcmp(b
->to
, a
->to
, len
);
695 /* lower protocols first */
696 res
= compare_int(a
->protocol
, b
->protocol
);
701 /* lower starting ports first */
702 res
= compare_int(a
->from_port
, b
->from_port
);
707 /* larger port ranges first */
708 return compare_int(b
->to_port
, a
->to_port
);
714 traffic_selector_t
*traffic_selector_create_from_bytes(uint8_t protocol
,
716 chunk_t from
, uint16_t from_port
,
717 chunk_t to
, uint16_t to_port
)
719 private_traffic_selector_t
*this = traffic_selector_create(protocol
, type
,
726 if (from
.len
!= to
.len
|| from
.len
!= TS_IP_LEN(this))
731 memcpy(this->from
, from
.ptr
, from
.len
);
732 memcpy(this->to
, to
.ptr
, to
.len
);
734 return &this->public;
740 traffic_selector_t
*traffic_selector_create_from_rfc3779_format(ts_type_t type
,
741 chunk_t from
, chunk_t to
)
743 private_traffic_selector_t
*this = traffic_selector_create(0, type
, 0, 65535);
750 len
= TS_IP_LEN(this);
752 memset(this->from
, 0x00, len
);
753 memset(this->to
, 0xff, len
);
757 memcpy(this->from
, from
.ptr
+1, from
.len
-1);
761 uint8_t mask
= to
.ptr
[0] ? (1 << to
.ptr
[0]) - 1 : 0;
763 memcpy(this->to
, to
.ptr
+1, to
.len
-1);
764 this->to
[to
.len
-2] |= mask
;
767 return &this->public;
773 traffic_selector_t
*traffic_selector_create_from_subnet(host_t
*net
,
774 uint8_t netbits
, uint8_t protocol
,
775 uint16_t from_port
, uint16_t to_port
)
777 private_traffic_selector_t
*this;
781 switch (net
->get_family(net
))
784 type
= TS_IPV4_ADDR_RANGE
;
787 type
= TS_IPV6_ADDR_RANGE
;
794 this = traffic_selector_create(protocol
, type
, from_port
, to_port
);
796 from
= net
->get_address(net
);
797 memcpy(this->from
, from
.ptr
, from
.len
);
798 netbits
= min(netbits
, TS_IP_LEN(this) * 8);
799 calc_range(this, netbits
);
801 return &this->public;
807 traffic_selector_t
*traffic_selector_create_from_string(
808 uint8_t protocol
, ts_type_t type
,
809 char *from_addr
, uint16_t from_port
,
810 char *to_addr
, uint16_t to_port
)
812 private_traffic_selector_t
*this;
817 case TS_IPV4_ADDR_RANGE
:
820 case TS_IPV6_ADDR_RANGE
:
827 this = traffic_selector_create(protocol
, type
, from_port
, to_port
);
829 if (inet_pton(family
, from_addr
, this->from
) != 1 ||
830 inet_pton(family
, to_addr
, this->to
) != 1)
836 return &this->public;
842 traffic_selector_t
*traffic_selector_create_from_cidr(
843 char *string
, uint8_t protocol
,
844 uint16_t from_port
, uint16_t to_port
)
849 net
= host_create_from_subnet(string
, &bits
);
852 return traffic_selector_create_from_subnet(net
, bits
, protocol
,
861 traffic_selector_t
*traffic_selector_create_dynamic(uint8_t protocol
,
862 uint16_t from_port
, uint16_t to_port
)
864 private_traffic_selector_t
*this = traffic_selector_create(
865 protocol
, TS_IPV4_ADDR_RANGE
, from_port
, to_port
);
867 memset(this->from
, 0, sizeof(this->from
));
868 memset(this->to
, 0xFF, sizeof(this->to
));
870 this->dynamic
= TRUE
;
872 return &this->public;
878 static private_traffic_selector_t
*traffic_selector_create(uint8_t protocol
,
879 ts_type_t type
, uint16_t from_port
, uint16_t to_port
)
881 private_traffic_selector_t
*this;
884 if (type
!= TS_IPV4_ADDR_RANGE
&& type
!= TS_IPV6_ADDR_RANGE
)
891 .get_subset
= _get_subset
,
893 .get_from_address
= _get_from_address
,
894 .get_to_address
= _get_to_address
,
895 .get_from_port
= _get_from_port
,
896 .get_to_port
= _get_to_port
,
897 .get_type
= _get_type
,
898 .get_protocol
= _get_protocol
,
900 .is_dynamic
= _is_dynamic
,
901 .is_contained_in
= _is_contained_in
,
902 .includes
= _includes
,
903 .set_address
= _set_address
,
904 .to_subnet
= _to_subnet
,
909 .from_port
= from_port
,
911 .protocol
= protocol
,
914 if (protocol
== IPPROTO_ICMP
|| protocol
== IPPROTO_ICMPV6
)
916 this->from_port
= from_port
< 256 ? from_port
<< 8 : from_port
;
917 this->to_port
= to_port
< 256 ? to_port
<< 8 : to_port
;