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
|
/*
* Client side of OSPF API.
* Copyright (C) 2001, 2002, 2003 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 GNU Zebra; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <zebra.h>
#include <lib/version.h>
#include "getopt.h"
#include "thread.h"
#include "prefix.h"
#include "linklist.h"
#include "if.h"
#include "vector.h"
#include "vty.h"
#include "command.h"
#include "filter.h"
#include "stream.h"
#include "log.h"
#include "memory.h"
#include "ospfd/ospfd.h"
#include "ospfd/ospf_interface.h"
#include "ospfd/ospf_asbr.h"
#include "ospfd/ospf_lsa.h"
#include "ospfd/ospf_opaque.h"
#include "ospfd/ospf_lsdb.h"
#include "ospfd/ospf_neighbor.h"
#include "ospfd/ospf_dump.h"
#include "ospfd/ospf_zebra.h"
#include "ospfd/ospf_api.h"
#include "ospf_apiclient.h"
/* Backlog for listen */
#define BACKLOG 5
/* -----------------------------------------------------------
* Forward declarations
* -----------------------------------------------------------
*/
void ospf_apiclient_handle_reply (struct ospf_apiclient *oclient,
struct msg *msg);
void ospf_apiclient_handle_update_notify (struct ospf_apiclient *oclient,
struct msg *msg);
void ospf_apiclient_handle_delete_notify (struct ospf_apiclient *oclient,
struct msg *msg);
/* -----------------------------------------------------------
* Initialization
* -----------------------------------------------------------
*/
static unsigned short
ospf_apiclient_getport (void)
{
struct servent *sp = getservbyname ("ospfapi", "tcp");
return sp ? ntohs (sp->s_port) : OSPF_API_SYNC_PORT;
}
/* -----------------------------------------------------------
* Followings are functions for connection management
* -----------------------------------------------------------
*/
struct ospf_apiclient *
ospf_apiclient_connect (char *host, int syncport)
{
struct sockaddr_in myaddr_sync;
struct sockaddr_in myaddr_async;
struct sockaddr_in peeraddr;
struct hostent *hp;
struct ospf_apiclient *new;
int size = 0;
unsigned int peeraddrlen;
int async_server_sock;
int fd1, fd2;
int ret;
int on = 1;
/* There are two connections between the client and the server.
First the client opens a connection for synchronous requests/replies
to the server. The server will accept this connection and
as a reaction open a reverse connection channel for
asynchronous messages. */
async_server_sock = socket (AF_INET, SOCK_STREAM, 0);
if (async_server_sock < 0)
{
fprintf (stderr,
"ospf_apiclient_connect: creating async socket failed\n");
return NULL;
}
/* Prepare socket for asynchronous messages */
/* Initialize async address structure */
memset (&myaddr_async, 0, sizeof (struct sockaddr_in));
myaddr_async.sin_family = AF_INET;
myaddr_async.sin_addr.s_addr = htonl (INADDR_ANY);
myaddr_async.sin_port = htons (syncport+1);
size = sizeof (struct sockaddr_in);
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
myaddr_async.sin_len = size;
#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
/* This is a server socket, reuse addr and port */
ret = setsockopt (async_server_sock, SOL_SOCKET,
SO_REUSEADDR, (void *) &on, sizeof (on));
if (ret < 0)
{
fprintf (stderr, "ospf_apiclient_connect: SO_REUSEADDR failed\n");
close (async_server_sock);
return NULL;
}
#ifdef SO_REUSEPORT
ret = setsockopt (async_server_sock, SOL_SOCKET, SO_REUSEPORT,
(void *) &on, sizeof (on));
if (ret < 0)
{
fprintf (stderr, "ospf_apiclient_connect: SO_REUSEPORT failed\n");
close (async_server_sock);
return NULL;
}
#endif /* SO_REUSEPORT */
/* Bind socket to address structure */
ret = bind (async_server_sock, (struct sockaddr *) &myaddr_async, size);
if (ret < 0)
{
fprintf (stderr, "ospf_apiclient_connect: bind async socket failed\n");
close (async_server_sock);
return NULL;
}
/* Wait for reverse channel connection establishment from server */
ret = listen (async_server_sock, BACKLOG);
if (ret < 0)
{
fprintf (stderr, "ospf_apiclient_connect: listen: %s\n", safe_strerror (errno));
close (async_server_sock);
return NULL;
}
/* Make connection for synchronous requests and connect to server */
/* Resolve address of server */
hp = gethostbyname (host);
if (!hp)
{
fprintf (stderr, "ospf_apiclient_connect: no such host %s\n", host);
close (async_server_sock);
return NULL;
}
fd1 = socket (AF_INET, SOCK_STREAM, 0);
if (fd1 < 0)
{
fprintf (stderr,
"ospf_apiclient_connect: creating sync socket failed\n");
return NULL;
}
/* Reuse addr and port */
ret = setsockopt (fd1, SOL_SOCKET,
SO_REUSEADDR, (void *) &on, sizeof (on));
if (ret < 0)
{
fprintf (stderr, "ospf_apiclient_connect: SO_REUSEADDR failed\n");
close (fd1);
return NULL;
}
#ifdef SO_REUSEPORT
ret = setsockopt (fd1, SOL_SOCKET, SO_REUSEPORT,
(void *) &on, sizeof (on));
if (ret < 0)
{
fprintf (stderr, "ospf_apiclient_connect: SO_REUSEPORT failed\n");
close (fd1);
return NULL;
}
#endif /* SO_REUSEPORT */
/* Bind sync socket to address structure. This is needed since we
want the sync port number on a fixed port number. The reverse
async channel will be at this port+1 */
memset (&myaddr_sync, 0, sizeof (struct sockaddr_in));
myaddr_sync.sin_family = AF_INET;
myaddr_sync.sin_port = htons (syncport);
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
myaddr_sync.sin_len = sizeof (struct sockaddr_in);
#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
ret = bind (fd1, (struct sockaddr *) &myaddr_sync, size);
if (ret < 0)
{
fprintf (stderr, "ospf_apiclient_connect: bind sync socket failed\n");
close (fd1);
return NULL;
}
/* Prepare address structure for connect */
memcpy (&myaddr_sync.sin_addr, hp->h_addr, hp->h_length);
myaddr_sync.sin_family = AF_INET;
myaddr_sync.sin_port = htons(ospf_apiclient_getport ());
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
myaddr_sync.sin_len = sizeof (struct sockaddr_in);
#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
/* Now establish synchronous channel with OSPF daemon */
ret = connect (fd1, (struct sockaddr *) &myaddr_sync,
sizeof (struct sockaddr_in));
if (ret < 0)
{
fprintf (stderr, "ospf_apiclient_connect: sync connect failed\n");
close (async_server_sock);
close (fd1);
return NULL;
}
/* Accept reverse connection */
peeraddrlen = sizeof (struct sockaddr_in);
memset (&peeraddr, 0, peeraddrlen);
fd2 =
accept (async_server_sock, (struct sockaddr *) &peeraddr, &peeraddrlen);
if (fd2 < 0)
{
fprintf (stderr, "ospf_apiclient_connect: accept async failed\n");
close (async_server_sock);
close (fd1);
return NULL;
}
/* Server socket is not needed anymore since we are not accepting more
connections */
close (async_server_sock);
/* Create new client-side instance */
new = XCALLOC (MTYPE_OSPF_APICLIENT, sizeof (struct ospf_apiclient));
/* Initialize socket descriptors for sync and async channels */
new->fd_sync = fd1;
new->fd_async = fd2;
return new;
}
int
ospf_apiclient_close (struct ospf_apiclient *oclient)
{
if (oclient->fd_sync >= 0)
{
close (oclient->fd_sync);
}
if (oclient->fd_async >= 0)
{
close (oclient->fd_async);
}
/* Free client structure */
XFREE (MTYPE_OSPF_APICLIENT, oclient);
return 0;
}
/* -----------------------------------------------------------
* Followings are functions to send a request to OSPFd
* -----------------------------------------------------------
*/
/* Send synchronous request, wait for reply */
static int
ospf_apiclient_send_request (struct ospf_apiclient *oclient, struct msg *msg)
{
u_int32_t reqseq;
struct msg_reply *msgreply;
int rc;
/* NB: Given "msg" is freed inside this function. */
/* Remember the sequence number of the request */
reqseq = ntohl (msg->hdr.msgseq);
/* Write message to OSPFd */
rc = msg_write (oclient->fd_sync, msg);
msg_free (msg);
if (rc < 0)
{
return -1;
}
/* Wait for reply *//* NB: New "msg" is allocated by "msg_read()". */
msg = msg_read (oclient->fd_sync);
if (!msg)
return -1;
assert (msg->hdr.msgtype == MSG_REPLY);
assert (ntohl (msg->hdr.msgseq) == reqseq);
msgreply = (struct msg_reply *) STREAM_DATA (msg->s);
rc = msgreply->errcode;
msg_free (msg);
return rc;
}
/* -----------------------------------------------------------
* Helper functions
* -----------------------------------------------------------
*/
static u_int32_t
ospf_apiclient_get_seqnr (void)
{
static u_int32_t seqnr = MIN_SEQ;
u_int32_t tmp;
tmp = seqnr;
/* Increment sequence number */
if (seqnr < MAX_SEQ)
{
seqnr++;
}
else
{
seqnr = MIN_SEQ;
}
return tmp;
}
/* -----------------------------------------------------------
* API to access OSPF daemon by client applications.
* -----------------------------------------------------------
*/
/*
* Synchronous request to register opaque type.
*/
int
ospf_apiclient_register_opaque_type (struct ospf_apiclient *cl,
u_char ltype, u_char otype)
{
struct msg *msg;
int rc;
/* just put 1 as a sequence number. */
msg = new_msg_register_opaque_type (ospf_apiclient_get_seqnr (),
ltype, otype);
if (!msg)
{
fprintf (stderr, "new_msg_register_opaque_type failed\n");
return -1;
}
rc = ospf_apiclient_send_request (cl, msg);
return rc;
}
/*
* Synchronous request to synchronize with OSPF's LSDB.
* Two steps required: register_event in order to get
* dynamic updates and LSDB_Sync.
*/
int
ospf_apiclient_sync_lsdb (struct ospf_apiclient *oclient)
{
struct msg *msg;
int rc;
struct lsa_filter_type filter;
filter.typemask = 0xFFFF; /* all LSAs */
filter.origin = ANY_ORIGIN;
filter.num_areas = 0; /* all Areas. */
msg = new_msg_register_event (ospf_apiclient_get_seqnr (), &filter);
if (!msg)
{
fprintf (stderr, "new_msg_register_event failed\n");
return -1;
}
rc = ospf_apiclient_send_request (oclient, msg);
if (rc != 0)
goto out;
msg = new_msg_sync_lsdb (ospf_apiclient_get_seqnr (), &filter);
if (!msg)
{
fprintf (stderr, "new_msg_sync_lsdb failed\n");
return -1;
}
rc = ospf_apiclient_send_request (oclient, msg);
out:
return rc;
}
/*
* Synchronous request to originate or update an LSA.
*/
int
ospf_apiclient_lsa_originate (struct ospf_apiclient *oclient,
struct in_addr ifaddr,
struct in_addr area_id,
u_char lsa_type,
u_char opaque_type, u_int32_t opaque_id,
void *opaquedata, int opaquelen)
{
struct msg *msg;
int rc;
u_char buf[OSPF_MAX_LSA_SIZE];
struct lsa_header *lsah;
u_int32_t tmp;
/* We can only originate opaque LSAs */
if (!IS_OPAQUE_LSA (lsa_type))
{
fprintf (stderr, "Cannot originate non-opaque LSA type %d\n", lsa_type);
return OSPF_API_ILLEGALLSATYPE;
}
/* Make a new LSA from parameters */
lsah = (struct lsa_header *) buf;
lsah->ls_age = 0;
lsah->options = 0;
lsah->type = lsa_type;
tmp = SET_OPAQUE_LSID (opaque_type, opaque_id);
lsah->id.s_addr = htonl (tmp);
lsah->adv_router.s_addr = 0;
lsah->ls_seqnum = 0;
lsah->checksum = 0;
lsah->length = htons (sizeof (struct lsa_header) + opaquelen);
memcpy (((u_char *) lsah) + sizeof (struct lsa_header), opaquedata,
opaquelen);
msg = new_msg_originate_request (ospf_apiclient_get_seqnr (),
ifaddr, area_id, lsah);
if (!msg)
{
fprintf (stderr, "new_msg_originate_request failed\n");
return OSPF_API_NOMEMORY;
}
rc = ospf_apiclient_send_request (oclient, msg);
return rc;
}
int
ospf_apiclient_lsa_delete (struct ospf_apiclient *oclient,
struct in_addr area_id, u_char lsa_type,
u_char opaque_type, u_int32_t opaque_id)
{
struct msg *msg;
int rc;
/* Only opaque LSA can be deleted */
if (!IS_OPAQUE_LSA (lsa_type))
{
fprintf (stderr, "Cannot delete non-opaque LSA type %d\n", lsa_type);
return OSPF_API_ILLEGALLSATYPE;
}
/* opaque_id is in host byte order and will be converted
* to network byte order by new_msg_delete_request */
msg = new_msg_delete_request (ospf_apiclient_get_seqnr (),
area_id, lsa_type, opaque_type, opaque_id);
rc = ospf_apiclient_send_request (oclient, msg);
return rc;
}
/* -----------------------------------------------------------
* Followings are handlers for messages from OSPF daemon
* -----------------------------------------------------------
*/
static void
ospf_apiclient_handle_ready (struct ospf_apiclient *oclient, struct msg *msg)
{
struct msg_ready_notify *r;
r = (struct msg_ready_notify *) STREAM_DATA (msg->s);
/* Invoke registered callback function. */
if (oclient->ready_notify)
{
(oclient->ready_notify) (r->lsa_type, r->opaque_type, r->addr);
}
}
static void
ospf_apiclient_handle_new_if (struct ospf_apiclient *oclient, struct msg *msg)
{
struct msg_new_if *n;
n = (struct msg_new_if *) STREAM_DATA (msg->s);
/* Invoke registered callback function. */
if (oclient->new_if)
{
(oclient->new_if) (n->ifaddr, n->area_id);
}
}
static void
ospf_apiclient_handle_del_if (struct ospf_apiclient *oclient, struct msg *msg)
{
struct msg_del_if *d;
d = (struct msg_del_if *) STREAM_DATA (msg->s);
/* Invoke registered callback function. */
if (oclient->del_if)
{
(oclient->del_if) (d->ifaddr);
}
}
static void
ospf_apiclient_handle_ism_change (struct ospf_apiclient *oclient,
struct msg *msg)
{
struct msg_ism_change *m;
m = (struct msg_ism_change *) STREAM_DATA (msg->s);
/* Invoke registered callback function. */
if (oclient->ism_change)
{
(oclient->ism_change) (m->ifaddr, m->area_id, m->status);
}
}
static void
ospf_apiclient_handle_nsm_change (struct ospf_apiclient *oclient,
struct msg *msg)
{
struct msg_nsm_change *m;
m = (struct msg_nsm_change *) STREAM_DATA (msg->s);
/* Invoke registered callback function. */
if (oclient->nsm_change)
{
(oclient->nsm_change) (m->ifaddr, m->nbraddr, m->router_id, m->status);
}
}
static void
ospf_apiclient_handle_lsa_update (struct ospf_apiclient *oclient,
struct msg *msg)
{
struct msg_lsa_change_notify *cn;
struct lsa_header *lsa;
int lsalen;
cn = (struct msg_lsa_change_notify *) STREAM_DATA (msg->s);
/* Extract LSA from message */
lsalen = ntohs (cn->data.length);
lsa = XMALLOC (MTYPE_OSPF_APICLIENT, lsalen);
if (!lsa)
{
fprintf (stderr, "LSA update: Cannot allocate memory for LSA\n");
return;
}
memcpy (lsa, &(cn->data), lsalen);
/* Invoke registered update callback function */
if (oclient->update_notify)
{
(oclient->update_notify) (cn->ifaddr, cn->area_id,
cn->is_self_originated, lsa);
}
/* free memory allocated by ospf apiclient library */
XFREE (MTYPE_OSPF_APICLIENT, lsa);
}
static void
ospf_apiclient_handle_lsa_delete (struct ospf_apiclient *oclient,
struct msg *msg)
{
struct msg_lsa_change_notify *cn;
struct lsa_header *lsa;
int lsalen;
cn = (struct msg_lsa_change_notify *) STREAM_DATA (msg->s);
/* Extract LSA from message */
lsalen = ntohs (cn->data.length);
lsa = XMALLOC (MTYPE_OSPF_APICLIENT, lsalen);
if (!lsa)
{
fprintf (stderr, "LSA delete: Cannot allocate memory for LSA\n");
return;
}
memcpy (lsa, &(cn->data), lsalen);
/* Invoke registered update callback function */
if (oclient->delete_notify)
{
(oclient->delete_notify) (cn->ifaddr, cn->area_id,
cn->is_self_originated, lsa);
}
/* free memory allocated by ospf apiclient library */
XFREE (MTYPE_OSPF_APICLIENT, lsa);
}
static void
ospf_apiclient_msghandle (struct ospf_apiclient *oclient, struct msg *msg)
{
/* Call message handler function. */
switch (msg->hdr.msgtype)
{
case MSG_READY_NOTIFY:
ospf_apiclient_handle_ready (oclient, msg);
break;
case MSG_NEW_IF:
ospf_apiclient_handle_new_if (oclient, msg);
break;
case MSG_DEL_IF:
ospf_apiclient_handle_del_if (oclient, msg);
break;
case MSG_ISM_CHANGE:
ospf_apiclient_handle_ism_change (oclient, msg);
break;
case MSG_NSM_CHANGE:
ospf_apiclient_handle_nsm_change (oclient, msg);
break;
case MSG_LSA_UPDATE_NOTIFY:
ospf_apiclient_handle_lsa_update (oclient, msg);
break;
case MSG_LSA_DELETE_NOTIFY:
ospf_apiclient_handle_lsa_delete (oclient, msg);
break;
default:
fprintf (stderr, "ospf_apiclient_read: Unknown message type: %d\n",
msg->hdr.msgtype);
break;
}
}
/* -----------------------------------------------------------
* Callback handler registration
* -----------------------------------------------------------
*/
void
ospf_apiclient_register_callback (struct ospf_apiclient *oclient,
void (*ready_notify) (u_char lsa_type,
u_char opaque_type,
struct in_addr addr),
void (*new_if) (struct in_addr ifaddr,
struct in_addr area_id),
void (*del_if) (struct in_addr ifaddr),
void (*ism_change) (struct in_addr ifaddr,
struct in_addr area_id,
u_char status),
void (*nsm_change) (struct in_addr ifaddr,
struct in_addr nbraddr,
struct in_addr
router_id,
u_char status),
void (*update_notify) (struct in_addr
ifaddr,
struct in_addr
area_id,
u_char self_origin,
struct lsa_header *
lsa),
void (*delete_notify) (struct in_addr
ifaddr,
struct in_addr
area_id,
u_char self_origin,
struct lsa_header *
lsa))
{
assert (oclient);
assert (update_notify);
/* Register callback function */
oclient->ready_notify = ready_notify;
oclient->new_if = new_if;
oclient->del_if = del_if;
oclient->ism_change = ism_change;
oclient->nsm_change = nsm_change;
oclient->update_notify = update_notify;
oclient->delete_notify = delete_notify;
}
/* -----------------------------------------------------------
* Asynchronous message handling
* -----------------------------------------------------------
*/
int
ospf_apiclient_handle_async (struct ospf_apiclient *oclient)
{
struct msg *msg;
/* Get a message */
msg = msg_read (oclient->fd_async);
if (!msg)
{
/* Connection broke down */
return -1;
}
/* Handle message */
ospf_apiclient_msghandle (oclient, msg);
/* Don't forget to free this message */
msg_free (msg);
return 0;
}
|