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