allow sending multiple DNS servers to dhcp clients

Signed-off-by: John Crispin <john@phrozen.org>
This commit is contained in:
John Crispin
2024-01-12 09:36:26 +01:00
parent 70bd716d4d
commit ecc742a373
6 changed files with 130 additions and 15 deletions

View File

@@ -1,7 +1,16 @@
{% let name = ethernet.calculate_name(interface) %}
{% let dhcp = ipv4.dhcp || { ignore: 1 } %}
{% let dhcpv6 = ipv6.dhcpv6 || {} %}
{% function use_dns() {
let ret = '';
if (type(dhcp.use_dns) == 'array') {
for (let k, v in dhcp.use_dns)
ret += ',' + v;
} else {
ret += ',' + dhcp.use_dns;
}
return ret;
} %}
set dhcp.{{ name }}=dhcp
set dhcp.{{ name }}.interface={{ s(ethernet.calculate_ipv4_name(interface)) }}
set dhcp.{{ name }}.start={{ dhcp.lease_first }}
@@ -9,7 +18,7 @@ set dhcp.{{ name }}.limit={{ dhcp.lease_count }}
set dhcp.{{ name }}.leasetime={{ dhcp.lease_time }}
set dhcp.{{ name }}.ignore={{ b(dhcp.ignore) }}
{% if (dhcp.use_dns): %}
add_list dhcp.{{ name }}.dhcp_option='6,{{ dhcp.use_dns }}'
add_list dhcp.{{ name }}.dhcp_option='6{{ use_dns() }}'
{% endif %}
{% if (interface.role != 'upstream'): %}
{% if (dhcpv6.mode == 'hybrid'): %}

View File

@@ -23,5 +23,10 @@ properties:
use-dns:
description:
The DNS server sent to clients as DHCP option 6.
type: string
format: uc-ip
anyOf:
- type: string
format: ipv4
- type: array
items:
type: string
format: ipv4

View File

@@ -1472,14 +1472,82 @@ function instantiateInterfaceIpv4Dhcp(location, value, errors) {
}
function parseUseDns(location, value, errors) {
if (type(value) == "string") {
if (!matchUcIp(value))
push(errors, [ location, "must be a valid IPv4 or IPv6 address" ]);
function parseVariant0(location, value, errors) {
if (type(value) == "string") {
if (!matchIpv4(value))
push(errors, [ location, "must be a valid IPv4 address" ]);
}
if (type(value) != "string")
push(errors, [ location, "must be of type string" ]);
return value;
}
if (type(value) != "string")
push(errors, [ location, "must be of type string" ]);
function parseVariant1(location, value, errors) {
if (type(value) == "array") {
function parseItem(location, value, errors) {
if (type(value) == "string") {
if (!matchIpv4(value))
push(errors, [ location, "must be a valid IPv4 address" ]);
}
if (type(value) != "string")
push(errors, [ location, "must be of type string" ]);
return value;
}
return map(value, (item, i) => parseItem(location + "/" + i, item, errors));
}
if (type(value) != "array")
push(errors, [ location, "must be of type array" ]);
return value;
}
let success = 0, tryval, tryerr, vvalue = null, verrors = [];
tryerr = [];
tryval = parseVariant0(location, value, tryerr);
if (!length(tryerr)) {
if (type(vvalue) == "object" && type(tryval) == "object")
vvalue = { ...vvalue, ...tryval };
else
vvalue = tryval;
success++;
}
else {
push(verrors, join(" and\n", map(tryerr, err => "\t - " + err[1])));
}
tryerr = [];
tryval = parseVariant1(location, value, tryerr);
if (!length(tryerr)) {
if (type(vvalue) == "object" && type(tryval) == "object")
vvalue = { ...vvalue, ...tryval };
else
vvalue = tryval;
success++;
}
else {
push(verrors, join(" and\n", map(tryerr, err => "\t - " + err[1])));
}
if (success == 0) {
if (length(verrors))
push(errors, [ location, "must match at least one of the following constraints:\n" + join("\n- or -\n", verrors) ]);
else
push(errors, [ location, "must match only one variant" ]);
return null;
}
value = vvalue;
return value;
}

View File

@@ -1042,8 +1042,19 @@
},
"use-dns": {
"description": "The DNS server sent to clients as DHCP option 6.",
"type": "string",
"format": "uc-ip"
"anyOf": [
{
"type": "string",
"format": "ipv4"
},
{
"type": "array",
"items": {
"type": "string",
"format": "ipv4"
}
}
]
}
}
},

View File

@@ -671,8 +671,19 @@
"default": "6h"
},
"use-dns": {
"type": "string",
"format": "uc-ip"
"anyOf": [
{
"type": "string",
"format": "ipv4"
},
{
"type": "array",
"items": {
"type": "string",
"format": "ipv4"
}
}
]
}
}
},

View File

@@ -753,8 +753,19 @@
},
"use-dns": {
"description": "The DNS server sent to clients as DHCP option 6.",
"type": "string",
"format": "uc-ip"
"anyOf": [
{
"type": "string",
"format": "ipv4"
},
{
"type": "array",
"items": {
"type": "string",
"format": "ipv4"
}
}
]
}
}
},