1 // SPDX-License-Identifier: GPL-2.0
2 /* Generic nexthop implementation
3 *
4 * Copyright (c) 2017-19 Cumulus Networks
5 * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
6 */
7
8 #include <linux/nexthop.h>
9 #include <linux/rtnetlink.h>
10 #include <linux/slab.h>
11 #include <linux/vmalloc.h>
12 #include <net/arp.h>
13 #include <net/ipv6_stubs.h>
14 #include <net/lwtunnel.h>
15 #include <net/ndisc.h>
16 #include <net/nexthop.h>
17 #include <net/route.h>
18 #include <net/sock.h>
19
20 #define NH_RES_DEFAULT_IDLE_TIMER (120 * HZ)
21 #define NH_RES_DEFAULT_UNBALANCED_TIMER 0 /* No forced rebalancing. */
22
23 static void remove_nexthop(struct net *net, struct nexthop *nh,
24 struct nl_info *nlinfo);
25
26 #define NH_DEV_HASHBITS 8
27 #define NH_DEV_HASHSIZE (1U << NH_DEV_HASHBITS)
28
29 #define NHA_OP_FLAGS_DUMP_ALL (NHA_OP_FLAG_DUMP_STATS | \
30 NHA_OP_FLAG_DUMP_HW_STATS)
31
32 static const struct nla_policy rtm_nh_policy_new[] = {
33 [NHA_ID] = { .type = NLA_U32 },
34 [NHA_GROUP] = { .type = NLA_BINARY },
35 [NHA_GROUP_TYPE] = { .type = NLA_U16 },
36 [NHA_BLACKHOLE] = { .type = NLA_FLAG },
37 [NHA_OIF] = { .type = NLA_U32 },
38 [NHA_GATEWAY] = { .type = NLA_BINARY },
39 [NHA_ENCAP_TYPE] = { .type = NLA_U16 },
40 [NHA_ENCAP] = { .type = NLA_NESTED },
41 [NHA_FDB] = { .type = NLA_FLAG },
42 [NHA_RES_GROUP] = { .type = NLA_NESTED },
43 [NHA_HW_STATS_ENABLE] = NLA_POLICY_MAX(NLA_U32, true),
44 };
45
46 static const struct nla_policy rtm_nh_policy_get[] = {
47 [NHA_ID] = { .type = NLA_U32 },
48 [NHA_OP_FLAGS] = NLA_POLICY_MASK(NLA_U32,
49 NHA_OP_FLAGS_DUMP_ALL),
50 };
51
52 static const struct nla_policy rtm_nh_policy_del[] = {
53 [NHA_ID] = { .type = NLA_U32 },
54 };
55
56 static const struct nla_policy rtm_nh_policy_dump[] = {
57 [NHA_OIF] = { .type = NLA_U32 },
58 [NHA_GROUPS] = { .type = NLA_FLAG },
59 [NHA_MASTER] = { .type = NLA_U32 },
60 [NHA_FDB] = { .type = NLA_FLAG },
61 [NHA_OP_FLAGS] = NLA_POLICY_MASK(NLA_U32,
62 NHA_OP_FLAGS_DUMP_ALL),
63 };
64
65 static const struct nla_policy rtm_nh_res_policy_new[] = {
66 [NHA_RES_GROUP_BUCKETS] = { .type = NLA_U16 },
67 [NHA_RES_GROUP_IDLE_TIMER] = { .type = NLA_U32 },
68 [NHA_RES_GROUP_UNBALANCED_TIMER] = { .type = NLA_U32 },
69 };
70
71 static const struct nla_policy rtm_nh_policy_dump_bucket[] = {
72 [NHA_ID] = { .type = NLA_U32 },
73 [NHA_OIF] = { .type = NLA_U32 },
74 [NHA_MASTER] = { .type = NLA_U32 },
75 [NHA_RES_BUCKET] = { .type = NLA_NESTED },
76 };
77
78 static const struct nla_policy rtm_nh_res_bucket_policy_dump[] = {
79 [NHA_RES_BUCKET_NH_ID] = { .type = NLA_U32 },
80 };
81
82 static const struct nla_policy rtm_nh_policy_get_bucket[] = {
83 [NHA_ID] = { .type = NLA_U32 },
84 [NHA_RES_BUCKET] = { .type = NLA_NESTED },
85 };
86
87 static const struct nla_policy rtm_nh_res_bucket_policy_get[] = {
88 [NHA_RES_BUCKET_INDEX] = { .type = NLA_U16 },
89 };
90
nexthop_notifiers_is_empty(struct net * net)91 static bool nexthop_notifiers_is_empty(struct net *net)
92 {
93 return !net->nexthop.notifier_chain.head;
94 }
95
96 static void
__nh_notifier_single_info_init(struct nh_notifier_single_info * nh_info,const struct nh_info * nhi)97 __nh_notifier_single_info_init(struct nh_notifier_single_info *nh_info,
98 const struct nh_info *nhi)
99 {
100 nh_info->dev = nhi->fib_nhc.nhc_dev;
101 nh_info->gw_family = nhi->fib_nhc.nhc_gw_family;
102 if (nh_info->gw_family == AF_INET)
103 nh_info->ipv4 = nhi->fib_nhc.nhc_gw.ipv4;
104 else if (nh_info->gw_family == AF_INET6)
105 nh_info->ipv6 = nhi->fib_nhc.nhc_gw.ipv6;
106
107 nh_info->id = nhi->nh_parent->id;
108 nh_info->is_reject = nhi->reject_nh;
109 nh_info->is_fdb = nhi->fdb_nh;
110 nh_info->has_encap = !!nhi->fib_nhc.nhc_lwtstate;
111 }
112
nh_notifier_single_info_init(struct nh_notifier_info * info,const struct nexthop * nh)113 static int nh_notifier_single_info_init(struct nh_notifier_info *info,
114 const struct nexthop *nh)
115 {
116 struct nh_info *nhi = rtnl_dereference(nh->nh_info);
117
118 info->type = NH_NOTIFIER_INFO_TYPE_SINGLE;
119 info->nh = kzalloc(sizeof(*info->nh), GFP_KERNEL);
120 if (!info->nh)
121 return -ENOMEM;
122
123 __nh_notifier_single_info_init(info->nh, nhi);
124
125 return 0;
126 }
127
nh_notifier_single_info_fini(struct nh_notifier_info * info)128 static void nh_notifier_single_info_fini(struct nh_notifier_info *info)
129 {
130 kfree(info->nh);
131 }
132
nh_notifier_mpath_info_init(struct nh_notifier_info * info,struct nh_group * nhg)133 static int nh_notifier_mpath_info_init(struct nh_notifier_info *info,
134 struct nh_group *nhg)
135 {
136 u16 num_nh = nhg->num_nh;
137 int i;
138
139 info->type = NH_NOTIFIER_INFO_TYPE_GRP;
140 info->nh_grp = kzalloc(struct_size(info->nh_grp, nh_entries, num_nh),
141 GFP_KERNEL);
142 if (!info->nh_grp)
143 return -ENOMEM;
144
145 info->nh_grp->num_nh = num_nh;
146 info->nh_grp->is_fdb = nhg->fdb_nh;
147 info->nh_grp->hw_stats = nhg->hw_stats;
148
149 for (i = 0; i < num_nh; i++) {
150 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
151 struct nh_info *nhi;
152
153 nhi = rtnl_dereference(nhge->nh->nh_info);
154 info->nh_grp->nh_entries[i].weight = nhge->weight;
155 __nh_notifier_single_info_init(&info->nh_grp->nh_entries[i].nh,
156 nhi);
157 }
158
159 return 0;
160 }
161
nh_notifier_res_table_info_init(struct nh_notifier_info * info,struct nh_group * nhg)162 static int nh_notifier_res_table_info_init(struct nh_notifier_info *info,
163 struct nh_group *nhg)
164 {
165 struct nh_res_table *res_table = rtnl_dereference(nhg->res_table);
166 u16 num_nh_buckets = res_table->num_nh_buckets;
167 unsigned long size;
168 u16 i;
169
170 info->type = NH_NOTIFIER_INFO_TYPE_RES_TABLE;
171 size = struct_size(info->nh_res_table, nhs, num_nh_buckets);
172 info->nh_res_table = __vmalloc(size, GFP_KERNEL | __GFP_ZERO |
173 __GFP_NOWARN);
174 if (!info->nh_res_table)
175 return -ENOMEM;
176
177 info->nh_res_table->num_nh_buckets = num_nh_buckets;
178 info->nh_res_table->hw_stats = nhg->hw_stats;
179
180 for (i = 0; i < num_nh_buckets; i++) {
181 struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
182 struct nh_grp_entry *nhge;
183 struct nh_info *nhi;
184
185 nhge = rtnl_dereference(bucket->nh_entry);
186 nhi = rtnl_dereference(nhge->nh->nh_info);
187 __nh_notifier_single_info_init(&info->nh_res_table->nhs[i],
188 nhi);
189 }
190
191 return 0;
192 }
193
nh_notifier_grp_info_init(struct nh_notifier_info * info,const struct nexthop * nh)194 static int nh_notifier_grp_info_init(struct nh_notifier_info *info,
195 const struct nexthop *nh)
196 {
197 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
198
199 if (nhg->hash_threshold)
200 return nh_notifier_mpath_info_init(info, nhg);
201 else if (nhg->resilient)
202 return nh_notifier_res_table_info_init(info, nhg);
203 return -EINVAL;
204 }
205
nh_notifier_grp_info_fini(struct nh_notifier_info * info,const struct nexthop * nh)206 static void nh_notifier_grp_info_fini(struct nh_notifier_info *info,
207 const struct nexthop *nh)
208 {
209 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
210
211 if (nhg->hash_threshold)
212 kfree(info->nh_grp);
213 else if (nhg->resilient)
214 vfree(info->nh_res_table);
215 }
216
nh_notifier_info_init(struct nh_notifier_info * info,const struct nexthop * nh)217 static int nh_notifier_info_init(struct nh_notifier_info *info,
218 const struct nexthop *nh)
219 {
220 info->id = nh->id;
221
222 if (nh->is_group)
223 return nh_notifier_grp_info_init(info, nh);
224 else
225 return nh_notifier_single_info_init(info, nh);
226 }
227
nh_notifier_info_fini(struct nh_notifier_info * info,const struct nexthop * nh)228 static void nh_notifier_info_fini(struct nh_notifier_info *info,
229 const struct nexthop *nh)
230 {
231 if (nh->is_group)
232 nh_notifier_grp_info_fini(info, nh);
233 else
234 nh_notifier_single_info_fini(info);
235 }
236
call_nexthop_notifiers(struct net * net,enum nexthop_event_type event_type,struct nexthop * nh,struct netlink_ext_ack * extack)237 static int call_nexthop_notifiers(struct net *net,
238 enum nexthop_event_type event_type,
239 struct nexthop *nh,
240 struct netlink_ext_ack *extack)
241 {
242 struct nh_notifier_info info = {
243 .net = net,
244 .extack = extack,
245 };
246 int err;
247
248 ASSERT_RTNL();
249
250 if (nexthop_notifiers_is_empty(net))
251 return 0;
252
253 err = nh_notifier_info_init(&info, nh);
254 if (err) {
255 NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info");
256 return err;
257 }
258
259 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
260 event_type, &info);
261 nh_notifier_info_fini(&info, nh);
262
263 return notifier_to_errno(err);
264 }
265
266 static int
nh_notifier_res_bucket_idle_timer_get(const struct nh_notifier_info * info,bool force,unsigned int * p_idle_timer_ms)267 nh_notifier_res_bucket_idle_timer_get(const struct nh_notifier_info *info,
268 bool force, unsigned int *p_idle_timer_ms)
269 {
270 struct nh_res_table *res_table;
271 struct nh_group *nhg;
272 struct nexthop *nh;
273 int err = 0;
274
275 /* When 'force' is false, nexthop bucket replacement is performed
276 * because the bucket was deemed to be idle. In this case, capable
277 * listeners can choose to perform an atomic replacement: The bucket is
278 * only replaced if it is inactive. However, if the idle timer interval
279 * is smaller than the interval in which a listener is querying
280 * buckets' activity from the device, then atomic replacement should
281 * not be tried. Pass the idle timer value to listeners, so that they
282 * could determine which type of replacement to perform.
283 */
284 if (force) {
285 *p_idle_timer_ms = 0;
286 return 0;
287 }
288
289 rcu_read_lock();
290
291 nh = nexthop_find_by_id(info->net, info->id);
292 if (!nh) {
293 err = -EINVAL;
294 goto out;
295 }
296
297 nhg = rcu_dereference(nh->nh_grp);
298 res_table = rcu_dereference(nhg->res_table);
299 *p_idle_timer_ms = jiffies_to_msecs(res_table->idle_timer);
300
301 out:
302 rcu_read_unlock();
303
304 return err;
305 }
306
nh_notifier_res_bucket_info_init(struct nh_notifier_info * info,u16 bucket_index,bool force,struct nh_info * oldi,struct nh_info * newi)307 static int nh_notifier_res_bucket_info_init(struct nh_notifier_info *info,
308 u16 bucket_index, bool force,
309 struct nh_info *oldi,
310 struct nh_info *newi)
311 {
312 unsigned int idle_timer_ms;
313 int err;
314
315 err = nh_notifier_res_bucket_idle_timer_get(info, force,
316 &idle_timer_ms);
317 if (err)
318 return err;
319
320 info->type = NH_NOTIFIER_INFO_TYPE_RES_BUCKET;
321 info->nh_res_bucket = kzalloc(sizeof(*info->nh_res_bucket),
322 GFP_KERNEL);
323 if (!info->nh_res_bucket)
324 return -ENOMEM;
325
326 info->nh_res_bucket->bucket_index = bucket_index;
327 info->nh_res_bucket->idle_timer_ms = idle_timer_ms;
328 info->nh_res_bucket->force = force;
329 __nh_notifier_single_info_init(&info->nh_res_bucket->old_nh, oldi);
330 __nh_notifier_single_info_init(&info->nh_res_bucket->new_nh, newi);
331 return 0;
332 }
333
nh_notifier_res_bucket_info_fini(struct nh_notifier_info * info)334 static void nh_notifier_res_bucket_info_fini(struct nh_notifier_info *info)
335 {
336 kfree(info->nh_res_bucket);
337 }
338
__call_nexthop_res_bucket_notifiers(struct net * net,u32 nhg_id,u16 bucket_index,bool force,struct nh_info * oldi,struct nh_info * newi,struct netlink_ext_ack * extack)339 static int __call_nexthop_res_bucket_notifiers(struct net *net, u32 nhg_id,
340 u16 bucket_index, bool force,
341 struct nh_info *oldi,
342 struct nh_info *newi,
343 struct netlink_ext_ack *extack)
344 {
345 struct nh_notifier_info info = {
346 .net = net,
347 .extack = extack,
348 .id = nhg_id,
349 };
350 int err;
351
352 if (nexthop_notifiers_is_empty(net))
353 return 0;
354
355 err = nh_notifier_res_bucket_info_init(&info, bucket_index, force,
356 oldi, newi);
357 if (err)
358 return err;
359
360 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
361 NEXTHOP_EVENT_BUCKET_REPLACE, &info);
362 nh_notifier_res_bucket_info_fini(&info);
363
364 return notifier_to_errno(err);
365 }
366
367 /* There are three users of RES_TABLE, and NHs etc. referenced from there:
368 *
369 * 1) a collection of callbacks for NH maintenance. This operates under
370 * RTNL,
371 * 2) the delayed work that gradually balances the resilient table,
372 * 3) and nexthop_select_path(), operating under RCU.
373 *
374 * Both the delayed work and the RTNL block are writers, and need to
375 * maintain mutual exclusion. Since there are only two and well-known
376 * writers for each table, the RTNL code can make sure it has exclusive
377 * access thus:
378 *
379 * - Have the DW operate without locking;
380 * - synchronously cancel the DW;
381 * - do the writing;
382 * - if the write was not actually a delete, call upkeep, which schedules
383 * DW again if necessary.
384 *
385 * The functions that are always called from the RTNL context use
386 * rtnl_dereference(). The functions that can also be called from the DW do
387 * a raw dereference and rely on the above mutual exclusion scheme.
388 */
389 #define nh_res_dereference(p) (rcu_dereference_raw(p))
390
call_nexthop_res_bucket_notifiers(struct net * net,u32 nhg_id,u16 bucket_index,bool force,struct nexthop * old_nh,struct nexthop * new_nh,struct netlink_ext_ack * extack)391 static int call_nexthop_res_bucket_notifiers(struct net *net, u32 nhg_id,
392 u16 bucket_index, bool force,
393 struct nexthop *old_nh,
394 struct nexthop *new_nh,
395 struct netlink_ext_ack *extack)
396 {
397 struct nh_info *oldi = nh_res_dereference(old_nh->nh_info);
398 struct nh_info *newi = nh_res_dereference(new_nh->nh_info);
399
400 return __call_nexthop_res_bucket_notifiers(net, nhg_id, bucket_index,
401 force, oldi, newi, extack);
402 }
403
call_nexthop_res_table_notifiers(struct net * net,struct nexthop * nh,struct netlink_ext_ack * extack)404 static int call_nexthop_res_table_notifiers(struct net *net, struct nexthop *nh,
405 struct netlink_ext_ack *extack)
406 {
407 struct nh_notifier_info info = {
408 .net = net,
409 .extack = extack,
410 .id = nh->id,
411 };
412 struct nh_group *nhg;
413 int err;
414
415 ASSERT_RTNL();
416
417 if (nexthop_notifiers_is_empty(net))
418 return 0;
419
420 /* At this point, the nexthop buckets are still not populated. Only
421 * emit a notification with the logical nexthops, so that a listener
422 * could potentially veto it in case of unsupported configuration.
423 */
424 nhg = rtnl_dereference(nh->nh_grp);
425 err = nh_notifier_mpath_info_init(&info, nhg);
426 if (err) {
427 NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info");
428 return err;
429 }
430
431 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
432 NEXTHOP_EVENT_RES_TABLE_PRE_REPLACE,
433 &info);
434 kfree(info.nh_grp);
435
436 return notifier_to_errno(err);
437 }
438
call_nexthop_notifier(struct notifier_block * nb,struct net * net,enum nexthop_event_type event_type,struct nexthop * nh,struct netlink_ext_ack * extack)439 static int call_nexthop_notifier(struct notifier_block *nb, struct net *net,
440 enum nexthop_event_type event_type,
441 struct nexthop *nh,
442 struct netlink_ext_ack *extack)
443 {
444 struct nh_notifier_info info = {
445 .net = net,
446 .extack = extack,
447 };
448 int err;
449
450 err = nh_notifier_info_init(&info, nh);
451 if (err)
452 return err;
453
454 err = nb->notifier_call(nb, event_type, &info);
455 nh_notifier_info_fini(&info, nh);
456
457 return notifier_to_errno(err);
458 }
459
nh_dev_hashfn(unsigned int val)460 static unsigned int nh_dev_hashfn(unsigned int val)
461 {
462 unsigned int mask = NH_DEV_HASHSIZE - 1;
463
464 return (val ^
465 (val >> NH_DEV_HASHBITS) ^
466 (val >> (NH_DEV_HASHBITS * 2))) & mask;
467 }
468
nexthop_devhash_add(struct net * net,struct nh_info * nhi)469 static void nexthop_devhash_add(struct net *net, struct nh_info *nhi)
470 {
471 struct net_device *dev = nhi->fib_nhc.nhc_dev;
472 struct hlist_head *head;
473 unsigned int hash;
474
475 WARN_ON(!dev);
476
477 hash = nh_dev_hashfn(dev->ifindex);
478 head = &net->nexthop.devhash[hash];
479 hlist_add_head(&nhi->dev_hash, head);
480 }
481
nexthop_free_group(struct nexthop * nh)482 static void nexthop_free_group(struct nexthop *nh)
483 {
484 struct nh_group *nhg;
485 int i;
486
487 nhg = rcu_dereference_raw(nh->nh_grp);
488 for (i = 0; i < nhg->num_nh; ++i) {
489 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
490
491 WARN_ON(!list_empty(&nhge->nh_list));
492 free_percpu(nhge->stats);
493 nexthop_put(nhge->nh);
494 }
495
496 WARN_ON(nhg->spare == nhg);
497
498 if (nhg->resilient)
499 vfree(rcu_dereference_raw(nhg->res_table));
500
501 kfree(nhg->spare);
502 kfree(nhg);
503 }
504
nexthop_free_single(struct nexthop * nh)505 static void nexthop_free_single(struct nexthop *nh)
506 {
507 struct nh_info *nhi;
508
509 nhi = rcu_dereference_raw(nh->nh_info);
510 switch (nhi->family) {
511 case AF_INET:
512 fib_nh_release(nh->net, &nhi->fib_nh);
513 break;
514 case AF_INET6:
515 ipv6_stub->fib6_nh_release(&nhi->fib6_nh);
516 break;
517 }
518 kfree(nhi);
519 }
520
nexthop_free_rcu(struct rcu_head * head)521 void nexthop_free_rcu(struct rcu_head *head)
522 {
523 struct nexthop *nh = container_of(head, struct nexthop, rcu);
524
525 if (nh->is_group)
526 nexthop_free_group(nh);
527 else
528 nexthop_free_single(nh);
529
530 kfree(nh);
531 }
532 EXPORT_SYMBOL_GPL(nexthop_free_rcu);
533
nexthop_alloc(void)534 static struct nexthop *nexthop_alloc(void)
535 {
536 struct nexthop *nh;
537
538 nh = kzalloc(sizeof(struct nexthop), GFP_KERNEL);
539 if (nh) {
540 INIT_LIST_HEAD(&nh->fi_list);
541 INIT_LIST_HEAD(&nh->f6i_list);
542 INIT_LIST_HEAD(&nh->grp_list);
543 INIT_LIST_HEAD(&nh->fdb_list);
544 spin_lock_init(&nh->lock);
545 }
546 return nh;
547 }
548
nexthop_grp_alloc(u16 num_nh)549 static struct nh_group *nexthop_grp_alloc(u16 num_nh)
550 {
551 struct nh_group *nhg;
552
553 nhg = kzalloc(struct_size(nhg, nh_entries, num_nh), GFP_KERNEL);
554 if (nhg)
555 nhg->num_nh = num_nh;
556
557 return nhg;
558 }
559
560 static void nh_res_table_upkeep_dw(struct work_struct *work);
561
562 static struct nh_res_table *
nexthop_res_table_alloc(struct net * net,u32 nhg_id,struct nh_config * cfg)563 nexthop_res_table_alloc(struct net *net, u32 nhg_id, struct nh_config *cfg)
564 {
565 const u16 num_nh_buckets = cfg->nh_grp_res_num_buckets;
566 struct nh_res_table *res_table;
567 unsigned long size;
568
569 size = struct_size(res_table, nh_buckets, num_nh_buckets);
570 res_table = __vmalloc(size, GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN);
571 if (!res_table)
572 return NULL;
573
574 res_table->net = net;
575 res_table->nhg_id = nhg_id;
576 INIT_DELAYED_WORK(&res_table->upkeep_dw, &nh_res_table_upkeep_dw);
577 INIT_LIST_HEAD(&res_table->uw_nh_entries);
578 res_table->idle_timer = cfg->nh_grp_res_idle_timer;
579 res_table->unbalanced_timer = cfg->nh_grp_res_unbalanced_timer;
580 res_table->num_nh_buckets = num_nh_buckets;
581 return res_table;
582 }
583
nh_base_seq_inc(struct net * net)584 static void nh_base_seq_inc(struct net *net)
585 {
586 while (++net->nexthop.seq == 0)
587 ;
588 }
589
590 /* no reference taken; rcu lock or rtnl must be held */
nexthop_find_by_id(struct net * net,u32 id)591 struct nexthop *nexthop_find_by_id(struct net *net, u32 id)
592 {
593 struct rb_node **pp, *parent = NULL, *next;
594
595 pp = &net->nexthop.rb_root.rb_node;
596 while (1) {
597 struct nexthop *nh;
598
599 next = rcu_dereference_raw(*pp);
600 if (!next)
601 break;
602 parent = next;
603
604 nh = rb_entry(parent, struct nexthop, rb_node);
605 if (id < nh->id)
606 pp = &next->rb_left;
607 else if (id > nh->id)
608 pp = &next->rb_right;
609 else
610 return nh;
611 }
612 return NULL;
613 }
614 EXPORT_SYMBOL_GPL(nexthop_find_by_id);
615
616 /* used for auto id allocation; called with rtnl held */
nh_find_unused_id(struct net * net)617 static u32 nh_find_unused_id(struct net *net)
618 {
619 u32 id_start = net->nexthop.last_id_allocated;
620
621 while (1) {
622 net->nexthop.last_id_allocated++;
623 if (net->nexthop.last_id_allocated == id_start)
624 break;
625
626 if (!nexthop_find_by_id(net, net->nexthop.last_id_allocated))
627 return net->nexthop.last_id_allocated;
628 }
629 return 0;
630 }
631
nh_res_time_set_deadline(unsigned long next_time,unsigned long * deadline)632 static void nh_res_time_set_deadline(unsigned long next_time,
633 unsigned long *deadline)
634 {
635 if (time_before(next_time, *deadline))
636 *deadline = next_time;
637 }
638
nh_res_table_unbalanced_time(struct nh_res_table * res_table)639 static clock_t nh_res_table_unbalanced_time(struct nh_res_table *res_table)
640 {
641 if (list_empty(&res_table->uw_nh_entries))
642 return 0;
643 return jiffies_delta_to_clock_t(jiffies - res_table->unbalanced_since);
644 }
645
nla_put_nh_group_res(struct sk_buff * skb,struct nh_group * nhg)646 static int nla_put_nh_group_res(struct sk_buff *skb, struct nh_group *nhg)
647 {
648 struct nh_res_table *res_table = rtnl_dereference(nhg->res_table);
649 struct nlattr *nest;
650
651 nest = nla_nest_start(skb, NHA_RES_GROUP);
652 if (!nest)
653 return -EMSGSIZE;
654
655 if (nla_put_u16(skb, NHA_RES_GROUP_BUCKETS,
656 res_table->num_nh_buckets) ||
657 nla_put_u32(skb, NHA_RES_GROUP_IDLE_TIMER,
658 jiffies_to_clock_t(res_table->idle_timer)) ||
659 nla_put_u32(skb, NHA_RES_GROUP_UNBALANCED_TIMER,
660 jiffies_to_clock_t(res_table->unbalanced_timer)) ||
661 nla_put_u64_64bit(skb, NHA_RES_GROUP_UNBALANCED_TIME,
662 nh_res_table_unbalanced_time(res_table),
663 NHA_RES_GROUP_PAD))
664 goto nla_put_failure;
665
666 nla_nest_end(skb, nest);
667 return 0;
668
669 nla_put_failure:
670 nla_nest_cancel(skb, nest);
671 return -EMSGSIZE;
672 }
673
nh_grp_entry_stats_inc(struct nh_grp_entry * nhge)674 static void nh_grp_entry_stats_inc(struct nh_grp_entry *nhge)
675 {
676 struct nh_grp_entry_stats *cpu_stats;
677
678 cpu_stats = get_cpu_ptr(nhge->stats);
679 u64_stats_update_begin(&cpu_stats->syncp);
680 u64_stats_inc(&cpu_stats->packets);
681 u64_stats_update_end(&cpu_stats->syncp);
682 put_cpu_ptr(cpu_stats);
683 }
684
nh_grp_entry_stats_read(struct nh_grp_entry * nhge,u64 * ret_packets)685 static void nh_grp_entry_stats_read(struct nh_grp_entry *nhge,
686 u64 *ret_packets)
687 {
688 int i;
689
690 *ret_packets = 0;
691
692 for_each_possible_cpu(i) {
693 struct nh_grp_entry_stats *cpu_stats;
694 unsigned int start;
695 u64 packets;
696
697 cpu_stats = per_cpu_ptr(nhge->stats, i);
698 do {
699 start = u64_stats_fetch_begin(&cpu_stats->syncp);
700 packets = u64_stats_read(&cpu_stats->packets);
701 } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
702
703 *ret_packets += packets;
704 }
705 }
706
nh_notifier_grp_hw_stats_init(struct nh_notifier_info * info,const struct nexthop * nh)707 static int nh_notifier_grp_hw_stats_init(struct nh_notifier_info *info,
708 const struct nexthop *nh)
709 {
710 struct nh_group *nhg;
711 int i;
712
713 ASSERT_RTNL();
714 nhg = rtnl_dereference(nh->nh_grp);
715
716 info->id = nh->id;
717 info->type = NH_NOTIFIER_INFO_TYPE_GRP_HW_STATS;
718 info->nh_grp_hw_stats = kzalloc(struct_size(info->nh_grp_hw_stats,
719 stats, nhg->num_nh),
720 GFP_KERNEL);
721 if (!info->nh_grp_hw_stats)
722 return -ENOMEM;
723
724 info->nh_grp_hw_stats->num_nh = nhg->num_nh;
725 for (i = 0; i < nhg->num_nh; i++) {
726 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
727
728 info->nh_grp_hw_stats->stats[i].id = nhge->nh->id;
729 }
730
731 return 0;
732 }
733
nh_notifier_grp_hw_stats_fini(struct nh_notifier_info * info)734 static void nh_notifier_grp_hw_stats_fini(struct nh_notifier_info *info)
735 {
736 kfree(info->nh_grp_hw_stats);
737 }
738
nh_grp_hw_stats_report_delta(struct nh_notifier_grp_hw_stats_info * info,unsigned int nh_idx,u64 delta_packets)739 void nh_grp_hw_stats_report_delta(struct nh_notifier_grp_hw_stats_info *info,
740 unsigned int nh_idx,
741 u64 delta_packets)
742 {
743 info->hw_stats_used = true;
744 info->stats[nh_idx].packets += delta_packets;
745 }
746 EXPORT_SYMBOL(nh_grp_hw_stats_report_delta);
747
nh_grp_hw_stats_apply_update(struct nexthop * nh,struct nh_notifier_info * info)748 static void nh_grp_hw_stats_apply_update(struct nexthop *nh,
749 struct nh_notifier_info *info)
750 {
751 struct nh_group *nhg;
752 int i;
753
754 ASSERT_RTNL();
755 nhg = rtnl_dereference(nh->nh_grp);
756
757 for (i = 0; i < nhg->num_nh; i++) {
758 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
759
760 nhge->packets_hw += info->nh_grp_hw_stats->stats[i].packets;
761 }
762 }
763
nh_grp_hw_stats_update(struct nexthop * nh,bool * hw_stats_used)764 static int nh_grp_hw_stats_update(struct nexthop *nh, bool *hw_stats_used)
765 {
766 struct nh_notifier_info info = {
767 .net = nh->net,
768 };
769 struct net *net = nh->net;
770 int err;
771
772 if (nexthop_notifiers_is_empty(net)) {
773 *hw_stats_used = false;
774 return 0;
775 }
776
777 err = nh_notifier_grp_hw_stats_init(&info, nh);
778 if (err)
779 return err;
780
781 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
782 NEXTHOP_EVENT_HW_STATS_REPORT_DELTA,
783 &info);
784
785 /* Cache whatever we got, even if there was an error, otherwise the
786 * successful stats retrievals would get lost.
787 */
788 nh_grp_hw_stats_apply_update(nh, &info);
789 *hw_stats_used = info.nh_grp_hw_stats->hw_stats_used;
790
791 nh_notifier_grp_hw_stats_fini(&info);
792 return notifier_to_errno(err);
793 }
794
nla_put_nh_group_stats_entry(struct sk_buff * skb,struct nh_grp_entry * nhge,u32 op_flags)795 static int nla_put_nh_group_stats_entry(struct sk_buff *skb,
796 struct nh_grp_entry *nhge,
797 u32 op_flags)
798 {
799 struct nlattr *nest;
800 u64 packets;
801
802 nh_grp_entry_stats_read(nhge, &packets);
803
804 nest = nla_nest_start(skb, NHA_GROUP_STATS_ENTRY);
805 if (!nest)
806 return -EMSGSIZE;
807
808 if (nla_put_u32(skb, NHA_GROUP_STATS_ENTRY_ID, nhge->nh->id) ||
809 nla_put_uint(skb, NHA_GROUP_STATS_ENTRY_PACKETS,
810 packets + nhge->packets_hw))
811 goto nla_put_failure;
812
813 if (op_flags & NHA_OP_FLAG_DUMP_HW_STATS &&
814 nla_put_uint(skb, NHA_GROUP_STATS_ENTRY_PACKETS_HW,
815 nhge->packets_hw))
816 goto nla_put_failure;
817
818 nla_nest_end(skb, nest);
819 return 0;
820
821 nla_put_failure:
822 nla_nest_cancel(skb, nest);
823 return -EMSGSIZE;
824 }
825
nla_put_nh_group_stats(struct sk_buff * skb,struct nexthop * nh,u32 op_flags)826 static int nla_put_nh_group_stats(struct sk_buff *skb, struct nexthop *nh,
827 u32 op_flags)
828 {
829 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
830 struct nlattr *nest;
831 bool hw_stats_used;
832 int err;
833 int i;
834
835 if (nla_put_u32(skb, NHA_HW_STATS_ENABLE, nhg->hw_stats))
836 goto err_out;
837
838 if (op_flags & NHA_OP_FLAG_DUMP_HW_STATS &&
839 nhg->hw_stats) {
840 err = nh_grp_hw_stats_update(nh, &hw_stats_used);
841 if (err)
842 goto out;
843
844 if (nla_put_u32(skb, NHA_HW_STATS_USED, hw_stats_used))
845 goto err_out;
846 }
847
848 nest = nla_nest_start(skb, NHA_GROUP_STATS);
849 if (!nest)
850 goto err_out;
851
852 for (i = 0; i < nhg->num_nh; i++)
853 if (nla_put_nh_group_stats_entry(skb, &nhg->nh_entries[i],
854 op_flags))
855 goto cancel_out;
856
857 nla_nest_end(skb, nest);
858 return 0;
859
860 cancel_out:
861 nla_nest_cancel(skb, nest);
862 err_out:
863 err = -EMSGSIZE;
864 out:
865 return err;
866 }
867
nla_put_nh_group(struct sk_buff * skb,struct nexthop * nh,u32 op_flags,u32 * resp_op_flags)868 static int nla_put_nh_group(struct sk_buff *skb, struct nexthop *nh,
869 u32 op_flags, u32 *resp_op_flags)
870 {
871 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
872 struct nexthop_grp *p;
873 size_t len = nhg->num_nh * sizeof(*p);
874 struct nlattr *nla;
875 u16 group_type = 0;
876 u16 weight;
877 int i;
878
879 *resp_op_flags |= NHA_OP_FLAG_RESP_GRP_RESVD_0;
880
881 if (nhg->hash_threshold)
882 group_type = NEXTHOP_GRP_TYPE_MPATH;
883 else if (nhg->resilient)
884 group_type = NEXTHOP_GRP_TYPE_RES;
885
886 if (nla_put_u16(skb, NHA_GROUP_TYPE, group_type))
887 goto nla_put_failure;
888
889 nla = nla_reserve(skb, NHA_GROUP, len);
890 if (!nla)
891 goto nla_put_failure;
892
893 p = nla_data(nla);
894 for (i = 0; i < nhg->num_nh; ++i) {
895 weight = nhg->nh_entries[i].weight - 1;
896
897 *p++ = (struct nexthop_grp) {
898 .id = nhg->nh_entries[i].nh->id,
899 .weight = weight,
900 .weight_high = weight >> 8,
901 };
902 }
903
904 if (nhg->resilient && nla_put_nh_group_res(skb, nhg))
905 goto nla_put_failure;
906
907 if (op_flags & NHA_OP_FLAG_DUMP_STATS &&
908 (nla_put_u32(skb, NHA_HW_STATS_ENABLE, nhg->hw_stats) ||
909 nla_put_nh_group_stats(skb, nh, op_flags)))
910 goto nla_put_failure;
911
912 return 0;
913
914 nla_put_failure:
915 return -EMSGSIZE;
916 }
917
nh_fill_node(struct sk_buff * skb,struct nexthop * nh,int event,u32 portid,u32 seq,unsigned int nlflags,u32 op_flags)918 static int nh_fill_node(struct sk_buff *skb, struct nexthop *nh,
919 int event, u32 portid, u32 seq, unsigned int nlflags,
920 u32 op_flags)
921 {
922 struct fib6_nh *fib6_nh;
923 struct fib_nh *fib_nh;
924 struct nlmsghdr *nlh;
925 struct nh_info *nhi;
926 struct nhmsg *nhm;
927
928 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags);
929 if (!nlh)
930 return -EMSGSIZE;
931
932 nhm = nlmsg_data(nlh);
933 nhm->nh_family = AF_UNSPEC;
934 nhm->nh_flags = nh->nh_flags;
935 nhm->nh_protocol = nh->protocol;
936 nhm->nh_scope = 0;
937 nhm->resvd = 0;
938
939 if (nla_put_u32(skb, NHA_ID, nh->id))
940 goto nla_put_failure;
941
942 if (nh->is_group) {
943 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
944 u32 resp_op_flags = 0;
945
946 if (nhg->fdb_nh && nla_put_flag(skb, NHA_FDB))
947 goto nla_put_failure;
948 if (nla_put_nh_group(skb, nh, op_flags, &resp_op_flags) ||
949 nla_put_u32(skb, NHA_OP_FLAGS, resp_op_flags))
950 goto nla_put_failure;
951 goto out;
952 }
953
954 nhi = rtnl_dereference(nh->nh_info);
955 nhm->nh_family = nhi->family;
956 if (nhi->reject_nh) {
957 if (nla_put_flag(skb, NHA_BLACKHOLE))
958 goto nla_put_failure;
959 goto out;
960 } else if (nhi->fdb_nh) {
961 if (nla_put_flag(skb, NHA_FDB))
962 goto nla_put_failure;
963 } else {
964 const struct net_device *dev;
965
966 dev = nhi->fib_nhc.nhc_dev;
967 if (dev && nla_put_u32(skb, NHA_OIF, dev->ifindex))
968 goto nla_put_failure;
969 }
970
971 nhm->nh_scope = nhi->fib_nhc.nhc_scope;
972 switch (nhi->family) {
973 case AF_INET:
974 fib_nh = &nhi->fib_nh;
975 if (fib_nh->fib_nh_gw_family &&
976 nla_put_be32(skb, NHA_GATEWAY, fib_nh->fib_nh_gw4))
977 goto nla_put_failure;
978 break;
979
980 case AF_INET6:
981 fib6_nh = &nhi->fib6_nh;
982 if (fib6_nh->fib_nh_gw_family &&
983 nla_put_in6_addr(skb, NHA_GATEWAY, &fib6_nh->fib_nh_gw6))
984 goto nla_put_failure;
985 break;
986 }
987
988 if (lwtunnel_fill_encap(skb, nhi->fib_nhc.nhc_lwtstate,
989 NHA_ENCAP, NHA_ENCAP_TYPE) < 0)
990 goto nla_put_failure;
991
992 out:
993 nlmsg_end(skb, nlh);
994 return 0;
995
996 nla_put_failure:
997 nlmsg_cancel(skb, nlh);
998 return -EMSGSIZE;
999 }
1000
nh_nlmsg_size_grp_res(struct nh_group * nhg)1001 static size_t nh_nlmsg_size_grp_res(struct nh_group *nhg)
1002 {
1003 return nla_total_size(0) + /* NHA_RES_GROUP */
1004 nla_total_size(2) + /* NHA_RES_GROUP_BUCKETS */
1005 nla_total_size(4) + /* NHA_RES_GROUP_IDLE_TIMER */
1006 nla_total_size(4) + /* NHA_RES_GROUP_UNBALANCED_TIMER */
1007 nla_total_size_64bit(8);/* NHA_RES_GROUP_UNBALANCED_TIME */
1008 }
1009
nh_nlmsg_size_grp(struct nexthop * nh)1010 static size_t nh_nlmsg_size_grp(struct nexthop *nh)
1011 {
1012 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
1013 size_t sz = sizeof(struct nexthop_grp) * nhg->num_nh;
1014 size_t tot = nla_total_size(sz) +
1015 nla_total_size(2); /* NHA_GROUP_TYPE */
1016
1017 if (nhg->resilient)
1018 tot += nh_nlmsg_size_grp_res(nhg);
1019
1020 return tot;
1021 }
1022
nh_nlmsg_size_single(struct nexthop * nh)1023 static size_t nh_nlmsg_size_single(struct nexthop *nh)
1024 {
1025 struct nh_info *nhi = rtnl_dereference(nh->nh_info);
1026 size_t sz;
1027
1028 /* covers NHA_BLACKHOLE since NHA_OIF and BLACKHOLE
1029 * are mutually exclusive
1030 */
1031 sz = nla_total_size(4); /* NHA_OIF */
1032
1033 switch (nhi->family) {
1034 case AF_INET:
1035 if (nhi->fib_nh.fib_nh_gw_family)
1036 sz += nla_total_size(4); /* NHA_GATEWAY */
1037 break;
1038
1039 case AF_INET6:
1040 /* NHA_GATEWAY */
1041 if (nhi->fib6_nh.fib_nh_gw_family)
1042 sz += nla_total_size(sizeof(const struct in6_addr));
1043 break;
1044 }
1045
1046 if (nhi->fib_nhc.nhc_lwtstate) {
1047 sz += lwtunnel_get_encap_size(nhi->fib_nhc.nhc_lwtstate);
1048 sz += nla_total_size(2); /* NHA_ENCAP_TYPE */
1049 }
1050
1051 return sz;
1052 }
1053
nh_nlmsg_size(struct nexthop * nh)1054 static size_t nh_nlmsg_size(struct nexthop *nh)
1055 {
1056 size_t sz = NLMSG_ALIGN(sizeof(struct nhmsg));
1057
1058 sz += nla_total_size(4); /* NHA_ID */
1059
1060 if (nh->is_group)
1061 sz += nh_nlmsg_size_grp(nh) +
1062 nla_total_size(4) + /* NHA_OP_FLAGS */
1063 0;
1064 else
1065 sz += nh_nlmsg_size_single(nh);
1066
1067 return sz;
1068 }
1069
nexthop_notify(int event,struct nexthop * nh,struct nl_info * info)1070 static void nexthop_notify(int event, struct nexthop *nh, struct nl_info *info)
1071 {
1072 unsigned int nlflags = info->nlh ? info->nlh->nlmsg_flags : 0;
1073 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
1074 struct sk_buff *skb;
1075 int err = -ENOBUFS;
1076
1077 skb = nlmsg_new(nh_nlmsg_size(nh), gfp_any());
1078 if (!skb)
1079 goto errout;
1080
1081 err = nh_fill_node(skb, nh, event, info->portid, seq, nlflags, 0);
1082 if (err < 0) {
1083 /* -EMSGSIZE implies BUG in nh_nlmsg_size() */
1084 WARN_ON(err == -EMSGSIZE);
1085 kfree_skb(skb);
1086 goto errout;
1087 }
1088
1089 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_NEXTHOP,
1090 info->nlh, gfp_any());
1091 return;
1092 errout:
1093 rtnl_set_sk_err(info->nl_net, RTNLGRP_NEXTHOP, err);
1094 }
1095
nh_res_bucket_used_time(const struct nh_res_bucket * bucket)1096 static unsigned long nh_res_bucket_used_time(const struct nh_res_bucket *bucket)
1097 {
1098 return (unsigned long)atomic_long_read(&bucket->used_time);
1099 }
1100
1101 static unsigned long
nh_res_bucket_idle_point(const struct nh_res_table * res_table,const struct nh_res_bucket * bucket,unsigned long now)1102 nh_res_bucket_idle_point(const struct nh_res_table *res_table,
1103 const struct nh_res_bucket *bucket,
1104 unsigned long now)
1105 {
1106 unsigned long time = nh_res_bucket_used_time(bucket);
1107
1108 /* Bucket was not used since it was migrated. The idle time is now. */
1109 if (time == bucket->migrated_time)
1110 return now;
1111
1112 return time + res_table->idle_timer;
1113 }
1114
1115 static unsigned long
nh_res_table_unb_point(const struct nh_res_table * res_table)1116 nh_res_table_unb_point(const struct nh_res_table *res_table)
1117 {
1118 return res_table->unbalanced_since + res_table->unbalanced_timer;
1119 }
1120
nh_res_bucket_set_idle(const struct nh_res_table * res_table,struct nh_res_bucket * bucket)1121 static void nh_res_bucket_set_idle(const struct nh_res_table *res_table,
1122 struct nh_res_bucket *bucket)
1123 {
1124 unsigned long now = jiffies;
1125
1126 atomic_long_set(&bucket->used_time, (long)now);
1127 bucket->migrated_time = now;
1128 }
1129
nh_res_bucket_set_busy(struct nh_res_bucket * bucket)1130 static void nh_res_bucket_set_busy(struct nh_res_bucket *bucket)
1131 {
1132 atomic_long_set(&bucket->used_time, (long)jiffies);
1133 }
1134
nh_res_bucket_idle_time(const struct nh_res_bucket * bucket)1135 static clock_t nh_res_bucket_idle_time(const struct nh_res_bucket *bucket)
1136 {
1137 unsigned long used_time = nh_res_bucket_used_time(bucket);
1138
1139 return jiffies_delta_to_clock_t(jiffies - used_time);
1140 }
1141
nh_fill_res_bucket(struct sk_buff * skb,struct nexthop * nh,struct nh_res_bucket * bucket,u16 bucket_index,int event,u32 portid,u32 seq,unsigned int nlflags,struct netlink_ext_ack * extack)1142 static int nh_fill_res_bucket(struct sk_buff *skb, struct nexthop *nh,
1143 struct nh_res_bucket *bucket, u16 bucket_index,
1144 int event, u32 portid, u32 seq,
1145 unsigned int nlflags,
1146 struct netlink_ext_ack *extack)
1147 {
1148 struct nh_grp_entry *nhge = nh_res_dereference(bucket->nh_entry);
1149 struct nlmsghdr *nlh;
1150 struct nlattr *nest;
1151 struct nhmsg *nhm;
1152
1153 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags);
1154 if (!nlh)
1155 return -EMSGSIZE;
1156
1157 nhm = nlmsg_data(nlh);
1158 nhm->nh_family = AF_UNSPEC;
1159 nhm->nh_flags = bucket->nh_flags;
1160 nhm->nh_protocol = nh->protocol;
1161 nhm->nh_scope = 0;
1162 nhm->resvd = 0;
1163
1164 if (nla_put_u32(skb, NHA_ID, nh->id))
1165 goto nla_put_failure;
1166
1167 nest = nla_nest_start(skb, NHA_RES_BUCKET);
1168 if (!nest)
1169 goto nla_put_failure;
1170
1171 if (nla_put_u16(skb, NHA_RES_BUCKET_INDEX, bucket_index) ||
1172 nla_put_u32(skb, NHA_RES_BUCKET_NH_ID, nhge->nh->id) ||
1173 nla_put_u64_64bit(skb, NHA_RES_BUCKET_IDLE_TIME,
1174 nh_res_bucket_idle_time(bucket),
1175 NHA_RES_BUCKET_PAD))
1176 goto nla_put_failure_nest;
1177
1178 nla_nest_end(skb, nest);
1179 nlmsg_end(skb, nlh);
1180 return 0;
1181
1182 nla_put_failure_nest:
1183 nla_nest_cancel(skb, nest);
1184 nla_put_failure:
1185 nlmsg_cancel(skb, nlh);
1186 return -EMSGSIZE;
1187 }
1188
nexthop_bucket_notify(struct nh_res_table * res_table,u16 bucket_index)1189 static void nexthop_bucket_notify(struct nh_res_table *res_table,
1190 u16 bucket_index)
1191 {
1192 struct nh_res_bucket *bucket = &res_table->nh_buckets[bucket_index];
1193 struct nh_grp_entry *nhge = nh_res_dereference(bucket->nh_entry);
1194 struct nexthop *nh = nhge->nh_parent;
1195 struct sk_buff *skb;
1196 int err = -ENOBUFS;
1197
1198 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1199 if (!skb)
1200 goto errout;
1201
1202 err = nh_fill_res_bucket(skb, nh, bucket, bucket_index,
1203 RTM_NEWNEXTHOPBUCKET, 0, 0, NLM_F_REPLACE,
1204 NULL);
1205 if (err < 0) {
1206 kfree_skb(skb);
1207 goto errout;
1208 }
1209
1210 rtnl_notify(skb, nh->net, 0, RTNLGRP_NEXTHOP, NULL, GFP_KERNEL);
1211 return;
1212 errout:
1213 rtnl_set_sk_err(nh->net, RTNLGRP_NEXTHOP, err);
1214 }
1215
valid_group_nh(struct nexthop * nh,unsigned int npaths,bool * is_fdb,struct netlink_ext_ack * extack)1216 static bool valid_group_nh(struct nexthop *nh, unsigned int npaths,
1217 bool *is_fdb, struct netlink_ext_ack *extack)
1218 {
1219 if (nh->is_group) {
1220 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
1221
1222 /* Nesting groups within groups is not supported. */
1223 if (nhg->hash_threshold) {
1224 NL_SET_ERR_MSG(extack,
1225 "Hash-threshold group can not be a nexthop within a group");
1226 return false;
1227 }
1228 if (nhg->resilient) {
1229 NL_SET_ERR_MSG(extack,
1230 "Resilient group can not be a nexthop within a group");
1231 return false;
1232 }
1233 *is_fdb = nhg->fdb_nh;
1234 } else {
1235 struct nh_info *nhi = rtnl_dereference(nh->nh_info);
1236
1237 if (nhi->reject_nh && npaths > 1) {
1238 NL_SET_ERR_MSG(extack,
1239 "Blackhole nexthop can not be used in a group with more than 1 path");
1240 return false;
1241 }
1242 *is_fdb = nhi->fdb_nh;
1243 }
1244
1245 return true;
1246 }
1247
nh_check_attr_fdb_group(struct nexthop * nh,u8 * nh_family,struct netlink_ext_ack * extack)1248 static int nh_check_attr_fdb_group(struct nexthop *nh, u8 *nh_family,
1249 struct netlink_ext_ack *extack)
1250 {
1251 struct nh_info *nhi;
1252
1253 nhi = rtnl_dereference(nh->nh_info);
1254
1255 if (!nhi->fdb_nh) {
1256 NL_SET_ERR_MSG(extack, "FDB nexthop group can only have fdb nexthops");
1257 return -EINVAL;
1258 }
1259
1260 if (*nh_family == AF_UNSPEC) {
1261 *nh_family = nhi->family;
1262 } else if (*nh_family != nhi->family) {
1263 NL_SET_ERR_MSG(extack, "FDB nexthop group cannot have mixed family nexthops");
1264 return -EINVAL;
1265 }
1266
1267 return 0;
1268 }
1269
nh_check_attr_group(struct net * net,struct nlattr * tb[],size_t tb_size,u16 nh_grp_type,struct netlink_ext_ack * extack)1270 static int nh_check_attr_group(struct net *net,
1271 struct nlattr *tb[], size_t tb_size,
1272 u16 nh_grp_type, struct netlink_ext_ack *extack)
1273 {
1274 unsigned int len = nla_len(tb[NHA_GROUP]);
1275 struct nexthop_grp *nhg;
1276 unsigned int i, j;
1277
1278 if (!len || len & (sizeof(struct nexthop_grp) - 1)) {
1279 NL_SET_ERR_MSG(extack,
1280 "Invalid length for nexthop group attribute");
1281 return -EINVAL;
1282 }
1283
1284 /* convert len to number of nexthop ids */
1285 len /= sizeof(*nhg);
1286
1287 nhg = nla_data(tb[NHA_GROUP]);
1288 for (i = 0; i < len; ++i) {
1289 if (nhg[i].resvd2) {
1290 NL_SET_ERR_MSG(extack, "Reserved field in nexthop_grp must be 0");
1291 return -EINVAL;
1292 }
1293 if (nexthop_grp_weight(&nhg[i]) == 0) {
1294 /* 0xffff got passed in, representing weight of 0x10000,
1295 * which is too heavy.
1296 */
1297 NL_SET_ERR_MSG(extack, "Invalid value for weight");
1298 return -EINVAL;
1299 }
1300 for (j = i + 1; j < len; ++j) {
1301 if (nhg[i].id == nhg[j].id) {
1302 NL_SET_ERR_MSG(extack, "Nexthop id can not be used twice in a group");
1303 return -EINVAL;
1304 }
1305 }
1306 }
1307
1308 nhg = nla_data(tb[NHA_GROUP]);
1309 for (i = NHA_GROUP_TYPE + 1; i < tb_size; ++i) {
1310 if (!tb[i])
1311 continue;
1312 switch (i) {
1313 case NHA_HW_STATS_ENABLE:
1314 case NHA_FDB:
1315 continue;
1316 case NHA_RES_GROUP:
1317 if (nh_grp_type == NEXTHOP_GRP_TYPE_RES)
1318 continue;
1319 break;
1320 }
1321 NL_SET_ERR_MSG(extack,
1322 "No other attributes can be set in nexthop groups");
1323 return -EINVAL;
1324 }
1325
1326 return 0;
1327 }
1328
nh_check_attr_group_rtnl(struct net * net,struct nlattr * tb[],struct netlink_ext_ack * extack)1329 static int nh_check_attr_group_rtnl(struct net *net, struct nlattr *tb[],
1330 struct netlink_ext_ack *extack)
1331 {
1332 u8 nh_family = AF_UNSPEC;
1333 struct nexthop_grp *nhg;
1334 unsigned int len;
1335 unsigned int i;
1336 u8 nhg_fdb;
1337
1338 len = nla_len(tb[NHA_GROUP]) / sizeof(*nhg);
1339 nhg = nla_data(tb[NHA_GROUP]);
1340 nhg_fdb = !!tb[NHA_FDB];
1341
1342 for (i = 0; i < len; i++) {
1343 struct nexthop *nh;
1344 bool is_fdb_nh;
1345
1346 nh = nexthop_find_by_id(net, nhg[i].id);
1347 if (!nh) {
1348 NL_SET_ERR_MSG(extack, "Invalid nexthop id");
1349 return -EINVAL;
1350 }
1351 if (!valid_group_nh(nh, len, &is_fdb_nh, extack))
1352 return -EINVAL;
1353
1354 if (nhg_fdb && nh_check_attr_fdb_group(nh, &nh_family, extack))
1355 return -EINVAL;
1356
1357 if (!nhg_fdb && is_fdb_nh) {
1358 NL_SET_ERR_MSG(extack, "Non FDB nexthop group cannot have fdb nexthops");
1359 return -EINVAL;
1360 }
1361 }
1362
1363 return 0;
1364 }
1365
ipv6_good_nh(const struct fib6_nh * nh)1366 static bool ipv6_good_nh(const struct fib6_nh *nh)
1367 {
1368 int state = NUD_REACHABLE;
1369 struct neighbour *n;
1370
1371 rcu_read_lock();
1372
1373 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev, &nh->fib_nh_gw6);
1374 if (n)
1375 state = READ_ONCE(n->nud_state);
1376
1377 rcu_read_unlock();
1378
1379 return !!(state & NUD_VALID);
1380 }
1381
ipv4_good_nh(const struct fib_nh * nh)1382 static bool ipv4_good_nh(const struct fib_nh *nh)
1383 {
1384 int state = NUD_REACHABLE;
1385 struct neighbour *n;
1386
1387 rcu_read_lock();
1388
1389 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
1390 (__force u32)nh->fib_nh_gw4);
1391 if (n)
1392 state = READ_ONCE(n->nud_state);
1393
1394 rcu_read_unlock();
1395
1396 return !!(state & NUD_VALID);
1397 }
1398
nexthop_is_good_nh(const struct nexthop * nh)1399 static bool nexthop_is_good_nh(const struct nexthop *nh)
1400 {
1401 struct nh_info *nhi = rcu_dereference(nh->nh_info);
1402
1403 switch (nhi->family) {
1404 case AF_INET:
1405 return ipv4_good_nh(&nhi->fib_nh);
1406 case AF_INET6:
1407 return ipv6_good_nh(&nhi->fib6_nh);
1408 }
1409
1410 return false;
1411 }
1412
nexthop_select_path_fdb(struct nh_group * nhg,int hash)1413 static struct nexthop *nexthop_select_path_fdb(struct nh_group *nhg, int hash)
1414 {
1415 int i;
1416
1417 for (i = 0; i < nhg->num_nh; i++) {
1418 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
1419
1420 if (hash > atomic_read(&nhge->hthr.upper_bound))
1421 continue;
1422
1423 nh_grp_entry_stats_inc(nhge);
1424 return nhge->nh;
1425 }
1426
1427 WARN_ON_ONCE(1);
1428 return NULL;
1429 }
1430
nexthop_select_path_hthr(struct nh_group * nhg,int hash)1431 static struct nexthop *nexthop_select_path_hthr(struct nh_group *nhg, int hash)
1432 {
1433 struct nh_grp_entry *nhge0 = NULL;
1434 int i;
1435
1436 if (nhg->fdb_nh)
1437 return nexthop_select_path_fdb(nhg, hash);
1438
1439 for (i = 0; i < nhg->num_nh; ++i) {
1440 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
1441
1442 /* nexthops always check if it is good and does
1443 * not rely on a sysctl for this behavior
1444 */
1445 if (!nexthop_is_good_nh(nhge->nh))
1446 continue;
1447
1448 if (!nhge0)
1449 nhge0 = nhge;
1450
1451 if (hash > atomic_read(&nhge->hthr.upper_bound))
1452 continue;
1453
1454 nh_grp_entry_stats_inc(nhge);
1455 return nhge->nh;
1456 }
1457
1458 if (!nhge0)
1459 nhge0 = &nhg->nh_entries[0];
1460 nh_grp_entry_stats_inc(nhge0);
1461 return nhge0->nh;
1462 }
1463
nexthop_select_path_res(struct nh_group * nhg,int hash)1464 static struct nexthop *nexthop_select_path_res(struct nh_group *nhg, int hash)
1465 {
1466 struct nh_res_table *res_table = rcu_dereference(nhg->res_table);
1467 u16 bucket_index = hash % res_table->num_nh_buckets;
1468 struct nh_res_bucket *bucket;
1469 struct nh_grp_entry *nhge;
1470
1471 /* nexthop_select_path() is expected to return a non-NULL value, so
1472 * skip protocol validation and just hand out whatever there is.
1473 */
1474 bucket = &res_table->nh_buckets[bucket_index];
1475 nh_res_bucket_set_busy(bucket);
1476 nhge = rcu_dereference(bucket->nh_entry);
1477 nh_grp_entry_stats_inc(nhge);
1478 return nhge->nh;
1479 }
1480
nexthop_select_path(struct nexthop * nh,int hash)1481 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash)
1482 {
1483 struct nh_group *nhg;
1484
1485 if (!nh->is_group)
1486 return nh;
1487
1488 nhg = rcu_dereference(nh->nh_grp);
1489 if (nhg->hash_threshold)
1490 return nexthop_select_path_hthr(nhg, hash);
1491 else if (nhg->resilient)
1492 return nexthop_select_path_res(nhg, hash);
1493
1494 /* Unreachable. */
1495 return NULL;
1496 }
1497 EXPORT_SYMBOL_GPL(nexthop_select_path);
1498
nexthop_for_each_fib6_nh(struct nexthop * nh,int (* cb)(struct fib6_nh * nh,void * arg),void * arg)1499 int nexthop_for_each_fib6_nh(struct nexthop *nh,
1500 int (*cb)(struct fib6_nh *nh, void *arg),
1501 void *arg)
1502 {
1503 struct nh_info *nhi;
1504 int err;
1505
1506 if (nh->is_group) {
1507 struct nh_group *nhg;
1508 int i;
1509
1510 nhg = rcu_dereference_rtnl(nh->nh_grp);
1511 for (i = 0; i < nhg->num_nh; i++) {
1512 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
1513
1514 nhi = rcu_dereference_rtnl(nhge->nh->nh_info);
1515 err = cb(&nhi->fib6_nh, arg);
1516 if (err)
1517 return err;
1518 }
1519 } else {
1520 nhi = rcu_dereference_rtnl(nh->nh_info);
1521 err = cb(&nhi->fib6_nh, arg);
1522 if (err)
1523 return err;
1524 }
1525
1526 return 0;
1527 }
1528 EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh);
1529
check_src_addr(const struct in6_addr * saddr,struct netlink_ext_ack * extack)1530 static int check_src_addr(const struct in6_addr *saddr,
1531 struct netlink_ext_ack *extack)
1532 {
1533 if (!ipv6_addr_any(saddr)) {
1534 NL_SET_ERR_MSG(extack, "IPv6 routes using source address can not use nexthop objects");
1535 return -EINVAL;
1536 }
1537 return 0;
1538 }
1539
fib6_check_nexthop(struct nexthop * nh,struct fib6_config * cfg,struct netlink_ext_ack * extack)1540 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
1541 struct netlink_ext_ack *extack)
1542 {
1543 struct nh_info *nhi;
1544 bool is_fdb_nh;
1545
1546 /* fib6_src is unique to a fib6_info and limits the ability to cache
1547 * routes in fib6_nh within a nexthop that is potentially shared
1548 * across multiple fib entries. If the config wants to use source
1549 * routing it can not use nexthop objects. mlxsw also does not allow
1550 * fib6_src on routes.
1551 */
1552 if (cfg && check_src_addr(&cfg->fc_src, extack) < 0)
1553 return -EINVAL;
1554
1555 if (nh->is_group) {
1556 struct nh_group *nhg;
1557
1558 nhg = rcu_dereference_rtnl(nh->nh_grp);
1559 if (nhg->has_v4)
1560 goto no_v4_nh;
1561 is_fdb_nh = nhg->fdb_nh;
1562 } else {
1563 nhi = rcu_dereference_rtnl(nh->nh_info);
1564 if (nhi->family == AF_INET)
1565 goto no_v4_nh;
1566 is_fdb_nh = nhi->fdb_nh;
1567 }
1568
1569 if (is_fdb_nh) {
1570 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
1571 return -EINVAL;
1572 }
1573
1574 return 0;
1575 no_v4_nh:
1576 NL_SET_ERR_MSG(extack, "IPv6 routes can not use an IPv4 nexthop");
1577 return -EINVAL;
1578 }
1579 EXPORT_SYMBOL_GPL(fib6_check_nexthop);
1580
1581 /* if existing nexthop has ipv6 routes linked to it, need
1582 * to verify this new spec works with ipv6
1583 */
fib6_check_nh_list(struct nexthop * old,struct nexthop * new,struct netlink_ext_ack * extack)1584 static int fib6_check_nh_list(struct nexthop *old, struct nexthop *new,
1585 struct netlink_ext_ack *extack)
1586 {
1587 struct fib6_info *f6i;
1588
1589 if (list_empty(&old->f6i_list))
1590 return 0;
1591
1592 list_for_each_entry(f6i, &old->f6i_list, nh_list) {
1593 if (check_src_addr(&f6i->fib6_src.addr, extack) < 0)
1594 return -EINVAL;
1595 }
1596
1597 return fib6_check_nexthop(new, NULL, extack);
1598 }
1599
nexthop_check_scope(struct nh_info * nhi,u8 scope,struct netlink_ext_ack * extack)1600 static int nexthop_check_scope(struct nh_info *nhi, u8 scope,
1601 struct netlink_ext_ack *extack)
1602 {
1603 if (scope == RT_SCOPE_HOST && nhi->fib_nhc.nhc_gw_family) {
1604 NL_SET_ERR_MSG(extack,
1605 "Route with host scope can not have a gateway");
1606 return -EINVAL;
1607 }
1608
1609 if (nhi->fib_nhc.nhc_flags & RTNH_F_ONLINK && scope >= RT_SCOPE_LINK) {
1610 NL_SET_ERR_MSG(extack, "Scope mismatch with nexthop");
1611 return -EINVAL;
1612 }
1613
1614 return 0;
1615 }
1616
1617 /* Invoked by fib add code to verify nexthop by id is ok with
1618 * config for prefix; parts of fib_check_nh not done when nexthop
1619 * object is used.
1620 */
fib_check_nexthop(struct nexthop * nh,u8 scope,struct netlink_ext_ack * extack)1621 int fib_check_nexthop(struct nexthop *nh, u8 scope,
1622 struct netlink_ext_ack *extack)
1623 {
1624 struct nh_info *nhi;
1625 int err = 0;
1626
1627 if (nh->is_group) {
1628 struct nh_group *nhg;
1629
1630 nhg = rtnl_dereference(nh->nh_grp);
1631 if (nhg->fdb_nh) {
1632 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
1633 err = -EINVAL;
1634 goto out;
1635 }
1636
1637 if (scope == RT_SCOPE_HOST) {
1638 NL_SET_ERR_MSG(extack, "Route with host scope can not have multiple nexthops");
1639 err = -EINVAL;
1640 goto out;
1641 }
1642
1643 /* all nexthops in a group have the same scope */
1644 nhi = rtnl_dereference(nhg->nh_entries[0].nh->nh_info);
1645 err = nexthop_check_scope(nhi, scope, extack);
1646 } else {
1647 nhi = rtnl_dereference(nh->nh_info);
1648 if (nhi->fdb_nh) {
1649 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
1650 err = -EINVAL;
1651 goto out;
1652 }
1653 err = nexthop_check_scope(nhi, scope, extack);
1654 }
1655
1656 out:
1657 return err;
1658 }
1659
fib_check_nh_list(struct nexthop * old,struct nexthop * new,struct netlink_ext_ack * extack)1660 static int fib_check_nh_list(struct nexthop *old, struct nexthop *new,
1661 struct netlink_ext_ack *extack)
1662 {
1663 struct fib_info *fi;
1664
1665 list_for_each_entry(fi, &old->fi_list, nh_list) {
1666 int err;
1667
1668 err = fib_check_nexthop(new, fi->fib_scope, extack);
1669 if (err)
1670 return err;
1671 }
1672 return 0;
1673 }
1674
nh_res_nhge_is_balanced(const struct nh_grp_entry * nhge)1675 static bool nh_res_nhge_is_balanced(const struct nh_grp_entry *nhge)
1676 {
1677 return nhge->res.count_buckets == nhge->res.wants_buckets;
1678 }
1679
nh_res_nhge_is_ow(const struct nh_grp_entry * nhge)1680 static bool nh_res_nhge_is_ow(const struct nh_grp_entry *nhge)
1681 {
1682 return nhge->res.count_buckets > nhge->res.wants_buckets;
1683 }
1684
nh_res_nhge_is_uw(const struct nh_grp_entry * nhge)1685 static bool nh_res_nhge_is_uw(const struct nh_grp_entry *nhge)
1686 {
1687 return nhge->res.count_buckets < nhge->res.wants_buckets;
1688 }
1689
nh_res_table_is_balanced(const struct nh_res_table * res_table)1690 static bool nh_res_table_is_balanced(const struct nh_res_table *res_table)
1691 {
1692 return list_empty(&res_table->uw_nh_entries);
1693 }
1694
nh_res_bucket_unset_nh(struct nh_res_bucket * bucket)1695 static void nh_res_bucket_unset_nh(struct nh_res_bucket *bucket)
1696 {
1697 struct nh_grp_entry *nhge;
1698
1699 if (bucket->occupied) {
1700 nhge = nh_res_dereference(bucket->nh_entry);
1701 nhge->res.count_buckets--;
1702 bucket->occupied = false;
1703 }
1704 }
1705
nh_res_bucket_set_nh(struct nh_res_bucket * bucket,struct nh_grp_entry * nhge)1706 static void nh_res_bucket_set_nh(struct nh_res_bucket *bucket,
1707 struct nh_grp_entry *nhge)
1708 {
1709 nh_res_bucket_unset_nh(bucket);
1710
1711 bucket->occupied = true;
1712 rcu_assign_pointer(bucket->nh_entry, nhge);
1713 nhge->res.count_buckets++;
1714 }
1715
nh_res_bucket_should_migrate(struct nh_res_table * res_table,struct nh_res_bucket * bucket,unsigned long * deadline,bool * force)1716 static bool nh_res_bucket_should_migrate(struct nh_res_table *res_table,
1717 struct nh_res_bucket *bucket,
1718 unsigned long *deadline, bool *force)
1719 {
1720 unsigned long now = jiffies;
1721 struct nh_grp_entry *nhge;
1722 unsigned long idle_point;
1723
1724 if (!bucket->occupied) {
1725 /* The bucket is not occupied, its NHGE pointer is either
1726 * NULL or obsolete. We _have to_ migrate: set force.
1727 */
1728 *force = true;
1729 return true;
1730 }
1731
1732 nhge = nh_res_dereference(bucket->nh_entry);
1733
1734 /* If the bucket is populated by an underweight or balanced
1735 * nexthop, do not migrate.
1736 */
1737 if (!nh_res_nhge_is_ow(nhge))
1738 return false;
1739
1740 /* At this point we know that the bucket is populated with an
1741 * overweight nexthop. It needs to be migrated to a new nexthop if
1742 * the idle timer of unbalanced timer expired.
1743 */
1744
1745 idle_point = nh_res_bucket_idle_point(res_table, bucket, now);
1746 if (time_after_eq(now, idle_point)) {
1747 /* The bucket is idle. We _can_ migrate: unset force. */
1748 *force = false;
1749 return true;
1750 }
1751
1752 /* Unbalanced timer of 0 means "never force". */
1753 if (res_table->unbalanced_timer) {
1754 unsigned long unb_point;
1755
1756 unb_point = nh_res_table_unb_point(res_table);
1757 if (time_after(now, unb_point)) {
1758 /* The bucket is not idle, but the unbalanced timer
1759 * expired. We _can_ migrate, but set force anyway,
1760 * so that drivers know to ignore activity reports
1761 * from the HW.
1762 */
1763 *force = true;
1764 return true;
1765 }
1766
1767 nh_res_time_set_deadline(unb_point, deadline);
1768 }
1769
1770 nh_res_time_set_deadline(idle_point, deadline);
1771 return false;
1772 }
1773
nh_res_bucket_migrate(struct nh_res_table * res_table,u16 bucket_index,bool notify,bool notify_nl,bool force)1774 static bool nh_res_bucket_migrate(struct nh_res_table *res_table,
1775 u16 bucket_index, bool notify,
1776 bool notify_nl, bool force)
1777 {
1778 struct nh_res_bucket *bucket = &res_table->nh_buckets[bucket_index];
1779 struct nh_grp_entry *new_nhge;
1780 struct netlink_ext_ack extack;
1781 int err;
1782
1783 new_nhge = list_first_entry_or_null(&res_table->uw_nh_entries,
1784 struct nh_grp_entry,
1785 res.uw_nh_entry);
1786 if (WARN_ON_ONCE(!new_nhge))
1787 /* If this function is called, "bucket" is either not
1788 * occupied, or it belongs to a next hop that is
1789 * overweight. In either case, there ought to be a
1790 * corresponding underweight next hop.
1791 */
1792 return false;
1793
1794 if (notify) {
1795 struct nh_grp_entry *old_nhge;
1796
1797 old_nhge = nh_res_dereference(bucket->nh_entry);
1798 err = call_nexthop_res_bucket_notifiers(res_table->net,
1799 res_table->nhg_id,
1800 bucket_index, force,
1801 old_nhge->nh,
1802 new_nhge->nh, &extack);
1803 if (err) {
1804 pr_err_ratelimited("%s\n", extack._msg);
1805 if (!force)
1806 return false;
1807 /* It is not possible to veto a forced replacement, so
1808 * just clear the hardware flags from the nexthop
1809 * bucket to indicate to user space that this bucket is
1810 * not correctly populated in hardware.
1811 */
1812 bucket->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP);
1813 }
1814 }
1815
1816 nh_res_bucket_set_nh(bucket, new_nhge);
1817 nh_res_bucket_set_idle(res_table, bucket);
1818
1819 if (notify_nl)
1820 nexthop_bucket_notify(res_table, bucket_index);
1821
1822 if (nh_res_nhge_is_balanced(new_nhge))
1823 list_del(&new_nhge->res.uw_nh_entry);
1824 return true;
1825 }
1826
1827 #define NH_RES_UPKEEP_DW_MINIMUM_INTERVAL (HZ / 2)
1828
nh_res_table_upkeep(struct nh_res_table * res_table,bool notify,bool notify_nl)1829 static void nh_res_table_upkeep(struct nh_res_table *res_table,
1830 bool notify, bool notify_nl)
1831 {
1832 unsigned long now = jiffies;
1833 unsigned long deadline;
1834 u16 i;
1835
1836 /* Deadline is the next time that upkeep should be run. It is the
1837 * earliest time at which one of the buckets might be migrated.
1838 * Start at the most pessimistic estimate: either unbalanced_timer
1839 * from now, or if there is none, idle_timer from now. For each
1840 * encountered time point, call nh_res_time_set_deadline() to
1841 * refine the estimate.
1842 */
1843 if (res_table->unbalanced_timer)
1844 deadline = now + res_table->unbalanced_timer;
1845 else
1846 deadline = now + res_table->idle_timer;
1847
1848 for (i = 0; i < res_table->num_nh_buckets; i++) {
1849 struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
1850 bool force;
1851
1852 if (nh_res_bucket_should_migrate(res_table, bucket,
1853 &deadline, &force)) {
1854 if (!nh_res_bucket_migrate(res_table, i, notify,
1855 notify_nl, force)) {
1856 unsigned long idle_point;
1857
1858 /* A driver can override the migration
1859 * decision if the HW reports that the
1860 * bucket is actually not idle. Therefore
1861 * remark the bucket as busy again and
1862 * update the deadline.
1863 */
1864 nh_res_bucket_set_busy(bucket);
1865 idle_point = nh_res_bucket_idle_point(res_table,
1866 bucket,
1867 now);
1868 nh_res_time_set_deadline(idle_point, &deadline);
1869 }
1870 }
1871 }
1872
1873 /* If the group is still unbalanced, schedule the next upkeep to
1874 * either the deadline computed above, or the minimum deadline,
1875 * whichever comes later.
1876 */
1877 if (!nh_res_table_is_balanced(res_table)) {
1878 unsigned long now = jiffies;
1879 unsigned long min_deadline;
1880
1881 min_deadline = now + NH_RES_UPKEEP_DW_MINIMUM_INTERVAL;
1882 if (time_before(deadline, min_deadline))
1883 deadline = min_deadline;
1884
1885 queue_delayed_work(system_power_efficient_wq,
1886 &res_table->upkeep_dw, deadline - now);
1887 }
1888 }
1889
nh_res_table_upkeep_dw(struct work_struct * work)1890 static void nh_res_table_upkeep_dw(struct work_struct *work)
1891 {
1892 struct delayed_work *dw = to_delayed_work(work);
1893 struct nh_res_table *res_table;
1894
1895 res_table = container_of(dw, struct nh_res_table, upkeep_dw);
1896 nh_res_table_upkeep(res_table, true, true);
1897 }
1898
nh_res_table_cancel_upkeep(struct nh_res_table * res_table)1899 static void nh_res_table_cancel_upkeep(struct nh_res_table *res_table)
1900 {
1901 cancel_delayed_work_sync(&res_table->upkeep_dw);
1902 }
1903
nh_res_group_rebalance(struct nh_group * nhg,struct nh_res_table * res_table)1904 static void nh_res_group_rebalance(struct nh_group *nhg,
1905 struct nh_res_table *res_table)
1906 {
1907 u16 prev_upper_bound = 0;
1908 u32 total = 0;
1909 u32 w = 0;
1910 int i;
1911
1912 INIT_LIST_HEAD(&res_table->uw_nh_entries);
1913
1914 for (i = 0; i < nhg->num_nh; ++i)
1915 total += nhg->nh_entries[i].weight;
1916
1917 for (i = 0; i < nhg->num_nh; ++i) {
1918 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
1919 u16 upper_bound;
1920 u64 btw;
1921
1922 w += nhge->weight;
1923 btw = ((u64)res_table->num_nh_buckets) * w;
1924 upper_bound = DIV_ROUND_CLOSEST_ULL(btw, total);
1925 nhge->res.wants_buckets = upper_bound - prev_upper_bound;
1926 prev_upper_bound = upper_bound;
1927
1928 if (nh_res_nhge_is_uw(nhge)) {
1929 if (list_empty(&res_table->uw_nh_entries))
1930 res_table->unbalanced_since = jiffies;
1931 list_add(&nhge->res.uw_nh_entry,
1932 &res_table->uw_nh_entries);
1933 }
1934 }
1935 }
1936
1937 /* Migrate buckets in res_table so that they reference NHGE's from NHG with
1938 * the right NH ID. Set those buckets that do not have a corresponding NHGE
1939 * entry in NHG as not occupied.
1940 */
nh_res_table_migrate_buckets(struct nh_res_table * res_table,struct nh_group * nhg)1941 static void nh_res_table_migrate_buckets(struct nh_res_table *res_table,
1942 struct nh_group *nhg)
1943 {
1944 u16 i;
1945
1946 for (i = 0; i < res_table->num_nh_buckets; i++) {
1947 struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
1948 u32 id = rtnl_dereference(bucket->nh_entry)->nh->id;
1949 bool found = false;
1950 int j;
1951
1952 for (j = 0; j < nhg->num_nh; j++) {
1953 struct nh_grp_entry *nhge = &nhg->nh_entries[j];
1954
1955 if (nhge->nh->id == id) {
1956 nh_res_bucket_set_nh(bucket, nhge);
1957 found = true;
1958 break;
1959 }
1960 }
1961
1962 if (!found)
1963 nh_res_bucket_unset_nh(bucket);
1964 }
1965 }
1966
replace_nexthop_grp_res(struct nh_group * oldg,struct nh_group * newg)1967 static void replace_nexthop_grp_res(struct nh_group *oldg,
1968 struct nh_group *newg)
1969 {
1970 /* For NH group replacement, the new NHG might only have a stub
1971 * hash table with 0 buckets, because the number of buckets was not
1972 * specified. For NH removal, oldg and newg both reference the same
1973 * res_table. So in any case, in the following, we want to work
1974 * with oldg->res_table.
1975 */
1976 struct nh_res_table *old_res_table = rtnl_dereference(oldg->res_table);
1977 unsigned long prev_unbalanced_since = old_res_table->unbalanced_since;
1978 bool prev_has_uw = !list_empty(&old_res_table->uw_nh_entries);
1979
1980 nh_res_table_cancel_upkeep(old_res_table);
1981 nh_res_table_migrate_buckets(old_res_table, newg);
1982 nh_res_group_rebalance(newg, old_res_table);
1983 if (prev_has_uw && !list_empty(&old_res_table->uw_nh_entries))
1984 old_res_table->unbalanced_since = prev_unbalanced_since;
1985 nh_res_table_upkeep(old_res_table, true, false);
1986 }
1987
nh_hthr_group_rebalance(struct nh_group * nhg)1988 static void nh_hthr_group_rebalance(struct nh_group *nhg)
1989 {
1990 u32 total = 0;
1991 u32 w = 0;
1992 int i;
1993
1994 for (i = 0; i < nhg->num_nh; ++i)
1995 total += nhg->nh_entries[i].weight;
1996
1997 for (i = 0; i < nhg->num_nh; ++i) {
1998 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
1999 u32 upper_bound;
2000
2001 w += nhge->weight;
2002 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, total) - 1;
2003 atomic_set(&nhge->hthr.upper_bound, upper_bound);
2004 }
2005 }
2006
remove_nh_grp_entry(struct net * net,struct nh_grp_entry * nhge,struct nl_info * nlinfo)2007 static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge,
2008 struct nl_info *nlinfo)
2009 {
2010 struct nh_grp_entry *nhges, *new_nhges;
2011 struct nexthop *nhp = nhge->nh_parent;
2012 struct netlink_ext_ack extack;
2013 struct nexthop *nh = nhge->nh;
2014 struct nh_group *nhg, *newg;
2015 int i, j, err;
2016
2017 WARN_ON(!nh);
2018
2019 nhg = rtnl_dereference(nhp->nh_grp);
2020 newg = nhg->spare;
2021
2022 /* last entry, keep it visible and remove the parent */
2023 if (nhg->num_nh == 1) {
2024 remove_nexthop(net, nhp, nlinfo);
2025 return;
2026 }
2027
2028 newg->has_v4 = false;
2029 newg->is_multipath = nhg->is_multipath;
2030 newg->hash_threshold = nhg->hash_threshold;
2031 newg->resilient = nhg->resilient;
2032 newg->fdb_nh = nhg->fdb_nh;
2033 newg->num_nh = nhg->num_nh;
2034
2035 /* copy old entries to new except the one getting removed */
2036 nhges = nhg->nh_entries;
2037 new_nhges = newg->nh_entries;
2038 for (i = 0, j = 0; i < nhg->num_nh; ++i) {
2039 struct nh_info *nhi;
2040
2041 /* current nexthop getting removed */
2042 if (nhg->nh_entries[i].nh == nh) {
2043 newg->num_nh--;
2044 continue;
2045 }
2046
2047 nhi = rtnl_dereference(nhges[i].nh->nh_info);
2048 if (nhi->family == AF_INET)
2049 newg->has_v4 = true;
2050
2051 list_del(&nhges[i].nh_list);
2052 new_nhges[j].stats = nhges[i].stats;
2053 new_nhges[j].nh_parent = nhges[i].nh_parent;
2054 new_nhges[j].nh = nhges[i].nh;
2055 new_nhges[j].weight = nhges[i].weight;
2056 list_add(&new_nhges[j].nh_list, &new_nhges[j].nh->grp_list);
2057 j++;
2058 }
2059
2060 if (newg->hash_threshold)
2061 nh_hthr_group_rebalance(newg);
2062 else if (newg->resilient)
2063 replace_nexthop_grp_res(nhg, newg);
2064
2065 rcu_assign_pointer(nhp->nh_grp, newg);
2066
2067 list_del(&nhge->nh_list);
2068 free_percpu(nhge->stats);
2069 nexthop_put(nhge->nh);
2070
2071 /* Removal of a NH from a resilient group is notified through
2072 * bucket notifications.
2073 */
2074 if (newg->hash_threshold) {
2075 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, nhp,
2076 &extack);
2077 if (err)
2078 pr_err("%s\n", extack._msg);
2079 }
2080
2081 if (nlinfo)
2082 nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo);
2083 }
2084
remove_nexthop_from_groups(struct net * net,struct nexthop * nh,struct nl_info * nlinfo)2085 static void remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
2086 struct nl_info *nlinfo)
2087 {
2088 struct nh_grp_entry *nhge, *tmp;
2089
2090 list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list)
2091 remove_nh_grp_entry(net, nhge, nlinfo);
2092
2093 /* make sure all see the newly published array before releasing rtnl */
2094 synchronize_net();
2095 }
2096
remove_nexthop_group(struct nexthop * nh,struct nl_info * nlinfo)2097 static void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo)
2098 {
2099 struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp);
2100 struct nh_res_table *res_table;
2101 int i, num_nh = nhg->num_nh;
2102
2103 for (i = 0; i < num_nh; ++i) {
2104 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
2105
2106 if (WARN_ON(!nhge->nh))
2107 continue;
2108
2109 list_del_init(&nhge->nh_list);
2110 }
2111
2112 if (nhg->resilient) {
2113 res_table = rtnl_dereference(nhg->res_table);
2114 nh_res_table_cancel_upkeep(res_table);
2115 }
2116 }
2117
2118 /* not called for nexthop replace */
__remove_nexthop_fib(struct net * net,struct nexthop * nh)2119 static void __remove_nexthop_fib(struct net *net, struct nexthop *nh)
2120 {
2121 struct fib6_info *f6i;
2122 bool do_flush = false;
2123 struct fib_info *fi;
2124
2125 list_for_each_entry(fi, &nh->fi_list, nh_list) {
2126 fi->fib_flags |= RTNH_F_DEAD;
2127 do_flush = true;
2128 }
2129 if (do_flush)
2130 fib_flush(net);
2131
2132 spin_lock_bh(&nh->lock);
2133
2134 nh->dead = true;
2135
2136 while (!list_empty(&nh->f6i_list)) {
2137 f6i = list_first_entry(&nh->f6i_list, typeof(*f6i), nh_list);
2138
2139 /* __ip6_del_rt does a release, so do a hold here */
2140 fib6_info_hold(f6i);
2141
2142 spin_unlock_bh(&nh->lock);
2143 ipv6_stub->ip6_del_rt(net, f6i,
2144 !READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode));
2145
2146 spin_lock_bh(&nh->lock);
2147 }
2148
2149 spin_unlock_bh(&nh->lock);
2150 }
2151
__remove_nexthop(struct net * net,struct nexthop * nh,struct nl_info * nlinfo)2152 static void __remove_nexthop(struct net *net, struct nexthop *nh,
2153 struct nl_info *nlinfo)
2154 {
2155 __remove_nexthop_fib(net, nh);
2156
2157 if (nh->is_group) {
2158 remove_nexthop_group(nh, nlinfo);
2159 } else {
2160 struct nh_info *nhi;
2161
2162 nhi = rtnl_dereference(nh->nh_info);
2163 if (nhi->fib_nhc.nhc_dev)
2164 hlist_del(&nhi->dev_hash);
2165
2166 remove_nexthop_from_groups(net, nh, nlinfo);
2167 }
2168 }
2169
remove_nexthop(struct net * net,struct nexthop * nh,struct nl_info * nlinfo)2170 static void remove_nexthop(struct net *net, struct nexthop *nh,
2171 struct nl_info *nlinfo)
2172 {
2173 call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh, NULL);
2174
2175 /* remove from the tree */
2176 rb_erase(&nh->rb_node, &net->nexthop.rb_root);
2177
2178 if (nlinfo)
2179 nexthop_notify(RTM_DELNEXTHOP, nh, nlinfo);
2180
2181 __remove_nexthop(net, nh, nlinfo);
2182 nh_base_seq_inc(net);
2183
2184 nexthop_put(nh);
2185 }
2186
2187 /* if any FIB entries reference this nexthop, any dst entries
2188 * need to be regenerated
2189 */
nh_rt_cache_flush(struct net * net,struct nexthop * nh,struct nexthop * replaced_nh)2190 static void nh_rt_cache_flush(struct net *net, struct nexthop *nh,
2191 struct nexthop *replaced_nh)
2192 {
2193 struct fib6_info *f6i;
2194 struct nh_group *nhg;
2195 int i;
2196
2197 if (!list_empty(&nh->fi_list))
2198 rt_cache_flush(net);
2199
2200 list_for_each_entry(f6i, &nh->f6i_list, nh_list)
2201 ipv6_stub->fib6_update_sernum(net, f6i);
2202
2203 /* if an IPv6 group was replaced, we have to release all old
2204 * dsts to make sure all refcounts are released
2205 */
2206 if (!replaced_nh->is_group)
2207 return;
2208
2209 nhg = rtnl_dereference(replaced_nh->nh_grp);
2210 for (i = 0; i < nhg->num_nh; i++) {
2211 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
2212 struct nh_info *nhi = rtnl_dereference(nhge->nh->nh_info);
2213
2214 if (nhi->family == AF_INET6)
2215 ipv6_stub->fib6_nh_release_dsts(&nhi->fib6_nh);
2216 }
2217 }
2218
replace_nexthop_grp(struct net * net,struct nexthop * old,struct nexthop * new,const struct nh_config * cfg,struct netlink_ext_ack * extack)2219 static int replace_nexthop_grp(struct net *net, struct nexthop *old,
2220 struct nexthop *new, const struct nh_config *cfg,
2221 struct netlink_ext_ack *extack)
2222 {
2223 struct nh_res_table *tmp_table = NULL;
2224 struct nh_res_table *new_res_table;
2225 struct nh_res_table *old_res_table;
2226 struct nh_group *oldg, *newg;
2227 int i, err;
2228
2229 if (!new->is_group) {
2230 NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop.");
2231 return -EINVAL;
2232 }
2233
2234 oldg = rtnl_dereference(old->nh_grp);
2235 newg = rtnl_dereference(new->nh_grp);
2236
2237 if (newg->hash_threshold != oldg->hash_threshold) {
2238 NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with one of a different type.");
2239 return -EINVAL;
2240 }
2241
2242 if (newg->hash_threshold) {
2243 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new,
2244 extack);
2245 if (err)
2246 return err;
2247 } else if (newg->resilient) {
2248 new_res_table = rtnl_dereference(newg->res_table);
2249 old_res_table = rtnl_dereference(oldg->res_table);
2250
2251 /* Accept if num_nh_buckets was not given, but if it was
2252 * given, demand that the value be correct.
2253 */
2254 if (cfg->nh_grp_res_has_num_buckets &&
2255 cfg->nh_grp_res_num_buckets !=
2256 old_res_table->num_nh_buckets) {
2257 NL_SET_ERR_MSG(extack, "Can not change number of buckets of a resilient nexthop group.");
2258 return -EINVAL;
2259 }
2260
2261 /* Emit a pre-replace notification so that listeners could veto
2262 * a potentially unsupported configuration. Otherwise,
2263 * individual bucket replacement notifications would need to be
2264 * vetoed, which is something that should only happen if the
2265 * bucket is currently active.
2266 */
2267 err = call_nexthop_res_table_notifiers(net, new, extack);
2268 if (err)
2269 return err;
2270
2271 if (cfg->nh_grp_res_has_idle_timer)
2272 old_res_table->idle_timer = cfg->nh_grp_res_idle_timer;
2273 if (cfg->nh_grp_res_has_unbalanced_timer)
2274 old_res_table->unbalanced_timer =
2275 cfg->nh_grp_res_unbalanced_timer;
2276
2277 replace_nexthop_grp_res(oldg, newg);
2278
2279 tmp_table = new_res_table;
2280 rcu_assign_pointer(newg->res_table, old_res_table);
2281 rcu_assign_pointer(newg->spare->res_table, old_res_table);
2282 }
2283
2284 /* update parents - used by nexthop code for cleanup */
2285 for (i = 0; i < newg->num_nh; i++)
2286 newg->nh_entries[i].nh_parent = old;
2287
2288 rcu_assign_pointer(old->nh_grp, newg);
2289
2290 /* Make sure concurrent readers are not using 'oldg' anymore. */
2291 synchronize_net();
2292
2293 if (newg->resilient) {
2294 rcu_assign_pointer(oldg->res_table, tmp_table);
2295 rcu_assign_pointer(oldg->spare->res_table, tmp_table);
2296 }
2297
2298 for (i = 0; i < oldg->num_nh; i++)
2299 oldg->nh_entries[i].nh_parent = new;
2300
2301 rcu_assign_pointer(new->nh_grp, oldg);
2302
2303 return 0;
2304 }
2305
nh_group_v4_update(struct nh_group * nhg)2306 static void nh_group_v4_update(struct nh_group *nhg)
2307 {
2308 struct nh_grp_entry *nhges;
2309 bool has_v4 = false;
2310 int i;
2311
2312 nhges = nhg->nh_entries;
2313 for (i = 0; i < nhg->num_nh; i++) {
2314 struct nh_info *nhi;
2315
2316 nhi = rtnl_dereference(nhges[i].nh->nh_info);
2317 if (nhi->family == AF_INET)
2318 has_v4 = true;
2319 }
2320 nhg->has_v4 = has_v4;
2321 }
2322
replace_nexthop_single_notify_res(struct net * net,struct nh_res_table * res_table,struct nexthop * old,struct nh_info * oldi,struct nh_info * newi,struct netlink_ext_ack * extack)2323 static int replace_nexthop_single_notify_res(struct net *net,
2324 struct nh_res_table *res_table,
2325 struct nexthop *old,
2326 struct nh_info *oldi,
2327 struct nh_info *newi,
2328 struct netlink_ext_ack *extack)
2329 {
2330 u32 nhg_id = res_table->nhg_id;
2331 int err;
2332 u16 i;
2333
2334 for (i = 0; i < res_table->num_nh_buckets; i++) {
2335 struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
2336 struct nh_grp_entry *nhge;
2337
2338 nhge = rtnl_dereference(bucket->nh_entry);
2339 if (nhge->nh == old) {
2340 err = __call_nexthop_res_bucket_notifiers(net, nhg_id,
2341 i, true,
2342 oldi, newi,
2343 extack);
2344 if (err)
2345 goto err_notify;
2346 }
2347 }
2348
2349 return 0;
2350
2351 err_notify:
2352 while (i-- > 0) {
2353 struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
2354 struct nh_grp_entry *nhge;
2355
2356 nhge = rtnl_dereference(bucket->nh_entry);
2357 if (nhge->nh == old)
2358 __call_nexthop_res_bucket_notifiers(net, nhg_id, i,
2359 true, newi, oldi,
2360 extack);
2361 }
2362 return err;
2363 }
2364
replace_nexthop_single_notify(struct net * net,struct nexthop * group_nh,struct nexthop * old,struct nh_info * oldi,struct nh_info * newi,struct netlink_ext_ack * extack)2365 static int replace_nexthop_single_notify(struct net *net,
2366 struct nexthop *group_nh,
2367 struct nexthop *old,
2368 struct nh_info *oldi,
2369 struct nh_info *newi,
2370 struct netlink_ext_ack *extack)
2371 {
2372 struct nh_group *nhg = rtnl_dereference(group_nh->nh_grp);
2373 struct nh_res_table *res_table;
2374
2375 if (nhg->hash_threshold) {
2376 return call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE,
2377 group_nh, extack);
2378 } else if (nhg->resilient) {
2379 res_table = rtnl_dereference(nhg->res_table);
2380 return replace_nexthop_single_notify_res(net, res_table,
2381 old, oldi, newi,
2382 extack);
2383 }
2384
2385 return -EINVAL;
2386 }
2387
replace_nexthop_single(struct net * net,struct nexthop * old,struct nexthop * new,struct netlink_ext_ack * extack)2388 static int replace_nexthop_single(struct net *net, struct nexthop *old,
2389 struct nexthop *new,
2390 struct netlink_ext_ack *extack)
2391 {
2392 u8 old_protocol, old_nh_flags;
2393 struct nh_info *oldi, *newi;
2394 struct nh_grp_entry *nhge;
2395 int err;
2396
2397 if (new->is_group) {
2398 NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group.");
2399 return -EINVAL;
2400 }
2401
2402 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new, extack);
2403 if (err)
2404 return err;
2405
2406 /* Hardware flags were set on 'old' as 'new' is not in the red-black
2407 * tree. Therefore, inherit the flags from 'old' to 'new'.
2408 */
2409 new->nh_flags |= old->nh_flags & (RTNH_F_OFFLOAD | RTNH_F_TRAP);
2410
2411 oldi = rtnl_dereference(old->nh_info);
2412 newi = rtnl_dereference(new->nh_info);
2413
2414 newi->nh_parent = old;
2415 oldi->nh_parent = new;
2416
2417 old_protocol = old->protocol;
2418 old_nh_flags = old->nh_flags;
2419
2420 old->protocol = new->protocol;
2421 old->nh_flags = new->nh_flags;
2422
2423 rcu_assign_pointer(old->nh_info, newi);
2424 rcu_assign_pointer(new->nh_info, oldi);
2425
2426 /* Send a replace notification for all the groups using the nexthop. */
2427 list_for_each_entry(nhge, &old->grp_list, nh_list) {
2428 struct nexthop *nhp = nhge->nh_parent;
2429
2430 err = replace_nexthop_single_notify(net, nhp, old, oldi, newi,
2431 extack);
2432 if (err)
2433 goto err_notify;
2434 }
2435
2436 /* When replacing an IPv4 nexthop with an IPv6 nexthop, potentially
2437 * update IPv4 indication in all the groups using the nexthop.
2438 */
2439 if (oldi->family == AF_INET && newi->family == AF_INET6) {
2440 list_for_each_entry(nhge, &old->grp_list, nh_list) {
2441 struct nexthop *nhp = nhge->nh_parent;
2442 struct nh_group *nhg;
2443
2444 nhg = rtnl_dereference(nhp->nh_grp);
2445 nh_group_v4_update(nhg);
2446 }
2447 }
2448
2449 return 0;
2450
2451 err_notify:
2452 rcu_assign_pointer(new->nh_info, newi);
2453 rcu_assign_pointer(old->nh_info, oldi);
2454 old->nh_flags = old_nh_flags;
2455 old->protocol = old_protocol;
2456 oldi->nh_parent = old;
2457 newi->nh_parent = new;
2458 list_for_each_entry_continue_reverse(nhge, &old->grp_list, nh_list) {
2459 struct nexthop *nhp = nhge->nh_parent;
2460
2461 replace_nexthop_single_notify(net, nhp, old, newi, oldi, NULL);
2462 }
2463 call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, old, extack);
2464 return err;
2465 }
2466
__nexthop_replace_notify(struct net * net,struct nexthop * nh,struct nl_info * info)2467 static void __nexthop_replace_notify(struct net *net, struct nexthop *nh,
2468 struct nl_info *info)
2469 {
2470 struct fib6_info *f6i;
2471
2472 if (!list_empty(&nh->fi_list)) {
2473 struct fib_info *fi;
2474
2475 /* expectation is a few fib_info per nexthop and then
2476 * a lot of routes per fib_info. So mark the fib_info
2477 * and then walk the fib tables once
2478 */
2479 list_for_each_entry(fi, &nh->fi_list, nh_list)
2480 fi->nh_updated = true;
2481
2482 fib_info_notify_update(net, info);
2483
2484 list_for_each_entry(fi, &nh->fi_list, nh_list)
2485 fi->nh_updated = false;
2486 }
2487
2488 list_for_each_entry(f6i, &nh->f6i_list, nh_list)
2489 ipv6_stub->fib6_rt_update(net, f6i, info);
2490 }
2491
2492 /* send RTM_NEWROUTE with REPLACE flag set for all FIB entries
2493 * linked to this nexthop and for all groups that the nexthop
2494 * is a member of
2495 */
nexthop_replace_notify(struct net * net,struct nexthop * nh,struct nl_info * info)2496 static void nexthop_replace_notify(struct net *net, struct nexthop *nh,
2497 struct nl_info *info)
2498 {
2499 struct nh_grp_entry *nhge;
2500
2501 __nexthop_replace_notify(net, nh, info);
2502
2503 list_for_each_entry(nhge, &nh->grp_list, nh_list)
2504 __nexthop_replace_notify(net, nhge->nh_parent, info);
2505 }
2506
replace_nexthop(struct net * net,struct nexthop * old,struct nexthop * new,const struct nh_config * cfg,struct netlink_ext_ack * extack)2507 static int replace_nexthop(struct net *net, struct nexthop *old,
2508 struct nexthop *new, const struct nh_config *cfg,
2509 struct netlink_ext_ack *extack)
2510 {
2511 bool new_is_reject = false;
2512 struct nh_grp_entry *nhge;
2513 int err;
2514
2515 /* check that existing FIB entries are ok with the
2516 * new nexthop definition
2517 */
2518 err = fib_check_nh_list(old, new, extack);
2519 if (err)
2520 return err;
2521
2522 err = fib6_check_nh_list(old, new, extack);
2523 if (err)
2524 return err;
2525
2526 if (!new->is_group) {
2527 struct nh_info *nhi = rtnl_dereference(new->nh_info);
2528
2529 new_is_reject = nhi->reject_nh;
2530 }
2531
2532 list_for_each_entry(nhge, &old->grp_list, nh_list) {
2533 /* if new nexthop is a blackhole, any groups using this
2534 * nexthop cannot have more than 1 path
2535 */
2536 if (new_is_reject &&
2537 nexthop_num_path(nhge->nh_parent) > 1) {
2538 NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path");
2539 return -EINVAL;
2540 }
2541
2542 err = fib_check_nh_list(nhge->nh_parent, new, extack);
2543 if (err)
2544 return err;
2545
2546 err = fib6_check_nh_list(nhge->nh_parent, new, extack);
2547 if (err)
2548 return err;
2549 }
2550
2551 if (old->is_group)
2552 err = replace_nexthop_grp(net, old, new, cfg, extack);
2553 else
2554 err = replace_nexthop_single(net, old, new, extack);
2555
2556 if (!err) {
2557 nh_rt_cache_flush(net, old, new);
2558
2559 __remove_nexthop(net, new, NULL);
2560 nexthop_put(new);
2561 }
2562
2563 return err;
2564 }
2565
2566 /* called with rtnl_lock held */
insert_nexthop(struct net * net,struct nexthop * new_nh,struct nh_config * cfg,struct netlink_ext_ack * extack)2567 static int insert_nexthop(struct net *net, struct nexthop *new_nh,
2568 struct nh_config *cfg, struct netlink_ext_ack *extack)
2569 {
2570 struct rb_node **pp, *parent = NULL, *next;
2571 struct rb_root *root = &net->nexthop.rb_root;
2572 bool replace = !!(cfg->nlflags & NLM_F_REPLACE);
2573 bool create = !!(cfg->nlflags & NLM_F_CREATE);
2574 u32 new_id = new_nh->id;
2575 int replace_notify = 0;
2576 int rc = -EEXIST;
2577
2578 pp = &root->rb_node;
2579 while (1) {
2580 struct nexthop *nh;
2581
2582 next = *pp;
2583 if (!next)
2584 break;
2585
2586 parent = next;
2587
2588 nh = rb_entry(parent, struct nexthop, rb_node);
2589 if (new_id < nh->id) {
2590 pp = &next->rb_left;
2591 } else if (new_id > nh->id) {
2592 pp = &next->rb_right;
2593 } else if (replace) {
2594 rc = replace_nexthop(net, nh, new_nh, cfg, extack);
2595 if (!rc) {
2596 new_nh = nh; /* send notification with old nh */
2597 replace_notify = 1;
2598 }
2599 goto out;
2600 } else {
2601 /* id already exists and not a replace */
2602 goto out;
2603 }
2604 }
2605
2606 if (replace && !create) {
2607 NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists");
2608 rc = -ENOENT;
2609 goto out;
2610 }
2611
2612 if (new_nh->is_group) {
2613 struct nh_group *nhg = rtnl_dereference(new_nh->nh_grp);
2614 struct nh_res_table *res_table;
2615
2616 if (nhg->resilient) {
2617 res_table = rtnl_dereference(nhg->res_table);
2618
2619 /* Not passing the number of buckets is OK when
2620 * replacing, but not when creating a new group.
2621 */
2622 if (!cfg->nh_grp_res_has_num_buckets) {
2623 NL_SET_ERR_MSG(extack, "Number of buckets not specified for nexthop group insertion");
2624 rc = -EINVAL;
2625 goto out;
2626 }
2627
2628 nh_res_group_rebalance(nhg, res_table);
2629
2630 /* Do not send bucket notifications, we do full
2631 * notification below.
2632 */
2633 nh_res_table_upkeep(res_table, false, false);
2634 }
2635 }
2636
2637 rb_link_node_rcu(&new_nh->rb_node, parent, pp);
2638 rb_insert_color(&new_nh->rb_node, root);
2639
2640 /* The initial insertion is a full notification for hash-threshold as
2641 * well as resilient groups.
2642 */
2643 rc = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new_nh, extack);
2644 if (rc)
2645 rb_erase(&new_nh->rb_node, &net->nexthop.rb_root);
2646
2647 out:
2648 if (!rc) {
2649 nh_base_seq_inc(net);
2650 nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo);
2651 if (replace_notify &&
2652 READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode))
2653 nexthop_replace_notify(net, new_nh, &cfg->nlinfo);
2654 }
2655
2656 return rc;
2657 }
2658
2659 /* rtnl */
2660 /* remove all nexthops tied to a device being deleted */
nexthop_flush_dev(struct net_device * dev,unsigned long event)2661 static void nexthop_flush_dev(struct net_device *dev, unsigned long event)
2662 {
2663 unsigned int hash = nh_dev_hashfn(dev->ifindex);
2664 struct net *net = dev_net(dev);
2665 struct hlist_head *head = &net->nexthop.devhash[hash];
2666 struct hlist_node *n;
2667 struct nh_info *nhi;
2668
2669 hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
2670 if (nhi->fib_nhc.nhc_dev != dev)
2671 continue;
2672
2673 if (nhi->reject_nh &&
2674 (event == NETDEV_DOWN || event == NETDEV_CHANGE))
2675 continue;
2676
2677 remove_nexthop(net, nhi->nh_parent, NULL);
2678 }
2679 }
2680
2681 /* rtnl; called when net namespace is deleted */
flush_all_nexthops(struct net * net)2682 static void flush_all_nexthops(struct net *net)
2683 {
2684 struct rb_root *root = &net->nexthop.rb_root;
2685 struct rb_node *node;
2686 struct nexthop *nh;
2687
2688 while ((node = rb_first(root))) {
2689 nh = rb_entry(node, struct nexthop, rb_node);
2690 remove_nexthop(net, nh, NULL);
2691 cond_resched();
2692 }
2693 }
2694
nexthop_create_group(struct net * net,struct nh_config * cfg)2695 static struct nexthop *nexthop_create_group(struct net *net,
2696 struct nh_config *cfg)
2697 {
2698 struct nlattr *grps_attr = cfg->nh_grp;
2699 struct nexthop_grp *entry = nla_data(grps_attr);
2700 u16 num_nh = nla_len(grps_attr) / sizeof(*entry);
2701 struct nh_group *nhg;
2702 struct nexthop *nh;
2703 int err;
2704 int i;
2705
2706 nh = nexthop_alloc();
2707 if (!nh)
2708 return ERR_PTR(-ENOMEM);
2709
2710 nh->is_group = 1;
2711
2712 nhg = nexthop_grp_alloc(num_nh);
2713 if (!nhg) {
2714 kfree(nh);
2715 return ERR_PTR(-ENOMEM);
2716 }
2717
2718 /* spare group used for removals */
2719 nhg->spare = nexthop_grp_alloc(num_nh);
2720 if (!nhg->spare) {
2721 kfree(nhg);
2722 kfree(nh);
2723 return ERR_PTR(-ENOMEM);
2724 }
2725 nhg->spare->spare = nhg;
2726
2727 for (i = 0; i < nhg->num_nh; ++i) {
2728 struct nexthop *nhe;
2729 struct nh_info *nhi;
2730
2731 nhe = nexthop_find_by_id(net, entry[i].id);
2732 if (!nexthop_get(nhe)) {
2733 err = -ENOENT;
2734 goto out_no_nh;
2735 }
2736
2737 nhi = rtnl_dereference(nhe->nh_info);
2738 if (nhi->family == AF_INET)
2739 nhg->has_v4 = true;
2740
2741 nhg->nh_entries[i].stats =
2742 netdev_alloc_pcpu_stats(struct nh_grp_entry_stats);
2743 if (!nhg->nh_entries[i].stats) {
2744 err = -ENOMEM;
2745 nexthop_put(nhe);
2746 goto out_no_nh;
2747 }
2748 nhg->nh_entries[i].nh = nhe;
2749 nhg->nh_entries[i].weight = nexthop_grp_weight(&entry[i]);
2750
2751 list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list);
2752 nhg->nh_entries[i].nh_parent = nh;
2753 }
2754
2755 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) {
2756 nhg->hash_threshold = 1;
2757 nhg->is_multipath = true;
2758 } else if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_RES) {
2759 struct nh_res_table *res_table;
2760
2761 res_table = nexthop_res_table_alloc(net, cfg->nh_id, cfg);
2762 if (!res_table) {
2763 err = -ENOMEM;
2764 goto out_no_nh;
2765 }
2766
2767 rcu_assign_pointer(nhg->spare->res_table, res_table);
2768 rcu_assign_pointer(nhg->res_table, res_table);
2769 nhg->resilient = true;
2770 nhg->is_multipath = true;
2771 }
2772
2773 WARN_ON_ONCE(nhg->hash_threshold + nhg->resilient != 1);
2774
2775 if (nhg->hash_threshold)
2776 nh_hthr_group_rebalance(nhg);
2777
2778 if (cfg->nh_fdb)
2779 nhg->fdb_nh = 1;
2780
2781 if (cfg->nh_hw_stats)
2782 nhg->hw_stats = true;
2783
2784 rcu_assign_pointer(nh->nh_grp, nhg);
2785
2786 return nh;
2787
2788 out_no_nh:
2789 for (i--; i >= 0; --i) {
2790 list_del(&nhg->nh_entries[i].nh_list);
2791 free_percpu(nhg->nh_entries[i].stats);
2792 nexthop_put(nhg->nh_entries[i].nh);
2793 }
2794
2795 kfree(nhg->spare);
2796 kfree(nhg);
2797 kfree(nh);
2798
2799 return ERR_PTR(err);
2800 }
2801
nh_create_ipv4(struct net * net,struct nexthop * nh,struct nh_info * nhi,struct nh_config * cfg,struct netlink_ext_ack * extack)2802 static int nh_create_ipv4(struct net *net, struct nexthop *nh,
2803 struct nh_info *nhi, struct nh_config *cfg,
2804 struct netlink_ext_ack *extack)
2805 {
2806 struct fib_nh *fib_nh = &nhi->fib_nh;
2807 struct fib_config fib_cfg = {
2808 .fc_oif = cfg->nh_ifindex,
2809 .fc_gw4 = cfg->gw.ipv4,
2810 .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0,
2811 .fc_flags = cfg->nh_flags,
2812 .fc_nlinfo = cfg->nlinfo,
2813 .fc_encap = cfg->nh_encap,
2814 .fc_encap_type = cfg->nh_encap_type,
2815 };
2816 u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN);
2817 int err;
2818
2819 err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack);
2820 if (err) {
2821 fib_nh_release(net, fib_nh);
2822 goto out;
2823 }
2824
2825 if (nhi->fdb_nh)
2826 goto out;
2827
2828 /* sets nh_dev if successful */
2829 err = fib_check_nh(net, fib_nh, tb_id, 0, extack);
2830 if (!err) {
2831 nh->nh_flags = fib_nh->fib_nh_flags;
2832 fib_info_update_nhc_saddr(net, &fib_nh->nh_common,
2833 !fib_nh->fib_nh_scope ? 0 : fib_nh->fib_nh_scope - 1);
2834 } else {
2835 fib_nh_release(net, fib_nh);
2836 }
2837 out:
2838 return err;
2839 }
2840
nh_create_ipv6(struct net * net,struct nexthop * nh,struct nh_info * nhi,struct nh_config * cfg,struct netlink_ext_ack * extack)2841 static int nh_create_ipv6(struct net *net, struct nexthop *nh,
2842 struct nh_info *nhi, struct nh_config *cfg,
2843 struct netlink_ext_ack *extack)
2844 {
2845 struct fib6_nh *fib6_nh = &nhi->fib6_nh;
2846 struct fib6_config fib6_cfg = {
2847 .fc_table = l3mdev_fib_table(cfg->dev),
2848 .fc_ifindex = cfg->nh_ifindex,
2849 .fc_gateway = cfg->gw.ipv6,
2850 .fc_flags = cfg->nh_flags,
2851 .fc_nlinfo = cfg->nlinfo,
2852 .fc_encap = cfg->nh_encap,
2853 .fc_encap_type = cfg->nh_encap_type,
2854 .fc_is_fdb = cfg->nh_fdb,
2855 };
2856 int err;
2857
2858 if (!ipv6_addr_any(&cfg->gw.ipv6))
2859 fib6_cfg.fc_flags |= RTF_GATEWAY;
2860
2861 /* sets nh_dev if successful */
2862 err = ipv6_stub->fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL,
2863 extack);
2864 if (err) {
2865 /* IPv6 is not enabled, don't call fib6_nh_release */
2866 if (err == -EAFNOSUPPORT)
2867 goto out;
2868 ipv6_stub->fib6_nh_release(fib6_nh);
2869 } else {
2870 nh->nh_flags = fib6_nh->fib_nh_flags;
2871 }
2872 out:
2873 return err;
2874 }
2875
nexthop_create(struct net * net,struct nh_config * cfg,struct netlink_ext_ack * extack)2876 static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg,
2877 struct netlink_ext_ack *extack)
2878 {
2879 struct nh_info *nhi;
2880 struct nexthop *nh;
2881 int err = 0;
2882
2883 nh = nexthop_alloc();
2884 if (!nh)
2885 return ERR_PTR(-ENOMEM);
2886
2887 nhi = kzalloc(sizeof(*nhi), GFP_KERNEL);
2888 if (!nhi) {
2889 kfree(nh);
2890 return ERR_PTR(-ENOMEM);
2891 }
2892
2893 nh->nh_flags = cfg->nh_flags;
2894 nh->net = net;
2895
2896 nhi->nh_parent = nh;
2897 nhi->family = cfg->nh_family;
2898 nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK;
2899
2900 if (cfg->nh_fdb)
2901 nhi->fdb_nh = 1;
2902
2903 if (cfg->nh_blackhole) {
2904 nhi->reject_nh = 1;
2905 cfg->nh_ifindex = net->loopback_dev->ifindex;
2906 }
2907
2908 switch (cfg->nh_family) {
2909 case AF_INET:
2910 err = nh_create_ipv4(net, nh, nhi, cfg, extack);
2911 break;
2912 case AF_INET6:
2913 err = nh_create_ipv6(net, nh, nhi, cfg, extack);
2914 break;
2915 }
2916
2917 if (err) {
2918 kfree(nhi);
2919 kfree(nh);
2920 return ERR_PTR(err);
2921 }
2922
2923 /* add the entry to the device based hash */
2924 if (!nhi->fdb_nh)
2925 nexthop_devhash_add(net, nhi);
2926
2927 rcu_assign_pointer(nh->nh_info, nhi);
2928
2929 return nh;
2930 }
2931
2932 /* called with rtnl lock held */
nexthop_add(struct net * net,struct nh_config * cfg,struct netlink_ext_ack * extack)2933 static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg,
2934 struct netlink_ext_ack *extack)
2935 {
2936 struct nexthop *nh;
2937 int err;
2938
2939 if (!cfg->nh_id) {
2940 cfg->nh_id = nh_find_unused_id(net);
2941 if (!cfg->nh_id) {
2942 NL_SET_ERR_MSG(extack, "No unused id");
2943 return ERR_PTR(-EINVAL);
2944 }
2945 }
2946
2947 if (cfg->nh_grp)
2948 nh = nexthop_create_group(net, cfg);
2949 else
2950 nh = nexthop_create(net, cfg, extack);
2951
2952 if (IS_ERR(nh))
2953 return nh;
2954
2955 refcount_set(&nh->refcnt, 1);
2956 nh->id = cfg->nh_id;
2957 nh->protocol = cfg->nh_protocol;
2958 nh->net = net;
2959
2960 err = insert_nexthop(net, nh, cfg, extack);
2961 if (err) {
2962 __remove_nexthop(net, nh, NULL);
2963 nexthop_put(nh);
2964 nh = ERR_PTR(err);
2965 }
2966
2967 return nh;
2968 }
2969
rtm_nh_get_timer(struct nlattr * attr,unsigned long fallback,unsigned long * timer_p,bool * has_p,struct netlink_ext_ack * extack)2970 static int rtm_nh_get_timer(struct nlattr *attr, unsigned long fallback,
2971 unsigned long *timer_p, bool *has_p,
2972 struct netlink_ext_ack *extack)
2973 {
2974 unsigned long timer;
2975 u32 value;
2976
2977 if (!attr) {
2978 *timer_p = fallback;
2979 *has_p = false;
2980 return 0;
2981 }
2982
2983 value = nla_get_u32(attr);
2984 timer = clock_t_to_jiffies(value);
2985 if (timer == ~0UL) {
2986 NL_SET_ERR_MSG(extack, "Timer value too large");
2987 return -EINVAL;
2988 }
2989
2990 *timer_p = timer;
2991 *has_p = true;
2992 return 0;
2993 }
2994
rtm_to_nh_config_grp_res(struct nlattr * res,struct nh_config * cfg,struct netlink_ext_ack * extack)2995 static int rtm_to_nh_config_grp_res(struct nlattr *res, struct nh_config *cfg,
2996 struct netlink_ext_ack *extack)
2997 {
2998 struct nlattr *tb[ARRAY_SIZE(rtm_nh_res_policy_new)] = {};
2999 int err;
3000
3001 if (res) {
3002 err = nla_parse_nested(tb,
3003 ARRAY_SIZE(rtm_nh_res_policy_new) - 1,
3004 res, rtm_nh_res_policy_new, extack);
3005 if (err < 0)
3006 return err;
3007 }
3008
3009 if (tb[NHA_RES_GROUP_BUCKETS]) {
3010 cfg->nh_grp_res_num_buckets =
3011 nla_get_u16(tb[NHA_RES_GROUP_BUCKETS]);
3012 cfg->nh_grp_res_has_num_buckets = true;
3013 if (!cfg->nh_grp_res_num_buckets) {
3014 NL_SET_ERR_MSG(extack, "Number of buckets needs to be non-0");
3015 return -EINVAL;
3016 }
3017 }
3018
3019 err = rtm_nh_get_timer(tb[NHA_RES_GROUP_IDLE_TIMER],
3020 NH_RES_DEFAULT_IDLE_TIMER,
3021 &cfg->nh_grp_res_idle_timer,
3022 &cfg->nh_grp_res_has_idle_timer,
3023 extack);
3024 if (err)
3025 return err;
3026
3027 return rtm_nh_get_timer(tb[NHA_RES_GROUP_UNBALANCED_TIMER],
3028 NH_RES_DEFAULT_UNBALANCED_TIMER,
3029 &cfg->nh_grp_res_unbalanced_timer,
3030 &cfg->nh_grp_res_has_unbalanced_timer,
3031 extack);
3032 }
3033
rtm_to_nh_config(struct net * net,struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** tb,struct nh_config * cfg,struct netlink_ext_ack * extack)3034 static int rtm_to_nh_config(struct net *net, struct sk_buff *skb,
3035 struct nlmsghdr *nlh, struct nlattr **tb,
3036 struct nh_config *cfg,
3037 struct netlink_ext_ack *extack)
3038 {
3039 struct nhmsg *nhm = nlmsg_data(nlh);
3040 int err;
3041
3042 err = -EINVAL;
3043 if (nhm->resvd || nhm->nh_scope) {
3044 NL_SET_ERR_MSG(extack, "Invalid values in ancillary header");
3045 goto out;
3046 }
3047 if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) {
3048 NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header");
3049 goto out;
3050 }
3051
3052 switch (nhm->nh_family) {
3053 case AF_INET:
3054 case AF_INET6:
3055 break;
3056 case AF_UNSPEC:
3057 if (tb[NHA_GROUP])
3058 break;
3059 fallthrough;
3060 default:
3061 NL_SET_ERR_MSG(extack, "Invalid address family");
3062 goto out;
3063 }
3064
3065 memset(cfg, 0, sizeof(*cfg));
3066 cfg->nlflags = nlh->nlmsg_flags;
3067 cfg->nlinfo.portid = NETLINK_CB(skb).portid;
3068 cfg->nlinfo.nlh = nlh;
3069 cfg->nlinfo.nl_net = net;
3070
3071 cfg->nh_family = nhm->nh_family;
3072 cfg->nh_protocol = nhm->nh_protocol;
3073 cfg->nh_flags = nhm->nh_flags;
3074
3075 if (tb[NHA_ID])
3076 cfg->nh_id = nla_get_u32(tb[NHA_ID]);
3077
3078 if (tb[NHA_FDB]) {
3079 if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] ||
3080 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) {
3081 NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole");
3082 goto out;
3083 }
3084 if (nhm->nh_flags) {
3085 NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header");
3086 goto out;
3087 }
3088 cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]);
3089 }
3090
3091 if (tb[NHA_GROUP]) {
3092 if (nhm->nh_family != AF_UNSPEC) {
3093 NL_SET_ERR_MSG(extack, "Invalid family for group");
3094 goto out;
3095 }
3096 cfg->nh_grp = tb[NHA_GROUP];
3097
3098 cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH;
3099 if (tb[NHA_GROUP_TYPE])
3100 cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]);
3101
3102 if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) {
3103 NL_SET_ERR_MSG(extack, "Invalid group type");
3104 goto out;
3105 }
3106
3107 err = nh_check_attr_group(net, tb, ARRAY_SIZE(rtm_nh_policy_new),
3108 cfg->nh_grp_type, extack);
3109 if (err)
3110 goto out;
3111
3112 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_RES)
3113 err = rtm_to_nh_config_grp_res(tb[NHA_RES_GROUP],
3114 cfg, extack);
3115
3116 if (tb[NHA_HW_STATS_ENABLE])
3117 cfg->nh_hw_stats = nla_get_u32(tb[NHA_HW_STATS_ENABLE]);
3118
3119 /* no other attributes should be set */
3120 goto out;
3121 }
3122
3123 if (tb[NHA_BLACKHOLE]) {
3124 if (tb[NHA_GATEWAY] || tb[NHA_OIF] ||
3125 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) {
3126 NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb");
3127 goto out;
3128 }
3129
3130 cfg->nh_blackhole = 1;
3131 err = 0;
3132 goto out;
3133 }
3134
3135 if (!cfg->nh_fdb && !tb[NHA_OIF]) {
3136 NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops");
3137 goto out;
3138 }
3139
3140 err = -EINVAL;
3141 if (tb[NHA_GATEWAY]) {
3142 struct nlattr *gwa = tb[NHA_GATEWAY];
3143
3144 switch (cfg->nh_family) {
3145 case AF_INET:
3146 if (nla_len(gwa) != sizeof(u32)) {
3147 NL_SET_ERR_MSG(extack, "Invalid gateway");
3148 goto out;
3149 }
3150 cfg->gw.ipv4 = nla_get_be32(gwa);
3151 break;
3152 case AF_INET6:
3153 if (nla_len(gwa) != sizeof(struct in6_addr)) {
3154 NL_SET_ERR_MSG(extack, "Invalid gateway");
3155 goto out;
3156 }
3157 cfg->gw.ipv6 = nla_get_in6_addr(gwa);
3158 break;
3159 default:
3160 NL_SET_ERR_MSG(extack,
3161 "Unknown address family for gateway");
3162 goto out;
3163 }
3164 } else {
3165 /* device only nexthop (no gateway) */
3166 if (cfg->nh_flags & RTNH_F_ONLINK) {
3167 NL_SET_ERR_MSG(extack,
3168 "ONLINK flag can not be set for nexthop without a gateway");
3169 goto out;
3170 }
3171 }
3172
3173 if (tb[NHA_ENCAP]) {
3174 cfg->nh_encap = tb[NHA_ENCAP];
3175
3176 if (!tb[NHA_ENCAP_TYPE]) {
3177 NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing");
3178 goto out;
3179 }
3180
3181 cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]);
3182 err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack);
3183 if (err < 0)
3184 goto out;
3185
3186 } else if (tb[NHA_ENCAP_TYPE]) {
3187 NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing");
3188 goto out;
3189 }
3190
3191 if (tb[NHA_HW_STATS_ENABLE]) {
3192 NL_SET_ERR_MSG(extack, "Cannot enable nexthop hardware statistics for non-group nexthops");
3193 goto out;
3194 }
3195
3196 err = 0;
3197 out:
3198 return err;
3199 }
3200
rtm_to_nh_config_rtnl(struct net * net,struct nlattr ** tb,struct nh_config * cfg,struct netlink_ext_ack * extack)3201 static int rtm_to_nh_config_rtnl(struct net *net, struct nlattr **tb,
3202 struct nh_config *cfg,
3203 struct netlink_ext_ack *extack)
3204 {
3205 if (tb[NHA_GROUP])
3206 return nh_check_attr_group_rtnl(net, tb, extack);
3207
3208 if (tb[NHA_OIF]) {
3209 cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]);
3210 if (cfg->nh_ifindex)
3211 cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex);
3212
3213 if (!cfg->dev) {
3214 NL_SET_ERR_MSG(extack, "Invalid device index");
3215 return -EINVAL;
3216 }
3217
3218 if (!(cfg->dev->flags & IFF_UP)) {
3219 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3220 return -ENETDOWN;
3221 }
3222
3223 if (!netif_carrier_ok(cfg->dev)) {
3224 NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down");
3225 return -ENETDOWN;
3226 }
3227 }
3228
3229 return 0;
3230 }
3231
3232 /* rtnl */
rtm_new_nexthop(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3233 static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
3234 struct netlink_ext_ack *extack)
3235 {
3236 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_new)];
3237 struct net *net = sock_net(skb->sk);
3238 struct nh_config cfg;
3239 struct nexthop *nh;
3240 int err;
3241
3242 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
3243 ARRAY_SIZE(rtm_nh_policy_new) - 1,
3244 rtm_nh_policy_new, extack);
3245 if (err < 0)
3246 goto out;
3247
3248 err = rtm_to_nh_config(net, skb, nlh, tb, &cfg, extack);
3249 if (err)
3250 goto out;
3251
3252 if (cfg.nlflags & NLM_F_REPLACE && !cfg.nh_id) {
3253 NL_SET_ERR_MSG(extack, "Replace requires nexthop id");
3254 err = -EINVAL;
3255 goto out;
3256 }
3257
3258 rtnl_net_lock(net);
3259
3260 err = rtm_to_nh_config_rtnl(net, tb, &cfg, extack);
3261 if (err)
3262 goto unlock;
3263
3264 nh = nexthop_add(net, &cfg, extack);
3265 if (IS_ERR(nh))
3266 err = PTR_ERR(nh);
3267
3268 unlock:
3269 rtnl_net_unlock(net);
3270 out:
3271 return err;
3272 }
3273
nh_valid_get_del_req(const struct nlmsghdr * nlh,struct nlattr ** tb,u32 * id,u32 * op_flags,struct netlink_ext_ack * extack)3274 static int nh_valid_get_del_req(const struct nlmsghdr *nlh,
3275 struct nlattr **tb, u32 *id, u32 *op_flags,
3276 struct netlink_ext_ack *extack)
3277 {
3278 struct nhmsg *nhm = nlmsg_data(nlh);
3279
3280 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
3281 NL_SET_ERR_MSG(extack, "Invalid values in header");
3282 return -EINVAL;
3283 }
3284
3285 if (!tb[NHA_ID]) {
3286 NL_SET_ERR_MSG(extack, "Nexthop id is missing");
3287 return -EINVAL;
3288 }
3289
3290 *id = nla_get_u32(tb[NHA_ID]);
3291 if (!(*id)) {
3292 NL_SET_ERR_MSG(extack, "Invalid nexthop id");
3293 return -EINVAL;
3294 }
3295
3296 if (op_flags)
3297 *op_flags = nla_get_u32_default(tb[NHA_OP_FLAGS], 0);
3298
3299 return 0;
3300 }
3301
3302 /* rtnl */
rtm_del_nexthop(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3303 static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
3304 struct netlink_ext_ack *extack)
3305 {
3306 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_del)];
3307 struct net *net = sock_net(skb->sk);
3308 struct nl_info nlinfo = {
3309 .nlh = nlh,
3310 .nl_net = net,
3311 .portid = NETLINK_CB(skb).portid,
3312 };
3313 struct nexthop *nh;
3314 int err;
3315 u32 id;
3316
3317 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
3318 ARRAY_SIZE(rtm_nh_policy_del) - 1, rtm_nh_policy_del,
3319 extack);
3320 if (err < 0)
3321 return err;
3322
3323 err = nh_valid_get_del_req(nlh, tb, &id, NULL, extack);
3324 if (err)
3325 return err;
3326
3327 rtnl_net_lock(net);
3328
3329 nh = nexthop_find_by_id(net, id);
3330 if (nh)
3331 remove_nexthop(net, nh, &nlinfo);
3332 else
3333 err = -ENOENT;
3334
3335 rtnl_net_unlock(net);
3336
3337 return err;
3338 }
3339
3340 /* rtnl */
rtm_get_nexthop(struct sk_buff * in_skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3341 static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh,
3342 struct netlink_ext_ack *extack)
3343 {
3344 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get)];
3345 struct net *net = sock_net(in_skb->sk);
3346 struct sk_buff *skb = NULL;
3347 struct nexthop *nh;
3348 u32 op_flags;
3349 int err;
3350 u32 id;
3351
3352 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
3353 ARRAY_SIZE(rtm_nh_policy_get) - 1, rtm_nh_policy_get,
3354 extack);
3355 if (err < 0)
3356 return err;
3357
3358 err = nh_valid_get_del_req(nlh, tb, &id, &op_flags, extack);
3359 if (err)
3360 return err;
3361
3362 err = -ENOBUFS;
3363 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3364 if (!skb)
3365 goto out;
3366
3367 err = -ENOENT;
3368 nh = nexthop_find_by_id(net, id);
3369 if (!nh)
3370 goto errout_free;
3371
3372 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid,
3373 nlh->nlmsg_seq, 0, op_flags);
3374 if (err < 0) {
3375 WARN_ON(err == -EMSGSIZE);
3376 goto errout_free;
3377 }
3378
3379 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
3380 out:
3381 return err;
3382 errout_free:
3383 kfree_skb(skb);
3384 goto out;
3385 }
3386
3387 struct nh_dump_filter {
3388 u32 nh_id;
3389 int dev_idx;
3390 int master_idx;
3391 bool group_filter;
3392 bool fdb_filter;
3393 u32 res_bucket_nh_id;
3394 u32 op_flags;
3395 };
3396
nh_dump_filtered(struct nexthop * nh,struct nh_dump_filter * filter,u8 family)3397 static bool nh_dump_filtered(struct nexthop *nh,
3398 struct nh_dump_filter *filter, u8 family)
3399 {
3400 const struct net_device *dev;
3401 const struct nh_info *nhi;
3402
3403 if (filter->group_filter && !nh->is_group)
3404 return true;
3405
3406 if (!filter->dev_idx && !filter->master_idx && !family)
3407 return false;
3408
3409 if (nh->is_group)
3410 return true;
3411
3412 nhi = rtnl_dereference(nh->nh_info);
3413 if (family && nhi->family != family)
3414 return true;
3415
3416 dev = nhi->fib_nhc.nhc_dev;
3417 if (filter->dev_idx && (!dev || dev->ifindex != filter->dev_idx))
3418 return true;
3419
3420 if (filter->master_idx) {
3421 struct net_device *master;
3422
3423 if (!dev)
3424 return true;
3425
3426 master = netdev_master_upper_dev_get((struct net_device *)dev);
3427 if (!master || master->ifindex != filter->master_idx)
3428 return true;
3429 }
3430
3431 return false;
3432 }
3433
__nh_valid_dump_req(const struct nlmsghdr * nlh,struct nlattr ** tb,struct nh_dump_filter * filter,struct netlink_ext_ack * extack)3434 static int __nh_valid_dump_req(const struct nlmsghdr *nlh, struct nlattr **tb,
3435 struct nh_dump_filter *filter,
3436 struct netlink_ext_ack *extack)
3437 {
3438 struct nhmsg *nhm;
3439 u32 idx;
3440
3441 if (tb[NHA_OIF]) {
3442 idx = nla_get_u32(tb[NHA_OIF]);
3443 if (idx > INT_MAX) {
3444 NL_SET_ERR_MSG(extack, "Invalid device index");
3445 return -EINVAL;
3446 }
3447 filter->dev_idx = idx;
3448 }
3449 if (tb[NHA_MASTER]) {
3450 idx = nla_get_u32(tb[NHA_MASTER]);
3451 if (idx > INT_MAX) {
3452 NL_SET_ERR_MSG(extack, "Invalid master device index");
3453 return -EINVAL;
3454 }
3455 filter->master_idx = idx;
3456 }
3457 filter->group_filter = nla_get_flag(tb[NHA_GROUPS]);
3458 filter->fdb_filter = nla_get_flag(tb[NHA_FDB]);
3459
3460 nhm = nlmsg_data(nlh);
3461 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
3462 NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request");
3463 return -EINVAL;
3464 }
3465
3466 return 0;
3467 }
3468
nh_valid_dump_req(const struct nlmsghdr * nlh,struct nh_dump_filter * filter,struct netlink_callback * cb)3469 static int nh_valid_dump_req(const struct nlmsghdr *nlh,
3470 struct nh_dump_filter *filter,
3471 struct netlink_callback *cb)
3472 {
3473 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_dump)];
3474 int err;
3475
3476 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
3477 ARRAY_SIZE(rtm_nh_policy_dump) - 1,
3478 rtm_nh_policy_dump, cb->extack);
3479 if (err < 0)
3480 return err;
3481
3482 filter->op_flags = nla_get_u32_default(tb[NHA_OP_FLAGS], 0);
3483
3484 return __nh_valid_dump_req(nlh, tb, filter, cb->extack);
3485 }
3486
3487 struct rtm_dump_nh_ctx {
3488 u32 idx;
3489 };
3490
3491 static struct rtm_dump_nh_ctx *
rtm_dump_nh_ctx(struct netlink_callback * cb)3492 rtm_dump_nh_ctx(struct netlink_callback *cb)
3493 {
3494 struct rtm_dump_nh_ctx *ctx = (void *)cb->ctx;
3495
3496 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
3497 return ctx;
3498 }
3499
rtm_dump_walk_nexthops(struct sk_buff * skb,struct netlink_callback * cb,struct rb_root * root,struct rtm_dump_nh_ctx * ctx,int (* nh_cb)(struct sk_buff * skb,struct netlink_callback * cb,struct nexthop * nh,void * data),void * data)3500 static int rtm_dump_walk_nexthops(struct sk_buff *skb,
3501 struct netlink_callback *cb,
3502 struct rb_root *root,
3503 struct rtm_dump_nh_ctx *ctx,
3504 int (*nh_cb)(struct sk_buff *skb,
3505 struct netlink_callback *cb,
3506 struct nexthop *nh, void *data),
3507 void *data)
3508 {
3509 struct rb_node *node;
3510 int s_idx;
3511 int err;
3512
3513 s_idx = ctx->idx;
3514 for (node = rb_first(root); node; node = rb_next(node)) {
3515 struct nexthop *nh;
3516
3517 nh = rb_entry(node, struct nexthop, rb_node);
3518 if (nh->id < s_idx)
3519 continue;
3520
3521 ctx->idx = nh->id;
3522 err = nh_cb(skb, cb, nh, data);
3523 if (err)
3524 return err;
3525 }
3526
3527 return 0;
3528 }
3529
rtm_dump_nexthop_cb(struct sk_buff * skb,struct netlink_callback * cb,struct nexthop * nh,void * data)3530 static int rtm_dump_nexthop_cb(struct sk_buff *skb, struct netlink_callback *cb,
3531 struct nexthop *nh, void *data)
3532 {
3533 struct nhmsg *nhm = nlmsg_data(cb->nlh);
3534 struct nh_dump_filter *filter = data;
3535
3536 if (nh_dump_filtered(nh, filter, nhm->nh_family))
3537 return 0;
3538
3539 return nh_fill_node(skb, nh, RTM_NEWNEXTHOP,
3540 NETLINK_CB(cb->skb).portid,
3541 cb->nlh->nlmsg_seq, NLM_F_MULTI, filter->op_flags);
3542 }
3543
3544 /* rtnl */
rtm_dump_nexthop(struct sk_buff * skb,struct netlink_callback * cb)3545 static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb)
3546 {
3547 struct rtm_dump_nh_ctx *ctx = rtm_dump_nh_ctx(cb);
3548 struct net *net = sock_net(skb->sk);
3549 struct rb_root *root = &net->nexthop.rb_root;
3550 struct nh_dump_filter filter = {};
3551 int err;
3552
3553 err = nh_valid_dump_req(cb->nlh, &filter, cb);
3554 if (err < 0)
3555 return err;
3556
3557 err = rtm_dump_walk_nexthops(skb, cb, root, ctx,
3558 &rtm_dump_nexthop_cb, &filter);
3559
3560 cb->seq = net->nexthop.seq;
3561 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3562 return err;
3563 }
3564
3565 static struct nexthop *
nexthop_find_group_resilient(struct net * net,u32 id,struct netlink_ext_ack * extack)3566 nexthop_find_group_resilient(struct net *net, u32 id,
3567 struct netlink_ext_ack *extack)
3568 {
3569 struct nh_group *nhg;
3570 struct nexthop *nh;
3571
3572 nh = nexthop_find_by_id(net, id);
3573 if (!nh)
3574 return ERR_PTR(-ENOENT);
3575
3576 if (!nh->is_group) {
3577 NL_SET_ERR_MSG(extack, "Not a nexthop group");
3578 return ERR_PTR(-EINVAL);
3579 }
3580
3581 nhg = rtnl_dereference(nh->nh_grp);
3582 if (!nhg->resilient) {
3583 NL_SET_ERR_MSG(extack, "Nexthop group not of type resilient");
3584 return ERR_PTR(-EINVAL);
3585 }
3586
3587 return nh;
3588 }
3589
nh_valid_dump_nhid(struct nlattr * attr,u32 * nh_id_p,struct netlink_ext_ack * extack)3590 static int nh_valid_dump_nhid(struct nlattr *attr, u32 *nh_id_p,
3591 struct netlink_ext_ack *extack)
3592 {
3593 u32 idx;
3594
3595 if (attr) {
3596 idx = nla_get_u32(attr);
3597 if (!idx) {
3598 NL_SET_ERR_MSG(extack, "Invalid nexthop id");
3599 return -EINVAL;
3600 }
3601 *nh_id_p = idx;
3602 } else {
3603 *nh_id_p = 0;
3604 }
3605
3606 return 0;
3607 }
3608
nh_valid_dump_bucket_req(const struct nlmsghdr * nlh,struct nh_dump_filter * filter,struct netlink_callback * cb)3609 static int nh_valid_dump_bucket_req(const struct nlmsghdr *nlh,
3610 struct nh_dump_filter *filter,
3611 struct netlink_callback *cb)
3612 {
3613 struct nlattr *res_tb[ARRAY_SIZE(rtm_nh_res_bucket_policy_dump)];
3614 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_dump_bucket)];
3615 int err;
3616
3617 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
3618 ARRAY_SIZE(rtm_nh_policy_dump_bucket) - 1,
3619 rtm_nh_policy_dump_bucket, NULL);
3620 if (err < 0)
3621 return err;
3622
3623 err = nh_valid_dump_nhid(tb[NHA_ID], &filter->nh_id, cb->extack);
3624 if (err)
3625 return err;
3626
3627 if (tb[NHA_RES_BUCKET]) {
3628 size_t max = ARRAY_SIZE(rtm_nh_res_bucket_policy_dump) - 1;
3629
3630 err = nla_parse_nested(res_tb, max,
3631 tb[NHA_RES_BUCKET],
3632 rtm_nh_res_bucket_policy_dump,
3633 cb->extack);
3634 if (err < 0)
3635 return err;
3636
3637 err = nh_valid_dump_nhid(res_tb[NHA_RES_BUCKET_NH_ID],
3638 &filter->res_bucket_nh_id,
3639 cb->extack);
3640 if (err)
3641 return err;
3642 }
3643
3644 return __nh_valid_dump_req(nlh, tb, filter, cb->extack);
3645 }
3646
3647 struct rtm_dump_res_bucket_ctx {
3648 struct rtm_dump_nh_ctx nh;
3649 u16 bucket_index;
3650 };
3651
3652 static struct rtm_dump_res_bucket_ctx *
rtm_dump_res_bucket_ctx(struct netlink_callback * cb)3653 rtm_dump_res_bucket_ctx(struct netlink_callback *cb)
3654 {
3655 struct rtm_dump_res_bucket_ctx *ctx = (void *)cb->ctx;
3656
3657 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
3658 return ctx;
3659 }
3660
3661 struct rtm_dump_nexthop_bucket_data {
3662 struct rtm_dump_res_bucket_ctx *ctx;
3663 struct nh_dump_filter filter;
3664 };
3665
rtm_dump_nexthop_bucket_nh(struct sk_buff * skb,struct netlink_callback * cb,struct nexthop * nh,struct rtm_dump_nexthop_bucket_data * dd)3666 static int rtm_dump_nexthop_bucket_nh(struct sk_buff *skb,
3667 struct netlink_callback *cb,
3668 struct nexthop *nh,
3669 struct rtm_dump_nexthop_bucket_data *dd)
3670 {
3671 u32 portid = NETLINK_CB(cb->skb).portid;
3672 struct nhmsg *nhm = nlmsg_data(cb->nlh);
3673 struct nh_res_table *res_table;
3674 struct nh_group *nhg;
3675 u16 bucket_index;
3676 int err;
3677
3678 nhg = rtnl_dereference(nh->nh_grp);
3679 res_table = rtnl_dereference(nhg->res_table);
3680 for (bucket_index = dd->ctx->bucket_index;
3681 bucket_index < res_table->num_nh_buckets;
3682 bucket_index++) {
3683 struct nh_res_bucket *bucket;
3684 struct nh_grp_entry *nhge;
3685
3686 bucket = &res_table->nh_buckets[bucket_index];
3687 nhge = rtnl_dereference(bucket->nh_entry);
3688 if (nh_dump_filtered(nhge->nh, &dd->filter, nhm->nh_family))
3689 continue;
3690
3691 if (dd->filter.res_bucket_nh_id &&
3692 dd->filter.res_bucket_nh_id != nhge->nh->id)
3693 continue;
3694
3695 dd->ctx->bucket_index = bucket_index;
3696 err = nh_fill_res_bucket(skb, nh, bucket, bucket_index,
3697 RTM_NEWNEXTHOPBUCKET, portid,
3698 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3699 cb->extack);
3700 if (err)
3701 return err;
3702 }
3703
3704 dd->ctx->bucket_index = 0;
3705
3706 return 0;
3707 }
3708
rtm_dump_nexthop_bucket_cb(struct sk_buff * skb,struct netlink_callback * cb,struct nexthop * nh,void * data)3709 static int rtm_dump_nexthop_bucket_cb(struct sk_buff *skb,
3710 struct netlink_callback *cb,
3711 struct nexthop *nh, void *data)
3712 {
3713 struct rtm_dump_nexthop_bucket_data *dd = data;
3714 struct nh_group *nhg;
3715
3716 if (!nh->is_group)
3717 return 0;
3718
3719 nhg = rtnl_dereference(nh->nh_grp);
3720 if (!nhg->resilient)
3721 return 0;
3722
3723 return rtm_dump_nexthop_bucket_nh(skb, cb, nh, dd);
3724 }
3725
3726 /* rtnl */
rtm_dump_nexthop_bucket(struct sk_buff * skb,struct netlink_callback * cb)3727 static int rtm_dump_nexthop_bucket(struct sk_buff *skb,
3728 struct netlink_callback *cb)
3729 {
3730 struct rtm_dump_res_bucket_ctx *ctx = rtm_dump_res_bucket_ctx(cb);
3731 struct rtm_dump_nexthop_bucket_data dd = { .ctx = ctx };
3732 struct net *net = sock_net(skb->sk);
3733 struct nexthop *nh;
3734 int err;
3735
3736 err = nh_valid_dump_bucket_req(cb->nlh, &dd.filter, cb);
3737 if (err)
3738 return err;
3739
3740 if (dd.filter.nh_id) {
3741 nh = nexthop_find_group_resilient(net, dd.filter.nh_id,
3742 cb->extack);
3743 if (IS_ERR(nh))
3744 return PTR_ERR(nh);
3745 err = rtm_dump_nexthop_bucket_nh(skb, cb, nh, &dd);
3746 } else {
3747 struct rb_root *root = &net->nexthop.rb_root;
3748
3749 err = rtm_dump_walk_nexthops(skb, cb, root, &ctx->nh,
3750 &rtm_dump_nexthop_bucket_cb, &dd);
3751 }
3752
3753 cb->seq = net->nexthop.seq;
3754 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3755 return err;
3756 }
3757
nh_valid_get_bucket_req_res_bucket(struct nlattr * res,u16 * bucket_index,struct netlink_ext_ack * extack)3758 static int nh_valid_get_bucket_req_res_bucket(struct nlattr *res,
3759 u16 *bucket_index,
3760 struct netlink_ext_ack *extack)
3761 {
3762 struct nlattr *tb[ARRAY_SIZE(rtm_nh_res_bucket_policy_get)];
3763 int err;
3764
3765 err = nla_parse_nested(tb, ARRAY_SIZE(rtm_nh_res_bucket_policy_get) - 1,
3766 res, rtm_nh_res_bucket_policy_get, extack);
3767 if (err < 0)
3768 return err;
3769
3770 if (!tb[NHA_RES_BUCKET_INDEX]) {
3771 NL_SET_ERR_MSG(extack, "Bucket index is missing");
3772 return -EINVAL;
3773 }
3774
3775 *bucket_index = nla_get_u16(tb[NHA_RES_BUCKET_INDEX]);
3776 return 0;
3777 }
3778
nh_valid_get_bucket_req(const struct nlmsghdr * nlh,u32 * id,u16 * bucket_index,struct netlink_ext_ack * extack)3779 static int nh_valid_get_bucket_req(const struct nlmsghdr *nlh,
3780 u32 *id, u16 *bucket_index,
3781 struct netlink_ext_ack *extack)
3782 {
3783 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get_bucket)];
3784 int err;
3785
3786 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
3787 ARRAY_SIZE(rtm_nh_policy_get_bucket) - 1,
3788 rtm_nh_policy_get_bucket, extack);
3789 if (err < 0)
3790 return err;
3791
3792 err = nh_valid_get_del_req(nlh, tb, id, NULL, extack);
3793 if (err)
3794 return err;
3795
3796 if (!tb[NHA_RES_BUCKET]) {
3797 NL_SET_ERR_MSG(extack, "Bucket information is missing");
3798 return -EINVAL;
3799 }
3800
3801 err = nh_valid_get_bucket_req_res_bucket(tb[NHA_RES_BUCKET],
3802 bucket_index, extack);
3803 if (err)
3804 return err;
3805
3806 return 0;
3807 }
3808
3809 /* rtnl */
rtm_get_nexthop_bucket(struct sk_buff * in_skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3810 static int rtm_get_nexthop_bucket(struct sk_buff *in_skb, struct nlmsghdr *nlh,
3811 struct netlink_ext_ack *extack)
3812 {
3813 struct net *net = sock_net(in_skb->sk);
3814 struct nh_res_table *res_table;
3815 struct sk_buff *skb = NULL;
3816 struct nh_group *nhg;
3817 struct nexthop *nh;
3818 u16 bucket_index;
3819 int err;
3820 u32 id;
3821
3822 err = nh_valid_get_bucket_req(nlh, &id, &bucket_index, extack);
3823 if (err)
3824 return err;
3825
3826 nh = nexthop_find_group_resilient(net, id, extack);
3827 if (IS_ERR(nh))
3828 return PTR_ERR(nh);
3829
3830 nhg = rtnl_dereference(nh->nh_grp);
3831 res_table = rtnl_dereference(nhg->res_table);
3832 if (bucket_index >= res_table->num_nh_buckets) {
3833 NL_SET_ERR_MSG(extack, "Bucket index out of bounds");
3834 return -ENOENT;
3835 }
3836
3837 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3838 if (!skb)
3839 return -ENOBUFS;
3840
3841 err = nh_fill_res_bucket(skb, nh, &res_table->nh_buckets[bucket_index],
3842 bucket_index, RTM_NEWNEXTHOPBUCKET,
3843 NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
3844 0, extack);
3845 if (err < 0) {
3846 WARN_ON(err == -EMSGSIZE);
3847 goto errout_free;
3848 }
3849
3850 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
3851
3852 errout_free:
3853 kfree_skb(skb);
3854 return err;
3855 }
3856
nexthop_sync_mtu(struct net_device * dev,u32 orig_mtu)3857 static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu)
3858 {
3859 unsigned int hash = nh_dev_hashfn(dev->ifindex);
3860 struct net *net = dev_net(dev);
3861 struct hlist_head *head = &net->nexthop.devhash[hash];
3862 struct hlist_node *n;
3863 struct nh_info *nhi;
3864
3865 hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
3866 if (nhi->fib_nhc.nhc_dev == dev) {
3867 if (nhi->family == AF_INET)
3868 fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu,
3869 orig_mtu);
3870 }
3871 }
3872 }
3873
3874 /* rtnl */
nh_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)3875 static int nh_netdev_event(struct notifier_block *this,
3876 unsigned long event, void *ptr)
3877 {
3878 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3879 struct netdev_notifier_info_ext *info_ext;
3880
3881 switch (event) {
3882 case NETDEV_DOWN:
3883 case NETDEV_UNREGISTER:
3884 nexthop_flush_dev(dev, event);
3885 break;
3886 case NETDEV_CHANGE:
3887 if (!(netif_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP)))
3888 nexthop_flush_dev(dev, event);
3889 break;
3890 case NETDEV_CHANGEMTU:
3891 info_ext = ptr;
3892 nexthop_sync_mtu(dev, info_ext->ext.mtu);
3893 rt_cache_flush(dev_net(dev));
3894 break;
3895 }
3896 return NOTIFY_DONE;
3897 }
3898
3899 static struct notifier_block nh_netdev_notifier = {
3900 .notifier_call = nh_netdev_event,
3901 };
3902
nexthops_dump(struct net * net,struct notifier_block * nb,enum nexthop_event_type event_type,struct netlink_ext_ack * extack)3903 static int nexthops_dump(struct net *net, struct notifier_block *nb,
3904 enum nexthop_event_type event_type,
3905 struct netlink_ext_ack *extack)
3906 {
3907 struct rb_root *root = &net->nexthop.rb_root;
3908 struct rb_node *node;
3909 int err = 0;
3910
3911 for (node = rb_first(root); node; node = rb_next(node)) {
3912 struct nexthop *nh;
3913
3914 nh = rb_entry(node, struct nexthop, rb_node);
3915 err = call_nexthop_notifier(nb, net, event_type, nh, extack);
3916 if (err)
3917 break;
3918 }
3919
3920 return err;
3921 }
3922
register_nexthop_notifier(struct net * net,struct notifier_block * nb,struct netlink_ext_ack * extack)3923 int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
3924 struct netlink_ext_ack *extack)
3925 {
3926 int err;
3927
3928 rtnl_lock();
3929 err = nexthops_dump(net, nb, NEXTHOP_EVENT_REPLACE, extack);
3930 if (err)
3931 goto unlock;
3932 err = blocking_notifier_chain_register(&net->nexthop.notifier_chain,
3933 nb);
3934 unlock:
3935 rtnl_unlock();
3936 return err;
3937 }
3938 EXPORT_SYMBOL(register_nexthop_notifier);
3939
__unregister_nexthop_notifier(struct net * net,struct notifier_block * nb)3940 int __unregister_nexthop_notifier(struct net *net, struct notifier_block *nb)
3941 {
3942 int err;
3943
3944 err = blocking_notifier_chain_unregister(&net->nexthop.notifier_chain,
3945 nb);
3946 if (!err)
3947 nexthops_dump(net, nb, NEXTHOP_EVENT_DEL, NULL);
3948 return err;
3949 }
3950 EXPORT_SYMBOL(__unregister_nexthop_notifier);
3951
unregister_nexthop_notifier(struct net * net,struct notifier_block * nb)3952 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb)
3953 {
3954 int err;
3955
3956 rtnl_lock();
3957 err = __unregister_nexthop_notifier(net, nb);
3958 rtnl_unlock();
3959 return err;
3960 }
3961 EXPORT_SYMBOL(unregister_nexthop_notifier);
3962
nexthop_set_hw_flags(struct net * net,u32 id,bool offload,bool trap)3963 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap)
3964 {
3965 struct nexthop *nexthop;
3966
3967 rcu_read_lock();
3968
3969 nexthop = nexthop_find_by_id(net, id);
3970 if (!nexthop)
3971 goto out;
3972
3973 nexthop->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP);
3974 if (offload)
3975 nexthop->nh_flags |= RTNH_F_OFFLOAD;
3976 if (trap)
3977 nexthop->nh_flags |= RTNH_F_TRAP;
3978
3979 out:
3980 rcu_read_unlock();
3981 }
3982 EXPORT_SYMBOL(nexthop_set_hw_flags);
3983
nexthop_bucket_set_hw_flags(struct net * net,u32 id,u16 bucket_index,bool offload,bool trap)3984 void nexthop_bucket_set_hw_flags(struct net *net, u32 id, u16 bucket_index,
3985 bool offload, bool trap)
3986 {
3987 struct nh_res_table *res_table;
3988 struct nh_res_bucket *bucket;
3989 struct nexthop *nexthop;
3990 struct nh_group *nhg;
3991
3992 rcu_read_lock();
3993
3994 nexthop = nexthop_find_by_id(net, id);
3995 if (!nexthop || !nexthop->is_group)
3996 goto out;
3997
3998 nhg = rcu_dereference(nexthop->nh_grp);
3999 if (!nhg->resilient)
4000 goto out;
4001
4002 if (bucket_index >= nhg->res_table->num_nh_buckets)
4003 goto out;
4004
4005 res_table = rcu_dereference(nhg->res_table);
4006 bucket = &res_table->nh_buckets[bucket_index];
4007 bucket->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP);
4008 if (offload)
4009 bucket->nh_flags |= RTNH_F_OFFLOAD;
4010 if (trap)
4011 bucket->nh_flags |= RTNH_F_TRAP;
4012
4013 out:
4014 rcu_read_unlock();
4015 }
4016 EXPORT_SYMBOL(nexthop_bucket_set_hw_flags);
4017
nexthop_res_grp_activity_update(struct net * net,u32 id,u16 num_buckets,unsigned long * activity)4018 void nexthop_res_grp_activity_update(struct net *net, u32 id, u16 num_buckets,
4019 unsigned long *activity)
4020 {
4021 struct nh_res_table *res_table;
4022 struct nexthop *nexthop;
4023 struct nh_group *nhg;
4024 u16 i;
4025
4026 rcu_read_lock();
4027
4028 nexthop = nexthop_find_by_id(net, id);
4029 if (!nexthop || !nexthop->is_group)
4030 goto out;
4031
4032 nhg = rcu_dereference(nexthop->nh_grp);
4033 if (!nhg->resilient)
4034 goto out;
4035
4036 /* Instead of silently ignoring some buckets, demand that the sizes
4037 * be the same.
4038 */
4039 res_table = rcu_dereference(nhg->res_table);
4040 if (num_buckets != res_table->num_nh_buckets)
4041 goto out;
4042
4043 for (i = 0; i < num_buckets; i++) {
4044 if (test_bit(i, activity))
4045 nh_res_bucket_set_busy(&res_table->nh_buckets[i]);
4046 }
4047
4048 out:
4049 rcu_read_unlock();
4050 }
4051 EXPORT_SYMBOL(nexthop_res_grp_activity_update);
4052
nexthop_net_exit_rtnl(struct net * net,struct list_head * dev_to_kill)4053 static void __net_exit nexthop_net_exit_rtnl(struct net *net,
4054 struct list_head *dev_to_kill)
4055 {
4056 ASSERT_RTNL_NET(net);
4057 flush_all_nexthops(net);
4058 }
4059
nexthop_net_exit(struct net * net)4060 static void __net_exit nexthop_net_exit(struct net *net)
4061 {
4062 kfree(net->nexthop.devhash);
4063 net->nexthop.devhash = NULL;
4064 }
4065
nexthop_net_init(struct net * net)4066 static int __net_init nexthop_net_init(struct net *net)
4067 {
4068 size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE;
4069
4070 net->nexthop.rb_root = RB_ROOT;
4071 net->nexthop.devhash = kzalloc(sz, GFP_KERNEL);
4072 if (!net->nexthop.devhash)
4073 return -ENOMEM;
4074 BLOCKING_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain);
4075
4076 return 0;
4077 }
4078
4079 static struct pernet_operations nexthop_net_ops = {
4080 .init = nexthop_net_init,
4081 .exit = nexthop_net_exit,
4082 .exit_rtnl = nexthop_net_exit_rtnl,
4083 };
4084
4085 static const struct rtnl_msg_handler nexthop_rtnl_msg_handlers[] __initconst = {
4086 {.msgtype = RTM_NEWNEXTHOP, .doit = rtm_new_nexthop,
4087 .flags = RTNL_FLAG_DOIT_PERNET},
4088 {.msgtype = RTM_DELNEXTHOP, .doit = rtm_del_nexthop,
4089 .flags = RTNL_FLAG_DOIT_PERNET},
4090 {.msgtype = RTM_GETNEXTHOP, .doit = rtm_get_nexthop,
4091 .dumpit = rtm_dump_nexthop},
4092 {.msgtype = RTM_GETNEXTHOPBUCKET, .doit = rtm_get_nexthop_bucket,
4093 .dumpit = rtm_dump_nexthop_bucket},
4094 {.protocol = PF_INET, .msgtype = RTM_NEWNEXTHOP,
4095 .doit = rtm_new_nexthop, .flags = RTNL_FLAG_DOIT_PERNET},
4096 {.protocol = PF_INET, .msgtype = RTM_GETNEXTHOP,
4097 .dumpit = rtm_dump_nexthop},
4098 {.protocol = PF_INET6, .msgtype = RTM_NEWNEXTHOP,
4099 .doit = rtm_new_nexthop, .flags = RTNL_FLAG_DOIT_PERNET},
4100 {.protocol = PF_INET6, .msgtype = RTM_GETNEXTHOP,
4101 .dumpit = rtm_dump_nexthop},
4102 };
4103
nexthop_init(void)4104 static int __init nexthop_init(void)
4105 {
4106 register_pernet_subsys(&nexthop_net_ops);
4107
4108 register_netdevice_notifier(&nh_netdev_notifier);
4109
4110 rtnl_register_many(nexthop_rtnl_msg_handlers);
4111
4112 return 0;
4113 }
4114 subsys_initcall(nexthop_init);
4115