ospf_api.c 16 KB

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