1/*
2 * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
3 * $FreeBSD: head/usr.sbin/rpcbind/util.c 293229 2016-01-06 00:00:11Z asomers $
4 */
5
6/*-
7 * Copyright (c) 2000 The NetBSD Foundation, Inc.
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Frank van der Linden.
--- 39 unchanged lines hidden (view full) ---
51
52#include "rpcbind.h"
53
54static struct sockaddr_in *local_in4;
55#ifdef INET6
56static struct sockaddr_in6 *local_in6;
57#endif
58
59static int bitmaskcmp(struct sockaddr *, struct sockaddr *, struct sockaddr *);
60
61/*
62 * For all bits set in "mask", compare the corresponding bits in
63 * "dst" and "src", and see if they match. Returns 0 if the addresses
64 * match.
65 */
66static int
67bitmaskcmp(struct sockaddr *dst, struct sockaddr *src, struct sockaddr *mask)
68{
69 int i;
70 u_int8_t *p1, *p2, *netmask;
71 int bytelen;
72
73 if (dst->sa_family != src->sa_family ||
74 dst->sa_family != mask->sa_family)
75 return (1);
76
77 switch (dst->sa_family) {
78 case AF_INET:
79 p1 = (uint8_t*) &SA2SINADDR(dst);
80 p2 = (uint8_t*) &SA2SINADDR(src);
81 netmask = (uint8_t*) &SA2SINADDR(mask);
82 bytelen = sizeof(struct in_addr);
83 break;
84#ifdef INET6
85 case AF_INET6:
86 p1 = (uint8_t*) &SA2SIN6ADDR(dst);
87 p2 = (uint8_t*) &SA2SIN6ADDR(src);
88 netmask = (uint8_t*) &SA2SIN6ADDR(mask);
89 bytelen = sizeof(struct in6_addr);
90 break;
91#endif
92 default:
93 return (1);
94 }
95
96 for (i = 0; i < bytelen; i++)
97 if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
98 return (1);
99 return (0);
100}
101
102/*
103 * Find a server address that can be used by `caller' to contact
104 * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
105 * non-NULL, it is used instead of `caller' as a hint suggesting
106 * the best address (e.g. the `r_addr' field of an rpc, which
107 * contains the rpcbind server address that the caller used).
108 *
109 * Returns the best server address as a malloc'd "universal address"
110 * string which should be freed by the caller. On error, returns NULL.
111 */
112char *
113addrmerge(struct netbuf *caller, const char *serv_uaddr, const char *clnt_uaddr,
114 const char *netid)
115{
116 struct ifaddrs *ifap, *ifp = NULL, *bestif;
117 struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
118 struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
119 struct sockaddr_storage ss;
120 struct netconfig *nconf;
121 char *caller_uaddr = NULL;
122 const char *hint_uaddr = NULL;
123 char *ret = NULL;
124 int bestif_goodness;
125
126#ifdef ND_DEBUG
127 if (debugging)
128 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
129 clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
130#endif
131 caller_sa = caller->buf;
132 if ((nconf = rpcbind_get_conf(netid)) == NULL)
--- 27 unchanged lines hidden (view full) ---
160 ret = strdup(serv_uaddr);
161 goto freeit;
162 }
163
164 if (getifaddrs(&ifp) < 0)
165 goto freeit;
166
167 /*
168 * Loop through all interface addresses. We are listening to an address
169 * if any of the following are true:
170 * a) It's a loopback address
171 * b) It was specified with the -h command line option
172 * c) There were no -h command line options.
173 *
174 * Among addresses on which we are listening, choose in order of
175 * preference an address that is:
176 *
177 * a) Equal to the hint
178 * b) A link local address with the same scope ID as the client's
179 * address, if the client's address is also link local
180 * c) An address on the same subnet as the client's address
181 * d) A non-localhost, non-p2p address
182 * e) Any usable address
183 */
184 bestif = NULL;
185 bestif_goodness = 0;
186 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
187 ifsa = ifap->ifa_addr;
188 ifmasksa = ifap->ifa_netmask;
189
190 /* Skip addresses where we don't listen */
191 if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
192 !(ifap->ifa_flags & IFF_UP))
193 continue;
194
195 if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
196 continue;
197
198 if ((hint_sa->sa_family == AF_INET) &&
199 ((((struct sockaddr_in*)hint_sa)->sin_addr.s_addr ==
200 ((struct sockaddr_in*)ifsa)->sin_addr.s_addr))) {
201 const int goodness = 4;
202
203 bestif_goodness = goodness;
204 bestif = ifap;
205 goto found;
206 }
207#ifdef INET6
208 if ((hint_sa->sa_family == AF_INET6) &&
209 (0 == memcmp(&((struct sockaddr_in6*)hint_sa)->sin6_addr,
210 &((struct sockaddr_in6*)ifsa)->sin6_addr,
211 sizeof(struct in6_addr))) &&
212 (((struct sockaddr_in6*)hint_sa)->sin6_scope_id ==
213 (((struct sockaddr_in6*)ifsa)->sin6_scope_id))) {
214 const int goodness = 4;
215
216 bestif_goodness = goodness;
217 bestif = ifap;
218 goto found;
219 }
220 if (hint_sa->sa_family == AF_INET6) {
221 /*
222 * For v6 link local addresses, if the caller is on
223 * a link-local address then use the scope id to see
224 * which one.
225 */
226 if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
227 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
228 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
229 if (SA2SIN6(ifsa)->sin6_scope_id ==
230 SA2SIN6(caller_sa)->sin6_scope_id) {
231 const int goodness = 3;
232
233 if (bestif_goodness < goodness) {
234 bestif = ifap;
235 bestif_goodness = goodness;
236 }
237 }
238 }
239 }
240#endif /* INET6 */
241 if (0 == bitmaskcmp(hint_sa, ifsa, ifmasksa)) {
242 const int goodness = 2;
243
244 if (bestif_goodness < goodness) {
245 bestif = ifap;
246 bestif_goodness = goodness;
247 }
248 }
249 if (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
250 const int goodness = 1;
251
252 if (bestif_goodness < goodness) {
253 bestif = ifap;
254 bestif_goodness = goodness;
255 }
256 }
257 if (bestif == NULL)
258 bestif = ifap;
259 }
260 if (bestif == NULL)
261 goto freeit;
262
263found:
264 /*
265 * Construct the new address using the address from
--- 136 unchanged lines hidden ---
2 * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
3 * $FreeBSD: head/usr.sbin/rpcbind/util.c 293229 2016-01-06 00:00:11Z asomers $
4 */
5
6/*-
7 * Copyright (c) 2000 The NetBSD Foundation, Inc.
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Frank van der Linden.
--- 39 unchanged lines hidden (view full) ---
51
52#include "rpcbind.h"
53
54static struct sockaddr_in *local_in4;
55#ifdef INET6
56static struct sockaddr_in6 *local_in6;
57#endif
58
59static int bitmaskcmp(struct sockaddr *, struct sockaddr *, struct sockaddr *);
60
61/*
62 * For all bits set in "mask", compare the corresponding bits in
63 * "dst" and "src", and see if they match. Returns 0 if the addresses
64 * match.
65 */
66static int
67bitmaskcmp(struct sockaddr *dst, struct sockaddr *src, struct sockaddr *mask)
68{
69 int i;
70 u_int8_t *p1, *p2, *netmask;
71 int bytelen;
72
73 if (dst->sa_family != src->sa_family ||
74 dst->sa_family != mask->sa_family)
75 return (1);
76
77 switch (dst->sa_family) {
78 case AF_INET:
79 p1 = (uint8_t*) &SA2SINADDR(dst);
80 p2 = (uint8_t*) &SA2SINADDR(src);
81 netmask = (uint8_t*) &SA2SINADDR(mask);
82 bytelen = sizeof(struct in_addr);
83 break;
84#ifdef INET6
85 case AF_INET6:
86 p1 = (uint8_t*) &SA2SIN6ADDR(dst);
87 p2 = (uint8_t*) &SA2SIN6ADDR(src);
88 netmask = (uint8_t*) &SA2SIN6ADDR(mask);
89 bytelen = sizeof(struct in6_addr);
90 break;
91#endif
92 default:
93 return (1);
94 }
95
96 for (i = 0; i < bytelen; i++)
97 if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
98 return (1);
99 return (0);
100}
101
102/*
103 * Find a server address that can be used by `caller' to contact
104 * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
105 * non-NULL, it is used instead of `caller' as a hint suggesting
106 * the best address (e.g. the `r_addr' field of an rpc, which
107 * contains the rpcbind server address that the caller used).
108 *
109 * Returns the best server address as a malloc'd "universal address"
110 * string which should be freed by the caller. On error, returns NULL.
111 */
112char *
113addrmerge(struct netbuf *caller, const char *serv_uaddr, const char *clnt_uaddr,
114 const char *netid)
115{
116 struct ifaddrs *ifap, *ifp = NULL, *bestif;
117 struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
118 struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
119 struct sockaddr_storage ss;
120 struct netconfig *nconf;
121 char *caller_uaddr = NULL;
122 const char *hint_uaddr = NULL;
123 char *ret = NULL;
124 int bestif_goodness;
125
126#ifdef ND_DEBUG
127 if (debugging)
128 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
129 clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
130#endif
131 caller_sa = caller->buf;
132 if ((nconf = rpcbind_get_conf(netid)) == NULL)
--- 27 unchanged lines hidden (view full) ---
160 ret = strdup(serv_uaddr);
161 goto freeit;
162 }
163
164 if (getifaddrs(&ifp) < 0)
165 goto freeit;
166
167 /*
168 * Loop through all interface addresses. We are listening to an address
169 * if any of the following are true:
170 * a) It's a loopback address
171 * b) It was specified with the -h command line option
172 * c) There were no -h command line options.
173 *
174 * Among addresses on which we are listening, choose in order of
175 * preference an address that is:
176 *
177 * a) Equal to the hint
178 * b) A link local address with the same scope ID as the client's
179 * address, if the client's address is also link local
180 * c) An address on the same subnet as the client's address
181 * d) A non-localhost, non-p2p address
182 * e) Any usable address
183 */
184 bestif = NULL;
185 bestif_goodness = 0;
186 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
187 ifsa = ifap->ifa_addr;
188 ifmasksa = ifap->ifa_netmask;
189
190 /* Skip addresses where we don't listen */
191 if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
192 !(ifap->ifa_flags & IFF_UP))
193 continue;
194
195 if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
196 continue;
197
198 if ((hint_sa->sa_family == AF_INET) &&
199 ((((struct sockaddr_in*)hint_sa)->sin_addr.s_addr ==
200 ((struct sockaddr_in*)ifsa)->sin_addr.s_addr))) {
201 const int goodness = 4;
202
203 bestif_goodness = goodness;
204 bestif = ifap;
205 goto found;
206 }
207#ifdef INET6
208 if ((hint_sa->sa_family == AF_INET6) &&
209 (0 == memcmp(&((struct sockaddr_in6*)hint_sa)->sin6_addr,
210 &((struct sockaddr_in6*)ifsa)->sin6_addr,
211 sizeof(struct in6_addr))) &&
212 (((struct sockaddr_in6*)hint_sa)->sin6_scope_id ==
213 (((struct sockaddr_in6*)ifsa)->sin6_scope_id))) {
214 const int goodness = 4;
215
216 bestif_goodness = goodness;
217 bestif = ifap;
218 goto found;
219 }
220 if (hint_sa->sa_family == AF_INET6) {
221 /*
222 * For v6 link local addresses, if the caller is on
223 * a link-local address then use the scope id to see
224 * which one.
225 */
226 if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
227 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
228 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
229 if (SA2SIN6(ifsa)->sin6_scope_id ==
230 SA2SIN6(caller_sa)->sin6_scope_id) {
231 const int goodness = 3;
232
233 if (bestif_goodness < goodness) {
234 bestif = ifap;
235 bestif_goodness = goodness;
236 }
237 }
238 }
239 }
240#endif /* INET6 */
241 if (0 == bitmaskcmp(hint_sa, ifsa, ifmasksa)) {
242 const int goodness = 2;
243
244 if (bestif_goodness < goodness) {
245 bestif = ifap;
246 bestif_goodness = goodness;
247 }
248 }
249 if (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
250 const int goodness = 1;
251
252 if (bestif_goodness < goodness) {
253 bestif = ifap;
254 bestif_goodness = goodness;
255 }
256 }
257 if (bestif == NULL)
258 bestif = ifap;
259 }
260 if (bestif == NULL)
261 goto freeit;
262
263found:
264 /*
265 * Construct the new address using the address from
--- 136 unchanged lines hidden ---