l3mdev.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * include/net/l3mdev.h - L3 master device API
  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. #ifndef _NET_L3MDEV_H_
  12. #define _NET_L3MDEV_H_
  13. #include <net/dst.h>
  14. #include <net/fib_rules.h>
  15. /**
  16. * struct l3mdev_ops - l3mdev operations
  17. *
  18. * @l3mdev_fib_table: Get FIB table id to use for lookups
  19. *
  20. * @l3mdev_l3_rcv: Hook in L3 receive path
  21. *
  22. * @l3mdev_l3_out: Hook in L3 output path
  23. *
  24. * @l3mdev_link_scope_lookup: IPv6 lookup for linklocal and mcast destinations
  25. */
  26. struct l3mdev_ops {
  27. u32 (*l3mdev_fib_table)(const struct net_device *dev);
  28. struct sk_buff * (*l3mdev_l3_rcv)(struct net_device *dev,
  29. struct sk_buff *skb, u16 proto);
  30. struct sk_buff * (*l3mdev_l3_out)(struct net_device *dev,
  31. struct sock *sk, struct sk_buff *skb,
  32. u16 proto);
  33. /* IPv6 ops */
  34. struct dst_entry * (*l3mdev_link_scope_lookup)(const struct net_device *dev,
  35. struct flowi6 *fl6);
  36. };
  37. #ifdef CONFIG_NET_L3_MASTER_DEV
  38. int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
  39. struct fib_lookup_arg *arg);
  40. void l3mdev_update_flow(struct net *net, struct flowi *fl);
  41. int l3mdev_master_ifindex_rcu(const struct net_device *dev);
  42. static inline int l3mdev_master_ifindex(struct net_device *dev)
  43. {
  44. int ifindex;
  45. rcu_read_lock();
  46. ifindex = l3mdev_master_ifindex_rcu(dev);
  47. rcu_read_unlock();
  48. return ifindex;
  49. }
  50. static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
  51. {
  52. struct net_device *dev;
  53. int rc = 0;
  54. if (likely(ifindex)) {
  55. rcu_read_lock();
  56. dev = dev_get_by_index_rcu(net, ifindex);
  57. if (dev)
  58. rc = l3mdev_master_ifindex_rcu(dev);
  59. rcu_read_unlock();
  60. }
  61. return rc;
  62. }
  63. static inline
  64. struct net_device *l3mdev_master_dev_rcu(const struct net_device *_dev)
  65. {
  66. /* netdev_master_upper_dev_get_rcu calls
  67. * list_first_or_null_rcu to walk the upper dev list.
  68. * list_first_or_null_rcu does not handle a const arg. We aren't
  69. * making changes, just want the master device from that list so
  70. * typecast to remove the const
  71. */
  72. struct net_device *dev = (struct net_device *)_dev;
  73. struct net_device *master;
  74. if (!dev)
  75. return NULL;
  76. if (netif_is_l3_master(dev))
  77. master = dev;
  78. else if (netif_is_l3_slave(dev))
  79. master = netdev_master_upper_dev_get_rcu(dev);
  80. else
  81. master = NULL;
  82. return master;
  83. }
  84. u32 l3mdev_fib_table_rcu(const struct net_device *dev);
  85. u32 l3mdev_fib_table_by_index(struct net *net, int ifindex);
  86. static inline u32 l3mdev_fib_table(const struct net_device *dev)
  87. {
  88. u32 tb_id;
  89. rcu_read_lock();
  90. tb_id = l3mdev_fib_table_rcu(dev);
  91. rcu_read_unlock();
  92. return tb_id;
  93. }
  94. static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
  95. {
  96. struct net_device *dev;
  97. bool rc = false;
  98. if (ifindex == 0)
  99. return false;
  100. rcu_read_lock();
  101. dev = dev_get_by_index_rcu(net, ifindex);
  102. if (dev)
  103. rc = netif_is_l3_master(dev);
  104. rcu_read_unlock();
  105. return rc;
  106. }
  107. struct dst_entry *l3mdev_link_scope_lookup(struct net *net, struct flowi6 *fl6);
  108. static inline
  109. struct sk_buff *l3mdev_l3_rcv(struct sk_buff *skb, u16 proto)
  110. {
  111. struct net_device *master = NULL;
  112. if (netif_is_l3_slave(skb->dev))
  113. master = netdev_master_upper_dev_get_rcu(skb->dev);
  114. else if (netif_is_l3_master(skb->dev) ||
  115. netif_has_l3_rx_handler(skb->dev))
  116. master = skb->dev;
  117. if (master && master->l3mdev_ops->l3mdev_l3_rcv)
  118. skb = master->l3mdev_ops->l3mdev_l3_rcv(master, skb, proto);
  119. return skb;
  120. }
  121. static inline
  122. struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
  123. {
  124. return l3mdev_l3_rcv(skb, AF_INET);
  125. }
  126. static inline
  127. struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
  128. {
  129. return l3mdev_l3_rcv(skb, AF_INET6);
  130. }
  131. static inline
  132. struct sk_buff *l3mdev_l3_out(struct sock *sk, struct sk_buff *skb, u16 proto)
  133. {
  134. struct net_device *dev = skb_dst(skb)->dev;
  135. if (netif_is_l3_slave(dev)) {
  136. struct net_device *master;
  137. master = netdev_master_upper_dev_get_rcu(dev);
  138. if (master && master->l3mdev_ops->l3mdev_l3_out)
  139. skb = master->l3mdev_ops->l3mdev_l3_out(master, sk,
  140. skb, proto);
  141. }
  142. return skb;
  143. }
  144. static inline
  145. struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
  146. {
  147. return l3mdev_l3_out(sk, skb, AF_INET);
  148. }
  149. static inline
  150. struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
  151. {
  152. return l3mdev_l3_out(sk, skb, AF_INET6);
  153. }
  154. #else
  155. static inline int l3mdev_master_ifindex_rcu(const struct net_device *dev)
  156. {
  157. return 0;
  158. }
  159. static inline int l3mdev_master_ifindex(struct net_device *dev)
  160. {
  161. return 0;
  162. }
  163. static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
  164. {
  165. return 0;
  166. }
  167. static inline
  168. struct net_device *l3mdev_master_dev_rcu(const struct net_device *dev)
  169. {
  170. return NULL;
  171. }
  172. static inline u32 l3mdev_fib_table_rcu(const struct net_device *dev)
  173. {
  174. return 0;
  175. }
  176. static inline u32 l3mdev_fib_table(const struct net_device *dev)
  177. {
  178. return 0;
  179. }
  180. static inline u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
  181. {
  182. return 0;
  183. }
  184. static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
  185. {
  186. return false;
  187. }
  188. static inline
  189. struct dst_entry *l3mdev_link_scope_lookup(struct net *net, struct flowi6 *fl6)
  190. {
  191. return NULL;
  192. }
  193. static inline
  194. struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
  195. {
  196. return skb;
  197. }
  198. static inline
  199. struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
  200. {
  201. return skb;
  202. }
  203. static inline
  204. struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
  205. {
  206. return skb;
  207. }
  208. static inline
  209. struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
  210. {
  211. return skb;
  212. }
  213. static inline
  214. int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
  215. struct fib_lookup_arg *arg)
  216. {
  217. return 1;
  218. }
  219. static inline
  220. void l3mdev_update_flow(struct net *net, struct flowi *fl)
  221. {
  222. }
  223. #endif
  224. #endif /* _NET_L3MDEV_H_ */