12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e3e4712eSRoopa Prabhu /*
3e3e4712eSRoopa Prabhu * mpls tunnels An implementation mpls tunnels using the light weight tunnel
4e3e4712eSRoopa Prabhu * infrastructure
5e3e4712eSRoopa Prabhu *
6e3e4712eSRoopa Prabhu * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com>
7e3e4712eSRoopa Prabhu */
8e3e4712eSRoopa Prabhu #include <linux/types.h>
9e3e4712eSRoopa Prabhu #include <linux/skbuff.h>
10e3e4712eSRoopa Prabhu #include <linux/net.h>
11e3e4712eSRoopa Prabhu #include <linux/module.h>
12e3e4712eSRoopa Prabhu #include <linux/mpls.h>
13e3e4712eSRoopa Prabhu #include <linux/vmalloc.h>
14e3e4712eSRoopa Prabhu #include <net/ip.h>
15e3e4712eSRoopa Prabhu #include <net/dst.h>
16e3e4712eSRoopa Prabhu #include <net/lwtunnel.h>
17e3e4712eSRoopa Prabhu #include <net/netevent.h>
18e3e4712eSRoopa Prabhu #include <net/netns/generic.h>
19e3e4712eSRoopa Prabhu #include <net/ip6_fib.h>
20e3e4712eSRoopa Prabhu #include <net/route.h>
21e3e4712eSRoopa Prabhu #include <net/mpls_iptunnel.h>
22e3e4712eSRoopa Prabhu #include <linux/mpls_iptunnel.h>
23e3e4712eSRoopa Prabhu #include "internal.h"
24e3e4712eSRoopa Prabhu
25e3e4712eSRoopa Prabhu static const struct nla_policy mpls_iptunnel_policy[MPLS_IPTUNNEL_MAX + 1] = {
262f3f7d1fSGeorge Wilkie [MPLS_IPTUNNEL_DST] = { .len = sizeof(u32) },
27a59166e4SRobert Shearman [MPLS_IPTUNNEL_TTL] = { .type = NLA_U8 },
28e3e4712eSRoopa Prabhu };
29e3e4712eSRoopa Prabhu
mpls_encap_size(struct mpls_iptunnel_encap * en)30e3e4712eSRoopa Prabhu static unsigned int mpls_encap_size(struct mpls_iptunnel_encap *en)
31e3e4712eSRoopa Prabhu {
32e3e4712eSRoopa Prabhu /* The size of the layer 2.5 labels to be added for this route */
33e3e4712eSRoopa Prabhu return en->labels * sizeof(struct mpls_shim_hdr);
34e3e4712eSRoopa Prabhu }
35e3e4712eSRoopa Prabhu
mpls_xmit(struct sk_buff * skb)3614972cbdSRoopa Prabhu static int mpls_xmit(struct sk_buff *skb)
37e3e4712eSRoopa Prabhu {
38e3e4712eSRoopa Prabhu struct mpls_iptunnel_encap *tun_encap_info;
39e3e4712eSRoopa Prabhu struct mpls_shim_hdr *hdr;
40e3e4712eSRoopa Prabhu struct net_device *out_dev;
41e3e4712eSRoopa Prabhu unsigned int hh_len;
42e3e4712eSRoopa Prabhu unsigned int new_header_size;
43e3e4712eSRoopa Prabhu unsigned int mtu;
44e3e4712eSRoopa Prabhu struct dst_entry *dst = skb_dst(skb);
45e3e4712eSRoopa Prabhu struct rtable *rt = NULL;
46e3e4712eSRoopa Prabhu struct rt6_info *rt6 = NULL;
4727d69105SRobert Shearman struct mpls_dev *out_mdev;
48a59166e4SRobert Shearman struct net *net;
49e3e4712eSRoopa Prabhu int err = 0;
50e3e4712eSRoopa Prabhu bool bos;
51e3e4712eSRoopa Prabhu int i;
52e3e4712eSRoopa Prabhu unsigned int ttl;
53e3e4712eSRoopa Prabhu
5427d69105SRobert Shearman /* Find the output device */
5527d69105SRobert Shearman out_dev = dst->dev;
56a59166e4SRobert Shearman net = dev_net(out_dev);
57e3e4712eSRoopa Prabhu
58e3e4712eSRoopa Prabhu skb_orphan(skb);
59e3e4712eSRoopa Prabhu
60e3e4712eSRoopa Prabhu if (!mpls_output_possible(out_dev) ||
6161adedf3SJiri Benc !dst->lwtstate || skb_warn_if_lro(skb))
62e3e4712eSRoopa Prabhu goto drop;
63e3e4712eSRoopa Prabhu
64e3e4712eSRoopa Prabhu skb_forward_csum(skb);
65e3e4712eSRoopa Prabhu
6661adedf3SJiri Benc tun_encap_info = mpls_lwtunnel_encap(dst->lwtstate);
67e3e4712eSRoopa Prabhu
68a59166e4SRobert Shearman /* Obtain the ttl using the following set of rules.
69a59166e4SRobert Shearman *
70a59166e4SRobert Shearman * LWT ttl propagation setting:
71a59166e4SRobert Shearman * - disabled => use default TTL value from LWT
72a59166e4SRobert Shearman * - enabled => use TTL value from IPv4/IPv6 header
73a59166e4SRobert Shearman * - default =>
74a59166e4SRobert Shearman * Global ttl propagation setting:
75a59166e4SRobert Shearman * - disabled => use default TTL value from global setting
76a59166e4SRobert Shearman * - enabled => use TTL value from IPv4/IPv6 header
77a59166e4SRobert Shearman */
78a59166e4SRobert Shearman if (dst->ops->family == AF_INET) {
79a59166e4SRobert Shearman if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DISABLED)
80a59166e4SRobert Shearman ttl = tun_encap_info->default_ttl;
81a59166e4SRobert Shearman else if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
82a59166e4SRobert Shearman !net->mpls.ip_ttl_propagate)
83a59166e4SRobert Shearman ttl = net->mpls.default_ttl;
84a59166e4SRobert Shearman else
85a59166e4SRobert Shearman ttl = ip_hdr(skb)->ttl;
86a59166e4SRobert Shearman rt = (struct rtable *)dst;
87a59166e4SRobert Shearman } else if (dst->ops->family == AF_INET6) {
88a59166e4SRobert Shearman if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DISABLED)
89a59166e4SRobert Shearman ttl = tun_encap_info->default_ttl;
90a59166e4SRobert Shearman else if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
91a59166e4SRobert Shearman !net->mpls.ip_ttl_propagate)
92a59166e4SRobert Shearman ttl = net->mpls.default_ttl;
93a59166e4SRobert Shearman else
94a59166e4SRobert Shearman ttl = ipv6_hdr(skb)->hop_limit;
95*797a4c1fSEric Dumazet rt6 = dst_rt6_info(dst);
96a59166e4SRobert Shearman } else {
97a59166e4SRobert Shearman goto drop;
98a59166e4SRobert Shearman }
99a59166e4SRobert Shearman
100e3e4712eSRoopa Prabhu /* Verify the destination can hold the packet */
101e3e4712eSRoopa Prabhu new_header_size = mpls_encap_size(tun_encap_info);
102e3e4712eSRoopa Prabhu mtu = mpls_dev_mtu(out_dev);
103e3e4712eSRoopa Prabhu if (mpls_pkt_too_big(skb, mtu - new_header_size))
104e3e4712eSRoopa Prabhu goto drop;
105e3e4712eSRoopa Prabhu
106e3e4712eSRoopa Prabhu hh_len = LL_RESERVED_SPACE(out_dev);
107e3e4712eSRoopa Prabhu if (!out_dev->header_ops)
108e3e4712eSRoopa Prabhu hh_len = 0;
109e3e4712eSRoopa Prabhu
110e3e4712eSRoopa Prabhu /* Ensure there is enough space for the headers in the skb */
111e3e4712eSRoopa Prabhu if (skb_cow(skb, hh_len + new_header_size))
112e3e4712eSRoopa Prabhu goto drop;
113e3e4712eSRoopa Prabhu
11448d2ab60SDavid Ahern skb_set_inner_protocol(skb, skb->protocol);
11548d2ab60SDavid Ahern skb_reset_inner_network_header(skb);
11648d2ab60SDavid Ahern
117e3e4712eSRoopa Prabhu skb_push(skb, new_header_size);
11848d2ab60SDavid Ahern
119e3e4712eSRoopa Prabhu skb_reset_network_header(skb);
120e3e4712eSRoopa Prabhu
121e3e4712eSRoopa Prabhu skb->dev = out_dev;
122e3e4712eSRoopa Prabhu skb->protocol = htons(ETH_P_MPLS_UC);
123e3e4712eSRoopa Prabhu
124e3e4712eSRoopa Prabhu /* Push the new labels */
125e3e4712eSRoopa Prabhu hdr = mpls_hdr(skb);
126e3e4712eSRoopa Prabhu bos = true;
127e3e4712eSRoopa Prabhu for (i = tun_encap_info->labels - 1; i >= 0; i--) {
128e3e4712eSRoopa Prabhu hdr[i] = mpls_entry_encode(tun_encap_info->label[i],
129e3e4712eSRoopa Prabhu ttl, 0, bos);
130e3e4712eSRoopa Prabhu bos = false;
131e3e4712eSRoopa Prabhu }
132e3e4712eSRoopa Prabhu
13327d69105SRobert Shearman mpls_stats_inc_outucastpkts(out_dev, skb);
13427d69105SRobert Shearman
1351550c171SDavid Ahern if (rt) {
136803f3e22SAlexey Kodanev if (rt->rt_gw_family == AF_INET6)
1370f5f7d7bSDavid Ahern err = neigh_xmit(NEIGH_ND_TABLE, out_dev, &rt->rt_gw6,
1380f5f7d7bSDavid Ahern skb);
139803f3e22SAlexey Kodanev else
140803f3e22SAlexey Kodanev err = neigh_xmit(NEIGH_ARP_TABLE, out_dev, &rt->rt_gw4,
141803f3e22SAlexey Kodanev skb);
1421550c171SDavid Ahern } else if (rt6) {
143f84532ceSVinay K Nallamothu if (ipv6_addr_v4mapped(&rt6->rt6i_gateway)) {
144f84532ceSVinay K Nallamothu /* 6PE (RFC 4798) */
145f84532ceSVinay K Nallamothu err = neigh_xmit(NEIGH_ARP_TABLE, out_dev, &rt6->rt6i_gateway.s6_addr32[3],
146f84532ceSVinay K Nallamothu skb);
147f84532ceSVinay K Nallamothu } else
148e3e4712eSRoopa Prabhu err = neigh_xmit(NEIGH_ND_TABLE, out_dev, &rt6->rt6i_gateway,
149e3e4712eSRoopa Prabhu skb);
150f84532ceSVinay K Nallamothu }
151e3e4712eSRoopa Prabhu if (err)
152e3e4712eSRoopa Prabhu net_dbg_ratelimited("%s: packet transmission failed: %d\n",
153e3e4712eSRoopa Prabhu __func__, err);
154e3e4712eSRoopa Prabhu
15514972cbdSRoopa Prabhu return LWTUNNEL_XMIT_DONE;
156e3e4712eSRoopa Prabhu
157e3e4712eSRoopa Prabhu drop:
15827d69105SRobert Shearman out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
15927d69105SRobert Shearman if (out_mdev)
16027d69105SRobert Shearman MPLS_INC_STATS(out_mdev, tx_errors);
161e3e4712eSRoopa Prabhu kfree_skb(skb);
162e3e4712eSRoopa Prabhu return -EINVAL;
163e3e4712eSRoopa Prabhu }
164e3e4712eSRoopa Prabhu
mpls_build_state(struct net * net,struct nlattr * nla,unsigned int family,const void * cfg,struct lwtunnel_state ** ts,struct netlink_ext_ack * extack)165faee6769SAlexander Aring static int mpls_build_state(struct net *net, struct nlattr *nla,
166127eb7cdSTom Herbert unsigned int family, const void *cfg,
1679ae28727SDavid Ahern struct lwtunnel_state **ts,
1689ae28727SDavid Ahern struct netlink_ext_ack *extack)
169e3e4712eSRoopa Prabhu {
170e3e4712eSRoopa Prabhu struct mpls_iptunnel_encap *tun_encap_info;
171e3e4712eSRoopa Prabhu struct nlattr *tb[MPLS_IPTUNNEL_MAX + 1];
172e3e4712eSRoopa Prabhu struct lwtunnel_state *newts;
1731511009cSDavid Ahern u8 n_labels;
174e3e4712eSRoopa Prabhu int ret;
175e3e4712eSRoopa Prabhu
1768cb08174SJohannes Berg ret = nla_parse_nested_deprecated(tb, MPLS_IPTUNNEL_MAX, nla,
1779ae28727SDavid Ahern mpls_iptunnel_policy, extack);
178e3e4712eSRoopa Prabhu if (ret < 0)
179e3e4712eSRoopa Prabhu return ret;
180e3e4712eSRoopa Prabhu
181a1f10abeSDavid Ahern if (!tb[MPLS_IPTUNNEL_DST]) {
182a1f10abeSDavid Ahern NL_SET_ERR_MSG(extack, "MPLS_IPTUNNEL_DST attribute is missing");
183e3e4712eSRoopa Prabhu return -EINVAL;
184a1f10abeSDavid Ahern }
185e3e4712eSRoopa Prabhu
1861511009cSDavid Ahern /* determine number of labels */
187a1f10abeSDavid Ahern if (nla_get_labels(tb[MPLS_IPTUNNEL_DST], MAX_NEW_LABELS,
188a1f10abeSDavid Ahern &n_labels, NULL, extack))
1891511009cSDavid Ahern return -EINVAL;
1901511009cSDavid Ahern
191b4ba9354SGustavo A. R. Silva newts = lwtunnel_state_alloc(struct_size(tun_encap_info, label,
192b4ba9354SGustavo A. R. Silva n_labels));
193e3e4712eSRoopa Prabhu if (!newts)
194e3e4712eSRoopa Prabhu return -ENOMEM;
195e3e4712eSRoopa Prabhu
196e3e4712eSRoopa Prabhu tun_encap_info = mpls_lwtunnel_encap(newts);
1971511009cSDavid Ahern ret = nla_get_labels(tb[MPLS_IPTUNNEL_DST], n_labels,
198a1f10abeSDavid Ahern &tun_encap_info->labels, tun_encap_info->label,
199a1f10abeSDavid Ahern extack);
200e3e4712eSRoopa Prabhu if (ret)
201e3e4712eSRoopa Prabhu goto errout;
202a59166e4SRobert Shearman
203a59166e4SRobert Shearman tun_encap_info->ttl_propagate = MPLS_TTL_PROP_DEFAULT;
204a59166e4SRobert Shearman
205a59166e4SRobert Shearman if (tb[MPLS_IPTUNNEL_TTL]) {
206a59166e4SRobert Shearman tun_encap_info->default_ttl = nla_get_u8(tb[MPLS_IPTUNNEL_TTL]);
207a59166e4SRobert Shearman /* TTL 0 implies propagate from IP header */
208a59166e4SRobert Shearman tun_encap_info->ttl_propagate = tun_encap_info->default_ttl ?
209a59166e4SRobert Shearman MPLS_TTL_PROP_DISABLED :
210a59166e4SRobert Shearman MPLS_TTL_PROP_ENABLED;
211a59166e4SRobert Shearman }
212a59166e4SRobert Shearman
213e3e4712eSRoopa Prabhu newts->type = LWTUNNEL_ENCAP_MPLS;
21414972cbdSRoopa Prabhu newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
21514972cbdSRoopa Prabhu newts->headroom = mpls_encap_size(tun_encap_info);
216e3e4712eSRoopa Prabhu
217e3e4712eSRoopa Prabhu *ts = newts;
218e3e4712eSRoopa Prabhu
219e3e4712eSRoopa Prabhu return 0;
220e3e4712eSRoopa Prabhu
221e3e4712eSRoopa Prabhu errout:
222e3e4712eSRoopa Prabhu kfree(newts);
223e3e4712eSRoopa Prabhu *ts = NULL;
224e3e4712eSRoopa Prabhu
225e3e4712eSRoopa Prabhu return ret;
226e3e4712eSRoopa Prabhu }
227e3e4712eSRoopa Prabhu
mpls_fill_encap_info(struct sk_buff * skb,struct lwtunnel_state * lwtstate)228e3e4712eSRoopa Prabhu static int mpls_fill_encap_info(struct sk_buff *skb,
229e3e4712eSRoopa Prabhu struct lwtunnel_state *lwtstate)
230e3e4712eSRoopa Prabhu {
231e3e4712eSRoopa Prabhu struct mpls_iptunnel_encap *tun_encap_info;
232e3e4712eSRoopa Prabhu
233e3e4712eSRoopa Prabhu tun_encap_info = mpls_lwtunnel_encap(lwtstate);
234e3e4712eSRoopa Prabhu
235e3e4712eSRoopa Prabhu if (nla_put_labels(skb, MPLS_IPTUNNEL_DST, tun_encap_info->labels,
236e3e4712eSRoopa Prabhu tun_encap_info->label))
237e3e4712eSRoopa Prabhu goto nla_put_failure;
238e3e4712eSRoopa Prabhu
239a59166e4SRobert Shearman if (tun_encap_info->ttl_propagate != MPLS_TTL_PROP_DEFAULT &&
240a59166e4SRobert Shearman nla_put_u8(skb, MPLS_IPTUNNEL_TTL, tun_encap_info->default_ttl))
241a59166e4SRobert Shearman goto nla_put_failure;
242a59166e4SRobert Shearman
243e3e4712eSRoopa Prabhu return 0;
244e3e4712eSRoopa Prabhu
245e3e4712eSRoopa Prabhu nla_put_failure:
246e3e4712eSRoopa Prabhu return -EMSGSIZE;
247e3e4712eSRoopa Prabhu }
248e3e4712eSRoopa Prabhu
mpls_encap_nlsize(struct lwtunnel_state * lwtstate)249e3e4712eSRoopa Prabhu static int mpls_encap_nlsize(struct lwtunnel_state *lwtstate)
250e3e4712eSRoopa Prabhu {
251e3e4712eSRoopa Prabhu struct mpls_iptunnel_encap *tun_encap_info;
252a59166e4SRobert Shearman int nlsize;
253e3e4712eSRoopa Prabhu
254e3e4712eSRoopa Prabhu tun_encap_info = mpls_lwtunnel_encap(lwtstate);
255e3e4712eSRoopa Prabhu
256a59166e4SRobert Shearman nlsize = nla_total_size(tun_encap_info->labels * 4);
257a59166e4SRobert Shearman
258a59166e4SRobert Shearman if (tun_encap_info->ttl_propagate != MPLS_TTL_PROP_DEFAULT)
259a59166e4SRobert Shearman nlsize += nla_total_size(1);
260a59166e4SRobert Shearman
261a59166e4SRobert Shearman return nlsize;
262e3e4712eSRoopa Prabhu }
263e3e4712eSRoopa Prabhu
mpls_encap_cmp(struct lwtunnel_state * a,struct lwtunnel_state * b)264e3e4712eSRoopa Prabhu static int mpls_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
265e3e4712eSRoopa Prabhu {
266e3e4712eSRoopa Prabhu struct mpls_iptunnel_encap *a_hdr = mpls_lwtunnel_encap(a);
267e3e4712eSRoopa Prabhu struct mpls_iptunnel_encap *b_hdr = mpls_lwtunnel_encap(b);
268e3e4712eSRoopa Prabhu int l;
269e3e4712eSRoopa Prabhu
270a59166e4SRobert Shearman if (a_hdr->labels != b_hdr->labels ||
271a59166e4SRobert Shearman a_hdr->ttl_propagate != b_hdr->ttl_propagate ||
272a59166e4SRobert Shearman a_hdr->default_ttl != b_hdr->default_ttl)
273e3e4712eSRoopa Prabhu return 1;
274e3e4712eSRoopa Prabhu
2751511009cSDavid Ahern for (l = 0; l < a_hdr->labels; l++)
276e3e4712eSRoopa Prabhu if (a_hdr->label[l] != b_hdr->label[l])
277e3e4712eSRoopa Prabhu return 1;
278e3e4712eSRoopa Prabhu return 0;
279e3e4712eSRoopa Prabhu }
280e3e4712eSRoopa Prabhu
281e3e4712eSRoopa Prabhu static const struct lwtunnel_encap_ops mpls_iptun_ops = {
282e3e4712eSRoopa Prabhu .build_state = mpls_build_state,
28314972cbdSRoopa Prabhu .xmit = mpls_xmit,
284e3e4712eSRoopa Prabhu .fill_encap = mpls_fill_encap_info,
285e3e4712eSRoopa Prabhu .get_encap_size = mpls_encap_nlsize,
286e3e4712eSRoopa Prabhu .cmp_encap = mpls_encap_cmp,
28788ff7334SRobert Shearman .owner = THIS_MODULE,
288e3e4712eSRoopa Prabhu };
289e3e4712eSRoopa Prabhu
mpls_iptunnel_init(void)290e3e4712eSRoopa Prabhu static int __init mpls_iptunnel_init(void)
291e3e4712eSRoopa Prabhu {
292e3e4712eSRoopa Prabhu return lwtunnel_encap_add_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS);
293e3e4712eSRoopa Prabhu }
294e3e4712eSRoopa Prabhu module_init(mpls_iptunnel_init);
295e3e4712eSRoopa Prabhu
mpls_iptunnel_exit(void)296e3e4712eSRoopa Prabhu static void __exit mpls_iptunnel_exit(void)
297e3e4712eSRoopa Prabhu {
298e3e4712eSRoopa Prabhu lwtunnel_encap_del_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS);
299e3e4712eSRoopa Prabhu }
300e3e4712eSRoopa Prabhu module_exit(mpls_iptunnel_exit);
301e3e4712eSRoopa Prabhu
302b2b04edcSRobert Shearman MODULE_ALIAS_RTNL_LWT(MPLS);
303b7c24497SAlexander Ovechkin MODULE_SOFTDEP("post: mpls_gso");
304e3e4712eSRoopa Prabhu MODULE_DESCRIPTION("MultiProtocol Label Switching IP Tunnels");
305e3e4712eSRoopa Prabhu MODULE_LICENSE("GPL v2");
306