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