fib_semantics.c 30 KB

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