]>
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
*addr1
;
87 const struct sockaddr
*addr2
;
91 if (addr1
== 0 && addr2
== 0)
93 if (addr1
== 0 || addr2
== 0)
96 if (addr1
->sa_len
!= addr2
->sa_len
97 || addr1
->sa_family
!= addr2
->sa_family
)
99 switch (addr1
->sa_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 with port, taking care wildcard.
126 * addr1 is a subject address, addr2 is in a database entry.
131 cmpsaddrwild(addr1
, addr2
)
132 const struct sockaddr
*addr1
;
133 const struct sockaddr
*addr2
;
136 u_short port1
, port2
;
138 if (addr1
== 0 && addr2
== 0)
140 if (addr1
== 0 || addr2
== 0)
143 if (addr1
->sa_len
!= addr2
->sa_len
144 || addr1
->sa_family
!= addr2
->sa_family
)
147 switch (addr1
->sa_family
) {
149 sa1
= (caddr_t
)&((struct sockaddr_in
*)addr1
)->sin_addr
;
150 sa2
= (caddr_t
)&((struct sockaddr_in
*)addr2
)->sin_addr
;
151 port1
= ((struct sockaddr_in
*)addr1
)->sin_port
;
152 port2
= ((struct sockaddr_in
*)addr2
)->sin_port
;
153 if (!(port1
== IPSEC_PORT_ANY
||
154 port2
== IPSEC_PORT_ANY
||
157 if (memcmp(sa1
, sa2
, sizeof(struct in_addr
)) != 0)
162 sa1
= (caddr_t
)&((struct sockaddr_in6
*)addr1
)->sin6_addr
;
163 sa2
= (caddr_t
)&((struct sockaddr_in6
*)addr2
)->sin6_addr
;
164 port1
= ((struct sockaddr_in6
*)addr1
)->sin6_port
;
165 port2
= ((struct sockaddr_in6
*)addr2
)->sin6_port
;
166 if (!(port1
== IPSEC_PORT_ANY
||
167 port2
== IPSEC_PORT_ANY
||
170 if (memcmp(sa1
, sa2
, sizeof(struct in6_addr
)) != 0)
172 if (((struct sockaddr_in6
*)addr1
)->sin6_scope_id
!=
173 ((struct sockaddr_in6
*)addr2
)->sin6_scope_id
)
185 * compare two sockaddr with strict match on port.
190 cmpsaddrstrict(addr1
, addr2
)
191 const struct sockaddr
*addr1
;
192 const struct sockaddr
*addr2
;
195 u_short port1
, port2
;
197 if (addr1
== 0 && addr2
== 0)
199 if (addr1
== 0 || addr2
== 0)
202 if (addr1
->sa_len
!= addr2
->sa_len
203 || addr1
->sa_family
!= addr2
->sa_family
)
206 switch (addr1
->sa_family
) {
208 sa1
= (caddr_t
)&((struct sockaddr_in
*)addr1
)->sin_addr
;
209 sa2
= (caddr_t
)&((struct sockaddr_in
*)addr2
)->sin_addr
;
210 port1
= ((struct sockaddr_in
*)addr1
)->sin_port
;
211 port2
= ((struct sockaddr_in
*)addr2
)->sin_port
;
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
;
225 if (memcmp(sa1
, sa2
, sizeof(struct in6_addr
)) != 0)
227 if (((struct sockaddr_in6
*)addr1
)->sin6_scope_id
!=
228 ((struct sockaddr_in6
*)addr2
)->sin6_scope_id
)
239 /* get local address against the destination. */
242 struct sockaddr
*remote
;
244 struct sockaddr
*local
;
245 u_int local_len
= sizeof(struct sockaddr_storage
);
246 int s
; /* for dummy connection */
248 /* allocate buffer */
249 if ((local
= racoon_calloc(1, local_len
)) == NULL
) {
250 plog(LLV_ERROR
, LOCATION
, NULL
,
251 "failed to get address buffer.\n");
255 /* get real interface received packet */
256 if ((s
= socket(remote
->sa_family
, SOCK_DGRAM
, 0)) < 0) {
257 plog(LLV_ERROR
, LOCATION
, NULL
,
258 "socket (%s)\n", strerror(errno
));
262 setsockopt_bypass(s
, remote
->sa_family
);
264 if (connect(s
, remote
, sysdep_sa_len(remote
)) < 0) {
265 plog(LLV_ERROR
, LOCATION
, NULL
,
266 "connect (%s)\n", strerror(errno
));
271 if (getsockname(s
, local
, &local_len
) < 0) {
272 plog(LLV_ERROR
, LOCATION
, NULL
,
273 "getsockname (%s)\n", strerror(errno
));
288 * Receive packet, with src/dst information. It is assumed that necessary
289 * setsockopt() have already performed on socket.
292 recvfromto(s
, buf
, buflen
, flags
, from
, fromlen
, to
, tolen
)
297 struct sockaddr
*from
;
304 struct sockaddr_storage ss
;
306 struct cmsghdr
*cm
, *cm_prev
;
309 #if defined(INET6) && defined(INET6_ADVAPI)
310 struct in6_pktinfo
*pi
;
311 #endif /*INET6_ADVAPI*/
312 struct sockaddr_in
*sin
;
314 struct sockaddr_in6
*sin6
;
318 if (getsockname(s
, (struct sockaddr
*)&ss
, &len
) < 0) {
319 plog(LLV_ERROR
, LOCATION
, NULL
,
320 "getsockname (%s)\n", strerror(errno
));
324 m
.msg_name
= (caddr_t
)from
;
325 m
.msg_namelen
= *fromlen
;
326 iov
[0].iov_base
= (caddr_t
)buf
;
327 iov
[0].iov_len
= buflen
;
330 memset(cmsgbuf
, 0, sizeof(cmsgbuf
));
331 cm
= (struct cmsghdr
*)cmsgbuf
;
332 m
.msg_control
= (caddr_t
)cm
;
333 m
.msg_controllen
= sizeof(cmsgbuf
);
334 if ((len
= recvmsg(s
, &m
, flags
)) < 0) {
335 plog(LLV_ERROR
, LOCATION
, NULL
,
336 "recvmsg (%s)\n", strerror(errno
));
338 } else if (len
== 0) {
341 *fromlen
= m
.msg_namelen
;
345 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(&m
), cm_prev
= NULL
;
346 m
.msg_controllen
!= 0 && cm
&& cm
!= cm_prev
;
347 cm_prev
= cm
, cm
= (struct cmsghdr
*)CMSG_NXTHDR(&m
, cm
)) {
349 plog(LLV_ERROR
, LOCATION
, NULL
,
350 "cmsg %d %d\n", cm
->cmsg_level
, cm
->cmsg_type
);)
352 #if defined(INET6) && defined(INET6_ADVAPI)
353 if (ss
.ss_family
== AF_INET6
354 && cm
->cmsg_level
== IPPROTO_IPV6
355 && cm
->cmsg_type
== IPV6_PKTINFO
356 && otolen
>= sizeof(*sin6
)) {
357 pi
= (struct in6_pktinfo
*)(CMSG_DATA(cm
));
358 *tolen
= sizeof(*sin6
);
359 sin6
= (struct sockaddr_in6
*)to
;
360 memset(sin6
, 0, sizeof(*sin6
));
361 sin6
->sin6_family
= AF_INET6
;
362 sin6
->sin6_len
= sizeof(*sin6
);
363 memcpy(&sin6
->sin6_addr
, &pi
->ipi6_addr
,
364 sizeof(sin6
->sin6_addr
));
365 /* XXX other cases, such as site-local? */
366 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
))
367 sin6
->sin6_scope_id
= pi
->ipi6_ifindex
;
369 sin6
->sin6_scope_id
= 0;
371 ((struct sockaddr_in6
*)&ss
)->sin6_port
;
372 otolen
= -1; /* "to" already set */
376 #if defined(INET6) && defined(IPV6_RECVDSTADDR)
377 if (ss
.ss_family
== AF_INET6
378 && cm
->cmsg_level
== IPPROTO_IPV6
379 && cm
->cmsg_type
== IPV6_RECVDSTADDR
380 && otolen
>= sizeof(*sin6
)) {
381 *tolen
= sizeof(*sin6
);
382 sin6
= (struct sockaddr_in6
*)to
;
383 memset(sin6
, 0, sizeof(*sin6
));
384 sin6
->sin6_family
= AF_INET6
;
385 sin6
->sin6_len
= sizeof(*sin6
);
386 memcpy(&sin6
->sin6_addr
, CMSG_DATA(cm
),
387 sizeof(sin6
->sin6_addr
));
389 ((struct sockaddr_in6
*)&ss
)->sin6_port
;
390 otolen
= -1; /* "to" already set */
394 if (ss
.ss_family
== AF_INET
395 && cm
->cmsg_level
== IPPROTO_IP
396 && cm
->cmsg_type
== IP_RECVDSTADDR
397 && otolen
>= sizeof(*sin
)) {
398 *tolen
= sizeof(*sin
);
399 sin
= (struct sockaddr_in
*)to
;
400 memset(sin
, 0, sizeof(*sin
));
401 sin
->sin_family
= AF_INET
;
402 sin
->sin_len
= sizeof(*sin
);
403 memcpy(&sin
->sin_addr
, CMSG_DATA(cm
),
404 sizeof(sin
->sin_addr
));
405 sin
->sin_port
= ((struct sockaddr_in
*)&ss
)->sin_port
;
406 otolen
= -1; /* "to" already set */
414 /* send packet, with fixing src/dst address pair. */
416 sendfromto(s
, buf
, buflen
, src
, dst
, cnt
)
420 struct sockaddr
*src
;
421 struct sockaddr
*dst
;
423 struct sockaddr_storage ss
;
427 if (src
->sa_family
!= dst
->sa_family
) {
428 plog(LLV_ERROR
, LOCATION
, NULL
,
429 "address family mismatch\n");
434 if (getsockname(s
, (struct sockaddr
*)&ss
, &len
) < 0) {
435 plog(LLV_ERROR
, LOCATION
, NULL
,
436 "getsockname (%s)\n", strerror(errno
));
440 plog(LLV_DEBUG
, LOCATION
, NULL
,
441 "sockname %s\n", saddr2str((struct sockaddr
*)&ss
));
442 plog(LLV_DEBUG
, LOCATION
, NULL
,
443 "send packet from %s\n", saddr2str(src
));
444 plog(LLV_DEBUG
, LOCATION
, NULL
,
445 "send packet to %s\n", saddr2str(dst
));
447 if (src
->sa_family
!= ss
.ss_family
) {
448 plog(LLV_ERROR
, LOCATION
, NULL
,
449 "address family mismatch\n");
453 switch (src
->sa_family
) {
454 #if defined(INET6) && defined(INET6_ADVAPI)
455 // XXX: This block wasn't compiled on Linux - does it work?
462 struct in6_pktinfo
*pi
;
464 struct sockaddr_in6 src6
, dst6
;
466 memcpy(&src6
, src
, sizeof(src6
));
467 memcpy(&dst6
, dst
, sizeof(dst6
));
469 /* XXX take care of other cases, such as site-local */
471 if (IN6_IS_ADDR_LINKLOCAL(&src6
.sin6_addr
)
472 || IN6_IS_ADDR_MULTICAST(&src6
.sin6_addr
)) {
473 ifindex
= src6
.sin6_scope_id
; /*???*/
476 /* XXX some sanity check on dst6.sin6_scope_id */
478 /* flowinfo for IKE? mmm, maybe useful but for now make it 0 */
479 src6
.sin6_flowinfo
= dst6
.sin6_flowinfo
= 0;
481 memset(&m
, 0, sizeof(m
));
482 m
.msg_name
= (caddr_t
)&dst6
;
483 m
.msg_namelen
= sizeof(dst6
);
484 iov
[0].iov_base
= (char *)buf
;
485 iov
[0].iov_len
= buflen
;
489 memset(cmsgbuf
, 0, sizeof(cmsgbuf
));
490 cm
= (struct cmsghdr
*)cmsgbuf
;
491 m
.msg_control
= (caddr_t
)cm
;
492 m
.msg_controllen
= CMSG_SPACE(sizeof(struct in6_pktinfo
));
494 cm
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
495 cm
->cmsg_level
= IPPROTO_IPV6
;
496 cm
->cmsg_type
= IPV6_PKTINFO
;
497 pi
= (struct in6_pktinfo
*)CMSG_DATA(cm
);
498 memcpy(&pi
->ipi6_addr
, &src6
.sin6_addr
, sizeof(src6
.sin6_addr
));
499 pi
->ipi6_ifindex
= ifindex
;
501 plog(LLV_DEBUG
, LOCATION
, NULL
,
503 saddr2str((struct sockaddr
*)&src6
),
505 plog(LLV_DEBUG
, LOCATION
, NULL
,
507 saddr2str((struct sockaddr
*)&dst6
),
510 for (i
= 0; i
< cnt
; i
++) {
511 len
= sendmsg(s
, &m
, 0 /*MSG_DONTROUTE*/);
513 plog(LLV_ERROR
, LOCATION
, NULL
,
514 "sendmsg (%s)\n", strerror(errno
));
515 if (errno
!= EHOSTUNREACH
&& errno
!= ENETDOWN
&& errno
!= ENETUNREACH
) {
518 // <rdar://problem/6609744> treat these failures like
519 // packet loss, in case the network interface is flaky
522 plog(LLV_DEBUG
, LOCATION
, NULL
,
523 "%d times of %d bytes message will be sent "
525 i
+ 1, len
, saddr2str(dst
));
527 plogdump(LLV_DEBUG
, (char *)buf
, buflen
);
537 if (ss
.ss_family
== src
->sa_family
&& memcmp(&ss
, src
, sysdep_sa_len(src
)) == 0) {
543 * Use newly opened socket for sending packets.
544 * NOTE: this is unsafe, because if the peer is quick enough
545 * the packet from the peer may be queued into sendsock.
546 * Better approach is to prepare bind'ed udp sockets for
547 * each of the interface addresses.
549 sendsock
= socket(src
->sa_family
, SOCK_DGRAM
, 0);
551 plog(LLV_ERROR
, LOCATION
, NULL
,
552 "socket (%s)\n", strerror(errno
));
555 if (setsockopt(sendsock
, SOL_SOCKET
,
557 (void *)&yes
, sizeof(yes
)) < 0) {
558 plog(LLV_ERROR
, LOCATION
, NULL
,
559 "setsockopt SO_REUSEPORT (%s)\n",
564 #ifdef IPV6_USE_MIN_MTU
565 if (src
->sa_family
== AF_INET6
&&
566 setsockopt(sendsock
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
567 (void *)&yes
, sizeof(yes
)) < 0) {
568 plog(LLV_ERROR
, LOCATION
, NULL
,
569 "setsockopt IPV6_USE_MIN_MTU (%s)\n",
575 if (setsockopt_bypass(sendsock
, src
->sa_family
) < 0) {
580 if (bind(sendsock
, (struct sockaddr
*)src
, sysdep_sa_len(src
)) < 0) {
581 plog(LLV_ERROR
, LOCATION
, NULL
,
582 "bind 1 (%s)\n", strerror(errno
));
589 for (i
= 0; i
< cnt
; i
++) {
590 len
= sendto(sendsock
, buf
, buflen
, 0, dst
, sysdep_sa_len(dst
));
592 plog(LLV_ERROR
, LOCATION
, NULL
,
593 "sendto (%s)\n", strerror(errno
));
594 if (errno
!= EHOSTUNREACH
&& errno
!= ENETDOWN
&& errno
!= ENETUNREACH
) {
599 plog(LLV_ERROR
, LOCATION
, NULL
,
600 "treating socket error (%s) like packet loss\n", strerror(errno
));
601 // else treat these failures like a packet loss
604 plog(LLV_DEBUG
, LOCATION
, NULL
,
605 "%d times of %d bytes message will be sent "
607 i
+ 1, len
, saddr2str(dst
));
609 plogdump(LLV_DEBUG
, (char *)buf
, buflen
);
620 setsockopt_bypass(so
, family
)
633 level
= IPPROTO_IPV6
;
637 plog(LLV_ERROR
, LOCATION
, NULL
,
638 "unsupported address family %d\n", family
);
642 policy
= "in bypass";
643 buf
= ipsec_set_policy(policy
, strlen(policy
));
645 plog(LLV_ERROR
, LOCATION
, NULL
,
646 "ipsec_set_policy (%s)\n",
650 if (setsockopt(so
, level
,
651 (level
== IPPROTO_IP
?
652 IP_IPSEC_POLICY
: IPV6_IPSEC_POLICY
),
653 buf
, ipsec_get_policylen(buf
)) < 0) {
654 plog(LLV_ERROR
, LOCATION
, NULL
,
655 "setsockopt IP_IPSEC_POLICY (%s)\n",
661 policy
= "out bypass";
662 buf
= ipsec_set_policy(policy
, strlen(policy
));
664 plog(LLV_ERROR
, LOCATION
, NULL
,
665 "ipsec_set_policy (%s)\n",
669 if (setsockopt(so
, level
,
670 (level
== IPPROTO_IP
?
671 IP_IPSEC_POLICY
: IPV6_IPSEC_POLICY
),
672 buf
, ipsec_get_policylen(buf
)) < 0) {
673 plog(LLV_ERROR
, LOCATION
, NULL
,
674 "setsockopt IP_IPSEC_POLICY (%s)\n",
687 struct sockaddr
*new;
689 if ((new = racoon_calloc(1, len
)) == NULL
) {
690 plog(LLV_ERROR
, LOCATION
, NULL
,
691 "%s\n", strerror(errno
));
702 struct sockaddr
*src
;
704 struct sockaddr
*dst
;
706 dst
= racoon_calloc(1, sysdep_sa_len(src
));
708 plog(LLV_ERROR
, LOCATION
, NULL
,
709 "%s\n", strerror(errno
));
713 memcpy(dst
, src
, sysdep_sa_len(src
));
720 const struct sockaddr
*saddr
;
722 static char buf
[NI_MAXHOST
+ NI_MAXSERV
+ 10];
723 char addr
[NI_MAXHOST
], port
[NI_MAXSERV
];
730 if (saddr
->sa_family
== AF_UNSPEC
)
731 snprintf (buf
, sizeof(buf
), "%s", "anonymous");
733 GETNAMEINFO(saddr
, addr
, port
);
734 snprintf(buf
, sizeof(buf
), "%s[%s]", addr
, port
);
742 const struct sockaddr
*saddr
;
744 static char buf
[NI_MAXHOST
+ NI_MAXSERV
+ 10];
745 char addr
[NI_MAXHOST
];
752 GETNAMEINFO_NULL(saddr
, addr
);
753 snprintf(buf
, sizeof(buf
), "%s", addr
);
759 naddrwop2str(const struct netaddr
*naddr
)
761 static char buf
[NI_MAXHOST
+ 10];
762 static const struct sockaddr sa_any
; /* this is initialized to all zeros */
769 if (memcmp(&naddr
->sa
, &sa_any
, sizeof(sa_any
)) == 0)
770 snprintf(buf
, sizeof(buf
), "%s", "any");
772 snprintf(buf
, sizeof(buf
), "%s", saddrwop2str(&naddr
->sa
.sa
));
773 snprintf(&buf
[strlen(buf
)], sizeof(buf
) - strlen(buf
), "/%ld", naddr
->prefix
);
779 naddrwop2str_fromto(const char *format
, const struct netaddr
*saddr
,
780 const struct netaddr
*daddr
)
782 static char buf
[2*(NI_MAXHOST
+ NI_MAXSERV
+ 10) + 100];
785 src
= racoon_strdup(naddrwop2str(saddr
));
786 dst
= racoon_strdup(naddrwop2str(daddr
));
789 /* WARNING: Be careful about the format string! Don't
790 ever pass in something that a user can modify!!! */
791 snprintf (buf
, sizeof(buf
), format
, src
, dst
);
799 saddr2str_fromto(format
, saddr
, daddr
)
801 const struct sockaddr
*saddr
;
802 const struct sockaddr
*daddr
;
804 static char buf
[2*(NI_MAXHOST
+ NI_MAXSERV
+ 10) + 100];
808 src
= racoon_strdup(saddr2str(saddr
));
814 dst
= racoon_strdup(saddr2str(daddr
));
819 /* WARNING: Be careful about the format string! Don't
820 ever pass in something that a user can modify!!! */
821 snprintf (buf
, sizeof(buf
), format
, src
? src
:"[null]", dst
? dst
:"[null]");
833 str2saddr(host
, port
)
837 struct addrinfo hints
, *res
;
838 struct sockaddr
*saddr
;
841 memset(&hints
, 0, sizeof(hints
));
842 hints
.ai_family
= PF_UNSPEC
;
843 hints
.ai_socktype
= SOCK_DGRAM
;
844 hints
.ai_flags
= AI_NUMERICHOST
;
845 error
= getaddrinfo(host
, port
, &hints
, &res
);
847 plog(LLV_ERROR
, LOCATION
, NULL
,
848 "getaddrinfo(%s%s%s): %s\n",
849 host
, port
? "," : "", port
? port
: "",
850 gai_strerror(error
));
853 if (res
->ai_next
!= NULL
) {
854 plog(LLV_WARNING
, LOCATION
, NULL
,
855 "getaddrinfo(%s%s%s): "
856 "resolved to multiple address, "
857 "taking the first one\n",
858 host
, port
? "," : "", port
? port
: "");
860 saddr
= racoon_malloc(res
->ai_addrlen
);
862 plog(LLV_ERROR
, LOCATION
, NULL
,
863 "failed to allocate buffer.\n");
867 memcpy(saddr
, res
->ai_addr
, res
->ai_addrlen
);
874 mask_sockaddr(a
, b
, l
)
876 const struct sockaddr
*b
;
882 switch (b
->sa_family
) {
884 alen
= sizeof(struct in_addr
);
885 p
= (u_int8_t
*)&((struct sockaddr_in
*)a
)->sin_addr
;
889 alen
= sizeof(struct in6_addr
);
890 p
= (u_int8_t
*)&((struct sockaddr_in6
*)a
)->sin6_addr
;
894 plog(LLV_ERROR2
, LOCATION
, NULL
,
895 "invalid address family: %d\n", b
->sa_family
);
899 if ((alen
<< 3) < l
) {
900 plog(LLV_ERROR2
, LOCATION
, NULL
,
901 "unexpected inconsistency: %d %zu\n", b
->sa_family
, l
);
905 memcpy(a
, b
, sysdep_sa_len(b
));
906 p
[l
/ 8] &= (0xff00 >> (l
% 8)) & 0xff;
907 for (i
= l
/ 8 + 1; i
< alen
; i
++)
911 /* Compute a score describing how "accurate" a netaddr is for a given sockaddr.
913 * Return values for address 10.20.30.40 [port 500] and given netaddresses...
914 * 10.10.0.0/16 => -1 ... doesn't match
915 * 0.0.0.0/0 => 0 ... matches, but only 0 bits.
916 * 10.20.0.0/16 => 16 ... 16 bits match
917 * 10.20.30.0/24 => 24 ... guess what ;-)
918 * 10.20.30.40/32 => 32 ... whole address match
919 * 10.20.30.40:500 => 33 ... both address and port match
920 * 10.20.30.40:501 => -1 ... port doesn't match and isn't 0 (=any)
923 naddr_score(const struct netaddr
*naddr
, const struct sockaddr
*saddr
)
925 static const struct netaddr naddr_any
; /* initialized to all-zeros */
927 u_int16_t naddr_port
, saddr_port
;
930 if (!naddr
|| !saddr
) {
931 plog(LLV_ERROR
, LOCATION
, NULL
,
932 "Call with null args: naddr=%p, saddr=%p\n",
937 /* Wildcard address matches, but only 0 bits. */
938 if (memcmp(naddr
, &naddr_any
, sizeof(naddr_any
)) == 0)
941 /* If families don't match we really can't do much... */
942 if (naddr
->sa
.sa
.sa_family
!= saddr
->sa_family
)
945 /* If port check fail don't bother to check addresses. */
946 naddr_port
= extract_port(&naddr
->sa
.sa
);
947 saddr_port
= extract_port(saddr
);
948 if (naddr_port
== 0 || saddr_port
== 0) /* wildcard match */
950 else if (naddr_port
== saddr_port
) /* exact match */
952 else /* mismatch :-) */
955 /* Here it comes - compare network addresses. */
956 mask_sockaddr(&sa
, saddr
, naddr
->prefix
);
957 if (loglevel
>= LLV_DEBUG
) { /* debug only */
959 a1
= racoon_strdup(naddrwop2str(naddr
));
960 a2
= racoon_strdup(saddrwop2str(saddr
));
961 a3
= racoon_strdup(saddrwop2str(&sa
));
965 plog(LLV_DEBUG
, LOCATION
, NULL
,
966 "naddr=%s, saddr=%s (masked=%s)\n",
972 if (cmpsaddrwop(&sa
, &naddr
->sa
.sa
) == 0)
973 return naddr
->prefix
+ port_score
;
978 /* Some usefull functions for sockaddr port manipulations. */
980 extract_port (const struct sockaddr
*addr
)
987 switch (addr
->sa_family
) {
989 port
= ((struct sockaddr_in
*)addr
)->sin_port
;
992 port
= ((struct sockaddr_in6
*)addr
)->sin6_port
;
995 plog(LLV_ERROR
, LOCATION
, NULL
, "unknown AF: %u\n", addr
->sa_family
);
1003 get_port_ptr (struct sockaddr
*addr
)
1005 u_int16_t
*port_ptr
;
1010 switch (addr
->sa_family
) {
1012 port_ptr
= &(((struct sockaddr_in
*)addr
)->sin_port
);
1015 port_ptr
= &(((struct sockaddr_in6
*)addr
)->sin6_port
);
1018 plog(LLV_ERROR
, LOCATION
, NULL
, "unknown AF: %u\n", addr
->sa_family
);
1027 set_port (struct sockaddr
*addr
, u_int16_t new_port
)
1029 u_int16_t
*port_ptr
;
1031 port_ptr
= get_port_ptr (addr
);
1034 *port_ptr
= htons(new_port
);