fib_semantics.c 33 KB

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