fib_semantics.c 39 KB

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