blob: ee3097a971852dcfe2a65ac47feeb1144dc3c95b [file] [log] [blame]
Eric W. Biederman01891972015-03-03 19:10:47 -06001#include <linux/types.h>
2#include <linux/skbuff.h>
3#include <linux/socket.h>
Eric W. Biederman7720c012015-03-03 19:11:20 -06004#include <linux/sysctl.h>
Eric W. Biederman01891972015-03-03 19:10:47 -06005#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>
Stephen Rothwell4b5edb22015-03-05 13:37:05 +110010#include <linux/vmalloc.h>
Eric W. Biederman01891972015-03-03 19:10:47 -060011#include <net/ip.h>
12#include <net/dst.h>
13#include <net/sock.h>
14#include <net/arp.h>
15#include <net/ip_fib.h>
16#include <net/netevent.h>
17#include <net/netns/generic.h>
Roopa Prabhubf215632015-07-30 13:34:54 -070018#if IS_ENABLED(CONFIG_IPV6)
19#include <net/ipv6.h>
20#include <net/addrconf.h>
21#endif
Roopa Prabhuf8efb732015-10-23 06:03:27 -070022#include <net/nexthop.h>
Eric W. Biederman01891972015-03-03 19:10:47 -060023#include "internal.h"
24
Eric W. Biederman7720c012015-03-03 19:11:20 -060025static int zero = 0;
26static int label_limit = (1 << 20) - 1;
27
Eric W. Biederman8de147d2015-03-03 19:14:31 -060028static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
29 struct nlmsghdr *nlh, struct net *net, u32 portid,
30 unsigned int nlm_flags);
31
Eric W. Biederman01891972015-03-03 19:10:47 -060032static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
33{
34 struct mpls_route *rt = NULL;
35
36 if (index < net->mpls.platform_labels) {
37 struct mpls_route __rcu **platform_label =
38 rcu_dereference(net->mpls.platform_label);
39 rt = rcu_dereference(platform_label[index]);
40 }
41 return rt;
42}
43
Robert Shearman03c57742015-04-22 11:14:37 +010044static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
45{
46 return rcu_dereference_rtnl(dev->mpls_ptr);
47}
48
Roopa Prabhuface0182015-07-21 10:43:52 +020049bool mpls_output_possible(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060050{
51 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
52}
Roopa Prabhuface0182015-07-21 10:43:52 +020053EXPORT_SYMBOL_GPL(mpls_output_possible);
Eric W. Biederman01891972015-03-03 19:10:47 -060054
Roopa Prabhuf8efb732015-10-23 06:03:27 -070055static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
Eric W. Biederman01891972015-03-03 19:10:47 -060056{
57 /* The size of the layer 2.5 labels to be added for this route */
Roopa Prabhuf8efb732015-10-23 06:03:27 -070058 return nh->nh_labels * sizeof(struct mpls_shim_hdr);
Eric W. Biederman01891972015-03-03 19:10:47 -060059}
60
Roopa Prabhuface0182015-07-21 10:43:52 +020061unsigned int mpls_dev_mtu(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060062{
63 /* The amount of data the layer 2 frame can hold */
64 return dev->mtu;
65}
Roopa Prabhuface0182015-07-21 10:43:52 +020066EXPORT_SYMBOL_GPL(mpls_dev_mtu);
Eric W. Biederman01891972015-03-03 19:10:47 -060067
Roopa Prabhuface0182015-07-21 10:43:52 +020068bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
Eric W. Biederman01891972015-03-03 19:10:47 -060069{
70 if (skb->len <= mtu)
71 return false;
72
73 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
74 return false;
75
76 return true;
77}
Roopa Prabhuface0182015-07-21 10:43:52 +020078EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
Eric W. Biederman01891972015-03-03 19:10:47 -060079
Roopa Prabhuf8efb732015-10-23 06:03:27 -070080static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt)
81{
82 /* assume single nexthop for now */
83 return &rt->rt_nh[0];
84}
85
Eric W. Biederman01891972015-03-03 19:10:47 -060086static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
87 struct mpls_entry_decoded dec)
88{
Robert Shearman118d5232015-08-06 11:04:56 +010089 enum mpls_payload_type payload_type;
90 bool success = false;
Eric W. Biederman01891972015-03-03 19:10:47 -060091
Eric W. Biederman76fecd82015-03-12 18:22:59 -050092 /* The IPv4 code below accesses through the IPv4 header
93 * checksum, which is 12 bytes into the packet.
94 * The IPv6 code below accesses through the IPv6 hop limit
95 * which is 8 bytes into the packet.
96 *
97 * For all supported cases there should always be at least 12
98 * bytes of packet data present. The IPv4 header is 20 bytes
99 * without options and the IPv6 header is always 40 bytes
100 * long.
101 */
102 if (!pskb_may_pull(skb, 12))
103 return false;
104
Robert Shearman118d5232015-08-06 11:04:56 +0100105 payload_type = rt->rt_payload_type;
106 if (payload_type == MPT_UNSPEC)
107 payload_type = ip_hdr(skb)->version;
108
109 switch (payload_type) {
110 case MPT_IPV4: {
111 struct iphdr *hdr4 = ip_hdr(skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600112 skb->protocol = htons(ETH_P_IP);
113 csum_replace2(&hdr4->check,
114 htons(hdr4->ttl << 8),
115 htons(dec.ttl << 8));
116 hdr4->ttl = dec.ttl;
Robert Shearman118d5232015-08-06 11:04:56 +0100117 success = true;
118 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600119 }
Robert Shearman118d5232015-08-06 11:04:56 +0100120 case MPT_IPV6: {
Eric W. Biederman01891972015-03-03 19:10:47 -0600121 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
122 skb->protocol = htons(ETH_P_IPV6);
123 hdr6->hop_limit = dec.ttl;
Robert Shearman118d5232015-08-06 11:04:56 +0100124 success = true;
125 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600126 }
Robert Shearman118d5232015-08-06 11:04:56 +0100127 case MPT_UNSPEC:
128 break;
129 }
130
Eric W. Biederman01891972015-03-03 19:10:47 -0600131 return success;
132}
133
134static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
135 struct packet_type *pt, struct net_device *orig_dev)
136{
137 struct net *net = dev_net(dev);
138 struct mpls_shim_hdr *hdr;
139 struct mpls_route *rt;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700140 struct mpls_nh *nh;
Eric W. Biederman01891972015-03-03 19:10:47 -0600141 struct mpls_entry_decoded dec;
142 struct net_device *out_dev;
Robert Shearman03c57742015-04-22 11:14:37 +0100143 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600144 unsigned int hh_len;
145 unsigned int new_header_size;
146 unsigned int mtu;
147 int err;
148
149 /* Careful this entire function runs inside of an rcu critical section */
150
Robert Shearman03c57742015-04-22 11:14:37 +0100151 mdev = mpls_dev_get(dev);
Robert Shearman37bde792015-04-22 11:14:38 +0100152 if (!mdev || !mdev->input_enabled)
Robert Shearman03c57742015-04-22 11:14:37 +0100153 goto drop;
154
Eric W. Biederman01891972015-03-03 19:10:47 -0600155 if (skb->pkt_type != PACKET_HOST)
156 goto drop;
157
158 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
159 goto drop;
160
161 if (!pskb_may_pull(skb, sizeof(*hdr)))
162 goto drop;
163
164 /* Read and decode the label */
165 hdr = mpls_hdr(skb);
166 dec = mpls_entry_decode(hdr);
167
168 /* Pop the label */
169 skb_pull(skb, sizeof(*hdr));
170 skb_reset_network_header(skb);
171
172 skb_orphan(skb);
173
174 rt = mpls_route_input_rcu(net, dec.label);
175 if (!rt)
176 goto drop;
177
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700178 nh = mpls_select_multipath(rt);
179 if (!nh)
180 goto drop;
181
Eric W. Biederman01891972015-03-03 19:10:47 -0600182 /* Find the output device */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700183 out_dev = rcu_dereference(nh->nh_dev);
Eric W. Biederman01891972015-03-03 19:10:47 -0600184 if (!mpls_output_possible(out_dev))
185 goto drop;
186
187 if (skb_warn_if_lro(skb))
188 goto drop;
189
190 skb_forward_csum(skb);
191
192 /* Verify ttl is valid */
Eric W. Biedermanaa7da932015-03-07 16:23:23 -0600193 if (dec.ttl <= 1)
Eric W. Biederman01891972015-03-03 19:10:47 -0600194 goto drop;
195 dec.ttl -= 1;
196
197 /* Verify the destination can hold the packet */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700198 new_header_size = mpls_nh_header_size(nh);
Eric W. Biederman01891972015-03-03 19:10:47 -0600199 mtu = mpls_dev_mtu(out_dev);
200 if (mpls_pkt_too_big(skb, mtu - new_header_size))
201 goto drop;
202
203 hh_len = LL_RESERVED_SPACE(out_dev);
204 if (!out_dev->header_ops)
205 hh_len = 0;
206
207 /* Ensure there is enough space for the headers in the skb */
208 if (skb_cow(skb, hh_len + new_header_size))
209 goto drop;
210
211 skb->dev = out_dev;
212 skb->protocol = htons(ETH_P_MPLS_UC);
213
214 if (unlikely(!new_header_size && dec.bos)) {
215 /* Penultimate hop popping */
216 if (!mpls_egress(rt, skb, dec))
217 goto drop;
218 } else {
219 bool bos;
220 int i;
221 skb_push(skb, new_header_size);
222 skb_reset_network_header(skb);
223 /* Push the new labels */
224 hdr = mpls_hdr(skb);
225 bos = dec.bos;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700226 for (i = nh->nh_labels - 1; i >= 0; i--) {
227 hdr[i] = mpls_entry_encode(nh->nh_label[i],
228 dec.ttl, 0, bos);
Eric W. Biederman01891972015-03-03 19:10:47 -0600229 bos = false;
230 }
231 }
232
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700233 err = neigh_xmit(nh->nh_via_table, out_dev, nh->nh_via, skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600234 if (err)
235 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
236 __func__, err);
237 return 0;
238
239drop:
240 kfree_skb(skb);
241 return NET_RX_DROP;
242}
243
244static struct packet_type mpls_packet_type __read_mostly = {
245 .type = cpu_to_be16(ETH_P_MPLS_UC),
246 .func = mpls_forward,
247};
248
Wu Fengguangf0126532015-03-05 05:33:54 +0800249static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600250 [RTA_DST] = { .type = NLA_U32 },
251 [RTA_OIF] = { .type = NLA_U32 },
252};
253
Eric W. Biedermana2519922015-03-03 19:12:40 -0600254struct mpls_route_config {
Robert Shearman118d5232015-08-06 11:04:56 +0100255 u32 rc_protocol;
256 u32 rc_ifindex;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700257 u8 rc_via_table;
258 u8 rc_via_alen;
Robert Shearman118d5232015-08-06 11:04:56 +0100259 u8 rc_via[MAX_VIA_ALEN];
260 u32 rc_label;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700261 u8 rc_output_labels;
Robert Shearman118d5232015-08-06 11:04:56 +0100262 u32 rc_output_label[MAX_NEW_LABELS];
263 u32 rc_nlflags;
264 enum mpls_payload_type rc_payload_type;
265 struct nl_info rc_nlinfo;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700266 struct rtnexthop *rc_mp;
267 int rc_mp_len;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600268};
269
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700270static struct mpls_route *mpls_rt_alloc(int num_nh)
Eric W. Biederman01891972015-03-03 19:10:47 -0600271{
272 struct mpls_route *rt;
273
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700274 rt = kzalloc(sizeof(*rt) + (num_nh * sizeof(struct mpls_nh)),
275 GFP_KERNEL);
Eric W. Biederman01891972015-03-03 19:10:47 -0600276 if (rt)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700277 rt->rt_nhn = num_nh;
278
Eric W. Biederman01891972015-03-03 19:10:47 -0600279 return rt;
280}
281
282static void mpls_rt_free(struct mpls_route *rt)
283{
284 if (rt)
285 kfree_rcu(rt, rt_rcu);
286}
287
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600288static void mpls_notify_route(struct net *net, unsigned index,
289 struct mpls_route *old, struct mpls_route *new,
290 const struct nl_info *info)
291{
292 struct nlmsghdr *nlh = info ? info->nlh : NULL;
293 unsigned portid = info ? info->portid : 0;
294 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
295 struct mpls_route *rt = new ? new : old;
296 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
297 /* Ignore reserved labels for now */
Robert Shearmana6affd22015-08-03 17:50:04 +0100298 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600299 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
300}
301
Eric W. Biederman01891972015-03-03 19:10:47 -0600302static void mpls_route_update(struct net *net, unsigned index,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700303 struct mpls_route *new,
Eric W. Biederman01891972015-03-03 19:10:47 -0600304 const struct nl_info *info)
305{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600306 struct mpls_route __rcu **platform_label;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700307 struct mpls_route *rt;
Eric W. Biederman01891972015-03-03 19:10:47 -0600308
309 ASSERT_RTNL();
310
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600311 platform_label = rtnl_dereference(net->mpls.platform_label);
312 rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700313 rcu_assign_pointer(platform_label[index], new);
Eric W. Biederman01891972015-03-03 19:10:47 -0600314
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700315 mpls_notify_route(net, index, rt, new, info);
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600316
Eric W. Biederman01891972015-03-03 19:10:47 -0600317 /* If we removed a route free it now */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700318 mpls_rt_free(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -0600319}
320
Eric W. Biedermana2519922015-03-03 19:12:40 -0600321static unsigned find_free_label(struct net *net)
322{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600323 struct mpls_route __rcu **platform_label;
324 size_t platform_labels;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600325 unsigned index;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600326
327 platform_label = rtnl_dereference(net->mpls.platform_label);
328 platform_labels = net->mpls.platform_labels;
Robert Shearmana6affd22015-08-03 17:50:04 +0100329 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
330 index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600331 if (!rtnl_dereference(platform_label[index]))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600332 return index;
333 }
334 return LABEL_NOT_SPECIFIED;
335}
336
Roopa Prabhubf215632015-07-30 13:34:54 -0700337#if IS_ENABLED(CONFIG_INET)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700338static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
339{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300340 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700341 struct rtable *rt;
342 struct in_addr daddr;
343
344 memcpy(&daddr, addr, sizeof(struct in_addr));
345 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
346 if (IS_ERR(rt))
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300347 return ERR_CAST(rt);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700348
349 dev = rt->dst.dev;
350 dev_hold(dev);
351
352 ip_rt_put(rt);
353
Roopa Prabhu01faef22015-07-21 09:16:24 -0700354 return dev;
355}
Roopa Prabhubf215632015-07-30 13:34:54 -0700356#else
357static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
358{
359 return ERR_PTR(-EAFNOSUPPORT);
360}
361#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700362
Roopa Prabhubf215632015-07-30 13:34:54 -0700363#if IS_ENABLED(CONFIG_IPV6)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700364static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
365{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300366 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700367 struct dst_entry *dst;
368 struct flowi6 fl6;
Roopa Prabhubf215632015-07-30 13:34:54 -0700369 int err;
370
371 if (!ipv6_stub)
372 return ERR_PTR(-EAFNOSUPPORT);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700373
374 memset(&fl6, 0, sizeof(fl6));
375 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
Roopa Prabhubf215632015-07-30 13:34:54 -0700376 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
377 if (err)
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300378 return ERR_PTR(err);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700379
380 dev = dst->dev;
381 dev_hold(dev);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700382 dst_release(dst);
383
384 return dev;
385}
Roopa Prabhubf215632015-07-30 13:34:54 -0700386#else
387static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
388{
389 return ERR_PTR(-EAFNOSUPPORT);
390}
391#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700392
393static struct net_device *find_outdev(struct net *net,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700394 struct mpls_nh *nh, int oif)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700395{
396 struct net_device *dev = NULL;
397
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700398 if (!oif) {
399 switch (nh->nh_via_table) {
Roopa Prabhu01faef22015-07-21 09:16:24 -0700400 case NEIGH_ARP_TABLE:
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700401 dev = inet_fib_lookup_dev(net, nh->nh_via);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700402 break;
403 case NEIGH_ND_TABLE:
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700404 dev = inet6_fib_lookup_dev(net, nh->nh_via);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700405 break;
406 case NEIGH_LINK_TABLE:
407 break;
408 }
409 } else {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700410 dev = dev_get_by_index(net, oif);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700411 }
412
Roopa Prabhu3dcb6152015-08-04 06:36:24 -0700413 if (!dev)
414 return ERR_PTR(-ENODEV);
415
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700416 /* The caller is holding rtnl anyways, so release the dev reference */
417 dev_put(dev);
418
Roopa Prabhu01faef22015-07-21 09:16:24 -0700419 return dev;
420}
421
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700422static int mpls_nh_assign_dev(struct net *net, struct mpls_nh *nh, int oif)
423{
424 struct net_device *dev = NULL;
425 int err = -ENODEV;
426
427 dev = find_outdev(net, nh, oif);
428 if (IS_ERR(dev)) {
429 err = PTR_ERR(dev);
430 dev = NULL;
431 goto errout;
432 }
433
434 /* Ensure this is a supported device */
435 err = -EINVAL;
436 if (!mpls_dev_get(dev))
437 goto errout;
438
439 RCU_INIT_POINTER(nh->nh_dev, dev);
440
441 return 0;
442
443errout:
444 return err;
445}
446
447static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
448 struct mpls_route *rt)
449{
450 struct net *net = cfg->rc_nlinfo.nl_net;
451 struct mpls_nh *nh = rt->rt_nh;
452 int err;
453 int i;
454
455 if (!nh)
456 return -ENOMEM;
457
458 err = -EINVAL;
459 /* Ensure only a supported number of labels are present */
460 if (cfg->rc_output_labels > MAX_NEW_LABELS)
461 goto errout;
462
463 nh->nh_labels = cfg->rc_output_labels;
464 for (i = 0; i < nh->nh_labels; i++)
465 nh->nh_label[i] = cfg->rc_output_label[i];
466
467 nh->nh_via_table = cfg->rc_via_table;
468 memcpy(nh->nh_via, cfg->rc_via, cfg->rc_via_alen);
469 nh->nh_via_alen = cfg->rc_via_alen;
470
471 err = mpls_nh_assign_dev(net, nh, cfg->rc_ifindex);
472 if (err)
473 goto errout;
474
475 return 0;
476
477errout:
478 return err;
479}
480
481static int mpls_nh_build(struct net *net, struct mpls_nh *nh,
482 int oif, struct nlattr *via, struct nlattr *newdst)
483{
484 int err = -ENOMEM;
485
486 if (!nh)
487 goto errout;
488
489 if (newdst) {
490 err = nla_get_labels(newdst, MAX_NEW_LABELS,
491 &nh->nh_labels, nh->nh_label);
492 if (err)
493 goto errout;
494 }
495
496 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
497 nh->nh_via);
498 if (err)
499 goto errout;
500
501 err = mpls_nh_assign_dev(net, nh, oif);
502 if (err)
503 goto errout;
504
505 return 0;
506
507errout:
508 return err;
509}
510
511static int mpls_count_nexthops(struct rtnexthop *rtnh, int len)
512{
513 int nhs = 0;
514 int remaining = len;
515
516 while (rtnh_ok(rtnh, remaining)) {
517 nhs++;
518 rtnh = rtnh_next(rtnh, &remaining);
519 }
520
521 /* leftover implies invalid nexthop configuration, discard it */
522 return remaining > 0 ? 0 : nhs;
523}
524
525static int mpls_nh_build_multi(struct mpls_route_config *cfg,
526 struct mpls_route *rt)
527{
528 struct rtnexthop *rtnh = cfg->rc_mp;
529 struct nlattr *nla_via, *nla_newdst;
530 int remaining = cfg->rc_mp_len;
531 int nhs = 0;
532 int err = 0;
533
534 change_nexthops(rt) {
535 int attrlen;
536
537 nla_via = NULL;
538 nla_newdst = NULL;
539
540 err = -EINVAL;
541 if (!rtnh_ok(rtnh, remaining))
542 goto errout;
543
544 attrlen = rtnh_attrlen(rtnh);
545 if (attrlen > 0) {
546 struct nlattr *attrs = rtnh_attrs(rtnh);
547
548 nla_via = nla_find(attrs, attrlen, RTA_VIA);
549 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
550 }
551
552 if (!nla_via)
553 goto errout;
554
555 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, nh,
556 rtnh->rtnh_ifindex, nla_via,
557 nla_newdst);
558 if (err)
559 goto errout;
560
561 rtnh = rtnh_next(rtnh, &remaining);
562 nhs++;
563 } endfor_nexthops(rt);
564
565 rt->rt_nhn = nhs;
566
567 return 0;
568
569errout:
570 return err;
571}
572
Eric W. Biedermana2519922015-03-03 19:12:40 -0600573static int mpls_route_add(struct mpls_route_config *cfg)
574{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600575 struct mpls_route __rcu **platform_label;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600576 struct net *net = cfg->rc_nlinfo.nl_net;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600577 struct mpls_route *rt, *old;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600578 int err = -EINVAL;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700579 unsigned index;
580 int nhs = 1; /* default to one nexthop */
Eric W. Biedermana2519922015-03-03 19:12:40 -0600581
582 index = cfg->rc_label;
583
584 /* If a label was not specified during insert pick one */
585 if ((index == LABEL_NOT_SPECIFIED) &&
586 (cfg->rc_nlflags & NLM_F_CREATE)) {
587 index = find_free_label(net);
588 }
589
Robert Shearmana6affd22015-08-03 17:50:04 +0100590 /* Reserved labels may not be set */
591 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600592 goto errout;
593
594 /* The full 20 bit range may not be supported. */
595 if (index >= net->mpls.platform_labels)
596 goto errout;
597
Eric W. Biedermana2519922015-03-03 19:12:40 -0600598 /* Append makes no sense with mpls */
Eric W. Biederman0f7bbd52015-03-07 16:22:40 -0600599 err = -EOPNOTSUPP;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600600 if (cfg->rc_nlflags & NLM_F_APPEND)
601 goto errout;
602
603 err = -EEXIST;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600604 platform_label = rtnl_dereference(net->mpls.platform_label);
605 old = rtnl_dereference(platform_label[index]);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600606 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
607 goto errout;
608
609 err = -EEXIST;
610 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
611 goto errout;
612
613 err = -ENOENT;
614 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
615 goto errout;
616
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700617 if (cfg->rc_mp) {
618 err = -EINVAL;
619 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len);
620 if (nhs == 0)
621 goto errout;
622 }
623
Eric W. Biedermana2519922015-03-03 19:12:40 -0600624 err = -ENOMEM;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700625 rt = mpls_rt_alloc(nhs);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600626 if (!rt)
627 goto errout;
628
Eric W. Biedermana2519922015-03-03 19:12:40 -0600629 rt->rt_protocol = cfg->rc_protocol;
Robert Shearman118d5232015-08-06 11:04:56 +0100630 rt->rt_payload_type = cfg->rc_payload_type;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600631
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700632 if (cfg->rc_mp)
633 err = mpls_nh_build_multi(cfg, rt);
634 else
635 err = mpls_nh_build_from_cfg(cfg, rt);
636 if (err)
637 goto freert;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600638
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700639 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
640
Eric W. Biedermana2519922015-03-03 19:12:40 -0600641 return 0;
642
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700643freert:
644 mpls_rt_free(rt);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600645errout:
Eric W. Biedermana2519922015-03-03 19:12:40 -0600646 return err;
647}
648
649static int mpls_route_del(struct mpls_route_config *cfg)
650{
651 struct net *net = cfg->rc_nlinfo.nl_net;
652 unsigned index;
653 int err = -EINVAL;
654
655 index = cfg->rc_label;
656
Robert Shearmana6affd22015-08-03 17:50:04 +0100657 /* Reserved labels may not be removed */
658 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600659 goto errout;
660
661 /* The full 20 bit range may not be supported */
662 if (index >= net->mpls.platform_labels)
663 goto errout;
664
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700665 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600666
667 err = 0;
668errout:
669 return err;
670}
671
Robert Shearman37bde792015-04-22 11:14:38 +0100672#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
673 (&((struct mpls_dev *)0)->field)
674
675static const struct ctl_table mpls_dev_table[] = {
676 {
677 .procname = "input",
678 .maxlen = sizeof(int),
679 .mode = 0644,
680 .proc_handler = proc_dointvec,
681 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
682 },
683 { }
684};
685
686static int mpls_dev_sysctl_register(struct net_device *dev,
687 struct mpls_dev *mdev)
688{
689 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
690 struct ctl_table *table;
691 int i;
692
693 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
694 if (!table)
695 goto out;
696
697 /* Table data contains only offsets relative to the base of
698 * the mdev at this point, so make them absolute.
699 */
700 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
701 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
702
703 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
704
705 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
706 if (!mdev->sysctl)
707 goto free;
708
709 return 0;
710
711free:
712 kfree(table);
713out:
714 return -ENOBUFS;
715}
716
717static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
718{
719 struct ctl_table *table;
720
721 table = mdev->sysctl->ctl_table_arg;
722 unregister_net_sysctl_table(mdev->sysctl);
723 kfree(table);
724}
725
Robert Shearman03c57742015-04-22 11:14:37 +0100726static struct mpls_dev *mpls_add_dev(struct net_device *dev)
727{
728 struct mpls_dev *mdev;
729 int err = -ENOMEM;
730
731 ASSERT_RTNL();
732
733 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
734 if (!mdev)
735 return ERR_PTR(err);
736
Robert Shearman37bde792015-04-22 11:14:38 +0100737 err = mpls_dev_sysctl_register(dev, mdev);
738 if (err)
739 goto free;
740
Robert Shearman03c57742015-04-22 11:14:37 +0100741 rcu_assign_pointer(dev->mpls_ptr, mdev);
742
743 return mdev;
Robert Shearman37bde792015-04-22 11:14:38 +0100744
745free:
746 kfree(mdev);
747 return ERR_PTR(err);
Robert Shearman03c57742015-04-22 11:14:37 +0100748}
749
Eric W. Biederman01891972015-03-03 19:10:47 -0600750static void mpls_ifdown(struct net_device *dev)
751{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600752 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -0600753 struct net *net = dev_net(dev);
Robert Shearman03c57742015-04-22 11:14:37 +0100754 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600755 unsigned index;
756
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600757 platform_label = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -0600758 for (index = 0; index < net->mpls.platform_labels; index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600759 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman01891972015-03-03 19:10:47 -0600760 if (!rt)
761 continue;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700762 for_nexthops(rt) {
763 if (rtnl_dereference(nh->nh_dev) != dev)
764 continue;
765 nh->nh_dev = NULL;
766 } endfor_nexthops(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -0600767 }
Robert Shearman03c57742015-04-22 11:14:37 +0100768
769 mdev = mpls_dev_get(dev);
770 if (!mdev)
771 return;
772
Robert Shearman37bde792015-04-22 11:14:38 +0100773 mpls_dev_sysctl_unregister(mdev);
774
Robert Shearman03c57742015-04-22 11:14:37 +0100775 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
776
Robert Shearman25cc8f02015-06-05 18:54:45 +0100777 kfree_rcu(mdev, rcu);
Eric W. Biederman01891972015-03-03 19:10:47 -0600778}
779
780static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
781 void *ptr)
782{
783 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Robert Shearman03c57742015-04-22 11:14:37 +0100784 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600785
786 switch(event) {
Robert Shearman03c57742015-04-22 11:14:37 +0100787 case NETDEV_REGISTER:
788 /* For now just support ethernet devices */
789 if ((dev->type == ARPHRD_ETHER) ||
790 (dev->type == ARPHRD_LOOPBACK)) {
791 mdev = mpls_add_dev(dev);
792 if (IS_ERR(mdev))
793 return notifier_from_errno(PTR_ERR(mdev));
794 }
795 break;
796
Eric W. Biederman01891972015-03-03 19:10:47 -0600797 case NETDEV_UNREGISTER:
798 mpls_ifdown(dev);
799 break;
Robert Shearman0fae3bf2015-06-11 19:58:26 +0100800 case NETDEV_CHANGENAME:
801 mdev = mpls_dev_get(dev);
802 if (mdev) {
803 int err;
804
805 mpls_dev_sysctl_unregister(mdev);
806 err = mpls_dev_sysctl_register(dev, mdev);
807 if (err)
808 return notifier_from_errno(err);
809 }
810 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600811 }
812 return NOTIFY_OK;
813}
814
815static struct notifier_block mpls_dev_notifier = {
816 .notifier_call = mpls_dev_notify,
817};
818
Eric W. Biederman03c05662015-03-03 19:13:56 -0600819static int nla_put_via(struct sk_buff *skb,
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600820 u8 table, const void *addr, int alen)
Eric W. Biederman03c05662015-03-03 19:13:56 -0600821{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600822 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
823 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
824 };
Eric W. Biederman03c05662015-03-03 19:13:56 -0600825 struct nlattr *nla;
826 struct rtvia *via;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600827 int family = AF_UNSPEC;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600828
829 nla = nla_reserve(skb, RTA_VIA, alen + 2);
830 if (!nla)
831 return -EMSGSIZE;
832
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600833 if (table <= NEIGH_NR_TABLES)
834 family = table_to_family[table];
835
Eric W. Biederman03c05662015-03-03 19:13:56 -0600836 via = nla_data(nla);
837 via->rtvia_family = family;
838 memcpy(via->rtvia_addr, addr, alen);
839 return 0;
840}
841
Eric W. Biederman966bae32015-03-03 19:13:19 -0600842int nla_put_labels(struct sk_buff *skb, int attrtype,
843 u8 labels, const u32 label[])
844{
845 struct nlattr *nla;
846 struct mpls_shim_hdr *nla_label;
847 bool bos;
848 int i;
849 nla = nla_reserve(skb, attrtype, labels*4);
850 if (!nla)
851 return -EMSGSIZE;
852
853 nla_label = nla_data(nla);
854 bos = true;
855 for (i = labels - 1; i >= 0; i--) {
856 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
857 bos = false;
858 }
859
860 return 0;
861}
Roopa Prabhuface0182015-07-21 10:43:52 +0200862EXPORT_SYMBOL_GPL(nla_put_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600863
864int nla_get_labels(const struct nlattr *nla,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700865 u32 max_labels, u8 *labels, u32 label[])
Eric W. Biederman966bae32015-03-03 19:13:19 -0600866{
867 unsigned len = nla_len(nla);
868 unsigned nla_labels;
869 struct mpls_shim_hdr *nla_label;
870 bool bos;
871 int i;
872
873 /* len needs to be an even multiple of 4 (the label size) */
874 if (len & 3)
875 return -EINVAL;
876
877 /* Limit the number of new labels allowed */
878 nla_labels = len/4;
879 if (nla_labels > max_labels)
880 return -EINVAL;
881
882 nla_label = nla_data(nla);
883 bos = true;
884 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
885 struct mpls_entry_decoded dec;
886 dec = mpls_entry_decode(nla_label + i);
887
888 /* Ensure the bottom of stack flag is properly set
889 * and ttl and tc are both clear.
890 */
891 if ((dec.bos != bos) || dec.ttl || dec.tc)
892 return -EINVAL;
893
Robert Shearman5a9ab012015-04-22 11:14:39 +0100894 switch (dec.label) {
Tom Herbert78f5b892015-05-07 08:08:51 -0700895 case MPLS_LABEL_IMPLNULL:
Robert Shearman5a9ab012015-04-22 11:14:39 +0100896 /* RFC3032: This is a label that an LSR may
897 * assign and distribute, but which never
898 * actually appears in the encapsulation.
899 */
900 return -EINVAL;
901 }
902
Eric W. Biederman966bae32015-03-03 19:13:19 -0600903 label[i] = dec.label;
904 }
905 *labels = nla_labels;
906 return 0;
907}
Roopa Prabhuface0182015-07-21 10:43:52 +0200908EXPORT_SYMBOL_GPL(nla_get_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600909
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700910int nla_get_via(const struct nlattr *nla, u8 *via_alen,
911 u8 *via_table, u8 via_addr[])
912{
913 struct rtvia *via = nla_data(nla);
914 int err = -EINVAL;
915 int alen;
916
917 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
918 goto errout;
919 alen = nla_len(nla) -
920 offsetof(struct rtvia, rtvia_addr);
921 if (alen > MAX_VIA_ALEN)
922 goto errout;
923
924 /* Validate the address family */
925 switch (via->rtvia_family) {
926 case AF_PACKET:
927 *via_table = NEIGH_LINK_TABLE;
928 break;
929 case AF_INET:
930 *via_table = NEIGH_ARP_TABLE;
931 if (alen != 4)
932 goto errout;
933 break;
934 case AF_INET6:
935 *via_table = NEIGH_ND_TABLE;
936 if (alen != 16)
937 goto errout;
938 break;
939 default:
940 /* Unsupported address family */
941 goto errout;
942 }
943
944 memcpy(via_addr, via->rtvia_addr, alen);
945 *via_alen = alen;
946 err = 0;
947
948errout:
949 return err;
950}
951
Eric W. Biederman03c05662015-03-03 19:13:56 -0600952static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
953 struct mpls_route_config *cfg)
954{
955 struct rtmsg *rtm;
956 struct nlattr *tb[RTA_MAX+1];
957 int index;
958 int err;
959
960 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
961 if (err < 0)
962 goto errout;
963
964 err = -EINVAL;
965 rtm = nlmsg_data(nlh);
966 memset(cfg, 0, sizeof(*cfg));
967
968 if (rtm->rtm_family != AF_MPLS)
969 goto errout;
970 if (rtm->rtm_dst_len != 20)
971 goto errout;
972 if (rtm->rtm_src_len != 0)
973 goto errout;
974 if (rtm->rtm_tos != 0)
975 goto errout;
976 if (rtm->rtm_table != RT_TABLE_MAIN)
977 goto errout;
978 /* Any value is acceptable for rtm_protocol */
979
980 /* As mpls uses destination specific addresses
981 * (or source specific address in the case of multicast)
982 * all addresses have universal scope.
983 */
984 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
985 goto errout;
986 if (rtm->rtm_type != RTN_UNICAST)
987 goto errout;
988 if (rtm->rtm_flags != 0)
989 goto errout;
990
991 cfg->rc_label = LABEL_NOT_SPECIFIED;
992 cfg->rc_protocol = rtm->rtm_protocol;
993 cfg->rc_nlflags = nlh->nlmsg_flags;
994 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
995 cfg->rc_nlinfo.nlh = nlh;
996 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
997
998 for (index = 0; index <= RTA_MAX; index++) {
999 struct nlattr *nla = tb[index];
1000 if (!nla)
1001 continue;
1002
1003 switch(index) {
1004 case RTA_OIF:
1005 cfg->rc_ifindex = nla_get_u32(nla);
1006 break;
1007 case RTA_NEWDST:
1008 if (nla_get_labels(nla, MAX_NEW_LABELS,
1009 &cfg->rc_output_labels,
1010 cfg->rc_output_label))
1011 goto errout;
1012 break;
1013 case RTA_DST:
1014 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001015 u8 label_count;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001016 if (nla_get_labels(nla, 1, &label_count,
1017 &cfg->rc_label))
1018 goto errout;
1019
Robert Shearmana6affd22015-08-03 17:50:04 +01001020 /* Reserved labels may not be set */
1021 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001022 goto errout;
1023
1024 break;
1025 }
1026 case RTA_VIA:
1027 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001028 if (nla_get_via(nla, &cfg->rc_via_alen,
1029 &cfg->rc_via_table, cfg->rc_via))
Robert Shearmanf8d54af2015-03-06 10:47:00 +00001030 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001031 break;
1032 }
1033 case RTA_MULTIPATH:
1034 {
1035 cfg->rc_mp = nla_data(nla);
1036 cfg->rc_mp_len = nla_len(nla);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001037 break;
1038 }
1039 default:
1040 /* Unsupported attribute */
1041 goto errout;
1042 }
1043 }
1044
1045 err = 0;
1046errout:
1047 return err;
1048}
1049
1050static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1051{
1052 struct mpls_route_config cfg;
1053 int err;
1054
1055 err = rtm_to_route_config(skb, nlh, &cfg);
1056 if (err < 0)
1057 return err;
1058
1059 return mpls_route_del(&cfg);
1060}
1061
1062
1063static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1064{
1065 struct mpls_route_config cfg;
1066 int err;
1067
1068 err = rtm_to_route_config(skb, nlh, &cfg);
1069 if (err < 0)
1070 return err;
1071
1072 return mpls_route_add(&cfg);
1073}
1074
1075static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1076 u32 label, struct mpls_route *rt, int flags)
1077{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001078 struct net_device *dev;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001079 struct nlmsghdr *nlh;
1080 struct rtmsg *rtm;
1081
1082 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1083 if (nlh == NULL)
1084 return -EMSGSIZE;
1085
1086 rtm = nlmsg_data(nlh);
1087 rtm->rtm_family = AF_MPLS;
1088 rtm->rtm_dst_len = 20;
1089 rtm->rtm_src_len = 0;
1090 rtm->rtm_tos = 0;
1091 rtm->rtm_table = RT_TABLE_MAIN;
1092 rtm->rtm_protocol = rt->rt_protocol;
1093 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1094 rtm->rtm_type = RTN_UNICAST;
1095 rtm->rtm_flags = 0;
1096
Eric W. Biederman03c05662015-03-03 19:13:56 -06001097 if (nla_put_labels(skb, RTA_DST, 1, &label))
1098 goto nla_put_failure;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001099 if (rt->rt_nhn == 1) {
1100 struct mpls_nh *nh = rt->rt_nh;
1101
1102 if (nh->nh_labels &&
1103 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1104 nh->nh_label))
1105 goto nla_put_failure;
1106 if (nla_put_via(skb, nh->nh_via_table, nh->nh_via,
1107 nh->nh_via_alen))
1108 goto nla_put_failure;
1109 dev = rtnl_dereference(nh->nh_dev);
1110 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1111 goto nla_put_failure;
1112 } else {
1113 struct rtnexthop *rtnh;
1114 struct nlattr *mp;
1115
1116 mp = nla_nest_start(skb, RTA_MULTIPATH);
1117 if (!mp)
1118 goto nla_put_failure;
1119
1120 for_nexthops(rt) {
1121 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1122 if (!rtnh)
1123 goto nla_put_failure;
1124
1125 dev = rtnl_dereference(nh->nh_dev);
1126 if (dev)
1127 rtnh->rtnh_ifindex = dev->ifindex;
1128 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1129 nh->nh_labels,
1130 nh->nh_label))
1131 goto nla_put_failure;
1132 if (nla_put_via(skb, nh->nh_via_table,
1133 nh->nh_via,
1134 nh->nh_via_alen))
1135 goto nla_put_failure;
1136
1137 /* length of rtnetlink header + attributes */
1138 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1139 } endfor_nexthops(rt);
1140
1141 nla_nest_end(skb, mp);
1142 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001143
1144 nlmsg_end(skb, nlh);
1145 return 0;
1146
1147nla_put_failure:
1148 nlmsg_cancel(skb, nlh);
1149 return -EMSGSIZE;
1150}
1151
1152static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1153{
1154 struct net *net = sock_net(skb->sk);
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001155 struct mpls_route __rcu **platform_label;
1156 size_t platform_labels;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001157 unsigned int index;
1158
1159 ASSERT_RTNL();
1160
1161 index = cb->args[0];
Robert Shearmana6affd22015-08-03 17:50:04 +01001162 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1163 index = MPLS_LABEL_FIRST_UNRESERVED;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001164
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001165 platform_label = rtnl_dereference(net->mpls.platform_label);
1166 platform_labels = net->mpls.platform_labels;
1167 for (; index < platform_labels; index++) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001168 struct mpls_route *rt;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001169 rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001170 if (!rt)
1171 continue;
1172
1173 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1174 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1175 index, rt, NLM_F_MULTI) < 0)
1176 break;
1177 }
1178 cb->args[0] = index;
1179
1180 return skb->len;
1181}
1182
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001183static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1184{
1185 size_t payload =
1186 NLMSG_ALIGN(sizeof(struct rtmsg))
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001187 + nla_total_size(4); /* RTA_DST */
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001188
1189 if (rt->rt_nhn == 1) {
1190 struct mpls_nh *nh = rt->rt_nh;
1191
1192 if (nh->nh_dev)
1193 payload += nla_total_size(4); /* RTA_OIF */
1194 payload += nla_total_size(2 + nh->nh_via_alen); /* RTA_VIA */
1195 if (nh->nh_labels) /* RTA_NEWDST */
1196 payload += nla_total_size(nh->nh_labels * 4);
1197 } else {
1198 /* each nexthop is packed in an attribute */
1199 size_t nhsize = 0;
1200
1201 for_nexthops(rt) {
1202 nhsize += nla_total_size(sizeof(struct rtnexthop));
1203 nhsize += nla_total_size(2 + nh->nh_via_alen);
1204 if (nh->nh_labels)
1205 nhsize += nla_total_size(nh->nh_labels * 4);
1206 } endfor_nexthops(rt);
1207 /* nested attribute */
1208 payload += nla_total_size(nhsize);
1209 }
1210
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001211 return payload;
1212}
1213
1214static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1215 struct nlmsghdr *nlh, struct net *net, u32 portid,
1216 unsigned int nlm_flags)
1217{
1218 struct sk_buff *skb;
1219 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1220 int err = -ENOBUFS;
1221
1222 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1223 if (skb == NULL)
1224 goto errout;
1225
1226 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1227 if (err < 0) {
1228 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1229 WARN_ON(err == -EMSGSIZE);
1230 kfree_skb(skb);
1231 goto errout;
1232 }
1233 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1234
1235 return;
1236errout:
1237 if (err < 0)
1238 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1239}
1240
Eric W. Biederman7720c012015-03-03 19:11:20 -06001241static int resize_platform_label_table(struct net *net, size_t limit)
1242{
1243 size_t size = sizeof(struct mpls_route *) * limit;
1244 size_t old_limit;
1245 size_t cp_size;
1246 struct mpls_route __rcu **labels = NULL, **old;
1247 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1248 unsigned index;
1249
1250 if (size) {
1251 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1252 if (!labels)
1253 labels = vzalloc(size);
1254
1255 if (!labels)
1256 goto nolabels;
1257 }
1258
1259 /* In case the predefined labels need to be populated */
Tom Herbert78f5b892015-05-07 08:08:51 -07001260 if (limit > MPLS_LABEL_IPV4NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001261 struct net_device *lo = net->loopback_dev;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001262 rt0 = mpls_rt_alloc(1);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001263 if (!rt0)
1264 goto nort0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001265 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001266 rt0->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001267 rt0->rt_payload_type = MPT_IPV4;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001268 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1269 memcpy(rt0->rt_nh->nh_via, lo->dev_addr, lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001270 }
Tom Herbert78f5b892015-05-07 08:08:51 -07001271 if (limit > MPLS_LABEL_IPV6NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001272 struct net_device *lo = net->loopback_dev;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001273 rt2 = mpls_rt_alloc(1);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001274 if (!rt2)
1275 goto nort2;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001276 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001277 rt2->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001278 rt2->rt_payload_type = MPT_IPV6;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001279 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1280 memcpy(rt2->rt_nh->nh_via, lo->dev_addr, lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001281 }
1282
1283 rtnl_lock();
1284 /* Remember the original table */
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001285 old = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001286 old_limit = net->mpls.platform_labels;
1287
1288 /* Free any labels beyond the new table */
1289 for (index = limit; index < old_limit; index++)
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001290 mpls_route_update(net, index, NULL, NULL);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001291
1292 /* Copy over the old labels */
1293 cp_size = size;
1294 if (old_limit < limit)
1295 cp_size = old_limit * sizeof(struct mpls_route *);
1296
1297 memcpy(labels, old, cp_size);
1298
1299 /* If needed set the predefined labels */
Tom Herbert78f5b892015-05-07 08:08:51 -07001300 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1301 (limit > MPLS_LABEL_IPV6NULL)) {
1302 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001303 rt2 = NULL;
1304 }
1305
Tom Herbert78f5b892015-05-07 08:08:51 -07001306 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1307 (limit > MPLS_LABEL_IPV4NULL)) {
1308 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001309 rt0 = NULL;
1310 }
1311
1312 /* Update the global pointers */
1313 net->mpls.platform_labels = limit;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001314 rcu_assign_pointer(net->mpls.platform_label, labels);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001315
1316 rtnl_unlock();
1317
1318 mpls_rt_free(rt2);
1319 mpls_rt_free(rt0);
1320
1321 if (old) {
1322 synchronize_rcu();
1323 kvfree(old);
1324 }
1325 return 0;
1326
1327nort2:
1328 mpls_rt_free(rt0);
1329nort0:
1330 kvfree(labels);
1331nolabels:
1332 return -ENOMEM;
1333}
1334
1335static int mpls_platform_labels(struct ctl_table *table, int write,
1336 void __user *buffer, size_t *lenp, loff_t *ppos)
1337{
1338 struct net *net = table->data;
1339 int platform_labels = net->mpls.platform_labels;
1340 int ret;
1341 struct ctl_table tmp = {
1342 .procname = table->procname,
1343 .data = &platform_labels,
1344 .maxlen = sizeof(int),
1345 .mode = table->mode,
1346 .extra1 = &zero,
1347 .extra2 = &label_limit,
1348 };
1349
1350 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1351
1352 if (write && ret == 0)
1353 ret = resize_platform_label_table(net, platform_labels);
1354
1355 return ret;
1356}
1357
Robert Shearman37bde792015-04-22 11:14:38 +01001358static const struct ctl_table mpls_table[] = {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001359 {
1360 .procname = "platform_labels",
1361 .data = NULL,
1362 .maxlen = sizeof(int),
1363 .mode = 0644,
1364 .proc_handler = mpls_platform_labels,
1365 },
1366 { }
1367};
1368
Eric W. Biederman01891972015-03-03 19:10:47 -06001369static int mpls_net_init(struct net *net)
1370{
Eric W. Biederman7720c012015-03-03 19:11:20 -06001371 struct ctl_table *table;
1372
Eric W. Biederman01891972015-03-03 19:10:47 -06001373 net->mpls.platform_labels = 0;
1374 net->mpls.platform_label = NULL;
1375
Eric W. Biederman7720c012015-03-03 19:11:20 -06001376 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1377 if (table == NULL)
1378 return -ENOMEM;
1379
1380 table[0].data = net;
1381 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07001382 if (net->mpls.ctl == NULL) {
1383 kfree(table);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001384 return -ENOMEM;
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07001385 }
Eric W. Biederman7720c012015-03-03 19:11:20 -06001386
Eric W. Biederman01891972015-03-03 19:10:47 -06001387 return 0;
1388}
1389
1390static void mpls_net_exit(struct net *net)
1391{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001392 struct mpls_route __rcu **platform_label;
1393 size_t platform_labels;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001394 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -06001395 unsigned int index;
1396
Eric W. Biederman7720c012015-03-03 19:11:20 -06001397 table = net->mpls.ctl->ctl_table_arg;
1398 unregister_net_sysctl_table(net->mpls.ctl);
1399 kfree(table);
1400
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001401 /* An rcu grace period has passed since there was a device in
1402 * the network namespace (and thus the last in flight packet)
Eric W. Biederman01891972015-03-03 19:10:47 -06001403 * left this network namespace. This is because
1404 * unregister_netdevice_many and netdev_run_todo has completed
1405 * for each network device that was in this network namespace.
1406 *
1407 * As such no additional rcu synchronization is necessary when
1408 * freeing the platform_label table.
1409 */
1410 rtnl_lock();
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001411 platform_label = rtnl_dereference(net->mpls.platform_label);
1412 platform_labels = net->mpls.platform_labels;
1413 for (index = 0; index < platform_labels; index++) {
1414 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1415 RCU_INIT_POINTER(platform_label[index], NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001416 mpls_rt_free(rt);
1417 }
1418 rtnl_unlock();
1419
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001420 kvfree(platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06001421}
1422
1423static struct pernet_operations mpls_net_ops = {
1424 .init = mpls_net_init,
1425 .exit = mpls_net_exit,
1426};
1427
1428static int __init mpls_init(void)
1429{
1430 int err;
1431
1432 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1433
1434 err = register_pernet_subsys(&mpls_net_ops);
1435 if (err)
1436 goto out;
1437
1438 err = register_netdevice_notifier(&mpls_dev_notifier);
1439 if (err)
1440 goto out_unregister_pernet;
1441
1442 dev_add_pack(&mpls_packet_type);
1443
Eric W. Biederman03c05662015-03-03 19:13:56 -06001444 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1445 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1446 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001447 err = 0;
1448out:
1449 return err;
1450
1451out_unregister_pernet:
1452 unregister_pernet_subsys(&mpls_net_ops);
1453 goto out;
1454}
1455module_init(mpls_init);
1456
1457static void __exit mpls_exit(void)
1458{
Eric W. Biederman03c05662015-03-03 19:13:56 -06001459 rtnl_unregister_all(PF_MPLS);
Eric W. Biederman01891972015-03-03 19:10:47 -06001460 dev_remove_pack(&mpls_packet_type);
1461 unregister_netdevice_notifier(&mpls_dev_notifier);
1462 unregister_pernet_subsys(&mpls_net_ops);
1463}
1464module_exit(mpls_exit);
1465
1466MODULE_DESCRIPTION("MultiProtocol Label Switching");
1467MODULE_LICENSE("GPL v2");
1468MODULE_ALIAS_NETPROTO(PF_MPLS);