fib_semantics.c 30 KB

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