blob: cc972e30355b600769051dc3841ba7fa582b42a5 [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
Robert Shearman1c78efa2015-10-23 06:03:28 -070025/* Maximum number of labels to look ahead at when selecting a path of
26 * a multipath route
27 */
28#define MAX_MP_SELECT_LABELS 4
29
Eric W. Biederman7720c012015-03-03 19:11:20 -060030static int zero = 0;
31static int label_limit = (1 << 20) - 1;
32
Eric W. Biederman8de147d2015-03-03 19:14:31 -060033static 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);
36
Eric W. Biederman01891972015-03-03 19:10:47 -060037static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
38{
39 struct mpls_route *rt = NULL;
40
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]);
45 }
46 return rt;
47}
48
Robert Shearman03c57742015-04-22 11:14:37 +010049static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
50{
51 return rcu_dereference_rtnl(dev->mpls_ptr);
52}
53
Roopa Prabhuface0182015-07-21 10:43:52 +020054bool mpls_output_possible(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060055{
56 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
57}
Roopa Prabhuface0182015-07-21 10:43:52 +020058EXPORT_SYMBOL_GPL(mpls_output_possible);
Eric W. Biederman01891972015-03-03 19:10:47 -060059
Roopa Prabhuf8efb732015-10-23 06:03:27 -070060static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
Eric W. Biederman01891972015-03-03 19:10:47 -060061{
62 /* The size of the layer 2.5 labels to be added for this route */
Roopa Prabhuf8efb732015-10-23 06:03:27 -070063 return nh->nh_labels * sizeof(struct mpls_shim_hdr);
Eric W. Biederman01891972015-03-03 19:10:47 -060064}
65
Roopa Prabhuface0182015-07-21 10:43:52 +020066unsigned int mpls_dev_mtu(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060067{
68 /* The amount of data the layer 2 frame can hold */
69 return dev->mtu;
70}
Roopa Prabhuface0182015-07-21 10:43:52 +020071EXPORT_SYMBOL_GPL(mpls_dev_mtu);
Eric W. Biederman01891972015-03-03 19:10:47 -060072
Roopa Prabhuface0182015-07-21 10:43:52 +020073bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
Eric W. Biederman01891972015-03-03 19:10:47 -060074{
75 if (skb->len <= mtu)
76 return false;
77
78 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
79 return false;
80
81 return true;
82}
Roopa Prabhuface0182015-07-21 10:43:52 +020083EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
Eric W. Biederman01891972015-03-03 19:10:47 -060084
Robert Shearman1c78efa2015-10-23 06:03:28 -070085static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
86 struct sk_buff *skb, bool bos)
Roopa Prabhuf8efb732015-10-23 06:03:27 -070087{
Robert Shearman1c78efa2015-10-23 06:03:28 -070088 struct mpls_entry_decoded dec;
89 struct mpls_shim_hdr *hdr;
90 bool eli_seen = false;
91 int label_index;
92 int nh_index = 0;
93 u32 hash = 0;
94
95 /* No need to look further into packet if there's only
96 * one path
97 */
98 if (rt->rt_nhn == 1)
99 goto out;
100
101 for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos;
102 label_index++) {
103 if (!pskb_may_pull(skb, sizeof(*hdr) * label_index))
104 break;
105
106 /* Read and decode the current label */
107 hdr = mpls_hdr(skb) + label_index;
108 dec = mpls_entry_decode(hdr);
109
110 /* RFC6790 - reserved labels MUST NOT be used as keys
111 * for the load-balancing function
112 */
113 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
114 hash = jhash_1word(dec.label, hash);
115
116 /* The entropy label follows the entropy label
117 * indicator, so this means that the entropy
118 * label was just added to the hash - no need to
119 * go any deeper either in the label stack or in the
120 * payload
121 */
122 if (eli_seen)
123 break;
124 } else if (dec.label == MPLS_LABEL_ENTROPY) {
125 eli_seen = true;
126 }
127
128 bos = dec.bos;
129 if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index +
130 sizeof(struct iphdr))) {
131 const struct iphdr *v4hdr;
132
133 v4hdr = (const struct iphdr *)(mpls_hdr(skb) +
134 label_index);
135 if (v4hdr->version == 4) {
136 hash = jhash_3words(ntohl(v4hdr->saddr),
137 ntohl(v4hdr->daddr),
138 v4hdr->protocol, hash);
139 } else if (v4hdr->version == 6 &&
140 pskb_may_pull(skb, sizeof(*hdr) * label_index +
141 sizeof(struct ipv6hdr))) {
142 const struct ipv6hdr *v6hdr;
143
144 v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) +
145 label_index);
146
147 hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
148 hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
149 hash = jhash_1word(v6hdr->nexthdr, hash);
150 }
151 }
152 }
153
154 nh_index = hash % rt->rt_nhn;
155out:
156 return &rt->rt_nh[nh_index];
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700157}
158
Eric W. Biederman01891972015-03-03 19:10:47 -0600159static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
160 struct mpls_entry_decoded dec)
161{
Robert Shearman118d5232015-08-06 11:04:56 +0100162 enum mpls_payload_type payload_type;
163 bool success = false;
Eric W. Biederman01891972015-03-03 19:10:47 -0600164
Eric W. Biederman76fecd82015-03-12 18:22:59 -0500165 /* The IPv4 code below accesses through the IPv4 header
166 * checksum, which is 12 bytes into the packet.
167 * The IPv6 code below accesses through the IPv6 hop limit
168 * which is 8 bytes into the packet.
169 *
170 * For all supported cases there should always be at least 12
171 * bytes of packet data present. The IPv4 header is 20 bytes
172 * without options and the IPv6 header is always 40 bytes
173 * long.
174 */
175 if (!pskb_may_pull(skb, 12))
176 return false;
177
Robert Shearman118d5232015-08-06 11:04:56 +0100178 payload_type = rt->rt_payload_type;
179 if (payload_type == MPT_UNSPEC)
180 payload_type = ip_hdr(skb)->version;
181
182 switch (payload_type) {
183 case MPT_IPV4: {
184 struct iphdr *hdr4 = ip_hdr(skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600185 skb->protocol = htons(ETH_P_IP);
186 csum_replace2(&hdr4->check,
187 htons(hdr4->ttl << 8),
188 htons(dec.ttl << 8));
189 hdr4->ttl = dec.ttl;
Robert Shearman118d5232015-08-06 11:04:56 +0100190 success = true;
191 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600192 }
Robert Shearman118d5232015-08-06 11:04:56 +0100193 case MPT_IPV6: {
Eric W. Biederman01891972015-03-03 19:10:47 -0600194 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
195 skb->protocol = htons(ETH_P_IPV6);
196 hdr6->hop_limit = dec.ttl;
Robert Shearman118d5232015-08-06 11:04:56 +0100197 success = true;
198 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600199 }
Robert Shearman118d5232015-08-06 11:04:56 +0100200 case MPT_UNSPEC:
201 break;
202 }
203
Eric W. Biederman01891972015-03-03 19:10:47 -0600204 return success;
205}
206
207static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
208 struct packet_type *pt, struct net_device *orig_dev)
209{
210 struct net *net = dev_net(dev);
211 struct mpls_shim_hdr *hdr;
212 struct mpls_route *rt;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700213 struct mpls_nh *nh;
Eric W. Biederman01891972015-03-03 19:10:47 -0600214 struct mpls_entry_decoded dec;
215 struct net_device *out_dev;
Robert Shearman03c57742015-04-22 11:14:37 +0100216 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600217 unsigned int hh_len;
218 unsigned int new_header_size;
219 unsigned int mtu;
220 int err;
221
222 /* Careful this entire function runs inside of an rcu critical section */
223
Robert Shearman03c57742015-04-22 11:14:37 +0100224 mdev = mpls_dev_get(dev);
Robert Shearman37bde792015-04-22 11:14:38 +0100225 if (!mdev || !mdev->input_enabled)
Robert Shearman03c57742015-04-22 11:14:37 +0100226 goto drop;
227
Eric W. Biederman01891972015-03-03 19:10:47 -0600228 if (skb->pkt_type != PACKET_HOST)
229 goto drop;
230
231 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
232 goto drop;
233
234 if (!pskb_may_pull(skb, sizeof(*hdr)))
235 goto drop;
236
237 /* Read and decode the label */
238 hdr = mpls_hdr(skb);
239 dec = mpls_entry_decode(hdr);
240
241 /* Pop the label */
242 skb_pull(skb, sizeof(*hdr));
243 skb_reset_network_header(skb);
244
245 skb_orphan(skb);
246
247 rt = mpls_route_input_rcu(net, dec.label);
248 if (!rt)
249 goto drop;
250
Robert Shearman1c78efa2015-10-23 06:03:28 -0700251 nh = mpls_select_multipath(rt, skb, dec.bos);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700252 if (!nh)
253 goto drop;
254
Eric W. Biederman01891972015-03-03 19:10:47 -0600255 /* Find the output device */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700256 out_dev = rcu_dereference(nh->nh_dev);
Eric W. Biederman01891972015-03-03 19:10:47 -0600257 if (!mpls_output_possible(out_dev))
258 goto drop;
259
260 if (skb_warn_if_lro(skb))
261 goto drop;
262
263 skb_forward_csum(skb);
264
265 /* Verify ttl is valid */
Eric W. Biedermanaa7da932015-03-07 16:23:23 -0600266 if (dec.ttl <= 1)
Eric W. Biederman01891972015-03-03 19:10:47 -0600267 goto drop;
268 dec.ttl -= 1;
269
270 /* Verify the destination can hold the packet */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700271 new_header_size = mpls_nh_header_size(nh);
Eric W. Biederman01891972015-03-03 19:10:47 -0600272 mtu = mpls_dev_mtu(out_dev);
273 if (mpls_pkt_too_big(skb, mtu - new_header_size))
274 goto drop;
275
276 hh_len = LL_RESERVED_SPACE(out_dev);
277 if (!out_dev->header_ops)
278 hh_len = 0;
279
280 /* Ensure there is enough space for the headers in the skb */
281 if (skb_cow(skb, hh_len + new_header_size))
282 goto drop;
283
284 skb->dev = out_dev;
285 skb->protocol = htons(ETH_P_MPLS_UC);
286
287 if (unlikely(!new_header_size && dec.bos)) {
288 /* Penultimate hop popping */
289 if (!mpls_egress(rt, skb, dec))
290 goto drop;
291 } else {
292 bool bos;
293 int i;
294 skb_push(skb, new_header_size);
295 skb_reset_network_header(skb);
296 /* Push the new labels */
297 hdr = mpls_hdr(skb);
298 bos = dec.bos;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700299 for (i = nh->nh_labels - 1; i >= 0; i--) {
300 hdr[i] = mpls_entry_encode(nh->nh_label[i],
301 dec.ttl, 0, bos);
Eric W. Biederman01891972015-03-03 19:10:47 -0600302 bos = false;
303 }
304 }
305
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700306 err = neigh_xmit(nh->nh_via_table, out_dev, nh->nh_via, skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600307 if (err)
308 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
309 __func__, err);
310 return 0;
311
312drop:
313 kfree_skb(skb);
314 return NET_RX_DROP;
315}
316
317static struct packet_type mpls_packet_type __read_mostly = {
318 .type = cpu_to_be16(ETH_P_MPLS_UC),
319 .func = mpls_forward,
320};
321
Wu Fengguangf0126532015-03-05 05:33:54 +0800322static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600323 [RTA_DST] = { .type = NLA_U32 },
324 [RTA_OIF] = { .type = NLA_U32 },
325};
326
Eric W. Biedermana2519922015-03-03 19:12:40 -0600327struct mpls_route_config {
Robert Shearman118d5232015-08-06 11:04:56 +0100328 u32 rc_protocol;
329 u32 rc_ifindex;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700330 u8 rc_via_table;
331 u8 rc_via_alen;
Robert Shearman118d5232015-08-06 11:04:56 +0100332 u8 rc_via[MAX_VIA_ALEN];
333 u32 rc_label;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700334 u8 rc_output_labels;
Robert Shearman118d5232015-08-06 11:04:56 +0100335 u32 rc_output_label[MAX_NEW_LABELS];
336 u32 rc_nlflags;
337 enum mpls_payload_type rc_payload_type;
338 struct nl_info rc_nlinfo;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700339 struct rtnexthop *rc_mp;
340 int rc_mp_len;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600341};
342
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700343static struct mpls_route *mpls_rt_alloc(int num_nh)
Eric W. Biederman01891972015-03-03 19:10:47 -0600344{
345 struct mpls_route *rt;
346
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700347 rt = kzalloc(sizeof(*rt) + (num_nh * sizeof(struct mpls_nh)),
348 GFP_KERNEL);
Eric W. Biederman01891972015-03-03 19:10:47 -0600349 if (rt)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700350 rt->rt_nhn = num_nh;
351
Eric W. Biederman01891972015-03-03 19:10:47 -0600352 return rt;
353}
354
355static void mpls_rt_free(struct mpls_route *rt)
356{
357 if (rt)
358 kfree_rcu(rt, rt_rcu);
359}
360
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600361static void mpls_notify_route(struct net *net, unsigned index,
362 struct mpls_route *old, struct mpls_route *new,
363 const struct nl_info *info)
364{
365 struct nlmsghdr *nlh = info ? info->nlh : NULL;
366 unsigned portid = info ? info->portid : 0;
367 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
368 struct mpls_route *rt = new ? new : old;
369 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
370 /* Ignore reserved labels for now */
Robert Shearmana6affd22015-08-03 17:50:04 +0100371 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600372 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
373}
374
Eric W. Biederman01891972015-03-03 19:10:47 -0600375static void mpls_route_update(struct net *net, unsigned index,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700376 struct mpls_route *new,
Eric W. Biederman01891972015-03-03 19:10:47 -0600377 const struct nl_info *info)
378{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600379 struct mpls_route __rcu **platform_label;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700380 struct mpls_route *rt;
Eric W. Biederman01891972015-03-03 19:10:47 -0600381
382 ASSERT_RTNL();
383
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600384 platform_label = rtnl_dereference(net->mpls.platform_label);
385 rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700386 rcu_assign_pointer(platform_label[index], new);
Eric W. Biederman01891972015-03-03 19:10:47 -0600387
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700388 mpls_notify_route(net, index, rt, new, info);
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600389
Eric W. Biederman01891972015-03-03 19:10:47 -0600390 /* If we removed a route free it now */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700391 mpls_rt_free(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -0600392}
393
Eric W. Biedermana2519922015-03-03 19:12:40 -0600394static unsigned find_free_label(struct net *net)
395{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600396 struct mpls_route __rcu **platform_label;
397 size_t platform_labels;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600398 unsigned index;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600399
400 platform_label = rtnl_dereference(net->mpls.platform_label);
401 platform_labels = net->mpls.platform_labels;
Robert Shearmana6affd22015-08-03 17:50:04 +0100402 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
403 index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600404 if (!rtnl_dereference(platform_label[index]))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600405 return index;
406 }
407 return LABEL_NOT_SPECIFIED;
408}
409
Roopa Prabhubf215632015-07-30 13:34:54 -0700410#if IS_ENABLED(CONFIG_INET)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700411static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
412{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300413 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700414 struct rtable *rt;
415 struct in_addr daddr;
416
417 memcpy(&daddr, addr, sizeof(struct in_addr));
418 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
419 if (IS_ERR(rt))
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300420 return ERR_CAST(rt);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700421
422 dev = rt->dst.dev;
423 dev_hold(dev);
424
425 ip_rt_put(rt);
426
Roopa Prabhu01faef22015-07-21 09:16:24 -0700427 return dev;
428}
Roopa Prabhubf215632015-07-30 13:34:54 -0700429#else
430static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
431{
432 return ERR_PTR(-EAFNOSUPPORT);
433}
434#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700435
Roopa Prabhubf215632015-07-30 13:34:54 -0700436#if IS_ENABLED(CONFIG_IPV6)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700437static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
438{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300439 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700440 struct dst_entry *dst;
441 struct flowi6 fl6;
Roopa Prabhubf215632015-07-30 13:34:54 -0700442 int err;
443
444 if (!ipv6_stub)
445 return ERR_PTR(-EAFNOSUPPORT);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700446
447 memset(&fl6, 0, sizeof(fl6));
448 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
Roopa Prabhubf215632015-07-30 13:34:54 -0700449 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
450 if (err)
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300451 return ERR_PTR(err);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700452
453 dev = dst->dev;
454 dev_hold(dev);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700455 dst_release(dst);
456
457 return dev;
458}
Roopa Prabhubf215632015-07-30 13:34:54 -0700459#else
460static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
461{
462 return ERR_PTR(-EAFNOSUPPORT);
463}
464#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700465
466static struct net_device *find_outdev(struct net *net,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700467 struct mpls_nh *nh, int oif)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700468{
469 struct net_device *dev = NULL;
470
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700471 if (!oif) {
472 switch (nh->nh_via_table) {
Roopa Prabhu01faef22015-07-21 09:16:24 -0700473 case NEIGH_ARP_TABLE:
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700474 dev = inet_fib_lookup_dev(net, nh->nh_via);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700475 break;
476 case NEIGH_ND_TABLE:
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700477 dev = inet6_fib_lookup_dev(net, nh->nh_via);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700478 break;
479 case NEIGH_LINK_TABLE:
480 break;
481 }
482 } else {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700483 dev = dev_get_by_index(net, oif);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700484 }
485
Roopa Prabhu3dcb6152015-08-04 06:36:24 -0700486 if (!dev)
487 return ERR_PTR(-ENODEV);
488
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700489 /* The caller is holding rtnl anyways, so release the dev reference */
490 dev_put(dev);
491
Roopa Prabhu01faef22015-07-21 09:16:24 -0700492 return dev;
493}
494
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700495static int mpls_nh_assign_dev(struct net *net, struct mpls_nh *nh, int oif)
496{
497 struct net_device *dev = NULL;
498 int err = -ENODEV;
499
500 dev = find_outdev(net, nh, oif);
501 if (IS_ERR(dev)) {
502 err = PTR_ERR(dev);
503 dev = NULL;
504 goto errout;
505 }
506
507 /* Ensure this is a supported device */
508 err = -EINVAL;
509 if (!mpls_dev_get(dev))
510 goto errout;
511
512 RCU_INIT_POINTER(nh->nh_dev, dev);
513
514 return 0;
515
516errout:
517 return err;
518}
519
520static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
521 struct mpls_route *rt)
522{
523 struct net *net = cfg->rc_nlinfo.nl_net;
524 struct mpls_nh *nh = rt->rt_nh;
525 int err;
526 int i;
527
528 if (!nh)
529 return -ENOMEM;
530
531 err = -EINVAL;
532 /* Ensure only a supported number of labels are present */
533 if (cfg->rc_output_labels > MAX_NEW_LABELS)
534 goto errout;
535
536 nh->nh_labels = cfg->rc_output_labels;
537 for (i = 0; i < nh->nh_labels; i++)
538 nh->nh_label[i] = cfg->rc_output_label[i];
539
540 nh->nh_via_table = cfg->rc_via_table;
541 memcpy(nh->nh_via, cfg->rc_via, cfg->rc_via_alen);
542 nh->nh_via_alen = cfg->rc_via_alen;
543
544 err = mpls_nh_assign_dev(net, nh, cfg->rc_ifindex);
545 if (err)
546 goto errout;
547
548 return 0;
549
550errout:
551 return err;
552}
553
554static int mpls_nh_build(struct net *net, struct mpls_nh *nh,
555 int oif, struct nlattr *via, struct nlattr *newdst)
556{
557 int err = -ENOMEM;
558
559 if (!nh)
560 goto errout;
561
562 if (newdst) {
563 err = nla_get_labels(newdst, MAX_NEW_LABELS,
564 &nh->nh_labels, nh->nh_label);
565 if (err)
566 goto errout;
567 }
568
569 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
570 nh->nh_via);
571 if (err)
572 goto errout;
573
574 err = mpls_nh_assign_dev(net, nh, oif);
575 if (err)
576 goto errout;
577
578 return 0;
579
580errout:
581 return err;
582}
583
584static int mpls_count_nexthops(struct rtnexthop *rtnh, int len)
585{
586 int nhs = 0;
587 int remaining = len;
588
589 while (rtnh_ok(rtnh, remaining)) {
590 nhs++;
591 rtnh = rtnh_next(rtnh, &remaining);
592 }
593
594 /* leftover implies invalid nexthop configuration, discard it */
595 return remaining > 0 ? 0 : nhs;
596}
597
598static int mpls_nh_build_multi(struct mpls_route_config *cfg,
599 struct mpls_route *rt)
600{
601 struct rtnexthop *rtnh = cfg->rc_mp;
602 struct nlattr *nla_via, *nla_newdst;
603 int remaining = cfg->rc_mp_len;
604 int nhs = 0;
605 int err = 0;
606
607 change_nexthops(rt) {
608 int attrlen;
609
610 nla_via = NULL;
611 nla_newdst = NULL;
612
613 err = -EINVAL;
614 if (!rtnh_ok(rtnh, remaining))
615 goto errout;
616
Robert Shearman1c78efa2015-10-23 06:03:28 -0700617 /* neither weighted multipath nor any flags
618 * are supported
619 */
620 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
621 goto errout;
622
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700623 attrlen = rtnh_attrlen(rtnh);
624 if (attrlen > 0) {
625 struct nlattr *attrs = rtnh_attrs(rtnh);
626
627 nla_via = nla_find(attrs, attrlen, RTA_VIA);
628 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
629 }
630
631 if (!nla_via)
632 goto errout;
633
634 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, nh,
635 rtnh->rtnh_ifindex, nla_via,
636 nla_newdst);
637 if (err)
638 goto errout;
639
640 rtnh = rtnh_next(rtnh, &remaining);
641 nhs++;
642 } endfor_nexthops(rt);
643
644 rt->rt_nhn = nhs;
645
646 return 0;
647
648errout:
649 return err;
650}
651
Eric W. Biedermana2519922015-03-03 19:12:40 -0600652static int mpls_route_add(struct mpls_route_config *cfg)
653{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600654 struct mpls_route __rcu **platform_label;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600655 struct net *net = cfg->rc_nlinfo.nl_net;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600656 struct mpls_route *rt, *old;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600657 int err = -EINVAL;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700658 unsigned index;
659 int nhs = 1; /* default to one nexthop */
Eric W. Biedermana2519922015-03-03 19:12:40 -0600660
661 index = cfg->rc_label;
662
663 /* If a label was not specified during insert pick one */
664 if ((index == LABEL_NOT_SPECIFIED) &&
665 (cfg->rc_nlflags & NLM_F_CREATE)) {
666 index = find_free_label(net);
667 }
668
Robert Shearmana6affd22015-08-03 17:50:04 +0100669 /* Reserved labels may not be set */
670 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600671 goto errout;
672
673 /* The full 20 bit range may not be supported. */
674 if (index >= net->mpls.platform_labels)
675 goto errout;
676
Eric W. Biedermana2519922015-03-03 19:12:40 -0600677 /* Append makes no sense with mpls */
Eric W. Biederman0f7bbd52015-03-07 16:22:40 -0600678 err = -EOPNOTSUPP;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600679 if (cfg->rc_nlflags & NLM_F_APPEND)
680 goto errout;
681
682 err = -EEXIST;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600683 platform_label = rtnl_dereference(net->mpls.platform_label);
684 old = rtnl_dereference(platform_label[index]);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600685 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
686 goto errout;
687
688 err = -EEXIST;
689 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
690 goto errout;
691
692 err = -ENOENT;
693 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
694 goto errout;
695
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700696 if (cfg->rc_mp) {
697 err = -EINVAL;
698 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len);
699 if (nhs == 0)
700 goto errout;
701 }
702
Eric W. Biedermana2519922015-03-03 19:12:40 -0600703 err = -ENOMEM;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700704 rt = mpls_rt_alloc(nhs);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600705 if (!rt)
706 goto errout;
707
Eric W. Biedermana2519922015-03-03 19:12:40 -0600708 rt->rt_protocol = cfg->rc_protocol;
Robert Shearman118d5232015-08-06 11:04:56 +0100709 rt->rt_payload_type = cfg->rc_payload_type;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600710
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700711 if (cfg->rc_mp)
712 err = mpls_nh_build_multi(cfg, rt);
713 else
714 err = mpls_nh_build_from_cfg(cfg, rt);
715 if (err)
716 goto freert;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600717
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700718 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
719
Eric W. Biedermana2519922015-03-03 19:12:40 -0600720 return 0;
721
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700722freert:
723 mpls_rt_free(rt);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600724errout:
Eric W. Biedermana2519922015-03-03 19:12:40 -0600725 return err;
726}
727
728static int mpls_route_del(struct mpls_route_config *cfg)
729{
730 struct net *net = cfg->rc_nlinfo.nl_net;
731 unsigned index;
732 int err = -EINVAL;
733
734 index = cfg->rc_label;
735
Robert Shearmana6affd22015-08-03 17:50:04 +0100736 /* Reserved labels may not be removed */
737 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600738 goto errout;
739
740 /* The full 20 bit range may not be supported */
741 if (index >= net->mpls.platform_labels)
742 goto errout;
743
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700744 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600745
746 err = 0;
747errout:
748 return err;
749}
750
Robert Shearman37bde792015-04-22 11:14:38 +0100751#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
752 (&((struct mpls_dev *)0)->field)
753
754static const struct ctl_table mpls_dev_table[] = {
755 {
756 .procname = "input",
757 .maxlen = sizeof(int),
758 .mode = 0644,
759 .proc_handler = proc_dointvec,
760 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
761 },
762 { }
763};
764
765static int mpls_dev_sysctl_register(struct net_device *dev,
766 struct mpls_dev *mdev)
767{
768 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
769 struct ctl_table *table;
770 int i;
771
772 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
773 if (!table)
774 goto out;
775
776 /* Table data contains only offsets relative to the base of
777 * the mdev at this point, so make them absolute.
778 */
779 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
780 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
781
782 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
783
784 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
785 if (!mdev->sysctl)
786 goto free;
787
788 return 0;
789
790free:
791 kfree(table);
792out:
793 return -ENOBUFS;
794}
795
796static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
797{
798 struct ctl_table *table;
799
800 table = mdev->sysctl->ctl_table_arg;
801 unregister_net_sysctl_table(mdev->sysctl);
802 kfree(table);
803}
804
Robert Shearman03c57742015-04-22 11:14:37 +0100805static struct mpls_dev *mpls_add_dev(struct net_device *dev)
806{
807 struct mpls_dev *mdev;
808 int err = -ENOMEM;
809
810 ASSERT_RTNL();
811
812 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
813 if (!mdev)
814 return ERR_PTR(err);
815
Robert Shearman37bde792015-04-22 11:14:38 +0100816 err = mpls_dev_sysctl_register(dev, mdev);
817 if (err)
818 goto free;
819
Robert Shearman03c57742015-04-22 11:14:37 +0100820 rcu_assign_pointer(dev->mpls_ptr, mdev);
821
822 return mdev;
Robert Shearman37bde792015-04-22 11:14:38 +0100823
824free:
825 kfree(mdev);
826 return ERR_PTR(err);
Robert Shearman03c57742015-04-22 11:14:37 +0100827}
828
Eric W. Biederman01891972015-03-03 19:10:47 -0600829static void mpls_ifdown(struct net_device *dev)
830{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600831 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -0600832 struct net *net = dev_net(dev);
Robert Shearman03c57742015-04-22 11:14:37 +0100833 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600834 unsigned index;
835
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600836 platform_label = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -0600837 for (index = 0; index < net->mpls.platform_labels; index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600838 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman01891972015-03-03 19:10:47 -0600839 if (!rt)
840 continue;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700841 for_nexthops(rt) {
842 if (rtnl_dereference(nh->nh_dev) != dev)
843 continue;
844 nh->nh_dev = NULL;
845 } endfor_nexthops(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -0600846 }
Robert Shearman03c57742015-04-22 11:14:37 +0100847
848 mdev = mpls_dev_get(dev);
849 if (!mdev)
850 return;
851
Robert Shearman37bde792015-04-22 11:14:38 +0100852 mpls_dev_sysctl_unregister(mdev);
853
Robert Shearman03c57742015-04-22 11:14:37 +0100854 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
855
Robert Shearman25cc8f02015-06-05 18:54:45 +0100856 kfree_rcu(mdev, rcu);
Eric W. Biederman01891972015-03-03 19:10:47 -0600857}
858
859static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
860 void *ptr)
861{
862 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Robert Shearman03c57742015-04-22 11:14:37 +0100863 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600864
865 switch(event) {
Robert Shearman03c57742015-04-22 11:14:37 +0100866 case NETDEV_REGISTER:
867 /* For now just support ethernet devices */
868 if ((dev->type == ARPHRD_ETHER) ||
869 (dev->type == ARPHRD_LOOPBACK)) {
870 mdev = mpls_add_dev(dev);
871 if (IS_ERR(mdev))
872 return notifier_from_errno(PTR_ERR(mdev));
873 }
874 break;
875
Eric W. Biederman01891972015-03-03 19:10:47 -0600876 case NETDEV_UNREGISTER:
877 mpls_ifdown(dev);
878 break;
Robert Shearman0fae3bf2015-06-11 19:58:26 +0100879 case NETDEV_CHANGENAME:
880 mdev = mpls_dev_get(dev);
881 if (mdev) {
882 int err;
883
884 mpls_dev_sysctl_unregister(mdev);
885 err = mpls_dev_sysctl_register(dev, mdev);
886 if (err)
887 return notifier_from_errno(err);
888 }
889 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600890 }
891 return NOTIFY_OK;
892}
893
894static struct notifier_block mpls_dev_notifier = {
895 .notifier_call = mpls_dev_notify,
896};
897
Eric W. Biederman03c05662015-03-03 19:13:56 -0600898static int nla_put_via(struct sk_buff *skb,
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600899 u8 table, const void *addr, int alen)
Eric W. Biederman03c05662015-03-03 19:13:56 -0600900{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600901 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
902 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
903 };
Eric W. Biederman03c05662015-03-03 19:13:56 -0600904 struct nlattr *nla;
905 struct rtvia *via;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600906 int family = AF_UNSPEC;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600907
908 nla = nla_reserve(skb, RTA_VIA, alen + 2);
909 if (!nla)
910 return -EMSGSIZE;
911
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600912 if (table <= NEIGH_NR_TABLES)
913 family = table_to_family[table];
914
Eric W. Biederman03c05662015-03-03 19:13:56 -0600915 via = nla_data(nla);
916 via->rtvia_family = family;
917 memcpy(via->rtvia_addr, addr, alen);
918 return 0;
919}
920
Eric W. Biederman966bae32015-03-03 19:13:19 -0600921int nla_put_labels(struct sk_buff *skb, int attrtype,
922 u8 labels, const u32 label[])
923{
924 struct nlattr *nla;
925 struct mpls_shim_hdr *nla_label;
926 bool bos;
927 int i;
928 nla = nla_reserve(skb, attrtype, labels*4);
929 if (!nla)
930 return -EMSGSIZE;
931
932 nla_label = nla_data(nla);
933 bos = true;
934 for (i = labels - 1; i >= 0; i--) {
935 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
936 bos = false;
937 }
938
939 return 0;
940}
Roopa Prabhuface0182015-07-21 10:43:52 +0200941EXPORT_SYMBOL_GPL(nla_put_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600942
943int nla_get_labels(const struct nlattr *nla,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700944 u32 max_labels, u8 *labels, u32 label[])
Eric W. Biederman966bae32015-03-03 19:13:19 -0600945{
946 unsigned len = nla_len(nla);
947 unsigned nla_labels;
948 struct mpls_shim_hdr *nla_label;
949 bool bos;
950 int i;
951
952 /* len needs to be an even multiple of 4 (the label size) */
953 if (len & 3)
954 return -EINVAL;
955
956 /* Limit the number of new labels allowed */
957 nla_labels = len/4;
958 if (nla_labels > max_labels)
959 return -EINVAL;
960
961 nla_label = nla_data(nla);
962 bos = true;
963 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
964 struct mpls_entry_decoded dec;
965 dec = mpls_entry_decode(nla_label + i);
966
967 /* Ensure the bottom of stack flag is properly set
968 * and ttl and tc are both clear.
969 */
970 if ((dec.bos != bos) || dec.ttl || dec.tc)
971 return -EINVAL;
972
Robert Shearman5a9ab012015-04-22 11:14:39 +0100973 switch (dec.label) {
Tom Herbert78f5b892015-05-07 08:08:51 -0700974 case MPLS_LABEL_IMPLNULL:
Robert Shearman5a9ab012015-04-22 11:14:39 +0100975 /* RFC3032: This is a label that an LSR may
976 * assign and distribute, but which never
977 * actually appears in the encapsulation.
978 */
979 return -EINVAL;
980 }
981
Eric W. Biederman966bae32015-03-03 19:13:19 -0600982 label[i] = dec.label;
983 }
984 *labels = nla_labels;
985 return 0;
986}
Roopa Prabhuface0182015-07-21 10:43:52 +0200987EXPORT_SYMBOL_GPL(nla_get_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600988
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700989int nla_get_via(const struct nlattr *nla, u8 *via_alen,
990 u8 *via_table, u8 via_addr[])
991{
992 struct rtvia *via = nla_data(nla);
993 int err = -EINVAL;
994 int alen;
995
996 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
997 goto errout;
998 alen = nla_len(nla) -
999 offsetof(struct rtvia, rtvia_addr);
1000 if (alen > MAX_VIA_ALEN)
1001 goto errout;
1002
1003 /* Validate the address family */
1004 switch (via->rtvia_family) {
1005 case AF_PACKET:
1006 *via_table = NEIGH_LINK_TABLE;
1007 break;
1008 case AF_INET:
1009 *via_table = NEIGH_ARP_TABLE;
1010 if (alen != 4)
1011 goto errout;
1012 break;
1013 case AF_INET6:
1014 *via_table = NEIGH_ND_TABLE;
1015 if (alen != 16)
1016 goto errout;
1017 break;
1018 default:
1019 /* Unsupported address family */
1020 goto errout;
1021 }
1022
1023 memcpy(via_addr, via->rtvia_addr, alen);
1024 *via_alen = alen;
1025 err = 0;
1026
1027errout:
1028 return err;
1029}
1030
Eric W. Biederman03c05662015-03-03 19:13:56 -06001031static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1032 struct mpls_route_config *cfg)
1033{
1034 struct rtmsg *rtm;
1035 struct nlattr *tb[RTA_MAX+1];
1036 int index;
1037 int err;
1038
1039 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1040 if (err < 0)
1041 goto errout;
1042
1043 err = -EINVAL;
1044 rtm = nlmsg_data(nlh);
1045 memset(cfg, 0, sizeof(*cfg));
1046
1047 if (rtm->rtm_family != AF_MPLS)
1048 goto errout;
1049 if (rtm->rtm_dst_len != 20)
1050 goto errout;
1051 if (rtm->rtm_src_len != 0)
1052 goto errout;
1053 if (rtm->rtm_tos != 0)
1054 goto errout;
1055 if (rtm->rtm_table != RT_TABLE_MAIN)
1056 goto errout;
1057 /* Any value is acceptable for rtm_protocol */
1058
1059 /* As mpls uses destination specific addresses
1060 * (or source specific address in the case of multicast)
1061 * all addresses have universal scope.
1062 */
1063 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1064 goto errout;
1065 if (rtm->rtm_type != RTN_UNICAST)
1066 goto errout;
1067 if (rtm->rtm_flags != 0)
1068 goto errout;
1069
1070 cfg->rc_label = LABEL_NOT_SPECIFIED;
1071 cfg->rc_protocol = rtm->rtm_protocol;
1072 cfg->rc_nlflags = nlh->nlmsg_flags;
1073 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1074 cfg->rc_nlinfo.nlh = nlh;
1075 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1076
1077 for (index = 0; index <= RTA_MAX; index++) {
1078 struct nlattr *nla = tb[index];
1079 if (!nla)
1080 continue;
1081
1082 switch(index) {
1083 case RTA_OIF:
1084 cfg->rc_ifindex = nla_get_u32(nla);
1085 break;
1086 case RTA_NEWDST:
1087 if (nla_get_labels(nla, MAX_NEW_LABELS,
1088 &cfg->rc_output_labels,
1089 cfg->rc_output_label))
1090 goto errout;
1091 break;
1092 case RTA_DST:
1093 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001094 u8 label_count;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001095 if (nla_get_labels(nla, 1, &label_count,
1096 &cfg->rc_label))
1097 goto errout;
1098
Robert Shearmana6affd22015-08-03 17:50:04 +01001099 /* Reserved labels may not be set */
1100 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001101 goto errout;
1102
1103 break;
1104 }
1105 case RTA_VIA:
1106 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001107 if (nla_get_via(nla, &cfg->rc_via_alen,
1108 &cfg->rc_via_table, cfg->rc_via))
Robert Shearmanf8d54af2015-03-06 10:47:00 +00001109 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001110 break;
1111 }
1112 case RTA_MULTIPATH:
1113 {
1114 cfg->rc_mp = nla_data(nla);
1115 cfg->rc_mp_len = nla_len(nla);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001116 break;
1117 }
1118 default:
1119 /* Unsupported attribute */
1120 goto errout;
1121 }
1122 }
1123
1124 err = 0;
1125errout:
1126 return err;
1127}
1128
1129static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1130{
1131 struct mpls_route_config cfg;
1132 int err;
1133
1134 err = rtm_to_route_config(skb, nlh, &cfg);
1135 if (err < 0)
1136 return err;
1137
1138 return mpls_route_del(&cfg);
1139}
1140
1141
1142static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1143{
1144 struct mpls_route_config cfg;
1145 int err;
1146
1147 err = rtm_to_route_config(skb, nlh, &cfg);
1148 if (err < 0)
1149 return err;
1150
1151 return mpls_route_add(&cfg);
1152}
1153
1154static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1155 u32 label, struct mpls_route *rt, int flags)
1156{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001157 struct net_device *dev;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001158 struct nlmsghdr *nlh;
1159 struct rtmsg *rtm;
1160
1161 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1162 if (nlh == NULL)
1163 return -EMSGSIZE;
1164
1165 rtm = nlmsg_data(nlh);
1166 rtm->rtm_family = AF_MPLS;
1167 rtm->rtm_dst_len = 20;
1168 rtm->rtm_src_len = 0;
1169 rtm->rtm_tos = 0;
1170 rtm->rtm_table = RT_TABLE_MAIN;
1171 rtm->rtm_protocol = rt->rt_protocol;
1172 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1173 rtm->rtm_type = RTN_UNICAST;
1174 rtm->rtm_flags = 0;
1175
Eric W. Biederman03c05662015-03-03 19:13:56 -06001176 if (nla_put_labels(skb, RTA_DST, 1, &label))
1177 goto nla_put_failure;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001178 if (rt->rt_nhn == 1) {
1179 struct mpls_nh *nh = rt->rt_nh;
1180
1181 if (nh->nh_labels &&
1182 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1183 nh->nh_label))
1184 goto nla_put_failure;
1185 if (nla_put_via(skb, nh->nh_via_table, nh->nh_via,
1186 nh->nh_via_alen))
1187 goto nla_put_failure;
1188 dev = rtnl_dereference(nh->nh_dev);
1189 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1190 goto nla_put_failure;
1191 } else {
1192 struct rtnexthop *rtnh;
1193 struct nlattr *mp;
1194
1195 mp = nla_nest_start(skb, RTA_MULTIPATH);
1196 if (!mp)
1197 goto nla_put_failure;
1198
1199 for_nexthops(rt) {
1200 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1201 if (!rtnh)
1202 goto nla_put_failure;
1203
1204 dev = rtnl_dereference(nh->nh_dev);
1205 if (dev)
1206 rtnh->rtnh_ifindex = dev->ifindex;
1207 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1208 nh->nh_labels,
1209 nh->nh_label))
1210 goto nla_put_failure;
1211 if (nla_put_via(skb, nh->nh_via_table,
1212 nh->nh_via,
1213 nh->nh_via_alen))
1214 goto nla_put_failure;
1215
1216 /* length of rtnetlink header + attributes */
1217 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1218 } endfor_nexthops(rt);
1219
1220 nla_nest_end(skb, mp);
1221 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001222
1223 nlmsg_end(skb, nlh);
1224 return 0;
1225
1226nla_put_failure:
1227 nlmsg_cancel(skb, nlh);
1228 return -EMSGSIZE;
1229}
1230
1231static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1232{
1233 struct net *net = sock_net(skb->sk);
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001234 struct mpls_route __rcu **platform_label;
1235 size_t platform_labels;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001236 unsigned int index;
1237
1238 ASSERT_RTNL();
1239
1240 index = cb->args[0];
Robert Shearmana6affd22015-08-03 17:50:04 +01001241 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1242 index = MPLS_LABEL_FIRST_UNRESERVED;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001243
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001244 platform_label = rtnl_dereference(net->mpls.platform_label);
1245 platform_labels = net->mpls.platform_labels;
1246 for (; index < platform_labels; index++) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001247 struct mpls_route *rt;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001248 rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001249 if (!rt)
1250 continue;
1251
1252 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1253 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1254 index, rt, NLM_F_MULTI) < 0)
1255 break;
1256 }
1257 cb->args[0] = index;
1258
1259 return skb->len;
1260}
1261
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001262static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1263{
1264 size_t payload =
1265 NLMSG_ALIGN(sizeof(struct rtmsg))
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001266 + nla_total_size(4); /* RTA_DST */
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001267
1268 if (rt->rt_nhn == 1) {
1269 struct mpls_nh *nh = rt->rt_nh;
1270
1271 if (nh->nh_dev)
1272 payload += nla_total_size(4); /* RTA_OIF */
1273 payload += nla_total_size(2 + nh->nh_via_alen); /* RTA_VIA */
1274 if (nh->nh_labels) /* RTA_NEWDST */
1275 payload += nla_total_size(nh->nh_labels * 4);
1276 } else {
1277 /* each nexthop is packed in an attribute */
1278 size_t nhsize = 0;
1279
1280 for_nexthops(rt) {
1281 nhsize += nla_total_size(sizeof(struct rtnexthop));
1282 nhsize += nla_total_size(2 + nh->nh_via_alen);
1283 if (nh->nh_labels)
1284 nhsize += nla_total_size(nh->nh_labels * 4);
1285 } endfor_nexthops(rt);
1286 /* nested attribute */
1287 payload += nla_total_size(nhsize);
1288 }
1289
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001290 return payload;
1291}
1292
1293static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1294 struct nlmsghdr *nlh, struct net *net, u32 portid,
1295 unsigned int nlm_flags)
1296{
1297 struct sk_buff *skb;
1298 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1299 int err = -ENOBUFS;
1300
1301 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1302 if (skb == NULL)
1303 goto errout;
1304
1305 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1306 if (err < 0) {
1307 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1308 WARN_ON(err == -EMSGSIZE);
1309 kfree_skb(skb);
1310 goto errout;
1311 }
1312 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1313
1314 return;
1315errout:
1316 if (err < 0)
1317 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1318}
1319
Eric W. Biederman7720c012015-03-03 19:11:20 -06001320static int resize_platform_label_table(struct net *net, size_t limit)
1321{
1322 size_t size = sizeof(struct mpls_route *) * limit;
1323 size_t old_limit;
1324 size_t cp_size;
1325 struct mpls_route __rcu **labels = NULL, **old;
1326 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1327 unsigned index;
1328
1329 if (size) {
1330 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1331 if (!labels)
1332 labels = vzalloc(size);
1333
1334 if (!labels)
1335 goto nolabels;
1336 }
1337
1338 /* In case the predefined labels need to be populated */
Tom Herbert78f5b892015-05-07 08:08:51 -07001339 if (limit > MPLS_LABEL_IPV4NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001340 struct net_device *lo = net->loopback_dev;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001341 rt0 = mpls_rt_alloc(1);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001342 if (!rt0)
1343 goto nort0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001344 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001345 rt0->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001346 rt0->rt_payload_type = MPT_IPV4;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001347 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1348 memcpy(rt0->rt_nh->nh_via, lo->dev_addr, lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001349 }
Tom Herbert78f5b892015-05-07 08:08:51 -07001350 if (limit > MPLS_LABEL_IPV6NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001351 struct net_device *lo = net->loopback_dev;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001352 rt2 = mpls_rt_alloc(1);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001353 if (!rt2)
1354 goto nort2;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001355 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001356 rt2->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001357 rt2->rt_payload_type = MPT_IPV6;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001358 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1359 memcpy(rt2->rt_nh->nh_via, lo->dev_addr, lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001360 }
1361
1362 rtnl_lock();
1363 /* Remember the original table */
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001364 old = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001365 old_limit = net->mpls.platform_labels;
1366
1367 /* Free any labels beyond the new table */
1368 for (index = limit; index < old_limit; index++)
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001369 mpls_route_update(net, index, NULL, NULL);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001370
1371 /* Copy over the old labels */
1372 cp_size = size;
1373 if (old_limit < limit)
1374 cp_size = old_limit * sizeof(struct mpls_route *);
1375
1376 memcpy(labels, old, cp_size);
1377
1378 /* If needed set the predefined labels */
Tom Herbert78f5b892015-05-07 08:08:51 -07001379 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1380 (limit > MPLS_LABEL_IPV6NULL)) {
1381 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001382 rt2 = NULL;
1383 }
1384
Tom Herbert78f5b892015-05-07 08:08:51 -07001385 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1386 (limit > MPLS_LABEL_IPV4NULL)) {
1387 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001388 rt0 = NULL;
1389 }
1390
1391 /* Update the global pointers */
1392 net->mpls.platform_labels = limit;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001393 rcu_assign_pointer(net->mpls.platform_label, labels);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001394
1395 rtnl_unlock();
1396
1397 mpls_rt_free(rt2);
1398 mpls_rt_free(rt0);
1399
1400 if (old) {
1401 synchronize_rcu();
1402 kvfree(old);
1403 }
1404 return 0;
1405
1406nort2:
1407 mpls_rt_free(rt0);
1408nort0:
1409 kvfree(labels);
1410nolabels:
1411 return -ENOMEM;
1412}
1413
1414static int mpls_platform_labels(struct ctl_table *table, int write,
1415 void __user *buffer, size_t *lenp, loff_t *ppos)
1416{
1417 struct net *net = table->data;
1418 int platform_labels = net->mpls.platform_labels;
1419 int ret;
1420 struct ctl_table tmp = {
1421 .procname = table->procname,
1422 .data = &platform_labels,
1423 .maxlen = sizeof(int),
1424 .mode = table->mode,
1425 .extra1 = &zero,
1426 .extra2 = &label_limit,
1427 };
1428
1429 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1430
1431 if (write && ret == 0)
1432 ret = resize_platform_label_table(net, platform_labels);
1433
1434 return ret;
1435}
1436
Robert Shearman37bde792015-04-22 11:14:38 +01001437static const struct ctl_table mpls_table[] = {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001438 {
1439 .procname = "platform_labels",
1440 .data = NULL,
1441 .maxlen = sizeof(int),
1442 .mode = 0644,
1443 .proc_handler = mpls_platform_labels,
1444 },
1445 { }
1446};
1447
Eric W. Biederman01891972015-03-03 19:10:47 -06001448static int mpls_net_init(struct net *net)
1449{
Eric W. Biederman7720c012015-03-03 19:11:20 -06001450 struct ctl_table *table;
1451
Eric W. Biederman01891972015-03-03 19:10:47 -06001452 net->mpls.platform_labels = 0;
1453 net->mpls.platform_label = NULL;
1454
Eric W. Biederman7720c012015-03-03 19:11:20 -06001455 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1456 if (table == NULL)
1457 return -ENOMEM;
1458
1459 table[0].data = net;
1460 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07001461 if (net->mpls.ctl == NULL) {
1462 kfree(table);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001463 return -ENOMEM;
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07001464 }
Eric W. Biederman7720c012015-03-03 19:11:20 -06001465
Eric W. Biederman01891972015-03-03 19:10:47 -06001466 return 0;
1467}
1468
1469static void mpls_net_exit(struct net *net)
1470{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001471 struct mpls_route __rcu **platform_label;
1472 size_t platform_labels;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001473 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -06001474 unsigned int index;
1475
Eric W. Biederman7720c012015-03-03 19:11:20 -06001476 table = net->mpls.ctl->ctl_table_arg;
1477 unregister_net_sysctl_table(net->mpls.ctl);
1478 kfree(table);
1479
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001480 /* An rcu grace period has passed since there was a device in
1481 * the network namespace (and thus the last in flight packet)
Eric W. Biederman01891972015-03-03 19:10:47 -06001482 * left this network namespace. This is because
1483 * unregister_netdevice_many and netdev_run_todo has completed
1484 * for each network device that was in this network namespace.
1485 *
1486 * As such no additional rcu synchronization is necessary when
1487 * freeing the platform_label table.
1488 */
1489 rtnl_lock();
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001490 platform_label = rtnl_dereference(net->mpls.platform_label);
1491 platform_labels = net->mpls.platform_labels;
1492 for (index = 0; index < platform_labels; index++) {
1493 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1494 RCU_INIT_POINTER(platform_label[index], NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001495 mpls_rt_free(rt);
1496 }
1497 rtnl_unlock();
1498
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001499 kvfree(platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06001500}
1501
1502static struct pernet_operations mpls_net_ops = {
1503 .init = mpls_net_init,
1504 .exit = mpls_net_exit,
1505};
1506
1507static int __init mpls_init(void)
1508{
1509 int err;
1510
1511 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1512
1513 err = register_pernet_subsys(&mpls_net_ops);
1514 if (err)
1515 goto out;
1516
1517 err = register_netdevice_notifier(&mpls_dev_notifier);
1518 if (err)
1519 goto out_unregister_pernet;
1520
1521 dev_add_pack(&mpls_packet_type);
1522
Eric W. Biederman03c05662015-03-03 19:13:56 -06001523 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1524 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1525 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001526 err = 0;
1527out:
1528 return err;
1529
1530out_unregister_pernet:
1531 unregister_pernet_subsys(&mpls_net_ops);
1532 goto out;
1533}
1534module_init(mpls_init);
1535
1536static void __exit mpls_exit(void)
1537{
Eric W. Biederman03c05662015-03-03 19:13:56 -06001538 rtnl_unregister_all(PF_MPLS);
Eric W. Biederman01891972015-03-03 19:10:47 -06001539 dev_remove_pack(&mpls_packet_type);
1540 unregister_netdevice_notifier(&mpls_dev_notifier);
1541 unregister_pernet_subsys(&mpls_net_ops);
1542}
1543module_exit(mpls_exit);
1544
1545MODULE_DESCRIPTION("MultiProtocol Label Switching");
1546MODULE_LICENSE("GPL v2");
1547MODULE_ALIAS_NETPROTO(PF_MPLS);