Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 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 */ |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 15 | #define LWTUNNEL_STATE_OUTPUT_REDIRECT BIT(0) |
| 16 | #define LWTUNNEL_STATE_INPUT_REDIRECT BIT(1) |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 17 | #define LWTUNNEL_STATE_XMIT_REDIRECT BIT(2) |
| 18 | |
| 19 | enum { |
| 20 | LWTUNNEL_XMIT_DONE, |
| 21 | LWTUNNEL_XMIT_CONTINUE, |
| 22 | }; |
| 23 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 24 | |
| 25 | struct lwtunnel_state { |
| 26 | __u16 type; |
| 27 | __u16 flags; |
Thomas Graf | f76a9db | 2016-10-21 16:10:22 +0200 | [diff] [blame] | 28 | __u16 headroom; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 29 | atomic_t refcnt; |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 30 | int (*orig_output)(struct net *net, struct sock *sk, struct sk_buff *skb); |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 31 | int (*orig_input)(struct sk_buff *); |
Tom Herbert | 1104d9b | 2016-10-14 11:25:36 -0700 | [diff] [blame] | 32 | struct rcu_head rcu; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 33 | __u8 data[0]; |
| 34 | }; |
| 35 | |
| 36 | struct lwtunnel_encap_ops { |
David Ahern | 30357d7 | 2017-01-30 12:07:37 -0800 | [diff] [blame] | 37 | int (*build_state)(struct nlattr *encap, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 38 | unsigned int family, const void *cfg, |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 39 | struct lwtunnel_state **ts, |
| 40 | struct netlink_ext_ack *extack); |
Tom Herbert | 1104d9b | 2016-10-14 11:25:36 -0700 | [diff] [blame] | 41 | void (*destroy_state)(struct lwtunnel_state *lws); |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 42 | int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb); |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 43 | int (*input)(struct sk_buff *skb); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 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); |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 48 | int (*xmit)(struct sk_buff *skb); |
Robert Shearman | 88ff733 | 2017-01-24 16:26:47 +0000 | [diff] [blame] | 49 | |
| 50 | struct module *owner; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 53 | #ifdef CONFIG_LWTUNNEL |
Tom Herbert | 1104d9b | 2016-10-14 11:25:36 -0700 | [diff] [blame] | 54 | void lwtstate_free(struct lwtunnel_state *lws); |
Jiri Benc | df383e6 | 2015-08-18 18:41:13 +0200 | [diff] [blame] | 55 | |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 56 | static inline struct lwtunnel_state * |
| 57 | lwtstate_get(struct lwtunnel_state *lws) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 58 | { |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 59 | if (lws) |
| 60 | atomic_inc(&lws->refcnt); |
| 61 | |
| 62 | return lws; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 65 | static inline void lwtstate_put(struct lwtunnel_state *lws) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 66 | { |
| 67 | if (!lws) |
| 68 | return; |
| 69 | |
| 70 | if (atomic_dec_and_test(&lws->refcnt)) |
Jiri Benc | df383e6 | 2015-08-18 18:41:13 +0200 | [diff] [blame] | 71 | lwtstate_free(lws); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate) |
| 75 | { |
| 76 | if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_OUTPUT_REDIRECT)) |
| 77 | return true; |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 82 | static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate) |
| 83 | { |
| 84 | if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_INPUT_REDIRECT)) |
| 85 | return true; |
| 86 | |
| 87 | return false; |
| 88 | } |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 89 | |
| 90 | static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate) |
| 91 | { |
| 92 | if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_XMIT_REDIRECT)) |
| 93 | return true; |
| 94 | |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate, |
| 99 | unsigned int mtu) |
| 100 | { |
David Lebrun | a23a8f5 | 2016-11-16 10:05:46 +0100 | [diff] [blame] | 101 | if ((lwtunnel_xmit_redirect(lwtstate) || |
| 102 | lwtunnel_output_redirect(lwtstate)) && lwtstate->headroom < mtu) |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 103 | return lwtstate->headroom; |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 108 | int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op, |
| 109 | unsigned int num); |
| 110 | int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op, |
| 111 | unsigned int num); |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 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); |
David Ahern | 30357d7 | 2017-01-30 12:07:37 -0800 | [diff] [blame] | 116 | int lwtunnel_build_state(u16 encap_type, |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 117 | struct nlattr *encap, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 118 | unsigned int family, const void *cfg, |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 119 | struct lwtunnel_state **lws, |
| 120 | struct netlink_ext_ack *extack); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 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); |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 126 | int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb); |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 127 | int lwtunnel_input(struct sk_buff *skb); |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 128 | int lwtunnel_xmit(struct sk_buff *skb); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 129 | |
| 130 | #else |
| 131 | |
Ying Xue | 824e738 | 2015-08-19 15:46:17 +0800 | [diff] [blame] | 132 | static inline void lwtstate_free(struct lwtunnel_state *lws) |
| 133 | { |
| 134 | } |
| 135 | |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 136 | static inline struct lwtunnel_state * |
| 137 | lwtstate_get(struct lwtunnel_state *lws) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 138 | { |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 139 | return lws; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Nicolas Dichtel | 5a6228a | 2015-07-24 12:28:36 +0200 | [diff] [blame] | 142 | static inline void lwtstate_put(struct lwtunnel_state *lws) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 143 | { |
| 144 | } |
| 145 | |
| 146 | static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate) |
| 147 | { |
| 148 | return false; |
| 149 | } |
| 150 | |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 151 | static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate) |
| 152 | { |
| 153 | return false; |
| 154 | } |
| 155 | |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 156 | static inline bool lwtunnel_xmit_redirect(struct lwtunnel_state *lwtstate) |
| 157 | { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | static inline unsigned int lwtunnel_headroom(struct lwtunnel_state *lwtstate, |
| 162 | unsigned int mtu) |
| 163 | { |
| 164 | return 0; |
| 165 | } |
| 166 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 167 | static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op, |
| 168 | unsigned int num) |
| 169 | { |
| 170 | return -EOPNOTSUPP; |
| 171 | |
| 172 | } |
| 173 | |
| 174 | static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op, |
| 175 | unsigned int num) |
| 176 | { |
| 177 | return -EOPNOTSUPP; |
| 178 | } |
| 179 | |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 180 | static inline int lwtunnel_valid_encap_type(u16 encap_type, |
| 181 | struct netlink_ext_ack *extack) |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 182 | { |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 183 | NL_SET_ERR_MSG(extack, "CONFIG_LWTUNNEL is not enabled in this kernel"); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 184 | return -EOPNOTSUPP; |
| 185 | } |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 186 | static inline int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int len, |
| 187 | struct netlink_ext_ack *extack) |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 188 | { |
David Ahern | 2bd137d | 2017-02-08 09:29:00 -0800 | [diff] [blame] | 189 | /* return 0 since we are not walking attr looking for |
| 190 | * RTA_ENCAP_TYPE attribute on nexthops. |
| 191 | */ |
| 192 | return 0; |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 193 | } |
| 194 | |
David Ahern | 30357d7 | 2017-01-30 12:07:37 -0800 | [diff] [blame] | 195 | static inline int lwtunnel_build_state(u16 encap_type, |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 196 | struct nlattr *encap, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 197 | unsigned int family, const void *cfg, |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 198 | struct lwtunnel_state **lws, |
| 199 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 200 | { |
| 201 | return -EOPNOTSUPP; |
| 202 | } |
| 203 | |
| 204 | static inline int lwtunnel_fill_encap(struct sk_buff *skb, |
| 205 | struct lwtunnel_state *lwtstate) |
| 206 | { |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate) |
| 211 | { |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len) |
| 216 | { |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a, |
| 221 | struct lwtunnel_state *b) |
| 222 | { |
| 223 | return 0; |
| 224 | } |
| 225 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 226 | static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
Roopa Prabhu | ffce419 | 2015-07-21 10:43:49 +0200 | [diff] [blame] | 227 | { |
| 228 | return -EOPNOTSUPP; |
| 229 | } |
| 230 | |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 231 | static inline int lwtunnel_input(struct sk_buff *skb) |
| 232 | { |
| 233 | return -EOPNOTSUPP; |
| 234 | } |
| 235 | |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 236 | static inline int lwtunnel_xmit(struct sk_buff *skb) |
| 237 | { |
| 238 | return -EOPNOTSUPP; |
| 239 | } |
| 240 | |
Robert Shearman | 745041e | 2016-02-19 09:43:16 +0000 | [diff] [blame] | 241 | #endif /* CONFIG_LWTUNNEL */ |
| 242 | |
| 243 | #define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type)) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 244 | |
| 245 | #endif /* __NET_LWTUNNEL_H */ |