ripng_zebra.c 16 KB

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