Commit | Line | Data |
---|---|---|
0189197f EB |
1 | #include <linux/types.h> |
2 | #include <linux/skbuff.h> | |
3 | #include <linux/socket.h> | |
7720c01f | 4 | #include <linux/sysctl.h> |
0189197f EB |
5 | #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> | |
24045a03 | 10 | #include <linux/netconf.h> |
4b5edb2f | 11 | #include <linux/vmalloc.h> |
27d69105 | 12 | #include <linux/percpu.h> |
0189197f EB |
13 | #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> | |
bf21563a RP |
20 | #if IS_ENABLED(CONFIG_IPV6) |
21 | #include <net/ipv6.h> | |
bf21563a | 22 | #endif |
27d69105 | 23 | #include <net/addrconf.h> |
f8efb73c | 24 | #include <net/nexthop.h> |
0189197f EB |
25 | #include "internal.h" |
26 | ||
df1c6316 DA |
27 | /* max memory we will use for mpls_route */ |
28 | #define MAX_MPLS_ROUTE_MEM 4096 | |
29 | ||
1c78efa8 RS |
30 | /* 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 | ||
eb7809f0 RS |
35 | #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1) |
36 | ||
7720c01f | 37 | static int zero = 0; |
5b441ac8 | 38 | static int one = 1; |
7720c01f | 39 | static int label_limit = (1 << 20) - 1; |
a59166e4 | 40 | static int ttl_max = 255; |
7720c01f | 41 | |
8de147dc EB |
42 | static 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 | ||
0189197f EB |
46 | static 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 | ||
face0188 | 58 | bool mpls_output_possible(const struct net_device *dev) |
0189197f EB |
59 | { |
60 | return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev); | |
61 | } | |
face0188 | 62 | EXPORT_SYMBOL_GPL(mpls_output_possible); |
0189197f | 63 | |
cf4b24f0 RS |
64 | static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh) |
65 | { | |
59b20966 | 66 | return (u8 *)nh + rt->rt_via_offset; |
cf4b24f0 RS |
67 | } |
68 | ||
69 | static 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 | ||
f8efb73c | 75 | static unsigned int mpls_nh_header_size(const struct mpls_nh *nh) |
0189197f EB |
76 | { |
77 | /* The size of the layer 2.5 labels to be added for this route */ | |
f8efb73c | 78 | return nh->nh_labels * sizeof(struct mpls_shim_hdr); |
0189197f EB |
79 | } |
80 | ||
face0188 | 81 | unsigned int mpls_dev_mtu(const struct net_device *dev) |
0189197f EB |
82 | { |
83 | /* The amount of data the layer 2 frame can hold */ | |
84 | return dev->mtu; | |
85 | } | |
face0188 | 86 | EXPORT_SYMBOL_GPL(mpls_dev_mtu); |
0189197f | 87 | |
face0188 | 88 | bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu) |
0189197f EB |
89 | { |
90 | if (skb->len <= mtu) | |
91 | return false; | |
92 | ||
ae7ef81e | 93 | if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu)) |
0189197f EB |
94 | return false; |
95 | ||
96 | return true; | |
97 | } | |
face0188 | 98 | EXPORT_SYMBOL_GPL(mpls_pkt_too_big); |
0189197f | 99 | |
27d69105 RS |
100 | void 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 | } | |
123 | EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts); | |
124 | ||
9f427a0e | 125 | static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb) |
f8efb73c | 126 | { |
1c78efa8 | 127 | struct mpls_entry_decoded dec; |
9f427a0e | 128 | unsigned int mpls_hdr_len = 0; |
1c78efa8 RS |
129 | struct mpls_shim_hdr *hdr; |
130 | bool eli_seen = false; | |
131 | int label_index; | |
1c78efa8 RS |
132 | u32 hash = 0; |
133 | ||
9f427a0e | 134 | for (label_index = 0; label_index < MAX_MP_SELECT_LABELS; |
1c78efa8 | 135 | label_index++) { |
9f427a0e DA |
136 | mpls_hdr_len += sizeof(*hdr); |
137 | if (!pskb_may_pull(skb, mpls_hdr_len)) | |
1c78efa8 RS |
138 | 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 | ||
9f427a0e DA |
162 | 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))) { | |
1c78efa8 RS |
167 | const struct iphdr *v4hdr; |
168 | ||
9f427a0e | 169 | v4hdr = (const struct iphdr *)(hdr + 1); |
1c78efa8 RS |
170 | 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 && | |
9f427a0e DA |
175 | pskb_may_pull(skb, mpls_hdr_len + |
176 | sizeof(struct ipv6hdr))) { | |
1c78efa8 RS |
177 | const struct ipv6hdr *v6hdr; |
178 | ||
9f427a0e | 179 | v6hdr = (const struct ipv6hdr *)(hdr + 1); |
1c78efa8 RS |
180 | hash = __ipv6_addr_jhash(&v6hdr->saddr, hash); |
181 | hash = __ipv6_addr_jhash(&v6hdr->daddr, hash); | |
182 | hash = jhash_1word(v6hdr->nexthdr, hash); | |
183 | } | |
184 | } | |
9f427a0e DA |
185 | |
186 | break; | |
1c78efa8 RS |
187 | } |
188 | ||
c89359a4 RP |
189 | return hash; |
190 | } | |
191 | ||
59b20966 DA |
192 | static 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 | ||
39eb8cd1 DA |
197 | /* 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 | */ | |
c89359a4 | 202 | static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt, |
9f427a0e | 203 | struct sk_buff *skb) |
c89359a4 | 204 | { |
c89359a4 RP |
205 | u32 hash = 0; |
206 | int nh_index = 0; | |
207 | int n = 0; | |
77ef013a | 208 | u8 alive; |
c89359a4 RP |
209 | |
210 | /* No need to look further into packet if there's only | |
211 | * one path | |
212 | */ | |
213 | if (rt->rt_nhn == 1) | |
59b20966 | 214 | return rt->rt_nh; |
c89359a4 | 215 | |
39eb8cd1 DA |
216 | alive = READ_ONCE(rt->rt_nhn_alive); |
217 | if (alive == 0) | |
c89359a4 RP |
218 | return NULL; |
219 | ||
9f427a0e | 220 | hash = mpls_multipath_hash(rt, skb); |
c89359a4 RP |
221 | nh_index = hash % alive; |
222 | if (alive == rt->rt_nhn) | |
223 | goto out; | |
224 | for_nexthops(rt) { | |
39eb8cd1 DA |
225 | unsigned int nh_flags = READ_ONCE(nh->nh_flags); |
226 | ||
227 | if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) | |
c89359a4 RP |
228 | continue; |
229 | if (n == nh_index) | |
230 | return nh; | |
231 | n++; | |
232 | } endfor_nexthops(rt); | |
233 | ||
1c78efa8 | 234 | out: |
59b20966 | 235 | return mpls_get_nexthop(rt, nh_index); |
f8efb73c RP |
236 | } |
237 | ||
5b441ac8 RS |
238 | static bool mpls_egress(struct net *net, struct mpls_route *rt, |
239 | struct sk_buff *skb, struct mpls_entry_decoded dec) | |
0189197f | 240 | { |
118d5234 RS |
241 | enum mpls_payload_type payload_type; |
242 | bool success = false; | |
0189197f | 243 | |
76fecd82 EB |
244 | /* 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 | ||
118d5234 RS |
257 | 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); | |
5b441ac8 | 264 | u8 new_ttl; |
0189197f | 265 | skb->protocol = htons(ETH_P_IP); |
5b441ac8 RS |
266 | |
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 | ||
0189197f EB |
278 | csum_replace2(&hdr4->check, |
279 | htons(hdr4->ttl << 8), | |
5b441ac8 RS |
280 | htons(new_ttl << 8)); |
281 | hdr4->ttl = new_ttl; | |
118d5234 RS |
282 | success = true; |
283 | break; | |
0189197f | 284 | } |
118d5234 | 285 | case MPT_IPV6: { |
0189197f EB |
286 | struct ipv6hdr *hdr6 = ipv6_hdr(skb); |
287 | skb->protocol = htons(ETH_P_IPV6); | |
5b441ac8 RS |
288 | |
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; | |
118d5234 RS |
299 | success = true; |
300 | break; | |
0189197f | 301 | } |
118d5234 | 302 | case MPT_UNSPEC: |
5b441ac8 | 303 | /* Should have decided which protocol it is by now */ |
118d5234 RS |
304 | break; |
305 | } | |
306 | ||
0189197f EB |
307 | return success; |
308 | } | |
309 | ||
310 | static 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; | |
f8efb73c | 316 | struct mpls_nh *nh; |
0189197f EB |
317 | struct mpls_entry_decoded dec; |
318 | struct net_device *out_dev; | |
27d69105 | 319 | struct mpls_dev *out_mdev; |
03c57747 | 320 | struct mpls_dev *mdev; |
0189197f EB |
321 | 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 | ||
03c57747 | 328 | mdev = mpls_dev_get(dev); |
27d69105 | 329 | if (!mdev) |
03c57747 RS |
330 | goto drop; |
331 | ||
27d69105 RS |
332 | 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); | |
0189197f | 337 | goto drop; |
27d69105 RS |
338 | } |
339 | ||
340 | if (skb->pkt_type != PACKET_HOST) | |
341 | goto err; | |
0189197f EB |
342 | |
343 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) | |
27d69105 | 344 | goto err; |
0189197f EB |
345 | |
346 | if (!pskb_may_pull(skb, sizeof(*hdr))) | |
27d69105 | 347 | goto err; |
0189197f EB |
348 | |
349 | /* Read and decode the label */ | |
350 | hdr = mpls_hdr(skb); | |
351 | dec = mpls_entry_decode(hdr); | |
352 | ||
0189197f | 353 | rt = mpls_route_input_rcu(net, dec.label); |
27d69105 RS |
354 | if (!rt) { |
355 | MPLS_INC_STATS(mdev, rx_noroute); | |
0189197f | 356 | goto drop; |
27d69105 | 357 | } |
0189197f | 358 | |
9f427a0e | 359 | nh = mpls_select_multipath(rt, skb); |
f8efb73c | 360 | if (!nh) |
27d69105 | 361 | goto err; |
0189197f | 362 | |
9f427a0e DA |
363 | /* Pop the label */ |
364 | skb_pull(skb, sizeof(*hdr)); | |
365 | skb_reset_network_header(skb); | |
366 | ||
367 | skb_orphan(skb); | |
368 | ||
0189197f | 369 | if (skb_warn_if_lro(skb)) |
27d69105 | 370 | goto err; |
0189197f EB |
371 | |
372 | skb_forward_csum(skb); | |
373 | ||
374 | /* Verify ttl is valid */ | |
aa7da937 | 375 | if (dec.ttl <= 1) |
27d69105 | 376 | goto err; |
0189197f EB |
377 | dec.ttl -= 1; |
378 | ||
27d69105 RS |
379 | /* Find the output device */ |
380 | out_dev = rcu_dereference(nh->nh_dev); | |
381 | if (!mpls_output_possible(out_dev)) | |
382 | goto tx_err; | |
383 | ||
0189197f | 384 | /* Verify the destination can hold the packet */ |
f8efb73c | 385 | new_header_size = mpls_nh_header_size(nh); |
0189197f EB |
386 | mtu = mpls_dev_mtu(out_dev); |
387 | if (mpls_pkt_too_big(skb, mtu - new_header_size)) | |
27d69105 | 388 | goto tx_err; |
0189197f EB |
389 | |
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)) | |
27d69105 | 396 | goto tx_err; |
0189197f EB |
397 | |
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 */ | |
5b441ac8 | 403 | if (!mpls_egress(dev_net(out_dev), rt, skb, dec)) |
27d69105 | 404 | goto err; |
0189197f EB |
405 | } 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; | |
f8efb73c RP |
413 | for (i = nh->nh_labels - 1; i >= 0; i--) { |
414 | hdr[i] = mpls_entry_encode(nh->nh_label[i], | |
415 | dec.ttl, 0, bos); | |
0189197f EB |
416 | bos = false; |
417 | } | |
418 | } | |
419 | ||
27d69105 RS |
420 | mpls_stats_inc_outucastpkts(out_dev, skb); |
421 | ||
eb7809f0 RS |
422 | /* 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); | |
0189197f EB |
429 | if (err) |
430 | net_dbg_ratelimited("%s: packet transmission failed: %d\n", | |
431 | __func__, err); | |
432 | return 0; | |
433 | ||
27d69105 RS |
434 | tx_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; | |
439 | err: | |
440 | MPLS_INC_STATS(mdev, rx_errors); | |
0189197f EB |
441 | drop: |
442 | kfree_skb(skb); | |
443 | return NET_RX_DROP; | |
444 | } | |
445 | ||
446 | static struct packet_type mpls_packet_type __read_mostly = { | |
447 | .type = cpu_to_be16(ETH_P_MPLS_UC), | |
448 | .func = mpls_forward, | |
449 | }; | |
450 | ||
f0126539 | 451 | static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = { |
03c05665 EB |
452 | [RTA_DST] = { .type = NLA_U32 }, |
453 | [RTA_OIF] = { .type = NLA_U32 }, | |
5b441ac8 | 454 | [RTA_TTL_PROPAGATE] = { .type = NLA_U8 }, |
03c05665 EB |
455 | }; |
456 | ||
a2519929 | 457 | struct mpls_route_config { |
118d5234 RS |
458 | u32 rc_protocol; |
459 | u32 rc_ifindex; | |
f8efb73c RP |
460 | u8 rc_via_table; |
461 | u8 rc_via_alen; | |
118d5234 RS |
462 | u8 rc_via[MAX_VIA_ALEN]; |
463 | u32 rc_label; | |
5b441ac8 | 464 | u8 rc_ttl_propagate; |
f8efb73c | 465 | u8 rc_output_labels; |
118d5234 RS |
466 | 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; | |
f8efb73c RP |
470 | struct rtnexthop *rc_mp; |
471 | int rc_mp_len; | |
a2519929 EB |
472 | }; |
473 | ||
59b20966 DA |
474 | /* all nexthops within a route have the same size based on max |
475 | * number of labels and max via length for a hop | |
476 | */ | |
477 | static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels) | |
0189197f | 478 | { |
59b20966 | 479 | u8 nh_size = MPLS_NH_SIZE(max_labels, max_alen); |
0189197f | 480 | struct mpls_route *rt; |
df1c6316 | 481 | size_t size; |
0189197f | 482 | |
df1c6316 DA |
483 | 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); | |
f8efb73c | 495 | |
0189197f EB |
496 | return rt; |
497 | } | |
498 | ||
499 | static void mpls_rt_free(struct mpls_route *rt) | |
500 | { | |
501 | if (rt) | |
502 | kfree_rcu(rt, rt_rcu); | |
503 | } | |
504 | ||
8de147dc EB |
505 | static 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 */ | |
a6affd24 | 515 | if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED)) |
8de147dc EB |
516 | rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags); |
517 | } | |
518 | ||
0189197f | 519 | static void mpls_route_update(struct net *net, unsigned index, |
f8efb73c | 520 | struct mpls_route *new, |
0189197f EB |
521 | const struct nl_info *info) |
522 | { | |
19d0c341 | 523 | struct mpls_route __rcu **platform_label; |
f8efb73c | 524 | struct mpls_route *rt; |
0189197f EB |
525 | |
526 | ASSERT_RTNL(); | |
527 | ||
19d0c341 EB |
528 | platform_label = rtnl_dereference(net->mpls.platform_label); |
529 | rt = rtnl_dereference(platform_label[index]); | |
f8efb73c | 530 | rcu_assign_pointer(platform_label[index], new); |
0189197f | 531 | |
f8efb73c | 532 | mpls_notify_route(net, index, rt, new, info); |
8de147dc | 533 | |
0189197f | 534 | /* If we removed a route free it now */ |
f8efb73c | 535 | mpls_rt_free(rt); |
0189197f EB |
536 | } |
537 | ||
a2519929 EB |
538 | static unsigned find_free_label(struct net *net) |
539 | { | |
19d0c341 EB |
540 | struct mpls_route __rcu **platform_label; |
541 | size_t platform_labels; | |
a2519929 | 542 | unsigned index; |
19d0c341 EB |
543 | |
544 | platform_label = rtnl_dereference(net->mpls.platform_label); | |
545 | platform_labels = net->mpls.platform_labels; | |
a6affd24 RS |
546 | for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels; |
547 | index++) { | |
19d0c341 | 548 | if (!rtnl_dereference(platform_label[index])) |
a2519929 EB |
549 | return index; |
550 | } | |
551 | return LABEL_NOT_SPECIFIED; | |
552 | } | |
553 | ||
bf21563a | 554 | #if IS_ENABLED(CONFIG_INET) |
cf4b24f0 RS |
555 | static struct net_device *inet_fib_lookup_dev(struct net *net, |
556 | const void *addr) | |
01faef2c | 557 | { |
5a9348b5 | 558 | struct net_device *dev; |
01faef2c RP |
559 | 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)) | |
5a9348b5 | 565 | return ERR_CAST(rt); |
01faef2c RP |
566 | |
567 | dev = rt->dst.dev; | |
568 | dev_hold(dev); | |
569 | ||
570 | ip_rt_put(rt); | |
571 | ||
01faef2c | 572 | return dev; |
bf21563a RP |
573 | } |
574 | #else | |
cf4b24f0 RS |
575 | static struct net_device *inet_fib_lookup_dev(struct net *net, |
576 | const void *addr) | |
bf21563a RP |
577 | { |
578 | return ERR_PTR(-EAFNOSUPPORT); | |
01faef2c | 579 | } |
bf21563a | 580 | #endif |
01faef2c | 581 | |
bf21563a | 582 | #if IS_ENABLED(CONFIG_IPV6) |
cf4b24f0 RS |
583 | static struct net_device *inet6_fib_lookup_dev(struct net *net, |
584 | const void *addr) | |
01faef2c | 585 | { |
5a9348b5 | 586 | struct net_device *dev; |
01faef2c RP |
587 | struct dst_entry *dst; |
588 | struct flowi6 fl6; | |
bf21563a RP |
589 | int err; |
590 | ||
591 | if (!ipv6_stub) | |
592 | return ERR_PTR(-EAFNOSUPPORT); | |
01faef2c RP |
593 | |
594 | memset(&fl6, 0, sizeof(fl6)); | |
595 | memcpy(&fl6.daddr, addr, sizeof(struct in6_addr)); | |
bf21563a RP |
596 | err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6); |
597 | if (err) | |
5a9348b5 | 598 | return ERR_PTR(err); |
01faef2c RP |
599 | |
600 | dev = dst->dev; | |
601 | dev_hold(dev); | |
01faef2c RP |
602 | dst_release(dst); |
603 | ||
604 | return dev; | |
605 | } | |
bf21563a | 606 | #else |
cf4b24f0 RS |
607 | static struct net_device *inet6_fib_lookup_dev(struct net *net, |
608 | const void *addr) | |
bf21563a RP |
609 | { |
610 | return ERR_PTR(-EAFNOSUPPORT); | |
611 | } | |
612 | #endif | |
01faef2c RP |
613 | |
614 | static struct net_device *find_outdev(struct net *net, | |
cf4b24f0 | 615 | struct mpls_route *rt, |
f8efb73c | 616 | struct mpls_nh *nh, int oif) |
01faef2c RP |
617 | { |
618 | struct net_device *dev = NULL; | |
619 | ||
f8efb73c RP |
620 | if (!oif) { |
621 | switch (nh->nh_via_table) { | |
01faef2c | 622 | case NEIGH_ARP_TABLE: |
cf4b24f0 | 623 | dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh)); |
01faef2c RP |
624 | break; |
625 | case NEIGH_ND_TABLE: | |
cf4b24f0 | 626 | dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh)); |
01faef2c RP |
627 | break; |
628 | case NEIGH_LINK_TABLE: | |
629 | break; | |
630 | } | |
631 | } else { | |
f8efb73c | 632 | dev = dev_get_by_index(net, oif); |
01faef2c RP |
633 | } |
634 | ||
3dcb615e RP |
635 | if (!dev) |
636 | return ERR_PTR(-ENODEV); | |
637 | ||
94a57f1f RP |
638 | if (IS_ERR(dev)) |
639 | return dev; | |
640 | ||
f8efb73c RP |
641 | /* The caller is holding rtnl anyways, so release the dev reference */ |
642 | dev_put(dev); | |
643 | ||
01faef2c RP |
644 | return dev; |
645 | } | |
646 | ||
cf4b24f0 RS |
647 | static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt, |
648 | struct mpls_nh *nh, int oif) | |
f8efb73c RP |
649 | { |
650 | struct net_device *dev = NULL; | |
651 | int err = -ENODEV; | |
652 | ||
cf4b24f0 | 653 | dev = find_outdev(net, rt, nh, oif); |
f8efb73c RP |
654 | 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 | ||
a3e948e8 RS |
665 | if ((nh->nh_via_table == NEIGH_LINK_TABLE) && |
666 | (dev->addr_len != nh->nh_via_alen)) | |
667 | goto errout; | |
668 | ||
f8efb73c RP |
669 | RCU_INIT_POINTER(nh->nh_dev, dev); |
670 | ||
c89359a4 RP |
671 | 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 | ||
f8efb73c RP |
681 | return 0; |
682 | ||
683 | errout: | |
684 | return err; | |
685 | } | |
686 | ||
687 | static 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; | |
f8efb73c RP |
699 | |
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; | |
cf4b24f0 | 705 | memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen); |
f8efb73c RP |
706 | nh->nh_via_alen = cfg->rc_via_alen; |
707 | ||
cf4b24f0 | 708 | err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex); |
f8efb73c RP |
709 | if (err) |
710 | goto errout; | |
711 | ||
c89359a4 RP |
712 | if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) |
713 | rt->rt_nhn_alive--; | |
714 | ||
f8efb73c RP |
715 | return 0; |
716 | ||
717 | errout: | |
718 | return err; | |
719 | } | |
720 | ||
cf4b24f0 | 721 | static int mpls_nh_build(struct net *net, struct mpls_route *rt, |
c89359a4 | 722 | struct mpls_nh *nh, int oif, struct nlattr *via, |
a4ac8c98 | 723 | struct nlattr *newdst, u8 max_labels) |
f8efb73c RP |
724 | { |
725 | int err = -ENOMEM; | |
726 | ||
727 | if (!nh) | |
728 | goto errout; | |
729 | ||
730 | if (newdst) { | |
a4ac8c98 | 731 | err = nla_get_labels(newdst, max_labels, |
f8efb73c RP |
732 | &nh->nh_labels, nh->nh_label); |
733 | if (err) | |
734 | goto errout; | |
735 | } | |
736 | ||
f20367df RS |
737 | 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 | } | |
f8efb73c | 745 | |
cf4b24f0 | 746 | err = mpls_nh_assign_dev(net, rt, nh, oif); |
f8efb73c RP |
747 | if (err) |
748 | goto errout; | |
749 | ||
750 | return 0; | |
751 | ||
752 | errout: | |
753 | return err; | |
754 | } | |
755 | ||
77ef013a | 756 | static u8 mpls_count_nexthops(struct rtnexthop *rtnh, int len, |
a4ac8c98 DA |
757 | u8 cfg_via_alen, u8 *max_via_alen, |
758 | u8 *max_labels) | |
f8efb73c | 759 | { |
f8efb73c | 760 | int remaining = len; |
77ef013a | 761 | u8 nhs = 0; |
f8efb73c | 762 | |
cf4b24f0 | 763 | *max_via_alen = 0; |
a4ac8c98 | 764 | *max_labels = 0; |
cf4b24f0 | 765 | |
f8efb73c | 766 | while (rtnh_ok(rtnh, remaining)) { |
cf4b24f0 RS |
767 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
768 | int attrlen; | |
a4ac8c98 | 769 | u8 n_labels = 0; |
cf4b24f0 RS |
770 | |
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 | ||
a4ac8c98 DA |
783 | 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 | ||
77ef013a DA |
790 | /* number of nexthops is tracked by a u8. |
791 | * Check for overflow. | |
792 | */ | |
793 | if (nhs == 255) | |
794 | return 0; | |
f8efb73c | 795 | nhs++; |
77ef013a | 796 | |
f8efb73c RP |
797 | rtnh = rtnh_next(rtnh, &remaining); |
798 | } | |
799 | ||
800 | /* leftover implies invalid nexthop configuration, discard it */ | |
801 | return remaining > 0 ? 0 : nhs; | |
802 | } | |
803 | ||
804 | static int mpls_nh_build_multi(struct mpls_route_config *cfg, | |
a4ac8c98 | 805 | struct mpls_route *rt, u8 max_labels) |
f8efb73c RP |
806 | { |
807 | struct rtnexthop *rtnh = cfg->rc_mp; | |
808 | struct nlattr *nla_via, *nla_newdst; | |
809 | int remaining = cfg->rc_mp_len; | |
f8efb73c | 810 | int err = 0; |
77ef013a | 811 | u8 nhs = 0; |
f8efb73c RP |
812 | |
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 | ||
1c78efa8 RS |
823 | /* neither weighted multipath nor any flags |
824 | * are supported | |
825 | */ | |
826 | if (rtnh->rtnh_hops || rtnh->rtnh_flags) | |
827 | goto errout; | |
828 | ||
f8efb73c RP |
829 | 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 | ||
cf4b24f0 | 837 | err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh, |
a4ac8c98 DA |
838 | rtnh->rtnh_ifindex, nla_via, nla_newdst, |
839 | max_labels); | |
f8efb73c RP |
840 | if (err) |
841 | goto errout; | |
842 | ||
c89359a4 RP |
843 | if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) |
844 | rt->rt_nhn_alive--; | |
845 | ||
f8efb73c RP |
846 | rtnh = rtnh_next(rtnh, &remaining); |
847 | nhs++; | |
848 | } endfor_nexthops(rt); | |
849 | ||
850 | rt->rt_nhn = nhs; | |
851 | ||
852 | return 0; | |
853 | ||
854 | errout: | |
855 | return err; | |
856 | } | |
857 | ||
a2519929 EB |
858 | static int mpls_route_add(struct mpls_route_config *cfg) |
859 | { | |
19d0c341 | 860 | struct mpls_route __rcu **platform_label; |
a2519929 | 861 | struct net *net = cfg->rc_nlinfo.nl_net; |
a2519929 | 862 | struct mpls_route *rt, *old; |
a2519929 | 863 | int err = -EINVAL; |
cf4b24f0 | 864 | u8 max_via_alen; |
f8efb73c | 865 | unsigned index; |
a4ac8c98 | 866 | u8 max_labels; |
77ef013a | 867 | u8 nhs; |
a2519929 EB |
868 | |
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 | ||
a6affd24 RS |
877 | /* Reserved labels may not be set */ |
878 | if (index < MPLS_LABEL_FIRST_UNRESERVED) | |
a2519929 EB |
879 | goto errout; |
880 | ||
881 | /* The full 20 bit range may not be supported. */ | |
882 | if (index >= net->mpls.platform_labels) | |
883 | goto errout; | |
884 | ||
a2519929 | 885 | /* Append makes no sense with mpls */ |
0f7bbd58 | 886 | err = -EOPNOTSUPP; |
a2519929 EB |
887 | if (cfg->rc_nlflags & NLM_F_APPEND) |
888 | goto errout; | |
889 | ||
890 | err = -EEXIST; | |
19d0c341 EB |
891 | platform_label = rtnl_dereference(net->mpls.platform_label); |
892 | old = rtnl_dereference(platform_label[index]); | |
a2519929 EB |
893 | 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 | ||
cf4b24f0 | 904 | err = -EINVAL; |
a4ac8c98 DA |
905 | 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 | ||
cf4b24f0 RS |
915 | if (nhs == 0) |
916 | goto errout; | |
f8efb73c | 917 | |
a2519929 | 918 | err = -ENOMEM; |
a4ac8c98 | 919 | rt = mpls_rt_alloc(nhs, max_via_alen, max_labels); |
df1c6316 DA |
920 | if (IS_ERR(rt)) { |
921 | err = PTR_ERR(rt); | |
a2519929 | 922 | goto errout; |
df1c6316 | 923 | } |
a2519929 | 924 | |
a2519929 | 925 | rt->rt_protocol = cfg->rc_protocol; |
118d5234 | 926 | rt->rt_payload_type = cfg->rc_payload_type; |
5b441ac8 | 927 | rt->rt_ttl_propagate = cfg->rc_ttl_propagate; |
a2519929 | 928 | |
f8efb73c | 929 | if (cfg->rc_mp) |
a4ac8c98 | 930 | err = mpls_nh_build_multi(cfg, rt, max_labels); |
f8efb73c RP |
931 | else |
932 | err = mpls_nh_build_from_cfg(cfg, rt); | |
933 | if (err) | |
934 | goto freert; | |
935 | ||
936 | mpls_route_update(net, index, rt, &cfg->rc_nlinfo); | |
a2519929 | 937 | |
a2519929 EB |
938 | return 0; |
939 | ||
f8efb73c RP |
940 | freert: |
941 | mpls_rt_free(rt); | |
a2519929 | 942 | errout: |
a2519929 EB |
943 | return err; |
944 | } | |
945 | ||
946 | static 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 | ||
a6affd24 RS |
954 | /* Reserved labels may not be removed */ |
955 | if (index < MPLS_LABEL_FIRST_UNRESERVED) | |
a2519929 EB |
956 | goto errout; |
957 | ||
958 | /* The full 20 bit range may not be supported */ | |
959 | if (index >= net->mpls.platform_labels) | |
960 | goto errout; | |
961 | ||
f8efb73c | 962 | mpls_route_update(net, index, NULL, &cfg->rc_nlinfo); |
a2519929 EB |
963 | |
964 | err = 0; | |
965 | errout: | |
966 | return err; | |
967 | } | |
968 | ||
27d69105 RS |
969 | static 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 | ||
999 | static 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 | ||
1022 | static 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 | ||
24045a03 DA |
1033 | static 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 | ||
1063 | nla_put_failure: | |
1064 | nlmsg_cancel(skb, nlh); | |
1065 | return -EMSGSIZE; | |
1066 | } | |
1067 | ||
1068 | static 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 | ||
823566ae DA |
1083 | static void mpls_netconf_notify_devconf(struct net *net, int event, |
1084 | int type, struct mpls_dev *mdev) | |
24045a03 DA |
1085 | { |
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 | ||
823566ae | 1093 | err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type); |
24045a03 DA |
1094 | 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; | |
1103 | errout: | |
1104 | if (err < 0) | |
1105 | rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err); | |
1106 | } | |
1107 | ||
1108 | static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = { | |
1109 | [NETCONFA_IFINDEX] = { .len = sizeof(int) }, | |
1110 | }; | |
1111 | ||
1112 | static int mpls_netconf_get_devconf(struct sk_buff *in_skb, | |
1113 | struct nlmsghdr *nlh) | |
1114 | { | |
1115 | struct net *net = sock_net(in_skb->sk); | |
1116 | struct nlattr *tb[NETCONFA_MAX + 1]; | |
1117 | struct netconfmsg *ncm; | |
1118 | struct net_device *dev; | |
1119 | struct mpls_dev *mdev; | |
1120 | struct sk_buff *skb; | |
1121 | int ifindex; | |
1122 | int err; | |
1123 | ||
1124 | err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX, | |
fceb6435 | 1125 | devconf_mpls_policy, NULL); |
24045a03 DA |
1126 | if (err < 0) |
1127 | goto errout; | |
1128 | ||
1129 | err = -EINVAL; | |
1130 | if (!tb[NETCONFA_IFINDEX]) | |
1131 | goto errout; | |
1132 | ||
1133 | ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); | |
1134 | dev = __dev_get_by_index(net, ifindex); | |
1135 | if (!dev) | |
1136 | goto errout; | |
1137 | ||
1138 | mdev = mpls_dev_get(dev); | |
1139 | if (!mdev) | |
1140 | goto errout; | |
1141 | ||
1142 | err = -ENOBUFS; | |
1143 | skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL); | |
1144 | if (!skb) | |
1145 | goto errout; | |
1146 | ||
1147 | err = mpls_netconf_fill_devconf(skb, mdev, | |
1148 | NETLINK_CB(in_skb).portid, | |
1149 | nlh->nlmsg_seq, RTM_NEWNETCONF, 0, | |
1150 | NETCONFA_ALL); | |
1151 | if (err < 0) { | |
1152 | /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */ | |
1153 | WARN_ON(err == -EMSGSIZE); | |
1154 | kfree_skb(skb); | |
1155 | goto errout; | |
1156 | } | |
1157 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); | |
1158 | errout: | |
1159 | return err; | |
1160 | } | |
1161 | ||
1162 | static int mpls_netconf_dump_devconf(struct sk_buff *skb, | |
1163 | struct netlink_callback *cb) | |
1164 | { | |
1165 | struct net *net = sock_net(skb->sk); | |
1166 | struct hlist_head *head; | |
1167 | struct net_device *dev; | |
1168 | struct mpls_dev *mdev; | |
1169 | int idx, s_idx; | |
1170 | int h, s_h; | |
1171 | ||
1172 | s_h = cb->args[0]; | |
1173 | s_idx = idx = cb->args[1]; | |
1174 | ||
1175 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { | |
1176 | idx = 0; | |
1177 | head = &net->dev_index_head[h]; | |
1178 | rcu_read_lock(); | |
1179 | cb->seq = net->dev_base_seq; | |
1180 | hlist_for_each_entry_rcu(dev, head, index_hlist) { | |
1181 | if (idx < s_idx) | |
1182 | goto cont; | |
1183 | mdev = mpls_dev_get(dev); | |
1184 | if (!mdev) | |
1185 | goto cont; | |
1186 | if (mpls_netconf_fill_devconf(skb, mdev, | |
1187 | NETLINK_CB(cb->skb).portid, | |
1188 | cb->nlh->nlmsg_seq, | |
1189 | RTM_NEWNETCONF, | |
1190 | NLM_F_MULTI, | |
1191 | NETCONFA_ALL) < 0) { | |
1192 | rcu_read_unlock(); | |
1193 | goto done; | |
1194 | } | |
1195 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
1196 | cont: | |
1197 | idx++; | |
1198 | } | |
1199 | rcu_read_unlock(); | |
1200 | } | |
1201 | done: | |
1202 | cb->args[0] = h; | |
1203 | cb->args[1] = idx; | |
1204 | ||
1205 | return skb->len; | |
1206 | } | |
1207 | ||
37bde799 RS |
1208 | #define MPLS_PERDEV_SYSCTL_OFFSET(field) \ |
1209 | (&((struct mpls_dev *)0)->field) | |
1210 | ||
24045a03 DA |
1211 | static int mpls_conf_proc(struct ctl_table *ctl, int write, |
1212 | void __user *buffer, | |
1213 | size_t *lenp, loff_t *ppos) | |
1214 | { | |
1215 | int oval = *(int *)ctl->data; | |
1216 | int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); | |
1217 | ||
1218 | if (write) { | |
1219 | struct mpls_dev *mdev = ctl->extra1; | |
1220 | int i = (int *)ctl->data - (int *)mdev; | |
1221 | struct net *net = ctl->extra2; | |
1222 | int val = *(int *)ctl->data; | |
1223 | ||
1224 | if (i == offsetof(struct mpls_dev, input_enabled) && | |
1225 | val != oval) { | |
823566ae DA |
1226 | mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, |
1227 | NETCONFA_INPUT, mdev); | |
24045a03 DA |
1228 | } |
1229 | } | |
1230 | ||
1231 | return ret; | |
1232 | } | |
1233 | ||
37bde799 RS |
1234 | static const struct ctl_table mpls_dev_table[] = { |
1235 | { | |
1236 | .procname = "input", | |
1237 | .maxlen = sizeof(int), | |
1238 | .mode = 0644, | |
24045a03 | 1239 | .proc_handler = mpls_conf_proc, |
37bde799 RS |
1240 | .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled), |
1241 | }, | |
1242 | { } | |
1243 | }; | |
1244 | ||
1245 | static int mpls_dev_sysctl_register(struct net_device *dev, | |
1246 | struct mpls_dev *mdev) | |
1247 | { | |
1248 | char path[sizeof("net/mpls/conf/") + IFNAMSIZ]; | |
24045a03 | 1249 | struct net *net = dev_net(dev); |
37bde799 RS |
1250 | struct ctl_table *table; |
1251 | int i; | |
1252 | ||
1253 | table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL); | |
1254 | if (!table) | |
1255 | goto out; | |
1256 | ||
1257 | /* Table data contains only offsets relative to the base of | |
1258 | * the mdev at this point, so make them absolute. | |
1259 | */ | |
24045a03 | 1260 | for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) { |
37bde799 | 1261 | table[i].data = (char *)mdev + (uintptr_t)table[i].data; |
24045a03 DA |
1262 | table[i].extra1 = mdev; |
1263 | table[i].extra2 = net; | |
1264 | } | |
37bde799 RS |
1265 | |
1266 | snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name); | |
1267 | ||
1182e4d0 | 1268 | mdev->sysctl = register_net_sysctl(net, path, table); |
37bde799 RS |
1269 | if (!mdev->sysctl) |
1270 | goto free; | |
1271 | ||
1182e4d0 | 1272 | mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev); |
37bde799 RS |
1273 | return 0; |
1274 | ||
1275 | free: | |
1276 | kfree(table); | |
1277 | out: | |
1278 | return -ENOBUFS; | |
1279 | } | |
1280 | ||
1182e4d0 DA |
1281 | static void mpls_dev_sysctl_unregister(struct net_device *dev, |
1282 | struct mpls_dev *mdev) | |
37bde799 | 1283 | { |
1182e4d0 | 1284 | struct net *net = dev_net(dev); |
37bde799 RS |
1285 | struct ctl_table *table; |
1286 | ||
1287 | table = mdev->sysctl->ctl_table_arg; | |
1288 | unregister_net_sysctl_table(mdev->sysctl); | |
1289 | kfree(table); | |
1182e4d0 DA |
1290 | |
1291 | mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev); | |
37bde799 RS |
1292 | } |
1293 | ||
03c57747 RS |
1294 | static struct mpls_dev *mpls_add_dev(struct net_device *dev) |
1295 | { | |
1296 | struct mpls_dev *mdev; | |
1297 | int err = -ENOMEM; | |
27d69105 | 1298 | int i; |
03c57747 RS |
1299 | |
1300 | ASSERT_RTNL(); | |
1301 | ||
1302 | mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); | |
1303 | if (!mdev) | |
1304 | return ERR_PTR(err); | |
1305 | ||
27d69105 RS |
1306 | mdev->stats = alloc_percpu(struct mpls_pcpu_stats); |
1307 | if (!mdev->stats) | |
1308 | goto free; | |
1309 | ||
1310 | for_each_possible_cpu(i) { | |
1311 | struct mpls_pcpu_stats *mpls_stats; | |
1312 | ||
1313 | mpls_stats = per_cpu_ptr(mdev->stats, i); | |
1314 | u64_stats_init(&mpls_stats->syncp); | |
1315 | } | |
1316 | ||
1182e4d0 DA |
1317 | mdev->dev = dev; |
1318 | ||
37bde799 RS |
1319 | err = mpls_dev_sysctl_register(dev, mdev); |
1320 | if (err) | |
1321 | goto free; | |
1322 | ||
03c57747 RS |
1323 | rcu_assign_pointer(dev->mpls_ptr, mdev); |
1324 | ||
1325 | return mdev; | |
37bde799 RS |
1326 | |
1327 | free: | |
27d69105 | 1328 | free_percpu(mdev->stats); |
37bde799 RS |
1329 | kfree(mdev); |
1330 | return ERR_PTR(err); | |
03c57747 RS |
1331 | } |
1332 | ||
27d69105 RS |
1333 | static void mpls_dev_destroy_rcu(struct rcu_head *head) |
1334 | { | |
1335 | struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu); | |
1336 | ||
1337 | free_percpu(mdev->stats); | |
1338 | kfree(mdev); | |
1339 | } | |
1340 | ||
c89359a4 | 1341 | static void mpls_ifdown(struct net_device *dev, int event) |
0189197f | 1342 | { |
19d0c341 | 1343 | struct mpls_route __rcu **platform_label; |
0189197f | 1344 | struct net *net = dev_net(dev); |
77ef013a | 1345 | u8 alive, deleted; |
0189197f EB |
1346 | unsigned index; |
1347 | ||
19d0c341 | 1348 | platform_label = rtnl_dereference(net->mpls.platform_label); |
0189197f | 1349 | for (index = 0; index < net->mpls.platform_labels; index++) { |
19d0c341 | 1350 | struct mpls_route *rt = rtnl_dereference(platform_label[index]); |
c89359a4 | 1351 | |
0189197f EB |
1352 | if (!rt) |
1353 | continue; | |
c89359a4 | 1354 | |
61733c91 | 1355 | alive = 0; |
4ea8efad | 1356 | deleted = 0; |
c89359a4 | 1357 | change_nexthops(rt) { |
39eb8cd1 DA |
1358 | unsigned int nh_flags = nh->nh_flags; |
1359 | ||
f8efb73c | 1360 | if (rtnl_dereference(nh->nh_dev) != dev) |
61733c91 DA |
1361 | goto next; |
1362 | ||
c89359a4 RP |
1363 | switch (event) { |
1364 | case NETDEV_DOWN: | |
1365 | case NETDEV_UNREGISTER: | |
39eb8cd1 | 1366 | nh_flags |= RTNH_F_DEAD; |
c89359a4 RP |
1367 | /* fall through */ |
1368 | case NETDEV_CHANGE: | |
39eb8cd1 | 1369 | nh_flags |= RTNH_F_LINKDOWN; |
c89359a4 RP |
1370 | break; |
1371 | } | |
1372 | if (event == NETDEV_UNREGISTER) | |
1373 | RCU_INIT_POINTER(nh->nh_dev, NULL); | |
39eb8cd1 DA |
1374 | |
1375 | if (nh->nh_flags != nh_flags) | |
1376 | WRITE_ONCE(nh->nh_flags, nh_flags); | |
61733c91 | 1377 | next: |
39eb8cd1 | 1378 | if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))) |
61733c91 | 1379 | alive++; |
4ea8efad DA |
1380 | if (!rtnl_dereference(nh->nh_dev)) |
1381 | deleted++; | |
f8efb73c | 1382 | } endfor_nexthops(rt); |
61733c91 DA |
1383 | |
1384 | WRITE_ONCE(rt->rt_nhn_alive, alive); | |
4ea8efad DA |
1385 | |
1386 | /* if there are no more nexthops, delete the route */ | |
1387 | if (event == NETDEV_UNREGISTER && deleted == rt->rt_nhn) | |
1388 | mpls_route_update(net, index, NULL, NULL); | |
0189197f | 1389 | } |
c89359a4 RP |
1390 | } |
1391 | ||
39eb8cd1 | 1392 | static void mpls_ifup(struct net_device *dev, unsigned int flags) |
c89359a4 RP |
1393 | { |
1394 | struct mpls_route __rcu **platform_label; | |
1395 | struct net *net = dev_net(dev); | |
1396 | unsigned index; | |
77ef013a | 1397 | u8 alive; |
c89359a4 RP |
1398 | |
1399 | platform_label = rtnl_dereference(net->mpls.platform_label); | |
1400 | for (index = 0; index < net->mpls.platform_labels; index++) { | |
1401 | struct mpls_route *rt = rtnl_dereference(platform_label[index]); | |
1402 | ||
1403 | if (!rt) | |
1404 | continue; | |
37bde799 | 1405 | |
c89359a4 RP |
1406 | alive = 0; |
1407 | change_nexthops(rt) { | |
39eb8cd1 | 1408 | unsigned int nh_flags = nh->nh_flags; |
c89359a4 RP |
1409 | struct net_device *nh_dev = |
1410 | rtnl_dereference(nh->nh_dev); | |
03c57747 | 1411 | |
39eb8cd1 | 1412 | if (!(nh_flags & flags)) { |
c89359a4 RP |
1413 | alive++; |
1414 | continue; | |
1415 | } | |
1416 | if (nh_dev != dev) | |
1417 | continue; | |
1418 | alive++; | |
39eb8cd1 DA |
1419 | nh_flags &= ~flags; |
1420 | WRITE_ONCE(nh->nh_flags, flags); | |
c89359a4 RP |
1421 | } endfor_nexthops(rt); |
1422 | ||
39eb8cd1 | 1423 | WRITE_ONCE(rt->rt_nhn_alive, alive); |
c89359a4 | 1424 | } |
0189197f EB |
1425 | } |
1426 | ||
1427 | static int mpls_dev_notify(struct notifier_block *this, unsigned long event, | |
1428 | void *ptr) | |
1429 | { | |
1430 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
03c57747 | 1431 | struct mpls_dev *mdev; |
c89359a4 | 1432 | unsigned int flags; |
0189197f | 1433 | |
c89359a4 | 1434 | if (event == NETDEV_REGISTER) { |
407f31be | 1435 | /* For now just support Ethernet, IPGRE, SIT and IPIP devices */ |
0d227a86 SH |
1436 | if (dev->type == ARPHRD_ETHER || |
1437 | dev->type == ARPHRD_LOOPBACK || | |
407f31be SH |
1438 | dev->type == ARPHRD_IPGRE || |
1439 | dev->type == ARPHRD_SIT || | |
1440 | dev->type == ARPHRD_TUNNEL) { | |
03c57747 RS |
1441 | mdev = mpls_add_dev(dev); |
1442 | if (IS_ERR(mdev)) | |
1443 | return notifier_from_errno(PTR_ERR(mdev)); | |
1444 | } | |
c89359a4 RP |
1445 | return NOTIFY_OK; |
1446 | } | |
03c57747 | 1447 | |
c89359a4 RP |
1448 | mdev = mpls_dev_get(dev); |
1449 | if (!mdev) | |
1450 | return NOTIFY_OK; | |
1451 | ||
1452 | switch (event) { | |
1453 | case NETDEV_DOWN: | |
1454 | mpls_ifdown(dev, event); | |
1455 | break; | |
1456 | case NETDEV_UP: | |
1457 | flags = dev_get_flags(dev); | |
1458 | if (flags & (IFF_RUNNING | IFF_LOWER_UP)) | |
1459 | mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN); | |
1460 | else | |
1461 | mpls_ifup(dev, RTNH_F_DEAD); | |
1462 | break; | |
1463 | case NETDEV_CHANGE: | |
1464 | flags = dev_get_flags(dev); | |
1465 | if (flags & (IFF_RUNNING | IFF_LOWER_UP)) | |
1466 | mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN); | |
1467 | else | |
1468 | mpls_ifdown(dev, event); | |
1469 | break; | |
0189197f | 1470 | case NETDEV_UNREGISTER: |
c89359a4 RP |
1471 | mpls_ifdown(dev, event); |
1472 | mdev = mpls_dev_get(dev); | |
1473 | if (mdev) { | |
1182e4d0 | 1474 | mpls_dev_sysctl_unregister(dev, mdev); |
c89359a4 | 1475 | RCU_INIT_POINTER(dev->mpls_ptr, NULL); |
27d69105 | 1476 | call_rcu(&mdev->rcu, mpls_dev_destroy_rcu); |
c89359a4 | 1477 | } |
0189197f | 1478 | break; |
0fae3bf0 RS |
1479 | case NETDEV_CHANGENAME: |
1480 | mdev = mpls_dev_get(dev); | |
1481 | if (mdev) { | |
1482 | int err; | |
1483 | ||
1182e4d0 | 1484 | mpls_dev_sysctl_unregister(dev, mdev); |
0fae3bf0 RS |
1485 | err = mpls_dev_sysctl_register(dev, mdev); |
1486 | if (err) | |
1487 | return notifier_from_errno(err); | |
1488 | } | |
1489 | break; | |
0189197f EB |
1490 | } |
1491 | return NOTIFY_OK; | |
1492 | } | |
1493 | ||
1494 | static struct notifier_block mpls_dev_notifier = { | |
1495 | .notifier_call = mpls_dev_notify, | |
1496 | }; | |
1497 | ||
03c05665 | 1498 | static int nla_put_via(struct sk_buff *skb, |
b79bda3d | 1499 | u8 table, const void *addr, int alen) |
03c05665 | 1500 | { |
b79bda3d EB |
1501 | static const int table_to_family[NEIGH_NR_TABLES + 1] = { |
1502 | AF_INET, AF_INET6, AF_DECnet, AF_PACKET, | |
1503 | }; | |
03c05665 EB |
1504 | struct nlattr *nla; |
1505 | struct rtvia *via; | |
b79bda3d | 1506 | int family = AF_UNSPEC; |
03c05665 EB |
1507 | |
1508 | nla = nla_reserve(skb, RTA_VIA, alen + 2); | |
1509 | if (!nla) | |
1510 | return -EMSGSIZE; | |
1511 | ||
b79bda3d EB |
1512 | if (table <= NEIGH_NR_TABLES) |
1513 | family = table_to_family[table]; | |
1514 | ||
03c05665 EB |
1515 | via = nla_data(nla); |
1516 | via->rtvia_family = family; | |
1517 | memcpy(via->rtvia_addr, addr, alen); | |
1518 | return 0; | |
1519 | } | |
1520 | ||
966bae33 EB |
1521 | int nla_put_labels(struct sk_buff *skb, int attrtype, |
1522 | u8 labels, const u32 label[]) | |
1523 | { | |
1524 | struct nlattr *nla; | |
1525 | struct mpls_shim_hdr *nla_label; | |
1526 | bool bos; | |
1527 | int i; | |
1528 | nla = nla_reserve(skb, attrtype, labels*4); | |
1529 | if (!nla) | |
1530 | return -EMSGSIZE; | |
1531 | ||
1532 | nla_label = nla_data(nla); | |
1533 | bos = true; | |
1534 | for (i = labels - 1; i >= 0; i--) { | |
1535 | nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos); | |
1536 | bos = false; | |
1537 | } | |
1538 | ||
1539 | return 0; | |
1540 | } | |
face0188 | 1541 | EXPORT_SYMBOL_GPL(nla_put_labels); |
966bae33 EB |
1542 | |
1543 | int nla_get_labels(const struct nlattr *nla, | |
a4ac8c98 | 1544 | u8 max_labels, u8 *labels, u32 label[]) |
966bae33 EB |
1545 | { |
1546 | unsigned len = nla_len(nla); | |
966bae33 | 1547 | struct mpls_shim_hdr *nla_label; |
a4ac8c98 | 1548 | u8 nla_labels; |
966bae33 EB |
1549 | bool bos; |
1550 | int i; | |
1551 | ||
a4ac8c98 DA |
1552 | /* len needs to be an even multiple of 4 (the label size). Number |
1553 | * of labels is a u8 so check for overflow. | |
1554 | */ | |
1555 | if (len & 3 || len / 4 > 255) | |
966bae33 EB |
1556 | return -EINVAL; |
1557 | ||
1558 | /* Limit the number of new labels allowed */ | |
1559 | nla_labels = len/4; | |
1560 | if (nla_labels > max_labels) | |
1561 | return -EINVAL; | |
1562 | ||
a4ac8c98 DA |
1563 | /* when label == NULL, caller wants number of labels */ |
1564 | if (!label) | |
1565 | goto out; | |
1566 | ||
966bae33 EB |
1567 | nla_label = nla_data(nla); |
1568 | bos = true; | |
1569 | for (i = nla_labels - 1; i >= 0; i--, bos = false) { | |
1570 | struct mpls_entry_decoded dec; | |
1571 | dec = mpls_entry_decode(nla_label + i); | |
1572 | ||
1573 | /* Ensure the bottom of stack flag is properly set | |
1574 | * and ttl and tc are both clear. | |
1575 | */ | |
1576 | if ((dec.bos != bos) || dec.ttl || dec.tc) | |
1577 | return -EINVAL; | |
1578 | ||
5a9ab017 | 1579 | switch (dec.label) { |
78f5b899 | 1580 | case MPLS_LABEL_IMPLNULL: |
5a9ab017 RS |
1581 | /* RFC3032: This is a label that an LSR may |
1582 | * assign and distribute, but which never | |
1583 | * actually appears in the encapsulation. | |
1584 | */ | |
1585 | return -EINVAL; | |
1586 | } | |
1587 | ||
966bae33 EB |
1588 | label[i] = dec.label; |
1589 | } | |
a4ac8c98 | 1590 | out: |
966bae33 EB |
1591 | *labels = nla_labels; |
1592 | return 0; | |
1593 | } | |
face0188 | 1594 | EXPORT_SYMBOL_GPL(nla_get_labels); |
966bae33 | 1595 | |
f8efb73c RP |
1596 | int nla_get_via(const struct nlattr *nla, u8 *via_alen, |
1597 | u8 *via_table, u8 via_addr[]) | |
1598 | { | |
1599 | struct rtvia *via = nla_data(nla); | |
1600 | int err = -EINVAL; | |
1601 | int alen; | |
1602 | ||
1603 | if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) | |
1604 | goto errout; | |
1605 | alen = nla_len(nla) - | |
1606 | offsetof(struct rtvia, rtvia_addr); | |
1607 | if (alen > MAX_VIA_ALEN) | |
1608 | goto errout; | |
1609 | ||
1610 | /* Validate the address family */ | |
1611 | switch (via->rtvia_family) { | |
1612 | case AF_PACKET: | |
1613 | *via_table = NEIGH_LINK_TABLE; | |
1614 | break; | |
1615 | case AF_INET: | |
1616 | *via_table = NEIGH_ARP_TABLE; | |
1617 | if (alen != 4) | |
1618 | goto errout; | |
1619 | break; | |
1620 | case AF_INET6: | |
1621 | *via_table = NEIGH_ND_TABLE; | |
1622 | if (alen != 16) | |
1623 | goto errout; | |
1624 | break; | |
1625 | default: | |
1626 | /* Unsupported address family */ | |
1627 | goto errout; | |
1628 | } | |
1629 | ||
1630 | memcpy(via_addr, via->rtvia_addr, alen); | |
1631 | *via_alen = alen; | |
1632 | err = 0; | |
1633 | ||
1634 | errout: | |
1635 | return err; | |
1636 | } | |
1637 | ||
03c05665 EB |
1638 | static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh, |
1639 | struct mpls_route_config *cfg) | |
1640 | { | |
1641 | struct rtmsg *rtm; | |
1642 | struct nlattr *tb[RTA_MAX+1]; | |
1643 | int index; | |
1644 | int err; | |
1645 | ||
fceb6435 JB |
1646 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy, |
1647 | NULL); | |
03c05665 EB |
1648 | if (err < 0) |
1649 | goto errout; | |
1650 | ||
1651 | err = -EINVAL; | |
1652 | rtm = nlmsg_data(nlh); | |
03c05665 EB |
1653 | |
1654 | if (rtm->rtm_family != AF_MPLS) | |
1655 | goto errout; | |
1656 | if (rtm->rtm_dst_len != 20) | |
1657 | goto errout; | |
1658 | if (rtm->rtm_src_len != 0) | |
1659 | goto errout; | |
1660 | if (rtm->rtm_tos != 0) | |
1661 | goto errout; | |
1662 | if (rtm->rtm_table != RT_TABLE_MAIN) | |
1663 | goto errout; | |
1664 | /* Any value is acceptable for rtm_protocol */ | |
1665 | ||
1666 | /* As mpls uses destination specific addresses | |
1667 | * (or source specific address in the case of multicast) | |
1668 | * all addresses have universal scope. | |
1669 | */ | |
1670 | if (rtm->rtm_scope != RT_SCOPE_UNIVERSE) | |
1671 | goto errout; | |
1672 | if (rtm->rtm_type != RTN_UNICAST) | |
1673 | goto errout; | |
1674 | if (rtm->rtm_flags != 0) | |
1675 | goto errout; | |
1676 | ||
1677 | cfg->rc_label = LABEL_NOT_SPECIFIED; | |
1678 | cfg->rc_protocol = rtm->rtm_protocol; | |
eb7809f0 | 1679 | cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC; |
5b441ac8 | 1680 | cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT; |
03c05665 EB |
1681 | cfg->rc_nlflags = nlh->nlmsg_flags; |
1682 | cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid; | |
1683 | cfg->rc_nlinfo.nlh = nlh; | |
1684 | cfg->rc_nlinfo.nl_net = sock_net(skb->sk); | |
1685 | ||
1686 | for (index = 0; index <= RTA_MAX; index++) { | |
1687 | struct nlattr *nla = tb[index]; | |
1688 | if (!nla) | |
1689 | continue; | |
1690 | ||
14dd3e1b | 1691 | switch (index) { |
03c05665 EB |
1692 | case RTA_OIF: |
1693 | cfg->rc_ifindex = nla_get_u32(nla); | |
1694 | break; | |
1695 | case RTA_NEWDST: | |
1696 | if (nla_get_labels(nla, MAX_NEW_LABELS, | |
1697 | &cfg->rc_output_labels, | |
1698 | cfg->rc_output_label)) | |
1699 | goto errout; | |
1700 | break; | |
1701 | case RTA_DST: | |
1702 | { | |
f8efb73c | 1703 | u8 label_count; |
03c05665 EB |
1704 | if (nla_get_labels(nla, 1, &label_count, |
1705 | &cfg->rc_label)) | |
1706 | goto errout; | |
1707 | ||
a6affd24 RS |
1708 | /* Reserved labels may not be set */ |
1709 | if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED) | |
03c05665 EB |
1710 | goto errout; |
1711 | ||
1712 | break; | |
1713 | } | |
1714 | case RTA_VIA: | |
1715 | { | |
f8efb73c RP |
1716 | if (nla_get_via(nla, &cfg->rc_via_alen, |
1717 | &cfg->rc_via_table, cfg->rc_via)) | |
03c05665 | 1718 | goto errout; |
f8efb73c RP |
1719 | break; |
1720 | } | |
1721 | case RTA_MULTIPATH: | |
1722 | { | |
1723 | cfg->rc_mp = nla_data(nla); | |
1724 | cfg->rc_mp_len = nla_len(nla); | |
03c05665 EB |
1725 | break; |
1726 | } | |
5b441ac8 RS |
1727 | case RTA_TTL_PROPAGATE: |
1728 | { | |
1729 | u8 ttl_propagate = nla_get_u8(nla); | |
1730 | ||
1731 | if (ttl_propagate > 1) | |
1732 | goto errout; | |
1733 | cfg->rc_ttl_propagate = ttl_propagate ? | |
1734 | MPLS_TTL_PROP_ENABLED : | |
1735 | MPLS_TTL_PROP_DISABLED; | |
1736 | break; | |
1737 | } | |
03c05665 EB |
1738 | default: |
1739 | /* Unsupported attribute */ | |
1740 | goto errout; | |
1741 | } | |
1742 | } | |
1743 | ||
1744 | err = 0; | |
1745 | errout: | |
1746 | return err; | |
1747 | } | |
1748 | ||
1749 | static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh) | |
1750 | { | |
a4ac8c98 | 1751 | struct mpls_route_config *cfg; |
03c05665 EB |
1752 | int err; |
1753 | ||
a4ac8c98 DA |
1754 | cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); |
1755 | if (!cfg) | |
1756 | return -ENOMEM; | |
1757 | ||
1758 | err = rtm_to_route_config(skb, nlh, cfg); | |
03c05665 | 1759 | if (err < 0) |
a4ac8c98 | 1760 | goto out; |
03c05665 | 1761 | |
a4ac8c98 DA |
1762 | err = mpls_route_del(cfg); |
1763 | out: | |
1764 | kfree(cfg); | |
1765 | ||
1766 | return err; | |
03c05665 EB |
1767 | } |
1768 | ||
1769 | ||
1770 | static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh) | |
1771 | { | |
a4ac8c98 | 1772 | struct mpls_route_config *cfg; |
03c05665 EB |
1773 | int err; |
1774 | ||
a4ac8c98 DA |
1775 | cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); |
1776 | if (!cfg) | |
1777 | return -ENOMEM; | |
1778 | ||
1779 | err = rtm_to_route_config(skb, nlh, cfg); | |
03c05665 | 1780 | if (err < 0) |
a4ac8c98 | 1781 | goto out; |
03c05665 | 1782 | |
a4ac8c98 DA |
1783 | err = mpls_route_add(cfg); |
1784 | out: | |
1785 | kfree(cfg); | |
1786 | ||
1787 | return err; | |
03c05665 EB |
1788 | } |
1789 | ||
1790 | static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event, | |
1791 | u32 label, struct mpls_route *rt, int flags) | |
1792 | { | |
19d0c341 | 1793 | struct net_device *dev; |
03c05665 EB |
1794 | struct nlmsghdr *nlh; |
1795 | struct rtmsg *rtm; | |
1796 | ||
1797 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags); | |
1798 | if (nlh == NULL) | |
1799 | return -EMSGSIZE; | |
1800 | ||
1801 | rtm = nlmsg_data(nlh); | |
1802 | rtm->rtm_family = AF_MPLS; | |
1803 | rtm->rtm_dst_len = 20; | |
1804 | rtm->rtm_src_len = 0; | |
1805 | rtm->rtm_tos = 0; | |
1806 | rtm->rtm_table = RT_TABLE_MAIN; | |
1807 | rtm->rtm_protocol = rt->rt_protocol; | |
1808 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; | |
1809 | rtm->rtm_type = RTN_UNICAST; | |
1810 | rtm->rtm_flags = 0; | |
1811 | ||
03c05665 EB |
1812 | if (nla_put_labels(skb, RTA_DST, 1, &label)) |
1813 | goto nla_put_failure; | |
5b441ac8 RS |
1814 | |
1815 | if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) { | |
1816 | bool ttl_propagate = | |
1817 | rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED; | |
1818 | ||
1819 | if (nla_put_u8(skb, RTA_TTL_PROPAGATE, | |
1820 | ttl_propagate)) | |
1821 | goto nla_put_failure; | |
1822 | } | |
f8efb73c | 1823 | if (rt->rt_nhn == 1) { |
cf4b24f0 | 1824 | const struct mpls_nh *nh = rt->rt_nh; |
f8efb73c RP |
1825 | |
1826 | if (nh->nh_labels && | |
1827 | nla_put_labels(skb, RTA_NEWDST, nh->nh_labels, | |
1828 | nh->nh_label)) | |
1829 | goto nla_put_failure; | |
eb7809f0 | 1830 | if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC && |
72dcac96 | 1831 | nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh), |
f8efb73c RP |
1832 | nh->nh_via_alen)) |
1833 | goto nla_put_failure; | |
1834 | dev = rtnl_dereference(nh->nh_dev); | |
1835 | if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex)) | |
1836 | goto nla_put_failure; | |
c89359a4 RP |
1837 | if (nh->nh_flags & RTNH_F_LINKDOWN) |
1838 | rtm->rtm_flags |= RTNH_F_LINKDOWN; | |
1839 | if (nh->nh_flags & RTNH_F_DEAD) | |
1840 | rtm->rtm_flags |= RTNH_F_DEAD; | |
f8efb73c RP |
1841 | } else { |
1842 | struct rtnexthop *rtnh; | |
1843 | struct nlattr *mp; | |
77ef013a DA |
1844 | u8 linkdown = 0; |
1845 | u8 dead = 0; | |
f8efb73c RP |
1846 | |
1847 | mp = nla_nest_start(skb, RTA_MULTIPATH); | |
1848 | if (!mp) | |
1849 | goto nla_put_failure; | |
1850 | ||
1851 | for_nexthops(rt) { | |
c00e51dd DA |
1852 | dev = rtnl_dereference(nh->nh_dev); |
1853 | if (!dev) | |
1854 | continue; | |
1855 | ||
f8efb73c RP |
1856 | rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); |
1857 | if (!rtnh) | |
1858 | goto nla_put_failure; | |
1859 | ||
c00e51dd | 1860 | rtnh->rtnh_ifindex = dev->ifindex; |
c89359a4 RP |
1861 | if (nh->nh_flags & RTNH_F_LINKDOWN) { |
1862 | rtnh->rtnh_flags |= RTNH_F_LINKDOWN; | |
1863 | linkdown++; | |
1864 | } | |
1865 | if (nh->nh_flags & RTNH_F_DEAD) { | |
1866 | rtnh->rtnh_flags |= RTNH_F_DEAD; | |
1867 | dead++; | |
1868 | } | |
1869 | ||
f8efb73c RP |
1870 | if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST, |
1871 | nh->nh_labels, | |
1872 | nh->nh_label)) | |
1873 | goto nla_put_failure; | |
f20367df RS |
1874 | if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC && |
1875 | nla_put_via(skb, nh->nh_via_table, | |
cf4b24f0 | 1876 | mpls_nh_via(rt, nh), |
f8efb73c RP |
1877 | nh->nh_via_alen)) |
1878 | goto nla_put_failure; | |
1879 | ||
1880 | /* length of rtnetlink header + attributes */ | |
1881 | rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; | |
1882 | } endfor_nexthops(rt); | |
1883 | ||
c89359a4 RP |
1884 | if (linkdown == rt->rt_nhn) |
1885 | rtm->rtm_flags |= RTNH_F_LINKDOWN; | |
1886 | if (dead == rt->rt_nhn) | |
1887 | rtm->rtm_flags |= RTNH_F_DEAD; | |
1888 | ||
f8efb73c RP |
1889 | nla_nest_end(skb, mp); |
1890 | } | |
03c05665 EB |
1891 | |
1892 | nlmsg_end(skb, nlh); | |
1893 | return 0; | |
1894 | ||
1895 | nla_put_failure: | |
1896 | nlmsg_cancel(skb, nlh); | |
1897 | return -EMSGSIZE; | |
1898 | } | |
1899 | ||
1900 | static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb) | |
1901 | { | |
1902 | struct net *net = sock_net(skb->sk); | |
19d0c341 EB |
1903 | struct mpls_route __rcu **platform_label; |
1904 | size_t platform_labels; | |
03c05665 EB |
1905 | unsigned int index; |
1906 | ||
1907 | ASSERT_RTNL(); | |
1908 | ||
1909 | index = cb->args[0]; | |
a6affd24 RS |
1910 | if (index < MPLS_LABEL_FIRST_UNRESERVED) |
1911 | index = MPLS_LABEL_FIRST_UNRESERVED; | |
03c05665 | 1912 | |
19d0c341 EB |
1913 | platform_label = rtnl_dereference(net->mpls.platform_label); |
1914 | platform_labels = net->mpls.platform_labels; | |
1915 | for (; index < platform_labels; index++) { | |
03c05665 | 1916 | struct mpls_route *rt; |
19d0c341 | 1917 | rt = rtnl_dereference(platform_label[index]); |
03c05665 EB |
1918 | if (!rt) |
1919 | continue; | |
1920 | ||
1921 | if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid, | |
1922 | cb->nlh->nlmsg_seq, RTM_NEWROUTE, | |
1923 | index, rt, NLM_F_MULTI) < 0) | |
1924 | break; | |
1925 | } | |
1926 | cb->args[0] = index; | |
1927 | ||
1928 | return skb->len; | |
1929 | } | |
1930 | ||
8de147dc EB |
1931 | static inline size_t lfib_nlmsg_size(struct mpls_route *rt) |
1932 | { | |
1933 | size_t payload = | |
1934 | NLMSG_ALIGN(sizeof(struct rtmsg)) | |
5b441ac8 RS |
1935 | + nla_total_size(4) /* RTA_DST */ |
1936 | + nla_total_size(1); /* RTA_TTL_PROPAGATE */ | |
f8efb73c RP |
1937 | |
1938 | if (rt->rt_nhn == 1) { | |
1939 | struct mpls_nh *nh = rt->rt_nh; | |
1940 | ||
1941 | if (nh->nh_dev) | |
1942 | payload += nla_total_size(4); /* RTA_OIF */ | |
eb7809f0 | 1943 | if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */ |
72dcac96 | 1944 | payload += nla_total_size(2 + nh->nh_via_alen); |
f8efb73c RP |
1945 | if (nh->nh_labels) /* RTA_NEWDST */ |
1946 | payload += nla_total_size(nh->nh_labels * 4); | |
1947 | } else { | |
1948 | /* each nexthop is packed in an attribute */ | |
1949 | size_t nhsize = 0; | |
1950 | ||
1951 | for_nexthops(rt) { | |
e944e97a DA |
1952 | if (!rtnl_dereference(nh->nh_dev)) |
1953 | continue; | |
f8efb73c | 1954 | nhsize += nla_total_size(sizeof(struct rtnexthop)); |
f20367df RS |
1955 | /* RTA_VIA */ |
1956 | if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) | |
1957 | nhsize += nla_total_size(2 + nh->nh_via_alen); | |
f8efb73c RP |
1958 | if (nh->nh_labels) |
1959 | nhsize += nla_total_size(nh->nh_labels * 4); | |
1960 | } endfor_nexthops(rt); | |
1961 | /* nested attribute */ | |
1962 | payload += nla_total_size(nhsize); | |
1963 | } | |
1964 | ||
8de147dc EB |
1965 | return payload; |
1966 | } | |
1967 | ||
1968 | static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt, | |
1969 | struct nlmsghdr *nlh, struct net *net, u32 portid, | |
1970 | unsigned int nlm_flags) | |
1971 | { | |
1972 | struct sk_buff *skb; | |
1973 | u32 seq = nlh ? nlh->nlmsg_seq : 0; | |
1974 | int err = -ENOBUFS; | |
1975 | ||
1976 | skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL); | |
1977 | if (skb == NULL) | |
1978 | goto errout; | |
1979 | ||
1980 | err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags); | |
1981 | if (err < 0) { | |
1982 | /* -EMSGSIZE implies BUG in lfib_nlmsg_size */ | |
1983 | WARN_ON(err == -EMSGSIZE); | |
1984 | kfree_skb(skb); | |
1985 | goto errout; | |
1986 | } | |
1987 | rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL); | |
1988 | ||
1989 | return; | |
1990 | errout: | |
1991 | if (err < 0) | |
1992 | rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err); | |
1993 | } | |
1994 | ||
7720c01f EB |
1995 | static int resize_platform_label_table(struct net *net, size_t limit) |
1996 | { | |
1997 | size_t size = sizeof(struct mpls_route *) * limit; | |
1998 | size_t old_limit; | |
1999 | size_t cp_size; | |
2000 | struct mpls_route __rcu **labels = NULL, **old; | |
2001 | struct mpls_route *rt0 = NULL, *rt2 = NULL; | |
2002 | unsigned index; | |
2003 | ||
2004 | if (size) { | |
2005 | labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); | |
2006 | if (!labels) | |
2007 | labels = vzalloc(size); | |
2008 | ||
2009 | if (!labels) | |
2010 | goto nolabels; | |
2011 | } | |
2012 | ||
2013 | /* In case the predefined labels need to be populated */ | |
78f5b899 | 2014 | if (limit > MPLS_LABEL_IPV4NULL) { |
7720c01f | 2015 | struct net_device *lo = net->loopback_dev; |
a4ac8c98 | 2016 | rt0 = mpls_rt_alloc(1, lo->addr_len, 0); |
df1c6316 | 2017 | if (IS_ERR(rt0)) |
7720c01f | 2018 | goto nort0; |
f8efb73c | 2019 | RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo); |
7720c01f | 2020 | rt0->rt_protocol = RTPROT_KERNEL; |
118d5234 | 2021 | rt0->rt_payload_type = MPT_IPV4; |
5b441ac8 | 2022 | rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT; |
f8efb73c | 2023 | rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE; |
b4e04fc7 | 2024 | rt0->rt_nh->nh_via_alen = lo->addr_len; |
cf4b24f0 RS |
2025 | memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr, |
2026 | lo->addr_len); | |
7720c01f | 2027 | } |
78f5b899 | 2028 | if (limit > MPLS_LABEL_IPV6NULL) { |
7720c01f | 2029 | struct net_device *lo = net->loopback_dev; |
a4ac8c98 | 2030 | rt2 = mpls_rt_alloc(1, lo->addr_len, 0); |
df1c6316 | 2031 | if (IS_ERR(rt2)) |
7720c01f | 2032 | goto nort2; |
f8efb73c | 2033 | RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo); |
7720c01f | 2034 | rt2->rt_protocol = RTPROT_KERNEL; |
118d5234 | 2035 | rt2->rt_payload_type = MPT_IPV6; |
6a18c312 | 2036 | rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT; |
f8efb73c | 2037 | rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE; |
b4e04fc7 | 2038 | rt2->rt_nh->nh_via_alen = lo->addr_len; |
cf4b24f0 RS |
2039 | memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr, |
2040 | lo->addr_len); | |
7720c01f EB |
2041 | } |
2042 | ||
2043 | rtnl_lock(); | |
2044 | /* Remember the original table */ | |
19d0c341 | 2045 | old = rtnl_dereference(net->mpls.platform_label); |
7720c01f EB |
2046 | old_limit = net->mpls.platform_labels; |
2047 | ||
2048 | /* Free any labels beyond the new table */ | |
2049 | for (index = limit; index < old_limit; index++) | |
f8efb73c | 2050 | mpls_route_update(net, index, NULL, NULL); |
7720c01f EB |
2051 | |
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 */ | |
78f5b899 TH |
2060 | if ((old_limit <= MPLS_LABEL_IPV6NULL) && |
2061 | (limit > MPLS_LABEL_IPV6NULL)) { | |
2062 | RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2); | |
7720c01f EB |
2063 | rt2 = NULL; |
2064 | } | |
2065 | ||
78f5b899 TH |
2066 | if ((old_limit <= MPLS_LABEL_IPV4NULL) && |
2067 | (limit > MPLS_LABEL_IPV4NULL)) { | |
2068 | RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0); | |
7720c01f EB |
2069 | rt0 = NULL; |
2070 | } | |
2071 | ||
2072 | /* Update the global pointers */ | |
2073 | net->mpls.platform_labels = limit; | |
19d0c341 | 2074 | rcu_assign_pointer(net->mpls.platform_label, labels); |
7720c01f EB |
2075 | |
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 | ||
2087 | nort2: | |
2088 | mpls_rt_free(rt0); | |
2089 | nort0: | |
2090 | kvfree(labels); | |
2091 | nolabels: | |
2092 | return -ENOMEM; | |
2093 | } | |
2094 | ||
2095 | static 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 | ||
5b441ac8 RS |
2118 | #define MPLS_NS_SYSCTL_OFFSET(field) \ |
2119 | (&((struct net *)0)->field) | |
2120 | ||
37bde799 | 2121 | static const struct ctl_table mpls_table[] = { |
7720c01f EB |
2122 | { |
2123 | .procname = "platform_labels", | |
2124 | .data = NULL, | |
2125 | .maxlen = sizeof(int), | |
2126 | .mode = 0644, | |
2127 | .proc_handler = mpls_platform_labels, | |
2128 | }, | |
5b441ac8 RS |
2129 | { |
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 | }, | |
a59166e4 RS |
2138 | { |
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 | }, | |
7720c01f EB |
2147 | { } |
2148 | }; | |
2149 | ||
0189197f EB |
2150 | static int mpls_net_init(struct net *net) |
2151 | { | |
7720c01f | 2152 | struct ctl_table *table; |
5b441ac8 | 2153 | int i; |
7720c01f | 2154 | |
0189197f EB |
2155 | net->mpls.platform_labels = 0; |
2156 | net->mpls.platform_label = NULL; | |
5b441ac8 | 2157 | net->mpls.ip_ttl_propagate = 1; |
a59166e4 | 2158 | net->mpls.default_ttl = 255; |
0189197f | 2159 | |
7720c01f EB |
2160 | table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL); |
2161 | if (table == NULL) | |
2162 | return -ENOMEM; | |
2163 | ||
5b441ac8 RS |
2164 | /* 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 | ||
7720c01f | 2170 | net->mpls.ctl = register_net_sysctl(net, "net/mpls", table); |
6ea3c9d5 NA |
2171 | if (net->mpls.ctl == NULL) { |
2172 | kfree(table); | |
7720c01f | 2173 | return -ENOMEM; |
6ea3c9d5 | 2174 | } |
7720c01f | 2175 | |
0189197f EB |
2176 | return 0; |
2177 | } | |
2178 | ||
2179 | static void mpls_net_exit(struct net *net) | |
2180 | { | |
19d0c341 EB |
2181 | struct mpls_route __rcu **platform_label; |
2182 | size_t platform_labels; | |
7720c01f | 2183 | struct ctl_table *table; |
0189197f EB |
2184 | unsigned int index; |
2185 | ||
7720c01f EB |
2186 | table = net->mpls.ctl->ctl_table_arg; |
2187 | unregister_net_sysctl_table(net->mpls.ctl); | |
2188 | kfree(table); | |
2189 | ||
19d0c341 EB |
2190 | /* An rcu grace period has passed since there was a device in |
2191 | * the network namespace (and thus the last in flight packet) | |
0189197f EB |
2192 | * 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(); | |
19d0c341 EB |
2200 | 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); | |
e37791ec | 2205 | mpls_notify_route(net, index, rt, NULL, NULL); |
0189197f EB |
2206 | mpls_rt_free(rt); |
2207 | } | |
2208 | rtnl_unlock(); | |
2209 | ||
19d0c341 | 2210 | kvfree(platform_label); |
0189197f EB |
2211 | } |
2212 | ||
2213 | static struct pernet_operations mpls_net_ops = { | |
2214 | .init = mpls_net_init, | |
2215 | .exit = mpls_net_exit, | |
2216 | }; | |
2217 | ||
27d69105 RS |
2218 | static 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 | ||
0189197f EB |
2224 | static 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 | ||
27d69105 RS |
2240 | rtnl_af_register(&mpls_af_ops); |
2241 | ||
03c05665 EB |
2242 | 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); | |
24045a03 DA |
2245 | rtnl_register(PF_MPLS, RTM_GETNETCONF, mpls_netconf_get_devconf, |
2246 | mpls_netconf_dump_devconf, NULL); | |
0189197f EB |
2247 | err = 0; |
2248 | out: | |
2249 | return err; | |
2250 | ||
2251 | out_unregister_pernet: | |
2252 | unregister_pernet_subsys(&mpls_net_ops); | |
2253 | goto out; | |
2254 | } | |
2255 | module_init(mpls_init); | |
2256 | ||
2257 | static void __exit mpls_exit(void) | |
2258 | { | |
03c05665 | 2259 | rtnl_unregister_all(PF_MPLS); |
27d69105 | 2260 | rtnl_af_unregister(&mpls_af_ops); |
0189197f EB |
2261 | dev_remove_pack(&mpls_packet_type); |
2262 | unregister_netdevice_notifier(&mpls_dev_notifier); | |
2263 | unregister_pernet_subsys(&mpls_net_ops); | |
2264 | } | |
2265 | module_exit(mpls_exit); | |
2266 | ||
2267 | MODULE_DESCRIPTION("MultiProtocol Label Switching"); | |
2268 | MODULE_LICENSE("GPL v2"); | |
2269 | MODULE_ALIAS_NETPROTO(PF_MPLS); |