ospf_zebra.c 35 KB

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