fib_semantics.c 29 KB

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