Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 1 | #ifndef __NET_LWTUNNEL_H |
| 2 | #define __NET_LWTUNNEL_H 1 |
| 3 | |
| 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 | |
| 10 | #define LWTUNNEL_HASH_BITS 7 |
| 11 | #define LWTUNNEL_HASH_SIZE (1 << LWTUNNEL_HASH_BITS) |
| 12 | |
| 13 | /* lw tunnel state flags */ |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 14 | #define LWTUNNEL_STATE_OUTPUT_REDIRECT BIT(0) |
| 15 | #define LWTUNNEL_STATE_INPUT_REDIRECT BIT(1) |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 16 | #define LWTUNNEL_STATE_XMIT_REDIRECT BIT(2) |
| 17 | |
| 18 | enum { |
| 19 | LWTUNNEL_XMIT_DONE, |
| 20 | LWTUNNEL_XMIT_CONTINUE, |
| 21 | }; |
| 22 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 23 | |
| 24 | struct lwtunnel_state { |
| 25 | __u16 type; |
| 26 | __u16 flags; |
Thomas Graf | f76a9db | 2016-10-21 16:10:22 +0200 | [diff] [blame] | 27 | __u16 headroom; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 28 | atomic_t refcnt; |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 29 | int (*orig_output)(struct net *net, struct sock *sk, struct sk_buff *skb); |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 30 | int (*orig_input)(struct sk_buff *); |
Tom Herbert | 1104d9b | 2016-10-14 11:25:36 -0700 | [diff] [blame] | 31 | struct rcu_head rcu; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 32 | __u8 data[0]; |
| 33 | }; |
| 34 | |
| 35 | struct lwtunnel_encap_ops { |
| 36 | int (*build_state)(struct net_device *dev, struct nlattr *encap, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 37 | unsigned int family, const void *cfg, |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 38 | struct lwtunnel_state **ts); |
Tom Herbert | 1104d9b | 2016-10-14 11:25:36 -0700 | [diff] [blame] | 39 | void (*destroy_state)(struct lwtunnel_state *lws); |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 40 | int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb); |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 41 | int (*input)(struct sk_buff *skb); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 42 | int (*fill_encap)(struct sk_buff *skb, |
| 43 | struct lwtunnel_state *lwtstate); |
| 44 | int (*get_encap_size)(struct lwtunnel_state *lwtstate); |
| 45 | int (*cmp_encap)(struct lwtunnel_state *a, struct lwtunnel_state *b); |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 46 | int (*xmit)(struct sk_buff *skb); |
Robert Shearman | 88ff733 | 2017-01-24 16:26:47 +0000 | [diff] [blame] | 47 | |
| 48 | struct module *owner; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 51 | #ifdef CONFIG_LWTUNNEL |
Tom Herbert | 1104d9b | 2016-10-14 11:25:36 -0700 | [diff] [blame] | 52 | void lwtstate_free(struct lwtunnel_state *lws); |
Jiri Benc | df383e6 | 2015-08-18 18:41:13 +0200 | [diff] [blame] | 53 | |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 54 | static inline struct lwtunnel_state * |
| 55 | lwtstate_get(struct lwtunnel_state *lws) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 56 | { |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 57 | if (lws) |
| 58 | atomic_inc(&lws->refcnt); |
| 59 | |
| 60 | return lws; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 63 | static inline void lwtstate_put(struct lwtunnel_state *lws) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 64 | { |
| 65 | if (!lws) |
| 66 | return; |
| 67 | |
| 68 | if (atomic_dec_and_test(&lws->refcnt)) |
Jiri Benc | df383e6 | 2015-08-18 18:41:13 +0200 | [diff] [blame] | 69 | lwtstate_free(lws); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate) |
| 73 | { |
| 74 | if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_OUTPUT_REDIRECT)) |
| 75 | return true; |
| 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 80 | static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate) |
| 81 | { |
| 82 | if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_INPUT_REDIRECT)) |
| 83 | return true; |
| 84 | |
| 85 | return false; |
| 86 | } |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 87 | |
| 88 | static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate) |
| 89 | { |
| 90 | if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_XMIT_REDIRECT)) |
| 91 | return true; |
| 92 | |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate, |
| 97 | unsigned int mtu) |
| 98 | { |
David Lebrun | a23a8f5 | 2016-11-16 10:05:46 +0100 | [diff] [blame] | 99 | if ((lwtunnel_xmit_redirect(lwtstate) || |
| 100 | lwtunnel_output_redirect(lwtstate)) && lwtstate->headroom < mtu) |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 101 | return lwtstate->headroom; |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 106 | int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op, |
| 107 | unsigned int num); |
| 108 | int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op, |
| 109 | unsigned int num); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 110 | int lwtunnel_valid_encap_type(u16 encap_type); |
| 111 | int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 112 | int lwtunnel_build_state(struct net_device *dev, u16 encap_type, |
| 113 | struct nlattr *encap, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 114 | unsigned int family, const void *cfg, |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 115 | struct lwtunnel_state **lws); |
| 116 | int lwtunnel_fill_encap(struct sk_buff *skb, |
| 117 | struct lwtunnel_state *lwtstate); |
| 118 | int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate); |
| 119 | struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len); |
| 120 | int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b); |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 121 | int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb); |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 122 | int lwtunnel_input(struct sk_buff *skb); |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 123 | int lwtunnel_xmit(struct sk_buff *skb); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 124 | |
| 125 | #else |
| 126 | |
Ying Xue | 824e738 | 2015-08-19 15:46:17 +0800 | [diff] [blame] | 127 | static inline void lwtstate_free(struct lwtunnel_state *lws) |
| 128 | { |
| 129 | } |
| 130 | |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 131 | static inline struct lwtunnel_state * |
| 132 | lwtstate_get(struct lwtunnel_state *lws) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 133 | { |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 134 | return lws; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 137 | static inline void lwtstate_put(struct lwtunnel_state *lws) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 138 | { |
| 139 | } |
| 140 | |
| 141 | static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate) |
| 142 | { |
| 143 | return false; |
| 144 | } |
| 145 | |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 146 | static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate) |
| 147 | { |
| 148 | return false; |
| 149 | } |
| 150 | |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 151 | static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate) |
| 152 | { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate, |
| 157 | unsigned int mtu) |
| 158 | { |
| 159 | return 0; |
| 160 | } |
| 161 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 162 | static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op, |
| 163 | unsigned int num) |
| 164 | { |
| 165 | return -EOPNOTSUPP; |
| 166 | |
| 167 | } |
| 168 | |
| 169 | static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op, |
| 170 | unsigned int num) |
| 171 | { |
| 172 | return -EOPNOTSUPP; |
| 173 | } |
| 174 | |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 175 | static inline int lwtunnel_valid_encap_type(u16 encap_type) |
| 176 | { |
| 177 | return -EOPNOTSUPP; |
| 178 | } |
| 179 | static inline int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len) |
| 180 | { |
| 181 | return -EOPNOTSUPP; |
| 182 | } |
| 183 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 184 | static inline int lwtunnel_build_state(struct net_device *dev, u16 encap_type, |
| 185 | struct nlattr *encap, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 186 | unsigned int family, const void *cfg, |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 187 | struct lwtunnel_state **lws) |
| 188 | { |
| 189 | return -EOPNOTSUPP; |
| 190 | } |
| 191 | |
| 192 | static inline int lwtunnel_fill_encap(struct sk_buff *skb, |
| 193 | struct lwtunnel_state *lwtstate) |
| 194 | { |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate) |
| 199 | { |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len) |
| 204 | { |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a, |
| 209 | struct lwtunnel_state *b) |
| 210 | { |
| 211 | return 0; |
| 212 | } |
| 213 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 214 | static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
Roopa Prabhu | ffce419 | 2015-07-21 10:43:49 +0200 | [diff] [blame] | 215 | { |
| 216 | return -EOPNOTSUPP; |
| 217 | } |
| 218 | |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 219 | static inline int lwtunnel_input(struct sk_buff *skb) |
| 220 | { |
| 221 | return -EOPNOTSUPP; |
| 222 | } |
| 223 | |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 224 | static inline int lwtunnel_xmit(struct sk_buff *skb) |
| 225 | { |
| 226 | return -EOPNOTSUPP; |
| 227 | } |
| 228 | |
Robert Shearman | 745041e | 2016-02-19 09:43:16 +0000 | [diff] [blame] | 229 | #endif /* CONFIG_LWTUNNEL */ |
| 230 | |
| 231 | #define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type)) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 232 | |
| 233 | #endif /* __NET_LWTUNNEL_H */ |