fib_semantics.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  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 <net/lwtunnel.h>
  44. #include "fib_lookup.h"
  45. static DEFINE_SPINLOCK(fib_info_lock);
  46. static struct hlist_head *fib_info_hash;
  47. static struct hlist_head *fib_info_laddrhash;
  48. static unsigned int fib_info_hash_size;
  49. static unsigned int fib_info_cnt;
  50. #define DEVINDEX_HASHBITS 8
  51. #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
  52. static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
  53. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  54. u32 fib_multipath_secret __read_mostly;
  55. #define for_nexthops(fi) { \
  56. int nhsel; const struct fib_nh *nh; \
  57. for (nhsel = 0, nh = (fi)->fib_nh; \
  58. nhsel < (fi)->fib_nhs; \
  59. nh++, nhsel++)
  60. #define change_nexthops(fi) { \
  61. int nhsel; struct fib_nh *nexthop_nh; \
  62. for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
  63. nhsel < (fi)->fib_nhs; \
  64. nexthop_nh++, nhsel++)
  65. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  66. /* Hope, that gcc will optimize it to get rid of dummy loop */
  67. #define for_nexthops(fi) { \
  68. int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
  69. for (nhsel = 0; nhsel < 1; nhsel++)
  70. #define change_nexthops(fi) { \
  71. int nhsel; \
  72. struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
  73. for (nhsel = 0; nhsel < 1; nhsel++)
  74. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  75. #define endfor_nexthops(fi) }
  76. const struct fib_prop fib_props[RTN_MAX + 1] = {
  77. [RTN_UNSPEC] = {
  78. .error = 0,
  79. .scope = RT_SCOPE_NOWHERE,
  80. },
  81. [RTN_UNICAST] = {
  82. .error = 0,
  83. .scope = RT_SCOPE_UNIVERSE,
  84. },
  85. [RTN_LOCAL] = {
  86. .error = 0,
  87. .scope = RT_SCOPE_HOST,
  88. },
  89. [RTN_BROADCAST] = {
  90. .error = 0,
  91. .scope = RT_SCOPE_LINK,
  92. },
  93. [RTN_ANYCAST] = {
  94. .error = 0,
  95. .scope = RT_SCOPE_LINK,
  96. },
  97. [RTN_MULTICAST] = {
  98. .error = 0,
  99. .scope = RT_SCOPE_UNIVERSE,
  100. },
  101. [RTN_BLACKHOLE] = {
  102. .error = -EINVAL,
  103. .scope = RT_SCOPE_UNIVERSE,
  104. },
  105. [RTN_UNREACHABLE] = {
  106. .error = -EHOSTUNREACH,
  107. .scope = RT_SCOPE_UNIVERSE,
  108. },
  109. [RTN_PROHIBIT] = {
  110. .error = -EACCES,
  111. .scope = RT_SCOPE_UNIVERSE,
  112. },
  113. [RTN_THROW] = {
  114. .error = -EAGAIN,
  115. .scope = RT_SCOPE_UNIVERSE,
  116. },
  117. [RTN_NAT] = {
  118. .error = -EINVAL,
  119. .scope = RT_SCOPE_NOWHERE,
  120. },
  121. [RTN_XRESOLVE] = {
  122. .error = -EINVAL,
  123. .scope = RT_SCOPE_NOWHERE,
  124. },
  125. };
  126. static void rt_fibinfo_free(struct rtable __rcu **rtp)
  127. {
  128. struct rtable *rt = rcu_dereference_protected(*rtp, 1);
  129. if (!rt)
  130. return;
  131. /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
  132. * because we waited an RCU grace period before calling
  133. * free_fib_info_rcu()
  134. */
  135. dst_free(&rt->dst);
  136. }
  137. static void free_nh_exceptions(struct fib_nh *nh)
  138. {
  139. struct fnhe_hash_bucket *hash;
  140. int i;
  141. hash = rcu_dereference_protected(nh->nh_exceptions, 1);
  142. if (!hash)
  143. return;
  144. for (i = 0; i < FNHE_HASH_SIZE; i++) {
  145. struct fib_nh_exception *fnhe;
  146. fnhe = rcu_dereference_protected(hash[i].chain, 1);
  147. while (fnhe) {
  148. struct fib_nh_exception *next;
  149. next = rcu_dereference_protected(fnhe->fnhe_next, 1);
  150. rt_fibinfo_free(&fnhe->fnhe_rth_input);
  151. rt_fibinfo_free(&fnhe->fnhe_rth_output);
  152. kfree(fnhe);
  153. fnhe = next;
  154. }
  155. }
  156. kfree(hash);
  157. }
  158. static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
  159. {
  160. int cpu;
  161. if (!rtp)
  162. return;
  163. for_each_possible_cpu(cpu) {
  164. struct rtable *rt;
  165. rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
  166. if (rt)
  167. dst_free(&rt->dst);
  168. }
  169. free_percpu(rtp);
  170. }
  171. /* Release a nexthop info record */
  172. static void free_fib_info_rcu(struct rcu_head *head)
  173. {
  174. struct fib_info *fi = container_of(head, struct fib_info, rcu);
  175. change_nexthops(fi) {
  176. if (nexthop_nh->nh_dev)
  177. dev_put(nexthop_nh->nh_dev);
  178. lwtstate_put(nexthop_nh->nh_lwtstate);
  179. free_nh_exceptions(nexthop_nh);
  180. rt_fibinfo_free_cpus(nexthop_nh->nh_pcpu_rth_output);
  181. rt_fibinfo_free(&nexthop_nh->nh_rth_input);
  182. } endfor_nexthops(fi);
  183. if (fi->fib_metrics != (u32 *) dst_default_metrics)
  184. kfree(fi->fib_metrics);
  185. kfree(fi);
  186. }
  187. void free_fib_info(struct fib_info *fi)
  188. {
  189. if (fi->fib_dead == 0) {
  190. pr_warn("Freeing alive fib_info %p\n", fi);
  191. return;
  192. }
  193. fib_info_cnt--;
  194. #ifdef CONFIG_IP_ROUTE_CLASSID
  195. change_nexthops(fi) {
  196. if (nexthop_nh->nh_tclassid)
  197. fi->fib_net->ipv4.fib_num_tclassid_users--;
  198. } endfor_nexthops(fi);
  199. #endif
  200. call_rcu(&fi->rcu, free_fib_info_rcu);
  201. }
  202. void fib_release_info(struct fib_info *fi)
  203. {
  204. spin_lock_bh(&fib_info_lock);
  205. if (fi && --fi->fib_treeref == 0) {
  206. hlist_del(&fi->fib_hash);
  207. if (fi->fib_prefsrc)
  208. hlist_del(&fi->fib_lhash);
  209. change_nexthops(fi) {
  210. if (!nexthop_nh->nh_dev)
  211. continue;
  212. hlist_del(&nexthop_nh->nh_hash);
  213. } endfor_nexthops(fi)
  214. fi->fib_dead = 1;
  215. fib_info_put(fi);
  216. }
  217. spin_unlock_bh(&fib_info_lock);
  218. }
  219. static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
  220. {
  221. const struct fib_nh *onh = ofi->fib_nh;
  222. for_nexthops(fi) {
  223. if (nh->nh_oif != onh->nh_oif ||
  224. nh->nh_gw != onh->nh_gw ||
  225. nh->nh_scope != onh->nh_scope ||
  226. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  227. nh->nh_weight != onh->nh_weight ||
  228. #endif
  229. #ifdef CONFIG_IP_ROUTE_CLASSID
  230. nh->nh_tclassid != onh->nh_tclassid ||
  231. #endif
  232. lwtunnel_cmp_encap(nh->nh_lwtstate, onh->nh_lwtstate) ||
  233. ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_COMPARE_MASK))
  234. return -1;
  235. onh++;
  236. } endfor_nexthops(fi);
  237. return 0;
  238. }
  239. static inline unsigned int fib_devindex_hashfn(unsigned int val)
  240. {
  241. unsigned int mask = DEVINDEX_HASHSIZE - 1;
  242. return (val ^
  243. (val >> DEVINDEX_HASHBITS) ^
  244. (val >> (DEVINDEX_HASHBITS * 2))) & mask;
  245. }
  246. static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
  247. {
  248. unsigned int mask = (fib_info_hash_size - 1);
  249. unsigned int val = fi->fib_nhs;
  250. val ^= (fi->fib_protocol << 8) | fi->fib_scope;
  251. val ^= (__force u32)fi->fib_prefsrc;
  252. val ^= fi->fib_priority;
  253. for_nexthops(fi) {
  254. val ^= fib_devindex_hashfn(nh->nh_oif);
  255. } endfor_nexthops(fi)
  256. return (val ^ (val >> 7) ^ (val >> 12)) & mask;
  257. }
  258. static struct fib_info *fib_find_info(const struct fib_info *nfi)
  259. {
  260. struct hlist_head *head;
  261. struct fib_info *fi;
  262. unsigned int hash;
  263. hash = fib_info_hashfn(nfi);
  264. head = &fib_info_hash[hash];
  265. hlist_for_each_entry(fi, head, fib_hash) {
  266. if (!net_eq(fi->fib_net, nfi->fib_net))
  267. continue;
  268. if (fi->fib_nhs != nfi->fib_nhs)
  269. continue;
  270. if (nfi->fib_protocol == fi->fib_protocol &&
  271. nfi->fib_scope == fi->fib_scope &&
  272. nfi->fib_prefsrc == fi->fib_prefsrc &&
  273. nfi->fib_priority == fi->fib_priority &&
  274. nfi->fib_type == fi->fib_type &&
  275. memcmp(nfi->fib_metrics, fi->fib_metrics,
  276. sizeof(u32) * RTAX_MAX) == 0 &&
  277. !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
  278. (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
  279. return fi;
  280. }
  281. return NULL;
  282. }
  283. /* Check, that the gateway is already configured.
  284. * Used only by redirect accept routine.
  285. */
  286. int ip_fib_check_default(__be32 gw, struct net_device *dev)
  287. {
  288. struct hlist_head *head;
  289. struct fib_nh *nh;
  290. unsigned int hash;
  291. spin_lock(&fib_info_lock);
  292. hash = fib_devindex_hashfn(dev->ifindex);
  293. head = &fib_info_devhash[hash];
  294. hlist_for_each_entry(nh, head, nh_hash) {
  295. if (nh->nh_dev == dev &&
  296. nh->nh_gw == gw &&
  297. !(nh->nh_flags & RTNH_F_DEAD)) {
  298. spin_unlock(&fib_info_lock);
  299. return 0;
  300. }
  301. }
  302. spin_unlock(&fib_info_lock);
  303. return -1;
  304. }
  305. static inline size_t fib_nlmsg_size(struct fib_info *fi)
  306. {
  307. size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
  308. + nla_total_size(4) /* RTA_TABLE */
  309. + nla_total_size(4) /* RTA_DST */
  310. + nla_total_size(4) /* RTA_PRIORITY */
  311. + nla_total_size(4) /* RTA_PREFSRC */
  312. + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
  313. /* space for nested metrics */
  314. payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
  315. if (fi->fib_nhs) {
  316. size_t nh_encapsize = 0;
  317. /* Also handles the special case fib_nhs == 1 */
  318. /* each nexthop is packed in an attribute */
  319. size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
  320. /* may contain flow and gateway attribute */
  321. nhsize += 2 * nla_total_size(4);
  322. /* grab encap info */
  323. for_nexthops(fi) {
  324. if (nh->nh_lwtstate) {
  325. /* RTA_ENCAP_TYPE */
  326. nh_encapsize += lwtunnel_get_encap_size(
  327. nh->nh_lwtstate);
  328. /* RTA_ENCAP */
  329. nh_encapsize += nla_total_size(2);
  330. }
  331. } endfor_nexthops(fi);
  332. /* all nexthops are packed in a nested attribute */
  333. payload += nla_total_size((fi->fib_nhs * nhsize) +
  334. nh_encapsize);
  335. }
  336. return payload;
  337. }
  338. void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
  339. int dst_len, u32 tb_id, const struct nl_info *info,
  340. unsigned int nlm_flags)
  341. {
  342. struct sk_buff *skb;
  343. u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
  344. int err = -ENOBUFS;
  345. skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
  346. if (!skb)
  347. goto errout;
  348. err = fib_dump_info(skb, info->portid, seq, event, tb_id,
  349. fa->fa_type, key, dst_len,
  350. fa->fa_tos, fa->fa_info, nlm_flags);
  351. if (err < 0) {
  352. /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
  353. WARN_ON(err == -EMSGSIZE);
  354. kfree_skb(skb);
  355. goto errout;
  356. }
  357. rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
  358. info->nlh, GFP_KERNEL);
  359. return;
  360. errout:
  361. if (err < 0)
  362. rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
  363. }
  364. static int fib_detect_death(struct fib_info *fi, int order,
  365. struct fib_info **last_resort, int *last_idx,
  366. int dflt)
  367. {
  368. struct neighbour *n;
  369. int state = NUD_NONE;
  370. n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
  371. if (n) {
  372. state = n->nud_state;
  373. neigh_release(n);
  374. } else {
  375. return 0;
  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 && state != NUD_INCOMPLETE)) {
  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. struct net *net = cfg->fc_nlinfo.nl_net;
  403. int ret;
  404. change_nexthops(fi) {
  405. int attrlen;
  406. if (!rtnh_ok(rtnh, remaining))
  407. return -EINVAL;
  408. if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  409. return -EINVAL;
  410. nexthop_nh->nh_flags =
  411. (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
  412. nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
  413. nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
  414. attrlen = rtnh_attrlen(rtnh);
  415. if (attrlen > 0) {
  416. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  417. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  418. nexthop_nh->nh_gw = nla ? nla_get_in_addr(nla) : 0;
  419. #ifdef CONFIG_IP_ROUTE_CLASSID
  420. nla = nla_find(attrs, attrlen, RTA_FLOW);
  421. nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
  422. if (nexthop_nh->nh_tclassid)
  423. fi->fib_net->ipv4.fib_num_tclassid_users++;
  424. #endif
  425. nla = nla_find(attrs, attrlen, RTA_ENCAP);
  426. if (nla) {
  427. struct lwtunnel_state *lwtstate;
  428. struct net_device *dev = NULL;
  429. struct nlattr *nla_entype;
  430. nla_entype = nla_find(attrs, attrlen,
  431. RTA_ENCAP_TYPE);
  432. if (!nla_entype)
  433. goto err_inval;
  434. if (cfg->fc_oif)
  435. dev = __dev_get_by_index(net, cfg->fc_oif);
  436. ret = lwtunnel_build_state(dev, nla_get_u16(
  437. nla_entype),
  438. nla, AF_INET, cfg,
  439. &lwtstate);
  440. if (ret)
  441. goto errout;
  442. nexthop_nh->nh_lwtstate =
  443. lwtstate_get(lwtstate);
  444. }
  445. }
  446. rtnh = rtnh_next(rtnh, &remaining);
  447. } endfor_nexthops(fi);
  448. return 0;
  449. err_inval:
  450. ret = -EINVAL;
  451. errout:
  452. return ret;
  453. }
  454. static void fib_rebalance(struct fib_info *fi)
  455. {
  456. int total;
  457. int w;
  458. struct in_device *in_dev;
  459. if (fi->fib_nhs < 2)
  460. return;
  461. total = 0;
  462. for_nexthops(fi) {
  463. if (nh->nh_flags & RTNH_F_DEAD)
  464. continue;
  465. in_dev = __in_dev_get_rtnl(nh->nh_dev);
  466. if (in_dev &&
  467. IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
  468. nh->nh_flags & RTNH_F_LINKDOWN)
  469. continue;
  470. total += nh->nh_weight;
  471. } endfor_nexthops(fi);
  472. w = 0;
  473. change_nexthops(fi) {
  474. int upper_bound;
  475. in_dev = __in_dev_get_rtnl(nexthop_nh->nh_dev);
  476. if (nexthop_nh->nh_flags & RTNH_F_DEAD) {
  477. upper_bound = -1;
  478. } else if (in_dev &&
  479. IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
  480. nexthop_nh->nh_flags & RTNH_F_LINKDOWN) {
  481. upper_bound = -1;
  482. } else {
  483. w += nexthop_nh->nh_weight;
  484. upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
  485. total) - 1;
  486. }
  487. atomic_set(&nexthop_nh->nh_upper_bound, upper_bound);
  488. } endfor_nexthops(fi);
  489. net_get_random_once(&fib_multipath_secret,
  490. sizeof(fib_multipath_secret));
  491. }
  492. static inline void fib_add_weight(struct fib_info *fi,
  493. const struct fib_nh *nh)
  494. {
  495. fi->fib_weight += nh->nh_weight;
  496. }
  497. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  498. #define fib_rebalance(fi) do { } while (0)
  499. #define fib_add_weight(fi, nh) do { } while (0)
  500. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  501. static int fib_encap_match(struct net *net, u16 encap_type,
  502. struct nlattr *encap,
  503. int oif, const struct fib_nh *nh,
  504. const struct fib_config *cfg)
  505. {
  506. struct lwtunnel_state *lwtstate;
  507. struct net_device *dev = NULL;
  508. int ret, result = 0;
  509. if (encap_type == LWTUNNEL_ENCAP_NONE)
  510. return 0;
  511. if (oif)
  512. dev = __dev_get_by_index(net, oif);
  513. ret = lwtunnel_build_state(dev, encap_type, encap,
  514. AF_INET, cfg, &lwtstate);
  515. if (!ret) {
  516. result = lwtunnel_cmp_encap(lwtstate, nh->nh_lwtstate);
  517. lwtstate_free(lwtstate);
  518. }
  519. return result;
  520. }
  521. int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
  522. {
  523. struct net *net = cfg->fc_nlinfo.nl_net;
  524. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  525. struct rtnexthop *rtnh;
  526. int remaining;
  527. #endif
  528. if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
  529. return 1;
  530. if (cfg->fc_oif || cfg->fc_gw) {
  531. if (cfg->fc_encap) {
  532. if (fib_encap_match(net, cfg->fc_encap_type,
  533. cfg->fc_encap, cfg->fc_oif,
  534. fi->fib_nh, cfg))
  535. return 1;
  536. }
  537. if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
  538. (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
  539. return 0;
  540. return 1;
  541. }
  542. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  543. if (!cfg->fc_mp)
  544. return 0;
  545. rtnh = cfg->fc_mp;
  546. remaining = cfg->fc_mp_len;
  547. for_nexthops(fi) {
  548. int attrlen;
  549. if (!rtnh_ok(rtnh, remaining))
  550. return -EINVAL;
  551. if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
  552. return 1;
  553. attrlen = rtnh_attrlen(rtnh);
  554. if (attrlen > 0) {
  555. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  556. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  557. if (nla && nla_get_in_addr(nla) != nh->nh_gw)
  558. return 1;
  559. #ifdef CONFIG_IP_ROUTE_CLASSID
  560. nla = nla_find(attrs, attrlen, RTA_FLOW);
  561. if (nla && nla_get_u32(nla) != nh->nh_tclassid)
  562. return 1;
  563. #endif
  564. }
  565. rtnh = rtnh_next(rtnh, &remaining);
  566. } endfor_nexthops(fi);
  567. #endif
  568. return 0;
  569. }
  570. /*
  571. * Picture
  572. * -------
  573. *
  574. * Semantics of nexthop is very messy by historical reasons.
  575. * We have to take into account, that:
  576. * a) gateway can be actually local interface address,
  577. * so that gatewayed route is direct.
  578. * b) gateway must be on-link address, possibly
  579. * described not by an ifaddr, but also by a direct route.
  580. * c) If both gateway and interface are specified, they should not
  581. * contradict.
  582. * d) If we use tunnel routes, gateway could be not on-link.
  583. *
  584. * Attempt to reconcile all of these (alas, self-contradictory) conditions
  585. * results in pretty ugly and hairy code with obscure logic.
  586. *
  587. * I chose to generalized it instead, so that the size
  588. * of code does not increase practically, but it becomes
  589. * much more general.
  590. * Every prefix is assigned a "scope" value: "host" is local address,
  591. * "link" is direct route,
  592. * [ ... "site" ... "interior" ... ]
  593. * and "universe" is true gateway route with global meaning.
  594. *
  595. * Every prefix refers to a set of "nexthop"s (gw, oif),
  596. * where gw must have narrower scope. This recursion stops
  597. * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
  598. * which means that gw is forced to be on link.
  599. *
  600. * Code is still hairy, but now it is apparently logically
  601. * consistent and very flexible. F.e. as by-product it allows
  602. * to co-exists in peace independent exterior and interior
  603. * routing processes.
  604. *
  605. * Normally it looks as following.
  606. *
  607. * {universe prefix} -> (gw, oif) [scope link]
  608. * |
  609. * |-> {link prefix} -> (gw, oif) [scope local]
  610. * |
  611. * |-> {local prefix} (terminal node)
  612. */
  613. static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
  614. struct fib_nh *nh)
  615. {
  616. int err = 0;
  617. struct net *net;
  618. struct net_device *dev;
  619. net = cfg->fc_nlinfo.nl_net;
  620. if (nh->nh_gw) {
  621. struct fib_result res;
  622. if (nh->nh_flags & RTNH_F_ONLINK) {
  623. unsigned int addr_type;
  624. if (cfg->fc_scope >= RT_SCOPE_LINK)
  625. return -EINVAL;
  626. dev = __dev_get_by_index(net, nh->nh_oif);
  627. if (!dev)
  628. return -ENODEV;
  629. if (!(dev->flags & IFF_UP))
  630. return -ENETDOWN;
  631. addr_type = inet_addr_type_dev_table(net, dev, nh->nh_gw);
  632. if (addr_type != RTN_UNICAST)
  633. return -EINVAL;
  634. if (!netif_carrier_ok(dev))
  635. nh->nh_flags |= RTNH_F_LINKDOWN;
  636. nh->nh_dev = dev;
  637. dev_hold(dev);
  638. nh->nh_scope = RT_SCOPE_LINK;
  639. return 0;
  640. }
  641. rcu_read_lock();
  642. {
  643. struct fib_table *tbl = NULL;
  644. struct flowi4 fl4 = {
  645. .daddr = nh->nh_gw,
  646. .flowi4_scope = cfg->fc_scope + 1,
  647. .flowi4_oif = nh->nh_oif,
  648. .flowi4_iif = LOOPBACK_IFINDEX,
  649. };
  650. /* It is not necessary, but requires a bit of thinking */
  651. if (fl4.flowi4_scope < RT_SCOPE_LINK)
  652. fl4.flowi4_scope = RT_SCOPE_LINK;
  653. if (cfg->fc_table)
  654. tbl = fib_get_table(net, cfg->fc_table);
  655. if (tbl)
  656. err = fib_table_lookup(tbl, &fl4, &res,
  657. FIB_LOOKUP_IGNORE_LINKSTATE |
  658. FIB_LOOKUP_NOREF);
  659. /* on error or if no table given do full lookup. This
  660. * is needed for example when nexthops are in the local
  661. * table rather than the given table
  662. */
  663. if (!tbl || err) {
  664. err = fib_lookup(net, &fl4, &res,
  665. FIB_LOOKUP_IGNORE_LINKSTATE);
  666. }
  667. if (err) {
  668. rcu_read_unlock();
  669. return err;
  670. }
  671. }
  672. err = -EINVAL;
  673. if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
  674. goto out;
  675. nh->nh_scope = res.scope;
  676. nh->nh_oif = FIB_RES_OIF(res);
  677. nh->nh_dev = dev = FIB_RES_DEV(res);
  678. if (!dev)
  679. goto out;
  680. dev_hold(dev);
  681. if (!netif_carrier_ok(dev))
  682. nh->nh_flags |= RTNH_F_LINKDOWN;
  683. err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
  684. } else {
  685. struct in_device *in_dev;
  686. if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
  687. return -EINVAL;
  688. rcu_read_lock();
  689. err = -ENODEV;
  690. in_dev = inetdev_by_index(net, nh->nh_oif);
  691. if (!in_dev)
  692. goto out;
  693. err = -ENETDOWN;
  694. if (!(in_dev->dev->flags & IFF_UP))
  695. goto out;
  696. nh->nh_dev = in_dev->dev;
  697. dev_hold(nh->nh_dev);
  698. nh->nh_scope = RT_SCOPE_HOST;
  699. if (!netif_carrier_ok(nh->nh_dev))
  700. nh->nh_flags |= RTNH_F_LINKDOWN;
  701. err = 0;
  702. }
  703. out:
  704. rcu_read_unlock();
  705. return err;
  706. }
  707. static inline unsigned int fib_laddr_hashfn(__be32 val)
  708. {
  709. unsigned int mask = (fib_info_hash_size - 1);
  710. return ((__force u32)val ^
  711. ((__force u32)val >> 7) ^
  712. ((__force u32)val >> 14)) & mask;
  713. }
  714. static struct hlist_head *fib_info_hash_alloc(int bytes)
  715. {
  716. if (bytes <= PAGE_SIZE)
  717. return kzalloc(bytes, GFP_KERNEL);
  718. else
  719. return (struct hlist_head *)
  720. __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  721. get_order(bytes));
  722. }
  723. static void fib_info_hash_free(struct hlist_head *hash, int bytes)
  724. {
  725. if (!hash)
  726. return;
  727. if (bytes <= PAGE_SIZE)
  728. kfree(hash);
  729. else
  730. free_pages((unsigned long) hash, get_order(bytes));
  731. }
  732. static void fib_info_hash_move(struct hlist_head *new_info_hash,
  733. struct hlist_head *new_laddrhash,
  734. unsigned int new_size)
  735. {
  736. struct hlist_head *old_info_hash, *old_laddrhash;
  737. unsigned int old_size = fib_info_hash_size;
  738. unsigned int i, bytes;
  739. spin_lock_bh(&fib_info_lock);
  740. old_info_hash = fib_info_hash;
  741. old_laddrhash = fib_info_laddrhash;
  742. fib_info_hash_size = new_size;
  743. for (i = 0; i < old_size; i++) {
  744. struct hlist_head *head = &fib_info_hash[i];
  745. struct hlist_node *n;
  746. struct fib_info *fi;
  747. hlist_for_each_entry_safe(fi, n, head, fib_hash) {
  748. struct hlist_head *dest;
  749. unsigned int new_hash;
  750. new_hash = fib_info_hashfn(fi);
  751. dest = &new_info_hash[new_hash];
  752. hlist_add_head(&fi->fib_hash, dest);
  753. }
  754. }
  755. fib_info_hash = new_info_hash;
  756. for (i = 0; i < old_size; i++) {
  757. struct hlist_head *lhead = &fib_info_laddrhash[i];
  758. struct hlist_node *n;
  759. struct fib_info *fi;
  760. hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
  761. struct hlist_head *ldest;
  762. unsigned int new_hash;
  763. new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
  764. ldest = &new_laddrhash[new_hash];
  765. hlist_add_head(&fi->fib_lhash, ldest);
  766. }
  767. }
  768. fib_info_laddrhash = new_laddrhash;
  769. spin_unlock_bh(&fib_info_lock);
  770. bytes = old_size * sizeof(struct hlist_head *);
  771. fib_info_hash_free(old_info_hash, bytes);
  772. fib_info_hash_free(old_laddrhash, bytes);
  773. }
  774. __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
  775. {
  776. nh->nh_saddr = inet_select_addr(nh->nh_dev,
  777. nh->nh_gw,
  778. nh->nh_parent->fib_scope);
  779. nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
  780. return nh->nh_saddr;
  781. }
  782. static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
  783. {
  784. if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
  785. fib_prefsrc != cfg->fc_dst) {
  786. u32 tb_id = cfg->fc_table;
  787. int rc;
  788. if (tb_id == RT_TABLE_MAIN)
  789. tb_id = RT_TABLE_LOCAL;
  790. rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
  791. fib_prefsrc, tb_id);
  792. if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
  793. rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
  794. fib_prefsrc, RT_TABLE_LOCAL);
  795. }
  796. if (rc != RTN_LOCAL)
  797. return false;
  798. }
  799. return true;
  800. }
  801. static int
  802. fib_convert_metrics(struct fib_info *fi, const struct fib_config *cfg)
  803. {
  804. bool ecn_ca = false;
  805. struct nlattr *nla;
  806. int remaining;
  807. if (!cfg->fc_mx)
  808. return 0;
  809. nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
  810. int type = nla_type(nla);
  811. u32 val;
  812. if (!type)
  813. continue;
  814. if (type > RTAX_MAX)
  815. return -EINVAL;
  816. if (type == RTAX_CC_ALGO) {
  817. char tmp[TCP_CA_NAME_MAX];
  818. nla_strlcpy(tmp, nla, sizeof(tmp));
  819. val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
  820. if (val == TCP_CA_UNSPEC)
  821. return -EINVAL;
  822. } else {
  823. val = nla_get_u32(nla);
  824. }
  825. if (type == RTAX_ADVMSS && val > 65535 - 40)
  826. val = 65535 - 40;
  827. if (type == RTAX_MTU && val > 65535 - 15)
  828. val = 65535 - 15;
  829. if (type == RTAX_HOPLIMIT && val > 255)
  830. val = 255;
  831. if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
  832. return -EINVAL;
  833. fi->fib_metrics[type - 1] = val;
  834. }
  835. if (ecn_ca)
  836. fi->fib_metrics[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
  837. return 0;
  838. }
  839. struct fib_info *fib_create_info(struct fib_config *cfg)
  840. {
  841. int err;
  842. struct fib_info *fi = NULL;
  843. struct fib_info *ofi;
  844. int nhs = 1;
  845. struct net *net = cfg->fc_nlinfo.nl_net;
  846. if (cfg->fc_type > RTN_MAX)
  847. goto err_inval;
  848. /* Fast check to catch the most weird cases */
  849. if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
  850. goto err_inval;
  851. if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  852. goto err_inval;
  853. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  854. if (cfg->fc_mp) {
  855. nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
  856. if (nhs == 0)
  857. goto err_inval;
  858. }
  859. #endif
  860. err = -ENOBUFS;
  861. if (fib_info_cnt >= fib_info_hash_size) {
  862. unsigned int new_size = fib_info_hash_size << 1;
  863. struct hlist_head *new_info_hash;
  864. struct hlist_head *new_laddrhash;
  865. unsigned int bytes;
  866. if (!new_size)
  867. new_size = 16;
  868. bytes = new_size * sizeof(struct hlist_head *);
  869. new_info_hash = fib_info_hash_alloc(bytes);
  870. new_laddrhash = fib_info_hash_alloc(bytes);
  871. if (!new_info_hash || !new_laddrhash) {
  872. fib_info_hash_free(new_info_hash, bytes);
  873. fib_info_hash_free(new_laddrhash, bytes);
  874. } else
  875. fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
  876. if (!fib_info_hash_size)
  877. goto failure;
  878. }
  879. fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
  880. if (!fi)
  881. goto failure;
  882. fib_info_cnt++;
  883. if (cfg->fc_mx) {
  884. fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
  885. if (!fi->fib_metrics)
  886. goto failure;
  887. } else
  888. fi->fib_metrics = (u32 *) dst_default_metrics;
  889. fi->fib_net = net;
  890. fi->fib_protocol = cfg->fc_protocol;
  891. fi->fib_scope = cfg->fc_scope;
  892. fi->fib_flags = cfg->fc_flags;
  893. fi->fib_priority = cfg->fc_priority;
  894. fi->fib_prefsrc = cfg->fc_prefsrc;
  895. fi->fib_type = cfg->fc_type;
  896. fi->fib_nhs = nhs;
  897. change_nexthops(fi) {
  898. nexthop_nh->nh_parent = fi;
  899. nexthop_nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
  900. if (!nexthop_nh->nh_pcpu_rth_output)
  901. goto failure;
  902. } endfor_nexthops(fi)
  903. err = fib_convert_metrics(fi, cfg);
  904. if (err)
  905. goto failure;
  906. if (cfg->fc_mp) {
  907. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  908. err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
  909. if (err != 0)
  910. goto failure;
  911. if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
  912. goto err_inval;
  913. if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
  914. goto err_inval;
  915. #ifdef CONFIG_IP_ROUTE_CLASSID
  916. if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
  917. goto err_inval;
  918. #endif
  919. #else
  920. goto err_inval;
  921. #endif
  922. } else {
  923. struct fib_nh *nh = fi->fib_nh;
  924. if (cfg->fc_encap) {
  925. struct lwtunnel_state *lwtstate;
  926. struct net_device *dev = NULL;
  927. if (cfg->fc_encap_type == LWTUNNEL_ENCAP_NONE)
  928. goto err_inval;
  929. if (cfg->fc_oif)
  930. dev = __dev_get_by_index(net, cfg->fc_oif);
  931. err = lwtunnel_build_state(dev, cfg->fc_encap_type,
  932. cfg->fc_encap, AF_INET, cfg,
  933. &lwtstate);
  934. if (err)
  935. goto failure;
  936. nh->nh_lwtstate = lwtstate_get(lwtstate);
  937. }
  938. nh->nh_oif = cfg->fc_oif;
  939. nh->nh_gw = cfg->fc_gw;
  940. nh->nh_flags = cfg->fc_flags;
  941. #ifdef CONFIG_IP_ROUTE_CLASSID
  942. nh->nh_tclassid = cfg->fc_flow;
  943. if (nh->nh_tclassid)
  944. fi->fib_net->ipv4.fib_num_tclassid_users++;
  945. #endif
  946. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  947. nh->nh_weight = 1;
  948. #endif
  949. }
  950. if (fib_props[cfg->fc_type].error) {
  951. if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
  952. goto err_inval;
  953. goto link_it;
  954. } else {
  955. switch (cfg->fc_type) {
  956. case RTN_UNICAST:
  957. case RTN_LOCAL:
  958. case RTN_BROADCAST:
  959. case RTN_ANYCAST:
  960. case RTN_MULTICAST:
  961. break;
  962. default:
  963. goto err_inval;
  964. }
  965. }
  966. if (cfg->fc_scope > RT_SCOPE_HOST)
  967. goto err_inval;
  968. if (cfg->fc_scope == RT_SCOPE_HOST) {
  969. struct fib_nh *nh = fi->fib_nh;
  970. /* Local address is added. */
  971. if (nhs != 1 || nh->nh_gw)
  972. goto err_inval;
  973. nh->nh_scope = RT_SCOPE_NOWHERE;
  974. nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
  975. err = -ENODEV;
  976. if (!nh->nh_dev)
  977. goto failure;
  978. } else {
  979. int linkdown = 0;
  980. change_nexthops(fi) {
  981. err = fib_check_nh(cfg, fi, nexthop_nh);
  982. if (err != 0)
  983. goto failure;
  984. if (nexthop_nh->nh_flags & RTNH_F_LINKDOWN)
  985. linkdown++;
  986. } endfor_nexthops(fi)
  987. if (linkdown == fi->fib_nhs)
  988. fi->fib_flags |= RTNH_F_LINKDOWN;
  989. }
  990. if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc))
  991. goto err_inval;
  992. change_nexthops(fi) {
  993. fib_info_update_nh_saddr(net, nexthop_nh);
  994. fib_add_weight(fi, nexthop_nh);
  995. } endfor_nexthops(fi)
  996. fib_rebalance(fi);
  997. link_it:
  998. ofi = fib_find_info(fi);
  999. if (ofi) {
  1000. fi->fib_dead = 1;
  1001. free_fib_info(fi);
  1002. ofi->fib_treeref++;
  1003. return ofi;
  1004. }
  1005. fi->fib_treeref++;
  1006. atomic_inc(&fi->fib_clntref);
  1007. spin_lock_bh(&fib_info_lock);
  1008. hlist_add_head(&fi->fib_hash,
  1009. &fib_info_hash[fib_info_hashfn(fi)]);
  1010. if (fi->fib_prefsrc) {
  1011. struct hlist_head *head;
  1012. head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
  1013. hlist_add_head(&fi->fib_lhash, head);
  1014. }
  1015. change_nexthops(fi) {
  1016. struct hlist_head *head;
  1017. unsigned int hash;
  1018. if (!nexthop_nh->nh_dev)
  1019. continue;
  1020. hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
  1021. head = &fib_info_devhash[hash];
  1022. hlist_add_head(&nexthop_nh->nh_hash, head);
  1023. } endfor_nexthops(fi)
  1024. spin_unlock_bh(&fib_info_lock);
  1025. return fi;
  1026. err_inval:
  1027. err = -EINVAL;
  1028. failure:
  1029. if (fi) {
  1030. fi->fib_dead = 1;
  1031. free_fib_info(fi);
  1032. }
  1033. return ERR_PTR(err);
  1034. }
  1035. int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1036. u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
  1037. struct fib_info *fi, unsigned int flags)
  1038. {
  1039. struct nlmsghdr *nlh;
  1040. struct rtmsg *rtm;
  1041. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1042. if (!nlh)
  1043. return -EMSGSIZE;
  1044. rtm = nlmsg_data(nlh);
  1045. rtm->rtm_family = AF_INET;
  1046. rtm->rtm_dst_len = dst_len;
  1047. rtm->rtm_src_len = 0;
  1048. rtm->rtm_tos = tos;
  1049. if (tb_id < 256)
  1050. rtm->rtm_table = tb_id;
  1051. else
  1052. rtm->rtm_table = RT_TABLE_COMPAT;
  1053. if (nla_put_u32(skb, RTA_TABLE, tb_id))
  1054. goto nla_put_failure;
  1055. rtm->rtm_type = type;
  1056. rtm->rtm_flags = fi->fib_flags;
  1057. rtm->rtm_scope = fi->fib_scope;
  1058. rtm->rtm_protocol = fi->fib_protocol;
  1059. if (rtm->rtm_dst_len &&
  1060. nla_put_in_addr(skb, RTA_DST, dst))
  1061. goto nla_put_failure;
  1062. if (fi->fib_priority &&
  1063. nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
  1064. goto nla_put_failure;
  1065. if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
  1066. goto nla_put_failure;
  1067. if (fi->fib_prefsrc &&
  1068. nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
  1069. goto nla_put_failure;
  1070. if (fi->fib_nhs == 1) {
  1071. struct in_device *in_dev;
  1072. if (fi->fib_nh->nh_gw &&
  1073. nla_put_in_addr(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
  1074. goto nla_put_failure;
  1075. if (fi->fib_nh->nh_oif &&
  1076. nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
  1077. goto nla_put_failure;
  1078. if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) {
  1079. in_dev = __in_dev_get_rtnl(fi->fib_nh->nh_dev);
  1080. if (in_dev &&
  1081. IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
  1082. rtm->rtm_flags |= RTNH_F_DEAD;
  1083. }
  1084. #ifdef CONFIG_IP_ROUTE_CLASSID
  1085. if (fi->fib_nh[0].nh_tclassid &&
  1086. nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
  1087. goto nla_put_failure;
  1088. #endif
  1089. if (fi->fib_nh->nh_lwtstate)
  1090. lwtunnel_fill_encap(skb, fi->fib_nh->nh_lwtstate);
  1091. }
  1092. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1093. if (fi->fib_nhs > 1) {
  1094. struct rtnexthop *rtnh;
  1095. struct nlattr *mp;
  1096. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1097. if (!mp)
  1098. goto nla_put_failure;
  1099. for_nexthops(fi) {
  1100. struct in_device *in_dev;
  1101. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1102. if (!rtnh)
  1103. goto nla_put_failure;
  1104. rtnh->rtnh_flags = nh->nh_flags & 0xFF;
  1105. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1106. in_dev = __in_dev_get_rtnl(nh->nh_dev);
  1107. if (in_dev &&
  1108. IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
  1109. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1110. }
  1111. rtnh->rtnh_hops = nh->nh_weight - 1;
  1112. rtnh->rtnh_ifindex = nh->nh_oif;
  1113. if (nh->nh_gw &&
  1114. nla_put_in_addr(skb, RTA_GATEWAY, nh->nh_gw))
  1115. goto nla_put_failure;
  1116. #ifdef CONFIG_IP_ROUTE_CLASSID
  1117. if (nh->nh_tclassid &&
  1118. nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
  1119. goto nla_put_failure;
  1120. #endif
  1121. if (nh->nh_lwtstate)
  1122. lwtunnel_fill_encap(skb, nh->nh_lwtstate);
  1123. /* length of rtnetlink header + attributes */
  1124. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
  1125. } endfor_nexthops(fi);
  1126. nla_nest_end(skb, mp);
  1127. }
  1128. #endif
  1129. nlmsg_end(skb, nlh);
  1130. return 0;
  1131. nla_put_failure:
  1132. nlmsg_cancel(skb, nlh);
  1133. return -EMSGSIZE;
  1134. }
  1135. /*
  1136. * Update FIB if:
  1137. * - local address disappeared -> we must delete all the entries
  1138. * referring to it.
  1139. * - device went down -> we must shutdown all nexthops going via it.
  1140. */
  1141. int fib_sync_down_addr(struct net *net, __be32 local)
  1142. {
  1143. int ret = 0;
  1144. unsigned int hash = fib_laddr_hashfn(local);
  1145. struct hlist_head *head = &fib_info_laddrhash[hash];
  1146. struct fib_info *fi;
  1147. if (!fib_info_laddrhash || local == 0)
  1148. return 0;
  1149. hlist_for_each_entry(fi, head, fib_lhash) {
  1150. if (!net_eq(fi->fib_net, net))
  1151. continue;
  1152. if (fi->fib_prefsrc == local) {
  1153. fi->fib_flags |= RTNH_F_DEAD;
  1154. ret++;
  1155. }
  1156. }
  1157. return ret;
  1158. }
  1159. /* Event force Flags Description
  1160. * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
  1161. * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
  1162. * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
  1163. * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
  1164. */
  1165. int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
  1166. {
  1167. int ret = 0;
  1168. int scope = RT_SCOPE_NOWHERE;
  1169. struct fib_info *prev_fi = NULL;
  1170. unsigned int hash = fib_devindex_hashfn(dev->ifindex);
  1171. struct hlist_head *head = &fib_info_devhash[hash];
  1172. struct fib_nh *nh;
  1173. if (force)
  1174. scope = -1;
  1175. hlist_for_each_entry(nh, head, nh_hash) {
  1176. struct fib_info *fi = nh->nh_parent;
  1177. int dead;
  1178. BUG_ON(!fi->fib_nhs);
  1179. if (nh->nh_dev != dev || fi == prev_fi)
  1180. continue;
  1181. prev_fi = fi;
  1182. dead = 0;
  1183. change_nexthops(fi) {
  1184. if (nexthop_nh->nh_flags & RTNH_F_DEAD)
  1185. dead++;
  1186. else if (nexthop_nh->nh_dev == dev &&
  1187. nexthop_nh->nh_scope != scope) {
  1188. switch (event) {
  1189. case NETDEV_DOWN:
  1190. case NETDEV_UNREGISTER:
  1191. nexthop_nh->nh_flags |= RTNH_F_DEAD;
  1192. /* fall through */
  1193. case NETDEV_CHANGE:
  1194. nexthop_nh->nh_flags |= RTNH_F_LINKDOWN;
  1195. break;
  1196. }
  1197. dead++;
  1198. }
  1199. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1200. if (event == NETDEV_UNREGISTER &&
  1201. nexthop_nh->nh_dev == dev) {
  1202. dead = fi->fib_nhs;
  1203. break;
  1204. }
  1205. #endif
  1206. } endfor_nexthops(fi)
  1207. if (dead == fi->fib_nhs) {
  1208. switch (event) {
  1209. case NETDEV_DOWN:
  1210. case NETDEV_UNREGISTER:
  1211. fi->fib_flags |= RTNH_F_DEAD;
  1212. /* fall through */
  1213. case NETDEV_CHANGE:
  1214. fi->fib_flags |= RTNH_F_LINKDOWN;
  1215. break;
  1216. }
  1217. ret++;
  1218. }
  1219. fib_rebalance(fi);
  1220. }
  1221. return ret;
  1222. }
  1223. /* Must be invoked inside of an RCU protected region. */
  1224. void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
  1225. {
  1226. struct fib_info *fi = NULL, *last_resort = NULL;
  1227. struct hlist_head *fa_head = res->fa_head;
  1228. struct fib_table *tb = res->table;
  1229. u8 slen = 32 - res->prefixlen;
  1230. int order = -1, last_idx = -1;
  1231. struct fib_alias *fa, *fa1 = NULL;
  1232. u32 last_prio = res->fi->fib_priority;
  1233. u8 last_tos = 0;
  1234. hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
  1235. struct fib_info *next_fi = fa->fa_info;
  1236. if (fa->fa_slen != slen)
  1237. continue;
  1238. if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
  1239. continue;
  1240. if (fa->tb_id != tb->tb_id)
  1241. continue;
  1242. if (next_fi->fib_priority > last_prio &&
  1243. fa->fa_tos == last_tos) {
  1244. if (last_tos)
  1245. continue;
  1246. break;
  1247. }
  1248. if (next_fi->fib_flags & RTNH_F_DEAD)
  1249. continue;
  1250. last_tos = fa->fa_tos;
  1251. last_prio = next_fi->fib_priority;
  1252. if (next_fi->fib_scope != res->scope ||
  1253. fa->fa_type != RTN_UNICAST)
  1254. continue;
  1255. if (!next_fi->fib_nh[0].nh_gw ||
  1256. next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
  1257. continue;
  1258. fib_alias_accessed(fa);
  1259. if (!fi) {
  1260. if (next_fi != res->fi)
  1261. break;
  1262. fa1 = fa;
  1263. } else if (!fib_detect_death(fi, order, &last_resort,
  1264. &last_idx, fa1->fa_default)) {
  1265. fib_result_assign(res, fi);
  1266. fa1->fa_default = order;
  1267. goto out;
  1268. }
  1269. fi = next_fi;
  1270. order++;
  1271. }
  1272. if (order <= 0 || !fi) {
  1273. if (fa1)
  1274. fa1->fa_default = -1;
  1275. goto out;
  1276. }
  1277. if (!fib_detect_death(fi, order, &last_resort, &last_idx,
  1278. fa1->fa_default)) {
  1279. fib_result_assign(res, fi);
  1280. fa1->fa_default = order;
  1281. goto out;
  1282. }
  1283. if (last_idx >= 0)
  1284. fib_result_assign(res, last_resort);
  1285. fa1->fa_default = last_idx;
  1286. out:
  1287. return;
  1288. }
  1289. /*
  1290. * Dead device goes up. We wake up dead nexthops.
  1291. * It takes sense only on multipath routes.
  1292. */
  1293. int fib_sync_up(struct net_device *dev, unsigned int nh_flags)
  1294. {
  1295. struct fib_info *prev_fi;
  1296. unsigned int hash;
  1297. struct hlist_head *head;
  1298. struct fib_nh *nh;
  1299. int ret;
  1300. if (!(dev->flags & IFF_UP))
  1301. return 0;
  1302. if (nh_flags & RTNH_F_DEAD) {
  1303. unsigned int flags = dev_get_flags(dev);
  1304. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1305. nh_flags |= RTNH_F_LINKDOWN;
  1306. }
  1307. prev_fi = NULL;
  1308. hash = fib_devindex_hashfn(dev->ifindex);
  1309. head = &fib_info_devhash[hash];
  1310. ret = 0;
  1311. hlist_for_each_entry(nh, head, nh_hash) {
  1312. struct fib_info *fi = nh->nh_parent;
  1313. int alive;
  1314. BUG_ON(!fi->fib_nhs);
  1315. if (nh->nh_dev != dev || fi == prev_fi)
  1316. continue;
  1317. prev_fi = fi;
  1318. alive = 0;
  1319. change_nexthops(fi) {
  1320. if (!(nexthop_nh->nh_flags & nh_flags)) {
  1321. alive++;
  1322. continue;
  1323. }
  1324. if (!nexthop_nh->nh_dev ||
  1325. !(nexthop_nh->nh_dev->flags & IFF_UP))
  1326. continue;
  1327. if (nexthop_nh->nh_dev != dev ||
  1328. !__in_dev_get_rtnl(dev))
  1329. continue;
  1330. alive++;
  1331. nexthop_nh->nh_flags &= ~nh_flags;
  1332. } endfor_nexthops(fi)
  1333. if (alive > 0) {
  1334. fi->fib_flags &= ~nh_flags;
  1335. ret++;
  1336. }
  1337. fib_rebalance(fi);
  1338. }
  1339. return ret;
  1340. }
  1341. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1342. static bool fib_good_nh(const struct fib_nh *nh)
  1343. {
  1344. int state = NUD_REACHABLE;
  1345. if (nh->nh_scope == RT_SCOPE_LINK) {
  1346. struct neighbour *n;
  1347. rcu_read_lock_bh();
  1348. n = __ipv4_neigh_lookup_noref(nh->nh_dev, nh->nh_gw);
  1349. if (n)
  1350. state = n->nud_state;
  1351. rcu_read_unlock_bh();
  1352. }
  1353. return !!(state & NUD_VALID);
  1354. }
  1355. void fib_select_multipath(struct fib_result *res, int hash)
  1356. {
  1357. struct fib_info *fi = res->fi;
  1358. struct net *net = fi->fib_net;
  1359. bool first = false;
  1360. for_nexthops(fi) {
  1361. if (hash > atomic_read(&nh->nh_upper_bound))
  1362. continue;
  1363. if (!net->ipv4.sysctl_fib_multipath_use_neigh ||
  1364. fib_good_nh(nh)) {
  1365. res->nh_sel = nhsel;
  1366. return;
  1367. }
  1368. if (!first) {
  1369. res->nh_sel = nhsel;
  1370. first = true;
  1371. }
  1372. } endfor_nexthops(fi);
  1373. }
  1374. #endif
  1375. void fib_select_path(struct net *net, struct fib_result *res,
  1376. struct flowi4 *fl4, int mp_hash)
  1377. {
  1378. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1379. if (res->fi->fib_nhs > 1 && fl4->flowi4_oif == 0) {
  1380. if (mp_hash < 0)
  1381. mp_hash = get_hash_from_flowi4(fl4) >> 1;
  1382. fib_select_multipath(res, mp_hash);
  1383. }
  1384. else
  1385. #endif
  1386. if (!res->prefixlen &&
  1387. res->table->tb_num_default > 1 &&
  1388. res->type == RTN_UNICAST && !fl4->flowi4_oif)
  1389. fib_select_default(fl4, res);
  1390. if (!fl4->saddr)
  1391. fl4->saddr = FIB_RES_PREFSRC(net, *res);
  1392. }
  1393. EXPORT_SYMBOL_GPL(fib_select_path);