ospf_apiserver.c 66 KB

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