ospf_api.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. * API message handling module for OSPF daemon and client.
  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 "network.h"
  41. #include "ospfd/ospfd.h"
  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. /* For debugging only, will be removed */
  58. void
  59. api_opaque_lsa_print (struct lsa_header *data)
  60. {
  61. struct opaque_lsa
  62. {
  63. struct lsa_header header;
  64. u_char mydata[0];
  65. };
  66. struct opaque_lsa *olsa;
  67. int opaquelen;
  68. int i;
  69. ospf_lsa_header_dump (data);
  70. olsa = (struct opaque_lsa *) data;
  71. opaquelen = ntohs (data->length) - OSPF_LSA_HEADER_SIZE;
  72. zlog_warn ("apiserver_lsa_print: opaquelen=%d\n", opaquelen);
  73. for (i = 0; i < opaquelen; i++)
  74. {
  75. zlog_warn ("0x%x ", olsa->mydata[i]);
  76. }
  77. zlog_warn ("\n");
  78. }
  79. /* -----------------------------------------------------------
  80. * Generic messages
  81. * -----------------------------------------------------------
  82. */
  83. struct msg *
  84. msg_new (u_char msgtype, void *msgbody, u_int32_t seqnum, u_int16_t msglen)
  85. {
  86. struct msg *new;
  87. new = XMALLOC (MTYPE_OSPF_API_MSG, sizeof (struct msg));
  88. memset (new, 0, sizeof (struct msg));
  89. new->hdr.version = OSPF_API_VERSION;
  90. new->hdr.msgtype = msgtype;
  91. new->hdr.msglen = htons (msglen);
  92. new->hdr.msgseq = htonl (seqnum);
  93. new->s = stream_new (msglen);
  94. assert (new->s);
  95. stream_put (new->s, msgbody, msglen);
  96. return new;
  97. }
  98. /* Duplicate a message by copying content. */
  99. struct msg *
  100. msg_dup (struct msg *msg)
  101. {
  102. struct msg *new;
  103. assert (msg);
  104. new = msg_new (msg->hdr.msgtype, STREAM_DATA (msg->s),
  105. ntohl (msg->hdr.msgseq), ntohs (msg->hdr.msglen));
  106. return new;
  107. }
  108. /* XXX only for testing, will be removed */
  109. struct nametab {
  110. int value;
  111. const char *name;
  112. };
  113. const char *
  114. ospf_api_typename (int msgtype)
  115. {
  116. struct nametab NameTab[] = {
  117. { MSG_REGISTER_OPAQUETYPE, "Register opaque-type", },
  118. { MSG_UNREGISTER_OPAQUETYPE, "Unregister opaque-type", },
  119. { MSG_REGISTER_EVENT, "Register event", },
  120. { MSG_SYNC_LSDB, "Sync LSDB", },
  121. { MSG_ORIGINATE_REQUEST, "Originate request", },
  122. { MSG_DELETE_REQUEST, "Delete request", },
  123. { MSG_REPLY, "Reply", },
  124. { MSG_READY_NOTIFY, "Ready notify", },
  125. { MSG_LSA_UPDATE_NOTIFY, "LSA update notify", },
  126. { MSG_LSA_DELETE_NOTIFY, "LSA delete notify", },
  127. { MSG_NEW_IF, "New interface", },
  128. { MSG_DEL_IF, "Del interface", },
  129. { MSG_ISM_CHANGE, "ISM change", },
  130. { MSG_NSM_CHANGE, "NSM change", },
  131. };
  132. int i, n = sizeof (NameTab) / sizeof (NameTab[0]);
  133. const char *name = NULL;
  134. for (i = 0; i < n; i++)
  135. {
  136. if (NameTab[i].value == msgtype)
  137. {
  138. name = NameTab[i].name;
  139. break;
  140. }
  141. }
  142. return name ? name : "?";
  143. }
  144. const char *
  145. ospf_api_errname (int errcode)
  146. {
  147. struct nametab NameTab[] = {
  148. { OSPF_API_OK, "OK", },
  149. { OSPF_API_NOSUCHINTERFACE, "No such interface", },
  150. { OSPF_API_NOSUCHAREA, "No such area", },
  151. { OSPF_API_NOSUCHLSA, "No such LSA", },
  152. { OSPF_API_ILLEGALLSATYPE, "Illegal LSA type", },
  153. { OSPF_API_OPAQUETYPEINUSE, "Opaque type in use", },
  154. { OSPF_API_OPAQUETYPENOTREGISTERED, "Opaque type not registered", },
  155. { OSPF_API_NOTREADY, "Not ready", },
  156. { OSPF_API_NOMEMORY, "No memory", },
  157. { OSPF_API_ERROR, "Other error", },
  158. { OSPF_API_UNDEF, "Undefined", },
  159. };
  160. int i, n = sizeof (NameTab) / sizeof (NameTab[0]);
  161. const char *name = NULL;
  162. for (i = 0; i < n; i++)
  163. {
  164. if (NameTab[i].value == errcode)
  165. {
  166. name = NameTab[i].name;
  167. break;
  168. }
  169. }
  170. return name ? name : "?";
  171. }
  172. void
  173. msg_print (struct msg *msg)
  174. {
  175. if (!msg)
  176. {
  177. zlog_warn ("msg_print msg=NULL!\n");
  178. return;
  179. }
  180. #ifdef ORIGINAL_CODING
  181. zlog_warn
  182. ("msg=%p msgtype=%d msglen=%d msgseq=%d streamdata=%p streamsize=%lu\n",
  183. msg, msg->hdr.msgtype, ntohs (msg->hdr.msglen), ntohl (msg->hdr.msgseq),
  184. STREAM_DATA (msg->s), STREAM_SIZE (msg->s));
  185. #else /* ORIGINAL_CODING */
  186. /* API message common header part. */
  187. zlog_info
  188. ("API-msg [%s]: type(%d),len(%d),seq(%lu),data(%p),size(%lu)",
  189. ospf_api_typename (msg->hdr.msgtype), msg->hdr.msgtype,
  190. ntohs (msg->hdr.msglen), (unsigned long) ntohl (msg->hdr.msgseq),
  191. STREAM_DATA (msg->s), STREAM_SIZE (msg->s));
  192. /* API message body part. */
  193. #ifdef ndef
  194. /* Generic Hex/Ascii dump */
  195. DumpBuf (STREAM_DATA (msg->s), STREAM_SIZE (msg->s)); /* Sorry, deleted! */
  196. #else /* ndef */
  197. /* Message-type dependent dump function. */
  198. #endif /* ndef */
  199. return;
  200. #endif /* ORIGINAL_CODING */
  201. }
  202. void
  203. msg_free (struct msg *msg)
  204. {
  205. if (msg->s)
  206. stream_free (msg->s);
  207. XFREE (MTYPE_OSPF_API_MSG, msg);
  208. }
  209. /* Set sequence number of message */
  210. void
  211. msg_set_seq (struct msg *msg, u_int32_t seqnr)
  212. {
  213. assert (msg);
  214. msg->hdr.msgseq = htonl (seqnr);
  215. }
  216. /* Get sequence number of message */
  217. u_int32_t
  218. msg_get_seq (struct msg *msg)
  219. {
  220. assert (msg);
  221. return ntohl (msg->hdr.msgseq);
  222. }
  223. /* -----------------------------------------------------------
  224. * Message fifo queues
  225. * -----------------------------------------------------------
  226. */
  227. struct msg_fifo *
  228. msg_fifo_new ()
  229. {
  230. struct msg_fifo *new;
  231. new = XMALLOC (MTYPE_OSPF_API_FIFO, sizeof (struct msg_fifo));
  232. memset (new, 0, sizeof (struct msg_fifo));
  233. return new;
  234. }
  235. /* Add new message to fifo. */
  236. void
  237. msg_fifo_push (struct msg_fifo *fifo, struct msg *msg)
  238. {
  239. if (fifo->tail)
  240. fifo->tail->next = msg;
  241. else
  242. fifo->head = msg;
  243. fifo->tail = msg;
  244. fifo->count++;
  245. }
  246. /* Remove first message from fifo. */
  247. struct msg *
  248. msg_fifo_pop (struct msg_fifo *fifo)
  249. {
  250. struct msg *msg;
  251. msg = fifo->head;
  252. if (msg)
  253. {
  254. fifo->head = msg->next;
  255. if (fifo->head == NULL)
  256. fifo->tail = NULL;
  257. fifo->count--;
  258. }
  259. return msg;
  260. }
  261. /* Return first fifo entry but do not remove it. */
  262. struct msg *
  263. msg_fifo_head (struct msg_fifo *fifo)
  264. {
  265. return fifo->head;
  266. }
  267. /* Flush message fifo. */
  268. void
  269. msg_fifo_flush (struct msg_fifo *fifo)
  270. {
  271. struct msg *op;
  272. struct msg *next;
  273. for (op = fifo->head; op; op = next)
  274. {
  275. next = op->next;
  276. msg_free (op);
  277. }
  278. fifo->head = fifo->tail = NULL;
  279. fifo->count = 0;
  280. }
  281. /* Free API message fifo. */
  282. void
  283. msg_fifo_free (struct msg_fifo *fifo)
  284. {
  285. msg_fifo_flush (fifo);
  286. XFREE (MTYPE_OSPF_API_FIFO, fifo);
  287. }
  288. struct msg *
  289. msg_read (int fd)
  290. {
  291. struct msg *msg;
  292. struct apimsghdr hdr;
  293. char buf[OSPF_API_MAX_MSG_SIZE];
  294. int bodylen;
  295. int rlen;
  296. /* Read message header */
  297. rlen = readn (fd, (char *) &hdr, sizeof (struct apimsghdr));
  298. if (rlen < 0)
  299. {
  300. zlog_warn ("msg_read: readn %s", strerror (errno));
  301. return NULL;
  302. }
  303. else if (rlen == 0)
  304. {
  305. zlog_warn ("msg_read: Connection closed by peer");
  306. return NULL;
  307. }
  308. else if (rlen != sizeof (struct apimsghdr))
  309. {
  310. zlog_warn ("msg_read: Cannot read message header!");
  311. return NULL;
  312. }
  313. /* Check version of API protocol */
  314. if (hdr.version != OSPF_API_VERSION)
  315. {
  316. zlog_warn ("msg_read: OSPF API protocol version mismatch");
  317. return NULL;
  318. }
  319. /* Determine body length. */
  320. bodylen = ntohs (hdr.msglen);
  321. if (bodylen > 0)
  322. {
  323. /* Read message body */
  324. rlen = readn (fd, buf, bodylen);
  325. if (rlen < 0)
  326. {
  327. zlog_warn ("msg_read: readn %s", strerror (errno));
  328. return NULL;
  329. }
  330. else if (rlen == 0)
  331. {
  332. zlog_warn ("msg_read: Connection closed by peer");
  333. return NULL;
  334. }
  335. else if (rlen != bodylen)
  336. {
  337. zlog_warn ("msg_read: Cannot read message body!");
  338. return NULL;
  339. }
  340. }
  341. /* Allocate new message */
  342. msg = msg_new (hdr.msgtype, buf, ntohl (hdr.msgseq), ntohs (hdr.msglen));
  343. return msg;
  344. }
  345. int
  346. msg_write (int fd, struct msg *msg)
  347. {
  348. u_char buf[OSPF_API_MAX_MSG_SIZE];
  349. int l;
  350. int wlen;
  351. assert (msg);
  352. assert (msg->s);
  353. /* Length of message including header */
  354. l = sizeof (struct apimsghdr) + ntohs (msg->hdr.msglen);
  355. /* Make contiguous memory buffer for message */
  356. memcpy (buf, &msg->hdr, sizeof (struct apimsghdr));
  357. memcpy (buf + sizeof (struct apimsghdr), STREAM_DATA (msg->s),
  358. ntohs (msg->hdr.msglen));
  359. wlen = writen (fd, buf, l);
  360. if (wlen < 0)
  361. {
  362. zlog_warn ("msg_write: writen %s", strerror (errno));
  363. return -1;
  364. }
  365. else if (wlen == 0)
  366. {
  367. zlog_warn ("msg_write: Connection closed by peer");
  368. return -1;
  369. }
  370. else if (wlen != l)
  371. {
  372. zlog_warn ("msg_write: Cannot write API message");
  373. return -1;
  374. }
  375. return 0;
  376. }
  377. /* -----------------------------------------------------------
  378. * Specific messages
  379. * -----------------------------------------------------------
  380. */
  381. struct msg *
  382. new_msg_register_opaque_type (u_int32_t seqnum, u_char ltype, u_char otype)
  383. {
  384. struct msg_register_opaque_type rmsg;
  385. rmsg.lsatype = ltype;
  386. rmsg.opaquetype = otype;
  387. memset (&rmsg.pad, 0, sizeof (rmsg.pad));
  388. return msg_new (MSG_REGISTER_OPAQUETYPE, &rmsg, seqnum,
  389. sizeof (struct msg_register_opaque_type));
  390. }
  391. struct msg *
  392. new_msg_register_event (u_int32_t seqnum, struct lsa_filter_type *filter)
  393. {
  394. u_char buf[OSPF_API_MAX_MSG_SIZE];
  395. struct msg_register_event *emsg;
  396. int len;
  397. emsg = (struct msg_register_event *) buf;
  398. len = sizeof (struct msg_register_event) +
  399. filter->num_areas * sizeof (struct in_addr);
  400. emsg->filter.typemask = htons (filter->typemask);
  401. emsg->filter.origin = filter->origin;
  402. emsg->filter.num_areas = filter->num_areas;
  403. return msg_new (MSG_REGISTER_EVENT, emsg, seqnum, len);
  404. }
  405. struct msg *
  406. new_msg_sync_lsdb (u_int32_t seqnum, struct lsa_filter_type *filter)
  407. {
  408. u_char buf[OSPF_API_MAX_MSG_SIZE];
  409. struct msg_sync_lsdb *smsg;
  410. int len;
  411. smsg = (struct msg_sync_lsdb *) buf;
  412. len = sizeof (struct msg_sync_lsdb) +
  413. filter->num_areas * sizeof (struct in_addr);
  414. smsg->filter.typemask = htons (filter->typemask);
  415. smsg->filter.origin = filter->origin;
  416. smsg->filter.num_areas = filter->num_areas;
  417. return msg_new (MSG_SYNC_LSDB, smsg, seqnum, len);
  418. }
  419. struct msg *
  420. new_msg_originate_request (u_int32_t seqnum,
  421. struct in_addr ifaddr,
  422. struct in_addr area_id, struct lsa_header *data)
  423. {
  424. struct msg_originate_request *omsg;
  425. int omsglen;
  426. char buf[OSPF_API_MAX_MSG_SIZE];
  427. omsglen = sizeof (struct msg_originate_request) - sizeof (struct lsa_header)
  428. + ntohs (data->length);
  429. omsg = (struct msg_originate_request *) buf;
  430. omsg->ifaddr = ifaddr;
  431. omsg->area_id = area_id;
  432. memcpy (&omsg->data, data, ntohs (data->length));
  433. return msg_new (MSG_ORIGINATE_REQUEST, omsg, seqnum, omsglen);
  434. }
  435. struct msg *
  436. new_msg_delete_request (u_int32_t seqnum,
  437. struct in_addr area_id, u_char lsa_type,
  438. u_char opaque_type, u_int32_t opaque_id)
  439. {
  440. struct msg_delete_request dmsg;
  441. dmsg.area_id = area_id;
  442. dmsg.lsa_type = lsa_type;
  443. dmsg.opaque_type = opaque_type;
  444. dmsg.opaque_id = htonl (opaque_id);
  445. memset (&dmsg.pad, 0, sizeof (dmsg.pad));
  446. return msg_new (MSG_DELETE_REQUEST, &dmsg, seqnum,
  447. sizeof (struct msg_delete_request));
  448. }
  449. struct msg *
  450. new_msg_reply (u_int32_t seqnr, u_char rc)
  451. {
  452. struct msg *msg;
  453. struct msg_reply rmsg;
  454. /* Set return code */
  455. rmsg.errcode = rc;
  456. memset (&rmsg.pad, 0, sizeof (rmsg.pad));
  457. msg = msg_new (MSG_REPLY, &rmsg, seqnr, sizeof (struct msg_reply));
  458. return msg;
  459. }
  460. struct msg *
  461. new_msg_ready_notify (u_int32_t seqnr, u_char lsa_type,
  462. u_char opaque_type, struct in_addr addr)
  463. {
  464. struct msg_ready_notify rmsg;
  465. rmsg.lsa_type = lsa_type;
  466. rmsg.opaque_type = opaque_type;
  467. memset (&rmsg.pad, 0, sizeof (rmsg.pad));
  468. rmsg.addr = addr;
  469. return msg_new (MSG_READY_NOTIFY, &rmsg, seqnr,
  470. sizeof (struct msg_ready_notify));
  471. }
  472. struct msg *
  473. new_msg_new_if (u_int32_t seqnr,
  474. struct in_addr ifaddr, struct in_addr area_id)
  475. {
  476. struct msg_new_if nmsg;
  477. nmsg.ifaddr = ifaddr;
  478. nmsg.area_id = area_id;
  479. return msg_new (MSG_NEW_IF, &nmsg, seqnr, sizeof (struct msg_new_if));
  480. }
  481. struct msg *
  482. new_msg_del_if (u_int32_t seqnr, struct in_addr ifaddr)
  483. {
  484. struct msg_del_if dmsg;
  485. dmsg.ifaddr = ifaddr;
  486. return msg_new (MSG_DEL_IF, &dmsg, seqnr, sizeof (struct msg_del_if));
  487. }
  488. struct msg *
  489. new_msg_ism_change (u_int32_t seqnr, struct in_addr ifaddr,
  490. struct in_addr area_id, u_char status)
  491. {
  492. struct msg_ism_change imsg;
  493. imsg.ifaddr = ifaddr;
  494. imsg.area_id = area_id;
  495. imsg.status = status;
  496. memset (&imsg.pad, 0, sizeof (imsg.pad));
  497. return msg_new (MSG_ISM_CHANGE, &imsg, seqnr,
  498. sizeof (struct msg_ism_change));
  499. }
  500. struct msg *
  501. new_msg_nsm_change (u_int32_t seqnr, struct in_addr ifaddr,
  502. struct in_addr nbraddr,
  503. struct in_addr router_id, u_char status)
  504. {
  505. struct msg_nsm_change nmsg;
  506. nmsg.ifaddr = ifaddr;
  507. nmsg.nbraddr = nbraddr;
  508. nmsg.router_id = router_id;
  509. nmsg.status = status;
  510. memset (&nmsg.pad, 0, sizeof (nmsg.pad));
  511. return msg_new (MSG_NSM_CHANGE, &nmsg, seqnr,
  512. sizeof (struct msg_nsm_change));
  513. }
  514. struct msg *
  515. new_msg_lsa_change_notify (u_char msgtype,
  516. u_int32_t seqnum,
  517. struct in_addr ifaddr,
  518. struct in_addr area_id,
  519. u_char is_self_originated, struct lsa_header *data)
  520. {
  521. u_char buf[OSPF_API_MAX_MSG_SIZE];
  522. struct msg_lsa_change_notify *nmsg;
  523. int len;
  524. assert (data);
  525. nmsg = (struct msg_lsa_change_notify *) buf;
  526. len = ntohs (data->length) + sizeof (struct msg_lsa_change_notify)
  527. - sizeof (struct lsa_header);
  528. nmsg->ifaddr = ifaddr;
  529. nmsg->area_id = area_id;
  530. nmsg->is_self_originated = is_self_originated;
  531. memset (&nmsg->pad, 0, sizeof (nmsg->pad));
  532. memcpy (&nmsg->data, data, ntohs (data->length));
  533. return msg_new (msgtype, nmsg, seqnum, len);
  534. }
  535. #endif /* SUPPORT_OSPF_API */