|
22 | 22 | #include <net/nexthop.h>
|
23 | 23 | #include "internal.h"
|
24 | 24 |
|
| 25 | +/* Maximum number of labels to look ahead at when selecting a path of |
| 26 | + * a multipath route |
| 27 | + */ |
| 28 | +#define MAX_MP_SELECT_LABELS 4 |
| 29 | + |
25 | 30 | static int zero = 0;
|
26 | 31 | static int label_limit = (1 << 20) - 1;
|
27 | 32 |
|
@@ -77,10 +82,78 @@ bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
|
77 | 82 | }
|
78 | 83 | EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
|
79 | 84 |
|
80 |
| -static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt) |
| 85 | +static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt, |
| 86 | + struct sk_buff *skb, bool bos) |
81 | 87 | {
|
82 |
| - /* assume single nexthop for now */ |
83 |
| - return &rt->rt_nh[0]; |
| 88 | + struct mpls_entry_decoded dec; |
| 89 | + struct mpls_shim_hdr *hdr; |
| 90 | + bool eli_seen = false; |
| 91 | + int label_index; |
| 92 | + int nh_index = 0; |
| 93 | + u32 hash = 0; |
| 94 | + |
| 95 | + /* No need to look further into packet if there's only |
| 96 | + * one path |
| 97 | + */ |
| 98 | + if (rt->rt_nhn == 1) |
| 99 | + goto out; |
| 100 | + |
| 101 | + for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos; |
| 102 | + label_index++) { |
| 103 | + if (!pskb_may_pull(skb, sizeof(*hdr) * label_index)) |
| 104 | + break; |
| 105 | + |
| 106 | + /* Read and decode the current label */ |
| 107 | + hdr = mpls_hdr(skb) + label_index; |
| 108 | + dec = mpls_entry_decode(hdr); |
| 109 | + |
| 110 | + /* RFC6790 - reserved labels MUST NOT be used as keys |
| 111 | + * for the load-balancing function |
| 112 | + */ |
| 113 | + if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) { |
| 114 | + hash = jhash_1word(dec.label, hash); |
| 115 | + |
| 116 | + /* The entropy label follows the entropy label |
| 117 | + * indicator, so this means that the entropy |
| 118 | + * label was just added to the hash - no need to |
| 119 | + * go any deeper either in the label stack or in the |
| 120 | + * payload |
| 121 | + */ |
| 122 | + if (eli_seen) |
| 123 | + break; |
| 124 | + } else if (dec.label == MPLS_LABEL_ENTROPY) { |
| 125 | + eli_seen = true; |
| 126 | + } |
| 127 | + |
| 128 | + bos = dec.bos; |
| 129 | + if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index + |
| 130 | + sizeof(struct iphdr))) { |
| 131 | + const struct iphdr *v4hdr; |
| 132 | + |
| 133 | + v4hdr = (const struct iphdr *)(mpls_hdr(skb) + |
| 134 | + label_index); |
| 135 | + if (v4hdr->version == 4) { |
| 136 | + hash = jhash_3words(ntohl(v4hdr->saddr), |
| 137 | + ntohl(v4hdr->daddr), |
| 138 | + v4hdr->protocol, hash); |
| 139 | + } else if (v4hdr->version == 6 && |
| 140 | + pskb_may_pull(skb, sizeof(*hdr) * label_index + |
| 141 | + sizeof(struct ipv6hdr))) { |
| 142 | + const struct ipv6hdr *v6hdr; |
| 143 | + |
| 144 | + v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) + |
| 145 | + label_index); |
| 146 | + |
| 147 | + hash = __ipv6_addr_jhash(&v6hdr->saddr, hash); |
| 148 | + hash = __ipv6_addr_jhash(&v6hdr->daddr, hash); |
| 149 | + hash = jhash_1word(v6hdr->nexthdr, hash); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + nh_index = hash % rt->rt_nhn; |
| 155 | +out: |
| 156 | + return &rt->rt_nh[nh_index]; |
84 | 157 | }
|
85 | 158 |
|
86 | 159 | static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
|
@@ -175,7 +248,7 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
|
175 | 248 | if (!rt)
|
176 | 249 | goto drop;
|
177 | 250 |
|
178 |
| - nh = mpls_select_multipath(rt); |
| 251 | + nh = mpls_select_multipath(rt, skb, dec.bos); |
179 | 252 | if (!nh)
|
180 | 253 | goto drop;
|
181 | 254 |
|
@@ -541,6 +614,12 @@ static int mpls_nh_build_multi(struct mpls_route_config *cfg,
|
541 | 614 | if (!rtnh_ok(rtnh, remaining))
|
542 | 615 | goto errout;
|
543 | 616 |
|
| 617 | + /* neither weighted multipath nor any flags |
| 618 | + * are supported |
| 619 | + */ |
| 620 | + if (rtnh->rtnh_hops || rtnh->rtnh_flags) |
| 621 | + goto errout; |
| 622 | + |
544 | 623 | attrlen = rtnh_attrlen(rtnh);
|
545 | 624 | if (attrlen > 0) {
|
546 | 625 | struct nlattr *attrs = rtnh_attrs(rtnh);
|
|
0 commit comments