radix_mpath.c revision 184837
1224006Shrs/*	$KAME: radix_mpath.c,v 1.17 2004/11/08 10:29:39 itojun Exp $	*/
2224006Shrs
3224006Shrs/*
4224006Shrs * Copyright (C) 2001 WIDE Project.
5224006Shrs * All rights reserved.
6224006Shrs *
7224006Shrs * Redistribution and use in source and binary forms, with or without
8224006Shrs * modification, are permitted provided that the following conditions
9224006Shrs * are met:
10224006Shrs * 1. Redistributions of source code must retain the above copyright
11224006Shrs *    notice, this list of conditions and the following disclaimer.
12224006Shrs * 2. Redistributions in binary form must reproduce the above copyright
13224006Shrs *    notice, this list of conditions and the following disclaimer in the
14224006Shrs *    documentation and/or other materials provided with the distribution.
15224006Shrs * 3. Neither the name of the project nor the names of its contributors
16224006Shrs *    may be used to endorse or promote products derived from this software
17224006Shrs *    without specific prior written permission.
18224006Shrs *
19224006Shrs * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20224006Shrs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21224006Shrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22224006Shrs * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23224006Shrs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24224006Shrs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25224006Shrs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26224006Shrs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27224006Shrs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28224006Shrs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29224006Shrs * SUCH DAMAGE.
30224006Shrs * THE AUTHORS DO NOT GUARANTEE THAT THIS SOFTWARE DOES NOT INFRINGE
31224006Shrs * ANY OTHERS' INTELLECTUAL PROPERTIES. IN NO EVENT SHALL THE AUTHORS
32224006Shrs * BE LIABLE FOR ANY INFRINGEMENT OF ANY OTHERS' INTELLECTUAL
33224006Shrs * PROPERTIES.
34224006Shrs */
35224006Shrs
36224006Shrs#include <sys/cdefs.h>
37224144Shrs__FBSDID("$FreeBSD: head/sys/net/radix_mpath.c 184837 2008-11-11 09:40:27Z kmacy $");
38253970Shrs
39224006Shrs#include "opt_inet.h"
40224006Shrs#include "opt_inet6.h"
41224006Shrs
42224006Shrs#include <sys/param.h>
43253970Shrs#include <sys/systm.h>
44224006Shrs#include <sys/malloc.h>
45224006Shrs#include <sys/socket.h>
46253970Shrs#include <sys/domain.h>
47224006Shrs#include <sys/syslog.h>
48253970Shrs#include <net/radix.h>
49253970Shrs#include <net/radix_mpath.h>
50224006Shrs#include <net/route.h>
51224006Shrs#include <net/if.h>
52224006Shrs#include <net/if_var.h>
53253970Shrs
54224006Shrs/*
55224006Shrs * give some jitter to hash, to avoid synchronization between routers
56253970Shrs */
57224006Shrsstatic uint32_t hashjitter;
58224006Shrs
59224006Shrsint
60224006Shrsrn_mpath_capable(struct radix_node_head *rnh)
61224006Shrs{
62224144Shrs
63224006Shrs	return rnh->rnh_multipath;
64224144Shrs}
65224144Shrs
66224144Shrsstruct radix_node *
67224144Shrsrn_mpath_next(struct radix_node *rn)
68224006Shrs{
69224144Shrs	struct radix_node *next;
70224006Shrs
71224006Shrs	if (!rn->rn_dupedkey)
72224006Shrs		return NULL;
73224006Shrs	next = rn->rn_dupedkey;
74224006Shrs	if (rn->rn_mask == next->rn_mask)
75224006Shrs		return next;
76224006Shrs	else
77224006Shrs		return NULL;
78224006Shrs}
79224006Shrs
80224006Shrsu_int32_t
81224144Shrsrn_mpath_count(struct radix_node *rn)
82224006Shrs{
83224144Shrs	u_int32_t i;
84224006Shrs
85224144Shrs	i = 1;
86224006Shrs	while ((rn = rn_mpath_next(rn)) != NULL)
87224144Shrs		i++;
88224144Shrs	return i;
89224006Shrs}
90224006Shrs
91224006Shrsstruct rtentry *
92rt_mpath_matchgate(struct rtentry *rt, struct sockaddr *gate)
93{
94	struct radix_node *rn;
95
96	if (!rn_mpath_next((struct radix_node *)rt))
97		return rt;
98
99	if (!gate)
100		return NULL;
101
102	/* beyond here, we use rn as the master copy */
103	rn = (struct radix_node *)rt;
104	do {
105		rt = (struct rtentry *)rn;
106		/*
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
111		 */
112		if (rt->rt_gateway->sa_family == AF_LINK) {
113			if (!memcmp(rt->rt_ifa->ifa_addr, gate, gate->sa_len))
114				break;
115		} else {
116			if (rt->rt_gateway->sa_len == gate->sa_len &&
117			    !memcmp(rt->rt_gateway, gate, gate->sa_len))
118				break;
119		}
120	} while ((rn = rn_mpath_next(rn)) != NULL);
121
122	return (struct rtentry *)rn;
123}
124
125/*
126 * go through the chain and unlink "rt" from the list
127 * the caller will free "rt"
128 */
129int
130rt_mpath_deldup(struct rtentry *headrt, struct rtentry *rt)
131{
132        struct radix_node *t, *tt;
133
134        if (!headrt || !rt)
135            return (0);
136        t = (struct radix_node *)headrt;
137        tt = rn_mpath_next(t);
138        while (tt) {
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;
144                return (1);
145            }
146            t = tt;
147            tt = rn_mpath_next((struct radix_node *)t);
148        }
149        return (0);
150}
151
152/*
153 * check if we have the same key/mask/gateway on the table already.
154 */
155int
156rt_mpath_conflict(struct radix_node_head *rnh, struct rtentry *rt,
157    struct sockaddr *netmask)
158{
159	struct radix_node *rn, *rn1;
160	struct rtentry *rt1;
161	char *p, *q, *eq;
162	int same, l, skip;
163
164	rn = (struct radix_node *)rt;
165	rn1 = rnh->rnh_lookup(rt_key(rt), netmask, rnh);
166	if (!rn1 || rn1->rn_flags & RNF_ROOT)
167		return 0;
168
169	/*
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.
172	 */
173	rt1 = (struct rtentry *)rn1;
174
175	/* compare key. */
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))
178		goto different;
179
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) {
184			/*
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.
189			 */
190			if (rt_mask(rt1)->sa_len > skip)
191				goto different;
192			else {
193				/* no bits to compare, i.e. same*/
194				goto maskmatched;
195			}
196		}
197
198		l = rt_mask(rt1)->sa_len;
199		if (skip > l) {
200			/* no bits to compare, i.e. same */
201			goto maskmatched;
202		}
203		p = (char *)rt_mask(rt1);
204		q = (char *)netmask;
205		if (bcmp(p + skip, q + skip, l - skip))
206			goto different;
207		/*
208		 * need to go through all the bit, as netmask is not
209		 * optimal and can contain trailing 0s
210		 */
211		eq = (char *)netmask + netmask->sa_len;
212		q += l;
213		same = 1;
214		while (eq > q)
215			if (*q++) {
216				same = 0;
217				break;
218			}
219		if (!same)
220			goto different;
221	} else if (!rt_mask(rt1) && !netmask)
222		; /* no mask to compare, i.e. same */
223	else {
224		/* one has mask and the other does not, different */
225		goto different;
226	}
227
228maskmatched:
229
230	/* key/mask were the same.  compare gateway for all multipaths */
231	do {
232		rt1 = (struct rtentry *)rn1;
233
234		/* sanity: no use in comparing the same thing */
235		if (rn1 == rn)
236			continue;
237
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))
242				continue;
243		} else {
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))
247				continue;
248		}
249
250		/* all key/mask/gateway are the same.  conflicting entry. */
251		return EEXIST;
252	} while ((rn1 = rn_mpath_next(rn1)) != NULL);
253
254different:
255	return 0;
256}
257
258void
259rtalloc_mpath_fib(struct route *ro, u_int32_t hash, u_int fibnum)
260{
261	struct radix_node *rn0, *rn;
262	u_int32_t n;
263
264	/*
265	 * XXX we don't attempt to lookup cached route again; what should
266	 * be done for sendto(3) case?
267	 */
268	if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
269		return;				 /* XXX */
270	ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, RTF_CLONING, fibnum);
271
272	/* if the route does not exist or it is not multipath, don't care */
273	if (ro->ro_rt == NULL)
274		return;
275	if (rn_mpath_next((struct radix_node *)ro->ro_rt) == NULL) {
276		RT_UNLOCK(ro->ro_rt);
277		return;
278	}
279
280	/* beyond here, we use rn as the master copy */
281	rn0 = rn = (struct radix_node *)ro->ro_rt;
282	n = rn_mpath_count(rn0);
283
284	/* gw selection by Modulo-N Hash (RFC2991) XXX need improvement? */
285	hash += hashjitter;
286	hash %= n;
287	while (hash-- > 0 && rn) {
288		/* stay within the multipath routes */
289		if (rn->rn_dupedkey && rn->rn_mask != rn->rn_dupedkey->rn_mask)
290			break;
291		rn = rn->rn_dupedkey;
292	}
293
294	/* XXX try filling rt_gwroute and avoid unreachable gw  */
295
296	/* if gw selection fails, use the first match (default) */
297	if (!rn) {
298		RT_UNLOCK(ro->ro_rt);
299		return;
300	}
301
302	RTFREE_LOCKED(ro->ro_rt);
303	ro->ro_rt = (struct rtentry *)rn;
304	RT_LOCK(ro->ro_rt);
305	RT_ADDREF(ro->ro_rt);
306	RT_UNLOCK(ro->ro_rt);
307}
308
309extern int	in6_inithead(void **head, int off);
310extern int	in_inithead(void **head, int off);
311
312#ifdef INET
313int
314rn4_mpath_inithead(void **head, int off)
315{
316	struct radix_node_head *rnh;
317
318	hashjitter = arc4random();
319	if (in_inithead(head, off) == 1) {
320		rnh = (struct radix_node_head *)*head;
321		rnh->rnh_multipath = 1;
322		return 1;
323	} else
324		return 0;
325}
326#endif
327
328#ifdef INET6
329int
330rn6_mpath_inithead(void **head, int off)
331{
332	struct radix_node_head *rnh;
333
334	hashjitter = arc4random();
335	if (in6_inithead(head, off) == 1) {
336		rnh = (struct radix_node_head *)*head;
337		rnh->rnh_multipath = 1;
338		return 1;
339	} else
340		return 0;
341}
342
343#endif
344