]>
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 |
1104d9ba | 54 | void lwtstate_free(struct lwtunnel_state *lws); |
df383e62 | 55 | |
5a6228a0 ND |
56 | static inline struct lwtunnel_state * |
57 | lwtstate_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 | 65 | static 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 | ||
74 | static 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 |
82 | static 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 | |
90 | static 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 | ||
98 | static 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 |
108 | int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op, |
109 | unsigned int num); | |
110 | int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op, | |
111 | unsigned int num); | |
c255bd68 DA |
112 | int lwtunnel_valid_encap_type(u16 encap_type, |
113 | struct netlink_ext_ack *extack); | |
114 | int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len, | |
115 | struct netlink_ext_ack *extack); | |
faee6769 | 116 | int lwtunnel_build_state(struct net *net, 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); | |
ffa8ce54 DA |
121 | int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate, |
122 | int encap_attr, int encap_type_attr); | |
499a2425 RP |
123 | int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate); |
124 | struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len); | |
125 | int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b); | |
ede2059d | 126 | int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb); |
25368623 | 127 | int lwtunnel_input(struct sk_buff *skb); |
14972cbd | 128 | int lwtunnel_xmit(struct sk_buff *skb); |
52f27877 PO |
129 | int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, |
130 | bool ingress); | |
499a2425 | 131 | |
9942895b DA |
132 | static 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 | } | |
499a2425 RP |
143 | #else |
144 | ||
824e7383 YX |
145 | static inline void lwtstate_free(struct lwtunnel_state *lws) |
146 | { | |
147 | } | |
148 | ||
5a6228a0 ND |
149 | static inline struct lwtunnel_state * |
150 | lwtstate_get(struct lwtunnel_state *lws) | |
499a2425 | 151 | { |
5a6228a0 | 152 | return lws; |
499a2425 RP |
153 | } |
154 | ||
5a6228a0 | 155 | static inline void lwtstate_put(struct lwtunnel_state *lws) |
499a2425 RP |
156 | { |
157 | } | |
158 | ||
159 | static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate) | |
160 | { | |
161 | return false; | |
162 | } | |
163 | ||
25368623 TH |
164 | static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate) |
165 | { | |
166 | return false; | |
167 | } | |
168 | ||
14972cbd RP |
169 | static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate) |
170 | { | |
171 | return false; | |
172 | } | |
173 | ||
9942895b DA |
174 | static inline void lwtunnel_set_redirect(struct dst_entry *dst) |
175 | { | |
176 | } | |
177 | ||
14972cbd RP |
178 | static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate, |
179 | unsigned int mtu) | |
180 | { | |
181 | return 0; | |
182 | } | |
183 | ||
499a2425 RP |
184 | static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op, |
185 | unsigned int num) | |
186 | { | |
187 | return -EOPNOTSUPP; | |
188 | ||
189 | } | |
190 | ||
191 | static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op, | |
192 | unsigned int num) | |
193 | { | |
194 | return -EOPNOTSUPP; | |
195 | } | |
196 | ||
c255bd68 DA |
197 | static inline int lwtunnel_valid_encap_type(u16 encap_type, |
198 | struct netlink_ext_ack *extack) | |
9ed59592 | 199 | { |
c255bd68 | 200 | NL_SET_ERR_MSG(extack, "CONFIG_LWTUNNEL is not enabled in this kernel"); |
9ed59592 DA |
201 | return -EOPNOTSUPP; |
202 | } | |
c255bd68 DA |
203 | static inline int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len, |
204 | struct netlink_ext_ack *extack) | |
9ed59592 | 205 | { |
2bd137de DA |
206 | /* return 0 since we are not walking attr looking for |
207 | * RTA_ENCAP_TYPE attribute on nexthops. | |
208 | */ | |
209 | return 0; | |
9ed59592 DA |
210 | } |
211 | ||
faee6769 | 212 | static inline int lwtunnel_build_state(struct net *net, u16 encap_type, |
499a2425 | 213 | struct nlattr *encap, |
127eb7cd | 214 | unsigned int family, const void *cfg, |
9ae28727 DA |
215 | struct lwtunnel_state **lws, |
216 | struct netlink_ext_ack *extack) | |
499a2425 RP |
217 | { |
218 | return -EOPNOTSUPP; | |
219 | } | |
220 | ||
221 | static inline int lwtunnel_fill_encap(struct sk_buff *skb, | |
ffa8ce54 DA |
222 | struct lwtunnel_state *lwtstate, |
223 | int encap_attr, int encap_type_attr) | |
499a2425 RP |
224 | { |
225 | return 0; | |
226 | } | |
227 | ||
228 | static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate) | |
229 | { | |
230 | return 0; | |
231 | } | |
232 | ||
233 | static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len) | |
234 | { | |
235 | return NULL; | |
236 | } | |
237 | ||
238 | static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a, | |
239 | struct lwtunnel_state *b) | |
240 | { | |
241 | return 0; | |
242 | } | |
243 | ||
ede2059d | 244 | static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
ffce4196 RP |
245 | { |
246 | return -EOPNOTSUPP; | |
247 | } | |
248 | ||
25368623 TH |
249 | static inline int lwtunnel_input(struct sk_buff *skb) |
250 | { | |
251 | return -EOPNOTSUPP; | |
252 | } | |
253 | ||
14972cbd RP |
254 | static inline int lwtunnel_xmit(struct sk_buff *skb) |
255 | { | |
256 | return -EOPNOTSUPP; | |
257 | } | |
258 | ||
745041e2 RS |
259 | #endif /* CONFIG_LWTUNNEL */ |
260 | ||
261 | #define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type)) | |
499a2425 RP |
262 | |
263 | #endif /* __NET_LWTUNNEL_H */ |