forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathospf_apiserver.c
2480 lines (2052 loc) · 62.3 KB
/
ospf_apiserver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Server side of OSPF API.
* Copyright (C) 2001, 2002 Ralph Keller
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2, or (at your
* option) any later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#ifdef SUPPORT_OSPF_API
#include "linklist.h"
#include "prefix.h"
#include "if.h"
#include "table.h"
#include "memory.h"
#include "command.h"
#include "vty.h"
#include "stream.h"
#include "log.h"
#include "thread.h"
#include "hash.h"
#include "sockunion.h" /* for inet_aton() */
#include "buffer.h"
#include <sys/types.h>
#include "ospfd/ospfd.h" /* for "struct thread_master" */
#include "ospfd/ospf_interface.h"
#include "ospfd/ospf_ism.h"
#include "ospfd/ospf_asbr.h"
#include "ospfd/ospf_lsa.h"
#include "ospfd/ospf_lsdb.h"
#include "ospfd/ospf_neighbor.h"
#include "ospfd/ospf_nsm.h"
#include "ospfd/ospf_flood.h"
#include "ospfd/ospf_packet.h"
#include "ospfd/ospf_spf.h"
#include "ospfd/ospf_dump.h"
#include "ospfd/ospf_route.h"
#include "ospfd/ospf_ase.h"
#include "ospfd/ospf_zebra.h"
#include "ospfd/ospf_errors.h"
#include "ospfd/ospf_api.h"
#include "ospfd/ospf_apiserver.h"
/* This is an implementation of an API to the OSPF daemon that allows
* external applications to access the OSPF daemon through socket
* connections. The application can use this API to inject its own
* opaque LSAs and flood them to other OSPF daemons. Other OSPF
* daemons then receive these LSAs and inform applications through the
* API by sending a corresponding message. The application can also
* register to receive all LSA types (in addition to opaque types) and
* use this information to reconstruct the OSPF's LSDB. The OSPF
* daemon supports multiple applications concurrently. */
/* List of all active connections. */
struct list *apiserver_list;
/* -----------------------------------------------------------
* Functions to lookup interfaces
* -----------------------------------------------------------
*/
struct ospf_interface *ospf_apiserver_if_lookup_by_addr(struct in_addr address)
{
struct listnode *node, *nnode;
struct ospf_interface *oi;
struct ospf *ospf = NULL;
ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
if (!ospf)
return NULL;
for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi))
if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
if (IPV4_ADDR_SAME(&address, &oi->address->u.prefix4))
return oi;
return NULL;
}
struct ospf_interface *ospf_apiserver_if_lookup_by_ifp(struct interface *ifp)
{
struct listnode *node, *nnode;
struct ospf_interface *oi;
struct ospf *ospf = NULL;
ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
if (!ospf)
return NULL;
for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi))
if (oi->ifp == ifp)
return oi;
return NULL;
}
/* -----------------------------------------------------------
* Initialization
* -----------------------------------------------------------
*/
unsigned short ospf_apiserver_getport(void)
{
struct servent *sp = getservbyname("ospfapi", "tcp");
return sp ? ntohs(sp->s_port) : OSPF_API_SYNC_PORT;
}
/* Initialize OSPF API module. Invoked from ospf_opaque_init() */
int ospf_apiserver_init(void)
{
int fd;
int rc = -1;
/* Create new socket for synchronous messages. */
fd = ospf_apiserver_serv_sock_family(ospf_apiserver_getport(), AF_INET);
if (fd < 0)
goto out;
/* Schedule new thread that handles accepted connections. */
ospf_apiserver_event(OSPF_APISERVER_ACCEPT, fd, NULL);
/* Initialize list that keeps track of all connections. */
apiserver_list = list_new();
/* Register opaque-independent call back functions. These functions
are invoked on ISM, NSM changes and LSA update and LSA deletes */
rc = ospf_register_opaque_functab(
0 /* all LSAs */, 0 /* all opaque types */,
ospf_apiserver_new_if, ospf_apiserver_del_if,
ospf_apiserver_ism_change, ospf_apiserver_nsm_change, NULL,
NULL, NULL, NULL, /* ospf_apiserver_show_info */
NULL, /* originator_func */
NULL, /* ospf_apiserver_lsa_refresher */
ospf_apiserver_lsa_update, ospf_apiserver_lsa_delete);
if (rc != 0) {
flog_warn(
EC_OSPF_OPAQUE_REGISTRATION,
"ospf_apiserver_init: Failed to register opaque type [0/0]");
}
rc = 0;
out:
return rc;
}
/* Terminate OSPF API module. */
void ospf_apiserver_term(void)
{
struct ospf_apiserver *apiserv;
/* Unregister wildcard [0/0] type */
ospf_delete_opaque_functab(0 /* all LSAs */, 0 /* all opaque types */);
/*
* Free all client instances. ospf_apiserver_free removes the node
* from the list, so we examine the head of the list anew each time.
*/
while (apiserver_list
&& (apiserv = listgetdata(listhead(apiserver_list))) != NULL)
ospf_apiserver_free(apiserv);
/* Free client list itself */
if (apiserver_list)
list_delete(&apiserver_list);
/* Free wildcard list */
/* XXX */
}
static struct ospf_apiserver *lookup_apiserver(uint8_t lsa_type,
uint8_t opaque_type)
{
struct listnode *n1, *n2;
struct registered_opaque_type *r;
struct ospf_apiserver *apiserv, *found = NULL;
/* XXX: this approaches O(n**2) */
for (ALL_LIST_ELEMENTS_RO(apiserver_list, n1, apiserv)) {
for (ALL_LIST_ELEMENTS_RO(apiserv->opaque_types, n2, r))
if (r->lsa_type == lsa_type
&& r->opaque_type == opaque_type) {
found = apiserv;
goto out;
}
}
out:
return found;
}
static struct ospf_apiserver *lookup_apiserver_by_lsa(struct ospf_lsa *lsa)
{
struct lsa_header *lsah = lsa->data;
struct ospf_apiserver *found = NULL;
if (IS_OPAQUE_LSA(lsah->type)) {
found = lookup_apiserver(
lsah->type, GET_OPAQUE_TYPE(ntohl(lsah->id.s_addr)));
}
return found;
}
/* -----------------------------------------------------------
* Followings are functions to manage client connections.
* -----------------------------------------------------------
*/
static int ospf_apiserver_new_lsa_hook(struct ospf_lsa *lsa)
{
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("API: Put LSA(%p)[%s] into reserve, total=%ld",
(void *)lsa, dump_lsa_key(lsa), lsa->lsdb->total);
return 0;
}
static int ospf_apiserver_del_lsa_hook(struct ospf_lsa *lsa)
{
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("API: Get LSA(%p)[%s] from reserve, total=%ld",
(void *)lsa, dump_lsa_key(lsa), lsa->lsdb->total);
return 0;
}
/* Allocate new connection structure. */
struct ospf_apiserver *ospf_apiserver_new(int fd_sync, int fd_async)
{
struct ospf_apiserver *new =
XMALLOC(MTYPE_OSPF_APISERVER, sizeof(struct ospf_apiserver));
new->filter = XMALLOC(MTYPE_OSPF_APISERVER_MSGFILTER,
sizeof(struct lsa_filter_type));
new->fd_sync = fd_sync;
new->fd_async = fd_async;
/* list of registered opaque types that application uses */
new->opaque_types = list_new();
/* Initialize temporary strage for LSA instances to be refreshed. */
memset(&new->reserve, 0, sizeof(struct ospf_lsdb));
ospf_lsdb_init(&new->reserve);
new->reserve.new_lsa_hook = ospf_apiserver_new_lsa_hook; /* debug */
new->reserve.del_lsa_hook = ospf_apiserver_del_lsa_hook; /* debug */
new->out_sync_fifo = msg_fifo_new();
new->out_async_fifo = msg_fifo_new();
new->t_sync_read = NULL;
#ifdef USE_ASYNC_READ
new->t_async_read = NULL;
#endif /* USE_ASYNC_READ */
new->t_sync_write = NULL;
new->t_async_write = NULL;
new->filter->typemask = 0; /* filter all LSAs */
new->filter->origin = ANY_ORIGIN;
new->filter->num_areas = 0;
return new;
}
void ospf_apiserver_event(enum event event, int fd,
struct ospf_apiserver *apiserv)
{
switch (event) {
case OSPF_APISERVER_ACCEPT:
(void)thread_add_read(master, ospf_apiserver_accept, apiserv,
fd, NULL);
break;
case OSPF_APISERVER_SYNC_READ:
apiserv->t_sync_read = NULL;
thread_add_read(master, ospf_apiserver_read, apiserv, fd,
&apiserv->t_sync_read);
break;
#ifdef USE_ASYNC_READ
case OSPF_APISERVER_ASYNC_READ:
apiserv->t_async_read = NULL;
thread_add_read(master, ospf_apiserver_read, apiserv, fd,
&apiserv->t_async_read);
break;
#endif /* USE_ASYNC_READ */
case OSPF_APISERVER_SYNC_WRITE:
thread_add_write(master, ospf_apiserver_sync_write, apiserv, fd,
&apiserv->t_sync_write);
break;
case OSPF_APISERVER_ASYNC_WRITE:
thread_add_write(master, ospf_apiserver_async_write, apiserv,
fd, &apiserv->t_async_write);
break;
}
}
/* Free instance. First unregister all opaque types used by
application, flush opaque LSAs injected by application
from network and close connection. */
void ospf_apiserver_free(struct ospf_apiserver *apiserv)
{
struct listnode *node;
/* Cancel read and write threads. */
thread_cancel(&apiserv->t_sync_read);
#ifdef USE_ASYNC_READ
thread_cancel(&apiserv->t_async_read);
#endif /* USE_ASYNC_READ */
thread_cancel(&apiserv->t_sync_write);
thread_cancel(&apiserv->t_async_write);
/* Unregister all opaque types that application registered
and flush opaque LSAs if still in LSDB. */
while ((node = listhead(apiserv->opaque_types)) != NULL) {
struct registered_opaque_type *regtype = listgetdata(node);
ospf_apiserver_unregister_opaque_type(
apiserv, regtype->lsa_type, regtype->opaque_type);
}
/* Close connections to OSPFd. */
if (apiserv->fd_sync > 0) {
close(apiserv->fd_sync);
}
if (apiserv->fd_async > 0) {
close(apiserv->fd_async);
}
/* Free fifos */
msg_fifo_free(apiserv->out_sync_fifo);
msg_fifo_free(apiserv->out_async_fifo);
/* Clear temporary strage for LSA instances to be refreshed. */
ospf_lsdb_delete_all(&apiserv->reserve);
ospf_lsdb_cleanup(&apiserv->reserve);
/* Remove from the list of active clients. */
listnode_delete(apiserver_list, apiserv);
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("API: Delete apiserv(%p), total#(%d)",
(void *)apiserv, apiserver_list->count);
/* And free instance. */
XFREE(MTYPE_OSPF_APISERVER, apiserv);
}
int ospf_apiserver_read(struct thread *thread)
{
struct ospf_apiserver *apiserv;
struct msg *msg;
int fd;
int rc = -1;
enum event event;
apiserv = THREAD_ARG(thread);
fd = THREAD_FD(thread);
if (fd == apiserv->fd_sync) {
event = OSPF_APISERVER_SYNC_READ;
apiserv->t_sync_read = NULL;
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("API: ospf_apiserver_read: Peer: %pI4/%u",
&apiserv->peer_sync.sin_addr,
ntohs(apiserv->peer_sync.sin_port));
}
#ifdef USE_ASYNC_READ
else if (fd == apiserv->fd_async) {
event = OSPF_APISERVER_ASYNC_READ;
apiserv->t_async_read = NULL;
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("API: ospf_apiserver_read: Peer: %pI4/%u",
&apiserv->peer_async.sin_addr,
ntohs(apiserv->peer_async.sin_port));
}
#endif /* USE_ASYNC_READ */
else {
zlog_warn("ospf_apiserver_read: Unknown fd(%d)", fd);
ospf_apiserver_free(apiserv);
goto out;
}
/* Read message from fd. */
msg = msg_read(fd);
if (msg == NULL) {
zlog_warn(
"ospf_apiserver_read: read failed on fd=%d, closing connection",
fd);
/* Perform cleanup. */
ospf_apiserver_free(apiserv);
goto out;
}
if (IS_DEBUG_OSPF_EVENT)
msg_print(msg);
/* Dispatch to corresponding message handler. */
rc = ospf_apiserver_handle_msg(apiserv, msg);
/* Prepare for next message, add read thread. */
ospf_apiserver_event(event, fd, apiserv);
msg_free(msg);
out:
return rc;
}
int ospf_apiserver_sync_write(struct thread *thread)
{
struct ospf_apiserver *apiserv;
struct msg *msg;
int fd;
int rc = -1;
apiserv = THREAD_ARG(thread);
assert(apiserv);
fd = THREAD_FD(thread);
apiserv->t_sync_write = NULL;
/* Sanity check */
if (fd != apiserv->fd_sync) {
zlog_warn("ospf_apiserver_sync_write: Unknown fd=%d", fd);
goto out;
}
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("API: ospf_apiserver_sync_write: Peer: %pI4/%u",
&apiserv->peer_sync.sin_addr,
ntohs(apiserv->peer_sync.sin_port));
/* Check whether there is really a message in the fifo. */
msg = msg_fifo_pop(apiserv->out_sync_fifo);
if (!msg) {
zlog_warn(
"API: ospf_apiserver_sync_write: No message in Sync-FIFO?");
return 0;
}
if (IS_DEBUG_OSPF_EVENT)
msg_print(msg);
rc = msg_write(fd, msg);
/* Once a message is dequeued, it should be freed anyway. */
msg_free(msg);
if (rc < 0) {
zlog_warn("ospf_apiserver_sync_write: write failed on fd=%d",
fd);
goto out;
}
/* If more messages are in sync message fifo, schedule write thread. */
if (msg_fifo_head(apiserv->out_sync_fifo)) {
ospf_apiserver_event(OSPF_APISERVER_SYNC_WRITE,
apiserv->fd_sync, apiserv);
}
out:
if (rc < 0) {
/* Perform cleanup and disconnect with peer */
ospf_apiserver_free(apiserv);
}
return rc;
}
int ospf_apiserver_async_write(struct thread *thread)
{
struct ospf_apiserver *apiserv;
struct msg *msg;
int fd;
int rc = -1;
apiserv = THREAD_ARG(thread);
assert(apiserv);
fd = THREAD_FD(thread);
apiserv->t_async_write = NULL;
/* Sanity check */
if (fd != apiserv->fd_async) {
zlog_warn("ospf_apiserver_async_write: Unknown fd=%d", fd);
goto out;
}
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("API: ospf_apiserver_async_write: Peer: %pI4/%u",
&apiserv->peer_async.sin_addr,
ntohs(apiserv->peer_async.sin_port));
/* Check whether there is really a message in the fifo. */
msg = msg_fifo_pop(apiserv->out_async_fifo);
if (!msg) {
zlog_warn(
"API: ospf_apiserver_async_write: No message in Async-FIFO?");
return 0;
}
if (IS_DEBUG_OSPF_EVENT)
msg_print(msg);
rc = msg_write(fd, msg);
/* Once a message is dequeued, it should be freed anyway. */
msg_free(msg);
if (rc < 0) {
zlog_warn("ospf_apiserver_async_write: write failed on fd=%d",
fd);
goto out;
}
/* If more messages are in async message fifo, schedule write thread. */
if (msg_fifo_head(apiserv->out_async_fifo)) {
ospf_apiserver_event(OSPF_APISERVER_ASYNC_WRITE,
apiserv->fd_async, apiserv);
}
out:
if (rc < 0) {
/* Perform cleanup and disconnect with peer */
ospf_apiserver_free(apiserv);
}
return rc;
}
int ospf_apiserver_serv_sock_family(unsigned short port, int family)
{
union sockunion su;
int accept_sock;
int rc;
memset(&su, 0, sizeof(union sockunion));
su.sa.sa_family = family;
/* Make new socket */
accept_sock = sockunion_stream_socket(&su);
if (accept_sock < 0)
return accept_sock;
/* This is a server, so reuse address and port */
sockopt_reuseaddr(accept_sock);
sockopt_reuseport(accept_sock);
/* Bind socket to address and given port. */
rc = sockunion_bind(accept_sock, &su, port, NULL);
if (rc < 0) {
close(accept_sock); /* Close socket */
return rc;
}
/* Listen socket under queue length 3. */
rc = listen(accept_sock, 3);
if (rc < 0) {
zlog_warn("ospf_apiserver_serv_sock_family: listen: %s",
safe_strerror(errno));
close(accept_sock); /* Close socket */
return rc;
}
return accept_sock;
}
/* Accept connection request from external applications. For each
accepted connection allocate own connection instance. */
int ospf_apiserver_accept(struct thread *thread)
{
int accept_sock;
int new_sync_sock;
int new_async_sock;
union sockunion su;
struct ospf_apiserver *apiserv;
struct sockaddr_in peer_async;
struct sockaddr_in peer_sync;
unsigned int peerlen;
int ret;
/* THREAD_ARG (thread) is NULL */
accept_sock = THREAD_FD(thread);
/* Keep hearing on socket for further connections. */
ospf_apiserver_event(OSPF_APISERVER_ACCEPT, accept_sock, NULL);
memset(&su, 0, sizeof(union sockunion));
/* Accept connection for synchronous messages */
new_sync_sock = sockunion_accept(accept_sock, &su);
if (new_sync_sock < 0) {
zlog_warn("ospf_apiserver_accept: accept: %s",
safe_strerror(errno));
return -1;
}
/* Get port address and port number of peer to make reverse connection.
The reverse channel uses the port number of the peer port+1. */
memset(&peer_sync, 0, sizeof(struct sockaddr_in));
peerlen = sizeof(struct sockaddr_in);
ret = getpeername(new_sync_sock, (struct sockaddr *)&peer_sync,
&peerlen);
if (ret < 0) {
zlog_warn("ospf_apiserver_accept: getpeername: %s",
safe_strerror(errno));
close(new_sync_sock);
return -1;
}
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("API: ospf_apiserver_accept: New peer: %pI4/%u",
&peer_sync.sin_addr,
ntohs(peer_sync.sin_port));
/* Create new socket for asynchronous messages. */
peer_async = peer_sync;
peer_async.sin_port = htons(ntohs(peer_sync.sin_port) + 1);
/* Check if remote port number to make reverse connection is valid one.
*/
if (ntohs(peer_async.sin_port) == ospf_apiserver_getport()) {
zlog_warn(
"API: ospf_apiserver_accept: Peer(%pI4/%u): Invalid async port number?",
&peer_async.sin_addr,
ntohs(peer_async.sin_port));
close(new_sync_sock);
return -1;
}
new_async_sock = socket(AF_INET, SOCK_STREAM, 0);
if (new_async_sock < 0) {
zlog_warn("ospf_apiserver_accept: socket: %s",
safe_strerror(errno));
close(new_sync_sock);
return -1;
}
ret = connect(new_async_sock, (struct sockaddr *)&peer_async,
sizeof(struct sockaddr_in));
if (ret < 0) {
zlog_warn("ospf_apiserver_accept: connect: %s",
safe_strerror(errno));
close(new_sync_sock);
close(new_async_sock);
return -1;
}
#ifdef USE_ASYNC_READ
#else /* USE_ASYNC_READ */
/* Make the asynchronous channel write-only. */
ret = shutdown(new_async_sock, SHUT_RD);
if (ret < 0) {
zlog_warn("ospf_apiserver_accept: shutdown: %s",
safe_strerror(errno));
close(new_sync_sock);
close(new_async_sock);
return -1;
}
#endif /* USE_ASYNC_READ */
/* Allocate new server-side connection structure */
apiserv = ospf_apiserver_new(new_sync_sock, new_async_sock);
/* Add to active connection list */
listnode_add(apiserver_list, apiserv);
apiserv->peer_sync = peer_sync;
apiserv->peer_async = peer_async;
/* And add read threads for new connection */
ospf_apiserver_event(OSPF_APISERVER_SYNC_READ, new_sync_sock, apiserv);
#ifdef USE_ASYNC_READ
ospf_apiserver_event(OSPF_APISERVER_ASYNC_READ, new_async_sock,
apiserv);
#endif /* USE_ASYNC_READ */
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("API: New apiserv(%p), total#(%d)", (void *)apiserv,
apiserver_list->count);
return 0;
}
/* -----------------------------------------------------------
* Send reply with return code to client application
* -----------------------------------------------------------
*/
static int ospf_apiserver_send_msg(struct ospf_apiserver *apiserv,
struct msg *msg)
{
struct msg_fifo *fifo;
struct msg *msg2;
enum event event;
int fd;
switch (msg->hdr.msgtype) {
case MSG_REPLY:
fifo = apiserv->out_sync_fifo;
fd = apiserv->fd_sync;
event = OSPF_APISERVER_SYNC_WRITE;
break;
case MSG_READY_NOTIFY:
case MSG_LSA_UPDATE_NOTIFY:
case MSG_LSA_DELETE_NOTIFY:
case MSG_NEW_IF:
case MSG_DEL_IF:
case MSG_ISM_CHANGE:
case MSG_NSM_CHANGE:
fifo = apiserv->out_async_fifo;
fd = apiserv->fd_async;
event = OSPF_APISERVER_ASYNC_WRITE;
break;
default:
zlog_warn("ospf_apiserver_send_msg: Unknown message type %d",
msg->hdr.msgtype);
return -1;
}
/* Make a copy of the message and put in the fifo. Once the fifo
gets drained by the write thread, the message will be freed. */
/* NB: Given "msg" is untouched in this function. */
msg2 = msg_dup(msg);
/* Enqueue message into corresponding fifo queue */
msg_fifo_push(fifo, msg2);
/* Schedule write thread */
ospf_apiserver_event(event, fd, apiserv);
return 0;
}
int ospf_apiserver_send_reply(struct ospf_apiserver *apiserv, uint32_t seqnr,
uint8_t rc)
{
struct msg *msg = new_msg_reply(seqnr, rc);
int ret;
if (!msg) {
zlog_warn("ospf_apiserver_send_reply: msg_new failed");
#ifdef NOTYET
/* Cannot allocate new message. What should we do? */
ospf_apiserver_free(apiserv);
#endif
return -1;
}
ret = ospf_apiserver_send_msg(apiserv, msg);
msg_free(msg);
return ret;
}
/* -----------------------------------------------------------
* Generic message dispatching handler function
* -----------------------------------------------------------
*/
int ospf_apiserver_handle_msg(struct ospf_apiserver *apiserv, struct msg *msg)
{
int rc;
/* Call corresponding message handler function. */
switch (msg->hdr.msgtype) {
case MSG_REGISTER_OPAQUETYPE:
rc = ospf_apiserver_handle_register_opaque_type(apiserv, msg);
break;
case MSG_UNREGISTER_OPAQUETYPE:
rc = ospf_apiserver_handle_unregister_opaque_type(apiserv, msg);
break;
case MSG_REGISTER_EVENT:
rc = ospf_apiserver_handle_register_event(apiserv, msg);
break;
case MSG_SYNC_LSDB:
rc = ospf_apiserver_handle_sync_lsdb(apiserv, msg);
break;
case MSG_ORIGINATE_REQUEST:
rc = ospf_apiserver_handle_originate_request(apiserv, msg);
break;
case MSG_DELETE_REQUEST:
rc = ospf_apiserver_handle_delete_request(apiserv, msg);
break;
default:
zlog_warn("ospf_apiserver_handle_msg: Unknown message type: %d",
msg->hdr.msgtype);
rc = -1;
}
return rc;
}
/* -----------------------------------------------------------
* Following are functions for opaque type registration
* -----------------------------------------------------------
*/
int ospf_apiserver_register_opaque_type(struct ospf_apiserver *apiserv,
uint8_t lsa_type, uint8_t opaque_type)
{
struct registered_opaque_type *regtype;
int (*originator_func)(void *arg);
int rc;
switch (lsa_type) {
case OSPF_OPAQUE_LINK_LSA:
originator_func = ospf_apiserver_lsa9_originator;
break;
case OSPF_OPAQUE_AREA_LSA:
originator_func = ospf_apiserver_lsa10_originator;
break;
case OSPF_OPAQUE_AS_LSA:
originator_func = ospf_apiserver_lsa11_originator;
break;
default:
zlog_warn("ospf_apiserver_register_opaque_type: lsa_type(%d)",
lsa_type);
return OSPF_API_ILLEGALLSATYPE;
}
/* Register opaque function table */
/* NB: Duplicated registration will be detected inside the function. */
rc = ospf_register_opaque_functab(
lsa_type, opaque_type, NULL, /* ospf_apiserver_new_if */
NULL, /* ospf_apiserver_del_if */
NULL, /* ospf_apiserver_ism_change */
NULL, /* ospf_apiserver_nsm_change */
NULL, NULL, NULL, ospf_apiserver_show_info, originator_func,
ospf_apiserver_lsa_refresher,
NULL, /* ospf_apiserver_lsa_update */
NULL /* ospf_apiserver_lsa_delete */);
if (rc != 0) {
flog_warn(EC_OSPF_OPAQUE_REGISTRATION,
"Failed to register opaque type [%d/%d]", lsa_type,
opaque_type);
return OSPF_API_OPAQUETYPEINUSE;
}
/* Remember the opaque type that application registers so when
connection shuts down, we can flush all LSAs of this opaque
type. */
regtype = XCALLOC(MTYPE_OSPF_APISERVER,
sizeof(struct registered_opaque_type));
regtype->lsa_type = lsa_type;
regtype->opaque_type = opaque_type;
/* Add to list of registered opaque types */
listnode_add(apiserv->opaque_types, regtype);
if (IS_DEBUG_OSPF_EVENT)
zlog_debug(
"API: Add LSA-type(%d)/Opaque-type(%d) into apiserv(%p), total#(%d)",
lsa_type, opaque_type, (void *)apiserv,
listcount(apiserv->opaque_types));
return 0;
}
int ospf_apiserver_unregister_opaque_type(struct ospf_apiserver *apiserv,
uint8_t lsa_type, uint8_t opaque_type)
{
struct listnode *node, *nnode;
struct registered_opaque_type *regtype;
for (ALL_LIST_ELEMENTS(apiserv->opaque_types, node, nnode, regtype)) {
/* Check if we really registered this opaque type */
if (regtype->lsa_type == lsa_type
&& regtype->opaque_type == opaque_type) {
/* Yes, we registered this opaque type. Flush
all existing opaque LSAs of this type */
ospf_apiserver_flush_opaque_lsa(apiserv, lsa_type,
opaque_type);
ospf_delete_opaque_functab(lsa_type, opaque_type);
/* Remove from list of registered opaque types */
listnode_delete(apiserv->opaque_types, regtype);
if (IS_DEBUG_OSPF_EVENT)
zlog_debug(
"API: Del LSA-type(%d)/Opaque-type(%d) from apiserv(%p), total#(%d)",
lsa_type, opaque_type, (void *)apiserv,
listcount(apiserv->opaque_types));
return 0;
}
}
/* Opaque type is not registered */
zlog_warn("Failed to unregister opaque type [%d/%d]", lsa_type,
opaque_type);
return OSPF_API_OPAQUETYPENOTREGISTERED;
}
static int apiserver_is_opaque_type_registered(struct ospf_apiserver *apiserv,
uint8_t lsa_type,
uint8_t opaque_type)
{
struct listnode *node, *nnode;
struct registered_opaque_type *regtype;
/* XXX: how many types are there? if few, why not just a bitmap? */
for (ALL_LIST_ELEMENTS(apiserv->opaque_types, node, nnode, regtype)) {
/* Check if we really registered this opaque type */
if (regtype->lsa_type == lsa_type
&& regtype->opaque_type == opaque_type) {
/* Yes registered */
return 1;
}
}
/* Not registered */
return 0;
}
int ospf_apiserver_handle_register_opaque_type(struct ospf_apiserver *apiserv,
struct msg *msg)
{
struct msg_register_opaque_type *rmsg;
uint8_t lsa_type;
uint8_t opaque_type;
int rc = 0;
/* Extract parameters from register opaque type message */
rmsg = (struct msg_register_opaque_type *)STREAM_DATA(msg->s);
lsa_type = rmsg->lsatype;
opaque_type = rmsg->opaquetype;
rc = ospf_apiserver_register_opaque_type(apiserv, lsa_type,
opaque_type);
/* Send a reply back to client including return code */
rc = ospf_apiserver_send_reply(apiserv, ntohl(msg->hdr.msgseq), rc);
if (rc < 0)
goto out;
/* Now inform application about opaque types that are ready */
switch (lsa_type) {
case OSPF_OPAQUE_LINK_LSA:
ospf_apiserver_notify_ready_type9(apiserv);
break;
case OSPF_OPAQUE_AREA_LSA:
ospf_apiserver_notify_ready_type10(apiserv);
break;
case OSPF_OPAQUE_AS_LSA:
ospf_apiserver_notify_ready_type11(apiserv);
break;
}
out:
return rc;
}
/* Notify specific client about all opaque types 9 that are ready. */
void ospf_apiserver_notify_ready_type9(struct ospf_apiserver *apiserv)
{
struct listnode *node, *nnode;
struct listnode *node2, *nnode2;
struct ospf *ospf;
struct ospf_interface *oi;
struct registered_opaque_type *r;
ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi)) {
/* Check if this interface is indeed ready for type 9 */
if (!ospf_apiserver_is_ready_type9(oi))