ripng_zebra.c 16 KB

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