This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathvrrpd.c
1898 lines (1717 loc) · 59.1 KB
/
vrrpd.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
/*==========================[ (c) JME SOFT ]===================================
FILE : [vrrp.c]
CREATED : 00/02/02 12:54:37 LAST SAVE : 00/10/04 22:11:39
WHO : jerome@mycpu Linux 2.2.14
REMARK :
================================================================================
- This program 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 of the License, or (at your option) any later version.
==============================================================================*/
/* system include */
#include <stdio.h>
#include <assert.h>
#include <net/ethernet.h>
#include <netinet/ip.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stddef.h>
#include <time.h>
#include <sys/sysinfo.h>
#include <sys/errno.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <string.h>
#include <sys/dir.h>
typedef unsigned int u64;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned char u8;
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <fcntl.h>
/* local include */
#include "vrrpd.h"
#include "ipaddr.h"
int ip_id = 0; /* to have my own ip_id creates collision with kernel ip->id
** but it should be ok because the packets are unlikely to be
** fragmented (they are non routable and small) */
/* WORK: this packet isnt routed, i can check the outgoing MTU
** to warn the user only if the outoing mtu is too small */
static char vrrp_hwaddr[6]; // WORK: lame hardcoded for ethernet
static vrrp_rt glob_vsrv; /* a global because used in the signal handler*/
/* Scott added 9-4-02 */
int master_ipaddr = 0;
static char PidDir[FILENAME_MAX+1];
static char scriptdown[FILENAME_MAX+1];
static char upscript[FILENAME_MAX+1];
uint32_t magicsipaddr;
char magicdipaddr[FILENAME_MAX+1];
FILE *f;
FILE *f2;
static time_t timenow;
static char *timenowstring;
int mypid;
/****************************************************************
NAME : Monitoring only
AIM :
REMARK :
****************************************************************/
char buff[80];
int max_monitor = 9;
char pidend[6] = ".pid";
// Path generated in main function
char pidfilepath[FILENAME_MAX+1];
char separetepath[2] = "/";
int monitor = 0;
int monitortemp = 0;
int pid = 0;
int ix = 0;
int maxrand = 10;
int nb = 0;
int retval = 0;
/* default delay */
int showdelay = 1;
char globalstatedown[FILENAME_MAX+1];
char statefilepath[FILENAME_MAX+1];
char statedown[FILENAME_MAX+1] = "/vrrpdstatedown_";
char statedownfilepath[FILENAME_MAX+1];
char backup_reason[FILENAME_MAX+1];
char master_reason[FILENAME_MAX+1];
char temp[FILENAME_MAX+1];
/****************************************************************
NAME : killvrrpd
AIM :
REMARK :
****************************************************************/
void killvrrpd(int killnu,char *ifname)
{
vrrp_rt *vsrv = &glob_vsrv;
char namepid[FILENAME_MAX+1]="vrrpd_";
char pidfilepath_final[FILENAME_MAX+1]="";
int pidtab[max_monitor];
// Tab with 0 value
for (ix = 0 ; ix < max_monitor; ix++)
pidtab[ix] = 0;
// Search process pid //
DIR *currentDir;
struct dirent *fichier;
if( NULL == ( currentDir = opendir(PidDir))) {
perror( "opendir()" );
} else {
ix = 0;
while( NULL != ( fichier = readdir( currentDir ))) {
if(!strncmp(namepid, fichier->d_name, 6) ){
strcpy(pidfilepath_final,pidfilepath);
strcat(pidfilepath_final,"/");
strcat(pidfilepath_final,fichier->d_name);
// ok we found vrrpd pid file
if ((f = fopen(pidfilepath_final, "rb")) != NULL){
//strcat(statefilepath,fichier->d_name);
fgets(buff, sizeof(buff), f);
fclose(f);
int pid2 = atoi(buff);
if (ix < max_monitor){
pidtab[ix] = pid2;
ix++;
}
}
}
}
closedir(currentDir);
}
/* Search another process with Backup state */
for (ix=0; ix <= max_monitor; ix++) {
sprintf (&statedownfilepath[24],"%d",ix);
/* AT FIRST LOCK MY VRRPD PROCESS */
/* Backup myself */
/* DON'T REMOVE, DOUBLE "IF" AVOID UNDE FILES */
if ((f = fopen(statedownfilepath, "rb")) == NULL){
if ((strlen(globalstatedown) == 0)){
if (monitor){
f = fopen(statedownfilepath,"w");
fprintf(f, "%d", mypid );
/* Now record my previous state */
strcpy(globalstatedown,statedownfilepath);
fclose(f);
}
vsrv->wantstate = VRRP_STATE_INIT;
vsrv->state = VRRP_STATE_INIT;
/* Killnu = 12 only at start */
if (killnu == 12){
return;
}
}
} else {
fclose(f);
}
}
/* Now action ! You shall not pass ! */
for (ix = 0 ; ix < max_monitor ; ix++) {
sprintf (&statedownfilepath[24],"%d",ix);
if ((f = fopen(statedownfilepath, "rb")) != NULL) {
fgets(buff, sizeof(buff), f);
fclose(f);
int pid = atoi(buff);
/* Another pid ? Switch to backup */
for (nb = 0; nb < max_monitor; nb++){
/* we remove the process if already backup */
if ((pidtab[nb] == pid) && (pidtab[nb] != mypid)) {
pidtab[nb] = 0;
}
}
}
}
/* send kill signal to master process */
/* Kill the others */
for (ix = 0 ; ix < max_monitor ; ix++) {
if ((pidtab[ix] != 0) && (pidtab[ix] != mypid))
kill(pidtab[ix],SIGUSR2);
}
/* Lock myself */
if ( vsrv->state == VRRP_STATE_MAST ){
killvrrpd(12,vsrv->vif.ifname);
vsrv->wantstate = VRRP_STATE_BACK;
vsrv->state = VRRP_STATE_INIT;
}
}
/****************************************************************
NAME : get_pid_name 00/10/04 21:06:44
AIM :
REMARK :
****************************************************************/
static char *pidfile_get_name( vrrp_rt *vsrv )
{
static char pidfile[2*FILENAME_MAX+1];
snprintf( pidfile, sizeof(pidfile), "%s/" VRRP_PID_FORMAT
, PidDir
, vsrv->vif.ifname );
return pidfile;
}
/****************************************************************
NAME : pidfile_write 00/10/04 21:12:26
AIM :
REMARK : write the pid file
****************************************************************/
static int pidfile_write( vrrp_rt *vsrv )
{
char *name = pidfile_get_name(vsrv);
FILE *fOut = fopen( name, "w" );
if( !fOut ){
fprintf( stderr, "Can't open %s (errno %d %s)\n", name
, errno
, strerror(errno)
);
return -1;
}
fprintf( fOut, "%d\n", mypid );
fclose( fOut );
return(0);
}
/****************************************************************
NAME : pidfile_rm 00/10/04 21:12:26
AIM :
REMARK :
****************************************************************/
static void pidfile_rm( vrrp_rt *vsrv )
{
unlink( pidfile_get_name(vsrv) );
if ((f = fopen(globalstatedown, "r")) != NULL) {
unlink(globalstatedown);
fclose(f);
}
// remove state file eg: /var/run/.vrrpstate456
unlink(statefilepath);
}
/****************************************************************
NAME : pidfile_exist 00/10/04 21:12:26
AIM : return 0 if there is no valid pid in the pidfile or no pidfile
REMARK :
****************************************************************/
static int pidfile_exist( vrrp_rt *vsrv )
{
char *name = pidfile_get_name(vsrv);
FILE *fIn = fopen( name, "r" );
pid_t pid;
/* if there is no file */
if( !fIn ) return 0;
fscanf( fIn, "%d", &pid );
fclose( fIn );
/* if there is no process, remove the stale file */
if( kill( pid, 0 ) ){
fprintf(stderr, "Remove a stale pid file %s\n", name );
pidfile_rm( vsrv );
return 0;
}
/* if the kill suceed, return an error */
return -1;
}
/****************************************************************
NAME : in_csum 00/05/10 20:12:20
AIM : compute a IP checksum
REMARK : from kuznet's iputils
****************************************************************/
static u_short in_csum( u_short *addr, int len, u_short csum)
{
register int nleft = len;
const u_short *w = addr;
register u_short answer;
register int sum = csum;
/*
* Our algorithm is simple, using a 32 bit accumulator (sum),
* we add sequential 16 bit words to it, and at the end, fold
* back all the carry bits from the top 16 bits into the lower
* 16 bits.
*/
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
/* mop up an odd byte, if necessary */
if (nleft == 1)
sum += htons(*(u_char *)w << 8);
/*
* add back carry outs from top 16 bits to low 16 bits
*/
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */
return (answer);
}
/****************************************************************
NAME : get_dev_from_ip 00/02/08 06:51:32
AIM :
REMARK :
****************************************************************/
static uint32_t ifname_to_ip( char *ifname )
{
struct ifreq ifr;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
uint32_t addr = 0;
if (fd < 0) return (-1);
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) == 0) {
struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
addr = ntohl(sin->sin_addr.s_addr);
}
close(fd);
return addr;
}
/****************************************************************
NAME : get_dev_from_ip 00/02/08 06:51:32
AIM :
REMARK :
****************************************************************/
static int ifname_to_idx( char *ifname )
{
struct ifreq ifr;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
int ifindex = -1;
if (fd < 0) return (-1);
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFINDEX, (char *)&ifr) == 0)
ifindex = ifr.ifr_ifindex;
close(fd);
return ifindex;
}
/****************************************************************
NAME : rcvhwaddr_op 00/02/08 06:51:32
AIM :
REMARK :
****************************************************************/
static int rcvhwaddr_op( char *ifname, char *addr, int addrlen, int addF )
{
struct ifreq ifr;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
int ret;
if (fd < 0) return (-1);
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
memcpy( ifr.ifr_hwaddr.sa_data, addr, addrlen );
ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
ret = ioctl(fd, addF ? SIOCADDMULTI : SIOCDELMULTI, (char *)&ifr);
if( ret ){
printf("Can't %s on %s. errno=%d\n"
, addF ? "SIOCADDMULTI" : "SIOCDELMULTI"
, ifname, errno );
}
close(fd);
return ret;
}
/****************************************************************
NAME : hwaddr_set 00/02/08 06:51:32
AIM :
REMARK : linux refuse to change the hwaddress if the interface is up
****************************************************************/
static int hwaddr_set( char *ifname, char *addr, int addrlen )
{
struct ifreq ifr;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
int ret;
unsigned long flags;
if (fd < 0) return (-1);
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
/* get the flags */
ret = ioctl(fd, SIOCGIFFLAGS, (char *)&ifr);
if( ret ) goto end;
flags = ifr.ifr_flags;
/* first we try to change the hwaddr */
memcpy( ifr.ifr_hwaddr.sa_data, addr, addrlen );
ifr.ifr_hwaddr.sa_family = AF_UNIX;
ret = ioctl(fd, SIOCSIFHWADDR, (char *)&ifr);
if( ret ) {
/* if there is a error */
/* set the interface down */
ifr.ifr_flags &= ~IFF_UP;
ret = ioctl(fd, SIOCSIFFLAGS, (char *)&ifr);
if( ret ) goto end;
/* change the hwaddr */
memcpy( ifr.ifr_hwaddr.sa_data, addr, addrlen );
ifr.ifr_hwaddr.sa_family = AF_UNIX;
ret = ioctl(fd, SIOCSIFHWADDR, (char *)&ifr);
if( ret ) goto end;
/* set the interface up */
ifr.ifr_flags = flags;
ret = ioctl(fd, SIOCSIFFLAGS, (char *)&ifr);
if( ret ) goto end;
}
end:;
if( ret ) vrrpd_log(LOG_WARNING, "Can't add Virtual Mac erreur: %d",errno );
close(fd);
return ret;
}
/****************************************************************
NAME : hwaddr_get 00/02/08 06:51:32
AIM :
REMARK :
****************************************************************/
static int hwaddr_get( char *ifname, char *addr, int addrlen )
{
struct ifreq ifr;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
int ret;
if (fd < 0) return (-1);
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
ret = ioctl(fd, SIOCGIFHWADDR, (char *)&ifr);
memcpy( addr, ifr.ifr_hwaddr.sa_data, addrlen );
//printf("%x:%x:%x:%x:%x:%x\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5] );
close(fd);
return ret;
}
/****************************************************************
NAME : ipaddr_ops 00/02/08 06:51:32
AIM :
REMARK :
****************************************************************/
static int ipaddr_ops( vrrp_rt *vsrv, int addF )
{
int i, err = 0;
int ifidx = ifname_to_idx( vsrv->vif.ifname );
/* struct in_addr in;
*/
for( i = 0; i < vsrv->naddr; i++ ){
vip_addr *vadd = &vsrv->vaddr[i];
if( !addF && !vadd->deletable ) continue;
if( ipaddr_op(ifidx , vadd->addr, vadd->length, addF)){
err = 1;
vadd->deletable = 0;
/*
in.s_addr = htonl(vadd->addr);
VRRP_LOG(("cant %s the address %s to %s\n"
, addF ? "set" : "remove"
, inet_ntoa(in)
, vsrv->vif.ifname));
*/
}else{
vadd->deletable = 1;
}
}
return err;
}
/****************************************************************
NAME : vrrp_dlthd_len 00/02/02 15:16:23
AIM : return the vrrp header size in byte
REMARK :
****************************************************************/
static int vrrp_dlt_len( vrrp_rt *rt )
{
return ETHER_HDR_LEN; /* hardcoded for ethernet */
}
/****************************************************************
NAME : vrrp_iphdr_len 00/02/02 15:16:23
AIM : return the ip header size in byte
REMARK :
****************************************************************/
static int vrrp_iphdr_len( vrrp_rt *vsrv )
{
return sizeof( struct iphdr );
}
/****************************************************************
NAME : vrrp_hd_len 00/02/02 15:16:23
AIM : return the vrrp header size in byte
REMARK :
****************************************************************/
static int vrrp_hd_len( vrrp_rt *vsrv )
{
return sizeof( vrrp_pkt ) + vsrv->naddr*sizeof(uint32_t)
+ VRRP_AUTH_LEN;
}
/****************************************************************
NAME : vrrp_in_chk 00/02/02 12:54:54
AIM : check a incoming packet. return 0 if the pkt is valid, != 0 else
REMARK : rfc2338.7.1
****************************************************************/
static int vrrp_in_chk( vrrp_rt *vsrv, struct iphdr *ip )
{
int ihl = ip->ihl << 2;
vrrp_pkt * hd = (vrrp_pkt *)((char *)ip + ihl);
vrrp_if *vif = &vsrv->vif;
/* MUST verify that the IP TTL is 255 */
if( ip->ttl != VRRP_IP_TTL ) {
vrrpd_log(LOG_WARNING,"invalid ttl. %d and expect %d", ip->ttl,VRRP_IP_TTL);
return 1;
}
/* MUST verify the VRRP version */
if( (hd->vers_type >> 4) != VRRP_VERSION ){
vrrpd_log(LOG_WARNING,"invalid version. %d and expect %d"
, (hd->vers_type >> 4), VRRP_VERSION);
return 1;
}
/* MUST verify that the received packet length is greater than or
** equal to the VRRP header */
if( (ntohs(ip->tot_len)-ihl) <= sizeof(vrrp_pkt) ){
vrrpd_log(LOG_WARNING,"ip payload too short. %d and expect at least %zu"
, ntohs(ip->tot_len)-ihl, sizeof(vrrp_pkt) );
return 1;
}
/* WORK: MUST verify the VRRP checksum */
if( in_csum( (u_short*)hd, vrrp_hd_len(vsrv), 0) ){
vrrpd_log(LOG_WARNING,"Invalid vrrp checksum" );
return 1;
}
/* MUST perform authentication specified by Auth Type */
/* MUST verify that the VRID is valid on the receiving interface */
if( vsrv->vrid != hd->vrid ){
return 3;
}
/* ok we have the same vid */
/* check the authentication if it is a passwd */
if( hd->auth_type == VRRP_AUTH_PASS ){
char *pw = (char *)ip + ntohs(ip->tot_len)
-sizeof(vif->auth_data);
if( memcmp( pw, vif->auth_data, sizeof(vif->auth_data)) ){
if( vsrv->vrid == hd->vrid ){
if ((vsrv->state = VRRP_STATE_BACK) || (vsrv->state = VRRP_STATE_INIT)){
vrrpd_log(LOG_WARNING,"VRRPD group: receive an invalid passwd ! \n");
vrrpd_log(LOG_WARNING,"passwd peer: %s passwd: %s - vid: %d from same vid: %d \n", pw, vif->auth_data, vsrv->vrid, hd->vrid);
// Avoid full log ...
sleep (2);
kill(mypid,SIGTERM);
} else {
vrrpd_log(LOG_WARNING,"VRRPD group: receive an invalid passwd ! \n");
vrrpd_log(LOG_WARNING,"passwd peer: %s passwd: %s - vid: %d from same vid: %d \n", pw, vif->auth_data, vsrv->vrid, hd->vrid);
}
return 2;
}
}
} else {
/* check the authentication type */
if( vif->auth_type != hd->auth_type ){
if ((vsrv->state = VRRP_STATE_BACK) || (vsrv->state = VRRP_STATE_INIT)){
killvrrpd(13, vsrv->vif.ifname );
vrrpd_log(LOG_WARNING,"VRRPD group: Vid: %d receive an invalid passwd type from peer \n", vsrv->vrid);
vrrpd_log(LOG_WARNING,"VRRPD group: Vid: %d receive a %d auth, expecting %d ! from %d \n", vsrv->vrid, vif->auth_type, hd->auth_type, hd->vrid);
// Avoid full log ...
sleep (2);
kill(mypid,SIGTERM);
} else {
vrrpd_log(LOG_WARNING,"VRRPD group: Vid: %d receive an invalid passwd type \n", vsrv->vrid);
vrrpd_log(LOG_WARNING,"VRRPD group: Vid: %d receive a %d auth, expecting %d ! from %d \n", vsrv->vrid, vif->auth_type, hd->auth_type, hd->vrid);
}
return 2;
}
}
/* MAY verify that the IP address(es) associated with the VRID are
** valid */
/* WORK: currently we don't */
/* MUST verify that the Adver Interval in the packet is the same as
** the locally configured for this virtual router */
if( vsrv->adver_int/VRRP_TIMER_HZ != hd->adver_int ){
vrrpd_log(LOG_WARNING,"VRRPD advertissement: same id %d but interval mismatch mine=%d rcved=%d"
, vsrv->vrid, vsrv->adver_int/VRRP_TIMER_HZ, hd->adver_int );
// return 1;
}
/* Scott added 9-4-02 */
master_ipaddr = ip->saddr;
return 0;
}
/****************************************************************
NAME : vrrp_build_dlt 00/02/02 14:39:18
AIM :
REMARK : rfc2338.7.3
****************************************************************/
static void vrrp_build_dlt( vrrp_rt *vsrv, char *buffer, int buflen )
{
/* hardcoded for ethernet */
struct ether_header * eth = (struct ether_header *)buffer;
/* destination address --rfc1122.6.4*/
eth->ether_dhost[0] = 0x01;
eth->ether_dhost[1] = 0x00;
eth->ether_dhost[2] = 0x5E;
eth->ether_dhost[3] = (INADDR_VRRP_GROUP >> 16) & 0x7F;
eth->ether_dhost[4] = (INADDR_VRRP_GROUP >> 8) & 0xFF;
eth->ether_dhost[5] = INADDR_VRRP_GROUP & 0xFF;
/* source address --rfc2338.7.3 */
memcpy( eth->ether_shost, vrrp_hwaddr, sizeof(vrrp_hwaddr));
/* type */
eth->ether_type = htons( ETHERTYPE_IP );
}
/****************************************************************
NAME : vrrp_build_ip 00/02/02 14:39:18
AIM : build a ip packet
REMARK :
****************************************************************/
static void vrrp_build_ip( vrrp_rt *vsrv, char *buffer, int buflen )
{
struct iphdr * ip = (struct iphdr *)(buffer);
ip->ihl = 5;
ip->version = 4;
ip->tos = 0;
ip->tot_len = ip->ihl*4 + vrrp_hd_len( vsrv );
ip->tot_len = htons(ip->tot_len);
ip->id = ++ip_id;
ip->frag_off = 0;
ip->ttl = VRRP_IP_TTL;
ip->protocol = IPPROTO_VRRP;
ip->saddr = htonl(vsrv->vif.ipaddr);
ip->daddr = htonl(INADDR_VRRP_GROUP);
/* checksum must be done last */
ip->check = in_csum( (u_short*)ip, ip->ihl*4, 0 );
}
/****************************************************************
NAME : vrrp_build_vrrp 00/02/02 14:39:18
AIM :
REMARK :
****************************************************************/
static int vrrp_build_vrrp( vrrp_rt *vsrv, int prio, char *buffer, int buflen )
{
int i;
vrrp_if *vif = &vsrv->vif;
vrrp_pkt *hd = (vrrp_pkt *)buffer;
uint32_t *iparr = (uint32_t *)((char *)hd+sizeof(*hd));
hd->vers_type = (VRRP_VERSION<<4) | VRRP_PKT_ADVERT;
hd->vrid = vsrv->vrid;
hd->priority = prio;
hd->naddr = vsrv->naddr;
hd->auth_type = vsrv->vif.auth_type;
hd->adver_int = vsrv->adver_int/VRRP_TIMER_HZ;
/* copy the ip addresses */
for( i = 0; i < vsrv->naddr; i++ ){
iparr[i] = htonl( vsrv->vaddr[i].addr );
}
/* copy the passwd if the authentication is VRRP_AH_PASS */
if( vif->auth_type == VRRP_AUTH_PASS ){
char *pw = (char *)hd+sizeof(*hd)+vsrv->naddr*4;
memcpy( pw, vif->auth_data, sizeof(vif->auth_data));
}
/* Must perform the checksum AFTER we copy the password */
hd->chksum = in_csum( (u_short*)hd, vrrp_hd_len(vsrv), 0);
return(0);
}
/****************************************************************
NAME : vrrp_set_ptk 00/02/02 13:33:32
AIM : build a advertissement packet
REMARK :
****************************************************************/
static void vrrp_build_pkt( vrrp_rt *vsrv, int prio, char *buffer, int buflen )
{
// printf("dltlen=%d iplen=%d", vrrp_dlt_len(vsrv), vrrp_iphdr_len(vsrv) );
/* build the ethernet header */
vrrp_build_dlt( vsrv, buffer, buflen );
buffer += vrrp_dlt_len(vsrv);
buflen -= vrrp_dlt_len(vsrv);
/* build the ip header */
vrrp_build_ip( vsrv, buffer, buflen );
buffer += vrrp_iphdr_len(vsrv);
buflen -= vrrp_iphdr_len(vsrv);
/* build the vrrp header */
vrrp_build_vrrp( vsrv, prio, buffer, buflen );
}
/****************************************************************
NAME : vrrp_send_pkt 00/02/06 16:37:10
AIM :
REMARK :
****************************************************************/
static int vrrp_send_pkt( vrrp_rt *vsrv, char *buffer, int buflen )
{
struct sockaddr from;
int len;
int fd = socket(PF_PACKET, SOCK_PACKET, 0x300); /* 0x300 is magic */
// WORK:
if( fd < 0 ){
perror( "socket" );
return -1;
}
/* build the address */
memset( &from, 0 , sizeof(from));
strcpy( from.sa_data, vsrv->vif.ifname );
/* send the data */
len = sendto( fd, buffer, buflen, 0, &from, sizeof(from) );
//printf("len=%d\n",len);
close( fd );
return len;
}
/****************************************************************
NAME : hello_send_pkt
AIM :
REMARK :
****************************************************************/
void hello_send_pkt(vrrp_rt *vsrv, int addr)
{
struct sockaddr_in stSockAddr;
int Res;
int SocketFD = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
fcntl(SocketFD, F_SETFL, O_NONBLOCK);
if (-1 == SocketFD)
{
perror("cannot create socket");
}
memset(&stSockAddr, 0, sizeof(stSockAddr));
stSockAddr.sin_addr.s_addr = magicsipaddr;
vrrpd_log(LOG_WARNING, "VRRP ID %d: sending magic packet \n",vsrv->vrid);
if (-1 == bind(SocketFD,(struct sockaddr *)&stSockAddr, sizeof(stSockAddr)))
{
perror("error bind failed");
close(SocketFD);
return;
}
stSockAddr.sin_family = AF_INET;
stSockAddr.sin_port = htons(1100);
Res = inet_pton(AF_INET, magicdipaddr, &stSockAddr.sin_addr);
if (0 > Res){
perror("error: first parameter is not a valid address family");
close(SocketFD);
return;
} else if (0 == Res){
perror("char string (second parameter does not contain valid ipaddress)");
close(SocketFD);
return;
}
if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr))){
close(SocketFD);
return;
}
shutdown(SocketFD, SHUT_RDWR);
close(SocketFD);
return;
}
/****************************************************************
NAME : vrrp_send_adv 00/02/06 16:31:24
AIM :
REMARK :
****************************************************************/
static int vrrp_send_adv( vrrp_rt *vsrv, int prio )
{
int buflen, ret;
char * buffer;
#if 0 /* just for debug */
struct in_addr in;
in.s_addr = htonl(vsrv->vif.ipaddr);
printf("send an advertissement on %s\n",inet_ntoa(in) );
#endif
/* alloc the memory */
buflen = vrrp_dlt_len(vsrv) + vrrp_iphdr_len(vsrv) + vrrp_hd_len(vsrv);
buffer = calloc( buflen, 1 );
assert( buffer );
/* build the packet */
vrrp_build_pkt( vsrv, prio, buffer, buflen );
/* send it */
ret = vrrp_send_pkt( vsrv, buffer, buflen );
/* build the memory */
free( buffer );
return ret;
}
/****************************************************************
NAME : usage 00/02/06 08:50:28
AIM : display the usage
REMARK :
****************************************************************/
static void usage( void )
{
fprintf( stderr, "vrrpd version %s\n", VRRPD_VERSION );
fprintf( stderr, "Usage: vrrpd -i ifname -v vrid [ -M monitor ] [-s] [-a auth] [-p prio] [-z prio] [-x prio] [-nh] ipaddr\n" );
fprintf( stderr, " -h : display this short inlined help\n" );
fprintf( stderr, " -n : handle virtual mac address\n" );
fprintf( stderr, " -i ifname: the interface name to run on\n" );
fprintf( stderr, " -v vrid : the id of the virtual server [1-255]\n" );
fprintf( stderr, " -s : Switch the preemption mode (%s by default)\n"
, VRRP_PREEMPT_DFL? "Enabled" : "Disabled" );
fprintf( stderr, " -a auth : auth=(none|pw/hexkey|ah/hexkey) hexkey=0x[0-9a-fA-F]+\n");
fprintf( stderr, " -p prio : Set the priority of this host in the virtual server (dfl: %d)\n"
, VRRP_PRIO_DFL );
fprintf( stderr, " -d delay : Set the advertisement interval (in sec) (dfl: %d)\n"
, VRRP_ADVER_DFL );
fprintf( stderr, " -z prio : Set the priority after SIGTTIN (not decrement as default)\n");
fprintf( stderr, " -x prio : Set the priority after SIGTTOU (not increment as default)\n");
fprintf( stderr, " ipaddr/length : Should be at the end - IP address(es) of the virtual server and the length of the subnet mask - \n" );
fprintf( stderr, " -V : display version\n\n" );
fprintf( stderr, " ---------------------------------------------------------------------------\n");
fprintf( stderr, " Frederic Bourgeois https://gitlab.com/fredbcode \n");
fprintf( stderr, " Based on (http://sourceforge.net/projects/vrrpd/)\n\n");
fprintf( stderr, " Supplementary Options: \n");
fprintf( stderr, " -U : (-U <file>): run <file> to become a master)\n");
fprintf( stderr, " -D : (-D <file>): run <file> to become a backup)\n");
fprintf( stderr, " -M : (-M x) Monitoring process and Network (Max 9)\n");
fprintf( stderr, " If one vrrpd become backup (or died), all other process go to Backup state\n");
fprintf( stderr, " If one network interface failed (ifconfig Down, link) all other process go to Backup state\n");
fprintf( stderr, " Not supported (in part or whole) on all ethernet drivers\n\n");
fprintf( stderr, " POWER USERS --------------------------------------------------------------\n");
fprintf( stderr, " Magic packet erase arp table (for those who work without vmac - send fake packet to port 1100 -)\n");
fprintf( stderr, " -I ipvip : Choose VIP source to send magic packet - Erase vip from mac table -\n");
fprintf( stderr, " -O ipdst : Gateway destination\n");
fprintf( stderr, " Example: vrrpd -i eth0 -v 11 10.1.1.1 -I 10.1.1.1 -O 10.1.1.254\n");
fprintf( stderr, "\n");
}
/****************************************************************
NAME : parse_authopt 00/09/26 22:01:17
AIM :
REMARK : parse the authentication option from the user
****************************************************************/
static int parse_authopt(vrrp_rt *vsrv, char *str)
{
int len = strlen(str);
vrrp_if *vif = &vsrv->vif;
vif->auth_type = VRRP_AUTH_NONE;
/* epxlicitly no authentication */
if( !strcmp(str,"none") ) return 0;
/* below the min len */
if( len < 5 ) return -1;
/* check the type of authentication */
if( !strncmp(str, "ah/0x", 5 ) ){
vif->auth_type = VRRP_AUTH_AH;
return -1; /* WORK: not yet implemented */
#if 0 // I don't know what he was trying to do here! -- Scott 9-6-02
}else if( !strncmp(str, "pw/", 5 ) ){
int i,j;
vif->auth_type = VRRP_AUTH_PASS;
memset( vif->auth_data, 0, sizeof(vif->auth_data) );
/* parse the key */
for( i=5,j=0; j < sizeof(vif->auth_data)*2 && i<len ;i++,j++ ){
/* sanity check */
if( !isxdigit(str[i]) ) return -1;
str[i] = toupper(str[i]);
if( str[i] >= 'A' ) str[i] -= 'A' - 10;
else str[i] -= '0';
vif->auth_data[j/2] |= str[i] << (j&1 ? 0 : 4 );
//WORK: printf(" j=%d c=%d 0x%x\n",j,str[i],vif->auth_data[j/2]);
}
if( i != len ) return -1;
#endif
}else if( !strncmp(str, "pw/", 3 ) ){
vif->auth_type = VRRP_AUTH_PASS;
// Must set unused bytes to 0
memset (vif->auth_data, 0, sizeof(vif->auth_data) );
// Simply copy up to 8 characters
if (len > 11)
len = 11;
memcpy (vif->auth_data, &str[3], len - 3);
}else{
return -1;
}
return(0);
}
/****************************************************************
NAME : ethtool
AIM :
REMARK :
****************************************************************/
int detect_ethtool(int skfd, char *ifname)
{
struct ifreq ifr;
struct ethtool_value edata;
memset(&ifr, 0, sizeof(ifr));
edata.cmd = ETHTOOL_GLINK;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)-1);
ifr.ifr_data = (char *) &edata;
if (ioctl(skfd, SIOCETHTOOL, &ifr) == -1){
return 2;
}
return (edata.data ? 0 : 1);
}
/****************************************************************
NAME : parse_cmdline 00/02/06 09:09:11
AIM :
REMARK :
****************************************************************/
static int parse_cmdline( vrrp_rt *vsrv, int argc, char *argv[] )
{
vrrp_if *vif = &vsrv->vif;
int c;
vsrv->no_vmac = 1;
while( 1 ){
c = getopt( argc, argv, "f:M:Vhnsi:v:a:p:z:x:d:D:U:I:O:" );
/* if the parsing is completed, exit */
if( c == EOF ) break;
switch( c ){
case 'n':
vsrv->no_vmac = 0;
break;
case 's':
vsrv->preempt = !vsrv->preempt;
break;
case 'f':
snprintf( PidDir, sizeof(PidDir), "%s", optarg );
break;
case 'i':
vif->ifname = strdup( optarg );
/* get the ip address */
vif->ipaddr = ifname_to_ip( optarg );
/* get the hwaddr */
if( hwaddr_get( vif->ifname, vif->hwaddr
, sizeof(vif->hwaddr)) ){
fprintf( stderr, "Can't read the hwaddr on this interface!\n" );
goto err;
}
break;
case 'v':
vsrv->vrid = atoi( optarg );
if( VRRP_IS_BAD_VID(vsrv->vrid) ){
fprintf( stderr, "bad vrid!\n" );
goto err;
}
vrrp_hwaddr[0] = 0x00;
vrrp_hwaddr[1] = 0x00;
vrrp_hwaddr[2] = 0x5E;
vrrp_hwaddr[3] = 0x00;
vrrp_hwaddr[4] = 0x01;
vrrp_hwaddr[5] = vsrv->vrid;
break;
case 'a':
if( parse_authopt( vsrv, optarg ) ){
fprintf( stderr, "Invalid authentication key!\n" );
goto err;
}
break;
case 'p':
vsrv->priority = atoi( optarg );
if( VRRP_IS_BAD_PRIORITY(vsrv->priority) ){
fprintf( stderr, "bad priority!\n" );
goto err;
}
break;