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 lsa_header
*data
)
64 struct lsa_header header
;
68 struct opaque_lsa
*olsa
;
72 ospf_lsa_header_dump(data
);
74 olsa
= (struct opaque_lsa
*)data
;
76 opaquelen
= ntohs(data
->length
) - 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
)
117 new = msg_new(msg
->hdr
.msgtype
, STREAM_DATA(msg
->s
),
118 ntohl(msg
->hdr
.msgseq
), ntohs(msg
->hdr
.msglen
));
123 /* XXX only for testing, will be removed */
130 const char *ospf_api_typename(int msgtype
)
132 struct nametab NameTab
[] = {
134 MSG_REGISTER_OPAQUETYPE
, "Register opaque-type",
137 MSG_UNREGISTER_OPAQUETYPE
, "Unregister opaque-type",
140 MSG_REGISTER_EVENT
, "Register event",
143 MSG_SYNC_LSDB
, "Sync LSDB",
146 MSG_ORIGINATE_REQUEST
, "Originate request",
149 MSG_DELETE_REQUEST
, "Delete request",
155 MSG_READY_NOTIFY
, "Ready notify",
158 MSG_LSA_UPDATE_NOTIFY
, "LSA update notify",
161 MSG_LSA_DELETE_NOTIFY
, "LSA delete notify",
164 MSG_NEW_IF
, "New interface",
167 MSG_DEL_IF
, "Del interface",
170 MSG_ISM_CHANGE
, "ISM change",
173 MSG_NSM_CHANGE
, "NSM change",
177 int i
, n
= array_size(NameTab
);
178 const char *name
= NULL
;
180 for (i
= 0; i
< n
; i
++) {
181 if (NameTab
[i
].value
== msgtype
) {
182 name
= NameTab
[i
].name
;
187 return name
? name
: "?";
190 const char *ospf_api_errname(int errcode
)
192 struct nametab NameTab
[] = {
197 OSPF_API_NOSUCHINTERFACE
, "No such interface",
200 OSPF_API_NOSUCHAREA
, "No such area",
203 OSPF_API_NOSUCHLSA
, "No such LSA",
206 OSPF_API_ILLEGALLSATYPE
, "Illegal LSA type",
209 OSPF_API_OPAQUETYPEINUSE
, "Opaque type in use",
212 OSPF_API_OPAQUETYPENOTREGISTERED
,
213 "Opaque type not registered",
216 OSPF_API_NOTREADY
, "Not ready",
219 OSPF_API_NOMEMORY
, "No memory",
222 OSPF_API_ERROR
, "Other error",
225 OSPF_API_UNDEF
, "Undefined",
229 int i
, n
= array_size(NameTab
);
230 const char *name
= NULL
;
232 for (i
= 0; i
< n
; i
++) {
233 if (NameTab
[i
].value
== errcode
) {
234 name
= NameTab
[i
].name
;
239 return name
? name
: "?";
242 void msg_print(struct msg
*msg
)
245 zlog_debug("msg_print msg=NULL!");
249 #ifdef ORIGINAL_CODING
251 "msg=%p msgtype=%d msglen=%d msgseq=%d streamdata=%p streamsize=%lu\n",
252 msg
, msg
->hdr
.msgtype
, ntohs(msg
->hdr
.msglen
),
253 ntohl(msg
->hdr
.msgseq
), STREAM_DATA(msg
->s
),
254 STREAM_SIZE(msg
->s
));
255 #else /* ORIGINAL_CODING */
256 /* API message common header part. */
257 zlog_debug("API-msg [%s]: type(%d),len(%d),seq(%lu),data(%p),size(%zd)",
258 ospf_api_typename(msg
->hdr
.msgtype
), msg
->hdr
.msgtype
,
259 ntohs(msg
->hdr
.msglen
),
260 (unsigned long)ntohl(msg
->hdr
.msgseq
), STREAM_DATA(msg
->s
),
261 STREAM_SIZE(msg
->s
));
263 /* API message body part. */
265 /* Generic Hex/Ascii dump */
266 DumpBuf(STREAM_DATA(msg
->s
), STREAM_SIZE(msg
->s
)); /* Sorry, deleted! */
268 /* Message-type dependent dump function. */
272 #endif /* ORIGINAL_CODING */
275 void msg_free(struct msg
*msg
)
280 XFREE(MTYPE_OSPF_API_MSG
, msg
);
284 /* Set sequence number of message */
285 void msg_set_seq(struct msg
*msg
, uint32_t seqnr
)
288 msg
->hdr
.msgseq
= htonl(seqnr
);
291 /* Get sequence number of message */
292 uint32_t msg_get_seq(struct msg
*msg
)
295 return ntohl(msg
->hdr
.msgseq
);
298 /* -----------------------------------------------------------
299 * Message fifo queues
300 * -----------------------------------------------------------
303 struct msg_fifo
*msg_fifo_new(void)
305 return XCALLOC(MTYPE_OSPF_API_FIFO
, sizeof(struct msg_fifo
));
308 /* Add new message to fifo. */
309 void msg_fifo_push(struct msg_fifo
*fifo
, struct msg
*msg
)
312 fifo
->tail
->next
= msg
;
321 /* Remove first message from fifo. */
322 struct msg
*msg_fifo_pop(struct msg_fifo
*fifo
)
328 fifo
->head
= msg
->next
;
330 if (fifo
->head
== NULL
)
338 /* Return first fifo entry but do not remove it. */
339 struct msg
*msg_fifo_head(struct msg_fifo
*fifo
)
344 /* Flush message fifo. */
345 void msg_fifo_flush(struct msg_fifo
*fifo
)
350 for (op
= fifo
->head
; op
; op
= next
) {
355 fifo
->head
= fifo
->tail
= NULL
;
359 /* Free API message fifo. */
360 void msg_fifo_free(struct msg_fifo
*fifo
)
362 msg_fifo_flush(fifo
);
364 XFREE(MTYPE_OSPF_API_FIFO
, fifo
);
367 struct msg
*msg_read(int fd
)
370 struct apimsghdr hdr
;
371 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
375 /* Read message header */
376 rlen
= readn(fd
, (uint8_t *)&hdr
, sizeof(struct apimsghdr
));
379 zlog_warn("msg_read: readn %s", safe_strerror(errno
));
381 } else if (rlen
== 0) {
382 zlog_warn("msg_read: Connection closed by peer");
384 } else if (rlen
!= sizeof(struct apimsghdr
)) {
385 zlog_warn("msg_read: Cannot read message header!");
389 /* Check version of API protocol */
390 if (hdr
.version
!= OSPF_API_VERSION
) {
391 zlog_warn("msg_read: OSPF API protocol version mismatch");
395 /* Determine body length. */
396 bodylen
= ntohs(hdr
.msglen
);
399 /* Read message body */
400 rlen
= readn(fd
, buf
, bodylen
);
402 zlog_warn("msg_read: readn %s", safe_strerror(errno
));
404 } else if (rlen
== 0) {
405 zlog_warn("msg_read: Connection closed by peer");
407 } else if (rlen
!= bodylen
) {
408 zlog_warn("msg_read: Cannot read message body!");
413 /* Allocate new message */
414 msg
= msg_new(hdr
.msgtype
, buf
, ntohl(hdr
.msgseq
), ntohs(hdr
.msglen
));
419 int msg_write(int fd
, struct msg
*msg
)
421 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
428 /* Length of message including header */
429 l
= sizeof(struct apimsghdr
) + ntohs(msg
->hdr
.msglen
);
431 /* Make contiguous memory buffer for message */
432 memcpy(buf
, &msg
->hdr
, sizeof(struct apimsghdr
));
433 memcpy(buf
+ sizeof(struct apimsghdr
), STREAM_DATA(msg
->s
),
434 ntohs(msg
->hdr
.msglen
));
436 wlen
= writen(fd
, buf
, l
);
438 zlog_warn("msg_write: writen %s", safe_strerror(errno
));
440 } else if (wlen
== 0) {
441 zlog_warn("msg_write: Connection closed by peer");
443 } else if (wlen
!= l
) {
444 zlog_warn("msg_write: Cannot write API message");
450 /* -----------------------------------------------------------
452 * -----------------------------------------------------------
455 struct msg
*new_msg_register_opaque_type(uint32_t seqnum
, uint8_t ltype
,
458 struct msg_register_opaque_type rmsg
;
460 rmsg
.lsatype
= ltype
;
461 rmsg
.opaquetype
= otype
;
462 memset(&rmsg
.pad
, 0, sizeof(rmsg
.pad
));
464 return msg_new(MSG_REGISTER_OPAQUETYPE
, &rmsg
, seqnum
,
465 sizeof(struct msg_register_opaque_type
));
468 struct msg
*new_msg_register_event(uint32_t seqnum
,
469 struct lsa_filter_type
*filter
)
471 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
472 struct msg_register_event
*emsg
;
475 emsg
= (struct msg_register_event
*)buf
;
476 len
= sizeof(struct msg_register_event
)
477 + filter
->num_areas
* sizeof(struct in_addr
);
478 emsg
->filter
.typemask
= htons(filter
->typemask
);
479 emsg
->filter
.origin
= filter
->origin
;
480 emsg
->filter
.num_areas
= filter
->num_areas
;
481 if (len
> sizeof(buf
))
483 /* API broken - missing memcpy to fill data */
484 return msg_new(MSG_REGISTER_EVENT
, emsg
, seqnum
, len
);
487 struct msg
*new_msg_sync_lsdb(uint32_t seqnum
, struct lsa_filter_type
*filter
)
489 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
490 struct msg_sync_lsdb
*smsg
;
493 smsg
= (struct msg_sync_lsdb
*)buf
;
494 len
= sizeof(struct msg_sync_lsdb
)
495 + filter
->num_areas
* sizeof(struct in_addr
);
496 smsg
->filter
.typemask
= htons(filter
->typemask
);
497 smsg
->filter
.origin
= filter
->origin
;
498 smsg
->filter
.num_areas
= filter
->num_areas
;
499 if (len
> sizeof(buf
))
501 /* API broken - missing memcpy to fill data */
502 return msg_new(MSG_SYNC_LSDB
, smsg
, seqnum
, len
);
506 struct msg
*new_msg_originate_request(uint32_t seqnum
, struct in_addr ifaddr
,
507 struct in_addr area_id
,
508 struct lsa_header
*data
)
510 struct msg_originate_request
*omsg
;
511 unsigned int omsglen
;
512 char buf
[OSPF_API_MAX_MSG_SIZE
];
513 size_t off_data
= offsetof(struct msg_originate_request
, data
);
514 size_t data_maxs
= sizeof(buf
) - off_data
;
515 struct lsa_header
*omsg_data
= (struct lsa_header
*)&buf
[off_data
];
517 omsg
= (struct msg_originate_request
*)buf
;
518 omsg
->ifaddr
= ifaddr
;
519 omsg
->area_id
= area_id
;
521 omsglen
= ntohs(data
->length
);
522 if (omsglen
> data_maxs
)
524 memcpy(omsg_data
, data
, omsglen
);
525 omsglen
+= sizeof(struct msg_originate_request
)
526 - sizeof(struct lsa_header
);
528 return msg_new(MSG_ORIGINATE_REQUEST
, omsg
, seqnum
, omsglen
);
531 struct msg
*new_msg_delete_request(uint32_t seqnum
, struct in_addr area_id
,
532 uint8_t lsa_type
, uint8_t opaque_type
,
535 struct msg_delete_request dmsg
;
536 dmsg
.area_id
= area_id
;
537 dmsg
.lsa_type
= lsa_type
;
538 dmsg
.opaque_type
= opaque_type
;
539 dmsg
.opaque_id
= htonl(opaque_id
);
540 memset(&dmsg
.pad
, 0, sizeof(dmsg
.pad
));
542 return msg_new(MSG_DELETE_REQUEST
, &dmsg
, seqnum
,
543 sizeof(struct msg_delete_request
));
547 struct msg
*new_msg_reply(uint32_t seqnr
, uint8_t rc
)
550 struct msg_reply rmsg
;
552 /* Set return code */
554 memset(&rmsg
.pad
, 0, sizeof(rmsg
.pad
));
556 msg
= msg_new(MSG_REPLY
, &rmsg
, seqnr
, sizeof(struct msg_reply
));
561 struct msg
*new_msg_ready_notify(uint32_t seqnr
, uint8_t lsa_type
,
562 uint8_t opaque_type
, struct in_addr addr
)
564 struct msg_ready_notify rmsg
;
566 rmsg
.lsa_type
= lsa_type
;
567 rmsg
.opaque_type
= opaque_type
;
568 memset(&rmsg
.pad
, 0, sizeof(rmsg
.pad
));
571 return msg_new(MSG_READY_NOTIFY
, &rmsg
, seqnr
,
572 sizeof(struct msg_ready_notify
));
575 struct msg
*new_msg_new_if(uint32_t seqnr
, struct in_addr ifaddr
,
576 struct in_addr area_id
)
578 struct msg_new_if nmsg
;
580 nmsg
.ifaddr
= ifaddr
;
581 nmsg
.area_id
= area_id
;
583 return msg_new(MSG_NEW_IF
, &nmsg
, seqnr
, sizeof(struct msg_new_if
));
586 struct msg
*new_msg_del_if(uint32_t seqnr
, struct in_addr ifaddr
)
588 struct msg_del_if dmsg
;
590 dmsg
.ifaddr
= ifaddr
;
592 return msg_new(MSG_DEL_IF
, &dmsg
, seqnr
, sizeof(struct msg_del_if
));
595 struct msg
*new_msg_ism_change(uint32_t seqnr
, struct in_addr ifaddr
,
596 struct in_addr area_id
, uint8_t status
)
598 struct msg_ism_change imsg
;
600 imsg
.ifaddr
= ifaddr
;
601 imsg
.area_id
= area_id
;
602 imsg
.status
= status
;
603 memset(&imsg
.pad
, 0, sizeof(imsg
.pad
));
605 return msg_new(MSG_ISM_CHANGE
, &imsg
, seqnr
,
606 sizeof(struct msg_ism_change
));
609 struct msg
*new_msg_nsm_change(uint32_t seqnr
, struct in_addr ifaddr
,
610 struct in_addr nbraddr
, struct in_addr router_id
,
613 struct msg_nsm_change nmsg
;
615 nmsg
.ifaddr
= ifaddr
;
616 nmsg
.nbraddr
= nbraddr
;
617 nmsg
.router_id
= router_id
;
618 nmsg
.status
= status
;
619 memset(&nmsg
.pad
, 0, sizeof(nmsg
.pad
));
621 return msg_new(MSG_NSM_CHANGE
, &nmsg
, seqnr
,
622 sizeof(struct msg_nsm_change
));
625 struct msg
*new_msg_lsa_change_notify(uint8_t msgtype
, uint32_t seqnum
,
626 struct in_addr ifaddr
,
627 struct in_addr area_id
,
628 uint8_t is_self_originated
,
629 struct lsa_header
*data
)
631 uint8_t buf
[OSPF_API_MAX_MSG_SIZE
];
632 struct msg_lsa_change_notify
*nmsg
;
634 size_t off_data
= offsetof(struct msg_lsa_change_notify
, data
);
635 size_t data_maxs
= sizeof(buf
) - off_data
;
636 struct lsa_header
*nmsg_data
= (struct lsa_header
*)&buf
[off_data
];
640 nmsg
= (struct msg_lsa_change_notify
*)buf
;
641 nmsg
->ifaddr
= ifaddr
;
642 nmsg
->area_id
= area_id
;
643 nmsg
->is_self_originated
= is_self_originated
;
644 memset(&nmsg
->pad
, 0, sizeof(nmsg
->pad
));
646 len
= ntohs(data
->length
);
649 memcpy(nmsg_data
, data
, len
);
650 len
+= sizeof(struct msg_lsa_change_notify
) - sizeof(struct lsa_header
);
652 return msg_new(msgtype
, nmsg
, seqnum
, len
);
655 #endif /* SUPPORT_OSPF_API */