1 .\" $NetBSD: in_getifa.9,v 1.5 2009/03/09 19:24:32 joerg Exp $
3 .\" Copyright (c) 2006 David Young. All rights reserved.
5 .\" This code was written by David Young.
7 .\" Redistribution and use in source and binary forms, with or
8 .\" without modification, are permitted provided that the following
9 .\" conditions are met:
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above
13 .\" copyright notice, this list of conditions and the following
14 .\" disclaimer in the documentation and/or other materials provided
15 .\" with the distribution.
17 .\" THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
18 .\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 .\" THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 .\" PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID
21 .\" YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 .\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23 .\" TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 .\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 .Nd Look up the IPv4 source address best matching an IPv4 destination
38 .In netinet/in_selsrc.h
40 .Fn in_getifa "struct ifaddr *ifa" "const struct sockaddr *dst0"
43 enforces the IPv4 source-address selection policy.
44 Add the source-address selection policy mechanism to your kernel with
45 .Cd options IPSELSRC .
47 lets the operator set the policy for choosing the source address
48 of any socket bound to the
52 Note that the policy is applied
54 the kernel makes its forwarding decision, thereby choosing the
56 in other words, this mechanism does not affect whether or not
61 An operator affects the source-address selection using
65 Operators set policies with
67 Some policies consider the
70 An operator may set preference numbers for each address with
73 A source-address policy is a priority-ordered list of source-address
75 A ranking function maps its arguments,
79 .Em source preference ,
80 .Em destination address
87 in the interface address list; the index of the first address is 0.
90 is the preference number the operator assigned
94 .Em destination address
95 is the socket peer / packet destination.
97 Presently, there are four ranking functions to choose from:
98 .Bl -tag -width "common-prefix-len"
102 lower indices are ranked more highly.
105 .Em source preference ;
106 higher preference numbers are ranked more highly.
107 .It common-prefix-len
110 by the length of the longest prefix it has in common with
111 .Em destination address ;
112 longer common prefixes rank more highly.
114 determines the "categories" of
117 .Em destination address .
123 If the categories exactly match, same-category assigns a rank of 2.
124 Some sources are ranked 1 by category:
138 All other sources rank 0.
140 Categories are defined as follows.
141 .Bl -tag -width "link-local"
143 RFC1918 networks, 192.168/16, 172.16/12, and 10/8
147 all other networks---i.e., not private, not link-local
151 To apply a policy, the kernel applies all ranking functions in the policy
152 to every source address, producing a vector of ranks for each source.
153 The kernel sorts the sources in descending, lexicographical order by their
154 rank-vector, and chooses the highest-ranking (first) source.
155 The kernel breaks ties by choosing the source with the least
158 The operator may set a policy on individual interfaces.
159 The operator may also set a global policy that applies to all
160 interfaces whose policy he does not set individually.
162 Here is the sysctl tree for the policy at system startup:
164 .Bd -literal -offset indent
165 net.inet.ip.selectsrc.default = index
166 net.inet.ip.interfaces.ath0.selectsrc =
167 net.inet.ip.interfaces.sip0.selectsrc =
168 net.inet.ip.interfaces.sip1.selectsrc =
169 net.inet.ip.interfaces.lo0.selectsrc =
170 net.inet.ip.interfaces.pflog0.selectsrc =
173 The policy on every interface is the
175 policy, so the default policy applies.
183 The operator may override the default policy on ath0,
184 .Bd -literal -offset indent
185 # sysctl -w net.inet.ip.interfaces.ath0.selectsrc=same-category,common-prefix-len,preference
188 yielding this policy:
189 .Bd -literal -offset indent
190 net.inet.ip.selectsrc.default = index
191 net.inet.ip.interfaces.ath0.selectsrc = same-category,common-prefix-len,preference
194 The operator may set a new default,
195 .Bd -literal -offset indent
196 # sysctl -w net.inet.ip.selectsrc.debug=\
197 \*[Gt] same-category,common-prefix-len,preference
198 # sysctl -w net.inet.ip.interfaces.ath0.selectsrc=
201 yielding this policy:
202 .Bd -literal -offset indent
203 net.inet.ip.selectsrc.default = same-category,common-prefix-len,preference
204 net.inet.ip.interfaces.ath0.selectsrc =
207 In a number of applications, the policy above will usually pick
208 suitable source addresses if ath0 is configured in this way:
209 .Bd -literal -offset indent
210 # ifconfig ath0 inet 64.198.255.1/24
211 # ifconfig ath0 inet 10.0.0.1/24
212 # ifconfig ath0 inet 169.254.1.1/24
213 # ifconfig ath0 inet 192.168.49.1/24 preference 5
214 # ifconfig ath0 inet 192.168.37.1/24 preference 9
216 A sysctl, net.inet.ip.selectsrc.debug, turns on and off debug messages
217 concerned with source selection.
218 You may set it to 0 (no messages) or 1.
223 The family of IPv6 source-address selection policies defined by
225 resembles the family of IPv4 policies that
229 .An David Young Aq dyoung@NetBSD.org
232 .Cd options IPSELSRC ,
235 .Dv SIOCSIFADDRPREF ,
237 It ought to be documented in
241 ought to cross-reference this manual page.
243 This work should be used to set IPv6 source-address selection
244 policies, especially the family of policies defined by