blob: ea3f80f58fd6d57f01d4cd9d7cbe659e7a123150 [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;
27 atomic_t refcnt;
Eric W. Biedermanede20592015-10-07 16:48:47 -050028 int (*orig_output)(struct net *net, struct sock *sk, struct sk_buff *skb);
Tom Herbert25368622015-08-17 13:42:24 -070029 int (*orig_input)(struct sk_buff *);
Roopa Prabhu499a2422015-07-21 10:43:46 +020030 int len;
Roopa Prabhu14972cb2016-08-24 20:10:43 -070031 __u16 headroom;
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);
Eric W. Biedermanede20592015-10-07 16:48:47 -050039 int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
Tom Herbert25368622015-08-17 13:42:24 -070040 int (*input)(struct sk_buff *skb);
Roopa Prabhu499a2422015-07-21 10:43:46 +020041 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 Prabhu14972cb2016-08-24 20:10:43 -070045 int (*xmit)(struct sk_buff *skb);
Roopa Prabhu499a2422015-07-21 10:43:46 +020046};
47
Roopa Prabhu499a2422015-07-21 10:43:46 +020048#ifdef CONFIG_LWTUNNEL
Jiri Bencdf383e62015-08-18 18:41:13 +020049static inline void lwtstate_free(struct lwtunnel_state *lws)
50{
51 kfree(lws);
52}
53
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{
99 if (lwtunnel_xmit_redirect(lwtstate) && lwtstate->headroom < mtu)
100 return lwtstate->headroom;
101
102 return 0;
103}
104
Roopa Prabhu499a2422015-07-21 10:43:46 +0200105int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
106 unsigned int num);
107int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
108 unsigned int num);
109int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
110 struct nlattr *encap,
Tom Herbert127eb7c2015-08-24 09:45:41 -0700111 unsigned int family, const void *cfg,
Roopa Prabhu499a2422015-07-21 10:43:46 +0200112 struct lwtunnel_state **lws);
113int lwtunnel_fill_encap(struct sk_buff *skb,
114 struct lwtunnel_state *lwtstate);
115int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate);
116struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len);
117int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b);
Eric W. Biedermanede20592015-10-07 16:48:47 -0500118int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb);
Tom Herbert25368622015-08-17 13:42:24 -0700119int lwtunnel_input(struct sk_buff *skb);
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700120int lwtunnel_xmit(struct sk_buff *skb);
Roopa Prabhu499a2422015-07-21 10:43:46 +0200121
122#else
123
Ying Xue824e7382015-08-19 15:46:17 +0800124static inline void lwtstate_free(struct lwtunnel_state *lws)
125{
126}
127
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200128static inline struct lwtunnel_state *
129lwtstate_get(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200130{
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200131 return lws;
Roopa Prabhu499a2422015-07-21 10:43:46 +0200132}
133
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200134static inline void lwtstate_put(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200135{
136}
137
138static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
139{
140 return false;
141}
142
Tom Herbert25368622015-08-17 13:42:24 -0700143static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
144{
145 return false;
146}
147
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700148static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate)
149{
150 return false;
151}
152
153static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
154 unsigned int mtu)
155{
156 return 0;
157}
158
Roopa Prabhu499a2422015-07-21 10:43:46 +0200159static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
160 unsigned int num)
161{
162 return -EOPNOTSUPP;
163
164}
165
166static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
167 unsigned int num)
168{
169 return -EOPNOTSUPP;
170}
171
172static inline int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
173 struct nlattr *encap,
Tom Herbert127eb7c2015-08-24 09:45:41 -0700174 unsigned int family, const void *cfg,
Roopa Prabhu499a2422015-07-21 10:43:46 +0200175 struct lwtunnel_state **lws)
176{
177 return -EOPNOTSUPP;
178}
179
180static inline int lwtunnel_fill_encap(struct sk_buff *skb,
181 struct lwtunnel_state *lwtstate)
182{
183 return 0;
184}
185
186static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
187{
188 return 0;
189}
190
191static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len)
192{
193 return NULL;
194}
195
196static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a,
197 struct lwtunnel_state *b)
198{
199 return 0;
200}
201
Eric W. Biedermanede20592015-10-07 16:48:47 -0500202static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Roopa Prabhuffce4192015-07-21 10:43:49 +0200203{
204 return -EOPNOTSUPP;
205}
206
Tom Herbert25368622015-08-17 13:42:24 -0700207static inline int lwtunnel_input(struct sk_buff *skb)
208{
209 return -EOPNOTSUPP;
210}
211
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700212static inline int lwtunnel_xmit(struct sk_buff *skb)
213{
214 return -EOPNOTSUPP;
215}
216
Robert Shearman745041e2016-02-19 09:43:16 +0000217#endif /* CONFIG_LWTUNNEL */
218
219#define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type))
Roopa Prabhu499a2422015-07-21 10:43:46 +0200220
221#endif /* __NET_LWTUNNEL_H */