blob: 0e1046f21af810dc9fc5f2d7d59ce0b8b186a547 [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
Robert Shearman1c78efa2015-10-23 06:03:28 -070027/* Maximum number of labels to look ahead at when selecting a path of
28 * a multipath route
29 */
30#define MAX_MP_SELECT_LABELS 4
31
Robert Shearmaneb7809f2015-12-10 19:30:50 +000032#define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
33
Eric W. Biederman7720c012015-03-03 19:11:20 -060034static int zero = 0;
Robert Shearman5b441ac2017-03-10 20:43:24 +000035static int one = 1;
Eric W. Biederman7720c012015-03-03 19:11:20 -060036static int label_limit = (1 << 20) - 1;
37
Eric W. Biederman8de147d2015-03-03 19:14:31 -060038static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
39 struct nlmsghdr *nlh, struct net *net, u32 portid,
40 unsigned int nlm_flags);
41
Eric W. Biederman01891972015-03-03 19:10:47 -060042static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
43{
44 struct mpls_route *rt = NULL;
45
46 if (index < net->mpls.platform_labels) {
47 struct mpls_route __rcu **platform_label =
48 rcu_dereference(net->mpls.platform_label);
49 rt = rcu_dereference(platform_label[index]);
50 }
51 return rt;
52}
53
Roopa Prabhuface0182015-07-21 10:43:52 +020054bool mpls_output_possible(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060055{
56 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
57}
Roopa Prabhuface0182015-07-21 10:43:52 +020058EXPORT_SYMBOL_GPL(mpls_output_possible);
Eric W. Biederman01891972015-03-03 19:10:47 -060059
Robert Shearmancf4b24f2015-10-27 00:37:36 +000060static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
61{
62 u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
63 int nh_index = nh - rt->rt_nh;
64
65 return nh0_via + rt->rt_max_alen * nh_index;
66}
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
191static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
David Ahern9f427a0e2017-01-20 12:58:34 -0800192 struct sk_buff *skb)
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800193{
194 int alive = ACCESS_ONCE(rt->rt_nhn_alive);
195 u32 hash = 0;
196 int nh_index = 0;
197 int n = 0;
198
199 /* No need to look further into packet if there's only
200 * one path
201 */
202 if (rt->rt_nhn == 1)
203 goto out;
204
205 if (alive <= 0)
206 return NULL;
207
David Ahern9f427a0e2017-01-20 12:58:34 -0800208 hash = mpls_multipath_hash(rt, skb);
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800209 nh_index = hash % alive;
210 if (alive == rt->rt_nhn)
211 goto out;
212 for_nexthops(rt) {
213 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
214 continue;
215 if (n == nh_index)
216 return nh;
217 n++;
218 } endfor_nexthops(rt);
219
Robert Shearman1c78efa2015-10-23 06:03:28 -0700220out:
221 return &rt->rt_nh[nh_index];
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700222}
223
Robert Shearman5b441ac2017-03-10 20:43:24 +0000224static bool mpls_egress(struct net *net, struct mpls_route *rt,
225 struct sk_buff *skb, struct mpls_entry_decoded dec)
Eric W. Biederman01891972015-03-03 19:10:47 -0600226{
Robert Shearman118d5232015-08-06 11:04:56 +0100227 enum mpls_payload_type payload_type;
228 bool success = false;
Eric W. Biederman01891972015-03-03 19:10:47 -0600229
Eric W. Biederman76fecd82015-03-12 18:22:59 -0500230 /* The IPv4 code below accesses through the IPv4 header
231 * checksum, which is 12 bytes into the packet.
232 * The IPv6 code below accesses through the IPv6 hop limit
233 * which is 8 bytes into the packet.
234 *
235 * For all supported cases there should always be at least 12
236 * bytes of packet data present. The IPv4 header is 20 bytes
237 * without options and the IPv6 header is always 40 bytes
238 * long.
239 */
240 if (!pskb_may_pull(skb, 12))
241 return false;
242
Robert Shearman118d5232015-08-06 11:04:56 +0100243 payload_type = rt->rt_payload_type;
244 if (payload_type == MPT_UNSPEC)
245 payload_type = ip_hdr(skb)->version;
246
247 switch (payload_type) {
248 case MPT_IPV4: {
249 struct iphdr *hdr4 = ip_hdr(skb);
Robert Shearman5b441ac2017-03-10 20:43:24 +0000250 u8 new_ttl;
Eric W. Biederman01891972015-03-03 19:10:47 -0600251 skb->protocol = htons(ETH_P_IP);
Robert Shearman5b441ac2017-03-10 20:43:24 +0000252
253 /* If propagating TTL, take the decremented TTL from
254 * the incoming MPLS header, otherwise decrement the
255 * TTL, but only if not 0 to avoid underflow.
256 */
257 if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
258 (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
259 net->mpls.ip_ttl_propagate))
260 new_ttl = dec.ttl;
261 else
262 new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
263
Eric W. Biederman01891972015-03-03 19:10:47 -0600264 csum_replace2(&hdr4->check,
265 htons(hdr4->ttl << 8),
Robert Shearman5b441ac2017-03-10 20:43:24 +0000266 htons(new_ttl << 8));
267 hdr4->ttl = new_ttl;
Robert Shearman118d5232015-08-06 11:04:56 +0100268 success = true;
269 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600270 }
Robert Shearman118d5232015-08-06 11:04:56 +0100271 case MPT_IPV6: {
Eric W. Biederman01891972015-03-03 19:10:47 -0600272 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
273 skb->protocol = htons(ETH_P_IPV6);
Robert Shearman5b441ac2017-03-10 20:43:24 +0000274
275 /* If propagating TTL, take the decremented TTL from
276 * the incoming MPLS header, otherwise decrement the
277 * hop limit, but only if not 0 to avoid underflow.
278 */
279 if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
280 (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
281 net->mpls.ip_ttl_propagate))
282 hdr6->hop_limit = dec.ttl;
283 else if (hdr6->hop_limit)
284 hdr6->hop_limit = hdr6->hop_limit - 1;
Robert Shearman118d5232015-08-06 11:04:56 +0100285 success = true;
286 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600287 }
Robert Shearman118d5232015-08-06 11:04:56 +0100288 case MPT_UNSPEC:
Robert Shearman5b441ac2017-03-10 20:43:24 +0000289 /* Should have decided which protocol it is by now */
Robert Shearman118d5232015-08-06 11:04:56 +0100290 break;
291 }
292
Eric W. Biederman01891972015-03-03 19:10:47 -0600293 return success;
294}
295
296static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
297 struct packet_type *pt, struct net_device *orig_dev)
298{
299 struct net *net = dev_net(dev);
300 struct mpls_shim_hdr *hdr;
301 struct mpls_route *rt;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700302 struct mpls_nh *nh;
Eric W. Biederman01891972015-03-03 19:10:47 -0600303 struct mpls_entry_decoded dec;
304 struct net_device *out_dev;
Robert Shearman27d69102017-01-16 14:16:37 +0000305 struct mpls_dev *out_mdev;
Robert Shearman03c57742015-04-22 11:14:37 +0100306 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600307 unsigned int hh_len;
308 unsigned int new_header_size;
309 unsigned int mtu;
310 int err;
311
312 /* Careful this entire function runs inside of an rcu critical section */
313
Robert Shearman03c57742015-04-22 11:14:37 +0100314 mdev = mpls_dev_get(dev);
Robert Shearman27d69102017-01-16 14:16:37 +0000315 if (!mdev)
Robert Shearman03c57742015-04-22 11:14:37 +0100316 goto drop;
317
Robert Shearman27d69102017-01-16 14:16:37 +0000318 MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
319 rx_bytes);
320
321 if (!mdev->input_enabled) {
322 MPLS_INC_STATS(mdev, rx_dropped);
323 goto drop;
324 }
325
Eric W. Biederman01891972015-03-03 19:10:47 -0600326 if (skb->pkt_type != PACKET_HOST)
Robert Shearman27d69102017-01-16 14:16:37 +0000327 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600328
329 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
Robert Shearman27d69102017-01-16 14:16:37 +0000330 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600331
332 if (!pskb_may_pull(skb, sizeof(*hdr)))
Robert Shearman27d69102017-01-16 14:16:37 +0000333 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600334
335 /* Read and decode the label */
336 hdr = mpls_hdr(skb);
337 dec = mpls_entry_decode(hdr);
338
Eric W. Biederman01891972015-03-03 19:10:47 -0600339 rt = mpls_route_input_rcu(net, dec.label);
Robert Shearman27d69102017-01-16 14:16:37 +0000340 if (!rt) {
341 MPLS_INC_STATS(mdev, rx_noroute);
Eric W. Biederman01891972015-03-03 19:10:47 -0600342 goto drop;
Robert Shearman27d69102017-01-16 14:16:37 +0000343 }
Eric W. Biederman01891972015-03-03 19:10:47 -0600344
David Ahern9f427a0e2017-01-20 12:58:34 -0800345 nh = mpls_select_multipath(rt, skb);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700346 if (!nh)
Robert Shearman27d69102017-01-16 14:16:37 +0000347 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600348
David Ahern9f427a0e2017-01-20 12:58:34 -0800349 /* Pop the label */
350 skb_pull(skb, sizeof(*hdr));
351 skb_reset_network_header(skb);
352
353 skb_orphan(skb);
354
Eric W. Biederman01891972015-03-03 19:10:47 -0600355 if (skb_warn_if_lro(skb))
Robert Shearman27d69102017-01-16 14:16:37 +0000356 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600357
358 skb_forward_csum(skb);
359
360 /* Verify ttl is valid */
Eric W. Biedermanaa7da932015-03-07 16:23:23 -0600361 if (dec.ttl <= 1)
Robert Shearman27d69102017-01-16 14:16:37 +0000362 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600363 dec.ttl -= 1;
364
Robert Shearman27d69102017-01-16 14:16:37 +0000365 /* Find the output device */
366 out_dev = rcu_dereference(nh->nh_dev);
367 if (!mpls_output_possible(out_dev))
368 goto tx_err;
369
Eric W. Biederman01891972015-03-03 19:10:47 -0600370 /* Verify the destination can hold the packet */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700371 new_header_size = mpls_nh_header_size(nh);
Eric W. Biederman01891972015-03-03 19:10:47 -0600372 mtu = mpls_dev_mtu(out_dev);
373 if (mpls_pkt_too_big(skb, mtu - new_header_size))
Robert Shearman27d69102017-01-16 14:16:37 +0000374 goto tx_err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600375
376 hh_len = LL_RESERVED_SPACE(out_dev);
377 if (!out_dev->header_ops)
378 hh_len = 0;
379
380 /* Ensure there is enough space for the headers in the skb */
381 if (skb_cow(skb, hh_len + new_header_size))
Robert Shearman27d69102017-01-16 14:16:37 +0000382 goto tx_err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600383
384 skb->dev = out_dev;
385 skb->protocol = htons(ETH_P_MPLS_UC);
386
387 if (unlikely(!new_header_size && dec.bos)) {
388 /* Penultimate hop popping */
Robert Shearman5b441ac2017-03-10 20:43:24 +0000389 if (!mpls_egress(dev_net(out_dev), rt, skb, dec))
Robert Shearman27d69102017-01-16 14:16:37 +0000390 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600391 } else {
392 bool bos;
393 int i;
394 skb_push(skb, new_header_size);
395 skb_reset_network_header(skb);
396 /* Push the new labels */
397 hdr = mpls_hdr(skb);
398 bos = dec.bos;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700399 for (i = nh->nh_labels - 1; i >= 0; i--) {
400 hdr[i] = mpls_entry_encode(nh->nh_label[i],
401 dec.ttl, 0, bos);
Eric W. Biederman01891972015-03-03 19:10:47 -0600402 bos = false;
403 }
404 }
405
Robert Shearman27d69102017-01-16 14:16:37 +0000406 mpls_stats_inc_outucastpkts(out_dev, skb);
407
Robert Shearmaneb7809f2015-12-10 19:30:50 +0000408 /* If via wasn't specified then send out using device address */
409 if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
410 err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
411 out_dev->dev_addr, skb);
412 else
413 err = neigh_xmit(nh->nh_via_table, out_dev,
414 mpls_nh_via(rt, nh), skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600415 if (err)
416 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
417 __func__, err);
418 return 0;
419
Robert Shearman27d69102017-01-16 14:16:37 +0000420tx_err:
421 out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
422 if (out_mdev)
423 MPLS_INC_STATS(out_mdev, tx_errors);
424 goto drop;
425err:
426 MPLS_INC_STATS(mdev, rx_errors);
Eric W. Biederman01891972015-03-03 19:10:47 -0600427drop:
428 kfree_skb(skb);
429 return NET_RX_DROP;
430}
431
432static struct packet_type mpls_packet_type __read_mostly = {
433 .type = cpu_to_be16(ETH_P_MPLS_UC),
434 .func = mpls_forward,
435};
436
Wu Fengguangf0126532015-03-05 05:33:54 +0800437static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600438 [RTA_DST] = { .type = NLA_U32 },
439 [RTA_OIF] = { .type = NLA_U32 },
Robert Shearman5b441ac2017-03-10 20:43:24 +0000440 [RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
Eric W. Biederman03c05662015-03-03 19:13:56 -0600441};
442
Eric W. Biedermana2519922015-03-03 19:12:40 -0600443struct mpls_route_config {
Robert Shearman118d5232015-08-06 11:04:56 +0100444 u32 rc_protocol;
445 u32 rc_ifindex;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700446 u8 rc_via_table;
447 u8 rc_via_alen;
Robert Shearman118d5232015-08-06 11:04:56 +0100448 u8 rc_via[MAX_VIA_ALEN];
449 u32 rc_label;
Robert Shearman5b441ac2017-03-10 20:43:24 +0000450 u8 rc_ttl_propagate;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700451 u8 rc_output_labels;
Robert Shearman118d5232015-08-06 11:04:56 +0100452 u32 rc_output_label[MAX_NEW_LABELS];
453 u32 rc_nlflags;
454 enum mpls_payload_type rc_payload_type;
455 struct nl_info rc_nlinfo;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700456 struct rtnexthop *rc_mp;
457 int rc_mp_len;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600458};
459
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000460static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
Eric W. Biederman01891972015-03-03 19:10:47 -0600461{
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000462 u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
Eric W. Biederman01891972015-03-03 19:10:47 -0600463 struct mpls_route *rt;
464
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000465 rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
466 VIA_ALEN_ALIGN) +
467 num_nh * max_alen_aligned,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700468 GFP_KERNEL);
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000469 if (rt) {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700470 rt->rt_nhn = num_nh;
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800471 rt->rt_nhn_alive = num_nh;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000472 rt->rt_max_alen = max_alen_aligned;
473 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700474
Eric W. Biederman01891972015-03-03 19:10:47 -0600475 return rt;
476}
477
478static void mpls_rt_free(struct mpls_route *rt)
479{
480 if (rt)
481 kfree_rcu(rt, rt_rcu);
482}
483
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600484static void mpls_notify_route(struct net *net, unsigned index,
485 struct mpls_route *old, struct mpls_route *new,
486 const struct nl_info *info)
487{
488 struct nlmsghdr *nlh = info ? info->nlh : NULL;
489 unsigned portid = info ? info->portid : 0;
490 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
491 struct mpls_route *rt = new ? new : old;
492 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
493 /* Ignore reserved labels for now */
Robert Shearmana6affd22015-08-03 17:50:04 +0100494 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600495 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
496}
497
Eric W. Biederman01891972015-03-03 19:10:47 -0600498static void mpls_route_update(struct net *net, unsigned index,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700499 struct mpls_route *new,
Eric W. Biederman01891972015-03-03 19:10:47 -0600500 const struct nl_info *info)
501{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600502 struct mpls_route __rcu **platform_label;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700503 struct mpls_route *rt;
Eric W. Biederman01891972015-03-03 19:10:47 -0600504
505 ASSERT_RTNL();
506
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600507 platform_label = rtnl_dereference(net->mpls.platform_label);
508 rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700509 rcu_assign_pointer(platform_label[index], new);
Eric W. Biederman01891972015-03-03 19:10:47 -0600510
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700511 mpls_notify_route(net, index, rt, new, info);
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600512
Eric W. Biederman01891972015-03-03 19:10:47 -0600513 /* If we removed a route free it now */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700514 mpls_rt_free(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -0600515}
516
Eric W. Biedermana2519922015-03-03 19:12:40 -0600517static unsigned find_free_label(struct net *net)
518{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600519 struct mpls_route __rcu **platform_label;
520 size_t platform_labels;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600521 unsigned index;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600522
523 platform_label = rtnl_dereference(net->mpls.platform_label);
524 platform_labels = net->mpls.platform_labels;
Robert Shearmana6affd22015-08-03 17:50:04 +0100525 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
526 index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600527 if (!rtnl_dereference(platform_label[index]))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600528 return index;
529 }
530 return LABEL_NOT_SPECIFIED;
531}
532
Roopa Prabhubf215632015-07-30 13:34:54 -0700533#if IS_ENABLED(CONFIG_INET)
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000534static struct net_device *inet_fib_lookup_dev(struct net *net,
535 const void *addr)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700536{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300537 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700538 struct rtable *rt;
539 struct in_addr daddr;
540
541 memcpy(&daddr, addr, sizeof(struct in_addr));
542 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
543 if (IS_ERR(rt))
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300544 return ERR_CAST(rt);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700545
546 dev = rt->dst.dev;
547 dev_hold(dev);
548
549 ip_rt_put(rt);
550
Roopa Prabhu01faef22015-07-21 09:16:24 -0700551 return dev;
552}
Roopa Prabhubf215632015-07-30 13:34:54 -0700553#else
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000554static struct net_device *inet_fib_lookup_dev(struct net *net,
555 const void *addr)
Roopa Prabhubf215632015-07-30 13:34:54 -0700556{
557 return ERR_PTR(-EAFNOSUPPORT);
558}
559#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700560
Roopa Prabhubf215632015-07-30 13:34:54 -0700561#if IS_ENABLED(CONFIG_IPV6)
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000562static struct net_device *inet6_fib_lookup_dev(struct net *net,
563 const void *addr)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700564{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300565 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700566 struct dst_entry *dst;
567 struct flowi6 fl6;
Roopa Prabhubf215632015-07-30 13:34:54 -0700568 int err;
569
570 if (!ipv6_stub)
571 return ERR_PTR(-EAFNOSUPPORT);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700572
573 memset(&fl6, 0, sizeof(fl6));
574 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
Roopa Prabhubf215632015-07-30 13:34:54 -0700575 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
576 if (err)
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300577 return ERR_PTR(err);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700578
579 dev = dst->dev;
580 dev_hold(dev);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700581 dst_release(dst);
582
583 return dev;
584}
Roopa Prabhubf215632015-07-30 13:34:54 -0700585#else
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000586static struct net_device *inet6_fib_lookup_dev(struct net *net,
587 const void *addr)
Roopa Prabhubf215632015-07-30 13:34:54 -0700588{
589 return ERR_PTR(-EAFNOSUPPORT);
590}
591#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700592
593static struct net_device *find_outdev(struct net *net,
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000594 struct mpls_route *rt,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700595 struct mpls_nh *nh, int oif)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700596{
597 struct net_device *dev = NULL;
598
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700599 if (!oif) {
600 switch (nh->nh_via_table) {
Roopa Prabhu01faef22015-07-21 09:16:24 -0700601 case NEIGH_ARP_TABLE:
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000602 dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
Roopa Prabhu01faef22015-07-21 09:16:24 -0700603 break;
604 case NEIGH_ND_TABLE:
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000605 dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
Roopa Prabhu01faef22015-07-21 09:16:24 -0700606 break;
607 case NEIGH_LINK_TABLE:
608 break;
609 }
610 } else {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700611 dev = dev_get_by_index(net, oif);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700612 }
613
Roopa Prabhu3dcb6152015-08-04 06:36:24 -0700614 if (!dev)
615 return ERR_PTR(-ENODEV);
616
Roopa Prabhu94a57f12016-04-07 21:28:38 -0700617 if (IS_ERR(dev))
618 return dev;
619
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700620 /* The caller is holding rtnl anyways, so release the dev reference */
621 dev_put(dev);
622
Roopa Prabhu01faef22015-07-21 09:16:24 -0700623 return dev;
624}
625
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000626static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
627 struct mpls_nh *nh, int oif)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700628{
629 struct net_device *dev = NULL;
630 int err = -ENODEV;
631
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000632 dev = find_outdev(net, rt, nh, oif);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700633 if (IS_ERR(dev)) {
634 err = PTR_ERR(dev);
635 dev = NULL;
636 goto errout;
637 }
638
639 /* Ensure this is a supported device */
640 err = -EINVAL;
641 if (!mpls_dev_get(dev))
642 goto errout;
643
Robert Shearmana3e948e2015-12-10 19:30:48 +0000644 if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
645 (dev->addr_len != nh->nh_via_alen))
646 goto errout;
647
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700648 RCU_INIT_POINTER(nh->nh_dev, dev);
649
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800650 if (!(dev->flags & IFF_UP)) {
651 nh->nh_flags |= RTNH_F_DEAD;
652 } else {
653 unsigned int flags;
654
655 flags = dev_get_flags(dev);
656 if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
657 nh->nh_flags |= RTNH_F_LINKDOWN;
658 }
659
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700660 return 0;
661
662errout:
663 return err;
664}
665
666static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
667 struct mpls_route *rt)
668{
669 struct net *net = cfg->rc_nlinfo.nl_net;
670 struct mpls_nh *nh = rt->rt_nh;
671 int err;
672 int i;
673
674 if (!nh)
675 return -ENOMEM;
676
677 err = -EINVAL;
678 /* Ensure only a supported number of labels are present */
679 if (cfg->rc_output_labels > MAX_NEW_LABELS)
680 goto errout;
681
682 nh->nh_labels = cfg->rc_output_labels;
683 for (i = 0; i < nh->nh_labels; i++)
684 nh->nh_label[i] = cfg->rc_output_label[i];
685
686 nh->nh_via_table = cfg->rc_via_table;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000687 memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700688 nh->nh_via_alen = cfg->rc_via_alen;
689
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000690 err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700691 if (err)
692 goto errout;
693
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800694 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
695 rt->rt_nhn_alive--;
696
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700697 return 0;
698
699errout:
700 return err;
701}
702
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000703static int mpls_nh_build(struct net *net, struct mpls_route *rt,
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800704 struct mpls_nh *nh, int oif, struct nlattr *via,
705 struct nlattr *newdst)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700706{
707 int err = -ENOMEM;
708
709 if (!nh)
710 goto errout;
711
712 if (newdst) {
713 err = nla_get_labels(newdst, MAX_NEW_LABELS,
714 &nh->nh_labels, nh->nh_label);
715 if (err)
716 goto errout;
717 }
718
Robert Shearmanf20367d2015-12-10 19:30:51 +0000719 if (via) {
720 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
721 __mpls_nh_via(rt, nh));
722 if (err)
723 goto errout;
724 } else {
725 nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
726 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700727
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000728 err = mpls_nh_assign_dev(net, rt, nh, oif);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700729 if (err)
730 goto errout;
731
732 return 0;
733
734errout:
735 return err;
736}
737
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000738static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
739 u8 cfg_via_alen, u8 *max_via_alen)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700740{
741 int nhs = 0;
742 int remaining = len;
743
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000744 if (!rtnh) {
745 *max_via_alen = cfg_via_alen;
746 return 1;
747 }
748
749 *max_via_alen = 0;
750
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700751 while (rtnh_ok(rtnh, remaining)) {
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000752 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
753 int attrlen;
754
755 attrlen = rtnh_attrlen(rtnh);
756 nla = nla_find(attrs, attrlen, RTA_VIA);
757 if (nla && nla_len(nla) >=
758 offsetof(struct rtvia, rtvia_addr)) {
759 int via_alen = nla_len(nla) -
760 offsetof(struct rtvia, rtvia_addr);
761
762 if (via_alen <= MAX_VIA_ALEN)
763 *max_via_alen = max_t(u16, *max_via_alen,
764 via_alen);
765 }
766
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700767 nhs++;
768 rtnh = rtnh_next(rtnh, &remaining);
769 }
770
771 /* leftover implies invalid nexthop configuration, discard it */
772 return remaining > 0 ? 0 : nhs;
773}
774
775static int mpls_nh_build_multi(struct mpls_route_config *cfg,
776 struct mpls_route *rt)
777{
778 struct rtnexthop *rtnh = cfg->rc_mp;
779 struct nlattr *nla_via, *nla_newdst;
780 int remaining = cfg->rc_mp_len;
781 int nhs = 0;
782 int err = 0;
783
784 change_nexthops(rt) {
785 int attrlen;
786
787 nla_via = NULL;
788 nla_newdst = NULL;
789
790 err = -EINVAL;
791 if (!rtnh_ok(rtnh, remaining))
792 goto errout;
793
Robert Shearman1c78efa2015-10-23 06:03:28 -0700794 /* neither weighted multipath nor any flags
795 * are supported
796 */
797 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
798 goto errout;
799
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700800 attrlen = rtnh_attrlen(rtnh);
801 if (attrlen > 0) {
802 struct nlattr *attrs = rtnh_attrs(rtnh);
803
804 nla_via = nla_find(attrs, attrlen, RTA_VIA);
805 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
806 }
807
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000808 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800809 rtnh->rtnh_ifindex, nla_via, nla_newdst);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700810 if (err)
811 goto errout;
812
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800813 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
814 rt->rt_nhn_alive--;
815
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700816 rtnh = rtnh_next(rtnh, &remaining);
817 nhs++;
818 } endfor_nexthops(rt);
819
820 rt->rt_nhn = nhs;
821
822 return 0;
823
824errout:
825 return err;
826}
827
Eric W. Biedermana2519922015-03-03 19:12:40 -0600828static int mpls_route_add(struct mpls_route_config *cfg)
829{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600830 struct mpls_route __rcu **platform_label;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600831 struct net *net = cfg->rc_nlinfo.nl_net;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600832 struct mpls_route *rt, *old;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600833 int err = -EINVAL;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000834 u8 max_via_alen;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700835 unsigned index;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000836 int nhs;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600837
838 index = cfg->rc_label;
839
840 /* If a label was not specified during insert pick one */
841 if ((index == LABEL_NOT_SPECIFIED) &&
842 (cfg->rc_nlflags & NLM_F_CREATE)) {
843 index = find_free_label(net);
844 }
845
Robert Shearmana6affd22015-08-03 17:50:04 +0100846 /* Reserved labels may not be set */
847 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600848 goto errout;
849
850 /* The full 20 bit range may not be supported. */
851 if (index >= net->mpls.platform_labels)
852 goto errout;
853
Eric W. Biedermana2519922015-03-03 19:12:40 -0600854 /* Append makes no sense with mpls */
Eric W. Biederman0f7bbd52015-03-07 16:22:40 -0600855 err = -EOPNOTSUPP;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600856 if (cfg->rc_nlflags & NLM_F_APPEND)
857 goto errout;
858
859 err = -EEXIST;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600860 platform_label = rtnl_dereference(net->mpls.platform_label);
861 old = rtnl_dereference(platform_label[index]);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600862 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
863 goto errout;
864
865 err = -EEXIST;
866 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
867 goto errout;
868
869 err = -ENOENT;
870 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
871 goto errout;
872
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000873 err = -EINVAL;
874 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
875 cfg->rc_via_alen, &max_via_alen);
876 if (nhs == 0)
877 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700878
Eric W. Biedermana2519922015-03-03 19:12:40 -0600879 err = -ENOMEM;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000880 rt = mpls_rt_alloc(nhs, max_via_alen);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600881 if (!rt)
882 goto errout;
883
Eric W. Biedermana2519922015-03-03 19:12:40 -0600884 rt->rt_protocol = cfg->rc_protocol;
Robert Shearman118d5232015-08-06 11:04:56 +0100885 rt->rt_payload_type = cfg->rc_payload_type;
Robert Shearman5b441ac2017-03-10 20:43:24 +0000886 rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600887
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700888 if (cfg->rc_mp)
889 err = mpls_nh_build_multi(cfg, rt);
890 else
891 err = mpls_nh_build_from_cfg(cfg, rt);
892 if (err)
893 goto freert;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600894
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700895 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
896
Eric W. Biedermana2519922015-03-03 19:12:40 -0600897 return 0;
898
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700899freert:
900 mpls_rt_free(rt);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600901errout:
Eric W. Biedermana2519922015-03-03 19:12:40 -0600902 return err;
903}
904
905static int mpls_route_del(struct mpls_route_config *cfg)
906{
907 struct net *net = cfg->rc_nlinfo.nl_net;
908 unsigned index;
909 int err = -EINVAL;
910
911 index = cfg->rc_label;
912
Robert Shearmana6affd22015-08-03 17:50:04 +0100913 /* Reserved labels may not be removed */
914 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600915 goto errout;
916
917 /* The full 20 bit range may not be supported */
918 if (index >= net->mpls.platform_labels)
919 goto errout;
920
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700921 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600922
923 err = 0;
924errout:
925 return err;
926}
927
Robert Shearman27d69102017-01-16 14:16:37 +0000928static void mpls_get_stats(struct mpls_dev *mdev,
929 struct mpls_link_stats *stats)
930{
931 struct mpls_pcpu_stats *p;
932 int i;
933
934 memset(stats, 0, sizeof(*stats));
935
936 for_each_possible_cpu(i) {
937 struct mpls_link_stats local;
938 unsigned int start;
939
940 p = per_cpu_ptr(mdev->stats, i);
941 do {
942 start = u64_stats_fetch_begin(&p->syncp);
943 local = p->stats;
944 } while (u64_stats_fetch_retry(&p->syncp, start));
945
946 stats->rx_packets += local.rx_packets;
947 stats->rx_bytes += local.rx_bytes;
948 stats->tx_packets += local.tx_packets;
949 stats->tx_bytes += local.tx_bytes;
950 stats->rx_errors += local.rx_errors;
951 stats->tx_errors += local.tx_errors;
952 stats->rx_dropped += local.rx_dropped;
953 stats->tx_dropped += local.tx_dropped;
954 stats->rx_noroute += local.rx_noroute;
955 }
956}
957
958static int mpls_fill_stats_af(struct sk_buff *skb,
959 const struct net_device *dev)
960{
961 struct mpls_link_stats *stats;
962 struct mpls_dev *mdev;
963 struct nlattr *nla;
964
965 mdev = mpls_dev_get(dev);
966 if (!mdev)
967 return -ENODATA;
968
969 nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
970 sizeof(struct mpls_link_stats),
971 MPLS_STATS_UNSPEC);
972 if (!nla)
973 return -EMSGSIZE;
974
975 stats = nla_data(nla);
976 mpls_get_stats(mdev, stats);
977
978 return 0;
979}
980
981static size_t mpls_get_stats_af_size(const struct net_device *dev)
982{
983 struct mpls_dev *mdev;
984
985 mdev = mpls_dev_get(dev);
986 if (!mdev)
987 return 0;
988
989 return nla_total_size_64bit(sizeof(struct mpls_link_stats));
990}
991
David Ahern24045a02017-02-20 08:03:30 -0800992static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
993 u32 portid, u32 seq, int event,
994 unsigned int flags, int type)
995{
996 struct nlmsghdr *nlh;
997 struct netconfmsg *ncm;
998 bool all = false;
999
1000 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1001 flags);
1002 if (!nlh)
1003 return -EMSGSIZE;
1004
1005 if (type == NETCONFA_ALL)
1006 all = true;
1007
1008 ncm = nlmsg_data(nlh);
1009 ncm->ncm_family = AF_MPLS;
1010
1011 if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
1012 goto nla_put_failure;
1013
1014 if ((all || type == NETCONFA_INPUT) &&
1015 nla_put_s32(skb, NETCONFA_INPUT,
1016 mdev->input_enabled) < 0)
1017 goto nla_put_failure;
1018
1019 nlmsg_end(skb, nlh);
1020 return 0;
1021
1022nla_put_failure:
1023 nlmsg_cancel(skb, nlh);
1024 return -EMSGSIZE;
1025}
1026
1027static int mpls_netconf_msgsize_devconf(int type)
1028{
1029 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1030 + nla_total_size(4); /* NETCONFA_IFINDEX */
1031 bool all = false;
1032
1033 if (type == NETCONFA_ALL)
1034 all = true;
1035
1036 if (all || type == NETCONFA_INPUT)
1037 size += nla_total_size(4);
1038
1039 return size;
1040}
1041
1042static void mpls_netconf_notify_devconf(struct net *net, int type,
1043 struct mpls_dev *mdev)
1044{
1045 struct sk_buff *skb;
1046 int err = -ENOBUFS;
1047
1048 skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
1049 if (!skb)
1050 goto errout;
1051
1052 err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, RTM_NEWNETCONF,
1053 0, type);
1054 if (err < 0) {
1055 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1056 WARN_ON(err == -EMSGSIZE);
1057 kfree_skb(skb);
1058 goto errout;
1059 }
1060
1061 rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
1062 return;
1063errout:
1064 if (err < 0)
1065 rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
1066}
1067
1068static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
1069 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
1070};
1071
1072static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
1073 struct nlmsghdr *nlh)
1074{
1075 struct net *net = sock_net(in_skb->sk);
1076 struct nlattr *tb[NETCONFA_MAX + 1];
1077 struct netconfmsg *ncm;
1078 struct net_device *dev;
1079 struct mpls_dev *mdev;
1080 struct sk_buff *skb;
1081 int ifindex;
1082 int err;
1083
1084 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
1085 devconf_mpls_policy);
1086 if (err < 0)
1087 goto errout;
1088
1089 err = -EINVAL;
1090 if (!tb[NETCONFA_IFINDEX])
1091 goto errout;
1092
1093 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
1094 dev = __dev_get_by_index(net, ifindex);
1095 if (!dev)
1096 goto errout;
1097
1098 mdev = mpls_dev_get(dev);
1099 if (!mdev)
1100 goto errout;
1101
1102 err = -ENOBUFS;
1103 skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
1104 if (!skb)
1105 goto errout;
1106
1107 err = mpls_netconf_fill_devconf(skb, mdev,
1108 NETLINK_CB(in_skb).portid,
1109 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
1110 NETCONFA_ALL);
1111 if (err < 0) {
1112 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1113 WARN_ON(err == -EMSGSIZE);
1114 kfree_skb(skb);
1115 goto errout;
1116 }
1117 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1118errout:
1119 return err;
1120}
1121
1122static int mpls_netconf_dump_devconf(struct sk_buff *skb,
1123 struct netlink_callback *cb)
1124{
1125 struct net *net = sock_net(skb->sk);
1126 struct hlist_head *head;
1127 struct net_device *dev;
1128 struct mpls_dev *mdev;
1129 int idx, s_idx;
1130 int h, s_h;
1131
1132 s_h = cb->args[0];
1133 s_idx = idx = cb->args[1];
1134
1135 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1136 idx = 0;
1137 head = &net->dev_index_head[h];
1138 rcu_read_lock();
1139 cb->seq = net->dev_base_seq;
1140 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1141 if (idx < s_idx)
1142 goto cont;
1143 mdev = mpls_dev_get(dev);
1144 if (!mdev)
1145 goto cont;
1146 if (mpls_netconf_fill_devconf(skb, mdev,
1147 NETLINK_CB(cb->skb).portid,
1148 cb->nlh->nlmsg_seq,
1149 RTM_NEWNETCONF,
1150 NLM_F_MULTI,
1151 NETCONFA_ALL) < 0) {
1152 rcu_read_unlock();
1153 goto done;
1154 }
1155 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1156cont:
1157 idx++;
1158 }
1159 rcu_read_unlock();
1160 }
1161done:
1162 cb->args[0] = h;
1163 cb->args[1] = idx;
1164
1165 return skb->len;
1166}
1167
Robert Shearman37bde792015-04-22 11:14:38 +01001168#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
1169 (&((struct mpls_dev *)0)->field)
1170
David Ahern24045a02017-02-20 08:03:30 -08001171static int mpls_conf_proc(struct ctl_table *ctl, int write,
1172 void __user *buffer,
1173 size_t *lenp, loff_t *ppos)
1174{
1175 int oval = *(int *)ctl->data;
1176 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1177
1178 if (write) {
1179 struct mpls_dev *mdev = ctl->extra1;
1180 int i = (int *)ctl->data - (int *)mdev;
1181 struct net *net = ctl->extra2;
1182 int val = *(int *)ctl->data;
1183
1184 if (i == offsetof(struct mpls_dev, input_enabled) &&
1185 val != oval) {
1186 mpls_netconf_notify_devconf(net,
1187 NETCONFA_INPUT,
1188 mdev);
1189 }
1190 }
1191
1192 return ret;
1193}
1194
Robert Shearman37bde792015-04-22 11:14:38 +01001195static const struct ctl_table mpls_dev_table[] = {
1196 {
1197 .procname = "input",
1198 .maxlen = sizeof(int),
1199 .mode = 0644,
David Ahern24045a02017-02-20 08:03:30 -08001200 .proc_handler = mpls_conf_proc,
Robert Shearman37bde792015-04-22 11:14:38 +01001201 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
1202 },
1203 { }
1204};
1205
1206static int mpls_dev_sysctl_register(struct net_device *dev,
1207 struct mpls_dev *mdev)
1208{
1209 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
David Ahern24045a02017-02-20 08:03:30 -08001210 struct net *net = dev_net(dev);
Robert Shearman37bde792015-04-22 11:14:38 +01001211 struct ctl_table *table;
1212 int i;
1213
1214 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
1215 if (!table)
1216 goto out;
1217
1218 /* Table data contains only offsets relative to the base of
1219 * the mdev at this point, so make them absolute.
1220 */
David Ahern24045a02017-02-20 08:03:30 -08001221 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) {
Robert Shearman37bde792015-04-22 11:14:38 +01001222 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
David Ahern24045a02017-02-20 08:03:30 -08001223 table[i].extra1 = mdev;
1224 table[i].extra2 = net;
1225 }
Robert Shearman37bde792015-04-22 11:14:38 +01001226
1227 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
1228
1229 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
1230 if (!mdev->sysctl)
1231 goto free;
1232
1233 return 0;
1234
1235free:
1236 kfree(table);
1237out:
1238 return -ENOBUFS;
1239}
1240
1241static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
1242{
1243 struct ctl_table *table;
1244
1245 table = mdev->sysctl->ctl_table_arg;
1246 unregister_net_sysctl_table(mdev->sysctl);
1247 kfree(table);
1248}
1249
Robert Shearman03c57742015-04-22 11:14:37 +01001250static struct mpls_dev *mpls_add_dev(struct net_device *dev)
1251{
1252 struct mpls_dev *mdev;
1253 int err = -ENOMEM;
Robert Shearman27d69102017-01-16 14:16:37 +00001254 int i;
Robert Shearman03c57742015-04-22 11:14:37 +01001255
1256 ASSERT_RTNL();
1257
1258 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1259 if (!mdev)
1260 return ERR_PTR(err);
1261
Robert Shearman27d69102017-01-16 14:16:37 +00001262 mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
1263 if (!mdev->stats)
1264 goto free;
1265
1266 for_each_possible_cpu(i) {
1267 struct mpls_pcpu_stats *mpls_stats;
1268
1269 mpls_stats = per_cpu_ptr(mdev->stats, i);
1270 u64_stats_init(&mpls_stats->syncp);
1271 }
1272
Robert Shearman37bde792015-04-22 11:14:38 +01001273 err = mpls_dev_sysctl_register(dev, mdev);
1274 if (err)
1275 goto free;
1276
David Ahern24045a02017-02-20 08:03:30 -08001277 mdev->dev = dev;
Robert Shearman03c57742015-04-22 11:14:37 +01001278 rcu_assign_pointer(dev->mpls_ptr, mdev);
1279
1280 return mdev;
Robert Shearman37bde792015-04-22 11:14:38 +01001281
1282free:
Robert Shearman27d69102017-01-16 14:16:37 +00001283 free_percpu(mdev->stats);
Robert Shearman37bde792015-04-22 11:14:38 +01001284 kfree(mdev);
1285 return ERR_PTR(err);
Robert Shearman03c57742015-04-22 11:14:37 +01001286}
1287
Robert Shearman27d69102017-01-16 14:16:37 +00001288static void mpls_dev_destroy_rcu(struct rcu_head *head)
1289{
1290 struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
1291
1292 free_percpu(mdev->stats);
1293 kfree(mdev);
1294}
1295
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001296static void mpls_ifdown(struct net_device *dev, int event)
Eric W. Biederman01891972015-03-03 19:10:47 -06001297{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001298 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -06001299 struct net *net = dev_net(dev);
1300 unsigned index;
1301
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001302 platform_label = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06001303 for (index = 0; index < net->mpls.platform_labels; index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001304 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001305
Eric W. Biederman01891972015-03-03 19:10:47 -06001306 if (!rt)
1307 continue;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001308
1309 change_nexthops(rt) {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001310 if (rtnl_dereference(nh->nh_dev) != dev)
1311 continue;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001312 switch (event) {
1313 case NETDEV_DOWN:
1314 case NETDEV_UNREGISTER:
1315 nh->nh_flags |= RTNH_F_DEAD;
1316 /* fall through */
1317 case NETDEV_CHANGE:
1318 nh->nh_flags |= RTNH_F_LINKDOWN;
1319 ACCESS_ONCE(rt->rt_nhn_alive) = rt->rt_nhn_alive - 1;
1320 break;
1321 }
1322 if (event == NETDEV_UNREGISTER)
1323 RCU_INIT_POINTER(nh->nh_dev, NULL);
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001324 } endfor_nexthops(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -06001325 }
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001326}
Robert Shearman37bde792015-04-22 11:14:38 +01001327
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001328static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
1329{
1330 struct mpls_route __rcu **platform_label;
1331 struct net *net = dev_net(dev);
1332 unsigned index;
1333 int alive;
Robert Shearman03c57742015-04-22 11:14:37 +01001334
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001335 platform_label = rtnl_dereference(net->mpls.platform_label);
1336 for (index = 0; index < net->mpls.platform_labels; index++) {
1337 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1338
1339 if (!rt)
1340 continue;
1341
1342 alive = 0;
1343 change_nexthops(rt) {
1344 struct net_device *nh_dev =
1345 rtnl_dereference(nh->nh_dev);
1346
1347 if (!(nh->nh_flags & nh_flags)) {
1348 alive++;
1349 continue;
1350 }
1351 if (nh_dev != dev)
1352 continue;
1353 alive++;
1354 nh->nh_flags &= ~nh_flags;
1355 } endfor_nexthops(rt);
1356
1357 ACCESS_ONCE(rt->rt_nhn_alive) = alive;
1358 }
Eric W. Biederman01891972015-03-03 19:10:47 -06001359}
1360
1361static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
1362 void *ptr)
1363{
1364 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Robert Shearman03c57742015-04-22 11:14:37 +01001365 struct mpls_dev *mdev;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001366 unsigned int flags;
Eric W. Biederman01891972015-03-03 19:10:47 -06001367
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001368 if (event == NETDEV_REGISTER) {
Simon Horman407f31b2016-07-07 07:56:15 +02001369 /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
Simon Horman0d227a82016-06-16 17:09:09 +09001370 if (dev->type == ARPHRD_ETHER ||
1371 dev->type == ARPHRD_LOOPBACK ||
Simon Horman407f31b2016-07-07 07:56:15 +02001372 dev->type == ARPHRD_IPGRE ||
1373 dev->type == ARPHRD_SIT ||
1374 dev->type == ARPHRD_TUNNEL) {
Robert Shearman03c57742015-04-22 11:14:37 +01001375 mdev = mpls_add_dev(dev);
1376 if (IS_ERR(mdev))
1377 return notifier_from_errno(PTR_ERR(mdev));
1378 }
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001379 return NOTIFY_OK;
1380 }
Robert Shearman03c57742015-04-22 11:14:37 +01001381
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001382 mdev = mpls_dev_get(dev);
1383 if (!mdev)
1384 return NOTIFY_OK;
1385
1386 switch (event) {
1387 case NETDEV_DOWN:
1388 mpls_ifdown(dev, event);
1389 break;
1390 case NETDEV_UP:
1391 flags = dev_get_flags(dev);
1392 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1393 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1394 else
1395 mpls_ifup(dev, RTNH_F_DEAD);
1396 break;
1397 case NETDEV_CHANGE:
1398 flags = dev_get_flags(dev);
1399 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1400 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1401 else
1402 mpls_ifdown(dev, event);
1403 break;
Eric W. Biederman01891972015-03-03 19:10:47 -06001404 case NETDEV_UNREGISTER:
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001405 mpls_ifdown(dev, event);
1406 mdev = mpls_dev_get(dev);
1407 if (mdev) {
1408 mpls_dev_sysctl_unregister(mdev);
1409 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
Robert Shearman27d69102017-01-16 14:16:37 +00001410 call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001411 }
Eric W. Biederman01891972015-03-03 19:10:47 -06001412 break;
Robert Shearman0fae3bf2015-06-11 19:58:26 +01001413 case NETDEV_CHANGENAME:
1414 mdev = mpls_dev_get(dev);
1415 if (mdev) {
1416 int err;
1417
1418 mpls_dev_sysctl_unregister(mdev);
1419 err = mpls_dev_sysctl_register(dev, mdev);
1420 if (err)
1421 return notifier_from_errno(err);
1422 }
1423 break;
Eric W. Biederman01891972015-03-03 19:10:47 -06001424 }
1425 return NOTIFY_OK;
1426}
1427
1428static struct notifier_block mpls_dev_notifier = {
1429 .notifier_call = mpls_dev_notify,
1430};
1431
Eric W. Biederman03c05662015-03-03 19:13:56 -06001432static int nla_put_via(struct sk_buff *skb,
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001433 u8 table, const void *addr, int alen)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001434{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001435 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
1436 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
1437 };
Eric W. Biederman03c05662015-03-03 19:13:56 -06001438 struct nlattr *nla;
1439 struct rtvia *via;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001440 int family = AF_UNSPEC;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001441
1442 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1443 if (!nla)
1444 return -EMSGSIZE;
1445
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001446 if (table <= NEIGH_NR_TABLES)
1447 family = table_to_family[table];
1448
Eric W. Biederman03c05662015-03-03 19:13:56 -06001449 via = nla_data(nla);
1450 via->rtvia_family = family;
1451 memcpy(via->rtvia_addr, addr, alen);
1452 return 0;
1453}
1454
Eric W. Biederman966bae32015-03-03 19:13:19 -06001455int nla_put_labels(struct sk_buff *skb, int attrtype,
1456 u8 labels, const u32 label[])
1457{
1458 struct nlattr *nla;
1459 struct mpls_shim_hdr *nla_label;
1460 bool bos;
1461 int i;
1462 nla = nla_reserve(skb, attrtype, labels*4);
1463 if (!nla)
1464 return -EMSGSIZE;
1465
1466 nla_label = nla_data(nla);
1467 bos = true;
1468 for (i = labels - 1; i >= 0; i--) {
1469 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1470 bos = false;
1471 }
1472
1473 return 0;
1474}
Roopa Prabhuface0182015-07-21 10:43:52 +02001475EXPORT_SYMBOL_GPL(nla_put_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001476
1477int nla_get_labels(const struct nlattr *nla,
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001478 u32 max_labels, u8 *labels, u32 label[])
Eric W. Biederman966bae32015-03-03 19:13:19 -06001479{
1480 unsigned len = nla_len(nla);
1481 unsigned nla_labels;
1482 struct mpls_shim_hdr *nla_label;
1483 bool bos;
1484 int i;
1485
1486 /* len needs to be an even multiple of 4 (the label size) */
1487 if (len & 3)
1488 return -EINVAL;
1489
1490 /* Limit the number of new labels allowed */
1491 nla_labels = len/4;
1492 if (nla_labels > max_labels)
1493 return -EINVAL;
1494
1495 nla_label = nla_data(nla);
1496 bos = true;
1497 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1498 struct mpls_entry_decoded dec;
1499 dec = mpls_entry_decode(nla_label + i);
1500
1501 /* Ensure the bottom of stack flag is properly set
1502 * and ttl and tc are both clear.
1503 */
1504 if ((dec.bos != bos) || dec.ttl || dec.tc)
1505 return -EINVAL;
1506
Robert Shearman5a9ab012015-04-22 11:14:39 +01001507 switch (dec.label) {
Tom Herbert78f5b892015-05-07 08:08:51 -07001508 case MPLS_LABEL_IMPLNULL:
Robert Shearman5a9ab012015-04-22 11:14:39 +01001509 /* RFC3032: This is a label that an LSR may
1510 * assign and distribute, but which never
1511 * actually appears in the encapsulation.
1512 */
1513 return -EINVAL;
1514 }
1515
Eric W. Biederman966bae32015-03-03 19:13:19 -06001516 label[i] = dec.label;
1517 }
1518 *labels = nla_labels;
1519 return 0;
1520}
Roopa Prabhuface0182015-07-21 10:43:52 +02001521EXPORT_SYMBOL_GPL(nla_get_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001522
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001523int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1524 u8 *via_table, u8 via_addr[])
1525{
1526 struct rtvia *via = nla_data(nla);
1527 int err = -EINVAL;
1528 int alen;
1529
1530 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1531 goto errout;
1532 alen = nla_len(nla) -
1533 offsetof(struct rtvia, rtvia_addr);
1534 if (alen > MAX_VIA_ALEN)
1535 goto errout;
1536
1537 /* Validate the address family */
1538 switch (via->rtvia_family) {
1539 case AF_PACKET:
1540 *via_table = NEIGH_LINK_TABLE;
1541 break;
1542 case AF_INET:
1543 *via_table = NEIGH_ARP_TABLE;
1544 if (alen != 4)
1545 goto errout;
1546 break;
1547 case AF_INET6:
1548 *via_table = NEIGH_ND_TABLE;
1549 if (alen != 16)
1550 goto errout;
1551 break;
1552 default:
1553 /* Unsupported address family */
1554 goto errout;
1555 }
1556
1557 memcpy(via_addr, via->rtvia_addr, alen);
1558 *via_alen = alen;
1559 err = 0;
1560
1561errout:
1562 return err;
1563}
1564
Eric W. Biederman03c05662015-03-03 19:13:56 -06001565static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1566 struct mpls_route_config *cfg)
1567{
1568 struct rtmsg *rtm;
1569 struct nlattr *tb[RTA_MAX+1];
1570 int index;
1571 int err;
1572
1573 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1574 if (err < 0)
1575 goto errout;
1576
1577 err = -EINVAL;
1578 rtm = nlmsg_data(nlh);
1579 memset(cfg, 0, sizeof(*cfg));
1580
1581 if (rtm->rtm_family != AF_MPLS)
1582 goto errout;
1583 if (rtm->rtm_dst_len != 20)
1584 goto errout;
1585 if (rtm->rtm_src_len != 0)
1586 goto errout;
1587 if (rtm->rtm_tos != 0)
1588 goto errout;
1589 if (rtm->rtm_table != RT_TABLE_MAIN)
1590 goto errout;
1591 /* Any value is acceptable for rtm_protocol */
1592
1593 /* As mpls uses destination specific addresses
1594 * (or source specific address in the case of multicast)
1595 * all addresses have universal scope.
1596 */
1597 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1598 goto errout;
1599 if (rtm->rtm_type != RTN_UNICAST)
1600 goto errout;
1601 if (rtm->rtm_flags != 0)
1602 goto errout;
1603
1604 cfg->rc_label = LABEL_NOT_SPECIFIED;
1605 cfg->rc_protocol = rtm->rtm_protocol;
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001606 cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001607 cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001608 cfg->rc_nlflags = nlh->nlmsg_flags;
1609 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1610 cfg->rc_nlinfo.nlh = nlh;
1611 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1612
1613 for (index = 0; index <= RTA_MAX; index++) {
1614 struct nlattr *nla = tb[index];
1615 if (!nla)
1616 continue;
1617
Suraj Deshmukh14dd3e12016-12-03 07:59:26 +00001618 switch (index) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001619 case RTA_OIF:
1620 cfg->rc_ifindex = nla_get_u32(nla);
1621 break;
1622 case RTA_NEWDST:
1623 if (nla_get_labels(nla, MAX_NEW_LABELS,
1624 &cfg->rc_output_labels,
1625 cfg->rc_output_label))
1626 goto errout;
1627 break;
1628 case RTA_DST:
1629 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001630 u8 label_count;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001631 if (nla_get_labels(nla, 1, &label_count,
1632 &cfg->rc_label))
1633 goto errout;
1634
Robert Shearmana6affd22015-08-03 17:50:04 +01001635 /* Reserved labels may not be set */
1636 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001637 goto errout;
1638
1639 break;
1640 }
1641 case RTA_VIA:
1642 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001643 if (nla_get_via(nla, &cfg->rc_via_alen,
1644 &cfg->rc_via_table, cfg->rc_via))
Robert Shearmanf8d54af2015-03-06 10:47:00 +00001645 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001646 break;
1647 }
1648 case RTA_MULTIPATH:
1649 {
1650 cfg->rc_mp = nla_data(nla);
1651 cfg->rc_mp_len = nla_len(nla);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001652 break;
1653 }
Robert Shearman5b441ac2017-03-10 20:43:24 +00001654 case RTA_TTL_PROPAGATE:
1655 {
1656 u8 ttl_propagate = nla_get_u8(nla);
1657
1658 if (ttl_propagate > 1)
1659 goto errout;
1660 cfg->rc_ttl_propagate = ttl_propagate ?
1661 MPLS_TTL_PROP_ENABLED :
1662 MPLS_TTL_PROP_DISABLED;
1663 break;
1664 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001665 default:
1666 /* Unsupported attribute */
1667 goto errout;
1668 }
1669 }
1670
1671 err = 0;
1672errout:
1673 return err;
1674}
1675
1676static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1677{
1678 struct mpls_route_config cfg;
1679 int err;
1680
1681 err = rtm_to_route_config(skb, nlh, &cfg);
1682 if (err < 0)
1683 return err;
1684
1685 return mpls_route_del(&cfg);
1686}
1687
1688
1689static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1690{
1691 struct mpls_route_config cfg;
1692 int err;
1693
1694 err = rtm_to_route_config(skb, nlh, &cfg);
1695 if (err < 0)
1696 return err;
1697
1698 return mpls_route_add(&cfg);
1699}
1700
1701static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1702 u32 label, struct mpls_route *rt, int flags)
1703{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001704 struct net_device *dev;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001705 struct nlmsghdr *nlh;
1706 struct rtmsg *rtm;
1707
1708 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1709 if (nlh == NULL)
1710 return -EMSGSIZE;
1711
1712 rtm = nlmsg_data(nlh);
1713 rtm->rtm_family = AF_MPLS;
1714 rtm->rtm_dst_len = 20;
1715 rtm->rtm_src_len = 0;
1716 rtm->rtm_tos = 0;
1717 rtm->rtm_table = RT_TABLE_MAIN;
1718 rtm->rtm_protocol = rt->rt_protocol;
1719 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1720 rtm->rtm_type = RTN_UNICAST;
1721 rtm->rtm_flags = 0;
1722
Eric W. Biederman03c05662015-03-03 19:13:56 -06001723 if (nla_put_labels(skb, RTA_DST, 1, &label))
1724 goto nla_put_failure;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001725
1726 if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
1727 bool ttl_propagate =
1728 rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
1729
1730 if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
1731 ttl_propagate))
1732 goto nla_put_failure;
1733 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001734 if (rt->rt_nhn == 1) {
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001735 const struct mpls_nh *nh = rt->rt_nh;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001736
1737 if (nh->nh_labels &&
1738 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1739 nh->nh_label))
1740 goto nla_put_failure;
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001741 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
Robert Shearman72dcac92015-12-10 19:30:49 +00001742 nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001743 nh->nh_via_alen))
1744 goto nla_put_failure;
1745 dev = rtnl_dereference(nh->nh_dev);
1746 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1747 goto nla_put_failure;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001748 if (nh->nh_flags & RTNH_F_LINKDOWN)
1749 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1750 if (nh->nh_flags & RTNH_F_DEAD)
1751 rtm->rtm_flags |= RTNH_F_DEAD;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001752 } else {
1753 struct rtnexthop *rtnh;
1754 struct nlattr *mp;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001755 int dead = 0;
1756 int linkdown = 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001757
1758 mp = nla_nest_start(skb, RTA_MULTIPATH);
1759 if (!mp)
1760 goto nla_put_failure;
1761
1762 for_nexthops(rt) {
1763 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1764 if (!rtnh)
1765 goto nla_put_failure;
1766
1767 dev = rtnl_dereference(nh->nh_dev);
1768 if (dev)
1769 rtnh->rtnh_ifindex = dev->ifindex;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001770 if (nh->nh_flags & RTNH_F_LINKDOWN) {
1771 rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
1772 linkdown++;
1773 }
1774 if (nh->nh_flags & RTNH_F_DEAD) {
1775 rtnh->rtnh_flags |= RTNH_F_DEAD;
1776 dead++;
1777 }
1778
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001779 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1780 nh->nh_labels,
1781 nh->nh_label))
1782 goto nla_put_failure;
Robert Shearmanf20367d2015-12-10 19:30:51 +00001783 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1784 nla_put_via(skb, nh->nh_via_table,
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001785 mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001786 nh->nh_via_alen))
1787 goto nla_put_failure;
1788
1789 /* length of rtnetlink header + attributes */
1790 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1791 } endfor_nexthops(rt);
1792
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001793 if (linkdown == rt->rt_nhn)
1794 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1795 if (dead == rt->rt_nhn)
1796 rtm->rtm_flags |= RTNH_F_DEAD;
1797
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001798 nla_nest_end(skb, mp);
1799 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001800
1801 nlmsg_end(skb, nlh);
1802 return 0;
1803
1804nla_put_failure:
1805 nlmsg_cancel(skb, nlh);
1806 return -EMSGSIZE;
1807}
1808
1809static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1810{
1811 struct net *net = sock_net(skb->sk);
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001812 struct mpls_route __rcu **platform_label;
1813 size_t platform_labels;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001814 unsigned int index;
1815
1816 ASSERT_RTNL();
1817
1818 index = cb->args[0];
Robert Shearmana6affd22015-08-03 17:50:04 +01001819 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1820 index = MPLS_LABEL_FIRST_UNRESERVED;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001821
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001822 platform_label = rtnl_dereference(net->mpls.platform_label);
1823 platform_labels = net->mpls.platform_labels;
1824 for (; index < platform_labels; index++) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001825 struct mpls_route *rt;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001826 rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001827 if (!rt)
1828 continue;
1829
1830 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1831 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1832 index, rt, NLM_F_MULTI) < 0)
1833 break;
1834 }
1835 cb->args[0] = index;
1836
1837 return skb->len;
1838}
1839
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001840static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1841{
1842 size_t payload =
1843 NLMSG_ALIGN(sizeof(struct rtmsg))
Robert Shearman5b441ac2017-03-10 20:43:24 +00001844 + nla_total_size(4) /* RTA_DST */
1845 + nla_total_size(1); /* RTA_TTL_PROPAGATE */
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001846
1847 if (rt->rt_nhn == 1) {
1848 struct mpls_nh *nh = rt->rt_nh;
1849
1850 if (nh->nh_dev)
1851 payload += nla_total_size(4); /* RTA_OIF */
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001852 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
Robert Shearman72dcac92015-12-10 19:30:49 +00001853 payload += nla_total_size(2 + nh->nh_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001854 if (nh->nh_labels) /* RTA_NEWDST */
1855 payload += nla_total_size(nh->nh_labels * 4);
1856 } else {
1857 /* each nexthop is packed in an attribute */
1858 size_t nhsize = 0;
1859
1860 for_nexthops(rt) {
1861 nhsize += nla_total_size(sizeof(struct rtnexthop));
Robert Shearmanf20367d2015-12-10 19:30:51 +00001862 /* RTA_VIA */
1863 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
1864 nhsize += nla_total_size(2 + nh->nh_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001865 if (nh->nh_labels)
1866 nhsize += nla_total_size(nh->nh_labels * 4);
1867 } endfor_nexthops(rt);
1868 /* nested attribute */
1869 payload += nla_total_size(nhsize);
1870 }
1871
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001872 return payload;
1873}
1874
1875static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1876 struct nlmsghdr *nlh, struct net *net, u32 portid,
1877 unsigned int nlm_flags)
1878{
1879 struct sk_buff *skb;
1880 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1881 int err = -ENOBUFS;
1882
1883 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1884 if (skb == NULL)
1885 goto errout;
1886
1887 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1888 if (err < 0) {
1889 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1890 WARN_ON(err == -EMSGSIZE);
1891 kfree_skb(skb);
1892 goto errout;
1893 }
1894 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1895
1896 return;
1897errout:
1898 if (err < 0)
1899 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1900}
1901
Eric W. Biederman7720c012015-03-03 19:11:20 -06001902static int resize_platform_label_table(struct net *net, size_t limit)
1903{
1904 size_t size = sizeof(struct mpls_route *) * limit;
1905 size_t old_limit;
1906 size_t cp_size;
1907 struct mpls_route __rcu **labels = NULL, **old;
1908 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1909 unsigned index;
1910
1911 if (size) {
1912 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1913 if (!labels)
1914 labels = vzalloc(size);
1915
1916 if (!labels)
1917 goto nolabels;
1918 }
1919
1920 /* In case the predefined labels need to be populated */
Tom Herbert78f5b892015-05-07 08:08:51 -07001921 if (limit > MPLS_LABEL_IPV4NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001922 struct net_device *lo = net->loopback_dev;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001923 rt0 = mpls_rt_alloc(1, lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001924 if (!rt0)
1925 goto nort0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001926 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001927 rt0->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001928 rt0->rt_payload_type = MPT_IPV4;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001929 rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001930 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00001931 rt0->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001932 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
1933 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001934 }
Tom Herbert78f5b892015-05-07 08:08:51 -07001935 if (limit > MPLS_LABEL_IPV6NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001936 struct net_device *lo = net->loopback_dev;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001937 rt2 = mpls_rt_alloc(1, lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001938 if (!rt2)
1939 goto nort2;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001940 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001941 rt2->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001942 rt2->rt_payload_type = MPT_IPV6;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001943 rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001944 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00001945 rt2->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001946 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
1947 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001948 }
1949
1950 rtnl_lock();
1951 /* Remember the original table */
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001952 old = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001953 old_limit = net->mpls.platform_labels;
1954
1955 /* Free any labels beyond the new table */
1956 for (index = limit; index < old_limit; index++)
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001957 mpls_route_update(net, index, NULL, NULL);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001958
1959 /* Copy over the old labels */
1960 cp_size = size;
1961 if (old_limit < limit)
1962 cp_size = old_limit * sizeof(struct mpls_route *);
1963
1964 memcpy(labels, old, cp_size);
1965
1966 /* If needed set the predefined labels */
Tom Herbert78f5b892015-05-07 08:08:51 -07001967 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1968 (limit > MPLS_LABEL_IPV6NULL)) {
1969 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001970 rt2 = NULL;
1971 }
1972
Tom Herbert78f5b892015-05-07 08:08:51 -07001973 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1974 (limit > MPLS_LABEL_IPV4NULL)) {
1975 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001976 rt0 = NULL;
1977 }
1978
1979 /* Update the global pointers */
1980 net->mpls.platform_labels = limit;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001981 rcu_assign_pointer(net->mpls.platform_label, labels);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001982
1983 rtnl_unlock();
1984
1985 mpls_rt_free(rt2);
1986 mpls_rt_free(rt0);
1987
1988 if (old) {
1989 synchronize_rcu();
1990 kvfree(old);
1991 }
1992 return 0;
1993
1994nort2:
1995 mpls_rt_free(rt0);
1996nort0:
1997 kvfree(labels);
1998nolabels:
1999 return -ENOMEM;
2000}
2001
2002static int mpls_platform_labels(struct ctl_table *table, int write,
2003 void __user *buffer, size_t *lenp, loff_t *ppos)
2004{
2005 struct net *net = table->data;
2006 int platform_labels = net->mpls.platform_labels;
2007 int ret;
2008 struct ctl_table tmp = {
2009 .procname = table->procname,
2010 .data = &platform_labels,
2011 .maxlen = sizeof(int),
2012 .mode = table->mode,
2013 .extra1 = &zero,
2014 .extra2 = &label_limit,
2015 };
2016
2017 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2018
2019 if (write && ret == 0)
2020 ret = resize_platform_label_table(net, platform_labels);
2021
2022 return ret;
2023}
2024
Robert Shearman5b441ac2017-03-10 20:43:24 +00002025#define MPLS_NS_SYSCTL_OFFSET(field) \
2026 (&((struct net *)0)->field)
2027
Robert Shearman37bde792015-04-22 11:14:38 +01002028static const struct ctl_table mpls_table[] = {
Eric W. Biederman7720c012015-03-03 19:11:20 -06002029 {
2030 .procname = "platform_labels",
2031 .data = NULL,
2032 .maxlen = sizeof(int),
2033 .mode = 0644,
2034 .proc_handler = mpls_platform_labels,
2035 },
Robert Shearman5b441ac2017-03-10 20:43:24 +00002036 {
2037 .procname = "ip_ttl_propagate",
2038 .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
2039 .maxlen = sizeof(int),
2040 .mode = 0644,
2041 .proc_handler = proc_dointvec_minmax,
2042 .extra1 = &zero,
2043 .extra2 = &one,
2044 },
Eric W. Biederman7720c012015-03-03 19:11:20 -06002045 { }
2046};
2047
Eric W. Biederman01891972015-03-03 19:10:47 -06002048static int mpls_net_init(struct net *net)
2049{
Eric W. Biederman7720c012015-03-03 19:11:20 -06002050 struct ctl_table *table;
Robert Shearman5b441ac2017-03-10 20:43:24 +00002051 int i;
Eric W. Biederman7720c012015-03-03 19:11:20 -06002052
Eric W. Biederman01891972015-03-03 19:10:47 -06002053 net->mpls.platform_labels = 0;
2054 net->mpls.platform_label = NULL;
Robert Shearman5b441ac2017-03-10 20:43:24 +00002055 net->mpls.ip_ttl_propagate = 1;
Eric W. Biederman01891972015-03-03 19:10:47 -06002056
Eric W. Biederman7720c012015-03-03 19:11:20 -06002057 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
2058 if (table == NULL)
2059 return -ENOMEM;
2060
Robert Shearman5b441ac2017-03-10 20:43:24 +00002061 /* Table data contains only offsets relative to the base of
2062 * the mdev at this point, so make them absolute.
2063 */
2064 for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++)
2065 table[i].data = (char *)net + (uintptr_t)table[i].data;
2066
Eric W. Biederman7720c012015-03-03 19:11:20 -06002067 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07002068 if (net->mpls.ctl == NULL) {
2069 kfree(table);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002070 return -ENOMEM;
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07002071 }
Eric W. Biederman7720c012015-03-03 19:11:20 -06002072
Eric W. Biederman01891972015-03-03 19:10:47 -06002073 return 0;
2074}
2075
2076static void mpls_net_exit(struct net *net)
2077{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002078 struct mpls_route __rcu **platform_label;
2079 size_t platform_labels;
Eric W. Biederman7720c012015-03-03 19:11:20 -06002080 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -06002081 unsigned int index;
2082
Eric W. Biederman7720c012015-03-03 19:11:20 -06002083 table = net->mpls.ctl->ctl_table_arg;
2084 unregister_net_sysctl_table(net->mpls.ctl);
2085 kfree(table);
2086
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002087 /* An rcu grace period has passed since there was a device in
2088 * the network namespace (and thus the last in flight packet)
Eric W. Biederman01891972015-03-03 19:10:47 -06002089 * left this network namespace. This is because
2090 * unregister_netdevice_many and netdev_run_todo has completed
2091 * for each network device that was in this network namespace.
2092 *
2093 * As such no additional rcu synchronization is necessary when
2094 * freeing the platform_label table.
2095 */
2096 rtnl_lock();
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002097 platform_label = rtnl_dereference(net->mpls.platform_label);
2098 platform_labels = net->mpls.platform_labels;
2099 for (index = 0; index < platform_labels; index++) {
2100 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
2101 RCU_INIT_POINTER(platform_label[index], NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06002102 mpls_rt_free(rt);
2103 }
2104 rtnl_unlock();
2105
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002106 kvfree(platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06002107}
2108
2109static struct pernet_operations mpls_net_ops = {
2110 .init = mpls_net_init,
2111 .exit = mpls_net_exit,
2112};
2113
Robert Shearman27d69102017-01-16 14:16:37 +00002114static struct rtnl_af_ops mpls_af_ops __read_mostly = {
2115 .family = AF_MPLS,
2116 .fill_stats_af = mpls_fill_stats_af,
2117 .get_stats_af_size = mpls_get_stats_af_size,
2118};
2119
Eric W. Biederman01891972015-03-03 19:10:47 -06002120static int __init mpls_init(void)
2121{
2122 int err;
2123
2124 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
2125
2126 err = register_pernet_subsys(&mpls_net_ops);
2127 if (err)
2128 goto out;
2129
2130 err = register_netdevice_notifier(&mpls_dev_notifier);
2131 if (err)
2132 goto out_unregister_pernet;
2133
2134 dev_add_pack(&mpls_packet_type);
2135
Robert Shearman27d69102017-01-16 14:16:37 +00002136 rtnl_af_register(&mpls_af_ops);
2137
Eric W. Biederman03c05662015-03-03 19:13:56 -06002138 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
2139 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
2140 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
David Ahern24045a02017-02-20 08:03:30 -08002141 rtnl_register(PF_MPLS, RTM_GETNETCONF, mpls_netconf_get_devconf,
2142 mpls_netconf_dump_devconf, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06002143 err = 0;
2144out:
2145 return err;
2146
2147out_unregister_pernet:
2148 unregister_pernet_subsys(&mpls_net_ops);
2149 goto out;
2150}
2151module_init(mpls_init);
2152
2153static void __exit mpls_exit(void)
2154{
Eric W. Biederman03c05662015-03-03 19:13:56 -06002155 rtnl_unregister_all(PF_MPLS);
Robert Shearman27d69102017-01-16 14:16:37 +00002156 rtnl_af_unregister(&mpls_af_ops);
Eric W. Biederman01891972015-03-03 19:10:47 -06002157 dev_remove_pack(&mpls_packet_type);
2158 unregister_netdevice_notifier(&mpls_dev_notifier);
2159 unregister_pernet_subsys(&mpls_net_ops);
2160}
2161module_exit(mpls_exit);
2162
2163MODULE_DESCRIPTION("MultiProtocol Label Switching");
2164MODULE_LICENSE("GPL v2");
2165MODULE_ALIAS_NETPROTO(PF_MPLS);