Handle Non-ESP marker in receiver and not individual socket plugins.
[strongswan.git] / src / libcharon / plugins / socket_raw / socket_raw_socket.c
1 /*
2 * Copyright (C) 2006-2012 Tobias Brunner
3 * Copyright (C) 2005-2010 Martin Willi
4 * Copyright (C) 2006 Daniel Roethlisberger
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
22 #include "socket_raw_socket.h"
23
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <netinet/in.h>
33 #include <netinet/ip.h>
34 #include <netinet/udp.h>
35 #include <linux/types.h>
36 #include <linux/filter.h>
37 #include <net/if.h>
38
39 #include <hydra.h>
40 #include <daemon.h>
41 #include <threading/thread.h>
42
43 /* Maximum size of a packet */
44 #define MAX_PACKET 10000
45
46 /* constants for packet handling */
47 #define IP_LEN sizeof(struct iphdr)
48 #define IP6_LEN sizeof(struct ip6_hdr)
49 #define UDP_LEN sizeof(struct udphdr)
50 #define MARKER_LEN sizeof(u_int32_t)
51
52 /* offsets for packet handling */
53 #define IP_PROTO_OFFSET 9
54 #define IP6_PROTO_OFFSET 6
55 #define IKE_VERSION_OFFSET 17
56 #define IKE_LENGTH_OFFSET 24
57
58 /* from linux/udp.h */
59 #ifndef UDP_ENCAP
60 #define UDP_ENCAP 100
61 #endif /*UDP_ENCAP*/
62
63 #ifndef UDP_ENCAP_ESPINUDP
64 #define UDP_ENCAP_ESPINUDP 2
65 #endif /*UDP_ENCAP_ESPINUDP*/
66
67 /* needed for older kernel headers */
68 #ifndef IPV6_2292PKTINFO
69 #define IPV6_2292PKTINFO 2
70 #endif /*IPV6_2292PKTINFO*/
71
72 typedef struct private_socket_raw_socket_t private_socket_raw_socket_t;
73
74 /**
75 * Private data of an socket_t object
76 */
77 struct private_socket_raw_socket_t {
78
79 /**
80 * public functions
81 */
82 socket_raw_socket_t public;
83
84 /**
85 * regular port
86 */
87 int port;
88
89 /**
90 * port used for nat-t
91 */
92 int natt_port;
93
94 /**
95 * raw receiver socket for IPv4
96 */
97 int recv4;
98
99 /**
100 * raw receiver socket for IPv6
101 */
102 int recv6;
103
104 /**
105 * send socket on regular port for IPv4
106 */
107 int send4;
108
109 /**
110 * send socket on regular port for IPv6
111 */
112 int send6;
113
114 /**
115 * send socket on nat-t port for IPv4
116 */
117 int send4_natt;
118
119 /**
120 * send socket on nat-t port for IPv6
121 */
122 int send6_natt;
123
124 /**
125 * Maximum packet size to receive
126 */
127 int max_packet;
128 };
129
130 METHOD(socket_t, receiver, status_t,
131 private_socket_raw_socket_t *this, packet_t **packet)
132 {
133 char buffer[this->max_packet];
134 chunk_t data;
135 packet_t *pkt;
136 struct udphdr *udp;
137 host_t *source = NULL, *dest = NULL;
138 int bytes_read = 0, data_offset;
139 bool oldstate;
140 fd_set rfds;
141
142 FD_ZERO(&rfds);
143
144 if (this->recv4)
145 {
146 FD_SET(this->recv4, &rfds);
147 }
148 if (this->recv6)
149 {
150 FD_SET(this->recv6, &rfds);
151 }
152
153 DBG2(DBG_NET, "waiting for data on raw sockets");
154
155 oldstate = thread_cancelability(TRUE);
156 if (select(max(this->recv4, this->recv6) + 1, &rfds, NULL, NULL, NULL) <= 0)
157 {
158 thread_cancelability(oldstate);
159 return FAILED;
160 }
161 thread_cancelability(oldstate);
162
163 if (this->recv4 && FD_ISSET(this->recv4, &rfds))
164 {
165 /* IPv4 raw sockets return the IP header. We read src/dest
166 * information directly from the raw header */
167 struct iphdr *ip;
168 struct sockaddr_in src, dst;
169
170 bytes_read = recv(this->recv4, buffer, this->max_packet, 0);
171 if (bytes_read < 0)
172 {
173 DBG1(DBG_NET, "error reading from IPv4 socket: %s", strerror(errno));
174 return FAILED;
175 }
176 if (bytes_read == this->max_packet)
177 {
178 DBG1(DBG_NET, "receive buffer too small, packet discarded");
179 return FAILED;
180 }
181 DBG3(DBG_NET, "received IPv4 packet %b", buffer, bytes_read);
182
183 /* read source/dest from raw IP/UDP header */
184 if (bytes_read < IP_LEN + UDP_LEN + MARKER_LEN)
185 {
186 DBG1(DBG_NET, "received IPv4 packet too short (%d bytes)",
187 bytes_read);
188 return FAILED;
189 }
190 ip = (struct iphdr*) buffer;
191 udp = (struct udphdr*) (buffer + IP_LEN);
192 src.sin_family = AF_INET;
193 src.sin_addr.s_addr = ip->saddr;
194 src.sin_port = udp->source;
195 dst.sin_family = AF_INET;
196 dst.sin_addr.s_addr = ip->daddr;
197 dst.sin_port = udp->dest;
198 source = host_create_from_sockaddr((sockaddr_t*)&src);
199 dest = host_create_from_sockaddr((sockaddr_t*)&dst);
200
201 pkt = packet_create();
202 pkt->set_source(pkt, source);
203 pkt->set_destination(pkt, dest);
204 DBG2(DBG_NET, "received packet: from %#H to %#H", source, dest);
205 data_offset = IP_LEN + UDP_LEN;
206 data.len = bytes_read - data_offset;
207 data.ptr = buffer + data_offset;
208 pkt->set_data(pkt, chunk_clone(data));
209 }
210 else if (this->recv6 && FD_ISSET(this->recv6, &rfds))
211 {
212 /* IPv6 raw sockets return no IP header. We must query
213 * src/dest via socket options/ancillary data */
214 struct msghdr msg;
215 struct cmsghdr *cmsgptr;
216 struct sockaddr_in6 src, dst;
217 struct iovec iov;
218 char ancillary[64];
219
220 msg.msg_name = &src;
221 msg.msg_namelen = sizeof(src);
222 iov.iov_base = buffer;
223 iov.iov_len = this->max_packet;
224 msg.msg_iov = &iov;
225 msg.msg_iovlen = 1;
226 msg.msg_control = ancillary;
227 msg.msg_controllen = sizeof(ancillary);
228 msg.msg_flags = 0;
229
230 bytes_read = recvmsg(this->recv6, &msg, 0);
231 if (bytes_read < 0)
232 {
233 DBG1(DBG_NET, "error reading from IPv6 socket: %s", strerror(errno));
234 return FAILED;
235 }
236 DBG3(DBG_NET, "received IPv6 packet %b", buffer, bytes_read);
237
238 if (bytes_read < IP_LEN + UDP_LEN + MARKER_LEN)
239 {
240 DBG3(DBG_NET, "received IPv6 packet too short (%d bytes)",
241 bytes_read);
242 return FAILED;
243 }
244
245 /* read ancillary data to get destination address */
246 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
247 cmsgptr = CMSG_NXTHDR(&msg, cmsgptr))
248 {
249 if (cmsgptr->cmsg_len == 0)
250 {
251 DBG1(DBG_NET, "error reading IPv6 ancillary data");
252 return FAILED;
253 }
254
255 #ifdef HAVE_IN6_PKTINFO
256 if (cmsgptr->cmsg_level == SOL_IPV6 &&
257 cmsgptr->cmsg_type == IPV6_2292PKTINFO)
258 {
259 struct in6_pktinfo *pktinfo;
260 pktinfo = (struct in6_pktinfo*)CMSG_DATA(cmsgptr);
261
262 memset(&dst, 0, sizeof(dst));
263 memcpy(&dst.sin6_addr, &pktinfo->ipi6_addr, sizeof(dst.sin6_addr));
264 dst.sin6_family = AF_INET6;
265 udp = (struct udphdr*) (buffer);
266 dst.sin6_port = udp->dest;
267 src.sin6_port = udp->source;
268 dest = host_create_from_sockaddr((sockaddr_t*)&dst);
269 }
270 #endif /* HAVE_IN6_PKTINFO */
271 }
272 /* ancillary data missing? */
273 if (dest == NULL)
274 {
275 DBG1(DBG_NET, "error reading IPv6 packet header");
276 return FAILED;
277 }
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_offset = UDP_LEN;
286 data.len = bytes_read - data_offset;
287 data.ptr = buffer + data_offset;
288 pkt->set_data(pkt, chunk_clone(data));
289 }
290 else
291 {
292 /* oops, shouldn't happen */
293 return FAILED;
294 }
295
296 /* return packet */
297 *packet = pkt;
298 return SUCCESS;
299 }
300
301 METHOD(socket_t, sender, status_t,
302 private_socket_raw_socket_t *this, packet_t *packet)
303 {
304 int sport, skt, family;
305 ssize_t bytes_sent;
306 chunk_t data, marked;
307 host_t *src, *dst;
308 struct msghdr msg;
309 struct cmsghdr *cmsg;
310 struct iovec iov;
311
312 src = packet->get_source(packet);
313 dst = packet->get_destination(packet);
314 data = packet->get_data(packet);
315
316 DBG2(DBG_NET, "sending packet: from %#H to %#H", src, dst);
317
318 /* send data */
319 sport = src->get_port(src);
320 family = dst->get_family(dst);
321 if (sport == CHARON_UDP_PORT)
322 {
323 if (family == AF_INET)
324 {
325 skt = this->send4;
326 }
327 else
328 {
329 skt = this->send6;
330 }
331 }
332 else if (sport == CHARON_NATT_PORT)
333 {
334 if (family == AF_INET)
335 {
336 skt = this->send4_natt;
337 }
338 else
339 {
340 skt = this->send6_natt;
341 }
342 /* NAT keepalives without marker */
343 if (data.len != 1 || data.ptr[0] != 0xFF)
344 {
345 /* add non esp marker to packet */
346 marked = chunk_alloc(data.len + MARKER_LEN);
347 memset(marked.ptr, 0, MARKER_LEN);
348 memcpy(marked.ptr + MARKER_LEN, data.ptr, data.len);
349 /* let the packet do the clean up for us */
350 packet->set_data(packet, marked);
351 data = marked;
352 }
353 }
354 else
355 {
356 DBG1(DBG_NET, "unable to locate a send socket for port %d", sport);
357 return FAILED;
358 }
359
360 memset(&msg, 0, sizeof(struct msghdr));
361 msg.msg_name = dst->get_sockaddr(dst);;
362 msg.msg_namelen = *dst->get_sockaddr_len(dst);
363 iov.iov_base = data.ptr;
364 iov.iov_len = data.len;
365 msg.msg_iov = &iov;
366 msg.msg_iovlen = 1;
367 msg.msg_flags = 0;
368
369 if (!src->is_anyaddr(src))
370 {
371 if (family == AF_INET)
372 {
373 char buf[CMSG_SPACE(sizeof(struct in_pktinfo))];
374 struct in_pktinfo *pktinfo;
375 struct sockaddr_in *sin;
376
377 msg.msg_control = buf;
378 msg.msg_controllen = sizeof(buf);
379 cmsg = CMSG_FIRSTHDR(&msg);
380 cmsg->cmsg_level = SOL_IP;
381 cmsg->cmsg_type = IP_PKTINFO;
382 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
383 pktinfo = (struct in_pktinfo*)CMSG_DATA(cmsg);
384 memset(pktinfo, 0, sizeof(struct in_pktinfo));
385 sin = (struct sockaddr_in*)src->get_sockaddr(src);
386 memcpy(&pktinfo->ipi_spec_dst, &sin->sin_addr, sizeof(struct in_addr));
387 }
388 #ifdef HAVE_IN6_PKTINFO
389 else
390 {
391 char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
392 struct in6_pktinfo *pktinfo;
393 struct sockaddr_in6 *sin;
394
395 msg.msg_control = buf;
396 msg.msg_controllen = sizeof(buf);
397 cmsg = CMSG_FIRSTHDR(&msg);
398 cmsg->cmsg_level = SOL_IPV6;
399 cmsg->cmsg_type = IPV6_2292PKTINFO;
400 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
401 pktinfo = (struct in6_pktinfo*)CMSG_DATA(cmsg);
402 memset(pktinfo, 0, sizeof(struct in6_pktinfo));
403 sin = (struct sockaddr_in6*)src->get_sockaddr(src);
404 memcpy(&pktinfo->ipi6_addr, &sin->sin6_addr, sizeof(struct in6_addr));
405 }
406 #endif /* HAVE_IN6_PKTINFO */
407 }
408
409 bytes_sent = sendmsg(skt, &msg, 0);
410
411 if (bytes_sent != data.len)
412 {
413 DBG1(DBG_NET, "error writing to socket: %s", strerror(errno));
414 return FAILED;
415 }
416 return SUCCESS;
417 }
418
419 /**
420 * open a socket to send packets
421 */
422 static int open_send_socket(private_socket_raw_socket_t *this,
423 int family, u_int16_t port)
424 {
425 int on = TRUE;
426 int type = UDP_ENCAP_ESPINUDP;
427 struct sockaddr_storage addr;
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 break;
441 }
442 case AF_INET6:
443 {
444 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
445 memcpy(&sin6->sin6_addr, &in6addr_any, sizeof(in6addr_any));
446 htoun16(&sin6->sin6_port, port);
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 send socket: %s", strerror(errno));
457 return 0;
458 }
459
460 if (setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) < 0)
461 {
462 DBG1(DBG_NET, "unable to set SO_REUSEADDR on send socket: %s",
463 strerror(errno));
464 close(skt);
465 return 0;
466 }
467
468 /* bind the send socket */
469 if (bind(skt, (struct sockaddr *)&addr, sizeof(addr)) < 0)
470 {
471 DBG1(DBG_NET, "unable to bind send socket: %s",
472 strerror(errno));
473 close(skt);
474 return 0;
475 }
476
477 if (family == AF_INET)
478 {
479 /* enable UDP decapsulation globally, only for one socket needed */
480 if (setsockopt(skt, SOL_UDP, UDP_ENCAP, &type, sizeof(type)) < 0)
481 {
482 DBG1(DBG_NET, "unable to set UDP_ENCAP: %s; NAT-T may fail",
483 strerror(errno));
484 }
485 }
486
487 if (!hydra->kernel_interface->bypass_socket(hydra->kernel_interface,
488 skt, family))
489 {
490 DBG1(DBG_NET, "installing bypass policy on send socket failed");
491 }
492
493 return skt;
494 }
495
496 /**
497 * open a socket to receive packets
498 */
499 static int open_recv_socket(private_socket_raw_socket_t *this, int family)
500 {
501 int skt;
502 int on = TRUE;
503 u_int ip_len, sol, udp_header, ike_header;
504
505 /* precalculate constants depending on address family */
506 switch (family)
507 {
508 case AF_INET:
509 ip_len = IP_LEN;
510 sol = SOL_IP;
511 break;
512 case AF_INET6:
513 ip_len = 0; /* IPv6 raw sockets contain no IP header */
514 sol = SOL_IPV6;
515 break;
516 default:
517 return 0;
518 }
519 udp_header = ip_len;
520 ike_header = ip_len + UDP_LEN;
521
522 /* This filter code filters out all non-IKEv2 traffic on
523 * a SOCK_RAW IP_PROTP_UDP socket. Handling of other
524 * IKE versions is done in pluto.
525 */
526 struct sock_filter ikev2_filter_code[] =
527 {
528 /* Destination Port must be either port or natt_port */
529 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, udp_header + 2),
530 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, CHARON_UDP_PORT, 1, 0),
531 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, CHARON_NATT_PORT, 6, 14),
532 /* port */
533 /* IKE version must be 2.x */
534 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, ike_header + IKE_VERSION_OFFSET),
535 BPF_STMT(BPF_ALU+BPF_RSH+BPF_K, 4),
536 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 2, 0, 11),
537 /* packet length is length in IKEv2 header + ip header + udp header */
538 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, ike_header + IKE_LENGTH_OFFSET),
539 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, ip_len + UDP_LEN),
540 BPF_STMT(BPF_RET+BPF_A, 0),
541 /* natt_port */
542 /* nat-t: check for marker */
543 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, ike_header),
544 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0, 0, 6),
545 /* nat-t: IKE version must be 2.x */
546 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, ike_header + MARKER_LEN + IKE_VERSION_OFFSET),
547 BPF_STMT(BPF_ALU+BPF_RSH+BPF_K, 4),
548 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 2, 0, 3),
549 /* nat-t: packet length is length in IKEv2 header + ip header + udp header + non esp marker */
550 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, ike_header + MARKER_LEN + IKE_LENGTH_OFFSET),
551 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, ip_len + UDP_LEN + MARKER_LEN),
552 BPF_STMT(BPF_RET+BPF_A, 0),
553 /* packet doesn't match, ignore */
554 BPF_STMT(BPF_RET+BPF_K, 0),
555 };
556
557 /* Filter struct to use with setsockopt */
558 struct sock_fprog ikev2_filter = {
559 sizeof(ikev2_filter_code) / sizeof(struct sock_filter),
560 ikev2_filter_code
561 };
562
563 /* set up a raw socket */
564 skt = socket(family, SOCK_RAW, IPPROTO_UDP);
565 if (skt < 0)
566 {
567 DBG1(DBG_NET, "unable to create raw socket: %s", strerror(errno));
568 return 0;
569 }
570
571 if (setsockopt(skt, SOL_SOCKET, SO_ATTACH_FILTER,
572 &ikev2_filter, sizeof(ikev2_filter)) < 0)
573 {
574 DBG1(DBG_NET, "unable to attach IKEv2 filter to raw socket: %s",
575 strerror(errno));
576 close(skt);
577 return 0;
578 }
579
580 if (family == AF_INET6 &&
581 /* we use IPV6_2292PKTINFO, as IPV6_PKTINFO is defined as
582 * 2 or 50 depending on kernel header version */
583 setsockopt(skt, sol, IPV6_2292PKTINFO, &on, sizeof(on)) < 0)
584 {
585 DBG1(DBG_NET, "unable to set IPV6_PKTINFO on raw socket: %s",
586 strerror(errno));
587 close(skt);
588 return 0;
589 }
590
591 if (!hydra->kernel_interface->bypass_socket(hydra->kernel_interface,
592 skt, family))
593 {
594 DBG1(DBG_NET, "installing bypass policy on receive socket failed");
595 }
596
597 return skt;
598 }
599
600 METHOD(socket_t, destroy, void,
601 private_socket_raw_socket_t *this)
602 {
603 if (this->recv4)
604 {
605 close(this->recv4);
606 }
607 if (this->recv6)
608 {
609 close(this->recv6);
610 }
611 if (this->send4)
612 {
613 close(this->send4);
614 }
615 if (this->send6)
616 {
617 close(this->send6);
618 }
619 if (this->send4_natt)
620 {
621 close(this->send4_natt);
622 }
623 if (this->send6_natt)
624 {
625 close(this->send6_natt);
626 }
627 free(this);
628 }
629
630 /*
631 * See header for description
632 */
633 socket_raw_socket_t *socket_raw_socket_create()
634 {
635 private_socket_raw_socket_t *this;
636
637 INIT(this,
638 .public = {
639 .socket = {
640 .send = _sender,
641 .receive = _receiver,
642 .destroy = _destroy,
643 },
644 },
645 .max_packet = lib->settings->get_int(lib->settings,
646 "%s.max_packet", MAX_PACKET, charon->name),
647 );
648
649 this->recv4 = open_recv_socket(this, AF_INET);
650 if (this->recv4 == 0)
651 {
652 DBG1(DBG_NET, "could not open IPv4 receive socket, IPv4 disabled");
653 }
654 else
655 {
656 this->send4 = open_send_socket(this, AF_INET, CHARON_UDP_PORT);
657 if (this->send4 == 0)
658 {
659 DBG1(DBG_NET, "could not open IPv4 send socket, IPv4 disabled");
660 close(this->recv4);
661 }
662 else
663 {
664 this->send4_natt = open_send_socket(this, AF_INET, CHARON_NATT_PORT);
665 if (this->send4_natt == 0)
666 {
667 DBG1(DBG_NET, "could not open IPv4 NAT-T send socket");
668 }
669 }
670 }
671
672 this->recv6 = open_recv_socket(this, AF_INET6);
673 if (this->recv6 == 0)
674 {
675 DBG1(DBG_NET, "could not open IPv6 receive socket, IPv6 disabled");
676 }
677 else
678 {
679 this->send6 = open_send_socket(this, AF_INET6, CHARON_UDP_PORT);
680 if (this->send6 == 0)
681 {
682 DBG1(DBG_NET, "could not open IPv6 send socket, IPv6 disabled");
683 close(this->recv6);
684 }
685 else
686 {
687 this->send6_natt = open_send_socket(this, AF_INET6, CHARON_NATT_PORT);
688 if (this->send6_natt == 0)
689 {
690 DBG1(DBG_NET, "could not open IPv6 NAT-T send socket");
691 }
692 }
693 }
694
695 if (!(this->send4 || this->send6) || !(this->recv4 || this->recv6))
696 {
697 DBG1(DBG_NET, "could not create any sockets");
698 destroy(this);
699 return NULL;
700 }
701
702 return &this->public;
703 }