blob: 7b05fd1497ceddea47c2c7917f5ee58f6ff2d560 [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 Aherna4ac8c92017-03-31 07:14:03 -0700731 err = nla_get_labels(newdst, max_labels,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700732 &nh->nh_labels, nh->nh_label);
733 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 &&
785 nla_get_labels(nla, MAX_NEW_LABELS, &n_labels, NULL) != 0)
786 return 0;
787
788 *max_labels = max_t(u8, *max_labels, n_labels);
789
David Ahern77ef013a2017-03-31 07:14:00 -0700790 /* number of nexthops is tracked by a u8.
791 * Check for overflow.
792 */
793 if (nhs == 255)
794 return 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700795 nhs++;
David Ahern77ef013a2017-03-31 07:14:00 -0700796
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700797 rtnh = rtnh_next(rtnh, &remaining);
798 }
799
800 /* leftover implies invalid nexthop configuration, discard it */
801 return remaining > 0 ? 0 : nhs;
802}
803
804static int mpls_nh_build_multi(struct mpls_route_config *cfg,
David Aherna4ac8c92017-03-31 07:14:03 -0700805 struct mpls_route *rt, u8 max_labels)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700806{
807 struct rtnexthop *rtnh = cfg->rc_mp;
808 struct nlattr *nla_via, *nla_newdst;
809 int remaining = cfg->rc_mp_len;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700810 int err = 0;
David Ahern77ef013a2017-03-31 07:14:00 -0700811 u8 nhs = 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700812
813 change_nexthops(rt) {
814 int attrlen;
815
816 nla_via = NULL;
817 nla_newdst = NULL;
818
819 err = -EINVAL;
820 if (!rtnh_ok(rtnh, remaining))
821 goto errout;
822
Robert Shearman1c78efa2015-10-23 06:03:28 -0700823 /* neither weighted multipath nor any flags
824 * are supported
825 */
826 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
827 goto errout;
828
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700829 attrlen = rtnh_attrlen(rtnh);
830 if (attrlen > 0) {
831 struct nlattr *attrs = rtnh_attrs(rtnh);
832
833 nla_via = nla_find(attrs, attrlen, RTA_VIA);
834 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
835 }
836
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000837 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
David Aherna4ac8c92017-03-31 07:14:03 -0700838 rtnh->rtnh_ifindex, nla_via, nla_newdst,
839 max_labels);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700840 if (err)
841 goto errout;
842
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800843 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
844 rt->rt_nhn_alive--;
845
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700846 rtnh = rtnh_next(rtnh, &remaining);
847 nhs++;
848 } endfor_nexthops(rt);
849
850 rt->rt_nhn = nhs;
851
852 return 0;
853
854errout:
855 return err;
856}
857
Eric W. Biedermana2519922015-03-03 19:12:40 -0600858static int mpls_route_add(struct mpls_route_config *cfg)
859{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600860 struct mpls_route __rcu **platform_label;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600861 struct net *net = cfg->rc_nlinfo.nl_net;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600862 struct mpls_route *rt, *old;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600863 int err = -EINVAL;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000864 u8 max_via_alen;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700865 unsigned index;
David Aherna4ac8c92017-03-31 07:14:03 -0700866 u8 max_labels;
David Ahern77ef013a2017-03-31 07:14:00 -0700867 u8 nhs;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600868
869 index = cfg->rc_label;
870
871 /* If a label was not specified during insert pick one */
872 if ((index == LABEL_NOT_SPECIFIED) &&
873 (cfg->rc_nlflags & NLM_F_CREATE)) {
874 index = find_free_label(net);
875 }
876
Robert Shearmana6affd22015-08-03 17:50:04 +0100877 /* Reserved labels may not be set */
878 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600879 goto errout;
880
881 /* The full 20 bit range may not be supported. */
882 if (index >= net->mpls.platform_labels)
883 goto errout;
884
Eric W. Biedermana2519922015-03-03 19:12:40 -0600885 /* Append makes no sense with mpls */
Eric W. Biederman0f7bbd52015-03-07 16:22:40 -0600886 err = -EOPNOTSUPP;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600887 if (cfg->rc_nlflags & NLM_F_APPEND)
888 goto errout;
889
890 err = -EEXIST;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600891 platform_label = rtnl_dereference(net->mpls.platform_label);
892 old = rtnl_dereference(platform_label[index]);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600893 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
894 goto errout;
895
896 err = -EEXIST;
897 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
898 goto errout;
899
900 err = -ENOENT;
901 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
902 goto errout;
903
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000904 err = -EINVAL;
David Aherna4ac8c92017-03-31 07:14:03 -0700905 if (cfg->rc_mp) {
906 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
907 cfg->rc_via_alen, &max_via_alen,
908 &max_labels);
909 } else {
910 max_via_alen = cfg->rc_via_alen;
911 max_labels = cfg->rc_output_labels;
912 nhs = 1;
913 }
914
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000915 if (nhs == 0)
916 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700917
Eric W. Biedermana2519922015-03-03 19:12:40 -0600918 err = -ENOMEM;
David Aherna4ac8c92017-03-31 07:14:03 -0700919 rt = mpls_rt_alloc(nhs, max_via_alen, max_labels);
David Aherndf1c6312017-03-31 07:14:02 -0700920 if (IS_ERR(rt)) {
921 err = PTR_ERR(rt);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600922 goto errout;
David Aherndf1c6312017-03-31 07:14:02 -0700923 }
Eric W. Biedermana2519922015-03-03 19:12:40 -0600924
Eric W. Biedermana2519922015-03-03 19:12:40 -0600925 rt->rt_protocol = cfg->rc_protocol;
Robert Shearman118d5232015-08-06 11:04:56 +0100926 rt->rt_payload_type = cfg->rc_payload_type;
Robert Shearman5b441ac2017-03-10 20:43:24 +0000927 rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600928
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700929 if (cfg->rc_mp)
David Aherna4ac8c92017-03-31 07:14:03 -0700930 err = mpls_nh_build_multi(cfg, rt, max_labels);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700931 else
932 err = mpls_nh_build_from_cfg(cfg, rt);
933 if (err)
934 goto freert;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600935
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700936 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
937
Eric W. Biedermana2519922015-03-03 19:12:40 -0600938 return 0;
939
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700940freert:
941 mpls_rt_free(rt);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600942errout:
Eric W. Biedermana2519922015-03-03 19:12:40 -0600943 return err;
944}
945
946static int mpls_route_del(struct mpls_route_config *cfg)
947{
948 struct net *net = cfg->rc_nlinfo.nl_net;
949 unsigned index;
950 int err = -EINVAL;
951
952 index = cfg->rc_label;
953
Robert Shearmana6affd22015-08-03 17:50:04 +0100954 /* Reserved labels may not be removed */
955 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600956 goto errout;
957
958 /* The full 20 bit range may not be supported */
959 if (index >= net->mpls.platform_labels)
960 goto errout;
961
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700962 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600963
964 err = 0;
965errout:
966 return err;
967}
968
Robert Shearman27d69102017-01-16 14:16:37 +0000969static void mpls_get_stats(struct mpls_dev *mdev,
970 struct mpls_link_stats *stats)
971{
972 struct mpls_pcpu_stats *p;
973 int i;
974
975 memset(stats, 0, sizeof(*stats));
976
977 for_each_possible_cpu(i) {
978 struct mpls_link_stats local;
979 unsigned int start;
980
981 p = per_cpu_ptr(mdev->stats, i);
982 do {
983 start = u64_stats_fetch_begin(&p->syncp);
984 local = p->stats;
985 } while (u64_stats_fetch_retry(&p->syncp, start));
986
987 stats->rx_packets += local.rx_packets;
988 stats->rx_bytes += local.rx_bytes;
989 stats->tx_packets += local.tx_packets;
990 stats->tx_bytes += local.tx_bytes;
991 stats->rx_errors += local.rx_errors;
992 stats->tx_errors += local.tx_errors;
993 stats->rx_dropped += local.rx_dropped;
994 stats->tx_dropped += local.tx_dropped;
995 stats->rx_noroute += local.rx_noroute;
996 }
997}
998
999static int mpls_fill_stats_af(struct sk_buff *skb,
1000 const struct net_device *dev)
1001{
1002 struct mpls_link_stats *stats;
1003 struct mpls_dev *mdev;
1004 struct nlattr *nla;
1005
1006 mdev = mpls_dev_get(dev);
1007 if (!mdev)
1008 return -ENODATA;
1009
1010 nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
1011 sizeof(struct mpls_link_stats),
1012 MPLS_STATS_UNSPEC);
1013 if (!nla)
1014 return -EMSGSIZE;
1015
1016 stats = nla_data(nla);
1017 mpls_get_stats(mdev, stats);
1018
1019 return 0;
1020}
1021
1022static size_t mpls_get_stats_af_size(const struct net_device *dev)
1023{
1024 struct mpls_dev *mdev;
1025
1026 mdev = mpls_dev_get(dev);
1027 if (!mdev)
1028 return 0;
1029
1030 return nla_total_size_64bit(sizeof(struct mpls_link_stats));
1031}
1032
David Ahern24045a02017-02-20 08:03:30 -08001033static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
1034 u32 portid, u32 seq, int event,
1035 unsigned int flags, int type)
1036{
1037 struct nlmsghdr *nlh;
1038 struct netconfmsg *ncm;
1039 bool all = false;
1040
1041 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1042 flags);
1043 if (!nlh)
1044 return -EMSGSIZE;
1045
1046 if (type == NETCONFA_ALL)
1047 all = true;
1048
1049 ncm = nlmsg_data(nlh);
1050 ncm->ncm_family = AF_MPLS;
1051
1052 if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
1053 goto nla_put_failure;
1054
1055 if ((all || type == NETCONFA_INPUT) &&
1056 nla_put_s32(skb, NETCONFA_INPUT,
1057 mdev->input_enabled) < 0)
1058 goto nla_put_failure;
1059
1060 nlmsg_end(skb, nlh);
1061 return 0;
1062
1063nla_put_failure:
1064 nlmsg_cancel(skb, nlh);
1065 return -EMSGSIZE;
1066}
1067
1068static int mpls_netconf_msgsize_devconf(int type)
1069{
1070 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1071 + nla_total_size(4); /* NETCONFA_IFINDEX */
1072 bool all = false;
1073
1074 if (type == NETCONFA_ALL)
1075 all = true;
1076
1077 if (all || type == NETCONFA_INPUT)
1078 size += nla_total_size(4);
1079
1080 return size;
1081}
1082
David Ahern823566a2017-03-28 14:28:06 -07001083static void mpls_netconf_notify_devconf(struct net *net, int event,
1084 int type, struct mpls_dev *mdev)
David Ahern24045a02017-02-20 08:03:30 -08001085{
1086 struct sk_buff *skb;
1087 int err = -ENOBUFS;
1088
1089 skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
1090 if (!skb)
1091 goto errout;
1092
David Ahern823566a2017-03-28 14:28:06 -07001093 err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type);
David Ahern24045a02017-02-20 08:03:30 -08001094 if (err < 0) {
1095 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1096 WARN_ON(err == -EMSGSIZE);
1097 kfree_skb(skb);
1098 goto errout;
1099 }
1100
1101 rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
1102 return;
1103errout:
1104 if (err < 0)
1105 rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
1106}
1107
1108static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
1109 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
1110};
1111
1112static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
David Ahernc21ef3e2017-04-16 09:48:24 -07001113 struct nlmsghdr *nlh,
1114 struct netlink_ext_ack *extack)
David Ahern24045a02017-02-20 08:03:30 -08001115{
1116 struct net *net = sock_net(in_skb->sk);
1117 struct nlattr *tb[NETCONFA_MAX + 1];
1118 struct netconfmsg *ncm;
1119 struct net_device *dev;
1120 struct mpls_dev *mdev;
1121 struct sk_buff *skb;
1122 int ifindex;
1123 int err;
1124
1125 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02001126 devconf_mpls_policy, NULL);
David Ahern24045a02017-02-20 08:03:30 -08001127 if (err < 0)
1128 goto errout;
1129
1130 err = -EINVAL;
1131 if (!tb[NETCONFA_IFINDEX])
1132 goto errout;
1133
1134 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
1135 dev = __dev_get_by_index(net, ifindex);
1136 if (!dev)
1137 goto errout;
1138
1139 mdev = mpls_dev_get(dev);
1140 if (!mdev)
1141 goto errout;
1142
1143 err = -ENOBUFS;
1144 skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
1145 if (!skb)
1146 goto errout;
1147
1148 err = mpls_netconf_fill_devconf(skb, mdev,
1149 NETLINK_CB(in_skb).portid,
1150 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
1151 NETCONFA_ALL);
1152 if (err < 0) {
1153 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1154 WARN_ON(err == -EMSGSIZE);
1155 kfree_skb(skb);
1156 goto errout;
1157 }
1158 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1159errout:
1160 return err;
1161}
1162
1163static int mpls_netconf_dump_devconf(struct sk_buff *skb,
1164 struct netlink_callback *cb)
1165{
1166 struct net *net = sock_net(skb->sk);
1167 struct hlist_head *head;
1168 struct net_device *dev;
1169 struct mpls_dev *mdev;
1170 int idx, s_idx;
1171 int h, s_h;
1172
1173 s_h = cb->args[0];
1174 s_idx = idx = cb->args[1];
1175
1176 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1177 idx = 0;
1178 head = &net->dev_index_head[h];
1179 rcu_read_lock();
1180 cb->seq = net->dev_base_seq;
1181 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1182 if (idx < s_idx)
1183 goto cont;
1184 mdev = mpls_dev_get(dev);
1185 if (!mdev)
1186 goto cont;
1187 if (mpls_netconf_fill_devconf(skb, mdev,
1188 NETLINK_CB(cb->skb).portid,
1189 cb->nlh->nlmsg_seq,
1190 RTM_NEWNETCONF,
1191 NLM_F_MULTI,
1192 NETCONFA_ALL) < 0) {
1193 rcu_read_unlock();
1194 goto done;
1195 }
1196 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1197cont:
1198 idx++;
1199 }
1200 rcu_read_unlock();
1201 }
1202done:
1203 cb->args[0] = h;
1204 cb->args[1] = idx;
1205
1206 return skb->len;
1207}
1208
Robert Shearman37bde792015-04-22 11:14:38 +01001209#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
1210 (&((struct mpls_dev *)0)->field)
1211
David Ahern24045a02017-02-20 08:03:30 -08001212static int mpls_conf_proc(struct ctl_table *ctl, int write,
1213 void __user *buffer,
1214 size_t *lenp, loff_t *ppos)
1215{
1216 int oval = *(int *)ctl->data;
1217 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1218
1219 if (write) {
1220 struct mpls_dev *mdev = ctl->extra1;
1221 int i = (int *)ctl->data - (int *)mdev;
1222 struct net *net = ctl->extra2;
1223 int val = *(int *)ctl->data;
1224
1225 if (i == offsetof(struct mpls_dev, input_enabled) &&
1226 val != oval) {
David Ahern823566a2017-03-28 14:28:06 -07001227 mpls_netconf_notify_devconf(net, RTM_NEWNETCONF,
1228 NETCONFA_INPUT, mdev);
David Ahern24045a02017-02-20 08:03:30 -08001229 }
1230 }
1231
1232 return ret;
1233}
1234
Robert Shearman37bde792015-04-22 11:14:38 +01001235static const struct ctl_table mpls_dev_table[] = {
1236 {
1237 .procname = "input",
1238 .maxlen = sizeof(int),
1239 .mode = 0644,
David Ahern24045a02017-02-20 08:03:30 -08001240 .proc_handler = mpls_conf_proc,
Robert Shearman37bde792015-04-22 11:14:38 +01001241 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
1242 },
1243 { }
1244};
1245
1246static int mpls_dev_sysctl_register(struct net_device *dev,
1247 struct mpls_dev *mdev)
1248{
1249 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
David Ahern24045a02017-02-20 08:03:30 -08001250 struct net *net = dev_net(dev);
Robert Shearman37bde792015-04-22 11:14:38 +01001251 struct ctl_table *table;
1252 int i;
1253
1254 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
1255 if (!table)
1256 goto out;
1257
1258 /* Table data contains only offsets relative to the base of
1259 * the mdev at this point, so make them absolute.
1260 */
David Ahern24045a02017-02-20 08:03:30 -08001261 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) {
Robert Shearman37bde792015-04-22 11:14:38 +01001262 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
David Ahern24045a02017-02-20 08:03:30 -08001263 table[i].extra1 = mdev;
1264 table[i].extra2 = net;
1265 }
Robert Shearman37bde792015-04-22 11:14:38 +01001266
1267 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
1268
David Ahern1182e4d2017-03-28 14:28:07 -07001269 mdev->sysctl = register_net_sysctl(net, path, table);
Robert Shearman37bde792015-04-22 11:14:38 +01001270 if (!mdev->sysctl)
1271 goto free;
1272
David Ahern1182e4d2017-03-28 14:28:07 -07001273 mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev);
Robert Shearman37bde792015-04-22 11:14:38 +01001274 return 0;
1275
1276free:
1277 kfree(table);
1278out:
1279 return -ENOBUFS;
1280}
1281
David Ahern1182e4d2017-03-28 14:28:07 -07001282static void mpls_dev_sysctl_unregister(struct net_device *dev,
1283 struct mpls_dev *mdev)
Robert Shearman37bde792015-04-22 11:14:38 +01001284{
David Ahern1182e4d2017-03-28 14:28:07 -07001285 struct net *net = dev_net(dev);
Robert Shearman37bde792015-04-22 11:14:38 +01001286 struct ctl_table *table;
1287
1288 table = mdev->sysctl->ctl_table_arg;
1289 unregister_net_sysctl_table(mdev->sysctl);
1290 kfree(table);
David Ahern1182e4d2017-03-28 14:28:07 -07001291
1292 mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
Robert Shearman37bde792015-04-22 11:14:38 +01001293}
1294
Robert Shearman03c57742015-04-22 11:14:37 +01001295static struct mpls_dev *mpls_add_dev(struct net_device *dev)
1296{
1297 struct mpls_dev *mdev;
1298 int err = -ENOMEM;
Robert Shearman27d69102017-01-16 14:16:37 +00001299 int i;
Robert Shearman03c57742015-04-22 11:14:37 +01001300
1301 ASSERT_RTNL();
1302
1303 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1304 if (!mdev)
1305 return ERR_PTR(err);
1306
Robert Shearman27d69102017-01-16 14:16:37 +00001307 mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
1308 if (!mdev->stats)
1309 goto free;
1310
1311 for_each_possible_cpu(i) {
1312 struct mpls_pcpu_stats *mpls_stats;
1313
1314 mpls_stats = per_cpu_ptr(mdev->stats, i);
1315 u64_stats_init(&mpls_stats->syncp);
1316 }
1317
David Ahern1182e4d2017-03-28 14:28:07 -07001318 mdev->dev = dev;
1319
Robert Shearman37bde792015-04-22 11:14:38 +01001320 err = mpls_dev_sysctl_register(dev, mdev);
1321 if (err)
1322 goto free;
1323
Robert Shearman03c57742015-04-22 11:14:37 +01001324 rcu_assign_pointer(dev->mpls_ptr, mdev);
1325
1326 return mdev;
Robert Shearman37bde792015-04-22 11:14:38 +01001327
1328free:
Robert Shearman27d69102017-01-16 14:16:37 +00001329 free_percpu(mdev->stats);
Robert Shearman37bde792015-04-22 11:14:38 +01001330 kfree(mdev);
1331 return ERR_PTR(err);
Robert Shearman03c57742015-04-22 11:14:37 +01001332}
1333
Robert Shearman27d69102017-01-16 14:16:37 +00001334static void mpls_dev_destroy_rcu(struct rcu_head *head)
1335{
1336 struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
1337
1338 free_percpu(mdev->stats);
1339 kfree(mdev);
1340}
1341
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001342static void mpls_ifdown(struct net_device *dev, int event)
Eric W. Biederman01891972015-03-03 19:10:47 -06001343{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001344 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -06001345 struct net *net = dev_net(dev);
David Ahern77ef013a2017-03-31 07:14:00 -07001346 u8 alive, deleted;
Eric W. Biederman01891972015-03-03 19:10:47 -06001347 unsigned index;
1348
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001349 platform_label = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06001350 for (index = 0; index < net->mpls.platform_labels; index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001351 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001352
Eric W. Biederman01891972015-03-03 19:10:47 -06001353 if (!rt)
1354 continue;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001355
David Ahern61733c92017-03-13 16:49:10 -07001356 alive = 0;
David Ahern4ea8efa2017-03-24 15:21:57 -07001357 deleted = 0;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001358 change_nexthops(rt) {
David Ahern39eb8cd2017-03-31 07:13:59 -07001359 unsigned int nh_flags = nh->nh_flags;
1360
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001361 if (rtnl_dereference(nh->nh_dev) != dev)
David Ahern61733c92017-03-13 16:49:10 -07001362 goto next;
1363
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001364 switch (event) {
1365 case NETDEV_DOWN:
1366 case NETDEV_UNREGISTER:
David Ahern39eb8cd2017-03-31 07:13:59 -07001367 nh_flags |= RTNH_F_DEAD;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001368 /* fall through */
1369 case NETDEV_CHANGE:
David Ahern39eb8cd2017-03-31 07:13:59 -07001370 nh_flags |= RTNH_F_LINKDOWN;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001371 break;
1372 }
1373 if (event == NETDEV_UNREGISTER)
1374 RCU_INIT_POINTER(nh->nh_dev, NULL);
David Ahern39eb8cd2017-03-31 07:13:59 -07001375
1376 if (nh->nh_flags != nh_flags)
1377 WRITE_ONCE(nh->nh_flags, nh_flags);
David Ahern61733c92017-03-13 16:49:10 -07001378next:
David Ahern39eb8cd2017-03-31 07:13:59 -07001379 if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
David Ahern61733c92017-03-13 16:49:10 -07001380 alive++;
David Ahern4ea8efa2017-03-24 15:21:57 -07001381 if (!rtnl_dereference(nh->nh_dev))
1382 deleted++;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001383 } endfor_nexthops(rt);
David Ahern61733c92017-03-13 16:49:10 -07001384
1385 WRITE_ONCE(rt->rt_nhn_alive, alive);
David Ahern4ea8efa2017-03-24 15:21:57 -07001386
1387 /* if there are no more nexthops, delete the route */
1388 if (event == NETDEV_UNREGISTER && deleted == rt->rt_nhn)
1389 mpls_route_update(net, index, NULL, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001390 }
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001391}
Robert Shearman37bde792015-04-22 11:14:38 +01001392
David Ahern39eb8cd2017-03-31 07:13:59 -07001393static void mpls_ifup(struct net_device *dev, unsigned int flags)
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001394{
1395 struct mpls_route __rcu **platform_label;
1396 struct net *net = dev_net(dev);
1397 unsigned index;
David Ahern77ef013a2017-03-31 07:14:00 -07001398 u8 alive;
Robert Shearman03c57742015-04-22 11:14:37 +01001399
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001400 platform_label = rtnl_dereference(net->mpls.platform_label);
1401 for (index = 0; index < net->mpls.platform_labels; index++) {
1402 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1403
1404 if (!rt)
1405 continue;
1406
1407 alive = 0;
1408 change_nexthops(rt) {
David Ahern39eb8cd2017-03-31 07:13:59 -07001409 unsigned int nh_flags = nh->nh_flags;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001410 struct net_device *nh_dev =
1411 rtnl_dereference(nh->nh_dev);
1412
David Ahern39eb8cd2017-03-31 07:13:59 -07001413 if (!(nh_flags & flags)) {
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001414 alive++;
1415 continue;
1416 }
1417 if (nh_dev != dev)
1418 continue;
1419 alive++;
David Ahern39eb8cd2017-03-31 07:13:59 -07001420 nh_flags &= ~flags;
Roopa Prabhuc2e84712017-05-30 23:50:36 -07001421 WRITE_ONCE(nh->nh_flags, nh_flags);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001422 } endfor_nexthops(rt);
1423
David Ahern39eb8cd2017-03-31 07:13:59 -07001424 WRITE_ONCE(rt->rt_nhn_alive, alive);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001425 }
Eric W. Biederman01891972015-03-03 19:10:47 -06001426}
1427
1428static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
1429 void *ptr)
1430{
1431 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Robert Shearman03c57742015-04-22 11:14:37 +01001432 struct mpls_dev *mdev;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001433 unsigned int flags;
Eric W. Biederman01891972015-03-03 19:10:47 -06001434
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001435 if (event == NETDEV_REGISTER) {
Simon Horman407f31b2016-07-07 07:56:15 +02001436 /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
Simon Horman0d227a82016-06-16 17:09:09 +09001437 if (dev->type == ARPHRD_ETHER ||
1438 dev->type == ARPHRD_LOOPBACK ||
Simon Horman407f31b2016-07-07 07:56:15 +02001439 dev->type == ARPHRD_IPGRE ||
1440 dev->type == ARPHRD_SIT ||
1441 dev->type == ARPHRD_TUNNEL) {
Robert Shearman03c57742015-04-22 11:14:37 +01001442 mdev = mpls_add_dev(dev);
1443 if (IS_ERR(mdev))
1444 return notifier_from_errno(PTR_ERR(mdev));
1445 }
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001446 return NOTIFY_OK;
1447 }
Robert Shearman03c57742015-04-22 11:14:37 +01001448
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001449 mdev = mpls_dev_get(dev);
1450 if (!mdev)
1451 return NOTIFY_OK;
1452
1453 switch (event) {
1454 case NETDEV_DOWN:
1455 mpls_ifdown(dev, event);
1456 break;
1457 case NETDEV_UP:
1458 flags = dev_get_flags(dev);
1459 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1460 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1461 else
1462 mpls_ifup(dev, RTNH_F_DEAD);
1463 break;
1464 case NETDEV_CHANGE:
1465 flags = dev_get_flags(dev);
1466 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1467 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1468 else
1469 mpls_ifdown(dev, event);
1470 break;
Eric W. Biederman01891972015-03-03 19:10:47 -06001471 case NETDEV_UNREGISTER:
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001472 mpls_ifdown(dev, event);
1473 mdev = mpls_dev_get(dev);
1474 if (mdev) {
David Ahern1182e4d2017-03-28 14:28:07 -07001475 mpls_dev_sysctl_unregister(dev, mdev);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001476 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
Robert Shearman27d69102017-01-16 14:16:37 +00001477 call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001478 }
Eric W. Biederman01891972015-03-03 19:10:47 -06001479 break;
Robert Shearman0fae3bf2015-06-11 19:58:26 +01001480 case NETDEV_CHANGENAME:
1481 mdev = mpls_dev_get(dev);
1482 if (mdev) {
1483 int err;
1484
David Ahern1182e4d2017-03-28 14:28:07 -07001485 mpls_dev_sysctl_unregister(dev, mdev);
Robert Shearman0fae3bf2015-06-11 19:58:26 +01001486 err = mpls_dev_sysctl_register(dev, mdev);
1487 if (err)
1488 return notifier_from_errno(err);
1489 }
1490 break;
Eric W. Biederman01891972015-03-03 19:10:47 -06001491 }
1492 return NOTIFY_OK;
1493}
1494
1495static struct notifier_block mpls_dev_notifier = {
1496 .notifier_call = mpls_dev_notify,
1497};
1498
Eric W. Biederman03c05662015-03-03 19:13:56 -06001499static int nla_put_via(struct sk_buff *skb,
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001500 u8 table, const void *addr, int alen)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001501{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001502 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
1503 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
1504 };
Eric W. Biederman03c05662015-03-03 19:13:56 -06001505 struct nlattr *nla;
1506 struct rtvia *via;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001507 int family = AF_UNSPEC;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001508
1509 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1510 if (!nla)
1511 return -EMSGSIZE;
1512
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001513 if (table <= NEIGH_NR_TABLES)
1514 family = table_to_family[table];
1515
Eric W. Biederman03c05662015-03-03 19:13:56 -06001516 via = nla_data(nla);
1517 via->rtvia_family = family;
1518 memcpy(via->rtvia_addr, addr, alen);
1519 return 0;
1520}
1521
Eric W. Biederman966bae32015-03-03 19:13:19 -06001522int nla_put_labels(struct sk_buff *skb, int attrtype,
1523 u8 labels, const u32 label[])
1524{
1525 struct nlattr *nla;
1526 struct mpls_shim_hdr *nla_label;
1527 bool bos;
1528 int i;
1529 nla = nla_reserve(skb, attrtype, labels*4);
1530 if (!nla)
1531 return -EMSGSIZE;
1532
1533 nla_label = nla_data(nla);
1534 bos = true;
1535 for (i = labels - 1; i >= 0; i--) {
1536 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1537 bos = false;
1538 }
1539
1540 return 0;
1541}
Roopa Prabhuface0182015-07-21 10:43:52 +02001542EXPORT_SYMBOL_GPL(nla_put_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001543
1544int nla_get_labels(const struct nlattr *nla,
David Aherna4ac8c92017-03-31 07:14:03 -07001545 u8 max_labels, u8 *labels, u32 label[])
Eric W. Biederman966bae32015-03-03 19:13:19 -06001546{
1547 unsigned len = nla_len(nla);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001548 struct mpls_shim_hdr *nla_label;
David Aherna4ac8c92017-03-31 07:14:03 -07001549 u8 nla_labels;
Eric W. Biederman966bae32015-03-03 19:13:19 -06001550 bool bos;
1551 int i;
1552
David Aherna4ac8c92017-03-31 07:14:03 -07001553 /* len needs to be an even multiple of 4 (the label size). Number
1554 * of labels is a u8 so check for overflow.
1555 */
1556 if (len & 3 || len / 4 > 255)
Eric W. Biederman966bae32015-03-03 19:13:19 -06001557 return -EINVAL;
1558
1559 /* Limit the number of new labels allowed */
1560 nla_labels = len/4;
1561 if (nla_labels > max_labels)
1562 return -EINVAL;
1563
David Aherna4ac8c92017-03-31 07:14:03 -07001564 /* when label == NULL, caller wants number of labels */
1565 if (!label)
1566 goto out;
1567
Eric W. Biederman966bae32015-03-03 19:13:19 -06001568 nla_label = nla_data(nla);
1569 bos = true;
1570 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1571 struct mpls_entry_decoded dec;
1572 dec = mpls_entry_decode(nla_label + i);
1573
1574 /* Ensure the bottom of stack flag is properly set
1575 * and ttl and tc are both clear.
1576 */
1577 if ((dec.bos != bos) || dec.ttl || dec.tc)
1578 return -EINVAL;
1579
Robert Shearman5a9ab012015-04-22 11:14:39 +01001580 switch (dec.label) {
Tom Herbert78f5b892015-05-07 08:08:51 -07001581 case MPLS_LABEL_IMPLNULL:
Robert Shearman5a9ab012015-04-22 11:14:39 +01001582 /* RFC3032: This is a label that an LSR may
1583 * assign and distribute, but which never
1584 * actually appears in the encapsulation.
1585 */
1586 return -EINVAL;
1587 }
1588
Eric W. Biederman966bae32015-03-03 19:13:19 -06001589 label[i] = dec.label;
1590 }
David Aherna4ac8c92017-03-31 07:14:03 -07001591out:
Eric W. Biederman966bae32015-03-03 19:13:19 -06001592 *labels = nla_labels;
1593 return 0;
1594}
Roopa Prabhuface0182015-07-21 10:43:52 +02001595EXPORT_SYMBOL_GPL(nla_get_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001596
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001597int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1598 u8 *via_table, u8 via_addr[])
1599{
1600 struct rtvia *via = nla_data(nla);
1601 int err = -EINVAL;
1602 int alen;
1603
1604 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1605 goto errout;
1606 alen = nla_len(nla) -
1607 offsetof(struct rtvia, rtvia_addr);
1608 if (alen > MAX_VIA_ALEN)
1609 goto errout;
1610
1611 /* Validate the address family */
1612 switch (via->rtvia_family) {
1613 case AF_PACKET:
1614 *via_table = NEIGH_LINK_TABLE;
1615 break;
1616 case AF_INET:
1617 *via_table = NEIGH_ARP_TABLE;
1618 if (alen != 4)
1619 goto errout;
1620 break;
1621 case AF_INET6:
1622 *via_table = NEIGH_ND_TABLE;
1623 if (alen != 16)
1624 goto errout;
1625 break;
1626 default:
1627 /* Unsupported address family */
1628 goto errout;
1629 }
1630
1631 memcpy(via_addr, via->rtvia_addr, alen);
1632 *via_alen = alen;
1633 err = 0;
1634
1635errout:
1636 return err;
1637}
1638
Eric W. Biederman03c05662015-03-03 19:13:56 -06001639static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1640 struct mpls_route_config *cfg)
1641{
1642 struct rtmsg *rtm;
1643 struct nlattr *tb[RTA_MAX+1];
1644 int index;
1645 int err;
1646
Johannes Bergfceb6432017-04-12 14:34:07 +02001647 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy,
1648 NULL);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001649 if (err < 0)
1650 goto errout;
1651
1652 err = -EINVAL;
1653 rtm = nlmsg_data(nlh);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001654
1655 if (rtm->rtm_family != AF_MPLS)
1656 goto errout;
1657 if (rtm->rtm_dst_len != 20)
1658 goto errout;
1659 if (rtm->rtm_src_len != 0)
1660 goto errout;
1661 if (rtm->rtm_tos != 0)
1662 goto errout;
1663 if (rtm->rtm_table != RT_TABLE_MAIN)
1664 goto errout;
1665 /* Any value is acceptable for rtm_protocol */
1666
1667 /* As mpls uses destination specific addresses
1668 * (or source specific address in the case of multicast)
1669 * all addresses have universal scope.
1670 */
1671 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1672 goto errout;
1673 if (rtm->rtm_type != RTN_UNICAST)
1674 goto errout;
1675 if (rtm->rtm_flags != 0)
1676 goto errout;
1677
1678 cfg->rc_label = LABEL_NOT_SPECIFIED;
1679 cfg->rc_protocol = rtm->rtm_protocol;
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001680 cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001681 cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001682 cfg->rc_nlflags = nlh->nlmsg_flags;
1683 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1684 cfg->rc_nlinfo.nlh = nlh;
1685 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1686
1687 for (index = 0; index <= RTA_MAX; index++) {
1688 struct nlattr *nla = tb[index];
1689 if (!nla)
1690 continue;
1691
Suraj Deshmukh14dd3e12016-12-03 07:59:26 +00001692 switch (index) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001693 case RTA_OIF:
1694 cfg->rc_ifindex = nla_get_u32(nla);
1695 break;
1696 case RTA_NEWDST:
1697 if (nla_get_labels(nla, MAX_NEW_LABELS,
1698 &cfg->rc_output_labels,
1699 cfg->rc_output_label))
1700 goto errout;
1701 break;
1702 case RTA_DST:
1703 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001704 u8 label_count;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001705 if (nla_get_labels(nla, 1, &label_count,
1706 &cfg->rc_label))
1707 goto errout;
1708
Robert Shearmana6affd22015-08-03 17:50:04 +01001709 /* Reserved labels may not be set */
1710 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001711 goto errout;
1712
1713 break;
1714 }
1715 case RTA_VIA:
1716 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001717 if (nla_get_via(nla, &cfg->rc_via_alen,
1718 &cfg->rc_via_table, cfg->rc_via))
Robert Shearmanf8d54af2015-03-06 10:47:00 +00001719 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001720 break;
1721 }
1722 case RTA_MULTIPATH:
1723 {
1724 cfg->rc_mp = nla_data(nla);
1725 cfg->rc_mp_len = nla_len(nla);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001726 break;
1727 }
Robert Shearman5b441ac2017-03-10 20:43:24 +00001728 case RTA_TTL_PROPAGATE:
1729 {
1730 u8 ttl_propagate = nla_get_u8(nla);
1731
1732 if (ttl_propagate > 1)
1733 goto errout;
1734 cfg->rc_ttl_propagate = ttl_propagate ?
1735 MPLS_TTL_PROP_ENABLED :
1736 MPLS_TTL_PROP_DISABLED;
1737 break;
1738 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001739 default:
1740 /* Unsupported attribute */
1741 goto errout;
1742 }
1743 }
1744
1745 err = 0;
1746errout:
1747 return err;
1748}
1749
David Ahernc21ef3e2017-04-16 09:48:24 -07001750static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1751 struct netlink_ext_ack *extack)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001752{
David Aherna4ac8c92017-03-31 07:14:03 -07001753 struct mpls_route_config *cfg;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001754 int err;
1755
David Aherna4ac8c92017-03-31 07:14:03 -07001756 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1757 if (!cfg)
1758 return -ENOMEM;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001759
David Aherna4ac8c92017-03-31 07:14:03 -07001760 err = rtm_to_route_config(skb, nlh, cfg);
1761 if (err < 0)
1762 goto out;
1763
1764 err = mpls_route_del(cfg);
1765out:
1766 kfree(cfg);
1767
1768 return err;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001769}
1770
1771
David Ahernc21ef3e2017-04-16 09:48:24 -07001772static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1773 struct netlink_ext_ack *extack)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001774{
David Aherna4ac8c92017-03-31 07:14:03 -07001775 struct mpls_route_config *cfg;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001776 int err;
1777
David Aherna4ac8c92017-03-31 07:14:03 -07001778 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1779 if (!cfg)
1780 return -ENOMEM;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001781
David Aherna4ac8c92017-03-31 07:14:03 -07001782 err = rtm_to_route_config(skb, nlh, cfg);
1783 if (err < 0)
1784 goto out;
1785
1786 err = mpls_route_add(cfg);
1787out:
1788 kfree(cfg);
1789
1790 return err;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001791}
1792
1793static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1794 u32 label, struct mpls_route *rt, int flags)
1795{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001796 struct net_device *dev;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001797 struct nlmsghdr *nlh;
1798 struct rtmsg *rtm;
1799
1800 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1801 if (nlh == NULL)
1802 return -EMSGSIZE;
1803
1804 rtm = nlmsg_data(nlh);
1805 rtm->rtm_family = AF_MPLS;
1806 rtm->rtm_dst_len = 20;
1807 rtm->rtm_src_len = 0;
1808 rtm->rtm_tos = 0;
1809 rtm->rtm_table = RT_TABLE_MAIN;
1810 rtm->rtm_protocol = rt->rt_protocol;
1811 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1812 rtm->rtm_type = RTN_UNICAST;
1813 rtm->rtm_flags = 0;
1814
Eric W. Biederman03c05662015-03-03 19:13:56 -06001815 if (nla_put_labels(skb, RTA_DST, 1, &label))
1816 goto nla_put_failure;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001817
1818 if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
1819 bool ttl_propagate =
1820 rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
1821
1822 if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
1823 ttl_propagate))
1824 goto nla_put_failure;
1825 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001826 if (rt->rt_nhn == 1) {
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001827 const struct mpls_nh *nh = rt->rt_nh;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001828
1829 if (nh->nh_labels &&
1830 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1831 nh->nh_label))
1832 goto nla_put_failure;
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001833 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
Robert Shearman72dcac92015-12-10 19:30:49 +00001834 nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001835 nh->nh_via_alen))
1836 goto nla_put_failure;
1837 dev = rtnl_dereference(nh->nh_dev);
1838 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1839 goto nla_put_failure;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001840 if (nh->nh_flags & RTNH_F_LINKDOWN)
1841 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1842 if (nh->nh_flags & RTNH_F_DEAD)
1843 rtm->rtm_flags |= RTNH_F_DEAD;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001844 } else {
1845 struct rtnexthop *rtnh;
1846 struct nlattr *mp;
David Ahern77ef013a2017-03-31 07:14:00 -07001847 u8 linkdown = 0;
1848 u8 dead = 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001849
1850 mp = nla_nest_start(skb, RTA_MULTIPATH);
1851 if (!mp)
1852 goto nla_put_failure;
1853
1854 for_nexthops(rt) {
David Ahernc00e51d2017-03-24 15:21:56 -07001855 dev = rtnl_dereference(nh->nh_dev);
1856 if (!dev)
1857 continue;
1858
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001859 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1860 if (!rtnh)
1861 goto nla_put_failure;
1862
David Ahernc00e51d2017-03-24 15:21:56 -07001863 rtnh->rtnh_ifindex = dev->ifindex;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001864 if (nh->nh_flags & RTNH_F_LINKDOWN) {
1865 rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
1866 linkdown++;
1867 }
1868 if (nh->nh_flags & RTNH_F_DEAD) {
1869 rtnh->rtnh_flags |= RTNH_F_DEAD;
1870 dead++;
1871 }
1872
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001873 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1874 nh->nh_labels,
1875 nh->nh_label))
1876 goto nla_put_failure;
Robert Shearmanf20367d2015-12-10 19:30:51 +00001877 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1878 nla_put_via(skb, nh->nh_via_table,
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001879 mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001880 nh->nh_via_alen))
1881 goto nla_put_failure;
1882
1883 /* length of rtnetlink header + attributes */
1884 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1885 } endfor_nexthops(rt);
1886
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001887 if (linkdown == rt->rt_nhn)
1888 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1889 if (dead == rt->rt_nhn)
1890 rtm->rtm_flags |= RTNH_F_DEAD;
1891
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001892 nla_nest_end(skb, mp);
1893 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001894
1895 nlmsg_end(skb, nlh);
1896 return 0;
1897
1898nla_put_failure:
1899 nlmsg_cancel(skb, nlh);
1900 return -EMSGSIZE;
1901}
1902
1903static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1904{
1905 struct net *net = sock_net(skb->sk);
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001906 struct mpls_route __rcu **platform_label;
1907 size_t platform_labels;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001908 unsigned int index;
1909
1910 ASSERT_RTNL();
1911
1912 index = cb->args[0];
Robert Shearmana6affd22015-08-03 17:50:04 +01001913 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1914 index = MPLS_LABEL_FIRST_UNRESERVED;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001915
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001916 platform_label = rtnl_dereference(net->mpls.platform_label);
1917 platform_labels = net->mpls.platform_labels;
1918 for (; index < platform_labels; index++) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001919 struct mpls_route *rt;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001920 rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001921 if (!rt)
1922 continue;
1923
1924 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1925 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1926 index, rt, NLM_F_MULTI) < 0)
1927 break;
1928 }
1929 cb->args[0] = index;
1930
1931 return skb->len;
1932}
1933
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001934static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1935{
1936 size_t payload =
1937 NLMSG_ALIGN(sizeof(struct rtmsg))
Robert Shearman5b441ac2017-03-10 20:43:24 +00001938 + nla_total_size(4) /* RTA_DST */
1939 + nla_total_size(1); /* RTA_TTL_PROPAGATE */
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001940
1941 if (rt->rt_nhn == 1) {
1942 struct mpls_nh *nh = rt->rt_nh;
1943
1944 if (nh->nh_dev)
1945 payload += nla_total_size(4); /* RTA_OIF */
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001946 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
Robert Shearman72dcac92015-12-10 19:30:49 +00001947 payload += nla_total_size(2 + nh->nh_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001948 if (nh->nh_labels) /* RTA_NEWDST */
1949 payload += nla_total_size(nh->nh_labels * 4);
1950 } else {
1951 /* each nexthop is packed in an attribute */
1952 size_t nhsize = 0;
1953
1954 for_nexthops(rt) {
David Aherne944e972017-03-28 15:19:49 -07001955 if (!rtnl_dereference(nh->nh_dev))
1956 continue;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001957 nhsize += nla_total_size(sizeof(struct rtnexthop));
Robert Shearmanf20367d2015-12-10 19:30:51 +00001958 /* RTA_VIA */
1959 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
1960 nhsize += nla_total_size(2 + nh->nh_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001961 if (nh->nh_labels)
1962 nhsize += nla_total_size(nh->nh_labels * 4);
1963 } endfor_nexthops(rt);
1964 /* nested attribute */
1965 payload += nla_total_size(nhsize);
1966 }
1967
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001968 return payload;
1969}
1970
1971static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1972 struct nlmsghdr *nlh, struct net *net, u32 portid,
1973 unsigned int nlm_flags)
1974{
1975 struct sk_buff *skb;
1976 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1977 int err = -ENOBUFS;
1978
1979 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1980 if (skb == NULL)
1981 goto errout;
1982
1983 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1984 if (err < 0) {
1985 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1986 WARN_ON(err == -EMSGSIZE);
1987 kfree_skb(skb);
1988 goto errout;
1989 }
1990 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1991
1992 return;
1993errout:
1994 if (err < 0)
1995 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1996}
1997
Eric W. Biederman7720c012015-03-03 19:11:20 -06001998static int resize_platform_label_table(struct net *net, size_t limit)
1999{
2000 size_t size = sizeof(struct mpls_route *) * limit;
2001 size_t old_limit;
2002 size_t cp_size;
2003 struct mpls_route __rcu **labels = NULL, **old;
2004 struct mpls_route *rt0 = NULL, *rt2 = NULL;
2005 unsigned index;
2006
2007 if (size) {
Michal Hocko752ade62017-05-08 15:57:27 -07002008 labels = kvzalloc(size, GFP_KERNEL);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002009 if (!labels)
2010 goto nolabels;
2011 }
2012
2013 /* In case the predefined labels need to be populated */
Tom Herbert78f5b892015-05-07 08:08:51 -07002014 if (limit > MPLS_LABEL_IPV4NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06002015 struct net_device *lo = net->loopback_dev;
David Aherna4ac8c92017-03-31 07:14:03 -07002016 rt0 = mpls_rt_alloc(1, lo->addr_len, 0);
David Aherndf1c6312017-03-31 07:14:02 -07002017 if (IS_ERR(rt0))
Eric W. Biederman7720c012015-03-03 19:11:20 -06002018 goto nort0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07002019 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002020 rt0->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01002021 rt0->rt_payload_type = MPT_IPV4;
Robert Shearman5b441ac2017-03-10 20:43:24 +00002022 rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07002023 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00002024 rt0->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00002025 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
2026 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002027 }
Tom Herbert78f5b892015-05-07 08:08:51 -07002028 if (limit > MPLS_LABEL_IPV6NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06002029 struct net_device *lo = net->loopback_dev;
David Aherna4ac8c92017-03-31 07:14:03 -07002030 rt2 = mpls_rt_alloc(1, lo->addr_len, 0);
David Aherndf1c6312017-03-31 07:14:02 -07002031 if (IS_ERR(rt2))
Eric W. Biederman7720c012015-03-03 19:11:20 -06002032 goto nort2;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07002033 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002034 rt2->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01002035 rt2->rt_payload_type = MPT_IPV6;
David Ahern6a18c312017-03-23 19:02:27 -06002036 rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07002037 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00002038 rt2->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00002039 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
2040 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002041 }
2042
2043 rtnl_lock();
2044 /* Remember the original table */
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002045 old = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002046 old_limit = net->mpls.platform_labels;
2047
2048 /* Free any labels beyond the new table */
2049 for (index = limit; index < old_limit; index++)
Roopa Prabhuf8efb732015-10-23 06:03:27 -07002050 mpls_route_update(net, index, NULL, NULL);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002051
2052 /* Copy over the old labels */
2053 cp_size = size;
2054 if (old_limit < limit)
2055 cp_size = old_limit * sizeof(struct mpls_route *);
2056
2057 memcpy(labels, old, cp_size);
2058
2059 /* If needed set the predefined labels */
Tom Herbert78f5b892015-05-07 08:08:51 -07002060 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
2061 (limit > MPLS_LABEL_IPV6NULL)) {
2062 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002063 rt2 = NULL;
2064 }
2065
Tom Herbert78f5b892015-05-07 08:08:51 -07002066 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
2067 (limit > MPLS_LABEL_IPV4NULL)) {
2068 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002069 rt0 = NULL;
2070 }
2071
2072 /* Update the global pointers */
2073 net->mpls.platform_labels = limit;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002074 rcu_assign_pointer(net->mpls.platform_label, labels);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002075
2076 rtnl_unlock();
2077
2078 mpls_rt_free(rt2);
2079 mpls_rt_free(rt0);
2080
2081 if (old) {
2082 synchronize_rcu();
2083 kvfree(old);
2084 }
2085 return 0;
2086
2087nort2:
2088 mpls_rt_free(rt0);
2089nort0:
2090 kvfree(labels);
2091nolabels:
2092 return -ENOMEM;
2093}
2094
2095static int mpls_platform_labels(struct ctl_table *table, int write,
2096 void __user *buffer, size_t *lenp, loff_t *ppos)
2097{
2098 struct net *net = table->data;
2099 int platform_labels = net->mpls.platform_labels;
2100 int ret;
2101 struct ctl_table tmp = {
2102 .procname = table->procname,
2103 .data = &platform_labels,
2104 .maxlen = sizeof(int),
2105 .mode = table->mode,
2106 .extra1 = &zero,
2107 .extra2 = &label_limit,
2108 };
2109
2110 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2111
2112 if (write && ret == 0)
2113 ret = resize_platform_label_table(net, platform_labels);
2114
2115 return ret;
2116}
2117
Robert Shearman5b441ac2017-03-10 20:43:24 +00002118#define MPLS_NS_SYSCTL_OFFSET(field) \
2119 (&((struct net *)0)->field)
2120
Robert Shearman37bde792015-04-22 11:14:38 +01002121static const struct ctl_table mpls_table[] = {
Eric W. Biederman7720c012015-03-03 19:11:20 -06002122 {
2123 .procname = "platform_labels",
2124 .data = NULL,
2125 .maxlen = sizeof(int),
2126 .mode = 0644,
2127 .proc_handler = mpls_platform_labels,
2128 },
Robert Shearman5b441ac2017-03-10 20:43:24 +00002129 {
2130 .procname = "ip_ttl_propagate",
2131 .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
2132 .maxlen = sizeof(int),
2133 .mode = 0644,
2134 .proc_handler = proc_dointvec_minmax,
2135 .extra1 = &zero,
2136 .extra2 = &one,
2137 },
Robert Shearmana59166e2017-03-10 20:43:25 +00002138 {
2139 .procname = "default_ttl",
2140 .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
2141 .maxlen = sizeof(int),
2142 .mode = 0644,
2143 .proc_handler = proc_dointvec_minmax,
2144 .extra1 = &one,
2145 .extra2 = &ttl_max,
2146 },
Eric W. Biederman7720c012015-03-03 19:11:20 -06002147 { }
2148};
2149
Eric W. Biederman01891972015-03-03 19:10:47 -06002150static int mpls_net_init(struct net *net)
2151{
Eric W. Biederman7720c012015-03-03 19:11:20 -06002152 struct ctl_table *table;
Robert Shearman5b441ac2017-03-10 20:43:24 +00002153 int i;
Eric W. Biederman7720c012015-03-03 19:11:20 -06002154
Eric W. Biederman01891972015-03-03 19:10:47 -06002155 net->mpls.platform_labels = 0;
2156 net->mpls.platform_label = NULL;
Robert Shearman5b441ac2017-03-10 20:43:24 +00002157 net->mpls.ip_ttl_propagate = 1;
Robert Shearmana59166e2017-03-10 20:43:25 +00002158 net->mpls.default_ttl = 255;
Eric W. Biederman01891972015-03-03 19:10:47 -06002159
Eric W. Biederman7720c012015-03-03 19:11:20 -06002160 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
2161 if (table == NULL)
2162 return -ENOMEM;
2163
Robert Shearman5b441ac2017-03-10 20:43:24 +00002164 /* Table data contains only offsets relative to the base of
2165 * the mdev at this point, so make them absolute.
2166 */
2167 for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++)
2168 table[i].data = (char *)net + (uintptr_t)table[i].data;
2169
Eric W. Biederman7720c012015-03-03 19:11:20 -06002170 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07002171 if (net->mpls.ctl == NULL) {
2172 kfree(table);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002173 return -ENOMEM;
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07002174 }
Eric W. Biederman7720c012015-03-03 19:11:20 -06002175
Eric W. Biederman01891972015-03-03 19:10:47 -06002176 return 0;
2177}
2178
2179static void mpls_net_exit(struct net *net)
2180{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002181 struct mpls_route __rcu **platform_label;
2182 size_t platform_labels;
Eric W. Biederman7720c012015-03-03 19:11:20 -06002183 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -06002184 unsigned int index;
2185
Eric W. Biederman7720c012015-03-03 19:11:20 -06002186 table = net->mpls.ctl->ctl_table_arg;
2187 unregister_net_sysctl_table(net->mpls.ctl);
2188 kfree(table);
2189
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002190 /* An rcu grace period has passed since there was a device in
2191 * the network namespace (and thus the last in flight packet)
Eric W. Biederman01891972015-03-03 19:10:47 -06002192 * left this network namespace. This is because
2193 * unregister_netdevice_many and netdev_run_todo has completed
2194 * for each network device that was in this network namespace.
2195 *
2196 * As such no additional rcu synchronization is necessary when
2197 * freeing the platform_label table.
2198 */
2199 rtnl_lock();
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002200 platform_label = rtnl_dereference(net->mpls.platform_label);
2201 platform_labels = net->mpls.platform_labels;
2202 for (index = 0; index < platform_labels; index++) {
2203 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
2204 RCU_INIT_POINTER(platform_label[index], NULL);
David Aherne37791e2017-03-10 09:46:15 -08002205 mpls_notify_route(net, index, rt, NULL, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06002206 mpls_rt_free(rt);
2207 }
2208 rtnl_unlock();
2209
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002210 kvfree(platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06002211}
2212
2213static struct pernet_operations mpls_net_ops = {
2214 .init = mpls_net_init,
2215 .exit = mpls_net_exit,
2216};
2217
Robert Shearman27d69102017-01-16 14:16:37 +00002218static struct rtnl_af_ops mpls_af_ops __read_mostly = {
2219 .family = AF_MPLS,
2220 .fill_stats_af = mpls_fill_stats_af,
2221 .get_stats_af_size = mpls_get_stats_af_size,
2222};
2223
Eric W. Biederman01891972015-03-03 19:10:47 -06002224static int __init mpls_init(void)
2225{
2226 int err;
2227
2228 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
2229
2230 err = register_pernet_subsys(&mpls_net_ops);
2231 if (err)
2232 goto out;
2233
2234 err = register_netdevice_notifier(&mpls_dev_notifier);
2235 if (err)
2236 goto out_unregister_pernet;
2237
2238 dev_add_pack(&mpls_packet_type);
2239
Robert Shearman27d69102017-01-16 14:16:37 +00002240 rtnl_af_register(&mpls_af_ops);
2241
Eric W. Biederman03c05662015-03-03 19:13:56 -06002242 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
2243 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
2244 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
David Ahern24045a02017-02-20 08:03:30 -08002245 rtnl_register(PF_MPLS, RTM_GETNETCONF, mpls_netconf_get_devconf,
2246 mpls_netconf_dump_devconf, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06002247 err = 0;
2248out:
2249 return err;
2250
2251out_unregister_pernet:
2252 unregister_pernet_subsys(&mpls_net_ops);
2253 goto out;
2254}
2255module_init(mpls_init);
2256
2257static void __exit mpls_exit(void)
2258{
Eric W. Biederman03c05662015-03-03 19:13:56 -06002259 rtnl_unregister_all(PF_MPLS);
Robert Shearman27d69102017-01-16 14:16:37 +00002260 rtnl_af_unregister(&mpls_af_ops);
Eric W. Biederman01891972015-03-03 19:10:47 -06002261 dev_remove_pack(&mpls_packet_type);
2262 unregister_netdevice_notifier(&mpls_dev_notifier);
2263 unregister_pernet_subsys(&mpls_net_ops);
2264}
2265module_exit(mpls_exit);
2266
2267MODULE_DESCRIPTION("MultiProtocol Label Switching");
2268MODULE_LICENSE("GPL v2");
2269MODULE_ALIAS_NETPROTO(PF_MPLS);