Commit | Line | Data |
---|---|---|
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 | ||
19 | enum { | |
20 | LWTUNNEL_XMIT_DONE, | |
21 | LWTUNNEL_XMIT_CONTINUE, | |
22 | }; | |
23 | ||
499a2425 RP |
24 | |
25 | struct 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; |
e8316026 | 33 | __u8 data[]; |
499a2425 RP |
34 | }; |
35 | ||
36 | struct lwtunnel_encap_ops { | |
faee6769 | 37 | int (*build_state)(struct net *net, 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 |
7a3f5b0d RS |
54 | |
55 | DECLARE_STATIC_KEY_FALSE(nf_hooks_lwtunnel_enabled); | |
56 | ||
1104d9ba | 57 | void lwtstate_free(struct lwtunnel_state *lws); |
df383e62 | 58 | |
5a6228a0 ND |
59 | static inline struct lwtunnel_state * |
60 | lwtstate_get(struct lwtunnel_state *lws) | |
499a2425 | 61 | { |
5a6228a0 ND |
62 | if (lws) |
63 | atomic_inc(&lws->refcnt); | |
64 | ||
65 | return lws; | |
499a2425 RP |
66 | } |
67 | ||
5a6228a0 | 68 | static inline void lwtstate_put(struct lwtunnel_state *lws) |
499a2425 RP |
69 | { |
70 | if (!lws) | |
71 | return; | |
72 | ||
73 | if (atomic_dec_and_test(&lws->refcnt)) | |
df383e62 | 74 | lwtstate_free(lws); |
499a2425 RP |
75 | } |
76 | ||
77 | static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate) | |
78 | { | |
79 | if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_OUTPUT_REDIRECT)) | |
80 | return true; | |
81 | ||
82 | return false; | |
83 | } | |
84 | ||
25368623 TH |
85 | static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate) |
86 | { | |
87 | if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_INPUT_REDIRECT)) | |
88 | return true; | |
89 | ||
90 | return false; | |
91 | } | |
14972cbd RP |
92 | |
93 | static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate) | |
94 | { | |
95 | if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_XMIT_REDIRECT)) | |
96 | return true; | |
97 | ||
98 | return false; | |
99 | } | |
100 | ||
101 | static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate, | |
102 | unsigned int mtu) | |
103 | { | |
a23a8f5b DL |
104 | if ((lwtunnel_xmit_redirect(lwtstate) || |
105 | lwtunnel_output_redirect(lwtstate)) && lwtstate->headroom < mtu) | |
14972cbd RP |
106 | return lwtstate->headroom; |
107 | ||
108 | return 0; | |
109 | } | |
110 | ||
499a2425 RP |
111 | int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op, |
112 | unsigned int num); | |
113 | int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op, | |
114 | unsigned int num); | |
c255bd68 DA |
115 | int lwtunnel_valid_encap_type(u16 encap_type, |
116 | struct netlink_ext_ack *extack); | |
117 | int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len, | |
118 | struct netlink_ext_ack *extack); | |
faee6769 | 119 | int lwtunnel_build_state(struct net *net, u16 encap_type, |
499a2425 | 120 | struct nlattr *encap, |
127eb7cd | 121 | unsigned int family, const void *cfg, |
9ae28727 DA |
122 | struct lwtunnel_state **lws, |
123 | struct netlink_ext_ack *extack); | |
ffa8ce54 DA |
124 | int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate, |
125 | int encap_attr, int encap_type_attr); | |
499a2425 RP |
126 | int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate); |
127 | struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len); | |
128 | int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b); | |
ede2059d | 129 | int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb); |
25368623 | 130 | int lwtunnel_input(struct sk_buff *skb); |
14972cbd | 131 | int lwtunnel_xmit(struct sk_buff *skb); |
52f27877 PO |
132 | int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, |
133 | bool ingress); | |
499a2425 | 134 | |
9942895b DA |
135 | static inline void lwtunnel_set_redirect(struct dst_entry *dst) |
136 | { | |
137 | if (lwtunnel_output_redirect(dst->lwtstate)) { | |
138 | dst->lwtstate->orig_output = dst->output; | |
139 | dst->output = lwtunnel_output; | |
140 | } | |
141 | if (lwtunnel_input_redirect(dst->lwtstate)) { | |
142 | dst->lwtstate->orig_input = dst->input; | |
143 | dst->input = lwtunnel_input; | |
144 | } | |
145 | } | |
499a2425 RP |
146 | #else |
147 | ||
824e7383 YX |
148 | static inline void lwtstate_free(struct lwtunnel_state *lws) |
149 | { | |
150 | } | |
151 | ||
5a6228a0 ND |
152 | static inline struct lwtunnel_state * |
153 | lwtstate_get(struct lwtunnel_state *lws) | |
499a2425 | 154 | { |
5a6228a0 | 155 | return lws; |
499a2425 RP |
156 | } |
157 | ||
5a6228a0 | 158 | static inline void lwtstate_put(struct lwtunnel_state *lws) |
499a2425 RP |
159 | { |
160 | } | |
161 | ||
162 | static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate) | |
163 | { | |
164 | return false; | |
165 | } | |
166 | ||
25368623 TH |
167 | static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate) |
168 | { | |
169 | return false; | |
170 | } | |
171 | ||
14972cbd RP |
172 | static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate) |
173 | { | |
174 | return false; | |
175 | } | |
176 | ||
9942895b DA |
177 | static inline void lwtunnel_set_redirect(struct dst_entry *dst) |
178 | { | |
179 | } | |
180 | ||
14972cbd RP |
181 | static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate, |
182 | unsigned int mtu) | |
183 | { | |
184 | return 0; | |
185 | } | |
186 | ||
499a2425 RP |
187 | static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op, |
188 | unsigned int num) | |
189 | { | |
190 | return -EOPNOTSUPP; | |
191 | ||
192 | } | |
193 | ||
194 | static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op, | |
195 | unsigned int num) | |
196 | { | |
197 | return -EOPNOTSUPP; | |
198 | } | |
199 | ||
c255bd68 DA |
200 | static inline int lwtunnel_valid_encap_type(u16 encap_type, |
201 | struct netlink_ext_ack *extack) | |
9ed59592 | 202 | { |
c255bd68 | 203 | NL_SET_ERR_MSG(extack, "CONFIG_LWTUNNEL is not enabled in this kernel"); |
9ed59592 DA |
204 | return -EOPNOTSUPP; |
205 | } | |
c255bd68 DA |
206 | static inline int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len, |
207 | struct netlink_ext_ack *extack) | |
9ed59592 | 208 | { |
2bd137de DA |
209 | /* return 0 since we are not walking attr looking for |
210 | * RTA_ENCAP_TYPE attribute on nexthops. | |
211 | */ | |
212 | return 0; | |
9ed59592 DA |
213 | } |
214 | ||
faee6769 | 215 | static inline int lwtunnel_build_state(struct net *net, u16 encap_type, |
499a2425 | 216 | struct nlattr *encap, |
127eb7cd | 217 | unsigned int family, const void *cfg, |
9ae28727 DA |
218 | struct lwtunnel_state **lws, |
219 | struct netlink_ext_ack *extack) | |
499a2425 RP |
220 | { |
221 | return -EOPNOTSUPP; | |
222 | } | |
223 | ||
224 | static inline int lwtunnel_fill_encap(struct sk_buff *skb, | |
ffa8ce54 DA |
225 | struct lwtunnel_state *lwtstate, |
226 | int encap_attr, int encap_type_attr) | |
499a2425 RP |
227 | { |
228 | return 0; | |
229 | } | |
230 | ||
231 | static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate) | |
232 | { | |
233 | return 0; | |
234 | } | |
235 | ||
236 | static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len) | |
237 | { | |
238 | return NULL; | |
239 | } | |
240 | ||
241 | static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a, | |
242 | struct lwtunnel_state *b) | |
243 | { | |
244 | return 0; | |
245 | } | |
246 | ||
ede2059d | 247 | static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
ffce4196 RP |
248 | { |
249 | return -EOPNOTSUPP; | |
250 | } | |
251 | ||
25368623 TH |
252 | static inline int lwtunnel_input(struct sk_buff *skb) |
253 | { | |
254 | return -EOPNOTSUPP; | |
255 | } | |
256 | ||
14972cbd RP |
257 | static inline int lwtunnel_xmit(struct sk_buff *skb) |
258 | { | |
259 | return -EOPNOTSUPP; | |
260 | } | |
261 | ||
745041e2 RS |
262 | #endif /* CONFIG_LWTUNNEL */ |
263 | ||
264 | #define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type)) | |
499a2425 RP |
265 | |
266 | #endif /* __NET_LWTUNNEL_H */ |