fib_semantics.c 31 KB

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