ospf_zebra.c 34 KB

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