| 648 | } |
| 649 | |
| 650 | static int |
| 651 | add_route(struct rib_head *rnh, struct rt_addrinfo *info, |
| 652 | struct rib_cmd_info *rc) |
| 653 | { |
| 654 | struct nhop_object *nh_orig; |
| 655 | struct route_nhop_data rnd_orig, rnd_add; |
| 656 | struct nhop_object *nh; |
| 657 | struct rtentry *rt, *rt_orig; |
| 658 | int error; |
| 659 | |
| 660 | error = create_rtentry(rnh, info, &rt); |
| 661 | if (error != 0) |
| 662 | return (error); |
| 663 | |
| 664 | rnd_add.rnd_nhop = rt->rt_nhop; |
| 665 | rnd_add.rnd_weight = rt->rt_weight; |
| 666 | nh = rt->rt_nhop; |
| 667 | |
| 668 | RIB_WLOCK(rnh); |
| 669 | error = add_route_nhop(rnh, rt, info, &rnd_add, rc); |
| 670 | if (error == 0) { |
| 671 | RIB_WUNLOCK(rnh); |
| 672 | return (0); |
| 673 | } |
| 674 | |
| 675 | /* addition failed. Lookup prefix in the rib to determine the cause */ |
| 676 | rt_orig = lookup_prefix(rnh, info, &rnd_orig); |
| 677 | if (rt_orig == NULL) { |
| 678 | /* No prefix -> rnh_addaddr() failed to allocate memory */ |
| 679 | RIB_WUNLOCK(rnh); |
| 680 | nhop_free(nh); |
| 681 | uma_zfree(V_rtzone, rt); |
| 682 | return (ENOMEM); |
| 683 | } |
| 684 | |
| 685 | /* We have existing route in the RIB. */ |
| 686 | nh_orig = rnd_orig.rnd_nhop; |
| 687 | /* Check if new route has higher preference */ |
| 688 | if (can_override_nhop(info, nh_orig) > 0) { |
| 689 | /* Update nexthop to the new route */ |
| 690 | change_route_nhop(rnh, rt_orig, info, &rnd_add, rc); |
| 691 | RIB_WUNLOCK(rnh); |
| 692 | uma_zfree(V_rtzone, rt); |
| 693 | nhop_free(nh_orig); |
| 694 | return (0); |
| 695 | } |
| 696 | |
| 697 | RIB_WUNLOCK(rnh); |
| 698 | |
| 699 | #ifdef ROUTE_MPATH |
| 700 | if (rib_can_multipath(rnh) && nhop_can_multipath(rnd_add.rnd_nhop) && |
| 701 | nhop_can_multipath(rnd_orig.rnd_nhop)) |
| 702 | error = add_route_mpath(rnh, info, rt, &rnd_add, &rnd_orig, rc); |
| 703 | else |
| 704 | #endif |
| 705 | /* Unable to add - another route with the same preference exists */ |
| 706 | error = EEXIST; |
| 707 |
no test coverage detected