blob: ac1c116abaacf70c2d07e922bf37de8834cd8d0c [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
Robert Shearman1c78efa2015-10-23 06:03:28 -070099static struct mpls_nh *mpls_select_multipath(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;
106 int nh_index = 0;
107 u32 hash = 0;
108
109 /* No need to look further into packet if there's only
110 * one path
111 */
112 if (rt->rt_nhn == 1)
113 goto out;
114
115 for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos;
116 label_index++) {
117 if (!pskb_may_pull(skb, sizeof(*hdr) * label_index))
118 break;
119
120 /* Read and decode the current label */
121 hdr = mpls_hdr(skb) + label_index;
122 dec = mpls_entry_decode(hdr);
123
124 /* RFC6790 - reserved labels MUST NOT be used as keys
125 * for the load-balancing function
126 */
127 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
128 hash = jhash_1word(dec.label, hash);
129
130 /* The entropy label follows the entropy label
131 * indicator, so this means that the entropy
132 * label was just added to the hash - no need to
133 * go any deeper either in the label stack or in the
134 * payload
135 */
136 if (eli_seen)
137 break;
138 } else if (dec.label == MPLS_LABEL_ENTROPY) {
139 eli_seen = true;
140 }
141
142 bos = dec.bos;
143 if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index +
144 sizeof(struct iphdr))) {
145 const struct iphdr *v4hdr;
146
147 v4hdr = (const struct iphdr *)(mpls_hdr(skb) +
148 label_index);
149 if (v4hdr->version == 4) {
150 hash = jhash_3words(ntohl(v4hdr->saddr),
151 ntohl(v4hdr->daddr),
152 v4hdr->protocol, hash);
153 } else if (v4hdr->version == 6 &&
154 pskb_may_pull(skb, sizeof(*hdr) * label_index +
155 sizeof(struct ipv6hdr))) {
156 const struct ipv6hdr *v6hdr;
157
158 v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) +
159 label_index);
160
161 hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
162 hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
163 hash = jhash_1word(v6hdr->nexthdr, hash);
164 }
165 }
166 }
167
168 nh_index = hash % rt->rt_nhn;
169out:
170 return &rt->rt_nh[nh_index];
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700171}
172
Eric W. Biederman01891972015-03-03 19:10:47 -0600173static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
174 struct mpls_entry_decoded dec)
175{
Robert Shearman118d5232015-08-06 11:04:56 +0100176 enum mpls_payload_type payload_type;
177 bool success = false;
Eric W. Biederman01891972015-03-03 19:10:47 -0600178
Eric W. Biederman76fecd82015-03-12 18:22:59 -0500179 /* The IPv4 code below accesses through the IPv4 header
180 * checksum, which is 12 bytes into the packet.
181 * The IPv6 code below accesses through the IPv6 hop limit
182 * which is 8 bytes into the packet.
183 *
184 * For all supported cases there should always be at least 12
185 * bytes of packet data present. The IPv4 header is 20 bytes
186 * without options and the IPv6 header is always 40 bytes
187 * long.
188 */
189 if (!pskb_may_pull(skb, 12))
190 return false;
191
Robert Shearman118d5232015-08-06 11:04:56 +0100192 payload_type = rt->rt_payload_type;
193 if (payload_type == MPT_UNSPEC)
194 payload_type = ip_hdr(skb)->version;
195
196 switch (payload_type) {
197 case MPT_IPV4: {
198 struct iphdr *hdr4 = ip_hdr(skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600199 skb->protocol = htons(ETH_P_IP);
200 csum_replace2(&hdr4->check,
201 htons(hdr4->ttl << 8),
202 htons(dec.ttl << 8));
203 hdr4->ttl = dec.ttl;
Robert Shearman118d5232015-08-06 11:04:56 +0100204 success = true;
205 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600206 }
Robert Shearman118d5232015-08-06 11:04:56 +0100207 case MPT_IPV6: {
Eric W. Biederman01891972015-03-03 19:10:47 -0600208 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
209 skb->protocol = htons(ETH_P_IPV6);
210 hdr6->hop_limit = dec.ttl;
Robert Shearman118d5232015-08-06 11:04:56 +0100211 success = true;
212 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600213 }
Robert Shearman118d5232015-08-06 11:04:56 +0100214 case MPT_UNSPEC:
215 break;
216 }
217
Eric W. Biederman01891972015-03-03 19:10:47 -0600218 return success;
219}
220
221static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
222 struct packet_type *pt, struct net_device *orig_dev)
223{
224 struct net *net = dev_net(dev);
225 struct mpls_shim_hdr *hdr;
226 struct mpls_route *rt;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700227 struct mpls_nh *nh;
Eric W. Biederman01891972015-03-03 19:10:47 -0600228 struct mpls_entry_decoded dec;
229 struct net_device *out_dev;
Robert Shearman03c57742015-04-22 11:14:37 +0100230 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600231 unsigned int hh_len;
232 unsigned int new_header_size;
233 unsigned int mtu;
234 int err;
235
236 /* Careful this entire function runs inside of an rcu critical section */
237
Robert Shearman03c57742015-04-22 11:14:37 +0100238 mdev = mpls_dev_get(dev);
Robert Shearman37bde792015-04-22 11:14:38 +0100239 if (!mdev || !mdev->input_enabled)
Robert Shearman03c57742015-04-22 11:14:37 +0100240 goto drop;
241
Eric W. Biederman01891972015-03-03 19:10:47 -0600242 if (skb->pkt_type != PACKET_HOST)
243 goto drop;
244
245 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
246 goto drop;
247
248 if (!pskb_may_pull(skb, sizeof(*hdr)))
249 goto drop;
250
251 /* Read and decode the label */
252 hdr = mpls_hdr(skb);
253 dec = mpls_entry_decode(hdr);
254
255 /* Pop the label */
256 skb_pull(skb, sizeof(*hdr));
257 skb_reset_network_header(skb);
258
259 skb_orphan(skb);
260
261 rt = mpls_route_input_rcu(net, dec.label);
262 if (!rt)
263 goto drop;
264
Robert Shearman1c78efa2015-10-23 06:03:28 -0700265 nh = mpls_select_multipath(rt, skb, dec.bos);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700266 if (!nh)
267 goto drop;
268
Eric W. Biederman01891972015-03-03 19:10:47 -0600269 /* Find the output device */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700270 out_dev = rcu_dereference(nh->nh_dev);
Eric W. Biederman01891972015-03-03 19:10:47 -0600271 if (!mpls_output_possible(out_dev))
272 goto drop;
273
274 if (skb_warn_if_lro(skb))
275 goto drop;
276
277 skb_forward_csum(skb);
278
279 /* Verify ttl is valid */
Eric W. Biedermanaa7da932015-03-07 16:23:23 -0600280 if (dec.ttl <= 1)
Eric W. Biederman01891972015-03-03 19:10:47 -0600281 goto drop;
282 dec.ttl -= 1;
283
284 /* Verify the destination can hold the packet */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700285 new_header_size = mpls_nh_header_size(nh);
Eric W. Biederman01891972015-03-03 19:10:47 -0600286 mtu = mpls_dev_mtu(out_dev);
287 if (mpls_pkt_too_big(skb, mtu - new_header_size))
288 goto drop;
289
290 hh_len = LL_RESERVED_SPACE(out_dev);
291 if (!out_dev->header_ops)
292 hh_len = 0;
293
294 /* Ensure there is enough space for the headers in the skb */
295 if (skb_cow(skb, hh_len + new_header_size))
296 goto drop;
297
298 skb->dev = out_dev;
299 skb->protocol = htons(ETH_P_MPLS_UC);
300
301 if (unlikely(!new_header_size && dec.bos)) {
302 /* Penultimate hop popping */
303 if (!mpls_egress(rt, skb, dec))
304 goto drop;
305 } else {
306 bool bos;
307 int i;
308 skb_push(skb, new_header_size);
309 skb_reset_network_header(skb);
310 /* Push the new labels */
311 hdr = mpls_hdr(skb);
312 bos = dec.bos;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700313 for (i = nh->nh_labels - 1; i >= 0; i--) {
314 hdr[i] = mpls_entry_encode(nh->nh_label[i],
315 dec.ttl, 0, bos);
Eric W. Biederman01891972015-03-03 19:10:47 -0600316 bos = false;
317 }
318 }
319
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000320 err = neigh_xmit(nh->nh_via_table, out_dev, mpls_nh_via(rt, nh), skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600321 if (err)
322 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
323 __func__, err);
324 return 0;
325
326drop:
327 kfree_skb(skb);
328 return NET_RX_DROP;
329}
330
331static struct packet_type mpls_packet_type __read_mostly = {
332 .type = cpu_to_be16(ETH_P_MPLS_UC),
333 .func = mpls_forward,
334};
335
Wu Fengguangf0126532015-03-05 05:33:54 +0800336static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600337 [RTA_DST] = { .type = NLA_U32 },
338 [RTA_OIF] = { .type = NLA_U32 },
339};
340
Eric W. Biedermana2519922015-03-03 19:12:40 -0600341struct mpls_route_config {
Robert Shearman118d5232015-08-06 11:04:56 +0100342 u32 rc_protocol;
343 u32 rc_ifindex;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700344 u8 rc_via_table;
345 u8 rc_via_alen;
Robert Shearman118d5232015-08-06 11:04:56 +0100346 u8 rc_via[MAX_VIA_ALEN];
347 u32 rc_label;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700348 u8 rc_output_labels;
Robert Shearman118d5232015-08-06 11:04:56 +0100349 u32 rc_output_label[MAX_NEW_LABELS];
350 u32 rc_nlflags;
351 enum mpls_payload_type rc_payload_type;
352 struct nl_info rc_nlinfo;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700353 struct rtnexthop *rc_mp;
354 int rc_mp_len;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600355};
356
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000357static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
Eric W. Biederman01891972015-03-03 19:10:47 -0600358{
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000359 u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
Eric W. Biederman01891972015-03-03 19:10:47 -0600360 struct mpls_route *rt;
361
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000362 rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
363 VIA_ALEN_ALIGN) +
364 num_nh * max_alen_aligned,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700365 GFP_KERNEL);
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000366 if (rt) {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700367 rt->rt_nhn = num_nh;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000368 rt->rt_max_alen = max_alen_aligned;
369 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700370
Eric W. Biederman01891972015-03-03 19:10:47 -0600371 return rt;
372}
373
374static void mpls_rt_free(struct mpls_route *rt)
375{
376 if (rt)
377 kfree_rcu(rt, rt_rcu);
378}
379
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600380static void mpls_notify_route(struct net *net, unsigned index,
381 struct mpls_route *old, struct mpls_route *new,
382 const struct nl_info *info)
383{
384 struct nlmsghdr *nlh = info ? info->nlh : NULL;
385 unsigned portid = info ? info->portid : 0;
386 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
387 struct mpls_route *rt = new ? new : old;
388 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
389 /* Ignore reserved labels for now */
Robert Shearmana6affd22015-08-03 17:50:04 +0100390 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600391 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
392}
393
Eric W. Biederman01891972015-03-03 19:10:47 -0600394static void mpls_route_update(struct net *net, unsigned index,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700395 struct mpls_route *new,
Eric W. Biederman01891972015-03-03 19:10:47 -0600396 const struct nl_info *info)
397{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600398 struct mpls_route __rcu **platform_label;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700399 struct mpls_route *rt;
Eric W. Biederman01891972015-03-03 19:10:47 -0600400
401 ASSERT_RTNL();
402
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600403 platform_label = rtnl_dereference(net->mpls.platform_label);
404 rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700405 rcu_assign_pointer(platform_label[index], new);
Eric W. Biederman01891972015-03-03 19:10:47 -0600406
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700407 mpls_notify_route(net, index, rt, new, info);
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600408
Eric W. Biederman01891972015-03-03 19:10:47 -0600409 /* If we removed a route free it now */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700410 mpls_rt_free(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -0600411}
412
Eric W. Biedermana2519922015-03-03 19:12:40 -0600413static unsigned find_free_label(struct net *net)
414{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600415 struct mpls_route __rcu **platform_label;
416 size_t platform_labels;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600417 unsigned index;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600418
419 platform_label = rtnl_dereference(net->mpls.platform_label);
420 platform_labels = net->mpls.platform_labels;
Robert Shearmana6affd22015-08-03 17:50:04 +0100421 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
422 index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600423 if (!rtnl_dereference(platform_label[index]))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600424 return index;
425 }
426 return LABEL_NOT_SPECIFIED;
427}
428
Roopa Prabhubf215632015-07-30 13:34:54 -0700429#if IS_ENABLED(CONFIG_INET)
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000430static struct net_device *inet_fib_lookup_dev(struct net *net,
431 const void *addr)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700432{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300433 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700434 struct rtable *rt;
435 struct in_addr daddr;
436
437 memcpy(&daddr, addr, sizeof(struct in_addr));
438 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
439 if (IS_ERR(rt))
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300440 return ERR_CAST(rt);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700441
442 dev = rt->dst.dev;
443 dev_hold(dev);
444
445 ip_rt_put(rt);
446
Roopa Prabhu01faef22015-07-21 09:16:24 -0700447 return dev;
448}
Roopa Prabhubf215632015-07-30 13:34:54 -0700449#else
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000450static struct net_device *inet_fib_lookup_dev(struct net *net,
451 const void *addr)
Roopa Prabhubf215632015-07-30 13:34:54 -0700452{
453 return ERR_PTR(-EAFNOSUPPORT);
454}
455#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700456
Roopa Prabhubf215632015-07-30 13:34:54 -0700457#if IS_ENABLED(CONFIG_IPV6)
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000458static struct net_device *inet6_fib_lookup_dev(struct net *net,
459 const void *addr)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700460{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300461 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700462 struct dst_entry *dst;
463 struct flowi6 fl6;
Roopa Prabhubf215632015-07-30 13:34:54 -0700464 int err;
465
466 if (!ipv6_stub)
467 return ERR_PTR(-EAFNOSUPPORT);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700468
469 memset(&fl6, 0, sizeof(fl6));
470 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
Roopa Prabhubf215632015-07-30 13:34:54 -0700471 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
472 if (err)
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300473 return ERR_PTR(err);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700474
475 dev = dst->dev;
476 dev_hold(dev);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700477 dst_release(dst);
478
479 return dev;
480}
Roopa Prabhubf215632015-07-30 13:34:54 -0700481#else
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000482static struct net_device *inet6_fib_lookup_dev(struct net *net,
483 const void *addr)
Roopa Prabhubf215632015-07-30 13:34:54 -0700484{
485 return ERR_PTR(-EAFNOSUPPORT);
486}
487#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700488
489static struct net_device *find_outdev(struct net *net,
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000490 struct mpls_route *rt,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700491 struct mpls_nh *nh, int oif)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700492{
493 struct net_device *dev = NULL;
494
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700495 if (!oif) {
496 switch (nh->nh_via_table) {
Roopa Prabhu01faef22015-07-21 09:16:24 -0700497 case NEIGH_ARP_TABLE:
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000498 dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
Roopa Prabhu01faef22015-07-21 09:16:24 -0700499 break;
500 case NEIGH_ND_TABLE:
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000501 dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
Roopa Prabhu01faef22015-07-21 09:16:24 -0700502 break;
503 case NEIGH_LINK_TABLE:
504 break;
505 }
506 } else {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700507 dev = dev_get_by_index(net, oif);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700508 }
509
Roopa Prabhu3dcb6152015-08-04 06:36:24 -0700510 if (!dev)
511 return ERR_PTR(-ENODEV);
512
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700513 /* The caller is holding rtnl anyways, so release the dev reference */
514 dev_put(dev);
515
Roopa Prabhu01faef22015-07-21 09:16:24 -0700516 return dev;
517}
518
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000519static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
520 struct mpls_nh *nh, int oif)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700521{
522 struct net_device *dev = NULL;
523 int err = -ENODEV;
524
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000525 dev = find_outdev(net, rt, nh, oif);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700526 if (IS_ERR(dev)) {
527 err = PTR_ERR(dev);
528 dev = NULL;
529 goto errout;
530 }
531
532 /* Ensure this is a supported device */
533 err = -EINVAL;
534 if (!mpls_dev_get(dev))
535 goto errout;
536
Robert Shearmana3e948e2015-12-10 19:30:48 +0000537 if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
538 (dev->addr_len != nh->nh_via_alen))
539 goto errout;
540
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700541 RCU_INIT_POINTER(nh->nh_dev, dev);
542
543 return 0;
544
545errout:
546 return err;
547}
548
549static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
550 struct mpls_route *rt)
551{
552 struct net *net = cfg->rc_nlinfo.nl_net;
553 struct mpls_nh *nh = rt->rt_nh;
554 int err;
555 int i;
556
557 if (!nh)
558 return -ENOMEM;
559
560 err = -EINVAL;
561 /* Ensure only a supported number of labels are present */
562 if (cfg->rc_output_labels > MAX_NEW_LABELS)
563 goto errout;
564
565 nh->nh_labels = cfg->rc_output_labels;
566 for (i = 0; i < nh->nh_labels; i++)
567 nh->nh_label[i] = cfg->rc_output_label[i];
568
569 nh->nh_via_table = cfg->rc_via_table;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000570 memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700571 nh->nh_via_alen = cfg->rc_via_alen;
572
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000573 err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700574 if (err)
575 goto errout;
576
577 return 0;
578
579errout:
580 return err;
581}
582
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000583static int mpls_nh_build(struct net *net, struct mpls_route *rt,
584 struct mpls_nh *nh, int oif,
585 struct nlattr *via, struct nlattr *newdst)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700586{
587 int err = -ENOMEM;
588
589 if (!nh)
590 goto errout;
591
592 if (newdst) {
593 err = nla_get_labels(newdst, MAX_NEW_LABELS,
594 &nh->nh_labels, nh->nh_label);
595 if (err)
596 goto errout;
597 }
598
599 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000600 __mpls_nh_via(rt, nh));
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700601 if (err)
602 goto errout;
603
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000604 err = mpls_nh_assign_dev(net, rt, nh, oif);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700605 if (err)
606 goto errout;
607
608 return 0;
609
610errout:
611 return err;
612}
613
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000614static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
615 u8 cfg_via_alen, u8 *max_via_alen)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700616{
617 int nhs = 0;
618 int remaining = len;
619
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000620 if (!rtnh) {
621 *max_via_alen = cfg_via_alen;
622 return 1;
623 }
624
625 *max_via_alen = 0;
626
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700627 while (rtnh_ok(rtnh, remaining)) {
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000628 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
629 int attrlen;
630
631 attrlen = rtnh_attrlen(rtnh);
632 nla = nla_find(attrs, attrlen, RTA_VIA);
633 if (nla && nla_len(nla) >=
634 offsetof(struct rtvia, rtvia_addr)) {
635 int via_alen = nla_len(nla) -
636 offsetof(struct rtvia, rtvia_addr);
637
638 if (via_alen <= MAX_VIA_ALEN)
639 *max_via_alen = max_t(u16, *max_via_alen,
640 via_alen);
641 }
642
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700643 nhs++;
644 rtnh = rtnh_next(rtnh, &remaining);
645 }
646
647 /* leftover implies invalid nexthop configuration, discard it */
648 return remaining > 0 ? 0 : nhs;
649}
650
651static int mpls_nh_build_multi(struct mpls_route_config *cfg,
652 struct mpls_route *rt)
653{
654 struct rtnexthop *rtnh = cfg->rc_mp;
655 struct nlattr *nla_via, *nla_newdst;
656 int remaining = cfg->rc_mp_len;
657 int nhs = 0;
658 int err = 0;
659
660 change_nexthops(rt) {
661 int attrlen;
662
663 nla_via = NULL;
664 nla_newdst = NULL;
665
666 err = -EINVAL;
667 if (!rtnh_ok(rtnh, remaining))
668 goto errout;
669
Robert Shearman1c78efa2015-10-23 06:03:28 -0700670 /* neither weighted multipath nor any flags
671 * are supported
672 */
673 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
674 goto errout;
675
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700676 attrlen = rtnh_attrlen(rtnh);
677 if (attrlen > 0) {
678 struct nlattr *attrs = rtnh_attrs(rtnh);
679
680 nla_via = nla_find(attrs, attrlen, RTA_VIA);
681 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
682 }
683
684 if (!nla_via)
685 goto errout;
686
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000687 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700688 rtnh->rtnh_ifindex, nla_via,
689 nla_newdst);
690 if (err)
691 goto errout;
692
693 rtnh = rtnh_next(rtnh, &remaining);
694 nhs++;
695 } endfor_nexthops(rt);
696
697 rt->rt_nhn = nhs;
698
699 return 0;
700
701errout:
702 return err;
703}
704
Eric W. Biedermana2519922015-03-03 19:12:40 -0600705static int mpls_route_add(struct mpls_route_config *cfg)
706{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600707 struct mpls_route __rcu **platform_label;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600708 struct net *net = cfg->rc_nlinfo.nl_net;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600709 struct mpls_route *rt, *old;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600710 int err = -EINVAL;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000711 u8 max_via_alen;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700712 unsigned index;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000713 int nhs;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600714
715 index = cfg->rc_label;
716
717 /* If a label was not specified during insert pick one */
718 if ((index == LABEL_NOT_SPECIFIED) &&
719 (cfg->rc_nlflags & NLM_F_CREATE)) {
720 index = find_free_label(net);
721 }
722
Robert Shearmana6affd22015-08-03 17:50:04 +0100723 /* Reserved labels may not be set */
724 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600725 goto errout;
726
727 /* The full 20 bit range may not be supported. */
728 if (index >= net->mpls.platform_labels)
729 goto errout;
730
Eric W. Biedermana2519922015-03-03 19:12:40 -0600731 /* Append makes no sense with mpls */
Eric W. Biederman0f7bbd52015-03-07 16:22:40 -0600732 err = -EOPNOTSUPP;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600733 if (cfg->rc_nlflags & NLM_F_APPEND)
734 goto errout;
735
736 err = -EEXIST;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600737 platform_label = rtnl_dereference(net->mpls.platform_label);
738 old = rtnl_dereference(platform_label[index]);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600739 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
740 goto errout;
741
742 err = -EEXIST;
743 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
744 goto errout;
745
746 err = -ENOENT;
747 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
748 goto errout;
749
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000750 err = -EINVAL;
751 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
752 cfg->rc_via_alen, &max_via_alen);
753 if (nhs == 0)
754 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700755
Eric W. Biedermana2519922015-03-03 19:12:40 -0600756 err = -ENOMEM;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000757 rt = mpls_rt_alloc(nhs, max_via_alen);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600758 if (!rt)
759 goto errout;
760
Eric W. Biedermana2519922015-03-03 19:12:40 -0600761 rt->rt_protocol = cfg->rc_protocol;
Robert Shearman118d5232015-08-06 11:04:56 +0100762 rt->rt_payload_type = cfg->rc_payload_type;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600763
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700764 if (cfg->rc_mp)
765 err = mpls_nh_build_multi(cfg, rt);
766 else
767 err = mpls_nh_build_from_cfg(cfg, rt);
768 if (err)
769 goto freert;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600770
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700771 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
772
Eric W. Biedermana2519922015-03-03 19:12:40 -0600773 return 0;
774
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700775freert:
776 mpls_rt_free(rt);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600777errout:
Eric W. Biedermana2519922015-03-03 19:12:40 -0600778 return err;
779}
780
781static int mpls_route_del(struct mpls_route_config *cfg)
782{
783 struct net *net = cfg->rc_nlinfo.nl_net;
784 unsigned index;
785 int err = -EINVAL;
786
787 index = cfg->rc_label;
788
Robert Shearmana6affd22015-08-03 17:50:04 +0100789 /* Reserved labels may not be removed */
790 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600791 goto errout;
792
793 /* The full 20 bit range may not be supported */
794 if (index >= net->mpls.platform_labels)
795 goto errout;
796
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700797 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600798
799 err = 0;
800errout:
801 return err;
802}
803
Robert Shearman37bde792015-04-22 11:14:38 +0100804#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
805 (&((struct mpls_dev *)0)->field)
806
807static const struct ctl_table mpls_dev_table[] = {
808 {
809 .procname = "input",
810 .maxlen = sizeof(int),
811 .mode = 0644,
812 .proc_handler = proc_dointvec,
813 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
814 },
815 { }
816};
817
818static int mpls_dev_sysctl_register(struct net_device *dev,
819 struct mpls_dev *mdev)
820{
821 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
822 struct ctl_table *table;
823 int i;
824
825 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
826 if (!table)
827 goto out;
828
829 /* Table data contains only offsets relative to the base of
830 * the mdev at this point, so make them absolute.
831 */
832 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
833 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
834
835 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
836
837 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
838 if (!mdev->sysctl)
839 goto free;
840
841 return 0;
842
843free:
844 kfree(table);
845out:
846 return -ENOBUFS;
847}
848
849static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
850{
851 struct ctl_table *table;
852
853 table = mdev->sysctl->ctl_table_arg;
854 unregister_net_sysctl_table(mdev->sysctl);
855 kfree(table);
856}
857
Robert Shearman03c57742015-04-22 11:14:37 +0100858static struct mpls_dev *mpls_add_dev(struct net_device *dev)
859{
860 struct mpls_dev *mdev;
861 int err = -ENOMEM;
862
863 ASSERT_RTNL();
864
865 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
866 if (!mdev)
867 return ERR_PTR(err);
868
Robert Shearman37bde792015-04-22 11:14:38 +0100869 err = mpls_dev_sysctl_register(dev, mdev);
870 if (err)
871 goto free;
872
Robert Shearman03c57742015-04-22 11:14:37 +0100873 rcu_assign_pointer(dev->mpls_ptr, mdev);
874
875 return mdev;
Robert Shearman37bde792015-04-22 11:14:38 +0100876
877free:
878 kfree(mdev);
879 return ERR_PTR(err);
Robert Shearman03c57742015-04-22 11:14:37 +0100880}
881
Eric W. Biederman01891972015-03-03 19:10:47 -0600882static void mpls_ifdown(struct net_device *dev)
883{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600884 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -0600885 struct net *net = dev_net(dev);
Robert Shearman03c57742015-04-22 11:14:37 +0100886 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600887 unsigned index;
888
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600889 platform_label = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -0600890 for (index = 0; index < net->mpls.platform_labels; index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600891 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman01891972015-03-03 19:10:47 -0600892 if (!rt)
893 continue;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700894 for_nexthops(rt) {
895 if (rtnl_dereference(nh->nh_dev) != dev)
896 continue;
897 nh->nh_dev = NULL;
898 } endfor_nexthops(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -0600899 }
Robert Shearman03c57742015-04-22 11:14:37 +0100900
901 mdev = mpls_dev_get(dev);
902 if (!mdev)
903 return;
904
Robert Shearman37bde792015-04-22 11:14:38 +0100905 mpls_dev_sysctl_unregister(mdev);
906
Robert Shearman03c57742015-04-22 11:14:37 +0100907 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
908
Robert Shearman25cc8f02015-06-05 18:54:45 +0100909 kfree_rcu(mdev, rcu);
Eric W. Biederman01891972015-03-03 19:10:47 -0600910}
911
912static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
913 void *ptr)
914{
915 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Robert Shearman03c57742015-04-22 11:14:37 +0100916 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600917
918 switch(event) {
Robert Shearman03c57742015-04-22 11:14:37 +0100919 case NETDEV_REGISTER:
920 /* For now just support ethernet devices */
921 if ((dev->type == ARPHRD_ETHER) ||
922 (dev->type == ARPHRD_LOOPBACK)) {
923 mdev = mpls_add_dev(dev);
924 if (IS_ERR(mdev))
925 return notifier_from_errno(PTR_ERR(mdev));
926 }
927 break;
928
Eric W. Biederman01891972015-03-03 19:10:47 -0600929 case NETDEV_UNREGISTER:
930 mpls_ifdown(dev);
931 break;
Robert Shearman0fae3bf2015-06-11 19:58:26 +0100932 case NETDEV_CHANGENAME:
933 mdev = mpls_dev_get(dev);
934 if (mdev) {
935 int err;
936
937 mpls_dev_sysctl_unregister(mdev);
938 err = mpls_dev_sysctl_register(dev, mdev);
939 if (err)
940 return notifier_from_errno(err);
941 }
942 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600943 }
944 return NOTIFY_OK;
945}
946
947static struct notifier_block mpls_dev_notifier = {
948 .notifier_call = mpls_dev_notify,
949};
950
Eric W. Biederman03c05662015-03-03 19:13:56 -0600951static int nla_put_via(struct sk_buff *skb,
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600952 u8 table, const void *addr, int alen)
Eric W. Biederman03c05662015-03-03 19:13:56 -0600953{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600954 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
955 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
956 };
Eric W. Biederman03c05662015-03-03 19:13:56 -0600957 struct nlattr *nla;
958 struct rtvia *via;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600959 int family = AF_UNSPEC;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600960
961 nla = nla_reserve(skb, RTA_VIA, alen + 2);
962 if (!nla)
963 return -EMSGSIZE;
964
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600965 if (table <= NEIGH_NR_TABLES)
966 family = table_to_family[table];
967
Eric W. Biederman03c05662015-03-03 19:13:56 -0600968 via = nla_data(nla);
969 via->rtvia_family = family;
970 memcpy(via->rtvia_addr, addr, alen);
971 return 0;
972}
973
Eric W. Biederman966bae32015-03-03 19:13:19 -0600974int nla_put_labels(struct sk_buff *skb, int attrtype,
975 u8 labels, const u32 label[])
976{
977 struct nlattr *nla;
978 struct mpls_shim_hdr *nla_label;
979 bool bos;
980 int i;
981 nla = nla_reserve(skb, attrtype, labels*4);
982 if (!nla)
983 return -EMSGSIZE;
984
985 nla_label = nla_data(nla);
986 bos = true;
987 for (i = labels - 1; i >= 0; i--) {
988 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
989 bos = false;
990 }
991
992 return 0;
993}
Roopa Prabhuface0182015-07-21 10:43:52 +0200994EXPORT_SYMBOL_GPL(nla_put_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600995
996int nla_get_labels(const struct nlattr *nla,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700997 u32 max_labels, u8 *labels, u32 label[])
Eric W. Biederman966bae32015-03-03 19:13:19 -0600998{
999 unsigned len = nla_len(nla);
1000 unsigned nla_labels;
1001 struct mpls_shim_hdr *nla_label;
1002 bool bos;
1003 int i;
1004
1005 /* len needs to be an even multiple of 4 (the label size) */
1006 if (len & 3)
1007 return -EINVAL;
1008
1009 /* Limit the number of new labels allowed */
1010 nla_labels = len/4;
1011 if (nla_labels > max_labels)
1012 return -EINVAL;
1013
1014 nla_label = nla_data(nla);
1015 bos = true;
1016 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1017 struct mpls_entry_decoded dec;
1018 dec = mpls_entry_decode(nla_label + i);
1019
1020 /* Ensure the bottom of stack flag is properly set
1021 * and ttl and tc are both clear.
1022 */
1023 if ((dec.bos != bos) || dec.ttl || dec.tc)
1024 return -EINVAL;
1025
Robert Shearman5a9ab012015-04-22 11:14:39 +01001026 switch (dec.label) {
Tom Herbert78f5b892015-05-07 08:08:51 -07001027 case MPLS_LABEL_IMPLNULL:
Robert Shearman5a9ab012015-04-22 11:14:39 +01001028 /* RFC3032: This is a label that an LSR may
1029 * assign and distribute, but which never
1030 * actually appears in the encapsulation.
1031 */
1032 return -EINVAL;
1033 }
1034
Eric W. Biederman966bae32015-03-03 19:13:19 -06001035 label[i] = dec.label;
1036 }
1037 *labels = nla_labels;
1038 return 0;
1039}
Roopa Prabhuface0182015-07-21 10:43:52 +02001040EXPORT_SYMBOL_GPL(nla_get_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001041
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001042int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1043 u8 *via_table, u8 via_addr[])
1044{
1045 struct rtvia *via = nla_data(nla);
1046 int err = -EINVAL;
1047 int alen;
1048
1049 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1050 goto errout;
1051 alen = nla_len(nla) -
1052 offsetof(struct rtvia, rtvia_addr);
1053 if (alen > MAX_VIA_ALEN)
1054 goto errout;
1055
1056 /* Validate the address family */
1057 switch (via->rtvia_family) {
1058 case AF_PACKET:
1059 *via_table = NEIGH_LINK_TABLE;
1060 break;
1061 case AF_INET:
1062 *via_table = NEIGH_ARP_TABLE;
1063 if (alen != 4)
1064 goto errout;
1065 break;
1066 case AF_INET6:
1067 *via_table = NEIGH_ND_TABLE;
1068 if (alen != 16)
1069 goto errout;
1070 break;
1071 default:
1072 /* Unsupported address family */
1073 goto errout;
1074 }
1075
1076 memcpy(via_addr, via->rtvia_addr, alen);
1077 *via_alen = alen;
1078 err = 0;
1079
1080errout:
1081 return err;
1082}
1083
Eric W. Biederman03c05662015-03-03 19:13:56 -06001084static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1085 struct mpls_route_config *cfg)
1086{
1087 struct rtmsg *rtm;
1088 struct nlattr *tb[RTA_MAX+1];
1089 int index;
1090 int err;
1091
1092 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1093 if (err < 0)
1094 goto errout;
1095
1096 err = -EINVAL;
1097 rtm = nlmsg_data(nlh);
1098 memset(cfg, 0, sizeof(*cfg));
1099
1100 if (rtm->rtm_family != AF_MPLS)
1101 goto errout;
1102 if (rtm->rtm_dst_len != 20)
1103 goto errout;
1104 if (rtm->rtm_src_len != 0)
1105 goto errout;
1106 if (rtm->rtm_tos != 0)
1107 goto errout;
1108 if (rtm->rtm_table != RT_TABLE_MAIN)
1109 goto errout;
1110 /* Any value is acceptable for rtm_protocol */
1111
1112 /* As mpls uses destination specific addresses
1113 * (or source specific address in the case of multicast)
1114 * all addresses have universal scope.
1115 */
1116 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1117 goto errout;
1118 if (rtm->rtm_type != RTN_UNICAST)
1119 goto errout;
1120 if (rtm->rtm_flags != 0)
1121 goto errout;
1122
1123 cfg->rc_label = LABEL_NOT_SPECIFIED;
1124 cfg->rc_protocol = rtm->rtm_protocol;
1125 cfg->rc_nlflags = nlh->nlmsg_flags;
1126 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1127 cfg->rc_nlinfo.nlh = nlh;
1128 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1129
1130 for (index = 0; index <= RTA_MAX; index++) {
1131 struct nlattr *nla = tb[index];
1132 if (!nla)
1133 continue;
1134
1135 switch(index) {
1136 case RTA_OIF:
1137 cfg->rc_ifindex = nla_get_u32(nla);
1138 break;
1139 case RTA_NEWDST:
1140 if (nla_get_labels(nla, MAX_NEW_LABELS,
1141 &cfg->rc_output_labels,
1142 cfg->rc_output_label))
1143 goto errout;
1144 break;
1145 case RTA_DST:
1146 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001147 u8 label_count;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001148 if (nla_get_labels(nla, 1, &label_count,
1149 &cfg->rc_label))
1150 goto errout;
1151
Robert Shearmana6affd22015-08-03 17:50:04 +01001152 /* Reserved labels may not be set */
1153 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001154 goto errout;
1155
1156 break;
1157 }
1158 case RTA_VIA:
1159 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001160 if (nla_get_via(nla, &cfg->rc_via_alen,
1161 &cfg->rc_via_table, cfg->rc_via))
Robert Shearmanf8d54af2015-03-06 10:47:00 +00001162 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001163 break;
1164 }
1165 case RTA_MULTIPATH:
1166 {
1167 cfg->rc_mp = nla_data(nla);
1168 cfg->rc_mp_len = nla_len(nla);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001169 break;
1170 }
1171 default:
1172 /* Unsupported attribute */
1173 goto errout;
1174 }
1175 }
1176
1177 err = 0;
1178errout:
1179 return err;
1180}
1181
1182static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1183{
1184 struct mpls_route_config cfg;
1185 int err;
1186
1187 err = rtm_to_route_config(skb, nlh, &cfg);
1188 if (err < 0)
1189 return err;
1190
1191 return mpls_route_del(&cfg);
1192}
1193
1194
1195static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1196{
1197 struct mpls_route_config cfg;
1198 int err;
1199
1200 err = rtm_to_route_config(skb, nlh, &cfg);
1201 if (err < 0)
1202 return err;
1203
1204 return mpls_route_add(&cfg);
1205}
1206
1207static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1208 u32 label, struct mpls_route *rt, int flags)
1209{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001210 struct net_device *dev;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001211 struct nlmsghdr *nlh;
1212 struct rtmsg *rtm;
1213
1214 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1215 if (nlh == NULL)
1216 return -EMSGSIZE;
1217
1218 rtm = nlmsg_data(nlh);
1219 rtm->rtm_family = AF_MPLS;
1220 rtm->rtm_dst_len = 20;
1221 rtm->rtm_src_len = 0;
1222 rtm->rtm_tos = 0;
1223 rtm->rtm_table = RT_TABLE_MAIN;
1224 rtm->rtm_protocol = rt->rt_protocol;
1225 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1226 rtm->rtm_type = RTN_UNICAST;
1227 rtm->rtm_flags = 0;
1228
Eric W. Biederman03c05662015-03-03 19:13:56 -06001229 if (nla_put_labels(skb, RTA_DST, 1, &label))
1230 goto nla_put_failure;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001231 if (rt->rt_nhn == 1) {
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001232 const struct mpls_nh *nh = rt->rt_nh;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001233
1234 if (nh->nh_labels &&
1235 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1236 nh->nh_label))
1237 goto nla_put_failure;
Robert Shearman72dcac92015-12-10 19:30:49 +00001238 if ((nh->nh_via_table != NEIGH_ARP_TABLE ||
1239 nh->nh_via_alen != 0) &&
1240 nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001241 nh->nh_via_alen))
1242 goto nla_put_failure;
1243 dev = rtnl_dereference(nh->nh_dev);
1244 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1245 goto nla_put_failure;
1246 } else {
1247 struct rtnexthop *rtnh;
1248 struct nlattr *mp;
1249
1250 mp = nla_nest_start(skb, RTA_MULTIPATH);
1251 if (!mp)
1252 goto nla_put_failure;
1253
1254 for_nexthops(rt) {
1255 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1256 if (!rtnh)
1257 goto nla_put_failure;
1258
1259 dev = rtnl_dereference(nh->nh_dev);
1260 if (dev)
1261 rtnh->rtnh_ifindex = dev->ifindex;
1262 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1263 nh->nh_labels,
1264 nh->nh_label))
1265 goto nla_put_failure;
1266 if (nla_put_via(skb, nh->nh_via_table,
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001267 mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001268 nh->nh_via_alen))
1269 goto nla_put_failure;
1270
1271 /* length of rtnetlink header + attributes */
1272 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1273 } endfor_nexthops(rt);
1274
1275 nla_nest_end(skb, mp);
1276 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001277
1278 nlmsg_end(skb, nlh);
1279 return 0;
1280
1281nla_put_failure:
1282 nlmsg_cancel(skb, nlh);
1283 return -EMSGSIZE;
1284}
1285
1286static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1287{
1288 struct net *net = sock_net(skb->sk);
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001289 struct mpls_route __rcu **platform_label;
1290 size_t platform_labels;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001291 unsigned int index;
1292
1293 ASSERT_RTNL();
1294
1295 index = cb->args[0];
Robert Shearmana6affd22015-08-03 17:50:04 +01001296 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1297 index = MPLS_LABEL_FIRST_UNRESERVED;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001298
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001299 platform_label = rtnl_dereference(net->mpls.platform_label);
1300 platform_labels = net->mpls.platform_labels;
1301 for (; index < platform_labels; index++) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001302 struct mpls_route *rt;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001303 rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001304 if (!rt)
1305 continue;
1306
1307 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1308 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1309 index, rt, NLM_F_MULTI) < 0)
1310 break;
1311 }
1312 cb->args[0] = index;
1313
1314 return skb->len;
1315}
1316
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001317static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1318{
1319 size_t payload =
1320 NLMSG_ALIGN(sizeof(struct rtmsg))
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001321 + nla_total_size(4); /* RTA_DST */
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001322
1323 if (rt->rt_nhn == 1) {
1324 struct mpls_nh *nh = rt->rt_nh;
1325
1326 if (nh->nh_dev)
1327 payload += nla_total_size(4); /* RTA_OIF */
Robert Shearman72dcac92015-12-10 19:30:49 +00001328 if (nh->nh_via_table != NEIGH_ARP_TABLE ||
1329 nh->nh_via_alen != 0) /* RTA_VIA */
1330 payload += nla_total_size(2 + nh->nh_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001331 if (nh->nh_labels) /* RTA_NEWDST */
1332 payload += nla_total_size(nh->nh_labels * 4);
1333 } else {
1334 /* each nexthop is packed in an attribute */
1335 size_t nhsize = 0;
1336
1337 for_nexthops(rt) {
1338 nhsize += nla_total_size(sizeof(struct rtnexthop));
1339 nhsize += nla_total_size(2 + nh->nh_via_alen);
1340 if (nh->nh_labels)
1341 nhsize += nla_total_size(nh->nh_labels * 4);
1342 } endfor_nexthops(rt);
1343 /* nested attribute */
1344 payload += nla_total_size(nhsize);
1345 }
1346
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001347 return payload;
1348}
1349
1350static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1351 struct nlmsghdr *nlh, struct net *net, u32 portid,
1352 unsigned int nlm_flags)
1353{
1354 struct sk_buff *skb;
1355 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1356 int err = -ENOBUFS;
1357
1358 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1359 if (skb == NULL)
1360 goto errout;
1361
1362 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1363 if (err < 0) {
1364 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1365 WARN_ON(err == -EMSGSIZE);
1366 kfree_skb(skb);
1367 goto errout;
1368 }
1369 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1370
1371 return;
1372errout:
1373 if (err < 0)
1374 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1375}
1376
Eric W. Biederman7720c012015-03-03 19:11:20 -06001377static int resize_platform_label_table(struct net *net, size_t limit)
1378{
1379 size_t size = sizeof(struct mpls_route *) * limit;
1380 size_t old_limit;
1381 size_t cp_size;
1382 struct mpls_route __rcu **labels = NULL, **old;
1383 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1384 unsigned index;
1385
1386 if (size) {
1387 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1388 if (!labels)
1389 labels = vzalloc(size);
1390
1391 if (!labels)
1392 goto nolabels;
1393 }
1394
1395 /* In case the predefined labels need to be populated */
Tom Herbert78f5b892015-05-07 08:08:51 -07001396 if (limit > MPLS_LABEL_IPV4NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001397 struct net_device *lo = net->loopback_dev;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001398 rt0 = mpls_rt_alloc(1, lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001399 if (!rt0)
1400 goto nort0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001401 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001402 rt0->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001403 rt0->rt_payload_type = MPT_IPV4;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001404 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00001405 rt0->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001406 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
1407 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001408 }
Tom Herbert78f5b892015-05-07 08:08:51 -07001409 if (limit > MPLS_LABEL_IPV6NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001410 struct net_device *lo = net->loopback_dev;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001411 rt2 = mpls_rt_alloc(1, lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001412 if (!rt2)
1413 goto nort2;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001414 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001415 rt2->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001416 rt2->rt_payload_type = MPT_IPV6;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001417 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00001418 rt2->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001419 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
1420 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001421 }
1422
1423 rtnl_lock();
1424 /* Remember the original table */
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001425 old = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001426 old_limit = net->mpls.platform_labels;
1427
1428 /* Free any labels beyond the new table */
1429 for (index = limit; index < old_limit; index++)
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001430 mpls_route_update(net, index, NULL, NULL);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001431
1432 /* Copy over the old labels */
1433 cp_size = size;
1434 if (old_limit < limit)
1435 cp_size = old_limit * sizeof(struct mpls_route *);
1436
1437 memcpy(labels, old, cp_size);
1438
1439 /* If needed set the predefined labels */
Tom Herbert78f5b892015-05-07 08:08:51 -07001440 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1441 (limit > MPLS_LABEL_IPV6NULL)) {
1442 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001443 rt2 = NULL;
1444 }
1445
Tom Herbert78f5b892015-05-07 08:08:51 -07001446 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1447 (limit > MPLS_LABEL_IPV4NULL)) {
1448 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001449 rt0 = NULL;
1450 }
1451
1452 /* Update the global pointers */
1453 net->mpls.platform_labels = limit;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001454 rcu_assign_pointer(net->mpls.platform_label, labels);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001455
1456 rtnl_unlock();
1457
1458 mpls_rt_free(rt2);
1459 mpls_rt_free(rt0);
1460
1461 if (old) {
1462 synchronize_rcu();
1463 kvfree(old);
1464 }
1465 return 0;
1466
1467nort2:
1468 mpls_rt_free(rt0);
1469nort0:
1470 kvfree(labels);
1471nolabels:
1472 return -ENOMEM;
1473}
1474
1475static int mpls_platform_labels(struct ctl_table *table, int write,
1476 void __user *buffer, size_t *lenp, loff_t *ppos)
1477{
1478 struct net *net = table->data;
1479 int platform_labels = net->mpls.platform_labels;
1480 int ret;
1481 struct ctl_table tmp = {
1482 .procname = table->procname,
1483 .data = &platform_labels,
1484 .maxlen = sizeof(int),
1485 .mode = table->mode,
1486 .extra1 = &zero,
1487 .extra2 = &label_limit,
1488 };
1489
1490 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1491
1492 if (write && ret == 0)
1493 ret = resize_platform_label_table(net, platform_labels);
1494
1495 return ret;
1496}
1497
Robert Shearman37bde792015-04-22 11:14:38 +01001498static const struct ctl_table mpls_table[] = {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001499 {
1500 .procname = "platform_labels",
1501 .data = NULL,
1502 .maxlen = sizeof(int),
1503 .mode = 0644,
1504 .proc_handler = mpls_platform_labels,
1505 },
1506 { }
1507};
1508
Eric W. Biederman01891972015-03-03 19:10:47 -06001509static int mpls_net_init(struct net *net)
1510{
Eric W. Biederman7720c012015-03-03 19:11:20 -06001511 struct ctl_table *table;
1512
Eric W. Biederman01891972015-03-03 19:10:47 -06001513 net->mpls.platform_labels = 0;
1514 net->mpls.platform_label = NULL;
1515
Eric W. Biederman7720c012015-03-03 19:11:20 -06001516 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1517 if (table == NULL)
1518 return -ENOMEM;
1519
1520 table[0].data = net;
1521 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07001522 if (net->mpls.ctl == NULL) {
1523 kfree(table);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001524 return -ENOMEM;
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07001525 }
Eric W. Biederman7720c012015-03-03 19:11:20 -06001526
Eric W. Biederman01891972015-03-03 19:10:47 -06001527 return 0;
1528}
1529
1530static void mpls_net_exit(struct net *net)
1531{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001532 struct mpls_route __rcu **platform_label;
1533 size_t platform_labels;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001534 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -06001535 unsigned int index;
1536
Eric W. Biederman7720c012015-03-03 19:11:20 -06001537 table = net->mpls.ctl->ctl_table_arg;
1538 unregister_net_sysctl_table(net->mpls.ctl);
1539 kfree(table);
1540
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001541 /* An rcu grace period has passed since there was a device in
1542 * the network namespace (and thus the last in flight packet)
Eric W. Biederman01891972015-03-03 19:10:47 -06001543 * left this network namespace. This is because
1544 * unregister_netdevice_many and netdev_run_todo has completed
1545 * for each network device that was in this network namespace.
1546 *
1547 * As such no additional rcu synchronization is necessary when
1548 * freeing the platform_label table.
1549 */
1550 rtnl_lock();
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001551 platform_label = rtnl_dereference(net->mpls.platform_label);
1552 platform_labels = net->mpls.platform_labels;
1553 for (index = 0; index < platform_labels; index++) {
1554 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1555 RCU_INIT_POINTER(platform_label[index], NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001556 mpls_rt_free(rt);
1557 }
1558 rtnl_unlock();
1559
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001560 kvfree(platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06001561}
1562
1563static struct pernet_operations mpls_net_ops = {
1564 .init = mpls_net_init,
1565 .exit = mpls_net_exit,
1566};
1567
1568static int __init mpls_init(void)
1569{
1570 int err;
1571
1572 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1573
1574 err = register_pernet_subsys(&mpls_net_ops);
1575 if (err)
1576 goto out;
1577
1578 err = register_netdevice_notifier(&mpls_dev_notifier);
1579 if (err)
1580 goto out_unregister_pernet;
1581
1582 dev_add_pack(&mpls_packet_type);
1583
Eric W. Biederman03c05662015-03-03 19:13:56 -06001584 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1585 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1586 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001587 err = 0;
1588out:
1589 return err;
1590
1591out_unregister_pernet:
1592 unregister_pernet_subsys(&mpls_net_ops);
1593 goto out;
1594}
1595module_init(mpls_init);
1596
1597static void __exit mpls_exit(void)
1598{
Eric W. Biederman03c05662015-03-03 19:13:56 -06001599 rtnl_unregister_all(PF_MPLS);
Eric W. Biederman01891972015-03-03 19:10:47 -06001600 dev_remove_pack(&mpls_packet_type);
1601 unregister_netdevice_notifier(&mpls_dev_notifier);
1602 unregister_pernet_subsys(&mpls_net_ops);
1603}
1604module_exit(mpls_exit);
1605
1606MODULE_DESCRIPTION("MultiProtocol Label Switching");
1607MODULE_LICENSE("GPL v2");
1608MODULE_ALIAS_NETPROTO(PF_MPLS);