Commit c18865f39276435abb9286f9a816cb5b66c99a00

Authored by Julian Anastasov
Committed by David S. Miller
1 parent ec9dbb1c3e

[IPV4] fib: fix route replacement, fib_info is shared

fib_info can be shared by many route prefixes but we don't want
duplicate alternative routes for a prefix+tos+priority. Last change
was not correct to check fib_treeref because it accounts usage from
other prefixes. Additionally, avoid replacement without error if new
route is same, as Joonwoo Park suggests.

Signed-off-by: Julian Anastasov <ja@ssi.bg>
Signed-off-by: David S. Miller <davem@davemloft.net>

Showing 1 changed file with 31 additions and 16 deletions Side-by-side Diff

... ... @@ -424,19 +424,43 @@
424 424  
425 425 if (fa && fa->fa_tos == tos &&
426 426 fa->fa_info->fib_priority == fi->fib_priority) {
427   - struct fib_alias *fa_orig;
  427 + struct fib_alias *fa_first, *fa_match;
428 428  
429 429 err = -EEXIST;
430 430 if (cfg->fc_nlflags & NLM_F_EXCL)
431 431 goto out;
432 432  
  433 + /* We have 2 goals:
  434 + * 1. Find exact match for type, scope, fib_info to avoid
  435 + * duplicate routes
  436 + * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
  437 + */
  438 + fa_match = NULL;
  439 + fa_first = fa;
  440 + fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  441 + list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  442 + if (fa->fa_tos != tos)
  443 + break;
  444 + if (fa->fa_info->fib_priority != fi->fib_priority)
  445 + break;
  446 + if (fa->fa_type == cfg->fc_type &&
  447 + fa->fa_scope == cfg->fc_scope &&
  448 + fa->fa_info == fi) {
  449 + fa_match = fa;
  450 + break;
  451 + }
  452 + }
  453 +
433 454 if (cfg->fc_nlflags & NLM_F_REPLACE) {
434 455 struct fib_info *fi_drop;
435 456 u8 state;
436 457  
437   - if (fi->fib_treeref > 1)
  458 + fa = fa_first;
  459 + if (fa_match) {
  460 + if (fa == fa_match)
  461 + err = 0;
438 462 goto out;
439   -
  463 + }
440 464 write_lock_bh(&fib_hash_lock);
441 465 fi_drop = fa->fa_info;
442 466 fa->fa_info = fi;
443 467  
... ... @@ -459,20 +483,11 @@
459 483 * uses the same scope, type, and nexthop
460 484 * information.
461 485 */
462   - fa_orig = fa;
463   - fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
464   - list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
465   - if (fa->fa_tos != tos)
466   - break;
467   - if (fa->fa_info->fib_priority != fi->fib_priority)
468   - break;
469   - if (fa->fa_type == cfg->fc_type &&
470   - fa->fa_scope == cfg->fc_scope &&
471   - fa->fa_info == fi)
472   - goto out;
473   - }
  486 + if (fa_match)
  487 + goto out;
  488 +
474 489 if (!(cfg->fc_nlflags & NLM_F_APPEND))
475   - fa = fa_orig;
  490 + fa = fa_first;
476 491 }
477 492  
478 493 err = -ENOENT;