fib_semantics.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * IPv4 Forwarding Information Base: semantics.
  8. *
  9. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10. */
  11. #include <linux/uaccess.h>
  12. #include <linux/bitops.h>
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/mm.h>
  17. #include <linux/string.h>
  18. #include <linux/socket.h>
  19. #include <linux/sockios.h>
  20. #include <linux/errno.h>
  21. #include <linux/in.h>
  22. #include <linux/inet.h>
  23. #include <linux/inetdevice.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/if_arp.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/netlink.h>
  31. #include <linux/hash.h>
  32. #include <linux/nospec.h>
  33. #include <net/arp.h>
  34. #include <net/inet_dscp.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/ip6_fib.h>
  42. #include <net/nexthop.h>
  43. #include <net/netlink.h>
  44. #include <net/rtnh.h>
  45. #include <net/lwtunnel.h>
  46. #include <net/fib_notifier.h>
  47. #include <net/addrconf.h>
  48. #include "fib_lookup.h"
  49. static DEFINE_SPINLOCK(fib_info_lock);
  50. static struct hlist_head *fib_info_hash;
  51. static struct hlist_head *fib_info_laddrhash;
  52. static unsigned int fib_info_hash_size;
  53. static unsigned int fib_info_hash_bits;
  54. static unsigned int fib_info_cnt;
  55. #define DEVINDEX_HASHBITS 8
  56. #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
  57. static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
  58. /* for_nexthops and change_nexthops only used when nexthop object
  59. * is not set in a fib_info. The logic within can reference fib_nh.
  60. */
  61. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  62. #define for_nexthops(fi) { \
  63. int nhsel; const struct fib_nh *nh; \
  64. for (nhsel = 0, nh = (fi)->fib_nh; \
  65. nhsel < fib_info_num_path((fi)); \
  66. nh++, nhsel++)
  67. #define change_nexthops(fi) { \
  68. int nhsel; struct fib_nh *nexthop_nh; \
  69. for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
  70. nhsel < fib_info_num_path((fi)); \
  71. nexthop_nh++, nhsel++)
  72. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  73. /* Hope, that gcc will optimize it to get rid of dummy loop */
  74. #define for_nexthops(fi) { \
  75. int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
  76. for (nhsel = 0; nhsel < 1; nhsel++)
  77. #define change_nexthops(fi) { \
  78. int nhsel; \
  79. struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
  80. for (nhsel = 0; nhsel < 1; nhsel++)
  81. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  82. #define endfor_nexthops(fi) }
  83. const struct fib_prop fib_props[RTN_MAX + 1] = {
  84. [RTN_UNSPEC] = {
  85. .error = 0,
  86. .scope = RT_SCOPE_NOWHERE,
  87. },
  88. [RTN_UNICAST] = {
  89. .error = 0,
  90. .scope = RT_SCOPE_UNIVERSE,
  91. },
  92. [RTN_LOCAL] = {
  93. .error = 0,
  94. .scope = RT_SCOPE_HOST,
  95. },
  96. [RTN_BROADCAST] = {
  97. .error = 0,
  98. .scope = RT_SCOPE_LINK,
  99. },
  100. [RTN_ANYCAST] = {
  101. .error = 0,
  102. .scope = RT_SCOPE_LINK,
  103. },
  104. [RTN_MULTICAST] = {
  105. .error = 0,
  106. .scope = RT_SCOPE_UNIVERSE,
  107. },
  108. [RTN_BLACKHOLE] = {
  109. .error = -EINVAL,
  110. .scope = RT_SCOPE_UNIVERSE,
  111. },
  112. [RTN_UNREACHABLE] = {
  113. .error = -EHOSTUNREACH,
  114. .scope = RT_SCOPE_UNIVERSE,
  115. },
  116. [RTN_PROHIBIT] = {
  117. .error = -EACCES,
  118. .scope = RT_SCOPE_UNIVERSE,
  119. },
  120. [RTN_THROW] = {
  121. .error = -EAGAIN,
  122. .scope = RT_SCOPE_UNIVERSE,
  123. },
  124. [RTN_NAT] = {
  125. .error = -EINVAL,
  126. .scope = RT_SCOPE_NOWHERE,
  127. },
  128. [RTN_XRESOLVE] = {
  129. .error = -EINVAL,
  130. .scope = RT_SCOPE_NOWHERE,
  131. },
  132. };
  133. static void rt_fibinfo_free(struct rtable __rcu **rtp)
  134. {
  135. struct rtable *rt = rcu_dereference_protected(*rtp, 1);
  136. if (!rt)
  137. return;
  138. /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
  139. * because we waited an RCU grace period before calling
  140. * free_fib_info_rcu()
  141. */
  142. dst_dev_put(&rt->dst);
  143. dst_release_immediate(&rt->dst);
  144. }
  145. static void free_nh_exceptions(struct fib_nh_common *nhc)
  146. {
  147. struct fnhe_hash_bucket *hash;
  148. int i;
  149. hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
  150. if (!hash)
  151. return;
  152. for (i = 0; i < FNHE_HASH_SIZE; i++) {
  153. struct fib_nh_exception *fnhe;
  154. fnhe = rcu_dereference_protected(hash[i].chain, 1);
  155. while (fnhe) {
  156. struct fib_nh_exception *next;
  157. next = rcu_dereference_protected(fnhe->fnhe_next, 1);
  158. rt_fibinfo_free(&fnhe->fnhe_rth_input);
  159. rt_fibinfo_free(&fnhe->fnhe_rth_output);
  160. kfree(fnhe);
  161. fnhe = next;
  162. }
  163. }
  164. kfree(hash);
  165. }
  166. static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
  167. {
  168. int cpu;
  169. if (!rtp)
  170. return;
  171. for_each_possible_cpu(cpu) {
  172. struct rtable *rt;
  173. rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
  174. if (rt) {
  175. dst_dev_put(&rt->dst);
  176. dst_release_immediate(&rt->dst);
  177. }
  178. }
  179. free_percpu(rtp);
  180. }
  181. void fib_nh_common_release(struct fib_nh_common *nhc)
  182. {
  183. netdev_put(nhc->nhc_dev, &nhc->nhc_dev_tracker);
  184. lwtstate_put(nhc->nhc_lwtstate);
  185. rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
  186. rt_fibinfo_free(&nhc->nhc_rth_input);
  187. free_nh_exceptions(nhc);
  188. }
  189. EXPORT_SYMBOL_GPL(fib_nh_common_release);
  190. void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
  191. {
  192. #ifdef CONFIG_IP_ROUTE_CLASSID
  193. if (fib_nh->nh_tclassid)
  194. atomic_dec(&net->ipv4.fib_num_tclassid_users);
  195. #endif
  196. fib_nh_common_release(&fib_nh->nh_common);
  197. }
  198. /* Release a nexthop info record */
  199. static void free_fib_info_rcu(struct rcu_head *head)
  200. {
  201. struct fib_info *fi = container_of(head, struct fib_info, rcu);
  202. if (fi->nh) {
  203. nexthop_put(fi->nh);
  204. } else {
  205. change_nexthops(fi) {
  206. fib_nh_release(fi->fib_net, nexthop_nh);
  207. } endfor_nexthops(fi);
  208. }
  209. ip_fib_metrics_put(fi->fib_metrics);
  210. kfree(fi);
  211. }
  212. void free_fib_info(struct fib_info *fi)
  213. {
  214. if (fi->fib_dead == 0) {
  215. pr_warn("Freeing alive fib_info %p\n", fi);
  216. return;
  217. }
  218. call_rcu_hurry(&fi->rcu, free_fib_info_rcu);
  219. }
  220. EXPORT_SYMBOL_GPL(free_fib_info);
  221. void fib_release_info(struct fib_info *fi)
  222. {
  223. spin_lock_bh(&fib_info_lock);
  224. if (fi && refcount_dec_and_test(&fi->fib_treeref)) {
  225. hlist_del(&fi->fib_hash);
  226. /* Paired with READ_ONCE() in fib_create_info(). */
  227. WRITE_ONCE(fib_info_cnt, fib_info_cnt - 1);
  228. if (fi->fib_prefsrc)
  229. hlist_del(&fi->fib_lhash);
  230. if (fi->nh) {
  231. list_del(&fi->nh_list);
  232. } else {
  233. change_nexthops(fi) {
  234. if (!nexthop_nh->fib_nh_dev)
  235. continue;
  236. hlist_del(&nexthop_nh->nh_hash);
  237. } endfor_nexthops(fi)
  238. }
  239. /* Paired with READ_ONCE() from fib_table_lookup() */
  240. WRITE_ONCE(fi->fib_dead, 1);
  241. fib_info_put(fi);
  242. }
  243. spin_unlock_bh(&fib_info_lock);
  244. }
  245. static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
  246. {
  247. const struct fib_nh *onh;
  248. if (fi->nh || ofi->nh)
  249. return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1;
  250. if (ofi->fib_nhs == 0)
  251. return 0;
  252. for_nexthops(fi) {
  253. onh = fib_info_nh(ofi, nhsel);
  254. if (nh->fib_nh_oif != onh->fib_nh_oif ||
  255. nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
  256. nh->fib_nh_scope != onh->fib_nh_scope ||
  257. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  258. nh->fib_nh_weight != onh->fib_nh_weight ||
  259. #endif
  260. #ifdef CONFIG_IP_ROUTE_CLASSID
  261. nh->nh_tclassid != onh->nh_tclassid ||
  262. #endif
  263. lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
  264. ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
  265. return -1;
  266. if (nh->fib_nh_gw_family == AF_INET &&
  267. nh->fib_nh_gw4 != onh->fib_nh_gw4)
  268. return -1;
  269. if (nh->fib_nh_gw_family == AF_INET6 &&
  270. ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
  271. return -1;
  272. } endfor_nexthops(fi);
  273. return 0;
  274. }
  275. static inline unsigned int fib_devindex_hashfn(unsigned int val)
  276. {
  277. return hash_32(val, DEVINDEX_HASHBITS);
  278. }
  279. static struct hlist_head *
  280. fib_info_devhash_bucket(const struct net_device *dev)
  281. {
  282. u32 val = net_hash_mix(dev_net(dev)) ^ dev->ifindex;
  283. return &fib_info_devhash[fib_devindex_hashfn(val)];
  284. }
  285. static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
  286. u32 prefsrc, u32 priority)
  287. {
  288. unsigned int val = init_val;
  289. val ^= (protocol << 8) | scope;
  290. val ^= prefsrc;
  291. val ^= priority;
  292. return val;
  293. }
  294. static unsigned int fib_info_hashfn_result(unsigned int val)
  295. {
  296. unsigned int mask = (fib_info_hash_size - 1);
  297. return (val ^ (val >> 7) ^ (val >> 12)) & mask;
  298. }
  299. static inline unsigned int fib_info_hashfn(struct fib_info *fi)
  300. {
  301. unsigned int val;
  302. val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol,
  303. fi->fib_scope, (__force u32)fi->fib_prefsrc,
  304. fi->fib_priority);
  305. if (fi->nh) {
  306. val ^= fib_devindex_hashfn(fi->nh->id);
  307. } else {
  308. for_nexthops(fi) {
  309. val ^= fib_devindex_hashfn(nh->fib_nh_oif);
  310. } endfor_nexthops(fi)
  311. }
  312. return fib_info_hashfn_result(val);
  313. }
  314. /* no metrics, only nexthop id */
  315. static struct fib_info *fib_find_info_nh(struct net *net,
  316. const struct fib_config *cfg)
  317. {
  318. struct hlist_head *head;
  319. struct fib_info *fi;
  320. unsigned int hash;
  321. hash = fib_info_hashfn_1(fib_devindex_hashfn(cfg->fc_nh_id),
  322. cfg->fc_protocol, cfg->fc_scope,
  323. (__force u32)cfg->fc_prefsrc,
  324. cfg->fc_priority);
  325. hash = fib_info_hashfn_result(hash);
  326. head = &fib_info_hash[hash];
  327. hlist_for_each_entry(fi, head, fib_hash) {
  328. if (!net_eq(fi->fib_net, net))
  329. continue;
  330. if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
  331. continue;
  332. if (cfg->fc_protocol == fi->fib_protocol &&
  333. cfg->fc_scope == fi->fib_scope &&
  334. cfg->fc_prefsrc == fi->fib_prefsrc &&
  335. cfg->fc_priority == fi->fib_priority &&
  336. cfg->fc_type == fi->fib_type &&
  337. cfg->fc_table == fi->fib_tb_id &&
  338. !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
  339. return fi;
  340. }
  341. return NULL;
  342. }
  343. static struct fib_info *fib_find_info(struct fib_info *nfi)
  344. {
  345. struct hlist_head *head;
  346. struct fib_info *fi;
  347. unsigned int hash;
  348. hash = fib_info_hashfn(nfi);
  349. head = &fib_info_hash[hash];
  350. hlist_for_each_entry(fi, head, fib_hash) {
  351. if (!net_eq(fi->fib_net, nfi->fib_net))
  352. continue;
  353. if (fi->fib_nhs != nfi->fib_nhs)
  354. continue;
  355. if (nfi->fib_protocol == fi->fib_protocol &&
  356. nfi->fib_scope == fi->fib_scope &&
  357. nfi->fib_prefsrc == fi->fib_prefsrc &&
  358. nfi->fib_priority == fi->fib_priority &&
  359. nfi->fib_type == fi->fib_type &&
  360. nfi->fib_tb_id == fi->fib_tb_id &&
  361. memcmp(nfi->fib_metrics, fi->fib_metrics,
  362. sizeof(u32) * RTAX_MAX) == 0 &&
  363. !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
  364. nh_comp(fi, nfi) == 0)
  365. return fi;
  366. }
  367. return NULL;
  368. }
  369. /* Check, that the gateway is already configured.
  370. * Used only by redirect accept routine.
  371. */
  372. int ip_fib_check_default(__be32 gw, struct net_device *dev)
  373. {
  374. struct hlist_head *head;
  375. struct fib_nh *nh;
  376. spin_lock(&fib_info_lock);
  377. head = fib_info_devhash_bucket(dev);
  378. hlist_for_each_entry(nh, head, nh_hash) {
  379. if (nh->fib_nh_dev == dev &&
  380. nh->fib_nh_gw4 == gw &&
  381. !(nh->fib_nh_flags & RTNH_F_DEAD)) {
  382. spin_unlock(&fib_info_lock);
  383. return 0;
  384. }
  385. }
  386. spin_unlock(&fib_info_lock);
  387. return -1;
  388. }
  389. size_t fib_nlmsg_size(struct fib_info *fi)
  390. {
  391. size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
  392. + nla_total_size(4) /* RTA_TABLE */
  393. + nla_total_size(4) /* RTA_DST */
  394. + nla_total_size(4) /* RTA_PRIORITY */
  395. + nla_total_size(4) /* RTA_PREFSRC */
  396. + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
  397. unsigned int nhs = fib_info_num_path(fi);
  398. /* space for nested metrics */
  399. payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
  400. if (fi->nh)
  401. payload += nla_total_size(4); /* RTA_NH_ID */
  402. if (nhs) {
  403. size_t nh_encapsize = 0;
  404. /* Also handles the special case nhs == 1 */
  405. /* each nexthop is packed in an attribute */
  406. size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
  407. unsigned int i;
  408. /* may contain flow and gateway attribute */
  409. nhsize += 2 * nla_total_size(4);
  410. /* grab encap info */
  411. for (i = 0; i < fib_info_num_path(fi); i++) {
  412. struct fib_nh_common *nhc = fib_info_nhc(fi, i);
  413. if (nhc->nhc_lwtstate) {
  414. /* RTA_ENCAP_TYPE */
  415. nh_encapsize += lwtunnel_get_encap_size(
  416. nhc->nhc_lwtstate);
  417. /* RTA_ENCAP */
  418. nh_encapsize += nla_total_size(2);
  419. }
  420. }
  421. /* all nexthops are packed in a nested attribute */
  422. payload += nla_total_size((nhs * nhsize) + nh_encapsize);
  423. }
  424. return payload;
  425. }
  426. void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
  427. int dst_len, u32 tb_id, const struct nl_info *info,
  428. unsigned int nlm_flags)
  429. {
  430. struct fib_rt_info fri;
  431. struct sk_buff *skb;
  432. u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
  433. int err = -ENOBUFS;
  434. skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
  435. if (!skb)
  436. goto errout;
  437. fri.fi = fa->fa_info;
  438. fri.tb_id = tb_id;
  439. fri.dst = key;
  440. fri.dst_len = dst_len;
  441. fri.dscp = fa->fa_dscp;
  442. fri.type = fa->fa_type;
  443. fri.offload = READ_ONCE(fa->offload);
  444. fri.trap = READ_ONCE(fa->trap);
  445. fri.offload_failed = READ_ONCE(fa->offload_failed);
  446. err = fib_dump_info(skb, info->portid, seq, event, &fri, nlm_flags);
  447. if (err < 0) {
  448. /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
  449. WARN_ON(err == -EMSGSIZE);
  450. kfree_skb(skb);
  451. goto errout;
  452. }
  453. rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
  454. info->nlh, GFP_KERNEL);
  455. return;
  456. errout:
  457. rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
  458. }
  459. static int fib_detect_death(struct fib_info *fi, int order,
  460. struct fib_info **last_resort, int *last_idx,
  461. int dflt)
  462. {
  463. const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
  464. struct neighbour *n;
  465. int state = NUD_NONE;
  466. if (likely(nhc->nhc_gw_family == AF_INET))
  467. n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
  468. else if (nhc->nhc_gw_family == AF_INET6)
  469. n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
  470. nhc->nhc_dev);
  471. else
  472. n = NULL;
  473. if (n) {
  474. state = READ_ONCE(n->nud_state);
  475. neigh_release(n);
  476. } else {
  477. return 0;
  478. }
  479. if (state == NUD_REACHABLE)
  480. return 0;
  481. if ((state & NUD_VALID) && order != dflt)
  482. return 0;
  483. if ((state & NUD_VALID) ||
  484. (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
  485. *last_resort = fi;
  486. *last_idx = order;
  487. }
  488. return 1;
  489. }
  490. int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
  491. struct nlattr *encap, u16 encap_type,
  492. void *cfg, gfp_t gfp_flags,
  493. struct netlink_ext_ack *extack)
  494. {
  495. int err;
  496. nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
  497. gfp_flags);
  498. if (!nhc->nhc_pcpu_rth_output)
  499. return -ENOMEM;
  500. if (encap) {
  501. struct lwtunnel_state *lwtstate;
  502. if (encap_type == LWTUNNEL_ENCAP_NONE) {
  503. NL_SET_ERR_MSG(extack, "LWT encap type not specified");
  504. err = -EINVAL;
  505. goto lwt_failure;
  506. }
  507. err = lwtunnel_build_state(net, encap_type, encap,
  508. nhc->nhc_family, cfg, &lwtstate,
  509. extack);
  510. if (err)
  511. goto lwt_failure;
  512. nhc->nhc_lwtstate = lwtstate_get(lwtstate);
  513. }
  514. return 0;
  515. lwt_failure:
  516. rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
  517. nhc->nhc_pcpu_rth_output = NULL;
  518. return err;
  519. }
  520. EXPORT_SYMBOL_GPL(fib_nh_common_init);
  521. int fib_nh_init(struct net *net, struct fib_nh *nh,
  522. struct fib_config *cfg, int nh_weight,
  523. struct netlink_ext_ack *extack)
  524. {
  525. int err;
  526. nh->fib_nh_family = AF_INET;
  527. err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap,
  528. cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
  529. if (err)
  530. return err;
  531. nh->fib_nh_oif = cfg->fc_oif;
  532. nh->fib_nh_gw_family = cfg->fc_gw_family;
  533. if (cfg->fc_gw_family == AF_INET)
  534. nh->fib_nh_gw4 = cfg->fc_gw4;
  535. else if (cfg->fc_gw_family == AF_INET6)
  536. nh->fib_nh_gw6 = cfg->fc_gw6;
  537. nh->fib_nh_flags = cfg->fc_flags;
  538. #ifdef CONFIG_IP_ROUTE_CLASSID
  539. nh->nh_tclassid = cfg->fc_flow;
  540. if (nh->nh_tclassid)
  541. atomic_inc(&net->ipv4.fib_num_tclassid_users);
  542. #endif
  543. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  544. nh->fib_nh_weight = nh_weight;
  545. #endif
  546. return 0;
  547. }
  548. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  549. static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
  550. struct netlink_ext_ack *extack)
  551. {
  552. int nhs = 0;
  553. while (rtnh_ok(rtnh, remaining)) {
  554. nhs++;
  555. rtnh = rtnh_next(rtnh, &remaining);
  556. }
  557. /* leftover implies invalid nexthop configuration, discard it */
  558. if (remaining > 0) {
  559. NL_SET_ERR_MSG(extack,
  560. "Invalid nexthop configuration - extra data after nexthops");
  561. nhs = 0;
  562. }
  563. return nhs;
  564. }
  565. static int fib_gw_from_attr(__be32 *gw, struct nlattr *nla,
  566. struct netlink_ext_ack *extack)
  567. {
  568. if (nla_len(nla) < sizeof(*gw)) {
  569. NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
  570. return -EINVAL;
  571. }
  572. *gw = nla_get_in_addr(nla);
  573. return 0;
  574. }
  575. /* only called when fib_nh is integrated into fib_info */
  576. static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
  577. int remaining, struct fib_config *cfg,
  578. struct netlink_ext_ack *extack)
  579. {
  580. struct net *net = fi->fib_net;
  581. struct fib_config fib_cfg;
  582. struct fib_nh *nh;
  583. int ret;
  584. change_nexthops(fi) {
  585. int attrlen;
  586. memset(&fib_cfg, 0, sizeof(fib_cfg));
  587. if (!rtnh_ok(rtnh, remaining)) {
  588. NL_SET_ERR_MSG(extack,
  589. "Invalid nexthop configuration - extra data after nexthop");
  590. return -EINVAL;
  591. }
  592. if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
  593. NL_SET_ERR_MSG(extack,
  594. "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
  595. return -EINVAL;
  596. }
  597. fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
  598. fib_cfg.fc_oif = rtnh->rtnh_ifindex;
  599. attrlen = rtnh_attrlen(rtnh);
  600. if (attrlen > 0) {
  601. struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
  602. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  603. nlav = nla_find(attrs, attrlen, RTA_VIA);
  604. if (nla && nlav) {
  605. NL_SET_ERR_MSG(extack,
  606. "Nexthop configuration can not contain both GATEWAY and VIA");
  607. return -EINVAL;
  608. }
  609. if (nla) {
  610. ret = fib_gw_from_attr(&fib_cfg.fc_gw4, nla,
  611. extack);
  612. if (ret)
  613. goto errout;
  614. if (fib_cfg.fc_gw4)
  615. fib_cfg.fc_gw_family = AF_INET;
  616. } else if (nlav) {
  617. ret = fib_gw_from_via(&fib_cfg, nlav, extack);
  618. if (ret)
  619. goto errout;
  620. }
  621. nla = nla_find(attrs, attrlen, RTA_FLOW);
  622. if (nla) {
  623. if (nla_len(nla) < sizeof(u32)) {
  624. NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
  625. return -EINVAL;
  626. }
  627. fib_cfg.fc_flow = nla_get_u32(nla);
  628. }
  629. fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
  630. /* RTA_ENCAP_TYPE length checked in
  631. * lwtunnel_valid_encap_type_attr
  632. */
  633. nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
  634. if (nla)
  635. fib_cfg.fc_encap_type = nla_get_u16(nla);
  636. }
  637. ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
  638. rtnh->rtnh_hops + 1, extack);
  639. if (ret)
  640. goto errout;
  641. rtnh = rtnh_next(rtnh, &remaining);
  642. } endfor_nexthops(fi);
  643. ret = -EINVAL;
  644. nh = fib_info_nh(fi, 0);
  645. if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
  646. NL_SET_ERR_MSG(extack,
  647. "Nexthop device index does not match RTA_OIF");
  648. goto errout;
  649. }
  650. if (cfg->fc_gw_family) {
  651. if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
  652. (cfg->fc_gw_family == AF_INET &&
  653. nh->fib_nh_gw4 != cfg->fc_gw4) ||
  654. (cfg->fc_gw_family == AF_INET6 &&
  655. ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
  656. NL_SET_ERR_MSG(extack,
  657. "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
  658. goto errout;
  659. }
  660. }
  661. #ifdef CONFIG_IP_ROUTE_CLASSID
  662. if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
  663. NL_SET_ERR_MSG(extack,
  664. "Nexthop class id does not match RTA_FLOW");
  665. goto errout;
  666. }
  667. #endif
  668. ret = 0;
  669. errout:
  670. return ret;
  671. }
  672. /* only called when fib_nh is integrated into fib_info */
  673. static void fib_rebalance(struct fib_info *fi)
  674. {
  675. int total;
  676. int w;
  677. if (fib_info_num_path(fi) < 2)
  678. return;
  679. total = 0;
  680. for_nexthops(fi) {
  681. if (nh->fib_nh_flags & RTNH_F_DEAD)
  682. continue;
  683. if (ip_ignore_linkdown(nh->fib_nh_dev) &&
  684. nh->fib_nh_flags & RTNH_F_LINKDOWN)
  685. continue;
  686. total += nh->fib_nh_weight;
  687. } endfor_nexthops(fi);
  688. w = 0;
  689. change_nexthops(fi) {
  690. int upper_bound;
  691. if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
  692. upper_bound = -1;
  693. } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
  694. nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
  695. upper_bound = -1;
  696. } else {
  697. w += nexthop_nh->fib_nh_weight;
  698. upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
  699. total) - 1;
  700. }
  701. atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
  702. } endfor_nexthops(fi);
  703. }
  704. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  705. static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
  706. int remaining, struct fib_config *cfg,
  707. struct netlink_ext_ack *extack)
  708. {
  709. NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
  710. return -EINVAL;
  711. }
  712. #define fib_rebalance(fi) do { } while (0)
  713. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  714. static int fib_encap_match(struct net *net, u16 encap_type,
  715. struct nlattr *encap,
  716. const struct fib_nh *nh,
  717. const struct fib_config *cfg,
  718. struct netlink_ext_ack *extack)
  719. {
  720. struct lwtunnel_state *lwtstate;
  721. int ret, result = 0;
  722. if (encap_type == LWTUNNEL_ENCAP_NONE)
  723. return 0;
  724. ret = lwtunnel_build_state(net, encap_type, encap, AF_INET,
  725. cfg, &lwtstate, extack);
  726. if (!ret) {
  727. result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
  728. lwtstate_free(lwtstate);
  729. }
  730. return result;
  731. }
  732. int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
  733. struct netlink_ext_ack *extack)
  734. {
  735. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  736. struct rtnexthop *rtnh;
  737. int remaining;
  738. #endif
  739. if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
  740. return 1;
  741. if (cfg->fc_nh_id) {
  742. if (fi->nh && cfg->fc_nh_id == fi->nh->id)
  743. return 0;
  744. return 1;
  745. }
  746. if (fi->nh) {
  747. if (cfg->fc_oif || cfg->fc_gw_family || cfg->fc_mp)
  748. return 1;
  749. return 0;
  750. }
  751. if (cfg->fc_oif || cfg->fc_gw_family) {
  752. struct fib_nh *nh;
  753. nh = fib_info_nh(fi, 0);
  754. if (cfg->fc_encap) {
  755. if (fib_encap_match(net, cfg->fc_encap_type,
  756. cfg->fc_encap, nh, cfg, extack))
  757. return 1;
  758. }
  759. #ifdef CONFIG_IP_ROUTE_CLASSID
  760. if (cfg->fc_flow &&
  761. cfg->fc_flow != nh->nh_tclassid)
  762. return 1;
  763. #endif
  764. if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
  765. (cfg->fc_gw_family &&
  766. cfg->fc_gw_family != nh->fib_nh_gw_family))
  767. return 1;
  768. if (cfg->fc_gw_family == AF_INET &&
  769. cfg->fc_gw4 != nh->fib_nh_gw4)
  770. return 1;
  771. if (cfg->fc_gw_family == AF_INET6 &&
  772. ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
  773. return 1;
  774. return 0;
  775. }
  776. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  777. if (!cfg->fc_mp)
  778. return 0;
  779. rtnh = cfg->fc_mp;
  780. remaining = cfg->fc_mp_len;
  781. for_nexthops(fi) {
  782. int attrlen;
  783. if (!rtnh_ok(rtnh, remaining))
  784. return -EINVAL;
  785. if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
  786. return 1;
  787. attrlen = rtnh_attrlen(rtnh);
  788. if (attrlen > 0) {
  789. struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
  790. int err;
  791. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  792. nlav = nla_find(attrs, attrlen, RTA_VIA);
  793. if (nla && nlav) {
  794. NL_SET_ERR_MSG(extack,
  795. "Nexthop configuration can not contain both GATEWAY and VIA");
  796. return -EINVAL;
  797. }
  798. if (nla) {
  799. __be32 gw;
  800. err = fib_gw_from_attr(&gw, nla, extack);
  801. if (err)
  802. return err;
  803. if (nh->fib_nh_gw_family != AF_INET ||
  804. gw != nh->fib_nh_gw4)
  805. return 1;
  806. } else if (nlav) {
  807. struct fib_config cfg2;
  808. err = fib_gw_from_via(&cfg2, nlav, extack);
  809. if (err)
  810. return err;
  811. switch (nh->fib_nh_gw_family) {
  812. case AF_INET:
  813. if (cfg2.fc_gw_family != AF_INET ||
  814. cfg2.fc_gw4 != nh->fib_nh_gw4)
  815. return 1;
  816. break;
  817. case AF_INET6:
  818. if (cfg2.fc_gw_family != AF_INET6 ||
  819. ipv6_addr_cmp(&cfg2.fc_gw6,
  820. &nh->fib_nh_gw6))
  821. return 1;
  822. break;
  823. }
  824. }
  825. #ifdef CONFIG_IP_ROUTE_CLASSID
  826. nla = nla_find(attrs, attrlen, RTA_FLOW);
  827. if (nla) {
  828. if (nla_len(nla) < sizeof(u32)) {
  829. NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
  830. return -EINVAL;
  831. }
  832. if (nla_get_u32(nla) != nh->nh_tclassid)
  833. return 1;
  834. }
  835. #endif
  836. }
  837. rtnh = rtnh_next(rtnh, &remaining);
  838. } endfor_nexthops(fi);
  839. #endif
  840. return 0;
  841. }
  842. bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
  843. {
  844. struct nlattr *nla;
  845. int remaining;
  846. if (!cfg->fc_mx)
  847. return true;
  848. nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
  849. int type = nla_type(nla);
  850. u32 fi_val, val;
  851. if (!type)
  852. continue;
  853. if (type > RTAX_MAX)
  854. return false;
  855. type = array_index_nospec(type, RTAX_MAX + 1);
  856. if (type == RTAX_CC_ALGO) {
  857. char tmp[TCP_CA_NAME_MAX];
  858. bool ecn_ca = false;
  859. nla_strscpy(tmp, nla, sizeof(tmp));
  860. val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
  861. } else {
  862. if (nla_len(nla) != sizeof(u32))
  863. return false;
  864. val = nla_get_u32(nla);
  865. }
  866. fi_val = fi->fib_metrics->metrics[type - 1];
  867. if (type == RTAX_FEATURES)
  868. fi_val &= ~DST_FEATURE_ECN_CA;
  869. if (fi_val != val)
  870. return false;
  871. }
  872. return true;
  873. }
  874. static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
  875. u32 table, struct netlink_ext_ack *extack)
  876. {
  877. struct fib6_config cfg = {
  878. .fc_table = table,
  879. .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
  880. .fc_ifindex = nh->fib_nh_oif,
  881. .fc_gateway = nh->fib_nh_gw6,
  882. };
  883. struct fib6_nh fib6_nh = {};
  884. int err;
  885. err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
  886. if (!err) {
  887. nh->fib_nh_dev = fib6_nh.fib_nh_dev;
  888. netdev_hold(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
  889. GFP_KERNEL);
  890. nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
  891. nh->fib_nh_scope = RT_SCOPE_LINK;
  892. ipv6_stub->fib6_nh_release(&fib6_nh);
  893. }
  894. return err;
  895. }
  896. /*
  897. * Picture
  898. * -------
  899. *
  900. * Semantics of nexthop is very messy by historical reasons.
  901. * We have to take into account, that:
  902. * a) gateway can be actually local interface address,
  903. * so that gatewayed route is direct.
  904. * b) gateway must be on-link address, possibly
  905. * described not by an ifaddr, but also by a direct route.
  906. * c) If both gateway and interface are specified, they should not
  907. * contradict.
  908. * d) If we use tunnel routes, gateway could be not on-link.
  909. *
  910. * Attempt to reconcile all of these (alas, self-contradictory) conditions
  911. * results in pretty ugly and hairy code with obscure logic.
  912. *
  913. * I chose to generalized it instead, so that the size
  914. * of code does not increase practically, but it becomes
  915. * much more general.
  916. * Every prefix is assigned a "scope" value: "host" is local address,
  917. * "link" is direct route,
  918. * [ ... "site" ... "interior" ... ]
  919. * and "universe" is true gateway route with global meaning.
  920. *
  921. * Every prefix refers to a set of "nexthop"s (gw, oif),
  922. * where gw must have narrower scope. This recursion stops
  923. * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
  924. * which means that gw is forced to be on link.
  925. *
  926. * Code is still hairy, but now it is apparently logically
  927. * consistent and very flexible. F.e. as by-product it allows
  928. * to co-exists in peace independent exterior and interior
  929. * routing processes.
  930. *
  931. * Normally it looks as following.
  932. *
  933. * {universe prefix} -> (gw, oif) [scope link]
  934. * |
  935. * |-> {link prefix} -> (gw, oif) [scope local]
  936. * |
  937. * |-> {local prefix} (terminal node)
  938. */
  939. static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
  940. u8 scope, struct netlink_ext_ack *extack)
  941. {
  942. struct net_device *dev;
  943. struct fib_result res;
  944. int err = 0;
  945. if (nh->fib_nh_flags & RTNH_F_ONLINK) {
  946. unsigned int addr_type;
  947. if (scope >= RT_SCOPE_LINK) {
  948. NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
  949. return -EINVAL;
  950. }
  951. dev = __dev_get_by_index(net, nh->fib_nh_oif);
  952. if (!dev) {
  953. NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
  954. return -ENODEV;
  955. }
  956. if (!(dev->flags & IFF_UP)) {
  957. NL_SET_ERR_MSG(extack, "Nexthop device is not up");
  958. return -ENETDOWN;
  959. }
  960. addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
  961. if (addr_type != RTN_UNICAST) {
  962. NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
  963. return -EINVAL;
  964. }
  965. if (!netif_carrier_ok(dev))
  966. nh->fib_nh_flags |= RTNH_F_LINKDOWN;
  967. nh->fib_nh_dev = dev;
  968. netdev_hold(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
  969. nh->fib_nh_scope = RT_SCOPE_LINK;
  970. return 0;
  971. }
  972. rcu_read_lock();
  973. {
  974. struct fib_table *tbl = NULL;
  975. struct flowi4 fl4 = {
  976. .daddr = nh->fib_nh_gw4,
  977. .flowi4_scope = scope + 1,
  978. .flowi4_oif = nh->fib_nh_oif,
  979. .flowi4_iif = LOOPBACK_IFINDEX,
  980. };
  981. /* It is not necessary, but requires a bit of thinking */
  982. if (fl4.flowi4_scope < RT_SCOPE_LINK)
  983. fl4.flowi4_scope = RT_SCOPE_LINK;
  984. if (table && table != RT_TABLE_MAIN)
  985. tbl = fib_get_table(net, table);
  986. if (tbl)
  987. err = fib_table_lookup(tbl, &fl4, &res,
  988. FIB_LOOKUP_IGNORE_LINKSTATE |
  989. FIB_LOOKUP_NOREF);
  990. /* on error or if no table given do full lookup. This
  991. * is needed for example when nexthops are in the local
  992. * table rather than the given table
  993. */
  994. if (!tbl || err) {
  995. err = fib_lookup(net, &fl4, &res,
  996. FIB_LOOKUP_IGNORE_LINKSTATE);
  997. }
  998. if (err) {
  999. NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
  1000. goto out;
  1001. }
  1002. }
  1003. err = -EINVAL;
  1004. if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
  1005. NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
  1006. goto out;
  1007. }
  1008. nh->fib_nh_scope = res.scope;
  1009. nh->fib_nh_oif = FIB_RES_OIF(res);
  1010. nh->fib_nh_dev = dev = FIB_RES_DEV(res);
  1011. if (!dev) {
  1012. NL_SET_ERR_MSG(extack,
  1013. "No egress device for nexthop gateway");
  1014. goto out;
  1015. }
  1016. netdev_hold(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
  1017. if (!netif_carrier_ok(dev))
  1018. nh->fib_nh_flags |= RTNH_F_LINKDOWN;
  1019. err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
  1020. out:
  1021. rcu_read_unlock();
  1022. return err;
  1023. }
  1024. static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
  1025. struct netlink_ext_ack *extack)
  1026. {
  1027. struct in_device *in_dev;
  1028. int err;
  1029. if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
  1030. NL_SET_ERR_MSG(extack,
  1031. "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
  1032. return -EINVAL;
  1033. }
  1034. rcu_read_lock();
  1035. err = -ENODEV;
  1036. in_dev = inetdev_by_index(net, nh->fib_nh_oif);
  1037. if (!in_dev)
  1038. goto out;
  1039. err = -ENETDOWN;
  1040. if (!(in_dev->dev->flags & IFF_UP)) {
  1041. NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
  1042. goto out;
  1043. }
  1044. nh->fib_nh_dev = in_dev->dev;
  1045. netdev_hold(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
  1046. nh->fib_nh_scope = RT_SCOPE_HOST;
  1047. if (!netif_carrier_ok(nh->fib_nh_dev))
  1048. nh->fib_nh_flags |= RTNH_F_LINKDOWN;
  1049. err = 0;
  1050. out:
  1051. rcu_read_unlock();
  1052. return err;
  1053. }
  1054. int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
  1055. struct netlink_ext_ack *extack)
  1056. {
  1057. int err;
  1058. if (nh->fib_nh_gw_family == AF_INET)
  1059. err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
  1060. else if (nh->fib_nh_gw_family == AF_INET6)
  1061. err = fib_check_nh_v6_gw(net, nh, table, extack);
  1062. else
  1063. err = fib_check_nh_nongw(net, nh, extack);
  1064. return err;
  1065. }
  1066. static struct hlist_head *
  1067. fib_info_laddrhash_bucket(const struct net *net, __be32 val)
  1068. {
  1069. u32 slot = hash_32(net_hash_mix(net) ^ (__force u32)val,
  1070. fib_info_hash_bits);
  1071. return &fib_info_laddrhash[slot];
  1072. }
  1073. static void fib_info_hash_move(struct hlist_head *new_info_hash,
  1074. struct hlist_head *new_laddrhash,
  1075. unsigned int new_size)
  1076. {
  1077. struct hlist_head *old_info_hash, *old_laddrhash;
  1078. unsigned int old_size = fib_info_hash_size;
  1079. unsigned int i;
  1080. spin_lock_bh(&fib_info_lock);
  1081. old_info_hash = fib_info_hash;
  1082. old_laddrhash = fib_info_laddrhash;
  1083. fib_info_hash_size = new_size;
  1084. fib_info_hash_bits = ilog2(new_size);
  1085. for (i = 0; i < old_size; i++) {
  1086. struct hlist_head *head = &fib_info_hash[i];
  1087. struct hlist_node *n;
  1088. struct fib_info *fi;
  1089. hlist_for_each_entry_safe(fi, n, head, fib_hash) {
  1090. struct hlist_head *dest;
  1091. unsigned int new_hash;
  1092. new_hash = fib_info_hashfn(fi);
  1093. dest = &new_info_hash[new_hash];
  1094. hlist_add_head(&fi->fib_hash, dest);
  1095. }
  1096. }
  1097. fib_info_hash = new_info_hash;
  1098. fib_info_laddrhash = new_laddrhash;
  1099. for (i = 0; i < old_size; i++) {
  1100. struct hlist_head *lhead = &old_laddrhash[i];
  1101. struct hlist_node *n;
  1102. struct fib_info *fi;
  1103. hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
  1104. struct hlist_head *ldest;
  1105. ldest = fib_info_laddrhash_bucket(fi->fib_net,
  1106. fi->fib_prefsrc);
  1107. hlist_add_head(&fi->fib_lhash, ldest);
  1108. }
  1109. }
  1110. spin_unlock_bh(&fib_info_lock);
  1111. kvfree(old_info_hash);
  1112. kvfree(old_laddrhash);
  1113. }
  1114. __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
  1115. unsigned char scope)
  1116. {
  1117. struct fib_nh *nh;
  1118. __be32 saddr;
  1119. if (nhc->nhc_family != AF_INET)
  1120. return inet_select_addr(nhc->nhc_dev, 0, scope);
  1121. nh = container_of(nhc, struct fib_nh, nh_common);
  1122. saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
  1123. WRITE_ONCE(nh->nh_saddr, saddr);
  1124. WRITE_ONCE(nh->nh_saddr_genid, atomic_read(&net->ipv4.dev_addr_genid));
  1125. return saddr;
  1126. }
  1127. __be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
  1128. {
  1129. struct fib_nh_common *nhc = res->nhc;
  1130. if (res->fi->fib_prefsrc)
  1131. return res->fi->fib_prefsrc;
  1132. if (nhc->nhc_family == AF_INET) {
  1133. struct fib_nh *nh;
  1134. nh = container_of(nhc, struct fib_nh, nh_common);
  1135. if (READ_ONCE(nh->nh_saddr_genid) ==
  1136. atomic_read(&net->ipv4.dev_addr_genid))
  1137. return READ_ONCE(nh->nh_saddr);
  1138. }
  1139. return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
  1140. }
  1141. static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
  1142. {
  1143. if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
  1144. fib_prefsrc != cfg->fc_dst) {
  1145. u32 tb_id = cfg->fc_table;
  1146. int rc;
  1147. if (tb_id == RT_TABLE_MAIN)
  1148. tb_id = RT_TABLE_LOCAL;
  1149. rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
  1150. fib_prefsrc, tb_id);
  1151. if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
  1152. rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
  1153. fib_prefsrc, RT_TABLE_LOCAL);
  1154. }
  1155. if (rc != RTN_LOCAL)
  1156. return false;
  1157. }
  1158. return true;
  1159. }
  1160. struct fib_info *fib_create_info(struct fib_config *cfg,
  1161. struct netlink_ext_ack *extack)
  1162. {
  1163. int err;
  1164. struct fib_info *fi = NULL;
  1165. struct nexthop *nh = NULL;
  1166. struct fib_info *ofi;
  1167. int nhs = 1;
  1168. struct net *net = cfg->fc_nlinfo.nl_net;
  1169. if (cfg->fc_type > RTN_MAX)
  1170. goto err_inval;
  1171. /* Fast check to catch the most weird cases */
  1172. if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
  1173. NL_SET_ERR_MSG(extack, "Invalid scope");
  1174. goto err_inval;
  1175. }
  1176. if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
  1177. NL_SET_ERR_MSG(extack,
  1178. "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
  1179. goto err_inval;
  1180. }
  1181. if (cfg->fc_nh_id) {
  1182. if (!cfg->fc_mx) {
  1183. fi = fib_find_info_nh(net, cfg);
  1184. if (fi) {
  1185. refcount_inc(&fi->fib_treeref);
  1186. return fi;
  1187. }
  1188. }
  1189. nh = nexthop_find_by_id(net, cfg->fc_nh_id);
  1190. if (!nh) {
  1191. NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
  1192. goto err_inval;
  1193. }
  1194. nhs = 0;
  1195. }
  1196. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1197. if (cfg->fc_mp) {
  1198. nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
  1199. if (nhs == 0)
  1200. goto err_inval;
  1201. }
  1202. #endif
  1203. err = -ENOBUFS;
  1204. /* Paired with WRITE_ONCE() in fib_release_info() */
  1205. if (READ_ONCE(fib_info_cnt) >= fib_info_hash_size) {
  1206. unsigned int new_size = fib_info_hash_size << 1;
  1207. struct hlist_head *new_info_hash;
  1208. struct hlist_head *new_laddrhash;
  1209. size_t bytes;
  1210. if (!new_size)
  1211. new_size = 16;
  1212. bytes = (size_t)new_size * sizeof(struct hlist_head *);
  1213. new_info_hash = kvzalloc(bytes, GFP_KERNEL);
  1214. new_laddrhash = kvzalloc(bytes, GFP_KERNEL);
  1215. if (!new_info_hash || !new_laddrhash) {
  1216. kvfree(new_info_hash);
  1217. kvfree(new_laddrhash);
  1218. } else {
  1219. fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
  1220. }
  1221. if (!fib_info_hash_size)
  1222. goto failure;
  1223. }
  1224. fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
  1225. if (!fi)
  1226. goto failure;
  1227. fi->fib_metrics = ip_fib_metrics_init(cfg->fc_mx, cfg->fc_mx_len, extack);
  1228. if (IS_ERR(fi->fib_metrics)) {
  1229. err = PTR_ERR(fi->fib_metrics);
  1230. kfree(fi);
  1231. return ERR_PTR(err);
  1232. }
  1233. fi->fib_net = net;
  1234. fi->fib_protocol = cfg->fc_protocol;
  1235. fi->fib_scope = cfg->fc_scope;
  1236. fi->fib_flags = cfg->fc_flags;
  1237. fi->fib_priority = cfg->fc_priority;
  1238. fi->fib_prefsrc = cfg->fc_prefsrc;
  1239. fi->fib_type = cfg->fc_type;
  1240. fi->fib_tb_id = cfg->fc_table;
  1241. fi->fib_nhs = nhs;
  1242. if (nh) {
  1243. if (!nexthop_get(nh)) {
  1244. NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
  1245. err = -EINVAL;
  1246. } else {
  1247. err = 0;
  1248. fi->nh = nh;
  1249. }
  1250. } else {
  1251. change_nexthops(fi) {
  1252. nexthop_nh->nh_parent = fi;
  1253. } endfor_nexthops(fi)
  1254. if (cfg->fc_mp)
  1255. err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
  1256. extack);
  1257. else
  1258. err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
  1259. }
  1260. if (err != 0)
  1261. goto failure;
  1262. if (fib_props[cfg->fc_type].error) {
  1263. if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
  1264. NL_SET_ERR_MSG(extack,
  1265. "Gateway, device and multipath can not be specified for this route type");
  1266. goto err_inval;
  1267. }
  1268. goto link_it;
  1269. } else {
  1270. switch (cfg->fc_type) {
  1271. case RTN_UNICAST:
  1272. case RTN_LOCAL:
  1273. case RTN_BROADCAST:
  1274. case RTN_ANYCAST:
  1275. case RTN_MULTICAST:
  1276. break;
  1277. default:
  1278. NL_SET_ERR_MSG(extack, "Invalid route type");
  1279. goto err_inval;
  1280. }
  1281. }
  1282. if (cfg->fc_scope > RT_SCOPE_HOST) {
  1283. NL_SET_ERR_MSG(extack, "Invalid scope");
  1284. goto err_inval;
  1285. }
  1286. if (fi->nh) {
  1287. err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
  1288. if (err)
  1289. goto failure;
  1290. } else if (cfg->fc_scope == RT_SCOPE_HOST) {
  1291. struct fib_nh *nh = fi->fib_nh;
  1292. /* Local address is added. */
  1293. if (nhs != 1) {
  1294. NL_SET_ERR_MSG(extack,
  1295. "Route with host scope can not have multiple nexthops");
  1296. goto err_inval;
  1297. }
  1298. if (nh->fib_nh_gw_family) {
  1299. NL_SET_ERR_MSG(extack,
  1300. "Route with host scope can not have a gateway");
  1301. goto err_inval;
  1302. }
  1303. nh->fib_nh_scope = RT_SCOPE_NOWHERE;
  1304. nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
  1305. err = -ENODEV;
  1306. if (!nh->fib_nh_dev)
  1307. goto failure;
  1308. netdev_tracker_alloc(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
  1309. GFP_KERNEL);
  1310. } else {
  1311. int linkdown = 0;
  1312. change_nexthops(fi) {
  1313. err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
  1314. cfg->fc_table, cfg->fc_scope,
  1315. extack);
  1316. if (err != 0)
  1317. goto failure;
  1318. if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
  1319. linkdown++;
  1320. } endfor_nexthops(fi)
  1321. if (linkdown == fi->fib_nhs)
  1322. fi->fib_flags |= RTNH_F_LINKDOWN;
  1323. }
  1324. if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
  1325. NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
  1326. goto err_inval;
  1327. }
  1328. if (!fi->nh) {
  1329. change_nexthops(fi) {
  1330. fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
  1331. fi->fib_scope);
  1332. if (nexthop_nh->fib_nh_gw_family == AF_INET6)
  1333. fi->fib_nh_is_v6 = true;
  1334. } endfor_nexthops(fi)
  1335. fib_rebalance(fi);
  1336. }
  1337. link_it:
  1338. ofi = fib_find_info(fi);
  1339. if (ofi) {
  1340. /* fib_table_lookup() should not see @fi yet. */
  1341. fi->fib_dead = 1;
  1342. free_fib_info(fi);
  1343. refcount_inc(&ofi->fib_treeref);
  1344. return ofi;
  1345. }
  1346. refcount_set(&fi->fib_treeref, 1);
  1347. refcount_set(&fi->fib_clntref, 1);
  1348. spin_lock_bh(&fib_info_lock);
  1349. fib_info_cnt++;
  1350. hlist_add_head(&fi->fib_hash,
  1351. &fib_info_hash[fib_info_hashfn(fi)]);
  1352. if (fi->fib_prefsrc) {
  1353. struct hlist_head *head;
  1354. head = fib_info_laddrhash_bucket(net, fi->fib_prefsrc);
  1355. hlist_add_head(&fi->fib_lhash, head);
  1356. }
  1357. if (fi->nh) {
  1358. list_add(&fi->nh_list, &nh->fi_list);
  1359. } else {
  1360. change_nexthops(fi) {
  1361. struct hlist_head *head;
  1362. if (!nexthop_nh->fib_nh_dev)
  1363. continue;
  1364. head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev);
  1365. hlist_add_head(&nexthop_nh->nh_hash, head);
  1366. } endfor_nexthops(fi)
  1367. }
  1368. spin_unlock_bh(&fib_info_lock);
  1369. return fi;
  1370. err_inval:
  1371. err = -EINVAL;
  1372. failure:
  1373. if (fi) {
  1374. /* fib_table_lookup() should not see @fi yet. */
  1375. fi->fib_dead = 1;
  1376. free_fib_info(fi);
  1377. }
  1378. return ERR_PTR(err);
  1379. }
  1380. int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
  1381. u8 rt_family, unsigned char *flags, bool skip_oif)
  1382. {
  1383. if (nhc->nhc_flags & RTNH_F_DEAD)
  1384. *flags |= RTNH_F_DEAD;
  1385. if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
  1386. *flags |= RTNH_F_LINKDOWN;
  1387. rcu_read_lock();
  1388. switch (nhc->nhc_family) {
  1389. case AF_INET:
  1390. if (ip_ignore_linkdown(nhc->nhc_dev))
  1391. *flags |= RTNH_F_DEAD;
  1392. break;
  1393. case AF_INET6:
  1394. if (ip6_ignore_linkdown(nhc->nhc_dev))
  1395. *flags |= RTNH_F_DEAD;
  1396. break;
  1397. }
  1398. rcu_read_unlock();
  1399. }
  1400. switch (nhc->nhc_gw_family) {
  1401. case AF_INET:
  1402. if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
  1403. goto nla_put_failure;
  1404. break;
  1405. case AF_INET6:
  1406. /* if gateway family does not match nexthop family
  1407. * gateway is encoded as RTA_VIA
  1408. */
  1409. if (rt_family != nhc->nhc_gw_family) {
  1410. int alen = sizeof(struct in6_addr);
  1411. struct nlattr *nla;
  1412. struct rtvia *via;
  1413. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  1414. if (!nla)
  1415. goto nla_put_failure;
  1416. via = nla_data(nla);
  1417. via->rtvia_family = AF_INET6;
  1418. memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
  1419. } else if (nla_put_in6_addr(skb, RTA_GATEWAY,
  1420. &nhc->nhc_gw.ipv6) < 0) {
  1421. goto nla_put_failure;
  1422. }
  1423. break;
  1424. }
  1425. *flags |= (nhc->nhc_flags &
  1426. (RTNH_F_ONLINK | RTNH_F_OFFLOAD | RTNH_F_TRAP));
  1427. if (!skip_oif && nhc->nhc_dev &&
  1428. nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
  1429. goto nla_put_failure;
  1430. if (nhc->nhc_lwtstate &&
  1431. lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
  1432. RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
  1433. goto nla_put_failure;
  1434. return 0;
  1435. nla_put_failure:
  1436. return -EMSGSIZE;
  1437. }
  1438. EXPORT_SYMBOL_GPL(fib_nexthop_info);
  1439. #if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
  1440. int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
  1441. int nh_weight, u8 rt_family, u32 nh_tclassid)
  1442. {
  1443. const struct net_device *dev = nhc->nhc_dev;
  1444. struct rtnexthop *rtnh;
  1445. unsigned char flags = 0;
  1446. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1447. if (!rtnh)
  1448. goto nla_put_failure;
  1449. rtnh->rtnh_hops = nh_weight - 1;
  1450. rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
  1451. if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
  1452. goto nla_put_failure;
  1453. rtnh->rtnh_flags = flags;
  1454. if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
  1455. goto nla_put_failure;
  1456. /* length of rtnetlink header + attributes */
  1457. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1458. return 0;
  1459. nla_put_failure:
  1460. return -EMSGSIZE;
  1461. }
  1462. EXPORT_SYMBOL_GPL(fib_add_nexthop);
  1463. #endif
  1464. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1465. static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
  1466. {
  1467. struct nlattr *mp;
  1468. mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
  1469. if (!mp)
  1470. goto nla_put_failure;
  1471. if (unlikely(fi->nh)) {
  1472. if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
  1473. goto nla_put_failure;
  1474. goto mp_end;
  1475. }
  1476. for_nexthops(fi) {
  1477. u32 nh_tclassid = 0;
  1478. #ifdef CONFIG_IP_ROUTE_CLASSID
  1479. nh_tclassid = nh->nh_tclassid;
  1480. #endif
  1481. if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
  1482. AF_INET, nh_tclassid) < 0)
  1483. goto nla_put_failure;
  1484. } endfor_nexthops(fi);
  1485. mp_end:
  1486. nla_nest_end(skb, mp);
  1487. return 0;
  1488. nla_put_failure:
  1489. return -EMSGSIZE;
  1490. }
  1491. #else
  1492. static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
  1493. {
  1494. return 0;
  1495. }
  1496. #endif
  1497. int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1498. const struct fib_rt_info *fri, unsigned int flags)
  1499. {
  1500. unsigned int nhs = fib_info_num_path(fri->fi);
  1501. struct fib_info *fi = fri->fi;
  1502. u32 tb_id = fri->tb_id;
  1503. struct nlmsghdr *nlh;
  1504. struct rtmsg *rtm;
  1505. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1506. if (!nlh)
  1507. return -EMSGSIZE;
  1508. rtm = nlmsg_data(nlh);
  1509. rtm->rtm_family = AF_INET;
  1510. rtm->rtm_dst_len = fri->dst_len;
  1511. rtm->rtm_src_len = 0;
  1512. rtm->rtm_tos = inet_dscp_to_dsfield(fri->dscp);
  1513. if (tb_id < 256)
  1514. rtm->rtm_table = tb_id;
  1515. else
  1516. rtm->rtm_table = RT_TABLE_COMPAT;
  1517. if (nla_put_u32(skb, RTA_TABLE, tb_id))
  1518. goto nla_put_failure;
  1519. rtm->rtm_type = fri->type;
  1520. rtm->rtm_flags = fi->fib_flags;
  1521. rtm->rtm_scope = fi->fib_scope;
  1522. rtm->rtm_protocol = fi->fib_protocol;
  1523. if (rtm->rtm_dst_len &&
  1524. nla_put_in_addr(skb, RTA_DST, fri->dst))
  1525. goto nla_put_failure;
  1526. if (fi->fib_priority &&
  1527. nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
  1528. goto nla_put_failure;
  1529. if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
  1530. goto nla_put_failure;
  1531. if (fi->fib_prefsrc &&
  1532. nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
  1533. goto nla_put_failure;
  1534. if (fi->nh) {
  1535. if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
  1536. goto nla_put_failure;
  1537. if (nexthop_is_blackhole(fi->nh))
  1538. rtm->rtm_type = RTN_BLACKHOLE;
  1539. if (!READ_ONCE(fi->fib_net->ipv4.sysctl_nexthop_compat_mode))
  1540. goto offload;
  1541. }
  1542. if (nhs == 1) {
  1543. const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
  1544. unsigned char flags = 0;
  1545. if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
  1546. goto nla_put_failure;
  1547. rtm->rtm_flags = flags;
  1548. #ifdef CONFIG_IP_ROUTE_CLASSID
  1549. if (nhc->nhc_family == AF_INET) {
  1550. struct fib_nh *nh;
  1551. nh = container_of(nhc, struct fib_nh, nh_common);
  1552. if (nh->nh_tclassid &&
  1553. nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
  1554. goto nla_put_failure;
  1555. }
  1556. #endif
  1557. } else {
  1558. if (fib_add_multipath(skb, fi) < 0)
  1559. goto nla_put_failure;
  1560. }
  1561. offload:
  1562. if (fri->offload)
  1563. rtm->rtm_flags |= RTM_F_OFFLOAD;
  1564. if (fri->trap)
  1565. rtm->rtm_flags |= RTM_F_TRAP;
  1566. if (fri->offload_failed)
  1567. rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
  1568. nlmsg_end(skb, nlh);
  1569. return 0;
  1570. nla_put_failure:
  1571. nlmsg_cancel(skb, nlh);
  1572. return -EMSGSIZE;
  1573. }
  1574. /*
  1575. * Update FIB if:
  1576. * - local address disappeared -> we must delete all the entries
  1577. * referring to it.
  1578. * - device went down -> we must shutdown all nexthops going via it.
  1579. */
  1580. int fib_sync_down_addr(struct net_device *dev, __be32 local)
  1581. {
  1582. int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
  1583. struct net *net = dev_net(dev);
  1584. struct hlist_head *head;
  1585. struct fib_info *fi;
  1586. int ret = 0;
  1587. if (!fib_info_laddrhash || local == 0)
  1588. return 0;
  1589. head = fib_info_laddrhash_bucket(net, local);
  1590. hlist_for_each_entry(fi, head, fib_lhash) {
  1591. if (!net_eq(fi->fib_net, net) ||
  1592. fi->fib_tb_id != tb_id)
  1593. continue;
  1594. if (fi->fib_prefsrc == local) {
  1595. fi->fib_flags |= RTNH_F_DEAD;
  1596. fi->pfsrc_removed = true;
  1597. ret++;
  1598. }
  1599. }
  1600. return ret;
  1601. }
  1602. static int call_fib_nh_notifiers(struct fib_nh *nh,
  1603. enum fib_event_type event_type)
  1604. {
  1605. bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
  1606. struct fib_nh_notifier_info info = {
  1607. .fib_nh = nh,
  1608. };
  1609. switch (event_type) {
  1610. case FIB_EVENT_NH_ADD:
  1611. if (nh->fib_nh_flags & RTNH_F_DEAD)
  1612. break;
  1613. if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
  1614. break;
  1615. return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
  1616. &info.info);
  1617. case FIB_EVENT_NH_DEL:
  1618. if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
  1619. (nh->fib_nh_flags & RTNH_F_DEAD))
  1620. return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
  1621. event_type, &info.info);
  1622. break;
  1623. default:
  1624. break;
  1625. }
  1626. return NOTIFY_DONE;
  1627. }
  1628. /* Update the PMTU of exceptions when:
  1629. * - the new MTU of the first hop becomes smaller than the PMTU
  1630. * - the old MTU was the same as the PMTU, and it limited discovery of
  1631. * larger MTUs on the path. With that limit raised, we can now
  1632. * discover larger MTUs
  1633. * A special case is locked exceptions, for which the PMTU is smaller
  1634. * than the minimal accepted PMTU:
  1635. * - if the new MTU is greater than the PMTU, don't make any change
  1636. * - otherwise, unlock and set PMTU
  1637. */
  1638. void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
  1639. {
  1640. struct fnhe_hash_bucket *bucket;
  1641. int i;
  1642. bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
  1643. if (!bucket)
  1644. return;
  1645. for (i = 0; i < FNHE_HASH_SIZE; i++) {
  1646. struct fib_nh_exception *fnhe;
  1647. for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
  1648. fnhe;
  1649. fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
  1650. if (fnhe->fnhe_mtu_locked) {
  1651. if (new <= fnhe->fnhe_pmtu) {
  1652. fnhe->fnhe_pmtu = new;
  1653. fnhe->fnhe_mtu_locked = false;
  1654. }
  1655. } else if (new < fnhe->fnhe_pmtu ||
  1656. orig == fnhe->fnhe_pmtu) {
  1657. fnhe->fnhe_pmtu = new;
  1658. }
  1659. }
  1660. }
  1661. }
  1662. void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
  1663. {
  1664. struct hlist_head *head = fib_info_devhash_bucket(dev);
  1665. struct fib_nh *nh;
  1666. hlist_for_each_entry(nh, head, nh_hash) {
  1667. if (nh->fib_nh_dev == dev)
  1668. fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
  1669. }
  1670. }
  1671. /* Event force Flags Description
  1672. * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
  1673. * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
  1674. * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
  1675. * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
  1676. *
  1677. * only used when fib_nh is built into fib_info
  1678. */
  1679. int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
  1680. {
  1681. struct hlist_head *head = fib_info_devhash_bucket(dev);
  1682. struct fib_info *prev_fi = NULL;
  1683. int scope = RT_SCOPE_NOWHERE;
  1684. struct fib_nh *nh;
  1685. int ret = 0;
  1686. if (force)
  1687. scope = -1;
  1688. hlist_for_each_entry(nh, head, nh_hash) {
  1689. struct fib_info *fi = nh->nh_parent;
  1690. int dead;
  1691. BUG_ON(!fi->fib_nhs);
  1692. if (nh->fib_nh_dev != dev || fi == prev_fi)
  1693. continue;
  1694. prev_fi = fi;
  1695. dead = 0;
  1696. change_nexthops(fi) {
  1697. if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
  1698. dead++;
  1699. else if (nexthop_nh->fib_nh_dev == dev &&
  1700. nexthop_nh->fib_nh_scope != scope) {
  1701. switch (event) {
  1702. case NETDEV_DOWN:
  1703. case NETDEV_UNREGISTER:
  1704. nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
  1705. fallthrough;
  1706. case NETDEV_CHANGE:
  1707. nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
  1708. break;
  1709. }
  1710. call_fib_nh_notifiers(nexthop_nh,
  1711. FIB_EVENT_NH_DEL);
  1712. dead++;
  1713. }
  1714. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1715. if (event == NETDEV_UNREGISTER &&
  1716. nexthop_nh->fib_nh_dev == dev) {
  1717. dead = fi->fib_nhs;
  1718. break;
  1719. }
  1720. #endif
  1721. } endfor_nexthops(fi)
  1722. if (dead == fi->fib_nhs) {
  1723. switch (event) {
  1724. case NETDEV_DOWN:
  1725. case NETDEV_UNREGISTER:
  1726. fi->fib_flags |= RTNH_F_DEAD;
  1727. fallthrough;
  1728. case NETDEV_CHANGE:
  1729. fi->fib_flags |= RTNH_F_LINKDOWN;
  1730. break;
  1731. }
  1732. ret++;
  1733. }
  1734. fib_rebalance(fi);
  1735. }
  1736. return ret;
  1737. }
  1738. /* Must be invoked inside of an RCU protected region. */
  1739. static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
  1740. {
  1741. struct fib_info *fi = NULL, *last_resort = NULL;
  1742. struct hlist_head *fa_head = res->fa_head;
  1743. struct fib_table *tb = res->table;
  1744. u8 slen = 32 - res->prefixlen;
  1745. int order = -1, last_idx = -1;
  1746. struct fib_alias *fa, *fa1 = NULL;
  1747. u32 last_prio = res->fi->fib_priority;
  1748. dscp_t last_dscp = 0;
  1749. hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
  1750. struct fib_info *next_fi = fa->fa_info;
  1751. struct fib_nh_common *nhc;
  1752. if (fa->fa_slen != slen)
  1753. continue;
  1754. if (fa->fa_dscp && !fib_dscp_masked_match(fa->fa_dscp, flp))
  1755. continue;
  1756. if (fa->tb_id != tb->tb_id)
  1757. continue;
  1758. if (next_fi->fib_priority > last_prio &&
  1759. fa->fa_dscp == last_dscp) {
  1760. if (last_dscp)
  1761. continue;
  1762. break;
  1763. }
  1764. if (next_fi->fib_flags & RTNH_F_DEAD)
  1765. continue;
  1766. last_dscp = fa->fa_dscp;
  1767. last_prio = next_fi->fib_priority;
  1768. if (next_fi->fib_scope != res->scope ||
  1769. fa->fa_type != RTN_UNICAST)
  1770. continue;
  1771. nhc = fib_info_nhc(next_fi, 0);
  1772. if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
  1773. continue;
  1774. fib_alias_accessed(fa);
  1775. if (!fi) {
  1776. if (next_fi != res->fi)
  1777. break;
  1778. fa1 = fa;
  1779. } else if (!fib_detect_death(fi, order, &last_resort,
  1780. &last_idx, fa1->fa_default)) {
  1781. fib_result_assign(res, fi);
  1782. fa1->fa_default = order;
  1783. goto out;
  1784. }
  1785. fi = next_fi;
  1786. order++;
  1787. }
  1788. if (order <= 0 || !fi) {
  1789. if (fa1)
  1790. fa1->fa_default = -1;
  1791. goto out;
  1792. }
  1793. if (!fib_detect_death(fi, order, &last_resort, &last_idx,
  1794. fa1->fa_default)) {
  1795. fib_result_assign(res, fi);
  1796. fa1->fa_default = order;
  1797. goto out;
  1798. }
  1799. if (last_idx >= 0)
  1800. fib_result_assign(res, last_resort);
  1801. fa1->fa_default = last_idx;
  1802. out:
  1803. return;
  1804. }
  1805. /*
  1806. * Dead device goes up. We wake up dead nexthops.
  1807. * It takes sense only on multipath routes.
  1808. *
  1809. * only used when fib_nh is built into fib_info
  1810. */
  1811. int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
  1812. {
  1813. struct fib_info *prev_fi;
  1814. struct hlist_head *head;
  1815. struct fib_nh *nh;
  1816. int ret;
  1817. if (!(dev->flags & IFF_UP))
  1818. return 0;
  1819. if (nh_flags & RTNH_F_DEAD) {
  1820. unsigned int flags = dev_get_flags(dev);
  1821. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1822. nh_flags |= RTNH_F_LINKDOWN;
  1823. }
  1824. prev_fi = NULL;
  1825. head = fib_info_devhash_bucket(dev);
  1826. ret = 0;
  1827. hlist_for_each_entry(nh, head, nh_hash) {
  1828. struct fib_info *fi = nh->nh_parent;
  1829. int alive;
  1830. BUG_ON(!fi->fib_nhs);
  1831. if (nh->fib_nh_dev != dev || fi == prev_fi)
  1832. continue;
  1833. prev_fi = fi;
  1834. alive = 0;
  1835. change_nexthops(fi) {
  1836. if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
  1837. alive++;
  1838. continue;
  1839. }
  1840. if (!nexthop_nh->fib_nh_dev ||
  1841. !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
  1842. continue;
  1843. if (nexthop_nh->fib_nh_dev != dev ||
  1844. !__in_dev_get_rtnl(dev))
  1845. continue;
  1846. alive++;
  1847. nexthop_nh->fib_nh_flags &= ~nh_flags;
  1848. call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
  1849. } endfor_nexthops(fi)
  1850. if (alive > 0) {
  1851. fi->fib_flags &= ~nh_flags;
  1852. ret++;
  1853. }
  1854. fib_rebalance(fi);
  1855. }
  1856. return ret;
  1857. }
  1858. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1859. static bool fib_good_nh(const struct fib_nh *nh)
  1860. {
  1861. int state = NUD_REACHABLE;
  1862. if (nh->fib_nh_scope == RT_SCOPE_LINK) {
  1863. struct neighbour *n;
  1864. rcu_read_lock();
  1865. if (likely(nh->fib_nh_gw_family == AF_INET))
  1866. n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
  1867. (__force u32)nh->fib_nh_gw4);
  1868. else if (nh->fib_nh_gw_family == AF_INET6)
  1869. n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
  1870. &nh->fib_nh_gw6);
  1871. else
  1872. n = NULL;
  1873. if (n)
  1874. state = READ_ONCE(n->nud_state);
  1875. rcu_read_unlock();
  1876. }
  1877. return !!(state & NUD_VALID);
  1878. }
  1879. void fib_select_multipath(struct fib_result *res, int hash)
  1880. {
  1881. struct fib_info *fi = res->fi;
  1882. struct net *net = fi->fib_net;
  1883. bool first = false;
  1884. if (unlikely(res->fi->nh)) {
  1885. nexthop_path_fib_result(res, hash);
  1886. return;
  1887. }
  1888. change_nexthops(fi) {
  1889. if (READ_ONCE(net->ipv4.sysctl_fib_multipath_use_neigh)) {
  1890. if (!fib_good_nh(nexthop_nh))
  1891. continue;
  1892. if (!first) {
  1893. res->nh_sel = nhsel;
  1894. res->nhc = &nexthop_nh->nh_common;
  1895. first = true;
  1896. }
  1897. }
  1898. if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
  1899. continue;
  1900. res->nh_sel = nhsel;
  1901. res->nhc = &nexthop_nh->nh_common;
  1902. return;
  1903. } endfor_nexthops(fi);
  1904. }
  1905. #endif
  1906. void fib_select_path(struct net *net, struct fib_result *res,
  1907. struct flowi4 *fl4, const struct sk_buff *skb)
  1908. {
  1909. if (fl4->flowi4_oif)
  1910. goto check_saddr;
  1911. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1912. if (fib_info_num_path(res->fi) > 1) {
  1913. int h = fib_multipath_hash(net, fl4, skb, NULL);
  1914. fib_select_multipath(res, h);
  1915. }
  1916. else
  1917. #endif
  1918. if (!res->prefixlen &&
  1919. res->table->tb_num_default > 1 &&
  1920. res->type == RTN_UNICAST)
  1921. fib_select_default(fl4, res);
  1922. check_saddr:
  1923. if (!fl4->saddr) {
  1924. struct net_device *l3mdev;
  1925. l3mdev = dev_get_by_index_rcu(net, fl4->flowi4_l3mdev);
  1926. if (!l3mdev ||
  1927. l3mdev_master_dev_rcu(FIB_RES_DEV(*res)) == l3mdev)
  1928. fl4->saddr = fib_result_prefsrc(net, res);
  1929. else
  1930. fl4->saddr = inet_select_addr(l3mdev, 0, RT_SCOPE_LINK);
  1931. }
  1932. }