1 #include <linux/types.h>
2 #include <linux/skbuff.h>
3 #include <linux/socket.h>
4 #include <linux/sysctl.h>
6 #include <linux/module.h>
7 #include <linux/if_arp.h>
8 #include <linux/ipv6.h>
9 #include <linux/mpls.h>
10 #include <linux/netconf.h>
11 #include <linux/vmalloc.h>
12 #include <linux/percpu.h>
17 #include <net/ip_fib.h>
18 #include <net/netevent.h>
19 #include <net/netns/generic.h>
20 #if IS_ENABLED(CONFIG_IPV6)
23 #include <net/addrconf.h>
24 #include <net/nexthop.h>
27 /* max memory we will use for mpls_route */
28 #define MAX_MPLS_ROUTE_MEM 4096
30 /* Maximum number of labels to look ahead at when selecting a path of
33 #define MAX_MP_SELECT_LABELS 4
35 #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
39 static int label_limit
= (1 << 20) - 1;
40 static int ttl_max
= 255;
42 static 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
);
46 static struct mpls_route
*mpls_route_input_rcu(struct net
*net
, unsigned index
)
48 struct mpls_route
*rt
= NULL
;
50 if (index
< net
->mpls
.platform_labels
) {
51 struct mpls_route __rcu
**platform_label
=
52 rcu_dereference(net
->mpls
.platform_label
);
53 rt
= rcu_dereference(platform_label
[index
]);
58 bool mpls_output_possible(const struct net_device
*dev
)
60 return dev
&& (dev
->flags
& IFF_UP
) && netif_carrier_ok(dev
);
62 EXPORT_SYMBOL_GPL(mpls_output_possible
);
64 static u8
*__mpls_nh_via(struct mpls_route
*rt
, struct mpls_nh
*nh
)
66 return (u8
*)nh
+ rt
->rt_via_offset
;
69 static const u8
*mpls_nh_via(const struct mpls_route
*rt
,
70 const struct mpls_nh
*nh
)
72 return __mpls_nh_via((struct mpls_route
*)rt
, (struct mpls_nh
*)nh
);
75 static unsigned int mpls_nh_header_size(const struct mpls_nh
*nh
)
77 /* The size of the layer 2.5 labels to be added for this route */
78 return nh
->nh_labels
* sizeof(struct mpls_shim_hdr
);
81 unsigned int mpls_dev_mtu(const struct net_device
*dev
)
83 /* The amount of data the layer 2 frame can hold */
86 EXPORT_SYMBOL_GPL(mpls_dev_mtu
);
88 bool mpls_pkt_too_big(const struct sk_buff
*skb
, unsigned int mtu
)
93 if (skb_is_gso(skb
) && skb_gso_validate_mtu(skb
, mtu
))
98 EXPORT_SYMBOL_GPL(mpls_pkt_too_big
);
100 void mpls_stats_inc_outucastpkts(struct net_device
*dev
,
101 const struct sk_buff
*skb
)
103 struct mpls_dev
*mdev
;
105 if (skb
->protocol
== htons(ETH_P_MPLS_UC
)) {
106 mdev
= mpls_dev_get(dev
);
108 MPLS_INC_STATS_LEN(mdev
, skb
->len
,
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
);
118 IP6_UPD_PO_STATS(dev_net(dev
), in6dev
,
119 IPSTATS_MIB_OUT
, skb
->len
);
123 EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts
);
125 static u32
mpls_multipath_hash(struct mpls_route
*rt
, struct sk_buff
*skb
)
127 struct mpls_entry_decoded dec
;
128 unsigned int mpls_hdr_len
= 0;
129 struct mpls_shim_hdr
*hdr
;
130 bool eli_seen
= false;
134 for (label_index
= 0; label_index
< MAX_MP_SELECT_LABELS
;
136 mpls_hdr_len
+= sizeof(*hdr
);
137 if (!pskb_may_pull(skb
, mpls_hdr_len
))
140 /* Read and decode the current label */
141 hdr
= mpls_hdr(skb
) + label_index
;
142 dec
= mpls_entry_decode(hdr
);
144 /* RFC6790 - reserved labels MUST NOT be used as keys
145 * for the load-balancing function
147 if (likely(dec
.label
>= MPLS_LABEL_FIRST_UNRESERVED
)) {
148 hash
= jhash_1word(dec
.label
, hash
);
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
158 } else if (dec
.label
== MPLS_LABEL_ENTROPY
) {
165 /* found bottom label; does skb have room for a header? */
166 if (pskb_may_pull(skb
, mpls_hdr_len
+ sizeof(struct iphdr
))) {
167 const struct iphdr
*v4hdr
;
169 v4hdr
= (const struct iphdr
*)(hdr
+ 1);
170 if (v4hdr
->version
== 4) {
171 hash
= jhash_3words(ntohl(v4hdr
->saddr
),
173 v4hdr
->protocol
, hash
);
174 } else if (v4hdr
->version
== 6 &&
175 pskb_may_pull(skb
, mpls_hdr_len
+
176 sizeof(struct ipv6hdr
))) {
177 const struct ipv6hdr
*v6hdr
;
179 v6hdr
= (const struct ipv6hdr
*)(hdr
+ 1);
180 hash
= __ipv6_addr_jhash(&v6hdr
->saddr
, hash
);
181 hash
= __ipv6_addr_jhash(&v6hdr
->daddr
, hash
);
182 hash
= jhash_1word(v6hdr
->nexthdr
, hash
);
192 static struct mpls_nh
*mpls_get_nexthop(struct mpls_route
*rt
, u8 index
)
194 return (struct mpls_nh
*)((u8
*)rt
->rt_nh
+ index
* rt
->rt_nh_size
);
197 /* number of alive nexthops (rt->rt_nhn_alive) and the flags for
198 * a next hop (nh->nh_flags) are modified by netdev event handlers.
199 * Since those fields can change at any moment, use READ_ONCE to
202 static struct mpls_nh
*mpls_select_multipath(struct mpls_route
*rt
,
210 /* No need to look further into packet if there's only
216 alive
= READ_ONCE(rt
->rt_nhn_alive
);
220 hash
= mpls_multipath_hash(rt
, skb
);
221 nh_index
= hash
% alive
;
222 if (alive
== rt
->rt_nhn
)
225 unsigned int nh_flags
= READ_ONCE(nh
->nh_flags
);
227 if (nh_flags
& (RTNH_F_DEAD
| RTNH_F_LINKDOWN
))
232 } endfor_nexthops(rt
);
235 return mpls_get_nexthop(rt
, nh_index
);
238 static bool mpls_egress(struct net
*net
, struct mpls_route
*rt
,
239 struct sk_buff
*skb
, struct mpls_entry_decoded dec
)
241 enum mpls_payload_type payload_type
;
242 bool success
= false;
244 /* The IPv4 code below accesses through the IPv4 header
245 * checksum, which is 12 bytes into the packet.
246 * The IPv6 code below accesses through the IPv6 hop limit
247 * which is 8 bytes into the packet.
249 * For all supported cases there should always be at least 12
250 * bytes of packet data present. The IPv4 header is 20 bytes
251 * without options and the IPv6 header is always 40 bytes
254 if (!pskb_may_pull(skb
, 12))
257 payload_type
= rt
->rt_payload_type
;
258 if (payload_type
== MPT_UNSPEC
)
259 payload_type
= ip_hdr(skb
)->version
;
261 switch (payload_type
) {
263 struct iphdr
*hdr4
= ip_hdr(skb
);
265 skb
->protocol
= htons(ETH_P_IP
);
267 /* If propagating TTL, take the decremented TTL from
268 * the incoming MPLS header, otherwise decrement the
269 * TTL, but only if not 0 to avoid underflow.
271 if (rt
->rt_ttl_propagate
== MPLS_TTL_PROP_ENABLED
||
272 (rt
->rt_ttl_propagate
== MPLS_TTL_PROP_DEFAULT
&&
273 net
->mpls
.ip_ttl_propagate
))
276 new_ttl
= hdr4
->ttl
? hdr4
->ttl
- 1 : 0;
278 csum_replace2(&hdr4
->check
,
279 htons(hdr4
->ttl
<< 8),
280 htons(new_ttl
<< 8));
286 struct ipv6hdr
*hdr6
= ipv6_hdr(skb
);
287 skb
->protocol
= htons(ETH_P_IPV6
);
289 /* If propagating TTL, take the decremented TTL from
290 * the incoming MPLS header, otherwise decrement the
291 * hop limit, but only if not 0 to avoid underflow.
293 if (rt
->rt_ttl_propagate
== MPLS_TTL_PROP_ENABLED
||
294 (rt
->rt_ttl_propagate
== MPLS_TTL_PROP_DEFAULT
&&
295 net
->mpls
.ip_ttl_propagate
))
296 hdr6
->hop_limit
= dec
.ttl
;
297 else if (hdr6
->hop_limit
)
298 hdr6
->hop_limit
= hdr6
->hop_limit
- 1;
303 /* Should have decided which protocol it is by now */
310 static int mpls_forward(struct sk_buff
*skb
, struct net_device
*dev
,
311 struct packet_type
*pt
, struct net_device
*orig_dev
)
313 struct net
*net
= dev_net(dev
);
314 struct mpls_shim_hdr
*hdr
;
315 struct mpls_route
*rt
;
317 struct mpls_entry_decoded dec
;
318 struct net_device
*out_dev
;
319 struct mpls_dev
*out_mdev
;
320 struct mpls_dev
*mdev
;
322 unsigned int new_header_size
;
326 /* Careful this entire function runs inside of an rcu critical section */
328 mdev
= mpls_dev_get(dev
);
332 MPLS_INC_STATS_LEN(mdev
, skb
->len
, rx_packets
,
335 if (!mdev
->input_enabled
) {
336 MPLS_INC_STATS(mdev
, rx_dropped
);
340 if (skb
->pkt_type
!= PACKET_HOST
)
343 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
346 if (!pskb_may_pull(skb
, sizeof(*hdr
)))
349 /* Read and decode the label */
351 dec
= mpls_entry_decode(hdr
);
353 rt
= mpls_route_input_rcu(net
, dec
.label
);
355 MPLS_INC_STATS(mdev
, rx_noroute
);
359 nh
= mpls_select_multipath(rt
, skb
);
364 skb_pull(skb
, sizeof(*hdr
));
365 skb_reset_network_header(skb
);
369 if (skb_warn_if_lro(skb
))
372 skb_forward_csum(skb
);
374 /* Verify ttl is valid */
379 /* Find the output device */
380 out_dev
= rcu_dereference(nh
->nh_dev
);
381 if (!mpls_output_possible(out_dev
))
384 /* Verify the destination can hold the packet */
385 new_header_size
= mpls_nh_header_size(nh
);
386 mtu
= mpls_dev_mtu(out_dev
);
387 if (mpls_pkt_too_big(skb
, mtu
- new_header_size
))
390 hh_len
= LL_RESERVED_SPACE(out_dev
);
391 if (!out_dev
->header_ops
)
394 /* Ensure there is enough space for the headers in the skb */
395 if (skb_cow(skb
, hh_len
+ new_header_size
))
399 skb
->protocol
= htons(ETH_P_MPLS_UC
);
401 if (unlikely(!new_header_size
&& dec
.bos
)) {
402 /* Penultimate hop popping */
403 if (!mpls_egress(dev_net(out_dev
), rt
, skb
, dec
))
408 skb_push(skb
, new_header_size
);
409 skb_reset_network_header(skb
);
410 /* Push the new labels */
413 for (i
= nh
->nh_labels
- 1; i
>= 0; i
--) {
414 hdr
[i
] = mpls_entry_encode(nh
->nh_label
[i
],
420 mpls_stats_inc_outucastpkts(out_dev
, skb
);
422 /* If via wasn't specified then send out using device address */
423 if (nh
->nh_via_table
== MPLS_NEIGH_TABLE_UNSPEC
)
424 err
= neigh_xmit(NEIGH_LINK_TABLE
, out_dev
,
425 out_dev
->dev_addr
, skb
);
427 err
= neigh_xmit(nh
->nh_via_table
, out_dev
,
428 mpls_nh_via(rt
, nh
), skb
);
430 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
435 out_mdev
= out_dev
? mpls_dev_get(out_dev
) : NULL
;
437 MPLS_INC_STATS(out_mdev
, tx_errors
);
440 MPLS_INC_STATS(mdev
, rx_errors
);
446 static struct packet_type mpls_packet_type __read_mostly
= {
447 .type
= cpu_to_be16(ETH_P_MPLS_UC
),
448 .func
= mpls_forward
,
451 static const struct nla_policy rtm_mpls_policy
[RTA_MAX
+1] = {
452 [RTA_DST
] = { .type
= NLA_U32
},
453 [RTA_OIF
] = { .type
= NLA_U32
},
454 [RTA_TTL_PROPAGATE
] = { .type
= NLA_U8
},
457 struct mpls_route_config
{
462 u8 rc_via
[MAX_VIA_ALEN
];
466 u32 rc_output_label
[MAX_NEW_LABELS
];
468 enum mpls_payload_type rc_payload_type
;
469 struct nl_info rc_nlinfo
;
470 struct rtnexthop
*rc_mp
;
474 /* all nexthops within a route have the same size based on max
475 * number of labels and max via length for a hop
477 static struct mpls_route
*mpls_rt_alloc(u8 num_nh
, u8 max_alen
, u8 max_labels
)
479 u8 nh_size
= MPLS_NH_SIZE(max_labels
, max_alen
);
480 struct mpls_route
*rt
;
483 size
= sizeof(*rt
) + num_nh
* nh_size
;
484 if (size
> MAX_MPLS_ROUTE_MEM
)
485 return ERR_PTR(-EINVAL
);
487 rt
= kzalloc(size
, GFP_KERNEL
);
489 return ERR_PTR(-ENOMEM
);
492 rt
->rt_nhn_alive
= num_nh
;
493 rt
->rt_nh_size
= nh_size
;
494 rt
->rt_via_offset
= MPLS_NH_VIA_OFF(max_labels
);
499 static void mpls_rt_free(struct mpls_route
*rt
)
502 kfree_rcu(rt
, rt_rcu
);
505 static void mpls_notify_route(struct net
*net
, unsigned index
,
506 struct mpls_route
*old
, struct mpls_route
*new,
507 const struct nl_info
*info
)
509 struct nlmsghdr
*nlh
= info
? info
->nlh
: NULL
;
510 unsigned portid
= info
? info
->portid
: 0;
511 int event
= new ? RTM_NEWROUTE
: RTM_DELROUTE
;
512 struct mpls_route
*rt
= new ? new : old
;
513 unsigned nlm_flags
= (old
&& new) ? NLM_F_REPLACE
: 0;
514 /* Ignore reserved labels for now */
515 if (rt
&& (index
>= MPLS_LABEL_FIRST_UNRESERVED
))
516 rtmsg_lfib(event
, index
, rt
, nlh
, net
, portid
, nlm_flags
);
519 static void mpls_route_update(struct net
*net
, unsigned index
,
520 struct mpls_route
*new,
521 const struct nl_info
*info
)
523 struct mpls_route __rcu
**platform_label
;
524 struct mpls_route
*rt
;
528 platform_label
= rtnl_dereference(net
->mpls
.platform_label
);
529 rt
= rtnl_dereference(platform_label
[index
]);
530 rcu_assign_pointer(platform_label
[index
], new);
532 mpls_notify_route(net
, index
, rt
, new, info
);
534 /* If we removed a route free it now */
538 static unsigned find_free_label(struct net
*net
)
540 struct mpls_route __rcu
**platform_label
;
541 size_t platform_labels
;
544 platform_label
= rtnl_dereference(net
->mpls
.platform_label
);
545 platform_labels
= net
->mpls
.platform_labels
;
546 for (index
= MPLS_LABEL_FIRST_UNRESERVED
; index
< platform_labels
;
548 if (!rtnl_dereference(platform_label
[index
]))
551 return LABEL_NOT_SPECIFIED
;
554 #if IS_ENABLED(CONFIG_INET)
555 static struct net_device
*inet_fib_lookup_dev(struct net
*net
,
558 struct net_device
*dev
;
560 struct in_addr daddr
;
562 memcpy(&daddr
, addr
, sizeof(struct in_addr
));
563 rt
= ip_route_output(net
, daddr
.s_addr
, 0, 0, 0);
575 static struct net_device
*inet_fib_lookup_dev(struct net
*net
,
578 return ERR_PTR(-EAFNOSUPPORT
);
582 #if IS_ENABLED(CONFIG_IPV6)
583 static struct net_device
*inet6_fib_lookup_dev(struct net
*net
,
586 struct net_device
*dev
;
587 struct dst_entry
*dst
;
592 return ERR_PTR(-EAFNOSUPPORT
);
594 memset(&fl6
, 0, sizeof(fl6
));
595 memcpy(&fl6
.daddr
, addr
, sizeof(struct in6_addr
));
596 err
= ipv6_stub
->ipv6_dst_lookup(net
, NULL
, &dst
, &fl6
);
607 static struct net_device
*inet6_fib_lookup_dev(struct net
*net
,
610 return ERR_PTR(-EAFNOSUPPORT
);
614 static struct net_device
*find_outdev(struct net
*net
,
615 struct mpls_route
*rt
,
616 struct mpls_nh
*nh
, int oif
)
618 struct net_device
*dev
= NULL
;
621 switch (nh
->nh_via_table
) {
622 case NEIGH_ARP_TABLE
:
623 dev
= inet_fib_lookup_dev(net
, mpls_nh_via(rt
, nh
));
626 dev
= inet6_fib_lookup_dev(net
, mpls_nh_via(rt
, nh
));
628 case NEIGH_LINK_TABLE
:
632 dev
= dev_get_by_index(net
, oif
);
636 return ERR_PTR(-ENODEV
);
641 /* The caller is holding rtnl anyways, so release the dev reference */
647 static int mpls_nh_assign_dev(struct net
*net
, struct mpls_route
*rt
,
648 struct mpls_nh
*nh
, int oif
)
650 struct net_device
*dev
= NULL
;
653 dev
= find_outdev(net
, rt
, nh
, oif
);
660 /* Ensure this is a supported device */
662 if (!mpls_dev_get(dev
))
665 if ((nh
->nh_via_table
== NEIGH_LINK_TABLE
) &&
666 (dev
->addr_len
!= nh
->nh_via_alen
))
669 RCU_INIT_POINTER(nh
->nh_dev
, dev
);
671 if (!(dev
->flags
& IFF_UP
)) {
672 nh
->nh_flags
|= RTNH_F_DEAD
;
676 flags
= dev_get_flags(dev
);
677 if (!(flags
& (IFF_RUNNING
| IFF_LOWER_UP
)))
678 nh
->nh_flags
|= RTNH_F_LINKDOWN
;
687 static int nla_get_via(const struct nlattr
*nla
, u8
*via_alen
, u8
*via_table
,
688 u8 via_addr
[], struct netlink_ext_ack
*extack
)
690 struct rtvia
*via
= nla_data(nla
);
694 if (nla_len(nla
) < offsetof(struct rtvia
, rtvia_addr
)) {
695 NL_SET_ERR_MSG_ATTR(extack
, nla
,
696 "Invalid attribute length for RTA_VIA");
699 alen
= nla_len(nla
) -
700 offsetof(struct rtvia
, rtvia_addr
);
701 if (alen
> MAX_VIA_ALEN
) {
702 NL_SET_ERR_MSG_ATTR(extack
, nla
,
703 "Invalid address length for RTA_VIA");
707 /* Validate the address family */
708 switch (via
->rtvia_family
) {
710 *via_table
= NEIGH_LINK_TABLE
;
713 *via_table
= NEIGH_ARP_TABLE
;
718 *via_table
= NEIGH_ND_TABLE
;
723 /* Unsupported address family */
727 memcpy(via_addr
, via
->rtvia_addr
, alen
);
735 static int mpls_nh_build_from_cfg(struct mpls_route_config
*cfg
,
736 struct mpls_route
*rt
)
738 struct net
*net
= cfg
->rc_nlinfo
.nl_net
;
739 struct mpls_nh
*nh
= rt
->rt_nh
;
746 nh
->nh_labels
= cfg
->rc_output_labels
;
747 for (i
= 0; i
< nh
->nh_labels
; i
++)
748 nh
->nh_label
[i
] = cfg
->rc_output_label
[i
];
750 nh
->nh_via_table
= cfg
->rc_via_table
;
751 memcpy(__mpls_nh_via(rt
, nh
), cfg
->rc_via
, cfg
->rc_via_alen
);
752 nh
->nh_via_alen
= cfg
->rc_via_alen
;
754 err
= mpls_nh_assign_dev(net
, rt
, nh
, cfg
->rc_ifindex
);
758 if (nh
->nh_flags
& (RTNH_F_DEAD
| RTNH_F_LINKDOWN
))
767 static int mpls_nh_build(struct net
*net
, struct mpls_route
*rt
,
768 struct mpls_nh
*nh
, int oif
, struct nlattr
*via
,
769 struct nlattr
*newdst
, u8 max_labels
,
770 struct netlink_ext_ack
*extack
)
778 err
= nla_get_labels(newdst
, max_labels
, &nh
->nh_labels
,
779 nh
->nh_label
, extack
);
785 err
= nla_get_via(via
, &nh
->nh_via_alen
, &nh
->nh_via_table
,
786 __mpls_nh_via(rt
, nh
), extack
);
790 nh
->nh_via_table
= MPLS_NEIGH_TABLE_UNSPEC
;
793 err
= mpls_nh_assign_dev(net
, rt
, nh
, oif
);
803 static u8
mpls_count_nexthops(struct rtnexthop
*rtnh
, int len
,
804 u8 cfg_via_alen
, u8
*max_via_alen
,
813 while (rtnh_ok(rtnh
, remaining
)) {
814 struct nlattr
*nla
, *attrs
= rtnh_attrs(rtnh
);
818 attrlen
= rtnh_attrlen(rtnh
);
819 nla
= nla_find(attrs
, attrlen
, RTA_VIA
);
820 if (nla
&& nla_len(nla
) >=
821 offsetof(struct rtvia
, rtvia_addr
)) {
822 int via_alen
= nla_len(nla
) -
823 offsetof(struct rtvia
, rtvia_addr
);
825 if (via_alen
<= MAX_VIA_ALEN
)
826 *max_via_alen
= max_t(u16
, *max_via_alen
,
830 nla
= nla_find(attrs
, attrlen
, RTA_NEWDST
);
832 nla_get_labels(nla
, MAX_NEW_LABELS
, &n_labels
,
836 *max_labels
= max_t(u8
, *max_labels
, n_labels
);
838 /* number of nexthops is tracked by a u8.
839 * Check for overflow.
845 rtnh
= rtnh_next(rtnh
, &remaining
);
848 /* leftover implies invalid nexthop configuration, discard it */
849 return remaining
> 0 ? 0 : nhs
;
852 static int mpls_nh_build_multi(struct mpls_route_config
*cfg
,
853 struct mpls_route
*rt
, u8 max_labels
,
854 struct netlink_ext_ack
*extack
)
856 struct rtnexthop
*rtnh
= cfg
->rc_mp
;
857 struct nlattr
*nla_via
, *nla_newdst
;
858 int remaining
= cfg
->rc_mp_len
;
862 change_nexthops(rt
) {
869 if (!rtnh_ok(rtnh
, remaining
))
872 /* neither weighted multipath nor any flags
875 if (rtnh
->rtnh_hops
|| rtnh
->rtnh_flags
)
878 attrlen
= rtnh_attrlen(rtnh
);
880 struct nlattr
*attrs
= rtnh_attrs(rtnh
);
882 nla_via
= nla_find(attrs
, attrlen
, RTA_VIA
);
883 nla_newdst
= nla_find(attrs
, attrlen
, RTA_NEWDST
);
886 err
= mpls_nh_build(cfg
->rc_nlinfo
.nl_net
, rt
, nh
,
887 rtnh
->rtnh_ifindex
, nla_via
, nla_newdst
,
892 if (nh
->nh_flags
& (RTNH_F_DEAD
| RTNH_F_LINKDOWN
))
895 rtnh
= rtnh_next(rtnh
, &remaining
);
897 } endfor_nexthops(rt
);
907 static bool mpls_label_ok(struct net
*net
, unsigned int index
,
908 struct netlink_ext_ack
*extack
)
910 /* Reserved labels may not be set */
911 if (index
< MPLS_LABEL_FIRST_UNRESERVED
) {
912 NL_SET_ERR_MSG(extack
,
913 "Invalid label - must be MPLS_LABEL_FIRST_UNRESERVED or higher");
917 /* The full 20 bit range may not be supported. */
918 if (index
>= net
->mpls
.platform_labels
) {
919 NL_SET_ERR_MSG(extack
,
920 "Label >= configured maximum in platform_labels");
927 static int mpls_route_add(struct mpls_route_config
*cfg
,
928 struct netlink_ext_ack
*extack
)
930 struct mpls_route __rcu
**platform_label
;
931 struct net
*net
= cfg
->rc_nlinfo
.nl_net
;
932 struct mpls_route
*rt
, *old
;
939 index
= cfg
->rc_label
;
941 /* If a label was not specified during insert pick one */
942 if ((index
== LABEL_NOT_SPECIFIED
) &&
943 (cfg
->rc_nlflags
& NLM_F_CREATE
)) {
944 index
= find_free_label(net
);
947 if (!mpls_label_ok(net
, index
, extack
))
950 /* Append makes no sense with mpls */
952 if (cfg
->rc_nlflags
& NLM_F_APPEND
) {
953 NL_SET_ERR_MSG(extack
, "MPLS does not support route append");
958 platform_label
= rtnl_dereference(net
->mpls
.platform_label
);
959 old
= rtnl_dereference(platform_label
[index
]);
960 if ((cfg
->rc_nlflags
& NLM_F_EXCL
) && old
)
964 if (!(cfg
->rc_nlflags
& NLM_F_REPLACE
) && old
)
968 if (!(cfg
->rc_nlflags
& NLM_F_CREATE
) && !old
)
973 nhs
= mpls_count_nexthops(cfg
->rc_mp
, cfg
->rc_mp_len
,
974 cfg
->rc_via_alen
, &max_via_alen
,
977 max_via_alen
= cfg
->rc_via_alen
;
978 max_labels
= cfg
->rc_output_labels
;
983 NL_SET_ERR_MSG(extack
, "Route does not contain a nexthop");
988 rt
= mpls_rt_alloc(nhs
, max_via_alen
, max_labels
);
994 rt
->rt_protocol
= cfg
->rc_protocol
;
995 rt
->rt_payload_type
= cfg
->rc_payload_type
;
996 rt
->rt_ttl_propagate
= cfg
->rc_ttl_propagate
;
999 err
= mpls_nh_build_multi(cfg
, rt
, max_labels
, extack
);
1001 err
= mpls_nh_build_from_cfg(cfg
, rt
);
1005 mpls_route_update(net
, index
, rt
, &cfg
->rc_nlinfo
);
1015 static int mpls_route_del(struct mpls_route_config
*cfg
,
1016 struct netlink_ext_ack
*extack
)
1018 struct net
*net
= cfg
->rc_nlinfo
.nl_net
;
1022 index
= cfg
->rc_label
;
1024 if (!mpls_label_ok(net
, index
, extack
))
1027 mpls_route_update(net
, index
, NULL
, &cfg
->rc_nlinfo
);
1034 static void mpls_get_stats(struct mpls_dev
*mdev
,
1035 struct mpls_link_stats
*stats
)
1037 struct mpls_pcpu_stats
*p
;
1040 memset(stats
, 0, sizeof(*stats
));
1042 for_each_possible_cpu(i
) {
1043 struct mpls_link_stats local
;
1046 p
= per_cpu_ptr(mdev
->stats
, i
);
1048 start
= u64_stats_fetch_begin(&p
->syncp
);
1050 } while (u64_stats_fetch_retry(&p
->syncp
, start
));
1052 stats
->rx_packets
+= local
.rx_packets
;
1053 stats
->rx_bytes
+= local
.rx_bytes
;
1054 stats
->tx_packets
+= local
.tx_packets
;
1055 stats
->tx_bytes
+= local
.tx_bytes
;
1056 stats
->rx_errors
+= local
.rx_errors
;
1057 stats
->tx_errors
+= local
.tx_errors
;
1058 stats
->rx_dropped
+= local
.rx_dropped
;
1059 stats
->tx_dropped
+= local
.tx_dropped
;
1060 stats
->rx_noroute
+= local
.rx_noroute
;
1064 static int mpls_fill_stats_af(struct sk_buff
*skb
,
1065 const struct net_device
*dev
)
1067 struct mpls_link_stats
*stats
;
1068 struct mpls_dev
*mdev
;
1071 mdev
= mpls_dev_get(dev
);
1075 nla
= nla_reserve_64bit(skb
, MPLS_STATS_LINK
,
1076 sizeof(struct mpls_link_stats
),
1081 stats
= nla_data(nla
);
1082 mpls_get_stats(mdev
, stats
);
1087 static size_t mpls_get_stats_af_size(const struct net_device
*dev
)
1089 struct mpls_dev
*mdev
;
1091 mdev
= mpls_dev_get(dev
);
1095 return nla_total_size_64bit(sizeof(struct mpls_link_stats
));
1098 static int mpls_netconf_fill_devconf(struct sk_buff
*skb
, struct mpls_dev
*mdev
,
1099 u32 portid
, u32 seq
, int event
,
1100 unsigned int flags
, int type
)
1102 struct nlmsghdr
*nlh
;
1103 struct netconfmsg
*ncm
;
1106 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct netconfmsg
),
1111 if (type
== NETCONFA_ALL
)
1114 ncm
= nlmsg_data(nlh
);
1115 ncm
->ncm_family
= AF_MPLS
;
1117 if (nla_put_s32(skb
, NETCONFA_IFINDEX
, mdev
->dev
->ifindex
) < 0)
1118 goto nla_put_failure
;
1120 if ((all
|| type
== NETCONFA_INPUT
) &&
1121 nla_put_s32(skb
, NETCONFA_INPUT
,
1122 mdev
->input_enabled
) < 0)
1123 goto nla_put_failure
;
1125 nlmsg_end(skb
, nlh
);
1129 nlmsg_cancel(skb
, nlh
);
1133 static int mpls_netconf_msgsize_devconf(int type
)
1135 int size
= NLMSG_ALIGN(sizeof(struct netconfmsg
))
1136 + nla_total_size(4); /* NETCONFA_IFINDEX */
1139 if (type
== NETCONFA_ALL
)
1142 if (all
|| type
== NETCONFA_INPUT
)
1143 size
+= nla_total_size(4);
1148 static void mpls_netconf_notify_devconf(struct net
*net
, int event
,
1149 int type
, struct mpls_dev
*mdev
)
1151 struct sk_buff
*skb
;
1154 skb
= nlmsg_new(mpls_netconf_msgsize_devconf(type
), GFP_KERNEL
);
1158 err
= mpls_netconf_fill_devconf(skb
, mdev
, 0, 0, event
, 0, type
);
1160 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1161 WARN_ON(err
== -EMSGSIZE
);
1166 rtnl_notify(skb
, net
, 0, RTNLGRP_MPLS_NETCONF
, NULL
, GFP_KERNEL
);
1170 rtnl_set_sk_err(net
, RTNLGRP_MPLS_NETCONF
, err
);
1173 static const struct nla_policy devconf_mpls_policy
[NETCONFA_MAX
+ 1] = {
1174 [NETCONFA_IFINDEX
] = { .len
= sizeof(int) },
1177 static int mpls_netconf_get_devconf(struct sk_buff
*in_skb
,
1178 struct nlmsghdr
*nlh
,
1179 struct netlink_ext_ack
*extack
)
1181 struct net
*net
= sock_net(in_skb
->sk
);
1182 struct nlattr
*tb
[NETCONFA_MAX
+ 1];
1183 struct netconfmsg
*ncm
;
1184 struct net_device
*dev
;
1185 struct mpls_dev
*mdev
;
1186 struct sk_buff
*skb
;
1190 err
= nlmsg_parse(nlh
, sizeof(*ncm
), tb
, NETCONFA_MAX
,
1191 devconf_mpls_policy
, NULL
);
1196 if (!tb
[NETCONFA_IFINDEX
])
1199 ifindex
= nla_get_s32(tb
[NETCONFA_IFINDEX
]);
1200 dev
= __dev_get_by_index(net
, ifindex
);
1204 mdev
= mpls_dev_get(dev
);
1209 skb
= nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL
), GFP_KERNEL
);
1213 err
= mpls_netconf_fill_devconf(skb
, mdev
,
1214 NETLINK_CB(in_skb
).portid
,
1215 nlh
->nlmsg_seq
, RTM_NEWNETCONF
, 0,
1218 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1219 WARN_ON(err
== -EMSGSIZE
);
1223 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
1228 static int mpls_netconf_dump_devconf(struct sk_buff
*skb
,
1229 struct netlink_callback
*cb
)
1231 struct net
*net
= sock_net(skb
->sk
);
1232 struct hlist_head
*head
;
1233 struct net_device
*dev
;
1234 struct mpls_dev
*mdev
;
1239 s_idx
= idx
= cb
->args
[1];
1241 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1243 head
= &net
->dev_index_head
[h
];
1245 cb
->seq
= net
->dev_base_seq
;
1246 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
1249 mdev
= mpls_dev_get(dev
);
1252 if (mpls_netconf_fill_devconf(skb
, mdev
,
1253 NETLINK_CB(cb
->skb
).portid
,
1257 NETCONFA_ALL
) < 0) {
1261 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1274 #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
1275 (&((struct mpls_dev *)0)->field)
1277 static int mpls_conf_proc(struct ctl_table
*ctl
, int write
,
1278 void __user
*buffer
,
1279 size_t *lenp
, loff_t
*ppos
)
1281 int oval
= *(int *)ctl
->data
;
1282 int ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
1285 struct mpls_dev
*mdev
= ctl
->extra1
;
1286 int i
= (int *)ctl
->data
- (int *)mdev
;
1287 struct net
*net
= ctl
->extra2
;
1288 int val
= *(int *)ctl
->data
;
1290 if (i
== offsetof(struct mpls_dev
, input_enabled
) &&
1292 mpls_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
1293 NETCONFA_INPUT
, mdev
);
1300 static const struct ctl_table mpls_dev_table
[] = {
1302 .procname
= "input",
1303 .maxlen
= sizeof(int),
1305 .proc_handler
= mpls_conf_proc
,
1306 .data
= MPLS_PERDEV_SYSCTL_OFFSET(input_enabled
),
1311 static int mpls_dev_sysctl_register(struct net_device
*dev
,
1312 struct mpls_dev
*mdev
)
1314 char path
[sizeof("net/mpls/conf/") + IFNAMSIZ
];
1315 struct net
*net
= dev_net(dev
);
1316 struct ctl_table
*table
;
1319 table
= kmemdup(&mpls_dev_table
, sizeof(mpls_dev_table
), GFP_KERNEL
);
1323 /* Table data contains only offsets relative to the base of
1324 * the mdev at this point, so make them absolute.
1326 for (i
= 0; i
< ARRAY_SIZE(mpls_dev_table
); i
++) {
1327 table
[i
].data
= (char *)mdev
+ (uintptr_t)table
[i
].data
;
1328 table
[i
].extra1
= mdev
;
1329 table
[i
].extra2
= net
;
1332 snprintf(path
, sizeof(path
), "net/mpls/conf/%s", dev
->name
);
1334 mdev
->sysctl
= register_net_sysctl(net
, path
, table
);
1338 mpls_netconf_notify_devconf(net
, RTM_NEWNETCONF
, NETCONFA_ALL
, mdev
);
1347 static void mpls_dev_sysctl_unregister(struct net_device
*dev
,
1348 struct mpls_dev
*mdev
)
1350 struct net
*net
= dev_net(dev
);
1351 struct ctl_table
*table
;
1353 table
= mdev
->sysctl
->ctl_table_arg
;
1354 unregister_net_sysctl_table(mdev
->sysctl
);
1357 mpls_netconf_notify_devconf(net
, RTM_DELNETCONF
, 0, mdev
);
1360 static struct mpls_dev
*mpls_add_dev(struct net_device
*dev
)
1362 struct mpls_dev
*mdev
;
1368 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
1370 return ERR_PTR(err
);
1372 mdev
->stats
= alloc_percpu(struct mpls_pcpu_stats
);
1376 for_each_possible_cpu(i
) {
1377 struct mpls_pcpu_stats
*mpls_stats
;
1379 mpls_stats
= per_cpu_ptr(mdev
->stats
, i
);
1380 u64_stats_init(&mpls_stats
->syncp
);
1385 err
= mpls_dev_sysctl_register(dev
, mdev
);
1389 rcu_assign_pointer(dev
->mpls_ptr
, mdev
);
1394 free_percpu(mdev
->stats
);
1396 return ERR_PTR(err
);
1399 static void mpls_dev_destroy_rcu(struct rcu_head
*head
)
1401 struct mpls_dev
*mdev
= container_of(head
, struct mpls_dev
, rcu
);
1403 free_percpu(mdev
->stats
);
1407 static void mpls_ifdown(struct net_device
*dev
, int event
)
1409 struct mpls_route __rcu
**platform_label
;
1410 struct net
*net
= dev_net(dev
);
1414 platform_label
= rtnl_dereference(net
->mpls
.platform_label
);
1415 for (index
= 0; index
< net
->mpls
.platform_labels
; index
++) {
1416 struct mpls_route
*rt
= rtnl_dereference(platform_label
[index
]);
1423 change_nexthops(rt
) {
1424 unsigned int nh_flags
= nh
->nh_flags
;
1426 if (rtnl_dereference(nh
->nh_dev
) != dev
)
1431 case NETDEV_UNREGISTER
:
1432 nh_flags
|= RTNH_F_DEAD
;
1435 nh_flags
|= RTNH_F_LINKDOWN
;
1438 if (event
== NETDEV_UNREGISTER
)
1439 RCU_INIT_POINTER(nh
->nh_dev
, NULL
);
1441 if (nh
->nh_flags
!= nh_flags
)
1442 WRITE_ONCE(nh
->nh_flags
, nh_flags
);
1444 if (!(nh_flags
& (RTNH_F_DEAD
| RTNH_F_LINKDOWN
)))
1446 if (!rtnl_dereference(nh
->nh_dev
))
1448 } endfor_nexthops(rt
);
1450 WRITE_ONCE(rt
->rt_nhn_alive
, alive
);
1452 /* if there are no more nexthops, delete the route */
1453 if (event
== NETDEV_UNREGISTER
&& deleted
== rt
->rt_nhn
)
1454 mpls_route_update(net
, index
, NULL
, NULL
);
1458 static void mpls_ifup(struct net_device
*dev
, unsigned int flags
)
1460 struct mpls_route __rcu
**platform_label
;
1461 struct net
*net
= dev_net(dev
);
1465 platform_label
= rtnl_dereference(net
->mpls
.platform_label
);
1466 for (index
= 0; index
< net
->mpls
.platform_labels
; index
++) {
1467 struct mpls_route
*rt
= rtnl_dereference(platform_label
[index
]);
1473 change_nexthops(rt
) {
1474 unsigned int nh_flags
= nh
->nh_flags
;
1475 struct net_device
*nh_dev
=
1476 rtnl_dereference(nh
->nh_dev
);
1478 if (!(nh_flags
& flags
)) {
1486 WRITE_ONCE(nh
->nh_flags
, nh_flags
);
1487 } endfor_nexthops(rt
);
1489 WRITE_ONCE(rt
->rt_nhn_alive
, alive
);
1493 static int mpls_dev_notify(struct notifier_block
*this, unsigned long event
,
1496 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1497 struct mpls_dev
*mdev
;
1500 if (event
== NETDEV_REGISTER
) {
1501 /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
1502 if (dev
->type
== ARPHRD_ETHER
||
1503 dev
->type
== ARPHRD_LOOPBACK
||
1504 dev
->type
== ARPHRD_IPGRE
||
1505 dev
->type
== ARPHRD_SIT
||
1506 dev
->type
== ARPHRD_TUNNEL
) {
1507 mdev
= mpls_add_dev(dev
);
1509 return notifier_from_errno(PTR_ERR(mdev
));
1514 mdev
= mpls_dev_get(dev
);
1520 mpls_ifdown(dev
, event
);
1523 flags
= dev_get_flags(dev
);
1524 if (flags
& (IFF_RUNNING
| IFF_LOWER_UP
))
1525 mpls_ifup(dev
, RTNH_F_DEAD
| RTNH_F_LINKDOWN
);
1527 mpls_ifup(dev
, RTNH_F_DEAD
);
1530 flags
= dev_get_flags(dev
);
1531 if (flags
& (IFF_RUNNING
| IFF_LOWER_UP
))
1532 mpls_ifup(dev
, RTNH_F_DEAD
| RTNH_F_LINKDOWN
);
1534 mpls_ifdown(dev
, event
);
1536 case NETDEV_UNREGISTER
:
1537 mpls_ifdown(dev
, event
);
1538 mdev
= mpls_dev_get(dev
);
1540 mpls_dev_sysctl_unregister(dev
, mdev
);
1541 RCU_INIT_POINTER(dev
->mpls_ptr
, NULL
);
1542 call_rcu(&mdev
->rcu
, mpls_dev_destroy_rcu
);
1545 case NETDEV_CHANGENAME
:
1546 mdev
= mpls_dev_get(dev
);
1550 mpls_dev_sysctl_unregister(dev
, mdev
);
1551 err
= mpls_dev_sysctl_register(dev
, mdev
);
1553 return notifier_from_errno(err
);
1560 static struct notifier_block mpls_dev_notifier
= {
1561 .notifier_call
= mpls_dev_notify
,
1564 static int nla_put_via(struct sk_buff
*skb
,
1565 u8 table
, const void *addr
, int alen
)
1567 static const int table_to_family
[NEIGH_NR_TABLES
+ 1] = {
1568 AF_INET
, AF_INET6
, AF_DECnet
, AF_PACKET
,
1572 int family
= AF_UNSPEC
;
1574 nla
= nla_reserve(skb
, RTA_VIA
, alen
+ 2);
1578 if (table
<= NEIGH_NR_TABLES
)
1579 family
= table_to_family
[table
];
1581 via
= nla_data(nla
);
1582 via
->rtvia_family
= family
;
1583 memcpy(via
->rtvia_addr
, addr
, alen
);
1587 int nla_put_labels(struct sk_buff
*skb
, int attrtype
,
1588 u8 labels
, const u32 label
[])
1591 struct mpls_shim_hdr
*nla_label
;
1594 nla
= nla_reserve(skb
, attrtype
, labels
*4);
1598 nla_label
= nla_data(nla
);
1600 for (i
= labels
- 1; i
>= 0; i
--) {
1601 nla_label
[i
] = mpls_entry_encode(label
[i
], 0, 0, bos
);
1607 EXPORT_SYMBOL_GPL(nla_put_labels
);
1609 int nla_get_labels(const struct nlattr
*nla
, u8 max_labels
, u8
*labels
,
1610 u32 label
[], struct netlink_ext_ack
*extack
)
1612 unsigned len
= nla_len(nla
);
1613 struct mpls_shim_hdr
*nla_label
;
1618 /* len needs to be an even multiple of 4 (the label size). Number
1619 * of labels is a u8 so check for overflow.
1621 if (len
& 3 || len
/ 4 > 255) {
1622 NL_SET_ERR_MSG_ATTR(extack
, nla
,
1623 "Invalid length for labels attribute");
1627 /* Limit the number of new labels allowed */
1629 if (nla_labels
> max_labels
) {
1630 NL_SET_ERR_MSG(extack
, "Too many labels");
1634 /* when label == NULL, caller wants number of labels */
1638 nla_label
= nla_data(nla
);
1640 for (i
= nla_labels
- 1; i
>= 0; i
--, bos
= false) {
1641 struct mpls_entry_decoded dec
;
1642 dec
= mpls_entry_decode(nla_label
+ i
);
1644 /* Ensure the bottom of stack flag is properly set
1645 * and ttl and tc are both clear.
1648 NL_SET_ERR_MSG_ATTR(extack
, nla
,
1649 "TTL in label must be 0");
1654 NL_SET_ERR_MSG_ATTR(extack
, nla
,
1655 "Traffic class in label must be 0");
1659 if (dec
.bos
!= bos
) {
1660 NL_SET_BAD_ATTR(extack
, nla
);
1662 NL_SET_ERR_MSG(extack
,
1663 "BOS bit must be set in first label");
1665 NL_SET_ERR_MSG(extack
,
1666 "BOS bit can only be set in first label");
1671 switch (dec
.label
) {
1672 case MPLS_LABEL_IMPLNULL
:
1673 /* RFC3032: This is a label that an LSR may
1674 * assign and distribute, but which never
1675 * actually appears in the encapsulation.
1677 NL_SET_ERR_MSG_ATTR(extack
, nla
,
1678 "Implicit NULL Label (3) can not be used in encapsulation");
1682 label
[i
] = dec
.label
;
1685 *labels
= nla_labels
;
1688 EXPORT_SYMBOL_GPL(nla_get_labels
);
1690 static int rtm_to_route_config(struct sk_buff
*skb
,
1691 struct nlmsghdr
*nlh
,
1692 struct mpls_route_config
*cfg
,
1693 struct netlink_ext_ack
*extack
)
1696 struct nlattr
*tb
[RTA_MAX
+1];
1700 err
= nlmsg_parse(nlh
, sizeof(*rtm
), tb
, RTA_MAX
, rtm_mpls_policy
,
1706 rtm
= nlmsg_data(nlh
);
1708 if (rtm
->rtm_family
!= AF_MPLS
) {
1709 NL_SET_ERR_MSG(extack
, "Invalid address family in rtmsg");
1712 if (rtm
->rtm_dst_len
!= 20) {
1713 NL_SET_ERR_MSG(extack
, "rtm_dst_len must be 20 for MPLS");
1716 if (rtm
->rtm_src_len
!= 0) {
1717 NL_SET_ERR_MSG(extack
, "rtm_src_len must be 0 for MPLS");
1720 if (rtm
->rtm_tos
!= 0) {
1721 NL_SET_ERR_MSG(extack
, "rtm_tos must be 0 for MPLS");
1724 if (rtm
->rtm_table
!= RT_TABLE_MAIN
) {
1725 NL_SET_ERR_MSG(extack
,
1726 "MPLS only supports the main route table");
1729 /* Any value is acceptable for rtm_protocol */
1731 /* As mpls uses destination specific addresses
1732 * (or source specific address in the case of multicast)
1733 * all addresses have universal scope.
1735 if (rtm
->rtm_scope
!= RT_SCOPE_UNIVERSE
) {
1736 NL_SET_ERR_MSG(extack
,
1737 "Invalid route scope - MPLS only supports UNIVERSE");
1740 if (rtm
->rtm_type
!= RTN_UNICAST
) {
1741 NL_SET_ERR_MSG(extack
,
1742 "Invalid route type - MPLS only supports UNICAST");
1745 if (rtm
->rtm_flags
!= 0) {
1746 NL_SET_ERR_MSG(extack
, "rtm_flags must be 0 for MPLS");
1750 cfg
->rc_label
= LABEL_NOT_SPECIFIED
;
1751 cfg
->rc_protocol
= rtm
->rtm_protocol
;
1752 cfg
->rc_via_table
= MPLS_NEIGH_TABLE_UNSPEC
;
1753 cfg
->rc_ttl_propagate
= MPLS_TTL_PROP_DEFAULT
;
1754 cfg
->rc_nlflags
= nlh
->nlmsg_flags
;
1755 cfg
->rc_nlinfo
.portid
= NETLINK_CB(skb
).portid
;
1756 cfg
->rc_nlinfo
.nlh
= nlh
;
1757 cfg
->rc_nlinfo
.nl_net
= sock_net(skb
->sk
);
1759 for (index
= 0; index
<= RTA_MAX
; index
++) {
1760 struct nlattr
*nla
= tb
[index
];
1766 cfg
->rc_ifindex
= nla_get_u32(nla
);
1769 if (nla_get_labels(nla
, MAX_NEW_LABELS
,
1770 &cfg
->rc_output_labels
,
1771 cfg
->rc_output_label
, extack
))
1777 if (nla_get_labels(nla
, 1, &label_count
,
1778 &cfg
->rc_label
, extack
))
1781 if (!mpls_label_ok(cfg
->rc_nlinfo
.nl_net
,
1782 cfg
->rc_label
, extack
))
1788 if (nla_get_via(nla
, &cfg
->rc_via_alen
,
1789 &cfg
->rc_via_table
, cfg
->rc_via
,
1796 cfg
->rc_mp
= nla_data(nla
);
1797 cfg
->rc_mp_len
= nla_len(nla
);
1800 case RTA_TTL_PROPAGATE
:
1802 u8 ttl_propagate
= nla_get_u8(nla
);
1804 if (ttl_propagate
> 1) {
1805 NL_SET_ERR_MSG_ATTR(extack
, nla
,
1806 "RTA_TTL_PROPAGATE can only be 0 or 1");
1809 cfg
->rc_ttl_propagate
= ttl_propagate
?
1810 MPLS_TTL_PROP_ENABLED
:
1811 MPLS_TTL_PROP_DISABLED
;
1815 NL_SET_ERR_MSG_ATTR(extack
, nla
, "Unknown attribute");
1816 /* Unsupported attribute */
1826 static int mpls_rtm_delroute(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1827 struct netlink_ext_ack
*extack
)
1829 struct mpls_route_config
*cfg
;
1832 cfg
= kzalloc(sizeof(*cfg
), GFP_KERNEL
);
1836 err
= rtm_to_route_config(skb
, nlh
, cfg
, extack
);
1840 err
= mpls_route_del(cfg
, extack
);
1848 static int mpls_rtm_newroute(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1849 struct netlink_ext_ack
*extack
)
1851 struct mpls_route_config
*cfg
;
1854 cfg
= kzalloc(sizeof(*cfg
), GFP_KERNEL
);
1858 err
= rtm_to_route_config(skb
, nlh
, cfg
, extack
);
1862 err
= mpls_route_add(cfg
, extack
);
1869 static int mpls_dump_route(struct sk_buff
*skb
, u32 portid
, u32 seq
, int event
,
1870 u32 label
, struct mpls_route
*rt
, int flags
)
1872 struct net_device
*dev
;
1873 struct nlmsghdr
*nlh
;
1876 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*rtm
), flags
);
1880 rtm
= nlmsg_data(nlh
);
1881 rtm
->rtm_family
= AF_MPLS
;
1882 rtm
->rtm_dst_len
= 20;
1883 rtm
->rtm_src_len
= 0;
1885 rtm
->rtm_table
= RT_TABLE_MAIN
;
1886 rtm
->rtm_protocol
= rt
->rt_protocol
;
1887 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1888 rtm
->rtm_type
= RTN_UNICAST
;
1891 if (nla_put_labels(skb
, RTA_DST
, 1, &label
))
1892 goto nla_put_failure
;
1894 if (rt
->rt_ttl_propagate
!= MPLS_TTL_PROP_DEFAULT
) {
1895 bool ttl_propagate
=
1896 rt
->rt_ttl_propagate
== MPLS_TTL_PROP_ENABLED
;
1898 if (nla_put_u8(skb
, RTA_TTL_PROPAGATE
,
1900 goto nla_put_failure
;
1902 if (rt
->rt_nhn
== 1) {
1903 const struct mpls_nh
*nh
= rt
->rt_nh
;
1905 if (nh
->nh_labels
&&
1906 nla_put_labels(skb
, RTA_NEWDST
, nh
->nh_labels
,
1908 goto nla_put_failure
;
1909 if (nh
->nh_via_table
!= MPLS_NEIGH_TABLE_UNSPEC
&&
1910 nla_put_via(skb
, nh
->nh_via_table
, mpls_nh_via(rt
, nh
),
1912 goto nla_put_failure
;
1913 dev
= rtnl_dereference(nh
->nh_dev
);
1914 if (dev
&& nla_put_u32(skb
, RTA_OIF
, dev
->ifindex
))
1915 goto nla_put_failure
;
1916 if (nh
->nh_flags
& RTNH_F_LINKDOWN
)
1917 rtm
->rtm_flags
|= RTNH_F_LINKDOWN
;
1918 if (nh
->nh_flags
& RTNH_F_DEAD
)
1919 rtm
->rtm_flags
|= RTNH_F_DEAD
;
1921 struct rtnexthop
*rtnh
;
1926 mp
= nla_nest_start(skb
, RTA_MULTIPATH
);
1928 goto nla_put_failure
;
1931 dev
= rtnl_dereference(nh
->nh_dev
);
1935 rtnh
= nla_reserve_nohdr(skb
, sizeof(*rtnh
));
1937 goto nla_put_failure
;
1939 rtnh
->rtnh_ifindex
= dev
->ifindex
;
1940 if (nh
->nh_flags
& RTNH_F_LINKDOWN
) {
1941 rtnh
->rtnh_flags
|= RTNH_F_LINKDOWN
;
1944 if (nh
->nh_flags
& RTNH_F_DEAD
) {
1945 rtnh
->rtnh_flags
|= RTNH_F_DEAD
;
1949 if (nh
->nh_labels
&& nla_put_labels(skb
, RTA_NEWDST
,
1952 goto nla_put_failure
;
1953 if (nh
->nh_via_table
!= MPLS_NEIGH_TABLE_UNSPEC
&&
1954 nla_put_via(skb
, nh
->nh_via_table
,
1955 mpls_nh_via(rt
, nh
),
1957 goto nla_put_failure
;
1959 /* length of rtnetlink header + attributes */
1960 rtnh
->rtnh_len
= nlmsg_get_pos(skb
) - (void *)rtnh
;
1961 } endfor_nexthops(rt
);
1963 if (linkdown
== rt
->rt_nhn
)
1964 rtm
->rtm_flags
|= RTNH_F_LINKDOWN
;
1965 if (dead
== rt
->rt_nhn
)
1966 rtm
->rtm_flags
|= RTNH_F_DEAD
;
1968 nla_nest_end(skb
, mp
);
1971 nlmsg_end(skb
, nlh
);
1975 nlmsg_cancel(skb
, nlh
);
1979 static int mpls_dump_routes(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1981 struct net
*net
= sock_net(skb
->sk
);
1982 struct mpls_route __rcu
**platform_label
;
1983 size_t platform_labels
;
1988 index
= cb
->args
[0];
1989 if (index
< MPLS_LABEL_FIRST_UNRESERVED
)
1990 index
= MPLS_LABEL_FIRST_UNRESERVED
;
1992 platform_label
= rtnl_dereference(net
->mpls
.platform_label
);
1993 platform_labels
= net
->mpls
.platform_labels
;
1994 for (; index
< platform_labels
; index
++) {
1995 struct mpls_route
*rt
;
1996 rt
= rtnl_dereference(platform_label
[index
]);
2000 if (mpls_dump_route(skb
, NETLINK_CB(cb
->skb
).portid
,
2001 cb
->nlh
->nlmsg_seq
, RTM_NEWROUTE
,
2002 index
, rt
, NLM_F_MULTI
) < 0)
2005 cb
->args
[0] = index
;
2010 static inline size_t lfib_nlmsg_size(struct mpls_route
*rt
)
2013 NLMSG_ALIGN(sizeof(struct rtmsg
))
2014 + nla_total_size(4) /* RTA_DST */
2015 + nla_total_size(1); /* RTA_TTL_PROPAGATE */
2017 if (rt
->rt_nhn
== 1) {
2018 struct mpls_nh
*nh
= rt
->rt_nh
;
2021 payload
+= nla_total_size(4); /* RTA_OIF */
2022 if (nh
->nh_via_table
!= MPLS_NEIGH_TABLE_UNSPEC
) /* RTA_VIA */
2023 payload
+= nla_total_size(2 + nh
->nh_via_alen
);
2024 if (nh
->nh_labels
) /* RTA_NEWDST */
2025 payload
+= nla_total_size(nh
->nh_labels
* 4);
2027 /* each nexthop is packed in an attribute */
2031 if (!rtnl_dereference(nh
->nh_dev
))
2033 nhsize
+= nla_total_size(sizeof(struct rtnexthop
));
2035 if (nh
->nh_via_table
!= MPLS_NEIGH_TABLE_UNSPEC
)
2036 nhsize
+= nla_total_size(2 + nh
->nh_via_alen
);
2038 nhsize
+= nla_total_size(nh
->nh_labels
* 4);
2039 } endfor_nexthops(rt
);
2040 /* nested attribute */
2041 payload
+= nla_total_size(nhsize
);
2047 static void rtmsg_lfib(int event
, u32 label
, struct mpls_route
*rt
,
2048 struct nlmsghdr
*nlh
, struct net
*net
, u32 portid
,
2049 unsigned int nlm_flags
)
2051 struct sk_buff
*skb
;
2052 u32 seq
= nlh
? nlh
->nlmsg_seq
: 0;
2055 skb
= nlmsg_new(lfib_nlmsg_size(rt
), GFP_KERNEL
);
2059 err
= mpls_dump_route(skb
, portid
, seq
, event
, label
, rt
, nlm_flags
);
2061 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
2062 WARN_ON(err
== -EMSGSIZE
);
2066 rtnl_notify(skb
, net
, portid
, RTNLGRP_MPLS_ROUTE
, nlh
, GFP_KERNEL
);
2071 rtnl_set_sk_err(net
, RTNLGRP_MPLS_ROUTE
, err
);
2074 static int mpls_getroute(struct sk_buff
*in_skb
, struct nlmsghdr
*in_nlh
,
2075 struct netlink_ext_ack
*extack
)
2077 struct net
*net
= sock_net(in_skb
->sk
);
2078 u32 portid
= NETLINK_CB(in_skb
).portid
;
2079 u32 in_label
= LABEL_NOT_SPECIFIED
;
2080 struct nlattr
*tb
[RTA_MAX
+ 1];
2081 u32 labels
[MAX_NEW_LABELS
];
2082 struct mpls_shim_hdr
*hdr
;
2083 unsigned int hdr_size
= 0;
2084 struct net_device
*dev
;
2085 struct mpls_route
*rt
;
2086 struct rtmsg
*rtm
, *r
;
2087 struct nlmsghdr
*nlh
;
2088 struct sk_buff
*skb
;
2093 err
= nlmsg_parse(in_nlh
, sizeof(*rtm
), tb
, RTA_MAX
,
2094 rtm_mpls_policy
, extack
);
2098 rtm
= nlmsg_data(in_nlh
);
2103 if (nla_get_labels(tb
[RTA_DST
], 1, &label_count
,
2104 &in_label
, extack
)) {
2109 if (!mpls_label_ok(net
, in_label
, extack
)) {
2115 rt
= mpls_route_input_rcu(net
, in_label
);
2121 if (rtm
->rtm_flags
& RTM_F_FIB_MATCH
) {
2122 skb
= nlmsg_new(lfib_nlmsg_size(rt
), GFP_KERNEL
);
2128 err
= mpls_dump_route(skb
, portid
, in_nlh
->nlmsg_seq
,
2129 RTM_NEWROUTE
, in_label
, rt
, 0);
2131 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
2132 WARN_ON(err
== -EMSGSIZE
);
2136 return rtnl_unicast(skb
, net
, portid
);
2139 if (tb
[RTA_NEWDST
]) {
2140 if (nla_get_labels(tb
[RTA_NEWDST
], MAX_NEW_LABELS
, &n_labels
,
2141 labels
, extack
) != 0) {
2146 hdr_size
= n_labels
* sizeof(struct mpls_shim_hdr
);
2149 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2155 skb
->protocol
= htons(ETH_P_MPLS_UC
);
2161 if (skb_cow(skb
, hdr_size
)) {
2166 skb_reserve(skb
, hdr_size
);
2167 skb_push(skb
, hdr_size
);
2168 skb_reset_network_header(skb
);
2170 /* Push new labels */
2171 hdr
= mpls_hdr(skb
);
2173 for (i
= n_labels
- 1; i
>= 0; i
--) {
2174 hdr
[i
] = mpls_entry_encode(labels
[i
],
2180 nh
= mpls_select_multipath(rt
, skb
);
2187 skb_pull(skb
, hdr_size
);
2188 skb_reset_network_header(skb
);
2191 nlh
= nlmsg_put(skb
, portid
, in_nlh
->nlmsg_seq
,
2192 RTM_NEWROUTE
, sizeof(*r
), 0);
2198 r
= nlmsg_data(nlh
);
2199 r
->rtm_family
= AF_MPLS
;
2200 r
->rtm_dst_len
= 20;
2202 r
->rtm_table
= RT_TABLE_MAIN
;
2203 r
->rtm_type
= RTN_UNICAST
;
2204 r
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2205 r
->rtm_protocol
= rt
->rt_protocol
;
2208 if (nla_put_labels(skb
, RTA_DST
, 1, &in_label
))
2209 goto nla_put_failure
;
2211 if (nh
->nh_labels
&&
2212 nla_put_labels(skb
, RTA_NEWDST
, nh
->nh_labels
,
2214 goto nla_put_failure
;
2216 if (nh
->nh_via_table
!= MPLS_NEIGH_TABLE_UNSPEC
&&
2217 nla_put_via(skb
, nh
->nh_via_table
, mpls_nh_via(rt
, nh
),
2219 goto nla_put_failure
;
2220 dev
= rtnl_dereference(nh
->nh_dev
);
2221 if (dev
&& nla_put_u32(skb
, RTA_OIF
, dev
->ifindex
))
2222 goto nla_put_failure
;
2224 nlmsg_end(skb
, nlh
);
2226 err
= rtnl_unicast(skb
, net
, portid
);
2231 nlmsg_cancel(skb
, nlh
);
2238 static int resize_platform_label_table(struct net
*net
, size_t limit
)
2240 size_t size
= sizeof(struct mpls_route
*) * limit
;
2243 struct mpls_route __rcu
**labels
= NULL
, **old
;
2244 struct mpls_route
*rt0
= NULL
, *rt2
= NULL
;
2248 labels
= kvzalloc(size
, GFP_KERNEL
);
2253 /* In case the predefined labels need to be populated */
2254 if (limit
> MPLS_LABEL_IPV4NULL
) {
2255 struct net_device
*lo
= net
->loopback_dev
;
2256 rt0
= mpls_rt_alloc(1, lo
->addr_len
, 0);
2259 RCU_INIT_POINTER(rt0
->rt_nh
->nh_dev
, lo
);
2260 rt0
->rt_protocol
= RTPROT_KERNEL
;
2261 rt0
->rt_payload_type
= MPT_IPV4
;
2262 rt0
->rt_ttl_propagate
= MPLS_TTL_PROP_DEFAULT
;
2263 rt0
->rt_nh
->nh_via_table
= NEIGH_LINK_TABLE
;
2264 rt0
->rt_nh
->nh_via_alen
= lo
->addr_len
;
2265 memcpy(__mpls_nh_via(rt0
, rt0
->rt_nh
), lo
->dev_addr
,
2268 if (limit
> MPLS_LABEL_IPV6NULL
) {
2269 struct net_device
*lo
= net
->loopback_dev
;
2270 rt2
= mpls_rt_alloc(1, lo
->addr_len
, 0);
2273 RCU_INIT_POINTER(rt2
->rt_nh
->nh_dev
, lo
);
2274 rt2
->rt_protocol
= RTPROT_KERNEL
;
2275 rt2
->rt_payload_type
= MPT_IPV6
;
2276 rt2
->rt_ttl_propagate
= MPLS_TTL_PROP_DEFAULT
;
2277 rt2
->rt_nh
->nh_via_table
= NEIGH_LINK_TABLE
;
2278 rt2
->rt_nh
->nh_via_alen
= lo
->addr_len
;
2279 memcpy(__mpls_nh_via(rt2
, rt2
->rt_nh
), lo
->dev_addr
,
2284 /* Remember the original table */
2285 old
= rtnl_dereference(net
->mpls
.platform_label
);
2286 old_limit
= net
->mpls
.platform_labels
;
2288 /* Free any labels beyond the new table */
2289 for (index
= limit
; index
< old_limit
; index
++)
2290 mpls_route_update(net
, index
, NULL
, NULL
);
2292 /* Copy over the old labels */
2294 if (old_limit
< limit
)
2295 cp_size
= old_limit
* sizeof(struct mpls_route
*);
2297 memcpy(labels
, old
, cp_size
);
2299 /* If needed set the predefined labels */
2300 if ((old_limit
<= MPLS_LABEL_IPV6NULL
) &&
2301 (limit
> MPLS_LABEL_IPV6NULL
)) {
2302 RCU_INIT_POINTER(labels
[MPLS_LABEL_IPV6NULL
], rt2
);
2306 if ((old_limit
<= MPLS_LABEL_IPV4NULL
) &&
2307 (limit
> MPLS_LABEL_IPV4NULL
)) {
2308 RCU_INIT_POINTER(labels
[MPLS_LABEL_IPV4NULL
], rt0
);
2312 /* Update the global pointers */
2313 net
->mpls
.platform_labels
= limit
;
2314 rcu_assign_pointer(net
->mpls
.platform_label
, labels
);
2335 static int mpls_platform_labels(struct ctl_table
*table
, int write
,
2336 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
2338 struct net
*net
= table
->data
;
2339 int platform_labels
= net
->mpls
.platform_labels
;
2341 struct ctl_table tmp
= {
2342 .procname
= table
->procname
,
2343 .data
= &platform_labels
,
2344 .maxlen
= sizeof(int),
2345 .mode
= table
->mode
,
2347 .extra2
= &label_limit
,
2350 ret
= proc_dointvec_minmax(&tmp
, write
, buffer
, lenp
, ppos
);
2352 if (write
&& ret
== 0)
2353 ret
= resize_platform_label_table(net
, platform_labels
);
2358 #define MPLS_NS_SYSCTL_OFFSET(field) \
2359 (&((struct net *)0)->field)
2361 static const struct ctl_table mpls_table
[] = {
2363 .procname
= "platform_labels",
2365 .maxlen
= sizeof(int),
2367 .proc_handler
= mpls_platform_labels
,
2370 .procname
= "ip_ttl_propagate",
2371 .data
= MPLS_NS_SYSCTL_OFFSET(mpls
.ip_ttl_propagate
),
2372 .maxlen
= sizeof(int),
2374 .proc_handler
= proc_dointvec_minmax
,
2379 .procname
= "default_ttl",
2380 .data
= MPLS_NS_SYSCTL_OFFSET(mpls
.default_ttl
),
2381 .maxlen
= sizeof(int),
2383 .proc_handler
= proc_dointvec_minmax
,
2390 static int mpls_net_init(struct net
*net
)
2392 struct ctl_table
*table
;
2395 net
->mpls
.platform_labels
= 0;
2396 net
->mpls
.platform_label
= NULL
;
2397 net
->mpls
.ip_ttl_propagate
= 1;
2398 net
->mpls
.default_ttl
= 255;
2400 table
= kmemdup(mpls_table
, sizeof(mpls_table
), GFP_KERNEL
);
2404 /* Table data contains only offsets relative to the base of
2405 * the mdev at this point, so make them absolute.
2407 for (i
= 0; i
< ARRAY_SIZE(mpls_table
) - 1; i
++)
2408 table
[i
].data
= (char *)net
+ (uintptr_t)table
[i
].data
;
2410 net
->mpls
.ctl
= register_net_sysctl(net
, "net/mpls", table
);
2411 if (net
->mpls
.ctl
== NULL
) {
2419 static void mpls_net_exit(struct net
*net
)
2421 struct mpls_route __rcu
**platform_label
;
2422 size_t platform_labels
;
2423 struct ctl_table
*table
;
2426 table
= net
->mpls
.ctl
->ctl_table_arg
;
2427 unregister_net_sysctl_table(net
->mpls
.ctl
);
2430 /* An rcu grace period has passed since there was a device in
2431 * the network namespace (and thus the last in flight packet)
2432 * left this network namespace. This is because
2433 * unregister_netdevice_many and netdev_run_todo has completed
2434 * for each network device that was in this network namespace.
2436 * As such no additional rcu synchronization is necessary when
2437 * freeing the platform_label table.
2440 platform_label
= rtnl_dereference(net
->mpls
.platform_label
);
2441 platform_labels
= net
->mpls
.platform_labels
;
2442 for (index
= 0; index
< platform_labels
; index
++) {
2443 struct mpls_route
*rt
= rtnl_dereference(platform_label
[index
]);
2444 RCU_INIT_POINTER(platform_label
[index
], NULL
);
2445 mpls_notify_route(net
, index
, rt
, NULL
, NULL
);
2450 kvfree(platform_label
);
2453 static struct pernet_operations mpls_net_ops
= {
2454 .init
= mpls_net_init
,
2455 .exit
= mpls_net_exit
,
2458 static struct rtnl_af_ops mpls_af_ops __read_mostly
= {
2460 .fill_stats_af
= mpls_fill_stats_af
,
2461 .get_stats_af_size
= mpls_get_stats_af_size
,
2464 static int __init
mpls_init(void)
2468 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr
) != 4);
2470 err
= register_pernet_subsys(&mpls_net_ops
);
2474 err
= register_netdevice_notifier(&mpls_dev_notifier
);
2476 goto out_unregister_pernet
;
2478 dev_add_pack(&mpls_packet_type
);
2480 rtnl_af_register(&mpls_af_ops
);
2482 rtnl_register(PF_MPLS
, RTM_NEWROUTE
, mpls_rtm_newroute
, NULL
, 0);
2483 rtnl_register(PF_MPLS
, RTM_DELROUTE
, mpls_rtm_delroute
, NULL
, 0);
2484 rtnl_register(PF_MPLS
, RTM_GETROUTE
, mpls_getroute
, mpls_dump_routes
,
2486 rtnl_register(PF_MPLS
, RTM_GETNETCONF
, mpls_netconf_get_devconf
,
2487 mpls_netconf_dump_devconf
, 0);
2492 out_unregister_pernet
:
2493 unregister_pernet_subsys(&mpls_net_ops
);
2496 module_init(mpls_init
);
2498 static void __exit
mpls_exit(void)
2500 rtnl_unregister_all(PF_MPLS
);
2501 rtnl_af_unregister(&mpls_af_ops
);
2502 dev_remove_pack(&mpls_packet_type
);
2503 unregister_netdevice_notifier(&mpls_dev_notifier
);
2504 unregister_pernet_subsys(&mpls_net_ops
);
2506 module_exit(mpls_exit
);
2508 MODULE_DESCRIPTION("MultiProtocol Label Switching");
2509 MODULE_LICENSE("GPL v2");
2510 MODULE_ALIAS_NETPROTO(PF_MPLS
);