0001
0002 #ifndef __NET_LWTUNNEL_H
0003 #define __NET_LWTUNNEL_H 1
0004
0005 #include <linux/lwtunnel.h>
0006 #include <linux/netdevice.h>
0007 #include <linux/skbuff.h>
0008 #include <linux/types.h>
0009 #include <net/route.h>
0010
0011 #define LWTUNNEL_HASH_BITS 7
0012 #define LWTUNNEL_HASH_SIZE (1 << LWTUNNEL_HASH_BITS)
0013
0014
0015 #define LWTUNNEL_STATE_OUTPUT_REDIRECT BIT(0)
0016 #define LWTUNNEL_STATE_INPUT_REDIRECT BIT(1)
0017 #define LWTUNNEL_STATE_XMIT_REDIRECT BIT(2)
0018
0019 enum {
0020 LWTUNNEL_XMIT_DONE,
0021 LWTUNNEL_XMIT_CONTINUE,
0022 };
0023
0024
0025 struct lwtunnel_state {
0026 __u16 type;
0027 __u16 flags;
0028 __u16 headroom;
0029 atomic_t refcnt;
0030 int (*orig_output)(struct net *net, struct sock *sk, struct sk_buff *skb);
0031 int (*orig_input)(struct sk_buff *);
0032 struct rcu_head rcu;
0033 __u8 data[];
0034 };
0035
0036 struct lwtunnel_encap_ops {
0037 int (*build_state)(struct net *net, struct nlattr *encap,
0038 unsigned int family, const void *cfg,
0039 struct lwtunnel_state **ts,
0040 struct netlink_ext_ack *extack);
0041 void (*destroy_state)(struct lwtunnel_state *lws);
0042 int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
0043 int (*input)(struct sk_buff *skb);
0044 int (*fill_encap)(struct sk_buff *skb,
0045 struct lwtunnel_state *lwtstate);
0046 int (*get_encap_size)(struct lwtunnel_state *lwtstate);
0047 int (*cmp_encap)(struct lwtunnel_state *a, struct lwtunnel_state *b);
0048 int (*xmit)(struct sk_buff *skb);
0049
0050 struct module *owner;
0051 };
0052
0053 #ifdef CONFIG_LWTUNNEL
0054
0055 DECLARE_STATIC_KEY_FALSE(nf_hooks_lwtunnel_enabled);
0056
0057 void lwtstate_free(struct lwtunnel_state *lws);
0058
0059 static inline struct lwtunnel_state *
0060 lwtstate_get(struct lwtunnel_state *lws)
0061 {
0062 if (lws)
0063 atomic_inc(&lws->refcnt);
0064
0065 return lws;
0066 }
0067
0068 static inline void lwtstate_put(struct lwtunnel_state *lws)
0069 {
0070 if (!lws)
0071 return;
0072
0073 if (atomic_dec_and_test(&lws->refcnt))
0074 lwtstate_free(lws);
0075 }
0076
0077 static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
0078 {
0079 if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_OUTPUT_REDIRECT))
0080 return true;
0081
0082 return false;
0083 }
0084
0085 static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
0086 {
0087 if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_INPUT_REDIRECT))
0088 return true;
0089
0090 return false;
0091 }
0092
0093 static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate)
0094 {
0095 if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_XMIT_REDIRECT))
0096 return true;
0097
0098 return false;
0099 }
0100
0101 static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
0102 unsigned int mtu)
0103 {
0104 if ((lwtunnel_xmit_redirect(lwtstate) ||
0105 lwtunnel_output_redirect(lwtstate)) && lwtstate->headroom < mtu)
0106 return lwtstate->headroom;
0107
0108 return 0;
0109 }
0110
0111 int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
0112 unsigned int num);
0113 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
0114 unsigned int num);
0115 int lwtunnel_valid_encap_type(u16 encap_type,
0116 struct netlink_ext_ack *extack);
0117 int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len,
0118 struct netlink_ext_ack *extack);
0119 int lwtunnel_build_state(struct net *net, u16 encap_type,
0120 struct nlattr *encap,
0121 unsigned int family, const void *cfg,
0122 struct lwtunnel_state **lws,
0123 struct netlink_ext_ack *extack);
0124 int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate,
0125 int encap_attr, int encap_type_attr);
0126 int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate);
0127 struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len);
0128 int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b);
0129 int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb);
0130 int lwtunnel_input(struct sk_buff *skb);
0131 int lwtunnel_xmit(struct sk_buff *skb);
0132 int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
0133 bool ingress);
0134
0135 static inline void lwtunnel_set_redirect(struct dst_entry *dst)
0136 {
0137 if (lwtunnel_output_redirect(dst->lwtstate)) {
0138 dst->lwtstate->orig_output = dst->output;
0139 dst->output = lwtunnel_output;
0140 }
0141 if (lwtunnel_input_redirect(dst->lwtstate)) {
0142 dst->lwtstate->orig_input = dst->input;
0143 dst->input = lwtunnel_input;
0144 }
0145 }
0146 #else
0147
0148 static inline void lwtstate_free(struct lwtunnel_state *lws)
0149 {
0150 }
0151
0152 static inline struct lwtunnel_state *
0153 lwtstate_get(struct lwtunnel_state *lws)
0154 {
0155 return lws;
0156 }
0157
0158 static inline void lwtstate_put(struct lwtunnel_state *lws)
0159 {
0160 }
0161
0162 static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
0163 {
0164 return false;
0165 }
0166
0167 static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
0168 {
0169 return false;
0170 }
0171
0172 static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate)
0173 {
0174 return false;
0175 }
0176
0177 static inline void lwtunnel_set_redirect(struct dst_entry *dst)
0178 {
0179 }
0180
0181 static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate,
0182 unsigned int mtu)
0183 {
0184 return 0;
0185 }
0186
0187 static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
0188 unsigned int num)
0189 {
0190 return -EOPNOTSUPP;
0191
0192 }
0193
0194 static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
0195 unsigned int num)
0196 {
0197 return -EOPNOTSUPP;
0198 }
0199
0200 static inline int lwtunnel_valid_encap_type(u16 encap_type,
0201 struct netlink_ext_ack *extack)
0202 {
0203 NL_SET_ERR_MSG(extack, "CONFIG_LWTUNNEL is not enabled in this kernel");
0204 return -EOPNOTSUPP;
0205 }
0206 static inline int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len,
0207 struct netlink_ext_ack *extack)
0208 {
0209
0210
0211
0212 return 0;
0213 }
0214
0215 static inline int lwtunnel_build_state(struct net *net, u16 encap_type,
0216 struct nlattr *encap,
0217 unsigned int family, const void *cfg,
0218 struct lwtunnel_state **lws,
0219 struct netlink_ext_ack *extack)
0220 {
0221 return -EOPNOTSUPP;
0222 }
0223
0224 static inline int lwtunnel_fill_encap(struct sk_buff *skb,
0225 struct lwtunnel_state *lwtstate,
0226 int encap_attr, int encap_type_attr)
0227 {
0228 return 0;
0229 }
0230
0231 static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
0232 {
0233 return 0;
0234 }
0235
0236 static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len)
0237 {
0238 return NULL;
0239 }
0240
0241 static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a,
0242 struct lwtunnel_state *b)
0243 {
0244 return 0;
0245 }
0246
0247 static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
0248 {
0249 return -EOPNOTSUPP;
0250 }
0251
0252 static inline int lwtunnel_input(struct sk_buff *skb)
0253 {
0254 return -EOPNOTSUPP;
0255 }
0256
0257 static inline int lwtunnel_xmit(struct sk_buff *skb)
0258 {
0259 return -EOPNOTSUPP;
0260 }
0261
0262 #endif
0263
0264 #define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type))
0265
0266 #endif