blob: d4c1c75b886244f76f9539c0709bb72be7974578 [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);
Roopa Prabhu499a2422015-07-21 10:43:46 +020047};
48
Roopa Prabhu499a2422015-07-21 10:43:46 +020049#ifdef CONFIG_LWTUNNEL
Tom Herbert1104d9b2016-10-14 11:25:36 -070050void lwtstate_free(struct lwtunnel_state *lws);
Jiri Bencdf383e62015-08-18 18:41:13 +020051
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +020052static inline struct lwtunnel_state *
53lwtstate_get(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +020054{
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +020055 if (lws)
56 atomic_inc(&lws->refcnt);
57
58 return lws;
Roopa Prabhu499a2422015-07-21 10:43:46 +020059}
60
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +020061static inline void lwtstate_put(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +020062{
63 if (!lws)
64 return;
65
66 if (atomic_dec_and_test(&lws->refcnt))
Jiri Bencdf383e62015-08-18 18:41:13 +020067 lwtstate_free(lws);
Roopa Prabhu499a2422015-07-21 10:43:46 +020068}
69
70static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
71{
72 if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_OUTPUT_REDIRECT))
73 return true;
74
75 return false;
76}
77
Tom Herbert25368622015-08-17 13:42:24 -070078static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
79{
80 if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_INPUT_REDIRECT))
81 return true;
82
83 return false;
84}
Roopa Prabhu14972cb2016-08-24 20:10:43 -070085
86static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate)
87{
88 if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_XMIT_REDIRECT))
89 return true;
90
91 return false;
92}
93
94static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
95 unsigned int mtu)
96{
David Lebruna23a8f52016-11-16 10:05:46 +010097 if ((lwtunnel_xmit_redirect(lwtstate) ||
98 lwtunnel_output_redirect(lwtstate)) && lwtstate->headroom < mtu)
Roopa Prabhu14972cb2016-08-24 20:10:43 -070099 return lwtstate->headroom;
100
101 return 0;
102}
103
Roopa Prabhu499a2422015-07-21 10:43:46 +0200104int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
105 unsigned int num);
106int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
107 unsigned int num);
108int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
109 struct nlattr *encap,
Tom Herbert127eb7c2015-08-24 09:45:41 -0700110 unsigned int family, const void *cfg,
Roopa Prabhu499a2422015-07-21 10:43:46 +0200111 struct lwtunnel_state **lws);
112int lwtunnel_fill_encap(struct sk_buff *skb,
113 struct lwtunnel_state *lwtstate);
114int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate);
115struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len);
116int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b);
Eric W. Biedermanede20592015-10-07 16:48:47 -0500117int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb);
Tom Herbert25368622015-08-17 13:42:24 -0700118int lwtunnel_input(struct sk_buff *skb);
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700119int lwtunnel_xmit(struct sk_buff *skb);
Roopa Prabhu499a2422015-07-21 10:43:46 +0200120
121#else
122
Ying Xue824e7382015-08-19 15:46:17 +0800123static inline void lwtstate_free(struct lwtunnel_state *lws)
124{
125}
126
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200127static inline struct lwtunnel_state *
128lwtstate_get(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200129{
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200130 return lws;
Roopa Prabhu499a2422015-07-21 10:43:46 +0200131}
132
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200133static inline void lwtstate_put(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200134{
135}
136
137static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
138{
139 return false;
140}
141
Tom Herbert25368622015-08-17 13:42:24 -0700142static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
143{
144 return false;
145}
146
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700147static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate)
148{
149 return false;
150}
151
152static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
153 unsigned int mtu)
154{
155 return 0;
156}
157
Roopa Prabhu499a2422015-07-21 10:43:46 +0200158static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
159 unsigned int num)
160{
161 return -EOPNOTSUPP;
162
163}
164
165static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
166 unsigned int num)
167{
168 return -EOPNOTSUPP;
169}
170
171static inline int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
172 struct nlattr *encap,
Tom Herbert127eb7c2015-08-24 09:45:41 -0700173 unsigned int family, const void *cfg,
Roopa Prabhu499a2422015-07-21 10:43:46 +0200174 struct lwtunnel_state **lws)
175{
176 return -EOPNOTSUPP;
177}
178
179static inline int lwtunnel_fill_encap(struct sk_buff *skb,
180 struct lwtunnel_state *lwtstate)
181{
182 return 0;
183}
184
185static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
186{
187 return 0;
188}
189
190static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len)
191{
192 return NULL;
193}
194
195static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a,
196 struct lwtunnel_state *b)
197{
198 return 0;
199}
200
Eric W. Biedermanede20592015-10-07 16:48:47 -0500201static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Roopa Prabhuffce4192015-07-21 10:43:49 +0200202{
203 return -EOPNOTSUPP;
204}
205
Tom Herbert25368622015-08-17 13:42:24 -0700206static inline int lwtunnel_input(struct sk_buff *skb)
207{
208 return -EOPNOTSUPP;
209}
210
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700211static inline int lwtunnel_xmit(struct sk_buff *skb)
212{
213 return -EOPNOTSUPP;
214}
215
Robert Shearman745041e2016-02-19 09:43:16 +0000216#endif /* CONFIG_LWTUNNEL */
217
218#define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type))
Roopa Prabhu499a2422015-07-21 10:43:46 +0200219
220#endif /* __NET_LWTUNNEL_H */