forked from eucalyptus/eucalyptus
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy patheucanetd_edge.c
2335 lines (2058 loc) · 96.4 KB
/
eucanetd_edge.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 2016 Ent. Services Development Corporation LP
*
* 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.
************************************************************************/
//!
//! @file net/eucanetd_edge.c
//! Implementation of the EDGE Network Driver Interface. This Network Driver
//! Interface implements the driver_handler_t APIs.
//!
//! Coding Standard:
//! Every function that has multiple words must follow the word1_word2_word3() naming
//! convention and variables must follow the 'word1Word2Word3()' convention were no
//! underscore is used and every word, except for the first one, starts with a capitalized
//! letter. Whenever possible (not mendatory but strongly encouraged), prefixing a variable
//! name with one or more of the following qualifier would help reading code:
//! - p - indicates a variable is a pointer (example: int *pAnIntegerPointer)
//! - s - indicates a string variable (examples: char sThisString[10], char *psAnotherString). When 's' is used on its own, this mean a static string.
//! - a - indicates an array of objects (example: int aAnArrayOfInteger[10])
//! - g - indicates a variable with global scope to the file or application (example: static eucanetdConfig gConfig)
//!
//! The network driver APIs must implement the following functions:
//! - network_driver_init()
//! - network_driver_cleanup()
//! - network_driver_system_flush()
//! - network_driver_system_scrub()
//! Optional functions:
//! - network_driver_upgrade()
//! - network_driver_implement_network()
//! - network_driver_implement_sg()
//! - network_driver_implement_addressing()
//! - network_driver_system_maint()
//! - network_driver_handle_signal()
//!
//! Any other function implemented within the scope of this network driver must have its name
//! start with the mode name followed by an underscore and the rest of the function name. For example,
//! if the mode name is "TEMPLATE", a non-driver API function would be named like: template_create_dhcp_configuration().
//!
//! @todo
//! - Implement the scrub API
//!
/*----------------------------------------------------------------------------*\
| |
| 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 <euca_string.h>
#include <log.h>
#include <hash.h>
#include <math.h>
#include <http.h>
#include <config.h>
#include <atomic_file.h>
#include "ipt_handler.h"
#include "ips_handler.h"
#include "ebt_handler.h"
#include "dev_handler.h"
#include "eucalyptus-config.h"
#include "euca_gni.h"
#include "eucanetd.h"
#include "eucanetd_util.h"
#include "eucanetd_edge.h"
#include "euca_arp.h"
/*----------------------------------------------------------------------------*\
| |
| DEFINES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| TYPEDEFS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| ENUMERATIONS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STRUCTURES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| EXTERNAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/* Should preferably be handled in header file */
/*----------------------------------------------------------------------------*\
| |
| GLOBAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STATIC VARIABLES |
| |
\*----------------------------------------------------------------------------*/
//! Set to TRUE when driver is initialized
static boolean gInitialized = FALSE;
static edge_config *edgeConfig_a = NULL;
static edge_config *edgeConfig_b = NULL;
static edge_config *edgeConfig = NULL;
static edge_config *edgeConfigApplied = NULL;
static edge_netmeter *netmeter = NULL;
static int edgeMaintCount = 0;
/*----------------------------------------------------------------------------*\
| |
| EXPORTED PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STATIC PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
//! @{
//! @name EDGE Mode Network Driver APIs
static int network_driver_init(eucanetdConfig *pEucanetdConfig, globalNetworkInfo *pGni);
static int network_driver_upgrade(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
static int network_driver_cleanup(eucanetdConfig *pConfig, globalNetworkInfo *pGni, boolean forceFlush);
static int network_driver_system_flush(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
static int network_driver_system_maint(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
static u32 network_driver_system_scrub(eucanetdConfig *pConfig, globalNetworkInfo *pGni, globalNetworkInfo *pGniApplied);
//static int network_driver_implement_network(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
//static int network_driver_implement_sg(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
//static int network_driver_implement_addressing(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
//static int network_driver_handle_signal(eucanetdConfig *pConfig, globalNetworkInfo *pGni, int signal);
//! @}
/*----------------------------------------------------------------------------*\
| |
| MACROS |
| |
\*----------------------------------------------------------------------------*/
//! Macro to see if the driver has been initialized
#define IS_INITIALIZED() ((gInitialized == TRUE) ? TRUE : FALSE)
//! Macro to get the driver name
#define DRIVER_NAME() edgeDriverHandler.name
/*----------------------------------------------------------------------------*\
| |
| CALLBACK STRUCTURE |
| |
\*----------------------------------------------------------------------------*/
//! EDGE driver operation handlers
struct driver_handler_t edgeDriverHandler = {
.name = NETMODE_EDGE,
.init = network_driver_init,
.cleanup = network_driver_cleanup,
.upgrade = network_driver_upgrade,
.system_flush = network_driver_system_flush,
.system_maint = network_driver_system_maint,
.system_scrub = network_driver_system_scrub,
.implement_network = NULL,
.implement_sg = NULL,
.implement_addressing = NULL,
.handle_signal = NULL,
};
/*----------------------------------------------------------------------------*\
| |
| IMPLEMENTATION |
| |
\*----------------------------------------------------------------------------*/
/**
* Initialize EDGE network driver.
* @param pEucanetdConfig [in] a pointer to eucanetd configuration structure
* @param pGni [in] a pointer to the Global Network Information structure
* @return 0 on success or 1 on any failure
*
* @pre \li The core application configuration must be completed prior calling
* \li The driver must not be already initialized (if its the case, a no-op will occur)
* \li The pEucanetdConfig parameter must not be NULL
*
* @post On success the driver is properly configured. On failure, the state of
* the driver is non-deterministic. If the driver was previously initialized,
* this will result into a no-op.
*/
static int network_driver_init(eucanetdConfig *pEucanetdConfig, globalNetworkInfo *pGni) {
int rc = 0;
int ret = 0;
LOGINFO("Initializing '%s' network driver.\n", DRIVER_NAME());
// Make sure our given pointer is valid
if (!pEucanetdConfig) {
LOGERROR("Failure to initialize '%s' networking mode. Invalid configuration parameter provided.\n", DRIVER_NAME());
return (1);
}
// Are we already initialized?
if (IS_INITIALIZED()) {
LOGERROR("Networking '%s' mode already initialized. Skipping!\n", DRIVER_NAME());
return (0);
}
if (!pEucanetdConfig->ipt) {
pEucanetdConfig->ipt = EUCA_ZALLOC_C(1, sizeof (ipt_handler));
rc = ipt_handler_init(pEucanetdConfig->ipt, pEucanetdConfig->cmdprefix, NULL);
if (rc) {
LOGERROR("could not initialize ipt_handler: check above log errors for details\n");
ret = 1;
} else {
ipt_handler_free(pEucanetdConfig->ipt);
}
}
if (!pEucanetdConfig->ips) {
pEucanetdConfig->ips = EUCA_ZALLOC_C(1, sizeof (ips_handler));
rc = ips_handler_init(pEucanetdConfig->ips, pEucanetdConfig->cmdprefix);
if (rc) {
LOGERROR("could not initialize ips_handler: check above log errors for details\n");
ret = 1;
} else {
ips_handler_free(pEucanetdConfig->ips);
}
}
if (!pEucanetdConfig->ebt) {
pEucanetdConfig->ebt = EUCA_ZALLOC_C(1, sizeof (ebt_handler));
rc = ebt_handler_init(pEucanetdConfig->ebt, pEucanetdConfig->cmdprefix);
if (rc) {
LOGERROR("could not initialize ebt_handler: check above log errors for details\n");
ret = 1;
} else {
ebt_handler_free(pEucanetdConfig->ebt);
}
}
if (ret) {
EUCA_FREE(pEucanetdConfig->ipt);
EUCA_FREE(pEucanetdConfig->ips);
EUCA_FREE(pEucanetdConfig->ebt);
return (1);
}
netmeter = EUCA_ZALLOC_C(1, sizeof (edge_netmeter));
edgeConfig_a = EUCA_ZALLOC_C(1, sizeof (edge_config));
edgeConfig_a->config = pEucanetdConfig;
edgeConfig_a->nmeter = netmeter;
edgeConfig_b = EUCA_ZALLOC_C(1, sizeof (edge_config));
edgeConfig_b->config = pEucanetdConfig;
edgeConfig_b->nmeter = netmeter;
edgeConfig = edgeConfigApplied = NULL;
// We are now initialize
gInitialized = TRUE;
return (0);
}
/**
* Upgrades 4.4 EDGE constructs to 5.0 EDGE constructs.
*
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
*
* @post
* - On failure, the system is left in an undetermined state
*
* @TODO:
* This will not be needed for 5.0+
*/
static int network_driver_upgrade(eucanetdConfig *pConfig, globalNetworkInfo *pGni) {
int ret = 0;
// Skip upgrade in flush mode
if (pConfig && pConfig->flushmode != FLUSH_NONE) {
LOGTRACE("\tflush mode selected. Skipping upgrade\n");
return (0);
}
LOGDEBUG("Upgrade 4.4 '%s' network driver artifacts.\n", DRIVER_NAME());
if (!pConfig) {
LOGWARN("Invalid argument: cannot upgrade with NULL config\n");
return (1);
}
// this only applies to NC components
if (!PEER_IS_NC(eucanetdPeer)) {
// no-op
return (0);
}
// Is our driver initialized?
if (!IS_INITIALIZED()) {
LOGERROR("Upgrade 4.4 '%s' network driver artifacts failed. Driver not initialized.\n", DRIVER_NAME());
return (1);
}
ips_handler_repopulate(pConfig->ips);
boolean do_upgrade = FALSE;
u32 euca_version = euca_version_dot2hex(EUCA_VERSION);
u32 detected_euca_version = 0;
ips_set *euca_version_ips = ips_handler_find_set(pConfig->ips, "EUCA_VERSION");
if (euca_version_ips) {
if ((euca_version_ips->max_member_ips == 1) && (euca_version_ips->member_nms[0] == 32)) {
detected_euca_version = euca_version_ips->member_ips[0];
} else if (euca_version_ips->max_member_ips > 1) {
LOGFATAL("Unable to detect eucanetd artifacts version - %d versions\n",
euca_version_ips->max_member_ips);
LOGINFO("eucanetd going down.\n");
exit (1);
}
}
if (detected_euca_version) {
if (detected_euca_version < dot2hex("4.4.0.0")) {
LOGWARN("Unable to upgrade from version %s\n", hex2dot_s(detected_euca_version));
LOGINFO("eucanetd going down.\n");
exit (1);
}
if (detected_euca_version < euca_version) {
do_upgrade = TRUE;
char *euca_version_str = hex2dot(euca_version);
LOGINFO("Upgrading eucanetd %s to %s\n", hex2dot_s(detected_euca_version), euca_version_str);
EUCA_FREE(euca_version_str);
}
if (detected_euca_version > euca_version) {
LOGFATAL("Downgrading eucanetd from %s to %s not supported.\n", hex2dot_s(detected_euca_version), EUCA_VERSION);
LOGINFO("Cleanup eucanetd artifacts with 'eucanetd -F'\n");
LOGINFO("eucanetd going down.\n");
exit (1);
}
} else {
LOGTRACE("Did not detect eucanetd artifacts - no upgrade action taken\n");
}
if (do_upgrade) {
// dhcpd
// no upgrade operation required
// iptables
// no upgrade operation required
// ipsets
// no upgrade operation required
// ebtables
// no upgrade operation required
}
return (ret);
}
/**
* Cleans up the network driver. This will work even if the initial initialization
* fail for any reasons. This will reset anything that could have been half-way or
* fully configured. If forceFlush is set, then a network flush will be performed.
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @param forceFlush [in] set to TRUE if a network flush needs to be performed
* @return 0 on success. Integer number on failure.
*/
static int network_driver_cleanup(eucanetdConfig *pConfig, globalNetworkInfo *pGni, boolean forceFlush) {
int ret = 0;
LOGINFO("Cleaning up '%s' network driver.\n", DRIVER_NAME());
if (forceFlush) {
if (network_driver_system_flush(pConfig, pGni)) {
LOGERROR("Fail to flush network artifacts during network driver cleanup. See above log errors for details.\n");
ret = 1;
}
}
free_edge_netmeter(netmeter);
free_edge_config(edgeConfig_a);
free_edge_config(edgeConfig_b);
EUCA_FREE(netmeter);
EUCA_FREE(edgeConfig_a);
EUCA_FREE(edgeConfig_b);
gInitialized = FALSE;
return (ret);
}
/**
* Responsible for flushing any networking artifacts implemented by this
* network driver.
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @return 0 on success. Integer number on failure.
*/
static int network_driver_system_flush(eucanetdConfig *pConfig, globalNetworkInfo *pGni) {
int rc = 0;
int ret = 0;
LOGINFO("Flushing '%s' network driver artifacts.\n", DRIVER_NAME());
// this only applies to NC components
if (!PEER_IS_NC(eucanetdPeer)) {
// no-op
return (0);
}
// Is our driver initialized?
if (!IS_INITIALIZED()) {
LOGERROR("Failed to flush the networking artifacts for '%s' network driver. Driver not initialized.\n", DRIVER_NAME());
return (1);
}
if (!pConfig) {
LOGWARN("Invalid argument: cannot flush %s with NULL config\n", DRIVER_NAME());
return (1);
}
// dhcpd
eucanetd_stop_dhcpd_server(pConfig);
// iptables
ipt_handler_repopulate(pConfig->ipt);
ipt_chain_flush(pConfig->ipt, "raw", "EUCA_COUNTERS_IN");
ipt_chain_flush(pConfig->ipt, "raw", "EUCA_COUNTERS_OUT");
ipt_chain_flush(pConfig->ipt, "raw", "EUCA_RAW_PRE");
ipt_chain_flush(pConfig->ipt, "filter", "EUCA_FILTER_FWD");
ipt_chain_flush(pConfig->ipt, "filter", "EUCA_FILTER_FWD_DROPPED");
ipt_chain_flush(pConfig->ipt, "nat", "EUCA_NAT_PRE");
ipt_chain_flush(pConfig->ipt, "nat", "EUCA_NAT_POST");
ipt_chain_flush(pConfig->ipt, "nat", "EUCA_NAT_OUT");
ipt_table_deletechainmatch(pConfig->ipt, "filter", "sg-");
// Flush core artifacts
if (pConfig->flushmode == FLUSH_ALL) {
ipt_table_deletechainmatch(pConfig->ipt, "raw", "EUCA_");
ipt_table_deletechainmatch(pConfig->ipt, "filter", "EUCA_");
ipt_table_deletechainmatch(pConfig->ipt, "nat", "EUCA_");
ipt_chain_flush_rule(pConfig->ipt, "raw", "PREROUTING", "-A PREROUTING -j EUCA_RAW_PRE");
ipt_chain_flush_rule(pConfig->ipt, "filter", "FORWARD", "-A FORWARD -j EUCA_FILTER_FWD_PREUSERHOOK");
ipt_chain_flush_rule(pConfig->ipt, "filter", "FORWARD", "-A FORWARD -j EUCA_FILTER_FWD");
ipt_chain_flush_rule(pConfig->ipt, "filter", "FORWARD", "-A FORWARD -j EUCA_FILTER_FWD_POSTUSERHOOK");
ipt_chain_flush_rule(pConfig->ipt, "nat", "PREROUTING", "-A PREROUTING -j EUCA_NAT_PRE_PREUSERHOOK");
ipt_chain_flush_rule(pConfig->ipt, "nat", "PREROUTING", "-A PREROUTING -j EUCA_NAT_PRE");
ipt_chain_flush_rule(pConfig->ipt, "nat", "PREROUTING", "-A PREROUTING -j EUCA_NAT_PRE_POSTUSERHOOK");
ipt_chain_flush_rule(pConfig->ipt, "nat", "POSTROUTING", "-A POSTROUTING -j EUCA_NAT_POST_PREUSERHOOK");
ipt_chain_flush_rule(pConfig->ipt, "nat", "POSTROUTING", "-A POSTROUTING -j EUCA_NAT_POST");
ipt_chain_flush_rule(pConfig->ipt, "nat", "POSTROUTING", "-A POSTROUTING -j EUCA_NAT_POST_POSTUSERHOOK");
ipt_chain_flush_rule(pConfig->ipt, "nat", "OUTPUT", "-A OUTPUT -j EUCA_NAT_OUT_PREUSERHOOK");
ipt_chain_flush_rule(pConfig->ipt, "nat", "OUTPUT", "-A OUTPUT -j EUCA_NAT_OUT");
ipt_chain_flush_rule(pConfig->ipt, "nat", "OUTPUT", "-A OUTPUT -j EUCA_NAT_OUT_POSTUSERHOOK");
}
ipt_handler_print(pConfig->ipt);
rc = ipt_handler_deploy(pConfig->ipt);
if (rc) {
LOGERROR("Failed to flush the IP Tables artifact in '%s' networking mode.\n", DRIVER_NAME());
ret = 1;
}
// ipsets
ips_handler_repopulate(pConfig->ips);
ips_handler_deletesetmatch(pConfig->ips, "sg-");
ips_handler_deletesetmatch(pConfig->ips, "EUCA_");
if (pConfig->flushmode != FLUSH_ALL) {
u32 euca_version = euca_version_dot2hex(EUCA_VERSION);
char *strptra = hex2dot(euca_version);
ips_handler_add_set(pConfig->ips, "EUCA_VERSION");
ips_set_flush(pConfig->ips, "EUCA_VERSION");
ips_set_add_ip(pConfig->ips, "EUCA_VERSION", strptra);
EUCA_FREE(strptra);
}
ips_handler_print(pConfig->ips);
rc = ips_handler_deploy(pConfig->ips, 1);
if (rc) {
LOGERROR("Failed to flush the IP Sets artifact in '%s' networking mode.\n", DRIVER_NAME());
ret = 1;
}
// ebtables
ebt_handler_repopulate(pConfig->ebt);
ebt_chain_flush(pConfig->ebt, "filter", "EUCA_EBT_FWD");
ebt_chain_flush(pConfig->ebt, "nat", "EUCA_EBT_NAT_PRE");
ebt_chain_flush(pConfig->ebt, "nat", "EUCA_EBT_NAT_POST");
// Flush core artifacts
if (pConfig->flushmode == FLUSH_ALL) {
ebt_table_deletechainmatch(pConfig->ebt, "filter", "EUCA_");
ebt_table_deletechainmatch(pConfig->ebt, "nat", "EUCA_");
ebt_chain_flush_rule(pConfig->ebt, "filter", "FORWARD", "-j EUCA_EBT_FWD");
ebt_chain_flush_rule(pConfig->ebt, "nat", "PREROUTING", "-j EUCA_EBT_NAT_PRE");
ebt_chain_flush_rule(pConfig->ebt, "nat", "POSTROUTING", "-j EUCA_EBT_NAT_POST");
}
rc = ebt_handler_deploy(pConfig->ebt);
if (rc) {
LOGERROR("Failed to flush the EB Tables artifact in '%s' networking mode.\n", DRIVER_NAME());
ret = 1;
}
// Clear public IPs that have been mapped
u32 *ips = NULL, *nms = NULL;
int max_nets = 0;
rc = 0;
int i = 0;
int j = 0;
char cmd[EUCA_MAX_PATH] = "";
char *strptra = NULL;
if (getdevinfo(pConfig->pubInterface, &ips, &nms, &max_nets)) {
// could not get interface info - skip public IP flush
max_nets = 0;
ips = NULL;
nms = NULL;
ret = 1;
LOGERROR("Failed to flush public IP address(es) in '%s' networking mode.\n", DRIVER_NAME());
} else {
if (pGni->max_public_ips_str && !pGni->max_public_ips) {
gni_serialize_iprange_list(pGni->public_ips_str, pGni->max_public_ips_str, &(pGni->public_ips), &(pGni->max_public_ips));
}
for (i = 0; i < pGni->max_public_ips; i++) {
for (j = 0; j < max_nets; j++) {
if (ips[j] == pGni->public_ips[i]) {
// this global public IP is assigned to the public interface
strptra = hex2dot(pGni->public_ips[i]);
snprintf(cmd, EUCA_MAX_PATH, "%s/32", strptra);
EUCA_FREE(strptra);
euca_execlp_redirect(&rc, NULL, "/dev/null", FALSE, "/dev/null", FALSE, pConfig->cmdprefix, "ip", "addr", "del", cmd, "dev", pConfig->pubInterface, NULL);
rc = rc >> 8;
if(!(rc == 0 || rc == 2)){
LOGERROR("Failed to run ip addr del %s/32 dev %s", strptra, pConfig->pubInterface);
ret = 1;
}
}
}
}
EUCA_FREE(ips);
EUCA_FREE(nms);
}
return (ret);
}
/**
* This API is invoked when eucanetd will potentially be idle. For example, after
* populating the global network state, eucanetd detects that no action needs to
* be taken. Good for pre-populating cache, or flushing dirty cache - so these
* actions are not necessary in the regular iteration.
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @return 0 on success. Integer number on failure.
*/
static int network_driver_system_maint(eucanetdConfig *pConfig, globalNetworkInfo *pGni) {
int rc = 0;
struct timeval tv;
LOGTRACE("Running maintenance for '%s' network driver.\n", DRIVER_NAME());
eucanetd_timer(&tv);
// Is the driver initialized?
if (!IS_INITIALIZED()) {
LOGERROR("Failed to run maintenance activities. Driver '%s' not initialized.\n", DRIVER_NAME());
return (1);
}
if ((edgeMaintCount % 10) == 0) {
if (pGni == edgeConfig_a->gni) {
edgeConfig_a->config = pConfig;
do_edge_update_netmeter(edgeConfig_a);
}
if (pGni == edgeConfig_b->gni) {
edgeConfig_b->config = pConfig;
do_edge_update_netmeter(edgeConfig_b);
}
}
edgeMaintCount++;
return (rc);
}
/**
* This API checks the new GNI against the system view to decide what really
* needs to be done.
* EDGE system scrub. Detect instances and security groups relevant to local NC.
* Then, process security groups, elastic/public IPs, and DHCP.
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @param pGniApplied [in] a pointer to the previously successfully implemented GNI
* @return A bitmask indicating what needs to be done. The following bits are
* the ones to look for: EUCANETD_RUN_NO_API (GNI successfully applied), or
* EUCANETD_RUN_ERROR_API (failed to apply GNI).
*/
static u32 network_driver_system_scrub(eucanetdConfig *pConfig, globalNetworkInfo *pGni, globalNetworkInfo *pGniApplied) {
int rc = 0;
u32 ret = EUCANETD_RUN_NO_API;
struct timeval tv;
eucanetd_timer(&tv);
LOGTRACE("Scrubbing for '%s' network driver.\n", DRIVER_NAME());
// this only applies to NC components
if (!PEER_IS_NC(eucanetdPeer)) {
// no-op
return (EUCANETD_RUN_NO_API);
}
// Is the driver initialized?
if (!IS_INITIALIZED()) {
LOGERROR("Unable to execute system scrub. Driver not initialized.\n");
return (EUCANETD_RUN_ERROR_API);
}
// Are the global and local network view structures NULL?
if (!pGni || !pConfig) {
LOGERROR("Unable to execute system scrub. Invalid parameters provided.\n");
return (EUCANETD_RUN_ERROR_API);
}
if (edgeConfig == edgeConfig_a) {
edgeConfig = edgeConfig_b;
edgeConfigApplied = edgeConfig_a;
} else {
edgeConfig = edgeConfig_a;
edgeConfigApplied = edgeConfig_b;
}
free_edge_config(edgeConfig);
free_edge_config(edgeConfigApplied);
edgeConfig->gni = pGni;
edgeConfig->config = pConfig;
rc = extract_edge_config_from_gni(edgeConfig);
if (rc) {
LOGDEBUG("failed to populate edgeConfig\n");
return (EUCANETD_RUN_ERROR_API);
}
boolean do_edge_update = TRUE;
int do_instances = 1;
int do_sgs = 1;
int do_allprivate = 1;
if (pGniApplied && (pGni != pGniApplied)) {
int config_changed = cmp_gni_config(pGni, pGniApplied);
if ((config_changed & GNI_CONFIG_DIFF_SUBNETS) == 0) {
edgeConfigApplied->gni = pGniApplied;
edgeConfigApplied->config = pConfig;
rc = extract_edge_config_from_gni(edgeConfigApplied);
if (!rc) {
if (!cmp_edge_config(edgeConfig, edgeConfigApplied, &do_instances,
&do_sgs, &do_allprivate)) {
do_edge_update = FALSE;
LOGINFO("\tSystem is already up-to-date\n");
}
}
}
}
if (do_edge_update) {
if (do_allprivate) {
rc += do_edge_update_allprivate(edgeConfig);
}
if (do_sgs) {
rc += do_edge_update_sgs(edgeConfig);
}
if (do_instances) {
rc += do_edge_update_eips(edgeConfig);
rc += do_edge_update_l2(edgeConfig);
rc += do_edge_update_ips(edgeConfig);
}
}
rc += do_edge_update_netmeter(edgeConfig);
if (rc) {
ret = EUCANETD_RUN_ERROR_API;
}
return (ret);
}
/**
* Updates the list of IP addresses in EUCA_ALLPRIVATE ipset.
* @param edge [in] pointer to EDGE configuration structure
* @return 0 on success. Positive integer on any error during processing.
*/
int do_edge_update_allprivate(edge_config *edge) {
int rc = 0;
int slashnet = 0;
char *strptra = NULL;
char *vmgwip = NULL;
struct timeval tv = { 0 };
eucanetd_timer(&tv);
LOGTRACE("Updating EUCA_ALLPRIVATE ipset.\n");
// Is EDGE configuration NULL?
if (!edge || !edge->config || !edge->gni) {
LOGERROR("Invalid argument: cannot update core ipset with NULL configuration.\n");
return (1);
}
// pull in latest IPS state
rc |= ips_handler_repopulate(edge->config->ips);
if (rc) {
LOGERROR("Failed to load ipset state\n");
return (1);
}
u32 euca_version = euca_version_dot2hex(EUCA_VERSION);
strptra = hex2dot(euca_version);
ips_handler_add_set(edge->config->ips, "EUCA_VERSION");
ips_set_flush(edge->config->ips, "EUCA_VERSION");
ips_set_add_ip(edge->config->ips, "EUCA_VERSION", strptra);
EUCA_FREE(strptra);
// reset and create ipset for allprivate
ips_handler_add_set(edge->config->ips, "EUCA_ALLPRIVATE");
ips_set_flush(edge->config->ips, "EUCA_ALLPRIVATE");
// Populate ipset with all private IPs
for (int i = 0; i < edge->gni->max_instances; i++) {
gni_instance *inst = edge->gni->instances[i];
if (inst->privateIp) {
strptra = hex2dot(inst->privateIp);
ips_set_add_ip(edge->config->ips, "EUCA_ALLPRIVATE", strptra);
EUCA_FREE(strptra);
}
}
// add additional private non-euca subnets to EUCA_ALLPRIVATE
for (int i = 0; i < edge->gni->max_subnets; i++) {
strptra = hex2dot(edge->gni->subnets[i].subnet);
slashnet = 32 - ((int)(log2((double)((0xFFFFFFFF - edge->gni->subnets[i].netmask) + 1))));
ips_set_add_net(edge->config->ips, "EUCA_ALLPRIVATE", strptra, slashnet);
EUCA_FREE(strptra);
}
// VM gateway IP
vmgwip = hex2dot(edge->config->vmGatewayIP);
ips_set_add_ip(edge->config->ips, "EUCA_ALLPRIVATE", vmgwip);
EUCA_FREE(vmgwip);
// Deploy our IP sets
rc = ips_handler_deploy(edge->config->ips, 0);
if (rc) {
LOGERROR("could not apply ipsets: check above log errors for details\n");
return (1);
}
LOGINFO("\tcore ipsets processed in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
return (0);
}
/**
* Implements security-group artifacts. This will add or remove remove networking
* rules pertaining to the groups and their membership. Entails installing the
* iptables rules for the groups and the ipset for the sg members.
* @param edge [in] pointer to EDGE configuration structure
* @return 0 on success. Positive integer on any error during processing.
*/
int do_edge_update_sgs(edge_config *edge) {
#define MAX_RULE_LEN 1024
int i = 0;
int j = 0;
int k = 0;
int rc = 0;
int ret = 0;
int ipcount = 0;
char *strptra = NULL;
char *vmgwip = NULL;
char *chainname = NULL;
char *refchainname = NULL;
char rule[MAX_RULE_LEN] = "";
gni_instance *instances;
int max_instances = 0;
gni_secgroup *secgroup = NULL;
gni_secgroup *refsecgroup = NULL;
u32 cidrnm = 0xffffffff;
struct timeval tv = { 0 };
eucanetd_timer(&tv);
LOGTRACE("Implementing security-group artifacts.\n");
// Is EDGE configuration NULL?
if (!edge || !edge->config || !edge->gni) {
LOGERROR("Invalid argument: cannot update SGs from NULL configuration.\n");
return (1);
}
// pull in latest IPT state
rc |= ipt_handler_repopulate(edge->config->ipt);
// pull in latest IPS state
rc |= ips_handler_repopulate(edge->config->ips);
if (rc) {
LOGERROR("Failed to load iptables and/or ipset state\n");
return (1);
}
// make sure euca chains are in place
ipt_table_add_chain(edge->config->ipt, "filter", "EUCA_FILTER_FWD_PREUSERHOOK", "-", "[0:0]");
ipt_table_add_chain(edge->config->ipt, "filter", "EUCA_FILTER_FWD", "-", "[0:0]");
ipt_table_add_chain(edge->config->ipt, "filter", "EUCA_FILTER_FWD_DROPPED", "-", "[0:0]");
ipt_table_add_chain(edge->config->ipt, "filter", "EUCA_FILTER_FWD_POSTUSERHOOK", "-", "[0:0]");
ipt_chain_add_rule(edge->config->ipt, "filter", "FORWARD", "-A FORWARD -j EUCA_FILTER_FWD_PREUSERHOOK");
ipt_chain_add_rule(edge->config->ipt, "filter", "FORWARD", "-A FORWARD -j EUCA_FILTER_FWD");
ipt_chain_add_rule(edge->config->ipt, "filter", "FORWARD", "-A FORWARD -j EUCA_FILTER_FWD_POSTUSERHOOK");
// clear all chains that we're about to (re)populate with latest network view
ipt_chain_flush(edge->config->ipt, "filter", "EUCA_FILTER_FWD");
ipt_chain_flush(edge->config->ipt, "filter", "EUCA_FILTER_FWD_DROPPED");
ipt_table_deletechainmatch(edge->config->ipt, "filter", "sg-");
ipt_chain_add_rule(edge->config->ipt, "filter", "EUCA_FILTER_FWD",
"-A EUCA_FILTER_FWD -m conntrack --ctstate ESTABLISHED -j ACCEPT");
// reset and create ipsets for allprivate, ncprivate and noneuca subnet sets
ips_handler_deletesetmatch(edge->config->ips, "sg-");
ips_handler_add_set(edge->config->ips, "EUCA_NCPRIVATE");
ips_set_flush(edge->config->ips, "EUCA_NCPRIVATE");
// Populate ipset with local private IPs
ipcount = 0;
for (i = 0; i < edge->max_my_instances; i++) {
gni_instance *inst = &(edge->my_instances[i]);
if (inst->privateIp) {
strptra = hex2dot(inst->privateIp);
ips_set_add_ip(edge->config->ips, "EUCA_NCPRIVATE", strptra);
ipcount++;
EUCA_FREE(strptra);
}
}
if (!ipcount) {
ips_set_add_net(edge->config->ips, "EUCA_NCPRIVATE", "127.0.0.1", 32);
}
// Forward packets generated by instances hosted by this NC and not destined
// to instances hosted by this NC (this should go out of this NC). Packets
// destined to instances hosted by this NC are subject to SG chains
snprintf(rule, MAX_RULE_LEN, "-A EUCA_FILTER_FWD -m physdev --physdev-in vn_i+ "
"-m set ! --match-set EUCA_NCPRIVATE dst -j ACCEPT");
ipt_chain_add_rule(edge->config->ipt, "filter", "EUCA_FILTER_FWD", rule);
vmgwip = hex2dot(edge->config->vmGatewayIP);
// add referenced SG ipsets
for (i = 0; i < edge->max_ref_sgs; i++) {
secgroup = edge->ref_sgs[i];
chainname = strdup(secgroup->name);
ips_handler_add_set(edge->config->ips, chainname);
ips_set_flush(edge->config->ips, chainname);
ips_set_add_ip(edge->config->ips, chainname, vmgwip);
max_instances = 0;
gni_secgroup_get_instances(edge->gni, secgroup, NULL, 0, NULL, 0, &instances, &max_instances);
for (j = 0; j < max_instances; j++) {
if (instances[j].privateIp) {
strptra = hex2dot(instances[j].privateIp);
ips_set_add_ip(edge->config->ips, chainname, strptra);
EUCA_FREE(strptra);
}
if (instances[j].publicIp) {
strptra = hex2dot(instances[j].publicIp);
ips_set_add_ip(edge->config->ips, chainname, strptra);
EUCA_FREE(strptra);
}
}
EUCA_FREE(instances);
EUCA_FREE(chainname);
}
// add SGs of VMs hosted by this NC
for (i = 0; i < edge->max_my_sgs; i++) {
secgroup = edge->my_sgs[i];
chainname = strdup(secgroup->name);
rule[0] = '\0';
ips_handler_add_set(edge->config->ips, chainname);
ips_set_flush(edge->config->ips, chainname);
ips_set_add_ip(edge->config->ips, chainname, vmgwip);
max_instances = 0;
gni_secgroup_get_instances(edge->gni, secgroup, NULL, 0, NULL, 0, &instances, &max_instances);
for (j = 0; j < max_instances; j++) {
if (instances[j].privateIp) {
strptra = hex2dot(instances[j].privateIp);
ips_set_add_ip(edge->config->ips, chainname, strptra);
EUCA_FREE(strptra);
}
if (instances[j].publicIp) {
strptra = hex2dot(instances[j].publicIp);
ips_set_add_ip(edge->config->ips, chainname, strptra);
EUCA_FREE(strptra);
}
}
// add forward chain
ipt_table_add_chain(edge->config->ipt, "filter", chainname, "-", "[0:0]");
ipt_chain_flush(edge->config->ipt, "filter", chainname);
// add jump rule
snprintf(rule, MAX_RULE_LEN, "-A EUCA_FILTER_FWD -m set --match-set %s dst -j %s", chainname, chainname);
ipt_chain_add_rule(edge->config->ipt, "filter", "EUCA_FILTER_FWD", rule);
// populate forward chain
// this one needs to be first
snprintf(rule, MAX_RULE_LEN, "-A %s -m set --match-set %s src -j ACCEPT", chainname, chainname);
ipt_chain_add_rule(edge->config->ipt, "filter", chainname, rule);
// then put all the group specific IPT rules (temporary one here)
if (secgroup->max_ingress_rules) {
for (j = 0; j < secgroup->max_ingress_rules; j++) {
// If this rule is in reference to another group, lets add this IP set here
if (strlen(secgroup->ingress_rules[j].groupId) != 0) {
refsecgroup = gni_get_secgroup(edge->gni, secgroup->ingress_rules[j].groupId, NULL);
if (refsecgroup == NULL) {
LOGWARN("Could not find referenced security group %s. Skipping ingress rule.\n", secgroup->ingress_rules[j].groupId);
} else {
LOGDEBUG("Found referenced security group %s owner %s\n", refsecgroup->name, refsecgroup->accountId);
refchainname = NULL;
refchainname = strdup(refsecgroup->name);
ingress_gni_to_iptables_rule(NULL, &(secgroup->ingress_rules[j]), rule, 0);
strptra = strdup(rule);
snprintf(rule, MAX_RULE_LEN, "-A %s -m set --match-set %s src %s -j ACCEPT", chainname, refchainname, strptra);
ipt_chain_add_rule(edge->config->ipt, "filter", chainname, rule);
EUCA_FREE(strptra);
EUCA_FREE(refchainname);
}
} else {
ingress_gni_to_iptables_rule(NULL, &(secgroup->ingress_rules[j]), rule, 0);
strptra = strdup(rule);
snprintf(rule, MAX_RULE_LEN, "-A %s %s -j ACCEPT", chainname, strptra);
ipt_chain_add_rule(edge->config->ipt, "filter", chainname, rule);
EUCA_FREE(strptra);
// Check if this rule refers to a public IP that this NC is responsible for
if (strlen(secgroup->ingress_rules[j].cidr)) {
// Ignoring potential shift by 32 on a u32. If cidrsn is 0, the rule will not be processed (it allows all, so the rule above suffices)
cidrnm = (u32) 0xffffffff << (32 - secgroup->ingress_rules[j].cidrSlashnet);
if (secgroup->ingress_rules[j].cidrSlashnet != 0) {
// Search for public IPs that this NC is responsible
for (k = 0; k < edge->max_my_instances; k++) {
if (((edge->my_instances[k].publicIp & cidrnm) == (secgroup->ingress_rules[j].cidrNetaddr & cidrnm)) &&
((edge->my_instances[k].privateIp & cidrnm) != (secgroup->ingress_rules[j].cidrNetaddr & cidrnm))) {
strptra = hex2dot(edge->my_instances[k].privateIp);
LOGDEBUG("Found instance private IP (%s) local to this NC affected by another rule.\n", strptra);
ingress_gni_to_iptables_rule(strptra, &(secgroup->ingress_rules[j]), rule, 1);
LOGDEBUG("Created new iptables rule: %s\n", rule);
EUCA_FREE(strptra);
strptra = strdup(rule);
snprintf(rule, MAX_RULE_LEN, "-A %s %s -j ACCEPT", chainname, strptra);
ipt_chain_add_rule(edge->config->ipt, "filter", chainname, rule);
EUCA_FREE(strptra);
}
}
}
}
}
}
}