]> git.proxmox.com Git - pve-common.git/blame - src/PVE/Network.pm
network: fix default of new bridge learning flag
[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;
d7cafe51 12use JSON;
bf52d27b 13use Net::IP;
8286ef53 14use NetAddr::IP qw(:lower);
f27d5e6b
TL
15use POSIX qw(ECONNREFUSED);
16use Socket qw(NI_NUMERICHOST NI_NUMERICSERV);
bf52d27b 17
b9436cda
DM
18# host network related utility functions
19
19819404 20our $PHYSICAL_NIC_RE = qr/(?:eth\d+|en[^:.]+|ib[^:.]+)/;
3dabe28a 21
61aa94e4
WB
22our $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
58our $ipv4_mask_hash_localnet = {
19e609fd
WB
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,
61aa94e4
WB
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,
e43faad9
WB
82 '255.255.255.254' => 31,
83 '255.255.255.255' => 32,
61aa94e4
WB
84};
85
74d1b045 86sub setup_tc_rate_limit {
6256f2c3 87 my ($iface, $rate, $burst) = @_;
74d1b045 88
2d6b3a90
FG
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"); };
74d1b045 94
d6f2623b 95 return if !$rate;
957753df 96
74d1b045
DM
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
5d35df41
W
104 run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress");
105 run_command("/sbin/tc filter add dev $iface parent ffff: " .
1b915170 106 "prio 50 basic " .
5d35df41 107 "police rate ${rate}bps burst ${burst}b mtu 64kb " .
edbdf0b2 108 "drop");
74d1b045
DM
109}
110
ec9ada18
AD
111sub tap_rate_limit {
112 my ($iface, $rate) = @_;
113
ad066ae2 114 $rate = int($rate*1024*1024) if $rate;
ec9ada18
AD
115 my $burst = 1024*1024;
116
6256f2c3 117 setup_tc_rate_limit($iface, $rate, $burst);
ec9ada18 118}
74d1b045 119
1b6ad61c 120sub read_bridge_mtu {
605bb891
DM
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
32cb7d27 132my $parse_tap_device_name = sub {
6c80e6d6 133 my ($iface, $noerr) = @_;
605bb891
DM
134
135 my ($vmid, $devid);
136
137 if ($iface =~ m/^tap(\d+)i(\d+)$/) {
138 $vmid = $1;
139 $devid = $2;
32cb7d27 140 } elsif ($iface =~ m/^veth(\d+)i(\d+)$/) {
605bb891
DM
141 $vmid = $1;
142 $devid = $2;
143 } else {
6c80e6d6
DM
144 return undef if $noerr;
145 die "can't create firewall bridge for random interface name '$iface'\n";
605bb891
DM
146 }
147
148 return ($vmid, $devid);
149};
150
70ab4434 151my $compute_fwbr_names = sub {
605bb891
DM
152 my ($vmid, $devid) = @_;
153
154 my $fwbr = "fwbr${vmid}i${devid}";
f193aa74 155 # Note: the firewall use 'fwln+' to filter traffic to VMs
7d78a966
AD
156 my $vethfw = "fwln${vmid}i${devid}";
157 my $vethfwpeer = "fwpr${vmid}p${devid}";
158 my $ovsintport = "fwln${vmid}o${devid}";
605bb891 159
70ab4434 160 return ($fwbr, $vethfw, $vethfwpeer, $ovsintport);
605bb891
DM
161};
162
e9b54cc6
WB
163sub 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
169sub 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
175sub 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:
182sub 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
605bb891
DM
193my $cond_create_bridge = sub {
194 my ($bridge) = @_;
195
196 if (! -d "/sys/class/net/$bridge") {
e9b54cc6 197 iface_create($bridge, 'bridge');
86b84237 198 disable_ipv6($bridge);
605bb891
DM
199 }
200};
201
f3ccd9b4
WB
202sub 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
354ec8de
AD
211my $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
605bb891 219my $bridge_add_interface = sub {
b0b34ffd 220 my ($bridge, $iface, $tag, $trunks) = @_;
605bb891 221
f3ccd9b4
WB
222 # drop link local address (it can't be used when on a bridge anyway)
223 disable_ipv6($iface);
e9b54cc6 224 iface_set_master($iface, $bridge);
4d25f4aa
AD
225
226 my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
227
228 if ($vlan_aware) {
aa91ae3d
AD
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 $@;
4d25f4aa 247 }
605bb891
DM
248};
249
70ab4434 250my $ovs_bridge_add_port = sub {
b0b34ffd
AD
251 my ($bridge, $iface, $tag, $internal, $trunks) = @_;
252
253 $trunks =~ s/;/,/g if $trunks;
70ab4434 254
89ea13ef
FG
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 $@;
b0b34ffd 269
f3ccd9b4 270 disable_ipv6($iface);
70ab4434
DM
271};
272
605bb891
DM
273my $activate_interface = sub {
274 my ($iface) = @_;
275
89ea13ef
FG
276 eval { run_command(['/sbin/ip', 'link', 'set', $iface, 'up']) };
277 die "can't activate interface '$iface' - $@\n" if $@;
605bb891
DM
278};
279
354ec8de
AD
280sub 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
b8638604 289 run_command(['/sbin/bridge', 'fdb', 'append', $mac, 'dev', $iface, 'master', 'static']);
354ec8de
AD
290
291 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
292
293 if (-d "/sys/class/net/$vethfwpeer") {
b8638604 294 run_command(['/sbin/bridge', 'fdb', 'append', $mac, 'dev', $vethfwpeer, 'master', 'static']);
354ec8de
AD
295 }
296
297}
298
299sub 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
b8638604 308 run_command(['/sbin/bridge', 'fdb', 'del', $mac, 'dev', $iface, 'master', 'static']);
354ec8de
AD
309
310 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
311
312 if (-d "/sys/class/net/$vethfwpeer") {
b8638604 313 run_command(['/sbin/bridge', 'fdb', 'del', $mac, 'dev', $vethfwpeer, 'master', 'static']);
354ec8de
AD
314 }
315}
316
3aa99c70
AD
317sub tap_create {
318 my ($iface, $bridge) = @_;
319
320 die "unable to get bridge setting\n" if !$bridge;
321
1b6ad61c 322 my $bridgemtu = read_bridge_mtu($bridge);
3aa99c70 323
9bbc4e17 324 eval {
f3ccd9b4 325 disable_ipv6($iface);
b8638604 326 run_command(['/sbin/ip', 'link', 'set', $iface, 'up', 'promisc', 'on', 'mtu', $bridgemtu]);
098795e0
DM
327 };
328 die "interface activation failed\n" if $@;
3aa99c70
AD
329}
330
35efc4eb
AD
331sub veth_create {
332 my ($veth, $vethpeer, $bridge, $mac) = @_;
333
334 die "unable to get bridge setting\n" if !$bridge;
335
1b6ad61c 336 my $bridgemtu = read_bridge_mtu($bridge);
35efc4eb
AD
337
338 # create veth pair
339 if (! -d "/sys/class/net/$veth") {
89ea13ef
FG
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 $@;
35efc4eb
AD
352 }
353
354 # up vethpair
f3ccd9b4
WB
355 disable_ipv6($veth);
356 disable_ipv6($vethpeer);
35efc4eb
AD
357 &$activate_interface($veth);
358 &$activate_interface($vethpeer);
359}
360
f3f0bc3a
AD
361sub veth_delete {
362 my ($veth) = @_;
363
364 if (-d "/sys/class/net/$veth") {
e9b54cc6 365 iface_delete($veth);
f3f0bc3a 366 }
e0a862e2 367 eval { tap_unplug($veth) };
f3f0bc3a 368}
35efc4eb 369
605bb891 370my $create_firewall_bridge_linux = sub {
93cc2aa9 371 my ($iface, $bridge, $tag, $trunks, $no_learning) = @_;
605bb891 372
32cb7d27 373 my ($vmid, $devid) = &$parse_tap_device_name($iface);
70ab4434 374 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
605bb891 375
605bb891
DM
376 &$cond_create_bridge($fwbr);
377 &$activate_interface($fwbr);
378
379 copy_bridge_config($bridge, $fwbr);
35efc4eb 380 veth_create($vethfw, $vethfwpeer, $bridge);
605bb891 381
b0b34ffd 382 &$bridge_add_interface($bridge, $vethfwpeer, $tag, $trunks);
93cc2aa9 383 &$bridge_disable_interface_learning($vethfwpeer) if $no_learning;
354ec8de 384 &$bridge_add_interface($fwbr, $vethfw);
605bb891 385
4d25f4aa 386 &$bridge_add_interface($fwbr, $iface);
605bb891
DM
387};
388
70ab4434 389my $create_firewall_bridge_ovs = sub {
93cc2aa9 390 my ($iface, $bridge, $tag, $trunks, $no_learning) = @_;
70ab4434 391
32cb7d27 392 my ($vmid, $devid) = &$parse_tap_device_name($iface);
70ab4434
DM
393 my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
394
1b6ad61c 395 my $bridgemtu = read_bridge_mtu($bridge);
70ab4434
DM
396
397 &$cond_create_bridge($fwbr);
398 &$activate_interface($fwbr);
399
400 &$bridge_add_interface($fwbr, $iface);
401
b0b34ffd 402 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1, $trunks);
ac3a04b8 403 &$activate_interface($ovsintport);
70ab4434
DM
404
405 # set the same mtu for ovs int port
b8638604 406 run_command(['/sbin/ip', 'link', 'set', $ovsintport, 'mtu', $bridgemtu]);
9bbc4e17 407
70ab4434 408 &$bridge_add_interface($fwbr, $ovsintport);
93cc2aa9 409 &$bridge_disable_interface_learning($ovsintport) if $no_learning;
70ab4434
DM
410};
411
412my $cleanup_firewall_bridge = sub {
605bb891
DM
413 my ($iface) = @_;
414
32cb7d27 415 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
9bbc4e17 416 return if !defined($vmid);
70ab4434
DM
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 }
605bb891
DM
423
424 # delete old vethfw interface
f3f0bc3a 425 veth_delete($vethfw);
605bb891
DM
426
427 # cleanup fwbr bridge
428 if (-d "/sys/class/net/$fwbr") {
e9b54cc6 429 iface_delete($fwbr);
605bb891
DM
430 }
431};
432
f0c190ee 433sub tap_plug {
93cc2aa9 434 my ($iface, $bridge, $tag, $firewall, $trunks, $rate, $opts) = @_;
f0c190ee 435
93cc2aa9
TL
436 $opts = {} if !defined($opts);
437
c1978f2e 438 my $no_learning = defined($opts->{learning}) && !$opts->{learning}; # default to learning on
93cc2aa9
TL
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 };
4cbabd40 444
098795e0 445 if (-d "/sys/class/net/$bridge/bridge") {
70ab4434 446 &$cleanup_firewall_bridge($iface); # remove stale devices
605bb891 447
4d25f4aa 448 my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
098795e0 449
4d25f4aa 450 if (!$vlan_aware) {
b0b34ffd 451 die "vlan aware feature need to be enabled to use trunks" if $trunks;
4d25f4aa
AD
452 my $newbridge = activate_bridge_vlan($bridge, $tag);
453 copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
ff042056 454 $bridge = $newbridge;
4d25f4aa
AD
455 $tag = undef;
456 }
457
458 if ($firewall) {
93cc2aa9 459 &$create_firewall_bridge_linux($iface, $bridge, $tag, $trunks, $no_learning);
4d25f4aa 460 } else {
b0b34ffd 461 &$bridge_add_interface($bridge, $iface, $tag, $trunks);
4d25f4aa 462 }
93cc2aa9 463 $bridge_disable_interface_learning->($iface) if $no_learning;
605bb891 464
098795e0 465 } else {
70ab4434
DM
466 &$cleanup_firewall_bridge($iface); # remove stale devices
467
468 if ($firewall) {
93cc2aa9 469 &$create_firewall_bridge_ovs($iface, $bridge, $tag, $trunks, $no_learning);
70ab4434 470 } else {
b0b34ffd 471 &$ovs_bridge_add_port($bridge, $iface, $tag, undef, $trunks);
70ab4434 472 }
4cbabd40 473 }
bce2a5b3
WB
474
475 tap_rate_limit($iface, $rate);
f0c190ee
AD
476}
477
a84b65c0 478sub tap_unplug {
2db1cc0d 479 my ($iface) = @_;
a84b65c0 480
2db1cc0d
DM
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+)/;
4cbabd40 486
e9b54cc6 487 iface_set_master($iface, undef);
4cbabd40 488 }
9bbc4e17 489
70ab4434 490 &$cleanup_firewall_bridge($iface);
dd44486e
WB
491 #cleanup old port config from any openvswitch bridge
492 eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) };
a84b65c0
AD
493}
494
b9436cda
DM
495sub copy_bridge_config {
496 my ($br0, $br1) = @_;
497
498 return if $br0 eq $br1;
499
b8638604
TL
500 my $br_configs = [
501 'ageing_time', 'stp_state', 'priority', 'forward_delay',
502 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier',
503 ];
b9436cda
DM
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) {
aec04803 510 PVE::ProcFSTools::write_proc_entry("/sys/class/net/$br1/bridge/$sysname", $v0);
b9436cda
DM
511 }
512 };
513 warn $@ if $@;
514 }
515}
516
70d89745
PRG
517sub activate_bridge_vlan_slave {
518 my ($bridgevlan, $iface, $tag) = @_;
b9436cda 519 my $ifacevlan = "${iface}.$tag";
9bbc4e17 520
b9436cda
DM
521 # create vlan on $iface is not already exist
522 if (! -d "/sys/class/net/$ifacevlan") {
89ea13ef
FG
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 $@;
b9436cda 531
86b84237
WB
532 # remove ipv6 link-local address before activation
533 disable_ipv6($ifacevlan);
534 }
f3ccd9b4 535
b9436cda 536 # be sure to have the $ifacevlan up
605bb891 537 &$activate_interface($ifacevlan);
b9436cda
DM
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));
70d89745 543 if ($tbridge ne $bridgevlan) {
b9436cda 544 die "interface $ifacevlan already exist in bridge $tbridge\n";
eee4b32a
PRG
545 } else {
546 # Port already attached to bridge: do nothing.
547 return;
b9436cda
DM
548 }
549 }
550
70d89745 551 # add $ifacevlan to the bridge
605bb891 552 &$bridge_add_interface($bridgevlan, $ifacevlan);
70d89745
PRG
553}
554
555sub 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
c9030d97
PRG
568 my @ifaces = ();
569 my $dir = "/sys/class/net/$bridge/brif";
899f8c4a 570 PVE::Tools::dir_glob_foreach($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub {
5ffa7628 571 push @ifaces, $_[0];
c9030d97
PRG
572 });
573
5ffa7628 574 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
c9030d97 575
a712bf6e
WB
576 lock_network(sub {
577 # add bridgevlan if it doesn't already exist
578 if (! -d "/sys/class/net/$bridgevlan") {
e9b54cc6 579 iface_create($bridgevlan, 'bridge');
a712bf6e 580 }
b9436cda 581
a712bf6e
WB
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 }
70d89745 586
a712bf6e 587 #fixme: set other bridge flags
b9436cda 588
f3ccd9b4
WB
589 # remove ipv6 link-local address before activation
590 disable_ipv6($bridgevlan);
a712bf6e 591 # be sure to have the bridge up
f3ccd9b4 592 &$activate_interface($bridgevlan);
a712bf6e 593 });
b9436cda
DM
594 return $bridgevlan;
595}
596
b6bff92e
WB
597sub 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
bf52d27b
WB
625sub 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
644sub 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
123c3104
FE
653 my $overlap = $cidr_obj->overlaps($ip_obj);
654
b0e3bcc1
FE
655 return if !defined($overlap);
656
123c3104 657 return $overlap == $Net::IP::IP_B_IN_A_OVERLAP || $overlap == $Net::IP::IP_IDENTICAL;
bf52d27b
WB
658}
659
d7cafe51
TL
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" }
663sub get_reachable_networks {
664 my $raw = '';
665 run_command([qw(ip -j addr show up scope global)], outfunc => sub { $raw .= shift });
4e405958 666 my $decoded = decode_json($raw);
d7cafe51 667
4e405958
TL
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 }
d7cafe51 673 my $res = [];
4e405958
TL
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 };
d7cafe51
TL
680 }
681
682 return $res;
683}
beb9820f 684
ac487a88
TL
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.
691sub 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
97809c69 712 my $live = eval { get_reachable_networks() } // [];
ac487a88
TL
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
beb9820f
TL
739sub get_local_ip_from_cidr {
740 my ($cidr) = @_;
741
1e55a6cd 742 my $IPs = {};
ef737f0b 743 my $i = 1;
b15e50dd
TL
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+)!) {
ef737f0b 746 $IPs->{$1} = $i++ if !exists($IPs->{$1});
beb9820f 747 }
b15e50dd 748 });
beb9820f 749
ef737f0b 750 return [ sort { $IPs->{$a} <=> $IPs->{$b} } keys %{$IPs} ];
beb9820f
TL
751}
752
87aa00de
TL
753sub 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
761sub get_ip_from_hostname {
762 my ($hostname, $noerr) = @_;
763
5bd1e56b 764 my @res = eval { PVE::Tools::getaddrinfo_all($hostname) };
87aa00de 765 if ($@) {
4ed6974a 766 die "hostname lookup '$hostname' failed - $@" if !$noerr;
87aa00de
TL
767 return undef;
768 }
769
5bd1e56b 770 for my $ai (@res) {
29dde5f4
TL
771 my $ip = addr_to_ip($ai->{addr});
772 if ($ip !~ m/^127\.|^::1$/) {
773 return wantarray ? ($ip, $ai->{family}) : $ip;
5bd1e56b
TL
774 }
775 }
29dde5f4
TL
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;
87aa00de
TL
779}
780
a712bf6e
WB
781sub 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
8286ef53
FE
788# the canonical form of the given IP, i.e. dotted quad for IPv4 and RFC 5952 for IPv6
789sub 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
8f75194c
FE
797# List of unique, canonical IPs in the provided list.
798# Keeps the original order, filtering later duplicates.
799sub 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
b9436cda 8171;