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; |
| 27 | atomic_t refcnt; |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 28 | 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] | 29 | int (*orig_input)(struct sk_buff *); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 30 | int len; |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame^] | 31 | __u16 headroom; |
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); |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 39 | int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb); |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 40 | int (*input)(struct sk_buff *skb); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 41 | int (*fill_encap)(struct sk_buff *skb, |
| 42 | struct lwtunnel_state *lwtstate); |
| 43 | int (*get_encap_size)(struct lwtunnel_state *lwtstate); |
| 44 | int (*cmp_encap)(struct lwtunnel_state *a, struct lwtunnel_state *b); |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame^] | 45 | int (*xmit)(struct sk_buff *skb); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 48 | #ifdef CONFIG_LWTUNNEL |
Jiri Benc | df383e6 | 2015-08-18 18:41:13 +0200 | [diff] [blame] | 49 | static inline void lwtstate_free(struct lwtunnel_state *lws) |
| 50 | { |
| 51 | kfree(lws); |
| 52 | } |
| 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 | { |
| 99 | if (lwtunnel_xmit_redirect(lwtstate) && lwtstate->headroom < mtu) |
| 100 | return lwtstate->headroom; |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 105 | int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op, |
| 106 | unsigned int num); |
| 107 | int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op, |
| 108 | unsigned int num); |
| 109 | int lwtunnel_build_state(struct net_device *dev, u16 encap_type, |
| 110 | struct nlattr *encap, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 111 | unsigned int family, const void *cfg, |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 112 | struct lwtunnel_state **lws); |
| 113 | int lwtunnel_fill_encap(struct sk_buff *skb, |
| 114 | struct lwtunnel_state *lwtstate); |
| 115 | int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate); |
| 116 | struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len); |
| 117 | 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] | 118 | 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] | 119 | int lwtunnel_input(struct sk_buff *skb); |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame^] | 120 | int lwtunnel_xmit(struct sk_buff *skb); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 121 | |
| 122 | #else |
| 123 | |
Ying Xue | 824e738 | 2015-08-19 15:46:17 +0800 | [diff] [blame] | 124 | static inline void lwtstate_free(struct lwtunnel_state *lws) |
| 125 | { |
| 126 | } |
| 127 | |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 128 | static inline struct lwtunnel_state * |
| 129 | lwtstate_get(struct lwtunnel_state *lws) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 130 | { |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 131 | return lws; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 134 | static inline void lwtstate_put(struct lwtunnel_state *lws) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 135 | { |
| 136 | } |
| 137 | |
| 138 | static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate) |
| 139 | { |
| 140 | return false; |
| 141 | } |
| 142 | |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 143 | static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate) |
| 144 | { |
| 145 | return false; |
| 146 | } |
| 147 | |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame^] | 148 | static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate) |
| 149 | { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate, |
| 154 | unsigned int mtu) |
| 155 | { |
| 156 | return 0; |
| 157 | } |
| 158 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 159 | static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op, |
| 160 | unsigned int num) |
| 161 | { |
| 162 | return -EOPNOTSUPP; |
| 163 | |
| 164 | } |
| 165 | |
| 166 | static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op, |
| 167 | unsigned int num) |
| 168 | { |
| 169 | return -EOPNOTSUPP; |
| 170 | } |
| 171 | |
| 172 | static inline int lwtunnel_build_state(struct net_device *dev, u16 encap_type, |
| 173 | struct nlattr *encap, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 174 | unsigned int family, const void *cfg, |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 175 | struct lwtunnel_state **lws) |
| 176 | { |
| 177 | return -EOPNOTSUPP; |
| 178 | } |
| 179 | |
| 180 | static inline int lwtunnel_fill_encap(struct sk_buff *skb, |
| 181 | struct lwtunnel_state *lwtstate) |
| 182 | { |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate) |
| 187 | { |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len) |
| 192 | { |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a, |
| 197 | struct lwtunnel_state *b) |
| 198 | { |
| 199 | return 0; |
| 200 | } |
| 201 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 202 | 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] | 203 | { |
| 204 | return -EOPNOTSUPP; |
| 205 | } |
| 206 | |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 207 | static inline int lwtunnel_input(struct sk_buff *skb) |
| 208 | { |
| 209 | return -EOPNOTSUPP; |
| 210 | } |
| 211 | |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame^] | 212 | static inline int lwtunnel_xmit(struct sk_buff *skb) |
| 213 | { |
| 214 | return -EOPNOTSUPP; |
| 215 | } |
| 216 | |
Robert Shearman | 745041e | 2016-02-19 09:43:16 +0000 | [diff] [blame] | 217 | #endif /* CONFIG_LWTUNNEL */ |
| 218 | |
| 219 | #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] | 220 | |
| 221 | #endif /* __NET_LWTUNNEL_H */ |