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