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 */
63 api_opaque_lsa_print (struct lsa_header *data)
67 struct lsa_header header;
71 struct opaque_lsa *olsa;
75 ospf_lsa_header_dump (data);
77 olsa = (struct opaque_lsa *) data;
79 opaquelen = ntohs (data->length) - OSPF_LSA_HEADER_SIZE;
80 zlog_debug ("apiserver_lsa_print: opaquelen=%d\n", opaquelen);
82 for (i = 0; i < opaquelen; i++)
84 zlog_debug ("0x%x ", olsa->mydata[i]);
89 /* -----------------------------------------------------------
91 * -----------------------------------------------------------
95 msg_new (u_char msgtype, void *msgbody, u_int32_t seqnum, u_int16_t msglen)
99 new = XCALLOC (MTYPE_OSPF_API_MSG, sizeof (struct msg));
101 new->hdr.version = OSPF_API_VERSION;
102 new->hdr.msgtype = msgtype;
103 new->hdr.msglen = htons (msglen);
104 new->hdr.msgseq = htonl (seqnum);
106 new->s = stream_new (msglen);
108 stream_put (new->s, msgbody, msglen);
114 /* Duplicate a message by copying content. */
116 msg_dup (struct msg *msg)
122 new = msg_new (msg->hdr.msgtype, STREAM_DATA (msg->s),
123 ntohl (msg->hdr.msgseq), ntohs (msg->hdr.msglen));
128 /* XXX only for testing, will be removed */
136 ospf_api_typename (int msgtype)
138 struct nametab NameTab[] = {
139 { MSG_REGISTER_OPAQUETYPE, "Register opaque-type", },
140 { MSG_UNREGISTER_OPAQUETYPE, "Unregister opaque-type", },
141 { MSG_REGISTER_EVENT, "Register event", },
142 { MSG_SYNC_LSDB, "Sync LSDB", },
143 { MSG_ORIGINATE_REQUEST, "Originate request", },
144 { MSG_DELETE_REQUEST, "Delete request", },
145 { MSG_REPLY, "Reply", },
146 { MSG_READY_NOTIFY, "Ready notify", },
147 { MSG_LSA_UPDATE_NOTIFY, "LSA update notify", },
148 { MSG_LSA_DELETE_NOTIFY, "LSA delete notify", },
149 { MSG_NEW_IF, "New interface", },
150 { MSG_DEL_IF, "Del interface", },
151 { MSG_ISM_CHANGE, "ISM change", },
152 { MSG_NSM_CHANGE, "NSM change", },
155 int i, n = array_size(NameTab);
156 const char *name = NULL;
158 for (i = 0; i < n; i++)
160 if (NameTab[i].value == msgtype)
162 name = NameTab[i].name;
167 return name ? name : "?";
171 ospf_api_errname (int errcode)
173 struct nametab NameTab[] = {
174 { OSPF_API_OK, "OK", },
175 { OSPF_API_NOSUCHINTERFACE, "No such interface", },
176 { OSPF_API_NOSUCHAREA, "No such area", },
177 { OSPF_API_NOSUCHLSA, "No such LSA", },
178 { OSPF_API_ILLEGALLSATYPE, "Illegal LSA type", },
179 { OSPF_API_OPAQUETYPEINUSE, "Opaque type in use", },
180 { OSPF_API_OPAQUETYPENOTREGISTERED, "Opaque type not registered", },
181 { OSPF_API_NOTREADY, "Not ready", },
182 { OSPF_API_NOMEMORY, "No memory", },
183 { OSPF_API_ERROR, "Other error", },
184 { OSPF_API_UNDEF, "Undefined", },
187 int i, n = array_size(NameTab);
188 const char *name = NULL;
190 for (i = 0; i < n; i++)
192 if (NameTab[i].value == errcode)
194 name = NameTab[i].name;
199 return name ? name : "?";
203 msg_print (struct msg *msg)
207 zlog_debug ("msg_print msg=NULL!\n");
211 #ifdef ORIGINAL_CODING
213 ("msg=%p msgtype=%d msglen=%d msgseq=%d streamdata=%p streamsize=%lu\n",
214 msg, msg->hdr.msgtype, ntohs (msg->hdr.msglen), ntohl (msg->hdr.msgseq),
215 STREAM_DATA (msg->s), STREAM_SIZE (msg->s));
216 #else /* ORIGINAL_CODING */
217 /* API message common header part. */
219 ("API-msg [%s]: type(%d),len(%d),seq(%lu),data(%p),size(%zd)",
220 ospf_api_typename (msg->hdr.msgtype), msg->hdr.msgtype,
221 ntohs (msg->hdr.msglen), (unsigned long) ntohl (msg->hdr.msgseq),
222 STREAM_DATA (msg->s), STREAM_SIZE (msg->s));
224 /* API message body part. */
226 /* Generic Hex/Ascii dump */
227 DumpBuf (STREAM_DATA (msg->s), STREAM_SIZE (msg->s)); /* Sorry, deleted! */
229 /* Message-type dependent dump function. */
233 #endif /* ORIGINAL_CODING */
237 msg_free (struct msg *msg)
240 stream_free (msg->s);
242 XFREE (MTYPE_OSPF_API_MSG, msg);
246 /* Set sequence number of message */
248 msg_set_seq (struct msg *msg, u_int32_t seqnr)
251 msg->hdr.msgseq = htonl (seqnr);
254 /* Get sequence number of message */
256 msg_get_seq (struct msg *msg)
259 return ntohl (msg->hdr.msgseq);
262 /* -----------------------------------------------------------
263 * Message fifo queues
264 * -----------------------------------------------------------
270 return XCALLOC (MTYPE_OSPF_API_FIFO, sizeof (struct msg_fifo));
273 /* Add new message to fifo. */
275 msg_fifo_push (struct msg_fifo *fifo, struct msg *msg)
278 fifo->tail->next = msg;
287 /* Remove first message from fifo. */
289 msg_fifo_pop (struct msg_fifo *fifo)
296 fifo->head = msg->next;
298 if (fifo->head == NULL)
306 /* Return first fifo entry but do not remove it. */
308 msg_fifo_head (struct msg_fifo *fifo)
313 /* Flush message fifo. */
315 msg_fifo_flush (struct msg_fifo *fifo)
320 for (op = fifo->head; op; op = next)
326 fifo->head = fifo->tail = NULL;
330 /* Free API message fifo. */
332 msg_fifo_free (struct msg_fifo *fifo)
334 msg_fifo_flush (fifo);
336 XFREE (MTYPE_OSPF_API_FIFO, fifo);
343 struct apimsghdr hdr;
344 u_char buf[OSPF_API_MAX_MSG_SIZE];
348 /* Read message header */
349 rlen = readn (fd, (u_char *) &hdr, sizeof (struct apimsghdr));
353 zlog_warn ("msg_read: readn %s", safe_strerror (errno));
358 zlog_warn ("msg_read: Connection closed by peer");
361 else if (rlen != sizeof (struct apimsghdr))
363 zlog_warn ("msg_read: Cannot read message header!");
367 /* Check version of API protocol */
368 if (hdr.version != OSPF_API_VERSION)
370 zlog_warn ("msg_read: OSPF API protocol version mismatch");
374 /* Determine body length. */
375 bodylen = ntohs (hdr.msglen);
379 /* Read message body */
380 rlen = readn (fd, buf, bodylen);
383 zlog_warn ("msg_read: readn %s", safe_strerror (errno));
388 zlog_warn ("msg_read: Connection closed by peer");
391 else if (rlen != bodylen)
393 zlog_warn ("msg_read: Cannot read message body!");
398 /* Allocate new message */
399 msg = msg_new (hdr.msgtype, buf, ntohl (hdr.msgseq), ntohs (hdr.msglen));
405 msg_write (int fd, struct msg *msg)
407 u_char buf[OSPF_API_MAX_MSG_SIZE];
414 /* Length of message including header */
415 l = sizeof (struct apimsghdr) + ntohs (msg->hdr.msglen);
417 /* Make contiguous memory buffer for message */
418 memcpy (buf, &msg->hdr, sizeof (struct apimsghdr));
419 memcpy (buf + sizeof (struct apimsghdr), STREAM_DATA (msg->s),
420 ntohs (msg->hdr.msglen));
422 wlen = writen (fd, buf, l);
425 zlog_warn ("msg_write: writen %s", safe_strerror (errno));
430 zlog_warn ("msg_write: Connection closed by peer");
435 zlog_warn ("msg_write: Cannot write API message");
441 /* -----------------------------------------------------------
443 * -----------------------------------------------------------
447 new_msg_register_opaque_type (u_int32_t seqnum, u_char ltype, u_char otype)
449 struct msg_register_opaque_type rmsg;
451 rmsg.lsatype = ltype;
452 rmsg.opaquetype = otype;
453 memset (&rmsg.pad, 0, sizeof (rmsg.pad));
455 return msg_new (MSG_REGISTER_OPAQUETYPE, &rmsg, seqnum,
456 sizeof (struct msg_register_opaque_type));
460 new_msg_register_event (u_int32_t seqnum, struct lsa_filter_type *filter)
462 u_char buf[OSPF_API_MAX_MSG_SIZE];
463 struct msg_register_event *emsg;
466 emsg = (struct msg_register_event *) buf;
467 len = sizeof (struct msg_register_event) +
468 filter->num_areas * sizeof (struct in_addr);
469 emsg->filter.typemask = htons (filter->typemask);
470 emsg->filter.origin = filter->origin;
471 emsg->filter.num_areas = filter->num_areas;
472 if (len > sizeof (buf))
474 /* API broken - missing memcpy to fill data */
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 if (len > sizeof (buf))
493 /* API broken - missing memcpy to fill data */
494 return msg_new (MSG_SYNC_LSDB, smsg, seqnum, len);
499 new_msg_originate_request (u_int32_t seqnum,
500 struct in_addr ifaddr,
501 struct in_addr area_id, struct lsa_header *data)
503 struct msg_originate_request *omsg;
505 char buf[OSPF_API_MAX_MSG_SIZE];
507 omsg = (struct msg_originate_request *) buf;
508 omsg->ifaddr = ifaddr;
509 omsg->area_id = area_id;
511 omsglen = ntohs (data->length);
512 if (omsglen > sizeof (buf) - offsetof (struct msg_originate_request, data))
513 omsglen = sizeof (buf) - offsetof (struct msg_originate_request, data);
514 memcpy (&omsg->data, data, omsglen);
515 omsglen += sizeof (struct msg_originate_request) - sizeof (struct lsa_header);
517 return msg_new (MSG_ORIGINATE_REQUEST, omsg, seqnum, omsglen);
521 new_msg_delete_request (u_int32_t seqnum,
522 struct in_addr area_id, u_char lsa_type,
523 u_char opaque_type, u_int32_t opaque_id)
525 struct msg_delete_request dmsg;
526 dmsg.area_id = area_id;
527 dmsg.lsa_type = lsa_type;
528 dmsg.opaque_type = opaque_type;
529 dmsg.opaque_id = htonl (opaque_id);
530 memset (&dmsg.pad, 0, sizeof (dmsg.pad));
532 return msg_new (MSG_DELETE_REQUEST, &dmsg, seqnum,
533 sizeof (struct msg_delete_request));
538 new_msg_reply (u_int32_t seqnr, u_char rc)
541 struct msg_reply rmsg;
543 /* Set return code */
545 memset (&rmsg.pad, 0, sizeof (rmsg.pad));
547 msg = msg_new (MSG_REPLY, &rmsg, seqnr, sizeof (struct msg_reply));
553 new_msg_ready_notify (u_int32_t seqnr, u_char lsa_type,
554 u_char opaque_type, struct in_addr addr)
556 struct msg_ready_notify rmsg;
558 rmsg.lsa_type = lsa_type;
559 rmsg.opaque_type = opaque_type;
560 memset (&rmsg.pad, 0, sizeof (rmsg.pad));
563 return msg_new (MSG_READY_NOTIFY, &rmsg, seqnr,
564 sizeof (struct msg_ready_notify));
568 new_msg_new_if (u_int32_t seqnr,
569 struct in_addr ifaddr, struct in_addr area_id)
571 struct msg_new_if nmsg;
573 nmsg.ifaddr = ifaddr;
574 nmsg.area_id = area_id;
576 return msg_new (MSG_NEW_IF, &nmsg, seqnr, sizeof (struct msg_new_if));
580 new_msg_del_if (u_int32_t seqnr, struct in_addr ifaddr)
582 struct msg_del_if dmsg;
584 dmsg.ifaddr = ifaddr;
586 return msg_new (MSG_DEL_IF, &dmsg, seqnr, sizeof (struct msg_del_if));
590 new_msg_ism_change (u_int32_t seqnr, struct in_addr ifaddr,
591 struct in_addr area_id, u_char status)
593 struct msg_ism_change imsg;
595 imsg.ifaddr = ifaddr;
596 imsg.area_id = area_id;
597 imsg.status = status;
598 memset (&imsg.pad, 0, sizeof (imsg.pad));
600 return msg_new (MSG_ISM_CHANGE, &imsg, seqnr,
601 sizeof (struct msg_ism_change));
605 new_msg_nsm_change (u_int32_t seqnr, struct in_addr ifaddr,
606 struct in_addr nbraddr,
607 struct in_addr router_id, u_char status)
609 struct msg_nsm_change nmsg;
611 nmsg.ifaddr = ifaddr;
612 nmsg.nbraddr = nbraddr;
613 nmsg.router_id = router_id;
614 nmsg.status = status;
615 memset (&nmsg.pad, 0, sizeof (nmsg.pad));
617 return msg_new (MSG_NSM_CHANGE, &nmsg, seqnr,
618 sizeof (struct msg_nsm_change));
622 new_msg_lsa_change_notify (u_char msgtype,
624 struct in_addr ifaddr,
625 struct in_addr area_id,
626 u_char is_self_originated, struct lsa_header *data)
628 u_char buf[OSPF_API_MAX_MSG_SIZE];
629 struct msg_lsa_change_notify *nmsg;
634 nmsg = (struct msg_lsa_change_notify *) buf;
635 nmsg->ifaddr = ifaddr;
636 nmsg->area_id = area_id;
637 nmsg->is_self_originated = is_self_originated;
638 memset (&nmsg->pad, 0, sizeof (nmsg->pad));
640 len = ntohs (data->length);
641 if (len > sizeof (buf) - offsetof (struct msg_lsa_change_notify, data))
642 len = sizeof (buf) - offsetof (struct msg_lsa_change_notify, data);
643 memcpy (&nmsg->data, data, len);
644 len += sizeof (struct msg_lsa_change_notify) - sizeof (struct lsa_header);
646 return msg_new (msgtype, nmsg, seqnum, len);
649 #endif /* SUPPORT_OSPF_API */