nexthop.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* A generic nexthop structure
  2. * Copyright (C) 2013 Cumulus Networks, Inc.
  3. *
  4. * This file is part of Quagga.
  5. *
  6. * Quagga is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * Quagga is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Quagga; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include "prefix.h"
  23. #include "table.h"
  24. #include "memory.h"
  25. #include "str.h"
  26. #include "command.h"
  27. #include "if.h"
  28. #include "log.h"
  29. #include "sockunion.h"
  30. #include "linklist.h"
  31. #include "thread.h"
  32. #include "prefix.h"
  33. #include "nexthop.h"
  34. /* check if nexthops are same, non-recursive */
  35. int
  36. nexthop_same_no_recurse (struct nexthop *next1, struct nexthop *next2)
  37. {
  38. if (next1->type != next2->type)
  39. return 0;
  40. switch (next1->type)
  41. {
  42. case NEXTHOP_TYPE_IPV4:
  43. case NEXTHOP_TYPE_IPV4_IFINDEX:
  44. if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4))
  45. return 0;
  46. if (next1->ifindex && (next1->ifindex != next2->ifindex))
  47. return 0;
  48. break;
  49. case NEXTHOP_TYPE_IFINDEX:
  50. case NEXTHOP_TYPE_IFNAME:
  51. if (next1->ifindex != next2->ifindex)
  52. return 0;
  53. break;
  54. #ifdef HAVE_IPV6
  55. case NEXTHOP_TYPE_IPV6:
  56. if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
  57. return 0;
  58. break;
  59. case NEXTHOP_TYPE_IPV6_IFINDEX:
  60. case NEXTHOP_TYPE_IPV6_IFNAME:
  61. if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
  62. return 0;
  63. if (next1->ifindex != next2->ifindex)
  64. return 0;
  65. break;
  66. #endif /* HAVE_IPV6 */
  67. default:
  68. /* do nothing */
  69. break;
  70. }
  71. return 1;
  72. }
  73. /*
  74. * nexthop_type_to_str
  75. */
  76. const char *
  77. nexthop_type_to_str (enum nexthop_types_t nh_type)
  78. {
  79. static const char *desc[] = {
  80. "none",
  81. "Directly connected",
  82. "Interface route",
  83. "IPv4 nexthop",
  84. "IPv4 nexthop with ifindex",
  85. "IPv4 nexthop with ifname",
  86. "IPv6 nexthop",
  87. "IPv6 nexthop with ifindex",
  88. "IPv6 nexthop with ifname",
  89. "Null0 nexthop",
  90. };
  91. if (nh_type >= ZEBRA_NUM_OF (desc))
  92. return "<Invalid nh type>";
  93. return desc[nh_type];
  94. }
  95. struct nexthop *
  96. nexthop_new (void)
  97. {
  98. return XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  99. }
  100. /* Add nexthop to the end of a nexthop list. */
  101. void
  102. nexthop_add (struct nexthop **target, struct nexthop *nexthop)
  103. {
  104. struct nexthop *last;
  105. for (last = *target; last && last->next; last = last->next)
  106. ;
  107. if (last)
  108. last->next = nexthop;
  109. else
  110. *target = nexthop;
  111. nexthop->prev = last;
  112. }
  113. void
  114. copy_nexthops (struct nexthop **tnh, struct nexthop *nh)
  115. {
  116. struct nexthop *nexthop;
  117. struct nexthop *nh1;
  118. for (nh1 = nh; nh1; nh1 = nh1->next)
  119. {
  120. nexthop = nexthop_new();
  121. nexthop->flags = nh->flags;
  122. nexthop->type = nh->type;
  123. nexthop->ifindex = nh->ifindex;
  124. if (nh->ifname)
  125. nexthop->ifname = XSTRDUP(0, nh->ifname);
  126. memcpy(&(nexthop->gate), &(nh->gate), sizeof(union g_addr));
  127. memcpy(&(nexthop->src), &(nh->src), sizeof(union g_addr));
  128. nexthop_add(tnh, nexthop);
  129. if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_RECURSIVE))
  130. copy_nexthops(&nexthop->resolved, nh1->resolved);
  131. }
  132. }
  133. /* Free nexthop. */
  134. void
  135. nexthop_free (struct nexthop *nexthop)
  136. {
  137. if (nexthop->ifname)
  138. XFREE (0, nexthop->ifname);
  139. if (nexthop->resolved)
  140. nexthops_free(nexthop->resolved);
  141. XFREE (MTYPE_NEXTHOP, nexthop);
  142. }
  143. /* Frees a list of nexthops */
  144. void
  145. nexthops_free (struct nexthop *nexthop)
  146. {
  147. struct nexthop *nh, *next;
  148. for (nh = nexthop; nh; nh = next)
  149. {
  150. next = nh->next;
  151. nexthop_free (nh);
  152. }
  153. }