X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=src%2FPVE%2FNetwork.pm;h=3a0d778dc38bb66012b5a21395d629548be55db4;hp=ab073505710150a5a24bea052e52070117fe99a6;hb=f3ccd9b4b9bad4c762c63ef144600c4e7016640f;hpb=e43faad9ff27f4d252d5604439c7aab1ff1c6251 diff --git a/src/PVE/Network.pm b/src/PVE/Network.pm index ab07350..3a0d778 100644 --- a/src/PVE/Network.pm +++ b/src/PVE/Network.pm @@ -2,7 +2,7 @@ package PVE::Network; use strict; use warnings; -use PVE::Tools qw(run_command); +use PVE::Tools qw(run_command lock_file); use PVE::ProcFSTools; use PVE::INotify; use File::Basename; @@ -11,12 +11,6 @@ use POSIX qw(ECONNREFUSED); use Net::IP; -use Socket qw(IPPROTO_IP); - -use constant IFF_UP => 1; -use constant IFNAMSIZ => 16; -use constant SIOCGIFFLAGS => 0x8913; - # host network related utility functions our $ipv4_reverse_mask = [ @@ -56,6 +50,14 @@ our $ipv4_reverse_mask = [ ]; our $ipv4_mask_hash_localnet = { + '255.0.0.0' => 8, + '255.128.0.0' => 9, + '255.192.0.0' => 10, + '255.224.0.0' => 11, + '255.240.0.0' => 12, + '255.248.0.0' => 13, + '255.252.0.0' => 14, + '255.254.0.0' => 15, '255.255.0.0' => 16, '255.255.128.0' => 17, '255.255.192.0' => 18, @@ -169,9 +171,20 @@ my $cond_create_bridge = sub { } }; +sub disable_ipv6 { + my ($iface) = @_; + return if !-d '/proc/sys/net/ipv6'; # ipv6 might be completely disabled + my $file = "/proc/sys/net/ipv6/conf/$iface/disable_ipv6"; + open(my $fh, '>', $file) or die "failed to open $file for writing: $!\n"; + print {$fh} "1\n" or die "failed to disable link-local ipv6 for $iface\n"; + close($fh); +} + my $bridge_add_interface = sub { my ($bridge, $iface, $tag, $trunks) = @_; + # drop link local address (it can't be used when on a bridge anyway) + disable_ipv6($iface); system("/sbin/brctl addif $bridge $iface") == 0 || die "can't add interface 'iface' to bridge '$bridge'\n"; @@ -179,8 +192,10 @@ my $bridge_add_interface = sub { if ($vlan_aware) { if ($tag) { - system("/sbin/bridge vlan add dev $iface vid $tag pvid untagged") == 0 || - die "unable to add vlan $tag to interface $iface\n"; + system({'/sbin/bridge'} 'bridge', 'vlan', 'del', 'dev', $iface, 'vid', '1-4094') == 0 + or die "failed to remove default vlan tags of $iface\n"; + system({'/sbin/bridge'} 'bridge', 'vlan', 'add', 'dev', $iface, 'vid', $tag, 'pvid', 'untagged') == 0 + or die "unable to add vlan $tag to interface $iface\n"; warn "Caution: Setting VLAN ID 1 on a VLAN aware bridge may be dangerous\n" if $tag == 1; } else { @@ -211,6 +226,7 @@ my $ovs_bridge_add_port = sub { $cmd .= " -- set Interface $iface type=internal" if $internal; system($cmd) == 0 || die "can't add ovs port '$iface'\n"; + disable_ipv6($iface); }; my $activate_interface = sub { @@ -228,6 +244,7 @@ sub tap_create { my $bridgemtu = &$read_bridge_mtu($bridge); eval { + disable_ipv6($iface); PVE::Tools::run_command("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu"); }; die "interface activation failed\n" if $@; @@ -248,6 +265,8 @@ sub veth_create { } # up vethpair + disable_ipv6($veth); + disable_ipv6($vethpeer); &$activate_interface($veth); &$activate_interface($vethpeer); } @@ -268,6 +287,7 @@ my $create_firewall_bridge_linux = sub { my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid); &$cond_create_bridge($fwbr); + disable_ipv6($fwbr); &$activate_interface($fwbr); copy_bridge_config($bridge, $fwbr); @@ -288,6 +308,7 @@ my $create_firewall_bridge_ovs = sub { my $bridgemtu = &$read_bridge_mtu($bridge); &$cond_create_bridge($fwbr); + disable_ipv6($fwbr); &$activate_interface($fwbr); &$bridge_add_interface($fwbr, $iface); @@ -406,10 +427,13 @@ sub activate_bridge_vlan_slave { # create vlan on $iface is not already exist if (! -d "/sys/class/net/$ifacevlan") { - system("/sbin/ip link add link $iface name ${iface}.${tag} type vlan id $tag") == 0 || + system("/sbin/ip link add link $iface name $ifacevlan type vlan id $tag") == 0 || die "can't add vlan tag $tag to interface $iface\n"; } + # remove ipv6 link-local address before activation + disable_ipv6($ifacevlan); + # be sure to have the $ifacevlan up &$activate_interface($ifacevlan); @@ -450,23 +474,25 @@ sub activate_bridge_vlan { die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0; - # add bridgevlan if it doesn't already exist - if (! -d "/sys/class/net/$bridgevlan") { - system("/sbin/brctl addbr $bridgevlan") == 0 || - die "can't add bridge $bridgevlan\n"; - } + lock_network(sub { + # add bridgevlan if it doesn't already exist + if (! -d "/sys/class/net/$bridgevlan") { + system("/sbin/brctl addbr $bridgevlan") == 0 || + die "can't add bridge $bridgevlan\n"; + } - # for each physical interface (eth or bridge) bind them to bridge vlan - foreach my $iface (@ifaces) { - activate_bridge_vlan_slave($bridgevlan, $iface, $tag); - } + # for each physical interface (eth or bridge) bind them to bridge vlan + foreach my $iface (@ifaces) { + activate_bridge_vlan_slave($bridgevlan, $iface, $tag); + } - #fixme: set other bridge flags + #fixme: set other bridge flags - # be sure to have the bridge up - system("/sbin/ip link set $bridgevlan up") == 0 || - die "can't up bridge $bridgevlan\n"; - + # remove ipv6 link-local address before activation + disable_ipv6($bridgevlan); + # be sure to have the bridge up + &$activate_interface($bridgevlan); + }); return $bridgevlan; } @@ -529,36 +555,11 @@ sub is_ip_in_cidr { return $cidr_obj->overlaps($ip_obj) == $Net::IP::IP_B_IN_A_OVERLAP; } -# struct ifreq { // FOR SIOCGIFFLAGS: -# char ifrn_name[IFNAMSIZ] -# short ifru_flags -# }; -my $STRUCT_IFREQ_SIOCGIFFLAGS = 'Z' . IFNAMSIZ . 's1'; -sub get_active_interfaces { - # Use the interface name list from /proc/net/dev - open my $fh, '<', '/proc/net/dev' - or die "failed to open /proc/net/dev: $!\n"; - # And filter by IFF_UP flag fetched via a PF_INET6 socket ioctl: - my $sock; - socket($sock, PF_INET6, SOCK_DGRAM, &IPPROTO_IP) - or socket($sock, PF_INET, SOCK_DGRAM, &IPPROTO_IP) - or return []; - - my $ifaces = []; - while(defined(my $line = <$fh>)) { - next if $line !~ /^\s*([^:\s]+):/; - my $ifname = $1; - my $ifreq = pack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifname, 0); - if (!defined(ioctl($sock, SIOCGIFFLAGS, $ifreq))) { - warn "failed to get interface flags for: $ifname\n"; - next; - } - my ($name, $flags) = unpack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifreq); - push @$ifaces, $ifname if ($flags & IFF_UP); - } - close $fh; - close $sock; - return $ifaces; +sub lock_network { + my ($code, @param) = @_; + my $res = lock_file('/var/lock/pve-network.lck', 10, $code, @param); + die $@ if $@; + return $res; } 1;