lwtunnel.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #else
  108. static inline void lwtstate_free(struct lwtunnel_state *lws)
  109. {
  110. }
  111. static inline struct lwtunnel_state *
  112. lwtstate_get(struct lwtunnel_state *lws)
  113. {
  114. return lws;
  115. }
  116. static inline void lwtstate_put(struct lwtunnel_state *lws)
  117. {
  118. }
  119. static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
  120. {
  121. return false;
  122. }
  123. static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
  124. {
  125. return false;
  126. }
  127. static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate)
  128. {
  129. return false;
  130. }
  131. static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
  132. unsigned int mtu)
  133. {
  134. return 0;
  135. }
  136. static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
  137. unsigned int num)
  138. {
  139. return -EOPNOTSUPP;
  140. }
  141. static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
  142. unsigned int num)
  143. {
  144. return -EOPNOTSUPP;
  145. }
  146. static inline int lwtunnel_valid_encap_type(u16 encap_type,
  147. struct netlink_ext_ack *extack)
  148. {
  149. NL_SET_ERR_MSG(extack, "CONFIG_LWTUNNEL is not enabled in this kernel");
  150. return -EOPNOTSUPP;
  151. }
  152. static inline int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len,
  153. struct netlink_ext_ack *extack)
  154. {
  155. /* return 0 since we are not walking attr looking for
  156. * RTA_ENCAP_TYPE attribute on nexthops.
  157. */
  158. return 0;
  159. }
  160. static inline int lwtunnel_build_state(u16 encap_type,
  161. struct nlattr *encap,
  162. unsigned int family, const void *cfg,
  163. struct lwtunnel_state **lws,
  164. struct netlink_ext_ack *extack)
  165. {
  166. return -EOPNOTSUPP;
  167. }
  168. static inline int lwtunnel_fill_encap(struct sk_buff *skb,
  169. struct lwtunnel_state *lwtstate)
  170. {
  171. return 0;
  172. }
  173. static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
  174. {
  175. return 0;
  176. }
  177. static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len)
  178. {
  179. return NULL;
  180. }
  181. static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a,
  182. struct lwtunnel_state *b)
  183. {
  184. return 0;
  185. }
  186. static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  187. {
  188. return -EOPNOTSUPP;
  189. }
  190. static inline int lwtunnel_input(struct sk_buff *skb)
  191. {
  192. return -EOPNOTSUPP;
  193. }
  194. static inline int lwtunnel_xmit(struct sk_buff *skb)
  195. {
  196. return -EOPNOTSUPP;
  197. }
  198. #endif /* CONFIG_LWTUNNEL */
  199. #define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type))
  200. #endif /* __NET_LWTUNNEL_H */