fib_semantics.c 36 KB

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