2016-01-21 13:08:56 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2013-2016 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2014-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
2016-01-21 13:08:56 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2016-01-21 13:08:56 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2016-01-21 13:08:56 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2016-01-21 13:08:56 +01:00
|
|
|
#
|
|
|
|
|
2016-06-16 16:39:48 +02:00
|
|
|
''' This module contains the NetVMMixin '''
|
2017-12-06 15:23:47 +01:00
|
|
|
import ipaddress
|
2016-09-09 03:14:16 +02:00
|
|
|
import os
|
2016-06-16 16:39:48 +02:00
|
|
|
import re
|
2016-06-02 22:02:06 +02:00
|
|
|
|
2016-06-16 16:39:48 +02:00
|
|
|
import libvirt # pylint: disable=import-error
|
2016-01-21 13:08:56 +01:00
|
|
|
import qubes
|
2017-12-01 03:24:34 +01:00
|
|
|
import qubes.config
|
2016-04-20 13:41:33 +02:00
|
|
|
import qubes.events
|
2016-09-09 03:14:16 +02:00
|
|
|
import qubes.firewall
|
2016-01-29 17:56:33 +01:00
|
|
|
import qubes.exc
|
2016-01-21 13:08:56 +01:00
|
|
|
|
2016-05-21 03:51:21 +02:00
|
|
|
def _setter_mac(self, prop, value):
|
2016-06-16 16:39:48 +02:00
|
|
|
''' Helper for setting the MAC address '''
|
2016-06-02 22:02:06 +02:00
|
|
|
# pylint: disable=unused-argument
|
2017-01-18 22:16:46 +01:00
|
|
|
if not isinstance(value, str):
|
2016-05-21 03:51:21 +02:00
|
|
|
raise ValueError('MAC address must be a string')
|
|
|
|
value = value.lower()
|
|
|
|
if re.match(r"^([0-9a-f][0-9a-f]:){5}[0-9a-f][0-9a-f]$", value) is None:
|
|
|
|
raise ValueError('Invalid MAC address value')
|
|
|
|
return value
|
|
|
|
|
2016-06-16 16:39:48 +02:00
|
|
|
|
2016-10-31 03:04:13 +01:00
|
|
|
def _default_ip(self):
|
|
|
|
if not self.is_networked():
|
|
|
|
return None
|
|
|
|
if self.netvm is not None:
|
|
|
|
return self.netvm.get_ip_for_vm(self) # pylint: disable=no-member
|
2017-04-15 20:04:38 +02:00
|
|
|
|
|
|
|
return self.get_ip_for_vm(self)
|
2016-10-31 03:04:13 +01:00
|
|
|
|
|
|
|
|
2017-12-01 03:24:34 +01:00
|
|
|
def _default_ip6(self):
|
|
|
|
if not self.is_networked():
|
|
|
|
return None
|
|
|
|
if not self.features.check_with_netvm('ipv6', False):
|
|
|
|
return None
|
|
|
|
if self.netvm is not None:
|
|
|
|
return self.netvm.get_ip6_for_vm(self) # pylint: disable=no-member
|
|
|
|
|
|
|
|
return self.get_ip6_for_vm(self)
|
|
|
|
|
|
|
|
|
2017-05-12 14:21:10 +02:00
|
|
|
def _setter_netvm(self, prop, value):
|
2017-05-12 17:41:38 +02:00
|
|
|
# pylint: disable=unused-argument
|
2017-05-12 14:21:10 +02:00
|
|
|
if value is None:
|
2017-12-21 18:19:10 +01:00
|
|
|
return None
|
2017-05-12 14:21:10 +02:00
|
|
|
if not value.provides_network:
|
|
|
|
raise qubes.exc.QubesValueError(
|
|
|
|
'The {!s} qube does not provide network'.format(value))
|
|
|
|
|
2017-12-01 02:59:17 +01:00
|
|
|
# skip check for netvm loops during qubes.xml loading, to avoid tricky
|
|
|
|
# loading order
|
|
|
|
if self.events_enabled:
|
|
|
|
if value is self \
|
|
|
|
or value in self.app.domains.get_vms_connected_to(self):
|
|
|
|
raise qubes.exc.QubesValueError(
|
|
|
|
'Loops in network are unsupported')
|
2017-05-12 14:21:10 +02:00
|
|
|
return value
|
|
|
|
|
2018-12-06 20:28:35 +01:00
|
|
|
def _setter_provides_network(self, prop, value):
|
|
|
|
value = qubes.property.bool(self, prop, value)
|
|
|
|
if not value:
|
|
|
|
if list(self.connected_vms):
|
|
|
|
raise qubes.exc.QubesValueError(
|
|
|
|
'The qube is still used by other qubes, change theirs '
|
|
|
|
'\'netvm\' first')
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
2017-05-12 14:21:10 +02:00
|
|
|
|
2016-04-20 13:41:33 +02:00
|
|
|
class NetVMMixin(qubes.events.Emitter):
|
2016-06-16 16:39:48 +02:00
|
|
|
''' Mixin containing network functionality '''
|
2016-01-21 13:08:56 +01:00
|
|
|
mac = qubes.property('mac', type=str,
|
2019-05-18 12:09:11 +02:00
|
|
|
default='00:16:3e:5e:6c:00',
|
2016-05-21 03:51:21 +02:00
|
|
|
setter=_setter_mac,
|
2016-01-21 13:08:56 +01:00
|
|
|
doc='MAC address of the NIC emulated inside VM')
|
|
|
|
|
2017-12-06 15:23:47 +01:00
|
|
|
ip = qubes.property('ip', type=ipaddress.IPv4Address,
|
2016-10-31 03:04:13 +01:00
|
|
|
default=_default_ip,
|
|
|
|
doc='IP address of this domain.')
|
|
|
|
|
2017-12-06 15:23:47 +01:00
|
|
|
ip6 = qubes.property('ip6', type=ipaddress.IPv6Address,
|
2017-12-01 03:24:34 +01:00
|
|
|
default=_default_ip6,
|
|
|
|
doc='IPv6 address of this domain.')
|
|
|
|
|
2016-06-16 16:39:48 +02:00
|
|
|
# CORE2: swallowed uses_default_netvm
|
2016-01-21 13:08:56 +01:00
|
|
|
netvm = qubes.VMProperty('netvm', load_stage=4, allow_none=True,
|
2017-12-01 03:05:57 +01:00
|
|
|
default=(lambda self: self.app.default_netvm),
|
2017-05-12 14:21:10 +02:00
|
|
|
setter=_setter_netvm,
|
2016-01-21 13:08:56 +01:00
|
|
|
doc='''VM that provides network connection to this domain. When
|
|
|
|
`None`, machine is disconnected. When absent, domain uses default
|
|
|
|
NetVM.''')
|
|
|
|
|
|
|
|
provides_network = qubes.property('provides_network', default=False,
|
2018-12-06 20:28:35 +01:00
|
|
|
type=bool, setter=_setter_provides_network,
|
2016-01-21 13:08:56 +01:00
|
|
|
doc='''If this domain can act as network provider (formerly known as
|
|
|
|
NetVM or ProxyVM)''')
|
|
|
|
|
2017-03-12 01:50:09 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def firewall_conf(self):
|
|
|
|
return 'firewall.xml'
|
2016-09-09 03:14:16 +02:00
|
|
|
|
2016-01-21 13:08:56 +01:00
|
|
|
#
|
|
|
|
# used in networked appvms or proxyvms (netvm is not None)
|
|
|
|
#
|
|
|
|
|
|
|
|
|
2017-04-10 04:02:13 +02:00
|
|
|
@qubes.stateless_property
|
2016-10-31 02:06:01 +01:00
|
|
|
def visible_ip(self):
|
|
|
|
'''IP address of this domain as seen by the domain.'''
|
2017-07-18 03:50:02 +02:00
|
|
|
return self.features.check_with_template('net.fake-ip', None) or \
|
2016-10-31 02:06:01 +01:00
|
|
|
self.ip
|
|
|
|
|
2017-12-01 03:24:34 +01:00
|
|
|
@qubes.stateless_property
|
|
|
|
def visible_ip6(self):
|
|
|
|
'''IPv6 address of this domain as seen by the domain.'''
|
|
|
|
return self.ip6
|
|
|
|
|
2017-04-10 04:02:13 +02:00
|
|
|
@qubes.stateless_property
|
2016-10-31 02:06:01 +01:00
|
|
|
def visible_gateway(self):
|
|
|
|
'''Default gateway of this domain as seen by the domain.'''
|
2017-07-18 03:50:02 +02:00
|
|
|
return self.features.check_with_template('net.fake-gateway', None) or \
|
2017-11-09 18:42:44 +01:00
|
|
|
(self.netvm.gateway if self.netvm else None)
|
2016-10-31 02:06:01 +01:00
|
|
|
|
2017-12-01 03:24:34 +01:00
|
|
|
@qubes.stateless_property
|
|
|
|
def visible_gateway6(self):
|
|
|
|
'''Default (IPv6) gateway of this domain as seen by the domain.'''
|
|
|
|
if self.features.check_with_netvm('ipv6', False):
|
|
|
|
return self.netvm.gateway6 if self.netvm else None
|
|
|
|
return None
|
|
|
|
|
2017-04-10 04:02:13 +02:00
|
|
|
@qubes.stateless_property
|
2016-10-31 02:06:01 +01:00
|
|
|
def visible_netmask(self):
|
|
|
|
'''Netmask as seen by the domain.'''
|
2017-07-18 03:50:02 +02:00
|
|
|
return self.features.check_with_template('net.fake-netmask', None) or \
|
2017-11-09 18:42:44 +01:00
|
|
|
(self.netvm.netmask if self.netvm else None)
|
2016-01-21 13:08:56 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# used in netvms (provides_network=True)
|
|
|
|
# those properties and methods are most likely accessed as vm.netvm.<prop>
|
|
|
|
#
|
|
|
|
|
2016-01-29 17:56:33 +01:00
|
|
|
@staticmethod
|
|
|
|
def get_ip_for_vm(vm):
|
2016-01-21 13:08:56 +01:00
|
|
|
'''Get IP address for (appvm) domain connected to this (netvm) domain.
|
|
|
|
'''
|
2016-06-16 16:39:48 +02:00
|
|
|
import qubes.vm.dispvm # pylint: disable=redefined-outer-name
|
2016-01-21 13:08:56 +01:00
|
|
|
if isinstance(vm, qubes.vm.dispvm.DispVM):
|
2017-12-06 15:23:47 +01:00
|
|
|
return ipaddress.IPv4Address('10.138.{}.{}'.format(
|
|
|
|
(vm.dispid >> 8) & 0xff, vm.dispid & 0xff))
|
2016-01-21 13:08:56 +01:00
|
|
|
|
|
|
|
# VM technically can get address which ends in '.0'. This currently
|
|
|
|
# does not happen, because qid < 253, but may happen in the future.
|
2017-12-06 15:23:47 +01:00
|
|
|
return ipaddress.IPv4Address('10.137.{}.{}'.format(
|
|
|
|
(vm.qid >> 8) & 0xff, vm.qid & 0xff))
|
2016-01-21 13:08:56 +01:00
|
|
|
|
2017-12-01 03:24:34 +01:00
|
|
|
@staticmethod
|
|
|
|
def get_ip6_for_vm(vm):
|
|
|
|
'''Get IPv6 address for (appvm) domain connected to this (netvm) domain.
|
|
|
|
|
|
|
|
Default address is constructed with Qubes-specific site-local prefix,
|
|
|
|
and IPv4 suffix (0xa89 is 10.137.).
|
|
|
|
'''
|
|
|
|
import qubes.vm.dispvm # pylint: disable=redefined-outer-name
|
|
|
|
if isinstance(vm, qubes.vm.dispvm.DispVM):
|
2017-12-06 15:23:47 +01:00
|
|
|
return ipaddress.IPv6Address('{}::a8a:{:x}'.format(
|
|
|
|
qubes.config.qubes_ipv6_prefix, vm.dispid))
|
2017-12-01 03:24:34 +01:00
|
|
|
|
2017-12-06 15:23:47 +01:00
|
|
|
return ipaddress.IPv6Address('{}::a89:{:x}'.format(
|
|
|
|
qubes.config.qubes_ipv6_prefix, vm.qid))
|
2017-12-01 03:24:34 +01:00
|
|
|
|
2017-04-10 04:02:13 +02:00
|
|
|
@qubes.stateless_property
|
2016-01-21 13:08:56 +01:00
|
|
|
def gateway(self):
|
|
|
|
'''Gateway for other domains that use this domain as netvm.'''
|
2016-11-01 00:30:11 +01:00
|
|
|
return self.visible_ip if self.provides_network else None
|
2016-01-21 13:08:56 +01:00
|
|
|
|
2017-12-01 03:24:34 +01:00
|
|
|
@qubes.stateless_property
|
|
|
|
def gateway6(self):
|
|
|
|
'''Gateway (IPv6) for other domains that use this domain as netvm.'''
|
|
|
|
if self.features.check_with_netvm('ipv6', False):
|
2018-04-02 23:59:22 +02:00
|
|
|
return self.visible_ip6 if self.provides_network else \
|
2017-12-01 03:24:34 +01:00
|
|
|
None
|
|
|
|
return None
|
|
|
|
|
2016-01-21 13:08:56 +01:00
|
|
|
@property
|
|
|
|
def netmask(self):
|
|
|
|
'''Netmask for gateway address.'''
|
|
|
|
return '255.255.255.255' if self.is_networked() else None
|
|
|
|
|
2016-03-07 01:10:39 +01:00
|
|
|
@property
|
|
|
|
def connected_vms(self):
|
2016-06-16 16:39:48 +02:00
|
|
|
''' Return a generator containing all domains connected to the current
|
|
|
|
NetVM.
|
|
|
|
'''
|
2016-03-07 01:10:39 +01:00
|
|
|
for vm in self.app.domains:
|
2017-04-01 01:25:57 +02:00
|
|
|
if getattr(vm, 'netvm', None) is self:
|
2016-03-07 01:10:39 +01:00
|
|
|
yield vm
|
2016-01-21 13:08:56 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# used in both
|
|
|
|
#
|
|
|
|
|
|
|
|
@property
|
|
|
|
def dns(self):
|
|
|
|
'''Secondary DNS server set up for this domain.'''
|
|
|
|
if self.netvm is not None or self.provides_network:
|
|
|
|
return (
|
|
|
|
'10.139.1.1',
|
|
|
|
'10.139.1.2',
|
|
|
|
)
|
2017-04-15 20:04:38 +02:00
|
|
|
|
|
|
|
return None
|
2016-01-21 13:08:56 +01:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2016-09-09 03:14:16 +02:00
|
|
|
self._firewall = None
|
2016-01-21 13:08:56 +01:00
|
|
|
super(NetVMMixin, self).__init__(*args, **kwargs)
|
|
|
|
|
2017-12-01 02:59:17 +01:00
|
|
|
@qubes.events.handler('domain-load')
|
|
|
|
def on_domain_load_netvm_loop_check(self, event):
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
# make sure there are no netvm loops - which could cause qubesd
|
|
|
|
# looping infinitely
|
|
|
|
if self is self.netvm:
|
|
|
|
self.log.error(
|
|
|
|
'vm \'%s\' network-connected to itself, breaking the '
|
|
|
|
'connection', self.name)
|
|
|
|
self.netvm = None
|
|
|
|
elif self.netvm in self.app.domains.get_vms_connected_to(self):
|
|
|
|
self.log.error(
|
|
|
|
'netvm loop detected on \'%s\', breaking the connection',
|
|
|
|
self.name)
|
|
|
|
self.netvm = None
|
|
|
|
|
vm/mixin/net: disconnect network interface on backend shutdown/crash
Since we have more reliable domain-shutdown event delivery (it si
guaranteed to be delivered before subsequent domain start, even if
libvirt fails to report it), it's better to move detach_network call to
domain-shutdown handler. This way, frontend domain will see immediately
that the backend is gone. Technically it already know that, but at least
Linux do not propagate that anywhere, keeping the interface up,
seemingly operational, leading to various timeouts.
Additionally, by avoiding attach_network call _just_ after
detach_network call, it avoids various race conditions (like calling
cleanup scripts after new device got already connected).
While libvirt itself still doesn't cleanup devices when the backend
domain is gone, this will emulate it within qubesd.
Fixes QubesOS/qubes-issues#3642
Fixes QubesOS/qubes-issues#1426
2019-02-24 00:41:43 +01:00
|
|
|
@qubes.events.handler('domain-shutdown')
|
|
|
|
def on_domain_shutdown(self, event, **kwargs):
|
|
|
|
'''Cleanup network interfaces of connected, running VMs.
|
|
|
|
|
|
|
|
This will allow re-reconnecting them cleanly later.
|
|
|
|
'''
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
for vm in self.connected_vms:
|
|
|
|
if not vm.is_running():
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
vm.detach_network()
|
|
|
|
except (qubes.exc.QubesException, libvirt.libvirtError):
|
|
|
|
# ignore errors
|
|
|
|
pass
|
|
|
|
|
2016-03-14 22:16:52 +01:00
|
|
|
@qubes.events.handler('domain-start')
|
|
|
|
def on_domain_started(self, event, **kwargs):
|
|
|
|
'''Connect this domain to its downstream domains. Also reload firewall
|
|
|
|
in its netvm.
|
2016-01-21 13:08:56 +01:00
|
|
|
|
|
|
|
This is needed when starting netvm *after* its connected domains.
|
2016-06-16 16:39:48 +02:00
|
|
|
''' # pylint: disable=unused-argument
|
2016-01-21 13:08:56 +01:00
|
|
|
|
2016-03-14 22:16:52 +01:00
|
|
|
if self.netvm:
|
2016-06-16 16:39:48 +02:00
|
|
|
self.netvm.reload_firewall_for_vm(self) # pylint: disable=no-member
|
2016-03-14 22:16:52 +01:00
|
|
|
|
2016-01-21 13:08:56 +01:00
|
|
|
for vm in self.connected_vms:
|
|
|
|
if not vm.is_running():
|
|
|
|
continue
|
|
|
|
vm.log.info('Attaching network')
|
|
|
|
try:
|
2016-08-12 21:08:00 +02:00
|
|
|
vm.attach_network()
|
2018-01-29 23:06:21 +01:00
|
|
|
except (qubes.exc.QubesException, libvirt.libvirtError):
|
2016-01-21 13:08:56 +01:00
|
|
|
vm.log.warning('Cannot attach network', exc_info=1)
|
|
|
|
|
2016-03-14 22:16:52 +01:00
|
|
|
@qubes.events.handler('domain-pre-shutdown')
|
vm/mixin/net: disconnect network interface on backend shutdown/crash
Since we have more reliable domain-shutdown event delivery (it si
guaranteed to be delivered before subsequent domain start, even if
libvirt fails to report it), it's better to move detach_network call to
domain-shutdown handler. This way, frontend domain will see immediately
that the backend is gone. Technically it already know that, but at least
Linux do not propagate that anywhere, keeping the interface up,
seemingly operational, leading to various timeouts.
Additionally, by avoiding attach_network call _just_ after
detach_network call, it avoids various race conditions (like calling
cleanup scripts after new device got already connected).
While libvirt itself still doesn't cleanup devices when the backend
domain is gone, this will emulate it within qubesd.
Fixes QubesOS/qubes-issues#3642
Fixes QubesOS/qubes-issues#1426
2019-02-24 00:41:43 +01:00
|
|
|
def on_domain_pre_shutdown(self, event, force=False):
|
2016-06-16 16:39:48 +02:00
|
|
|
''' Checks before NetVM shutdown if any connected domains are running.
|
|
|
|
If `force` is `True` tries to detach network interfaces of connected
|
|
|
|
vms
|
|
|
|
''' # pylint: disable=unused-argument
|
2016-06-02 22:02:06 +02:00
|
|
|
|
2016-01-29 17:56:33 +01:00
|
|
|
connected_vms = [vm for vm in self.connected_vms if vm.is_running()]
|
2016-01-21 13:08:56 +01:00
|
|
|
if connected_vms and not force:
|
2016-06-02 22:02:06 +02:00
|
|
|
raise qubes.exc.QubesVMError(self,
|
2016-01-21 13:08:56 +01:00
|
|
|
'There are other VMs connected to this VM: {}'.format(
|
|
|
|
', '.join(vm.name for vm in connected_vms)))
|
|
|
|
|
|
|
|
|
|
|
|
def attach_network(self):
|
|
|
|
'''Attach network in this machine to it's netvm.'''
|
|
|
|
|
|
|
|
if not self.is_running():
|
|
|
|
raise qubes.exc.QubesVMNotRunningError(self)
|
2018-06-11 12:32:05 +02:00
|
|
|
if self.netvm is None:
|
|
|
|
raise qubes.exc.QubesVMError(self,
|
|
|
|
'netvm should not be {}'.format(self.netvm))
|
2016-01-21 13:08:56 +01:00
|
|
|
|
2016-06-16 16:39:48 +02:00
|
|
|
if not self.netvm.is_running(): # pylint: disable=no-member
|
|
|
|
# pylint: disable=no-member
|
2016-01-21 13:08:56 +01:00
|
|
|
self.log.info('Starting NetVM ({0})'.format(self.netvm.name))
|
|
|
|
self.netvm.start()
|
|
|
|
|
2016-11-01 00:37:43 +01:00
|
|
|
self.netvm.set_mapped_ip_info_for_vm(self)
|
2016-05-20 02:56:05 +02:00
|
|
|
self.libvirt_domain.attachDevice(
|
2016-06-15 19:41:11 +02:00
|
|
|
self.app.env.get_template('libvirt/devices/net.xml').render(
|
|
|
|
vm=self))
|
2016-01-21 13:08:56 +01:00
|
|
|
|
|
|
|
def detach_network(self):
|
|
|
|
'''Detach machine from it's netvm'''
|
|
|
|
|
|
|
|
if not self.is_running():
|
|
|
|
raise qubes.exc.QubesVMNotRunningError(self)
|
2018-06-11 12:32:05 +02:00
|
|
|
if self.netvm is None:
|
|
|
|
raise qubes.exc.QubesVMError(self,
|
|
|
|
'netvm should not be {}'.format(self.netvm))
|
2016-01-21 13:08:56 +01:00
|
|
|
|
2016-09-06 13:36:07 +02:00
|
|
|
self.libvirt_domain.detachDevice(
|
2016-06-15 19:41:11 +02:00
|
|
|
self.app.env.get_template('libvirt/devices/net.xml').render(
|
|
|
|
vm=self))
|
2016-01-21 13:08:56 +01:00
|
|
|
|
|
|
|
def is_networked(self):
|
|
|
|
'''Check whether this VM can reach network (firewall notwithstanding).
|
|
|
|
|
|
|
|
:returns: :py:obj:`True` if is machine can reach network, \
|
|
|
|
:py:obj:`False` otherwise.
|
|
|
|
:rtype: bool
|
|
|
|
'''
|
|
|
|
|
|
|
|
if self.provides_network:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return self.netvm is not None
|
|
|
|
|
2016-03-07 01:22:27 +01:00
|
|
|
def reload_firewall_for_vm(self, vm):
|
2016-06-16 16:39:48 +02:00
|
|
|
''' Reload the firewall rules for the vm '''
|
2016-09-09 03:14:16 +02:00
|
|
|
if not self.is_running():
|
|
|
|
return
|
|
|
|
|
2017-12-01 03:24:34 +01:00
|
|
|
for addr_family in (4, 6):
|
|
|
|
ip = vm.ip6 if addr_family == 6 else vm.ip
|
|
|
|
if ip is None:
|
|
|
|
continue
|
2017-12-14 02:07:57 +01:00
|
|
|
base_dir = '/qubes-firewall/{}/'.format(ip)
|
2017-12-01 03:24:34 +01:00
|
|
|
# remove old entries if any (but don't touch base empty entry - it
|
|
|
|
# would trigger reload right away
|
|
|
|
self.untrusted_qdb.rm(base_dir)
|
|
|
|
# write new rules
|
|
|
|
for key, value in vm.firewall.qdb_entries(
|
|
|
|
addr_family=addr_family).items():
|
|
|
|
self.untrusted_qdb.write(base_dir + key, value)
|
|
|
|
# signal its done
|
|
|
|
self.untrusted_qdb.write(base_dir[:-1], '')
|
2016-01-21 13:08:56 +01:00
|
|
|
|
2016-11-01 00:37:43 +01:00
|
|
|
def set_mapped_ip_info_for_vm(self, vm):
|
|
|
|
'''
|
|
|
|
Set configuration to possibly hide real IP from the VM.
|
|
|
|
This needs to be done before executing 'script'
|
|
|
|
(`/etc/xen/scripts/vif-route-qubes`) in network providing VM
|
|
|
|
'''
|
2016-10-31 02:06:01 +01:00
|
|
|
# add info about remapped IPs (VM IP hidden from the VM itself)
|
|
|
|
mapped_ip_base = '/mapped-ip/{}'.format(vm.ip)
|
|
|
|
if vm.visible_ip:
|
2017-07-21 23:11:24 +02:00
|
|
|
self.untrusted_qdb.write(mapped_ip_base + '/visible-ip',
|
2017-12-14 02:07:57 +01:00
|
|
|
str(vm.visible_ip))
|
2016-10-31 02:06:01 +01:00
|
|
|
else:
|
2017-07-21 23:11:24 +02:00
|
|
|
self.untrusted_qdb.rm(mapped_ip_base + '/visible-ip')
|
2016-10-31 02:06:01 +01:00
|
|
|
if vm.visible_gateway:
|
2017-07-21 23:11:24 +02:00
|
|
|
self.untrusted_qdb.write(mapped_ip_base + '/visible-gateway',
|
2017-12-14 02:07:57 +01:00
|
|
|
str(vm.visible_gateway))
|
2016-10-31 02:06:01 +01:00
|
|
|
else:
|
2017-07-21 23:11:24 +02:00
|
|
|
self.untrusted_qdb.rm(mapped_ip_base + '/visible-gateway')
|
2016-10-31 02:06:01 +01:00
|
|
|
|
2020-01-09 11:59:53 +01:00
|
|
|
def reload_connected_ips(self):
|
|
|
|
'''
|
|
|
|
Update list of IPs possibly connected to this machine.
|
|
|
|
This is used by qubes-firewall to implement anti-spoofing.
|
|
|
|
'''
|
2020-01-09 16:18:07 +01:00
|
|
|
connected_ips = [str(vm.visible_ip) for vm in self.connected_vms
|
|
|
|
if vm.visible_ip is not None]
|
|
|
|
connected_ips6 = [str(vm.visible_ip6) for vm in self.connected_vms
|
|
|
|
if vm.visible_ip6 is not None]
|
|
|
|
|
2020-01-09 11:59:53 +01:00
|
|
|
self.untrusted_qdb.write(
|
|
|
|
'/connected-ips',
|
|
|
|
' '.join(connected_ips))
|
2020-01-09 16:18:07 +01:00
|
|
|
self.untrusted_qdb.write(
|
|
|
|
'/connected-ips6',
|
|
|
|
' '.join(connected_ips6))
|
2020-01-09 11:59:53 +01:00
|
|
|
|
2017-10-20 02:05:49 +02:00
|
|
|
@qubes.events.handler('property-pre-del:netvm')
|
|
|
|
def on_property_pre_del_netvm(self, event, name, oldvalue=None):
|
|
|
|
''' Sets the the NetVM to default NetVM '''
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
# we are changing to default netvm
|
|
|
|
newvalue = type(self).netvm.get_default(self)
|
2017-12-01 02:59:17 +01:00
|
|
|
# check for netvm loop
|
|
|
|
_setter_netvm(self, type(self).netvm, newvalue)
|
2017-10-20 02:05:49 +02:00
|
|
|
if newvalue == oldvalue:
|
|
|
|
return
|
|
|
|
self.fire_event('property-pre-set:netvm', pre_event=True,
|
|
|
|
name='netvm', newvalue=newvalue, oldvalue=oldvalue)
|
|
|
|
|
2016-01-21 13:08:56 +01:00
|
|
|
@qubes.events.handler('property-del:netvm')
|
2017-03-15 18:36:34 +01:00
|
|
|
def on_property_del_netvm(self, event, name, oldvalue=None):
|
2016-06-16 16:39:48 +02:00
|
|
|
''' Sets the the NetVM to default NetVM '''
|
2016-01-21 13:08:56 +01:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
# we are changing to default netvm
|
2017-03-06 17:20:57 +01:00
|
|
|
newvalue = self.netvm
|
|
|
|
if newvalue == oldvalue:
|
2016-01-21 13:08:56 +01:00
|
|
|
return
|
2017-02-21 14:09:06 +01:00
|
|
|
self.fire_event('property-set:netvm',
|
2017-03-06 17:20:57 +01:00
|
|
|
name='netvm', newvalue=newvalue, oldvalue=oldvalue)
|
2016-01-21 13:08:56 +01:00
|
|
|
|
2016-04-20 13:41:33 +02:00
|
|
|
@qubes.events.handler('property-pre-set:netvm')
|
2017-03-06 17:20:57 +01:00
|
|
|
def on_property_pre_set_netvm(self, event, name, newvalue, oldvalue=None):
|
2016-06-16 16:39:48 +02:00
|
|
|
''' Run sanity checks before setting a new NetVM '''
|
2016-06-02 22:02:06 +02:00
|
|
|
# pylint: disable=unused-argument
|
2017-03-06 17:20:57 +01:00
|
|
|
if newvalue is not None:
|
2016-09-06 13:36:07 +02:00
|
|
|
if not self.app.vmm.offline_mode \
|
2017-03-06 17:20:57 +01:00
|
|
|
and self.is_running() and not newvalue.is_running():
|
|
|
|
raise qubes.exc.QubesVMNotStartedError(newvalue,
|
2016-09-06 13:36:07 +02:00
|
|
|
'Cannot dynamically attach to stopped NetVM: {!r}'.format(
|
2017-03-06 17:20:57 +01:00
|
|
|
newvalue))
|
2016-09-06 13:36:07 +02:00
|
|
|
|
2018-02-26 02:52:27 +01:00
|
|
|
# don't check oldvalue, because it's missing if it was default
|
|
|
|
if self.netvm is not None:
|
2016-09-06 13:36:07 +02:00
|
|
|
if self.is_running():
|
|
|
|
self.detach_network()
|
2016-01-21 13:08:56 +01:00
|
|
|
|
2016-04-20 13:41:33 +02:00
|
|
|
@qubes.events.handler('property-set:netvm')
|
2017-03-06 17:20:57 +01:00
|
|
|
def on_property_set_netvm(self, event, name, newvalue, oldvalue=None):
|
2016-06-16 16:39:48 +02:00
|
|
|
''' Replaces the current NetVM with a new one and fires
|
|
|
|
net-domain-connect event
|
|
|
|
'''
|
2016-04-20 13:41:33 +02:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
|
2020-01-09 12:37:43 +01:00
|
|
|
if oldvalue is not None and oldvalue.is_running():
|
2020-01-09 11:59:53 +01:00
|
|
|
oldvalue.reload_connected_ips()
|
|
|
|
|
2017-03-06 17:20:57 +01:00
|
|
|
if newvalue is None:
|
2016-01-21 13:08:56 +01:00
|
|
|
return
|
|
|
|
|
2020-01-09 12:37:43 +01:00
|
|
|
if newvalue.is_running():
|
|
|
|
newvalue.reload_connected_ips()
|
2020-01-09 11:59:53 +01:00
|
|
|
|
2016-01-21 13:08:56 +01:00
|
|
|
if self.is_running():
|
|
|
|
# refresh IP, DNS etc
|
|
|
|
self.create_qdb_entries()
|
|
|
|
self.attach_network()
|
|
|
|
|
2017-03-06 17:20:57 +01:00
|
|
|
newvalue.fire_event('net-domain-connect', vm=self)
|
2016-06-16 16:39:48 +02:00
|
|
|
|
|
|
|
@qubes.events.handler('net-domain-connect')
|
|
|
|
def on_net_domain_connect(self, event, vm):
|
|
|
|
''' Reloads the firewall config for vm '''
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
self.reload_firewall_for_vm(vm)
|
2016-03-07 01:22:27 +01:00
|
|
|
|
2016-03-14 22:16:52 +01:00
|
|
|
@qubes.events.handler('domain-qdb-create')
|
|
|
|
def on_domain_qdb_create(self, event):
|
2017-12-14 02:10:11 +01:00
|
|
|
''' Fills the QubesDB with firewall entries. '''
|
|
|
|
# pylint: disable=unused-argument
|
2020-01-09 11:59:53 +01:00
|
|
|
self.reload_connected_ips()
|
2017-12-14 02:10:11 +01:00
|
|
|
for vm in self.connected_vms:
|
|
|
|
if vm.is_running():
|
|
|
|
# keep in sync with on_firewall_changed
|
|
|
|
self.set_mapped_ip_info_for_vm(vm)
|
|
|
|
self.reload_firewall_for_vm(vm)
|
2016-03-07 01:22:27 +01:00
|
|
|
|
2016-06-16 16:39:48 +02:00
|
|
|
@qubes.events.handler('firewall-changed', 'domain-spawn')
|
2016-06-21 17:03:04 +02:00
|
|
|
def on_firewall_changed(self, event, **kwargs):
|
2016-06-16 16:39:48 +02:00
|
|
|
''' Reloads the firewall if vm is running and has a NetVM assigned '''
|
2016-06-02 22:02:06 +02:00
|
|
|
# pylint: disable=unused-argument
|
2016-03-07 01:22:27 +01:00
|
|
|
if self.is_running() and self.netvm:
|
2016-11-01 00:37:43 +01:00
|
|
|
self.netvm.set_mapped_ip_info_for_vm(self)
|
2016-06-16 16:39:48 +02:00
|
|
|
self.netvm.reload_firewall_for_vm(self) # pylint: disable=no-member
|
2016-09-09 03:14:16 +02:00
|
|
|
|
|
|
|
# CORE2: swallowed get_firewall_conf, write_firewall_conf,
|
|
|
|
# get_firewall_defaults
|
|
|
|
@property
|
|
|
|
def firewall(self):
|
|
|
|
if self._firewall is None:
|
|
|
|
self._firewall = qubes.firewall.Firewall(self)
|
|
|
|
return self._firewall
|
|
|
|
|
|
|
|
def has_firewall(self):
|
|
|
|
''' Return `True` if there are some vm specific firewall rules set '''
|
|
|
|
return os.path.exists(os.path.join(self.dir_path, self.firewall_conf))
|