ripng_zebra.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * RIPngd and zebra interface.
  3. * Copyright (C) 1998, 1999 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "command.h"
  24. #include "prefix.h"
  25. #include "stream.h"
  26. #include "routemap.h"
  27. #include "zclient.h"
  28. #include "log.h"
  29. #include "ripngd/ripngd.h"
  30. /* All information about zebra. */
  31. struct zclient *zclient = NULL;
  32. /* Callback prototypes for zebra client service. */
  33. int ripng_interface_up (int, struct zclient *, zebra_size_t);
  34. int ripng_interface_down (int, struct zclient *, zebra_size_t);
  35. int ripng_interface_add (int, struct zclient *, zebra_size_t);
  36. int ripng_interface_delete (int, struct zclient *, zebra_size_t);
  37. int ripng_interface_address_add (int, struct zclient *, zebra_size_t);
  38. int ripng_interface_address_delete (int, struct zclient *, zebra_size_t);
  39. void
  40. ripng_zebra_ipv6_add (struct prefix_ipv6 *p, struct in6_addr *nexthop,
  41. unsigned int ifindex, u_char metric)
  42. {
  43. struct zapi_ipv6 api;
  44. if (zclient->redist[ZEBRA_ROUTE_RIPNG])
  45. {
  46. api.type = ZEBRA_ROUTE_RIPNG;
  47. api.flags = 0;
  48. api.message = 0;
  49. SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
  50. api.nexthop_num = 1;
  51. api.nexthop = &nexthop;
  52. SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
  53. api.ifindex_num = 1;
  54. api.ifindex = &ifindex;
  55. SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
  56. api.metric = metric;
  57. zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD, zclient, p, &api);
  58. }
  59. }
  60. void
  61. ripng_zebra_ipv6_delete (struct prefix_ipv6 *p, struct in6_addr *nexthop,
  62. unsigned int ifindex)
  63. {
  64. struct zapi_ipv6 api;
  65. if (zclient->redist[ZEBRA_ROUTE_RIPNG])
  66. {
  67. api.type = ZEBRA_ROUTE_RIPNG;
  68. api.flags = 0;
  69. api.message = 0;
  70. SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
  71. api.nexthop_num = 1;
  72. api.nexthop = &nexthop;
  73. SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
  74. api.ifindex_num = 1;
  75. api.ifindex = &ifindex;
  76. zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient, p, &api);
  77. }
  78. }
  79. /* Zebra route add and delete treatment. */
  80. int
  81. ripng_zebra_read_ipv6 (int command, struct zclient *zclient,
  82. zebra_size_t length)
  83. {
  84. struct stream *s;
  85. struct zapi_ipv6 api;
  86. unsigned long ifindex;
  87. struct in6_addr nexthop;
  88. struct prefix_ipv6 p;
  89. s = zclient->ibuf;
  90. ifindex = 0;
  91. memset (&nexthop, 0, sizeof (struct in6_addr));
  92. /* Type, flags, message. */
  93. api.type = stream_getc (s);
  94. api.flags = stream_getc (s);
  95. api.message = stream_getc (s);
  96. /* IPv6 prefix. */
  97. memset (&p, 0, sizeof (struct prefix_ipv6));
  98. p.family = AF_INET6;
  99. p.prefixlen = stream_getc (s);
  100. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  101. /* Nexthop, ifindex, distance, metric. */
  102. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  103. {
  104. api.nexthop_num = stream_getc (s);
  105. stream_get (&nexthop, s, 16);
  106. }
  107. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
  108. {
  109. api.ifindex_num = stream_getc (s);
  110. ifindex = stream_getl (s);
  111. }
  112. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  113. api.distance = stream_getc (s);
  114. else
  115. api.distance = 0;
  116. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  117. api.metric = stream_getl (s);
  118. else
  119. api.metric = 0;
  120. if (command == ZEBRA_IPV6_ROUTE_ADD)
  121. ripng_redistribute_add (api.type, RIPNG_ROUTE_REDISTRIBUTE, &p, ifindex, &nexthop);
  122. else
  123. ripng_redistribute_delete (api.type, RIPNG_ROUTE_REDISTRIBUTE, &p, ifindex);
  124. return 0;
  125. }
  126. void
  127. ripng_zclient_reset ()
  128. {
  129. zclient_reset (zclient);
  130. }
  131. int
  132. ripng_redistribute_unset (int type)
  133. {
  134. if (! zclient->redist[type])
  135. return CMD_SUCCESS;
  136. zclient->redist[type] = 0;
  137. if (zclient->sock > 0)
  138. zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, type);
  139. ripng_redistribute_withdraw (type);
  140. return CMD_SUCCESS;
  141. }
  142. int
  143. ripng_redistribute_check (int type)
  144. {
  145. return (zclient->redist[type]);
  146. }
  147. void
  148. ripng_redistribute_metric_set (int type, int metric)
  149. {
  150. ripng->route_map[type].metric_config = 1;
  151. ripng->route_map[type].metric = metric;
  152. }
  153. int
  154. ripng_redistribute_metric_unset (int type)
  155. {
  156. ripng->route_map[type].metric_config = 0;
  157. ripng->route_map[type].metric = 0;
  158. return 0;
  159. }
  160. void
  161. ripng_redistribute_routemap_set (int type, const char *name)
  162. {
  163. if (ripng->route_map[type].name)
  164. free (ripng->route_map[type].name);
  165. ripng->route_map[type].name = strdup (name);
  166. ripng->route_map[type].map = route_map_lookup_by_name (name);
  167. }
  168. void
  169. ripng_redistribute_routemap_unset (int type)
  170. {
  171. if (ripng->route_map[type].name)
  172. free (ripng->route_map[type].name);
  173. ripng->route_map[type].name = NULL;
  174. ripng->route_map[type].map = NULL;
  175. }
  176. /* Redistribution types */
  177. static struct {
  178. int type;
  179. int str_min_len;
  180. const char *str;
  181. } redist_type[] = {
  182. {ZEBRA_ROUTE_KERNEL, 1, "kernel"},
  183. {ZEBRA_ROUTE_CONNECT, 1, "connected"},
  184. {ZEBRA_ROUTE_STATIC, 1, "static"},
  185. {ZEBRA_ROUTE_OSPF6, 1, "ospf6"},
  186. {ZEBRA_ROUTE_BGP, 1, "bgp"},
  187. {0, 0, NULL}
  188. };
  189. void
  190. ripng_redistribute_clean ()
  191. {
  192. int i;
  193. for (i = 0; redist_type[i].str; i++)
  194. {
  195. if (zclient->redist[redist_type[i].type])
  196. {
  197. if (zclient->sock > 0)
  198. zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE,
  199. zclient, redist_type[i].type);
  200. zclient->redist[redist_type[i].type] = 0;
  201. /* Remove the routes from RIPng table. */
  202. ripng_redistribute_withdraw (redist_type[i].type);
  203. }
  204. }
  205. }
  206. DEFUN (router_zebra,
  207. router_zebra_cmd,
  208. "router zebra",
  209. "Enable a routing process\n"
  210. "Make connection to zebra daemon\n")
  211. {
  212. vty->node = ZEBRA_NODE;
  213. zclient->enable = 1;
  214. zclient_start (zclient);
  215. return CMD_SUCCESS;
  216. }
  217. DEFUN (no_router_zebra,
  218. no_router_zebra_cmd,
  219. "no router zebra",
  220. NO_STR
  221. "Disable a routing process\n"
  222. "Stop connection to zebra daemon\n")
  223. {
  224. zclient->enable = 0;
  225. zclient_stop (zclient);
  226. return CMD_SUCCESS;
  227. }
  228. DEFUN (ripng_redistribute_ripng,
  229. ripng_redistribute_ripng_cmd,
  230. "redistribute ripng",
  231. "Redistribute information from another routing protocol\n"
  232. "RIPng route\n")
  233. {
  234. zclient->redist[ZEBRA_ROUTE_RIPNG] = 1;
  235. return CMD_SUCCESS;
  236. }
  237. DEFUN (no_ripng_redistribute_ripng,
  238. no_ripng_redistribute_ripng_cmd,
  239. "no redistribute ripng",
  240. NO_STR
  241. "Redistribute information from another routing protocol\n"
  242. "RIPng route\n")
  243. {
  244. zclient->redist[ZEBRA_ROUTE_RIPNG] = 0;
  245. return CMD_SUCCESS;
  246. }
  247. DEFUN (ripng_redistribute_type,
  248. ripng_redistribute_type_cmd,
  249. "redistribute (kernel|connected|static|ospf6|bgp)",
  250. "Redistribute information from another routing protocol\n"
  251. "Kernel routes\n"
  252. "Connected\n"
  253. "Static routes\n"
  254. "Open Shortest Path First (OSPFv3)\n"
  255. "Border Gateway Protocol (BGP)\n")
  256. {
  257. int i;
  258. for(i = 0; redist_type[i].str; i++)
  259. {
  260. if (strncmp (redist_type[i].str, argv[0],
  261. redist_type[i].str_min_len) == 0)
  262. {
  263. zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, redist_type[i].type);
  264. return CMD_SUCCESS;
  265. }
  266. }
  267. vty_out(vty, "Invalid type %s%s", argv[0],
  268. VTY_NEWLINE);
  269. return CMD_WARNING;
  270. }
  271. DEFUN (no_ripng_redistribute_type,
  272. no_ripng_redistribute_type_cmd,
  273. "no redistribute (kernel|connected|static|ospf6|bgp)",
  274. NO_STR
  275. "Redistribute information from another routing protocol\n"
  276. "Kernel routes\n"
  277. "Connected\n"
  278. "Static routes\n"
  279. "Open Shortest Path First (OSPFv3)\n"
  280. "Border Gateway Protocol (BGP)\n")
  281. {
  282. int i;
  283. for (i = 0; redist_type[i].str; i++)
  284. {
  285. if (strncmp(redist_type[i].str, argv[0],
  286. redist_type[i].str_min_len) == 0)
  287. {
  288. ripng_redistribute_metric_unset (redist_type[i].type);
  289. ripng_redistribute_routemap_unset (redist_type[i].type);
  290. return ripng_redistribute_unset (redist_type[i].type);
  291. }
  292. }
  293. vty_out(vty, "Invalid type %s%s", argv[0],
  294. VTY_NEWLINE);
  295. return CMD_WARNING;
  296. }
  297. DEFUN (ripng_redistribute_type_metric,
  298. ripng_redistribute_type_metric_cmd,
  299. "redistribute (kernel|connected|static|ospf6|bgp) metric <0-16>",
  300. "Redistribute information from another routing protocol\n"
  301. "Kernel routes\n"
  302. "Connected\n"
  303. "Static routes\n"
  304. "Open Shortest Path First (OSPFv3)\n"
  305. "Border Gateway Protocol (BGP)\n"
  306. "Metric\n"
  307. "Metric value\n")
  308. {
  309. int i;
  310. int metric;
  311. metric = atoi (argv[1]);
  312. for (i = 0; redist_type[i].str; i++) {
  313. if (strncmp(redist_type[i].str, argv[0],
  314. redist_type[i].str_min_len) == 0)
  315. {
  316. ripng_redistribute_metric_set (redist_type[i].type, metric);
  317. zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, redist_type[i].type);
  318. return CMD_SUCCESS;
  319. }
  320. }
  321. vty_out(vty, "Invalid type %s%s", argv[0],
  322. VTY_NEWLINE);
  323. return CMD_WARNING;
  324. }
  325. ALIAS (no_ripng_redistribute_type,
  326. no_ripng_redistribute_type_metric_cmd,
  327. "no redistribute (kernel|connected|static|ospf6|bgp) metric <0-16>",
  328. NO_STR
  329. "Redistribute information from another routing protocol\n"
  330. "Kernel routes\n"
  331. "Connected\n"
  332. "Static routes\n"
  333. "Open Shortest Path First (OSPFv3)\n"
  334. "Border Gateway Protocol (BGP)\n"
  335. "Metric\n"
  336. "Metric value\n")
  337. DEFUN (ripng_redistribute_type_routemap,
  338. ripng_redistribute_type_routemap_cmd,
  339. "redistribute (kernel|connected|static|ospf6|bgp) route-map WORD",
  340. "Redistribute information from another routing protocol\n"
  341. "Kernel routes\n"
  342. "Connected\n"
  343. "Static routes\n"
  344. "Open Shortest Path First (OSPFv3)\n"
  345. "Border Gateway Protocol (BGP)\n"
  346. "Route map reference\n"
  347. "Pointer to route-map entries\n")
  348. {
  349. int i;
  350. for (i = 0; redist_type[i].str; i++) {
  351. if (strncmp(redist_type[i].str, argv[0],
  352. redist_type[i].str_min_len) == 0)
  353. {
  354. ripng_redistribute_routemap_set (redist_type[i].type, argv[1]);
  355. zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, redist_type[i].type);
  356. return CMD_SUCCESS;
  357. }
  358. }
  359. vty_out(vty, "Invalid type %s%s", argv[0],
  360. VTY_NEWLINE);
  361. return CMD_WARNING;
  362. }
  363. ALIAS (no_ripng_redistribute_type,
  364. no_ripng_redistribute_type_routemap_cmd,
  365. "no redistribute (kernel|connected|static|ospf6|bgp) route-map WORD",
  366. NO_STR
  367. "Redistribute information from another routing protocol\n"
  368. "Kernel routes\n"
  369. "Connected\n"
  370. "Static routes\n"
  371. "Open Shortest Path First (OSPFv3)\n"
  372. "Border Gateway Protocol (BGP)\n"
  373. "Route map reference\n"
  374. "Pointer to route-map entries\n")
  375. DEFUN (ripng_redistribute_type_metric_routemap,
  376. ripng_redistribute_type_metric_routemap_cmd,
  377. "redistribute (kernel|connected|static|ospf6|bgp) metric <0-16> route-map WORD",
  378. "Redistribute information from another routing protocol\n"
  379. "Kernel routes\n"
  380. "Connected\n"
  381. "Static routes\n"
  382. "Open Shortest Path First (OSPFv3)\n"
  383. "Border Gateway Protocol (BGP)\n"
  384. "Metric\n"
  385. "Metric value\n"
  386. "Route map reference\n"
  387. "Pointer to route-map entries\n")
  388. {
  389. int i;
  390. int metric;
  391. metric = atoi (argv[1]);
  392. for (i = 0; redist_type[i].str; i++) {
  393. if (strncmp(redist_type[i].str, argv[0],
  394. redist_type[i].str_min_len) == 0)
  395. {
  396. ripng_redistribute_metric_set (redist_type[i].type, metric);
  397. ripng_redistribute_routemap_set (redist_type[i].type, argv[2]);
  398. zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, redist_type[i].type);
  399. return CMD_SUCCESS;
  400. }
  401. }
  402. vty_out(vty, "Invalid type %s%s", argv[0],
  403. VTY_NEWLINE);
  404. return CMD_WARNING;
  405. }
  406. ALIAS (no_ripng_redistribute_type,
  407. no_ripng_redistribute_type_metric_routemap_cmd,
  408. "no redistribute (kernel|connected|static|ospf6|bgp) metric <0-16> route-map WORD",
  409. NO_STR
  410. "Redistribute information from another routing protocol\n"
  411. "Kernel routes\n"
  412. "Connected\n"
  413. "Static routes\n"
  414. "Open Shortest Path First (OSPFv3)\n"
  415. "Border Gateway Protocol (BGP)\n"
  416. "Route map reference\n"
  417. "Pointer to route-map entries\n")
  418. void
  419. ripng_redistribute_write (struct vty *vty, int config_mode)
  420. {
  421. int i;
  422. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  423. if (i != zclient->redist_default && zclient->redist[i])
  424. {
  425. if (config_mode)
  426. {
  427. if (ripng->route_map[i].metric_config)
  428. {
  429. if (ripng->route_map[i].name)
  430. vty_out (vty, " redistribute %s metric %d route-map %s%s",
  431. zebra_route_string(i), ripng->route_map[i].metric,
  432. ripng->route_map[i].name, VTY_NEWLINE);
  433. else
  434. vty_out (vty, " redistribute %s metric %d%s",
  435. zebra_route_string(i), ripng->route_map[i].metric,
  436. VTY_NEWLINE);
  437. }
  438. else
  439. {
  440. if (ripng->route_map[i].name)
  441. vty_out (vty, " redistribute %s route-map %s%s",
  442. zebra_route_string(i), ripng->route_map[i].name,
  443. VTY_NEWLINE);
  444. else
  445. vty_out (vty, " redistribute %s%s", zebra_route_string(i),
  446. VTY_NEWLINE);
  447. }
  448. }
  449. else
  450. vty_out (vty, " %s", zebra_route_string(i));
  451. }
  452. }
  453. /* RIPng configuration write function. */
  454. int
  455. zebra_config_write (struct vty *vty)
  456. {
  457. if (! zclient->enable)
  458. {
  459. vty_out (vty, "no router zebra%s", VTY_NEWLINE);
  460. return 1;
  461. }
  462. else if (! zclient->redist[ZEBRA_ROUTE_RIPNG])
  463. {
  464. vty_out (vty, "router zebra%s", VTY_NEWLINE);
  465. vty_out (vty, " no redistribute ripng%s", VTY_NEWLINE);
  466. return 1;
  467. }
  468. return 0;
  469. }
  470. /* Zebra node structure. */
  471. struct cmd_node zebra_node =
  472. {
  473. ZEBRA_NODE,
  474. "%s(config-router)# ",
  475. };
  476. /* Initialize zebra structure and it's commands. */
  477. void
  478. zebra_init ()
  479. {
  480. /* Allocate zebra structure. */
  481. zclient = zclient_new ();
  482. zclient_init (zclient, ZEBRA_ROUTE_RIPNG);
  483. zclient->interface_up = ripng_interface_up;
  484. zclient->interface_down = ripng_interface_down;
  485. zclient->interface_add = ripng_interface_add;
  486. zclient->interface_delete = ripng_interface_delete;
  487. zclient->interface_address_add = ripng_interface_address_add;
  488. zclient->interface_address_delete = ripng_interface_address_delete;
  489. zclient->ipv6_route_add = ripng_zebra_read_ipv6;
  490. zclient->ipv6_route_delete = ripng_zebra_read_ipv6;
  491. /* Install zebra node. */
  492. install_node (&zebra_node, zebra_config_write);
  493. /* Install command element for zebra node. */
  494. install_element (CONFIG_NODE, &router_zebra_cmd);
  495. install_element (CONFIG_NODE, &no_router_zebra_cmd);
  496. install_default (ZEBRA_NODE);
  497. install_element (ZEBRA_NODE, &ripng_redistribute_ripng_cmd);
  498. install_element (ZEBRA_NODE, &no_ripng_redistribute_ripng_cmd);
  499. /* Install command elements to ripng node */
  500. install_element (RIPNG_NODE, &ripng_redistribute_type_cmd);
  501. install_element (RIPNG_NODE, &ripng_redistribute_type_routemap_cmd);
  502. install_element (RIPNG_NODE, &ripng_redistribute_type_metric_cmd);
  503. install_element (RIPNG_NODE, &ripng_redistribute_type_metric_routemap_cmd);
  504. install_element (RIPNG_NODE, &no_ripng_redistribute_type_cmd);
  505. install_element (RIPNG_NODE, &no_ripng_redistribute_type_routemap_cmd);
  506. install_element (RIPNG_NODE, &no_ripng_redistribute_type_metric_cmd);
  507. install_element (RIPNG_NODE, &no_ripng_redistribute_type_metric_routemap_cmd);
  508. }