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