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