c2b0734188dbf7696814bee3d031a9163b2bc958
[strongswan.git] / src / libcharon / plugins / socket_default / socket_default_socket.c
1 /*
2 * Copyright (C) 2006-2012 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2010 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 /* for struct in6_pktinfo */
20 #define _GNU_SOURCE
21 #ifdef __sun
22 #define _XPG4_2
23 #define __EXTENSIONS__
24 #endif
25 /* make sure to use the proper defs on Mac OS X */
26 #define __APPLE_USE_RFC_3542
27
28 #include "socket_default_socket.h"
29
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <fcntl.h>
37 #include <sys/ioctl.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/in.h>
40 #include <netinet/ip.h>
41 #include <netinet/udp.h>
42 #include <net/if.h>
43
44 #include <hydra.h>
45 #include <daemon.h>
46 #include <threading/thread.h>
47
48 /* Maximum size of a packet */
49 #define MAX_PACKET 10000
50
51 /* these are not defined on some platforms */
52 #ifndef SOL_IP
53 #define SOL_IP IPPROTO_IP
54 #endif
55 #ifndef SOL_IPV6
56 #define SOL_IPV6 IPPROTO_IPV6
57 #endif
58
59 /* IPV6_RECVPKTINFO is defined in RFC 3542 which obsoletes RFC 2292 that
60 * previously defined IPV6_PKTINFO */
61 #ifndef IPV6_RECVPKTINFO
62 #define IPV6_RECVPKTINFO IPV6_PKTINFO
63 #endif
64
65 #ifndef IN6ADDR_ANY_INIT
66 #define IN6ADDR_ANY_INIT {{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}}
67 #endif
68
69 #ifndef HAVE_IN6ADDR_ANY
70 static const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
71 #endif
72
73 typedef struct private_socket_default_socket_t private_socket_default_socket_t;
74
75 /**
76 * Private data of an socket_t object
77 */
78 struct private_socket_default_socket_t {
79
80 /**
81 * public functions
82 */
83 socket_default_socket_t public;
84
85 /**
86 * Configured port (or random, if initially 0)
87 */
88 u_int16_t port;
89
90 /**
91 * Configured port for NAT-T (or random, if initially 0)
92 */
93 u_int16_t natt;
94
95 /**
96 * IPv4 socket (500 or port)
97 */
98 int ipv4;
99
100 /**
101 * IPv4 socket for NAT-T (4500 or natt)
102 */
103 int ipv4_natt;
104
105 /**
106 * IPv6 socket (500 or port)
107 */
108 int ipv6;
109
110 /**
111 * IPv6 socket for NAT-T (4500 or natt)
112 */
113 int ipv6_natt;
114
115 /**
116 * Maximum packet size to receive
117 */
118 int max_packet;
119
120 /**
121 * TRUE if the source address should be set on outbound packets
122 */
123 bool set_source;
124 };
125
126 METHOD(socket_t, receiver, status_t,
127 private_socket_default_socket_t *this, packet_t **packet)
128 {
129 char buffer[this->max_packet];
130 chunk_t data;
131 packet_t *pkt;
132 host_t *source = NULL, *dest = NULL;
133 int bytes_read = 0;
134 bool oldstate;
135
136 fd_set rfds;
137 int max_fd = 0, selected = 0;
138 u_int16_t port = 0;
139
140 FD_ZERO(&rfds);
141
142 if (this->ipv4)
143 {
144 FD_SET(this->ipv4, &rfds);
145 }
146 if (this->ipv4_natt)
147 {
148 FD_SET(this->ipv4_natt, &rfds);
149 }
150 if (this->ipv6)
151 {
152 FD_SET(this->ipv6, &rfds);
153 }
154 if (this->ipv6_natt)
155 {
156 FD_SET(this->ipv6_natt, &rfds);
157 }
158 max_fd = max(max(this->ipv4, this->ipv4_natt), max(this->ipv6, this->ipv6_natt));
159
160 DBG2(DBG_NET, "waiting for data on sockets");
161 oldstate = thread_cancelability(TRUE);
162 if (select(max_fd + 1, &rfds, NULL, NULL, NULL) <= 0)
163 {
164 thread_cancelability(oldstate);
165 return FAILED;
166 }
167 thread_cancelability(oldstate);
168
169 if (FD_ISSET(this->ipv4, &rfds))
170 {
171 port = this->port;
172 selected = this->ipv4;
173 }
174 if (FD_ISSET(this->ipv4_natt, &rfds))
175 {
176 port = this->natt;
177 selected = this->ipv4_natt;
178 }
179 if (FD_ISSET(this->ipv6, &rfds))
180 {
181 port = this->port;
182 selected = this->ipv6;
183 }
184 if (FD_ISSET(this->ipv6_natt, &rfds))
185 {
186 port = this->natt;
187 selected = this->ipv6_natt;
188 }
189 if (selected)
190 {
191 struct msghdr msg;
192 struct cmsghdr *cmsgptr;
193 struct iovec iov;
194 char ancillary[64];
195 union {
196 struct sockaddr_in in4;
197 struct sockaddr_in6 in6;
198 } src;
199
200 msg.msg_name = &src;
201 msg.msg_namelen = sizeof(src);
202 iov.iov_base = buffer;
203 iov.iov_len = this->max_packet;
204 msg.msg_iov = &iov;
205 msg.msg_iovlen = 1;
206 msg.msg_control = ancillary;
207 msg.msg_controllen = sizeof(ancillary);
208 msg.msg_flags = 0;
209 bytes_read = recvmsg(selected, &msg, 0);
210 if (bytes_read < 0)
211 {
212 DBG1(DBG_NET, "error reading socket: %s", strerror(errno));
213 return FAILED;
214 }
215 if (msg.msg_flags & MSG_TRUNC)
216 {
217 DBG1(DBG_NET, "receive buffer too small, packet discarded");
218 return FAILED;
219 }
220 DBG3(DBG_NET, "received packet %b", buffer, bytes_read);
221
222 /* read ancillary data to get destination address */
223 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
224 cmsgptr = CMSG_NXTHDR(&msg, cmsgptr))
225 {
226 if (cmsgptr->cmsg_len == 0)
227 {
228 DBG1(DBG_NET, "error reading ancillary data");
229 return FAILED;
230 }
231
232 #ifdef HAVE_IN6_PKTINFO
233 if (cmsgptr->cmsg_level == SOL_IPV6 &&
234 cmsgptr->cmsg_type == IPV6_PKTINFO)
235 {
236 struct in6_pktinfo *pktinfo;
237 pktinfo = (struct in6_pktinfo*)CMSG_DATA(cmsgptr);
238 struct sockaddr_in6 dst;
239
240 memset(&dst, 0, sizeof(dst));
241 memcpy(&dst.sin6_addr, &pktinfo->ipi6_addr, sizeof(dst.sin6_addr));
242 dst.sin6_family = AF_INET6;
243 dst.sin6_port = htons(port);
244 dest = host_create_from_sockaddr((sockaddr_t*)&dst);
245 }
246 #endif /* HAVE_IN6_PKTINFO */
247 if (cmsgptr->cmsg_level == SOL_IP &&
248 #ifdef IP_PKTINFO
249 cmsgptr->cmsg_type == IP_PKTINFO
250 #elif defined(IP_RECVDSTADDR)
251 cmsgptr->cmsg_type == IP_RECVDSTADDR
252 #else
253 FALSE
254 #endif
255 )
256 {
257 struct in_addr *addr;
258 struct sockaddr_in dst;
259
260 #ifdef IP_PKTINFO
261 struct in_pktinfo *pktinfo;
262 pktinfo = (struct in_pktinfo*)CMSG_DATA(cmsgptr);
263 addr = &pktinfo->ipi_addr;
264 #elif defined(IP_RECVDSTADDR)
265 addr = (struct in_addr*)CMSG_DATA(cmsgptr);
266 #endif
267 memset(&dst, 0, sizeof(dst));
268 memcpy(&dst.sin_addr, addr, sizeof(dst.sin_addr));
269
270 dst.sin_family = AF_INET;
271 dst.sin_port = htons(port);
272 dest = host_create_from_sockaddr((sockaddr_t*)&dst);
273 }
274 if (dest)
275 {
276 break;
277 }
278 }
279 if (dest == NULL)
280 {
281 DBG1(DBG_NET, "error reading IP header");
282 return FAILED;
283 }
284 source = host_create_from_sockaddr((sockaddr_t*)&src);
285
286 pkt = packet_create();
287 pkt->set_source(pkt, source);
288 pkt->set_destination(pkt, dest);
289 DBG2(DBG_NET, "received packet: from %#H to %#H", source, dest);
290 data = chunk_create(buffer, bytes_read);
291 pkt->set_data(pkt, chunk_clone(data));
292 }
293 else
294 {
295 /* oops, shouldn't happen */
296 return FAILED;
297 }
298 /* return packet */
299 *packet = pkt;
300 return SUCCESS;
301 }
302
303 METHOD(socket_t, sender, status_t,
304 private_socket_default_socket_t *this, packet_t *packet)
305 {
306 int sport, skt, family;
307 ssize_t bytes_sent;
308 chunk_t data;
309 host_t *src, *dst;
310 struct msghdr msg;
311 struct cmsghdr *cmsg;
312 struct iovec iov;
313
314 src = packet->get_source(packet);
315 dst = packet->get_destination(packet);
316 data = packet->get_data(packet);
317
318 DBG2(DBG_NET, "sending packet: from %#H to %#H", src, dst);
319
320 /* send data */
321 sport = src->get_port(src);
322 family = dst->get_family(dst);
323 if (sport == 0 || sport == this->port)
324 {
325 if (family == AF_INET)
326 {
327 skt = this->ipv4;
328 }
329 else
330 {
331 skt = this->ipv6;
332 }
333 }
334 else if (sport == this->natt)
335 {
336 if (family == AF_INET)
337 {
338 skt = this->ipv4_natt;
339 }
340 else
341 {
342 skt = this->ipv6_natt;
343 }
344 }
345 else
346 {
347 DBG1(DBG_NET, "unable to locate a send socket for port %d", sport);
348 return FAILED;
349 }
350
351 memset(&msg, 0, sizeof(struct msghdr));
352 msg.msg_name = dst->get_sockaddr(dst);;
353 msg.msg_namelen = *dst->get_sockaddr_len(dst);
354 iov.iov_base = data.ptr;
355 iov.iov_len = data.len;
356 msg.msg_iov = &iov;
357 msg.msg_iovlen = 1;
358 msg.msg_flags = 0;
359
360 if (this->set_source && !src->is_anyaddr(src))
361 {
362 if (family == AF_INET)
363 {
364 #if defined(IP_PKTINFO) || defined(IP_SENDSRCADDR)
365 struct in_addr *addr;
366 struct sockaddr_in *sin;
367 #ifdef IP_PKTINFO
368 char buf[CMSG_SPACE(sizeof(struct in_pktinfo))];
369 struct in_pktinfo *pktinfo;
370 #elif defined(IP_SENDSRCADDR)
371 char buf[CMSG_SPACE(sizeof(struct in_addr))];
372 #endif
373 msg.msg_control = buf;
374 msg.msg_controllen = sizeof(buf);
375 cmsg = CMSG_FIRSTHDR(&msg);
376 cmsg->cmsg_level = SOL_IP;
377 #ifdef IP_PKTINFO
378 cmsg->cmsg_type = IP_PKTINFO;
379 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
380 pktinfo = (struct in_pktinfo*)CMSG_DATA(cmsg);
381 memset(pktinfo, 0, sizeof(struct in_pktinfo));
382 addr = &pktinfo->ipi_spec_dst;
383 #elif defined(IP_SENDSRCADDR)
384 cmsg->cmsg_type = IP_SENDSRCADDR;
385 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
386 addr = (struct in_addr*)CMSG_DATA(cmsg);
387 #endif
388 sin = (struct sockaddr_in*)src->get_sockaddr(src);
389 memcpy(addr, &sin->sin_addr, sizeof(struct in_addr));
390 #endif /* IP_PKTINFO || IP_SENDSRCADDR */
391 }
392 #ifdef HAVE_IN6_PKTINFO
393 else
394 {
395 char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
396 struct in6_pktinfo *pktinfo;
397 struct sockaddr_in6 *sin;
398
399 msg.msg_control = buf;
400 msg.msg_controllen = sizeof(buf);
401 cmsg = CMSG_FIRSTHDR(&msg);
402 cmsg->cmsg_level = SOL_IPV6;
403 cmsg->cmsg_type = IPV6_PKTINFO;
404 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
405 pktinfo = (struct in6_pktinfo*)CMSG_DATA(cmsg);
406 memset(pktinfo, 0, sizeof(struct in6_pktinfo));
407 sin = (struct sockaddr_in6*)src->get_sockaddr(src);
408 memcpy(&pktinfo->ipi6_addr, &sin->sin6_addr, sizeof(struct in6_addr));
409 }
410 #endif /* HAVE_IN6_PKTINFO */
411 }
412
413 bytes_sent = sendmsg(skt, &msg, 0);
414
415 if (bytes_sent != data.len)
416 {
417 DBG1(DBG_NET, "error writing to socket: %s", strerror(errno));
418 return FAILED;
419 }
420 return SUCCESS;
421 }
422
423 METHOD(socket_t, get_port, u_int16_t,
424 private_socket_default_socket_t *this, bool nat_t)
425 {
426 return nat_t ? this->natt : this->port;
427 }
428
429 /**
430 * open a socket to send and receive packets
431 */
432 static int open_socket(private_socket_default_socket_t *this,
433 int family, u_int16_t *port)
434 {
435 int on = TRUE;
436 struct sockaddr_storage addr;
437 socklen_t addrlen;
438 u_int sol, pktinfo = 0;
439 int skt;
440
441 memset(&addr, 0, sizeof(addr));
442 addr.ss_family = family;
443 /* precalculate constants depending on address family */
444 switch (family)
445 {
446 case AF_INET:
447 {
448 struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
449 htoun32(&sin->sin_addr.s_addr, INADDR_ANY);
450 htoun16(&sin->sin_port, *port);
451 addrlen = sizeof(struct sockaddr_in);
452 sol = SOL_IP;
453 #ifdef IP_PKTINFO
454 pktinfo = IP_PKTINFO;
455 #elif defined(IP_RECVDSTADDR)
456 pktinfo = IP_RECVDSTADDR;
457 #endif
458 break;
459 }
460 case AF_INET6:
461 {
462 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
463 memcpy(&sin6->sin6_addr, &in6addr_any, sizeof(in6addr_any));
464 htoun16(&sin6->sin6_port, *port);
465 addrlen = sizeof(struct sockaddr_in6);
466 sol = SOL_IPV6;
467 pktinfo = IPV6_RECVPKTINFO;
468 break;
469 }
470 default:
471 return 0;
472 }
473
474 skt = socket(family, SOCK_DGRAM, IPPROTO_UDP);
475 if (skt < 0)
476 {
477 DBG1(DBG_NET, "could not open socket: %s", strerror(errno));
478 return 0;
479 }
480 if (setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) < 0)
481 {
482 DBG1(DBG_NET, "unable to set SO_REUSEADDR on socket: %s", strerror(errno));
483 close(skt);
484 return 0;
485 }
486
487 /* bind the socket */
488 if (bind(skt, (struct sockaddr *)&addr, addrlen) < 0)
489 {
490 DBG1(DBG_NET, "unable to bind socket: %s", strerror(errno));
491 close(skt);
492 return 0;
493 }
494
495 /* retrieve randomly allocated port if needed */
496 if (*port == 0)
497 {
498 if (getsockname(skt, (struct sockaddr *)&addr, &addrlen) < 0)
499 {
500 DBG1(DBG_NET, "unable to determine port: %s", strerror(errno));
501 close(skt);
502 return 0;
503 }
504 switch (family)
505 {
506 case AF_INET:
507 {
508 struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
509 *port = untoh16(&sin->sin_port);
510 break;
511 }
512 case AF_INET6:
513 {
514 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
515 *port = untoh16(&sin6->sin6_port);
516 break;
517 }
518 }
519 }
520
521 /* get additional packet info on receive */
522 if (pktinfo > 0)
523 {
524 if (setsockopt(skt, sol, pktinfo, &on, sizeof(on)) < 0)
525 {
526 DBG1(DBG_NET, "unable to set IP_PKTINFO on socket: %s", strerror(errno));
527 close(skt);
528 return 0;
529 }
530 }
531
532 if (!hydra->kernel_interface->bypass_socket(hydra->kernel_interface,
533 skt, family))
534 {
535 DBG1(DBG_NET, "installing IKE bypass policy failed");
536 }
537 return skt;
538 }
539
540 METHOD(socket_t, destroy, void,
541 private_socket_default_socket_t *this)
542 {
543 if (this->ipv4)
544 {
545 close(this->ipv4);
546 }
547 if (this->ipv4_natt)
548 {
549 close(this->ipv4_natt);
550 }
551 if (this->ipv6)
552 {
553 close(this->ipv6);
554 }
555 if (this->ipv6_natt)
556 {
557 close(this->ipv6_natt);
558 }
559 free(this);
560 }
561
562 /*
563 * See header for description
564 */
565 socket_default_socket_t *socket_default_socket_create()
566 {
567 private_socket_default_socket_t *this;
568
569 INIT(this,
570 .public = {
571 .socket = {
572 .send = _sender,
573 .receive = _receiver,
574 .get_port = _get_port,
575 .destroy = _destroy,
576 },
577 },
578 .port = lib->settings->get_int(lib->settings,
579 "%s.port", CHARON_UDP_PORT, charon->name),
580 .natt = lib->settings->get_int(lib->settings,
581 "%s.port_nat_t", CHARON_NATT_PORT, charon->name),
582 .max_packet = lib->settings->get_int(lib->settings,
583 "%s.max_packet", MAX_PACKET, charon->name),
584 .set_source = lib->settings->get_bool(lib->settings,
585 "%s.plugins.socket-default.set_source", TRUE,
586 charon->name),
587 );
588
589 if (this->port && this->port == this->natt)
590 {
591 DBG1(DBG_NET, "IKE ports can't be equal, will allocate NAT-T "
592 "port randomly");
593 this->natt = 0;
594 }
595
596 /* we allocate IPv6 sockets first as that will reserve randomly allocated
597 * ports also for IPv4 */
598 this->ipv6 = open_socket(this, AF_INET6, &this->port);
599 if (this->ipv6 == 0)
600 {
601 DBG1(DBG_NET, "could not open IPv6 socket, IPv6 disabled");
602 }
603 else
604 {
605 this->ipv6_natt = open_socket(this, AF_INET6, &this->natt);
606 if (this->ipv6_natt == 0)
607 {
608 DBG1(DBG_NET, "could not open IPv6 NAT-T socket");
609 }
610 }
611
612 this->ipv4 = open_socket(this, AF_INET, &this->port);
613 if (this->ipv4 == 0)
614 {
615 DBG1(DBG_NET, "could not open IPv4 socket, IPv4 disabled");
616 }
617 else
618 {
619 this->ipv4_natt = open_socket(this, AF_INET, &this->natt);
620 if (this->ipv4_natt == 0)
621 {
622 DBG1(DBG_NET, "could not open IPv4 NAT-T socket");
623 }
624 }
625
626 if (!this->ipv4 && !this->ipv6)
627 {
628 DBG1(DBG_NET, "could not create any sockets");
629 destroy(this);
630 return NULL;
631 }
632
633 /* enable UDP decapsulation globally, only for one socket needed */
634 if (!hydra->kernel_interface->enable_udp_decap(hydra->kernel_interface,
635 this->ipv6_natt ?: this->ipv4_natt,
636 this->ipv6_natt ? AF_INET6 : AF_INET, this->natt))
637 {
638 DBG1(DBG_NET, "enabling UDP decapsulation failed");
639 }
640 return &this->public;
641 }
642