Fixed compilation of kernel-pfroute plugin
[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 failed");
410 }
411
412 return fd;
413 }
414
415 /**
416 * Find/Create a socket to send from host
417 */
418 static dynsock_t *find_socket(private_socket_dynamic_socket_t *this,
419 int family, u_int16_t port)
420 {
421 dynsock_t *skt, lookup = {
422 .family = family,
423 .port = port,
424 };
425 char buf[] = {0x01};
426 int fd;
427
428 this->lock->read_lock(this->lock);
429 skt = this->sockets->get(this->sockets, &lookup);
430 this->lock->unlock(this->lock);
431 if (skt)
432 {
433 return skt;
434 }
435 fd = open_socket(this, family, port);
436 if (!fd)
437 {
438 return NULL;
439 }
440 INIT(skt,
441 .family = family,
442 .port = port,
443 .fd = fd,
444 );
445 this->lock->write_lock(this->lock);
446 this->sockets->put(this->sockets, skt, skt);
447 this->lock->unlock(this->lock);
448 /* notify receiver thread to reread socket list */
449 ignore_result(write(this->notify[1], buf, sizeof(buf)));
450
451 return skt;
452 }
453
454 METHOD(socket_t, sender, status_t,
455 private_socket_dynamic_socket_t *this, packet_t *packet)
456 {
457 dynsock_t *skt;
458 host_t *src, *dst;
459 int port, family;
460 ssize_t len;
461 chunk_t data;
462 struct msghdr msg;
463 struct cmsghdr *cmsg;
464 struct iovec iov;
465
466 src = packet->get_source(packet);
467 dst = packet->get_destination(packet);
468 family = src->get_family(src);
469 port = src->get_port(src);
470 port = port ?: CHARON_UDP_PORT;
471 skt = find_socket(this, family, port);
472 if (!skt)
473 {
474 return FAILED;
475 }
476
477 data = packet->get_data(packet);
478 DBG2(DBG_NET, "sending packet: from %#H to %#H", src, dst);
479
480 memset(&msg, 0, sizeof(struct msghdr));
481 msg.msg_name = dst->get_sockaddr(dst);;
482 msg.msg_namelen = *dst->get_sockaddr_len(dst);
483 iov.iov_base = data.ptr;
484 iov.iov_len = data.len;
485 msg.msg_iov = &iov;
486 msg.msg_iovlen = 1;
487 msg.msg_flags = 0;
488
489 if (!src->is_anyaddr(src))
490 {
491 if (family == AF_INET)
492 {
493 struct in_addr *addr;
494 struct sockaddr_in *sin;
495 char buf[CMSG_SPACE(sizeof(struct in_pktinfo))];
496 struct in_pktinfo *pktinfo;
497
498 msg.msg_control = buf;
499 msg.msg_controllen = sizeof(buf);
500 cmsg = CMSG_FIRSTHDR(&msg);
501 cmsg->cmsg_level = SOL_IP;
502 cmsg->cmsg_type = IP_PKTINFO;
503 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
504 pktinfo = (struct in_pktinfo*)CMSG_DATA(cmsg);
505 memset(pktinfo, 0, sizeof(struct in_pktinfo));
506 addr = &pktinfo->ipi_spec_dst;
507 sin = (struct sockaddr_in*)src->get_sockaddr(src);
508 memcpy(addr, &sin->sin_addr, sizeof(struct in_addr));
509 }
510 else
511 {
512 char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
513 struct in6_pktinfo *pktinfo;
514 struct sockaddr_in6 *sin;
515
516 msg.msg_control = buf;
517 msg.msg_controllen = sizeof(buf);
518 cmsg = CMSG_FIRSTHDR(&msg);
519 cmsg->cmsg_level = SOL_IPV6;
520 cmsg->cmsg_type = IPV6_PKTINFO;
521 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
522 pktinfo = (struct in6_pktinfo*)CMSG_DATA(cmsg);
523 memset(pktinfo, 0, sizeof(struct in6_pktinfo));
524 sin = (struct sockaddr_in6*)src->get_sockaddr(src);
525 memcpy(&pktinfo->ipi6_addr, &sin->sin6_addr, sizeof(struct in6_addr));
526 }
527 }
528
529 len = sendmsg(skt->fd, &msg, 0);
530 if (len != data.len)
531 {
532 DBG1(DBG_NET, "error writing to socket: %s", strerror(errno));
533 return FAILED;
534 }
535 return SUCCESS;
536 }
537
538 METHOD(socket_t, get_port, u_int16_t,
539 private_socket_dynamic_socket_t *this, bool nat_t)
540 {
541 /* we return 0 here for users that have no explicit port configured, the
542 * sender will default to the default port in this case */
543 return 0;
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 .get_port = _get_port,
580 .destroy = _destroy,
581 },
582 },
583 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
584 .max_packet = lib->settings->get_int(lib->settings,
585 "%s.max_packet", MAX_PACKET, charon->name),
586 );
587
588 if (pipe(this->notify) != 0)
589 {
590 DBG1(DBG_NET, "creating notify pipe for dynamic socket failed");
591 free(this);
592 return NULL;
593 }
594
595 this->sockets = hashtable_create((void*)hash, (void*)equals, 8);
596
597 return &this->public;
598 }
599