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