]>
git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/sockmisc.c
1 /* $Id: sockmisc.c,v 1.17.4.4 2005/10/04 09:54:27 manubsd Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #ifndef HAVE_NETINET6_IPSEC
41 #include <netinet/ipsec.h>
43 #include <netinet6/ipsec.h>
46 #if defined(INET6) && !defined(INET6_ADVAPI) && \
47 defined(IP_RECVDSTADDR) && !defined(IPV6_RECVDSTADDR)
48 #define IPV6_RECVDSTADDR IP_RECVDSTADDR
68 #ifndef IP_IPSEC_POLICY
69 #define IP_IPSEC_POLICY 16 /* XXX: from linux/in.h */
72 #ifndef IPV6_IPSEC_POLICY
73 #define IPV6_IPSEC_POLICY 34 /* XXX: from linux/???.h per
74 "Tom Lendacky" <toml@us.ibm.com> */
77 const int niflags
= 0;
80 * compare two sockaddr without port number.
85 cmpsaddrwop(addr1
, addr2
)
86 const struct sockaddr_storage
*addr1
;
87 const struct sockaddr_storage
*addr2
;
91 if (addr1
== 0 && addr2
== 0)
93 if (addr1
== 0 || addr2
== 0)
96 if (addr1
->ss_len
!= addr2
->ss_len
97 || addr1
->ss_family
!= addr2
->ss_family
)
99 switch (addr1
->ss_family
) {
101 sa1
= (caddr_t
)&((struct sockaddr_in
*)addr1
)->sin_addr
;
102 sa2
= (caddr_t
)&((struct sockaddr_in
*)addr2
)->sin_addr
;
103 if (memcmp(sa1
, sa2
, sizeof(struct in_addr
)) != 0)
108 sa1
= (caddr_t
)&((struct sockaddr_in6
*)addr1
)->sin6_addr
;
109 sa2
= (caddr_t
)&((struct sockaddr_in6
*)addr2
)->sin6_addr
;
110 if (memcmp(sa1
, sa2
, sizeof(struct in6_addr
)) != 0)
112 if (((struct sockaddr_in6
*)addr1
)->sin6_scope_id
!=
113 ((struct sockaddr_in6
*)addr2
)->sin6_scope_id
)
125 * compare two sockaddr without port number using prefix.
130 cmpsaddrwop_withprefix(const struct sockaddr_storage
*addr1
, const struct sockaddr_storage
*addr2
, int prefix
)
135 if (addr1
== 0 && addr2
== 0)
137 if (addr1
== 0 || addr2
== 0)
140 if (addr1
->ss_len
!= addr2
->ss_len
141 || addr1
->ss_family
!= addr2
->ss_family
)
143 switch (addr1
->ss_family
) {
147 if ((((struct sockaddr_in
*)addr1
)->sin_addr
.s_addr
& htonl(mask
)) !=
148 (((struct sockaddr_in
*)addr2
)->sin_addr
.s_addr
& htonl(mask
)))
153 for (i
= 0; i
< 4; i
++) {
157 } else if (prefix
== 0)
164 if ((((struct sockaddr_in6
*)addr1
)->sin6_addr
.__u6_addr
.__u6_addr32
[i
] & htonl(mask
)) !=
165 (((struct sockaddr_in6
*)addr2
)->sin6_addr
.__u6_addr
.__u6_addr32
[i
] & htonl(mask
)))
168 if (((struct sockaddr_in6
*)addr1
)->sin6_scope_id
!=
169 ((struct sockaddr_in6
*)addr2
)->sin6_scope_id
)
182 * compare two sockaddr with port, taking care wildcard.
183 * addr1 is a subject address, addr2 is in a database entry.
188 cmpsaddrwild(addr1
, addr2
)
189 const struct sockaddr_storage
*addr1
;
190 const struct sockaddr_storage
*addr2
;
193 u_short port1
, port2
;
195 if (addr1
== 0 && addr2
== 0)
197 if (addr1
== 0 || addr2
== 0)
200 if (addr1
->ss_len
!= addr2
->ss_len
201 || addr1
->ss_family
!= addr2
->ss_family
)
204 switch (addr1
->ss_family
) {
206 sa1
= (caddr_t
)&((struct sockaddr_in
*)addr1
)->sin_addr
;
207 sa2
= (caddr_t
)&((struct sockaddr_in
*)addr2
)->sin_addr
;
208 port1
= ((struct sockaddr_in
*)addr1
)->sin_port
;
209 port2
= ((struct sockaddr_in
*)addr2
)->sin_port
;
210 if (!(port1
== IPSEC_PORT_ANY
||
211 port2
== IPSEC_PORT_ANY
||
214 if (memcmp(sa1
, sa2
, sizeof(struct in_addr
)) != 0)
219 sa1
= (caddr_t
)&((struct sockaddr_in6
*)addr1
)->sin6_addr
;
220 sa2
= (caddr_t
)&((struct sockaddr_in6
*)addr2
)->sin6_addr
;
221 port1
= ((struct sockaddr_in6
*)addr1
)->sin6_port
;
222 port2
= ((struct sockaddr_in6
*)addr2
)->sin6_port
;
223 if (!(port1
== IPSEC_PORT_ANY
||
224 port2
== IPSEC_PORT_ANY
||
227 if (memcmp(sa1
, sa2
, sizeof(struct in6_addr
)) != 0)
229 if (((struct sockaddr_in6
*)addr1
)->sin6_scope_id
!=
230 ((struct sockaddr_in6
*)addr2
)->sin6_scope_id
)
242 * compare two sockaddr with strict match on port.
247 cmpsaddrstrict(const struct sockaddr_storage
*addr1
, const struct sockaddr_storage
*addr2
)
250 u_short port1
, port2
;
252 if (addr1
== 0 && addr2
== 0)
254 if (addr1
== 0 || addr2
== 0)
257 if (addr1
->ss_len
!= addr2
->ss_len
258 || addr1
->ss_family
!= addr2
->ss_family
)
261 switch (addr1
->ss_family
) {
263 sa1
= (caddr_t
)&((struct sockaddr_in
*)addr1
)->sin_addr
;
264 sa2
= (caddr_t
)&((struct sockaddr_in
*)addr2
)->sin_addr
;
265 port1
= ((struct sockaddr_in
*)addr1
)->sin_port
;
266 port2
= ((struct sockaddr_in
*)addr2
)->sin_port
;
269 if (memcmp(sa1
, sa2
, sizeof(struct in_addr
)) != 0)
274 sa1
= (caddr_t
)&((struct sockaddr_in6
*)addr1
)->sin6_addr
;
275 sa2
= (caddr_t
)&((struct sockaddr_in6
*)addr2
)->sin6_addr
;
276 port1
= ((struct sockaddr_in6
*)addr1
)->sin6_port
;
277 port2
= ((struct sockaddr_in6
*)addr2
)->sin6_port
;
280 if (memcmp(sa1
, sa2
, sizeof(struct in6_addr
)) != 0)
282 if (((struct sockaddr_in6
*)addr1
)->sin6_scope_id
!=
283 ((struct sockaddr_in6
*)addr2
)->sin6_scope_id
)
295 * compare two sockaddr with strict match on port using prefix.
300 cmpsaddrstrict_withprefix(const struct sockaddr_storage
*addr1
, const struct sockaddr_storage
*addr2
, int prefix
)
302 u_short port1
, port2
;
306 if (addr1
== 0 && addr2
== 0)
308 if (addr1
== 0 || addr2
== 0)
311 if (addr1
->ss_len
!= addr2
->ss_len
312 || addr1
->ss_family
!= addr2
->ss_family
)
315 switch (addr1
->ss_family
) {
317 port1
= ((struct sockaddr_in
*)addr1
)->sin_port
;
318 port2
= ((struct sockaddr_in
*)addr2
)->sin_port
;
323 if ((((struct sockaddr_in
*)addr1
)->sin_addr
.s_addr
& htonl(mask
)) !=
324 (((struct sockaddr_in
*)addr2
)->sin_addr
.s_addr
& htonl(mask
)))
329 port1
= ((struct sockaddr_in6
*)addr1
)->sin6_port
;
330 port2
= ((struct sockaddr_in6
*)addr2
)->sin6_port
;
333 for (i
= 0; i
< 4; i
++) {
337 } else if (prefix
== 0)
344 if ((((struct sockaddr_in6
*)addr1
)->sin6_addr
.__u6_addr
.__u6_addr32
[i
] & htonl(mask
)) !=
345 (((struct sockaddr_in6
*)addr2
)->sin6_addr
.__u6_addr
.__u6_addr32
[i
] & htonl(mask
)))
348 if (((struct sockaddr_in6
*)addr1
)->sin6_scope_id
!=
349 ((struct sockaddr_in6
*)addr2
)->sin6_scope_id
)
361 /* get local address against the destination. */
362 struct sockaddr_storage
*
363 getlocaladdr(struct sockaddr
*remote
)
365 struct sockaddr_storage
*local
;
366 u_int local_len
= sizeof(struct sockaddr_storage
);
367 int s
; /* for dummy connection */
369 /* allocate buffer */
370 if ((local
= racoon_calloc(1, local_len
)) == NULL
) {
372 "failed to get address buffer.\n");
376 /* get real interface received packet */
377 if ((s
= socket(remote
->sa_family
, SOCK_DGRAM
, 0)) < 0) {
379 "socket (%s)\n", strerror(errno
));
383 if (fcntl(s
, F_SETFL
, O_NONBLOCK
) == -1) {
384 plog(ASL_LEVEL_ERR
, "failed to put localaddr socket in non-blocking mode\n");
387 setsockopt_bypass(s
, remote
->sa_family
);
389 if (connect(s
, remote
, sysdep_sa_len(remote
)) < 0) {
391 "connect (%s)\n", strerror(errno
));
396 if (getsockname(s
, (struct sockaddr
*)local
, &local_len
) < 0) {
398 "getsockname (%s)\n", strerror(errno
));
413 * Receive packet, with src/dst information. It is assumed that necessary
414 * setsockopt() have already performed on socket.
421 struct sockaddr_storage
*from
,
423 struct sockaddr_storage
*to
,
428 struct sockaddr_storage ss
;
430 struct cmsghdr
*cm
, *cm_prev
;
432 u_int32_t cmsgbuf
[256/sizeof(u_int32_t
)]; // Wcast-align fix - force 32 bit alignment
433 #if defined(INET6) && defined(INET6_ADVAPI)
434 struct in6_pktinfo
*pi
;
435 #endif /*INET6_ADVAPI*/
436 struct sockaddr_in
*sin
;
438 struct sockaddr_in6
*sin6
;
442 if (getsockname(s
, (struct sockaddr
*)&ss
, (socklen_t
*)&len
) < 0) {
444 "getsockname (%s)\n", strerror(errno
));
448 m
.msg_name
= (caddr_t
)from
;
449 m
.msg_namelen
= *fromlen
;
450 iov
[0].iov_base
= (caddr_t
)buf
;
451 iov
[0].iov_len
= buflen
;
454 memset(cmsgbuf
, 0, sizeof(cmsgbuf
));
455 cm
= (struct cmsghdr
*)cmsgbuf
;
456 m
.msg_control
= (caddr_t
)cm
;
457 m
.msg_controllen
= sizeof(cmsgbuf
);
459 while ((len
= recvmsg(s
, &m
, flags
)) < 0) {
462 plog(ASL_LEVEL_ERR
, "recvmsg (%s)\n", strerror(errno
));
468 *fromlen
= m
.msg_namelen
;
472 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(&m
), cm_prev
= NULL
;
473 m
.msg_controllen
!= 0 && cm
&& cm
!= cm_prev
;
474 cm_prev
= cm
, cm
= (struct cmsghdr
*)CMSG_NXTHDR(&m
, cm
)) {
477 "cmsg %d %d\n", cm
->cmsg_level
, cm
->cmsg_type
);)
479 #if defined(INET6) && defined(INET6_ADVAPI)
480 if (ss
.ss_family
== AF_INET6
481 && cm
->cmsg_level
== IPPROTO_IPV6
482 && cm
->cmsg_type
== IPV6_PKTINFO
483 && otolen
>= sizeof(*sin6
)) {
484 pi
= ALIGNED_CAST(struct in6_pktinfo
*)(CMSG_DATA(cm
));
485 *tolen
= sizeof(*sin6
);
486 sin6
= (struct sockaddr_in6
*)to
;
487 memset(sin6
, 0, sizeof(*sin6
));
488 sin6
->sin6_family
= AF_INET6
;
489 sin6
->sin6_len
= sizeof(*sin6
);
490 memcpy(&sin6
->sin6_addr
, &pi
->ipi6_addr
,
491 sizeof(sin6
->sin6_addr
));
492 /* XXX other cases, such as site-local? */
493 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
))
494 sin6
->sin6_scope_id
= pi
->ipi6_ifindex
;
496 sin6
->sin6_scope_id
= 0;
498 ((struct sockaddr_in6
*)&ss
)->sin6_port
;
499 otolen
= -1; /* "to" already set */
503 #if defined(INET6) && defined(IPV6_RECVDSTADDR)
504 if (ss
.ss_family
== AF_INET6
505 && cm
->cmsg_level
== IPPROTO_IPV6
506 && cm
->cmsg_type
== IPV6_RECVDSTADDR
507 && otolen
>= sizeof(*sin6
)) {
508 *tolen
= sizeof(*sin6
);
509 sin6
= (struct sockaddr_in6
*)to
;
510 memset(sin6
, 0, sizeof(*sin6
));
511 sin6
->sin6_family
= AF_INET6
;
512 sin6
->sin6_len
= sizeof(*sin6
);
513 memcpy(&sin6
->sin6_addr
, CMSG_DATA(cm
),
514 sizeof(sin6
->sin6_addr
));
516 ((struct sockaddr_in6
*)&ss
)->sin6_port
;
517 otolen
= -1; /* "to" already set */
521 if (ss
.ss_family
== AF_INET
522 && cm
->cmsg_level
== IPPROTO_IP
523 && cm
->cmsg_type
== IP_RECVDSTADDR
524 && otolen
>= sizeof(*sin
)) {
525 *tolen
= sizeof(*sin
);
526 sin
= (struct sockaddr_in
*)to
;
527 memset(sin
, 0, sizeof(*sin
));
528 sin
->sin_family
= AF_INET
;
529 sin
->sin_len
= sizeof(*sin
);
530 memcpy(&sin
->sin_addr
, CMSG_DATA(cm
),
531 sizeof(sin
->sin_addr
));
532 sin
->sin_port
= ((struct sockaddr_in
*)&ss
)->sin_port
;
533 otolen
= -1; /* "to" already set */
537 plogdump(ASL_LEVEL_DEBUG
, buf
, buflen
, "@@@@@@ data from readmsg:\n");
541 /* send packet, with fixing src/dst address pair. */
543 sendfromto(s
, buf
, buflen
, src
, dst
, cnt
)
547 struct sockaddr_storage
*src
;
548 struct sockaddr_storage
*dst
;
550 struct sockaddr_storage ss
;
554 if (src
->ss_family
!= dst
->ss_family
) {
556 "address family mismatch\n");
561 if (getsockname(s
, (struct sockaddr
*)&ss
, (socklen_t
*)&len
) < 0) {
563 "getsockname (%s)\n", strerror(errno
));
567 plog(ASL_LEVEL_DEBUG
,
568 "sockname %s\n", saddr2str((struct sockaddr
*)&ss
));
569 plog(ASL_LEVEL_DEBUG
,
570 "send packet from %s\n", saddr2str((struct sockaddr
*)src
));
571 plog(ASL_LEVEL_DEBUG
,
572 "send packet to %s\n", saddr2str((struct sockaddr
*)dst
));
574 if (src
->ss_family
!= ss
.ss_family
) {
576 "address family mismatch\n");
580 switch (src
->ss_family
) {
581 #if defined(INET6) && defined(INET6_ADVAPI)
587 u_int32_t cmsgbuf
[256/sizeof(u_int32_t
)]; // Wcast-align fix - force 32 bit alignment
588 struct in6_pktinfo
*pi
;
590 struct sockaddr_in6 src6
, dst6
;
592 memcpy(&src6
, src
, sizeof(src6
));
593 memcpy(&dst6
, dst
, sizeof(dst6
));
595 /* XXX take care of other cases, such as site-local */
597 if (IN6_IS_ADDR_LINKLOCAL(&src6
.sin6_addr
)
598 || IN6_IS_ADDR_MULTICAST(&src6
.sin6_addr
)) {
599 ifindex
= src6
.sin6_scope_id
; /*???*/
602 /* XXX some sanity check on dst6.sin6_scope_id */
604 /* flowinfo for IKE? mmm, maybe useful but for now make it 0 */
605 src6
.sin6_flowinfo
= dst6
.sin6_flowinfo
= 0;
607 memset(&m
, 0, sizeof(m
));
608 m
.msg_name
= (caddr_t
)&dst6
;
609 m
.msg_namelen
= sizeof(dst6
);
610 iov
[0].iov_base
= (char *)buf
;
611 iov
[0].iov_len
= buflen
;
615 memset(cmsgbuf
, 0, sizeof(cmsgbuf
));
616 cm
= (struct cmsghdr
*)cmsgbuf
;
617 m
.msg_control
= (caddr_t
)cm
;
618 m
.msg_controllen
= CMSG_SPACE(sizeof(struct in6_pktinfo
));
620 cm
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
621 cm
->cmsg_level
= IPPROTO_IPV6
;
622 cm
->cmsg_type
= IPV6_PKTINFO
;
623 pi
= ALIGNED_CAST(struct in6_pktinfo
*)CMSG_DATA(cm
);
624 memcpy(&pi
->ipi6_addr
, &src6
.sin6_addr
, sizeof(src6
.sin6_addr
));
625 pi
->ipi6_ifindex
= ifindex
;
627 plog(ASL_LEVEL_DEBUG
,
629 saddr2str((struct sockaddr
*)&src6
),
631 plog(ASL_LEVEL_DEBUG
,
633 saddr2str((struct sockaddr
*)&dst6
),
636 for (i
= 0; i
< cnt
; i
++) {
637 len
= sendmsg(s
, &m
, 0 /*MSG_DONTROUTE*/);
640 "sendmsg (%s)\n", strerror(errno
));
641 if (errno
!= EHOSTUNREACH
&& errno
!= ENETDOWN
&& errno
!= ENETUNREACH
) {
644 // <rdar://problem/6609744> treat these failures like
645 // packet loss, in case the network interface is flaky
648 plog(ASL_LEVEL_DEBUG
,
649 "%d times of %d bytes message will be sent "
651 i
+ 1, len
, saddr2str((struct sockaddr
*)dst
));
662 if (ss
.ss_family
== src
->ss_family
&& memcmp(&ss
, src
, sysdep_sa_len((struct sockaddr
*)src
)) == 0) {
668 * Use newly opened socket for sending packets.
669 * NOTE: this is unsafe, because if the peer is quick enough
670 * the packet from the peer may be queued into sendsock.
671 * Better approach is to prepare bind'ed udp sockets for
672 * each of the interface addresses.
674 sendsock
= socket(src
->ss_family
, SOCK_DGRAM
, 0);
677 "socket (%s)\n", strerror(errno
));
680 if (fcntl(sendsock
, F_SETFL
, O_NONBLOCK
) == -1) {
681 plog(ASL_LEVEL_ERR
, "failed to put sendsock socket in non-blocking mode\n");
683 if (setsockopt(sendsock
, SOL_SOCKET
,
685 (void *)&yes
, sizeof(yes
)) < 0) {
687 "setsockopt SO_REUSEPORT (%s)\n",
692 #ifdef IPV6_USE_MIN_MTU
693 if (src
->ss_family
== AF_INET6
&&
694 setsockopt(sendsock
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
695 (void *)&yes
, sizeof(yes
)) < 0) {
697 "setsockopt IPV6_USE_MIN_MTU (%s)\n",
703 if (setsockopt_bypass(sendsock
, src
->ss_family
) < 0) {
708 if (bind(sendsock
, (struct sockaddr
*)src
, sysdep_sa_len((struct sockaddr
*)src
)) < 0) {
710 "bind 1 (%s)\n", strerror(errno
));
717 plogdump(ASL_LEVEL_DEBUG
, (void*)buf
, buflen
, "@@@@@@ data being sent:\n");
719 for (i
= 0; i
< cnt
; i
++) {
720 len
= sendto(sendsock
, buf
, buflen
, 0, (struct sockaddr
*)dst
, sysdep_sa_len((struct sockaddr
*)dst
));
723 "sendto (%s)\n", strerror(errno
));
724 if (errno
!= EHOSTUNREACH
&& errno
!= ENETDOWN
&& errno
!= ENETUNREACH
) {
730 "treating socket error (%s) like packet loss\n", strerror(errno
));
731 // else treat these failures like a packet loss
734 plog(ASL_LEVEL_DEBUG
,
735 "%d times of %d bytes message will be sent "
737 i
+ 1, len
, saddr2str((struct sockaddr
*)dst
));
739 //plog(ASL_LEVEL_DEBUG, "sent %d bytes", buflen);
750 setsockopt_bypass(int so
, int family
)
762 level
= IPPROTO_IPV6
;
767 "unsupported address family %d\n", family
);
771 policy
= "in bypass";
772 buf
= ipsec_set_policy(policy
, strlen(policy
));
775 "ipsec_set_policy (%s)\n",
779 if (setsockopt(so
, level
,
780 (level
== IPPROTO_IP
?
781 IP_IPSEC_POLICY
: IPV6_IPSEC_POLICY
),
782 buf
, ipsec_get_policylen(buf
)) < 0) {
784 "setsockopt IP_IPSEC_POLICY (%s)\n",
790 policy
= "out bypass";
791 buf
= ipsec_set_policy(policy
, strlen(policy
));
794 "ipsec_set_policy (%s)\n",
798 if (setsockopt(so
, level
,
799 (level
== IPPROTO_IP
?
800 IP_IPSEC_POLICY
: IPV6_IPSEC_POLICY
),
801 buf
, ipsec_get_policylen(buf
)) < 0) {
803 "setsockopt IP_IPSEC_POLICY (%s)\n",
812 struct sockaddr_storage
*
815 struct sockaddr_storage
*new;
817 if ((new = racoon_calloc(1, sizeof(*new))) == NULL
) {
819 "%s\n", strerror(errno
));
828 struct sockaddr_storage
*
829 dupsaddr(struct sockaddr_storage
*addr
)
831 struct sockaddr_storage
*new;
833 new = racoon_calloc(1, sizeof(*new));
835 plog(ASL_LEVEL_ERR
, "%s\n", strerror(errno
));
839 memcpy(new, addr
, addr
->ss_len
);
845 saddr2str(const struct sockaddr
*saddr
)
847 static char buf
[NI_MAXHOST
+ NI_MAXSERV
+ 10];
848 char addr
[NI_MAXHOST
], port
[NI_MAXSERV
];
855 if (saddr
->sa_family
== AF_UNSPEC
)
856 snprintf (buf
, sizeof(buf
), "%s", "anonymous");
858 GETNAMEINFO(saddr
, addr
, port
);
859 snprintf(buf
, sizeof(buf
), "%s[%s]", addr
, port
);
866 saddr2str_with_prefix(const struct sockaddr
*saddr
, int prefix
)
868 static char buf
[NI_MAXHOST
+ NI_MAXSERV
+ 10];
869 char addr
[NI_MAXHOST
], port
[NI_MAXSERV
];
876 if (saddr
->sa_family
== AF_UNSPEC
)
877 snprintf (buf
, sizeof(buf
), "%s", "anonymous");
879 GETNAMEINFO(saddr
, addr
, port
);
880 snprintf(buf
, sizeof(buf
), "%s/%d[%s]", addr
, prefix
, port
);
888 saddrwop2str(const struct sockaddr
*saddr
)
890 static char buf
[NI_MAXHOST
+ NI_MAXSERV
+ 10];
891 char addr
[NI_MAXHOST
];
898 GETNAMEINFO_NULL(saddr
, addr
);
899 snprintf(buf
, sizeof(buf
), "%s", addr
);
905 naddrwop2str(const struct netaddr
*naddr
)
907 static char buf
[NI_MAXHOST
+ 10];
908 static const struct sockaddr sa_any
; /* this is initialized to all zeros */
915 if (memcmp(&naddr
->sa
, &sa_any
, sizeof(sa_any
)) == 0)
916 snprintf(buf
, sizeof(buf
), "%s", "any");
918 snprintf(buf
, sizeof(buf
), "%s", saddrwop2str((struct sockaddr
*)&naddr
->sa
.sa
));
919 snprintf(&buf
[strlen(buf
)], sizeof(buf
) - strlen(buf
), "/%ld", naddr
->prefix
);
925 naddrwop2str_fromto(const char *format
, const struct netaddr
*saddr
,
926 const struct netaddr
*daddr
)
928 static char buf
[2*(NI_MAXHOST
+ NI_MAXSERV
+ 10) + 100];
931 src
= racoon_strdup(naddrwop2str(saddr
));
932 dst
= racoon_strdup(naddrwop2str(daddr
));
935 /* WARNING: Be careful about the format string! Don't
936 ever pass in something that a user can modify!!! */
937 snprintf (buf
, sizeof(buf
), format
, src
, dst
);
945 saddr2str_fromto(format
, saddr
, daddr
)
947 const struct sockaddr
*saddr
;
948 const struct sockaddr
*daddr
;
950 static char buf
[2*(NI_MAXHOST
+ NI_MAXSERV
+ 10) + 100];
954 src
= racoon_strdup(saddr2str(saddr
));
960 dst
= racoon_strdup(saddr2str(daddr
));
965 /* WARNING: Be careful about the format string! Don't
966 ever pass in something that a user can modify!!! */
967 snprintf (buf
, sizeof(buf
), format
, src
? src
:"[null]", dst
? dst
:"[null]");
978 struct sockaddr_storage
*
979 str2saddr(char *host
, char *port
)
981 struct addrinfo hints
, *res
;
982 struct sockaddr_storage
*saddr
;
985 memset(&hints
, 0, sizeof(hints
));
986 hints
.ai_family
= PF_UNSPEC
;
987 hints
.ai_socktype
= SOCK_DGRAM
;
988 hints
.ai_flags
= AI_NUMERICHOST
;
989 error
= getaddrinfo(host
, port
, &hints
, &res
);
992 "getaddrinfo(%s%s%s): %s\n",
993 host
, port
? "," : "", port
? port
: "",
994 gai_strerror(error
));
997 if (res
->ai_next
!= NULL
) {
998 plog(ASL_LEVEL_WARNING
,
999 "getaddrinfo(%s%s%s): "
1000 "resolved to multiple address, "
1001 "taking the first one\n",
1002 host
, port
? "," : "", port
? port
: "");
1004 saddr
= newsaddr(sizeof(*saddr
));
1005 if (saddr
== NULL
) {
1007 "failed to allocate buffer.\n");
1011 memcpy(saddr
, res
->ai_addr
, res
->ai_addrlen
);
1018 mask_sockaddr(a
, b
, l
)
1019 struct sockaddr_storage
*a
;
1020 const struct sockaddr_storage
*b
;
1026 switch (b
->ss_family
) {
1028 alen
= sizeof(struct in_addr
);
1029 p
= (u_int8_t
*)&((struct sockaddr_in
*)a
)->sin_addr
;
1033 alen
= sizeof(struct in6_addr
);
1034 p
= (u_int8_t
*)&((struct sockaddr_in6
*)a
)->sin6_addr
;
1039 "invalid address family: %d\n", b
->ss_family
);
1043 if ((alen
<< 3) < l
) {
1045 "unexpected inconsistency: %d %zu\n", b
->ss_family
, l
);
1049 memcpy(a
, b
, sysdep_sa_len((struct sockaddr
*)b
));
1050 p
[l
/ 8] &= (0xff00 >> (l
% 8)) & 0xff;
1051 for (i
= l
/ 8 + 1; i
< alen
; i
++)
1055 /* Compute a score describing how "accurate" a netaddr is for a given sockaddr.
1057 * Return values for address 10.20.30.40 [port 500] and given netaddresses...
1058 * 10.10.0.0/16 => -1 ... doesn't match
1059 * 0.0.0.0/0 => 0 ... matches, but only 0 bits.
1060 * 10.20.0.0/16 => 16 ... 16 bits match
1061 * 10.20.30.0/24 => 24 ... guess what ;-)
1062 * 10.20.30.40/32 => 32 ... whole address match
1063 * 10.20.30.40:500 => 33 ... both address and port match
1064 * 10.20.30.40:501 => -1 ... port doesn't match and isn't 0 (=any)
1067 naddr_score(const struct netaddr
*naddr
, const struct sockaddr_storage
*saddr
)
1069 static const struct netaddr naddr_any
; /* initialized to all-zeros */
1070 struct sockaddr_storage sa
;
1071 u_int16_t naddr_port
, saddr_port
;
1074 if (!naddr
|| !saddr
) {
1076 "Call with null args: naddr=%p, saddr=%p\n",
1081 /* Wildcard address matches, but only 0 bits. */
1082 if (memcmp(naddr
, &naddr_any
, sizeof(naddr_any
)) == 0)
1085 /* If families don't match we really can't do much... */
1086 if (naddr
->sa
.sa
.ss_family
!= saddr
->ss_family
)
1089 /* If port check fail don't bother to check addresses. */
1090 naddr_port
= extract_port(&naddr
->sa
.sa
);
1091 saddr_port
= extract_port(saddr
);
1092 if (naddr_port
== 0 || saddr_port
== 0) /* wildcard match */
1094 else if (naddr_port
== saddr_port
) /* exact match */
1096 else /* mismatch :-) */
1099 /* Here it comes - compare network addresses. */
1100 mask_sockaddr(&sa
, saddr
, naddr
->prefix
);
1101 if (loglevel
>= ASL_LEVEL_DEBUG
) { /* debug only */
1103 a1
= racoon_strdup(naddrwop2str(naddr
));
1104 a2
= racoon_strdup(saddrwop2str((struct sockaddr
*)saddr
));
1105 a3
= racoon_strdup(saddrwop2str((struct sockaddr
*)&sa
));
1109 plog(ASL_LEVEL_DEBUG
,
1110 "naddr=%s, saddr=%s (masked=%s)\n",
1116 if (cmpsaddrwop(&sa
, &naddr
->sa
.sa
) == 0)
1117 return naddr
->prefix
+ port_score
;
1122 /* Some useful functions for sockaddr_storage port manipulations. */
1124 extract_port (const struct sockaddr_storage
*addr
)
1126 u_int16_t port
= -1;
1131 switch (addr
->ss_family
) {
1133 port
= ((struct sockaddr_in
*)addr
)->sin_port
;
1136 port
= ((struct sockaddr_in6
*)addr
)->sin6_port
;
1139 plog(ASL_LEVEL_ERR
, "unknown AF: %u\n", addr
->ss_family
);
1147 get_port_ptr (struct sockaddr_storage
*addr
)
1149 u_int16_t
*port_ptr
;
1154 switch (addr
->ss_family
) {
1156 port_ptr
= &(((struct sockaddr_in
*)addr
)->sin_port
);
1159 port_ptr
= &(((struct sockaddr_in6
*)addr
)->sin6_port
);
1162 plog(ASL_LEVEL_ERR
, "unknown AF: %u\n", addr
->ss_family
);
1171 set_port (struct sockaddr_storage
*addr
, u_int16_t new_port
)
1173 u_int16_t
*port_ptr
;
1175 port_ptr
= get_port_ptr (addr
);
1178 *port_ptr
= htons(new_port
);