blob: 9c9997bad1e6cd22d8115ae02b77dab0e8d75c48 [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
102 new = XMALLOC (MTYPE_OSPF_API_MSG, sizeof (struct msg));
103 memset (new, 0, sizeof (struct msg));
104
105 new->hdr.version = OSPF_API_VERSION;
106 new->hdr.msgtype = msgtype;
107 new->hdr.msglen = htons (msglen);
108 new->hdr.msgseq = htonl (seqnum);
109
110 new->s = stream_new (msglen);
111 assert (new->s);
112 stream_put (new->s, msgbody, msglen);
113
114 return new;
115}
116
117
118/* Duplicate a message by copying content. */
119struct msg *
120msg_dup (struct msg *msg)
121{
122 struct msg *new;
123
124 assert (msg);
125
126 new = msg_new (msg->hdr.msgtype, STREAM_DATA (msg->s),
127 ntohl (msg->hdr.msgseq), ntohs (msg->hdr.msglen));
128 return new;
129}
130
131
132/* XXX only for testing, will be removed */
133
134struct nametab {
135 int value;
136 const char *name;
137};
138
139const char *
140ospf_api_typename (int msgtype)
141{
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", },
157 };
158
159 int i, n = sizeof (NameTab) / sizeof (NameTab[0]);
160 const char *name = NULL;
161
162 for (i = 0; i < n; i++)
163 {
164 if (NameTab[i].value == msgtype)
165 {
166 name = NameTab[i].name;
167 break;
168 }
169 }
170
171 return name ? name : "?";
172}
173
174const char *
175ospf_api_errname (int errcode)
176{
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", },
189 };
190
191 int i, n = sizeof (NameTab) / sizeof (NameTab[0]);
192 const char *name = NULL;
193
194 for (i = 0; i < n; i++)
195 {
196 if (NameTab[i].value == errcode)
197 {
198 name = NameTab[i].name;
199 break;
200 }
201 }
202
203 return name ? name : "?";
204}
205
206void
207msg_print (struct msg *msg)
208{
209 if (!msg)
210 {
ajse84cc642004-12-08 17:28:56 +0000211 zlog_debug ("msg_print msg=NULL!\n");
paul2d33f152003-03-17 01:10:58 +0000212 return;
213 }
214
215#ifdef ORIGINAL_CODING
ajse84cc642004-12-08 17:28:56 +0000216 zlog_debug
paul2d33f152003-03-17 01:10:58 +0000217 ("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. */
ajse84cc642004-12-08 17:28:56 +0000222 zlog_debug
paul2d33f152003-03-17 01:10:58 +0000223 ("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));
227
228 /* API message body part. */
229#ifdef ndef
230 /* Generic Hex/Ascii dump */
231 DumpBuf (STREAM_DATA (msg->s), STREAM_SIZE (msg->s)); /* Sorry, deleted! */
232#else /* ndef */
233 /* Message-type dependent dump function. */
234#endif /* ndef */
235
236 return;
237#endif /* ORIGINAL_CODING */
238}
239
240void
241msg_free (struct msg *msg)
242{
243 if (msg->s)
244 stream_free (msg->s);
245
246 XFREE (MTYPE_OSPF_API_MSG, msg);
247}
248
249
250/* Set sequence number of message */
251void
252msg_set_seq (struct msg *msg, u_int32_t seqnr)
253{
254 assert (msg);
255 msg->hdr.msgseq = htonl (seqnr);
256}
257
258/* Get sequence number of message */
259u_int32_t
260msg_get_seq (struct msg *msg)
261{
262 assert (msg);
263 return ntohl (msg->hdr.msgseq);
264}
265
266/* -----------------------------------------------------------
267 * Message fifo queues
268 * -----------------------------------------------------------
269 */
270
271struct msg_fifo *
272msg_fifo_new ()
273{
274 struct msg_fifo *new;
275
276 new = XMALLOC (MTYPE_OSPF_API_FIFO, sizeof (struct msg_fifo));
277 memset (new, 0, sizeof (struct msg_fifo));
278
279 return new;
280}
281
282/* Add new message to fifo. */
283void
284msg_fifo_push (struct msg_fifo *fifo, struct msg *msg)
285{
286 if (fifo->tail)
287 fifo->tail->next = msg;
288 else
289 fifo->head = msg;
290
291 fifo->tail = msg;
292 fifo->count++;
293}
294
295
296/* Remove first message from fifo. */
297struct msg *
298msg_fifo_pop (struct msg_fifo *fifo)
299{
300 struct msg *msg;
301
302 msg = fifo->head;
303 if (msg)
304 {
305 fifo->head = msg->next;
306
307 if (fifo->head == NULL)
308 fifo->tail = NULL;
309
310 fifo->count--;
311 }
312 return msg;
313}
314
315/* Return first fifo entry but do not remove it. */
316struct msg *
317msg_fifo_head (struct msg_fifo *fifo)
318{
319 return fifo->head;
320}
321
322/* Flush message fifo. */
323void
324msg_fifo_flush (struct msg_fifo *fifo)
325{
326 struct msg *op;
327 struct msg *next;
328
329 for (op = fifo->head; op; op = next)
330 {
331 next = op->next;
332 msg_free (op);
333 }
334
335 fifo->head = fifo->tail = NULL;
336 fifo->count = 0;
337}
338
339/* Free API message fifo. */
340void
341msg_fifo_free (struct msg_fifo *fifo)
342{
343 msg_fifo_flush (fifo);
344
345 XFREE (MTYPE_OSPF_API_FIFO, fifo);
346}
347
348struct msg *
349msg_read (int fd)
350{
351 struct msg *msg;
352 struct apimsghdr hdr;
paul36238142005-10-11 04:12:54 +0000353 u_char buf[OSPF_API_MAX_MSG_SIZE];
paul2d33f152003-03-17 01:10:58 +0000354 int bodylen;
355 int rlen;
356
357 /* Read message header */
paul36238142005-10-11 04:12:54 +0000358 rlen = readn (fd, (u_char *) &hdr, sizeof (struct apimsghdr));
paul2d33f152003-03-17 01:10:58 +0000359
360 if (rlen < 0)
361 {
ajs6099b3b2004-11-20 02:06:59 +0000362 zlog_warn ("msg_read: readn %s", safe_strerror (errno));
paul2d33f152003-03-17 01:10:58 +0000363 return NULL;
364 }
365 else if (rlen == 0)
366 {
367 zlog_warn ("msg_read: Connection closed by peer");
368 return NULL;
369 }
370 else if (rlen != sizeof (struct apimsghdr))
371 {
372 zlog_warn ("msg_read: Cannot read message header!");
373 return NULL;
374 }
375
376 /* Check version of API protocol */
377 if (hdr.version != OSPF_API_VERSION)
378 {
379 zlog_warn ("msg_read: OSPF API protocol version mismatch");
380 return NULL;
381 }
382
383 /* Determine body length. */
384 bodylen = ntohs (hdr.msglen);
385 if (bodylen > 0)
386 {
387
388 /* Read message body */
389 rlen = readn (fd, buf, bodylen);
390 if (rlen < 0)
391 {
ajs6099b3b2004-11-20 02:06:59 +0000392 zlog_warn ("msg_read: readn %s", safe_strerror (errno));
paul2d33f152003-03-17 01:10:58 +0000393 return NULL;
394 }
395 else if (rlen == 0)
396 {
397 zlog_warn ("msg_read: Connection closed by peer");
398 return NULL;
399 }
400 else if (rlen != bodylen)
401 {
402 zlog_warn ("msg_read: Cannot read message body!");
403 return NULL;
404 }
405 }
406
407 /* Allocate new message */
408 msg = msg_new (hdr.msgtype, buf, ntohl (hdr.msgseq), ntohs (hdr.msglen));
409
410 return msg;
411}
412
413int
414msg_write (int fd, struct msg *msg)
415{
416 u_char buf[OSPF_API_MAX_MSG_SIZE];
417 int l;
418 int wlen;
419
420 assert (msg);
421 assert (msg->s);
422
423 /* Length of message including header */
424 l = sizeof (struct apimsghdr) + ntohs (msg->hdr.msglen);
425
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));
430
431 wlen = writen (fd, buf, l);
432 if (wlen < 0)
433 {
ajs6099b3b2004-11-20 02:06:59 +0000434 zlog_warn ("msg_write: writen %s", safe_strerror (errno));
paul2d33f152003-03-17 01:10:58 +0000435 return -1;
436 }
437 else if (wlen == 0)
438 {
439 zlog_warn ("msg_write: Connection closed by peer");
440 return -1;
441 }
442 else if (wlen != l)
443 {
444 zlog_warn ("msg_write: Cannot write API message");
445 return -1;
446 }
447 return 0;
448}
449
450/* -----------------------------------------------------------
451 * Specific messages
452 * -----------------------------------------------------------
453 */
454
455struct msg *
456new_msg_register_opaque_type (u_int32_t seqnum, u_char ltype, u_char otype)
457{
458 struct msg_register_opaque_type rmsg;
459
460 rmsg.lsatype = ltype;
461 rmsg.opaquetype = otype;
462 memset (&rmsg.pad, 0, sizeof (rmsg.pad));
463
464 return msg_new (MSG_REGISTER_OPAQUETYPE, &rmsg, seqnum,
465 sizeof (struct msg_register_opaque_type));
466}
467
468struct msg *
469new_msg_register_event (u_int32_t seqnum, struct lsa_filter_type *filter)
470{
471 u_char buf[OSPF_API_MAX_MSG_SIZE];
472 struct msg_register_event *emsg;
473 int len;
474
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);
482}
483
484struct msg *
485new_msg_sync_lsdb (u_int32_t seqnum, struct lsa_filter_type *filter)
486{
487 u_char buf[OSPF_API_MAX_MSG_SIZE];
488 struct msg_sync_lsdb *smsg;
489 int len;
490
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);
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;
507 int omsglen;
508 char buf[OSPF_API_MAX_MSG_SIZE];
509
510 omsglen = sizeof (struct msg_originate_request) - sizeof (struct lsa_header)
511 + ntohs (data->length);
512
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));
517
518 return msg_new (MSG_ORIGINATE_REQUEST, omsg, seqnum, omsglen);
519}
520
521struct msg *
522new_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)
525{
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));
532
533 return msg_new (MSG_DELETE_REQUEST, &dmsg, seqnum,
534 sizeof (struct msg_delete_request));
535}
536
537
538struct msg *
539new_msg_reply (u_int32_t seqnr, u_char rc)
540{
541 struct msg *msg;
542 struct msg_reply rmsg;
543
544 /* Set return code */
545 rmsg.errcode = rc;
546 memset (&rmsg.pad, 0, sizeof (rmsg.pad));
547
548 msg = msg_new (MSG_REPLY, &rmsg, seqnr, sizeof (struct msg_reply));
549
550 return msg;
551}
552
553struct msg *
554new_msg_ready_notify (u_int32_t seqnr, u_char lsa_type,
555 u_char opaque_type, struct in_addr addr)
556{
557 struct msg_ready_notify rmsg;
558
559 rmsg.lsa_type = lsa_type;
560 rmsg.opaque_type = opaque_type;
561 memset (&rmsg.pad, 0, sizeof (rmsg.pad));
562 rmsg.addr = addr;
563
564 return msg_new (MSG_READY_NOTIFY, &rmsg, seqnr,
565 sizeof (struct msg_ready_notify));
566}
567
568struct msg *
569new_msg_new_if (u_int32_t seqnr,
570 struct in_addr ifaddr, struct in_addr area_id)
571{
572 struct msg_new_if nmsg;
573
574 nmsg.ifaddr = ifaddr;
575 nmsg.area_id = area_id;
576
577 return msg_new (MSG_NEW_IF, &nmsg, seqnr, sizeof (struct msg_new_if));
578}
579
580struct msg *
581new_msg_del_if (u_int32_t seqnr, struct in_addr ifaddr)
582{
583 struct msg_del_if dmsg;
584
585 dmsg.ifaddr = ifaddr;
586
587 return msg_new (MSG_DEL_IF, &dmsg, seqnr, sizeof (struct msg_del_if));
588}
589
590struct msg *
591new_msg_ism_change (u_int32_t seqnr, struct in_addr ifaddr,
592 struct in_addr area_id, u_char status)
593{
594 struct msg_ism_change imsg;
595
596 imsg.ifaddr = ifaddr;
597 imsg.area_id = area_id;
598 imsg.status = status;
599 memset (&imsg.pad, 0, sizeof (imsg.pad));
600
601 return msg_new (MSG_ISM_CHANGE, &imsg, seqnr,
602 sizeof (struct msg_ism_change));
603}
604
605struct msg *
606new_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)
609{
610 struct msg_nsm_change nmsg;
611
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));
617
618 return msg_new (MSG_NSM_CHANGE, &nmsg, seqnr,
619 sizeof (struct msg_nsm_change));
620}
621
622struct msg *
623new_msg_lsa_change_notify (u_char msgtype,
624 u_int32_t seqnum,
625 struct in_addr ifaddr,
626 struct in_addr area_id,
627 u_char is_self_originated, struct lsa_header *data)
628{
629 u_char buf[OSPF_API_MAX_MSG_SIZE];
630 struct msg_lsa_change_notify *nmsg;
631 int len;
632
633 assert (data);
634
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));
643
644 return msg_new (msgtype, nmsg, seqnum, len);
645}
646
647#endif /* SUPPORT_OSPF_API */