l3mdev.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * net/l3mdev/l3mdev.c - L3 master device implementation
  3. * Copyright (c) 2015 Cumulus Networks
  4. * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/netdevice.h>
  12. #include <net/fib_rules.h>
  13. #include <net/l3mdev.h>
  14. /**
  15. * l3mdev_master_ifindex - get index of L3 master device
  16. * @dev: targeted interface
  17. */
  18. int l3mdev_master_ifindex_rcu(const struct net_device *dev)
  19. {
  20. int ifindex = 0;
  21. if (!dev)
  22. return 0;
  23. if (netif_is_l3_master(dev)) {
  24. ifindex = dev->ifindex;
  25. } else if (netif_is_l3_slave(dev)) {
  26. struct net_device *master;
  27. struct net_device *_dev = (struct net_device *)dev;
  28. /* netdev_master_upper_dev_get_rcu calls
  29. * list_first_or_null_rcu to walk the upper dev list.
  30. * list_first_or_null_rcu does not handle a const arg. We aren't
  31. * making changes, just want the master device from that list so
  32. * typecast to remove the const
  33. */
  34. master = netdev_master_upper_dev_get_rcu(_dev);
  35. if (master)
  36. ifindex = master->ifindex;
  37. }
  38. return ifindex;
  39. }
  40. EXPORT_SYMBOL_GPL(l3mdev_master_ifindex_rcu);
  41. /**
  42. * l3mdev_fib_table - get FIB table id associated with an L3
  43. * master interface
  44. * @dev: targeted interface
  45. */
  46. u32 l3mdev_fib_table_rcu(const struct net_device *dev)
  47. {
  48. u32 tb_id = 0;
  49. if (!dev)
  50. return 0;
  51. if (netif_is_l3_master(dev)) {
  52. if (dev->l3mdev_ops->l3mdev_fib_table)
  53. tb_id = dev->l3mdev_ops->l3mdev_fib_table(dev);
  54. } else if (netif_is_l3_slave(dev)) {
  55. /* Users of netdev_master_upper_dev_get_rcu need non-const,
  56. * but current inet_*type functions take a const
  57. */
  58. struct net_device *_dev = (struct net_device *) dev;
  59. const struct net_device *master;
  60. master = netdev_master_upper_dev_get_rcu(_dev);
  61. if (master &&
  62. master->l3mdev_ops->l3mdev_fib_table)
  63. tb_id = master->l3mdev_ops->l3mdev_fib_table(master);
  64. }
  65. return tb_id;
  66. }
  67. EXPORT_SYMBOL_GPL(l3mdev_fib_table_rcu);
  68. u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
  69. {
  70. struct net_device *dev;
  71. u32 tb_id = 0;
  72. if (!ifindex)
  73. return 0;
  74. rcu_read_lock();
  75. dev = dev_get_by_index_rcu(net, ifindex);
  76. if (dev)
  77. tb_id = l3mdev_fib_table_rcu(dev);
  78. rcu_read_unlock();
  79. return tb_id;
  80. }
  81. EXPORT_SYMBOL_GPL(l3mdev_fib_table_by_index);
  82. /**
  83. * l3mdev_link_scope_lookup - IPv6 route lookup based on flow for link
  84. * local and multicast addresses
  85. * @net: network namespace for device index lookup
  86. * @fl6: IPv6 flow struct for lookup
  87. */
  88. struct dst_entry *l3mdev_link_scope_lookup(struct net *net,
  89. struct flowi6 *fl6)
  90. {
  91. struct dst_entry *dst = NULL;
  92. struct net_device *dev;
  93. if (fl6->flowi6_oif) {
  94. rcu_read_lock();
  95. dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
  96. if (dev && netif_is_l3_slave(dev))
  97. dev = netdev_master_upper_dev_get_rcu(dev);
  98. if (dev && netif_is_l3_master(dev) &&
  99. dev->l3mdev_ops->l3mdev_link_scope_lookup)
  100. dst = dev->l3mdev_ops->l3mdev_link_scope_lookup(dev, fl6);
  101. rcu_read_unlock();
  102. }
  103. return dst;
  104. }
  105. EXPORT_SYMBOL_GPL(l3mdev_link_scope_lookup);
  106. /**
  107. * l3mdev_fib_rule_match - Determine if flowi references an
  108. * L3 master device
  109. * @net: network namespace for device index lookup
  110. * @fl: flow struct
  111. */
  112. int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
  113. struct fib_lookup_arg *arg)
  114. {
  115. struct net_device *dev;
  116. int rc = 0;
  117. rcu_read_lock();
  118. dev = dev_get_by_index_rcu(net, fl->flowi_oif);
  119. if (dev && netif_is_l3_master(dev) &&
  120. dev->l3mdev_ops->l3mdev_fib_table) {
  121. arg->table = dev->l3mdev_ops->l3mdev_fib_table(dev);
  122. rc = 1;
  123. goto out;
  124. }
  125. dev = dev_get_by_index_rcu(net, fl->flowi_iif);
  126. if (dev && netif_is_l3_master(dev) &&
  127. dev->l3mdev_ops->l3mdev_fib_table) {
  128. arg->table = dev->l3mdev_ops->l3mdev_fib_table(dev);
  129. rc = 1;
  130. goto out;
  131. }
  132. out:
  133. rcu_read_unlock();
  134. return rc;
  135. }
  136. void l3mdev_update_flow(struct net *net, struct flowi *fl)
  137. {
  138. struct net_device *dev;
  139. int ifindex;
  140. rcu_read_lock();
  141. if (fl->flowi_oif) {
  142. dev = dev_get_by_index_rcu(net, fl->flowi_oif);
  143. if (dev) {
  144. ifindex = l3mdev_master_ifindex_rcu(dev);
  145. if (ifindex) {
  146. fl->flowi_oif = ifindex;
  147. fl->flowi_flags |= FLOWI_FLAG_SKIP_NH_OIF;
  148. goto out;
  149. }
  150. }
  151. }
  152. if (fl->flowi_iif) {
  153. dev = dev_get_by_index_rcu(net, fl->flowi_iif);
  154. if (dev) {
  155. ifindex = l3mdev_master_ifindex_rcu(dev);
  156. if (ifindex) {
  157. fl->flowi_iif = ifindex;
  158. fl->flowi_flags |= FLOWI_FLAG_SKIP_NH_OIF;
  159. }
  160. }
  161. }
  162. out:
  163. rcu_read_unlock();
  164. }
  165. EXPORT_SYMBOL_GPL(l3mdev_update_flow);