pim_rpf.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. PIM for Quagga
  3. Copyright (C) 2008 Everton da Silva Marques
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING; if not, write to the
  14. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  15. MA 02110-1301 USA
  16. $QuaggaId: $Format:%an, %ai, %h$ $
  17. */
  18. #include <zebra.h>
  19. #include "log.h"
  20. #include "prefix.h"
  21. #include "memory.h"
  22. #include "pimd.h"
  23. #include "pim_rpf.h"
  24. #include "pim_pim.h"
  25. #include "pim_str.h"
  26. #include "pim_iface.h"
  27. #include "pim_zlookup.h"
  28. #include "pim_ifchannel.h"
  29. static struct in_addr pim_rpf_find_rpf_addr(struct pim_upstream *up);
  30. int pim_nexthop_lookup(struct pim_nexthop *nexthop,
  31. struct in_addr addr)
  32. {
  33. struct pim_zlookup_nexthop nexthop_tab[PIM_NEXTHOP_IFINDEX_TAB_SIZE];
  34. int num_ifindex;
  35. struct interface *ifp;
  36. int first_ifindex;
  37. num_ifindex = zclient_lookup_nexthop(qpim_zclient_lookup, nexthop_tab,
  38. PIM_NEXTHOP_IFINDEX_TAB_SIZE,
  39. addr, PIM_NEXTHOP_LOOKUP_MAX);
  40. if (num_ifindex < 1) {
  41. char addr_str[100];
  42. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  43. zlog_warn("%s %s: could not find nexthop ifindex for address %s",
  44. __FILE__, __PRETTY_FUNCTION__,
  45. addr_str);
  46. return -1;
  47. }
  48. first_ifindex = nexthop_tab[0].ifindex;
  49. if (num_ifindex > 1) {
  50. char addr_str[100];
  51. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  52. zlog_info("%s %s: FIXME ignoring multiple nexthop ifindex'es num_ifindex=%d for address %s (using only ifindex=%d)",
  53. __FILE__, __PRETTY_FUNCTION__,
  54. num_ifindex, addr_str, first_ifindex);
  55. /* debug warning only, do not return */
  56. }
  57. ifp = if_lookup_by_index(first_ifindex);
  58. if (!ifp) {
  59. char addr_str[100];
  60. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  61. zlog_warn("%s %s: could not find interface for ifindex %d (address %s)",
  62. __FILE__, __PRETTY_FUNCTION__,
  63. first_ifindex, addr_str);
  64. return -2;
  65. }
  66. if (!ifp->info) {
  67. char addr_str[100];
  68. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  69. zlog_warn("%s: multicast not enabled on input interface %s (ifindex=%d, RPF for source %s)",
  70. __PRETTY_FUNCTION__,
  71. ifp->name, first_ifindex, addr_str);
  72. /* debug warning only, do not return */
  73. }
  74. if (PIM_DEBUG_PIM_TRACE) {
  75. char nexthop_str[100];
  76. char addr_str[100];
  77. pim_inet4_dump("<nexthop?>", nexthop_tab[0].nexthop_addr, nexthop_str, sizeof(nexthop_str));
  78. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  79. zlog_debug("%s %s: found nexthop %s for address %s: interface %s ifindex=%d metric=%d pref=%d",
  80. __FILE__, __PRETTY_FUNCTION__,
  81. nexthop_str, addr_str,
  82. ifp->name, first_ifindex,
  83. nexthop_tab[0].route_metric,
  84. nexthop_tab[0].protocol_distance);
  85. }
  86. /* update nextop data */
  87. nexthop->interface = ifp;
  88. nexthop->mrib_nexthop_addr = nexthop_tab[0].nexthop_addr;
  89. nexthop->mrib_metric_preference = nexthop_tab[0].protocol_distance;
  90. nexthop->mrib_route_metric = nexthop_tab[0].route_metric;
  91. return 0;
  92. }
  93. static int nexthop_mismatch(const struct pim_nexthop *nh1,
  94. const struct pim_nexthop *nh2)
  95. {
  96. return (nh1->interface != nh2->interface)
  97. ||
  98. (nh1->mrib_nexthop_addr.s_addr != nh2->mrib_nexthop_addr.s_addr)
  99. ||
  100. (nh1->mrib_metric_preference != nh2->mrib_metric_preference)
  101. ||
  102. (nh1->mrib_route_metric != nh2->mrib_route_metric);
  103. }
  104. enum pim_rpf_result pim_rpf_update(struct pim_upstream *up,
  105. struct pim_rpf *old_rpf)
  106. {
  107. struct pim_nexthop save_nexthop;
  108. struct pim_rpf save_rpf;
  109. struct pim_rpf *rpf = &up->rpf;
  110. save_nexthop = rpf->source_nexthop; /* detect change in pim_nexthop */
  111. save_rpf = up->rpf;
  112. if (pim_nexthop_lookup(&rpf->source_nexthop,
  113. up->source_addr)) {
  114. return PIM_RPF_FAILURE;
  115. }
  116. rpf->rpf_addr = pim_rpf_find_rpf_addr(up);
  117. if (PIM_INADDR_IS_ANY(rpf->rpf_addr) && PIM_DEBUG_PIM_EVENTS) {
  118. /* RPF'(S,G) not found */
  119. char src_str[100];
  120. char grp_str[100];
  121. pim_inet4_dump("<src?>", up->source_addr, src_str, sizeof(src_str));
  122. pim_inet4_dump("<grp?>", up->group_addr, grp_str, sizeof(grp_str));
  123. zlog_debug("%s %s: RPF'(%s,%s) not found: won't send join upstream",
  124. __FILE__, __PRETTY_FUNCTION__,
  125. src_str, grp_str);
  126. /* warning only */
  127. }
  128. /* detect change in pim_nexthop */
  129. if (nexthop_mismatch(&rpf->source_nexthop, &save_nexthop)) {
  130. if (PIM_DEBUG_PIM_EVENTS) {
  131. char src_str[100];
  132. char grp_str[100];
  133. char nhaddr_str[100];
  134. pim_inet4_dump("<src?>", up->source_addr, src_str, sizeof(src_str));
  135. pim_inet4_dump("<grp?>", up->group_addr, grp_str, sizeof(grp_str));
  136. pim_inet4_dump("<addr?>", rpf->source_nexthop.mrib_nexthop_addr, nhaddr_str, sizeof(nhaddr_str));
  137. zlog_debug("%s %s: (S,G)=(%s,%s) source nexthop now is: interface=%s address=%s pref=%d metric=%d",
  138. __FILE__, __PRETTY_FUNCTION__,
  139. src_str, grp_str,
  140. rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<ifname?>",
  141. nhaddr_str,
  142. rpf->source_nexthop.mrib_metric_preference,
  143. rpf->source_nexthop.mrib_route_metric);
  144. /* warning only */
  145. }
  146. pim_upstream_update_join_desired(up);
  147. pim_upstream_update_could_assert(up);
  148. pim_upstream_update_my_assert_metric(up);
  149. }
  150. /* detect change in RPF_interface(S) */
  151. if (save_nexthop.interface != rpf->source_nexthop.interface) {
  152. if (PIM_DEBUG_PIM_EVENTS) {
  153. char src_str[100];
  154. char grp_str[100];
  155. pim_inet4_dump("<src?>", up->source_addr, src_str, sizeof(src_str));
  156. pim_inet4_dump("<grp?>", up->group_addr, grp_str, sizeof(grp_str));
  157. zlog_debug("%s %s: (S,G)=(%s,%s) RPF_interface(S) changed from %s to %s",
  158. __FILE__, __PRETTY_FUNCTION__,
  159. src_str, grp_str,
  160. save_nexthop.interface ? save_nexthop.interface->name : "<oldif?>",
  161. rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<newif?>");
  162. /* warning only */
  163. }
  164. pim_upstream_rpf_interface_changed(up, save_nexthop.interface);
  165. }
  166. /* detect change in RPF'(S,G) */
  167. if (save_rpf.rpf_addr.s_addr != rpf->rpf_addr.s_addr) {
  168. /* return old rpf to caller ? */
  169. if (old_rpf)
  170. *old_rpf = save_rpf;
  171. return PIM_RPF_CHANGED;
  172. }
  173. return PIM_RPF_OK;
  174. }
  175. /*
  176. RFC 4601: 4.1.6. State Summarization Macros
  177. neighbor RPF'(S,G) {
  178. if ( I_Am_Assert_Loser(S, G, RPF_interface(S) )) {
  179. return AssertWinner(S, G, RPF_interface(S) )
  180. } else {
  181. return NBR( RPF_interface(S), MRIB.next_hop( S ) )
  182. }
  183. }
  184. RPF'(*,G) and RPF'(S,G) indicate the neighbor from which data
  185. packets should be coming and to which joins should be sent on the RP
  186. tree and SPT, respectively.
  187. */
  188. static struct in_addr pim_rpf_find_rpf_addr(struct pim_upstream *up)
  189. {
  190. struct pim_ifchannel *rpf_ch;
  191. struct pim_neighbor *neigh;
  192. struct in_addr rpf_addr;
  193. if (!up->rpf.source_nexthop.interface) {
  194. char src_str[100];
  195. char grp_str[100];
  196. pim_inet4_dump("<src?>", up->source_addr, src_str, sizeof(src_str));
  197. pim_inet4_dump("<grp?>", up->group_addr, grp_str, sizeof(grp_str));
  198. zlog_warn("%s: missing RPF interface for upstream (S,G)=(%s,%s)",
  199. __PRETTY_FUNCTION__,
  200. src_str, grp_str);
  201. rpf_addr.s_addr = PIM_NET_INADDR_ANY;
  202. return rpf_addr;
  203. }
  204. rpf_ch = pim_ifchannel_find(up->rpf.source_nexthop.interface,
  205. up->source_addr, up->group_addr);
  206. if (rpf_ch) {
  207. if (rpf_ch->ifassert_state == PIM_IFASSERT_I_AM_LOSER) {
  208. return rpf_ch->ifassert_winner;
  209. }
  210. }
  211. /* return NBR( RPF_interface(S), MRIB.next_hop( S ) ) */
  212. neigh = pim_if_find_neighbor(up->rpf.source_nexthop.interface,
  213. up->rpf.source_nexthop.mrib_nexthop_addr);
  214. if (neigh)
  215. rpf_addr = neigh->source_addr;
  216. else
  217. rpf_addr.s_addr = PIM_NET_INADDR_ANY;
  218. return rpf_addr;
  219. }