diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 286f2ae787..08c1d3e21a 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -177,6 +177,15 @@ public: */ string& to_xml(string& xml) const; + /** + * Function to print the object into a string in XML format + * base64 encoded + * @param xml64 the resulting XML string + * @param extended return the extended template or the simple one + * @return a reference to the generated string + */ + string& to_xml64(string &xml64, bool extended); + /** * Function to print the VirtualNetwork object into a string in * XML format. The extended XML includes the LEASES diff --git a/share/router/vmcontext b/share/router/vmcontext new file mode 100644 index 0000000000..1cc627840a --- /dev/null +++ b/share/router/vmcontext @@ -0,0 +1,6 @@ +#!/sbin/runscript + +start() { + /etc/init.d/vmcontext.rb + return 0 +} diff --git a/share/router/init.rb b/share/router/vmcontext.rb similarity index 62% rename from share/router/init.rb rename to share/router/vmcontext.rb index fbae8b5b1b..14ac41dad9 100644 --- a/share/router/init.rb +++ b/share/router/vmcontext.rb @@ -19,13 +19,16 @@ require 'rexml/document' require 'base64' require 'fileutils' +require 'erb' class Router # Default files + FILES = { :resolv_conf => "/etc/resolv.conf", :context => "/mnt/context/context.sh", :dnsmasq_conf => "/etc/dnsmasq.conf", + :radvd_conf => "/etc/radvd.conf", :log_file => "/var/log/router.log" } @@ -39,7 +42,10 @@ class Router DEFAULT_NETMASK = "255.255.255.0" # Context parameters that are base64 encoded - BASE_64_KEYS = [:privnet, :pubnet, :template] + BASE_64_KEYS = [:privnet, :pubnet, :template, :root_password] + + # Context parameters that are XML documents + XML_KEYS = [:privnet, :pubnet, :template] # The specification on how to fetch these attributes. # The order in the array matters, the first non-empty one is returned. @@ -65,7 +71,7 @@ class Router :nat => [ { :resource => :context, - :resource_name => "FORWARDING" + :resource_name => :forwarding }, { :resource => :privnet, @@ -81,13 +87,27 @@ class Router :resource_xpath => 'TEMPLATE/DHCP' } ], - :ntp => [ + :radvd => [ { :resource => :context }, { :resource => :privnet, - :resource_xpath => 'TEMPLATE/NTP' + :resource_xpath => 'TEMPLATE/RADVD' + } + ], + :ntp_server => [ + { + :resource => :context + }, + { + :resource => :privnet, + :resource_xpath => 'TEMPLATE/NTP_SERVER' + } + ], + :ssh_public_key => [ + { + :resource => :context } ], :root_pubkey => [ @@ -103,8 +123,9 @@ class Router } def initialize + mount_context @context = read_context - unpack + unpack if @context get_network_information end @@ -134,7 +155,7 @@ class Router end def root_pubkey - get_attribute(:root_pubkey) + get_attribute(:root_pubkey) || get_attribute(:ssh_public_key) end def nat @@ -151,19 +172,26 @@ class Router end end - def ntp - ntp_raw = get_attribute(:ntp) || "" - if ntp_raw.downcase.match(/^y(es)?$/) - true - else - false - end + def ntp_server + get_attribute(:ntp_server) + end + + def radvd + radvd_raw = get_attribute(:radvd) || "" + radvd = !!radvd_raw.downcase.match(/^y(es)?$/) + radvd and @privnet[:ipv6] end ############################################################################ # ACTIONS ############################################################################ + def mount_context + log("mounting context") + FileUtils.mkdir_p("/mnt/context") + run "mount -t iso9660 /dev/cdrom /mnt/context 2>/dev/null" + end + def write_resolv_conf if dns File.open(FILES[:resolv_conf], 'w') do |resolv_conf| @@ -183,43 +211,80 @@ class Router end def configure_network - get_network_information + if has_context? + log("has context") + configure_network_context + else + log("no context") + configure_network_static + end + end - if pubnet - ip = @pubnet[:ip] - nic = @pubnet[:interface] - netmask = @pubnet[:netmask] - gateway = @pubnet[:gateway] + def configure_network_static + macs = @mac_interfaces.invert + + defaultgw = true + macs.keys.sort.each do |nic| + next if nic !~ /^eth/ + + mac = macs[nic] + ip = mac2ip(mac) + gateway = ip.gsub(/\.\d{1,3}$/,".#{DEFAULT_GW}") run "ip link set #{nic} up" + run "ip addr add #{ip}/24 dev #{nic}" + + if defaultgw + defaultgw = false + run "ip route add default via #{gateway}" + end + end + end + + def configure_network_context + if pubnet + ip = @pubnet[:ip] + ip6_global = @pubnet[:ip6_global] + ip6_site = @pubnet[:ip6_site] + nic = @pubnet[:interface] + netmask = @pubnet[:netmask] + gateway = @pubnet[:gateway] + + run "ip link set #{nic} up" + run "ip addr add #{ip}/#{netmask} dev #{nic}" + run "ip addr add #{ip6_global} dev #{nic}" if ip6_global + run "ip addr add #{ip6_site} dev #{nic}" if ip6_site + run "ip route add default via #{gateway}" end if privnet - ip = @privnet[:ip] - nic = @privnet[:interface] - netmask = @privnet[:netmask] + ip = @privnet[:ip] + ip6_global = @privnet[:ip6_global] + ip6_site = @privnet[:ip6_site] + nic = @privnet[:interface] + netmask = @privnet[:netmask] run "ip link set #{nic} up" run "ip addr add #{ip}/#{netmask} dev #{nic}" + run "ip addr add #{ip6_global} dev #{nic}" if ip6_global + run "ip addr add #{ip6_site} dev #{nic}" if ip6_site end end - def configure_dnsmasq - File.open(FILES[:dnsmasq_conf],'w') do |dnsmasq_conf| + File.open(FILES[:dnsmasq_conf],'w') do |conf| _,ip_start,_ = dhcp_ip_mac_pairs[0] _,ip_end,_ = dhcp_ip_mac_pairs[-1] - dnsmasq_conf.puts "dhcp-range=#{ip_start},#{ip_end},infinite" - dnsmasq_conf.puts - dnsmasq_conf.puts "dhcp-option=42,#{@privnet[:ip]} # ntp server" - dnsmasq_conf.puts "dhcp-option=4,#{@privnet[:ip]} # name server" - dnsmasq_conf.puts + conf.puts "dhcp-range=#{ip_start},#{ip_end},infinite" + conf.puts "dhcp-option=42,#{ntp_server} # ntp server" if ntp_server + conf.puts "dhcp-option=4,#{@privnet[:ip]} # name server" + dhcp_ip_mac_pairs.each do |pair| mac, ip = pair - dnsmasq_conf.puts "dhcp-host=#{mac},#{ip}" + conf.puts "dhcp-host=#{mac},#{ip}" end end end @@ -239,6 +304,27 @@ class Router end end + def configure_radvd + prefixes = [@privnet[:ip6_global],@privnet[:ip6_site]].compact + privnet_iface = @privnet[:interface] + + radvd_conf_tpl =<<-EOF.gsub(/^\s{12}/,"") + interface <%= privnet_iface %> + { + AdvSendAdvert on; + <% prefixes.each do |p| %> + prefix <%= p %>/64 + { + AdvOnLink on; + }; + <% end %> + }; + EOF + + radvd_conf = ERB.new(radvd_conf_tpl).result(binding) + File.open(FILES[:radvd_conf],'w') {|c| c.puts radvd_conf } + end + def configure_masquerade run "iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE" end @@ -248,7 +334,7 @@ class Router end def configure_root_password - run "usermod -p #{root_password} root" + run "echo -n 'root:#{root_password}'|chpasswd -e" end def configure_root_pubkey @@ -260,7 +346,12 @@ class Router def service(service, action = :start) action = action.to_s - run "/etc/rc.d/#{service} #{action}" + run "/etc/init.d/#{service} #{action}" + end + + def log(msg, command = false) + msg = "=> #{msg}" unless command + File.open(FILES[:log_file],'a') {|f| f.puts msg} end ############################################################################ @@ -269,26 +360,38 @@ class Router private + def has_context? + !@context.nil? + end + def get_network_information @pubnet = Hash.new @privnet = Hash.new - mac_interfaces = Hash[ + @mac_interfaces = Hash[ Dir["/sys/class/net/*/address"].collect do |f| [ File.read(f).strip, f.split('/')[4] ] end ] + return if !has_context? + if (pubnet_id = get_element_xpath(:pubnet, 'ID')) @pubnet[:network_id] = pubnet_id - xpath_ip = "TEMPLATE/NIC[NETWORK_ID='#{pubnet_id}']/IP" - xpath_mac = "TEMPLATE/NIC[NETWORK_ID='#{pubnet_id}']/MAC" + xpath_ip = "TEMPLATE/NIC[NETWORK_ID='#{pubnet_id}']/IP" + xpath_ip6_global = "TEMPLATE/NIC[NETWORK_ID='#{pubnet_id}']/IP6_GLOBAL" + xpath_ip6_site = "TEMPLATE/NIC[NETWORK_ID='#{pubnet_id}']/IP6_SITE" + xpath_mac = "TEMPLATE/NIC[NETWORK_ID='#{pubnet_id}']/MAC" - @pubnet[:ip] = get_element_xpath(:template, xpath_ip) - @pubnet[:mac] = get_element_xpath(:template, xpath_mac) + @pubnet[:ip] = get_element_xpath(:template, xpath_ip) + @pubnet[:ip6_global] = get_element_xpath(:template, xpath_ip6_global) + @pubnet[:ip6_site] = get_element_xpath(:template, xpath_ip6_site) + @pubnet[:mac] = get_element_xpath(:template, xpath_mac) - @pubnet[:interface] = mac_interfaces[@pubnet[:mac]] + @pubnet[:ipv6] = true if @pubnet[:ip6_global] or @pubnet[:ip6_site] + + @pubnet[:interface] = @mac_interfaces[@pubnet[:mac]] netmask = get_element_xpath(:pubnet, 'TEMPLATE/NETWORK_MASK') @pubnet[:netmask] = netmask || DEFAULT_NETMASK @@ -303,25 +406,27 @@ private if (privnet_id = get_element_xpath(:privnet, 'ID')) @privnet[:network_id] = privnet_id - xpath_ip = "TEMPLATE/NIC[NETWORK_ID='#{privnet_id}']/IP" - xpath_mac = "TEMPLATE/NIC[NETWORK_ID='#{privnet_id}']/MAC" + xpath_ip = "TEMPLATE/NIC[NETWORK_ID='#{privnet_id}']/IP" + xpath_ip6_global = "TEMPLATE/NIC[NETWORK_ID='#{privnet_id}']/IP6_GLOBAL" + xpath_ip6_site = "TEMPLATE/NIC[NETWORK_ID='#{privnet_id}']/IP6_SITE" + xpath_mac = "TEMPLATE/NIC[NETWORK_ID='#{privnet_id}']/MAC" - @privnet[:ip] = get_element_xpath(:template, xpath_ip) - @privnet[:mac] = get_element_xpath(:template, xpath_mac) + @privnet[:ip] = get_element_xpath(:template, xpath_ip) + @privnet[:ip6_global] = get_element_xpath(:template, xpath_ip6_global) + @privnet[:ip6_site] = get_element_xpath(:template, xpath_ip6_site) + @privnet[:mac] = get_element_xpath(:template, xpath_mac) - @privnet[:interface] = mac_interfaces[@privnet[:mac]] + @privnet[:ipv6] = true if @privnet[:ip6_global] or @privnet[:ip6_site] + + @privnet[:interface] = @mac_interfaces[@privnet[:mac]] netmask = get_element_xpath(:privnet, 'TEMPLATE/NETWORK_MASK') @privnet[:netmask] = netmask || DEFAULT_NETMASK end end - def log(msg) - File.open(FILES[:log_file],'a') {|f| f.puts msg} - end - def run(cmd) - log(cmd) + log(cmd, true) output = `#{cmd} 2>&1` exitstatus = $?.exitstatus log(output) if !output.empty? @@ -341,12 +446,20 @@ private ip = int_to_ip(int_ip) mac = ip2mac(ip) + used_node = netxml.elements["LEASES/LEASE[IP='#{ip}']/USED"] + used = (used_node.text.to_i == 1 if used_node) || false + next if used + pairs << [mac, ip, int_ip] end elsif netxml.elements['LEASES/LEASE'] netxml.elements.each('LEASES/LEASE') do |lease| - ip = lease.elements['IP'].text - mac = lease.elements['MAC'].text + used = lease.elements['USED'].text.to_i == 1 + next if used + + ip = lease.elements['IP'].text + mac = lease.elements['MAC'].text + int_ip = ip_to_int(ip) pairs << [mac, ip, int_ip] @@ -387,8 +500,13 @@ private BASE_64_KEYS.each do |key| if @context.include? key - tpl = Base64::decode64(@context[key]) - @xml[key] = REXML::Document.new(tpl).root + @context[key] = Base64::decode64(@context[key]) + end + end + + XML_KEYS.each do |key| + if @context.include? key + @xml[key] = REXML::Document.new(@context[key]).root end end end @@ -397,6 +515,7 @@ private order = ATTRIBUTES[name] return nil if order.nil? + return nil if !has_context? order.each do |e| if e[:resource] != :context @@ -430,6 +549,8 @@ private end def read_context + return nil if !File.exists?(FILES[:context]) + context = Hash.new context_file = File.read(FILES[:context]) @@ -448,40 +569,48 @@ end router = Router.new +router.log("configure network") router.configure_network if router.pubnet if router.dns || router.search + router.log("write resolv.conf") router.write_resolv_conf end # Set masquerade + router.log("set masquerade") router.configure_masquerade # Set ipv4 forward + router.log("ip forward") router.configure_ip_forward # Set NAT rules if router.nat + router.log("configure nat") router.configure_nat end end -if router.privnet - if router.dhcp - router.configure_dnsmasq - router.service("dnsmasq") - end +if router.privnet and router.dhcp + router.log("configure dnsmasq") + router.configure_dnsmasq + router.service("dnsmasq") +end - if router.ntp - router.service("ntpd") - end +if router.radvd + router.log("configure radvd") + router.configure_radvd + router.service("radvd") end if router.root_password + router.log("configure root password") router.configure_root_password end if router.root_pubkey + router.log("configure root pubkey") router.configure_root_pubkey end diff --git a/src/vm/vm_var_syntax.cc b/src/vm/vm_var_syntax.cc index 71ecb6c849..3579984451 100644 --- a/src/vm/vm_var_syntax.cc +++ b/src/vm/vm_var_syntax.cc @@ -1,8 +1,8 @@ -/* A Bison parser, made by GNU Bison 2.7. */ +/* A Bison parser, made by GNU Bison 2.7.12-4996. */ /* Bison implementation for Yacc-like parsers in C - Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc. + Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, 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 @@ -44,7 +44,7 @@ #define YYBISON 1 /* Bison version. */ -#define YYBISON_VERSION "2.7" +#define YYBISON_VERSION "2.7.12-4996" /* Skeleton name. */ #define YYSKELETON_NAME "yacc.c" @@ -287,7 +287,7 @@ void get_network_attribute(VirtualMachine * vm, if (attr_name == "TEMPLATE") { - attr_value = vn->to_xml64(attr_value); + attr_value = vn->to_xml64(attr_value, true); } else { @@ -637,6 +637,14 @@ typedef short int yytype_int16; # endif #endif +#ifndef __attribute__ +/* This feature is available in gcc versions 2.5 and later. */ +# if (! defined __GNUC__ || __GNUC__ < 2 \ + || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)) +# define __attribute__(Spec) /* empty */ +# endif +#endif + /* Suppress unused-variable warnings by "using" E. */ #if ! defined lint || defined __GNUC__ # define YYUSE(E) ((void) (E)) @@ -644,6 +652,7 @@ typedef short int yytype_int16; # define YYUSE(E) /* empty */ #endif + /* Identity function, used to suppress warnings about constant conditions. */ #ifndef lint # define YYID(N) (N) @@ -1162,11 +1171,7 @@ yy_symbol_value_print (yyoutput, yytype, yyvaluep, yylocationp, mc, vm, parsed, # else YYUSE (yyoutput); # endif - switch (yytype) - { - default: - break; - } + YYUSE (yytype); } @@ -1578,12 +1583,7 @@ yydestruct (yymsg, yytype, yyvaluep, yylocationp, mc, vm, parsed, errmsg) yymsg = "Deleting"; YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp); - switch (yytype) - { - - default: - break; - } + YYUSE (yytype); } @@ -1910,7 +1910,7 @@ yyreduce: switch (yyn) { case 4: -/* Line 1792 of yacc.c */ +/* Line 1787 of yacc.c */ #line 456 "vm_var_syntax.y" { (*parsed) << (yyvsp[(1) - (1)].val_str); @@ -1918,7 +1918,7 @@ yyreduce: break; case 5: -/* Line 1792 of yacc.c */ +/* Line 1787 of yacc.c */ #line 460 "vm_var_syntax.y" { string name((yyvsp[(1) - (2)].val_str)); @@ -1935,7 +1935,7 @@ yyreduce: break; case 6: -/* Line 1792 of yacc.c */ +/* Line 1787 of yacc.c */ #line 473 "vm_var_syntax.y" { string name((yyvsp[(1) - (5)].val_str)); @@ -1954,7 +1954,7 @@ yyreduce: break; case 7: -/* Line 1792 of yacc.c */ +/* Line 1787 of yacc.c */ #line 488 "vm_var_syntax.y" { string name((yyvsp[(1) - (9)].val_str)); @@ -1976,7 +1976,7 @@ yyreduce: break; -/* Line 1792 of yacc.c */ +/* Line 1787 of yacc.c */ #line 1981 "vm_var_syntax.cc" default: break; } @@ -2215,7 +2215,7 @@ yyreturn: } -/* Line 2055 of yacc.c */ +/* Line 2050 of yacc.c */ #line 506 "vm_var_syntax.y" diff --git a/src/vm/vm_var_syntax.h b/src/vm/vm_var_syntax.h index d41fdb34c1..3d71ff86c0 100644 --- a/src/vm/vm_var_syntax.h +++ b/src/vm/vm_var_syntax.h @@ -1,8 +1,8 @@ -/* A Bison parser, made by GNU Bison 2.7. */ +/* A Bison parser, made by GNU Bison 2.7.12-4996. */ /* Bison interface for Yacc-like parsers in C - Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc. + Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, 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 @@ -62,7 +62,7 @@ extern int vm_var__debug; #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED typedef union YYSTYPE { -/* Line 2058 of yacc.c */ +/* Line 2053 of yacc.c */ #line 427 "vm_var_syntax.y" char * val_str; @@ -70,7 +70,7 @@ typedef union YYSTYPE char val_char; -/* Line 2058 of yacc.c */ +/* Line 2053 of yacc.c */ #line 75 "vm_var_syntax.hh" } YYSTYPE; # define YYSTYPE_IS_TRIVIAL 1 diff --git a/src/vm/vm_var_syntax.y b/src/vm/vm_var_syntax.y index 294bf32d9c..02c393790b 100644 --- a/src/vm/vm_var_syntax.y +++ b/src/vm/vm_var_syntax.y @@ -229,7 +229,7 @@ void get_network_attribute(VirtualMachine * vm, if (attr_name == "TEMPLATE") { - attr_value = vn->to_xml64(attr_value); + attr_value = vn->to_xml64(attr_value, true); } else { diff --git a/src/vnm/Leases.cc b/src/vnm/Leases.cc index 05ee665fed..2dea794bdc 100644 --- a/src/vnm/Leases.cc +++ b/src/vnm/Leases.cc @@ -109,7 +109,7 @@ void Leases::Lease::ip6_to_string(const unsigned int eui64[], ip6.s6_addr32[3] = htonl(eui64[0]); // global or site unicast address - if (prefix[1] != 0 && prefix[0] != 0 ) + if (prefix[1] != 0 || prefix[0] != 0 ) { ip6.s6_addr32[0] = htonl(prefix[1]); ip6.s6_addr32[1] = htonl(prefix[0]); @@ -177,7 +177,6 @@ int Leases::Lease::mac_to_number(const string& _mac, unsigned int i_mac[]) int Leases::Lease::prefix6_to_number(const string& prefix, unsigned int ip[]) { struct in6_addr s6; - ostringstream oss; if (prefix.empty()) { @@ -185,9 +184,7 @@ int Leases::Lease::prefix6_to_number(const string& prefix, unsigned int ip[]) return 0; } - oss << prefix << ":0:0:0:1"; - - int rc = inet_pton(AF_INET6, oss.str().c_str(), &s6); + int rc = inet_pton(AF_INET6, prefix.c_str(), &s6); if ( rc != 1 ) { diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 94d60dcee0..aa7b7ef23c 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -26,6 +26,8 @@ #include "AuthManager.h" #include "ClusterPool.h" +#include "NebulaUtil.h" + #define TO_UPPER(S) transform(S.begin(),S.end(),S.begin(),(int(*)(int))toupper) /* ************************************************************************** */ @@ -494,6 +496,24 @@ string& VirtualNetwork::to_xml(string& xml) const /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +string& VirtualNetwork::to_xml64(string &xml64, bool extended) +{ + string *str64; + + to_xml_extended(xml64, extended); + + str64 = one_util::base64_encode(xml64); + + xml64 = *str64; + + delete str64; + + return xml64; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + string& VirtualNetwork::to_xml_extended(string& xml) const { return to_xml_extended(xml,true);