blob: cbb234ae6bf505ef945fc12e86990f4e06ecd5e2 [file] [log] [blame]
paul2d33f152003-03-17 01:10:58 +00001/*
2 * API message handling module for OSPF daemon and client.
3 * Copyright (C) 2001, 2002 Ralph Keller
4 *
5 * This file is part of GNU Zebra.
6 *
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.
11 *
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.
16 *
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.
21 */
22
23#include <zebra.h>
24
25#ifdef SUPPORT_OSPF_API
26#ifndef HAVE_OPAQUE_LSA
27#error "Core Opaque-LSA module must be configured."
28#endif /* HAVE_OPAQUE_LSA */
29
30#include "linklist.h"
31#include "prefix.h"
32#include "if.h"
33#include "table.h"
34#include "memory.h"
35#include "command.h"
36#include "vty.h"
37#include "stream.h"
38#include "log.h"
39#include "thread.h"
40#include "hash.h"
41#include "sockunion.h" /* for inet_aton() */
42#include "buffer.h"
43#include "network.h"
44
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"
60
61#include "ospfd/ospf_api.h"
62
63
64/* For debugging only, will be removed */
65void
66api_opaque_lsa_print (struct lsa_header *data)
67{
68 struct opaque_lsa
69 {
70 struct lsa_header header;
paul00c290e2005-11-26 09:21:43 +000071 u_char mydata[];
paul2d33f152003-03-17 01:10:58 +000072 };
73
74 struct opaque_lsa *olsa;
75 int opaquelen;
76 int i;
77
78 ospf_lsa_header_dump (data);
79
80 olsa = (struct opaque_lsa *) data;
81
82 opaquelen = ntohs (data->length) - OSPF_LSA_HEADER_SIZE;
ajse84cc642004-12-08 17:28:56 +000083 zlog_debug ("apiserver_lsa_print: opaquelen=%d\n", opaquelen);
paul2d33f152003-03-17 01:10:58 +000084
85 for (i = 0; i < opaquelen; i++)
86 {
ajse84cc642004-12-08 17:28:56 +000087 zlog_debug ("0x%x ", olsa->mydata[i]);
paul2d33f152003-03-17 01:10:58 +000088 }
ajse84cc642004-12-08 17:28:56 +000089 zlog_debug ("\n");
paul2d33f152003-03-17 01:10:58 +000090}
91
92/* -----------------------------------------------------------
93 * Generic messages
94 * -----------------------------------------------------------
95 */
96
97struct msg *
98msg_new (u_char msgtype, void *msgbody, u_int32_t seqnum, u_int16_t msglen)
99{
100 struct msg *new;
101
Stephen Hemminger393deb92008-08-18 14:13:29 -0700102 new = XCALLOC (MTYPE_OSPF_API_MSG, sizeof (struct msg));
paul2d33f152003-03-17 01:10:58 +0000103
104 new->hdr.version = OSPF_API_VERSION;
105 new->hdr.msgtype = msgtype;
106 new->hdr.msglen = htons (msglen);
107 new->hdr.msgseq = htonl (seqnum);
108
109 new->s = stream_new (msglen);
110 assert (new->s);
111 stream_put (new->s, msgbody, msglen);
112
113 return new;
114}
115
116
117/* Duplicate a message by copying content. */
118struct msg *
119msg_dup (struct msg *msg)
120{
121 struct msg *new;
122
123 assert (msg);
124
125 new = msg_new (msg->hdr.msgtype, STREAM_DATA (msg->s),
126 ntohl (msg->hdr.msgseq), ntohs (msg->hdr.msglen));
127 return new;
128}
129
130
131/* XXX only for testing, will be removed */
132
133struct nametab {
134 int value;
135 const char *name;
136};
137
138const char *
139ospf_api_typename (int msgtype)
140{
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", },
156 };
157
Balaji.G837d16c2012-09-26 14:09:10 +0530158 int i, n = array_size(NameTab);
paul2d33f152003-03-17 01:10:58 +0000159 const char *name = NULL;
160
161 for (i = 0; i < n; i++)
162 {
163 if (NameTab[i].value == msgtype)
164 {
165 name = NameTab[i].name;
166 break;
167 }
168 }
169
170 return name ? name : "?";
171}
172
173const char *
174ospf_api_errname (int errcode)
175{
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", },
188 };
189
Balaji.G837d16c2012-09-26 14:09:10 +0530190 int i, n = array_size(NameTab);
paul2d33f152003-03-17 01:10:58 +0000191 const char *name = NULL;
192
193 for (i = 0; i < n; i++)
194 {
195 if (NameTab[i].value == errcode)
196 {
197 name = NameTab[i].name;
198 break;
199 }
200 }
201
202 return name ? name : "?";
203}
204
205void
206msg_print (struct msg *msg)
207{
208 if (!msg)
209 {
ajse84cc642004-12-08 17:28:56 +0000210 zlog_debug ("msg_print msg=NULL!\n");
paul2d33f152003-03-17 01:10:58 +0000211 return;
212 }
213
214#ifdef ORIGINAL_CODING
ajse84cc642004-12-08 17:28:56 +0000215 zlog_debug
paul2d33f152003-03-17 01:10:58 +0000216 ("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. */
ajse84cc642004-12-08 17:28:56 +0000221 zlog_debug
Paul Jakma072990e2011-04-11 16:28:16 +0100222 ("API-msg [%s]: type(%d),len(%d),seq(%lu),data(%p),size(%zd)",
paul2d33f152003-03-17 01:10:58 +0000223 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));
226
227 /* API message body part. */
228#ifdef ndef
229 /* Generic Hex/Ascii dump */
230 DumpBuf (STREAM_DATA (msg->s), STREAM_SIZE (msg->s)); /* Sorry, deleted! */
231#else /* ndef */
232 /* Message-type dependent dump function. */
233#endif /* ndef */
234
235 return;
236#endif /* ORIGINAL_CODING */
237}
238
239void
240msg_free (struct msg *msg)
241{
242 if (msg->s)
243 stream_free (msg->s);
244
245 XFREE (MTYPE_OSPF_API_MSG, msg);
246}
247
248
249/* Set sequence number of message */
250void
251msg_set_seq (struct msg *msg, u_int32_t seqnr)
252{
253 assert (msg);
254 msg->hdr.msgseq = htonl (seqnr);
255}
256
257/* Get sequence number of message */
258u_int32_t
259msg_get_seq (struct msg *msg)
260{
261 assert (msg);
262 return ntohl (msg->hdr.msgseq);
263}
264
265/* -----------------------------------------------------------
266 * Message fifo queues
267 * -----------------------------------------------------------
268 */
269
270struct msg_fifo *
271msg_fifo_new ()
272{
Stephen Hemminger393deb92008-08-18 14:13:29 -0700273 return XCALLOC (MTYPE_OSPF_API_FIFO, sizeof (struct msg_fifo));
paul2d33f152003-03-17 01:10:58 +0000274}
275
276/* Add new message to fifo. */
277void
278msg_fifo_push (struct msg_fifo *fifo, struct msg *msg)
279{
280 if (fifo->tail)
281 fifo->tail->next = msg;
282 else
283 fifo->head = msg;
284
285 fifo->tail = msg;
286 fifo->count++;
287}
288
289
290/* Remove first message from fifo. */
291struct msg *
292msg_fifo_pop (struct msg_fifo *fifo)
293{
294 struct msg *msg;
295
296 msg = fifo->head;
297 if (msg)
298 {
299 fifo->head = msg->next;
300
301 if (fifo->head == NULL)
302 fifo->tail = NULL;
303
304 fifo->count--;
305 }
306 return msg;
307}
308
309/* Return first fifo entry but do not remove it. */
310struct msg *
311msg_fifo_head (struct msg_fifo *fifo)
312{
313 return fifo->head;
314}
315
316/* Flush message fifo. */
317void
318msg_fifo_flush (struct msg_fifo *fifo)
319{
320 struct msg *op;
321 struct msg *next;
322
323 for (op = fifo->head; op; op = next)
324 {
325 next = op->next;
326 msg_free (op);
327 }
328
329 fifo->head = fifo->tail = NULL;
330 fifo->count = 0;
331}
332
333/* Free API message fifo. */
334void
335msg_fifo_free (struct msg_fifo *fifo)
336{
337 msg_fifo_flush (fifo);
338
339 XFREE (MTYPE_OSPF_API_FIFO, fifo);
340}
341
342struct msg *
343msg_read (int fd)
344{
345 struct msg *msg;
346 struct apimsghdr hdr;
paul36238142005-10-11 04:12:54 +0000347 u_char buf[OSPF_API_MAX_MSG_SIZE];
paul2d33f152003-03-17 01:10:58 +0000348 int bodylen;
349 int rlen;
350
351 /* Read message header */
paul36238142005-10-11 04:12:54 +0000352 rlen = readn (fd, (u_char *) &hdr, sizeof (struct apimsghdr));
paul2d33f152003-03-17 01:10:58 +0000353
354 if (rlen < 0)
355 {
ajs6099b3b2004-11-20 02:06:59 +0000356 zlog_warn ("msg_read: readn %s", safe_strerror (errno));
paul2d33f152003-03-17 01:10:58 +0000357 return NULL;
358 }
359 else if (rlen == 0)
360 {
361 zlog_warn ("msg_read: Connection closed by peer");
362 return NULL;
363 }
364 else if (rlen != sizeof (struct apimsghdr))
365 {
366 zlog_warn ("msg_read: Cannot read message header!");
367 return NULL;
368 }
369
370 /* Check version of API protocol */
371 if (hdr.version != OSPF_API_VERSION)
372 {
373 zlog_warn ("msg_read: OSPF API protocol version mismatch");
374 return NULL;
375 }
376
377 /* Determine body length. */
378 bodylen = ntohs (hdr.msglen);
379 if (bodylen > 0)
380 {
381
382 /* Read message body */
383 rlen = readn (fd, buf, bodylen);
384 if (rlen < 0)
385 {
ajs6099b3b2004-11-20 02:06:59 +0000386 zlog_warn ("msg_read: readn %s", safe_strerror (errno));
paul2d33f152003-03-17 01:10:58 +0000387 return NULL;
388 }
389 else if (rlen == 0)
390 {
391 zlog_warn ("msg_read: Connection closed by peer");
392 return NULL;
393 }
394 else if (rlen != bodylen)
395 {
396 zlog_warn ("msg_read: Cannot read message body!");
397 return NULL;
398 }
399 }
400
401 /* Allocate new message */
402 msg = msg_new (hdr.msgtype, buf, ntohl (hdr.msgseq), ntohs (hdr.msglen));
403
404 return msg;
405}
406
407int
408msg_write (int fd, struct msg *msg)
409{
410 u_char buf[OSPF_API_MAX_MSG_SIZE];
411 int l;
412 int wlen;
413
414 assert (msg);
415 assert (msg->s);
416
417 /* Length of message including header */
418 l = sizeof (struct apimsghdr) + ntohs (msg->hdr.msglen);
419
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));
424
425 wlen = writen (fd, buf, l);
426 if (wlen < 0)
427 {
ajs6099b3b2004-11-20 02:06:59 +0000428 zlog_warn ("msg_write: writen %s", safe_strerror (errno));
paul2d33f152003-03-17 01:10:58 +0000429 return -1;
430 }
431 else if (wlen == 0)
432 {
433 zlog_warn ("msg_write: Connection closed by peer");
434 return -1;
435 }
436 else if (wlen != l)
437 {
438 zlog_warn ("msg_write: Cannot write API message");
439 return -1;
440 }
441 return 0;
442}
443
444/* -----------------------------------------------------------
445 * Specific messages
446 * -----------------------------------------------------------
447 */
448
449struct msg *
450new_msg_register_opaque_type (u_int32_t seqnum, u_char ltype, u_char otype)
451{
452 struct msg_register_opaque_type rmsg;
453
454 rmsg.lsatype = ltype;
455 rmsg.opaquetype = otype;
456 memset (&rmsg.pad, 0, sizeof (rmsg.pad));
457
458 return msg_new (MSG_REGISTER_OPAQUETYPE, &rmsg, seqnum,
459 sizeof (struct msg_register_opaque_type));
460}
461
462struct msg *
463new_msg_register_event (u_int32_t seqnum, struct lsa_filter_type *filter)
464{
465 u_char buf[OSPF_API_MAX_MSG_SIZE];
466 struct msg_register_event *emsg;
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100467 size_t len;
paul2d33f152003-03-17 01:10:58 +0000468
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;
David Lamparterc51443f2013-07-08 23:05:28 +0200475 if (len > sizeof (buf))
476 len = sizeof(buf);
477 /* API broken - missing memcpy to fill data */
paul2d33f152003-03-17 01:10:58 +0000478 return msg_new (MSG_REGISTER_EVENT, emsg, seqnum, len);
479}
480
481struct msg *
482new_msg_sync_lsdb (u_int32_t seqnum, struct lsa_filter_type *filter)
483{
484 u_char buf[OSPF_API_MAX_MSG_SIZE];
485 struct msg_sync_lsdb *smsg;
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100486 size_t len;
paul2d33f152003-03-17 01:10:58 +0000487
488 smsg = (struct msg_sync_lsdb *) buf;
489 len = sizeof (struct msg_sync_lsdb) +
490 filter->num_areas * sizeof (struct in_addr);
491 smsg->filter.typemask = htons (filter->typemask);
492 smsg->filter.origin = filter->origin;
493 smsg->filter.num_areas = filter->num_areas;
David Lamparterc51443f2013-07-08 23:05:28 +0200494 if (len > sizeof (buf))
495 len = sizeof(buf);
496 /* API broken - missing memcpy to fill data */
paul2d33f152003-03-17 01:10:58 +0000497 return msg_new (MSG_SYNC_LSDB, smsg, seqnum, len);
498}
499
500
501struct msg *
502new_msg_originate_request (u_int32_t seqnum,
503 struct in_addr ifaddr,
504 struct in_addr area_id, struct lsa_header *data)
505{
506 struct msg_originate_request *omsg;
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100507 size_t omsglen;
paul2d33f152003-03-17 01:10:58 +0000508 char buf[OSPF_API_MAX_MSG_SIZE];
509
paul2d33f152003-03-17 01:10:58 +0000510 omsg = (struct msg_originate_request *) buf;
511 omsg->ifaddr = ifaddr;
512 omsg->area_id = area_id;
David Lamparterc51443f2013-07-08 23:05:28 +0200513
514 omsglen = ntohs (data->length);
515 if (omsglen > sizeof (buf) - offsetof (struct msg_originate_request, data))
516 omsglen = sizeof (buf) - offsetof (struct msg_originate_request, data);
517 memcpy (&omsg->data, data, omsglen);
518 omsglen += sizeof (struct msg_originate_request) - sizeof (struct lsa_header);
paul2d33f152003-03-17 01:10:58 +0000519
520 return msg_new (MSG_ORIGINATE_REQUEST, omsg, seqnum, omsglen);
521}
522
523struct msg *
524new_msg_delete_request (u_int32_t seqnum,
525 struct in_addr area_id, u_char lsa_type,
526 u_char opaque_type, u_int32_t opaque_id)
527{
528 struct msg_delete_request dmsg;
529 dmsg.area_id = area_id;
530 dmsg.lsa_type = lsa_type;
531 dmsg.opaque_type = opaque_type;
532 dmsg.opaque_id = htonl (opaque_id);
533 memset (&dmsg.pad, 0, sizeof (dmsg.pad));
534
535 return msg_new (MSG_DELETE_REQUEST, &dmsg, seqnum,
536 sizeof (struct msg_delete_request));
537}
538
539
540struct msg *
541new_msg_reply (u_int32_t seqnr, u_char rc)
542{
543 struct msg *msg;
544 struct msg_reply rmsg;
545
546 /* Set return code */
547 rmsg.errcode = rc;
548 memset (&rmsg.pad, 0, sizeof (rmsg.pad));
549
550 msg = msg_new (MSG_REPLY, &rmsg, seqnr, sizeof (struct msg_reply));
551
552 return msg;
553}
554
555struct msg *
556new_msg_ready_notify (u_int32_t seqnr, u_char lsa_type,
557 u_char opaque_type, struct in_addr addr)
558{
559 struct msg_ready_notify rmsg;
560
561 rmsg.lsa_type = lsa_type;
562 rmsg.opaque_type = opaque_type;
563 memset (&rmsg.pad, 0, sizeof (rmsg.pad));
564 rmsg.addr = addr;
565
566 return msg_new (MSG_READY_NOTIFY, &rmsg, seqnr,
567 sizeof (struct msg_ready_notify));
568}
569
570struct msg *
571new_msg_new_if (u_int32_t seqnr,
572 struct in_addr ifaddr, struct in_addr area_id)
573{
574 struct msg_new_if nmsg;
575
576 nmsg.ifaddr = ifaddr;
577 nmsg.area_id = area_id;
578
579 return msg_new (MSG_NEW_IF, &nmsg, seqnr, sizeof (struct msg_new_if));
580}
581
582struct msg *
583new_msg_del_if (u_int32_t seqnr, struct in_addr ifaddr)
584{
585 struct msg_del_if dmsg;
586
587 dmsg.ifaddr = ifaddr;
588
589 return msg_new (MSG_DEL_IF, &dmsg, seqnr, sizeof (struct msg_del_if));
590}
591
592struct msg *
593new_msg_ism_change (u_int32_t seqnr, struct in_addr ifaddr,
594 struct in_addr area_id, u_char status)
595{
596 struct msg_ism_change imsg;
597
598 imsg.ifaddr = ifaddr;
599 imsg.area_id = area_id;
600 imsg.status = status;
601 memset (&imsg.pad, 0, sizeof (imsg.pad));
602
603 return msg_new (MSG_ISM_CHANGE, &imsg, seqnr,
604 sizeof (struct msg_ism_change));
605}
606
607struct msg *
608new_msg_nsm_change (u_int32_t seqnr, struct in_addr ifaddr,
609 struct in_addr nbraddr,
610 struct in_addr router_id, u_char status)
611{
612 struct msg_nsm_change nmsg;
613
614 nmsg.ifaddr = ifaddr;
615 nmsg.nbraddr = nbraddr;
616 nmsg.router_id = router_id;
617 nmsg.status = status;
618 memset (&nmsg.pad, 0, sizeof (nmsg.pad));
619
620 return msg_new (MSG_NSM_CHANGE, &nmsg, seqnr,
621 sizeof (struct msg_nsm_change));
622}
623
624struct msg *
625new_msg_lsa_change_notify (u_char msgtype,
626 u_int32_t seqnum,
627 struct in_addr ifaddr,
628 struct in_addr area_id,
629 u_char is_self_originated, struct lsa_header *data)
630{
631 u_char buf[OSPF_API_MAX_MSG_SIZE];
632 struct msg_lsa_change_notify *nmsg;
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100633 size_t len;
paul2d33f152003-03-17 01:10:58 +0000634
635 assert (data);
636
637 nmsg = (struct msg_lsa_change_notify *) buf;
paul2d33f152003-03-17 01:10:58 +0000638 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));
David Lamparterc51443f2013-07-08 23:05:28 +0200642
643 len = ntohs (data->length);
644 if (len > sizeof (buf) - offsetof (struct msg_lsa_change_notify, data))
645 len = sizeof (buf) - offsetof (struct msg_lsa_change_notify, data);
646 memcpy (&nmsg->data, data, len);
647 len += sizeof (struct msg_lsa_change_notify) - sizeof (struct lsa_header);
paul2d33f152003-03-17 01:10:58 +0000648
649 return msg_new (msgtype, nmsg, seqnum, len);
650}
651
652#endif /* SUPPORT_OSPF_API */