blob: 83d0f42b619a439bacd59b6173a86e100524a457 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/bitops.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
20#include <linux/jiffies.h>
21#include <linux/mm.h>
22#include <linux/string.h>
23#include <linux/socket.h>
24#include <linux/sockios.h>
25#include <linux/errno.h>
26#include <linux/in.h>
27#include <linux/inet.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020028#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/netdevice.h>
30#include <linux/if_arp.h>
31#include <linux/proc_fs.h>
32#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020036#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <net/ip.h>
38#include <net/protocol.h>
39#include <net/route.h>
40#include <net/tcp.h>
41#include <net/sock.h>
42#include <net/ip_fib.h>
Thomas Graff21c7bc2006-08-15 00:34:17 -070043#include <net/netlink.h>
Thomas Graf4e902c52006-08-17 18:14:52 -070044#include <net/nexthop.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include "fib_lookup.h"
47
Stephen Hemminger832b4c52006-08-29 16:48:09 -070048static DEFINE_SPINLOCK(fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static struct hlist_head *fib_info_hash;
50static struct hlist_head *fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -080051static unsigned int fib_info_hash_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static unsigned int fib_info_cnt;
53
54#define DEVINDEX_HASHBITS 8
55#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
56static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
57
58#ifdef CONFIG_IP_ROUTE_MULTIPATH
59
60static DEFINE_SPINLOCK(fib_multipath_lock);
61
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000062#define for_nexthops(fi) { \
63 int nhsel; const struct fib_nh *nh; \
64 for (nhsel = 0, nh = (fi)->fib_nh; \
65 nhsel < (fi)->fib_nhs; \
66 nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000068#define change_nexthops(fi) { \
69 int nhsel; struct fib_nh *nexthop_nh; \
70 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
71 nhsel < (fi)->fib_nhs; \
72 nexthop_nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#else /* CONFIG_IP_ROUTE_MULTIPATH */
75
76/* Hope, that gcc will optimize it to get rid of dummy loop */
77
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000078#define for_nexthops(fi) { \
79 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
80 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000082#define change_nexthops(fi) { \
83 int nhsel; \
84 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
85 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#endif /* CONFIG_IP_ROUTE_MULTIPATH */
88
89#define endfor_nexthops(fi) }
90
91
David S. Miller3be06862011-03-07 15:01:10 -080092const struct fib_prop fib_props[RTN_MAX + 1] = {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000093 [RTN_UNSPEC] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .error = 0,
95 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000096 },
97 [RTN_UNICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 .error = 0,
99 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000100 },
101 [RTN_LOCAL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .error = 0,
103 .scope = RT_SCOPE_HOST,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000104 },
105 [RTN_BROADCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .error = 0,
107 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000108 },
109 [RTN_ANYCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 .error = 0,
111 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000112 },
113 [RTN_MULTICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .error = 0,
115 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000116 },
117 [RTN_BLACKHOLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .error = -EINVAL,
119 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000120 },
121 [RTN_UNREACHABLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .error = -EHOSTUNREACH,
123 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000124 },
125 [RTN_PROHIBIT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .error = -EACCES,
127 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000128 },
129 [RTN_THROW] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .error = -EAGAIN,
131 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000132 },
133 [RTN_NAT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .error = -EINVAL,
135 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000136 },
137 [RTN_XRESOLVE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .error = -EINVAL,
139 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000140 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
David S. Miller4895c772012-07-17 04:19:00 -0700143static void free_nh_exceptions(struct fib_nh *nh)
144{
145 struct fnhe_hash_bucket *hash = nh->nh_exceptions;
146 int i;
147
148 for (i = 0; i < FNHE_HASH_SIZE; i++) {
149 struct fib_nh_exception *fnhe;
150
Eric Dumazet5abf7f72012-07-17 22:42:13 +0200151 fnhe = rcu_dereference_protected(hash[i].chain, 1);
David S. Miller4895c772012-07-17 04:19:00 -0700152 while (fnhe) {
153 struct fib_nh_exception *next;
154
Eric Dumazet5abf7f72012-07-17 22:42:13 +0200155 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
David S. Miller4895c772012-07-17 04:19:00 -0700156 kfree(fnhe);
157
158 fnhe = next;
159 }
160 }
161 kfree(hash);
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/* Release a nexthop info record */
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000165static void free_fib_info_rcu(struct rcu_head *head)
166{
167 struct fib_info *fi = container_of(head, struct fib_info, rcu);
168
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000169 change_nexthops(fi) {
170 if (nexthop_nh->nh_dev)
171 dev_put(nexthop_nh->nh_dev);
David S. Miller4895c772012-07-17 04:19:00 -0700172 if (nexthop_nh->nh_exceptions)
173 free_nh_exceptions(nexthop_nh);
David S. Millerf2bb4be2012-07-17 12:20:47 -0700174 if (nexthop_nh->nh_rth_output)
175 dst_release(&nexthop_nh->nh_rth_output->dst);
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000176 } endfor_nexthops(fi);
177
178 release_net(fi->fib_net);
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000179 if (fi->fib_metrics != (u32 *) dst_default_metrics)
180 kfree(fi->fib_metrics);
181 kfree(fi);
182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184void free_fib_info(struct fib_info *fi)
185{
186 if (fi->fib_dead == 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000187 pr_warn("Freeing alive fib_info %p\n", fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return;
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 fib_info_cnt--;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700191#ifdef CONFIG_IP_ROUTE_CLASSID
192 change_nexthops(fi) {
193 if (nexthop_nh->nh_tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -0700194 fi->fib_net->ipv4.fib_num_tclassid_users--;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700195 } endfor_nexthops(fi);
196#endif
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000197 call_rcu(&fi->rcu, free_fib_info_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200void fib_release_info(struct fib_info *fi)
201{
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700202 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (fi && --fi->fib_treeref == 0) {
204 hlist_del(&fi->fib_hash);
205 if (fi->fib_prefsrc)
206 hlist_del(&fi->fib_lhash);
207 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -0800208 if (!nexthop_nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800210 hlist_del(&nexthop_nh->nh_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 } endfor_nexthops(fi)
212 fi->fib_dead = 1;
213 fib_info_put(fi);
214 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700215 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000218static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 const struct fib_nh *onh = ofi->fib_nh;
221
222 for_nexthops(fi) {
223 if (nh->nh_oif != onh->nh_oif ||
224 nh->nh_gw != onh->nh_gw ||
225 nh->nh_scope != onh->nh_scope ||
226#ifdef CONFIG_IP_ROUTE_MULTIPATH
227 nh->nh_weight != onh->nh_weight ||
228#endif
Patrick McHardyc7066f72011-01-14 13:36:42 +0100229#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 nh->nh_tclassid != onh->nh_tclassid ||
231#endif
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000232 ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -1;
234 onh++;
235 } endfor_nexthops(fi);
236 return 0;
237}
238
David S. Miller88ebc722008-01-12 21:49:01 -0800239static inline unsigned int fib_devindex_hashfn(unsigned int val)
240{
241 unsigned int mask = DEVINDEX_HASHSIZE - 1;
242
243 return (val ^
244 (val >> DEVINDEX_HASHBITS) ^
245 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
249{
David S. Miller123b9732011-02-01 15:34:21 -0800250 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned int val = fi->fib_nhs;
252
David S. Miller37e826c2011-03-24 18:06:47 -0700253 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
Al Viro81f7bf62006-09-27 18:40:00 -0700254 val ^= (__force u32)fi->fib_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 val ^= fi->fib_priority;
David S. Miller88ebc722008-01-12 21:49:01 -0800256 for_nexthops(fi) {
257 val ^= fib_devindex_hashfn(nh->nh_oif);
258 } endfor_nexthops(fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
261}
262
263static struct fib_info *fib_find_info(const struct fib_info *nfi)
264{
265 struct hlist_head *head;
266 struct hlist_node *node;
267 struct fib_info *fi;
268 unsigned int hash;
269
270 hash = fib_info_hashfn(nfi);
271 head = &fib_info_hash[hash];
272
273 hlist_for_each_entry(fi, node, head, fib_hash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800274 if (!net_eq(fi->fib_net, nfi->fib_net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -0800275 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (fi->fib_nhs != nfi->fib_nhs)
277 continue;
278 if (nfi->fib_protocol == fi->fib_protocol &&
David S. Miller37e826c2011-03-24 18:06:47 -0700279 nfi->fib_scope == fi->fib_scope &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 nfi->fib_prefsrc == fi->fib_prefsrc &&
281 nfi->fib_priority == fi->fib_priority &&
282 memcmp(nfi->fib_metrics, fi->fib_metrics,
Eric Dumazetfcd13f42011-03-24 07:01:24 +0000283 sizeof(u32) * RTAX_MAX) == 0 &&
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000284 ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
286 return fi;
287 }
288
289 return NULL;
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/* Check, that the gateway is already configured.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000293 * Used only by redirect accept routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
Al Virod878e72e2006-09-26 22:18:13 -0700295int ip_fib_check_default(__be32 gw, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct hlist_head *head;
298 struct hlist_node *node;
299 struct fib_nh *nh;
300 unsigned int hash;
301
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700302 spin_lock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 hash = fib_devindex_hashfn(dev->ifindex);
305 head = &fib_info_devhash[hash];
306 hlist_for_each_entry(nh, node, head, nh_hash) {
307 if (nh->nh_dev == dev &&
308 nh->nh_gw == gw &&
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000309 !(nh->nh_flags & RTNH_F_DEAD)) {
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700310 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return 0;
312 }
313 }
314
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700315 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 return -1;
318}
319
Thomas Graf339bf982006-11-10 14:10:15 -0800320static inline size_t fib_nlmsg_size(struct fib_info *fi)
321{
322 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
323 + nla_total_size(4) /* RTA_TABLE */
324 + nla_total_size(4) /* RTA_DST */
325 + nla_total_size(4) /* RTA_PRIORITY */
326 + nla_total_size(4); /* RTA_PREFSRC */
327
328 /* space for nested metrics */
329 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
330
331 if (fi->fib_nhs) {
332 /* Also handles the special case fib_nhs == 1 */
333
334 /* each nexthop is packed in an attribute */
335 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
336
337 /* may contain flow and gateway attribute */
338 nhsize += 2 * nla_total_size(4);
339
340 /* all nexthops are packed in a nested attribute */
341 payload += nla_total_size(fi->fib_nhs * nhsize);
342 }
343
344 return payload;
345}
346
Al Viro81f7bf62006-09-27 18:40:00 -0700347void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
Milan Kocianb8f55832007-05-23 14:55:06 -0700348 int dst_len, u32 tb_id, struct nl_info *info,
349 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct sk_buff *skb;
Thomas Graf4e902c52006-08-17 18:14:52 -0700352 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700353 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Thomas Graf339bf982006-11-10 14:10:15 -0800355 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
Thomas Graff21c7bc2006-08-15 00:34:17 -0700356 if (skb == NULL)
357 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Thomas Graf4e902c52006-08-17 18:14:52 -0700359 err = fib_dump_info(skb, info->pid, seq, event, tb_id,
David S. Miller37e826c2011-03-24 18:06:47 -0700360 fa->fa_type, key, dst_len,
Milan Kocianb8f55832007-05-23 14:55:06 -0700361 fa->fa_tos, fa->fa_info, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -0800362 if (err < 0) {
363 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
364 WARN_ON(err == -EMSGSIZE);
365 kfree_skb(skb);
366 goto errout;
367 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800368 rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
369 info->nlh, GFP_KERNEL);
370 return;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700371errout:
372 if (err < 0)
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800373 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376/* Return the first fib alias matching TOS with
377 * priority less than or equal to PRIO.
378 */
379struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
380{
381 if (fah) {
382 struct fib_alias *fa;
383 list_for_each_entry(fa, fah, fa_list) {
384 if (fa->fa_tos > tos)
385 continue;
386 if (fa->fa_info->fib_priority >= prio ||
387 fa->fa_tos < tos)
388 return fa;
389 }
390 }
391 return NULL;
392}
393
394int fib_detect_death(struct fib_info *fi, int order,
Denis V. Lunevc17860a2007-12-08 00:22:13 -0800395 struct fib_info **last_resort, int *last_idx, int dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 struct neighbour *n;
398 int state = NUD_NONE;
399
400 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
401 if (n) {
402 state = n->nud_state;
403 neigh_release(n);
404 }
Jianjun Kongd9319102008-11-03 00:23:42 -0800405 if (state == NUD_REACHABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000407 if ((state & NUD_VALID) && order != dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000409 if ((state & NUD_VALID) ||
410 (*last_idx < 0 && order > dflt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *last_resort = fi;
412 *last_idx = order;
413 }
414 return 1;
415}
416
417#ifdef CONFIG_IP_ROUTE_MULTIPATH
418
Thomas Graf4e902c52006-08-17 18:14:52 -0700419static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 int nhs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Thomas Graf4e902c52006-08-17 18:14:52 -0700423 while (rtnh_ok(rtnh, remaining)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 nhs++;
Thomas Graf4e902c52006-08-17 18:14:52 -0700425 rtnh = rtnh_next(rtnh, &remaining);
426 }
427
428 /* leftover implies invalid nexthop configuration, discard it */
429 return remaining > 0 ? 0 : nhs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Thomas Graf4e902c52006-08-17 18:14:52 -0700432static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
433 int remaining, struct fib_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 change_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700436 int attrlen;
437
438 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700440
David S. Miller71fceff2010-01-15 01:16:40 -0800441 nexthop_nh->nh_flags =
442 (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
443 nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
444 nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700445
446 attrlen = rtnh_attrlen(rtnh);
447 if (attrlen > 0) {
448 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
449
450 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David S. Miller71fceff2010-01-15 01:16:40 -0800451 nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100452#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700453 nla = nla_find(attrs, attrlen, RTA_FLOW);
David S. Miller71fceff2010-01-15 01:16:40 -0800454 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700455 if (nexthop_nh->nh_tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -0700456 fi->fib_net->ipv4.fib_num_tclassid_users++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#endif
458 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700459
460 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 } endfor_nexthops(fi);
Thomas Graf4e902c52006-08-17 18:14:52 -0700462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return 0;
464}
465
466#endif
467
Thomas Graf4e902c52006-08-17 18:14:52 -0700468int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700471 struct rtnexthop *rtnh;
472 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473#endif
474
Thomas Graf4e902c52006-08-17 18:14:52 -0700475 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return 1;
477
Thomas Graf4e902c52006-08-17 18:14:52 -0700478 if (cfg->fc_oif || cfg->fc_gw) {
479 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
480 (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return 0;
482 return 1;
483 }
484
485#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700486 if (cfg->fc_mp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700488
489 rtnh = cfg->fc_mp;
490 remaining = cfg->fc_mp_len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 for_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700493 int attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Thomas Graf4e902c52006-08-17 18:14:52 -0700495 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700497
498 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700500
501 attrlen = rtnh_attrlen(rtnh);
502 if (attrlen < 0) {
503 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
504
505 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
Al Viro17fb2c62006-09-26 22:15:25 -0700506 if (nla && nla_get_be32(nla) != nh->nh_gw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 1;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100508#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700509 nla = nla_find(attrs, attrlen, RTA_FLOW);
510 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return 1;
512#endif
513 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700514
515 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 } endfor_nexthops(fi);
517#endif
518 return 0;
519}
520
521
522/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000523 * Picture
524 * -------
525 *
526 * Semantics of nexthop is very messy by historical reasons.
527 * We have to take into account, that:
528 * a) gateway can be actually local interface address,
529 * so that gatewayed route is direct.
530 * b) gateway must be on-link address, possibly
531 * described not by an ifaddr, but also by a direct route.
532 * c) If both gateway and interface are specified, they should not
533 * contradict.
534 * d) If we use tunnel routes, gateway could be not on-link.
535 *
536 * Attempt to reconcile all of these (alas, self-contradictory) conditions
537 * results in pretty ugly and hairy code with obscure logic.
538 *
539 * I chose to generalized it instead, so that the size
540 * of code does not increase practically, but it becomes
541 * much more general.
542 * Every prefix is assigned a "scope" value: "host" is local address,
543 * "link" is direct route,
544 * [ ... "site" ... "interior" ... ]
545 * and "universe" is true gateway route with global meaning.
546 *
547 * Every prefix refers to a set of "nexthop"s (gw, oif),
548 * where gw must have narrower scope. This recursion stops
549 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
550 * which means that gw is forced to be on link.
551 *
552 * Code is still hairy, but now it is apparently logically
553 * consistent and very flexible. F.e. as by-product it allows
554 * to co-exists in peace independent exterior and interior
555 * routing processes.
556 *
557 * Normally it looks as following.
558 *
559 * {universe prefix} -> (gw, oif) [scope link]
560 * |
561 * |-> {link prefix} -> (gw, oif) [scope local]
562 * |
563 * |-> {local prefix} (terminal node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Thomas Graf4e902c52006-08-17 18:14:52 -0700565static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
566 struct fib_nh *nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 int err;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800569 struct net *net;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000570 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Denis V. Lunev86167a32008-01-21 17:34:00 -0800572 net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (nh->nh_gw) {
574 struct fib_result res;
575
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000576 if (nh->nh_flags & RTNH_F_ONLINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Thomas Graf4e902c52006-08-17 18:14:52 -0700578 if (cfg->fc_scope >= RT_SCOPE_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return -EINVAL;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800580 if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return -EINVAL;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000582 dev = __dev_get_by_index(net, nh->nh_oif);
583 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return -ENODEV;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000585 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return -ENETDOWN;
587 nh->nh_dev = dev;
588 dev_hold(dev);
589 nh->nh_scope = RT_SCOPE_LINK;
590 return 0;
591 }
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000592 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 {
David S. Miller9ade2282011-03-12 02:02:42 -0500594 struct flowi4 fl4 = {
595 .daddr = nh->nh_gw,
596 .flowi4_scope = cfg->fc_scope + 1,
597 .flowi4_oif = nh->nh_oif,
Thomas Graf4e902c52006-08-17 18:14:52 -0700598 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* It is not necessary, but requires a bit of thinking */
David S. Miller9ade2282011-03-12 02:02:42 -0500601 if (fl4.flowi4_scope < RT_SCOPE_LINK)
602 fl4.flowi4_scope = RT_SCOPE_LINK;
603 err = fib_lookup(net, &fl4, &res);
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000604 if (err) {
605 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return err;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609 err = -EINVAL;
610 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
611 goto out;
612 nh->nh_scope = res.scope;
613 nh->nh_oif = FIB_RES_OIF(res);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000614 nh->nh_dev = dev = FIB_RES_DEV(res);
615 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto out;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000617 dev_hold(dev);
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000618 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 } else {
620 struct in_device *in_dev;
621
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000622 if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EINVAL;
624
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000625 rcu_read_lock();
626 err = -ENODEV;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800627 in_dev = inetdev_by_index(net, nh->nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (in_dev == NULL)
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000629 goto out;
630 err = -ENETDOWN;
631 if (!(in_dev->dev->flags & IFF_UP))
632 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 nh->nh_dev = in_dev->dev;
634 dev_hold(nh->nh_dev);
635 nh->nh_scope = RT_SCOPE_HOST;
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000636 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000638out:
639 rcu_read_unlock();
640 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Al Viro81f7bf62006-09-27 18:40:00 -0700643static inline unsigned int fib_laddr_hashfn(__be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
David S. Miller123b9732011-02-01 15:34:21 -0800645 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000647 return ((__force u32)val ^
648 ((__force u32)val >> 7) ^
649 ((__force u32)val >> 14)) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
David S. Miller123b9732011-02-01 15:34:21 -0800652static struct hlist_head *fib_info_hash_alloc(int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 if (bytes <= PAGE_SIZE)
Joonwoo Park88f83492007-11-26 23:29:32 +0800655 return kzalloc(bytes, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 else
657 return (struct hlist_head *)
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000658 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
659 get_order(bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
David S. Miller123b9732011-02-01 15:34:21 -0800662static void fib_info_hash_free(struct hlist_head *hash, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 if (!hash)
665 return;
666
667 if (bytes <= PAGE_SIZE)
668 kfree(hash);
669 else
670 free_pages((unsigned long) hash, get_order(bytes));
671}
672
David S. Miller123b9732011-02-01 15:34:21 -0800673static void fib_info_hash_move(struct hlist_head *new_info_hash,
674 struct hlist_head *new_laddrhash,
675 unsigned int new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
David S. Millerb7656e72005-08-05 04:12:48 -0700677 struct hlist_head *old_info_hash, *old_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -0800678 unsigned int old_size = fib_info_hash_size;
David S. Millerb7656e72005-08-05 04:12:48 -0700679 unsigned int i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700681 spin_lock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -0700682 old_info_hash = fib_info_hash;
683 old_laddrhash = fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -0800684 fib_info_hash_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 for (i = 0; i < old_size; i++) {
687 struct hlist_head *head = &fib_info_hash[i];
688 struct hlist_node *node, *n;
689 struct fib_info *fi;
690
691 hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
692 struct hlist_head *dest;
693 unsigned int new_hash;
694
695 hlist_del(&fi->fib_hash);
696
697 new_hash = fib_info_hashfn(fi);
698 dest = &new_info_hash[new_hash];
699 hlist_add_head(&fi->fib_hash, dest);
700 }
701 }
702 fib_info_hash = new_info_hash;
703
704 for (i = 0; i < old_size; i++) {
705 struct hlist_head *lhead = &fib_info_laddrhash[i];
706 struct hlist_node *node, *n;
707 struct fib_info *fi;
708
709 hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
710 struct hlist_head *ldest;
711 unsigned int new_hash;
712
713 hlist_del(&fi->fib_lhash);
714
715 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
716 ldest = &new_laddrhash[new_hash];
717 hlist_add_head(&fi->fib_lhash, ldest);
718 }
719 }
720 fib_info_laddrhash = new_laddrhash;
721
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700722 spin_unlock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -0700723
724 bytes = old_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -0800725 fib_info_hash_free(old_info_hash, bytes);
726 fib_info_hash_free(old_laddrhash, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
David S. Miller436c3b62011-03-24 17:42:21 -0700729__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
730{
731 nh->nh_saddr = inet_select_addr(nh->nh_dev,
732 nh->nh_gw,
David S. Miller37e826c2011-03-24 18:06:47 -0700733 nh->nh_parent->fib_scope);
David S. Miller436c3b62011-03-24 17:42:21 -0700734 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
735
736 return nh->nh_saddr;
737}
738
Thomas Graf4e902c52006-08-17 18:14:52 -0700739struct fib_info *fib_create_info(struct fib_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 int err;
742 struct fib_info *fi = NULL;
743 struct fib_info *ofi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 int nhs = 1;
Denis V. Lunev7462bd742008-01-31 18:49:32 -0800745 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
David S. Miller4c8237c2011-03-07 14:27:38 -0800747 if (cfg->fc_type > RTN_MAX)
748 goto err_inval;
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* Fast check to catch the most weird cases */
Thomas Graf4e902c52006-08-17 18:14:52 -0700751 if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 goto err_inval;
753
754#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700755 if (cfg->fc_mp) {
756 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (nhs == 0)
758 goto err_inval;
759 }
760#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 err = -ENOBUFS;
David S. Miller123b9732011-02-01 15:34:21 -0800763 if (fib_info_cnt >= fib_info_hash_size) {
764 unsigned int new_size = fib_info_hash_size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct hlist_head *new_info_hash;
766 struct hlist_head *new_laddrhash;
767 unsigned int bytes;
768
769 if (!new_size)
770 new_size = 1;
771 bytes = new_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -0800772 new_info_hash = fib_info_hash_alloc(bytes);
773 new_laddrhash = fib_info_hash_alloc(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (!new_info_hash || !new_laddrhash) {
David S. Miller123b9732011-02-01 15:34:21 -0800775 fib_info_hash_free(new_info_hash, bytes);
776 fib_info_hash_free(new_laddrhash, bytes);
Joonwoo Park88f83492007-11-26 23:29:32 +0800777 } else
David S. Miller123b9732011-02-01 15:34:21 -0800778 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
David S. Miller123b9732011-02-01 15:34:21 -0800780 if (!fib_info_hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 goto failure;
782 }
783
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700784 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (fi == NULL)
786 goto failure;
David S. Miller725d1e12011-01-28 14:05:05 -0800787 if (cfg->fc_mx) {
788 fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
789 if (!fi->fib_metrics)
790 goto failure;
791 } else
792 fi->fib_metrics = (u32 *) dst_default_metrics;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 fib_info_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Denis V. Lunev57d7a602008-04-16 02:00:50 -0700795 fi->fib_net = hold_net(net);
Thomas Graf4e902c52006-08-17 18:14:52 -0700796 fi->fib_protocol = cfg->fc_protocol;
David S. Miller37e826c2011-03-24 18:06:47 -0700797 fi->fib_scope = cfg->fc_scope;
Thomas Graf4e902c52006-08-17 18:14:52 -0700798 fi->fib_flags = cfg->fc_flags;
799 fi->fib_priority = cfg->fc_priority;
800 fi->fib_prefsrc = cfg->fc_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 fi->fib_nhs = nhs;
803 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -0800804 nexthop_nh->nh_parent = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 } endfor_nexthops(fi)
806
Thomas Graf4e902c52006-08-17 18:14:52 -0700807 if (cfg->fc_mx) {
808 struct nlattr *nla;
809 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Thomas Graf4e902c52006-08-17 18:14:52 -0700811 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200812 int type = nla_type(nla);
Thomas Graf4e902c52006-08-17 18:14:52 -0700813
814 if (type) {
David S. Miller6fac2622012-06-17 19:47:34 -0700815 u32 val;
816
Thomas Graf4e902c52006-08-17 18:14:52 -0700817 if (type > RTAX_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto err_inval;
David S. Miller6fac2622012-06-17 19:47:34 -0700819 val = nla_get_u32(nla);
820 if (type == RTAX_ADVMSS && val > 65535 - 40)
821 val = 65535 - 40;
David S. Miller710ab6c2012-07-10 07:02:09 -0700822 if (type == RTAX_MTU && val > 65535 - 15)
823 val = 65535 - 15;
David S. Miller6fac2622012-06-17 19:47:34 -0700824 fi->fib_metrics[type - 1] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Thomas Graf4e902c52006-08-17 18:14:52 -0700829 if (cfg->fc_mp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700831 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
832 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 goto failure;
Thomas Graf4e902c52006-08-17 18:14:52 -0700834 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 goto err_inval;
Thomas Graf4e902c52006-08-17 18:14:52 -0700836 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto err_inval;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100838#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700839 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 goto err_inval;
841#endif
842#else
843 goto err_inval;
844#endif
845 } else {
846 struct fib_nh *nh = fi->fib_nh;
Thomas Graf4e902c52006-08-17 18:14:52 -0700847
848 nh->nh_oif = cfg->fc_oif;
849 nh->nh_gw = cfg->fc_gw;
850 nh->nh_flags = cfg->fc_flags;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100851#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700852 nh->nh_tclassid = cfg->fc_flow;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700853 if (nh->nh_tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -0700854 fi->fib_net->ipv4.fib_num_tclassid_users++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856#ifdef CONFIG_IP_ROUTE_MULTIPATH
857 nh->nh_weight = 1;
858#endif
859 }
860
Thomas Graf4e902c52006-08-17 18:14:52 -0700861 if (fib_props[cfg->fc_type].error) {
862 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 goto err_inval;
864 goto link_it;
David S. Miller4c8237c2011-03-07 14:27:38 -0800865 } else {
866 switch (cfg->fc_type) {
867 case RTN_UNICAST:
868 case RTN_LOCAL:
869 case RTN_BROADCAST:
870 case RTN_ANYCAST:
871 case RTN_MULTICAST:
872 break;
873 default:
874 goto err_inval;
875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
Thomas Graf4e902c52006-08-17 18:14:52 -0700878 if (cfg->fc_scope > RT_SCOPE_HOST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 goto err_inval;
880
Thomas Graf4e902c52006-08-17 18:14:52 -0700881 if (cfg->fc_scope == RT_SCOPE_HOST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct fib_nh *nh = fi->fib_nh;
883
884 /* Local address is added. */
885 if (nhs != 1 || nh->nh_gw)
886 goto err_inval;
887 nh->nh_scope = RT_SCOPE_NOWHERE;
Denis V. Lunev7462bd742008-01-31 18:49:32 -0800888 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 err = -ENODEV;
890 if (nh->nh_dev == NULL)
891 goto failure;
892 } else {
893 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000894 err = fib_check_nh(cfg, fi, nexthop_nh);
895 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 goto failure;
897 } endfor_nexthops(fi)
898 }
899
900 if (fi->fib_prefsrc) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700901 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
902 fi->fib_prefsrc != cfg->fc_dst)
Denis V. Lunev7462bd742008-01-31 18:49:32 -0800903 if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 goto err_inval;
905 }
906
David S. Miller1fc050a2011-03-07 20:54:48 -0800907 change_nexthops(fi) {
David S. Miller436c3b62011-03-24 17:42:21 -0700908 fib_info_update_nh_saddr(net, nexthop_nh);
David S. Miller1fc050a2011-03-07 20:54:48 -0800909 } endfor_nexthops(fi)
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911link_it:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000912 ofi = fib_find_info(fi);
913 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 fi->fib_dead = 1;
915 free_fib_info(fi);
916 ofi->fib_treeref++;
917 return ofi;
918 }
919
920 fi->fib_treeref++;
921 atomic_inc(&fi->fib_clntref);
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700922 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 hlist_add_head(&fi->fib_hash,
924 &fib_info_hash[fib_info_hashfn(fi)]);
925 if (fi->fib_prefsrc) {
926 struct hlist_head *head;
927
928 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
929 hlist_add_head(&fi->fib_lhash, head);
930 }
931 change_nexthops(fi) {
932 struct hlist_head *head;
933 unsigned int hash;
934
David S. Miller71fceff2010-01-15 01:16:40 -0800935 if (!nexthop_nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800937 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 head = &fib_info_devhash[hash];
David S. Miller71fceff2010-01-15 01:16:40 -0800939 hlist_add_head(&nexthop_nh->nh_hash, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 } endfor_nexthops(fi)
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700941 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return fi;
943
944err_inval:
945 err = -EINVAL;
946
947failure:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900948 if (fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 fi->fib_dead = 1;
950 free_fib_info(fi);
951 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700952
953 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
Thomas Grafbe403ea2006-08-17 18:15:17 -0700956int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
David S. Miller37e826c2011-03-24 18:06:47 -0700957 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
Thomas Grafbe403ea2006-08-17 18:15:17 -0700958 struct fib_info *fi, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Thomas Grafbe403ea2006-08-17 18:15:17 -0700960 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Thomas Grafbe403ea2006-08-17 18:15:17 -0700963 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
964 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800965 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -0700966
967 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 rtm->rtm_family = AF_INET;
969 rtm->rtm_dst_len = dst_len;
970 rtm->rtm_src_len = 0;
971 rtm->rtm_tos = tos;
Krzysztof Piotr Oledzki709772e2008-06-10 15:44:49 -0700972 if (tb_id < 256)
973 rtm->rtm_table = tb_id;
974 else
975 rtm->rtm_table = RT_TABLE_COMPAT;
David S. Millerf3756b72012-04-01 20:39:02 -0400976 if (nla_put_u32(skb, RTA_TABLE, tb_id))
977 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 rtm->rtm_type = type;
979 rtm->rtm_flags = fi->fib_flags;
David S. Miller37e826c2011-03-24 18:06:47 -0700980 rtm->rtm_scope = fi->fib_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 rtm->rtm_protocol = fi->fib_protocol;
Thomas Grafbe403ea2006-08-17 18:15:17 -0700982
David S. Millerf3756b72012-04-01 20:39:02 -0400983 if (rtm->rtm_dst_len &&
984 nla_put_be32(skb, RTA_DST, dst))
985 goto nla_put_failure;
986 if (fi->fib_priority &&
987 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
988 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -0700990 goto nla_put_failure;
991
David S. Millerf3756b72012-04-01 20:39:02 -0400992 if (fi->fib_prefsrc &&
993 nla_put_be32(skb, RTA_PREFSRC, fi->fib_prefsrc))
994 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (fi->fib_nhs == 1) {
David S. Millerf3756b72012-04-01 20:39:02 -0400996 if (fi->fib_nh->nh_gw &&
997 nla_put_be32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
998 goto nla_put_failure;
999 if (fi->fib_nh->nh_oif &&
1000 nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
1001 goto nla_put_failure;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001002#ifdef CONFIG_IP_ROUTE_CLASSID
David S. Millerf3756b72012-04-01 20:39:02 -04001003 if (fi->fib_nh[0].nh_tclassid &&
1004 nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
1005 goto nla_put_failure;
Patrick McHardy8265abc2006-07-21 15:09:55 -07001006#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008#ifdef CONFIG_IP_ROUTE_MULTIPATH
1009 if (fi->fib_nhs > 1) {
Thomas Grafbe403ea2006-08-17 18:15:17 -07001010 struct rtnexthop *rtnh;
1011 struct nlattr *mp;
1012
1013 mp = nla_nest_start(skb, RTA_MULTIPATH);
1014 if (mp == NULL)
1015 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 for_nexthops(fi) {
Thomas Grafbe403ea2006-08-17 18:15:17 -07001018 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1019 if (rtnh == NULL)
1020 goto nla_put_failure;
1021
1022 rtnh->rtnh_flags = nh->nh_flags & 0xFF;
1023 rtnh->rtnh_hops = nh->nh_weight - 1;
1024 rtnh->rtnh_ifindex = nh->nh_oif;
1025
David S. Millerf3756b72012-04-01 20:39:02 -04001026 if (nh->nh_gw &&
1027 nla_put_be32(skb, RTA_GATEWAY, nh->nh_gw))
1028 goto nla_put_failure;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001029#ifdef CONFIG_IP_ROUTE_CLASSID
David S. Millerf3756b72012-04-01 20:39:02 -04001030 if (nh->nh_tclassid &&
1031 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1032 goto nla_put_failure;
Patrick McHardy8265abc2006-07-21 15:09:55 -07001033#endif
Thomas Grafbe403ea2006-08-17 18:15:17 -07001034 /* length of rtnetlink header + attributes */
1035 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 } endfor_nexthops(fi);
Thomas Grafbe403ea2006-08-17 18:15:17 -07001037
1038 nla_nest_end(skb, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040#endif
Thomas Grafbe403ea2006-08-17 18:15:17 -07001041 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Thomas Grafbe403ea2006-08-17 18:15:17 -07001043nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001044 nlmsg_cancel(skb, nlh);
1045 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001049 * Update FIB if:
1050 * - local address disappeared -> we must delete all the entries
1051 * referring to it.
1052 * - device went down -> we must shutdown all nexthops going via it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001054int fib_sync_down_addr(struct net *net, __be32 local)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001055{
1056 int ret = 0;
1057 unsigned int hash = fib_laddr_hashfn(local);
1058 struct hlist_head *head = &fib_info_laddrhash[hash];
1059 struct hlist_node *node;
1060 struct fib_info *fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001062 if (fib_info_laddrhash == NULL || local == 0)
1063 return 0;
1064
1065 hlist_for_each_entry(fi, node, head, fib_lhash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001066 if (!net_eq(fi->fib_net, net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001067 continue;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001068 if (fi->fib_prefsrc == local) {
1069 fi->fib_flags |= RTNH_F_DEAD;
1070 ret++;
1071 }
1072 }
1073 return ret;
1074}
1075
1076int fib_sync_down_dev(struct net_device *dev, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 int ret = 0;
1079 int scope = RT_SCOPE_NOWHERE;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001080 struct fib_info *prev_fi = NULL;
1081 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1082 struct hlist_head *head = &fib_info_devhash[hash];
1083 struct hlist_node *node;
1084 struct fib_nh *nh;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (force)
1087 scope = -1;
1088
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001089 hlist_for_each_entry(nh, node, head, nh_hash) {
1090 struct fib_info *fi = nh->nh_parent;
1091 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001093 BUG_ON(!fi->fib_nhs);
1094 if (nh->nh_dev != dev || fi == prev_fi)
1095 continue;
1096 prev_fi = fi;
1097 dead = 0;
1098 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001099 if (nexthop_nh->nh_flags & RTNH_F_DEAD)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001100 dead++;
David S. Miller71fceff2010-01-15 01:16:40 -08001101 else if (nexthop_nh->nh_dev == dev &&
1102 nexthop_nh->nh_scope != scope) {
1103 nexthop_nh->nh_flags |= RTNH_F_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104#ifdef CONFIG_IP_ROUTE_MULTIPATH
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001105 spin_lock_bh(&fib_multipath_lock);
David S. Miller71fceff2010-01-15 01:16:40 -08001106 fi->fib_power -= nexthop_nh->nh_power;
1107 nexthop_nh->nh_power = 0;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001108 spin_unlock_bh(&fib_multipath_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109#endif
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001110 dead++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001112#ifdef CONFIG_IP_ROUTE_MULTIPATH
David S. Miller71fceff2010-01-15 01:16:40 -08001113 if (force > 1 && nexthop_nh->nh_dev == dev) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001114 dead = fi->fib_nhs;
1115 break;
1116 }
1117#endif
1118 } endfor_nexthops(fi)
1119 if (dead == fi->fib_nhs) {
1120 fi->fib_flags |= RTNH_F_DEAD;
1121 ret++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123 }
1124
1125 return ret;
1126}
1127
David S. Miller0c838ff2011-01-31 16:16:50 -08001128/* Must be invoked inside of an RCU protected region. */
1129void fib_select_default(struct fib_result *res)
1130{
1131 struct fib_info *fi = NULL, *last_resort = NULL;
1132 struct list_head *fa_head = res->fa_head;
1133 struct fib_table *tb = res->table;
1134 int order = -1, last_idx = -1;
1135 struct fib_alias *fa;
1136
1137 list_for_each_entry_rcu(fa, fa_head, fa_list) {
1138 struct fib_info *next_fi = fa->fa_info;
1139
David S. Miller37e826c2011-03-24 18:06:47 -07001140 if (next_fi->fib_scope != res->scope ||
David S. Miller0c838ff2011-01-31 16:16:50 -08001141 fa->fa_type != RTN_UNICAST)
1142 continue;
1143
1144 if (next_fi->fib_priority > res->fi->fib_priority)
1145 break;
1146 if (!next_fi->fib_nh[0].nh_gw ||
1147 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
1148 continue;
1149
1150 fib_alias_accessed(fa);
1151
1152 if (fi == NULL) {
1153 if (next_fi != res->fi)
1154 break;
1155 } else if (!fib_detect_death(fi, order, &last_resort,
1156 &last_idx, tb->tb_default)) {
1157 fib_result_assign(res, fi);
1158 tb->tb_default = order;
1159 goto out;
1160 }
1161 fi = next_fi;
1162 order++;
1163 }
1164
1165 if (order <= 0 || fi == NULL) {
1166 tb->tb_default = -1;
1167 goto out;
1168 }
1169
1170 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
1171 tb->tb_default)) {
1172 fib_result_assign(res, fi);
1173 tb->tb_default = order;
1174 goto out;
1175 }
1176
1177 if (last_idx >= 0)
1178 fib_result_assign(res, last_resort);
1179 tb->tb_default = last_idx;
1180out:
Eric Dumazet31d40932011-02-14 11:23:04 -08001181 return;
David S. Miller0c838ff2011-01-31 16:16:50 -08001182}
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184#ifdef CONFIG_IP_ROUTE_MULTIPATH
1185
1186/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001187 * Dead device goes up. We wake up dead nexthops.
1188 * It takes sense only on multipath routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190int fib_sync_up(struct net_device *dev)
1191{
1192 struct fib_info *prev_fi;
1193 unsigned int hash;
1194 struct hlist_head *head;
1195 struct hlist_node *node;
1196 struct fib_nh *nh;
1197 int ret;
1198
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001199 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0;
1201
1202 prev_fi = NULL;
1203 hash = fib_devindex_hashfn(dev->ifindex);
1204 head = &fib_info_devhash[hash];
1205 ret = 0;
1206
1207 hlist_for_each_entry(nh, node, head, nh_hash) {
1208 struct fib_info *fi = nh->nh_parent;
1209 int alive;
1210
1211 BUG_ON(!fi->fib_nhs);
1212 if (nh->nh_dev != dev || fi == prev_fi)
1213 continue;
1214
1215 prev_fi = fi;
1216 alive = 0;
1217 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001218 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 alive++;
1220 continue;
1221 }
David S. Miller71fceff2010-01-15 01:16:40 -08001222 if (nexthop_nh->nh_dev == NULL ||
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001223 !(nexthop_nh->nh_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 continue;
David S. Miller71fceff2010-01-15 01:16:40 -08001225 if (nexthop_nh->nh_dev != dev ||
1226 !__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 continue;
1228 alive++;
1229 spin_lock_bh(&fib_multipath_lock);
David S. Miller71fceff2010-01-15 01:16:40 -08001230 nexthop_nh->nh_power = 0;
1231 nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 spin_unlock_bh(&fib_multipath_lock);
1233 } endfor_nexthops(fi)
1234
1235 if (alive > 0) {
1236 fi->fib_flags &= ~RTNH_F_DEAD;
1237 ret++;
1238 }
1239 }
1240
1241 return ret;
1242}
1243
1244/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001245 * The algorithm is suboptimal, but it provides really
1246 * fair weighted route distribution.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 */
David S. Miller1b7fe5932011-03-10 17:01:16 -08001248void fib_select_multipath(struct fib_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
1250 struct fib_info *fi = res->fi;
1251 int w;
1252
1253 spin_lock_bh(&fib_multipath_lock);
1254 if (fi->fib_power <= 0) {
1255 int power = 0;
1256 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001257 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
David S. Miller71fceff2010-01-15 01:16:40 -08001258 power += nexthop_nh->nh_weight;
1259 nexthop_nh->nh_power = nexthop_nh->nh_weight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261 } endfor_nexthops(fi);
1262 fi->fib_power = power;
1263 if (power <= 0) {
1264 spin_unlock_bh(&fib_multipath_lock);
1265 /* Race condition: route has just become dead. */
1266 res->nh_sel = 0;
1267 return;
1268 }
1269 }
1270
1271
1272 /* w should be random number [0..fi->fib_power-1],
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001273 * it is pretty bad approximation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 */
1275
1276 w = jiffies % fi->fib_power;
1277
1278 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001279 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
David S. Miller71fceff2010-01-15 01:16:40 -08001280 nexthop_nh->nh_power) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001281 w -= nexthop_nh->nh_power;
1282 if (w <= 0) {
David S. Miller71fceff2010-01-15 01:16:40 -08001283 nexthop_nh->nh_power--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 fi->fib_power--;
1285 res->nh_sel = nhsel;
1286 spin_unlock_bh(&fib_multipath_lock);
1287 return;
1288 }
1289 }
1290 } endfor_nexthops(fi);
1291
1292 /* Race condition: route has just become dead. */
1293 res->nh_sel = 0;
1294 spin_unlock_bh(&fib_multipath_lock);
1295}
1296#endif