in_selsrc.c revision 1.3.8.3 1 1.3.8.3 yamt /* $NetBSD: in_selsrc.c,v 1.3.8.3 2007/02/26 09:11:43 yamt Exp $ */
2 1.3.8.2 yamt
3 1.3.8.2 yamt /*-
4 1.3.8.2 yamt * Copyright (c) 2005 David Young. All rights reserved.
5 1.3.8.2 yamt *
6 1.3.8.2 yamt * This code was written by David Young.
7 1.3.8.2 yamt *
8 1.3.8.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.3.8.2 yamt * modification, are permitted provided that the following conditions
10 1.3.8.2 yamt * are met:
11 1.3.8.2 yamt * 1. Redistributions of source code must retain the above copyright
12 1.3.8.2 yamt * notice, this list of conditions and the following disclaimer.
13 1.3.8.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
14 1.3.8.2 yamt * notice, this list of conditions and the following disclaimer in the
15 1.3.8.2 yamt * documentation and/or other materials provided with the distribution.
16 1.3.8.2 yamt * 3. All advertising materials mentioning features or use of this software
17 1.3.8.2 yamt * must display the following acknowledgement:
18 1.3.8.2 yamt * This product includes software developed by David Young.
19 1.3.8.2 yamt * 4. The name of David Young may not be used to endorse or promote
20 1.3.8.2 yamt * products derived from this software without specific prior
21 1.3.8.2 yamt * written permission.
22 1.3.8.2 yamt *
23 1.3.8.2 yamt * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
24 1.3.8.2 yamt * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 1.3.8.2 yamt * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26 1.3.8.2 yamt * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 1.3.8.2 yamt * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 1.3.8.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 1.3.8.2 yamt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.3.8.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 1.3.8.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 1.3.8.2 yamt * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 1.3.8.2 yamt * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 1.3.8.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 1.3.8.2 yamt */
36 1.3.8.2 yamt
37 1.3.8.2 yamt #include <sys/cdefs.h>
38 1.3.8.3 yamt __KERNEL_RCSID(0, "$NetBSD: in_selsrc.c,v 1.3.8.3 2007/02/26 09:11:43 yamt Exp $");
39 1.3.8.2 yamt
40 1.3.8.2 yamt #include "opt_inet.h"
41 1.3.8.2 yamt #include "opt_inet_conf.h"
42 1.3.8.2 yamt
43 1.3.8.2 yamt #include <lib/libkern/libkern.h>
44 1.3.8.2 yamt
45 1.3.8.2 yamt #include <sys/param.h>
46 1.3.8.2 yamt #include <sys/ioctl.h>
47 1.3.8.2 yamt #include <sys/errno.h>
48 1.3.8.2 yamt #include <sys/malloc.h>
49 1.3.8.2 yamt #include <sys/socket.h>
50 1.3.8.2 yamt #include <sys/socketvar.h>
51 1.3.8.2 yamt #include <sys/sysctl.h>
52 1.3.8.2 yamt #include <sys/systm.h>
53 1.3.8.2 yamt #include <sys/proc.h>
54 1.3.8.2 yamt #include <sys/syslog.h>
55 1.3.8.2 yamt
56 1.3.8.2 yamt #include <net/if.h>
57 1.3.8.2 yamt
58 1.3.8.2 yamt #include <net/if_ether.h>
59 1.3.8.2 yamt
60 1.3.8.2 yamt #include <netinet/in_systm.h>
61 1.3.8.2 yamt #include <netinet/in.h>
62 1.3.8.2 yamt #include <netinet/in_var.h>
63 1.3.8.2 yamt #include <netinet/ip.h>
64 1.3.8.2 yamt #include <netinet/ip_var.h>
65 1.3.8.2 yamt #include <netinet/in_ifattach.h>
66 1.3.8.2 yamt #include <netinet/in_pcb.h>
67 1.3.8.2 yamt #include <netinet/if_inarp.h>
68 1.3.8.2 yamt #include <netinet/ip_mroute.h>
69 1.3.8.2 yamt #include <netinet/igmp_var.h>
70 1.3.8.2 yamt #include <netinet/in_selsrc.h>
71 1.3.8.2 yamt
72 1.3.8.2 yamt #ifdef INET
73 1.3.8.2 yamt struct score_src_name {
74 1.3.8.2 yamt const char *sn_name;
75 1.3.8.2 yamt const in_score_src_t sn_score_src;
76 1.3.8.2 yamt };
77 1.3.8.2 yamt
78 1.3.8.2 yamt static const struct sysctlnode *in_domifattach_sysctl(struct in_ifsysctl *);
79 1.3.8.2 yamt static int in_preference(const struct in_addr *, int, int,
80 1.3.8.2 yamt const struct in_addr *);
81 1.3.8.2 yamt static int in_index(const struct in_addr *, int, int, const struct in_addr *);
82 1.3.8.2 yamt static int in_matchlen(const struct in_addr *, int, int,
83 1.3.8.2 yamt const struct in_addr *);
84 1.3.8.2 yamt static int in_match_category(const struct in_addr *, int, int,
85 1.3.8.2 yamt const struct in_addr *);
86 1.3.8.2 yamt static size_t in_get_selectsrc(const struct in_ifselsrc *, char *,
87 1.3.8.2 yamt const size_t);
88 1.3.8.2 yamt static int in_set_selectsrc(struct in_ifselsrc *, char *buf);
89 1.3.8.2 yamt static int in_sysctl_selectsrc(SYSCTLFN_PROTO);
90 1.3.8.2 yamt static in_score_src_t name_to_score_src(const char *);
91 1.3.8.2 yamt static const char *score_src_to_name(const in_score_src_t);
92 1.3.8.2 yamt static void in_score(const in_score_src_t *, int *, int *,
93 1.3.8.2 yamt const struct in_addr *, int, int, const struct in_addr *);
94 1.3.8.2 yamt
95 1.3.8.2 yamt static const struct score_src_name score_src_names[] = {
96 1.3.8.2 yamt {"same-category", in_match_category}
97 1.3.8.2 yamt , {"common-prefix-len", in_matchlen}
98 1.3.8.2 yamt , {"index", in_index}
99 1.3.8.2 yamt , {"preference", in_preference}
100 1.3.8.2 yamt , {NULL, NULL}
101 1.3.8.2 yamt };
102 1.3.8.2 yamt
103 1.3.8.2 yamt static const struct in_ifselsrc initial_iss = { 0, {NULL} };
104 1.3.8.2 yamt
105 1.3.8.2 yamt static struct in_ifselsrc default_iss = { 0, {in_index} };
106 1.3.8.2 yamt
107 1.3.8.2 yamt #ifdef GETIFA_DEBUG
108 1.3.8.2 yamt int in_selsrc_debug = 0;
109 1.3.8.3 yamt #endif /* GETIFA_DEBUG */
110 1.3.8.2 yamt
111 1.3.8.2 yamt SYSCTL_SETUP(sysctl_selectsrc_setup, "sysctl selectsrc subtree setup")
112 1.3.8.2 yamt {
113 1.3.8.2 yamt int rc;
114 1.3.8.2 yamt const struct sysctlnode *rnode, *cnode;
115 1.3.8.2 yamt
116 1.3.8.2 yamt if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
117 1.3.8.2 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE, "net",
118 1.3.8.2 yamt NULL, NULL, 0, NULL, 0, CTL_NET, CTL_EOL)) != 0) {
119 1.3.8.2 yamt printf("%s: could not create net, rc = %d\n", __func__, rc);
120 1.3.8.2 yamt return;
121 1.3.8.2 yamt }
122 1.3.8.2 yamt if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
123 1.3.8.2 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE, "inet",
124 1.3.8.2 yamt NULL, NULL, 0, NULL, 0, CTL_NET, PF_INET, CTL_EOL)) != 0) {
125 1.3.8.2 yamt printf("%s: could not create net.inet, rc = %d\n", __func__,
126 1.3.8.2 yamt rc);
127 1.3.8.2 yamt return;
128 1.3.8.2 yamt }
129 1.3.8.2 yamt if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
130 1.3.8.2 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE, "ip",
131 1.3.8.2 yamt NULL, NULL, 0, NULL, 0,
132 1.3.8.2 yamt CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL)) != 0) {
133 1.3.8.2 yamt printf("%s: could not create net.inet.ip, rc = %d\n", __func__,
134 1.3.8.2 yamt rc);
135 1.3.8.2 yamt return;
136 1.3.8.2 yamt }
137 1.3.8.2 yamt if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
138 1.3.8.2 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE, "selectsrc",
139 1.3.8.2 yamt NULL, NULL, 0, NULL, 0,
140 1.3.8.2 yamt CTL_NET, PF_INET, IPPROTO_IP, CTL_CREATE, CTL_EOL)) != 0) {
141 1.3.8.2 yamt printf("%s: could not create net.inet.ip.selectsrc, "
142 1.3.8.2 yamt "rc = %d\n", __func__, rc);
143 1.3.8.2 yamt return;
144 1.3.8.2 yamt }
145 1.3.8.3 yamt #ifdef GETIFA_DEBUG
146 1.3.8.2 yamt if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
147 1.3.8.2 yamt CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "debug",
148 1.3.8.2 yamt SYSCTL_DESCR("enable source-selection debug messages"),
149 1.3.8.2 yamt NULL, 0, &in_selsrc_debug, 0, CTL_CREATE, CTL_EOL)) != 0) {
150 1.3.8.2 yamt printf("%s: could not create net.inet.ip.selectsrc.debug, "
151 1.3.8.2 yamt "rc = %d\n", __func__, rc);
152 1.3.8.2 yamt return;
153 1.3.8.2 yamt }
154 1.3.8.3 yamt #endif /* GETIFA_DEBUG */
155 1.3.8.2 yamt if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
156 1.3.8.2 yamt CTLFLAG_READWRITE, CTLTYPE_STRING, "default",
157 1.3.8.2 yamt SYSCTL_DESCR("default source selection policy"),
158 1.3.8.2 yamt in_sysctl_selectsrc, 0, &default_iss, IN_SELECTSRC_LEN,
159 1.3.8.2 yamt CTL_CREATE, CTL_EOL)) != 0) {
160 1.3.8.2 yamt printf(
161 1.3.8.2 yamt "%s: could not create net.inet.ip.selectsrc.default (%d)\n",
162 1.3.8.2 yamt __func__, rc);
163 1.3.8.2 yamt return;
164 1.3.8.2 yamt }
165 1.3.8.2 yamt }
166 1.3.8.2 yamt
167 1.3.8.2 yamt /*
168 1.3.8.3 yamt * Score by address preference: prefer addresses with higher preference
169 1.3.8.2 yamt * number. Preference numbers are assigned with ioctl SIOCSIFADDRPREF.
170 1.3.8.2 yamt */
171 1.3.8.2 yamt static int
172 1.3.8.2 yamt in_preference(const struct in_addr *src, int preference,
173 1.3.8.2 yamt int idx, const struct in_addr *dst)
174 1.3.8.2 yamt {
175 1.3.8.3 yamt return preference;
176 1.3.8.2 yamt }
177 1.3.8.2 yamt
178 1.3.8.2 yamt /*
179 1.3.8.2 yamt * Score by address "index": prefer addresses nearer the head of
180 1.3.8.2 yamt * the ifaddr list.
181 1.3.8.2 yamt */
182 1.3.8.2 yamt static int
183 1.3.8.2 yamt in_index(const struct in_addr *src, int preference, int idx,
184 1.3.8.2 yamt const struct in_addr *dst)
185 1.3.8.2 yamt {
186 1.3.8.2 yamt return -idx;
187 1.3.8.2 yamt }
188 1.3.8.2 yamt
189 1.3.8.2 yamt /*
190 1.3.8.2 yamt * Length of longest common prefix of src and dst.
191 1.3.8.2 yamt *
192 1.3.8.2 yamt * (Derived from in6_matchlen.)
193 1.3.8.2 yamt */
194 1.3.8.2 yamt static int
195 1.3.8.2 yamt in_matchlen(const struct in_addr *src, int preference,
196 1.3.8.2 yamt int idx, const struct in_addr *dst)
197 1.3.8.2 yamt {
198 1.3.8.2 yamt int match = 0;
199 1.3.8.2 yamt const uint8_t *s = (const uint8_t *)src, *d = (const uint8_t *)dst;
200 1.3.8.2 yamt const uint8_t *lim = s + 4;
201 1.3.8.2 yamt uint_fast8_t r = 0;
202 1.3.8.2 yamt
203 1.3.8.2 yamt while (s < lim && (r = (*d++ ^ *s++)) == 0)
204 1.3.8.2 yamt match += 8;
205 1.3.8.2 yamt
206 1.3.8.2 yamt if (s == lim)
207 1.3.8.2 yamt return match;
208 1.3.8.2 yamt
209 1.3.8.2 yamt while ((r & 0x80) == 0) {
210 1.3.8.2 yamt match++;
211 1.3.8.2 yamt r <<= 1;
212 1.3.8.2 yamt }
213 1.3.8.2 yamt return match;
214 1.3.8.2 yamt }
215 1.3.8.2 yamt
216 1.3.8.2 yamt static enum in_category
217 1.3.8.2 yamt in_categorize(const struct in_addr *s)
218 1.3.8.2 yamt {
219 1.3.8.2 yamt if (IN_ANY_LOCAL(s->s_addr))
220 1.3.8.2 yamt return IN_CATEGORY_LINKLOCAL;
221 1.3.8.2 yamt else if (IN_PRIVATE(s->s_addr))
222 1.3.8.2 yamt return IN_CATEGORY_PRIVATE;
223 1.3.8.2 yamt else
224 1.3.8.2 yamt return IN_CATEGORY_OTHER;
225 1.3.8.2 yamt }
226 1.3.8.2 yamt
227 1.3.8.2 yamt static int
228 1.3.8.2 yamt in_match_category(const struct in_addr *src, int preference,
229 1.3.8.2 yamt int idx, const struct in_addr *dst)
230 1.3.8.2 yamt {
231 1.3.8.2 yamt enum in_category dst_c = in_categorize(dst),
232 1.3.8.2 yamt src_c = in_categorize(src);
233 1.3.8.2 yamt #ifdef GETIFA_DEBUG
234 1.3.8.2 yamt if (in_selsrc_debug) {
235 1.3.8.2 yamt printf("%s: dst %#08" PRIx32 " categ %d, src %#08" PRIx32
236 1.3.8.2 yamt " categ %d\n", __func__, ntohl(dst->s_addr), dst_c,
237 1.3.8.2 yamt ntohl(src->s_addr), src_c);
238 1.3.8.2 yamt }
239 1.3.8.2 yamt #endif /* GETIFA_DEBUG */
240 1.3.8.2 yamt
241 1.3.8.2 yamt if (dst_c == src_c)
242 1.3.8.2 yamt return 2;
243 1.3.8.2 yamt else if (dst_c == IN_CATEGORY_LINKLOCAL && src_c == IN_CATEGORY_PRIVATE)
244 1.3.8.2 yamt return 1;
245 1.3.8.2 yamt else if (dst_c == IN_CATEGORY_PRIVATE && src_c == IN_CATEGORY_LINKLOCAL)
246 1.3.8.2 yamt return 1;
247 1.3.8.2 yamt else if (dst_c == IN_CATEGORY_OTHER && src_c == IN_CATEGORY_PRIVATE)
248 1.3.8.2 yamt return 1;
249 1.3.8.2 yamt else
250 1.3.8.2 yamt return 0;
251 1.3.8.2 yamt }
252 1.3.8.2 yamt
253 1.3.8.2 yamt static void
254 1.3.8.2 yamt in_score(const in_score_src_t *score_src, int *score, int *scorelenp,
255 1.3.8.2 yamt const struct in_addr *src, int preference, int idx,
256 1.3.8.2 yamt const struct in_addr *dst)
257 1.3.8.2 yamt {
258 1.3.8.2 yamt int i;
259 1.3.8.2 yamt
260 1.3.8.2 yamt for (i = 0; i < IN_SCORE_SRC_MAX && score_src[i] != NULL; i++)
261 1.3.8.2 yamt score[i] = (*score_src[i])(src, preference, idx, dst);
262 1.3.8.2 yamt if (scorelenp != NULL)
263 1.3.8.2 yamt *scorelenp = i;
264 1.3.8.2 yamt }
265 1.3.8.2 yamt
266 1.3.8.2 yamt static int
267 1.3.8.2 yamt in_score_cmp(int *score1, int *score2, int scorelen)
268 1.3.8.2 yamt {
269 1.3.8.2 yamt int i;
270 1.3.8.2 yamt
271 1.3.8.2 yamt for (i = 0; i < scorelen; i++) {
272 1.3.8.2 yamt if (score1[i] == score2[i])
273 1.3.8.2 yamt continue;
274 1.3.8.2 yamt return score1[i] - score2[i];
275 1.3.8.2 yamt }
276 1.3.8.2 yamt return 0;
277 1.3.8.2 yamt }
278 1.3.8.2 yamt
279 1.3.8.2 yamt #ifdef GETIFA_DEBUG
280 1.3.8.2 yamt static void
281 1.3.8.2 yamt in_score_println(int *score, int scorelen)
282 1.3.8.2 yamt {
283 1.3.8.2 yamt int i;
284 1.3.8.2 yamt const char *delim = "[";
285 1.3.8.2 yamt
286 1.3.8.2 yamt for (i = 0; i < scorelen; i++) {
287 1.3.8.2 yamt printf("%s%d", delim, score[i]);
288 1.3.8.2 yamt delim = ", ";
289 1.3.8.2 yamt }
290 1.3.8.2 yamt printf("]\n");
291 1.3.8.2 yamt }
292 1.3.8.2 yamt #endif /* GETIFA_DEBUG */
293 1.3.8.2 yamt
294 1.3.8.2 yamt /* Scan the interface addresses on the interface ifa->ifa_ifp for
295 1.3.8.2 yamt * the source address that best matches the destination, dst0,
296 1.3.8.2 yamt * according to the source address-selection policy for this
297 1.3.8.2 yamt * interface. If there is no better match than `ifa', return `ifa'.
298 1.3.8.2 yamt * Otherwise, return the best address.
299 1.3.8.2 yamt *
300 1.3.8.2 yamt * Note that in_getifa is called after the kernel has decided which
301 1.3.8.2 yamt * output interface to use (ifa->ifa_ifp), and in_getifa will not
302 1.3.8.2 yamt * scan an address belonging to any other interface.
303 1.3.8.2 yamt */
304 1.3.8.2 yamt struct ifaddr *
305 1.3.8.2 yamt in_getifa(struct ifaddr *ifa, const struct sockaddr *dst0)
306 1.3.8.2 yamt {
307 1.3.8.2 yamt const in_score_src_t *score_src;
308 1.3.8.2 yamt int idx, scorelen;
309 1.3.8.2 yamt const struct sockaddr_in *dst, *src;
310 1.3.8.2 yamt struct ifaddr *alt_ifa, *best_ifa;
311 1.3.8.2 yamt struct ifnet *ifp;
312 1.3.8.2 yamt struct in_ifsysctl *isc;
313 1.3.8.2 yamt struct in_ifselsrc *iss;
314 1.3.8.2 yamt int best_score[IN_SCORE_SRC_MAX], score[IN_SCORE_SRC_MAX];
315 1.3.8.2 yamt
316 1.3.8.2 yamt if (ifa->ifa_addr->sa_family != AF_INET ||
317 1.3.8.2 yamt dst0 == NULL || dst0->sa_family != AF_INET) { /* Possible. */
318 1.3.8.2 yamt ifa->ifa_seqno = NULL;
319 1.3.8.2 yamt return ifa;
320 1.3.8.2 yamt }
321 1.3.8.2 yamt
322 1.3.8.2 yamt ifp = ifa->ifa_ifp;
323 1.3.8.2 yamt isc = (struct in_ifsysctl *)ifp->if_afdata[AF_INET];
324 1.3.8.2 yamt if (isc != NULL && isc->isc_selsrc != NULL &&
325 1.3.8.2 yamt isc->isc_selsrc->iss_score_src[0] != NULL)
326 1.3.8.2 yamt iss = isc->isc_selsrc;
327 1.3.8.2 yamt else
328 1.3.8.2 yamt iss = &default_iss;
329 1.3.8.2 yamt score_src = &iss->iss_score_src[0];
330 1.3.8.2 yamt
331 1.3.8.2 yamt dst = (const struct sockaddr_in *)dst0;
332 1.3.8.2 yamt
333 1.3.8.2 yamt best_ifa = ifa;
334 1.3.8.2 yamt
335 1.3.8.2 yamt /* Find out the index of this ifaddr. */
336 1.3.8.2 yamt idx = 0;
337 1.3.8.2 yamt TAILQ_FOREACH(alt_ifa, &ifa->ifa_ifp->if_addrlist, ifa_list) {
338 1.3.8.2 yamt if (alt_ifa == best_ifa)
339 1.3.8.2 yamt break;
340 1.3.8.2 yamt idx++;
341 1.3.8.2 yamt }
342 1.3.8.2 yamt in_score(score_src, best_score, &scorelen, &IA_SIN(best_ifa)->sin_addr,
343 1.3.8.2 yamt best_ifa->ifa_preference, idx, &dst->sin_addr);
344 1.3.8.2 yamt
345 1.3.8.2 yamt #ifdef GETIFA_DEBUG
346 1.3.8.2 yamt if (in_selsrc_debug) {
347 1.3.8.2 yamt printf("%s: enter dst %#" PRIx32 " src %#" PRIx32 " score ",
348 1.3.8.2 yamt __func__, ntohl(dst->sin_addr.s_addr),
349 1.3.8.2 yamt ntohl(satosin(best_ifa->ifa_addr)->sin_addr.s_addr));
350 1.3.8.2 yamt in_score_println(best_score, scorelen);
351 1.3.8.2 yamt }
352 1.3.8.2 yamt #endif /* GETIFA_DEBUG */
353 1.3.8.2 yamt
354 1.3.8.2 yamt idx = -1;
355 1.3.8.2 yamt TAILQ_FOREACH(alt_ifa, &ifa->ifa_ifp->if_addrlist, ifa_list) {
356 1.3.8.2 yamt ++idx;
357 1.3.8.2 yamt src = IA_SIN(alt_ifa);
358 1.3.8.2 yamt
359 1.3.8.2 yamt if (alt_ifa == ifa || src->sin_family != AF_INET)
360 1.3.8.2 yamt continue;
361 1.3.8.2 yamt
362 1.3.8.2 yamt in_score(score_src, score, NULL, &src->sin_addr,
363 1.3.8.2 yamt alt_ifa->ifa_preference, idx, &dst->sin_addr);
364 1.3.8.2 yamt
365 1.3.8.2 yamt #ifdef GETIFA_DEBUG
366 1.3.8.2 yamt if (in_selsrc_debug) {
367 1.3.8.2 yamt printf("%s: src %#" PRIx32 " score ", __func__,
368 1.3.8.2 yamt ntohl(src->sin_addr.s_addr));
369 1.3.8.2 yamt in_score_println(score, scorelen);
370 1.3.8.2 yamt }
371 1.3.8.2 yamt #endif /* GETIFA_DEBUG */
372 1.3.8.2 yamt
373 1.3.8.2 yamt if (in_score_cmp(score, best_score, scorelen) > 0) {
374 1.3.8.2 yamt (void)memcpy(best_score, score, sizeof(best_score));
375 1.3.8.2 yamt best_ifa = alt_ifa;
376 1.3.8.2 yamt }
377 1.3.8.2 yamt }
378 1.3.8.2 yamt #ifdef GETIFA_DEBUG
379 1.3.8.2 yamt if (in_selsrc_debug) {
380 1.3.8.2 yamt printf("%s: choose src %#" PRIx32 " score ", __func__,
381 1.3.8.2 yamt ntohl(IA_SIN(best_ifa)->sin_addr.s_addr));
382 1.3.8.2 yamt in_score_println(best_score, scorelen);
383 1.3.8.2 yamt }
384 1.3.8.2 yamt #endif /* GETIFA_DEBUG */
385 1.3.8.2 yamt
386 1.3.8.2 yamt best_ifa->ifa_seqno = &iss->iss_seqno;
387 1.3.8.2 yamt return best_ifa;
388 1.3.8.2 yamt }
389 1.3.8.2 yamt
390 1.3.8.2 yamt static in_score_src_t
391 1.3.8.2 yamt name_to_score_src(const char *name)
392 1.3.8.2 yamt {
393 1.3.8.2 yamt int i;
394 1.3.8.2 yamt
395 1.3.8.2 yamt for (i = 0; score_src_names[i].sn_name != NULL; i++) {
396 1.3.8.2 yamt if (strcmp(score_src_names[i].sn_name, name) == 0)
397 1.3.8.2 yamt return score_src_names[i].sn_score_src;
398 1.3.8.2 yamt }
399 1.3.8.2 yamt return NULL;
400 1.3.8.2 yamt }
401 1.3.8.2 yamt
402 1.3.8.2 yamt static const char *
403 1.3.8.2 yamt score_src_to_name(const in_score_src_t score_src)
404 1.3.8.2 yamt {
405 1.3.8.2 yamt int i;
406 1.3.8.2 yamt for (i = 0; score_src_names[i].sn_name != NULL; i++) {
407 1.3.8.2 yamt if (score_src == score_src_names[i].sn_score_src)
408 1.3.8.2 yamt return score_src_names[i].sn_name;
409 1.3.8.2 yamt }
410 1.3.8.2 yamt return "<unknown>";
411 1.3.8.2 yamt }
412 1.3.8.2 yamt
413 1.3.8.2 yamt static size_t
414 1.3.8.2 yamt in_get_selectsrc(const struct in_ifselsrc *iss, char *buf0,
415 1.3.8.2 yamt const size_t buflen0)
416 1.3.8.2 yamt {
417 1.3.8.2 yamt int i, rc;
418 1.3.8.2 yamt char *buf = buf0;
419 1.3.8.2 yamt const char *delim;
420 1.3.8.2 yamt size_t buflen = buflen0;
421 1.3.8.2 yamt
422 1.3.8.2 yamt KASSERT(buflen >= 1);
423 1.3.8.2 yamt
424 1.3.8.2 yamt for (delim = "", i = 0;
425 1.3.8.2 yamt i < IN_SCORE_SRC_MAX && iss->iss_score_src[i] != NULL;
426 1.3.8.2 yamt delim = ",", i++) {
427 1.3.8.2 yamt rc = snprintf(buf, buflen, "%s%s",
428 1.3.8.2 yamt delim, score_src_to_name(iss->iss_score_src[i]));
429 1.3.8.2 yamt if (rc == -1)
430 1.3.8.2 yamt return buflen0 - buflen;
431 1.3.8.2 yamt if (rc >= buflen)
432 1.3.8.2 yamt return buflen0 + rc - buflen;
433 1.3.8.2 yamt buf += rc;
434 1.3.8.2 yamt buflen -= rc;
435 1.3.8.2 yamt }
436 1.3.8.2 yamt if (buf == buf0)
437 1.3.8.2 yamt *buf++ = '\0';
438 1.3.8.2 yamt return buf - buf0;
439 1.3.8.2 yamt }
440 1.3.8.2 yamt
441 1.3.8.2 yamt static int
442 1.3.8.2 yamt in_set_selectsrc(struct in_ifselsrc *iss, char *buf)
443 1.3.8.2 yamt {
444 1.3.8.2 yamt int i, s;
445 1.3.8.2 yamt char *next = buf;
446 1.3.8.2 yamt const char *name;
447 1.3.8.2 yamt in_score_src_t score_src;
448 1.3.8.2 yamt in_score_src_t scorers[IN_SCORE_SRC_MAX];
449 1.3.8.2 yamt
450 1.3.8.2 yamt memset(&scorers, 0, sizeof(scorers));
451 1.3.8.2 yamt for (i = 0;
452 1.3.8.2 yamt (name = strsep(&next, ",")) != NULL && i < IN_SCORE_SRC_MAX;
453 1.3.8.2 yamt i++) {
454 1.3.8.2 yamt if (strcmp(name, "") == 0)
455 1.3.8.2 yamt break;
456 1.3.8.2 yamt if ((score_src = name_to_score_src(name)) == NULL)
457 1.3.8.2 yamt return EINVAL;
458 1.3.8.2 yamt scorers[i] = score_src;
459 1.3.8.2 yamt }
460 1.3.8.2 yamt if (i == IN_SCORE_SRC_MAX && name != NULL)
461 1.3.8.2 yamt return EFBIG;
462 1.3.8.2 yamt s = splnet();
463 1.3.8.2 yamt (void)memcpy(iss->iss_score_src, scorers, sizeof(iss->iss_score_src));
464 1.3.8.2 yamt /* If iss affects a specific interface that used to use
465 1.3.8.2 yamt * the default policy, increase the sequence number on the
466 1.3.8.2 yamt * default policy, forcing routes that cache a source
467 1.3.8.2 yamt * (rt_ifa) found by the default policy to refresh their
468 1.3.8.2 yamt * cache.
469 1.3.8.2 yamt */
470 1.3.8.2 yamt if (iss != &default_iss && iss->iss_score_src[0] == NULL &&
471 1.3.8.2 yamt scorers[0] != NULL)
472 1.3.8.2 yamt default_iss.iss_seqno++;
473 1.3.8.2 yamt iss->iss_seqno++;
474 1.3.8.2 yamt splx(s);
475 1.3.8.2 yamt return 0;
476 1.3.8.2 yamt }
477 1.3.8.2 yamt
478 1.3.8.2 yamt /*
479 1.3.8.2 yamt * sysctl helper routine for net.inet.ip.interfaces.<interface>.selectsrc.
480 1.3.8.2 yamt * Pulls the old value out as a human-readable string, interprets
481 1.3.8.2 yamt * and records the new value.
482 1.3.8.2 yamt */
483 1.3.8.2 yamt static int
484 1.3.8.2 yamt in_sysctl_selectsrc(SYSCTLFN_ARGS)
485 1.3.8.2 yamt {
486 1.3.8.2 yamt char policy[IN_SELECTSRC_LEN];
487 1.3.8.2 yamt int error;
488 1.3.8.2 yamt struct sysctlnode node;
489 1.3.8.2 yamt struct in_ifselsrc *iss;
490 1.3.8.2 yamt
491 1.3.8.2 yamt node = *rnode;
492 1.3.8.2 yamt iss = (struct in_ifselsrc *)node.sysctl_data;
493 1.3.8.2 yamt if (oldp != NULL &&
494 1.3.8.2 yamt (error = in_get_selectsrc(iss, policy, sizeof(policy))) >= sizeof(policy))
495 1.3.8.2 yamt return error;
496 1.3.8.2 yamt node.sysctl_data = &policy[0];
497 1.3.8.2 yamt error = sysctl_lookup(SYSCTLFN_CALL(&node));
498 1.3.8.2 yamt if (error || newp == NULL)
499 1.3.8.2 yamt return (error);
500 1.3.8.2 yamt
501 1.3.8.2 yamt return in_set_selectsrc(iss, policy);
502 1.3.8.2 yamt }
503 1.3.8.2 yamt
504 1.3.8.2 yamt static const struct sysctlnode *
505 1.3.8.2 yamt in_domifattach_sysctl(struct in_ifsysctl *isc)
506 1.3.8.2 yamt {
507 1.3.8.2 yamt int rc;
508 1.3.8.2 yamt const struct sysctlnode *rnode;
509 1.3.8.2 yamt
510 1.3.8.2 yamt if ((rc = sysctl_createv(&isc->isc_log, 0, NULL, &rnode,
511 1.3.8.2 yamt CTLFLAG_READWRITE, CTLTYPE_NODE,
512 1.3.8.2 yamt "interfaces", NULL,
513 1.3.8.2 yamt NULL, 0, NULL, 0,
514 1.3.8.2 yamt CTL_NET, PF_INET, IPPROTO_IP, CTL_CREATE,
515 1.3.8.2 yamt CTL_EOL)) != 0) {
516 1.3.8.2 yamt printf("%s: could not create net.inet.ip.interfaces, rc = %d\n",
517 1.3.8.2 yamt __func__, rc);
518 1.3.8.2 yamt return NULL;
519 1.3.8.2 yamt }
520 1.3.8.2 yamt if ((rc = sysctl_createv(&isc->isc_log, 0, &rnode, &rnode,
521 1.3.8.2 yamt CTLFLAG_READWRITE, CTLTYPE_NODE,
522 1.3.8.2 yamt isc->isc_ifp->if_xname,
523 1.3.8.2 yamt SYSCTL_DESCR("interface ip options"),
524 1.3.8.2 yamt NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0) {
525 1.3.8.2 yamt printf("%s: could not create net.inet.ip.interfaces.%s, "
526 1.3.8.2 yamt "rc = %d\n", __func__, isc->isc_ifp->if_xname, rc);
527 1.3.8.2 yamt goto err;
528 1.3.8.2 yamt }
529 1.3.8.2 yamt if ((rc = sysctl_createv(&isc->isc_log, 0, &rnode, &rnode,
530 1.3.8.2 yamt CTLFLAG_READWRITE, CTLTYPE_STRING,
531 1.3.8.2 yamt "selectsrc",
532 1.3.8.2 yamt SYSCTL_DESCR("source selection policy"),
533 1.3.8.2 yamt in_sysctl_selectsrc, 0,
534 1.3.8.2 yamt isc->isc_selsrc, IN_SELECTSRC_LEN,
535 1.3.8.2 yamt CTL_CREATE, CTL_EOL)) != 0) {
536 1.3.8.2 yamt printf(
537 1.3.8.2 yamt "%s: could not create net.inet.ip.%s.selectsrc, rc = %d\n",
538 1.3.8.2 yamt __func__, isc->isc_ifp->if_xname, rc);
539 1.3.8.2 yamt goto err;
540 1.3.8.2 yamt }
541 1.3.8.2 yamt return rnode;
542 1.3.8.2 yamt err:
543 1.3.8.2 yamt sysctl_teardown(&isc->isc_log);
544 1.3.8.2 yamt return NULL;
545 1.3.8.2 yamt }
546 1.3.8.2 yamt
547 1.3.8.2 yamt void *
548 1.3.8.2 yamt in_domifattach(struct ifnet *ifp)
549 1.3.8.2 yamt {
550 1.3.8.2 yamt struct in_ifsysctl *isc;
551 1.3.8.2 yamt struct in_ifselsrc *iss;
552 1.3.8.2 yamt
553 1.3.8.2 yamt isc = (struct in_ifsysctl *)malloc(sizeof(*isc), M_IFADDR,
554 1.3.8.2 yamt M_WAITOK | M_ZERO);
555 1.3.8.2 yamt
556 1.3.8.2 yamt iss = (struct in_ifselsrc *)malloc(sizeof(*iss), M_IFADDR,
557 1.3.8.2 yamt M_WAITOK | M_ZERO);
558 1.3.8.2 yamt
559 1.3.8.2 yamt memcpy(&iss->iss_score_src[0], &initial_iss.iss_score_src[0],
560 1.3.8.2 yamt MIN(sizeof(iss->iss_score_src), sizeof(initial_iss.iss_score_src)));
561 1.3.8.2 yamt
562 1.3.8.2 yamt isc->isc_ifp = ifp;
563 1.3.8.2 yamt isc->isc_selsrc = iss;
564 1.3.8.2 yamt
565 1.3.8.2 yamt if (in_domifattach_sysctl(isc) == NULL)
566 1.3.8.2 yamt goto err;
567 1.3.8.2 yamt
568 1.3.8.2 yamt return isc;
569 1.3.8.2 yamt err:
570 1.3.8.2 yamt free(iss, M_IFADDR);
571 1.3.8.2 yamt free(isc, M_IFADDR);
572 1.3.8.2 yamt return NULL;
573 1.3.8.2 yamt }
574 1.3.8.2 yamt
575 1.3.8.2 yamt void
576 1.3.8.2 yamt in_domifdetach(struct ifnet *ifp, void *aux)
577 1.3.8.2 yamt {
578 1.3.8.2 yamt struct in_ifsysctl *isc;
579 1.3.8.2 yamt struct in_ifselsrc *iss;
580 1.3.8.2 yamt
581 1.3.8.2 yamt if (aux == NULL)
582 1.3.8.2 yamt return;
583 1.3.8.2 yamt isc = (struct in_ifsysctl *)aux;
584 1.3.8.2 yamt iss = isc->isc_selsrc;
585 1.3.8.2 yamt sysctl_teardown(&isc->isc_log);
586 1.3.8.2 yamt free(isc, M_IFADDR);
587 1.3.8.2 yamt free(iss, M_IFADDR);
588 1.3.8.2 yamt }
589 1.3.8.2 yamt #endif /* INET */
590