forked from formorer/pkg-ipvsadm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipvsadm.c
2030 lines (1797 loc) · 56.5 KB
/
ipvsadm.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
/*
* ipvsadm - IP Virtual Server ADMinistration program
* for IPVS NetFilter Module in kernel 2.4
*
* Version: $Id$
*
* Authors: Wensong Zhang <[email protected]>
* Peter Kese <[email protected]>
*
* This program is based on ippfvsadm.
*
* Changes:
* Wensong Zhang : added the editting service & destination support
* Wensong Zhang : added the feature to specify persistent port
* Jacob Rief : found the bug that masquerading dest of
* different vport and dport cannot be deleted.
* Wensong Zhang : fixed it and changed some cosmetic things
* Wensong Zhang : added the timeout setting for persistent service
* Wensong Zhang : added specifying the dest weight zero
* Wensong Zhang : fixed the -E and -e options
* Wensong Zhang : added the long options
* Wensong Zhang : added the hostname and portname input
* Wensong Zhang : added the hostname and portname output
* Lars Marowsky-Brée : added persistence granularity support
* Julian Anastasov : fixed the (null) print for unknown services
* Wensong Zhang : added the port_to_anyname function
* Horms : added option to read commands from stdin
* Horms : modified usage function so it prints to
* : stdout if an exit value of 0 is used and
* : stdout otherwise. Program is then terminated
* : with the supplied exit value.
* Horms : updated manpage and usage funtion so
* : the reflect the options available
* Wensong Zhang : added option to write rules to stdout
* Horms : added ability to specify a fwmark
* : instead of a server and port for
* : a virtual service
* Horms : tightened up checking of services
* : in parse_service
* Horms : ensure that a -r is passed when needed
* Wensong Zhang : fixed the output of fwmark rules
* Horms : added kernel version verification
* Horms : Specifying command and option options
* (e.g. -Ln or -At) in one short option
* with popt problem fixed.
* Wensong Zhang : split the process_options and make
* two versions of parse_options.
* Horms : attempting to save or restore when
* compiled against getopt_long now results
* in an informative error message rather
* than the usage information
* Horms : added -v option
* Wensong Zhang : rewrite most code of parsing options and
* processing options.
* Alexandre Cassen : added ipvs_syncd SyncdID support to filter
* incoming sync messages.
* Guy Waugh & Ratz : added --exact option and spelling cleanup
* [email protected] : added IPv6 support
*
*
* ippfvsadm - Port Fowarding & Virtual Server ADMinistration program
*
* Copyright (c) 1998 Wensong Zhang
* All rights reserved.
*
* Author: Wensong Zhang <[email protected]>
*
* This ippfvsadm is derived from Steven Clarke's ipportfw program.
*
* portfw - Port Forwarding Table Editing v1.1
*
* Copyright (c) 1997 Steven Clarke
* All rights reserved.
*
* Author: Steven Clarke <[email protected]>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#undef __KERNEL__ /* Makefile lazyness ;) */
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <stdarg.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/wait.h> /* For waitpid */
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/ip_icmp.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include "popt.h"
#define IPVS_OPTION_PROCESSING "popt"
#include "config_stream.h"
#include "libipvs/libipvs.h"
#define IPVSADM_VERSION_NO "v" VERSION
#define IPVSADM_VERSION_DATE "2016/12/23"
#define IPVSADM_VERSION IPVSADM_VERSION_NO " " IPVSADM_VERSION_DATE
#define MAX_TIMEOUT (86400*31) /* 31 days */
#define CMD_NONE 0
#define CMD_ADD (CMD_NONE+1)
#define CMD_EDIT (CMD_NONE+2)
#define CMD_DEL (CMD_NONE+3)
#define CMD_FLUSH (CMD_NONE+4)
#define CMD_LIST (CMD_NONE+5)
#define CMD_ADDDEST (CMD_NONE+6)
#define CMD_DELDEST (CMD_NONE+7)
#define CMD_EDITDEST (CMD_NONE+8)
#define CMD_TIMEOUT (CMD_NONE+9)
#define CMD_STARTDAEMON (CMD_NONE+10)
#define CMD_STOPDAEMON (CMD_NONE+11)
#define CMD_RESTORE (CMD_NONE+12)
#define CMD_SAVE (CMD_NONE+13)
#define CMD_ZERO (CMD_NONE+14)
#define CMD_MAX CMD_ZERO
#define NUMBER_OF_CMD (CMD_MAX - CMD_NONE)
static const char* cmdnames[] = {
"add-service",
"edit-service",
"delete-service",
"flush",
"list",
"add-server",
"delete-server",
"edit-server",
"set",
"start-daemon",
"stop-daemon",
"restore",
"save",
"zero",
};
#define OPT_NONE 0x000000
#define OPT_NUMERIC 0x000001
#define OPT_CONNECTION 0x000002
#define OPT_SERVICE 0x000004
#define OPT_SCHEDULER 0x000008
#define OPT_PERSISTENT 0x000010
#define OPT_NETMASK 0x000020
#define OPT_SERVER 0x000040
#define OPT_FORWARD 0x000080
#define OPT_WEIGHT 0x000100
#define OPT_UTHRESHOLD 0x000200
#define OPT_LTHRESHOLD 0x000400
#define OPT_MCAST 0x000800
#define OPT_TIMEOUT 0x001000
#define OPT_DAEMON 0x002000
#define OPT_STATS 0x004000
#define OPT_RATE 0x008000
#define OPT_THRESHOLDS 0x010000
#define OPT_PERSISTENTCONN 0x020000
#define OPT_NOSORT 0x040000
#define OPT_SYNCID 0x080000
#define OPT_EXACT 0x100000
#define OPT_ONEPACKET 0x200000
#define OPT_PERSISTENCE_ENGINE 0x400000
#define OPT_SCHED_FLAGS 0x800000
#define OPT_MCAST_GROUP 0x01000000
#define OPT_MCAST_PORT 0x02000000
#define OPT_MCAST_TTL 0x04000000
#define OPT_SYNC_MAXLEN 0x08000000
#define NUMBER_OF_OPT 28
static const char* optnames[] = {
"numeric",
"connection",
"service-address",
"scheduler",
"persistent",
"netmask",
"real-server",
"forwarding-method",
"weight",
"u-threshold",
"l-threshold",
"mcast-interface",
"timeout",
"daemon",
"stats",
"rate",
"thresholds",
"persistent-conn",
"nosort",
"syncid",
"exact",
"ops",
"pe",
"sched-flags",
"mcast-group",
"mcast-port",
"mcast-ttl",
"sync-maxlen",
};
/*
* Table of legal combinations of commands and options.
* Key:
* '+' compulsory
* 'x' illegal
* '1' exclusive (only one '1' option can be supplied)
* ' ' optional
*/
static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
{
/* -n -c svc -s -p -M -r fwd -w -x -y -mc tot dmn -st -rt thr -pc srt sid -ex ops -pe -b grp port ttl size */
/*ADD*/ {'x', 'x', '+', ' ', ' ', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', ' ', ' ', ' ', 'x', 'x', 'x', 'x'},
/*EDIT*/ {'x', 'x', '+', ' ', ' ', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', ' ', ' ', ' ', 'x', 'x', 'x', 'x'},
/*DEL*/ {'x', 'x', '+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*FLUSH*/ {'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*LIST*/ {' ', '1', '1', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', '1', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*ADDSRV*/ {'x', 'x', '+', 'x', 'x', 'x', '+', ' ', ' ', ' ', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*DELSRV*/ {'x', 'x', '+', 'x', 'x', 'x', '+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*EDITSRV*/ {'x', 'x', '+', 'x', 'x', 'x', '+', ' ', ' ', ' ', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*TIMEOUT*/ {'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*STARTD*/ {'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', ' ', 'x', 'x', 'x', 'x', ' ', ' ', ' ', ' '},
/*STOPD*/ {'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*RESTORE*/ {'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*SAVE*/ {' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*ZERO*/ {'x', 'x', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
};
/* printing format flags */
#define FMT_NONE 0x0000
#define FMT_NUMERIC 0x0001
#define FMT_RULE 0x0002
#define FMT_STATS 0x0004
#define FMT_RATE 0x0008
#define FMT_THRESHOLDS 0x0010
#define FMT_PERSISTENTCONN 0x0020
#define FMT_NOSORT 0x0040
#define FMT_EXACT 0x0080
#define SERVICE_NONE 0x0000
#define SERVICE_ADDR 0x0001
#define SERVICE_PORT 0x0002
/* default scheduler */
#define DEF_SCHED "wlc"
/* default multicast interface name */
#define DEF_MCAST_IFN "eth0"
#define CONN_PROC_FILE "/proc/net/ip_vs_conn"
struct ipvs_command_entry {
int cmd;
ipvs_service_t svc;
ipvs_dest_t dest;
ipvs_timeout_t timeout;
ipvs_daemon_t daemon;
};
/* Use values outside ASCII range so that if an option has
* a short name it can be used as the tag
*/
enum {
TAG_SET = 128,
TAG_START_DAEMON,
TAG_STOP_DAEMON ,
TAG_MCAST_INTERFACE,
TAG_TIMEOUT,
TAG_DAEMON,
TAG_STATS,
TAG_RATE,
TAG_THRESHOLDS,
TAG_PERSISTENTCONN,
TAG_SORT,
TAG_NO_SORT,
TAG_PERSISTENCE_ENGINE,
TAG_SCTP_SERVICE,
TAG_MCAST_GROUP,
TAG_MCAST_PORT,
TAG_MCAST_TTL,
TAG_SYNC_MAXLEN,
};
/* various parsing helpers & parsing functions */
static int str_is_digit(const char *str);
static int string_to_number(const char *s, int min, int max);
static int host_to_addr(const char *name, struct in_addr *addr);
static char * addr_to_host(int af, const void *addr);
static char * addr_to_anyname(int af, const void *addr);
static int service_to_port(const char *name, unsigned short proto);
static char * port_to_service(unsigned short port, unsigned short proto);
static char * port_to_anyname(unsigned short port, unsigned short proto);
static char * addrport_to_anyname(int af, const void *addr, unsigned short port,
unsigned short proto, unsigned int format);
static int parse_service(char *buf, ipvs_service_t *svc);
static int parse_netmask(char *buf, u_int32_t *addr);
static int parse_timeout(char *buf, int min, int max);
static unsigned int parse_fwmark(char *buf);
static unsigned int parse_sched_flags(const char *sched, char *optarg);
/* check the options based on the commands_v_options table */
static void generic_opt_check(int command, int options);
static void set_command(int *cmd, const int newcmd);
static void set_option(unsigned int *options, unsigned int option);
static void tryhelp_exit(const char *program, const int exit_status);
static void usage_exit(const char *program, const int exit_status);
static void version_exit(int exit_status);
static void version(FILE *stream);
static void fail(int err, char *msg, ...);
/* various listing functions */
static void list_conn(unsigned int format);
static void list_service(ipvs_service_t *svc, unsigned int format);
static void list_all(unsigned int format);
static void list_timeout(void);
static void list_daemon(void);
static int modprobe_ipvs(void);
static void check_ipvs_version(void);
static int process_options(int argc, char **argv, int reading_stdin);
int main(int argc, char **argv)
{
int result;
if (ipvs_init()) {
/* try to insmod the ip_vs module if ipvs_init failed */
if (modprobe_ipvs() || ipvs_init())
fail(2, "Can't initialize ipvs: %s\n"
"Are you sure that IP Virtual Server is "
"built in the kernel or as module?",
ipvs_strerror(errno));
}
/* warn the user if the IPVS version is out of date */
check_ipvs_version();
/* list the table if there is no other arguement */
if (argc == 1){
list_all(FMT_NONE);
ipvs_close();
return 0;
}
/* process command line arguments */
result = process_options(argc, argv, 0);
ipvs_close();
return result;
}
static int option_to_protocol(int opt)
{
switch (opt) {
case 't':
return IPPROTO_TCP;
case 'u':
return IPPROTO_UDP;
case TAG_SCTP_SERVICE:
return IPPROTO_SCTP;
default:
return IPPROTO_IP;
}
}
static char *option_from_protocol(int proto)
{
switch (proto) {
case IPPROTO_TCP:
return "-t";
case IPPROTO_UDP:
return "-u";
case IPPROTO_SCTP:
return "--sctp-service";
default:
return NULL;
}
}
static char *protocol_name(int proto)
{
switch (proto) {
case IPPROTO_TCP:
return "TCP";
case IPPROTO_UDP:
return "UDP";
case IPPROTO_SCTP:
return "SCTP";
default:
return "?";
}
}
static int
parse_options(int argc, char **argv, struct ipvs_command_entry *ce,
unsigned int *options, unsigned int *format)
{
int c, parse;
poptContext context;
char *optarg = NULL, sched_flags_arg[128];
struct poptOption options_table[] = {
{ "add-service", 'A', POPT_ARG_NONE, NULL, 'A', NULL, NULL },
{ "edit-service", 'E', POPT_ARG_NONE, NULL, 'E', NULL, NULL },
{ "delete-service", 'D', POPT_ARG_NONE, NULL, 'D', NULL, NULL },
{ "clear", 'C', POPT_ARG_NONE, NULL, 'C', NULL, NULL },
{ "list", 'L', POPT_ARG_NONE, NULL, 'L', NULL, NULL },
{ "list", 'l', POPT_ARG_NONE, NULL, 'l', NULL, NULL },
{ "zero", 'Z', POPT_ARG_NONE, NULL, 'Z', NULL, NULL },
{ "add-server", 'a', POPT_ARG_NONE, NULL, 'a', NULL, NULL },
{ "edit-server", 'e', POPT_ARG_NONE, NULL, 'e', NULL, NULL },
{ "delete-server", 'd', POPT_ARG_NONE, NULL, 'd', NULL, NULL },
{ "set", '\0', POPT_ARG_NONE, NULL, TAG_SET, NULL, NULL },
{ "help", 'h', POPT_ARG_NONE, NULL, 'h', NULL, NULL },
{ "version", 'v', POPT_ARG_NONE, NULL, 'v', NULL, NULL },
{ "restore", 'R', POPT_ARG_NONE, NULL, 'R', NULL, NULL },
{ "save", 'S', POPT_ARG_NONE, NULL, 'S', NULL, NULL },
{ "start-daemon", '\0', POPT_ARG_STRING, &optarg,
TAG_START_DAEMON, NULL, NULL },
{ "stop-daemon", '\0', POPT_ARG_STRING, &optarg,
TAG_STOP_DAEMON, NULL, NULL },
{ "tcp-service", 't', POPT_ARG_STRING, &optarg, 't',
NULL, NULL },
{ "udp-service", 'u', POPT_ARG_STRING, &optarg, 'u',
NULL, NULL },
{ "sctp-service", '\0', POPT_ARG_STRING, &optarg,
TAG_SCTP_SERVICE, NULL, NULL },
{ "fwmark-service", 'f', POPT_ARG_STRING, &optarg, 'f',
NULL, NULL },
{ "scheduler", 's', POPT_ARG_STRING, &optarg, 's', NULL, NULL },
{ "persistent", 'p', POPT_ARG_STRING|POPT_ARGFLAG_OPTIONAL,
&optarg, 'p', NULL, NULL },
{ "netmask", 'M', POPT_ARG_STRING, &optarg, 'M', NULL, NULL },
{ "real-server", 'r', POPT_ARG_STRING, &optarg, 'r',
NULL, NULL },
{ "masquerading", 'm', POPT_ARG_NONE, NULL, 'm', NULL, NULL },
{ "ipip", 'i', POPT_ARG_NONE, NULL, 'i', NULL, NULL },
{ "gatewaying", 'g', POPT_ARG_NONE, NULL, 'g', NULL, NULL },
{ "weight", 'w', POPT_ARG_STRING, &optarg, 'w', NULL, NULL },
{ "u-threshold", 'x', POPT_ARG_STRING, &optarg, 'x',
NULL, NULL },
{ "l-threshold", 'y', POPT_ARG_STRING, &optarg, 'y',
NULL, NULL },
{ "numeric", 'n', POPT_ARG_NONE, NULL, 'n', NULL, NULL },
{ "connection", 'c', POPT_ARG_NONE, NULL, 'c', NULL, NULL },
{ "mcast-interface", '\0', POPT_ARG_STRING, &optarg,
TAG_MCAST_INTERFACE, NULL, NULL },
{ "syncid", '\0', POPT_ARG_STRING, &optarg, 'I', NULL, NULL },
{ "timeout", '\0', POPT_ARG_NONE, NULL, TAG_TIMEOUT,
NULL, NULL },
{ "daemon", '\0', POPT_ARG_NONE, NULL, TAG_DAEMON, NULL, NULL },
{ "stats", '\0', POPT_ARG_NONE, NULL, TAG_STATS, NULL, NULL },
{ "rate", '\0', POPT_ARG_NONE, NULL, TAG_RATE, NULL, NULL },
{ "thresholds", '\0', POPT_ARG_NONE, NULL,
TAG_THRESHOLDS, NULL, NULL },
{ "persistent-conn", '\0', POPT_ARG_NONE, NULL,
TAG_PERSISTENTCONN, NULL, NULL },
{ "nosort", '\0', POPT_ARG_NONE, NULL,
TAG_NO_SORT, NULL, NULL },
{ "sort", '\0', POPT_ARG_NONE, NULL, TAG_SORT, NULL, NULL },
{ "exact", 'X', POPT_ARG_NONE, NULL, 'X', NULL, NULL },
{ "ipv6", '6', POPT_ARG_NONE, NULL, '6', NULL, NULL },
{ "ops", 'o', POPT_ARG_NONE, NULL, 'o', NULL, NULL },
{ "pe", '\0', POPT_ARG_STRING, &optarg, TAG_PERSISTENCE_ENGINE,
NULL, NULL },
{ "sched-flags", 'b', POPT_ARG_STRING, &optarg, 'b',
NULL, NULL },
{ "mcast-group", '\0', POPT_ARG_STRING, &optarg,
TAG_MCAST_GROUP, NULL, NULL },
{ "mcast-port", '\0', POPT_ARG_STRING, &optarg,
TAG_MCAST_PORT, NULL, NULL },
{ "mcast-ttl", '\0', POPT_ARG_STRING, &optarg,
TAG_MCAST_TTL, NULL, NULL },
{ "sync-maxlen", '\0', POPT_ARG_STRING, &optarg,
TAG_SYNC_MAXLEN, NULL, NULL },
{ NULL, 0, 0, NULL, 0, NULL, NULL }
};
sched_flags_arg[0] = '\0';
context = poptGetContext("ipvsadm", argc, (const char **)argv,
options_table, 0);
if ((c = poptGetNextOpt(context)) < 0)
tryhelp_exit(argv[0], -1);
switch (c) {
case 'A':
set_command(&ce->cmd, CMD_ADD);
break;
case 'E':
set_command(&ce->cmd, CMD_EDIT);
break;
case 'D':
set_command(&ce->cmd, CMD_DEL);
break;
case 'a':
set_command(&ce->cmd, CMD_ADDDEST);
break;
case 'e':
set_command(&ce->cmd, CMD_EDITDEST);
break;
case 'd':
set_command(&ce->cmd, CMD_DELDEST);
break;
case 'C':
set_command(&ce->cmd, CMD_FLUSH);
break;
case 'L':
case 'l':
set_command(&ce->cmd, CMD_LIST);
break;
case 'Z':
set_command(&ce->cmd, CMD_ZERO);
break;
case TAG_SET:
set_command(&ce->cmd, CMD_TIMEOUT);
break;
case 'R':
set_command(&ce->cmd, CMD_RESTORE);
break;
case 'S':
set_command(&ce->cmd, CMD_SAVE);
break;
case TAG_START_DAEMON:
set_command(&ce->cmd, CMD_STARTDAEMON);
if (!strcmp(optarg, "master"))
ce->daemon.state = IP_VS_STATE_MASTER;
else if (!strcmp(optarg, "backup"))
ce->daemon.state = IP_VS_STATE_BACKUP;
else fail(2, "illegal start-daemon parameter specified");
break;
case TAG_STOP_DAEMON:
set_command(&ce->cmd, CMD_STOPDAEMON);
if (!strcmp(optarg, "master"))
ce->daemon.state = IP_VS_STATE_MASTER;
else if (!strcmp(optarg, "backup"))
ce->daemon.state = IP_VS_STATE_BACKUP;
else fail(2, "illegal start_daemon specified");
break;
case 'h':
usage_exit(argv[0], 0);
break;
case 'v':
version_exit(0);
break;
default:
tryhelp_exit(argv[0], -1);
}
while ((c=poptGetNextOpt(context)) >= 0){
switch (c) {
case 't':
case 'u':
case TAG_SCTP_SERVICE:
set_option(options, OPT_SERVICE);
ce->svc.protocol = option_to_protocol(c);
parse = parse_service(optarg, &ce->svc);
if (!(parse & SERVICE_ADDR))
fail(2, "illegal virtual server "
"address[:port] specified");
break;
case 'f':
set_option(options, OPT_SERVICE);
/*
* Set protocol to a sane values, even
* though it is not used
*/
ce->svc.af = AF_INET;
ce->svc.protocol = IPPROTO_TCP;
ce->svc.fwmark = parse_fwmark(optarg);
break;
case 's':
set_option(options, OPT_SCHEDULER);
strncpy(ce->svc.sched_name,
optarg, IP_VS_SCHEDNAME_MAXLEN);
break;
case 'p':
set_option(options, OPT_PERSISTENT);
ce->svc.flags |= IP_VS_SVC_F_PERSISTENT;
ce->svc.timeout =
parse_timeout(optarg, 1, MAX_TIMEOUT);
break;
case 'M':
set_option(options, OPT_NETMASK);
if (ce->svc.af != AF_INET6) {
parse = parse_netmask(optarg, &ce->svc.netmask);
if (parse != 1)
fail(2, "illegal virtual server "
"persistent mask specified");
} else {
ce->svc.netmask = atoi(optarg);
if ((ce->svc.netmask < 1) || (ce->svc.netmask > 128))
fail(2, "illegal ipv6 netmask specified");
}
break;
case 'r':
set_option(options, OPT_SERVER);
ipvs_service_t t_dest = ce->svc;
parse = parse_service(optarg, &t_dest);
ce->dest.af = t_dest.af;
ce->dest.addr = t_dest.addr;
ce->dest.port = t_dest.port;
if (!(parse & SERVICE_ADDR))
fail(2, "illegal real server "
"address[:port] specified");
/* copy vport to dport if not specified */
if (parse == 1)
ce->dest.port = ce->svc.port;
break;
case 'i':
set_option(options, OPT_FORWARD);
ce->dest.conn_flags = IP_VS_CONN_F_TUNNEL;
break;
case 'g':
set_option(options, OPT_FORWARD);
ce->dest.conn_flags = IP_VS_CONN_F_DROUTE;
break;
case 'm':
set_option(options, OPT_FORWARD);
ce->dest.conn_flags = IP_VS_CONN_F_MASQ;
break;
case 'w':
set_option(options, OPT_WEIGHT);
if ((ce->dest.weight =
string_to_number(optarg, 0, 65535)) == -1)
fail(2, "illegal weight specified");
break;
case 'x':
set_option(options, OPT_UTHRESHOLD);
if ((ce->dest.u_threshold =
string_to_number(optarg, 0, INT_MAX)) == -1)
fail(2, "illegal u_threshold specified");
break;
case 'y':
set_option(options, OPT_LTHRESHOLD);
if ((ce->dest.l_threshold =
string_to_number(optarg, 0, INT_MAX)) == -1)
fail(2, "illegal l_threshold specified");
break;
case 'c':
set_option(options, OPT_CONNECTION);
break;
case 'n':
set_option(options, OPT_NUMERIC);
*format |= FMT_NUMERIC;
break;
case TAG_MCAST_INTERFACE:
set_option(options, OPT_MCAST);
strncpy(ce->daemon.mcast_ifn,
optarg, IP_VS_IFNAME_MAXLEN);
break;
case 'I':
set_option(options, OPT_SYNCID);
if ((ce->daemon.syncid =
string_to_number(optarg, 0, 255)) == -1)
fail(2, "illegal syncid specified");
break;
case TAG_TIMEOUT:
set_option(options, OPT_TIMEOUT);
break;
case TAG_DAEMON:
set_option(options, OPT_DAEMON);
break;
case TAG_STATS:
set_option(options, OPT_STATS);
*format |= FMT_STATS;
break;
case TAG_RATE:
set_option(options, OPT_RATE);
*format |= FMT_RATE;
break;
case TAG_THRESHOLDS:
set_option(options, OPT_THRESHOLDS);
*format |= FMT_THRESHOLDS;
break;
case TAG_PERSISTENTCONN:
set_option(options, OPT_PERSISTENTCONN);
*format |= FMT_PERSISTENTCONN;
break;
case TAG_NO_SORT:
set_option(options, OPT_NOSORT );
*format |= FMT_NOSORT;
break;
case TAG_SORT:
/* Sort is the default, this is a no-op for compatibility */
break;
case 'X':
set_option(options, OPT_EXACT);
*format |= FMT_EXACT;
break;
case '6':
if (ce->svc.fwmark) {
ce->svc.af = AF_INET6;
ce->svc.netmask = 128;
} else {
fail(2, "-6 used before -f\n");
}
break;
case 'o':
set_option(options, OPT_ONEPACKET);
ce->svc.flags |= IP_VS_SVC_F_ONEPACKET;
break;
case TAG_PERSISTENCE_ENGINE:
set_option(options, OPT_PERSISTENCE_ENGINE);
strncpy(ce->svc.pe_name, optarg, IP_VS_PENAME_MAXLEN);
break;
case 'b':
set_option(options, OPT_SCHED_FLAGS);
snprintf(sched_flags_arg, sizeof(sched_flags_arg),
"%s", optarg);
break;
case TAG_MCAST_GROUP:
set_option(options, OPT_MCAST_GROUP);
if (strchr(optarg, ':')) {
if (inet_pton(AF_INET6, optarg,
&ce->daemon.mcast_group) <= 0 ||
!IN6_IS_ADDR_MULTICAST(
&ce->daemon.mcast_group.in6))
fail(2, "invalid IPv6 mcast-group `%s'",
optarg);
ce->daemon.mcast_af = AF_INET6;
} else {
if (inet_pton(AF_INET, optarg,
&ce->daemon.mcast_group) <= 0 ||
!IN_MULTICAST(ntohl(
ce->daemon.mcast_group.ip)))
fail(2, "invalid IPv4 mcast-group `%s'",
optarg);
ce->daemon.mcast_af = AF_INET;
}
break;
case TAG_MCAST_PORT:
set_option(options, OPT_MCAST_PORT);
parse = string_to_number(optarg, 1, 65535);
if (parse == -1)
fail(2, "illegal mcast-port specified");
ce->daemon.mcast_port = parse;
break;
case TAG_MCAST_TTL:
set_option(options, OPT_MCAST_TTL);
parse = string_to_number(optarg, 1, 255);
if (parse == -1)
fail(2, "illegal mcast-ttl specified");
ce->daemon.mcast_ttl = parse;
break;
case TAG_SYNC_MAXLEN:
set_option(options, OPT_SYNC_MAXLEN);
parse = string_to_number(optarg, 1, 65535 - 20 - 8);
if (parse == -1)
fail(2, "illegal sync-maxlen specified");
ce->daemon.sync_maxlen = parse;
break;
default:
fail(2, "invalid option `%s'",
poptBadOption(context, POPT_BADOPTION_NOALIAS));
}
}
if (c < -1) {
/* an error occurred during option processing */
fprintf(stderr, "%s: %s\n",
poptBadOption(context, POPT_BADOPTION_NOALIAS),
poptStrerror(c));
poptFreeContext(context);
return -1;
}
if (ce->cmd == CMD_TIMEOUT) {
char *optarg1, *optarg2;
if ((optarg=(char *)poptGetArg(context))
&& (optarg1=(char *)poptGetArg(context))
&& (optarg2=(char *)poptGetArg(context))) {
ce->timeout.tcp_timeout =
parse_timeout(optarg, 0, MAX_TIMEOUT);
ce->timeout.tcp_fin_timeout =
parse_timeout(optarg1, 0, MAX_TIMEOUT);
ce->timeout.udp_timeout =
parse_timeout(optarg2, 0, MAX_TIMEOUT);
} else
fail(2, "--set option requires 3 timeout values");
}
if ((optarg=(char *)poptGetArg(context)))
fail(2, "unexpected argument %s", optarg);
if (sched_flags_arg[0]) {
ce->svc.flags &= ~(IP_VS_SVC_F_SCHED1 |
IP_VS_SVC_F_SCHED2 |
IP_VS_SVC_F_SCHED3);
ce->svc.flags |= parse_sched_flags(ce->svc.sched_name,
sched_flags_arg);
}
poptFreeContext(context);
return 0;
}
static int restore_table(int argc, char **argv, int reading_stdin)
{
int result = 0;
dynamic_array_t *a;
/* avoid infinite loop */
if (reading_stdin != 0)
tryhelp_exit(argv[0], -1);
while ((a = config_stream_read(stdin, argv[0])) != NULL) {
int i;
if ((i = (int)dynamic_array_get_count(a)) > 1) {
char **strv = dynamic_array_get_vector(a);
result = process_options(i, strv, 1);
}
dynamic_array_destroy(a, DESTROY_STR);
}
return result;
}
static int process_options(int argc, char **argv, int reading_stdin)
{
struct ipvs_command_entry ce;
unsigned int options = OPT_NONE;
unsigned int format = FMT_NONE;
int result = 0;
memset(&ce, 0, sizeof(struct ipvs_command_entry));
ce.cmd = CMD_NONE;
/* Set the default weight 1 */
ce.dest.weight = 1;
/* Set direct routing as default forwarding method */
ce.dest.conn_flags = IP_VS_CONN_F_DROUTE;
/* Set the default persistent granularity to /32 mask */
ce.svc.netmask = ((u_int32_t) 0xffffffff);
if (parse_options(argc, argv, &ce, &options, &format))
return -1;
generic_opt_check(ce.cmd, options);
if (ce.cmd == CMD_ADD || ce.cmd == CMD_EDIT) {
/* Make sure that port zero service is persistent */
if (!ce.svc.fwmark && !ce.svc.port &&
!(ce.svc.flags & IP_VS_SVC_F_PERSISTENT))
fail(2, "Zero port specified "
"for non-persistent service");
if (ce.svc.flags & IP_VS_SVC_F_ONEPACKET &&
!ce.svc.fwmark && ce.svc.protocol != IPPROTO_UDP)
fail(2, "One-Packet Scheduling is only "
"for UDP virtual services");
/* Set the default scheduling algorithm if not specified */
if (strlen(ce.svc.sched_name) == 0)
strcpy(ce.svc.sched_name, DEF_SCHED);
}
if (ce.cmd == CMD_STARTDAEMON && strlen(ce.daemon.mcast_ifn) == 0)
strcpy(ce.daemon.mcast_ifn, DEF_MCAST_IFN);
if (ce.cmd == CMD_ADDDEST || ce.cmd == CMD_EDITDEST) {
/*
* The destination port must be equal to the service port
* if the IP_VS_CONN_F_TUNNEL or IP_VS_CONN_F_DROUTE is set.
* Don't worry about this if fwmark is used.
*/
if (!ce.svc.fwmark &&
(ce.dest.conn_flags == IP_VS_CONN_F_TUNNEL
|| ce.dest.conn_flags == IP_VS_CONN_F_DROUTE))
ce.dest.port = ce.svc.port;
/* Tunneling allows different address family */
if (ce.dest.af != ce.svc.af &&
ce.dest.conn_flags != IP_VS_CONN_F_TUNNEL)
fail(2, "Different address family is allowed only "
"for tunneling servers");
}
switch (ce.cmd) {
case CMD_LIST:
if ((options & (OPT_CONNECTION|OPT_TIMEOUT|OPT_DAEMON) &&
options & (OPT_STATS|OPT_RATE|OPT_THRESHOLDS)) ||
(options & (OPT_TIMEOUT|OPT_DAEMON) &&
options & OPT_PERSISTENTCONN))
fail(2, "options conflicts in the list command");
if (options & OPT_CONNECTION)
list_conn(format);
else if (options & OPT_SERVICE)
list_service(&ce.svc, format);
else if (options & OPT_TIMEOUT)
list_timeout();
else if (options & OPT_DAEMON)
list_daemon();
else
list_all(format);
return 0;
case CMD_RESTORE:
return restore_table(argc, argv, reading_stdin);
case CMD_SAVE:
format |= FMT_RULE;
list_all(format);
return 0;
case CMD_FLUSH:
result = ipvs_flush();
break;
case CMD_ADD:
result = ipvs_add_service(&ce.svc);
break;
case CMD_EDIT:
result = ipvs_update_service(&ce.svc);
break;
case CMD_DEL:
result = ipvs_del_service(&ce.svc);
break;
case CMD_ZERO:
result = ipvs_zero_service(&ce.svc);
break;
case CMD_ADDDEST:
result = ipvs_add_dest(&ce.svc, &ce.dest);
break;
case CMD_EDITDEST:
result = ipvs_update_dest(&ce.svc, &ce.dest);
break;
case CMD_DELDEST:
result = ipvs_del_dest(&ce.svc, &ce.dest);
break;
case CMD_TIMEOUT:
result = ipvs_set_timeout(&ce.timeout);
break;
case CMD_STARTDAEMON:
result = ipvs_start_daemon(&ce.daemon);
break;
case CMD_STOPDAEMON:
result = ipvs_stop_daemon(&ce.daemon);
}
if (result)
fprintf(stderr, "%s\n", ipvs_strerror(errno));
return result;
}
static int string_to_number(const char *s, int min, int max)
{
long number;
char *end;
errno = 0;
number = strtol(s, &end, 10);
if (*end == '\0' && end != s) {
/* We parsed a number, let's see if we want this. */
if (errno != ERANGE && min <= number && number <= max)
return number;
}
return -1;
}
/*
* Parse the timeout value.