fib_semantics.c 29 KB

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