/
vnetwork.c
4633 lines (4146 loc) · 166 KB
/
vnetwork.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
//! Implements the Virtual Network library.
//!
/*----------------------------------------------------------------------------*\
| |
| INCLUDES |
| |
\*----------------------------------------------------------------------------*/
#define _FILE_OFFSET_BITS 64 // so large-file support works on 32-bit systems
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pthread.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdarg.h>
#include <ifaddrs.h>
#include <math.h> /* log2 */
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <eucalyptus.h>
#include <vnetwork.h>
#include <misc.h>
#include <hash.h>
#include <fault.h>
#include <euca_string.h>
/*----------------------------------------------------------------------------*\
| |
| DEFINES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| TYPEDEFS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| ENUMERATIONS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STRUCTURES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| EXTERNAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/* Should preferably be handled in header file */
/*----------------------------------------------------------------------------*\
| |
| GLOBAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
char *iptablesCache = NULL; //!< contains the IP tables cache
/*----------------------------------------------------------------------------*\
| |
| STATIC VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STATIC PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| MACROS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| IMPLEMENTATION |
| |
\*----------------------------------------------------------------------------*/
//!
//! Initialize the given network configuration structure based on the given parameters. This API validates the given parameters
//! prior initializing the network configuration structure.
//!
//! @param[in] vnetconfig a pointer to the Virtual Network Configuration information structure
//! @param[in] mode the network mode (ie. SYSTEM, STATIC, STATIC-DYNMAC, MANAGED-NOVLAN, MANAGED)
//! @param[in] eucahome
//! @param[in] path
//! @param[in] role
//! @param[in] pubInterface
//! @param[in] privInterface
//! @param[in] numberofaddrs
//! @param[in] network
//! @param[in] netmask
//! @param[in] broadcast
//! @param[in] nameserver
//! @param[in] domainname
//! @param[in] router
//! @param[in] daemon
//! @param[in] dhcpuser
//! @param[in] bridgedev
//! @param[in] localIp
//! @param[in] macPrefix
//!
//! @return EUCA_OK on success or proper error code on failure. Known error code returned include:
//! \li EUCA_ERROR: For any other issues executing this function
//! \li EUCA_INVALID_ERROR: If any parameter does not meet the preconditions
//!
//! @pre \p vnetconfig, \p mode and \p role must not be NULL
//!
int vnetInit(vnetConfig * vnetconfig, char *mode, char *eucahome, char *path, int role, char *pubInterface, char *privInterface, char *numberofaddrs,
char *network, char *netmask, char *broadcast, char *nameserver, char *domainname, char *router, char *daemon, char *dhcpuser,
char *bridgedev, char *localIp, char *macPrefix)
{
u32 nw = 0;
u32 nm = 0;
u32 unw = 0;
u32 unm = 0;
u32 dns = 0;
u32 bc = 0;
u32 rt = 0;
u32 rc = 0;
u32 slashnet = 0;
int vlan = 0;
int bits = 1;
int numaddrs = 1;
int numberofaddrs_i = 0;
char cmd[256] = "";
char *ipbuf = NULL;
if (vnetconfig == NULL) {
LOGERROR("bad input params: vnetconfig:%p\n", vnetconfig);
return (EUCA_INVALID_ERROR);
}
if (!vnetconfig->initialized) {
bzero(vnetconfig, sizeof(vnetConfig));
// always need 'mode' set
if (mode) {
euca_strncpy(vnetconfig->mode, mode, 32);
} else {
LOGERROR("VNET_MODE is not set\n");
return (EUCA_INVALID_ERROR);
}
if (role != CLC && role != NC) {
LOGERROR("bad role specified\n");
return (EUCA_INVALID_ERROR);
}
//check mode specific parameters
if (!strcmp(mode, "SYSTEM")) {
if (role == CLC) {
} else if (role == NC) {
if (!bridgedev || check_bridge(bridgedev)) {
LOGERROR("cannot verify VNET_BRIDGE(%s), please check parameters and bridge device\n", SP(bridgedev));
return (EUCA_ERROR);
}
}
} else if (!strcmp(mode, "STATIC") || !strcmp(mode, "STATIC-DYNMAC")) {
if (role == CLC) {
if (!daemon || check_file(daemon)) {
LOGERROR("cannot verify VNET_DHCPDAEMON (%s), please check parameter and location\n", SP(daemon));
return (EUCA_ERROR);
}
if (!privInterface || check_device(privInterface)) {
LOGERROR("cannot verify VNET_PRIVINTERFACE (%s), please check parameter and device name\n", SP(privInterface));
return (EUCA_ERROR);
}
if (!network || !netmask || !broadcast || !nameserver || !router) {
LOGERROR("cannot verify network settings (VNET_SUBNET(%s), VNET_NETMASK(%s), VNET_BROADCAST(%s), VNET_DNS(%s), VNET_ROUTER(%s)), "
"please check parameters\n", SP(network), SP(netmask), SP(broadcast), SP(nameserver), SP(router));
return (EUCA_ERROR);
}
} else if (role == NC) {
if (!strcmp(mode, "STATIC-DYNMAC")) {
if (!pubInterface || check_device(pubInterface)) {
LOGERROR("cannot verify VNET_PUBINTERFACE(%s), please check parameters and device\n", SP(pubInterface));
return (EUCA_ERROR);
}
}
}
} else if (!strcmp(mode, "MANAGED-NOVLAN")) {
if (role == CLC) {
if (!daemon || check_file(daemon)) {
LOGERROR("cannot verify VNET_DHCPDAEMON (%s), please check parameter and location\n", SP(daemon));
return (EUCA_ERROR);
}
if (!pubInterface || check_device(pubInterface)) {
LOGERROR("cannot verify VNET_PUBINTERFACE (%s), please check parameter and device name\n", SP(pubInterface));
return (EUCA_ERROR);
}
if (!privInterface || check_device(privInterface)) {
LOGERROR("cannot verify VNET_PRIVINTERFACE (%s), please check parameter and device name\n", SP(privInterface));
return (EUCA_ERROR);
}
if (!network || !netmask || !nameserver) {
LOGERROR("cannot verify network settings (VNET_SUBNET(%s), VNET_NETMASK(%s), VNET_DNS(%s), please check parameters\n",
SP(network), SP(netmask), SP(nameserver));
return (EUCA_ERROR);
}
} else if (role == NC) {
if (!bridgedev || check_bridge(bridgedev)) {
LOGERROR("cannot verify VNET_BRIDGE(%s), please check parameters and bridge device\n", SP(bridgedev));
return (EUCA_ERROR);
}
}
} else if (!strcmp(mode, "MANAGED")) {
if (role == CLC) {
if (!daemon || check_file(daemon)) {
LOGERROR("cannot verify VNET_DHCPDAEMON (%s), please check parameter and location\n", SP(daemon));
return (EUCA_ERROR);
}
if (!pubInterface || check_device(pubInterface)) {
LOGERROR("cannot verify VNET_PUBINTERFACE (%s), please check parameter and device name\n", SP(pubInterface));
return (EUCA_ERROR);
}
if (!privInterface || check_device(privInterface)) {
LOGERROR("cannot verify VNET_PRIVINTERFACE (%s), please check parameter and device name\n", SP(privInterface));
return (EUCA_ERROR);
}
if (!network || !netmask || !nameserver) {
LOGERROR("cannot verify network settings (VNET_SUBNET(%s), VNET_NETMASK(%s), VNET_DNS(%s)), please check parameters\n",
SP(network), SP(netmask), SP(nameserver));
return (EUCA_ERROR);
}
} else if (role == NC) {
}
} else {
LOGERROR("invalid networking mode %s, please check VNET_MODE parameter\n", SP(mode));
return (EUCA_INVALID_ERROR);
}
if (macPrefix)
euca_strncpy(vnetconfig->macPrefix, macPrefix, 6);
if (path)
euca_strncpy(vnetconfig->path, path, MAX_PATH);
if (eucahome)
euca_strncpy(vnetconfig->eucahome, eucahome, MAX_PATH);
if (pubInterface)
euca_strncpy(vnetconfig->pubInterface, pubInterface, 32);
if (bridgedev)
euca_strncpy(vnetconfig->bridgedev, bridgedev, 32);
if (daemon)
euca_strncpy(vnetconfig->dhcpdaemon, daemon, MAX_PATH);
if (privInterface)
euca_strncpy(vnetconfig->privInterface, privInterface, 32);
if (dhcpuser)
euca_strncpy(vnetconfig->dhcpuser, dhcpuser, 32);
if (domainname) {
euca_strncpy(vnetconfig->euca_domainname, domainname, 256);
} else {
strncpy(vnetconfig->euca_domainname, "eucalyptus", strlen("eucalyptus") + 1);
}
if (localIp) {
if ((ipbuf = host2ip(localIp)) != NULL) {
vnetAddLocalIP(vnetconfig, dot2hex(ipbuf));
EUCA_FREE(ipbuf);
}
}
vnetconfig->tunnels.localIpId = -1;
vnetconfig->tunnels.localIpIdLast = -1;
vnetconfig->tunnels.tunneling = 0;
vnetconfig->role = role;
vnetconfig->enabled = 1;
vnetconfig->initialized = 1;
vnetconfig->max_vlan = NUMBER_OF_VLANS;
if (numberofaddrs) {
numberofaddrs_i = atoi(numberofaddrs);
if (numberofaddrs_i > NUMBER_OF_HOSTS_PER_VLAN) {
LOGWARN("specified ADDRSPERNET exceeds maximum addresses per network (%d), setting to maximum.\n", NUMBER_OF_HOSTS_PER_VLAN);
vnetconfig->numaddrs = NUMBER_OF_HOSTS_PER_VLAN;
log_eucafault("1001", "component", "CC", NULL);
} else if (numberofaddrs_i <= NUMBER_OF_CCS) {
//! @fixme Why is NUMBER_OF_CCS not hard-coded here, but the absolute minimum setting of 16 is?
//! (Note: this 16 also appears hard-coded in the non-power-of-2 case.)
LOGWARN("specified ADDRSPERNET lower than absolute minimum (16), setting to minimum.\n");
vnetconfig->numaddrs = 16;
log_eucafault("1001", "component", "CC", NULL);
} else if (numberofaddrs_i && (!(numberofaddrs_i & (numberofaddrs_i - 1)))) {
// Is a legal power of 2.
vnetconfig->numaddrs = numberofaddrs_i;
} else {
bits = 1;
while (numberofaddrs_i >>= 1) {
bits <<= 1;
}
// Not a power of 2, so reduce to next power of 2 (but not below 16).
//! @fixme Use real address here!
vnetconfig->numaddrs = bits < 16 ? 16 : bits;
LOGWARN("specified ADDRSPERNET not a power of 2, setting to next lower power of 2 (%d).\n", vnetconfig->numaddrs);
log_eucafault("1001", "component", "CC", NULL);
}
}
vnetconfig->addrIndexMin = NUMBER_OF_CCS + 1;
vnetconfig->addrIndexMax = vnetconfig->numaddrs - 2;
if (network)
vnetconfig->nw = dot2hex(network);
if (netmask)
vnetconfig->nm = dot2hex(netmask);
// populate networks
bzero(vnetconfig->users, sizeof(userEntry) * NUMBER_OF_VLANS);
bzero(vnetconfig->networks, sizeof(networkEntry) * NUMBER_OF_VLANS);
bzero(vnetconfig->etherdevs, NUMBER_OF_VLANS * MAX_ETH_DEV_PATH);
bzero(vnetconfig->publicips, sizeof(publicip) * NUMBER_OF_PUBLIC_IPS);
if (role != NC) {
if (network)
nw = dot2hex(network);
if (netmask)
nm = dot2hex(netmask);
if (nameserver)
dns = dot2hex(nameserver);
if (broadcast)
bc = dot2hex(broadcast);
if (router)
rt = dot2hex(router);
if (numberofaddrs)
numaddrs = atoi(numberofaddrs);
numaddrs -= 1;
if (!strcmp(mode, "MANAGED") || !strcmp(mode, "MANAGED-NOVLAN")) {
// do some parameter checking
if ((numaddrs + 1) < 4) {
LOGERROR("NUMADDRS must be >= 4, instances will not start with current value of '%d'\n", numaddrs + 1);
}
// check to make sure our specified range is big enough for all VLANs
if ((0xFFFFFFFF - nm) < (NUMBER_OF_VLANS * (numaddrs + 1))) {
// not big enough
vnetconfig->max_vlan = (0xFFFFFFFF - nm) / (numaddrs + 1);
LOGWARN("private network is not large enough to support all vlans, restricting to max vlan '%d'\n", vnetconfig->max_vlan);
if (vnetconfig->max_vlan < 2) {
LOGWARN("Instances will not run with current max vlan '%d'. Either increase the size of your private subnet "
"(VNET_SUBNET/VNET_NETMASK) or decrease the number of addrs per group (VNET_ADDRSPERNET).\n", vnetconfig->max_vlan);
}
} else {
vnetconfig->max_vlan = NUMBER_OF_VLANS;
}
// set up iptables
snprintf(cmd, 256, EUCALYPTUS_ROOTWRAP " iptables -L -n", vnetconfig->eucahome);
rc = system(cmd);
LOGDEBUG("flushing 'filter' table\n");
rc = vnetApplySingleTableRule(vnetconfig, "filter", "-F");
LOGDEBUG("flushing 'nat' table\n");
rc = vnetApplySingleTableRule(vnetconfig, "nat", "-F");
if (path) {
vnetLoadIPTables(vnetconfig);
}
rc = vnetApplySingleTableRule(vnetconfig, "filter", "-P FORWARD DROP");
rc = vnetApplySingleTableRule(vnetconfig, "filter", "-A FORWARD -m conntrack --ctstate ESTABLISHED -j ACCEPT");
slashnet = 32 - ((int)log2((double)(0xFFFFFFFF - nm)) + 1);
snprintf(cmd, 256, "-A FORWARD ! -d %s/%d -j ACCEPT", network, slashnet);
rc = vnetApplySingleTableRule(vnetconfig, "filter", cmd);
snprintf(cmd, 256, "-A POSTROUTING ! -d %s/%d -s %s/%d -j MASQUERADE", network, slashnet, network, slashnet);
rc = vnetApplySingleTableRule(vnetconfig, "nat", cmd);
// snprintf(cmd, 256, "-A POSTROUTING -d %s/%d -j MASQUERADE", network, slashnet);
// rc = vnetApplySingleTableRule(vnetconfig, "nat", cmd);
// Provides rules for doing internal/external network reporting/stats.
snprintf(cmd, 256, "-N EUCA_COUNTERS_IN");
rc = vnetApplySingleTableRule(vnetconfig, "filter", cmd);
snprintf(cmd, 256, "-N EUCA_COUNTERS_OUT");
rc = vnetApplySingleTableRule(vnetconfig, "filter", cmd);
snprintf(cmd, 256, "-A EUCA_COUNTERS_IN -d %s/%d", network, slashnet);
rc = vnetApplySingleTableRule(vnetconfig, "filter", cmd);
snprintf(cmd, 256, "-A EUCA_COUNTERS_OUT -s %s/%d", network, slashnet);
rc = vnetApplySingleTableRule(vnetconfig, "filter", cmd);
snprintf(cmd, 256, "-I FORWARD -j EUCA_COUNTERS_IN");
rc = vnetApplySingleTableRule(vnetconfig, "filter", cmd);
snprintf(cmd, 256, "-I FORWARD -j EUCA_COUNTERS_OUT");
rc = vnetApplySingleTableRule(vnetconfig, "filter", cmd);
unm = 0xFFFFFFFF - numaddrs;
unw = nw;
for (vlan = 2; vlan < vnetconfig->max_vlan; vlan++) {
vnetconfig->networks[vlan].nw = unw;
vnetconfig->networks[vlan].nm = unm;
vnetconfig->networks[vlan].bc = unw + numaddrs;
vnetconfig->networks[vlan].dns = dns;
vnetconfig->networks[vlan].router = unw + 1;
unw += numaddrs + 1;
}
} else if (!strcmp(mode, "STATIC") || !strcmp(mode, "STATIC-DYNMAC")) {
for (vlan = 0; vlan < vnetconfig->max_vlan; vlan++) {
vnetconfig->networks[vlan].nw = nw;
vnetconfig->networks[vlan].nm = nm;
vnetconfig->networks[vlan].bc = bc;
vnetconfig->networks[vlan].dns = dns;
vnetconfig->networks[vlan].router = rt;
vnetconfig->numaddrs = 0xFFFFFFFF - nm;
if (vnetconfig->numaddrs > NUMBER_OF_PUBLIC_IPS) {
vnetconfig->numaddrs = NUMBER_OF_PUBLIC_IPS;
}
vnetconfig->addrIndexMin = NUMBER_OF_CCS + 1;
vnetconfig->addrIndexMax = vnetconfig->numaddrs - 2;
}
}
} else {
if (!strcmp(vnetconfig->mode, "SYSTEM")) {
// set up iptables rule to log DHCP replies to syslog
snprintf(cmd, 256, "-A FORWARD -p udp -m udp --sport 67:68 --dport 67:68 -j LOG --log-level 6");
rc = vnetApplySingleTableRule(vnetconfig, "filter", cmd);
if (rc) {
LOGWARN("could not add logging rule for DHCP replies, may not see instance IPs as they are assigned by system DHCP server");
}
}
if (strcmp(vnetconfig->mode, "MANAGED")) {
/*
// if we're not in MANAGED mode, set up ebtables
snprintf(cmd, 256, EUCALYPTUS_ROOTWRAP " ebtables -F FORWARD", vnetconfig->eucahome);
rc = system(cmd);
if (rc) {
LOGWARN("could not flush ebtables FORWARD rules\n");
}
snprintf(cmd, 256, EUCALYPTUS_ROOTWRAP " ebtables -P FORWARD DROP", vnetconfig->eucahome);
rc = system(cmd);
if (rc) {
LOGWARN("could set default ebtables FORWARD policy to DROP\n");
}
// forward non-VM traffic
snprintf(cmd, 256, "-A FORWARD -i %s -j ACCEPT", vnetconfig->pubInterface);
rc = vnetApplySingleEBTableRule(vnetconfig, "filter", cmd);
if (rc) {
LOGWARN("could set up default ebtables rule '%s'\n", cmd);
}
// allow VM DHCP traffic
snprintf(cmd, 256, "-A FORWARD -p IPv4 -d Broadcast -i ! %s --ip-proto udp --ip-dport 67:68 -j ACCEPT", vnetconfig->pubInterface);
rc = vnetApplySingleEBTableRule(vnetconfig, "filter", cmd);
if (rc) {
LOGWARN("could set up default ebtables rule '%s'\n", cmd);
}
*/
}
}
LOGINFO(" VNET Configuration: eucahome=%s,\n", SP(vnetconfig->eucahome));
LOGINFO(" path=%s,\n", SP(vnetconfig->path));
LOGINFO(" dhcpdaemon=%s,\n", SP(vnetconfig->dhcpdaemon));
LOGINFO(" dhcpuser=%s,\n", SP(vnetconfig->dhcpuser));
LOGINFO(" pubInterface=%s,\n", SP(vnetconfig->pubInterface));
LOGINFO(" privInterface=%s,\n", SP(vnetconfig->privInterface));
LOGINFO(" bridgedev=%s,\n", SP(vnetconfig->bridgedev));
LOGINFO(" networkMode=%s\n", SP(vnetconfig->mode));
}
return (EUCA_OK);
}
//!
//! Installs an IP table rule redirecting the metadata request towards the cloud controller.
//!
//! @param[in] vnetconfig a pointer to the Virtual Network Configuration information structure
//!
//! @return EUCA_OK on success or EUCA_INVALID_ERROR if any parameters does not meet the preconditions.
//!
//! @see vnetUnsetMetadataRedirect()
//!
//! @pre \li \p vnetconfig must not be NULL.
//! \li the cloud IP must be set
//!
//! @post The IP table rule has been installed on success. If any failure occured, the rule isn't is not
//! to be considered installed
//!
int vnetSetMetadataRedirect(vnetConfig * vnetconfig)
{
int rc = 0;
char cmd[256] = "";
char *ipbuf = NULL;
if (!vnetconfig) {
LOGERROR("bad input params: vnetconfig=%p\n", vnetconfig);
return (EUCA_INVALID_ERROR);
}
snprintf(cmd, 256, EUCALYPTUS_ROOTWRAP " ip addr add 169.254.169.254 scope link dev %s", vnetconfig->eucahome, vnetconfig->privInterface);
rc = system(cmd);
if (vnetconfig->cloudIp != 0) {
ipbuf = hex2dot(vnetconfig->cloudIp);
snprintf(cmd, 256, "-A PREROUTING -d 169.254.169.254/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination %s:8773", ipbuf);
EUCA_FREE(ipbuf);
rc = vnetApplySingleTableRule(vnetconfig, "nat", cmd);
} else {
LOGWARN("cloudIp is not yet set, not installing redirect rule\n");
}
return (EUCA_OK);
}
//!
//! Uninstall the metadata request redirect rule from the IP table
//!
//! @param[in] vnetconfig a pointer to the Virtual Network Configuration information structure
//!
//! @return EUCA_OK on success or EUCA_INVALID_ERROR if any parameters does not meet the preconditions.
//!
//! @see vnetSetMetadataRedirect()
//!
//! @pre \li \p vnetconfig must not be NULL.
//! \li the cloud IP must be set
//!
//! @post The IP table rule has been installed on success. If any failure occured, the rule isn't is not
//! to be considered installed
//!
int vnetUnsetMetadataRedirect(vnetConfig * vnetconfig)
{
int rc = 0;
char cmd[256] = "";
char *ipbuf = NULL;
if (!vnetconfig) {
LOGERROR("bad input params: vnetconfig=%p\n", vnetconfig);
return (EUCA_INVALID_ERROR);
}
snprintf(cmd, 256, EUCALYPTUS_ROOTWRAP " ip addr del 169.254.169.254 scope link dev %s", vnetconfig->eucahome, vnetconfig->privInterface);
rc = system(cmd);
if (vnetconfig->cloudIp != 0) {
ipbuf = hex2dot(vnetconfig->cloudIp);
snprintf(cmd, 256, "-D PREROUTING -d 169.254.169.254/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination %s:8773", ipbuf);
EUCA_FREE(ipbuf);
rc = vnetApplySingleTableRule(vnetconfig, "nat", cmd);
} else {
LOGWARN("cloudIp is not yet set, not installing redirect rule\n");
}
return (EUCA_OK);
}
//!
//!
//!
//! @param[in] vnetconfig a pointer to the Virtual Network Configuration information structure
//!
//! @return EUCA_OK on success or the following error code:
//! \li EUCA_ERROR: if we fail to initialize the tunelling
//! \li EUCA_INVALID_ERROR: if any parameters does not meet the preconditions
//!
//! @pre \p vnetconfig must not be NULL.
//!
int vnetInitTunnels(vnetConfig * vnetconfig)
{
int ret = EUCA_OK;
int rc = 0;
char file[MAX_PATH] = "";
char *template = NULL;
char *pass = NULL;
char *newl = NULL;
boolean done = FALSE;
if (!vnetconfig) {
LOGERROR("bad input params: vnetconfig=%p\n", vnetconfig);
return (EUCA_INVALID_ERROR);
}
vnetconfig->tunnels.tunneling = 0;
if (!strcmp(vnetconfig->mode, "MANAGED") || !strcmp(vnetconfig->mode, "MANAGED-NOVLAN")) {
if (vnetCountLocalIP(vnetconfig) <= 0) {
// localIp not set, no tunneling
LOGWARN("VNET_LOCALIP not set, tunneling is disabled\n");
return (EUCA_OK);
} else if (!strcmp(vnetconfig->mode, "MANAGED-NOVLAN") && check_bridge(vnetconfig->privInterface)) {
LOGWARN("in MANAGED-NOVLAN mode, priv interface '%s' must be a bridge, tunneling disabled\n", vnetconfig->privInterface);
return (EUCA_OK);
} else {
ret = EUCA_OK;
snprintf(file, MAX_PATH, EUCALYPTUS_KEYS_DIR "/vtunpass", vnetconfig->eucahome);
if (check_file(file)) {
LOGWARN("cannot locate tunnel password file '%s', tunneling disabled\n", file);
ret = EUCA_ERROR;
} else if (!check_file_newer_than(file, vnetconfig->tunnels.tunpassMtime)) {
ret = EUCA_ERROR;
LOGDEBUG("tunnel password file has changed, reading new value\n");
if ((pass = file2str(file)) != NULL) {
if ((newl = strchr(pass, '\n')) != NULL)
*newl = '\0';
snprintf(file, MAX_PATH, EUCALYPTUS_DATA_DIR "/vtunall.conf.template", vnetconfig->eucahome);
template = file2str(file);
if (template) {
euca_strreplace(&template, "VPASS", pass);
vnetconfig->tunnels.tunpassMtime = time(NULL);
done = TRUE;
}
EUCA_FREE(pass);
}
if (done) {
// success
snprintf(file, MAX_PATH, EUCALYPTUS_KEYS_DIR "/vtunall.conf", vnetconfig->eucahome);
if ((rc = write2file(file, template)) != 0) {
// error
LOGERROR("cannot write vtun config file '%s', tunneling disabled\n", file);
} else {
vnetconfig->tunnels.tunneling = 1;
ret = EUCA_OK;
}
} else {
LOGERROR("cannot set up tunnel configuration file, tunneling is disabled\n");
}
EUCA_FREE(template);
} else {
ret = EUCA_OK;
}
}
}
// enable tunneling if all went well
if (!ret) {
vnetconfig->tunnels.tunneling = 1;
}
return (ret);
}
//!
//! Adds a given host \p mac / \p ip to our network configuration.
//!
//! @param[in] vnetconfig a pointer to the Virtual Network Configuration information structure
//! @param[in] mac the host MAC address string
//! @param[in] ip the host IP address string
//! @param[in] vlan the Virtual LAN index
//! @param[in] idx the host index
//!
//! @return EUCA_OK on success or the following error codes;
//! \li EUCA_ERROR: if we fail to add the given host
//! \li EUCA_PERMISSION_ERROR: if virtual network support is not enabled
//! \li EUCA_INVALID_ERROR: if any parameters does not meet the preconditions
//!
//! @see vnetDelHost(), vnetRefreshHost()
//!
//! @pre \li \p vnetconfig and \p mac must not be NULL.
//! \li \p vlan must be between [0..NUMBER_OF_VLANS].
//! \li \p idx must be less than 0 or between [vnetconfig->addrIndexMin..vnetconfig->addrIndexMax].
//! \li \p virtual network support must be enabled.
//!
//! @post On success, the host has been added our our network
//!
int vnetAddHost(vnetConfig * vnetconfig, char *mac, char *ip, int vlan, int idx)
{
int i = 0;
int found = 0;
int start = 0;
int stop = 0;
char *newip = NULL;
boolean done = FALSE;
if (param_check("vnetAddHost", vnetconfig, mac, ip, vlan)) {
LOGERROR("bad input params: vnetconfig=%p, mac=%s, ip=%s, idx=%d\n", vnetconfig, SP(mac), SP(ip), idx);
return (EUCA_INVALID_ERROR);
}
if (!vnetconfig->enabled) {
LOGDEBUG("network support is not enabled\n");
return (EUCA_PERMISSION_ERROR);
}
if (idx < 0) {
start = vnetconfig->addrIndexMin;
stop = vnetconfig->addrIndexMax;
} else if ((idx >= vnetconfig->addrIndexMin) && (idx <= vnetconfig->addrIndexMax)) {
start = idx;
stop = idx;
} else {
LOGERROR("index out of bounds: idx=%d, min=%d max=%d\n", idx, vnetconfig->addrIndexMin, vnetconfig->addrIndexMax);
return (EUCA_INVALID_ERROR);
}
for (i = start, done = FALSE, found = 0; ((i <= stop) && !done); i++) {
if (!maczero(vnetconfig->networks[vlan].addrs[i].mac)) {
if (!found)
found = i;
} else if (!machexcmp(mac, vnetconfig->networks[vlan].addrs[i].mac)) {
done = TRUE;
}
}
if (done) {
// duplicate IP found
LOGWARN("attempting to add duplicate macmap entry, ignoring\n");
} else if (found) {
if (mac2hex(mac, vnetconfig->networks[vlan].addrs[found].mac) != NULL) {
if (ip) {
vnetconfig->networks[vlan].addrs[found].ip = dot2hex(ip);
} else {
if ((newip = hex2dot(vnetconfig->networks[vlan].nw + found)) == NULL) {
LOGWARN("Out of memory\n");
} else {
vnetconfig->networks[vlan].addrs[found].ip = dot2hex(newip);
EUCA_FREE(newip);
}
}
vnetconfig->networks[vlan].numhosts++;
} else {
LOGERROR("failed to convers mac address '%s on vlan %d'\n", mac, vlan);
}
} else {
LOGERROR("failed to add host %s on vlan %d\n", mac, vlan);
return (EUCA_ERROR);
}
return (EUCA_OK);
}
//!
//! Deletes a given host \p mac / \p ip address from our network configuration
//!
//! @param[in] vnetconfig a pointer to the Virtual Network Configuration information structure
//! @param[in] mac the host mac address string
//! @param[in] ip the host IP address string
//! @param[in] vlan the Virtual LAN index
//!
//! @return EUCA_OK on success or the following error codes:
//! \li EUCA_INVALID_ERROR: if any parameters does not meet the preconditions
//! \li EUCA_NOT_FOUND_ERROR: if we fail to find the host in our list
//! \li EUCA_PERMISSION_ERROR: if virtual network support is not enabled
//!
//! @see vnetAddHost(), vnetRefreshHost()
//!
//! @pre \li \p vnetconfig must not be NULL.
//! \li \p mac and/or \p ip must not be NULL.
//! \li \p vlan must be in the [0..NUMBER_OF_VLANS] range.
//! \li \p virtual network support must be enabled.
//!
//! @post on success, the host has been removed from our configuration
//!
int vnetDelHost(vnetConfig * vnetconfig, char *mac, char *ip, int vlan)
{
int i = 0;
if (param_check("vnetDelHost", vnetconfig, mac, ip, vlan)) {
LOGERROR("bad input params: vnetconfig=%p, mac=%s, ip=%s, vlan=%d\n", vnetconfig, SP(mac), SP(ip), vlan);
return (EUCA_INVALID_ERROR);
}
if (!vnetconfig->enabled) {
LOGDEBUG("network support is not enabled\n");
return (EUCA_PERMISSION_ERROR);
}
for (i = vnetconfig->addrIndexMin; i <= vnetconfig->addrIndexMax; i++) {
if ((!mac || !machexcmp(mac, vnetconfig->networks[vlan].addrs[i].mac)) && (!ip || (vnetconfig->networks[vlan].addrs[i].ip == dot2hex(ip)))) {
bzero(&(vnetconfig->networks[vlan].addrs[i]), sizeof(netEntry));
vnetconfig->networks[vlan].numhosts--;
return (EUCA_OK);
}
}
return (EUCA_NOT_FOUND_ERROR);
}
//!
//!
//!
//! @param[in] vnetconfig a pointer to the Virtual Network Configuration information structure
//! @param[in] mac
//! @param[in] ip
//! @param[in] vlan the Virtual LAN index
//! @param[in] idx
//!
//! @return If the host is not part of our list, the result of vnetAddHost() is returned. In
//! any other cases, EUCA_OK on success is returned or the following error codes:
//! \li EUCA_INVALID_ERROR: if any parameters does not meet the preconditions
//! \li EUCA_PERMISSION_ERROR: if the virtual network support is disabled
//!
//! @see vnetAddHost(), vnetDelHost()
//!
//! @pre \li \p vnetconfig must not be NULL.
//! \li \p mac and/or \p ip must not be NULL.
//! \li \p vlan must be in the [0..NUMBER_OF_VLAN] range.
//! \li \p idx must be less than 0 or in the [vnetconfig->addrIndexMin..vnetconfig->addrIndexMax] range.
//! \li \p virtual network support must be enabled.
//!
int vnetRefreshHost(vnetConfig * vnetconfig, char *mac, char *ip, int vlan, int idx)
{
int i = 0;
int found = 0;
int start = 0;
int stop = 0;
boolean done = FALSE;
if ((vnetconfig == NULL) || ((mac == NULL) && (ip == NULL)) || (vlan < 0) || (vlan >= NUMBER_OF_VLANS)) {
LOGERROR("bad input params: vnetconfig=%p, mac=%s, ip=%s, vlan=%d, idx=%d\n", vnetconfig, SP(mac), SP(ip), vlan, idx);
return (EUCA_INVALID_ERROR);
}
if (!vnetconfig->enabled) {
LOGDEBUG("network support is not enabled\n");
return (EUCA_PERMISSION_ERROR);
}
if (idx < 0) {
start = vnetconfig->addrIndexMin;
stop = vnetconfig->addrIndexMax;
} else if (idx >= vnetconfig->addrIndexMin && idx <= (vnetconfig->addrIndexMax)) {
start = idx;
stop = idx;
} else {
LOGERROR("index out of bounds: idx=%d, min=%d max=%d\n", idx, vnetconfig->addrIndexMin, vnetconfig->addrIndexMax);
return (EUCA_INVALID_ERROR);
}
for (i = start, done = FALSE, found = 0; ((i <= stop) && !done); i++) {
if (ip) {
if (vnetconfig->networks[vlan].addrs[i].ip == dot2hex(ip)) {
found = i;
done = TRUE;
}
}
if (mac) {
if (!machexcmp(mac, vnetconfig->networks[vlan].addrs[i].mac)) {
found = i;
done = TRUE;
}
}
}
if (!done) {
return (vnetAddHost(vnetconfig, mac, ip, vlan, idx));
} else {
if (mac) {
mac2hex(mac, vnetconfig->networks[vlan].addrs[found].mac);
}
if (ip) {
vnetconfig->networks[vlan].addrs[found].ip = dot2hex(ip);
}
}
return (EUCA_OK);
}
//!
//! Activate networking for a given host
//!
//! @param[in] vnetconfig a pointer to the Virtual Network Configuration information structure
//! @param[in] mac
//! @param[in] ip
//! @param[in] vlan the Virtual LAN index
//!
//! @return EUCA_OK on success or the following error codes:
//! \li EUCA_INVALID_ERROR: if any parameters does not meet the preconditions
//! \li EUCA_NOT_FOUND_ERROR: if we fail to find the host
//! \li EUCA_PERMISSION_ERROR: if the virtual network support is not available
//!
//! @see vnetDisableHost()
//!
//! @pre \li \p vnetconfig must not be NULL.
//! \li \p mac and/or \p ip must not be NULL.
//! \li \p vlan must be in the [0..NUMBER_OF_VLANS] range.
//! \li virtual network support must be enabled.
//!
int vnetEnableHost(vnetConfig * vnetconfig, char *mac, char *ip, int vlan)
{
int i = 0;
if (param_check("vnetEnableHost", vnetconfig, mac, ip, vlan)) {
LOGERROR("bad input params: vnetconfig=%p, mac=%s, ip=%s, vlan=%d\n", vnetconfig, SP(mac), SP(ip), vlan);
return (EUCA_INVALID_ERROR);
}
if (!vnetconfig->enabled) {
LOGDEBUG("network support is not enabled\n");
return (EUCA_PERMISSION_ERROR);
}
for (i = vnetconfig->addrIndexMin; i <= vnetconfig->addrIndexMax; i++) {
if ((!mac || !machexcmp(mac, vnetconfig->networks[vlan].addrs[i].mac)) && (!ip || (vnetconfig->networks[vlan].addrs[i].ip == dot2hex(ip)))) {
vnetconfig->networks[vlan].addrs[i].active = 1;
return (EUCA_OK);
}
}
return (EUCA_NOT_FOUND_ERROR);
}
//!
//! Deactivate networking for a given host