ospf_api.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. /* This file is used both by the OSPFd and client applications to
  23. define message formats used for communication. */
  24. #ifndef _OSPF_API_H
  25. #define _OSPF_API_H
  26. #define OSPF_API_VERSION 1
  27. /* MTYPE definition is not reflected to "memory.h". */
  28. #define MTYPE_OSPF_API_MSG MTYPE_TMP
  29. #define MTYPE_OSPF_API_FIFO MTYPE_TMP
  30. /* Default API server port to accept connection request from client-side. */
  31. /* This value could be overridden by "ospfapi" entry in "/etc/services". */
  32. #define OSPF_API_SYNC_PORT 2607
  33. /* -----------------------------------------------------------
  34. * Generic messages
  35. * -----------------------------------------------------------
  36. */
  37. /* Message header structure, fields are in network byte order and
  38. aligned to four octets. */
  39. struct apimsghdr
  40. {
  41. u_char version; /* OSPF API protocol version */
  42. u_char msgtype; /* Type of message */
  43. u_int16_t msglen; /* Length of message w/o header */
  44. u_int32_t msgseq; /* Sequence number */
  45. };
  46. /* Message representation with header and body */
  47. struct msg
  48. {
  49. struct msg *next; /* to link into fifo */
  50. /* Message header */
  51. struct apimsghdr hdr;
  52. /* Message body */
  53. struct stream *s;
  54. };
  55. /* Prototypes for generic messages. */
  56. extern struct msg *msg_new (u_char msgtype, void *msgbody,
  57. u_int32_t seqnum, u_int16_t msglen);
  58. extern struct msg *msg_dup (struct msg *msg);
  59. extern void msg_print (struct msg *msg); /* XXX debug only */
  60. extern void msg_free (struct msg *msg);
  61. struct msg *msg_read (int fd);
  62. extern int msg_write (int fd, struct msg *msg);
  63. /* For requests, the message sequence number is between MIN_SEQ and
  64. MAX_SEQ. For notifications, the sequence number is 0. */
  65. #define MIN_SEQ 1
  66. #define MAX_SEQ 2147483647
  67. extern void msg_set_seq (struct msg *msg, u_int32_t seqnr);
  68. extern u_int32_t msg_get_seq (struct msg *msg);
  69. /* -----------------------------------------------------------
  70. * Message fifo queues
  71. * -----------------------------------------------------------
  72. */
  73. /* Message queue structure. */
  74. struct msg_fifo
  75. {
  76. unsigned long count;
  77. struct msg *head;
  78. struct msg *tail;
  79. };
  80. /* Prototype for message fifo queues. */
  81. extern struct msg_fifo *msg_fifo_new (void);
  82. extern void msg_fifo_push (struct msg_fifo *, struct msg *msg);
  83. extern struct msg *msg_fifo_pop (struct msg_fifo *fifo);
  84. extern struct msg *msg_fifo_head (struct msg_fifo *fifo);
  85. extern void msg_fifo_flush (struct msg_fifo *fifo);
  86. extern void msg_fifo_free (struct msg_fifo *fifo);
  87. /* -----------------------------------------------------------
  88. * Specific message type and format definitions
  89. * -----------------------------------------------------------
  90. */
  91. /* Messages to OSPF daemon. */
  92. #define MSG_REGISTER_OPAQUETYPE 1
  93. #define MSG_UNREGISTER_OPAQUETYPE 2
  94. #define MSG_REGISTER_EVENT 3
  95. #define MSG_SYNC_LSDB 4
  96. #define MSG_ORIGINATE_REQUEST 5
  97. #define MSG_DELETE_REQUEST 6
  98. /* Messages from OSPF daemon. */
  99. #define MSG_REPLY 10
  100. #define MSG_READY_NOTIFY 11
  101. #define MSG_LSA_UPDATE_NOTIFY 12
  102. #define MSG_LSA_DELETE_NOTIFY 13
  103. #define MSG_NEW_IF 14
  104. #define MSG_DEL_IF 15
  105. #define MSG_ISM_CHANGE 16
  106. #define MSG_NSM_CHANGE 17
  107. struct msg_register_opaque_type
  108. {
  109. u_char lsatype;
  110. u_char opaquetype;
  111. u_char pad[2]; /* padding */
  112. };
  113. struct msg_unregister_opaque_type
  114. {
  115. u_char lsatype;
  116. u_char opaquetype;
  117. u_char pad[2]; /* padding */
  118. };
  119. /* Power2 is needed to convert LSA types into bit positions,
  120. * see typemask below. Type definition starts at 1, so
  121. * Power2[0] is not used. */
  122. #ifdef ORIGINAL_CODING
  123. static const u_int16_t
  124. Power2[] = { 0x0, 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80,
  125. 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000
  126. };
  127. #else
  128. static const u_int16_t
  129. Power2[] = { 0, (1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4),
  130. (1 << 5), (1 << 6), (1 << 7), (1 << 8), (1 << 9),
  131. (1 << 10), (1 << 11), (1 << 12), (1 << 13), (1 << 14),
  132. (1 << 15)
  133. };
  134. #endif /* ORIGINAL_CODING */
  135. struct lsa_filter_type
  136. {
  137. u_int16_t typemask; /* bitmask for selecting LSA types (1..16) */
  138. u_char origin; /* selects according to origin. */
  139. #define NON_SELF_ORIGINATED 0
  140. #define SELF_ORIGINATED (OSPF_LSA_SELF)
  141. #define ANY_ORIGIN 2
  142. u_char num_areas; /* number of areas in the filter. */
  143. /* areas, if any, go here. */
  144. };
  145. struct msg_register_event
  146. {
  147. struct lsa_filter_type filter;
  148. };
  149. struct msg_sync_lsdb
  150. {
  151. struct lsa_filter_type filter;
  152. };
  153. struct msg_originate_request
  154. {
  155. /* Used for LSA type 9 otherwise ignored */
  156. struct in_addr ifaddr;
  157. /* Used for LSA type 10 otherwise ignored */
  158. struct in_addr area_id;
  159. /* LSA header and LSA-specific part */
  160. struct lsa_header data;
  161. };
  162. struct msg_delete_request
  163. {
  164. struct in_addr area_id; /* "0.0.0.0" for AS-external opaque LSAs */
  165. u_char lsa_type;
  166. u_char opaque_type;
  167. u_char pad[2]; /* padding */
  168. u_int32_t opaque_id;
  169. };
  170. struct msg_reply
  171. {
  172. signed char errcode;
  173. #define OSPF_API_OK 0
  174. #define OSPF_API_NOSUCHINTERFACE (-1)
  175. #define OSPF_API_NOSUCHAREA (-2)
  176. #define OSPF_API_NOSUCHLSA (-3)
  177. #define OSPF_API_ILLEGALLSATYPE (-4)
  178. #define OSPF_API_OPAQUETYPEINUSE (-5)
  179. #define OSPF_API_OPAQUETYPENOTREGISTERED (-6)
  180. #define OSPF_API_NOTREADY (-7)
  181. #define OSPF_API_NOMEMORY (-8)
  182. #define OSPF_API_ERROR (-9)
  183. #define OSPF_API_UNDEF (-10)
  184. u_char pad[3]; /* padding to four byte alignment */
  185. };
  186. /* Message to tell client application that it ospf daemon is
  187. * ready to accept opaque LSAs for a given interface or area. */
  188. struct msg_ready_notify
  189. {
  190. u_char lsa_type;
  191. u_char opaque_type;
  192. u_char pad[2]; /* padding */
  193. struct in_addr addr; /* interface address or area address */
  194. };
  195. /* These messages have a dynamic length depending on the embodied LSA.
  196. They are aligned to four octets. msg_lsa_change_notify is used for
  197. both LSA update and LSAs delete. */
  198. struct msg_lsa_change_notify
  199. {
  200. /* Used for LSA type 9 otherwise ignored */
  201. struct in_addr ifaddr;
  202. /* Area ID. Not valid for AS-External and Opaque11 LSAs. */
  203. struct in_addr area_id;
  204. u_char is_self_originated; /* 1 if self originated. */
  205. u_char pad[3];
  206. struct lsa_header data;
  207. };
  208. struct msg_new_if
  209. {
  210. struct in_addr ifaddr; /* interface IP address */
  211. struct in_addr area_id; /* area this interface belongs to */
  212. };
  213. struct msg_del_if
  214. {
  215. struct in_addr ifaddr; /* interface IP address */
  216. };
  217. struct msg_ism_change
  218. {
  219. struct in_addr ifaddr; /* interface IP address */
  220. struct in_addr area_id; /* area this interface belongs to */
  221. u_char status; /* interface status (up/down) */
  222. u_char pad[3]; /* not used */
  223. };
  224. struct msg_nsm_change
  225. {
  226. struct in_addr ifaddr; /* attached interface */
  227. struct in_addr nbraddr; /* Neighbor interface address */
  228. struct in_addr router_id; /* Router ID of neighbor */
  229. u_char status; /* NSM status */
  230. u_char pad[3];
  231. };
  232. /* We make use of a union to define a structure that covers all
  233. possible API messages. This allows us to find out how much memory
  234. needs to be reserved for the largest API message. */
  235. struct apimsg
  236. {
  237. struct apimsghdr hdr;
  238. union
  239. {
  240. struct msg_register_opaque_type register_opaque_type;
  241. struct msg_register_event register_event;
  242. struct msg_sync_lsdb sync_lsdb;
  243. struct msg_originate_request originate_request;
  244. struct msg_delete_request delete_request;
  245. struct msg_reply reply;
  246. struct msg_ready_notify ready_notify;
  247. struct msg_new_if new_if;
  248. struct msg_del_if del_if;
  249. struct msg_ism_change ism_change;
  250. struct msg_nsm_change nsm_change;
  251. struct msg_lsa_change_notify lsa_change_notify;
  252. }
  253. u;
  254. };
  255. #define OSPF_API_MAX_MSG_SIZE (sizeof(struct apimsg) + OSPF_MAX_LSA_SIZE)
  256. /* -----------------------------------------------------------
  257. * Prototypes for specific messages
  258. * -----------------------------------------------------------
  259. */
  260. /* For debugging only. */
  261. extern void api_opaque_lsa_print (struct lsa_header *data);
  262. /* Messages sent by client */
  263. extern struct msg *new_msg_register_opaque_type (u_int32_t seqnum,
  264. u_char ltype, u_char otype);
  265. extern struct msg *new_msg_register_event (u_int32_t seqnum,
  266. struct lsa_filter_type *filter);
  267. extern struct msg *new_msg_sync_lsdb (u_int32_t seqnum,
  268. struct lsa_filter_type *filter);
  269. extern struct msg *new_msg_originate_request (u_int32_t seqnum,
  270. struct in_addr ifaddr,
  271. struct in_addr area_id,
  272. struct lsa_header *data);
  273. extern struct msg *new_msg_delete_request (u_int32_t seqnum,
  274. struct in_addr area_id,
  275. u_char lsa_type,
  276. u_char opaque_type,
  277. u_int32_t opaque_id);
  278. /* Messages sent by OSPF daemon */
  279. extern struct msg *new_msg_reply (u_int32_t seqnum, u_char rc);
  280. extern struct msg *new_msg_ready_notify (u_int32_t seqnr, u_char lsa_type,
  281. u_char opaque_type,
  282. struct in_addr addr);
  283. extern struct msg *new_msg_new_if (u_int32_t seqnr,
  284. struct in_addr ifaddr,
  285. struct in_addr area);
  286. extern struct msg *new_msg_del_if (u_int32_t seqnr, struct in_addr ifaddr);
  287. extern struct msg *new_msg_ism_change (u_int32_t seqnr, struct in_addr ifaddr,
  288. struct in_addr area, u_char status);
  289. extern struct msg *new_msg_nsm_change (u_int32_t seqnr, struct in_addr ifaddr,
  290. struct in_addr nbraddr,
  291. struct in_addr router_id,
  292. u_char status);
  293. /* msgtype is MSG_LSA_UPDATE_NOTIFY or MSG_LSA_DELETE_NOTIFY */
  294. extern struct msg *new_msg_lsa_change_notify (u_char msgtype,
  295. u_int32_t seqnum,
  296. struct in_addr ifaddr,
  297. struct in_addr area_id,
  298. u_char is_self_originated,
  299. struct lsa_header *data);
  300. /* string printing functions */
  301. extern const char *ospf_api_errname (int errcode);
  302. extern const char *ospf_api_typename (int msgtype);
  303. #endif /* _OSPF_API_H */