ospf_apiserver.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644
  1. /*
  2. * Server side of OSPF API.
  3. * Copyright (C) 2001, 2002 Ralph Keller
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2, or (at your
  10. * option) any 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. #ifdef SUPPORT_OSPF_API
  24. #ifndef HAVE_OPAQUE_LSA
  25. #error "Core Opaque-LSA module must be configured."
  26. #endif /* HAVE_OPAQUE_LSA */
  27. #include "linklist.h"
  28. #include "prefix.h"
  29. #include "if.h"
  30. #include "table.h"
  31. #include "memory.h"
  32. #include "command.h"
  33. #include "vty.h"
  34. #include "stream.h"
  35. #include "log.h"
  36. #include "thread.h"
  37. #include "hash.h"
  38. #include "sockunion.h" /* for inet_aton() */
  39. #include "buffer.h"
  40. #include <sys/types.h>
  41. #include "ospfd/ospfd.h" /* for "struct thread_master" */
  42. #include "ospfd/ospf_interface.h"
  43. #include "ospfd/ospf_ism.h"
  44. #include "ospfd/ospf_asbr.h"
  45. #include "ospfd/ospf_lsa.h"
  46. #include "ospfd/ospf_lsdb.h"
  47. #include "ospfd/ospf_neighbor.h"
  48. #include "ospfd/ospf_nsm.h"
  49. #include "ospfd/ospf_flood.h"
  50. #include "ospfd/ospf_packet.h"
  51. #include "ospfd/ospf_spf.h"
  52. #include "ospfd/ospf_dump.h"
  53. #include "ospfd/ospf_route.h"
  54. #include "ospfd/ospf_ase.h"
  55. #include "ospfd/ospf_zebra.h"
  56. #include "ospfd/ospf_api.h"
  57. #include "ospfd/ospf_apiserver.h"
  58. /* This is an implementation of an API to the OSPF daemon that allows
  59. * external applications to access the OSPF daemon through socket
  60. * connections. The application can use this API to inject its own
  61. * opaque LSAs and flood them to other OSPF daemons. Other OSPF
  62. * daemons then receive these LSAs and inform applications through the
  63. * API by sending a corresponding message. The application can also
  64. * register to receive all LSA types (in addition to opaque types) and
  65. * use this information to reconstruct the OSPF's LSDB. The OSPF
  66. * daemon supports multiple applications concurrently. */
  67. /* List of all active connections. */
  68. struct list *apiserver_list;
  69. /* -----------------------------------------------------------
  70. * Functions to lookup interfaces
  71. * -----------------------------------------------------------
  72. */
  73. struct ospf_interface *
  74. ospf_apiserver_if_lookup_by_addr (struct in_addr address)
  75. {
  76. struct listnode *node, *nnode;
  77. struct ospf_interface *oi;
  78. struct ospf *ospf;
  79. if (!(ospf = ospf_lookup ()))
  80. return NULL;
  81. for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
  82. if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
  83. if (IPV4_ADDR_SAME (&address, &oi->address->u.prefix4))
  84. return oi;
  85. return NULL;
  86. }
  87. struct ospf_interface *
  88. ospf_apiserver_if_lookup_by_ifp (struct interface *ifp)
  89. {
  90. struct listnode *node, *nnode;
  91. struct ospf_interface *oi;
  92. struct ospf *ospf;
  93. if (!(ospf = ospf_lookup ()))
  94. return NULL;
  95. for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
  96. if (oi->ifp == ifp)
  97. return oi;
  98. return NULL;
  99. }
  100. /* -----------------------------------------------------------
  101. * Initialization
  102. * -----------------------------------------------------------
  103. */
  104. unsigned short
  105. ospf_apiserver_getport (void)
  106. {
  107. struct servent *sp = getservbyname ("ospfapi", "tcp");
  108. return sp ? ntohs (sp->s_port) : OSPF_API_SYNC_PORT;
  109. }
  110. /* Initialize OSPF API module. Invoked from ospf_opaque_init() */
  111. int
  112. ospf_apiserver_init (void)
  113. {
  114. int fd;
  115. int rc = -1;
  116. /* Create new socket for synchronous messages. */
  117. fd = ospf_apiserver_serv_sock_family (ospf_apiserver_getport (), AF_INET);
  118. if (fd < 0)
  119. goto out;
  120. /* Schedule new thread that handles accepted connections. */
  121. ospf_apiserver_event (OSPF_APISERVER_ACCEPT, fd, NULL);
  122. /* Initialize list that keeps track of all connections. */
  123. apiserver_list = list_new ();
  124. /* Register opaque-independent call back functions. These functions
  125. are invoked on ISM, NSM changes and LSA update and LSA deletes */
  126. rc =
  127. ospf_register_opaque_functab (0 /* all LSAs */,
  128. 0 /* all opaque types */,
  129. ospf_apiserver_new_if,
  130. ospf_apiserver_del_if,
  131. ospf_apiserver_ism_change,
  132. ospf_apiserver_nsm_change,
  133. NULL,
  134. NULL,
  135. NULL,
  136. NULL, /* ospf_apiserver_show_info */
  137. NULL, /* originator_func */
  138. NULL, /* ospf_apiserver_lsa_refresher */
  139. ospf_apiserver_lsa_update,
  140. ospf_apiserver_lsa_delete);
  141. if (rc != 0)
  142. {
  143. zlog_warn ("ospf_apiserver_init: Failed to register opaque type [0/0]");
  144. }
  145. rc = 0;
  146. out:
  147. return rc;
  148. }
  149. /* Terminate OSPF API module. */
  150. void
  151. ospf_apiserver_term (void)
  152. {
  153. struct ospf_apiserver *apiserv;
  154. /* Unregister wildcard [0/0] type */
  155. ospf_delete_opaque_functab (0 /* all LSAs */,
  156. 0 /* all opaque types */);
  157. /*
  158. * Free all client instances. ospf_apiserver_free removes the node
  159. * from the list, so we examine the head of the list anew each time.
  160. */
  161. while ( apiserver_list &&
  162. (apiserv = listgetdata (listhead (apiserver_list))) != NULL)
  163. ospf_apiserver_free (apiserv);
  164. /* Free client list itself */
  165. if (apiserver_list)
  166. list_delete (apiserver_list);
  167. /* Free wildcard list */
  168. /* XXX */
  169. }
  170. static struct ospf_apiserver *
  171. lookup_apiserver (u_char lsa_type, u_char opaque_type)
  172. {
  173. struct listnode *n1, *n2;
  174. struct registered_opaque_type *r;
  175. struct ospf_apiserver *apiserv, *found = NULL;
  176. /* XXX: this approaches O(n**2) */
  177. for (ALL_LIST_ELEMENTS_RO (apiserver_list, n1, apiserv))
  178. {
  179. for (ALL_LIST_ELEMENTS_RO (apiserv->opaque_types, n2, r))
  180. if (r->lsa_type == lsa_type && r->opaque_type == opaque_type)
  181. {
  182. found = apiserv;
  183. goto out;
  184. }
  185. }
  186. out:
  187. return found;
  188. }
  189. static struct ospf_apiserver *
  190. lookup_apiserver_by_lsa (struct ospf_lsa *lsa)
  191. {
  192. struct lsa_header *lsah = lsa->data;
  193. struct ospf_apiserver *found = NULL;
  194. if (IS_OPAQUE_LSA (lsah->type))
  195. {
  196. found = lookup_apiserver (lsah->type,
  197. GET_OPAQUE_TYPE (ntohl (lsah->id.s_addr)));
  198. }
  199. return found;
  200. }
  201. /* -----------------------------------------------------------
  202. * Followings are functions to manage client connections.
  203. * -----------------------------------------------------------
  204. */
  205. static int
  206. ospf_apiserver_new_lsa_hook (struct ospf_lsa *lsa)
  207. {
  208. if (IS_DEBUG_OSPF_EVENT)
  209. zlog_debug ("API: Put LSA(%p)[%s] into reserve, total=%ld", (void *)lsa,
  210. dump_lsa_key (lsa), lsa->lsdb->total);
  211. return 0;
  212. }
  213. static int
  214. ospf_apiserver_del_lsa_hook (struct ospf_lsa *lsa)
  215. {
  216. if (IS_DEBUG_OSPF_EVENT)
  217. zlog_debug ("API: Get LSA(%p)[%s] from reserve, total=%ld", (void *)lsa,
  218. dump_lsa_key (lsa), lsa->lsdb->total);
  219. return 0;
  220. }
  221. /* Allocate new connection structure. */
  222. struct ospf_apiserver *
  223. ospf_apiserver_new (int fd_sync, int fd_async)
  224. {
  225. struct ospf_apiserver *new =
  226. XMALLOC (MTYPE_OSPF_APISERVER, sizeof (struct ospf_apiserver));
  227. new->filter =
  228. XMALLOC (MTYPE_OSPF_APISERVER_MSGFILTER, sizeof (struct lsa_filter_type));
  229. new->fd_sync = fd_sync;
  230. new->fd_async = fd_async;
  231. /* list of registered opaque types that application uses */
  232. new->opaque_types = list_new ();
  233. /* Initialize temporary strage for LSA instances to be refreshed. */
  234. memset (&new->reserve, 0, sizeof (struct ospf_lsdb));
  235. ospf_lsdb_init (&new->reserve);
  236. new->reserve.new_lsa_hook = ospf_apiserver_new_lsa_hook; /* debug */
  237. new->reserve.del_lsa_hook = ospf_apiserver_del_lsa_hook; /* debug */
  238. new->out_sync_fifo = msg_fifo_new ();
  239. new->out_async_fifo = msg_fifo_new ();
  240. new->t_sync_read = NULL;
  241. #ifdef USE_ASYNC_READ
  242. new->t_async_read = NULL;
  243. #endif /* USE_ASYNC_READ */
  244. new->t_sync_write = NULL;
  245. new->t_async_write = NULL;
  246. new->filter->typemask = 0; /* filter all LSAs */
  247. new->filter->origin = ANY_ORIGIN;
  248. new->filter->num_areas = 0;
  249. return new;
  250. }
  251. void
  252. ospf_apiserver_event (enum event event, int fd,
  253. struct ospf_apiserver *apiserv)
  254. {
  255. switch (event)
  256. {
  257. case OSPF_APISERVER_ACCEPT:
  258. (void)thread_add_read (master, ospf_apiserver_accept, apiserv, fd);
  259. break;
  260. case OSPF_APISERVER_SYNC_READ:
  261. apiserv->t_sync_read =
  262. thread_add_read (master, ospf_apiserver_read, apiserv, fd);
  263. break;
  264. #ifdef USE_ASYNC_READ
  265. case OSPF_APISERVER_ASYNC_READ:
  266. apiserv->t_async_read =
  267. thread_add_read (master, ospf_apiserver_read, apiserv, fd);
  268. break;
  269. #endif /* USE_ASYNC_READ */
  270. case OSPF_APISERVER_SYNC_WRITE:
  271. if (!apiserv->t_sync_write)
  272. {
  273. apiserv->t_sync_write =
  274. thread_add_write (master, ospf_apiserver_sync_write, apiserv, fd);
  275. }
  276. break;
  277. case OSPF_APISERVER_ASYNC_WRITE:
  278. if (!apiserv->t_async_write)
  279. {
  280. apiserv->t_async_write =
  281. thread_add_write (master, ospf_apiserver_async_write, apiserv, fd);
  282. }
  283. break;
  284. }
  285. }
  286. /* Free instance. First unregister all opaque types used by
  287. application, flush opaque LSAs injected by application
  288. from network and close connection. */
  289. void
  290. ospf_apiserver_free (struct ospf_apiserver *apiserv)
  291. {
  292. struct listnode *node;
  293. /* Cancel read and write threads. */
  294. if (apiserv->t_sync_read)
  295. {
  296. thread_cancel (apiserv->t_sync_read);
  297. }
  298. #ifdef USE_ASYNC_READ
  299. if (apiserv->t_async_read)
  300. {
  301. thread_cancel (apiserv->t_async_read);
  302. }
  303. #endif /* USE_ASYNC_READ */
  304. if (apiserv->t_sync_write)
  305. {
  306. thread_cancel (apiserv->t_sync_write);
  307. }
  308. if (apiserv->t_async_write)
  309. {
  310. thread_cancel (apiserv->t_async_write);
  311. }
  312. /* Unregister all opaque types that application registered
  313. and flush opaque LSAs if still in LSDB. */
  314. while ((node = listhead (apiserv->opaque_types)) != NULL)
  315. {
  316. struct registered_opaque_type *regtype = listgetdata(node);
  317. ospf_apiserver_unregister_opaque_type (apiserv, regtype->lsa_type,
  318. regtype->opaque_type);
  319. }
  320. /* Close connections to OSPFd. */
  321. if (apiserv->fd_sync > 0)
  322. {
  323. close (apiserv->fd_sync);
  324. }
  325. if (apiserv->fd_async > 0)
  326. {
  327. close (apiserv->fd_async);
  328. }
  329. /* Free fifos */
  330. msg_fifo_free (apiserv->out_sync_fifo);
  331. msg_fifo_free (apiserv->out_async_fifo);
  332. /* Clear temporary strage for LSA instances to be refreshed. */
  333. ospf_lsdb_delete_all (&apiserv->reserve);
  334. ospf_lsdb_cleanup (&apiserv->reserve);
  335. /* Remove from the list of active clients. */
  336. listnode_delete (apiserver_list, apiserv);
  337. if (IS_DEBUG_OSPF_EVENT)
  338. zlog_debug ("API: Delete apiserv(%p), total#(%d)",
  339. (void *)apiserv, apiserver_list->count);
  340. /* And free instance. */
  341. XFREE (MTYPE_OSPF_APISERVER, apiserv);
  342. }
  343. int
  344. ospf_apiserver_read (struct thread *thread)
  345. {
  346. struct ospf_apiserver *apiserv;
  347. struct msg *msg;
  348. int fd;
  349. int rc = -1;
  350. enum event event;
  351. apiserv = THREAD_ARG (thread);
  352. fd = THREAD_FD (thread);
  353. if (fd == apiserv->fd_sync)
  354. {
  355. event = OSPF_APISERVER_SYNC_READ;
  356. apiserv->t_sync_read = NULL;
  357. if (IS_DEBUG_OSPF_EVENT)
  358. zlog_debug ("API: ospf_apiserver_read: Peer: %s/%u",
  359. inet_ntoa (apiserv->peer_sync.sin_addr),
  360. ntohs (apiserv->peer_sync.sin_port));
  361. }
  362. #ifdef USE_ASYNC_READ
  363. else if (fd == apiserv->fd_async)
  364. {
  365. event = OSPF_APISERVER_ASYNC_READ;
  366. apiserv->t_async_read = NULL;
  367. if (IS_DEBUG_OSPF_EVENT)
  368. zlog_debug ("API: ospf_apiserver_read: Peer: %s/%u",
  369. inet_ntoa (apiserv->peer_async.sin_addr),
  370. ntohs (apiserv->peer_async.sin_port));
  371. }
  372. #endif /* USE_ASYNC_READ */
  373. else
  374. {
  375. zlog_warn ("ospf_apiserver_read: Unknown fd(%d)", fd);
  376. ospf_apiserver_free (apiserv);
  377. goto out;
  378. }
  379. /* Read message from fd. */
  380. msg = msg_read (fd);
  381. if (msg == NULL)
  382. {
  383. zlog_warn
  384. ("ospf_apiserver_read: read failed on fd=%d, closing connection", fd);
  385. /* Perform cleanup. */
  386. ospf_apiserver_free (apiserv);
  387. goto out;
  388. }
  389. if (IS_DEBUG_OSPF_EVENT)
  390. msg_print (msg);
  391. /* Dispatch to corresponding message handler. */
  392. rc = ospf_apiserver_handle_msg (apiserv, msg);
  393. /* Prepare for next message, add read thread. */
  394. ospf_apiserver_event (event, fd, apiserv);
  395. msg_free (msg);
  396. out:
  397. return rc;
  398. }
  399. int
  400. ospf_apiserver_sync_write (struct thread *thread)
  401. {
  402. struct ospf_apiserver *apiserv;
  403. struct msg *msg;
  404. int fd;
  405. int rc = -1;
  406. apiserv = THREAD_ARG (thread);
  407. assert (apiserv);
  408. fd = THREAD_FD (thread);
  409. apiserv->t_sync_write = NULL;
  410. /* Sanity check */
  411. if (fd != apiserv->fd_sync)
  412. {
  413. zlog_warn ("ospf_apiserver_sync_write: Unknown fd=%d", fd);
  414. goto out;
  415. }
  416. if (IS_DEBUG_OSPF_EVENT)
  417. zlog_debug ("API: ospf_apiserver_sync_write: Peer: %s/%u",
  418. inet_ntoa (apiserv->peer_sync.sin_addr),
  419. ntohs (apiserv->peer_sync.sin_port));
  420. /* Check whether there is really a message in the fifo. */
  421. msg = msg_fifo_pop (apiserv->out_sync_fifo);
  422. if (!msg)
  423. {
  424. zlog_warn ("API: ospf_apiserver_sync_write: No message in Sync-FIFO?");
  425. return 0;
  426. }
  427. if (IS_DEBUG_OSPF_EVENT)
  428. msg_print (msg);
  429. rc = msg_write (fd, msg);
  430. /* Once a message is dequeued, it should be freed anyway. */
  431. msg_free (msg);
  432. if (rc < 0)
  433. {
  434. zlog_warn
  435. ("ospf_apiserver_sync_write: write failed on fd=%d", fd);
  436. goto out;
  437. }
  438. /* If more messages are in sync message fifo, schedule write thread. */
  439. if (msg_fifo_head (apiserv->out_sync_fifo))
  440. {
  441. ospf_apiserver_event (OSPF_APISERVER_SYNC_WRITE, apiserv->fd_sync,
  442. apiserv);
  443. }
  444. out:
  445. if (rc < 0)
  446. {
  447. /* Perform cleanup and disconnect with peer */
  448. ospf_apiserver_free (apiserv);
  449. }
  450. return rc;
  451. }
  452. int
  453. ospf_apiserver_async_write (struct thread *thread)
  454. {
  455. struct ospf_apiserver *apiserv;
  456. struct msg *msg;
  457. int fd;
  458. int rc = -1;
  459. apiserv = THREAD_ARG (thread);
  460. assert (apiserv);
  461. fd = THREAD_FD (thread);
  462. apiserv->t_async_write = NULL;
  463. /* Sanity check */
  464. if (fd != apiserv->fd_async)
  465. {
  466. zlog_warn ("ospf_apiserver_async_write: Unknown fd=%d", fd);
  467. goto out;
  468. }
  469. if (IS_DEBUG_OSPF_EVENT)
  470. zlog_debug ("API: ospf_apiserver_async_write: Peer: %s/%u",
  471. inet_ntoa (apiserv->peer_async.sin_addr),
  472. ntohs (apiserv->peer_async.sin_port));
  473. /* Check whether there is really a message in the fifo. */
  474. msg = msg_fifo_pop (apiserv->out_async_fifo);
  475. if (!msg)
  476. {
  477. zlog_warn ("API: ospf_apiserver_async_write: No message in Async-FIFO?");
  478. return 0;
  479. }
  480. if (IS_DEBUG_OSPF_EVENT)
  481. msg_print (msg);
  482. rc = msg_write (fd, msg);
  483. /* Once a message is dequeued, it should be freed anyway. */
  484. msg_free (msg);
  485. if (rc < 0)
  486. {
  487. zlog_warn
  488. ("ospf_apiserver_async_write: write failed on fd=%d", fd);
  489. goto out;
  490. }
  491. /* If more messages are in async message fifo, schedule write thread. */
  492. if (msg_fifo_head (apiserv->out_async_fifo))
  493. {
  494. ospf_apiserver_event (OSPF_APISERVER_ASYNC_WRITE, apiserv->fd_async,
  495. apiserv);
  496. }
  497. out:
  498. if (rc < 0)
  499. {
  500. /* Perform cleanup and disconnect with peer */
  501. ospf_apiserver_free (apiserv);
  502. }
  503. return rc;
  504. }
  505. int
  506. ospf_apiserver_serv_sock_family (unsigned short port, int family)
  507. {
  508. union sockunion su;
  509. int accept_sock;
  510. int rc;
  511. memset (&su, 0, sizeof (union sockunion));
  512. su.sa.sa_family = family;
  513. /* Make new socket */
  514. accept_sock = sockunion_stream_socket (&su);
  515. if (accept_sock < 0)
  516. return accept_sock;
  517. /* This is a server, so reuse address and port */
  518. sockopt_reuseaddr (accept_sock);
  519. sockopt_reuseport (accept_sock);
  520. /* Bind socket to address and given port. */
  521. rc = sockunion_bind (accept_sock, &su, port, NULL);
  522. if (rc < 0)
  523. {
  524. close (accept_sock); /* Close socket */
  525. return rc;
  526. }
  527. /* Listen socket under queue length 3. */
  528. rc = listen (accept_sock, 3);
  529. if (rc < 0)
  530. {
  531. zlog_warn ("ospf_apiserver_serv_sock_family: listen: %s",
  532. safe_strerror (errno));
  533. close (accept_sock); /* Close socket */
  534. return rc;
  535. }
  536. return accept_sock;
  537. }
  538. /* Accept connection request from external applications. For each
  539. accepted connection allocate own connection instance. */
  540. int
  541. ospf_apiserver_accept (struct thread *thread)
  542. {
  543. int accept_sock;
  544. int new_sync_sock;
  545. int new_async_sock;
  546. union sockunion su;
  547. struct ospf_apiserver *apiserv;
  548. struct sockaddr_in peer_async;
  549. struct sockaddr_in peer_sync;
  550. unsigned int peerlen;
  551. int ret;
  552. /* THREAD_ARG (thread) is NULL */
  553. accept_sock = THREAD_FD (thread);
  554. /* Keep hearing on socket for further connections. */
  555. ospf_apiserver_event (OSPF_APISERVER_ACCEPT, accept_sock, NULL);
  556. memset (&su, 0, sizeof (union sockunion));
  557. /* Accept connection for synchronous messages */
  558. new_sync_sock = sockunion_accept (accept_sock, &su);
  559. if (new_sync_sock < 0)
  560. {
  561. zlog_warn ("ospf_apiserver_accept: accept: %s", safe_strerror (errno));
  562. return -1;
  563. }
  564. /* Get port address and port number of peer to make reverse connection.
  565. The reverse channel uses the port number of the peer port+1. */
  566. memset(&peer_sync, 0, sizeof(struct sockaddr_in));
  567. peerlen = sizeof (struct sockaddr_in);
  568. ret = getpeername (new_sync_sock, (struct sockaddr *)&peer_sync, &peerlen);
  569. if (ret < 0)
  570. {
  571. zlog_warn ("ospf_apiserver_accept: getpeername: %s", safe_strerror (errno));
  572. close (new_sync_sock);
  573. return -1;
  574. }
  575. if (IS_DEBUG_OSPF_EVENT)
  576. zlog_debug ("API: ospf_apiserver_accept: New peer: %s/%u",
  577. inet_ntoa (peer_sync.sin_addr), ntohs (peer_sync.sin_port));
  578. /* Create new socket for asynchronous messages. */
  579. peer_async = peer_sync;
  580. peer_async.sin_port = htons(ntohs(peer_sync.sin_port) + 1);
  581. /* Check if remote port number to make reverse connection is valid one. */
  582. if (ntohs (peer_async.sin_port) == ospf_apiserver_getport ())
  583. {
  584. zlog_warn ("API: ospf_apiserver_accept: Peer(%s/%u): Invalid async port number?",
  585. inet_ntoa (peer_async.sin_addr), ntohs (peer_async.sin_port));
  586. close (new_sync_sock);
  587. return -1;
  588. }
  589. new_async_sock = socket (AF_INET, SOCK_STREAM, 0);
  590. if (new_async_sock < 0)
  591. {
  592. zlog_warn ("ospf_apiserver_accept: socket: %s", safe_strerror (errno));
  593. close (new_sync_sock);
  594. return -1;
  595. }
  596. ret = connect (new_async_sock, (struct sockaddr *) &peer_async,
  597. sizeof (struct sockaddr_in));
  598. if (ret < 0)
  599. {
  600. zlog_warn ("ospf_apiserver_accept: connect: %s", safe_strerror (errno));
  601. close (new_sync_sock);
  602. close (new_async_sock);
  603. return -1;
  604. }
  605. #ifdef USE_ASYNC_READ
  606. #else /* USE_ASYNC_READ */
  607. /* Make the asynchronous channel write-only. */
  608. ret = shutdown (new_async_sock, SHUT_RD);
  609. if (ret < 0)
  610. {
  611. zlog_warn ("ospf_apiserver_accept: shutdown: %s", safe_strerror (errno));
  612. close (new_sync_sock);
  613. close (new_async_sock);
  614. return -1;
  615. }
  616. #endif /* USE_ASYNC_READ */
  617. /* Allocate new server-side connection structure */
  618. apiserv = ospf_apiserver_new (new_sync_sock, new_async_sock);
  619. /* Add to active connection list */
  620. listnode_add (apiserver_list, apiserv);
  621. apiserv->peer_sync = peer_sync;
  622. apiserv->peer_async = peer_async;
  623. /* And add read threads for new connection */
  624. ospf_apiserver_event (OSPF_APISERVER_SYNC_READ, new_sync_sock, apiserv);
  625. #ifdef USE_ASYNC_READ
  626. ospf_apiserver_event (OSPF_APISERVER_ASYNC_READ, new_async_sock, apiserv);
  627. #endif /* USE_ASYNC_READ */
  628. if (IS_DEBUG_OSPF_EVENT)
  629. zlog_debug ("API: New apiserv(%p), total#(%d)",
  630. (void *)apiserv, apiserver_list->count);
  631. return 0;
  632. }
  633. /* -----------------------------------------------------------
  634. * Send reply with return code to client application
  635. * -----------------------------------------------------------
  636. */
  637. static int
  638. ospf_apiserver_send_msg (struct ospf_apiserver *apiserv, struct msg *msg)
  639. {
  640. struct msg_fifo *fifo;
  641. struct msg *msg2;
  642. enum event event;
  643. int fd;
  644. switch (msg->hdr.msgtype)
  645. {
  646. case MSG_REPLY:
  647. fifo = apiserv->out_sync_fifo;
  648. fd = apiserv->fd_sync;
  649. event = OSPF_APISERVER_SYNC_WRITE;
  650. break;
  651. case MSG_READY_NOTIFY:
  652. case MSG_LSA_UPDATE_NOTIFY:
  653. case MSG_LSA_DELETE_NOTIFY:
  654. case MSG_NEW_IF:
  655. case MSG_DEL_IF:
  656. case MSG_ISM_CHANGE:
  657. case MSG_NSM_CHANGE:
  658. fifo = apiserv->out_async_fifo;
  659. fd = apiserv->fd_async;
  660. event = OSPF_APISERVER_ASYNC_WRITE;
  661. break;
  662. default:
  663. zlog_warn ("ospf_apiserver_send_msg: Unknown message type %d",
  664. msg->hdr.msgtype);
  665. return -1;
  666. }
  667. /* Make a copy of the message and put in the fifo. Once the fifo
  668. gets drained by the write thread, the message will be freed. */
  669. /* NB: Given "msg" is untouched in this function. */
  670. msg2 = msg_dup (msg);
  671. /* Enqueue message into corresponding fifo queue */
  672. msg_fifo_push (fifo, msg2);
  673. /* Schedule write thread */
  674. ospf_apiserver_event (event, fd, apiserv);
  675. return 0;
  676. }
  677. int
  678. ospf_apiserver_send_reply (struct ospf_apiserver *apiserv, u_int32_t seqnr,
  679. u_char rc)
  680. {
  681. struct msg *msg = new_msg_reply (seqnr, rc);
  682. int ret;
  683. if (!msg)
  684. {
  685. zlog_warn ("ospf_apiserver_send_reply: msg_new failed");
  686. #ifdef NOTYET
  687. /* Cannot allocate new message. What should we do? */
  688. ospf_apiserver_free (apiserv);
  689. #endif
  690. return -1;
  691. }
  692. ret = ospf_apiserver_send_msg (apiserv, msg);
  693. msg_free (msg);
  694. return ret;
  695. }
  696. /* -----------------------------------------------------------
  697. * Generic message dispatching handler function
  698. * -----------------------------------------------------------
  699. */
  700. int
  701. ospf_apiserver_handle_msg (struct ospf_apiserver *apiserv, struct msg *msg)
  702. {
  703. int rc;
  704. /* Call corresponding message handler function. */
  705. switch (msg->hdr.msgtype)
  706. {
  707. case MSG_REGISTER_OPAQUETYPE:
  708. rc = ospf_apiserver_handle_register_opaque_type (apiserv, msg);
  709. break;
  710. case MSG_UNREGISTER_OPAQUETYPE:
  711. rc = ospf_apiserver_handle_unregister_opaque_type (apiserv, msg);
  712. break;
  713. case MSG_REGISTER_EVENT:
  714. rc = ospf_apiserver_handle_register_event (apiserv, msg);
  715. break;
  716. case MSG_SYNC_LSDB:
  717. rc = ospf_apiserver_handle_sync_lsdb (apiserv, msg);
  718. break;
  719. case MSG_ORIGINATE_REQUEST:
  720. rc = ospf_apiserver_handle_originate_request (apiserv, msg);
  721. break;
  722. case MSG_DELETE_REQUEST:
  723. rc = ospf_apiserver_handle_delete_request (apiserv, msg);
  724. break;
  725. default:
  726. zlog_warn ("ospf_apiserver_handle_msg: Unknown message type: %d",
  727. msg->hdr.msgtype);
  728. rc = -1;
  729. }
  730. return rc;
  731. }
  732. /* -----------------------------------------------------------
  733. * Following are functions for opaque type registration
  734. * -----------------------------------------------------------
  735. */
  736. int
  737. ospf_apiserver_register_opaque_type (struct ospf_apiserver *apiserv,
  738. u_char lsa_type, u_char opaque_type)
  739. {
  740. struct registered_opaque_type *regtype;
  741. int (*originator_func) (void *arg);
  742. int rc;
  743. switch (lsa_type)
  744. {
  745. case OSPF_OPAQUE_LINK_LSA:
  746. originator_func = ospf_apiserver_lsa9_originator;
  747. break;
  748. case OSPF_OPAQUE_AREA_LSA:
  749. originator_func = ospf_apiserver_lsa10_originator;
  750. break;
  751. case OSPF_OPAQUE_AS_LSA:
  752. originator_func = ospf_apiserver_lsa11_originator;
  753. break;
  754. default:
  755. zlog_warn ("ospf_apiserver_register_opaque_type: lsa_type(%d)",
  756. lsa_type);
  757. return OSPF_API_ILLEGALLSATYPE;
  758. }
  759. /* Register opaque function table */
  760. /* NB: Duplicated registration will be detected inside the function. */
  761. rc =
  762. ospf_register_opaque_functab (lsa_type, opaque_type,
  763. NULL, /* ospf_apiserver_new_if */
  764. NULL, /* ospf_apiserver_del_if */
  765. NULL, /* ospf_apiserver_ism_change */
  766. NULL, /* ospf_apiserver_nsm_change */
  767. NULL,
  768. NULL,
  769. NULL,
  770. ospf_apiserver_show_info,
  771. originator_func,
  772. ospf_apiserver_lsa_refresher,
  773. NULL, /* ospf_apiserver_lsa_update */
  774. NULL /* ospf_apiserver_lsa_delete */);
  775. if (rc != 0)
  776. {
  777. zlog_warn ("Failed to register opaque type [%d/%d]",
  778. lsa_type, opaque_type);
  779. return OSPF_API_OPAQUETYPEINUSE;
  780. }
  781. /* Remember the opaque type that application registers so when
  782. connection shuts down, we can flush all LSAs of this opaque
  783. type. */
  784. regtype =
  785. XCALLOC (MTYPE_OSPF_APISERVER, sizeof (struct registered_opaque_type));
  786. regtype->lsa_type = lsa_type;
  787. regtype->opaque_type = opaque_type;
  788. /* Add to list of registered opaque types */
  789. listnode_add (apiserv->opaque_types, regtype);
  790. if (IS_DEBUG_OSPF_EVENT)
  791. zlog_debug ("API: Add LSA-type(%d)/Opaque-type(%d) into"
  792. " apiserv(%p), total#(%d)",
  793. lsa_type, opaque_type, (void *)apiserv,
  794. listcount (apiserv->opaque_types));
  795. return 0;
  796. }
  797. int
  798. ospf_apiserver_unregister_opaque_type (struct ospf_apiserver *apiserv,
  799. u_char lsa_type, u_char opaque_type)
  800. {
  801. struct listnode *node, *nnode;
  802. struct registered_opaque_type *regtype;
  803. for (ALL_LIST_ELEMENTS (apiserv->opaque_types, node, nnode, regtype))
  804. {
  805. /* Check if we really registered this opaque type */
  806. if (regtype->lsa_type == lsa_type &&
  807. regtype->opaque_type == opaque_type)
  808. {
  809. /* Yes, we registered this opaque type. Flush
  810. all existing opaque LSAs of this type */
  811. ospf_apiserver_flush_opaque_lsa (apiserv, lsa_type, opaque_type);
  812. ospf_delete_opaque_functab (lsa_type, opaque_type);
  813. /* Remove from list of registered opaque types */
  814. listnode_delete (apiserv->opaque_types, regtype);
  815. if (IS_DEBUG_OSPF_EVENT)
  816. zlog_debug ("API: Del LSA-type(%d)/Opaque-type(%d)"
  817. " from apiserv(%p), total#(%d)",
  818. lsa_type, opaque_type, (void *)apiserv,
  819. listcount (apiserv->opaque_types));
  820. return 0;
  821. }
  822. }
  823. /* Opaque type is not registered */
  824. zlog_warn ("Failed to unregister opaque type [%d/%d]",
  825. lsa_type, opaque_type);
  826. return OSPF_API_OPAQUETYPENOTREGISTERED;
  827. }
  828. static int
  829. apiserver_is_opaque_type_registered (struct ospf_apiserver *apiserv,
  830. u_char lsa_type, u_char opaque_type)
  831. {
  832. struct listnode *node, *nnode;
  833. struct registered_opaque_type *regtype;
  834. /* XXX: how many types are there? if few, why not just a bitmap? */
  835. for (ALL_LIST_ELEMENTS (apiserv->opaque_types, node, nnode, regtype))
  836. {
  837. /* Check if we really registered this opaque type */
  838. if (regtype->lsa_type == lsa_type &&
  839. regtype->opaque_type == opaque_type)
  840. {
  841. /* Yes registered */
  842. return 1;
  843. }
  844. }
  845. /* Not registered */
  846. return 0;
  847. }
  848. int
  849. ospf_apiserver_handle_register_opaque_type (struct ospf_apiserver *apiserv,
  850. struct msg *msg)
  851. {
  852. struct msg_register_opaque_type *rmsg;
  853. u_char lsa_type;
  854. u_char opaque_type;
  855. int rc = 0;
  856. /* Extract parameters from register opaque type message */
  857. rmsg = (struct msg_register_opaque_type *) STREAM_DATA (msg->s);
  858. lsa_type = rmsg->lsatype;
  859. opaque_type = rmsg->opaquetype;
  860. rc = ospf_apiserver_register_opaque_type (apiserv, lsa_type, opaque_type);
  861. /* Send a reply back to client including return code */
  862. rc = ospf_apiserver_send_reply (apiserv, ntohl (msg->hdr.msgseq), rc);
  863. if (rc < 0)
  864. goto out;
  865. /* Now inform application about opaque types that are ready */
  866. switch (lsa_type)
  867. {
  868. case OSPF_OPAQUE_LINK_LSA:
  869. ospf_apiserver_notify_ready_type9 (apiserv);
  870. break;
  871. case OSPF_OPAQUE_AREA_LSA:
  872. ospf_apiserver_notify_ready_type10 (apiserv);
  873. break;
  874. case OSPF_OPAQUE_AS_LSA:
  875. ospf_apiserver_notify_ready_type11 (apiserv);
  876. break;
  877. }
  878. out:
  879. return rc;
  880. }
  881. /* Notify specific client about all opaque types 9 that are ready. */
  882. void
  883. ospf_apiserver_notify_ready_type9 (struct ospf_apiserver *apiserv)
  884. {
  885. struct listnode *node, *nnode;
  886. struct listnode *node2, *nnode2;
  887. struct ospf *ospf;
  888. struct ospf_interface *oi;
  889. struct registered_opaque_type *r;
  890. ospf = ospf_lookup ();
  891. for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
  892. {
  893. /* Check if this interface is indeed ready for type 9 */
  894. if (!ospf_apiserver_is_ready_type9 (oi))
  895. continue;
  896. /* Check for registered opaque type 9 types */
  897. /* XXX: loop-de-loop - optimise me */
  898. for (ALL_LIST_ELEMENTS (apiserv->opaque_types, node2, nnode2, r))
  899. {
  900. struct msg *msg;
  901. if (r->lsa_type == OSPF_OPAQUE_LINK_LSA)
  902. {
  903. /* Yes, this opaque type is ready */
  904. msg = new_msg_ready_notify (0, OSPF_OPAQUE_LINK_LSA,
  905. r->opaque_type,
  906. oi->address->u.prefix4);
  907. if (!msg)
  908. {
  909. zlog_warn ("apiserver_notify_ready_type9: msg_new failed");
  910. #ifdef NOTYET
  911. /* Cannot allocate new message. What should we do? */
  912. ospf_apiserver_free (apiserv);
  913. #endif
  914. goto out;
  915. }
  916. ospf_apiserver_send_msg (apiserv, msg);
  917. msg_free (msg);
  918. }
  919. }
  920. }
  921. out:
  922. return;
  923. }
  924. /* Notify specific client about all opaque types 10 that are ready. */
  925. void
  926. ospf_apiserver_notify_ready_type10 (struct ospf_apiserver *apiserv)
  927. {
  928. struct listnode *node, *nnode;
  929. struct listnode *node2, *nnode2;
  930. struct ospf *ospf;
  931. struct ospf_area *area;
  932. ospf = ospf_lookup ();
  933. for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
  934. {
  935. struct registered_opaque_type *r;
  936. if (!ospf_apiserver_is_ready_type10 (area))
  937. {
  938. continue;
  939. }
  940. /* Check for registered opaque type 10 types */
  941. /* XXX: loop in loop - optimise me */
  942. for (ALL_LIST_ELEMENTS (apiserv->opaque_types, node2, nnode2, r))
  943. {
  944. struct msg *msg;
  945. if (r->lsa_type == OSPF_OPAQUE_AREA_LSA)
  946. {
  947. /* Yes, this opaque type is ready */
  948. msg =
  949. new_msg_ready_notify (0, OSPF_OPAQUE_AREA_LSA,
  950. r->opaque_type, area->area_id);
  951. if (!msg)
  952. {
  953. zlog_warn ("apiserver_notify_ready_type10: msg_new failed");
  954. #ifdef NOTYET
  955. /* Cannot allocate new message. What should we do? */
  956. ospf_apiserver_free (apiserv);
  957. #endif
  958. goto out;
  959. }
  960. ospf_apiserver_send_msg (apiserv, msg);
  961. msg_free (msg);
  962. }
  963. }
  964. }
  965. out:
  966. return;
  967. }
  968. /* Notify specific client about all opaque types 11 that are ready */
  969. void
  970. ospf_apiserver_notify_ready_type11 (struct ospf_apiserver *apiserv)
  971. {
  972. struct listnode *node, *nnode;
  973. struct ospf *ospf;
  974. struct registered_opaque_type *r;
  975. ospf = ospf_lookup ();
  976. /* Can type 11 be originated? */
  977. if (!ospf_apiserver_is_ready_type11 (ospf))
  978. goto out;
  979. /* Check for registered opaque type 11 types */
  980. for (ALL_LIST_ELEMENTS (apiserv->opaque_types, node, nnode, r))
  981. {
  982. struct msg *msg;
  983. struct in_addr noarea_id = { .s_addr = 0L };
  984. if (r->lsa_type == OSPF_OPAQUE_AS_LSA)
  985. {
  986. /* Yes, this opaque type is ready */
  987. msg = new_msg_ready_notify (0, OSPF_OPAQUE_AS_LSA,
  988. r->opaque_type, noarea_id);
  989. if (!msg)
  990. {
  991. zlog_warn ("apiserver_notify_ready_type11: msg_new failed");
  992. #ifdef NOTYET
  993. /* Cannot allocate new message. What should we do? */
  994. ospf_apiserver_free (apiserv);
  995. #endif
  996. goto out;
  997. }
  998. ospf_apiserver_send_msg (apiserv, msg);
  999. msg_free (msg);
  1000. }
  1001. }
  1002. out:
  1003. return;
  1004. }
  1005. int
  1006. ospf_apiserver_handle_unregister_opaque_type (struct ospf_apiserver *apiserv,
  1007. struct msg *msg)
  1008. {
  1009. struct msg_unregister_opaque_type *umsg;
  1010. u_char ltype;
  1011. u_char otype;
  1012. int rc = 0;
  1013. /* Extract parameters from unregister opaque type message */
  1014. umsg = (struct msg_unregister_opaque_type *) STREAM_DATA (msg->s);
  1015. ltype = umsg->lsatype;
  1016. otype = umsg->opaquetype;
  1017. rc = ospf_apiserver_unregister_opaque_type (apiserv, ltype, otype);
  1018. /* Send a reply back to client including return code */
  1019. rc = ospf_apiserver_send_reply (apiserv, ntohl (msg->hdr.msgseq), rc);
  1020. return rc;
  1021. }
  1022. /* -----------------------------------------------------------
  1023. * Following are functions for event (filter) registration.
  1024. * -----------------------------------------------------------
  1025. */
  1026. int
  1027. ospf_apiserver_handle_register_event (struct ospf_apiserver *apiserv,
  1028. struct msg *msg)
  1029. {
  1030. struct msg_register_event *rmsg;
  1031. int rc;
  1032. u_int32_t seqnum;
  1033. rmsg = (struct msg_register_event *) STREAM_DATA (msg->s);
  1034. /* Get request sequence number */
  1035. seqnum = msg_get_seq (msg);
  1036. /* Free existing filter in apiserv. */
  1037. XFREE (MTYPE_OSPF_APISERVER_MSGFILTER, apiserv->filter);
  1038. /* Alloc new space for filter. */
  1039. apiserv->filter = XMALLOC (MTYPE_OSPF_APISERVER_MSGFILTER,
  1040. ntohs (msg->hdr.msglen));
  1041. if (apiserv->filter)
  1042. {
  1043. /* copy it over. */
  1044. memcpy (apiserv->filter, &rmsg->filter, ntohs (msg->hdr.msglen));
  1045. rc = OSPF_API_OK;
  1046. }
  1047. else
  1048. {
  1049. rc = OSPF_API_NOMEMORY;
  1050. }
  1051. /* Send a reply back to client with return code */
  1052. rc = ospf_apiserver_send_reply (apiserv, seqnum, rc);
  1053. return rc;
  1054. }
  1055. /* -----------------------------------------------------------
  1056. * Followings are functions for LSDB synchronization.
  1057. * -----------------------------------------------------------
  1058. */
  1059. static int
  1060. apiserver_sync_callback (struct ospf_lsa *lsa, void *p_arg, int int_arg)
  1061. {
  1062. struct ospf_apiserver *apiserv;
  1063. int seqnum;
  1064. struct msg *msg;
  1065. struct param_t
  1066. {
  1067. struct ospf_apiserver *apiserv;
  1068. struct lsa_filter_type *filter;
  1069. }
  1070. *param;
  1071. int rc = -1;
  1072. /* Sanity check */
  1073. assert (lsa->data);
  1074. assert (p_arg);
  1075. param = (struct param_t *) p_arg;
  1076. apiserv = param->apiserv;
  1077. seqnum = (u_int32_t) int_arg;
  1078. /* Check origin in filter. */
  1079. if ((param->filter->origin == ANY_ORIGIN) ||
  1080. (param->filter->origin == (lsa->flags & OSPF_LSA_SELF)))
  1081. {
  1082. /* Default area for AS-External and Opaque11 LSAs */
  1083. struct in_addr area_id = { .s_addr = 0L };
  1084. /* Default interface for non Opaque9 LSAs */
  1085. struct in_addr ifaddr = { .s_addr = 0L };
  1086. if (lsa->area)
  1087. {
  1088. area_id = lsa->area->area_id;
  1089. }
  1090. if (lsa->data->type == OSPF_OPAQUE_LINK_LSA)
  1091. {
  1092. ifaddr = lsa->oi->address->u.prefix4;
  1093. }
  1094. msg = new_msg_lsa_change_notify (MSG_LSA_UPDATE_NOTIFY,
  1095. seqnum,
  1096. ifaddr, area_id,
  1097. lsa->flags & OSPF_LSA_SELF, lsa->data);
  1098. if (!msg)
  1099. {
  1100. zlog_warn ("apiserver_sync_callback: new_msg_update failed");
  1101. #ifdef NOTYET
  1102. /* Cannot allocate new message. What should we do? */
  1103. /* ospf_apiserver_free (apiserv);*//* Do nothing here XXX */
  1104. #endif
  1105. goto out;
  1106. }
  1107. /* Send LSA */
  1108. ospf_apiserver_send_msg (apiserv, msg);
  1109. msg_free (msg);
  1110. }
  1111. rc = 0;
  1112. out:
  1113. return rc;
  1114. }
  1115. int
  1116. ospf_apiserver_handle_sync_lsdb (struct ospf_apiserver *apiserv,
  1117. struct msg *msg)
  1118. {
  1119. struct listnode *node, *nnode;
  1120. u_int32_t seqnum;
  1121. int rc = 0;
  1122. struct msg_sync_lsdb *smsg;
  1123. struct ospf_apiserver_param_t
  1124. {
  1125. struct ospf_apiserver *apiserv;
  1126. struct lsa_filter_type *filter;
  1127. } param;
  1128. u_int16_t mask;
  1129. struct route_node *rn;
  1130. struct ospf_lsa *lsa;
  1131. struct ospf *ospf;
  1132. struct ospf_area *area;
  1133. ospf = ospf_lookup ();
  1134. /* Get request sequence number */
  1135. seqnum = msg_get_seq (msg);
  1136. /* Set sync msg. */
  1137. smsg = (struct msg_sync_lsdb *) STREAM_DATA (msg->s);
  1138. /* Set parameter struct. */
  1139. param.apiserv = apiserv;
  1140. param.filter = &smsg->filter;
  1141. /* Remember mask. */
  1142. mask = ntohs (smsg->filter.typemask);
  1143. /* Iterate over all areas. */
  1144. for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
  1145. {
  1146. int i;
  1147. u_int32_t *area_id = NULL;
  1148. /* Compare area_id with area_ids in sync request. */
  1149. if ((i = smsg->filter.num_areas) > 0)
  1150. {
  1151. /* Let area_id point to the list of area IDs,
  1152. * which is at the end of smsg->filter. */
  1153. area_id = (u_int32_t *) (&smsg->filter + 1);
  1154. while (i)
  1155. {
  1156. if (*area_id == area->area_id.s_addr)
  1157. {
  1158. break;
  1159. }
  1160. i--;
  1161. area_id++;
  1162. }
  1163. }
  1164. else
  1165. {
  1166. i = 1;
  1167. }
  1168. /* If area was found, then i>0 here. */
  1169. if (i)
  1170. {
  1171. /* Check msg type. */
  1172. if (mask & Power2[OSPF_ROUTER_LSA])
  1173. LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
  1174. apiserver_sync_callback(lsa, (void *) &param, seqnum);
  1175. if (mask & Power2[OSPF_NETWORK_LSA])
  1176. LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
  1177. apiserver_sync_callback(lsa, (void *) &param, seqnum);
  1178. if (mask & Power2[OSPF_SUMMARY_LSA])
  1179. LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
  1180. apiserver_sync_callback(lsa, (void *) &param, seqnum);
  1181. if (mask & Power2[OSPF_ASBR_SUMMARY_LSA])
  1182. LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
  1183. apiserver_sync_callback(lsa, (void *) &param, seqnum);
  1184. if (mask & Power2[OSPF_OPAQUE_LINK_LSA])
  1185. LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
  1186. apiserver_sync_callback(lsa, (void *) &param, seqnum);
  1187. if (mask & Power2[OSPF_OPAQUE_AREA_LSA])
  1188. LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
  1189. apiserver_sync_callback(lsa, (void *) &param, seqnum);
  1190. }
  1191. }
  1192. /* For AS-external LSAs */
  1193. if (ospf->lsdb)
  1194. {
  1195. if (mask & Power2[OSPF_AS_EXTERNAL_LSA])
  1196. LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
  1197. apiserver_sync_callback(lsa, (void *) &param, seqnum);
  1198. }
  1199. /* For AS-external opaque LSAs */
  1200. if (ospf->lsdb)
  1201. {
  1202. if (mask & Power2[OSPF_OPAQUE_AS_LSA])
  1203. LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
  1204. apiserver_sync_callback(lsa, (void *) &param, seqnum);
  1205. }
  1206. /* Send a reply back to client with return code */
  1207. rc = ospf_apiserver_send_reply (apiserv, seqnum, rc);
  1208. return rc;
  1209. }
  1210. /* -----------------------------------------------------------
  1211. * Followings are functions to originate or update LSA
  1212. * from an application.
  1213. * -----------------------------------------------------------
  1214. */
  1215. /* Create a new internal opaque LSA by taking prototype and filling in
  1216. missing fields such as age, sequence number, advertising router,
  1217. checksum and so on. The interface parameter is used for type 9
  1218. LSAs, area parameter for type 10. Type 11 LSAs do neither need area
  1219. nor interface. */
  1220. struct ospf_lsa *
  1221. ospf_apiserver_opaque_lsa_new (struct ospf_area *area,
  1222. struct ospf_interface *oi,
  1223. struct lsa_header *protolsa)
  1224. {
  1225. struct stream *s;
  1226. struct lsa_header *newlsa;
  1227. struct ospf_lsa *new = NULL;
  1228. u_char options = 0x0;
  1229. u_int16_t length;
  1230. struct ospf *ospf;
  1231. ospf = ospf_lookup();
  1232. assert(ospf);
  1233. /* Create a stream for internal opaque LSA */
  1234. if ((s = stream_new (OSPF_MAX_LSA_SIZE)) == NULL)
  1235. {
  1236. zlog_warn ("ospf_apiserver_opaque_lsa_new: stream_new failed");
  1237. return NULL;
  1238. }
  1239. newlsa = (struct lsa_header *) STREAM_DATA (s);
  1240. /* XXX If this is a link-local LSA or an AS-external LSA, how do we
  1241. have to set options? */
  1242. if (area)
  1243. {
  1244. options = LSA_OPTIONS_GET (area);
  1245. options |= LSA_OPTIONS_NSSA_GET (area);
  1246. }
  1247. options |= OSPF_OPTION_O; /* Don't forget to set option bit */
  1248. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1249. {
  1250. zlog_debug ("LSA[Type%d:%s]: Creating an Opaque-LSA instance",
  1251. protolsa->type, inet_ntoa (protolsa->id));
  1252. }
  1253. /* Set opaque-LSA header fields. */
  1254. lsa_header_set (s, options, protolsa->type, protolsa->id,
  1255. ospf->router_id);
  1256. /* Set opaque-LSA body fields. */
  1257. stream_put (s, ((u_char *) protolsa) + sizeof (struct lsa_header),
  1258. ntohs (protolsa->length) - sizeof (struct lsa_header));
  1259. /* Determine length of LSA. */
  1260. length = stream_get_endp (s);
  1261. newlsa->length = htons (length);
  1262. /* Create OSPF LSA. */
  1263. if ((new = ospf_lsa_new ()) == NULL)
  1264. {
  1265. zlog_warn ("ospf_apiserver_opaque_lsa_new: ospf_lsa_new() ?");
  1266. stream_free (s);
  1267. return NULL;
  1268. }
  1269. if ((new->data = ospf_lsa_data_new (length)) == NULL)
  1270. {
  1271. zlog_warn ("ospf_apiserver_opaque_lsa_new: ospf_lsa_data_new() ?");
  1272. ospf_lsa_unlock (&new);
  1273. stream_free (s);
  1274. return NULL;
  1275. }
  1276. new->area = area;
  1277. new->oi = oi;
  1278. SET_FLAG (new->flags, OSPF_LSA_SELF);
  1279. memcpy (new->data, newlsa, length);
  1280. stream_free (s);
  1281. return new;
  1282. }
  1283. int
  1284. ospf_apiserver_is_ready_type9 (struct ospf_interface *oi)
  1285. {
  1286. /* Type 9 opaque LSA can be originated if there is at least one
  1287. active opaque-capable neighbor attached to the outgoing
  1288. interface. */
  1289. return (ospf_nbr_count_opaque_capable (oi) > 0);
  1290. }
  1291. int
  1292. ospf_apiserver_is_ready_type10 (struct ospf_area *area)
  1293. {
  1294. /* Type 10 opaque LSA can be originated if there is at least one
  1295. interface belonging to the area that has an active opaque-capable
  1296. neighbor. */
  1297. struct listnode *node, *nnode;
  1298. struct ospf_interface *oi;
  1299. for (ALL_LIST_ELEMENTS (area->oiflist, node, nnode, oi))
  1300. /* Is there an active neighbor attached to this interface? */
  1301. if (ospf_apiserver_is_ready_type9 (oi))
  1302. return 1;
  1303. /* No active neighbor in area */
  1304. return 0;
  1305. }
  1306. int
  1307. ospf_apiserver_is_ready_type11 (struct ospf *ospf)
  1308. {
  1309. /* Type 11 opaque LSA can be originated if there is at least one interface
  1310. that has an active opaque-capable neighbor. */
  1311. struct listnode *node, *nnode;
  1312. struct ospf_interface *oi;
  1313. for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
  1314. /* Is there an active neighbor attached to this interface? */
  1315. if (ospf_apiserver_is_ready_type9 (oi))
  1316. return 1;
  1317. /* No active neighbor at all */
  1318. return 0;
  1319. }
  1320. int
  1321. ospf_apiserver_handle_originate_request (struct ospf_apiserver *apiserv,
  1322. struct msg *msg)
  1323. {
  1324. struct msg_originate_request *omsg;
  1325. struct lsa_header *data;
  1326. struct ospf_lsa *new;
  1327. struct ospf_lsa *old;
  1328. struct ospf_area *area = NULL;
  1329. struct ospf_interface *oi = NULL;
  1330. struct ospf_lsdb *lsdb = NULL;
  1331. struct ospf *ospf;
  1332. int lsa_type, opaque_type;
  1333. int ready = 0;
  1334. int rc = 0;
  1335. ospf = ospf_lookup();
  1336. /* Extract opaque LSA data from message */
  1337. omsg = (struct msg_originate_request *) STREAM_DATA (msg->s);
  1338. data = &omsg->data;
  1339. /* Determine interface for type9 or area for type10 LSAs. */
  1340. switch (data->type)
  1341. {
  1342. case OSPF_OPAQUE_LINK_LSA:
  1343. oi = ospf_apiserver_if_lookup_by_addr (omsg->ifaddr);
  1344. if (!oi)
  1345. {
  1346. zlog_warn ("apiserver_originate: unknown interface %s",
  1347. inet_ntoa (omsg->ifaddr));
  1348. rc = OSPF_API_NOSUCHINTERFACE;
  1349. goto out;
  1350. }
  1351. area = oi->area;
  1352. lsdb = area->lsdb;
  1353. break;
  1354. case OSPF_OPAQUE_AREA_LSA:
  1355. area = ospf_area_lookup_by_area_id (ospf, omsg->area_id);
  1356. if (!area)
  1357. {
  1358. zlog_warn ("apiserver_originate: unknown area %s",
  1359. inet_ntoa (omsg->area_id));
  1360. rc = OSPF_API_NOSUCHAREA;
  1361. goto out;
  1362. }
  1363. lsdb = area->lsdb;
  1364. break;
  1365. case OSPF_OPAQUE_AS_LSA:
  1366. lsdb = ospf->lsdb;
  1367. break;
  1368. default:
  1369. /* We can only handle opaque types here */
  1370. zlog_warn ("apiserver_originate: Cannot originate non-opaque LSA type %d",
  1371. data->type);
  1372. rc = OSPF_API_ILLEGALLSATYPE;
  1373. goto out;
  1374. }
  1375. /* Check if we registered this opaque type */
  1376. lsa_type = data->type;
  1377. opaque_type = GET_OPAQUE_TYPE (ntohl (data->id.s_addr));
  1378. if (!apiserver_is_opaque_type_registered (apiserv, lsa_type, opaque_type))
  1379. {
  1380. zlog_warn ("apiserver_originate: LSA-type(%d)/Opaque-type(%d): Not registered", lsa_type, opaque_type);
  1381. rc = OSPF_API_OPAQUETYPENOTREGISTERED;
  1382. goto out;
  1383. }
  1384. /* Make sure that the neighbors are ready before we can originate */
  1385. switch (data->type)
  1386. {
  1387. case OSPF_OPAQUE_LINK_LSA:
  1388. ready = ospf_apiserver_is_ready_type9 (oi);
  1389. break;
  1390. case OSPF_OPAQUE_AREA_LSA:
  1391. ready = ospf_apiserver_is_ready_type10 (area);
  1392. break;
  1393. case OSPF_OPAQUE_AS_LSA:
  1394. ready = ospf_apiserver_is_ready_type11 (ospf);
  1395. break;
  1396. default:
  1397. break;
  1398. }
  1399. if (!ready)
  1400. {
  1401. zlog_warn ("Neighbors not ready to originate type %d", data->type);
  1402. rc = OSPF_API_NOTREADY;
  1403. goto out;
  1404. }
  1405. /* Create OSPF's internal opaque LSA representation */
  1406. new = ospf_apiserver_opaque_lsa_new (area, oi, data);
  1407. if (!new)
  1408. {
  1409. rc = OSPF_API_NOMEMORY; /* XXX */
  1410. goto out;
  1411. }
  1412. /* Determine if LSA is new or an update for an existing one. */
  1413. old = ospf_lsdb_lookup (lsdb, new);
  1414. if (!old)
  1415. {
  1416. /* New LSA install in LSDB. */
  1417. rc = ospf_apiserver_originate1 (new);
  1418. }
  1419. else
  1420. {
  1421. /*
  1422. * Keep the new LSA instance in the "waiting place" until the next
  1423. * refresh timing. If several LSA update requests for the same LSID
  1424. * have issued by peer, the last one takes effect.
  1425. */
  1426. new->lsdb = &apiserv->reserve;
  1427. ospf_lsdb_add (&apiserv->reserve, new);
  1428. /* Kick the scheduler function. */
  1429. ospf_opaque_lsa_refresh_schedule (old);
  1430. }
  1431. out:
  1432. /* Send a reply back to client with return code */
  1433. rc = ospf_apiserver_send_reply (apiserv, ntohl (msg->hdr.msgseq), rc);
  1434. return rc;
  1435. }
  1436. /* -----------------------------------------------------------
  1437. * Flood an LSA within its flooding scope.
  1438. * -----------------------------------------------------------
  1439. */
  1440. /* XXX We can probably use ospf_flood_through instead of this function
  1441. but then we need the neighbor parameter. If we set nbr to
  1442. NULL then ospf_flood_through crashes due to dereferencing NULL. */
  1443. void
  1444. ospf_apiserver_flood_opaque_lsa (struct ospf_lsa *lsa)
  1445. {
  1446. assert (lsa);
  1447. switch (lsa->data->type)
  1448. {
  1449. case OSPF_OPAQUE_LINK_LSA:
  1450. /* Increment counters? XXX */
  1451. /* Flood LSA through local network. */
  1452. ospf_flood_through_area (lsa->area, NULL /*nbr */ , lsa);
  1453. break;
  1454. case OSPF_OPAQUE_AREA_LSA:
  1455. /* Update LSA origination count. */
  1456. assert (lsa->area);
  1457. lsa->area->ospf->lsa_originate_count++;
  1458. /* Flood LSA through area. */
  1459. ospf_flood_through_area (lsa->area, NULL /*nbr */ , lsa);
  1460. break;
  1461. case OSPF_OPAQUE_AS_LSA:
  1462. {
  1463. struct ospf *ospf;
  1464. ospf = ospf_lookup();
  1465. assert(ospf);
  1466. /* Increment counters? XXX */
  1467. /* Flood LSA through AS. */
  1468. ospf_flood_through_as (ospf, NULL /*nbr */ , lsa);
  1469. break;
  1470. }
  1471. }
  1472. }
  1473. int
  1474. ospf_apiserver_originate1 (struct ospf_lsa *lsa)
  1475. {
  1476. struct ospf *ospf;
  1477. ospf = ospf_lookup();
  1478. assert(ospf);
  1479. /* Install this LSA into LSDB. */
  1480. if (ospf_lsa_install (ospf, lsa->oi, lsa) == NULL)
  1481. {
  1482. zlog_warn ("ospf_apiserver_originate1: ospf_lsa_install failed");
  1483. return -1;
  1484. }
  1485. /* Flood LSA within scope */
  1486. #ifdef NOTYET
  1487. /*
  1488. * NB: Modified version of "ospf_flood_though ()" accepts NULL "inbr"
  1489. * parameter, and thus it does not cause SIGSEGV error.
  1490. */
  1491. ospf_flood_through (NULL /*nbr */ , lsa);
  1492. #else /* NOTYET */
  1493. ospf_apiserver_flood_opaque_lsa (lsa);
  1494. #endif /* NOTYET */
  1495. return 0;
  1496. }
  1497. /* Opaque LSAs of type 9 on a specific interface can now be
  1498. originated. Tell clients that registered type 9. */
  1499. int
  1500. ospf_apiserver_lsa9_originator (void *arg)
  1501. {
  1502. struct ospf_interface *oi;
  1503. oi = (struct ospf_interface *) arg;
  1504. if (listcount (apiserver_list) > 0) {
  1505. ospf_apiserver_clients_notify_ready_type9 (oi);
  1506. }
  1507. return 0;
  1508. }
  1509. int
  1510. ospf_apiserver_lsa10_originator (void *arg)
  1511. {
  1512. struct ospf_area *area;
  1513. area = (struct ospf_area *) arg;
  1514. if (listcount (apiserver_list) > 0) {
  1515. ospf_apiserver_clients_notify_ready_type10 (area);
  1516. }
  1517. return 0;
  1518. }
  1519. int
  1520. ospf_apiserver_lsa11_originator (void *arg)
  1521. {
  1522. struct ospf *ospf;
  1523. ospf = (struct ospf *) arg;
  1524. if (listcount (apiserver_list) > 0) {
  1525. ospf_apiserver_clients_notify_ready_type11 (ospf);
  1526. }
  1527. return 0;
  1528. }
  1529. /* Periodically refresh opaque LSAs so that they do not expire in
  1530. other routers. */
  1531. struct ospf_lsa *
  1532. ospf_apiserver_lsa_refresher (struct ospf_lsa *lsa)
  1533. {
  1534. struct ospf_apiserver *apiserv;
  1535. struct ospf_lsa *new = NULL;
  1536. struct ospf * ospf;
  1537. ospf = ospf_lookup();
  1538. assert(ospf);
  1539. apiserv = lookup_apiserver_by_lsa (lsa);
  1540. if (!apiserv)
  1541. {
  1542. zlog_warn ("ospf_apiserver_lsa_refresher: LSA[%s]: No apiserver?", dump_lsa_key (lsa));
  1543. lsa->data->ls_age = htons (OSPF_LSA_MAXAGE); /* Flush it anyway. */
  1544. }
  1545. if (IS_LSA_MAXAGE (lsa))
  1546. {
  1547. ospf_opaque_lsa_flush_schedule (lsa);
  1548. goto out;
  1549. }
  1550. /* Check if updated version of LSA instance has already prepared. */
  1551. new = ospf_lsdb_lookup (&apiserv->reserve, lsa);
  1552. if (!new)
  1553. {
  1554. /* This is a periodic refresh, driven by core OSPF mechanism. */
  1555. new = ospf_apiserver_opaque_lsa_new (lsa->area, lsa->oi, lsa->data);
  1556. if (!new)
  1557. {
  1558. zlog_warn ("ospf_apiserver_lsa_refresher: Cannot create a new LSA?");
  1559. goto out;
  1560. }
  1561. }
  1562. else
  1563. {
  1564. /* This is a forcible refresh, requested by OSPF-API client. */
  1565. ospf_lsdb_delete (&apiserv->reserve, new);
  1566. new->lsdb = NULL;
  1567. }
  1568. /* Increment sequence number */
  1569. new->data->ls_seqnum = lsa_seqnum_increment (lsa);
  1570. /* New LSA is in same area. */
  1571. new->area = lsa->area;
  1572. SET_FLAG (new->flags, OSPF_LSA_SELF);
  1573. /* Install LSA into LSDB. */
  1574. if (ospf_lsa_install (ospf, new->oi, new) == NULL)
  1575. {
  1576. zlog_warn ("ospf_apiserver_lsa_refresher: ospf_lsa_install failed");
  1577. ospf_lsa_unlock (&new);
  1578. goto out;
  1579. }
  1580. /* Flood updated LSA through interface, area or AS */
  1581. #ifdef NOTYET
  1582. ospf_flood_through (NULL /*nbr */ , new);
  1583. #endif /* NOTYET */
  1584. ospf_apiserver_flood_opaque_lsa (new);
  1585. /* Debug logging. */
  1586. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1587. {
  1588. zlog_debug ("LSA[Type%d:%s]: Refresh Opaque LSA",
  1589. new->data->type, inet_ntoa (new->data->id));
  1590. ospf_lsa_header_dump (new->data);
  1591. }
  1592. out:
  1593. return new;
  1594. }
  1595. /* -----------------------------------------------------------
  1596. * Followings are functions to delete LSAs
  1597. * -----------------------------------------------------------
  1598. */
  1599. int
  1600. ospf_apiserver_handle_delete_request (struct ospf_apiserver *apiserv,
  1601. struct msg *msg)
  1602. {
  1603. struct msg_delete_request *dmsg;
  1604. struct ospf_lsa *old;
  1605. struct ospf_area *area = NULL;
  1606. struct in_addr id;
  1607. int lsa_type, opaque_type;
  1608. int rc = 0;
  1609. struct ospf * ospf;
  1610. ospf = ospf_lookup();
  1611. assert(ospf);
  1612. /* Extract opaque LSA from message */
  1613. dmsg = (struct msg_delete_request *) STREAM_DATA (msg->s);
  1614. /* Lookup area for link-local and area-local opaque LSAs */
  1615. switch (dmsg->lsa_type)
  1616. {
  1617. case OSPF_OPAQUE_LINK_LSA:
  1618. case OSPF_OPAQUE_AREA_LSA:
  1619. area = ospf_area_lookup_by_area_id (ospf, dmsg->area_id);
  1620. if (!area)
  1621. {
  1622. zlog_warn ("ospf_apiserver_lsa_delete: unknown area %s",
  1623. inet_ntoa (dmsg->area_id));
  1624. rc = OSPF_API_NOSUCHAREA;
  1625. goto out;
  1626. }
  1627. break;
  1628. case OSPF_OPAQUE_AS_LSA:
  1629. /* AS-external opaque LSAs have no designated area */
  1630. area = NULL;
  1631. break;
  1632. default:
  1633. zlog_warn
  1634. ("ospf_apiserver_lsa_delete: Cannot delete non-opaque LSA type %d",
  1635. dmsg->lsa_type);
  1636. rc = OSPF_API_ILLEGALLSATYPE;
  1637. goto out;
  1638. }
  1639. /* Check if we registered this opaque type */
  1640. lsa_type = dmsg->lsa_type;
  1641. opaque_type = dmsg->opaque_type;
  1642. if (!apiserver_is_opaque_type_registered (apiserv, lsa_type, opaque_type))
  1643. {
  1644. zlog_warn ("ospf_apiserver_lsa_delete: LSA-type(%d)/Opaque-type(%d): Not registered", lsa_type, opaque_type);
  1645. rc = OSPF_API_OPAQUETYPENOTREGISTERED;
  1646. goto out;
  1647. }
  1648. /* opaque_id is in network byte order */
  1649. id.s_addr = htonl (SET_OPAQUE_LSID (dmsg->opaque_type,
  1650. ntohl (dmsg->opaque_id)));
  1651. /*
  1652. * Even if the target LSA has once scheduled to flush, it remains in
  1653. * the LSDB until it is finally handled by the maxage remover thread.
  1654. * Therefore, the lookup function below may return non-NULL result.
  1655. */
  1656. old = ospf_lsa_lookup (area, dmsg->lsa_type, id, ospf->router_id);
  1657. if (!old)
  1658. {
  1659. zlog_warn ("ospf_apiserver_lsa_delete: LSA[Type%d:%s] not in LSDB",
  1660. dmsg->lsa_type, inet_ntoa (id));
  1661. rc = OSPF_API_NOSUCHLSA;
  1662. goto out;
  1663. }
  1664. /* Schedule flushing of LSA from LSDB */
  1665. /* NB: Multiple scheduling will produce a warning message, but harmless. */
  1666. ospf_opaque_lsa_flush_schedule (old);
  1667. out:
  1668. /* Send reply back to client including return code */
  1669. rc = ospf_apiserver_send_reply (apiserv, ntohl (msg->hdr.msgseq), rc);
  1670. return rc;
  1671. }
  1672. /* Flush self-originated opaque LSA */
  1673. static int
  1674. apiserver_flush_opaque_type_callback (struct ospf_lsa *lsa,
  1675. void *p_arg, int int_arg)
  1676. {
  1677. struct param_t
  1678. {
  1679. struct ospf_apiserver *apiserv;
  1680. u_char lsa_type;
  1681. u_char opaque_type;
  1682. }
  1683. *param;
  1684. /* Sanity check */
  1685. assert (lsa->data);
  1686. assert (p_arg);
  1687. param = (struct param_t *) p_arg;
  1688. /* If LSA matches type and opaque type then delete it */
  1689. if (IS_LSA_SELF (lsa) && lsa->data->type == param->lsa_type
  1690. && GET_OPAQUE_TYPE (ntohl (lsa->data->id.s_addr)) == param->opaque_type)
  1691. {
  1692. ospf_opaque_lsa_flush_schedule (lsa);
  1693. }
  1694. return 0;
  1695. }
  1696. /* Delete self-originated opaque LSAs of a given opaque type. This
  1697. function is called when an application unregisters a given opaque
  1698. type or a connection to an application closes and all those opaque
  1699. LSAs need to be flushed the LSDB. */
  1700. void
  1701. ospf_apiserver_flush_opaque_lsa (struct ospf_apiserver *apiserv,
  1702. u_char lsa_type, u_char opaque_type)
  1703. {
  1704. struct param_t
  1705. {
  1706. struct ospf_apiserver *apiserv;
  1707. u_char lsa_type;
  1708. u_char opaque_type;
  1709. } param;
  1710. struct listnode *node, *nnode;
  1711. struct ospf * ospf;
  1712. struct ospf_area *area;
  1713. ospf = ospf_lookup();
  1714. assert(ospf);
  1715. /* Set parameter struct. */
  1716. param.apiserv = apiserv;
  1717. param.lsa_type = lsa_type;
  1718. param.opaque_type = opaque_type;
  1719. switch (lsa_type)
  1720. {
  1721. struct route_node *rn;
  1722. struct ospf_lsa *lsa;
  1723. case OSPF_OPAQUE_LINK_LSA:
  1724. for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
  1725. LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
  1726. apiserver_flush_opaque_type_callback(lsa, (void *) &param, 0);
  1727. break;
  1728. case OSPF_OPAQUE_AREA_LSA:
  1729. for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
  1730. LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
  1731. apiserver_flush_opaque_type_callback(lsa, (void *) &param, 0);
  1732. break;
  1733. case OSPF_OPAQUE_AS_LSA:
  1734. LSDB_LOOP (OPAQUE_LINK_LSDB (ospf), rn, lsa)
  1735. apiserver_flush_opaque_type_callback(lsa, (void *) &param, 0);
  1736. break;
  1737. default:
  1738. break;
  1739. }
  1740. return;
  1741. }
  1742. /* -----------------------------------------------------------
  1743. * Followings are callback functions to handle opaque types
  1744. * -----------------------------------------------------------
  1745. */
  1746. int
  1747. ospf_apiserver_new_if (struct interface *ifp)
  1748. {
  1749. struct ospf_interface *oi;
  1750. /* For some strange reason it seems possible that we are invoked
  1751. with an interface that has no name. This seems to happen during
  1752. initialization. Return if this happens */
  1753. if (ifp->name[0] == '\0') {
  1754. /* interface has empty name */
  1755. zlog_warn ("ospf_apiserver_new_if: interface has no name?");
  1756. return 0;
  1757. }
  1758. /* zlog_warn for debugging */
  1759. zlog_warn ("ospf_apiserver_new_if");
  1760. zlog_warn ("ifp name=%s status=%d index=%d", ifp->name, ifp->status,
  1761. ifp->ifindex);
  1762. if (ifp->name[0] == '\0') {
  1763. /* interface has empty name */
  1764. zlog_warn ("ospf_apiserver_new_if: interface has no name?");
  1765. return 0;
  1766. }
  1767. oi = ospf_apiserver_if_lookup_by_ifp (ifp);
  1768. if (!oi) {
  1769. /* This interface is known to Zebra but not to OSPF daemon yet. */
  1770. zlog_warn ("ospf_apiserver_new_if: interface %s not known to OSPFd?",
  1771. ifp->name);
  1772. return 0;
  1773. }
  1774. assert (oi);
  1775. /* New interface added to OSPF, tell clients about it */
  1776. if (listcount (apiserver_list) > 0) {
  1777. ospf_apiserver_clients_notify_new_if (oi);
  1778. }
  1779. return 0;
  1780. }
  1781. int
  1782. ospf_apiserver_del_if (struct interface *ifp)
  1783. {
  1784. struct ospf_interface *oi;
  1785. /* zlog_warn for debugging */
  1786. zlog_warn ("ospf_apiserver_del_if");
  1787. zlog_warn ("ifp name=%s status=%d index=%d\n", ifp->name, ifp->status,
  1788. ifp->ifindex);
  1789. oi = ospf_apiserver_if_lookup_by_ifp (ifp);
  1790. if (!oi) {
  1791. /* This interface is known to Zebra but not to OSPF daemon
  1792. anymore. No need to tell clients about it */
  1793. return 0;
  1794. }
  1795. /* Interface deleted, tell clients about it */
  1796. if (listcount (apiserver_list) > 0) {
  1797. ospf_apiserver_clients_notify_del_if (oi);
  1798. }
  1799. return 0;
  1800. }
  1801. void
  1802. ospf_apiserver_ism_change (struct ospf_interface *oi, int old_state)
  1803. {
  1804. /* Tell clients about interface change */
  1805. /* zlog_warn for debugging */
  1806. zlog_warn ("ospf_apiserver_ism_change");
  1807. if (listcount (apiserver_list) > 0) {
  1808. ospf_apiserver_clients_notify_ism_change (oi);
  1809. }
  1810. zlog_warn ("oi->ifp->name=%s", oi->ifp->name);
  1811. zlog_warn ("old_state=%d", old_state);
  1812. zlog_warn ("oi->state=%d", oi->state);
  1813. }
  1814. void
  1815. ospf_apiserver_nsm_change (struct ospf_neighbor *nbr, int old_status)
  1816. {
  1817. /* Neighbor status changed, tell clients about it */
  1818. zlog_warn ("ospf_apiserver_nsm_change");
  1819. if (listcount (apiserver_list) > 0) {
  1820. ospf_apiserver_clients_notify_nsm_change (nbr);
  1821. }
  1822. }
  1823. void
  1824. ospf_apiserver_show_info (struct vty *vty, struct ospf_lsa *lsa)
  1825. {
  1826. struct opaque_lsa
  1827. {
  1828. struct lsa_header header;
  1829. u_char data[1]; /* opaque data have variable length. This is start
  1830. address */
  1831. };
  1832. struct opaque_lsa *olsa;
  1833. int opaquelen;
  1834. olsa = (struct opaque_lsa *) lsa->data;
  1835. if (VALID_OPAQUE_INFO_LEN (lsa->data))
  1836. opaquelen = ntohs (lsa->data->length) - OSPF_LSA_HEADER_SIZE;
  1837. else
  1838. opaquelen = 0;
  1839. /* Output information about opaque LSAs */
  1840. if (vty != NULL)
  1841. {
  1842. int i;
  1843. vty_out (vty, " Added using OSPF API: %u octets of opaque data %s%s",
  1844. opaquelen,
  1845. VALID_OPAQUE_INFO_LEN (lsa->data) ? "" : "(Invalid length?)",
  1846. VTY_NEWLINE);
  1847. vty_out (vty, " Opaque data: ");
  1848. for (i = 0; i < opaquelen; i++)
  1849. {
  1850. vty_out (vty, "0x%x ", olsa->data[i]);
  1851. }
  1852. vty_out (vty, "%s", VTY_NEWLINE);
  1853. }
  1854. else
  1855. {
  1856. int i;
  1857. zlog_debug (" Added using OSPF API: %u octets of opaque data %s",
  1858. opaquelen,
  1859. VALID_OPAQUE_INFO_LEN (lsa->
  1860. data) ? "" : "(Invalid length?)");
  1861. zlog_debug (" Opaque data: ");
  1862. for (i = 0; i < opaquelen; i++)
  1863. {
  1864. zlog_debug ("0x%x ", olsa->data[i]);
  1865. }
  1866. zlog_debug ("\n");
  1867. }
  1868. return;
  1869. }
  1870. /* -----------------------------------------------------------
  1871. * Followings are functions to notify clients about events
  1872. * -----------------------------------------------------------
  1873. */
  1874. /* Send a message to all clients. This is useful for messages
  1875. that need to be notified to all clients (such as interface
  1876. changes) */
  1877. void
  1878. ospf_apiserver_clients_notify_all (struct msg *msg)
  1879. {
  1880. struct listnode *node, *nnode;
  1881. struct ospf_apiserver *apiserv;
  1882. /* Send message to all clients */
  1883. for (ALL_LIST_ELEMENTS (apiserver_list, node, nnode, apiserv))
  1884. ospf_apiserver_send_msg (apiserv, msg);
  1885. }
  1886. /* An interface is now ready to accept opaque LSAs. Notify all
  1887. clients that registered to use this opaque type */
  1888. void
  1889. ospf_apiserver_clients_notify_ready_type9 (struct ospf_interface *oi)
  1890. {
  1891. struct listnode *node, *nnode;
  1892. struct msg *msg;
  1893. struct ospf_apiserver *apiserv;
  1894. assert (oi);
  1895. if (!oi->address)
  1896. {
  1897. zlog_warn ("Interface has no address?");
  1898. return;
  1899. }
  1900. if (!ospf_apiserver_is_ready_type9 (oi))
  1901. {
  1902. zlog_warn ("Interface not ready for type 9?");
  1903. return;
  1904. }
  1905. for (ALL_LIST_ELEMENTS (apiserver_list, node, nnode, apiserv))
  1906. {
  1907. struct listnode *node2, *nnode2;
  1908. struct registered_opaque_type *r;
  1909. for (ALL_LIST_ELEMENTS (apiserv->opaque_types, node2, nnode2, r))
  1910. {
  1911. if (r->lsa_type == OSPF_OPAQUE_LINK_LSA)
  1912. {
  1913. msg = new_msg_ready_notify (0, OSPF_OPAQUE_LINK_LSA,
  1914. r->opaque_type,
  1915. oi->address->u.prefix4);
  1916. if (!msg)
  1917. {
  1918. zlog_warn
  1919. ("ospf_apiserver_clients_notify_ready_type9: new_msg_ready_notify failed");
  1920. #ifdef NOTYET
  1921. /* Cannot allocate new message. What should we do? */
  1922. ospf_apiserver_free (apiserv);
  1923. #endif
  1924. goto out;
  1925. }
  1926. ospf_apiserver_send_msg (apiserv, msg);
  1927. msg_free (msg);
  1928. }
  1929. }
  1930. }
  1931. out:
  1932. return;
  1933. }
  1934. void
  1935. ospf_apiserver_clients_notify_ready_type10 (struct ospf_area *area)
  1936. {
  1937. struct listnode *node, *nnode;
  1938. struct msg *msg;
  1939. struct ospf_apiserver *apiserv;
  1940. assert (area);
  1941. if (!ospf_apiserver_is_ready_type10 (area))
  1942. {
  1943. zlog_warn ("Area not ready for type 10?");
  1944. return;
  1945. }
  1946. for (ALL_LIST_ELEMENTS (apiserver_list, node, nnode, apiserv))
  1947. {
  1948. struct listnode *node2, *nnode2;
  1949. struct registered_opaque_type *r;
  1950. for (ALL_LIST_ELEMENTS (apiserv->opaque_types, node2, nnode2, r))
  1951. {
  1952. if (r->lsa_type == OSPF_OPAQUE_AREA_LSA)
  1953. {
  1954. msg = new_msg_ready_notify (0, OSPF_OPAQUE_AREA_LSA,
  1955. r->opaque_type, area->area_id);
  1956. if (!msg)
  1957. {
  1958. zlog_warn
  1959. ("ospf_apiserver_clients_notify_ready_type10: new_msg_ready_nofity failed");
  1960. #ifdef NOTYET
  1961. /* Cannot allocate new message. What should we do? */
  1962. ospf_apiserver_free (apiserv);
  1963. #endif
  1964. goto out;
  1965. }
  1966. ospf_apiserver_send_msg (apiserv, msg);
  1967. msg_free (msg);
  1968. }
  1969. }
  1970. }
  1971. out:
  1972. return;
  1973. }
  1974. void
  1975. ospf_apiserver_clients_notify_ready_type11 (struct ospf *top)
  1976. {
  1977. struct listnode *node, *nnode;
  1978. struct msg *msg;
  1979. struct in_addr id_null = { .s_addr = 0L };
  1980. struct ospf_apiserver *apiserv;
  1981. assert (top);
  1982. if (!ospf_apiserver_is_ready_type11 (top))
  1983. {
  1984. zlog_warn ("AS not ready for type 11?");
  1985. return;
  1986. }
  1987. for (ALL_LIST_ELEMENTS (apiserver_list, node, nnode, apiserv))
  1988. {
  1989. struct listnode *node2, *nnode2;
  1990. struct registered_opaque_type *r;
  1991. for (ALL_LIST_ELEMENTS (apiserv->opaque_types, node2, nnode2, r))
  1992. {
  1993. if (r->lsa_type == OSPF_OPAQUE_AS_LSA)
  1994. {
  1995. msg = new_msg_ready_notify (0, OSPF_OPAQUE_AS_LSA,
  1996. r->opaque_type, id_null);
  1997. if (!msg)
  1998. {
  1999. zlog_warn
  2000. ("ospf_apiserver_clients_notify_ready_type11: new_msg_ready_notify failed");
  2001. #ifdef NOTYET
  2002. /* Cannot allocate new message. What should we do? */
  2003. ospf_apiserver_free (apiserv);
  2004. #endif
  2005. goto out;
  2006. }
  2007. ospf_apiserver_send_msg (apiserv, msg);
  2008. msg_free (msg);
  2009. }
  2010. }
  2011. }
  2012. out:
  2013. return;
  2014. }
  2015. void
  2016. ospf_apiserver_clients_notify_new_if (struct ospf_interface *oi)
  2017. {
  2018. struct msg *msg;
  2019. msg = new_msg_new_if (0, oi->address->u.prefix4, oi->area->area_id);
  2020. if (msg != NULL)
  2021. {
  2022. ospf_apiserver_clients_notify_all (msg);
  2023. msg_free (msg);
  2024. }
  2025. }
  2026. void
  2027. ospf_apiserver_clients_notify_del_if (struct ospf_interface *oi)
  2028. {
  2029. struct msg *msg;
  2030. msg = new_msg_del_if (0, oi->address->u.prefix4);
  2031. if (msg != NULL)
  2032. {
  2033. ospf_apiserver_clients_notify_all (msg);
  2034. msg_free (msg);
  2035. }
  2036. }
  2037. void
  2038. ospf_apiserver_clients_notify_ism_change (struct ospf_interface *oi)
  2039. {
  2040. struct msg *msg;
  2041. struct in_addr ifaddr = { .s_addr = 0L };
  2042. struct in_addr area_id = { .s_addr = 0L };
  2043. assert (oi);
  2044. assert (oi->ifp);
  2045. if (oi->address)
  2046. {
  2047. ifaddr = oi->address->u.prefix4;
  2048. }
  2049. if (oi->area)
  2050. {
  2051. area_id = oi->area->area_id;
  2052. }
  2053. msg = new_msg_ism_change (0, ifaddr, area_id, oi->state);
  2054. if (!msg)
  2055. {
  2056. zlog_warn ("apiserver_clients_notify_ism_change: msg_new failed");
  2057. return;
  2058. }
  2059. ospf_apiserver_clients_notify_all (msg);
  2060. msg_free (msg);
  2061. }
  2062. void
  2063. ospf_apiserver_clients_notify_nsm_change (struct ospf_neighbor *nbr)
  2064. {
  2065. struct msg *msg;
  2066. struct in_addr ifaddr = { .s_addr = 0L };
  2067. struct in_addr nbraddr = { .s_addr = 0L };
  2068. assert (nbr);
  2069. if (nbr->oi)
  2070. {
  2071. ifaddr = nbr->oi->address->u.prefix4;
  2072. }
  2073. nbraddr = nbr->address.u.prefix4;
  2074. msg = new_msg_nsm_change (0, ifaddr, nbraddr, nbr->router_id, nbr->state);
  2075. if (!msg)
  2076. {
  2077. zlog_warn ("apiserver_clients_notify_nsm_change: msg_new failed");
  2078. return;
  2079. }
  2080. ospf_apiserver_clients_notify_all (msg);
  2081. msg_free (msg);
  2082. }
  2083. static void
  2084. apiserver_clients_lsa_change_notify (u_char msgtype, struct ospf_lsa *lsa)
  2085. {
  2086. struct msg *msg;
  2087. struct listnode *node, *nnode;
  2088. struct ospf_apiserver *apiserv;
  2089. /* Default area for AS-External and Opaque11 LSAs */
  2090. struct in_addr area_id = { .s_addr = 0L };
  2091. /* Default interface for non Opaque9 LSAs */
  2092. struct in_addr ifaddr = { .s_addr = 0L };
  2093. if (lsa->area)
  2094. {
  2095. area_id = lsa->area->area_id;
  2096. }
  2097. if (lsa->data->type == OSPF_OPAQUE_LINK_LSA)
  2098. {
  2099. assert (lsa->oi);
  2100. ifaddr = lsa->oi->address->u.prefix4;
  2101. }
  2102. /* Prepare message that can be sent to clients that have a matching
  2103. filter */
  2104. msg = new_msg_lsa_change_notify (msgtype, 0L, /* no sequence number */
  2105. ifaddr, area_id,
  2106. lsa->flags & OSPF_LSA_SELF, lsa->data);
  2107. if (!msg)
  2108. {
  2109. zlog_warn ("apiserver_clients_lsa_change_notify: msg_new failed");
  2110. return;
  2111. }
  2112. /* Now send message to all clients with a matching filter */
  2113. for (ALL_LIST_ELEMENTS (apiserver_list, node, nnode, apiserv))
  2114. {
  2115. struct lsa_filter_type *filter;
  2116. u_int16_t mask;
  2117. u_int32_t *area;
  2118. int i;
  2119. /* Check filter for this client. */
  2120. filter = apiserv->filter;
  2121. /* Check area IDs in case of non AS-E LSAs.
  2122. * If filter has areas (num_areas > 0),
  2123. * then one of the areas must match the area ID of this LSA. */
  2124. i = filter->num_areas;
  2125. if ((lsa->data->type == OSPF_AS_EXTERNAL_LSA) ||
  2126. (lsa->data->type == OSPF_OPAQUE_AS_LSA))
  2127. {
  2128. i = 0;
  2129. }
  2130. if (i > 0)
  2131. {
  2132. area = (u_int32_t *) (filter + 1);
  2133. while (i)
  2134. {
  2135. if (*area == area_id.s_addr)
  2136. {
  2137. break;
  2138. }
  2139. i--;
  2140. area++;
  2141. }
  2142. }
  2143. else
  2144. {
  2145. i = 1;
  2146. }
  2147. if (i > 0)
  2148. {
  2149. /* Area match. Check LSA type. */
  2150. mask = ntohs (filter->typemask);
  2151. if (mask & Power2[lsa->data->type])
  2152. {
  2153. /* Type also matches. Check origin. */
  2154. if ((filter->origin == ANY_ORIGIN) ||
  2155. (filter->origin == IS_LSA_SELF (lsa)))
  2156. {
  2157. ospf_apiserver_send_msg (apiserv, msg);
  2158. }
  2159. }
  2160. }
  2161. }
  2162. /* Free message since it is not used anymore */
  2163. msg_free (msg);
  2164. }
  2165. /* -------------------------------------------------------------
  2166. * Followings are hooks invoked when LSAs are updated or deleted
  2167. * -------------------------------------------------------------
  2168. */
  2169. static int
  2170. apiserver_notify_clients_lsa (u_char msgtype, struct ospf_lsa *lsa)
  2171. {
  2172. struct msg *msg;
  2173. /* default area for AS-External and Opaque11 LSAs */
  2174. struct in_addr area_id = { .s_addr = 0L };
  2175. /* default interface for non Opaque9 LSAs */
  2176. struct in_addr ifaddr = { .s_addr = 0L };
  2177. /* Only notify this update if the LSA's age is smaller than
  2178. MAXAGE. Otherwise clients would see LSA updates with max age just
  2179. before they are deleted from the LSDB. LSA delete messages have
  2180. MAXAGE too but should not be filtered. */
  2181. if (IS_LSA_MAXAGE(lsa) && (msgtype == MSG_LSA_UPDATE_NOTIFY)) {
  2182. return 0;
  2183. }
  2184. if (lsa->area)
  2185. {
  2186. area_id = lsa->area->area_id;
  2187. }
  2188. if (lsa->data->type == OSPF_OPAQUE_LINK_LSA)
  2189. {
  2190. ifaddr = lsa->oi->address->u.prefix4;
  2191. }
  2192. msg = new_msg_lsa_change_notify (msgtype, 0L, /* no sequence number */
  2193. ifaddr, area_id,
  2194. lsa->flags & OSPF_LSA_SELF, lsa->data);
  2195. if (!msg)
  2196. {
  2197. zlog_warn ("notify_clients_lsa: msg_new failed");
  2198. return -1;
  2199. }
  2200. /* Notify all clients that new LSA is added/updated */
  2201. apiserver_clients_lsa_change_notify (msgtype, lsa);
  2202. /* Clients made their own copies of msg so we can free msg here */
  2203. msg_free (msg);
  2204. return 0;
  2205. }
  2206. int
  2207. ospf_apiserver_lsa_update (struct ospf_lsa *lsa)
  2208. {
  2209. return apiserver_notify_clients_lsa (MSG_LSA_UPDATE_NOTIFY, lsa);
  2210. }
  2211. int
  2212. ospf_apiserver_lsa_delete (struct ospf_lsa *lsa)
  2213. {
  2214. return apiserver_notify_clients_lsa (MSG_LSA_DELETE_NOTIFY, lsa);
  2215. }
  2216. #endif /* SUPPORT_OSPF_API */