]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_api.c
*: auto-convert to SPDX License IDs
[mirror_frr.git] / ospfd / ospf_api.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * API message handling module for OSPF daemon and client.
4 * Copyright (C) 2001, 2002 Ralph Keller
5 * Copyright (c) 2022, LabN Consulting, L.L.C.
6 */
7
8 #include <zebra.h>
9
10 #ifdef SUPPORT_OSPF_API
11
12 #include "linklist.h"
13 #include "prefix.h"
14 #include "if.h"
15 #include "table.h"
16 #include "memory.h"
17 #include "command.h"
18 #include "vty.h"
19 #include "stream.h"
20 #include "log.h"
21 #include "thread.h"
22 #include "hash.h"
23 #include "sockunion.h" /* for inet_aton() */
24 #include "buffer.h"
25 #include "network.h"
26
27 #include "ospfd/ospfd.h"
28 #include "ospfd/ospf_interface.h"
29 #include "ospfd/ospf_ism.h"
30 #include "ospfd/ospf_asbr.h"
31 #include "ospfd/ospf_lsa.h"
32 #include "ospfd/ospf_lsdb.h"
33 #include "ospfd/ospf_neighbor.h"
34 #include "ospfd/ospf_nsm.h"
35 #include "ospfd/ospf_flood.h"
36 #include "ospfd/ospf_packet.h"
37 #include "ospfd/ospf_spf.h"
38 #include "ospfd/ospf_dump.h"
39 #include "ospfd/ospf_route.h"
40 #include "ospfd/ospf_ase.h"
41 #include "ospfd/ospf_zebra.h"
42
43 #include "ospfd/ospf_api.h"
44
45
46 /* For debugging only, will be removed */
47 void api_opaque_lsa_print(struct ospf_lsa *lsa)
48 {
49 struct opaque_lsa {
50 struct lsa_header header;
51 uint8_t mydata[];
52 };
53
54 struct opaque_lsa *olsa;
55 int opaquelen;
56 int i;
57
58 ospf_lsa_header_dump(lsa->data);
59
60 olsa = (struct opaque_lsa *)lsa->data;
61
62 opaquelen = lsa->size - OSPF_LSA_HEADER_SIZE;
63 zlog_debug("apiserver_lsa_print: opaquelen=%d", opaquelen);
64
65 for (i = 0; i < opaquelen; i++) {
66 zlog_debug("0x%x ", olsa->mydata[i]);
67 }
68 zlog_debug(" ");
69 }
70
71 /* -----------------------------------------------------------
72 * Generic messages
73 * -----------------------------------------------------------
74 */
75
76 struct msg *msg_new(uint8_t msgtype, void *msgbody, uint32_t seqnum,
77 uint16_t msglen)
78 {
79 struct msg *new;
80
81 new = XCALLOC(MTYPE_OSPF_API_MSG, sizeof(struct msg));
82
83 new->hdr.version = OSPF_API_VERSION;
84 new->hdr.msgtype = msgtype;
85 new->hdr.msglen = htons(msglen);
86 new->hdr.msgseq = htonl(seqnum);
87
88 new->s = stream_new(msglen);
89 assert(new->s);
90 stream_put(new->s, msgbody, msglen);
91
92 return new;
93 }
94
95
96 /* Duplicate a message by copying content. */
97 struct msg *msg_dup(struct msg *msg)
98 {
99 struct msg *new;
100 size_t size;
101
102 assert(msg);
103
104 size = ntohs(msg->hdr.msglen);
105 if (size > OSPF_MAX_LSA_SIZE)
106 return NULL;
107
108 new = msg_new(msg->hdr.msgtype, STREAM_DATA(msg->s),
109 ntohl(msg->hdr.msgseq), size);
110 return new;
111 }
112
113
114 /* XXX only for testing, will be removed */
115
116 struct nametab {
117 int value;
118 const char *name;
119 };
120
121 const char *ospf_api_typename(int msgtype)
122 {
123 struct nametab NameTab[] = {
124 {
125 MSG_REGISTER_OPAQUETYPE, "Register opaque-type",
126 },
127 {
128 MSG_UNREGISTER_OPAQUETYPE, "Unregister opaque-type",
129 },
130 {
131 MSG_REGISTER_EVENT, "Register event",
132 },
133 {
134 MSG_SYNC_LSDB, "Sync LSDB",
135 },
136 {
137 MSG_ORIGINATE_REQUEST, "Originate request",
138 },
139 {
140 MSG_DELETE_REQUEST, "Delete request",
141 },
142 {
143 MSG_REPLY, "Reply",
144 },
145 {
146 MSG_READY_NOTIFY, "Ready notify",
147 },
148 {
149 MSG_LSA_UPDATE_NOTIFY, "LSA update notify",
150 },
151 {
152 MSG_LSA_DELETE_NOTIFY, "LSA delete notify",
153 },
154 {
155 MSG_NEW_IF, "New interface",
156 },
157 {
158 MSG_DEL_IF, "Del interface",
159 },
160 {
161 MSG_ISM_CHANGE, "ISM change",
162 },
163 {
164 MSG_NSM_CHANGE, "NSM change",
165 },
166 {
167 MSG_REACHABLE_CHANGE,
168 "Reachable change",
169 },
170 };
171
172 int i, n = array_size(NameTab);
173 const char *name = NULL;
174
175 for (i = 0; i < n; i++) {
176 if (NameTab[i].value == msgtype) {
177 name = NameTab[i].name;
178 break;
179 }
180 }
181
182 return name ? name : "?";
183 }
184
185 const char *ospf_api_errname(int errcode)
186 {
187 struct nametab NameTab[] = {
188 {
189 OSPF_API_OK, "OK",
190 },
191 {
192 OSPF_API_NOSUCHINTERFACE, "No such interface",
193 },
194 {
195 OSPF_API_NOSUCHAREA, "No such area",
196 },
197 {
198 OSPF_API_NOSUCHLSA, "No such LSA",
199 },
200 {
201 OSPF_API_ILLEGALLSATYPE, "Illegal LSA type",
202 },
203 {
204 OSPF_API_OPAQUETYPEINUSE, "Opaque type in use",
205 },
206 {
207 OSPF_API_OPAQUETYPENOTREGISTERED,
208 "Opaque type not registered",
209 },
210 {
211 OSPF_API_NOTREADY, "Not ready",
212 },
213 {
214 OSPF_API_NOMEMORY, "No memory",
215 },
216 {
217 OSPF_API_ERROR, "Other error",
218 },
219 {
220 OSPF_API_UNDEF, "Undefined",
221 },
222 };
223
224 int i, n = array_size(NameTab);
225 const char *name = NULL;
226
227 for (i = 0; i < n; i++) {
228 if (NameTab[i].value == errcode) {
229 name = NameTab[i].name;
230 break;
231 }
232 }
233
234 return name ? name : "?";
235 }
236
237 void msg_print(struct msg *msg)
238 {
239 if (!msg) {
240 zlog_debug("msg_print msg=NULL!");
241 return;
242 }
243
244 /* API message common header part. */
245 zlog_debug("API-msg [%s]: type(%d),len(%d),seq(%lu),data(%p),size(%zd)",
246 ospf_api_typename(msg->hdr.msgtype), msg->hdr.msgtype,
247 ntohs(msg->hdr.msglen),
248 (unsigned long)ntohl(msg->hdr.msgseq), STREAM_DATA(msg->s),
249 STREAM_SIZE(msg->s));
250
251 return;
252 }
253
254 void msg_free(struct msg *msg)
255 {
256 if (msg->s)
257 stream_free(msg->s);
258
259 XFREE(MTYPE_OSPF_API_MSG, msg);
260 }
261
262
263 /* Set sequence number of message */
264 void msg_set_seq(struct msg *msg, uint32_t seqnr)
265 {
266 assert(msg);
267 msg->hdr.msgseq = htonl(seqnr);
268 }
269
270 /* Get sequence number of message */
271 uint32_t msg_get_seq(struct msg *msg)
272 {
273 assert(msg);
274 return ntohl(msg->hdr.msgseq);
275 }
276
277 /* -----------------------------------------------------------
278 * Message fifo queues
279 * -----------------------------------------------------------
280 */
281
282 struct msg_fifo *msg_fifo_new(void)
283 {
284 return XCALLOC(MTYPE_OSPF_API_FIFO, sizeof(struct msg_fifo));
285 }
286
287 /* Add new message to fifo. */
288 void msg_fifo_push(struct msg_fifo *fifo, struct msg *msg)
289 {
290 if (fifo->tail)
291 fifo->tail->next = msg;
292 else
293 fifo->head = msg;
294
295 fifo->tail = msg;
296 fifo->count++;
297 }
298
299
300 /* Remove first message from fifo. */
301 struct msg *msg_fifo_pop(struct msg_fifo *fifo)
302 {
303 struct msg *msg;
304
305 msg = fifo->head;
306 if (msg) {
307 fifo->head = msg->next;
308
309 if (fifo->head == NULL)
310 fifo->tail = NULL;
311
312 fifo->count--;
313 }
314 return msg;
315 }
316
317 /* Return first fifo entry but do not remove it. */
318 struct msg *msg_fifo_head(struct msg_fifo *fifo)
319 {
320 return fifo->head;
321 }
322
323 /* Flush message fifo. */
324 void msg_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 next = op->next;
331 msg_free(op);
332 }
333
334 fifo->head = fifo->tail = NULL;
335 fifo->count = 0;
336 }
337
338 /* Free API message fifo. */
339 void msg_fifo_free(struct msg_fifo *fifo)
340 {
341 msg_fifo_flush(fifo);
342
343 XFREE(MTYPE_OSPF_API_FIFO, fifo);
344 }
345
346 struct msg *msg_read(int fd)
347 {
348 struct msg *msg;
349 struct apimsghdr hdr;
350 uint8_t buf[OSPF_API_MAX_MSG_SIZE];
351 ssize_t bodylen;
352 ssize_t rlen;
353
354 /* Read message header */
355 rlen = readn(fd, (uint8_t *)&hdr, sizeof(struct apimsghdr));
356
357 if (rlen < 0) {
358 zlog_warn("msg_read: readn %s", safe_strerror(errno));
359 return NULL;
360 } else if (rlen == 0) {
361 zlog_warn("msg_read: Connection closed by peer");
362 return NULL;
363 } else if (rlen != sizeof(struct apimsghdr)) {
364 zlog_warn("msg_read: Cannot read message header!");
365 return NULL;
366 }
367
368 /* Check version of API protocol */
369 if (hdr.version != OSPF_API_VERSION) {
370 zlog_warn("msg_read: OSPF API protocol version mismatch");
371 return NULL;
372 }
373
374 /* Determine body length. */
375 bodylen = ntohs(hdr.msglen);
376 if (bodylen > (ssize_t)sizeof(buf)) {
377 zlog_warn("%s: Body Length of message greater than what we can read",
378 __func__);
379 return NULL;
380 }
381
382 if (bodylen > 0) {
383 /* Read message body */
384 rlen = readn(fd, buf, bodylen);
385 if (rlen < 0) {
386 zlog_warn("msg_read: readn %s", safe_strerror(errno));
387 return NULL;
388 } else if (rlen == 0) {
389 zlog_warn("msg_read: Connection closed by peer");
390 return NULL;
391 } else if (rlen != bodylen) {
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), bodylen);
399
400 return msg;
401 }
402
403 int msg_write(int fd, struct msg *msg)
404 {
405 uint8_t buf[OSPF_API_MAX_MSG_SIZE];
406 uint16_t l;
407 int wlen;
408
409 assert(msg);
410 assert(msg->s);
411
412 /* Length of OSPF LSA payload */
413 l = ntohs(msg->hdr.msglen);
414 if (l > OSPF_MAX_LSA_SIZE) {
415 zlog_warn("%s: wrong LSA size %d", __func__, l);
416 return -1;
417 }
418
419 /* Make contiguous memory buffer for message */
420 memcpy(buf, &msg->hdr, sizeof(struct apimsghdr));
421 memcpy(buf + sizeof(struct apimsghdr), STREAM_DATA(msg->s), l);
422
423 /* Total length of OSPF API Message */
424 l += sizeof(struct apimsghdr);
425 wlen = writen(fd, buf, l);
426 if (wlen < 0) {
427 zlog_warn("%s: writen %s", __func__, safe_strerror(errno));
428 return -1;
429 } else if (wlen == 0) {
430 zlog_warn("%s: Connection closed by peer", __func__);
431 return -1;
432 } else if (wlen != l) {
433 zlog_warn("%s: Cannot write API message", __func__);
434 return -1;
435 }
436 return 0;
437 }
438
439 /* -----------------------------------------------------------
440 * Specific messages
441 * -----------------------------------------------------------
442 */
443
444 struct msg *new_msg_register_opaque_type(uint32_t seqnum, uint8_t ltype,
445 uint8_t otype)
446 {
447 struct msg_register_opaque_type rmsg;
448
449 rmsg.lsatype = ltype;
450 rmsg.opaquetype = otype;
451 memset(&rmsg.pad, 0, sizeof(rmsg.pad));
452
453 return msg_new(MSG_REGISTER_OPAQUETYPE, &rmsg, seqnum,
454 sizeof(struct msg_register_opaque_type));
455 }
456
457 struct msg *new_msg_register_event(uint32_t seqnum,
458 struct lsa_filter_type *filter)
459 {
460 uint8_t buf[OSPF_API_MAX_MSG_SIZE];
461 struct msg_register_event *emsg;
462 unsigned int len;
463
464 emsg = (struct msg_register_event *)buf;
465 len = sizeof(struct msg_register_event)
466 + filter->num_areas * sizeof(struct in_addr);
467 emsg->filter.typemask = htons(filter->typemask);
468 emsg->filter.origin = filter->origin;
469 emsg->filter.num_areas = filter->num_areas;
470 if (len > sizeof(buf))
471 len = sizeof(buf);
472 /* API broken - missing memcpy to fill data */
473 return msg_new(MSG_REGISTER_EVENT, emsg, seqnum, len);
474 }
475
476 struct msg *new_msg_sync_lsdb(uint32_t seqnum, struct lsa_filter_type *filter)
477 {
478 uint8_t buf[OSPF_API_MAX_MSG_SIZE];
479 struct msg_sync_lsdb *smsg;
480 unsigned int len;
481
482 smsg = (struct msg_sync_lsdb *)buf;
483 len = sizeof(struct msg_sync_lsdb)
484 + filter->num_areas * sizeof(struct in_addr);
485 smsg->filter.typemask = htons(filter->typemask);
486 smsg->filter.origin = filter->origin;
487 smsg->filter.num_areas = filter->num_areas;
488 if (len > sizeof(buf))
489 len = sizeof(buf);
490 /* API broken - missing memcpy to fill data */
491 return msg_new(MSG_SYNC_LSDB, smsg, seqnum, len);
492 }
493
494
495 struct msg *new_msg_originate_request(uint32_t seqnum, struct in_addr ifaddr,
496 struct in_addr area_id,
497 struct lsa_header *data)
498 {
499 struct msg_originate_request *omsg;
500 unsigned int omsglen;
501 char buf[OSPF_API_MAX_MSG_SIZE];
502 size_t off_data = offsetof(struct msg_originate_request, data);
503 size_t data_maxs = sizeof(buf) - off_data;
504 struct lsa_header *omsg_data = (struct lsa_header *)&buf[off_data];
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 > data_maxs)
512 omsglen = data_maxs;
513 memcpy(omsg_data, data, omsglen);
514 omsglen += sizeof(struct msg_originate_request)
515 - sizeof(struct lsa_header);
516
517 return msg_new(MSG_ORIGINATE_REQUEST, omsg, seqnum, omsglen);
518 }
519
520 struct msg *new_msg_delete_request(uint32_t seqnum, struct in_addr addr,
521 uint8_t lsa_type, uint8_t opaque_type,
522 uint32_t opaque_id, uint8_t flags)
523 {
524 struct msg_delete_request dmsg;
525 dmsg.addr = addr;
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 dmsg.flags = flags;
531
532 return msg_new(MSG_DELETE_REQUEST, &dmsg, seqnum,
533 sizeof(struct msg_delete_request));
534 }
535
536
537 struct msg *new_msg_reply(uint32_t seqnr, uint8_t 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 *new_msg_ready_notify(uint32_t seqnr, uint8_t lsa_type,
552 uint8_t opaque_type, struct in_addr addr)
553 {
554 struct msg_ready_notify rmsg;
555
556 rmsg.lsa_type = lsa_type;
557 rmsg.opaque_type = opaque_type;
558 memset(&rmsg.pad, 0, sizeof(rmsg.pad));
559 rmsg.addr = addr;
560
561 return msg_new(MSG_READY_NOTIFY, &rmsg, seqnr,
562 sizeof(struct msg_ready_notify));
563 }
564
565 struct msg *new_msg_new_if(uint32_t seqnr, struct in_addr ifaddr,
566 struct in_addr area_id)
567 {
568 struct msg_new_if nmsg;
569
570 nmsg.ifaddr = ifaddr;
571 nmsg.area_id = area_id;
572
573 return msg_new(MSG_NEW_IF, &nmsg, seqnr, sizeof(struct msg_new_if));
574 }
575
576 struct msg *new_msg_del_if(uint32_t seqnr, struct in_addr ifaddr)
577 {
578 struct msg_del_if dmsg;
579
580 dmsg.ifaddr = ifaddr;
581
582 return msg_new(MSG_DEL_IF, &dmsg, seqnr, sizeof(struct msg_del_if));
583 }
584
585 struct msg *new_msg_ism_change(uint32_t seqnr, struct in_addr ifaddr,
586 struct in_addr area_id, uint8_t status)
587 {
588 struct msg_ism_change imsg;
589
590 imsg.ifaddr = ifaddr;
591 imsg.area_id = area_id;
592 imsg.status = status;
593 memset(&imsg.pad, 0, sizeof(imsg.pad));
594
595 return msg_new(MSG_ISM_CHANGE, &imsg, seqnr,
596 sizeof(struct msg_ism_change));
597 }
598
599 struct msg *new_msg_nsm_change(uint32_t seqnr, struct in_addr ifaddr,
600 struct in_addr nbraddr, struct in_addr router_id,
601 uint8_t status)
602 {
603 struct msg_nsm_change nmsg;
604
605 nmsg.ifaddr = ifaddr;
606 nmsg.nbraddr = nbraddr;
607 nmsg.router_id = router_id;
608 nmsg.status = status;
609 memset(&nmsg.pad, 0, sizeof(nmsg.pad));
610
611 return msg_new(MSG_NSM_CHANGE, &nmsg, seqnr,
612 sizeof(struct msg_nsm_change));
613 }
614
615 struct msg *new_msg_lsa_change_notify(uint8_t msgtype, uint32_t seqnum,
616 struct in_addr ifaddr,
617 struct in_addr area_id,
618 uint8_t is_self_originated,
619 struct lsa_header *data)
620 {
621 uint8_t buf[OSPF_API_MAX_MSG_SIZE];
622 struct msg_lsa_change_notify *nmsg;
623 unsigned int len;
624 size_t off_data = offsetof(struct msg_lsa_change_notify, data);
625 size_t data_maxs = sizeof(buf) - off_data;
626 struct lsa_header *nmsg_data = (struct lsa_header *)&buf[off_data];
627
628 assert(data);
629
630 nmsg = (struct msg_lsa_change_notify *)buf;
631 nmsg->ifaddr = ifaddr;
632 nmsg->area_id = area_id;
633 nmsg->is_self_originated = is_self_originated;
634 memset(&nmsg->pad, 0, sizeof(nmsg->pad));
635
636 len = ntohs(data->length);
637 if (len > data_maxs)
638 len = data_maxs;
639 memcpy(nmsg_data, data, len);
640 len += sizeof(struct msg_lsa_change_notify) - sizeof(struct lsa_header);
641
642 return msg_new(msgtype, nmsg, seqnum, len);
643 }
644
645 struct msg *new_msg_reachable_change(uint32_t seqnum, uint16_t nadd,
646 struct in_addr *add, uint16_t nremove,
647 struct in_addr *remove)
648 {
649 uint8_t buf[OSPF_API_MAX_MSG_SIZE];
650 struct msg_reachable_change *nmsg = (void *)buf;
651 const uint insz = sizeof(*nmsg->router_ids);
652 const uint nmax = (sizeof(buf) - sizeof(*nmsg)) / insz;
653 uint len;
654
655 if (nadd > nmax)
656 nadd = nmax;
657 if (nremove > (nmax - nadd))
658 nremove = (nmax - nadd);
659
660 if (nadd)
661 memcpy(nmsg->router_ids, add, nadd * insz);
662 if (nremove)
663 memcpy(&nmsg->router_ids[nadd], remove, nremove * insz);
664
665 nmsg->nadd = htons(nadd);
666 nmsg->nremove = htons(nremove);
667 len = sizeof(*nmsg) + insz * (nadd + nremove);
668
669 return msg_new(MSG_REACHABLE_CHANGE, nmsg, seqnum, len);
670 }
671
672 struct msg *new_msg_router_id_change(uint32_t seqnum, struct in_addr router_id)
673 {
674 struct msg_router_id_change rmsg = {.router_id = router_id};
675
676 return msg_new(MSG_ROUTER_ID_CHANGE, &rmsg, seqnum,
677 sizeof(struct msg_router_id_change));
678 }
679
680 #endif /* SUPPORT_OSPF_API */