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