ospf_api.c 16 KB

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