]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_api.c
Merge pull request #10655 from donaldsharp/timers_warning_when_large
[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 api_opaque_lsa_print(struct ospf_lsa *lsa)
62 {
63 struct opaque_lsa {
64 struct lsa_header header;
65 uint8_t mydata[];
66 };
67
68 struct opaque_lsa *olsa;
69 int opaquelen;
70 int i;
71
72 ospf_lsa_header_dump(lsa->data);
73
74 olsa = (struct opaque_lsa *)lsa->data;
75
76 opaquelen = lsa->size - OSPF_LSA_HEADER_SIZE;
77 zlog_debug("apiserver_lsa_print: opaquelen=%d", opaquelen);
78
79 for (i = 0; i < opaquelen; i++) {
80 zlog_debug("0x%x ", olsa->mydata[i]);
81 }
82 zlog_debug(" ");
83 }
84
85 /* -----------------------------------------------------------
86 * Generic messages
87 * -----------------------------------------------------------
88 */
89
90 struct msg *msg_new(uint8_t msgtype, void *msgbody, uint32_t seqnum,
91 uint16_t msglen)
92 {
93 struct msg *new;
94
95 new = XCALLOC(MTYPE_OSPF_API_MSG, sizeof(struct msg));
96
97 new->hdr.version = OSPF_API_VERSION;
98 new->hdr.msgtype = msgtype;
99 new->hdr.msglen = htons(msglen);
100 new->hdr.msgseq = htonl(seqnum);
101
102 new->s = stream_new(msglen);
103 assert(new->s);
104 stream_put(new->s, msgbody, msglen);
105
106 return new;
107 }
108
109
110 /* Duplicate a message by copying content. */
111 struct msg *msg_dup(struct msg *msg)
112 {
113 struct msg *new;
114 size_t size;
115
116 assert(msg);
117
118 size = ntohs(msg->hdr.msglen);
119 if (size > OSPF_MAX_LSA_SIZE)
120 return NULL;
121
122 new = msg_new(msg->hdr.msgtype, STREAM_DATA(msg->s),
123 ntohl(msg->hdr.msgseq), size);
124 return new;
125 }
126
127
128 /* XXX only for testing, will be removed */
129
130 struct nametab {
131 int value;
132 const char *name;
133 };
134
135 const char *ospf_api_typename(int msgtype)
136 {
137 struct nametab NameTab[] = {
138 {
139 MSG_REGISTER_OPAQUETYPE, "Register opaque-type",
140 },
141 {
142 MSG_UNREGISTER_OPAQUETYPE, "Unregister opaque-type",
143 },
144 {
145 MSG_REGISTER_EVENT, "Register event",
146 },
147 {
148 MSG_SYNC_LSDB, "Sync LSDB",
149 },
150 {
151 MSG_ORIGINATE_REQUEST, "Originate request",
152 },
153 {
154 MSG_DELETE_REQUEST, "Delete request",
155 },
156 {
157 MSG_REPLY, "Reply",
158 },
159 {
160 MSG_READY_NOTIFY, "Ready notify",
161 },
162 {
163 MSG_LSA_UPDATE_NOTIFY, "LSA update notify",
164 },
165 {
166 MSG_LSA_DELETE_NOTIFY, "LSA delete notify",
167 },
168 {
169 MSG_NEW_IF, "New interface",
170 },
171 {
172 MSG_DEL_IF, "Del interface",
173 },
174 {
175 MSG_ISM_CHANGE, "ISM change",
176 },
177 {
178 MSG_NSM_CHANGE, "NSM change",
179 },
180 };
181
182 int i, n = array_size(NameTab);
183 const char *name = NULL;
184
185 for (i = 0; i < n; i++) {
186 if (NameTab[i].value == msgtype) {
187 name = NameTab[i].name;
188 break;
189 }
190 }
191
192 return name ? name : "?";
193 }
194
195 const char *ospf_api_errname(int errcode)
196 {
197 struct nametab NameTab[] = {
198 {
199 OSPF_API_OK, "OK",
200 },
201 {
202 OSPF_API_NOSUCHINTERFACE, "No such interface",
203 },
204 {
205 OSPF_API_NOSUCHAREA, "No such area",
206 },
207 {
208 OSPF_API_NOSUCHLSA, "No such LSA",
209 },
210 {
211 OSPF_API_ILLEGALLSATYPE, "Illegal LSA type",
212 },
213 {
214 OSPF_API_OPAQUETYPEINUSE, "Opaque type in use",
215 },
216 {
217 OSPF_API_OPAQUETYPENOTREGISTERED,
218 "Opaque type not registered",
219 },
220 {
221 OSPF_API_NOTREADY, "Not ready",
222 },
223 {
224 OSPF_API_NOMEMORY, "No memory",
225 },
226 {
227 OSPF_API_ERROR, "Other error",
228 },
229 {
230 OSPF_API_UNDEF, "Undefined",
231 },
232 };
233
234 int i, n = array_size(NameTab);
235 const char *name = NULL;
236
237 for (i = 0; i < n; i++) {
238 if (NameTab[i].value == errcode) {
239 name = NameTab[i].name;
240 break;
241 }
242 }
243
244 return name ? name : "?";
245 }
246
247 void msg_print(struct msg *msg)
248 {
249 if (!msg) {
250 zlog_debug("msg_print msg=NULL!");
251 return;
252 }
253
254 /* API message common header part. */
255 zlog_debug("API-msg [%s]: type(%d),len(%d),seq(%lu),data(%p),size(%zd)",
256 ospf_api_typename(msg->hdr.msgtype), msg->hdr.msgtype,
257 ntohs(msg->hdr.msglen),
258 (unsigned long)ntohl(msg->hdr.msgseq), STREAM_DATA(msg->s),
259 STREAM_SIZE(msg->s));
260
261 return;
262 }
263
264 void msg_free(struct msg *msg)
265 {
266 if (msg->s)
267 stream_free(msg->s);
268
269 XFREE(MTYPE_OSPF_API_MSG, msg);
270 }
271
272
273 /* Set sequence number of message */
274 void msg_set_seq(struct msg *msg, uint32_t seqnr)
275 {
276 assert(msg);
277 msg->hdr.msgseq = htonl(seqnr);
278 }
279
280 /* Get sequence number of message */
281 uint32_t msg_get_seq(struct msg *msg)
282 {
283 assert(msg);
284 return ntohl(msg->hdr.msgseq);
285 }
286
287 /* -----------------------------------------------------------
288 * Message fifo queues
289 * -----------------------------------------------------------
290 */
291
292 struct msg_fifo *msg_fifo_new(void)
293 {
294 return XCALLOC(MTYPE_OSPF_API_FIFO, sizeof(struct msg_fifo));
295 }
296
297 /* Add new message to fifo. */
298 void msg_fifo_push(struct msg_fifo *fifo, struct msg *msg)
299 {
300 if (fifo->tail)
301 fifo->tail->next = msg;
302 else
303 fifo->head = msg;
304
305 fifo->tail = msg;
306 fifo->count++;
307 }
308
309
310 /* Remove first message from fifo. */
311 struct msg *msg_fifo_pop(struct msg_fifo *fifo)
312 {
313 struct msg *msg;
314
315 msg = fifo->head;
316 if (msg) {
317 fifo->head = msg->next;
318
319 if (fifo->head == NULL)
320 fifo->tail = NULL;
321
322 fifo->count--;
323 }
324 return msg;
325 }
326
327 /* Return first fifo entry but do not remove it. */
328 struct msg *msg_fifo_head(struct msg_fifo *fifo)
329 {
330 return fifo->head;
331 }
332
333 /* Flush message fifo. */
334 void msg_fifo_flush(struct msg_fifo *fifo)
335 {
336 struct msg *op;
337 struct msg *next;
338
339 for (op = fifo->head; op; op = next) {
340 next = op->next;
341 msg_free(op);
342 }
343
344 fifo->head = fifo->tail = NULL;
345 fifo->count = 0;
346 }
347
348 /* Free API message fifo. */
349 void msg_fifo_free(struct msg_fifo *fifo)
350 {
351 msg_fifo_flush(fifo);
352
353 XFREE(MTYPE_OSPF_API_FIFO, fifo);
354 }
355
356 struct msg *msg_read(int fd)
357 {
358 struct msg *msg;
359 struct apimsghdr hdr;
360 uint8_t buf[OSPF_API_MAX_MSG_SIZE];
361 ssize_t bodylen;
362 ssize_t rlen;
363
364 /* Read message header */
365 rlen = readn(fd, (uint8_t *)&hdr, sizeof(struct apimsghdr));
366
367 if (rlen < 0) {
368 zlog_warn("msg_read: readn %s", safe_strerror(errno));
369 return NULL;
370 } else if (rlen == 0) {
371 zlog_warn("msg_read: Connection closed by peer");
372 return NULL;
373 } else if (rlen != sizeof(struct apimsghdr)) {
374 zlog_warn("msg_read: Cannot read message header!");
375 return NULL;
376 }
377
378 /* Check version of API protocol */
379 if (hdr.version != OSPF_API_VERSION) {
380 zlog_warn("msg_read: OSPF API protocol version mismatch");
381 return NULL;
382 }
383
384 /* Determine body length. */
385 bodylen = ntohs(hdr.msglen);
386 if (bodylen > (ssize_t)sizeof(buf)) {
387 zlog_warn("%s: Body Length of message greater than what we can read",
388 __func__);
389 return NULL;
390 }
391
392 if (bodylen > 0) {
393 /* Read message body */
394 rlen = readn(fd, buf, bodylen);
395 if (rlen < 0) {
396 zlog_warn("msg_read: readn %s", safe_strerror(errno));
397 return NULL;
398 } else if (rlen == 0) {
399 zlog_warn("msg_read: Connection closed by peer");
400 return NULL;
401 } else if (rlen != bodylen) {
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), bodylen);
409
410 return msg;
411 }
412
413 int msg_write(int fd, struct msg *msg)
414 {
415 uint8_t buf[OSPF_API_MAX_MSG_SIZE];
416 uint16_t l;
417 int wlen;
418
419 assert(msg);
420 assert(msg->s);
421
422 /* Length of OSPF LSA payload */
423 l = ntohs(msg->hdr.msglen);
424 if (l > OSPF_MAX_LSA_SIZE) {
425 zlog_warn("%s: wrong LSA size %d", __func__, l);
426 return -1;
427 }
428
429 /* Make contiguous memory buffer for message */
430 memcpy(buf, &msg->hdr, sizeof(struct apimsghdr));
431 memcpy(buf + sizeof(struct apimsghdr), STREAM_DATA(msg->s), l);
432
433 /* Total length of OSPF API Message */
434 l += sizeof(struct apimsghdr);
435 wlen = writen(fd, buf, l);
436 if (wlen < 0) {
437 zlog_warn("%s: writen %s", __func__, safe_strerror(errno));
438 return -1;
439 } else if (wlen == 0) {
440 zlog_warn("%s: Connection closed by peer", __func__);
441 return -1;
442 } else if (wlen != l) {
443 zlog_warn("%s: Cannot write API message", __func__);
444 return -1;
445 }
446 return 0;
447 }
448
449 /* -----------------------------------------------------------
450 * Specific messages
451 * -----------------------------------------------------------
452 */
453
454 struct msg *new_msg_register_opaque_type(uint32_t seqnum, uint8_t ltype,
455 uint8_t otype)
456 {
457 struct msg_register_opaque_type rmsg;
458
459 rmsg.lsatype = ltype;
460 rmsg.opaquetype = otype;
461 memset(&rmsg.pad, 0, sizeof(rmsg.pad));
462
463 return msg_new(MSG_REGISTER_OPAQUETYPE, &rmsg, seqnum,
464 sizeof(struct msg_register_opaque_type));
465 }
466
467 struct msg *new_msg_register_event(uint32_t seqnum,
468 struct lsa_filter_type *filter)
469 {
470 uint8_t buf[OSPF_API_MAX_MSG_SIZE];
471 struct msg_register_event *emsg;
472 unsigned int len;
473
474 emsg = (struct msg_register_event *)buf;
475 len = sizeof(struct msg_register_event)
476 + filter->num_areas * sizeof(struct in_addr);
477 emsg->filter.typemask = htons(filter->typemask);
478 emsg->filter.origin = filter->origin;
479 emsg->filter.num_areas = filter->num_areas;
480 if (len > sizeof(buf))
481 len = sizeof(buf);
482 /* API broken - missing memcpy to fill data */
483 return msg_new(MSG_REGISTER_EVENT, emsg, seqnum, len);
484 }
485
486 struct msg *new_msg_sync_lsdb(uint32_t seqnum, struct lsa_filter_type *filter)
487 {
488 uint8_t buf[OSPF_API_MAX_MSG_SIZE];
489 struct msg_sync_lsdb *smsg;
490 unsigned int len;
491
492 smsg = (struct msg_sync_lsdb *)buf;
493 len = sizeof(struct msg_sync_lsdb)
494 + filter->num_areas * sizeof(struct in_addr);
495 smsg->filter.typemask = htons(filter->typemask);
496 smsg->filter.origin = filter->origin;
497 smsg->filter.num_areas = filter->num_areas;
498 if (len > sizeof(buf))
499 len = sizeof(buf);
500 /* API broken - missing memcpy to fill data */
501 return msg_new(MSG_SYNC_LSDB, smsg, seqnum, len);
502 }
503
504
505 struct msg *new_msg_originate_request(uint32_t seqnum, struct in_addr ifaddr,
506 struct in_addr area_id,
507 struct lsa_header *data)
508 {
509 struct msg_originate_request *omsg;
510 unsigned int omsglen;
511 char buf[OSPF_API_MAX_MSG_SIZE];
512 size_t off_data = offsetof(struct msg_originate_request, data);
513 size_t data_maxs = sizeof(buf) - off_data;
514 struct lsa_header *omsg_data = (struct lsa_header *)&buf[off_data];
515
516 omsg = (struct msg_originate_request *)buf;
517 omsg->ifaddr = ifaddr;
518 omsg->area_id = area_id;
519
520 omsglen = ntohs(data->length);
521 if (omsglen > data_maxs)
522 omsglen = data_maxs;
523 memcpy(omsg_data, data, omsglen);
524 omsglen += sizeof(struct msg_originate_request)
525 - sizeof(struct lsa_header);
526
527 return msg_new(MSG_ORIGINATE_REQUEST, omsg, seqnum, omsglen);
528 }
529
530 struct msg *new_msg_delete_request(uint32_t seqnum, struct in_addr area_id,
531 uint8_t lsa_type, uint8_t opaque_type,
532 uint32_t opaque_id)
533 {
534 struct msg_delete_request dmsg;
535 dmsg.area_id = area_id;
536 dmsg.lsa_type = lsa_type;
537 dmsg.opaque_type = opaque_type;
538 dmsg.opaque_id = htonl(opaque_id);
539 memset(&dmsg.pad, 0, sizeof(dmsg.pad));
540
541 return msg_new(MSG_DELETE_REQUEST, &dmsg, seqnum,
542 sizeof(struct msg_delete_request));
543 }
544
545
546 struct msg *new_msg_reply(uint32_t seqnr, uint8_t rc)
547 {
548 struct msg *msg;
549 struct msg_reply rmsg;
550
551 /* Set return code */
552 rmsg.errcode = rc;
553 memset(&rmsg.pad, 0, sizeof(rmsg.pad));
554
555 msg = msg_new(MSG_REPLY, &rmsg, seqnr, sizeof(struct msg_reply));
556
557 return msg;
558 }
559
560 struct msg *new_msg_ready_notify(uint32_t seqnr, uint8_t lsa_type,
561 uint8_t opaque_type, struct in_addr addr)
562 {
563 struct msg_ready_notify rmsg;
564
565 rmsg.lsa_type = lsa_type;
566 rmsg.opaque_type = opaque_type;
567 memset(&rmsg.pad, 0, sizeof(rmsg.pad));
568 rmsg.addr = addr;
569
570 return msg_new(MSG_READY_NOTIFY, &rmsg, seqnr,
571 sizeof(struct msg_ready_notify));
572 }
573
574 struct msg *new_msg_new_if(uint32_t seqnr, struct in_addr ifaddr,
575 struct in_addr area_id)
576 {
577 struct msg_new_if nmsg;
578
579 nmsg.ifaddr = ifaddr;
580 nmsg.area_id = area_id;
581
582 return msg_new(MSG_NEW_IF, &nmsg, seqnr, sizeof(struct msg_new_if));
583 }
584
585 struct msg *new_msg_del_if(uint32_t seqnr, struct in_addr ifaddr)
586 {
587 struct msg_del_if dmsg;
588
589 dmsg.ifaddr = ifaddr;
590
591 return msg_new(MSG_DEL_IF, &dmsg, seqnr, sizeof(struct msg_del_if));
592 }
593
594 struct msg *new_msg_ism_change(uint32_t seqnr, struct in_addr ifaddr,
595 struct in_addr area_id, uint8_t status)
596 {
597 struct msg_ism_change imsg;
598
599 imsg.ifaddr = ifaddr;
600 imsg.area_id = area_id;
601 imsg.status = status;
602 memset(&imsg.pad, 0, sizeof(imsg.pad));
603
604 return msg_new(MSG_ISM_CHANGE, &imsg, seqnr,
605 sizeof(struct msg_ism_change));
606 }
607
608 struct msg *new_msg_nsm_change(uint32_t seqnr, struct in_addr ifaddr,
609 struct in_addr nbraddr, struct in_addr router_id,
610 uint8_t 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
624 struct msg *new_msg_lsa_change_notify(uint8_t msgtype, uint32_t seqnum,
625 struct in_addr ifaddr,
626 struct in_addr area_id,
627 uint8_t is_self_originated,
628 struct lsa_header *data)
629 {
630 uint8_t buf[OSPF_API_MAX_MSG_SIZE];
631 struct msg_lsa_change_notify *nmsg;
632 unsigned int len;
633 size_t off_data = offsetof(struct msg_lsa_change_notify, data);
634 size_t data_maxs = sizeof(buf) - off_data;
635 struct lsa_header *nmsg_data = (struct lsa_header *)&buf[off_data];
636
637 assert(data);
638
639 nmsg = (struct msg_lsa_change_notify *)buf;
640 nmsg->ifaddr = ifaddr;
641 nmsg->area_id = area_id;
642 nmsg->is_self_originated = is_self_originated;
643 memset(&nmsg->pad, 0, sizeof(nmsg->pad));
644
645 len = ntohs(data->length);
646 if (len > data_maxs)
647 len = data_maxs;
648 memcpy(nmsg_data, data, len);
649 len += sizeof(struct msg_lsa_change_notify) - sizeof(struct lsa_header);
650
651 return msg_new(msgtype, nmsg, seqnum, len);
652 }
653
654 #endif /* SUPPORT_OSPF_API */