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 */
62 api_opaque_lsa_print (struct lsa_header
*data
)
66 struct lsa_header header
;
70 struct opaque_lsa
*olsa
;
74 ospf_lsa_header_dump (data
);
76 olsa
= (struct opaque_lsa
*) data
;
78 opaquelen
= ntohs (data
->length
) - OSPF_LSA_HEADER_SIZE
;
79 zlog_debug ("apiserver_lsa_print: opaquelen=%d\n", opaquelen
);
81 for (i
= 0; i
< opaquelen
; i
++)
83 zlog_debug ("0x%x ", olsa
->mydata
[i
]);
88 /* -----------------------------------------------------------
90 * -----------------------------------------------------------
94 msg_new (u_char msgtype
, void *msgbody
, u_int32_t seqnum
, u_int16_t msglen
)
98 new = XCALLOC (MTYPE_OSPF_API_MSG
, sizeof (struct msg
));
100 new->hdr
.version
= OSPF_API_VERSION
;
101 new->hdr
.msgtype
= msgtype
;
102 new->hdr
.msglen
= htons (msglen
);
103 new->hdr
.msgseq
= htonl (seqnum
);
105 new->s
= stream_new (msglen
);
107 stream_put (new->s
, msgbody
, msglen
);
113 /* Duplicate a message by copying content. */
115 msg_dup (struct msg
*msg
)
121 new = msg_new (msg
->hdr
.msgtype
, STREAM_DATA (msg
->s
),
122 ntohl (msg
->hdr
.msgseq
), ntohs (msg
->hdr
.msglen
));
127 /* XXX only for testing, will be removed */
135 ospf_api_typename (int msgtype
)
137 struct nametab NameTab
[] = {
138 { MSG_REGISTER_OPAQUETYPE
, "Register opaque-type", },
139 { MSG_UNREGISTER_OPAQUETYPE
, "Unregister opaque-type", },
140 { MSG_REGISTER_EVENT
, "Register event", },
141 { MSG_SYNC_LSDB
, "Sync LSDB", },
142 { MSG_ORIGINATE_REQUEST
, "Originate request", },
143 { MSG_DELETE_REQUEST
, "Delete request", },
144 { MSG_REPLY
, "Reply", },
145 { MSG_READY_NOTIFY
, "Ready notify", },
146 { MSG_LSA_UPDATE_NOTIFY
, "LSA update notify", },
147 { MSG_LSA_DELETE_NOTIFY
, "LSA delete notify", },
148 { MSG_NEW_IF
, "New interface", },
149 { MSG_DEL_IF
, "Del interface", },
150 { MSG_ISM_CHANGE
, "ISM change", },
151 { MSG_NSM_CHANGE
, "NSM change", },
154 int i
, n
= array_size(NameTab
);
155 const char *name
= NULL
;
157 for (i
= 0; i
< n
; i
++)
159 if (NameTab
[i
].value
== msgtype
)
161 name
= NameTab
[i
].name
;
166 return name
? name
: "?";
170 ospf_api_errname (int errcode
)
172 struct nametab NameTab
[] = {
173 { OSPF_API_OK
, "OK", },
174 { OSPF_API_NOSUCHINTERFACE
, "No such interface", },
175 { OSPF_API_NOSUCHAREA
, "No such area", },
176 { OSPF_API_NOSUCHLSA
, "No such LSA", },
177 { OSPF_API_ILLEGALLSATYPE
, "Illegal LSA type", },
178 { OSPF_API_OPAQUETYPEINUSE
, "Opaque type in use", },
179 { OSPF_API_OPAQUETYPENOTREGISTERED
, "Opaque type not registered", },
180 { OSPF_API_NOTREADY
, "Not ready", },
181 { OSPF_API_NOMEMORY
, "No memory", },
182 { OSPF_API_ERROR
, "Other error", },
183 { OSPF_API_UNDEF
, "Undefined", },
186 int i
, n
= array_size(NameTab
);
187 const char *name
= NULL
;
189 for (i
= 0; i
< n
; i
++)
191 if (NameTab
[i
].value
== errcode
)
193 name
= NameTab
[i
].name
;
198 return name
? name
: "?";
202 msg_print (struct msg
*msg
)
206 zlog_debug ("msg_print msg=NULL!\n");
210 #ifdef ORIGINAL_CODING
212 ("msg=%p msgtype=%d msglen=%d msgseq=%d streamdata=%p streamsize=%lu\n",
213 msg
, msg
->hdr
.msgtype
, ntohs (msg
->hdr
.msglen
), ntohl (msg
->hdr
.msgseq
),
214 STREAM_DATA (msg
->s
), STREAM_SIZE (msg
->s
));
215 #else /* ORIGINAL_CODING */
216 /* API message common header part. */
218 ("API-msg [%s]: type(%d),len(%d),seq(%lu),data(%p),size(%zd)",
219 ospf_api_typename (msg
->hdr
.msgtype
), msg
->hdr
.msgtype
,
220 ntohs (msg
->hdr
.msglen
), (unsigned long) ntohl (msg
->hdr
.msgseq
),
221 STREAM_DATA (msg
->s
), STREAM_SIZE (msg
->s
));
223 /* API message body part. */
225 /* Generic Hex/Ascii dump */
226 DumpBuf (STREAM_DATA (msg
->s
), STREAM_SIZE (msg
->s
)); /* Sorry, deleted! */
228 /* Message-type dependent dump function. */
232 #endif /* ORIGINAL_CODING */
236 msg_free (struct msg
*msg
)
239 stream_free (msg
->s
);
241 XFREE (MTYPE_OSPF_API_MSG
, msg
);
245 /* Set sequence number of message */
247 msg_set_seq (struct msg
*msg
, u_int32_t seqnr
)
250 msg
->hdr
.msgseq
= htonl (seqnr
);
253 /* Get sequence number of message */
255 msg_get_seq (struct msg
*msg
)
258 return ntohl (msg
->hdr
.msgseq
);
261 /* -----------------------------------------------------------
262 * Message fifo queues
263 * -----------------------------------------------------------
269 return XCALLOC (MTYPE_OSPF_API_FIFO
, sizeof (struct msg_fifo
));
272 /* Add new message to fifo. */
274 msg_fifo_push (struct msg_fifo
*fifo
, struct msg
*msg
)
277 fifo
->tail
->next
= msg
;
286 /* Remove first message from fifo. */
288 msg_fifo_pop (struct msg_fifo
*fifo
)
295 fifo
->head
= msg
->next
;
297 if (fifo
->head
== NULL
)
305 /* Return first fifo entry but do not remove it. */
307 msg_fifo_head (struct msg_fifo
*fifo
)
312 /* Flush message fifo. */
314 msg_fifo_flush (struct msg_fifo
*fifo
)
319 for (op
= fifo
->head
; op
; op
= next
)
325 fifo
->head
= fifo
->tail
= NULL
;
329 /* Free API message fifo. */
331 msg_fifo_free (struct msg_fifo
*fifo
)
333 msg_fifo_flush (fifo
);
335 XFREE (MTYPE_OSPF_API_FIFO
, fifo
);
342 struct apimsghdr hdr
;
343 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
347 /* Read message header */
348 rlen
= readn (fd
, (u_char
*) &hdr
, sizeof (struct apimsghdr
));
352 zlog_warn ("msg_read: readn %s", safe_strerror (errno
));
357 zlog_warn ("msg_read: Connection closed by peer");
360 else if (rlen
!= sizeof (struct apimsghdr
))
362 zlog_warn ("msg_read: Cannot read message header!");
366 /* Check version of API protocol */
367 if (hdr
.version
!= OSPF_API_VERSION
)
369 zlog_warn ("msg_read: OSPF API protocol version mismatch");
373 /* Determine body length. */
374 bodylen
= ntohs (hdr
.msglen
);
378 /* Read message body */
379 rlen
= readn (fd
, buf
, bodylen
);
382 zlog_warn ("msg_read: readn %s", safe_strerror (errno
));
387 zlog_warn ("msg_read: Connection closed by peer");
390 else if (rlen
!= bodylen
)
392 zlog_warn ("msg_read: Cannot read message body!");
397 /* Allocate new message */
398 msg
= msg_new (hdr
.msgtype
, buf
, ntohl (hdr
.msgseq
), ntohs (hdr
.msglen
));
404 msg_write (int fd
, struct msg
*msg
)
406 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
413 /* Length of message including header */
414 l
= sizeof (struct apimsghdr
) + ntohs (msg
->hdr
.msglen
);
416 /* Make contiguous memory buffer for message */
417 memcpy (buf
, &msg
->hdr
, sizeof (struct apimsghdr
));
418 memcpy (buf
+ sizeof (struct apimsghdr
), STREAM_DATA (msg
->s
),
419 ntohs (msg
->hdr
.msglen
));
421 wlen
= writen (fd
, buf
, l
);
424 zlog_warn ("msg_write: writen %s", safe_strerror (errno
));
429 zlog_warn ("msg_write: Connection closed by peer");
434 zlog_warn ("msg_write: Cannot write API message");
440 /* -----------------------------------------------------------
442 * -----------------------------------------------------------
446 new_msg_register_opaque_type (u_int32_t seqnum
, u_char ltype
, u_char otype
)
448 struct msg_register_opaque_type rmsg
;
450 rmsg
.lsatype
= ltype
;
451 rmsg
.opaquetype
= otype
;
452 memset (&rmsg
.pad
, 0, sizeof (rmsg
.pad
));
454 return msg_new (MSG_REGISTER_OPAQUETYPE
, &rmsg
, seqnum
,
455 sizeof (struct msg_register_opaque_type
));
459 new_msg_register_event (u_int32_t seqnum
, struct lsa_filter_type
*filter
)
461 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
462 struct msg_register_event
*emsg
;
465 emsg
= (struct msg_register_event
*) buf
;
466 len
= sizeof (struct msg_register_event
) +
467 filter
->num_areas
* sizeof (struct in_addr
);
468 emsg
->filter
.typemask
= htons (filter
->typemask
);
469 emsg
->filter
.origin
= filter
->origin
;
470 emsg
->filter
.num_areas
= filter
->num_areas
;
471 if (len
> sizeof (buf
))
473 /* API broken - missing memcpy to fill data */
474 return msg_new (MSG_REGISTER_EVENT
, emsg
, seqnum
, len
);
478 new_msg_sync_lsdb (u_int32_t seqnum
, struct lsa_filter_type
*filter
)
480 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
481 struct msg_sync_lsdb
*smsg
;
484 smsg
= (struct msg_sync_lsdb
*) buf
;
485 len
= sizeof (struct msg_sync_lsdb
) +
486 filter
->num_areas
* sizeof (struct in_addr
);
487 smsg
->filter
.typemask
= htons (filter
->typemask
);
488 smsg
->filter
.origin
= filter
->origin
;
489 smsg
->filter
.num_areas
= filter
->num_areas
;
490 if (len
> sizeof (buf
))
492 /* API broken - missing memcpy to fill data */
493 return msg_new (MSG_SYNC_LSDB
, smsg
, seqnum
, len
);
498 new_msg_originate_request (u_int32_t seqnum
,
499 struct in_addr ifaddr
,
500 struct in_addr area_id
, struct lsa_header
*data
)
502 struct msg_originate_request
*omsg
;
503 unsigned int omsglen
;
504 char buf
[OSPF_API_MAX_MSG_SIZE
];
506 omsg
= (struct msg_originate_request
*) buf
;
507 omsg
->ifaddr
= ifaddr
;
508 omsg
->area_id
= area_id
;
510 omsglen
= ntohs (data
->length
);
511 if (omsglen
> sizeof (buf
) - offsetof (struct msg_originate_request
, data
))
512 omsglen
= sizeof (buf
) - offsetof (struct msg_originate_request
, data
);
513 memcpy (&omsg
->data
, data
, omsglen
);
514 omsglen
+= sizeof (struct msg_originate_request
) - sizeof (struct lsa_header
);
516 return msg_new (MSG_ORIGINATE_REQUEST
, omsg
, seqnum
, omsglen
);
520 new_msg_delete_request (u_int32_t seqnum
,
521 struct in_addr area_id
, u_char lsa_type
,
522 u_char opaque_type
, u_int32_t opaque_id
)
524 struct msg_delete_request dmsg
;
525 dmsg
.area_id
= area_id
;
526 dmsg
.lsa_type
= lsa_type
;
527 dmsg
.opaque_type
= opaque_type
;
528 dmsg
.opaque_id
= htonl (opaque_id
);
529 memset (&dmsg
.pad
, 0, sizeof (dmsg
.pad
));
531 return msg_new (MSG_DELETE_REQUEST
, &dmsg
, seqnum
,
532 sizeof (struct msg_delete_request
));
537 new_msg_reply (u_int32_t seqnr
, u_char rc
)
540 struct msg_reply rmsg
;
542 /* Set return code */
544 memset (&rmsg
.pad
, 0, sizeof (rmsg
.pad
));
546 msg
= msg_new (MSG_REPLY
, &rmsg
, seqnr
, sizeof (struct msg_reply
));
552 new_msg_ready_notify (u_int32_t seqnr
, u_char lsa_type
,
553 u_char opaque_type
, struct in_addr addr
)
555 struct msg_ready_notify rmsg
;
557 rmsg
.lsa_type
= lsa_type
;
558 rmsg
.opaque_type
= opaque_type
;
559 memset (&rmsg
.pad
, 0, sizeof (rmsg
.pad
));
562 return msg_new (MSG_READY_NOTIFY
, &rmsg
, seqnr
,
563 sizeof (struct msg_ready_notify
));
567 new_msg_new_if (u_int32_t seqnr
,
568 struct in_addr ifaddr
, struct in_addr area_id
)
570 struct msg_new_if nmsg
;
572 nmsg
.ifaddr
= ifaddr
;
573 nmsg
.area_id
= area_id
;
575 return msg_new (MSG_NEW_IF
, &nmsg
, seqnr
, sizeof (struct msg_new_if
));
579 new_msg_del_if (u_int32_t seqnr
, struct in_addr ifaddr
)
581 struct msg_del_if dmsg
;
583 dmsg
.ifaddr
= ifaddr
;
585 return msg_new (MSG_DEL_IF
, &dmsg
, seqnr
, sizeof (struct msg_del_if
));
589 new_msg_ism_change (u_int32_t seqnr
, struct in_addr ifaddr
,
590 struct in_addr area_id
, u_char status
)
592 struct msg_ism_change imsg
;
594 imsg
.ifaddr
= ifaddr
;
595 imsg
.area_id
= area_id
;
596 imsg
.status
= status
;
597 memset (&imsg
.pad
, 0, sizeof (imsg
.pad
));
599 return msg_new (MSG_ISM_CHANGE
, &imsg
, seqnr
,
600 sizeof (struct msg_ism_change
));
604 new_msg_nsm_change (u_int32_t seqnr
, struct in_addr ifaddr
,
605 struct in_addr nbraddr
,
606 struct in_addr router_id
, u_char status
)
608 struct msg_nsm_change nmsg
;
610 nmsg
.ifaddr
= ifaddr
;
611 nmsg
.nbraddr
= nbraddr
;
612 nmsg
.router_id
= router_id
;
613 nmsg
.status
= status
;
614 memset (&nmsg
.pad
, 0, sizeof (nmsg
.pad
));
616 return msg_new (MSG_NSM_CHANGE
, &nmsg
, seqnr
,
617 sizeof (struct msg_nsm_change
));
621 new_msg_lsa_change_notify (u_char msgtype
,
623 struct in_addr ifaddr
,
624 struct in_addr area_id
,
625 u_char is_self_originated
, struct lsa_header
*data
)
627 u_char buf
[OSPF_API_MAX_MSG_SIZE
];
628 struct msg_lsa_change_notify
*nmsg
;
633 nmsg
= (struct msg_lsa_change_notify
*) buf
;
634 nmsg
->ifaddr
= ifaddr
;
635 nmsg
->area_id
= area_id
;
636 nmsg
->is_self_originated
= is_self_originated
;
637 memset (&nmsg
->pad
, 0, sizeof (nmsg
->pad
));
639 len
= ntohs (data
->length
);
640 if (len
> sizeof (buf
) - offsetof (struct msg_lsa_change_notify
, data
))
641 len
= sizeof (buf
) - offsetof (struct msg_lsa_change_notify
, data
);
642 memcpy (&nmsg
->data
, data
, len
);
643 len
+= sizeof (struct msg_lsa_change_notify
) - sizeof (struct lsa_header
);
645 return msg_new (msgtype
, nmsg
, seqnum
, len
);
648 #endif /* SUPPORT_OSPF_API */