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
26 #ifndef HAVE_OPAQUE_LSA
27 #error "Core Opaque-LSA module must be configured."
28 #endif /* HAVE_OPAQUE_LSA */
41 #include "sockunion.h" /* for inet_aton() */
45 #include "ospfd/ospfd.h"
46 #include "ospfd/ospf_interface.h"
47 #include "ospfd/ospf_ism.h"
48 #include "ospfd/ospf_asbr.h"
49 #include "ospfd/ospf_lsa.h"
50 #include "ospfd/ospf_lsdb.h"
51 #include "ospfd/ospf_neighbor.h"
52 #include "ospfd/ospf_nsm.h"
53 #include "ospfd/ospf_flood.h"
54 #include "ospfd/ospf_packet.h"
55 #include "ospfd/ospf_spf.h"
56 #include "ospfd/ospf_dump.h"
57 #include "ospfd/ospf_route.h"
58 #include "ospfd/ospf_ase.h"
59 #include "ospfd/ospf_zebra.h"
61 #include "ospfd/ospf_api.h"
64 /* For debugging only, will be removed */
66 api_opaque_lsa_print (struct lsa_header
*data
)
70 struct lsa_header header
;
74 struct opaque_lsa
*olsa
;
78 ospf_lsa_header_dump (data
);
80 olsa
= (struct opaque_lsa
*) data
;
82 opaquelen
= ntohs (data
->length
) - OSPF_LSA_HEADER_SIZE
;
83 zlog_debug ("apiserver_lsa_print: opaquelen=%d\n", opaquelen
);
85 for (i
= 0; i
< opaquelen
; i
++)
87 zlog_debug ("0x%x ", olsa
->mydata
[i
]);
92 /* -----------------------------------------------------------
94 * -----------------------------------------------------------
98 msg_new (u_char msgtype
, void *msgbody
, u_int32_t seqnum
, u_int16_t msglen
)
102 new = XMALLOC (MTYPE_OSPF_API_MSG
, sizeof (struct msg
));
103 memset (new, 0, sizeof (struct msg
));
105 new->hdr
.version
= OSPF_API_VERSION
;
106 new->hdr
.msgtype
= msgtype
;
107 new->hdr
.msglen
= htons (msglen
);
108 new->hdr
.msgseq
= htonl (seqnum
);
110 new->s
= stream_new (msglen
);
112 stream_put (new->s
, msgbody
, msglen
);
118 /* Duplicate a message by copying content. */
120 msg_dup (struct msg
*msg
)
126 new = msg_new (msg
->hdr
.msgtype
, STREAM_DATA (msg
->s
),
127 ntohl (msg
->hdr
.msgseq
), ntohs (msg
->hdr
.msglen
));
132 /* XXX only for testing, will be removed */
140 ospf_api_typename (int msgtype
)
142 struct nametab NameTab
[] = {
143 { MSG_REGISTER_OPAQUETYPE
, "Register opaque-type", },
144 { MSG_UNREGISTER_OPAQUETYPE
, "Unregister opaque-type", },
145 { MSG_REGISTER_EVENT
, "Register event", },
146 { MSG_SYNC_LSDB
, "Sync LSDB", },
147 { MSG_ORIGINATE_REQUEST
, "Originate request", },
148 { MSG_DELETE_REQUEST
, "Delete request", },
149 { MSG_REPLY
, "Reply", },
150 { MSG_READY_NOTIFY
, "Ready notify", },
151 { MSG_LSA_UPDATE_NOTIFY
, "LSA update notify", },
152 { MSG_LSA_DELETE_NOTIFY
, "LSA delete notify", },
153 { MSG_NEW_IF
, "New interface", },
154 { MSG_DEL_IF
, "Del interface", },
155 { MSG_ISM_CHANGE
, "ISM change", },
156 { MSG_NSM_CHANGE
, "NSM change", },
159 int i
, n
= sizeof (NameTab
) / sizeof (NameTab
[0]);
160 const char *name
= NULL
;
162 for (i
= 0; i
< n
; i
++)
164 if (NameTab
[i
].value
== msgtype
)
166 name
= NameTab
[i
].name
;
171 return name
? name
: "?";
175 ospf_api_errname (int errcode
)
177 struct nametab NameTab
[] = {
178 { OSPF_API_OK
, "OK", },
179 { OSPF_API_NOSUCHINTERFACE
, "No such interface", },
180 { OSPF_API_NOSUCHAREA
, "No such area", },
181 { OSPF_API_NOSUCHLSA
, "No such LSA", },
182 { OSPF_API_ILLEGALLSATYPE
, "Illegal LSA type", },
183 { OSPF_API_OPAQUETYPEINUSE
, "Opaque type in use", },
184 { OSPF_API_OPAQUETYPENOTREGISTERED
, "Opaque type not registered", },
185 { OSPF_API_NOTREADY
, "Not ready", },
186 { OSPF_API_NOMEMORY
, "No memory", },
187 { OSPF_API_ERROR
, "Other error", },
188 { OSPF_API_UNDEF
, "Undefined", },
191 int i
, n
= sizeof (NameTab
) / sizeof (NameTab
[0]);
192 const char *name
= NULL
;
194 for (i
= 0; i
< n
; i
++)
196 if (NameTab
[i
].value
== errcode
)
198 name
= NameTab
[i
].name
;
203 return name
? name
: "?";
207 msg_print (struct msg
*msg
)
211 zlog_debug ("msg_print msg=NULL!\n");
215 #ifdef ORIGINAL_CODING
217 ("msg=%p msgtype=%d msglen=%d msgseq=%d streamdata=%p streamsize=%lu\n",
218 msg
, msg
->hdr
.msgtype
, ntohs (msg
->hdr
.msglen
), ntohl (msg
->hdr
.msgseq
),
219 STREAM_DATA (msg
->s
), STREAM_SIZE (msg
->s
));
220 #else /* ORIGINAL_CODING */
221 /* API message common header part. */
223 ("API-msg [%s]: type(%d),len(%d),seq(%lu),data(%p),size(%lu)",
224 ospf_api_typename (msg
->hdr
.msgtype
), msg
->hdr
.msgtype
,
225 ntohs (msg
->hdr
.msglen
), (unsigned long) ntohl (msg
->hdr
.msgseq
),
226 STREAM_DATA (msg
->s
), STREAM_SIZE (msg
->s
));
228 /* API message body part. */
230 /* Generic Hex/Ascii dump */
231 DumpBuf (STREAM_DATA (msg
->s
), STREAM_SIZE (msg
->s
)); /* Sorry, deleted! */
233 /* Message-type dependent dump function. */
237 #endif /* ORIGINAL_CODING */
241 msg_free (struct msg
*msg
)
244 stream_free (msg
->s
);
246 XFREE (MTYPE_OSPF_API_MSG
, msg
);
250 /* Set sequence number of message */
252 msg_set_seq (struct msg
*msg
, u_int32_t seqnr
)
255 msg
->hdr
.msgseq
= htonl (seqnr
);
258 /* Get sequence number of message */
260 msg_get_seq (struct msg
*msg
)
263 return ntohl (msg
->hdr
.msgseq
);
266 /* -----------------------------------------------------------
267 * Message fifo queues
268 * -----------------------------------------------------------
274 struct msg_fifo
*new;
276 new = XMALLOC (MTYPE_OSPF_API_FIFO
, sizeof (struct msg_fifo
));
277 memset (new, 0, sizeof (struct msg_fifo
));
282 /* Add new message to fifo. */
284 msg_fifo_push (struct msg_fifo
*fifo
, struct msg
*msg
)
287 fifo
->tail
->next
= msg
;
296 /* Remove first message from fifo. */
298 msg_fifo_pop (struct msg_fifo
*fifo
)
305 fifo
->head
= msg
->next
;
307 if (fifo
->head
== NULL
)
315 /* Return first fifo entry but do not remove it. */
317 msg_fifo_head (struct msg_fifo
*fifo
)
322 /* Flush message fifo. */
324 msg_fifo_flush (struct msg_fifo
*fifo
)
329 for (op
= fifo
->head
; op
; op
= next
)
335 fifo
->head
= fifo
->tail
= NULL
;
339 /* Free API message fifo. */
341 msg_fifo_free (struct msg_fifo
*fifo
)
343 msg_fifo_flush (fifo
);
345 XFREE (MTYPE_OSPF_API_FIFO
, fifo
);
352 struct apimsghdr hdr
;
353 char buf
[OSPF_API_MAX_MSG_SIZE
];
357 /* Read message header */
358 rlen
= readn (fd
, (char *) &hdr
, sizeof (struct apimsghdr
));
362 zlog_warn ("msg_read: readn %s", safe_strerror (errno
));
367 zlog_warn ("msg_read: Connection closed by peer");
370 else if (rlen
!= sizeof (struct apimsghdr
))
372 zlog_warn ("msg_read: Cannot read message header!");
376 /* Check version of API protocol */
377 if (hdr
.version
!= OSPF_API_VERSION
)
379 zlog_warn ("msg_read: OSPF API protocol version mismatch");
383 /* Determine body length. */
384 bodylen
= ntohs (hdr
.msglen
);
388 /* Read message body */
389 rlen
= readn (fd
, buf
, bodylen
);
392 zlog_warn ("msg_read: readn %s", safe_strerror (errno
));
397 zlog_warn ("msg_read: Connection closed by peer");
400 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
), ntohs (hdr
.msglen
));
414 msg_write (int fd
, struct msg
*msg
)
416 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
423 /* Length of message including header */
424 l
= sizeof (struct apimsghdr
) + ntohs (msg
->hdr
.msglen
);
426 /* Make contiguous memory buffer for message */
427 memcpy (buf
, &msg
->hdr
, sizeof (struct apimsghdr
));
428 memcpy (buf
+ sizeof (struct apimsghdr
), STREAM_DATA (msg
->s
),
429 ntohs (msg
->hdr
.msglen
));
431 wlen
= writen (fd
, buf
, l
);
434 zlog_warn ("msg_write: writen %s", safe_strerror (errno
));
439 zlog_warn ("msg_write: Connection closed by peer");
444 zlog_warn ("msg_write: Cannot write API message");
450 /* -----------------------------------------------------------
452 * -----------------------------------------------------------
456 new_msg_register_opaque_type (u_int32_t seqnum
, u_char ltype
, u_char otype
)
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
));
469 new_msg_register_event (u_int32_t seqnum
, struct lsa_filter_type
*filter
)
471 u_char 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 return msg_new (MSG_REGISTER_EVENT
, emsg
, seqnum
, len
);
485 new_msg_sync_lsdb (u_int32_t seqnum
, struct lsa_filter_type
*filter
)
487 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
488 struct msg_sync_lsdb
*smsg
;
491 smsg
= (struct msg_sync_lsdb
*) buf
;
492 len
= sizeof (struct msg_sync_lsdb
) +
493 filter
->num_areas
* sizeof (struct in_addr
);
494 smsg
->filter
.typemask
= htons (filter
->typemask
);
495 smsg
->filter
.origin
= filter
->origin
;
496 smsg
->filter
.num_areas
= filter
->num_areas
;
497 return msg_new (MSG_SYNC_LSDB
, smsg
, seqnum
, len
);
502 new_msg_originate_request (u_int32_t seqnum
,
503 struct in_addr ifaddr
,
504 struct in_addr area_id
, struct lsa_header
*data
)
506 struct msg_originate_request
*omsg
;
508 char buf
[OSPF_API_MAX_MSG_SIZE
];
510 omsglen
= sizeof (struct msg_originate_request
) - sizeof (struct lsa_header
)
511 + ntohs (data
->length
);
513 omsg
= (struct msg_originate_request
*) buf
;
514 omsg
->ifaddr
= ifaddr
;
515 omsg
->area_id
= area_id
;
516 memcpy (&omsg
->data
, data
, ntohs (data
->length
));
518 return msg_new (MSG_ORIGINATE_REQUEST
, omsg
, seqnum
, omsglen
);
522 new_msg_delete_request (u_int32_t seqnum
,
523 struct in_addr area_id
, u_char lsa_type
,
524 u_char opaque_type
, u_int32_t opaque_id
)
526 struct msg_delete_request dmsg
;
527 dmsg
.area_id
= area_id
;
528 dmsg
.lsa_type
= lsa_type
;
529 dmsg
.opaque_type
= opaque_type
;
530 dmsg
.opaque_id
= htonl (opaque_id
);
531 memset (&dmsg
.pad
, 0, sizeof (dmsg
.pad
));
533 return msg_new (MSG_DELETE_REQUEST
, &dmsg
, seqnum
,
534 sizeof (struct msg_delete_request
));
539 new_msg_reply (u_int32_t seqnr
, u_char rc
)
542 struct msg_reply rmsg
;
544 /* Set return code */
546 memset (&rmsg
.pad
, 0, sizeof (rmsg
.pad
));
548 msg
= msg_new (MSG_REPLY
, &rmsg
, seqnr
, sizeof (struct msg_reply
));
554 new_msg_ready_notify (u_int32_t seqnr
, u_char lsa_type
,
555 u_char opaque_type
, struct in_addr addr
)
557 struct msg_ready_notify rmsg
;
559 rmsg
.lsa_type
= lsa_type
;
560 rmsg
.opaque_type
= opaque_type
;
561 memset (&rmsg
.pad
, 0, sizeof (rmsg
.pad
));
564 return msg_new (MSG_READY_NOTIFY
, &rmsg
, seqnr
,
565 sizeof (struct msg_ready_notify
));
569 new_msg_new_if (u_int32_t seqnr
,
570 struct in_addr ifaddr
, struct in_addr area_id
)
572 struct msg_new_if nmsg
;
574 nmsg
.ifaddr
= ifaddr
;
575 nmsg
.area_id
= area_id
;
577 return msg_new (MSG_NEW_IF
, &nmsg
, seqnr
, sizeof (struct msg_new_if
));
581 new_msg_del_if (u_int32_t seqnr
, struct in_addr ifaddr
)
583 struct msg_del_if dmsg
;
585 dmsg
.ifaddr
= ifaddr
;
587 return msg_new (MSG_DEL_IF
, &dmsg
, seqnr
, sizeof (struct msg_del_if
));
591 new_msg_ism_change (u_int32_t seqnr
, struct in_addr ifaddr
,
592 struct in_addr area_id
, u_char status
)
594 struct msg_ism_change imsg
;
596 imsg
.ifaddr
= ifaddr
;
597 imsg
.area_id
= area_id
;
598 imsg
.status
= status
;
599 memset (&imsg
.pad
, 0, sizeof (imsg
.pad
));
601 return msg_new (MSG_ISM_CHANGE
, &imsg
, seqnr
,
602 sizeof (struct msg_ism_change
));
606 new_msg_nsm_change (u_int32_t seqnr
, struct in_addr ifaddr
,
607 struct in_addr nbraddr
,
608 struct in_addr router_id
, u_char status
)
610 struct msg_nsm_change nmsg
;
612 nmsg
.ifaddr
= ifaddr
;
613 nmsg
.nbraddr
= nbraddr
;
614 nmsg
.router_id
= router_id
;
615 nmsg
.status
= status
;
616 memset (&nmsg
.pad
, 0, sizeof (nmsg
.pad
));
618 return msg_new (MSG_NSM_CHANGE
, &nmsg
, seqnr
,
619 sizeof (struct msg_nsm_change
));
623 new_msg_lsa_change_notify (u_char msgtype
,
625 struct in_addr ifaddr
,
626 struct in_addr area_id
,
627 u_char is_self_originated
, struct lsa_header
*data
)
629 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
630 struct msg_lsa_change_notify
*nmsg
;
635 nmsg
= (struct msg_lsa_change_notify
*) buf
;
636 len
= ntohs (data
->length
) + sizeof (struct msg_lsa_change_notify
)
637 - sizeof (struct lsa_header
);
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
));
642 memcpy (&nmsg
->data
, data
, ntohs (data
->length
));
644 return msg_new (msgtype
, nmsg
, seqnum
, len
);
647 #endif /* SUPPORT_OSPF_API */