fib_semantics.c 30 KB

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