blob: 1863b94133e4bc260d510d8d403213d1596dc373 [file] [log] [blame]
Eric W. Biederman01891972015-03-03 19:10:47 -06001#include <linux/types.h>
2#include <linux/skbuff.h>
3#include <linux/socket.h>
Eric W. Biederman7720c012015-03-03 19:11:20 -06004#include <linux/sysctl.h>
Eric W. Biederman01891972015-03-03 19:10:47 -06005#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 Ahern24045a02017-02-20 08:03:30 -080010#include <linux/netconf.h>
Stephen Rothwell4b5edb22015-03-05 13:37:05 +110011#include <linux/vmalloc.h>
Robert Shearman27d69102017-01-16 14:16:37 +000012#include <linux/percpu.h>
Eric W. Biederman01891972015-03-03 19:10:47 -060013#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 Prabhubf215632015-07-30 13:34:54 -070020#if IS_ENABLED(CONFIG_IPV6)
21#include <net/ipv6.h>
Roopa Prabhubf215632015-07-30 13:34:54 -070022#endif
Robert Shearman27d69102017-01-16 14:16:37 +000023#include <net/addrconf.h>
Roopa Prabhuf8efb732015-10-23 06:03:27 -070024#include <net/nexthop.h>
Eric W. Biederman01891972015-03-03 19:10:47 -060025#include "internal.h"
26
David Ahern59b20962017-03-31 07:14:01 -070027#define MAX_NEW_LABELS 2
28
Robert Shearman1c78efa2015-10-23 06:03:28 -070029/* Maximum number of labels to look ahead at when selecting a path of
30 * a multipath route
31 */
32#define MAX_MP_SELECT_LABELS 4
33
Robert Shearmaneb7809f2015-12-10 19:30:50 +000034#define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
35
Eric W. Biederman7720c012015-03-03 19:11:20 -060036static int zero = 0;
Robert Shearman5b441ac2017-03-10 20:43:24 +000037static int one = 1;
Eric W. Biederman7720c012015-03-03 19:11:20 -060038static int label_limit = (1 << 20) - 1;
Robert Shearmana59166e2017-03-10 20:43:25 +000039static int ttl_max = 255;
Eric W. Biederman7720c012015-03-03 19:11:20 -060040
Eric W. Biederman8de147d2015-03-03 19:14:31 -060041static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
42 struct nlmsghdr *nlh, struct net *net, u32 portid,
43 unsigned int nlm_flags);
44
Eric W. Biederman01891972015-03-03 19:10:47 -060045static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
46{
47 struct mpls_route *rt = NULL;
48
49 if (index < net->mpls.platform_labels) {
50 struct mpls_route __rcu **platform_label =
51 rcu_dereference(net->mpls.platform_label);
52 rt = rcu_dereference(platform_label[index]);
53 }
54 return rt;
55}
56
Roopa Prabhuface0182015-07-21 10:43:52 +020057bool mpls_output_possible(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060058{
59 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
60}
Roopa Prabhuface0182015-07-21 10:43:52 +020061EXPORT_SYMBOL_GPL(mpls_output_possible);
Eric W. Biederman01891972015-03-03 19:10:47 -060062
Robert Shearmancf4b24f2015-10-27 00:37:36 +000063static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
64{
David Ahern59b20962017-03-31 07:14:01 -070065 return (u8 *)nh + rt->rt_via_offset;
Robert Shearmancf4b24f2015-10-27 00:37:36 +000066}
67
68static const u8 *mpls_nh_via(const struct mpls_route *rt,
69 const struct mpls_nh *nh)
70{
71 return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
72}
73
Roopa Prabhuf8efb732015-10-23 06:03:27 -070074static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
Eric W. Biederman01891972015-03-03 19:10:47 -060075{
76 /* The size of the layer 2.5 labels to be added for this route */
Roopa Prabhuf8efb732015-10-23 06:03:27 -070077 return nh->nh_labels * sizeof(struct mpls_shim_hdr);
Eric W. Biederman01891972015-03-03 19:10:47 -060078}
79
Roopa Prabhuface0182015-07-21 10:43:52 +020080unsigned int mpls_dev_mtu(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060081{
82 /* The amount of data the layer 2 frame can hold */
83 return dev->mtu;
84}
Roopa Prabhuface0182015-07-21 10:43:52 +020085EXPORT_SYMBOL_GPL(mpls_dev_mtu);
Eric W. Biederman01891972015-03-03 19:10:47 -060086
Roopa Prabhuface0182015-07-21 10:43:52 +020087bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
Eric W. Biederman01891972015-03-03 19:10:47 -060088{
89 if (skb->len <= mtu)
90 return false;
91
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -030092 if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
Eric W. Biederman01891972015-03-03 19:10:47 -060093 return false;
94
95 return true;
96}
Roopa Prabhuface0182015-07-21 10:43:52 +020097EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
Eric W. Biederman01891972015-03-03 19:10:47 -060098
Robert Shearman27d69102017-01-16 14:16:37 +000099void mpls_stats_inc_outucastpkts(struct net_device *dev,
100 const struct sk_buff *skb)
101{
102 struct mpls_dev *mdev;
103
104 if (skb->protocol == htons(ETH_P_MPLS_UC)) {
105 mdev = mpls_dev_get(dev);
106 if (mdev)
107 MPLS_INC_STATS_LEN(mdev, skb->len,
108 tx_packets,
109 tx_bytes);
110 } else if (skb->protocol == htons(ETH_P_IP)) {
111 IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
112#if IS_ENABLED(CONFIG_IPV6)
113 } else if (skb->protocol == htons(ETH_P_IPV6)) {
114 struct inet6_dev *in6dev = __in6_dev_get(dev);
115
116 if (in6dev)
117 IP6_UPD_PO_STATS(dev_net(dev), in6dev,
118 IPSTATS_MIB_OUT, skb->len);
119#endif
120 }
121}
122EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
123
David Ahern9f427a0e2017-01-20 12:58:34 -0800124static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700125{
Robert Shearman1c78efa2015-10-23 06:03:28 -0700126 struct mpls_entry_decoded dec;
David Ahern9f427a0e2017-01-20 12:58:34 -0800127 unsigned int mpls_hdr_len = 0;
Robert Shearman1c78efa2015-10-23 06:03:28 -0700128 struct mpls_shim_hdr *hdr;
129 bool eli_seen = false;
130 int label_index;
Robert Shearman1c78efa2015-10-23 06:03:28 -0700131 u32 hash = 0;
132
David Ahern9f427a0e2017-01-20 12:58:34 -0800133 for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
Robert Shearman1c78efa2015-10-23 06:03:28 -0700134 label_index++) {
David Ahern9f427a0e2017-01-20 12:58:34 -0800135 mpls_hdr_len += sizeof(*hdr);
136 if (!pskb_may_pull(skb, mpls_hdr_len))
Robert Shearman1c78efa2015-10-23 06:03:28 -0700137 break;
138
139 /* Read and decode the current label */
140 hdr = mpls_hdr(skb) + label_index;
141 dec = mpls_entry_decode(hdr);
142
143 /* RFC6790 - reserved labels MUST NOT be used as keys
144 * for the load-balancing function
145 */
146 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
147 hash = jhash_1word(dec.label, hash);
148
149 /* The entropy label follows the entropy label
150 * indicator, so this means that the entropy
151 * label was just added to the hash - no need to
152 * go any deeper either in the label stack or in the
153 * payload
154 */
155 if (eli_seen)
156 break;
157 } else if (dec.label == MPLS_LABEL_ENTROPY) {
158 eli_seen = true;
159 }
160
David Ahern9f427a0e2017-01-20 12:58:34 -0800161 if (!dec.bos)
162 continue;
163
164 /* found bottom label; does skb have room for a header? */
165 if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
Robert Shearman1c78efa2015-10-23 06:03:28 -0700166 const struct iphdr *v4hdr;
167
David Ahern9f427a0e2017-01-20 12:58:34 -0800168 v4hdr = (const struct iphdr *)(hdr + 1);
Robert Shearman1c78efa2015-10-23 06:03:28 -0700169 if (v4hdr->version == 4) {
170 hash = jhash_3words(ntohl(v4hdr->saddr),
171 ntohl(v4hdr->daddr),
172 v4hdr->protocol, hash);
173 } else if (v4hdr->version == 6 &&
David Ahern9f427a0e2017-01-20 12:58:34 -0800174 pskb_may_pull(skb, mpls_hdr_len +
175 sizeof(struct ipv6hdr))) {
Robert Shearman1c78efa2015-10-23 06:03:28 -0700176 const struct ipv6hdr *v6hdr;
177
David Ahern9f427a0e2017-01-20 12:58:34 -0800178 v6hdr = (const struct ipv6hdr *)(hdr + 1);
Robert Shearman1c78efa2015-10-23 06:03:28 -0700179 hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
180 hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
181 hash = jhash_1word(v6hdr->nexthdr, hash);
182 }
183 }
David Ahern9f427a0e2017-01-20 12:58:34 -0800184
185 break;
Robert Shearman1c78efa2015-10-23 06:03:28 -0700186 }
187
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800188 return hash;
189}
190
David Ahern59b20962017-03-31 07:14:01 -0700191static struct mpls_nh *mpls_get_nexthop(struct mpls_route *rt, u8 index)
192{
193 return (struct mpls_nh *)((u8 *)rt->rt_nh + index * rt->rt_nh_size);
194}
195
David Ahern39eb8cd2017-03-31 07:13:59 -0700196/* number of alive nexthops (rt->rt_nhn_alive) and the flags for
197 * a next hop (nh->nh_flags) are modified by netdev event handlers.
198 * Since those fields can change at any moment, use READ_ONCE to
199 * access both.
200 */
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800201static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
David Ahern9f427a0e2017-01-20 12:58:34 -0800202 struct sk_buff *skb)
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800203{
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800204 u32 hash = 0;
205 int nh_index = 0;
206 int n = 0;
David Ahern77ef013a2017-03-31 07:14:00 -0700207 u8 alive;
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800208
209 /* No need to look further into packet if there's only
210 * one path
211 */
212 if (rt->rt_nhn == 1)
David Ahern59b20962017-03-31 07:14:01 -0700213 return rt->rt_nh;
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800214
David Ahern39eb8cd2017-03-31 07:13:59 -0700215 alive = READ_ONCE(rt->rt_nhn_alive);
216 if (alive == 0)
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800217 return NULL;
218
David Ahern9f427a0e2017-01-20 12:58:34 -0800219 hash = mpls_multipath_hash(rt, skb);
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800220 nh_index = hash % alive;
221 if (alive == rt->rt_nhn)
222 goto out;
223 for_nexthops(rt) {
David Ahern39eb8cd2017-03-31 07:13:59 -0700224 unsigned int nh_flags = READ_ONCE(nh->nh_flags);
225
226 if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800227 continue;
228 if (n == nh_index)
229 return nh;
230 n++;
231 } endfor_nexthops(rt);
232
Robert Shearman1c78efa2015-10-23 06:03:28 -0700233out:
David Ahern59b20962017-03-31 07:14:01 -0700234 return mpls_get_nexthop(rt, nh_index);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700235}
236
Robert Shearman5b441ac2017-03-10 20:43:24 +0000237static bool mpls_egress(struct net *net, struct mpls_route *rt,
238 struct sk_buff *skb, struct mpls_entry_decoded dec)
Eric W. Biederman01891972015-03-03 19:10:47 -0600239{
Robert Shearman118d5232015-08-06 11:04:56 +0100240 enum mpls_payload_type payload_type;
241 bool success = false;
Eric W. Biederman01891972015-03-03 19:10:47 -0600242
Eric W. Biederman76fecd82015-03-12 18:22:59 -0500243 /* The IPv4 code below accesses through the IPv4 header
244 * checksum, which is 12 bytes into the packet.
245 * The IPv6 code below accesses through the IPv6 hop limit
246 * which is 8 bytes into the packet.
247 *
248 * For all supported cases there should always be at least 12
249 * bytes of packet data present. The IPv4 header is 20 bytes
250 * without options and the IPv6 header is always 40 bytes
251 * long.
252 */
253 if (!pskb_may_pull(skb, 12))
254 return false;
255
Robert Shearman118d5232015-08-06 11:04:56 +0100256 payload_type = rt->rt_payload_type;
257 if (payload_type == MPT_UNSPEC)
258 payload_type = ip_hdr(skb)->version;
259
260 switch (payload_type) {
261 case MPT_IPV4: {
262 struct iphdr *hdr4 = ip_hdr(skb);
Robert Shearman5b441ac2017-03-10 20:43:24 +0000263 u8 new_ttl;
Eric W. Biederman01891972015-03-03 19:10:47 -0600264 skb->protocol = htons(ETH_P_IP);
Robert Shearman5b441ac2017-03-10 20:43:24 +0000265
266 /* If propagating TTL, take the decremented TTL from
267 * the incoming MPLS header, otherwise decrement the
268 * TTL, but only if not 0 to avoid underflow.
269 */
270 if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
271 (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
272 net->mpls.ip_ttl_propagate))
273 new_ttl = dec.ttl;
274 else
275 new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
276
Eric W. Biederman01891972015-03-03 19:10:47 -0600277 csum_replace2(&hdr4->check,
278 htons(hdr4->ttl << 8),
Robert Shearman5b441ac2017-03-10 20:43:24 +0000279 htons(new_ttl << 8));
280 hdr4->ttl = new_ttl;
Robert Shearman118d5232015-08-06 11:04:56 +0100281 success = true;
282 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600283 }
Robert Shearman118d5232015-08-06 11:04:56 +0100284 case MPT_IPV6: {
Eric W. Biederman01891972015-03-03 19:10:47 -0600285 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
286 skb->protocol = htons(ETH_P_IPV6);
Robert Shearman5b441ac2017-03-10 20:43:24 +0000287
288 /* If propagating TTL, take the decremented TTL from
289 * the incoming MPLS header, otherwise decrement the
290 * hop limit, but only if not 0 to avoid underflow.
291 */
292 if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
293 (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
294 net->mpls.ip_ttl_propagate))
295 hdr6->hop_limit = dec.ttl;
296 else if (hdr6->hop_limit)
297 hdr6->hop_limit = hdr6->hop_limit - 1;
Robert Shearman118d5232015-08-06 11:04:56 +0100298 success = true;
299 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600300 }
Robert Shearman118d5232015-08-06 11:04:56 +0100301 case MPT_UNSPEC:
Robert Shearman5b441ac2017-03-10 20:43:24 +0000302 /* Should have decided which protocol it is by now */
Robert Shearman118d5232015-08-06 11:04:56 +0100303 break;
304 }
305
Eric W. Biederman01891972015-03-03 19:10:47 -0600306 return success;
307}
308
309static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
310 struct packet_type *pt, struct net_device *orig_dev)
311{
312 struct net *net = dev_net(dev);
313 struct mpls_shim_hdr *hdr;
314 struct mpls_route *rt;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700315 struct mpls_nh *nh;
Eric W. Biederman01891972015-03-03 19:10:47 -0600316 struct mpls_entry_decoded dec;
317 struct net_device *out_dev;
Robert Shearman27d69102017-01-16 14:16:37 +0000318 struct mpls_dev *out_mdev;
Robert Shearman03c57742015-04-22 11:14:37 +0100319 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600320 unsigned int hh_len;
321 unsigned int new_header_size;
322 unsigned int mtu;
323 int err;
324
325 /* Careful this entire function runs inside of an rcu critical section */
326
Robert Shearman03c57742015-04-22 11:14:37 +0100327 mdev = mpls_dev_get(dev);
Robert Shearman27d69102017-01-16 14:16:37 +0000328 if (!mdev)
Robert Shearman03c57742015-04-22 11:14:37 +0100329 goto drop;
330
Robert Shearman27d69102017-01-16 14:16:37 +0000331 MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
332 rx_bytes);
333
334 if (!mdev->input_enabled) {
335 MPLS_INC_STATS(mdev, rx_dropped);
336 goto drop;
337 }
338
Eric W. Biederman01891972015-03-03 19:10:47 -0600339 if (skb->pkt_type != PACKET_HOST)
Robert Shearman27d69102017-01-16 14:16:37 +0000340 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600341
342 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
Robert Shearman27d69102017-01-16 14:16:37 +0000343 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600344
345 if (!pskb_may_pull(skb, sizeof(*hdr)))
Robert Shearman27d69102017-01-16 14:16:37 +0000346 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600347
348 /* Read and decode the label */
349 hdr = mpls_hdr(skb);
350 dec = mpls_entry_decode(hdr);
351
Eric W. Biederman01891972015-03-03 19:10:47 -0600352 rt = mpls_route_input_rcu(net, dec.label);
Robert Shearman27d69102017-01-16 14:16:37 +0000353 if (!rt) {
354 MPLS_INC_STATS(mdev, rx_noroute);
Eric W. Biederman01891972015-03-03 19:10:47 -0600355 goto drop;
Robert Shearman27d69102017-01-16 14:16:37 +0000356 }
Eric W. Biederman01891972015-03-03 19:10:47 -0600357
David Ahern9f427a0e2017-01-20 12:58:34 -0800358 nh = mpls_select_multipath(rt, skb);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700359 if (!nh)
Robert Shearman27d69102017-01-16 14:16:37 +0000360 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600361
David Ahern9f427a0e2017-01-20 12:58:34 -0800362 /* Pop the label */
363 skb_pull(skb, sizeof(*hdr));
364 skb_reset_network_header(skb);
365
366 skb_orphan(skb);
367
Eric W. Biederman01891972015-03-03 19:10:47 -0600368 if (skb_warn_if_lro(skb))
Robert Shearman27d69102017-01-16 14:16:37 +0000369 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600370
371 skb_forward_csum(skb);
372
373 /* Verify ttl is valid */
Eric W. Biedermanaa7da932015-03-07 16:23:23 -0600374 if (dec.ttl <= 1)
Robert Shearman27d69102017-01-16 14:16:37 +0000375 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600376 dec.ttl -= 1;
377
Robert Shearman27d69102017-01-16 14:16:37 +0000378 /* Find the output device */
379 out_dev = rcu_dereference(nh->nh_dev);
380 if (!mpls_output_possible(out_dev))
381 goto tx_err;
382
Eric W. Biederman01891972015-03-03 19:10:47 -0600383 /* Verify the destination can hold the packet */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700384 new_header_size = mpls_nh_header_size(nh);
Eric W. Biederman01891972015-03-03 19:10:47 -0600385 mtu = mpls_dev_mtu(out_dev);
386 if (mpls_pkt_too_big(skb, mtu - new_header_size))
Robert Shearman27d69102017-01-16 14:16:37 +0000387 goto tx_err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600388
389 hh_len = LL_RESERVED_SPACE(out_dev);
390 if (!out_dev->header_ops)
391 hh_len = 0;
392
393 /* Ensure there is enough space for the headers in the skb */
394 if (skb_cow(skb, hh_len + new_header_size))
Robert Shearman27d69102017-01-16 14:16:37 +0000395 goto tx_err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600396
397 skb->dev = out_dev;
398 skb->protocol = htons(ETH_P_MPLS_UC);
399
400 if (unlikely(!new_header_size && dec.bos)) {
401 /* Penultimate hop popping */
Robert Shearman5b441ac2017-03-10 20:43:24 +0000402 if (!mpls_egress(dev_net(out_dev), rt, skb, dec))
Robert Shearman27d69102017-01-16 14:16:37 +0000403 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600404 } else {
405 bool bos;
406 int i;
407 skb_push(skb, new_header_size);
408 skb_reset_network_header(skb);
409 /* Push the new labels */
410 hdr = mpls_hdr(skb);
411 bos = dec.bos;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700412 for (i = nh->nh_labels - 1; i >= 0; i--) {
413 hdr[i] = mpls_entry_encode(nh->nh_label[i],
414 dec.ttl, 0, bos);
Eric W. Biederman01891972015-03-03 19:10:47 -0600415 bos = false;
416 }
417 }
418
Robert Shearman27d69102017-01-16 14:16:37 +0000419 mpls_stats_inc_outucastpkts(out_dev, skb);
420
Robert Shearmaneb7809f2015-12-10 19:30:50 +0000421 /* If via wasn't specified then send out using device address */
422 if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
423 err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
424 out_dev->dev_addr, skb);
425 else
426 err = neigh_xmit(nh->nh_via_table, out_dev,
427 mpls_nh_via(rt, nh), skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600428 if (err)
429 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
430 __func__, err);
431 return 0;
432
Robert Shearman27d69102017-01-16 14:16:37 +0000433tx_err:
434 out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
435 if (out_mdev)
436 MPLS_INC_STATS(out_mdev, tx_errors);
437 goto drop;
438err:
439 MPLS_INC_STATS(mdev, rx_errors);
Eric W. Biederman01891972015-03-03 19:10:47 -0600440drop:
441 kfree_skb(skb);
442 return NET_RX_DROP;
443}
444
445static struct packet_type mpls_packet_type __read_mostly = {
446 .type = cpu_to_be16(ETH_P_MPLS_UC),
447 .func = mpls_forward,
448};
449
Wu Fengguangf0126532015-03-05 05:33:54 +0800450static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600451 [RTA_DST] = { .type = NLA_U32 },
452 [RTA_OIF] = { .type = NLA_U32 },
Robert Shearman5b441ac2017-03-10 20:43:24 +0000453 [RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
Eric W. Biederman03c05662015-03-03 19:13:56 -0600454};
455
Eric W. Biedermana2519922015-03-03 19:12:40 -0600456struct mpls_route_config {
Robert Shearman118d5232015-08-06 11:04:56 +0100457 u32 rc_protocol;
458 u32 rc_ifindex;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700459 u8 rc_via_table;
460 u8 rc_via_alen;
Robert Shearman118d5232015-08-06 11:04:56 +0100461 u8 rc_via[MAX_VIA_ALEN];
462 u32 rc_label;
Robert Shearman5b441ac2017-03-10 20:43:24 +0000463 u8 rc_ttl_propagate;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700464 u8 rc_output_labels;
Robert Shearman118d5232015-08-06 11:04:56 +0100465 u32 rc_output_label[MAX_NEW_LABELS];
466 u32 rc_nlflags;
467 enum mpls_payload_type rc_payload_type;
468 struct nl_info rc_nlinfo;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700469 struct rtnexthop *rc_mp;
470 int rc_mp_len;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600471};
472
David Ahern59b20962017-03-31 07:14:01 -0700473/* all nexthops within a route have the same size based on max
474 * number of labels and max via length for a hop
475 */
476static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels)
Eric W. Biederman01891972015-03-03 19:10:47 -0600477{
David Ahern59b20962017-03-31 07:14:01 -0700478 u8 nh_size = MPLS_NH_SIZE(max_labels, max_alen);
Eric W. Biederman01891972015-03-03 19:10:47 -0600479 struct mpls_route *rt;
480
David Ahern59b20962017-03-31 07:14:01 -0700481 rt = kzalloc(sizeof(*rt) + num_nh * nh_size, GFP_KERNEL);
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000482 if (rt) {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700483 rt->rt_nhn = num_nh;
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800484 rt->rt_nhn_alive = num_nh;
David Ahern59b20962017-03-31 07:14:01 -0700485 rt->rt_nh_size = nh_size;
486 rt->rt_via_offset = MPLS_NH_VIA_OFF(max_labels);
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000487 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700488
Eric W. Biederman01891972015-03-03 19:10:47 -0600489 return rt;
490}
491
492static void mpls_rt_free(struct mpls_route *rt)
493{
494 if (rt)
495 kfree_rcu(rt, rt_rcu);
496}
497
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600498static void mpls_notify_route(struct net *net, unsigned index,
499 struct mpls_route *old, struct mpls_route *new,
500 const struct nl_info *info)
501{
502 struct nlmsghdr *nlh = info ? info->nlh : NULL;
503 unsigned portid = info ? info->portid : 0;
504 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
505 struct mpls_route *rt = new ? new : old;
506 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
507 /* Ignore reserved labels for now */
Robert Shearmana6affd22015-08-03 17:50:04 +0100508 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600509 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
510}
511
Eric W. Biederman01891972015-03-03 19:10:47 -0600512static void mpls_route_update(struct net *net, unsigned index,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700513 struct mpls_route *new,
Eric W. Biederman01891972015-03-03 19:10:47 -0600514 const struct nl_info *info)
515{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600516 struct mpls_route __rcu **platform_label;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700517 struct mpls_route *rt;
Eric W. Biederman01891972015-03-03 19:10:47 -0600518
519 ASSERT_RTNL();
520
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600521 platform_label = rtnl_dereference(net->mpls.platform_label);
522 rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700523 rcu_assign_pointer(platform_label[index], new);
Eric W. Biederman01891972015-03-03 19:10:47 -0600524
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700525 mpls_notify_route(net, index, rt, new, info);
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600526
Eric W. Biederman01891972015-03-03 19:10:47 -0600527 /* If we removed a route free it now */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700528 mpls_rt_free(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -0600529}
530
Eric W. Biedermana2519922015-03-03 19:12:40 -0600531static unsigned find_free_label(struct net *net)
532{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600533 struct mpls_route __rcu **platform_label;
534 size_t platform_labels;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600535 unsigned index;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600536
537 platform_label = rtnl_dereference(net->mpls.platform_label);
538 platform_labels = net->mpls.platform_labels;
Robert Shearmana6affd22015-08-03 17:50:04 +0100539 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
540 index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600541 if (!rtnl_dereference(platform_label[index]))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600542 return index;
543 }
544 return LABEL_NOT_SPECIFIED;
545}
546
Roopa Prabhubf215632015-07-30 13:34:54 -0700547#if IS_ENABLED(CONFIG_INET)
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000548static struct net_device *inet_fib_lookup_dev(struct net *net,
549 const void *addr)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700550{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300551 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700552 struct rtable *rt;
553 struct in_addr daddr;
554
555 memcpy(&daddr, addr, sizeof(struct in_addr));
556 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
557 if (IS_ERR(rt))
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300558 return ERR_CAST(rt);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700559
560 dev = rt->dst.dev;
561 dev_hold(dev);
562
563 ip_rt_put(rt);
564
Roopa Prabhu01faef22015-07-21 09:16:24 -0700565 return dev;
566}
Roopa Prabhubf215632015-07-30 13:34:54 -0700567#else
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000568static struct net_device *inet_fib_lookup_dev(struct net *net,
569 const void *addr)
Roopa Prabhubf215632015-07-30 13:34:54 -0700570{
571 return ERR_PTR(-EAFNOSUPPORT);
572}
573#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700574
Roopa Prabhubf215632015-07-30 13:34:54 -0700575#if IS_ENABLED(CONFIG_IPV6)
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000576static struct net_device *inet6_fib_lookup_dev(struct net *net,
577 const void *addr)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700578{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300579 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700580 struct dst_entry *dst;
581 struct flowi6 fl6;
Roopa Prabhubf215632015-07-30 13:34:54 -0700582 int err;
583
584 if (!ipv6_stub)
585 return ERR_PTR(-EAFNOSUPPORT);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700586
587 memset(&fl6, 0, sizeof(fl6));
588 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
Roopa Prabhubf215632015-07-30 13:34:54 -0700589 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
590 if (err)
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300591 return ERR_PTR(err);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700592
593 dev = dst->dev;
594 dev_hold(dev);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700595 dst_release(dst);
596
597 return dev;
598}
Roopa Prabhubf215632015-07-30 13:34:54 -0700599#else
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000600static struct net_device *inet6_fib_lookup_dev(struct net *net,
601 const void *addr)
Roopa Prabhubf215632015-07-30 13:34:54 -0700602{
603 return ERR_PTR(-EAFNOSUPPORT);
604}
605#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700606
607static struct net_device *find_outdev(struct net *net,
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000608 struct mpls_route *rt,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700609 struct mpls_nh *nh, int oif)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700610{
611 struct net_device *dev = NULL;
612
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700613 if (!oif) {
614 switch (nh->nh_via_table) {
Roopa Prabhu01faef22015-07-21 09:16:24 -0700615 case NEIGH_ARP_TABLE:
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000616 dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
Roopa Prabhu01faef22015-07-21 09:16:24 -0700617 break;
618 case NEIGH_ND_TABLE:
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000619 dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
Roopa Prabhu01faef22015-07-21 09:16:24 -0700620 break;
621 case NEIGH_LINK_TABLE:
622 break;
623 }
624 } else {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700625 dev = dev_get_by_index(net, oif);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700626 }
627
Roopa Prabhu3dcb6152015-08-04 06:36:24 -0700628 if (!dev)
629 return ERR_PTR(-ENODEV);
630
Roopa Prabhu94a57f12016-04-07 21:28:38 -0700631 if (IS_ERR(dev))
632 return dev;
633
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700634 /* The caller is holding rtnl anyways, so release the dev reference */
635 dev_put(dev);
636
Roopa Prabhu01faef22015-07-21 09:16:24 -0700637 return dev;
638}
639
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000640static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
641 struct mpls_nh *nh, int oif)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700642{
643 struct net_device *dev = NULL;
644 int err = -ENODEV;
645
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000646 dev = find_outdev(net, rt, nh, oif);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700647 if (IS_ERR(dev)) {
648 err = PTR_ERR(dev);
649 dev = NULL;
650 goto errout;
651 }
652
653 /* Ensure this is a supported device */
654 err = -EINVAL;
655 if (!mpls_dev_get(dev))
656 goto errout;
657
Robert Shearmana3e948e2015-12-10 19:30:48 +0000658 if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
659 (dev->addr_len != nh->nh_via_alen))
660 goto errout;
661
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700662 RCU_INIT_POINTER(nh->nh_dev, dev);
663
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800664 if (!(dev->flags & IFF_UP)) {
665 nh->nh_flags |= RTNH_F_DEAD;
666 } else {
667 unsigned int flags;
668
669 flags = dev_get_flags(dev);
670 if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
671 nh->nh_flags |= RTNH_F_LINKDOWN;
672 }
673
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700674 return 0;
675
676errout:
677 return err;
678}
679
680static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
681 struct mpls_route *rt)
682{
683 struct net *net = cfg->rc_nlinfo.nl_net;
684 struct mpls_nh *nh = rt->rt_nh;
685 int err;
686 int i;
687
688 if (!nh)
689 return -ENOMEM;
690
691 err = -EINVAL;
692 /* Ensure only a supported number of labels are present */
693 if (cfg->rc_output_labels > MAX_NEW_LABELS)
694 goto errout;
695
696 nh->nh_labels = cfg->rc_output_labels;
697 for (i = 0; i < nh->nh_labels; i++)
698 nh->nh_label[i] = cfg->rc_output_label[i];
699
700 nh->nh_via_table = cfg->rc_via_table;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000701 memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700702 nh->nh_via_alen = cfg->rc_via_alen;
703
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000704 err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700705 if (err)
706 goto errout;
707
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800708 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
709 rt->rt_nhn_alive--;
710
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700711 return 0;
712
713errout:
714 return err;
715}
716
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000717static int mpls_nh_build(struct net *net, struct mpls_route *rt,
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800718 struct mpls_nh *nh, int oif, struct nlattr *via,
719 struct nlattr *newdst)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700720{
721 int err = -ENOMEM;
722
723 if (!nh)
724 goto errout;
725
726 if (newdst) {
727 err = nla_get_labels(newdst, MAX_NEW_LABELS,
728 &nh->nh_labels, nh->nh_label);
729 if (err)
730 goto errout;
731 }
732
Robert Shearmanf20367d2015-12-10 19:30:51 +0000733 if (via) {
734 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
735 __mpls_nh_via(rt, nh));
736 if (err)
737 goto errout;
738 } else {
739 nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
740 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700741
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000742 err = mpls_nh_assign_dev(net, rt, nh, oif);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700743 if (err)
744 goto errout;
745
746 return 0;
747
748errout:
749 return err;
750}
751
David Ahern77ef013a2017-03-31 07:14:00 -0700752static u8 mpls_count_nexthops(struct rtnexthop *rtnh, int len,
753 u8 cfg_via_alen, u8 *max_via_alen)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700754{
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700755 int remaining = len;
David Ahern77ef013a2017-03-31 07:14:00 -0700756 u8 nhs = 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700757
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000758 if (!rtnh) {
759 *max_via_alen = cfg_via_alen;
760 return 1;
761 }
762
763 *max_via_alen = 0;
764
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700765 while (rtnh_ok(rtnh, remaining)) {
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000766 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
767 int attrlen;
768
769 attrlen = rtnh_attrlen(rtnh);
770 nla = nla_find(attrs, attrlen, RTA_VIA);
771 if (nla && nla_len(nla) >=
772 offsetof(struct rtvia, rtvia_addr)) {
773 int via_alen = nla_len(nla) -
774 offsetof(struct rtvia, rtvia_addr);
775
776 if (via_alen <= MAX_VIA_ALEN)
777 *max_via_alen = max_t(u16, *max_via_alen,
778 via_alen);
779 }
780
David Ahern77ef013a2017-03-31 07:14:00 -0700781 /* number of nexthops is tracked by a u8.
782 * Check for overflow.
783 */
784 if (nhs == 255)
785 return 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700786 nhs++;
David Ahern77ef013a2017-03-31 07:14:00 -0700787
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700788 rtnh = rtnh_next(rtnh, &remaining);
789 }
790
791 /* leftover implies invalid nexthop configuration, discard it */
792 return remaining > 0 ? 0 : nhs;
793}
794
795static int mpls_nh_build_multi(struct mpls_route_config *cfg,
796 struct mpls_route *rt)
797{
798 struct rtnexthop *rtnh = cfg->rc_mp;
799 struct nlattr *nla_via, *nla_newdst;
800 int remaining = cfg->rc_mp_len;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700801 int err = 0;
David Ahern77ef013a2017-03-31 07:14:00 -0700802 u8 nhs = 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700803
804 change_nexthops(rt) {
805 int attrlen;
806
807 nla_via = NULL;
808 nla_newdst = NULL;
809
810 err = -EINVAL;
811 if (!rtnh_ok(rtnh, remaining))
812 goto errout;
813
Robert Shearman1c78efa2015-10-23 06:03:28 -0700814 /* neither weighted multipath nor any flags
815 * are supported
816 */
817 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
818 goto errout;
819
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700820 attrlen = rtnh_attrlen(rtnh);
821 if (attrlen > 0) {
822 struct nlattr *attrs = rtnh_attrs(rtnh);
823
824 nla_via = nla_find(attrs, attrlen, RTA_VIA);
825 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
826 }
827
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000828 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800829 rtnh->rtnh_ifindex, nla_via, nla_newdst);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700830 if (err)
831 goto errout;
832
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800833 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
834 rt->rt_nhn_alive--;
835
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700836 rtnh = rtnh_next(rtnh, &remaining);
837 nhs++;
838 } endfor_nexthops(rt);
839
840 rt->rt_nhn = nhs;
841
842 return 0;
843
844errout:
845 return err;
846}
847
Eric W. Biedermana2519922015-03-03 19:12:40 -0600848static int mpls_route_add(struct mpls_route_config *cfg)
849{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600850 struct mpls_route __rcu **platform_label;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600851 struct net *net = cfg->rc_nlinfo.nl_net;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600852 struct mpls_route *rt, *old;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600853 int err = -EINVAL;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000854 u8 max_via_alen;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700855 unsigned index;
David Ahern77ef013a2017-03-31 07:14:00 -0700856 u8 nhs;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600857
858 index = cfg->rc_label;
859
860 /* If a label was not specified during insert pick one */
861 if ((index == LABEL_NOT_SPECIFIED) &&
862 (cfg->rc_nlflags & NLM_F_CREATE)) {
863 index = find_free_label(net);
864 }
865
Robert Shearmana6affd22015-08-03 17:50:04 +0100866 /* Reserved labels may not be set */
867 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600868 goto errout;
869
870 /* The full 20 bit range may not be supported. */
871 if (index >= net->mpls.platform_labels)
872 goto errout;
873
Eric W. Biedermana2519922015-03-03 19:12:40 -0600874 /* Append makes no sense with mpls */
Eric W. Biederman0f7bbd52015-03-07 16:22:40 -0600875 err = -EOPNOTSUPP;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600876 if (cfg->rc_nlflags & NLM_F_APPEND)
877 goto errout;
878
879 err = -EEXIST;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600880 platform_label = rtnl_dereference(net->mpls.platform_label);
881 old = rtnl_dereference(platform_label[index]);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600882 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
883 goto errout;
884
885 err = -EEXIST;
886 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
887 goto errout;
888
889 err = -ENOENT;
890 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
891 goto errout;
892
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000893 err = -EINVAL;
894 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
895 cfg->rc_via_alen, &max_via_alen);
896 if (nhs == 0)
897 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700898
Eric W. Biedermana2519922015-03-03 19:12:40 -0600899 err = -ENOMEM;
David Ahern59b20962017-03-31 07:14:01 -0700900 rt = mpls_rt_alloc(nhs, max_via_alen, MAX_NEW_LABELS);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600901 if (!rt)
902 goto errout;
903
Eric W. Biedermana2519922015-03-03 19:12:40 -0600904 rt->rt_protocol = cfg->rc_protocol;
Robert Shearman118d5232015-08-06 11:04:56 +0100905 rt->rt_payload_type = cfg->rc_payload_type;
Robert Shearman5b441ac2017-03-10 20:43:24 +0000906 rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600907
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700908 if (cfg->rc_mp)
909 err = mpls_nh_build_multi(cfg, rt);
910 else
911 err = mpls_nh_build_from_cfg(cfg, rt);
912 if (err)
913 goto freert;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600914
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700915 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
916
Eric W. Biedermana2519922015-03-03 19:12:40 -0600917 return 0;
918
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700919freert:
920 mpls_rt_free(rt);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600921errout:
Eric W. Biedermana2519922015-03-03 19:12:40 -0600922 return err;
923}
924
925static int mpls_route_del(struct mpls_route_config *cfg)
926{
927 struct net *net = cfg->rc_nlinfo.nl_net;
928 unsigned index;
929 int err = -EINVAL;
930
931 index = cfg->rc_label;
932
Robert Shearmana6affd22015-08-03 17:50:04 +0100933 /* Reserved labels may not be removed */
934 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600935 goto errout;
936
937 /* The full 20 bit range may not be supported */
938 if (index >= net->mpls.platform_labels)
939 goto errout;
940
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700941 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600942
943 err = 0;
944errout:
945 return err;
946}
947
Robert Shearman27d69102017-01-16 14:16:37 +0000948static void mpls_get_stats(struct mpls_dev *mdev,
949 struct mpls_link_stats *stats)
950{
951 struct mpls_pcpu_stats *p;
952 int i;
953
954 memset(stats, 0, sizeof(*stats));
955
956 for_each_possible_cpu(i) {
957 struct mpls_link_stats local;
958 unsigned int start;
959
960 p = per_cpu_ptr(mdev->stats, i);
961 do {
962 start = u64_stats_fetch_begin(&p->syncp);
963 local = p->stats;
964 } while (u64_stats_fetch_retry(&p->syncp, start));
965
966 stats->rx_packets += local.rx_packets;
967 stats->rx_bytes += local.rx_bytes;
968 stats->tx_packets += local.tx_packets;
969 stats->tx_bytes += local.tx_bytes;
970 stats->rx_errors += local.rx_errors;
971 stats->tx_errors += local.tx_errors;
972 stats->rx_dropped += local.rx_dropped;
973 stats->tx_dropped += local.tx_dropped;
974 stats->rx_noroute += local.rx_noroute;
975 }
976}
977
978static int mpls_fill_stats_af(struct sk_buff *skb,
979 const struct net_device *dev)
980{
981 struct mpls_link_stats *stats;
982 struct mpls_dev *mdev;
983 struct nlattr *nla;
984
985 mdev = mpls_dev_get(dev);
986 if (!mdev)
987 return -ENODATA;
988
989 nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
990 sizeof(struct mpls_link_stats),
991 MPLS_STATS_UNSPEC);
992 if (!nla)
993 return -EMSGSIZE;
994
995 stats = nla_data(nla);
996 mpls_get_stats(mdev, stats);
997
998 return 0;
999}
1000
1001static size_t mpls_get_stats_af_size(const struct net_device *dev)
1002{
1003 struct mpls_dev *mdev;
1004
1005 mdev = mpls_dev_get(dev);
1006 if (!mdev)
1007 return 0;
1008
1009 return nla_total_size_64bit(sizeof(struct mpls_link_stats));
1010}
1011
David Ahern24045a02017-02-20 08:03:30 -08001012static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
1013 u32 portid, u32 seq, int event,
1014 unsigned int flags, int type)
1015{
1016 struct nlmsghdr *nlh;
1017 struct netconfmsg *ncm;
1018 bool all = false;
1019
1020 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1021 flags);
1022 if (!nlh)
1023 return -EMSGSIZE;
1024
1025 if (type == NETCONFA_ALL)
1026 all = true;
1027
1028 ncm = nlmsg_data(nlh);
1029 ncm->ncm_family = AF_MPLS;
1030
1031 if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
1032 goto nla_put_failure;
1033
1034 if ((all || type == NETCONFA_INPUT) &&
1035 nla_put_s32(skb, NETCONFA_INPUT,
1036 mdev->input_enabled) < 0)
1037 goto nla_put_failure;
1038
1039 nlmsg_end(skb, nlh);
1040 return 0;
1041
1042nla_put_failure:
1043 nlmsg_cancel(skb, nlh);
1044 return -EMSGSIZE;
1045}
1046
1047static int mpls_netconf_msgsize_devconf(int type)
1048{
1049 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1050 + nla_total_size(4); /* NETCONFA_IFINDEX */
1051 bool all = false;
1052
1053 if (type == NETCONFA_ALL)
1054 all = true;
1055
1056 if (all || type == NETCONFA_INPUT)
1057 size += nla_total_size(4);
1058
1059 return size;
1060}
1061
David Ahern823566a2017-03-28 14:28:06 -07001062static void mpls_netconf_notify_devconf(struct net *net, int event,
1063 int type, struct mpls_dev *mdev)
David Ahern24045a02017-02-20 08:03:30 -08001064{
1065 struct sk_buff *skb;
1066 int err = -ENOBUFS;
1067
1068 skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
1069 if (!skb)
1070 goto errout;
1071
David Ahern823566a2017-03-28 14:28:06 -07001072 err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type);
David Ahern24045a02017-02-20 08:03:30 -08001073 if (err < 0) {
1074 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1075 WARN_ON(err == -EMSGSIZE);
1076 kfree_skb(skb);
1077 goto errout;
1078 }
1079
1080 rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
1081 return;
1082errout:
1083 if (err < 0)
1084 rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
1085}
1086
1087static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
1088 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
1089};
1090
1091static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
1092 struct nlmsghdr *nlh)
1093{
1094 struct net *net = sock_net(in_skb->sk);
1095 struct nlattr *tb[NETCONFA_MAX + 1];
1096 struct netconfmsg *ncm;
1097 struct net_device *dev;
1098 struct mpls_dev *mdev;
1099 struct sk_buff *skb;
1100 int ifindex;
1101 int err;
1102
1103 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
1104 devconf_mpls_policy);
1105 if (err < 0)
1106 goto errout;
1107
1108 err = -EINVAL;
1109 if (!tb[NETCONFA_IFINDEX])
1110 goto errout;
1111
1112 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
1113 dev = __dev_get_by_index(net, ifindex);
1114 if (!dev)
1115 goto errout;
1116
1117 mdev = mpls_dev_get(dev);
1118 if (!mdev)
1119 goto errout;
1120
1121 err = -ENOBUFS;
1122 skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
1123 if (!skb)
1124 goto errout;
1125
1126 err = mpls_netconf_fill_devconf(skb, mdev,
1127 NETLINK_CB(in_skb).portid,
1128 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
1129 NETCONFA_ALL);
1130 if (err < 0) {
1131 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1132 WARN_ON(err == -EMSGSIZE);
1133 kfree_skb(skb);
1134 goto errout;
1135 }
1136 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1137errout:
1138 return err;
1139}
1140
1141static int mpls_netconf_dump_devconf(struct sk_buff *skb,
1142 struct netlink_callback *cb)
1143{
1144 struct net *net = sock_net(skb->sk);
1145 struct hlist_head *head;
1146 struct net_device *dev;
1147 struct mpls_dev *mdev;
1148 int idx, s_idx;
1149 int h, s_h;
1150
1151 s_h = cb->args[0];
1152 s_idx = idx = cb->args[1];
1153
1154 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1155 idx = 0;
1156 head = &net->dev_index_head[h];
1157 rcu_read_lock();
1158 cb->seq = net->dev_base_seq;
1159 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1160 if (idx < s_idx)
1161 goto cont;
1162 mdev = mpls_dev_get(dev);
1163 if (!mdev)
1164 goto cont;
1165 if (mpls_netconf_fill_devconf(skb, mdev,
1166 NETLINK_CB(cb->skb).portid,
1167 cb->nlh->nlmsg_seq,
1168 RTM_NEWNETCONF,
1169 NLM_F_MULTI,
1170 NETCONFA_ALL) < 0) {
1171 rcu_read_unlock();
1172 goto done;
1173 }
1174 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1175cont:
1176 idx++;
1177 }
1178 rcu_read_unlock();
1179 }
1180done:
1181 cb->args[0] = h;
1182 cb->args[1] = idx;
1183
1184 return skb->len;
1185}
1186
Robert Shearman37bde792015-04-22 11:14:38 +01001187#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
1188 (&((struct mpls_dev *)0)->field)
1189
David Ahern24045a02017-02-20 08:03:30 -08001190static int mpls_conf_proc(struct ctl_table *ctl, int write,
1191 void __user *buffer,
1192 size_t *lenp, loff_t *ppos)
1193{
1194 int oval = *(int *)ctl->data;
1195 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1196
1197 if (write) {
1198 struct mpls_dev *mdev = ctl->extra1;
1199 int i = (int *)ctl->data - (int *)mdev;
1200 struct net *net = ctl->extra2;
1201 int val = *(int *)ctl->data;
1202
1203 if (i == offsetof(struct mpls_dev, input_enabled) &&
1204 val != oval) {
David Ahern823566a2017-03-28 14:28:06 -07001205 mpls_netconf_notify_devconf(net, RTM_NEWNETCONF,
1206 NETCONFA_INPUT, mdev);
David Ahern24045a02017-02-20 08:03:30 -08001207 }
1208 }
1209
1210 return ret;
1211}
1212
Robert Shearman37bde792015-04-22 11:14:38 +01001213static const struct ctl_table mpls_dev_table[] = {
1214 {
1215 .procname = "input",
1216 .maxlen = sizeof(int),
1217 .mode = 0644,
David Ahern24045a02017-02-20 08:03:30 -08001218 .proc_handler = mpls_conf_proc,
Robert Shearman37bde792015-04-22 11:14:38 +01001219 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
1220 },
1221 { }
1222};
1223
1224static int mpls_dev_sysctl_register(struct net_device *dev,
1225 struct mpls_dev *mdev)
1226{
1227 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
David Ahern24045a02017-02-20 08:03:30 -08001228 struct net *net = dev_net(dev);
Robert Shearman37bde792015-04-22 11:14:38 +01001229 struct ctl_table *table;
1230 int i;
1231
1232 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
1233 if (!table)
1234 goto out;
1235
1236 /* Table data contains only offsets relative to the base of
1237 * the mdev at this point, so make them absolute.
1238 */
David Ahern24045a02017-02-20 08:03:30 -08001239 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) {
Robert Shearman37bde792015-04-22 11:14:38 +01001240 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
David Ahern24045a02017-02-20 08:03:30 -08001241 table[i].extra1 = mdev;
1242 table[i].extra2 = net;
1243 }
Robert Shearman37bde792015-04-22 11:14:38 +01001244
1245 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
1246
David Ahern1182e4d2017-03-28 14:28:07 -07001247 mdev->sysctl = register_net_sysctl(net, path, table);
Robert Shearman37bde792015-04-22 11:14:38 +01001248 if (!mdev->sysctl)
1249 goto free;
1250
David Ahern1182e4d2017-03-28 14:28:07 -07001251 mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev);
Robert Shearman37bde792015-04-22 11:14:38 +01001252 return 0;
1253
1254free:
1255 kfree(table);
1256out:
1257 return -ENOBUFS;
1258}
1259
David Ahern1182e4d2017-03-28 14:28:07 -07001260static void mpls_dev_sysctl_unregister(struct net_device *dev,
1261 struct mpls_dev *mdev)
Robert Shearman37bde792015-04-22 11:14:38 +01001262{
David Ahern1182e4d2017-03-28 14:28:07 -07001263 struct net *net = dev_net(dev);
Robert Shearman37bde792015-04-22 11:14:38 +01001264 struct ctl_table *table;
1265
1266 table = mdev->sysctl->ctl_table_arg;
1267 unregister_net_sysctl_table(mdev->sysctl);
1268 kfree(table);
David Ahern1182e4d2017-03-28 14:28:07 -07001269
1270 mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
Robert Shearman37bde792015-04-22 11:14:38 +01001271}
1272
Robert Shearman03c57742015-04-22 11:14:37 +01001273static struct mpls_dev *mpls_add_dev(struct net_device *dev)
1274{
1275 struct mpls_dev *mdev;
1276 int err = -ENOMEM;
Robert Shearman27d69102017-01-16 14:16:37 +00001277 int i;
Robert Shearman03c57742015-04-22 11:14:37 +01001278
1279 ASSERT_RTNL();
1280
1281 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1282 if (!mdev)
1283 return ERR_PTR(err);
1284
Robert Shearman27d69102017-01-16 14:16:37 +00001285 mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
1286 if (!mdev->stats)
1287 goto free;
1288
1289 for_each_possible_cpu(i) {
1290 struct mpls_pcpu_stats *mpls_stats;
1291
1292 mpls_stats = per_cpu_ptr(mdev->stats, i);
1293 u64_stats_init(&mpls_stats->syncp);
1294 }
1295
David Ahern1182e4d2017-03-28 14:28:07 -07001296 mdev->dev = dev;
1297
Robert Shearman37bde792015-04-22 11:14:38 +01001298 err = mpls_dev_sysctl_register(dev, mdev);
1299 if (err)
1300 goto free;
1301
Robert Shearman03c57742015-04-22 11:14:37 +01001302 rcu_assign_pointer(dev->mpls_ptr, mdev);
1303
1304 return mdev;
Robert Shearman37bde792015-04-22 11:14:38 +01001305
1306free:
Robert Shearman27d69102017-01-16 14:16:37 +00001307 free_percpu(mdev->stats);
Robert Shearman37bde792015-04-22 11:14:38 +01001308 kfree(mdev);
1309 return ERR_PTR(err);
Robert Shearman03c57742015-04-22 11:14:37 +01001310}
1311
Robert Shearman27d69102017-01-16 14:16:37 +00001312static void mpls_dev_destroy_rcu(struct rcu_head *head)
1313{
1314 struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
1315
1316 free_percpu(mdev->stats);
1317 kfree(mdev);
1318}
1319
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001320static void mpls_ifdown(struct net_device *dev, int event)
Eric W. Biederman01891972015-03-03 19:10:47 -06001321{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001322 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -06001323 struct net *net = dev_net(dev);
David Ahern77ef013a2017-03-31 07:14:00 -07001324 u8 alive, deleted;
Eric W. Biederman01891972015-03-03 19:10:47 -06001325 unsigned index;
1326
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001327 platform_label = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06001328 for (index = 0; index < net->mpls.platform_labels; index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001329 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001330
Eric W. Biederman01891972015-03-03 19:10:47 -06001331 if (!rt)
1332 continue;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001333
David Ahern61733c92017-03-13 16:49:10 -07001334 alive = 0;
David Ahern4ea8efa2017-03-24 15:21:57 -07001335 deleted = 0;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001336 change_nexthops(rt) {
David Ahern39eb8cd2017-03-31 07:13:59 -07001337 unsigned int nh_flags = nh->nh_flags;
1338
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001339 if (rtnl_dereference(nh->nh_dev) != dev)
David Ahern61733c92017-03-13 16:49:10 -07001340 goto next;
1341
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001342 switch (event) {
1343 case NETDEV_DOWN:
1344 case NETDEV_UNREGISTER:
David Ahern39eb8cd2017-03-31 07:13:59 -07001345 nh_flags |= RTNH_F_DEAD;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001346 /* fall through */
1347 case NETDEV_CHANGE:
David Ahern39eb8cd2017-03-31 07:13:59 -07001348 nh_flags |= RTNH_F_LINKDOWN;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001349 break;
1350 }
1351 if (event == NETDEV_UNREGISTER)
1352 RCU_INIT_POINTER(nh->nh_dev, NULL);
David Ahern39eb8cd2017-03-31 07:13:59 -07001353
1354 if (nh->nh_flags != nh_flags)
1355 WRITE_ONCE(nh->nh_flags, nh_flags);
David Ahern61733c92017-03-13 16:49:10 -07001356next:
David Ahern39eb8cd2017-03-31 07:13:59 -07001357 if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
David Ahern61733c92017-03-13 16:49:10 -07001358 alive++;
David Ahern4ea8efa2017-03-24 15:21:57 -07001359 if (!rtnl_dereference(nh->nh_dev))
1360 deleted++;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001361 } endfor_nexthops(rt);
David Ahern61733c92017-03-13 16:49:10 -07001362
1363 WRITE_ONCE(rt->rt_nhn_alive, alive);
David Ahern4ea8efa2017-03-24 15:21:57 -07001364
1365 /* if there are no more nexthops, delete the route */
1366 if (event == NETDEV_UNREGISTER && deleted == rt->rt_nhn)
1367 mpls_route_update(net, index, NULL, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001368 }
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001369}
Robert Shearman37bde792015-04-22 11:14:38 +01001370
David Ahern39eb8cd2017-03-31 07:13:59 -07001371static void mpls_ifup(struct net_device *dev, unsigned int flags)
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001372{
1373 struct mpls_route __rcu **platform_label;
1374 struct net *net = dev_net(dev);
1375 unsigned index;
David Ahern77ef013a2017-03-31 07:14:00 -07001376 u8 alive;
Robert Shearman03c57742015-04-22 11:14:37 +01001377
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001378 platform_label = rtnl_dereference(net->mpls.platform_label);
1379 for (index = 0; index < net->mpls.platform_labels; index++) {
1380 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1381
1382 if (!rt)
1383 continue;
1384
1385 alive = 0;
1386 change_nexthops(rt) {
David Ahern39eb8cd2017-03-31 07:13:59 -07001387 unsigned int nh_flags = nh->nh_flags;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001388 struct net_device *nh_dev =
1389 rtnl_dereference(nh->nh_dev);
1390
David Ahern39eb8cd2017-03-31 07:13:59 -07001391 if (!(nh_flags & flags)) {
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001392 alive++;
1393 continue;
1394 }
1395 if (nh_dev != dev)
1396 continue;
1397 alive++;
David Ahern39eb8cd2017-03-31 07:13:59 -07001398 nh_flags &= ~flags;
1399 WRITE_ONCE(nh->nh_flags, flags);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001400 } endfor_nexthops(rt);
1401
David Ahern39eb8cd2017-03-31 07:13:59 -07001402 WRITE_ONCE(rt->rt_nhn_alive, alive);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001403 }
Eric W. Biederman01891972015-03-03 19:10:47 -06001404}
1405
1406static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
1407 void *ptr)
1408{
1409 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Robert Shearman03c57742015-04-22 11:14:37 +01001410 struct mpls_dev *mdev;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001411 unsigned int flags;
Eric W. Biederman01891972015-03-03 19:10:47 -06001412
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001413 if (event == NETDEV_REGISTER) {
Simon Horman407f31b2016-07-07 07:56:15 +02001414 /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
Simon Horman0d227a82016-06-16 17:09:09 +09001415 if (dev->type == ARPHRD_ETHER ||
1416 dev->type == ARPHRD_LOOPBACK ||
Simon Horman407f31b2016-07-07 07:56:15 +02001417 dev->type == ARPHRD_IPGRE ||
1418 dev->type == ARPHRD_SIT ||
1419 dev->type == ARPHRD_TUNNEL) {
Robert Shearman03c57742015-04-22 11:14:37 +01001420 mdev = mpls_add_dev(dev);
1421 if (IS_ERR(mdev))
1422 return notifier_from_errno(PTR_ERR(mdev));
1423 }
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001424 return NOTIFY_OK;
1425 }
Robert Shearman03c57742015-04-22 11:14:37 +01001426
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001427 mdev = mpls_dev_get(dev);
1428 if (!mdev)
1429 return NOTIFY_OK;
1430
1431 switch (event) {
1432 case NETDEV_DOWN:
1433 mpls_ifdown(dev, event);
1434 break;
1435 case NETDEV_UP:
1436 flags = dev_get_flags(dev);
1437 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1438 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1439 else
1440 mpls_ifup(dev, RTNH_F_DEAD);
1441 break;
1442 case NETDEV_CHANGE:
1443 flags = dev_get_flags(dev);
1444 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1445 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1446 else
1447 mpls_ifdown(dev, event);
1448 break;
Eric W. Biederman01891972015-03-03 19:10:47 -06001449 case NETDEV_UNREGISTER:
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001450 mpls_ifdown(dev, event);
1451 mdev = mpls_dev_get(dev);
1452 if (mdev) {
David Ahern1182e4d2017-03-28 14:28:07 -07001453 mpls_dev_sysctl_unregister(dev, mdev);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001454 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
Robert Shearman27d69102017-01-16 14:16:37 +00001455 call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001456 }
Eric W. Biederman01891972015-03-03 19:10:47 -06001457 break;
Robert Shearman0fae3bf2015-06-11 19:58:26 +01001458 case NETDEV_CHANGENAME:
1459 mdev = mpls_dev_get(dev);
1460 if (mdev) {
1461 int err;
1462
David Ahern1182e4d2017-03-28 14:28:07 -07001463 mpls_dev_sysctl_unregister(dev, mdev);
Robert Shearman0fae3bf2015-06-11 19:58:26 +01001464 err = mpls_dev_sysctl_register(dev, mdev);
1465 if (err)
1466 return notifier_from_errno(err);
1467 }
1468 break;
Eric W. Biederman01891972015-03-03 19:10:47 -06001469 }
1470 return NOTIFY_OK;
1471}
1472
1473static struct notifier_block mpls_dev_notifier = {
1474 .notifier_call = mpls_dev_notify,
1475};
1476
Eric W. Biederman03c05662015-03-03 19:13:56 -06001477static int nla_put_via(struct sk_buff *skb,
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001478 u8 table, const void *addr, int alen)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001479{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001480 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
1481 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
1482 };
Eric W. Biederman03c05662015-03-03 19:13:56 -06001483 struct nlattr *nla;
1484 struct rtvia *via;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001485 int family = AF_UNSPEC;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001486
1487 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1488 if (!nla)
1489 return -EMSGSIZE;
1490
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001491 if (table <= NEIGH_NR_TABLES)
1492 family = table_to_family[table];
1493
Eric W. Biederman03c05662015-03-03 19:13:56 -06001494 via = nla_data(nla);
1495 via->rtvia_family = family;
1496 memcpy(via->rtvia_addr, addr, alen);
1497 return 0;
1498}
1499
Eric W. Biederman966bae32015-03-03 19:13:19 -06001500int nla_put_labels(struct sk_buff *skb, int attrtype,
1501 u8 labels, const u32 label[])
1502{
1503 struct nlattr *nla;
1504 struct mpls_shim_hdr *nla_label;
1505 bool bos;
1506 int i;
1507 nla = nla_reserve(skb, attrtype, labels*4);
1508 if (!nla)
1509 return -EMSGSIZE;
1510
1511 nla_label = nla_data(nla);
1512 bos = true;
1513 for (i = labels - 1; i >= 0; i--) {
1514 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1515 bos = false;
1516 }
1517
1518 return 0;
1519}
Roopa Prabhuface0182015-07-21 10:43:52 +02001520EXPORT_SYMBOL_GPL(nla_put_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001521
1522int nla_get_labels(const struct nlattr *nla,
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001523 u32 max_labels, u8 *labels, u32 label[])
Eric W. Biederman966bae32015-03-03 19:13:19 -06001524{
1525 unsigned len = nla_len(nla);
1526 unsigned nla_labels;
1527 struct mpls_shim_hdr *nla_label;
1528 bool bos;
1529 int i;
1530
1531 /* len needs to be an even multiple of 4 (the label size) */
1532 if (len & 3)
1533 return -EINVAL;
1534
1535 /* Limit the number of new labels allowed */
1536 nla_labels = len/4;
1537 if (nla_labels > max_labels)
1538 return -EINVAL;
1539
1540 nla_label = nla_data(nla);
1541 bos = true;
1542 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1543 struct mpls_entry_decoded dec;
1544 dec = mpls_entry_decode(nla_label + i);
1545
1546 /* Ensure the bottom of stack flag is properly set
1547 * and ttl and tc are both clear.
1548 */
1549 if ((dec.bos != bos) || dec.ttl || dec.tc)
1550 return -EINVAL;
1551
Robert Shearman5a9ab012015-04-22 11:14:39 +01001552 switch (dec.label) {
Tom Herbert78f5b892015-05-07 08:08:51 -07001553 case MPLS_LABEL_IMPLNULL:
Robert Shearman5a9ab012015-04-22 11:14:39 +01001554 /* RFC3032: This is a label that an LSR may
1555 * assign and distribute, but which never
1556 * actually appears in the encapsulation.
1557 */
1558 return -EINVAL;
1559 }
1560
Eric W. Biederman966bae32015-03-03 19:13:19 -06001561 label[i] = dec.label;
1562 }
1563 *labels = nla_labels;
1564 return 0;
1565}
Roopa Prabhuface0182015-07-21 10:43:52 +02001566EXPORT_SYMBOL_GPL(nla_get_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001567
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001568int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1569 u8 *via_table, u8 via_addr[])
1570{
1571 struct rtvia *via = nla_data(nla);
1572 int err = -EINVAL;
1573 int alen;
1574
1575 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1576 goto errout;
1577 alen = nla_len(nla) -
1578 offsetof(struct rtvia, rtvia_addr);
1579 if (alen > MAX_VIA_ALEN)
1580 goto errout;
1581
1582 /* Validate the address family */
1583 switch (via->rtvia_family) {
1584 case AF_PACKET:
1585 *via_table = NEIGH_LINK_TABLE;
1586 break;
1587 case AF_INET:
1588 *via_table = NEIGH_ARP_TABLE;
1589 if (alen != 4)
1590 goto errout;
1591 break;
1592 case AF_INET6:
1593 *via_table = NEIGH_ND_TABLE;
1594 if (alen != 16)
1595 goto errout;
1596 break;
1597 default:
1598 /* Unsupported address family */
1599 goto errout;
1600 }
1601
1602 memcpy(via_addr, via->rtvia_addr, alen);
1603 *via_alen = alen;
1604 err = 0;
1605
1606errout:
1607 return err;
1608}
1609
Eric W. Biederman03c05662015-03-03 19:13:56 -06001610static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1611 struct mpls_route_config *cfg)
1612{
1613 struct rtmsg *rtm;
1614 struct nlattr *tb[RTA_MAX+1];
1615 int index;
1616 int err;
1617
1618 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1619 if (err < 0)
1620 goto errout;
1621
1622 err = -EINVAL;
1623 rtm = nlmsg_data(nlh);
1624 memset(cfg, 0, sizeof(*cfg));
1625
1626 if (rtm->rtm_family != AF_MPLS)
1627 goto errout;
1628 if (rtm->rtm_dst_len != 20)
1629 goto errout;
1630 if (rtm->rtm_src_len != 0)
1631 goto errout;
1632 if (rtm->rtm_tos != 0)
1633 goto errout;
1634 if (rtm->rtm_table != RT_TABLE_MAIN)
1635 goto errout;
1636 /* Any value is acceptable for rtm_protocol */
1637
1638 /* As mpls uses destination specific addresses
1639 * (or source specific address in the case of multicast)
1640 * all addresses have universal scope.
1641 */
1642 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1643 goto errout;
1644 if (rtm->rtm_type != RTN_UNICAST)
1645 goto errout;
1646 if (rtm->rtm_flags != 0)
1647 goto errout;
1648
1649 cfg->rc_label = LABEL_NOT_SPECIFIED;
1650 cfg->rc_protocol = rtm->rtm_protocol;
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001651 cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001652 cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001653 cfg->rc_nlflags = nlh->nlmsg_flags;
1654 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1655 cfg->rc_nlinfo.nlh = nlh;
1656 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1657
1658 for (index = 0; index <= RTA_MAX; index++) {
1659 struct nlattr *nla = tb[index];
1660 if (!nla)
1661 continue;
1662
Suraj Deshmukh14dd3e12016-12-03 07:59:26 +00001663 switch (index) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001664 case RTA_OIF:
1665 cfg->rc_ifindex = nla_get_u32(nla);
1666 break;
1667 case RTA_NEWDST:
1668 if (nla_get_labels(nla, MAX_NEW_LABELS,
1669 &cfg->rc_output_labels,
1670 cfg->rc_output_label))
1671 goto errout;
1672 break;
1673 case RTA_DST:
1674 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001675 u8 label_count;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001676 if (nla_get_labels(nla, 1, &label_count,
1677 &cfg->rc_label))
1678 goto errout;
1679
Robert Shearmana6affd22015-08-03 17:50:04 +01001680 /* Reserved labels may not be set */
1681 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001682 goto errout;
1683
1684 break;
1685 }
1686 case RTA_VIA:
1687 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001688 if (nla_get_via(nla, &cfg->rc_via_alen,
1689 &cfg->rc_via_table, cfg->rc_via))
Robert Shearmanf8d54af2015-03-06 10:47:00 +00001690 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001691 break;
1692 }
1693 case RTA_MULTIPATH:
1694 {
1695 cfg->rc_mp = nla_data(nla);
1696 cfg->rc_mp_len = nla_len(nla);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001697 break;
1698 }
Robert Shearman5b441ac2017-03-10 20:43:24 +00001699 case RTA_TTL_PROPAGATE:
1700 {
1701 u8 ttl_propagate = nla_get_u8(nla);
1702
1703 if (ttl_propagate > 1)
1704 goto errout;
1705 cfg->rc_ttl_propagate = ttl_propagate ?
1706 MPLS_TTL_PROP_ENABLED :
1707 MPLS_TTL_PROP_DISABLED;
1708 break;
1709 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001710 default:
1711 /* Unsupported attribute */
1712 goto errout;
1713 }
1714 }
1715
1716 err = 0;
1717errout:
1718 return err;
1719}
1720
1721static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1722{
1723 struct mpls_route_config cfg;
1724 int err;
1725
1726 err = rtm_to_route_config(skb, nlh, &cfg);
1727 if (err < 0)
1728 return err;
1729
1730 return mpls_route_del(&cfg);
1731}
1732
1733
1734static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1735{
1736 struct mpls_route_config cfg;
1737 int err;
1738
1739 err = rtm_to_route_config(skb, nlh, &cfg);
1740 if (err < 0)
1741 return err;
1742
1743 return mpls_route_add(&cfg);
1744}
1745
1746static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1747 u32 label, struct mpls_route *rt, int flags)
1748{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001749 struct net_device *dev;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001750 struct nlmsghdr *nlh;
1751 struct rtmsg *rtm;
1752
1753 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1754 if (nlh == NULL)
1755 return -EMSGSIZE;
1756
1757 rtm = nlmsg_data(nlh);
1758 rtm->rtm_family = AF_MPLS;
1759 rtm->rtm_dst_len = 20;
1760 rtm->rtm_src_len = 0;
1761 rtm->rtm_tos = 0;
1762 rtm->rtm_table = RT_TABLE_MAIN;
1763 rtm->rtm_protocol = rt->rt_protocol;
1764 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1765 rtm->rtm_type = RTN_UNICAST;
1766 rtm->rtm_flags = 0;
1767
Eric W. Biederman03c05662015-03-03 19:13:56 -06001768 if (nla_put_labels(skb, RTA_DST, 1, &label))
1769 goto nla_put_failure;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001770
1771 if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
1772 bool ttl_propagate =
1773 rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
1774
1775 if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
1776 ttl_propagate))
1777 goto nla_put_failure;
1778 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001779 if (rt->rt_nhn == 1) {
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001780 const struct mpls_nh *nh = rt->rt_nh;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001781
1782 if (nh->nh_labels &&
1783 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1784 nh->nh_label))
1785 goto nla_put_failure;
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001786 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
Robert Shearman72dcac92015-12-10 19:30:49 +00001787 nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001788 nh->nh_via_alen))
1789 goto nla_put_failure;
1790 dev = rtnl_dereference(nh->nh_dev);
1791 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1792 goto nla_put_failure;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001793 if (nh->nh_flags & RTNH_F_LINKDOWN)
1794 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1795 if (nh->nh_flags & RTNH_F_DEAD)
1796 rtm->rtm_flags |= RTNH_F_DEAD;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001797 } else {
1798 struct rtnexthop *rtnh;
1799 struct nlattr *mp;
David Ahern77ef013a2017-03-31 07:14:00 -07001800 u8 linkdown = 0;
1801 u8 dead = 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001802
1803 mp = nla_nest_start(skb, RTA_MULTIPATH);
1804 if (!mp)
1805 goto nla_put_failure;
1806
1807 for_nexthops(rt) {
David Ahernc00e51d2017-03-24 15:21:56 -07001808 dev = rtnl_dereference(nh->nh_dev);
1809 if (!dev)
1810 continue;
1811
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001812 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1813 if (!rtnh)
1814 goto nla_put_failure;
1815
David Ahernc00e51d2017-03-24 15:21:56 -07001816 rtnh->rtnh_ifindex = dev->ifindex;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001817 if (nh->nh_flags & RTNH_F_LINKDOWN) {
1818 rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
1819 linkdown++;
1820 }
1821 if (nh->nh_flags & RTNH_F_DEAD) {
1822 rtnh->rtnh_flags |= RTNH_F_DEAD;
1823 dead++;
1824 }
1825
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001826 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1827 nh->nh_labels,
1828 nh->nh_label))
1829 goto nla_put_failure;
Robert Shearmanf20367d2015-12-10 19:30:51 +00001830 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1831 nla_put_via(skb, nh->nh_via_table,
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001832 mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001833 nh->nh_via_alen))
1834 goto nla_put_failure;
1835
1836 /* length of rtnetlink header + attributes */
1837 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1838 } endfor_nexthops(rt);
1839
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001840 if (linkdown == rt->rt_nhn)
1841 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1842 if (dead == rt->rt_nhn)
1843 rtm->rtm_flags |= RTNH_F_DEAD;
1844
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001845 nla_nest_end(skb, mp);
1846 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001847
1848 nlmsg_end(skb, nlh);
1849 return 0;
1850
1851nla_put_failure:
1852 nlmsg_cancel(skb, nlh);
1853 return -EMSGSIZE;
1854}
1855
1856static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1857{
1858 struct net *net = sock_net(skb->sk);
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001859 struct mpls_route __rcu **platform_label;
1860 size_t platform_labels;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001861 unsigned int index;
1862
1863 ASSERT_RTNL();
1864
1865 index = cb->args[0];
Robert Shearmana6affd22015-08-03 17:50:04 +01001866 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1867 index = MPLS_LABEL_FIRST_UNRESERVED;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001868
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001869 platform_label = rtnl_dereference(net->mpls.platform_label);
1870 platform_labels = net->mpls.platform_labels;
1871 for (; index < platform_labels; index++) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001872 struct mpls_route *rt;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001873 rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001874 if (!rt)
1875 continue;
1876
1877 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1878 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1879 index, rt, NLM_F_MULTI) < 0)
1880 break;
1881 }
1882 cb->args[0] = index;
1883
1884 return skb->len;
1885}
1886
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001887static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1888{
1889 size_t payload =
1890 NLMSG_ALIGN(sizeof(struct rtmsg))
Robert Shearman5b441ac2017-03-10 20:43:24 +00001891 + nla_total_size(4) /* RTA_DST */
1892 + nla_total_size(1); /* RTA_TTL_PROPAGATE */
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001893
1894 if (rt->rt_nhn == 1) {
1895 struct mpls_nh *nh = rt->rt_nh;
1896
1897 if (nh->nh_dev)
1898 payload += nla_total_size(4); /* RTA_OIF */
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001899 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
Robert Shearman72dcac92015-12-10 19:30:49 +00001900 payload += nla_total_size(2 + nh->nh_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001901 if (nh->nh_labels) /* RTA_NEWDST */
1902 payload += nla_total_size(nh->nh_labels * 4);
1903 } else {
1904 /* each nexthop is packed in an attribute */
1905 size_t nhsize = 0;
1906
1907 for_nexthops(rt) {
David Aherne944e972017-03-28 15:19:49 -07001908 if (!rtnl_dereference(nh->nh_dev))
1909 continue;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001910 nhsize += nla_total_size(sizeof(struct rtnexthop));
Robert Shearmanf20367d2015-12-10 19:30:51 +00001911 /* RTA_VIA */
1912 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
1913 nhsize += nla_total_size(2 + nh->nh_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001914 if (nh->nh_labels)
1915 nhsize += nla_total_size(nh->nh_labels * 4);
1916 } endfor_nexthops(rt);
1917 /* nested attribute */
1918 payload += nla_total_size(nhsize);
1919 }
1920
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001921 return payload;
1922}
1923
1924static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1925 struct nlmsghdr *nlh, struct net *net, u32 portid,
1926 unsigned int nlm_flags)
1927{
1928 struct sk_buff *skb;
1929 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1930 int err = -ENOBUFS;
1931
1932 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1933 if (skb == NULL)
1934 goto errout;
1935
1936 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1937 if (err < 0) {
1938 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1939 WARN_ON(err == -EMSGSIZE);
1940 kfree_skb(skb);
1941 goto errout;
1942 }
1943 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1944
1945 return;
1946errout:
1947 if (err < 0)
1948 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1949}
1950
Eric W. Biederman7720c012015-03-03 19:11:20 -06001951static int resize_platform_label_table(struct net *net, size_t limit)
1952{
1953 size_t size = sizeof(struct mpls_route *) * limit;
1954 size_t old_limit;
1955 size_t cp_size;
1956 struct mpls_route __rcu **labels = NULL, **old;
1957 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1958 unsigned index;
1959
1960 if (size) {
1961 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1962 if (!labels)
1963 labels = vzalloc(size);
1964
1965 if (!labels)
1966 goto nolabels;
1967 }
1968
1969 /* In case the predefined labels need to be populated */
Tom Herbert78f5b892015-05-07 08:08:51 -07001970 if (limit > MPLS_LABEL_IPV4NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001971 struct net_device *lo = net->loopback_dev;
David Ahern59b20962017-03-31 07:14:01 -07001972 rt0 = mpls_rt_alloc(1, lo->addr_len, MAX_NEW_LABELS);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001973 if (!rt0)
1974 goto nort0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001975 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001976 rt0->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001977 rt0->rt_payload_type = MPT_IPV4;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001978 rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001979 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00001980 rt0->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001981 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
1982 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001983 }
Tom Herbert78f5b892015-05-07 08:08:51 -07001984 if (limit > MPLS_LABEL_IPV6NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001985 struct net_device *lo = net->loopback_dev;
David Ahern59b20962017-03-31 07:14:01 -07001986 rt2 = mpls_rt_alloc(1, lo->addr_len, MAX_NEW_LABELS);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001987 if (!rt2)
1988 goto nort2;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001989 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001990 rt2->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001991 rt2->rt_payload_type = MPT_IPV6;
David Ahern6a18c312017-03-23 19:02:27 -06001992 rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001993 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00001994 rt2->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001995 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
1996 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001997 }
1998
1999 rtnl_lock();
2000 /* Remember the original table */
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002001 old = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002002 old_limit = net->mpls.platform_labels;
2003
2004 /* Free any labels beyond the new table */
2005 for (index = limit; index < old_limit; index++)
Roopa Prabhuf8efb732015-10-23 06:03:27 -07002006 mpls_route_update(net, index, NULL, NULL);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002007
2008 /* Copy over the old labels */
2009 cp_size = size;
2010 if (old_limit < limit)
2011 cp_size = old_limit * sizeof(struct mpls_route *);
2012
2013 memcpy(labels, old, cp_size);
2014
2015 /* If needed set the predefined labels */
Tom Herbert78f5b892015-05-07 08:08:51 -07002016 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
2017 (limit > MPLS_LABEL_IPV6NULL)) {
2018 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002019 rt2 = NULL;
2020 }
2021
Tom Herbert78f5b892015-05-07 08:08:51 -07002022 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
2023 (limit > MPLS_LABEL_IPV4NULL)) {
2024 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002025 rt0 = NULL;
2026 }
2027
2028 /* Update the global pointers */
2029 net->mpls.platform_labels = limit;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002030 rcu_assign_pointer(net->mpls.platform_label, labels);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002031
2032 rtnl_unlock();
2033
2034 mpls_rt_free(rt2);
2035 mpls_rt_free(rt0);
2036
2037 if (old) {
2038 synchronize_rcu();
2039 kvfree(old);
2040 }
2041 return 0;
2042
2043nort2:
2044 mpls_rt_free(rt0);
2045nort0:
2046 kvfree(labels);
2047nolabels:
2048 return -ENOMEM;
2049}
2050
2051static int mpls_platform_labels(struct ctl_table *table, int write,
2052 void __user *buffer, size_t *lenp, loff_t *ppos)
2053{
2054 struct net *net = table->data;
2055 int platform_labels = net->mpls.platform_labels;
2056 int ret;
2057 struct ctl_table tmp = {
2058 .procname = table->procname,
2059 .data = &platform_labels,
2060 .maxlen = sizeof(int),
2061 .mode = table->mode,
2062 .extra1 = &zero,
2063 .extra2 = &label_limit,
2064 };
2065
2066 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2067
2068 if (write && ret == 0)
2069 ret = resize_platform_label_table(net, platform_labels);
2070
2071 return ret;
2072}
2073
Robert Shearman5b441ac2017-03-10 20:43:24 +00002074#define MPLS_NS_SYSCTL_OFFSET(field) \
2075 (&((struct net *)0)->field)
2076
Robert Shearman37bde792015-04-22 11:14:38 +01002077static const struct ctl_table mpls_table[] = {
Eric W. Biederman7720c012015-03-03 19:11:20 -06002078 {
2079 .procname = "platform_labels",
2080 .data = NULL,
2081 .maxlen = sizeof(int),
2082 .mode = 0644,
2083 .proc_handler = mpls_platform_labels,
2084 },
Robert Shearman5b441ac2017-03-10 20:43:24 +00002085 {
2086 .procname = "ip_ttl_propagate",
2087 .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
2088 .maxlen = sizeof(int),
2089 .mode = 0644,
2090 .proc_handler = proc_dointvec_minmax,
2091 .extra1 = &zero,
2092 .extra2 = &one,
2093 },
Robert Shearmana59166e2017-03-10 20:43:25 +00002094 {
2095 .procname = "default_ttl",
2096 .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
2097 .maxlen = sizeof(int),
2098 .mode = 0644,
2099 .proc_handler = proc_dointvec_minmax,
2100 .extra1 = &one,
2101 .extra2 = &ttl_max,
2102 },
Eric W. Biederman7720c012015-03-03 19:11:20 -06002103 { }
2104};
2105
Eric W. Biederman01891972015-03-03 19:10:47 -06002106static int mpls_net_init(struct net *net)
2107{
Eric W. Biederman7720c012015-03-03 19:11:20 -06002108 struct ctl_table *table;
Robert Shearman5b441ac2017-03-10 20:43:24 +00002109 int i;
Eric W. Biederman7720c012015-03-03 19:11:20 -06002110
Eric W. Biederman01891972015-03-03 19:10:47 -06002111 net->mpls.platform_labels = 0;
2112 net->mpls.platform_label = NULL;
Robert Shearman5b441ac2017-03-10 20:43:24 +00002113 net->mpls.ip_ttl_propagate = 1;
Robert Shearmana59166e2017-03-10 20:43:25 +00002114 net->mpls.default_ttl = 255;
Eric W. Biederman01891972015-03-03 19:10:47 -06002115
Eric W. Biederman7720c012015-03-03 19:11:20 -06002116 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
2117 if (table == NULL)
2118 return -ENOMEM;
2119
Robert Shearman5b441ac2017-03-10 20:43:24 +00002120 /* Table data contains only offsets relative to the base of
2121 * the mdev at this point, so make them absolute.
2122 */
2123 for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++)
2124 table[i].data = (char *)net + (uintptr_t)table[i].data;
2125
Eric W. Biederman7720c012015-03-03 19:11:20 -06002126 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07002127 if (net->mpls.ctl == NULL) {
2128 kfree(table);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002129 return -ENOMEM;
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07002130 }
Eric W. Biederman7720c012015-03-03 19:11:20 -06002131
Eric W. Biederman01891972015-03-03 19:10:47 -06002132 return 0;
2133}
2134
2135static void mpls_net_exit(struct net *net)
2136{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002137 struct mpls_route __rcu **platform_label;
2138 size_t platform_labels;
Eric W. Biederman7720c012015-03-03 19:11:20 -06002139 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -06002140 unsigned int index;
2141
Eric W. Biederman7720c012015-03-03 19:11:20 -06002142 table = net->mpls.ctl->ctl_table_arg;
2143 unregister_net_sysctl_table(net->mpls.ctl);
2144 kfree(table);
2145
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002146 /* An rcu grace period has passed since there was a device in
2147 * the network namespace (and thus the last in flight packet)
Eric W. Biederman01891972015-03-03 19:10:47 -06002148 * left this network namespace. This is because
2149 * unregister_netdevice_many and netdev_run_todo has completed
2150 * for each network device that was in this network namespace.
2151 *
2152 * As such no additional rcu synchronization is necessary when
2153 * freeing the platform_label table.
2154 */
2155 rtnl_lock();
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002156 platform_label = rtnl_dereference(net->mpls.platform_label);
2157 platform_labels = net->mpls.platform_labels;
2158 for (index = 0; index < platform_labels; index++) {
2159 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
2160 RCU_INIT_POINTER(platform_label[index], NULL);
David Aherne37791e2017-03-10 09:46:15 -08002161 mpls_notify_route(net, index, rt, NULL, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06002162 mpls_rt_free(rt);
2163 }
2164 rtnl_unlock();
2165
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002166 kvfree(platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06002167}
2168
2169static struct pernet_operations mpls_net_ops = {
2170 .init = mpls_net_init,
2171 .exit = mpls_net_exit,
2172};
2173
Robert Shearman27d69102017-01-16 14:16:37 +00002174static struct rtnl_af_ops mpls_af_ops __read_mostly = {
2175 .family = AF_MPLS,
2176 .fill_stats_af = mpls_fill_stats_af,
2177 .get_stats_af_size = mpls_get_stats_af_size,
2178};
2179
Eric W. Biederman01891972015-03-03 19:10:47 -06002180static int __init mpls_init(void)
2181{
2182 int err;
2183
2184 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
2185
2186 err = register_pernet_subsys(&mpls_net_ops);
2187 if (err)
2188 goto out;
2189
2190 err = register_netdevice_notifier(&mpls_dev_notifier);
2191 if (err)
2192 goto out_unregister_pernet;
2193
2194 dev_add_pack(&mpls_packet_type);
2195
Robert Shearman27d69102017-01-16 14:16:37 +00002196 rtnl_af_register(&mpls_af_ops);
2197
Eric W. Biederman03c05662015-03-03 19:13:56 -06002198 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
2199 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
2200 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
David Ahern24045a02017-02-20 08:03:30 -08002201 rtnl_register(PF_MPLS, RTM_GETNETCONF, mpls_netconf_get_devconf,
2202 mpls_netconf_dump_devconf, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06002203 err = 0;
2204out:
2205 return err;
2206
2207out_unregister_pernet:
2208 unregister_pernet_subsys(&mpls_net_ops);
2209 goto out;
2210}
2211module_init(mpls_init);
2212
2213static void __exit mpls_exit(void)
2214{
Eric W. Biederman03c05662015-03-03 19:13:56 -06002215 rtnl_unregister_all(PF_MPLS);
Robert Shearman27d69102017-01-16 14:16:37 +00002216 rtnl_af_unregister(&mpls_af_ops);
Eric W. Biederman01891972015-03-03 19:10:47 -06002217 dev_remove_pack(&mpls_packet_type);
2218 unregister_netdevice_notifier(&mpls_dev_notifier);
2219 unregister_pernet_subsys(&mpls_net_ops);
2220}
2221module_exit(mpls_exit);
2222
2223MODULE_DESCRIPTION("MultiProtocol Label Switching");
2224MODULE_LICENSE("GPL v2");
2225MODULE_ALIAS_NETPROTO(PF_MPLS);