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