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