fib_semantics.c 30 KB

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