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.
10 #ifdef SUPPORT_OSPF_API
23 #include "sockunion.h" /* for inet_aton() */
27 #include "ospfd/ospfd.h"
28 #include "ospfd/ospf_interface.h"
29 #include "ospfd/ospf_ism.h"
30 #include "ospfd/ospf_asbr.h"
31 #include "ospfd/ospf_lsa.h"
32 #include "ospfd/ospf_lsdb.h"
33 #include "ospfd/ospf_neighbor.h"
34 #include "ospfd/ospf_nsm.h"
35 #include "ospfd/ospf_flood.h"
36 #include "ospfd/ospf_packet.h"
37 #include "ospfd/ospf_spf.h"
38 #include "ospfd/ospf_dump.h"
39 #include "ospfd/ospf_route.h"
40 #include "ospfd/ospf_ase.h"
41 #include "ospfd/ospf_zebra.h"
43 #include "ospfd/ospf_api.h"
46 /* For debugging only, will be removed */
47 void api_opaque_lsa_print(struct ospf_lsa
*lsa
)
50 struct lsa_header header
;
54 struct opaque_lsa
*olsa
;
58 ospf_lsa_header_dump(lsa
->data
);
60 olsa
= (struct opaque_lsa
*)lsa
->data
;
62 opaquelen
= lsa
->size
- OSPF_LSA_HEADER_SIZE
;
63 zlog_debug("apiserver_lsa_print: opaquelen=%d", opaquelen
);
65 for (i
= 0; i
< opaquelen
; i
++) {
66 zlog_debug("0x%x ", olsa
->mydata
[i
]);
71 /* -----------------------------------------------------------
73 * -----------------------------------------------------------
76 struct msg
*msg_new(uint8_t msgtype
, void *msgbody
, uint32_t seqnum
,
81 new = XCALLOC(MTYPE_OSPF_API_MSG
, sizeof(struct msg
));
83 new->hdr
.version
= OSPF_API_VERSION
;
84 new->hdr
.msgtype
= msgtype
;
85 new->hdr
.msglen
= htons(msglen
);
86 new->hdr
.msgseq
= htonl(seqnum
);
88 new->s
= stream_new(msglen
);
90 stream_put(new->s
, msgbody
, msglen
);
96 /* Duplicate a message by copying content. */
97 struct msg
*msg_dup(struct msg
*msg
)
104 size
= ntohs(msg
->hdr
.msglen
);
105 if (size
> OSPF_MAX_LSA_SIZE
)
108 new = msg_new(msg
->hdr
.msgtype
, STREAM_DATA(msg
->s
),
109 ntohl(msg
->hdr
.msgseq
), size
);
114 /* XXX only for testing, will be removed */
121 const char *ospf_api_typename(int msgtype
)
123 struct nametab NameTab
[] = {
125 MSG_REGISTER_OPAQUETYPE
, "Register opaque-type",
128 MSG_UNREGISTER_OPAQUETYPE
, "Unregister opaque-type",
131 MSG_REGISTER_EVENT
, "Register event",
134 MSG_SYNC_LSDB
, "Sync LSDB",
137 MSG_ORIGINATE_REQUEST
, "Originate request",
140 MSG_DELETE_REQUEST
, "Delete request",
146 MSG_READY_NOTIFY
, "Ready notify",
149 MSG_LSA_UPDATE_NOTIFY
, "LSA update notify",
152 MSG_LSA_DELETE_NOTIFY
, "LSA delete notify",
155 MSG_NEW_IF
, "New interface",
158 MSG_DEL_IF
, "Del interface",
161 MSG_ISM_CHANGE
, "ISM change",
164 MSG_NSM_CHANGE
, "NSM change",
167 MSG_REACHABLE_CHANGE
,
172 int i
, n
= array_size(NameTab
);
173 const char *name
= NULL
;
175 for (i
= 0; i
< n
; i
++) {
176 if (NameTab
[i
].value
== msgtype
) {
177 name
= NameTab
[i
].name
;
182 return name
? name
: "?";
185 const char *ospf_api_errname(int errcode
)
187 struct nametab NameTab
[] = {
192 OSPF_API_NOSUCHINTERFACE
, "No such interface",
195 OSPF_API_NOSUCHAREA
, "No such area",
198 OSPF_API_NOSUCHLSA
, "No such LSA",
201 OSPF_API_ILLEGALLSATYPE
, "Illegal LSA type",
204 OSPF_API_OPAQUETYPEINUSE
, "Opaque type in use",
207 OSPF_API_OPAQUETYPENOTREGISTERED
,
208 "Opaque type not registered",
211 OSPF_API_NOTREADY
, "Not ready",
214 OSPF_API_NOMEMORY
, "No memory",
217 OSPF_API_ERROR
, "Other error",
220 OSPF_API_UNDEF
, "Undefined",
224 int i
, n
= array_size(NameTab
);
225 const char *name
= NULL
;
227 for (i
= 0; i
< n
; i
++) {
228 if (NameTab
[i
].value
== errcode
) {
229 name
= NameTab
[i
].name
;
234 return name
? name
: "?";
237 void msg_print(struct msg
*msg
)
240 zlog_debug("msg_print msg=NULL!");
244 /* API message common header part. */
245 zlog_debug("API-msg [%s]: type(%d),len(%d),seq(%lu),data(%p),size(%zd)",
246 ospf_api_typename(msg
->hdr
.msgtype
), msg
->hdr
.msgtype
,
247 ntohs(msg
->hdr
.msglen
),
248 (unsigned long)ntohl(msg
->hdr
.msgseq
), STREAM_DATA(msg
->s
),
249 STREAM_SIZE(msg
->s
));
254 void msg_free(struct msg
*msg
)
259 XFREE(MTYPE_OSPF_API_MSG
, msg
);
263 /* Set sequence number of message */
264 void msg_set_seq(struct msg
*msg
, uint32_t seqnr
)
267 msg
->hdr
.msgseq
= htonl(seqnr
);
270 /* Get sequence number of message */
271 uint32_t msg_get_seq(struct msg
*msg
)
274 return ntohl(msg
->hdr
.msgseq
);
277 /* -----------------------------------------------------------
278 * Message fifo queues
279 * -----------------------------------------------------------
282 struct msg_fifo
*msg_fifo_new(void)
284 return XCALLOC(MTYPE_OSPF_API_FIFO
, sizeof(struct msg_fifo
));
287 /* Add new message to fifo. */
288 void msg_fifo_push(struct msg_fifo
*fifo
, struct msg
*msg
)
291 fifo
->tail
->next
= msg
;
300 /* Remove first message from fifo. */
301 struct msg
*msg_fifo_pop(struct msg_fifo
*fifo
)
307 fifo
->head
= msg
->next
;
309 if (fifo
->head
== NULL
)
317 /* Return first fifo entry but do not remove it. */
318 struct msg
*msg_fifo_head(struct msg_fifo
*fifo
)
323 /* Flush message fifo. */
324 void msg_fifo_flush(struct msg_fifo
*fifo
)
329 for (op
= fifo
->head
; op
; op
= next
) {
334 fifo
->head
= fifo
->tail
= NULL
;
338 /* Free API message fifo. */
339 void msg_fifo_free(struct msg_fifo
*fifo
)
341 msg_fifo_flush(fifo
);
343 XFREE(MTYPE_OSPF_API_FIFO
, fifo
);
346 struct msg
*msg_read(int fd
)
349 struct apimsghdr hdr
;
350 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
354 /* Read message header */
355 rlen
= readn(fd
, (uint8_t *)&hdr
, sizeof(struct apimsghdr
));
358 zlog_warn("msg_read: readn %s", safe_strerror(errno
));
360 } else if (rlen
== 0) {
361 zlog_warn("msg_read: Connection closed by peer");
363 } else if (rlen
!= sizeof(struct apimsghdr
)) {
364 zlog_warn("msg_read: Cannot read message header!");
368 /* Check version of API protocol */
369 if (hdr
.version
!= OSPF_API_VERSION
) {
370 zlog_warn("msg_read: OSPF API protocol version mismatch");
374 /* Determine body length. */
375 bodylen
= ntohs(hdr
.msglen
);
376 if (bodylen
> (ssize_t
)sizeof(buf
)) {
377 zlog_warn("%s: Body Length of message greater than what we can read",
383 /* Read message body */
384 rlen
= readn(fd
, buf
, bodylen
);
386 zlog_warn("msg_read: readn %s", safe_strerror(errno
));
388 } else if (rlen
== 0) {
389 zlog_warn("msg_read: Connection closed by peer");
391 } else if (rlen
!= bodylen
) {
392 zlog_warn("msg_read: Cannot read message body!");
397 /* Allocate new message */
398 msg
= msg_new(hdr
.msgtype
, buf
, ntohl(hdr
.msgseq
), bodylen
);
403 int msg_write(int fd
, struct msg
*msg
)
405 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
412 /* Length of OSPF LSA payload */
413 l
= ntohs(msg
->hdr
.msglen
);
414 if (l
> OSPF_MAX_LSA_SIZE
) {
415 zlog_warn("%s: wrong LSA size %d", __func__
, l
);
419 /* Make contiguous memory buffer for message */
420 memcpy(buf
, &msg
->hdr
, sizeof(struct apimsghdr
));
421 memcpy(buf
+ sizeof(struct apimsghdr
), STREAM_DATA(msg
->s
), l
);
423 /* Total length of OSPF API Message */
424 l
+= sizeof(struct apimsghdr
);
425 wlen
= writen(fd
, buf
, l
);
427 zlog_warn("%s: writen %s", __func__
, safe_strerror(errno
));
429 } else if (wlen
== 0) {
430 zlog_warn("%s: Connection closed by peer", __func__
);
432 } else if (wlen
!= l
) {
433 zlog_warn("%s: Cannot write API message", __func__
);
439 /* -----------------------------------------------------------
441 * -----------------------------------------------------------
444 struct msg
*new_msg_register_opaque_type(uint32_t seqnum
, uint8_t ltype
,
447 struct msg_register_opaque_type rmsg
;
449 rmsg
.lsatype
= ltype
;
450 rmsg
.opaquetype
= otype
;
451 memset(&rmsg
.pad
, 0, sizeof(rmsg
.pad
));
453 return msg_new(MSG_REGISTER_OPAQUETYPE
, &rmsg
, seqnum
,
454 sizeof(struct msg_register_opaque_type
));
457 struct msg
*new_msg_register_event(uint32_t seqnum
,
458 struct lsa_filter_type
*filter
)
460 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
461 struct msg_register_event
*emsg
;
464 emsg
= (struct msg_register_event
*)buf
;
465 len
= sizeof(struct msg_register_event
)
466 + filter
->num_areas
* sizeof(struct in_addr
);
467 emsg
->filter
.typemask
= htons(filter
->typemask
);
468 emsg
->filter
.origin
= filter
->origin
;
469 emsg
->filter
.num_areas
= filter
->num_areas
;
470 if (len
> sizeof(buf
))
472 /* API broken - missing memcpy to fill data */
473 return msg_new(MSG_REGISTER_EVENT
, emsg
, seqnum
, len
);
476 struct msg
*new_msg_sync_lsdb(uint32_t seqnum
, struct lsa_filter_type
*filter
)
478 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
479 struct msg_sync_lsdb
*smsg
;
482 smsg
= (struct msg_sync_lsdb
*)buf
;
483 len
= sizeof(struct msg_sync_lsdb
)
484 + filter
->num_areas
* sizeof(struct in_addr
);
485 smsg
->filter
.typemask
= htons(filter
->typemask
);
486 smsg
->filter
.origin
= filter
->origin
;
487 smsg
->filter
.num_areas
= filter
->num_areas
;
488 if (len
> sizeof(buf
))
490 /* API broken - missing memcpy to fill data */
491 return msg_new(MSG_SYNC_LSDB
, smsg
, seqnum
, len
);
495 struct msg
*new_msg_originate_request(uint32_t seqnum
, struct in_addr ifaddr
,
496 struct in_addr area_id
,
497 struct lsa_header
*data
)
499 struct msg_originate_request
*omsg
;
500 unsigned int omsglen
;
501 char buf
[OSPF_API_MAX_MSG_SIZE
];
502 size_t off_data
= offsetof(struct msg_originate_request
, data
);
503 size_t data_maxs
= sizeof(buf
) - off_data
;
504 struct lsa_header
*omsg_data
= (struct lsa_header
*)&buf
[off_data
];
506 omsg
= (struct msg_originate_request
*)buf
;
507 omsg
->ifaddr
= ifaddr
;
508 omsg
->area_id
= area_id
;
510 omsglen
= ntohs(data
->length
);
511 if (omsglen
> data_maxs
)
513 memcpy(omsg_data
, data
, omsglen
);
514 omsglen
+= sizeof(struct msg_originate_request
)
515 - sizeof(struct lsa_header
);
517 return msg_new(MSG_ORIGINATE_REQUEST
, omsg
, seqnum
, omsglen
);
520 struct msg
*new_msg_delete_request(uint32_t seqnum
, struct in_addr addr
,
521 uint8_t lsa_type
, uint8_t opaque_type
,
522 uint32_t opaque_id
, uint8_t flags
)
524 struct msg_delete_request dmsg
;
526 dmsg
.lsa_type
= lsa_type
;
527 dmsg
.opaque_type
= opaque_type
;
528 dmsg
.opaque_id
= htonl(opaque_id
);
529 memset(&dmsg
.pad
, 0, sizeof(dmsg
.pad
));
532 return msg_new(MSG_DELETE_REQUEST
, &dmsg
, seqnum
,
533 sizeof(struct msg_delete_request
));
537 struct msg
*new_msg_reply(uint32_t seqnr
, uint8_t rc
)
540 struct msg_reply rmsg
;
542 /* Set return code */
544 memset(&rmsg
.pad
, 0, sizeof(rmsg
.pad
));
546 msg
= msg_new(MSG_REPLY
, &rmsg
, seqnr
, sizeof(struct msg_reply
));
551 struct msg
*new_msg_ready_notify(uint32_t seqnr
, uint8_t lsa_type
,
552 uint8_t opaque_type
, struct in_addr addr
)
554 struct msg_ready_notify rmsg
;
556 rmsg
.lsa_type
= lsa_type
;
557 rmsg
.opaque_type
= opaque_type
;
558 memset(&rmsg
.pad
, 0, sizeof(rmsg
.pad
));
561 return msg_new(MSG_READY_NOTIFY
, &rmsg
, seqnr
,
562 sizeof(struct msg_ready_notify
));
565 struct msg
*new_msg_new_if(uint32_t seqnr
, struct in_addr ifaddr
,
566 struct in_addr area_id
)
568 struct msg_new_if nmsg
;
570 nmsg
.ifaddr
= ifaddr
;
571 nmsg
.area_id
= area_id
;
573 return msg_new(MSG_NEW_IF
, &nmsg
, seqnr
, sizeof(struct msg_new_if
));
576 struct msg
*new_msg_del_if(uint32_t seqnr
, struct in_addr ifaddr
)
578 struct msg_del_if dmsg
;
580 dmsg
.ifaddr
= ifaddr
;
582 return msg_new(MSG_DEL_IF
, &dmsg
, seqnr
, sizeof(struct msg_del_if
));
585 struct msg
*new_msg_ism_change(uint32_t seqnr
, struct in_addr ifaddr
,
586 struct in_addr area_id
, uint8_t status
)
588 struct msg_ism_change imsg
;
590 imsg
.ifaddr
= ifaddr
;
591 imsg
.area_id
= area_id
;
592 imsg
.status
= status
;
593 memset(&imsg
.pad
, 0, sizeof(imsg
.pad
));
595 return msg_new(MSG_ISM_CHANGE
, &imsg
, seqnr
,
596 sizeof(struct msg_ism_change
));
599 struct msg
*new_msg_nsm_change(uint32_t seqnr
, struct in_addr ifaddr
,
600 struct in_addr nbraddr
, struct in_addr router_id
,
603 struct msg_nsm_change nmsg
;
605 nmsg
.ifaddr
= ifaddr
;
606 nmsg
.nbraddr
= nbraddr
;
607 nmsg
.router_id
= router_id
;
608 nmsg
.status
= status
;
609 memset(&nmsg
.pad
, 0, sizeof(nmsg
.pad
));
611 return msg_new(MSG_NSM_CHANGE
, &nmsg
, seqnr
,
612 sizeof(struct msg_nsm_change
));
615 struct msg
*new_msg_lsa_change_notify(uint8_t msgtype
, uint32_t seqnum
,
616 struct in_addr ifaddr
,
617 struct in_addr area_id
,
618 uint8_t is_self_originated
,
619 struct lsa_header
*data
)
621 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
622 struct msg_lsa_change_notify
*nmsg
;
624 size_t off_data
= offsetof(struct msg_lsa_change_notify
, data
);
625 size_t data_maxs
= sizeof(buf
) - off_data
;
626 struct lsa_header
*nmsg_data
= (struct lsa_header
*)&buf
[off_data
];
630 nmsg
= (struct msg_lsa_change_notify
*)buf
;
631 nmsg
->ifaddr
= ifaddr
;
632 nmsg
->area_id
= area_id
;
633 nmsg
->is_self_originated
= is_self_originated
;
634 memset(&nmsg
->pad
, 0, sizeof(nmsg
->pad
));
636 len
= ntohs(data
->length
);
639 memcpy(nmsg_data
, data
, len
);
640 len
+= sizeof(struct msg_lsa_change_notify
) - sizeof(struct lsa_header
);
642 return msg_new(msgtype
, nmsg
, seqnum
, len
);
645 struct msg
*new_msg_reachable_change(uint32_t seqnum
, uint16_t nadd
,
646 struct in_addr
*add
, uint16_t nremove
,
647 struct in_addr
*remove
)
649 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
650 struct msg_reachable_change
*nmsg
= (void *)buf
;
651 const uint insz
= sizeof(*nmsg
->router_ids
);
652 const uint nmax
= (sizeof(buf
) - sizeof(*nmsg
)) / insz
;
657 if (nremove
> (nmax
- nadd
))
658 nremove
= (nmax
- nadd
);
661 memcpy(nmsg
->router_ids
, add
, nadd
* insz
);
663 memcpy(&nmsg
->router_ids
[nadd
], remove
, nremove
* insz
);
665 nmsg
->nadd
= htons(nadd
);
666 nmsg
->nremove
= htons(nremove
);
667 len
= sizeof(*nmsg
) + insz
* (nadd
+ nremove
);
669 return msg_new(MSG_REACHABLE_CHANGE
, nmsg
, seqnum
, len
);
672 struct msg
*new_msg_router_id_change(uint32_t seqnum
, struct in_addr router_id
)
674 struct msg_router_id_change rmsg
= {.router_id
= router_id
};
676 return msg_new(MSG_ROUTER_ID_CHANGE
, &rmsg
, seqnum
,
677 sizeof(struct msg_router_id_change
));
680 #endif /* SUPPORT_OSPF_API */