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