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