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