fpm.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Public definitions pertaining to the Forwarding Plane Manager component.
  3. *
  4. * Permission is granted to use, copy, modify and/or distribute this
  5. * software under either one of the licenses below.
  6. *
  7. * Note that if you use other files from the Quagga tree directly or
  8. * indirectly, then the licenses in those files still apply.
  9. *
  10. * Please retain both licenses below when modifying this code in the
  11. * Quagga tree.
  12. *
  13. * Copyright (C) 2012 by Open Source Routing.
  14. * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
  15. */
  16. /*
  17. * License Option 1: GPL
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by the Free
  21. * Software Foundation; either version 2 of the License, or (at your option)
  22. * any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,but WITHOUT
  25. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  26. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  27. * more details.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  32. */
  33. /*
  34. * License Option 2: ISC License
  35. *
  36. * Permission to use, copy, modify, and/or distribute this software
  37. * for any purpose with or without fee is hereby granted, provided
  38. * that the above copyright notice and this permission notice appear
  39. * in all copies.
  40. *
  41. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  42. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  43. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  44. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  45. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  46. * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  47. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  48. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  49. */
  50. #ifndef _FPM_H
  51. #define _FPM_H
  52. /*
  53. * The Forwarding Plane Manager (FPM) is an optional component that
  54. * may be used in scenarios where the router has a forwarding path
  55. * that is distinct from the kernel, commonly a hardware-based fast
  56. * path. It is responsible for programming forwarding information
  57. * (such as routes and nexthops) in the fast path.
  58. *
  59. * In Quagga, the Routing Information Base is maintained in the
  60. * 'zebra' infrastructure daemon. Routing protocols communicate their
  61. * best routes to zebra, and zebra computes the best route across
  62. * protocols for each prefix. This latter information comprises the
  63. * bulk of the Forwarding Information Base.
  64. *
  65. * This header file defines a point-to-point interface using which
  66. * zebra can update the FPM about changes in routes. The communication
  67. * takes place over a stream socket. The FPM listens on a well-known
  68. * TCP port, and zebra initiates the connection.
  69. *
  70. * All messages sent over the connection start with a short FPM
  71. * header, fpm_msg_hdr_t. In the case of route add/delete messages,
  72. * the header is followed by a netlink message. Zebra should send a
  73. * complete copy of the forwarding table(s) to the FPM, including
  74. * routes that it may have picked up from the kernel.
  75. *
  76. * The FPM interface uses replace semantics. That is, if a 'route add'
  77. * message for a prefix is followed by another 'route add' message, the
  78. * information in the second message is complete by itself, and replaces
  79. * the information sent in the first message.
  80. *
  81. * If the connection to the FPM goes down for some reason, the client
  82. * (zebra) should send the FPM a complete copy of the forwarding
  83. * table(s) when it reconnects.
  84. */
  85. /*
  86. * Local host as a default server for fpm connection
  87. */
  88. #define FPM_DEFAULT_IP (htonl (INADDR_LOOPBACK))
  89. /*
  90. * default port for fpm connections
  91. */
  92. #define FPM_DEFAULT_PORT 2620
  93. /*
  94. * Largest message that can be sent to or received from the FPM.
  95. */
  96. #define FPM_MAX_MSG_LEN 4096
  97. #ifdef __SUNPRO_C
  98. #pragma pack(1)
  99. #endif
  100. /*
  101. * Header that precedes each fpm message to/from the FPM.
  102. */
  103. typedef struct fpm_msg_hdr_t_
  104. {
  105. /*
  106. * Protocol version.
  107. */
  108. uint8_t version;
  109. /*
  110. * Type of message, see below.
  111. */
  112. uint8_t msg_type;
  113. /*
  114. * Length of entire message, including the header, in network byte
  115. * order.
  116. */
  117. uint16_t msg_len;
  118. } __attribute__ ((packed)) fpm_msg_hdr_t;
  119. #ifdef __SUNPRO_C
  120. #pragma pack()
  121. #endif
  122. /*
  123. * The current version of the FPM protocol is 1.
  124. */
  125. #define FPM_PROTO_VERSION 1
  126. typedef enum fpm_msg_type_e_ {
  127. FPM_MSG_TYPE_NONE = 0,
  128. /*
  129. * Indicates that the payload is a completely formed netlink
  130. * message.
  131. *
  132. * XXX Netlink cares about the alignment of messages. When any
  133. * FPM_MSG_TYPE_NETLINK messages are sent over a channel, then all
  134. * messages should be sized such that netlink alignment is
  135. * maintained.
  136. */
  137. FPM_MSG_TYPE_NETLINK = 1,
  138. FPM_MSG_TYPE_PROTOBUF = 2,
  139. } fpm_msg_type_e;
  140. /*
  141. * The FPM message header is aligned to the same boundary as netlink
  142. * messages (4). This means that a netlink message does not need
  143. * padding when encapsulated in an FPM message.
  144. */
  145. #define FPM_MSG_ALIGNTO 4
  146. /*
  147. * fpm_msg_align
  148. *
  149. * Round up the given length to the desired alignment.
  150. *
  151. * **NB**: Alignment is required only when netlink messages are used.
  152. */
  153. static inline size_t
  154. fpm_msg_align (size_t len)
  155. {
  156. return (len + FPM_MSG_ALIGNTO - 1) & ~(FPM_MSG_ALIGNTO - 1);
  157. }
  158. /*
  159. * The (rounded up) size of the FPM message header. This ensures that
  160. * the message payload always starts at an aligned address.
  161. */
  162. #define FPM_MSG_HDR_LEN (sizeof (fpm_msg_hdr_t))
  163. #ifndef COMPILE_ASSERT
  164. #define COMPILE_ASSERT(x) extern int __dummy[2 * !!(x) - 1]
  165. #endif
  166. COMPILE_ASSERT(FPM_MSG_ALIGNTO == FPM_MSG_HDR_LEN);
  167. /*
  168. * fpm_data_len_to_msg_len
  169. *
  170. * The length value that should be placed in the msg_len field of the
  171. * header for a *payload* of size 'data_len'.
  172. */
  173. static inline size_t
  174. fpm_data_len_to_msg_len (size_t data_len)
  175. {
  176. return data_len + FPM_MSG_HDR_LEN;
  177. }
  178. /*
  179. * fpm_msg_data
  180. *
  181. * Pointer to the payload of the given fpm header.
  182. */
  183. static inline void *
  184. fpm_msg_data (fpm_msg_hdr_t *hdr)
  185. {
  186. return ((char*) hdr) + FPM_MSG_HDR_LEN;
  187. }
  188. /*
  189. * fpm_msg_len
  190. */
  191. static inline size_t
  192. fpm_msg_len (const fpm_msg_hdr_t *hdr)
  193. {
  194. return ntohs (hdr->msg_len);
  195. }
  196. /*
  197. * fpm_msg_data_len
  198. */
  199. static inline size_t
  200. fpm_msg_data_len (const fpm_msg_hdr_t *hdr)
  201. {
  202. return (fpm_msg_len (hdr) - FPM_MSG_HDR_LEN);
  203. }
  204. /*
  205. * fpm_msg_next
  206. *
  207. * Move to the next message in a buffer.
  208. */
  209. static inline fpm_msg_hdr_t *
  210. fpm_msg_next (fpm_msg_hdr_t *hdr, size_t *len)
  211. {
  212. size_t msg_len;
  213. msg_len = fpm_msg_len (hdr);
  214. if (len) {
  215. if (*len < msg_len)
  216. {
  217. assert(0);
  218. return NULL;
  219. }
  220. *len -= msg_len;
  221. }
  222. return (fpm_msg_hdr_t *) (((char*) hdr) + msg_len);
  223. }
  224. /*
  225. * fpm_msg_hdr_ok
  226. *
  227. * Returns TRUE if a message header looks well-formed.
  228. */
  229. static inline int
  230. fpm_msg_hdr_ok (const fpm_msg_hdr_t *hdr)
  231. {
  232. size_t msg_len;
  233. if (hdr->msg_type == FPM_MSG_TYPE_NONE)
  234. return 0;
  235. msg_len = fpm_msg_len (hdr);
  236. if (msg_len < FPM_MSG_HDR_LEN || msg_len > FPM_MAX_MSG_LEN)
  237. return 0;
  238. /*
  239. * Netlink messages must be aligned properly.
  240. */
  241. if (hdr->msg_type == FPM_MSG_TYPE_NETLINK &&
  242. fpm_msg_align (msg_len) != msg_len)
  243. return 0;
  244. return 1;
  245. }
  246. /*
  247. * fpm_msg_ok
  248. *
  249. * Returns TRUE if a message looks well-formed.
  250. *
  251. * @param len The length in bytes from 'hdr' to the end of the buffer.
  252. */
  253. static inline int
  254. fpm_msg_ok (const fpm_msg_hdr_t *hdr, size_t len)
  255. {
  256. if (len < FPM_MSG_HDR_LEN)
  257. return 0;
  258. if (!fpm_msg_hdr_ok (hdr))
  259. return 0;
  260. if (fpm_msg_len (hdr) > len)
  261. return 0;
  262. return 1;
  263. }
  264. // tcp maximum range
  265. #define TCP_MAX_PORT 65535
  266. // tcp minimum range
  267. #define TCP_MIN_PORT 1
  268. #endif /* _FPM_H */