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