1
0

fib_semantics.c 53 KB

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