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