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 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
54 void lwtstate_free(struct lwtunnel_state
*lws
);
56 static inline struct lwtunnel_state
*
57 lwtstate_get(struct lwtunnel_state
*lws
)
60 atomic_inc(&lws
->refcnt
);
65 static inline void lwtstate_put(struct lwtunnel_state
*lws
)
70 if (atomic_dec_and_test(&lws
->refcnt
))
74 static inline bool lwtunnel_output_redirect(struct lwtunnel_state
*lwtstate
)
76 if (lwtstate
&& (lwtstate
->flags
& LWTUNNEL_STATE_OUTPUT_REDIRECT
))
82 static inline bool lwtunnel_input_redirect(struct lwtunnel_state
*lwtstate
)
84 if (lwtstate
&& (lwtstate
->flags
& LWTUNNEL_STATE_INPUT_REDIRECT
))
90 static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state
*lwtstate
)
92 if (lwtstate
&& (lwtstate
->flags
& LWTUNNEL_STATE_XMIT_REDIRECT
))
98 static inline unsigned int lwtunnel_headroom(struct lwtunnel_state
*lwtstate
,
101 if ((lwtunnel_xmit_redirect(lwtstate
) ||
102 lwtunnel_output_redirect(lwtstate
)) && lwtstate
->headroom
< mtu
)
103 return lwtstate
->headroom
;
108 int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops
*op
,
110 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops
*op
,
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
);
116 int lwtunnel_build_state(u16 encap_type
,
117 struct nlattr
*encap
,
118 unsigned int family
, const void *cfg
,
119 struct lwtunnel_state
**lws
,
120 struct netlink_ext_ack
*extack
);
121 int lwtunnel_fill_encap(struct sk_buff
*skb
,
122 struct lwtunnel_state
*lwtstate
);
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
);
126 int lwtunnel_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
);
127 int lwtunnel_input(struct sk_buff
*skb
);
128 int lwtunnel_xmit(struct sk_buff
*skb
);
130 static inline void lwtunnel_set_redirect(struct dst_entry
*dst
)
132 if (lwtunnel_output_redirect(dst
->lwtstate
)) {
133 dst
->lwtstate
->orig_output
= dst
->output
;
134 dst
->output
= lwtunnel_output
;
136 if (lwtunnel_input_redirect(dst
->lwtstate
)) {
137 dst
->lwtstate
->orig_input
= dst
->input
;
138 dst
->input
= lwtunnel_input
;
143 static inline void lwtstate_free(struct lwtunnel_state
*lws
)
147 static inline struct lwtunnel_state
*
148 lwtstate_get(struct lwtunnel_state
*lws
)
153 static inline void lwtstate_put(struct lwtunnel_state
*lws
)
157 static inline bool lwtunnel_output_redirect(struct lwtunnel_state
*lwtstate
)
162 static inline bool lwtunnel_input_redirect(struct lwtunnel_state
*lwtstate
)
167 static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state
*lwtstate
)
172 static inline void lwtunnel_set_redirect(struct dst_entry
*dst
)
176 static inline unsigned int lwtunnel_headroom(struct lwtunnel_state
*lwtstate
,
182 static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops
*op
,
189 static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops
*op
,
195 static inline int lwtunnel_valid_encap_type(u16 encap_type
,
196 struct netlink_ext_ack
*extack
)
198 NL_SET_ERR_MSG(extack
, "CONFIG_LWTUNNEL is not enabled in this kernel");
201 static inline int lwtunnel_valid_encap_type_attr(struct nlattr
*attr
, int len
,
202 struct netlink_ext_ack
*extack
)
204 /* return 0 since we are not walking attr looking for
205 * RTA_ENCAP_TYPE attribute on nexthops.
210 static inline int lwtunnel_build_state(u16 encap_type
,
211 struct nlattr
*encap
,
212 unsigned int family
, const void *cfg
,
213 struct lwtunnel_state
**lws
,
214 struct netlink_ext_ack
*extack
)
219 static inline int lwtunnel_fill_encap(struct sk_buff
*skb
,
220 struct lwtunnel_state
*lwtstate
)
225 static inline int lwtunnel_get_encap_size(struct lwtunnel_state
*lwtstate
)
230 static inline struct lwtunnel_state
*lwtunnel_state_alloc(int hdr_len
)
235 static inline int lwtunnel_cmp_encap(struct lwtunnel_state
*a
,
236 struct lwtunnel_state
*b
)
241 static inline int lwtunnel_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
246 static inline int lwtunnel_input(struct sk_buff
*skb
)
251 static inline int lwtunnel_xmit(struct sk_buff
*skb
)
256 #endif /* CONFIG_LWTUNNEL */
258 #define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type))
260 #endif /* __NET_LWTUNNEL_H */