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 }, |
| 32 | }; |
| 33 | |
| 34 | static unsigned int mpls_encap_size(struct mpls_iptunnel_encap *en) |
| 35 | { |
| 36 | /* The size of the layer 2.5 labels to be added for this route */ |
| 37 | return en->labels * sizeof(struct mpls_shim_hdr); |
| 38 | } |
| 39 | |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 40 | static int mpls_xmit(struct sk_buff *skb) |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 41 | { |
| 42 | struct mpls_iptunnel_encap *tun_encap_info; |
| 43 | struct mpls_shim_hdr *hdr; |
| 44 | struct net_device *out_dev; |
| 45 | unsigned int hh_len; |
| 46 | unsigned int new_header_size; |
| 47 | unsigned int mtu; |
| 48 | struct dst_entry *dst = skb_dst(skb); |
| 49 | struct rtable *rt = NULL; |
| 50 | struct rt6_info *rt6 = NULL; |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 51 | int err = 0; |
| 52 | bool bos; |
| 53 | int i; |
| 54 | unsigned int ttl; |
| 55 | |
| 56 | /* Obtain the ttl */ |
Robert Shearman | fe82b33 | 2015-12-07 12:53:15 +0000 | [diff] [blame] | 57 | if (dst->ops->family == AF_INET) { |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 58 | ttl = ip_hdr(skb)->ttl; |
| 59 | rt = (struct rtable *)dst; |
Robert Shearman | fe82b33 | 2015-12-07 12:53:15 +0000 | [diff] [blame] | 60 | } else if (dst->ops->family == AF_INET6) { |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 61 | ttl = ipv6_hdr(skb)->hop_limit; |
| 62 | rt6 = (struct rt6_info *)dst; |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 63 | } else { |
| 64 | goto drop; |
| 65 | } |
| 66 | |
| 67 | skb_orphan(skb); |
| 68 | |
| 69 | /* Find the output device */ |
Roopa Prabhu | de18547 | 2015-07-21 22:49:00 -0700 | [diff] [blame] | 70 | out_dev = dst->dev; |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 71 | if (!mpls_output_possible(out_dev) || |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 72 | !dst->lwtstate || skb_warn_if_lro(skb)) |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 73 | goto drop; |
| 74 | |
| 75 | skb_forward_csum(skb); |
| 76 | |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 77 | tun_encap_info = mpls_lwtunnel_encap(dst->lwtstate); |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 78 | |
| 79 | /* Verify the destination can hold the packet */ |
| 80 | new_header_size = mpls_encap_size(tun_encap_info); |
| 81 | mtu = mpls_dev_mtu(out_dev); |
| 82 | if (mpls_pkt_too_big(skb, mtu - new_header_size)) |
| 83 | goto drop; |
| 84 | |
| 85 | hh_len = LL_RESERVED_SPACE(out_dev); |
| 86 | if (!out_dev->header_ops) |
| 87 | hh_len = 0; |
| 88 | |
| 89 | /* Ensure there is enough space for the headers in the skb */ |
| 90 | if (skb_cow(skb, hh_len + new_header_size)) |
| 91 | goto drop; |
| 92 | |
David Ahern | 48d2ab6 | 2016-08-24 20:10:44 -0700 | [diff] [blame] | 93 | skb_set_inner_protocol(skb, skb->protocol); |
| 94 | skb_reset_inner_network_header(skb); |
| 95 | |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 96 | skb_push(skb, new_header_size); |
David Ahern | 48d2ab6 | 2016-08-24 20:10:44 -0700 | [diff] [blame] | 97 | |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 98 | skb_reset_network_header(skb); |
| 99 | |
| 100 | skb->dev = out_dev; |
| 101 | skb->protocol = htons(ETH_P_MPLS_UC); |
| 102 | |
| 103 | /* Push the new labels */ |
| 104 | hdr = mpls_hdr(skb); |
| 105 | bos = true; |
| 106 | for (i = tun_encap_info->labels - 1; i >= 0; i--) { |
| 107 | hdr[i] = mpls_entry_encode(tun_encap_info->label[i], |
| 108 | ttl, 0, bos); |
| 109 | bos = false; |
| 110 | } |
| 111 | |
| 112 | if (rt) |
| 113 | err = neigh_xmit(NEIGH_ARP_TABLE, out_dev, &rt->rt_gateway, |
| 114 | skb); |
| 115 | else if (rt6) |
| 116 | err = neigh_xmit(NEIGH_ND_TABLE, out_dev, &rt6->rt6i_gateway, |
| 117 | skb); |
| 118 | if (err) |
| 119 | net_dbg_ratelimited("%s: packet transmission failed: %d\n", |
| 120 | __func__, err); |
| 121 | |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 122 | return LWTUNNEL_XMIT_DONE; |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 123 | |
| 124 | drop: |
| 125 | kfree_skb(skb); |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | |
| 129 | static int mpls_build_state(struct net_device *dev, struct nlattr *nla, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 130 | unsigned int family, const void *cfg, |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 131 | struct lwtunnel_state **ts) |
| 132 | { |
| 133 | struct mpls_iptunnel_encap *tun_encap_info; |
| 134 | struct nlattr *tb[MPLS_IPTUNNEL_MAX + 1]; |
| 135 | struct lwtunnel_state *newts; |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 136 | int ret; |
| 137 | |
| 138 | ret = nla_parse_nested(tb, MPLS_IPTUNNEL_MAX, nla, |
| 139 | mpls_iptunnel_policy); |
| 140 | if (ret < 0) |
| 141 | return ret; |
| 142 | |
| 143 | if (!tb[MPLS_IPTUNNEL_DST]) |
| 144 | return -EINVAL; |
| 145 | |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 146 | |
Thomas Graf | f76a9db | 2016-10-21 16:10:22 +0200 | [diff] [blame] | 147 | newts = lwtunnel_state_alloc(sizeof(*tun_encap_info)); |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 148 | if (!newts) |
| 149 | return -ENOMEM; |
| 150 | |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 151 | tun_encap_info = mpls_lwtunnel_encap(newts); |
| 152 | ret = nla_get_labels(tb[MPLS_IPTUNNEL_DST], MAX_NEW_LABELS, |
| 153 | &tun_encap_info->labels, tun_encap_info->label); |
| 154 | if (ret) |
| 155 | goto errout; |
| 156 | newts->type = LWTUNNEL_ENCAP_MPLS; |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 157 | newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT; |
| 158 | newts->headroom = mpls_encap_size(tun_encap_info); |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 159 | |
| 160 | *ts = newts; |
| 161 | |
| 162 | return 0; |
| 163 | |
| 164 | errout: |
| 165 | kfree(newts); |
| 166 | *ts = NULL; |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | static int mpls_fill_encap_info(struct sk_buff *skb, |
| 172 | struct lwtunnel_state *lwtstate) |
| 173 | { |
| 174 | struct mpls_iptunnel_encap *tun_encap_info; |
| 175 | |
| 176 | tun_encap_info = mpls_lwtunnel_encap(lwtstate); |
| 177 | |
| 178 | if (nla_put_labels(skb, MPLS_IPTUNNEL_DST, tun_encap_info->labels, |
| 179 | tun_encap_info->label)) |
| 180 | goto nla_put_failure; |
| 181 | |
| 182 | return 0; |
| 183 | |
| 184 | nla_put_failure: |
| 185 | return -EMSGSIZE; |
| 186 | } |
| 187 | |
| 188 | static int mpls_encap_nlsize(struct lwtunnel_state *lwtstate) |
| 189 | { |
| 190 | struct mpls_iptunnel_encap *tun_encap_info; |
| 191 | |
| 192 | tun_encap_info = mpls_lwtunnel_encap(lwtstate); |
| 193 | |
| 194 | return nla_total_size(tun_encap_info->labels * 4); |
| 195 | } |
| 196 | |
| 197 | static int mpls_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b) |
| 198 | { |
| 199 | struct mpls_iptunnel_encap *a_hdr = mpls_lwtunnel_encap(a); |
| 200 | struct mpls_iptunnel_encap *b_hdr = mpls_lwtunnel_encap(b); |
| 201 | int l; |
| 202 | |
| 203 | if (a_hdr->labels != b_hdr->labels) |
| 204 | return 1; |
| 205 | |
| 206 | for (l = 0; l < MAX_NEW_LABELS; l++) |
| 207 | if (a_hdr->label[l] != b_hdr->label[l]) |
| 208 | return 1; |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static const struct lwtunnel_encap_ops mpls_iptun_ops = { |
| 213 | .build_state = mpls_build_state, |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 214 | .xmit = mpls_xmit, |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 215 | .fill_encap = mpls_fill_encap_info, |
| 216 | .get_encap_size = mpls_encap_nlsize, |
| 217 | .cmp_encap = mpls_encap_cmp, |
| 218 | }; |
| 219 | |
| 220 | static int __init mpls_iptunnel_init(void) |
| 221 | { |
| 222 | return lwtunnel_encap_add_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS); |
| 223 | } |
| 224 | module_init(mpls_iptunnel_init); |
| 225 | |
| 226 | static void __exit mpls_iptunnel_exit(void) |
| 227 | { |
| 228 | lwtunnel_encap_del_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS); |
| 229 | } |
| 230 | module_exit(mpls_iptunnel_exit); |
| 231 | |
Robert Shearman | b2b04ed | 2016-02-19 09:43:17 +0000 | [diff] [blame] | 232 | MODULE_ALIAS_RTNL_LWT(MPLS); |
Roopa Prabhu | e3e4712 | 2015-07-21 10:43:53 +0200 | [diff] [blame] | 233 | MODULE_DESCRIPTION("MultiProtocol Label Switching IP Tunnels"); |
| 234 | MODULE_LICENSE("GPL v2"); |