]>
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 | 104 | "police rate ${rate}bps burst ${burst}b mtu 64kb " . |
edbdf0b2 | 105 | "drop"); |
5d35df41 | 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 | ||
e9b54cc6 WB |
168 | sub iface_delete($) { |
169 | my ($iface) = @_; | |
170 | run_command(['/sbin/ip', 'link', 'delete', 'dev', $iface], noerr => 1) | |
171 | == 0 or die "failed to delete interface '$iface'\n"; | |
172 | } | |
173 | ||
174 | sub iface_create($$@) { | |
175 | my ($iface, $type, @args) = @_; | |
176 | run_command(['/sbin/ip', 'link', 'add', $iface, 'type', $type, @args], noerr => 1) | |
177 | == 0 or die "failed to create interface '$iface'\n"; | |
178 | } | |
179 | ||
180 | sub iface_set($@) { | |
181 | my ($iface, @opts) = @_; | |
182 | run_command(['/sbin/ip', 'link', 'set', $iface, @opts], noerr => 1) | |
183 | == 0 or die "failed to set interface options for '$iface' (".join(' ', @opts).")\n"; | |
184 | } | |
185 | ||
186 | # helper for nicer error messages: | |
187 | sub iface_set_master($$) { | |
188 | my ($iface, $master) = @_; | |
189 | if (defined($master)) { | |
190 | eval { iface_set($iface, 'master', $master) }; | |
191 | die "can't enslave '$iface' to '$master'\n" if $@; | |
192 | } else { | |
193 | eval { iface_set($iface, 'nomaster') }; | |
194 | die "can't unenslave '$iface'\n" if $@; | |
195 | } | |
196 | } | |
197 | ||
605bb891 DM |
198 | my $cond_create_bridge = sub { |
199 | my ($bridge) = @_; | |
200 | ||
201 | if (! -d "/sys/class/net/$bridge") { | |
e9b54cc6 | 202 | iface_create($bridge, 'bridge'); |
86b84237 | 203 | disable_ipv6($bridge); |
605bb891 DM |
204 | } |
205 | }; | |
206 | ||
f3ccd9b4 WB |
207 | sub disable_ipv6 { |
208 | my ($iface) = @_; | |
209 | return if !-d '/proc/sys/net/ipv6'; # ipv6 might be completely disabled | |
210 | my $file = "/proc/sys/net/ipv6/conf/$iface/disable_ipv6"; | |
211 | open(my $fh, '>', $file) or die "failed to open $file for writing: $!\n"; | |
212 | print {$fh} "1\n" or die "failed to disable link-local ipv6 for $iface\n"; | |
213 | close($fh); | |
214 | } | |
215 | ||
605bb891 | 216 | my $bridge_add_interface = sub { |
b0b34ffd | 217 | my ($bridge, $iface, $tag, $trunks) = @_; |
605bb891 | 218 | |
f3ccd9b4 WB |
219 | # drop link local address (it can't be used when on a bridge anyway) |
220 | disable_ipv6($iface); | |
e9b54cc6 | 221 | iface_set_master($iface, $bridge); |
4d25f4aa AD |
222 | |
223 | my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering"); | |
224 | ||
225 | if ($vlan_aware) { | |
226 | if ($tag) { | |
78e912a3 WB |
227 | system({'/sbin/bridge'} 'bridge', 'vlan', 'del', 'dev', $iface, 'vid', '1-4094') == 0 |
228 | or die "failed to remove default vlan tags of $iface\n"; | |
229 | system({'/sbin/bridge'} 'bridge', 'vlan', 'add', 'dev', $iface, 'vid', $tag, 'pvid', 'untagged') == 0 | |
230 | or die "unable to add vlan $tag to interface $iface\n"; | |
5d662b31 DC |
231 | |
232 | warn "Caution: Setting VLAN ID 1 on a VLAN aware bridge may be dangerous\n" if $tag == 1; | |
4d25f4aa AD |
233 | } else { |
234 | system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 || | |
b0b34ffd | 235 | die "unable to add default vlan tags to interface $iface\n" if !$trunks; |
4d25f4aa | 236 | } |
b0b34ffd | 237 | |
846337ad WB |
238 | if ($trunks) { |
239 | my @trunks_array = split /;/, $trunks; | |
240 | foreach my $trunk (@trunks_array) { | |
241 | system("/sbin/bridge vlan add dev $iface vid $trunk") == 0 || | |
242 | die "unable to add vlan $trunk to interface $iface\n"; | |
243 | } | |
b0b34ffd | 244 | } |
4d25f4aa | 245 | } |
605bb891 DM |
246 | }; |
247 | ||
70ab4434 | 248 | my $ovs_bridge_add_port = sub { |
b0b34ffd AD |
249 | my ($bridge, $iface, $tag, $internal, $trunks) = @_; |
250 | ||
251 | $trunks =~ s/;/,/g if $trunks; | |
70ab4434 DM |
252 | |
253 | my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface"; | |
254 | $cmd .= " tag=$tag" if $tag; | |
b0b34ffd AD |
255 | $cmd .= " trunks=". join(',', $trunks) if $trunks; |
256 | $cmd .= " vlan_mode=native-untagged" if $tag && $trunks; | |
257 | ||
70ab4434 DM |
258 | $cmd .= " -- set Interface $iface type=internal" if $internal; |
259 | system($cmd) == 0 || | |
260 | die "can't add ovs port '$iface'\n"; | |
f3ccd9b4 | 261 | disable_ipv6($iface); |
70ab4434 DM |
262 | }; |
263 | ||
605bb891 DM |
264 | my $activate_interface = sub { |
265 | my ($iface) = @_; | |
266 | ||
267 | system("/sbin/ip link set $iface up") == 0 || | |
268 | die "can't activate interface '$iface'\n"; | |
269 | }; | |
270 | ||
3aa99c70 AD |
271 | sub tap_create { |
272 | my ($iface, $bridge) = @_; | |
273 | ||
274 | die "unable to get bridge setting\n" if !$bridge; | |
275 | ||
605bb891 | 276 | my $bridgemtu = &$read_bridge_mtu($bridge); |
3aa99c70 | 277 | |
098795e0 | 278 | eval { |
f3ccd9b4 | 279 | disable_ipv6($iface); |
86330049 | 280 | PVE::Tools::run_command(['/sbin/ip', 'link', 'set', $iface, 'up', 'promisc', 'on', 'mtu', $bridgemtu]); |
098795e0 DM |
281 | }; |
282 | die "interface activation failed\n" if $@; | |
3aa99c70 AD |
283 | } |
284 | ||
35efc4eb AD |
285 | sub veth_create { |
286 | my ($veth, $vethpeer, $bridge, $mac) = @_; | |
287 | ||
288 | die "unable to get bridge setting\n" if !$bridge; | |
289 | ||
290 | my $bridgemtu = &$read_bridge_mtu($bridge); | |
291 | ||
292 | # create veth pair | |
293 | if (! -d "/sys/class/net/$veth") { | |
294 | my $cmd = "/sbin/ip link add name $veth type veth peer name $vethpeer mtu $bridgemtu"; | |
295 | $cmd .= " addr $mac" if $mac; | |
296 | system($cmd) == 0 || die "can't create interface $veth\n"; | |
297 | } | |
298 | ||
299 | # up vethpair | |
f3ccd9b4 WB |
300 | disable_ipv6($veth); |
301 | disable_ipv6($vethpeer); | |
35efc4eb AD |
302 | &$activate_interface($veth); |
303 | &$activate_interface($vethpeer); | |
304 | } | |
305 | ||
f3f0bc3a AD |
306 | sub veth_delete { |
307 | my ($veth) = @_; | |
308 | ||
309 | if (-d "/sys/class/net/$veth") { | |
e9b54cc6 | 310 | iface_delete($veth); |
f3f0bc3a | 311 | } |
e0a862e2 | 312 | eval { tap_unplug($veth) }; |
f3f0bc3a | 313 | } |
35efc4eb | 314 | |
605bb891 | 315 | my $create_firewall_bridge_linux = sub { |
b0b34ffd | 316 | my ($iface, $bridge, $tag, $trunks) = @_; |
605bb891 | 317 | |
32cb7d27 | 318 | my ($vmid, $devid) = &$parse_tap_device_name($iface); |
70ab4434 | 319 | my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid); |
605bb891 | 320 | |
605bb891 DM |
321 | &$cond_create_bridge($fwbr); |
322 | &$activate_interface($fwbr); | |
323 | ||
324 | copy_bridge_config($bridge, $fwbr); | |
35efc4eb | 325 | veth_create($vethfw, $vethfwpeer, $bridge); |
605bb891 | 326 | |
7d78a966 | 327 | &$bridge_add_interface($fwbr, $vethfw); |
b0b34ffd | 328 | &$bridge_add_interface($bridge, $vethfwpeer, $tag, $trunks); |
605bb891 | 329 | |
4d25f4aa | 330 | &$bridge_add_interface($fwbr, $iface); |
605bb891 DM |
331 | }; |
332 | ||
70ab4434 | 333 | my $create_firewall_bridge_ovs = sub { |
b0b34ffd | 334 | my ($iface, $bridge, $tag, $trunks) = @_; |
70ab4434 | 335 | |
32cb7d27 | 336 | my ($vmid, $devid) = &$parse_tap_device_name($iface); |
70ab4434 DM |
337 | my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid); |
338 | ||
339 | my $bridgemtu = &$read_bridge_mtu($bridge); | |
340 | ||
341 | &$cond_create_bridge($fwbr); | |
342 | &$activate_interface($fwbr); | |
343 | ||
344 | &$bridge_add_interface($fwbr, $iface); | |
345 | ||
b0b34ffd | 346 | &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1, $trunks); |
ac3a04b8 | 347 | &$activate_interface($ovsintport); |
70ab4434 DM |
348 | |
349 | # set the same mtu for ovs int port | |
86330049 | 350 | PVE::Tools::run_command(['/sbin/ip', 'link', 'set', $ovsintport, 'mtu', $bridgemtu]); |
70ab4434 DM |
351 | |
352 | &$bridge_add_interface($fwbr, $ovsintport); | |
353 | }; | |
354 | ||
355 | my $cleanup_firewall_bridge = sub { | |
605bb891 DM |
356 | my ($iface) = @_; |
357 | ||
32cb7d27 | 358 | my ($vmid, $devid) = &$parse_tap_device_name($iface, 1); |
6c80e6d6 | 359 | return if !defined($vmid); |
70ab4434 DM |
360 | my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid); |
361 | ||
362 | # cleanup old port config from any openvswitch bridge | |
363 | if (-d "/sys/class/net/$ovsintport") { | |
364 | run_command("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc => sub {}, errfunc => sub {}); | |
365 | } | |
605bb891 DM |
366 | |
367 | # delete old vethfw interface | |
f3f0bc3a | 368 | veth_delete($vethfw); |
605bb891 DM |
369 | |
370 | # cleanup fwbr bridge | |
371 | if (-d "/sys/class/net/$fwbr") { | |
e9b54cc6 | 372 | iface_delete($fwbr); |
605bb891 DM |
373 | } |
374 | }; | |
375 | ||
f0c190ee | 376 | sub tap_plug { |
bce2a5b3 | 377 | my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_; |
f0c190ee | 378 | |
4cbabd40 AD |
379 | #cleanup old port config from any openvswitch bridge |
380 | eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) }; | |
381 | ||
098795e0 | 382 | if (-d "/sys/class/net/$bridge/bridge") { |
70ab4434 | 383 | &$cleanup_firewall_bridge($iface); # remove stale devices |
605bb891 | 384 | |
4d25f4aa | 385 | my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering"); |
098795e0 | 386 | |
4d25f4aa | 387 | if (!$vlan_aware) { |
b0b34ffd | 388 | die "vlan aware feature need to be enabled to use trunks" if $trunks; |
4d25f4aa AD |
389 | my $newbridge = activate_bridge_vlan($bridge, $tag); |
390 | copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge; | |
ff042056 | 391 | $bridge = $newbridge; |
4d25f4aa AD |
392 | $tag = undef; |
393 | } | |
394 | ||
395 | if ($firewall) { | |
b0b34ffd | 396 | &$create_firewall_bridge_linux($iface, $bridge, $tag, $trunks); |
4d25f4aa | 397 | } else { |
b0b34ffd | 398 | &$bridge_add_interface($bridge, $iface, $tag, $trunks); |
4d25f4aa | 399 | } |
605bb891 | 400 | |
098795e0 | 401 | } else { |
70ab4434 DM |
402 | &$cleanup_firewall_bridge($iface); # remove stale devices |
403 | ||
404 | if ($firewall) { | |
b0b34ffd | 405 | &$create_firewall_bridge_ovs($iface, $bridge, $tag, $trunks); |
70ab4434 | 406 | } else { |
b0b34ffd | 407 | &$ovs_bridge_add_port($bridge, $iface, $tag, undef, $trunks); |
70ab4434 | 408 | } |
4cbabd40 | 409 | } |
bce2a5b3 WB |
410 | |
411 | tap_rate_limit($iface, $rate); | |
f0c190ee AD |
412 | } |
413 | ||
a84b65c0 | 414 | sub tap_unplug { |
2db1cc0d | 415 | my ($iface) = @_; |
a84b65c0 | 416 | |
2db1cc0d DM |
417 | my $path= "/sys/class/net/$iface/brport/bridge"; |
418 | if (-l $path) { | |
419 | my $bridge = basename(readlink($path)); | |
420 | #avoid insecure dependency; | |
421 | ($bridge) = $bridge =~ /(\S+)/; | |
4cbabd40 | 422 | |
e9b54cc6 | 423 | iface_set_master($iface, undef); |
4cbabd40 | 424 | } |
70ab4434 DM |
425 | |
426 | &$cleanup_firewall_bridge($iface); | |
dd44486e WB |
427 | #cleanup old port config from any openvswitch bridge |
428 | eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) }; | |
a84b65c0 AD |
429 | } |
430 | ||
b9436cda DM |
431 | sub copy_bridge_config { |
432 | my ($br0, $br1) = @_; | |
433 | ||
434 | return if $br0 eq $br1; | |
435 | ||
436 | my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay', | |
ba4af65b | 437 | 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier']; |
b9436cda DM |
438 | |
439 | foreach my $sysname (@$br_configs) { | |
440 | eval { | |
441 | my $v0 = PVE::Tools::file_read_firstline("/sys/class/net/$br0/bridge/$sysname"); | |
442 | my $v1 = PVE::Tools::file_read_firstline("/sys/class/net/$br1/bridge/$sysname"); | |
443 | if ($v0 ne $v1) { | |
aec04803 | 444 | PVE::ProcFSTools::write_proc_entry("/sys/class/net/$br1/bridge/$sysname", $v0); |
b9436cda DM |
445 | } |
446 | }; | |
447 | warn $@ if $@; | |
448 | } | |
449 | } | |
450 | ||
70d89745 PRG |
451 | sub activate_bridge_vlan_slave { |
452 | my ($bridgevlan, $iface, $tag) = @_; | |
b9436cda | 453 | my $ifacevlan = "${iface}.$tag"; |
70d89745 | 454 | |
b9436cda DM |
455 | # create vlan on $iface is not already exist |
456 | if (! -d "/sys/class/net/$ifacevlan") { | |
f3ccd9b4 | 457 | system("/sbin/ip link add link $iface name $ifacevlan type vlan id $tag") == 0 || |
02c9a6b4 | 458 | die "can't add vlan tag $tag to interface $iface\n"; |
b9436cda | 459 | |
86b84237 WB |
460 | # remove ipv6 link-local address before activation |
461 | disable_ipv6($ifacevlan); | |
462 | } | |
f3ccd9b4 | 463 | |
b9436cda | 464 | # be sure to have the $ifacevlan up |
605bb891 | 465 | &$activate_interface($ifacevlan); |
b9436cda DM |
466 | |
467 | # test if $vlaniface is already enslaved in another bridge | |
468 | my $path= "/sys/class/net/$ifacevlan/brport/bridge"; | |
469 | if (-l $path) { | |
470 | my $tbridge = basename(readlink($path)); | |
70d89745 | 471 | if ($tbridge ne $bridgevlan) { |
b9436cda | 472 | die "interface $ifacevlan already exist in bridge $tbridge\n"; |
eee4b32a PRG |
473 | } else { |
474 | # Port already attached to bridge: do nothing. | |
475 | return; | |
b9436cda DM |
476 | } |
477 | } | |
478 | ||
70d89745 | 479 | # add $ifacevlan to the bridge |
605bb891 | 480 | &$bridge_add_interface($bridgevlan, $ifacevlan); |
70d89745 PRG |
481 | } |
482 | ||
483 | sub activate_bridge_vlan { | |
484 | my ($bridge, $tag_param) = @_; | |
485 | ||
486 | die "bridge '$bridge' is not active\n" if ! -d "/sys/class/net/$bridge"; | |
487 | ||
488 | return $bridge if !defined($tag_param); # no vlan, simply return | |
489 | ||
490 | my $tag = int($tag_param); | |
491 | ||
492 | die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094; | |
493 | ||
494 | my $bridgevlan = "${bridge}v$tag"; | |
495 | ||
c9030d97 PRG |
496 | my @ifaces = (); |
497 | my $dir = "/sys/class/net/$bridge/brif"; | |
899f8c4a | 498 | PVE::Tools::dir_glob_foreach($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub { |
5ffa7628 | 499 | push @ifaces, $_[0]; |
c9030d97 PRG |
500 | }); |
501 | ||
5ffa7628 | 502 | die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0; |
c9030d97 | 503 | |
a712bf6e WB |
504 | lock_network(sub { |
505 | # add bridgevlan if it doesn't already exist | |
506 | if (! -d "/sys/class/net/$bridgevlan") { | |
e9b54cc6 | 507 | iface_create($bridgevlan, 'bridge'); |
a712bf6e | 508 | } |
b9436cda | 509 | |
a712bf6e WB |
510 | # for each physical interface (eth or bridge) bind them to bridge vlan |
511 | foreach my $iface (@ifaces) { | |
512 | activate_bridge_vlan_slave($bridgevlan, $iface, $tag); | |
513 | } | |
70d89745 | 514 | |
a712bf6e | 515 | #fixme: set other bridge flags |
b9436cda | 516 | |
f3ccd9b4 WB |
517 | # remove ipv6 link-local address before activation |
518 | disable_ipv6($bridgevlan); | |
a712bf6e | 519 | # be sure to have the bridge up |
f3ccd9b4 | 520 | &$activate_interface($bridgevlan); |
a712bf6e | 521 | }); |
b9436cda DM |
522 | return $bridgevlan; |
523 | } | |
524 | ||
b6bff92e WB |
525 | sub tcp_ping { |
526 | my ($host, $port, $timeout) = @_; | |
527 | ||
528 | my $refused = 1; | |
529 | ||
530 | $timeout = 3 if !$timeout; # sane default | |
531 | if (!$port) { | |
532 | # Net::Ping defaults to the echo port | |
533 | $port = 7; | |
534 | } else { | |
535 | # Net::Ping's port_number() implies service_check(1) | |
536 | $refused = 0; | |
537 | } | |
538 | ||
539 | my ($sock, $result); | |
540 | eval { | |
541 | $result = PVE::Tools::run_with_timeout($timeout, sub { | |
542 | $sock = IO::Socket::IP->new(PeerHost => $host, PeerPort => $port, Type => SOCK_STREAM); | |
543 | $result = $refused if $! == ECONNREFUSED; | |
544 | }); | |
545 | }; | |
546 | if ($sock) { | |
547 | $sock->close(); | |
548 | $result = 1; | |
549 | } | |
550 | return $result; | |
551 | } | |
552 | ||
bf52d27b WB |
553 | sub IP_from_cidr { |
554 | my ($cidr, $version) = @_; | |
555 | ||
556 | return if $cidr !~ m!^(\S+?)/(\S+)$!; | |
557 | my ($ip, $prefix) = ($1, $2); | |
558 | ||
559 | my $ipobj = Net::IP->new($ip, $version); | |
560 | return if !$ipobj; | |
561 | ||
562 | $version = $ipobj->version(); | |
563 | ||
564 | my $binmask = Net::IP::ip_get_mask($prefix, $version); | |
565 | return if !$binmask; | |
566 | ||
567 | my $masked_binip = $ipobj->binip() & $binmask; | |
568 | my $masked_ip = Net::IP::ip_bintoip($masked_binip, $version); | |
569 | return Net::IP->new("$masked_ip/$prefix"); | |
570 | } | |
571 | ||
572 | sub is_ip_in_cidr { | |
573 | my ($ip, $cidr, $version) = @_; | |
574 | ||
575 | my $cidr_obj = IP_from_cidr($cidr, $version); | |
576 | return undef if !$cidr_obj; | |
577 | ||
578 | my $ip_obj = Net::IP->new($ip, $version); | |
579 | return undef if !$ip_obj; | |
580 | ||
581 | return $cidr_obj->overlaps($ip_obj) == $Net::IP::IP_B_IN_A_OVERLAP; | |
582 | } | |
583 | ||
beb9820f TL |
584 | |
585 | sub get_local_ip_from_cidr { | |
586 | my ($cidr) = @_; | |
587 | ||
588 | my $cmd = ['/sbin/ip', 'address', 'show', 'to', $cidr, 'up']; | |
589 | ||
590 | my $IPs = []; | |
591 | ||
592 | my $code = sub { | |
593 | my $line = shift; | |
594 | ||
595 | if ($line =~ m!^\s*inet(?:6)?\s+($PVE::Tools::IPRE)/\d+!) { | |
596 | push @$IPs, $1; | |
597 | } | |
598 | }; | |
599 | ||
600 | PVE::Tools::run_command($cmd, outfunc => $code); | |
601 | ||
602 | return $IPs; | |
603 | } | |
604 | ||
87aa00de TL |
605 | sub addr_to_ip { |
606 | my ($addr) = @_; | |
607 | my ($err, $host, $port) = Socket::getnameinfo($addr, NI_NUMERICHOST | NI_NUMERICSERV); | |
608 | die "failed to get numerical host address: $err\n" if $err; | |
609 | return ($host, $port) if wantarray; | |
610 | return $host; | |
611 | } | |
612 | ||
613 | sub get_ip_from_hostname { | |
614 | my ($hostname, $noerr) = @_; | |
615 | ||
616 | my ($family, $ip); | |
617 | ||
618 | eval { | |
619 | my @res = PVE::Tools::getaddrinfo_all($hostname); | |
620 | $family = $res[0]->{family}; | |
621 | $ip = addr_to_ip($res[0]->{addr}) | |
622 | }; | |
623 | if ($@) { | |
4ed6974a | 624 | die "hostname lookup '$hostname' failed - $@" if !$noerr; |
87aa00de TL |
625 | return undef; |
626 | } | |
627 | ||
628 | if ($ip =~ m/^127\.|^::1$/) { | |
4ed6974a | 629 | die "hostname lookup '$hostname' failed - got local IP address '$ip'\n" if !$noerr; |
87aa00de TL |
630 | return undef; |
631 | } | |
632 | ||
633 | return wantarray ? ($ip, $family) : $ip; | |
634 | } | |
635 | ||
a712bf6e WB |
636 | sub lock_network { |
637 | my ($code, @param) = @_; | |
638 | my $res = lock_file('/var/lock/pve-network.lck', 10, $code, @param); | |
639 | die $@ if $@; | |
640 | return $res; | |
641 | } | |
642 | ||
b9436cda | 643 | 1; |