-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathvrrp.c
2425 lines (2072 loc) · 64.6 KB
/
vrrp.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
/*
* VRRP global definitions and state machine.
* Copyright (C) 2018-2019 Cumulus Networks, Inc.
* Quentin Young
*/
#include <zebra.h>
#include "lib/hash.h"
#include "lib/hook.h"
#include "lib/if.h"
#include "lib/linklist.h"
#include "lib/memory.h"
#include "lib/network.h"
#include "lib/prefix.h"
#include "lib/sockopt.h"
#include "lib/sockunion.h"
#include "lib/vrf.h"
#include "lib/vty.h"
#include "vrrp.h"
#include "vrrp_arp.h"
#include "vrrp_debug.h"
#include "vrrp_ndisc.h"
#include "vrrp_packet.h"
#include "vrrp_zebra.h"
#define VRRP_LOGPFX "[CORE] "
DEFINE_MTYPE_STATIC(VRRPD, VRRP_IP, "VRRP IP address");
DEFINE_MTYPE_STATIC(VRRPD, VRRP_RTR, "VRRP Router");
/* statics */
struct hash *vrrp_vrouters_hash;
bool vrrp_autoconfig_is_on;
int vrrp_autoconfig_version;
struct vrrp_defaults vd;
const char *const vrrp_state_names[3] = {
[VRRP_STATE_INITIALIZE] = "Initialize",
[VRRP_STATE_MASTER] = "Master",
[VRRP_STATE_BACKUP] = "Backup",
};
static const char *const vrrp_event_names[2] = {
[VRRP_EVENT_STARTUP] = "Startup",
[VRRP_EVENT_SHUTDOWN] = "Shutdown",
};
/* Utility functions ------------------------------------------------------- */
/*
* Sets an ethaddr to RFC-defined Virtual Router MAC address.
*
* mac
* ethaddr to set
*
* v6
* Whether this is a V6 or V4 Virtual Router MAC
*
* vrid
* Virtual Router Identifier
*/
static void vrrp_mac_set(struct ethaddr *mac, bool v6, uint8_t vrid)
{
/*
* V4: 00-00-5E-00-01-{VRID}
* V6: 00-00-5E-00-02-{VRID}
*/
mac->octet[0] = 0x00;
mac->octet[1] = 0x00;
mac->octet[2] = 0x5E;
mac->octet[3] = 0x00;
mac->octet[4] = v6 ? 0x02 : 0x01;
mac->octet[5] = vrid;
}
/*
* Recalculates and sets skew_time and master_down_interval based
* values.
*
* r
* VRRP Router to operate on
*/
static void vrrp_recalculate_timers(struct vrrp_router *r)
{
uint16_t mdiadv = r->vr->version == 3 ? r->master_adver_interval
: r->vr->advertisement_interval;
uint16_t skm = (r->vr->version == 3) ? r->master_adver_interval : 100;
r->skew_time = ((256 - r->vr->priority) * skm) / 256;
r->master_down_interval = 3 * mdiadv;
r->master_down_interval += r->skew_time;
}
/*
* Determines if a VRRP router is the owner of the specified address.
*
* The determining factor for whether an interface is the address owner is
* simply whether the address is assigned to the VRRP base interface by someone
* other than vrrpd.
*
* This function should always return the correct answer regardless of
* master/backup status.
*
* ifp
* The interface to check owernship of. This should be the base interface of
* a VRRP router.
*
* vr
* Virtual Router
*
* Returns:
* whether or not vr owns the specified address
*/
static bool vrrp_is_owner(struct interface *ifp, struct ipaddr *addr)
{
/*
* This code sanity checks implicit ownership configuration. Ideally,
* the way we determine address ownership status for this VRRP router
* is by looking at whether our VIPs are also assigned to the base
* interface, and therefore count as "real" addresses. This frees the
* user from having to manually configure priority 255 to indicate
* address ownership. However, this means one of the VIPs will be used
* as the source address for VRRP advertisements, which in turn means
* that other VRRP routers will be receiving packets with a source
* address they themselves have. This causes lots of different issues
* so for now we're disabling this and forcing the user to configure
* priority 255 to indicate ownership.
*/
return false;
#if 0
struct prefix p;
p.family = IS_IPADDR_V4(addr) ? AF_INET : AF_INET6;
p.prefixlen = IS_IPADDR_V4(addr) ? IPV4_MAX_BITLEN : IPV6_MAX_BITLEN;
memcpy(&p.u, &addr->ip, sizeof(addr->ip));
return !!connected_lookup_prefix_exact(ifp, &p);
#endif
}
/*
* Whether an interface has a MAC address that matches the VRRP RFC.
*
* ifp
* Interface to check
*
* Returns:
* Whether the interface has a VRRP mac or not
*/
static bool vrrp_ifp_has_vrrp_mac(struct interface *ifp)
{
struct ethaddr vmac4;
struct ethaddr vmac6;
vrrp_mac_set(&vmac4, 0, 0x00);
vrrp_mac_set(&vmac6, 1, 0x00);
return !memcmp(ifp->hw_addr, vmac4.octet, sizeof(vmac4.octet) - 1)
|| !memcmp(ifp->hw_addr, vmac6.octet, sizeof(vmac6.octet) - 1);
}
/*
* Lookup a Virtual Router instance given a macvlan subinterface.
*
* The VRID is extracted from the interface MAC and the 2-tuple (iface, vrid)
* is used to look up any existing instances that match the interface. It does
* not matter whether the instance is already bound to the interface or not.
*
* Note that the interface linkages must be correct for this to work. In other
* words, the macvlan must have a valid VRRP MAC, and its link_ifindex must be
* be equal to the ifindex of another interface in the interface RB trees (its
* parent). If these conditions aren't satisfied we won't find the VR.
*
* mvl_ifp
* Interface pointer to use to lookup. Should be a macvlan device.
*
* Returns:
* Virtual Router, if found
* NULL otherwise
*/
static struct vrrp_vrouter *vrrp_lookup_by_if_mvl(struct interface *mvl_ifp)
{
struct interface *p;
if (!mvl_ifp || mvl_ifp->link_ifindex == 0
|| !vrrp_ifp_has_vrrp_mac(mvl_ifp)) {
if (mvl_ifp && mvl_ifp->link_ifindex == 0)
DEBUGD(&vrrp_dbg_zebra,
VRRP_LOGPFX
"Interface %s has no parent ifindex; disregarding",
mvl_ifp->name);
if (mvl_ifp && !vrrp_ifp_has_vrrp_mac(mvl_ifp))
DEBUGD(&vrrp_dbg_zebra,
VRRP_LOGPFX
"Interface %s has a non-VRRP MAC; disregarding",
mvl_ifp->name);
return NULL;
}
p = if_lookup_by_index(mvl_ifp->link_ifindex, mvl_ifp->vrf->vrf_id);
if (!p) {
DEBUGD(&vrrp_dbg_zebra,
VRRP_LOGPFX
"Tried to lookup interface %d, parent of %s, but it doesn't exist",
mvl_ifp->link_ifindex, mvl_ifp->name);
return NULL;
}
uint8_t vrid = mvl_ifp->hw_addr[5];
return vrrp_lookup(p, vrid);
}
/*
* Lookup the Virtual Router instances configured on a particular interface.
*
* ifp
* Interface pointer to use to lookup. Should not be a macvlan device.
*
* Returns:
* List of virtual routers found
*/
static struct list *vrrp_lookup_by_if(struct interface *ifp)
{
struct list *l = hash_to_list(vrrp_vrouters_hash);
struct listnode *ln, *nn;
struct vrrp_vrouter *vr;
for (ALL_LIST_ELEMENTS(l, ln, nn, vr))
if (vr->ifp != ifp)
list_delete_node(l, ln);
return l;
}
/*
* Lookup any Virtual Router instances associated with a particular interface.
* This is a combination of the results from vrrp_lookup_by_if_mvl and
* vrrp_lookup_by_if.
*
* Suppose the system interface list looks like the following:
*
* eth0
* \- eth0-v0 00:00:5e:00:01:01
* \- eth0-v1 00:00:5e:00:02:01
* \- eth0-v2 00:00:5e:00:01:0a
*
* Passing eth0-v2 to this function will give you the VRRP instance configured
* on eth0 with VRID 10. Passing eth0-v0 or eth0-v1 will give you the VRRP
* instance configured on eth0 with VRID 1. Passing eth0 will give you both.
*
* ifp
* Interface pointer to use to lookup. Can be any interface.
*
* Returns:
* List of virtual routers found
*/
static struct list *vrrp_lookup_by_if_any(struct interface *ifp)
{
struct vrrp_vrouter *vr;
struct list *vrs;
vr = vrrp_lookup_by_if_mvl(ifp);
vrs = vr ? list_new() : vrrp_lookup_by_if(ifp);
if (vr)
listnode_add(vrs, vr);
return vrs;
}
/* Configuration controllers ----------------------------------------------- */
void vrrp_check_start(struct vrrp_vrouter *vr)
{
struct vrrp_router *r;
bool start;
const char *whynot = NULL;
if (vr->shutdown || vr->ifp == NULL)
return;
r = vr->v4;
/* Must not already be started */
start = r->fsm.state == VRRP_STATE_INITIALIZE;
whynot = (!start && !whynot) ? "Already running" : whynot;
/* Must have a parent interface */
start = start && (vr->ifp != NULL);
whynot = (!start && !whynot) ? "No base interface" : whynot;
#if 0
/* Parent interface must be up */
start = start && if_is_operative(vr->ifp);
start = (!start && !whynot) ? "Base interface inoperative" : whynot;
#endif
/* Parent interface must have at least one v4 */
start = start && connected_count_by_family(vr->ifp, AF_INET) > 0;
whynot = (!start && !whynot) ? "No primary IPv4 address" : whynot;
/* Must have a macvlan interface */
start = start && (r->mvl_ifp != NULL);
whynot = (!start && !whynot) ? "No VRRP interface" : whynot;
#if 0
/* Macvlan interface must be admin up */
start = start && CHECK_FLAG(r->mvl_ifp->flags, IFF_UP);
start = (!start && !whynot) ? "Macvlan device admin down" : whynot;
#endif
/* Must have at least one VIP configured */
start = start && r->addrs->count > 0;
whynot = (!start && !whynot) ? "No Virtual IP address configured"
: whynot;
if (start)
vrrp_event(r, VRRP_EVENT_STARTUP);
else if (whynot)
zlog_warn(VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Refusing to start Virtual Router: %s",
vr->vrid, family2str(r->family), whynot);
whynot = NULL;
r = vr->v6;
/* Must not already be started */
start = r->fsm.state == VRRP_STATE_INITIALIZE;
whynot = (!start && !whynot) ? "Already running" : whynot;
/* Must not be v2 */
start = start && vr->version != 2;
whynot = (!start && !whynot) ? "VRRPv2 does not support v6" : whynot;
/* Must have a parent interface */
start = start && (vr->ifp != NULL);
whynot = (!start && !whynot) ? "No base interface" : whynot;
#if 0
/* Parent interface must be up */
start = start && if_is_operative(vr->ifp);
start = (!start && !whynot) ? "Base interface inoperative" : whynot;
#endif
/* Must have a macvlan interface */
start = start && (r->mvl_ifp != NULL);
whynot = (!start && !whynot) ? "No VRRP interface" : whynot;
#if 0
/* Macvlan interface must be admin up */
start = start && CHECK_FLAG(r->mvl_ifp->flags, IFF_UP);
start = (!start && !whynot) ? "Macvlan device admin down" : whynot;
/* Macvlan interface must have a link local */
start = start && connected_get_linklocal(r->mvl_ifp);
whynot =
(!start && !whynot) ? "No link local address configured" : whynot;
/* Macvlan interface must have a v6 IP besides the link local */
start = start && (connected_count_by_family(r->mvl_ifp, AF_INET6) > 1);
whynot = (!start && !whynot)
? "No Virtual IPv6 address configured on macvlan device"
: whynot;
#endif
/* Must have at least one VIP configured */
start = start && r->addrs->count > 0;
whynot =
(!start && !whynot) ? "No Virtual IP address configured" : whynot;
if (start)
vrrp_event(r, VRRP_EVENT_STARTUP);
else if (whynot)
zlog_warn(VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Refusing to start Virtual Router: %s",
vr->vrid, family2str(r->family), whynot);
}
void vrrp_set_priority(struct vrrp_vrouter *vr, uint8_t priority)
{
vr->priority = priority;
vr->v4->priority = priority;
vr->v6->priority = priority;
}
void vrrp_set_advertisement_interval(struct vrrp_vrouter *vr,
uint16_t advertisement_interval)
{
if (vr->advertisement_interval == advertisement_interval)
return;
vr->advertisement_interval = advertisement_interval;
vrrp_recalculate_timers(vr->v4);
vrrp_recalculate_timers(vr->v6);
}
static bool vrrp_has_ip(struct vrrp_vrouter *vr, struct ipaddr *ip)
{
struct vrrp_router *r = ip->ipa_type == IPADDR_V4 ? vr->v4 : vr->v6;
struct listnode *ln;
struct ipaddr *iter;
for (ALL_LIST_ELEMENTS_RO(r->addrs, ln, iter))
if (!ipaddr_cmp(iter, ip))
return true;
return false;
}
int vrrp_add_ip(struct vrrp_vrouter *vr, struct ipaddr *ip)
{
struct vrrp_router *r = IS_IPADDR_V4(ip) ? vr->v4 : vr->v6;
int af = r->family;
assert(r->family == af);
assert(!(r->vr->version == 2 && ip->ipa_type == IPADDR_V6));
if (vrrp_has_ip(r->vr, ip))
return 0;
if (!vrrp_is_owner(r->vr->ifp, ip) && r->is_owner) {
char ipbuf[INET6_ADDRSTRLEN];
inet_ntop(r->family, &ip->ip, ipbuf, sizeof(ipbuf));
zlog_err(
VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"This VRRP router is not the address owner of %s, but is the address owner of other addresses; this config is unsupported.",
r->vr->vrid, family2str(r->family), ipbuf);
return -1;
}
struct ipaddr *new = XCALLOC(MTYPE_VRRP_IP, sizeof(struct ipaddr));
*new = *ip;
listnode_add(r->addrs, new);
if (r->fsm.state == VRRP_STATE_MASTER) {
switch (r->family) {
case AF_INET:
vrrp_garp_send(r, &new->ipaddr_v4);
break;
case AF_INET6:
vrrp_ndisc_una_send(r, new);
break;
}
}
return 0;
}
int vrrp_add_ipv4(struct vrrp_vrouter *vr, struct in_addr v4)
{
struct ipaddr ip;
ip.ipa_type = IPADDR_V4;
ip.ipaddr_v4 = v4;
return vrrp_add_ip(vr, &ip);
}
int vrrp_add_ipv6(struct vrrp_vrouter *vr, struct in6_addr v6)
{
assert(vr->version != 2);
struct ipaddr ip;
ip.ipa_type = IPADDR_V6;
ip.ipaddr_v6 = v6;
return vrrp_add_ip(vr, &ip);
}
int vrrp_del_ip(struct vrrp_vrouter *vr, struct ipaddr *ip)
{
struct listnode *ln, *nn;
struct ipaddr *iter;
int ret = 0;
struct vrrp_router *r = IS_IPADDR_V4(ip) ? vr->v4 : vr->v6;
if (!vrrp_has_ip(r->vr, ip))
return 0;
for (ALL_LIST_ELEMENTS(r->addrs, ln, nn, iter))
if (!ipaddr_cmp(iter, ip))
list_delete_node(r->addrs, ln);
/*
* NB: Deleting the last address and then issuing a shutdown will cause
* transmission of a priority 0 VRRP Advertisement - as per the RFC -
* but it will have no addresses. This is not forbidden in the RFC but
* might confuse other implementations.
*/
if (r->addrs->count == 0 && r->fsm.state != VRRP_STATE_INITIALIZE)
ret = vrrp_event(r, VRRP_EVENT_SHUTDOWN);
return ret;
}
int vrrp_del_ipv6(struct vrrp_vrouter *vr, struct in6_addr v6)
{
struct ipaddr ip;
ip.ipa_type = IPADDR_V6;
ip.ipaddr_v6 = v6;
return vrrp_del_ip(vr, &ip);
}
int vrrp_del_ipv4(struct vrrp_vrouter *vr, struct in_addr v4)
{
struct ipaddr ip;
ip.ipa_type = IPADDR_V4;
ip.ipaddr_v4 = v4;
return vrrp_del_ip(vr, &ip);
}
/* Creation and destruction ------------------------------------------------ */
static void vrrp_router_addr_list_del_cb(void *val)
{
struct ipaddr *ip = val;
XFREE(MTYPE_VRRP_IP, ip);
}
/*
* Search for a suitable macvlan subinterface we can attach to, and if found,
* attach to it.
*
* r
* Router to attach to interface
*
* Returns:
* Whether an interface was successfully attached
*/
static bool vrrp_attach_interface(struct vrrp_router *r)
{
/* Search for existing interface with computed MAC address */
struct interface **ifps;
size_t ifps_cnt =
if_lookup_by_hwaddr(r->vmac.octet, sizeof(r->vmac.octet), &ifps,
r->vr->ifp->vrf->vrf_id);
/*
* Filter to only those macvlan interfaces whose parent is the base
* interface this VRRP router is configured on.
*
* If there are still multiple interfaces we just select the first one,
* as it should be functionally identical to the others.
*/
unsigned int candidates = 0;
struct interface *selection = NULL;
for (unsigned int i = 0; i < ifps_cnt; i++) {
if (ifps[i]->link_ifindex != r->vr->ifp->ifindex)
ifps[i] = NULL;
else {
selection = selection ? selection : ifps[i];
candidates++;
}
}
if (ifps_cnt)
XFREE(MTYPE_TMP, ifps);
char ethstr[ETHER_ADDR_STRLEN];
prefix_mac2str(&r->vmac, ethstr, sizeof(ethstr));
assert(!!selection == !!candidates);
if (candidates == 0)
zlog_warn(VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Interface: None (no interface found w/ MAC %s)",
r->vr->vrid, family2str(r->family), ethstr);
else if (candidates > 1)
zlog_warn(VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Interface: Multiple interfaces found; using %s",
r->vr->vrid, family2str(r->family), selection->name);
else
zlog_info(VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Interface: %s",
r->vr->vrid, family2str(r->family), selection->name);
r->mvl_ifp = selection;
return !!r->mvl_ifp;
}
static struct vrrp_router *vrrp_router_create(struct vrrp_vrouter *vr,
int family)
{
struct vrrp_router *r =
XCALLOC(MTYPE_VRRP_RTR, sizeof(struct vrrp_router));
r->family = family;
r->sock_rx = -1;
r->sock_tx = -1;
r->vr = vr;
r->addrs = list_new();
r->addrs->del = vrrp_router_addr_list_del_cb;
r->priority = vr->priority;
r->fsm.state = VRRP_STATE_INITIALIZE;
vrrp_mac_set(&r->vmac, family == AF_INET6, vr->vrid);
vrrp_attach_interface(r);
return r;
}
static void vrrp_router_destroy(struct vrrp_router *r)
{
if (r->is_active)
vrrp_event(r, VRRP_EVENT_SHUTDOWN);
if (r->sock_rx >= 0)
close(r->sock_rx);
if (r->sock_tx >= 0)
close(r->sock_tx);
/* FIXME: also delete list elements */
list_delete(&r->addrs);
XFREE(MTYPE_VRRP_RTR, r);
}
struct vrrp_vrouter *vrrp_vrouter_create(struct interface *ifp, uint8_t vrid,
uint8_t version)
{
struct vrrp_vrouter *vr = vrrp_lookup(ifp, vrid);
if (vr)
return vr;
if (version != 2 && version != 3)
return NULL;
vr = XCALLOC(MTYPE_VRRP_RTR, sizeof(struct vrrp_vrouter));
vr->ifp = ifp;
vr->version = version;
vr->vrid = vrid;
vr->priority = vd.priority;
vr->preempt_mode = vd.preempt_mode;
vr->accept_mode = vd.accept_mode;
vr->checksum_with_ipv4_pseudoheader =
vd.checksum_with_ipv4_pseudoheader;
vr->shutdown = vd.shutdown;
vr->v4 = vrrp_router_create(vr, AF_INET);
vr->v6 = vrrp_router_create(vr, AF_INET6);
vrrp_set_advertisement_interval(vr, vd.advertisement_interval);
(void)hash_get(vrrp_vrouters_hash, vr, hash_alloc_intern);
return vr;
}
void vrrp_vrouter_destroy(struct vrrp_vrouter *vr)
{
vrrp_router_destroy(vr->v4);
vrrp_router_destroy(vr->v6);
hash_release(vrrp_vrouters_hash, vr);
XFREE(MTYPE_VRRP_RTR, vr);
}
struct vrrp_vrouter *vrrp_lookup(const struct interface *ifp, uint8_t vrid)
{
if (!ifp)
return NULL;
struct vrrp_vrouter vr;
vr.vrid = vrid;
vr.ifp = (struct interface *)ifp;
return hash_lookup(vrrp_vrouters_hash, &vr);
}
/* Network ----------------------------------------------------------------- */
/* Forward decls */
static void vrrp_change_state(struct vrrp_router *r, int to);
static void vrrp_adver_timer_expire(struct event *thread);
static void vrrp_master_down_timer_expire(struct event *thread);
/*
* Finds the first connected address of the appropriate family on a VRRP
* router's interface and binds the Tx socket of the VRRP router to that
* address.
*
* Also sets src field of vrrp_router.
*
* r
* VRRP router to operate on
*
* Returns:
* 0 on success
* -1 on failure
*/
static int vrrp_bind_to_primary_connected(struct vrrp_router *r)
{
struct interface *ifp;
/*
* A slight quirk: the RFC specifies that advertisements under IPv6 must
* be transmitted using the link local address of the source interface
*/
ifp = r->family == AF_INET ? r->vr->ifp : r->mvl_ifp;
struct connected *c = NULL;
frr_each (if_connected, ifp->connected, c)
if (c->address->family == r->family) {
if (r->family == AF_INET6
&& IN6_IS_ADDR_LINKLOCAL(&c->address->u.prefix6))
break;
else if (r->family == AF_INET)
break;
}
if (c == NULL) {
zlog_err(VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Failed to find address to bind on %s",
r->vr->vrid, family2str(r->family), ifp->name);
return -1;
}
union sockunion su;
memset(&su, 0x00, sizeof(su));
switch (r->family) {
case AF_INET:
r->src.ipa_type = IPADDR_V4;
r->src.ipaddr_v4 = c->address->u.prefix4;
su.sin.sin_family = AF_INET;
su.sin.sin_addr = c->address->u.prefix4;
break;
case AF_INET6:
r->src.ipa_type = IPADDR_V6;
r->src.ipaddr_v6 = c->address->u.prefix6;
su.sin6.sin6_family = AF_INET6;
su.sin6.sin6_scope_id = ifp->ifindex;
su.sin6.sin6_addr = c->address->u.prefix6;
break;
}
int ret = 0;
sockopt_reuseaddr(r->sock_tx);
if (bind(r->sock_tx, (const struct sockaddr *)&su, sizeof(su)) < 0) {
zlog_err(
VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Failed to bind Tx socket to primary IP address %pFX: %s",
r->vr->vrid, family2str(r->family), c->address,
safe_strerror(errno));
ret = -1;
} else {
DEBUGD(&vrrp_dbg_sock,
VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Bound Tx socket to primary IP address %pFX",
r->vr->vrid, family2str(r->family), c->address);
}
return ret;
}
/*
* Create and multicast a VRRP ADVERTISEMENT message.
*
* r
* VRRP Router for which to send ADVERTISEMENT
*/
static void vrrp_send_advertisement(struct vrrp_router *r)
{
struct vrrp_pkt *pkt;
ssize_t pktsz;
struct ipaddr *addrs[r->addrs->count];
union sockunion dest;
if (r->src.ipa_type == IPADDR_NONE
&& vrrp_bind_to_primary_connected(r) < 0)
return;
list_to_array(r->addrs, (void **)addrs, r->addrs->count);
pktsz = vrrp_pkt_adver_build(&pkt, &r->src, r->vr->version, r->vr->vrid,
r->priority, r->vr->advertisement_interval,
r->addrs->count, (struct ipaddr **)&addrs,
r->vr->checksum_with_ipv4_pseudoheader);
if (DEBUG_MODE_CHECK(&vrrp_dbg_pkt, DEBUG_MODE_ALL))
zlog_hexdump(pkt, (size_t)pktsz);
const char *group = r->family == AF_INET ? VRRP_MCASTV4_GROUP_STR
: VRRP_MCASTV6_GROUP_STR;
(void)str2sockunion(group, &dest);
ssize_t sent = sendto(r->sock_tx, pkt, (size_t)pktsz, 0, &dest.sa,
sockunion_sizeof(&dest));
vrrp_pkt_free(pkt);
if (sent < 0) {
zlog_warn(VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Failed to send VRRP Advertisement: %s",
r->vr->vrid, family2str(r->family),
safe_strerror(errno));
} else {
++r->stats.adver_tx_cnt;
}
}
/*
* Receive and parse VRRP advertisement.
*
* By the time we get here all fields have been validated for basic correctness
* and the packet is a valid VRRP packet.
*
* However, we have not validated whether the VRID is correct for this virtual
* router, nor whether the priority is correct (i.e. is not 255 when we are the
* address owner), nor whether the advertisement interval equals our own
* configured value (this check is only performed in VRRPv2).
*
* r
* VRRP Router associated with the socket this advertisement was received on
*
* src
* Source address of sender
*
* pkt
* The advertisement they sent
*
* pktsize
* Size of advertisement
*
* Returns:
* -1 if advertisement is invalid
* 0 otherwise
*/
static int vrrp_recv_advertisement(struct vrrp_router *r, struct ipaddr *src,
struct vrrp_pkt *pkt, size_t pktsize)
{
char sipstr[INET6_ADDRSTRLEN];
char dipstr[INET6_ADDRSTRLEN];
ipaddr2str(src, sipstr, sizeof(sipstr));
ipaddr2str(&r->src, dipstr, sizeof(dipstr));
char dumpbuf[BUFSIZ];
vrrp_pkt_adver_dump(dumpbuf, sizeof(dumpbuf), pkt);
DEBUGD(&vrrp_dbg_proto,
VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Received VRRP Advertisement from %s: %s",
r->vr->vrid, family2str(r->family), sipstr, dumpbuf);
/* Check that VRID matches our configured VRID */
if (pkt->hdr.vrid != r->vr->vrid) {
DEBUGD(&vrrp_dbg_proto,
VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Datagram invalid: Advertisement contains VRID %hhu which does not match our instance",
r->vr->vrid, family2str(r->family), pkt->hdr.vrid);
return -1;
}
/* Verify that we are not the IPvX address owner */
if (r->is_owner) {
DEBUGD(&vrrp_dbg_proto,
VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Datagram invalid: Received advertisement but we are the address owner",
r->vr->vrid, family2str(r->family));
return -1;
}
/* If v2, verify that adver time matches ours */
bool adveq = (pkt->hdr.v2.adver_int
== MAX(r->vr->advertisement_interval / 100, 1));
if (r->vr->version == 2 && !adveq) {
DEBUGD(&vrrp_dbg_proto,
VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Datagram invalid: Received advertisement with advertisement interval %hhu unequal to our configured value %u",
r->vr->vrid, family2str(r->family),
pkt->hdr.v2.adver_int,
MAX(r->vr->advertisement_interval / 100, 1));
return -1;
}
/* Check that # IPs received matches our # configured IPs */
if (pkt->hdr.naddr != r->addrs->count)
DEBUGD(&vrrp_dbg_proto,
VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Datagram has %hhu addresses, but this VRRP instance has %u",
r->vr->vrid, family2str(r->family), pkt->hdr.naddr,
r->addrs->count);
++r->stats.adver_rx_cnt;
int addrcmp;
switch (r->fsm.state) {
case VRRP_STATE_MASTER:
addrcmp = ipaddr_cmp(src, &r->src);
if (pkt->hdr.priority == 0) {
vrrp_send_advertisement(r);
EVENT_OFF(r->t_adver_timer);
event_add_timer_msec(master, vrrp_adver_timer_expire, r,
r->vr->advertisement_interval *
CS2MS,
&r->t_adver_timer);
} else if (pkt->hdr.priority > r->priority
|| ((pkt->hdr.priority == r->priority)
&& addrcmp > 0)) {
zlog_info(
VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Received advertisement from %s w/ priority %hhu; switching to Backup",
r->vr->vrid, family2str(r->family), sipstr,
pkt->hdr.priority);
EVENT_OFF(r->t_adver_timer);
if (r->vr->version == 3) {
r->master_adver_interval =
htons(pkt->hdr.v3.adver_int);
}
vrrp_recalculate_timers(r);
EVENT_OFF(r->t_master_down_timer);
event_add_timer_msec(master,
vrrp_master_down_timer_expire, r,
r->master_down_interval * CS2MS,
&r->t_master_down_timer);
vrrp_change_state(r, VRRP_STATE_BACKUP);
} else {
/* Discard advertisement */
DEBUGD(&vrrp_dbg_proto,
VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Discarding advertisement from %s (%hhu <= %hhu & %s <= %s)",
r->vr->vrid, family2str(r->family), sipstr,
pkt->hdr.priority, r->priority, sipstr, dipstr);
}
break;
case VRRP_STATE_BACKUP:
if (pkt->hdr.priority == 0) {
EVENT_OFF(r->t_master_down_timer);
event_add_timer_msec(
master, vrrp_master_down_timer_expire, r,
r->skew_time * CS2MS, &r->t_master_down_timer);
} else if (!r->vr->preempt_mode
|| pkt->hdr.priority >= r->priority) {
if (r->vr->version == 3) {
r->master_adver_interval =
ntohs(pkt->hdr.v3.adver_int);
}
vrrp_recalculate_timers(r);
EVENT_OFF(r->t_master_down_timer);
event_add_timer_msec(master,
vrrp_master_down_timer_expire, r,
r->master_down_interval * CS2MS,
&r->t_master_down_timer);
} else if (r->vr->preempt_mode
&& pkt->hdr.priority < r->priority) {
/* Discard advertisement */
DEBUGD(&vrrp_dbg_proto,
VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Discarding advertisement from %s (%hhu < %hhu & preempt = true)",
r->vr->vrid, family2str(r->family), sipstr,
pkt->hdr.priority, r->priority);
}
break;
case VRRP_STATE_INITIALIZE:
zlog_err(VRRP_LOGPFX VRRP_LOGPFX_VRID VRRP_LOGPFX_FAM
"Received ADVERTISEMENT in state %s; this is a bug",
r->vr->vrid, family2str(r->family),
vrrp_state_names[r->fsm.state]);
break;
}
return 0;
}
/*
* Read and process next IPvX datagram.
*/
static void vrrp_read(struct event *thread)
{
struct vrrp_router *r = EVENT_ARG(thread);
struct vrrp_pkt *pkt;
ssize_t pktsize;
ssize_t nbytes;
bool resched;
char errbuf[BUFSIZ];
struct sockaddr_storage sa;
uint8_t control[64];
struct ipaddr src = {};
struct msghdr m = {};
struct iovec iov;
iov.iov_base = r->ibuf;
iov.iov_len = sizeof(r->ibuf);
m.msg_name = &sa;
m.msg_namelen = sizeof(sa);
m.msg_iov = &iov;
m.msg_iovlen = 1;
m.msg_control = control;