util.c revision 1.1 1 /* $NetBSD: util.c,v 1.1 2000/06/02 23:15:42 fvdl Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/queue.h>
42 #include <net/if.h>
43 #include <netinet/in.h>
44 #include <ifaddrs.h>
45 #include <sys/poll.h>
46 #include <rpc/rpc.h>
47 #include <errno.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <netdb.h>
51 #include <netconfig.h>
52 #include <stdio.h>
53 #include <arpa/inet.h>
54
55 #include "rpcbind.h"
56
57 static struct sockaddr_in *local_in4;
58 #ifdef INET6
59 static struct sockaddr_in6 *local_in6;
60 #endif
61
62 static int bitmaskcmp __P((void *, void *, void *, int));
63 #ifdef INET6
64 static void in6_fillscopeid __P((struct sockaddr_in6 *));
65 #endif
66
67 /*
68 * For all bits set in "mask", compare the corresponding bits in
69 * "dst" and "src", and see if they match.
70 */
71 static int
72 bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
73 {
74 int i, j;
75 u_int8_t *p1 = dst, *p2 = src, *netmask = mask;
76 u_int8_t bitmask;
77
78 for (i = 0; i < bytelen; i++) {
79 for (j = 0; j < 8; j++) {
80 bitmask = 1 << j;
81 if (!(netmask[i] & bitmask))
82 continue;
83 if ((p1[i] & bitmask) != (p2[i] & bitmask))
84 return 1;
85 }
86 }
87
88 return 0;
89 }
90
91 /*
92 * Taken from ifconfig.c
93 */
94 #ifdef INET6
95 static void
96 in6_fillscopeid(struct sockaddr_in6 *sin6)
97 {
98 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
99 sin6->sin6_scope_id =
100 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
101 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
102 }
103 }
104 #endif
105
106 char *
107 addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
108 char *netid)
109 {
110 struct ifaddrs *ifap, *ifp;
111 #ifdef INET6
112 struct sockaddr_in6 *servsin6, *sin6mask, *clntsin6, *ifsin6, *realsin6;
113 struct sockaddr_in6 *newsin6;
114 #endif
115 struct sockaddr_in *servsin, *sinmask, *clntsin, *newsin, *ifsin;
116 struct netbuf *serv_nbp, *clnt_nbp = NULL, tbuf;
117 struct sockaddr *serv_sa;
118 struct sockaddr *clnt_sa;
119 struct sockaddr_storage ss;
120 struct netconfig *nconf;
121 struct sockaddr *clnt = caller->buf;
122 char *ret = NULL;
123
124 #ifdef ND_DEBUG
125 if (debugging)
126 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
127 clnt_uaddr, netid);
128 #endif
129 nconf = getnetconfigent(netid);
130 if (nconf == NULL)
131 return NULL;
132
133 serv_nbp = uaddr2taddr(nconf, serv_uaddr);
134 if (serv_nbp == NULL)
135 return NULL;
136
137 serv_sa = (struct sockaddr *)serv_nbp->buf;
138 if (clnt_uaddr != NULL) {
139 clnt_nbp = uaddr2taddr(nconf, clnt_uaddr);
140 clnt_sa = (struct sockaddr *)clnt_nbp->buf;
141 } else {
142 clnt_sa = (struct sockaddr *)
143 malloc(sizeof (struct sockaddr_storage));
144 memcpy(clnt_sa, clnt, clnt->sa_len);
145 }
146
147 if (getifaddrs(&ifp) < 0)
148 return 0;
149
150 /*
151 * Loop through all interfaces. For each interface, see if the
152 * network portion of its address is equal to that of the client.
153 * If so, we have found the interface that we want to use.
154 */
155 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
156 if (ifap->ifa_addr->sa_family != clnt->sa_family ||
157 !(ifap->ifa_flags & IFF_UP))
158 continue;
159
160 switch (clnt->sa_family) {
161 case AF_INET:
162 /*
163 * realsin: address that recvfrom gave us.
164 * ifsin: address of interface being examined.
165 * clntsin: address that client want us to contact
166 * it on
167 * servsin: local address of RPC service.
168 * sinmask: netmask of this interface
169 * newsin: initially a copy of clntsin, eventually
170 * the merged address
171 */
172 servsin = (struct sockaddr_in *)serv_sa;
173 clntsin = (struct sockaddr_in *)clnt_sa;
174 sinmask = (struct sockaddr_in *)ifap->ifa_netmask;
175 newsin = (struct sockaddr_in *)&ss;
176 ifsin = (struct sockaddr_in *)ifap->ifa_addr;
177 if (!bitmaskcmp(&ifsin->sin_addr, &clntsin->sin_addr,
178 &sinmask->sin_addr, sizeof (struct in_addr))) {
179 /*
180 * Found it.
181 */
182 memcpy(newsin, ifap->ifa_addr,
183 clnt_sa->sa_len);
184 newsin->sin_port = servsin->sin_port;
185 tbuf.len = clnt_sa->sa_len;
186 tbuf.maxlen = sizeof (struct sockaddr_storage);
187 tbuf.buf = newsin;
188 goto found;
189 }
190 break;
191 #ifdef INET6
192 case AF_INET6:
193 /*
194 * realsin6: address that recvfrom gave us.
195 * ifsin6: address of interface being examined.
196 * clntsin6: address that client want us to contact
197 * it on
198 * servsin6: local address of RPC service.
199 * sin6mask: netmask of this interface
200 * newsin6: initially a copy of clntsin, eventually
201 * the merged address
202 *
203 * For v6 link local addresses, if the client contacted
204 * us via a link-local address, and wants us to reply
205 * to one, use the scope id to see which one.
206 */
207 realsin6 = (struct sockaddr_in6 *)clnt;
208 ifsin6 = (struct sockaddr_in6 *)ifap->ifa_addr;
209 in6_fillscopeid(ifsin6);
210 clntsin6 = (struct sockaddr_in6 *)clnt_sa;
211 servsin6 = (struct sockaddr_in6 *)serv_sa;
212 sin6mask = (struct sockaddr_in6 *)ifap->ifa_netmask;
213 newsin6 = (struct sockaddr_in6 *)&ss;
214 if (IN6_IS_ADDR_LINKLOCAL(&ifsin6->sin6_addr) &&
215 IN6_IS_ADDR_LINKLOCAL(&realsin6->sin6_addr) &&
216 IN6_IS_ADDR_LINKLOCAL(&clntsin6->sin6_addr)) {
217 if (ifsin6->sin6_scope_id !=
218 realsin6->sin6_scope_id)
219 continue;
220 match:
221 memcpy(newsin6, ifsin6, clnt_sa->sa_len);
222 newsin6->sin6_port = servsin6->sin6_port;
223 tbuf.maxlen = sizeof (struct sockaddr_storage);
224 tbuf.len = clnt_sa->sa_len;
225 tbuf.buf = newsin6;
226 goto found;
227 }
228 if (!bitmaskcmp(&ifsin6->sin6_addr,
229 &clntsin6->sin6_addr, &sin6mask->sin6_addr,
230 sizeof (struct in6_addr)))
231 goto match;
232 break;
233 #endif
234 default:
235 goto freeit;
236 }
237 }
238 found:
239 if (ifap != NULL)
240 ret = taddr2uaddr(nconf, &tbuf);
241 freeit:
242 free(serv_sa);
243 free(serv_nbp);
244 if (clnt_sa != NULL)
245 free(clnt_sa);
246 if (clnt_nbp != NULL)
247 free(clnt_nbp);
248 freeifaddrs(ifp);
249
250 #ifdef ND_DEBUG
251 if (debugging)
252 fprintf(stderr, "addrmerge: returning %s\n", ret);
253 #endif
254 return ret;
255 }
256
257 void
258 network_init()
259 {
260 #ifdef INET6
261 struct ifaddrs *ifap, *ifp;
262 struct ipv6_mreq mreq6;
263 int ifindex, s;
264 #endif
265 int ecode;
266 struct addrinfo hints, *res;
267
268 memset(&hints, 0, sizeof hints);
269 hints.ai_family = AF_INET;
270 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
271 if (debugging)
272 fprintf(stderr, "can't get local ip4 address: %s\n",
273 gai_strerror(ecode));
274 } else {
275 local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
276 if (local_in4 == NULL) {
277 if (debugging)
278 fprintf(stderr, "can't alloc local ip4 addr\n");
279 }
280 memcpy(local_in4, res->ai_addr, sizeof *local_in4);
281 }
282
283 #ifdef INET6
284 hints.ai_family = AF_INET6;
285 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
286 if (debugging)
287 fprintf(stderr, "can't get local ip6 address: %s\n",
288 gai_strerror(ecode));
289 } else {
290 local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
291 if (local_in6 == NULL) {
292 if (debugging)
293 fprintf(stderr, "can't alloc local ip6 addr\n");
294 }
295 memcpy(local_in6, res->ai_addr, sizeof *local_in6);
296 }
297
298 /*
299 * Now join the RPC ipv6 multicast group on all interfaces.
300 */
301 if (getifaddrs(&ifp) < 0)
302 return;
303
304 mreq6.ipv6mr_interface = 0;
305 inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
306
307 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
308
309 /*
310 * Loop through all interfaces. For each interface, see if the
311 * network portion of its address is equal to that of the client.
312 * If so, we have found the interface that we want to use.
313 */
314 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
315 if (ifap->ifa_addr->sa_family != AF_INET6 ||
316 !(ifap->ifa_flags & IFF_MULTICAST))
317 continue;
318 ifindex = if_nametoindex(ifap->ifa_name);
319 if (ifindex == mreq6.ipv6mr_interface)
320 /*
321 * Already did this one.
322 */
323 continue;
324 mreq6.ipv6mr_interface = ifindex;
325 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
326 sizeof mreq6) < 0)
327 if (debugging)
328 perror("setsockopt v6 multicast");
329 }
330 #endif
331
332 /* close(s); */
333 }
334
335 struct sockaddr *
336 local_sa(int af)
337 {
338 switch (af) {
339 case AF_INET:
340 return (struct sockaddr *)local_in4;
341 #ifdef INET6
342 case AF_INET6:
343 return (struct sockaddr *)local_in6;
344 #endif
345 default:
346 return NULL;
347 }
348 }
349