Skip to content

Commit 708f897

Browse files
authored
Merge pull request #3568 from stgraber/main
incusd/devices: Allow /32 and /128 for OCI addresses
2 parents d0e60b2 + 6eb95b4 commit 708f897

7 files changed

Lines changed: 28 additions & 16 deletions

File tree

cmd/incus/admin_init_interactive.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ func (c *cmdAdminInit) askNetworking(config *api.InitPreseed, d incus.InstanceSe
169169

170170
// IPv4
171171
network.Config["ipv4.address"], err = c.global.asker.AskString(i18n.G("What IPv4 address should be used?")+" (CIDR subnet notation, “auto” or “none”) [default=auto]: ", "auto", func(value string) error {
172-
if slices.Contains([]string{"auto", "none"}, value) {
172+
if slices.Contains([]string{"auto", "none", ""}, value) {
173173
return nil
174174
}
175175

176-
return validate.Optional(validate.IsNetworkAddressCIDRV4)(value)
176+
return validate.IsNetworkAddressCIDRV4(value, false)
177177
})
178178
if err != nil {
179179
return err
@@ -190,11 +190,11 @@ func (c *cmdAdminInit) askNetworking(config *api.InitPreseed, d incus.InstanceSe
190190

191191
// IPv6
192192
network.Config["ipv6.address"], err = c.global.asker.AskString(i18n.G("What IPv6 address should be used?")+" (CIDR subnet notation, “auto” or “none”) [default=auto]: ", "auto", func(value string) error {
193-
if slices.Contains([]string{"auto", "none"}, value) {
193+
if slices.Contains([]string{"auto", "none", ""}, value) {
194194
return nil
195195
}
196196

197-
return validate.Optional(validate.IsNetworkAddressCIDRV6)(value)
197+
return validate.IsNetworkAddressCIDRV6(value, false)
198198
})
199199
if err != nil {
200200
return err

internal/server/device/nic_bridged.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ func (d *nicBridged) validateConfig(instConf instance.ConfigReader, partialValid
608608
}
609609

610610
if strings.Contains(value, "/") {
611-
return validate.IsNetworkAddressCIDRV4(value)
611+
return validate.IsNetworkAddressCIDRV4(value, true)
612612
}
613613

614614
return validate.IsNetworkAddressV4(value)
@@ -620,7 +620,7 @@ func (d *nicBridged) validateConfig(instConf instance.ConfigReader, partialValid
620620
}
621621

622622
if strings.Contains(value, "/") {
623-
return validate.IsNetworkAddressCIDRV6(value)
623+
return validate.IsNetworkAddressCIDRV6(value, true)
624624
}
625625

626626
return validate.IsNetworkAddressV6(value)

internal/server/device/nic_ovn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ func (d *nicOVN) validateConfig(instConf instance.ConfigReader, partialValidatio
509509
}
510510

511511
if strings.Contains(value, "/") {
512-
return validate.IsNetworkAddressCIDRV4(value)
512+
return validate.IsNetworkAddressCIDRV4(value, true)
513513
}
514514

515515
return validate.IsNetworkAddressV4(value)
@@ -521,7 +521,7 @@ func (d *nicOVN) validateConfig(instConf instance.ConfigReader, partialValidatio
521521
}
522522

523523
if strings.Contains(value, "/") {
524-
return validate.IsNetworkAddressCIDRV6(value)
524+
return validate.IsNetworkAddressCIDRV6(value, true)
525525
}
526526

527527
return validate.IsNetworkAddressV6(value)

internal/server/network/driver_bridge.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func (n *bridge) Validate(config map[string]string, clientType request.ClientTyp
269269
return nil
270270
}
271271

272-
return validate.IsNetworkAddressCIDRV4(value)
272+
return validate.IsNetworkAddressCIDRV4(value, false)
273273
}),
274274

275275
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.firewall)
@@ -398,7 +398,7 @@ func (n *bridge) Validate(config map[string]string, clientType request.ClientTyp
398398
return nil
399399
}
400400

401-
return validate.Or(validate.IsNetworkAddressCIDRV6, validate.IsNetworkV6)(value)
401+
return validate.Or(func(value string) error { return validate.IsNetworkAddressCIDRV6(value, false) }, validate.IsNetworkV6)(value)
402402
}),
403403

404404
// gendoc:generate(entity=network_bridge, group=common, key=ipv6.firewall)

internal/server/network/driver_ovn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ func (n *ovn) Validate(config map[string]string, clientType request.ClientType)
510510
return nil
511511
}
512512

513-
return validate.IsNetworkAddressCIDRV4(value)
513+
return validate.IsNetworkAddressCIDRV4(value, true)
514514
}),
515515

516516
// gendoc:generate(entity=network_ovn, group=common, key=ipv4.dhcp)
@@ -578,7 +578,7 @@ func (n *ovn) Validate(config map[string]string, clientType request.ClientType)
578578
return nil
579579
}
580580

581-
return validate.IsNetworkAddressCIDRV6(value)
581+
return validate.IsNetworkAddressCIDRV6(value, true)
582582
}),
583583

584584
// gendoc:generate(entity=network_ovn, group=common, key=ipv6.dhcp)

internal/server/network/driver_physical.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ func (n *physical) Validate(config map[string]string, clientType request.ClientT
9595
// type: string
9696
// condition: standard mode
9797
// shortdesc: IPv4 address for the gateway and network (CIDR)
98-
"ipv4.gateway": validate.Optional(validate.IsNetworkAddressCIDRV4),
98+
"ipv4.gateway": validate.Optional(func(value string) error { return validate.IsNetworkAddressCIDRV4(value, false) }),
9999

100100
// gendoc:generate(entity=network_physical, group=ipv6, key=ipv6.gateway)
101101
//
102102
// ---
103103
// type: string
104104
// condition: standard mode
105105
// shortdesc: IPv6 address for the gateway and network (CIDR)
106-
"ipv6.gateway": validate.Optional(validate.IsNetworkAddressCIDRV6),
106+
"ipv6.gateway": validate.Optional(func(value string) error { return validate.IsNetworkAddressCIDRV6(value, false) }),
107107

108108
// gendoc:generate(entity=network_physical, group=ipv4, key=ipv4.gateway.hwaddr)
109109
//

shared/validate/validate.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ func IsNetworkAddressV4(value string) error {
367367
}
368368

369369
// IsNetworkAddressCIDRV4 validates an IPv4 address string in CIDR format.
370-
func IsNetworkAddressCIDRV4(value string) error {
370+
func IsNetworkAddressCIDRV4(value string, allowSingle bool) error {
371371
ip, subnet, err := net.ParseCIDR(value)
372372
if err != nil {
373373
return err
@@ -377,6 +377,12 @@ func IsNetworkAddressCIDRV4(value string) error {
377377
return fmt.Errorf("Not an IPv4 address %q", value)
378378
}
379379

380+
subnetSize, _ := subnet.Mask.Size()
381+
if allowSingle && subnetSize == 32 {
382+
// Single addresses are allowed through.
383+
return nil
384+
}
385+
380386
if ip.String() == subnet.IP.String() {
381387
return fmt.Errorf("Not a usable IPv4 address %q", value)
382388
}
@@ -430,7 +436,7 @@ func IsNetworkAddressV6(value string) error {
430436
}
431437

432438
// IsNetworkAddressCIDRV6 validates an IPv6 address string in CIDR format.
433-
func IsNetworkAddressCIDRV6(value string) error {
439+
func IsNetworkAddressCIDRV6(value string, allowSingle bool) error {
434440
ip, subnet, err := net.ParseCIDR(value)
435441
if err != nil {
436442
return err
@@ -440,6 +446,12 @@ func IsNetworkAddressCIDRV6(value string) error {
440446
return fmt.Errorf("Not an IPv6 address %q", value)
441447
}
442448

449+
subnetSize, _ := subnet.Mask.Size()
450+
if allowSingle && subnetSize == 128 {
451+
// Single addresses are allowed through.
452+
return nil
453+
}
454+
443455
if ip.String() == subnet.IP.String() {
444456
return fmt.Errorf("Not a usable IPv6 address %q", value)
445457
}

0 commit comments

Comments
 (0)