lwtunnel.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_LWTUNNEL_H
  3. #define __NET_LWTUNNEL_H 1
  4. #include <linux/lwtunnel.h>
  5. #include <linux/netdevice.h>
  6. #include <linux/skbuff.h>
  7. #include <linux/types.h>
  8. #include <net/route.h>
  9. #define LWTUNNEL_HASH_BITS 7
  10. #define LWTUNNEL_HASH_SIZE (1 << LWTUNNEL_HASH_BITS)
  11. /* lw tunnel state flags */
  12. #define LWTUNNEL_STATE_OUTPUT_REDIRECT BIT(0)
  13. #define LWTUNNEL_STATE_INPUT_REDIRECT BIT(1)
  14. #define LWTUNNEL_STATE_XMIT_REDIRECT BIT(2)
  15. enum {
  16. LWTUNNEL_XMIT_DONE,
  17. LWTUNNEL_XMIT_CONTINUE,
  18. };
  19. struct lwtunnel_state {
  20. __u16 type;
  21. __u16 flags;
  22. __u16 headroom;
  23. atomic_t refcnt;
  24. int (*orig_output)(struct net *net, struct sock *sk, struct sk_buff *skb);
  25. int (*orig_input)(struct sk_buff *);
  26. struct rcu_head rcu;
  27. __u8 data[0];
  28. };
  29. struct lwtunnel_encap_ops {
  30. int (*build_state)(struct nlattr *encap,
  31. unsigned int family, const void *cfg,
  32. struct lwtunnel_state **ts,
  33. struct netlink_ext_ack *extack);
  34. void (*destroy_state)(struct lwtunnel_state *lws);
  35. int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
  36. int (*input)(struct sk_buff *skb);
  37. int (*fill_encap)(struct sk_buff *skb,
  38. struct lwtunnel_state *lwtstate);
  39. int (*get_encap_size)(struct lwtunnel_state *lwtstate);
  40. int (*cmp_encap)(struct lwtunnel_state *a, struct lwtunnel_state *b);
  41. int (*xmit)(struct sk_buff *skb);
  42. struct module *owner;
  43. };
  44. #ifdef CONFIG_LWTUNNEL
  45. void lwtstate_free(struct lwtunnel_state *lws);
  46. static inline struct lwtunnel_state *
  47. lwtstate_get(struct lwtunnel_state *lws)
  48. {
  49. if (lws)
  50. atomic_inc(&lws->refcnt);
  51. return lws;
  52. }
  53. static inline void lwtstate_put(struct lwtunnel_state *lws)
  54. {
  55. if (!lws)
  56. return;
  57. if (atomic_dec_and_test(&lws->refcnt))
  58. lwtstate_free(lws);
  59. }
  60. static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
  61. {
  62. if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_OUTPUT_REDIRECT))
  63. return true;
  64. return false;
  65. }
  66. static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
  67. {
  68. if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_INPUT_REDIRECT))
  69. return true;
  70. return false;
  71. }
  72. static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate)
  73. {
  74. if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_XMIT_REDIRECT))
  75. return true;
  76. return false;
  77. }
  78. static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
  79. unsigned int mtu)
  80. {
  81. if ((lwtunnel_xmit_redirect(lwtstate) ||
  82. lwtunnel_output_redirect(lwtstate)) && lwtstate->headroom < mtu)
  83. return lwtstate->headroom;
  84. return 0;
  85. }
  86. int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
  87. unsigned int num);
  88. int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
  89. unsigned int num);
  90. int lwtunnel_valid_encap_type(u16 encap_type,
  91. struct netlink_ext_ack *extack);
  92. int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len,
  93. struct netlink_ext_ack *extack);
  94. int lwtunnel_build_state(u16 encap_type,
  95. struct nlattr *encap,
  96. unsigned int family, const void *cfg,
  97. struct lwtunnel_state **lws,
  98. struct netlink_ext_ack *extack);
  99. int lwtunnel_fill_encap(struct sk_buff *skb,
  100. struct lwtunnel_state *lwtstate);
  101. int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate);
  102. struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len);
  103. int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b);
  104. int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb);
  105. int lwtunnel_input(struct sk_buff *skb);
  106. int lwtunnel_xmit(struct sk_buff *skb);
  107. static inline void lwtunnel_set_redirect(struct dst_entry *dst)
  108. {
  109. if (lwtunnel_output_redirect(dst->lwtstate)) {
  110. dst->lwtstate->orig_output = dst->output;
  111. dst->output = lwtunnel_output;
  112. }
  113. if (lwtunnel_input_redirect(dst->lwtstate)) {
  114. dst->lwtstate->orig_input = dst->input;
  115. dst->input = lwtunnel_input;
  116. }
  117. }
  118. #else
  119. static inline void lwtstate_free(struct lwtunnel_state *lws)
  120. {
  121. }
  122. static inline struct lwtunnel_state *
  123. lwtstate_get(struct lwtunnel_state *lws)
  124. {
  125. return lws;
  126. }
  127. static inline void lwtstate_put(struct lwtunnel_state *lws)
  128. {
  129. }
  130. static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
  131. {
  132. return false;
  133. }
  134. static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
  135. {
  136. return false;
  137. }
  138. static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate)
  139. {
  140. return false;
  141. }
  142. static inline void lwtunnel_set_redirect(struct dst_entry *dst)
  143. {
  144. }
  145. static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
  146. unsigned int mtu)
  147. {
  148. return 0;
  149. }
  150. static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
  151. unsigned int num)
  152. {
  153. return -EOPNOTSUPP;
  154. }
  155. static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
  156. unsigned int num)
  157. {
  158. return -EOPNOTSUPP;
  159. }
  160. static inline int lwtunnel_valid_encap_type(u16 encap_type,
  161. struct netlink_ext_ack *extack)
  162. {
  163. NL_SET_ERR_MSG(extack, "CONFIG_LWTUNNEL is not enabled in this kernel");
  164. return -EOPNOTSUPP;
  165. }
  166. static inline int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len,
  167. struct netlink_ext_ack *extack)
  168. {
  169. /* return 0 since we are not walking attr looking for
  170. * RTA_ENCAP_TYPE attribute on nexthops.
  171. */
  172. return 0;
  173. }
  174. static inline int lwtunnel_build_state(u16 encap_type,
  175. struct nlattr *encap,
  176. unsigned int family, const void *cfg,
  177. struct lwtunnel_state **lws,
  178. struct netlink_ext_ack *extack)
  179. {
  180. return -EOPNOTSUPP;
  181. }
  182. static inline int lwtunnel_fill_encap(struct sk_buff *skb,
  183. struct lwtunnel_state *lwtstate)
  184. {
  185. return 0;
  186. }
  187. static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
  188. {
  189. return 0;
  190. }
  191. static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len)
  192. {
  193. return NULL;
  194. }
  195. static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a,
  196. struct lwtunnel_state *b)
  197. {
  198. return 0;
  199. }
  200. static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  201. {
  202. return -EOPNOTSUPP;
  203. }
  204. static inline int lwtunnel_input(struct sk_buff *skb)
  205. {
  206. return -EOPNOTSUPP;
  207. }
  208. static inline int lwtunnel_xmit(struct sk_buff *skb)
  209. {
  210. return -EOPNOTSUPP;
  211. }
  212. #endif /* CONFIG_LWTUNNEL */
  213. #define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type))
  214. #endif /* __NET_LWTUNNEL_H */