fib_semantics.c 37 KB

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