blob: 73dd8764746069d48cd9ed6863f54c53864bb47a [file] [log] [blame]
Roopa Prabhu499a2422015-07-21 10:43:46 +02001#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 Herbert25368622015-08-17 13:42:24 -070014#define LWTUNNEL_STATE_OUTPUT_REDIRECT BIT(0)
15#define LWTUNNEL_STATE_INPUT_REDIRECT BIT(1)
Roopa Prabhu14972cb2016-08-24 20:10:43 -070016#define LWTUNNEL_STATE_XMIT_REDIRECT BIT(2)
17
18enum {
19 LWTUNNEL_XMIT_DONE,
20 LWTUNNEL_XMIT_CONTINUE,
21};
22
Roopa Prabhu499a2422015-07-21 10:43:46 +020023
24struct lwtunnel_state {
25 __u16 type;
26 __u16 flags;
Thomas Graff76a9db2016-10-21 16:10:22 +020027 __u16 headroom;
Roopa Prabhu499a2422015-07-21 10:43:46 +020028 atomic_t refcnt;
Eric W. Biedermanede20592015-10-07 16:48:47 -050029 int (*orig_output)(struct net *net, struct sock *sk, struct sk_buff *skb);
Tom Herbert25368622015-08-17 13:42:24 -070030 int (*orig_input)(struct sk_buff *);
Tom Herbert1104d9b2016-10-14 11:25:36 -070031 struct rcu_head rcu;
Roopa Prabhu499a2422015-07-21 10:43:46 +020032 __u8 data[0];
33};
34
35struct lwtunnel_encap_ops {
36 int (*build_state)(struct net_device *dev, struct nlattr *encap,
Tom Herbert127eb7c2015-08-24 09:45:41 -070037 unsigned int family, const void *cfg,
Roopa Prabhu499a2422015-07-21 10:43:46 +020038 struct lwtunnel_state **ts);
Tom Herbert1104d9b2016-10-14 11:25:36 -070039 void (*destroy_state)(struct lwtunnel_state *lws);
Eric W. Biedermanede20592015-10-07 16:48:47 -050040 int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
Tom Herbert25368622015-08-17 13:42:24 -070041 int (*input)(struct sk_buff *skb);
Roopa Prabhu499a2422015-07-21 10:43:46 +020042 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 Prabhu14972cb2016-08-24 20:10:43 -070046 int (*xmit)(struct sk_buff *skb);
Robert Shearman88ff7332017-01-24 16:26:47 +000047
48 struct module *owner;
Roopa Prabhu499a2422015-07-21 10:43:46 +020049};
50
Roopa Prabhu499a2422015-07-21 10:43:46 +020051#ifdef CONFIG_LWTUNNEL
Tom Herbert1104d9b2016-10-14 11:25:36 -070052void lwtstate_free(struct lwtunnel_state *lws);
Jiri Bencdf383e62015-08-18 18:41:13 +020053
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +020054static inline struct lwtunnel_state *
55lwtstate_get(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +020056{
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +020057 if (lws)
58 atomic_inc(&lws->refcnt);
59
60 return lws;
Roopa Prabhu499a2422015-07-21 10:43:46 +020061}
62
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +020063static inline void lwtstate_put(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +020064{
65 if (!lws)
66 return;
67
68 if (atomic_dec_and_test(&lws->refcnt))
Jiri Bencdf383e62015-08-18 18:41:13 +020069 lwtstate_free(lws);
Roopa Prabhu499a2422015-07-21 10:43:46 +020070}
71
72static 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 Herbert25368622015-08-17 13:42:24 -070080static 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 Prabhu14972cb2016-08-24 20:10:43 -070087
88static 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
96static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
97 unsigned int mtu)
98{
David Lebruna23a8f52016-11-16 10:05:46 +010099 if ((lwtunnel_xmit_redirect(lwtstate) ||
100 lwtunnel_output_redirect(lwtstate)) && lwtstate->headroom < mtu)
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700101 return lwtstate->headroom;
102
103 return 0;
104}
105
Roopa Prabhu499a2422015-07-21 10:43:46 +0200106int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
107 unsigned int num);
108int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
109 unsigned int num);
David Ahern9ed59592017-01-17 14:57:36 -0800110int lwtunnel_valid_encap_type(u16 encap_type);
111int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len);
Roopa Prabhu499a2422015-07-21 10:43:46 +0200112int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
113 struct nlattr *encap,
Tom Herbert127eb7c2015-08-24 09:45:41 -0700114 unsigned int family, const void *cfg,
Roopa Prabhu499a2422015-07-21 10:43:46 +0200115 struct lwtunnel_state **lws);
116int lwtunnel_fill_encap(struct sk_buff *skb,
117 struct lwtunnel_state *lwtstate);
118int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate);
119struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len);
120int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b);
Eric W. Biedermanede20592015-10-07 16:48:47 -0500121int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb);
Tom Herbert25368622015-08-17 13:42:24 -0700122int lwtunnel_input(struct sk_buff *skb);
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700123int lwtunnel_xmit(struct sk_buff *skb);
Roopa Prabhu499a2422015-07-21 10:43:46 +0200124
125#else
126
Ying Xue824e7382015-08-19 15:46:17 +0800127static inline void lwtstate_free(struct lwtunnel_state *lws)
128{
129}
130
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200131static inline struct lwtunnel_state *
132lwtstate_get(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200133{
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200134 return lws;
Roopa Prabhu499a2422015-07-21 10:43:46 +0200135}
136
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200137static inline void lwtstate_put(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200138{
139}
140
141static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
142{
143 return false;
144}
145
Tom Herbert25368622015-08-17 13:42:24 -0700146static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
147{
148 return false;
149}
150
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700151static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate)
152{
153 return false;
154}
155
156static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
157 unsigned int mtu)
158{
159 return 0;
160}
161
Roopa Prabhu499a2422015-07-21 10:43:46 +0200162static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
163 unsigned int num)
164{
165 return -EOPNOTSUPP;
166
167}
168
169static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
170 unsigned int num)
171{
172 return -EOPNOTSUPP;
173}
174
David Ahern9ed59592017-01-17 14:57:36 -0800175static inline int lwtunnel_valid_encap_type(u16 encap_type)
176{
177 return -EOPNOTSUPP;
178}
179static inline int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len)
180{
181 return -EOPNOTSUPP;
182}
183
Roopa Prabhu499a2422015-07-21 10:43:46 +0200184static inline int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
185 struct nlattr *encap,
Tom Herbert127eb7c2015-08-24 09:45:41 -0700186 unsigned int family, const void *cfg,
Roopa Prabhu499a2422015-07-21 10:43:46 +0200187 struct lwtunnel_state **lws)
188{
189 return -EOPNOTSUPP;
190}
191
192static inline int lwtunnel_fill_encap(struct sk_buff *skb,
193 struct lwtunnel_state *lwtstate)
194{
195 return 0;
196}
197
198static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
199{
200 return 0;
201}
202
203static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len)
204{
205 return NULL;
206}
207
208static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a,
209 struct lwtunnel_state *b)
210{
211 return 0;
212}
213
Eric W. Biedermanede20592015-10-07 16:48:47 -0500214static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Roopa Prabhuffce4192015-07-21 10:43:49 +0200215{
216 return -EOPNOTSUPP;
217}
218
Tom Herbert25368622015-08-17 13:42:24 -0700219static inline int lwtunnel_input(struct sk_buff *skb)
220{
221 return -EOPNOTSUPP;
222}
223
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700224static inline int lwtunnel_xmit(struct sk_buff *skb)
225{
226 return -EOPNOTSUPP;
227}
228
Robert Shearman745041e2016-02-19 09:43:16 +0000229#endif /* CONFIG_LWTUNNEL */
230
231#define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type))
Roopa Prabhu499a2422015-07-21 10:43:46 +0200232
233#endif /* __NET_LWTUNNEL_H */