Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 1 | #include <linux/types.h> |
| 2 | #include <linux/skbuff.h> |
| 3 | #include <linux/socket.h> |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 4 | #include <linux/sysctl.h> |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 5 | #include <linux/net.h> |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/if_arp.h> |
| 8 | #include <linux/ipv6.h> |
| 9 | #include <linux/mpls.h> |
Stephen Rothwell | 4b5edb2 | 2015-03-05 13:37:05 +1100 | [diff] [blame] | 10 | #include <linux/vmalloc.h> |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 11 | #include <net/ip.h> |
| 12 | #include <net/dst.h> |
| 13 | #include <net/sock.h> |
| 14 | #include <net/arp.h> |
| 15 | #include <net/ip_fib.h> |
| 16 | #include <net/netevent.h> |
| 17 | #include <net/netns/generic.h> |
Roopa Prabhu | bf21563 | 2015-07-30 13:34:54 -0700 | [diff] [blame] | 18 | #if IS_ENABLED(CONFIG_IPV6) |
| 19 | #include <net/ipv6.h> |
| 20 | #include <net/addrconf.h> |
| 21 | #endif |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 22 | #include <net/nexthop.h> |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 23 | #include "internal.h" |
| 24 | |
Robert Shearman | 1c78efa | 2015-10-23 06:03:28 -0700 | [diff] [blame] | 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 | |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 30 | static int zero = 0; |
| 31 | static int label_limit = (1 << 20) - 1; |
| 32 | |
Eric W. Biederman | 8de147d | 2015-03-03 19:14:31 -0600 | [diff] [blame] | 33 | static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt, |
| 34 | struct nlmsghdr *nlh, struct net *net, u32 portid, |
| 35 | unsigned int nlm_flags); |
| 36 | |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 37 | static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index) |
| 38 | { |
| 39 | struct mpls_route *rt = NULL; |
| 40 | |
| 41 | if (index < net->mpls.platform_labels) { |
| 42 | struct mpls_route __rcu **platform_label = |
| 43 | rcu_dereference(net->mpls.platform_label); |
| 44 | rt = rcu_dereference(platform_label[index]); |
| 45 | } |
| 46 | return rt; |
| 47 | } |
| 48 | |
Robert Shearman | 03c5774 | 2015-04-22 11:14:37 +0100 | [diff] [blame] | 49 | static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev) |
| 50 | { |
| 51 | return rcu_dereference_rtnl(dev->mpls_ptr); |
| 52 | } |
| 53 | |
Roopa Prabhu | face018 | 2015-07-21 10:43:52 +0200 | [diff] [blame] | 54 | bool mpls_output_possible(const struct net_device *dev) |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 55 | { |
| 56 | return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev); |
| 57 | } |
Roopa Prabhu | face018 | 2015-07-21 10:43:52 +0200 | [diff] [blame] | 58 | EXPORT_SYMBOL_GPL(mpls_output_possible); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 59 | |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 60 | static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh) |
| 61 | { |
| 62 | u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN); |
| 63 | int nh_index = nh - rt->rt_nh; |
| 64 | |
| 65 | return nh0_via + rt->rt_max_alen * nh_index; |
| 66 | } |
| 67 | |
| 68 | static const u8 *mpls_nh_via(const struct mpls_route *rt, |
| 69 | const struct mpls_nh *nh) |
| 70 | { |
| 71 | return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh); |
| 72 | } |
| 73 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 74 | static unsigned int mpls_nh_header_size(const struct mpls_nh *nh) |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 75 | { |
| 76 | /* The size of the layer 2.5 labels to be added for this route */ |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 77 | return nh->nh_labels * sizeof(struct mpls_shim_hdr); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 78 | } |
| 79 | |
Roopa Prabhu | face018 | 2015-07-21 10:43:52 +0200 | [diff] [blame] | 80 | unsigned int mpls_dev_mtu(const struct net_device *dev) |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 81 | { |
| 82 | /* The amount of data the layer 2 frame can hold */ |
| 83 | return dev->mtu; |
| 84 | } |
Roopa Prabhu | face018 | 2015-07-21 10:43:52 +0200 | [diff] [blame] | 85 | EXPORT_SYMBOL_GPL(mpls_dev_mtu); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 86 | |
Roopa Prabhu | face018 | 2015-07-21 10:43:52 +0200 | [diff] [blame] | 87 | bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu) |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 88 | { |
| 89 | if (skb->len <= mtu) |
| 90 | return false; |
| 91 | |
| 92 | if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu) |
| 93 | return false; |
| 94 | |
| 95 | return true; |
| 96 | } |
Roopa Prabhu | face018 | 2015-07-21 10:43:52 +0200 | [diff] [blame] | 97 | EXPORT_SYMBOL_GPL(mpls_pkt_too_big); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 98 | |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 99 | static u32 mpls_multipath_hash(struct mpls_route *rt, |
| 100 | struct sk_buff *skb, bool bos) |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 101 | { |
Robert Shearman | 1c78efa | 2015-10-23 06:03:28 -0700 | [diff] [blame] | 102 | struct mpls_entry_decoded dec; |
| 103 | struct mpls_shim_hdr *hdr; |
| 104 | bool eli_seen = false; |
| 105 | int label_index; |
Robert Shearman | 1c78efa | 2015-10-23 06:03:28 -0700 | [diff] [blame] | 106 | u32 hash = 0; |
| 107 | |
Robert Shearman | 1c78efa | 2015-10-23 06:03:28 -0700 | [diff] [blame] | 108 | for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos; |
| 109 | label_index++) { |
| 110 | if (!pskb_may_pull(skb, sizeof(*hdr) * label_index)) |
| 111 | break; |
| 112 | |
| 113 | /* Read and decode the current label */ |
| 114 | hdr = mpls_hdr(skb) + label_index; |
| 115 | dec = mpls_entry_decode(hdr); |
| 116 | |
| 117 | /* RFC6790 - reserved labels MUST NOT be used as keys |
| 118 | * for the load-balancing function |
| 119 | */ |
| 120 | if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) { |
| 121 | hash = jhash_1word(dec.label, hash); |
| 122 | |
| 123 | /* The entropy label follows the entropy label |
| 124 | * indicator, so this means that the entropy |
| 125 | * label was just added to the hash - no need to |
| 126 | * go any deeper either in the label stack or in the |
| 127 | * payload |
| 128 | */ |
| 129 | if (eli_seen) |
| 130 | break; |
| 131 | } else if (dec.label == MPLS_LABEL_ENTROPY) { |
| 132 | eli_seen = true; |
| 133 | } |
| 134 | |
| 135 | bos = dec.bos; |
| 136 | if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index + |
| 137 | sizeof(struct iphdr))) { |
| 138 | const struct iphdr *v4hdr; |
| 139 | |
| 140 | v4hdr = (const struct iphdr *)(mpls_hdr(skb) + |
| 141 | label_index); |
| 142 | if (v4hdr->version == 4) { |
| 143 | hash = jhash_3words(ntohl(v4hdr->saddr), |
| 144 | ntohl(v4hdr->daddr), |
| 145 | v4hdr->protocol, hash); |
| 146 | } else if (v4hdr->version == 6 && |
| 147 | pskb_may_pull(skb, sizeof(*hdr) * label_index + |
| 148 | sizeof(struct ipv6hdr))) { |
| 149 | const struct ipv6hdr *v6hdr; |
| 150 | |
| 151 | v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) + |
| 152 | label_index); |
| 153 | |
| 154 | hash = __ipv6_addr_jhash(&v6hdr->saddr, hash); |
| 155 | hash = __ipv6_addr_jhash(&v6hdr->daddr, hash); |
| 156 | hash = jhash_1word(v6hdr->nexthdr, hash); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 161 | return hash; |
| 162 | } |
| 163 | |
| 164 | static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt, |
| 165 | struct sk_buff *skb, bool bos) |
| 166 | { |
| 167 | int alive = ACCESS_ONCE(rt->rt_nhn_alive); |
| 168 | u32 hash = 0; |
| 169 | int nh_index = 0; |
| 170 | int n = 0; |
| 171 | |
| 172 | /* No need to look further into packet if there's only |
| 173 | * one path |
| 174 | */ |
| 175 | if (rt->rt_nhn == 1) |
| 176 | goto out; |
| 177 | |
| 178 | if (alive <= 0) |
| 179 | return NULL; |
| 180 | |
| 181 | hash = mpls_multipath_hash(rt, skb, bos); |
| 182 | nh_index = hash % alive; |
| 183 | if (alive == rt->rt_nhn) |
| 184 | goto out; |
| 185 | for_nexthops(rt) { |
| 186 | if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) |
| 187 | continue; |
| 188 | if (n == nh_index) |
| 189 | return nh; |
| 190 | n++; |
| 191 | } endfor_nexthops(rt); |
| 192 | |
Robert Shearman | 1c78efa | 2015-10-23 06:03:28 -0700 | [diff] [blame] | 193 | out: |
| 194 | return &rt->rt_nh[nh_index]; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 197 | static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb, |
| 198 | struct mpls_entry_decoded dec) |
| 199 | { |
Robert Shearman | 118d523 | 2015-08-06 11:04:56 +0100 | [diff] [blame] | 200 | enum mpls_payload_type payload_type; |
| 201 | bool success = false; |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 202 | |
Eric W. Biederman | 76fecd8 | 2015-03-12 18:22:59 -0500 | [diff] [blame] | 203 | /* The IPv4 code below accesses through the IPv4 header |
| 204 | * checksum, which is 12 bytes into the packet. |
| 205 | * The IPv6 code below accesses through the IPv6 hop limit |
| 206 | * which is 8 bytes into the packet. |
| 207 | * |
| 208 | * For all supported cases there should always be at least 12 |
| 209 | * bytes of packet data present. The IPv4 header is 20 bytes |
| 210 | * without options and the IPv6 header is always 40 bytes |
| 211 | * long. |
| 212 | */ |
| 213 | if (!pskb_may_pull(skb, 12)) |
| 214 | return false; |
| 215 | |
Robert Shearman | 118d523 | 2015-08-06 11:04:56 +0100 | [diff] [blame] | 216 | payload_type = rt->rt_payload_type; |
| 217 | if (payload_type == MPT_UNSPEC) |
| 218 | payload_type = ip_hdr(skb)->version; |
| 219 | |
| 220 | switch (payload_type) { |
| 221 | case MPT_IPV4: { |
| 222 | struct iphdr *hdr4 = ip_hdr(skb); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 223 | skb->protocol = htons(ETH_P_IP); |
| 224 | csum_replace2(&hdr4->check, |
| 225 | htons(hdr4->ttl << 8), |
| 226 | htons(dec.ttl << 8)); |
| 227 | hdr4->ttl = dec.ttl; |
Robert Shearman | 118d523 | 2015-08-06 11:04:56 +0100 | [diff] [blame] | 228 | success = true; |
| 229 | break; |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 230 | } |
Robert Shearman | 118d523 | 2015-08-06 11:04:56 +0100 | [diff] [blame] | 231 | case MPT_IPV6: { |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 232 | struct ipv6hdr *hdr6 = ipv6_hdr(skb); |
| 233 | skb->protocol = htons(ETH_P_IPV6); |
| 234 | hdr6->hop_limit = dec.ttl; |
Robert Shearman | 118d523 | 2015-08-06 11:04:56 +0100 | [diff] [blame] | 235 | success = true; |
| 236 | break; |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 237 | } |
Robert Shearman | 118d523 | 2015-08-06 11:04:56 +0100 | [diff] [blame] | 238 | case MPT_UNSPEC: |
| 239 | break; |
| 240 | } |
| 241 | |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 242 | return success; |
| 243 | } |
| 244 | |
| 245 | static int mpls_forward(struct sk_buff *skb, struct net_device *dev, |
| 246 | struct packet_type *pt, struct net_device *orig_dev) |
| 247 | { |
| 248 | struct net *net = dev_net(dev); |
| 249 | struct mpls_shim_hdr *hdr; |
| 250 | struct mpls_route *rt; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 251 | struct mpls_nh *nh; |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 252 | struct mpls_entry_decoded dec; |
| 253 | struct net_device *out_dev; |
Robert Shearman | 03c5774 | 2015-04-22 11:14:37 +0100 | [diff] [blame] | 254 | struct mpls_dev *mdev; |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 255 | unsigned int hh_len; |
| 256 | unsigned int new_header_size; |
| 257 | unsigned int mtu; |
| 258 | int err; |
| 259 | |
| 260 | /* Careful this entire function runs inside of an rcu critical section */ |
| 261 | |
Robert Shearman | 03c5774 | 2015-04-22 11:14:37 +0100 | [diff] [blame] | 262 | mdev = mpls_dev_get(dev); |
Robert Shearman | 37bde79 | 2015-04-22 11:14:38 +0100 | [diff] [blame] | 263 | if (!mdev || !mdev->input_enabled) |
Robert Shearman | 03c5774 | 2015-04-22 11:14:37 +0100 | [diff] [blame] | 264 | goto drop; |
| 265 | |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 266 | if (skb->pkt_type != PACKET_HOST) |
| 267 | goto drop; |
| 268 | |
| 269 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) |
| 270 | goto drop; |
| 271 | |
| 272 | if (!pskb_may_pull(skb, sizeof(*hdr))) |
| 273 | goto drop; |
| 274 | |
| 275 | /* Read and decode the label */ |
| 276 | hdr = mpls_hdr(skb); |
| 277 | dec = mpls_entry_decode(hdr); |
| 278 | |
| 279 | /* Pop the label */ |
| 280 | skb_pull(skb, sizeof(*hdr)); |
| 281 | skb_reset_network_header(skb); |
| 282 | |
| 283 | skb_orphan(skb); |
| 284 | |
| 285 | rt = mpls_route_input_rcu(net, dec.label); |
| 286 | if (!rt) |
| 287 | goto drop; |
| 288 | |
Robert Shearman | 1c78efa | 2015-10-23 06:03:28 -0700 | [diff] [blame] | 289 | nh = mpls_select_multipath(rt, skb, dec.bos); |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 290 | if (!nh) |
| 291 | goto drop; |
| 292 | |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 293 | /* Find the output device */ |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 294 | out_dev = rcu_dereference(nh->nh_dev); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 295 | if (!mpls_output_possible(out_dev)) |
| 296 | goto drop; |
| 297 | |
| 298 | if (skb_warn_if_lro(skb)) |
| 299 | goto drop; |
| 300 | |
| 301 | skb_forward_csum(skb); |
| 302 | |
| 303 | /* Verify ttl is valid */ |
Eric W. Biederman | aa7da93 | 2015-03-07 16:23:23 -0600 | [diff] [blame] | 304 | if (dec.ttl <= 1) |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 305 | goto drop; |
| 306 | dec.ttl -= 1; |
| 307 | |
| 308 | /* Verify the destination can hold the packet */ |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 309 | new_header_size = mpls_nh_header_size(nh); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 310 | mtu = mpls_dev_mtu(out_dev); |
| 311 | if (mpls_pkt_too_big(skb, mtu - new_header_size)) |
| 312 | goto drop; |
| 313 | |
| 314 | hh_len = LL_RESERVED_SPACE(out_dev); |
| 315 | if (!out_dev->header_ops) |
| 316 | hh_len = 0; |
| 317 | |
| 318 | /* Ensure there is enough space for the headers in the skb */ |
| 319 | if (skb_cow(skb, hh_len + new_header_size)) |
| 320 | goto drop; |
| 321 | |
| 322 | skb->dev = out_dev; |
| 323 | skb->protocol = htons(ETH_P_MPLS_UC); |
| 324 | |
| 325 | if (unlikely(!new_header_size && dec.bos)) { |
| 326 | /* Penultimate hop popping */ |
| 327 | if (!mpls_egress(rt, skb, dec)) |
| 328 | goto drop; |
| 329 | } else { |
| 330 | bool bos; |
| 331 | int i; |
| 332 | skb_push(skb, new_header_size); |
| 333 | skb_reset_network_header(skb); |
| 334 | /* Push the new labels */ |
| 335 | hdr = mpls_hdr(skb); |
| 336 | bos = dec.bos; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 337 | for (i = nh->nh_labels - 1; i >= 0; i--) { |
| 338 | hdr[i] = mpls_entry_encode(nh->nh_label[i], |
| 339 | dec.ttl, 0, bos); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 340 | bos = false; |
| 341 | } |
| 342 | } |
| 343 | |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 344 | err = neigh_xmit(nh->nh_via_table, out_dev, mpls_nh_via(rt, nh), skb); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 345 | if (err) |
| 346 | net_dbg_ratelimited("%s: packet transmission failed: %d\n", |
| 347 | __func__, err); |
| 348 | return 0; |
| 349 | |
| 350 | drop: |
| 351 | kfree_skb(skb); |
| 352 | return NET_RX_DROP; |
| 353 | } |
| 354 | |
| 355 | static struct packet_type mpls_packet_type __read_mostly = { |
| 356 | .type = cpu_to_be16(ETH_P_MPLS_UC), |
| 357 | .func = mpls_forward, |
| 358 | }; |
| 359 | |
Wu Fengguang | f012653 | 2015-03-05 05:33:54 +0800 | [diff] [blame] | 360 | static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = { |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 361 | [RTA_DST] = { .type = NLA_U32 }, |
| 362 | [RTA_OIF] = { .type = NLA_U32 }, |
| 363 | }; |
| 364 | |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 365 | struct mpls_route_config { |
Robert Shearman | 118d523 | 2015-08-06 11:04:56 +0100 | [diff] [blame] | 366 | u32 rc_protocol; |
| 367 | u32 rc_ifindex; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 368 | u8 rc_via_table; |
| 369 | u8 rc_via_alen; |
Robert Shearman | 118d523 | 2015-08-06 11:04:56 +0100 | [diff] [blame] | 370 | u8 rc_via[MAX_VIA_ALEN]; |
| 371 | u32 rc_label; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 372 | u8 rc_output_labels; |
Robert Shearman | 118d523 | 2015-08-06 11:04:56 +0100 | [diff] [blame] | 373 | u32 rc_output_label[MAX_NEW_LABELS]; |
| 374 | u32 rc_nlflags; |
| 375 | enum mpls_payload_type rc_payload_type; |
| 376 | struct nl_info rc_nlinfo; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 377 | struct rtnexthop *rc_mp; |
| 378 | int rc_mp_len; |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 379 | }; |
| 380 | |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 381 | static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen) |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 382 | { |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 383 | u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 384 | struct mpls_route *rt; |
| 385 | |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 386 | rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh), |
| 387 | VIA_ALEN_ALIGN) + |
| 388 | num_nh * max_alen_aligned, |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 389 | GFP_KERNEL); |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 390 | if (rt) { |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 391 | rt->rt_nhn = num_nh; |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 392 | rt->rt_nhn_alive = num_nh; |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 393 | rt->rt_max_alen = max_alen_aligned; |
| 394 | } |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 395 | |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 396 | return rt; |
| 397 | } |
| 398 | |
| 399 | static void mpls_rt_free(struct mpls_route *rt) |
| 400 | { |
| 401 | if (rt) |
| 402 | kfree_rcu(rt, rt_rcu); |
| 403 | } |
| 404 | |
Eric W. Biederman | 8de147d | 2015-03-03 19:14:31 -0600 | [diff] [blame] | 405 | static void mpls_notify_route(struct net *net, unsigned index, |
| 406 | struct mpls_route *old, struct mpls_route *new, |
| 407 | const struct nl_info *info) |
| 408 | { |
| 409 | struct nlmsghdr *nlh = info ? info->nlh : NULL; |
| 410 | unsigned portid = info ? info->portid : 0; |
| 411 | int event = new ? RTM_NEWROUTE : RTM_DELROUTE; |
| 412 | struct mpls_route *rt = new ? new : old; |
| 413 | unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0; |
| 414 | /* Ignore reserved labels for now */ |
Robert Shearman | a6affd2 | 2015-08-03 17:50:04 +0100 | [diff] [blame] | 415 | if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED)) |
Eric W. Biederman | 8de147d | 2015-03-03 19:14:31 -0600 | [diff] [blame] | 416 | rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags); |
| 417 | } |
| 418 | |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 419 | static void mpls_route_update(struct net *net, unsigned index, |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 420 | struct mpls_route *new, |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 421 | const struct nl_info *info) |
| 422 | { |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 423 | struct mpls_route __rcu **platform_label; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 424 | struct mpls_route *rt; |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 425 | |
| 426 | ASSERT_RTNL(); |
| 427 | |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 428 | platform_label = rtnl_dereference(net->mpls.platform_label); |
| 429 | rt = rtnl_dereference(platform_label[index]); |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 430 | rcu_assign_pointer(platform_label[index], new); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 431 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 432 | mpls_notify_route(net, index, rt, new, info); |
Eric W. Biederman | 8de147d | 2015-03-03 19:14:31 -0600 | [diff] [blame] | 433 | |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 434 | /* If we removed a route free it now */ |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 435 | mpls_rt_free(rt); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 436 | } |
| 437 | |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 438 | static unsigned find_free_label(struct net *net) |
| 439 | { |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 440 | struct mpls_route __rcu **platform_label; |
| 441 | size_t platform_labels; |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 442 | unsigned index; |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 443 | |
| 444 | platform_label = rtnl_dereference(net->mpls.platform_label); |
| 445 | platform_labels = net->mpls.platform_labels; |
Robert Shearman | a6affd2 | 2015-08-03 17:50:04 +0100 | [diff] [blame] | 446 | for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels; |
| 447 | index++) { |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 448 | if (!rtnl_dereference(platform_label[index])) |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 449 | return index; |
| 450 | } |
| 451 | return LABEL_NOT_SPECIFIED; |
| 452 | } |
| 453 | |
Roopa Prabhu | bf21563 | 2015-07-30 13:34:54 -0700 | [diff] [blame] | 454 | #if IS_ENABLED(CONFIG_INET) |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 455 | static struct net_device *inet_fib_lookup_dev(struct net *net, |
| 456 | const void *addr) |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 457 | { |
Dan Carpenter | 5a9348b | 2015-08-04 10:44:22 +0300 | [diff] [blame] | 458 | struct net_device *dev; |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 459 | struct rtable *rt; |
| 460 | struct in_addr daddr; |
| 461 | |
| 462 | memcpy(&daddr, addr, sizeof(struct in_addr)); |
| 463 | rt = ip_route_output(net, daddr.s_addr, 0, 0, 0); |
| 464 | if (IS_ERR(rt)) |
Dan Carpenter | 5a9348b | 2015-08-04 10:44:22 +0300 | [diff] [blame] | 465 | return ERR_CAST(rt); |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 466 | |
| 467 | dev = rt->dst.dev; |
| 468 | dev_hold(dev); |
| 469 | |
| 470 | ip_rt_put(rt); |
| 471 | |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 472 | return dev; |
| 473 | } |
Roopa Prabhu | bf21563 | 2015-07-30 13:34:54 -0700 | [diff] [blame] | 474 | #else |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 475 | static struct net_device *inet_fib_lookup_dev(struct net *net, |
| 476 | const void *addr) |
Roopa Prabhu | bf21563 | 2015-07-30 13:34:54 -0700 | [diff] [blame] | 477 | { |
| 478 | return ERR_PTR(-EAFNOSUPPORT); |
| 479 | } |
| 480 | #endif |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 481 | |
Roopa Prabhu | bf21563 | 2015-07-30 13:34:54 -0700 | [diff] [blame] | 482 | #if IS_ENABLED(CONFIG_IPV6) |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 483 | static struct net_device *inet6_fib_lookup_dev(struct net *net, |
| 484 | const void *addr) |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 485 | { |
Dan Carpenter | 5a9348b | 2015-08-04 10:44:22 +0300 | [diff] [blame] | 486 | struct net_device *dev; |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 487 | struct dst_entry *dst; |
| 488 | struct flowi6 fl6; |
Roopa Prabhu | bf21563 | 2015-07-30 13:34:54 -0700 | [diff] [blame] | 489 | int err; |
| 490 | |
| 491 | if (!ipv6_stub) |
| 492 | return ERR_PTR(-EAFNOSUPPORT); |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 493 | |
| 494 | memset(&fl6, 0, sizeof(fl6)); |
| 495 | memcpy(&fl6.daddr, addr, sizeof(struct in6_addr)); |
Roopa Prabhu | bf21563 | 2015-07-30 13:34:54 -0700 | [diff] [blame] | 496 | err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6); |
| 497 | if (err) |
Dan Carpenter | 5a9348b | 2015-08-04 10:44:22 +0300 | [diff] [blame] | 498 | return ERR_PTR(err); |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 499 | |
| 500 | dev = dst->dev; |
| 501 | dev_hold(dev); |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 502 | dst_release(dst); |
| 503 | |
| 504 | return dev; |
| 505 | } |
Roopa Prabhu | bf21563 | 2015-07-30 13:34:54 -0700 | [diff] [blame] | 506 | #else |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 507 | static struct net_device *inet6_fib_lookup_dev(struct net *net, |
| 508 | const void *addr) |
Roopa Prabhu | bf21563 | 2015-07-30 13:34:54 -0700 | [diff] [blame] | 509 | { |
| 510 | return ERR_PTR(-EAFNOSUPPORT); |
| 511 | } |
| 512 | #endif |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 513 | |
| 514 | static struct net_device *find_outdev(struct net *net, |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 515 | struct mpls_route *rt, |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 516 | struct mpls_nh *nh, int oif) |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 517 | { |
| 518 | struct net_device *dev = NULL; |
| 519 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 520 | if (!oif) { |
| 521 | switch (nh->nh_via_table) { |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 522 | case NEIGH_ARP_TABLE: |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 523 | dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh)); |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 524 | break; |
| 525 | case NEIGH_ND_TABLE: |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 526 | dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh)); |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 527 | break; |
| 528 | case NEIGH_LINK_TABLE: |
| 529 | break; |
| 530 | } |
| 531 | } else { |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 532 | dev = dev_get_by_index(net, oif); |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Roopa Prabhu | 3dcb615 | 2015-08-04 06:36:24 -0700 | [diff] [blame] | 535 | if (!dev) |
| 536 | return ERR_PTR(-ENODEV); |
| 537 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 538 | /* The caller is holding rtnl anyways, so release the dev reference */ |
| 539 | dev_put(dev); |
| 540 | |
Roopa Prabhu | 01faef2 | 2015-07-21 09:16:24 -0700 | [diff] [blame] | 541 | return dev; |
| 542 | } |
| 543 | |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 544 | static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt, |
| 545 | struct mpls_nh *nh, int oif) |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 546 | { |
| 547 | struct net_device *dev = NULL; |
| 548 | int err = -ENODEV; |
| 549 | |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 550 | dev = find_outdev(net, rt, nh, oif); |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 551 | if (IS_ERR(dev)) { |
| 552 | err = PTR_ERR(dev); |
| 553 | dev = NULL; |
| 554 | goto errout; |
| 555 | } |
| 556 | |
| 557 | /* Ensure this is a supported device */ |
| 558 | err = -EINVAL; |
| 559 | if (!mpls_dev_get(dev)) |
| 560 | goto errout; |
| 561 | |
| 562 | RCU_INIT_POINTER(nh->nh_dev, dev); |
| 563 | |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 564 | if (!(dev->flags & IFF_UP)) { |
| 565 | nh->nh_flags |= RTNH_F_DEAD; |
| 566 | } else { |
| 567 | unsigned int flags; |
| 568 | |
| 569 | flags = dev_get_flags(dev); |
| 570 | if (!(flags & (IFF_RUNNING | IFF_LOWER_UP))) |
| 571 | nh->nh_flags |= RTNH_F_LINKDOWN; |
| 572 | } |
| 573 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 574 | return 0; |
| 575 | |
| 576 | errout: |
| 577 | return err; |
| 578 | } |
| 579 | |
| 580 | static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg, |
| 581 | struct mpls_route *rt) |
| 582 | { |
| 583 | struct net *net = cfg->rc_nlinfo.nl_net; |
| 584 | struct mpls_nh *nh = rt->rt_nh; |
| 585 | int err; |
| 586 | int i; |
| 587 | |
| 588 | if (!nh) |
| 589 | return -ENOMEM; |
| 590 | |
| 591 | err = -EINVAL; |
| 592 | /* Ensure only a supported number of labels are present */ |
| 593 | if (cfg->rc_output_labels > MAX_NEW_LABELS) |
| 594 | goto errout; |
| 595 | |
| 596 | nh->nh_labels = cfg->rc_output_labels; |
| 597 | for (i = 0; i < nh->nh_labels; i++) |
| 598 | nh->nh_label[i] = cfg->rc_output_label[i]; |
| 599 | |
| 600 | nh->nh_via_table = cfg->rc_via_table; |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 601 | memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen); |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 602 | nh->nh_via_alen = cfg->rc_via_alen; |
| 603 | |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 604 | err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex); |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 605 | if (err) |
| 606 | goto errout; |
| 607 | |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 608 | if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) |
| 609 | rt->rt_nhn_alive--; |
| 610 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 611 | return 0; |
| 612 | |
| 613 | errout: |
| 614 | return err; |
| 615 | } |
| 616 | |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 617 | static int mpls_nh_build(struct net *net, struct mpls_route *rt, |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 618 | struct mpls_nh *nh, int oif, struct nlattr *via, |
| 619 | struct nlattr *newdst) |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 620 | { |
| 621 | int err = -ENOMEM; |
| 622 | |
| 623 | if (!nh) |
| 624 | goto errout; |
| 625 | |
| 626 | if (newdst) { |
| 627 | err = nla_get_labels(newdst, MAX_NEW_LABELS, |
| 628 | &nh->nh_labels, nh->nh_label); |
| 629 | if (err) |
| 630 | goto errout; |
| 631 | } |
| 632 | |
| 633 | err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table, |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 634 | __mpls_nh_via(rt, nh)); |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 635 | if (err) |
| 636 | goto errout; |
| 637 | |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 638 | err = mpls_nh_assign_dev(net, rt, nh, oif); |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 639 | if (err) |
| 640 | goto errout; |
| 641 | |
| 642 | return 0; |
| 643 | |
| 644 | errout: |
| 645 | return err; |
| 646 | } |
| 647 | |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 648 | static int mpls_count_nexthops(struct rtnexthop *rtnh, int len, |
| 649 | u8 cfg_via_alen, u8 *max_via_alen) |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 650 | { |
| 651 | int nhs = 0; |
| 652 | int remaining = len; |
| 653 | |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 654 | if (!rtnh) { |
| 655 | *max_via_alen = cfg_via_alen; |
| 656 | return 1; |
| 657 | } |
| 658 | |
| 659 | *max_via_alen = 0; |
| 660 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 661 | while (rtnh_ok(rtnh, remaining)) { |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 662 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 663 | int attrlen; |
| 664 | |
| 665 | attrlen = rtnh_attrlen(rtnh); |
| 666 | nla = nla_find(attrs, attrlen, RTA_VIA); |
| 667 | if (nla && nla_len(nla) >= |
| 668 | offsetof(struct rtvia, rtvia_addr)) { |
| 669 | int via_alen = nla_len(nla) - |
| 670 | offsetof(struct rtvia, rtvia_addr); |
| 671 | |
| 672 | if (via_alen <= MAX_VIA_ALEN) |
| 673 | *max_via_alen = max_t(u16, *max_via_alen, |
| 674 | via_alen); |
| 675 | } |
| 676 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 677 | nhs++; |
| 678 | rtnh = rtnh_next(rtnh, &remaining); |
| 679 | } |
| 680 | |
| 681 | /* leftover implies invalid nexthop configuration, discard it */ |
| 682 | return remaining > 0 ? 0 : nhs; |
| 683 | } |
| 684 | |
| 685 | static int mpls_nh_build_multi(struct mpls_route_config *cfg, |
| 686 | struct mpls_route *rt) |
| 687 | { |
| 688 | struct rtnexthop *rtnh = cfg->rc_mp; |
| 689 | struct nlattr *nla_via, *nla_newdst; |
| 690 | int remaining = cfg->rc_mp_len; |
| 691 | int nhs = 0; |
| 692 | int err = 0; |
| 693 | |
| 694 | change_nexthops(rt) { |
| 695 | int attrlen; |
| 696 | |
| 697 | nla_via = NULL; |
| 698 | nla_newdst = NULL; |
| 699 | |
| 700 | err = -EINVAL; |
| 701 | if (!rtnh_ok(rtnh, remaining)) |
| 702 | goto errout; |
| 703 | |
Robert Shearman | 1c78efa | 2015-10-23 06:03:28 -0700 | [diff] [blame] | 704 | /* neither weighted multipath nor any flags |
| 705 | * are supported |
| 706 | */ |
| 707 | if (rtnh->rtnh_hops || rtnh->rtnh_flags) |
| 708 | goto errout; |
| 709 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 710 | attrlen = rtnh_attrlen(rtnh); |
| 711 | if (attrlen > 0) { |
| 712 | struct nlattr *attrs = rtnh_attrs(rtnh); |
| 713 | |
| 714 | nla_via = nla_find(attrs, attrlen, RTA_VIA); |
| 715 | nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST); |
| 716 | } |
| 717 | |
| 718 | if (!nla_via) |
| 719 | goto errout; |
| 720 | |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 721 | err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh, |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 722 | rtnh->rtnh_ifindex, nla_via, nla_newdst); |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 723 | if (err) |
| 724 | goto errout; |
| 725 | |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 726 | if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) |
| 727 | rt->rt_nhn_alive--; |
| 728 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 729 | rtnh = rtnh_next(rtnh, &remaining); |
| 730 | nhs++; |
| 731 | } endfor_nexthops(rt); |
| 732 | |
| 733 | rt->rt_nhn = nhs; |
| 734 | |
| 735 | return 0; |
| 736 | |
| 737 | errout: |
| 738 | return err; |
| 739 | } |
| 740 | |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 741 | static int mpls_route_add(struct mpls_route_config *cfg) |
| 742 | { |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 743 | struct mpls_route __rcu **platform_label; |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 744 | struct net *net = cfg->rc_nlinfo.nl_net; |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 745 | struct mpls_route *rt, *old; |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 746 | int err = -EINVAL; |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 747 | u8 max_via_alen; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 748 | unsigned index; |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 749 | int nhs; |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 750 | |
| 751 | index = cfg->rc_label; |
| 752 | |
| 753 | /* If a label was not specified during insert pick one */ |
| 754 | if ((index == LABEL_NOT_SPECIFIED) && |
| 755 | (cfg->rc_nlflags & NLM_F_CREATE)) { |
| 756 | index = find_free_label(net); |
| 757 | } |
| 758 | |
Robert Shearman | a6affd2 | 2015-08-03 17:50:04 +0100 | [diff] [blame] | 759 | /* Reserved labels may not be set */ |
| 760 | if (index < MPLS_LABEL_FIRST_UNRESERVED) |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 761 | goto errout; |
| 762 | |
| 763 | /* The full 20 bit range may not be supported. */ |
| 764 | if (index >= net->mpls.platform_labels) |
| 765 | goto errout; |
| 766 | |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 767 | /* Append makes no sense with mpls */ |
Eric W. Biederman | 0f7bbd5 | 2015-03-07 16:22:40 -0600 | [diff] [blame] | 768 | err = -EOPNOTSUPP; |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 769 | if (cfg->rc_nlflags & NLM_F_APPEND) |
| 770 | goto errout; |
| 771 | |
| 772 | err = -EEXIST; |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 773 | platform_label = rtnl_dereference(net->mpls.platform_label); |
| 774 | old = rtnl_dereference(platform_label[index]); |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 775 | if ((cfg->rc_nlflags & NLM_F_EXCL) && old) |
| 776 | goto errout; |
| 777 | |
| 778 | err = -EEXIST; |
| 779 | if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old) |
| 780 | goto errout; |
| 781 | |
| 782 | err = -ENOENT; |
| 783 | if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old) |
| 784 | goto errout; |
| 785 | |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 786 | err = -EINVAL; |
| 787 | nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len, |
| 788 | cfg->rc_via_alen, &max_via_alen); |
| 789 | if (nhs == 0) |
| 790 | goto errout; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 791 | |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 792 | err = -ENOMEM; |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 793 | rt = mpls_rt_alloc(nhs, max_via_alen); |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 794 | if (!rt) |
| 795 | goto errout; |
| 796 | |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 797 | rt->rt_protocol = cfg->rc_protocol; |
Robert Shearman | 118d523 | 2015-08-06 11:04:56 +0100 | [diff] [blame] | 798 | rt->rt_payload_type = cfg->rc_payload_type; |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 799 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 800 | if (cfg->rc_mp) |
| 801 | err = mpls_nh_build_multi(cfg, rt); |
| 802 | else |
| 803 | err = mpls_nh_build_from_cfg(cfg, rt); |
| 804 | if (err) |
| 805 | goto freert; |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 806 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 807 | mpls_route_update(net, index, rt, &cfg->rc_nlinfo); |
| 808 | |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 809 | return 0; |
| 810 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 811 | freert: |
| 812 | mpls_rt_free(rt); |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 813 | errout: |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 814 | return err; |
| 815 | } |
| 816 | |
| 817 | static int mpls_route_del(struct mpls_route_config *cfg) |
| 818 | { |
| 819 | struct net *net = cfg->rc_nlinfo.nl_net; |
| 820 | unsigned index; |
| 821 | int err = -EINVAL; |
| 822 | |
| 823 | index = cfg->rc_label; |
| 824 | |
Robert Shearman | a6affd2 | 2015-08-03 17:50:04 +0100 | [diff] [blame] | 825 | /* Reserved labels may not be removed */ |
| 826 | if (index < MPLS_LABEL_FIRST_UNRESERVED) |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 827 | goto errout; |
| 828 | |
| 829 | /* The full 20 bit range may not be supported */ |
| 830 | if (index >= net->mpls.platform_labels) |
| 831 | goto errout; |
| 832 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 833 | mpls_route_update(net, index, NULL, &cfg->rc_nlinfo); |
Eric W. Biederman | a251992 | 2015-03-03 19:12:40 -0600 | [diff] [blame] | 834 | |
| 835 | err = 0; |
| 836 | errout: |
| 837 | return err; |
| 838 | } |
| 839 | |
Robert Shearman | 37bde79 | 2015-04-22 11:14:38 +0100 | [diff] [blame] | 840 | #define MPLS_PERDEV_SYSCTL_OFFSET(field) \ |
| 841 | (&((struct mpls_dev *)0)->field) |
| 842 | |
| 843 | static const struct ctl_table mpls_dev_table[] = { |
| 844 | { |
| 845 | .procname = "input", |
| 846 | .maxlen = sizeof(int), |
| 847 | .mode = 0644, |
| 848 | .proc_handler = proc_dointvec, |
| 849 | .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled), |
| 850 | }, |
| 851 | { } |
| 852 | }; |
| 853 | |
| 854 | static int mpls_dev_sysctl_register(struct net_device *dev, |
| 855 | struct mpls_dev *mdev) |
| 856 | { |
| 857 | char path[sizeof("net/mpls/conf/") + IFNAMSIZ]; |
| 858 | struct ctl_table *table; |
| 859 | int i; |
| 860 | |
| 861 | table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL); |
| 862 | if (!table) |
| 863 | goto out; |
| 864 | |
| 865 | /* Table data contains only offsets relative to the base of |
| 866 | * the mdev at this point, so make them absolute. |
| 867 | */ |
| 868 | for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) |
| 869 | table[i].data = (char *)mdev + (uintptr_t)table[i].data; |
| 870 | |
| 871 | snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name); |
| 872 | |
| 873 | mdev->sysctl = register_net_sysctl(dev_net(dev), path, table); |
| 874 | if (!mdev->sysctl) |
| 875 | goto free; |
| 876 | |
| 877 | return 0; |
| 878 | |
| 879 | free: |
| 880 | kfree(table); |
| 881 | out: |
| 882 | return -ENOBUFS; |
| 883 | } |
| 884 | |
| 885 | static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev) |
| 886 | { |
| 887 | struct ctl_table *table; |
| 888 | |
| 889 | table = mdev->sysctl->ctl_table_arg; |
| 890 | unregister_net_sysctl_table(mdev->sysctl); |
| 891 | kfree(table); |
| 892 | } |
| 893 | |
Robert Shearman | 03c5774 | 2015-04-22 11:14:37 +0100 | [diff] [blame] | 894 | static struct mpls_dev *mpls_add_dev(struct net_device *dev) |
| 895 | { |
| 896 | struct mpls_dev *mdev; |
| 897 | int err = -ENOMEM; |
| 898 | |
| 899 | ASSERT_RTNL(); |
| 900 | |
| 901 | mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); |
| 902 | if (!mdev) |
| 903 | return ERR_PTR(err); |
| 904 | |
Robert Shearman | 37bde79 | 2015-04-22 11:14:38 +0100 | [diff] [blame] | 905 | err = mpls_dev_sysctl_register(dev, mdev); |
| 906 | if (err) |
| 907 | goto free; |
| 908 | |
Robert Shearman | 03c5774 | 2015-04-22 11:14:37 +0100 | [diff] [blame] | 909 | rcu_assign_pointer(dev->mpls_ptr, mdev); |
| 910 | |
| 911 | return mdev; |
Robert Shearman | 37bde79 | 2015-04-22 11:14:38 +0100 | [diff] [blame] | 912 | |
| 913 | free: |
| 914 | kfree(mdev); |
| 915 | return ERR_PTR(err); |
Robert Shearman | 03c5774 | 2015-04-22 11:14:37 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 918 | static void mpls_ifdown(struct net_device *dev, int event) |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 919 | { |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 920 | struct mpls_route __rcu **platform_label; |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 921 | struct net *net = dev_net(dev); |
| 922 | unsigned index; |
| 923 | |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 924 | platform_label = rtnl_dereference(net->mpls.platform_label); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 925 | for (index = 0; index < net->mpls.platform_labels; index++) { |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 926 | struct mpls_route *rt = rtnl_dereference(platform_label[index]); |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 927 | |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 928 | if (!rt) |
| 929 | continue; |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 930 | |
| 931 | change_nexthops(rt) { |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 932 | if (rtnl_dereference(nh->nh_dev) != dev) |
| 933 | continue; |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 934 | switch (event) { |
| 935 | case NETDEV_DOWN: |
| 936 | case NETDEV_UNREGISTER: |
| 937 | nh->nh_flags |= RTNH_F_DEAD; |
| 938 | /* fall through */ |
| 939 | case NETDEV_CHANGE: |
| 940 | nh->nh_flags |= RTNH_F_LINKDOWN; |
| 941 | ACCESS_ONCE(rt->rt_nhn_alive) = rt->rt_nhn_alive - 1; |
| 942 | break; |
| 943 | } |
| 944 | if (event == NETDEV_UNREGISTER) |
| 945 | RCU_INIT_POINTER(nh->nh_dev, NULL); |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 946 | } endfor_nexthops(rt); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 947 | } |
Robert Shearman | 03c5774 | 2015-04-22 11:14:37 +0100 | [diff] [blame] | 948 | |
Robert Shearman | 03c5774 | 2015-04-22 11:14:37 +0100 | [diff] [blame] | 949 | |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 950 | return; |
| 951 | } |
Robert Shearman | 37bde79 | 2015-04-22 11:14:38 +0100 | [diff] [blame] | 952 | |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 953 | static void mpls_ifup(struct net_device *dev, unsigned int nh_flags) |
| 954 | { |
| 955 | struct mpls_route __rcu **platform_label; |
| 956 | struct net *net = dev_net(dev); |
| 957 | unsigned index; |
| 958 | int alive; |
Robert Shearman | 03c5774 | 2015-04-22 11:14:37 +0100 | [diff] [blame] | 959 | |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 960 | platform_label = rtnl_dereference(net->mpls.platform_label); |
| 961 | for (index = 0; index < net->mpls.platform_labels; index++) { |
| 962 | struct mpls_route *rt = rtnl_dereference(platform_label[index]); |
| 963 | |
| 964 | if (!rt) |
| 965 | continue; |
| 966 | |
| 967 | alive = 0; |
| 968 | change_nexthops(rt) { |
| 969 | struct net_device *nh_dev = |
| 970 | rtnl_dereference(nh->nh_dev); |
| 971 | |
| 972 | if (!(nh->nh_flags & nh_flags)) { |
| 973 | alive++; |
| 974 | continue; |
| 975 | } |
| 976 | if (nh_dev != dev) |
| 977 | continue; |
| 978 | alive++; |
| 979 | nh->nh_flags &= ~nh_flags; |
| 980 | } endfor_nexthops(rt); |
| 981 | |
| 982 | ACCESS_ONCE(rt->rt_nhn_alive) = alive; |
| 983 | } |
| 984 | |
| 985 | return; |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | static int mpls_dev_notify(struct notifier_block *this, unsigned long event, |
| 989 | void *ptr) |
| 990 | { |
| 991 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Robert Shearman | 03c5774 | 2015-04-22 11:14:37 +0100 | [diff] [blame] | 992 | struct mpls_dev *mdev; |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 993 | unsigned int flags; |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 994 | |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 995 | if (event == NETDEV_REGISTER) { |
Robert Shearman | 03c5774 | 2015-04-22 11:14:37 +0100 | [diff] [blame] | 996 | /* For now just support ethernet devices */ |
| 997 | if ((dev->type == ARPHRD_ETHER) || |
| 998 | (dev->type == ARPHRD_LOOPBACK)) { |
| 999 | mdev = mpls_add_dev(dev); |
| 1000 | if (IS_ERR(mdev)) |
| 1001 | return notifier_from_errno(PTR_ERR(mdev)); |
| 1002 | } |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 1003 | return NOTIFY_OK; |
| 1004 | } |
Robert Shearman | 03c5774 | 2015-04-22 11:14:37 +0100 | [diff] [blame] | 1005 | |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 1006 | mdev = mpls_dev_get(dev); |
| 1007 | if (!mdev) |
| 1008 | return NOTIFY_OK; |
| 1009 | |
| 1010 | switch (event) { |
| 1011 | case NETDEV_DOWN: |
| 1012 | mpls_ifdown(dev, event); |
| 1013 | break; |
| 1014 | case NETDEV_UP: |
| 1015 | flags = dev_get_flags(dev); |
| 1016 | if (flags & (IFF_RUNNING | IFF_LOWER_UP)) |
| 1017 | mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN); |
| 1018 | else |
| 1019 | mpls_ifup(dev, RTNH_F_DEAD); |
| 1020 | break; |
| 1021 | case NETDEV_CHANGE: |
| 1022 | flags = dev_get_flags(dev); |
| 1023 | if (flags & (IFF_RUNNING | IFF_LOWER_UP)) |
| 1024 | mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN); |
| 1025 | else |
| 1026 | mpls_ifdown(dev, event); |
| 1027 | break; |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 1028 | case NETDEV_UNREGISTER: |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 1029 | mpls_ifdown(dev, event); |
| 1030 | mdev = mpls_dev_get(dev); |
| 1031 | if (mdev) { |
| 1032 | mpls_dev_sysctl_unregister(mdev); |
| 1033 | RCU_INIT_POINTER(dev->mpls_ptr, NULL); |
| 1034 | kfree_rcu(mdev, rcu); |
| 1035 | } |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 1036 | break; |
Robert Shearman | 0fae3bf | 2015-06-11 19:58:26 +0100 | [diff] [blame] | 1037 | case NETDEV_CHANGENAME: |
| 1038 | mdev = mpls_dev_get(dev); |
| 1039 | if (mdev) { |
| 1040 | int err; |
| 1041 | |
| 1042 | mpls_dev_sysctl_unregister(mdev); |
| 1043 | err = mpls_dev_sysctl_register(dev, mdev); |
| 1044 | if (err) |
| 1045 | return notifier_from_errno(err); |
| 1046 | } |
| 1047 | break; |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 1048 | } |
| 1049 | return NOTIFY_OK; |
| 1050 | } |
| 1051 | |
| 1052 | static struct notifier_block mpls_dev_notifier = { |
| 1053 | .notifier_call = mpls_dev_notify, |
| 1054 | }; |
| 1055 | |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1056 | static int nla_put_via(struct sk_buff *skb, |
Eric W. Biederman | b79bda3 | 2015-03-07 16:25:56 -0600 | [diff] [blame] | 1057 | u8 table, const void *addr, int alen) |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1058 | { |
Eric W. Biederman | b79bda3 | 2015-03-07 16:25:56 -0600 | [diff] [blame] | 1059 | static const int table_to_family[NEIGH_NR_TABLES + 1] = { |
| 1060 | AF_INET, AF_INET6, AF_DECnet, AF_PACKET, |
| 1061 | }; |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1062 | struct nlattr *nla; |
| 1063 | struct rtvia *via; |
Eric W. Biederman | b79bda3 | 2015-03-07 16:25:56 -0600 | [diff] [blame] | 1064 | int family = AF_UNSPEC; |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1065 | |
| 1066 | nla = nla_reserve(skb, RTA_VIA, alen + 2); |
| 1067 | if (!nla) |
| 1068 | return -EMSGSIZE; |
| 1069 | |
Eric W. Biederman | b79bda3 | 2015-03-07 16:25:56 -0600 | [diff] [blame] | 1070 | if (table <= NEIGH_NR_TABLES) |
| 1071 | family = table_to_family[table]; |
| 1072 | |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1073 | via = nla_data(nla); |
| 1074 | via->rtvia_family = family; |
| 1075 | memcpy(via->rtvia_addr, addr, alen); |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
Eric W. Biederman | 966bae3 | 2015-03-03 19:13:19 -0600 | [diff] [blame] | 1079 | int nla_put_labels(struct sk_buff *skb, int attrtype, |
| 1080 | u8 labels, const u32 label[]) |
| 1081 | { |
| 1082 | struct nlattr *nla; |
| 1083 | struct mpls_shim_hdr *nla_label; |
| 1084 | bool bos; |
| 1085 | int i; |
| 1086 | nla = nla_reserve(skb, attrtype, labels*4); |
| 1087 | if (!nla) |
| 1088 | return -EMSGSIZE; |
| 1089 | |
| 1090 | nla_label = nla_data(nla); |
| 1091 | bos = true; |
| 1092 | for (i = labels - 1; i >= 0; i--) { |
| 1093 | nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos); |
| 1094 | bos = false; |
| 1095 | } |
| 1096 | |
| 1097 | return 0; |
| 1098 | } |
Roopa Prabhu | face018 | 2015-07-21 10:43:52 +0200 | [diff] [blame] | 1099 | EXPORT_SYMBOL_GPL(nla_put_labels); |
Eric W. Biederman | 966bae3 | 2015-03-03 19:13:19 -0600 | [diff] [blame] | 1100 | |
| 1101 | int nla_get_labels(const struct nlattr *nla, |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1102 | u32 max_labels, u8 *labels, u32 label[]) |
Eric W. Biederman | 966bae3 | 2015-03-03 19:13:19 -0600 | [diff] [blame] | 1103 | { |
| 1104 | unsigned len = nla_len(nla); |
| 1105 | unsigned nla_labels; |
| 1106 | struct mpls_shim_hdr *nla_label; |
| 1107 | bool bos; |
| 1108 | int i; |
| 1109 | |
| 1110 | /* len needs to be an even multiple of 4 (the label size) */ |
| 1111 | if (len & 3) |
| 1112 | return -EINVAL; |
| 1113 | |
| 1114 | /* Limit the number of new labels allowed */ |
| 1115 | nla_labels = len/4; |
| 1116 | if (nla_labels > max_labels) |
| 1117 | return -EINVAL; |
| 1118 | |
| 1119 | nla_label = nla_data(nla); |
| 1120 | bos = true; |
| 1121 | for (i = nla_labels - 1; i >= 0; i--, bos = false) { |
| 1122 | struct mpls_entry_decoded dec; |
| 1123 | dec = mpls_entry_decode(nla_label + i); |
| 1124 | |
| 1125 | /* Ensure the bottom of stack flag is properly set |
| 1126 | * and ttl and tc are both clear. |
| 1127 | */ |
| 1128 | if ((dec.bos != bos) || dec.ttl || dec.tc) |
| 1129 | return -EINVAL; |
| 1130 | |
Robert Shearman | 5a9ab01 | 2015-04-22 11:14:39 +0100 | [diff] [blame] | 1131 | switch (dec.label) { |
Tom Herbert | 78f5b89 | 2015-05-07 08:08:51 -0700 | [diff] [blame] | 1132 | case MPLS_LABEL_IMPLNULL: |
Robert Shearman | 5a9ab01 | 2015-04-22 11:14:39 +0100 | [diff] [blame] | 1133 | /* RFC3032: This is a label that an LSR may |
| 1134 | * assign and distribute, but which never |
| 1135 | * actually appears in the encapsulation. |
| 1136 | */ |
| 1137 | return -EINVAL; |
| 1138 | } |
| 1139 | |
Eric W. Biederman | 966bae3 | 2015-03-03 19:13:19 -0600 | [diff] [blame] | 1140 | label[i] = dec.label; |
| 1141 | } |
| 1142 | *labels = nla_labels; |
| 1143 | return 0; |
| 1144 | } |
Roopa Prabhu | face018 | 2015-07-21 10:43:52 +0200 | [diff] [blame] | 1145 | EXPORT_SYMBOL_GPL(nla_get_labels); |
Eric W. Biederman | 966bae3 | 2015-03-03 19:13:19 -0600 | [diff] [blame] | 1146 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1147 | int nla_get_via(const struct nlattr *nla, u8 *via_alen, |
| 1148 | u8 *via_table, u8 via_addr[]) |
| 1149 | { |
| 1150 | struct rtvia *via = nla_data(nla); |
| 1151 | int err = -EINVAL; |
| 1152 | int alen; |
| 1153 | |
| 1154 | if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) |
| 1155 | goto errout; |
| 1156 | alen = nla_len(nla) - |
| 1157 | offsetof(struct rtvia, rtvia_addr); |
| 1158 | if (alen > MAX_VIA_ALEN) |
| 1159 | goto errout; |
| 1160 | |
| 1161 | /* Validate the address family */ |
| 1162 | switch (via->rtvia_family) { |
| 1163 | case AF_PACKET: |
| 1164 | *via_table = NEIGH_LINK_TABLE; |
| 1165 | break; |
| 1166 | case AF_INET: |
| 1167 | *via_table = NEIGH_ARP_TABLE; |
| 1168 | if (alen != 4) |
| 1169 | goto errout; |
| 1170 | break; |
| 1171 | case AF_INET6: |
| 1172 | *via_table = NEIGH_ND_TABLE; |
| 1173 | if (alen != 16) |
| 1174 | goto errout; |
| 1175 | break; |
| 1176 | default: |
| 1177 | /* Unsupported address family */ |
| 1178 | goto errout; |
| 1179 | } |
| 1180 | |
| 1181 | memcpy(via_addr, via->rtvia_addr, alen); |
| 1182 | *via_alen = alen; |
| 1183 | err = 0; |
| 1184 | |
| 1185 | errout: |
| 1186 | return err; |
| 1187 | } |
| 1188 | |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1189 | static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 1190 | struct mpls_route_config *cfg) |
| 1191 | { |
| 1192 | struct rtmsg *rtm; |
| 1193 | struct nlattr *tb[RTA_MAX+1]; |
| 1194 | int index; |
| 1195 | int err; |
| 1196 | |
| 1197 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy); |
| 1198 | if (err < 0) |
| 1199 | goto errout; |
| 1200 | |
| 1201 | err = -EINVAL; |
| 1202 | rtm = nlmsg_data(nlh); |
| 1203 | memset(cfg, 0, sizeof(*cfg)); |
| 1204 | |
| 1205 | if (rtm->rtm_family != AF_MPLS) |
| 1206 | goto errout; |
| 1207 | if (rtm->rtm_dst_len != 20) |
| 1208 | goto errout; |
| 1209 | if (rtm->rtm_src_len != 0) |
| 1210 | goto errout; |
| 1211 | if (rtm->rtm_tos != 0) |
| 1212 | goto errout; |
| 1213 | if (rtm->rtm_table != RT_TABLE_MAIN) |
| 1214 | goto errout; |
| 1215 | /* Any value is acceptable for rtm_protocol */ |
| 1216 | |
| 1217 | /* As mpls uses destination specific addresses |
| 1218 | * (or source specific address in the case of multicast) |
| 1219 | * all addresses have universal scope. |
| 1220 | */ |
| 1221 | if (rtm->rtm_scope != RT_SCOPE_UNIVERSE) |
| 1222 | goto errout; |
| 1223 | if (rtm->rtm_type != RTN_UNICAST) |
| 1224 | goto errout; |
| 1225 | if (rtm->rtm_flags != 0) |
| 1226 | goto errout; |
| 1227 | |
| 1228 | cfg->rc_label = LABEL_NOT_SPECIFIED; |
| 1229 | cfg->rc_protocol = rtm->rtm_protocol; |
| 1230 | cfg->rc_nlflags = nlh->nlmsg_flags; |
| 1231 | cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid; |
| 1232 | cfg->rc_nlinfo.nlh = nlh; |
| 1233 | cfg->rc_nlinfo.nl_net = sock_net(skb->sk); |
| 1234 | |
| 1235 | for (index = 0; index <= RTA_MAX; index++) { |
| 1236 | struct nlattr *nla = tb[index]; |
| 1237 | if (!nla) |
| 1238 | continue; |
| 1239 | |
| 1240 | switch(index) { |
| 1241 | case RTA_OIF: |
| 1242 | cfg->rc_ifindex = nla_get_u32(nla); |
| 1243 | break; |
| 1244 | case RTA_NEWDST: |
| 1245 | if (nla_get_labels(nla, MAX_NEW_LABELS, |
| 1246 | &cfg->rc_output_labels, |
| 1247 | cfg->rc_output_label)) |
| 1248 | goto errout; |
| 1249 | break; |
| 1250 | case RTA_DST: |
| 1251 | { |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1252 | u8 label_count; |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1253 | if (nla_get_labels(nla, 1, &label_count, |
| 1254 | &cfg->rc_label)) |
| 1255 | goto errout; |
| 1256 | |
Robert Shearman | a6affd2 | 2015-08-03 17:50:04 +0100 | [diff] [blame] | 1257 | /* Reserved labels may not be set */ |
| 1258 | if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED) |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1259 | goto errout; |
| 1260 | |
| 1261 | break; |
| 1262 | } |
| 1263 | case RTA_VIA: |
| 1264 | { |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1265 | if (nla_get_via(nla, &cfg->rc_via_alen, |
| 1266 | &cfg->rc_via_table, cfg->rc_via)) |
Robert Shearman | f8d54af | 2015-03-06 10:47:00 +0000 | [diff] [blame] | 1267 | goto errout; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1268 | break; |
| 1269 | } |
| 1270 | case RTA_MULTIPATH: |
| 1271 | { |
| 1272 | cfg->rc_mp = nla_data(nla); |
| 1273 | cfg->rc_mp_len = nla_len(nla); |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1274 | break; |
| 1275 | } |
| 1276 | default: |
| 1277 | /* Unsupported attribute */ |
| 1278 | goto errout; |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | err = 0; |
| 1283 | errout: |
| 1284 | return err; |
| 1285 | } |
| 1286 | |
| 1287 | static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 1288 | { |
| 1289 | struct mpls_route_config cfg; |
| 1290 | int err; |
| 1291 | |
| 1292 | err = rtm_to_route_config(skb, nlh, &cfg); |
| 1293 | if (err < 0) |
| 1294 | return err; |
| 1295 | |
| 1296 | return mpls_route_del(&cfg); |
| 1297 | } |
| 1298 | |
| 1299 | |
| 1300 | static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 1301 | { |
| 1302 | struct mpls_route_config cfg; |
| 1303 | int err; |
| 1304 | |
| 1305 | err = rtm_to_route_config(skb, nlh, &cfg); |
| 1306 | if (err < 0) |
| 1307 | return err; |
| 1308 | |
| 1309 | return mpls_route_add(&cfg); |
| 1310 | } |
| 1311 | |
| 1312 | static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event, |
| 1313 | u32 label, struct mpls_route *rt, int flags) |
| 1314 | { |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 1315 | struct net_device *dev; |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1316 | struct nlmsghdr *nlh; |
| 1317 | struct rtmsg *rtm; |
| 1318 | |
| 1319 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags); |
| 1320 | if (nlh == NULL) |
| 1321 | return -EMSGSIZE; |
| 1322 | |
| 1323 | rtm = nlmsg_data(nlh); |
| 1324 | rtm->rtm_family = AF_MPLS; |
| 1325 | rtm->rtm_dst_len = 20; |
| 1326 | rtm->rtm_src_len = 0; |
| 1327 | rtm->rtm_tos = 0; |
| 1328 | rtm->rtm_table = RT_TABLE_MAIN; |
| 1329 | rtm->rtm_protocol = rt->rt_protocol; |
| 1330 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; |
| 1331 | rtm->rtm_type = RTN_UNICAST; |
| 1332 | rtm->rtm_flags = 0; |
| 1333 | |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1334 | if (nla_put_labels(skb, RTA_DST, 1, &label)) |
| 1335 | goto nla_put_failure; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1336 | if (rt->rt_nhn == 1) { |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 1337 | const struct mpls_nh *nh = rt->rt_nh; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1338 | |
| 1339 | if (nh->nh_labels && |
| 1340 | nla_put_labels(skb, RTA_NEWDST, nh->nh_labels, |
| 1341 | nh->nh_label)) |
| 1342 | goto nla_put_failure; |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 1343 | if (nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh), |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1344 | nh->nh_via_alen)) |
| 1345 | goto nla_put_failure; |
| 1346 | dev = rtnl_dereference(nh->nh_dev); |
| 1347 | if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex)) |
| 1348 | goto nla_put_failure; |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 1349 | if (nh->nh_flags & RTNH_F_LINKDOWN) |
| 1350 | rtm->rtm_flags |= RTNH_F_LINKDOWN; |
| 1351 | if (nh->nh_flags & RTNH_F_DEAD) |
| 1352 | rtm->rtm_flags |= RTNH_F_DEAD; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1353 | } else { |
| 1354 | struct rtnexthop *rtnh; |
| 1355 | struct nlattr *mp; |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 1356 | int dead = 0; |
| 1357 | int linkdown = 0; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1358 | |
| 1359 | mp = nla_nest_start(skb, RTA_MULTIPATH); |
| 1360 | if (!mp) |
| 1361 | goto nla_put_failure; |
| 1362 | |
| 1363 | for_nexthops(rt) { |
| 1364 | rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); |
| 1365 | if (!rtnh) |
| 1366 | goto nla_put_failure; |
| 1367 | |
| 1368 | dev = rtnl_dereference(nh->nh_dev); |
| 1369 | if (dev) |
| 1370 | rtnh->rtnh_ifindex = dev->ifindex; |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 1371 | if (nh->nh_flags & RTNH_F_LINKDOWN) { |
| 1372 | rtnh->rtnh_flags |= RTNH_F_LINKDOWN; |
| 1373 | linkdown++; |
| 1374 | } |
| 1375 | if (nh->nh_flags & RTNH_F_DEAD) { |
| 1376 | rtnh->rtnh_flags |= RTNH_F_DEAD; |
| 1377 | dead++; |
| 1378 | } |
| 1379 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1380 | if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST, |
| 1381 | nh->nh_labels, |
| 1382 | nh->nh_label)) |
| 1383 | goto nla_put_failure; |
| 1384 | if (nla_put_via(skb, nh->nh_via_table, |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 1385 | mpls_nh_via(rt, nh), |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1386 | nh->nh_via_alen)) |
| 1387 | goto nla_put_failure; |
| 1388 | |
| 1389 | /* length of rtnetlink header + attributes */ |
| 1390 | rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; |
| 1391 | } endfor_nexthops(rt); |
| 1392 | |
Roopa Prabhu | c89359a | 2015-12-01 22:18:11 -0800 | [diff] [blame^] | 1393 | if (linkdown == rt->rt_nhn) |
| 1394 | rtm->rtm_flags |= RTNH_F_LINKDOWN; |
| 1395 | if (dead == rt->rt_nhn) |
| 1396 | rtm->rtm_flags |= RTNH_F_DEAD; |
| 1397 | |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1398 | nla_nest_end(skb, mp); |
| 1399 | } |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1400 | |
| 1401 | nlmsg_end(skb, nlh); |
| 1402 | return 0; |
| 1403 | |
| 1404 | nla_put_failure: |
| 1405 | nlmsg_cancel(skb, nlh); |
| 1406 | return -EMSGSIZE; |
| 1407 | } |
| 1408 | |
| 1409 | static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb) |
| 1410 | { |
| 1411 | struct net *net = sock_net(skb->sk); |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 1412 | struct mpls_route __rcu **platform_label; |
| 1413 | size_t platform_labels; |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1414 | unsigned int index; |
| 1415 | |
| 1416 | ASSERT_RTNL(); |
| 1417 | |
| 1418 | index = cb->args[0]; |
Robert Shearman | a6affd2 | 2015-08-03 17:50:04 +0100 | [diff] [blame] | 1419 | if (index < MPLS_LABEL_FIRST_UNRESERVED) |
| 1420 | index = MPLS_LABEL_FIRST_UNRESERVED; |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1421 | |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 1422 | platform_label = rtnl_dereference(net->mpls.platform_label); |
| 1423 | platform_labels = net->mpls.platform_labels; |
| 1424 | for (; index < platform_labels; index++) { |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1425 | struct mpls_route *rt; |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 1426 | rt = rtnl_dereference(platform_label[index]); |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1427 | if (!rt) |
| 1428 | continue; |
| 1429 | |
| 1430 | if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid, |
| 1431 | cb->nlh->nlmsg_seq, RTM_NEWROUTE, |
| 1432 | index, rt, NLM_F_MULTI) < 0) |
| 1433 | break; |
| 1434 | } |
| 1435 | cb->args[0] = index; |
| 1436 | |
| 1437 | return skb->len; |
| 1438 | } |
| 1439 | |
Eric W. Biederman | 8de147d | 2015-03-03 19:14:31 -0600 | [diff] [blame] | 1440 | static inline size_t lfib_nlmsg_size(struct mpls_route *rt) |
| 1441 | { |
| 1442 | size_t payload = |
| 1443 | NLMSG_ALIGN(sizeof(struct rtmsg)) |
Eric W. Biederman | 8de147d | 2015-03-03 19:14:31 -0600 | [diff] [blame] | 1444 | + nla_total_size(4); /* RTA_DST */ |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1445 | |
| 1446 | if (rt->rt_nhn == 1) { |
| 1447 | struct mpls_nh *nh = rt->rt_nh; |
| 1448 | |
| 1449 | if (nh->nh_dev) |
| 1450 | payload += nla_total_size(4); /* RTA_OIF */ |
| 1451 | payload += nla_total_size(2 + nh->nh_via_alen); /* RTA_VIA */ |
| 1452 | if (nh->nh_labels) /* RTA_NEWDST */ |
| 1453 | payload += nla_total_size(nh->nh_labels * 4); |
| 1454 | } else { |
| 1455 | /* each nexthop is packed in an attribute */ |
| 1456 | size_t nhsize = 0; |
| 1457 | |
| 1458 | for_nexthops(rt) { |
| 1459 | nhsize += nla_total_size(sizeof(struct rtnexthop)); |
| 1460 | nhsize += nla_total_size(2 + nh->nh_via_alen); |
| 1461 | if (nh->nh_labels) |
| 1462 | nhsize += nla_total_size(nh->nh_labels * 4); |
| 1463 | } endfor_nexthops(rt); |
| 1464 | /* nested attribute */ |
| 1465 | payload += nla_total_size(nhsize); |
| 1466 | } |
| 1467 | |
Eric W. Biederman | 8de147d | 2015-03-03 19:14:31 -0600 | [diff] [blame] | 1468 | return payload; |
| 1469 | } |
| 1470 | |
| 1471 | static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt, |
| 1472 | struct nlmsghdr *nlh, struct net *net, u32 portid, |
| 1473 | unsigned int nlm_flags) |
| 1474 | { |
| 1475 | struct sk_buff *skb; |
| 1476 | u32 seq = nlh ? nlh->nlmsg_seq : 0; |
| 1477 | int err = -ENOBUFS; |
| 1478 | |
| 1479 | skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL); |
| 1480 | if (skb == NULL) |
| 1481 | goto errout; |
| 1482 | |
| 1483 | err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags); |
| 1484 | if (err < 0) { |
| 1485 | /* -EMSGSIZE implies BUG in lfib_nlmsg_size */ |
| 1486 | WARN_ON(err == -EMSGSIZE); |
| 1487 | kfree_skb(skb); |
| 1488 | goto errout; |
| 1489 | } |
| 1490 | rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL); |
| 1491 | |
| 1492 | return; |
| 1493 | errout: |
| 1494 | if (err < 0) |
| 1495 | rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err); |
| 1496 | } |
| 1497 | |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1498 | static int resize_platform_label_table(struct net *net, size_t limit) |
| 1499 | { |
| 1500 | size_t size = sizeof(struct mpls_route *) * limit; |
| 1501 | size_t old_limit; |
| 1502 | size_t cp_size; |
| 1503 | struct mpls_route __rcu **labels = NULL, **old; |
| 1504 | struct mpls_route *rt0 = NULL, *rt2 = NULL; |
| 1505 | unsigned index; |
| 1506 | |
| 1507 | if (size) { |
| 1508 | labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); |
| 1509 | if (!labels) |
| 1510 | labels = vzalloc(size); |
| 1511 | |
| 1512 | if (!labels) |
| 1513 | goto nolabels; |
| 1514 | } |
| 1515 | |
| 1516 | /* In case the predefined labels need to be populated */ |
Tom Herbert | 78f5b89 | 2015-05-07 08:08:51 -0700 | [diff] [blame] | 1517 | if (limit > MPLS_LABEL_IPV4NULL) { |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1518 | struct net_device *lo = net->loopback_dev; |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 1519 | rt0 = mpls_rt_alloc(1, lo->addr_len); |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1520 | if (!rt0) |
| 1521 | goto nort0; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1522 | RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo); |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1523 | rt0->rt_protocol = RTPROT_KERNEL; |
Robert Shearman | 118d523 | 2015-08-06 11:04:56 +0100 | [diff] [blame] | 1524 | rt0->rt_payload_type = MPT_IPV4; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1525 | rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE; |
Robert Shearman | b4e04fc | 2015-10-27 00:37:35 +0000 | [diff] [blame] | 1526 | rt0->rt_nh->nh_via_alen = lo->addr_len; |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 1527 | memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr, |
| 1528 | lo->addr_len); |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1529 | } |
Tom Herbert | 78f5b89 | 2015-05-07 08:08:51 -0700 | [diff] [blame] | 1530 | if (limit > MPLS_LABEL_IPV6NULL) { |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1531 | struct net_device *lo = net->loopback_dev; |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 1532 | rt2 = mpls_rt_alloc(1, lo->addr_len); |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1533 | if (!rt2) |
| 1534 | goto nort2; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1535 | RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo); |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1536 | rt2->rt_protocol = RTPROT_KERNEL; |
Robert Shearman | 118d523 | 2015-08-06 11:04:56 +0100 | [diff] [blame] | 1537 | rt2->rt_payload_type = MPT_IPV6; |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1538 | rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE; |
Robert Shearman | b4e04fc | 2015-10-27 00:37:35 +0000 | [diff] [blame] | 1539 | rt2->rt_nh->nh_via_alen = lo->addr_len; |
Robert Shearman | cf4b24f | 2015-10-27 00:37:36 +0000 | [diff] [blame] | 1540 | memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr, |
| 1541 | lo->addr_len); |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | rtnl_lock(); |
| 1545 | /* Remember the original table */ |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 1546 | old = rtnl_dereference(net->mpls.platform_label); |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1547 | old_limit = net->mpls.platform_labels; |
| 1548 | |
| 1549 | /* Free any labels beyond the new table */ |
| 1550 | for (index = limit; index < old_limit; index++) |
Roopa Prabhu | f8efb73 | 2015-10-23 06:03:27 -0700 | [diff] [blame] | 1551 | mpls_route_update(net, index, NULL, NULL); |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1552 | |
| 1553 | /* Copy over the old labels */ |
| 1554 | cp_size = size; |
| 1555 | if (old_limit < limit) |
| 1556 | cp_size = old_limit * sizeof(struct mpls_route *); |
| 1557 | |
| 1558 | memcpy(labels, old, cp_size); |
| 1559 | |
| 1560 | /* If needed set the predefined labels */ |
Tom Herbert | 78f5b89 | 2015-05-07 08:08:51 -0700 | [diff] [blame] | 1561 | if ((old_limit <= MPLS_LABEL_IPV6NULL) && |
| 1562 | (limit > MPLS_LABEL_IPV6NULL)) { |
| 1563 | RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2); |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1564 | rt2 = NULL; |
| 1565 | } |
| 1566 | |
Tom Herbert | 78f5b89 | 2015-05-07 08:08:51 -0700 | [diff] [blame] | 1567 | if ((old_limit <= MPLS_LABEL_IPV4NULL) && |
| 1568 | (limit > MPLS_LABEL_IPV4NULL)) { |
| 1569 | RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0); |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1570 | rt0 = NULL; |
| 1571 | } |
| 1572 | |
| 1573 | /* Update the global pointers */ |
| 1574 | net->mpls.platform_labels = limit; |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 1575 | rcu_assign_pointer(net->mpls.platform_label, labels); |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1576 | |
| 1577 | rtnl_unlock(); |
| 1578 | |
| 1579 | mpls_rt_free(rt2); |
| 1580 | mpls_rt_free(rt0); |
| 1581 | |
| 1582 | if (old) { |
| 1583 | synchronize_rcu(); |
| 1584 | kvfree(old); |
| 1585 | } |
| 1586 | return 0; |
| 1587 | |
| 1588 | nort2: |
| 1589 | mpls_rt_free(rt0); |
| 1590 | nort0: |
| 1591 | kvfree(labels); |
| 1592 | nolabels: |
| 1593 | return -ENOMEM; |
| 1594 | } |
| 1595 | |
| 1596 | static int mpls_platform_labels(struct ctl_table *table, int write, |
| 1597 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 1598 | { |
| 1599 | struct net *net = table->data; |
| 1600 | int platform_labels = net->mpls.platform_labels; |
| 1601 | int ret; |
| 1602 | struct ctl_table tmp = { |
| 1603 | .procname = table->procname, |
| 1604 | .data = &platform_labels, |
| 1605 | .maxlen = sizeof(int), |
| 1606 | .mode = table->mode, |
| 1607 | .extra1 = &zero, |
| 1608 | .extra2 = &label_limit, |
| 1609 | }; |
| 1610 | |
| 1611 | ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); |
| 1612 | |
| 1613 | if (write && ret == 0) |
| 1614 | ret = resize_platform_label_table(net, platform_labels); |
| 1615 | |
| 1616 | return ret; |
| 1617 | } |
| 1618 | |
Robert Shearman | 37bde79 | 2015-04-22 11:14:38 +0100 | [diff] [blame] | 1619 | static const struct ctl_table mpls_table[] = { |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1620 | { |
| 1621 | .procname = "platform_labels", |
| 1622 | .data = NULL, |
| 1623 | .maxlen = sizeof(int), |
| 1624 | .mode = 0644, |
| 1625 | .proc_handler = mpls_platform_labels, |
| 1626 | }, |
| 1627 | { } |
| 1628 | }; |
| 1629 | |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 1630 | static int mpls_net_init(struct net *net) |
| 1631 | { |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1632 | struct ctl_table *table; |
| 1633 | |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 1634 | net->mpls.platform_labels = 0; |
| 1635 | net->mpls.platform_label = NULL; |
| 1636 | |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1637 | table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL); |
| 1638 | if (table == NULL) |
| 1639 | return -ENOMEM; |
| 1640 | |
| 1641 | table[0].data = net; |
| 1642 | net->mpls.ctl = register_net_sysctl(net, "net/mpls", table); |
Nikolay Aleksandrov | 6ea3c9d5 | 2015-08-31 10:44:19 -0700 | [diff] [blame] | 1643 | if (net->mpls.ctl == NULL) { |
| 1644 | kfree(table); |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1645 | return -ENOMEM; |
Nikolay Aleksandrov | 6ea3c9d5 | 2015-08-31 10:44:19 -0700 | [diff] [blame] | 1646 | } |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1647 | |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 1648 | return 0; |
| 1649 | } |
| 1650 | |
| 1651 | static void mpls_net_exit(struct net *net) |
| 1652 | { |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 1653 | struct mpls_route __rcu **platform_label; |
| 1654 | size_t platform_labels; |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1655 | struct ctl_table *table; |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 1656 | unsigned int index; |
| 1657 | |
Eric W. Biederman | 7720c01 | 2015-03-03 19:11:20 -0600 | [diff] [blame] | 1658 | table = net->mpls.ctl->ctl_table_arg; |
| 1659 | unregister_net_sysctl_table(net->mpls.ctl); |
| 1660 | kfree(table); |
| 1661 | |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 1662 | /* An rcu grace period has passed since there was a device in |
| 1663 | * the network namespace (and thus the last in flight packet) |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 1664 | * left this network namespace. This is because |
| 1665 | * unregister_netdevice_many and netdev_run_todo has completed |
| 1666 | * for each network device that was in this network namespace. |
| 1667 | * |
| 1668 | * As such no additional rcu synchronization is necessary when |
| 1669 | * freeing the platform_label table. |
| 1670 | */ |
| 1671 | rtnl_lock(); |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 1672 | platform_label = rtnl_dereference(net->mpls.platform_label); |
| 1673 | platform_labels = net->mpls.platform_labels; |
| 1674 | for (index = 0; index < platform_labels; index++) { |
| 1675 | struct mpls_route *rt = rtnl_dereference(platform_label[index]); |
| 1676 | RCU_INIT_POINTER(platform_label[index], NULL); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 1677 | mpls_rt_free(rt); |
| 1678 | } |
| 1679 | rtnl_unlock(); |
| 1680 | |
Eric W. Biederman | 19d0c34 | 2015-03-07 16:21:56 -0600 | [diff] [blame] | 1681 | kvfree(platform_label); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | static struct pernet_operations mpls_net_ops = { |
| 1685 | .init = mpls_net_init, |
| 1686 | .exit = mpls_net_exit, |
| 1687 | }; |
| 1688 | |
| 1689 | static int __init mpls_init(void) |
| 1690 | { |
| 1691 | int err; |
| 1692 | |
| 1693 | BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4); |
| 1694 | |
| 1695 | err = register_pernet_subsys(&mpls_net_ops); |
| 1696 | if (err) |
| 1697 | goto out; |
| 1698 | |
| 1699 | err = register_netdevice_notifier(&mpls_dev_notifier); |
| 1700 | if (err) |
| 1701 | goto out_unregister_pernet; |
| 1702 | |
| 1703 | dev_add_pack(&mpls_packet_type); |
| 1704 | |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1705 | rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL); |
| 1706 | rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL); |
| 1707 | rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 1708 | err = 0; |
| 1709 | out: |
| 1710 | return err; |
| 1711 | |
| 1712 | out_unregister_pernet: |
| 1713 | unregister_pernet_subsys(&mpls_net_ops); |
| 1714 | goto out; |
| 1715 | } |
| 1716 | module_init(mpls_init); |
| 1717 | |
| 1718 | static void __exit mpls_exit(void) |
| 1719 | { |
Eric W. Biederman | 03c0566 | 2015-03-03 19:13:56 -0600 | [diff] [blame] | 1720 | rtnl_unregister_all(PF_MPLS); |
Eric W. Biederman | 0189197 | 2015-03-03 19:10:47 -0600 | [diff] [blame] | 1721 | dev_remove_pack(&mpls_packet_type); |
| 1722 | unregister_netdevice_notifier(&mpls_dev_notifier); |
| 1723 | unregister_pernet_subsys(&mpls_net_ops); |
| 1724 | } |
| 1725 | module_exit(mpls_exit); |
| 1726 | |
| 1727 | MODULE_DESCRIPTION("MultiProtocol Label Switching"); |
| 1728 | MODULE_LICENSE("GPL v2"); |
| 1729 | MODULE_ALIAS_NETPROTO(PF_MPLS); |