1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * API message handling module for OSPF daemon and client.
4 * Copyright (C) 2001, 2002 Ralph Keller
5 * Copyright (c) 2022, LabN Consulting, L.L.C.
9 /* This file is used both by the OSPFd and client applications to
10 define message formats used for communication. */
18 #define OSPF_API_VERSION 1
20 /* MTYPE definition is not reflected to "memory.h". */
21 #define MTYPE_OSPF_API_MSG MTYPE_TMP
22 #define MTYPE_OSPF_API_FIFO MTYPE_TMP
24 /* Default API server port to accept connection request from client-side. */
25 /* This value could be overridden by "ospfapi" entry in "/etc/services". */
26 #define OSPF_API_SYNC_PORT 2607
28 /* -----------------------------------------------------------
30 * -----------------------------------------------------------
33 /* Message header structure, fields are in network byte order and
34 aligned to four octets. */
36 uint8_t version
; /* OSPF API protocol version */
37 uint8_t msgtype
; /* Type of message */
38 uint16_t msglen
; /* Length of message w/o header */
39 uint32_t msgseq
; /* Sequence number */
42 /* Message representation with header and body */
44 struct msg
*next
; /* to link into fifo */
53 /* Prototypes for generic messages. */
54 extern struct msg
*msg_new(uint8_t msgtype
, void *msgbody
, uint32_t seqnum
,
56 extern struct msg
*msg_dup(struct msg
*msg
);
57 extern void msg_print(struct msg
*msg
); /* XXX debug only */
58 extern void msg_free(struct msg
*msg
);
59 struct msg
*msg_read(int fd
);
60 extern int msg_write(int fd
, struct msg
*msg
);
62 /* For requests, the message sequence number is between MIN_SEQ and
63 MAX_SEQ. For notifications, the sequence number is 0. */
66 #define MAX_SEQ 2147483647
68 extern void msg_set_seq(struct msg
*msg
, uint32_t seqnr
);
69 extern uint32_t msg_get_seq(struct msg
*msg
);
71 /* -----------------------------------------------------------
73 * -----------------------------------------------------------
76 /* Message queue structure. */
84 /* Prototype for message fifo queues. */
85 extern struct msg_fifo
*msg_fifo_new(void);
86 extern void msg_fifo_push(struct msg_fifo
*, struct msg
*msg
);
87 extern struct msg
*msg_fifo_pop(struct msg_fifo
*fifo
);
88 extern struct msg
*msg_fifo_head(struct msg_fifo
*fifo
);
89 extern void msg_fifo_flush(struct msg_fifo
*fifo
);
90 extern void msg_fifo_free(struct msg_fifo
*fifo
);
92 /* -----------------------------------------------------------
93 * Specific message type and format definitions
94 * -----------------------------------------------------------
97 /* Messages to OSPF daemon. */
98 #define MSG_REGISTER_OPAQUETYPE 1
99 #define MSG_UNREGISTER_OPAQUETYPE 2
100 #define MSG_REGISTER_EVENT 3
101 #define MSG_SYNC_LSDB 4
102 #define MSG_ORIGINATE_REQUEST 5
103 #define MSG_DELETE_REQUEST 6
104 #define MSG_SYNC_REACHABLE 7
105 #define MSG_SYNC_ISM 8
106 #define MSG_SYNC_NSM 9
107 #define MSG_SYNC_ROUTER_ID 19
109 /* Messages from OSPF daemon. */
111 #define MSG_READY_NOTIFY 11
112 #define MSG_LSA_UPDATE_NOTIFY 12
113 #define MSG_LSA_DELETE_NOTIFY 13
114 #define MSG_NEW_IF 14
115 #define MSG_DEL_IF 15
116 #define MSG_ISM_CHANGE 16
117 #define MSG_NSM_CHANGE 17
118 #define MSG_REACHABLE_CHANGE 18
119 #define MSG_ROUTER_ID_CHANGE 20
121 struct msg_register_opaque_type
{
124 uint8_t pad
[2]; /* padding */
127 struct msg_unregister_opaque_type
{
130 uint8_t pad
[2]; /* padding */
133 /* Power2 is needed to convert LSA types into bit positions,
134 * see typemask below. Type definition starts at 1, so
135 * Power2[0] is not used. */
138 static const uint16_t Power2
[] = {
139 0, (1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4),
140 (1 << 5), (1 << 6), (1 << 7), (1 << 8), (1 << 9), (1 << 10),
141 (1 << 11), (1 << 12), (1 << 13), (1 << 14), (1 << 15)};
143 struct lsa_filter_type
{
144 uint16_t typemask
; /* bitmask for selecting LSA types (1..16) */
145 uint8_t origin
; /* selects according to origin. */
146 #define NON_SELF_ORIGINATED 0
147 #define SELF_ORIGINATED (OSPF_LSA_SELF)
150 uint8_t num_areas
; /* number of areas in the filter. */
151 /* areas, if any, go here. */
154 struct msg_register_event
{
155 struct lsa_filter_type filter
;
158 struct msg_sync_lsdb
{
159 struct lsa_filter_type filter
;
162 struct msg_originate_request
{
163 /* Used for LSA type 9 otherwise ignored */
164 struct in_addr ifaddr
;
166 /* Used for LSA type 10 otherwise ignored */
167 struct in_addr area_id
;
169 /* LSA header and LSA-specific part */
170 struct lsa_header data
;
174 /* OSPF API MSG Delete Flag. */
175 #define OSPF_API_DEL_ZERO_LEN_LSA 0x01 /* send withdrawal with no LSA data */
177 #define IS_DEL_ZERO_LEN_LSA(x) ((x)->flags & OSPF_API_DEL_ZERO_LEN_LSA)
179 struct msg_delete_request
{
180 struct in_addr addr
; /* intf IP for link local, area for type 10,
181 "0.0.0.0" for AS-external */
184 uint8_t pad
; /* padding */
185 uint8_t flags
; /* delete flags */
191 #define OSPF_API_OK 0
192 #define OSPF_API_NOSUCHINTERFACE (-1)
193 #define OSPF_API_NOSUCHAREA (-2)
194 #define OSPF_API_NOSUCHLSA (-3)
195 #define OSPF_API_ILLEGALLSATYPE (-4)
196 #define OSPF_API_OPAQUETYPEINUSE (-5)
197 #define OSPF_API_OPAQUETYPENOTREGISTERED (-6)
198 #define OSPF_API_NOTREADY (-7)
199 #define OSPF_API_NOMEMORY (-8)
200 #define OSPF_API_ERROR (-9)
201 #define OSPF_API_UNDEF (-10)
202 uint8_t pad
[3]; /* padding to four byte alignment */
205 /* Message to tell client application that it ospf daemon is
206 * ready to accept opaque LSAs for a given interface or area. */
208 struct msg_ready_notify
{
211 uint8_t pad
[2]; /* padding */
212 struct in_addr addr
; /* interface address or area address */
215 /* These messages have a dynamic length depending on the embodied LSA.
216 They are aligned to four octets. msg_lsa_change_notify is used for
217 both LSA update and LSAs delete. */
219 struct msg_lsa_change_notify
{
220 /* Used for LSA type 9 otherwise ignored */
221 struct in_addr ifaddr
;
222 /* Area ID. Not valid for AS-External and Opaque11 LSAs. */
223 struct in_addr area_id
;
224 uint8_t is_self_originated
; /* 1 if self originated. */
226 struct lsa_header data
;
230 struct in_addr ifaddr
; /* interface IP address */
231 struct in_addr area_id
; /* area this interface belongs to */
235 struct in_addr ifaddr
; /* interface IP address */
238 struct msg_ism_change
{
239 struct in_addr ifaddr
; /* interface IP address */
240 struct in_addr area_id
; /* area this interface belongs to */
241 uint8_t status
; /* interface status (up/down) */
242 uint8_t pad
[3]; /* not used */
245 struct msg_nsm_change
{
246 struct in_addr ifaddr
; /* attached interface */
247 struct in_addr nbraddr
; /* Neighbor interface address */
248 struct in_addr router_id
; /* Router ID of neighbor */
249 uint8_t status
; /* NSM status */
253 struct msg_reachable_change
{
256 struct in_addr router_ids
[]; /* add followed by remove */
259 struct msg_router_id_change
{
260 struct in_addr router_id
; /* this systems router id */
263 /* We make use of a union to define a structure that covers all
264 possible API messages. This allows us to find out how much memory
265 needs to be reserved for the largest API message. */
267 struct apimsghdr hdr
;
269 struct msg_register_opaque_type register_opaque_type
;
270 struct msg_register_event register_event
;
271 struct msg_sync_lsdb sync_lsdb
;
272 struct msg_originate_request originate_request
;
273 struct msg_delete_request delete_request
;
274 struct msg_reply reply
;
275 struct msg_ready_notify ready_notify
;
276 struct msg_new_if new_if
;
277 struct msg_del_if del_if
;
278 struct msg_ism_change ism_change
;
279 struct msg_nsm_change nsm_change
;
280 struct msg_lsa_change_notify lsa_change_notify
;
281 struct msg_reachable_change reachable_change
;
282 struct msg_router_id_change router_id_change
;
286 #define OSPF_API_MAX_MSG_SIZE (sizeof(struct apimsg) + OSPF_MAX_PACKET_SIZE)
288 /* -----------------------------------------------------------
289 * Prototypes for specific messages
290 * -----------------------------------------------------------
293 /* For debugging only. */
294 extern void api_opaque_lsa_print(struct ospf_lsa
*lsa
);
296 /* Messages sent by client */
297 extern struct msg
*new_msg_register_opaque_type(uint32_t seqnum
, uint8_t ltype
,
299 extern struct msg
*new_msg_register_event(uint32_t seqnum
,
300 struct lsa_filter_type
*filter
);
301 extern struct msg
*new_msg_sync_lsdb(uint32_t seqnum
,
302 struct lsa_filter_type
*filter
);
303 extern struct msg
*new_msg_originate_request(uint32_t seqnum
,
304 struct in_addr ifaddr
,
305 struct in_addr area_id
,
306 struct lsa_header
*data
);
307 extern struct msg
*new_msg_delete_request(uint32_t seqnum
, struct in_addr addr
,
308 uint8_t lsa_type
, uint8_t opaque_type
,
309 uint32_t opaque_id
, uint8_t flags
);
311 /* Messages sent by OSPF daemon */
312 extern struct msg
*new_msg_reply(uint32_t seqnum
, uint8_t rc
);
314 extern struct msg
*new_msg_ready_notify(uint32_t seqnr
, uint8_t lsa_type
,
316 struct in_addr addr
);
318 extern struct msg
*new_msg_new_if(uint32_t seqnr
, struct in_addr ifaddr
,
319 struct in_addr area
);
321 extern struct msg
*new_msg_del_if(uint32_t seqnr
, struct in_addr ifaddr
);
323 extern struct msg
*new_msg_ism_change(uint32_t seqnr
, struct in_addr ifaddr
,
324 struct in_addr area
, uint8_t status
);
326 extern struct msg
*new_msg_nsm_change(uint32_t seqnr
, struct in_addr ifaddr
,
327 struct in_addr nbraddr
,
328 struct in_addr router_id
, uint8_t status
);
330 /* msgtype is MSG_LSA_UPDATE_NOTIFY or MSG_LSA_DELETE_NOTIFY */
331 extern struct msg
*new_msg_lsa_change_notify(uint8_t msgtype
, uint32_t seqnum
,
332 struct in_addr ifaddr
,
333 struct in_addr area_id
,
334 uint8_t is_self_originated
,
335 struct lsa_header
*data
);
337 extern struct msg
*new_msg_reachable_change(uint32_t seqnum
, uint16_t nadd
,
340 struct in_addr
*remove
);
342 extern struct msg
*new_msg_router_id_change(uint32_t seqnr
,
343 struct in_addr router_id
);
344 /* string printing functions */
345 extern const char *ospf_api_errname(int errcode
);
346 extern const char *ospf_api_typename(int msgtype
);
348 #endif /* _OSPF_API_H */