fib_semantics.c 30 KB

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