PageRenderTime 52ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/net/ipv4/fib_semantics.c

https://github.com/mstsirkin/kvm
C | 1242 lines | 971 code | 175 blank | 96 comment | 233 complexity | b814d73cd427f7aeecdc0d5ecc98b43b MD5 | raw file
  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 <asm/system.h>
  17. #include <linux/bitops.h>
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/mm.h>
  22. #include <linux/string.h>
  23. #include <linux/socket.h>
  24. #include <linux/sockios.h>
  25. #include <linux/errno.h>
  26. #include <linux/in.h>
  27. #include <linux/inet.h>
  28. #include <linux/inetdevice.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/if_arp.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/init.h>
  34. #include <linux/slab.h>
  35. #include <net/arp.h>
  36. #include <net/ip.h>
  37. #include <net/protocol.h>
  38. #include <net/route.h>
  39. #include <net/tcp.h>
  40. #include <net/sock.h>
  41. #include <net/ip_fib.h>
  42. #include <net/netlink.h>
  43. #include <net/nexthop.h>
  44. #include "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. /* Release a nexthop info record */
  127. void free_fib_info(struct fib_info *fi)
  128. {
  129. if (fi->fib_dead == 0) {
  130. pr_warning("Freeing alive fib_info %p\n", fi);
  131. return;
  132. }
  133. change_nexthops(fi) {
  134. if (nexthop_nh->nh_dev)
  135. dev_put(nexthop_nh->nh_dev);
  136. nexthop_nh->nh_dev = NULL;
  137. } endfor_nexthops(fi);
  138. fib_info_cnt--;
  139. release_net(fi->fib_net);
  140. kfree_rcu(fi, rcu);
  141. }
  142. void fib_release_info(struct fib_info *fi)
  143. {
  144. spin_lock_bh(&fib_info_lock);
  145. if (fi && --fi->fib_treeref == 0) {
  146. hlist_del(&fi->fib_hash);
  147. if (fi->fib_prefsrc)
  148. hlist_del(&fi->fib_lhash);
  149. change_nexthops(fi) {
  150. if (!nexthop_nh->nh_dev)
  151. continue;
  152. hlist_del(&nexthop_nh->nh_hash);
  153. } endfor_nexthops(fi)
  154. fi->fib_dead = 1;
  155. fib_info_put(fi);
  156. }
  157. spin_unlock_bh(&fib_info_lock);
  158. }
  159. static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
  160. {
  161. const struct fib_nh *onh = ofi->fib_nh;
  162. for_nexthops(fi) {
  163. if (nh->nh_oif != onh->nh_oif ||
  164. nh->nh_gw != onh->nh_gw ||
  165. nh->nh_scope != onh->nh_scope ||
  166. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  167. nh->nh_weight != onh->nh_weight ||
  168. #endif
  169. #ifdef CONFIG_IP_ROUTE_CLASSID
  170. nh->nh_tclassid != onh->nh_tclassid ||
  171. #endif
  172. ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
  173. return -1;
  174. onh++;
  175. } endfor_nexthops(fi);
  176. return 0;
  177. }
  178. static inline unsigned int fib_devindex_hashfn(unsigned int val)
  179. {
  180. unsigned int mask = DEVINDEX_HASHSIZE - 1;
  181. return (val ^
  182. (val >> DEVINDEX_HASHBITS) ^
  183. (val >> (DEVINDEX_HASHBITS * 2))) & mask;
  184. }
  185. static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
  186. {
  187. unsigned int mask = (fib_info_hash_size - 1);
  188. unsigned int val = fi->fib_nhs;
  189. val ^= (fi->fib_protocol << 8) | fi->fib_scope;
  190. val ^= (__force u32)fi->fib_prefsrc;
  191. val ^= fi->fib_priority;
  192. for_nexthops(fi) {
  193. val ^= fib_devindex_hashfn(nh->nh_oif);
  194. } endfor_nexthops(fi)
  195. return (val ^ (val >> 7) ^ (val >> 12)) & mask;
  196. }
  197. static struct fib_info *fib_find_info(const struct fib_info *nfi)
  198. {
  199. struct hlist_head *head;
  200. struct hlist_node *node;
  201. struct fib_info *fi;
  202. unsigned int hash;
  203. hash = fib_info_hashfn(nfi);
  204. head = &fib_info_hash[hash];
  205. hlist_for_each_entry(fi, node, head, fib_hash) {
  206. if (!net_eq(fi->fib_net, nfi->fib_net))
  207. continue;
  208. if (fi->fib_nhs != nfi->fib_nhs)
  209. continue;
  210. if (nfi->fib_protocol == fi->fib_protocol &&
  211. nfi->fib_scope == fi->fib_scope &&
  212. nfi->fib_prefsrc == fi->fib_prefsrc &&
  213. nfi->fib_priority == fi->fib_priority &&
  214. memcmp(nfi->fib_metrics, fi->fib_metrics,
  215. sizeof(u32) * RTAX_MAX) == 0 &&
  216. ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
  217. (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
  218. return fi;
  219. }
  220. return NULL;
  221. }
  222. /* Check, that the gateway is already configured.
  223. * Used only by redirect accept routine.
  224. */
  225. int ip_fib_check_default(__be32 gw, struct net_device *dev)
  226. {
  227. struct hlist_head *head;
  228. struct hlist_node *node;
  229. struct fib_nh *nh;
  230. unsigned int hash;
  231. spin_lock(&fib_info_lock);
  232. hash = fib_devindex_hashfn(dev->ifindex);
  233. head = &fib_info_devhash[hash];
  234. hlist_for_each_entry(nh, node, head, nh_hash) {
  235. if (nh->nh_dev == dev &&
  236. nh->nh_gw == gw &&
  237. !(nh->nh_flags & RTNH_F_DEAD)) {
  238. spin_unlock(&fib_info_lock);
  239. return 0;
  240. }
  241. }
  242. spin_unlock(&fib_info_lock);
  243. return -1;
  244. }
  245. static inline size_t fib_nlmsg_size(struct fib_info *fi)
  246. {
  247. size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
  248. + nla_total_size(4) /* RTA_TABLE */
  249. + nla_total_size(4) /* RTA_DST */
  250. + nla_total_size(4) /* RTA_PRIORITY */
  251. + nla_total_size(4); /* RTA_PREFSRC */
  252. /* space for nested metrics */
  253. payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
  254. if (fi->fib_nhs) {
  255. /* Also handles the special case fib_nhs == 1 */
  256. /* each nexthop is packed in an attribute */
  257. size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
  258. /* may contain flow and gateway attribute */
  259. nhsize += 2 * nla_total_size(4);
  260. /* all nexthops are packed in a nested attribute */
  261. payload += nla_total_size(fi->fib_nhs * nhsize);
  262. }
  263. return payload;
  264. }
  265. void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
  266. int dst_len, u32 tb_id, struct nl_info *info,
  267. unsigned int nlm_flags)
  268. {
  269. struct sk_buff *skb;
  270. u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
  271. int err = -ENOBUFS;
  272. skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
  273. if (skb == NULL)
  274. goto errout;
  275. err = fib_dump_info(skb, info->pid, seq, event, tb_id,
  276. fa->fa_type, key, dst_len,
  277. fa->fa_tos, fa->fa_info, nlm_flags);
  278. if (err < 0) {
  279. /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
  280. WARN_ON(err == -EMSGSIZE);
  281. kfree_skb(skb);
  282. goto errout;
  283. }
  284. rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
  285. info->nlh, GFP_KERNEL);
  286. return;
  287. errout:
  288. if (err < 0)
  289. rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
  290. }
  291. /* Return the first fib alias matching TOS with
  292. * priority less than or equal to PRIO.
  293. */
  294. struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
  295. {
  296. if (fah) {
  297. struct fib_alias *fa;
  298. list_for_each_entry(fa, fah, fa_list) {
  299. if (fa->fa_tos > tos)
  300. continue;
  301. if (fa->fa_info->fib_priority >= prio ||
  302. fa->fa_tos < tos)
  303. return fa;
  304. }
  305. }
  306. return NULL;
  307. }
  308. int fib_detect_death(struct fib_info *fi, int order,
  309. struct fib_info **last_resort, int *last_idx, int dflt)
  310. {
  311. struct neighbour *n;
  312. int state = NUD_NONE;
  313. n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
  314. if (n) {
  315. state = n->nud_state;
  316. neigh_release(n);
  317. }
  318. if (state == NUD_REACHABLE)
  319. return 0;
  320. if ((state & NUD_VALID) && order != dflt)
  321. return 0;
  322. if ((state & NUD_VALID) ||
  323. (*last_idx < 0 && order > dflt)) {
  324. *last_resort = fi;
  325. *last_idx = order;
  326. }
  327. return 1;
  328. }
  329. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  330. static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
  331. {
  332. int nhs = 0;
  333. while (rtnh_ok(rtnh, remaining)) {
  334. nhs++;
  335. rtnh = rtnh_next(rtnh, &remaining);
  336. }
  337. /* leftover implies invalid nexthop configuration, discard it */
  338. return remaining > 0 ? 0 : nhs;
  339. }
  340. static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
  341. int remaining, struct fib_config *cfg)
  342. {
  343. change_nexthops(fi) {
  344. int attrlen;
  345. if (!rtnh_ok(rtnh, remaining))
  346. return -EINVAL;
  347. nexthop_nh->nh_flags =
  348. (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
  349. nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
  350. nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
  351. attrlen = rtnh_attrlen(rtnh);
  352. if (attrlen > 0) {
  353. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  354. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  355. nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
  356. #ifdef CONFIG_IP_ROUTE_CLASSID
  357. nla = nla_find(attrs, attrlen, RTA_FLOW);
  358. nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
  359. #endif
  360. }
  361. rtnh = rtnh_next(rtnh, &remaining);
  362. } endfor_nexthops(fi);
  363. return 0;
  364. }
  365. #endif
  366. int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
  367. {
  368. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  369. struct rtnexthop *rtnh;
  370. int remaining;
  371. #endif
  372. if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
  373. return 1;
  374. if (cfg->fc_oif || cfg->fc_gw) {
  375. if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
  376. (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
  377. return 0;
  378. return 1;
  379. }
  380. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  381. if (cfg->fc_mp == NULL)
  382. return 0;
  383. rtnh = cfg->fc_mp;
  384. remaining = cfg->fc_mp_len;
  385. for_nexthops(fi) {
  386. int attrlen;
  387. if (!rtnh_ok(rtnh, remaining))
  388. return -EINVAL;
  389. if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
  390. return 1;
  391. attrlen = rtnh_attrlen(rtnh);
  392. if (attrlen < 0) {
  393. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  394. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  395. if (nla && nla_get_be32(nla) != nh->nh_gw)
  396. return 1;
  397. #ifdef CONFIG_IP_ROUTE_CLASSID
  398. nla = nla_find(attrs, attrlen, RTA_FLOW);
  399. if (nla && nla_get_u32(nla) != nh->nh_tclassid)
  400. return 1;
  401. #endif
  402. }
  403. rtnh = rtnh_next(rtnh, &remaining);
  404. } endfor_nexthops(fi);
  405. #endif
  406. return 0;
  407. }
  408. /*
  409. * Picture
  410. * -------
  411. *
  412. * Semantics of nexthop is very messy by historical reasons.
  413. * We have to take into account, that:
  414. * a) gateway can be actually local interface address,
  415. * so that gatewayed route is direct.
  416. * b) gateway must be on-link address, possibly
  417. * described not by an ifaddr, but also by a direct route.
  418. * c) If both gateway and interface are specified, they should not
  419. * contradict.
  420. * d) If we use tunnel routes, gateway could be not on-link.
  421. *
  422. * Attempt to reconcile all of these (alas, self-contradictory) conditions
  423. * results in pretty ugly and hairy code with obscure logic.
  424. *
  425. * I chose to generalized it instead, so that the size
  426. * of code does not increase practically, but it becomes
  427. * much more general.
  428. * Every prefix is assigned a "scope" value: "host" is local address,
  429. * "link" is direct route,
  430. * [ ... "site" ... "interior" ... ]
  431. * and "universe" is true gateway route with global meaning.
  432. *
  433. * Every prefix refers to a set of "nexthop"s (gw, oif),
  434. * where gw must have narrower scope. This recursion stops
  435. * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
  436. * which means that gw is forced to be on link.
  437. *
  438. * Code is still hairy, but now it is apparently logically
  439. * consistent and very flexible. F.e. as by-product it allows
  440. * to co-exists in peace independent exterior and interior
  441. * routing processes.
  442. *
  443. * Normally it looks as following.
  444. *
  445. * {universe prefix} -> (gw, oif) [scope link]
  446. * |
  447. * |-> {link prefix} -> (gw, oif) [scope local]
  448. * |
  449. * |-> {local prefix} (terminal node)
  450. */
  451. static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
  452. struct fib_nh *nh)
  453. {
  454. int err;
  455. struct net *net;
  456. struct net_device *dev;
  457. net = cfg->fc_nlinfo.nl_net;
  458. if (nh->nh_gw) {
  459. struct fib_result res;
  460. if (nh->nh_flags & RTNH_F_ONLINK) {
  461. if (cfg->fc_scope >= RT_SCOPE_LINK)
  462. return -EINVAL;
  463. if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
  464. return -EINVAL;
  465. dev = __dev_get_by_index(net, nh->nh_oif);
  466. if (!dev)
  467. return -ENODEV;
  468. if (!(dev->flags & IFF_UP))
  469. return -ENETDOWN;
  470. nh->nh_dev = dev;
  471. dev_hold(dev);
  472. nh->nh_scope = RT_SCOPE_LINK;
  473. return 0;
  474. }
  475. rcu_read_lock();
  476. {
  477. struct flowi4 fl4 = {
  478. .daddr = nh->nh_gw,
  479. .flowi4_scope = cfg->fc_scope + 1,
  480. .flowi4_oif = nh->nh_oif,
  481. };
  482. /* It is not necessary, but requires a bit of thinking */
  483. if (fl4.flowi4_scope < RT_SCOPE_LINK)
  484. fl4.flowi4_scope = RT_SCOPE_LINK;
  485. err = fib_lookup(net, &fl4, &res);
  486. if (err) {
  487. rcu_read_unlock();
  488. return err;
  489. }
  490. }
  491. err = -EINVAL;
  492. if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
  493. goto out;
  494. nh->nh_scope = res.scope;
  495. nh->nh_oif = FIB_RES_OIF(res);
  496. nh->nh_dev = dev = FIB_RES_DEV(res);
  497. if (!dev)
  498. goto out;
  499. dev_hold(dev);
  500. err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
  501. } else {
  502. struct in_device *in_dev;
  503. if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
  504. return -EINVAL;
  505. rcu_read_lock();
  506. err = -ENODEV;
  507. in_dev = inetdev_by_index(net, nh->nh_oif);
  508. if (in_dev == NULL)
  509. goto out;
  510. err = -ENETDOWN;
  511. if (!(in_dev->dev->flags & IFF_UP))
  512. goto out;
  513. nh->nh_dev = in_dev->dev;
  514. dev_hold(nh->nh_dev);
  515. nh->nh_scope = RT_SCOPE_HOST;
  516. err = 0;
  517. }
  518. out:
  519. rcu_read_unlock();
  520. return err;
  521. }
  522. static inline unsigned int fib_laddr_hashfn(__be32 val)
  523. {
  524. unsigned int mask = (fib_info_hash_size - 1);
  525. return ((__force u32)val ^
  526. ((__force u32)val >> 7) ^
  527. ((__force u32)val >> 14)) & mask;
  528. }
  529. static struct hlist_head *fib_info_hash_alloc(int bytes)
  530. {
  531. if (bytes <= PAGE_SIZE)
  532. return kzalloc(bytes, GFP_KERNEL);
  533. else
  534. return (struct hlist_head *)
  535. __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  536. get_order(bytes));
  537. }
  538. static void fib_info_hash_free(struct hlist_head *hash, int bytes)
  539. {
  540. if (!hash)
  541. return;
  542. if (bytes <= PAGE_SIZE)
  543. kfree(hash);
  544. else
  545. free_pages((unsigned long) hash, get_order(bytes));
  546. }
  547. static void fib_info_hash_move(struct hlist_head *new_info_hash,
  548. struct hlist_head *new_laddrhash,
  549. unsigned int new_size)
  550. {
  551. struct hlist_head *old_info_hash, *old_laddrhash;
  552. unsigned int old_size = fib_info_hash_size;
  553. unsigned int i, bytes;
  554. spin_lock_bh(&fib_info_lock);
  555. old_info_hash = fib_info_hash;
  556. old_laddrhash = fib_info_laddrhash;
  557. fib_info_hash_size = new_size;
  558. for (i = 0; i < old_size; i++) {
  559. struct hlist_head *head = &fib_info_hash[i];
  560. struct hlist_node *node, *n;
  561. struct fib_info *fi;
  562. hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
  563. struct hlist_head *dest;
  564. unsigned int new_hash;
  565. hlist_del(&fi->fib_hash);
  566. new_hash = fib_info_hashfn(fi);
  567. dest = &new_info_hash[new_hash];
  568. hlist_add_head(&fi->fib_hash, dest);
  569. }
  570. }
  571. fib_info_hash = new_info_hash;
  572. for (i = 0; i < old_size; i++) {
  573. struct hlist_head *lhead = &fib_info_laddrhash[i];
  574. struct hlist_node *node, *n;
  575. struct fib_info *fi;
  576. hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
  577. struct hlist_head *ldest;
  578. unsigned int new_hash;
  579. hlist_del(&fi->fib_lhash);
  580. new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
  581. ldest = &new_laddrhash[new_hash];
  582. hlist_add_head(&fi->fib_lhash, ldest);
  583. }
  584. }
  585. fib_info_laddrhash = new_laddrhash;
  586. spin_unlock_bh(&fib_info_lock);
  587. bytes = old_size * sizeof(struct hlist_head *);
  588. fib_info_hash_free(old_info_hash, bytes);
  589. fib_info_hash_free(old_laddrhash, bytes);
  590. }
  591. __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
  592. {
  593. nh->nh_saddr = inet_select_addr(nh->nh_dev,
  594. nh->nh_gw,
  595. nh->nh_parent->fib_scope);
  596. nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
  597. return nh->nh_saddr;
  598. }
  599. struct fib_info *fib_create_info(struct fib_config *cfg)
  600. {
  601. int err;
  602. struct fib_info *fi = NULL;
  603. struct fib_info *ofi;
  604. int nhs = 1;
  605. struct net *net = cfg->fc_nlinfo.nl_net;
  606. if (cfg->fc_type > RTN_MAX)
  607. goto err_inval;
  608. /* Fast check to catch the most weird cases */
  609. if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
  610. goto err_inval;
  611. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  612. if (cfg->fc_mp) {
  613. nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
  614. if (nhs == 0)
  615. goto err_inval;
  616. }
  617. #endif
  618. err = -ENOBUFS;
  619. if (fib_info_cnt >= fib_info_hash_size) {
  620. unsigned int new_size = fib_info_hash_size << 1;
  621. struct hlist_head *new_info_hash;
  622. struct hlist_head *new_laddrhash;
  623. unsigned int bytes;
  624. if (!new_size)
  625. new_size = 1;
  626. bytes = new_size * sizeof(struct hlist_head *);
  627. new_info_hash = fib_info_hash_alloc(bytes);
  628. new_laddrhash = fib_info_hash_alloc(bytes);
  629. if (!new_info_hash || !new_laddrhash) {
  630. fib_info_hash_free(new_info_hash, bytes);
  631. fib_info_hash_free(new_laddrhash, bytes);
  632. } else
  633. fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
  634. if (!fib_info_hash_size)
  635. goto failure;
  636. }
  637. fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
  638. if (fi == NULL)
  639. goto failure;
  640. if (cfg->fc_mx) {
  641. fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
  642. if (!fi->fib_metrics)
  643. goto failure;
  644. } else
  645. fi->fib_metrics = (u32 *) dst_default_metrics;
  646. fib_info_cnt++;
  647. fi->fib_net = hold_net(net);
  648. fi->fib_protocol = cfg->fc_protocol;
  649. fi->fib_scope = cfg->fc_scope;
  650. fi->fib_flags = cfg->fc_flags;
  651. fi->fib_priority = cfg->fc_priority;
  652. fi->fib_prefsrc = cfg->fc_prefsrc;
  653. fi->fib_nhs = nhs;
  654. change_nexthops(fi) {
  655. nexthop_nh->nh_parent = fi;
  656. } endfor_nexthops(fi)
  657. if (cfg->fc_mx) {
  658. struct nlattr *nla;
  659. int remaining;
  660. nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
  661. int type = nla_type(nla);
  662. if (type) {
  663. if (type > RTAX_MAX)
  664. goto err_inval;
  665. fi->fib_metrics[type - 1] = nla_get_u32(nla);
  666. }
  667. }
  668. }
  669. if (cfg->fc_mp) {
  670. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  671. err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
  672. if (err != 0)
  673. goto failure;
  674. if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
  675. goto err_inval;
  676. if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
  677. goto err_inval;
  678. #ifdef CONFIG_IP_ROUTE_CLASSID
  679. if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
  680. goto err_inval;
  681. #endif
  682. #else
  683. goto err_inval;
  684. #endif
  685. } else {
  686. struct fib_nh *nh = fi->fib_nh;
  687. nh->nh_oif = cfg->fc_oif;
  688. nh->nh_gw = cfg->fc_gw;
  689. nh->nh_flags = cfg->fc_flags;
  690. #ifdef CONFIG_IP_ROUTE_CLASSID
  691. nh->nh_tclassid = cfg->fc_flow;
  692. #endif
  693. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  694. nh->nh_weight = 1;
  695. #endif
  696. }
  697. if (fib_props[cfg->fc_type].error) {
  698. if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
  699. goto err_inval;
  700. goto link_it;
  701. } else {
  702. switch (cfg->fc_type) {
  703. case RTN_UNICAST:
  704. case RTN_LOCAL:
  705. case RTN_BROADCAST:
  706. case RTN_ANYCAST:
  707. case RTN_MULTICAST:
  708. break;
  709. default:
  710. goto err_inval;
  711. }
  712. }
  713. if (cfg->fc_scope > RT_SCOPE_HOST)
  714. goto err_inval;
  715. if (cfg->fc_scope == RT_SCOPE_HOST) {
  716. struct fib_nh *nh = fi->fib_nh;
  717. /* Local address is added. */
  718. if (nhs != 1 || nh->nh_gw)
  719. goto err_inval;
  720. nh->nh_scope = RT_SCOPE_NOWHERE;
  721. nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
  722. err = -ENODEV;
  723. if (nh->nh_dev == NULL)
  724. goto failure;
  725. } else {
  726. change_nexthops(fi) {
  727. err = fib_check_nh(cfg, fi, nexthop_nh);
  728. if (err != 0)
  729. goto failure;
  730. } endfor_nexthops(fi)
  731. }
  732. if (fi->fib_prefsrc) {
  733. if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
  734. fi->fib_prefsrc != cfg->fc_dst)
  735. if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
  736. goto err_inval;
  737. }
  738. change_nexthops(fi) {
  739. fib_info_update_nh_saddr(net, nexthop_nh);
  740. } endfor_nexthops(fi)
  741. link_it:
  742. ofi = fib_find_info(fi);
  743. if (ofi) {
  744. fi->fib_dead = 1;
  745. free_fib_info(fi);
  746. ofi->fib_treeref++;
  747. return ofi;
  748. }
  749. fi->fib_treeref++;
  750. atomic_inc(&fi->fib_clntref);
  751. spin_lock_bh(&fib_info_lock);
  752. hlist_add_head(&fi->fib_hash,
  753. &fib_info_hash[fib_info_hashfn(fi)]);
  754. if (fi->fib_prefsrc) {
  755. struct hlist_head *head;
  756. head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
  757. hlist_add_head(&fi->fib_lhash, head);
  758. }
  759. change_nexthops(fi) {
  760. struct hlist_head *head;
  761. unsigned int hash;
  762. if (!nexthop_nh->nh_dev)
  763. continue;
  764. hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
  765. head = &fib_info_devhash[hash];
  766. hlist_add_head(&nexthop_nh->nh_hash, head);
  767. } endfor_nexthops(fi)
  768. spin_unlock_bh(&fib_info_lock);
  769. return fi;
  770. err_inval:
  771. err = -EINVAL;
  772. failure:
  773. if (fi) {
  774. fi->fib_dead = 1;
  775. free_fib_info(fi);
  776. }
  777. return ERR_PTR(err);
  778. }
  779. int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
  780. u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
  781. struct fib_info *fi, unsigned int flags)
  782. {
  783. struct nlmsghdr *nlh;
  784. struct rtmsg *rtm;
  785. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
  786. if (nlh == NULL)
  787. return -EMSGSIZE;
  788. rtm = nlmsg_data(nlh);
  789. rtm->rtm_family = AF_INET;
  790. rtm->rtm_dst_len = dst_len;
  791. rtm->rtm_src_len = 0;
  792. rtm->rtm_tos = tos;
  793. if (tb_id < 256)
  794. rtm->rtm_table = tb_id;
  795. else
  796. rtm->rtm_table = RT_TABLE_COMPAT;
  797. NLA_PUT_U32(skb, RTA_TABLE, tb_id);
  798. rtm->rtm_type = type;
  799. rtm->rtm_flags = fi->fib_flags;
  800. rtm->rtm_scope = fi->fib_scope;
  801. rtm->rtm_protocol = fi->fib_protocol;
  802. if (rtm->rtm_dst_len)
  803. NLA_PUT_BE32(skb, RTA_DST, dst);
  804. if (fi->fib_priority)
  805. NLA_PUT_U32(skb, RTA_PRIORITY, fi->fib_priority);
  806. if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
  807. goto nla_put_failure;
  808. if (fi->fib_prefsrc)
  809. NLA_PUT_BE32(skb, RTA_PREFSRC, fi->fib_prefsrc);
  810. if (fi->fib_nhs == 1) {
  811. if (fi->fib_nh->nh_gw)
  812. NLA_PUT_BE32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw);
  813. if (fi->fib_nh->nh_oif)
  814. NLA_PUT_U32(skb, RTA_OIF, fi->fib_nh->nh_oif);
  815. #ifdef CONFIG_IP_ROUTE_CLASSID
  816. if (fi->fib_nh[0].nh_tclassid)
  817. NLA_PUT_U32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid);
  818. #endif
  819. }
  820. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  821. if (fi->fib_nhs > 1) {
  822. struct rtnexthop *rtnh;
  823. struct nlattr *mp;
  824. mp = nla_nest_start(skb, RTA_MULTIPATH);
  825. if (mp == NULL)
  826. goto nla_put_failure;
  827. for_nexthops(fi) {
  828. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  829. if (rtnh == NULL)
  830. goto nla_put_failure;
  831. rtnh->rtnh_flags = nh->nh_flags & 0xFF;
  832. rtnh->rtnh_hops = nh->nh_weight - 1;
  833. rtnh->rtnh_ifindex = nh->nh_oif;
  834. if (nh->nh_gw)
  835. NLA_PUT_BE32(skb, RTA_GATEWAY, nh->nh_gw);
  836. #ifdef CONFIG_IP_ROUTE_CLASSID
  837. if (nh->nh_tclassid)
  838. NLA_PUT_U32(skb, RTA_FLOW, nh->nh_tclassid);
  839. #endif
  840. /* length of rtnetlink header + attributes */
  841. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
  842. } endfor_nexthops(fi);
  843. nla_nest_end(skb, mp);
  844. }
  845. #endif
  846. return nlmsg_end(skb, nlh);
  847. nla_put_failure:
  848. nlmsg_cancel(skb, nlh);
  849. return -EMSGSIZE;
  850. }
  851. /*
  852. * Update FIB if:
  853. * - local address disappeared -> we must delete all the entries
  854. * referring to it.
  855. * - device went down -> we must shutdown all nexthops going via it.
  856. */
  857. int fib_sync_down_addr(struct net *net, __be32 local)
  858. {
  859. int ret = 0;
  860. unsigned int hash = fib_laddr_hashfn(local);
  861. struct hlist_head *head = &fib_info_laddrhash[hash];
  862. struct hlist_node *node;
  863. struct fib_info *fi;
  864. if (fib_info_laddrhash == NULL || local == 0)
  865. return 0;
  866. hlist_for_each_entry(fi, node, head, fib_lhash) {
  867. if (!net_eq(fi->fib_net, net))
  868. continue;
  869. if (fi->fib_prefsrc == local) {
  870. fi->fib_flags |= RTNH_F_DEAD;
  871. ret++;
  872. }
  873. }
  874. return ret;
  875. }
  876. int fib_sync_down_dev(struct net_device *dev, int force)
  877. {
  878. int ret = 0;
  879. int scope = RT_SCOPE_NOWHERE;
  880. struct fib_info *prev_fi = NULL;
  881. unsigned int hash = fib_devindex_hashfn(dev->ifindex);
  882. struct hlist_head *head = &fib_info_devhash[hash];
  883. struct hlist_node *node;
  884. struct fib_nh *nh;
  885. if (force)
  886. scope = -1;
  887. hlist_for_each_entry(nh, node, head, nh_hash) {
  888. struct fib_info *fi = nh->nh_parent;
  889. int dead;
  890. BUG_ON(!fi->fib_nhs);
  891. if (nh->nh_dev != dev || fi == prev_fi)
  892. continue;
  893. prev_fi = fi;
  894. dead = 0;
  895. change_nexthops(fi) {
  896. if (nexthop_nh->nh_flags & RTNH_F_DEAD)
  897. dead++;
  898. else if (nexthop_nh->nh_dev == dev &&
  899. nexthop_nh->nh_scope != scope) {
  900. nexthop_nh->nh_flags |= RTNH_F_DEAD;
  901. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  902. spin_lock_bh(&fib_multipath_lock);
  903. fi->fib_power -= nexthop_nh->nh_power;
  904. nexthop_nh->nh_power = 0;
  905. spin_unlock_bh(&fib_multipath_lock);
  906. #endif
  907. dead++;
  908. }
  909. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  910. if (force > 1 && nexthop_nh->nh_dev == dev) {
  911. dead = fi->fib_nhs;
  912. break;
  913. }
  914. #endif
  915. } endfor_nexthops(fi)
  916. if (dead == fi->fib_nhs) {
  917. fi->fib_flags |= RTNH_F_DEAD;
  918. ret++;
  919. }
  920. }
  921. return ret;
  922. }
  923. /* Must be invoked inside of an RCU protected region. */
  924. void fib_select_default(struct fib_result *res)
  925. {
  926. struct fib_info *fi = NULL, *last_resort = NULL;
  927. struct list_head *fa_head = res->fa_head;
  928. struct fib_table *tb = res->table;
  929. int order = -1, last_idx = -1;
  930. struct fib_alias *fa;
  931. list_for_each_entry_rcu(fa, fa_head, fa_list) {
  932. struct fib_info *next_fi = fa->fa_info;
  933. if (next_fi->fib_scope != res->scope ||
  934. fa->fa_type != RTN_UNICAST)
  935. continue;
  936. if (next_fi->fib_priority > res->fi->fib_priority)
  937. break;
  938. if (!next_fi->fib_nh[0].nh_gw ||
  939. next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
  940. continue;
  941. fib_alias_accessed(fa);
  942. if (fi == NULL) {
  943. if (next_fi != res->fi)
  944. break;
  945. } else if (!fib_detect_death(fi, order, &last_resort,
  946. &last_idx, tb->tb_default)) {
  947. fib_result_assign(res, fi);
  948. tb->tb_default = order;
  949. goto out;
  950. }
  951. fi = next_fi;
  952. order++;
  953. }
  954. if (order <= 0 || fi == NULL) {
  955. tb->tb_default = -1;
  956. goto out;
  957. }
  958. if (!fib_detect_death(fi, order, &last_resort, &last_idx,
  959. tb->tb_default)) {
  960. fib_result_assign(res, fi);
  961. tb->tb_default = order;
  962. goto out;
  963. }
  964. if (last_idx >= 0)
  965. fib_result_assign(res, last_resort);
  966. tb->tb_default = last_idx;
  967. out:
  968. return;
  969. }
  970. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  971. /*
  972. * Dead device goes up. We wake up dead nexthops.
  973. * It takes sense only on multipath routes.
  974. */
  975. int fib_sync_up(struct net_device *dev)
  976. {
  977. struct fib_info *prev_fi;
  978. unsigned int hash;
  979. struct hlist_head *head;
  980. struct hlist_node *node;
  981. struct fib_nh *nh;
  982. int ret;
  983. if (!(dev->flags & IFF_UP))
  984. return 0;
  985. prev_fi = NULL;
  986. hash = fib_devindex_hashfn(dev->ifindex);
  987. head = &fib_info_devhash[hash];
  988. ret = 0;
  989. hlist_for_each_entry(nh, node, head, nh_hash) {
  990. struct fib_info *fi = nh->nh_parent;
  991. int alive;
  992. BUG_ON(!fi->fib_nhs);
  993. if (nh->nh_dev != dev || fi == prev_fi)
  994. continue;
  995. prev_fi = fi;
  996. alive = 0;
  997. change_nexthops(fi) {
  998. if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
  999. alive++;
  1000. continue;
  1001. }
  1002. if (nexthop_nh->nh_dev == NULL ||
  1003. !(nexthop_nh->nh_dev->flags & IFF_UP))
  1004. continue;
  1005. if (nexthop_nh->nh_dev != dev ||
  1006. !__in_dev_get_rtnl(dev))
  1007. continue;
  1008. alive++;
  1009. spin_lock_bh(&fib_multipath_lock);
  1010. nexthop_nh->nh_power = 0;
  1011. nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
  1012. spin_unlock_bh(&fib_multipath_lock);
  1013. } endfor_nexthops(fi)
  1014. if (alive > 0) {
  1015. fi->fib_flags &= ~RTNH_F_DEAD;
  1016. ret++;
  1017. }
  1018. }
  1019. return ret;
  1020. }
  1021. /*
  1022. * The algorithm is suboptimal, but it provides really
  1023. * fair weighted route distribution.
  1024. */
  1025. void fib_select_multipath(struct fib_result *res)
  1026. {
  1027. struct fib_info *fi = res->fi;
  1028. int w;
  1029. spin_lock_bh(&fib_multipath_lock);
  1030. if (fi->fib_power <= 0) {
  1031. int power = 0;
  1032. change_nexthops(fi) {
  1033. if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
  1034. power += nexthop_nh->nh_weight;
  1035. nexthop_nh->nh_power = nexthop_nh->nh_weight;
  1036. }
  1037. } endfor_nexthops(fi);
  1038. fi->fib_power = power;
  1039. if (power <= 0) {
  1040. spin_unlock_bh(&fib_multipath_lock);
  1041. /* Race condition: route has just become dead. */
  1042. res->nh_sel = 0;
  1043. return;
  1044. }
  1045. }
  1046. /* w should be random number [0..fi->fib_power-1],
  1047. * it is pretty bad approximation.
  1048. */
  1049. w = jiffies % fi->fib_power;
  1050. change_nexthops(fi) {
  1051. if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
  1052. nexthop_nh->nh_power) {
  1053. w -= nexthop_nh->nh_power;
  1054. if (w <= 0) {
  1055. nexthop_nh->nh_power--;
  1056. fi->fib_power--;
  1057. res->nh_sel = nhsel;
  1058. spin_unlock_bh(&fib_multipath_lock);
  1059. return;
  1060. }
  1061. }
  1062. } endfor_nexthops(fi);
  1063. /* Race condition: route has just become dead. */
  1064. res->nh_sel = 0;
  1065. spin_unlock_bh(&fib_multipath_lock);
  1066. }
  1067. #endif