Skip to content

Commit 8d73d08

Browse files
fix(VirtualIP): make carp vhid unique per interface #754
This commit fixes an issue where a CARP virtual IP's VHID was forced to be unique across all existing CARP virtual IPs regardless of interface. This change allows the VHID to be reused by other interfaces.
1 parent 19a0922 commit 8d73d08

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

  • pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/VirtualIP.inc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use RESTAPI\Fields\IntegerField;
1010
use RESTAPI\Fields\InterfaceField;
1111
use RESTAPI\Fields\StringField;
1212
use RESTAPI\Fields\UIDField;
13+
use RESTAPI\Responses\ConflictError;
1314
use RESTAPI\Responses\ValidationError;
1415
use RESTAPI\Validators\IPAddressValidator;
1516
use RESTAPI\Validators\UniqueFromForeignModelValidator;
@@ -89,7 +90,6 @@ class VirtualIP extends Model {
8990
);
9091
$this->vhid = new IntegerField(
9192
required: true,
92-
unique: true,
9393
minimum: 1,
9494
maximum: 255,
9595
conditions: ['mode' => 'carp'],
@@ -178,6 +178,27 @@ class VirtualIP extends Model {
178178
return $subnet_bits;
179179
}
180180

181+
/**
182+
* Adds extra validation to the vhid field.
183+
* @param int $vhid The incoming `vhid` value to be validated.
184+
* @return int The validated `vhid` value to be set.
185+
* @throws ValidationError When the `vhid` value is already used by another CARP virtual IP on the same interface.
186+
*/
187+
public function validate_vhid(int $vhid): int {
188+
# Check for an existing CARP virtual IP with the same VHID on this interface
189+
$vip_q = $this->query(id__except: $this->id, mode: 'carp', interface: $this->interface->value, vhid: $vhid);
190+
191+
# Ensure no other CARP virtual IP on this interface is using the same VHID
192+
if ($vip_q->exists()) {
193+
$vip = $vip_q->first();
194+
throw new ConflictError(
195+
message: "Virtual IP with ID '$vip->id' is already using VHID '$vhid' on interface '{$this->interface->value}'",
196+
response_id: 'VIRTUALIP_VHID_ALREADY_IN_USE',
197+
);
198+
}
199+
return $vhid;
200+
}
201+
181202
/**
182203
* Obtains the current internal CARP status of this object
183204
* @return string|null Returns a string that indicates the current CARP status of this virtual IP, or null

0 commit comments

Comments
 (0)