tcp: resets are misrouted
[firefly-linux-kernel-4.4.55.git] / net / ipv4 / fib_semantics.c
1 /*
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  *
8  * 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
16 #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>
29 #include <linux/inetdevice.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/proc_fs.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
36
37 #include <net/arp.h>
38 #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>
44 #include <net/netlink.h>
45 #include <net/nexthop.h>
46
47 #include "fib_lookup.h"
48
49 static DEFINE_SPINLOCK(fib_info_lock);
50 static struct hlist_head *fib_info_hash;
51 static struct hlist_head *fib_info_laddrhash;
52 static unsigned int fib_info_hash_size;
53 static unsigned int fib_info_cnt;
54
55 #define DEVINDEX_HASHBITS 8
56 #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
57 static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
58
59 #ifdef CONFIG_IP_ROUTE_MULTIPATH
60
61 static DEFINE_SPINLOCK(fib_multipath_lock);
62
63 #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++)
68
69 #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++)
74
75 #else /* CONFIG_IP_ROUTE_MULTIPATH */
76
77 /* Hope, that gcc will optimize it to get rid of dummy loop */
78
79 #define for_nexthops(fi) {                                              \
80         int nhsel; const struct fib_nh *nh = (fi)->fib_nh;              \
81         for (nhsel = 0; nhsel < 1; nhsel++)
82
83 #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++)
87
88 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
89
90 #define endfor_nexthops(fi) }
91
92
93 const struct fib_prop fib_props[RTN_MAX + 1] = {
94         [RTN_UNSPEC] = {
95                 .error  = 0,
96                 .scope  = RT_SCOPE_NOWHERE,
97         },
98         [RTN_UNICAST] = {
99                 .error  = 0,
100                 .scope  = RT_SCOPE_UNIVERSE,
101         },
102         [RTN_LOCAL] = {
103                 .error  = 0,
104                 .scope  = RT_SCOPE_HOST,
105         },
106         [RTN_BROADCAST] = {
107                 .error  = 0,
108                 .scope  = RT_SCOPE_LINK,
109         },
110         [RTN_ANYCAST] = {
111                 .error  = 0,
112                 .scope  = RT_SCOPE_LINK,
113         },
114         [RTN_MULTICAST] = {
115                 .error  = 0,
116                 .scope  = RT_SCOPE_UNIVERSE,
117         },
118         [RTN_BLACKHOLE] = {
119                 .error  = -EINVAL,
120                 .scope  = RT_SCOPE_UNIVERSE,
121         },
122         [RTN_UNREACHABLE] = {
123                 .error  = -EHOSTUNREACH,
124                 .scope  = RT_SCOPE_UNIVERSE,
125         },
126         [RTN_PROHIBIT] = {
127                 .error  = -EACCES,
128                 .scope  = RT_SCOPE_UNIVERSE,
129         },
130         [RTN_THROW] = {
131                 .error  = -EAGAIN,
132                 .scope  = RT_SCOPE_UNIVERSE,
133         },
134         [RTN_NAT] = {
135                 .error  = -EINVAL,
136                 .scope  = RT_SCOPE_NOWHERE,
137         },
138         [RTN_XRESOLVE] = {
139                 .error  = -EINVAL,
140                 .scope  = RT_SCOPE_NOWHERE,
141         },
142 };
143
144 /* Release a nexthop info record */
145 static void free_fib_info_rcu(struct rcu_head *head)
146 {
147         struct fib_info *fi = container_of(head, struct fib_info, rcu);
148
149         change_nexthops(fi) {
150                 if (nexthop_nh->nh_dev)
151                         dev_put(nexthop_nh->nh_dev);
152         } endfor_nexthops(fi);
153
154         release_net(fi->fib_net);
155         kfree(fi);
156 }
157
158 void free_fib_info(struct fib_info *fi)
159 {
160         if (fi->fib_dead == 0) {
161                 pr_warning("Freeing alive fib_info %p\n", fi);
162                 return;
163         }
164         fib_info_cnt--;
165         call_rcu(&fi->rcu, free_fib_info_rcu);
166 }
167
168 void fib_release_info(struct fib_info *fi)
169 {
170         spin_lock_bh(&fib_info_lock);
171         if (fi && --fi->fib_treeref == 0) {
172                 hlist_del(&fi->fib_hash);
173                 if (fi->fib_prefsrc)
174                         hlist_del(&fi->fib_lhash);
175                 change_nexthops(fi) {
176                         if (!nexthop_nh->nh_dev)
177                                 continue;
178                         hlist_del(&nexthop_nh->nh_hash);
179                 } endfor_nexthops(fi)
180                 fi->fib_dead = 1;
181                 fib_info_put(fi);
182         }
183         spin_unlock_bh(&fib_info_lock);
184 }
185
186 static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
187 {
188         const struct fib_nh *onh = ofi->fib_nh;
189
190         for_nexthops(fi) {
191                 if (nh->nh_oif != onh->nh_oif ||
192                     nh->nh_gw  != onh->nh_gw ||
193                     nh->nh_scope != onh->nh_scope ||
194 #ifdef CONFIG_IP_ROUTE_MULTIPATH
195                     nh->nh_weight != onh->nh_weight ||
196 #endif
197 #ifdef CONFIG_IP_ROUTE_CLASSID
198                     nh->nh_tclassid != onh->nh_tclassid ||
199 #endif
200                     ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
201                         return -1;
202                 onh++;
203         } endfor_nexthops(fi);
204         return 0;
205 }
206
207 static inline unsigned int fib_devindex_hashfn(unsigned int val)
208 {
209         unsigned int mask = DEVINDEX_HASHSIZE - 1;
210
211         return (val ^
212                 (val >> DEVINDEX_HASHBITS) ^
213                 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
214 }
215
216 static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
217 {
218         unsigned int mask = (fib_info_hash_size - 1);
219         unsigned int val = fi->fib_nhs;
220
221         val ^= (fi->fib_protocol << 8) | fi->fib_scope;
222         val ^= (__force u32)fi->fib_prefsrc;
223         val ^= fi->fib_priority;
224         for_nexthops(fi) {
225                 val ^= fib_devindex_hashfn(nh->nh_oif);
226         } endfor_nexthops(fi)
227
228         return (val ^ (val >> 7) ^ (val >> 12)) & mask;
229 }
230
231 static struct fib_info *fib_find_info(const struct fib_info *nfi)
232 {
233         struct hlist_head *head;
234         struct hlist_node *node;
235         struct fib_info *fi;
236         unsigned int hash;
237
238         hash = fib_info_hashfn(nfi);
239         head = &fib_info_hash[hash];
240
241         hlist_for_each_entry(fi, node, head, fib_hash) {
242                 if (!net_eq(fi->fib_net, nfi->fib_net))
243                         continue;
244                 if (fi->fib_nhs != nfi->fib_nhs)
245                         continue;
246                 if (nfi->fib_protocol == fi->fib_protocol &&
247                     nfi->fib_scope == fi->fib_scope &&
248                     nfi->fib_prefsrc == fi->fib_prefsrc &&
249                     nfi->fib_priority == fi->fib_priority &&
250                     memcmp(nfi->fib_metrics, fi->fib_metrics,
251                            sizeof(u32) * RTAX_MAX) == 0 &&
252                     ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
253                     (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
254                         return fi;
255         }
256
257         return NULL;
258 }
259
260 /* Check, that the gateway is already configured.
261  * Used only by redirect accept routine.
262  */
263 int ip_fib_check_default(__be32 gw, struct net_device *dev)
264 {
265         struct hlist_head *head;
266         struct hlist_node *node;
267         struct fib_nh *nh;
268         unsigned int hash;
269
270         spin_lock(&fib_info_lock);
271
272         hash = fib_devindex_hashfn(dev->ifindex);
273         head = &fib_info_devhash[hash];
274         hlist_for_each_entry(nh, node, head, nh_hash) {
275                 if (nh->nh_dev == dev &&
276                     nh->nh_gw == gw &&
277                     !(nh->nh_flags & RTNH_F_DEAD)) {
278                         spin_unlock(&fib_info_lock);
279                         return 0;
280                 }
281         }
282
283         spin_unlock(&fib_info_lock);
284
285         return -1;
286 }
287
288 static inline size_t fib_nlmsg_size(struct fib_info *fi)
289 {
290         size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
291                          + nla_total_size(4) /* RTA_TABLE */
292                          + nla_total_size(4) /* RTA_DST */
293                          + nla_total_size(4) /* RTA_PRIORITY */
294                          + nla_total_size(4); /* RTA_PREFSRC */
295
296         /* space for nested metrics */
297         payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
298
299         if (fi->fib_nhs) {
300                 /* Also handles the special case fib_nhs == 1 */
301
302                 /* each nexthop is packed in an attribute */
303                 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
304
305                 /* may contain flow and gateway attribute */
306                 nhsize += 2 * nla_total_size(4);
307
308                 /* all nexthops are packed in a nested attribute */
309                 payload += nla_total_size(fi->fib_nhs * nhsize);
310         }
311
312         return payload;
313 }
314
315 void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
316                int dst_len, u32 tb_id, struct nl_info *info,
317                unsigned int nlm_flags)
318 {
319         struct sk_buff *skb;
320         u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
321         int err = -ENOBUFS;
322
323         skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
324         if (skb == NULL)
325                 goto errout;
326
327         err = fib_dump_info(skb, info->pid, seq, event, tb_id,
328                             fa->fa_type, key, dst_len,
329                             fa->fa_tos, fa->fa_info, nlm_flags);
330         if (err < 0) {
331                 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
332                 WARN_ON(err == -EMSGSIZE);
333                 kfree_skb(skb);
334                 goto errout;
335         }
336         rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
337                     info->nlh, GFP_KERNEL);
338         return;
339 errout:
340         if (err < 0)
341                 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
342 }
343
344 /* Return the first fib alias matching TOS with
345  * priority less than or equal to PRIO.
346  */
347 struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
348 {
349         if (fah) {
350                 struct fib_alias *fa;
351                 list_for_each_entry(fa, fah, fa_list) {
352                         if (fa->fa_tos > tos)
353                                 continue;
354                         if (fa->fa_info->fib_priority >= prio ||
355                             fa->fa_tos < tos)
356                                 return fa;
357                 }
358         }
359         return NULL;
360 }
361
362 int fib_detect_death(struct fib_info *fi, int order,
363                      struct fib_info **last_resort, int *last_idx, int dflt)
364 {
365         struct neighbour *n;
366         int state = NUD_NONE;
367
368         n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
369         if (n) {
370                 state = n->nud_state;
371                 neigh_release(n);
372         }
373         if (state == NUD_REACHABLE)
374                 return 0;
375         if ((state & NUD_VALID) && order != dflt)
376                 return 0;
377         if ((state & NUD_VALID) ||
378             (*last_idx < 0 && order > dflt)) {
379                 *last_resort = fi;
380                 *last_idx = order;
381         }
382         return 1;
383 }
384
385 #ifdef CONFIG_IP_ROUTE_MULTIPATH
386
387 static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
388 {
389         int nhs = 0;
390
391         while (rtnh_ok(rtnh, remaining)) {
392                 nhs++;
393                 rtnh = rtnh_next(rtnh, &remaining);
394         }
395
396         /* leftover implies invalid nexthop configuration, discard it */
397         return remaining > 0 ? 0 : nhs;
398 }
399
400 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
401                        int remaining, struct fib_config *cfg)
402 {
403         change_nexthops(fi) {
404                 int attrlen;
405
406                 if (!rtnh_ok(rtnh, remaining))
407                         return -EINVAL;
408
409                 nexthop_nh->nh_flags =
410                         (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
411                 nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
412                 nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
413
414                 attrlen = rtnh_attrlen(rtnh);
415                 if (attrlen > 0) {
416                         struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
417
418                         nla = nla_find(attrs, attrlen, RTA_GATEWAY);
419                         nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
420 #ifdef CONFIG_IP_ROUTE_CLASSID
421                         nla = nla_find(attrs, attrlen, RTA_FLOW);
422                         nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
423 #endif
424                 }
425
426                 rtnh = rtnh_next(rtnh, &remaining);
427         } endfor_nexthops(fi);
428
429         return 0;
430 }
431
432 #endif
433
434 int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
435 {
436 #ifdef CONFIG_IP_ROUTE_MULTIPATH
437         struct rtnexthop *rtnh;
438         int remaining;
439 #endif
440
441         if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
442                 return 1;
443
444         if (cfg->fc_oif || cfg->fc_gw) {
445                 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
446                     (!cfg->fc_gw  || cfg->fc_gw == fi->fib_nh->nh_gw))
447                         return 0;
448                 return 1;
449         }
450
451 #ifdef CONFIG_IP_ROUTE_MULTIPATH
452         if (cfg->fc_mp == NULL)
453                 return 0;
454
455         rtnh = cfg->fc_mp;
456         remaining = cfg->fc_mp_len;
457
458         for_nexthops(fi) {
459                 int attrlen;
460
461                 if (!rtnh_ok(rtnh, remaining))
462                         return -EINVAL;
463
464                 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
465                         return 1;
466
467                 attrlen = rtnh_attrlen(rtnh);
468                 if (attrlen < 0) {
469                         struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
470
471                         nla = nla_find(attrs, attrlen, RTA_GATEWAY);
472                         if (nla && nla_get_be32(nla) != nh->nh_gw)
473                                 return 1;
474 #ifdef CONFIG_IP_ROUTE_CLASSID
475                         nla = nla_find(attrs, attrlen, RTA_FLOW);
476                         if (nla && nla_get_u32(nla) != nh->nh_tclassid)
477                                 return 1;
478 #endif
479                 }
480
481                 rtnh = rtnh_next(rtnh, &remaining);
482         } endfor_nexthops(fi);
483 #endif
484         return 0;
485 }
486
487
488 /*
489  * Picture
490  * -------
491  *
492  * Semantics of nexthop is very messy by historical reasons.
493  * We have to take into account, that:
494  * a) gateway can be actually local interface address,
495  *    so that gatewayed route is direct.
496  * b) gateway must be on-link address, possibly
497  *    described not by an ifaddr, but also by a direct route.
498  * c) If both gateway and interface are specified, they should not
499  *    contradict.
500  * d) If we use tunnel routes, gateway could be not on-link.
501  *
502  * Attempt to reconcile all of these (alas, self-contradictory) conditions
503  * results in pretty ugly and hairy code with obscure logic.
504  *
505  * I chose to generalized it instead, so that the size
506  * of code does not increase practically, but it becomes
507  * much more general.
508  * Every prefix is assigned a "scope" value: "host" is local address,
509  * "link" is direct route,
510  * [ ... "site" ... "interior" ... ]
511  * and "universe" is true gateway route with global meaning.
512  *
513  * Every prefix refers to a set of "nexthop"s (gw, oif),
514  * where gw must have narrower scope. This recursion stops
515  * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
516  * which means that gw is forced to be on link.
517  *
518  * Code is still hairy, but now it is apparently logically
519  * consistent and very flexible. F.e. as by-product it allows
520  * to co-exists in peace independent exterior and interior
521  * routing processes.
522  *
523  * Normally it looks as following.
524  *
525  * {universe prefix}  -> (gw, oif) [scope link]
526  *                |
527  *                |-> {link prefix} -> (gw, oif) [scope local]
528  *                                      |
529  *                                      |-> {local prefix} (terminal node)
530  */
531 static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
532                         struct fib_nh *nh)
533 {
534         int err;
535         struct net *net;
536         struct net_device *dev;
537
538         net = cfg->fc_nlinfo.nl_net;
539         if (nh->nh_gw) {
540                 struct fib_result res;
541
542                 if (nh->nh_flags & RTNH_F_ONLINK) {
543
544                         if (cfg->fc_scope >= RT_SCOPE_LINK)
545                                 return -EINVAL;
546                         if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
547                                 return -EINVAL;
548                         dev = __dev_get_by_index(net, nh->nh_oif);
549                         if (!dev)
550                                 return -ENODEV;
551                         if (!(dev->flags & IFF_UP))
552                                 return -ENETDOWN;
553                         nh->nh_dev = dev;
554                         dev_hold(dev);
555                         nh->nh_scope = RT_SCOPE_LINK;
556                         return 0;
557                 }
558                 rcu_read_lock();
559                 {
560                         struct flowi4 fl4 = {
561                                 .daddr = nh->nh_gw,
562                                 .flowi4_scope = cfg->fc_scope + 1,
563                                 .flowi4_oif = nh->nh_oif,
564                         };
565
566                         /* It is not necessary, but requires a bit of thinking */
567                         if (fl4.flowi4_scope < RT_SCOPE_LINK)
568                                 fl4.flowi4_scope = RT_SCOPE_LINK;
569                         err = fib_lookup(net, &fl4, &res);
570                         if (err) {
571                                 rcu_read_unlock();
572                                 return err;
573                         }
574                 }
575                 err = -EINVAL;
576                 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
577                         goto out;
578                 nh->nh_scope = res.scope;
579                 nh->nh_oif = FIB_RES_OIF(res);
580                 nh->nh_dev = dev = FIB_RES_DEV(res);
581                 if (!dev)
582                         goto out;
583                 dev_hold(dev);
584                 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
585         } else {
586                 struct in_device *in_dev;
587
588                 if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
589                         return -EINVAL;
590
591                 rcu_read_lock();
592                 err = -ENODEV;
593                 in_dev = inetdev_by_index(net, nh->nh_oif);
594                 if (in_dev == NULL)
595                         goto out;
596                 err = -ENETDOWN;
597                 if (!(in_dev->dev->flags & IFF_UP))
598                         goto out;
599                 nh->nh_dev = in_dev->dev;
600                 dev_hold(nh->nh_dev);
601                 nh->nh_scope = RT_SCOPE_HOST;
602                 err = 0;
603         }
604 out:
605         rcu_read_unlock();
606         return err;
607 }
608
609 static inline unsigned int fib_laddr_hashfn(__be32 val)
610 {
611         unsigned int mask = (fib_info_hash_size - 1);
612
613         return ((__force u32)val ^
614                 ((__force u32)val >> 7) ^
615                 ((__force u32)val >> 14)) & mask;
616 }
617
618 static struct hlist_head *fib_info_hash_alloc(int bytes)
619 {
620         if (bytes <= PAGE_SIZE)
621                 return kzalloc(bytes, GFP_KERNEL);
622         else
623                 return (struct hlist_head *)
624                         __get_free_pages(GFP_KERNEL | __GFP_ZERO,
625                                          get_order(bytes));
626 }
627
628 static void fib_info_hash_free(struct hlist_head *hash, int bytes)
629 {
630         if (!hash)
631                 return;
632
633         if (bytes <= PAGE_SIZE)
634                 kfree(hash);
635         else
636                 free_pages((unsigned long) hash, get_order(bytes));
637 }
638
639 static void fib_info_hash_move(struct hlist_head *new_info_hash,
640                                struct hlist_head *new_laddrhash,
641                                unsigned int new_size)
642 {
643         struct hlist_head *old_info_hash, *old_laddrhash;
644         unsigned int old_size = fib_info_hash_size;
645         unsigned int i, bytes;
646
647         spin_lock_bh(&fib_info_lock);
648         old_info_hash = fib_info_hash;
649         old_laddrhash = fib_info_laddrhash;
650         fib_info_hash_size = new_size;
651
652         for (i = 0; i < old_size; i++) {
653                 struct hlist_head *head = &fib_info_hash[i];
654                 struct hlist_node *node, *n;
655                 struct fib_info *fi;
656
657                 hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
658                         struct hlist_head *dest;
659                         unsigned int new_hash;
660
661                         hlist_del(&fi->fib_hash);
662
663                         new_hash = fib_info_hashfn(fi);
664                         dest = &new_info_hash[new_hash];
665                         hlist_add_head(&fi->fib_hash, dest);
666                 }
667         }
668         fib_info_hash = new_info_hash;
669
670         for (i = 0; i < old_size; i++) {
671                 struct hlist_head *lhead = &fib_info_laddrhash[i];
672                 struct hlist_node *node, *n;
673                 struct fib_info *fi;
674
675                 hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
676                         struct hlist_head *ldest;
677                         unsigned int new_hash;
678
679                         hlist_del(&fi->fib_lhash);
680
681                         new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
682                         ldest = &new_laddrhash[new_hash];
683                         hlist_add_head(&fi->fib_lhash, ldest);
684                 }
685         }
686         fib_info_laddrhash = new_laddrhash;
687
688         spin_unlock_bh(&fib_info_lock);
689
690         bytes = old_size * sizeof(struct hlist_head *);
691         fib_info_hash_free(old_info_hash, bytes);
692         fib_info_hash_free(old_laddrhash, bytes);
693 }
694
695 __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
696 {
697         nh->nh_saddr = inet_select_addr(nh->nh_dev,
698                                         nh->nh_gw,
699                                         nh->nh_parent->fib_scope);
700         nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
701
702         return nh->nh_saddr;
703 }
704
705 struct fib_info *fib_create_info(struct fib_config *cfg)
706 {
707         int err;
708         struct fib_info *fi = NULL;
709         struct fib_info *ofi;
710         int nhs = 1;
711         struct net *net = cfg->fc_nlinfo.nl_net;
712
713         if (cfg->fc_type > RTN_MAX)
714                 goto err_inval;
715
716         /* Fast check to catch the most weird cases */
717         if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
718                 goto err_inval;
719
720 #ifdef CONFIG_IP_ROUTE_MULTIPATH
721         if (cfg->fc_mp) {
722                 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
723                 if (nhs == 0)
724                         goto err_inval;
725         }
726 #endif
727
728         err = -ENOBUFS;
729         if (fib_info_cnt >= fib_info_hash_size) {
730                 unsigned int new_size = fib_info_hash_size << 1;
731                 struct hlist_head *new_info_hash;
732                 struct hlist_head *new_laddrhash;
733                 unsigned int bytes;
734
735                 if (!new_size)
736                         new_size = 1;
737                 bytes = new_size * sizeof(struct hlist_head *);
738                 new_info_hash = fib_info_hash_alloc(bytes);
739                 new_laddrhash = fib_info_hash_alloc(bytes);
740                 if (!new_info_hash || !new_laddrhash) {
741                         fib_info_hash_free(new_info_hash, bytes);
742                         fib_info_hash_free(new_laddrhash, bytes);
743                 } else
744                         fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
745
746                 if (!fib_info_hash_size)
747                         goto failure;
748         }
749
750         fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
751         if (fi == NULL)
752                 goto failure;
753         if (cfg->fc_mx) {
754                 fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
755                 if (!fi->fib_metrics)
756                         goto failure;
757         } else
758                 fi->fib_metrics = (u32 *) dst_default_metrics;
759         fib_info_cnt++;
760
761         fi->fib_net = hold_net(net);
762         fi->fib_protocol = cfg->fc_protocol;
763         fi->fib_scope = cfg->fc_scope;
764         fi->fib_flags = cfg->fc_flags;
765         fi->fib_priority = cfg->fc_priority;
766         fi->fib_prefsrc = cfg->fc_prefsrc;
767
768         fi->fib_nhs = nhs;
769         change_nexthops(fi) {
770                 nexthop_nh->nh_parent = fi;
771         } endfor_nexthops(fi)
772
773         if (cfg->fc_mx) {
774                 struct nlattr *nla;
775                 int remaining;
776
777                 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
778                         int type = nla_type(nla);
779
780                         if (type) {
781                                 if (type > RTAX_MAX)
782                                         goto err_inval;
783                                 fi->fib_metrics[type - 1] = nla_get_u32(nla);
784                         }
785                 }
786         }
787
788         if (cfg->fc_mp) {
789 #ifdef CONFIG_IP_ROUTE_MULTIPATH
790                 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
791                 if (err != 0)
792                         goto failure;
793                 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
794                         goto err_inval;
795                 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
796                         goto err_inval;
797 #ifdef CONFIG_IP_ROUTE_CLASSID
798                 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
799                         goto err_inval;
800 #endif
801 #else
802                 goto err_inval;
803 #endif
804         } else {
805                 struct fib_nh *nh = fi->fib_nh;
806
807                 nh->nh_oif = cfg->fc_oif;
808                 nh->nh_gw = cfg->fc_gw;
809                 nh->nh_flags = cfg->fc_flags;
810 #ifdef CONFIG_IP_ROUTE_CLASSID
811                 nh->nh_tclassid = cfg->fc_flow;
812 #endif
813 #ifdef CONFIG_IP_ROUTE_MULTIPATH
814                 nh->nh_weight = 1;
815 #endif
816         }
817
818         if (fib_props[cfg->fc_type].error) {
819                 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
820                         goto err_inval;
821                 goto link_it;
822         } else {
823                 switch (cfg->fc_type) {
824                 case RTN_UNICAST:
825                 case RTN_LOCAL:
826                 case RTN_BROADCAST:
827                 case RTN_ANYCAST:
828                 case RTN_MULTICAST:
829                         break;
830                 default:
831                         goto err_inval;
832                 }
833         }
834
835         if (cfg->fc_scope > RT_SCOPE_HOST)
836                 goto err_inval;
837
838         if (cfg->fc_scope == RT_SCOPE_HOST) {
839                 struct fib_nh *nh = fi->fib_nh;
840
841                 /* Local address is added. */
842                 if (nhs != 1 || nh->nh_gw)
843                         goto err_inval;
844                 nh->nh_scope = RT_SCOPE_NOWHERE;
845                 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
846                 err = -ENODEV;
847                 if (nh->nh_dev == NULL)
848                         goto failure;
849         } else {
850                 change_nexthops(fi) {
851                         err = fib_check_nh(cfg, fi, nexthop_nh);
852                         if (err != 0)
853                                 goto failure;
854                 } endfor_nexthops(fi)
855         }
856
857         if (fi->fib_prefsrc) {
858                 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
859                     fi->fib_prefsrc != cfg->fc_dst)
860                         if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
861                                 goto err_inval;
862         }
863
864         change_nexthops(fi) {
865                 fib_info_update_nh_saddr(net, nexthop_nh);
866         } endfor_nexthops(fi)
867
868 link_it:
869         ofi = fib_find_info(fi);
870         if (ofi) {
871                 fi->fib_dead = 1;
872                 free_fib_info(fi);
873                 ofi->fib_treeref++;
874                 return ofi;
875         }
876
877         fi->fib_treeref++;
878         atomic_inc(&fi->fib_clntref);
879         spin_lock_bh(&fib_info_lock);
880         hlist_add_head(&fi->fib_hash,
881                        &fib_info_hash[fib_info_hashfn(fi)]);
882         if (fi->fib_prefsrc) {
883                 struct hlist_head *head;
884
885                 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
886                 hlist_add_head(&fi->fib_lhash, head);
887         }
888         change_nexthops(fi) {
889                 struct hlist_head *head;
890                 unsigned int hash;
891
892                 if (!nexthop_nh->nh_dev)
893                         continue;
894                 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
895                 head = &fib_info_devhash[hash];
896                 hlist_add_head(&nexthop_nh->nh_hash, head);
897         } endfor_nexthops(fi)
898         spin_unlock_bh(&fib_info_lock);
899         return fi;
900
901 err_inval:
902         err = -EINVAL;
903
904 failure:
905         if (fi) {
906                 fi->fib_dead = 1;
907                 free_fib_info(fi);
908         }
909
910         return ERR_PTR(err);
911 }
912
913 int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
914                   u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
915                   struct fib_info *fi, unsigned int flags)
916 {
917         struct nlmsghdr *nlh;
918         struct rtmsg *rtm;
919
920         nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
921         if (nlh == NULL)
922                 return -EMSGSIZE;
923
924         rtm = nlmsg_data(nlh);
925         rtm->rtm_family = AF_INET;
926         rtm->rtm_dst_len = dst_len;
927         rtm->rtm_src_len = 0;
928         rtm->rtm_tos = tos;
929         if (tb_id < 256)
930                 rtm->rtm_table = tb_id;
931         else
932                 rtm->rtm_table = RT_TABLE_COMPAT;
933         NLA_PUT_U32(skb, RTA_TABLE, tb_id);
934         rtm->rtm_type = type;
935         rtm->rtm_flags = fi->fib_flags;
936         rtm->rtm_scope = fi->fib_scope;
937         rtm->rtm_protocol = fi->fib_protocol;
938
939         if (rtm->rtm_dst_len)
940                 NLA_PUT_BE32(skb, RTA_DST, dst);
941
942         if (fi->fib_priority)
943                 NLA_PUT_U32(skb, RTA_PRIORITY, fi->fib_priority);
944
945         if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
946                 goto nla_put_failure;
947
948         if (fi->fib_prefsrc)
949                 NLA_PUT_BE32(skb, RTA_PREFSRC, fi->fib_prefsrc);
950
951         if (fi->fib_nhs == 1) {
952                 if (fi->fib_nh->nh_gw)
953                         NLA_PUT_BE32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw);
954
955                 if (fi->fib_nh->nh_oif)
956                         NLA_PUT_U32(skb, RTA_OIF, fi->fib_nh->nh_oif);
957 #ifdef CONFIG_IP_ROUTE_CLASSID
958                 if (fi->fib_nh[0].nh_tclassid)
959                         NLA_PUT_U32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid);
960 #endif
961         }
962 #ifdef CONFIG_IP_ROUTE_MULTIPATH
963         if (fi->fib_nhs > 1) {
964                 struct rtnexthop *rtnh;
965                 struct nlattr *mp;
966
967                 mp = nla_nest_start(skb, RTA_MULTIPATH);
968                 if (mp == NULL)
969                         goto nla_put_failure;
970
971                 for_nexthops(fi) {
972                         rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
973                         if (rtnh == NULL)
974                                 goto nla_put_failure;
975
976                         rtnh->rtnh_flags = nh->nh_flags & 0xFF;
977                         rtnh->rtnh_hops = nh->nh_weight - 1;
978                         rtnh->rtnh_ifindex = nh->nh_oif;
979
980                         if (nh->nh_gw)
981                                 NLA_PUT_BE32(skb, RTA_GATEWAY, nh->nh_gw);
982 #ifdef CONFIG_IP_ROUTE_CLASSID
983                         if (nh->nh_tclassid)
984                                 NLA_PUT_U32(skb, RTA_FLOW, nh->nh_tclassid);
985 #endif
986                         /* length of rtnetlink header + attributes */
987                         rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
988                 } endfor_nexthops(fi);
989
990                 nla_nest_end(skb, mp);
991         }
992 #endif
993         return nlmsg_end(skb, nlh);
994
995 nla_put_failure:
996         nlmsg_cancel(skb, nlh);
997         return -EMSGSIZE;
998 }
999
1000 /*
1001  * Update FIB if:
1002  * - local address disappeared -> we must delete all the entries
1003  *   referring to it.
1004  * - device went down -> we must shutdown all nexthops going via it.
1005  */
1006 int fib_sync_down_addr(struct net *net, __be32 local)
1007 {
1008         int ret = 0;
1009         unsigned int hash = fib_laddr_hashfn(local);
1010         struct hlist_head *head = &fib_info_laddrhash[hash];
1011         struct hlist_node *node;
1012         struct fib_info *fi;
1013
1014         if (fib_info_laddrhash == NULL || local == 0)
1015                 return 0;
1016
1017         hlist_for_each_entry(fi, node, head, fib_lhash) {
1018                 if (!net_eq(fi->fib_net, net))
1019                         continue;
1020                 if (fi->fib_prefsrc == local) {
1021                         fi->fib_flags |= RTNH_F_DEAD;
1022                         ret++;
1023                 }
1024         }
1025         return ret;
1026 }
1027
1028 int fib_sync_down_dev(struct net_device *dev, int force)
1029 {
1030         int ret = 0;
1031         int scope = RT_SCOPE_NOWHERE;
1032         struct fib_info *prev_fi = NULL;
1033         unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1034         struct hlist_head *head = &fib_info_devhash[hash];
1035         struct hlist_node *node;
1036         struct fib_nh *nh;
1037
1038         if (force)
1039                 scope = -1;
1040
1041         hlist_for_each_entry(nh, node, head, nh_hash) {
1042                 struct fib_info *fi = nh->nh_parent;
1043                 int dead;
1044
1045                 BUG_ON(!fi->fib_nhs);
1046                 if (nh->nh_dev != dev || fi == prev_fi)
1047                         continue;
1048                 prev_fi = fi;
1049                 dead = 0;
1050                 change_nexthops(fi) {
1051                         if (nexthop_nh->nh_flags & RTNH_F_DEAD)
1052                                 dead++;
1053                         else if (nexthop_nh->nh_dev == dev &&
1054                                  nexthop_nh->nh_scope != scope) {
1055                                 nexthop_nh->nh_flags |= RTNH_F_DEAD;
1056 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1057                                 spin_lock_bh(&fib_multipath_lock);
1058                                 fi->fib_power -= nexthop_nh->nh_power;
1059                                 nexthop_nh->nh_power = 0;
1060                                 spin_unlock_bh(&fib_multipath_lock);
1061 #endif
1062                                 dead++;
1063                         }
1064 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1065                         if (force > 1 && nexthop_nh->nh_dev == dev) {
1066                                 dead = fi->fib_nhs;
1067                                 break;
1068                         }
1069 #endif
1070                 } endfor_nexthops(fi)
1071                 if (dead == fi->fib_nhs) {
1072                         fi->fib_flags |= RTNH_F_DEAD;
1073                         ret++;
1074                 }
1075         }
1076
1077         return ret;
1078 }
1079
1080 /* Must be invoked inside of an RCU protected region.  */
1081 void fib_select_default(struct fib_result *res)
1082 {
1083         struct fib_info *fi = NULL, *last_resort = NULL;
1084         struct list_head *fa_head = res->fa_head;
1085         struct fib_table *tb = res->table;
1086         int order = -1, last_idx = -1;
1087         struct fib_alias *fa;
1088
1089         list_for_each_entry_rcu(fa, fa_head, fa_list) {
1090                 struct fib_info *next_fi = fa->fa_info;
1091
1092                 if (next_fi->fib_scope != res->scope ||
1093                     fa->fa_type != RTN_UNICAST)
1094                         continue;
1095
1096                 if (next_fi->fib_priority > res->fi->fib_priority)
1097                         break;
1098                 if (!next_fi->fib_nh[0].nh_gw ||
1099                     next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
1100                         continue;
1101
1102                 fib_alias_accessed(fa);
1103
1104                 if (fi == NULL) {
1105                         if (next_fi != res->fi)
1106                                 break;
1107                 } else if (!fib_detect_death(fi, order, &last_resort,
1108                                              &last_idx, tb->tb_default)) {
1109                         fib_result_assign(res, fi);
1110                         tb->tb_default = order;
1111                         goto out;
1112                 }
1113                 fi = next_fi;
1114                 order++;
1115         }
1116
1117         if (order <= 0 || fi == NULL) {
1118                 tb->tb_default = -1;
1119                 goto out;
1120         }
1121
1122         if (!fib_detect_death(fi, order, &last_resort, &last_idx,
1123                                 tb->tb_default)) {
1124                 fib_result_assign(res, fi);
1125                 tb->tb_default = order;
1126                 goto out;
1127         }
1128
1129         if (last_idx >= 0)
1130                 fib_result_assign(res, last_resort);
1131         tb->tb_default = last_idx;
1132 out:
1133         return;
1134 }
1135
1136 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1137
1138 /*
1139  * Dead device goes up. We wake up dead nexthops.
1140  * It takes sense only on multipath routes.
1141  */
1142 int fib_sync_up(struct net_device *dev)
1143 {
1144         struct fib_info *prev_fi;
1145         unsigned int hash;
1146         struct hlist_head *head;
1147         struct hlist_node *node;
1148         struct fib_nh *nh;
1149         int ret;
1150
1151         if (!(dev->flags & IFF_UP))
1152                 return 0;
1153
1154         prev_fi = NULL;
1155         hash = fib_devindex_hashfn(dev->ifindex);
1156         head = &fib_info_devhash[hash];
1157         ret = 0;
1158
1159         hlist_for_each_entry(nh, node, head, nh_hash) {
1160                 struct fib_info *fi = nh->nh_parent;
1161                 int alive;
1162
1163                 BUG_ON(!fi->fib_nhs);
1164                 if (nh->nh_dev != dev || fi == prev_fi)
1165                         continue;
1166
1167                 prev_fi = fi;
1168                 alive = 0;
1169                 change_nexthops(fi) {
1170                         if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
1171                                 alive++;
1172                                 continue;
1173                         }
1174                         if (nexthop_nh->nh_dev == NULL ||
1175                             !(nexthop_nh->nh_dev->flags & IFF_UP))
1176                                 continue;
1177                         if (nexthop_nh->nh_dev != dev ||
1178                             !__in_dev_get_rtnl(dev))
1179                                 continue;
1180                         alive++;
1181                         spin_lock_bh(&fib_multipath_lock);
1182                         nexthop_nh->nh_power = 0;
1183                         nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
1184                         spin_unlock_bh(&fib_multipath_lock);
1185                 } endfor_nexthops(fi)
1186
1187                 if (alive > 0) {
1188                         fi->fib_flags &= ~RTNH_F_DEAD;
1189                         ret++;
1190                 }
1191         }
1192
1193         return ret;
1194 }
1195
1196 /*
1197  * The algorithm is suboptimal, but it provides really
1198  * fair weighted route distribution.
1199  */
1200 void fib_select_multipath(struct fib_result *res)
1201 {
1202         struct fib_info *fi = res->fi;
1203         int w;
1204
1205         spin_lock_bh(&fib_multipath_lock);
1206         if (fi->fib_power <= 0) {
1207                 int power = 0;
1208                 change_nexthops(fi) {
1209                         if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
1210                                 power += nexthop_nh->nh_weight;
1211                                 nexthop_nh->nh_power = nexthop_nh->nh_weight;
1212                         }
1213                 } endfor_nexthops(fi);
1214                 fi->fib_power = power;
1215                 if (power <= 0) {
1216                         spin_unlock_bh(&fib_multipath_lock);
1217                         /* Race condition: route has just become dead. */
1218                         res->nh_sel = 0;
1219                         return;
1220                 }
1221         }
1222
1223
1224         /* w should be random number [0..fi->fib_power-1],
1225          * it is pretty bad approximation.
1226          */
1227
1228         w = jiffies % fi->fib_power;
1229
1230         change_nexthops(fi) {
1231                 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
1232                     nexthop_nh->nh_power) {
1233                         w -= nexthop_nh->nh_power;
1234                         if (w <= 0) {
1235                                 nexthop_nh->nh_power--;
1236                                 fi->fib_power--;
1237                                 res->nh_sel = nhsel;
1238                                 spin_unlock_bh(&fib_multipath_lock);
1239                                 return;
1240                         }
1241                 }
1242         } endfor_nexthops(fi);
1243
1244         /* Race condition: route has just become dead. */
1245         res->nh_sel = 0;
1246         spin_unlock_bh(&fib_multipath_lock);
1247 }
1248 #endif