Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * mpls tunnels An implementation mpls tunnels using the light weight tunnel |
| 3 | * infrastructure |
| 4 | * |
| 5 | * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/net.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/mpls.h> |
| 18 | #include <linux/vmalloc.h> |
| 19 | #include <net/ip.h> |
| 20 | #include <net/dst.h> |
| 21 | #include <net/lwtunnel.h> |
| 22 | #include <net/netevent.h> |
| 23 | #include <net/netns/generic.h> |
| 24 | #include <net/ip6_fib.h> |
| 25 | #include <net/route.h> |
| 26 | #include <net/mpls_iptunnel.h> |
| 27 | #include <linux/mpls_iptunnel.h> |
| 28 | #include "internal.h" |
| 29 | |
| 30 | static const struct nla_policy mpls_iptunnel_policy[MPLS_IPTUNNEL_MAX + 1] = { |
| 31 | [MPLS_IPTUNNEL_DST] = { .type = NLA_U32 }, |
Robert Shearman | a59166e | 2017-03-10 20:43:25 +0000 | [diff] [blame] | 32 | [MPLS_IPTUNNEL_TTL] = { .type = NLA_U8 }, |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static unsigned int mpls_encap_size(struct mpls_iptunnel_encap *en) |
| 36 | { |
| 37 | /* The size of the layer 2.5 labels to be added for this route */ |
| 38 | return en->labels * sizeof(struct mpls_shim_hdr); |
| 39 | } |
| 40 | |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 41 | static int mpls_xmit(struct sk_buff *skb) |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 42 | { |
| 43 | struct mpls_iptunnel_encap *tun_encap_info; |
| 44 | struct mpls_shim_hdr *hdr; |
| 45 | struct net_device *out_dev; |
| 46 | unsigned int hh_len; |
| 47 | unsigned int new_header_size; |
| 48 | unsigned int mtu; |
| 49 | struct dst_entry *dst = skb_dst(skb); |
| 50 | struct rtable *rt = NULL; |
| 51 | struct rt6_info *rt6 = NULL; |
Robert Shearman | 27d6910 | 2017-01-16 14:16:37 +0000 | [diff] [blame] | 52 | struct mpls_dev *out_mdev; |
Robert Shearman | a59166e | 2017-03-10 20:43:25 +0000 | [diff] [blame] | 53 | struct net *net; |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 54 | int err = 0; |
| 55 | bool bos; |
| 56 | int i; |
| 57 | unsigned int ttl; |
| 58 | |
Robert Shearman | 27d6910 | 2017-01-16 14:16:37 +0000 | [diff] [blame] | 59 | /* Find the output device */ |
| 60 | out_dev = dst->dev; |
Robert Shearman | a59166e | 2017-03-10 20:43:25 +0000 | [diff] [blame] | 61 | net = dev_net(out_dev); |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 62 | |
| 63 | skb_orphan(skb); |
| 64 | |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 65 | if (!mpls_output_possible(out_dev) || |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 66 | !dst->lwtstate || skb_warn_if_lro(skb)) |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 67 | goto drop; |
| 68 | |
| 69 | skb_forward_csum(skb); |
| 70 | |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 71 | tun_encap_info = mpls_lwtunnel_encap(dst->lwtstate); |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 72 | |
Robert Shearman | a59166e | 2017-03-10 20:43:25 +0000 | [diff] [blame] | 73 | /* Obtain the ttl using the following set of rules. |
| 74 | * |
| 75 | * LWT ttl propagation setting: |
| 76 | * - disabled => use default TTL value from LWT |
| 77 | * - enabled => use TTL value from IPv4/IPv6 header |
| 78 | * - default => |
| 79 | * Global ttl propagation setting: |
| 80 | * - disabled => use default TTL value from global setting |
| 81 | * - enabled => use TTL value from IPv4/IPv6 header |
| 82 | */ |
| 83 | if (dst->ops->family == AF_INET) { |
| 84 | if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DISABLED) |
| 85 | ttl = tun_encap_info->default_ttl; |
| 86 | else if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DEFAULT && |
| 87 | !net->mpls.ip_ttl_propagate) |
| 88 | ttl = net->mpls.default_ttl; |
| 89 | else |
| 90 | ttl = ip_hdr(skb)->ttl; |
| 91 | rt = (struct rtable *)dst; |
| 92 | } else if (dst->ops->family == AF_INET6) { |
| 93 | if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DISABLED) |
| 94 | ttl = tun_encap_info->default_ttl; |
| 95 | else if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DEFAULT && |
| 96 | !net->mpls.ip_ttl_propagate) |
| 97 | ttl = net->mpls.default_ttl; |
| 98 | else |
| 99 | ttl = ipv6_hdr(skb)->hop_limit; |
| 100 | rt6 = (struct rt6_info *)dst; |
| 101 | } else { |
| 102 | goto drop; |
| 103 | } |
| 104 | |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 105 | /* Verify the destination can hold the packet */ |
| 106 | new_header_size = mpls_encap_size(tun_encap_info); |
| 107 | mtu = mpls_dev_mtu(out_dev); |
| 108 | if (mpls_pkt_too_big(skb, mtu - new_header_size)) |
| 109 | goto drop; |
| 110 | |
| 111 | hh_len = LL_RESERVED_SPACE(out_dev); |
| 112 | if (!out_dev->header_ops) |
| 113 | hh_len = 0; |
| 114 | |
| 115 | /* Ensure there is enough space for the headers in the skb */ |
| 116 | if (skb_cow(skb, hh_len + new_header_size)) |
| 117 | goto drop; |
| 118 | |
David Ahern | 48d2ab6 | 2016-08-24 20:10:44 -0700 | [diff] [blame] | 119 | skb_set_inner_protocol(skb, skb->protocol); |
| 120 | skb_reset_inner_network_header(skb); |
| 121 | |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 122 | skb_push(skb, new_header_size); |
David Ahern | 48d2ab6 | 2016-08-24 20:10:44 -0700 | [diff] [blame] | 123 | |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 124 | skb_reset_network_header(skb); |
| 125 | |
| 126 | skb->dev = out_dev; |
| 127 | skb->protocol = htons(ETH_P_MPLS_UC); |
| 128 | |
| 129 | /* Push the new labels */ |
| 130 | hdr = mpls_hdr(skb); |
| 131 | bos = true; |
| 132 | for (i = tun_encap_info->labels - 1; i >= 0; i--) { |
| 133 | hdr[i] = mpls_entry_encode(tun_encap_info->label[i], |
| 134 | ttl, 0, bos); |
| 135 | bos = false; |
| 136 | } |
| 137 | |
Robert Shearman | 27d6910 | 2017-01-16 14:16:37 +0000 | [diff] [blame] | 138 | mpls_stats_inc_outucastpkts(out_dev, skb); |
| 139 | |
David Ahern | 1550c17 | 2019-04-05 16:30:27 -0700 | [diff] [blame] | 140 | if (rt) { |
| 141 | if (rt->rt_gw_family == AF_INET) |
| 142 | err = neigh_xmit(NEIGH_ARP_TABLE, out_dev, &rt->rt_gw4, |
| 143 | skb); |
David Ahern | 0f5f7d7 | 2019-04-05 16:30:29 -0700 | [diff] [blame] | 144 | else if (rt->rt_gw_family == AF_INET6) |
| 145 | err = neigh_xmit(NEIGH_ND_TABLE, out_dev, &rt->rt_gw6, |
| 146 | skb); |
David Ahern | 1550c17 | 2019-04-05 16:30:27 -0700 | [diff] [blame] | 147 | } else if (rt6) { |
Vinay K Nallamothu | f84532c | 2019-03-19 22:41:18 +0000 | [diff] [blame] | 148 | if (ipv6_addr_v4mapped(&rt6->rt6i_gateway)) { |
| 149 | /* 6PE (RFC 4798) */ |
| 150 | err = neigh_xmit(NEIGH_ARP_TABLE, out_dev, &rt6->rt6i_gateway.s6_addr32[3], |
| 151 | skb); |
| 152 | } else |
| 153 | err = neigh_xmit(NEIGH_ND_TABLE, out_dev, &rt6->rt6i_gateway, |
| 154 | skb); |
| 155 | } |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 156 | if (err) |
| 157 | net_dbg_ratelimited("%s: packet transmission failed: %d\n", |
| 158 | __func__, err); |
| 159 | |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 160 | return LWTUNNEL_XMIT_DONE; |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 161 | |
| 162 | drop: |
Robert Shearman | 27d6910 | 2017-01-16 14:16:37 +0000 | [diff] [blame] | 163 | out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL; |
| 164 | if (out_mdev) |
| 165 | MPLS_INC_STATS(out_mdev, tx_errors); |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 166 | kfree_skb(skb); |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | |
David Ahern | 30357d7 | 2017-01-30 12:07:37 -0800 | [diff] [blame] | 170 | static int mpls_build_state(struct nlattr *nla, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 171 | unsigned int family, const void *cfg, |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 172 | struct lwtunnel_state **ts, |
| 173 | struct netlink_ext_ack *extack) |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 174 | { |
| 175 | struct mpls_iptunnel_encap *tun_encap_info; |
| 176 | struct nlattr *tb[MPLS_IPTUNNEL_MAX + 1]; |
| 177 | struct lwtunnel_state *newts; |
David Ahern | 1511009 | 2017-03-31 07:14:04 -0700 | [diff] [blame] | 178 | u8 n_labels; |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 179 | int ret; |
| 180 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 181 | ret = nla_parse_nested_deprecated(tb, MPLS_IPTUNNEL_MAX, nla, |
| 182 | mpls_iptunnel_policy, extack); |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 183 | if (ret < 0) |
| 184 | return ret; |
| 185 | |
David Ahern | a1f10ab | 2017-05-27 16:19:29 -0600 | [diff] [blame] | 186 | if (!tb[MPLS_IPTUNNEL_DST]) { |
| 187 | NL_SET_ERR_MSG(extack, "MPLS_IPTUNNEL_DST attribute is missing"); |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 188 | return -EINVAL; |
David Ahern | a1f10ab | 2017-05-27 16:19:29 -0600 | [diff] [blame] | 189 | } |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 190 | |
David Ahern | 1511009 | 2017-03-31 07:14:04 -0700 | [diff] [blame] | 191 | /* determine number of labels */ |
David Ahern | a1f10ab | 2017-05-27 16:19:29 -0600 | [diff] [blame] | 192 | if (nla_get_labels(tb[MPLS_IPTUNNEL_DST], MAX_NEW_LABELS, |
| 193 | &n_labels, NULL, extack)) |
David Ahern | 1511009 | 2017-03-31 07:14:04 -0700 | [diff] [blame] | 194 | return -EINVAL; |
| 195 | |
Gustavo A. R. Silva | b4ba935 | 2019-02-07 19:10:52 -0600 | [diff] [blame] | 196 | newts = lwtunnel_state_alloc(struct_size(tun_encap_info, label, |
| 197 | n_labels)); |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 198 | if (!newts) |
| 199 | return -ENOMEM; |
| 200 | |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 201 | tun_encap_info = mpls_lwtunnel_encap(newts); |
David Ahern | 1511009 | 2017-03-31 07:14:04 -0700 | [diff] [blame] | 202 | ret = nla_get_labels(tb[MPLS_IPTUNNEL_DST], n_labels, |
David Ahern | a1f10ab | 2017-05-27 16:19:29 -0600 | [diff] [blame] | 203 | &tun_encap_info->labels, tun_encap_info->label, |
| 204 | extack); |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 205 | if (ret) |
| 206 | goto errout; |
Robert Shearman | a59166e | 2017-03-10 20:43:25 +0000 | [diff] [blame] | 207 | |
| 208 | tun_encap_info->ttl_propagate = MPLS_TTL_PROP_DEFAULT; |
| 209 | |
| 210 | if (tb[MPLS_IPTUNNEL_TTL]) { |
| 211 | tun_encap_info->default_ttl = nla_get_u8(tb[MPLS_IPTUNNEL_TTL]); |
| 212 | /* TTL 0 implies propagate from IP header */ |
| 213 | tun_encap_info->ttl_propagate = tun_encap_info->default_ttl ? |
| 214 | MPLS_TTL_PROP_DISABLED : |
| 215 | MPLS_TTL_PROP_ENABLED; |
| 216 | } |
| 217 | |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 218 | newts->type = LWTUNNEL_ENCAP_MPLS; |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 219 | newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT; |
| 220 | newts->headroom = mpls_encap_size(tun_encap_info); |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 221 | |
| 222 | *ts = newts; |
| 223 | |
| 224 | return 0; |
| 225 | |
| 226 | errout: |
| 227 | kfree(newts); |
| 228 | *ts = NULL; |
| 229 | |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | static int mpls_fill_encap_info(struct sk_buff *skb, |
| 234 | struct lwtunnel_state *lwtstate) |
| 235 | { |
| 236 | struct mpls_iptunnel_encap *tun_encap_info; |
Stephen Hemminger | 04c6a3a | 2018-07-24 12:29:05 -0700 | [diff] [blame] | 237 | |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 238 | tun_encap_info = mpls_lwtunnel_encap(lwtstate); |
| 239 | |
| 240 | if (nla_put_labels(skb, MPLS_IPTUNNEL_DST, tun_encap_info->labels, |
| 241 | tun_encap_info->label)) |
| 242 | goto nla_put_failure; |
| 243 | |
Robert Shearman | a59166e | 2017-03-10 20:43:25 +0000 | [diff] [blame] | 244 | if (tun_encap_info->ttl_propagate != MPLS_TTL_PROP_DEFAULT && |
| 245 | nla_put_u8(skb, MPLS_IPTUNNEL_TTL, tun_encap_info->default_ttl)) |
| 246 | goto nla_put_failure; |
| 247 | |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 248 | return 0; |
| 249 | |
| 250 | nla_put_failure: |
| 251 | return -EMSGSIZE; |
| 252 | } |
| 253 | |
| 254 | static int mpls_encap_nlsize(struct lwtunnel_state *lwtstate) |
| 255 | { |
| 256 | struct mpls_iptunnel_encap *tun_encap_info; |
Robert Shearman | a59166e | 2017-03-10 20:43:25 +0000 | [diff] [blame] | 257 | int nlsize; |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 258 | |
| 259 | tun_encap_info = mpls_lwtunnel_encap(lwtstate); |
| 260 | |
Robert Shearman | a59166e | 2017-03-10 20:43:25 +0000 | [diff] [blame] | 261 | nlsize = nla_total_size(tun_encap_info->labels * 4); |
| 262 | |
| 263 | if (tun_encap_info->ttl_propagate != MPLS_TTL_PROP_DEFAULT) |
| 264 | nlsize += nla_total_size(1); |
| 265 | |
| 266 | return nlsize; |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static int mpls_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b) |
| 270 | { |
| 271 | struct mpls_iptunnel_encap *a_hdr = mpls_lwtunnel_encap(a); |
| 272 | struct mpls_iptunnel_encap *b_hdr = mpls_lwtunnel_encap(b); |
| 273 | int l; |
| 274 | |
Robert Shearman | a59166e | 2017-03-10 20:43:25 +0000 | [diff] [blame] | 275 | if (a_hdr->labels != b_hdr->labels || |
| 276 | a_hdr->ttl_propagate != b_hdr->ttl_propagate || |
| 277 | a_hdr->default_ttl != b_hdr->default_ttl) |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 278 | return 1; |
| 279 | |
David Ahern | 1511009 | 2017-03-31 07:14:04 -0700 | [diff] [blame] | 280 | for (l = 0; l < a_hdr->labels; l++) |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 281 | if (a_hdr->label[l] != b_hdr->label[l]) |
| 282 | return 1; |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static const struct lwtunnel_encap_ops mpls_iptun_ops = { |
| 287 | .build_state = mpls_build_state, |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 288 | .xmit = mpls_xmit, |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 289 | .fill_encap = mpls_fill_encap_info, |
| 290 | .get_encap_size = mpls_encap_nlsize, |
| 291 | .cmp_encap = mpls_encap_cmp, |
Robert Shearman | 88ff733 | 2017-01-24 16:26:47 +0000 | [diff] [blame] | 292 | .owner = THIS_MODULE, |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | static int __init mpls_iptunnel_init(void) |
| 296 | { |
| 297 | return lwtunnel_encap_add_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS); |
| 298 | } |
| 299 | module_init(mpls_iptunnel_init); |
| 300 | |
| 301 | static void __exit mpls_iptunnel_exit(void) |
| 302 | { |
| 303 | lwtunnel_encap_del_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS); |
| 304 | } |
| 305 | module_exit(mpls_iptunnel_exit); |
| 306 | |
Robert Shearman | b2b04ed | 2016-02-19 09:43:17 +0000 | [diff] [blame] | 307 | MODULE_ALIAS_RTNL_LWT(MPLS); |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 308 | MODULE_DESCRIPTION("MultiProtocol Label Switching IP Tunnels"); |
| 309 | MODULE_LICENSE("GPL v2"); |