this->type is set by traffic_selector_create()
[strongswan.git] / src / libstrongswan / selectors / traffic_selector.c
1 /*
2 * Copyright (C) 2007-2009 Tobias Brunner
3 * Copyright (C) 2005-2007 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
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>.
11 *
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
15 * for more details.
16 */
17
18 #include <arpa/inet.h>
19 #include <string.h>
20 #include <netdb.h>
21 #include <stdio.h>
22
23 #include "traffic_selector.h"
24
25 #include <utils/linked_list.h>
26 #include <utils/identification.h>
27
28 ENUM(ts_type_name, TS_IPV4_ADDR_RANGE, TS_IPV6_ADDR_RANGE,
29 "TS_IPV4_ADDR_RANGE",
30 "TS_IPV6_ADDR_RANGE",
31 );
32
33 typedef struct private_traffic_selector_t private_traffic_selector_t;
34
35 /**
36 * Private data of an traffic_selector_t object
37 */
38 struct private_traffic_selector_t {
39
40 /**
41 * Public part
42 */
43 traffic_selector_t public;
44
45 /**
46 * Type of address
47 */
48 ts_type_t type;
49
50 /**
51 * IP protocol (UDP, TCP, ICMP, ...)
52 */
53 u_int8_t protocol;
54
55 /**
56 * narrow this traffic selector to hosts external ip
57 * if set, from and to have no meaning until set_address() is called
58 */
59 bool dynamic;
60
61 /**
62 * begin of address range, network order
63 */
64 union {
65 /** dummy char for common address manipulation */
66 char from[0];
67 /** IPv4 address */
68 u_int32_t from4[1];
69 /** IPv6 address */
70 u_int32_t from6[4];
71 };
72
73 /**
74 * end of address range, network order
75 */
76 union {
77 /** dummy char for common address manipulation */
78 char to[0];
79 /** IPv4 address */
80 u_int32_t to4[1];
81 /** IPv6 address */
82 u_int32_t to6[4];
83 };
84
85 /**
86 * begin of port range
87 */
88 u_int16_t from_port;
89
90 /**
91 * end of port range
92 */
93 u_int16_t to_port;
94 };
95
96 /**
97 * calculate to "to"-address for the "from" address and a subnet size
98 */
99 static void calc_range(private_traffic_selector_t *this, u_int8_t netbits)
100 {
101 int byte;
102 size_t size = (this->type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
103
104 /* go through the from address, starting at the tail. While we
105 * have not processed the bits belonging to the host, set them to 1 on
106 * the to address. If we reach the bits for the net, copy them from "from". */
107 for (byte = size - 1; byte >=0; byte--)
108 {
109 u_char mask = 0x00;
110 int shift;
111
112 shift = (byte+1) * 8 - netbits;
113 if (shift > 0)
114 {
115 mask = 1 << shift;
116 if (mask != 0xFF)
117 {
118 mask--;
119 }
120 }
121 this->from[byte] &= ~mask;
122 this->to[byte] = this->from[byte] | mask;
123 }
124 }
125
126 /**
127 * calculate to subnet size from "to"- and "from"-address
128 */
129 static u_int8_t calc_netbits(private_traffic_selector_t *this)
130 {
131 int byte, bit;
132 size_t size = (this->type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
133
134 /* go trough all bits of the addresses, beginning in the front.
135 * as long as they are equal, the subnet gets larger
136 */
137 for (byte = 0; byte < size; byte++)
138 {
139 for (bit = 7; bit >= 0; bit--)
140 {
141 if ((1<<bit & this->from[byte]) != (1<<bit & this->to[byte]))
142 {
143 return ((7 - bit) + (byte * 8));
144 }
145 }
146 }
147 /* single host, netmask is 32/128 */
148 return (size * 8);
149 }
150
151 /**
152 * internal generic constructor
153 */
154 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);
155
156 /**
157 * Described in header.
158 */
159 int traffic_selector_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
160 const void *const *args)
161 {
162 private_traffic_selector_t *this = *((private_traffic_selector_t**)(args[0]));
163 linked_list_t *list = *((linked_list_t**)(args[0]));
164 iterator_t *iterator;
165 char addr_str[INET6_ADDRSTRLEN] = "";
166 char *serv_proto = NULL;
167 u_int8_t mask;
168 bool has_proto;
169 bool has_ports;
170 size_t written = 0;
171 u_int32_t from[4], to[4];
172
173 if (this == NULL)
174 {
175 return print_in_hook(dst, len, "(null)");
176 }
177
178 if (spec->hash)
179 {
180 iterator = list->create_iterator(list, TRUE);
181 while (iterator->iterate(iterator, (void**)&this))
182 {
183 /* call recursivly */
184 written += print_in_hook(dst, len, "%R ", this);
185 }
186 iterator->destroy(iterator);
187 return written;
188 }
189
190 memset(from, 0, sizeof(from));
191 memset(to, 0xFF, sizeof(to));
192 if (this->dynamic &&
193 memeq(this->from, from, this->type == TS_IPV4_ADDR_RANGE ? 4 : 16) &&
194 memeq(this->to, to, this->type == TS_IPV4_ADDR_RANGE ? 4 : 16))
195 {
196 written += print_in_hook(dst, len, "dynamic");
197 }
198 else
199 {
200 if (this->type == TS_IPV4_ADDR_RANGE)
201 {
202 inet_ntop(AF_INET, &this->from4, addr_str, sizeof(addr_str));
203 }
204 else
205 {
206 inet_ntop(AF_INET6, &this->from6, addr_str, sizeof(addr_str));
207 }
208 mask = calc_netbits(this);
209 written += print_in_hook(dst, len, "%s/%d", addr_str, mask);
210 }
211
212 /* check if we have protocol and/or port selectors */
213 has_proto = this->protocol != 0;
214 has_ports = !(this->from_port == 0 && this->to_port == 0xFFFF);
215
216 if (!has_proto && !has_ports)
217 {
218 return written;
219 }
220
221 written += print_in_hook(dst, len, "[");
222
223 /* build protocol string */
224 if (has_proto)
225 {
226 struct protoent *proto = getprotobynumber(this->protocol);
227
228 if (proto)
229 {
230 written += print_in_hook(dst, len, "%s", proto->p_name);
231 serv_proto = proto->p_name;
232 }
233 else
234 {
235 written += print_in_hook(dst, len, "%d", this->protocol);
236 }
237 }
238
239 if (has_proto && has_ports)
240 {
241 written += print_in_hook(dst, len, "/");
242 }
243
244 /* build port string */
245 if (has_ports)
246 {
247 if (this->from_port == this->to_port)
248 {
249 struct servent *serv = getservbyport(htons(this->from_port), serv_proto);
250
251 if (serv)
252 {
253 written += print_in_hook(dst, len, "%s", serv->s_name);
254 }
255 else
256 {
257 written += print_in_hook(dst, len, "%d", this->from_port);
258 }
259 }
260 else
261 {
262 written += print_in_hook(dst, len, "%d-%d", this->from_port, this->to_port);
263 }
264 }
265
266 written += print_in_hook(dst, len, "]");
267
268 return written;
269 }
270
271 /**
272 * implements traffic_selector_t.get_subset
273 */
274 static traffic_selector_t *get_subset(private_traffic_selector_t *this, private_traffic_selector_t *other)
275 {
276 if (this->type == other->type && (this->protocol == other->protocol ||
277 this->protocol == 0 || other->protocol == 0))
278 {
279 u_int16_t from_port, to_port;
280 u_char *from, *to;
281 u_int8_t protocol;
282 size_t size;
283 private_traffic_selector_t *new_ts;
284
285 /* calculate the maximum port range allowed for both */
286 from_port = max(this->from_port, other->from_port);
287 to_port = min(this->to_port, other->to_port);
288 if (from_port > to_port)
289 {
290 return NULL;
291 }
292 /* select protocol, which is not zero */
293 protocol = max(this->protocol, other->protocol);
294
295 switch (this->type)
296 {
297 case TS_IPV4_ADDR_RANGE:
298 size = sizeof(this->from4);
299 break;
300 case TS_IPV6_ADDR_RANGE:
301 size = sizeof(this->from6);
302 break;
303 default:
304 return NULL;
305 }
306
307 /* get higher from-address */
308 if (memcmp(this->from, other->from, size) > 0)
309 {
310 from = this->from;
311 }
312 else
313 {
314 from = other->from;
315 }
316 /* get lower to-address */
317 if (memcmp(this->to, other->to, size) > 0)
318 {
319 to = other->to;
320 }
321 else
322 {
323 to = this->to;
324 }
325 /* if "from" > "to", we don't have a match */
326 if (memcmp(from, to, size) > 0)
327 {
328 return NULL;
329 }
330
331 /* we have a match in protocol, port, and address: return it... */
332 new_ts = traffic_selector_create(protocol, this->type, from_port, to_port);
333 new_ts->dynamic = this->dynamic || other->dynamic;
334 memcpy(new_ts->from, from, size);
335 memcpy(new_ts->to, to, size);
336
337 return &new_ts->public;
338 }
339 return NULL;
340 }
341
342 /**
343 * implements traffic_selector_t.equals
344 */
345 static bool equals(private_traffic_selector_t *this, private_traffic_selector_t *other)
346 {
347 if (this->type != other->type)
348 {
349 return FALSE;
350 }
351 if (!(this->from_port == other->from_port &&
352 this->to_port == other->to_port &&
353 this->protocol == other->protocol))
354 {
355 return FALSE;
356 }
357 switch (this->type)
358 {
359 case TS_IPV4_ADDR_RANGE:
360 if (memeq(this->from4, other->from4, sizeof(this->from4)))
361 {
362 return TRUE;
363 }
364 break;
365 case TS_IPV6_ADDR_RANGE:
366 if (memeq(this->from6, other->from6, sizeof(this->from6)))
367 {
368 return TRUE;
369 }
370 break;
371 default:
372 break;
373 }
374 return FALSE;
375 }
376
377 /**
378 * Implements traffic_selector_t.get_from_address.
379 */
380 static chunk_t get_from_address(private_traffic_selector_t *this)
381 {
382 switch (this->type)
383 {
384 case TS_IPV4_ADDR_RANGE:
385 return chunk_create(this->from, sizeof(this->from4));
386 case TS_IPV6_ADDR_RANGE:
387 return chunk_create(this->from, sizeof(this->from6));
388 default:
389 return chunk_empty;
390 }
391 }
392
393 /**
394 * Implements traffic_selector_t.get_to_address.
395 */
396 static chunk_t get_to_address(private_traffic_selector_t *this)
397 {
398 switch (this->type)
399 {
400 case TS_IPV4_ADDR_RANGE:
401 return chunk_create(this->to, sizeof(this->to4));
402 case TS_IPV6_ADDR_RANGE:
403 return chunk_create(this->to, sizeof(this->to6));
404 default:
405 return chunk_empty;
406 }
407 }
408
409 /**
410 * Implements traffic_selector_t.get_from_port.
411 */
412 static u_int16_t get_from_port(private_traffic_selector_t *this)
413 {
414 return this->from_port;
415 }
416
417 /**
418 * Implements traffic_selector_t.get_to_port.
419 */
420 static u_int16_t get_to_port(private_traffic_selector_t *this)
421 {
422 return this->to_port;
423 }
424
425 /**
426 * Implements traffic_selector_t.get_type.
427 */
428 static ts_type_t get_type(private_traffic_selector_t *this)
429 {
430 return this->type;
431 }
432
433 /**
434 * Implements traffic_selector_t.get_protocol.
435 */
436 static u_int8_t get_protocol(private_traffic_selector_t *this)
437 {
438 return this->protocol;
439 }
440
441 /**
442 * Implements traffic_selector_t.is_host.
443 */
444 static bool is_host(private_traffic_selector_t *this, host_t *host)
445 {
446 if (host)
447 {
448 chunk_t addr;
449 int family = host->get_family(host);
450
451 if ((family == AF_INET && this->type == TS_IPV4_ADDR_RANGE) ||
452 (family == AF_INET6 && this->type == TS_IPV6_ADDR_RANGE))
453 {
454 addr = host->get_address(host);
455 if (memeq(addr.ptr, this->from, addr.len) &&
456 memeq(addr.ptr, this->to, addr.len))
457 {
458 return TRUE;
459 }
460 }
461 }
462 else
463 {
464 size_t length = (this->type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
465
466 if (this->dynamic)
467 {
468 return TRUE;
469 }
470
471 if (memeq(this->from, this->to, length))
472 {
473 return TRUE;
474 }
475 }
476 return FALSE;
477 }
478
479 /**
480 * Implementation of traffic_selector_t.is_dynamic
481 */
482 static bool is_dynamic(private_traffic_selector_t *this)
483 {
484 return this->dynamic;
485 }
486
487 /**
488 * Implements traffic_selector_t.set_address.
489 */
490 static void set_address(private_traffic_selector_t *this, host_t *host)
491 {
492 if (this->dynamic)
493 {
494 this->type = host->get_family(host) == AF_INET ?
495 TS_IPV4_ADDR_RANGE : TS_IPV6_ADDR_RANGE;
496
497 if (host->is_anyaddr(host))
498 {
499 memset(this->from6, 0x00, sizeof(this->from6));
500 memset(this->to6, 0xFF, sizeof(this->to6));
501 }
502 else
503 {
504 chunk_t from = host->get_address(host);
505 memcpy(this->from, from.ptr, from.len);
506 memcpy(this->to, from.ptr, from.len);
507 }
508 }
509 }
510
511 /**
512 * Implements traffic_selector_t.is_contained_in.
513 */
514 static bool is_contained_in(private_traffic_selector_t *this,
515 private_traffic_selector_t *other)
516 {
517 private_traffic_selector_t *subset;
518 bool contained_in = FALSE;
519
520 subset = (private_traffic_selector_t*)get_subset(this, other);
521
522 if (subset)
523 {
524 if (equals(subset, this))
525 {
526 contained_in = TRUE;
527 }
528 free(subset);
529 }
530 return contained_in;
531 }
532
533 /**
534 * Implements traffic_selector_t.includes.
535 */
536 static bool includes(private_traffic_selector_t *this, host_t *host)
537 {
538 chunk_t addr;
539 int family = host->get_family(host);
540
541 if ((family == AF_INET && this->type == TS_IPV4_ADDR_RANGE) ||
542 (family == AF_INET6 && this->type == TS_IPV6_ADDR_RANGE))
543 {
544 addr = host->get_address(host);
545
546 return memcmp(this->from, addr.ptr, addr.len) <= 0 &&
547 memcmp(this->to, addr.ptr, addr.len) >= 0;
548 }
549
550 return FALSE;
551 }
552
553 /**
554 * Implements traffic_selector_t.to_subnet.
555 */
556 static void to_subnet(private_traffic_selector_t *this, host_t **net, u_int8_t *mask)
557 {
558 /* there is no way to do this cleanly, as the address range may
559 * be anything else but a subnet. We use from_addr as subnet
560 * and try to calculate a usable subnet mask.
561 */
562 int family, byte;
563 u_int16_t port = 0;
564 chunk_t net_chunk;
565
566 *mask = calc_netbits(this);
567
568 switch (this->type)
569 {
570 case TS_IPV4_ADDR_RANGE:
571 {
572 family = AF_INET;
573 net_chunk.len = sizeof(this->from4);
574 break;
575 }
576 case TS_IPV6_ADDR_RANGE:
577 {
578 family = AF_INET6;
579 net_chunk.len = sizeof(this->from6);
580 break;
581 }
582 default:
583 {
584 /* unreachable */
585 return;
586 }
587 }
588
589 net_chunk.ptr = malloc(net_chunk.len);
590 memcpy(net_chunk.ptr, this->from, net_chunk.len);
591
592 for (byte = net_chunk.len - 1; byte >= (*mask / 8); --byte)
593 {
594 int shift = (byte + 1) * 8 - *mask;
595 net_chunk.ptr[byte] = net_chunk.ptr[byte] & (0xFF << shift);
596 }
597
598 if (this->to_port == this->from_port)
599 {
600 port = this->to_port;
601 }
602
603 *net = host_create_from_chunk(family, net_chunk, port);
604 chunk_free(&net_chunk);
605 }
606
607 /**
608 * Implements traffic_selector_t.clone.
609 */
610 static traffic_selector_t *clone_(private_traffic_selector_t *this)
611 {
612 private_traffic_selector_t *clone;
613
614 clone = traffic_selector_create(this->protocol, this->type,
615 this->from_port, this->to_port);
616
617 clone->dynamic = this->dynamic;
618 switch (clone->type)
619 {
620 case TS_IPV4_ADDR_RANGE:
621 {
622 memcpy(clone->from4, this->from4, sizeof(this->from4));
623 memcpy(clone->to4, this->to4, sizeof(this->to4));
624 return &clone->public;
625 }
626 case TS_IPV6_ADDR_RANGE:
627 {
628 memcpy(clone->from6, this->from6, sizeof(this->from6));
629 memcpy(clone->to6, this->to6, sizeof(this->to6));
630 return &clone->public;
631 }
632 default:
633 {
634 /* unreachable */
635 return &clone->public;
636 }
637 }
638 }
639
640 /**
641 * Implements traffic_selector_t.destroy.
642 */
643 static void destroy(private_traffic_selector_t *this)
644 {
645 free(this);
646 }
647
648 /*
649 * see header
650 */
651 traffic_selector_t *traffic_selector_create_from_bytes(u_int8_t protocol,
652 ts_type_t type,
653 chunk_t from, u_int16_t from_port,
654 chunk_t to, u_int16_t to_port)
655 {
656 private_traffic_selector_t *this = traffic_selector_create(protocol, type,
657 from_port, to_port);
658
659 switch (type)
660 {
661 case TS_IPV4_ADDR_RANGE:
662 {
663 if (from.len != 4 || to.len != 4)
664 {
665 free(this);
666 return NULL;
667 }
668 memcpy(this->from4, from.ptr, from.len);
669 memcpy(this->to4, to.ptr, to.len);
670 break;
671 }
672 case TS_IPV6_ADDR_RANGE:
673 {
674 if (from.len != 16 || to.len != 16)
675 {
676 free(this);
677 return NULL;
678 }
679 memcpy(this->from6, from.ptr, from.len);
680 memcpy(this->to6, to.ptr, to.len);
681 break;
682 }
683 default:
684 {
685 free(this);
686 return NULL;
687 }
688 }
689 return (&this->public);
690 }
691
692 /*
693 * see header
694 */
695 traffic_selector_t *traffic_selector_create_from_subnet(host_t *net,
696 u_int8_t netbits, u_int8_t protocol, u_int16_t port)
697 {
698 private_traffic_selector_t *this = traffic_selector_create(protocol, 0, 0, 65535);
699
700 switch (net->get_family(net))
701 {
702 case AF_INET:
703 {
704 chunk_t from;
705
706 this->type = TS_IPV4_ADDR_RANGE;
707 from = net->get_address(net);
708 memcpy(this->from4, from.ptr, from.len);
709 if (this->from4[0] == 0)
710 {
711 /* use /0 for 0.0.0.0 */
712 this->to4[0] = ~0;
713 }
714 else
715 {
716 calc_range(this, netbits);
717 }
718 break;
719 }
720 case AF_INET6:
721 {
722 chunk_t from;
723
724 this->type = TS_IPV6_ADDR_RANGE;
725 from = net->get_address(net);
726 memcpy(this->from6, from.ptr, from.len);
727 if (this->from6[0] == 0 && this->from6[1] == 0 &&
728 this->from6[2] == 0 && this->from6[3] == 0)
729 {
730 /* use /0 for ::0 */
731 this->to6[0] = ~0;
732 this->to6[1] = ~0;
733 this->to6[2] = ~0;
734 this->to6[3] = ~0;
735 }
736 else
737 {
738 calc_range(this, netbits);
739 }
740 break;
741 }
742 default:
743 {
744 net->destroy(net);
745 free(this);
746 return NULL;
747 }
748 }
749 if (port)
750 {
751 this->from_port = port;
752 this->to_port = port;
753 }
754 net->destroy(net);
755 return (&this->public);
756 }
757
758 /*
759 * see header
760 */
761 traffic_selector_t *traffic_selector_create_from_string(
762 u_int8_t protocol, ts_type_t type,
763 char *from_addr, u_int16_t from_port,
764 char *to_addr, u_int16_t to_port)
765 {
766 private_traffic_selector_t *this = traffic_selector_create(protocol, type,
767 from_port, to_port);
768
769 switch (type)
770 {
771 case TS_IPV4_ADDR_RANGE:
772 {
773 if (inet_pton(AF_INET, from_addr, (struct in_addr*)this->from4) < 0)
774 {
775 free(this);
776 return NULL;
777 }
778 if (inet_pton(AF_INET, to_addr, (struct in_addr*)this->to4) < 0)
779 {
780 free(this);
781 return NULL;
782 }
783 break;
784 }
785 case TS_IPV6_ADDR_RANGE:
786 {
787 if (inet_pton(AF_INET6, from_addr, (struct in6_addr*)this->from6) < 0)
788 {
789 free(this);
790 return NULL;
791 }
792 if (inet_pton(AF_INET6, to_addr, (struct in6_addr*)this->to6) < 0)
793 {
794 free(this);
795 return NULL;
796 }
797 break;
798 }
799 }
800 return (&this->public);
801 }
802
803 /*
804 * see header
805 */
806 traffic_selector_t *traffic_selector_create_dynamic(u_int8_t protocol,
807 u_int16_t from_port, u_int16_t to_port)
808 {
809 private_traffic_selector_t *this = traffic_selector_create(
810 protocol, TS_IPV4_ADDR_RANGE, from_port, to_port);
811
812 memset(this->from6, 0, sizeof(this->from6));
813 memset(this->to6, 0xFF, sizeof(this->to6));
814
815 this->dynamic = TRUE;
816
817 return &this->public;
818 }
819
820 /*
821 * see declaration
822 */
823 static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol,
824 ts_type_t type, u_int16_t from_port, u_int16_t to_port)
825 {
826 private_traffic_selector_t *this = malloc_thing(private_traffic_selector_t);
827
828 /* public functions */
829 this->public.get_subset = (traffic_selector_t*(*)(traffic_selector_t*,traffic_selector_t*))get_subset;
830 this->public.equals = (bool(*)(traffic_selector_t*,traffic_selector_t*))equals;
831 this->public.get_from_address = (chunk_t(*)(traffic_selector_t*))get_from_address;
832 this->public.get_to_address = (chunk_t(*)(traffic_selector_t*))get_to_address;
833 this->public.get_from_port = (u_int16_t(*)(traffic_selector_t*))get_from_port;
834 this->public.get_to_port = (u_int16_t(*)(traffic_selector_t*))get_to_port;
835 this->public.get_type = (ts_type_t(*)(traffic_selector_t*))get_type;
836 this->public.get_protocol = (u_int8_t(*)(traffic_selector_t*))get_protocol;
837 this->public.is_host = (bool(*)(traffic_selector_t*,host_t*))is_host;
838 this->public.is_dynamic = (bool(*)(traffic_selector_t*))is_dynamic;
839 this->public.is_contained_in = (bool(*)(traffic_selector_t*,traffic_selector_t*))is_contained_in;
840 this->public.includes = (bool(*)(traffic_selector_t*,host_t*))includes;
841 this->public.set_address = (void(*)(traffic_selector_t*,host_t*))set_address;
842 this->public.to_subnet = (void(*)(traffic_selector_t*,host_t**,u_int8_t*))to_subnet;
843 this->public.clone = (traffic_selector_t*(*)(traffic_selector_t*))clone_;
844 this->public.destroy = (void(*)(traffic_selector_t*))destroy;
845
846 this->from_port = from_port;
847 this->to_port = to_port;
848 this->protocol = protocol;
849 this->type = type;
850 this->dynamic = FALSE;
851
852 return this;
853 }
854