blob: 2b57d768240d9499db471e7d889f422652b84245 [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);
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000174 } endfor_nexthops(fi);
175
176 release_net(fi->fib_net);
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000177 if (fi->fib_metrics != (u32 *) dst_default_metrics)
178 kfree(fi->fib_metrics);
179 kfree(fi);
180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182void free_fib_info(struct fib_info *fi)
183{
184 if (fi->fib_dead == 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000185 pr_warn("Freeing alive fib_info %p\n", fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return;
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 fib_info_cnt--;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700189#ifdef CONFIG_IP_ROUTE_CLASSID
190 change_nexthops(fi) {
191 if (nexthop_nh->nh_tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -0700192 fi->fib_net->ipv4.fib_num_tclassid_users--;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700193 } endfor_nexthops(fi);
194#endif
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000195 call_rcu(&fi->rcu, free_fib_info_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198void fib_release_info(struct fib_info *fi)
199{
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700200 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (fi && --fi->fib_treeref == 0) {
202 hlist_del(&fi->fib_hash);
203 if (fi->fib_prefsrc)
204 hlist_del(&fi->fib_lhash);
205 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -0800206 if (!nexthop_nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800208 hlist_del(&nexthop_nh->nh_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 } endfor_nexthops(fi)
210 fi->fib_dead = 1;
211 fib_info_put(fi);
212 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700213 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000216static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 const struct fib_nh *onh = ofi->fib_nh;
219
220 for_nexthops(fi) {
221 if (nh->nh_oif != onh->nh_oif ||
222 nh->nh_gw != onh->nh_gw ||
223 nh->nh_scope != onh->nh_scope ||
224#ifdef CONFIG_IP_ROUTE_MULTIPATH
225 nh->nh_weight != onh->nh_weight ||
226#endif
Patrick McHardyc7066f72011-01-14 13:36:42 +0100227#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 nh->nh_tclassid != onh->nh_tclassid ||
229#endif
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000230 ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -1;
232 onh++;
233 } endfor_nexthops(fi);
234 return 0;
235}
236
David S. Miller88ebc722008-01-12 21:49:01 -0800237static inline unsigned int fib_devindex_hashfn(unsigned int val)
238{
239 unsigned int mask = DEVINDEX_HASHSIZE - 1;
240
241 return (val ^
242 (val >> DEVINDEX_HASHBITS) ^
243 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
247{
David S. Miller123b9732011-02-01 15:34:21 -0800248 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 unsigned int val = fi->fib_nhs;
250
David S. Miller37e826c2011-03-24 18:06:47 -0700251 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
Al Viro81f7bf62006-09-27 18:40:00 -0700252 val ^= (__force u32)fi->fib_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 val ^= fi->fib_priority;
David S. Miller88ebc722008-01-12 21:49:01 -0800254 for_nexthops(fi) {
255 val ^= fib_devindex_hashfn(nh->nh_oif);
256 } endfor_nexthops(fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
259}
260
261static struct fib_info *fib_find_info(const struct fib_info *nfi)
262{
263 struct hlist_head *head;
264 struct hlist_node *node;
265 struct fib_info *fi;
266 unsigned int hash;
267
268 hash = fib_info_hashfn(nfi);
269 head = &fib_info_hash[hash];
270
271 hlist_for_each_entry(fi, node, head, fib_hash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800272 if (!net_eq(fi->fib_net, nfi->fib_net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -0800273 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (fi->fib_nhs != nfi->fib_nhs)
275 continue;
276 if (nfi->fib_protocol == fi->fib_protocol &&
David S. Miller37e826c2011-03-24 18:06:47 -0700277 nfi->fib_scope == fi->fib_scope &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 nfi->fib_prefsrc == fi->fib_prefsrc &&
279 nfi->fib_priority == fi->fib_priority &&
280 memcmp(nfi->fib_metrics, fi->fib_metrics,
Eric Dumazetfcd13f42011-03-24 07:01:24 +0000281 sizeof(u32) * RTAX_MAX) == 0 &&
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000282 ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
284 return fi;
285 }
286
287 return NULL;
288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/* Check, that the gateway is already configured.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000291 * Used only by redirect accept routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Al Virod878e72e2006-09-26 22:18:13 -0700293int ip_fib_check_default(__be32 gw, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 struct hlist_head *head;
296 struct hlist_node *node;
297 struct fib_nh *nh;
298 unsigned int hash;
299
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700300 spin_lock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 hash = fib_devindex_hashfn(dev->ifindex);
303 head = &fib_info_devhash[hash];
304 hlist_for_each_entry(nh, node, head, nh_hash) {
305 if (nh->nh_dev == dev &&
306 nh->nh_gw == gw &&
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000307 !(nh->nh_flags & RTNH_F_DEAD)) {
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700308 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return 0;
310 }
311 }
312
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700313 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 return -1;
316}
317
Thomas Graf339bf982006-11-10 14:10:15 -0800318static inline size_t fib_nlmsg_size(struct fib_info *fi)
319{
320 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
321 + nla_total_size(4) /* RTA_TABLE */
322 + nla_total_size(4) /* RTA_DST */
323 + nla_total_size(4) /* RTA_PRIORITY */
324 + nla_total_size(4); /* RTA_PREFSRC */
325
326 /* space for nested metrics */
327 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
328
329 if (fi->fib_nhs) {
330 /* Also handles the special case fib_nhs == 1 */
331
332 /* each nexthop is packed in an attribute */
333 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
334
335 /* may contain flow and gateway attribute */
336 nhsize += 2 * nla_total_size(4);
337
338 /* all nexthops are packed in a nested attribute */
339 payload += nla_total_size(fi->fib_nhs * nhsize);
340 }
341
342 return payload;
343}
344
Al Viro81f7bf62006-09-27 18:40:00 -0700345void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
Milan Kocianb8f55832007-05-23 14:55:06 -0700346 int dst_len, u32 tb_id, struct nl_info *info,
347 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct sk_buff *skb;
Thomas Graf4e902c52006-08-17 18:14:52 -0700350 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700351 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Thomas Graf339bf982006-11-10 14:10:15 -0800353 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
Thomas Graff21c7bc2006-08-15 00:34:17 -0700354 if (skb == NULL)
355 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Thomas Graf4e902c52006-08-17 18:14:52 -0700357 err = fib_dump_info(skb, info->pid, seq, event, tb_id,
David S. Miller37e826c2011-03-24 18:06:47 -0700358 fa->fa_type, key, dst_len,
Milan Kocianb8f55832007-05-23 14:55:06 -0700359 fa->fa_tos, fa->fa_info, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -0800360 if (err < 0) {
361 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
362 WARN_ON(err == -EMSGSIZE);
363 kfree_skb(skb);
364 goto errout;
365 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800366 rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
367 info->nlh, GFP_KERNEL);
368 return;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700369errout:
370 if (err < 0)
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800371 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374/* Return the first fib alias matching TOS with
375 * priority less than or equal to PRIO.
376 */
377struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
378{
379 if (fah) {
380 struct fib_alias *fa;
381 list_for_each_entry(fa, fah, fa_list) {
382 if (fa->fa_tos > tos)
383 continue;
384 if (fa->fa_info->fib_priority >= prio ||
385 fa->fa_tos < tos)
386 return fa;
387 }
388 }
389 return NULL;
390}
391
392int fib_detect_death(struct fib_info *fi, int order,
Denis V. Lunevc17860a2007-12-08 00:22:13 -0800393 struct fib_info **last_resort, int *last_idx, int dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct neighbour *n;
396 int state = NUD_NONE;
397
398 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
399 if (n) {
400 state = n->nud_state;
401 neigh_release(n);
402 }
Jianjun Kongd93191002008-11-03 00:23:42 -0800403 if (state == NUD_REACHABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000405 if ((state & NUD_VALID) && order != dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000407 if ((state & NUD_VALID) ||
408 (*last_idx < 0 && order > dflt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 *last_resort = fi;
410 *last_idx = order;
411 }
412 return 1;
413}
414
415#ifdef CONFIG_IP_ROUTE_MULTIPATH
416
Thomas Graf4e902c52006-08-17 18:14:52 -0700417static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 int nhs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Thomas Graf4e902c52006-08-17 18:14:52 -0700421 while (rtnh_ok(rtnh, remaining)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 nhs++;
Thomas Graf4e902c52006-08-17 18:14:52 -0700423 rtnh = rtnh_next(rtnh, &remaining);
424 }
425
426 /* leftover implies invalid nexthop configuration, discard it */
427 return remaining > 0 ? 0 : nhs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Thomas Graf4e902c52006-08-17 18:14:52 -0700430static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
431 int remaining, struct fib_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 change_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700434 int attrlen;
435
436 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700438
David S. Miller71fceff2010-01-15 01:16:40 -0800439 nexthop_nh->nh_flags =
440 (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
441 nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
442 nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700443
444 attrlen = rtnh_attrlen(rtnh);
445 if (attrlen > 0) {
446 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
447
448 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David S. Miller71fceff2010-01-15 01:16:40 -0800449 nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100450#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700451 nla = nla_find(attrs, attrlen, RTA_FLOW);
David S. Miller71fceff2010-01-15 01:16:40 -0800452 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700453 if (nexthop_nh->nh_tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -0700454 fi->fib_net->ipv4.fib_num_tclassid_users++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455#endif
456 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700457
458 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 } endfor_nexthops(fi);
Thomas Graf4e902c52006-08-17 18:14:52 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
462}
463
464#endif
465
Thomas Graf4e902c52006-08-17 18:14:52 -0700466int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700469 struct rtnexthop *rtnh;
470 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471#endif
472
Thomas Graf4e902c52006-08-17 18:14:52 -0700473 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return 1;
475
Thomas Graf4e902c52006-08-17 18:14:52 -0700476 if (cfg->fc_oif || cfg->fc_gw) {
477 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
478 (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480 return 1;
481 }
482
483#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700484 if (cfg->fc_mp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700486
487 rtnh = cfg->fc_mp;
488 remaining = cfg->fc_mp_len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 for_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700491 int attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Thomas Graf4e902c52006-08-17 18:14:52 -0700493 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700495
496 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700498
499 attrlen = rtnh_attrlen(rtnh);
500 if (attrlen < 0) {
501 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
502
503 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
Al Viro17fb2c62006-09-26 22:15:25 -0700504 if (nla && nla_get_be32(nla) != nh->nh_gw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 1;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100506#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700507 nla = nla_find(attrs, attrlen, RTA_FLOW);
508 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return 1;
510#endif
511 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700512
513 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 } endfor_nexthops(fi);
515#endif
516 return 0;
517}
518
519
520/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000521 * Picture
522 * -------
523 *
524 * Semantics of nexthop is very messy by historical reasons.
525 * We have to take into account, that:
526 * a) gateway can be actually local interface address,
527 * so that gatewayed route is direct.
528 * b) gateway must be on-link address, possibly
529 * described not by an ifaddr, but also by a direct route.
530 * c) If both gateway and interface are specified, they should not
531 * contradict.
532 * d) If we use tunnel routes, gateway could be not on-link.
533 *
534 * Attempt to reconcile all of these (alas, self-contradictory) conditions
535 * results in pretty ugly and hairy code with obscure logic.
536 *
537 * I chose to generalized it instead, so that the size
538 * of code does not increase practically, but it becomes
539 * much more general.
540 * Every prefix is assigned a "scope" value: "host" is local address,
541 * "link" is direct route,
542 * [ ... "site" ... "interior" ... ]
543 * and "universe" is true gateway route with global meaning.
544 *
545 * Every prefix refers to a set of "nexthop"s (gw, oif),
546 * where gw must have narrower scope. This recursion stops
547 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
548 * which means that gw is forced to be on link.
549 *
550 * Code is still hairy, but now it is apparently logically
551 * consistent and very flexible. F.e. as by-product it allows
552 * to co-exists in peace independent exterior and interior
553 * routing processes.
554 *
555 * Normally it looks as following.
556 *
557 * {universe prefix} -> (gw, oif) [scope link]
558 * |
559 * |-> {link prefix} -> (gw, oif) [scope local]
560 * |
561 * |-> {local prefix} (terminal node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
Thomas Graf4e902c52006-08-17 18:14:52 -0700563static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
564 struct fib_nh *nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 int err;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800567 struct net *net;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000568 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Denis V. Lunev86167a32008-01-21 17:34:00 -0800570 net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (nh->nh_gw) {
572 struct fib_result res;
573
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000574 if (nh->nh_flags & RTNH_F_ONLINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Thomas Graf4e902c52006-08-17 18:14:52 -0700576 if (cfg->fc_scope >= RT_SCOPE_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return -EINVAL;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800578 if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return -EINVAL;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000580 dev = __dev_get_by_index(net, nh->nh_oif);
581 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return -ENODEV;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000583 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return -ENETDOWN;
585 nh->nh_dev = dev;
586 dev_hold(dev);
587 nh->nh_scope = RT_SCOPE_LINK;
588 return 0;
589 }
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000590 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 {
David S. Miller9ade2282011-03-12 02:02:42 -0500592 struct flowi4 fl4 = {
593 .daddr = nh->nh_gw,
594 .flowi4_scope = cfg->fc_scope + 1,
595 .flowi4_oif = nh->nh_oif,
Thomas Graf4e902c52006-08-17 18:14:52 -0700596 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /* It is not necessary, but requires a bit of thinking */
David S. Miller9ade2282011-03-12 02:02:42 -0500599 if (fl4.flowi4_scope < RT_SCOPE_LINK)
600 fl4.flowi4_scope = RT_SCOPE_LINK;
601 err = fib_lookup(net, &fl4, &res);
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000602 if (err) {
603 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return err;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 err = -EINVAL;
608 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
609 goto out;
610 nh->nh_scope = res.scope;
611 nh->nh_oif = FIB_RES_OIF(res);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000612 nh->nh_dev = dev = FIB_RES_DEV(res);
613 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 goto out;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000615 dev_hold(dev);
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000616 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 } else {
618 struct in_device *in_dev;
619
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000620 if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return -EINVAL;
622
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000623 rcu_read_lock();
624 err = -ENODEV;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800625 in_dev = inetdev_by_index(net, nh->nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (in_dev == NULL)
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000627 goto out;
628 err = -ENETDOWN;
629 if (!(in_dev->dev->flags & IFF_UP))
630 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 nh->nh_dev = in_dev->dev;
632 dev_hold(nh->nh_dev);
633 nh->nh_scope = RT_SCOPE_HOST;
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000634 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000636out:
637 rcu_read_unlock();
638 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Al Viro81f7bf62006-09-27 18:40:00 -0700641static inline unsigned int fib_laddr_hashfn(__be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
David S. Miller123b9732011-02-01 15:34:21 -0800643 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000645 return ((__force u32)val ^
646 ((__force u32)val >> 7) ^
647 ((__force u32)val >> 14)) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
David S. Miller123b9732011-02-01 15:34:21 -0800650static struct hlist_head *fib_info_hash_alloc(int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 if (bytes <= PAGE_SIZE)
Joonwoo Park88f83492007-11-26 23:29:32 +0800653 return kzalloc(bytes, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 else
655 return (struct hlist_head *)
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000656 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
657 get_order(bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
David S. Miller123b9732011-02-01 15:34:21 -0800660static void fib_info_hash_free(struct hlist_head *hash, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 if (!hash)
663 return;
664
665 if (bytes <= PAGE_SIZE)
666 kfree(hash);
667 else
668 free_pages((unsigned long) hash, get_order(bytes));
669}
670
David S. Miller123b9732011-02-01 15:34:21 -0800671static void fib_info_hash_move(struct hlist_head *new_info_hash,
672 struct hlist_head *new_laddrhash,
673 unsigned int new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
David S. Millerb7656e72005-08-05 04:12:48 -0700675 struct hlist_head *old_info_hash, *old_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -0800676 unsigned int old_size = fib_info_hash_size;
David S. Millerb7656e72005-08-05 04:12:48 -0700677 unsigned int i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700679 spin_lock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -0700680 old_info_hash = fib_info_hash;
681 old_laddrhash = fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -0800682 fib_info_hash_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 for (i = 0; i < old_size; i++) {
685 struct hlist_head *head = &fib_info_hash[i];
686 struct hlist_node *node, *n;
687 struct fib_info *fi;
688
689 hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
690 struct hlist_head *dest;
691 unsigned int new_hash;
692
693 hlist_del(&fi->fib_hash);
694
695 new_hash = fib_info_hashfn(fi);
696 dest = &new_info_hash[new_hash];
697 hlist_add_head(&fi->fib_hash, dest);
698 }
699 }
700 fib_info_hash = new_info_hash;
701
702 for (i = 0; i < old_size; i++) {
703 struct hlist_head *lhead = &fib_info_laddrhash[i];
704 struct hlist_node *node, *n;
705 struct fib_info *fi;
706
707 hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
708 struct hlist_head *ldest;
709 unsigned int new_hash;
710
711 hlist_del(&fi->fib_lhash);
712
713 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
714 ldest = &new_laddrhash[new_hash];
715 hlist_add_head(&fi->fib_lhash, ldest);
716 }
717 }
718 fib_info_laddrhash = new_laddrhash;
719
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700720 spin_unlock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -0700721
722 bytes = old_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -0800723 fib_info_hash_free(old_info_hash, bytes);
724 fib_info_hash_free(old_laddrhash, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
David S. Miller436c3b62011-03-24 17:42:21 -0700727__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
728{
729 nh->nh_saddr = inet_select_addr(nh->nh_dev,
730 nh->nh_gw,
David S. Miller37e826c2011-03-24 18:06:47 -0700731 nh->nh_parent->fib_scope);
David S. Miller436c3b62011-03-24 17:42:21 -0700732 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
733
734 return nh->nh_saddr;
735}
736
Thomas Graf4e902c52006-08-17 18:14:52 -0700737struct fib_info *fib_create_info(struct fib_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 int err;
740 struct fib_info *fi = NULL;
741 struct fib_info *ofi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 int nhs = 1;
Denis V. Lunev7462bd742008-01-31 18:49:32 -0800743 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
David S. Miller4c8237c2011-03-07 14:27:38 -0800745 if (cfg->fc_type > RTN_MAX)
746 goto err_inval;
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* Fast check to catch the most weird cases */
Thomas Graf4e902c52006-08-17 18:14:52 -0700749 if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 goto err_inval;
751
752#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700753 if (cfg->fc_mp) {
754 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (nhs == 0)
756 goto err_inval;
757 }
758#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 err = -ENOBUFS;
David S. Miller123b9732011-02-01 15:34:21 -0800761 if (fib_info_cnt >= fib_info_hash_size) {
762 unsigned int new_size = fib_info_hash_size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct hlist_head *new_info_hash;
764 struct hlist_head *new_laddrhash;
765 unsigned int bytes;
766
767 if (!new_size)
768 new_size = 1;
769 bytes = new_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -0800770 new_info_hash = fib_info_hash_alloc(bytes);
771 new_laddrhash = fib_info_hash_alloc(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (!new_info_hash || !new_laddrhash) {
David S. Miller123b9732011-02-01 15:34:21 -0800773 fib_info_hash_free(new_info_hash, bytes);
774 fib_info_hash_free(new_laddrhash, bytes);
Joonwoo Park88f83492007-11-26 23:29:32 +0800775 } else
David S. Miller123b9732011-02-01 15:34:21 -0800776 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
David S. Miller123b9732011-02-01 15:34:21 -0800778 if (!fib_info_hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 goto failure;
780 }
781
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700782 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (fi == NULL)
784 goto failure;
David S. Miller725d1e12011-01-28 14:05:05 -0800785 if (cfg->fc_mx) {
786 fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
787 if (!fi->fib_metrics)
788 goto failure;
789 } else
790 fi->fib_metrics = (u32 *) dst_default_metrics;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 fib_info_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Denis V. Lunev57d7a602008-04-16 02:00:50 -0700793 fi->fib_net = hold_net(net);
Thomas Graf4e902c52006-08-17 18:14:52 -0700794 fi->fib_protocol = cfg->fc_protocol;
David S. Miller37e826c2011-03-24 18:06:47 -0700795 fi->fib_scope = cfg->fc_scope;
Thomas Graf4e902c52006-08-17 18:14:52 -0700796 fi->fib_flags = cfg->fc_flags;
797 fi->fib_priority = cfg->fc_priority;
798 fi->fib_prefsrc = cfg->fc_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 fi->fib_nhs = nhs;
801 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -0800802 nexthop_nh->nh_parent = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 } endfor_nexthops(fi)
804
Thomas Graf4e902c52006-08-17 18:14:52 -0700805 if (cfg->fc_mx) {
806 struct nlattr *nla;
807 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Thomas Graf4e902c52006-08-17 18:14:52 -0700809 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200810 int type = nla_type(nla);
Thomas Graf4e902c52006-08-17 18:14:52 -0700811
812 if (type) {
David S. Miller6fac2622012-06-17 19:47:34 -0700813 u32 val;
814
Thomas Graf4e902c52006-08-17 18:14:52 -0700815 if (type > RTAX_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 goto err_inval;
David S. Miller6fac2622012-06-17 19:47:34 -0700817 val = nla_get_u32(nla);
818 if (type == RTAX_ADVMSS && val > 65535 - 40)
819 val = 65535 - 40;
David S. Miller710ab6c2012-07-10 07:02:09 -0700820 if (type == RTAX_MTU && val > 65535 - 15)
821 val = 65535 - 15;
David S. Miller6fac2622012-06-17 19:47:34 -0700822 fi->fib_metrics[type - 1] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Thomas Graf4e902c52006-08-17 18:14:52 -0700827 if (cfg->fc_mp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700829 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
830 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto failure;
Thomas Graf4e902c52006-08-17 18:14:52 -0700832 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 goto err_inval;
Thomas Graf4e902c52006-08-17 18:14:52 -0700834 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 goto err_inval;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100836#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700837 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 goto err_inval;
839#endif
840#else
841 goto err_inval;
842#endif
843 } else {
844 struct fib_nh *nh = fi->fib_nh;
Thomas Graf4e902c52006-08-17 18:14:52 -0700845
846 nh->nh_oif = cfg->fc_oif;
847 nh->nh_gw = cfg->fc_gw;
848 nh->nh_flags = cfg->fc_flags;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100849#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700850 nh->nh_tclassid = cfg->fc_flow;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700851 if (nh->nh_tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -0700852 fi->fib_net->ipv4.fib_num_tclassid_users++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854#ifdef CONFIG_IP_ROUTE_MULTIPATH
855 nh->nh_weight = 1;
856#endif
857 }
858
Thomas Graf4e902c52006-08-17 18:14:52 -0700859 if (fib_props[cfg->fc_type].error) {
860 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 goto err_inval;
862 goto link_it;
David S. Miller4c8237c2011-03-07 14:27:38 -0800863 } else {
864 switch (cfg->fc_type) {
865 case RTN_UNICAST:
866 case RTN_LOCAL:
867 case RTN_BROADCAST:
868 case RTN_ANYCAST:
869 case RTN_MULTICAST:
870 break;
871 default:
872 goto err_inval;
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
Thomas Graf4e902c52006-08-17 18:14:52 -0700876 if (cfg->fc_scope > RT_SCOPE_HOST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 goto err_inval;
878
Thomas Graf4e902c52006-08-17 18:14:52 -0700879 if (cfg->fc_scope == RT_SCOPE_HOST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct fib_nh *nh = fi->fib_nh;
881
882 /* Local address is added. */
883 if (nhs != 1 || nh->nh_gw)
884 goto err_inval;
885 nh->nh_scope = RT_SCOPE_NOWHERE;
Denis V. Lunev7462bd742008-01-31 18:49:32 -0800886 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 err = -ENODEV;
888 if (nh->nh_dev == NULL)
889 goto failure;
890 } else {
891 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000892 err = fib_check_nh(cfg, fi, nexthop_nh);
893 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto failure;
895 } endfor_nexthops(fi)
896 }
897
898 if (fi->fib_prefsrc) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700899 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
900 fi->fib_prefsrc != cfg->fc_dst)
Denis V. Lunev7462bd742008-01-31 18:49:32 -0800901 if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 goto err_inval;
903 }
904
David S. Miller1fc050a2011-03-07 20:54:48 -0800905 change_nexthops(fi) {
David S. Miller436c3b62011-03-24 17:42:21 -0700906 fib_info_update_nh_saddr(net, nexthop_nh);
David S. Miller1fc050a2011-03-07 20:54:48 -0800907 } endfor_nexthops(fi)
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909link_it:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000910 ofi = fib_find_info(fi);
911 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 fi->fib_dead = 1;
913 free_fib_info(fi);
914 ofi->fib_treeref++;
915 return ofi;
916 }
917
918 fi->fib_treeref++;
919 atomic_inc(&fi->fib_clntref);
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700920 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 hlist_add_head(&fi->fib_hash,
922 &fib_info_hash[fib_info_hashfn(fi)]);
923 if (fi->fib_prefsrc) {
924 struct hlist_head *head;
925
926 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
927 hlist_add_head(&fi->fib_lhash, head);
928 }
929 change_nexthops(fi) {
930 struct hlist_head *head;
931 unsigned int hash;
932
David S. Miller71fceff2010-01-15 01:16:40 -0800933 if (!nexthop_nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800935 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 head = &fib_info_devhash[hash];
David S. Miller71fceff2010-01-15 01:16:40 -0800937 hlist_add_head(&nexthop_nh->nh_hash, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 } endfor_nexthops(fi)
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700939 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return fi;
941
942err_inval:
943 err = -EINVAL;
944
945failure:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900946 if (fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 fi->fib_dead = 1;
948 free_fib_info(fi);
949 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700950
951 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Thomas Grafbe403ea2006-08-17 18:15:17 -0700954int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
David S. Miller37e826c2011-03-24 18:06:47 -0700955 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
Thomas Grafbe403ea2006-08-17 18:15:17 -0700956 struct fib_info *fi, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Thomas Grafbe403ea2006-08-17 18:15:17 -0700958 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Thomas Grafbe403ea2006-08-17 18:15:17 -0700961 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
962 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800963 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -0700964
965 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 rtm->rtm_family = AF_INET;
967 rtm->rtm_dst_len = dst_len;
968 rtm->rtm_src_len = 0;
969 rtm->rtm_tos = tos;
Krzysztof Piotr Oledzki709772e2008-06-10 15:44:49 -0700970 if (tb_id < 256)
971 rtm->rtm_table = tb_id;
972 else
973 rtm->rtm_table = RT_TABLE_COMPAT;
David S. Millerf3756b72012-04-01 20:39:02 -0400974 if (nla_put_u32(skb, RTA_TABLE, tb_id))
975 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 rtm->rtm_type = type;
977 rtm->rtm_flags = fi->fib_flags;
David S. Miller37e826c2011-03-24 18:06:47 -0700978 rtm->rtm_scope = fi->fib_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 rtm->rtm_protocol = fi->fib_protocol;
Thomas Grafbe403ea2006-08-17 18:15:17 -0700980
David S. Millerf3756b72012-04-01 20:39:02 -0400981 if (rtm->rtm_dst_len &&
982 nla_put_be32(skb, RTA_DST, dst))
983 goto nla_put_failure;
984 if (fi->fib_priority &&
985 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
986 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -0700988 goto nla_put_failure;
989
David S. Millerf3756b72012-04-01 20:39:02 -0400990 if (fi->fib_prefsrc &&
991 nla_put_be32(skb, RTA_PREFSRC, fi->fib_prefsrc))
992 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (fi->fib_nhs == 1) {
David S. Millerf3756b72012-04-01 20:39:02 -0400994 if (fi->fib_nh->nh_gw &&
995 nla_put_be32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
996 goto nla_put_failure;
997 if (fi->fib_nh->nh_oif &&
998 nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
999 goto nla_put_failure;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001000#ifdef CONFIG_IP_ROUTE_CLASSID
David S. Millerf3756b72012-04-01 20:39:02 -04001001 if (fi->fib_nh[0].nh_tclassid &&
1002 nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
1003 goto nla_put_failure;
Patrick McHardy8265abc2006-07-21 15:09:55 -07001004#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006#ifdef CONFIG_IP_ROUTE_MULTIPATH
1007 if (fi->fib_nhs > 1) {
Thomas Grafbe403ea2006-08-17 18:15:17 -07001008 struct rtnexthop *rtnh;
1009 struct nlattr *mp;
1010
1011 mp = nla_nest_start(skb, RTA_MULTIPATH);
1012 if (mp == NULL)
1013 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 for_nexthops(fi) {
Thomas Grafbe403ea2006-08-17 18:15:17 -07001016 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1017 if (rtnh == NULL)
1018 goto nla_put_failure;
1019
1020 rtnh->rtnh_flags = nh->nh_flags & 0xFF;
1021 rtnh->rtnh_hops = nh->nh_weight - 1;
1022 rtnh->rtnh_ifindex = nh->nh_oif;
1023
David S. Millerf3756b72012-04-01 20:39:02 -04001024 if (nh->nh_gw &&
1025 nla_put_be32(skb, RTA_GATEWAY, nh->nh_gw))
1026 goto nla_put_failure;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001027#ifdef CONFIG_IP_ROUTE_CLASSID
David S. Millerf3756b72012-04-01 20:39:02 -04001028 if (nh->nh_tclassid &&
1029 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1030 goto nla_put_failure;
Patrick McHardy8265abc2006-07-21 15:09:55 -07001031#endif
Thomas Grafbe403ea2006-08-17 18:15:17 -07001032 /* length of rtnetlink header + attributes */
1033 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 } endfor_nexthops(fi);
Thomas Grafbe403ea2006-08-17 18:15:17 -07001035
1036 nla_nest_end(skb, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038#endif
Thomas Grafbe403ea2006-08-17 18:15:17 -07001039 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Thomas Grafbe403ea2006-08-17 18:15:17 -07001041nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001042 nlmsg_cancel(skb, nlh);
1043 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001047 * Update FIB if:
1048 * - local address disappeared -> we must delete all the entries
1049 * referring to it.
1050 * - device went down -> we must shutdown all nexthops going via it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001052int fib_sync_down_addr(struct net *net, __be32 local)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001053{
1054 int ret = 0;
1055 unsigned int hash = fib_laddr_hashfn(local);
1056 struct hlist_head *head = &fib_info_laddrhash[hash];
1057 struct hlist_node *node;
1058 struct fib_info *fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001060 if (fib_info_laddrhash == NULL || local == 0)
1061 return 0;
1062
1063 hlist_for_each_entry(fi, node, head, fib_lhash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001064 if (!net_eq(fi->fib_net, net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001065 continue;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001066 if (fi->fib_prefsrc == local) {
1067 fi->fib_flags |= RTNH_F_DEAD;
1068 ret++;
1069 }
1070 }
1071 return ret;
1072}
1073
1074int fib_sync_down_dev(struct net_device *dev, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 int ret = 0;
1077 int scope = RT_SCOPE_NOWHERE;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001078 struct fib_info *prev_fi = NULL;
1079 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1080 struct hlist_head *head = &fib_info_devhash[hash];
1081 struct hlist_node *node;
1082 struct fib_nh *nh;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (force)
1085 scope = -1;
1086
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001087 hlist_for_each_entry(nh, node, head, nh_hash) {
1088 struct fib_info *fi = nh->nh_parent;
1089 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001091 BUG_ON(!fi->fib_nhs);
1092 if (nh->nh_dev != dev || fi == prev_fi)
1093 continue;
1094 prev_fi = fi;
1095 dead = 0;
1096 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001097 if (nexthop_nh->nh_flags & RTNH_F_DEAD)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001098 dead++;
David S. Miller71fceff2010-01-15 01:16:40 -08001099 else if (nexthop_nh->nh_dev == dev &&
1100 nexthop_nh->nh_scope != scope) {
1101 nexthop_nh->nh_flags |= RTNH_F_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102#ifdef CONFIG_IP_ROUTE_MULTIPATH
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001103 spin_lock_bh(&fib_multipath_lock);
David S. Miller71fceff2010-01-15 01:16:40 -08001104 fi->fib_power -= nexthop_nh->nh_power;
1105 nexthop_nh->nh_power = 0;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001106 spin_unlock_bh(&fib_multipath_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107#endif
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001108 dead++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001110#ifdef CONFIG_IP_ROUTE_MULTIPATH
David S. Miller71fceff2010-01-15 01:16:40 -08001111 if (force > 1 && nexthop_nh->nh_dev == dev) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001112 dead = fi->fib_nhs;
1113 break;
1114 }
1115#endif
1116 } endfor_nexthops(fi)
1117 if (dead == fi->fib_nhs) {
1118 fi->fib_flags |= RTNH_F_DEAD;
1119 ret++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121 }
1122
1123 return ret;
1124}
1125
David S. Miller0c838ff2011-01-31 16:16:50 -08001126/* Must be invoked inside of an RCU protected region. */
1127void fib_select_default(struct fib_result *res)
1128{
1129 struct fib_info *fi = NULL, *last_resort = NULL;
1130 struct list_head *fa_head = res->fa_head;
1131 struct fib_table *tb = res->table;
1132 int order = -1, last_idx = -1;
1133 struct fib_alias *fa;
1134
1135 list_for_each_entry_rcu(fa, fa_head, fa_list) {
1136 struct fib_info *next_fi = fa->fa_info;
1137
David S. Miller37e826c2011-03-24 18:06:47 -07001138 if (next_fi->fib_scope != res->scope ||
David S. Miller0c838ff2011-01-31 16:16:50 -08001139 fa->fa_type != RTN_UNICAST)
1140 continue;
1141
1142 if (next_fi->fib_priority > res->fi->fib_priority)
1143 break;
1144 if (!next_fi->fib_nh[0].nh_gw ||
1145 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
1146 continue;
1147
1148 fib_alias_accessed(fa);
1149
1150 if (fi == NULL) {
1151 if (next_fi != res->fi)
1152 break;
1153 } else if (!fib_detect_death(fi, order, &last_resort,
1154 &last_idx, tb->tb_default)) {
1155 fib_result_assign(res, fi);
1156 tb->tb_default = order;
1157 goto out;
1158 }
1159 fi = next_fi;
1160 order++;
1161 }
1162
1163 if (order <= 0 || fi == NULL) {
1164 tb->tb_default = -1;
1165 goto out;
1166 }
1167
1168 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
1169 tb->tb_default)) {
1170 fib_result_assign(res, fi);
1171 tb->tb_default = order;
1172 goto out;
1173 }
1174
1175 if (last_idx >= 0)
1176 fib_result_assign(res, last_resort);
1177 tb->tb_default = last_idx;
1178out:
Eric Dumazet31d40932011-02-14 11:23:04 -08001179 return;
David S. Miller0c838ff2011-01-31 16:16:50 -08001180}
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182#ifdef CONFIG_IP_ROUTE_MULTIPATH
1183
1184/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001185 * Dead device goes up. We wake up dead nexthops.
1186 * It takes sense only on multipath routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188int fib_sync_up(struct net_device *dev)
1189{
1190 struct fib_info *prev_fi;
1191 unsigned int hash;
1192 struct hlist_head *head;
1193 struct hlist_node *node;
1194 struct fib_nh *nh;
1195 int ret;
1196
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001197 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return 0;
1199
1200 prev_fi = NULL;
1201 hash = fib_devindex_hashfn(dev->ifindex);
1202 head = &fib_info_devhash[hash];
1203 ret = 0;
1204
1205 hlist_for_each_entry(nh, node, head, nh_hash) {
1206 struct fib_info *fi = nh->nh_parent;
1207 int alive;
1208
1209 BUG_ON(!fi->fib_nhs);
1210 if (nh->nh_dev != dev || fi == prev_fi)
1211 continue;
1212
1213 prev_fi = fi;
1214 alive = 0;
1215 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001216 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 alive++;
1218 continue;
1219 }
David S. Miller71fceff2010-01-15 01:16:40 -08001220 if (nexthop_nh->nh_dev == NULL ||
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001221 !(nexthop_nh->nh_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 continue;
David S. Miller71fceff2010-01-15 01:16:40 -08001223 if (nexthop_nh->nh_dev != dev ||
1224 !__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 continue;
1226 alive++;
1227 spin_lock_bh(&fib_multipath_lock);
David S. Miller71fceff2010-01-15 01:16:40 -08001228 nexthop_nh->nh_power = 0;
1229 nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 spin_unlock_bh(&fib_multipath_lock);
1231 } endfor_nexthops(fi)
1232
1233 if (alive > 0) {
1234 fi->fib_flags &= ~RTNH_F_DEAD;
1235 ret++;
1236 }
1237 }
1238
1239 return ret;
1240}
1241
1242/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001243 * The algorithm is suboptimal, but it provides really
1244 * fair weighted route distribution.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 */
David S. Miller1b7fe5932011-03-10 17:01:16 -08001246void fib_select_multipath(struct fib_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 struct fib_info *fi = res->fi;
1249 int w;
1250
1251 spin_lock_bh(&fib_multipath_lock);
1252 if (fi->fib_power <= 0) {
1253 int power = 0;
1254 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001255 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
David S. Miller71fceff2010-01-15 01:16:40 -08001256 power += nexthop_nh->nh_weight;
1257 nexthop_nh->nh_power = nexthop_nh->nh_weight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259 } endfor_nexthops(fi);
1260 fi->fib_power = power;
1261 if (power <= 0) {
1262 spin_unlock_bh(&fib_multipath_lock);
1263 /* Race condition: route has just become dead. */
1264 res->nh_sel = 0;
1265 return;
1266 }
1267 }
1268
1269
1270 /* w should be random number [0..fi->fib_power-1],
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001271 * it is pretty bad approximation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
1273
1274 w = jiffies % fi->fib_power;
1275
1276 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001277 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
David S. Miller71fceff2010-01-15 01:16:40 -08001278 nexthop_nh->nh_power) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001279 w -= nexthop_nh->nh_power;
1280 if (w <= 0) {
David S. Miller71fceff2010-01-15 01:16:40 -08001281 nexthop_nh->nh_power--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 fi->fib_power--;
1283 res->nh_sel = nhsel;
1284 spin_unlock_bh(&fib_multipath_lock);
1285 return;
1286 }
1287 }
1288 } endfor_nexthops(fi);
1289
1290 /* Race condition: route has just become dead. */
1291 res->nh_sel = 0;
1292 spin_unlock_bh(&fib_multipath_lock);
1293}
1294#endif