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