ospf6_zebra.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * Copyright (C) 2003 Yasuhiro Ohara
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  6. * GNU Zebra is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * GNU Zebra is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GNU Zebra; see the file COPYING. If not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include "log.h"
  23. #include "vty.h"
  24. #include "command.h"
  25. #include "prefix.h"
  26. #include "stream.h"
  27. #include "zclient.h"
  28. #include "memory.h"
  29. #include "ospf6_proto.h"
  30. #include "ospf6_top.h"
  31. #include "ospf6_interface.h"
  32. #include "ospf6_route.h"
  33. #include "ospf6_lsa.h"
  34. #include "ospf6_lsdb.h"
  35. #include "ospf6_asbr.h"
  36. #include "ospf6_zebra.h"
  37. #include "ospf6d.h"
  38. unsigned char conf_debug_ospf6_zebra = 0;
  39. /* information about zebra. */
  40. struct zclient *zclient = NULL;
  41. struct in_addr router_id_zebra;
  42. /* Router-id update message from zebra. */
  43. static int
  44. ospf6_router_id_update_zebra (int command, struct zclient *zclient,
  45. zebra_size_t length, vrf_id_t vrf_id)
  46. {
  47. struct prefix router_id;
  48. struct ospf6 *o = ospf6;
  49. zebra_router_id_update_read(zclient->ibuf,&router_id);
  50. router_id_zebra = router_id.u.prefix4;
  51. if (o == NULL)
  52. return 0;
  53. if (o->router_id == 0)
  54. o->router_id = (u_int32_t) router_id_zebra.s_addr;
  55. return 0;
  56. }
  57. /* redistribute function */
  58. void
  59. ospf6_zebra_redistribute (int type)
  60. {
  61. if (vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT))
  62. return;
  63. vrf_bitmap_set (zclient->redist[type], VRF_DEFAULT);
  64. if (zclient->sock > 0)
  65. zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, type,
  66. VRF_DEFAULT);
  67. }
  68. void
  69. ospf6_zebra_no_redistribute (int type)
  70. {
  71. if (! vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT))
  72. return;
  73. vrf_bitmap_unset (zclient->redist[type], VRF_DEFAULT);
  74. if (zclient->sock > 0)
  75. zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, type,
  76. VRF_DEFAULT);
  77. }
  78. /* Inteface addition message from zebra. */
  79. static int
  80. ospf6_zebra_if_add (int command, struct zclient *zclient, zebra_size_t length,
  81. vrf_id_t vrf_id)
  82. {
  83. struct interface *ifp;
  84. ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
  85. if (IS_OSPF6_DEBUG_ZEBRA (RECV))
  86. zlog_debug ("Zebra Interface add: %s index %d mtu %d",
  87. ifp->name, ifp->ifindex, ifp->mtu6);
  88. ospf6_interface_if_add (ifp);
  89. return 0;
  90. }
  91. static int
  92. ospf6_zebra_if_del (int command, struct zclient *zclient, zebra_size_t length,
  93. vrf_id_t vrf_id)
  94. {
  95. struct interface *ifp;
  96. if (!(ifp = zebra_interface_state_read (zclient->ibuf, vrf_id)))
  97. return 0;
  98. if (if_is_up (ifp))
  99. zlog_warn ("Zebra: got delete of %s, but interface is still up", ifp->name);
  100. if (IS_OSPF6_DEBUG_ZEBRA (RECV))
  101. zlog_debug ("Zebra Interface delete: %s index %d mtu %d",
  102. ifp->name, ifp->ifindex, ifp->mtu6);
  103. #if 0
  104. /* XXX: ospf6_interface_if_del is not the right way to handle this,
  105. * because among other thinkable issues, it will also clear all
  106. * settings as they are contained in the struct ospf6_interface. */
  107. ospf6_interface_if_del (ifp);
  108. #endif /*0*/
  109. ifp->ifindex = IFINDEX_INTERNAL;
  110. return 0;
  111. }
  112. static int
  113. ospf6_zebra_if_state_update (int command, struct zclient *zclient,
  114. zebra_size_t length, vrf_id_t vrf_id)
  115. {
  116. struct interface *ifp;
  117. ifp = zebra_interface_state_read (zclient->ibuf, vrf_id);
  118. if (ifp == NULL)
  119. return 0;
  120. if (IS_OSPF6_DEBUG_ZEBRA (RECV))
  121. zlog_debug ("Zebra Interface state change: "
  122. "%s index %d flags %llx metric %d mtu %d bandwidth %d",
  123. ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
  124. ifp->metric, ifp->mtu6, ifp->bandwidth);
  125. ospf6_interface_state_update (ifp);
  126. return 0;
  127. }
  128. static int
  129. ospf6_zebra_if_address_update_add (int command, struct zclient *zclient,
  130. zebra_size_t length, vrf_id_t vrf_id)
  131. {
  132. struct connected *c;
  133. char buf[128];
  134. c = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD, zclient->ibuf,
  135. vrf_id);
  136. if (c == NULL)
  137. return 0;
  138. if (IS_OSPF6_DEBUG_ZEBRA (RECV))
  139. zlog_debug ("Zebra Interface address add: %s %5s %s/%d",
  140. c->ifp->name, prefix_family_str (c->address),
  141. inet_ntop (c->address->family, &c->address->u.prefix,
  142. buf, sizeof (buf)), c->address->prefixlen);
  143. if (c->address->family == AF_INET6)
  144. {
  145. ospf6_interface_state_update (c->ifp);
  146. ospf6_interface_connected_route_update (c->ifp);
  147. }
  148. return 0;
  149. }
  150. static int
  151. ospf6_zebra_if_address_update_delete (int command, struct zclient *zclient,
  152. zebra_size_t length, vrf_id_t vrf_id)
  153. {
  154. struct connected *c;
  155. char buf[128];
  156. c = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_DELETE, zclient->ibuf,
  157. vrf_id);
  158. if (c == NULL)
  159. return 0;
  160. if (IS_OSPF6_DEBUG_ZEBRA (RECV))
  161. zlog_debug ("Zebra Interface address delete: %s %5s %s/%d",
  162. c->ifp->name, prefix_family_str (c->address),
  163. inet_ntop (c->address->family, &c->address->u.prefix,
  164. buf, sizeof (buf)), c->address->prefixlen);
  165. if (c->address->family == AF_INET6)
  166. {
  167. ospf6_interface_connected_route_update (c->ifp);
  168. ospf6_interface_state_update (c->ifp);
  169. }
  170. return 0;
  171. }
  172. static int
  173. ospf6_zebra_read_ipv6 (int command, struct zclient *zclient,
  174. zebra_size_t length, vrf_id_t vrf_id)
  175. {
  176. struct stream *s;
  177. struct zapi_ipv6 api;
  178. unsigned long ifindex;
  179. struct prefix_ipv6 p;
  180. struct in6_addr *nexthop;
  181. unsigned char plength = 0;
  182. s = zclient->ibuf;
  183. ifindex = 0;
  184. nexthop = NULL;
  185. memset (&api, 0, sizeof (api));
  186. /* Type, flags, message. */
  187. api.type = stream_getc (s);
  188. api.flags = stream_getc (s);
  189. api.message = stream_getc (s);
  190. /* IPv6 prefix. */
  191. memset (&p, 0, sizeof (struct prefix_ipv6));
  192. p.family = AF_INET6;
  193. plength = stream_getc (s);
  194. p.prefixlen = MIN(IPV6_MAX_PREFIXLEN, plength);
  195. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  196. /* Nexthop, ifindex, distance, metric. */
  197. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  198. {
  199. api.nexthop_num = stream_getc (s);
  200. nexthop = (struct in6_addr *)
  201. malloc (api.nexthop_num * sizeof (struct in6_addr));
  202. stream_get (nexthop, s, api.nexthop_num * sizeof (struct in6_addr));
  203. }
  204. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
  205. {
  206. api.ifindex_num = stream_getc (s);
  207. ifindex = stream_getl (s);
  208. }
  209. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  210. api.distance = stream_getc (s);
  211. else
  212. api.distance = 0;
  213. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  214. api.metric = stream_getl (s);
  215. else
  216. api.metric = 0;
  217. if (IS_OSPF6_DEBUG_ZEBRA (RECV))
  218. {
  219. char prefixstr[128], nexthopstr[128];
  220. prefix2str ((struct prefix *)&p, prefixstr, sizeof (prefixstr));
  221. if (nexthop)
  222. inet_ntop (AF_INET6, nexthop, nexthopstr, sizeof (nexthopstr));
  223. else
  224. snprintf (nexthopstr, sizeof (nexthopstr), "::");
  225. zlog_debug ("Zebra Receive route %s: %s %s nexthop %s ifindex %ld",
  226. (command == ZEBRA_IPV6_ROUTE_ADD ? "add" : "delete"),
  227. zebra_route_string(api.type), prefixstr, nexthopstr, ifindex);
  228. }
  229. if (command == ZEBRA_IPV6_ROUTE_ADD)
  230. ospf6_asbr_redistribute_add (api.type, ifindex, (struct prefix *) &p,
  231. api.nexthop_num, nexthop);
  232. else
  233. ospf6_asbr_redistribute_remove (api.type, ifindex, (struct prefix *) &p);
  234. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  235. free (nexthop);
  236. return 0;
  237. }
  238. DEFUN (show_zebra,
  239. show_zebra_cmd,
  240. "show zebra",
  241. SHOW_STR
  242. "Zebra information\n")
  243. {
  244. int i;
  245. if (zclient == NULL)
  246. {
  247. vty_out (vty, "Not connected to zebra%s", VNL);
  248. return CMD_SUCCESS;
  249. }
  250. vty_out (vty, "Zebra Infomation%s", VNL);
  251. vty_out (vty, " enable: %d fail: %d%s",
  252. zclient->enable, zclient->fail, VNL);
  253. vty_out (vty, " redistribute default: %d%s",
  254. vrf_bitmap_check (zclient->default_information, VRF_DEFAULT),
  255. VNL);
  256. vty_out (vty, " redistribute:");
  257. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  258. {
  259. if (vrf_bitmap_check (zclient->redist[i], VRF_DEFAULT))
  260. vty_out (vty, " %s", zebra_route_string(i));
  261. }
  262. vty_out (vty, "%s", VNL);
  263. return CMD_SUCCESS;
  264. }
  265. DEFUN (router_zebra,
  266. router_zebra_cmd,
  267. "router zebra",
  268. "Enable a routing process\n"
  269. "Make connection to zebra daemon\n")
  270. {
  271. vty->node = ZEBRA_NODE;
  272. zclient->enable = 1;
  273. zclient_start (zclient);
  274. return CMD_SUCCESS;
  275. }
  276. DEFUN (no_router_zebra,
  277. no_router_zebra_cmd,
  278. "no router zebra",
  279. NO_STR
  280. "Configure routing process\n"
  281. "Disable connection to zebra daemon\n")
  282. {
  283. zclient->enable = 0;
  284. zclient_stop (zclient);
  285. return CMD_SUCCESS;
  286. }
  287. /* Zebra configuration write function. */
  288. static int
  289. config_write_ospf6_zebra (struct vty *vty)
  290. {
  291. if (! zclient->enable)
  292. {
  293. vty_out (vty, "no router zebra%s", VNL);
  294. vty_out (vty, "!%s", VNL);
  295. }
  296. else if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT))
  297. {
  298. vty_out (vty, "router zebra%s", VNL);
  299. vty_out (vty, " no redistribute ospf6%s", VNL);
  300. vty_out (vty, "!%s", VNL);
  301. }
  302. return 0;
  303. }
  304. /* Zebra node structure. */
  305. static struct cmd_node zebra_node =
  306. {
  307. ZEBRA_NODE,
  308. "%s(config-zebra)# ",
  309. };
  310. #define ADD 0
  311. #define REM 1
  312. static void
  313. ospf6_zebra_route_update (int type, struct ospf6_route *request)
  314. {
  315. struct zapi_ipv6 api;
  316. char buf[64];
  317. int nhcount;
  318. struct in6_addr **nexthops;
  319. ifindex_t *ifindexes;
  320. int i, ret = 0;
  321. struct prefix_ipv6 *dest;
  322. if (IS_OSPF6_DEBUG_ZEBRA (SEND))
  323. {
  324. prefix2str (&request->prefix, buf, sizeof (buf));
  325. zlog_debug ("Send %s route: %s",
  326. (type == REM ? "remove" : "add"), buf);
  327. }
  328. if (zclient->sock < 0)
  329. {
  330. if (IS_OSPF6_DEBUG_ZEBRA (SEND))
  331. zlog_debug (" Not connected to Zebra");
  332. return;
  333. }
  334. if (request->path.origin.adv_router == ospf6->router_id &&
  335. (request->path.type == OSPF6_PATH_TYPE_EXTERNAL1 ||
  336. request->path.type == OSPF6_PATH_TYPE_EXTERNAL2))
  337. {
  338. if (IS_OSPF6_DEBUG_ZEBRA (SEND))
  339. zlog_debug (" Ignore self-originated external route");
  340. return;
  341. }
  342. /* If removing is the best path and if there's another path,
  343. treat this request as add the secondary path */
  344. if (type == REM && ospf6_route_is_best (request) &&
  345. request->next && ospf6_route_is_same (request, request->next))
  346. {
  347. if (IS_OSPF6_DEBUG_ZEBRA (SEND))
  348. zlog_debug (" Best-path removal resulted Sencondary addition");
  349. type = ADD;
  350. request = request->next;
  351. }
  352. /* Only the best path will be sent to zebra. */
  353. if (! ospf6_route_is_best (request))
  354. {
  355. /* this is not preferred best route, ignore */
  356. if (IS_OSPF6_DEBUG_ZEBRA (SEND))
  357. zlog_debug (" Ignore non-best route");
  358. return;
  359. }
  360. nhcount = 0;
  361. for (i = 0; i < OSPF6_MULTI_PATH_LIMIT; i++)
  362. if (ospf6_nexthop_is_set (&request->nexthop[i]))
  363. nhcount++;
  364. if (nhcount == 0)
  365. {
  366. if (IS_OSPF6_DEBUG_ZEBRA (SEND))
  367. zlog_debug (" No nexthop, ignore");
  368. return;
  369. }
  370. /* allocate memory for nexthop_list */
  371. nexthops = XCALLOC (MTYPE_OSPF6_OTHER,
  372. nhcount * sizeof (struct in6_addr *));
  373. if (nexthops == NULL)
  374. {
  375. zlog_warn ("Can't send route to zebra: malloc failed");
  376. return;
  377. }
  378. /* allocate memory for ifindex_list */
  379. ifindexes = XCALLOC (MTYPE_OSPF6_OTHER,
  380. nhcount * sizeof (unsigned int));
  381. if (ifindexes == NULL)
  382. {
  383. zlog_warn ("Can't send route to zebra: malloc failed");
  384. XFREE (MTYPE_OSPF6_OTHER, nexthops);
  385. return;
  386. }
  387. for (i = 0; i < nhcount; i++)
  388. {
  389. if (IS_OSPF6_DEBUG_ZEBRA (SEND))
  390. {
  391. const char *ifname;
  392. inet_ntop (AF_INET6, &request->nexthop[i].address,
  393. buf, sizeof (buf));
  394. ifname = ifindex2ifname (request->nexthop[i].ifindex);
  395. zlog_debug (" nexthop: %s%%%.*s(%d)", buf, IFNAMSIZ, ifname,
  396. request->nexthop[i].ifindex);
  397. }
  398. nexthops[i] = &request->nexthop[i].address;
  399. ifindexes[i] = request->nexthop[i].ifindex;
  400. }
  401. api.vrf_id = VRF_DEFAULT;
  402. api.type = ZEBRA_ROUTE_OSPF6;
  403. api.flags = 0;
  404. api.message = 0;
  405. api.safi = SAFI_UNICAST;
  406. SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
  407. api.nexthop_num = nhcount;
  408. api.nexthop = nexthops;
  409. SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
  410. api.ifindex_num = nhcount;
  411. api.ifindex = ifindexes;
  412. SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
  413. api.metric = (request->path.metric_type == 2 ?
  414. request->path.cost_e2 : request->path.cost);
  415. SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
  416. api.distance = ospf6_distance_apply (request, ospf6);
  417. dest = (struct prefix_ipv6 *) &request->prefix;
  418. if (type == REM)
  419. ret = zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient, dest, &api);
  420. else
  421. ret = zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD, zclient, dest, &api);
  422. if (ret < 0)
  423. zlog_err ("zapi_ipv6_route() %s failed: %s",
  424. (type == REM ? "delete" : "add"), safe_strerror (errno));
  425. XFREE (MTYPE_OSPF6_OTHER, nexthops);
  426. XFREE (MTYPE_OSPF6_OTHER, ifindexes);
  427. return;
  428. }
  429. void
  430. ospf6_zebra_route_update_add (struct ospf6_route *request)
  431. {
  432. if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT))
  433. {
  434. ospf6->route_table->hook_add = NULL;
  435. ospf6->route_table->hook_remove = NULL;
  436. return;
  437. }
  438. ospf6_zebra_route_update (ADD, request);
  439. }
  440. void
  441. ospf6_zebra_route_update_remove (struct ospf6_route *request)
  442. {
  443. if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT))
  444. {
  445. ospf6->route_table->hook_add = NULL;
  446. ospf6->route_table->hook_remove = NULL;
  447. return;
  448. }
  449. ospf6_zebra_route_update (REM, request);
  450. }
  451. DEFUN (redistribute_ospf6,
  452. redistribute_ospf6_cmd,
  453. "redistribute ospf6",
  454. "Redistribute control\n"
  455. "OSPF6 route\n")
  456. {
  457. struct ospf6_route *route;
  458. if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT))
  459. return CMD_SUCCESS;
  460. vrf_bitmap_set (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT);
  461. if (ospf6 == NULL)
  462. return CMD_SUCCESS;
  463. /* send ospf6 route to zebra route table */
  464. for (route = ospf6_route_head (ospf6->route_table); route;
  465. route = ospf6_route_next (route))
  466. ospf6_zebra_route_update_add (route);
  467. ospf6->route_table->hook_add = ospf6_zebra_route_update_add;
  468. ospf6->route_table->hook_remove = ospf6_zebra_route_update_remove;
  469. return CMD_SUCCESS;
  470. }
  471. DEFUN (no_redistribute_ospf6,
  472. no_redistribute_ospf6_cmd,
  473. "no redistribute ospf6",
  474. NO_STR
  475. "Redistribute control\n"
  476. "OSPF6 route\n")
  477. {
  478. struct ospf6_route *route;
  479. if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT))
  480. return CMD_SUCCESS;
  481. vrf_bitmap_unset (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT);
  482. if (ospf6 == NULL)
  483. return CMD_SUCCESS;
  484. ospf6->route_table->hook_add = NULL;
  485. ospf6->route_table->hook_remove = NULL;
  486. /* withdraw ospf6 route from zebra route table */
  487. for (route = ospf6_route_head (ospf6->route_table); route;
  488. route = ospf6_route_next (route))
  489. ospf6_zebra_route_update_remove (route);
  490. return CMD_SUCCESS;
  491. }
  492. static void
  493. ospf6_zebra_connected (struct zclient *zclient)
  494. {
  495. zclient_send_requests (zclient, VRF_DEFAULT);
  496. }
  497. static struct ospf6_distance *
  498. ospf6_distance_new (void)
  499. {
  500. return XCALLOC (MTYPE_OSPF6_DISTANCE, sizeof (struct ospf6_distance));
  501. }
  502. static void
  503. ospf6_distance_free (struct ospf6_distance *odistance)
  504. {
  505. XFREE (MTYPE_OSPF6_DISTANCE, odistance);
  506. }
  507. int
  508. ospf6_distance_set (struct vty *vty, struct ospf6 *o,
  509. const char *distance_str,
  510. const char *ip_str,
  511. const char *access_list_str)
  512. {
  513. int ret;
  514. struct prefix_ipv6 p;
  515. u_char distance;
  516. struct route_node *rn;
  517. struct ospf6_distance *odistance;
  518. ret = str2prefix_ipv6 (ip_str, &p);
  519. if (ret == 0)
  520. {
  521. vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
  522. return CMD_WARNING;
  523. }
  524. distance = atoi (distance_str);
  525. /* Get OSPF6 distance node. */
  526. rn = route_node_get (o->distance_table, (struct prefix *) &p);
  527. if (rn->info)
  528. {
  529. odistance = rn->info;
  530. route_unlock_node (rn);
  531. }
  532. else
  533. {
  534. odistance = ospf6_distance_new ();
  535. rn->info = odistance;
  536. }
  537. /* Set distance value. */
  538. odistance->distance = distance;
  539. /*Reset access-list configuration. */
  540. if (odistance->access_list)
  541. {
  542. free (odistance->access_list);
  543. odistance->access_list = NULL;
  544. }
  545. if (access_list_str)
  546. odistance->access_list = strdup (access_list_str);
  547. return CMD_SUCCESS;
  548. }
  549. int
  550. ospf6_distance_unset (struct vty *vty, struct ospf6 *o,
  551. const char *ip_str,
  552. const char *access_list_str)
  553. {
  554. int ret;
  555. struct prefix_ipv6 p;
  556. struct route_node *rn;
  557. struct ospf6_distance *odistance;
  558. ret = str2prefix_ipv6 (ip_str, &p);
  559. if (ret == 0)
  560. {
  561. vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
  562. return CMD_WARNING;
  563. }
  564. rn = route_node_lookup (o->distance_table, (struct prefix *) &p);
  565. if (!rn)
  566. {
  567. vty_out (vty, "Cant't find specified prefix%s", VTY_NEWLINE);
  568. return CMD_WARNING;
  569. }
  570. odistance = rn->info;
  571. if (odistance->access_list)
  572. free (odistance->access_list);
  573. ospf6_distance_free (odistance);
  574. rn->info = NULL;
  575. route_unlock_node (rn);
  576. route_unlock_node (rn);
  577. return CMD_SUCCESS;
  578. }
  579. void
  580. ospf6_distance_reset (struct ospf6 *o)
  581. {
  582. struct route_node *rn;
  583. struct ospf6_distance *odistance;
  584. for (rn = route_top (o->distance_table); rn; rn = route_next (rn))
  585. if ((odistance = rn->info) != NULL)
  586. {
  587. if (odistance->access_list)
  588. free (odistance->access_list);
  589. ospf6_distance_free (odistance);
  590. rn->info = NULL;
  591. route_unlock_node (rn);
  592. }
  593. }
  594. u_char
  595. ospf6_distance_apply (struct ospf6_route *or, struct ospf6 *o)
  596. {
  597. if (o == NULL)
  598. return 0;
  599. if (o->distance_intra)
  600. if (or->path.type == OSPF6_PATH_TYPE_INTRA)
  601. return o->distance_intra;
  602. if (o->distance_inter)
  603. if (or->path.type == OSPF6_PATH_TYPE_INTER)
  604. return o->distance_inter;
  605. if (o->distance_external)
  606. if(or->path.type == OSPF6_PATH_TYPE_EXTERNAL1
  607. || or->path.type == OSPF6_PATH_TYPE_EXTERNAL2)
  608. return o->distance_external;
  609. if (o->distance_all)
  610. return o->distance_all;
  611. return 0;
  612. }
  613. void
  614. ospf6_zebra_init (struct thread_master *master)
  615. {
  616. /* Allocate zebra structure. */
  617. zclient = zclient_new (master);
  618. zclient_init (zclient, ZEBRA_ROUTE_OSPF6);
  619. zclient->zebra_connected = ospf6_zebra_connected;
  620. zclient->router_id_update = ospf6_router_id_update_zebra;
  621. zclient->interface_add = ospf6_zebra_if_add;
  622. zclient->interface_delete = ospf6_zebra_if_del;
  623. zclient->interface_up = ospf6_zebra_if_state_update;
  624. zclient->interface_down = ospf6_zebra_if_state_update;
  625. zclient->interface_address_add = ospf6_zebra_if_address_update_add;
  626. zclient->interface_address_delete = ospf6_zebra_if_address_update_delete;
  627. zclient->ipv4_route_add = NULL;
  628. zclient->ipv4_route_delete = NULL;
  629. zclient->ipv6_route_add = ospf6_zebra_read_ipv6;
  630. zclient->ipv6_route_delete = ospf6_zebra_read_ipv6;
  631. /* redistribute connected route by default */
  632. /* ospf6_zebra_redistribute (ZEBRA_ROUTE_CONNECT); */
  633. /* Install zebra node. */
  634. install_node (&zebra_node, config_write_ospf6_zebra);
  635. /* Install command element for zebra node. */
  636. install_element (VIEW_NODE, &show_zebra_cmd);
  637. install_element (CONFIG_NODE, &router_zebra_cmd);
  638. install_element (CONFIG_NODE, &no_router_zebra_cmd);
  639. install_default (ZEBRA_NODE);
  640. install_element (ZEBRA_NODE, &redistribute_ospf6_cmd);
  641. install_element (ZEBRA_NODE, &no_redistribute_ospf6_cmd);
  642. return;
  643. }
  644. /* Debug */
  645. DEFUN (debug_ospf6_zebra_sendrecv,
  646. debug_ospf6_zebra_sendrecv_cmd,
  647. "debug ospf6 zebra (send|recv)",
  648. DEBUG_STR
  649. OSPF6_STR
  650. "Debug connection between zebra\n"
  651. "Debug Sending zebra\n"
  652. "Debug Receiving zebra\n"
  653. )
  654. {
  655. unsigned char level = 0;
  656. if (argc)
  657. {
  658. if (! strncmp (argv[0], "s", 1))
  659. level = OSPF6_DEBUG_ZEBRA_SEND;
  660. else if (! strncmp (argv[0], "r", 1))
  661. level = OSPF6_DEBUG_ZEBRA_RECV;
  662. }
  663. else
  664. level = OSPF6_DEBUG_ZEBRA_SEND | OSPF6_DEBUG_ZEBRA_RECV;
  665. OSPF6_DEBUG_ZEBRA_ON (level);
  666. return CMD_SUCCESS;
  667. }
  668. ALIAS (debug_ospf6_zebra_sendrecv,
  669. debug_ospf6_zebra_cmd,
  670. "debug ospf6 zebra",
  671. DEBUG_STR
  672. OSPF6_STR
  673. "Debug connection between zebra\n"
  674. )
  675. DEFUN (no_debug_ospf6_zebra_sendrecv,
  676. no_debug_ospf6_zebra_sendrecv_cmd,
  677. "no debug ospf6 zebra (send|recv)",
  678. NO_STR
  679. DEBUG_STR
  680. OSPF6_STR
  681. "Debug connection between zebra\n"
  682. "Debug Sending zebra\n"
  683. "Debug Receiving zebra\n"
  684. )
  685. {
  686. unsigned char level = 0;
  687. if (argc)
  688. {
  689. if (! strncmp (argv[0], "s", 1))
  690. level = OSPF6_DEBUG_ZEBRA_SEND;
  691. else if (! strncmp (argv[0], "r", 1))
  692. level = OSPF6_DEBUG_ZEBRA_RECV;
  693. }
  694. else
  695. level = OSPF6_DEBUG_ZEBRA_SEND | OSPF6_DEBUG_ZEBRA_RECV;
  696. OSPF6_DEBUG_ZEBRA_OFF (level);
  697. return CMD_SUCCESS;
  698. }
  699. ALIAS (no_debug_ospf6_zebra_sendrecv,
  700. no_debug_ospf6_zebra_cmd,
  701. "no debug ospf6 zebra",
  702. NO_STR
  703. DEBUG_STR
  704. OSPF6_STR
  705. "Debug connection between zebra\n"
  706. )
  707. int
  708. config_write_ospf6_debug_zebra (struct vty *vty)
  709. {
  710. if (IS_OSPF6_DEBUG_ZEBRA (SEND) && IS_OSPF6_DEBUG_ZEBRA (RECV))
  711. vty_out (vty, "debug ospf6 zebra%s", VNL);
  712. else
  713. {
  714. if (IS_OSPF6_DEBUG_ZEBRA (SEND))
  715. vty_out (vty, "debug ospf6 zebra send%s", VNL);
  716. if (IS_OSPF6_DEBUG_ZEBRA (RECV))
  717. vty_out (vty, "debug ospf6 zebra recv%s", VNL);
  718. }
  719. return 0;
  720. }
  721. void
  722. install_element_ospf6_debug_zebra (void)
  723. {
  724. install_element (ENABLE_NODE, &debug_ospf6_zebra_cmd);
  725. install_element (ENABLE_NODE, &no_debug_ospf6_zebra_cmd);
  726. install_element (ENABLE_NODE, &debug_ospf6_zebra_sendrecv_cmd);
  727. install_element (ENABLE_NODE, &no_debug_ospf6_zebra_sendrecv_cmd);
  728. install_element (CONFIG_NODE, &debug_ospf6_zebra_cmd);
  729. install_element (CONFIG_NODE, &no_debug_ospf6_zebra_cmd);
  730. install_element (CONFIG_NODE, &debug_ospf6_zebra_sendrecv_cmd);
  731. install_element (CONFIG_NODE, &no_debug_ospf6_zebra_sendrecv_cmd);
  732. }