blob: 0c5d111abe363bd5997eb590b3525d7bad04c43e [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
Robert Shearman1c78efa2015-10-23 06:03:28 -070027/* Maximum number of labels to look ahead at when selecting a path of
28 * a multipath route
29 */
30#define MAX_MP_SELECT_LABELS 4
31
Robert Shearmaneb7809f2015-12-10 19:30:50 +000032#define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
33
Eric W. Biederman7720c012015-03-03 19:11:20 -060034static int zero = 0;
Robert Shearman5b441ac2017-03-10 20:43:24 +000035static int one = 1;
Eric W. Biederman7720c012015-03-03 19:11:20 -060036static int label_limit = (1 << 20) - 1;
Robert Shearmana59166e2017-03-10 20:43:25 +000037static int ttl_max = 255;
Eric W. Biederman7720c012015-03-03 19:11:20 -060038
Eric W. Biederman8de147d2015-03-03 19:14:31 -060039static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
40 struct nlmsghdr *nlh, struct net *net, u32 portid,
41 unsigned int nlm_flags);
42
Eric W. Biederman01891972015-03-03 19:10:47 -060043static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
44{
45 struct mpls_route *rt = NULL;
46
47 if (index < net->mpls.platform_labels) {
48 struct mpls_route __rcu **platform_label =
49 rcu_dereference(net->mpls.platform_label);
50 rt = rcu_dereference(platform_label[index]);
51 }
52 return rt;
53}
54
Roopa Prabhuface0182015-07-21 10:43:52 +020055bool mpls_output_possible(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060056{
57 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
58}
Roopa Prabhuface0182015-07-21 10:43:52 +020059EXPORT_SYMBOL_GPL(mpls_output_possible);
Eric W. Biederman01891972015-03-03 19:10:47 -060060
Robert Shearmancf4b24f2015-10-27 00:37:36 +000061static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
62{
63 u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
64 int nh_index = nh - rt->rt_nh;
65
66 return nh0_via + rt->rt_max_alen * nh_index;
67}
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
192static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
David Ahern9f427a0e2017-01-20 12:58:34 -0800193 struct sk_buff *skb)
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800194{
195 int alive = ACCESS_ONCE(rt->rt_nhn_alive);
196 u32 hash = 0;
197 int nh_index = 0;
198 int n = 0;
199
200 /* No need to look further into packet if there's only
201 * one path
202 */
203 if (rt->rt_nhn == 1)
204 goto out;
205
206 if (alive <= 0)
207 return NULL;
208
David Ahern9f427a0e2017-01-20 12:58:34 -0800209 hash = mpls_multipath_hash(rt, skb);
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800210 nh_index = hash % alive;
211 if (alive == rt->rt_nhn)
212 goto out;
213 for_nexthops(rt) {
214 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
215 continue;
216 if (n == nh_index)
217 return nh;
218 n++;
219 } endfor_nexthops(rt);
220
Robert Shearman1c78efa2015-10-23 06:03:28 -0700221out:
222 return &rt->rt_nh[nh_index];
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700223}
224
Robert Shearman5b441ac2017-03-10 20:43:24 +0000225static bool mpls_egress(struct net *net, struct mpls_route *rt,
226 struct sk_buff *skb, struct mpls_entry_decoded dec)
Eric W. Biederman01891972015-03-03 19:10:47 -0600227{
Robert Shearman118d5232015-08-06 11:04:56 +0100228 enum mpls_payload_type payload_type;
229 bool success = false;
Eric W. Biederman01891972015-03-03 19:10:47 -0600230
Eric W. Biederman76fecd82015-03-12 18:22:59 -0500231 /* The IPv4 code below accesses through the IPv4 header
232 * checksum, which is 12 bytes into the packet.
233 * The IPv6 code below accesses through the IPv6 hop limit
234 * which is 8 bytes into the packet.
235 *
236 * For all supported cases there should always be at least 12
237 * bytes of packet data present. The IPv4 header is 20 bytes
238 * without options and the IPv6 header is always 40 bytes
239 * long.
240 */
241 if (!pskb_may_pull(skb, 12))
242 return false;
243
Robert Shearman118d5232015-08-06 11:04:56 +0100244 payload_type = rt->rt_payload_type;
245 if (payload_type == MPT_UNSPEC)
246 payload_type = ip_hdr(skb)->version;
247
248 switch (payload_type) {
249 case MPT_IPV4: {
250 struct iphdr *hdr4 = ip_hdr(skb);
Robert Shearman5b441ac2017-03-10 20:43:24 +0000251 u8 new_ttl;
Eric W. Biederman01891972015-03-03 19:10:47 -0600252 skb->protocol = htons(ETH_P_IP);
Robert Shearman5b441ac2017-03-10 20:43:24 +0000253
254 /* If propagating TTL, take the decremented TTL from
255 * the incoming MPLS header, otherwise decrement the
256 * TTL, but only if not 0 to avoid underflow.
257 */
258 if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
259 (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
260 net->mpls.ip_ttl_propagate))
261 new_ttl = dec.ttl;
262 else
263 new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
264
Eric W. Biederman01891972015-03-03 19:10:47 -0600265 csum_replace2(&hdr4->check,
266 htons(hdr4->ttl << 8),
Robert Shearman5b441ac2017-03-10 20:43:24 +0000267 htons(new_ttl << 8));
268 hdr4->ttl = new_ttl;
Robert Shearman118d5232015-08-06 11:04:56 +0100269 success = true;
270 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600271 }
Robert Shearman118d5232015-08-06 11:04:56 +0100272 case MPT_IPV6: {
Eric W. Biederman01891972015-03-03 19:10:47 -0600273 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
274 skb->protocol = htons(ETH_P_IPV6);
Robert Shearman5b441ac2017-03-10 20:43:24 +0000275
276 /* If propagating TTL, take the decremented TTL from
277 * the incoming MPLS header, otherwise decrement the
278 * hop limit, but only if not 0 to avoid underflow.
279 */
280 if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
281 (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
282 net->mpls.ip_ttl_propagate))
283 hdr6->hop_limit = dec.ttl;
284 else if (hdr6->hop_limit)
285 hdr6->hop_limit = hdr6->hop_limit - 1;
Robert Shearman118d5232015-08-06 11:04:56 +0100286 success = true;
287 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600288 }
Robert Shearman118d5232015-08-06 11:04:56 +0100289 case MPT_UNSPEC:
Robert Shearman5b441ac2017-03-10 20:43:24 +0000290 /* Should have decided which protocol it is by now */
Robert Shearman118d5232015-08-06 11:04:56 +0100291 break;
292 }
293
Eric W. Biederman01891972015-03-03 19:10:47 -0600294 return success;
295}
296
297static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
298 struct packet_type *pt, struct net_device *orig_dev)
299{
300 struct net *net = dev_net(dev);
301 struct mpls_shim_hdr *hdr;
302 struct mpls_route *rt;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700303 struct mpls_nh *nh;
Eric W. Biederman01891972015-03-03 19:10:47 -0600304 struct mpls_entry_decoded dec;
305 struct net_device *out_dev;
Robert Shearman27d69102017-01-16 14:16:37 +0000306 struct mpls_dev *out_mdev;
Robert Shearman03c57742015-04-22 11:14:37 +0100307 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600308 unsigned int hh_len;
309 unsigned int new_header_size;
310 unsigned int mtu;
311 int err;
312
313 /* Careful this entire function runs inside of an rcu critical section */
314
Robert Shearman03c57742015-04-22 11:14:37 +0100315 mdev = mpls_dev_get(dev);
Robert Shearman27d69102017-01-16 14:16:37 +0000316 if (!mdev)
Robert Shearman03c57742015-04-22 11:14:37 +0100317 goto drop;
318
Robert Shearman27d69102017-01-16 14:16:37 +0000319 MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
320 rx_bytes);
321
322 if (!mdev->input_enabled) {
323 MPLS_INC_STATS(mdev, rx_dropped);
324 goto drop;
325 }
326
Eric W. Biederman01891972015-03-03 19:10:47 -0600327 if (skb->pkt_type != PACKET_HOST)
Robert Shearman27d69102017-01-16 14:16:37 +0000328 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600329
330 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
Robert Shearman27d69102017-01-16 14:16:37 +0000331 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600332
333 if (!pskb_may_pull(skb, sizeof(*hdr)))
Robert Shearman27d69102017-01-16 14:16:37 +0000334 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600335
336 /* Read and decode the label */
337 hdr = mpls_hdr(skb);
338 dec = mpls_entry_decode(hdr);
339
Eric W. Biederman01891972015-03-03 19:10:47 -0600340 rt = mpls_route_input_rcu(net, dec.label);
Robert Shearman27d69102017-01-16 14:16:37 +0000341 if (!rt) {
342 MPLS_INC_STATS(mdev, rx_noroute);
Eric W. Biederman01891972015-03-03 19:10:47 -0600343 goto drop;
Robert Shearman27d69102017-01-16 14:16:37 +0000344 }
Eric W. Biederman01891972015-03-03 19:10:47 -0600345
David Ahern9f427a0e2017-01-20 12:58:34 -0800346 nh = mpls_select_multipath(rt, skb);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700347 if (!nh)
Robert Shearman27d69102017-01-16 14:16:37 +0000348 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600349
David Ahern9f427a0e2017-01-20 12:58:34 -0800350 /* Pop the label */
351 skb_pull(skb, sizeof(*hdr));
352 skb_reset_network_header(skb);
353
354 skb_orphan(skb);
355
Eric W. Biederman01891972015-03-03 19:10:47 -0600356 if (skb_warn_if_lro(skb))
Robert Shearman27d69102017-01-16 14:16:37 +0000357 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600358
359 skb_forward_csum(skb);
360
361 /* Verify ttl is valid */
Eric W. Biedermanaa7da932015-03-07 16:23:23 -0600362 if (dec.ttl <= 1)
Robert Shearman27d69102017-01-16 14:16:37 +0000363 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600364 dec.ttl -= 1;
365
Robert Shearman27d69102017-01-16 14:16:37 +0000366 /* Find the output device */
367 out_dev = rcu_dereference(nh->nh_dev);
368 if (!mpls_output_possible(out_dev))
369 goto tx_err;
370
Eric W. Biederman01891972015-03-03 19:10:47 -0600371 /* Verify the destination can hold the packet */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700372 new_header_size = mpls_nh_header_size(nh);
Eric W. Biederman01891972015-03-03 19:10:47 -0600373 mtu = mpls_dev_mtu(out_dev);
374 if (mpls_pkt_too_big(skb, mtu - new_header_size))
Robert Shearman27d69102017-01-16 14:16:37 +0000375 goto tx_err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600376
377 hh_len = LL_RESERVED_SPACE(out_dev);
378 if (!out_dev->header_ops)
379 hh_len = 0;
380
381 /* Ensure there is enough space for the headers in the skb */
382 if (skb_cow(skb, hh_len + new_header_size))
Robert Shearman27d69102017-01-16 14:16:37 +0000383 goto tx_err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600384
385 skb->dev = out_dev;
386 skb->protocol = htons(ETH_P_MPLS_UC);
387
388 if (unlikely(!new_header_size && dec.bos)) {
389 /* Penultimate hop popping */
Robert Shearman5b441ac2017-03-10 20:43:24 +0000390 if (!mpls_egress(dev_net(out_dev), rt, skb, dec))
Robert Shearman27d69102017-01-16 14:16:37 +0000391 goto err;
Eric W. Biederman01891972015-03-03 19:10:47 -0600392 } else {
393 bool bos;
394 int i;
395 skb_push(skb, new_header_size);
396 skb_reset_network_header(skb);
397 /* Push the new labels */
398 hdr = mpls_hdr(skb);
399 bos = dec.bos;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700400 for (i = nh->nh_labels - 1; i >= 0; i--) {
401 hdr[i] = mpls_entry_encode(nh->nh_label[i],
402 dec.ttl, 0, bos);
Eric W. Biederman01891972015-03-03 19:10:47 -0600403 bos = false;
404 }
405 }
406
Robert Shearman27d69102017-01-16 14:16:37 +0000407 mpls_stats_inc_outucastpkts(out_dev, skb);
408
Robert Shearmaneb7809f2015-12-10 19:30:50 +0000409 /* If via wasn't specified then send out using device address */
410 if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
411 err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
412 out_dev->dev_addr, skb);
413 else
414 err = neigh_xmit(nh->nh_via_table, out_dev,
415 mpls_nh_via(rt, nh), skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600416 if (err)
417 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
418 __func__, err);
419 return 0;
420
Robert Shearman27d69102017-01-16 14:16:37 +0000421tx_err:
422 out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
423 if (out_mdev)
424 MPLS_INC_STATS(out_mdev, tx_errors);
425 goto drop;
426err:
427 MPLS_INC_STATS(mdev, rx_errors);
Eric W. Biederman01891972015-03-03 19:10:47 -0600428drop:
429 kfree_skb(skb);
430 return NET_RX_DROP;
431}
432
433static struct packet_type mpls_packet_type __read_mostly = {
434 .type = cpu_to_be16(ETH_P_MPLS_UC),
435 .func = mpls_forward,
436};
437
Wu Fengguangf0126532015-03-05 05:33:54 +0800438static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600439 [RTA_DST] = { .type = NLA_U32 },
440 [RTA_OIF] = { .type = NLA_U32 },
Robert Shearman5b441ac2017-03-10 20:43:24 +0000441 [RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
Eric W. Biederman03c05662015-03-03 19:13:56 -0600442};
443
Eric W. Biedermana2519922015-03-03 19:12:40 -0600444struct mpls_route_config {
Robert Shearman118d5232015-08-06 11:04:56 +0100445 u32 rc_protocol;
446 u32 rc_ifindex;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700447 u8 rc_via_table;
448 u8 rc_via_alen;
Robert Shearman118d5232015-08-06 11:04:56 +0100449 u8 rc_via[MAX_VIA_ALEN];
450 u32 rc_label;
Robert Shearman5b441ac2017-03-10 20:43:24 +0000451 u8 rc_ttl_propagate;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700452 u8 rc_output_labels;
Robert Shearman118d5232015-08-06 11:04:56 +0100453 u32 rc_output_label[MAX_NEW_LABELS];
454 u32 rc_nlflags;
455 enum mpls_payload_type rc_payload_type;
456 struct nl_info rc_nlinfo;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700457 struct rtnexthop *rc_mp;
458 int rc_mp_len;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600459};
460
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000461static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
Eric W. Biederman01891972015-03-03 19:10:47 -0600462{
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000463 u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
Eric W. Biederman01891972015-03-03 19:10:47 -0600464 struct mpls_route *rt;
465
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000466 rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
467 VIA_ALEN_ALIGN) +
468 num_nh * max_alen_aligned,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700469 GFP_KERNEL);
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000470 if (rt) {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700471 rt->rt_nhn = num_nh;
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800472 rt->rt_nhn_alive = num_nh;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000473 rt->rt_max_alen = max_alen_aligned;
474 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700475
Eric W. Biederman01891972015-03-03 19:10:47 -0600476 return rt;
477}
478
479static void mpls_rt_free(struct mpls_route *rt)
480{
481 if (rt)
482 kfree_rcu(rt, rt_rcu);
483}
484
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600485static void mpls_notify_route(struct net *net, unsigned index,
486 struct mpls_route *old, struct mpls_route *new,
487 const struct nl_info *info)
488{
489 struct nlmsghdr *nlh = info ? info->nlh : NULL;
490 unsigned portid = info ? info->portid : 0;
491 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
492 struct mpls_route *rt = new ? new : old;
493 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
494 /* Ignore reserved labels for now */
Robert Shearmana6affd22015-08-03 17:50:04 +0100495 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600496 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
497}
498
Eric W. Biederman01891972015-03-03 19:10:47 -0600499static void mpls_route_update(struct net *net, unsigned index,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700500 struct mpls_route *new,
Eric W. Biederman01891972015-03-03 19:10:47 -0600501 const struct nl_info *info)
502{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600503 struct mpls_route __rcu **platform_label;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700504 struct mpls_route *rt;
Eric W. Biederman01891972015-03-03 19:10:47 -0600505
506 ASSERT_RTNL();
507
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600508 platform_label = rtnl_dereference(net->mpls.platform_label);
509 rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700510 rcu_assign_pointer(platform_label[index], new);
Eric W. Biederman01891972015-03-03 19:10:47 -0600511
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700512 mpls_notify_route(net, index, rt, new, info);
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600513
Eric W. Biederman01891972015-03-03 19:10:47 -0600514 /* If we removed a route free it now */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700515 mpls_rt_free(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -0600516}
517
Eric W. Biedermana2519922015-03-03 19:12:40 -0600518static unsigned find_free_label(struct net *net)
519{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600520 struct mpls_route __rcu **platform_label;
521 size_t platform_labels;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600522 unsigned index;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600523
524 platform_label = rtnl_dereference(net->mpls.platform_label);
525 platform_labels = net->mpls.platform_labels;
Robert Shearmana6affd22015-08-03 17:50:04 +0100526 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
527 index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600528 if (!rtnl_dereference(platform_label[index]))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600529 return index;
530 }
531 return LABEL_NOT_SPECIFIED;
532}
533
Roopa Prabhubf215632015-07-30 13:34:54 -0700534#if IS_ENABLED(CONFIG_INET)
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000535static struct net_device *inet_fib_lookup_dev(struct net *net,
536 const void *addr)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700537{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300538 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700539 struct rtable *rt;
540 struct in_addr daddr;
541
542 memcpy(&daddr, addr, sizeof(struct in_addr));
543 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
544 if (IS_ERR(rt))
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300545 return ERR_CAST(rt);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700546
547 dev = rt->dst.dev;
548 dev_hold(dev);
549
550 ip_rt_put(rt);
551
Roopa Prabhu01faef22015-07-21 09:16:24 -0700552 return dev;
553}
Roopa Prabhubf215632015-07-30 13:34:54 -0700554#else
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000555static struct net_device *inet_fib_lookup_dev(struct net *net,
556 const void *addr)
Roopa Prabhubf215632015-07-30 13:34:54 -0700557{
558 return ERR_PTR(-EAFNOSUPPORT);
559}
560#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700561
Roopa Prabhubf215632015-07-30 13:34:54 -0700562#if IS_ENABLED(CONFIG_IPV6)
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000563static struct net_device *inet6_fib_lookup_dev(struct net *net,
564 const void *addr)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700565{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300566 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700567 struct dst_entry *dst;
568 struct flowi6 fl6;
Roopa Prabhubf215632015-07-30 13:34:54 -0700569 int err;
570
571 if (!ipv6_stub)
572 return ERR_PTR(-EAFNOSUPPORT);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700573
574 memset(&fl6, 0, sizeof(fl6));
575 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
Roopa Prabhubf215632015-07-30 13:34:54 -0700576 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
577 if (err)
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300578 return ERR_PTR(err);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700579
580 dev = dst->dev;
581 dev_hold(dev);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700582 dst_release(dst);
583
584 return dev;
585}
Roopa Prabhubf215632015-07-30 13:34:54 -0700586#else
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000587static struct net_device *inet6_fib_lookup_dev(struct net *net,
588 const void *addr)
Roopa Prabhubf215632015-07-30 13:34:54 -0700589{
590 return ERR_PTR(-EAFNOSUPPORT);
591}
592#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700593
594static struct net_device *find_outdev(struct net *net,
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000595 struct mpls_route *rt,
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700596 struct mpls_nh *nh, int oif)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700597{
598 struct net_device *dev = NULL;
599
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700600 if (!oif) {
601 switch (nh->nh_via_table) {
Roopa Prabhu01faef22015-07-21 09:16:24 -0700602 case NEIGH_ARP_TABLE:
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000603 dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
Roopa Prabhu01faef22015-07-21 09:16:24 -0700604 break;
605 case NEIGH_ND_TABLE:
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000606 dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
Roopa Prabhu01faef22015-07-21 09:16:24 -0700607 break;
608 case NEIGH_LINK_TABLE:
609 break;
610 }
611 } else {
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700612 dev = dev_get_by_index(net, oif);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700613 }
614
Roopa Prabhu3dcb6152015-08-04 06:36:24 -0700615 if (!dev)
616 return ERR_PTR(-ENODEV);
617
Roopa Prabhu94a57f12016-04-07 21:28:38 -0700618 if (IS_ERR(dev))
619 return dev;
620
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700621 /* The caller is holding rtnl anyways, so release the dev reference */
622 dev_put(dev);
623
Roopa Prabhu01faef22015-07-21 09:16:24 -0700624 return dev;
625}
626
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000627static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
628 struct mpls_nh *nh, int oif)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700629{
630 struct net_device *dev = NULL;
631 int err = -ENODEV;
632
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000633 dev = find_outdev(net, rt, nh, oif);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700634 if (IS_ERR(dev)) {
635 err = PTR_ERR(dev);
636 dev = NULL;
637 goto errout;
638 }
639
640 /* Ensure this is a supported device */
641 err = -EINVAL;
642 if (!mpls_dev_get(dev))
643 goto errout;
644
Robert Shearmana3e948e2015-12-10 19:30:48 +0000645 if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
646 (dev->addr_len != nh->nh_via_alen))
647 goto errout;
648
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700649 RCU_INIT_POINTER(nh->nh_dev, dev);
650
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800651 if (!(dev->flags & IFF_UP)) {
652 nh->nh_flags |= RTNH_F_DEAD;
653 } else {
654 unsigned int flags;
655
656 flags = dev_get_flags(dev);
657 if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
658 nh->nh_flags |= RTNH_F_LINKDOWN;
659 }
660
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700661 return 0;
662
663errout:
664 return err;
665}
666
667static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
668 struct mpls_route *rt)
669{
670 struct net *net = cfg->rc_nlinfo.nl_net;
671 struct mpls_nh *nh = rt->rt_nh;
672 int err;
673 int i;
674
675 if (!nh)
676 return -ENOMEM;
677
678 err = -EINVAL;
679 /* Ensure only a supported number of labels are present */
680 if (cfg->rc_output_labels > MAX_NEW_LABELS)
681 goto errout;
682
683 nh->nh_labels = cfg->rc_output_labels;
684 for (i = 0; i < nh->nh_labels; i++)
685 nh->nh_label[i] = cfg->rc_output_label[i];
686
687 nh->nh_via_table = cfg->rc_via_table;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000688 memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700689 nh->nh_via_alen = cfg->rc_via_alen;
690
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000691 err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700692 if (err)
693 goto errout;
694
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800695 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
696 rt->rt_nhn_alive--;
697
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700698 return 0;
699
700errout:
701 return err;
702}
703
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000704static int mpls_nh_build(struct net *net, struct mpls_route *rt,
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800705 struct mpls_nh *nh, int oif, struct nlattr *via,
706 struct nlattr *newdst)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700707{
708 int err = -ENOMEM;
709
710 if (!nh)
711 goto errout;
712
713 if (newdst) {
714 err = nla_get_labels(newdst, MAX_NEW_LABELS,
715 &nh->nh_labels, nh->nh_label);
716 if (err)
717 goto errout;
718 }
719
Robert Shearmanf20367d2015-12-10 19:30:51 +0000720 if (via) {
721 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
722 __mpls_nh_via(rt, nh));
723 if (err)
724 goto errout;
725 } else {
726 nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
727 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700728
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000729 err = mpls_nh_assign_dev(net, rt, nh, oif);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700730 if (err)
731 goto errout;
732
733 return 0;
734
735errout:
736 return err;
737}
738
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000739static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
740 u8 cfg_via_alen, u8 *max_via_alen)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700741{
742 int nhs = 0;
743 int remaining = len;
744
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000745 if (!rtnh) {
746 *max_via_alen = cfg_via_alen;
747 return 1;
748 }
749
750 *max_via_alen = 0;
751
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700752 while (rtnh_ok(rtnh, remaining)) {
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000753 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
754 int attrlen;
755
756 attrlen = rtnh_attrlen(rtnh);
757 nla = nla_find(attrs, attrlen, RTA_VIA);
758 if (nla && nla_len(nla) >=
759 offsetof(struct rtvia, rtvia_addr)) {
760 int via_alen = nla_len(nla) -
761 offsetof(struct rtvia, rtvia_addr);
762
763 if (via_alen <= MAX_VIA_ALEN)
764 *max_via_alen = max_t(u16, *max_via_alen,
765 via_alen);
766 }
767
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700768 nhs++;
769 rtnh = rtnh_next(rtnh, &remaining);
770 }
771
772 /* leftover implies invalid nexthop configuration, discard it */
773 return remaining > 0 ? 0 : nhs;
774}
775
776static int mpls_nh_build_multi(struct mpls_route_config *cfg,
777 struct mpls_route *rt)
778{
779 struct rtnexthop *rtnh = cfg->rc_mp;
780 struct nlattr *nla_via, *nla_newdst;
781 int remaining = cfg->rc_mp_len;
782 int nhs = 0;
783 int err = 0;
784
785 change_nexthops(rt) {
786 int attrlen;
787
788 nla_via = NULL;
789 nla_newdst = NULL;
790
791 err = -EINVAL;
792 if (!rtnh_ok(rtnh, remaining))
793 goto errout;
794
Robert Shearman1c78efa2015-10-23 06:03:28 -0700795 /* neither weighted multipath nor any flags
796 * are supported
797 */
798 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
799 goto errout;
800
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700801 attrlen = rtnh_attrlen(rtnh);
802 if (attrlen > 0) {
803 struct nlattr *attrs = rtnh_attrs(rtnh);
804
805 nla_via = nla_find(attrs, attrlen, RTA_VIA);
806 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
807 }
808
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000809 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800810 rtnh->rtnh_ifindex, nla_via, nla_newdst);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700811 if (err)
812 goto errout;
813
Roopa Prabhuc89359a2015-12-01 22:18:11 -0800814 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
815 rt->rt_nhn_alive--;
816
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700817 rtnh = rtnh_next(rtnh, &remaining);
818 nhs++;
819 } endfor_nexthops(rt);
820
821 rt->rt_nhn = nhs;
822
823 return 0;
824
825errout:
826 return err;
827}
828
Eric W. Biedermana2519922015-03-03 19:12:40 -0600829static int mpls_route_add(struct mpls_route_config *cfg)
830{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600831 struct mpls_route __rcu **platform_label;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600832 struct net *net = cfg->rc_nlinfo.nl_net;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600833 struct mpls_route *rt, *old;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600834 int err = -EINVAL;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000835 u8 max_via_alen;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700836 unsigned index;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000837 int nhs;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600838
839 index = cfg->rc_label;
840
841 /* If a label was not specified during insert pick one */
842 if ((index == LABEL_NOT_SPECIFIED) &&
843 (cfg->rc_nlflags & NLM_F_CREATE)) {
844 index = find_free_label(net);
845 }
846
Robert Shearmana6affd22015-08-03 17:50:04 +0100847 /* Reserved labels may not be set */
848 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600849 goto errout;
850
851 /* The full 20 bit range may not be supported. */
852 if (index >= net->mpls.platform_labels)
853 goto errout;
854
Eric W. Biedermana2519922015-03-03 19:12:40 -0600855 /* Append makes no sense with mpls */
Eric W. Biederman0f7bbd52015-03-07 16:22:40 -0600856 err = -EOPNOTSUPP;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600857 if (cfg->rc_nlflags & NLM_F_APPEND)
858 goto errout;
859
860 err = -EEXIST;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600861 platform_label = rtnl_dereference(net->mpls.platform_label);
862 old = rtnl_dereference(platform_label[index]);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600863 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
864 goto errout;
865
866 err = -EEXIST;
867 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
868 goto errout;
869
870 err = -ENOENT;
871 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
872 goto errout;
873
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000874 err = -EINVAL;
875 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
876 cfg->rc_via_alen, &max_via_alen);
877 if (nhs == 0)
878 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700879
Eric W. Biedermana2519922015-03-03 19:12:40 -0600880 err = -ENOMEM;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000881 rt = mpls_rt_alloc(nhs, max_via_alen);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600882 if (!rt)
883 goto errout;
884
Eric W. Biedermana2519922015-03-03 19:12:40 -0600885 rt->rt_protocol = cfg->rc_protocol;
Robert Shearman118d5232015-08-06 11:04:56 +0100886 rt->rt_payload_type = cfg->rc_payload_type;
Robert Shearman5b441ac2017-03-10 20:43:24 +0000887 rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600888
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700889 if (cfg->rc_mp)
890 err = mpls_nh_build_multi(cfg, rt);
891 else
892 err = mpls_nh_build_from_cfg(cfg, rt);
893 if (err)
894 goto freert;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600895
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700896 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
897
Eric W. Biedermana2519922015-03-03 19:12:40 -0600898 return 0;
899
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700900freert:
901 mpls_rt_free(rt);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600902errout:
Eric W. Biedermana2519922015-03-03 19:12:40 -0600903 return err;
904}
905
906static int mpls_route_del(struct mpls_route_config *cfg)
907{
908 struct net *net = cfg->rc_nlinfo.nl_net;
909 unsigned index;
910 int err = -EINVAL;
911
912 index = cfg->rc_label;
913
Robert Shearmana6affd22015-08-03 17:50:04 +0100914 /* Reserved labels may not be removed */
915 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600916 goto errout;
917
918 /* The full 20 bit range may not be supported */
919 if (index >= net->mpls.platform_labels)
920 goto errout;
921
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700922 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600923
924 err = 0;
925errout:
926 return err;
927}
928
Robert Shearman27d69102017-01-16 14:16:37 +0000929static void mpls_get_stats(struct mpls_dev *mdev,
930 struct mpls_link_stats *stats)
931{
932 struct mpls_pcpu_stats *p;
933 int i;
934
935 memset(stats, 0, sizeof(*stats));
936
937 for_each_possible_cpu(i) {
938 struct mpls_link_stats local;
939 unsigned int start;
940
941 p = per_cpu_ptr(mdev->stats, i);
942 do {
943 start = u64_stats_fetch_begin(&p->syncp);
944 local = p->stats;
945 } while (u64_stats_fetch_retry(&p->syncp, start));
946
947 stats->rx_packets += local.rx_packets;
948 stats->rx_bytes += local.rx_bytes;
949 stats->tx_packets += local.tx_packets;
950 stats->tx_bytes += local.tx_bytes;
951 stats->rx_errors += local.rx_errors;
952 stats->tx_errors += local.tx_errors;
953 stats->rx_dropped += local.rx_dropped;
954 stats->tx_dropped += local.tx_dropped;
955 stats->rx_noroute += local.rx_noroute;
956 }
957}
958
959static int mpls_fill_stats_af(struct sk_buff *skb,
960 const struct net_device *dev)
961{
962 struct mpls_link_stats *stats;
963 struct mpls_dev *mdev;
964 struct nlattr *nla;
965
966 mdev = mpls_dev_get(dev);
967 if (!mdev)
968 return -ENODATA;
969
970 nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
971 sizeof(struct mpls_link_stats),
972 MPLS_STATS_UNSPEC);
973 if (!nla)
974 return -EMSGSIZE;
975
976 stats = nla_data(nla);
977 mpls_get_stats(mdev, stats);
978
979 return 0;
980}
981
982static size_t mpls_get_stats_af_size(const struct net_device *dev)
983{
984 struct mpls_dev *mdev;
985
986 mdev = mpls_dev_get(dev);
987 if (!mdev)
988 return 0;
989
990 return nla_total_size_64bit(sizeof(struct mpls_link_stats));
991}
992
David Ahern24045a02017-02-20 08:03:30 -0800993static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
994 u32 portid, u32 seq, int event,
995 unsigned int flags, int type)
996{
997 struct nlmsghdr *nlh;
998 struct netconfmsg *ncm;
999 bool all = false;
1000
1001 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1002 flags);
1003 if (!nlh)
1004 return -EMSGSIZE;
1005
1006 if (type == NETCONFA_ALL)
1007 all = true;
1008
1009 ncm = nlmsg_data(nlh);
1010 ncm->ncm_family = AF_MPLS;
1011
1012 if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
1013 goto nla_put_failure;
1014
1015 if ((all || type == NETCONFA_INPUT) &&
1016 nla_put_s32(skb, NETCONFA_INPUT,
1017 mdev->input_enabled) < 0)
1018 goto nla_put_failure;
1019
1020 nlmsg_end(skb, nlh);
1021 return 0;
1022
1023nla_put_failure:
1024 nlmsg_cancel(skb, nlh);
1025 return -EMSGSIZE;
1026}
1027
1028static int mpls_netconf_msgsize_devconf(int type)
1029{
1030 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1031 + nla_total_size(4); /* NETCONFA_IFINDEX */
1032 bool all = false;
1033
1034 if (type == NETCONFA_ALL)
1035 all = true;
1036
1037 if (all || type == NETCONFA_INPUT)
1038 size += nla_total_size(4);
1039
1040 return size;
1041}
1042
1043static void mpls_netconf_notify_devconf(struct net *net, int type,
1044 struct mpls_dev *mdev)
1045{
1046 struct sk_buff *skb;
1047 int err = -ENOBUFS;
1048
1049 skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
1050 if (!skb)
1051 goto errout;
1052
1053 err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, RTM_NEWNETCONF,
1054 0, type);
1055 if (err < 0) {
1056 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1057 WARN_ON(err == -EMSGSIZE);
1058 kfree_skb(skb);
1059 goto errout;
1060 }
1061
1062 rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
1063 return;
1064errout:
1065 if (err < 0)
1066 rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
1067}
1068
1069static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
1070 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
1071};
1072
1073static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
1074 struct nlmsghdr *nlh)
1075{
1076 struct net *net = sock_net(in_skb->sk);
1077 struct nlattr *tb[NETCONFA_MAX + 1];
1078 struct netconfmsg *ncm;
1079 struct net_device *dev;
1080 struct mpls_dev *mdev;
1081 struct sk_buff *skb;
1082 int ifindex;
1083 int err;
1084
1085 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
1086 devconf_mpls_policy);
1087 if (err < 0)
1088 goto errout;
1089
1090 err = -EINVAL;
1091 if (!tb[NETCONFA_IFINDEX])
1092 goto errout;
1093
1094 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
1095 dev = __dev_get_by_index(net, ifindex);
1096 if (!dev)
1097 goto errout;
1098
1099 mdev = mpls_dev_get(dev);
1100 if (!mdev)
1101 goto errout;
1102
1103 err = -ENOBUFS;
1104 skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
1105 if (!skb)
1106 goto errout;
1107
1108 err = mpls_netconf_fill_devconf(skb, mdev,
1109 NETLINK_CB(in_skb).portid,
1110 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
1111 NETCONFA_ALL);
1112 if (err < 0) {
1113 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1114 WARN_ON(err == -EMSGSIZE);
1115 kfree_skb(skb);
1116 goto errout;
1117 }
1118 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1119errout:
1120 return err;
1121}
1122
1123static int mpls_netconf_dump_devconf(struct sk_buff *skb,
1124 struct netlink_callback *cb)
1125{
1126 struct net *net = sock_net(skb->sk);
1127 struct hlist_head *head;
1128 struct net_device *dev;
1129 struct mpls_dev *mdev;
1130 int idx, s_idx;
1131 int h, s_h;
1132
1133 s_h = cb->args[0];
1134 s_idx = idx = cb->args[1];
1135
1136 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1137 idx = 0;
1138 head = &net->dev_index_head[h];
1139 rcu_read_lock();
1140 cb->seq = net->dev_base_seq;
1141 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1142 if (idx < s_idx)
1143 goto cont;
1144 mdev = mpls_dev_get(dev);
1145 if (!mdev)
1146 goto cont;
1147 if (mpls_netconf_fill_devconf(skb, mdev,
1148 NETLINK_CB(cb->skb).portid,
1149 cb->nlh->nlmsg_seq,
1150 RTM_NEWNETCONF,
1151 NLM_F_MULTI,
1152 NETCONFA_ALL) < 0) {
1153 rcu_read_unlock();
1154 goto done;
1155 }
1156 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1157cont:
1158 idx++;
1159 }
1160 rcu_read_unlock();
1161 }
1162done:
1163 cb->args[0] = h;
1164 cb->args[1] = idx;
1165
1166 return skb->len;
1167}
1168
Robert Shearman37bde792015-04-22 11:14:38 +01001169#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
1170 (&((struct mpls_dev *)0)->field)
1171
David Ahern24045a02017-02-20 08:03:30 -08001172static int mpls_conf_proc(struct ctl_table *ctl, int write,
1173 void __user *buffer,
1174 size_t *lenp, loff_t *ppos)
1175{
1176 int oval = *(int *)ctl->data;
1177 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1178
1179 if (write) {
1180 struct mpls_dev *mdev = ctl->extra1;
1181 int i = (int *)ctl->data - (int *)mdev;
1182 struct net *net = ctl->extra2;
1183 int val = *(int *)ctl->data;
1184
1185 if (i == offsetof(struct mpls_dev, input_enabled) &&
1186 val != oval) {
1187 mpls_netconf_notify_devconf(net,
1188 NETCONFA_INPUT,
1189 mdev);
1190 }
1191 }
1192
1193 return ret;
1194}
1195
Robert Shearman37bde792015-04-22 11:14:38 +01001196static const struct ctl_table mpls_dev_table[] = {
1197 {
1198 .procname = "input",
1199 .maxlen = sizeof(int),
1200 .mode = 0644,
David Ahern24045a02017-02-20 08:03:30 -08001201 .proc_handler = mpls_conf_proc,
Robert Shearman37bde792015-04-22 11:14:38 +01001202 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
1203 },
1204 { }
1205};
1206
1207static int mpls_dev_sysctl_register(struct net_device *dev,
1208 struct mpls_dev *mdev)
1209{
1210 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
David Ahern24045a02017-02-20 08:03:30 -08001211 struct net *net = dev_net(dev);
Robert Shearman37bde792015-04-22 11:14:38 +01001212 struct ctl_table *table;
1213 int i;
1214
1215 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
1216 if (!table)
1217 goto out;
1218
1219 /* Table data contains only offsets relative to the base of
1220 * the mdev at this point, so make them absolute.
1221 */
David Ahern24045a02017-02-20 08:03:30 -08001222 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) {
Robert Shearman37bde792015-04-22 11:14:38 +01001223 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
David Ahern24045a02017-02-20 08:03:30 -08001224 table[i].extra1 = mdev;
1225 table[i].extra2 = net;
1226 }
Robert Shearman37bde792015-04-22 11:14:38 +01001227
1228 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
1229
1230 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
1231 if (!mdev->sysctl)
1232 goto free;
1233
1234 return 0;
1235
1236free:
1237 kfree(table);
1238out:
1239 return -ENOBUFS;
1240}
1241
1242static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
1243{
1244 struct ctl_table *table;
1245
1246 table = mdev->sysctl->ctl_table_arg;
1247 unregister_net_sysctl_table(mdev->sysctl);
1248 kfree(table);
1249}
1250
Robert Shearman03c57742015-04-22 11:14:37 +01001251static struct mpls_dev *mpls_add_dev(struct net_device *dev)
1252{
1253 struct mpls_dev *mdev;
1254 int err = -ENOMEM;
Robert Shearman27d69102017-01-16 14:16:37 +00001255 int i;
Robert Shearman03c57742015-04-22 11:14:37 +01001256
1257 ASSERT_RTNL();
1258
1259 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1260 if (!mdev)
1261 return ERR_PTR(err);
1262
Robert Shearman27d69102017-01-16 14:16:37 +00001263 mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
1264 if (!mdev->stats)
1265 goto free;
1266
1267 for_each_possible_cpu(i) {
1268 struct mpls_pcpu_stats *mpls_stats;
1269
1270 mpls_stats = per_cpu_ptr(mdev->stats, i);
1271 u64_stats_init(&mpls_stats->syncp);
1272 }
1273
Robert Shearman37bde792015-04-22 11:14:38 +01001274 err = mpls_dev_sysctl_register(dev, mdev);
1275 if (err)
1276 goto free;
1277
David Ahern24045a02017-02-20 08:03:30 -08001278 mdev->dev = dev;
Robert Shearman03c57742015-04-22 11:14:37 +01001279 rcu_assign_pointer(dev->mpls_ptr, mdev);
1280
1281 return mdev;
Robert Shearman37bde792015-04-22 11:14:38 +01001282
1283free:
Robert Shearman27d69102017-01-16 14:16:37 +00001284 free_percpu(mdev->stats);
Robert Shearman37bde792015-04-22 11:14:38 +01001285 kfree(mdev);
1286 return ERR_PTR(err);
Robert Shearman03c57742015-04-22 11:14:37 +01001287}
1288
Robert Shearman27d69102017-01-16 14:16:37 +00001289static void mpls_dev_destroy_rcu(struct rcu_head *head)
1290{
1291 struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
1292
1293 free_percpu(mdev->stats);
1294 kfree(mdev);
1295}
1296
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001297static void mpls_ifdown(struct net_device *dev, int event)
Eric W. Biederman01891972015-03-03 19:10:47 -06001298{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001299 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -06001300 struct net *net = dev_net(dev);
1301 unsigned index;
1302
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001303 platform_label = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06001304 for (index = 0; index < net->mpls.platform_labels; index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001305 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001306
Eric W. Biederman01891972015-03-03 19:10:47 -06001307 if (!rt)
1308 continue;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001309
1310 change_nexthops(rt) {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001311 if (rtnl_dereference(nh->nh_dev) != dev)
1312 continue;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001313 switch (event) {
1314 case NETDEV_DOWN:
1315 case NETDEV_UNREGISTER:
1316 nh->nh_flags |= RTNH_F_DEAD;
1317 /* fall through */
1318 case NETDEV_CHANGE:
1319 nh->nh_flags |= RTNH_F_LINKDOWN;
1320 ACCESS_ONCE(rt->rt_nhn_alive) = rt->rt_nhn_alive - 1;
1321 break;
1322 }
1323 if (event == NETDEV_UNREGISTER)
1324 RCU_INIT_POINTER(nh->nh_dev, NULL);
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001325 } endfor_nexthops(rt);
Eric W. Biederman01891972015-03-03 19:10:47 -06001326 }
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001327}
Robert Shearman37bde792015-04-22 11:14:38 +01001328
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001329static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
1330{
1331 struct mpls_route __rcu **platform_label;
1332 struct net *net = dev_net(dev);
1333 unsigned index;
1334 int alive;
Robert Shearman03c57742015-04-22 11:14:37 +01001335
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001336 platform_label = rtnl_dereference(net->mpls.platform_label);
1337 for (index = 0; index < net->mpls.platform_labels; index++) {
1338 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1339
1340 if (!rt)
1341 continue;
1342
1343 alive = 0;
1344 change_nexthops(rt) {
1345 struct net_device *nh_dev =
1346 rtnl_dereference(nh->nh_dev);
1347
1348 if (!(nh->nh_flags & nh_flags)) {
1349 alive++;
1350 continue;
1351 }
1352 if (nh_dev != dev)
1353 continue;
1354 alive++;
1355 nh->nh_flags &= ~nh_flags;
1356 } endfor_nexthops(rt);
1357
1358 ACCESS_ONCE(rt->rt_nhn_alive) = alive;
1359 }
Eric W. Biederman01891972015-03-03 19:10:47 -06001360}
1361
1362static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
1363 void *ptr)
1364{
1365 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Robert Shearman03c57742015-04-22 11:14:37 +01001366 struct mpls_dev *mdev;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001367 unsigned int flags;
Eric W. Biederman01891972015-03-03 19:10:47 -06001368
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001369 if (event == NETDEV_REGISTER) {
Simon Horman407f31b2016-07-07 07:56:15 +02001370 /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
Simon Horman0d227a82016-06-16 17:09:09 +09001371 if (dev->type == ARPHRD_ETHER ||
1372 dev->type == ARPHRD_LOOPBACK ||
Simon Horman407f31b2016-07-07 07:56:15 +02001373 dev->type == ARPHRD_IPGRE ||
1374 dev->type == ARPHRD_SIT ||
1375 dev->type == ARPHRD_TUNNEL) {
Robert Shearman03c57742015-04-22 11:14:37 +01001376 mdev = mpls_add_dev(dev);
1377 if (IS_ERR(mdev))
1378 return notifier_from_errno(PTR_ERR(mdev));
1379 }
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001380 return NOTIFY_OK;
1381 }
Robert Shearman03c57742015-04-22 11:14:37 +01001382
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001383 mdev = mpls_dev_get(dev);
1384 if (!mdev)
1385 return NOTIFY_OK;
1386
1387 switch (event) {
1388 case NETDEV_DOWN:
1389 mpls_ifdown(dev, event);
1390 break;
1391 case NETDEV_UP:
1392 flags = dev_get_flags(dev);
1393 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1394 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1395 else
1396 mpls_ifup(dev, RTNH_F_DEAD);
1397 break;
1398 case NETDEV_CHANGE:
1399 flags = dev_get_flags(dev);
1400 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1401 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1402 else
1403 mpls_ifdown(dev, event);
1404 break;
Eric W. Biederman01891972015-03-03 19:10:47 -06001405 case NETDEV_UNREGISTER:
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001406 mpls_ifdown(dev, event);
1407 mdev = mpls_dev_get(dev);
1408 if (mdev) {
1409 mpls_dev_sysctl_unregister(mdev);
1410 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
Robert Shearman27d69102017-01-16 14:16:37 +00001411 call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001412 }
Eric W. Biederman01891972015-03-03 19:10:47 -06001413 break;
Robert Shearman0fae3bf2015-06-11 19:58:26 +01001414 case NETDEV_CHANGENAME:
1415 mdev = mpls_dev_get(dev);
1416 if (mdev) {
1417 int err;
1418
1419 mpls_dev_sysctl_unregister(mdev);
1420 err = mpls_dev_sysctl_register(dev, mdev);
1421 if (err)
1422 return notifier_from_errno(err);
1423 }
1424 break;
Eric W. Biederman01891972015-03-03 19:10:47 -06001425 }
1426 return NOTIFY_OK;
1427}
1428
1429static struct notifier_block mpls_dev_notifier = {
1430 .notifier_call = mpls_dev_notify,
1431};
1432
Eric W. Biederman03c05662015-03-03 19:13:56 -06001433static int nla_put_via(struct sk_buff *skb,
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001434 u8 table, const void *addr, int alen)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001435{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001436 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
1437 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
1438 };
Eric W. Biederman03c05662015-03-03 19:13:56 -06001439 struct nlattr *nla;
1440 struct rtvia *via;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001441 int family = AF_UNSPEC;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001442
1443 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1444 if (!nla)
1445 return -EMSGSIZE;
1446
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001447 if (table <= NEIGH_NR_TABLES)
1448 family = table_to_family[table];
1449
Eric W. Biederman03c05662015-03-03 19:13:56 -06001450 via = nla_data(nla);
1451 via->rtvia_family = family;
1452 memcpy(via->rtvia_addr, addr, alen);
1453 return 0;
1454}
1455
Eric W. Biederman966bae32015-03-03 19:13:19 -06001456int nla_put_labels(struct sk_buff *skb, int attrtype,
1457 u8 labels, const u32 label[])
1458{
1459 struct nlattr *nla;
1460 struct mpls_shim_hdr *nla_label;
1461 bool bos;
1462 int i;
1463 nla = nla_reserve(skb, attrtype, labels*4);
1464 if (!nla)
1465 return -EMSGSIZE;
1466
1467 nla_label = nla_data(nla);
1468 bos = true;
1469 for (i = labels - 1; i >= 0; i--) {
1470 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1471 bos = false;
1472 }
1473
1474 return 0;
1475}
Roopa Prabhuface0182015-07-21 10:43:52 +02001476EXPORT_SYMBOL_GPL(nla_put_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001477
1478int nla_get_labels(const struct nlattr *nla,
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001479 u32 max_labels, u8 *labels, u32 label[])
Eric W. Biederman966bae32015-03-03 19:13:19 -06001480{
1481 unsigned len = nla_len(nla);
1482 unsigned nla_labels;
1483 struct mpls_shim_hdr *nla_label;
1484 bool bos;
1485 int i;
1486
1487 /* len needs to be an even multiple of 4 (the label size) */
1488 if (len & 3)
1489 return -EINVAL;
1490
1491 /* Limit the number of new labels allowed */
1492 nla_labels = len/4;
1493 if (nla_labels > max_labels)
1494 return -EINVAL;
1495
1496 nla_label = nla_data(nla);
1497 bos = true;
1498 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1499 struct mpls_entry_decoded dec;
1500 dec = mpls_entry_decode(nla_label + i);
1501
1502 /* Ensure the bottom of stack flag is properly set
1503 * and ttl and tc are both clear.
1504 */
1505 if ((dec.bos != bos) || dec.ttl || dec.tc)
1506 return -EINVAL;
1507
Robert Shearman5a9ab012015-04-22 11:14:39 +01001508 switch (dec.label) {
Tom Herbert78f5b892015-05-07 08:08:51 -07001509 case MPLS_LABEL_IMPLNULL:
Robert Shearman5a9ab012015-04-22 11:14:39 +01001510 /* RFC3032: This is a label that an LSR may
1511 * assign and distribute, but which never
1512 * actually appears in the encapsulation.
1513 */
1514 return -EINVAL;
1515 }
1516
Eric W. Biederman966bae32015-03-03 19:13:19 -06001517 label[i] = dec.label;
1518 }
1519 *labels = nla_labels;
1520 return 0;
1521}
Roopa Prabhuface0182015-07-21 10:43:52 +02001522EXPORT_SYMBOL_GPL(nla_get_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -06001523
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001524int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1525 u8 *via_table, u8 via_addr[])
1526{
1527 struct rtvia *via = nla_data(nla);
1528 int err = -EINVAL;
1529 int alen;
1530
1531 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1532 goto errout;
1533 alen = nla_len(nla) -
1534 offsetof(struct rtvia, rtvia_addr);
1535 if (alen > MAX_VIA_ALEN)
1536 goto errout;
1537
1538 /* Validate the address family */
1539 switch (via->rtvia_family) {
1540 case AF_PACKET:
1541 *via_table = NEIGH_LINK_TABLE;
1542 break;
1543 case AF_INET:
1544 *via_table = NEIGH_ARP_TABLE;
1545 if (alen != 4)
1546 goto errout;
1547 break;
1548 case AF_INET6:
1549 *via_table = NEIGH_ND_TABLE;
1550 if (alen != 16)
1551 goto errout;
1552 break;
1553 default:
1554 /* Unsupported address family */
1555 goto errout;
1556 }
1557
1558 memcpy(via_addr, via->rtvia_addr, alen);
1559 *via_alen = alen;
1560 err = 0;
1561
1562errout:
1563 return err;
1564}
1565
Eric W. Biederman03c05662015-03-03 19:13:56 -06001566static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1567 struct mpls_route_config *cfg)
1568{
1569 struct rtmsg *rtm;
1570 struct nlattr *tb[RTA_MAX+1];
1571 int index;
1572 int err;
1573
1574 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1575 if (err < 0)
1576 goto errout;
1577
1578 err = -EINVAL;
1579 rtm = nlmsg_data(nlh);
1580 memset(cfg, 0, sizeof(*cfg));
1581
1582 if (rtm->rtm_family != AF_MPLS)
1583 goto errout;
1584 if (rtm->rtm_dst_len != 20)
1585 goto errout;
1586 if (rtm->rtm_src_len != 0)
1587 goto errout;
1588 if (rtm->rtm_tos != 0)
1589 goto errout;
1590 if (rtm->rtm_table != RT_TABLE_MAIN)
1591 goto errout;
1592 /* Any value is acceptable for rtm_protocol */
1593
1594 /* As mpls uses destination specific addresses
1595 * (or source specific address in the case of multicast)
1596 * all addresses have universal scope.
1597 */
1598 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1599 goto errout;
1600 if (rtm->rtm_type != RTN_UNICAST)
1601 goto errout;
1602 if (rtm->rtm_flags != 0)
1603 goto errout;
1604
1605 cfg->rc_label = LABEL_NOT_SPECIFIED;
1606 cfg->rc_protocol = rtm->rtm_protocol;
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001607 cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001608 cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001609 cfg->rc_nlflags = nlh->nlmsg_flags;
1610 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1611 cfg->rc_nlinfo.nlh = nlh;
1612 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1613
1614 for (index = 0; index <= RTA_MAX; index++) {
1615 struct nlattr *nla = tb[index];
1616 if (!nla)
1617 continue;
1618
Suraj Deshmukh14dd3e12016-12-03 07:59:26 +00001619 switch (index) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001620 case RTA_OIF:
1621 cfg->rc_ifindex = nla_get_u32(nla);
1622 break;
1623 case RTA_NEWDST:
1624 if (nla_get_labels(nla, MAX_NEW_LABELS,
1625 &cfg->rc_output_labels,
1626 cfg->rc_output_label))
1627 goto errout;
1628 break;
1629 case RTA_DST:
1630 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001631 u8 label_count;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001632 if (nla_get_labels(nla, 1, &label_count,
1633 &cfg->rc_label))
1634 goto errout;
1635
Robert Shearmana6affd22015-08-03 17:50:04 +01001636 /* Reserved labels may not be set */
1637 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biederman03c05662015-03-03 19:13:56 -06001638 goto errout;
1639
1640 break;
1641 }
1642 case RTA_VIA:
1643 {
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001644 if (nla_get_via(nla, &cfg->rc_via_alen,
1645 &cfg->rc_via_table, cfg->rc_via))
Robert Shearmanf8d54af2015-03-06 10:47:00 +00001646 goto errout;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001647 break;
1648 }
1649 case RTA_MULTIPATH:
1650 {
1651 cfg->rc_mp = nla_data(nla);
1652 cfg->rc_mp_len = nla_len(nla);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001653 break;
1654 }
Robert Shearman5b441ac2017-03-10 20:43:24 +00001655 case RTA_TTL_PROPAGATE:
1656 {
1657 u8 ttl_propagate = nla_get_u8(nla);
1658
1659 if (ttl_propagate > 1)
1660 goto errout;
1661 cfg->rc_ttl_propagate = ttl_propagate ?
1662 MPLS_TTL_PROP_ENABLED :
1663 MPLS_TTL_PROP_DISABLED;
1664 break;
1665 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001666 default:
1667 /* Unsupported attribute */
1668 goto errout;
1669 }
1670 }
1671
1672 err = 0;
1673errout:
1674 return err;
1675}
1676
1677static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1678{
1679 struct mpls_route_config cfg;
1680 int err;
1681
1682 err = rtm_to_route_config(skb, nlh, &cfg);
1683 if (err < 0)
1684 return err;
1685
1686 return mpls_route_del(&cfg);
1687}
1688
1689
1690static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1691{
1692 struct mpls_route_config cfg;
1693 int err;
1694
1695 err = rtm_to_route_config(skb, nlh, &cfg);
1696 if (err < 0)
1697 return err;
1698
1699 return mpls_route_add(&cfg);
1700}
1701
1702static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1703 u32 label, struct mpls_route *rt, int flags)
1704{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001705 struct net_device *dev;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001706 struct nlmsghdr *nlh;
1707 struct rtmsg *rtm;
1708
1709 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1710 if (nlh == NULL)
1711 return -EMSGSIZE;
1712
1713 rtm = nlmsg_data(nlh);
1714 rtm->rtm_family = AF_MPLS;
1715 rtm->rtm_dst_len = 20;
1716 rtm->rtm_src_len = 0;
1717 rtm->rtm_tos = 0;
1718 rtm->rtm_table = RT_TABLE_MAIN;
1719 rtm->rtm_protocol = rt->rt_protocol;
1720 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1721 rtm->rtm_type = RTN_UNICAST;
1722 rtm->rtm_flags = 0;
1723
Eric W. Biederman03c05662015-03-03 19:13:56 -06001724 if (nla_put_labels(skb, RTA_DST, 1, &label))
1725 goto nla_put_failure;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001726
1727 if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
1728 bool ttl_propagate =
1729 rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
1730
1731 if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
1732 ttl_propagate))
1733 goto nla_put_failure;
1734 }
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001735 if (rt->rt_nhn == 1) {
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001736 const struct mpls_nh *nh = rt->rt_nh;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001737
1738 if (nh->nh_labels &&
1739 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1740 nh->nh_label))
1741 goto nla_put_failure;
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001742 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
Robert Shearman72dcac92015-12-10 19:30:49 +00001743 nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001744 nh->nh_via_alen))
1745 goto nla_put_failure;
1746 dev = rtnl_dereference(nh->nh_dev);
1747 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1748 goto nla_put_failure;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001749 if (nh->nh_flags & RTNH_F_LINKDOWN)
1750 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1751 if (nh->nh_flags & RTNH_F_DEAD)
1752 rtm->rtm_flags |= RTNH_F_DEAD;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001753 } else {
1754 struct rtnexthop *rtnh;
1755 struct nlattr *mp;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001756 int dead = 0;
1757 int linkdown = 0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001758
1759 mp = nla_nest_start(skb, RTA_MULTIPATH);
1760 if (!mp)
1761 goto nla_put_failure;
1762
1763 for_nexthops(rt) {
1764 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1765 if (!rtnh)
1766 goto nla_put_failure;
1767
1768 dev = rtnl_dereference(nh->nh_dev);
1769 if (dev)
1770 rtnh->rtnh_ifindex = dev->ifindex;
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001771 if (nh->nh_flags & RTNH_F_LINKDOWN) {
1772 rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
1773 linkdown++;
1774 }
1775 if (nh->nh_flags & RTNH_F_DEAD) {
1776 rtnh->rtnh_flags |= RTNH_F_DEAD;
1777 dead++;
1778 }
1779
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001780 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1781 nh->nh_labels,
1782 nh->nh_label))
1783 goto nla_put_failure;
Robert Shearmanf20367d2015-12-10 19:30:51 +00001784 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1785 nla_put_via(skb, nh->nh_via_table,
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001786 mpls_nh_via(rt, nh),
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001787 nh->nh_via_alen))
1788 goto nla_put_failure;
1789
1790 /* length of rtnetlink header + attributes */
1791 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1792 } endfor_nexthops(rt);
1793
Roopa Prabhuc89359a2015-12-01 22:18:11 -08001794 if (linkdown == rt->rt_nhn)
1795 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1796 if (dead == rt->rt_nhn)
1797 rtm->rtm_flags |= RTNH_F_DEAD;
1798
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001799 nla_nest_end(skb, mp);
1800 }
Eric W. Biederman03c05662015-03-03 19:13:56 -06001801
1802 nlmsg_end(skb, nlh);
1803 return 0;
1804
1805nla_put_failure:
1806 nlmsg_cancel(skb, nlh);
1807 return -EMSGSIZE;
1808}
1809
1810static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1811{
1812 struct net *net = sock_net(skb->sk);
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001813 struct mpls_route __rcu **platform_label;
1814 size_t platform_labels;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001815 unsigned int index;
1816
1817 ASSERT_RTNL();
1818
1819 index = cb->args[0];
Robert Shearmana6affd22015-08-03 17:50:04 +01001820 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1821 index = MPLS_LABEL_FIRST_UNRESERVED;
Eric W. Biederman03c05662015-03-03 19:13:56 -06001822
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001823 platform_label = rtnl_dereference(net->mpls.platform_label);
1824 platform_labels = net->mpls.platform_labels;
1825 for (; index < platform_labels; index++) {
Eric W. Biederman03c05662015-03-03 19:13:56 -06001826 struct mpls_route *rt;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001827 rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman03c05662015-03-03 19:13:56 -06001828 if (!rt)
1829 continue;
1830
1831 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1832 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1833 index, rt, NLM_F_MULTI) < 0)
1834 break;
1835 }
1836 cb->args[0] = index;
1837
1838 return skb->len;
1839}
1840
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001841static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1842{
1843 size_t payload =
1844 NLMSG_ALIGN(sizeof(struct rtmsg))
Robert Shearman5b441ac2017-03-10 20:43:24 +00001845 + nla_total_size(4) /* RTA_DST */
1846 + nla_total_size(1); /* RTA_TTL_PROPAGATE */
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001847
1848 if (rt->rt_nhn == 1) {
1849 struct mpls_nh *nh = rt->rt_nh;
1850
1851 if (nh->nh_dev)
1852 payload += nla_total_size(4); /* RTA_OIF */
Robert Shearmaneb7809f2015-12-10 19:30:50 +00001853 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
Robert Shearman72dcac92015-12-10 19:30:49 +00001854 payload += nla_total_size(2 + nh->nh_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001855 if (nh->nh_labels) /* RTA_NEWDST */
1856 payload += nla_total_size(nh->nh_labels * 4);
1857 } else {
1858 /* each nexthop is packed in an attribute */
1859 size_t nhsize = 0;
1860
1861 for_nexthops(rt) {
1862 nhsize += nla_total_size(sizeof(struct rtnexthop));
Robert Shearmanf20367d2015-12-10 19:30:51 +00001863 /* RTA_VIA */
1864 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
1865 nhsize += nla_total_size(2 + nh->nh_via_alen);
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001866 if (nh->nh_labels)
1867 nhsize += nla_total_size(nh->nh_labels * 4);
1868 } endfor_nexthops(rt);
1869 /* nested attribute */
1870 payload += nla_total_size(nhsize);
1871 }
1872
Eric W. Biederman8de147d2015-03-03 19:14:31 -06001873 return payload;
1874}
1875
1876static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1877 struct nlmsghdr *nlh, struct net *net, u32 portid,
1878 unsigned int nlm_flags)
1879{
1880 struct sk_buff *skb;
1881 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1882 int err = -ENOBUFS;
1883
1884 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1885 if (skb == NULL)
1886 goto errout;
1887
1888 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1889 if (err < 0) {
1890 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1891 WARN_ON(err == -EMSGSIZE);
1892 kfree_skb(skb);
1893 goto errout;
1894 }
1895 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1896
1897 return;
1898errout:
1899 if (err < 0)
1900 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1901}
1902
Eric W. Biederman7720c012015-03-03 19:11:20 -06001903static int resize_platform_label_table(struct net *net, size_t limit)
1904{
1905 size_t size = sizeof(struct mpls_route *) * limit;
1906 size_t old_limit;
1907 size_t cp_size;
1908 struct mpls_route __rcu **labels = NULL, **old;
1909 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1910 unsigned index;
1911
1912 if (size) {
1913 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1914 if (!labels)
1915 labels = vzalloc(size);
1916
1917 if (!labels)
1918 goto nolabels;
1919 }
1920
1921 /* In case the predefined labels need to be populated */
Tom Herbert78f5b892015-05-07 08:08:51 -07001922 if (limit > MPLS_LABEL_IPV4NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001923 struct net_device *lo = net->loopback_dev;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001924 rt0 = mpls_rt_alloc(1, lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001925 if (!rt0)
1926 goto nort0;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001927 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001928 rt0->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001929 rt0->rt_payload_type = MPT_IPV4;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001930 rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001931 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00001932 rt0->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001933 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
1934 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001935 }
Tom Herbert78f5b892015-05-07 08:08:51 -07001936 if (limit > MPLS_LABEL_IPV6NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001937 struct net_device *lo = net->loopback_dev;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001938 rt2 = mpls_rt_alloc(1, lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001939 if (!rt2)
1940 goto nort2;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001941 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001942 rt2->rt_protocol = RTPROT_KERNEL;
Robert Shearman118d5232015-08-06 11:04:56 +01001943 rt2->rt_payload_type = MPT_IPV6;
Robert Shearman5b441ac2017-03-10 20:43:24 +00001944 rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001945 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
Robert Shearmanb4e04fc2015-10-27 00:37:35 +00001946 rt2->rt_nh->nh_via_alen = lo->addr_len;
Robert Shearmancf4b24f2015-10-27 00:37:36 +00001947 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
1948 lo->addr_len);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001949 }
1950
1951 rtnl_lock();
1952 /* Remember the original table */
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001953 old = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001954 old_limit = net->mpls.platform_labels;
1955
1956 /* Free any labels beyond the new table */
1957 for (index = limit; index < old_limit; index++)
Roopa Prabhuf8efb732015-10-23 06:03:27 -07001958 mpls_route_update(net, index, NULL, NULL);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001959
1960 /* Copy over the old labels */
1961 cp_size = size;
1962 if (old_limit < limit)
1963 cp_size = old_limit * sizeof(struct mpls_route *);
1964
1965 memcpy(labels, old, cp_size);
1966
1967 /* If needed set the predefined labels */
Tom Herbert78f5b892015-05-07 08:08:51 -07001968 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1969 (limit > MPLS_LABEL_IPV6NULL)) {
1970 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001971 rt2 = NULL;
1972 }
1973
Tom Herbert78f5b892015-05-07 08:08:51 -07001974 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1975 (limit > MPLS_LABEL_IPV4NULL)) {
1976 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001977 rt0 = NULL;
1978 }
1979
1980 /* Update the global pointers */
1981 net->mpls.platform_labels = limit;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001982 rcu_assign_pointer(net->mpls.platform_label, labels);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001983
1984 rtnl_unlock();
1985
1986 mpls_rt_free(rt2);
1987 mpls_rt_free(rt0);
1988
1989 if (old) {
1990 synchronize_rcu();
1991 kvfree(old);
1992 }
1993 return 0;
1994
1995nort2:
1996 mpls_rt_free(rt0);
1997nort0:
1998 kvfree(labels);
1999nolabels:
2000 return -ENOMEM;
2001}
2002
2003static int mpls_platform_labels(struct ctl_table *table, int write,
2004 void __user *buffer, size_t *lenp, loff_t *ppos)
2005{
2006 struct net *net = table->data;
2007 int platform_labels = net->mpls.platform_labels;
2008 int ret;
2009 struct ctl_table tmp = {
2010 .procname = table->procname,
2011 .data = &platform_labels,
2012 .maxlen = sizeof(int),
2013 .mode = table->mode,
2014 .extra1 = &zero,
2015 .extra2 = &label_limit,
2016 };
2017
2018 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2019
2020 if (write && ret == 0)
2021 ret = resize_platform_label_table(net, platform_labels);
2022
2023 return ret;
2024}
2025
Robert Shearman5b441ac2017-03-10 20:43:24 +00002026#define MPLS_NS_SYSCTL_OFFSET(field) \
2027 (&((struct net *)0)->field)
2028
Robert Shearman37bde792015-04-22 11:14:38 +01002029static const struct ctl_table mpls_table[] = {
Eric W. Biederman7720c012015-03-03 19:11:20 -06002030 {
2031 .procname = "platform_labels",
2032 .data = NULL,
2033 .maxlen = sizeof(int),
2034 .mode = 0644,
2035 .proc_handler = mpls_platform_labels,
2036 },
Robert Shearman5b441ac2017-03-10 20:43:24 +00002037 {
2038 .procname = "ip_ttl_propagate",
2039 .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
2040 .maxlen = sizeof(int),
2041 .mode = 0644,
2042 .proc_handler = proc_dointvec_minmax,
2043 .extra1 = &zero,
2044 .extra2 = &one,
2045 },
Robert Shearmana59166e2017-03-10 20:43:25 +00002046 {
2047 .procname = "default_ttl",
2048 .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
2049 .maxlen = sizeof(int),
2050 .mode = 0644,
2051 .proc_handler = proc_dointvec_minmax,
2052 .extra1 = &one,
2053 .extra2 = &ttl_max,
2054 },
Eric W. Biederman7720c012015-03-03 19:11:20 -06002055 { }
2056};
2057
Eric W. Biederman01891972015-03-03 19:10:47 -06002058static int mpls_net_init(struct net *net)
2059{
Eric W. Biederman7720c012015-03-03 19:11:20 -06002060 struct ctl_table *table;
Robert Shearman5b441ac2017-03-10 20:43:24 +00002061 int i;
Eric W. Biederman7720c012015-03-03 19:11:20 -06002062
Eric W. Biederman01891972015-03-03 19:10:47 -06002063 net->mpls.platform_labels = 0;
2064 net->mpls.platform_label = NULL;
Robert Shearman5b441ac2017-03-10 20:43:24 +00002065 net->mpls.ip_ttl_propagate = 1;
Robert Shearmana59166e2017-03-10 20:43:25 +00002066 net->mpls.default_ttl = 255;
Eric W. Biederman01891972015-03-03 19:10:47 -06002067
Eric W. Biederman7720c012015-03-03 19:11:20 -06002068 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
2069 if (table == NULL)
2070 return -ENOMEM;
2071
Robert Shearman5b441ac2017-03-10 20:43:24 +00002072 /* Table data contains only offsets relative to the base of
2073 * the mdev at this point, so make them absolute.
2074 */
2075 for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++)
2076 table[i].data = (char *)net + (uintptr_t)table[i].data;
2077
Eric W. Biederman7720c012015-03-03 19:11:20 -06002078 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07002079 if (net->mpls.ctl == NULL) {
2080 kfree(table);
Eric W. Biederman7720c012015-03-03 19:11:20 -06002081 return -ENOMEM;
Nikolay Aleksandrov6ea3c9d52015-08-31 10:44:19 -07002082 }
Eric W. Biederman7720c012015-03-03 19:11:20 -06002083
Eric W. Biederman01891972015-03-03 19:10:47 -06002084 return 0;
2085}
2086
2087static void mpls_net_exit(struct net *net)
2088{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002089 struct mpls_route __rcu **platform_label;
2090 size_t platform_labels;
Eric W. Biederman7720c012015-03-03 19:11:20 -06002091 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -06002092 unsigned int index;
2093
Eric W. Biederman7720c012015-03-03 19:11:20 -06002094 table = net->mpls.ctl->ctl_table_arg;
2095 unregister_net_sysctl_table(net->mpls.ctl);
2096 kfree(table);
2097
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002098 /* An rcu grace period has passed since there was a device in
2099 * the network namespace (and thus the last in flight packet)
Eric W. Biederman01891972015-03-03 19:10:47 -06002100 * left this network namespace. This is because
2101 * unregister_netdevice_many and netdev_run_todo has completed
2102 * for each network device that was in this network namespace.
2103 *
2104 * As such no additional rcu synchronization is necessary when
2105 * freeing the platform_label table.
2106 */
2107 rtnl_lock();
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002108 platform_label = rtnl_dereference(net->mpls.platform_label);
2109 platform_labels = net->mpls.platform_labels;
2110 for (index = 0; index < platform_labels; index++) {
2111 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
2112 RCU_INIT_POINTER(platform_label[index], NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06002113 mpls_rt_free(rt);
2114 }
2115 rtnl_unlock();
2116
Eric W. Biederman19d0c342015-03-07 16:21:56 -06002117 kvfree(platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06002118}
2119
2120static struct pernet_operations mpls_net_ops = {
2121 .init = mpls_net_init,
2122 .exit = mpls_net_exit,
2123};
2124
Robert Shearman27d69102017-01-16 14:16:37 +00002125static struct rtnl_af_ops mpls_af_ops __read_mostly = {
2126 .family = AF_MPLS,
2127 .fill_stats_af = mpls_fill_stats_af,
2128 .get_stats_af_size = mpls_get_stats_af_size,
2129};
2130
Eric W. Biederman01891972015-03-03 19:10:47 -06002131static int __init mpls_init(void)
2132{
2133 int err;
2134
2135 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
2136
2137 err = register_pernet_subsys(&mpls_net_ops);
2138 if (err)
2139 goto out;
2140
2141 err = register_netdevice_notifier(&mpls_dev_notifier);
2142 if (err)
2143 goto out_unregister_pernet;
2144
2145 dev_add_pack(&mpls_packet_type);
2146
Robert Shearman27d69102017-01-16 14:16:37 +00002147 rtnl_af_register(&mpls_af_ops);
2148
Eric W. Biederman03c05662015-03-03 19:13:56 -06002149 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
2150 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
2151 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
David Ahern24045a02017-02-20 08:03:30 -08002152 rtnl_register(PF_MPLS, RTM_GETNETCONF, mpls_netconf_get_devconf,
2153 mpls_netconf_dump_devconf, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06002154 err = 0;
2155out:
2156 return err;
2157
2158out_unregister_pernet:
2159 unregister_pernet_subsys(&mpls_net_ops);
2160 goto out;
2161}
2162module_init(mpls_init);
2163
2164static void __exit mpls_exit(void)
2165{
Eric W. Biederman03c05662015-03-03 19:13:56 -06002166 rtnl_unregister_all(PF_MPLS);
Robert Shearman27d69102017-01-16 14:16:37 +00002167 rtnl_af_unregister(&mpls_af_ops);
Eric W. Biederman01891972015-03-03 19:10:47 -06002168 dev_remove_pack(&mpls_packet_type);
2169 unregister_netdevice_notifier(&mpls_dev_notifier);
2170 unregister_pernet_subsys(&mpls_net_ops);
2171}
2172module_exit(mpls_exit);
2173
2174MODULE_DESCRIPTION("MultiProtocol Label Switching");
2175MODULE_LICENSE("GPL v2");
2176MODULE_ALIAS_NETPROTO(PF_MPLS);