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