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