fib_semantics.c 31 KB

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