blob: 0f80dfc2f7fb49a4336329b48935aa937669351e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * IPv4 Forwarding Information Base: semantics.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/uaccess.h>
17#include <asm/system.h>
18#include <linux/bitops.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/jiffies.h>
22#include <linux/mm.h>
23#include <linux/string.h>
24#include <linux/socket.h>
25#include <linux/sockios.h>
26#include <linux/errno.h>
27#include <linux/in.h>
28#include <linux/inet.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020029#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/proc_fs.h>
33#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020037#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <net/ip.h>
39#include <net/protocol.h>
40#include <net/route.h>
41#include <net/tcp.h>
42#include <net/sock.h>
43#include <net/ip_fib.h>
Thomas Graff21c7bc2006-08-15 00:34:17 -070044#include <net/netlink.h>
Thomas Graf4e902c52006-08-17 18:14:52 -070045#include <net/nexthop.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include "fib_lookup.h"
48
Stephen Hemminger832b4c52006-08-29 16:48:09 -070049static DEFINE_SPINLOCK(fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static struct hlist_head *fib_info_hash;
51static struct hlist_head *fib_info_laddrhash;
52static unsigned int fib_hash_size;
53static unsigned int fib_info_cnt;
54
55#define DEVINDEX_HASHBITS 8
56#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
57static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
58
59#ifdef CONFIG_IP_ROUTE_MULTIPATH
60
61static DEFINE_SPINLOCK(fib_multipath_lock);
62
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000063#define for_nexthops(fi) { \
64 int nhsel; const struct fib_nh *nh; \
65 for (nhsel = 0, nh = (fi)->fib_nh; \
66 nhsel < (fi)->fib_nhs; \
67 nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000069#define change_nexthops(fi) { \
70 int nhsel; struct fib_nh *nexthop_nh; \
71 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
72 nhsel < (fi)->fib_nhs; \
73 nexthop_nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75#else /* CONFIG_IP_ROUTE_MULTIPATH */
76
77/* Hope, that gcc will optimize it to get rid of dummy loop */
78
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000079#define for_nexthops(fi) { \
80 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
81 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000083#define change_nexthops(fi) { \
84 int nhsel; \
85 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
86 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#endif /* CONFIG_IP_ROUTE_MULTIPATH */
89
90#define endfor_nexthops(fi) }
91
92
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090093static const struct
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 int error;
96 u8 scope;
Thomas Grafa0ee18b2007-03-24 20:32:54 -070097} fib_props[RTN_MAX + 1] = {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000098 [RTN_UNSPEC] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .error = 0,
100 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000101 },
102 [RTN_UNICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .error = 0,
104 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000105 },
106 [RTN_LOCAL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .error = 0,
108 .scope = RT_SCOPE_HOST,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000109 },
110 [RTN_BROADCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .error = 0,
112 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000113 },
114 [RTN_ANYCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .error = 0,
116 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000117 },
118 [RTN_MULTICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .error = 0,
120 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000121 },
122 [RTN_BLACKHOLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .error = -EINVAL,
124 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000125 },
126 [RTN_UNREACHABLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .error = -EHOSTUNREACH,
128 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000129 },
130 [RTN_PROHIBIT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .error = -EACCES,
132 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000133 },
134 [RTN_THROW] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .error = -EAGAIN,
136 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000137 },
138 [RTN_NAT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .error = -EINVAL,
140 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000141 },
142 [RTN_XRESOLVE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .error = -EINVAL,
144 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000145 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
148
149/* Release a nexthop info record */
150
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000151static void free_fib_info_rcu(struct rcu_head *head)
152{
153 struct fib_info *fi = container_of(head, struct fib_info, rcu);
154
155 kfree(fi);
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158void free_fib_info(struct fib_info *fi)
159{
160 if (fi->fib_dead == 0) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000161 pr_warning("Freeing alive fib_info %p\n", fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return;
163 }
164 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -0800165 if (nexthop_nh->nh_dev)
166 dev_put(nexthop_nh->nh_dev);
167 nexthop_nh->nh_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 } endfor_nexthops(fi);
169 fib_info_cnt--;
Denis V. Lunev57d7a602008-04-16 02:00:50 -0700170 release_net(fi->fib_net);
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000171 call_rcu(&fi->rcu, free_fib_info_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174void fib_release_info(struct fib_info *fi)
175{
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700176 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (fi && --fi->fib_treeref == 0) {
178 hlist_del(&fi->fib_hash);
179 if (fi->fib_prefsrc)
180 hlist_del(&fi->fib_lhash);
181 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -0800182 if (!nexthop_nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800184 hlist_del(&nexthop_nh->nh_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 } endfor_nexthops(fi)
186 fi->fib_dead = 1;
187 fib_info_put(fi);
188 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700189 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000192static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 const struct fib_nh *onh = ofi->fib_nh;
195
196 for_nexthops(fi) {
197 if (nh->nh_oif != onh->nh_oif ||
198 nh->nh_gw != onh->nh_gw ||
199 nh->nh_scope != onh->nh_scope ||
200#ifdef CONFIG_IP_ROUTE_MULTIPATH
201 nh->nh_weight != onh->nh_weight ||
202#endif
203#ifdef CONFIG_NET_CLS_ROUTE
204 nh->nh_tclassid != onh->nh_tclassid ||
205#endif
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000206 ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return -1;
208 onh++;
209 } endfor_nexthops(fi);
210 return 0;
211}
212
David S. Miller88ebc722008-01-12 21:49:01 -0800213static inline unsigned int fib_devindex_hashfn(unsigned int val)
214{
215 unsigned int mask = DEVINDEX_HASHSIZE - 1;
216
217 return (val ^
218 (val >> DEVINDEX_HASHBITS) ^
219 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
223{
224 unsigned int mask = (fib_hash_size - 1);
225 unsigned int val = fi->fib_nhs;
226
227 val ^= fi->fib_protocol;
Al Viro81f7bf62006-09-27 18:40:00 -0700228 val ^= (__force u32)fi->fib_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 val ^= fi->fib_priority;
David S. Miller88ebc722008-01-12 21:49:01 -0800230 for_nexthops(fi) {
231 val ^= fib_devindex_hashfn(nh->nh_oif);
232 } endfor_nexthops(fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
235}
236
237static struct fib_info *fib_find_info(const struct fib_info *nfi)
238{
239 struct hlist_head *head;
240 struct hlist_node *node;
241 struct fib_info *fi;
242 unsigned int hash;
243
244 hash = fib_info_hashfn(nfi);
245 head = &fib_info_hash[hash];
246
247 hlist_for_each_entry(fi, node, head, fib_hash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800248 if (!net_eq(fi->fib_net, nfi->fib_net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -0800249 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (fi->fib_nhs != nfi->fib_nhs)
251 continue;
252 if (nfi->fib_protocol == fi->fib_protocol &&
253 nfi->fib_prefsrc == fi->fib_prefsrc &&
254 nfi->fib_priority == fi->fib_priority &&
255 memcmp(nfi->fib_metrics, fi->fib_metrics,
256 sizeof(fi->fib_metrics)) == 0 &&
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000257 ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
259 return fi;
260 }
261
262 return NULL;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/* Check, that the gateway is already configured.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000266 * Used only by redirect accept routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Al Virod878e72e2006-09-26 22:18:13 -0700268int ip_fib_check_default(__be32 gw, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 struct hlist_head *head;
271 struct hlist_node *node;
272 struct fib_nh *nh;
273 unsigned int hash;
274
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700275 spin_lock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 hash = fib_devindex_hashfn(dev->ifindex);
278 head = &fib_info_devhash[hash];
279 hlist_for_each_entry(nh, node, head, nh_hash) {
280 if (nh->nh_dev == dev &&
281 nh->nh_gw == gw &&
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000282 !(nh->nh_flags & RTNH_F_DEAD)) {
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700283 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
285 }
286 }
287
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700288 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 return -1;
291}
292
Thomas Graf339bf982006-11-10 14:10:15 -0800293static inline size_t fib_nlmsg_size(struct fib_info *fi)
294{
295 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
296 + nla_total_size(4) /* RTA_TABLE */
297 + nla_total_size(4) /* RTA_DST */
298 + nla_total_size(4) /* RTA_PRIORITY */
299 + nla_total_size(4); /* RTA_PREFSRC */
300
301 /* space for nested metrics */
302 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
303
304 if (fi->fib_nhs) {
305 /* Also handles the special case fib_nhs == 1 */
306
307 /* each nexthop is packed in an attribute */
308 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
309
310 /* may contain flow and gateway attribute */
311 nhsize += 2 * nla_total_size(4);
312
313 /* all nexthops are packed in a nested attribute */
314 payload += nla_total_size(fi->fib_nhs * nhsize);
315 }
316
317 return payload;
318}
319
Al Viro81f7bf62006-09-27 18:40:00 -0700320void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
Milan Kocianb8f55832007-05-23 14:55:06 -0700321 int dst_len, u32 tb_id, struct nl_info *info,
322 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct sk_buff *skb;
Thomas Graf4e902c52006-08-17 18:14:52 -0700325 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700326 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Thomas Graf339bf982006-11-10 14:10:15 -0800328 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
Thomas Graff21c7bc2006-08-15 00:34:17 -0700329 if (skb == NULL)
330 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Thomas Graf4e902c52006-08-17 18:14:52 -0700332 err = fib_dump_info(skb, info->pid, seq, event, tb_id,
Thomas Grafbe403ea2006-08-17 18:15:17 -0700333 fa->fa_type, fa->fa_scope, key, dst_len,
Milan Kocianb8f55832007-05-23 14:55:06 -0700334 fa->fa_tos, fa->fa_info, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -0800335 if (err < 0) {
336 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
337 WARN_ON(err == -EMSGSIZE);
338 kfree_skb(skb);
339 goto errout;
340 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800341 rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
342 info->nlh, GFP_KERNEL);
343 return;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700344errout:
345 if (err < 0)
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800346 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349/* Return the first fib alias matching TOS with
350 * priority less than or equal to PRIO.
351 */
352struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
353{
354 if (fah) {
355 struct fib_alias *fa;
356 list_for_each_entry(fa, fah, fa_list) {
357 if (fa->fa_tos > tos)
358 continue;
359 if (fa->fa_info->fib_priority >= prio ||
360 fa->fa_tos < tos)
361 return fa;
362 }
363 }
364 return NULL;
365}
366
367int fib_detect_death(struct fib_info *fi, int order,
Denis V. Lunevc17860a2007-12-08 00:22:13 -0800368 struct fib_info **last_resort, int *last_idx, int dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 struct neighbour *n;
371 int state = NUD_NONE;
372
373 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
374 if (n) {
375 state = n->nud_state;
376 neigh_release(n);
377 }
Jianjun Kongd93191002008-11-03 00:23:42 -0800378 if (state == NUD_REACHABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000380 if ((state & NUD_VALID) && order != dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000382 if ((state & NUD_VALID) ||
383 (*last_idx < 0 && order > dflt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 *last_resort = fi;
385 *last_idx = order;
386 }
387 return 1;
388}
389
390#ifdef CONFIG_IP_ROUTE_MULTIPATH
391
Thomas Graf4e902c52006-08-17 18:14:52 -0700392static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 int nhs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Thomas Graf4e902c52006-08-17 18:14:52 -0700396 while (rtnh_ok(rtnh, remaining)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 nhs++;
Thomas Graf4e902c52006-08-17 18:14:52 -0700398 rtnh = rtnh_next(rtnh, &remaining);
399 }
400
401 /* leftover implies invalid nexthop configuration, discard it */
402 return remaining > 0 ? 0 : nhs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Thomas Graf4e902c52006-08-17 18:14:52 -0700405static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
406 int remaining, struct fib_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 change_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700409 int attrlen;
410
411 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700413
David S. Miller71fceff2010-01-15 01:16:40 -0800414 nexthop_nh->nh_flags =
415 (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
416 nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
417 nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700418
419 attrlen = rtnh_attrlen(rtnh);
420 if (attrlen > 0) {
421 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
422
423 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David S. Miller71fceff2010-01-15 01:16:40 -0800424 nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425#ifdef CONFIG_NET_CLS_ROUTE
Thomas Graf4e902c52006-08-17 18:14:52 -0700426 nla = nla_find(attrs, attrlen, RTA_FLOW);
David S. Miller71fceff2010-01-15 01:16:40 -0800427 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428#endif
429 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700430
431 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 } endfor_nexthops(fi);
Thomas Graf4e902c52006-08-17 18:14:52 -0700433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435}
436
437#endif
438
Thomas Graf4e902c52006-08-17 18:14:52 -0700439int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700442 struct rtnexthop *rtnh;
443 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444#endif
445
Thomas Graf4e902c52006-08-17 18:14:52 -0700446 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 1;
448
Thomas Graf4e902c52006-08-17 18:14:52 -0700449 if (cfg->fc_oif || cfg->fc_gw) {
450 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
451 (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return 0;
453 return 1;
454 }
455
456#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700457 if (cfg->fc_mp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700459
460 rtnh = cfg->fc_mp;
461 remaining = cfg->fc_mp_len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 for_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700464 int attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Thomas Graf4e902c52006-08-17 18:14:52 -0700466 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700468
469 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700471
472 attrlen = rtnh_attrlen(rtnh);
473 if (attrlen < 0) {
474 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
475
476 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
Al Viro17fb2c62006-09-26 22:15:25 -0700477 if (nla && nla_get_be32(nla) != nh->nh_gw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return 1;
479#ifdef CONFIG_NET_CLS_ROUTE
Thomas Graf4e902c52006-08-17 18:14:52 -0700480 nla = nla_find(attrs, attrlen, RTA_FLOW);
481 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 1;
483#endif
484 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700485
486 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 } endfor_nexthops(fi);
488#endif
489 return 0;
490}
491
492
493/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000494 * Picture
495 * -------
496 *
497 * Semantics of nexthop is very messy by historical reasons.
498 * We have to take into account, that:
499 * a) gateway can be actually local interface address,
500 * so that gatewayed route is direct.
501 * b) gateway must be on-link address, possibly
502 * described not by an ifaddr, but also by a direct route.
503 * c) If both gateway and interface are specified, they should not
504 * contradict.
505 * d) If we use tunnel routes, gateway could be not on-link.
506 *
507 * Attempt to reconcile all of these (alas, self-contradictory) conditions
508 * results in pretty ugly and hairy code with obscure logic.
509 *
510 * I chose to generalized it instead, so that the size
511 * of code does not increase practically, but it becomes
512 * much more general.
513 * Every prefix is assigned a "scope" value: "host" is local address,
514 * "link" is direct route,
515 * [ ... "site" ... "interior" ... ]
516 * and "universe" is true gateway route with global meaning.
517 *
518 * Every prefix refers to a set of "nexthop"s (gw, oif),
519 * where gw must have narrower scope. This recursion stops
520 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
521 * which means that gw is forced to be on link.
522 *
523 * Code is still hairy, but now it is apparently logically
524 * consistent and very flexible. F.e. as by-product it allows
525 * to co-exists in peace independent exterior and interior
526 * routing processes.
527 *
528 * Normally it looks as following.
529 *
530 * {universe prefix} -> (gw, oif) [scope link]
531 * |
532 * |-> {link prefix} -> (gw, oif) [scope local]
533 * |
534 * |-> {local prefix} (terminal node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 */
Thomas Graf4e902c52006-08-17 18:14:52 -0700536static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
537 struct fib_nh *nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 int err;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800540 struct net *net;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000541 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Denis V. Lunev86167a32008-01-21 17:34:00 -0800543 net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (nh->nh_gw) {
545 struct fib_result res;
546
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000547 if (nh->nh_flags & RTNH_F_ONLINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Thomas Graf4e902c52006-08-17 18:14:52 -0700549 if (cfg->fc_scope >= RT_SCOPE_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return -EINVAL;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800551 if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return -EINVAL;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000553 dev = __dev_get_by_index(net, nh->nh_oif);
554 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return -ENODEV;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000556 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return -ENETDOWN;
558 nh->nh_dev = dev;
559 dev_hold(dev);
560 nh->nh_scope = RT_SCOPE_LINK;
561 return 0;
562 }
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000563 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 {
Thomas Graf4e902c52006-08-17 18:14:52 -0700565 struct flowi fl = {
566 .nl_u = {
567 .ip4_u = {
568 .daddr = nh->nh_gw,
569 .scope = cfg->fc_scope + 1,
570 },
571 },
572 .oif = nh->nh_oif,
573 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /* It is not necessary, but requires a bit of thinking */
576 if (fl.fl4_scope < RT_SCOPE_LINK)
577 fl.fl4_scope = RT_SCOPE_LINK;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000578 err = fib_lookup(net, &fl, &res);
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000579 if (err) {
580 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return err;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 err = -EINVAL;
585 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
586 goto out;
587 nh->nh_scope = res.scope;
588 nh->nh_oif = FIB_RES_OIF(res);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000589 nh->nh_dev = dev = FIB_RES_DEV(res);
590 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 goto out;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000592 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 err = -ENETDOWN;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000594 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 goto out;
596 err = 0;
597out:
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000598 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return err;
600 } else {
601 struct in_device *in_dev;
602
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000603 if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return -EINVAL;
605
Denis V. Lunev86167a32008-01-21 17:34:00 -0800606 in_dev = inetdev_by_index(net, nh->nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (in_dev == NULL)
608 return -ENODEV;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000609 if (!(in_dev->dev->flags & IFF_UP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 in_dev_put(in_dev);
611 return -ENETDOWN;
612 }
613 nh->nh_dev = in_dev->dev;
614 dev_hold(nh->nh_dev);
615 nh->nh_scope = RT_SCOPE_HOST;
616 in_dev_put(in_dev);
617 }
618 return 0;
619}
620
Al Viro81f7bf62006-09-27 18:40:00 -0700621static inline unsigned int fib_laddr_hashfn(__be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 unsigned int mask = (fib_hash_size - 1);
624
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000625 return ((__force u32)val ^
626 ((__force u32)val >> 7) ^
627 ((__force u32)val >> 14)) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630static struct hlist_head *fib_hash_alloc(int bytes)
631{
632 if (bytes <= PAGE_SIZE)
Joonwoo Park88f83492007-11-26 23:29:32 +0800633 return kzalloc(bytes, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 else
635 return (struct hlist_head *)
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000636 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
637 get_order(bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640static void fib_hash_free(struct hlist_head *hash, int bytes)
641{
642 if (!hash)
643 return;
644
645 if (bytes <= PAGE_SIZE)
646 kfree(hash);
647 else
648 free_pages((unsigned long) hash, get_order(bytes));
649}
650
651static void fib_hash_move(struct hlist_head *new_info_hash,
652 struct hlist_head *new_laddrhash,
653 unsigned int new_size)
654{
David S. Millerb7656e72005-08-05 04:12:48 -0700655 struct hlist_head *old_info_hash, *old_laddrhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 unsigned int old_size = fib_hash_size;
David S. Millerb7656e72005-08-05 04:12:48 -0700657 unsigned int i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700659 spin_lock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -0700660 old_info_hash = fib_info_hash;
661 old_laddrhash = fib_info_laddrhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 fib_hash_size = new_size;
663
664 for (i = 0; i < old_size; i++) {
665 struct hlist_head *head = &fib_info_hash[i];
666 struct hlist_node *node, *n;
667 struct fib_info *fi;
668
669 hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
670 struct hlist_head *dest;
671 unsigned int new_hash;
672
673 hlist_del(&fi->fib_hash);
674
675 new_hash = fib_info_hashfn(fi);
676 dest = &new_info_hash[new_hash];
677 hlist_add_head(&fi->fib_hash, dest);
678 }
679 }
680 fib_info_hash = new_info_hash;
681
682 for (i = 0; i < old_size; i++) {
683 struct hlist_head *lhead = &fib_info_laddrhash[i];
684 struct hlist_node *node, *n;
685 struct fib_info *fi;
686
687 hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
688 struct hlist_head *ldest;
689 unsigned int new_hash;
690
691 hlist_del(&fi->fib_lhash);
692
693 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
694 ldest = &new_laddrhash[new_hash];
695 hlist_add_head(&fi->fib_lhash, ldest);
696 }
697 }
698 fib_info_laddrhash = new_laddrhash;
699
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700700 spin_unlock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -0700701
702 bytes = old_size * sizeof(struct hlist_head *);
703 fib_hash_free(old_info_hash, bytes);
704 fib_hash_free(old_laddrhash, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Thomas Graf4e902c52006-08-17 18:14:52 -0700707struct fib_info *fib_create_info(struct fib_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 int err;
710 struct fib_info *fi = NULL;
711 struct fib_info *ofi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int nhs = 1;
Denis V. Lunev7462bd742008-01-31 18:49:32 -0800713 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /* Fast check to catch the most weird cases */
Thomas Graf4e902c52006-08-17 18:14:52 -0700716 if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto err_inval;
718
719#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700720 if (cfg->fc_mp) {
721 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (nhs == 0)
723 goto err_inval;
724 }
725#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 err = -ENOBUFS;
728 if (fib_info_cnt >= fib_hash_size) {
729 unsigned int new_size = fib_hash_size << 1;
730 struct hlist_head *new_info_hash;
731 struct hlist_head *new_laddrhash;
732 unsigned int bytes;
733
734 if (!new_size)
735 new_size = 1;
736 bytes = new_size * sizeof(struct hlist_head *);
737 new_info_hash = fib_hash_alloc(bytes);
738 new_laddrhash = fib_hash_alloc(bytes);
739 if (!new_info_hash || !new_laddrhash) {
740 fib_hash_free(new_info_hash, bytes);
741 fib_hash_free(new_laddrhash, bytes);
Joonwoo Park88f83492007-11-26 23:29:32 +0800742 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 fib_hash_move(new_info_hash, new_laddrhash, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (!fib_hash_size)
746 goto failure;
747 }
748
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700749 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (fi == NULL)
751 goto failure;
752 fib_info_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Denis V. Lunev57d7a602008-04-16 02:00:50 -0700754 fi->fib_net = hold_net(net);
Thomas Graf4e902c52006-08-17 18:14:52 -0700755 fi->fib_protocol = cfg->fc_protocol;
756 fi->fib_flags = cfg->fc_flags;
757 fi->fib_priority = cfg->fc_priority;
758 fi->fib_prefsrc = cfg->fc_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 fi->fib_nhs = nhs;
761 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -0800762 nexthop_nh->nh_parent = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 } endfor_nexthops(fi)
764
Thomas Graf4e902c52006-08-17 18:14:52 -0700765 if (cfg->fc_mx) {
766 struct nlattr *nla;
767 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Thomas Graf4e902c52006-08-17 18:14:52 -0700769 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200770 int type = nla_type(nla);
Thomas Graf4e902c52006-08-17 18:14:52 -0700771
772 if (type) {
773 if (type > RTAX_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 goto err_inval;
Thomas Graf4e902c52006-08-17 18:14:52 -0700775 fi->fib_metrics[type - 1] = nla_get_u32(nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Thomas Graf4e902c52006-08-17 18:14:52 -0700780 if (cfg->fc_mp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700782 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
783 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 goto failure;
Thomas Graf4e902c52006-08-17 18:14:52 -0700785 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto err_inval;
Thomas Graf4e902c52006-08-17 18:14:52 -0700787 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto err_inval;
789#ifdef CONFIG_NET_CLS_ROUTE
Thomas Graf4e902c52006-08-17 18:14:52 -0700790 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 goto err_inval;
792#endif
793#else
794 goto err_inval;
795#endif
796 } else {
797 struct fib_nh *nh = fi->fib_nh;
Thomas Graf4e902c52006-08-17 18:14:52 -0700798
799 nh->nh_oif = cfg->fc_oif;
800 nh->nh_gw = cfg->fc_gw;
801 nh->nh_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802#ifdef CONFIG_NET_CLS_ROUTE
Thomas Graf4e902c52006-08-17 18:14:52 -0700803 nh->nh_tclassid = cfg->fc_flow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805#ifdef CONFIG_IP_ROUTE_MULTIPATH
806 nh->nh_weight = 1;
807#endif
808 }
809
Thomas Graf4e902c52006-08-17 18:14:52 -0700810 if (fib_props[cfg->fc_type].error) {
811 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto err_inval;
813 goto link_it;
814 }
815
Thomas Graf4e902c52006-08-17 18:14:52 -0700816 if (cfg->fc_scope > RT_SCOPE_HOST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 goto err_inval;
818
Thomas Graf4e902c52006-08-17 18:14:52 -0700819 if (cfg->fc_scope == RT_SCOPE_HOST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 struct fib_nh *nh = fi->fib_nh;
821
822 /* Local address is added. */
823 if (nhs != 1 || nh->nh_gw)
824 goto err_inval;
825 nh->nh_scope = RT_SCOPE_NOWHERE;
Denis V. Lunev7462bd742008-01-31 18:49:32 -0800826 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 err = -ENODEV;
828 if (nh->nh_dev == NULL)
829 goto failure;
830 } else {
831 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000832 err = fib_check_nh(cfg, fi, nexthop_nh);
833 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 goto failure;
835 } endfor_nexthops(fi)
836 }
837
838 if (fi->fib_prefsrc) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700839 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
840 fi->fib_prefsrc != cfg->fc_dst)
Denis V. Lunev7462bd742008-01-31 18:49:32 -0800841 if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto err_inval;
843 }
844
845link_it:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000846 ofi = fib_find_info(fi);
847 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 fi->fib_dead = 1;
849 free_fib_info(fi);
850 ofi->fib_treeref++;
851 return ofi;
852 }
853
854 fi->fib_treeref++;
855 atomic_inc(&fi->fib_clntref);
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700856 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 hlist_add_head(&fi->fib_hash,
858 &fib_info_hash[fib_info_hashfn(fi)]);
859 if (fi->fib_prefsrc) {
860 struct hlist_head *head;
861
862 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
863 hlist_add_head(&fi->fib_lhash, head);
864 }
865 change_nexthops(fi) {
866 struct hlist_head *head;
867 unsigned int hash;
868
David S. Miller71fceff2010-01-15 01:16:40 -0800869 if (!nexthop_nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800871 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 head = &fib_info_devhash[hash];
David S. Miller71fceff2010-01-15 01:16:40 -0800873 hlist_add_head(&nexthop_nh->nh_hash, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 } endfor_nexthops(fi)
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700875 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return fi;
877
878err_inval:
879 err = -EINVAL;
880
881failure:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900882 if (fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 fi->fib_dead = 1;
884 free_fib_info(fi);
885 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700886
887 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Robert Olssone5b43762005-08-25 13:01:03 -0700890/* Note! fib_semantic_match intentionally uses RCU list functions. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891int fib_semantic_match(struct list_head *head, const struct flowi *flp,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000892 struct fib_result *res, int prefixlen, int fib_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 struct fib_alias *fa;
895 int nh_sel = 0;
896
Robert Olssone5b43762005-08-25 13:01:03 -0700897 list_for_each_entry_rcu(fa, head, fa_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 int err;
899
900 if (fa->fa_tos &&
901 fa->fa_tos != flp->fl4_tos)
902 continue;
903
904 if (fa->fa_scope < flp->fl4_scope)
905 continue;
906
907 fa->fa_state |= FA_S_ACCESSED;
908
909 err = fib_props[fa->fa_type].error;
910 if (err == 0) {
911 struct fib_info *fi = fa->fa_info;
912
913 if (fi->fib_flags & RTNH_F_DEAD)
914 continue;
915
916 switch (fa->fa_type) {
917 case RTN_UNICAST:
918 case RTN_LOCAL:
919 case RTN_BROADCAST:
920 case RTN_ANYCAST:
921 case RTN_MULTICAST:
922 for_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000923 if (nh->nh_flags & RTNH_F_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 continue;
925 if (!flp->oif || flp->oif == nh->nh_oif)
926 break;
927 }
928#ifdef CONFIG_IP_ROUTE_MULTIPATH
929 if (nhsel < fi->fib_nhs) {
930 nh_sel = nhsel;
931 goto out_fill_res;
932 }
933#else
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000934 if (nhsel < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 goto out_fill_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936#endif
937 endfor_nexthops(fi);
938 continue;
939
940 default:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000941 pr_warning("fib_semantic_match bad type %#x\n",
942 fa->fa_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 return err;
947 }
948 return 1;
949
950out_fill_res:
951 res->prefixlen = prefixlen;
952 res->nh_sel = nh_sel;
953 res->type = fa->fa_type;
954 res->scope = fa->fa_scope;
955 res->fi = fa->fa_info;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000956 if (!(fib_flags & FIB_LOOKUP_NOREF))
957 atomic_inc(&res->fi->fib_clntref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 0;
959}
960
961/* Find appropriate source address to this destination */
962
Al Virob83738a2006-09-26 22:14:15 -0700963__be32 __fib_res_prefsrc(struct fib_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 return inet_select_addr(FIB_RES_DEV(*res), FIB_RES_GW(*res), res->scope);
966}
967
Thomas Grafbe403ea2006-08-17 18:15:17 -0700968int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
Al Viro81f7bf62006-09-27 18:40:00 -0700969 u32 tb_id, u8 type, u8 scope, __be32 dst, int dst_len, u8 tos,
Thomas Grafbe403ea2006-08-17 18:15:17 -0700970 struct fib_info *fi, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Thomas Grafbe403ea2006-08-17 18:15:17 -0700972 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Thomas Grafbe403ea2006-08-17 18:15:17 -0700975 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
976 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800977 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -0700978
979 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 rtm->rtm_family = AF_INET;
981 rtm->rtm_dst_len = dst_len;
982 rtm->rtm_src_len = 0;
983 rtm->rtm_tos = tos;
Krzysztof Piotr Oledzki709772e2008-06-10 15:44:49 -0700984 if (tb_id < 256)
985 rtm->rtm_table = tb_id;
986 else
987 rtm->rtm_table = RT_TABLE_COMPAT;
Thomas Grafbe403ea2006-08-17 18:15:17 -0700988 NLA_PUT_U32(skb, RTA_TABLE, tb_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 rtm->rtm_type = type;
990 rtm->rtm_flags = fi->fib_flags;
991 rtm->rtm_scope = scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 rtm->rtm_protocol = fi->fib_protocol;
Thomas Grafbe403ea2006-08-17 18:15:17 -0700993
994 if (rtm->rtm_dst_len)
Al Viro17fb2c62006-09-26 22:15:25 -0700995 NLA_PUT_BE32(skb, RTA_DST, dst);
Thomas Grafbe403ea2006-08-17 18:15:17 -0700996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (fi->fib_priority)
Thomas Grafbe403ea2006-08-17 18:15:17 -0700998 NLA_PUT_U32(skb, RTA_PRIORITY, fi->fib_priority);
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001001 goto nla_put_failure;
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (fi->fib_prefsrc)
Al Viro17fb2c62006-09-26 22:15:25 -07001004 NLA_PUT_BE32(skb, RTA_PREFSRC, fi->fib_prefsrc);
Thomas Grafbe403ea2006-08-17 18:15:17 -07001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (fi->fib_nhs == 1) {
1007 if (fi->fib_nh->nh_gw)
Al Viro17fb2c62006-09-26 22:15:25 -07001008 NLA_PUT_BE32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw);
Thomas Grafbe403ea2006-08-17 18:15:17 -07001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (fi->fib_nh->nh_oif)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001011 NLA_PUT_U32(skb, RTA_OIF, fi->fib_nh->nh_oif);
Patrick McHardy8265abc2006-07-21 15:09:55 -07001012#ifdef CONFIG_NET_CLS_ROUTE
1013 if (fi->fib_nh[0].nh_tclassid)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001014 NLA_PUT_U32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid);
Patrick McHardy8265abc2006-07-21 15:09:55 -07001015#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017#ifdef CONFIG_IP_ROUTE_MULTIPATH
1018 if (fi->fib_nhs > 1) {
Thomas Grafbe403ea2006-08-17 18:15:17 -07001019 struct rtnexthop *rtnh;
1020 struct nlattr *mp;
1021
1022 mp = nla_nest_start(skb, RTA_MULTIPATH);
1023 if (mp == NULL)
1024 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 for_nexthops(fi) {
Thomas Grafbe403ea2006-08-17 18:15:17 -07001027 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1028 if (rtnh == NULL)
1029 goto nla_put_failure;
1030
1031 rtnh->rtnh_flags = nh->nh_flags & 0xFF;
1032 rtnh->rtnh_hops = nh->nh_weight - 1;
1033 rtnh->rtnh_ifindex = nh->nh_oif;
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (nh->nh_gw)
Al Viro17fb2c62006-09-26 22:15:25 -07001036 NLA_PUT_BE32(skb, RTA_GATEWAY, nh->nh_gw);
Patrick McHardy8265abc2006-07-21 15:09:55 -07001037#ifdef CONFIG_NET_CLS_ROUTE
1038 if (nh->nh_tclassid)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001039 NLA_PUT_U32(skb, RTA_FLOW, nh->nh_tclassid);
Patrick McHardy8265abc2006-07-21 15:09:55 -07001040#endif
Thomas Grafbe403ea2006-08-17 18:15:17 -07001041 /* length of rtnetlink header + attributes */
1042 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 } endfor_nexthops(fi);
Thomas Grafbe403ea2006-08-17 18:15:17 -07001044
1045 nla_nest_end(skb, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047#endif
Thomas Grafbe403ea2006-08-17 18:15:17 -07001048 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Thomas Grafbe403ea2006-08-17 18:15:17 -07001050nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001051 nlmsg_cancel(skb, nlh);
1052 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001056 * Update FIB if:
1057 * - local address disappeared -> we must delete all the entries
1058 * referring to it.
1059 * - device went down -> we must shutdown all nexthops going via it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 */
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001061int fib_sync_down_addr(struct net *net, __be32 local)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001062{
1063 int ret = 0;
1064 unsigned int hash = fib_laddr_hashfn(local);
1065 struct hlist_head *head = &fib_info_laddrhash[hash];
1066 struct hlist_node *node;
1067 struct fib_info *fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001069 if (fib_info_laddrhash == NULL || local == 0)
1070 return 0;
1071
1072 hlist_for_each_entry(fi, node, head, fib_lhash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001073 if (!net_eq(fi->fib_net, net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001074 continue;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001075 if (fi->fib_prefsrc == local) {
1076 fi->fib_flags |= RTNH_F_DEAD;
1077 ret++;
1078 }
1079 }
1080 return ret;
1081}
1082
1083int fib_sync_down_dev(struct net_device *dev, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 int ret = 0;
1086 int scope = RT_SCOPE_NOWHERE;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001087 struct fib_info *prev_fi = NULL;
1088 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1089 struct hlist_head *head = &fib_info_devhash[hash];
1090 struct hlist_node *node;
1091 struct fib_nh *nh;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (force)
1094 scope = -1;
1095
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001096 hlist_for_each_entry(nh, node, head, nh_hash) {
1097 struct fib_info *fi = nh->nh_parent;
1098 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001100 BUG_ON(!fi->fib_nhs);
1101 if (nh->nh_dev != dev || fi == prev_fi)
1102 continue;
1103 prev_fi = fi;
1104 dead = 0;
1105 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001106 if (nexthop_nh->nh_flags & RTNH_F_DEAD)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001107 dead++;
David S. Miller71fceff2010-01-15 01:16:40 -08001108 else if (nexthop_nh->nh_dev == dev &&
1109 nexthop_nh->nh_scope != scope) {
1110 nexthop_nh->nh_flags |= RTNH_F_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111#ifdef CONFIG_IP_ROUTE_MULTIPATH
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001112 spin_lock_bh(&fib_multipath_lock);
David S. Miller71fceff2010-01-15 01:16:40 -08001113 fi->fib_power -= nexthop_nh->nh_power;
1114 nexthop_nh->nh_power = 0;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001115 spin_unlock_bh(&fib_multipath_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116#endif
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001117 dead++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001119#ifdef CONFIG_IP_ROUTE_MULTIPATH
David S. Miller71fceff2010-01-15 01:16:40 -08001120 if (force > 1 && nexthop_nh->nh_dev == dev) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001121 dead = fi->fib_nhs;
1122 break;
1123 }
1124#endif
1125 } endfor_nexthops(fi)
1126 if (dead == fi->fib_nhs) {
1127 fi->fib_flags |= RTNH_F_DEAD;
1128 ret++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 }
1131
1132 return ret;
1133}
1134
1135#ifdef CONFIG_IP_ROUTE_MULTIPATH
1136
1137/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001138 * Dead device goes up. We wake up dead nexthops.
1139 * It takes sense only on multipath routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141int fib_sync_up(struct net_device *dev)
1142{
1143 struct fib_info *prev_fi;
1144 unsigned int hash;
1145 struct hlist_head *head;
1146 struct hlist_node *node;
1147 struct fib_nh *nh;
1148 int ret;
1149
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001150 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return 0;
1152
1153 prev_fi = NULL;
1154 hash = fib_devindex_hashfn(dev->ifindex);
1155 head = &fib_info_devhash[hash];
1156 ret = 0;
1157
1158 hlist_for_each_entry(nh, node, head, nh_hash) {
1159 struct fib_info *fi = nh->nh_parent;
1160 int alive;
1161
1162 BUG_ON(!fi->fib_nhs);
1163 if (nh->nh_dev != dev || fi == prev_fi)
1164 continue;
1165
1166 prev_fi = fi;
1167 alive = 0;
1168 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001169 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 alive++;
1171 continue;
1172 }
David S. Miller71fceff2010-01-15 01:16:40 -08001173 if (nexthop_nh->nh_dev == NULL ||
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001174 !(nexthop_nh->nh_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 continue;
David S. Miller71fceff2010-01-15 01:16:40 -08001176 if (nexthop_nh->nh_dev != dev ||
1177 !__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 continue;
1179 alive++;
1180 spin_lock_bh(&fib_multipath_lock);
David S. Miller71fceff2010-01-15 01:16:40 -08001181 nexthop_nh->nh_power = 0;
1182 nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 spin_unlock_bh(&fib_multipath_lock);
1184 } endfor_nexthops(fi)
1185
1186 if (alive > 0) {
1187 fi->fib_flags &= ~RTNH_F_DEAD;
1188 ret++;
1189 }
1190 }
1191
1192 return ret;
1193}
1194
1195/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001196 * The algorithm is suboptimal, but it provides really
1197 * fair weighted route distribution.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199void fib_select_multipath(const struct flowi *flp, struct fib_result *res)
1200{
1201 struct fib_info *fi = res->fi;
1202 int w;
1203
1204 spin_lock_bh(&fib_multipath_lock);
1205 if (fi->fib_power <= 0) {
1206 int power = 0;
1207 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001208 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
David S. Miller71fceff2010-01-15 01:16:40 -08001209 power += nexthop_nh->nh_weight;
1210 nexthop_nh->nh_power = nexthop_nh->nh_weight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212 } endfor_nexthops(fi);
1213 fi->fib_power = power;
1214 if (power <= 0) {
1215 spin_unlock_bh(&fib_multipath_lock);
1216 /* Race condition: route has just become dead. */
1217 res->nh_sel = 0;
1218 return;
1219 }
1220 }
1221
1222
1223 /* w should be random number [0..fi->fib_power-1],
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001224 * it is pretty bad approximation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 */
1226
1227 w = jiffies % fi->fib_power;
1228
1229 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001230 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
David S. Miller71fceff2010-01-15 01:16:40 -08001231 nexthop_nh->nh_power) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001232 w -= nexthop_nh->nh_power;
1233 if (w <= 0) {
David S. Miller71fceff2010-01-15 01:16:40 -08001234 nexthop_nh->nh_power--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 fi->fib_power--;
1236 res->nh_sel = nhsel;
1237 spin_unlock_bh(&fib_multipath_lock);
1238 return;
1239 }
1240 }
1241 } endfor_nexthops(fi);
1242
1243 /* Race condition: route has just become dead. */
1244 res->nh_sel = 0;
1245 spin_unlock_bh(&fib_multipath_lock);
1246}
1247#endif