ospf_zebra.c 34 KB

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