babel_zebra.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * This file is free software: you may copy, redistribute and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 2 of the License, or (at your
  5. * option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * This file incorporates work covered by the following copyright and
  16. * permission notice:
  17. *
  18. Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
  19. Permission is hereby granted, free of charge, to any person obtaining a copy
  20. of this software and associated documentation files (the "Software"), to deal
  21. in the Software without restriction, including without limitation the rights
  22. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  23. copies of the Software, and to permit persons to whom the Software is
  24. furnished to do so, subject to the following conditions:
  25. The above copyright notice and this permission notice shall be included in
  26. all copies or substantial portions of the Software.
  27. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  30. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  32. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  33. THE SOFTWARE.
  34. */
  35. /* quagga's includes */
  36. #include <zebra.h>
  37. #include "command.h"
  38. #include "zclient.h"
  39. #include "stream.h"
  40. /* babel's includes*/
  41. #include "babel_zebra.h"
  42. #include "babel_interface.h"
  43. #include "xroute.h"
  44. #include "util.h"
  45. void babelz_zebra_init(void);
  46. /* we must use a pointer because of zclient.c's functions (new, free). */
  47. struct zclient *zclient;
  48. static int zebra_config_write (struct vty *vty);
  49. /* Debug types */
  50. static struct {
  51. int type;
  52. int str_min_len;
  53. const char *str;
  54. } debug_type[] = {
  55. {BABEL_DEBUG_COMMON, 1, "common"},
  56. {BABEL_DEBUG_KERNEL, 1, "kernel"},
  57. {BABEL_DEBUG_FILTER, 1, "filter"},
  58. {BABEL_DEBUG_TIMEOUT, 1, "timeout"},
  59. {BABEL_DEBUG_IF, 1, "interface"},
  60. {BABEL_DEBUG_ROUTE, 1, "route"},
  61. {BABEL_DEBUG_ALL, 1, "all"},
  62. {0, 0, NULL}
  63. };
  64. /* Zebra node structure. */
  65. struct cmd_node zebra_node =
  66. {
  67. ZEBRA_NODE,
  68. "%s(config-router)# ",
  69. 1 /* vtysh? yes */
  70. };
  71. /* Zebra route add and delete treatment (ipv6). */
  72. static int
  73. babel_zebra_read_ipv6 (int command, struct zclient *zclient,
  74. zebra_size_t length)
  75. {
  76. struct stream *s;
  77. struct zapi_ipv6 api;
  78. unsigned long ifindex = -1;
  79. struct in6_addr nexthop;
  80. struct prefix_ipv6 prefix;
  81. s = zclient->ibuf;
  82. ifindex = 0;
  83. memset (&nexthop, 0, sizeof (struct in6_addr));
  84. memset (&api, 0, sizeof(struct zapi_ipv6));
  85. memset (&prefix, 0, sizeof (struct prefix_ipv6));
  86. /* Type, flags, message. */
  87. api.type = stream_getc (s);
  88. api.flags = stream_getc (s);
  89. api.message = stream_getc (s);
  90. /* IPv6 prefix. */
  91. prefix.family = AF_INET6;
  92. prefix.prefixlen = stream_getc (s);
  93. stream_get (&prefix.prefix, s, PSIZE (prefix.prefixlen));
  94. /* Nexthop, ifindex, distance, metric. */
  95. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) {
  96. api.nexthop_num = stream_getc (s);
  97. stream_get (&nexthop, s, sizeof(nexthop));
  98. }
  99. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) {
  100. api.ifindex_num = stream_getc (s);
  101. ifindex = stream_getl (s);
  102. }
  103. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  104. api.distance = stream_getc (s);
  105. else
  106. api.distance = 0;
  107. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  108. api.metric = stream_getl (s);
  109. else
  110. api.metric = 0;
  111. if (command == ZEBRA_IPV6_ROUTE_ADD)
  112. babel_ipv6_route_add(&api, &prefix, ifindex, &nexthop);
  113. else
  114. babel_ipv6_route_delete(&api, &prefix, ifindex);
  115. return 0;
  116. }
  117. static int
  118. babel_zebra_read_ipv4 (int command, struct zclient *zclient,
  119. zebra_size_t length)
  120. {
  121. struct stream *s;
  122. struct zapi_ipv4 api;
  123. unsigned long ifindex = -1;
  124. struct in_addr nexthop;
  125. struct prefix_ipv4 prefix;
  126. s = zclient->ibuf;
  127. ifindex = 0;
  128. memset (&nexthop, 0, sizeof (struct in_addr));
  129. memset (&api, 0, sizeof(struct zapi_ipv4));
  130. memset (&prefix, 0, sizeof (struct prefix_ipv4));
  131. /* Type, flags, message. */
  132. api.type = stream_getc (s);
  133. api.flags = stream_getc (s);
  134. api.message = stream_getc (s);
  135. /* IPv6 prefix. */
  136. prefix.family = AF_INET;
  137. prefix.prefixlen = stream_getc (s);
  138. stream_get (&prefix.prefix, s, PSIZE (prefix.prefixlen));
  139. /* Nexthop, ifindex, distance, metric. */
  140. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) {
  141. api.nexthop_num = stream_getc (s);
  142. stream_get (&nexthop, s, sizeof(nexthop));
  143. }
  144. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) {
  145. api.ifindex_num = stream_getc (s);
  146. ifindex = stream_getl (s);
  147. }
  148. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  149. api.distance = stream_getc (s);
  150. else
  151. api.distance = 0;
  152. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  153. api.metric = stream_getl (s);
  154. else
  155. api.metric = 0;
  156. if (command == ZEBRA_IPV6_ROUTE_ADD) {
  157. babel_ipv4_route_add(&api, &prefix, ifindex, &nexthop);
  158. } else {
  159. babel_ipv4_route_delete(&api, &prefix, ifindex);
  160. }
  161. return 0;
  162. }
  163. /* [Babel Command] */
  164. DEFUN (babel_redistribute_type,
  165. babel_redistribute_type_cmd,
  166. "redistribute " QUAGGA_REDIST_STR_BABELD,
  167. "Redistribute\n"
  168. QUAGGA_REDIST_HELP_STR_BABELD)
  169. {
  170. int type;
  171. type = proto_redistnum(AFI_IP6, argv[0]);
  172. if (type < 0)
  173. type = proto_redistnum(AFI_IP, argv[0]);
  174. if (type < 0) {
  175. vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
  176. return CMD_WARNING;
  177. }
  178. zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
  179. return CMD_SUCCESS;
  180. }
  181. /* [Babel Command] */
  182. DEFUN (no_babel_redistribute_type,
  183. no_babel_redistribute_type_cmd,
  184. "no redistribute " QUAGGA_REDIST_STR_BABELD,
  185. NO_STR
  186. "Redistribute\n"
  187. QUAGGA_REDIST_HELP_STR_BABELD)
  188. {
  189. int type;
  190. type = proto_redistnum(AFI_IP6, argv[0]);
  191. if (type < 0)
  192. type = proto_redistnum(AFI_IP, argv[0]);
  193. if (type < 0) {
  194. vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
  195. return CMD_WARNING;
  196. }
  197. zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type);
  198. /* perhaps should we remove xroutes having the same type... */
  199. return CMD_SUCCESS;
  200. }
  201. #ifndef NO_DEBUG
  202. /* [Babel Command] */
  203. DEFUN (debug_babel,
  204. debug_babel_cmd,
  205. "debug babel (common|kernel|filter|timeout|interface|route|all)",
  206. "Enable debug messages for specific or all part.\n"
  207. "Babel information\n"
  208. "Common messages (default)\n"
  209. "Kernel messages\n"
  210. "Filter messages\n"
  211. "Timeout messages\n"
  212. "Interface messages\n"
  213. "Route messages\n"
  214. "All messages\n")
  215. {
  216. int i;
  217. for(i = 0; debug_type[i].str != NULL; i++) {
  218. if (strncmp (debug_type[i].str, argv[0],
  219. debug_type[i].str_min_len) == 0) {
  220. debug |= debug_type[i].type;
  221. return CMD_SUCCESS;
  222. }
  223. }
  224. vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
  225. return CMD_WARNING;
  226. }
  227. /* [Babel Command] */
  228. DEFUN (no_debug_babel,
  229. no_debug_babel_cmd,
  230. "no debug babel (common|kernel|filter|timeout|interface|route|all)",
  231. NO_STR
  232. "Disable debug messages for specific or all part.\n"
  233. "Babel information\n"
  234. "Common messages (default)\n"
  235. "Kernel messages\n"
  236. "Filter messages\n"
  237. "Timeout messages\n"
  238. "Interface messages\n"
  239. "Route messages\n"
  240. "All messages\n")
  241. {
  242. int i;
  243. for (i = 0; debug_type[i].str; i++) {
  244. if (strncmp(debug_type[i].str, argv[0],
  245. debug_type[i].str_min_len) == 0) {
  246. debug &= ~debug_type[i].type;
  247. return CMD_SUCCESS;
  248. }
  249. }
  250. vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
  251. return CMD_WARNING;
  252. }
  253. #endif /* NO_DEBUG */
  254. /* Output "debug" statement lines, if necessary. */
  255. int
  256. debug_babel_config_write (struct vty * vty)
  257. {
  258. #ifdef NO_DEBUG
  259. return 0;
  260. #else
  261. int i, lines = 0;
  262. if (debug == BABEL_DEBUG_ALL)
  263. {
  264. vty_out (vty, "debug babel all%s", VTY_NEWLINE);
  265. lines++;
  266. }
  267. else
  268. for (i = 0; debug_type[i].str != NULL; i++)
  269. if
  270. (
  271. debug_type[i].type != BABEL_DEBUG_ALL
  272. && CHECK_FLAG (debug, debug_type[i].type)
  273. )
  274. {
  275. vty_out (vty, "debug babel %s%s", debug_type[i].str, VTY_NEWLINE);
  276. lines++;
  277. }
  278. if (lines)
  279. {
  280. vty_out (vty, "!%s", VTY_NEWLINE);
  281. lines++;
  282. }
  283. return lines;
  284. #endif /* NO_DEBUG */
  285. }
  286. void babelz_zebra_init(void)
  287. {
  288. zclient = zclient_new();
  289. zclient_init(zclient, ZEBRA_ROUTE_BABEL);
  290. zclient->interface_add = babel_interface_add;
  291. zclient->interface_delete = babel_interface_delete;
  292. zclient->interface_up = babel_interface_up;
  293. zclient->interface_down = babel_interface_down;
  294. zclient->interface_address_add = babel_interface_address_add;
  295. zclient->interface_address_delete = babel_interface_address_delete;
  296. zclient->ipv4_route_add = babel_zebra_read_ipv4;
  297. zclient->ipv4_route_delete = babel_zebra_read_ipv4;
  298. zclient->ipv6_route_add = babel_zebra_read_ipv6;
  299. zclient->ipv6_route_delete = babel_zebra_read_ipv6;
  300. install_node (&zebra_node, zebra_config_write);
  301. install_element(BABEL_NODE, &babel_redistribute_type_cmd);
  302. install_element(BABEL_NODE, &no_babel_redistribute_type_cmd);
  303. install_element(ENABLE_NODE, &debug_babel_cmd);
  304. install_element(ENABLE_NODE, &no_debug_babel_cmd);
  305. install_element(CONFIG_NODE, &debug_babel_cmd);
  306. install_element(CONFIG_NODE, &no_debug_babel_cmd);
  307. }
  308. static int
  309. zebra_config_write (struct vty *vty)
  310. {
  311. if (! zclient->enable)
  312. {
  313. vty_out (vty, "no router zebra%s", VTY_NEWLINE);
  314. return 1;
  315. }
  316. else if (! zclient->redist[ZEBRA_ROUTE_BABEL])
  317. {
  318. vty_out (vty, "router zebra%s", VTY_NEWLINE);
  319. vty_out (vty, " no redistribute babel%s", VTY_NEWLINE);
  320. return 1;
  321. }
  322. return 0;
  323. }
  324. void
  325. babel_zebra_close_connexion(void)
  326. {
  327. zclient_stop(zclient);
  328. }