Add Non-ESP marker in sender 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;
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 }
343 else
344 {
345 DBG1(DBG_NET, "unable to locate a send socket for port %d", sport);
346 return FAILED;
347 }
348
349 memset(&msg, 0, sizeof(struct msghdr));
350 msg.msg_name = dst->get_sockaddr(dst);;
351 msg.msg_namelen = *dst->get_sockaddr_len(dst);
352 iov.iov_base = data.ptr;
353 iov.iov_len = data.len;
354 msg.msg_iov = &iov;
355 msg.msg_iovlen = 1;
356 msg.msg_flags = 0;
357
358 if (!src->is_anyaddr(src))
359 {
360 if (family == AF_INET)
361 {
362 char buf[CMSG_SPACE(sizeof(struct in_pktinfo))];
363 struct in_pktinfo *pktinfo;
364 struct sockaddr_in *sin;
365
366 msg.msg_control = buf;
367 msg.msg_controllen = sizeof(buf);
368 cmsg = CMSG_FIRSTHDR(&msg);
369 cmsg->cmsg_level = SOL_IP;
370 cmsg->cmsg_type = IP_PKTINFO;
371 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
372 pktinfo = (struct in_pktinfo*)CMSG_DATA(cmsg);
373 memset(pktinfo, 0, sizeof(struct in_pktinfo));
374 sin = (struct sockaddr_in*)src->get_sockaddr(src);
375 memcpy(&pktinfo->ipi_spec_dst, &sin->sin_addr, sizeof(struct in_addr));
376 }
377 #ifdef HAVE_IN6_PKTINFO
378 else
379 {
380 char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
381 struct in6_pktinfo *pktinfo;
382 struct sockaddr_in6 *sin;
383
384 msg.msg_control = buf;
385 msg.msg_controllen = sizeof(buf);
386 cmsg = CMSG_FIRSTHDR(&msg);
387 cmsg->cmsg_level = SOL_IPV6;
388 cmsg->cmsg_type = IPV6_2292PKTINFO;
389 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
390 pktinfo = (struct in6_pktinfo*)CMSG_DATA(cmsg);
391 memset(pktinfo, 0, sizeof(struct in6_pktinfo));
392 sin = (struct sockaddr_in6*)src->get_sockaddr(src);
393 memcpy(&pktinfo->ipi6_addr, &sin->sin6_addr, sizeof(struct in6_addr));
394 }
395 #endif /* HAVE_IN6_PKTINFO */
396 }
397
398 bytes_sent = sendmsg(skt, &msg, 0);
399
400 if (bytes_sent != data.len)
401 {
402 DBG1(DBG_NET, "error writing to socket: %s", strerror(errno));
403 return FAILED;
404 }
405 return SUCCESS;
406 }
407
408 /**
409 * open a socket to send packets
410 */
411 static int open_send_socket(private_socket_raw_socket_t *this,
412 int family, u_int16_t port)
413 {
414 int on = TRUE;
415 int type = UDP_ENCAP_ESPINUDP;
416 struct sockaddr_storage addr;
417 int skt;
418
419 memset(&addr, 0, sizeof(addr));
420 addr.ss_family = family;
421 /* precalculate constants depending on address family */
422 switch (family)
423 {
424 case AF_INET:
425 {
426 struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
427 htoun32(&sin->sin_addr.s_addr, INADDR_ANY);
428 htoun16(&sin->sin_port, port);
429 break;
430 }
431 case AF_INET6:
432 {
433 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
434 memcpy(&sin6->sin6_addr, &in6addr_any, sizeof(in6addr_any));
435 htoun16(&sin6->sin6_port, port);
436 break;
437 }
438 default:
439 return 0;
440 }
441
442 skt = socket(family, SOCK_DGRAM, IPPROTO_UDP);
443 if (skt < 0)
444 {
445 DBG1(DBG_NET, "could not open send socket: %s", strerror(errno));
446 return 0;
447 }
448
449 if (setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) < 0)
450 {
451 DBG1(DBG_NET, "unable to set SO_REUSEADDR on send socket: %s",
452 strerror(errno));
453 close(skt);
454 return 0;
455 }
456
457 /* bind the send socket */
458 if (bind(skt, (struct sockaddr *)&addr, sizeof(addr)) < 0)
459 {
460 DBG1(DBG_NET, "unable to bind send socket: %s",
461 strerror(errno));
462 close(skt);
463 return 0;
464 }
465
466 if (family == AF_INET)
467 {
468 /* enable UDP decapsulation globally, only for one socket needed */
469 if (setsockopt(skt, SOL_UDP, UDP_ENCAP, &type, sizeof(type)) < 0)
470 {
471 DBG1(DBG_NET, "unable to set UDP_ENCAP: %s; NAT-T may fail",
472 strerror(errno));
473 }
474 }
475
476 if (!hydra->kernel_interface->bypass_socket(hydra->kernel_interface,
477 skt, family))
478 {
479 DBG1(DBG_NET, "installing bypass policy on send socket failed");
480 }
481
482 return skt;
483 }
484
485 /**
486 * open a socket to receive packets
487 */
488 static int open_recv_socket(private_socket_raw_socket_t *this, int family)
489 {
490 int skt;
491 int on = TRUE;
492 u_int ip_len, sol, udp_header, ike_header;
493
494 /* precalculate constants depending on address family */
495 switch (family)
496 {
497 case AF_INET:
498 ip_len = IP_LEN;
499 sol = SOL_IP;
500 break;
501 case AF_INET6:
502 ip_len = 0; /* IPv6 raw sockets contain no IP header */
503 sol = SOL_IPV6;
504 break;
505 default:
506 return 0;
507 }
508 udp_header = ip_len;
509 ike_header = ip_len + UDP_LEN;
510
511 /* This filter code filters out all non-IKEv2 traffic on
512 * a SOCK_RAW IP_PROTP_UDP socket. Handling of other
513 * IKE versions is done in pluto.
514 */
515 struct sock_filter ikev2_filter_code[] =
516 {
517 /* Destination Port must be either port or natt_port */
518 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, udp_header + 2),
519 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, CHARON_UDP_PORT, 1, 0),
520 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, CHARON_NATT_PORT, 6, 14),
521 /* port */
522 /* IKE version must be 2.x */
523 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, ike_header + IKE_VERSION_OFFSET),
524 BPF_STMT(BPF_ALU+BPF_RSH+BPF_K, 4),
525 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 2, 0, 11),
526 /* packet length is length in IKEv2 header + ip header + udp header */
527 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, ike_header + IKE_LENGTH_OFFSET),
528 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, ip_len + UDP_LEN),
529 BPF_STMT(BPF_RET+BPF_A, 0),
530 /* natt_port */
531 /* nat-t: check for marker */
532 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, ike_header),
533 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0, 0, 6),
534 /* nat-t: IKE version must be 2.x */
535 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, ike_header + MARKER_LEN + IKE_VERSION_OFFSET),
536 BPF_STMT(BPF_ALU+BPF_RSH+BPF_K, 4),
537 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 2, 0, 3),
538 /* nat-t: packet length is length in IKEv2 header + ip header + udp header + non esp marker */
539 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, ike_header + MARKER_LEN + IKE_LENGTH_OFFSET),
540 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, ip_len + UDP_LEN + MARKER_LEN),
541 BPF_STMT(BPF_RET+BPF_A, 0),
542 /* packet doesn't match, ignore */
543 BPF_STMT(BPF_RET+BPF_K, 0),
544 };
545
546 /* Filter struct to use with setsockopt */
547 struct sock_fprog ikev2_filter = {
548 sizeof(ikev2_filter_code) / sizeof(struct sock_filter),
549 ikev2_filter_code
550 };
551
552 /* set up a raw socket */
553 skt = socket(family, SOCK_RAW, IPPROTO_UDP);
554 if (skt < 0)
555 {
556 DBG1(DBG_NET, "unable to create raw socket: %s", strerror(errno));
557 return 0;
558 }
559
560 if (setsockopt(skt, SOL_SOCKET, SO_ATTACH_FILTER,
561 &ikev2_filter, sizeof(ikev2_filter)) < 0)
562 {
563 DBG1(DBG_NET, "unable to attach IKEv2 filter to raw socket: %s",
564 strerror(errno));
565 close(skt);
566 return 0;
567 }
568
569 if (family == AF_INET6 &&
570 /* we use IPV6_2292PKTINFO, as IPV6_PKTINFO is defined as
571 * 2 or 50 depending on kernel header version */
572 setsockopt(skt, sol, IPV6_2292PKTINFO, &on, sizeof(on)) < 0)
573 {
574 DBG1(DBG_NET, "unable to set IPV6_PKTINFO on raw socket: %s",
575 strerror(errno));
576 close(skt);
577 return 0;
578 }
579
580 if (!hydra->kernel_interface->bypass_socket(hydra->kernel_interface,
581 skt, family))
582 {
583 DBG1(DBG_NET, "installing bypass policy on receive socket failed");
584 }
585
586 return skt;
587 }
588
589 METHOD(socket_t, destroy, void,
590 private_socket_raw_socket_t *this)
591 {
592 if (this->recv4)
593 {
594 close(this->recv4);
595 }
596 if (this->recv6)
597 {
598 close(this->recv6);
599 }
600 if (this->send4)
601 {
602 close(this->send4);
603 }
604 if (this->send6)
605 {
606 close(this->send6);
607 }
608 if (this->send4_natt)
609 {
610 close(this->send4_natt);
611 }
612 if (this->send6_natt)
613 {
614 close(this->send6_natt);
615 }
616 free(this);
617 }
618
619 /*
620 * See header for description
621 */
622 socket_raw_socket_t *socket_raw_socket_create()
623 {
624 private_socket_raw_socket_t *this;
625
626 INIT(this,
627 .public = {
628 .socket = {
629 .send = _sender,
630 .receive = _receiver,
631 .destroy = _destroy,
632 },
633 },
634 .max_packet = lib->settings->get_int(lib->settings,
635 "%s.max_packet", MAX_PACKET, charon->name),
636 );
637
638 this->recv4 = open_recv_socket(this, AF_INET);
639 if (this->recv4 == 0)
640 {
641 DBG1(DBG_NET, "could not open IPv4 receive socket, IPv4 disabled");
642 }
643 else
644 {
645 this->send4 = open_send_socket(this, AF_INET, CHARON_UDP_PORT);
646 if (this->send4 == 0)
647 {
648 DBG1(DBG_NET, "could not open IPv4 send socket, IPv4 disabled");
649 close(this->recv4);
650 }
651 else
652 {
653 this->send4_natt = open_send_socket(this, AF_INET, CHARON_NATT_PORT);
654 if (this->send4_natt == 0)
655 {
656 DBG1(DBG_NET, "could not open IPv4 NAT-T send socket");
657 }
658 }
659 }
660
661 this->recv6 = open_recv_socket(this, AF_INET6);
662 if (this->recv6 == 0)
663 {
664 DBG1(DBG_NET, "could not open IPv6 receive socket, IPv6 disabled");
665 }
666 else
667 {
668 this->send6 = open_send_socket(this, AF_INET6, CHARON_UDP_PORT);
669 if (this->send6 == 0)
670 {
671 DBG1(DBG_NET, "could not open IPv6 send socket, IPv6 disabled");
672 close(this->recv6);
673 }
674 else
675 {
676 this->send6_natt = open_send_socket(this, AF_INET6, CHARON_NATT_PORT);
677 if (this->send6_natt == 0)
678 {
679 DBG1(DBG_NET, "could not open IPv6 NAT-T send socket");
680 }
681 }
682 }
683
684 if (!(this->send4 || this->send6) || !(this->recv4 || this->recv6))
685 {
686 DBG1(DBG_NET, "could not create any sockets");
687 destroy(this);
688 return NULL;
689 }
690
691 return &this->public;
692 }