1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_LWTUNNEL_H
3 #define __NET_LWTUNNEL_H 1
5 #include <linux/lwtunnel.h>
6 #include <linux/netdevice.h>
7 #include <linux/skbuff.h>
8 #include <linux/types.h>
11 #define LWTUNNEL_HASH_BITS 7
12 #define LWTUNNEL_HASH_SIZE (1 << LWTUNNEL_HASH_BITS)
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)
21 LWTUNNEL_XMIT_CONTINUE
,
25 struct lwtunnel_state
{
30 int (*orig_output
)(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
);
31 int (*orig_input
)(struct sk_buff
*);
36 struct lwtunnel_encap_ops
{
37 int (*build_state
)(struct net
*net
, struct nlattr
*encap
,
38 unsigned int family
, const void *cfg
,
39 struct lwtunnel_state
**ts
,
40 struct netlink_ext_ack
*extack
);
41 void (*destroy_state
)(struct lwtunnel_state
*lws
);
42 int (*output
)(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
);
43 int (*input
)(struct sk_buff
*skb
);
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
);
48 int (*xmit
)(struct sk_buff
*skb
);
53 #ifdef CONFIG_LWTUNNEL
55 DECLARE_STATIC_KEY_FALSE(nf_hooks_lwtunnel_enabled
);
57 void lwtstate_free(struct lwtunnel_state
*lws
);
59 static inline struct lwtunnel_state
*
60 lwtstate_get(struct lwtunnel_state
*lws
)
63 atomic_inc(&lws
->refcnt
);
68 static inline void lwtstate_put(struct lwtunnel_state
*lws
)
73 if (atomic_dec_and_test(&lws
->refcnt
))
77 static inline bool lwtunnel_output_redirect(struct lwtunnel_state
*lwtstate
)
79 if (lwtstate
&& (lwtstate
->flags
& LWTUNNEL_STATE_OUTPUT_REDIRECT
))
85 static inline bool lwtunnel_input_redirect(struct lwtunnel_state
*lwtstate
)
87 if (lwtstate
&& (lwtstate
->flags
& LWTUNNEL_STATE_INPUT_REDIRECT
))
93 static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state
*lwtstate
)
95 if (lwtstate
&& (lwtstate
->flags
& LWTUNNEL_STATE_XMIT_REDIRECT
))
101 static inline unsigned int lwtunnel_headroom(struct lwtunnel_state
*lwtstate
,
104 if ((lwtunnel_xmit_redirect(lwtstate
) ||
105 lwtunnel_output_redirect(lwtstate
)) && lwtstate
->headroom
< mtu
)
106 return lwtstate
->headroom
;
111 int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops
*op
,
113 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops
*op
,
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(struct net
*net
, 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
, struct lwtunnel_state
*lwtstate
,
125 int encap_attr
, int encap_type_attr
);
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 int bpf_lwt_push_ip_encap(struct sk_buff
*skb
, void *hdr
, u32 len
,
135 static inline void lwtunnel_set_redirect(struct dst_entry
*dst
)
137 if (lwtunnel_output_redirect(dst
->lwtstate
)) {
138 dst
->lwtstate
->orig_output
= dst
->output
;
139 dst
->output
= lwtunnel_output
;
141 if (lwtunnel_input_redirect(dst
->lwtstate
)) {
142 dst
->lwtstate
->orig_input
= dst
->input
;
143 dst
->input
= lwtunnel_input
;
148 static inline void lwtstate_free(struct lwtunnel_state
*lws
)
152 static inline struct lwtunnel_state
*
153 lwtstate_get(struct lwtunnel_state
*lws
)
158 static inline void lwtstate_put(struct lwtunnel_state
*lws
)
162 static inline bool lwtunnel_output_redirect(struct lwtunnel_state
*lwtstate
)
167 static inline bool lwtunnel_input_redirect(struct lwtunnel_state
*lwtstate
)
172 static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state
*lwtstate
)
177 static inline void lwtunnel_set_redirect(struct dst_entry
*dst
)
181 static inline unsigned int lwtunnel_headroom(struct lwtunnel_state
*lwtstate
,
187 static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops
*op
,
194 static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops
*op
,
200 static inline int lwtunnel_valid_encap_type(u16 encap_type
,
201 struct netlink_ext_ack
*extack
)
203 NL_SET_ERR_MSG(extack
, "CONFIG_LWTUNNEL is not enabled in this kernel");
206 static inline int lwtunnel_valid_encap_type_attr(struct nlattr
*attr
, int len
,
207 struct netlink_ext_ack
*extack
)
209 /* return 0 since we are not walking attr looking for
210 * RTA_ENCAP_TYPE attribute on nexthops.
215 static inline int lwtunnel_build_state(struct net
*net
, u16 encap_type
,
216 struct nlattr
*encap
,
217 unsigned int family
, const void *cfg
,
218 struct lwtunnel_state
**lws
,
219 struct netlink_ext_ack
*extack
)
224 static inline int lwtunnel_fill_encap(struct sk_buff
*skb
,
225 struct lwtunnel_state
*lwtstate
,
226 int encap_attr
, int encap_type_attr
)
231 static inline int lwtunnel_get_encap_size(struct lwtunnel_state
*lwtstate
)
236 static inline struct lwtunnel_state
*lwtunnel_state_alloc(int hdr_len
)
241 static inline int lwtunnel_cmp_encap(struct lwtunnel_state
*a
,
242 struct lwtunnel_state
*b
)
247 static inline int lwtunnel_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
252 static inline int lwtunnel_input(struct sk_buff
*skb
)
257 static inline int lwtunnel_xmit(struct sk_buff
*skb
)
262 #endif /* CONFIG_LWTUNNEL */
264 #define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type))
266 #endif /* __NET_LWTUNNEL_H */