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 = XCALLOC (MTYPE_OSPF_API_MSG
, sizeof (struct msg
));
104 new->hdr
.version
= OSPF_API_VERSION
;
105 new->hdr
.msgtype
= msgtype
;
106 new->hdr
.msglen
= htons (msglen
);
107 new->hdr
.msgseq
= htonl (seqnum
);
109 new->s
= stream_new (msglen
);
111 stream_put (new->s
, msgbody
, msglen
);
117 /* Duplicate a message by copying content. */
119 msg_dup (struct msg
*msg
)
125 new = msg_new (msg
->hdr
.msgtype
, STREAM_DATA (msg
->s
),
126 ntohl (msg
->hdr
.msgseq
), ntohs (msg
->hdr
.msglen
));
131 /* XXX only for testing, will be removed */
139 ospf_api_typename (int msgtype
)
141 struct nametab NameTab
[] = {
142 { MSG_REGISTER_OPAQUETYPE
, "Register opaque-type", },
143 { MSG_UNREGISTER_OPAQUETYPE
, "Unregister opaque-type", },
144 { MSG_REGISTER_EVENT
, "Register event", },
145 { MSG_SYNC_LSDB
, "Sync LSDB", },
146 { MSG_ORIGINATE_REQUEST
, "Originate request", },
147 { MSG_DELETE_REQUEST
, "Delete request", },
148 { MSG_REPLY
, "Reply", },
149 { MSG_READY_NOTIFY
, "Ready notify", },
150 { MSG_LSA_UPDATE_NOTIFY
, "LSA update notify", },
151 { MSG_LSA_DELETE_NOTIFY
, "LSA delete notify", },
152 { MSG_NEW_IF
, "New interface", },
153 { MSG_DEL_IF
, "Del interface", },
154 { MSG_ISM_CHANGE
, "ISM change", },
155 { MSG_NSM_CHANGE
, "NSM change", },
158 int i
, n
= sizeof (NameTab
) / sizeof (NameTab
[0]);
159 const char *name
= NULL
;
161 for (i
= 0; i
< n
; i
++)
163 if (NameTab
[i
].value
== msgtype
)
165 name
= NameTab
[i
].name
;
170 return name
? name
: "?";
174 ospf_api_errname (int errcode
)
176 struct nametab NameTab
[] = {
177 { OSPF_API_OK
, "OK", },
178 { OSPF_API_NOSUCHINTERFACE
, "No such interface", },
179 { OSPF_API_NOSUCHAREA
, "No such area", },
180 { OSPF_API_NOSUCHLSA
, "No such LSA", },
181 { OSPF_API_ILLEGALLSATYPE
, "Illegal LSA type", },
182 { OSPF_API_OPAQUETYPEINUSE
, "Opaque type in use", },
183 { OSPF_API_OPAQUETYPENOTREGISTERED
, "Opaque type not registered", },
184 { OSPF_API_NOTREADY
, "Not ready", },
185 { OSPF_API_NOMEMORY
, "No memory", },
186 { OSPF_API_ERROR
, "Other error", },
187 { OSPF_API_UNDEF
, "Undefined", },
190 int i
, n
= sizeof (NameTab
) / sizeof (NameTab
[0]);
191 const char *name
= NULL
;
193 for (i
= 0; i
< n
; i
++)
195 if (NameTab
[i
].value
== errcode
)
197 name
= NameTab
[i
].name
;
202 return name
? name
: "?";
206 msg_print (struct msg
*msg
)
210 zlog_debug ("msg_print msg=NULL!\n");
214 #ifdef ORIGINAL_CODING
216 ("msg=%p msgtype=%d msglen=%d msgseq=%d streamdata=%p streamsize=%lu\n",
217 msg
, msg
->hdr
.msgtype
, ntohs (msg
->hdr
.msglen
), ntohl (msg
->hdr
.msgseq
),
218 STREAM_DATA (msg
->s
), STREAM_SIZE (msg
->s
));
219 #else /* ORIGINAL_CODING */
220 /* API message common header part. */
222 ("API-msg [%s]: type(%d),len(%d),seq(%lu),data(%p),size(%lu)",
223 ospf_api_typename (msg
->hdr
.msgtype
), msg
->hdr
.msgtype
,
224 ntohs (msg
->hdr
.msglen
), (unsigned long) ntohl (msg
->hdr
.msgseq
),
225 STREAM_DATA (msg
->s
), STREAM_SIZE (msg
->s
));
227 /* API message body part. */
229 /* Generic Hex/Ascii dump */
230 DumpBuf (STREAM_DATA (msg
->s
), STREAM_SIZE (msg
->s
)); /* Sorry, deleted! */
232 /* Message-type dependent dump function. */
236 #endif /* ORIGINAL_CODING */
240 msg_free (struct msg
*msg
)
243 stream_free (msg
->s
);
245 XFREE (MTYPE_OSPF_API_MSG
, msg
);
249 /* Set sequence number of message */
251 msg_set_seq (struct msg
*msg
, u_int32_t seqnr
)
254 msg
->hdr
.msgseq
= htonl (seqnr
);
257 /* Get sequence number of message */
259 msg_get_seq (struct msg
*msg
)
262 return ntohl (msg
->hdr
.msgseq
);
265 /* -----------------------------------------------------------
266 * Message fifo queues
267 * -----------------------------------------------------------
273 return XCALLOC (MTYPE_OSPF_API_FIFO
, sizeof (struct msg_fifo
));
276 /* Add new message to fifo. */
278 msg_fifo_push (struct msg_fifo
*fifo
, struct msg
*msg
)
281 fifo
->tail
->next
= msg
;
290 /* Remove first message from fifo. */
292 msg_fifo_pop (struct msg_fifo
*fifo
)
299 fifo
->head
= msg
->next
;
301 if (fifo
->head
== NULL
)
309 /* Return first fifo entry but do not remove it. */
311 msg_fifo_head (struct msg_fifo
*fifo
)
316 /* Flush message fifo. */
318 msg_fifo_flush (struct msg_fifo
*fifo
)
323 for (op
= fifo
->head
; op
; op
= next
)
329 fifo
->head
= fifo
->tail
= NULL
;
333 /* Free API message fifo. */
335 msg_fifo_free (struct msg_fifo
*fifo
)
337 msg_fifo_flush (fifo
);
339 XFREE (MTYPE_OSPF_API_FIFO
, fifo
);
346 struct apimsghdr hdr
;
347 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
351 /* Read message header */
352 rlen
= readn (fd
, (u_char
*) &hdr
, sizeof (struct apimsghdr
));
356 zlog_warn ("msg_read: readn %s", safe_strerror (errno
));
361 zlog_warn ("msg_read: Connection closed by peer");
364 else if (rlen
!= sizeof (struct apimsghdr
))
366 zlog_warn ("msg_read: Cannot read message header!");
370 /* Check version of API protocol */
371 if (hdr
.version
!= OSPF_API_VERSION
)
373 zlog_warn ("msg_read: OSPF API protocol version mismatch");
377 /* Determine body length. */
378 bodylen
= ntohs (hdr
.msglen
);
382 /* Read message body */
383 rlen
= readn (fd
, buf
, bodylen
);
386 zlog_warn ("msg_read: readn %s", safe_strerror (errno
));
391 zlog_warn ("msg_read: Connection closed by peer");
394 else if (rlen
!= bodylen
)
396 zlog_warn ("msg_read: Cannot read message body!");
401 /* Allocate new message */
402 msg
= msg_new (hdr
.msgtype
, buf
, ntohl (hdr
.msgseq
), ntohs (hdr
.msglen
));
408 msg_write (int fd
, struct msg
*msg
)
410 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
417 /* Length of message including header */
418 l
= sizeof (struct apimsghdr
) + ntohs (msg
->hdr
.msglen
);
420 /* Make contiguous memory buffer for message */
421 memcpy (buf
, &msg
->hdr
, sizeof (struct apimsghdr
));
422 memcpy (buf
+ sizeof (struct apimsghdr
), STREAM_DATA (msg
->s
),
423 ntohs (msg
->hdr
.msglen
));
425 wlen
= writen (fd
, buf
, l
);
428 zlog_warn ("msg_write: writen %s", safe_strerror (errno
));
433 zlog_warn ("msg_write: Connection closed by peer");
438 zlog_warn ("msg_write: Cannot write API message");
444 /* -----------------------------------------------------------
446 * -----------------------------------------------------------
450 new_msg_register_opaque_type (u_int32_t seqnum
, u_char ltype
, u_char otype
)
452 struct msg_register_opaque_type rmsg
;
454 rmsg
.lsatype
= ltype
;
455 rmsg
.opaquetype
= otype
;
456 memset (&rmsg
.pad
, 0, sizeof (rmsg
.pad
));
458 return msg_new (MSG_REGISTER_OPAQUETYPE
, &rmsg
, seqnum
,
459 sizeof (struct msg_register_opaque_type
));
463 new_msg_register_event (u_int32_t seqnum
, struct lsa_filter_type
*filter
)
465 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
466 struct msg_register_event
*emsg
;
469 emsg
= (struct msg_register_event
*) buf
;
470 len
= sizeof (struct msg_register_event
) +
471 filter
->num_areas
* sizeof (struct in_addr
);
472 emsg
->filter
.typemask
= htons (filter
->typemask
);
473 emsg
->filter
.origin
= filter
->origin
;
474 emsg
->filter
.num_areas
= filter
->num_areas
;
475 return msg_new (MSG_REGISTER_EVENT
, emsg
, seqnum
, len
);
479 new_msg_sync_lsdb (u_int32_t seqnum
, struct lsa_filter_type
*filter
)
481 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
482 struct msg_sync_lsdb
*smsg
;
485 smsg
= (struct msg_sync_lsdb
*) buf
;
486 len
= sizeof (struct msg_sync_lsdb
) +
487 filter
->num_areas
* sizeof (struct in_addr
);
488 smsg
->filter
.typemask
= htons (filter
->typemask
);
489 smsg
->filter
.origin
= filter
->origin
;
490 smsg
->filter
.num_areas
= filter
->num_areas
;
491 return msg_new (MSG_SYNC_LSDB
, smsg
, seqnum
, len
);
496 new_msg_originate_request (u_int32_t seqnum
,
497 struct in_addr ifaddr
,
498 struct in_addr area_id
, struct lsa_header
*data
)
500 struct msg_originate_request
*omsg
;
502 char buf
[OSPF_API_MAX_MSG_SIZE
];
504 omsglen
= sizeof (struct msg_originate_request
) - sizeof (struct lsa_header
)
505 + ntohs (data
->length
);
507 omsg
= (struct msg_originate_request
*) buf
;
508 omsg
->ifaddr
= ifaddr
;
509 omsg
->area_id
= area_id
;
510 memcpy (&omsg
->data
, data
, ntohs (data
->length
));
512 return msg_new (MSG_ORIGINATE_REQUEST
, omsg
, seqnum
, omsglen
);
516 new_msg_delete_request (u_int32_t seqnum
,
517 struct in_addr area_id
, u_char lsa_type
,
518 u_char opaque_type
, u_int32_t opaque_id
)
520 struct msg_delete_request dmsg
;
521 dmsg
.area_id
= area_id
;
522 dmsg
.lsa_type
= lsa_type
;
523 dmsg
.opaque_type
= opaque_type
;
524 dmsg
.opaque_id
= htonl (opaque_id
);
525 memset (&dmsg
.pad
, 0, sizeof (dmsg
.pad
));
527 return msg_new (MSG_DELETE_REQUEST
, &dmsg
, seqnum
,
528 sizeof (struct msg_delete_request
));
533 new_msg_reply (u_int32_t seqnr
, u_char rc
)
536 struct msg_reply rmsg
;
538 /* Set return code */
540 memset (&rmsg
.pad
, 0, sizeof (rmsg
.pad
));
542 msg
= msg_new (MSG_REPLY
, &rmsg
, seqnr
, sizeof (struct msg_reply
));
548 new_msg_ready_notify (u_int32_t seqnr
, u_char lsa_type
,
549 u_char opaque_type
, struct in_addr addr
)
551 struct msg_ready_notify rmsg
;
553 rmsg
.lsa_type
= lsa_type
;
554 rmsg
.opaque_type
= opaque_type
;
555 memset (&rmsg
.pad
, 0, sizeof (rmsg
.pad
));
558 return msg_new (MSG_READY_NOTIFY
, &rmsg
, seqnr
,
559 sizeof (struct msg_ready_notify
));
563 new_msg_new_if (u_int32_t seqnr
,
564 struct in_addr ifaddr
, struct in_addr area_id
)
566 struct msg_new_if nmsg
;
568 nmsg
.ifaddr
= ifaddr
;
569 nmsg
.area_id
= area_id
;
571 return msg_new (MSG_NEW_IF
, &nmsg
, seqnr
, sizeof (struct msg_new_if
));
575 new_msg_del_if (u_int32_t seqnr
, struct in_addr ifaddr
)
577 struct msg_del_if dmsg
;
579 dmsg
.ifaddr
= ifaddr
;
581 return msg_new (MSG_DEL_IF
, &dmsg
, seqnr
, sizeof (struct msg_del_if
));
585 new_msg_ism_change (u_int32_t seqnr
, struct in_addr ifaddr
,
586 struct in_addr area_id
, u_char 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
));
600 new_msg_nsm_change (u_int32_t seqnr
, struct in_addr ifaddr
,
601 struct in_addr nbraddr
,
602 struct in_addr router_id
, u_char status
)
604 struct msg_nsm_change nmsg
;
606 nmsg
.ifaddr
= ifaddr
;
607 nmsg
.nbraddr
= nbraddr
;
608 nmsg
.router_id
= router_id
;
609 nmsg
.status
= status
;
610 memset (&nmsg
.pad
, 0, sizeof (nmsg
.pad
));
612 return msg_new (MSG_NSM_CHANGE
, &nmsg
, seqnr
,
613 sizeof (struct msg_nsm_change
));
617 new_msg_lsa_change_notify (u_char msgtype
,
619 struct in_addr ifaddr
,
620 struct in_addr area_id
,
621 u_char is_self_originated
, struct lsa_header
*data
)
623 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
624 struct msg_lsa_change_notify
*nmsg
;
629 nmsg
= (struct msg_lsa_change_notify
*) buf
;
630 len
= ntohs (data
->length
) + sizeof (struct msg_lsa_change_notify
)
631 - sizeof (struct lsa_header
);
632 nmsg
->ifaddr
= ifaddr
;
633 nmsg
->area_id
= area_id
;
634 nmsg
->is_self_originated
= is_self_originated
;
635 memset (&nmsg
->pad
, 0, sizeof (nmsg
->pad
));
636 memcpy (&nmsg
->data
, data
, ntohs (data
->length
));
638 return msg_new (msgtype
, nmsg
, seqnum
, len
);
641 #endif /* SUPPORT_OSPF_API */