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