forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathospf_lsa.c
3737 lines (3082 loc) · 96.5 KB
/
ospf_lsa.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
/*
* OSPF Link State Advertisement
* Copyright (C) 1999, 2000 Toshiaki Takada
*
* 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>
#include "monotime.h"
#include "linklist.h"
#include "prefix.h"
#include "if.h"
#include "table.h"
#include "memory.h"
#include "stream.h"
#include "log.h"
#include "thread.h"
#include "hash.h"
#include "sockunion.h" /* for inet_aton() */
#include "checksum.h"
#include "ospfd/ospfd.h"
#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_abr.h"
#include "ospfd/ospf_errors.h"
uint32_t get_metric(uint8_t *metric)
{
uint32_t m;
m = metric[0];
m = (m << 8) + metric[1];
m = (m << 8) + metric[2];
return m;
}
struct timeval int2tv(int a)
{
struct timeval ret;
ret.tv_sec = a;
ret.tv_usec = 0;
return ret;
}
struct timeval msec2tv(int a)
{
struct timeval ret;
ret.tv_sec = a / 1000;
ret.tv_usec = (a % 1000) * 1000;
return ret;
}
int ospf_lsa_refresh_delay(struct ospf_lsa *lsa)
{
struct timeval delta;
int delay = 0;
if (monotime_since(&lsa->tv_orig, &delta)
< OSPF_MIN_LS_INTERVAL * 1000LL) {
struct timeval minv = msec2tv(OSPF_MIN_LS_INTERVAL);
timersub(&minv, &delta, &minv);
/* TBD: remove padding to full sec, return timeval instead */
delay = minv.tv_sec + !!minv.tv_usec;
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
zlog_debug(
"LSA[Type%d:%s]: Refresh timer delay %d seconds",
lsa->data->type, inet_ntoa(lsa->data->id),
delay);
assert(delay > 0);
}
return delay;
}
int get_age(struct ospf_lsa *lsa)
{
struct timeval rel;
monotime_since(&lsa->tv_recv, &rel);
return ntohs(lsa->data->ls_age) + rel.tv_sec;
}
/* Fletcher Checksum -- Refer to RFC1008. */
/* All the offsets are zero-based. The offsets in the RFC1008 are
one-based. */
uint16_t ospf_lsa_checksum(struct lsa_header *lsa)
{
uint8_t *buffer = (uint8_t *)&lsa->options;
int options_offset = buffer - (uint8_t *)&lsa->ls_age; /* should be 2 */
/* Skip the AGE field */
uint16_t len = ntohs(lsa->length) - options_offset;
/* Checksum offset starts from "options" field, not the beginning of the
lsa_header struct. The offset is 14, rather than 16. */
int checksum_offset = (uint8_t *)&lsa->checksum - buffer;
return fletcher_checksum(buffer, len, checksum_offset);
}
int ospf_lsa_checksum_valid(struct lsa_header *lsa)
{
uint8_t *buffer = (uint8_t *)&lsa->options;
int options_offset = buffer - (uint8_t *)&lsa->ls_age; /* should be 2 */
/* Skip the AGE field */
uint16_t len = ntohs(lsa->length) - options_offset;
return (fletcher_checksum(buffer, len, FLETCHER_CHECKSUM_VALIDATE)
== 0);
}
/* Create OSPF LSA. */
struct ospf_lsa *ospf_lsa_new(void)
{
struct ospf_lsa *new;
new = XCALLOC(MTYPE_OSPF_LSA, sizeof(struct ospf_lsa));
new->flags = 0;
new->lock = 1;
new->retransmit_counter = 0;
monotime(&new->tv_recv);
new->tv_orig = new->tv_recv;
new->refresh_list = -1;
new->vrf_id = VRF_DEFAULT;
return new;
}
struct ospf_lsa *ospf_lsa_new_and_data(size_t size)
{
struct ospf_lsa *new;
new = ospf_lsa_new();
new->data = ospf_lsa_data_new(size);
return new;
}
/* Duplicate OSPF LSA. */
struct ospf_lsa *ospf_lsa_dup(struct ospf_lsa *lsa)
{
struct ospf_lsa *new;
if (lsa == NULL)
return NULL;
new = XCALLOC(MTYPE_OSPF_LSA, sizeof(struct ospf_lsa));
memcpy(new, lsa, sizeof(struct ospf_lsa));
UNSET_FLAG(new->flags, OSPF_LSA_DISCARD);
new->lock = 1;
new->retransmit_counter = 0;
new->data = ospf_lsa_data_dup(lsa->data);
/* kevinm: Clear the refresh_list, otherwise there are going
to be problems when we try to remove the LSA from the
queue (which it's not a member of.)
XXX: Should we add the LSA to the refresh_list queue? */
new->refresh_list = -1;
if (IS_DEBUG_OSPF(lsa, LSA))
zlog_debug("LSA: duplicated %p (new: %p)", (void *)lsa,
(void *)new);
return new;
}
/* Free OSPF LSA. */
void ospf_lsa_free(struct ospf_lsa *lsa)
{
assert(lsa->lock == 0);
if (IS_DEBUG_OSPF(lsa, LSA))
zlog_debug("LSA: freed %p", (void *)lsa);
/* Delete LSA data. */
if (lsa->data != NULL)
ospf_lsa_data_free(lsa->data);
assert(lsa->refresh_list < 0);
memset(lsa, 0, sizeof(struct ospf_lsa));
XFREE(MTYPE_OSPF_LSA, lsa);
}
/* Lock LSA. */
struct ospf_lsa *ospf_lsa_lock(struct ospf_lsa *lsa)
{
lsa->lock++;
return lsa;
}
/* Unlock LSA. */
void ospf_lsa_unlock(struct ospf_lsa **lsa)
{
/* This is sanity check. */
if (!lsa || !*lsa)
return;
(*lsa)->lock--;
assert((*lsa)->lock >= 0);
if ((*lsa)->lock == 0) {
assert(CHECK_FLAG((*lsa)->flags, OSPF_LSA_DISCARD));
ospf_lsa_free(*lsa);
*lsa = NULL;
}
}
/* Check discard flag. */
void ospf_lsa_discard(struct ospf_lsa *lsa)
{
if (!CHECK_FLAG(lsa->flags, OSPF_LSA_DISCARD)) {
SET_FLAG(lsa->flags, OSPF_LSA_DISCARD);
ospf_lsa_unlock(&lsa);
}
}
/* Create LSA data. */
struct lsa_header *ospf_lsa_data_new(size_t size)
{
return XCALLOC(MTYPE_OSPF_LSA_DATA, size);
}
/* Duplicate LSA data. */
struct lsa_header *ospf_lsa_data_dup(struct lsa_header *lsah)
{
struct lsa_header *new;
new = ospf_lsa_data_new(ntohs(lsah->length));
memcpy(new, lsah, ntohs(lsah->length));
return new;
}
/* Free LSA data. */
void ospf_lsa_data_free(struct lsa_header *lsah)
{
if (IS_DEBUG_OSPF(lsa, LSA))
zlog_debug("LSA[Type%d:%s]: data freed %p", lsah->type,
inet_ntoa(lsah->id), (void *)lsah);
XFREE(MTYPE_OSPF_LSA_DATA, lsah);
}
/* LSA general functions. */
const char *dump_lsa_key(struct ospf_lsa *lsa)
{
static char buf[] = {"Type255,id(255.255.255.255),ar(255.255.255.255)"};
struct lsa_header *lsah;
if (lsa != NULL && (lsah = lsa->data) != NULL) {
char id[INET_ADDRSTRLEN], ar[INET_ADDRSTRLEN];
strlcpy(id, inet_ntoa(lsah->id), sizeof(id));
strlcpy(ar, inet_ntoa(lsah->adv_router), sizeof(ar));
sprintf(buf, "Type%d,id(%s),ar(%s)", lsah->type, id, ar);
} else
strlcpy(buf, "NULL", sizeof(buf));
return buf;
}
uint32_t lsa_seqnum_increment(struct ospf_lsa *lsa)
{
uint32_t seqnum;
seqnum = ntohl(lsa->data->ls_seqnum) + 1;
return htonl(seqnum);
}
void lsa_header_set(struct stream *s, uint8_t options, uint8_t type,
struct in_addr id, struct in_addr router_id)
{
struct lsa_header *lsah;
lsah = (struct lsa_header *)STREAM_DATA(s);
lsah->ls_age = htons(OSPF_LSA_INITIAL_AGE);
lsah->options = options;
lsah->type = type;
lsah->id = id;
lsah->adv_router = router_id;
lsah->ls_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);
stream_forward_endp(s, OSPF_LSA_HEADER_SIZE);
}
/* router-LSA related functions. */
/* Get router-LSA flags. */
static uint8_t router_lsa_flags(struct ospf_area *area)
{
uint8_t flags;
flags = area->ospf->flags;
/* Set virtual link flag. */
if (ospf_full_virtual_nbrs(area))
SET_FLAG(flags, ROUTER_LSA_VIRTUAL);
else
/* Just sanity check */
UNSET_FLAG(flags, ROUTER_LSA_VIRTUAL);
/* Set Shortcut ABR behabiour flag. */
UNSET_FLAG(flags, ROUTER_LSA_SHORTCUT);
if (area->ospf->abr_type == OSPF_ABR_SHORTCUT)
if (!OSPF_IS_AREA_BACKBONE(area))
if ((area->shortcut_configured == OSPF_SHORTCUT_DEFAULT
&& area->ospf->backbone == NULL)
|| area->shortcut_configured
== OSPF_SHORTCUT_ENABLE)
SET_FLAG(flags, ROUTER_LSA_SHORTCUT);
/* ASBR can't exit in stub area. */
if (area->external_routing == OSPF_AREA_STUB)
UNSET_FLAG(flags, ROUTER_LSA_EXTERNAL);
/* If ASBR set External flag */
else if (IS_OSPF_ASBR(area->ospf))
SET_FLAG(flags, ROUTER_LSA_EXTERNAL);
/* Set ABR dependent flags */
if (IS_OSPF_ABR(area->ospf)) {
SET_FLAG(flags, ROUTER_LSA_BORDER);
/* If Area is NSSA and we are both ABR and unconditional
* translator,
* set Nt bit to inform other routers.
*/
if ((area->external_routing == OSPF_AREA_NSSA)
&& (area->NSSATranslatorRole == OSPF_NSSA_ROLE_ALWAYS))
SET_FLAG(flags, ROUTER_LSA_NT);
}
return flags;
}
/* Lookup neighbor other than myself.
And check neighbor count,
Point-to-Point link must have only 1 neighbor. */
struct ospf_neighbor *ospf_nbr_lookup_ptop(struct ospf_interface *oi)
{
struct ospf_neighbor *nbr = NULL;
struct route_node *rn;
/* Search neighbor, there must be one of two nbrs. */
for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
if ((nbr = rn->info))
if (!IPV4_ADDR_SAME(&nbr->router_id,
&oi->ospf->router_id))
if (nbr->state == NSM_Full) {
route_unlock_node(rn);
break;
}
/* PtoP link must have only 1 neighbor. */
if (ospf_nbr_count(oi, 0) > 1)
flog_warn(EC_OSPF_PTP_NEIGHBOR,
"Point-to-Point link has more than 1 neighobrs.");
return nbr;
}
/* Determine cost of link, taking RFC3137 stub-router support into
* consideration
*/
static uint16_t ospf_link_cost(struct ospf_interface *oi)
{
/* RFC3137 stub router support */
if (!CHECK_FLAG(oi->area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED))
return oi->output_cost;
else
return OSPF_OUTPUT_COST_INFINITE;
}
/* Set a link information. */
static char link_info_set(struct stream **s, struct in_addr id,
struct in_addr data, uint8_t type, uint8_t tos,
uint16_t cost)
{
/* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
* vast majority of cases. Some rare routers with lots of links need
* more.
* we try accomodate those here.
*/
if (STREAM_WRITEABLE(*s) < OSPF_ROUTER_LSA_LINK_SIZE) {
size_t ret = OSPF_MAX_LSA_SIZE;
/* Can we enlarge the stream still? */
if (STREAM_SIZE(*s) == OSPF_MAX_LSA_SIZE) {
/* we futz the size here for simplicity, really we need
* to account
* for just:
* IP Header - (sizeof (struct ip))
* OSPF Header - OSPF_HEADER_SIZE
* LSA Header - OSPF_LSA_HEADER_SIZE
* MD5 auth data, if MD5 is configured -
* OSPF_AUTH_MD5_SIZE.
*
* Simpler just to subtract OSPF_MAX_LSA_SIZE though.
*/
ret = stream_resize_inplace(
s, OSPF_MAX_PACKET_SIZE - OSPF_MAX_LSA_SIZE);
}
if (ret == OSPF_MAX_LSA_SIZE) {
flog_warn(
EC_OSPF_LSA_SIZE,
"%s: Out of space in LSA stream, left %zd, size %zd",
__func__, STREAM_WRITEABLE(*s),
STREAM_SIZE(*s));
return 0;
}
}
/* TOS based routing is not supported. */
stream_put_ipv4(*s, id.s_addr); /* Link ID. */
stream_put_ipv4(*s, data.s_addr); /* Link Data. */
stream_putc(*s, type); /* Link Type. */
stream_putc(*s, tos); /* TOS = 0. */
stream_putw(*s, cost); /* Link Cost. */
return 1;
}
/* Describe Point-to-Point link (Section 12.4.1.1). */
static int lsa_link_ptop_set(struct stream **s, struct ospf_interface *oi)
{
int links = 0;
struct ospf_neighbor *nbr;
struct in_addr id, mask, data;
uint16_t cost = ospf_link_cost(oi);
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
zlog_debug("LSA[Type1]: Set link Point-to-Point");
if ((nbr = ospf_nbr_lookup_ptop(oi)))
if (nbr->state == NSM_Full) {
if (CHECK_FLAG(oi->connected->flags,
ZEBRA_IFA_UNNUMBERED)) {
/* For unnumbered point-to-point networks, the
Link Data field
should specify the interface's MIB-II ifIndex
value. */
data.s_addr = htonl(oi->ifp->ifindex);
links += link_info_set(
s, nbr->router_id, data,
LSA_LINK_TYPE_POINTOPOINT, 0, cost);
} else {
links += link_info_set(
s, nbr->router_id,
oi->address->u.prefix4,
LSA_LINK_TYPE_POINTOPOINT, 0, cost);
}
}
/* no need for a stub link for unnumbered interfaces */
if (!CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED)) {
/* Regardless of the state of the neighboring router, we must
add a Type 3 link (stub network).
N.B. Options 1 & 2 share basically the same logic. */
masklen2ip(oi->address->prefixlen, &mask);
id.s_addr = CONNECTED_PREFIX(oi->connected)->u.prefix4.s_addr
& mask.s_addr;
links += link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
oi->output_cost);
}
return links;
}
/* Describe Broadcast Link. */
static int lsa_link_broadcast_set(struct stream **s, struct ospf_interface *oi)
{
struct ospf_neighbor *dr;
struct in_addr id, mask;
uint16_t cost = ospf_link_cost(oi);
/* Describe Type 3 Link. */
if (oi->state == ISM_Waiting) {
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
zlog_debug(
"LSA[Type1]: Interface %s is in state Waiting. "
"Adding stub interface",
oi->ifp->name);
masklen2ip(oi->address->prefixlen, &mask);
id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
oi->output_cost);
}
dr = ospf_nbr_lookup_by_addr(oi->nbrs, &DR(oi));
/* Describe Type 2 link. */
if (dr && (dr->state == NSM_Full
|| IPV4_ADDR_SAME(&oi->address->u.prefix4, &DR(oi)))
&& ospf_nbr_count(oi, NSM_Full) > 0) {
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
zlog_debug(
"LSA[Type1]: Interface %s has a DR. "
"Adding transit interface",
oi->ifp->name);
return link_info_set(s, DR(oi), oi->address->u.prefix4,
LSA_LINK_TYPE_TRANSIT, 0, cost);
}
/* Describe type 3 link. */
else {
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
zlog_debug(
"LSA[Type1]: Interface %s has no DR. "
"Adding stub interface",
oi->ifp->name);
masklen2ip(oi->address->prefixlen, &mask);
id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
oi->output_cost);
}
}
static int lsa_link_loopback_set(struct stream **s, struct ospf_interface *oi)
{
struct in_addr id, mask;
/* Describe Type 3 Link. */
if (oi->state != ISM_Loopback)
return 0;
mask.s_addr = 0xffffffff;
id.s_addr = oi->address->u.prefix4.s_addr;
return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
}
/* Describe Virtual Link. */
static int lsa_link_virtuallink_set(struct stream **s,
struct ospf_interface *oi)
{
struct ospf_neighbor *nbr;
uint16_t cost = ospf_link_cost(oi);
if (oi->state == ISM_PointToPoint)
if ((nbr = ospf_nbr_lookup_ptop(oi)))
if (nbr->state == NSM_Full) {
return link_info_set(s, nbr->router_id,
oi->address->u.prefix4,
LSA_LINK_TYPE_VIRTUALLINK,
0, cost);
}
return 0;
}
#define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
/* this function add for support point-to-multipoint ,see rfc2328
12.4.1.4.*/
/* from "edward rrr" <edward_rrr@hotmail.com>
http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
static int lsa_link_ptomp_set(struct stream **s, struct ospf_interface *oi)
{
int links = 0;
struct route_node *rn;
struct ospf_neighbor *nbr = NULL;
struct in_addr id, mask;
uint16_t cost = ospf_link_cost(oi);
mask.s_addr = 0xffffffff;
id.s_addr = oi->address->u.prefix4.s_addr;
links += link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
zlog_debug("PointToMultipoint: running ptomultip_set");
/* Search neighbor, */
for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
if ((nbr = rn->info) != NULL)
/* Ignore myself. */
if (!IPV4_ADDR_SAME(&nbr->router_id,
&oi->ospf->router_id))
if (nbr->state == NSM_Full)
{
links += link_info_set(
s, nbr->router_id,
oi->address->u.prefix4,
LSA_LINK_TYPE_POINTOPOINT, 0,
cost);
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
zlog_debug(
"PointToMultipoint: set link to %s",
inet_ntoa(
oi->address->u
.prefix4));
}
return links;
}
/* Set router-LSA link information. */
static int router_lsa_link_set(struct stream **s, struct ospf_area *area)
{
struct listnode *node;
struct ospf_interface *oi;
int links = 0;
for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi)) {
struct interface *ifp = oi->ifp;
/* Check interface is up, OSPF is enable. */
if (if_is_operative(ifp)) {
if (oi->state != ISM_Down) {
oi->lsa_pos_beg = links;
/* Describe each link. */
switch (oi->type) {
case OSPF_IFTYPE_POINTOPOINT:
links += lsa_link_ptop_set(s, oi);
break;
case OSPF_IFTYPE_BROADCAST:
links += lsa_link_broadcast_set(s, oi);
break;
case OSPF_IFTYPE_NBMA:
links += lsa_link_nbma_set(s, oi);
break;
case OSPF_IFTYPE_POINTOMULTIPOINT:
links += lsa_link_ptomp_set(s, oi);
break;
case OSPF_IFTYPE_VIRTUALLINK:
links +=
lsa_link_virtuallink_set(s, oi);
break;
case OSPF_IFTYPE_LOOPBACK:
links += lsa_link_loopback_set(s, oi);
}
oi->lsa_pos_end = links;
}
}
}
return links;
}
/* Set router-LSA body. */
static void ospf_router_lsa_body_set(struct stream **s, struct ospf_area *area)
{
unsigned long putp;
uint16_t cnt;
/* Set flags. */
stream_putc(*s, router_lsa_flags(area));
/* Set Zero fields. */
stream_putc(*s, 0);
/* Keep pointer to # links. */
putp = stream_get_endp(*s);
/* Forward word */
stream_putw(*s, 0);
/* Set all link information. */
cnt = router_lsa_link_set(s, area);
/* Set # of links here. */
stream_putw_at(*s, putp, cnt);
}
static int ospf_stub_router_timer(struct thread *t)
{
struct ospf_area *area = THREAD_ARG(t);
area->t_stub_router = NULL;
SET_FLAG(area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
/* clear stub route state and generate router-lsa refresh, don't
* clobber an administratively set stub-router state though.
*/
if (CHECK_FLAG(area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
return 0;
UNSET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
ospf_router_lsa_update_area(area);
return 0;
}
static void ospf_stub_router_check(struct ospf_area *area)
{
/* area must either be administratively configured to be stub
* or startup-time stub-router must be configured and we must in a
* pre-stub
* state.
*/
if (CHECK_FLAG(area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED)) {
SET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
return;
}
/* not admin-stubbed, check whether startup stubbing is configured and
* whether it's not been done yet
*/
if (CHECK_FLAG(area->stub_router_state,
OSPF_AREA_WAS_START_STUB_ROUTED))
return;
if (area->ospf->stub_router_startup_time
== OSPF_STUB_ROUTER_UNCONFIGURED) {
/* stub-router is hence done forever for this area, even if
* someone
* tries configure it (take effect next restart).
*/
SET_FLAG(area->stub_router_state,
OSPF_AREA_WAS_START_STUB_ROUTED);
return;
}
/* startup stub-router configured and not yet done */
SET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
OSPF_AREA_TIMER_ON(area->t_stub_router, ospf_stub_router_timer,
area->ospf->stub_router_startup_time);
}
/* Create new router-LSA. */
static struct ospf_lsa *ospf_router_lsa_new(struct ospf_area *area)
{
struct ospf *ospf = area->ospf;
struct stream *s;
struct lsa_header *lsah;
struct ospf_lsa *new;
int length;
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
zlog_debug("LSA[Type1]: Create router-LSA instance");
/* check whether stub-router is desired, and if this is the first
* router LSA.
*/
ospf_stub_router_check(area);
/* Create a stream for LSA. */
s = stream_new(OSPF_MAX_LSA_SIZE);
/* Set LSA common header fields. */
lsa_header_set(s, LSA_OPTIONS_GET(area) | LSA_OPTIONS_NSSA_GET(area),
OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);
/* Set router-LSA body fields. */
ospf_router_lsa_body_set(&s, area);
/* Set length. */
length = stream_get_endp(s);
lsah = (struct lsa_header *)STREAM_DATA(s);
lsah->length = htons(length);
/* Now, create OSPF LSA instance. */
new = ospf_lsa_new_and_data(length);
new->area = area;
SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
new->vrf_id = area->ospf->vrf_id;
/* Copy LSA data to store, discard stream. */
memcpy(new->data, lsah, length);
stream_free(s);
return new;
}
/* Originate Router-LSA. */
static struct ospf_lsa *ospf_router_lsa_originate(struct ospf_area *area)
{
struct ospf_lsa *new;
/* Create new router-LSA instance. */
if ((new = ospf_router_lsa_new(area)) == NULL) {
zlog_err("%s: ospf_router_lsa_new returned NULL", __func__);
return NULL;
}
/* Sanity check. */
if (new->data->adv_router.s_addr == 0) {
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
ospf_lsa_discard(new);
return NULL;
}
/* Install LSA to LSDB. */
new = ospf_lsa_install(area->ospf, NULL, new);
/* Update LSA origination count. */
area->ospf->lsa_originate_count++;
/* Flooding new LSA through area. */
ospf_flood_through_area(area, NULL, new);
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
zlog_debug("LSA[Type%d:%s]: Originate router-LSA %p",
new->data->type, inet_ntoa(new->data->id),
(void *)new);
ospf_lsa_header_dump(new->data);
}
return new;
}
/* Refresh router-LSA. */
static struct ospf_lsa *ospf_router_lsa_refresh(struct ospf_lsa *lsa)
{
struct ospf_area *area = lsa->area;
struct ospf_lsa *new;
/* Sanity check. */
assert(lsa->data);
/* Delete LSA from neighbor retransmit-list. */
ospf_ls_retransmit_delete_nbr_area(area, lsa);
/* Unregister LSA from refresh-list */
ospf_refresher_unregister_lsa(area->ospf, lsa);
/* Create new router-LSA instance. */
if ((new = ospf_router_lsa_new(area)) == NULL) {
zlog_err("%s: ospf_router_lsa_new returned NULL", __func__);
return NULL;
}
new->data->ls_seqnum = lsa_seqnum_increment(lsa);
ospf_lsa_install(area->ospf, NULL, new);
/* Flood LSA through area. */
ospf_flood_through_area(area, NULL, new);
/* Debug logging. */
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
zlog_debug("LSA[Type%d:%s]: router-LSA refresh",
new->data->type, inet_ntoa(new->data->id));
ospf_lsa_header_dump(new->data);
}
return NULL;
}
int ospf_router_lsa_update_area(struct ospf_area *area)
{
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("[router-LSA]: (router-LSA area update)");
/* Now refresh router-LSA. */
if (area->router_lsa_self)
ospf_lsa_refresh(area->ospf, area->router_lsa_self);
/* Newly originate router-LSA. */
else
ospf_router_lsa_originate(area);
return 0;
}
int ospf_router_lsa_update(struct ospf *ospf)
{
struct listnode *node, *nnode;
struct ospf_area *area;
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
zlog_debug("Timer[router-LSA Update]: (timer expire)");
for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
struct ospf_lsa *lsa = area->router_lsa_self;
struct router_lsa *rl;
const char *area_str;
/* Keep Area ID string. */
area_str = AREA_NAME(area);
/* If LSA not exist in this Area, originate new. */
if (lsa == NULL) {
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
zlog_debug(
"LSA[Type1]: Create router-LSA for Area %s",
area_str);
ospf_router_lsa_originate(area);
}
/* If router-ID is changed, Link ID must change.
First flush old LSA, then originate new. */
else if (!IPV4_ADDR_SAME(&lsa->data->id, &ospf->router_id)) {
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
zlog_debug(
"LSA[Type%d:%s]: Refresh router-LSA for Area %s",
lsa->data->type,
inet_ntoa(lsa->data->id), area_str);
ospf_refresher_unregister_lsa(ospf, lsa);
ospf_lsa_flush_area(lsa, area);
ospf_lsa_unlock(&area->router_lsa_self);
area->router_lsa_self = NULL;
/* Refresh router-LSA, (not install) and flood through
* area. */
ospf_router_lsa_update_area(area);
} else {
rl = (struct router_lsa *)lsa->data;
/* Refresh router-LSA, (not install) and flood through
* area. */
if (rl->flags != ospf->flags)
ospf_router_lsa_update_area(area);
}
}
return 0;
}
/* network-LSA related functions. */
/* Originate Network-LSA. */
static void ospf_network_lsa_body_set(struct stream *s,
struct ospf_interface *oi)
{
struct in_addr mask;
struct route_node *rn;
struct ospf_neighbor *nbr;
masklen2ip(oi->address->prefixlen, &mask);
stream_put_ipv4(s, mask.s_addr);
/* The network-LSA lists those routers that are fully adjacent to
the Designated Router; each fully adjacent router is identified by
its OSPF Router ID. The Designated Router includes itself in this
list. RFC2328, Section 12.4.2 */
for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
if ((nbr = rn->info) != NULL)
if (nbr->state == NSM_Full || nbr == oi->nbr_self)
stream_put_ipv4(s, nbr->router_id.s_addr);
}
static struct ospf_lsa *ospf_network_lsa_new(struct ospf_interface *oi)
{
struct stream *s;
struct ospf_lsa *new;
struct lsa_header *lsah;
struct ospf_if_params *oip;
int length;
/* If there are no neighbours on this network (the net is stub),
the router does not originate network-LSA (see RFC 12.4.2) */
if (oi->full_nbrs == 0)
return NULL;
if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
zlog_debug("LSA[Type2]: Create network-LSA instance");
/* Create new stream for LSA. */
s = stream_new(OSPF_MAX_LSA_SIZE);
lsah = (struct lsa_header *)STREAM_DATA(s);
lsa_header_set(s, (OPTIONS(oi) | LSA_OPTIONS_GET(oi->area)),
OSPF_NETWORK_LSA, DR(oi), oi->ospf->router_id);