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