blob: 726eafecc7935394d631e459031357fcd74eb79d [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>
David Ahern24045a02017-02-20 08:03:30 -080010#include <linux/netconf.h>
Stephen Rothwell4b5edb22015-03-05 13:37:05 +110011#include <linux/vmalloc.h>
Robert Shearman27d69102017-01-16 14:16:37 +000012#include <linux/percpu.h>
Eric W. Biederman01891972015-03-03 19:10:47 -060013#include <net/ip.h>
14#include <net/dst.h>
15#include <net/sock.h>
16#include <net/arp.h>
17#include <net/ip_fib.h>
18#include <net/netevent.h>
19#include <net/netns/generic.h>
Roopa Prabhubf215632015-07-30 13:34:54 -070020#if IS_ENABLED(CONFIG_IPV6)
21#include <net/ipv6.h>
Roopa Prabhubf215632015-07-30 13:34:54 -070022#endif
Robert Shearman27d69102017-01-16 14:16:37 +000023#include <net/addrconf.h>
Roopa Prabhuf8efb732015-10-23 06:03:27 -070024#include <net/nexthop.h>
Eric W. Biederman01891972015-03-03 19:10:47 -060025#include "internal.h"
26
David Aherndf1c6312017-03-31 07:14:02 -070027/* max memory we will use for mpls_route */
28#define MAX_MPLS_ROUTE_MEM 4096
29
Robert Shearman1c78efa2015-10-23 06:03:28 -070030/* Maximum number of labels to look ahead at when selecting a path of
31 * a multipath route
32 */
33#define MAX_MP_SELECT_LABELS 4
34
Robert Shearmaneb7809f2015-12-10 19:30:50 +000035#define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
36
Eric W. Biederman7720c012015-03-03 19:11:20 -060037static int zero = 0;
Robert Shearman5b441ac2017-03-10 20:43:24 +000038static int one = 1;
Eric W. Biederman7720c012015-03-03 19:11:20 -060039static int label_limit = (1 << 20) - 1;
Robert Shearmana59166e2017-03-10 20:43:25 +000040static int ttl_max = 255;
Eric W. Biederman7720c012015-03-03 19:11:20 -060041
Eric W. Biederman8de147d2015-03-03 19:14:31 -060042static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
43 struct nlmsghdr *nlh, struct net *net, u32 portid,
44 unsigned int nlm_flags);
45
Eric W. Biederman01891972015-03-03 19:10:47 -060046static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
47{
48 struct mpls_route *rt = NULL;
49
50 if (index < net->mpls.platform_labels) {
51 struct mpls_route __rcu **platform_label =
52 rcu_dereference(net->mpls.platform_label);
53 rt = rcu_dereference(platform_label[index]);
54 }
55 return rt;
56}
57
Roopa Prabhuface0182015-07-21 10:43:52 +020058bool mpls_output_possible(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060059{
60 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
61}
Roopa Prabhuface0182015-07-21 10:43:52 +020062EXPORT_SYMBOL_GPL(mpls_output_possible);
Eric W. Biederman01891972015-03-03 19:10:47 -060063
Robert Shearmancf4b24f2015-10-27 00:37:36 +000064static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
65{
David Ahern59b20962017-03-31 07:14:01 -070066 return (u8 *)nh + rt->rt_via_offset;
Robert Shearmancf4b24f2015-10-27 00:37:36 +000067}
68
69static const u8 *mpls_nh_via(const struct mpls_route *rt,
70 const struct mpls_nh *nh)
71{
72 return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
73}
74
Roopa Prabhuf8efb732015-10-23 06:03:27 -070075static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
Eric W. Biederman01891972015-03-03 19:10:47 -060076{
77 /* The size of the layer 2.5 labels to be added for this route */
Roopa Prabhuf8efb732015-10-23 06:03:27 -070078 return nh->nh_labels * sizeof(struct mpls_shim_hdr);
Eric W. Biederman01891972015-03-03 19:10:47 -060079}
80
Roopa Prabhuface0182015-07-21 10:43:52 +020081unsigned int mpls_dev_mtu(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060082{
83 /* The amount of data the layer 2 frame can hold */
84 return dev->mtu;
85}
Roopa Prabhuface0182015-07-21 10:43:52 +020086EXPORT_SYMBOL_GPL(mpls_dev_mtu);
Eric W. Biederman01891972015-03-03 19:10:47 -060087
Roopa Prabhuface0182015-07-21 10:43:52 +020088bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
Eric W. Biederman01891972015-03-03 19:10:47 -060089{
90 if (skb->len <= mtu)
91 return false;
92
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -030093 if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
Eric W. Biederman01891972015-03-03 19:10:47 -060094 return false;
95
96 return true;
97}
Roopa Prabhuface0182015-07-21 10:43:52 +020098EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
Eric W. Biederman01891972015-03-03 19:10:47 -060099
Robert Shearman27d69102017-01-16 14:16:37 +0000100void mpls_stats_inc_outucastpkts(struct net_device *dev,
101 const struct sk_buff *skb)
102{
103 struct mpls_dev *mdev;
104
105 if (skb->protocol == htons(ETH_P_MPLS_UC)) {
106 mdev = mpls_dev_get(dev);
107 if (mdev)
108 MPLS_INC_STATS_LEN(mdev, skb->len,
109 tx_packets,
110 tx_bytes);
111 } else if (skb->protocol == htons(ETH_P_IP)) {
112 IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
113#if IS_ENABLED(CONFIG_IPV6)
114 } else if (skb->protocol == htons(ETH_P_IPV6)) {
115 struct inet6_dev *in6dev = __in6_dev_get(dev);
116
117 if (in6dev)
118 IP6_UPD_PO_STATS(dev_net(dev), in6dev,
119 IPSTATS_MIB_OUT, skb->len);
120#endif
121 }
122}
123EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
124
David Ahern9f427a0e2017-01-20 12:58:34 -0800125static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700126{
Robert Shearman1c78efa2015-10-23 06:03:28 -0700127 struct mpls_entry_decoded dec;
David Ahern9f427a0e2017-01-20 12:58:34 -0800128 unsigned int mpls_hdr_len = 0;
Robert Shearman1c78efa2015-10-23 06:03:28 -0700129 struct mpls_shim_hdr *hdr;
130 bool eli_seen = false;
131 int label_index;
Robert Shearman1c78efa2015-10-23 06:03:28 -0700132 u32 hash = 0;
133
David Ahern9f427a0e2017-01-20 12:58:34 -0800134 for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
Robert Shearman1c78efa2015-10-23 06:03:28 -0700135 label_index++) {
David Ahern9f427a0e2017-01-20 12:58:34 -0800136 mpls_hdr_len += sizeof(*hdr);
137 if (!pskb_may_pull(skb, mpls_hdr_len))
Robert Shearman1c78efa2015-10-23 06:03:28 -0700138 break;
139
140 /* Read and decode the current label */
141 hdr = mpls_hdr(skb) + label_index;
142 dec = mpls_entry_decode(hdr);
143
144 /* RFC6790 - reserved labels MUST NOT be used as keys
145 * for the load-balancing function
146 */
147 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
148 hash = jhash_1word(dec.label, hash);
149
150 /* The entropy label follows the entropy label
151 * indicator, so this means that the entropy
152 * label was just added to the hash - no need to
153 * go any deeper either in the label stack or in the
154 * payload
155 */
156 if (eli_seen)
157 break;
158 } else if (dec.label == MPLS_LABEL_ENTROPY) {
159 eli_seen = true;
160 }
161
David Ahern9f427a0e2017-01-20 12:58:34 -0800162 if (!dec.bos)
163 continue;
164
165 /* found bottom label; does skb have room for a header? */
166 if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
Robert Shearman1c78efa2015-10-23 06:03:28 -0700167 const struct iphdr *v4hdr;
168
David Ahern9f427a0e2017-01-20 12:58:34 -0800169 v4hdr = (const struct iphdr *)(hdr + 1);
Robert Shearman1c78efa2015-10-23 06:03:28 -0700170 if (v4hdr->version == 4) {
171 hash = jhash_3words(ntohl(v4hdr->saddr),
172 ntohl(v4hdr->daddr),
173 v4hdr->protocol, hash);
174 } else if (v4hdr->version == 6 &&
David Ahern9f427a0e2017-01-20 12:58:34 -0800175 pskb_may_pull(skb, mpls_hdr_len +
176 sizeof(struct ipv6hdr))) {
Robert Shearman1c78efa2015-10-23 06:03:28 -0700177 const struct ipv6hdr *v6hdr;
178
David Ahern9f427a0e2017-01-20 12:58:34 -0800179 v6hdr = (const struct ipv6hdr *)(hdr + 1);
Robert Shearman1c78efa2015-10-23 06:03:28 -0700180 hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
181 hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
182 hash = jhash_1word(v6hdr->nexthdr, hash);
183 }
184 }
David Ahern9f427a0e2017-01-20 12:58:34 -0800185
186 break;
Robert Shearman1c78efa2015-10-23 06:03:28 -0700187 }
188
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800189 return hash;
190}
191
David Ahern59b20962017-03-31 07:14:01 -0700192static struct mpls_nh *mpls_get_nexthop(struct mpls_route *rt, u8 index)
193{
194 return (struct mpls_nh *)((u8 *)rt->rt_nh + index * rt->rt_nh_size);
195}
196
David Ahern39eb8cd2017-03-31 07:13:59 -0700197/* number of alive nexthops (rt->rt_nhn_alive) and the flags for
198 * a next hop (nh->nh_flags) are modified by netdev event handlers.
199 * Since those fields can change at any moment, use READ_ONCE to
200 * access both.
201 */
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800202static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
David Ahern9f427a0e2017-01-20 12:58:34 -0800203 struct sk_buff *skb)
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800204{
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800205 u32 hash = 0;
206 int nh_index = 0;
207 int n = 0;
David Ahern77ef013a2017-03-31 07:14:00 -0700208 u8 alive;
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800209
210 /* No need to look further into packet if there's only
211 * one path
212 */
213 if (rt->rt_nhn == 1)
David Ahern59b20962017-03-31 07:14:01 -0700214 return rt->rt_nh;
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800215
David Ahern39eb8cd2017-03-31 07:13:59 -0700216 alive = READ_ONCE(rt->rt_nhn_alive);
217 if (alive == 0)
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800218 return NULL;
219
David Ahern9f427a0e2017-01-20 12:58:34 -0800220 hash = mpls_multipath_hash(rt, skb);
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800221 nh_index = hash % alive;
222 if (alive == rt->rt_nhn)
223 goto out;
224 for_nexthops(rt) {
David Ahern39eb8cd2017-03-31 07:13:59 -0700225 unsigned int nh_flags = READ_ONCE(nh->nh_flags);
226
227 if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800228 continue;
229 if (n == nh_index)
230 return nh;
231 n++;
232 } endfor_nexthops(rt);
233
Robert Shearman1c78efa2015-10-23 06:03:28 -0700234out:
David Ahern59b20962017-03-31 07:14:01 -0700235 return mpls_get_nexthop(rt, nh_index);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700236}
237
Robert Shearman5b441ac2017-03-10 20:43:24 +0000238static bool mpls_egress(struct net *net, struct mpls_route *rt,
239 struct sk_buff *skb, struct mpls_entry_decoded dec)
Eric W. Biederman01891972015-03-03 19:10:47 -0600240{
Robert Shearman118d5232015-08-06 11:04:56 +0100241 enum mpls_payload_type payload_type;
242 bool success = false;
Eric W. Biederman01891972015-03-03 19:10:47 -0600243
Eric W. Biederman76fecd82015-03-12 18:22:59 -0500244 /* The IPv4 code below accesses through the IPv4 header
245 * checksum, which is 12 bytes into the packet.
246 * The IPv6 code below accesses through the IPv6 hop limit
247 * which is 8 bytes into the packet.
248 *
249 * For all supported cases there should always be at least 12
250 * bytes of packet data present. The IPv4 header is 20 bytes
251 * without options and the IPv6 header is always 40 bytes
252 * long.
253 */
254 if (!pskb_may_pull(skb, 12))
255 return false;
256
Robert Shearman118d5232015-08-06 11:04:56 +0100257 payload_type = rt->rt_payload_type;
258 if (payload_type == MPT_UNSPEC)
259 payload_type = ip_hdr(skb)->version;
260
261 switch (payload_type) {
262 case MPT_IPV4: {
263 struct iphdr *hdr4 = ip_hdr(skb);
Robert Shearman5b441ac2017-03-10 20:43:24 +0000264 u8 new_ttl;
Eric W. Biederman01891972015-03-03 19:10:47 -0600265 skb->protocol = htons(ETH_P_IP);
Robert Shearman5b441ac2017-03-10 20:43:24 +0000266
267 /* If propagating TTL, take the decremented TTL from
268 * the incoming MPLS header, otherwise decrement the
269 * TTL, but only if not 0 to avoid underflow.
270 */
271 if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
272 (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
273 net->mpls.ip_ttl_propagate))
274 new_ttl = dec.ttl;
275 else
276 new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
277
Eric W. Biederman01891972015-03-03 19:10:47 -0600278 csum_replace2(&hdr4->check,
279 htons(hdr4->ttl << 8),
Robert Shearman5b441ac2017-03-10 20:43:24 +0000280 htons(new_ttl << 8));
281 hdr4->ttl = new_ttl;
Robert Shearman118d5232015-08-06 11:04:56 +0100282 success = true;
283 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600284 }
Robert Shearman118d5232015-08-06 11:04:56 +0100285 case MPT_IPV6: {
Eric W. Biederman01891972015-03-03 19:10:47 -0600286 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
287 skb->protocol = htons(ETH_P_IPV6);
Robert Shearman5b441ac2017-03-10 20:43:24 +0000288
289 /* If propagating TTL, take the decremented TTL from
290 * the incoming MPLS header, otherwise decrement the
291 * hop limit, but only if not 0 to avoid underflow.
292 */
293 if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
294 (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
295 net->mpls.ip_ttl_propagate))
296 hdr6->hop_limit = dec.ttl;
297 else if (hdr6->hop_limit)
298 hdr6->hop_limit = hdr6->hop_limit - 1;
Robert Shearman118d5232015-08-06 11:04:56 +0100299 success = true;
300 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600301 }
Robert Shearman118d5232015-08-06 11:04:56 +0100302 case MPT_UNSPEC:
Robert Shearman5b441ac2017-03-10 20:43:24 +0000303 /* Should have decided which protocol it is by now */
Robert Shearman118d5232015-08-06 11:04:56 +0100304 break;
305 }
306
Eric W. Biederman01891972015-03-03 19:10:47 -0600307 return success;
308}
309
310static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
311 struct packet_type *pt, struct net_device *orig_dev)
312{
313 struct net *net = dev_net(dev);
314 struct mpls_shim_hdr *hdr;
315 struct mpls_route *rt;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700316 struct mpls_nh *nh;
Eric W. Biederman01891972015-03-03 19:10:47 -0600317 struct mpls_entry_decoded dec;
318 struct net_device *out_dev;
Robert Shearman27d69102017-01-16 14:16:37 +0000319 struct mpls_dev *out_mdev;
Robert Shearman03c57742015-04-22 11:14:37 +0100320 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600321 unsigned int hh_len;
322 unsigned int new_header_size;
323 unsigned int mtu;
324 int err;
325
326 /* Careful this entire function runs inside of an rcu critical section */
327
Robert Shearman03c57742015-04-22 11:14:37 +0100328 mdev = mpls_dev_get(dev);
Robert Shearman27d69102017-01-16 14:16:37 +0000329 if (!mdev)
Robert Shearman03c57742015-04-22 11:14:37 +0100330 goto drop;
331
Robert Shearman27d69102017-01-16 14:16:37 +0000332 MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
333 rx_bytes);
334
335 if (!mdev->input_enabled) {
336 MPLS_INC_STATS(mdev, rx_dropped);
337 goto drop;
338 }
339
Eric W. Biederman01891972015-03-03 19:10:47 -0600340 if (skb->pkt_type != PACKET_HOST)
Robert Shearman27d69102017-01-16 14:16:37 +0000341 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600342
343 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
Robert Shearman27d69102017-01-16 14:16:37 +0000344 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600345
346 if (!pskb_may_pull(skb, sizeof(*hdr)))
Robert Shearman27d69102017-01-16 14:16:37 +0000347 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600348
349 /* Read and decode the label */
350 hdr = mpls_hdr(skb);
351 dec = mpls_entry_decode(hdr);
352
Eric W. Biederman01891972015-03-03 19:10:47 -0600353 rt = mpls_route_input_rcu(net, dec.label);
Robert Shearman27d69102017-01-16 14:16:37 +0000354 if (!rt) {
355 MPLS_INC_STATS(mdev, rx_noroute);
Eric W. Biederman01891972015-03-03 19:10:47 -0600356 goto drop;
Robert Shearman27d69102017-01-16 14:16:37 +0000357 }
Eric W. Biederman01891972015-03-03 19:10:47 -0600358
David Ahern9f427a0e2017-01-20 12:58:34 -0800359 nh = mpls_select_multipath(rt, skb);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700360 if (!nh)
Robert Shearman27d69102017-01-16 14:16:37 +0000361 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600362
David Ahern9f427a0e2017-01-20 12:58:34 -0800363 /* Pop the label */
364 skb_pull(skb, sizeof(*hdr));
365 skb_reset_network_header(skb);
366
367 skb_orphan(skb);
368
Eric W. Biederman01891972015-03-03 19:10:47 -0600369 if (skb_warn_if_lro(skb))
Robert Shearman27d69102017-01-16 14:16:37 +0000370 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600371
372 skb_forward_csum(skb);
373
374 /* Verify ttl is valid */
Eric W. Biedermanaa7da932015-03-07 16:23:23 -0600375 if (dec.ttl <= 1)
Robert Shearman27d69102017-01-16 14:16:37 +0000376 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600377 dec.ttl -= 1;
378
Robert Shearman27d69102017-01-16 14:16:37 +0000379 /* Find the output device */
380 out_dev = rcu_dereference(nh->nh_dev);
381 if (!mpls_output_possible(out_dev))
382 goto tx_err;
383
Eric W. Biederman01891972015-03-03 19:10:47 -0600384 /* Verify the destination can hold the packet */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700385 new_header_size = mpls_nh_header_size(nh);
Eric W. Biederman01891972015-03-03 19:10:47 -0600386 mtu = mpls_dev_mtu(out_dev);
387 if (mpls_pkt_too_big(skb, mtu - new_header_size))
Robert Shearman27d69102017-01-16 14:16:37 +0000388 goto tx_err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600389
390 hh_len = LL_RESERVED_SPACE(out_dev);
391 if (!out_dev->header_ops)
392 hh_len = 0;
393
394 /* Ensure there is enough space for the headers in the skb */
395 if (skb_cow(skb, hh_len + new_header_size))
Robert Shearman27d69102017-01-16 14:16:37 +0000396 goto tx_err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600397
398 skb->dev = out_dev;
399 skb->protocol = htons(ETH_P_MPLS_UC);
400
401 if (unlikely(!new_header_size && dec.bos)) {
402 /* Penultimate hop popping */
Robert Shearman5b441ac2017-03-10 20:43:24 +0000403 if (!mpls_egress(dev_net(out_dev), rt, skb, dec))
Robert Shearman27d69102017-01-16 14:16:37 +0000404 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600405 } else {
406 bool bos;
407 int i;
408 skb_push(skb, new_header_size);
409 skb_reset_network_header(skb);
410 /* Push the new labels */
411 hdr = mpls_hdr(skb);
412 bos = dec.bos;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700413 for (i = nh->nh_labels - 1; i >= 0; i--) {
414 hdr[i] = mpls_entry_encode(nh->nh_label[i],
415 dec.ttl, 0, bos);
Eric W. Biederman01891972015-03-03 19:10:47 -0600416 bos = false;
417 }
418 }
419
Robert Shearman27d69102017-01-16 14:16:37 +0000420 mpls_stats_inc_outucastpkts(out_dev, skb);
421
Robert Shearmaneb7809f2015-12-10 19:30:50 +0000422 /* If via wasn't specified then send out using device address */
423 if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
424 err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
425 out_dev->dev_addr, skb);
426 else
427 err = neigh_xmit(nh->nh_via_table, out_dev,
428 mpls_nh_via(rt, nh), skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600429 if (err)
430 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
431 __func__, err);
432 return 0;
433
Robert Shearman27d69102017-01-16 14:16:37 +0000434tx_err:
435 out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
436 if (out_mdev)
437 MPLS_INC_STATS(out_mdev, tx_errors);
438 goto drop;
439err:
440 MPLS_INC_STATS(mdev, rx_errors);
Eric W. Biederman01891972015-03-03 19:10:47 -0600441drop:
442 kfree_skb(skb);
443 return NET_RX_DROP;
444}
445
446static struct packet_type mpls_packet_type __read_mostly = {
447 .type = cpu_to_be16(ETH_P_MPLS_UC),
448 .func = mpls_forward,
449};
450
Wu Fengguangf0126532015-03-05 05:33:54 +0800451static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600452 [RTA_DST] = { .type = NLA_U32 },
453 [RTA_OIF] = { .type = NLA_U32 },
Robert Shearman5b441ac2017-03-10 20:43:24 +0000454 [RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
Eric W. Biederman03c05662015-03-03 19:13:56 -0600455};
456
Eric W. Biedermana2519922015-03-03 19:12:40 -0600457struct mpls_route_config {
Robert Shearman118d5232015-08-06 11:04:56 +0100458 u32 rc_protocol;
459 u32 rc_ifindex;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700460 u8 rc_via_table;
461 u8 rc_via_alen;
Robert Shearman118d5232015-08-06 11:04:56 +0100462 u8 rc_via[MAX_VIA_ALEN];
463 u32 rc_label;
Robert Shearman5b441ac2017-03-10 20:43:24 +0000464 u8 rc_ttl_propagate;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700465 u8 rc_output_labels;
Robert Shearman118d5232015-08-06 11:04:56 +0100466 u32 rc_output_label[MAX_NEW_LABELS];
467 u32 rc_nlflags;
468 enum mpls_payload_type rc_payload_type;
469 struct nl_info rc_nlinfo;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700470 struct rtnexthop *rc_mp;
471 int rc_mp_len;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600472};
473
David Ahern59b20962017-03-31 07:14:01 -0700474/* all nexthops within a route have the same size based on max
475 * number of labels and max via length for a hop
476 */
477static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels)
Eric W. Biederman01891972015-03-03 19:10:47 -0600478{
David Ahern59b20962017-03-31 07:14:01 -0700479 u8 nh_size = MPLS_NH_SIZE(max_labels, max_alen);
Eric W. Biederman01891972015-03-03 19:10:47 -0600480 struct mpls_route *rt;
David Aherndf1c6312017-03-31 07:14:02 -0700481 size_t size;
Eric W. Biederman01891972015-03-03 19:10:47 -0600482
David Aherndf1c6312017-03-31 07:14:02 -0700483 size = sizeof(*rt) + num_nh * nh_size;
484 if (size > MAX_MPLS_ROUTE_MEM)
485 return ERR_PTR(-EINVAL);
486
487 rt = kzalloc(size, GFP_KERNEL);
488 if (!rt)
489 return ERR_PTR(-ENOMEM);
490
491 rt->rt_nhn = num_nh;
492 rt->rt_nhn_alive = num_nh;
493 rt->rt_nh_size = nh_size;
494 rt->rt_via_offset = MPLS_NH_VIA_OFF(max_labels);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700495
Eric W. Biederman01891972015-03-03 19:10:47 -0600496 return rt;
497}
498
499static void mpls_rt_free(struct mpls_route *rt)
500{
501 if (rt)
502 kfree_rcu(rt, rt_rcu);
503}
504
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600505static void mpls_notify_route(struct net *net, unsigned index,
506 struct mpls_route *old, struct mpls_route *new,
507 const struct nl_info *info)
508{
509 struct nlmsghdr *nlh = info ? info->nlh : NULL;
510 unsigned portid = info ? info->portid : 0;
511 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
512 struct mpls_route *rt = new ? new : old;
513 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
514 /* Ignore reserved labels for now */
Robert Shearmana6affd22015-08-03 17:50:04 +0100515 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600516 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
517}
518
Eric W. Biederman01891972015-03-03 19:10:47 -0600519static void mpls_route_update(struct net *net, unsigned index,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700520 struct mpls_route *new,
Eric W. Biederman01891972015-03-03 19:10:47 -0600521 const struct nl_info *info)
522{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600523 struct mpls_route __rcu **platform_label;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700524 struct mpls_route *rt;
Eric W. Biederman01891972015-03-03 19:10:47 -0600525
526 ASSERT_RTNL();
527
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600528 platform_label = rtnl_dereference(net->mpls.platform_label);
529 rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700530 rcu_assign_pointer(platform_label[index], new);
Eric W. Biederman01891972015-03-03 19:10:47 -0600531
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700532 mpls_notify_route(net, index, rt, new, info);
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600533
Eric W. Biederman01891972015-03-03 19:10:47 -0600534 /* If we removed a route free it now */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700535 mpls_rt_free(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -0600536}
537
Eric W. Biedermana2519922015-03-03 19:12:40 -0600538static unsigned find_free_label(struct net *net)
539{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600540 struct mpls_route __rcu **platform_label;
541 size_t platform_labels;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600542 unsigned index;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600543
544 platform_label = rtnl_dereference(net->mpls.platform_label);
545 platform_labels = net->mpls.platform_labels;
Robert Shearmana6affd22015-08-03 17:50:04 +0100546 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
547 index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600548 if (!rtnl_dereference(platform_label[index]))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600549 return index;
550 }
551 return LABEL_NOT_SPECIFIED;
552}
553
Roopa Prabhubf215632015-07-30 13:34:54 -0700554#if IS_ENABLED(CONFIG_INET)
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000555static struct net_device *inet_fib_lookup_dev(struct net *net,
556 const void *addr)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700557{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300558 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700559 struct rtable *rt;
560 struct in_addr daddr;
561
562 memcpy(&daddr, addr, sizeof(struct in_addr));
563 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
564 if (IS_ERR(rt))
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300565 return ERR_CAST(rt);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700566
567 dev = rt->dst.dev;
568 dev_hold(dev);
569
570 ip_rt_put(rt);
571
Roopa Prabhu01faef22015-07-21 09:16:24 -0700572 return dev;
573}
Roopa Prabhubf215632015-07-30 13:34:54 -0700574#else
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000575static struct net_device *inet_fib_lookup_dev(struct net *net,
576 const void *addr)
Roopa Prabhubf215632015-07-30 13:34:54 -0700577{
578 return ERR_PTR(-EAFNOSUPPORT);
579}
580#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700581
Roopa Prabhubf215632015-07-30 13:34:54 -0700582#if IS_ENABLED(CONFIG_IPV6)
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000583static struct net_device *inet6_fib_lookup_dev(struct net *net,
584 const void *addr)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700585{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300586 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700587 struct dst_entry *dst;
588 struct flowi6 fl6;
Roopa Prabhubf215632015-07-30 13:34:54 -0700589 int err;
590
591 if (!ipv6_stub)
592 return ERR_PTR(-EAFNOSUPPORT);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700593
594 memset(&fl6, 0, sizeof(fl6));
595 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
Roopa Prabhubf215632015-07-30 13:34:54 -0700596 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
597 if (err)
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300598 return ERR_PTR(err);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700599
600 dev = dst->dev;
601 dev_hold(dev);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700602 dst_release(dst);
603
604 return dev;
605}
Roopa Prabhubf215632015-07-30 13:34:54 -0700606#else
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000607static struct net_device *inet6_fib_lookup_dev(struct net *net,
608 const void *addr)
Roopa Prabhubf215632015-07-30 13:34:54 -0700609{
610 return ERR_PTR(-EAFNOSUPPORT);
611}
612#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700613
614static struct net_device *find_outdev(struct net *net,
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000615 struct mpls_route *rt,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700616 struct mpls_nh *nh, int oif)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700617{
618 struct net_device *dev = NULL;
619
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700620 if (!oif) {
621 switch (nh->nh_via_table) {
Roopa Prabhu01faef22015-07-21 09:16:24 -0700622 case NEIGH_ARP_TABLE:
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000623 dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
Roopa Prabhu01faef22015-07-21 09:16:24 -0700624 break;
625 case NEIGH_ND_TABLE:
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000626 dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
Roopa Prabhu01faef22015-07-21 09:16:24 -0700627 break;
628 case NEIGH_LINK_TABLE:
629 break;
630 }
631 } else {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700632 dev = dev_get_by_index(net, oif);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700633 }
634
Roopa Prabhu3dcb6152015-08-04 06:36:24 -0700635 if (!dev)
636 return ERR_PTR(-ENODEV);
637
Roopa Prabhu94a57f12016-04-07 21:28:38 -0700638 if (IS_ERR(dev))
639 return dev;
640
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700641 /* The caller is holding rtnl anyways, so release the dev reference */
642 dev_put(dev);
643
Roopa Prabhu01faef22015-07-21 09:16:24 -0700644 return dev;
645}
646
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000647static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
648 struct mpls_nh *nh, int oif)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700649{
650 struct net_device *dev = NULL;
651 int err = -ENODEV;
652
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000653 dev = find_outdev(net, rt, nh, oif);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700654 if (IS_ERR(dev)) {
655 err = PTR_ERR(dev);
656 dev = NULL;
657 goto errout;
658 }
659
660 /* Ensure this is a supported device */
661 err = -EINVAL;
662 if (!mpls_dev_get(dev))
663 goto errout;
664
Robert Shearmana3e948e2015-12-10 19:30:48 +0000665 if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
666 (dev->addr_len != nh->nh_via_alen))
667 goto errout;
668
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700669 RCU_INIT_POINTER(nh->nh_dev, dev);
670
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800671 if (!(dev->flags & IFF_UP)) {
672 nh->nh_flags |= RTNH_F_DEAD;
673 } else {
674 unsigned int flags;
675
676 flags = dev_get_flags(dev);
677 if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
678 nh->nh_flags |= RTNH_F_LINKDOWN;
679 }
680
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700681 return 0;
682
683errout:
684 return err;
685}
686
687static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
688 struct mpls_route *rt)
689{
690 struct net *net = cfg->rc_nlinfo.nl_net;
691 struct mpls_nh *nh = rt->rt_nh;
692 int err;
693 int i;
694
695 if (!nh)
696 return -ENOMEM;
697
698 err = -EINVAL;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700699
700 nh->nh_labels = cfg->rc_output_labels;
701 for (i = 0; i < nh->nh_labels; i++)
702 nh->nh_label[i] = cfg->rc_output_label[i];
703
704 nh->nh_via_table = cfg->rc_via_table;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000705 memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700706 nh->nh_via_alen = cfg->rc_via_alen;
707
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000708 err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700709 if (err)
710 goto errout;
711
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800712 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
713 rt->rt_nhn_alive--;
714
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700715 return 0;
716
717errout:
718 return err;
719}
720
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000721static int mpls_nh_build(struct net *net, struct mpls_route *rt,
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800722 struct mpls_nh *nh, int oif, struct nlattr *via,
David Aherna4ac8c92017-03-31 07:14:03 -0700723 struct nlattr *newdst, u8 max_labels)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700724{
725 int err = -ENOMEM;
726
727 if (!nh)
728 goto errout;
729
730 if (newdst) {
David Aherna1f10ab2017-05-27 16:19:29 -0600731 err = nla_get_labels(newdst, max_labels, &nh->nh_labels,
732 nh->nh_label, NULL);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700733 if (err)
734 goto errout;
735 }
736
Robert Shearmanf20367d2015-12-10 19:30:51 +0000737 if (via) {
738 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
739 __mpls_nh_via(rt, nh));
740 if (err)
741 goto errout;
742 } else {
743 nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
744 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700745
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000746 err = mpls_nh_assign_dev(net, rt, nh, oif);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700747 if (err)
748 goto errout;
749
750 return 0;
751
752errout:
753 return err;
754}
755
David Ahern77ef013a2017-03-31 07:14:00 -0700756static u8 mpls_count_nexthops(struct rtnexthop *rtnh, int len,
David Aherna4ac8c92017-03-31 07:14:03 -0700757 u8 cfg_via_alen, u8 *max_via_alen,
758 u8 *max_labels)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700759{
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700760 int remaining = len;
David Ahern77ef013a2017-03-31 07:14:00 -0700761 u8 nhs = 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700762
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000763 *max_via_alen = 0;
David Aherna4ac8c92017-03-31 07:14:03 -0700764 *max_labels = 0;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000765
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700766 while (rtnh_ok(rtnh, remaining)) {
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000767 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
768 int attrlen;
David Aherna4ac8c92017-03-31 07:14:03 -0700769 u8 n_labels = 0;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000770
771 attrlen = rtnh_attrlen(rtnh);
772 nla = nla_find(attrs, attrlen, RTA_VIA);
773 if (nla && nla_len(nla) >=
774 offsetof(struct rtvia, rtvia_addr)) {
775 int via_alen = nla_len(nla) -
776 offsetof(struct rtvia, rtvia_addr);
777
778 if (via_alen <= MAX_VIA_ALEN)
779 *max_via_alen = max_t(u16, *max_via_alen,
780 via_alen);
781 }
782
David Aherna4ac8c92017-03-31 07:14:03 -0700783 nla = nla_find(attrs, attrlen, RTA_NEWDST);
784 if (nla &&
David Aherna1f10ab2017-05-27 16:19:29 -0600785 nla_get_labels(nla, MAX_NEW_LABELS, &n_labels,
786 NULL, NULL) != 0)
David Aherna4ac8c92017-03-31 07:14:03 -0700787 return 0;
788
789 *max_labels = max_t(u8, *max_labels, n_labels);
790
David Ahern77ef013a2017-03-31 07:14:00 -0700791 /* number of nexthops is tracked by a u8.
792 * Check for overflow.
793 */
794 if (nhs == 255)
795 return 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700796 nhs++;
David Ahern77ef013a2017-03-31 07:14:00 -0700797
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700798 rtnh = rtnh_next(rtnh, &remaining);
799 }
800
801 /* leftover implies invalid nexthop configuration, discard it */
802 return remaining > 0 ? 0 : nhs;
803}
804
805static int mpls_nh_build_multi(struct mpls_route_config *cfg,
David Aherna4ac8c92017-03-31 07:14:03 -0700806 struct mpls_route *rt, u8 max_labels)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700807{
808 struct rtnexthop *rtnh = cfg->rc_mp;
809 struct nlattr *nla_via, *nla_newdst;
810 int remaining = cfg->rc_mp_len;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700811 int err = 0;
David Ahern77ef013a2017-03-31 07:14:00 -0700812 u8 nhs = 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700813
814 change_nexthops(rt) {
815 int attrlen;
816
817 nla_via = NULL;
818 nla_newdst = NULL;
819
820 err = -EINVAL;
821 if (!rtnh_ok(rtnh, remaining))
822 goto errout;
823
Robert Shearman1c78efa2015-10-23 06:03:28 -0700824 /* neither weighted multipath nor any flags
825 * are supported
826 */
827 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
828 goto errout;
829
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700830 attrlen = rtnh_attrlen(rtnh);
831 if (attrlen > 0) {
832 struct nlattr *attrs = rtnh_attrs(rtnh);
833
834 nla_via = nla_find(attrs, attrlen, RTA_VIA);
835 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
836 }
837
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000838 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
David Aherna4ac8c92017-03-31 07:14:03 -0700839 rtnh->rtnh_ifindex, nla_via, nla_newdst,
840 max_labels);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700841 if (err)
842 goto errout;
843
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800844 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
845 rt->rt_nhn_alive--;
846
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700847 rtnh = rtnh_next(rtnh, &remaining);
848 nhs++;
849 } endfor_nexthops(rt);
850
851 rt->rt_nhn = nhs;
852
853 return 0;
854
855errout:
856 return err;
857}
858
David Ahernb7b386f2017-05-27 16:19:30 -0600859static bool mpls_label_ok(struct net *net, unsigned int index)
860{
861 /* Reserved labels may not be set */
862 if (index < MPLS_LABEL_FIRST_UNRESERVED)
863 return false;
864
865 /* The full 20 bit range may not be supported. */
866 if (index >= net->mpls.platform_labels)
867 return false;
868
869 return true;
870}
871
Eric W. Biedermana2519922015-03-03 19:12:40 -0600872static int mpls_route_add(struct mpls_route_config *cfg)
873{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600874 struct mpls_route __rcu **platform_label;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600875 struct net *net = cfg->rc_nlinfo.nl_net;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600876 struct mpls_route *rt, *old;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600877 int err = -EINVAL;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000878 u8 max_via_alen;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700879 unsigned index;
David Aherna4ac8c92017-03-31 07:14:03 -0700880 u8 max_labels;
David Ahern77ef013a2017-03-31 07:14:00 -0700881 u8 nhs;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600882
883 index = cfg->rc_label;
884
885 /* If a label was not specified during insert pick one */
886 if ((index == LABEL_NOT_SPECIFIED) &&
887 (cfg->rc_nlflags & NLM_F_CREATE)) {
888 index = find_free_label(net);
889 }
890
David Ahernb7b386f2017-05-27 16:19:30 -0600891 if (!mpls_label_ok(net, index))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600892 goto errout;
893
Eric W. Biedermana2519922015-03-03 19:12:40 -0600894 /* Append makes no sense with mpls */
Eric W. Biederman0f7bbd52015-03-07 16:22:40 -0600895 err = -EOPNOTSUPP;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600896 if (cfg->rc_nlflags & NLM_F_APPEND)
897 goto errout;
898
899 err = -EEXIST;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600900 platform_label = rtnl_dereference(net->mpls.platform_label);
901 old = rtnl_dereference(platform_label[index]);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600902 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
903 goto errout;
904
905 err = -EEXIST;
906 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
907 goto errout;
908
909 err = -ENOENT;
910 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
911 goto errout;
912
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000913 err = -EINVAL;
David Aherna4ac8c92017-03-31 07:14:03 -0700914 if (cfg->rc_mp) {
915 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
916 cfg->rc_via_alen, &max_via_alen,
917 &max_labels);
918 } else {
919 max_via_alen = cfg->rc_via_alen;
920 max_labels = cfg->rc_output_labels;
921 nhs = 1;
922 }
923
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000924 if (nhs == 0)
925 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700926
Eric W. Biedermana2519922015-03-03 19:12:40 -0600927 err = -ENOMEM;
David Aherna4ac8c92017-03-31 07:14:03 -0700928 rt = mpls_rt_alloc(nhs, max_via_alen, max_labels);
David Aherndf1c6312017-03-31 07:14:02 -0700929 if (IS_ERR(rt)) {
930 err = PTR_ERR(rt);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600931 goto errout;
David Aherndf1c6312017-03-31 07:14:02 -0700932 }
Eric W. Biedermana2519922015-03-03 19:12:40 -0600933
Eric W. Biedermana2519922015-03-03 19:12:40 -0600934 rt->rt_protocol = cfg->rc_protocol;
Robert Shearman118d5232015-08-06 11:04:56 +0100935 rt->rt_payload_type = cfg->rc_payload_type;
Robert Shearman5b441ac2017-03-10 20:43:24 +0000936 rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600937
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700938 if (cfg->rc_mp)
David Aherna4ac8c92017-03-31 07:14:03 -0700939 err = mpls_nh_build_multi(cfg, rt, max_labels);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700940 else
941 err = mpls_nh_build_from_cfg(cfg, rt);
942 if (err)
943 goto freert;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600944
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700945 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
946
Eric W. Biedermana2519922015-03-03 19:12:40 -0600947 return 0;
948
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700949freert:
950 mpls_rt_free(rt);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600951errout:
Eric W. Biedermana2519922015-03-03 19:12:40 -0600952 return err;
953}
954
955static int mpls_route_del(struct mpls_route_config *cfg)
956{
957 struct net *net = cfg->rc_nlinfo.nl_net;
958 unsigned index;
959 int err = -EINVAL;
960
961 index = cfg->rc_label;
962
David Ahernb7b386f2017-05-27 16:19:30 -0600963 if (!mpls_label_ok(net, index))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600964 goto errout;
965
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700966 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600967
968 err = 0;
969errout:
970 return err;
971}
972
Robert Shearman27d69102017-01-16 14:16:37 +0000973static void mpls_get_stats(struct mpls_dev *mdev,
974 struct mpls_link_stats *stats)
975{
976 struct mpls_pcpu_stats *p;
977 int i;
978
979 memset(stats, 0, sizeof(*stats));
980
981 for_each_possible_cpu(i) {
982 struct mpls_link_stats local;
983 unsigned int start;
984
985 p = per_cpu_ptr(mdev->stats, i);
986 do {
987 start = u64_stats_fetch_begin(&p->syncp);
988 local = p->stats;
989 } while (u64_stats_fetch_retry(&p->syncp, start));
990
991 stats->rx_packets += local.rx_packets;
992 stats->rx_bytes += local.rx_bytes;
993 stats->tx_packets += local.tx_packets;
994 stats->tx_bytes += local.tx_bytes;
995 stats->rx_errors += local.rx_errors;
996 stats->tx_errors += local.tx_errors;
997 stats->rx_dropped += local.rx_dropped;
998 stats->tx_dropped += local.tx_dropped;
999 stats->rx_noroute += local.rx_noroute;
1000 }
1001}
1002
1003static int mpls_fill_stats_af(struct sk_buff *skb,
1004 const struct net_device *dev)
1005{
1006 struct mpls_link_stats *stats;
1007 struct mpls_dev *mdev;
1008 struct nlattr *nla;
1009
1010 mdev = mpls_dev_get(dev);
1011 if (!mdev)
1012 return -ENODATA;
1013
1014 nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
1015 sizeof(struct mpls_link_stats),
1016 MPLS_STATS_UNSPEC);
1017 if (!nla)
1018 return -EMSGSIZE;
1019
1020 stats = nla_data(nla);
1021 mpls_get_stats(mdev, stats);
1022
1023 return 0;
1024}
1025
1026static size_t mpls_get_stats_af_size(const struct net_device *dev)
1027{
1028 struct mpls_dev *mdev;
1029
1030 mdev = mpls_dev_get(dev);
1031 if (!mdev)
1032 return 0;
1033
1034 return nla_total_size_64bit(sizeof(struct mpls_link_stats));
1035}
1036
David Ahern24045a02017-02-20 08:03:30 -08001037static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
1038 u32 portid, u32 seq, int event,
1039 unsigned int flags, int type)
1040{
1041 struct nlmsghdr *nlh;
1042 struct netconfmsg *ncm;
1043 bool all = false;
1044
1045 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1046 flags);
1047 if (!nlh)
1048 return -EMSGSIZE;
1049
1050 if (type == NETCONFA_ALL)
1051 all = true;
1052
1053 ncm = nlmsg_data(nlh);
1054 ncm->ncm_family = AF_MPLS;
1055
1056 if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
1057 goto nla_put_failure;
1058
1059 if ((all || type == NETCONFA_INPUT) &&
1060 nla_put_s32(skb, NETCONFA_INPUT,
1061 mdev->input_enabled) < 0)
1062 goto nla_put_failure;
1063
1064 nlmsg_end(skb, nlh);
1065 return 0;
1066
1067nla_put_failure:
1068 nlmsg_cancel(skb, nlh);
1069 return -EMSGSIZE;
1070}
1071
1072static int mpls_netconf_msgsize_devconf(int type)
1073{
1074 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1075 + nla_total_size(4); /* NETCONFA_IFINDEX */
1076 bool all = false;
1077
1078 if (type == NETCONFA_ALL)
1079 all = true;
1080
1081 if (all || type == NETCONFA_INPUT)
1082 size += nla_total_size(4);
1083
1084 return size;
1085}
1086
David Ahern823566a2017-03-28 14:28:06 -07001087static void mpls_netconf_notify_devconf(struct net *net, int event,
1088 int type, struct mpls_dev *mdev)
David Ahern24045a02017-02-20 08:03:30 -08001089{
1090 struct sk_buff *skb;
1091 int err = -ENOBUFS;
1092
1093 skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
1094 if (!skb)
1095 goto errout;
1096
David Ahern823566a2017-03-28 14:28:06 -07001097 err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type);
David Ahern24045a02017-02-20 08:03:30 -08001098 if (err < 0) {
1099 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1100 WARN_ON(err == -EMSGSIZE);
1101 kfree_skb(skb);
1102 goto errout;
1103 }
1104
1105 rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
1106 return;
1107errout:
1108 if (err < 0)
1109 rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
1110}
1111
1112static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
1113 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
1114};
1115
1116static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
David Ahernc21ef3e2017-04-16 09:48:24 -07001117 struct nlmsghdr *nlh,
1118 struct netlink_ext_ack *extack)
David Ahern24045a02017-02-20 08:03:30 -08001119{
1120 struct net *net = sock_net(in_skb->sk);
1121 struct nlattr *tb[NETCONFA_MAX + 1];
1122 struct netconfmsg *ncm;
1123 struct net_device *dev;
1124 struct mpls_dev *mdev;
1125 struct sk_buff *skb;
1126 int ifindex;
1127 int err;
1128
1129 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02001130 devconf_mpls_policy, NULL);
David Ahern24045a02017-02-20 08:03:30 -08001131 if (err < 0)
1132 goto errout;
1133
1134 err = -EINVAL;
1135 if (!tb[NETCONFA_IFINDEX])
1136 goto errout;
1137
1138 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
1139 dev = __dev_get_by_index(net, ifindex);
1140 if (!dev)
1141 goto errout;
1142
1143 mdev = mpls_dev_get(dev);
1144 if (!mdev)
1145 goto errout;
1146
1147 err = -ENOBUFS;
1148 skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
1149 if (!skb)
1150 goto errout;
1151
1152 err = mpls_netconf_fill_devconf(skb, mdev,
1153 NETLINK_CB(in_skb).portid,
1154 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
1155 NETCONFA_ALL);
1156 if (err < 0) {
1157 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1158 WARN_ON(err == -EMSGSIZE);
1159 kfree_skb(skb);
1160 goto errout;
1161 }
1162 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1163errout:
1164 return err;
1165}
1166
1167static int mpls_netconf_dump_devconf(struct sk_buff *skb,
1168 struct netlink_callback *cb)
1169{
1170 struct net *net = sock_net(skb->sk);
1171 struct hlist_head *head;
1172 struct net_device *dev;
1173 struct mpls_dev *mdev;
1174 int idx, s_idx;
1175 int h, s_h;
1176
1177 s_h = cb->args[0];
1178 s_idx = idx = cb->args[1];
1179
1180 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1181 idx = 0;
1182 head = &net->dev_index_head[h];
1183 rcu_read_lock();
1184 cb->seq = net->dev_base_seq;
1185 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1186 if (idx < s_idx)
1187 goto cont;
1188 mdev = mpls_dev_get(dev);
1189 if (!mdev)
1190 goto cont;
1191 if (mpls_netconf_fill_devconf(skb, mdev,
1192 NETLINK_CB(cb->skb).portid,
1193 cb->nlh->nlmsg_seq,
1194 RTM_NEWNETCONF,
1195 NLM_F_MULTI,
1196 NETCONFA_ALL) < 0) {
1197 rcu_read_unlock();
1198 goto done;
1199 }
1200 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1201cont:
1202 idx++;
1203 }
1204 rcu_read_unlock();
1205 }
1206done:
1207 cb->args[0] = h;
1208 cb->args[1] = idx;
1209
1210 return skb->len;
1211}
1212
Robert Shearman37bde792015-04-22 11:14:38 +01001213#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
1214 (&((struct mpls_dev *)0)->field)
1215
David Ahern24045a02017-02-20 08:03:30 -08001216static int mpls_conf_proc(struct ctl_table *ctl, int write,
1217 void __user *buffer,
1218 size_t *lenp, loff_t *ppos)
1219{
1220 int oval = *(int *)ctl->data;
1221 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1222
1223 if (write) {
1224 struct mpls_dev *mdev = ctl->extra1;
1225 int i = (int *)ctl->data - (int *)mdev;
1226 struct net *net = ctl->extra2;
1227 int val = *(int *)ctl->data;
1228
1229 if (i == offsetof(struct mpls_dev, input_enabled) &&
1230 val != oval) {
David Ahern823566a2017-03-28 14:28:06 -07001231 mpls_netconf_notify_devconf(net, RTM_NEWNETCONF,
1232 NETCONFA_INPUT, mdev);
David Ahern24045a02017-02-20 08:03:30 -08001233 }
1234 }
1235
1236 return ret;
1237}
1238
Robert Shearman37bde792015-04-22 11:14:38 +01001239static const struct ctl_table mpls_dev_table[] = {
1240 {
1241 .procname = "input",
1242 .maxlen = sizeof(int),
1243 .mode = 0644,
David Ahern24045a02017-02-20 08:03:30 -08001244 .proc_handler = mpls_conf_proc,
Robert Shearman37bde792015-04-22 11:14:38 +01001245 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
1246 },
1247 { }
1248};
1249
1250static int mpls_dev_sysctl_register(struct net_device *dev,
1251 struct mpls_dev *mdev)
1252{
1253 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
David Ahern24045a02017-02-20 08:03:30 -08001254 struct net *net = dev_net(dev);
Robert Shearman37bde792015-04-22 11:14:38 +01001255 struct ctl_table *table;
1256 int i;
1257
1258 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
1259 if (!table)
1260 goto out;
1261
1262 /* Table data contains only offsets relative to the base of
1263 * the mdev at this point, so make them absolute.
1264 */
David Ahern24045a02017-02-20 08:03:30 -08001265 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) {
Robert Shearman37bde792015-04-22 11:14:38 +01001266 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
David Ahern24045a02017-02-20 08:03:30 -08001267 table[i].extra1 = mdev;
1268 table[i].extra2 = net;
1269 }
Robert Shearman37bde792015-04-22 11:14:38 +01001270
1271 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
1272
David Ahern1182e4d2017-03-28 14:28:07 -07001273 mdev->sysctl = register_net_sysctl(net, path, table);
Robert Shearman37bde792015-04-22 11:14:38 +01001274 if (!mdev->sysctl)
1275 goto free;
1276
David Ahern1182e4d2017-03-28 14:28:07 -07001277 mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev);
Robert Shearman37bde792015-04-22 11:14:38 +01001278 return 0;
1279
1280free:
1281 kfree(table);
1282out:
1283 return -ENOBUFS;
1284}
1285
David Ahern1182e4d2017-03-28 14:28:07 -07001286static void mpls_dev_sysctl_unregister(struct net_device *dev,
1287 struct mpls_dev *mdev)
Robert Shearman37bde792015-04-22 11:14:38 +01001288{
David Ahern1182e4d2017-03-28 14:28:07 -07001289 struct net *net = dev_net(dev);
Robert Shearman37bde792015-04-22 11:14:38 +01001290 struct ctl_table *table;
1291
1292 table = mdev->sysctl->ctl_table_arg;
1293 unregister_net_sysctl_table(mdev->sysctl);
1294 kfree(table);
David Ahern1182e4d2017-03-28 14:28:07 -07001295
1296 mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
Robert Shearman37bde792015-04-22 11:14:38 +01001297}
1298
Robert Shearman03c57742015-04-22 11:14:37 +01001299static struct mpls_dev *mpls_add_dev(struct net_device *dev)
1300{
1301 struct mpls_dev *mdev;
1302 int err = -ENOMEM;
Robert Shearman27d69102017-01-16 14:16:37 +00001303 int i;
Robert Shearman03c57742015-04-22 11:14:37 +01001304
1305 ASSERT_RTNL();
1306
1307 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1308 if (!mdev)
1309 return ERR_PTR(err);
1310
Robert Shearman27d69102017-01-16 14:16:37 +00001311 mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
1312 if (!mdev->stats)
1313 goto free;
1314
1315 for_each_possible_cpu(i) {
1316 struct mpls_pcpu_stats *mpls_stats;
1317
1318 mpls_stats = per_cpu_ptr(mdev->stats, i);
1319 u64_stats_init(&mpls_stats->syncp);
1320 }
1321
David Ahern1182e4d2017-03-28 14:28:07 -07001322 mdev->dev = dev;
1323
Robert Shearman37bde792015-04-22 11:14:38 +01001324 err = mpls_dev_sysctl_register(dev, mdev);
1325 if (err)
1326 goto free;
1327
Robert Shearman03c57742015-04-22 11:14:37 +01001328 rcu_assign_pointer(dev->mpls_ptr, mdev);
1329
1330 return mdev;
Robert Shearman37bde792015-04-22 11:14:38 +01001331
1332free:
Robert Shearman27d69102017-01-16 14:16:37 +00001333 free_percpu(mdev->stats);
Robert Shearman37bde792015-04-22 11:14:38 +01001334 kfree(mdev);
1335 return ERR_PTR(err);
Robert Shearman03c57742015-04-22 11:14:37 +01001336}
1337
Robert Shearman27d69102017-01-16 14:16:37 +00001338static void mpls_dev_destroy_rcu(struct rcu_head *head)
1339{
1340 struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
1341
1342 free_percpu(mdev->stats);
1343 kfree(mdev);
1344}
1345
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001346static void mpls_ifdown(struct net_device *dev, int event)
Eric W. Biederman01891972015-03-03 19:10:47 -06001347{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001348 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -06001349 struct net *net = dev_net(dev);
David Ahern77ef013a2017-03-31 07:14:00 -07001350 u8 alive, deleted;
Eric W. Biederman01891972015-03-03 19:10:47 -06001351 unsigned index;
1352
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001353 platform_label = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06001354 for (index = 0; index < net->mpls.platform_labels; index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001355 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001356
Eric W. Biederman01891972015-03-03 19:10:47 -06001357 if (!rt)
1358 continue;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001359
David Ahern61733c92017-03-13 16:49:10 -07001360 alive = 0;
David Ahern4ea8efa2017-03-24 15:21:57 -07001361 deleted = 0;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001362 change_nexthops(rt) {
David Ahern39eb8cd2017-03-31 07:13:59 -07001363 unsigned int nh_flags = nh->nh_flags;
1364
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001365 if (rtnl_dereference(nh->nh_dev) != dev)
David Ahern61733c92017-03-13 16:49:10 -07001366 goto next;
1367
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001368 switch (event) {
1369 case NETDEV_DOWN:
1370 case NETDEV_UNREGISTER:
David Ahern39eb8cd2017-03-31 07:13:59 -07001371 nh_flags |= RTNH_F_DEAD;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001372 /* fall through */
1373 case NETDEV_CHANGE:
David Ahern39eb8cd2017-03-31 07:13:59 -07001374 nh_flags |= RTNH_F_LINKDOWN;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001375 break;
1376 }
1377 if (event == NETDEV_UNREGISTER)
1378 RCU_INIT_POINTER(nh->nh_dev, NULL);
David Ahern39eb8cd2017-03-31 07:13:59 -07001379
1380 if (nh->nh_flags != nh_flags)
1381 WRITE_ONCE(nh->nh_flags, nh_flags);
David Ahern61733c92017-03-13 16:49:10 -07001382next:
David Ahern39eb8cd2017-03-31 07:13:59 -07001383 if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
David Ahern61733c92017-03-13 16:49:10 -07001384 alive++;
David Ahern4ea8efa2017-03-24 15:21:57 -07001385 if (!rtnl_dereference(nh->nh_dev))
1386 deleted++;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001387 } endfor_nexthops(rt);
David Ahern61733c92017-03-13 16:49:10 -07001388
1389 WRITE_ONCE(rt->rt_nhn_alive, alive);
David Ahern4ea8efa2017-03-24 15:21:57 -07001390
1391 /* if there are no more nexthops, delete the route */
1392 if (event == NETDEV_UNREGISTER && deleted == rt->rt_nhn)
1393 mpls_route_update(net, index, NULL, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001394 }
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001395}
Robert Shearman37bde792015-04-22 11:14:38 +01001396
David Ahern39eb8cd2017-03-31 07:13:59 -07001397static void mpls_ifup(struct net_device *dev, unsigned int flags)
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001398{
1399 struct mpls_route __rcu **platform_label;
1400 struct net *net = dev_net(dev);
1401 unsigned index;
David Ahern77ef013a2017-03-31 07:14:00 -07001402 u8 alive;
Robert Shearman03c57742015-04-22 11:14:37 +01001403
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001404 platform_label = rtnl_dereference(net->mpls.platform_label);
1405 for (index = 0; index < net->mpls.platform_labels; index++) {
1406 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1407
1408 if (!rt)
1409 continue;
1410
1411 alive = 0;
1412 change_nexthops(rt) {
David Ahern39eb8cd2017-03-31 07:13:59 -07001413 unsigned int nh_flags = nh->nh_flags;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001414 struct net_device *nh_dev =
1415 rtnl_dereference(nh->nh_dev);
1416
David Ahern39eb8cd2017-03-31 07:13:59 -07001417 if (!(nh_flags & flags)) {
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001418 alive++;
1419 continue;
1420 }
1421 if (nh_dev != dev)
1422 continue;
1423 alive++;
David Ahern39eb8cd2017-03-31 07:13:59 -07001424 nh_flags &= ~flags;
1425 WRITE_ONCE(nh->nh_flags, flags);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001426 } endfor_nexthops(rt);
1427
David Ahern39eb8cd2017-03-31 07:13:59 -07001428 WRITE_ONCE(rt->rt_nhn_alive, alive);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001429 }
Eric W. Biederman01891972015-03-03 19:10:47 -06001430}
1431
1432static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
1433 void *ptr)
1434{
1435 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Robert Shearman03c57742015-04-22 11:14:37 +01001436 struct mpls_dev *mdev;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001437 unsigned int flags;
Eric W. Biederman01891972015-03-03 19:10:47 -06001438
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001439 if (event == NETDEV_REGISTER) {
Simon Horman407f31b2016-07-07 07:56:15 +02001440 /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
Simon Horman0d227a82016-06-16 17:09:09 +09001441 if (dev->type == ARPHRD_ETHER ||
1442 dev->type == ARPHRD_LOOPBACK ||
Simon Horman407f31b2016-07-07 07:56:15 +02001443 dev->type == ARPHRD_IPGRE ||
1444 dev->type == ARPHRD_SIT ||
1445 dev->type == ARPHRD_TUNNEL) {
Robert Shearman03c57742015-04-22 11:14:37 +01001446 mdev = mpls_add_dev(dev);
1447 if (IS_ERR(mdev))
1448 return notifier_from_errno(PTR_ERR(mdev));
1449 }
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001450 return NOTIFY_OK;
1451 }
Robert Shearman03c57742015-04-22 11:14:37 +01001452
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001453 mdev = mpls_dev_get(dev);
1454 if (!mdev)
1455 return NOTIFY_OK;
1456
1457 switch (event) {
1458 case NETDEV_DOWN:
1459 mpls_ifdown(dev, event);
1460 break;
1461 case NETDEV_UP:
1462 flags = dev_get_flags(dev);
1463 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1464 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1465 else
1466 mpls_ifup(dev, RTNH_F_DEAD);
1467 break;
1468 case NETDEV_CHANGE:
1469 flags = dev_get_flags(dev);
1470 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1471 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1472 else
1473 mpls_ifdown(dev, event);
1474 break;
Eric W. Biederman01891972015-03-03 19:10:47 -06001475 case NETDEV_UNREGISTER:
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001476 mpls_ifdown(dev, event);
1477 mdev = mpls_dev_get(dev);
1478 if (mdev) {
David Ahern1182e4d2017-03-28 14:28:07 -07001479 mpls_dev_sysctl_unregister(dev, mdev);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001480 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
Robert Shearman27d69102017-01-16 14:16:37 +00001481 call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001482 }
Eric W. Biederman01891972015-03-03 19:10:47 -06001483 break;
Robert Shearman0fae3bf2015-06-11 19:58:26 +01001484 case NETDEV_CHANGENAME:
1485 mdev = mpls_dev_get(dev);
1486 if (mdev) {
1487 int err;
1488
David Ahern1182e4d2017-03-28 14:28:07 -07001489 mpls_dev_sysctl_unregister(dev, mdev);
Robert Shearman0fae3bf2015-06-11 19:58:26 +01001490 err = mpls_dev_sysctl_register(dev, mdev);
1491 if (err)
1492 return notifier_from_errno(err);
1493 }
1494 break;
Eric W. Biederman01891972015-03-03 19:10:47 -06001495 }
1496 return NOTIFY_OK;
1497}
1498
1499static struct notifier_block mpls_dev_notifier = {
1500 .notifier_call = mpls_dev_notify,
1501};
1502
Eric W. Biederman03c05662015-03-03 19:13:56 -06001503static int nla_put_via(struct sk_buff *skb,
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001504 u8 table, const void *addr, int alen)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001505{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001506 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
1507 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
1508 };
Eric W. Biederman03c05662015-03-03 19:13:56 -06001509 struct nlattr *nla;
1510 struct rtvia *via;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001511 int family = AF_UNSPEC;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001512
1513 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1514 if (!nla)
1515 return -EMSGSIZE;
1516
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001517 if (table <= NEIGH_NR_TABLES)
1518 family = table_to_family[table];
1519
Eric W. Biederman03c05662015-03-03 19:13:56 -06001520 via = nla_data(nla);
1521 via->rtvia_family = family;
1522 memcpy(via->rtvia_addr, addr, alen);
1523 return 0;
1524}
1525
Eric W. Biederman966bae32015-03-03 19:13:19 -06001526int nla_put_labels(struct sk_buff *skb, int attrtype,
1527 u8 labels, const u32 label[])
1528{
1529 struct nlattr *nla;
1530 struct mpls_shim_hdr *nla_label;
1531 bool bos;
1532 int i;
1533 nla = nla_reserve(skb, attrtype, labels*4);
1534 if (!nla)
1535 return -EMSGSIZE;
1536
1537 nla_label = nla_data(nla);
1538 bos = true;
1539 for (i = labels - 1; i >= 0; i--) {
1540 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1541 bos = false;
1542 }
1543
1544 return 0;
1545}
Roopa Prabhuface0182015-07-21 10:43:52 +02001546EXPORT_SYMBOL_GPL(nla_put_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001547
David Aherna1f10ab2017-05-27 16:19:29 -06001548int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,
1549 u32 label[], struct netlink_ext_ack *extack)
Eric W. Biederman966bae32015-03-03 19:13:19 -06001550{
1551 unsigned len = nla_len(nla);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001552 struct mpls_shim_hdr *nla_label;
David Aherna4ac8c92017-03-31 07:14:03 -07001553 u8 nla_labels;
Eric W. Biederman966bae32015-03-03 19:13:19 -06001554 bool bos;
1555 int i;
1556
David Aherna4ac8c92017-03-31 07:14:03 -07001557 /* len needs to be an even multiple of 4 (the label size). Number
1558 * of labels is a u8 so check for overflow.
1559 */
David Aherna1f10ab2017-05-27 16:19:29 -06001560 if (len & 3 || len / 4 > 255) {
1561 NL_SET_ERR_MSG_ATTR(extack, nla,
1562 "Invalid length for labels attribute");
Eric W. Biederman966bae32015-03-03 19:13:19 -06001563 return -EINVAL;
David Aherna1f10ab2017-05-27 16:19:29 -06001564 }
Eric W. Biederman966bae32015-03-03 19:13:19 -06001565
1566 /* Limit the number of new labels allowed */
1567 nla_labels = len/4;
David Aherna1f10ab2017-05-27 16:19:29 -06001568 if (nla_labels > max_labels) {
1569 NL_SET_ERR_MSG(extack, "Too many labels");
Eric W. Biederman966bae32015-03-03 19:13:19 -06001570 return -EINVAL;
David Aherna1f10ab2017-05-27 16:19:29 -06001571 }
Eric W. Biederman966bae32015-03-03 19:13:19 -06001572
David Aherna4ac8c92017-03-31 07:14:03 -07001573 /* when label == NULL, caller wants number of labels */
1574 if (!label)
1575 goto out;
1576
Eric W. Biederman966bae32015-03-03 19:13:19 -06001577 nla_label = nla_data(nla);
1578 bos = true;
1579 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1580 struct mpls_entry_decoded dec;
1581 dec = mpls_entry_decode(nla_label + i);
1582
1583 /* Ensure the bottom of stack flag is properly set
1584 * and ttl and tc are both clear.
1585 */
David Aherna1f10ab2017-05-27 16:19:29 -06001586 if (dec.ttl) {
1587 NL_SET_ERR_MSG_ATTR(extack, nla,
1588 "TTL in label must be 0");
Eric W. Biederman966bae32015-03-03 19:13:19 -06001589 return -EINVAL;
David Aherna1f10ab2017-05-27 16:19:29 -06001590 }
1591
1592 if (dec.tc) {
1593 NL_SET_ERR_MSG_ATTR(extack, nla,
1594 "Traffic class in label must be 0");
1595 return -EINVAL;
1596 }
1597
1598 if (dec.bos != bos) {
1599 NL_SET_BAD_ATTR(extack, nla);
1600 if (bos) {
1601 NL_SET_ERR_MSG(extack,
1602 "BOS bit must be set in first label");
1603 } else {
1604 NL_SET_ERR_MSG(extack,
1605 "BOS bit can only be set in first label");
1606 }
1607 return -EINVAL;
1608 }
Eric W. Biederman966bae32015-03-03 19:13:19 -06001609
Robert Shearman5a9ab012015-04-22 11:14:39 +01001610 switch (dec.label) {
Tom Herbert78f5b892015-05-07 08:08:51 -07001611 case MPLS_LABEL_IMPLNULL:
Robert Shearman5a9ab012015-04-22 11:14:39 +01001612 /* RFC3032: This is a label that an LSR may
1613 * assign and distribute, but which never
1614 * actually appears in the encapsulation.
1615 */
David Aherna1f10ab2017-05-27 16:19:29 -06001616 NL_SET_ERR_MSG_ATTR(extack, nla,
1617 "Implicit NULL Label (3) can not be used in encapsulation");
Robert Shearman5a9ab012015-04-22 11:14:39 +01001618 return -EINVAL;
1619 }
1620
Eric W. Biederman966bae32015-03-03 19:13:19 -06001621 label[i] = dec.label;
1622 }
David Aherna4ac8c92017-03-31 07:14:03 -07001623out:
Eric W. Biederman966bae32015-03-03 19:13:19 -06001624 *labels = nla_labels;
1625 return 0;
1626}
Roopa Prabhuface0182015-07-21 10:43:52 +02001627EXPORT_SYMBOL_GPL(nla_get_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001628
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001629int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1630 u8 *via_table, u8 via_addr[])
1631{
1632 struct rtvia *via = nla_data(nla);
1633 int err = -EINVAL;
1634 int alen;
1635
1636 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1637 goto errout;
1638 alen = nla_len(nla) -
1639 offsetof(struct rtvia, rtvia_addr);
1640 if (alen > MAX_VIA_ALEN)
1641 goto errout;
1642
1643 /* Validate the address family */
1644 switch (via->rtvia_family) {
1645 case AF_PACKET:
1646 *via_table = NEIGH_LINK_TABLE;
1647 break;
1648 case AF_INET:
1649 *via_table = NEIGH_ARP_TABLE;
1650 if (alen != 4)
1651 goto errout;
1652 break;
1653 case AF_INET6:
1654 *via_table = NEIGH_ND_TABLE;
1655 if (alen != 16)
1656 goto errout;
1657 break;
1658 default:
1659 /* Unsupported address family */
1660 goto errout;
1661 }
1662
1663 memcpy(via_addr, via->rtvia_addr, alen);
1664 *via_alen = alen;
1665 err = 0;
1666
1667errout:
1668 return err;
1669}
1670
Eric W. Biederman03c05662015-03-03 19:13:56 -06001671static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1672 struct mpls_route_config *cfg)
1673{
1674 struct rtmsg *rtm;
1675 struct nlattr *tb[RTA_MAX+1];
1676 int index;
1677 int err;
1678
Johannes Bergfceb6432017-04-12 14:34:07 +02001679 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy,
1680 NULL);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001681 if (err < 0)
1682 goto errout;
1683
1684 err = -EINVAL;
1685 rtm = nlmsg_data(nlh);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001686
1687 if (rtm->rtm_family != AF_MPLS)
1688 goto errout;
1689 if (rtm->rtm_dst_len != 20)
1690 goto errout;
1691 if (rtm->rtm_src_len != 0)
1692 goto errout;
1693 if (rtm->rtm_tos != 0)
1694 goto errout;
1695 if (rtm->rtm_table != RT_TABLE_MAIN)
1696 goto errout;
1697 /* Any value is acceptable for rtm_protocol */
1698
1699 /* As mpls uses destination specific addresses
1700 * (or source specific address in the case of multicast)
1701 * all addresses have universal scope.
1702 */
1703 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1704 goto errout;
1705 if (rtm->rtm_type != RTN_UNICAST)
1706 goto errout;
1707 if (rtm->rtm_flags != 0)
1708 goto errout;
1709
1710 cfg->rc_label = LABEL_NOT_SPECIFIED;
1711 cfg->rc_protocol = rtm->rtm_protocol;
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001712 cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001713 cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001714 cfg->rc_nlflags = nlh->nlmsg_flags;
1715 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1716 cfg->rc_nlinfo.nlh = nlh;
1717 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1718
1719 for (index = 0; index <= RTA_MAX; index++) {
1720 struct nlattr *nla = tb[index];
1721 if (!nla)
1722 continue;
1723
Suraj Deshmukh14dd3e12016-12-03 07:59:26 +00001724 switch (index) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001725 case RTA_OIF:
1726 cfg->rc_ifindex = nla_get_u32(nla);
1727 break;
1728 case RTA_NEWDST:
1729 if (nla_get_labels(nla, MAX_NEW_LABELS,
1730 &cfg->rc_output_labels,
David Aherna1f10ab2017-05-27 16:19:29 -06001731 cfg->rc_output_label, NULL))
Eric W. Biederman03c05662015-03-03 19:13:56 -06001732 goto errout;
1733 break;
1734 case RTA_DST:
1735 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001736 u8 label_count;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001737 if (nla_get_labels(nla, 1, &label_count,
David Aherna1f10ab2017-05-27 16:19:29 -06001738 &cfg->rc_label, NULL))
Eric W. Biederman03c05662015-03-03 19:13:56 -06001739 goto errout;
1740
David Ahernb7b386f2017-05-27 16:19:30 -06001741 if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
1742 cfg->rc_label))
Eric W. Biederman03c05662015-03-03 19:13:56 -06001743 goto errout;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001744 break;
1745 }
1746 case RTA_VIA:
1747 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001748 if (nla_get_via(nla, &cfg->rc_via_alen,
1749 &cfg->rc_via_table, cfg->rc_via))
Robert Shearmanf8d54af2015-03-06 10:47:00 +00001750 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001751 break;
1752 }
1753 case RTA_MULTIPATH:
1754 {
1755 cfg->rc_mp = nla_data(nla);
1756 cfg->rc_mp_len = nla_len(nla);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001757 break;
1758 }
Robert Shearman5b441ac2017-03-10 20:43:24 +00001759 case RTA_TTL_PROPAGATE:
1760 {
1761 u8 ttl_propagate = nla_get_u8(nla);
1762
1763 if (ttl_propagate > 1)
1764 goto errout;
1765 cfg->rc_ttl_propagate = ttl_propagate ?
1766 MPLS_TTL_PROP_ENABLED :
1767 MPLS_TTL_PROP_DISABLED;
1768 break;
1769 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001770 default:
1771 /* Unsupported attribute */
1772 goto errout;
1773 }
1774 }
1775
1776 err = 0;
1777errout:
1778 return err;
1779}
1780
David Ahernc21ef3e2017-04-16 09:48:24 -07001781static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1782 struct netlink_ext_ack *extack)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001783{
David Aherna4ac8c92017-03-31 07:14:03 -07001784 struct mpls_route_config *cfg;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001785 int err;
1786
David Aherna4ac8c92017-03-31 07:14:03 -07001787 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1788 if (!cfg)
1789 return -ENOMEM;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001790
David Aherna4ac8c92017-03-31 07:14:03 -07001791 err = rtm_to_route_config(skb, nlh, cfg);
1792 if (err < 0)
1793 goto out;
1794
1795 err = mpls_route_del(cfg);
1796out:
1797 kfree(cfg);
1798
1799 return err;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001800}
1801
1802
David Ahernc21ef3e2017-04-16 09:48:24 -07001803static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1804 struct netlink_ext_ack *extack)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001805{
David Aherna4ac8c92017-03-31 07:14:03 -07001806 struct mpls_route_config *cfg;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001807 int err;
1808
David Aherna4ac8c92017-03-31 07:14:03 -07001809 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1810 if (!cfg)
1811 return -ENOMEM;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001812
David Aherna4ac8c92017-03-31 07:14:03 -07001813 err = rtm_to_route_config(skb, nlh, cfg);
1814 if (err < 0)
1815 goto out;
1816
1817 err = mpls_route_add(cfg);
1818out:
1819 kfree(cfg);
1820
1821 return err;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001822}
1823
1824static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1825 u32 label, struct mpls_route *rt, int flags)
1826{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001827 struct net_device *dev;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001828 struct nlmsghdr *nlh;
1829 struct rtmsg *rtm;
1830
1831 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1832 if (nlh == NULL)
1833 return -EMSGSIZE;
1834
1835 rtm = nlmsg_data(nlh);
1836 rtm->rtm_family = AF_MPLS;
1837 rtm->rtm_dst_len = 20;
1838 rtm->rtm_src_len = 0;
1839 rtm->rtm_tos = 0;
1840 rtm->rtm_table = RT_TABLE_MAIN;
1841 rtm->rtm_protocol = rt->rt_protocol;
1842 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1843 rtm->rtm_type = RTN_UNICAST;
1844 rtm->rtm_flags = 0;
1845
Eric W. Biederman03c05662015-03-03 19:13:56 -06001846 if (nla_put_labels(skb, RTA_DST, 1, &label))
1847 goto nla_put_failure;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001848
1849 if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
1850 bool ttl_propagate =
1851 rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
1852
1853 if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
1854 ttl_propagate))
1855 goto nla_put_failure;
1856 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001857 if (rt->rt_nhn == 1) {
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001858 const struct mpls_nh *nh = rt->rt_nh;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001859
1860 if (nh->nh_labels &&
1861 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1862 nh->nh_label))
1863 goto nla_put_failure;
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001864 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
Robert Shearman72dcac92015-12-10 19:30:49 +00001865 nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001866 nh->nh_via_alen))
1867 goto nla_put_failure;
1868 dev = rtnl_dereference(nh->nh_dev);
1869 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1870 goto nla_put_failure;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001871 if (nh->nh_flags & RTNH_F_LINKDOWN)
1872 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1873 if (nh->nh_flags & RTNH_F_DEAD)
1874 rtm->rtm_flags |= RTNH_F_DEAD;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001875 } else {
1876 struct rtnexthop *rtnh;
1877 struct nlattr *mp;
David Ahern77ef013a2017-03-31 07:14:00 -07001878 u8 linkdown = 0;
1879 u8 dead = 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001880
1881 mp = nla_nest_start(skb, RTA_MULTIPATH);
1882 if (!mp)
1883 goto nla_put_failure;
1884
1885 for_nexthops(rt) {
David Ahernc00e51d2017-03-24 15:21:56 -07001886 dev = rtnl_dereference(nh->nh_dev);
1887 if (!dev)
1888 continue;
1889
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001890 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1891 if (!rtnh)
1892 goto nla_put_failure;
1893
David Ahernc00e51d2017-03-24 15:21:56 -07001894 rtnh->rtnh_ifindex = dev->ifindex;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001895 if (nh->nh_flags & RTNH_F_LINKDOWN) {
1896 rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
1897 linkdown++;
1898 }
1899 if (nh->nh_flags & RTNH_F_DEAD) {
1900 rtnh->rtnh_flags |= RTNH_F_DEAD;
1901 dead++;
1902 }
1903
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001904 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1905 nh->nh_labels,
1906 nh->nh_label))
1907 goto nla_put_failure;
Robert Shearmanf20367d2015-12-10 19:30:51 +00001908 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1909 nla_put_via(skb, nh->nh_via_table,
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001910 mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001911 nh->nh_via_alen))
1912 goto nla_put_failure;
1913
1914 /* length of rtnetlink header + attributes */
1915 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1916 } endfor_nexthops(rt);
1917
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001918 if (linkdown == rt->rt_nhn)
1919 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1920 if (dead == rt->rt_nhn)
1921 rtm->rtm_flags |= RTNH_F_DEAD;
1922
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001923 nla_nest_end(skb, mp);
1924 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001925
1926 nlmsg_end(skb, nlh);
1927 return 0;
1928
1929nla_put_failure:
1930 nlmsg_cancel(skb, nlh);
1931 return -EMSGSIZE;
1932}
1933
1934static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1935{
1936 struct net *net = sock_net(skb->sk);
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001937 struct mpls_route __rcu **platform_label;
1938 size_t platform_labels;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001939 unsigned int index;
1940
1941 ASSERT_RTNL();
1942
1943 index = cb->args[0];
Robert Shearmana6affd22015-08-03 17:50:04 +01001944 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1945 index = MPLS_LABEL_FIRST_UNRESERVED;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001946
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001947 platform_label = rtnl_dereference(net->mpls.platform_label);
1948 platform_labels = net->mpls.platform_labels;
1949 for (; index < platform_labels; index++) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001950 struct mpls_route *rt;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001951 rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001952 if (!rt)
1953 continue;
1954
1955 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1956 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1957 index, rt, NLM_F_MULTI) < 0)
1958 break;
1959 }
1960 cb->args[0] = index;
1961
1962 return skb->len;
1963}
1964
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001965static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1966{
1967 size_t payload =
1968 NLMSG_ALIGN(sizeof(struct rtmsg))
Robert Shearman5b441ac2017-03-10 20:43:24 +00001969 + nla_total_size(4) /* RTA_DST */
1970 + nla_total_size(1); /* RTA_TTL_PROPAGATE */
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001971
1972 if (rt->rt_nhn == 1) {
1973 struct mpls_nh *nh = rt->rt_nh;
1974
1975 if (nh->nh_dev)
1976 payload += nla_total_size(4); /* RTA_OIF */
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001977 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
Robert Shearman72dcac92015-12-10 19:30:49 +00001978 payload += nla_total_size(2 + nh->nh_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001979 if (nh->nh_labels) /* RTA_NEWDST */
1980 payload += nla_total_size(nh->nh_labels * 4);
1981 } else {
1982 /* each nexthop is packed in an attribute */
1983 size_t nhsize = 0;
1984
1985 for_nexthops(rt) {
David Aherne944e972017-03-28 15:19:49 -07001986 if (!rtnl_dereference(nh->nh_dev))
1987 continue;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001988 nhsize += nla_total_size(sizeof(struct rtnexthop));
Robert Shearmanf20367d2015-12-10 19:30:51 +00001989 /* RTA_VIA */
1990 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
1991 nhsize += nla_total_size(2 + nh->nh_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001992 if (nh->nh_labels)
1993 nhsize += nla_total_size(nh->nh_labels * 4);
1994 } endfor_nexthops(rt);
1995 /* nested attribute */
1996 payload += nla_total_size(nhsize);
1997 }
1998
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001999 return payload;
2000}
2001
2002static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
2003 struct nlmsghdr *nlh, struct net *net, u32 portid,
2004 unsigned int nlm_flags)
2005{
2006 struct sk_buff *skb;
2007 u32 seq = nlh ? nlh->nlmsg_seq : 0;
2008 int err = -ENOBUFS;
2009
2010 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
2011 if (skb == NULL)
2012 goto errout;
2013
2014 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
2015 if (err < 0) {
2016 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
2017 WARN_ON(err == -EMSGSIZE);
2018 kfree_skb(skb);
2019 goto errout;
2020 }
2021 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
2022
2023 return;
2024errout:
2025 if (err < 0)
2026 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
2027}
2028
Eric W. Biederman7720c012015-03-03 19:11:20 -06002029static int resize_platform_label_table(struct net *net, size_t limit)
2030{
2031 size_t size = sizeof(struct mpls_route *) * limit;
2032 size_t old_limit;
2033 size_t cp_size;
2034 struct mpls_route __rcu **labels = NULL, **old;
2035 struct mpls_route *rt0 = NULL, *rt2 = NULL;
2036 unsigned index;
2037
2038 if (size) {
Michal Hocko752ade62017-05-08 15:57:27 -07002039 labels = kvzalloc(size, GFP_KERNEL);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002040 if (!labels)
2041 goto nolabels;
2042 }
2043
2044 /* In case the predefined labels need to be populated */
Tom Herbert78f5b892015-05-07 08:08:51 -07002045 if (limit > MPLS_LABEL_IPV4NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06002046 struct net_device *lo = net->loopback_dev;
David Aherna4ac8c92017-03-31 07:14:03 -07002047 rt0 = mpls_rt_alloc(1, lo->addr_len, 0);
David Aherndf1c6312017-03-31 07:14:02 -07002048 if (IS_ERR(rt0))
Eric W. Biederman7720c012015-03-03 19:11:20 -06002049 goto nort0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07002050 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002051 rt0->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01002052 rt0->rt_payload_type = MPT_IPV4;
Robert Shearman5b441ac2017-03-10 20:43:24 +00002053 rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07002054 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00002055 rt0->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00002056 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
2057 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002058 }
Tom Herbert78f5b892015-05-07 08:08:51 -07002059 if (limit > MPLS_LABEL_IPV6NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06002060 struct net_device *lo = net->loopback_dev;
David Aherna4ac8c92017-03-31 07:14:03 -07002061 rt2 = mpls_rt_alloc(1, lo->addr_len, 0);
David Aherndf1c6312017-03-31 07:14:02 -07002062 if (IS_ERR(rt2))
Eric W. Biederman7720c012015-03-03 19:11:20 -06002063 goto nort2;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07002064 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002065 rt2->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01002066 rt2->rt_payload_type = MPT_IPV6;
David Ahern6a18c312017-03-23 19:02:27 -06002067 rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07002068 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00002069 rt2->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00002070 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
2071 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002072 }
2073
2074 rtnl_lock();
2075 /* Remember the original table */
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002076 old = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002077 old_limit = net->mpls.platform_labels;
2078
2079 /* Free any labels beyond the new table */
2080 for (index = limit; index < old_limit; index++)
Roopa Prabhuf8efb732015-10-23 06:03:27 -07002081 mpls_route_update(net, index, NULL, NULL);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002082
2083 /* Copy over the old labels */
2084 cp_size = size;
2085 if (old_limit < limit)
2086 cp_size = old_limit * sizeof(struct mpls_route *);
2087
2088 memcpy(labels, old, cp_size);
2089
2090 /* If needed set the predefined labels */
Tom Herbert78f5b892015-05-07 08:08:51 -07002091 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
2092 (limit > MPLS_LABEL_IPV6NULL)) {
2093 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002094 rt2 = NULL;
2095 }
2096
Tom Herbert78f5b892015-05-07 08:08:51 -07002097 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
2098 (limit > MPLS_LABEL_IPV4NULL)) {
2099 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002100 rt0 = NULL;
2101 }
2102
2103 /* Update the global pointers */
2104 net->mpls.platform_labels = limit;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002105 rcu_assign_pointer(net->mpls.platform_label, labels);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002106
2107 rtnl_unlock();
2108
2109 mpls_rt_free(rt2);
2110 mpls_rt_free(rt0);
2111
2112 if (old) {
2113 synchronize_rcu();
2114 kvfree(old);
2115 }
2116 return 0;
2117
2118nort2:
2119 mpls_rt_free(rt0);
2120nort0:
2121 kvfree(labels);
2122nolabels:
2123 return -ENOMEM;
2124}
2125
2126static int mpls_platform_labels(struct ctl_table *table, int write,
2127 void __user *buffer, size_t *lenp, loff_t *ppos)
2128{
2129 struct net *net = table->data;
2130 int platform_labels = net->mpls.platform_labels;
2131 int ret;
2132 struct ctl_table tmp = {
2133 .procname = table->procname,
2134 .data = &platform_labels,
2135 .maxlen = sizeof(int),
2136 .mode = table->mode,
2137 .extra1 = &zero,
2138 .extra2 = &label_limit,
2139 };
2140
2141 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2142
2143 if (write && ret == 0)
2144 ret = resize_platform_label_table(net, platform_labels);
2145
2146 return ret;
2147}
2148
Robert Shearman5b441ac2017-03-10 20:43:24 +00002149#define MPLS_NS_SYSCTL_OFFSET(field) \
2150 (&((struct net *)0)->field)
2151
Robert Shearman37bde792015-04-22 11:14:38 +01002152static const struct ctl_table mpls_table[] = {
Eric W. Biederman7720c012015-03-03 19:11:20 -06002153 {
2154 .procname = "platform_labels",
2155 .data = NULL,
2156 .maxlen = sizeof(int),
2157 .mode = 0644,
2158 .proc_handler = mpls_platform_labels,
2159 },
Robert Shearman5b441ac2017-03-10 20:43:24 +00002160 {
2161 .procname = "ip_ttl_propagate",
2162 .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
2163 .maxlen = sizeof(int),
2164 .mode = 0644,
2165 .proc_handler = proc_dointvec_minmax,
2166 .extra1 = &zero,
2167 .extra2 = &one,
2168 },
Robert Shearmana59166e2017-03-10 20:43:25 +00002169 {
2170 .procname = "default_ttl",
2171 .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
2172 .maxlen = sizeof(int),
2173 .mode = 0644,
2174 .proc_handler = proc_dointvec_minmax,
2175 .extra1 = &one,
2176 .extra2 = &ttl_max,
2177 },
Eric W. Biederman7720c012015-03-03 19:11:20 -06002178 { }
2179};
2180
Eric W. Biederman01891972015-03-03 19:10:47 -06002181static int mpls_net_init(struct net *net)
2182{
Eric W. Biederman7720c012015-03-03 19:11:20 -06002183 struct ctl_table *table;
Robert Shearman5b441ac2017-03-10 20:43:24 +00002184 int i;
Eric W. Biederman7720c012015-03-03 19:11:20 -06002185
Eric W. Biederman01891972015-03-03 19:10:47 -06002186 net->mpls.platform_labels = 0;
2187 net->mpls.platform_label = NULL;
Robert Shearman5b441ac2017-03-10 20:43:24 +00002188 net->mpls.ip_ttl_propagate = 1;
Robert Shearmana59166e2017-03-10 20:43:25 +00002189 net->mpls.default_ttl = 255;
Eric W. Biederman01891972015-03-03 19:10:47 -06002190
Eric W. Biederman7720c012015-03-03 19:11:20 -06002191 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
2192 if (table == NULL)
2193 return -ENOMEM;
2194
Robert Shearman5b441ac2017-03-10 20:43:24 +00002195 /* Table data contains only offsets relative to the base of
2196 * the mdev at this point, so make them absolute.
2197 */
2198 for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++)
2199 table[i].data = (char *)net + (uintptr_t)table[i].data;
2200
Eric W. Biederman7720c012015-03-03 19:11:20 -06002201 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07002202 if (net->mpls.ctl == NULL) {
2203 kfree(table);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002204 return -ENOMEM;
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07002205 }
Eric W. Biederman7720c012015-03-03 19:11:20 -06002206
Eric W. Biederman01891972015-03-03 19:10:47 -06002207 return 0;
2208}
2209
2210static void mpls_net_exit(struct net *net)
2211{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002212 struct mpls_route __rcu **platform_label;
2213 size_t platform_labels;
Eric W. Biederman7720c012015-03-03 19:11:20 -06002214 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -06002215 unsigned int index;
2216
Eric W. Biederman7720c012015-03-03 19:11:20 -06002217 table = net->mpls.ctl->ctl_table_arg;
2218 unregister_net_sysctl_table(net->mpls.ctl);
2219 kfree(table);
2220
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002221 /* An rcu grace period has passed since there was a device in
2222 * the network namespace (and thus the last in flight packet)
Eric W. Biederman01891972015-03-03 19:10:47 -06002223 * left this network namespace. This is because
2224 * unregister_netdevice_many and netdev_run_todo has completed
2225 * for each network device that was in this network namespace.
2226 *
2227 * As such no additional rcu synchronization is necessary when
2228 * freeing the platform_label table.
2229 */
2230 rtnl_lock();
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002231 platform_label = rtnl_dereference(net->mpls.platform_label);
2232 platform_labels = net->mpls.platform_labels;
2233 for (index = 0; index < platform_labels; index++) {
2234 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
2235 RCU_INIT_POINTER(platform_label[index], NULL);
David Aherne37791e2017-03-10 09:46:15 -08002236 mpls_notify_route(net, index, rt, NULL, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06002237 mpls_rt_free(rt);
2238 }
2239 rtnl_unlock();
2240
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002241 kvfree(platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06002242}
2243
2244static struct pernet_operations mpls_net_ops = {
2245 .init = mpls_net_init,
2246 .exit = mpls_net_exit,
2247};
2248
Robert Shearman27d69102017-01-16 14:16:37 +00002249static struct rtnl_af_ops mpls_af_ops __read_mostly = {
2250 .family = AF_MPLS,
2251 .fill_stats_af = mpls_fill_stats_af,
2252 .get_stats_af_size = mpls_get_stats_af_size,
2253};
2254
Eric W. Biederman01891972015-03-03 19:10:47 -06002255static int __init mpls_init(void)
2256{
2257 int err;
2258
2259 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
2260
2261 err = register_pernet_subsys(&mpls_net_ops);
2262 if (err)
2263 goto out;
2264
2265 err = register_netdevice_notifier(&mpls_dev_notifier);
2266 if (err)
2267 goto out_unregister_pernet;
2268
2269 dev_add_pack(&mpls_packet_type);
2270
Robert Shearman27d69102017-01-16 14:16:37 +00002271 rtnl_af_register(&mpls_af_ops);
2272
Eric W. Biederman03c05662015-03-03 19:13:56 -06002273 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
2274 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
2275 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
David Ahern24045a02017-02-20 08:03:30 -08002276 rtnl_register(PF_MPLS, RTM_GETNETCONF, mpls_netconf_get_devconf,
2277 mpls_netconf_dump_devconf, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06002278 err = 0;
2279out:
2280 return err;
2281
2282out_unregister_pernet:
2283 unregister_pernet_subsys(&mpls_net_ops);
2284 goto out;
2285}
2286module_init(mpls_init);
2287
2288static void __exit mpls_exit(void)
2289{
Eric W. Biederman03c05662015-03-03 19:13:56 -06002290 rtnl_unregister_all(PF_MPLS);
Robert Shearman27d69102017-01-16 14:16:37 +00002291 rtnl_af_unregister(&mpls_af_ops);
Eric W. Biederman01891972015-03-03 19:10:47 -06002292 dev_remove_pack(&mpls_packet_type);
2293 unregister_netdevice_notifier(&mpls_dev_notifier);
2294 unregister_pernet_subsys(&mpls_net_ops);
2295}
2296module_exit(mpls_exit);
2297
2298MODULE_DESCRIPTION("MultiProtocol Label Switching");
2299MODULE_LICENSE("GPL v2");
2300MODULE_ALIAS_NETPROTO(PF_MPLS);