1/*	$KAME: radix_mpath.c,v 1.17 2004/11/08 10:29:39 itojun Exp $	*/
2
3/*
4 * Copyright (C) 2001 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * 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 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.
18 *
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
29 * SUCH DAMAGE.
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
33 * PROPERTIES.
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD$");
38
39#include "opt_inet.h"
40#include "opt_inet6.h"
41
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>
51#include <net/route_var.h>
52#include <net/if.h>
53#include <net/if_var.h>
54
55/*
56 * give some jitter to hash, to avoid synchronization between routers
57 */
58static uint32_t hashjitter;
59
60int
61rt_mpath_capable(struct rib_head *rnh)
62{
63
64	return rnh->rnh_multipath;
65}
66
67int
68rn_mpath_capable(struct radix_head *rh)
69{
70
71	return (rt_mpath_capable((struct rib_head *)rh));
72}
73
74struct radix_node *
75rn_mpath_next(struct radix_node *rn)
76{
77	struct radix_node *next;
78
79	if (!rn->rn_dupedkey)
80		return NULL;
81	next = rn->rn_dupedkey;
82	if (rn->rn_mask == next->rn_mask)
83		return next;
84	else
85		return NULL;
86}
87
88uint32_t
89rn_mpath_count(struct radix_node *rn)
90{
91	uint32_t i = 0;
92	struct rtentry *rt;
93
94	while (rn != NULL) {
95		rt = (struct rtentry *)rn;
96		i += rt->rt_weight;
97		rn = rn_mpath_next(rn);
98	}
99	return (i);
100}
101
102struct rtentry *
103rt_mpath_matchgate(struct rtentry *rt, struct sockaddr *gate)
104{
105	struct radix_node *rn;
106
107	if (!gate || !rt->rt_gateway)
108		return NULL;
109
110	/* beyond here, we use rn as the master copy */
111	rn = (struct radix_node *)rt;
112	do {
113		rt = (struct rtentry *)rn;
114		/*
115		 * we are removing an address alias that has
116		 * the same prefix as another address
117		 * we need to compare the interface address because
118		 * rt_gateway is a special sockadd_dl structure
119		 */
120		if (rt->rt_gateway->sa_family == AF_LINK) {
121			if (!memcmp(rt->rt_ifa->ifa_addr, gate, gate->sa_len))
122				break;
123		}
124
125		/*
126		 * Check for other options:
127		 * 1) Routes with 'real' IPv4/IPv6 gateway
128		 * 2) Loopback host routes (another AF_LINK/sockadd_dl check)
129		 * */
130		if (rt->rt_gateway->sa_len == gate->sa_len &&
131		    !memcmp(rt->rt_gateway, gate, gate->sa_len))
132			break;
133	} while ((rn = rn_mpath_next(rn)) != NULL);
134
135	return (struct rtentry *)rn;
136}
137
138/*
139 * go through the chain and unlink "rt" from the list
140 * the caller will free "rt"
141 */
142int
143rt_mpath_deldup(struct rtentry *headrt, struct rtentry *rt)
144{
145        struct radix_node *t, *tt;
146
147        if (!headrt || !rt)
148            return (0);
149        t = (struct radix_node *)headrt;
150        tt = rn_mpath_next(t);
151        while (tt) {
152            if (tt == (struct radix_node *)rt) {
153                t->rn_dupedkey = tt->rn_dupedkey;
154                tt->rn_dupedkey = NULL;
155    	        tt->rn_flags &= ~RNF_ACTIVE;
156	        tt[1].rn_flags &= ~RNF_ACTIVE;
157                return (1);
158            }
159            t = tt;
160            tt = rn_mpath_next((struct radix_node *)t);
161        }
162        return (0);
163}
164
165/*
166 * check if we have the same key/mask/gateway on the table already.
167 * Assume @rt rt_key host bits are cleared according to @netmask
168 */
169int
170rt_mpath_conflict(struct rib_head *rnh, struct rtentry *rt,
171    struct sockaddr *netmask)
172{
173	struct radix_node *rn, *rn1;
174	struct rtentry *rt1;
175
176	rn = (struct radix_node *)rt;
177	rn1 = rnh->rnh_lookup(rt_key(rt), netmask, &rnh->head);
178	if (!rn1 || rn1->rn_flags & RNF_ROOT)
179		return (0);
180
181	/* key/mask are the same. compare gateway for all multipaths */
182	do {
183		rt1 = (struct rtentry *)rn1;
184
185		/* sanity: no use in comparing the same thing */
186		if (rn1 == rn)
187			continue;
188
189		if (rt1->rt_gateway->sa_family == AF_LINK) {
190			if (rt1->rt_ifa->ifa_addr->sa_len != rt->rt_ifa->ifa_addr->sa_len ||
191			    bcmp(rt1->rt_ifa->ifa_addr, rt->rt_ifa->ifa_addr,
192			    rt1->rt_ifa->ifa_addr->sa_len))
193				continue;
194		} else {
195			if (rt1->rt_gateway->sa_len != rt->rt_gateway->sa_len ||
196			    bcmp(rt1->rt_gateway, rt->rt_gateway,
197			    rt1->rt_gateway->sa_len))
198				continue;
199		}
200
201		/* all key/mask/gateway are the same.  conflicting entry. */
202		return (EEXIST);
203	} while ((rn1 = rn_mpath_next(rn1)) != NULL);
204
205	return (0);
206}
207
208static struct rtentry *
209rt_mpath_selectrte(struct rtentry *rte, uint32_t hash)
210{
211	struct radix_node *rn0, *rn;
212	uint32_t total_weight;
213	struct rtentry *rt;
214	int64_t weight;
215
216	/* beyond here, we use rn as the master copy */
217	rn0 = rn = (struct radix_node *)rte;
218	rt = rte;
219
220	/* gw selection by Modulo-N Hash (RFC2991) XXX need improvement? */
221	total_weight = rn_mpath_count(rn0);
222	hash += hashjitter;
223	hash %= total_weight;
224	for (weight = abs((int32_t)hash);
225	     rt != NULL && weight >= rt->rt_weight;
226	     weight -= (rt == NULL) ? 0 : rt->rt_weight) {
227
228		/* stay within the multipath routes */
229		if (rn->rn_dupedkey && rn->rn_mask != rn->rn_dupedkey->rn_mask)
230			break;
231		rn = rn->rn_dupedkey;
232		rt = (struct rtentry *)rn;
233	}
234
235	return (rt);
236}
237
238struct rtentry *
239rt_mpath_select(struct rtentry *rte, uint32_t hash)
240{
241	if (rn_mpath_next((struct radix_node *)rte) == NULL)
242		return (rte);
243
244	return (rt_mpath_selectrte(rte, hash));
245}
246
247void
248rtalloc_mpath_fib(struct route *ro, uint32_t hash, u_int fibnum)
249{
250	struct rtentry *rt;
251
252	/*
253	 * XXX we don't attempt to lookup cached route again; what should
254	 * be done for sendto(3) case?
255	 */
256	if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP)
257	    && RT_LINK_IS_UP(ro->ro_rt->rt_ifp))
258		return;
259	ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, 0, fibnum);
260
261	/* if the route does not exist or it is not multipath, don't care */
262	if (ro->ro_rt == NULL)
263		return;
264	if (rn_mpath_next((struct radix_node *)ro->ro_rt) == NULL) {
265		RT_UNLOCK(ro->ro_rt);
266		return;
267	}
268
269	rt = rt_mpath_selectrte(ro->ro_rt, hash);
270	/* XXX try filling rt_gwroute and avoid unreachable gw  */
271
272	/* gw selection has failed - there must be only zero weight routes */
273	if (!rt) {
274		RT_UNLOCK(ro->ro_rt);
275		ro->ro_rt = NULL;
276		return;
277	}
278	if (ro->ro_rt != rt) {
279		RTFREE_LOCKED(ro->ro_rt);
280		ro->ro_rt = rt;
281		RT_LOCK(ro->ro_rt);
282		RT_ADDREF(ro->ro_rt);
283
284	}
285	RT_UNLOCK(ro->ro_rt);
286}
287
288extern int	in6_inithead(void **head, int off);
289extern int	in_inithead(void **head, int off);
290
291#ifdef INET
292int
293rn4_mpath_inithead(void **head, int off)
294{
295	struct rib_head *rnh;
296
297	hashjitter = arc4random();
298	if (in_inithead(head, off) == 1) {
299		rnh = (struct rib_head *)*head;
300		rnh->rnh_multipath = 1;
301		return 1;
302	} else
303		return 0;
304}
305#endif
306
307#ifdef INET6
308int
309rn6_mpath_inithead(void **head, int off)
310{
311	struct rib_head *rnh;
312
313	hashjitter = arc4random();
314	if (in6_inithead(head, off) == 1) {
315		rnh = (struct rib_head *)*head;
316		rnh->rnh_multipath = 1;
317		return 1;
318	} else
319		return 0;
320}
321
322#endif
323