pim_static.c 11 KB

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