fib_semantics.c 41 KB

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