fib_semantics.c 42 KB

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