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/vmalloc.h>
15 #include <net/ip_fib.h>
16 #include <net/netevent.h>
17 #include <net/netns/generic.h>
18 #if IS_ENABLED(CONFIG_IPV6)
20 #include <net/addrconf.h>
22 #include <net/nexthop.h>
25 /* Maximum number of labels to look ahead at when selecting a path of
28 #define MAX_MP_SELECT_LABELS 4
31 static int label_limit
= (1 << 20) - 1;
33 static void rtmsg_lfib(int event
, u32 label
, struct mpls_route
*rt
,
34 struct nlmsghdr
*nlh
, struct net
*net
, u32 portid
,
35 unsigned int nlm_flags
);
37 static struct mpls_route
*mpls_route_input_rcu(struct net
*net
, unsigned index
)
39 struct mpls_route
*rt
= NULL
;
41 if (index
< net
->mpls
.platform_labels
) {
42 struct mpls_route __rcu
**platform_label
=
43 rcu_dereference(net
->mpls
.platform_label
);
44 rt
= rcu_dereference(platform_label
[index
]);
49 static inline struct mpls_dev
*mpls_dev_get(const struct net_device
*dev
)
51 return rcu_dereference_rtnl(dev
->mpls_ptr
);
54 bool mpls_output_possible(const struct net_device
*dev
)
56 return dev
&& (dev
->flags
& IFF_UP
) && netif_carrier_ok(dev
);
58 EXPORT_SYMBOL_GPL(mpls_output_possible
);
60 static u8
*__mpls_nh_via(struct mpls_route
*rt
, struct mpls_nh
*nh
)
62 u8
*nh0_via
= PTR_ALIGN((u8
*)&rt
->rt_nh
[rt
->rt_nhn
], VIA_ALEN_ALIGN
);
63 int nh_index
= nh
- rt
->rt_nh
;
65 return nh0_via
+ rt
->rt_max_alen
* nh_index
;
68 static const u8
*mpls_nh_via(const struct mpls_route
*rt
,
69 const struct mpls_nh
*nh
)
71 return __mpls_nh_via((struct mpls_route
*)rt
, (struct mpls_nh
*)nh
);
74 static unsigned int mpls_nh_header_size(const struct mpls_nh
*nh
)
76 /* The size of the layer 2.5 labels to be added for this route */
77 return nh
->nh_labels
* sizeof(struct mpls_shim_hdr
);
80 unsigned int mpls_dev_mtu(const struct net_device
*dev
)
82 /* The amount of data the layer 2 frame can hold */
85 EXPORT_SYMBOL_GPL(mpls_dev_mtu
);
87 bool mpls_pkt_too_big(const struct sk_buff
*skb
, unsigned int mtu
)
92 if (skb_is_gso(skb
) && skb_gso_network_seglen(skb
) <= mtu
)
97 EXPORT_SYMBOL_GPL(mpls_pkt_too_big
);
99 static struct mpls_nh
*mpls_select_multipath(struct mpls_route
*rt
,
100 struct sk_buff
*skb
, bool bos
)
102 struct mpls_entry_decoded dec
;
103 struct mpls_shim_hdr
*hdr
;
104 bool eli_seen
= false;
109 /* No need to look further into packet if there's only
115 for (label_index
= 0; label_index
< MAX_MP_SELECT_LABELS
&& !bos
;
117 if (!pskb_may_pull(skb
, sizeof(*hdr
) * label_index
))
120 /* Read and decode the current label */
121 hdr
= mpls_hdr(skb
) + label_index
;
122 dec
= mpls_entry_decode(hdr
);
124 /* RFC6790 - reserved labels MUST NOT be used as keys
125 * for the load-balancing function
127 if (likely(dec
.label
>= MPLS_LABEL_FIRST_UNRESERVED
)) {
128 hash
= jhash_1word(dec
.label
, hash
);
130 /* The entropy label follows the entropy label
131 * indicator, so this means that the entropy
132 * label was just added to the hash - no need to
133 * go any deeper either in the label stack or in the
138 } else if (dec
.label
== MPLS_LABEL_ENTROPY
) {
143 if (bos
&& pskb_may_pull(skb
, sizeof(*hdr
) * label_index
+
144 sizeof(struct iphdr
))) {
145 const struct iphdr
*v4hdr
;
147 v4hdr
= (const struct iphdr
*)(mpls_hdr(skb
) +
149 if (v4hdr
->version
== 4) {
150 hash
= jhash_3words(ntohl(v4hdr
->saddr
),
152 v4hdr
->protocol
, hash
);
153 } else if (v4hdr
->version
== 6 &&
154 pskb_may_pull(skb
, sizeof(*hdr
) * label_index
+
155 sizeof(struct ipv6hdr
))) {
156 const struct ipv6hdr
*v6hdr
;
158 v6hdr
= (const struct ipv6hdr
*)(mpls_hdr(skb
) +
161 hash
= __ipv6_addr_jhash(&v6hdr
->saddr
, hash
);
162 hash
= __ipv6_addr_jhash(&v6hdr
->daddr
, hash
);
163 hash
= jhash_1word(v6hdr
->nexthdr
, hash
);
168 nh_index
= hash
% rt
->rt_nhn
;
170 return &rt
->rt_nh
[nh_index
];
173 static bool mpls_egress(struct mpls_route
*rt
, struct sk_buff
*skb
,
174 struct mpls_entry_decoded dec
)
176 enum mpls_payload_type payload_type
;
177 bool success
= false;
179 /* The IPv4 code below accesses through the IPv4 header
180 * checksum, which is 12 bytes into the packet.
181 * The IPv6 code below accesses through the IPv6 hop limit
182 * which is 8 bytes into the packet.
184 * For all supported cases there should always be at least 12
185 * bytes of packet data present. The IPv4 header is 20 bytes
186 * without options and the IPv6 header is always 40 bytes
189 if (!pskb_may_pull(skb
, 12))
192 payload_type
= rt
->rt_payload_type
;
193 if (payload_type
== MPT_UNSPEC
)
194 payload_type
= ip_hdr(skb
)->version
;
196 switch (payload_type
) {
198 struct iphdr
*hdr4
= ip_hdr(skb
);
199 skb
->protocol
= htons(ETH_P_IP
);
200 csum_replace2(&hdr4
->check
,
201 htons(hdr4
->ttl
<< 8),
202 htons(dec
.ttl
<< 8));
208 struct ipv6hdr
*hdr6
= ipv6_hdr(skb
);
209 skb
->protocol
= htons(ETH_P_IPV6
);
210 hdr6
->hop_limit
= dec
.ttl
;
221 static int mpls_forward(struct sk_buff
*skb
, struct net_device
*dev
,
222 struct packet_type
*pt
, struct net_device
*orig_dev
)
224 struct net
*net
= dev_net(dev
);
225 struct mpls_shim_hdr
*hdr
;
226 struct mpls_route
*rt
;
228 struct mpls_entry_decoded dec
;
229 struct net_device
*out_dev
;
230 struct mpls_dev
*mdev
;
232 unsigned int new_header_size
;
236 /* Careful this entire function runs inside of an rcu critical section */
238 mdev
= mpls_dev_get(dev
);
239 if (!mdev
|| !mdev
->input_enabled
)
242 if (skb
->pkt_type
!= PACKET_HOST
)
245 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
248 if (!pskb_may_pull(skb
, sizeof(*hdr
)))
251 /* Read and decode the label */
253 dec
= mpls_entry_decode(hdr
);
256 skb_pull(skb
, sizeof(*hdr
));
257 skb_reset_network_header(skb
);
261 rt
= mpls_route_input_rcu(net
, dec
.label
);
265 nh
= mpls_select_multipath(rt
, skb
, dec
.bos
);
269 /* Find the output device */
270 out_dev
= rcu_dereference(nh
->nh_dev
);
271 if (!mpls_output_possible(out_dev
))
274 if (skb_warn_if_lro(skb
))
277 skb_forward_csum(skb
);
279 /* Verify ttl is valid */
284 /* Verify the destination can hold the packet */
285 new_header_size
= mpls_nh_header_size(nh
);
286 mtu
= mpls_dev_mtu(out_dev
);
287 if (mpls_pkt_too_big(skb
, mtu
- new_header_size
))
290 hh_len
= LL_RESERVED_SPACE(out_dev
);
291 if (!out_dev
->header_ops
)
294 /* Ensure there is enough space for the headers in the skb */
295 if (skb_cow(skb
, hh_len
+ new_header_size
))
299 skb
->protocol
= htons(ETH_P_MPLS_UC
);
301 if (unlikely(!new_header_size
&& dec
.bos
)) {
302 /* Penultimate hop popping */
303 if (!mpls_egress(rt
, skb
, dec
))
308 skb_push(skb
, new_header_size
);
309 skb_reset_network_header(skb
);
310 /* Push the new labels */
313 for (i
= nh
->nh_labels
- 1; i
>= 0; i
--) {
314 hdr
[i
] = mpls_entry_encode(nh
->nh_label
[i
],
320 err
= neigh_xmit(nh
->nh_via_table
, out_dev
, mpls_nh_via(rt
, nh
), skb
);
322 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
331 static struct packet_type mpls_packet_type __read_mostly
= {
332 .type
= cpu_to_be16(ETH_P_MPLS_UC
),
333 .func
= mpls_forward
,
336 static const struct nla_policy rtm_mpls_policy
[RTA_MAX
+1] = {
337 [RTA_DST
] = { .type
= NLA_U32
},
338 [RTA_OIF
] = { .type
= NLA_U32
},
341 struct mpls_route_config
{
346 u8 rc_via
[MAX_VIA_ALEN
];
349 u32 rc_output_label
[MAX_NEW_LABELS
];
351 enum mpls_payload_type rc_payload_type
;
352 struct nl_info rc_nlinfo
;
353 struct rtnexthop
*rc_mp
;
357 static struct mpls_route
*mpls_rt_alloc(int num_nh
, u8 max_alen
)
359 u8 max_alen_aligned
= ALIGN(max_alen
, VIA_ALEN_ALIGN
);
360 struct mpls_route
*rt
;
362 rt
= kzalloc(ALIGN(sizeof(*rt
) + num_nh
* sizeof(*rt
->rt_nh
),
364 num_nh
* max_alen_aligned
,
368 rt
->rt_max_alen
= max_alen_aligned
;
374 static void mpls_rt_free(struct mpls_route
*rt
)
377 kfree_rcu(rt
, rt_rcu
);
380 static void mpls_notify_route(struct net
*net
, unsigned index
,
381 struct mpls_route
*old
, struct mpls_route
*new,
382 const struct nl_info
*info
)
384 struct nlmsghdr
*nlh
= info
? info
->nlh
: NULL
;
385 unsigned portid
= info
? info
->portid
: 0;
386 int event
= new ? RTM_NEWROUTE
: RTM_DELROUTE
;
387 struct mpls_route
*rt
= new ? new : old
;
388 unsigned nlm_flags
= (old
&& new) ? NLM_F_REPLACE
: 0;
389 /* Ignore reserved labels for now */
390 if (rt
&& (index
>= MPLS_LABEL_FIRST_UNRESERVED
))
391 rtmsg_lfib(event
, index
, rt
, nlh
, net
, portid
, nlm_flags
);
394 static void mpls_route_update(struct net
*net
, unsigned index
,
395 struct mpls_route
*new,
396 const struct nl_info
*info
)
398 struct mpls_route __rcu
**platform_label
;
399 struct mpls_route
*rt
;
403 platform_label
= rtnl_dereference(net
->mpls
.platform_label
);
404 rt
= rtnl_dereference(platform_label
[index
]);
405 rcu_assign_pointer(platform_label
[index
], new);
407 mpls_notify_route(net
, index
, rt
, new, info
);
409 /* If we removed a route free it now */
413 static unsigned find_free_label(struct net
*net
)
415 struct mpls_route __rcu
**platform_label
;
416 size_t platform_labels
;
419 platform_label
= rtnl_dereference(net
->mpls
.platform_label
);
420 platform_labels
= net
->mpls
.platform_labels
;
421 for (index
= MPLS_LABEL_FIRST_UNRESERVED
; index
< platform_labels
;
423 if (!rtnl_dereference(platform_label
[index
]))
426 return LABEL_NOT_SPECIFIED
;
429 #if IS_ENABLED(CONFIG_INET)
430 static struct net_device
*inet_fib_lookup_dev(struct net
*net
,
433 struct net_device
*dev
;
435 struct in_addr daddr
;
437 memcpy(&daddr
, addr
, sizeof(struct in_addr
));
438 rt
= ip_route_output(net
, daddr
.s_addr
, 0, 0, 0);
450 static struct net_device
*inet_fib_lookup_dev(struct net
*net
,
453 return ERR_PTR(-EAFNOSUPPORT
);
457 #if IS_ENABLED(CONFIG_IPV6)
458 static struct net_device
*inet6_fib_lookup_dev(struct net
*net
,
461 struct net_device
*dev
;
462 struct dst_entry
*dst
;
467 return ERR_PTR(-EAFNOSUPPORT
);
469 memset(&fl6
, 0, sizeof(fl6
));
470 memcpy(&fl6
.daddr
, addr
, sizeof(struct in6_addr
));
471 err
= ipv6_stub
->ipv6_dst_lookup(net
, NULL
, &dst
, &fl6
);
482 static struct net_device
*inet6_fib_lookup_dev(struct net
*net
,
485 return ERR_PTR(-EAFNOSUPPORT
);
489 static struct net_device
*find_outdev(struct net
*net
,
490 struct mpls_route
*rt
,
491 struct mpls_nh
*nh
, int oif
)
493 struct net_device
*dev
= NULL
;
496 switch (nh
->nh_via_table
) {
497 case NEIGH_ARP_TABLE
:
498 dev
= inet_fib_lookup_dev(net
, mpls_nh_via(rt
, nh
));
501 dev
= inet6_fib_lookup_dev(net
, mpls_nh_via(rt
, nh
));
503 case NEIGH_LINK_TABLE
:
507 dev
= dev_get_by_index(net
, oif
);
511 return ERR_PTR(-ENODEV
);
513 /* The caller is holding rtnl anyways, so release the dev reference */
519 static int mpls_nh_assign_dev(struct net
*net
, struct mpls_route
*rt
,
520 struct mpls_nh
*nh
, int oif
)
522 struct net_device
*dev
= NULL
;
525 dev
= find_outdev(net
, rt
, nh
, oif
);
532 /* Ensure this is a supported device */
534 if (!mpls_dev_get(dev
))
537 RCU_INIT_POINTER(nh
->nh_dev
, dev
);
545 static int mpls_nh_build_from_cfg(struct mpls_route_config
*cfg
,
546 struct mpls_route
*rt
)
548 struct net
*net
= cfg
->rc_nlinfo
.nl_net
;
549 struct mpls_nh
*nh
= rt
->rt_nh
;
557 /* Ensure only a supported number of labels are present */
558 if (cfg
->rc_output_labels
> MAX_NEW_LABELS
)
561 nh
->nh_labels
= cfg
->rc_output_labels
;
562 for (i
= 0; i
< nh
->nh_labels
; i
++)
563 nh
->nh_label
[i
] = cfg
->rc_output_label
[i
];
565 nh
->nh_via_table
= cfg
->rc_via_table
;
566 memcpy(__mpls_nh_via(rt
, nh
), cfg
->rc_via
, cfg
->rc_via_alen
);
567 nh
->nh_via_alen
= cfg
->rc_via_alen
;
569 err
= mpls_nh_assign_dev(net
, rt
, nh
, cfg
->rc_ifindex
);
579 static int mpls_nh_build(struct net
*net
, struct mpls_route
*rt
,
580 struct mpls_nh
*nh
, int oif
,
581 struct nlattr
*via
, struct nlattr
*newdst
)
589 err
= nla_get_labels(newdst
, MAX_NEW_LABELS
,
590 &nh
->nh_labels
, nh
->nh_label
);
595 err
= nla_get_via(via
, &nh
->nh_via_alen
, &nh
->nh_via_table
,
596 __mpls_nh_via(rt
, nh
));
600 err
= mpls_nh_assign_dev(net
, rt
, nh
, oif
);
610 static int mpls_count_nexthops(struct rtnexthop
*rtnh
, int len
,
611 u8 cfg_via_alen
, u8
*max_via_alen
)
617 *max_via_alen
= cfg_via_alen
;
623 while (rtnh_ok(rtnh
, remaining
)) {
624 struct nlattr
*nla
, *attrs
= rtnh_attrs(rtnh
);
627 attrlen
= rtnh_attrlen(rtnh
);
628 nla
= nla_find(attrs
, attrlen
, RTA_VIA
);
629 if (nla
&& nla_len(nla
) >=
630 offsetof(struct rtvia
, rtvia_addr
)) {
631 int via_alen
= nla_len(nla
) -
632 offsetof(struct rtvia
, rtvia_addr
);
634 if (via_alen
<= MAX_VIA_ALEN
)
635 *max_via_alen
= max_t(u16
, *max_via_alen
,
640 rtnh
= rtnh_next(rtnh
, &remaining
);
643 /* leftover implies invalid nexthop configuration, discard it */
644 return remaining
> 0 ? 0 : nhs
;
647 static int mpls_nh_build_multi(struct mpls_route_config
*cfg
,
648 struct mpls_route
*rt
)
650 struct rtnexthop
*rtnh
= cfg
->rc_mp
;
651 struct nlattr
*nla_via
, *nla_newdst
;
652 int remaining
= cfg
->rc_mp_len
;
656 change_nexthops(rt
) {
663 if (!rtnh_ok(rtnh
, remaining
))
666 /* neither weighted multipath nor any flags
669 if (rtnh
->rtnh_hops
|| rtnh
->rtnh_flags
)
672 attrlen
= rtnh_attrlen(rtnh
);
674 struct nlattr
*attrs
= rtnh_attrs(rtnh
);
676 nla_via
= nla_find(attrs
, attrlen
, RTA_VIA
);
677 nla_newdst
= nla_find(attrs
, attrlen
, RTA_NEWDST
);
683 err
= mpls_nh_build(cfg
->rc_nlinfo
.nl_net
, rt
, nh
,
684 rtnh
->rtnh_ifindex
, nla_via
,
689 rtnh
= rtnh_next(rtnh
, &remaining
);
691 } endfor_nexthops(rt
);
701 static int mpls_route_add(struct mpls_route_config
*cfg
)
703 struct mpls_route __rcu
**platform_label
;
704 struct net
*net
= cfg
->rc_nlinfo
.nl_net
;
705 struct mpls_route
*rt
, *old
;
711 index
= cfg
->rc_label
;
713 /* If a label was not specified during insert pick one */
714 if ((index
== LABEL_NOT_SPECIFIED
) &&
715 (cfg
->rc_nlflags
& NLM_F_CREATE
)) {
716 index
= find_free_label(net
);
719 /* Reserved labels may not be set */
720 if (index
< MPLS_LABEL_FIRST_UNRESERVED
)
723 /* The full 20 bit range may not be supported. */
724 if (index
>= net
->mpls
.platform_labels
)
727 /* Append makes no sense with mpls */
729 if (cfg
->rc_nlflags
& NLM_F_APPEND
)
733 platform_label
= rtnl_dereference(net
->mpls
.platform_label
);
734 old
= rtnl_dereference(platform_label
[index
]);
735 if ((cfg
->rc_nlflags
& NLM_F_EXCL
) && old
)
739 if (!(cfg
->rc_nlflags
& NLM_F_REPLACE
) && old
)
743 if (!(cfg
->rc_nlflags
& NLM_F_CREATE
) && !old
)
747 nhs
= mpls_count_nexthops(cfg
->rc_mp
, cfg
->rc_mp_len
,
748 cfg
->rc_via_alen
, &max_via_alen
);
753 rt
= mpls_rt_alloc(nhs
, max_via_alen
);
757 rt
->rt_protocol
= cfg
->rc_protocol
;
758 rt
->rt_payload_type
= cfg
->rc_payload_type
;
761 err
= mpls_nh_build_multi(cfg
, rt
);
763 err
= mpls_nh_build_from_cfg(cfg
, rt
);
767 mpls_route_update(net
, index
, rt
, &cfg
->rc_nlinfo
);
777 static int mpls_route_del(struct mpls_route_config
*cfg
)
779 struct net
*net
= cfg
->rc_nlinfo
.nl_net
;
783 index
= cfg
->rc_label
;
785 /* Reserved labels may not be removed */
786 if (index
< MPLS_LABEL_FIRST_UNRESERVED
)
789 /* The full 20 bit range may not be supported */
790 if (index
>= net
->mpls
.platform_labels
)
793 mpls_route_update(net
, index
, NULL
, &cfg
->rc_nlinfo
);
800 #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
801 (&((struct mpls_dev *)0)->field)
803 static const struct ctl_table mpls_dev_table
[] = {
806 .maxlen
= sizeof(int),
808 .proc_handler
= proc_dointvec
,
809 .data
= MPLS_PERDEV_SYSCTL_OFFSET(input_enabled
),
814 static int mpls_dev_sysctl_register(struct net_device
*dev
,
815 struct mpls_dev
*mdev
)
817 char path
[sizeof("net/mpls/conf/") + IFNAMSIZ
];
818 struct ctl_table
*table
;
821 table
= kmemdup(&mpls_dev_table
, sizeof(mpls_dev_table
), GFP_KERNEL
);
825 /* Table data contains only offsets relative to the base of
826 * the mdev at this point, so make them absolute.
828 for (i
= 0; i
< ARRAY_SIZE(mpls_dev_table
); i
++)
829 table
[i
].data
= (char *)mdev
+ (uintptr_t)table
[i
].data
;
831 snprintf(path
, sizeof(path
), "net/mpls/conf/%s", dev
->name
);
833 mdev
->sysctl
= register_net_sysctl(dev_net(dev
), path
, table
);
845 static void mpls_dev_sysctl_unregister(struct mpls_dev
*mdev
)
847 struct ctl_table
*table
;
849 table
= mdev
->sysctl
->ctl_table_arg
;
850 unregister_net_sysctl_table(mdev
->sysctl
);
854 static struct mpls_dev
*mpls_add_dev(struct net_device
*dev
)
856 struct mpls_dev
*mdev
;
861 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
865 err
= mpls_dev_sysctl_register(dev
, mdev
);
869 rcu_assign_pointer(dev
->mpls_ptr
, mdev
);
878 static void mpls_ifdown(struct net_device
*dev
)
880 struct mpls_route __rcu
**platform_label
;
881 struct net
*net
= dev_net(dev
);
882 struct mpls_dev
*mdev
;
885 platform_label
= rtnl_dereference(net
->mpls
.platform_label
);
886 for (index
= 0; index
< net
->mpls
.platform_labels
; index
++) {
887 struct mpls_route
*rt
= rtnl_dereference(platform_label
[index
]);
891 if (rtnl_dereference(nh
->nh_dev
) != dev
)
894 } endfor_nexthops(rt
);
897 mdev
= mpls_dev_get(dev
);
901 mpls_dev_sysctl_unregister(mdev
);
903 RCU_INIT_POINTER(dev
->mpls_ptr
, NULL
);
905 kfree_rcu(mdev
, rcu
);
908 static int mpls_dev_notify(struct notifier_block
*this, unsigned long event
,
911 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
912 struct mpls_dev
*mdev
;
915 case NETDEV_REGISTER
:
916 /* For now just support ethernet devices */
917 if ((dev
->type
== ARPHRD_ETHER
) ||
918 (dev
->type
== ARPHRD_LOOPBACK
)) {
919 mdev
= mpls_add_dev(dev
);
921 return notifier_from_errno(PTR_ERR(mdev
));
925 case NETDEV_UNREGISTER
:
928 case NETDEV_CHANGENAME
:
929 mdev
= mpls_dev_get(dev
);
933 mpls_dev_sysctl_unregister(mdev
);
934 err
= mpls_dev_sysctl_register(dev
, mdev
);
936 return notifier_from_errno(err
);
943 static struct notifier_block mpls_dev_notifier
= {
944 .notifier_call
= mpls_dev_notify
,
947 static int nla_put_via(struct sk_buff
*skb
,
948 u8 table
, const void *addr
, int alen
)
950 static const int table_to_family
[NEIGH_NR_TABLES
+ 1] = {
951 AF_INET
, AF_INET6
, AF_DECnet
, AF_PACKET
,
955 int family
= AF_UNSPEC
;
957 nla
= nla_reserve(skb
, RTA_VIA
, alen
+ 2);
961 if (table
<= NEIGH_NR_TABLES
)
962 family
= table_to_family
[table
];
965 via
->rtvia_family
= family
;
966 memcpy(via
->rtvia_addr
, addr
, alen
);
970 int nla_put_labels(struct sk_buff
*skb
, int attrtype
,
971 u8 labels
, const u32 label
[])
974 struct mpls_shim_hdr
*nla_label
;
977 nla
= nla_reserve(skb
, attrtype
, labels
*4);
981 nla_label
= nla_data(nla
);
983 for (i
= labels
- 1; i
>= 0; i
--) {
984 nla_label
[i
] = mpls_entry_encode(label
[i
], 0, 0, bos
);
990 EXPORT_SYMBOL_GPL(nla_put_labels
);
992 int nla_get_labels(const struct nlattr
*nla
,
993 u32 max_labels
, u8
*labels
, u32 label
[])
995 unsigned len
= nla_len(nla
);
997 struct mpls_shim_hdr
*nla_label
;
1001 /* len needs to be an even multiple of 4 (the label size) */
1005 /* Limit the number of new labels allowed */
1007 if (nla_labels
> max_labels
)
1010 nla_label
= nla_data(nla
);
1012 for (i
= nla_labels
- 1; i
>= 0; i
--, bos
= false) {
1013 struct mpls_entry_decoded dec
;
1014 dec
= mpls_entry_decode(nla_label
+ i
);
1016 /* Ensure the bottom of stack flag is properly set
1017 * and ttl and tc are both clear.
1019 if ((dec
.bos
!= bos
) || dec
.ttl
|| dec
.tc
)
1022 switch (dec
.label
) {
1023 case MPLS_LABEL_IMPLNULL
:
1024 /* RFC3032: This is a label that an LSR may
1025 * assign and distribute, but which never
1026 * actually appears in the encapsulation.
1031 label
[i
] = dec
.label
;
1033 *labels
= nla_labels
;
1036 EXPORT_SYMBOL_GPL(nla_get_labels
);
1038 int nla_get_via(const struct nlattr
*nla
, u8
*via_alen
,
1039 u8
*via_table
, u8 via_addr
[])
1041 struct rtvia
*via
= nla_data(nla
);
1045 if (nla_len(nla
) < offsetof(struct rtvia
, rtvia_addr
))
1047 alen
= nla_len(nla
) -
1048 offsetof(struct rtvia
, rtvia_addr
);
1049 if (alen
> MAX_VIA_ALEN
)
1052 /* Validate the address family */
1053 switch (via
->rtvia_family
) {
1055 *via_table
= NEIGH_LINK_TABLE
;
1058 *via_table
= NEIGH_ARP_TABLE
;
1063 *via_table
= NEIGH_ND_TABLE
;
1068 /* Unsupported address family */
1072 memcpy(via_addr
, via
->rtvia_addr
, alen
);
1080 static int rtm_to_route_config(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1081 struct mpls_route_config
*cfg
)
1084 struct nlattr
*tb
[RTA_MAX
+1];
1088 err
= nlmsg_parse(nlh
, sizeof(*rtm
), tb
, RTA_MAX
, rtm_mpls_policy
);
1093 rtm
= nlmsg_data(nlh
);
1094 memset(cfg
, 0, sizeof(*cfg
));
1096 if (rtm
->rtm_family
!= AF_MPLS
)
1098 if (rtm
->rtm_dst_len
!= 20)
1100 if (rtm
->rtm_src_len
!= 0)
1102 if (rtm
->rtm_tos
!= 0)
1104 if (rtm
->rtm_table
!= RT_TABLE_MAIN
)
1106 /* Any value is acceptable for rtm_protocol */
1108 /* As mpls uses destination specific addresses
1109 * (or source specific address in the case of multicast)
1110 * all addresses have universal scope.
1112 if (rtm
->rtm_scope
!= RT_SCOPE_UNIVERSE
)
1114 if (rtm
->rtm_type
!= RTN_UNICAST
)
1116 if (rtm
->rtm_flags
!= 0)
1119 cfg
->rc_label
= LABEL_NOT_SPECIFIED
;
1120 cfg
->rc_protocol
= rtm
->rtm_protocol
;
1121 cfg
->rc_nlflags
= nlh
->nlmsg_flags
;
1122 cfg
->rc_nlinfo
.portid
= NETLINK_CB(skb
).portid
;
1123 cfg
->rc_nlinfo
.nlh
= nlh
;
1124 cfg
->rc_nlinfo
.nl_net
= sock_net(skb
->sk
);
1126 for (index
= 0; index
<= RTA_MAX
; index
++) {
1127 struct nlattr
*nla
= tb
[index
];
1133 cfg
->rc_ifindex
= nla_get_u32(nla
);
1136 if (nla_get_labels(nla
, MAX_NEW_LABELS
,
1137 &cfg
->rc_output_labels
,
1138 cfg
->rc_output_label
))
1144 if (nla_get_labels(nla
, 1, &label_count
,
1148 /* Reserved labels may not be set */
1149 if (cfg
->rc_label
< MPLS_LABEL_FIRST_UNRESERVED
)
1156 if (nla_get_via(nla
, &cfg
->rc_via_alen
,
1157 &cfg
->rc_via_table
, cfg
->rc_via
))
1163 cfg
->rc_mp
= nla_data(nla
);
1164 cfg
->rc_mp_len
= nla_len(nla
);
1168 /* Unsupported attribute */
1178 static int mpls_rtm_delroute(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1180 struct mpls_route_config cfg
;
1183 err
= rtm_to_route_config(skb
, nlh
, &cfg
);
1187 return mpls_route_del(&cfg
);
1191 static int mpls_rtm_newroute(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1193 struct mpls_route_config cfg
;
1196 err
= rtm_to_route_config(skb
, nlh
, &cfg
);
1200 return mpls_route_add(&cfg
);
1203 static int mpls_dump_route(struct sk_buff
*skb
, u32 portid
, u32 seq
, int event
,
1204 u32 label
, struct mpls_route
*rt
, int flags
)
1206 struct net_device
*dev
;
1207 struct nlmsghdr
*nlh
;
1210 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*rtm
), flags
);
1214 rtm
= nlmsg_data(nlh
);
1215 rtm
->rtm_family
= AF_MPLS
;
1216 rtm
->rtm_dst_len
= 20;
1217 rtm
->rtm_src_len
= 0;
1219 rtm
->rtm_table
= RT_TABLE_MAIN
;
1220 rtm
->rtm_protocol
= rt
->rt_protocol
;
1221 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1222 rtm
->rtm_type
= RTN_UNICAST
;
1225 if (nla_put_labels(skb
, RTA_DST
, 1, &label
))
1226 goto nla_put_failure
;
1227 if (rt
->rt_nhn
== 1) {
1228 const struct mpls_nh
*nh
= rt
->rt_nh
;
1230 if (nh
->nh_labels
&&
1231 nla_put_labels(skb
, RTA_NEWDST
, nh
->nh_labels
,
1233 goto nla_put_failure
;
1234 if (nla_put_via(skb
, nh
->nh_via_table
, mpls_nh_via(rt
, nh
),
1236 goto nla_put_failure
;
1237 dev
= rtnl_dereference(nh
->nh_dev
);
1238 if (dev
&& nla_put_u32(skb
, RTA_OIF
, dev
->ifindex
))
1239 goto nla_put_failure
;
1241 struct rtnexthop
*rtnh
;
1244 mp
= nla_nest_start(skb
, RTA_MULTIPATH
);
1246 goto nla_put_failure
;
1249 rtnh
= nla_reserve_nohdr(skb
, sizeof(*rtnh
));
1251 goto nla_put_failure
;
1253 dev
= rtnl_dereference(nh
->nh_dev
);
1255 rtnh
->rtnh_ifindex
= dev
->ifindex
;
1256 if (nh
->nh_labels
&& nla_put_labels(skb
, RTA_NEWDST
,
1259 goto nla_put_failure
;
1260 if (nla_put_via(skb
, nh
->nh_via_table
,
1261 mpls_nh_via(rt
, nh
),
1263 goto nla_put_failure
;
1265 /* length of rtnetlink header + attributes */
1266 rtnh
->rtnh_len
= nlmsg_get_pos(skb
) - (void *)rtnh
;
1267 } endfor_nexthops(rt
);
1269 nla_nest_end(skb
, mp
);
1272 nlmsg_end(skb
, nlh
);
1276 nlmsg_cancel(skb
, nlh
);
1280 static int mpls_dump_routes(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1282 struct net
*net
= sock_net(skb
->sk
);
1283 struct mpls_route __rcu
**platform_label
;
1284 size_t platform_labels
;
1289 index
= cb
->args
[0];
1290 if (index
< MPLS_LABEL_FIRST_UNRESERVED
)
1291 index
= MPLS_LABEL_FIRST_UNRESERVED
;
1293 platform_label
= rtnl_dereference(net
->mpls
.platform_label
);
1294 platform_labels
= net
->mpls
.platform_labels
;
1295 for (; index
< platform_labels
; index
++) {
1296 struct mpls_route
*rt
;
1297 rt
= rtnl_dereference(platform_label
[index
]);
1301 if (mpls_dump_route(skb
, NETLINK_CB(cb
->skb
).portid
,
1302 cb
->nlh
->nlmsg_seq
, RTM_NEWROUTE
,
1303 index
, rt
, NLM_F_MULTI
) < 0)
1306 cb
->args
[0] = index
;
1311 static inline size_t lfib_nlmsg_size(struct mpls_route
*rt
)
1314 NLMSG_ALIGN(sizeof(struct rtmsg
))
1315 + nla_total_size(4); /* RTA_DST */
1317 if (rt
->rt_nhn
== 1) {
1318 struct mpls_nh
*nh
= rt
->rt_nh
;
1321 payload
+= nla_total_size(4); /* RTA_OIF */
1322 payload
+= nla_total_size(2 + nh
->nh_via_alen
); /* RTA_VIA */
1323 if (nh
->nh_labels
) /* RTA_NEWDST */
1324 payload
+= nla_total_size(nh
->nh_labels
* 4);
1326 /* each nexthop is packed in an attribute */
1330 nhsize
+= nla_total_size(sizeof(struct rtnexthop
));
1331 nhsize
+= nla_total_size(2 + nh
->nh_via_alen
);
1333 nhsize
+= nla_total_size(nh
->nh_labels
* 4);
1334 } endfor_nexthops(rt
);
1335 /* nested attribute */
1336 payload
+= nla_total_size(nhsize
);
1342 static void rtmsg_lfib(int event
, u32 label
, struct mpls_route
*rt
,
1343 struct nlmsghdr
*nlh
, struct net
*net
, u32 portid
,
1344 unsigned int nlm_flags
)
1346 struct sk_buff
*skb
;
1347 u32 seq
= nlh
? nlh
->nlmsg_seq
: 0;
1350 skb
= nlmsg_new(lfib_nlmsg_size(rt
), GFP_KERNEL
);
1354 err
= mpls_dump_route(skb
, portid
, seq
, event
, label
, rt
, nlm_flags
);
1356 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1357 WARN_ON(err
== -EMSGSIZE
);
1361 rtnl_notify(skb
, net
, portid
, RTNLGRP_MPLS_ROUTE
, nlh
, GFP_KERNEL
);
1366 rtnl_set_sk_err(net
, RTNLGRP_MPLS_ROUTE
, err
);
1369 static int resize_platform_label_table(struct net
*net
, size_t limit
)
1371 size_t size
= sizeof(struct mpls_route
*) * limit
;
1374 struct mpls_route __rcu
**labels
= NULL
, **old
;
1375 struct mpls_route
*rt0
= NULL
, *rt2
= NULL
;
1379 labels
= kzalloc(size
, GFP_KERNEL
| __GFP_NOWARN
| __GFP_NORETRY
);
1381 labels
= vzalloc(size
);
1387 /* In case the predefined labels need to be populated */
1388 if (limit
> MPLS_LABEL_IPV4NULL
) {
1389 struct net_device
*lo
= net
->loopback_dev
;
1390 rt0
= mpls_rt_alloc(1, lo
->addr_len
);
1393 RCU_INIT_POINTER(rt0
->rt_nh
->nh_dev
, lo
);
1394 rt0
->rt_protocol
= RTPROT_KERNEL
;
1395 rt0
->rt_payload_type
= MPT_IPV4
;
1396 rt0
->rt_nh
->nh_via_table
= NEIGH_LINK_TABLE
;
1397 rt0
->rt_nh
->nh_via_alen
= lo
->addr_len
;
1398 memcpy(__mpls_nh_via(rt0
, rt0
->rt_nh
), lo
->dev_addr
,
1401 if (limit
> MPLS_LABEL_IPV6NULL
) {
1402 struct net_device
*lo
= net
->loopback_dev
;
1403 rt2
= mpls_rt_alloc(1, lo
->addr_len
);
1406 RCU_INIT_POINTER(rt2
->rt_nh
->nh_dev
, lo
);
1407 rt2
->rt_protocol
= RTPROT_KERNEL
;
1408 rt2
->rt_payload_type
= MPT_IPV6
;
1409 rt2
->rt_nh
->nh_via_table
= NEIGH_LINK_TABLE
;
1410 rt2
->rt_nh
->nh_via_alen
= lo
->addr_len
;
1411 memcpy(__mpls_nh_via(rt2
, rt2
->rt_nh
), lo
->dev_addr
,
1416 /* Remember the original table */
1417 old
= rtnl_dereference(net
->mpls
.platform_label
);
1418 old_limit
= net
->mpls
.platform_labels
;
1420 /* Free any labels beyond the new table */
1421 for (index
= limit
; index
< old_limit
; index
++)
1422 mpls_route_update(net
, index
, NULL
, NULL
);
1424 /* Copy over the old labels */
1426 if (old_limit
< limit
)
1427 cp_size
= old_limit
* sizeof(struct mpls_route
*);
1429 memcpy(labels
, old
, cp_size
);
1431 /* If needed set the predefined labels */
1432 if ((old_limit
<= MPLS_LABEL_IPV6NULL
) &&
1433 (limit
> MPLS_LABEL_IPV6NULL
)) {
1434 RCU_INIT_POINTER(labels
[MPLS_LABEL_IPV6NULL
], rt2
);
1438 if ((old_limit
<= MPLS_LABEL_IPV4NULL
) &&
1439 (limit
> MPLS_LABEL_IPV4NULL
)) {
1440 RCU_INIT_POINTER(labels
[MPLS_LABEL_IPV4NULL
], rt0
);
1444 /* Update the global pointers */
1445 net
->mpls
.platform_labels
= limit
;
1446 rcu_assign_pointer(net
->mpls
.platform_label
, labels
);
1467 static int mpls_platform_labels(struct ctl_table
*table
, int write
,
1468 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1470 struct net
*net
= table
->data
;
1471 int platform_labels
= net
->mpls
.platform_labels
;
1473 struct ctl_table tmp
= {
1474 .procname
= table
->procname
,
1475 .data
= &platform_labels
,
1476 .maxlen
= sizeof(int),
1477 .mode
= table
->mode
,
1479 .extra2
= &label_limit
,
1482 ret
= proc_dointvec_minmax(&tmp
, write
, buffer
, lenp
, ppos
);
1484 if (write
&& ret
== 0)
1485 ret
= resize_platform_label_table(net
, platform_labels
);
1490 static const struct ctl_table mpls_table
[] = {
1492 .procname
= "platform_labels",
1494 .maxlen
= sizeof(int),
1496 .proc_handler
= mpls_platform_labels
,
1501 static int mpls_net_init(struct net
*net
)
1503 struct ctl_table
*table
;
1505 net
->mpls
.platform_labels
= 0;
1506 net
->mpls
.platform_label
= NULL
;
1508 table
= kmemdup(mpls_table
, sizeof(mpls_table
), GFP_KERNEL
);
1512 table
[0].data
= net
;
1513 net
->mpls
.ctl
= register_net_sysctl(net
, "net/mpls", table
);
1514 if (net
->mpls
.ctl
== NULL
) {
1522 static void mpls_net_exit(struct net
*net
)
1524 struct mpls_route __rcu
**platform_label
;
1525 size_t platform_labels
;
1526 struct ctl_table
*table
;
1529 table
= net
->mpls
.ctl
->ctl_table_arg
;
1530 unregister_net_sysctl_table(net
->mpls
.ctl
);
1533 /* An rcu grace period has passed since there was a device in
1534 * the network namespace (and thus the last in flight packet)
1535 * left this network namespace. This is because
1536 * unregister_netdevice_many and netdev_run_todo has completed
1537 * for each network device that was in this network namespace.
1539 * As such no additional rcu synchronization is necessary when
1540 * freeing the platform_label table.
1543 platform_label
= rtnl_dereference(net
->mpls
.platform_label
);
1544 platform_labels
= net
->mpls
.platform_labels
;
1545 for (index
= 0; index
< platform_labels
; index
++) {
1546 struct mpls_route
*rt
= rtnl_dereference(platform_label
[index
]);
1547 RCU_INIT_POINTER(platform_label
[index
], NULL
);
1552 kvfree(platform_label
);
1555 static struct pernet_operations mpls_net_ops
= {
1556 .init
= mpls_net_init
,
1557 .exit
= mpls_net_exit
,
1560 static int __init
mpls_init(void)
1564 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr
) != 4);
1566 err
= register_pernet_subsys(&mpls_net_ops
);
1570 err
= register_netdevice_notifier(&mpls_dev_notifier
);
1572 goto out_unregister_pernet
;
1574 dev_add_pack(&mpls_packet_type
);
1576 rtnl_register(PF_MPLS
, RTM_NEWROUTE
, mpls_rtm_newroute
, NULL
, NULL
);
1577 rtnl_register(PF_MPLS
, RTM_DELROUTE
, mpls_rtm_delroute
, NULL
, NULL
);
1578 rtnl_register(PF_MPLS
, RTM_GETROUTE
, NULL
, mpls_dump_routes
, NULL
);
1583 out_unregister_pernet
:
1584 unregister_pernet_subsys(&mpls_net_ops
);
1587 module_init(mpls_init
);
1589 static void __exit
mpls_exit(void)
1591 rtnl_unregister_all(PF_MPLS
);
1592 dev_remove_pack(&mpls_packet_type
);
1593 unregister_netdevice_notifier(&mpls_dev_notifier
);
1594 unregister_pernet_subsys(&mpls_net_ops
);
1596 module_exit(mpls_exit
);
1598 MODULE_DESCRIPTION("MultiProtocol Label Switching");
1599 MODULE_LICENSE("GPL v2");
1600 MODULE_ALIAS_NETPROTO(PF_MPLS
);