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