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 written
+= fprintf(stream
, "dynamic/%d",
199 this->type
== TS_IPV4_ADDR_RANGE ?
32 : 128);
203 if (this->type
== TS_IPV4_ADDR_RANGE
)
205 inet_ntop(AF_INET
, &this->from4
, addr_str
, sizeof(addr_str
));
209 inet_ntop(AF_INET6
, &this->from6
, addr_str
, sizeof(addr_str
));
211 mask
= calc_netbits(this);
212 written
+= fprintf(stream
, "%s/%d", addr_str
, mask
);
215 /* check if we have protocol and/or port selectors */
216 has_proto
= this->protocol
!= 0;
217 has_ports
= !(this->from_port
== 0 && this->to_port
== 0xFFFF);
219 if (!has_proto
&& !has_ports
)
224 written
+= fprintf(stream
, "[");
226 /* build protocol string */
229 struct protoent
*proto
= getprotobynumber(this->protocol
);
233 written
+= fprintf(stream
, "%s", proto
->p_name
);
234 serv_proto
= proto
->p_name
;
238 written
+= fprintf(stream
, "%d", this->protocol
);
242 if (has_proto
&& has_ports
)
244 written
+= fprintf(stream
, "/");
247 /* build port string */
250 if (this->from_port
== this->to_port
)
252 struct servent
*serv
= getservbyport(htons(this->from_port
), serv_proto
);
256 written
+= fprintf(stream
, "%s", serv
->s_name
);
260 written
+= fprintf(stream
, "%d", this->from_port
);
265 written
+= fprintf(stream
, "%d-%d", this->from_port
, this->to_port
);
269 written
+= fprintf(stream
, "]");
275 * arginfo handler for printf() traffic selector
277 static int arginfo(const struct printf_info
*info
, size_t n
, int *argtypes
)
281 argtypes
[0] = PA_POINTER
;
287 * return printf hook functions for a chunk
289 printf_hook_functions_t
traffic_selector_get_printf_hooks()
291 printf_hook_functions_t hooks
= {print
, arginfo
};
297 * implements traffic_selector_t.get_subset
299 static traffic_selector_t
*get_subset(private_traffic_selector_t
*this, private_traffic_selector_t
*other
)
301 if (this->type
== other
->type
&& (this->protocol
== other
->protocol
||
302 this->protocol
== 0 || other
->protocol
== 0))
304 u_int16_t from_port
, to_port
;
308 private_traffic_selector_t
*new_ts
;
310 /* calculate the maximum port range allowed for both */
311 from_port
= max(this->from_port
, other
->from_port
);
312 to_port
= min(this->to_port
, other
->to_port
);
313 if (from_port
> to_port
)
317 /* select protocol, which is not zero */
318 protocol
= max(this->protocol
, other
->protocol
);
322 case TS_IPV4_ADDR_RANGE
:
323 size
= sizeof(this->from4
);
325 case TS_IPV6_ADDR_RANGE
:
326 size
= sizeof(this->from6
);
332 /* get higher from-address */
333 if (memcmp(this->from
, other
->from
, size
) > 0)
341 /* get lower to-address */
342 if (memcmp(this->to
, other
->to
, size
) > 0)
350 /* if "from" > "to", we don't have a match */
351 if (memcmp(from
, to
, size
) > 0)
356 /* we have a match in protocol, port, and address: return it... */
357 new_ts
= traffic_selector_create(protocol
, this->type
, from_port
, to_port
);
358 new_ts
->type
= this->type
;
359 new_ts
->dynamic
= this->dynamic
|| other
->dynamic
;
360 memcpy(new_ts
->from
, from
, size
);
361 memcpy(new_ts
->to
, to
, size
);
363 return &new_ts
->public;
369 * implements traffic_selector_t.equals
371 static bool equals(private_traffic_selector_t
*this, private_traffic_selector_t
*other
)
373 if (this->type
!= other
->type
)
377 if (!(this->from_port
== other
->from_port
&&
378 this->to_port
== other
->to_port
&&
379 this->protocol
== other
->protocol
))
385 case TS_IPV4_ADDR_RANGE
:
386 if (memeq(this->from4
, other
->from4
, sizeof(this->from4
)))
391 case TS_IPV6_ADDR_RANGE
:
392 if (memeq(this->from6
, other
->from6
, sizeof(this->from6
)))
404 * Implements traffic_selector_t.get_from_address.
406 static chunk_t
get_from_address(private_traffic_selector_t
*this)
408 chunk_t from
= chunk_empty
;
412 case TS_IPV4_ADDR_RANGE
:
414 from
.len
= sizeof(this->from4
);
415 from
.ptr
= malloc(from
.len
);
416 memcpy(from
.ptr
, this->from4
, from
.len
);
419 case TS_IPV6_ADDR_RANGE
:
421 from
.len
= sizeof(this->from6
);
422 from
.ptr
= malloc(from
.len
);
423 memcpy(from
.ptr
, this->from6
, from
.len
);
431 * Implements traffic_selector_t.get_to_address.
433 static chunk_t
get_to_address(private_traffic_selector_t
*this)
435 chunk_t to
= chunk_empty
;
439 case TS_IPV4_ADDR_RANGE
:
441 to
.len
= sizeof(this->to4
);
442 to
.ptr
= malloc(to
.len
);
443 memcpy(to
.ptr
, this->to4
, to
.len
);
446 case TS_IPV6_ADDR_RANGE
:
448 to
.len
= sizeof(this->to6
);
449 to
.ptr
= malloc(to
.len
);
450 memcpy(to
.ptr
, this->to6
, to
.len
);
458 * Implements traffic_selector_t.get_from_port.
460 static u_int16_t
get_from_port(private_traffic_selector_t
*this)
462 return this->from_port
;
466 * Implements traffic_selector_t.get_to_port.
468 static u_int16_t
get_to_port(private_traffic_selector_t
*this)
470 return this->to_port
;
474 * Implements traffic_selector_t.get_type.
476 static ts_type_t
get_type(private_traffic_selector_t
*this)
482 * Implements traffic_selector_t.get_protocol.
484 static u_int8_t
get_protocol(private_traffic_selector_t
*this)
486 return this->protocol
;
490 * Implements traffic_selector_t.is_host.
492 static bool is_host(private_traffic_selector_t
*this, host_t
*host
)
497 int family
= host
->get_family(host
);
499 if ((family
== AF_INET
&& this->type
== TS_IPV4_ADDR_RANGE
) ||
500 (family
== AF_INET6
&& this->type
== TS_IPV6_ADDR_RANGE
))
502 addr
= host
->get_address(host
);
503 if (memeq(addr
.ptr
, this->from
, addr
.len
) &&
504 memeq(addr
.ptr
, this->to
, addr
.len
))
512 size_t length
= (this->type
== TS_IPV4_ADDR_RANGE
) ?
4 : 16;
519 if (memeq(this->from
, this->to
, length
))
528 * Implements traffic_selector_t.set_address.
530 static void set_address(private_traffic_selector_t
*this, host_t
*host
)
534 this->type
= host
->get_family(host
) == AF_INET ?
535 TS_IPV4_ADDR_RANGE
: TS_IPV6_ADDR_RANGE
;
537 chunk_t from
= host
->get_address(host
);
538 memcpy(this->from
, from
.ptr
, from
.len
);
539 memcpy(this->to
, from
.ptr
, from
.len
);
544 * Implements traffic_selector_t.is_contained_in.
546 static bool is_contained_in(private_traffic_selector_t
*this,
547 private_traffic_selector_t
*other
)
549 private_traffic_selector_t
*subset
;
550 bool contained_in
= FALSE
;
552 subset
= (private_traffic_selector_t
*)get_subset(this, other
);
556 if (equals(subset
, this))
566 * Implements traffic_selector_t.includes.
568 static bool includes(private_traffic_selector_t
*this, host_t
*host
)
571 int family
= host
->get_family(host
);
573 if ((family
== AF_INET
&& this->type
== TS_IPV4_ADDR_RANGE
) ||
574 (family
== AF_INET6
&& this->type
== TS_IPV6_ADDR_RANGE
))
576 addr
= host
->get_address(host
);
578 return memcmp(this->from
, addr
.ptr
, addr
.len
) <= 0 &&
579 memcmp(this->to
, addr
.ptr
, addr
.len
) >= 0;
586 * Implements traffic_selector_t.clone.
588 static traffic_selector_t
*clone_(private_traffic_selector_t
*this)
590 private_traffic_selector_t
*clone
;
592 clone
= traffic_selector_create(this->protocol
, this->type
,
593 this->from_port
, this->to_port
);
595 clone
->dynamic
= this->dynamic
;
598 case TS_IPV4_ADDR_RANGE
:
600 memcpy(clone
->from4
, this->from4
, sizeof(this->from4
));
601 memcpy(clone
->to4
, this->to4
, sizeof(this->to4
));
602 return &clone
->public;
604 case TS_IPV6_ADDR_RANGE
:
606 memcpy(clone
->from6
, this->from6
, sizeof(this->from6
));
607 memcpy(clone
->to6
, this->to6
, sizeof(this->to6
));
608 return &clone
->public;
613 return &clone
->public;
619 * Implements traffic_selector_t.destroy.
621 static void destroy(private_traffic_selector_t
*this)
629 traffic_selector_t
*traffic_selector_create_from_bytes(u_int8_t protocol
,
631 chunk_t from
, u_int16_t from_port
,
632 chunk_t to
, u_int16_t to_port
)
634 private_traffic_selector_t
*this = traffic_selector_create(protocol
, type
,
639 case TS_IPV4_ADDR_RANGE
:
641 if (from
.len
!= 4 || to
.len
!= 4)
646 memcpy(this->from4
, from
.ptr
, from
.len
);
647 memcpy(this->to4
, to
.ptr
, to
.len
);
650 case TS_IPV6_ADDR_RANGE
:
652 if (from
.len
!= 16 || to
.len
!= 16)
657 memcpy(this->from6
, from
.ptr
, from
.len
);
658 memcpy(this->to6
, to
.ptr
, to
.len
);
667 return (&this->public);
673 traffic_selector_t
*traffic_selector_create_from_subnet(host_t
*net
,
674 u_int8_t netbits
, u_int8_t protocol
, u_int16_t port
)
676 private_traffic_selector_t
*this = traffic_selector_create(protocol
, 0, 0, 65535);
678 switch (net
->get_family(net
))
684 this->type
= TS_IPV4_ADDR_RANGE
;
685 from
= net
->get_address(net
);
686 memcpy(this->from4
, from
.ptr
, from
.len
);
687 if (this->from4
[0] == 0)
689 /* use /0 for 0.0.0.0 */
694 calc_range(this, netbits
);
702 this->type
= TS_IPV6_ADDR_RANGE
;
703 from
= net
->get_address(net
);
704 memcpy(this->from6
, from
.ptr
, from
.len
);
705 if (this->from6
[0] == 0 && this->from6
[1] == 0 &&
706 this->from6
[2] == 0 && this->from6
[3] == 0)
716 calc_range(this, netbits
);
729 this->from_port
= port
;
730 this->to_port
= port
;
733 return (&this->public);
739 traffic_selector_t
*traffic_selector_create_from_string(
740 u_int8_t protocol
, ts_type_t type
,
741 char *from_addr
, u_int16_t from_port
,
742 char *to_addr
, u_int16_t to_port
)
744 private_traffic_selector_t
*this = traffic_selector_create(protocol
, type
,
750 case TS_IPV4_ADDR_RANGE
:
752 if (inet_pton(AF_INET
, from_addr
, (struct in_addr
*)this->from4
) < 0)
757 if (inet_pton(AF_INET
, to_addr
, (struct in_addr
*)this->to4
) < 0)
764 case TS_IPV6_ADDR_RANGE
:
766 if (inet_pton(AF_INET6
, from_addr
, (struct in6_addr
*)this->from6
) < 0)
771 if (inet_pton(AF_INET6
, to_addr
, (struct in6_addr
*)this->to6
) < 0)
779 return (&this->public);
785 traffic_selector_t
*traffic_selector_create_dynamic(u_int8_t protocol
,
786 u_int16_t from_port
, u_int16_t to_port
)
788 private_traffic_selector_t
*this = traffic_selector_create(
789 protocol
, TS_IPV4_ADDR_RANGE
, from_port
, to_port
);
791 memset(this->from6
, 0, sizeof(this->from6
));
792 memset(this->to6
, 0xFF, sizeof(this->to6
));
794 this->dynamic
= TRUE
;
796 return &this->public;
802 static private_traffic_selector_t
*traffic_selector_create(u_int8_t protocol
,
803 ts_type_t type
, u_int16_t from_port
, u_int16_t to_port
)
805 private_traffic_selector_t
*this = malloc_thing(private_traffic_selector_t
);
807 /* public functions */
808 this->public.get_subset
= (traffic_selector_t
*(*)(traffic_selector_t
*,traffic_selector_t
*))get_subset
;
809 this->public.equals
= (bool(*)(traffic_selector_t
*,traffic_selector_t
*))equals
;
810 this->public.get_from_address
= (chunk_t(*)(traffic_selector_t
*))get_from_address
;
811 this->public.get_to_address
= (chunk_t(*)(traffic_selector_t
*))get_to_address
;
812 this->public.get_from_port
= (u_int16_t(*)(traffic_selector_t
*))get_from_port
;
813 this->public.get_to_port
= (u_int16_t(*)(traffic_selector_t
*))get_to_port
;
814 this->public.get_type
= (ts_type_t(*)(traffic_selector_t
*))get_type
;
815 this->public.get_protocol
= (u_int8_t(*)(traffic_selector_t
*))get_protocol
;
816 this->public.is_host
= (bool(*)(traffic_selector_t
*,host_t
*))is_host
;
817 this->public.is_contained_in
= (bool(*)(traffic_selector_t
*,traffic_selector_t
*))is_contained_in
;
818 this->public.includes
= (bool(*)(traffic_selector_t
*,host_t
*))includes
;
819 this->public.set_address
= (void(*)(traffic_selector_t
*,host_t
*))set_address
;
820 this->public.clone
= (traffic_selector_t
*(*)(traffic_selector_t
*))clone_
;
821 this->public.destroy
= (void(*)(traffic_selector_t
*))destroy
;
823 this->from_port
= from_port
;
824 this->to_port
= to_port
;
825 this->protocol
= protocol
;
827 this->dynamic
= FALSE
;