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
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 #ifdef SUPPORT_OSPF_API
38 #include "sockunion.h" /* for inet_aton() */
42 #include "ospfd/ospfd.h"
43 #include "ospfd/ospf_interface.h"
44 #include "ospfd/ospf_ism.h"
45 #include "ospfd/ospf_asbr.h"
46 #include "ospfd/ospf_lsa.h"
47 #include "ospfd/ospf_lsdb.h"
48 #include "ospfd/ospf_neighbor.h"
49 #include "ospfd/ospf_nsm.h"
50 #include "ospfd/ospf_flood.h"
51 #include "ospfd/ospf_packet.h"
52 #include "ospfd/ospf_spf.h"
53 #include "ospfd/ospf_dump.h"
54 #include "ospfd/ospf_route.h"
55 #include "ospfd/ospf_ase.h"
56 #include "ospfd/ospf_zebra.h"
58 #include "ospfd/ospf_api.h"
61 /* For debugging only, will be removed */
62 void api_opaque_lsa_print(struct lsa_header
*data
)
65 struct lsa_header header
;
69 struct opaque_lsa
*olsa
;
73 ospf_lsa_header_dump(data
);
75 olsa
= (struct opaque_lsa
*)data
;
77 opaquelen
= ntohs(data
->length
) - OSPF_LSA_HEADER_SIZE
;
78 zlog_debug("apiserver_lsa_print: opaquelen=%d\n", opaquelen
);
80 for (i
= 0; i
< opaquelen
; i
++) {
81 zlog_debug("0x%x ", olsa
->mydata
[i
]);
86 /* -----------------------------------------------------------
88 * -----------------------------------------------------------
91 struct msg
*msg_new(u_char msgtype
, void *msgbody
, u_int32_t seqnum
,
96 new = XCALLOC(MTYPE_OSPF_API_MSG
, sizeof(struct msg
));
98 new->hdr
.version
= OSPF_API_VERSION
;
99 new->hdr
.msgtype
= msgtype
;
100 new->hdr
.msglen
= htons(msglen
);
101 new->hdr
.msgseq
= htonl(seqnum
);
103 new->s
= stream_new(msglen
);
105 stream_put(new->s
, msgbody
, msglen
);
111 /* Duplicate a message by copying content. */
112 struct msg
*msg_dup(struct msg
*msg
)
118 new = msg_new(msg
->hdr
.msgtype
, STREAM_DATA(msg
->s
),
119 ntohl(msg
->hdr
.msgseq
), ntohs(msg
->hdr
.msglen
));
124 /* XXX only for testing, will be removed */
131 const char *ospf_api_typename(int msgtype
)
133 struct nametab NameTab
[] = {
135 MSG_REGISTER_OPAQUETYPE
, "Register opaque-type",
138 MSG_UNREGISTER_OPAQUETYPE
, "Unregister opaque-type",
141 MSG_REGISTER_EVENT
, "Register event",
144 MSG_SYNC_LSDB
, "Sync LSDB",
147 MSG_ORIGINATE_REQUEST
, "Originate request",
150 MSG_DELETE_REQUEST
, "Delete request",
156 MSG_READY_NOTIFY
, "Ready notify",
159 MSG_LSA_UPDATE_NOTIFY
, "LSA update notify",
162 MSG_LSA_DELETE_NOTIFY
, "LSA delete notify",
165 MSG_NEW_IF
, "New interface",
168 MSG_DEL_IF
, "Del interface",
171 MSG_ISM_CHANGE
, "ISM change",
174 MSG_NSM_CHANGE
, "NSM change",
178 int i
, n
= array_size(NameTab
);
179 const char *name
= NULL
;
181 for (i
= 0; i
< n
; i
++) {
182 if (NameTab
[i
].value
== msgtype
) {
183 name
= NameTab
[i
].name
;
188 return name
? name
: "?";
191 const char *ospf_api_errname(int errcode
)
193 struct nametab NameTab
[] = {
198 OSPF_API_NOSUCHINTERFACE
, "No such interface",
201 OSPF_API_NOSUCHAREA
, "No such area",
204 OSPF_API_NOSUCHLSA
, "No such LSA",
207 OSPF_API_ILLEGALLSATYPE
, "Illegal LSA type",
210 OSPF_API_OPAQUETYPEINUSE
, "Opaque type in use",
213 OSPF_API_OPAQUETYPENOTREGISTERED
,
214 "Opaque type not registered",
217 OSPF_API_NOTREADY
, "Not ready",
220 OSPF_API_NOMEMORY
, "No memory",
223 OSPF_API_ERROR
, "Other error",
226 OSPF_API_UNDEF
, "Undefined",
230 int i
, n
= array_size(NameTab
);
231 const char *name
= NULL
;
233 for (i
= 0; i
< n
; i
++) {
234 if (NameTab
[i
].value
== errcode
) {
235 name
= NameTab
[i
].name
;
240 return name
? name
: "?";
243 void msg_print(struct msg
*msg
)
246 zlog_debug("msg_print msg=NULL!\n");
250 #ifdef ORIGINAL_CODING
252 "msg=%p msgtype=%d msglen=%d msgseq=%d streamdata=%p streamsize=%lu\n",
253 msg
, msg
->hdr
.msgtype
, ntohs(msg
->hdr
.msglen
),
254 ntohl(msg
->hdr
.msgseq
), STREAM_DATA(msg
->s
),
255 STREAM_SIZE(msg
->s
));
256 #else /* ORIGINAL_CODING */
257 /* API message common header part. */
258 zlog_debug("API-msg [%s]: type(%d),len(%d),seq(%lu),data(%p),size(%zd)",
259 ospf_api_typename(msg
->hdr
.msgtype
), msg
->hdr
.msgtype
,
260 ntohs(msg
->hdr
.msglen
),
261 (unsigned long)ntohl(msg
->hdr
.msgseq
), STREAM_DATA(msg
->s
),
262 STREAM_SIZE(msg
->s
));
264 /* API message body part. */
266 /* Generic Hex/Ascii dump */
267 DumpBuf(STREAM_DATA(msg
->s
), STREAM_SIZE(msg
->s
)); /* Sorry, deleted! */
269 /* Message-type dependent dump function. */
273 #endif /* ORIGINAL_CODING */
276 void msg_free(struct msg
*msg
)
281 XFREE(MTYPE_OSPF_API_MSG
, msg
);
285 /* Set sequence number of message */
286 void msg_set_seq(struct msg
*msg
, u_int32_t seqnr
)
289 msg
->hdr
.msgseq
= htonl(seqnr
);
292 /* Get sequence number of message */
293 u_int32_t
msg_get_seq(struct msg
*msg
)
296 return ntohl(msg
->hdr
.msgseq
);
299 /* -----------------------------------------------------------
300 * Message fifo queues
301 * -----------------------------------------------------------
304 struct msg_fifo
*msg_fifo_new()
306 return XCALLOC(MTYPE_OSPF_API_FIFO
, sizeof(struct msg_fifo
));
309 /* Add new message to fifo. */
310 void msg_fifo_push(struct msg_fifo
*fifo
, struct msg
*msg
)
313 fifo
->tail
->next
= msg
;
322 /* Remove first message from fifo. */
323 struct msg
*msg_fifo_pop(struct msg_fifo
*fifo
)
329 fifo
->head
= msg
->next
;
331 if (fifo
->head
== NULL
)
339 /* Return first fifo entry but do not remove it. */
340 struct msg
*msg_fifo_head(struct msg_fifo
*fifo
)
345 /* Flush message fifo. */
346 void msg_fifo_flush(struct msg_fifo
*fifo
)
351 for (op
= fifo
->head
; op
; op
= next
) {
356 fifo
->head
= fifo
->tail
= NULL
;
360 /* Free API message fifo. */
361 void msg_fifo_free(struct msg_fifo
*fifo
)
363 msg_fifo_flush(fifo
);
365 XFREE(MTYPE_OSPF_API_FIFO
, fifo
);
368 struct msg
*msg_read(int fd
)
371 struct apimsghdr hdr
;
372 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
376 /* Read message header */
377 rlen
= readn(fd
, (u_char
*)&hdr
, sizeof(struct apimsghdr
));
380 zlog_warn("msg_read: readn %s", safe_strerror(errno
));
382 } else if (rlen
== 0) {
383 zlog_warn("msg_read: Connection closed by peer");
385 } else if (rlen
!= sizeof(struct apimsghdr
)) {
386 zlog_warn("msg_read: Cannot read message header!");
390 /* Check version of API protocol */
391 if (hdr
.version
!= OSPF_API_VERSION
) {
392 zlog_warn("msg_read: OSPF API protocol version mismatch");
396 /* Determine body length. */
397 bodylen
= ntohs(hdr
.msglen
);
400 /* Read message body */
401 rlen
= readn(fd
, buf
, bodylen
);
403 zlog_warn("msg_read: readn %s", safe_strerror(errno
));
405 } else if (rlen
== 0) {
406 zlog_warn("msg_read: Connection closed by peer");
408 } else if (rlen
!= bodylen
) {
409 zlog_warn("msg_read: Cannot read message body!");
414 /* Allocate new message */
415 msg
= msg_new(hdr
.msgtype
, buf
, ntohl(hdr
.msgseq
), ntohs(hdr
.msglen
));
420 int msg_write(int fd
, struct msg
*msg
)
422 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
429 /* Length of message including header */
430 l
= sizeof(struct apimsghdr
) + ntohs(msg
->hdr
.msglen
);
432 /* Make contiguous memory buffer for message */
433 memcpy(buf
, &msg
->hdr
, sizeof(struct apimsghdr
));
434 memcpy(buf
+ sizeof(struct apimsghdr
), STREAM_DATA(msg
->s
),
435 ntohs(msg
->hdr
.msglen
));
437 wlen
= writen(fd
, buf
, l
);
439 zlog_warn("msg_write: writen %s", safe_strerror(errno
));
441 } else if (wlen
== 0) {
442 zlog_warn("msg_write: Connection closed by peer");
444 } else if (wlen
!= l
) {
445 zlog_warn("msg_write: Cannot write API message");
451 /* -----------------------------------------------------------
453 * -----------------------------------------------------------
456 struct msg
*new_msg_register_opaque_type(u_int32_t seqnum
, u_char ltype
,
459 struct msg_register_opaque_type rmsg
;
461 rmsg
.lsatype
= ltype
;
462 rmsg
.opaquetype
= otype
;
463 memset(&rmsg
.pad
, 0, sizeof(rmsg
.pad
));
465 return msg_new(MSG_REGISTER_OPAQUETYPE
, &rmsg
, seqnum
,
466 sizeof(struct msg_register_opaque_type
));
469 struct msg
*new_msg_register_event(u_int32_t seqnum
,
470 struct lsa_filter_type
*filter
)
472 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
473 struct msg_register_event
*emsg
;
476 emsg
= (struct msg_register_event
*)buf
;
477 len
= sizeof(struct msg_register_event
)
478 + filter
->num_areas
* sizeof(struct in_addr
);
479 emsg
->filter
.typemask
= htons(filter
->typemask
);
480 emsg
->filter
.origin
= filter
->origin
;
481 emsg
->filter
.num_areas
= filter
->num_areas
;
482 if (len
> sizeof(buf
))
484 /* API broken - missing memcpy to fill data */
485 return msg_new(MSG_REGISTER_EVENT
, emsg
, seqnum
, len
);
488 struct msg
*new_msg_sync_lsdb(u_int32_t seqnum
, struct lsa_filter_type
*filter
)
490 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
491 struct msg_sync_lsdb
*smsg
;
494 smsg
= (struct msg_sync_lsdb
*)buf
;
495 len
= sizeof(struct msg_sync_lsdb
)
496 + filter
->num_areas
* sizeof(struct in_addr
);
497 smsg
->filter
.typemask
= htons(filter
->typemask
);
498 smsg
->filter
.origin
= filter
->origin
;
499 smsg
->filter
.num_areas
= filter
->num_areas
;
500 if (len
> sizeof(buf
))
502 /* API broken - missing memcpy to fill data */
503 return msg_new(MSG_SYNC_LSDB
, smsg
, seqnum
, len
);
507 struct msg
*new_msg_originate_request(u_int32_t seqnum
, struct in_addr ifaddr
,
508 struct in_addr area_id
,
509 struct lsa_header
*data
)
511 struct msg_originate_request
*omsg
;
512 unsigned int omsglen
;
513 char buf
[OSPF_API_MAX_MSG_SIZE
];
515 omsg
= (struct msg_originate_request
*)buf
;
516 omsg
->ifaddr
= ifaddr
;
517 omsg
->area_id
= area_id
;
519 omsglen
= ntohs(data
->length
);
521 > sizeof(buf
) - offsetof(struct msg_originate_request
, data
))
522 omsglen
= sizeof(buf
)
523 - offsetof(struct msg_originate_request
, data
);
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(u_int32_t seqnum
, struct in_addr area_id
,
532 u_char lsa_type
, u_char 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(u_int32_t seqnr
, u_char 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(u_int32_t seqnr
, u_char lsa_type
,
562 u_char 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(u_int32_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(u_int32_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(u_int32_t seqnr
, struct in_addr ifaddr
,
596 struct in_addr area_id
, u_char 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(u_int32_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(u_char msgtype
, u_int32_t seqnum
,
626 struct in_addr ifaddr
,
627 struct in_addr area_id
,
628 u_char is_self_originated
,
629 struct lsa_header
*data
)
631 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
632 struct msg_lsa_change_notify
*nmsg
;
637 nmsg
= (struct msg_lsa_change_notify
*)buf
;
638 nmsg
->ifaddr
= ifaddr
;
639 nmsg
->area_id
= area_id
;
640 nmsg
->is_self_originated
= is_self_originated
;
641 memset(&nmsg
->pad
, 0, sizeof(nmsg
->pad
));
643 len
= ntohs(data
->length
);
644 if (len
> sizeof(buf
) - offsetof(struct msg_lsa_change_notify
, data
))
646 - offsetof(struct msg_lsa_change_notify
, data
);
647 memcpy(&nmsg
->data
, data
, len
);
648 len
+= sizeof(struct msg_lsa_change_notify
) - sizeof(struct lsa_header
);
650 return msg_new(msgtype
, nmsg
, seqnum
, len
);
653 #endif /* SUPPORT_OSPF_API */