Merge tag 'for-4.18/dm-fixes-2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / include / net / lwtunnel.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
499a2425
RP
2#ifndef __NET_LWTUNNEL_H
3#define __NET_LWTUNNEL_H 1
4
5#include <linux/lwtunnel.h>
6#include <linux/netdevice.h>
7#include <linux/skbuff.h>
8#include <linux/types.h>
9#include <net/route.h>
10
11#define LWTUNNEL_HASH_BITS 7
12#define LWTUNNEL_HASH_SIZE (1 << LWTUNNEL_HASH_BITS)
13
14/* lw tunnel state flags */
25368623
TH
15#define LWTUNNEL_STATE_OUTPUT_REDIRECT BIT(0)
16#define LWTUNNEL_STATE_INPUT_REDIRECT BIT(1)
14972cbd
RP
17#define LWTUNNEL_STATE_XMIT_REDIRECT BIT(2)
18
19enum {
20 LWTUNNEL_XMIT_DONE,
21 LWTUNNEL_XMIT_CONTINUE,
22};
23
499a2425
RP
24
25struct lwtunnel_state {
26 __u16 type;
27 __u16 flags;
f76a9db3 28 __u16 headroom;
499a2425 29 atomic_t refcnt;
ede2059d 30 int (*orig_output)(struct net *net, struct sock *sk, struct sk_buff *skb);
25368623 31 int (*orig_input)(struct sk_buff *);
1104d9ba 32 struct rcu_head rcu;
499a2425
RP
33 __u8 data[0];
34};
35
36struct lwtunnel_encap_ops {
30357d7d 37 int (*build_state)(struct nlattr *encap,
127eb7cd 38 unsigned int family, const void *cfg,
9ae28727
DA
39 struct lwtunnel_state **ts,
40 struct netlink_ext_ack *extack);
1104d9ba 41 void (*destroy_state)(struct lwtunnel_state *lws);
ede2059d 42 int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
25368623 43 int (*input)(struct sk_buff *skb);
499a2425
RP
44 int (*fill_encap)(struct sk_buff *skb,
45 struct lwtunnel_state *lwtstate);
46 int (*get_encap_size)(struct lwtunnel_state *lwtstate);
47 int (*cmp_encap)(struct lwtunnel_state *a, struct lwtunnel_state *b);
14972cbd 48 int (*xmit)(struct sk_buff *skb);
88ff7334
RS
49
50 struct module *owner;
499a2425
RP
51};
52
499a2425 53#ifdef CONFIG_LWTUNNEL
1104d9ba 54void lwtstate_free(struct lwtunnel_state *lws);
df383e62 55
5a6228a0
ND
56static inline struct lwtunnel_state *
57lwtstate_get(struct lwtunnel_state *lws)
499a2425 58{
5a6228a0
ND
59 if (lws)
60 atomic_inc(&lws->refcnt);
61
62 return lws;
499a2425
RP
63}
64
5a6228a0 65static inline void lwtstate_put(struct lwtunnel_state *lws)
499a2425
RP
66{
67 if (!lws)
68 return;
69
70 if (atomic_dec_and_test(&lws->refcnt))
df383e62 71 lwtstate_free(lws);
499a2425
RP
72}
73
74static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
75{
76 if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_OUTPUT_REDIRECT))
77 return true;
78
79 return false;
80}
81
25368623
TH
82static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
83{
84 if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_INPUT_REDIRECT))
85 return true;
86
87 return false;
88}
14972cbd
RP
89
90static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate)
91{
92 if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_XMIT_REDIRECT))
93 return true;
94
95 return false;
96}
97
98static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
99 unsigned int mtu)
100{
a23a8f5b
DL
101 if ((lwtunnel_xmit_redirect(lwtstate) ||
102 lwtunnel_output_redirect(lwtstate)) && lwtstate->headroom < mtu)
14972cbd
RP
103 return lwtstate->headroom;
104
105 return 0;
106}
107
499a2425
RP
108int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
109 unsigned int num);
110int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
111 unsigned int num);
c255bd68
DA
112int lwtunnel_valid_encap_type(u16 encap_type,
113 struct netlink_ext_ack *extack);
114int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len,
115 struct netlink_ext_ack *extack);
30357d7d 116int lwtunnel_build_state(u16 encap_type,
499a2425 117 struct nlattr *encap,
127eb7cd 118 unsigned int family, const void *cfg,
9ae28727
DA
119 struct lwtunnel_state **lws,
120 struct netlink_ext_ack *extack);
499a2425
RP
121int lwtunnel_fill_encap(struct sk_buff *skb,
122 struct lwtunnel_state *lwtstate);
123int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate);
124struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len);
125int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b);
ede2059d 126int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb);
25368623 127int lwtunnel_input(struct sk_buff *skb);
14972cbd 128int lwtunnel_xmit(struct sk_buff *skb);
499a2425 129
9942895b
DA
130static inline void lwtunnel_set_redirect(struct dst_entry *dst)
131{
132 if (lwtunnel_output_redirect(dst->lwtstate)) {
133 dst->lwtstate->orig_output = dst->output;
134 dst->output = lwtunnel_output;
135 }
136 if (lwtunnel_input_redirect(dst->lwtstate)) {
137 dst->lwtstate->orig_input = dst->input;
138 dst->input = lwtunnel_input;
139 }
140}
499a2425
RP
141#else
142
824e7383
YX
143static inline void lwtstate_free(struct lwtunnel_state *lws)
144{
145}
146
5a6228a0
ND
147static inline struct lwtunnel_state *
148lwtstate_get(struct lwtunnel_state *lws)
499a2425 149{
5a6228a0 150 return lws;
499a2425
RP
151}
152
5a6228a0 153static inline void lwtstate_put(struct lwtunnel_state *lws)
499a2425
RP
154{
155}
156
157static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
158{
159 return false;
160}
161
25368623
TH
162static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
163{
164 return false;
165}
166
14972cbd
RP
167static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate)
168{
169 return false;
170}
171
9942895b
DA
172static inline void lwtunnel_set_redirect(struct dst_entry *dst)
173{
174}
175
14972cbd
RP
176static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
177 unsigned int mtu)
178{
179 return 0;
180}
181
499a2425
RP
182static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
183 unsigned int num)
184{
185 return -EOPNOTSUPP;
186
187}
188
189static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
190 unsigned int num)
191{
192 return -EOPNOTSUPP;
193}
194
c255bd68
DA
195static inline int lwtunnel_valid_encap_type(u16 encap_type,
196 struct netlink_ext_ack *extack)
9ed59592 197{
c255bd68 198 NL_SET_ERR_MSG(extack, "CONFIG_LWTUNNEL is not enabled in this kernel");
9ed59592
DA
199 return -EOPNOTSUPP;
200}
c255bd68
DA
201static inline int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len,
202 struct netlink_ext_ack *extack)
9ed59592 203{
2bd137de
DA
204 /* return 0 since we are not walking attr looking for
205 * RTA_ENCAP_TYPE attribute on nexthops.
206 */
207 return 0;
9ed59592
DA
208}
209
30357d7d 210static inline int lwtunnel_build_state(u16 encap_type,
499a2425 211 struct nlattr *encap,
127eb7cd 212 unsigned int family, const void *cfg,
9ae28727
DA
213 struct lwtunnel_state **lws,
214 struct netlink_ext_ack *extack)
499a2425
RP
215{
216 return -EOPNOTSUPP;
217}
218
219static inline int lwtunnel_fill_encap(struct sk_buff *skb,
220 struct lwtunnel_state *lwtstate)
221{
222 return 0;
223}
224
225static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
226{
227 return 0;
228}
229
230static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len)
231{
232 return NULL;
233}
234
235static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a,
236 struct lwtunnel_state *b)
237{
238 return 0;
239}
240
ede2059d 241static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
ffce4196
RP
242{
243 return -EOPNOTSUPP;
244}
245
25368623
TH
246static inline int lwtunnel_input(struct sk_buff *skb)
247{
248 return -EOPNOTSUPP;
249}
250
14972cbd
RP
251static inline int lwtunnel_xmit(struct sk_buff *skb)
252{
253 return -EOPNOTSUPP;
254}
255
745041e2
RS
256#endif /* CONFIG_LWTUNNEL */
257
258#define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type))
499a2425
RP
259
260#endif /* __NET_LWTUNNEL_H */