ripng_zebra.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 "table.h"
  26. #include "stream.h"
  27. #include "memory.h"
  28. #include "routemap.h"
  29. #include "zclient.h"
  30. #include "log.h"
  31. #include "ripngd/ripngd.h"
  32. #include "ripngd/ripng_debug.h"
  33. /* All information about zebra. */
  34. struct zclient *zclient = NULL;
  35. /* Callback prototypes for zebra client service. */
  36. int ripng_interface_up (int, struct zclient *, zebra_size_t);
  37. int ripng_interface_down (int, struct zclient *, zebra_size_t);
  38. int ripng_interface_add (int, struct zclient *, zebra_size_t);
  39. int ripng_interface_delete (int, struct zclient *, zebra_size_t);
  40. int ripng_interface_address_add (int, struct zclient *, zebra_size_t);
  41. int ripng_interface_address_delete (int, struct zclient *, zebra_size_t);
  42. /* Send ECMP routes to zebra. */
  43. static void
  44. ripng_zebra_ipv6_send (struct route_node *rp, u_char cmd)
  45. {
  46. static struct in6_addr **nexthops = NULL;
  47. static unsigned int *ifindexes = NULL;
  48. static unsigned int nexthops_len = 0;
  49. struct list *list = (struct list *)rp->info;
  50. struct zapi_ipv6 api;
  51. struct listnode *listnode = NULL;
  52. struct ripng_info *rinfo = NULL;
  53. int count = 0;
  54. if (zclient->redist[ZEBRA_ROUTE_RIPNG])
  55. {
  56. api.type = ZEBRA_ROUTE_RIPNG;
  57. api.flags = 0;
  58. api.message = 0;
  59. api.safi = SAFI_UNICAST;
  60. if (nexthops_len < listcount (list))
  61. {
  62. nexthops_len = listcount (list);
  63. nexthops = XREALLOC (MTYPE_TMP, nexthops,
  64. nexthops_len * sizeof (struct in6_addr *));
  65. ifindexes = XREALLOC (MTYPE_TMP, ifindexes,
  66. nexthops_len * sizeof (unsigned int));
  67. }
  68. SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
  69. SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
  70. for (ALL_LIST_ELEMENTS_RO (list, listnode, rinfo))
  71. {
  72. nexthops[count] = &rinfo->nexthop;
  73. ifindexes[count] = rinfo->ifindex;
  74. count++;
  75. if (cmd == ZEBRA_IPV6_ROUTE_ADD)
  76. SET_FLAG (rinfo->flags, RIPNG_RTF_FIB);
  77. else
  78. UNSET_FLAG (rinfo->flags, RIPNG_RTF_FIB);
  79. }
  80. api.nexthop = nexthops;
  81. api.nexthop_num = count;
  82. api.ifindex = ifindexes;
  83. api.ifindex_num = count;
  84. rinfo = listgetdata (listhead (list));
  85. SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
  86. api.metric = rinfo->metric;
  87. zapi_ipv6_route (cmd, zclient,
  88. (struct prefix_ipv6 *)&rp->p, &api);
  89. if (IS_RIPNG_DEBUG_ZEBRA)
  90. zlog_debug ("%s: %s/%d nexthops %d",
  91. (cmd == ZEBRA_IPV6_ROUTE_ADD) ? \
  92. "Install into zebra" : "Delete from zebra",
  93. inet6_ntoa (rp->p.u.prefix6), rp->p.prefixlen, count);
  94. }
  95. }
  96. /* Add/update ECMP routes to zebra. */
  97. void
  98. ripng_zebra_ipv6_add (struct route_node *rp)
  99. {
  100. ripng_zebra_ipv6_send (rp, ZEBRA_IPV6_ROUTE_ADD);
  101. }
  102. /* Delete ECMP routes from zebra. */
  103. void
  104. ripng_zebra_ipv6_delete (struct route_node *rp)
  105. {
  106. ripng_zebra_ipv6_send (rp, ZEBRA_IPV6_ROUTE_DELETE);
  107. }
  108. /* Zebra route add and delete treatment. */
  109. static int
  110. ripng_zebra_read_ipv6 (int command, struct zclient *zclient,
  111. zebra_size_t length)
  112. {
  113. struct stream *s;
  114. struct zapi_ipv6 api;
  115. unsigned long ifindex;
  116. struct in6_addr nexthop;
  117. struct prefix_ipv6 p;
  118. s = zclient->ibuf;
  119. ifindex = 0;
  120. memset (&nexthop, 0, sizeof (struct in6_addr));
  121. /* Type, flags, message. */
  122. api.type = stream_getc (s);
  123. api.flags = stream_getc (s);
  124. api.message = stream_getc (s);
  125. /* IPv6 prefix. */
  126. memset (&p, 0, sizeof (struct prefix_ipv6));
  127. p.family = AF_INET6;
  128. p.prefixlen = stream_getc (s);
  129. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  130. /* Nexthop, ifindex, distance, metric. */
  131. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  132. {
  133. api.nexthop_num = stream_getc (s);
  134. stream_get (&nexthop, s, 16);
  135. }
  136. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
  137. {
  138. api.ifindex_num = stream_getc (s);
  139. ifindex = stream_getl (s);
  140. }
  141. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  142. api.distance = stream_getc (s);
  143. else
  144. api.distance = 0;
  145. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  146. api.metric = stream_getl (s);
  147. else
  148. api.metric = 0;
  149. if (command == ZEBRA_IPV6_ROUTE_ADD)
  150. ripng_redistribute_add (api.type, RIPNG_ROUTE_REDISTRIBUTE, &p, ifindex, &nexthop);
  151. else
  152. ripng_redistribute_delete (api.type, RIPNG_ROUTE_REDISTRIBUTE, &p, ifindex);
  153. return 0;
  154. }
  155. void
  156. ripng_zclient_reset (void)
  157. {
  158. zclient_reset (zclient);
  159. }
  160. static int
  161. ripng_redistribute_unset (int type)
  162. {
  163. if (! zclient->redist[type])
  164. return CMD_SUCCESS;
  165. zclient->redist[type] = 0;
  166. if (zclient->sock > 0)
  167. zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, type);
  168. ripng_redistribute_withdraw (type);
  169. return CMD_SUCCESS;
  170. }
  171. int
  172. ripng_redistribute_check (int type)
  173. {
  174. return (zclient->redist[type]);
  175. }
  176. static void
  177. ripng_redistribute_metric_set (int type, int metric)
  178. {
  179. ripng->route_map[type].metric_config = 1;
  180. ripng->route_map[type].metric = metric;
  181. }
  182. static int
  183. ripng_redistribute_metric_unset (int type)
  184. {
  185. ripng->route_map[type].metric_config = 0;
  186. ripng->route_map[type].metric = 0;
  187. return 0;
  188. }
  189. static void
  190. ripng_redistribute_routemap_set (int type, const char *name)
  191. {
  192. if (ripng->route_map[type].name)
  193. free (ripng->route_map[type].name);
  194. ripng->route_map[type].name = strdup (name);
  195. ripng->route_map[type].map = route_map_lookup_by_name (name);
  196. }
  197. static void
  198. ripng_redistribute_routemap_unset (int type)
  199. {
  200. if (ripng->route_map[type].name)
  201. free (ripng->route_map[type].name);
  202. ripng->route_map[type].name = NULL;
  203. ripng->route_map[type].map = NULL;
  204. }
  205. /* Redistribution types */
  206. static struct {
  207. int type;
  208. int str_min_len;
  209. const char *str;
  210. } redist_type[] = {
  211. {ZEBRA_ROUTE_KERNEL, 1, "kernel"},
  212. {ZEBRA_ROUTE_CONNECT, 1, "connected"},
  213. {ZEBRA_ROUTE_STATIC, 1, "static"},
  214. {ZEBRA_ROUTE_OSPF6, 1, "ospf6"},
  215. {ZEBRA_ROUTE_BGP, 2, "bgp"},
  216. {ZEBRA_ROUTE_BABEL, 2, "babel"},
  217. {0, 0, NULL}
  218. };
  219. void
  220. ripng_redistribute_clean ()
  221. {
  222. int i;
  223. for (i = 0; redist_type[i].str; i++)
  224. {
  225. if (zclient->redist[redist_type[i].type])
  226. {
  227. if (zclient->sock > 0)
  228. zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE,
  229. zclient, redist_type[i].type);
  230. zclient->redist[redist_type[i].type] = 0;
  231. /* Remove the routes from RIPng table. */
  232. ripng_redistribute_withdraw (redist_type[i].type);
  233. }
  234. }
  235. }
  236. DEFUN (router_zebra,
  237. router_zebra_cmd,
  238. "router zebra",
  239. "Enable a routing process\n"
  240. "Make connection to zebra daemon\n")
  241. {
  242. vty->node = ZEBRA_NODE;
  243. zclient->enable = 1;
  244. zclient_start (zclient);
  245. return CMD_SUCCESS;
  246. }
  247. DEFUN (no_router_zebra,
  248. no_router_zebra_cmd,
  249. "no router zebra",
  250. NO_STR
  251. "Disable a routing process\n"
  252. "Stop connection to zebra daemon\n")
  253. {
  254. zclient->enable = 0;
  255. zclient_stop (zclient);
  256. return CMD_SUCCESS;
  257. }
  258. DEFUN (ripng_redistribute_ripng,
  259. ripng_redistribute_ripng_cmd,
  260. "redistribute ripng",
  261. "Redistribute information from another routing protocol\n"
  262. "RIPng route\n")
  263. {
  264. zclient->redist[ZEBRA_ROUTE_RIPNG] = 1;
  265. return CMD_SUCCESS;
  266. }
  267. DEFUN (no_ripng_redistribute_ripng,
  268. no_ripng_redistribute_ripng_cmd,
  269. "no redistribute ripng",
  270. NO_STR
  271. "Redistribute information from another routing protocol\n"
  272. "RIPng route\n")
  273. {
  274. zclient->redist[ZEBRA_ROUTE_RIPNG] = 0;
  275. return CMD_SUCCESS;
  276. }
  277. DEFUN (ripng_redistribute_type,
  278. ripng_redistribute_type_cmd,
  279. "redistribute " QUAGGA_REDIST_STR_RIPNGD,
  280. "Redistribute\n"
  281. QUAGGA_REDIST_HELP_STR_RIPNGD)
  282. {
  283. int type;
  284. type = proto_redistnum(AFI_IP6, argv[0]);
  285. if (type < 0)
  286. {
  287. vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
  288. return CMD_WARNING;
  289. }
  290. zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
  291. return CMD_SUCCESS;
  292. }
  293. DEFUN (no_ripng_redistribute_type,
  294. no_ripng_redistribute_type_cmd,
  295. "no redistribute " QUAGGA_REDIST_STR_RIPNGD,
  296. NO_STR
  297. "Redistribute\n"
  298. QUAGGA_REDIST_HELP_STR_RIPNGD)
  299. {
  300. int type;
  301. type = proto_redistnum(AFI_IP6, argv[0]);
  302. if (type < 0)
  303. {
  304. vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
  305. return CMD_WARNING;
  306. }
  307. ripng_redistribute_metric_unset (type);
  308. ripng_redistribute_routemap_unset (type);
  309. return ripng_redistribute_unset (type);
  310. }
  311. DEFUN (ripng_redistribute_type_metric,
  312. ripng_redistribute_type_metric_cmd,
  313. "redistribute " QUAGGA_REDIST_STR_RIPNGD " metric <0-16>",
  314. "Redistribute\n"
  315. QUAGGA_REDIST_HELP_STR_RIPNGD
  316. "Metric\n"
  317. "Metric value\n")
  318. {
  319. int type;
  320. int metric;
  321. metric = atoi (argv[1]);
  322. type = proto_redistnum(AFI_IP6, argv[0]);
  323. if (type < 0)
  324. {
  325. vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
  326. return CMD_WARNING;
  327. }
  328. ripng_redistribute_metric_set (type, metric);
  329. zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
  330. return CMD_SUCCESS;
  331. }
  332. ALIAS (no_ripng_redistribute_type,
  333. no_ripng_redistribute_type_metric_cmd,
  334. "no redistribute " QUAGGA_REDIST_STR_RIPNGD " metric <0-16>",
  335. NO_STR
  336. "Redistribute\n"
  337. QUAGGA_REDIST_HELP_STR_RIPNGD
  338. "Metric\n"
  339. "Metric value\n")
  340. DEFUN (ripng_redistribute_type_routemap,
  341. ripng_redistribute_type_routemap_cmd,
  342. "redistribute " QUAGGA_REDIST_STR_RIPNGD " route-map WORD",
  343. "Redistribute\n"
  344. QUAGGA_REDIST_HELP_STR_RIPNGD
  345. "Route map reference\n"
  346. "Pointer to route-map entries\n")
  347. {
  348. int type;
  349. type = proto_redistnum(AFI_IP6, argv[0]);
  350. if (type < 0)
  351. {
  352. vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
  353. return CMD_WARNING;
  354. }
  355. ripng_redistribute_routemap_set (type, argv[1]);
  356. zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
  357. return CMD_SUCCESS;
  358. }
  359. ALIAS (no_ripng_redistribute_type,
  360. no_ripng_redistribute_type_routemap_cmd,
  361. "no redistribute " QUAGGA_REDIST_STR_RIPNGD " route-map WORD",
  362. NO_STR
  363. "Redistribute\n"
  364. QUAGGA_REDIST_HELP_STR_RIPNGD
  365. "Route map reference\n"
  366. "Pointer to route-map entries\n")
  367. DEFUN (ripng_redistribute_type_metric_routemap,
  368. ripng_redistribute_type_metric_routemap_cmd,
  369. "redistribute " QUAGGA_REDIST_STR_RIPNGD " metric <0-16> route-map WORD",
  370. "Redistribute\n"
  371. QUAGGA_REDIST_HELP_STR_RIPNGD
  372. "Metric\n"
  373. "Metric value\n"
  374. "Route map reference\n"
  375. "Pointer to route-map entries\n")
  376. {
  377. int type;
  378. int metric;
  379. type = proto_redistnum(AFI_IP6, argv[0]);
  380. metric = atoi (argv[1]);
  381. if (type < 0)
  382. {
  383. vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
  384. return CMD_WARNING;
  385. }
  386. ripng_redistribute_metric_set (type, metric);
  387. ripng_redistribute_routemap_set (type, argv[2]);
  388. zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
  389. return CMD_SUCCESS;
  390. }
  391. ALIAS (no_ripng_redistribute_type,
  392. no_ripng_redistribute_type_metric_routemap_cmd,
  393. "no redistribute " QUAGGA_REDIST_STR_RIPNGD " metric <0-16> route-map WORD",
  394. NO_STR
  395. "Redistribute\n"
  396. QUAGGA_REDIST_HELP_STR_RIPNGD
  397. "Route map reference\n"
  398. "Pointer to route-map entries\n")
  399. void
  400. ripng_redistribute_write (struct vty *vty, int config_mode)
  401. {
  402. int i;
  403. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  404. if (i != zclient->redist_default && zclient->redist[i])
  405. {
  406. if (config_mode)
  407. {
  408. if (ripng->route_map[i].metric_config)
  409. {
  410. if (ripng->route_map[i].name)
  411. vty_out (vty, " redistribute %s metric %d route-map %s%s",
  412. zebra_route_string(i), ripng->route_map[i].metric,
  413. ripng->route_map[i].name, VTY_NEWLINE);
  414. else
  415. vty_out (vty, " redistribute %s metric %d%s",
  416. zebra_route_string(i), ripng->route_map[i].metric,
  417. VTY_NEWLINE);
  418. }
  419. else
  420. {
  421. if (ripng->route_map[i].name)
  422. vty_out (vty, " redistribute %s route-map %s%s",
  423. zebra_route_string(i), ripng->route_map[i].name,
  424. VTY_NEWLINE);
  425. else
  426. vty_out (vty, " redistribute %s%s", zebra_route_string(i),
  427. VTY_NEWLINE);
  428. }
  429. }
  430. else
  431. vty_out (vty, " %s", zebra_route_string(i));
  432. }
  433. }
  434. /* RIPng configuration write function. */
  435. static int
  436. zebra_config_write (struct vty *vty)
  437. {
  438. if (! zclient->enable)
  439. {
  440. vty_out (vty, "no router zebra%s", VTY_NEWLINE);
  441. return 1;
  442. }
  443. else if (! zclient->redist[ZEBRA_ROUTE_RIPNG])
  444. {
  445. vty_out (vty, "router zebra%s", VTY_NEWLINE);
  446. vty_out (vty, " no redistribute ripng%s", VTY_NEWLINE);
  447. return 1;
  448. }
  449. return 0;
  450. }
  451. /* Zebra node structure. */
  452. static struct cmd_node zebra_node =
  453. {
  454. ZEBRA_NODE,
  455. "%s(config-router)# ",
  456. };
  457. /* Initialize zebra structure and it's commands. */
  458. void
  459. zebra_init ()
  460. {
  461. /* Allocate zebra structure. */
  462. zclient = zclient_new ();
  463. zclient_init (zclient, ZEBRA_ROUTE_RIPNG);
  464. zclient->interface_up = ripng_interface_up;
  465. zclient->interface_down = ripng_interface_down;
  466. zclient->interface_add = ripng_interface_add;
  467. zclient->interface_delete = ripng_interface_delete;
  468. zclient->interface_address_add = ripng_interface_address_add;
  469. zclient->interface_address_delete = ripng_interface_address_delete;
  470. zclient->ipv6_route_add = ripng_zebra_read_ipv6;
  471. zclient->ipv6_route_delete = ripng_zebra_read_ipv6;
  472. /* Install zebra node. */
  473. install_node (&zebra_node, zebra_config_write);
  474. /* Install command element for zebra node. */
  475. install_element (CONFIG_NODE, &router_zebra_cmd);
  476. install_element (CONFIG_NODE, &no_router_zebra_cmd);
  477. install_default (ZEBRA_NODE);
  478. install_element (ZEBRA_NODE, &ripng_redistribute_ripng_cmd);
  479. install_element (ZEBRA_NODE, &no_ripng_redistribute_ripng_cmd);
  480. /* Install command elements to ripng node */
  481. install_element (RIPNG_NODE, &ripng_redistribute_type_cmd);
  482. install_element (RIPNG_NODE, &ripng_redistribute_type_routemap_cmd);
  483. install_element (RIPNG_NODE, &ripng_redistribute_type_metric_cmd);
  484. install_element (RIPNG_NODE, &ripng_redistribute_type_metric_routemap_cmd);
  485. install_element (RIPNG_NODE, &no_ripng_redistribute_type_cmd);
  486. install_element (RIPNG_NODE, &no_ripng_redistribute_type_routemap_cmd);
  487. install_element (RIPNG_NODE, &no_ripng_redistribute_type_metric_cmd);
  488. install_element (RIPNG_NODE, &no_ripng_redistribute_type_metric_routemap_cmd);
  489. }