005QubesNetVm.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/python2
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2013 Marek Marczykowski <marmarek@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. #
  23. #
  24. import sys
  25. import os.path
  26. import xen.lowlevel.xs
  27. from qubes.qubes import QubesVm,register_qubes_vm_class,vmm,dry_run
  28. from qubes.qubes import defaults,system_path,vm_files
  29. from qubes.qubes import QubesVmCollection,QubesException
  30. class QubesNetVm(QubesVm):
  31. """
  32. A class that represents a NetVM. A child of QubesCowVM.
  33. """
  34. # In which order load this VM type from qubes.xml
  35. load_order = 70
  36. def get_attrs_config(self):
  37. attrs_config = super(QubesNetVm, self).get_attrs_config()
  38. attrs_config['dir_path']['func'] = \
  39. lambda value: value if value is not None else \
  40. os.path.join(system_path["qubes_servicevms_dir"], self.name)
  41. attrs_config['label']['default'] = defaults["servicevm_label"]
  42. attrs_config['memory']['default'] = 300
  43. # New attributes
  44. attrs_config['netid'] = {
  45. 'save': lambda: str(self.netid),
  46. 'order': 30,
  47. 'func': lambda value: value if value is not None else
  48. self._collection.get_new_unused_netid() }
  49. attrs_config['netprefix'] = {
  50. 'func': lambda x: "10.137.{0}.".format(self.netid) }
  51. attrs_config['dispnetprefix'] = {
  52. 'func': lambda x: "10.138.{0}.".format(self.netid) }
  53. # Dont save netvm prop
  54. attrs_config['netvm'].pop('save')
  55. attrs_config['uses_default_netvm'].pop('save')
  56. return attrs_config
  57. def __init__(self, **kwargs):
  58. super(QubesNetVm, self).__init__(**kwargs)
  59. self.connected_vms = QubesVmCollection()
  60. self.__network = "10.137.{0}.0".format(self.netid)
  61. self.__netmask = defaults["vm_default_netmask"]
  62. self.__gateway = self.netprefix + "1"
  63. self.__secondary_dns = self.netprefix + "254"
  64. self.__external_ip_allowed_xids = set()
  65. @property
  66. def type(self):
  67. return "NetVM"
  68. def is_netvm(self):
  69. return True
  70. @property
  71. def gateway(self):
  72. return self.__gateway
  73. @property
  74. def secondary_dns(self):
  75. return self.__secondary_dns
  76. @property
  77. def netmask(self):
  78. return self.__netmask
  79. @property
  80. def network(self):
  81. return self.__network
  82. def get_ip_for_vm(self, qid):
  83. lo = qid % 253 + 2
  84. assert lo >= 2 and lo <= 254, "Wrong IP address for VM"
  85. return self.netprefix + "{0}".format(lo)
  86. def get_ip_for_dispvm(self, dispid):
  87. lo = dispid % 254 + 1
  88. assert lo >= 1 and lo <= 254, "Wrong IP address for VM"
  89. return self.dispnetprefix + "{0}".format(lo)
  90. def update_external_ip_permissions(self, xid = -1):
  91. # TODO: VMs in __external_ip_allowed_xids should be notified via RPC
  92. # service on exteran IP change
  93. pass
  94. def start(self, **kwargs):
  95. if dry_run:
  96. return
  97. xid=super(QubesNetVm, self).start(**kwargs)
  98. # Connect vif's of already running VMs
  99. for vm in self.connected_vms.values():
  100. if not vm.is_running():
  101. continue
  102. if 'verbose' in kwargs and kwargs['verbose']:
  103. print >> sys.stderr, "--> Attaching network to '{0}'...".format(vm.name)
  104. # Cleanup stale VIFs
  105. vm.cleanup_vifs()
  106. # force frontend to forget about this device
  107. # module actually will be loaded back by udev, as soon as network is attached
  108. try:
  109. vm.run("modprobe -r xen-netfront xennet", user="root")
  110. except:
  111. pass
  112. try:
  113. vm.attach_network(wait=False)
  114. except QubesException as ex:
  115. print >> sys.stderr, ("WARNING: Cannot attach to network to '{0}': {1}".format(vm.name, ex))
  116. return xid
  117. def shutdown(self, force=False):
  118. if dry_run:
  119. return
  120. connected_vms = [vm for vm in self.connected_vms.values() if vm.is_running()]
  121. if connected_vms and not force:
  122. raise QubesException("There are other VMs connected to this VM: " + str([vm.name for vm in connected_vms]))
  123. super(QubesNetVm, self).shutdown(force=force)
  124. def add_external_ip_permission(self, xid):
  125. if int(xid) < 0:
  126. return
  127. self.__external_ip_allowed_xids.add(int(xid))
  128. self.update_external_ip_permissions()
  129. def remove_external_ip_permission(self, xid):
  130. self.__external_ip_allowed_xids.discard(int(xid))
  131. self.update_external_ip_permissions()
  132. register_qubes_vm_class(QubesNetVm)