fib_semantics.c 38 KB

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