1 /* $KAME: radix_mpath.c,v 1.17 2004/11/08 10:29:39 itojun Exp $ */
4 * Copyright (C) 2001 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * THE AUTHORS DO NOT GUARANTEE THAT THIS SOFTWARE DOES NOT INFRINGE
31 * ANY OTHERS' INTELLECTUAL PROPERTIES. IN NO EVENT SHALL THE AUTHORS
32 * BE LIABLE FOR ANY INFRINGEMENT OF ANY OTHERS' INTELLECTUAL
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/domain.h>
47 #include <sys/syslog.h>
48 #include <net/radix.h>
49 #include <net/radix_mpath.h>
50 #include <net/route.h>
52 #include <net/if_var.h>
55 * give some jitter to hash, to avoid synchronization between routers
57 static uint32_t hashjitter;
60 rn_mpath_capable(struct radix_node_head *rnh)
63 return rnh->rnh_multipath;
67 rn_mpath_next(struct radix_node *rn)
69 struct radix_node *next;
73 next = rn->rn_dupedkey;
74 if (rn->rn_mask == next->rn_mask)
81 rn_mpath_count(struct radix_node *rn)
87 rt = (struct rtentry *)rn;
88 i += rt->rt_rmx.rmx_weight;
89 rn = rn_mpath_next(rn);
95 rt_mpath_matchgate(struct rtentry *rt, struct sockaddr *gate)
97 struct radix_node *rn;
99 if (!gate || !rt->rt_gateway)
102 /* beyond here, we use rn as the master copy */
103 rn = (struct radix_node *)rt;
105 rt = (struct rtentry *)rn;
107 * we are removing an address alias that has
108 * the same prefix as another address
109 * we need to compare the interface address because
110 * rt_gateway is a special sockadd_dl structure
112 if (rt->rt_gateway->sa_family == AF_LINK) {
113 if (!memcmp(rt->rt_ifa->ifa_addr, gate, gate->sa_len))
116 if (rt->rt_gateway->sa_len == gate->sa_len &&
117 !memcmp(rt->rt_gateway, gate, gate->sa_len))
120 } while ((rn = rn_mpath_next(rn)) != NULL);
122 return (struct rtentry *)rn;
126 * go through the chain and unlink "rt" from the list
127 * the caller will free "rt"
130 rt_mpath_deldup(struct rtentry *headrt, struct rtentry *rt)
132 struct radix_node *t, *tt;
136 t = (struct radix_node *)headrt;
137 tt = rn_mpath_next(t);
139 if (tt == (struct radix_node *)rt) {
140 t->rn_dupedkey = tt->rn_dupedkey;
141 tt->rn_dupedkey = NULL;
142 tt->rn_flags &= ~RNF_ACTIVE;
143 tt[1].rn_flags &= ~RNF_ACTIVE;
147 tt = rn_mpath_next((struct radix_node *)t);
153 * check if we have the same key/mask/gateway on the table already.
156 rt_mpath_conflict(struct radix_node_head *rnh, struct rtentry *rt,
157 struct sockaddr *netmask)
159 struct radix_node *rn, *rn1;
164 rn = (struct radix_node *)rt;
165 rn1 = rnh->rnh_lookup(rt_key(rt), netmask, rnh);
166 if (!rn1 || rn1->rn_flags & RNF_ROOT)
170 * unlike other functions we have in this file, we have to check
171 * all key/mask/gateway as rnh_lookup can match less specific entry.
173 rt1 = (struct rtentry *)rn1;
176 if (rt_key(rt1)->sa_len != rt_key(rt)->sa_len ||
177 bcmp(rt_key(rt1), rt_key(rt), rt_key(rt1)->sa_len))
180 /* key was the same. compare netmask. hairy... */
181 if (rt_mask(rt1) && netmask) {
182 skip = rnh->rnh_treetop->rn_offset;
183 if (rt_mask(rt1)->sa_len > netmask->sa_len) {
185 * as rt_mask(rt1) is made optimal by radix.c,
186 * there must be some 1-bits on rt_mask(rt1)
187 * after netmask->sa_len. therefore, in
188 * this case, the entries are different.
190 if (rt_mask(rt1)->sa_len > skip)
193 /* no bits to compare, i.e. same*/
198 l = rt_mask(rt1)->sa_len;
200 /* no bits to compare, i.e. same */
203 p = (char *)rt_mask(rt1);
205 if (bcmp(p + skip, q + skip, l - skip))
208 * need to go through all the bit, as netmask is not
209 * optimal and can contain trailing 0s
211 eq = (char *)netmask + netmask->sa_len;
221 } else if (!rt_mask(rt1) && !netmask)
222 ; /* no mask to compare, i.e. same */
224 /* one has mask and the other does not, different */
230 /* key/mask were the same. compare gateway for all multipaths */
232 rt1 = (struct rtentry *)rn1;
234 /* sanity: no use in comparing the same thing */
238 if (rt1->rt_gateway->sa_family == AF_LINK) {
239 if (rt1->rt_ifa->ifa_addr->sa_len != rt->rt_ifa->ifa_addr->sa_len ||
240 bcmp(rt1->rt_ifa->ifa_addr, rt->rt_ifa->ifa_addr,
241 rt1->rt_ifa->ifa_addr->sa_len))
244 if (rt1->rt_gateway->sa_len != rt->rt_gateway->sa_len ||
245 bcmp(rt1->rt_gateway, rt->rt_gateway,
246 rt1->rt_gateway->sa_len))
250 /* all key/mask/gateway are the same. conflicting entry. */
252 } while ((rn1 = rn_mpath_next(rn1)) != NULL);
259 rtalloc_mpath_fib(struct route *ro, uint32_t hash, u_int fibnum)
261 struct radix_node *rn0, *rn;
267 * XXX we don't attempt to lookup cached route again; what should
268 * be done for sendto(3) case?
270 if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP)
271 && RT_LINK_IS_UP(ro->ro_rt->rt_ifp))
273 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, 0, fibnum);
275 /* if the route does not exist or it is not multipath, don't care */
276 if (ro->ro_rt == NULL)
278 if (rn_mpath_next((struct radix_node *)ro->ro_rt) == NULL) {
279 RT_UNLOCK(ro->ro_rt);
283 /* beyond here, we use rn as the master copy */
284 rn0 = rn = (struct radix_node *)ro->ro_rt;
285 n = rn_mpath_count(rn0);
287 /* gw selection by Modulo-N Hash (RFC2991) XXX need improvement? */
290 for (weight = abs((int32_t)hash), rt = ro->ro_rt;
291 weight >= rt->rt_rmx.rmx_weight && rn;
292 weight -= rt->rt_rmx.rmx_weight) {
294 /* stay within the multipath routes */
295 if (rn->rn_dupedkey && rn->rn_mask != rn->rn_dupedkey->rn_mask)
297 rn = rn->rn_dupedkey;
298 rt = (struct rtentry *)rn;
300 /* XXX try filling rt_gwroute and avoid unreachable gw */
302 /* gw selection has failed - there must be only zero weight routes */
304 RT_UNLOCK(ro->ro_rt);
308 if (ro->ro_rt != rt) {
309 RTFREE_LOCKED(ro->ro_rt);
310 ro->ro_rt = (struct rtentry *)rn;
312 RT_ADDREF(ro->ro_rt);
315 RT_UNLOCK(ro->ro_rt);
318 extern int in6_inithead(void **head, int off);
319 extern int in_inithead(void **head, int off);
323 rn4_mpath_inithead(void **head, int off)
325 struct radix_node_head *rnh;
327 hashjitter = arc4random();
328 if (in_inithead(head, off) == 1) {
329 rnh = (struct radix_node_head *)*head;
330 rnh->rnh_multipath = 1;
339 rn6_mpath_inithead(void **head, int off)
341 struct radix_node_head *rnh;
343 hashjitter = arc4random();
344 if (in6_inithead(head, off) == 1) {
345 rnh = (struct radix_node_head *)*head;
346 rnh->rnh_multipath = 1;