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