fib_semantics.c 31 KB

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