fib_semantics.c 39 KB

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