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