ospf6_zebra.c 22 KB

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