ospf_zebra.c 34 KB

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