~ubuntu-branches/ubuntu/trusty/keepalived/trusty

« back to all changes in this revision

Viewing changes to keepalived/vrrp/vrrp.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt
  • Date: 2005-04-29 23:22:40 UTC
  • mfrom: (1.1.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050429232240-a8m3jtpi3cvuyyy2
Tags: 1.1.11-3
Added a warning about sarge kernels to README.Debian and 
the package description 

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 *              master fails, a backup server takes over.
9
9
 *              The original implementation has been made by jerome etienne.
10
10
 *
11
 
 * Version:     $Id: vrrp.c,v 1.1.7 2004/04/04 23:28:05 acassen Exp $
 
11
 * Version:     $Id: vrrp.c,v 1.1.11 2005/03/01 01:22:13 acassen Exp $
12
12
 *
13
13
 * Author:      Alexandre Cassen, <acassen@linux-vs.org>
14
14
 *
22
22
 *              as published by the Free Software Foundation; either version
23
23
 *              2 of the License, or (at your option) any later version.
24
24
 *
25
 
 * Copyright (C) 2001-2004 Alexandre Cassen, <acassen@linux-vs.org>
 
25
 * Copyright (C) 2001-2005 Alexandre Cassen, <acassen@linux-vs.org>
26
26
 */
27
27
 
28
28
/* local include */
38
38
#include "vrrp_index.h"
39
39
#include "memory.h"
40
40
#include "list.h"
41
 
 
42
 
/* extern global vars */
43
 
extern vrrp_conf_data *vrrp_data;
44
 
extern vrrp_conf_data *old_vrrp_data;
45
 
extern unsigned int debug;
46
 
 
47
 
/* compute checksum */
48
 
static u_short
49
 
in_csum(u_short * addr, int len, u_short csum)
50
 
{
51
 
        register int nleft = len;
52
 
        const u_short *w = addr;
53
 
        register u_short answer;
54
 
        register int sum = csum;
55
 
 
56
 
        /*
57
 
         *  Our algorithm is simple, using a 32 bit accumulator (sum),
58
 
         *  we add sequential 16 bit words to it, and at the end, fold
59
 
         *  back all the carry bits from the top 16 bits into the lower
60
 
         *  16 bits.
61
 
         */
62
 
        while (nleft > 1) {
63
 
                sum += *w++;
64
 
                nleft -= 2;
65
 
        }
66
 
 
67
 
        /* mop up an odd byte, if necessary */
68
 
        if (nleft == 1)
69
 
                sum += htons(*(u_char *) w << 8);
70
 
 
71
 
        /*
72
 
         * add back carry outs from top 16 bits to low 16 bits
73
 
         */
74
 
        sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
75
 
        sum += (sum >> 16);                     /* add carry */
76
 
        answer = ~sum;                          /* truncate to 16 bits */
77
 
        return (answer);
78
 
}
 
41
#include "main.h"
 
42
#include "utils.h"
79
43
 
80
44
/* add/remove Virtual IP addresses */
81
45
static int
259
223
        }
260
224
 
261
225
        /* MUST verify the VRRP checksum */
262
 
        if (in_csum((u_short *) hd, vrrp_hd_len(vrrp), 0)) {
 
226
        if (in_csum((u_short *) hd,
 
227
            sizeof(vrrp_pkt) + VRRP_AUTH_LEN + hd->naddr * sizeof(uint32_t), 0)) {
263
228
                syslog(LOG_INFO, "Invalid vrrp checksum");
264
229
                return VRRP_PACKET_KO;
265
230
        }
424
389
           In the current implementation if counter has cycled, we stop sending adverts and 
425
390
           become BACKUP. If all the master are down we reset the counter for becoming MASTER.
426
391
         */
427
 
//  if (vrrp->ipsecah_counter->seq_number > 5) {
428
392
        if (vrrp->ipsecah_counter->seq_number > 0xFFFFFFFD) {
429
393
                vrrp->ipsecah_counter->cycle = 1;
430
394
        } else {
614
578
                return;
615
579
 
616
580
        /* send gratuitous arp for each virtual ip */
617
 
        if (debug & 32)
618
 
                syslog(LOG_INFO, "VRRP_Instance(%s) Sending gratuitous ARP on %s",
619
 
                       vrrp->iname, IF_NAME(vrrp->ifp));
620
 
 
621
581
        for (j = 0; j < 5; j++) {
622
582
                if (!LIST_ISEMPTY(vrrp->vip))
623
583
                        for (e = LIST_HEAD(vrrp->vip); e; ELEMENT_NEXT(e)) {
624
584
                                ipaddress = ELEMENT_DATA(e);
 
585
                                if (0 == j && debug & 32)
 
586
                                        syslog(LOG_INFO,
 
587
                                               "VRRP_Instance(%s) Sending gratuitous ARPs "
 
588
                                               "on %s for %s",
 
589
                                               vrrp->iname,
 
590
                                               IF_NAME(ipaddress->ifp),
 
591
                                               inet_ntop2(ipaddress->addr));
625
592
                                send_gratuitous_arp(ipaddress);
626
593
                        }
627
594
                if (!LIST_ISEMPTY(vrrp->evip))
628
595
                        for (e = LIST_HEAD(vrrp->evip); e; ELEMENT_NEXT(e)) {
629
596
                                ipaddress = ELEMENT_DATA(e);
 
597
                                if (0 == j && debug & 32)
 
598
                                        syslog(LOG_INFO,
 
599
                                               "VRRP_Instance(%s) Sending gratuitous ARPs "
 
600
                                               "on %s for %s",
 
601
                                               vrrp->iname,
 
602
                                               IF_NAME(ipaddress->ifp),
 
603
                                               inet_ntop2(ipaddress->addr));
630
604
                                send_gratuitous_arp(ipaddress);
631
605
                        }
632
606
        }
675
649
}
676
650
 
677
651
/* leaving master state */
678
 
static void
 
652
void
679
653
vrrp_restore_interface(vrrp_rt * vrrp, int advF)
680
654
{
681
 
        /* remove the ip addresses */
682
 
        if (VRRP_VIP_ISSET(vrrp)) {
 
655
        /* remove virtual routes */
 
656
        if (!LIST_ISEMPTY(vrrp->vroutes))
 
657
                vrrp_handle_iproutes(vrrp, IPROUTE_DEL);
 
658
 
 
659
        /*
 
660
         * Remove the ip addresses.
 
661
         *
 
662
         * If started with "--dont-release-vrrp" (debug & 8) then try to remove
 
663
         * addresses even if we didn't add them during this run.
 
664
         */
 
665
        if (debug & 8 || VRRP_VIP_ISSET(vrrp)) {
683
666
                if (!LIST_ISEMPTY(vrrp->vip))
684
667
                        vrrp_handle_ipaddress(vrrp, IPADDRESS_DEL,
685
668
                                              VRRP_VIP_TYPE);
689
672
                vrrp->vipset = 0;
690
673
        }
691
674
 
692
 
        /* remove virtual routes */
693
 
        if (!LIST_ISEMPTY(vrrp->vroutes))
694
 
                vrrp_handle_iproutes(vrrp, IPROUTE_DEL);
695
675
 
696
676
        /* if we stop vrrp, warn the other routers to speed up the recovery */
697
677
        if (advF)
761
741
                    3 * vrrp->adver_int + VRRP_TIMER_SKEW(vrrp);
762
742
        } else if (hd->priority == 0) {
763
743
                vrrp->ms_down_timer = VRRP_TIMER_SKEW(vrrp);
764
 
        } else if (!vrrp->preempt || hd->priority >= vrrp->priority) {
 
744
        } else if (vrrp->nopreempt || hd->priority >= vrrp->priority ||
 
745
                   timer_cmp(vrrp->preempt_time, timer_now()) > 0) {
765
746
                vrrp->ms_down_timer =
766
747
                    3 * vrrp->adver_int + VRRP_TIMER_SKEW(vrrp);
767
748
        } else if (hd->priority < vrrp->priority) {
924
905
 
925
906
/* open a VRRP sending socket */
926
907
int
927
 
open_vrrp_send_socket(const int proto, const int index)
 
908
open_vrrp_send_socket(const int proto, const int idx)
928
909
{
929
910
        interface *ifp;
930
911
        int fd = -1;
931
912
 
932
913
        /* Retreive interface */
933
 
        ifp = if_get_by_ifindex(index);
 
914
        ifp = if_get_by_ifindex(idx);
934
915
 
935
916
        /* Create and init socket descriptor */
936
917
        fd = socket(AF_INET, SOCK_RAW, proto);
951
932
 
952
933
/* open a VRRP socket and join the multicast group. */
953
934
int
954
 
open_vrrp_socket(const int proto, const int index)
 
935
open_vrrp_socket(const int proto, const int idx)
955
936
{
956
937
        interface *ifp;
957
938
        int fd = -1;
958
939
 
959
940
        /* Retreive interface */
960
 
        ifp = if_get_by_ifindex(index);
961
 
 
962
 
        /* Simply return if interface is shut */
963
 
        if (!IF_ISUP(ifp))
964
 
                return fd;
 
941
        ifp = if_get_by_ifindex(idx);
965
942
 
966
943
        /* open the socket */
967
944
        fd = socket(AF_INET, SOCK_RAW, proto);
1024
1001
                        vrrp_restore_interface(vrrp, 1);
1025
1002
 
1026
1003
#ifdef _HAVE_IPVS_SYNCD_
1027
 
                /* Stop stalled syncd */
 
1004
                /*
 
1005
                 * Stop stalled syncd. IPVS syncd state is the
 
1006
                 * same as VRRP instance one. We need here to
 
1007
                 * stop stalled syncd thread according to last
 
1008
                 * VRRP instance state.
 
1009
                 */
1028
1010
                if (vrrp->lvs_syncd_if)
1029
 
                        ipvs_syncd_cmd(IPVS_STOPDAEMON, NULL, 0);
 
1011
                        ipvs_syncd_cmd(IPVS_STOPDAEMON, NULL,
 
1012
                                       (vrrp->state == VRRP_STATE_MAST) ? IPVS_MASTER:
 
1013
                                                                          IPVS_BACKUP);
1030
1014
#endif
1031
1015
        }
1032
1016
}
1040
1024
                vrrp->adver_int = VRRP_ADVER_DFL * TIMER_HZ;
1041
1025
        if (!vrrp->priority)
1042
1026
                vrrp->priority = VRRP_PRIO_DFL;
1043
 
        if (!vrrp->preempt)
1044
 
                vrrp->preempt = VRRP_PREEMPT_DFL;
1045
1027
 
1046
1028
        return (chk_min_cfg(vrrp));
1047
1029
}
1080
1062
        list l = vrrp_data->vrrp;
1081
1063
        vrrp_rt *vrrp;
1082
1064
 
 
1065
        if (LIST_ISEMPTY(l))
 
1066
                return NULL;
 
1067
 
1083
1068
        for (e = LIST_HEAD(l); e; ELEMENT_NEXT(e)) {
1084
1069
                vrrp = ELEMENT_DATA(e);
1085
1070
                if (!strcmp(vrrp->iname, old_vrrp->iname))
1115
1100
        list l = old_vrrp_data->vrrp;
1116
1101
        vrrp_rt *vrrp;
1117
1102
 
 
1103
        if (LIST_ISEMPTY(l))
 
1104
                return;
 
1105
 
1118
1106
        for (e = LIST_HEAD(l); e; ELEMENT_NEXT(e)) {
1119
1107
                vrrp = ELEMENT_DATA(e);
1120
1108