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