Linux Audio

Check our new training course

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