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