~ubuntu-branches/ubuntu/saucy/quagga/saucy

« back to all changes in this revision

Viewing changes to zebra/zebra_rib.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2007-09-14 09:47:54 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070914094754-kqi815lcg6n8mh51
Tags: 0.99.9-1ubuntu1
* Merged new upstream version (LP: #139376)
  - Merged debian/changelog
* Fixes a DoS and some bugs introduced in 0.99.8
  (see: http://www.quagga.net/news2.php?y=2007&m=9&d=7#id1189190760)
* Remaining Ubuntu Patches:
  - debian/rules: use bash as shell
  - debian/quagga.prerm: handle upgrades more gracefully
  - debian/patches/81_32bit_u64.dpatch: Define __u64 as uint64_t
    before including the netlink headers, since that symbol does not exist
    by default on 32 bit arches. Fixes i386/powerpc FTBFS.
  - debian/patches/83_ifaddr_defs.dpatch:
    zebra/rt_netlink.c: #include <linux/if_addr.h> and define IF[L]A_RTA
    macros, so that the file compiles again with our kernel headers.
  - debian/patches/20_ht-20061217-0.99.6-bgp-md5.dpatch: updated for
    linux kernel 2.6.20.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include "linklist.h"
33
33
#include "thread.h"
34
34
#include "workqueue.h"
 
35
#include "prefix.h"
 
36
#include "routemap.h"
35
37
 
36
38
#include "zebra/rib.h"
37
39
#include "zebra/rt.h"
233
235
}
234
236
 
235
237
struct nexthop *
236
 
nexthop_ipv4_add (struct rib *rib, struct in_addr *ipv4)
 
238
nexthop_ipv4_add (struct rib *rib, struct in_addr *ipv4, struct in_addr *src)
237
239
{
238
240
  struct nexthop *nexthop;
239
241
 
241
243
  memset (nexthop, 0, sizeof (struct nexthop));
242
244
  nexthop->type = NEXTHOP_TYPE_IPV4;
243
245
  nexthop->gate.ipv4 = *ipv4;
 
246
  if (src)
 
247
    nexthop->src.ipv4 = *src;
244
248
 
245
249
  nexthop_add (rib, nexthop);
246
250
 
249
253
 
250
254
static struct nexthop *
251
255
nexthop_ipv4_ifindex_add (struct rib *rib, struct in_addr *ipv4, 
252
 
                          unsigned int ifindex)
 
256
                          struct in_addr *src, unsigned int ifindex)
253
257
{
254
258
  struct nexthop *nexthop;
255
259
 
257
261
  memset (nexthop, 0, sizeof (struct nexthop));
258
262
  nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
259
263
  nexthop->gate.ipv4 = *ipv4;
 
264
  if (src)
 
265
    nexthop->src.ipv4 = *src;
260
266
  nexthop->ifindex = ifindex;
261
267
 
262
268
  nexthop_add (rib, nexthop);
625
631
  return NULL;
626
632
}
627
633
 
 
634
/*
 
635
 * This clone function, unlike its original rib_lookup_ipv4(), checks
 
636
 * if specified IPv4 route record (prefix/mask -> gate) exists in
 
637
 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
 
638
 *
 
639
 * Return values:
 
640
 * -1: error
 
641
 * 0: exact match found
 
642
 * 1: a match was found with a different gate
 
643
 * 2: connected route found
 
644
 * 3: no matches found
 
645
 */
 
646
int
 
647
rib_lookup_ipv4_route (struct prefix_ipv4 *p, union sockunion * qgate)
 
648
{
 
649
  struct route_table *table;
 
650
  struct route_node *rn;
 
651
  struct rib *match;
 
652
  struct nexthop *nexthop;
 
653
 
 
654
  /* Lookup table.  */
 
655
  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
 
656
  if (! table)
 
657
    return ZEBRA_RIB_LOOKUP_ERROR;
 
658
 
 
659
  /* Scan the RIB table for exactly matching RIB entry. */
 
660
  rn = route_node_lookup (table, (struct prefix *) p);
 
661
 
 
662
  /* No route for this prefix. */
 
663
  if (! rn)
 
664
    return ZEBRA_RIB_NOTFOUND;
 
665
 
 
666
  /* Unlock node. */
 
667
  route_unlock_node (rn);
 
668
 
 
669
  /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
 
670
  for (match = rn->info; match; match = match->next)
 
671
  {
 
672
    if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
 
673
      continue;
 
674
    if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
 
675
      break;
 
676
  }
 
677
 
 
678
  /* None such found :( */
 
679
  if (!match)
 
680
    return ZEBRA_RIB_NOTFOUND;
 
681
 
 
682
  if (match->type == ZEBRA_ROUTE_CONNECT)
 
683
    return ZEBRA_RIB_FOUND_CONNECTED;
 
684
  
 
685
  /* Ok, we have a cood candidate, let's check it's nexthop list... */
 
686
  for (nexthop = match->nexthop; nexthop; nexthop = nexthop->next)
 
687
    if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
 
688
    {
 
689
      /* We are happy with either direct or recursive hexthop */
 
690
      if (nexthop->gate.ipv4.s_addr == qgate->sin.sin_addr.s_addr ||
 
691
          nexthop->rgate.ipv4.s_addr == qgate->sin.sin_addr.s_addr)
 
692
        return ZEBRA_RIB_FOUND_EXACT;
 
693
      else
 
694
      {
 
695
        if (IS_ZEBRA_DEBUG_RIB)
 
696
        {
 
697
          char gate_buf[INET_ADDRSTRLEN], rgate_buf[INET_ADDRSTRLEN], qgate_buf[INET_ADDRSTRLEN];
 
698
          inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, gate_buf, INET_ADDRSTRLEN);
 
699
          inet_ntop (AF_INET, &nexthop->rgate.ipv4.s_addr, rgate_buf, INET_ADDRSTRLEN);
 
700
          inet_ntop (AF_INET, &qgate->sin.sin_addr.s_addr, qgate_buf, INET_ADDRSTRLEN);
 
701
          zlog_debug ("%s: qgate == %s, gate == %s, rgate == %s", __func__, qgate_buf, gate_buf, rgate_buf);
 
702
        }
 
703
        return ZEBRA_RIB_FOUND_NOGATE;
 
704
      }
 
705
    }
 
706
 
 
707
  return ZEBRA_RIB_NOTFOUND;
 
708
}
 
709
 
628
710
#ifdef HAVE_IPV6
629
711
struct rib *
630
712
rib_match_ipv6 (struct in6_addr *addr)
685
767
}
686
768
#endif /* HAVE_IPV6 */
687
769
 
 
770
#define RIB_SYSTEM_ROUTE(R) \
 
771
        ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
 
772
 
 
773
/* This function verifies reachability of one given nexthop, which can be
 
774
 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
 
775
 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
 
776
 * nexthop->ifindex will be updated appropriately as well.
 
777
 * An existing route map can turn (otherwise active) nexthop into inactive, but
 
778
 * not vice versa.
 
779
 *
 
780
 * The return value is the final value of 'ACTIVE' flag.
 
781
 */
 
782
 
688
783
static int
689
784
nexthop_active_check (struct route_node *rn, struct rib *rib,
690
785
                      struct nexthop *nexthop, int set)
691
786
{
692
787
  struct interface *ifp;
 
788
  route_map_result_t ret = RMAP_MATCH;
 
789
  extern char *proto_rm[AFI_MAX][ZEBRA_ROUTE_MAX+1];
 
790
  struct route_map *rmap;
 
791
  int family;
693
792
 
 
793
  family = 0;
694
794
  switch (nexthop->type)
695
795
    {
696
796
    case NEXTHOP_TYPE_IFINDEX:
700
800
      else
701
801
        UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
702
802
      break;
 
803
    case NEXTHOP_TYPE_IPV6_IFNAME:
 
804
      family = AFI_IP6;
703
805
    case NEXTHOP_TYPE_IFNAME:
704
 
    case NEXTHOP_TYPE_IPV6_IFNAME:
705
806
      ifp = if_lookup_by_name (nexthop->ifname);
706
807
      if (ifp && if_is_up (ifp))
707
808
        {
718
819
      break;
719
820
    case NEXTHOP_TYPE_IPV4:
720
821
    case NEXTHOP_TYPE_IPV4_IFINDEX:
 
822
      family = AFI_IP;
721
823
      if (nexthop_active_ipv4 (rib, nexthop, set, rn))
722
824
        SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
723
825
      else
725
827
      break;
726
828
#ifdef HAVE_IPV6
727
829
    case NEXTHOP_TYPE_IPV6:
 
830
      family = AFI_IP6;
728
831
      if (nexthop_active_ipv6 (rib, nexthop, set, rn))
729
832
        SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
730
833
      else
731
834
        UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
732
835
      break;
733
836
    case NEXTHOP_TYPE_IPV6_IFINDEX:
 
837
      family = AFI_IP6;
734
838
      if (IN6_IS_ADDR_LINKLOCAL (&nexthop->gate.ipv6))
735
839
        {
736
840
          ifp = if_lookup_by_index (nexthop->ifindex);
754
858
    default:
755
859
      break;
756
860
    }
 
861
  if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
 
862
    return 0;
 
863
 
 
864
  if (RIB_SYSTEM_ROUTE(rib) ||
 
865
      (family == AFI_IP && rn->p.family != AF_INET) ||
 
866
      (family == AFI_IP6 && rn->p.family != AF_INET6))
 
867
    return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
 
868
 
 
869
  rmap = 0;
 
870
  if (rib->type >= 0 && rib->type < ZEBRA_ROUTE_MAX &&
 
871
                proto_rm[family][rib->type])
 
872
    rmap = route_map_lookup_by_name (proto_rm[family][rib->type]);
 
873
  if (!rmap && proto_rm[family][ZEBRA_ROUTE_MAX])
 
874
    rmap = route_map_lookup_by_name (proto_rm[family][ZEBRA_ROUTE_MAX]);
 
875
  if (rmap) {
 
876
      ret = route_map_apply(rmap, &rn->p, RMAP_ZEBRA, nexthop);
 
877
  }
 
878
 
 
879
  if (ret == RMAP_DENYMATCH)
 
880
    UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
757
881
  return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
758
882
}
759
883
 
 
884
/* Iterate over all nexthops of the given RIB entry and refresh their
 
885
 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
 
886
 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
 
887
 * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is
 
888
 * transparently passed to nexthop_active_check().
 
889
 *
 
890
 * Return value is the new number of active nexthops.
 
891
 */
 
892
 
760
893
static int
761
894
nexthop_active_update (struct route_node *rn, struct rib *rib, int set)
762
895
{
763
896
  struct nexthop *nexthop;
764
 
  int active;
 
897
  int prev_active, new_active;
765
898
 
766
899
  rib->nexthop_active_num = 0;
767
900
  UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
768
901
 
769
902
  for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
770
 
    {
771
 
      active = CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
772
 
 
773
 
      nexthop_active_check (rn, rib, nexthop, set);
774
 
      if ((MULTIPATH_NUM == 0 || rib->nexthop_active_num < MULTIPATH_NUM)
775
 
          && active != CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
776
 
        SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
777
 
 
778
 
      if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
779
 
        rib->nexthop_active_num++;
780
 
    }
 
903
  {
 
904
    prev_active = CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
 
905
    if ((new_active = nexthop_active_check (rn, rib, nexthop, set)))
 
906
      rib->nexthop_active_num++;
 
907
    if (prev_active != new_active)
 
908
      SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
 
909
  }
781
910
  return rib->nexthop_active_num;
782
911
}
783
912
 
784
913
 
785
 
#define RIB_SYSTEM_ROUTE(R) \
786
 
        ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
787
914
 
788
915
static void
789
916
rib_install_kernel (struct route_node *rn, struct rib *rib)
803
930
#endif /* HAVE_IPV6 */
804
931
    }
805
932
 
 
933
  /* This condition is never met, if we are using rt_socket.c */
806
934
  if (ret < 0)
807
935
    {
808
936
      for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
824
952
      break;
825
953
#ifdef HAVE_IPV6
826
954
    case AF_INET6:
 
955
      if (IS_ZEBRA_DEBUG_RIB)
 
956
        zlog_debug ("%s: calling kernel_delete_ipv4 (%p, %p)", __func__, rn, rib);
827
957
      ret = kernel_delete_ipv6 (&rn->p, rib);
828
958
      break;
829
959
#endif /* HAVE_IPV6 */
862
992
  struct route_node *rn = data;
863
993
  int installed = 0;
864
994
  struct nexthop *nexthop = NULL;
 
995
  char buf[INET_ADDRSTRLEN];
865
996
  
866
997
  assert (rn);
867
998
  
 
999
  if (IS_ZEBRA_DEBUG_RIB || IS_ZEBRA_DEBUG_RIB_Q)
 
1000
    inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN);
 
1001
 
868
1002
  for (rib = rn->info; rib; rib = next)
869
1003
    {
 
1004
      /* The next pointer is saved, because current pointer
 
1005
       * may be passed to rib_unlink() in the middle of iteration.
 
1006
       */
870
1007
      next = rib->next;
871
1008
      
872
1009
      /* Currently installed rib. */
884
1021
          if (rib != fib)
885
1022
            {
886
1023
              if (IS_ZEBRA_DEBUG_RIB)
887
 
                zlog_debug ("%s: rn %p, removing rib %p", __func__, rn, rib);
 
1024
                zlog_debug ("%s: %s/%d: rn %p, removing rib %p", __func__,
 
1025
                  buf, rn->p.prefixlen, rn, rib);
888
1026
                rib_unlink (rn, rib);
889
1027
            }
890
1028
          else
942
1080
      /* metric tie-breaks equal distance */
943
1081
      if (rib->metric <= select->metric)
944
1082
        select = rib;
945
 
    }
946
 
  
947
 
  /* Same route is selected. */
 
1083
    } /* for (rib = rn->info; rib; rib = next) */
 
1084
 
 
1085
  /* After the cycle is finished, the following pointers will be set:
 
1086
   * select --- the winner RIB entry, if any was found, otherwise NULL
 
1087
   * fib    --- the SELECTED RIB entry, if any, otherwise NULL
 
1088
   * del    --- equal to fib, if fib is queued for deletion, NULL otherwise
 
1089
   * rib    --- NULL
 
1090
   */
 
1091
 
 
1092
  /* Same RIB entry is selected. Update FIB and finish. */
948
1093
  if (select && select == fib)
949
1094
    {
950
1095
      if (IS_ZEBRA_DEBUG_RIB)
951
 
        zlog_debug ("%s: Updating existing route, select %p, fib %p",
952
 
                     __func__, select, fib);
 
1096
        zlog_debug ("%s: %s/%d: Updating existing route, select %p, fib %p",
 
1097
                     __func__, buf, rn->p.prefixlen, select, fib);
953
1098
      if (CHECK_FLAG (select->flags, ZEBRA_FLAG_CHANGED))
954
1099
        {
955
1100
          redistribute_delete (&rn->p, select);
983
1128
      goto end;
984
1129
    }
985
1130
 
986
 
  /* Uninstall old rib from forwarding table. */
 
1131
  /* At this point we either haven't found the best RIB entry or it is
 
1132
   * different from what we currently intend to flag with SELECTED. In both
 
1133
   * cases, if a RIB block is present in FIB, it should be withdrawn.
 
1134
   */
987
1135
  if (fib)
988
1136
    {
989
1137
      if (IS_ZEBRA_DEBUG_RIB)
990
 
        zlog_debug ("%s: Removing existing route, fib %p", __func__, fib);
 
1138
        zlog_debug ("%s: %s/%d: Removing existing route, fib %p", __func__,
 
1139
          buf, rn->p.prefixlen, fib);
991
1140
      redistribute_delete (&rn->p, fib);
992
1141
      if (! RIB_SYSTEM_ROUTE (fib))
993
1142
        rib_uninstall_kernel (rn, fib);
997
1146
      nexthop_active_update (rn, fib, 1);
998
1147
    }
999
1148
 
1000
 
  /* Install new rib into forwarding table. */
 
1149
  /* Regardless of some RIB entry being SELECTED or not before, now we can
 
1150
   * tell, that if a new winner exists, FIB is still not updated with this
 
1151
   * data, but ready to be.
 
1152
   */
1001
1153
  if (select)
1002
1154
    {
1003
1155
      if (IS_ZEBRA_DEBUG_RIB)
1004
 
        zlog_debug ("%s: Adding route, select %p", __func__, select);
 
1156
        zlog_debug ("%s: %s/%d: Adding route, select %p", __func__, buf,
 
1157
          rn->p.prefixlen, select);
1005
1158
      /* Set real nexthop. */
1006
1159
      nexthop_active_update (rn, select, 1);
1007
1160
 
1015
1168
  if (del)
1016
1169
    {
1017
1170
      if (IS_ZEBRA_DEBUG_RIB)
1018
 
        zlog_debug ("%s: Deleting fib %p, rn %p", __func__, del, rn);
 
1171
        zlog_debug ("%s: %s/%d: Deleting fib %p, rn %p", __func__, buf,
 
1172
          rn->p.prefixlen, del, rn);
1019
1173
      rib_unlink (rn, del);
1020
1174
    }
1021
1175
 
1022
1176
end:
1023
1177
  if (IS_ZEBRA_DEBUG_RIB_Q)
1024
 
    zlog_debug ("%s: rn %p dequeued", __func__, rn);
 
1178
    zlog_debug ("%s: %s/%d: rn %p dequeued", __func__, buf, rn->p.prefixlen, rn);
1025
1179
  if (rn->info)
1026
1180
    UNSET_FLAG (((struct rib *)rn->info)->rn_status, RIB_ROUTE_QUEUED);  
1027
1181
  route_unlock_node (rn); /* rib queue lock */
1032
1186
static void
1033
1187
rib_queue_add (struct zebra_t *zebra, struct route_node *rn)
1034
1188
{
 
1189
  char buf[INET_ADDRSTRLEN];
1035
1190
  assert (zebra && rn);
1036
1191
  
 
1192
  if (IS_ZEBRA_DEBUG_RIB_Q)
 
1193
    inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN);
 
1194
 
1037
1195
  /* Pointless to queue a route_node with no RIB entries to add or remove */
1038
1196
  if (!rn->info)
1039
1197
    {
1047
1205
  if (CHECK_FLAG (((struct rib *)rn->info)->rn_status, RIB_ROUTE_QUEUED))
1048
1206
    {
1049
1207
      if (IS_ZEBRA_DEBUG_RIB_Q)
1050
 
        zlog_debug ("%s: rn %p already queued", __func__, rn);
 
1208
        zlog_debug ("%s: %s/%d: rn %p already queued", __func__, buf,
 
1209
          rn->p.prefixlen, rn);
1051
1210
      return;
1052
1211
    }
1053
1212
 
1054
1213
  route_lock_node (rn); /* rib queue lock */
1055
1214
 
1056
1215
  if (IS_ZEBRA_DEBUG_RIB_Q)
1057
 
    zlog_info ("%s: work queue added", __func__);
 
1216
    zlog_info ("%s: %s/%d: work queue added", __func__, buf, rn->p.prefixlen);
1058
1217
 
1059
1218
  assert (zebra);
1060
1219
 
1070
1229
  SET_FLAG (((struct rib *)rn->info)->rn_status, RIB_ROUTE_QUEUED);
1071
1230
 
1072
1231
  if (IS_ZEBRA_DEBUG_RIB_Q)
1073
 
    zlog_debug ("%s: rn %p queued", __func__, rn);
 
1232
    zlog_debug ("%s: %s/%d: rn %p queued", __func__, buf, rn->p.prefixlen, rn);
1074
1233
 
1075
1234
  return;
1076
1235
}
1141
1300
rib_link (struct route_node *rn, struct rib *rib)
1142
1301
{
1143
1302
  struct rib *head;
 
1303
  char buf[INET_ADDRSTRLEN];
1144
1304
  
1145
1305
  assert (rib && rn);
1146
1306
  
1147
1307
  route_lock_node (rn); /* rn route table reference */
1148
1308
 
1149
1309
  if (IS_ZEBRA_DEBUG_RIB)
1150
 
    zlog_debug ("%s: rn %p, rib %p", __func__, rn, rib);
 
1310
  {
 
1311
    inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN);
 
1312
    zlog_debug ("%s: %s/%d: rn %p, rib %p", __func__,
 
1313
      buf, rn->p.prefixlen, rn, rib);
 
1314
  }
1151
1315
 
1152
1316
  head = rn->info;
1153
1317
  if (head)
1154
1318
    {
1155
1319
      if (IS_ZEBRA_DEBUG_RIB)
1156
 
        zlog_debug ("%s: new head, rn_status copied over", __func__);
 
1320
        zlog_debug ("%s: %s/%d: new head, rn_status copied over", __func__,
 
1321
          buf, rn->p.prefixlen);
1157
1322
      head->prev = rib;
1158
1323
      /* Transfer the rn status flags to the new head RIB */
1159
1324
      rib->rn_status = head->rn_status;
1172
1337
  if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
1173
1338
    {
1174
1339
      if (IS_ZEBRA_DEBUG_RIB)
1175
 
        zlog_debug ("%s: rn %p, un-removed rib %p",
1176
 
                    __func__, rn, rib);
 
1340
      {
 
1341
        char buf[INET_ADDRSTRLEN];
 
1342
        inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN);
 
1343
        zlog_debug ("%s: %s/%d: rn %p, un-removed rib %p",
 
1344
                    __func__, buf, rn->p.prefixlen, rn, rib);
 
1345
      }
1177
1346
      UNSET_FLAG (rib->status, RIB_ENTRY_REMOVED);
1178
1347
      return;
1179
1348
    }
1184
1353
rib_unlink (struct route_node *rn, struct rib *rib)
1185
1354
{
1186
1355
  struct nexthop *nexthop, *next;
 
1356
  char buf[INET_ADDRSTRLEN];
1187
1357
 
1188
1358
  assert (rn && rib);
1189
1359
 
1190
1360
  if (IS_ZEBRA_DEBUG_RIB)
1191
 
    zlog_debug ("%s: rn %p, rib %p",
1192
 
                __func__, rn, rib);
 
1361
  {
 
1362
    inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN);
 
1363
    zlog_debug ("%s: %s/%d: rn %p, rib %p",
 
1364
                __func__, buf, rn->p.prefixlen, rn, rib);
 
1365
  }
1193
1366
 
1194
1367
  if (rib->next)
1195
1368
    rib->next->prev = rib->prev;
1203
1376
      if (rn->info)
1204
1377
        {
1205
1378
          if (IS_ZEBRA_DEBUG_RIB)
1206
 
            zlog_debug ("%s: rn %p, rib %p, new head copy",
1207
 
                        __func__, rn, rib);
 
1379
            zlog_debug ("%s: %s/%d: rn %p, rib %p, new head copy",
 
1380
                        __func__, buf, rn->p.prefixlen, rn, rib);
1208
1381
          rib->next->rn_status = rib->rn_status;
1209
1382
        }
1210
1383
    }
1224
1397
rib_delnode (struct route_node *rn, struct rib *rib)
1225
1398
{
1226
1399
  if (IS_ZEBRA_DEBUG_RIB)
1227
 
    zlog_debug ("%s: rn %p, rib %p, removing", __func__, rn, rib);
 
1400
  {
 
1401
    char buf[INET_ADDRSTRLEN];
 
1402
    inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN);
 
1403
    zlog_debug ("%s: %s/%d: rn %p, rib %p, removing", __func__,
 
1404
      buf, rn->p.prefixlen, rn, rib);
 
1405
  }
1228
1406
  SET_FLAG (rib->status, RIB_ENTRY_REMOVED);
1229
1407
  rib_queue_add (&zebrad, rn);
1230
1408
}
1231
1409
 
1232
1410
int
1233
1411
rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p, 
1234
 
              struct in_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
 
1412
              struct in_addr *gate, struct in_addr *src,
 
1413
              unsigned int ifindex, u_int32_t vrf_id,
1235
1414
              u_int32_t metric, u_char distance)
1236
1415
{
1237
1416
  struct rib *rib;
1300
1479
  if (gate)
1301
1480
    {
1302
1481
      if (ifindex)
1303
 
        nexthop_ipv4_ifindex_add (rib, gate, ifindex);
 
1482
        nexthop_ipv4_ifindex_add (rib, gate, src, ifindex);
1304
1483
      else
1305
 
        nexthop_ipv4_add (rib, gate);
 
1484
        nexthop_ipv4_add (rib, gate, src);
1306
1485
    }
1307
1486
  else
1308
1487
    nexthop_ifindex_add (rib, ifindex);
1313
1492
      SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1314
1493
 
1315
1494
  /* Link new rib to node.*/
 
1495
  if (IS_ZEBRA_DEBUG_RIB)
 
1496
    zlog_debug ("%s: calling rib_addnode (%p, %p)", __func__, rn, rib);
1316
1497
  rib_addnode (rn, rib);
1317
1498
  
1318
1499
  /* Free implicit route.*/
1319
1500
  if (same)
 
1501
  {
 
1502
    if (IS_ZEBRA_DEBUG_RIB)
 
1503
      zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__, rn, rib);
1320
1504
    rib_delnode (rn, same);
 
1505
  }
1321
1506
  
1322
1507
  route_unlock_node (rn);
1323
1508
  return 0;
1324
1509
}
1325
1510
 
 
1511
/* This function dumps the contents of a given RIB entry into
 
1512
 * standard debug log. Calling function name and IP prefix in
 
1513
 * question are passed as 1st and 2nd arguments.
 
1514
 */
 
1515
 
 
1516
void rib_dump (const char * func, const struct prefix_ipv4 * p, const struct rib * rib)
 
1517
{
 
1518
  char straddr1[INET_ADDRSTRLEN], straddr2[INET_ADDRSTRLEN];
 
1519
  struct nexthop *nexthop;
 
1520
 
 
1521
  inet_ntop (AF_INET, &p->prefix, straddr1, INET_ADDRSTRLEN);
 
1522
  zlog_debug ("%s: dumping RIB entry %p for %s/%d", func, rib, straddr1, p->prefixlen);
 
1523
  zlog_debug
 
1524
  (
 
1525
    "%s: refcnt == %lu, uptime == %u, type == %u, table == %d",
 
1526
    func,
 
1527
    rib->refcnt,
 
1528
    rib->uptime,
 
1529
    rib->type,
 
1530
    rib->table
 
1531
  );
 
1532
  zlog_debug
 
1533
  (
 
1534
    "%s: metric == %u, distance == %u, flags == %u, status == %u",
 
1535
    func,
 
1536
    rib->metric,
 
1537
    rib->distance,
 
1538
    rib->flags,
 
1539
    rib->status
 
1540
  );
 
1541
  zlog_debug
 
1542
  (
 
1543
    "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
 
1544
    func,
 
1545
    rib->nexthop_num,
 
1546
    rib->nexthop_active_num,
 
1547
    rib->nexthop_fib_num
 
1548
  );
 
1549
  for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
 
1550
  {
 
1551
    inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, straddr1, INET_ADDRSTRLEN);
 
1552
    inet_ntop (AF_INET, &nexthop->rgate.ipv4.s_addr, straddr2, INET_ADDRSTRLEN);
 
1553
    zlog_debug
 
1554
    (
 
1555
      "%s: NH %s (%s) with flags %s%s%s",
 
1556
      func,
 
1557
      straddr1,
 
1558
      straddr2,
 
1559
      (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE) ? "ACTIVE " : ""),
 
1560
      (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? "FIB " : ""),
 
1561
      (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ? "RECURSIVE" : "")
 
1562
    );
 
1563
  }
 
1564
  zlog_debug ("%s: dump complete", func);
 
1565
}
 
1566
 
 
1567
/* This is an exported helper to rtm_read() to dump the strange
 
1568
 * RIB entry found by rib_lookup_ipv4_route()
 
1569
 */
 
1570
 
 
1571
void rib_lookup_and_dump (struct prefix_ipv4 * p)
 
1572
{
 
1573
  struct route_table *table;
 
1574
  struct route_node *rn;
 
1575
  struct rib *rib;
 
1576
  char prefix_buf[INET_ADDRSTRLEN];
 
1577
 
 
1578
  /* Lookup table.  */
 
1579
  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
 
1580
  if (! table)
 
1581
  {
 
1582
    zlog_err ("%s: vrf_table() returned NULL", __func__);
 
1583
    return;
 
1584
  }
 
1585
 
 
1586
  inet_ntop (AF_INET, &p->prefix.s_addr, prefix_buf, INET_ADDRSTRLEN);
 
1587
  /* Scan the RIB table for exactly matching RIB entry. */
 
1588
  rn = route_node_lookup (table, (struct prefix *) p);
 
1589
 
 
1590
  /* No route for this prefix. */
 
1591
  if (! rn)
 
1592
  {
 
1593
    zlog_debug ("%s: lookup failed for %s/%d", __func__, prefix_buf, p->prefixlen);
 
1594
    return;
 
1595
  }
 
1596
 
 
1597
  /* Unlock node. */
 
1598
  route_unlock_node (rn);
 
1599
 
 
1600
  /* let's go */
 
1601
  for (rib = rn->info; rib; rib = rib->next)
 
1602
  {
 
1603
    zlog_debug
 
1604
    (
 
1605
      "%s: rn %p, rib %p: %s, %s",
 
1606
      __func__,
 
1607
      rn,
 
1608
      rib,
 
1609
      (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED) ? "removed" : "NOT removed"),
 
1610
      (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) ? "selected" : "NOT selected")
 
1611
    );
 
1612
    rib_dump (__func__, p, rib);
 
1613
  }
 
1614
}
 
1615
 
1326
1616
int
1327
1617
rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib)
1328
1618
{
1356
1646
     withdraw. */
1357
1647
  for (same = rn->info; same; same = same->next)
1358
1648
    {
1359
 
      if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
 
1649
      if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED))
1360
1650
        continue;
1361
1651
      
1362
1652
      if (same->type == rib->type && same->table == rib->table
1371
1661
 
1372
1662
  /* Link new rib to node.*/
1373
1663
  rib_addnode (rn, rib);
 
1664
  if (IS_ZEBRA_DEBUG_RIB)
 
1665
  {
 
1666
    zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
 
1667
      __func__, rn, rib);
 
1668
    rib_dump (__func__, p, rib);
 
1669
  }
1374
1670
 
1375
1671
  /* Free implicit route.*/
1376
1672
  if (same)
 
1673
  {
 
1674
    if (IS_ZEBRA_DEBUG_RIB)
 
1675
    {
 
1676
      zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
 
1677
        __func__, rn, same);
 
1678
      rib_dump (__func__, p, same);
 
1679
    }
1377
1680
    rib_delnode (rn, same);
 
1681
  }
1378
1682
  
1379
1683
  route_unlock_node (rn);
1380
1684
  return 0;
1539
1843
      switch (si->type)
1540
1844
        {
1541
1845
          case STATIC_IPV4_GATEWAY:
1542
 
            nexthop_ipv4_add (rib, &si->gate.ipv4);
 
1846
            nexthop_ipv4_add (rib, &si->gate.ipv4, NULL);
1543
1847
            break;
1544
1848
          case STATIC_IPV4_IFNAME:
1545
1849
            nexthop_ifname_add (rib, si->gate.ifname);
1563
1867
      switch (si->type)
1564
1868
        {
1565
1869
          case STATIC_IPV4_GATEWAY:
1566
 
            nexthop_ipv4_add (rib, &si->gate.ipv4);
 
1870
            nexthop_ipv4_add (rib, &si->gate.ipv4, NULL);
1567
1871
            break;
1568
1872
          case STATIC_IPV4_IFNAME:
1569
1873
            nexthop_ifname_add (rib, si->gate.ifname);