l3mdev.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. master = skb->dev;
  116. if (master && master->l3mdev_ops->l3mdev_l3_rcv)
  117. skb = master->l3mdev_ops->l3mdev_l3_rcv(master, skb, proto);
  118. return skb;
  119. }
  120. static inline
  121. struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
  122. {
  123. return l3mdev_l3_rcv(skb, AF_INET);
  124. }
  125. static inline
  126. struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
  127. {
  128. return l3mdev_l3_rcv(skb, AF_INET6);
  129. }
  130. static inline
  131. struct sk_buff *l3mdev_l3_out(struct sock *sk, struct sk_buff *skb, u16 proto)
  132. {
  133. struct net_device *dev = skb_dst(skb)->dev;
  134. if (netif_is_l3_slave(dev)) {
  135. struct net_device *master;
  136. master = netdev_master_upper_dev_get_rcu(dev);
  137. if (master && master->l3mdev_ops->l3mdev_l3_out)
  138. skb = master->l3mdev_ops->l3mdev_l3_out(master, sk,
  139. skb, proto);
  140. }
  141. return skb;
  142. }
  143. static inline
  144. struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
  145. {
  146. return l3mdev_l3_out(sk, skb, AF_INET);
  147. }
  148. static inline
  149. struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
  150. {
  151. return l3mdev_l3_out(sk, skb, AF_INET6);
  152. }
  153. #else
  154. static inline int l3mdev_master_ifindex_rcu(const struct net_device *dev)
  155. {
  156. return 0;
  157. }
  158. static inline int l3mdev_master_ifindex(struct net_device *dev)
  159. {
  160. return 0;
  161. }
  162. static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
  163. {
  164. return 0;
  165. }
  166. static inline
  167. struct net_device *l3mdev_master_dev_rcu(const struct net_device *dev)
  168. {
  169. return NULL;
  170. }
  171. static inline u32 l3mdev_fib_table_rcu(const struct net_device *dev)
  172. {
  173. return 0;
  174. }
  175. static inline u32 l3mdev_fib_table(const struct net_device *dev)
  176. {
  177. return 0;
  178. }
  179. static inline u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
  180. {
  181. return 0;
  182. }
  183. static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
  184. {
  185. return false;
  186. }
  187. static inline
  188. struct dst_entry *l3mdev_link_scope_lookup(struct net *net, struct flowi6 *fl6)
  189. {
  190. return NULL;
  191. }
  192. static inline
  193. struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
  194. {
  195. return skb;
  196. }
  197. static inline
  198. struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
  199. {
  200. return skb;
  201. }
  202. static inline
  203. struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
  204. {
  205. return skb;
  206. }
  207. static inline
  208. struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
  209. {
  210. return skb;
  211. }
  212. static inline
  213. int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
  214. struct fib_lookup_arg *arg)
  215. {
  216. return 1;
  217. }
  218. static inline
  219. void l3mdev_update_flow(struct net *net, struct flowi *fl)
  220. {
  221. }
  222. #endif
  223. #endif /* _NET_L3MDEV_H_ */