]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_api.c
Merge pull request #549 from qlyoung/fix-poll-eof
[mirror_frr.git] / ospfd / ospf_api.c
1 /*
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 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
20 */
21
22 #include <zebra.h>
23
24 #ifdef SUPPORT_OSPF_API
25
26 #include "linklist.h"
27 #include "prefix.h"
28 #include "if.h"
29 #include "table.h"
30 #include "memory.h"
31 #include "command.h"
32 #include "vty.h"
33 #include "stream.h"
34 #include "log.h"
35 #include "thread.h"
36 #include "hash.h"
37 #include "sockunion.h" /* for inet_aton() */
38 #include "buffer.h"
39 #include "network.h"
40
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"
56
57 #include "ospfd/ospf_api.h"
58
59
60 /* For debugging only, will be removed */
61 void
62 api_opaque_lsa_print (struct lsa_header *data)
63 {
64 struct opaque_lsa
65 {
66 struct lsa_header header;
67 u_char mydata[];
68 };
69
70 struct opaque_lsa *olsa;
71 int opaquelen;
72 int i;
73
74 ospf_lsa_header_dump (data);
75
76 olsa = (struct opaque_lsa *) data;
77
78 opaquelen = ntohs (data->length) - OSPF_LSA_HEADER_SIZE;
79 zlog_debug ("apiserver_lsa_print: opaquelen=%d\n", opaquelen);
80
81 for (i = 0; i < opaquelen; i++)
82 {
83 zlog_debug ("0x%x ", olsa->mydata[i]);
84 }
85 zlog_debug ("\n");
86 }
87
88 /* -----------------------------------------------------------
89 * Generic messages
90 * -----------------------------------------------------------
91 */
92
93 struct msg *
94 msg_new (u_char msgtype, void *msgbody, u_int32_t seqnum, u_int16_t msglen)
95 {
96 struct msg *new;
97
98 new = XCALLOC (MTYPE_OSPF_API_MSG, sizeof (struct msg));
99
100 new->hdr.version = OSPF_API_VERSION;
101 new->hdr.msgtype = msgtype;
102 new->hdr.msglen = htons (msglen);
103 new->hdr.msgseq = htonl (seqnum);
104
105 new->s = stream_new (msglen);
106 assert (new->s);
107 stream_put (new->s, msgbody, msglen);
108
109 return new;
110 }
111
112
113 /* Duplicate a message by copying content. */
114 struct msg *
115 msg_dup (struct msg *msg)
116 {
117 struct msg *new;
118
119 assert (msg);
120
121 new = msg_new (msg->hdr.msgtype, STREAM_DATA (msg->s),
122 ntohl (msg->hdr.msgseq), ntohs (msg->hdr.msglen));
123 return new;
124 }
125
126
127 /* XXX only for testing, will be removed */
128
129 struct nametab {
130 int value;
131 const char *name;
132 };
133
134 const char *
135 ospf_api_typename (int msgtype)
136 {
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", },
152 };
153
154 int i, n = array_size(NameTab);
155 const char *name = NULL;
156
157 for (i = 0; i < n; i++)
158 {
159 if (NameTab[i].value == msgtype)
160 {
161 name = NameTab[i].name;
162 break;
163 }
164 }
165
166 return name ? name : "?";
167 }
168
169 const char *
170 ospf_api_errname (int errcode)
171 {
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", },
184 };
185
186 int i, n = array_size(NameTab);
187 const char *name = NULL;
188
189 for (i = 0; i < n; i++)
190 {
191 if (NameTab[i].value == errcode)
192 {
193 name = NameTab[i].name;
194 break;
195 }
196 }
197
198 return name ? name : "?";
199 }
200
201 void
202 msg_print (struct msg *msg)
203 {
204 if (!msg)
205 {
206 zlog_debug ("msg_print msg=NULL!\n");
207 return;
208 }
209
210 #ifdef ORIGINAL_CODING
211 zlog_debug
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. */
217 zlog_debug
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));
222
223 /* API message body part. */
224 #ifdef ndef
225 /* Generic Hex/Ascii dump */
226 DumpBuf (STREAM_DATA (msg->s), STREAM_SIZE (msg->s)); /* Sorry, deleted! */
227 #else /* ndef */
228 /* Message-type dependent dump function. */
229 #endif /* ndef */
230
231 return;
232 #endif /* ORIGINAL_CODING */
233 }
234
235 void
236 msg_free (struct msg *msg)
237 {
238 if (msg->s)
239 stream_free (msg->s);
240
241 XFREE (MTYPE_OSPF_API_MSG, msg);
242 }
243
244
245 /* Set sequence number of message */
246 void
247 msg_set_seq (struct msg *msg, u_int32_t seqnr)
248 {
249 assert (msg);
250 msg->hdr.msgseq = htonl (seqnr);
251 }
252
253 /* Get sequence number of message */
254 u_int32_t
255 msg_get_seq (struct msg *msg)
256 {
257 assert (msg);
258 return ntohl (msg->hdr.msgseq);
259 }
260
261 /* -----------------------------------------------------------
262 * Message fifo queues
263 * -----------------------------------------------------------
264 */
265
266 struct msg_fifo *
267 msg_fifo_new ()
268 {
269 return XCALLOC (MTYPE_OSPF_API_FIFO, sizeof (struct msg_fifo));
270 }
271
272 /* Add new message to fifo. */
273 void
274 msg_fifo_push (struct msg_fifo *fifo, struct msg *msg)
275 {
276 if (fifo->tail)
277 fifo->tail->next = msg;
278 else
279 fifo->head = msg;
280
281 fifo->tail = msg;
282 fifo->count++;
283 }
284
285
286 /* Remove first message from fifo. */
287 struct msg *
288 msg_fifo_pop (struct msg_fifo *fifo)
289 {
290 struct msg *msg;
291
292 msg = fifo->head;
293 if (msg)
294 {
295 fifo->head = msg->next;
296
297 if (fifo->head == NULL)
298 fifo->tail = NULL;
299
300 fifo->count--;
301 }
302 return msg;
303 }
304
305 /* Return first fifo entry but do not remove it. */
306 struct msg *
307 msg_fifo_head (struct msg_fifo *fifo)
308 {
309 return fifo->head;
310 }
311
312 /* Flush message fifo. */
313 void
314 msg_fifo_flush (struct msg_fifo *fifo)
315 {
316 struct msg *op;
317 struct msg *next;
318
319 for (op = fifo->head; op; op = next)
320 {
321 next = op->next;
322 msg_free (op);
323 }
324
325 fifo->head = fifo->tail = NULL;
326 fifo->count = 0;
327 }
328
329 /* Free API message fifo. */
330 void
331 msg_fifo_free (struct msg_fifo *fifo)
332 {
333 msg_fifo_flush (fifo);
334
335 XFREE (MTYPE_OSPF_API_FIFO, fifo);
336 }
337
338 struct msg *
339 msg_read (int fd)
340 {
341 struct msg *msg;
342 struct apimsghdr hdr;
343 u_char buf[OSPF_API_MAX_MSG_SIZE];
344 int bodylen;
345 int rlen;
346
347 /* Read message header */
348 rlen = readn (fd, (u_char *) &hdr, sizeof (struct apimsghdr));
349
350 if (rlen < 0)
351 {
352 zlog_warn ("msg_read: readn %s", safe_strerror (errno));
353 return NULL;
354 }
355 else if (rlen == 0)
356 {
357 zlog_warn ("msg_read: Connection closed by peer");
358 return NULL;
359 }
360 else if (rlen != sizeof (struct apimsghdr))
361 {
362 zlog_warn ("msg_read: Cannot read message header!");
363 return NULL;
364 }
365
366 /* Check version of API protocol */
367 if (hdr.version != OSPF_API_VERSION)
368 {
369 zlog_warn ("msg_read: OSPF API protocol version mismatch");
370 return NULL;
371 }
372
373 /* Determine body length. */
374 bodylen = ntohs (hdr.msglen);
375 if (bodylen > 0)
376 {
377
378 /* Read message body */
379 rlen = readn (fd, buf, bodylen);
380 if (rlen < 0)
381 {
382 zlog_warn ("msg_read: readn %s", safe_strerror (errno));
383 return NULL;
384 }
385 else if (rlen == 0)
386 {
387 zlog_warn ("msg_read: Connection closed by peer");
388 return NULL;
389 }
390 else if (rlen != bodylen)
391 {
392 zlog_warn ("msg_read: Cannot read message body!");
393 return NULL;
394 }
395 }
396
397 /* Allocate new message */
398 msg = msg_new (hdr.msgtype, buf, ntohl (hdr.msgseq), ntohs (hdr.msglen));
399
400 return msg;
401 }
402
403 int
404 msg_write (int fd, struct msg *msg)
405 {
406 u_char buf[OSPF_API_MAX_MSG_SIZE];
407 int l;
408 int wlen;
409
410 assert (msg);
411 assert (msg->s);
412
413 /* Length of message including header */
414 l = sizeof (struct apimsghdr) + ntohs (msg->hdr.msglen);
415
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));
420
421 wlen = writen (fd, buf, l);
422 if (wlen < 0)
423 {
424 zlog_warn ("msg_write: writen %s", safe_strerror (errno));
425 return -1;
426 }
427 else if (wlen == 0)
428 {
429 zlog_warn ("msg_write: Connection closed by peer");
430 return -1;
431 }
432 else if (wlen != l)
433 {
434 zlog_warn ("msg_write: Cannot write API message");
435 return -1;
436 }
437 return 0;
438 }
439
440 /* -----------------------------------------------------------
441 * Specific messages
442 * -----------------------------------------------------------
443 */
444
445 struct msg *
446 new_msg_register_opaque_type (u_int32_t seqnum, u_char ltype, u_char otype)
447 {
448 struct msg_register_opaque_type rmsg;
449
450 rmsg.lsatype = ltype;
451 rmsg.opaquetype = otype;
452 memset (&rmsg.pad, 0, sizeof (rmsg.pad));
453
454 return msg_new (MSG_REGISTER_OPAQUETYPE, &rmsg, seqnum,
455 sizeof (struct msg_register_opaque_type));
456 }
457
458 struct msg *
459 new_msg_register_event (u_int32_t seqnum, struct lsa_filter_type *filter)
460 {
461 u_char buf[OSPF_API_MAX_MSG_SIZE];
462 struct msg_register_event *emsg;
463 unsigned int len;
464
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))
472 len = sizeof(buf);
473 /* API broken - missing memcpy to fill data */
474 return msg_new (MSG_REGISTER_EVENT, emsg, seqnum, len);
475 }
476
477 struct msg *
478 new_msg_sync_lsdb (u_int32_t seqnum, struct lsa_filter_type *filter)
479 {
480 u_char buf[OSPF_API_MAX_MSG_SIZE];
481 struct msg_sync_lsdb *smsg;
482 unsigned int len;
483
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))
491 len = sizeof(buf);
492 /* API broken - missing memcpy to fill data */
493 return msg_new (MSG_SYNC_LSDB, smsg, seqnum, len);
494 }
495
496
497 struct msg *
498 new_msg_originate_request (u_int32_t seqnum,
499 struct in_addr ifaddr,
500 struct in_addr area_id, struct lsa_header *data)
501 {
502 struct msg_originate_request *omsg;
503 unsigned int omsglen;
504 char buf[OSPF_API_MAX_MSG_SIZE];
505
506 omsg = (struct msg_originate_request *) buf;
507 omsg->ifaddr = ifaddr;
508 omsg->area_id = area_id;
509
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);
515
516 return msg_new (MSG_ORIGINATE_REQUEST, omsg, seqnum, omsglen);
517 }
518
519 struct msg *
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)
523 {
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));
530
531 return msg_new (MSG_DELETE_REQUEST, &dmsg, seqnum,
532 sizeof (struct msg_delete_request));
533 }
534
535
536 struct msg *
537 new_msg_reply (u_int32_t seqnr, u_char rc)
538 {
539 struct msg *msg;
540 struct msg_reply rmsg;
541
542 /* Set return code */
543 rmsg.errcode = rc;
544 memset (&rmsg.pad, 0, sizeof (rmsg.pad));
545
546 msg = msg_new (MSG_REPLY, &rmsg, seqnr, sizeof (struct msg_reply));
547
548 return msg;
549 }
550
551 struct msg *
552 new_msg_ready_notify (u_int32_t seqnr, u_char lsa_type,
553 u_char opaque_type, struct in_addr addr)
554 {
555 struct msg_ready_notify rmsg;
556
557 rmsg.lsa_type = lsa_type;
558 rmsg.opaque_type = opaque_type;
559 memset (&rmsg.pad, 0, sizeof (rmsg.pad));
560 rmsg.addr = addr;
561
562 return msg_new (MSG_READY_NOTIFY, &rmsg, seqnr,
563 sizeof (struct msg_ready_notify));
564 }
565
566 struct msg *
567 new_msg_new_if (u_int32_t seqnr,
568 struct in_addr ifaddr, struct in_addr area_id)
569 {
570 struct msg_new_if nmsg;
571
572 nmsg.ifaddr = ifaddr;
573 nmsg.area_id = area_id;
574
575 return msg_new (MSG_NEW_IF, &nmsg, seqnr, sizeof (struct msg_new_if));
576 }
577
578 struct msg *
579 new_msg_del_if (u_int32_t seqnr, struct in_addr ifaddr)
580 {
581 struct msg_del_if dmsg;
582
583 dmsg.ifaddr = ifaddr;
584
585 return msg_new (MSG_DEL_IF, &dmsg, seqnr, sizeof (struct msg_del_if));
586 }
587
588 struct msg *
589 new_msg_ism_change (u_int32_t seqnr, struct in_addr ifaddr,
590 struct in_addr area_id, u_char status)
591 {
592 struct msg_ism_change imsg;
593
594 imsg.ifaddr = ifaddr;
595 imsg.area_id = area_id;
596 imsg.status = status;
597 memset (&imsg.pad, 0, sizeof (imsg.pad));
598
599 return msg_new (MSG_ISM_CHANGE, &imsg, seqnr,
600 sizeof (struct msg_ism_change));
601 }
602
603 struct msg *
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)
607 {
608 struct msg_nsm_change nmsg;
609
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));
615
616 return msg_new (MSG_NSM_CHANGE, &nmsg, seqnr,
617 sizeof (struct msg_nsm_change));
618 }
619
620 struct msg *
621 new_msg_lsa_change_notify (u_char msgtype,
622 u_int32_t seqnum,
623 struct in_addr ifaddr,
624 struct in_addr area_id,
625 u_char is_self_originated, struct lsa_header *data)
626 {
627 u_char buf[OSPF_API_MAX_MSG_SIZE];
628 struct msg_lsa_change_notify *nmsg;
629 unsigned int len;
630
631 assert (data);
632
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));
638
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);
644
645 return msg_new (msgtype, nmsg, seqnum, len);
646 }
647
648 #endif /* SUPPORT_OSPF_API */