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