ospf_zebra.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. /*
  2. * Zebra connect library for OSPFd
  3. * Copyright (C) 1997, 98, 99, 2000 Kunihiro Ishiguro, Toshiaki Takada
  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
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "thread.h"
  24. #include "command.h"
  25. #include "network.h"
  26. #include "prefix.h"
  27. #include "routemap.h"
  28. #include "table.h"
  29. #include "stream.h"
  30. #include "memory.h"
  31. #include "zclient.h"
  32. #include "filter.h"
  33. #include "plist.h"
  34. #include "log.h"
  35. #include "ospfd/ospfd.h"
  36. #include "ospfd/ospf_interface.h"
  37. #include "ospfd/ospf_ism.h"
  38. #include "ospfd/ospf_asbr.h"
  39. #include "ospfd/ospf_asbr.h"
  40. #include "ospfd/ospf_abr.h"
  41. #include "ospfd/ospf_lsa.h"
  42. #include "ospfd/ospf_dump.h"
  43. #include "ospfd/ospf_route.h"
  44. #include "ospfd/ospf_zebra.h"
  45. #ifdef HAVE_SNMP
  46. #include "ospfd/ospf_snmp.h"
  47. #endif /* HAVE_SNMP */
  48. #include "ospfd/ospf_te.h"
  49. /* Zebra structure to hold current status. */
  50. struct zclient *zclient = NULL;
  51. /* For registering threads. */
  52. extern struct thread_master *master;
  53. struct in_addr router_id_zebra;
  54. /* Router-id update message from zebra. */
  55. static int
  56. ospf_router_id_update_zebra (int command, struct zclient *zclient,
  57. zebra_size_t length, vrf_id_t vrf_id)
  58. {
  59. struct ospf *ospf;
  60. struct prefix router_id;
  61. zebra_router_id_update_read(zclient->ibuf,&router_id);
  62. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  63. {
  64. char buf[128];
  65. prefix2str(&router_id, buf, sizeof(buf));
  66. zlog_debug("Zebra rcvd: router id update %s", buf);
  67. }
  68. router_id_zebra = router_id.u.prefix4;
  69. ospf = ospf_lookup ();
  70. if (ospf != NULL)
  71. ospf_router_id_update (ospf);
  72. return 0;
  73. }
  74. /* Inteface addition message from zebra. */
  75. static int
  76. ospf_interface_add (int command, struct zclient *zclient, zebra_size_t length,
  77. vrf_id_t vrf_id)
  78. {
  79. struct interface *ifp;
  80. ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
  81. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  82. zlog_debug ("Zebra: interface add %s index %d flags %llx metric %d mtu %d",
  83. ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
  84. ifp->metric, ifp->mtu);
  85. assert (ifp->info);
  86. if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp), type))
  87. {
  88. SET_IF_PARAM (IF_DEF_PARAMS (ifp), type);
  89. IF_DEF_PARAMS (ifp)->type = ospf_default_iftype(ifp);
  90. }
  91. ospf_if_update (NULL, ifp);
  92. #ifdef HAVE_SNMP
  93. ospf_snmp_if_update (ifp);
  94. #endif /* HAVE_SNMP */
  95. return 0;
  96. }
  97. static int
  98. ospf_interface_delete (int command, struct zclient *zclient,
  99. zebra_size_t length, vrf_id_t vrf_id)
  100. {
  101. struct interface *ifp;
  102. struct stream *s;
  103. struct route_node *rn;
  104. s = zclient->ibuf;
  105. /* zebra_interface_state_read() updates interface structure in iflist */
  106. ifp = zebra_interface_state_read (s, vrf_id);
  107. if (ifp == NULL)
  108. return 0;
  109. if (if_is_up (ifp))
  110. zlog_warn ("Zebra: got delete of %s, but interface is still up",
  111. ifp->name);
  112. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  113. zlog_debug
  114. ("Zebra: interface delete %s index %d flags %llx metric %d mtu %d",
  115. ifp->name, ifp->ifindex, (unsigned long long)ifp->flags, ifp->metric, ifp->mtu);
  116. #ifdef HAVE_SNMP
  117. ospf_snmp_if_delete (ifp);
  118. #endif /* HAVE_SNMP */
  119. for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
  120. if (rn->info)
  121. ospf_if_free ((struct ospf_interface *) rn->info);
  122. ifp->ifindex = IFINDEX_INTERNAL;
  123. return 0;
  124. }
  125. static struct interface *
  126. zebra_interface_if_lookup (struct stream *s, vrf_id_t vrf_id)
  127. {
  128. char ifname_tmp[INTERFACE_NAMSIZ];
  129. /* Read interface name. */
  130. stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
  131. /* And look it up. */
  132. return if_lookup_by_name_len(ifname_tmp,
  133. strnlen(ifname_tmp, INTERFACE_NAMSIZ));
  134. }
  135. static int
  136. ospf_interface_state_up (int command, struct zclient *zclient,
  137. zebra_size_t length, vrf_id_t vrf_id)
  138. {
  139. struct interface *ifp;
  140. struct ospf_interface *oi;
  141. struct route_node *rn;
  142. ifp = zebra_interface_if_lookup (zclient->ibuf, vrf_id);
  143. if (ifp == NULL)
  144. return 0;
  145. /* Interface is already up. */
  146. if (if_is_operative (ifp))
  147. {
  148. /* Temporarily keep ifp values. */
  149. struct interface if_tmp;
  150. memcpy (&if_tmp, ifp, sizeof (struct interface));
  151. zebra_interface_if_set_value (zclient->ibuf, ifp);
  152. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  153. zlog_debug ("Zebra: Interface[%s] state update.", ifp->name);
  154. if (if_tmp.bandwidth != ifp->bandwidth)
  155. {
  156. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  157. zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
  158. ifp->name, if_tmp.bandwidth, ifp->bandwidth);
  159. ospf_if_recalculate_output_cost (ifp);
  160. }
  161. if (if_tmp.mtu != ifp->mtu)
  162. {
  163. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  164. zlog_debug ("Zebra: Interface[%s] MTU change %u -> %u.",
  165. ifp->name, if_tmp.mtu, ifp->mtu);
  166. /* Must reset the interface (simulate down/up) when MTU changes. */
  167. ospf_if_reset(ifp);
  168. }
  169. return 0;
  170. }
  171. zebra_interface_if_set_value (zclient->ibuf, ifp);
  172. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  173. zlog_debug ("Zebra: Interface[%s] state change to up.", ifp->name);
  174. for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
  175. {
  176. if ((oi = rn->info) == NULL)
  177. continue;
  178. ospf_if_up (oi);
  179. }
  180. return 0;
  181. }
  182. static int
  183. ospf_interface_state_down (int command, struct zclient *zclient,
  184. zebra_size_t length, vrf_id_t vrf_id)
  185. {
  186. struct interface *ifp;
  187. struct ospf_interface *oi;
  188. struct route_node *node;
  189. ifp = zebra_interface_state_read (zclient->ibuf, vrf_id);
  190. if (ifp == NULL)
  191. return 0;
  192. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  193. zlog_debug ("Zebra: Interface[%s] state change to down.", ifp->name);
  194. for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
  195. {
  196. if ((oi = node->info) == NULL)
  197. continue;
  198. ospf_if_down (oi);
  199. }
  200. return 0;
  201. }
  202. static int
  203. ospf_interface_address_add (int command, struct zclient *zclient,
  204. zebra_size_t length, vrf_id_t vrf_id)
  205. {
  206. struct connected *c;
  207. c = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
  208. if (c == NULL)
  209. return 0;
  210. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  211. {
  212. char buf[128];
  213. prefix2str(c->address, buf, sizeof(buf));
  214. zlog_debug("Zebra: interface %s address add %s", c->ifp->name, buf);
  215. }
  216. ospf_if_update (NULL, c->ifp);
  217. #ifdef HAVE_SNMP
  218. ospf_snmp_if_update (c->ifp);
  219. #endif /* HAVE_SNMP */
  220. return 0;
  221. }
  222. static int
  223. ospf_interface_address_delete (int command, struct zclient *zclient,
  224. zebra_size_t length, vrf_id_t vrf_id)
  225. {
  226. struct connected *c;
  227. struct interface *ifp;
  228. struct ospf_interface *oi;
  229. struct route_node *rn;
  230. struct prefix p;
  231. c = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
  232. if (c == NULL)
  233. return 0;
  234. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  235. {
  236. char buf[128];
  237. prefix2str(c->address, buf, sizeof(buf));
  238. zlog_debug("Zebra: interface %s address delete %s", c->ifp->name, buf);
  239. }
  240. ifp = c->ifp;
  241. p = *c->address;
  242. p.prefixlen = IPV4_MAX_PREFIXLEN;
  243. rn = route_node_lookup (IF_OIFS (ifp), &p);
  244. if (!rn)
  245. {
  246. connected_free (c);
  247. return 0;
  248. }
  249. assert (rn->info);
  250. oi = rn->info;
  251. route_unlock_node (rn);
  252. /* Call interface hook functions to clean up */
  253. ospf_if_free (oi);
  254. #ifdef HAVE_SNMP
  255. ospf_snmp_if_update (c->ifp);
  256. #endif /* HAVE_SNMP */
  257. connected_free (c);
  258. return 0;
  259. }
  260. static int
  261. ospf_interface_link_params (int command, struct zclient *zclient,
  262. zebra_size_t length)
  263. {
  264. struct interface *ifp;
  265. ifp = zebra_interface_link_params_read (zclient->ibuf);
  266. if (ifp == NULL)
  267. return 0;
  268. /* Update TE TLV */
  269. ospf_mpls_te_update_if (ifp);
  270. return 0;
  271. }
  272. void
  273. ospf_zebra_add (struct prefix_ipv4 *p, struct ospf_route *or)
  274. {
  275. u_char message;
  276. u_char distance;
  277. u_char flags;
  278. int psize;
  279. struct stream *s;
  280. struct ospf_path *path;
  281. struct listnode *node;
  282. if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF], VRF_DEFAULT))
  283. {
  284. message = 0;
  285. flags = 0;
  286. /* OSPF pass nexthop and metric */
  287. SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
  288. SET_FLAG (message, ZAPI_MESSAGE_METRIC);
  289. /* Distance value. */
  290. distance = ospf_distance_apply (p, or);
  291. if (distance)
  292. SET_FLAG (message, ZAPI_MESSAGE_DISTANCE);
  293. /* Check if path type is ASE */
  294. if (((or->path_type == OSPF_PATH_TYPE1_EXTERNAL) ||
  295. (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)) &&
  296. (or->u.ext.tag > 0) && (or->u.ext.tag <= ROUTE_TAG_MAX))
  297. SET_FLAG (message, ZAPI_MESSAGE_TAG);
  298. /* Make packet. */
  299. s = zclient->obuf;
  300. stream_reset (s);
  301. /* Put command, type, flags, message. */
  302. zclient_create_header (s, ZEBRA_IPV4_ROUTE_ADD, VRF_DEFAULT);
  303. stream_putc (s, ZEBRA_ROUTE_OSPF);
  304. stream_putc (s, flags);
  305. stream_putc (s, message);
  306. stream_putw (s, SAFI_UNICAST);
  307. /* Put prefix information. */
  308. psize = PSIZE (p->prefixlen);
  309. stream_putc (s, p->prefixlen);
  310. stream_write (s, (u_char *) & p->prefix, psize);
  311. /* Nexthop count. */
  312. stream_putc (s, or->paths->count);
  313. /* Nexthop, ifindex, distance and metric information. */
  314. for (ALL_LIST_ELEMENTS_RO (or->paths, node, path))
  315. {
  316. if (path->nexthop.s_addr != INADDR_ANY &&
  317. path->ifindex != 0)
  318. {
  319. stream_putc (s, ZEBRA_NEXTHOP_IPV4_IFINDEX);
  320. stream_put_in_addr (s, &path->nexthop);
  321. stream_putl (s, path->ifindex);
  322. }
  323. else if (path->nexthop.s_addr != INADDR_ANY)
  324. {
  325. stream_putc (s, ZEBRA_NEXTHOP_IPV4);
  326. stream_put_in_addr (s, &path->nexthop);
  327. }
  328. else
  329. {
  330. stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
  331. if (path->ifindex)
  332. stream_putl (s, path->ifindex);
  333. else
  334. stream_putl (s, 0);
  335. }
  336. if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
  337. {
  338. char buf[2][INET_ADDRSTRLEN];
  339. zlog_debug("Zebra: Route add %s/%d nexthop %s",
  340. inet_ntop(AF_INET, &p->prefix,
  341. buf[0], sizeof(buf[0])),
  342. p->prefixlen,
  343. inet_ntop(AF_INET, &path->nexthop,
  344. buf[1], sizeof(buf[1])));
  345. }
  346. }
  347. if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
  348. stream_putc (s, distance);
  349. if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
  350. {
  351. if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
  352. stream_putl (s, or->cost + or->u.ext.type2_cost);
  353. else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
  354. stream_putl (s, or->u.ext.type2_cost);
  355. else
  356. stream_putl (s, or->cost);
  357. }
  358. if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
  359. stream_putl (s, or->u.ext.tag);
  360. stream_putw_at (s, 0, stream_get_endp (s));
  361. zclient_send_message(zclient);
  362. }
  363. }
  364. void
  365. ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or)
  366. {
  367. u_char message;
  368. u_char distance;
  369. u_char flags;
  370. int psize;
  371. struct stream *s;
  372. struct ospf_path *path;
  373. struct listnode *node;
  374. if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF], VRF_DEFAULT))
  375. {
  376. message = 0;
  377. flags = 0;
  378. /* Distance value. */
  379. distance = ospf_distance_apply (p, or);
  380. /* Make packet. */
  381. s = zclient->obuf;
  382. stream_reset (s);
  383. /* Put command, type, flags, message. */
  384. zclient_create_header (s, ZEBRA_IPV4_ROUTE_DELETE, VRF_DEFAULT);
  385. stream_putc (s, ZEBRA_ROUTE_OSPF);
  386. stream_putc (s, flags);
  387. stream_putc (s, message);
  388. stream_putw (s, SAFI_UNICAST);
  389. /* Put prefix information. */
  390. psize = PSIZE (p->prefixlen);
  391. stream_putc (s, p->prefixlen);
  392. stream_write (s, (u_char *) & p->prefix, psize);
  393. /* Nexthop count. */
  394. stream_putc (s, or->paths->count);
  395. /* Nexthop, ifindex, distance and metric information. */
  396. for (ALL_LIST_ELEMENTS_RO (or->paths, node, path))
  397. {
  398. if (path->nexthop.s_addr != INADDR_ANY &&
  399. path->ifindex != 0)
  400. {
  401. stream_putc (s, ZEBRA_NEXTHOP_IPV4_IFINDEX);
  402. stream_put_in_addr (s, &path->nexthop);
  403. stream_putl (s, path->ifindex);
  404. }
  405. else if (path->nexthop.s_addr != INADDR_ANY)
  406. {
  407. stream_putc (s, ZEBRA_NEXTHOP_IPV4);
  408. stream_put_in_addr (s, &path->nexthop);
  409. }
  410. else
  411. {
  412. stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
  413. stream_putl (s, path->ifindex);
  414. }
  415. if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
  416. {
  417. char buf[2][INET_ADDRSTRLEN];
  418. zlog_debug("Zebra: Route delete %s/%d nexthop %s",
  419. inet_ntop(AF_INET, &p->prefix,
  420. buf[0], sizeof(buf[0])),
  421. p->prefixlen,
  422. inet_ntop(AF_INET, &path->nexthop,
  423. buf[1], sizeof(buf[1])));
  424. }
  425. }
  426. if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
  427. stream_putc (s, distance);
  428. if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
  429. {
  430. if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
  431. stream_putl (s, or->cost + or->u.ext.type2_cost);
  432. else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
  433. stream_putl (s, or->u.ext.type2_cost);
  434. else
  435. stream_putl (s, or->cost);
  436. }
  437. stream_putw_at (s, 0, stream_get_endp (s));
  438. zclient_send_message(zclient);
  439. }
  440. }
  441. void
  442. ospf_zebra_add_discard (struct prefix_ipv4 *p)
  443. {
  444. struct zapi_ipv4 api;
  445. if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF], VRF_DEFAULT))
  446. {
  447. api.vrf_id = VRF_DEFAULT;
  448. api.type = ZEBRA_ROUTE_OSPF;
  449. api.flags = ZEBRA_FLAG_BLACKHOLE;
  450. api.message = 0;
  451. api.safi = SAFI_UNICAST;
  452. SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
  453. api.nexthop_num = 0;
  454. api.ifindex_num = 0;
  455. api.tag = 0;
  456. zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, p, &api);
  457. if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
  458. zlog_debug ("Zebra: Route add discard %s/%d",
  459. inet_ntoa (p->prefix), p->prefixlen);
  460. }
  461. }
  462. void
  463. ospf_zebra_delete_discard (struct prefix_ipv4 *p)
  464. {
  465. struct zapi_ipv4 api;
  466. if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF], VRF_DEFAULT))
  467. {
  468. api.vrf_id = VRF_DEFAULT;
  469. api.type = ZEBRA_ROUTE_OSPF;
  470. api.flags = ZEBRA_FLAG_BLACKHOLE;
  471. api.message = 0;
  472. api.safi = SAFI_UNICAST;
  473. SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
  474. api.nexthop_num = 0;
  475. api.ifindex_num = 0;
  476. api.tag = 0;
  477. zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
  478. if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
  479. zlog_debug ("Zebra: Route delete discard %s/%d",
  480. inet_ntoa (p->prefix), p->prefixlen);
  481. }
  482. }
  483. int
  484. ospf_is_type_redistributed (int type)
  485. {
  486. return (DEFAULT_ROUTE_TYPE (type)) ?
  487. vrf_bitmap_check (zclient->default_information, VRF_DEFAULT) : \
  488. vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT);
  489. }
  490. int
  491. ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue)
  492. {
  493. int force = 0;
  494. if (ospf_is_type_redistributed (type))
  495. {
  496. if (mtype != ospf->dmetric[type].type)
  497. {
  498. ospf->dmetric[type].type = mtype;
  499. force = LSA_REFRESH_FORCE;
  500. }
  501. if (mvalue != ospf->dmetric[type].value)
  502. {
  503. ospf->dmetric[type].value = mvalue;
  504. force = LSA_REFRESH_FORCE;
  505. }
  506. ospf_external_lsa_refresh_type (ospf, type, force);
  507. if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
  508. zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
  509. ospf_redist_string(type),
  510. metric_type (ospf, type), metric_value (ospf, type));
  511. return CMD_SUCCESS;
  512. }
  513. ospf->dmetric[type].type = mtype;
  514. ospf->dmetric[type].value = mvalue;
  515. zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT);
  516. if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
  517. zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
  518. ospf_redist_string(type),
  519. metric_type (ospf, type), metric_value (ospf, type));
  520. ospf_asbr_status_update (ospf, ++ospf->redistribute);
  521. return CMD_SUCCESS;
  522. }
  523. int
  524. ospf_redistribute_unset (struct ospf *ospf, int type)
  525. {
  526. if (type == zclient->redist_default)
  527. return CMD_SUCCESS;
  528. if (!ospf_is_type_redistributed (type))
  529. return CMD_SUCCESS;
  530. zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type, VRF_DEFAULT);
  531. if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
  532. zlog_debug ("Redistribute[%s]: Stop",
  533. ospf_redist_string(type));
  534. ospf->dmetric[type].type = -1;
  535. ospf->dmetric[type].value = -1;
  536. /* Remove the routes from OSPF table. */
  537. ospf_redistribute_withdraw (ospf, type);
  538. ospf_asbr_status_update (ospf, --ospf->redistribute);
  539. return CMD_SUCCESS;
  540. }
  541. int
  542. ospf_redistribute_default_set (struct ospf *ospf, int originate,
  543. int mtype, int mvalue)
  544. {
  545. ospf->default_originate = originate;
  546. ospf->dmetric[DEFAULT_ROUTE].type = mtype;
  547. ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
  548. if (ospf_is_type_redistributed (DEFAULT_ROUTE))
  549. {
  550. /* if ospf->default_originate changes value, is calling
  551. ospf_external_lsa_refresh_default sufficient to implement
  552. the change? */
  553. ospf_external_lsa_refresh_default (ospf);
  554. if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
  555. zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
  556. ospf_redist_string(DEFAULT_ROUTE),
  557. metric_type (ospf, DEFAULT_ROUTE),
  558. metric_value (ospf, DEFAULT_ROUTE));
  559. return CMD_SUCCESS;
  560. }
  561. zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient,
  562. VRF_DEFAULT);
  563. if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
  564. zlog_debug ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
  565. metric_type (ospf, DEFAULT_ROUTE),
  566. metric_value (ospf, DEFAULT_ROUTE));
  567. if (ospf->router_id.s_addr == 0)
  568. ospf->external_origin |= (1 << DEFAULT_ROUTE);
  569. else
  570. thread_add_timer (master, ospf_default_originate_timer, ospf, 1);
  571. ospf_asbr_status_update (ospf, ++ospf->redistribute);
  572. return CMD_SUCCESS;
  573. }
  574. int
  575. ospf_redistribute_default_unset (struct ospf *ospf)
  576. {
  577. if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
  578. return CMD_SUCCESS;
  579. ospf->default_originate = DEFAULT_ORIGINATE_NONE;
  580. ospf->dmetric[DEFAULT_ROUTE].type = -1;
  581. ospf->dmetric[DEFAULT_ROUTE].value = -1;
  582. zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient,
  583. VRF_DEFAULT);
  584. if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
  585. zlog_debug ("Redistribute[DEFAULT]: Stop");
  586. ospf_asbr_status_update (ospf, --ospf->redistribute);
  587. return CMD_SUCCESS;
  588. }
  589. static int
  590. ospf_external_lsa_originate_check (struct ospf *ospf,
  591. struct external_info *ei)
  592. {
  593. /* If prefix is multicast, then do not originate LSA. */
  594. if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
  595. {
  596. zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
  597. "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
  598. return 0;
  599. }
  600. /* Take care of default-originate. */
  601. if (is_prefix_default (&ei->p))
  602. if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
  603. {
  604. zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-external-LSA "
  605. "for default");
  606. return 0;
  607. }
  608. return 1;
  609. }
  610. /* If connected prefix is OSPF enable interface, then do not announce. */
  611. int
  612. ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei)
  613. {
  614. struct listnode *node;
  615. struct ospf_interface *oi;
  616. for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
  617. if (prefix_match (oi->address, (struct prefix *) &ei->p))
  618. return 0;
  619. return 1;
  620. }
  621. /* return 1 if external LSA must be originated, 0 otherwise */
  622. int
  623. ospf_redistribute_check (struct ospf *ospf,
  624. struct external_info *ei, int *changed)
  625. {
  626. struct route_map_set_values save_values;
  627. struct prefix_ipv4 *p = &ei->p;
  628. u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
  629. if (changed)
  630. *changed = 0;
  631. if (!ospf_external_lsa_originate_check (ospf, ei))
  632. return 0;
  633. /* Take care connected route. */
  634. if (type == ZEBRA_ROUTE_CONNECT &&
  635. !ospf_distribute_check_connected (ospf, ei))
  636. return 0;
  637. if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type))
  638. /* distirbute-list exists, but access-list may not? */
  639. if (DISTRIBUTE_LIST (ospf, type))
  640. if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY)
  641. {
  642. if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
  643. zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
  644. ospf_redist_string(type),
  645. inet_ntoa (p->prefix), p->prefixlen);
  646. return 0;
  647. }
  648. save_values = ei->route_map_set;
  649. ospf_reset_route_map_set_values (&ei->route_map_set);
  650. /* apply route-map if needed */
  651. if (ROUTEMAP_NAME (ospf, type))
  652. {
  653. int ret;
  654. ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p,
  655. RMAP_OSPF, ei);
  656. if (ret == RMAP_DENYMATCH)
  657. {
  658. ei->route_map_set = save_values;
  659. if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
  660. zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
  661. ospf_redist_string(type),
  662. inet_ntoa (p->prefix), p->prefixlen);
  663. return 0;
  664. }
  665. /* check if 'route-map set' changed something */
  666. if (changed)
  667. *changed = !ospf_route_map_set_compare (&ei->route_map_set,
  668. &save_values);
  669. }
  670. return 1;
  671. }
  672. /* OSPF route-map set for redistribution */
  673. void
  674. ospf_routemap_set (struct ospf *ospf, int type, const char *name)
  675. {
  676. if (ROUTEMAP_NAME (ospf, type))
  677. free (ROUTEMAP_NAME (ospf, type));
  678. ROUTEMAP_NAME (ospf, type) = strdup (name);
  679. ROUTEMAP (ospf, type) = route_map_lookup_by_name (name);
  680. }
  681. void
  682. ospf_routemap_unset (struct ospf *ospf, int type)
  683. {
  684. if (ROUTEMAP_NAME (ospf, type))
  685. free (ROUTEMAP_NAME (ospf, type));
  686. ROUTEMAP_NAME (ospf, type) = NULL;
  687. ROUTEMAP (ospf, type) = NULL;
  688. }
  689. /* Zebra route add and delete treatment. */
  690. static int
  691. ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
  692. zebra_size_t length, vrf_id_t vrf_id)
  693. {
  694. struct stream *s;
  695. struct zapi_ipv4 api;
  696. unsigned long ifindex;
  697. struct in_addr nexthop;
  698. struct prefix_ipv4 p;
  699. struct external_info *ei;
  700. struct ospf *ospf;
  701. unsigned char plength = 0;
  702. s = zclient->ibuf;
  703. ifindex = 0;
  704. nexthop.s_addr = 0;
  705. /* Type, flags, message. */
  706. api.type = stream_getc (s);
  707. api.flags = stream_getc (s);
  708. api.message = stream_getc (s);
  709. /* IPv4 prefix. */
  710. memset (&p, 0, sizeof (struct prefix_ipv4));
  711. p.family = AF_INET;
  712. plength = stream_getc (s);
  713. p.prefixlen = MIN(IPV4_MAX_PREFIXLEN, plength);
  714. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  715. if (IPV4_NET127(ntohl(p.prefix.s_addr)))
  716. return 0;
  717. /* Nexthop, ifindex, distance, metric. */
  718. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  719. {
  720. api.nexthop_num = stream_getc (s);
  721. nexthop.s_addr = stream_get_ipv4 (s);
  722. }
  723. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
  724. {
  725. api.ifindex_num = stream_getc (s);
  726. /* XXX assert(api.ifindex_num == 1); */
  727. ifindex = stream_getl (s);
  728. }
  729. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  730. api.distance = stream_getc (s);
  731. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  732. api.metric = stream_getl (s);
  733. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
  734. api.tag = stream_getl (s);
  735. else
  736. api.tag = 0;
  737. ospf = ospf_lookup ();
  738. if (ospf == NULL)
  739. return 0;
  740. if (command == ZEBRA_IPV4_ROUTE_ADD)
  741. {
  742. /* XXX|HACK|TODO|FIXME:
  743. * Maybe we should ignore reject/blackhole routes? Testing shows that
  744. * there is no problems though and this is only way to "summarize"
  745. * routes in ASBR at the moment. Maybe we need just a better generalised
  746. * solution for these types?
  747. *
  748. * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
  749. * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
  750. * return 0;
  751. */
  752. /* Protocol tag overwrites all other tag value send by zebra */
  753. if (ospf->dtag[api.type] > 0)
  754. api.tag = ospf->dtag[api.type];
  755. ei = ospf_external_info_add (api.type, p, ifindex, nexthop, api.tag);
  756. if (ospf->router_id.s_addr == 0)
  757. /* Set flags to generate AS-external-LSA originate event
  758. for each redistributed protocols later. */
  759. ospf->external_origin |= (1 << api.type);
  760. else
  761. {
  762. if (ei)
  763. {
  764. if (is_prefix_default (&p))
  765. ospf_external_lsa_refresh_default (ospf);
  766. else
  767. {
  768. struct ospf_lsa *current;
  769. current = ospf_external_info_find_lsa (ospf, &ei->p);
  770. if (!current)
  771. ospf_external_lsa_originate (ospf, ei);
  772. else if (IS_LSA_MAXAGE (current))
  773. ospf_external_lsa_refresh (ospf, current,
  774. ei, LSA_REFRESH_FORCE);
  775. else
  776. zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
  777. inet_ntoa (p.prefix));
  778. }
  779. }
  780. }
  781. }
  782. else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
  783. {
  784. ospf_external_info_delete (api.type, p);
  785. if (is_prefix_default (&p))
  786. ospf_external_lsa_refresh_default (ospf);
  787. else
  788. ospf_external_lsa_flush (ospf, api.type, &p, ifindex /*, nexthop */);
  789. }
  790. return 0;
  791. }
  792. int
  793. ospf_distribute_list_out_set (struct ospf *ospf, int type, const char *name)
  794. {
  795. /* Lookup access-list for distribute-list. */
  796. DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name);
  797. /* Clear previous distribute-name. */
  798. if (DISTRIBUTE_NAME (ospf, type))
  799. free (DISTRIBUTE_NAME (ospf, type));
  800. /* Set distribute-name. */
  801. DISTRIBUTE_NAME (ospf, type) = strdup (name);
  802. /* If access-list have been set, schedule update timer. */
  803. if (DISTRIBUTE_LIST (ospf, type))
  804. ospf_distribute_list_update (ospf, type);
  805. return CMD_SUCCESS;
  806. }
  807. int
  808. ospf_distribute_list_out_unset (struct ospf *ospf, int type, const char *name)
  809. {
  810. /* Schedule update timer. */
  811. if (DISTRIBUTE_LIST (ospf, type))
  812. ospf_distribute_list_update (ospf, type);
  813. /* Unset distribute-list. */
  814. DISTRIBUTE_LIST (ospf, type) = NULL;
  815. /* Clear distribute-name. */
  816. if (DISTRIBUTE_NAME (ospf, type))
  817. free (DISTRIBUTE_NAME (ospf, type));
  818. DISTRIBUTE_NAME (ospf, type) = NULL;
  819. return CMD_SUCCESS;
  820. }
  821. /* distribute-list update timer. */
  822. static int
  823. ospf_distribute_list_update_timer (struct thread *thread)
  824. {
  825. struct route_node *rn;
  826. struct external_info *ei;
  827. struct route_table *rt;
  828. struct ospf_lsa *lsa;
  829. int type, default_refresh = 0;
  830. struct ospf *ospf;
  831. ospf = ospf_lookup ();
  832. if (ospf == NULL)
  833. return 0;
  834. ospf->t_distribute_update = NULL;
  835. zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
  836. /* foreach all external info. */
  837. for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
  838. {
  839. rt = EXTERNAL_INFO (type);
  840. if (!rt)
  841. continue;
  842. for (rn = route_top (rt); rn; rn = route_next (rn))
  843. if ((ei = rn->info) != NULL)
  844. {
  845. if (is_prefix_default (&ei->p))
  846. default_refresh = 1;
  847. else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
  848. ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED);
  849. else
  850. ospf_external_lsa_originate (ospf, ei);
  851. }
  852. }
  853. if (default_refresh)
  854. ospf_external_lsa_refresh_default (ospf);
  855. return 0;
  856. }
  857. /* Update distribute-list and set timer to apply access-list. */
  858. void
  859. ospf_distribute_list_update (struct ospf *ospf, uintptr_t type)
  860. {
  861. struct route_table *rt;
  862. /* External info does not exist. */
  863. if (!(rt = EXTERNAL_INFO (type)))
  864. return;
  865. /* If exists previously invoked thread, then let it continue. */
  866. if (ospf->t_distribute_update)
  867. return;
  868. /* Set timer. */
  869. ospf->t_distribute_update =
  870. thread_add_timer_msec (master, ospf_distribute_list_update_timer,
  871. (void *) type, ospf->min_ls_interval);
  872. }
  873. /* If access-list is updated, apply some check. */
  874. static void
  875. ospf_filter_update (const char *name)
  876. {
  877. struct ospf *ospf;
  878. int type;
  879. int abr_inv = 0;
  880. struct ospf_area *area;
  881. struct listnode *node;
  882. /* If OSPF instatnce does not exist, return right now. */
  883. ospf = ospf_lookup ();
  884. if (ospf == NULL)
  885. return;
  886. /* Update distribute-list, and apply filter. */
  887. for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
  888. {
  889. if (ROUTEMAP (ospf, type) != NULL)
  890. {
  891. /* if route-map is not NULL it may be using this access list */
  892. ospf_distribute_list_update (ospf, type);
  893. continue;
  894. }
  895. /* There is place for route-map for default-information (ZEBRA_ROUTE_MAX),
  896. * but no distribute list. */
  897. if (type == ZEBRA_ROUTE_MAX)
  898. break;
  899. if (DISTRIBUTE_NAME (ospf, type))
  900. {
  901. /* Keep old access-list for distribute-list. */
  902. struct access_list *old = DISTRIBUTE_LIST (ospf, type);
  903. /* Update access-list for distribute-list. */
  904. DISTRIBUTE_LIST (ospf, type) =
  905. access_list_lookup (AFI_IP, DISTRIBUTE_NAME (ospf, type));
  906. /* No update for this distribute type. */
  907. if (old == NULL && DISTRIBUTE_LIST (ospf, type) == NULL)
  908. continue;
  909. /* Schedule distribute-list update timer. */
  910. if (DISTRIBUTE_LIST (ospf, type) == NULL ||
  911. strcmp (DISTRIBUTE_NAME (ospf, type), name) == 0)
  912. ospf_distribute_list_update (ospf, type);
  913. }
  914. }
  915. /* Update Area access-list. */
  916. for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
  917. {
  918. if (EXPORT_NAME (area))
  919. {
  920. EXPORT_LIST (area) = NULL;
  921. abr_inv++;
  922. }
  923. if (IMPORT_NAME (area))
  924. {
  925. IMPORT_LIST (area) = NULL;
  926. abr_inv++;
  927. }
  928. }
  929. /* Schedule ABR tasks -- this will be changed -- takada. */
  930. if (IS_OSPF_ABR (ospf) && abr_inv)
  931. ospf_schedule_abr_task (ospf);
  932. }
  933. /* If prefix-list is updated, do some updates. */
  934. void
  935. ospf_prefix_list_update (struct prefix_list *plist)
  936. {
  937. struct ospf *ospf;
  938. int type;
  939. int abr_inv = 0;
  940. struct ospf_area *area;
  941. struct listnode *node;
  942. /* If OSPF instatnce does not exist, return right now. */
  943. ospf = ospf_lookup ();
  944. if (ospf == NULL)
  945. return;
  946. /* Update all route-maps which are used as redistribution filters.
  947. * They might use prefix-list.
  948. */
  949. for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
  950. {
  951. if (ROUTEMAP (ospf, type) != NULL)
  952. {
  953. /* If route-map is not NULL it may be using this prefix list */
  954. ospf_distribute_list_update (ospf, type);
  955. continue;
  956. }
  957. }
  958. /* Update area filter-lists. */
  959. for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
  960. {
  961. /* Update filter-list in. */
  962. if (PREFIX_NAME_IN (area))
  963. if (strcmp (PREFIX_NAME_IN (area), prefix_list_name (plist)) == 0)
  964. {
  965. PREFIX_LIST_IN (area) =
  966. prefix_list_lookup (AFI_IP, PREFIX_NAME_IN (area));
  967. abr_inv++;
  968. }
  969. /* Update filter-list out. */
  970. if (PREFIX_NAME_OUT (area))
  971. if (strcmp (PREFIX_NAME_OUT (area), prefix_list_name (plist)) == 0)
  972. {
  973. PREFIX_LIST_IN (area) =
  974. prefix_list_lookup (AFI_IP, PREFIX_NAME_OUT (area));
  975. abr_inv++;
  976. }
  977. }
  978. /* Schedule ABR task. */
  979. if (IS_OSPF_ABR (ospf) && abr_inv)
  980. ospf_schedule_abr_task (ospf);
  981. }
  982. static struct ospf_distance *
  983. ospf_distance_new (void)
  984. {
  985. return XCALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
  986. }
  987. static void
  988. ospf_distance_free (struct ospf_distance *odistance)
  989. {
  990. XFREE (MTYPE_OSPF_DISTANCE, odistance);
  991. }
  992. int
  993. ospf_distance_set (struct vty *vty, struct ospf *ospf,
  994. const char *distance_str,
  995. const char *ip_str,
  996. const char *access_list_str)
  997. {
  998. int ret;
  999. struct prefix_ipv4 p;
  1000. u_char distance;
  1001. struct route_node *rn;
  1002. struct ospf_distance *odistance;
  1003. ret = str2prefix_ipv4 (ip_str, &p);
  1004. if (ret == 0)
  1005. {
  1006. vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
  1007. return CMD_WARNING;
  1008. }
  1009. distance = atoi (distance_str);
  1010. /* Get OSPF distance node. */
  1011. rn = route_node_get (ospf->distance_table, (struct prefix *) &p);
  1012. if (rn->info)
  1013. {
  1014. odistance = rn->info;
  1015. route_unlock_node (rn);
  1016. }
  1017. else
  1018. {
  1019. odistance = ospf_distance_new ();
  1020. rn->info = odistance;
  1021. }
  1022. /* Set distance value. */
  1023. odistance->distance = distance;
  1024. /* Reset access-list configuration. */
  1025. if (odistance->access_list)
  1026. {
  1027. free (odistance->access_list);
  1028. odistance->access_list = NULL;
  1029. }
  1030. if (access_list_str)
  1031. odistance->access_list = strdup (access_list_str);
  1032. return CMD_SUCCESS;
  1033. }
  1034. int
  1035. ospf_distance_unset (struct vty *vty, struct ospf *ospf,
  1036. const char *distance_str,
  1037. const char *ip_str, char
  1038. const *access_list_str)
  1039. {
  1040. int ret;
  1041. struct prefix_ipv4 p;
  1042. struct route_node *rn;
  1043. struct ospf_distance *odistance;
  1044. ret = str2prefix_ipv4 (ip_str, &p);
  1045. if (ret == 0)
  1046. {
  1047. vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
  1048. return CMD_WARNING;
  1049. }
  1050. rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p);
  1051. if (!rn)
  1052. {
  1053. vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
  1054. return CMD_WARNING;
  1055. }
  1056. odistance = rn->info;
  1057. if (odistance->access_list)
  1058. free (odistance->access_list);
  1059. ospf_distance_free (odistance);
  1060. rn->info = NULL;
  1061. route_unlock_node (rn);
  1062. route_unlock_node (rn);
  1063. return CMD_SUCCESS;
  1064. }
  1065. void
  1066. ospf_distance_reset (struct ospf *ospf)
  1067. {
  1068. struct route_node *rn;
  1069. struct ospf_distance *odistance;
  1070. for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn))
  1071. if ((odistance = rn->info) != NULL)
  1072. {
  1073. if (odistance->access_list)
  1074. free (odistance->access_list);
  1075. ospf_distance_free (odistance);
  1076. rn->info = NULL;
  1077. route_unlock_node (rn);
  1078. }
  1079. }
  1080. u_char
  1081. ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
  1082. {
  1083. struct ospf *ospf;
  1084. ospf = ospf_lookup ();
  1085. if (ospf == NULL)
  1086. return 0;
  1087. if (ospf->distance_intra)
  1088. if (or->path_type == OSPF_PATH_INTRA_AREA)
  1089. return ospf->distance_intra;
  1090. if (ospf->distance_inter)
  1091. if (or->path_type == OSPF_PATH_INTER_AREA)
  1092. return ospf->distance_inter;
  1093. if (ospf->distance_external)
  1094. if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
  1095. || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
  1096. return ospf->distance_external;
  1097. if (ospf->distance_all)
  1098. return ospf->distance_all;
  1099. return 0;
  1100. }
  1101. static void
  1102. ospf_zebra_connected (struct zclient *zclient)
  1103. {
  1104. zclient_send_requests (zclient, VRF_DEFAULT);
  1105. }
  1106. void
  1107. ospf_zebra_init (struct thread_master *master)
  1108. {
  1109. /* Allocate zebra structure. */
  1110. zclient = zclient_new (master);
  1111. zclient_init (zclient, ZEBRA_ROUTE_OSPF);
  1112. zclient->zebra_connected = ospf_zebra_connected;
  1113. zclient->router_id_update = ospf_router_id_update_zebra;
  1114. zclient->interface_add = ospf_interface_add;
  1115. zclient->interface_delete = ospf_interface_delete;
  1116. zclient->interface_up = ospf_interface_state_up;
  1117. zclient->interface_down = ospf_interface_state_down;
  1118. zclient->interface_address_add = ospf_interface_address_add;
  1119. zclient->interface_address_delete = ospf_interface_address_delete;
  1120. zclient->interface_link_params = ospf_interface_link_params;
  1121. zclient->ipv4_route_add = ospf_zebra_read_ipv4;
  1122. zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
  1123. access_list_add_hook (ospf_filter_update);
  1124. access_list_delete_hook (ospf_filter_update);
  1125. prefix_list_add_hook (ospf_prefix_list_update);
  1126. prefix_list_delete_hook (ospf_prefix_list_update);
  1127. }