fib_semantics.c 31 KB

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