blob: 33e2c35b74b770f526045776e355e01114477fd9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * IPv4 Forwarding Information Base: semantics.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/uaccess.h>
17#include <asm/system.h>
18#include <linux/bitops.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/jiffies.h>
22#include <linux/mm.h>
23#include <linux/string.h>
24#include <linux/socket.h>
25#include <linux/sockios.h>
26#include <linux/errno.h>
27#include <linux/in.h>
28#include <linux/inet.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020029#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/proc_fs.h>
33#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020037#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <net/ip.h>
39#include <net/protocol.h>
40#include <net/route.h>
41#include <net/tcp.h>
42#include <net/sock.h>
43#include <net/ip_fib.h>
Thomas Graff21c7bc2006-08-15 00:34:17 -070044#include <net/netlink.h>
Thomas Graf4e902c52006-08-17 18:14:52 -070045#include <net/nexthop.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include "fib_lookup.h"
48
Stephen Hemminger832b4c52006-08-29 16:48:09 -070049static DEFINE_SPINLOCK(fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static struct hlist_head *fib_info_hash;
51static struct hlist_head *fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -080052static unsigned int fib_info_hash_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static unsigned int fib_info_cnt;
54
55#define DEVINDEX_HASHBITS 8
56#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
57static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
58
59#ifdef CONFIG_IP_ROUTE_MULTIPATH
60
61static DEFINE_SPINLOCK(fib_multipath_lock);
62
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000063#define for_nexthops(fi) { \
64 int nhsel; const struct fib_nh *nh; \
65 for (nhsel = 0, nh = (fi)->fib_nh; \
66 nhsel < (fi)->fib_nhs; \
67 nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000069#define change_nexthops(fi) { \
70 int nhsel; struct fib_nh *nexthop_nh; \
71 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
72 nhsel < (fi)->fib_nhs; \
73 nexthop_nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75#else /* CONFIG_IP_ROUTE_MULTIPATH */
76
77/* Hope, that gcc will optimize it to get rid of dummy loop */
78
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000079#define for_nexthops(fi) { \
80 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
81 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000083#define change_nexthops(fi) { \
84 int nhsel; \
85 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
86 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#endif /* CONFIG_IP_ROUTE_MULTIPATH */
89
90#define endfor_nexthops(fi) }
91
92
David S. Miller3be06862011-03-07 15:01:10 -080093const struct fib_prop fib_props[RTN_MAX + 1] = {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000094 [RTN_UNSPEC] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .error = 0,
96 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000097 },
98 [RTN_UNICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .error = 0,
100 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000101 },
102 [RTN_LOCAL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .error = 0,
104 .scope = RT_SCOPE_HOST,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000105 },
106 [RTN_BROADCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .error = 0,
108 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000109 },
110 [RTN_ANYCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .error = 0,
112 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000113 },
114 [RTN_MULTICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .error = 0,
116 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000117 },
118 [RTN_BLACKHOLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .error = -EINVAL,
120 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000121 },
122 [RTN_UNREACHABLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .error = -EHOSTUNREACH,
124 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000125 },
126 [RTN_PROHIBIT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .error = -EACCES,
128 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000129 },
130 [RTN_THROW] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .error = -EAGAIN,
132 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000133 },
134 [RTN_NAT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .error = -EINVAL,
136 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000137 },
138 [RTN_XRESOLVE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .error = -EINVAL,
140 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000141 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Release a nexthop info record */
145
146void free_fib_info(struct fib_info *fi)
147{
148 if (fi->fib_dead == 0) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000149 pr_warning("Freeing alive fib_info %p\n", fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return;
151 }
152 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -0800153 if (nexthop_nh->nh_dev)
154 dev_put(nexthop_nh->nh_dev);
155 nexthop_nh->nh_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 } endfor_nexthops(fi);
157 fib_info_cnt--;
Denis V. Lunev57d7a602008-04-16 02:00:50 -0700158 release_net(fi->fib_net);
Lai Jiangshan46709942011-03-18 11:42:11 +0800159 kfree_rcu(fi, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162void fib_release_info(struct fib_info *fi)
163{
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700164 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (fi && --fi->fib_treeref == 0) {
166 hlist_del(&fi->fib_hash);
167 if (fi->fib_prefsrc)
168 hlist_del(&fi->fib_lhash);
169 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -0800170 if (!nexthop_nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800172 hlist_del(&nexthop_nh->nh_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 } endfor_nexthops(fi)
174 fi->fib_dead = 1;
175 fib_info_put(fi);
176 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700177 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000180static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 const struct fib_nh *onh = ofi->fib_nh;
183
184 for_nexthops(fi) {
185 if (nh->nh_oif != onh->nh_oif ||
186 nh->nh_gw != onh->nh_gw ||
187 nh->nh_scope != onh->nh_scope ||
188#ifdef CONFIG_IP_ROUTE_MULTIPATH
189 nh->nh_weight != onh->nh_weight ||
190#endif
Patrick McHardyc7066f72011-01-14 13:36:42 +0100191#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 nh->nh_tclassid != onh->nh_tclassid ||
193#endif
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000194 ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -1;
196 onh++;
197 } endfor_nexthops(fi);
198 return 0;
199}
200
David S. Miller88ebc722008-01-12 21:49:01 -0800201static inline unsigned int fib_devindex_hashfn(unsigned int val)
202{
203 unsigned int mask = DEVINDEX_HASHSIZE - 1;
204
205 return (val ^
206 (val >> DEVINDEX_HASHBITS) ^
207 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
211{
David S. Miller123b9732011-02-01 15:34:21 -0800212 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 unsigned int val = fi->fib_nhs;
214
David S. Miller37e826c2011-03-24 18:06:47 -0700215 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
Al Viro81f7bf62006-09-27 18:40:00 -0700216 val ^= (__force u32)fi->fib_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 val ^= fi->fib_priority;
David S. Miller88ebc722008-01-12 21:49:01 -0800218 for_nexthops(fi) {
219 val ^= fib_devindex_hashfn(nh->nh_oif);
220 } endfor_nexthops(fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
223}
224
225static struct fib_info *fib_find_info(const struct fib_info *nfi)
226{
227 struct hlist_head *head;
228 struct hlist_node *node;
229 struct fib_info *fi;
230 unsigned int hash;
231
232 hash = fib_info_hashfn(nfi);
233 head = &fib_info_hash[hash];
234
235 hlist_for_each_entry(fi, node, head, fib_hash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800236 if (!net_eq(fi->fib_net, nfi->fib_net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -0800237 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (fi->fib_nhs != nfi->fib_nhs)
239 continue;
240 if (nfi->fib_protocol == fi->fib_protocol &&
David S. Miller37e826c2011-03-24 18:06:47 -0700241 nfi->fib_scope == fi->fib_scope &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 nfi->fib_prefsrc == fi->fib_prefsrc &&
243 nfi->fib_priority == fi->fib_priority &&
244 memcmp(nfi->fib_metrics, fi->fib_metrics,
Eric Dumazetfcd13f42011-03-24 07:01:24 +0000245 sizeof(u32) * RTAX_MAX) == 0 &&
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000246 ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
248 return fi;
249 }
250
251 return NULL;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/* Check, that the gateway is already configured.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000255 * Used only by redirect accept routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Al Virod878e72e2006-09-26 22:18:13 -0700257int ip_fib_check_default(__be32 gw, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct hlist_head *head;
260 struct hlist_node *node;
261 struct fib_nh *nh;
262 unsigned int hash;
263
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700264 spin_lock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 hash = fib_devindex_hashfn(dev->ifindex);
267 head = &fib_info_devhash[hash];
268 hlist_for_each_entry(nh, node, head, nh_hash) {
269 if (nh->nh_dev == dev &&
270 nh->nh_gw == gw &&
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000271 !(nh->nh_flags & RTNH_F_DEAD)) {
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700272 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return 0;
274 }
275 }
276
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700277 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 return -1;
280}
281
Thomas Graf339bf982006-11-10 14:10:15 -0800282static inline size_t fib_nlmsg_size(struct fib_info *fi)
283{
284 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
285 + nla_total_size(4) /* RTA_TABLE */
286 + nla_total_size(4) /* RTA_DST */
287 + nla_total_size(4) /* RTA_PRIORITY */
288 + nla_total_size(4); /* RTA_PREFSRC */
289
290 /* space for nested metrics */
291 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
292
293 if (fi->fib_nhs) {
294 /* Also handles the special case fib_nhs == 1 */
295
296 /* each nexthop is packed in an attribute */
297 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
298
299 /* may contain flow and gateway attribute */
300 nhsize += 2 * nla_total_size(4);
301
302 /* all nexthops are packed in a nested attribute */
303 payload += nla_total_size(fi->fib_nhs * nhsize);
304 }
305
306 return payload;
307}
308
Al Viro81f7bf62006-09-27 18:40:00 -0700309void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
Milan Kocianb8f55832007-05-23 14:55:06 -0700310 int dst_len, u32 tb_id, struct nl_info *info,
311 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct sk_buff *skb;
Thomas Graf4e902c52006-08-17 18:14:52 -0700314 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700315 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Thomas Graf339bf982006-11-10 14:10:15 -0800317 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
Thomas Graff21c7bc2006-08-15 00:34:17 -0700318 if (skb == NULL)
319 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Thomas Graf4e902c52006-08-17 18:14:52 -0700321 err = fib_dump_info(skb, info->pid, seq, event, tb_id,
David S. Miller37e826c2011-03-24 18:06:47 -0700322 fa->fa_type, key, dst_len,
Milan Kocianb8f55832007-05-23 14:55:06 -0700323 fa->fa_tos, fa->fa_info, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -0800324 if (err < 0) {
325 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
326 WARN_ON(err == -EMSGSIZE);
327 kfree_skb(skb);
328 goto errout;
329 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800330 rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
331 info->nlh, GFP_KERNEL);
332 return;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700333errout:
334 if (err < 0)
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800335 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338/* Return the first fib alias matching TOS with
339 * priority less than or equal to PRIO.
340 */
341struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
342{
343 if (fah) {
344 struct fib_alias *fa;
345 list_for_each_entry(fa, fah, fa_list) {
346 if (fa->fa_tos > tos)
347 continue;
348 if (fa->fa_info->fib_priority >= prio ||
349 fa->fa_tos < tos)
350 return fa;
351 }
352 }
353 return NULL;
354}
355
356int fib_detect_death(struct fib_info *fi, int order,
Denis V. Lunevc17860a2007-12-08 00:22:13 -0800357 struct fib_info **last_resort, int *last_idx, int dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct neighbour *n;
360 int state = NUD_NONE;
361
362 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
363 if (n) {
364 state = n->nud_state;
365 neigh_release(n);
366 }
Jianjun Kongd9319102008-11-03 00:23:42 -0800367 if (state == NUD_REACHABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000369 if ((state & NUD_VALID) && order != dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000371 if ((state & NUD_VALID) ||
372 (*last_idx < 0 && order > dflt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 *last_resort = fi;
374 *last_idx = order;
375 }
376 return 1;
377}
378
379#ifdef CONFIG_IP_ROUTE_MULTIPATH
380
Thomas Graf4e902c52006-08-17 18:14:52 -0700381static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 int nhs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Thomas Graf4e902c52006-08-17 18:14:52 -0700385 while (rtnh_ok(rtnh, remaining)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 nhs++;
Thomas Graf4e902c52006-08-17 18:14:52 -0700387 rtnh = rtnh_next(rtnh, &remaining);
388 }
389
390 /* leftover implies invalid nexthop configuration, discard it */
391 return remaining > 0 ? 0 : nhs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Thomas Graf4e902c52006-08-17 18:14:52 -0700394static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
395 int remaining, struct fib_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 change_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700398 int attrlen;
399
400 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700402
David S. Miller71fceff2010-01-15 01:16:40 -0800403 nexthop_nh->nh_flags =
404 (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
405 nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
406 nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700407
408 attrlen = rtnh_attrlen(rtnh);
409 if (attrlen > 0) {
410 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
411
412 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David S. Miller71fceff2010-01-15 01:16:40 -0800413 nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100414#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700415 nla = nla_find(attrs, attrlen, RTA_FLOW);
David S. Miller71fceff2010-01-15 01:16:40 -0800416 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#endif
418 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700419
420 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 } endfor_nexthops(fi);
Thomas Graf4e902c52006-08-17 18:14:52 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
424}
425
426#endif
427
Thomas Graf4e902c52006-08-17 18:14:52 -0700428int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700431 struct rtnexthop *rtnh;
432 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433#endif
434
Thomas Graf4e902c52006-08-17 18:14:52 -0700435 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return 1;
437
Thomas Graf4e902c52006-08-17 18:14:52 -0700438 if (cfg->fc_oif || cfg->fc_gw) {
439 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
440 (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
442 return 1;
443 }
444
445#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700446 if (cfg->fc_mp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700448
449 rtnh = cfg->fc_mp;
450 remaining = cfg->fc_mp_len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 for_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700453 int attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Thomas Graf4e902c52006-08-17 18:14:52 -0700455 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700457
458 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700460
461 attrlen = rtnh_attrlen(rtnh);
462 if (attrlen < 0) {
463 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
464
465 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
Al Viro17fb2c62006-09-26 22:15:25 -0700466 if (nla && nla_get_be32(nla) != nh->nh_gw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return 1;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100468#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700469 nla = nla_find(attrs, attrlen, RTA_FLOW);
470 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return 1;
472#endif
473 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700474
475 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 } endfor_nexthops(fi);
477#endif
478 return 0;
479}
480
481
482/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000483 * Picture
484 * -------
485 *
486 * Semantics of nexthop is very messy by historical reasons.
487 * We have to take into account, that:
488 * a) gateway can be actually local interface address,
489 * so that gatewayed route is direct.
490 * b) gateway must be on-link address, possibly
491 * described not by an ifaddr, but also by a direct route.
492 * c) If both gateway and interface are specified, they should not
493 * contradict.
494 * d) If we use tunnel routes, gateway could be not on-link.
495 *
496 * Attempt to reconcile all of these (alas, self-contradictory) conditions
497 * results in pretty ugly and hairy code with obscure logic.
498 *
499 * I chose to generalized it instead, so that the size
500 * of code does not increase practically, but it becomes
501 * much more general.
502 * Every prefix is assigned a "scope" value: "host" is local address,
503 * "link" is direct route,
504 * [ ... "site" ... "interior" ... ]
505 * and "universe" is true gateway route with global meaning.
506 *
507 * Every prefix refers to a set of "nexthop"s (gw, oif),
508 * where gw must have narrower scope. This recursion stops
509 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
510 * which means that gw is forced to be on link.
511 *
512 * Code is still hairy, but now it is apparently logically
513 * consistent and very flexible. F.e. as by-product it allows
514 * to co-exists in peace independent exterior and interior
515 * routing processes.
516 *
517 * Normally it looks as following.
518 *
519 * {universe prefix} -> (gw, oif) [scope link]
520 * |
521 * |-> {link prefix} -> (gw, oif) [scope local]
522 * |
523 * |-> {local prefix} (terminal node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
Thomas Graf4e902c52006-08-17 18:14:52 -0700525static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
526 struct fib_nh *nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 int err;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800529 struct net *net;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000530 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Denis V. Lunev86167a32008-01-21 17:34:00 -0800532 net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (nh->nh_gw) {
534 struct fib_result res;
535
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000536 if (nh->nh_flags & RTNH_F_ONLINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Thomas Graf4e902c52006-08-17 18:14:52 -0700538 if (cfg->fc_scope >= RT_SCOPE_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return -EINVAL;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800540 if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return -EINVAL;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000542 dev = __dev_get_by_index(net, nh->nh_oif);
543 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return -ENODEV;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000545 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return -ENETDOWN;
547 nh->nh_dev = dev;
548 dev_hold(dev);
549 nh->nh_scope = RT_SCOPE_LINK;
550 return 0;
551 }
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000552 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 {
David S. Miller9ade2282011-03-12 02:02:42 -0500554 struct flowi4 fl4 = {
555 .daddr = nh->nh_gw,
556 .flowi4_scope = cfg->fc_scope + 1,
557 .flowi4_oif = nh->nh_oif,
Thomas Graf4e902c52006-08-17 18:14:52 -0700558 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* It is not necessary, but requires a bit of thinking */
David S. Miller9ade2282011-03-12 02:02:42 -0500561 if (fl4.flowi4_scope < RT_SCOPE_LINK)
562 fl4.flowi4_scope = RT_SCOPE_LINK;
563 err = fib_lookup(net, &fl4, &res);
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000564 if (err) {
565 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return err;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 err = -EINVAL;
570 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
571 goto out;
572 nh->nh_scope = res.scope;
573 nh->nh_oif = FIB_RES_OIF(res);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000574 nh->nh_dev = dev = FIB_RES_DEV(res);
575 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 goto out;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000577 dev_hold(dev);
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000578 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 } else {
580 struct in_device *in_dev;
581
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000582 if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -EINVAL;
584
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000585 rcu_read_lock();
586 err = -ENODEV;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800587 in_dev = inetdev_by_index(net, nh->nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (in_dev == NULL)
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000589 goto out;
590 err = -ENETDOWN;
591 if (!(in_dev->dev->flags & IFF_UP))
592 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 nh->nh_dev = in_dev->dev;
594 dev_hold(nh->nh_dev);
595 nh->nh_scope = RT_SCOPE_HOST;
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000596 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000598out:
599 rcu_read_unlock();
600 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Al Viro81f7bf62006-09-27 18:40:00 -0700603static inline unsigned int fib_laddr_hashfn(__be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
David S. Miller123b9732011-02-01 15:34:21 -0800605 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000607 return ((__force u32)val ^
608 ((__force u32)val >> 7) ^
609 ((__force u32)val >> 14)) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
David S. Miller123b9732011-02-01 15:34:21 -0800612static struct hlist_head *fib_info_hash_alloc(int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 if (bytes <= PAGE_SIZE)
Joonwoo Park88f83492007-11-26 23:29:32 +0800615 return kzalloc(bytes, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 else
617 return (struct hlist_head *)
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000618 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
619 get_order(bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
David S. Miller123b9732011-02-01 15:34:21 -0800622static void fib_info_hash_free(struct hlist_head *hash, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 if (!hash)
625 return;
626
627 if (bytes <= PAGE_SIZE)
628 kfree(hash);
629 else
630 free_pages((unsigned long) hash, get_order(bytes));
631}
632
David S. Miller123b9732011-02-01 15:34:21 -0800633static void fib_info_hash_move(struct hlist_head *new_info_hash,
634 struct hlist_head *new_laddrhash,
635 unsigned int new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
David S. Millerb7656e72005-08-05 04:12:48 -0700637 struct hlist_head *old_info_hash, *old_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -0800638 unsigned int old_size = fib_info_hash_size;
David S. Millerb7656e72005-08-05 04:12:48 -0700639 unsigned int i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700641 spin_lock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -0700642 old_info_hash = fib_info_hash;
643 old_laddrhash = fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -0800644 fib_info_hash_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 for (i = 0; i < old_size; i++) {
647 struct hlist_head *head = &fib_info_hash[i];
648 struct hlist_node *node, *n;
649 struct fib_info *fi;
650
651 hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
652 struct hlist_head *dest;
653 unsigned int new_hash;
654
655 hlist_del(&fi->fib_hash);
656
657 new_hash = fib_info_hashfn(fi);
658 dest = &new_info_hash[new_hash];
659 hlist_add_head(&fi->fib_hash, dest);
660 }
661 }
662 fib_info_hash = new_info_hash;
663
664 for (i = 0; i < old_size; i++) {
665 struct hlist_head *lhead = &fib_info_laddrhash[i];
666 struct hlist_node *node, *n;
667 struct fib_info *fi;
668
669 hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
670 struct hlist_head *ldest;
671 unsigned int new_hash;
672
673 hlist_del(&fi->fib_lhash);
674
675 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
676 ldest = &new_laddrhash[new_hash];
677 hlist_add_head(&fi->fib_lhash, ldest);
678 }
679 }
680 fib_info_laddrhash = new_laddrhash;
681
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700682 spin_unlock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -0700683
684 bytes = old_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -0800685 fib_info_hash_free(old_info_hash, bytes);
686 fib_info_hash_free(old_laddrhash, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
David S. Miller436c3b62011-03-24 17:42:21 -0700689__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
690{
691 nh->nh_saddr = inet_select_addr(nh->nh_dev,
692 nh->nh_gw,
David S. Miller37e826c2011-03-24 18:06:47 -0700693 nh->nh_parent->fib_scope);
David S. Miller436c3b62011-03-24 17:42:21 -0700694 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
695
696 return nh->nh_saddr;
697}
698
Thomas Graf4e902c52006-08-17 18:14:52 -0700699struct fib_info *fib_create_info(struct fib_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 int err;
702 struct fib_info *fi = NULL;
703 struct fib_info *ofi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int nhs = 1;
Denis V. Lunev7462bd742008-01-31 18:49:32 -0800705 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
David S. Miller4c8237c2011-03-07 14:27:38 -0800707 if (cfg->fc_type > RTN_MAX)
708 goto err_inval;
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* Fast check to catch the most weird cases */
Thomas Graf4e902c52006-08-17 18:14:52 -0700711 if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 goto err_inval;
713
714#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700715 if (cfg->fc_mp) {
716 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (nhs == 0)
718 goto err_inval;
719 }
720#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 err = -ENOBUFS;
David S. Miller123b9732011-02-01 15:34:21 -0800723 if (fib_info_cnt >= fib_info_hash_size) {
724 unsigned int new_size = fib_info_hash_size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct hlist_head *new_info_hash;
726 struct hlist_head *new_laddrhash;
727 unsigned int bytes;
728
729 if (!new_size)
730 new_size = 1;
731 bytes = new_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -0800732 new_info_hash = fib_info_hash_alloc(bytes);
733 new_laddrhash = fib_info_hash_alloc(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (!new_info_hash || !new_laddrhash) {
David S. Miller123b9732011-02-01 15:34:21 -0800735 fib_info_hash_free(new_info_hash, bytes);
736 fib_info_hash_free(new_laddrhash, bytes);
Joonwoo Park88f83492007-11-26 23:29:32 +0800737 } else
David S. Miller123b9732011-02-01 15:34:21 -0800738 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
David S. Miller123b9732011-02-01 15:34:21 -0800740 if (!fib_info_hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 goto failure;
742 }
743
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700744 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (fi == NULL)
746 goto failure;
David S. Miller725d1e12011-01-28 14:05:05 -0800747 if (cfg->fc_mx) {
748 fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
749 if (!fi->fib_metrics)
750 goto failure;
751 } else
752 fi->fib_metrics = (u32 *) dst_default_metrics;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 fib_info_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Denis V. Lunev57d7a602008-04-16 02:00:50 -0700755 fi->fib_net = hold_net(net);
Thomas Graf4e902c52006-08-17 18:14:52 -0700756 fi->fib_protocol = cfg->fc_protocol;
David S. Miller37e826c2011-03-24 18:06:47 -0700757 fi->fib_scope = cfg->fc_scope;
Thomas Graf4e902c52006-08-17 18:14:52 -0700758 fi->fib_flags = cfg->fc_flags;
759 fi->fib_priority = cfg->fc_priority;
760 fi->fib_prefsrc = cfg->fc_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 fi->fib_nhs = nhs;
763 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -0800764 nexthop_nh->nh_parent = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 } endfor_nexthops(fi)
766
Thomas Graf4e902c52006-08-17 18:14:52 -0700767 if (cfg->fc_mx) {
768 struct nlattr *nla;
769 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Thomas Graf4e902c52006-08-17 18:14:52 -0700771 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200772 int type = nla_type(nla);
Thomas Graf4e902c52006-08-17 18:14:52 -0700773
774 if (type) {
775 if (type > RTAX_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 goto err_inval;
Thomas Graf4e902c52006-08-17 18:14:52 -0700777 fi->fib_metrics[type - 1] = nla_get_u32(nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Thomas Graf4e902c52006-08-17 18:14:52 -0700782 if (cfg->fc_mp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700784 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
785 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto failure;
Thomas Graf4e902c52006-08-17 18:14:52 -0700787 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto err_inval;
Thomas Graf4e902c52006-08-17 18:14:52 -0700789 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goto err_inval;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100791#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700792 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto err_inval;
794#endif
795#else
796 goto err_inval;
797#endif
798 } else {
799 struct fib_nh *nh = fi->fib_nh;
Thomas Graf4e902c52006-08-17 18:14:52 -0700800
801 nh->nh_oif = cfg->fc_oif;
802 nh->nh_gw = cfg->fc_gw;
803 nh->nh_flags = cfg->fc_flags;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100804#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700805 nh->nh_tclassid = cfg->fc_flow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807#ifdef CONFIG_IP_ROUTE_MULTIPATH
808 nh->nh_weight = 1;
809#endif
810 }
811
Thomas Graf4e902c52006-08-17 18:14:52 -0700812 if (fib_props[cfg->fc_type].error) {
813 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 goto err_inval;
815 goto link_it;
David S. Miller4c8237c2011-03-07 14:27:38 -0800816 } else {
817 switch (cfg->fc_type) {
818 case RTN_UNICAST:
819 case RTN_LOCAL:
820 case RTN_BROADCAST:
821 case RTN_ANYCAST:
822 case RTN_MULTICAST:
823 break;
824 default:
825 goto err_inval;
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
Thomas Graf4e902c52006-08-17 18:14:52 -0700829 if (cfg->fc_scope > RT_SCOPE_HOST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 goto err_inval;
831
Thomas Graf4e902c52006-08-17 18:14:52 -0700832 if (cfg->fc_scope == RT_SCOPE_HOST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 struct fib_nh *nh = fi->fib_nh;
834
835 /* Local address is added. */
836 if (nhs != 1 || nh->nh_gw)
837 goto err_inval;
838 nh->nh_scope = RT_SCOPE_NOWHERE;
Denis V. Lunev7462bd742008-01-31 18:49:32 -0800839 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 err = -ENODEV;
841 if (nh->nh_dev == NULL)
842 goto failure;
843 } else {
844 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000845 err = fib_check_nh(cfg, fi, nexthop_nh);
846 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto failure;
848 } endfor_nexthops(fi)
849 }
850
851 if (fi->fib_prefsrc) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700852 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
853 fi->fib_prefsrc != cfg->fc_dst)
Denis V. Lunev7462bd742008-01-31 18:49:32 -0800854 if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 goto err_inval;
856 }
857
David S. Miller1fc050a2011-03-07 20:54:48 -0800858 change_nexthops(fi) {
David S. Miller436c3b62011-03-24 17:42:21 -0700859 fib_info_update_nh_saddr(net, nexthop_nh);
David S. Miller1fc050a2011-03-07 20:54:48 -0800860 } endfor_nexthops(fi)
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862link_it:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000863 ofi = fib_find_info(fi);
864 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 fi->fib_dead = 1;
866 free_fib_info(fi);
867 ofi->fib_treeref++;
868 return ofi;
869 }
870
871 fi->fib_treeref++;
872 atomic_inc(&fi->fib_clntref);
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700873 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 hlist_add_head(&fi->fib_hash,
875 &fib_info_hash[fib_info_hashfn(fi)]);
876 if (fi->fib_prefsrc) {
877 struct hlist_head *head;
878
879 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
880 hlist_add_head(&fi->fib_lhash, head);
881 }
882 change_nexthops(fi) {
883 struct hlist_head *head;
884 unsigned int hash;
885
David S. Miller71fceff2010-01-15 01:16:40 -0800886 if (!nexthop_nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800888 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 head = &fib_info_devhash[hash];
David S. Miller71fceff2010-01-15 01:16:40 -0800890 hlist_add_head(&nexthop_nh->nh_hash, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 } endfor_nexthops(fi)
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700892 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return fi;
894
895err_inval:
896 err = -EINVAL;
897
898failure:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900899 if (fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 fi->fib_dead = 1;
901 free_fib_info(fi);
902 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700903
904 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Thomas Grafbe403ea2006-08-17 18:15:17 -0700907int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
David S. Miller37e826c2011-03-24 18:06:47 -0700908 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
Thomas Grafbe403ea2006-08-17 18:15:17 -0700909 struct fib_info *fi, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Thomas Grafbe403ea2006-08-17 18:15:17 -0700911 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Thomas Grafbe403ea2006-08-17 18:15:17 -0700914 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
915 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800916 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -0700917
918 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 rtm->rtm_family = AF_INET;
920 rtm->rtm_dst_len = dst_len;
921 rtm->rtm_src_len = 0;
922 rtm->rtm_tos = tos;
Krzysztof Piotr Oledzki709772e2008-06-10 15:44:49 -0700923 if (tb_id < 256)
924 rtm->rtm_table = tb_id;
925 else
926 rtm->rtm_table = RT_TABLE_COMPAT;
Thomas Grafbe403ea2006-08-17 18:15:17 -0700927 NLA_PUT_U32(skb, RTA_TABLE, tb_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 rtm->rtm_type = type;
929 rtm->rtm_flags = fi->fib_flags;
David S. Miller37e826c2011-03-24 18:06:47 -0700930 rtm->rtm_scope = fi->fib_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 rtm->rtm_protocol = fi->fib_protocol;
Thomas Grafbe403ea2006-08-17 18:15:17 -0700932
933 if (rtm->rtm_dst_len)
Al Viro17fb2c62006-09-26 22:15:25 -0700934 NLA_PUT_BE32(skb, RTA_DST, dst);
Thomas Grafbe403ea2006-08-17 18:15:17 -0700935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (fi->fib_priority)
Thomas Grafbe403ea2006-08-17 18:15:17 -0700937 NLA_PUT_U32(skb, RTA_PRIORITY, fi->fib_priority);
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -0700940 goto nla_put_failure;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (fi->fib_prefsrc)
Al Viro17fb2c62006-09-26 22:15:25 -0700943 NLA_PUT_BE32(skb, RTA_PREFSRC, fi->fib_prefsrc);
Thomas Grafbe403ea2006-08-17 18:15:17 -0700944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (fi->fib_nhs == 1) {
946 if (fi->fib_nh->nh_gw)
Al Viro17fb2c62006-09-26 22:15:25 -0700947 NLA_PUT_BE32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw);
Thomas Grafbe403ea2006-08-17 18:15:17 -0700948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (fi->fib_nh->nh_oif)
Thomas Grafbe403ea2006-08-17 18:15:17 -0700950 NLA_PUT_U32(skb, RTA_OIF, fi->fib_nh->nh_oif);
Patrick McHardyc7066f72011-01-14 13:36:42 +0100951#ifdef CONFIG_IP_ROUTE_CLASSID
Patrick McHardy8265abc2006-07-21 15:09:55 -0700952 if (fi->fib_nh[0].nh_tclassid)
Thomas Grafbe403ea2006-08-17 18:15:17 -0700953 NLA_PUT_U32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid);
Patrick McHardy8265abc2006-07-21 15:09:55 -0700954#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956#ifdef CONFIG_IP_ROUTE_MULTIPATH
957 if (fi->fib_nhs > 1) {
Thomas Grafbe403ea2006-08-17 18:15:17 -0700958 struct rtnexthop *rtnh;
959 struct nlattr *mp;
960
961 mp = nla_nest_start(skb, RTA_MULTIPATH);
962 if (mp == NULL)
963 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 for_nexthops(fi) {
Thomas Grafbe403ea2006-08-17 18:15:17 -0700966 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
967 if (rtnh == NULL)
968 goto nla_put_failure;
969
970 rtnh->rtnh_flags = nh->nh_flags & 0xFF;
971 rtnh->rtnh_hops = nh->nh_weight - 1;
972 rtnh->rtnh_ifindex = nh->nh_oif;
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (nh->nh_gw)
Al Viro17fb2c62006-09-26 22:15:25 -0700975 NLA_PUT_BE32(skb, RTA_GATEWAY, nh->nh_gw);
Patrick McHardyc7066f72011-01-14 13:36:42 +0100976#ifdef CONFIG_IP_ROUTE_CLASSID
Patrick McHardy8265abc2006-07-21 15:09:55 -0700977 if (nh->nh_tclassid)
Thomas Grafbe403ea2006-08-17 18:15:17 -0700978 NLA_PUT_U32(skb, RTA_FLOW, nh->nh_tclassid);
Patrick McHardy8265abc2006-07-21 15:09:55 -0700979#endif
Thomas Grafbe403ea2006-08-17 18:15:17 -0700980 /* length of rtnetlink header + attributes */
981 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 } endfor_nexthops(fi);
Thomas Grafbe403ea2006-08-17 18:15:17 -0700983
984 nla_nest_end(skb, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986#endif
Thomas Grafbe403ea2006-08-17 18:15:17 -0700987 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Thomas Grafbe403ea2006-08-17 18:15:17 -0700989nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800990 nlmsg_cancel(skb, nlh);
991 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000995 * Update FIB if:
996 * - local address disappeared -> we must delete all the entries
997 * referring to it.
998 * - device went down -> we must shutdown all nexthops going via it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001000int fib_sync_down_addr(struct net *net, __be32 local)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001001{
1002 int ret = 0;
1003 unsigned int hash = fib_laddr_hashfn(local);
1004 struct hlist_head *head = &fib_info_laddrhash[hash];
1005 struct hlist_node *node;
1006 struct fib_info *fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001008 if (fib_info_laddrhash == NULL || local == 0)
1009 return 0;
1010
1011 hlist_for_each_entry(fi, node, head, fib_lhash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001012 if (!net_eq(fi->fib_net, net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001013 continue;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001014 if (fi->fib_prefsrc == local) {
1015 fi->fib_flags |= RTNH_F_DEAD;
1016 ret++;
1017 }
1018 }
1019 return ret;
1020}
1021
1022int fib_sync_down_dev(struct net_device *dev, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 int ret = 0;
1025 int scope = RT_SCOPE_NOWHERE;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001026 struct fib_info *prev_fi = NULL;
1027 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1028 struct hlist_head *head = &fib_info_devhash[hash];
1029 struct hlist_node *node;
1030 struct fib_nh *nh;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (force)
1033 scope = -1;
1034
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001035 hlist_for_each_entry(nh, node, head, nh_hash) {
1036 struct fib_info *fi = nh->nh_parent;
1037 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001039 BUG_ON(!fi->fib_nhs);
1040 if (nh->nh_dev != dev || fi == prev_fi)
1041 continue;
1042 prev_fi = fi;
1043 dead = 0;
1044 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001045 if (nexthop_nh->nh_flags & RTNH_F_DEAD)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001046 dead++;
David S. Miller71fceff2010-01-15 01:16:40 -08001047 else if (nexthop_nh->nh_dev == dev &&
1048 nexthop_nh->nh_scope != scope) {
1049 nexthop_nh->nh_flags |= RTNH_F_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050#ifdef CONFIG_IP_ROUTE_MULTIPATH
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001051 spin_lock_bh(&fib_multipath_lock);
David S. Miller71fceff2010-01-15 01:16:40 -08001052 fi->fib_power -= nexthop_nh->nh_power;
1053 nexthop_nh->nh_power = 0;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001054 spin_unlock_bh(&fib_multipath_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055#endif
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001056 dead++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001058#ifdef CONFIG_IP_ROUTE_MULTIPATH
David S. Miller71fceff2010-01-15 01:16:40 -08001059 if (force > 1 && nexthop_nh->nh_dev == dev) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001060 dead = fi->fib_nhs;
1061 break;
1062 }
1063#endif
1064 } endfor_nexthops(fi)
1065 if (dead == fi->fib_nhs) {
1066 fi->fib_flags |= RTNH_F_DEAD;
1067 ret++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069 }
1070
1071 return ret;
1072}
1073
David S. Miller0c838ff2011-01-31 16:16:50 -08001074/* Must be invoked inside of an RCU protected region. */
1075void fib_select_default(struct fib_result *res)
1076{
1077 struct fib_info *fi = NULL, *last_resort = NULL;
1078 struct list_head *fa_head = res->fa_head;
1079 struct fib_table *tb = res->table;
1080 int order = -1, last_idx = -1;
1081 struct fib_alias *fa;
1082
1083 list_for_each_entry_rcu(fa, fa_head, fa_list) {
1084 struct fib_info *next_fi = fa->fa_info;
1085
David S. Miller37e826c2011-03-24 18:06:47 -07001086 if (next_fi->fib_scope != res->scope ||
David S. Miller0c838ff2011-01-31 16:16:50 -08001087 fa->fa_type != RTN_UNICAST)
1088 continue;
1089
1090 if (next_fi->fib_priority > res->fi->fib_priority)
1091 break;
1092 if (!next_fi->fib_nh[0].nh_gw ||
1093 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
1094 continue;
1095
1096 fib_alias_accessed(fa);
1097
1098 if (fi == NULL) {
1099 if (next_fi != res->fi)
1100 break;
1101 } else if (!fib_detect_death(fi, order, &last_resort,
1102 &last_idx, tb->tb_default)) {
1103 fib_result_assign(res, fi);
1104 tb->tb_default = order;
1105 goto out;
1106 }
1107 fi = next_fi;
1108 order++;
1109 }
1110
1111 if (order <= 0 || fi == NULL) {
1112 tb->tb_default = -1;
1113 goto out;
1114 }
1115
1116 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
1117 tb->tb_default)) {
1118 fib_result_assign(res, fi);
1119 tb->tb_default = order;
1120 goto out;
1121 }
1122
1123 if (last_idx >= 0)
1124 fib_result_assign(res, last_resort);
1125 tb->tb_default = last_idx;
1126out:
Eric Dumazet31d40932011-02-14 11:23:04 -08001127 return;
David S. Miller0c838ff2011-01-31 16:16:50 -08001128}
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130#ifdef CONFIG_IP_ROUTE_MULTIPATH
1131
1132/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001133 * Dead device goes up. We wake up dead nexthops.
1134 * It takes sense only on multipath routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136int fib_sync_up(struct net_device *dev)
1137{
1138 struct fib_info *prev_fi;
1139 unsigned int hash;
1140 struct hlist_head *head;
1141 struct hlist_node *node;
1142 struct fib_nh *nh;
1143 int ret;
1144
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001145 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return 0;
1147
1148 prev_fi = NULL;
1149 hash = fib_devindex_hashfn(dev->ifindex);
1150 head = &fib_info_devhash[hash];
1151 ret = 0;
1152
1153 hlist_for_each_entry(nh, node, head, nh_hash) {
1154 struct fib_info *fi = nh->nh_parent;
1155 int alive;
1156
1157 BUG_ON(!fi->fib_nhs);
1158 if (nh->nh_dev != dev || fi == prev_fi)
1159 continue;
1160
1161 prev_fi = fi;
1162 alive = 0;
1163 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001164 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 alive++;
1166 continue;
1167 }
David S. Miller71fceff2010-01-15 01:16:40 -08001168 if (nexthop_nh->nh_dev == NULL ||
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001169 !(nexthop_nh->nh_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 continue;
David S. Miller71fceff2010-01-15 01:16:40 -08001171 if (nexthop_nh->nh_dev != dev ||
1172 !__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 continue;
1174 alive++;
1175 spin_lock_bh(&fib_multipath_lock);
David S. Miller71fceff2010-01-15 01:16:40 -08001176 nexthop_nh->nh_power = 0;
1177 nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 spin_unlock_bh(&fib_multipath_lock);
1179 } endfor_nexthops(fi)
1180
1181 if (alive > 0) {
1182 fi->fib_flags &= ~RTNH_F_DEAD;
1183 ret++;
1184 }
1185 }
1186
1187 return ret;
1188}
1189
1190/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001191 * The algorithm is suboptimal, but it provides really
1192 * fair weighted route distribution.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 */
David S. Miller1b7fe5932011-03-10 17:01:16 -08001194void fib_select_multipath(struct fib_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 struct fib_info *fi = res->fi;
1197 int w;
1198
1199 spin_lock_bh(&fib_multipath_lock);
1200 if (fi->fib_power <= 0) {
1201 int power = 0;
1202 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001203 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
David S. Miller71fceff2010-01-15 01:16:40 -08001204 power += nexthop_nh->nh_weight;
1205 nexthop_nh->nh_power = nexthop_nh->nh_weight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207 } endfor_nexthops(fi);
1208 fi->fib_power = power;
1209 if (power <= 0) {
1210 spin_unlock_bh(&fib_multipath_lock);
1211 /* Race condition: route has just become dead. */
1212 res->nh_sel = 0;
1213 return;
1214 }
1215 }
1216
1217
1218 /* w should be random number [0..fi->fib_power-1],
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001219 * it is pretty bad approximation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 */
1221
1222 w = jiffies % fi->fib_power;
1223
1224 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001225 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
David S. Miller71fceff2010-01-15 01:16:40 -08001226 nexthop_nh->nh_power) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001227 w -= nexthop_nh->nh_power;
1228 if (w <= 0) {
David S. Miller71fceff2010-01-15 01:16:40 -08001229 nexthop_nh->nh_power--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 fi->fib_power--;
1231 res->nh_sel = nhsel;
1232 spin_unlock_bh(&fib_multipath_lock);
1233 return;
1234 }
1235 }
1236 } endfor_nexthops(fi);
1237
1238 /* Race condition: route has just become dead. */
1239 res->nh_sel = 0;
1240 spin_unlock_bh(&fib_multipath_lock);
1241}
1242#endif