lwtunnel.h 5.4 KB

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