From: "d12fk (Code Review)" Date: Tue, 18 Mar 2025 05:30:38 +0000 In-Reply-To: References: Reply-To: heiko@...515..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="jEjmESKsRB4="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: dns: clean up --dhcp-options when --dns is active List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --jEjmESKsRB4= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: d12fk, flichtenheld, plaisthos. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/904?usp=3Demail to look at the new patch set (#6). Change subject: dns: clean up --dhcp-options when --dns is active ...................................................................... dns: clean up --dhcp-options when --dns is active Since --dns settings overrule DNS related --dhcp-options, remove the latter when we got some via --dns. To stay as backward compatible as possible, the --dns server addresses and search domains are added as foreign_options env vars, so that an existing --up script can use them to set up DNS as before, without the immediate need to change after an openvpn upgrade. Same goes for the DNS information in struct tuntap_options, which is used by Android. In case an --up script is defined, the --dns-updown is not run to prevent potential double DNS configuration. Change-Id: I635c4018fb43b5976a39b6a90cb2e9cb2570cd6a Signed-off-by: Heiko Hund --- M src/openvpn/dns.c M src/openvpn/options.c 2 files changed, 155 insertions(+), 1 deletion(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/04/904/6 diff --git a/src/openvpn/dns.c b/src/openvpn/dns.c index 0ad8e44..6a5999a 100644 --- a/src/openvpn/dns.c +++ b/src/openvpn/dns.c @@ -703,7 +703,7 @@ static void run_up_down_command(bool up, struct options *o, const struct tuntap *tt, s= truct dns_updown_runner_info *updown_runner) { - if (!o->dns_options.updown) + if (!o->dns_options.updown || o->up_script) { return; } diff --git a/src/openvpn/options.c b/src/openvpn/options.c index b63f929..966230a 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -4163,6 +4163,160 @@ if (success) { dns_options_postprocess_pull(&o->dns_options); + if (!o->dns_options.servers) + { + return true; + } + +#if defined(_WIN32) || defined(TARGET_ANDROID) + /* Remove DNS related --dhcp-options */ + struct tuntap_options *tt =3D &o->tuntap_options; + tt->dns_len =3D 0; + tt->dns6_len =3D 0; + tt->domain =3D NULL; + tt->domain_search_list_len =3D 0; + +#if defined(TARGET_ANDROID) + /* Set options from --dns config */ + const struct dns_domain *d =3D o->dns_options.search_domains; + while (d && tt->domain_search_list_len + 1 < N_SEARCH_LIST_LEN) + { + tt->domain_search_list[tt->domain_search_list_len++] =3D d->na= me; + d =3D d->next; + } + + const struct dns_server *s =3D o->dns_options.servers; + while (s) + { + bool non_standard_server_port =3D false; + for (int i =3D 0; i < s->addr_count; ++i) + { + if (s->addr[i].port && s->addr[i].port !=3D 53) + { + non_standard_server_port =3D true; + break; + } + } + if ((s->transport && s->transport !=3D DNS_TRANSPORT_PLAIN) + || (s->dnssec && s->dnssec !=3D DNS_SECURITY_NO) + || non_standard_server_port) + { + /* Skip servers requiring unsupported config to be set */ + s =3D s->next; + } + else + { + for (int i =3D 0; i < s->addr_count; ++i) + { + if (s->addr[i].family =3D=3D AF_INET && tt->dns_len + = 1 < N_DHCP_ADDR) + { + tt->dns[tt->dns_len++] =3D s->addr[i].in.a4.s_addr; + } + else if (tt->dns6_len + 1 < N_DHCP_ADDR) + { + tt->dns6[tt->dns6_len] =3D s->addr[i].in.a6; + } + } + break; + } + } +#endif /* if defined(TARGET_ANDROID) */ + +#else /* if defined(_WIN32) || defined(TARGET_ANDROID) */ + + /* Clean up env from --dhcp-option DNS config */ + struct gc_arena gc =3D gc_new(); + struct buffer name =3D alloc_buf_gc(OPTION_PARM_SIZE, &gc); + struct buffer value =3D alloc_buf_gc(OPTION_PARM_SIZE, &gc); + + const int fo_count =3D o->foreign_option_index; + o->foreign_option_index =3D 0; + + for (int i =3D 1; i <=3D fo_count; ++i) + { + buf_clear(&name); + buf_printf(&name, "foreign_option_%d", i); + const char *env_str =3D env_set_get(es, BSTR(&name)); + const char *item_val =3D strchr(env_str, '=3D') + 1; + buf_clear(&value); + buf_printf(&value, "%s", item_val); + + /* Remove foreign option item from env set */ + env_set_del(es, BSTR(&name)); + + item_val =3D BSTR(&value); + if (strncmp(item_val, "dhcp-option ", 12) !=3D 0 + || (strncmp(item_val + 12, "ADAPTER-DOMAIN-SUFFIX ", 22) != =3D 0 + && strncmp(item_val + 12, "DOMAIN-SEARCH ", 14) !=3D 0 + && strncmp(item_val + 12, "DOMAIN ", 7) !=3D 0 + && strncmp(item_val + 12, "DNS6 ", 5) !=3D 0 + && strncmp(item_val + 12, "DNS ", 4) !=3D 0)) + { + /* Re-set the item with potentially updated name */ + buf_clear(&name); + buf_printf(&name, "foreign_option_%d", ++o->foreign_option= _index); + setenv_str(es, BSTR(&name), BSTR(&value)); + } + } + + /* Set foreign option env vars from --dns config */ + if (!o->up_script) + { + /* No need to, when there is no --up script */ + return true; + } + const char *p[] =3D { "dhcp-option", NULL, NULL }; + size_t p_len =3D sizeof(p) / sizeof(p[0]); + + p[1] =3D "DOMAIN"; + const struct dns_domain *d =3D o->dns_options.search_domains; + while (d) + { + p[2] =3D d->name; + setenv_foreign_option(o, (const char **)p, p_len, es); + d =3D d->next; + } + + const struct dns_server *s =3D o->dns_options.servers; + while (s) + { + bool non_standard_server_port =3D false; + for (int i =3D 0; i < s->addr_count; ++i) + { + if (s->addr[i].port && s->addr[i].port !=3D 53) + { + non_standard_server_port =3D true; + break; + } + } + if ((s->transport && s->transport !=3D DNS_TRANSPORT_PLAIN) + || (s->dnssec && s->dnssec !=3D DNS_SECURITY_NO) + || non_standard_server_port) + { + /* Skip servers requiring unsupported config to be set */ + s =3D s->next; + } + else + { + for (int i =3D 0; i < s->addr_count; ++i) + { + if (s->addr[i].family =3D=3D AF_INET) + { + p[1] =3D "DNS"; + p[2] =3D print_in_addr_t(s->addr[i].in.a4.s_addr, = IA_NET_ORDER, &gc); + } + else + { + p[1] =3D "DNS6"; + p[2] =3D print_in6_addr(s->addr[i].in.a6, 0, &gc); + } + setenv_foreign_option(o, (const char **)p, p_len, es); + } + break; + } + } + gc_free(&gc); +#endif /* defined(_WIN32) || defined(TARGET_ANDROID) */ } return success; } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/904?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I635c4018fb43b5976a39b6a90cb2e9cb2570cd6a Gerrit-Change-Number: 904 Gerrit-PatchSet: 6 Gerrit-Owner: d12fk Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-Attention: d12fk Gerrit-MessageType: newpatchset --jEjmESKsRB4= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: d12fk, flichtenheld, plaisthos. <= /p>

d12fk uploaded patch set #6 to this change.

View Change

git pull ssh:=
//gerrit.openvpn.net:29418/openvpn refs/changes/04/904/6
diff --git=
 a/src/openvpn/dns.c b/src/openvpn/dns.c
index 0ad8e44..6a5= 999a 100644
--- a/src/openvpn/dns.c
+++ b/s= rc/openvpn/dns.c
@@ -703,7 +703,7 @@
stati= c void
run_up_down_command(bool up, struct options *o, con= st struct tuntap *tt, struct dns_updown_runner_info *updown_runner)<= br> {
- if (!o= ->dns_options.updown)
+ if (!o->dns_options.updown || o->up_script)
= {
return;
}
diff --git a/src/openvpn/options.c b/src/openvpn/options.c
index b63f929..966230a 100644
--- a/src/openvpn/options= .c
+++ b/src/openvpn/options.c
@@ -4163,6 += 4163,160 @@
if (success)
{=
dns_options_postprocess_pull(&o->dns_options);

+ if (!o->dns= _options.servers)
+ = {
+ = return true;
+ = }
+
+#if defined(_WIN32) || defined(TARGE= T_ANDROID)
+ /= * Remove DNS related --dhcp-options */
+ struct tuntap_options *tt =3D &o->tuntap_= options;
+ tt-= >dns_len =3D 0;
+ = tt->dns6_len =3D 0;
+ tt->domain =3D NULL;
+ tt->domain_search_list_len =3D 0;+
+#if defined(TARGET_ANDROID)
+ /* Set options from --dns confi= g */
+ const s= truct dns_domain *d =3D o->dns_options.search_domains;
+ while (d && tt->do= main_search_list_len + 1 < N_SEARCH_LIST_LEN)
+ {
+ tt->domain_search_list[tt->domain_sear= ch_list_len++] =3D d->name;
+ d =3D d->next;
+ }
+
+ = const struct dns_server *s =3D o->dns_options.servers;
+ while (s)
+ {
+ bool non_standard_server_port =3D fal= se;
+ for = (int i =3D 0; i < s->addr_count; ++i)
+ {
+ if (s->addr[i].port && s->a= ddr[i].port !=3D 53)
= + {
+ = non_standard_server_port =3D true;
+ break;
+ }
+ }
+ if ((s->transport &&am= p; s->transport !=3D DNS_TRANSPORT_PLAIN)
+ || (s->dnssec && s->= dnssec !=3D DNS_SECURITY_NO)
+ || non_standard_server_port)
+ {
+ /* Skip servers requiring unsup= ported config to be set */
+ s =3D s->next;
+ }
+ else
+ {
+ for (int i =3D 0; i < s->addr_count; ++i)
+ {<= br>+ if (s-&= gt;addr[i].family =3D=3D AF_INET && tt->dns_len + 1 < N_DHCP_= ADDR)
+ = {
+ = tt->dns[tt->dns_len++] =3D s->addr[i].in.a4.s_addr;

+ }<= /span>
+ = else if (tt->dns6_len + 1 < N_DHCP_ADDR)
+ {
+ tt->dns6[tt->dns= 6_len] =3D s->addr[i].in.a6;
+ }
+ }
+ break;
+ }
+ }
+#= endif /* if defined(TARGET_ANDROID) */
+
+= #else /* if defined(_WIN32) || defined(TARGET_ANDROID) */
+
+ /* Clean up env from --dhcp-option DNS config *= /
+ struct gc_= arena gc =3D gc_new();
+ struct buffe= r value =3D alloc_buf_gc(OPTION_PARM_SIZE, &gc);
+
+ const int fo_count =3D o->foreign_option_index;
+ o->foreign= _option_index =3D 0;
= +
+ for (int i= =3D 1; i <=3D fo_count; ++i)
+ {
+ buf_printf(&name, "foreign_option_%d&= quot;, i);
+ = const char *env_str =3D env_set_get(es, BSTR(&name));
+ const char *item_val = =3D strchr(env_str, '=3D') + 1;

+ buf_clear(&value);
+ buf_printf(&value, "= ;%s", item_val);
+
+ /* Re= move foreign option item from env set */
+ env_set_del(es, BSTR(&name));+
+ item_val =3D BSTR(&value);
+ if (strncmp(= item_val, "dhcp-option ", 12) !=3D 0
+ || (strncmp(item_val + 12, "= ;ADAPTER-DOMAIN-SUFFIX ", 22) !=3D 0
+ && strncmp(item_val + 12,= "DOMAIN-SEARCH ", 14) !=3D 0
+ && strncmp(item_val + 12, &= quot;DOMAIN ", 7) !=3D 0
+ && strncmp(item_val + 12, "DNS6 = ", 5) !=3D 0
+ = && strncmp(item_val + 12, "DNS ", 4) != =3D 0))
+ = {
+ /*= Re-set the item with potentially updated name */
+ buf_clear(&name);<= br>+ buf_printf(= &name, "foreign_option_%d", ++o->foreign_option_index);
+ setenv= _str(es, BSTR(&name), BSTR(&value));
+ }
+ }
+
+ /*= Set foreign option env vars from --dns config */
+ if (!o->up_script)
+ {

+ /* No need to, when there is no -= -up script */
+ = return true;
+ = }
+ con= st char *p[] =3D { "dhcp-option", NULL, NULL };
+ size_t p_len =3D sizeof(p) / = sizeof(p[0]);
+
+ p[1] =3D "DO= MAIN";
+ = const struct dns_domain *d =3D o->dns_options.search_domains;
= + while (d)
+ {

+ p[2] =3D d->name;+ setenv_foreign_o= ption(o, (const char **)p, p_len, es);
+ d =3D d->next;
+ }
+
+= const struct dns_server *s =3D o->dns_options.servers;+ while (s)
<= span style=3D"color: hsl(120, 100%, 40%);">+ {

+ bool non_standard_server_port= =3D false;
+ = for (int i =3D 0; i < s->addr_count; ++i)
+ {
+ if (s->addr[i].port &&am= p; s->addr[i].port !=3D 53)
+ {
+ non_standard_server_port =3D true;
<= span style=3D"color: hsl(120, 100%, 40%);">+ break;
+ }=
+ }
+ if ((s->transport= && s->transport !=3D DNS_TRANSPORT_PLAIN)

+ || (s->dnssec &&am= p; s->dnssec !=3D DNS_SECURITY_NO)
+ || non_standard_server_port)
+ {
+ /* Skip servers requir= ing unsupported config to be set */
+ s =3D s->next;
+ }
+ else
+ {
+ for (int i =3D 0; i < s->addr_count; += +i)
+ = {
+ = if (s->addr[i].family =3D=3D AF_INET)
+ {
+ p[1] =3D "DNS";<= /span>
+ = p[2] =3D print_in_addr_t(s->addr[i].in.a4.s_addr, IA_NET_ORDER, &= ;gc);
+ = }
+ = else
+ = {
+ = p[1] =3D "DNS6";
+ p[2] =3D print_in6_addr(s= ->addr[i].in.a6, 0, &gc);
+ }
+ setenv_foreign_option(o, (const char **)= p, p_len, es);
+ = }
+ = break;
+ = }
+ }<= /span>
+ gc_free(&= ;gc);
+#endif /* defi= ned(_WIN32) || defined(TARGET_ANDROID) */
}
= return success;
}

To view, visit change 904. To unsubscribe, or for help writing mail filte= rs, visit settings.

<= div itemscope itemtype=3D"http://schema.org/EmailMessage">
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I635c4018fb43b5976a39b6a90cb= 2e9cb2570cd6a
Gerrit-Change-Number: 904
Gerrit-PatchSet: 6
Gerrit-Owner: d12fk <heiko@...515...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-Attention: d12fk <heiko@...515...>= ;
Gerrit-MessageType: newpatchset
--jEjmESKsRB4=--