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