Handle Non-ESP marker in receiver 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, marked;
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 /* NAT keepalives without marker */
355 if (data.len != 1 || data.ptr[0] != 0xFF)
356 {
357 /* add non esp marker to packet */
358 marked = chunk_alloc(data.len + MARKER_LEN);
359 memset(marked.ptr, 0, MARKER_LEN);
360 memcpy(marked.ptr + MARKER_LEN, data.ptr, data.len);
361 /* let the packet do the clean up for us */
362 packet->set_data(packet, marked);
363 data = marked;
364 }
365 }
366 else
367 {
368 DBG1(DBG_NET, "unable to locate a send socket for port %d", sport);
369 return FAILED;
370 }
371
372 memset(&msg, 0, sizeof(struct msghdr));
373 msg.msg_name = dst->get_sockaddr(dst);;
374 msg.msg_namelen = *dst->get_sockaddr_len(dst);
375 iov.iov_base = data.ptr;
376 iov.iov_len = data.len;
377 msg.msg_iov = &iov;
378 msg.msg_iovlen = 1;
379 msg.msg_flags = 0;
380
381 if (!src->is_anyaddr(src))
382 {
383 if (family == AF_INET)
384 {
385 #if defined(IP_PKTINFO) || defined(IP_SENDSRCADDR)
386 struct in_addr *addr;
387 struct sockaddr_in *sin;
388 #ifdef IP_PKTINFO
389 char buf[CMSG_SPACE(sizeof(struct in_pktinfo))];
390 struct in_pktinfo *pktinfo;
391 #elif defined(IP_SENDSRCADDR)
392 char buf[CMSG_SPACE(sizeof(struct in_addr))];
393 #endif
394 msg.msg_control = buf;
395 msg.msg_controllen = sizeof(buf);
396 cmsg = CMSG_FIRSTHDR(&msg);
397 cmsg->cmsg_level = SOL_IP;
398 #ifdef IP_PKTINFO
399 cmsg->cmsg_type = IP_PKTINFO;
400 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
401 pktinfo = (struct in_pktinfo*)CMSG_DATA(cmsg);
402 memset(pktinfo, 0, sizeof(struct in_pktinfo));
403 addr = &pktinfo->ipi_spec_dst;
404 #elif defined(IP_SENDSRCADDR)
405 cmsg->cmsg_type = IP_SENDSRCADDR;
406 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
407 addr = (struct in_addr*)CMSG_DATA(cmsg);
408 #endif
409 sin = (struct sockaddr_in*)src->get_sockaddr(src);
410 memcpy(addr, &sin->sin_addr, sizeof(struct in_addr));
411 #endif /* IP_PKTINFO || IP_SENDSRCADDR */
412 }
413 #ifdef HAVE_IN6_PKTINFO
414 else
415 {
416 char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
417 struct in6_pktinfo *pktinfo;
418 struct sockaddr_in6 *sin;
419
420 msg.msg_control = buf;
421 msg.msg_controllen = sizeof(buf);
422 cmsg = CMSG_FIRSTHDR(&msg);
423 cmsg->cmsg_level = SOL_IPV6;
424 cmsg->cmsg_type = IPV6_PKTINFO;
425 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
426 pktinfo = (struct in6_pktinfo*)CMSG_DATA(cmsg);
427 memset(pktinfo, 0, sizeof(struct in6_pktinfo));
428 sin = (struct sockaddr_in6*)src->get_sockaddr(src);
429 memcpy(&pktinfo->ipi6_addr, &sin->sin6_addr, sizeof(struct in6_addr));
430 }
431 #endif /* HAVE_IN6_PKTINFO */
432 }
433
434 bytes_sent = sendmsg(skt, &msg, 0);
435
436 if (bytes_sent != data.len)
437 {
438 DBG1(DBG_NET, "error writing to socket: %s", strerror(errno));
439 return FAILED;
440 }
441 return SUCCESS;
442 }
443
444 /**
445 * open a socket to send and receive packets
446 */
447 static int open_socket(private_socket_default_socket_t *this,
448 int family, u_int16_t port)
449 {
450 int on = TRUE;
451 struct sockaddr_storage addr;
452 socklen_t addrlen;
453 u_int sol, pktinfo = 0;
454 int skt;
455
456 memset(&addr, 0, sizeof(addr));
457 addr.ss_family = family;
458 /* precalculate constants depending on address family */
459 switch (family)
460 {
461 case AF_INET:
462 {
463 struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
464 htoun32(&sin->sin_addr.s_addr, INADDR_ANY);
465 htoun16(&sin->sin_port, port);
466 addrlen = sizeof(struct sockaddr_in);
467 sol = SOL_IP;
468 #ifdef IP_PKTINFO
469 pktinfo = IP_PKTINFO;
470 #elif defined(IP_RECVDSTADDR)
471 pktinfo = IP_RECVDSTADDR;
472 #endif
473 break;
474 }
475 case AF_INET6:
476 {
477 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
478 memcpy(&sin6->sin6_addr, &in6addr_any, sizeof(in6addr_any));
479 htoun16(&sin6->sin6_port, port);
480 addrlen = sizeof(struct sockaddr_in6);
481 sol = SOL_IPV6;
482 pktinfo = IPV6_RECVPKTINFO;
483 break;
484 }
485 default:
486 return 0;
487 }
488
489 skt = socket(family, SOCK_DGRAM, IPPROTO_UDP);
490 if (skt < 0)
491 {
492 DBG1(DBG_NET, "could not open socket: %s", strerror(errno));
493 return 0;
494 }
495 if (setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) < 0)
496 {
497 DBG1(DBG_NET, "unable to set SO_REUSEADDR on socket: %s", strerror(errno));
498 close(skt);
499 return 0;
500 }
501
502 /* bind the socket */
503 if (bind(skt, (struct sockaddr *)&addr, addrlen) < 0)
504 {
505 DBG1(DBG_NET, "unable to bind socket: %s", strerror(errno));
506 close(skt);
507 return 0;
508 }
509
510 /* get additional packet info on receive */
511 if (pktinfo > 0)
512 {
513 if (setsockopt(skt, sol, pktinfo, &on, sizeof(on)) < 0)
514 {
515 DBG1(DBG_NET, "unable to set IP_PKTINFO on socket: %s", strerror(errno));
516 close(skt);
517 return 0;
518 }
519 }
520
521 if (!hydra->kernel_interface->bypass_socket(hydra->kernel_interface,
522 skt, family))
523 {
524 DBG1(DBG_NET, "installing IKE bypass policy failed");
525 }
526
527 #ifndef __APPLE__
528 {
529 /* enable UDP decapsulation globally, only for one socket needed */
530 int type = UDP_ENCAP_ESPINUDP;
531 if (family == AF_INET && port == CHARON_NATT_PORT &&
532 setsockopt(skt, SOL_UDP, UDP_ENCAP, &type, sizeof(type)) < 0)
533 {
534 DBG1(DBG_NET, "unable to set UDP_ENCAP: %s", strerror(errno));
535 }
536 }
537 #endif
538 return skt;
539 }
540
541 METHOD(socket_t, destroy, void,
542 private_socket_default_socket_t *this)
543 {
544 if (this->ipv4)
545 {
546 close(this->ipv4);
547 }
548 if (this->ipv4_natt)
549 {
550 close(this->ipv4_natt);
551 }
552 if (this->ipv6)
553 {
554 close(this->ipv6);
555 }
556 if (this->ipv6_natt)
557 {
558 close(this->ipv6_natt);
559 }
560 free(this);
561 }
562
563 /*
564 * See header for description
565 */
566 socket_default_socket_t *socket_default_socket_create()
567 {
568 private_socket_default_socket_t *this;
569
570 INIT(this,
571 .public = {
572 .socket = {
573 .send = _sender,
574 .receive = _receiver,
575 .destroy = _destroy,
576 },
577 },
578 .max_packet = lib->settings->get_int(lib->settings,
579 "%s.max_packet", MAX_PACKET, charon->name),
580 );
581
582 #ifdef __APPLE__
583 {
584 int natt_port = CHARON_NATT_PORT;
585 if (sysctlbyname("net.inet.ipsec.esp_port", NULL, NULL, &natt_port,
586 sizeof(natt_port)) != 0)
587 {
588 DBG1(DBG_NET, "could not set net.inet.ipsec.esp_port to %d: %s",
589 natt_port, strerror(errno));
590 }
591 }
592 #endif
593
594 this->ipv4 = open_socket(this, AF_INET, CHARON_UDP_PORT);
595 if (this->ipv4 == 0)
596 {
597 DBG1(DBG_NET, "could not open IPv4 socket, IPv4 disabled");
598 }
599 else
600 {
601 this->ipv4_natt = open_socket(this, AF_INET, CHARON_NATT_PORT);
602 if (this->ipv4_natt == 0)
603 {
604 DBG1(DBG_NET, "could not open IPv4 NAT-T socket");
605 }
606 }
607
608 this->ipv6 = open_socket(this, AF_INET6, CHARON_UDP_PORT);
609 if (this->ipv6 == 0)
610 {
611 DBG1(DBG_NET, "could not open IPv6 socket, IPv6 disabled");
612 }
613 else
614 {
615 this->ipv6_natt = open_socket(this, AF_INET6, CHARON_NATT_PORT);
616 if (this->ipv6_natt == 0)
617 {
618 DBG1(DBG_NET, "could not open IPv6 NAT-T socket");
619 }
620 }
621
622 if (!this->ipv4 && !this->ipv6)
623 {
624 DBG1(DBG_NET, "could not create any sockets");
625 destroy(this);
626 return NULL;
627 }
628 return &this->public;
629 }
630