Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 2 | /* |
| 3 | * lwtunnel Infrastructure for light weight tunnels like mpls |
| 4 | * |
| 5 | * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com> |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/capability.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/uaccess.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/lwtunnel.h> |
| 17 | #include <linux/in.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/err.h> |
| 20 | |
| 21 | #include <net/lwtunnel.h> |
| 22 | #include <net/rtnetlink.h> |
Roopa Prabhu | ffce419 | 2015-07-21 10:43:49 +0200 | [diff] [blame] | 23 | #include <net/ip6_fib.h> |
David Ahern | 3c618c1 | 2019-04-20 09:28:20 -0700 | [diff] [blame] | 24 | #include <net/rtnh.h> |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 25 | |
Robert Shearman | 745041e | 2016-02-19 09:43:16 +0000 | [diff] [blame] | 26 | #ifdef CONFIG_MODULES |
| 27 | |
| 28 | static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type) |
| 29 | { |
| 30 | /* Only lwt encaps implemented without using an interface for |
| 31 | * the encap need to return a string here. |
| 32 | */ |
| 33 | switch (encap_type) { |
| 34 | case LWTUNNEL_ENCAP_MPLS: |
| 35 | return "MPLS"; |
| 36 | case LWTUNNEL_ENCAP_ILA: |
| 37 | return "ILA"; |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 38 | case LWTUNNEL_ENCAP_SEG6: |
| 39 | return "SEG6"; |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 40 | case LWTUNNEL_ENCAP_BPF: |
| 41 | return "BPF"; |
David Lebrun | d1df6fd | 2017-08-05 12:38:26 +0200 | [diff] [blame] | 42 | case LWTUNNEL_ENCAP_SEG6_LOCAL: |
| 43 | return "SEG6LOCAL"; |
Alexander Aring | a7a29f9 | 2020-03-27 18:00:22 -0400 | [diff] [blame^] | 44 | case LWTUNNEL_ENCAP_RPL: |
| 45 | return "RPL"; |
Robert Shearman | 745041e | 2016-02-19 09:43:16 +0000 | [diff] [blame] | 46 | case LWTUNNEL_ENCAP_IP6: |
| 47 | case LWTUNNEL_ENCAP_IP: |
| 48 | case LWTUNNEL_ENCAP_NONE: |
| 49 | case __LWTUNNEL_ENCAP_MAX: |
| 50 | /* should not have got here */ |
| 51 | WARN_ON(1); |
| 52 | break; |
| 53 | } |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | #endif /* CONFIG_MODULES */ |
| 58 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 59 | struct lwtunnel_state *lwtunnel_state_alloc(int encap_len) |
| 60 | { |
| 61 | struct lwtunnel_state *lws; |
| 62 | |
| 63 | lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC); |
| 64 | |
| 65 | return lws; |
| 66 | } |
Roopa Prabhu | 08bd10f | 2017-08-04 18:19:18 -0700 | [diff] [blame] | 67 | EXPORT_SYMBOL_GPL(lwtunnel_state_alloc); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 68 | |
Thomas Graf | 92a99bf | 2015-07-29 09:45:40 +0200 | [diff] [blame] | 69 | static const struct lwtunnel_encap_ops __rcu * |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 70 | lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly; |
| 71 | |
| 72 | int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops, |
| 73 | unsigned int num) |
| 74 | { |
| 75 | if (num > LWTUNNEL_ENCAP_MAX) |
| 76 | return -ERANGE; |
| 77 | |
| 78 | return !cmpxchg((const struct lwtunnel_encap_ops **) |
| 79 | &lwtun_encaps[num], |
| 80 | NULL, ops) ? 0 : -1; |
| 81 | } |
Roopa Prabhu | 08bd10f | 2017-08-04 18:19:18 -0700 | [diff] [blame] | 82 | EXPORT_SYMBOL_GPL(lwtunnel_encap_add_ops); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 83 | |
| 84 | int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops, |
| 85 | unsigned int encap_type) |
| 86 | { |
| 87 | int ret; |
| 88 | |
| 89 | if (encap_type == LWTUNNEL_ENCAP_NONE || |
| 90 | encap_type > LWTUNNEL_ENCAP_MAX) |
| 91 | return -ERANGE; |
| 92 | |
| 93 | ret = (cmpxchg((const struct lwtunnel_encap_ops **) |
| 94 | &lwtun_encaps[encap_type], |
| 95 | ops, NULL) == ops) ? 0 : -1; |
| 96 | |
| 97 | synchronize_net(); |
| 98 | |
| 99 | return ret; |
| 100 | } |
Roopa Prabhu | 08bd10f | 2017-08-04 18:19:18 -0700 | [diff] [blame] | 101 | EXPORT_SYMBOL_GPL(lwtunnel_encap_del_ops); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 102 | |
Alexander Aring | faee676 | 2020-03-27 18:00:21 -0400 | [diff] [blame] | 103 | int lwtunnel_build_state(struct net *net, u16 encap_type, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 104 | struct nlattr *encap, unsigned int family, |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 105 | const void *cfg, struct lwtunnel_state **lws, |
| 106 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 107 | { |
| 108 | const struct lwtunnel_encap_ops *ops; |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 109 | bool found = false; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 110 | int ret = -EINVAL; |
| 111 | |
| 112 | if (encap_type == LWTUNNEL_ENCAP_NONE || |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 113 | encap_type > LWTUNNEL_ENCAP_MAX) { |
| 114 | NL_SET_ERR_MSG_ATTR(extack, encap, |
| 115 | "Unknown LWT encapsulation type"); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 116 | return ret; |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 117 | } |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 118 | |
| 119 | ret = -EOPNOTSUPP; |
| 120 | rcu_read_lock(); |
| 121 | ops = rcu_dereference(lwtun_encaps[encap_type]); |
wenxu | 3d25eab | 2019-02-23 21:32:54 +0800 | [diff] [blame] | 122 | if (likely(ops && ops->build_state && try_module_get(ops->owner))) |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 123 | found = true; |
wenxu | 3d25eab | 2019-02-23 21:32:54 +0800 | [diff] [blame] | 124 | rcu_read_unlock(); |
| 125 | |
| 126 | if (found) { |
Alexander Aring | faee676 | 2020-03-27 18:00:21 -0400 | [diff] [blame] | 127 | ret = ops->build_state(net, encap, family, cfg, lws, extack); |
Robert Shearman | 85c8140 | 2017-01-24 16:26:48 +0000 | [diff] [blame] | 128 | if (ret) |
| 129 | module_put(ops->owner); |
wenxu | 3d25eab | 2019-02-23 21:32:54 +0800 | [diff] [blame] | 130 | } else { |
| 131 | /* don't rely on -EOPNOTSUPP to detect match as build_state |
| 132 | * handlers could return it |
| 133 | */ |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 134 | NL_SET_ERR_MSG_ATTR(extack, encap, |
| 135 | "LWT encapsulation type not supported"); |
| 136 | } |
| 137 | |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 138 | return ret; |
| 139 | } |
Roopa Prabhu | 08bd10f | 2017-08-04 18:19:18 -0700 | [diff] [blame] | 140 | EXPORT_SYMBOL_GPL(lwtunnel_build_state); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 141 | |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 142 | int lwtunnel_valid_encap_type(u16 encap_type, struct netlink_ext_ack *extack) |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 143 | { |
| 144 | const struct lwtunnel_encap_ops *ops; |
| 145 | int ret = -EINVAL; |
| 146 | |
| 147 | if (encap_type == LWTUNNEL_ENCAP_NONE || |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 148 | encap_type > LWTUNNEL_ENCAP_MAX) { |
| 149 | NL_SET_ERR_MSG(extack, "Unknown lwt encapsulation type"); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 150 | return ret; |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 151 | } |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 152 | |
| 153 | rcu_read_lock(); |
| 154 | ops = rcu_dereference(lwtun_encaps[encap_type]); |
| 155 | rcu_read_unlock(); |
| 156 | #ifdef CONFIG_MODULES |
| 157 | if (!ops) { |
| 158 | const char *encap_type_str = lwtunnel_encap_str(encap_type); |
| 159 | |
| 160 | if (encap_type_str) { |
| 161 | __rtnl_unlock(); |
| 162 | request_module("rtnl-lwt-%s", encap_type_str); |
| 163 | rtnl_lock(); |
| 164 | |
| 165 | rcu_read_lock(); |
| 166 | ops = rcu_dereference(lwtun_encaps[encap_type]); |
| 167 | rcu_read_unlock(); |
| 168 | } |
| 169 | } |
| 170 | #endif |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 171 | ret = ops ? 0 : -EOPNOTSUPP; |
| 172 | if (ret < 0) |
| 173 | NL_SET_ERR_MSG(extack, "lwt encapsulation type not supported"); |
| 174 | |
| 175 | return ret; |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 176 | } |
Roopa Prabhu | 08bd10f | 2017-08-04 18:19:18 -0700 | [diff] [blame] | 177 | EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 178 | |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 179 | int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining, |
| 180 | struct netlink_ext_ack *extack) |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 181 | { |
| 182 | struct rtnexthop *rtnh = (struct rtnexthop *)attr; |
| 183 | struct nlattr *nla_entype; |
| 184 | struct nlattr *attrs; |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 185 | u16 encap_type; |
| 186 | int attrlen; |
| 187 | |
| 188 | while (rtnh_ok(rtnh, remaining)) { |
| 189 | attrlen = rtnh_attrlen(rtnh); |
| 190 | if (attrlen > 0) { |
| 191 | attrs = rtnh_attrs(rtnh); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 192 | nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); |
| 193 | |
| 194 | if (nla_entype) { |
| 195 | encap_type = nla_get_u16(nla_entype); |
| 196 | |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 197 | if (lwtunnel_valid_encap_type(encap_type, |
| 198 | extack) != 0) |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 199 | return -EOPNOTSUPP; |
| 200 | } |
| 201 | } |
| 202 | rtnh = rtnh_next(rtnh, &remaining); |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | } |
Roopa Prabhu | 08bd10f | 2017-08-04 18:19:18 -0700 | [diff] [blame] | 207 | EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type_attr); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 208 | |
Tom Herbert | 1104d9b | 2016-10-14 11:25:36 -0700 | [diff] [blame] | 209 | void lwtstate_free(struct lwtunnel_state *lws) |
| 210 | { |
| 211 | const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type]; |
| 212 | |
| 213 | if (ops->destroy_state) { |
| 214 | ops->destroy_state(lws); |
| 215 | kfree_rcu(lws, rcu); |
| 216 | } else { |
| 217 | kfree(lws); |
| 218 | } |
Robert Shearman | 85c8140 | 2017-01-24 16:26:48 +0000 | [diff] [blame] | 219 | module_put(ops->owner); |
Tom Herbert | 1104d9b | 2016-10-14 11:25:36 -0700 | [diff] [blame] | 220 | } |
Roopa Prabhu | 08bd10f | 2017-08-04 18:19:18 -0700 | [diff] [blame] | 221 | EXPORT_SYMBOL_GPL(lwtstate_free); |
Tom Herbert | 1104d9b | 2016-10-14 11:25:36 -0700 | [diff] [blame] | 222 | |
David Ahern | ffa8ce5 | 2019-04-23 08:23:41 -0700 | [diff] [blame] | 223 | int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate, |
| 224 | int encap_attr, int encap_type_attr) |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 225 | { |
| 226 | const struct lwtunnel_encap_ops *ops; |
| 227 | struct nlattr *nest; |
Dan Carpenter | 39f3709 | 2017-04-28 16:03:48 +0300 | [diff] [blame] | 228 | int ret; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 229 | |
| 230 | if (!lwtstate) |
| 231 | return 0; |
| 232 | |
| 233 | if (lwtstate->type == LWTUNNEL_ENCAP_NONE || |
| 234 | lwtstate->type > LWTUNNEL_ENCAP_MAX) |
| 235 | return 0; |
| 236 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 237 | nest = nla_nest_start_noflag(skb, encap_attr); |
Pan Bian | a50fe0f | 2017-04-23 14:28:37 +0800 | [diff] [blame] | 238 | if (!nest) |
Dan Carpenter | 39f3709 | 2017-04-28 16:03:48 +0300 | [diff] [blame] | 239 | return -EMSGSIZE; |
| 240 | |
| 241 | ret = -EOPNOTSUPP; |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 242 | rcu_read_lock(); |
| 243 | ops = rcu_dereference(lwtun_encaps[lwtstate->type]); |
| 244 | if (likely(ops && ops->fill_encap)) |
| 245 | ret = ops->fill_encap(skb, lwtstate); |
| 246 | rcu_read_unlock(); |
| 247 | |
| 248 | if (ret) |
| 249 | goto nla_put_failure; |
| 250 | nla_nest_end(skb, nest); |
David Ahern | ffa8ce5 | 2019-04-23 08:23:41 -0700 | [diff] [blame] | 251 | ret = nla_put_u16(skb, encap_type_attr, lwtstate->type); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 252 | if (ret) |
| 253 | goto nla_put_failure; |
| 254 | |
| 255 | return 0; |
| 256 | |
| 257 | nla_put_failure: |
| 258 | nla_nest_cancel(skb, nest); |
| 259 | |
| 260 | return (ret == -EOPNOTSUPP ? 0 : ret); |
| 261 | } |
Roopa Prabhu | 08bd10f | 2017-08-04 18:19:18 -0700 | [diff] [blame] | 262 | EXPORT_SYMBOL_GPL(lwtunnel_fill_encap); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 263 | |
| 264 | int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate) |
| 265 | { |
| 266 | const struct lwtunnel_encap_ops *ops; |
| 267 | int ret = 0; |
| 268 | |
| 269 | if (!lwtstate) |
| 270 | return 0; |
| 271 | |
| 272 | if (lwtstate->type == LWTUNNEL_ENCAP_NONE || |
| 273 | lwtstate->type > LWTUNNEL_ENCAP_MAX) |
| 274 | return 0; |
| 275 | |
| 276 | rcu_read_lock(); |
| 277 | ops = rcu_dereference(lwtun_encaps[lwtstate->type]); |
| 278 | if (likely(ops && ops->get_encap_size)) |
| 279 | ret = nla_total_size(ops->get_encap_size(lwtstate)); |
| 280 | rcu_read_unlock(); |
| 281 | |
| 282 | return ret; |
| 283 | } |
Roopa Prabhu | 08bd10f | 2017-08-04 18:19:18 -0700 | [diff] [blame] | 284 | EXPORT_SYMBOL_GPL(lwtunnel_get_encap_size); |
Roopa Prabhu | 499a242 | 2015-07-21 10:43:46 +0200 | [diff] [blame] | 285 | |
| 286 | int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b) |
| 287 | { |
| 288 | const struct lwtunnel_encap_ops *ops; |
| 289 | int ret = 0; |
| 290 | |
| 291 | if (!a && !b) |
| 292 | return 0; |
| 293 | |
| 294 | if (!a || !b) |
| 295 | return 1; |
| 296 | |
| 297 | if (a->type != b->type) |
| 298 | return 1; |
| 299 | |
| 300 | if (a->type == LWTUNNEL_ENCAP_NONE || |
| 301 | a->type > LWTUNNEL_ENCAP_MAX) |
| 302 | return 0; |
| 303 | |
| 304 | rcu_read_lock(); |
| 305 | ops = rcu_dereference(lwtun_encaps[a->type]); |
| 306 | if (likely(ops && ops->cmp_encap)) |
| 307 | ret = ops->cmp_encap(a, b); |
| 308 | rcu_read_unlock(); |
| 309 | |
| 310 | return ret; |
| 311 | } |
Roopa Prabhu | 08bd10f | 2017-08-04 18:19:18 -0700 | [diff] [blame] | 312 | EXPORT_SYMBOL_GPL(lwtunnel_cmp_encap); |
Roopa Prabhu | ffce419 | 2015-07-21 10:43:49 +0200 | [diff] [blame] | 313 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 314 | 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] | 315 | { |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 316 | struct dst_entry *dst = skb_dst(skb); |
Roopa Prabhu | ffce419 | 2015-07-21 10:43:49 +0200 | [diff] [blame] | 317 | const struct lwtunnel_encap_ops *ops; |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 318 | struct lwtunnel_state *lwtstate; |
Roopa Prabhu | ffce419 | 2015-07-21 10:43:49 +0200 | [diff] [blame] | 319 | int ret = -EINVAL; |
| 320 | |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 321 | if (!dst) |
Roopa Prabhu | ffce419 | 2015-07-21 10:43:49 +0200 | [diff] [blame] | 322 | goto drop; |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 323 | lwtstate = dst->lwtstate; |
Roopa Prabhu | ffce419 | 2015-07-21 10:43:49 +0200 | [diff] [blame] | 324 | |
| 325 | if (lwtstate->type == LWTUNNEL_ENCAP_NONE || |
| 326 | lwtstate->type > LWTUNNEL_ENCAP_MAX) |
| 327 | return 0; |
| 328 | |
| 329 | ret = -EOPNOTSUPP; |
| 330 | rcu_read_lock(); |
| 331 | ops = rcu_dereference(lwtun_encaps[lwtstate->type]); |
| 332 | if (likely(ops && ops->output)) |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 333 | ret = ops->output(net, sk, skb); |
Roopa Prabhu | ffce419 | 2015-07-21 10:43:49 +0200 | [diff] [blame] | 334 | rcu_read_unlock(); |
| 335 | |
| 336 | if (ret == -EOPNOTSUPP) |
| 337 | goto drop; |
| 338 | |
| 339 | return ret; |
| 340 | |
| 341 | drop: |
Dan Carpenter | e11f40b | 2015-07-27 11:07:47 +0300 | [diff] [blame] | 342 | kfree_skb(skb); |
Roopa Prabhu | ffce419 | 2015-07-21 10:43:49 +0200 | [diff] [blame] | 343 | |
| 344 | return ret; |
| 345 | } |
Roopa Prabhu | 08bd10f | 2017-08-04 18:19:18 -0700 | [diff] [blame] | 346 | EXPORT_SYMBOL_GPL(lwtunnel_output); |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 347 | |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 348 | int lwtunnel_xmit(struct sk_buff *skb) |
| 349 | { |
| 350 | struct dst_entry *dst = skb_dst(skb); |
| 351 | const struct lwtunnel_encap_ops *ops; |
| 352 | struct lwtunnel_state *lwtstate; |
| 353 | int ret = -EINVAL; |
| 354 | |
| 355 | if (!dst) |
| 356 | goto drop; |
| 357 | |
| 358 | lwtstate = dst->lwtstate; |
| 359 | |
| 360 | if (lwtstate->type == LWTUNNEL_ENCAP_NONE || |
| 361 | lwtstate->type > LWTUNNEL_ENCAP_MAX) |
| 362 | return 0; |
| 363 | |
| 364 | ret = -EOPNOTSUPP; |
| 365 | rcu_read_lock(); |
| 366 | ops = rcu_dereference(lwtun_encaps[lwtstate->type]); |
| 367 | if (likely(ops && ops->xmit)) |
| 368 | ret = ops->xmit(skb); |
| 369 | rcu_read_unlock(); |
| 370 | |
| 371 | if (ret == -EOPNOTSUPP) |
| 372 | goto drop; |
| 373 | |
| 374 | return ret; |
| 375 | |
| 376 | drop: |
| 377 | kfree_skb(skb); |
| 378 | |
| 379 | return ret; |
| 380 | } |
Roopa Prabhu | 08bd10f | 2017-08-04 18:19:18 -0700 | [diff] [blame] | 381 | EXPORT_SYMBOL_GPL(lwtunnel_xmit); |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 382 | |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 383 | int lwtunnel_input(struct sk_buff *skb) |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 384 | { |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 385 | struct dst_entry *dst = skb_dst(skb); |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 386 | const struct lwtunnel_encap_ops *ops; |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 387 | struct lwtunnel_state *lwtstate; |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 388 | int ret = -EINVAL; |
| 389 | |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 390 | if (!dst) |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 391 | goto drop; |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 392 | lwtstate = dst->lwtstate; |
Tom Herbert | 2536862 | 2015-08-17 13:42:24 -0700 | [diff] [blame] | 393 | |
| 394 | if (lwtstate->type == LWTUNNEL_ENCAP_NONE || |
| 395 | lwtstate->type > LWTUNNEL_ENCAP_MAX) |
| 396 | return 0; |
| 397 | |
| 398 | ret = -EOPNOTSUPP; |
| 399 | rcu_read_lock(); |
| 400 | ops = rcu_dereference(lwtun_encaps[lwtstate->type]); |
| 401 | if (likely(ops && ops->input)) |
| 402 | ret = ops->input(skb); |
| 403 | rcu_read_unlock(); |
| 404 | |
| 405 | if (ret == -EOPNOTSUPP) |
| 406 | goto drop; |
| 407 | |
| 408 | return ret; |
| 409 | |
| 410 | drop: |
| 411 | kfree_skb(skb); |
| 412 | |
| 413 | return ret; |
| 414 | } |
Roopa Prabhu | 08bd10f | 2017-08-04 18:19:18 -0700 | [diff] [blame] | 415 | EXPORT_SYMBOL_GPL(lwtunnel_input); |