1 /*-
2  * Copyright (c) 2010 Bjoern A. Zeeb <bz@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #else
32 #include "default_config.h"
33 #endif
34 
35 #include <string.h>
36 #ifndef WIN32
37 # include <sys/types.h>
38 # include <sys/socket.h>
39 #include <netinet/in.h>
40 #else
41 # include <winsock2.h>
42 # include <ws2tcpip.h> /*sockaddr, addrinfo etc.*/
43 # include <stdint.h>
44 #endif /*WIN32*/
45 
46 #include "pcp.h"
47 #include "findsaddr.h"
48 #include "unp.h"
49 
50 /*
51  * Return the source address for the given destination address.
52  *
53  * This makes use of proper source address selection in the FreeBSD kernel
54  * even taking jails into account (sys/netinet/in_pcb.c:in_pcbladdr()).
55  * We open a UDP socket, and connect to the destination, letting the kernel
56  * do the bind and then read the source IPv4 address using getsockname(2).
57  * This has multiple advantages: no need to do PF_ROUTE operations possibly
58  * needing special privileges, jails properly taken into account and most
59  * important - getting the result the kernel would give us rather than
60  * best-guessing ourselves.
61  */
62 
63 #ifndef SOCKET
64 #define SOCKET int
65 #endif
66 
67 #ifndef INVALID_SOCKET
68 #define INVALID_SOCKET -1
69 #endif
70 
findsaddr(register const struct sockaddr_in * to,struct in6_addr * from)71 const char *findsaddr(register const struct sockaddr_in *to,
72         struct in6_addr *from)
73 {
74     const char *errstr;
75     struct sockaddr_in cto, cfrom;
76     SOCKET s;
77     socklen_t len;
78 
79     s=socket(AF_INET, SOCK_DGRAM, 0);
80     if (s == INVALID_SOCKET)
81         return ("failed to open DGRAM socket for src addr selection.");
82     errstr=NULL;
83     len=sizeof(struct sockaddr_in);
84     memcpy(&cto, to, len);
85     cto.sin_port=htons(65535); /* Dummy port for connect(2). */
86     if (connect(s, (struct sockaddr *)&cto, len) == -1) {
87         errstr="failed to connect to peer for src addr selection.";
88         goto err;
89     }
90 
91     if (getsockname(s, (struct sockaddr *)&cfrom, &len) == -1) {
92         errstr="failed to get socket name for src addr selection.";
93         goto err;
94     }
95 
96     if (len != sizeof(struct sockaddr_in) || cfrom.sin_family != AF_INET) {
97         errstr="unexpected address family in src addr selection.";
98         goto err;
99     }
100 
101     ((uint32_t *)from)[0]=0;
102     ((uint32_t *)from)[1]=0;
103     ((uint32_t *)from)[2]=htonl(0xffff);
104     ((uint32_t *)from)[3]=cfrom.sin_addr.s_addr;
105 
106 err:
107     (void)CLOSE(s);
108 
109     /* No error (string) to return. */
110     return (errstr);
111 }
112 
findsaddr6(register const struct sockaddr_in6 * to,register struct in6_addr * from)113 const char *findsaddr6(register const struct sockaddr_in6 *to,
114         register struct in6_addr *from)
115 {
116     const char *errstr;
117     struct sockaddr_in6 cto, cfrom;
118     SOCKET s;
119     socklen_t len;
120     uint32_t sock_flg=0;
121 
122     if (IN6_IS_ADDR_LOOPBACK(&to->sin6_addr)) {
123         memcpy(from, &to->sin6_addr, sizeof(struct in6_addr));
124         return NULL;
125     }
126 
127     s=socket(AF_INET6, SOCK_DGRAM, 0);
128     if (s == INVALID_SOCKET)
129         return ("failed to open DGRAM socket for src addr selection.");
130 
131     errstr=NULL;
132 
133     //Enable Dual-stack socket for Vista and higher
134     if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&sock_flg,
135             sizeof(sock_flg)) == -1) {
136         errstr="setsockopt failed to set dual stack mode.";
137         goto err;
138     }
139 
140     len=sizeof(struct sockaddr_in6);
141     memcpy(&cto, to, len);
142     cto.sin6_port=htons(65535); /* Dummy port for connect(2). */
143     if (connect(s, (struct sockaddr *)&cto, len) == -1) {
144         errstr="failed to connect to peer for src addr selection.";
145         goto err;
146     }
147 
148     if (getsockname(s, (struct sockaddr *)&cfrom, &len) == -1) {
149         errstr="failed to get socket name for src addr selection.";
150         goto err;
151     }
152 
153     if (len != sizeof(struct sockaddr_in6) || cfrom.sin6_family != AF_INET6) {
154         errstr="unexpected address family in src addr selection.";
155         goto err;
156     }
157 
158     memcpy(from->s6_addr, cfrom.sin6_addr.s6_addr, sizeof(struct in6_addr));
159 
160 err:
161     (void) CLOSE(s);
162 
163     /* No error (string) to return. */
164     return (errstr);
165 }
166 
167 /* end */
168