cregit-Linux how code gets into the kernel

Release 4.8 net/mpls/af_mpls.c

Directory: net/mpls
#include <linux/types.h>
#include <linux/skbuff.h>
#include <linux/socket.h>
#include <linux/sysctl.h>
#include <linux/net.h>
#include <linux/module.h>
#include <linux/if_arp.h>
#include <linux/ipv6.h>
#include <linux/mpls.h>
#include <linux/vmalloc.h>
#include <net/ip.h>
#include <net/dst.h>
#include <net/sock.h>
#include <net/arp.h>
#include <net/ip_fib.h>
#include <net/netevent.h>
#include <net/netns/generic.h>
#if IS_ENABLED(CONFIG_IPV6)
#include <net/ipv6.h>
#include <net/addrconf.h>
#endif
#include <net/nexthop.h>
#include "internal.h"

/* Maximum number of labels to look ahead at when selecting a path of
 * a multipath route
 */

#define MAX_MP_SELECT_LABELS 4


#define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)


static int zero = 0;

static int label_limit = (1 << 20) - 1;

static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
		       struct nlmsghdr *nlh, struct net *net, u32 portid,
		       unsigned int nlm_flags);


static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index) { struct mpls_route *rt = NULL; if (index < net->mpls.platform_labels) { struct mpls_route __rcu **platform_label = rcu_dereference(net->mpls.platform_label); rt = rcu_dereference(platform_label[index]); } return rt; }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman64100.00%1100.00%
Total64100.00%1100.00%


static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev) { return rcu_dereference_rtnl(dev->mpls_ptr); }

Contributors

PersonTokensPropCommitsCommitProp
robert shearmanrobert shearman23100.00%1100.00%
Total23100.00%1100.00%


bool mpls_output_possible(const struct net_device *dev) { return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev); }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman27100.00%1100.00%
Total27100.00%1100.00%

EXPORT_SYMBOL_GPL(mpls_output_possible);
static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh) { u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN); int nh_index = nh - rt->rt_nh; return nh0_via + rt->rt_max_alen * nh_index; }

Contributors

PersonTokensPropCommitsCommitProp
robert shearmanrobert shearman58100.00%1100.00%
Total58100.00%1100.00%


static const u8 *mpls_nh_via(const struct mpls_route *rt, const struct mpls_nh *nh) { return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh); }

Contributors

PersonTokensPropCommitsCommitProp
robert shearmanrobert shearman38100.00%1100.00%
Total38100.00%1100.00%


static unsigned int mpls_nh_header_size(const struct mpls_nh *nh) { /* The size of the layer 2.5 labels to be added for this route */ return nh->nh_labels * sizeof(struct mpls_shim_hdr); }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman2080.00%150.00%
roopa prabhuroopa prabhu520.00%150.00%
Total25100.00%2100.00%


unsigned int mpls_dev_mtu(const struct net_device *dev) { /* The amount of data the layer 2 frame can hold */ return dev->mtu; }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman18100.00%1100.00%
Total18100.00%1100.00%

EXPORT_SYMBOL_GPL(mpls_dev_mtu);
bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu) { if (skb->len <= mtu) return false; if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu)) return false; return true; }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman4393.48%150.00%
marcelo ricardo leitnermarcelo ricardo leitner36.52%150.00%
Total46100.00%2100.00%

EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb, bool bos) { struct mpls_entry_decoded dec; struct mpls_shim_hdr *hdr; bool eli_seen = false; int label_index; u32 hash = 0; for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos; label_index++) { if (!pskb_may_pull(skb, sizeof(*hdr) * label_index)) break; /* Read and decode the current label */ hdr = mpls_hdr(skb) + label_index; dec = mpls_entry_decode(hdr); /* RFC6790 - reserved labels MUST NOT be used as keys * for the load-balancing function */ if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) { hash = jhash_1word(dec.label, hash); /* The entropy label follows the entropy label * indicator, so this means that the entropy * label was just added to the hash - no need to * go any deeper either in the label stack or in the * payload */ if (eli_seen) break; } else if (dec.label == MPLS_LABEL_ENTROPY) { eli_seen = true; } bos = dec.bos; if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index + sizeof(struct iphdr))) { const struct iphdr *v4hdr; v4hdr = (const struct iphdr *)(mpls_hdr(skb) + label_index); if (v4hdr->version == 4) { hash = jhash_3words(ntohl(v4hdr->saddr), ntohl(v4hdr->daddr), v4hdr->protocol, hash); } else if (v4hdr->version == 6 && pskb_may_pull(skb, sizeof(*hdr) * label_index + sizeof(struct ipv6hdr))) { const struct ipv6hdr *v6hdr; v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) + label_index); hash = __ipv6_addr_jhash(&v6hdr->saddr, hash); hash = __ipv6_addr_jhash(&v6hdr->daddr, hash); hash = jhash_1word(v6hdr->nexthdr, hash); } } } return hash; }

Contributors

PersonTokensPropCommitsCommitProp
robert shearmanrobert shearman30595.61%133.33%
roopa prabhuroopa prabhu144.39%266.67%
Total319100.00%3100.00%


static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt, struct sk_buff *skb, bool bos) { int alive = ACCESS_ONCE(rt->rt_nhn_alive); u32 hash = 0; int nh_index = 0; int n = 0; /* No need to look further into packet if there's only * one path */ if (rt->rt_nhn == 1) goto out; if (alive <= 0) return NULL; hash = mpls_multipath_hash(rt, skb, bos); nh_index = hash % alive; if (alive == rt->rt_nhn) goto out; for_nexthops(rt) { if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) continue; if (n == nh_index) return nh; n++; } endfor_nexthops(rt); out: return &rt->rt_nh[nh_index]; }

Contributors

PersonTokensPropCommitsCommitProp
roopa prabhuroopa prabhu13192.25%266.67%
robert shearmanrobert shearman117.75%133.33%
Total142100.00%3100.00%


static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb, struct mpls_entry_decoded dec) { enum mpls_payload_type payload_type; bool success = false; /* The IPv4 code below accesses through the IPv4 header * checksum, which is 12 bytes into the packet. * The IPv6 code below accesses through the IPv6 hop limit * which is 8 bytes into the packet. * * For all supported cases there should always be at least 12 * bytes of packet data present. The IPv4 header is 20 bytes * without options and the IPv6 header is always 40 bytes * long. */ if (!pskb_may_pull(skb, 12)) return false; payload_type = rt->rt_payload_type; if (payload_type == MPT_UNSPEC) payload_type = ip_hdr(skb)->version; switch (payload_type) { case MPT_IPV4: { struct iphdr *hdr4 = ip_hdr(skb); skb->protocol = htons(ETH_P_IP); csum_replace2(&hdr4->check, htons(hdr4->ttl << 8), htons(dec.ttl << 8)); hdr4->ttl = dec.ttl; success = true; break; } case MPT_IPV6: { struct ipv6hdr *hdr6 = ipv6_hdr(skb); skb->protocol = htons(ETH_P_IPV6); hdr6->hop_limit = dec.ttl; success = true; break; } case MPT_UNSPEC: break; } return success; }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman12671.19%266.67%
robert shearmanrobert shearman5128.81%133.33%
Total177100.00%3100.00%


static int mpls_forward(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) { struct net *net = dev_net(dev); struct mpls_shim_hdr *hdr; struct mpls_route *rt; struct mpls_nh *nh; struct mpls_entry_decoded dec; struct net_device *out_dev; struct mpls_dev *mdev; unsigned int hh_len; unsigned int new_header_size; unsigned int mtu; int err; /* Careful this entire function runs inside of an rcu critical section */ mdev = mpls_dev_get(dev); if (!mdev || !mdev->input_enabled) goto drop; if (skb->pkt_type != PACKET_HOST) goto drop; if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) goto drop; if (!pskb_may_pull(skb, sizeof(*hdr))) goto drop; /* Read and decode the label */ hdr = mpls_hdr(skb); dec = mpls_entry_decode(hdr); /* Pop the label */ skb_pull(skb, sizeof(*hdr)); skb_reset_network_header(skb); skb_orphan(skb); rt = mpls_route_input_rcu(net, dec.label); if (!rt) goto drop; nh = mpls_select_multipath(rt, skb, dec.bos); if (!nh) goto drop; /* Find the output device */ out_dev = rcu_dereference(nh->nh_dev); if (!mpls_output_possible(out_dev)) goto drop; if (skb_warn_if_lro(skb)) goto drop; skb_forward_csum(skb); /* Verify ttl is valid */ if (dec.ttl <= 1) goto drop; dec.ttl -= 1; /* Verify the destination can hold the packet */ new_header_size = mpls_nh_header_size(nh); mtu = mpls_dev_mtu(out_dev); if (mpls_pkt_too_big(skb, mtu - new_header_size)) goto drop; hh_len = LL_RESERVED_SPACE(out_dev); if (!out_dev->header_ops) hh_len = 0; /* Ensure there is enough space for the headers in the skb */ if (skb_cow(skb, hh_len + new_header_size)) goto drop; skb->dev = out_dev; skb->protocol = htons(ETH_P_MPLS_UC); if (unlikely(!new_header_size && dec.bos)) { /* Penultimate hop popping */ if (!mpls_egress(rt, skb, dec)) goto drop; } else { bool bos; int i; skb_push(skb, new_header_size); skb_reset_network_header(skb); /* Push the new labels */ hdr = mpls_hdr(skb); bos = dec.bos; for (i = nh->nh_labels - 1; i >= 0; i--) { hdr[i] = mpls_entry_encode(nh->nh_label[i], dec.ttl, 0, bos); bos = false; } } /* If via wasn't specified then send out using device address */ if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC) err = neigh_xmit(NEIGH_LINK_TABLE, out_dev, out_dev->dev_addr, skb); else err = neigh_xmit(nh->nh_via_table, out_dev, mpls_nh_via(rt, nh), skb); if (err) net_dbg_ratelimited("%s: packet transmission failed: %d\n", __func__, err); return 0; drop: kfree_skb(skb); return NET_RX_DROP; }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman44582.87%333.33%
robert shearmanrobert shearman6111.36%555.56%
roopa prabhuroopa prabhu315.77%111.11%
Total537100.00%9100.00%

static struct packet_type mpls_packet_type __read_mostly = { .type = cpu_to_be16(ETH_P_MPLS_UC), .func = mpls_forward, }; static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = { [RTA_DST] = { .type = NLA_U32 }, [RTA_OIF] = { .type = NLA_U32 }, }; struct mpls_route_config { u32 rc_protocol; u32 rc_ifindex; u8 rc_via_table; u8 rc_via_alen; u8 rc_via[MAX_VIA_ALEN]; u32 rc_label; u8 rc_output_labels; u32 rc_output_label[MAX_NEW_LABELS]; u32 rc_nlflags; enum mpls_payload_type rc_payload_type; struct nl_info rc_nlinfo; struct rtnexthop *rc_mp; int rc_mp_len; };
static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen) { u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN); struct mpls_route *rt; rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh), VIA_ALEN_ALIGN) + num_nh * max_alen_aligned, GFP_KERNEL); if (rt) { rt->rt_nhn = num_nh; rt->rt_nhn_alive = num_nh; rt->rt_max_alen = max_alen_aligned; } return rt; }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman3944.32%240.00%
robert shearmanrobert shearman3438.64%120.00%
roopa prabhuroopa prabhu1517.05%240.00%
Total88100.00%5100.00%


static void mpls_rt_free(struct mpls_route *rt) { if (rt) kfree_rcu(rt, rt_rcu); }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman22100.00%1100.00%
Total22100.00%1100.00%


static void mpls_notify_route(struct net *net, unsigned index, struct mpls_route *old, struct mpls_route *new, const struct nl_info *info) { struct nlmsghdr *nlh = info ? info->nlh : NULL; unsigned portid = info ? info->portid : 0; int event = new ? RTM_NEWROUTE : RTM_DELROUTE; struct mpls_route *rt = new ? new : old; unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0; /* Ignore reserved labels for now */ if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED)) rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags); }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman11499.13%150.00%
robert shearmanrobert shearman10.87%150.00%
Total115100.00%2100.00%


static void mpls_route_update(struct net *net, unsigned index, struct mpls_route *new, const struct nl_info *info) { struct mpls_route __rcu **platform_label; struct mpls_route *rt; ASSERT_RTNL(); platform_label = rtnl_dereference(net->mpls.platform_label); rt = rtnl_dereference(platform_label[index]); rcu_assign_pointer(platform_label[index], new); mpls_notify_route(net, index, rt, new, info); /* If we removed a route free it now */ mpls_rt_free(rt); }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman8897.78%375.00%
roopa prabhuroopa prabhu22.22%125.00%
Total90100.00%4100.00%


static unsigned find_free_label(struct net *net) { struct mpls_route __rcu **platform_label; size_t platform_labels; unsigned index; platform_label = rtnl_dereference(net->mpls.platform_label); platform_labels = net->mpls.platform_labels; for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels; index++) { if (!rtnl_dereference(platform_label[index])) return index; } return LABEL_NOT_SPECIFIED; }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman7498.67%266.67%
robert shearmanrobert shearman11.33%133.33%
Total75100.00%3100.00%

#if IS_ENABLED(CONFIG_INET)
static struct net_device *inet_fib_lookup_dev(struct net *net, const void *addr) { struct net_device *dev; struct rtable *rt; struct in_addr daddr; memcpy(&daddr, addr, sizeof(struct in_addr)); rt = ip_route_output(net, daddr.s_addr, 0, 0, 0); if (IS_ERR(rt)) return ERR_CAST(rt); dev = rt->dst.dev; dev_hold(dev); ip_rt_put(rt); return dev; }

Contributors

PersonTokensPropCommitsCommitProp
roopa prabhuroopa prabhu9092.78%250.00%
dan carpenterdan carpenter66.19%125.00%
robert shearmanrobert shearman11.03%125.00%
Total97100.00%4100.00%

#else
static struct net_device *inet_fib_lookup_dev(struct net *net, const void *addr) { return ERR_PTR(-EAFNOSUPPORT); }

Contributors

PersonTokensPropCommitsCommitProp
roopa prabhuroopa prabhu2496.00%150.00%
robert shearmanrobert shearman14.00%150.00%
Total25100.00%2100.00%

#endif #if IS_ENABLED(CONFIG_IPV6)
static struct net_device *inet6_fib_lookup_dev(struct net *net, const void *addr) { struct net_device *dev; struct dst_entry *dst; struct flowi6 fl6; int err; if (!ipv6_stub) return ERR_PTR(-EAFNOSUPPORT); memset(&fl6, 0, sizeof(fl6)); memcpy(&fl6.daddr, addr, sizeof(struct in6_addr)); err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6); if (err) return ERR_PTR(err); dev = dst->dev; dev_hold(dev); dst_release(dst); return dev; }

Contributors

PersonTokensPropCommitsCommitProp
roopa prabhuroopa prabhu11594.26%250.00%
dan carpenterdan carpenter64.92%125.00%
robert shearmanrobert shearman10.82%125.00%
Total122100.00%4100.00%

#else
static struct net_device *inet6_fib_lookup_dev(struct net *net, const void *addr) { return ERR_PTR(-EAFNOSUPPORT); }

Contributors

PersonTokensPropCommitsCommitProp
roopa prabhuroopa prabhu2496.00%266.67%
robert shearmanrobert shearman14.00%133.33%
Total25100.00%3100.00%

#endif
static struct net_device *find_outdev(struct net *net, struct mpls_route *rt, struct mpls_nh *nh, int oif) { struct net_device *dev = NULL; if (!oif) { switch (nh->nh_via_table) { case NEIGH_ARP_TABLE: dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh)); break; case NEIGH_ND_TABLE: dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh)); break; case NEIGH_LINK_TABLE: break; } } else { dev = dev_get_by_index(net, oif); } if (!dev) return ERR_PTR(-ENODEV); if (IS_ERR(dev)) return dev; /* The caller is holding rtnl anyways, so release the dev reference */ dev_put(dev); return dev; }

Contributors

PersonTokensPropCommitsCommitProp
roopa prabhuroopa prabhu11688.55%480.00%
robert shearmanrobert shearman1511.45%120.00%
Total131100.00%5100.00%


static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt, struct mpls_nh *nh, int oif) { struct net_device *dev = NULL; int err = -ENODEV; dev = find_outdev(net, rt, nh, oif); if (IS_ERR(dev)) { err = PTR_ERR(dev); dev = NULL; goto errout; } /* Ensure this is a supported device */ err = -EINVAL; if (!mpls_dev_get(dev)) goto errout; if ((nh->nh_via_table == NEIGH_LINK_TABLE) && (dev->addr_len != nh->nh_via_alen)) goto errout; RCU_INIT_POINTER(nh->nh_dev, dev); if (!(dev->flags & IFF_UP)) { nh->nh_flags |= RTNH_F_DEAD; } else { unsigned int flags; flags = dev_get_flags(dev); if (!(flags & (IFF_RUNNING | IFF_LOWER_UP))) nh->nh_flags |= RTNH_F_LINKDOWN; } return 0; errout: return err; }

Contributors

PersonTokensPropCommitsCommitProp
roopa prabhuroopa prabhu12970.88%240.00%
robert shearmanrobert shearman3016.48%240.00%
eric w. biedermaneric w. biederman2312.64%120.00%
Total182100.00%5100.00%


static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg, struct mpls_route *rt) { struct net *net = cfg->rc_nlinfo.nl_net; struct mpls_nh *nh = rt->rt_nh; int err; int i; if (!nh) return -ENOMEM; err = -EINVAL; /* Ensure only a supported number of labels are present */ if (cfg->rc_output_labels > MAX_NEW_LABELS) goto errout; nh->nh_labels = cfg->rc_output_labels; for (i = 0; i < nh->nh_labels; i++) nh->nh_label[i] = cfg->rc_output_label[i]; nh->nh_via_table = cfg->rc_via_table; memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen); nh->nh_via_alen = cfg->rc_via_alen; err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex); if (err) goto errout; if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) rt->rt_nhn_alive--; return 0; errout: return err; }

Contributors

PersonTokensPropCommitsCommitProp
roopa prabhuroopa prabhu17795.16%250.00%
robert shearmanrobert shearman73.76%125.00%
eric w. biedermaneric w. biederman21.08%125.00%
Total186100.00%4100.00%


static int mpls_nh_build(struct net *net, struct mpls_route *rt, struct mpls_nh *nh, int oif, struct nlattr *via, struct nlattr *newdst) { int err = -ENOMEM; if (!nh) goto errout; if (newdst) { err = nla_get_labels(newdst, MAX_NEW_LABELS, &nh->nh_labels, nh->nh_label); if (err) goto errout; } if (via) { err = nla_get_via(via, &nh->