1 /* SPDX-License-Identifier: GPL-2.0 */
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 */
15 #define LWTUNNEL_STATE_OUTPUT_REDIRECT BIT(0)
16 #define LWTUNNEL_STATE_INPUT_REDIRECT BIT(1)
17 #define LWTUNNEL_STATE_XMIT_REDIRECT BIT(2)
18
19 /* LWTUNNEL_XMIT_CONTINUE should be distinguishable from dst_output return
20 * values (NET_XMIT_xxx and NETDEV_TX_xxx in linux/netdevice.h) for safety.
21 */
22 enum {
23 LWTUNNEL_XMIT_DONE,
24 LWTUNNEL_XMIT_CONTINUE = 0x100,
25 };
26
27
28 struct lwtunnel_state {
29 __u16 type;
30 __u16 flags;
31 __u16 headroom;
32 atomic_t refcnt;
33 int (*orig_output)(struct net *net, struct sock *sk, struct sk_buff *skb);
34 int (*orig_input)(struct sk_buff *);
35 struct rcu_head rcu;
36 __u8 data[0];
37 };
38
39 struct lwtunnel_encap_ops {
40 int (*build_state)(struct nlattr *encap,
41 unsigned int family, const void *cfg,
42 struct lwtunnel_state **ts,
43 struct netlink_ext_ack *extack);
44 void (*destroy_state)(struct lwtunnel_state *lws);
45 int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
46 int (*input)(struct sk_buff *skb);
47 int (*fill_encap)(struct sk_buff *skb,
48 struct lwtunnel_state *lwtstate);
49 int (*get_encap_size)(struct lwtunnel_state *lwtstate);
50 int (*cmp_encap)(struct lwtunnel_state *a, struct lwtunnel_state *b);
51 int (*xmit)(struct sk_buff *skb);
52
53 struct module *owner;
54 };
55
56 #ifdef CONFIG_LWTUNNEL
57 void lwtstate_free(struct lwtunnel_state *lws);
58
59 static inline struct lwtunnel_state *
lwtstate_get(struct lwtunnel_state * lws)60 lwtstate_get(struct lwtunnel_state *lws)
61 {
62 if (lws)
63 atomic_inc(&lws->refcnt);
64
65 return lws;
66 }
67
lwtstate_put(struct lwtunnel_state * lws)68 static inline void lwtstate_put(struct lwtunnel_state *lws)
69 {
70 if (!lws)
71 return;
72
73 if (atomic_dec_and_test(&lws->refcnt))
74 lwtstate_free(lws);
75 }
76
lwtunnel_output_redirect(struct lwtunnel_state * lwtstate)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
lwtunnel_input_redirect(struct lwtunnel_state * lwtstate)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 }
92
lwtunnel_xmit_redirect(struct lwtunnel_state * lwtstate)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
lwtunnel_headroom(struct lwtunnel_state * lwtstate,unsigned int mtu)101 static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
102 unsigned int mtu)
103 {
104 if ((lwtunnel_xmit_redirect(lwtstate) ||
105 lwtunnel_output_redirect(lwtstate)) && lwtstate->headroom < mtu)
106 return lwtstate->headroom;
107
108 return 0;
109 }
110
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);
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);
119 int lwtunnel_build_state(u16 encap_type,
120 struct nlattr *encap,
121 unsigned int family, const void *cfg,
122 struct lwtunnel_state **lws,
123 struct netlink_ext_ack *extack);
124 int lwtunnel_fill_encap(struct sk_buff *skb,
125 struct lwtunnel_state *lwtstate);
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);
129 int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb);
130 int lwtunnel_input(struct sk_buff *skb);
131 int lwtunnel_xmit(struct sk_buff *skb);
132
lwtunnel_set_redirect(struct dst_entry * dst)133 static inline void lwtunnel_set_redirect(struct dst_entry *dst)
134 {
135 if (lwtunnel_output_redirect(dst->lwtstate)) {
136 dst->lwtstate->orig_output = dst->output;
137 dst->output = lwtunnel_output;
138 }
139 if (lwtunnel_input_redirect(dst->lwtstate)) {
140 dst->lwtstate->orig_input = dst->input;
141 dst->input = lwtunnel_input;
142 }
143 }
144 #else
145
lwtstate_free(struct lwtunnel_state * lws)146 static inline void lwtstate_free(struct lwtunnel_state *lws)
147 {
148 }
149
150 static inline struct lwtunnel_state *
lwtstate_get(struct lwtunnel_state * lws)151 lwtstate_get(struct lwtunnel_state *lws)
152 {
153 return lws;
154 }
155
lwtstate_put(struct lwtunnel_state * lws)156 static inline void lwtstate_put(struct lwtunnel_state *lws)
157 {
158 }
159
lwtunnel_output_redirect(struct lwtunnel_state * lwtstate)160 static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
161 {
162 return false;
163 }
164
lwtunnel_input_redirect(struct lwtunnel_state * lwtstate)165 static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
166 {
167 return false;
168 }
169
lwtunnel_xmit_redirect(struct lwtunnel_state * lwtstate)170 static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate)
171 {
172 return false;
173 }
174
lwtunnel_set_redirect(struct dst_entry * dst)175 static inline void lwtunnel_set_redirect(struct dst_entry *dst)
176 {
177 }
178
lwtunnel_headroom(struct lwtunnel_state * lwtstate,unsigned int mtu)179 static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
180 unsigned int mtu)
181 {
182 return 0;
183 }
184
lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops * op,unsigned int num)185 static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
186 unsigned int num)
187 {
188 return -EOPNOTSUPP;
189
190 }
191
lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops * op,unsigned int num)192 static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
193 unsigned int num)
194 {
195 return -EOPNOTSUPP;
196 }
197
lwtunnel_valid_encap_type(u16 encap_type,struct netlink_ext_ack * extack)198 static inline int lwtunnel_valid_encap_type(u16 encap_type,
199 struct netlink_ext_ack *extack)
200 {
201 NL_SET_ERR_MSG(extack, "CONFIG_LWTUNNEL is not enabled in this kernel");
202 return -EOPNOTSUPP;
203 }
lwtunnel_valid_encap_type_attr(struct nlattr * attr,int len,struct netlink_ext_ack * extack)204 static inline int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len,
205 struct netlink_ext_ack *extack)
206 {
207 /* return 0 since we are not walking attr looking for
208 * RTA_ENCAP_TYPE attribute on nexthops.
209 */
210 return 0;
211 }
212
lwtunnel_build_state(u16 encap_type,struct nlattr * encap,unsigned int family,const void * cfg,struct lwtunnel_state ** lws,struct netlink_ext_ack * extack)213 static inline int lwtunnel_build_state(u16 encap_type,
214 struct nlattr *encap,
215 unsigned int family, const void *cfg,
216 struct lwtunnel_state **lws,
217 struct netlink_ext_ack *extack)
218 {
219 return -EOPNOTSUPP;
220 }
221
lwtunnel_fill_encap(struct sk_buff * skb,struct lwtunnel_state * lwtstate)222 static inline int lwtunnel_fill_encap(struct sk_buff *skb,
223 struct lwtunnel_state *lwtstate)
224 {
225 return 0;
226 }
227
lwtunnel_get_encap_size(struct lwtunnel_state * lwtstate)228 static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
229 {
230 return 0;
231 }
232
lwtunnel_state_alloc(int hdr_len)233 static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len)
234 {
235 return NULL;
236 }
237
lwtunnel_cmp_encap(struct lwtunnel_state * a,struct lwtunnel_state * b)238 static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a,
239 struct lwtunnel_state *b)
240 {
241 return 0;
242 }
243
lwtunnel_output(struct net * net,struct sock * sk,struct sk_buff * skb)244 static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
245 {
246 return -EOPNOTSUPP;
247 }
248
lwtunnel_input(struct sk_buff * skb)249 static inline int lwtunnel_input(struct sk_buff *skb)
250 {
251 return -EOPNOTSUPP;
252 }
253
lwtunnel_xmit(struct sk_buff * skb)254 static inline int lwtunnel_xmit(struct sk_buff *skb)
255 {
256 return -EOPNOTSUPP;
257 }
258
259 #endif /* CONFIG_LWTUNNEL */
260
261 #define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type))
262
263 #endif /* __NET_LWTUNNEL_H */
264