blob: 15e1aa708e5007f81fbe1cd1bee49dd40525c0c2 [file] [log] [blame]
Roopa Prabhue3e47122015-07-21 10:43:53 +02001/*
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
30static const struct nla_policy mpls_iptunnel_policy[MPLS_IPTUNNEL_MAX + 1] = {
31 [MPLS_IPTUNNEL_DST] = { .type = NLA_U32 },
Robert Shearmana59166e2017-03-10 20:43:25 +000032 [MPLS_IPTUNNEL_TTL] = { .type = NLA_U8 },
Roopa Prabhue3e47122015-07-21 10:43:53 +020033};
34
35static 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 Prabhu14972cb2016-08-24 20:10:43 -070041static int mpls_xmit(struct sk_buff *skb)
Roopa Prabhue3e47122015-07-21 10:43:53 +020042{
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 Shearman27d69102017-01-16 14:16:37 +000052 struct mpls_dev *out_mdev;
Robert Shearmana59166e2017-03-10 20:43:25 +000053 struct net *net;
Roopa Prabhue3e47122015-07-21 10:43:53 +020054 int err = 0;
55 bool bos;
56 int i;
57 unsigned int ttl;
58
Robert Shearman27d69102017-01-16 14:16:37 +000059 /* Find the output device */
60 out_dev = dst->dev;
Robert Shearmana59166e2017-03-10 20:43:25 +000061 net = dev_net(out_dev);
Roopa Prabhue3e47122015-07-21 10:43:53 +020062
63 skb_orphan(skb);
64
Roopa Prabhue3e47122015-07-21 10:43:53 +020065 if (!mpls_output_possible(out_dev) ||
Jiri Benc61adedf2015-08-20 13:56:25 +020066 !dst->lwtstate || skb_warn_if_lro(skb))
Roopa Prabhue3e47122015-07-21 10:43:53 +020067 goto drop;
68
69 skb_forward_csum(skb);
70
Jiri Benc61adedf2015-08-20 13:56:25 +020071 tun_encap_info = mpls_lwtunnel_encap(dst->lwtstate);
Roopa Prabhue3e47122015-07-21 10:43:53 +020072
Robert Shearmana59166e2017-03-10 20:43:25 +000073 /* 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 Prabhue3e47122015-07-21 10:43:53 +0200105 /* 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 Ahern48d2ab62016-08-24 20:10:44 -0700119 skb_set_inner_protocol(skb, skb->protocol);
120 skb_reset_inner_network_header(skb);
121
Roopa Prabhue3e47122015-07-21 10:43:53 +0200122 skb_push(skb, new_header_size);
David Ahern48d2ab62016-08-24 20:10:44 -0700123
Roopa Prabhue3e47122015-07-21 10:43:53 +0200124 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 Shearman27d69102017-01-16 14:16:37 +0000138 mpls_stats_inc_outucastpkts(out_dev, skb);
139
Roopa Prabhue3e47122015-07-21 10:43:53 +0200140 if (rt)
141 err = neigh_xmit(NEIGH_ARP_TABLE, out_dev, &rt->rt_gateway,
142 skb);
143 else if (rt6)
144 err = neigh_xmit(NEIGH_ND_TABLE, out_dev, &rt6->rt6i_gateway,
145 skb);
146 if (err)
147 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
148 __func__, err);
149
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700150 return LWTUNNEL_XMIT_DONE;
Roopa Prabhue3e47122015-07-21 10:43:53 +0200151
152drop:
Robert Shearman27d69102017-01-16 14:16:37 +0000153 out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
154 if (out_mdev)
155 MPLS_INC_STATS(out_mdev, tx_errors);
Roopa Prabhue3e47122015-07-21 10:43:53 +0200156 kfree_skb(skb);
157 return -EINVAL;
158}
159
David Ahern30357d72017-01-30 12:07:37 -0800160static int mpls_build_state(struct nlattr *nla,
Tom Herbert127eb7c2015-08-24 09:45:41 -0700161 unsigned int family, const void *cfg,
David Ahern9ae28722017-05-27 16:19:28 -0600162 struct lwtunnel_state **ts,
163 struct netlink_ext_ack *extack)
Roopa Prabhue3e47122015-07-21 10:43:53 +0200164{
165 struct mpls_iptunnel_encap *tun_encap_info;
166 struct nlattr *tb[MPLS_IPTUNNEL_MAX + 1];
167 struct lwtunnel_state *newts;
David Ahern15110092017-03-31 07:14:04 -0700168 u8 n_labels;
Roopa Prabhue3e47122015-07-21 10:43:53 +0200169 int ret;
170
171 ret = nla_parse_nested(tb, MPLS_IPTUNNEL_MAX, nla,
David Ahern9ae28722017-05-27 16:19:28 -0600172 mpls_iptunnel_policy, extack);
Roopa Prabhue3e47122015-07-21 10:43:53 +0200173 if (ret < 0)
174 return ret;
175
176 if (!tb[MPLS_IPTUNNEL_DST])
177 return -EINVAL;
178
Roopa Prabhue3e47122015-07-21 10:43:53 +0200179
David Ahern15110092017-03-31 07:14:04 -0700180 /* determine number of labels */
181 if (nla_get_labels(tb[MPLS_IPTUNNEL_DST],
182 MAX_NEW_LABELS, &n_labels, NULL))
183 return -EINVAL;
184
185 newts = lwtunnel_state_alloc(sizeof(*tun_encap_info) +
186 n_labels * sizeof(u32));
Roopa Prabhue3e47122015-07-21 10:43:53 +0200187 if (!newts)
188 return -ENOMEM;
189
Roopa Prabhue3e47122015-07-21 10:43:53 +0200190 tun_encap_info = mpls_lwtunnel_encap(newts);
David Ahern15110092017-03-31 07:14:04 -0700191 ret = nla_get_labels(tb[MPLS_IPTUNNEL_DST], n_labels,
Roopa Prabhue3e47122015-07-21 10:43:53 +0200192 &tun_encap_info->labels, tun_encap_info->label);
193 if (ret)
194 goto errout;
Robert Shearmana59166e2017-03-10 20:43:25 +0000195
196 tun_encap_info->ttl_propagate = MPLS_TTL_PROP_DEFAULT;
197
198 if (tb[MPLS_IPTUNNEL_TTL]) {
199 tun_encap_info->default_ttl = nla_get_u8(tb[MPLS_IPTUNNEL_TTL]);
200 /* TTL 0 implies propagate from IP header */
201 tun_encap_info->ttl_propagate = tun_encap_info->default_ttl ?
202 MPLS_TTL_PROP_DISABLED :
203 MPLS_TTL_PROP_ENABLED;
204 }
205
Roopa Prabhue3e47122015-07-21 10:43:53 +0200206 newts->type = LWTUNNEL_ENCAP_MPLS;
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700207 newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
208 newts->headroom = mpls_encap_size(tun_encap_info);
Roopa Prabhue3e47122015-07-21 10:43:53 +0200209
210 *ts = newts;
211
212 return 0;
213
214errout:
215 kfree(newts);
216 *ts = NULL;
217
218 return ret;
219}
220
221static int mpls_fill_encap_info(struct sk_buff *skb,
222 struct lwtunnel_state *lwtstate)
223{
224 struct mpls_iptunnel_encap *tun_encap_info;
225
226 tun_encap_info = mpls_lwtunnel_encap(lwtstate);
227
228 if (nla_put_labels(skb, MPLS_IPTUNNEL_DST, tun_encap_info->labels,
229 tun_encap_info->label))
230 goto nla_put_failure;
231
Robert Shearmana59166e2017-03-10 20:43:25 +0000232 if (tun_encap_info->ttl_propagate != MPLS_TTL_PROP_DEFAULT &&
233 nla_put_u8(skb, MPLS_IPTUNNEL_TTL, tun_encap_info->default_ttl))
234 goto nla_put_failure;
235
Roopa Prabhue3e47122015-07-21 10:43:53 +0200236 return 0;
237
238nla_put_failure:
239 return -EMSGSIZE;
240}
241
242static int mpls_encap_nlsize(struct lwtunnel_state *lwtstate)
243{
244 struct mpls_iptunnel_encap *tun_encap_info;
Robert Shearmana59166e2017-03-10 20:43:25 +0000245 int nlsize;
Roopa Prabhue3e47122015-07-21 10:43:53 +0200246
247 tun_encap_info = mpls_lwtunnel_encap(lwtstate);
248
Robert Shearmana59166e2017-03-10 20:43:25 +0000249 nlsize = nla_total_size(tun_encap_info->labels * 4);
250
251 if (tun_encap_info->ttl_propagate != MPLS_TTL_PROP_DEFAULT)
252 nlsize += nla_total_size(1);
253
254 return nlsize;
Roopa Prabhue3e47122015-07-21 10:43:53 +0200255}
256
257static int mpls_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
258{
259 struct mpls_iptunnel_encap *a_hdr = mpls_lwtunnel_encap(a);
260 struct mpls_iptunnel_encap *b_hdr = mpls_lwtunnel_encap(b);
261 int l;
262
Robert Shearmana59166e2017-03-10 20:43:25 +0000263 if (a_hdr->labels != b_hdr->labels ||
264 a_hdr->ttl_propagate != b_hdr->ttl_propagate ||
265 a_hdr->default_ttl != b_hdr->default_ttl)
Roopa Prabhue3e47122015-07-21 10:43:53 +0200266 return 1;
267
David Ahern15110092017-03-31 07:14:04 -0700268 for (l = 0; l < a_hdr->labels; l++)
Roopa Prabhue3e47122015-07-21 10:43:53 +0200269 if (a_hdr->label[l] != b_hdr->label[l])
270 return 1;
271 return 0;
272}
273
274static const struct lwtunnel_encap_ops mpls_iptun_ops = {
275 .build_state = mpls_build_state,
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700276 .xmit = mpls_xmit,
Roopa Prabhue3e47122015-07-21 10:43:53 +0200277 .fill_encap = mpls_fill_encap_info,
278 .get_encap_size = mpls_encap_nlsize,
279 .cmp_encap = mpls_encap_cmp,
Robert Shearman88ff7332017-01-24 16:26:47 +0000280 .owner = THIS_MODULE,
Roopa Prabhue3e47122015-07-21 10:43:53 +0200281};
282
283static int __init mpls_iptunnel_init(void)
284{
285 return lwtunnel_encap_add_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS);
286}
287module_init(mpls_iptunnel_init);
288
289static void __exit mpls_iptunnel_exit(void)
290{
291 lwtunnel_encap_del_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS);
292}
293module_exit(mpls_iptunnel_exit);
294
Robert Shearmanb2b04ed2016-02-19 09:43:17 +0000295MODULE_ALIAS_RTNL_LWT(MPLS);
Roopa Prabhue3e47122015-07-21 10:43:53 +0200296MODULE_DESCRIPTION("MultiProtocol Label Switching IP Tunnels");
297MODULE_LICENSE("GPL v2");