fib_semantics.c 30 KB

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