pim_static.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. PIM for Quagga: add the ability to configure multicast static routes
  3. Copyright (C) 2014 Nathan Bahr, ATCorp
  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 "pim_static.h"
  20. #include "pim_time.h"
  21. #include "pim_str.h"
  22. #include "pimd.h"
  23. #include "pim_iface.h"
  24. #include "log.h"
  25. #include "memory.h"
  26. #include "linklist.h"
  27. void pim_static_route_free(struct static_route *s_route)
  28. {
  29. XFREE(MTYPE_PIM_STATIC_ROUTE, s_route);
  30. }
  31. static struct static_route * static_route_alloc()
  32. {
  33. struct static_route *s_route;
  34. s_route = XCALLOC(MTYPE_PIM_STATIC_ROUTE, sizeof(*s_route));
  35. if (!s_route) {
  36. zlog_err("PIM XCALLOC(%lu) failure", sizeof(*s_route));
  37. return 0;
  38. }
  39. return s_route;
  40. }
  41. static struct static_route *static_route_new(unsigned int iif,
  42. unsigned int oif,
  43. struct in_addr group,
  44. struct in_addr source)
  45. {
  46. struct static_route * s_route;
  47. s_route = static_route_alloc();
  48. if (!s_route) {
  49. return 0;
  50. }
  51. s_route->group = group;
  52. s_route->source = source;
  53. s_route->iif = iif;
  54. s_route->oif_ttls[oif] = 1;
  55. s_route->oif_count = 1;
  56. s_route->mc.mfcc_origin = source;
  57. s_route->mc.mfcc_mcastgrp = group;
  58. s_route->mc.mfcc_parent = iif;
  59. s_route->mc.mfcc_ttls[oif] = 1;
  60. s_route->creation[oif] = pim_time_monotonic_sec();
  61. return s_route;
  62. }
  63. int pim_static_add(struct interface *iif, struct interface *oif, struct in_addr group, struct in_addr source)
  64. {
  65. struct listnode *node = 0;
  66. struct static_route *s_route = 0;
  67. struct static_route *original_s_route = 0;
  68. struct pim_interface *pim_iif = iif ? iif->info : 0;
  69. struct pim_interface *pim_oif = oif ? oif->info : 0;
  70. unsigned int iif_index = pim_iif ? pim_iif->mroute_vif_index : 0;
  71. unsigned int oif_index = pim_oif ? pim_oif->mroute_vif_index : 0;
  72. if (!iif_index || !oif_index) {
  73. zlog_warn("%s %s: Unable to add static route: Invalid interface index(iif=%d,oif=%d)",
  74. __FILE__, __PRETTY_FUNCTION__,
  75. iif_index,
  76. oif_index);
  77. return -2;
  78. }
  79. #ifdef PIM_ENFORCE_LOOPFREE_MFC
  80. if (iif_index == oif_index) {
  81. /* looped MFC entry */
  82. zlog_warn("%s %s: Unable to add static route: Looped MFC entry(iif=%d,oif=%d)",
  83. __FILE__, __PRETTY_FUNCTION__,
  84. iif_index,
  85. oif_index);
  86. return -4;
  87. }
  88. #endif
  89. for (ALL_LIST_ELEMENTS_RO(qpim_static_route_list, node, s_route)) {
  90. if (s_route->group.s_addr == group.s_addr &&
  91. s_route->source.s_addr == source.s_addr) {
  92. if (s_route->iif == iif_index &&
  93. s_route->oif_ttls[oif_index]) {
  94. char gifaddr_str[100];
  95. char sifaddr_str[100];
  96. pim_inet4_dump("<ifaddr?>", group, gifaddr_str, sizeof(gifaddr_str));
  97. pim_inet4_dump("<ifaddr?>", source, sifaddr_str, sizeof(sifaddr_str));
  98. zlog_warn("%s %s: Unable to add static route: Route already exists (iif=%d,oif=%d,group=%s,source=%s)",
  99. __FILE__, __PRETTY_FUNCTION__,
  100. iif_index,
  101. oif_index,
  102. gifaddr_str,
  103. sifaddr_str);
  104. return -3;
  105. }
  106. /* Ok, from here on out we will be making changes to the s_route structure, but if
  107. * for some reason we fail to commit these changes to the kernel, we want to be able
  108. * restore the state of the list. So copy the node data and if need be, we can copy
  109. * back if it fails.
  110. */
  111. original_s_route = static_route_alloc();
  112. if (!original_s_route) {
  113. return -5;
  114. }
  115. memcpy(original_s_route, s_route, sizeof(struct static_route));
  116. /* Route exists and has the same input interface, but adding a new output interface */
  117. if (s_route->iif == iif_index) {
  118. s_route->oif_ttls[oif_index] = 1;
  119. s_route->mc.mfcc_ttls[oif_index] = 1;
  120. s_route->creation[oif_index] = pim_time_monotonic_sec();
  121. ++s_route->oif_count;
  122. } else {
  123. /* input interface changed */
  124. s_route->iif = iif_index;
  125. s_route->mc.mfcc_parent = iif_index;
  126. #ifdef PIM_ENFORCE_LOOPFREE_MFC
  127. /* check to make sure the new input was not an old output */
  128. if (s_route->oif_ttls[iif_index]) {
  129. s_route->oif_ttls[iif_index] = 0;
  130. s_route->creation[iif_index] = 0;
  131. s_route->mc.mfcc_ttls[iif_index] = 0;
  132. --s_route->oif_count;
  133. }
  134. #endif
  135. /* now add the new output, if it is new */
  136. if (!s_route->oif_ttls[oif_index]) {
  137. s_route->oif_ttls[oif_index] = 1;
  138. s_route->creation[oif_index] = pim_time_monotonic_sec();
  139. s_route->mc.mfcc_ttls[oif_index] = 1;
  140. ++s_route->oif_count;
  141. }
  142. }
  143. break;
  144. }
  145. }
  146. /* If node is null then we reached the end of the list without finding a match */
  147. if (!node) {
  148. s_route = static_route_new(iif_index, oif_index, group, source);
  149. listnode_add(qpim_static_route_list, s_route);
  150. }
  151. if (pim_mroute_add(&(s_route->mc)))
  152. {
  153. char gifaddr_str[100];
  154. char sifaddr_str[100];
  155. pim_inet4_dump("<ifaddr?>", group, gifaddr_str, sizeof(gifaddr_str));
  156. pim_inet4_dump("<ifaddr?>", source, sifaddr_str, sizeof(sifaddr_str));
  157. zlog_warn("%s %s: Unable to add static route(iif=%d,oif=%d,group=%s,source=%s)",
  158. __FILE__, __PRETTY_FUNCTION__,
  159. iif_index,
  160. oif_index,
  161. gifaddr_str,
  162. sifaddr_str);
  163. /* Need to put s_route back to the way it was */
  164. if (original_s_route) {
  165. memcpy(s_route, original_s_route, sizeof(struct static_route));
  166. } else {
  167. /* we never stored off a copy, so it must have been a fresh new route */
  168. listnode_delete(qpim_static_route_list, s_route);
  169. pim_static_route_free(s_route);
  170. }
  171. return -1;
  172. }
  173. /* Make sure we free the memory for the route copy if used */
  174. if (original_s_route) {
  175. pim_static_route_free(original_s_route);
  176. }
  177. if (PIM_DEBUG_STATIC) {
  178. char gifaddr_str[100];
  179. char sifaddr_str[100];
  180. pim_inet4_dump("<ifaddr?>", group, gifaddr_str, sizeof(gifaddr_str));
  181. pim_inet4_dump("<ifaddr?>", source, sifaddr_str, sizeof(sifaddr_str));
  182. zlog_debug("%s: Static route added(iif=%d,oif=%d,group=%s,source=%s)",
  183. __PRETTY_FUNCTION__,
  184. iif_index,
  185. oif_index,
  186. gifaddr_str,
  187. sifaddr_str);
  188. }
  189. return 0;
  190. }
  191. int pim_static_del(struct interface *iif, struct interface *oif, struct in_addr group, struct in_addr source)
  192. {
  193. struct listnode *node = 0;
  194. struct listnode *nextnode = 0;
  195. struct static_route *s_route = 0;
  196. struct pim_interface *pim_iif = iif ? iif->info : 0;
  197. struct pim_interface *pim_oif = oif ? oif->info : 0;
  198. unsigned int iif_index = pim_iif ? pim_iif->mroute_vif_index : 0;
  199. unsigned int oif_index = pim_oif ? pim_oif->mroute_vif_index : 0;
  200. if (!iif_index || !oif_index) {
  201. zlog_warn("%s %s: Unable to remove static route: Invalid interface index(iif=%d,oif=%d)",
  202. __FILE__, __PRETTY_FUNCTION__,
  203. iif_index,
  204. oif_index);
  205. return -2;
  206. }
  207. for (ALL_LIST_ELEMENTS(qpim_static_route_list, node, nextnode, s_route)) {
  208. if (s_route->iif == iif_index &&
  209. s_route->group.s_addr == group.s_addr &&
  210. s_route->source.s_addr == source.s_addr &&
  211. s_route->oif_ttls[oif_index]) {
  212. s_route->oif_ttls[oif_index] = 0;
  213. s_route->mc.mfcc_ttls[oif_index] = 0;
  214. --s_route->oif_count;
  215. /* If there are no more outputs then delete the whole route, otherwise set the route with the new outputs */
  216. if (s_route->oif_count <= 0 ? pim_mroute_del(&s_route->mc) : pim_mroute_add(&s_route->mc)) {
  217. char gifaddr_str[100];
  218. char sifaddr_str[100];
  219. pim_inet4_dump("<ifaddr?>", group, gifaddr_str, sizeof(gifaddr_str));
  220. pim_inet4_dump("<ifaddr?>", source, sifaddr_str, sizeof(sifaddr_str));
  221. zlog_warn("%s %s: Unable to remove static route(iif=%d,oif=%d,group=%s,source=%s)",
  222. __FILE__, __PRETTY_FUNCTION__,
  223. iif_index,
  224. oif_index,
  225. gifaddr_str,
  226. sifaddr_str);
  227. s_route->oif_ttls[oif_index] = 1;
  228. s_route->mc.mfcc_ttls[oif_index] = 1;
  229. ++s_route->oif_count;
  230. return -1;
  231. }
  232. s_route->creation[oif_index] = 0;
  233. if (s_route->oif_count <= 0) {
  234. listnode_delete(qpim_static_route_list, s_route);
  235. pim_static_route_free(s_route);
  236. }
  237. if (PIM_DEBUG_STATIC) {
  238. char gifaddr_str[100];
  239. char sifaddr_str[100];
  240. pim_inet4_dump("<ifaddr?>", group, gifaddr_str, sizeof(gifaddr_str));
  241. pim_inet4_dump("<ifaddr?>", source, sifaddr_str, sizeof(sifaddr_str));
  242. zlog_debug("%s: Static route removed(iif=%d,oif=%d,group=%s,source=%s)",
  243. __PRETTY_FUNCTION__,
  244. iif_index,
  245. oif_index,
  246. gifaddr_str,
  247. sifaddr_str);
  248. }
  249. break;
  250. }
  251. }
  252. if (!node) {
  253. char gifaddr_str[100];
  254. char sifaddr_str[100];
  255. pim_inet4_dump("<ifaddr?>", group, gifaddr_str, sizeof(gifaddr_str));
  256. pim_inet4_dump("<ifaddr?>", source, sifaddr_str, sizeof(sifaddr_str));
  257. zlog_warn("%s %s: Unable to remove static route: Route does not exist(iif=%d,oif=%d,group=%s,source=%s)",
  258. __FILE__, __PRETTY_FUNCTION__,
  259. iif_index,
  260. oif_index,
  261. gifaddr_str,
  262. sifaddr_str);
  263. return -3;
  264. }
  265. return 0;
  266. }