]>
Commit | Line | Data |
---|---|---|
b9436cda DM |
1 | package PVE::Network; |
2 | ||
3 | use strict; | |
c36f332e | 4 | use warnings; |
a712bf6e | 5 | use PVE::Tools qw(run_command lock_file); |
b9436cda DM |
6 | use PVE::ProcFSTools; |
7 | use PVE::INotify; | |
8 | use File::Basename; | |
b6bff92e | 9 | use IO::Socket::IP; |
87aa00de | 10 | use Socket qw(NI_NUMERICHOST NI_NUMERICSERV); |
b6bff92e | 11 | use POSIX qw(ECONNREFUSED); |
b9436cda | 12 | |
bf52d27b WB |
13 | use Net::IP; |
14 | ||
b9436cda DM |
15 | # host network related utility functions |
16 | ||
3dabe28a FG |
17 | our $PHYSICAL_NIC_RE = qr/(?:eth\d+|en[^:.]+|ib\d+)/; |
18 | ||
61aa94e4 WB |
19 | our $ipv4_reverse_mask = [ |
20 | '0.0.0.0', | |
21 | '128.0.0.0', | |
22 | '192.0.0.0', | |
23 | '224.0.0.0', | |
24 | '240.0.0.0', | |
25 | '248.0.0.0', | |
26 | '252.0.0.0', | |
27 | '254.0.0.0', | |
28 | '255.0.0.0', | |
29 | '255.128.0.0', | |
30 | '255.192.0.0', | |
31 | '255.224.0.0', | |
32 | '255.240.0.0', | |
33 | '255.248.0.0', | |
34 | '255.252.0.0', | |
35 | '255.254.0.0', | |
36 | '255.255.0.0', | |
37 | '255.255.128.0', | |
38 | '255.255.192.0', | |
39 | '255.255.224.0', | |
40 | '255.255.240.0', | |
41 | '255.255.248.0', | |
42 | '255.255.252.0', | |
43 | '255.255.254.0', | |
44 | '255.255.255.0', | |
45 | '255.255.255.128', | |
46 | '255.255.255.192', | |
47 | '255.255.255.224', | |
48 | '255.255.255.240', | |
49 | '255.255.255.248', | |
50 | '255.255.255.252', | |
51 | '255.255.255.254', | |
52 | '255.255.255.255', | |
53 | ]; | |
54 | ||
55 | our $ipv4_mask_hash_localnet = { | |
19e609fd WB |
56 | '255.0.0.0' => 8, |
57 | '255.128.0.0' => 9, | |
58 | '255.192.0.0' => 10, | |
59 | '255.224.0.0' => 11, | |
60 | '255.240.0.0' => 12, | |
61 | '255.248.0.0' => 13, | |
62 | '255.252.0.0' => 14, | |
63 | '255.254.0.0' => 15, | |
61aa94e4 WB |
64 | '255.255.0.0' => 16, |
65 | '255.255.128.0' => 17, | |
66 | '255.255.192.0' => 18, | |
67 | '255.255.224.0' => 19, | |
68 | '255.255.240.0' => 20, | |
69 | '255.255.248.0' => 21, | |
70 | '255.255.252.0' => 22, | |
71 | '255.255.254.0' => 23, | |
72 | '255.255.255.0' => 24, | |
73 | '255.255.255.128' => 25, | |
74 | '255.255.255.192' => 26, | |
75 | '255.255.255.224' => 27, | |
76 | '255.255.255.240' => 28, | |
77 | '255.255.255.248' => 29, | |
78 | '255.255.255.252' => 30, | |
e43faad9 WB |
79 | '255.255.255.254' => 31, |
80 | '255.255.255.255' => 32, | |
61aa94e4 WB |
81 | }; |
82 | ||
74d1b045 DM |
83 | sub setup_tc_rate_limit { |
84 | my ($iface, $rate, $burst, $debug) = @_; | |
85 | ||
2d6b3a90 FG |
86 | # these are allowed / expected to fail, e.g. when there is no previous rate limit to remove |
87 | eval { run_command("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1"); }; | |
88 | eval { run_command("/sbin/tc filter del dev $iface parent ffff: protocol all pref 50 u32 >/dev/null 2>&1"); }; | |
89 | eval { run_command("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1"); }; | |
90 | eval { run_command("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1"); }; | |
74d1b045 | 91 | |
d6f2623b | 92 | return if !$rate; |
957753df | 93 | |
74d1b045 DM |
94 | # tbf does not work for unknown reason |
95 | #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST | |
96 | # so we use htb instead | |
97 | run_command("/sbin/tc qdisc add dev $iface root handle 1: htb default 1"); | |
98 | run_command("/sbin/tc class add dev $iface parent 1: classid 1:1 " . | |
99 | "htb rate ${rate}bps burst ${burst}b"); | |
100 | ||
5d35df41 W |
101 | run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress"); |
102 | run_command("/sbin/tc filter add dev $iface parent ffff: " . | |
1b915170 | 103 | "prio 50 basic " . |
5d35df41 W |
104 | "police rate ${rate}bps burst ${burst}b mtu 64kb " . |
105 | "drop flowid :1"); | |
106 | ||
74d1b045 DM |
107 | if ($debug) { |
108 | print "DEBUG tc settings\n"; | |
109 | system("/sbin/tc qdisc ls dev $iface"); | |
110 | system("/sbin/tc class ls dev $iface"); | |
111 | system("/sbin/tc filter ls dev $iface parent ffff:"); | |
112 | } | |
113 | } | |
114 | ||
ec9ada18 AD |
115 | sub tap_rate_limit { |
116 | my ($iface, $rate) = @_; | |
117 | ||
118 | my $debug = 0; | |
ad066ae2 | 119 | $rate = int($rate*1024*1024) if $rate; |
ec9ada18 AD |
120 | my $burst = 1024*1024; |
121 | ||
122 | setup_tc_rate_limit($iface, $rate, $burst, $debug); | |
123 | } | |
74d1b045 | 124 | |
605bb891 DM |
125 | my $read_bridge_mtu = sub { |
126 | my ($bridge) = @_; | |
127 | ||
128 | my $mtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu"); | |
129 | die "bridge '$bridge' does not exist\n" if !$mtu; | |
130 | # avoid insecure dependency; | |
131 | die "unable to parse mtu value" if $mtu !~ /^(\d+)$/; | |
132 | $mtu = int($1); | |
133 | ||
134 | return $mtu; | |
135 | }; | |
136 | ||
32cb7d27 | 137 | my $parse_tap_device_name = sub { |
6c80e6d6 | 138 | my ($iface, $noerr) = @_; |
605bb891 DM |
139 | |
140 | my ($vmid, $devid); | |
141 | ||
142 | if ($iface =~ m/^tap(\d+)i(\d+)$/) { | |
143 | $vmid = $1; | |
144 | $devid = $2; | |
32cb7d27 | 145 | } elsif ($iface =~ m/^veth(\d+)i(\d+)$/) { |
605bb891 DM |
146 | $vmid = $1; |
147 | $devid = $2; | |
148 | } else { | |
6c80e6d6 DM |
149 | return undef if $noerr; |
150 | die "can't create firewall bridge for random interface name '$iface'\n"; | |
605bb891 DM |
151 | } |
152 | ||
153 | return ($vmid, $devid); | |
154 | }; | |
155 | ||
70ab4434 | 156 | my $compute_fwbr_names = sub { |
605bb891 DM |
157 | my ($vmid, $devid) = @_; |
158 | ||
159 | my $fwbr = "fwbr${vmid}i${devid}"; | |
f193aa74 | 160 | # Note: the firewall use 'fwln+' to filter traffic to VMs |
7d78a966 AD |
161 | my $vethfw = "fwln${vmid}i${devid}"; |
162 | my $vethfwpeer = "fwpr${vmid}p${devid}"; | |
163 | my $ovsintport = "fwln${vmid}o${devid}"; | |
605bb891 | 164 | |
70ab4434 | 165 | return ($fwbr, $vethfw, $vethfwpeer, $ovsintport); |
605bb891 DM |
166 | }; |
167 | ||
168 | my $cond_create_bridge = sub { | |
169 | my ($bridge) = @_; | |
170 | ||
171 | if (! -d "/sys/class/net/$bridge") { | |
86b84237 WB |
172 | system("/sbin/brctl addbr $bridge") == 0 || |
173 | die "can't add bridge '$bridge'\n"; | |
174 | disable_ipv6($bridge); | |
605bb891 DM |
175 | } |
176 | }; | |
177 | ||
f3ccd9b4 WB |
178 | sub disable_ipv6 { |
179 | my ($iface) = @_; | |
180 | return if !-d '/proc/sys/net/ipv6'; # ipv6 might be completely disabled | |
181 | my $file = "/proc/sys/net/ipv6/conf/$iface/disable_ipv6"; | |
182 | open(my $fh, '>', $file) or die "failed to open $file for writing: $!\n"; | |
183 | print {$fh} "1\n" or die "failed to disable link-local ipv6 for $iface\n"; | |
184 | close($fh); | |
185 | } | |
186 | ||
605bb891 | 187 | my $bridge_add_interface = sub { |
b0b34ffd | 188 | my ($bridge, $iface, $tag, $trunks) = @_; |
605bb891 | 189 | |
f3ccd9b4 WB |
190 | # drop link local address (it can't be used when on a bridge anyway) |
191 | disable_ipv6($iface); | |
605bb891 DM |
192 | system("/sbin/brctl addif $bridge $iface") == 0 || |
193 | die "can't add interface 'iface' to bridge '$bridge'\n"; | |
4d25f4aa AD |
194 | |
195 | my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering"); | |
196 | ||
197 | if ($vlan_aware) { | |
198 | if ($tag) { | |
78e912a3 WB |
199 | system({'/sbin/bridge'} 'bridge', 'vlan', 'del', 'dev', $iface, 'vid', '1-4094') == 0 |
200 | or die "failed to remove default vlan tags of $iface\n"; | |
201 | system({'/sbin/bridge'} 'bridge', 'vlan', 'add', 'dev', $iface, 'vid', $tag, 'pvid', 'untagged') == 0 | |
202 | or die "unable to add vlan $tag to interface $iface\n"; | |
5d662b31 DC |
203 | |
204 | warn "Caution: Setting VLAN ID 1 on a VLAN aware bridge may be dangerous\n" if $tag == 1; | |
4d25f4aa AD |
205 | } else { |
206 | system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 || | |
b0b34ffd | 207 | die "unable to add default vlan tags to interface $iface\n" if !$trunks; |
4d25f4aa | 208 | } |
b0b34ffd | 209 | |
846337ad WB |
210 | if ($trunks) { |
211 | my @trunks_array = split /;/, $trunks; | |
212 | foreach my $trunk (@trunks_array) { | |
213 | system("/sbin/bridge vlan add dev $iface vid $trunk") == 0 || | |
214 | die "unable to add vlan $trunk to interface $iface\n"; | |
215 | } | |
b0b34ffd | 216 | } |
4d25f4aa | 217 | } |
605bb891 DM |
218 | }; |
219 | ||
70ab4434 | 220 | my $ovs_bridge_add_port = sub { |
b0b34ffd AD |
221 | my ($bridge, $iface, $tag, $internal, $trunks) = @_; |
222 | ||
223 | $trunks =~ s/;/,/g if $trunks; | |
70ab4434 DM |
224 | |
225 | my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface"; | |
226 | $cmd .= " tag=$tag" if $tag; | |
b0b34ffd AD |
227 | $cmd .= " trunks=". join(',', $trunks) if $trunks; |
228 | $cmd .= " vlan_mode=native-untagged" if $tag && $trunks; | |
229 | ||
70ab4434 DM |
230 | $cmd .= " -- set Interface $iface type=internal" if $internal; |
231 | system($cmd) == 0 || | |
232 | die "can't add ovs port '$iface'\n"; | |
f3ccd9b4 | 233 | disable_ipv6($iface); |
70ab4434 DM |
234 | }; |
235 | ||
605bb891 DM |
236 | my $activate_interface = sub { |
237 | my ($iface) = @_; | |
238 | ||
239 | system("/sbin/ip link set $iface up") == 0 || | |
240 | die "can't activate interface '$iface'\n"; | |
241 | }; | |
242 | ||
3aa99c70 AD |
243 | sub tap_create { |
244 | my ($iface, $bridge) = @_; | |
245 | ||
246 | die "unable to get bridge setting\n" if !$bridge; | |
247 | ||
605bb891 | 248 | my $bridgemtu = &$read_bridge_mtu($bridge); |
3aa99c70 | 249 | |
098795e0 | 250 | eval { |
f3ccd9b4 | 251 | disable_ipv6($iface); |
86330049 | 252 | PVE::Tools::run_command(['/sbin/ip', 'link', 'set', $iface, 'up', 'promisc', 'on', 'mtu', $bridgemtu]); |
098795e0 DM |
253 | }; |
254 | die "interface activation failed\n" if $@; | |
3aa99c70 AD |
255 | } |
256 | ||
35efc4eb AD |
257 | sub veth_create { |
258 | my ($veth, $vethpeer, $bridge, $mac) = @_; | |
259 | ||
260 | die "unable to get bridge setting\n" if !$bridge; | |
261 | ||
262 | my $bridgemtu = &$read_bridge_mtu($bridge); | |
263 | ||
264 | # create veth pair | |
265 | if (! -d "/sys/class/net/$veth") { | |
266 | my $cmd = "/sbin/ip link add name $veth type veth peer name $vethpeer mtu $bridgemtu"; | |
267 | $cmd .= " addr $mac" if $mac; | |
268 | system($cmd) == 0 || die "can't create interface $veth\n"; | |
269 | } | |
270 | ||
271 | # up vethpair | |
f3ccd9b4 WB |
272 | disable_ipv6($veth); |
273 | disable_ipv6($vethpeer); | |
35efc4eb AD |
274 | &$activate_interface($veth); |
275 | &$activate_interface($vethpeer); | |
276 | } | |
277 | ||
f3f0bc3a AD |
278 | sub veth_delete { |
279 | my ($veth) = @_; | |
280 | ||
281 | if (-d "/sys/class/net/$veth") { | |
282 | run_command("/sbin/ip link delete dev $veth", outfunc => sub {}, errfunc => sub {}); | |
283 | } | |
e0a862e2 | 284 | eval { tap_unplug($veth) }; |
f3f0bc3a | 285 | } |
35efc4eb | 286 | |
605bb891 | 287 | my $create_firewall_bridge_linux = sub { |
b0b34ffd | 288 | my ($iface, $bridge, $tag, $trunks) = @_; |
605bb891 | 289 | |
32cb7d27 | 290 | my ($vmid, $devid) = &$parse_tap_device_name($iface); |
70ab4434 | 291 | my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid); |
605bb891 | 292 | |
605bb891 DM |
293 | &$cond_create_bridge($fwbr); |
294 | &$activate_interface($fwbr); | |
295 | ||
296 | copy_bridge_config($bridge, $fwbr); | |
35efc4eb | 297 | veth_create($vethfw, $vethfwpeer, $bridge); |
605bb891 | 298 | |
7d78a966 | 299 | &$bridge_add_interface($fwbr, $vethfw); |
b0b34ffd | 300 | &$bridge_add_interface($bridge, $vethfwpeer, $tag, $trunks); |
605bb891 | 301 | |
4d25f4aa | 302 | &$bridge_add_interface($fwbr, $iface); |
605bb891 DM |
303 | }; |
304 | ||
70ab4434 | 305 | my $create_firewall_bridge_ovs = sub { |
b0b34ffd | 306 | my ($iface, $bridge, $tag, $trunks) = @_; |
70ab4434 | 307 | |
32cb7d27 | 308 | my ($vmid, $devid) = &$parse_tap_device_name($iface); |
70ab4434 DM |
309 | my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid); |
310 | ||
311 | my $bridgemtu = &$read_bridge_mtu($bridge); | |
312 | ||
313 | &$cond_create_bridge($fwbr); | |
314 | &$activate_interface($fwbr); | |
315 | ||
316 | &$bridge_add_interface($fwbr, $iface); | |
317 | ||
b0b34ffd | 318 | &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1, $trunks); |
ac3a04b8 | 319 | &$activate_interface($ovsintport); |
70ab4434 DM |
320 | |
321 | # set the same mtu for ovs int port | |
86330049 | 322 | PVE::Tools::run_command(['/sbin/ip', 'link', 'set', $ovsintport, 'mtu', $bridgemtu]); |
70ab4434 DM |
323 | |
324 | &$bridge_add_interface($fwbr, $ovsintport); | |
325 | }; | |
326 | ||
327 | my $cleanup_firewall_bridge = sub { | |
605bb891 DM |
328 | my ($iface) = @_; |
329 | ||
32cb7d27 | 330 | my ($vmid, $devid) = &$parse_tap_device_name($iface, 1); |
6c80e6d6 | 331 | return if !defined($vmid); |
70ab4434 DM |
332 | my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid); |
333 | ||
334 | # cleanup old port config from any openvswitch bridge | |
335 | if (-d "/sys/class/net/$ovsintport") { | |
336 | run_command("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc => sub {}, errfunc => sub {}); | |
337 | } | |
605bb891 DM |
338 | |
339 | # delete old vethfw interface | |
f3f0bc3a | 340 | veth_delete($vethfw); |
605bb891 DM |
341 | |
342 | # cleanup fwbr bridge | |
343 | if (-d "/sys/class/net/$fwbr") { | |
344 | run_command("/sbin/ip link set dev $fwbr down", outfunc => sub {}, errfunc => sub {}); | |
345 | run_command("/sbin/brctl delbr $fwbr", outfunc => sub {}, errfunc => sub {}); | |
346 | } | |
347 | }; | |
348 | ||
f0c190ee | 349 | sub tap_plug { |
bce2a5b3 | 350 | my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_; |
f0c190ee | 351 | |
4cbabd40 AD |
352 | #cleanup old port config from any openvswitch bridge |
353 | eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) }; | |
354 | ||
098795e0 | 355 | if (-d "/sys/class/net/$bridge/bridge") { |
70ab4434 | 356 | &$cleanup_firewall_bridge($iface); # remove stale devices |
605bb891 | 357 | |
4d25f4aa | 358 | my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering"); |
098795e0 | 359 | |
4d25f4aa | 360 | if (!$vlan_aware) { |
b0b34ffd | 361 | die "vlan aware feature need to be enabled to use trunks" if $trunks; |
4d25f4aa AD |
362 | my $newbridge = activate_bridge_vlan($bridge, $tag); |
363 | copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge; | |
ff042056 | 364 | $bridge = $newbridge; |
4d25f4aa AD |
365 | $tag = undef; |
366 | } | |
367 | ||
368 | if ($firewall) { | |
b0b34ffd | 369 | &$create_firewall_bridge_linux($iface, $bridge, $tag, $trunks); |
4d25f4aa | 370 | } else { |
b0b34ffd | 371 | &$bridge_add_interface($bridge, $iface, $tag, $trunks); |
4d25f4aa | 372 | } |
605bb891 | 373 | |
098795e0 | 374 | } else { |
70ab4434 DM |
375 | &$cleanup_firewall_bridge($iface); # remove stale devices |
376 | ||
377 | if ($firewall) { | |
b0b34ffd | 378 | &$create_firewall_bridge_ovs($iface, $bridge, $tag, $trunks); |
70ab4434 | 379 | } else { |
b0b34ffd | 380 | &$ovs_bridge_add_port($bridge, $iface, $tag, undef, $trunks); |
70ab4434 | 381 | } |
4cbabd40 | 382 | } |
bce2a5b3 WB |
383 | |
384 | tap_rate_limit($iface, $rate); | |
f0c190ee AD |
385 | } |
386 | ||
a84b65c0 | 387 | sub tap_unplug { |
2db1cc0d | 388 | my ($iface) = @_; |
a84b65c0 | 389 | |
2db1cc0d DM |
390 | my $path= "/sys/class/net/$iface/brport/bridge"; |
391 | if (-l $path) { | |
392 | my $bridge = basename(readlink($path)); | |
393 | #avoid insecure dependency; | |
394 | ($bridge) = $bridge =~ /(\S+)/; | |
4cbabd40 | 395 | |
098795e0 | 396 | system("/sbin/brctl delif $bridge $iface") == 0 || |
2db1cc0d | 397 | die "can't del interface '$iface' from bridge '$bridge'\n"; |
605bb891 | 398 | |
4cbabd40 | 399 | } |
70ab4434 DM |
400 | |
401 | &$cleanup_firewall_bridge($iface); | |
dd44486e WB |
402 | #cleanup old port config from any openvswitch bridge |
403 | eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) }; | |
a84b65c0 AD |
404 | } |
405 | ||
b9436cda DM |
406 | sub copy_bridge_config { |
407 | my ($br0, $br1) = @_; | |
408 | ||
409 | return if $br0 eq $br1; | |
410 | ||
411 | my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay', | |
ba4af65b | 412 | 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier']; |
b9436cda DM |
413 | |
414 | foreach my $sysname (@$br_configs) { | |
415 | eval { | |
416 | my $v0 = PVE::Tools::file_read_firstline("/sys/class/net/$br0/bridge/$sysname"); | |
417 | my $v1 = PVE::Tools::file_read_firstline("/sys/class/net/$br1/bridge/$sysname"); | |
418 | if ($v0 ne $v1) { | |
aec04803 | 419 | PVE::ProcFSTools::write_proc_entry("/sys/class/net/$br1/bridge/$sysname", $v0); |
b9436cda DM |
420 | } |
421 | }; | |
422 | warn $@ if $@; | |
423 | } | |
424 | } | |
425 | ||
70d89745 PRG |
426 | sub activate_bridge_vlan_slave { |
427 | my ($bridgevlan, $iface, $tag) = @_; | |
b9436cda | 428 | my $ifacevlan = "${iface}.$tag"; |
70d89745 | 429 | |
b9436cda DM |
430 | # create vlan on $iface is not already exist |
431 | if (! -d "/sys/class/net/$ifacevlan") { | |
f3ccd9b4 | 432 | system("/sbin/ip link add link $iface name $ifacevlan type vlan id $tag") == 0 || |
02c9a6b4 | 433 | die "can't add vlan tag $tag to interface $iface\n"; |
b9436cda | 434 | |
86b84237 WB |
435 | # remove ipv6 link-local address before activation |
436 | disable_ipv6($ifacevlan); | |
437 | } | |
f3ccd9b4 | 438 | |
b9436cda | 439 | # be sure to have the $ifacevlan up |
605bb891 | 440 | &$activate_interface($ifacevlan); |
b9436cda DM |
441 | |
442 | # test if $vlaniface is already enslaved in another bridge | |
443 | my $path= "/sys/class/net/$ifacevlan/brport/bridge"; | |
444 | if (-l $path) { | |
445 | my $tbridge = basename(readlink($path)); | |
70d89745 | 446 | if ($tbridge ne $bridgevlan) { |
b9436cda | 447 | die "interface $ifacevlan already exist in bridge $tbridge\n"; |
eee4b32a PRG |
448 | } else { |
449 | # Port already attached to bridge: do nothing. | |
450 | return; | |
b9436cda DM |
451 | } |
452 | } | |
453 | ||
70d89745 | 454 | # add $ifacevlan to the bridge |
605bb891 | 455 | &$bridge_add_interface($bridgevlan, $ifacevlan); |
70d89745 PRG |
456 | } |
457 | ||
458 | sub activate_bridge_vlan { | |
459 | my ($bridge, $tag_param) = @_; | |
460 | ||
461 | die "bridge '$bridge' is not active\n" if ! -d "/sys/class/net/$bridge"; | |
462 | ||
463 | return $bridge if !defined($tag_param); # no vlan, simply return | |
464 | ||
465 | my $tag = int($tag_param); | |
466 | ||
467 | die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094; | |
468 | ||
469 | my $bridgevlan = "${bridge}v$tag"; | |
470 | ||
c9030d97 PRG |
471 | my @ifaces = (); |
472 | my $dir = "/sys/class/net/$bridge/brif"; | |
899f8c4a | 473 | PVE::Tools::dir_glob_foreach($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub { |
5ffa7628 | 474 | push @ifaces, $_[0]; |
c9030d97 PRG |
475 | }); |
476 | ||
5ffa7628 | 477 | die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0; |
c9030d97 | 478 | |
a712bf6e WB |
479 | lock_network(sub { |
480 | # add bridgevlan if it doesn't already exist | |
481 | if (! -d "/sys/class/net/$bridgevlan") { | |
482 | system("/sbin/brctl addbr $bridgevlan") == 0 || | |
483 | die "can't add bridge $bridgevlan\n"; | |
484 | } | |
b9436cda | 485 | |
a712bf6e WB |
486 | # for each physical interface (eth or bridge) bind them to bridge vlan |
487 | foreach my $iface (@ifaces) { | |
488 | activate_bridge_vlan_slave($bridgevlan, $iface, $tag); | |
489 | } | |
70d89745 | 490 | |
a712bf6e | 491 | #fixme: set other bridge flags |
b9436cda | 492 | |
f3ccd9b4 WB |
493 | # remove ipv6 link-local address before activation |
494 | disable_ipv6($bridgevlan); | |
a712bf6e | 495 | # be sure to have the bridge up |
f3ccd9b4 | 496 | &$activate_interface($bridgevlan); |
a712bf6e | 497 | }); |
b9436cda DM |
498 | return $bridgevlan; |
499 | } | |
500 | ||
b6bff92e WB |
501 | sub tcp_ping { |
502 | my ($host, $port, $timeout) = @_; | |
503 | ||
504 | my $refused = 1; | |
505 | ||
506 | $timeout = 3 if !$timeout; # sane default | |
507 | if (!$port) { | |
508 | # Net::Ping defaults to the echo port | |
509 | $port = 7; | |
510 | } else { | |
511 | # Net::Ping's port_number() implies service_check(1) | |
512 | $refused = 0; | |
513 | } | |
514 | ||
515 | my ($sock, $result); | |
516 | eval { | |
517 | $result = PVE::Tools::run_with_timeout($timeout, sub { | |
518 | $sock = IO::Socket::IP->new(PeerHost => $host, PeerPort => $port, Type => SOCK_STREAM); | |
519 | $result = $refused if $! == ECONNREFUSED; | |
520 | }); | |
521 | }; | |
522 | if ($sock) { | |
523 | $sock->close(); | |
524 | $result = 1; | |
525 | } | |
526 | return $result; | |
527 | } | |
528 | ||
bf52d27b WB |
529 | sub IP_from_cidr { |
530 | my ($cidr, $version) = @_; | |
531 | ||
532 | return if $cidr !~ m!^(\S+?)/(\S+)$!; | |
533 | my ($ip, $prefix) = ($1, $2); | |
534 | ||
535 | my $ipobj = Net::IP->new($ip, $version); | |
536 | return if !$ipobj; | |
537 | ||
538 | $version = $ipobj->version(); | |
539 | ||
540 | my $binmask = Net::IP::ip_get_mask($prefix, $version); | |
541 | return if !$binmask; | |
542 | ||
543 | my $masked_binip = $ipobj->binip() & $binmask; | |
544 | my $masked_ip = Net::IP::ip_bintoip($masked_binip, $version); | |
545 | return Net::IP->new("$masked_ip/$prefix"); | |
546 | } | |
547 | ||
548 | sub is_ip_in_cidr { | |
549 | my ($ip, $cidr, $version) = @_; | |
550 | ||
551 | my $cidr_obj = IP_from_cidr($cidr, $version); | |
552 | return undef if !$cidr_obj; | |
553 | ||
554 | my $ip_obj = Net::IP->new($ip, $version); | |
555 | return undef if !$ip_obj; | |
556 | ||
557 | return $cidr_obj->overlaps($ip_obj) == $Net::IP::IP_B_IN_A_OVERLAP; | |
558 | } | |
559 | ||
beb9820f TL |
560 | |
561 | sub get_local_ip_from_cidr { | |
562 | my ($cidr) = @_; | |
563 | ||
564 | my $cmd = ['/sbin/ip', 'address', 'show', 'to', $cidr, 'up']; | |
565 | ||
566 | my $IPs = []; | |
567 | ||
568 | my $code = sub { | |
569 | my $line = shift; | |
570 | ||
571 | if ($line =~ m!^\s*inet(?:6)?\s+($PVE::Tools::IPRE)/\d+!) { | |
572 | push @$IPs, $1; | |
573 | } | |
574 | }; | |
575 | ||
576 | PVE::Tools::run_command($cmd, outfunc => $code); | |
577 | ||
578 | return $IPs; | |
579 | } | |
580 | ||
87aa00de TL |
581 | sub addr_to_ip { |
582 | my ($addr) = @_; | |
583 | my ($err, $host, $port) = Socket::getnameinfo($addr, NI_NUMERICHOST | NI_NUMERICSERV); | |
584 | die "failed to get numerical host address: $err\n" if $err; | |
585 | return ($host, $port) if wantarray; | |
586 | return $host; | |
587 | } | |
588 | ||
589 | sub get_ip_from_hostname { | |
590 | my ($hostname, $noerr) = @_; | |
591 | ||
592 | my ($family, $ip); | |
593 | ||
594 | eval { | |
595 | my @res = PVE::Tools::getaddrinfo_all($hostname); | |
596 | $family = $res[0]->{family}; | |
597 | $ip = addr_to_ip($res[0]->{addr}) | |
598 | }; | |
599 | if ($@) { | |
4ed6974a | 600 | die "hostname lookup '$hostname' failed - $@" if !$noerr; |
87aa00de TL |
601 | return undef; |
602 | } | |
603 | ||
604 | if ($ip =~ m/^127\.|^::1$/) { | |
4ed6974a | 605 | die "hostname lookup '$hostname' failed - got local IP address '$ip'\n" if !$noerr; |
87aa00de TL |
606 | return undef; |
607 | } | |
608 | ||
609 | return wantarray ? ($ip, $family) : $ip; | |
610 | } | |
611 | ||
a712bf6e WB |
612 | sub lock_network { |
613 | my ($code, @param) = @_; | |
614 | my $res = lock_file('/var/lock/pve-network.lck', 10, $code, @param); | |
615 | die $@ if $@; | |
616 | return $res; | |
617 | } | |
618 | ||
b9436cda | 619 | 1; |