fib_semantics.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274
  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, <[email protected]>
  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(&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. if (err < 0)
  458. rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
  459. }
  460. static int fib_detect_death(struct fib_info *fi, int order,
  461. struct fib_info **last_resort, int *last_idx,
  462. int dflt)
  463. {
  464. const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
  465. struct neighbour *n;
  466. int state = NUD_NONE;
  467. if (likely(nhc->nhc_gw_family == AF_INET))
  468. n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
  469. else if (nhc->nhc_gw_family == AF_INET6)
  470. n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
  471. nhc->nhc_dev);
  472. else
  473. n = NULL;
  474. if (n) {
  475. state = READ_ONCE(n->nud_state);
  476. neigh_release(n);
  477. } else {
  478. return 0;
  479. }
  480. if (state == NUD_REACHABLE)
  481. return 0;
  482. if ((state & NUD_VALID) && order != dflt)
  483. return 0;
  484. if ((state & NUD_VALID) ||
  485. (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
  486. *last_resort = fi;
  487. *last_idx = order;
  488. }
  489. return 1;
  490. }
  491. int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
  492. struct nlattr *encap, u16 encap_type,
  493. void *cfg, gfp_t gfp_flags,
  494. struct netlink_ext_ack *extack)
  495. {
  496. int err;
  497. nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
  498. gfp_flags);
  499. if (!nhc->nhc_pcpu_rth_output)
  500. return -ENOMEM;
  501. if (encap) {
  502. struct lwtunnel_state *lwtstate;
  503. if (encap_type == LWTUNNEL_ENCAP_NONE) {
  504. NL_SET_ERR_MSG(extack, "LWT encap type not specified");
  505. err = -EINVAL;
  506. goto lwt_failure;
  507. }
  508. err = lwtunnel_build_state(net, encap_type, encap,
  509. nhc->nhc_family, cfg, &lwtstate,
  510. extack);
  511. if (err)
  512. goto lwt_failure;
  513. nhc->nhc_lwtstate = lwtstate_get(lwtstate);
  514. }
  515. return 0;
  516. lwt_failure:
  517. rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
  518. nhc->nhc_pcpu_rth_output = NULL;
  519. return err;
  520. }
  521. EXPORT_SYMBOL_GPL(fib_nh_common_init);
  522. int fib_nh_init(struct net *net, struct fib_nh *nh,
  523. struct fib_config *cfg, int nh_weight,
  524. struct netlink_ext_ack *extack)
  525. {
  526. int err;
  527. nh->fib_nh_family = AF_INET;
  528. err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap,
  529. cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
  530. if (err)
  531. return err;
  532. nh->fib_nh_oif = cfg->fc_oif;
  533. nh->fib_nh_gw_family = cfg->fc_gw_family;
  534. if (cfg->fc_gw_family == AF_INET)
  535. nh->fib_nh_gw4 = cfg->fc_gw4;
  536. else if (cfg->fc_gw_family == AF_INET6)
  537. nh->fib_nh_gw6 = cfg->fc_gw6;
  538. nh->fib_nh_flags = cfg->fc_flags;
  539. #ifdef CONFIG_IP_ROUTE_CLASSID
  540. nh->nh_tclassid = cfg->fc_flow;
  541. if (nh->nh_tclassid)
  542. atomic_inc(&net->ipv4.fib_num_tclassid_users);
  543. #endif
  544. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  545. nh->fib_nh_weight = nh_weight;
  546. #endif
  547. return 0;
  548. }
  549. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  550. static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
  551. struct netlink_ext_ack *extack)
  552. {
  553. int nhs = 0;
  554. while (rtnh_ok(rtnh, remaining)) {
  555. nhs++;
  556. rtnh = rtnh_next(rtnh, &remaining);
  557. }
  558. /* leftover implies invalid nexthop configuration, discard it */
  559. if (remaining > 0) {
  560. NL_SET_ERR_MSG(extack,
  561. "Invalid nexthop configuration - extra data after nexthops");
  562. nhs = 0;
  563. }
  564. return nhs;
  565. }
  566. static int fib_gw_from_attr(__be32 *gw, struct nlattr *nla,
  567. struct netlink_ext_ack *extack)
  568. {
  569. if (nla_len(nla) < sizeof(*gw)) {
  570. NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
  571. return -EINVAL;
  572. }
  573. *gw = nla_get_in_addr(nla);
  574. return 0;
  575. }
  576. /* only called when fib_nh is integrated into fib_info */
  577. static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
  578. int remaining, struct fib_config *cfg,
  579. struct netlink_ext_ack *extack)
  580. {
  581. struct net *net = fi->fib_net;
  582. struct fib_config fib_cfg;
  583. struct fib_nh *nh;
  584. int ret;
  585. change_nexthops(fi) {
  586. int attrlen;
  587. memset(&fib_cfg, 0, sizeof(fib_cfg));
  588. if (!rtnh_ok(rtnh, remaining)) {
  589. NL_SET_ERR_MSG(extack,
  590. "Invalid nexthop configuration - extra data after nexthop");
  591. return -EINVAL;
  592. }
  593. if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
  594. NL_SET_ERR_MSG(extack,
  595. "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
  596. return -EINVAL;
  597. }
  598. fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
  599. fib_cfg.fc_oif = rtnh->rtnh_ifindex;
  600. attrlen = rtnh_attrlen(rtnh);
  601. if (attrlen > 0) {
  602. struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
  603. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  604. nlav = nla_find(attrs, attrlen, RTA_VIA);
  605. if (nla && nlav) {
  606. NL_SET_ERR_MSG(extack,
  607. "Nexthop configuration can not contain both GATEWAY and VIA");
  608. return -EINVAL;
  609. }
  610. if (nla) {
  611. ret = fib_gw_from_attr(&fib_cfg.fc_gw4, nla,
  612. extack);
  613. if (ret)
  614. goto errout;
  615. if (fib_cfg.fc_gw4)
  616. fib_cfg.fc_gw_family = AF_INET;
  617. } else if (nlav) {
  618. ret = fib_gw_from_via(&fib_cfg, nlav, extack);
  619. if (ret)
  620. goto errout;
  621. }
  622. nla = nla_find(attrs, attrlen, RTA_FLOW);
  623. if (nla) {
  624. if (nla_len(nla) < sizeof(u32)) {
  625. NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
  626. return -EINVAL;
  627. }
  628. fib_cfg.fc_flow = nla_get_u32(nla);
  629. }
  630. fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
  631. /* RTA_ENCAP_TYPE length checked in
  632. * lwtunnel_valid_encap_type_attr
  633. */
  634. nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
  635. if (nla)
  636. fib_cfg.fc_encap_type = nla_get_u16(nla);
  637. }
  638. ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
  639. rtnh->rtnh_hops + 1, extack);
  640. if (ret)
  641. goto errout;
  642. rtnh = rtnh_next(rtnh, &remaining);
  643. } endfor_nexthops(fi);
  644. ret = -EINVAL;
  645. nh = fib_info_nh(fi, 0);
  646. if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
  647. NL_SET_ERR_MSG(extack,
  648. "Nexthop device index does not match RTA_OIF");
  649. goto errout;
  650. }
  651. if (cfg->fc_gw_family) {
  652. if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
  653. (cfg->fc_gw_family == AF_INET &&
  654. nh->fib_nh_gw4 != cfg->fc_gw4) ||
  655. (cfg->fc_gw_family == AF_INET6 &&
  656. ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
  657. NL_SET_ERR_MSG(extack,
  658. "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
  659. goto errout;
  660. }
  661. }
  662. #ifdef CONFIG_IP_ROUTE_CLASSID
  663. if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
  664. NL_SET_ERR_MSG(extack,
  665. "Nexthop class id does not match RTA_FLOW");
  666. goto errout;
  667. }
  668. #endif
  669. ret = 0;
  670. errout:
  671. return ret;
  672. }
  673. /* only called when fib_nh is integrated into fib_info */
  674. static void fib_rebalance(struct fib_info *fi)
  675. {
  676. int total;
  677. int w;
  678. if (fib_info_num_path(fi) < 2)
  679. return;
  680. total = 0;
  681. for_nexthops(fi) {
  682. if (nh->fib_nh_flags & RTNH_F_DEAD)
  683. continue;
  684. if (ip_ignore_linkdown(nh->fib_nh_dev) &&
  685. nh->fib_nh_flags & RTNH_F_LINKDOWN)
  686. continue;
  687. total += nh->fib_nh_weight;
  688. } endfor_nexthops(fi);
  689. w = 0;
  690. change_nexthops(fi) {
  691. int upper_bound;
  692. if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
  693. upper_bound = -1;
  694. } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
  695. nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
  696. upper_bound = -1;
  697. } else {
  698. w += nexthop_nh->fib_nh_weight;
  699. upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
  700. total) - 1;
  701. }
  702. atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
  703. } endfor_nexthops(fi);
  704. }
  705. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  706. static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
  707. int remaining, struct fib_config *cfg,
  708. struct netlink_ext_ack *extack)
  709. {
  710. NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
  711. return -EINVAL;
  712. }
  713. #define fib_rebalance(fi) do { } while (0)
  714. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  715. static int fib_encap_match(struct net *net, u16 encap_type,
  716. struct nlattr *encap,
  717. const struct fib_nh *nh,
  718. const struct fib_config *cfg,
  719. struct netlink_ext_ack *extack)
  720. {
  721. struct lwtunnel_state *lwtstate;
  722. int ret, result = 0;
  723. if (encap_type == LWTUNNEL_ENCAP_NONE)
  724. return 0;
  725. ret = lwtunnel_build_state(net, encap_type, encap, AF_INET,
  726. cfg, &lwtstate, extack);
  727. if (!ret) {
  728. result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
  729. lwtstate_free(lwtstate);
  730. }
  731. return result;
  732. }
  733. int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
  734. struct netlink_ext_ack *extack)
  735. {
  736. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  737. struct rtnexthop *rtnh;
  738. int remaining;
  739. #endif
  740. if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
  741. return 1;
  742. if (cfg->fc_nh_id) {
  743. if (fi->nh && cfg->fc_nh_id == fi->nh->id)
  744. return 0;
  745. return 1;
  746. }
  747. if (fi->nh) {
  748. if (cfg->fc_oif || cfg->fc_gw_family || cfg->fc_mp)
  749. return 1;
  750. return 0;
  751. }
  752. if (cfg->fc_oif || cfg->fc_gw_family) {
  753. struct fib_nh *nh;
  754. nh = fib_info_nh(fi, 0);
  755. if (cfg->fc_encap) {
  756. if (fib_encap_match(net, cfg->fc_encap_type,
  757. cfg->fc_encap, nh, cfg, extack))
  758. return 1;
  759. }
  760. #ifdef CONFIG_IP_ROUTE_CLASSID
  761. if (cfg->fc_flow &&
  762. cfg->fc_flow != nh->nh_tclassid)
  763. return 1;
  764. #endif
  765. if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
  766. (cfg->fc_gw_family &&
  767. cfg->fc_gw_family != nh->fib_nh_gw_family))
  768. return 1;
  769. if (cfg->fc_gw_family == AF_INET &&
  770. cfg->fc_gw4 != nh->fib_nh_gw4)
  771. return 1;
  772. if (cfg->fc_gw_family == AF_INET6 &&
  773. ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
  774. return 1;
  775. return 0;
  776. }
  777. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  778. if (!cfg->fc_mp)
  779. return 0;
  780. rtnh = cfg->fc_mp;
  781. remaining = cfg->fc_mp_len;
  782. for_nexthops(fi) {
  783. int attrlen;
  784. if (!rtnh_ok(rtnh, remaining))
  785. return -EINVAL;
  786. if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
  787. return 1;
  788. attrlen = rtnh_attrlen(rtnh);
  789. if (attrlen > 0) {
  790. struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
  791. int err;
  792. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  793. nlav = nla_find(attrs, attrlen, RTA_VIA);
  794. if (nla && nlav) {
  795. NL_SET_ERR_MSG(extack,
  796. "Nexthop configuration can not contain both GATEWAY and VIA");
  797. return -EINVAL;
  798. }
  799. if (nla) {
  800. __be32 gw;
  801. err = fib_gw_from_attr(&gw, nla, extack);
  802. if (err)
  803. return err;
  804. if (nh->fib_nh_gw_family != AF_INET ||
  805. gw != nh->fib_nh_gw4)
  806. return 1;
  807. } else if (nlav) {
  808. struct fib_config cfg2;
  809. err = fib_gw_from_via(&cfg2, nlav, extack);
  810. if (err)
  811. return err;
  812. switch (nh->fib_nh_gw_family) {
  813. case AF_INET:
  814. if (cfg2.fc_gw_family != AF_INET ||
  815. cfg2.fc_gw4 != nh->fib_nh_gw4)
  816. return 1;
  817. break;
  818. case AF_INET6:
  819. if (cfg2.fc_gw_family != AF_INET6 ||
  820. ipv6_addr_cmp(&cfg2.fc_gw6,
  821. &nh->fib_nh_gw6))
  822. return 1;
  823. break;
  824. }
  825. }
  826. #ifdef CONFIG_IP_ROUTE_CLASSID
  827. nla = nla_find(attrs, attrlen, RTA_FLOW);
  828. if (nla) {
  829. if (nla_len(nla) < sizeof(u32)) {
  830. NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
  831. return -EINVAL;
  832. }
  833. if (nla_get_u32(nla) != nh->nh_tclassid)
  834. return 1;
  835. }
  836. #endif
  837. }
  838. rtnh = rtnh_next(rtnh, &remaining);
  839. } endfor_nexthops(fi);
  840. #endif
  841. return 0;
  842. }
  843. bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
  844. {
  845. struct nlattr *nla;
  846. int remaining;
  847. if (!cfg->fc_mx)
  848. return true;
  849. nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
  850. int type = nla_type(nla);
  851. u32 fi_val, val;
  852. if (!type)
  853. continue;
  854. if (type > RTAX_MAX)
  855. return false;
  856. type = array_index_nospec(type, RTAX_MAX + 1);
  857. if (type == RTAX_CC_ALGO) {
  858. char tmp[TCP_CA_NAME_MAX];
  859. bool ecn_ca = false;
  860. nla_strscpy(tmp, nla, sizeof(tmp));
  861. val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
  862. } else {
  863. if (nla_len(nla) != sizeof(u32))
  864. return false;
  865. val = nla_get_u32(nla);
  866. }
  867. fi_val = fi->fib_metrics->metrics[type - 1];
  868. if (type == RTAX_FEATURES)
  869. fi_val &= ~DST_FEATURE_ECN_CA;
  870. if (fi_val != val)
  871. return false;
  872. }
  873. return true;
  874. }
  875. static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
  876. u32 table, struct netlink_ext_ack *extack)
  877. {
  878. struct fib6_config cfg = {
  879. .fc_table = table,
  880. .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
  881. .fc_ifindex = nh->fib_nh_oif,
  882. .fc_gateway = nh->fib_nh_gw6,
  883. };
  884. struct fib6_nh fib6_nh = {};
  885. int err;
  886. err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
  887. if (!err) {
  888. nh->fib_nh_dev = fib6_nh.fib_nh_dev;
  889. netdev_hold(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
  890. GFP_KERNEL);
  891. nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
  892. nh->fib_nh_scope = RT_SCOPE_LINK;
  893. ipv6_stub->fib6_nh_release(&fib6_nh);
  894. }
  895. return err;
  896. }
  897. /*
  898. * Picture
  899. * -------
  900. *
  901. * Semantics of nexthop is very messy by historical reasons.
  902. * We have to take into account, that:
  903. * a) gateway can be actually local interface address,
  904. * so that gatewayed route is direct.
  905. * b) gateway must be on-link address, possibly
  906. * described not by an ifaddr, but also by a direct route.
  907. * c) If both gateway and interface are specified, they should not
  908. * contradict.
  909. * d) If we use tunnel routes, gateway could be not on-link.
  910. *
  911. * Attempt to reconcile all of these (alas, self-contradictory) conditions
  912. * results in pretty ugly and hairy code with obscure logic.
  913. *
  914. * I chose to generalized it instead, so that the size
  915. * of code does not increase practically, but it becomes
  916. * much more general.
  917. * Every prefix is assigned a "scope" value: "host" is local address,
  918. * "link" is direct route,
  919. * [ ... "site" ... "interior" ... ]
  920. * and "universe" is true gateway route with global meaning.
  921. *
  922. * Every prefix refers to a set of "nexthop"s (gw, oif),
  923. * where gw must have narrower scope. This recursion stops
  924. * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
  925. * which means that gw is forced to be on link.
  926. *
  927. * Code is still hairy, but now it is apparently logically
  928. * consistent and very flexible. F.e. as by-product it allows
  929. * to co-exists in peace independent exterior and interior
  930. * routing processes.
  931. *
  932. * Normally it looks as following.
  933. *
  934. * {universe prefix} -> (gw, oif) [scope link]
  935. * |
  936. * |-> {link prefix} -> (gw, oif) [scope local]
  937. * |
  938. * |-> {local prefix} (terminal node)
  939. */
  940. static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
  941. u8 scope, struct netlink_ext_ack *extack)
  942. {
  943. struct net_device *dev;
  944. struct fib_result res;
  945. int err = 0;
  946. if (nh->fib_nh_flags & RTNH_F_ONLINK) {
  947. unsigned int addr_type;
  948. if (scope >= RT_SCOPE_LINK) {
  949. NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
  950. return -EINVAL;
  951. }
  952. dev = __dev_get_by_index(net, nh->fib_nh_oif);
  953. if (!dev) {
  954. NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
  955. return -ENODEV;
  956. }
  957. if (!(dev->flags & IFF_UP)) {
  958. NL_SET_ERR_MSG(extack, "Nexthop device is not up");
  959. return -ENETDOWN;
  960. }
  961. addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
  962. if (addr_type != RTN_UNICAST) {
  963. NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
  964. return -EINVAL;
  965. }
  966. if (!netif_carrier_ok(dev))
  967. nh->fib_nh_flags |= RTNH_F_LINKDOWN;
  968. nh->fib_nh_dev = dev;
  969. netdev_hold(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
  970. nh->fib_nh_scope = RT_SCOPE_LINK;
  971. return 0;
  972. }
  973. rcu_read_lock();
  974. {
  975. struct fib_table *tbl = NULL;
  976. struct flowi4 fl4 = {
  977. .daddr = nh->fib_nh_gw4,
  978. .flowi4_scope = scope + 1,
  979. .flowi4_oif = nh->fib_nh_oif,
  980. .flowi4_iif = LOOPBACK_IFINDEX,
  981. };
  982. /* It is not necessary, but requires a bit of thinking */
  983. if (fl4.flowi4_scope < RT_SCOPE_LINK)
  984. fl4.flowi4_scope = RT_SCOPE_LINK;
  985. if (table && table != RT_TABLE_MAIN)
  986. tbl = fib_get_table(net, table);
  987. if (tbl)
  988. err = fib_table_lookup(tbl, &fl4, &res,
  989. FIB_LOOKUP_IGNORE_LINKSTATE |
  990. FIB_LOOKUP_NOREF);
  991. /* on error or if no table given do full lookup. This
  992. * is needed for example when nexthops are in the local
  993. * table rather than the given table
  994. */
  995. if (!tbl || err) {
  996. err = fib_lookup(net, &fl4, &res,
  997. FIB_LOOKUP_IGNORE_LINKSTATE);
  998. }
  999. if (err) {
  1000. NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
  1001. goto out;
  1002. }
  1003. }
  1004. err = -EINVAL;
  1005. if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
  1006. NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
  1007. goto out;
  1008. }
  1009. nh->fib_nh_scope = res.scope;
  1010. nh->fib_nh_oif = FIB_RES_OIF(res);
  1011. nh->fib_nh_dev = dev = FIB_RES_DEV(res);
  1012. if (!dev) {
  1013. NL_SET_ERR_MSG(extack,
  1014. "No egress device for nexthop gateway");
  1015. goto out;
  1016. }
  1017. netdev_hold(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
  1018. if (!netif_carrier_ok(dev))
  1019. nh->fib_nh_flags |= RTNH_F_LINKDOWN;
  1020. err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
  1021. out:
  1022. rcu_read_unlock();
  1023. return err;
  1024. }
  1025. static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
  1026. struct netlink_ext_ack *extack)
  1027. {
  1028. struct in_device *in_dev;
  1029. int err;
  1030. if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
  1031. NL_SET_ERR_MSG(extack,
  1032. "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
  1033. return -EINVAL;
  1034. }
  1035. rcu_read_lock();
  1036. err = -ENODEV;
  1037. in_dev = inetdev_by_index(net, nh->fib_nh_oif);
  1038. if (!in_dev)
  1039. goto out;
  1040. err = -ENETDOWN;
  1041. if (!(in_dev->dev->flags & IFF_UP)) {
  1042. NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
  1043. goto out;
  1044. }
  1045. nh->fib_nh_dev = in_dev->dev;
  1046. netdev_hold(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
  1047. nh->fib_nh_scope = RT_SCOPE_HOST;
  1048. if (!netif_carrier_ok(nh->fib_nh_dev))
  1049. nh->fib_nh_flags |= RTNH_F_LINKDOWN;
  1050. err = 0;
  1051. out:
  1052. rcu_read_unlock();
  1053. return err;
  1054. }
  1055. int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
  1056. struct netlink_ext_ack *extack)
  1057. {
  1058. int err;
  1059. if (nh->fib_nh_gw_family == AF_INET)
  1060. err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
  1061. else if (nh->fib_nh_gw_family == AF_INET6)
  1062. err = fib_check_nh_v6_gw(net, nh, table, extack);
  1063. else
  1064. err = fib_check_nh_nongw(net, nh, extack);
  1065. return err;
  1066. }
  1067. static struct hlist_head *
  1068. fib_info_laddrhash_bucket(const struct net *net, __be32 val)
  1069. {
  1070. u32 slot = hash_32(net_hash_mix(net) ^ (__force u32)val,
  1071. fib_info_hash_bits);
  1072. return &fib_info_laddrhash[slot];
  1073. }
  1074. static void fib_info_hash_move(struct hlist_head *new_info_hash,
  1075. struct hlist_head *new_laddrhash,
  1076. unsigned int new_size)
  1077. {
  1078. struct hlist_head *old_info_hash, *old_laddrhash;
  1079. unsigned int old_size = fib_info_hash_size;
  1080. unsigned int i;
  1081. spin_lock_bh(&fib_info_lock);
  1082. old_info_hash = fib_info_hash;
  1083. old_laddrhash = fib_info_laddrhash;
  1084. fib_info_hash_size = new_size;
  1085. fib_info_hash_bits = ilog2(new_size);
  1086. for (i = 0; i < old_size; i++) {
  1087. struct hlist_head *head = &fib_info_hash[i];
  1088. struct hlist_node *n;
  1089. struct fib_info *fi;
  1090. hlist_for_each_entry_safe(fi, n, head, fib_hash) {
  1091. struct hlist_head *dest;
  1092. unsigned int new_hash;
  1093. new_hash = fib_info_hashfn(fi);
  1094. dest = &new_info_hash[new_hash];
  1095. hlist_add_head(&fi->fib_hash, dest);
  1096. }
  1097. }
  1098. fib_info_hash = new_info_hash;
  1099. fib_info_laddrhash = new_laddrhash;
  1100. for (i = 0; i < old_size; i++) {
  1101. struct hlist_head *lhead = &old_laddrhash[i];
  1102. struct hlist_node *n;
  1103. struct fib_info *fi;
  1104. hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
  1105. struct hlist_head *ldest;
  1106. ldest = fib_info_laddrhash_bucket(fi->fib_net,
  1107. fi->fib_prefsrc);
  1108. hlist_add_head(&fi->fib_lhash, ldest);
  1109. }
  1110. }
  1111. spin_unlock_bh(&fib_info_lock);
  1112. kvfree(old_info_hash);
  1113. kvfree(old_laddrhash);
  1114. }
  1115. __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
  1116. unsigned char scope)
  1117. {
  1118. struct fib_nh *nh;
  1119. __be32 saddr;
  1120. if (nhc->nhc_family != AF_INET)
  1121. return inet_select_addr(nhc->nhc_dev, 0, scope);
  1122. nh = container_of(nhc, struct fib_nh, nh_common);
  1123. saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
  1124. WRITE_ONCE(nh->nh_saddr, saddr);
  1125. WRITE_ONCE(nh->nh_saddr_genid, atomic_read(&net->ipv4.dev_addr_genid));
  1126. return saddr;
  1127. }
  1128. __be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
  1129. {
  1130. struct fib_nh_common *nhc = res->nhc;
  1131. if (res->fi->fib_prefsrc)
  1132. return res->fi->fib_prefsrc;
  1133. if (nhc->nhc_family == AF_INET) {
  1134. struct fib_nh *nh;
  1135. nh = container_of(nhc, struct fib_nh, nh_common);
  1136. if (READ_ONCE(nh->nh_saddr_genid) ==
  1137. atomic_read(&net->ipv4.dev_addr_genid))
  1138. return READ_ONCE(nh->nh_saddr);
  1139. }
  1140. return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
  1141. }
  1142. static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
  1143. {
  1144. if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
  1145. fib_prefsrc != cfg->fc_dst) {
  1146. u32 tb_id = cfg->fc_table;
  1147. int rc;
  1148. if (tb_id == RT_TABLE_MAIN)
  1149. tb_id = RT_TABLE_LOCAL;
  1150. rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
  1151. fib_prefsrc, tb_id);
  1152. if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
  1153. rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
  1154. fib_prefsrc, RT_TABLE_LOCAL);
  1155. }
  1156. if (rc != RTN_LOCAL)
  1157. return false;
  1158. }
  1159. return true;
  1160. }
  1161. struct fib_info *fib_create_info(struct fib_config *cfg,
  1162. struct netlink_ext_ack *extack)
  1163. {
  1164. int err;
  1165. struct fib_info *fi = NULL;
  1166. struct nexthop *nh = NULL;
  1167. struct fib_info *ofi;
  1168. int nhs = 1;
  1169. struct net *net = cfg->fc_nlinfo.nl_net;
  1170. if (cfg->fc_type > RTN_MAX)
  1171. goto err_inval;
  1172. /* Fast check to catch the most weird cases */
  1173. if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
  1174. NL_SET_ERR_MSG(extack, "Invalid scope");
  1175. goto err_inval;
  1176. }
  1177. if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
  1178. NL_SET_ERR_MSG(extack,
  1179. "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
  1180. goto err_inval;
  1181. }
  1182. if (cfg->fc_nh_id) {
  1183. if (!cfg->fc_mx) {
  1184. fi = fib_find_info_nh(net, cfg);
  1185. if (fi) {
  1186. refcount_inc(&fi->fib_treeref);
  1187. return fi;
  1188. }
  1189. }
  1190. nh = nexthop_find_by_id(net, cfg->fc_nh_id);
  1191. if (!nh) {
  1192. NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
  1193. goto err_inval;
  1194. }
  1195. nhs = 0;
  1196. }
  1197. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1198. if (cfg->fc_mp) {
  1199. nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
  1200. if (nhs == 0)
  1201. goto err_inval;
  1202. }
  1203. #endif
  1204. err = -ENOBUFS;
  1205. /* Paired with WRITE_ONCE() in fib_release_info() */
  1206. if (READ_ONCE(fib_info_cnt) >= fib_info_hash_size) {
  1207. unsigned int new_size = fib_info_hash_size << 1;
  1208. struct hlist_head *new_info_hash;
  1209. struct hlist_head *new_laddrhash;
  1210. size_t bytes;
  1211. if (!new_size)
  1212. new_size = 16;
  1213. bytes = (size_t)new_size * sizeof(struct hlist_head *);
  1214. new_info_hash = kvzalloc(bytes, GFP_KERNEL);
  1215. new_laddrhash = kvzalloc(bytes, GFP_KERNEL);
  1216. if (!new_info_hash || !new_laddrhash) {
  1217. kvfree(new_info_hash);
  1218. kvfree(new_laddrhash);
  1219. } else {
  1220. fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
  1221. }
  1222. if (!fib_info_hash_size)
  1223. goto failure;
  1224. }
  1225. fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
  1226. if (!fi)
  1227. goto failure;
  1228. fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
  1229. cfg->fc_mx_len, extack);
  1230. if (IS_ERR(fi->fib_metrics)) {
  1231. err = PTR_ERR(fi->fib_metrics);
  1232. kfree(fi);
  1233. return ERR_PTR(err);
  1234. }
  1235. fi->fib_net = net;
  1236. fi->fib_protocol = cfg->fc_protocol;
  1237. fi->fib_scope = cfg->fc_scope;
  1238. fi->fib_flags = cfg->fc_flags;
  1239. fi->fib_priority = cfg->fc_priority;
  1240. fi->fib_prefsrc = cfg->fc_prefsrc;
  1241. fi->fib_type = cfg->fc_type;
  1242. fi->fib_tb_id = cfg->fc_table;
  1243. fi->fib_nhs = nhs;
  1244. if (nh) {
  1245. if (!nexthop_get(nh)) {
  1246. NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
  1247. err = -EINVAL;
  1248. } else {
  1249. err = 0;
  1250. fi->nh = nh;
  1251. }
  1252. } else {
  1253. change_nexthops(fi) {
  1254. nexthop_nh->nh_parent = fi;
  1255. } endfor_nexthops(fi)
  1256. if (cfg->fc_mp)
  1257. err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
  1258. extack);
  1259. else
  1260. err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
  1261. }
  1262. if (err != 0)
  1263. goto failure;
  1264. if (fib_props[cfg->fc_type].error) {
  1265. if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
  1266. NL_SET_ERR_MSG(extack,
  1267. "Gateway, device and multipath can not be specified for this route type");
  1268. goto err_inval;
  1269. }
  1270. goto link_it;
  1271. } else {
  1272. switch (cfg->fc_type) {
  1273. case RTN_UNICAST:
  1274. case RTN_LOCAL:
  1275. case RTN_BROADCAST:
  1276. case RTN_ANYCAST:
  1277. case RTN_MULTICAST:
  1278. break;
  1279. default:
  1280. NL_SET_ERR_MSG(extack, "Invalid route type");
  1281. goto err_inval;
  1282. }
  1283. }
  1284. if (cfg->fc_scope > RT_SCOPE_HOST) {
  1285. NL_SET_ERR_MSG(extack, "Invalid scope");
  1286. goto err_inval;
  1287. }
  1288. if (fi->nh) {
  1289. err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
  1290. if (err)
  1291. goto failure;
  1292. } else if (cfg->fc_scope == RT_SCOPE_HOST) {
  1293. struct fib_nh *nh = fi->fib_nh;
  1294. /* Local address is added. */
  1295. if (nhs != 1) {
  1296. NL_SET_ERR_MSG(extack,
  1297. "Route with host scope can not have multiple nexthops");
  1298. goto err_inval;
  1299. }
  1300. if (nh->fib_nh_gw_family) {
  1301. NL_SET_ERR_MSG(extack,
  1302. "Route with host scope can not have a gateway");
  1303. goto err_inval;
  1304. }
  1305. nh->fib_nh_scope = RT_SCOPE_NOWHERE;
  1306. nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
  1307. err = -ENODEV;
  1308. if (!nh->fib_nh_dev)
  1309. goto failure;
  1310. netdev_tracker_alloc(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
  1311. GFP_KERNEL);
  1312. } else {
  1313. int linkdown = 0;
  1314. change_nexthops(fi) {
  1315. err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
  1316. cfg->fc_table, cfg->fc_scope,
  1317. extack);
  1318. if (err != 0)
  1319. goto failure;
  1320. if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
  1321. linkdown++;
  1322. } endfor_nexthops(fi)
  1323. if (linkdown == fi->fib_nhs)
  1324. fi->fib_flags |= RTNH_F_LINKDOWN;
  1325. }
  1326. if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
  1327. NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
  1328. goto err_inval;
  1329. }
  1330. if (!fi->nh) {
  1331. change_nexthops(fi) {
  1332. fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
  1333. fi->fib_scope);
  1334. if (nexthop_nh->fib_nh_gw_family == AF_INET6)
  1335. fi->fib_nh_is_v6 = true;
  1336. } endfor_nexthops(fi)
  1337. fib_rebalance(fi);
  1338. }
  1339. link_it:
  1340. ofi = fib_find_info(fi);
  1341. if (ofi) {
  1342. /* fib_table_lookup() should not see @fi yet. */
  1343. fi->fib_dead = 1;
  1344. free_fib_info(fi);
  1345. refcount_inc(&ofi->fib_treeref);
  1346. return ofi;
  1347. }
  1348. refcount_set(&fi->fib_treeref, 1);
  1349. refcount_set(&fi->fib_clntref, 1);
  1350. spin_lock_bh(&fib_info_lock);
  1351. fib_info_cnt++;
  1352. hlist_add_head(&fi->fib_hash,
  1353. &fib_info_hash[fib_info_hashfn(fi)]);
  1354. if (fi->fib_prefsrc) {
  1355. struct hlist_head *head;
  1356. head = fib_info_laddrhash_bucket(net, fi->fib_prefsrc);
  1357. hlist_add_head(&fi->fib_lhash, head);
  1358. }
  1359. if (fi->nh) {
  1360. list_add(&fi->nh_list, &nh->fi_list);
  1361. } else {
  1362. change_nexthops(fi) {
  1363. struct hlist_head *head;
  1364. if (!nexthop_nh->fib_nh_dev)
  1365. continue;
  1366. head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev);
  1367. hlist_add_head(&nexthop_nh->nh_hash, head);
  1368. } endfor_nexthops(fi)
  1369. }
  1370. spin_unlock_bh(&fib_info_lock);
  1371. return fi;
  1372. err_inval:
  1373. err = -EINVAL;
  1374. failure:
  1375. if (fi) {
  1376. /* fib_table_lookup() should not see @fi yet. */
  1377. fi->fib_dead = 1;
  1378. free_fib_info(fi);
  1379. }
  1380. return ERR_PTR(err);
  1381. }
  1382. int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
  1383. u8 rt_family, unsigned char *flags, bool skip_oif)
  1384. {
  1385. if (nhc->nhc_flags & RTNH_F_DEAD)
  1386. *flags |= RTNH_F_DEAD;
  1387. if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
  1388. *flags |= RTNH_F_LINKDOWN;
  1389. rcu_read_lock();
  1390. switch (nhc->nhc_family) {
  1391. case AF_INET:
  1392. if (ip_ignore_linkdown(nhc->nhc_dev))
  1393. *flags |= RTNH_F_DEAD;
  1394. break;
  1395. case AF_INET6:
  1396. if (ip6_ignore_linkdown(nhc->nhc_dev))
  1397. *flags |= RTNH_F_DEAD;
  1398. break;
  1399. }
  1400. rcu_read_unlock();
  1401. }
  1402. switch (nhc->nhc_gw_family) {
  1403. case AF_INET:
  1404. if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
  1405. goto nla_put_failure;
  1406. break;
  1407. case AF_INET6:
  1408. /* if gateway family does not match nexthop family
  1409. * gateway is encoded as RTA_VIA
  1410. */
  1411. if (rt_family != nhc->nhc_gw_family) {
  1412. int alen = sizeof(struct in6_addr);
  1413. struct nlattr *nla;
  1414. struct rtvia *via;
  1415. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  1416. if (!nla)
  1417. goto nla_put_failure;
  1418. via = nla_data(nla);
  1419. via->rtvia_family = AF_INET6;
  1420. memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
  1421. } else if (nla_put_in6_addr(skb, RTA_GATEWAY,
  1422. &nhc->nhc_gw.ipv6) < 0) {
  1423. goto nla_put_failure;
  1424. }
  1425. break;
  1426. }
  1427. *flags |= (nhc->nhc_flags &
  1428. (RTNH_F_ONLINK | RTNH_F_OFFLOAD | RTNH_F_TRAP));
  1429. if (!skip_oif && nhc->nhc_dev &&
  1430. nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
  1431. goto nla_put_failure;
  1432. if (nhc->nhc_lwtstate &&
  1433. lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
  1434. RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
  1435. goto nla_put_failure;
  1436. return 0;
  1437. nla_put_failure:
  1438. return -EMSGSIZE;
  1439. }
  1440. EXPORT_SYMBOL_GPL(fib_nexthop_info);
  1441. #if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
  1442. int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
  1443. int nh_weight, u8 rt_family, u32 nh_tclassid)
  1444. {
  1445. const struct net_device *dev = nhc->nhc_dev;
  1446. struct rtnexthop *rtnh;
  1447. unsigned char flags = 0;
  1448. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1449. if (!rtnh)
  1450. goto nla_put_failure;
  1451. rtnh->rtnh_hops = nh_weight - 1;
  1452. rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
  1453. if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
  1454. goto nla_put_failure;
  1455. rtnh->rtnh_flags = flags;
  1456. if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
  1457. goto nla_put_failure;
  1458. /* length of rtnetlink header + attributes */
  1459. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1460. return 0;
  1461. nla_put_failure:
  1462. return -EMSGSIZE;
  1463. }
  1464. EXPORT_SYMBOL_GPL(fib_add_nexthop);
  1465. #endif
  1466. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1467. static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
  1468. {
  1469. struct nlattr *mp;
  1470. mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
  1471. if (!mp)
  1472. goto nla_put_failure;
  1473. if (unlikely(fi->nh)) {
  1474. if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
  1475. goto nla_put_failure;
  1476. goto mp_end;
  1477. }
  1478. for_nexthops(fi) {
  1479. u32 nh_tclassid = 0;
  1480. #ifdef CONFIG_IP_ROUTE_CLASSID
  1481. nh_tclassid = nh->nh_tclassid;
  1482. #endif
  1483. if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
  1484. AF_INET, nh_tclassid) < 0)
  1485. goto nla_put_failure;
  1486. } endfor_nexthops(fi);
  1487. mp_end:
  1488. nla_nest_end(skb, mp);
  1489. return 0;
  1490. nla_put_failure:
  1491. return -EMSGSIZE;
  1492. }
  1493. #else
  1494. static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
  1495. {
  1496. return 0;
  1497. }
  1498. #endif
  1499. int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1500. const struct fib_rt_info *fri, unsigned int flags)
  1501. {
  1502. unsigned int nhs = fib_info_num_path(fri->fi);
  1503. struct fib_info *fi = fri->fi;
  1504. u32 tb_id = fri->tb_id;
  1505. struct nlmsghdr *nlh;
  1506. struct rtmsg *rtm;
  1507. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1508. if (!nlh)
  1509. return -EMSGSIZE;
  1510. rtm = nlmsg_data(nlh);
  1511. rtm->rtm_family = AF_INET;
  1512. rtm->rtm_dst_len = fri->dst_len;
  1513. rtm->rtm_src_len = 0;
  1514. rtm->rtm_tos = inet_dscp_to_dsfield(fri->dscp);
  1515. if (tb_id < 256)
  1516. rtm->rtm_table = tb_id;
  1517. else
  1518. rtm->rtm_table = RT_TABLE_COMPAT;
  1519. if (nla_put_u32(skb, RTA_TABLE, tb_id))
  1520. goto nla_put_failure;
  1521. rtm->rtm_type = fri->type;
  1522. rtm->rtm_flags = fi->fib_flags;
  1523. rtm->rtm_scope = fi->fib_scope;
  1524. rtm->rtm_protocol = fi->fib_protocol;
  1525. if (rtm->rtm_dst_len &&
  1526. nla_put_in_addr(skb, RTA_DST, fri->dst))
  1527. goto nla_put_failure;
  1528. if (fi->fib_priority &&
  1529. nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
  1530. goto nla_put_failure;
  1531. if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
  1532. goto nla_put_failure;
  1533. if (fi->fib_prefsrc &&
  1534. nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
  1535. goto nla_put_failure;
  1536. if (fi->nh) {
  1537. if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
  1538. goto nla_put_failure;
  1539. if (nexthop_is_blackhole(fi->nh))
  1540. rtm->rtm_type = RTN_BLACKHOLE;
  1541. if (!READ_ONCE(fi->fib_net->ipv4.sysctl_nexthop_compat_mode))
  1542. goto offload;
  1543. }
  1544. if (nhs == 1) {
  1545. const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
  1546. unsigned char flags = 0;
  1547. if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
  1548. goto nla_put_failure;
  1549. rtm->rtm_flags = flags;
  1550. #ifdef CONFIG_IP_ROUTE_CLASSID
  1551. if (nhc->nhc_family == AF_INET) {
  1552. struct fib_nh *nh;
  1553. nh = container_of(nhc, struct fib_nh, nh_common);
  1554. if (nh->nh_tclassid &&
  1555. nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
  1556. goto nla_put_failure;
  1557. }
  1558. #endif
  1559. } else {
  1560. if (fib_add_multipath(skb, fi) < 0)
  1561. goto nla_put_failure;
  1562. }
  1563. offload:
  1564. if (fri->offload)
  1565. rtm->rtm_flags |= RTM_F_OFFLOAD;
  1566. if (fri->trap)
  1567. rtm->rtm_flags |= RTM_F_TRAP;
  1568. if (fri->offload_failed)
  1569. rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
  1570. nlmsg_end(skb, nlh);
  1571. return 0;
  1572. nla_put_failure:
  1573. nlmsg_cancel(skb, nlh);
  1574. return -EMSGSIZE;
  1575. }
  1576. /*
  1577. * Update FIB if:
  1578. * - local address disappeared -> we must delete all the entries
  1579. * referring to it.
  1580. * - device went down -> we must shutdown all nexthops going via it.
  1581. */
  1582. int fib_sync_down_addr(struct net_device *dev, __be32 local)
  1583. {
  1584. int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
  1585. struct net *net = dev_net(dev);
  1586. struct hlist_head *head;
  1587. struct fib_info *fi;
  1588. int ret = 0;
  1589. if (!fib_info_laddrhash || local == 0)
  1590. return 0;
  1591. head = fib_info_laddrhash_bucket(net, local);
  1592. hlist_for_each_entry(fi, head, fib_lhash) {
  1593. if (!net_eq(fi->fib_net, net) ||
  1594. fi->fib_tb_id != tb_id)
  1595. continue;
  1596. if (fi->fib_prefsrc == local) {
  1597. fi->fib_flags |= RTNH_F_DEAD;
  1598. ret++;
  1599. }
  1600. }
  1601. return ret;
  1602. }
  1603. static int call_fib_nh_notifiers(struct fib_nh *nh,
  1604. enum fib_event_type event_type)
  1605. {
  1606. bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
  1607. struct fib_nh_notifier_info info = {
  1608. .fib_nh = nh,
  1609. };
  1610. switch (event_type) {
  1611. case FIB_EVENT_NH_ADD:
  1612. if (nh->fib_nh_flags & RTNH_F_DEAD)
  1613. break;
  1614. if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
  1615. break;
  1616. return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
  1617. &info.info);
  1618. case FIB_EVENT_NH_DEL:
  1619. if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
  1620. (nh->fib_nh_flags & RTNH_F_DEAD))
  1621. return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
  1622. event_type, &info.info);
  1623. break;
  1624. default:
  1625. break;
  1626. }
  1627. return NOTIFY_DONE;
  1628. }
  1629. /* Update the PMTU of exceptions when:
  1630. * - the new MTU of the first hop becomes smaller than the PMTU
  1631. * - the old MTU was the same as the PMTU, and it limited discovery of
  1632. * larger MTUs on the path. With that limit raised, we can now
  1633. * discover larger MTUs
  1634. * A special case is locked exceptions, for which the PMTU is smaller
  1635. * than the minimal accepted PMTU:
  1636. * - if the new MTU is greater than the PMTU, don't make any change
  1637. * - otherwise, unlock and set PMTU
  1638. */
  1639. void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
  1640. {
  1641. struct fnhe_hash_bucket *bucket;
  1642. int i;
  1643. bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
  1644. if (!bucket)
  1645. return;
  1646. for (i = 0; i < FNHE_HASH_SIZE; i++) {
  1647. struct fib_nh_exception *fnhe;
  1648. for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
  1649. fnhe;
  1650. fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
  1651. if (fnhe->fnhe_mtu_locked) {
  1652. if (new <= fnhe->fnhe_pmtu) {
  1653. fnhe->fnhe_pmtu = new;
  1654. fnhe->fnhe_mtu_locked = false;
  1655. }
  1656. } else if (new < fnhe->fnhe_pmtu ||
  1657. orig == fnhe->fnhe_pmtu) {
  1658. fnhe->fnhe_pmtu = new;
  1659. }
  1660. }
  1661. }
  1662. }
  1663. void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
  1664. {
  1665. struct hlist_head *head = fib_info_devhash_bucket(dev);
  1666. struct fib_nh *nh;
  1667. hlist_for_each_entry(nh, head, nh_hash) {
  1668. if (nh->fib_nh_dev == dev)
  1669. fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
  1670. }
  1671. }
  1672. /* Event force Flags Description
  1673. * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
  1674. * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
  1675. * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
  1676. * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
  1677. *
  1678. * only used when fib_nh is built into fib_info
  1679. */
  1680. int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
  1681. {
  1682. struct hlist_head *head = fib_info_devhash_bucket(dev);
  1683. struct fib_info *prev_fi = NULL;
  1684. int scope = RT_SCOPE_NOWHERE;
  1685. struct fib_nh *nh;
  1686. int ret = 0;
  1687. if (force)
  1688. scope = -1;
  1689. hlist_for_each_entry(nh, head, nh_hash) {
  1690. struct fib_info *fi = nh->nh_parent;
  1691. int dead;
  1692. BUG_ON(!fi->fib_nhs);
  1693. if (nh->fib_nh_dev != dev || fi == prev_fi)
  1694. continue;
  1695. prev_fi = fi;
  1696. dead = 0;
  1697. change_nexthops(fi) {
  1698. if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
  1699. dead++;
  1700. else if (nexthop_nh->fib_nh_dev == dev &&
  1701. nexthop_nh->fib_nh_scope != scope) {
  1702. switch (event) {
  1703. case NETDEV_DOWN:
  1704. case NETDEV_UNREGISTER:
  1705. nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
  1706. fallthrough;
  1707. case NETDEV_CHANGE:
  1708. nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
  1709. break;
  1710. }
  1711. call_fib_nh_notifiers(nexthop_nh,
  1712. FIB_EVENT_NH_DEL);
  1713. dead++;
  1714. }
  1715. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1716. if (event == NETDEV_UNREGISTER &&
  1717. nexthop_nh->fib_nh_dev == dev) {
  1718. dead = fi->fib_nhs;
  1719. break;
  1720. }
  1721. #endif
  1722. } endfor_nexthops(fi)
  1723. if (dead == fi->fib_nhs) {
  1724. switch (event) {
  1725. case NETDEV_DOWN:
  1726. case NETDEV_UNREGISTER:
  1727. fi->fib_flags |= RTNH_F_DEAD;
  1728. fallthrough;
  1729. case NETDEV_CHANGE:
  1730. fi->fib_flags |= RTNH_F_LINKDOWN;
  1731. break;
  1732. }
  1733. ret++;
  1734. }
  1735. fib_rebalance(fi);
  1736. }
  1737. return ret;
  1738. }
  1739. /* Must be invoked inside of an RCU protected region. */
  1740. static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
  1741. {
  1742. struct fib_info *fi = NULL, *last_resort = NULL;
  1743. struct hlist_head *fa_head = res->fa_head;
  1744. struct fib_table *tb = res->table;
  1745. u8 slen = 32 - res->prefixlen;
  1746. int order = -1, last_idx = -1;
  1747. struct fib_alias *fa, *fa1 = NULL;
  1748. u32 last_prio = res->fi->fib_priority;
  1749. dscp_t last_dscp = 0;
  1750. hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
  1751. struct fib_info *next_fi = fa->fa_info;
  1752. struct fib_nh_common *nhc;
  1753. if (fa->fa_slen != slen)
  1754. continue;
  1755. if (fa->fa_dscp &&
  1756. fa->fa_dscp != inet_dsfield_to_dscp(flp->flowi4_tos))
  1757. continue;
  1758. if (fa->tb_id != tb->tb_id)
  1759. continue;
  1760. if (next_fi->fib_priority > last_prio &&
  1761. fa->fa_dscp == last_dscp) {
  1762. if (last_dscp)
  1763. continue;
  1764. break;
  1765. }
  1766. if (next_fi->fib_flags & RTNH_F_DEAD)
  1767. continue;
  1768. last_dscp = fa->fa_dscp;
  1769. last_prio = next_fi->fib_priority;
  1770. if (next_fi->fib_scope != res->scope ||
  1771. fa->fa_type != RTN_UNICAST)
  1772. continue;
  1773. nhc = fib_info_nhc(next_fi, 0);
  1774. if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
  1775. continue;
  1776. fib_alias_accessed(fa);
  1777. if (!fi) {
  1778. if (next_fi != res->fi)
  1779. break;
  1780. fa1 = fa;
  1781. } else if (!fib_detect_death(fi, order, &last_resort,
  1782. &last_idx, fa1->fa_default)) {
  1783. fib_result_assign(res, fi);
  1784. fa1->fa_default = order;
  1785. goto out;
  1786. }
  1787. fi = next_fi;
  1788. order++;
  1789. }
  1790. if (order <= 0 || !fi) {
  1791. if (fa1)
  1792. fa1->fa_default = -1;
  1793. goto out;
  1794. }
  1795. if (!fib_detect_death(fi, order, &last_resort, &last_idx,
  1796. fa1->fa_default)) {
  1797. fib_result_assign(res, fi);
  1798. fa1->fa_default = order;
  1799. goto out;
  1800. }
  1801. if (last_idx >= 0)
  1802. fib_result_assign(res, last_resort);
  1803. fa1->fa_default = last_idx;
  1804. out:
  1805. return;
  1806. }
  1807. /*
  1808. * Dead device goes up. We wake up dead nexthops.
  1809. * It takes sense only on multipath routes.
  1810. *
  1811. * only used when fib_nh is built into fib_info
  1812. */
  1813. int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
  1814. {
  1815. struct fib_info *prev_fi;
  1816. struct hlist_head *head;
  1817. struct fib_nh *nh;
  1818. int ret;
  1819. if (!(dev->flags & IFF_UP))
  1820. return 0;
  1821. if (nh_flags & RTNH_F_DEAD) {
  1822. unsigned int flags = dev_get_flags(dev);
  1823. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1824. nh_flags |= RTNH_F_LINKDOWN;
  1825. }
  1826. prev_fi = NULL;
  1827. head = fib_info_devhash_bucket(dev);
  1828. ret = 0;
  1829. hlist_for_each_entry(nh, head, nh_hash) {
  1830. struct fib_info *fi = nh->nh_parent;
  1831. int alive;
  1832. BUG_ON(!fi->fib_nhs);
  1833. if (nh->fib_nh_dev != dev || fi == prev_fi)
  1834. continue;
  1835. prev_fi = fi;
  1836. alive = 0;
  1837. change_nexthops(fi) {
  1838. if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
  1839. alive++;
  1840. continue;
  1841. }
  1842. if (!nexthop_nh->fib_nh_dev ||
  1843. !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
  1844. continue;
  1845. if (nexthop_nh->fib_nh_dev != dev ||
  1846. !__in_dev_get_rtnl(dev))
  1847. continue;
  1848. alive++;
  1849. nexthop_nh->fib_nh_flags &= ~nh_flags;
  1850. call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
  1851. } endfor_nexthops(fi)
  1852. if (alive > 0) {
  1853. fi->fib_flags &= ~nh_flags;
  1854. ret++;
  1855. }
  1856. fib_rebalance(fi);
  1857. }
  1858. return ret;
  1859. }
  1860. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1861. static bool fib_good_nh(const struct fib_nh *nh)
  1862. {
  1863. int state = NUD_REACHABLE;
  1864. if (nh->fib_nh_scope == RT_SCOPE_LINK) {
  1865. struct neighbour *n;
  1866. rcu_read_lock();
  1867. if (likely(nh->fib_nh_gw_family == AF_INET))
  1868. n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
  1869. (__force u32)nh->fib_nh_gw4);
  1870. else if (nh->fib_nh_gw_family == AF_INET6)
  1871. n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
  1872. &nh->fib_nh_gw6);
  1873. else
  1874. n = NULL;
  1875. if (n)
  1876. state = READ_ONCE(n->nud_state);
  1877. rcu_read_unlock();
  1878. }
  1879. return !!(state & NUD_VALID);
  1880. }
  1881. void fib_select_multipath(struct fib_result *res, int hash)
  1882. {
  1883. struct fib_info *fi = res->fi;
  1884. struct net *net = fi->fib_net;
  1885. bool first = false;
  1886. if (unlikely(res->fi->nh)) {
  1887. nexthop_path_fib_result(res, hash);
  1888. return;
  1889. }
  1890. change_nexthops(fi) {
  1891. if (READ_ONCE(net->ipv4.sysctl_fib_multipath_use_neigh)) {
  1892. if (!fib_good_nh(nexthop_nh))
  1893. continue;
  1894. if (!first) {
  1895. res->nh_sel = nhsel;
  1896. res->nhc = &nexthop_nh->nh_common;
  1897. first = true;
  1898. }
  1899. }
  1900. if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
  1901. continue;
  1902. res->nh_sel = nhsel;
  1903. res->nhc = &nexthop_nh->nh_common;
  1904. return;
  1905. } endfor_nexthops(fi);
  1906. }
  1907. #endif
  1908. void fib_select_path(struct net *net, struct fib_result *res,
  1909. struct flowi4 *fl4, const struct sk_buff *skb)
  1910. {
  1911. if (fl4->flowi4_oif)
  1912. goto check_saddr;
  1913. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1914. if (fib_info_num_path(res->fi) > 1) {
  1915. int h = fib_multipath_hash(net, fl4, skb, NULL);
  1916. fib_select_multipath(res, h);
  1917. }
  1918. else
  1919. #endif
  1920. if (!res->prefixlen &&
  1921. res->table->tb_num_default > 1 &&
  1922. res->type == RTN_UNICAST)
  1923. fib_select_default(fl4, res);
  1924. check_saddr:
  1925. if (!fl4->saddr)
  1926. fl4->saddr = fib_result_prefsrc(net, res);
  1927. }