get rid of unused iterator hook functions
[strongswan.git] / src / charon / network / socket-raw.c
1 /*
2 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * $Id$
18 */
19
20 /* for struct in6_pktinfo */
21 #define _GNU_SOURCE
22
23 #include <pthread.h>
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/ip6.h>
35 #include <netinet/udp.h>
36 #include <linux/ipsec.h>
37 #include <linux/filter.h>
38 #include <net/if.h>
39
40 #include "socket.h"
41
42 #include <daemon.h>
43
44 /* constants for packet handling */
45 #define IP_LEN sizeof(struct iphdr)
46 #define IP6_LEN sizeof(struct ip6_hdr)
47 #define UDP_LEN sizeof(struct udphdr)
48 #define MARKER_LEN sizeof(u_int32_t)
49
50 /* offsets for packet handling */
51 #define IP_PROTO_OFFSET 9
52 #define IP6_PROTO_OFFSET 6
53 #define IKE_VERSION_OFFSET 17
54 #define IKE_LENGTH_OFFSET 24
55
56 /* from linux/in.h */
57 #ifndef IP_IPSEC_POLICY
58 #define IP_IPSEC_POLICY 16
59 #endif /*IP_IPSEC_POLICY*/
60
61 /* from linux/udp.h */
62 #ifndef UDP_ENCAP
63 #define UDP_ENCAP 100
64 #endif /*UDP_ENCAP*/
65
66 #ifndef UDP_ENCAP_ESPINUDP
67 #define UDP_ENCAP_ESPINUDP 2
68 #endif /*UDP_ENCAP_ESPINUDP*/
69
70 /* needed for older kernel headers */
71 #ifndef IPV6_2292PKTINFO
72 #define IPV6_2292PKTINFO 2
73 #endif /*IPV6_2292PKTINFO*/
74
75 /* missing on uclibc */
76 #ifndef IPV6_IPSEC_POLICY
77 #define IPV6_IPSEC_POLICY 34
78 #endif /*IPV6_IPSEC_POLICY*/
79
80 typedef struct private_socket_t private_socket_t;
81
82 /**
83 * Private data of an socket_t object
84 */
85 struct private_socket_t{
86 /**
87 * public functions
88 */
89 socket_t public;
90
91 /**
92 * regular port
93 */
94 int port;
95
96 /**
97 * port used for nat-t
98 */
99 int natt_port;
100
101 /**
102 * raw receiver socket for IPv4
103 */
104 int recv4;
105
106 /**
107 * raw receiver socket for IPv6
108 */
109 int recv6;
110
111 /**
112 * send socket on regular port for IPv4
113 */
114 int send4;
115
116 /**
117 * send socket on regular port for IPv6
118 */
119 int send6;
120
121 /**
122 * send socket on nat-t port for IPv4
123 */
124 int send4_natt;
125
126 /**
127 * send socket on nat-t port for IPv6
128 */
129 int send6_natt;
130 };
131
132 /**
133 * implementation of socket_t.receive
134 */
135 static status_t receiver(private_socket_t *this, packet_t **packet)
136 {
137 char buffer[MAX_PACKET];
138 chunk_t data;
139 packet_t *pkt;
140 struct udphdr *udp;
141 host_t *source = NULL, *dest = NULL;
142 int bytes_read = 0;
143 int data_offset, oldstate;
144 fd_set rfds;
145
146 FD_ZERO(&rfds);
147
148 if (this->recv4)
149 {
150 FD_SET(this->recv4, &rfds);
151 }
152 if (this->recv6)
153 {
154 FD_SET(this->recv6, &rfds);
155 }
156
157 DBG2(DBG_NET, "waiting for data on raw sockets");
158
159 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
160 if (select(max(this->recv4, this->recv6) + 1, &rfds, NULL, NULL, NULL) <= 0)
161 {
162 pthread_setcancelstate(oldstate, NULL);
163 return FAILED;
164 }
165 pthread_setcancelstate(oldstate, NULL);
166
167 if (this->recv4 && FD_ISSET(this->recv4, &rfds))
168 {
169 /* IPv4 raw sockets return the IP header. We read src/dest
170 * information directly from the raw header */
171 struct iphdr *ip;
172 struct sockaddr_in src, dst;
173
174 bytes_read = recv(this->recv4, buffer, MAX_PACKET, 0);
175 if (bytes_read < 0)
176 {
177 DBG1(DBG_NET, "error reading from IPv4 socket: %s", strerror(errno));
178 return FAILED;
179 }
180 DBG3(DBG_NET, "received IPv4 packet %b", buffer, bytes_read);
181
182 /* read source/dest from raw IP/UDP header */
183 if (bytes_read < IP_LEN + UDP_LEN + MARKER_LEN)
184 {
185 DBG1(DBG_NET, "received IPv4 packet too short (%d bytes)",
186 bytes_read);
187 return FAILED;
188 }
189 ip = (struct iphdr*) buffer;
190 udp = (struct udphdr*) (buffer + IP_LEN);
191 src.sin_family = AF_INET;
192 src.sin_addr.s_addr = ip->saddr;
193 src.sin_port = udp->source;
194 dst.sin_family = AF_INET;
195 dst.sin_addr.s_addr = ip->daddr;
196 dst.sin_port = udp->dest;
197 source = host_create_from_sockaddr((sockaddr_t*)&src);
198 dest = host_create_from_sockaddr((sockaddr_t*)&dst);
199
200 pkt = packet_create();
201 pkt->set_source(pkt, source);
202 pkt->set_destination(pkt, dest);
203 DBG2(DBG_NET, "received packet: from %#H to %#H", source, dest);
204 data_offset = IP_LEN + UDP_LEN;
205 /* remove non esp marker */
206 if (dest->get_port(dest) == IKEV2_NATT_PORT)
207 {
208 data_offset += MARKER_LEN;
209 }
210 /* fill in packet */
211 data.len = bytes_read - data_offset;
212 data.ptr = malloc(data.len);
213 memcpy(data.ptr, buffer + data_offset, data.len);
214 pkt->set_data(pkt, data);
215 }
216 else if (this->recv6 && FD_ISSET(this->recv6, &rfds))
217 {
218 /* IPv6 raw sockets return no IP header. We must query
219 * src/dest via socket options/ancillary data */
220 struct msghdr msg;
221 struct cmsghdr *cmsgptr;
222 struct sockaddr_in6 src, dst;
223 struct iovec iov;
224 char ancillary[64];
225
226 msg.msg_name = &src;
227 msg.msg_namelen = sizeof(src);
228 iov.iov_base = buffer;
229 iov.iov_len = sizeof(buffer);
230 msg.msg_iov = &iov;
231 msg.msg_iovlen = 1;
232 msg.msg_control = ancillary;
233 msg.msg_controllen = sizeof(ancillary);
234 msg.msg_flags = 0;
235
236 bytes_read = recvmsg(this->recv6, &msg, 0);
237 if (bytes_read < 0)
238 {
239 DBG1(DBG_NET, "error reading from IPv6 socket: %s", strerror(errno));
240 return FAILED;
241 }
242 DBG3(DBG_NET, "received IPv6 packet %b", buffer, bytes_read);
243
244 if (bytes_read < IP_LEN + UDP_LEN + MARKER_LEN)
245 {
246 DBG3(DBG_NET, "received IPv6 packet too short (%d bytes)",
247 bytes_read);
248 return FAILED;
249 }
250
251 /* read ancillary data to get destination address */
252 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
253 cmsgptr = CMSG_NXTHDR(&msg, cmsgptr))
254 {
255 if (cmsgptr->cmsg_len == 0)
256 {
257 DBG1(DBG_NET, "error reading IPv6 ancillary data");
258 return FAILED;
259 }
260 if (cmsgptr->cmsg_level == SOL_IPV6 &&
261 cmsgptr->cmsg_type == IPV6_2292PKTINFO)
262 {
263 struct in6_pktinfo *pktinfo;
264 pktinfo = (struct in6_pktinfo*)CMSG_DATA(cmsgptr);
265
266 memset(&dst, 0, sizeof(dst));
267 memcpy(&dst.sin6_addr, &pktinfo->ipi6_addr, sizeof(dst.sin6_addr));
268 dst.sin6_family = AF_INET6;
269 udp = (struct udphdr*) (buffer);
270 dst.sin6_port = udp->dest;
271 src.sin6_port = udp->source;
272 dest = host_create_from_sockaddr((sockaddr_t*)&dst);
273 }
274 }
275 /* ancillary data missing? */
276 if (dest == NULL)
277 {
278 DBG1(DBG_NET, "error reading IPv6 packet header");
279 return FAILED;
280 }
281
282 source = host_create_from_sockaddr((sockaddr_t*)&src);
283
284 pkt = packet_create();
285 pkt->set_source(pkt, source);
286 pkt->set_destination(pkt, dest);
287 DBG2(DBG_NET, "received packet: from %#H to %#H", source, dest);
288 data_offset = UDP_LEN;
289 /* remove non esp marker */
290 if (dest->get_port(dest) == IKEV2_NATT_PORT)
291 {
292 data_offset += MARKER_LEN;
293 }
294 /* fill in packet */
295 data.len = bytes_read - data_offset;
296 data.ptr = malloc(data.len);
297 memcpy(data.ptr, buffer + data_offset, data.len);
298 pkt->set_data(pkt, data);
299 }
300 else
301 {
302 /* oops, shouldn't happen */
303 return FAILED;
304 }
305
306 /* return packet */
307 *packet = pkt;
308 return SUCCESS;
309 }
310
311 /**
312 * implementation of socket_t.send
313 */
314 status_t sender(private_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 == IKEV2_UDP_PORT)
334 {
335 if (family == AF_INET)
336 {
337 skt = this->send4;
338 }
339 else
340 {
341 skt = this->send6;
342 }
343 }
344 else if (sport == IKEV2_NATT_PORT)
345 {
346 if (family == AF_INET)
347 {
348 skt = this->send4_natt;
349 }
350 else
351 {
352 skt = this->send6_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 if (data.len > MAX_PACKET - MARKER_LEN)
359 {
360 DBG1(DBG_NET, "unable to send packet: it's too big (%d bytes)",
361 data.len);
362 return FAILED;
363 }
364 marked = chunk_alloc(data.len + MARKER_LEN);
365 memset(marked.ptr, 0, MARKER_LEN);
366 memcpy(marked.ptr + MARKER_LEN, data.ptr, data.len);
367 /* let the packet do the clean up for us */
368 packet->set_data(packet, marked);
369 data = marked;
370 }
371 }
372 else
373 {
374 DBG1(DBG_NET, "unable to locate a send socket for port %d", sport);
375 return FAILED;
376 }
377
378 memset(&msg, 0, sizeof(struct msghdr));
379 msg.msg_name = dst->get_sockaddr(dst);;
380 msg.msg_namelen = *dst->get_sockaddr_len(dst);
381 iov.iov_base = data.ptr;
382 iov.iov_len = data.len;
383 msg.msg_iov = &iov;
384 msg.msg_iovlen = 1;
385 msg.msg_flags = 0;
386
387 if (!dst->is_anyaddr(dst))
388 {
389 if (family == AF_INET)
390 {
391 char buf[CMSG_SPACE(sizeof(struct in_pktinfo))];
392 struct in_pktinfo *pktinfo;
393 struct sockaddr_in *sin;
394
395 msg.msg_control = buf;
396 msg.msg_controllen = sizeof(buf);
397 cmsg = CMSG_FIRSTHDR(&msg);
398 cmsg->cmsg_level = SOL_IP;
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 sin = (struct sockaddr_in*)src->get_sockaddr(src);
404 memcpy(&pktinfo->ipi_spec_dst, &sin->sin_addr, sizeof(struct in_addr));
405 }
406 else
407 {
408 char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
409 struct in6_pktinfo *pktinfo;
410 struct sockaddr_in6 *sin;
411
412 msg.msg_control = buf;
413 msg.msg_controllen = sizeof(buf);
414 cmsg = CMSG_FIRSTHDR(&msg);
415 cmsg->cmsg_level = SOL_IPV6;
416 cmsg->cmsg_type = IPV6_2292PKTINFO;
417 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
418 pktinfo = (struct in6_pktinfo*)CMSG_DATA(cmsg);
419 memset(pktinfo, 0, sizeof(struct in6_pktinfo));
420 sin = (struct sockaddr_in6*)src->get_sockaddr(src);
421 memcpy(&pktinfo->ipi6_addr, &sin->sin6_addr, sizeof(struct in6_addr));
422 }
423 }
424
425 bytes_sent = sendmsg(skt, &msg, 0);
426
427 if (bytes_sent != data.len)
428 {
429 DBG1(DBG_NET, "error writing to socket: %s", strerror(errno));
430 return FAILED;
431 }
432 return SUCCESS;
433 }
434
435 /**
436 * open a socket to send packets
437 */
438 static int open_send_socket(private_socket_t *this, int family, u_int16_t port)
439 {
440 int on = TRUE;
441 int type = UDP_ENCAP_ESPINUDP;
442 struct sockaddr_storage addr;
443 u_int sol, ipsec_policy;
444 struct sadb_x_policy policy;
445 int skt;
446
447 memset(&addr, 0, sizeof(addr));
448 /* precalculate constants depending on address family */
449 switch (family)
450 {
451 case AF_INET:
452 {
453 struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
454 sin->sin_family = AF_INET;
455 sin->sin_addr.s_addr = INADDR_ANY;
456 sin->sin_port = htons(port);
457 sol = SOL_IP;
458 ipsec_policy = IP_IPSEC_POLICY;
459 break;
460 }
461 case AF_INET6:
462 {
463 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
464 sin6->sin6_family = AF_INET6;
465 memcpy(&sin6->sin6_addr, &in6addr_any, sizeof(in6addr_any));
466 sin6->sin6_port = htons(port);
467 sol = SOL_IPV6;
468 ipsec_policy = IPV6_IPSEC_POLICY;
469 break;
470 }
471 default:
472 return 0;
473 }
474
475 skt = socket(family, SOCK_DGRAM, IPPROTO_UDP);
476 if (skt < 0)
477 {
478 DBG1(DBG_NET, "could not open send socket: %s", strerror(errno));
479 return 0;
480 }
481
482 if (setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) < 0)
483 {
484 DBG1(DBG_NET, "unable to set SO_REUSEADDR on send socket: %s",
485 strerror(errno));
486 close(skt);
487 return 0;
488 }
489
490 /* bypass outgoung IKE traffic on send socket */
491 memset(&policy, 0, sizeof(policy));
492 policy.sadb_x_policy_len = sizeof(policy) / sizeof(u_int64_t);
493 policy.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
494 policy.sadb_x_policy_type = IPSEC_POLICY_BYPASS;
495 policy.sadb_x_policy_dir = IPSEC_DIR_OUTBOUND;
496
497 if (setsockopt(skt, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
498 {
499 DBG1(DBG_NET, "unable to set IPSEC_POLICY on send socket: %s",
500 strerror(errno));
501 close(skt);
502 return 0;
503 }
504
505 /* We don't receive packets on the send socket, but we need a INBOUND policy.
506 * Otherwise, UDP decapsulation does not work!!! */
507 policy.sadb_x_policy_dir = IPSEC_DIR_INBOUND;
508 if (setsockopt(skt, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
509 {
510 DBG1(DBG_NET, "unable to set IPSEC_POLICY on send socket: %s",
511 strerror(errno));
512 close(skt);
513 return 0;
514 }
515
516 /* bind the send socket */
517 if (bind(skt, (struct sockaddr *)&addr, sizeof(addr)) < 0)
518 {
519 DBG1(DBG_NET, "unable to bind send socket: %s",
520 strerror(errno));
521 close(skt);
522 return 0;
523 }
524
525 if (family == AF_INET)
526 {
527 /* enable UDP decapsulation globally, only for one socket needed */
528 if (setsockopt(skt, SOL_UDP, UDP_ENCAP, &type, sizeof(type)) < 0)
529 {
530 DBG1(DBG_NET, "unable to set UDP_ENCAP: %s; NAT-T may fail",
531 strerror(errno));
532 }
533 }
534
535 return skt;
536 }
537
538 /**
539 * open a socket to receive packets
540 */
541 static int open_recv_socket(private_socket_t *this, int family)
542 {
543 int skt;
544 int on = TRUE;
545 u_int proto_offset, ip_len, sol, ipsec_policy, udp_header, ike_header;
546 struct sadb_x_policy policy;
547
548 /* precalculate constants depending on address family */
549 switch (family)
550 {
551 case AF_INET:
552 proto_offset = IP_PROTO_OFFSET;
553 ip_len = IP_LEN;
554 sol = SOL_IP;
555 ipsec_policy = IP_IPSEC_POLICY;
556 break;
557 case AF_INET6:
558 proto_offset = IP6_PROTO_OFFSET;
559 ip_len = 0; /* IPv6 raw sockets contain no IP header */
560 sol = SOL_IPV6;
561 ipsec_policy = IPV6_IPSEC_POLICY;
562 break;
563 default:
564 return 0;
565 }
566 udp_header = ip_len;
567 ike_header = ip_len + UDP_LEN;
568
569 /* This filter code filters out all non-IKEv2 traffic on
570 * a SOCK_RAW IP_PROTP_UDP socket. Handling of other
571 * IKE versions is done in pluto.
572 */
573 struct sock_filter ikev2_filter_code[] =
574 {
575 /* Destination Port must be either port or natt_port */
576 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, udp_header + 2),
577 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, IKEV2_UDP_PORT, 1, 0),
578 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, IKEV2_NATT_PORT, 5, 12),
579 /* port */
580 /* IKE version must be 2.0 */
581 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, ike_header + IKE_VERSION_OFFSET),
582 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x20, 0, 10),
583 /* packet length is length in IKEv2 header + ip header + udp header */
584 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, ike_header + IKE_LENGTH_OFFSET),
585 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, ip_len + UDP_LEN),
586 BPF_STMT(BPF_RET+BPF_A, 0),
587 /* natt_port */
588 /* nat-t: check for marker */
589 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, ike_header),
590 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0, 0, 5),
591 /* nat-t: IKE version must be 2.0 */
592 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, ike_header + MARKER_LEN + IKE_VERSION_OFFSET),
593 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x20, 0, 3),
594 /* nat-t: packet length is length in IKEv2 header + ip header + udp header + non esp marker */
595 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, ike_header + MARKER_LEN + IKE_LENGTH_OFFSET),
596 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, ip_len + UDP_LEN + MARKER_LEN),
597 BPF_STMT(BPF_RET+BPF_A, 0),
598 /* packet doesn't match, ignore */
599 BPF_STMT(BPF_RET+BPF_K, 0),
600 };
601
602 /* Filter struct to use with setsockopt */
603 struct sock_fprog ikev2_filter = {
604 sizeof(ikev2_filter_code) / sizeof(struct sock_filter),
605 ikev2_filter_code
606 };
607
608 /* set up a raw socket */
609 skt = socket(family, SOCK_RAW, IPPROTO_UDP);
610 if (skt < 0)
611 {
612 DBG1(DBG_NET, "unable to create raw socket: %s", strerror(errno));
613 return 0;
614 }
615
616 if (setsockopt(skt, SOL_SOCKET, SO_ATTACH_FILTER,
617 &ikev2_filter, sizeof(ikev2_filter)) < 0)
618 {
619 DBG1(DBG_NET, "unable to attach IKEv2 filter to raw socket: %s",
620 strerror(errno));
621 close(skt);
622 return 0;
623 }
624
625 if (family == AF_INET6 &&
626 /* we use IPV6_2292PKTINFO, as IPV6_PKTINFO is defined as
627 * 2 or 50 depending on kernel header version */
628 setsockopt(skt, sol, IPV6_2292PKTINFO, &on, sizeof(on)) < 0)
629 {
630 DBG1(DBG_NET, "unable to set IPV6_PKTINFO on raw socket: %s",
631 strerror(errno));
632 close(skt);
633 return 0;
634 }
635
636 /* bypass incomining IKE traffic on this socket */
637 memset(&policy, 0, sizeof(policy));
638 policy.sadb_x_policy_len = sizeof(policy) / sizeof(u_int64_t);
639 policy.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
640 policy.sadb_x_policy_type = IPSEC_POLICY_BYPASS;
641 policy.sadb_x_policy_dir = IPSEC_DIR_INBOUND;
642
643 if (setsockopt(skt, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
644 {
645 DBG1(DBG_NET, "unable to set IPSEC_POLICY on raw socket: %s",
646 strerror(errno));
647 close(skt);
648 return 0;
649 }
650
651 return skt;
652 }
653
654 /**
655 * implementation of socket_t.destroy
656 */
657 static void destroy(private_socket_t *this)
658 {
659 if (this->recv4)
660 {
661 close(this->recv4);
662 }
663 if (this->recv6)
664 {
665 close(this->recv6);
666 }
667 if (this->send4)
668 {
669 close(this->send4);
670 }
671 if (this->send6)
672 {
673 close(this->send6);
674 }
675 if (this->send4_natt)
676 {
677 close(this->send4_natt);
678 }
679 if (this->send6_natt)
680 {
681 close(this->send6_natt);
682 }
683 free(this);
684 }
685
686 /*
687 * See header for description
688 */
689 socket_t *socket_create()
690 {
691 int key;
692 private_socket_t *this = malloc_thing(private_socket_t);
693
694 /* public functions */
695 this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
696 this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
697 this->public.destroy = (void(*)(socket_t*)) destroy;
698
699 this->recv4 = 0;
700 this->recv6 = 0;
701 this->send4 = 0;
702 this->send6 = 0;
703 this->send4_natt = 0;
704 this->send6_natt = 0;
705
706 /* we open a AF_KEY socket to autoload the af_key module. Otherwise
707 * setsockopt(IPSEC_POLICY) won't work. */
708 key = socket(AF_KEY, SOCK_RAW, PF_KEY_V2);
709 if (key == 0)
710 {
711 charon->kill(charon, "could not open AF_KEY socket");
712 }
713 close(key);
714
715 this->recv4 = open_recv_socket(this, AF_INET);
716 if (this->recv4 == 0)
717 {
718 DBG1(DBG_NET, "could not open IPv4 receive socket, IPv4 disabled");
719 }
720 else
721 {
722 this->send4 = open_send_socket(this, AF_INET, IKEV2_UDP_PORT);
723 if (this->send4 == 0)
724 {
725 DBG1(DBG_NET, "could not open IPv4 send socket, IPv4 disabled");
726 close(this->recv4);
727 }
728 else
729 {
730 this->send4_natt = open_send_socket(this, AF_INET, IKEV2_NATT_PORT);
731 if (this->send4_natt == 0)
732 {
733 DBG1(DBG_NET, "could not open IPv4 NAT-T send socket");
734 }
735 }
736 }
737
738 this->recv6 = open_recv_socket(this, AF_INET6);
739 if (this->recv6 == 0)
740 {
741 DBG1(DBG_NET, "could not open IPv6 receive socket, IPv6 disabled");
742 }
743 else
744 {
745 this->send6 = open_send_socket(this, AF_INET6, IKEV2_UDP_PORT);
746 if (this->send6 == 0)
747 {
748 DBG1(DBG_NET, "could not open IPv6 send socket, IPv6 disabled");
749 close(this->recv6);
750 }
751 else
752 {
753 this->send6_natt = open_send_socket(this, AF_INET6, IKEV2_NATT_PORT);
754 if (this->send6_natt == 0)
755 {
756 DBG1(DBG_NET, "could not open IPv6 NAT-T send socket");
757 }
758 }
759 }
760
761 if (!(this->send4 || this->send6) || !(this->recv4 || this->recv6))
762 {
763 DBG1(DBG_NET, "could not create any sockets");
764 destroy(this);
765 charon->kill(charon, "socket initialization failed");
766 }
767
768 return (socket_t*)this;
769 }