blob: 05cfd6ff65287e51dbfd5c05b4c2a0a8a45d6d6a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Roopa Prabhu499a2422015-07-21 10:43:46 +02002#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 */
Tom Herbert25368622015-08-17 13:42:24 -070015#define LWTUNNEL_STATE_OUTPUT_REDIRECT BIT(0)
16#define LWTUNNEL_STATE_INPUT_REDIRECT BIT(1)
Roopa Prabhu14972cb2016-08-24 20:10:43 -070017#define LWTUNNEL_STATE_XMIT_REDIRECT BIT(2)
18
19enum {
20 LWTUNNEL_XMIT_DONE,
21 LWTUNNEL_XMIT_CONTINUE,
22};
23
Roopa Prabhu499a2422015-07-21 10:43:46 +020024
25struct lwtunnel_state {
26 __u16 type;
27 __u16 flags;
Thomas Graff76a9db2016-10-21 16:10:22 +020028 __u16 headroom;
Roopa Prabhu499a2422015-07-21 10:43:46 +020029 atomic_t refcnt;
Eric W. Biedermanede20592015-10-07 16:48:47 -050030 int (*orig_output)(struct net *net, struct sock *sk, struct sk_buff *skb);
Tom Herbert25368622015-08-17 13:42:24 -070031 int (*orig_input)(struct sk_buff *);
Tom Herbert1104d9b2016-10-14 11:25:36 -070032 struct rcu_head rcu;
Gustavo A. R. Silvae8316022020-02-28 18:50:23 -060033 __u8 data[];
Roopa Prabhu499a2422015-07-21 10:43:46 +020034};
35
36struct lwtunnel_encap_ops {
Alexander Aringfaee6762020-03-27 18:00:21 -040037 int (*build_state)(struct net *net, struct nlattr *encap,
Tom Herbert127eb7c2015-08-24 09:45:41 -070038 unsigned int family, const void *cfg,
David Ahern9ae28722017-05-27 16:19:28 -060039 struct lwtunnel_state **ts,
40 struct netlink_ext_ack *extack);
Tom Herbert1104d9b2016-10-14 11:25:36 -070041 void (*destroy_state)(struct lwtunnel_state *lws);
Eric W. Biedermanede20592015-10-07 16:48:47 -050042 int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
Tom Herbert25368622015-08-17 13:42:24 -070043 int (*input)(struct sk_buff *skb);
Roopa Prabhu499a2422015-07-21 10:43:46 +020044 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);
Roopa Prabhu14972cb2016-08-24 20:10:43 -070048 int (*xmit)(struct sk_buff *skb);
Robert Shearman88ff7332017-01-24 16:26:47 +000049
50 struct module *owner;
Roopa Prabhu499a2422015-07-21 10:43:46 +020051};
52
Roopa Prabhu499a2422015-07-21 10:43:46 +020053#ifdef CONFIG_LWTUNNEL
Tom Herbert1104d9b2016-10-14 11:25:36 -070054void lwtstate_free(struct lwtunnel_state *lws);
Jiri Bencdf383e62015-08-18 18:41:13 +020055
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +020056static inline struct lwtunnel_state *
57lwtstate_get(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +020058{
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +020059 if (lws)
60 atomic_inc(&lws->refcnt);
61
62 return lws;
Roopa Prabhu499a2422015-07-21 10:43:46 +020063}
64
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +020065static inline void lwtstate_put(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +020066{
67 if (!lws)
68 return;
69
70 if (atomic_dec_and_test(&lws->refcnt))
Jiri Bencdf383e62015-08-18 18:41:13 +020071 lwtstate_free(lws);
Roopa Prabhu499a2422015-07-21 10:43:46 +020072}
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
Tom Herbert25368622015-08-17 13:42:24 -070082static 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}
Roopa Prabhu14972cb2016-08-24 20:10:43 -070089
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{
David Lebruna23a8f52016-11-16 10:05:46 +0100101 if ((lwtunnel_xmit_redirect(lwtstate) ||
102 lwtunnel_output_redirect(lwtstate)) && lwtstate->headroom < mtu)
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700103 return lwtstate->headroom;
104
105 return 0;
106}
107
Roopa Prabhu499a2422015-07-21 10:43:46 +0200108int 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);
David Ahernc255bd62017-05-27 16:19:27 -0600112int 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);
Alexander Aringfaee6762020-03-27 18:00:21 -0400116int lwtunnel_build_state(struct net *net, u16 encap_type,
Roopa Prabhu499a2422015-07-21 10:43:46 +0200117 struct nlattr *encap,
Tom Herbert127eb7c2015-08-24 09:45:41 -0700118 unsigned int family, const void *cfg,
David Ahern9ae28722017-05-27 16:19:28 -0600119 struct lwtunnel_state **lws,
120 struct netlink_ext_ack *extack);
David Ahernffa8ce52019-04-23 08:23:41 -0700121int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate,
122 int encap_attr, int encap_type_attr);
Roopa Prabhu499a2422015-07-21 10:43:46 +0200123int 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);
Eric W. Biedermanede20592015-10-07 16:48:47 -0500126int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb);
Tom Herbert25368622015-08-17 13:42:24 -0700127int lwtunnel_input(struct sk_buff *skb);
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700128int lwtunnel_xmit(struct sk_buff *skb);
Peter Oskolkov52f27872019-02-13 11:53:36 -0800129int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
130 bool ingress);
Roopa Prabhu499a2422015-07-21 10:43:46 +0200131
David Ahern99428952018-02-13 20:32:04 -0800132static inline void lwtunnel_set_redirect(struct dst_entry *dst)
133{
134 if (lwtunnel_output_redirect(dst->lwtstate)) {
135 dst->lwtstate->orig_output = dst->output;
136 dst->output = lwtunnel_output;
137 }
138 if (lwtunnel_input_redirect(dst->lwtstate)) {
139 dst->lwtstate->orig_input = dst->input;
140 dst->input = lwtunnel_input;
141 }
142}
Roopa Prabhu499a2422015-07-21 10:43:46 +0200143#else
144
Ying Xue824e7382015-08-19 15:46:17 +0800145static inline void lwtstate_free(struct lwtunnel_state *lws)
146{
147}
148
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200149static inline struct lwtunnel_state *
150lwtstate_get(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200151{
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200152 return lws;
Roopa Prabhu499a2422015-07-21 10:43:46 +0200153}
154
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200155static inline void lwtstate_put(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200156{
157}
158
159static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
160{
161 return false;
162}
163
Tom Herbert25368622015-08-17 13:42:24 -0700164static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
165{
166 return false;
167}
168
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700169static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate)
170{
171 return false;
172}
173
David Ahern99428952018-02-13 20:32:04 -0800174static inline void lwtunnel_set_redirect(struct dst_entry *dst)
175{
176}
177
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700178static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
179 unsigned int mtu)
180{
181 return 0;
182}
183
Roopa Prabhu499a2422015-07-21 10:43:46 +0200184static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
185 unsigned int num)
186{
187 return -EOPNOTSUPP;
188
189}
190
191static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
192 unsigned int num)
193{
194 return -EOPNOTSUPP;
195}
196
David Ahernc255bd62017-05-27 16:19:27 -0600197static inline int lwtunnel_valid_encap_type(u16 encap_type,
198 struct netlink_ext_ack *extack)
David Ahern9ed59592017-01-17 14:57:36 -0800199{
David Ahernc255bd62017-05-27 16:19:27 -0600200 NL_SET_ERR_MSG(extack, "CONFIG_LWTUNNEL is not enabled in this kernel");
David Ahern9ed59592017-01-17 14:57:36 -0800201 return -EOPNOTSUPP;
202}
David Ahernc255bd62017-05-27 16:19:27 -0600203static inline int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len,
204 struct netlink_ext_ack *extack)
David Ahern9ed59592017-01-17 14:57:36 -0800205{
David Ahern2bd137d2017-02-08 09:29:00 -0800206 /* return 0 since we are not walking attr looking for
207 * RTA_ENCAP_TYPE attribute on nexthops.
208 */
209 return 0;
David Ahern9ed59592017-01-17 14:57:36 -0800210}
211
Alexander Aringfaee6762020-03-27 18:00:21 -0400212static inline int lwtunnel_build_state(struct net *net, u16 encap_type,
Roopa Prabhu499a2422015-07-21 10:43:46 +0200213 struct nlattr *encap,
Tom Herbert127eb7c2015-08-24 09:45:41 -0700214 unsigned int family, const void *cfg,
David Ahern9ae28722017-05-27 16:19:28 -0600215 struct lwtunnel_state **lws,
216 struct netlink_ext_ack *extack)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200217{
218 return -EOPNOTSUPP;
219}
220
221static inline int lwtunnel_fill_encap(struct sk_buff *skb,
David Ahernffa8ce52019-04-23 08:23:41 -0700222 struct lwtunnel_state *lwtstate,
223 int encap_attr, int encap_type_attr)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200224{
225 return 0;
226}
227
228static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
229{
230 return 0;
231}
232
233static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len)
234{
235 return NULL;
236}
237
238static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a,
239 struct lwtunnel_state *b)
240{
241 return 0;
242}
243
Eric W. Biedermanede20592015-10-07 16:48:47 -0500244static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Roopa Prabhuffce4192015-07-21 10:43:49 +0200245{
246 return -EOPNOTSUPP;
247}
248
Tom Herbert25368622015-08-17 13:42:24 -0700249static inline int lwtunnel_input(struct sk_buff *skb)
250{
251 return -EOPNOTSUPP;
252}
253
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700254static inline int lwtunnel_xmit(struct sk_buff *skb)
255{
256 return -EOPNOTSUPP;
257}
258
Robert Shearman745041e2016-02-19 09:43:16 +0000259#endif /* CONFIG_LWTUNNEL */
260
261#define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type))
Roopa Prabhu499a2422015-07-21 10:43:46 +0200262
263#endif /* __NET_LWTUNNEL_H */