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