ip.py 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. import netaddr
  2. from django.contrib.contenttypes.fields import GenericForeignKey
  3. from django.core.exceptions import ValidationError
  4. from django.db import models
  5. from django.db.models import F
  6. from django.db.models.functions import Cast
  7. from django.utils.functional import cached_property
  8. from django.utils.translation import gettext_lazy as _
  9. from netaddr.ip import IPNetwork
  10. from core.models import ObjectType
  11. from dcim.models.mixins import CachedScopeMixin
  12. from ipam.choices import *
  13. from ipam.constants import *
  14. from ipam.fields import IPNetworkField, IPAddressField
  15. from ipam.lookups import Host
  16. from ipam.managers import IPAddressManager
  17. from ipam.querysets import PrefixQuerySet
  18. from ipam.validators import DNSValidator
  19. from netbox.config import get_config
  20. from netbox.models import OrganizationalModel, PrimaryModel
  21. from netbox.models.features import ContactsMixin
  22. __all__ = (
  23. 'Aggregate',
  24. 'IPAddress',
  25. 'IPRange',
  26. 'Prefix',
  27. 'RIR',
  28. 'Role',
  29. )
  30. class GetAvailablePrefixesMixin:
  31. def get_available_prefixes(self):
  32. """
  33. Return all available prefixes within this Aggregate or Prefix as an IPSet.
  34. """
  35. params = {
  36. 'prefix__net_contained': str(self.prefix)
  37. }
  38. if hasattr(self, 'vrf'):
  39. params['vrf'] = self.vrf
  40. child_prefixes = Prefix.objects.filter(**params).values_list('prefix', flat=True)
  41. return netaddr.IPSet(self.prefix) - netaddr.IPSet(child_prefixes)
  42. def get_first_available_prefix(self):
  43. """
  44. Return the first available child prefix within the prefix (or None).
  45. """
  46. available_prefixes = self.get_available_prefixes()
  47. if not available_prefixes:
  48. return None
  49. return available_prefixes.iter_cidrs()[0]
  50. class RIR(OrganizationalModel):
  51. """
  52. A Regional Internet Registry (RIR) is responsible for the allocation of a large portion of the global IP address
  53. space. This can be an organization like ARIN or RIPE, or a governing standard such as RFC 1918.
  54. """
  55. is_private = models.BooleanField(
  56. default=False,
  57. verbose_name=_('private'),
  58. help_text=_('IP space managed by this RIR is considered private')
  59. )
  60. class Meta:
  61. ordering = ('name',)
  62. verbose_name = _('RIR')
  63. verbose_name_plural = _('RIRs')
  64. class Aggregate(ContactsMixin, GetAvailablePrefixesMixin, PrimaryModel):
  65. """
  66. An aggregate exists at the root level of the IP address space hierarchy in NetBox. Aggregates are used to organize
  67. the hierarchy and track the overall utilization of available address space. Each Aggregate is assigned to a RIR.
  68. """
  69. prefix = IPNetworkField(
  70. help_text=_("IPv4 or IPv6 network")
  71. )
  72. rir = models.ForeignKey(
  73. to='ipam.RIR',
  74. on_delete=models.PROTECT,
  75. related_name='aggregates',
  76. verbose_name=_('RIR'),
  77. help_text=_("Regional Internet Registry responsible for this IP space")
  78. )
  79. tenant = models.ForeignKey(
  80. to='tenancy.Tenant',
  81. on_delete=models.PROTECT,
  82. related_name='aggregates',
  83. blank=True,
  84. null=True
  85. )
  86. date_added = models.DateField(
  87. verbose_name=_('date added'),
  88. blank=True,
  89. null=True
  90. )
  91. clone_fields = (
  92. 'rir', 'tenant', 'date_added', 'description',
  93. )
  94. prerequisite_models = (
  95. 'ipam.RIR',
  96. )
  97. class Meta:
  98. ordering = ('prefix', 'pk') # prefix may be non-unique
  99. verbose_name = _('aggregate')
  100. verbose_name_plural = _('aggregates')
  101. def __str__(self):
  102. return str(self.prefix)
  103. def clean(self):
  104. super().clean()
  105. if self.prefix:
  106. # /0 masks are not acceptable
  107. if self.prefix.prefixlen == 0:
  108. raise ValidationError({
  109. 'prefix': _("Cannot create aggregate with /0 mask.")
  110. })
  111. # Ensure that the aggregate being added is not covered by an existing aggregate
  112. covering_aggregates = Aggregate.objects.filter(
  113. prefix__net_contains_or_equals=str(self.prefix)
  114. )
  115. if self.pk:
  116. covering_aggregates = covering_aggregates.exclude(pk=self.pk)
  117. if covering_aggregates:
  118. raise ValidationError({
  119. 'prefix': _(
  120. "Aggregates cannot overlap. {prefix} is already covered by an existing aggregate ({aggregate})."
  121. ).format(
  122. prefix=self.prefix,
  123. aggregate=covering_aggregates[0]
  124. )
  125. })
  126. # Ensure that the aggregate being added does not cover an existing aggregate
  127. covered_aggregates = Aggregate.objects.filter(prefix__net_contained=str(self.prefix))
  128. if self.pk:
  129. covered_aggregates = covered_aggregates.exclude(pk=self.pk)
  130. if covered_aggregates:
  131. raise ValidationError({
  132. 'prefix': _(
  133. "Prefixes cannot overlap aggregates. {prefix} covers an existing aggregate ({aggregate})."
  134. ).format(
  135. prefix=self.prefix,
  136. aggregate=covered_aggregates[0]
  137. )
  138. })
  139. @property
  140. def family(self):
  141. if self.prefix:
  142. return self.prefix.version
  143. return None
  144. def get_child_prefixes(self):
  145. """
  146. Return all Prefixes within this Aggregate
  147. """
  148. return Prefix.objects.filter(prefix__net_contained=str(self.prefix))
  149. def get_utilization(self):
  150. """
  151. Determine the prefix utilization of the aggregate and return it as a percentage.
  152. """
  153. queryset = Prefix.objects.filter(prefix__net_contained_or_equal=str(self.prefix))
  154. child_prefixes = netaddr.IPSet([p.prefix for p in queryset])
  155. utilization = float(child_prefixes.size) / self.prefix.size * 100
  156. return min(utilization, 100)
  157. class Role(OrganizationalModel):
  158. """
  159. A Role represents the functional role of a Prefix or VLAN; for example, "Customer," "Infrastructure," or
  160. "Management."
  161. """
  162. weight = models.PositiveSmallIntegerField(
  163. verbose_name=_('weight'),
  164. default=1000
  165. )
  166. class Meta:
  167. ordering = ('weight', 'name')
  168. verbose_name = _('role')
  169. verbose_name_plural = _('roles')
  170. def __str__(self):
  171. return self.name
  172. class Prefix(ContactsMixin, GetAvailablePrefixesMixin, CachedScopeMixin, PrimaryModel):
  173. """
  174. A Prefix represents an IPv4 or IPv6 network, including mask length. Prefixes can optionally be scoped to certain
  175. areas and/or assigned to VRFs. A Prefix must be assigned a status and may optionally be assigned a used-define Role.
  176. A Prefix can also be assigned to a VLAN where appropriate.
  177. """
  178. aggregate = models.ForeignKey(
  179. to='ipam.Aggregate',
  180. on_delete=models.SET_NULL,
  181. related_name='prefixes',
  182. blank=True,
  183. null=True,
  184. verbose_name=_('aggregate')
  185. )
  186. parent = models.ForeignKey(
  187. to='ipam.Prefix',
  188. on_delete=models.SET_NULL,
  189. related_name='children',
  190. blank=True,
  191. null=True,
  192. verbose_name=_('Prefix')
  193. )
  194. prefix = IPNetworkField(
  195. verbose_name=_('prefix'),
  196. help_text=_('IPv4 or IPv6 network with mask')
  197. )
  198. vrf = models.ForeignKey(
  199. to='ipam.VRF',
  200. on_delete=models.PROTECT,
  201. related_name='prefixes',
  202. blank=True,
  203. null=True,
  204. verbose_name=_('VRF')
  205. )
  206. tenant = models.ForeignKey(
  207. to='tenancy.Tenant',
  208. on_delete=models.PROTECT,
  209. related_name='prefixes',
  210. blank=True,
  211. null=True
  212. )
  213. vlan = models.ForeignKey(
  214. to='ipam.VLAN',
  215. on_delete=models.PROTECT,
  216. related_name='prefixes',
  217. blank=True,
  218. null=True
  219. )
  220. status = models.CharField(
  221. max_length=50,
  222. choices=PrefixStatusChoices,
  223. default=PrefixStatusChoices.STATUS_ACTIVE,
  224. verbose_name=_('status'),
  225. help_text=_('Operational status of this prefix')
  226. )
  227. role = models.ForeignKey(
  228. to='ipam.Role',
  229. on_delete=models.SET_NULL,
  230. related_name='prefixes',
  231. blank=True,
  232. null=True,
  233. help_text=_('The primary function of this prefix')
  234. )
  235. is_pool = models.BooleanField(
  236. verbose_name=_('is a pool'),
  237. default=False,
  238. help_text=_('All IP addresses within this prefix are considered usable')
  239. )
  240. mark_utilized = models.BooleanField(
  241. verbose_name=_('mark utilized'),
  242. default=False,
  243. help_text=_("Treat as fully utilized")
  244. )
  245. # Cached depth & child counts
  246. _depth = models.PositiveSmallIntegerField(
  247. default=0,
  248. editable=False
  249. )
  250. _children = models.PositiveBigIntegerField(
  251. default=0,
  252. editable=False
  253. )
  254. objects = PrefixQuerySet.as_manager()
  255. clone_fields = (
  256. 'scope_type', 'scope_id', 'vrf', 'tenant', 'vlan', 'status', 'role', 'is_pool', 'mark_utilized', 'description',
  257. )
  258. class Meta:
  259. ordering = (F('vrf').asc(nulls_first=True), 'prefix', 'pk') # (vrf, prefix) may be non-unique
  260. verbose_name = _('prefix')
  261. verbose_name_plural = _('prefixes')
  262. def __init__(self, *args, **kwargs):
  263. super().__init__(*args, **kwargs)
  264. # Cache the original prefix and VRF so we can check if they have changed on post_save
  265. self._prefix = self.__dict__.get('prefix')
  266. self._vrf_id = self.__dict__.get('vrf_id')
  267. def __str__(self):
  268. return str(self.prefix)
  269. def clean(self):
  270. super().clean()
  271. if self.prefix:
  272. if not isinstance(self.prefix, IPNetwork):
  273. self.prefix = IPNetwork(self.prefix)
  274. # /0 masks are not acceptable
  275. if self.prefix.prefixlen == 0:
  276. raise ValidationError({
  277. 'prefix': _("Cannot create prefix with /0 mask.")
  278. })
  279. if self.parent:
  280. if self.prefix not in self.parent.prefix:
  281. raise ValidationError({
  282. 'parent': _("Prefix must be part of parent prefix.")
  283. })
  284. if self.parent.status != PrefixStatusChoices.STATUS_CONTAINER and self.vrf != self.parent.vrf:
  285. raise ValidationError({
  286. 'vrf': _("VRF must match the parent VRF.")
  287. })
  288. # Enforce unique IP space (if applicable)
  289. if (self.vrf is None and get_config().ENFORCE_GLOBAL_UNIQUE) or (self.vrf and self.vrf.enforce_unique):
  290. duplicate_prefixes = self.get_duplicates()
  291. if duplicate_prefixes:
  292. table = _("VRF {vrf}").format(vrf=self.vrf) if self.vrf else _("global table")
  293. raise ValidationError({
  294. 'prefix': _("Duplicate prefix found in {table}: {prefix}").format(
  295. table=table,
  296. prefix=duplicate_prefixes.first(),
  297. )
  298. })
  299. def save(self, *args, **kwargs):
  300. vrf_id = self.vrf.pk if self.vrf else None
  301. if not self.pk and not self.parent:
  302. parent = self.find_parent_prefix(self)
  303. self.parent = parent
  304. elif self.parent and (self.prefix != self._prefix or vrf_id != self._vrf_id):
  305. parent = self.find_parent_prefix(self)
  306. self.parent = parent
  307. if isinstance(self.prefix, netaddr.IPNetwork):
  308. # Clear host bits from prefix
  309. self.prefix = self.prefix.cidr
  310. # Cache objects associated with the terminating object (for filtering)
  311. self.cache_related_objects()
  312. super().save(*args, **kwargs)
  313. @property
  314. def family(self):
  315. return self.prefix.version if self.prefix else None
  316. @property
  317. def mask_length(self):
  318. return self.prefix.prefixlen if self.prefix else None
  319. @property
  320. def depth_count(self):
  321. return self._depth
  322. @property
  323. def children_count(self):
  324. return self._children
  325. def _set_prefix_length(self, value):
  326. """
  327. Expose the IPNetwork object's prefixlen attribute on the parent model so that it can be manipulated directly,
  328. e.g. for bulk editing.
  329. """
  330. if self.prefix is not None:
  331. self.prefix.prefixlen = value
  332. prefix_length = property(fset=_set_prefix_length)
  333. def get_status_color(self):
  334. return PrefixStatusChoices.colors.get(self.status)
  335. def get_parents(self, include_self=False):
  336. """
  337. Return all containing Prefixes in the hierarchy.
  338. """
  339. lookup = 'net_contains_or_equals' if include_self else 'net_contains'
  340. return Prefix.objects.filter(**{
  341. 'vrf': self.vrf,
  342. f'prefix__{lookup}': self.prefix
  343. })
  344. def get_children(self, include_self=False):
  345. """
  346. Return all covered Prefixes in the hierarchy.
  347. """
  348. lookup = 'net_contained_or_equal' if include_self else 'net_contained'
  349. return Prefix.objects.filter(**{
  350. 'vrf': self.vrf,
  351. f'prefix__{lookup}': self.prefix
  352. })
  353. def get_duplicates(self):
  354. return Prefix.objects.filter(vrf=self.vrf, prefix=str(self.prefix)).exclude(pk=self.pk)
  355. def get_child_prefixes(self):
  356. """
  357. Return all Prefixes within this Prefix and VRF. If this Prefix is a container in the global table, return child
  358. Prefixes belonging to any VRF.
  359. """
  360. if self.vrf is None and self.status == PrefixStatusChoices.STATUS_CONTAINER:
  361. return Prefix.objects.filter(prefix__net_contained=str(self.prefix))
  362. else:
  363. return Prefix.objects.filter(prefix__net_contained=str(self.prefix), vrf=self.vrf)
  364. def get_child_ranges(self, **kwargs):
  365. """
  366. Return all IPRanges within this Prefix and VRF.
  367. """
  368. return IPRange.objects.filter(
  369. vrf=self.vrf,
  370. start_address__net_host_contained=str(self.prefix),
  371. end_address__net_host_contained=str(self.prefix),
  372. **kwargs
  373. )
  374. def get_child_ips(self):
  375. """
  376. Return all IPAddresses within this Prefix and VRF. If this Prefix is a container in the global table, return
  377. child IPAddresses belonging to any VRF.
  378. """
  379. if self.vrf is None and self.status == PrefixStatusChoices.STATUS_CONTAINER:
  380. return IPAddress.objects.filter(address__net_host_contained=str(self.prefix))
  381. else:
  382. return IPAddress.objects.filter(address__net_host_contained=str(self.prefix), vrf=self.vrf)
  383. def get_available_ips(self):
  384. """
  385. Return all available IPs within this prefix as an IPSet.
  386. """
  387. prefix = netaddr.IPSet(self.prefix)
  388. child_ips = netaddr.IPSet([
  389. ip.address.ip for ip in self.get_child_ips()
  390. ])
  391. child_ranges = netaddr.IPSet([
  392. iprange.range for iprange in self.get_child_ranges().filter(mark_populated=True)
  393. ])
  394. available_ips = prefix - child_ips - child_ranges
  395. # IPv6 /127's, pool, or IPv4 /31-/32 sets are fully usable
  396. if (self.family == 6 and self.prefix.prefixlen >= 127) or self.is_pool or (
  397. self.family == 4 and self.prefix.prefixlen >= 31
  398. ):
  399. return available_ips
  400. if self.family == 4:
  401. # For "normal" IPv4 prefixes, omit first and last addresses
  402. available_ips -= netaddr.IPSet([
  403. netaddr.IPAddress(self.prefix.first),
  404. netaddr.IPAddress(self.prefix.last),
  405. ])
  406. else:
  407. # For IPv6 prefixes, omit the Subnet-Router anycast address
  408. # per RFC 4291
  409. available_ips -= netaddr.IPSet([netaddr.IPAddress(self.prefix.first)])
  410. return available_ips
  411. def get_first_available_ip(self):
  412. """
  413. Return the first available IP within the prefix (or None).
  414. """
  415. available_ips = self.get_available_ips()
  416. if not available_ips:
  417. return None
  418. return '{}/{}'.format(next(available_ips.__iter__()), self.prefix.prefixlen)
  419. def get_utilization(self):
  420. """
  421. Determine the utilization of the prefix and return it as a percentage. For Prefixes with a status of
  422. "container", calculate utilization based on child prefixes. For all others, count child IP addresses.
  423. """
  424. if self.mark_utilized:
  425. return 100
  426. if self.status == PrefixStatusChoices.STATUS_CONTAINER:
  427. queryset = Prefix.objects.filter(
  428. prefix__net_contained=str(self.prefix),
  429. vrf=self.vrf
  430. )
  431. child_prefixes = netaddr.IPSet([p.prefix for p in queryset])
  432. utilization = float(child_prefixes.size) / self.prefix.size * 100
  433. else:
  434. # Compile an IPSet to avoid counting duplicate IPs
  435. child_ips = netaddr.IPSet()
  436. for iprange in self.get_child_ranges().filter(mark_utilized=True):
  437. child_ips.add(iprange.range)
  438. for ip in self.get_child_ips():
  439. child_ips.add(ip.address.ip)
  440. prefix_size = self.prefix.size
  441. if self.prefix.version == 4 and self.prefix.prefixlen < 31 and not self.is_pool:
  442. prefix_size -= 2
  443. utilization = float(child_ips.size) / prefix_size * 100
  444. return min(utilization, 100)
  445. @classmethod
  446. def find_parent_prefix(cls, network):
  447. prefixes = Prefix.objects.filter(
  448. models.Q(
  449. vrf=network.vrf,
  450. prefix__net_contains=str(network)
  451. ) | models.Q(
  452. vrf=None,
  453. status=PrefixStatusChoices.STATUS_CONTAINER,
  454. prefix__net_contains=str(network),
  455. )
  456. )
  457. return prefixes.last()
  458. class IPRange(ContactsMixin, PrimaryModel):
  459. """
  460. A range of IP addresses, defined by start and end addresses.
  461. """
  462. prefix = models.ForeignKey(
  463. to='ipam.Prefix',
  464. on_delete=models.SET_NULL,
  465. related_name='ip_ranges',
  466. null=True,
  467. blank=True,
  468. verbose_name=_('prefix'),
  469. )
  470. start_address = IPAddressField(
  471. verbose_name=_('start address'),
  472. help_text=_('IPv4 or IPv6 address (with mask)')
  473. )
  474. end_address = IPAddressField(
  475. verbose_name=_('end address'),
  476. help_text=_('IPv4 or IPv6 address (with mask)')
  477. )
  478. size = models.PositiveIntegerField(
  479. verbose_name=_('size'),
  480. editable=False
  481. )
  482. vrf = models.ForeignKey(
  483. to='ipam.VRF',
  484. on_delete=models.PROTECT,
  485. related_name='ip_ranges',
  486. blank=True,
  487. null=True,
  488. verbose_name=_('VRF')
  489. )
  490. tenant = models.ForeignKey(
  491. to='tenancy.Tenant',
  492. on_delete=models.PROTECT,
  493. related_name='ip_ranges',
  494. blank=True,
  495. null=True
  496. )
  497. status = models.CharField(
  498. verbose_name=_('status'),
  499. max_length=50,
  500. choices=IPRangeStatusChoices,
  501. default=IPRangeStatusChoices.STATUS_ACTIVE,
  502. help_text=_('Operational status of this range')
  503. )
  504. role = models.ForeignKey(
  505. to='ipam.Role',
  506. on_delete=models.SET_NULL,
  507. related_name='ip_ranges',
  508. blank=True,
  509. null=True,
  510. help_text=_('The primary function of this range')
  511. )
  512. mark_populated = models.BooleanField(
  513. verbose_name=_('mark populated'),
  514. default=False,
  515. help_text=_("Prevent the creation of IP addresses within this range")
  516. )
  517. mark_utilized = models.BooleanField(
  518. verbose_name=_('mark utilized'),
  519. default=False,
  520. help_text=_("Report space as 100% utilized")
  521. )
  522. clone_fields = (
  523. 'vrf', 'tenant', 'status', 'role', 'description', 'mark_populated', 'mark_utilized',
  524. )
  525. class Meta:
  526. ordering = (F('vrf').asc(nulls_first=True), 'start_address', 'pk') # (vrf, start_address) may be non-unique
  527. verbose_name = _('IP range')
  528. verbose_name_plural = _('IP ranges')
  529. def __str__(self):
  530. return self.name
  531. def clean(self):
  532. super().clean()
  533. if self.start_address and self.end_address:
  534. # If prefix is set, validate suitability
  535. if self.prefix:
  536. # Check that start address and end address are within the prefix range
  537. if self.start_address not in self.prefix.prefix and self.end_address not in self.prefix.prefix:
  538. raise ValidationError({
  539. 'start_address': _("Start address must be part of the selected prefix"),
  540. 'end_address': _("End address must be part of the selected prefix.")
  541. })
  542. elif self.start_address not in self.prefix.prefix:
  543. raise ValidationError({
  544. 'start_address': _("Start address must be part of the selected prefix")
  545. })
  546. elif self.end_address not in self.prefix.prefix:
  547. raise ValidationError({
  548. 'end_address': _("End address must be part of the selected prefix.")
  549. })
  550. # Check that VRF matches prefix VRF
  551. if self.vrf != self.prefix.vrf:
  552. raise ValidationError({
  553. 'vrf': _("VRF must match the prefix VRF.")
  554. })
  555. # Check that start & end IP versions match
  556. if self.start_address.version != self.end_address.version:
  557. raise ValidationError({
  558. 'end_address': _("Starting and ending IP address versions must match")
  559. })
  560. # Check that the start & end IP prefix lengths match
  561. if self.start_address.prefixlen != self.end_address.prefixlen:
  562. raise ValidationError({
  563. 'end_address': _("Starting and ending IP address masks must match")
  564. })
  565. # Check that the ending address is greater than the starting address
  566. if not self.end_address > self.start_address:
  567. raise ValidationError({
  568. 'end_address': _(
  569. "Ending address must be greater than the starting address ({start_address})"
  570. ).format(start_address=self.start_address)
  571. })
  572. # Check for overlapping ranges
  573. overlapping_ranges = (
  574. IPRange.objects.exclude(pk=self.pk)
  575. .filter(vrf=self.vrf)
  576. .filter(
  577. # Starts inside
  578. Q(
  579. start_address__host__inet__gte=self.start_address.ip,
  580. start_address__host__inet__lte=self.end_address.ip,
  581. ) |
  582. # Ends inside
  583. Q(
  584. end_address__host__inet__gte=self.start_address.ip,
  585. end_address__host__inet__lte=self.end_address.ip,
  586. ) |
  587. # Starts & ends outside
  588. Q(
  589. start_address__host__inet__lte=self.start_address.ip,
  590. end_address__host__inet__gte=self.end_address.ip,
  591. )
  592. )
  593. )
  594. if overlapping_ranges.exists():
  595. raise ValidationError(
  596. _("Defined addresses overlap with range {overlapping_range} in VRF {vrf}").format(
  597. overlapping_range=overlapping_ranges.first(),
  598. vrf=self.vrf
  599. ))
  600. # Validate maximum size
  601. MAX_SIZE = 2 ** 32 - 1
  602. if int(self.end_address.ip - self.start_address.ip) + 1 > MAX_SIZE:
  603. raise ValidationError(
  604. _("Defined range exceeds maximum supported size ({max_size})").format(max_size=MAX_SIZE)
  605. )
  606. def save(self, *args, **kwargs):
  607. # Record the range's size (number of IP addresses)
  608. self.size = int(self.end_address.ip - self.start_address.ip) + 1
  609. super().save(*args, **kwargs)
  610. @property
  611. def family(self):
  612. return self.start_address.version if self.start_address else None
  613. @property
  614. def range(self):
  615. return netaddr.IPRange(self.start_address.ip, self.end_address.ip)
  616. @property
  617. def mask_length(self):
  618. return self.start_address.prefixlen if self.start_address else None
  619. @cached_property
  620. def name(self):
  621. """
  622. Return an efficient string representation of the IP range.
  623. """
  624. separator = ':' if self.family == 6 else '.'
  625. start_chunks = str(self.start_address.ip).split(separator)
  626. end_chunks = str(self.end_address.ip).split(separator)
  627. base_chunks = []
  628. for a, b in zip(start_chunks, end_chunks):
  629. if a == b:
  630. base_chunks.append(a)
  631. base_str = separator.join(base_chunks)
  632. start_str = separator.join(start_chunks[len(base_chunks):])
  633. end_str = separator.join(end_chunks[len(base_chunks):])
  634. return f'{base_str}{separator}{start_str}-{end_str}/{self.start_address.prefixlen}'
  635. def _set_prefix_length(self, value):
  636. """
  637. Expose the IPRange object's prefixlen attribute on the parent model so that it can be manipulated directly,
  638. e.g. for bulk editing.
  639. """
  640. self.start_address.prefixlen = value
  641. self.end_address.prefixlen = value
  642. prefix_length = property(fset=_set_prefix_length)
  643. def get_status_color(self):
  644. return IPRangeStatusChoices.colors.get(self.status)
  645. def get_child_ips(self):
  646. """
  647. Return all IPAddresses within this IPRange and VRF.
  648. """
  649. return IPAddress.objects.filter(
  650. address__gte=self.start_address,
  651. address__lte=self.end_address,
  652. vrf=self.vrf
  653. )
  654. def get_available_ips(self):
  655. """
  656. Return all available IPs within this range as an IPSet.
  657. """
  658. if self.mark_populated:
  659. return netaddr.IPSet()
  660. range = netaddr.IPRange(self.start_address.ip, self.end_address.ip)
  661. child_ips = netaddr.IPSet([ip.address.ip for ip in self.get_child_ips()])
  662. return netaddr.IPSet(range) - child_ips
  663. @cached_property
  664. def first_available_ip(self):
  665. """
  666. Return the first available IP within the range (or None).
  667. """
  668. available_ips = self.get_available_ips()
  669. if not available_ips:
  670. return None
  671. return '{}/{}'.format(next(available_ips.__iter__()), self.start_address.prefixlen)
  672. @cached_property
  673. def utilization(self):
  674. """
  675. Determine the utilization of the range and return it as a percentage.
  676. """
  677. if self.mark_utilized:
  678. return 100
  679. # Compile an IPSet to avoid counting duplicate IPs
  680. child_count = netaddr.IPSet([
  681. ip.address.ip for ip in self.get_child_ips()
  682. ]).size
  683. return min(float(child_count) / self.size * 100, 100)
  684. class IPAddress(ContactsMixin, PrimaryModel):
  685. """
  686. An IPAddress represents an individual IPv4 or IPv6 address and its mask. The mask length should match what is
  687. configured in the real world. (Typically, only loopback interfaces are configured with /32 or /128 masks.) Like
  688. Prefixes, IPAddresses can optionally be assigned to a VRF. An IPAddress can optionally be assigned to an Interface.
  689. Interfaces can have zero or more IPAddresses assigned to them.
  690. An IPAddress can also optionally point to a NAT inside IP, designating itself as a NAT outside IP. This is useful,
  691. for example, when mapping public addresses to private addresses. When an Interface has been assigned an IPAddress
  692. which has a NAT outside IP, that Interface's Device can use either the inside or outside IP as its primary IP.
  693. """
  694. prefix = models.ForeignKey(
  695. to='ipam.Prefix',
  696. on_delete=models.SET_NULL,
  697. related_name='ip_addresses',
  698. blank=True,
  699. null=True,
  700. verbose_name=_('Prefix')
  701. )
  702. address = IPAddressField(
  703. verbose_name=_('address'),
  704. help_text=_('IPv4 or IPv6 address (with mask)')
  705. )
  706. vrf = models.ForeignKey(
  707. to='ipam.VRF',
  708. on_delete=models.PROTECT,
  709. related_name='ip_addresses',
  710. blank=True,
  711. null=True,
  712. verbose_name=_('VRF')
  713. )
  714. tenant = models.ForeignKey(
  715. to='tenancy.Tenant',
  716. on_delete=models.PROTECT,
  717. related_name='ip_addresses',
  718. blank=True,
  719. null=True
  720. )
  721. status = models.CharField(
  722. verbose_name=_('status'),
  723. max_length=50,
  724. choices=IPAddressStatusChoices,
  725. default=IPAddressStatusChoices.STATUS_ACTIVE,
  726. help_text=_('The operational status of this IP')
  727. )
  728. role = models.CharField(
  729. verbose_name=_('role'),
  730. max_length=50,
  731. choices=IPAddressRoleChoices,
  732. blank=True,
  733. null=True,
  734. help_text=_('The functional role of this IP')
  735. )
  736. assigned_object_type = models.ForeignKey(
  737. to='contenttypes.ContentType',
  738. on_delete=models.PROTECT,
  739. related_name='+',
  740. blank=True,
  741. null=True
  742. )
  743. assigned_object_id = models.PositiveBigIntegerField(
  744. blank=True,
  745. null=True
  746. )
  747. assigned_object = GenericForeignKey(
  748. ct_field='assigned_object_type',
  749. fk_field='assigned_object_id'
  750. )
  751. nat_inside = models.ForeignKey(
  752. to='self',
  753. on_delete=models.SET_NULL,
  754. related_name='nat_outside',
  755. blank=True,
  756. null=True,
  757. verbose_name=_('NAT (inside)'),
  758. help_text=_('The IP for which this address is the "outside" IP')
  759. )
  760. dns_name = models.CharField(
  761. max_length=255,
  762. blank=True,
  763. validators=[DNSValidator],
  764. verbose_name=_('DNS name'),
  765. help_text=_('Hostname or FQDN (not case-sensitive)')
  766. )
  767. objects = IPAddressManager()
  768. clone_fields = (
  769. 'vrf', 'tenant', 'status', 'role', 'dns_name', 'description',
  770. )
  771. class Meta:
  772. ordering = ('address', 'pk') # address may be non-unique
  773. indexes = (
  774. models.Index(Cast(Host('address'), output_field=IPAddressField()), name='ipam_ipaddress_host'),
  775. models.Index(fields=('assigned_object_type', 'assigned_object_id')),
  776. )
  777. verbose_name = _('IP address')
  778. verbose_name_plural = _('IP addresses')
  779. def __str__(self):
  780. return str(self.address)
  781. def __init__(self, *args, **kwargs):
  782. super().__init__(*args, **kwargs)
  783. self._address = self.address
  784. # Denote the original assigned object (if any) for validation in clean()
  785. self._original_assigned_object_id = self.__dict__.get('assigned_object_id')
  786. self._original_assigned_object_type_id = self.__dict__.get('assigned_object_type_id')
  787. def get_duplicates(self):
  788. return IPAddress.objects.filter(
  789. vrf=self.vrf,
  790. address__net_host=str(self.address.ip)
  791. ).exclude(pk=self.pk)
  792. def get_next_available_ip(self):
  793. """
  794. Return the next available IP address within this IP's network (if any)
  795. """
  796. if self.address and self.address.broadcast:
  797. start_ip = self.address.ip + 1
  798. end_ip = self.address.broadcast - 1
  799. if start_ip <= end_ip:
  800. available_ips = netaddr.IPSet(netaddr.IPRange(start_ip, end_ip))
  801. available_ips -= netaddr.IPSet([
  802. address.ip for address in IPAddress.objects.filter(
  803. vrf=self.vrf,
  804. address__gt=self.address,
  805. address__net_contained_or_equal=self.address.cidr
  806. ).values_list('address', flat=True)
  807. ])
  808. if available_ips:
  809. return next(iter(available_ips))
  810. def get_related_ips(self):
  811. """
  812. Return all IPAddresses belonging to the same VRF.
  813. """
  814. return IPAddress.objects.exclude(address=str(self.address)).filter(
  815. vrf=self.vrf, address__net_contained_or_equal=str(self.address)
  816. )
  817. def clean(self):
  818. super().clean()
  819. if self.address:
  820. # If prefix is set, validate suitability
  821. if self.prefix:
  822. if self.address not in self.prefix.prefix:
  823. raise ValidationError({
  824. 'prefix': _("IP address must be part of the selected prefix.")
  825. })
  826. if self.vrf != self.prefix.vrf:
  827. raise ValidationError({
  828. 'vrf': _("IP address VRF must match the prefix VRF.")
  829. })
  830. # /0 masks are not acceptable
  831. if self.address.prefixlen == 0:
  832. raise ValidationError({
  833. 'address': _("Cannot create IP address with /0 mask.")
  834. })
  835. # Do not allow assigning a network ID or broadcast address to an interface.
  836. if self.assigned_object:
  837. if self.address.ip == self.address.network:
  838. msg = _("{ip} is a network ID, which may not be assigned to an interface.").format(
  839. ip=self.address.ip
  840. )
  841. if self.address.version == 4 and self.address.prefixlen not in (31, 32):
  842. raise ValidationError(msg)
  843. if self.address.version == 6 and self.address.prefixlen not in (127, 128):
  844. raise ValidationError(msg)
  845. if (
  846. self.address.version == 4 and self.address.ip == self.address.broadcast and
  847. self.address.prefixlen not in (31, 32)
  848. ):
  849. msg = _("{ip} is a broadcast address, which may not be assigned to an interface.").format(
  850. ip=self.address.ip
  851. )
  852. raise ValidationError(msg)
  853. # Enforce unique IP space (if applicable)
  854. if (self.vrf is None and get_config().ENFORCE_GLOBAL_UNIQUE) or (self.vrf and self.vrf.enforce_unique):
  855. duplicate_ips = self.get_duplicates()
  856. if duplicate_ips and (
  857. self.role not in IPADDRESS_ROLES_NONUNIQUE or
  858. any(dip.role not in IPADDRESS_ROLES_NONUNIQUE for dip in duplicate_ips)
  859. ):
  860. table = _("VRF {vrf}").format(vrf=self.vrf) if self.vrf else _("global table")
  861. raise ValidationError({
  862. 'address': _("Duplicate IP address found in {table}: {ipaddress}").format(
  863. table=table,
  864. ipaddress=duplicate_ips.first(),
  865. )
  866. })
  867. # Disallow the creation of IPAddresses within an IPRange with mark_populated=True
  868. parent_range = IPRange.objects.filter(
  869. start_address__lte=self.address,
  870. end_address__gte=self.address,
  871. vrf=self.vrf,
  872. mark_populated=True
  873. ).first()
  874. if parent_range:
  875. raise ValidationError({
  876. 'address': _(
  877. "Cannot create IP address {ip} inside range {range}."
  878. ).format(ip=self.address, range=parent_range)
  879. })
  880. if self._original_assigned_object_id and self._original_assigned_object_type_id:
  881. parent = getattr(self.assigned_object, 'parent_object', None)
  882. ct = ObjectType.objects.get_for_id(self._original_assigned_object_type_id)
  883. original_assigned_object = ct.get_object_for_this_type(pk=self._original_assigned_object_id)
  884. original_parent = getattr(original_assigned_object, 'parent_object', None)
  885. # can't use is_primary_ip as self.assigned_object might be changed
  886. is_primary = False
  887. if self.family == 4 and hasattr(original_parent, 'primary_ip4'):
  888. if original_parent.primary_ip4_id == self.pk:
  889. is_primary = True
  890. if self.family == 6 and hasattr(original_parent, 'primary_ip6'):
  891. if original_parent.primary_ip6_id == self.pk:
  892. is_primary = True
  893. if is_primary and (parent != original_parent):
  894. raise ValidationError(
  895. _("Cannot reassign IP address while it is designated as the primary IP for the parent object")
  896. )
  897. # Validate IP status selection
  898. if self.status == IPAddressStatusChoices.STATUS_SLAAC and self.family != 6:
  899. raise ValidationError({
  900. 'status': _("Only IPv6 addresses can be assigned SLAAC status")
  901. })
  902. def save(self, *args, **kwargs):
  903. # Force dns_name to lowercase
  904. self.dns_name = self.dns_name.lower()
  905. super().save(*args, **kwargs)
  906. def clone(self):
  907. attrs = super().clone()
  908. # Populate the address field with the next available IP (if any)
  909. if next_available_ip := self.get_next_available_ip():
  910. attrs['address'] = f'{next_available_ip}/{self.address.prefixlen}'
  911. return attrs
  912. def to_objectchange(self, action):
  913. objectchange = super().to_objectchange(action)
  914. objectchange.related_object = self.assigned_object
  915. return objectchange
  916. @property
  917. def family(self):
  918. if self.address:
  919. return self.address.version
  920. return None
  921. @property
  922. def is_oob_ip(self):
  923. if self.assigned_object:
  924. parent = getattr(self.assigned_object, 'parent_object', None)
  925. if hasattr(parent, 'oob_ip') and parent.oob_ip_id == self.pk:
  926. return True
  927. return False
  928. @property
  929. def is_primary_ip(self):
  930. if self.assigned_object:
  931. parent = getattr(self.assigned_object, 'parent_object', None)
  932. if self.family == 4 and hasattr(parent, 'primary_ip4') and parent.primary_ip4_id == self.pk:
  933. return True
  934. if self.family == 6 and hasattr(parent, 'primary_ip6') and parent.primary_ip6_id == self.pk:
  935. return True
  936. return False
  937. def _set_mask_length(self, value):
  938. """
  939. Expose the IPNetwork object's prefixlen attribute on the parent model so that it can be manipulated directly,
  940. e.g. for bulk editing.
  941. """
  942. if self.address is not None:
  943. self.address.prefixlen = value
  944. mask_length = property(fset=_set_mask_length)
  945. def get_status_color(self):
  946. return IPAddressStatusChoices.colors.get(self.status)
  947. def get_role_color(self):
  948. return IPAddressRoleChoices.colors.get(self.role)