forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathospf6_interface.c
3316 lines (2796 loc) · 88.3 KB
/
ospf6_interface.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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2003 Yasuhiro Ohara
*/
#include <zebra.h>
#include "memory.h"
#include "if.h"
#include "log.h"
#include "command.h"
#include "frrevent.h"
#include "prefix.h"
#include "plist.h"
#include "zclient.h"
#include "ospf6_lsa.h"
#include "ospf6_lsdb.h"
#include "ospf6_top.h"
#include "ospf6_network.h"
#include "ospf6_message.h"
#include "ospf6_route.h"
#include "ospf6_area.h"
#include "ospf6_abr.h"
#include "ospf6_nssa.h"
#include "ospf6_interface.h"
#include "ospf6_neighbor.h"
#include "ospf6_intra.h"
#include "ospf6_spf.h"
#include "ospf6d.h"
#include "ospf6_bfd.h"
#include "ospf6_zebra.h"
#include "ospf6_gr.h"
#include "lib/json.h"
#include "ospf6_proto.h"
#include "lib/keychain.h"
#include "ospf6_auth_trailer.h"
#include "ospf6d/ospf6_interface_clippy.c"
DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_IF, "OSPF6 interface");
DEFINE_MTYPE(OSPF6D, OSPF6_AUTH_KEYCHAIN, "OSPF6 auth keychain");
DEFINE_MTYPE(OSPF6D, OSPF6_AUTH_MANUAL_KEY, "OSPF6 auth key");
DEFINE_MTYPE_STATIC(OSPF6D, CFG_PLIST_NAME, "configured prefix list names");
DEFINE_QOBJ_TYPE(ospf6_interface);
DEFINE_HOOK(ospf6_interface_change,
(struct ospf6_interface * oi, int state, int old_state),
(oi, state, old_state));
unsigned char conf_debug_ospf6_interface = 0;
const char *const ospf6_interface_state_str[] = {
"None", "Down", "Loopback", "Waiting", "PointToPoint",
"PtMultipoint", "DROther", "BDR", "DR", NULL
};
int ospf6_interface_neighbor_count(struct ospf6_interface *oi)
{
int count = 0;
struct ospf6_neighbor *nbr = NULL;
struct listnode *node;
for (ALL_LIST_ELEMENTS_RO(oi->neighbor_list, node, nbr)) {
/* Down state is not shown. */
if (nbr->state == OSPF6_NEIGHBOR_DOWN)
continue;
count++;
}
return count;
}
struct ospf6_interface *ospf6_interface_lookup_by_ifindex(ifindex_t ifindex,
vrf_id_t vrf_id)
{
struct ospf6_interface *oi;
struct interface *ifp;
ifp = if_lookup_by_index(ifindex, vrf_id);
if (ifp == NULL)
return (struct ospf6_interface *)NULL;
oi = (struct ospf6_interface *)ifp->info;
return oi;
}
/* schedule routing table recalculation */
static void ospf6_interface_lsdb_hook(struct ospf6_lsa *lsa, unsigned int reason)
{
struct ospf6_interface *oi;
if (lsa == NULL)
return;
oi = lsa->lsdb->data;
switch (ntohs(lsa->header->type)) {
case OSPF6_LSTYPE_LINK:
if (oi->state == OSPF6_INTERFACE_DR)
OSPF6_INTRA_PREFIX_LSA_SCHEDULE_TRANSIT(oi);
if (oi->area)
ospf6_spf_schedule(oi->area->ospf6, reason);
break;
default:
break;
}
}
static void ospf6_interface_lsdb_hook_add(struct ospf6_lsa *lsa)
{
ospf6_interface_lsdb_hook(lsa, ospf6_lsadd_to_spf_reason(lsa));
}
static void ospf6_interface_lsdb_hook_remove(struct ospf6_lsa *lsa)
{
ospf6_interface_lsdb_hook(lsa, ospf6_lsremove_to_spf_reason(lsa));
}
static uint8_t ospf6_default_iftype(struct interface *ifp)
{
if (if_is_pointopoint(ifp))
return OSPF_IFTYPE_POINTOPOINT;
else if (if_is_loopback(ifp))
return OSPF_IFTYPE_LOOPBACK;
else
return OSPF_IFTYPE_BROADCAST;
}
static uint32_t ospf6_interface_get_cost(struct ospf6_interface *oi)
{
/* If all else fails, use default OSPF cost */
uint32_t cost;
uint32_t bw, refbw;
struct ospf6 *ospf6;
/* interface speed and bw can be 0 in some platforms,
* use ospf default bw. If bw is configured then it would
* be used.
*/
if (!oi->interface->bandwidth && oi->interface->speed) {
bw = oi->interface->speed;
} else {
bw = oi->interface->bandwidth ? oi->interface->bandwidth
: OSPF6_INTERFACE_BANDWIDTH;
}
ospf6 = oi->interface->vrf->info;
refbw = ospf6 ? ospf6->ref_bandwidth : OSPF6_REFERENCE_BANDWIDTH;
/* A specified ip ospf cost overrides a calculated one. */
if (CHECK_FLAG(oi->flag, OSPF6_INTERFACE_NOAUTOCOST))
cost = oi->cost;
else {
cost = (uint32_t)((double)refbw / (double)bw + (double)0.5);
if (cost < 1)
cost = 1;
/* If the interface type is point-to-multipoint or the interface
* is in the state Loopback, the global scope IPv6 addresses
* associated with the interface (if any) are copied into the
* intra-area-prefix-LSA with the PrefixOptions LA-bit set, the
* PrefixLength set to 128, and the metric set to 0.
*/
if (if_is_loopback(oi->interface))
cost = 0;
}
return cost;
}
static void ospf6_interface_force_recalculate_cost(struct ospf6_interface *oi)
{
/* update cost held in route_connected list in ospf6_interface */
ospf6_interface_connected_route_update(oi->interface);
/* execute LSA hooks */
if (oi->area) {
OSPF6_LINK_LSA_SCHEDULE(oi);
OSPF6_ROUTER_LSA_SCHEDULE(oi->area);
OSPF6_NETWORK_LSA_SCHEDULE(oi);
OSPF6_INTRA_PREFIX_LSA_SCHEDULE_TRANSIT(oi);
OSPF6_INTRA_PREFIX_LSA_SCHEDULE_STUB(oi->area);
}
}
static void ospf6_interface_recalculate_cost(struct ospf6_interface *oi)
{
uint32_t newcost;
newcost = ospf6_interface_get_cost(oi);
if (newcost == oi->cost)
return;
oi->cost = newcost;
ospf6_interface_force_recalculate_cost(oi);
}
/* Create new ospf6 interface structure */
struct ospf6_interface *ospf6_interface_create(struct interface *ifp)
{
struct ospf6_interface *oi;
unsigned int iobuflen;
oi = XCALLOC(MTYPE_OSPF6_IF, sizeof(struct ospf6_interface));
oi->obuf = ospf6_fifo_new();
oi->area = (struct ospf6_area *)NULL;
oi->neighbor_list = list_new();
oi->neighbor_list->cmp = ospf6_neighbor_cmp;
oi->linklocal_addr = (struct in6_addr *)NULL;
oi->instance_id = OSPF6_INTERFACE_INSTANCE_ID;
oi->transdelay = OSPF6_INTERFACE_TRANSDELAY;
oi->priority = OSPF6_INTERFACE_PRIORITY;
oi->hello_interval = OSPF_HELLO_INTERVAL_DEFAULT;
oi->gr.hello_delay.interval = OSPF_HELLO_DELAY_DEFAULT;
oi->dead_interval = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT;
oi->rxmt_interval = OSPF_RETRANSMIT_INTERVAL_DEFAULT;
oi->type = ospf6_default_iftype(ifp);
oi->state = OSPF6_INTERFACE_DOWN;
oi->flag = 0;
oi->mtu_ignore = 0;
oi->c_ifmtu = 0;
/* Try to adjust I/O buffer size with IfMtu */
oi->ifmtu = ifp->mtu6;
iobuflen = ospf6_iobuf_size(ifp->mtu6);
if (oi->ifmtu > iobuflen) {
if (IS_OSPF6_DEBUG_INTERFACE)
zlog_debug("Interface %s: IfMtu is adjusted to I/O buffer size: %d.",
ifp->name, iobuflen);
oi->ifmtu = iobuflen;
}
QOBJ_REG(oi, ospf6_interface);
oi->lsupdate_list = ospf6_lsdb_create(oi);
oi->lsack_list = ospf6_lsdb_create(oi);
oi->lsdb = ospf6_lsdb_create(oi);
oi->lsdb->hook_add = ospf6_interface_lsdb_hook_add;
oi->lsdb->hook_remove = ospf6_interface_lsdb_hook_remove;
oi->lsdb_self = ospf6_lsdb_create(oi);
oi->route_connected = OSPF6_ROUTE_TABLE_CREATE(INTERFACE,
CONNECTED_ROUTES);
oi->route_connected->scope = oi;
/* link both */
oi->interface = ifp;
ifp->info = oi;
/* Compute cost. */
oi->cost = ospf6_interface_get_cost(oi);
oi->at_data.flags = 0;
return oi;
}
void ospf6_interface_delete(struct ospf6_interface *oi)
{
struct listnode *node, *nnode;
struct ospf6_neighbor *on;
QOBJ_UNREG(oi);
ospf6_fifo_free(oi->obuf);
for (ALL_LIST_ELEMENTS(oi->neighbor_list, node, nnode, on))
ospf6_neighbor_delete(on);
list_delete(&oi->neighbor_list);
EVENT_OFF(oi->thread_send_hello);
EVENT_OFF(oi->thread_send_lsupdate);
EVENT_OFF(oi->thread_send_lsack);
EVENT_OFF(oi->thread_sso);
EVENT_OFF(oi->thread_wait_timer);
ospf6_lsdb_remove_all(oi->lsdb);
ospf6_lsdb_remove_all(oi->lsupdate_list);
ospf6_lsdb_remove_all(oi->lsack_list);
ospf6_lsdb_delete(oi->lsdb);
ospf6_lsdb_delete(oi->lsdb_self);
ospf6_lsdb_delete(oi->lsupdate_list);
ospf6_lsdb_delete(oi->lsack_list);
ospf6_route_table_delete(oi->route_connected);
/* cut link */
oi->interface->info = NULL;
/* plist_name */
if (oi->plist_name)
XFREE(MTYPE_CFG_PLIST_NAME, oi->plist_name);
/* disable from area list if possible */
ospf6_area_interface_delete(oi);
if (oi->at_data.auth_key)
XFREE(MTYPE_OSPF6_AUTH_MANUAL_KEY, oi->at_data.auth_key);
/* Free BFD allocated data. */
XFREE(MTYPE_TMP, oi->bfd_config.profile);
XFREE(MTYPE_OSPF6_IF, oi);
}
void ospf6_interface_enable(struct ospf6_interface *oi)
{
UNSET_FLAG(oi->flag, OSPF6_INTERFACE_DISABLE);
ospf6_interface_state_update(oi->interface);
}
void ospf6_interface_disable(struct ospf6_interface *oi)
{
SET_FLAG(oi->flag, OSPF6_INTERFACE_DISABLE);
event_execute(master, interface_down, oi, 0, NULL);
ospf6_lsdb_remove_all(oi->lsdb);
ospf6_lsdb_remove_all(oi->lsdb_self);
ospf6_lsdb_remove_all(oi->lsupdate_list);
ospf6_lsdb_remove_all(oi->lsack_list);
EVENT_OFF(oi->thread_send_hello);
EVENT_OFF(oi->thread_send_lsupdate);
EVENT_OFF(oi->thread_send_lsack);
EVENT_OFF(oi->thread_sso);
EVENT_OFF(oi->thread_network_lsa);
EVENT_OFF(oi->thread_link_lsa);
EVENT_OFF(oi->thread_intra_prefix_lsa);
EVENT_OFF(oi->thread_as_extern_lsa);
EVENT_OFF(oi->thread_wait_timer);
oi->gr.hello_delay.elapsed_seconds = 0;
EVENT_OFF(oi->gr.hello_delay.t_grace_send);
}
static struct in6_addr *
ospf6_interface_get_linklocal_address(struct interface *ifp)
{
struct connected *c;
struct in6_addr *l = (struct in6_addr *)NULL;
/* for each connected address */
frr_each (if_connected, ifp->connected, c) {
/* if family not AF_INET6, ignore */
if (c->address->family != AF_INET6)
continue;
/* linklocal scope check */
if (IN6_IS_ADDR_LINKLOCAL(&c->address->u.prefix6))
l = &c->address->u.prefix6;
}
return l;
}
void ospf6_interface_state_update(struct interface *ifp)
{
struct ospf6_interface *oi;
unsigned int iobuflen;
oi = (struct ospf6_interface *)ifp->info;
if (oi == NULL)
return;
if (CHECK_FLAG(oi->flag, OSPF6_INTERFACE_DISABLE))
return;
/* Adjust the mtu values if the kernel told us something new */
if (ifp->mtu6 != oi->ifmtu) {
/* If nothing configured, accept it and check for buffer size */
if (!oi->c_ifmtu) {
oi->ifmtu = ifp->mtu6;
iobuflen = ospf6_iobuf_size(ifp->mtu6);
if (oi->ifmtu > iobuflen) {
if (IS_OSPF6_DEBUG_INTERFACE)
zlog_debug("Interface %s: IfMtu is adjusted to I/O buffer size: %d.",
ifp->name, iobuflen);
oi->ifmtu = iobuflen;
}
} else if (oi->c_ifmtu > ifp->mtu6) {
oi->ifmtu = ifp->mtu6;
zlog_warn("Configured mtu %u on %s overridden by kernel %u",
oi->c_ifmtu, ifp->name, ifp->mtu6);
} else
oi->ifmtu = oi->c_ifmtu;
}
if (if_is_operative(ifp) &&
(ospf6_interface_get_linklocal_address(oi->interface) ||
if_is_loopback(oi->interface)))
event_execute(master, interface_up, oi, 0, NULL);
else
event_execute(master, interface_down, oi, 0, NULL);
return;
}
void ospf6_interface_connected_route_update(struct interface *ifp)
{
struct ospf6_interface *oi;
struct connected *c;
struct in6_addr nh_addr;
oi = (struct ospf6_interface *)ifp->info;
if (oi == NULL)
return;
/* reset linklocal pointer */
oi->linklocal_addr = ospf6_interface_get_linklocal_address(ifp);
/* if area is null, do not make connected-route list */
if (oi->area == NULL)
return;
if (CHECK_FLAG(oi->flag, OSPF6_INTERFACE_DISABLE))
return;
/* update "route to advertise" interface route table */
ospf6_route_remove_all(oi->route_connected);
frr_each (if_connected, ifp->connected, c) {
if (c->address->family != AF_INET6)
continue;
CONTINUE_IF_ADDRESS_LINKLOCAL(IS_OSPF6_DEBUG_INTERFACE,
c->address);
CONTINUE_IF_ADDRESS_UNSPECIFIED(IS_OSPF6_DEBUG_INTERFACE,
c->address);
CONTINUE_IF_ADDRESS_LOOPBACK(IS_OSPF6_DEBUG_INTERFACE,
c->address);
CONTINUE_IF_ADDRESS_V4COMPAT(IS_OSPF6_DEBUG_INTERFACE,
c->address);
CONTINUE_IF_ADDRESS_V4MAPPED(IS_OSPF6_DEBUG_INTERFACE,
c->address);
/* apply filter */
if (oi->plist_name) {
struct prefix_list *plist;
enum prefix_list_type ret;
plist = prefix_list_lookup(AFI_IP6, oi->plist_name);
ret = prefix_list_apply(plist, (void *)c->address);
if (ret == PREFIX_DENY) {
if (IS_OSPF6_DEBUG_INTERFACE)
zlog_debug("%pFX on %s filtered by prefix-list %s ",
c->address,
oi->interface->name,
oi->plist_name);
continue;
}
}
if (oi->type == OSPF_IFTYPE_LOOPBACK ||
oi->type == OSPF_IFTYPE_POINTOMULTIPOINT ||
oi->type == OSPF_IFTYPE_POINTOPOINT) {
struct ospf6_route *la_route;
la_route = ospf6_route_create(oi->area->ospf6);
la_route->prefix = *c->address;
la_route->prefix.prefixlen = 128;
la_route->prefix_options |= OSPF6_PREFIX_OPTION_LA;
la_route->type = OSPF6_DEST_TYPE_NETWORK;
la_route->path.area_id = oi->area->area_id;
la_route->path.type = OSPF6_PATH_TYPE_INTRA;
la_route->path.cost = 0;
inet_pton(AF_INET6, "::1", &nh_addr);
ospf6_route_add_nexthop(la_route, oi->interface->ifindex,
&nh_addr);
ospf6_route_add(la_route, oi->route_connected);
}
if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT &&
!oi->p2xp_connected_pfx_include)
continue;
if (oi->type == OSPF_IFTYPE_POINTOPOINT &&
oi->p2xp_connected_pfx_exclude)
continue;
struct ospf6_route *route;
route = ospf6_route_create(oi->area->ospf6);
memcpy(&route->prefix, c->address, sizeof(struct prefix));
apply_mask(&route->prefix);
route->type = OSPF6_DEST_TYPE_NETWORK;
route->path.area_id = oi->area->area_id;
route->path.type = OSPF6_PATH_TYPE_INTRA;
route->path.cost = oi->cost;
inet_pton(AF_INET6, "::1", &nh_addr);
ospf6_route_add_nexthop(route, oi->interface->ifindex, &nh_addr);
ospf6_route_add(route, oi->route_connected);
}
/* create new Link-LSA */
OSPF6_LINK_LSA_SCHEDULE(oi);
OSPF6_INTRA_PREFIX_LSA_SCHEDULE_TRANSIT(oi);
OSPF6_INTRA_PREFIX_LSA_SCHEDULE_STUB(oi->area);
}
static int ospf6_interface_state_change(uint8_t next_state,
struct ospf6_interface *oi)
{
uint8_t prev_state;
struct ospf6 *ospf6;
prev_state = oi->state;
oi->state = next_state;
if (prev_state == next_state)
return -1;
if (!oi->area)
return -1;
/* log */
if (IS_OSPF6_DEBUG_INTERFACE) {
zlog_debug("Interface state change %s: %s -> %s",
oi->interface->name,
ospf6_interface_state_str[prev_state],
ospf6_interface_state_str[next_state]);
}
oi->state_change++;
ospf6 = oi->area->ospf6;
if ((prev_state == OSPF6_INTERFACE_DR ||
prev_state == OSPF6_INTERFACE_BDR) &&
(next_state != OSPF6_INTERFACE_DR &&
next_state != OSPF6_INTERFACE_BDR))
ospf6_sso(oi->interface->ifindex, &alldrouters6,
IPV6_LEAVE_GROUP, ospf6->fd);
if ((prev_state != OSPF6_INTERFACE_DR &&
prev_state != OSPF6_INTERFACE_BDR) &&
(next_state == OSPF6_INTERFACE_DR ||
next_state == OSPF6_INTERFACE_BDR))
ospf6_sso(oi->interface->ifindex, &alldrouters6,
IPV6_JOIN_GROUP, ospf6->fd);
OSPF6_ROUTER_LSA_SCHEDULE(oi->area);
OSPF6_LINK_LSA_SCHEDULE(oi);
if (next_state == OSPF6_INTERFACE_DOWN) {
OSPF6_NETWORK_LSA_EXECUTE(oi);
OSPF6_INTRA_PREFIX_LSA_EXECUTE_TRANSIT(oi);
OSPF6_INTRA_PREFIX_LSA_SCHEDULE_STUB(oi->area);
} else if (prev_state == OSPF6_INTERFACE_DR ||
next_state == OSPF6_INTERFACE_DR) {
OSPF6_NETWORK_LSA_SCHEDULE(oi);
OSPF6_INTRA_PREFIX_LSA_SCHEDULE_TRANSIT(oi);
OSPF6_INTRA_PREFIX_LSA_SCHEDULE_STUB(oi->area);
}
if (next_state == OSPF6_INTERFACE_POINTTOPOINT ||
next_state == OSPF6_INTERFACE_POINTTOMULTIPOINT)
ospf6_if_p2xp_up(oi);
hook_call(ospf6_interface_change, oi, next_state, prev_state);
return 0;
}
/* DR Election, RFC2328 section 9.4 */
#define IS_ELIGIBLE(n) \
((n)->state >= OSPF6_NEIGHBOR_TWOWAY && (n)->priority != 0)
static struct ospf6_neighbor *better_bdrouter(struct ospf6_neighbor *a,
struct ospf6_neighbor *b)
{
if ((a == NULL || !IS_ELIGIBLE(a) || a->drouter == a->router_id) &&
(b == NULL || !IS_ELIGIBLE(b) || b->drouter == b->router_id))
return NULL;
else if (a == NULL || !IS_ELIGIBLE(a) || a->drouter == a->router_id)
return b;
else if (b == NULL || !IS_ELIGIBLE(b) || b->drouter == b->router_id)
return a;
if (a->bdrouter == a->router_id && b->bdrouter != b->router_id)
return a;
if (a->bdrouter != a->router_id && b->bdrouter == b->router_id)
return b;
if (a->priority > b->priority)
return a;
if (a->priority < b->priority)
return b;
if (ntohl(a->router_id) > ntohl(b->router_id))
return a;
if (ntohl(a->router_id) < ntohl(b->router_id))
return b;
zlog_warn("Router-ID duplicate ?");
return a;
}
static struct ospf6_neighbor *better_drouter(struct ospf6_neighbor *a,
struct ospf6_neighbor *b)
{
if ((a == NULL || !IS_ELIGIBLE(a) || a->drouter != a->router_id) &&
(b == NULL || !IS_ELIGIBLE(b) || b->drouter != b->router_id))
return NULL;
else if (a == NULL || !IS_ELIGIBLE(a) || a->drouter != a->router_id)
return b;
else if (b == NULL || !IS_ELIGIBLE(b) || b->drouter != b->router_id)
return a;
if (a->drouter == a->router_id && b->drouter != b->router_id)
return a;
if (a->drouter != a->router_id && b->drouter == b->router_id)
return b;
if (a->priority > b->priority)
return a;
if (a->priority < b->priority)
return b;
if (ntohl(a->router_id) > ntohl(b->router_id))
return a;
if (ntohl(a->router_id) < ntohl(b->router_id))
return b;
zlog_warn("Router-ID duplicate ?");
return a;
}
uint8_t dr_election(struct ospf6_interface *oi)
{
struct ospf6 *ospf6 = oi->area->ospf6;
struct listnode *node, *nnode;
struct ospf6_neighbor *on, *drouter, *bdrouter, myself;
struct ospf6_neighbor *best_drouter, *best_bdrouter;
uint8_t next_state = 0;
drouter = bdrouter = NULL;
best_drouter = best_bdrouter = NULL;
/* pseudo neighbor myself, including noting current DR/BDR (1) */
memset(&myself, 0, sizeof(myself));
inet_ntop(AF_INET, &ospf6->router_id, myself.name, sizeof(myself.name));
myself.state = OSPF6_NEIGHBOR_TWOWAY;
myself.drouter = oi->drouter;
myself.bdrouter = oi->bdrouter;
myself.priority = oi->priority;
myself.router_id = ospf6->router_id;
/* Electing BDR (2) */
for (ALL_LIST_ELEMENTS(oi->neighbor_list, node, nnode, on))
bdrouter = better_bdrouter(bdrouter, on);
best_bdrouter = bdrouter;
bdrouter = better_bdrouter(best_bdrouter, &myself);
/* Electing DR (3) */
for (ALL_LIST_ELEMENTS(oi->neighbor_list, node, nnode, on))
drouter = better_drouter(drouter, on);
best_drouter = drouter;
drouter = better_drouter(best_drouter, &myself);
if (drouter == NULL)
drouter = bdrouter;
/* the router itself is newly/no longer DR/BDR (4) */
if ((drouter == &myself && myself.drouter != myself.router_id) ||
(drouter != &myself && myself.drouter == myself.router_id) ||
(bdrouter == &myself && myself.bdrouter != myself.router_id) ||
(bdrouter != &myself && myself.bdrouter == myself.router_id)) {
myself.drouter = (drouter ? drouter->router_id : htonl(0));
myself.bdrouter = (bdrouter ? bdrouter->router_id : htonl(0));
/* compatible to Electing BDR (2) */
bdrouter = better_bdrouter(best_bdrouter, &myself);
/* compatible to Electing DR (3) */
drouter = better_drouter(best_drouter, &myself);
if (drouter == NULL)
drouter = bdrouter;
}
/* Set interface state accordingly (5) */
if (drouter && drouter == &myself)
next_state = OSPF6_INTERFACE_DR;
else if (bdrouter && bdrouter == &myself)
next_state = OSPF6_INTERFACE_BDR;
else
next_state = OSPF6_INTERFACE_DROTHER;
/* If NBMA, schedule Start for each neighbor having priority of 0 (6) */
/* XXX */
/* If DR or BDR change, invoke AdjOK? for each neighbor (7) */
/* RFC 2328 section 12.4. Originating LSAs (3) will be handled
accordingly after AdjOK */
if (oi->drouter != (drouter ? drouter->router_id : htonl(0)) ||
oi->bdrouter != (bdrouter ? bdrouter->router_id : htonl(0)) ||
ospf6->gr_info.restart_in_progress) {
if (IS_OSPF6_DEBUG_INTERFACE)
zlog_debug("DR Election on %s: DR: %s BDR: %s",
oi->interface->name,
(drouter ? drouter->name : "0.0.0.0"),
(bdrouter ? bdrouter->name : "0.0.0.0"));
for (ALL_LIST_ELEMENTS_RO(oi->neighbor_list, node, on)) {
if (on->state < OSPF6_NEIGHBOR_TWOWAY)
continue;
/* Schedule AdjOK. */
event_add_event(master, adj_ok, on, 0,
&on->thread_adj_ok);
}
}
oi->drouter = (drouter ? drouter->router_id : htonl(0));
oi->bdrouter = (bdrouter ? bdrouter->router_id : htonl(0));
return next_state;
}
#ifdef __FreeBSD__
#include <ifaddrs.h>
static bool ifmaddr_check(ifindex_t ifindex, struct in6_addr *addr)
{
struct ifmaddrs *ifmap, *ifma;
struct sockaddr_dl *sdl;
struct sockaddr_in6 *sin6;
bool found = false;
if (getifmaddrs(&ifmap) != 0)
return false;
for (ifma = ifmap; ifma; ifma = ifma->ifma_next) {
if (ifma->ifma_name == NULL || ifma->ifma_addr == NULL)
continue;
if (ifma->ifma_name->sa_family != AF_LINK)
continue;
if (ifma->ifma_addr->sa_family != AF_INET6)
continue;
sdl = (struct sockaddr_dl *)ifma->ifma_name;
sin6 = (struct sockaddr_in6 *)ifma->ifma_addr;
if (sdl->sdl_index == ifindex &&
memcmp(&sin6->sin6_addr, addr, IPV6_MAX_BYTELEN) == 0) {
found = true;
break;
}
}
if (ifmap)
freeifmaddrs(ifmap);
return found;
}
#endif /* __FreeBSD__ */
/* Interface State Machine */
void interface_up(struct event *thread)
{
struct ospf6_interface *oi;
struct ospf6 *ospf6;
oi = (struct ospf6_interface *)EVENT_ARG(thread);
assert(oi && oi->interface);
if (!oi->type_cfg)
oi->type = ospf6_default_iftype(oi->interface);
event_cancel(&oi->thread_sso);
if (IS_OSPF6_DEBUG_INTERFACE)
zlog_debug("Interface Event %s: [InterfaceUp]",
oi->interface->name);
/* check physical interface is up */
if (!if_is_operative(oi->interface)) {
zlog_warn("Interface %s is down, can't execute [InterfaceUp]",
oi->interface->name);
return;
}
/* check interface has a link-local address */
if (!(ospf6_interface_get_linklocal_address(oi->interface) ||
if_is_loopback(oi->interface))) {
zlog_warn("Interface %s has no link local address, can't execute [InterfaceUp]",
oi->interface->name);
return;
}
/* Recompute cost & update connected LSAs */
ospf6_interface_force_recalculate_cost(oi);
/* if already enabled, do nothing */
if (oi->state > OSPF6_INTERFACE_DOWN) {
if (IS_OSPF6_DEBUG_INTERFACE)
zlog_debug("Interface %s already enabled",
oi->interface->name);
return;
}
/* If no area assigned, return */
if (oi->area == NULL) {
zlog_warn("%s: Not scheduling Hello for %s as there is no area assigned yet",
__func__, oi->interface->name);
return;
}
/*
* RFC 3623 - Section 5 ("Unplanned Outages"):
* "The grace-LSAs are encapsulated in Link State Update Packets
* and sent out to all interfaces, even though the restarted
* router has no adjacencies and no knowledge of previous
* adjacencies".
*/
if (oi->area->ospf6->gr_info.restart_in_progress &&
oi->area->ospf6->gr_info.reason == OSPF6_GR_UNKNOWN_RESTART)
ospf6_gr_unplanned_start_interface(oi);
#ifdef __FreeBSD__
/*
* There's a delay in FreeBSD between issuing a command to leave a
* multicast group and an actual leave. If we execute "no router ospf6"
* and "router ospf6" fast enough, we can end up in a situation when OS
* performs the leave later than it performs the join and the interface
* remains without a multicast group. We have to do the join only after
* the interface actually left the group.
*/
if (ifmaddr_check(oi->interface->ifindex, &allspfrouters6)) {
zlog_info("Interface %s is still in all routers group, rescheduling for SSO",
oi->interface->name);
event_add_timer(master, interface_up, oi,
OSPF6_INTERFACE_SSO_RETRY_INT, &oi->thread_sso);
return;
}
#endif /* __FreeBSD__ */
ospf6 = oi->area->ospf6;
/* Join AllSPFRouters */
if (ospf6_sso(oi->interface->ifindex, &allspfrouters6, IPV6_JOIN_GROUP,
ospf6->fd) < 0) {
if (oi->sso_try_cnt++ < OSPF6_INTERFACE_SSO_RETRY_MAX) {
zlog_info("Scheduling %s for sso retry, trial count: %d",
oi->interface->name, oi->sso_try_cnt);
event_add_timer(master, interface_up, oi,
OSPF6_INTERFACE_SSO_RETRY_INT,
&oi->thread_sso);
}
return;
}
oi->sso_try_cnt = 0; /* Reset on success */
/* Update interface route */
ospf6_interface_connected_route_update(oi->interface);
/* Schedule Hello */
if (!CHECK_FLAG(oi->flag, OSPF6_INTERFACE_PASSIVE) &&
!if_is_loopback(oi->interface)) {
event_add_timer(master, ospf6_hello_send, oi, 0,
&oi->thread_send_hello);
}
/* decide next interface state */
if (oi->type == OSPF_IFTYPE_LOOPBACK) {
ospf6_interface_state_change(OSPF6_INTERFACE_LOOPBACK, oi);
} else if (oi->type == OSPF_IFTYPE_POINTOPOINT) {
ospf6_interface_state_change(OSPF6_INTERFACE_POINTTOPOINT, oi);
} else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT) {
ospf6_interface_state_change(OSPF6_INTERFACE_POINTTOMULTIPOINT,
oi);
} else if (oi->priority == 0)
ospf6_interface_state_change(OSPF6_INTERFACE_DROTHER, oi);
else {
ospf6_interface_state_change(OSPF6_INTERFACE_WAITING, oi);
event_add_timer(master, wait_timer, oi, oi->dead_interval,
&oi->thread_wait_timer);
}
}
void wait_timer(struct event *thread)
{
struct ospf6_interface *oi;
oi = (struct ospf6_interface *)EVENT_ARG(thread);
assert(oi && oi->interface);
if (IS_OSPF6_DEBUG_INTERFACE)
zlog_debug("Interface Event %s: [WaitTimer]",
oi->interface->name);
if (oi->state == OSPF6_INTERFACE_WAITING)
ospf6_interface_state_change(dr_election(oi), oi);
}
void backup_seen(struct event *thread)
{
struct ospf6_interface *oi;
oi = (struct ospf6_interface *)EVENT_ARG(thread);
assert(oi && oi->interface);
if (IS_OSPF6_DEBUG_INTERFACE)
zlog_debug("Interface Event %s: [BackupSeen]",
oi->interface->name);
if (oi->state == OSPF6_INTERFACE_WAITING)
ospf6_interface_state_change(dr_election(oi), oi);
}
void neighbor_change(struct event *thread)
{
struct ospf6_interface *oi;
oi = (struct ospf6_interface *)EVENT_ARG(thread);
assert(oi && oi->interface);
if (IS_OSPF6_DEBUG_INTERFACE)
zlog_debug("Interface Event %s: [NeighborChange]",
oi->interface->name);
if (oi->state == OSPF6_INTERFACE_DROTHER ||
oi->state == OSPF6_INTERFACE_BDR || oi->state == OSPF6_INTERFACE_DR)
ospf6_interface_state_change(dr_election(oi), oi);
}
void interface_down(struct event *thread)
{
struct ospf6_interface *oi;
struct listnode *node, *nnode;
struct ospf6_neighbor *on;
struct ospf6 *ospf6;
oi = (struct ospf6_interface *)EVENT_ARG(thread);
assert(oi && oi->interface);
if (IS_OSPF6_DEBUG_INTERFACE)
zlog_debug("Interface Event %s: [InterfaceDown]",
oi->interface->name);
/* Stop Hellos */
EVENT_OFF(oi->thread_send_hello);
/* Stop trying to set socket options. */
EVENT_OFF(oi->thread_sso);
/* Cease the HELPER role for all the neighbours
* of this interface.
*/
if (ospf6_interface_neighbor_count(oi)) {
struct listnode *ln;
struct ospf6_neighbor *nbr = NULL;
for (ALL_LIST_ELEMENTS_RO(oi->neighbor_list, ln, nbr))
ospf6_gr_helper_exit(nbr, OSPF6_GR_HELPER_TOPO_CHG);
}
for (ALL_LIST_ELEMENTS(oi->neighbor_list, node, nnode, on))
ospf6_neighbor_delete(on);
list_delete_all_node(oi->neighbor_list);
/* When interface state is reset, also reset information about
* DR election, as it is no longer valid. */
oi->drouter = oi->prev_drouter = htonl(0);
oi->bdrouter = oi->prev_bdrouter = htonl(0);
if (oi->area == NULL)
return;
ospf6 = oi->area->ospf6;
/* Leave AllSPFRouters */
if (oi->state > OSPF6_INTERFACE_DOWN)
ospf6_sso(oi->interface->ifindex, &allspfrouters6,
IPV6_LEAVE_GROUP, ospf6->fd);
/* deal with write fifo */
ospf6_fifo_flush(oi->obuf);
if (oi->on_write_q) {
listnode_delete(ospf6->oi_write_q, oi);
if (list_isempty(ospf6->oi_write_q))
event_cancel(&ospf6->t_write);
oi->on_write_q = 0;
}
ospf6_interface_state_change(OSPF6_INTERFACE_DOWN, oi);
}
static const char *ospf6_iftype_str(uint8_t iftype)
{
switch (iftype) {
case OSPF_IFTYPE_LOOPBACK:
return "LOOPBACK";
case OSPF_IFTYPE_BROADCAST:
return "BROADCAST";