fib_semantics.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  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. #ifdef CONFIG_IP_ROUTE_CLASSID
  540. if (cfg->fc_flow &&
  541. cfg->fc_flow != fi->fib_nh->nh_tclassid)
  542. return 1;
  543. #endif
  544. if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
  545. (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
  546. return 0;
  547. return 1;
  548. }
  549. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  550. if (!cfg->fc_mp)
  551. return 0;
  552. rtnh = cfg->fc_mp;
  553. remaining = cfg->fc_mp_len;
  554. for_nexthops(fi) {
  555. int attrlen;
  556. if (!rtnh_ok(rtnh, remaining))
  557. return -EINVAL;
  558. if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
  559. return 1;
  560. attrlen = rtnh_attrlen(rtnh);
  561. if (attrlen > 0) {
  562. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  563. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  564. if (nla && nla_get_in_addr(nla) != nh->nh_gw)
  565. return 1;
  566. #ifdef CONFIG_IP_ROUTE_CLASSID
  567. nla = nla_find(attrs, attrlen, RTA_FLOW);
  568. if (nla && nla_get_u32(nla) != nh->nh_tclassid)
  569. return 1;
  570. #endif
  571. }
  572. rtnh = rtnh_next(rtnh, &remaining);
  573. } endfor_nexthops(fi);
  574. #endif
  575. return 0;
  576. }
  577. /*
  578. * Picture
  579. * -------
  580. *
  581. * Semantics of nexthop is very messy by historical reasons.
  582. * We have to take into account, that:
  583. * a) gateway can be actually local interface address,
  584. * so that gatewayed route is direct.
  585. * b) gateway must be on-link address, possibly
  586. * described not by an ifaddr, but also by a direct route.
  587. * c) If both gateway and interface are specified, they should not
  588. * contradict.
  589. * d) If we use tunnel routes, gateway could be not on-link.
  590. *
  591. * Attempt to reconcile all of these (alas, self-contradictory) conditions
  592. * results in pretty ugly and hairy code with obscure logic.
  593. *
  594. * I chose to generalized it instead, so that the size
  595. * of code does not increase practically, but it becomes
  596. * much more general.
  597. * Every prefix is assigned a "scope" value: "host" is local address,
  598. * "link" is direct route,
  599. * [ ... "site" ... "interior" ... ]
  600. * and "universe" is true gateway route with global meaning.
  601. *
  602. * Every prefix refers to a set of "nexthop"s (gw, oif),
  603. * where gw must have narrower scope. This recursion stops
  604. * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
  605. * which means that gw is forced to be on link.
  606. *
  607. * Code is still hairy, but now it is apparently logically
  608. * consistent and very flexible. F.e. as by-product it allows
  609. * to co-exists in peace independent exterior and interior
  610. * routing processes.
  611. *
  612. * Normally it looks as following.
  613. *
  614. * {universe prefix} -> (gw, oif) [scope link]
  615. * |
  616. * |-> {link prefix} -> (gw, oif) [scope local]
  617. * |
  618. * |-> {local prefix} (terminal node)
  619. */
  620. static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
  621. struct fib_nh *nh)
  622. {
  623. int err = 0;
  624. struct net *net;
  625. struct net_device *dev;
  626. net = cfg->fc_nlinfo.nl_net;
  627. if (nh->nh_gw) {
  628. struct fib_result res;
  629. if (nh->nh_flags & RTNH_F_ONLINK) {
  630. unsigned int addr_type;
  631. if (cfg->fc_scope >= RT_SCOPE_LINK)
  632. return -EINVAL;
  633. dev = __dev_get_by_index(net, nh->nh_oif);
  634. if (!dev)
  635. return -ENODEV;
  636. if (!(dev->flags & IFF_UP))
  637. return -ENETDOWN;
  638. addr_type = inet_addr_type_dev_table(net, dev, nh->nh_gw);
  639. if (addr_type != RTN_UNICAST)
  640. return -EINVAL;
  641. if (!netif_carrier_ok(dev))
  642. nh->nh_flags |= RTNH_F_LINKDOWN;
  643. nh->nh_dev = dev;
  644. dev_hold(dev);
  645. nh->nh_scope = RT_SCOPE_LINK;
  646. return 0;
  647. }
  648. rcu_read_lock();
  649. {
  650. struct fib_table *tbl = NULL;
  651. struct flowi4 fl4 = {
  652. .daddr = nh->nh_gw,
  653. .flowi4_scope = cfg->fc_scope + 1,
  654. .flowi4_oif = nh->nh_oif,
  655. .flowi4_iif = LOOPBACK_IFINDEX,
  656. };
  657. /* It is not necessary, but requires a bit of thinking */
  658. if (fl4.flowi4_scope < RT_SCOPE_LINK)
  659. fl4.flowi4_scope = RT_SCOPE_LINK;
  660. if (cfg->fc_table)
  661. tbl = fib_get_table(net, cfg->fc_table);
  662. if (tbl)
  663. err = fib_table_lookup(tbl, &fl4, &res,
  664. FIB_LOOKUP_IGNORE_LINKSTATE |
  665. FIB_LOOKUP_NOREF);
  666. /* on error or if no table given do full lookup. This
  667. * is needed for example when nexthops are in the local
  668. * table rather than the given table
  669. */
  670. if (!tbl || err) {
  671. err = fib_lookup(net, &fl4, &res,
  672. FIB_LOOKUP_IGNORE_LINKSTATE);
  673. }
  674. if (err) {
  675. rcu_read_unlock();
  676. return err;
  677. }
  678. }
  679. err = -EINVAL;
  680. if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
  681. goto out;
  682. nh->nh_scope = res.scope;
  683. nh->nh_oif = FIB_RES_OIF(res);
  684. nh->nh_dev = dev = FIB_RES_DEV(res);
  685. if (!dev)
  686. goto out;
  687. dev_hold(dev);
  688. if (!netif_carrier_ok(dev))
  689. nh->nh_flags |= RTNH_F_LINKDOWN;
  690. err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
  691. } else {
  692. struct in_device *in_dev;
  693. if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
  694. return -EINVAL;
  695. rcu_read_lock();
  696. err = -ENODEV;
  697. in_dev = inetdev_by_index(net, nh->nh_oif);
  698. if (!in_dev)
  699. goto out;
  700. err = -ENETDOWN;
  701. if (!(in_dev->dev->flags & IFF_UP))
  702. goto out;
  703. nh->nh_dev = in_dev->dev;
  704. dev_hold(nh->nh_dev);
  705. nh->nh_scope = RT_SCOPE_HOST;
  706. if (!netif_carrier_ok(nh->nh_dev))
  707. nh->nh_flags |= RTNH_F_LINKDOWN;
  708. err = 0;
  709. }
  710. out:
  711. rcu_read_unlock();
  712. return err;
  713. }
  714. static inline unsigned int fib_laddr_hashfn(__be32 val)
  715. {
  716. unsigned int mask = (fib_info_hash_size - 1);
  717. return ((__force u32)val ^
  718. ((__force u32)val >> 7) ^
  719. ((__force u32)val >> 14)) & mask;
  720. }
  721. static struct hlist_head *fib_info_hash_alloc(int bytes)
  722. {
  723. if (bytes <= PAGE_SIZE)
  724. return kzalloc(bytes, GFP_KERNEL);
  725. else
  726. return (struct hlist_head *)
  727. __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  728. get_order(bytes));
  729. }
  730. static void fib_info_hash_free(struct hlist_head *hash, int bytes)
  731. {
  732. if (!hash)
  733. return;
  734. if (bytes <= PAGE_SIZE)
  735. kfree(hash);
  736. else
  737. free_pages((unsigned long) hash, get_order(bytes));
  738. }
  739. static void fib_info_hash_move(struct hlist_head *new_info_hash,
  740. struct hlist_head *new_laddrhash,
  741. unsigned int new_size)
  742. {
  743. struct hlist_head *old_info_hash, *old_laddrhash;
  744. unsigned int old_size = fib_info_hash_size;
  745. unsigned int i, bytes;
  746. spin_lock_bh(&fib_info_lock);
  747. old_info_hash = fib_info_hash;
  748. old_laddrhash = fib_info_laddrhash;
  749. fib_info_hash_size = new_size;
  750. for (i = 0; i < old_size; i++) {
  751. struct hlist_head *head = &fib_info_hash[i];
  752. struct hlist_node *n;
  753. struct fib_info *fi;
  754. hlist_for_each_entry_safe(fi, n, head, fib_hash) {
  755. struct hlist_head *dest;
  756. unsigned int new_hash;
  757. new_hash = fib_info_hashfn(fi);
  758. dest = &new_info_hash[new_hash];
  759. hlist_add_head(&fi->fib_hash, dest);
  760. }
  761. }
  762. fib_info_hash = new_info_hash;
  763. for (i = 0; i < old_size; i++) {
  764. struct hlist_head *lhead = &fib_info_laddrhash[i];
  765. struct hlist_node *n;
  766. struct fib_info *fi;
  767. hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
  768. struct hlist_head *ldest;
  769. unsigned int new_hash;
  770. new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
  771. ldest = &new_laddrhash[new_hash];
  772. hlist_add_head(&fi->fib_lhash, ldest);
  773. }
  774. }
  775. fib_info_laddrhash = new_laddrhash;
  776. spin_unlock_bh(&fib_info_lock);
  777. bytes = old_size * sizeof(struct hlist_head *);
  778. fib_info_hash_free(old_info_hash, bytes);
  779. fib_info_hash_free(old_laddrhash, bytes);
  780. }
  781. __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
  782. {
  783. nh->nh_saddr = inet_select_addr(nh->nh_dev,
  784. nh->nh_gw,
  785. nh->nh_parent->fib_scope);
  786. nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
  787. return nh->nh_saddr;
  788. }
  789. static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
  790. {
  791. if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
  792. fib_prefsrc != cfg->fc_dst) {
  793. u32 tb_id = cfg->fc_table;
  794. int rc;
  795. if (tb_id == RT_TABLE_MAIN)
  796. tb_id = RT_TABLE_LOCAL;
  797. rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
  798. fib_prefsrc, tb_id);
  799. if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
  800. rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
  801. fib_prefsrc, RT_TABLE_LOCAL);
  802. }
  803. if (rc != RTN_LOCAL)
  804. return false;
  805. }
  806. return true;
  807. }
  808. static int
  809. fib_convert_metrics(struct fib_info *fi, const struct fib_config *cfg)
  810. {
  811. bool ecn_ca = false;
  812. struct nlattr *nla;
  813. int remaining;
  814. if (!cfg->fc_mx)
  815. return 0;
  816. nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
  817. int type = nla_type(nla);
  818. u32 val;
  819. if (!type)
  820. continue;
  821. if (type > RTAX_MAX)
  822. return -EINVAL;
  823. if (type == RTAX_CC_ALGO) {
  824. char tmp[TCP_CA_NAME_MAX];
  825. nla_strlcpy(tmp, nla, sizeof(tmp));
  826. val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
  827. if (val == TCP_CA_UNSPEC)
  828. return -EINVAL;
  829. } else {
  830. if (nla_len(nla) != sizeof(u32))
  831. return -EINVAL;
  832. val = nla_get_u32(nla);
  833. }
  834. if (type == RTAX_ADVMSS && val > 65535 - 40)
  835. val = 65535 - 40;
  836. if (type == RTAX_MTU && val > 65535 - 15)
  837. val = 65535 - 15;
  838. if (type == RTAX_HOPLIMIT && val > 255)
  839. val = 255;
  840. if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
  841. return -EINVAL;
  842. fi->fib_metrics->metrics[type - 1] = val;
  843. }
  844. if (ecn_ca)
  845. fi->fib_metrics->metrics[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
  846. return 0;
  847. }
  848. struct fib_info *fib_create_info(struct fib_config *cfg)
  849. {
  850. int err;
  851. struct fib_info *fi = NULL;
  852. struct fib_info *ofi;
  853. int nhs = 1;
  854. struct net *net = cfg->fc_nlinfo.nl_net;
  855. if (cfg->fc_type > RTN_MAX)
  856. goto err_inval;
  857. /* Fast check to catch the most weird cases */
  858. if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
  859. goto err_inval;
  860. if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  861. goto err_inval;
  862. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  863. if (cfg->fc_mp) {
  864. nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
  865. if (nhs == 0)
  866. goto err_inval;
  867. }
  868. #endif
  869. err = -ENOBUFS;
  870. if (fib_info_cnt >= fib_info_hash_size) {
  871. unsigned int new_size = fib_info_hash_size << 1;
  872. struct hlist_head *new_info_hash;
  873. struct hlist_head *new_laddrhash;
  874. unsigned int bytes;
  875. if (!new_size)
  876. new_size = 16;
  877. bytes = new_size * sizeof(struct hlist_head *);
  878. new_info_hash = fib_info_hash_alloc(bytes);
  879. new_laddrhash = fib_info_hash_alloc(bytes);
  880. if (!new_info_hash || !new_laddrhash) {
  881. fib_info_hash_free(new_info_hash, bytes);
  882. fib_info_hash_free(new_laddrhash, bytes);
  883. } else
  884. fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
  885. if (!fib_info_hash_size)
  886. goto failure;
  887. }
  888. fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
  889. if (!fi)
  890. goto failure;
  891. if (cfg->fc_mx) {
  892. fi->fib_metrics = kzalloc(sizeof(*fi->fib_metrics), GFP_KERNEL);
  893. if (unlikely(!fi->fib_metrics)) {
  894. kfree(fi);
  895. return ERR_PTR(err);
  896. }
  897. atomic_set(&fi->fib_metrics->refcnt, 1);
  898. } else {
  899. fi->fib_metrics = (struct dst_metrics *)&dst_default_metrics;
  900. }
  901. fib_info_cnt++;
  902. fi->fib_net = net;
  903. fi->fib_protocol = cfg->fc_protocol;
  904. fi->fib_scope = cfg->fc_scope;
  905. fi->fib_flags = cfg->fc_flags;
  906. fi->fib_priority = cfg->fc_priority;
  907. fi->fib_prefsrc = cfg->fc_prefsrc;
  908. fi->fib_type = cfg->fc_type;
  909. fi->fib_tb_id = cfg->fc_table;
  910. fi->fib_nhs = nhs;
  911. change_nexthops(fi) {
  912. nexthop_nh->nh_parent = fi;
  913. nexthop_nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
  914. if (!nexthop_nh->nh_pcpu_rth_output)
  915. goto failure;
  916. } endfor_nexthops(fi)
  917. err = fib_convert_metrics(fi, cfg);
  918. if (err)
  919. goto failure;
  920. if (cfg->fc_mp) {
  921. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  922. err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
  923. if (err != 0)
  924. goto failure;
  925. if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
  926. goto err_inval;
  927. if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
  928. goto err_inval;
  929. #ifdef CONFIG_IP_ROUTE_CLASSID
  930. if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
  931. goto err_inval;
  932. #endif
  933. #else
  934. goto err_inval;
  935. #endif
  936. } else {
  937. struct fib_nh *nh = fi->fib_nh;
  938. if (cfg->fc_encap) {
  939. struct lwtunnel_state *lwtstate;
  940. struct net_device *dev = NULL;
  941. if (cfg->fc_encap_type == LWTUNNEL_ENCAP_NONE)
  942. goto err_inval;
  943. if (cfg->fc_oif)
  944. dev = __dev_get_by_index(net, cfg->fc_oif);
  945. err = lwtunnel_build_state(dev, cfg->fc_encap_type,
  946. cfg->fc_encap, AF_INET, cfg,
  947. &lwtstate);
  948. if (err)
  949. goto failure;
  950. nh->nh_lwtstate = lwtstate_get(lwtstate);
  951. }
  952. nh->nh_oif = cfg->fc_oif;
  953. nh->nh_gw = cfg->fc_gw;
  954. nh->nh_flags = cfg->fc_flags;
  955. #ifdef CONFIG_IP_ROUTE_CLASSID
  956. nh->nh_tclassid = cfg->fc_flow;
  957. if (nh->nh_tclassid)
  958. fi->fib_net->ipv4.fib_num_tclassid_users++;
  959. #endif
  960. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  961. nh->nh_weight = 1;
  962. #endif
  963. }
  964. if (fib_props[cfg->fc_type].error) {
  965. if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
  966. goto err_inval;
  967. goto link_it;
  968. } else {
  969. switch (cfg->fc_type) {
  970. case RTN_UNICAST:
  971. case RTN_LOCAL:
  972. case RTN_BROADCAST:
  973. case RTN_ANYCAST:
  974. case RTN_MULTICAST:
  975. break;
  976. default:
  977. goto err_inval;
  978. }
  979. }
  980. if (cfg->fc_scope > RT_SCOPE_HOST)
  981. goto err_inval;
  982. if (cfg->fc_scope == RT_SCOPE_HOST) {
  983. struct fib_nh *nh = fi->fib_nh;
  984. /* Local address is added. */
  985. if (nhs != 1 || nh->nh_gw)
  986. goto err_inval;
  987. nh->nh_scope = RT_SCOPE_NOWHERE;
  988. nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
  989. err = -ENODEV;
  990. if (!nh->nh_dev)
  991. goto failure;
  992. } else {
  993. int linkdown = 0;
  994. change_nexthops(fi) {
  995. err = fib_check_nh(cfg, fi, nexthop_nh);
  996. if (err != 0)
  997. goto failure;
  998. if (nexthop_nh->nh_flags & RTNH_F_LINKDOWN)
  999. linkdown++;
  1000. } endfor_nexthops(fi)
  1001. if (linkdown == fi->fib_nhs)
  1002. fi->fib_flags |= RTNH_F_LINKDOWN;
  1003. }
  1004. if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc))
  1005. goto err_inval;
  1006. change_nexthops(fi) {
  1007. fib_info_update_nh_saddr(net, nexthop_nh);
  1008. fib_add_weight(fi, nexthop_nh);
  1009. } endfor_nexthops(fi)
  1010. fib_rebalance(fi);
  1011. link_it:
  1012. ofi = fib_find_info(fi);
  1013. if (ofi) {
  1014. fi->fib_dead = 1;
  1015. free_fib_info(fi);
  1016. ofi->fib_treeref++;
  1017. return ofi;
  1018. }
  1019. fi->fib_treeref++;
  1020. atomic_inc(&fi->fib_clntref);
  1021. spin_lock_bh(&fib_info_lock);
  1022. hlist_add_head(&fi->fib_hash,
  1023. &fib_info_hash[fib_info_hashfn(fi)]);
  1024. if (fi->fib_prefsrc) {
  1025. struct hlist_head *head;
  1026. head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
  1027. hlist_add_head(&fi->fib_lhash, head);
  1028. }
  1029. change_nexthops(fi) {
  1030. struct hlist_head *head;
  1031. unsigned int hash;
  1032. if (!nexthop_nh->nh_dev)
  1033. continue;
  1034. hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
  1035. head = &fib_info_devhash[hash];
  1036. hlist_add_head(&nexthop_nh->nh_hash, head);
  1037. } endfor_nexthops(fi)
  1038. spin_unlock_bh(&fib_info_lock);
  1039. return fi;
  1040. err_inval:
  1041. err = -EINVAL;
  1042. failure:
  1043. if (fi) {
  1044. fi->fib_dead = 1;
  1045. free_fib_info(fi);
  1046. }
  1047. return ERR_PTR(err);
  1048. }
  1049. int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1050. u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
  1051. struct fib_info *fi, unsigned int flags)
  1052. {
  1053. struct nlmsghdr *nlh;
  1054. struct rtmsg *rtm;
  1055. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1056. if (!nlh)
  1057. return -EMSGSIZE;
  1058. rtm = nlmsg_data(nlh);
  1059. rtm->rtm_family = AF_INET;
  1060. rtm->rtm_dst_len = dst_len;
  1061. rtm->rtm_src_len = 0;
  1062. rtm->rtm_tos = tos;
  1063. if (tb_id < 256)
  1064. rtm->rtm_table = tb_id;
  1065. else
  1066. rtm->rtm_table = RT_TABLE_COMPAT;
  1067. if (nla_put_u32(skb, RTA_TABLE, tb_id))
  1068. goto nla_put_failure;
  1069. rtm->rtm_type = type;
  1070. rtm->rtm_flags = fi->fib_flags;
  1071. rtm->rtm_scope = fi->fib_scope;
  1072. rtm->rtm_protocol = fi->fib_protocol;
  1073. if (rtm->rtm_dst_len &&
  1074. nla_put_in_addr(skb, RTA_DST, dst))
  1075. goto nla_put_failure;
  1076. if (fi->fib_priority &&
  1077. nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
  1078. goto nla_put_failure;
  1079. if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
  1080. goto nla_put_failure;
  1081. if (fi->fib_prefsrc &&
  1082. nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
  1083. goto nla_put_failure;
  1084. if (fi->fib_nhs == 1) {
  1085. struct in_device *in_dev;
  1086. if (fi->fib_nh->nh_gw &&
  1087. nla_put_in_addr(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
  1088. goto nla_put_failure;
  1089. if (fi->fib_nh->nh_oif &&
  1090. nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
  1091. goto nla_put_failure;
  1092. if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) {
  1093. in_dev = __in_dev_get_rtnl(fi->fib_nh->nh_dev);
  1094. if (in_dev &&
  1095. IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
  1096. rtm->rtm_flags |= RTNH_F_DEAD;
  1097. }
  1098. #ifdef CONFIG_IP_ROUTE_CLASSID
  1099. if (fi->fib_nh[0].nh_tclassid &&
  1100. nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
  1101. goto nla_put_failure;
  1102. #endif
  1103. if (fi->fib_nh->nh_lwtstate &&
  1104. lwtunnel_fill_encap(skb, fi->fib_nh->nh_lwtstate) < 0)
  1105. goto nla_put_failure;
  1106. }
  1107. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1108. if (fi->fib_nhs > 1) {
  1109. struct rtnexthop *rtnh;
  1110. struct nlattr *mp;
  1111. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1112. if (!mp)
  1113. goto nla_put_failure;
  1114. for_nexthops(fi) {
  1115. struct in_device *in_dev;
  1116. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1117. if (!rtnh)
  1118. goto nla_put_failure;
  1119. rtnh->rtnh_flags = nh->nh_flags & 0xFF;
  1120. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1121. in_dev = __in_dev_get_rtnl(nh->nh_dev);
  1122. if (in_dev &&
  1123. IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
  1124. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1125. }
  1126. rtnh->rtnh_hops = nh->nh_weight - 1;
  1127. rtnh->rtnh_ifindex = nh->nh_oif;
  1128. if (nh->nh_gw &&
  1129. nla_put_in_addr(skb, RTA_GATEWAY, nh->nh_gw))
  1130. goto nla_put_failure;
  1131. #ifdef CONFIG_IP_ROUTE_CLASSID
  1132. if (nh->nh_tclassid &&
  1133. nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
  1134. goto nla_put_failure;
  1135. #endif
  1136. if (nh->nh_lwtstate &&
  1137. lwtunnel_fill_encap(skb, nh->nh_lwtstate) < 0)
  1138. goto nla_put_failure;
  1139. /* length of rtnetlink header + attributes */
  1140. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
  1141. } endfor_nexthops(fi);
  1142. nla_nest_end(skb, mp);
  1143. }
  1144. #endif
  1145. nlmsg_end(skb, nlh);
  1146. return 0;
  1147. nla_put_failure:
  1148. nlmsg_cancel(skb, nlh);
  1149. return -EMSGSIZE;
  1150. }
  1151. /*
  1152. * Update FIB if:
  1153. * - local address disappeared -> we must delete all the entries
  1154. * referring to it.
  1155. * - device went down -> we must shutdown all nexthops going via it.
  1156. */
  1157. int fib_sync_down_addr(struct net_device *dev, __be32 local)
  1158. {
  1159. int ret = 0;
  1160. unsigned int hash = fib_laddr_hashfn(local);
  1161. struct hlist_head *head = &fib_info_laddrhash[hash];
  1162. struct net *net = dev_net(dev);
  1163. int tb_id = l3mdev_fib_table(dev);
  1164. struct fib_info *fi;
  1165. if (!fib_info_laddrhash || local == 0)
  1166. return 0;
  1167. hlist_for_each_entry(fi, head, fib_lhash) {
  1168. if (!net_eq(fi->fib_net, net) ||
  1169. fi->fib_tb_id != tb_id)
  1170. continue;
  1171. if (fi->fib_prefsrc == local) {
  1172. fi->fib_flags |= RTNH_F_DEAD;
  1173. ret++;
  1174. }
  1175. }
  1176. return ret;
  1177. }
  1178. /* Event force Flags Description
  1179. * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
  1180. * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
  1181. * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
  1182. * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
  1183. */
  1184. int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
  1185. {
  1186. int ret = 0;
  1187. int scope = RT_SCOPE_NOWHERE;
  1188. struct fib_info *prev_fi = NULL;
  1189. unsigned int hash = fib_devindex_hashfn(dev->ifindex);
  1190. struct hlist_head *head = &fib_info_devhash[hash];
  1191. struct fib_nh *nh;
  1192. if (force)
  1193. scope = -1;
  1194. hlist_for_each_entry(nh, head, nh_hash) {
  1195. struct fib_info *fi = nh->nh_parent;
  1196. int dead;
  1197. BUG_ON(!fi->fib_nhs);
  1198. if (nh->nh_dev != dev || fi == prev_fi)
  1199. continue;
  1200. prev_fi = fi;
  1201. dead = 0;
  1202. change_nexthops(fi) {
  1203. if (nexthop_nh->nh_flags & RTNH_F_DEAD)
  1204. dead++;
  1205. else if (nexthop_nh->nh_dev == dev &&
  1206. nexthop_nh->nh_scope != scope) {
  1207. switch (event) {
  1208. case NETDEV_DOWN:
  1209. case NETDEV_UNREGISTER:
  1210. nexthop_nh->nh_flags |= RTNH_F_DEAD;
  1211. /* fall through */
  1212. case NETDEV_CHANGE:
  1213. nexthop_nh->nh_flags |= RTNH_F_LINKDOWN;
  1214. break;
  1215. }
  1216. dead++;
  1217. }
  1218. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1219. if (event == NETDEV_UNREGISTER &&
  1220. nexthop_nh->nh_dev == dev) {
  1221. dead = fi->fib_nhs;
  1222. break;
  1223. }
  1224. #endif
  1225. } endfor_nexthops(fi)
  1226. if (dead == fi->fib_nhs) {
  1227. switch (event) {
  1228. case NETDEV_DOWN:
  1229. case NETDEV_UNREGISTER:
  1230. fi->fib_flags |= RTNH_F_DEAD;
  1231. /* fall through */
  1232. case NETDEV_CHANGE:
  1233. fi->fib_flags |= RTNH_F_LINKDOWN;
  1234. break;
  1235. }
  1236. ret++;
  1237. }
  1238. fib_rebalance(fi);
  1239. }
  1240. return ret;
  1241. }
  1242. /* Must be invoked inside of an RCU protected region. */
  1243. void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
  1244. {
  1245. struct fib_info *fi = NULL, *last_resort = NULL;
  1246. struct hlist_head *fa_head = res->fa_head;
  1247. struct fib_table *tb = res->table;
  1248. u8 slen = 32 - res->prefixlen;
  1249. int order = -1, last_idx = -1;
  1250. struct fib_alias *fa, *fa1 = NULL;
  1251. u32 last_prio = res->fi->fib_priority;
  1252. u8 last_tos = 0;
  1253. hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
  1254. struct fib_info *next_fi = fa->fa_info;
  1255. if (fa->fa_slen != slen)
  1256. continue;
  1257. if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
  1258. continue;
  1259. if (fa->tb_id != tb->tb_id)
  1260. continue;
  1261. if (next_fi->fib_priority > last_prio &&
  1262. fa->fa_tos == last_tos) {
  1263. if (last_tos)
  1264. continue;
  1265. break;
  1266. }
  1267. if (next_fi->fib_flags & RTNH_F_DEAD)
  1268. continue;
  1269. last_tos = fa->fa_tos;
  1270. last_prio = next_fi->fib_priority;
  1271. if (next_fi->fib_scope != res->scope ||
  1272. fa->fa_type != RTN_UNICAST)
  1273. continue;
  1274. if (!next_fi->fib_nh[0].nh_gw ||
  1275. next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
  1276. continue;
  1277. fib_alias_accessed(fa);
  1278. if (!fi) {
  1279. if (next_fi != res->fi)
  1280. break;
  1281. fa1 = fa;
  1282. } else if (!fib_detect_death(fi, order, &last_resort,
  1283. &last_idx, fa1->fa_default)) {
  1284. fib_result_assign(res, fi);
  1285. fa1->fa_default = order;
  1286. goto out;
  1287. }
  1288. fi = next_fi;
  1289. order++;
  1290. }
  1291. if (order <= 0 || !fi) {
  1292. if (fa1)
  1293. fa1->fa_default = -1;
  1294. goto out;
  1295. }
  1296. if (!fib_detect_death(fi, order, &last_resort, &last_idx,
  1297. fa1->fa_default)) {
  1298. fib_result_assign(res, fi);
  1299. fa1->fa_default = order;
  1300. goto out;
  1301. }
  1302. if (last_idx >= 0)
  1303. fib_result_assign(res, last_resort);
  1304. fa1->fa_default = last_idx;
  1305. out:
  1306. return;
  1307. }
  1308. /*
  1309. * Dead device goes up. We wake up dead nexthops.
  1310. * It takes sense only on multipath routes.
  1311. */
  1312. int fib_sync_up(struct net_device *dev, unsigned int nh_flags)
  1313. {
  1314. struct fib_info *prev_fi;
  1315. unsigned int hash;
  1316. struct hlist_head *head;
  1317. struct fib_nh *nh;
  1318. int ret;
  1319. if (!(dev->flags & IFF_UP))
  1320. return 0;
  1321. if (nh_flags & RTNH_F_DEAD) {
  1322. unsigned int flags = dev_get_flags(dev);
  1323. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1324. nh_flags |= RTNH_F_LINKDOWN;
  1325. }
  1326. prev_fi = NULL;
  1327. hash = fib_devindex_hashfn(dev->ifindex);
  1328. head = &fib_info_devhash[hash];
  1329. ret = 0;
  1330. hlist_for_each_entry(nh, head, nh_hash) {
  1331. struct fib_info *fi = nh->nh_parent;
  1332. int alive;
  1333. BUG_ON(!fi->fib_nhs);
  1334. if (nh->nh_dev != dev || fi == prev_fi)
  1335. continue;
  1336. prev_fi = fi;
  1337. alive = 0;
  1338. change_nexthops(fi) {
  1339. if (!(nexthop_nh->nh_flags & nh_flags)) {
  1340. alive++;
  1341. continue;
  1342. }
  1343. if (!nexthop_nh->nh_dev ||
  1344. !(nexthop_nh->nh_dev->flags & IFF_UP))
  1345. continue;
  1346. if (nexthop_nh->nh_dev != dev ||
  1347. !__in_dev_get_rtnl(dev))
  1348. continue;
  1349. alive++;
  1350. nexthop_nh->nh_flags &= ~nh_flags;
  1351. } endfor_nexthops(fi)
  1352. if (alive > 0) {
  1353. fi->fib_flags &= ~nh_flags;
  1354. ret++;
  1355. }
  1356. fib_rebalance(fi);
  1357. }
  1358. return ret;
  1359. }
  1360. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1361. static bool fib_good_nh(const struct fib_nh *nh)
  1362. {
  1363. int state = NUD_REACHABLE;
  1364. if (nh->nh_scope == RT_SCOPE_LINK) {
  1365. struct neighbour *n;
  1366. rcu_read_lock_bh();
  1367. n = __ipv4_neigh_lookup_noref(nh->nh_dev,
  1368. (__force u32)nh->nh_gw);
  1369. if (n)
  1370. state = n->nud_state;
  1371. rcu_read_unlock_bh();
  1372. }
  1373. return !!(state & NUD_VALID);
  1374. }
  1375. void fib_select_multipath(struct fib_result *res, int hash)
  1376. {
  1377. struct fib_info *fi = res->fi;
  1378. struct net *net = fi->fib_net;
  1379. bool first = false;
  1380. for_nexthops(fi) {
  1381. if (net->ipv4.sysctl_fib_multipath_use_neigh) {
  1382. if (!fib_good_nh(nh))
  1383. continue;
  1384. if (!first) {
  1385. res->nh_sel = nhsel;
  1386. first = true;
  1387. }
  1388. }
  1389. if (hash > atomic_read(&nh->nh_upper_bound))
  1390. continue;
  1391. res->nh_sel = nhsel;
  1392. return;
  1393. } endfor_nexthops(fi);
  1394. }
  1395. #endif
  1396. void fib_select_path(struct net *net, struct fib_result *res,
  1397. struct flowi4 *fl4, int mp_hash)
  1398. {
  1399. bool oif_check;
  1400. oif_check = (fl4->flowi4_oif == 0 ||
  1401. fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF);
  1402. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1403. if (res->fi->fib_nhs > 1 && oif_check) {
  1404. if (mp_hash < 0)
  1405. mp_hash = get_hash_from_flowi4(fl4) >> 1;
  1406. fib_select_multipath(res, mp_hash);
  1407. }
  1408. else
  1409. #endif
  1410. if (!res->prefixlen &&
  1411. res->table->tb_num_default > 1 &&
  1412. res->type == RTN_UNICAST && oif_check)
  1413. fib_select_default(fl4, res);
  1414. if (!fl4->saddr)
  1415. fl4->saddr = FIB_RES_PREFSRC(net, *res);
  1416. }
  1417. EXPORT_SYMBOL_GPL(fib_select_path);