2 * API message handling module for OSPF daemon and client.
3 * Copyright (C) 2001, 2002 Ralph Keller
5 * This file is part of GNU Zebra.
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.
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.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #ifdef SUPPORT_OSPF_API
37 #include "sockunion.h" /* for inet_aton() */
41 #include "ospfd/ospfd.h"
42 #include "ospfd/ospf_interface.h"
43 #include "ospfd/ospf_ism.h"
44 #include "ospfd/ospf_asbr.h"
45 #include "ospfd/ospf_lsa.h"
46 #include "ospfd/ospf_lsdb.h"
47 #include "ospfd/ospf_neighbor.h"
48 #include "ospfd/ospf_nsm.h"
49 #include "ospfd/ospf_flood.h"
50 #include "ospfd/ospf_packet.h"
51 #include "ospfd/ospf_spf.h"
52 #include "ospfd/ospf_dump.h"
53 #include "ospfd/ospf_route.h"
54 #include "ospfd/ospf_ase.h"
55 #include "ospfd/ospf_zebra.h"
57 #include "ospfd/ospf_api.h"
60 /* For debugging only, will be removed */
61 void api_opaque_lsa_print(struct ospf_lsa
*lsa
)
64 struct lsa_header header
;
68 struct opaque_lsa
*olsa
;
72 ospf_lsa_header_dump(lsa
->data
);
74 olsa
= (struct opaque_lsa
*)lsa
->data
;
76 opaquelen
= lsa
->size
- OSPF_LSA_HEADER_SIZE
;
77 zlog_debug("apiserver_lsa_print: opaquelen=%d", opaquelen
);
79 for (i
= 0; i
< opaquelen
; i
++) {
80 zlog_debug("0x%x ", olsa
->mydata
[i
]);
85 /* -----------------------------------------------------------
87 * -----------------------------------------------------------
90 struct msg
*msg_new(uint8_t msgtype
, void *msgbody
, uint32_t seqnum
,
95 new = XCALLOC(MTYPE_OSPF_API_MSG
, sizeof(struct msg
));
97 new->hdr
.version
= OSPF_API_VERSION
;
98 new->hdr
.msgtype
= msgtype
;
99 new->hdr
.msglen
= htons(msglen
);
100 new->hdr
.msgseq
= htonl(seqnum
);
102 new->s
= stream_new(msglen
);
104 stream_put(new->s
, msgbody
, msglen
);
110 /* Duplicate a message by copying content. */
111 struct msg
*msg_dup(struct msg
*msg
)
118 size
= ntohs(msg
->hdr
.msglen
);
119 if (size
> OSPF_MAX_LSA_SIZE
)
122 new = msg_new(msg
->hdr
.msgtype
, STREAM_DATA(msg
->s
),
123 ntohl(msg
->hdr
.msgseq
), size
);
128 /* XXX only for testing, will be removed */
135 const char *ospf_api_typename(int msgtype
)
137 struct nametab NameTab
[] = {
139 MSG_REGISTER_OPAQUETYPE
, "Register opaque-type",
142 MSG_UNREGISTER_OPAQUETYPE
, "Unregister opaque-type",
145 MSG_REGISTER_EVENT
, "Register event",
148 MSG_SYNC_LSDB
, "Sync LSDB",
151 MSG_ORIGINATE_REQUEST
, "Originate request",
154 MSG_DELETE_REQUEST
, "Delete request",
160 MSG_READY_NOTIFY
, "Ready notify",
163 MSG_LSA_UPDATE_NOTIFY
, "LSA update notify",
166 MSG_LSA_DELETE_NOTIFY
, "LSA delete notify",
169 MSG_NEW_IF
, "New interface",
172 MSG_DEL_IF
, "Del interface",
175 MSG_ISM_CHANGE
, "ISM change",
178 MSG_NSM_CHANGE
, "NSM change",
182 int i
, n
= array_size(NameTab
);
183 const char *name
= NULL
;
185 for (i
= 0; i
< n
; i
++) {
186 if (NameTab
[i
].value
== msgtype
) {
187 name
= NameTab
[i
].name
;
192 return name
? name
: "?";
195 const char *ospf_api_errname(int errcode
)
197 struct nametab NameTab
[] = {
202 OSPF_API_NOSUCHINTERFACE
, "No such interface",
205 OSPF_API_NOSUCHAREA
, "No such area",
208 OSPF_API_NOSUCHLSA
, "No such LSA",
211 OSPF_API_ILLEGALLSATYPE
, "Illegal LSA type",
214 OSPF_API_OPAQUETYPEINUSE
, "Opaque type in use",
217 OSPF_API_OPAQUETYPENOTREGISTERED
,
218 "Opaque type not registered",
221 OSPF_API_NOTREADY
, "Not ready",
224 OSPF_API_NOMEMORY
, "No memory",
227 OSPF_API_ERROR
, "Other error",
230 OSPF_API_UNDEF
, "Undefined",
234 int i
, n
= array_size(NameTab
);
235 const char *name
= NULL
;
237 for (i
= 0; i
< n
; i
++) {
238 if (NameTab
[i
].value
== errcode
) {
239 name
= NameTab
[i
].name
;
244 return name
? name
: "?";
247 void msg_print(struct msg
*msg
)
250 zlog_debug("msg_print msg=NULL!");
254 /* API message common header part. */
255 zlog_debug("API-msg [%s]: type(%d),len(%d),seq(%lu),data(%p),size(%zd)",
256 ospf_api_typename(msg
->hdr
.msgtype
), msg
->hdr
.msgtype
,
257 ntohs(msg
->hdr
.msglen
),
258 (unsigned long)ntohl(msg
->hdr
.msgseq
), STREAM_DATA(msg
->s
),
259 STREAM_SIZE(msg
->s
));
264 void msg_free(struct msg
*msg
)
269 XFREE(MTYPE_OSPF_API_MSG
, msg
);
273 /* Set sequence number of message */
274 void msg_set_seq(struct msg
*msg
, uint32_t seqnr
)
277 msg
->hdr
.msgseq
= htonl(seqnr
);
280 /* Get sequence number of message */
281 uint32_t msg_get_seq(struct msg
*msg
)
284 return ntohl(msg
->hdr
.msgseq
);
287 /* -----------------------------------------------------------
288 * Message fifo queues
289 * -----------------------------------------------------------
292 struct msg_fifo
*msg_fifo_new(void)
294 return XCALLOC(MTYPE_OSPF_API_FIFO
, sizeof(struct msg_fifo
));
297 /* Add new message to fifo. */
298 void msg_fifo_push(struct msg_fifo
*fifo
, struct msg
*msg
)
301 fifo
->tail
->next
= msg
;
310 /* Remove first message from fifo. */
311 struct msg
*msg_fifo_pop(struct msg_fifo
*fifo
)
317 fifo
->head
= msg
->next
;
319 if (fifo
->head
== NULL
)
327 /* Return first fifo entry but do not remove it. */
328 struct msg
*msg_fifo_head(struct msg_fifo
*fifo
)
333 /* Flush message fifo. */
334 void msg_fifo_flush(struct msg_fifo
*fifo
)
339 for (op
= fifo
->head
; op
; op
= next
) {
344 fifo
->head
= fifo
->tail
= NULL
;
348 /* Free API message fifo. */
349 void msg_fifo_free(struct msg_fifo
*fifo
)
351 msg_fifo_flush(fifo
);
353 XFREE(MTYPE_OSPF_API_FIFO
, fifo
);
356 struct msg
*msg_read(int fd
)
359 struct apimsghdr hdr
;
360 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
364 /* Read message header */
365 rlen
= readn(fd
, (uint8_t *)&hdr
, sizeof(struct apimsghdr
));
368 zlog_warn("msg_read: readn %s", safe_strerror(errno
));
370 } else if (rlen
== 0) {
371 zlog_warn("msg_read: Connection closed by peer");
373 } else if (rlen
!= sizeof(struct apimsghdr
)) {
374 zlog_warn("msg_read: Cannot read message header!");
378 /* Check version of API protocol */
379 if (hdr
.version
!= OSPF_API_VERSION
) {
380 zlog_warn("msg_read: OSPF API protocol version mismatch");
384 /* Determine body length. */
385 bodylen
= ntohs(hdr
.msglen
);
386 if (bodylen
> (ssize_t
)sizeof(buf
)) {
387 zlog_warn("%s: Body Length of message greater than what we can read",
393 /* Read message body */
394 rlen
= readn(fd
, buf
, bodylen
);
396 zlog_warn("msg_read: readn %s", safe_strerror(errno
));
398 } else if (rlen
== 0) {
399 zlog_warn("msg_read: Connection closed by peer");
401 } else if (rlen
!= bodylen
) {
402 zlog_warn("msg_read: Cannot read message body!");
407 /* Allocate new message */
408 msg
= msg_new(hdr
.msgtype
, buf
, ntohl(hdr
.msgseq
), bodylen
);
413 int msg_write(int fd
, struct msg
*msg
)
415 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
422 /* Length of OSPF LSA payload */
423 l
= ntohs(msg
->hdr
.msglen
);
424 if (l
> OSPF_MAX_LSA_SIZE
) {
425 zlog_warn("%s: wrong LSA size %d", __func__
, l
);
429 /* Make contiguous memory buffer for message */
430 memcpy(buf
, &msg
->hdr
, sizeof(struct apimsghdr
));
431 memcpy(buf
+ sizeof(struct apimsghdr
), STREAM_DATA(msg
->s
), l
);
433 /* Total length of OSPF API Message */
434 l
+= sizeof(struct apimsghdr
);
435 wlen
= writen(fd
, buf
, l
);
437 zlog_warn("%s: writen %s", __func__
, safe_strerror(errno
));
439 } else if (wlen
== 0) {
440 zlog_warn("%s: Connection closed by peer", __func__
);
442 } else if (wlen
!= l
) {
443 zlog_warn("%s: Cannot write API message", __func__
);
449 /* -----------------------------------------------------------
451 * -----------------------------------------------------------
454 struct msg
*new_msg_register_opaque_type(uint32_t seqnum
, uint8_t ltype
,
457 struct msg_register_opaque_type rmsg
;
459 rmsg
.lsatype
= ltype
;
460 rmsg
.opaquetype
= otype
;
461 memset(&rmsg
.pad
, 0, sizeof(rmsg
.pad
));
463 return msg_new(MSG_REGISTER_OPAQUETYPE
, &rmsg
, seqnum
,
464 sizeof(struct msg_register_opaque_type
));
467 struct msg
*new_msg_register_event(uint32_t seqnum
,
468 struct lsa_filter_type
*filter
)
470 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
471 struct msg_register_event
*emsg
;
474 emsg
= (struct msg_register_event
*)buf
;
475 len
= sizeof(struct msg_register_event
)
476 + filter
->num_areas
* sizeof(struct in_addr
);
477 emsg
->filter
.typemask
= htons(filter
->typemask
);
478 emsg
->filter
.origin
= filter
->origin
;
479 emsg
->filter
.num_areas
= filter
->num_areas
;
480 if (len
> sizeof(buf
))
482 /* API broken - missing memcpy to fill data */
483 return msg_new(MSG_REGISTER_EVENT
, emsg
, seqnum
, len
);
486 struct msg
*new_msg_sync_lsdb(uint32_t seqnum
, struct lsa_filter_type
*filter
)
488 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
489 struct msg_sync_lsdb
*smsg
;
492 smsg
= (struct msg_sync_lsdb
*)buf
;
493 len
= sizeof(struct msg_sync_lsdb
)
494 + filter
->num_areas
* sizeof(struct in_addr
);
495 smsg
->filter
.typemask
= htons(filter
->typemask
);
496 smsg
->filter
.origin
= filter
->origin
;
497 smsg
->filter
.num_areas
= filter
->num_areas
;
498 if (len
> sizeof(buf
))
500 /* API broken - missing memcpy to fill data */
501 return msg_new(MSG_SYNC_LSDB
, smsg
, seqnum
, len
);
505 struct msg
*new_msg_originate_request(uint32_t seqnum
, struct in_addr ifaddr
,
506 struct in_addr area_id
,
507 struct lsa_header
*data
)
509 struct msg_originate_request
*omsg
;
510 unsigned int omsglen
;
511 char buf
[OSPF_API_MAX_MSG_SIZE
];
512 size_t off_data
= offsetof(struct msg_originate_request
, data
);
513 size_t data_maxs
= sizeof(buf
) - off_data
;
514 struct lsa_header
*omsg_data
= (struct lsa_header
*)&buf
[off_data
];
516 omsg
= (struct msg_originate_request
*)buf
;
517 omsg
->ifaddr
= ifaddr
;
518 omsg
->area_id
= area_id
;
520 omsglen
= ntohs(data
->length
);
521 if (omsglen
> data_maxs
)
523 memcpy(omsg_data
, data
, omsglen
);
524 omsglen
+= sizeof(struct msg_originate_request
)
525 - sizeof(struct lsa_header
);
527 return msg_new(MSG_ORIGINATE_REQUEST
, omsg
, seqnum
, omsglen
);
530 struct msg
*new_msg_delete_request(uint32_t seqnum
, struct in_addr area_id
,
531 uint8_t lsa_type
, uint8_t opaque_type
,
534 struct msg_delete_request dmsg
;
535 dmsg
.area_id
= area_id
;
536 dmsg
.lsa_type
= lsa_type
;
537 dmsg
.opaque_type
= opaque_type
;
538 dmsg
.opaque_id
= htonl(opaque_id
);
539 memset(&dmsg
.pad
, 0, sizeof(dmsg
.pad
));
541 return msg_new(MSG_DELETE_REQUEST
, &dmsg
, seqnum
,
542 sizeof(struct msg_delete_request
));
546 struct msg
*new_msg_reply(uint32_t seqnr
, uint8_t rc
)
549 struct msg_reply rmsg
;
551 /* Set return code */
553 memset(&rmsg
.pad
, 0, sizeof(rmsg
.pad
));
555 msg
= msg_new(MSG_REPLY
, &rmsg
, seqnr
, sizeof(struct msg_reply
));
560 struct msg
*new_msg_ready_notify(uint32_t seqnr
, uint8_t lsa_type
,
561 uint8_t opaque_type
, struct in_addr addr
)
563 struct msg_ready_notify rmsg
;
565 rmsg
.lsa_type
= lsa_type
;
566 rmsg
.opaque_type
= opaque_type
;
567 memset(&rmsg
.pad
, 0, sizeof(rmsg
.pad
));
570 return msg_new(MSG_READY_NOTIFY
, &rmsg
, seqnr
,
571 sizeof(struct msg_ready_notify
));
574 struct msg
*new_msg_new_if(uint32_t seqnr
, struct in_addr ifaddr
,
575 struct in_addr area_id
)
577 struct msg_new_if nmsg
;
579 nmsg
.ifaddr
= ifaddr
;
580 nmsg
.area_id
= area_id
;
582 return msg_new(MSG_NEW_IF
, &nmsg
, seqnr
, sizeof(struct msg_new_if
));
585 struct msg
*new_msg_del_if(uint32_t seqnr
, struct in_addr ifaddr
)
587 struct msg_del_if dmsg
;
589 dmsg
.ifaddr
= ifaddr
;
591 return msg_new(MSG_DEL_IF
, &dmsg
, seqnr
, sizeof(struct msg_del_if
));
594 struct msg
*new_msg_ism_change(uint32_t seqnr
, struct in_addr ifaddr
,
595 struct in_addr area_id
, uint8_t status
)
597 struct msg_ism_change imsg
;
599 imsg
.ifaddr
= ifaddr
;
600 imsg
.area_id
= area_id
;
601 imsg
.status
= status
;
602 memset(&imsg
.pad
, 0, sizeof(imsg
.pad
));
604 return msg_new(MSG_ISM_CHANGE
, &imsg
, seqnr
,
605 sizeof(struct msg_ism_change
));
608 struct msg
*new_msg_nsm_change(uint32_t seqnr
, struct in_addr ifaddr
,
609 struct in_addr nbraddr
, struct in_addr router_id
,
612 struct msg_nsm_change nmsg
;
614 nmsg
.ifaddr
= ifaddr
;
615 nmsg
.nbraddr
= nbraddr
;
616 nmsg
.router_id
= router_id
;
617 nmsg
.status
= status
;
618 memset(&nmsg
.pad
, 0, sizeof(nmsg
.pad
));
620 return msg_new(MSG_NSM_CHANGE
, &nmsg
, seqnr
,
621 sizeof(struct msg_nsm_change
));
624 struct msg
*new_msg_lsa_change_notify(uint8_t msgtype
, uint32_t seqnum
,
625 struct in_addr ifaddr
,
626 struct in_addr area_id
,
627 uint8_t is_self_originated
,
628 struct lsa_header
*data
)
630 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
631 struct msg_lsa_change_notify
*nmsg
;
633 size_t off_data
= offsetof(struct msg_lsa_change_notify
, data
);
634 size_t data_maxs
= sizeof(buf
) - off_data
;
635 struct lsa_header
*nmsg_data
= (struct lsa_header
*)&buf
[off_data
];
639 nmsg
= (struct msg_lsa_change_notify
*)buf
;
640 nmsg
->ifaddr
= ifaddr
;
641 nmsg
->area_id
= area_id
;
642 nmsg
->is_self_originated
= is_self_originated
;
643 memset(&nmsg
->pad
, 0, sizeof(nmsg
->pad
));
645 len
= ntohs(data
->length
);
648 memcpy(nmsg_data
, data
, len
);
649 len
+= sizeof(struct msg_lsa_change_notify
) - sizeof(struct lsa_header
);
651 return msg_new(msgtype
, nmsg
, seqnum
, len
);
654 #endif /* SUPPORT_OSPF_API */