]> git.proxmox.com Git - pve-common.git/blob - src/PVE/Network.pm
c83b1bb858b812dd01488a89d3d20b327c7a30dd
[pve-common.git] / src / PVE / Network.pm
1 package PVE::Network;
2
3 use strict;
4 use warnings;
5
6 use PVE::INotify;
7 use PVE::ProcFSTools;
8 use PVE::Tools qw(run_command lock_file);
9
10 use File::Basename;
11 use IO::Socket::IP;
12 use JSON;
13 use Net::IP;
14 use NetAddr::IP qw(:lower);
15 use POSIX qw(ECONNREFUSED);
16 use Socket qw(NI_NUMERICHOST NI_NUMERICSERV);
17
18 # host network related utility functions
19
20 our $PHYSICAL_NIC_RE = qr/(?:eth\d+|en[^:.]+|ib[^:.]+)/;
21
22 our $ipv4_reverse_mask = [
23 '0.0.0.0',
24 '128.0.0.0',
25 '192.0.0.0',
26 '224.0.0.0',
27 '240.0.0.0',
28 '248.0.0.0',
29 '252.0.0.0',
30 '254.0.0.0',
31 '255.0.0.0',
32 '255.128.0.0',
33 '255.192.0.0',
34 '255.224.0.0',
35 '255.240.0.0',
36 '255.248.0.0',
37 '255.252.0.0',
38 '255.254.0.0',
39 '255.255.0.0',
40 '255.255.128.0',
41 '255.255.192.0',
42 '255.255.224.0',
43 '255.255.240.0',
44 '255.255.248.0',
45 '255.255.252.0',
46 '255.255.254.0',
47 '255.255.255.0',
48 '255.255.255.128',
49 '255.255.255.192',
50 '255.255.255.224',
51 '255.255.255.240',
52 '255.255.255.248',
53 '255.255.255.252',
54 '255.255.255.254',
55 '255.255.255.255',
56 ];
57
58 our $ipv4_mask_hash_localnet = {
59 '255.0.0.0' => 8,
60 '255.128.0.0' => 9,
61 '255.192.0.0' => 10,
62 '255.224.0.0' => 11,
63 '255.240.0.0' => 12,
64 '255.248.0.0' => 13,
65 '255.252.0.0' => 14,
66 '255.254.0.0' => 15,
67 '255.255.0.0' => 16,
68 '255.255.128.0' => 17,
69 '255.255.192.0' => 18,
70 '255.255.224.0' => 19,
71 '255.255.240.0' => 20,
72 '255.255.248.0' => 21,
73 '255.255.252.0' => 22,
74 '255.255.254.0' => 23,
75 '255.255.255.0' => 24,
76 '255.255.255.128' => 25,
77 '255.255.255.192' => 26,
78 '255.255.255.224' => 27,
79 '255.255.255.240' => 28,
80 '255.255.255.248' => 29,
81 '255.255.255.252' => 30,
82 '255.255.255.254' => 31,
83 '255.255.255.255' => 32,
84 };
85
86 sub setup_tc_rate_limit {
87 my ($iface, $rate, $burst) = @_;
88
89 # these are allowed / expected to fail, e.g. when there is no previous rate limit to remove
90 eval { run_command("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1"); };
91 eval { run_command("/sbin/tc filter del dev $iface parent ffff: protocol all pref 50 u32 >/dev/null 2>&1"); };
92 eval { run_command("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1"); };
93 eval { run_command("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1"); };
94
95 return if !$rate;
96
97 # tbf does not work for unknown reason
98 #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST
99 # so we use htb instead
100 run_command("/sbin/tc qdisc add dev $iface root handle 1: htb default 1");
101 run_command("/sbin/tc class add dev $iface parent 1: classid 1:1 " .
102 "htb rate ${rate}bps burst ${burst}b");
103
104 run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress");
105 run_command("/sbin/tc filter add dev $iface parent ffff: " .
106 "prio 50 basic " .
107 "police rate ${rate}bps burst ${burst}b mtu 64kb " .
108 "drop");
109 }
110
111 sub tap_rate_limit {
112 my ($iface, $rate) = @_;
113
114 $rate = int($rate*1024*1024) if $rate;
115 my $burst = 1024*1024;
116
117 setup_tc_rate_limit($iface, $rate, $burst);
118 }
119
120 sub read_bridge_mtu {
121 my ($bridge) = @_;
122
123 my $mtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu");
124 die "bridge '$bridge' does not exist\n" if !$mtu;
125 # avoid insecure dependency;
126 die "unable to parse mtu value" if $mtu !~ /^(\d+)$/;
127 $mtu = int($1);
128
129 return $mtu;
130 };
131
132 my $parse_tap_device_name = sub {
133 my ($iface, $noerr) = @_;
134
135 my ($vmid, $devid);
136
137 if ($iface =~ m/^tap(\d+)i(\d+)$/) {
138 $vmid = $1;
139 $devid = $2;
140 } elsif ($iface =~ m/^veth(\d+)i(\d+)$/) {
141 $vmid = $1;
142 $devid = $2;
143 } else {
144 return undef if $noerr;
145 die "can't create firewall bridge for random interface name '$iface'\n";
146 }
147
148 return ($vmid, $devid);
149 };
150
151 my $compute_fwbr_names = sub {
152 my ($vmid, $devid) = @_;
153
154 my $fwbr = "fwbr${vmid}i${devid}";
155 # Note: the firewall use 'fwln+' to filter traffic to VMs
156 my $vethfw = "fwln${vmid}i${devid}";
157 my $vethfwpeer = "fwpr${vmid}p${devid}";
158 my $ovsintport = "fwln${vmid}o${devid}";
159
160 return ($fwbr, $vethfw, $vethfwpeer, $ovsintport);
161 };
162
163 sub iface_delete($) {
164 my ($iface) = @_;
165 run_command(['/sbin/ip', 'link', 'delete', 'dev', $iface], noerr => 1)
166 == 0 or die "failed to delete interface '$iface'\n";
167 }
168
169 sub iface_create($$@) {
170 my ($iface, $type, @args) = @_;
171 run_command(['/sbin/ip', 'link', 'add', $iface, 'type', $type, @args], noerr => 1)
172 == 0 or die "failed to create interface '$iface'\n";
173 }
174
175 sub iface_set($@) {
176 my ($iface, @opts) = @_;
177 run_command(['/sbin/ip', 'link', 'set', $iface, @opts], noerr => 1)
178 == 0 or die "failed to set interface options for '$iface' (".join(' ', @opts).")\n";
179 }
180
181 # helper for nicer error messages:
182 sub iface_set_master($$) {
183 my ($iface, $master) = @_;
184 if (defined($master)) {
185 eval { iface_set($iface, 'master', $master) };
186 die "can't enslave '$iface' to '$master'\n" if $@;
187 } else {
188 eval { iface_set($iface, 'nomaster') };
189 die "can't unenslave '$iface'\n" if $@;
190 }
191 }
192
193 my $cond_create_bridge = sub {
194 my ($bridge) = @_;
195
196 if (! -d "/sys/class/net/$bridge") {
197 iface_create($bridge, 'bridge');
198 disable_ipv6($bridge);
199 }
200 };
201
202 sub disable_ipv6 {
203 my ($iface) = @_;
204 return if !-d '/proc/sys/net/ipv6'; # ipv6 might be completely disabled
205 my $file = "/proc/sys/net/ipv6/conf/$iface/disable_ipv6";
206 open(my $fh, '>', $file) or die "failed to open $file for writing: $!\n";
207 print {$fh} "1\n" or die "failed to disable link-local ipv6 for $iface\n";
208 close($fh);
209 }
210
211 my $bridge_disable_interface_learning = sub {
212 my ($iface) = @_;
213
214 PVE::ProcFSTools::write_proc_entry("/sys/class/net/$iface/brport/unicast_flood", "0");
215 PVE::ProcFSTools::write_proc_entry("/sys/class/net/$iface/brport/learning", "0");
216
217 };
218
219 my $bridge_add_interface = sub {
220 my ($bridge, $iface, $tag, $trunks) = @_;
221
222 # drop link local address (it can't be used when on a bridge anyway)
223 disable_ipv6($iface);
224 iface_set_master($iface, $bridge);
225
226 my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
227
228 if ($vlan_aware) {
229
230 eval { run_command(['/sbin/bridge', 'vlan', 'del', 'dev', $iface, 'vid', '1-4094']) };
231 die "failed to remove default vlan tags of $iface - $@\n" if $@;
232
233 if ($trunks) {
234 my @trunks_array = split /;/, $trunks;
235 foreach my $trunk (@trunks_array) {
236 eval { run_command(['/sbin/bridge', 'vlan', 'add', 'dev', $iface, 'vid', $trunk]) };
237 die "unable to add vlan $trunk to interface $iface - $@\n" if $@;
238 }
239 } elsif (!$tag) {
240 eval { run_command(['/sbin/bridge', 'vlan', 'add', 'dev', $iface, 'vid', '2-4094']) };
241 die "unable to add default vlan tags to interface $iface - $@\n" if $@;
242 }
243
244 $tag = 1 if !$tag;
245 eval { run_command(['/sbin/bridge', 'vlan', 'add', 'dev', $iface, 'vid', $tag, 'pvid', 'untagged']) };
246 die "unable to add vlan $tag to interface $iface - $@\n" if $@;
247 }
248 };
249
250 my $ovs_bridge_add_port = sub {
251 my ($bridge, $iface, $tag, $internal, $trunks) = @_;
252
253 $trunks =~ s/;/,/g if $trunks;
254
255 my $cmd = ['/usr/bin/ovs-vsctl'];
256 # first command
257 push @$cmd, '--', 'add-port', $bridge, $iface;
258 push @$cmd, "tag=$tag" if $tag;
259 push @$cmd, "trunks=". join(',', $trunks) if $trunks;
260 push @$cmd, "vlan_mode=native-untagged" if $tag && $trunks;
261
262 if ($internal) {
263 # second command
264 push @$cmd, '--', 'set', 'Interface', $iface, 'type=internal';
265 }
266
267 eval { run_command($cmd) };
268 die "can't add ovs port '$iface' - $@\n" if $@;
269
270 disable_ipv6($iface);
271 };
272
273 my $activate_interface = sub {
274 my ($iface) = @_;
275
276 eval { run_command(['/sbin/ip', 'link', 'set', $iface, 'up']) };
277 die "can't activate interface '$iface' - $@\n" if $@;
278 };
279
280 sub add_bridge_fdb {
281 my ($iface, $mac) = @_;
282
283 my $learning = PVE::Tools::file_read_firstline("/sys/class/net/$iface/brport/learning");
284 return if $learning;
285
286 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
287 return if !defined($vmid);
288
289 run_command(['/sbin/bridge', 'fdb', 'append', $mac, 'dev', $iface, 'master', 'static']);
290
291 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
292
293 if (-d "/sys/class/net/$vethfwpeer") {
294 run_command(['/sbin/bridge', 'fdb', 'append', $mac, 'dev', $vethfwpeer, 'master', 'static']);
295 }
296
297 }
298
299 sub del_bridge_fdb {
300 my ($iface, $mac) = @_;
301
302 my $learning = PVE::Tools::file_read_firstline("/sys/class/net/$iface/brport/learning");
303 return if $learning;
304
305 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
306 return if !defined($vmid);
307
308 run_command(['/sbin/bridge', 'fdb', 'del', $mac, 'dev', $iface, 'master', 'static']);
309
310 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
311
312 if (-d "/sys/class/net/$vethfwpeer") {
313 run_command(['/sbin/bridge', 'fdb', 'del', $mac, 'dev', $vethfwpeer, 'master', 'static']);
314 }
315 }
316
317 sub tap_create {
318 my ($iface, $bridge) = @_;
319
320 die "unable to get bridge setting\n" if !$bridge;
321
322 my $bridgemtu = read_bridge_mtu($bridge);
323
324 eval {
325 disable_ipv6($iface);
326 run_command(['/sbin/ip', 'link', 'set', $iface, 'up', 'promisc', 'on', 'mtu', $bridgemtu]);
327 };
328 die "interface activation failed\n" if $@;
329 }
330
331 sub veth_create {
332 my ($veth, $vethpeer, $bridge, $mac) = @_;
333
334 die "unable to get bridge setting\n" if !$bridge;
335
336 my $bridgemtu = read_bridge_mtu($bridge);
337
338 # create veth pair
339 if (! -d "/sys/class/net/$veth") {
340 my $cmd = ['/sbin/ip', 'link', 'add'];
341 # veth device + MTU
342 push @$cmd, 'name', $veth;
343 push @$cmd, 'mtu', $bridgemtu;
344 push @$cmd, 'type', 'veth';
345 # peer device + MTU
346 push @$cmd, 'peer', 'name', $vethpeer, 'mtu', $bridgemtu;
347
348 push @$cmd, 'addr', $mac if $mac;
349
350 eval { run_command($cmd) };
351 die "can't create interface $veth - $@\n" if $@;
352 }
353
354 # up vethpair
355 disable_ipv6($veth);
356 disable_ipv6($vethpeer);
357 &$activate_interface($veth);
358 &$activate_interface($vethpeer);
359 }
360
361 sub veth_delete {
362 my ($veth) = @_;
363
364 if (-d "/sys/class/net/$veth") {
365 iface_delete($veth);
366 }
367 eval { tap_unplug($veth) };
368 }
369
370 my $create_firewall_bridge_linux = sub {
371 my ($iface, $bridge, $tag, $trunks, $no_learning) = @_;
372
373 my ($vmid, $devid) = &$parse_tap_device_name($iface);
374 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
375
376 &$cond_create_bridge($fwbr);
377 &$activate_interface($fwbr);
378
379 copy_bridge_config($bridge, $fwbr);
380 veth_create($vethfw, $vethfwpeer, $bridge);
381
382 &$bridge_add_interface($bridge, $vethfwpeer, $tag, $trunks);
383 &$bridge_disable_interface_learning($vethfwpeer) if $no_learning;
384 &$bridge_add_interface($fwbr, $vethfw);
385
386 &$bridge_add_interface($fwbr, $iface);
387 };
388
389 my $create_firewall_bridge_ovs = sub {
390 my ($iface, $bridge, $tag, $trunks, $no_learning) = @_;
391
392 my ($vmid, $devid) = &$parse_tap_device_name($iface);
393 my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
394
395 my $bridgemtu = read_bridge_mtu($bridge);
396
397 &$cond_create_bridge($fwbr);
398 &$activate_interface($fwbr);
399
400 &$bridge_add_interface($fwbr, $iface);
401
402 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1, $trunks);
403 &$activate_interface($ovsintport);
404
405 # set the same mtu for ovs int port
406 run_command(['/sbin/ip', 'link', 'set', $ovsintport, 'mtu', $bridgemtu]);
407
408 &$bridge_add_interface($fwbr, $ovsintport);
409 &$bridge_disable_interface_learning($ovsintport) if $no_learning;
410 };
411
412 my $cleanup_firewall_bridge = sub {
413 my ($iface) = @_;
414
415 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
416 return if !defined($vmid);
417 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
418
419 # cleanup old port config from any openvswitch bridge
420 if (-d "/sys/class/net/$ovsintport") {
421 run_command("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc => sub {}, errfunc => sub {});
422 }
423
424 # delete old vethfw interface
425 veth_delete($vethfw);
426
427 # cleanup fwbr bridge
428 if (-d "/sys/class/net/$fwbr") {
429 iface_delete($fwbr);
430 }
431 };
432
433 sub tap_plug {
434 my ($iface, $bridge, $tag, $firewall, $trunks, $rate, $opts) = @_;
435
436 $opts = {} if !defined($opts);
437
438 my $no_learning = defined($opts->{learning}) && !$opts->{learning}; # default to learning on
439
440 # cleanup old port config from any openvswitch bridge
441 eval {
442 run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {});
443 };
444
445 if (-d "/sys/class/net/$bridge/bridge") {
446 &$cleanup_firewall_bridge($iface); # remove stale devices
447
448 my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
449
450 if (!$vlan_aware) {
451 die "vlan aware feature need to be enabled to use trunks" if $trunks;
452 my $newbridge = activate_bridge_vlan($bridge, $tag);
453 copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
454 $bridge = $newbridge;
455 $tag = undef;
456 }
457
458 if ($firewall) {
459 &$create_firewall_bridge_linux($iface, $bridge, $tag, $trunks, $no_learning);
460 } else {
461 &$bridge_add_interface($bridge, $iface, $tag, $trunks);
462 }
463 $bridge_disable_interface_learning->($iface) if $no_learning;
464
465 } else {
466 &$cleanup_firewall_bridge($iface); # remove stale devices
467
468 if ($firewall) {
469 &$create_firewall_bridge_ovs($iface, $bridge, $tag, $trunks, $no_learning);
470 } else {
471 &$ovs_bridge_add_port($bridge, $iface, $tag, undef, $trunks);
472 }
473 }
474
475 tap_rate_limit($iface, $rate);
476 }
477
478 sub tap_unplug {
479 my ($iface) = @_;
480
481 my $path= "/sys/class/net/$iface/brport/bridge";
482 if (-l $path) {
483 my $bridge = basename(readlink($path));
484 #avoid insecure dependency;
485 ($bridge) = $bridge =~ /(\S+)/;
486
487 iface_set_master($iface, undef);
488 }
489
490 &$cleanup_firewall_bridge($iface);
491 #cleanup old port config from any openvswitch bridge
492 eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) };
493 }
494
495 sub copy_bridge_config {
496 my ($br0, $br1) = @_;
497
498 return if $br0 eq $br1;
499
500 my $br_configs = [
501 'ageing_time', 'stp_state', 'priority', 'forward_delay',
502 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier',
503 ];
504
505 foreach my $sysname (@$br_configs) {
506 eval {
507 my $v0 = PVE::Tools::file_read_firstline("/sys/class/net/$br0/bridge/$sysname");
508 my $v1 = PVE::Tools::file_read_firstline("/sys/class/net/$br1/bridge/$sysname");
509 if ($v0 ne $v1) {
510 PVE::ProcFSTools::write_proc_entry("/sys/class/net/$br1/bridge/$sysname", $v0);
511 }
512 };
513 warn $@ if $@;
514 }
515 }
516
517 sub activate_bridge_vlan_slave {
518 my ($bridgevlan, $iface, $tag) = @_;
519 my $ifacevlan = "${iface}.$tag";
520
521 # create vlan on $iface is not already exist
522 if (! -d "/sys/class/net/$ifacevlan") {
523 eval {
524 my $cmd = ['/sbin/ip', 'link', 'add'];
525 push @$cmd, 'link', $iface;
526 push @$cmd, 'name', $ifacevlan;
527 push @$cmd, 'type', 'vlan', 'id', $tag;
528 run_command($cmd);
529 };
530 die "can't add vlan tag $tag to interface $iface - $@\n" if $@;
531
532 # remove ipv6 link-local address before activation
533 disable_ipv6($ifacevlan);
534 }
535
536 # be sure to have the $ifacevlan up
537 &$activate_interface($ifacevlan);
538
539 # test if $vlaniface is already enslaved in another bridge
540 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
541 if (-l $path) {
542 my $tbridge = basename(readlink($path));
543 if ($tbridge ne $bridgevlan) {
544 die "interface $ifacevlan already exist in bridge $tbridge\n";
545 } else {
546 # Port already attached to bridge: do nothing.
547 return;
548 }
549 }
550
551 # add $ifacevlan to the bridge
552 &$bridge_add_interface($bridgevlan, $ifacevlan);
553 }
554
555 sub activate_bridge_vlan {
556 my ($bridge, $tag_param) = @_;
557
558 die "bridge '$bridge' is not active\n" if ! -d "/sys/class/net/$bridge";
559
560 return $bridge if !defined($tag_param); # no vlan, simply return
561
562 my $tag = int($tag_param);
563
564 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
565
566 my $bridgevlan = "${bridge}v$tag";
567
568 my @ifaces = ();
569 my $dir = "/sys/class/net/$bridge/brif";
570 PVE::Tools::dir_glob_foreach($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub {
571 push @ifaces, $_[0];
572 });
573
574 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
575
576 lock_network(sub {
577 # add bridgevlan if it doesn't already exist
578 if (! -d "/sys/class/net/$bridgevlan") {
579 iface_create($bridgevlan, 'bridge');
580 }
581
582 # for each physical interface (eth or bridge) bind them to bridge vlan
583 foreach my $iface (@ifaces) {
584 activate_bridge_vlan_slave($bridgevlan, $iface, $tag);
585 }
586
587 #fixme: set other bridge flags
588
589 # remove ipv6 link-local address before activation
590 disable_ipv6($bridgevlan);
591 # be sure to have the bridge up
592 &$activate_interface($bridgevlan);
593 });
594 return $bridgevlan;
595 }
596
597 sub tcp_ping {
598 my ($host, $port, $timeout) = @_;
599
600 my $refused = 1;
601
602 $timeout = 3 if !$timeout; # sane default
603 if (!$port) {
604 # Net::Ping defaults to the echo port
605 $port = 7;
606 } else {
607 # Net::Ping's port_number() implies service_check(1)
608 $refused = 0;
609 }
610
611 my ($sock, $result);
612 eval {
613 $result = PVE::Tools::run_with_timeout($timeout, sub {
614 $sock = IO::Socket::IP->new(PeerHost => $host, PeerPort => $port, Type => SOCK_STREAM);
615 $result = $refused if $! == ECONNREFUSED;
616 });
617 };
618 if ($sock) {
619 $sock->close();
620 $result = 1;
621 }
622 return $result;
623 }
624
625 sub IP_from_cidr {
626 my ($cidr, $version) = @_;
627
628 return if $cidr !~ m!^(\S+?)/(\S+)$!;
629 my ($ip, $prefix) = ($1, $2);
630
631 my $ipobj = Net::IP->new($ip, $version);
632 return if !$ipobj;
633
634 $version = $ipobj->version();
635
636 my $binmask = Net::IP::ip_get_mask($prefix, $version);
637 return if !$binmask;
638
639 my $masked_binip = $ipobj->binip() & $binmask;
640 my $masked_ip = Net::IP::ip_bintoip($masked_binip, $version);
641 return Net::IP->new("$masked_ip/$prefix");
642 }
643
644 sub is_ip_in_cidr {
645 my ($ip, $cidr, $version) = @_;
646
647 my $cidr_obj = IP_from_cidr($cidr, $version);
648 return undef if !$cidr_obj;
649
650 my $ip_obj = Net::IP->new($ip, $version);
651 return undef if !$ip_obj;
652
653 my $overlap = $cidr_obj->overlaps($ip_obj);
654
655 return if !defined($overlap);
656
657 return $overlap == $Net::IP::IP_B_IN_A_OVERLAP || $overlap == $Net::IP::IP_IDENTICAL;
658 }
659
660 # get all currently configured addresses that have a global scope, i.e., are reachable from the
661 # outside of the host and thus are neither loopback nor link-local ones
662 # returns an array ref of: { addr => "IP", cidr => "IP/PREFIXLEN", family => "inet|inet6" }
663 sub get_reachable_networks {
664 my $raw = '';
665 run_command([qw(ip -j addr show up scope global)], outfunc => sub { $raw .= shift });
666 my $decoded = decode_json($raw);
667
668 my $addrs = []; # filter/transform first so that we can sort correctly more easily below
669 for my $e ($decoded->@*) {
670 next if !$e->{addr_info} || grep { $_ eq 'LOOPBACK' } $e->{flags}->@*;
671 push $addrs->@*, grep { scalar(keys $_->%*) } $e->{addr_info}->@*
672 }
673 my $res = [];
674 for my $info (sort { $a->{family} cmp $b->{family} || $a->{local} cmp $b->{local} } $addrs->@*) {
675 push $res->@*, {
676 addr => $info->{local},
677 cidr => "$info->{local}/$info->{prefixlen}",
678 family => $info->{family},
679 };
680 }
681
682 return $res;
683 }
684
685 # get one or all local IPs that are not loopback ones, able to pick up the following ones (in order)
686 # - the hostname primary resolves too, follows gai.conf (admin controlled) and will be prioritised
687 # - all configured in the interfaces configuration
688 # - all currently networks known to the kernel in the current (root) namespace
689 # returns a single address if no parameter is passed, and all found, grouped by type, if `all => 1`
690 # is passed.
691 sub get_local_ip {
692 my (%param) = @_;
693
694 my $nodename = PVE::INotify::nodename();
695 my $resolved_host = eval { get_ip_from_hostname($nodename) };
696
697 return $resolved_host if defined($resolved_host) && !$param{all};
698
699 my $all = { v4 => {}, v6 => {} }; # hash to avoid duplicates and group by type
700
701 my $ifaces = PVE::INotify::read_file('interfaces', 1)->{data}->{ifaces};
702 for my $if (values $ifaces->%*) {
703 next if $if->{type} eq 'loopback' || (!defined($if->{address}) && !defined($if->{address6}));
704 my ($v4, $v6) = ($if->{address}, $if->{address6});
705
706 return ($v4 // $v6) if !$param{all}; # prefer v4, admin can override $resolved_host via hosts/gai.conf
707
708 $all->{v4}->{$v4} = 1 if defined($v4);
709 $all->{v6}->{$v6} = 1 if defined($v6);
710 }
711
712 my $live = eval { get_reachable_networks() } // [];
713 for my $info ($live->@*) {
714 my $addr = $info->{addr};
715
716 return $addr if !$param{all};
717
718 if ($info->{family} eq 'inet') {
719 $all->{v4}->{$addr} = 1;
720 } else {
721 $all->{v6}->{$addr} = 1;
722 }
723 }
724
725 return undef if !$param{all}; # getting here means no early return above triggered -> no IPs
726
727 my $res = []; # order gai.conf controlled first, then group v4 and v6, simply lexically sorted
728 if ($resolved_host) {
729 push $res->@*, $resolved_host;
730 delete $all->{v4}->{$resolved_host};
731 delete $all->{v6}->{$resolved_host};
732 }
733 push $res->@*, sort { $a cmp $b } keys $all->{v4}->%*;
734 push $res->@*, sort { $a cmp $b } keys $all->{v6}->%*;
735
736 return $res;
737 }
738
739 sub get_local_ip_from_cidr {
740 my ($cidr) = @_;
741
742 my $IPs = {};
743 my $i = 1;
744 run_command(['/sbin/ip', 'address', 'show', 'to', $cidr, 'up'], outfunc => sub {
745 if ($_[0] =~ m!^\s*inet(?:6)?\s+($PVE::Tools::IPRE)(?:/\d+|\s+peer\s+)!) {
746 $IPs->{$1} = $i++ if !exists($IPs->{$1});
747 }
748 });
749
750 return [ sort { $IPs->{$a} <=> $IPs->{$b} } keys %{$IPs} ];
751 }
752
753 sub addr_to_ip {
754 my ($addr) = @_;
755 my ($err, $host, $port) = Socket::getnameinfo($addr, NI_NUMERICHOST | NI_NUMERICSERV);
756 die "failed to get numerical host address: $err\n" if $err;
757 return ($host, $port) if wantarray;
758 return $host;
759 }
760
761 sub get_ip_from_hostname {
762 my ($hostname, $noerr) = @_;
763
764 my @res = eval { PVE::Tools::getaddrinfo_all($hostname) };
765 if ($@) {
766 die "hostname lookup '$hostname' failed - $@" if !$noerr;
767 return undef;
768 }
769
770 for my $ai (@res) {
771 my $ip = addr_to_ip($ai->{addr});
772 if ($ip !~ m/^127\.|^::1$/) {
773 return wantarray ? ($ip, $ai->{family}) : $ip;
774 }
775 }
776 # NOTE: we only get here if no WAN/LAN IP was found, so this is now the error path!
777 die "address lookup for '$hostname' did not find any IP address\n" if !$noerr;
778 return undef;
779 }
780
781 sub lock_network {
782 my ($code, @param) = @_;
783 my $res = lock_file('/var/lock/pve-network.lck', 10, $code, @param);
784 die $@ if $@;
785 return $res;
786 }
787
788 # the canonical form of the given IP, i.e. dotted quad for IPv4 and RFC 5952 for IPv6
789 sub canonical_ip {
790 my ($ip) = @_;
791
792 my $ip_obj = NetAddr::IP->new($ip) or die "invalid IP string '$ip'\n";
793
794 return $ip_obj->canon();
795 }
796
797 # List of unique, canonical IPs in the provided list.
798 # Keeps the original order, filtering later duplicates.
799 sub unique_ips {
800 my ($ips) = @_;
801
802 my $res = [];
803 my $seen = {};
804
805 for my $ip (@{$ips}) {
806 $ip = canonical_ip($ip);
807
808 next if $seen->{$ip};
809
810 $seen->{$ip} = 1;
811 push @{$res}, $ip;
812 }
813
814 return $res;
815 }
816
817 1;