This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 258
/
eucanetd.c
2070 lines (1866 loc) · 66.8 KB
/
eucanetd.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
// -*- mode: C; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
// vim: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
/*************************************************************************
* Copyright 2009-2012 Eucalyptus Systems, Inc.
*
* 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; version 3 of the License.
*
* 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, see http://www.gnu.org/licenses/.
*
* Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta
* CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need
* additional information or have any questions.
*
* This file may incorporate work covered under the following copyright
* and permission notice:
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Regents of the University of California
* All rights reserved.
*
* Redistribution and use of this software in source and binary forms,
* with or without modification, are permitted provided that the
* following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. USERS OF THIS SOFTWARE ACKNOWLEDGE
* THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE LICENSED MATERIAL,
* COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS SOFTWARE,
* AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
* IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA,
* SANTA BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY,
* WHICH IN THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION,
* REPLACEMENT OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO
* IDENTIFIED, OR WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT
* NEEDED TO COMPLY WITH ANY SUCH LICENSES OR RIGHTS.
************************************************************************/
//!
//! @file util/template.c
//! Template source file
//!
/*----------------------------------------------------------------------------*\
| |
| INCLUDES |
| |
\*----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <dirent.h>
#include <errno.h>
#include <eucalyptus.h>
#include <misc.h>
#include <vnetwork.h>
#include <euca_string.h>
#include <log.h>
#include <hash.h>
#include <math.h>
#include <http.h>
#include <config.h>
#include <sequence_executor.h>
#include <ipt_handler.h>
#include <atomic_file.h>
#include "eucanetd.h"
#include "config-eucanetd.h"
/*----------------------------------------------------------------------------*\
| |
| DEFINES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| TYPEDEFS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| ENUMERATIONS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STRUCTURES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| EXTERNAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/* Should preferably be handled in header file */
/*----------------------------------------------------------------------------*\
| |
| GLOBAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
vnetConfig *vnetconfig = NULL;
eucanetdConfig *config = NULL;
configEntry configKeysRestartEUCANETD[] = {
{"EUCALYPTUS", "/"}
,
{"VNET_BRIDGE", NULL}
,
{"VNET_BROADCAST", NULL}
,
{"VNET_DHCPDAEMON", "/usr/sbin/dhcpd41"}
,
{"VNET_DHCPUSER", "root"}
,
{"VNET_DNS", NULL}
,
{"VNET_DOMAINNAME", "eucalyptus.internal"}
,
{"VNET_MODE", "EDGE"}
,
{"VNET_NETMASK", NULL}
,
{"VNET_PRIVINTERFACE", NULL}
,
{"VNET_PUBINTERFACE", NULL}
,
{"VNET_PUBLICIPS", NULL}
,
{"VNET_PRIVATEIPS", NULL}
,
{"VNET_ROUTER", NULL}
,
{"VNET_SUBNET", NULL}
,
{"VNET_MACPREFIX", "d0:0d"}
,
{"EUCA_USER", "eucalyptus"}
,
{NULL, NULL}
,
};
configEntry configKeysNoRestartEUCANETD[] = {
{"CC_POLLING_FREQUENCY", "5"}
,
{"LOGLEVEL", "INFO"}
,
{"LOGROLLNUMBER", "10"}
,
{"LOGMAXSIZE", "104857600"}
,
{"LOGPREFIX", ""}
,
{"LOGFACILITY", ""}
,
{NULL, NULL}
,
};
/*----------------------------------------------------------------------------*\
| |
| STATIC VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STATIC PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| MACROS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| IMPLEMENTATION |
| |
\*----------------------------------------------------------------------------*/
//!
//! Function description.
//!
//! @param[in] argc
//! @param[in] argv
//!
//! @return
//!
//! @see
//!
//! @pre List of pre-conditions
//!
//! @post List of post conditions
//!
//! @note
//!
int main(int argc, char **argv)
{
int rc = 0, opt = 0, debug = 0, firstrun = 1, counter = 0;
int epoch_updates = 0, epoch_failed_updates = 0;
int update_localnet_failed = 0, update_networktopo_failed = 0, update_cc_config_failed = 0, update_clcip_failed = 0;
int update_localnet = 0, update_networktopo = 0, update_cc_config = 0, update_clcip = 0, i;
time_t epoch_timer = 0;
// initialize
eucanetdInit();
// parse commandline arguments
while ((opt = getopt(argc, argv, "s:dh")) != -1) {
switch (opt) {
case 'd':
config->debug = 1;
break;
case 's':
config->ccIp = strdup(optarg);
config->cc_cmdline_override = 1;
break;
case 'h':
printf("USAGE: %s OPTIONS\n %-12s| override automatic detection of CC IP address with <ccIp>\n %-12s| debug - run eucanetd in foreground, all output to terminal\n",
argv[0], "-s <ccIp>", "-d");
exit(1);
break;
default:
printf("USAGE: %s OPTIONS\n %-12s| override automatic detection of CC IP address with <ccIp>\n %-12s| debug - run eucanetd in foreground, all output to terminal\n",
argv[0], "-s <ccIp>", "-d");
exit(1);
break;
}
}
// initialize vnetconfig from local eucalyptus.conf and remote (CC) dynamic config; spin looking for config from CC until one is available
vnetconfig = malloc(sizeof(vnetConfig));
if (!vnetconfig) {
LOGFATAL("out of memory!\n");
exit(1);
}
bzero(vnetconfig, sizeof(vnetConfig));
// need just enough config to initialize things and set up logging subsystem
rc = read_config_bootstrap();
if (rc) {
fprintf(stderr, "could not read enough config to bootstrap eucanetd, exiting\n");
exit(1);
}
// daemonize!
rc = daemonize();
if (rc) {
fprintf(stderr, "failed to daemonize eucanetd, exiting\n");
exit(1);
}
// spin here until we get the latest config from active CC
rc = 1;
while (rc) {
rc = read_config();
if (rc) {
LOGWARN("cannot complete pre-flight checks, retrying\n");
sleep(1);
}
}
// got all config, enter main loop
// while(counter<50000) {
while (1) {
update_localnet = update_networktopo = update_cc_config = update_clcip = 0;
counter++;
// fetch all latest networking information from various sources
rc = fetch_latest_network(&update_clcip, &update_networktopo, &update_cc_config, &update_localnet);
if (rc) {
LOGWARN("one or more fetches for latest network information was unsucessful\n");
}
// first time we run, force an update
if (firstrun) {
update_networktopo = update_localnet = update_cc_config = update_clcip = 1;
firstrun = 0;
}
// if the last update operations failed, regardless of new info, force an update
LOGDEBUG("failed bits 1: update_clcip_failed=%d update_networktopo_failed=%d update_localnet_failed=%d\n", update_clcip_failed, update_networktopo_failed,
update_localnet_failed);
if (update_clcip_failed)
update_clcip = 1;
if (update_networktopo_failed)
update_networktopo = 1;
if (update_localnet_failed)
update_localnet = 1;
// whether or not updates have occurred due to remote content being updated, read local networking info
rc = read_latest_network();
if (rc) {
LOGWARN("read_latest_network failed, skipping update\n");
// if the local read failed for some reason, skip any attempt to update (leave current state in place)
update_localnet = update_networktopo = update_cc_config = update_clcip = 0;
}
// now, preform any updates that are required
if (update_clcip) {
LOGINFO("new networking state (CLC IP metadata service): updating system\n");
// update metadata redirect rule
update_clcip_failed = 0;
rc = update_metadata_redirect();
if (rc) {
LOGERROR("could not update metadata redirect rule\n");
update_clcip_failed = 1;
}
}
// if information on sec. group rules/membership has changed, apply
if (update_networktopo || update_localnet) {
LOGINFO("new networking state (network topology/security groups): updating system\n");
update_networktopo_failed = 0;
// install iptables FW rules, using IPsets for sec. group
rc = update_sec_groups();
if (rc) {
LOGERROR("could not complete update of security groups\n");
update_networktopo_failed = 1;
}
}
// if information about local VM network config has changed, apply
if (update_networktopo || update_localnet) {
LOGINFO("new networking state (VM public/private network addresses): updating system\n");
update_localnet_failed = 0;
// update list of private IPs, handle DHCP daemon re-configure and restart
rc = update_private_ips();
if (rc) {
LOGERROR("could not complete update of private IPs\n");
update_localnet_failed = 1;
}
// update public IP assignment and NAT table entries
rc = update_public_ips();
if (rc) {
LOGERROR("could not complete update of public IPs\n");
update_localnet_failed = 1;
}
// install ebtables rules for isolation
rc = update_isolation_rules();
if (rc) {
LOGERROR("could not complete update of VM network isolation rules\n");
update_localnet_failed = 1;
}
}
if (update_localnet || update_networktopo || update_clcip) {
if (update_clcip_failed || update_localnet_failed || update_networktopo_failed) {
epoch_failed_updates++;
} else {
epoch_updates++;
}
}
// temporary exit after one iteration
// exit(0);
if (epoch_timer >= 300) {
LOGINFO("eucanetd has performed %d successful updates and %d failed updates during this %f minute duty cycle\n", epoch_updates, epoch_failed_updates, 10.0 / 60.0);
epoch_updates = epoch_failed_updates = epoch_timer = 0;
}
// do it all over again...
if (update_clcip_failed || update_localnet_failed || update_networktopo_failed) {
LOGDEBUG("main loop complete: failures detected sleeping %d seconds before next poll\n", 1);
sleep(1);
} else {
LOGDEBUG("main loop complete: sleeping %d seconds before next poll\n", config->cc_polling_frequency);
sleep(config->cc_polling_frequency);
}
epoch_timer += config->cc_polling_frequency;
}
exit(0);
}
//!
//! Function description.
//!
//! @return
//!
//! @see
//!
//! @pre List of pre-conditions
//!
//! @post List of post conditions
//!
//! @note
//!
int fetch_latest_localconfig(void)
{
int rc;
if (isConfigModified(config->configFiles, 2) > 0) { // config modification time has changed
if (readConfigFile(config->configFiles, 2)) {
// something has changed that can be read in
LOGINFO("configuration file has been modified, ingressing new options\n");
logInit();
// TODO pick up other NC options dynamically
}
}
return (0);
}
//!
//! daemonize switches user (drop priv), closes FDs, and back-grounds
//!
//! @return
//!
//! @see
//!
//! @pre List of pre-conditions
//!
//! @post List of post conditions
//!
//! @note
//!
int daemonize(void)
{
int pid, sid;
struct passwd *pwent = NULL;
char pidfile[MAX_PATH];
FILE *FH = NULL;
if (!config->debug) {
pid = fork();
if (pid) {
exit(0);
}
sid = setsid();
if (sid < 0) {
perror("daemonize()");
fprintf(stderr, "could not establish a new session id\n");
exit(1);
}
}
pid = getpid();
if (pid > 1) {
snprintf(pidfile, MAX_PATH, "%s/var/run/eucalyptus/eucalyptus-eucanetd.pid", config->eucahome);
FH = fopen(pidfile, "w");
if (FH) {
fprintf(FH, "%d\n", pid);
fclose(FH);
} else {
fprintf(stderr, "could not open pidfile for write (%s)\n", pidfile);
exit(1);
}
}
pwent = getpwnam(config->eucauser);
if (!pwent) {
fprintf(stderr, "could not find UID of configured user '%s'\n", SP(config->eucauser));
exit(1);
}
if (setgid(pwent->pw_gid) || setuid(pwent->pw_uid)) {
perror("setgid() setuid()");
fprintf(stderr, "could not switch daemon process to UID/GID '%d/%d'\n", pwent->pw_uid, pwent->pw_gid);
exit(1);
}
if (!config->debug) {
close(0);
close(1);
close(2);
}
return (0);
}
//!
//! application of EBT rules to only allow unique and known IP<->MAC pairings to send traffice through the bridge
//!
//! @return
//!
//! @see
//!
//! @pre List of pre-conditions
//!
//! @post List of post conditions
//!
//! @note
//!
int update_isolation_rules(void)
{
int rc, ret = 0, i, fd, j, doit;
char cmd[MAX_PATH];
char *strptra = NULL, *strptrb = NULL, *vnetinterface = NULL;
sec_group *group = NULL;
// TODO - check ebtables thoroughly
// this rule clears, but dont understand exactly why: ebtables -I EUCA_EBT_FWD -p IPv4 -i vnet3 --logical-in br0 --ip-src 1.1.0.5 -j ACCEPT
rc = ebt_handler_repopulate(config->ebt);
rc = ebt_table_add_chain(config->ebt, "filter", "EUCA_EBT_FWD", "ACCEPT", "");
rc = ebt_chain_add_rule(config->ebt, "filter", "FORWARD", "-j EUCA_EBT_FWD");
rc = ebt_chain_flush(config->ebt, "filter", "EUCA_EBT_FWD");
// add these for DHCP to pass
rc = ebt_chain_add_rule(config->ebt, "filter", "EUCA_EBT_FWD", "-p IPv4 -d Broadcast --ip-proto udp --ip-dport 67:68 -j ACCEPT");
rc = ebt_chain_add_rule(config->ebt, "filter", "EUCA_EBT_FWD", "-p IPv4 -d Broadcast --ip-proto udp --ip-sport 67:68 -j ACCEPT");
for (i = 0; i < config->max_security_groups; i++) {
group = &(config->security_groups[i]);
for (j = 0; j < group->max_member_ips; j++) {
if (group->member_ips[j] && maczero(group->member_macs[j])) {
strptra = strptrb = NULL;
strptra = hex2dot(group->member_ips[j]);
hex2mac(group->member_macs[j], &strptrb);
vnetinterface = mac2interface(strptrb);
if (strptra && strptrb && vnetinterface) {
snprintf(cmd, MAX_PATH, "-p IPv4 -i %s --logical-in %s --ip-src %s -j ACCEPT", vnetinterface, vnetconfig->bridgedev, strptra);
rc = ebt_chain_add_rule(config->ebt, "filter", "EUCA_EBT_FWD", cmd);
snprintf(cmd, MAX_PATH, "-p IPv4 -s %s -i %s --ip-src ! %s -j DROP", strptrb, vnetinterface, strptra);
rc = ebt_chain_add_rule(config->ebt, "filter", "EUCA_EBT_FWD", cmd);
snprintf(cmd, MAX_PATH, "-p IPv4 -s ! %s -i %s --ip-src %s -j DROP", strptrb, vnetinterface, strptra);
rc = ebt_chain_add_rule(config->ebt, "filter", "EUCA_EBT_FWD", cmd);
} else {
LOGWARN("could not retrieve local vnet interface for instance mac (%s), skipping but will retry\n", SP(strptrb));
ret = 1;
}
EUCA_FREE(vnetinterface);
EUCA_FREE(strptra);
EUCA_FREE(strptrb);
}
}
}
rc = ebt_handler_print(config->ebt);
rc = ebt_handler_deploy(config->ebt);
if (rc) {
LOGERROR("could not install ebtables rules\n");
ret = 1;
}
return (ret);
}
//!
//! handle 169.254.169.254 AWS metadata redirect to the CLC
//!
//! @return
//!
//! @see
//!
//! @pre List of pre-conditions
//!
//! @post List of post conditions
//!
//! @note
//!
int update_metadata_redirect(void)
{
int ret = 0, rc;
char rule[1024];
if (!config->clcIp || !strlen(config->clcIp)) {
LOGWARN("no valid CLC IP has been set yet, skipping metadata redirect update\n");
return (1);
}
if (ipt_handler_repopulate(config->ipt))
return (1);
snprintf(rule, 1024, "-A PREROUTING -d 169.254.169.254/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination %s:8773", config->clcIp);
if (ipt_chain_add_rule(config->ipt, "nat", "PREROUTING", rule))
return (1);
// rc = ipt_handler_print(config->ipt);
rc = ipt_handler_deploy(config->ipt);
if (rc) {
LOGERROR("could not apply new rule (%s)\n", rule);
ret = 1;
}
return (ret);
}
//!
//! initialize eucanetd config
//!
//! @return
//!
//! @see
//!
//! @pre List of pre-conditions
//!
//! @post List of post conditions
//!
//! @note
//!
int eucanetdInit(void)
{
int rc;
if (!config) {
config = malloc(sizeof(eucanetdConfig));
if (!config) {
LOGFATAL("out of memory\n");
exit(1);
}
}
bzero(config, sizeof(eucanetdConfig));
config->cc_polling_frequency = 5;
config->init = 1;
return (0);
}
//!
//! update IPT
//!
//! @return
//!
//! @see
//!
//! @pre List of pre-conditions
//!
//! @post List of post conditions
//!
//! @note
//!
int update_sec_groups(void)
{
int ret = 0, i, rc, j, fd;
char ips_file[MAX_PATH], *strptra = NULL;
char cmd[MAX_PATH], clcmd[MAX_PATH], rule[1024];
FILE *FH = NULL;
sequence_executor cmds;
sec_group *group = NULL;
ret = 0;
// pull in latest IPT state
rc = ipt_handler_repopulate(config->ipt);
if (rc) {
LOGERROR("cannot read current IPT rules\n");
return (1);
}
// pull in latest IPS state
rc = ips_handler_repopulate(config->ips);
if (rc) {
LOGERROR("cannot read current IPS sets\n");
return (1);
}
// make sure euca chains are in place
rc = ipt_table_add_chain(config->ipt, "filter", "EUCA_FILTER_FWD", "-", "[0:0]");
rc = ipt_table_add_chain(config->ipt, "filter", "EUCA_COUNTERS_IN", "-", "[0:0]");
rc = ipt_table_add_chain(config->ipt, "filter", "EUCA_COUNTERS_OUT", "-", "[0:0]");
rc = ipt_chain_add_rule(config->ipt, "filter", "FORWARD", "-A FORWARD -j EUCA_FILTER_FWD");
// clear all chains that we're about to (re)populate with latest network metadata
rc = ipt_table_deletechainmatch(config->ipt, "filter", "EU_");
rc = ipt_chain_flush(config->ipt, "filter", "EUCA_FILTER_FWD");
rc = ipt_chain_add_rule(config->ipt, "filter", "EUCA_FILTER_FWD", "-A EUCA_FILTER_FWD -j EUCA_COUNTERS_IN");
rc = ipt_chain_add_rule(config->ipt, "filter", "EUCA_FILTER_FWD", "-A EUCA_FILTER_FWD -j EUCA_COUNTERS_OUT");
rc = ipt_chain_add_rule(config->ipt, "filter", "EUCA_FILTER_FWD", "-A EUCA_FILTER_FWD -m conntrack --ctstate ESTABLISHED -j ACCEPT");
rc = ips_handler_deletesetmatch(config->ips, "EU_");
// add chains/rules
for (i = 0; i < config->max_security_groups; i++) {
group = &(config->security_groups[i]);
rule[0] = '\0';
ips_handler_add_set(config->ips, group->chainname);
ips_set_flush(config->ips, group->chainname);
strptra = hex2dot(config->defaultgw);
ips_set_add_ip(config->ips, group->chainname, strptra);
EUCA_FREE(strptra);
for (j = 0; j < group->max_member_ips; j++) {
if (group->member_ips[j]) {
strptra = hex2dot(group->member_ips[j]);
ips_set_add_ip(config->ips, group->chainname, strptra);
EUCA_FREE(strptra);
}
}
// add forward chain
ipt_table_add_chain(config->ipt, "filter", group->chainname, "-", "[0:0]");
ipt_chain_flush(config->ipt, "filter", group->chainname);
// add jump rule
snprintf(rule, 1024, "-A EUCA_FILTER_FWD -m set --match-set %s dst -j %s", group->chainname, group->chainname);
ipt_chain_add_rule(config->ipt, "filter", "EUCA_FILTER_FWD", rule);
// populate forward chain
// this one needs to be first
snprintf(rule, 1024, "-A %s -m set --match-set %s src,dst -j ACCEPT", group->chainname, group->chainname);
ipt_chain_add_rule(config->ipt, "filter", group->chainname, rule);
// make sure conntrack rule is in place
snprintf(rule, 1024, "-A %s -m conntrack --ctstate ESTABLISHED -j ACCEPT", group->chainname);
ipt_chain_add_rule(config->ipt, "filter", group->chainname, rule);
// then put all the group specific IPT rules (temporary one here)
if (group->max_grouprules) {
for (j = 0; j < group->max_grouprules; j++) {
snprintf(rule, 1024, "-A %s %s -j ACCEPT", group->chainname, group->grouprules[j]);
ipt_chain_add_rule(config->ipt, "filter", group->chainname, rule);
}
}
// this ones needs to be last
snprintf(rule, 1024, "-A %s -j DROP", group->chainname);
ipt_chain_add_rule(config->ipt, "filter", group->chainname, rule);
}
if (1 || !ret) {
ips_handler_print(config->ips);
rc = ips_handler_deploy(config->ips, 0);
if (rc) {
LOGERROR("could not apply ipsets\n");
ret = 1;
}
}
if (1 || !ret) {
ipt_handler_print(config->ipt);
rc = ipt_handler_deploy(config->ipt);
if (rc) {
LOGERROR("could not apply new rules\n");
ret = 1;
}
}
if (1 || !ret) {
ips_handler_print(config->ips);
rc = ips_handler_deploy(config->ips, 1);
if (rc) {
LOGERROR("could not apply ipsets\n");
ret = 1;
}
}
return (ret);
}
//!
//! Function description.
//!
//! @param[in] groups
//! @param[in] max_groups
//! @param[in] privip
//! @param[in] outfoundidx
//!
//! @return
//!
//! @see
//!
//! @pre List of pre-conditions
//!
//! @post List of post conditions
//!
//! @note
//!
sec_group *find_sec_group_bypriv(sec_group * groups, int max_groups, u32 privip, int *outfoundidx)
{
int i, j, rc = 0, found = 0, foundgidx = 0, foundipidx = 0;
if (!groups || max_groups <= 0 || !privip) {
return (NULL);
}
for (i = 0; i < max_groups && !found; i++) {
for (j = 0; j < groups[i].max_member_ips && !found; j++) {
if (groups[i].member_ips[j] == privip) {
foundgidx = i;
foundipidx = j;
found++;
}
}
}
if (found) {
if (outfoundidx) {
*outfoundidx = foundipidx;
}
return (&(groups[foundgidx]));
}
return (NULL);
}
//!
//! Function description.
//!
//! @param[in] groups
//! @param[in] max_groups
//! @param[in] pubip
//! @param[in] outfoundidx
//!
//! @return
//!
//! @see
//!
//! @pre List of pre-conditions
//!
//! @post List of post conditions
//!
//! @note
//!
sec_group *find_sec_group_bypub(sec_group * groups, int max_groups, u32 pubip, int *outfoundidx)
{
int i, j, rc = 0, found = 0, foundgidx = 0, foundipidx = 0;
if (!groups || max_groups <= 0 || !pubip) {
return (NULL);
}
for (i = 0; i < max_groups && !found; i++) {
for (j = 0; j < groups[i].max_member_ips && !found; j++) {
if (groups[i].member_public_ips[j] == pubip) {
foundgidx = i;
foundipidx = j;
found++;
}
}
}
if (found) {
if (outfoundidx) {
*outfoundidx = foundipidx;
}
return (&(groups[foundgidx]));
}
return (NULL);
}
//!
//! Function description.
//!
//! @return
//!
//! @see
//!
//! @pre List of pre-conditions
//!
//! @post List of post conditions
//!
//! @note
//!
int update_public_ips(void)
{
int slashnet = 0, ret = 0, rc = 0, i = 0, j = 0, foundidx = 0, doit = 0;
char cmd[MAX_PATH], clcmd[MAX_PATH], rule[1024];
char *strptra = NULL, *strptrb = NULL;
sequence_executor cmds;
sec_group *group = NULL;
slashnet = 32 - ((int)(log2((double)((0xFFFFFFFF - vnetconfig->networks[0].nm) + 1))));
// install EL IP addrs and NAT rules
rc = ipt_handler_repopulate(config->ipt);
ipt_table_add_chain(config->ipt, "nat", "EUCA_NAT_PRE", "-", "[0:0]");
ipt_table_add_chain(config->ipt, "nat", "EUCA_NAT_POST", "-", "[0:0]");
ipt_table_add_chain(config->ipt, "nat", "EUCA_NAT_OUT", "-", "[0:0]");
ipt_chain_add_rule(config->ipt, "nat", "PREROUTING", "-A PREROUTING -j EUCA_NAT_PRE");
ipt_chain_add_rule(config->ipt, "nat", "POSTROUTING", "-A POSTROUTING -j EUCA_NAT_POST");
ipt_chain_add_rule(config->ipt, "nat", "OUTPUT", "-A OUTPUT -j EUCA_NAT_OUT");
// rc = ipt_handler_print(config->ipt);
rc = ipt_handler_deploy(config->ipt);
if (rc) {
LOGERROR("could not add euca net chains\n");
ret = 1;
}
rc = ipt_handler_repopulate(config->ipt);
rc = se_init(&cmds, config->cmdprefix, 2, 1);
ipt_chain_flush(config->ipt, "nat", "EUCA_NAT_PRE");
ipt_chain_flush(config->ipt, "nat", "EUCA_NAT_POST");
ipt_chain_flush(config->ipt, "nat", "EUCA_NAT_OUT");
strptra = hex2dot(vnetconfig->networks[0].nw);
snprintf(rule, 1024, "-A EUCA_NAT_PRE -s %s/%d -d %s/%d -j MARK --set-xmark 0x2a/0xffffffff", strptra, slashnet, strptra, slashnet);
ipt_chain_add_rule(config->ipt, "nat", "EUCA_NAT_PRE", rule);
snprintf(rule, 1024, "-A EUCA_COUNTERS_IN -d %s/%d", strptra, slashnet);
ipt_chain_add_rule(config->ipt, "filter", "EUCA_COUNTERS_IN", rule);
snprintf(rule, 1024, "-A EUCA_COUNTERS_OUT -s %s/%d", strptra, slashnet);
ipt_chain_add_rule(config->ipt, "filter", "EUCA_COUNTERS_OUT", rule);
EUCA_FREE(strptra);
for (i = 0; i < config->max_security_groups; i++) {
group = &(config->security_groups[i]);
for (j = 0; j < group->max_member_ips; j++) {
strptra = hex2dot(group->member_public_ips[j]);
strptrb = hex2dot(group->member_ips[j]);
if ((group->member_public_ips[j] && group->member_ips[j]) && (group->member_public_ips[j] != group->member_ips[j])) {
snprintf(cmd, MAX_PATH, "ip addr add %s/%d dev %s >/dev/null 2>&1", strptra, 32, vnetconfig->pubInterface);
rc = se_add(&cmds, cmd, NULL, ignore_exit2);
snprintf(rule, 1024, "-A EUCA_NAT_PRE -d %s/32 -j DNAT --to-destination %s", strptra, strptrb);
rc = ipt_chain_add_rule(config->ipt, "nat", "EUCA_NAT_PRE", rule);
snprintf(rule, 1024, "-A EUCA_NAT_OUT -d %s/32 -j DNAT --to-destination %s", strptra, strptrb);
rc = ipt_chain_add_rule(config->ipt, "nat", "EUCA_NAT_OUT", rule);
snprintf(rule, 1024, "-A EUCA_NAT_POST -s %s/32 -m mark ! --mark 0x2a -j SNAT --to-source %s", strptrb, strptra);
rc = ipt_chain_add_rule(config->ipt, "nat", "EUCA_NAT_POST", rule);
//snprintf(rule, 1024, "-A EUCA_COUNTERS_IN -m conntrack --ctstate DNAT --ctorigdst %s/32", strptra);
snprintf(rule, 1024, "-A EUCA_COUNTERS_IN -d %s/32", strptrb);
rc = ipt_chain_add_rule(config->ipt, "filter", "EUCA_COUNTERS_IN", rule);
//snprintf(rule, 1024, "-A EUCA_COUNTERS_OUT -m conntrack --ctstate SNAT --ctrepldst %s/32", strptra);
snprintf(rule, 1024, "-A EUCA_COUNTERS_OUT -s %s/32", strptrb);
rc = ipt_chain_add_rule(config->ipt, "filter", "EUCA_COUNTERS_OUT", rule);
// actually added some stuff to do
doit++;
}
EUCA_FREE(strptra);
EUCA_FREE(strptrb);
}
}
if (doit) {
se_print(&cmds);
rc = se_execute(&cmds);
if (rc) {
LOGERROR("could not execute command sequence 1\n");
ret = 1;
}
se_free(&cmds);
}
// rc = ipt_handler_print(config->ipt);
rc = ipt_handler_deploy(config->ipt);
if (rc) {
LOGERROR("could not apply new rules\n");
ret = 1;
}
// if all has gone well, now clear any public IPs that have not been mapped to private IPs
if (!ret) {
se_init(&cmds, config->cmdprefix, 2, 1);
for (i = 0; i < config->max_all_public_ips; i++) {
group = find_sec_group_bypub(config->security_groups, config->max_security_groups, config->all_public_ips[i], &foundidx);
if (!group) {
strptra = hex2dot(config->all_public_ips[i]);
snprintf(cmd, MAX_PATH, "ip addr del %s/%d dev %s >/dev/null 2>&1", strptra, 32, vnetconfig->pubInterface);
rc = se_add(&cmds, cmd, NULL, ignore_exit2);
EUCA_FREE(strptra);
}
}
se_print(&cmds);
rc = se_execute(&cmds);
if (rc) {
LOGERROR("could not execute command sequence 2\n");
ret = 1;
}
se_free(&cmds);
}
return (ret);
}
//!
//! Function description.
//!
//! @return