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
);
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
);
298 written
+= print_in_hook(data
, "0");
301 /* build port string */
304 written
+= print_in_hook(data
, "/");
306 if (this->from_port
== this->to_port
)
308 struct servent
*serv
;
310 if (this->protocol
== IPPROTO_ICMP
||
311 this->protocol
== IPPROTO_ICMPV6
)
313 written
+= print_icmp(data
, this->from_port
);
317 serv
= getservbyport(htons(this->from_port
), serv_proto
);
320 written
+= print_in_hook(data
, "%s", serv
->s_name
);
324 written
+= print_in_hook(data
, "%d", this->from_port
);
328 else if (is_opaque(this))
330 written
+= print_in_hook(data
, "OPAQUE");
332 else if (this->protocol
== IPPROTO_ICMP
||
333 this->protocol
== IPPROTO_ICMPV6
)
335 written
+= print_icmp(data
, this->from_port
);
336 written
+= print_in_hook(data
, "-");
337 written
+= print_icmp(data
, this->to_port
);
341 written
+= print_in_hook(data
, "%d-%d",
342 this->from_port
, this->to_port
);
346 written
+= print_in_hook(data
, "]");
351 METHOD(traffic_selector_t
, get_subset
, traffic_selector_t
*,
352 private_traffic_selector_t
*this, traffic_selector_t
*other_public
)
354 private_traffic_selector_t
*other
, *subset
;
355 uint16_t from_port
, to_port
;
360 other
= (private_traffic_selector_t
*)other_public
;
362 if (this->dynamic
|| other
->dynamic
)
363 { /* no set_address() applied, TS has no subset */
367 if (this->type
!= other
->type
)
372 if (this->protocol
!= other
->protocol
&&
373 this->protocol
!= 0 && other
->protocol
!= 0)
377 /* select protocol, which is not zero */
378 protocol
= max(this->protocol
, other
->protocol
);
380 if ((is_opaque(this) && is_opaque(other
)) ||
381 (is_opaque(this) && is_any(other
)) ||
382 (is_opaque(other
) && is_any(this)))
389 /* calculate the maximum port range allowed for both */
390 from_port
= max(this->from_port
, other
->from_port
);
391 to_port
= min(this->to_port
, other
->to_port
);
392 if (from_port
> to_port
)
397 size
= TS_IP_LEN(this);
398 /* get higher from-address */
399 if (memcmp(this->from
, other
->from
, size
) > 0)
407 /* get lower to-address */
408 if (memcmp(this->to
, other
->to
, size
) > 0)
416 /* if "from" > "to", we don't have a match */
417 if (memcmp(from
, to
, size
) > 0)
422 /* we have a match in protocol, port, and address: return it... */
423 subset
= traffic_selector_create(protocol
, this->type
, from_port
, to_port
);
424 memcpy(subset
->from
, from
, size
);
425 memcpy(subset
->to
, to
, size
);
426 calc_netbits(subset
);
428 return &subset
->public;
431 METHOD(traffic_selector_t
, equals
, bool,
432 private_traffic_selector_t
*this, traffic_selector_t
*other
)
434 return traffic_selector_cmp(&this->public, other
, NULL
) == 0;
437 METHOD(traffic_selector_t
, get_from_address
, chunk_t
,
438 private_traffic_selector_t
*this)
440 return chunk_create(this->from
, TS_IP_LEN(this));
443 METHOD(traffic_selector_t
, get_to_address
, chunk_t
,
444 private_traffic_selector_t
*this)
446 return chunk_create(this->to
, TS_IP_LEN(this));
449 METHOD(traffic_selector_t
, get_from_port
, uint16_t,
450 private_traffic_selector_t
*this)
452 return this->from_port
;
455 METHOD(traffic_selector_t
, get_to_port
, uint16_t,
456 private_traffic_selector_t
*this)
458 return this->to_port
;
461 METHOD(traffic_selector_t
, get_type
, ts_type_t
,
462 private_traffic_selector_t
*this)
467 METHOD(traffic_selector_t
, get_protocol
, uint8_t,
468 private_traffic_selector_t
*this)
470 return this->protocol
;
473 METHOD(traffic_selector_t
, is_host
, bool,
474 private_traffic_selector_t
*this, host_t
*host
)
479 int family
= host
->get_family(host
);
481 if ((family
== AF_INET
&& this->type
== TS_IPV4_ADDR_RANGE
) ||
482 (family
== AF_INET6
&& this->type
== TS_IPV6_ADDR_RANGE
))
484 addr
= host
->get_address(host
);
485 if (memeq(addr
.ptr
, this->from
, addr
.len
) &&
486 memeq(addr
.ptr
, this->to
, addr
.len
))
494 size_t length
= TS_IP_LEN(this);
501 if (memeq(this->from
, this->to
, length
))
509 METHOD(traffic_selector_t
, is_dynamic
, bool,
510 private_traffic_selector_t
*this)
512 return this->dynamic
;
515 METHOD(traffic_selector_t
, set_address
, void,
516 private_traffic_selector_t
*this, host_t
*host
)
518 this->type
= host
->get_family(host
) == AF_INET
? TS_IPV4_ADDR_RANGE
519 : TS_IPV6_ADDR_RANGE
;
521 if (host
->is_anyaddr(host
))
523 memset(this->from
, 0x00, sizeof(this->from
));
524 memset(this->to
, 0xFF, sizeof(this->to
));
529 chunk_t from
= host
->get_address(host
);
530 memcpy(this->from
, from
.ptr
, from
.len
);
531 memcpy(this->to
, from
.ptr
, from
.len
);
532 this->netbits
= from
.len
* 8;
534 this->dynamic
= FALSE
;
537 METHOD(traffic_selector_t
, is_contained_in
, bool,
538 private_traffic_selector_t
*this, traffic_selector_t
*other
)
540 private_traffic_selector_t
*subset
;
541 bool contained_in
= FALSE
;
543 subset
= (private_traffic_selector_t
*)get_subset(this, other
);
547 if (equals(subset
, &this->public))
556 METHOD(traffic_selector_t
, includes
, bool,
557 private_traffic_selector_t
*this, host_t
*host
)
560 int family
= host
->get_family(host
);
562 if ((family
== AF_INET
&& this->type
== TS_IPV4_ADDR_RANGE
) ||
563 (family
== AF_INET6
&& this->type
== TS_IPV6_ADDR_RANGE
))
565 addr
= host
->get_address(host
);
567 return memcmp(this->from
, addr
.ptr
, addr
.len
) <= 0 &&
568 memcmp(this->to
, addr
.ptr
, addr
.len
) >= 0;
574 METHOD(traffic_selector_t
, to_subnet
, bool,
575 private_traffic_selector_t
*this, host_t
**net
, uint8_t *mask
)
577 /* there is no way to do this cleanly, as the address range may
578 * be anything else but a subnet. We use from_addr as subnet
579 * and try to calculate a usable subnet mask.
581 int family
, non_zero_bytes
;
585 *mask
= (this->netbits
== NON_SUBNET_ADDRESS_RANGE
) ? calc_netbits(this)
590 case TS_IPV4_ADDR_RANGE
:
592 net_chunk
.len
= IPV4_LEN
;
594 case TS_IPV6_ADDR_RANGE
:
596 net_chunk
.len
= IPV6_LEN
;
603 net_chunk
.ptr
= malloc(net_chunk
.len
);
604 memset(net_chunk
.ptr
, 0x00, net_chunk
.len
);
607 non_zero_bytes
= (*mask
+ 7) / 8;
608 memcpy(net_chunk
.ptr
, this->from
, non_zero_bytes
);
609 net_chunk
.ptr
[non_zero_bytes
-1] &= 0xFF << (8 * non_zero_bytes
- *mask
);
612 if (this->to_port
== this->from_port
)
614 port
= this->to_port
;
617 *net
= host_create_from_chunk(family
, net_chunk
, port
);
618 chunk_free(&net_chunk
);
620 return this->netbits
!= NON_SUBNET_ADDRESS_RANGE
;
623 METHOD(traffic_selector_t
, clone_
, traffic_selector_t
*,
624 private_traffic_selector_t
*this)
626 private_traffic_selector_t
*clone
;
627 size_t len
= TS_IP_LEN(this);
629 clone
= traffic_selector_create(this->protocol
, this->type
,
630 this->from_port
, this->to_port
);
631 clone
->netbits
= this->netbits
;
632 clone
->dynamic
= this->dynamic
;
634 memcpy(clone
->from
, this->from
, len
);
635 memcpy(clone
->to
, this->to
, len
);
636 return &clone
->public;
639 METHOD(traffic_selector_t
, hash
, u_int
,
640 private_traffic_selector_t
*this, u_int hash
)
642 return chunk_hash_inc(get_from_address(this),
643 chunk_hash_inc(get_to_address(this),
644 chunk_hash_inc(chunk_from_thing(this->from_port
),
645 chunk_hash_inc(chunk_from_thing(this->to_port
),
646 chunk_hash_inc(chunk_from_thing(this->protocol
),
650 METHOD(traffic_selector_t
, destroy
, void,
651 private_traffic_selector_t
*this)
657 * Compare two integers
659 static int compare_int(int a
, int b
)
667 int traffic_selector_cmp(traffic_selector_t
*a_pub
, traffic_selector_t
*b_pub
,
670 private_traffic_selector_t
*a
, *b
;
674 a
= (private_traffic_selector_t
*)a_pub
;
675 b
= (private_traffic_selector_t
*)b_pub
;
677 /* IPv4 before IPv6 */
678 res
= compare_int(a
->type
, b
->type
);
684 /* lower starting subnets first */
685 res
= memcmp(a
->from
, b
->from
, len
);
690 /* larger subnets first */
691 res
= memcmp(b
->to
, a
->to
, len
);
696 /* lower protocols first */
697 res
= compare_int(a
->protocol
, b
->protocol
);
702 /* lower starting ports first */
703 res
= compare_int(a
->from_port
, b
->from_port
);
708 /* larger port ranges first */
709 return compare_int(b
->to_port
, a
->to_port
);
715 traffic_selector_t
*traffic_selector_create_from_bytes(uint8_t protocol
,
717 chunk_t from
, uint16_t from_port
,
718 chunk_t to
, uint16_t to_port
)
720 private_traffic_selector_t
*this = traffic_selector_create(protocol
, type
,
727 if (from
.len
!= to
.len
|| from
.len
!= TS_IP_LEN(this))
732 memcpy(this->from
, from
.ptr
, from
.len
);
733 memcpy(this->to
, to
.ptr
, to
.len
);
735 return &this->public;
741 traffic_selector_t
*traffic_selector_create_from_rfc3779_format(ts_type_t type
,
742 chunk_t from
, chunk_t to
)
744 private_traffic_selector_t
*this = traffic_selector_create(0, type
, 0, 65535);
751 len
= TS_IP_LEN(this);
753 memset(this->from
, 0x00, len
);
754 memset(this->to
, 0xff, len
);
758 memcpy(this->from
, from
.ptr
+1, from
.len
-1);
762 uint8_t mask
= to
.ptr
[0] ? (1 << to
.ptr
[0]) - 1 : 0;
764 memcpy(this->to
, to
.ptr
+1, to
.len
-1);
765 this->to
[to
.len
-2] |= mask
;
768 return &this->public;
774 traffic_selector_t
*traffic_selector_create_from_subnet(host_t
*net
,
775 uint8_t netbits
, uint8_t protocol
,
776 uint16_t from_port
, uint16_t to_port
)
778 private_traffic_selector_t
*this;
782 switch (net
->get_family(net
))
785 type
= TS_IPV4_ADDR_RANGE
;
788 type
= TS_IPV6_ADDR_RANGE
;
795 this = traffic_selector_create(protocol
, type
, from_port
, to_port
);
797 from
= net
->get_address(net
);
798 memcpy(this->from
, from
.ptr
, from
.len
);
799 netbits
= min(netbits
, TS_IP_LEN(this) * 8);
800 calc_range(this, netbits
);
802 return &this->public;
808 traffic_selector_t
*traffic_selector_create_from_string(
809 uint8_t protocol
, ts_type_t type
,
810 char *from_addr
, uint16_t from_port
,
811 char *to_addr
, uint16_t to_port
)
813 private_traffic_selector_t
*this;
818 case TS_IPV4_ADDR_RANGE
:
821 case TS_IPV6_ADDR_RANGE
:
828 this = traffic_selector_create(protocol
, type
, from_port
, to_port
);
830 if (inet_pton(family
, from_addr
, this->from
) != 1 ||
831 inet_pton(family
, to_addr
, this->to
) != 1)
837 return &this->public;
843 traffic_selector_t
*traffic_selector_create_from_cidr(
844 char *string
, uint8_t protocol
,
845 uint16_t from_port
, uint16_t to_port
)
850 net
= host_create_from_subnet(string
, &bits
);
853 return traffic_selector_create_from_subnet(net
, bits
, protocol
,
862 traffic_selector_t
*traffic_selector_create_dynamic(uint8_t protocol
,
863 uint16_t from_port
, uint16_t to_port
)
865 private_traffic_selector_t
*this = traffic_selector_create(
866 protocol
, TS_IPV4_ADDR_RANGE
, from_port
, to_port
);
868 memset(this->from
, 0, sizeof(this->from
));
869 memset(this->to
, 0xFF, sizeof(this->to
));
871 this->dynamic
= TRUE
;
873 return &this->public;
879 static private_traffic_selector_t
*traffic_selector_create(uint8_t protocol
,
880 ts_type_t type
, uint16_t from_port
, uint16_t to_port
)
882 private_traffic_selector_t
*this;
885 if (type
!= TS_IPV4_ADDR_RANGE
&& type
!= TS_IPV6_ADDR_RANGE
)
892 .get_subset
= _get_subset
,
894 .get_from_address
= _get_from_address
,
895 .get_to_address
= _get_to_address
,
896 .get_from_port
= _get_from_port
,
897 .get_to_port
= _get_to_port
,
898 .get_type
= _get_type
,
899 .get_protocol
= _get_protocol
,
901 .is_dynamic
= _is_dynamic
,
902 .is_contained_in
= _is_contained_in
,
903 .includes
= _includes
,
904 .set_address
= _set_address
,
905 .to_subnet
= _to_subnet
,
910 .from_port
= from_port
,
912 .protocol
= protocol
,
915 if (protocol
== IPPROTO_ICMP
|| protocol
== IPPROTO_ICMPV6
)
917 this->from_port
= from_port
< 256 ? from_port
<< 8 : from_port
;
918 this->to_port
= to_port
< 256 ? to_port
<< 8 : to_port
;