blob: 4b3b9b310c3a00baba320d4071ea98d108a79d48 [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
Robert Shearmancf4b24f2015-10-27 00:37:36 +000060static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
61{
62 u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
63 int nh_index = nh - rt->rt_nh;
64
65 return nh0_via + rt->rt_max_alen * nh_index;
66}
67
68static const u8 *mpls_nh_via(const struct mpls_route *rt,
69 const struct mpls_nh *nh)
70{
71 return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
72}
73
Roopa Prabhuf8efb732015-10-23 06:03:27 -070074static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
Eric W. Biederman01891972015-03-03 19:10:47 -060075{
76 /* The size of the layer 2.5 labels to be added for this route */
Roopa Prabhuf8efb732015-10-23 06:03:27 -070077 return nh->nh_labels * sizeof(struct mpls_shim_hdr);
Eric W. Biederman01891972015-03-03 19:10:47 -060078}
79
Roopa Prabhuface0182015-07-21 10:43:52 +020080unsigned int mpls_dev_mtu(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060081{
82 /* The amount of data the layer 2 frame can hold */
83 return dev->mtu;
84}
Roopa Prabhuface0182015-07-21 10:43:52 +020085EXPORT_SYMBOL_GPL(mpls_dev_mtu);
Eric W. Biederman01891972015-03-03 19:10:47 -060086
Roopa Prabhuface0182015-07-21 10:43:52 +020087bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
Eric W. Biederman01891972015-03-03 19:10:47 -060088{
89 if (skb->len <= mtu)
90 return false;
91
92 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
93 return false;
94
95 return true;
96}
Roopa Prabhuface0182015-07-21 10:43:52 +020097EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
Eric W. Biederman01891972015-03-03 19:10:47 -060098
Roopa Prabhuc89359a2015-12-01 22:18:11 -080099static u32 mpls_multipath_hash(struct mpls_route *rt,
100 struct sk_buff *skb, bool bos)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700101{
Robert Shearman1c78efa2015-10-23 06:03:28 -0700102 struct mpls_entry_decoded dec;
103 struct mpls_shim_hdr *hdr;
104 bool eli_seen = false;
105 int label_index;
Robert Shearman1c78efa2015-10-23 06:03:28 -0700106 u32 hash = 0;
107
Robert Shearman1c78efa2015-10-23 06:03:28 -0700108 for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos;
109 label_index++) {
110 if (!pskb_may_pull(skb, sizeof(*hdr) * label_index))
111 break;
112
113 /* Read and decode the current label */
114 hdr = mpls_hdr(skb) + label_index;
115 dec = mpls_entry_decode(hdr);
116
117 /* RFC6790 - reserved labels MUST NOT be used as keys
118 * for the load-balancing function
119 */
120 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
121 hash = jhash_1word(dec.label, hash);
122
123 /* The entropy label follows the entropy label
124 * indicator, so this means that the entropy
125 * label was just added to the hash - no need to
126 * go any deeper either in the label stack or in the
127 * payload
128 */
129 if (eli_seen)
130 break;
131 } else if (dec.label == MPLS_LABEL_ENTROPY) {
132 eli_seen = true;
133 }
134
135 bos = dec.bos;
136 if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index +
137 sizeof(struct iphdr))) {
138 const struct iphdr *v4hdr;
139
140 v4hdr = (const struct iphdr *)(mpls_hdr(skb) +
141 label_index);
142 if (v4hdr->version == 4) {
143 hash = jhash_3words(ntohl(v4hdr->saddr),
144 ntohl(v4hdr->daddr),
145 v4hdr->protocol, hash);
146 } else if (v4hdr->version == 6 &&
147 pskb_may_pull(skb, sizeof(*hdr) * label_index +
148 sizeof(struct ipv6hdr))) {
149 const struct ipv6hdr *v6hdr;
150
151 v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) +
152 label_index);
153
154 hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
155 hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
156 hash = jhash_1word(v6hdr->nexthdr, hash);
157 }
158 }
159 }
160
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800161 return hash;
162}
163
164static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
165 struct sk_buff *skb, bool bos)
166{
167 int alive = ACCESS_ONCE(rt->rt_nhn_alive);
168 u32 hash = 0;
169 int nh_index = 0;
170 int n = 0;
171
172 /* No need to look further into packet if there's only
173 * one path
174 */
175 if (rt->rt_nhn == 1)
176 goto out;
177
178 if (alive <= 0)
179 return NULL;
180
181 hash = mpls_multipath_hash(rt, skb, bos);
182 nh_index = hash % alive;
183 if (alive == rt->rt_nhn)
184 goto out;
185 for_nexthops(rt) {
186 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
187 continue;
188 if (n == nh_index)
189 return nh;
190 n++;
191 } endfor_nexthops(rt);
192
Robert Shearman1c78efa2015-10-23 06:03:28 -0700193out:
194 return &rt->rt_nh[nh_index];
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700195}
196
Eric W. Biederman01891972015-03-03 19:10:47 -0600197static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
198 struct mpls_entry_decoded dec)
199{
Robert Shearman118d5232015-08-06 11:04:56 +0100200 enum mpls_payload_type payload_type;
201 bool success = false;
Eric W. Biederman01891972015-03-03 19:10:47 -0600202
Eric W. Biederman76fecd82015-03-12 18:22:59 -0500203 /* The IPv4 code below accesses through the IPv4 header
204 * checksum, which is 12 bytes into the packet.
205 * The IPv6 code below accesses through the IPv6 hop limit
206 * which is 8 bytes into the packet.
207 *
208 * For all supported cases there should always be at least 12
209 * bytes of packet data present. The IPv4 header is 20 bytes
210 * without options and the IPv6 header is always 40 bytes
211 * long.
212 */
213 if (!pskb_may_pull(skb, 12))
214 return false;
215
Robert Shearman118d5232015-08-06 11:04:56 +0100216 payload_type = rt->rt_payload_type;
217 if (payload_type == MPT_UNSPEC)
218 payload_type = ip_hdr(skb)->version;
219
220 switch (payload_type) {
221 case MPT_IPV4: {
222 struct iphdr *hdr4 = ip_hdr(skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600223 skb->protocol = htons(ETH_P_IP);
224 csum_replace2(&hdr4->check,
225 htons(hdr4->ttl << 8),
226 htons(dec.ttl << 8));
227 hdr4->ttl = dec.ttl;
Robert Shearman118d5232015-08-06 11:04:56 +0100228 success = true;
229 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600230 }
Robert Shearman118d5232015-08-06 11:04:56 +0100231 case MPT_IPV6: {
Eric W. Biederman01891972015-03-03 19:10:47 -0600232 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
233 skb->protocol = htons(ETH_P_IPV6);
234 hdr6->hop_limit = dec.ttl;
Robert Shearman118d5232015-08-06 11:04:56 +0100235 success = true;
236 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600237 }
Robert Shearman118d5232015-08-06 11:04:56 +0100238 case MPT_UNSPEC:
239 break;
240 }
241
Eric W. Biederman01891972015-03-03 19:10:47 -0600242 return success;
243}
244
245static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
246 struct packet_type *pt, struct net_device *orig_dev)
247{
248 struct net *net = dev_net(dev);
249 struct mpls_shim_hdr *hdr;
250 struct mpls_route *rt;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700251 struct mpls_nh *nh;
Eric W. Biederman01891972015-03-03 19:10:47 -0600252 struct mpls_entry_decoded dec;
253 struct net_device *out_dev;
Robert Shearman03c57742015-04-22 11:14:37 +0100254 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600255 unsigned int hh_len;
256 unsigned int new_header_size;
257 unsigned int mtu;
258 int err;
259
260 /* Careful this entire function runs inside of an rcu critical section */
261
Robert Shearman03c57742015-04-22 11:14:37 +0100262 mdev = mpls_dev_get(dev);
Robert Shearman37bde792015-04-22 11:14:38 +0100263 if (!mdev || !mdev->input_enabled)
Robert Shearman03c57742015-04-22 11:14:37 +0100264 goto drop;
265
Eric W. Biederman01891972015-03-03 19:10:47 -0600266 if (skb->pkt_type != PACKET_HOST)
267 goto drop;
268
269 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
270 goto drop;
271
272 if (!pskb_may_pull(skb, sizeof(*hdr)))
273 goto drop;
274
275 /* Read and decode the label */
276 hdr = mpls_hdr(skb);
277 dec = mpls_entry_decode(hdr);
278
279 /* Pop the label */
280 skb_pull(skb, sizeof(*hdr));
281 skb_reset_network_header(skb);
282
283 skb_orphan(skb);
284
285 rt = mpls_route_input_rcu(net, dec.label);
286 if (!rt)
287 goto drop;
288
Robert Shearman1c78efa2015-10-23 06:03:28 -0700289 nh = mpls_select_multipath(rt, skb, dec.bos);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700290 if (!nh)
291 goto drop;
292
Eric W. Biederman01891972015-03-03 19:10:47 -0600293 /* Find the output device */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700294 out_dev = rcu_dereference(nh->nh_dev);
Eric W. Biederman01891972015-03-03 19:10:47 -0600295 if (!mpls_output_possible(out_dev))
296 goto drop;
297
298 if (skb_warn_if_lro(skb))
299 goto drop;
300
301 skb_forward_csum(skb);
302
303 /* Verify ttl is valid */
Eric W. Biedermanaa7da932015-03-07 16:23:23 -0600304 if (dec.ttl <= 1)
Eric W. Biederman01891972015-03-03 19:10:47 -0600305 goto drop;
306 dec.ttl -= 1;
307
308 /* Verify the destination can hold the packet */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700309 new_header_size = mpls_nh_header_size(nh);
Eric W. Biederman01891972015-03-03 19:10:47 -0600310 mtu = mpls_dev_mtu(out_dev);
311 if (mpls_pkt_too_big(skb, mtu - new_header_size))
312 goto drop;
313
314 hh_len = LL_RESERVED_SPACE(out_dev);
315 if (!out_dev->header_ops)
316 hh_len = 0;
317
318 /* Ensure there is enough space for the headers in the skb */
319 if (skb_cow(skb, hh_len + new_header_size))
320 goto drop;
321
322 skb->dev = out_dev;
323 skb->protocol = htons(ETH_P_MPLS_UC);
324
325 if (unlikely(!new_header_size && dec.bos)) {
326 /* Penultimate hop popping */
327 if (!mpls_egress(rt, skb, dec))
328 goto drop;
329 } else {
330 bool bos;
331 int i;
332 skb_push(skb, new_header_size);
333 skb_reset_network_header(skb);
334 /* Push the new labels */
335 hdr = mpls_hdr(skb);
336 bos = dec.bos;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700337 for (i = nh->nh_labels - 1; i >= 0; i--) {
338 hdr[i] = mpls_entry_encode(nh->nh_label[i],
339 dec.ttl, 0, bos);
Eric W. Biederman01891972015-03-03 19:10:47 -0600340 bos = false;
341 }
342 }
343
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000344 err = neigh_xmit(nh->nh_via_table, out_dev, mpls_nh_via(rt, nh), skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600345 if (err)
346 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
347 __func__, err);
348 return 0;
349
350drop:
351 kfree_skb(skb);
352 return NET_RX_DROP;
353}
354
355static struct packet_type mpls_packet_type __read_mostly = {
356 .type = cpu_to_be16(ETH_P_MPLS_UC),
357 .func = mpls_forward,
358};
359
Wu Fengguangf0126532015-03-05 05:33:54 +0800360static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600361 [RTA_DST] = { .type = NLA_U32 },
362 [RTA_OIF] = { .type = NLA_U32 },
363};
364
Eric W. Biedermana2519922015-03-03 19:12:40 -0600365struct mpls_route_config {
Robert Shearman118d5232015-08-06 11:04:56 +0100366 u32 rc_protocol;
367 u32 rc_ifindex;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700368 u8 rc_via_table;
369 u8 rc_via_alen;
Robert Shearman118d5232015-08-06 11:04:56 +0100370 u8 rc_via[MAX_VIA_ALEN];
371 u32 rc_label;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700372 u8 rc_output_labels;
Robert Shearman118d5232015-08-06 11:04:56 +0100373 u32 rc_output_label[MAX_NEW_LABELS];
374 u32 rc_nlflags;
375 enum mpls_payload_type rc_payload_type;
376 struct nl_info rc_nlinfo;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700377 struct rtnexthop *rc_mp;
378 int rc_mp_len;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600379};
380
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000381static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
Eric W. Biederman01891972015-03-03 19:10:47 -0600382{
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000383 u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
Eric W. Biederman01891972015-03-03 19:10:47 -0600384 struct mpls_route *rt;
385
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000386 rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
387 VIA_ALEN_ALIGN) +
388 num_nh * max_alen_aligned,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700389 GFP_KERNEL);
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000390 if (rt) {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700391 rt->rt_nhn = num_nh;
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800392 rt->rt_nhn_alive = num_nh;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000393 rt->rt_max_alen = max_alen_aligned;
394 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700395
Eric W. Biederman01891972015-03-03 19:10:47 -0600396 return rt;
397}
398
399static void mpls_rt_free(struct mpls_route *rt)
400{
401 if (rt)
402 kfree_rcu(rt, rt_rcu);
403}
404
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600405static void mpls_notify_route(struct net *net, unsigned index,
406 struct mpls_route *old, struct mpls_route *new,
407 const struct nl_info *info)
408{
409 struct nlmsghdr *nlh = info ? info->nlh : NULL;
410 unsigned portid = info ? info->portid : 0;
411 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
412 struct mpls_route *rt = new ? new : old;
413 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
414 /* Ignore reserved labels for now */
Robert Shearmana6affd22015-08-03 17:50:04 +0100415 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600416 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
417}
418
Eric W. Biederman01891972015-03-03 19:10:47 -0600419static void mpls_route_update(struct net *net, unsigned index,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700420 struct mpls_route *new,
Eric W. Biederman01891972015-03-03 19:10:47 -0600421 const struct nl_info *info)
422{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600423 struct mpls_route __rcu **platform_label;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700424 struct mpls_route *rt;
Eric W. Biederman01891972015-03-03 19:10:47 -0600425
426 ASSERT_RTNL();
427
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600428 platform_label = rtnl_dereference(net->mpls.platform_label);
429 rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700430 rcu_assign_pointer(platform_label[index], new);
Eric W. Biederman01891972015-03-03 19:10:47 -0600431
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700432 mpls_notify_route(net, index, rt, new, info);
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600433
Eric W. Biederman01891972015-03-03 19:10:47 -0600434 /* If we removed a route free it now */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700435 mpls_rt_free(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -0600436}
437
Eric W. Biedermana2519922015-03-03 19:12:40 -0600438static unsigned find_free_label(struct net *net)
439{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600440 struct mpls_route __rcu **platform_label;
441 size_t platform_labels;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600442 unsigned index;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600443
444 platform_label = rtnl_dereference(net->mpls.platform_label);
445 platform_labels = net->mpls.platform_labels;
Robert Shearmana6affd22015-08-03 17:50:04 +0100446 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
447 index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600448 if (!rtnl_dereference(platform_label[index]))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600449 return index;
450 }
451 return LABEL_NOT_SPECIFIED;
452}
453
Roopa Prabhubf215632015-07-30 13:34:54 -0700454#if IS_ENABLED(CONFIG_INET)
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000455static struct net_device *inet_fib_lookup_dev(struct net *net,
456 const void *addr)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700457{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300458 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700459 struct rtable *rt;
460 struct in_addr daddr;
461
462 memcpy(&daddr, addr, sizeof(struct in_addr));
463 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
464 if (IS_ERR(rt))
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300465 return ERR_CAST(rt);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700466
467 dev = rt->dst.dev;
468 dev_hold(dev);
469
470 ip_rt_put(rt);
471
Roopa Prabhu01faef22015-07-21 09:16:24 -0700472 return dev;
473}
Roopa Prabhubf215632015-07-30 13:34:54 -0700474#else
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000475static struct net_device *inet_fib_lookup_dev(struct net *net,
476 const void *addr)
Roopa Prabhubf215632015-07-30 13:34:54 -0700477{
478 return ERR_PTR(-EAFNOSUPPORT);
479}
480#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700481
Roopa Prabhubf215632015-07-30 13:34:54 -0700482#if IS_ENABLED(CONFIG_IPV6)
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000483static struct net_device *inet6_fib_lookup_dev(struct net *net,
484 const void *addr)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700485{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300486 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700487 struct dst_entry *dst;
488 struct flowi6 fl6;
Roopa Prabhubf215632015-07-30 13:34:54 -0700489 int err;
490
491 if (!ipv6_stub)
492 return ERR_PTR(-EAFNOSUPPORT);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700493
494 memset(&fl6, 0, sizeof(fl6));
495 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
Roopa Prabhubf215632015-07-30 13:34:54 -0700496 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
497 if (err)
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300498 return ERR_PTR(err);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700499
500 dev = dst->dev;
501 dev_hold(dev);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700502 dst_release(dst);
503
504 return dev;
505}
Roopa Prabhubf215632015-07-30 13:34:54 -0700506#else
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000507static struct net_device *inet6_fib_lookup_dev(struct net *net,
508 const void *addr)
Roopa Prabhubf215632015-07-30 13:34:54 -0700509{
510 return ERR_PTR(-EAFNOSUPPORT);
511}
512#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700513
514static struct net_device *find_outdev(struct net *net,
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000515 struct mpls_route *rt,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700516 struct mpls_nh *nh, int oif)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700517{
518 struct net_device *dev = NULL;
519
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700520 if (!oif) {
521 switch (nh->nh_via_table) {
Roopa Prabhu01faef22015-07-21 09:16:24 -0700522 case NEIGH_ARP_TABLE:
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000523 dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
Roopa Prabhu01faef22015-07-21 09:16:24 -0700524 break;
525 case NEIGH_ND_TABLE:
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000526 dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
Roopa Prabhu01faef22015-07-21 09:16:24 -0700527 break;
528 case NEIGH_LINK_TABLE:
529 break;
530 }
531 } else {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700532 dev = dev_get_by_index(net, oif);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700533 }
534
Roopa Prabhu3dcb6152015-08-04 06:36:24 -0700535 if (!dev)
536 return ERR_PTR(-ENODEV);
537
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700538 /* The caller is holding rtnl anyways, so release the dev reference */
539 dev_put(dev);
540
Roopa Prabhu01faef22015-07-21 09:16:24 -0700541 return dev;
542}
543
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000544static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
545 struct mpls_nh *nh, int oif)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700546{
547 struct net_device *dev = NULL;
548 int err = -ENODEV;
549
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000550 dev = find_outdev(net, rt, nh, oif);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700551 if (IS_ERR(dev)) {
552 err = PTR_ERR(dev);
553 dev = NULL;
554 goto errout;
555 }
556
557 /* Ensure this is a supported device */
558 err = -EINVAL;
559 if (!mpls_dev_get(dev))
560 goto errout;
561
562 RCU_INIT_POINTER(nh->nh_dev, dev);
563
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800564 if (!(dev->flags & IFF_UP)) {
565 nh->nh_flags |= RTNH_F_DEAD;
566 } else {
567 unsigned int flags;
568
569 flags = dev_get_flags(dev);
570 if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
571 nh->nh_flags |= RTNH_F_LINKDOWN;
572 }
573
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700574 return 0;
575
576errout:
577 return err;
578}
579
580static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
581 struct mpls_route *rt)
582{
583 struct net *net = cfg->rc_nlinfo.nl_net;
584 struct mpls_nh *nh = rt->rt_nh;
585 int err;
586 int i;
587
588 if (!nh)
589 return -ENOMEM;
590
591 err = -EINVAL;
592 /* Ensure only a supported number of labels are present */
593 if (cfg->rc_output_labels > MAX_NEW_LABELS)
594 goto errout;
595
596 nh->nh_labels = cfg->rc_output_labels;
597 for (i = 0; i < nh->nh_labels; i++)
598 nh->nh_label[i] = cfg->rc_output_label[i];
599
600 nh->nh_via_table = cfg->rc_via_table;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000601 memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700602 nh->nh_via_alen = cfg->rc_via_alen;
603
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000604 err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700605 if (err)
606 goto errout;
607
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800608 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
609 rt->rt_nhn_alive--;
610
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700611 return 0;
612
613errout:
614 return err;
615}
616
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000617static int mpls_nh_build(struct net *net, struct mpls_route *rt,
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800618 struct mpls_nh *nh, int oif, struct nlattr *via,
619 struct nlattr *newdst)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700620{
621 int err = -ENOMEM;
622
623 if (!nh)
624 goto errout;
625
626 if (newdst) {
627 err = nla_get_labels(newdst, MAX_NEW_LABELS,
628 &nh->nh_labels, nh->nh_label);
629 if (err)
630 goto errout;
631 }
632
633 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000634 __mpls_nh_via(rt, nh));
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700635 if (err)
636 goto errout;
637
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000638 err = mpls_nh_assign_dev(net, rt, nh, oif);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700639 if (err)
640 goto errout;
641
642 return 0;
643
644errout:
645 return err;
646}
647
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000648static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
649 u8 cfg_via_alen, u8 *max_via_alen)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700650{
651 int nhs = 0;
652 int remaining = len;
653
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000654 if (!rtnh) {
655 *max_via_alen = cfg_via_alen;
656 return 1;
657 }
658
659 *max_via_alen = 0;
660
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700661 while (rtnh_ok(rtnh, remaining)) {
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000662 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
663 int attrlen;
664
665 attrlen = rtnh_attrlen(rtnh);
666 nla = nla_find(attrs, attrlen, RTA_VIA);
667 if (nla && nla_len(nla) >=
668 offsetof(struct rtvia, rtvia_addr)) {
669 int via_alen = nla_len(nla) -
670 offsetof(struct rtvia, rtvia_addr);
671
672 if (via_alen <= MAX_VIA_ALEN)
673 *max_via_alen = max_t(u16, *max_via_alen,
674 via_alen);
675 }
676
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700677 nhs++;
678 rtnh = rtnh_next(rtnh, &remaining);
679 }
680
681 /* leftover implies invalid nexthop configuration, discard it */
682 return remaining > 0 ? 0 : nhs;
683}
684
685static int mpls_nh_build_multi(struct mpls_route_config *cfg,
686 struct mpls_route *rt)
687{
688 struct rtnexthop *rtnh = cfg->rc_mp;
689 struct nlattr *nla_via, *nla_newdst;
690 int remaining = cfg->rc_mp_len;
691 int nhs = 0;
692 int err = 0;
693
694 change_nexthops(rt) {
695 int attrlen;
696
697 nla_via = NULL;
698 nla_newdst = NULL;
699
700 err = -EINVAL;
701 if (!rtnh_ok(rtnh, remaining))
702 goto errout;
703
Robert Shearman1c78efa2015-10-23 06:03:28 -0700704 /* neither weighted multipath nor any flags
705 * are supported
706 */
707 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
708 goto errout;
709
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700710 attrlen = rtnh_attrlen(rtnh);
711 if (attrlen > 0) {
712 struct nlattr *attrs = rtnh_attrs(rtnh);
713
714 nla_via = nla_find(attrs, attrlen, RTA_VIA);
715 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
716 }
717
718 if (!nla_via)
719 goto errout;
720
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000721 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800722 rtnh->rtnh_ifindex, nla_via, nla_newdst);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700723 if (err)
724 goto errout;
725
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800726 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
727 rt->rt_nhn_alive--;
728
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700729 rtnh = rtnh_next(rtnh, &remaining);
730 nhs++;
731 } endfor_nexthops(rt);
732
733 rt->rt_nhn = nhs;
734
735 return 0;
736
737errout:
738 return err;
739}
740
Eric W. Biedermana2519922015-03-03 19:12:40 -0600741static int mpls_route_add(struct mpls_route_config *cfg)
742{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600743 struct mpls_route __rcu **platform_label;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600744 struct net *net = cfg->rc_nlinfo.nl_net;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600745 struct mpls_route *rt, *old;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600746 int err = -EINVAL;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000747 u8 max_via_alen;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700748 unsigned index;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000749 int nhs;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600750
751 index = cfg->rc_label;
752
753 /* If a label was not specified during insert pick one */
754 if ((index == LABEL_NOT_SPECIFIED) &&
755 (cfg->rc_nlflags & NLM_F_CREATE)) {
756 index = find_free_label(net);
757 }
758
Robert Shearmana6affd22015-08-03 17:50:04 +0100759 /* Reserved labels may not be set */
760 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600761 goto errout;
762
763 /* The full 20 bit range may not be supported. */
764 if (index >= net->mpls.platform_labels)
765 goto errout;
766
Eric W. Biedermana2519922015-03-03 19:12:40 -0600767 /* Append makes no sense with mpls */
Eric W. Biederman0f7bbd52015-03-07 16:22:40 -0600768 err = -EOPNOTSUPP;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600769 if (cfg->rc_nlflags & NLM_F_APPEND)
770 goto errout;
771
772 err = -EEXIST;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600773 platform_label = rtnl_dereference(net->mpls.platform_label);
774 old = rtnl_dereference(platform_label[index]);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600775 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
776 goto errout;
777
778 err = -EEXIST;
779 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
780 goto errout;
781
782 err = -ENOENT;
783 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
784 goto errout;
785
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000786 err = -EINVAL;
787 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
788 cfg->rc_via_alen, &max_via_alen);
789 if (nhs == 0)
790 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700791
Eric W. Biedermana2519922015-03-03 19:12:40 -0600792 err = -ENOMEM;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000793 rt = mpls_rt_alloc(nhs, max_via_alen);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600794 if (!rt)
795 goto errout;
796
Eric W. Biedermana2519922015-03-03 19:12:40 -0600797 rt->rt_protocol = cfg->rc_protocol;
Robert Shearman118d5232015-08-06 11:04:56 +0100798 rt->rt_payload_type = cfg->rc_payload_type;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600799
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700800 if (cfg->rc_mp)
801 err = mpls_nh_build_multi(cfg, rt);
802 else
803 err = mpls_nh_build_from_cfg(cfg, rt);
804 if (err)
805 goto freert;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600806
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700807 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
808
Eric W. Biedermana2519922015-03-03 19:12:40 -0600809 return 0;
810
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700811freert:
812 mpls_rt_free(rt);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600813errout:
Eric W. Biedermana2519922015-03-03 19:12:40 -0600814 return err;
815}
816
817static int mpls_route_del(struct mpls_route_config *cfg)
818{
819 struct net *net = cfg->rc_nlinfo.nl_net;
820 unsigned index;
821 int err = -EINVAL;
822
823 index = cfg->rc_label;
824
Robert Shearmana6affd22015-08-03 17:50:04 +0100825 /* Reserved labels may not be removed */
826 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600827 goto errout;
828
829 /* The full 20 bit range may not be supported */
830 if (index >= net->mpls.platform_labels)
831 goto errout;
832
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700833 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600834
835 err = 0;
836errout:
837 return err;
838}
839
Robert Shearman37bde792015-04-22 11:14:38 +0100840#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
841 (&((struct mpls_dev *)0)->field)
842
843static const struct ctl_table mpls_dev_table[] = {
844 {
845 .procname = "input",
846 .maxlen = sizeof(int),
847 .mode = 0644,
848 .proc_handler = proc_dointvec,
849 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
850 },
851 { }
852};
853
854static int mpls_dev_sysctl_register(struct net_device *dev,
855 struct mpls_dev *mdev)
856{
857 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
858 struct ctl_table *table;
859 int i;
860
861 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
862 if (!table)
863 goto out;
864
865 /* Table data contains only offsets relative to the base of
866 * the mdev at this point, so make them absolute.
867 */
868 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
869 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
870
871 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
872
873 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
874 if (!mdev->sysctl)
875 goto free;
876
877 return 0;
878
879free:
880 kfree(table);
881out:
882 return -ENOBUFS;
883}
884
885static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
886{
887 struct ctl_table *table;
888
889 table = mdev->sysctl->ctl_table_arg;
890 unregister_net_sysctl_table(mdev->sysctl);
891 kfree(table);
892}
893
Robert Shearman03c57742015-04-22 11:14:37 +0100894static struct mpls_dev *mpls_add_dev(struct net_device *dev)
895{
896 struct mpls_dev *mdev;
897 int err = -ENOMEM;
898
899 ASSERT_RTNL();
900
901 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
902 if (!mdev)
903 return ERR_PTR(err);
904
Robert Shearman37bde792015-04-22 11:14:38 +0100905 err = mpls_dev_sysctl_register(dev, mdev);
906 if (err)
907 goto free;
908
Robert Shearman03c57742015-04-22 11:14:37 +0100909 rcu_assign_pointer(dev->mpls_ptr, mdev);
910
911 return mdev;
Robert Shearman37bde792015-04-22 11:14:38 +0100912
913free:
914 kfree(mdev);
915 return ERR_PTR(err);
Robert Shearman03c57742015-04-22 11:14:37 +0100916}
917
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800918static void mpls_ifdown(struct net_device *dev, int event)
Eric W. Biederman01891972015-03-03 19:10:47 -0600919{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600920 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -0600921 struct net *net = dev_net(dev);
922 unsigned index;
923
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600924 platform_label = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -0600925 for (index = 0; index < net->mpls.platform_labels; index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600926 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800927
Eric W. Biederman01891972015-03-03 19:10:47 -0600928 if (!rt)
929 continue;
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800930
931 change_nexthops(rt) {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700932 if (rtnl_dereference(nh->nh_dev) != dev)
933 continue;
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800934 switch (event) {
935 case NETDEV_DOWN:
936 case NETDEV_UNREGISTER:
937 nh->nh_flags |= RTNH_F_DEAD;
938 /* fall through */
939 case NETDEV_CHANGE:
940 nh->nh_flags |= RTNH_F_LINKDOWN;
941 ACCESS_ONCE(rt->rt_nhn_alive) = rt->rt_nhn_alive - 1;
942 break;
943 }
944 if (event == NETDEV_UNREGISTER)
945 RCU_INIT_POINTER(nh->nh_dev, NULL);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700946 } endfor_nexthops(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -0600947 }
Robert Shearman03c57742015-04-22 11:14:37 +0100948
Robert Shearman03c57742015-04-22 11:14:37 +0100949
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800950 return;
951}
Robert Shearman37bde792015-04-22 11:14:38 +0100952
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800953static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
954{
955 struct mpls_route __rcu **platform_label;
956 struct net *net = dev_net(dev);
957 unsigned index;
958 int alive;
Robert Shearman03c57742015-04-22 11:14:37 +0100959
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800960 platform_label = rtnl_dereference(net->mpls.platform_label);
961 for (index = 0; index < net->mpls.platform_labels; index++) {
962 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
963
964 if (!rt)
965 continue;
966
967 alive = 0;
968 change_nexthops(rt) {
969 struct net_device *nh_dev =
970 rtnl_dereference(nh->nh_dev);
971
972 if (!(nh->nh_flags & nh_flags)) {
973 alive++;
974 continue;
975 }
976 if (nh_dev != dev)
977 continue;
978 alive++;
979 nh->nh_flags &= ~nh_flags;
980 } endfor_nexthops(rt);
981
982 ACCESS_ONCE(rt->rt_nhn_alive) = alive;
983 }
984
985 return;
Eric W. Biederman01891972015-03-03 19:10:47 -0600986}
987
988static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
989 void *ptr)
990{
991 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Robert Shearman03c57742015-04-22 11:14:37 +0100992 struct mpls_dev *mdev;
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800993 unsigned int flags;
Eric W. Biederman01891972015-03-03 19:10:47 -0600994
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800995 if (event == NETDEV_REGISTER) {
Robert Shearman03c57742015-04-22 11:14:37 +0100996 /* For now just support ethernet devices */
997 if ((dev->type == ARPHRD_ETHER) ||
998 (dev->type == ARPHRD_LOOPBACK)) {
999 mdev = mpls_add_dev(dev);
1000 if (IS_ERR(mdev))
1001 return notifier_from_errno(PTR_ERR(mdev));
1002 }
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001003 return NOTIFY_OK;
1004 }
Robert Shearman03c57742015-04-22 11:14:37 +01001005
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001006 mdev = mpls_dev_get(dev);
1007 if (!mdev)
1008 return NOTIFY_OK;
1009
1010 switch (event) {
1011 case NETDEV_DOWN:
1012 mpls_ifdown(dev, event);
1013 break;
1014 case NETDEV_UP:
1015 flags = dev_get_flags(dev);
1016 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1017 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1018 else
1019 mpls_ifup(dev, RTNH_F_DEAD);
1020 break;
1021 case NETDEV_CHANGE:
1022 flags = dev_get_flags(dev);
1023 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1024 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1025 else
1026 mpls_ifdown(dev, event);
1027 break;
Eric W. Biederman01891972015-03-03 19:10:47 -06001028 case NETDEV_UNREGISTER:
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001029 mpls_ifdown(dev, event);
1030 mdev = mpls_dev_get(dev);
1031 if (mdev) {
1032 mpls_dev_sysctl_unregister(mdev);
1033 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
1034 kfree_rcu(mdev, rcu);
1035 }
Eric W. Biederman01891972015-03-03 19:10:47 -06001036 break;
Robert Shearman0fae3bf2015-06-11 19:58:26 +01001037 case NETDEV_CHANGENAME:
1038 mdev = mpls_dev_get(dev);
1039 if (mdev) {
1040 int err;
1041
1042 mpls_dev_sysctl_unregister(mdev);
1043 err = mpls_dev_sysctl_register(dev, mdev);
1044 if (err)
1045 return notifier_from_errno(err);
1046 }
1047 break;
Eric W. Biederman01891972015-03-03 19:10:47 -06001048 }
1049 return NOTIFY_OK;
1050}
1051
1052static struct notifier_block mpls_dev_notifier = {
1053 .notifier_call = mpls_dev_notify,
1054};
1055
Eric W. Biederman03c05662015-03-03 19:13:56 -06001056static int nla_put_via(struct sk_buff *skb,
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001057 u8 table, const void *addr, int alen)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001058{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001059 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
1060 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
1061 };
Eric W. Biederman03c05662015-03-03 19:13:56 -06001062 struct nlattr *nla;
1063 struct rtvia *via;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001064 int family = AF_UNSPEC;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001065
1066 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1067 if (!nla)
1068 return -EMSGSIZE;
1069
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001070 if (table <= NEIGH_NR_TABLES)
1071 family = table_to_family[table];
1072
Eric W. Biederman03c05662015-03-03 19:13:56 -06001073 via = nla_data(nla);
1074 via->rtvia_family = family;
1075 memcpy(via->rtvia_addr, addr, alen);
1076 return 0;
1077}
1078
Eric W. Biederman966bae32015-03-03 19:13:19 -06001079int nla_put_labels(struct sk_buff *skb, int attrtype,
1080 u8 labels, const u32 label[])
1081{
1082 struct nlattr *nla;
1083 struct mpls_shim_hdr *nla_label;
1084 bool bos;
1085 int i;
1086 nla = nla_reserve(skb, attrtype, labels*4);
1087 if (!nla)
1088 return -EMSGSIZE;
1089
1090 nla_label = nla_data(nla);
1091 bos = true;
1092 for (i = labels - 1; i >= 0; i--) {
1093 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1094 bos = false;
1095 }
1096
1097 return 0;
1098}
Roopa Prabhuface0182015-07-21 10:43:52 +02001099EXPORT_SYMBOL_GPL(nla_put_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001100
1101int nla_get_labels(const struct nlattr *nla,
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001102 u32 max_labels, u8 *labels, u32 label[])
Eric W. Biederman966bae32015-03-03 19:13:19 -06001103{
1104 unsigned len = nla_len(nla);
1105 unsigned nla_labels;
1106 struct mpls_shim_hdr *nla_label;
1107 bool bos;
1108 int i;
1109
1110 /* len needs to be an even multiple of 4 (the label size) */
1111 if (len & 3)
1112 return -EINVAL;
1113
1114 /* Limit the number of new labels allowed */
1115 nla_labels = len/4;
1116 if (nla_labels > max_labels)
1117 return -EINVAL;
1118
1119 nla_label = nla_data(nla);
1120 bos = true;
1121 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1122 struct mpls_entry_decoded dec;
1123 dec = mpls_entry_decode(nla_label + i);
1124
1125 /* Ensure the bottom of stack flag is properly set
1126 * and ttl and tc are both clear.
1127 */
1128 if ((dec.bos != bos) || dec.ttl || dec.tc)
1129 return -EINVAL;
1130
Robert Shearman5a9ab012015-04-22 11:14:39 +01001131 switch (dec.label) {
Tom Herbert78f5b892015-05-07 08:08:51 -07001132 case MPLS_LABEL_IMPLNULL:
Robert Shearman5a9ab012015-04-22 11:14:39 +01001133 /* RFC3032: This is a label that an LSR may
1134 * assign and distribute, but which never
1135 * actually appears in the encapsulation.
1136 */
1137 return -EINVAL;
1138 }
1139
Eric W. Biederman966bae32015-03-03 19:13:19 -06001140 label[i] = dec.label;
1141 }
1142 *labels = nla_labels;
1143 return 0;
1144}
Roopa Prabhuface0182015-07-21 10:43:52 +02001145EXPORT_SYMBOL_GPL(nla_get_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001146
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001147int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1148 u8 *via_table, u8 via_addr[])
1149{
1150 struct rtvia *via = nla_data(nla);
1151 int err = -EINVAL;
1152 int alen;
1153
1154 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1155 goto errout;
1156 alen = nla_len(nla) -
1157 offsetof(struct rtvia, rtvia_addr);
1158 if (alen > MAX_VIA_ALEN)
1159 goto errout;
1160
1161 /* Validate the address family */
1162 switch (via->rtvia_family) {
1163 case AF_PACKET:
1164 *via_table = NEIGH_LINK_TABLE;
1165 break;
1166 case AF_INET:
1167 *via_table = NEIGH_ARP_TABLE;
1168 if (alen != 4)
1169 goto errout;
1170 break;
1171 case AF_INET6:
1172 *via_table = NEIGH_ND_TABLE;
1173 if (alen != 16)
1174 goto errout;
1175 break;
1176 default:
1177 /* Unsupported address family */
1178 goto errout;
1179 }
1180
1181 memcpy(via_addr, via->rtvia_addr, alen);
1182 *via_alen = alen;
1183 err = 0;
1184
1185errout:
1186 return err;
1187}
1188
Eric W. Biederman03c05662015-03-03 19:13:56 -06001189static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1190 struct mpls_route_config *cfg)
1191{
1192 struct rtmsg *rtm;
1193 struct nlattr *tb[RTA_MAX+1];
1194 int index;
1195 int err;
1196
1197 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1198 if (err < 0)
1199 goto errout;
1200
1201 err = -EINVAL;
1202 rtm = nlmsg_data(nlh);
1203 memset(cfg, 0, sizeof(*cfg));
1204
1205 if (rtm->rtm_family != AF_MPLS)
1206 goto errout;
1207 if (rtm->rtm_dst_len != 20)
1208 goto errout;
1209 if (rtm->rtm_src_len != 0)
1210 goto errout;
1211 if (rtm->rtm_tos != 0)
1212 goto errout;
1213 if (rtm->rtm_table != RT_TABLE_MAIN)
1214 goto errout;
1215 /* Any value is acceptable for rtm_protocol */
1216
1217 /* As mpls uses destination specific addresses
1218 * (or source specific address in the case of multicast)
1219 * all addresses have universal scope.
1220 */
1221 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1222 goto errout;
1223 if (rtm->rtm_type != RTN_UNICAST)
1224 goto errout;
1225 if (rtm->rtm_flags != 0)
1226 goto errout;
1227
1228 cfg->rc_label = LABEL_NOT_SPECIFIED;
1229 cfg->rc_protocol = rtm->rtm_protocol;
1230 cfg->rc_nlflags = nlh->nlmsg_flags;
1231 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1232 cfg->rc_nlinfo.nlh = nlh;
1233 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1234
1235 for (index = 0; index <= RTA_MAX; index++) {
1236 struct nlattr *nla = tb[index];
1237 if (!nla)
1238 continue;
1239
1240 switch(index) {
1241 case RTA_OIF:
1242 cfg->rc_ifindex = nla_get_u32(nla);
1243 break;
1244 case RTA_NEWDST:
1245 if (nla_get_labels(nla, MAX_NEW_LABELS,
1246 &cfg->rc_output_labels,
1247 cfg->rc_output_label))
1248 goto errout;
1249 break;
1250 case RTA_DST:
1251 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001252 u8 label_count;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001253 if (nla_get_labels(nla, 1, &label_count,
1254 &cfg->rc_label))
1255 goto errout;
1256
Robert Shearmana6affd22015-08-03 17:50:04 +01001257 /* Reserved labels may not be set */
1258 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001259 goto errout;
1260
1261 break;
1262 }
1263 case RTA_VIA:
1264 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001265 if (nla_get_via(nla, &cfg->rc_via_alen,
1266 &cfg->rc_via_table, cfg->rc_via))
Robert Shearmanf8d54af2015-03-06 10:47:00 +00001267 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001268 break;
1269 }
1270 case RTA_MULTIPATH:
1271 {
1272 cfg->rc_mp = nla_data(nla);
1273 cfg->rc_mp_len = nla_len(nla);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001274 break;
1275 }
1276 default:
1277 /* Unsupported attribute */
1278 goto errout;
1279 }
1280 }
1281
1282 err = 0;
1283errout:
1284 return err;
1285}
1286
1287static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1288{
1289 struct mpls_route_config cfg;
1290 int err;
1291
1292 err = rtm_to_route_config(skb, nlh, &cfg);
1293 if (err < 0)
1294 return err;
1295
1296 return mpls_route_del(&cfg);
1297}
1298
1299
1300static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1301{
1302 struct mpls_route_config cfg;
1303 int err;
1304
1305 err = rtm_to_route_config(skb, nlh, &cfg);
1306 if (err < 0)
1307 return err;
1308
1309 return mpls_route_add(&cfg);
1310}
1311
1312static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1313 u32 label, struct mpls_route *rt, int flags)
1314{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001315 struct net_device *dev;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001316 struct nlmsghdr *nlh;
1317 struct rtmsg *rtm;
1318
1319 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1320 if (nlh == NULL)
1321 return -EMSGSIZE;
1322
1323 rtm = nlmsg_data(nlh);
1324 rtm->rtm_family = AF_MPLS;
1325 rtm->rtm_dst_len = 20;
1326 rtm->rtm_src_len = 0;
1327 rtm->rtm_tos = 0;
1328 rtm->rtm_table = RT_TABLE_MAIN;
1329 rtm->rtm_protocol = rt->rt_protocol;
1330 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1331 rtm->rtm_type = RTN_UNICAST;
1332 rtm->rtm_flags = 0;
1333
Eric W. Biederman03c05662015-03-03 19:13:56 -06001334 if (nla_put_labels(skb, RTA_DST, 1, &label))
1335 goto nla_put_failure;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001336 if (rt->rt_nhn == 1) {
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001337 const struct mpls_nh *nh = rt->rt_nh;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001338
1339 if (nh->nh_labels &&
1340 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1341 nh->nh_label))
1342 goto nla_put_failure;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001343 if (nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001344 nh->nh_via_alen))
1345 goto nla_put_failure;
1346 dev = rtnl_dereference(nh->nh_dev);
1347 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1348 goto nla_put_failure;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001349 if (nh->nh_flags & RTNH_F_LINKDOWN)
1350 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1351 if (nh->nh_flags & RTNH_F_DEAD)
1352 rtm->rtm_flags |= RTNH_F_DEAD;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001353 } else {
1354 struct rtnexthop *rtnh;
1355 struct nlattr *mp;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001356 int dead = 0;
1357 int linkdown = 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001358
1359 mp = nla_nest_start(skb, RTA_MULTIPATH);
1360 if (!mp)
1361 goto nla_put_failure;
1362
1363 for_nexthops(rt) {
1364 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1365 if (!rtnh)
1366 goto nla_put_failure;
1367
1368 dev = rtnl_dereference(nh->nh_dev);
1369 if (dev)
1370 rtnh->rtnh_ifindex = dev->ifindex;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001371 if (nh->nh_flags & RTNH_F_LINKDOWN) {
1372 rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
1373 linkdown++;
1374 }
1375 if (nh->nh_flags & RTNH_F_DEAD) {
1376 rtnh->rtnh_flags |= RTNH_F_DEAD;
1377 dead++;
1378 }
1379
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001380 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1381 nh->nh_labels,
1382 nh->nh_label))
1383 goto nla_put_failure;
1384 if (nla_put_via(skb, nh->nh_via_table,
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001385 mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001386 nh->nh_via_alen))
1387 goto nla_put_failure;
1388
1389 /* length of rtnetlink header + attributes */
1390 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1391 } endfor_nexthops(rt);
1392
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001393 if (linkdown == rt->rt_nhn)
1394 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1395 if (dead == rt->rt_nhn)
1396 rtm->rtm_flags |= RTNH_F_DEAD;
1397
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001398 nla_nest_end(skb, mp);
1399 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001400
1401 nlmsg_end(skb, nlh);
1402 return 0;
1403
1404nla_put_failure:
1405 nlmsg_cancel(skb, nlh);
1406 return -EMSGSIZE;
1407}
1408
1409static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1410{
1411 struct net *net = sock_net(skb->sk);
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001412 struct mpls_route __rcu **platform_label;
1413 size_t platform_labels;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001414 unsigned int index;
1415
1416 ASSERT_RTNL();
1417
1418 index = cb->args[0];
Robert Shearmana6affd22015-08-03 17:50:04 +01001419 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1420 index = MPLS_LABEL_FIRST_UNRESERVED;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001421
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001422 platform_label = rtnl_dereference(net->mpls.platform_label);
1423 platform_labels = net->mpls.platform_labels;
1424 for (; index < platform_labels; index++) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001425 struct mpls_route *rt;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001426 rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001427 if (!rt)
1428 continue;
1429
1430 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1431 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1432 index, rt, NLM_F_MULTI) < 0)
1433 break;
1434 }
1435 cb->args[0] = index;
1436
1437 return skb->len;
1438}
1439
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001440static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1441{
1442 size_t payload =
1443 NLMSG_ALIGN(sizeof(struct rtmsg))
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001444 + nla_total_size(4); /* RTA_DST */
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001445
1446 if (rt->rt_nhn == 1) {
1447 struct mpls_nh *nh = rt->rt_nh;
1448
1449 if (nh->nh_dev)
1450 payload += nla_total_size(4); /* RTA_OIF */
1451 payload += nla_total_size(2 + nh->nh_via_alen); /* RTA_VIA */
1452 if (nh->nh_labels) /* RTA_NEWDST */
1453 payload += nla_total_size(nh->nh_labels * 4);
1454 } else {
1455 /* each nexthop is packed in an attribute */
1456 size_t nhsize = 0;
1457
1458 for_nexthops(rt) {
1459 nhsize += nla_total_size(sizeof(struct rtnexthop));
1460 nhsize += nla_total_size(2 + nh->nh_via_alen);
1461 if (nh->nh_labels)
1462 nhsize += nla_total_size(nh->nh_labels * 4);
1463 } endfor_nexthops(rt);
1464 /* nested attribute */
1465 payload += nla_total_size(nhsize);
1466 }
1467
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001468 return payload;
1469}
1470
1471static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1472 struct nlmsghdr *nlh, struct net *net, u32 portid,
1473 unsigned int nlm_flags)
1474{
1475 struct sk_buff *skb;
1476 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1477 int err = -ENOBUFS;
1478
1479 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1480 if (skb == NULL)
1481 goto errout;
1482
1483 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1484 if (err < 0) {
1485 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1486 WARN_ON(err == -EMSGSIZE);
1487 kfree_skb(skb);
1488 goto errout;
1489 }
1490 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1491
1492 return;
1493errout:
1494 if (err < 0)
1495 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1496}
1497
Eric W. Biederman7720c012015-03-03 19:11:20 -06001498static int resize_platform_label_table(struct net *net, size_t limit)
1499{
1500 size_t size = sizeof(struct mpls_route *) * limit;
1501 size_t old_limit;
1502 size_t cp_size;
1503 struct mpls_route __rcu **labels = NULL, **old;
1504 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1505 unsigned index;
1506
1507 if (size) {
1508 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1509 if (!labels)
1510 labels = vzalloc(size);
1511
1512 if (!labels)
1513 goto nolabels;
1514 }
1515
1516 /* In case the predefined labels need to be populated */
Tom Herbert78f5b892015-05-07 08:08:51 -07001517 if (limit > MPLS_LABEL_IPV4NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001518 struct net_device *lo = net->loopback_dev;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001519 rt0 = mpls_rt_alloc(1, lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001520 if (!rt0)
1521 goto nort0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001522 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001523 rt0->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001524 rt0->rt_payload_type = MPT_IPV4;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001525 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00001526 rt0->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001527 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
1528 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001529 }
Tom Herbert78f5b892015-05-07 08:08:51 -07001530 if (limit > MPLS_LABEL_IPV6NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001531 struct net_device *lo = net->loopback_dev;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001532 rt2 = mpls_rt_alloc(1, lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001533 if (!rt2)
1534 goto nort2;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001535 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001536 rt2->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001537 rt2->rt_payload_type = MPT_IPV6;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001538 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00001539 rt2->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001540 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
1541 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001542 }
1543
1544 rtnl_lock();
1545 /* Remember the original table */
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001546 old = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001547 old_limit = net->mpls.platform_labels;
1548
1549 /* Free any labels beyond the new table */
1550 for (index = limit; index < old_limit; index++)
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001551 mpls_route_update(net, index, NULL, NULL);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001552
1553 /* Copy over the old labels */
1554 cp_size = size;
1555 if (old_limit < limit)
1556 cp_size = old_limit * sizeof(struct mpls_route *);
1557
1558 memcpy(labels, old, cp_size);
1559
1560 /* If needed set the predefined labels */
Tom Herbert78f5b892015-05-07 08:08:51 -07001561 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1562 (limit > MPLS_LABEL_IPV6NULL)) {
1563 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001564 rt2 = NULL;
1565 }
1566
Tom Herbert78f5b892015-05-07 08:08:51 -07001567 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1568 (limit > MPLS_LABEL_IPV4NULL)) {
1569 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001570 rt0 = NULL;
1571 }
1572
1573 /* Update the global pointers */
1574 net->mpls.platform_labels = limit;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001575 rcu_assign_pointer(net->mpls.platform_label, labels);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001576
1577 rtnl_unlock();
1578
1579 mpls_rt_free(rt2);
1580 mpls_rt_free(rt0);
1581
1582 if (old) {
1583 synchronize_rcu();
1584 kvfree(old);
1585 }
1586 return 0;
1587
1588nort2:
1589 mpls_rt_free(rt0);
1590nort0:
1591 kvfree(labels);
1592nolabels:
1593 return -ENOMEM;
1594}
1595
1596static int mpls_platform_labels(struct ctl_table *table, int write,
1597 void __user *buffer, size_t *lenp, loff_t *ppos)
1598{
1599 struct net *net = table->data;
1600 int platform_labels = net->mpls.platform_labels;
1601 int ret;
1602 struct ctl_table tmp = {
1603 .procname = table->procname,
1604 .data = &platform_labels,
1605 .maxlen = sizeof(int),
1606 .mode = table->mode,
1607 .extra1 = &zero,
1608 .extra2 = &label_limit,
1609 };
1610
1611 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1612
1613 if (write && ret == 0)
1614 ret = resize_platform_label_table(net, platform_labels);
1615
1616 return ret;
1617}
1618
Robert Shearman37bde792015-04-22 11:14:38 +01001619static const struct ctl_table mpls_table[] = {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001620 {
1621 .procname = "platform_labels",
1622 .data = NULL,
1623 .maxlen = sizeof(int),
1624 .mode = 0644,
1625 .proc_handler = mpls_platform_labels,
1626 },
1627 { }
1628};
1629
Eric W. Biederman01891972015-03-03 19:10:47 -06001630static int mpls_net_init(struct net *net)
1631{
Eric W. Biederman7720c012015-03-03 19:11:20 -06001632 struct ctl_table *table;
1633
Eric W. Biederman01891972015-03-03 19:10:47 -06001634 net->mpls.platform_labels = 0;
1635 net->mpls.platform_label = NULL;
1636
Eric W. Biederman7720c012015-03-03 19:11:20 -06001637 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1638 if (table == NULL)
1639 return -ENOMEM;
1640
1641 table[0].data = net;
1642 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07001643 if (net->mpls.ctl == NULL) {
1644 kfree(table);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001645 return -ENOMEM;
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07001646 }
Eric W. Biederman7720c012015-03-03 19:11:20 -06001647
Eric W. Biederman01891972015-03-03 19:10:47 -06001648 return 0;
1649}
1650
1651static void mpls_net_exit(struct net *net)
1652{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001653 struct mpls_route __rcu **platform_label;
1654 size_t platform_labels;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001655 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -06001656 unsigned int index;
1657
Eric W. Biederman7720c012015-03-03 19:11:20 -06001658 table = net->mpls.ctl->ctl_table_arg;
1659 unregister_net_sysctl_table(net->mpls.ctl);
1660 kfree(table);
1661
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001662 /* An rcu grace period has passed since there was a device in
1663 * the network namespace (and thus the last in flight packet)
Eric W. Biederman01891972015-03-03 19:10:47 -06001664 * left this network namespace. This is because
1665 * unregister_netdevice_many and netdev_run_todo has completed
1666 * for each network device that was in this network namespace.
1667 *
1668 * As such no additional rcu synchronization is necessary when
1669 * freeing the platform_label table.
1670 */
1671 rtnl_lock();
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001672 platform_label = rtnl_dereference(net->mpls.platform_label);
1673 platform_labels = net->mpls.platform_labels;
1674 for (index = 0; index < platform_labels; index++) {
1675 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1676 RCU_INIT_POINTER(platform_label[index], NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001677 mpls_rt_free(rt);
1678 }
1679 rtnl_unlock();
1680
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001681 kvfree(platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06001682}
1683
1684static struct pernet_operations mpls_net_ops = {
1685 .init = mpls_net_init,
1686 .exit = mpls_net_exit,
1687};
1688
1689static int __init mpls_init(void)
1690{
1691 int err;
1692
1693 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1694
1695 err = register_pernet_subsys(&mpls_net_ops);
1696 if (err)
1697 goto out;
1698
1699 err = register_netdevice_notifier(&mpls_dev_notifier);
1700 if (err)
1701 goto out_unregister_pernet;
1702
1703 dev_add_pack(&mpls_packet_type);
1704
Eric W. Biederman03c05662015-03-03 19:13:56 -06001705 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1706 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1707 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001708 err = 0;
1709out:
1710 return err;
1711
1712out_unregister_pernet:
1713 unregister_pernet_subsys(&mpls_net_ops);
1714 goto out;
1715}
1716module_init(mpls_init);
1717
1718static void __exit mpls_exit(void)
1719{
Eric W. Biederman03c05662015-03-03 19:13:56 -06001720 rtnl_unregister_all(PF_MPLS);
Eric W. Biederman01891972015-03-03 19:10:47 -06001721 dev_remove_pack(&mpls_packet_type);
1722 unregister_netdevice_notifier(&mpls_dev_notifier);
1723 unregister_pernet_subsys(&mpls_net_ops);
1724}
1725module_exit(mpls_exit);
1726
1727MODULE_DESCRIPTION("MultiProtocol Label Switching");
1728MODULE_LICENSE("GPL v2");
1729MODULE_ALIAS_NETPROTO(PF_MPLS);