From: "its_Giaan (Code Review)" Date: Mon, 30 Dec 2024 13:35:11 +0000 In-Reply-To: References: Reply-To: gianmarco@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296..., a@...2181... Message-ID: <0bccd18d28aedd7e589f9f1ad2f81ded803a9b0c-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="u+GpWyKx0zc="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: allow user to specify 'local' multiple times in config files List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , cron2 , flichtenheld Cc: openvpn-devel --u+GpWyKx0zc= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, flichtenheld, ordex, plaisthos. its_Giaan has uploaded a new patch set (#10) to the change originally creat= ed by ordex. ( http://gerrit.openvpn.net/c/openvpn/+/436?usp=3Demail ) Change subject: allow user to specify 'local' multiple times in config files ...................................................................... allow user to specify 'local' multiple times in config files It is now possible to specify 'local' multiple times in a server config to let it listen on multiple sockets (address:port) of the same protocol. Change-Id: I4d1c96662c5a8c750d883e3b20adde09529e2764 Signed-off-by: Antonio Quartulli Signed-off-by: Gianmarco De Gregori --- M doc/man-sections/link-options.rst M src/openvpn/init.c M src/openvpn/options.c M src/openvpn/options.h M src/openvpn/socket.c 5 files changed, 183 insertions(+), 45 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/36/436/10 diff --git a/doc/man-sections/link-options.rst b/doc/man-sections/link-opti= ons.rst index ca192c3..8eb2b65 100644 --- a/doc/man-sections/link-options.rst +++ b/doc/man-sections/link-options.rst @@ -106,13 +106,25 @@ is not reliable. It is recommended to set tun-mtu with enough headroom instead. ---local host - Local host name or IP address for bind. If specified, OpenVPN will bind - to this address only. If unspecified, OpenVPN will bind to all - interfaces. +--local host|* [port] + Local host name or IP address and port for bind. If specified, OpenVPN w= ill bind + to this address. If unspecified, OpenVPN will bind to all interfaces. + '*' can be used as hostname and means 'any host' (OpenVPN will listen on= what + is returned by the OS). + On a client, or in point-to-point mode, this can only be specified once = (1 socket). + On an OpenVPN setup running as ``--server``, this can be specified multi= ple times + to open multiple listening sockets on different addresses and/or differe= nt ports. + In order to specify multiple listen ports without specifying an address,= use '*' + to signal "use what the operating system gives you as default", for + "all IPv4 addresses" use "0.0.0.0", for "all IPv6 addresses" use '::'. + ``--local`` implies ``--bind``. + + + Implies --bind, 0.0.0.0 or :: can be used to specifically + open a socket. --lport port - Set local TCP/UDP port number or name. Cannot be used together with + Set default TCP/UDP port number. Cannot be used together with ``--nobind`` option. --mark value diff --git a/src/openvpn/init.c b/src/openvpn/init.c index 850cd12..86378f7 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -751,7 +751,7 @@ init_connection_list(c); - c->c1.link_sockets_num =3D 1; + c->c1.link_sockets_num =3D c->options.ce.local_list->len; do_link_socket_addr_new(c); @@ -4981,6 +4981,7 @@ if (dest->mode =3D=3D CM_CHILD_UDP) { ASSERT(!dest->c2.link_sockets); + ASSERT(dest->options.ce.local_list); /* inherit buffers */ dest->c2.buffers =3D src->c2.buffers; diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 20e8d55..09d23d7 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -124,7 +124,17 @@ "--version : Show copyright and version information.\n" "\n" "Tunnel Options:\n" - "--local host : Local host name or ip address. Implies --bind.\n" + "--local host|* [port]: Local host name or IP address and port for bin= d.\n" + " If specified, OpenVPN will bindto this addres= s. If unspecified,\n" + " OpenVPN will bind to all interfaces. '*' can = be used as hostname\n" + " and means 'any host' (OpenVPN will listen on = what is returned by the OS).\n" + " On a client, or in point-to-point mode, this = can only be specified once (1 socket).\n" + " On an OpenVPN setup running as ``--server``, = this can be specified multiple times\n" + " to open multiple listening sockets on differe= nt addresses and/or different ports.\n" + " In order to specify multiple listen ports wit= hout specifying an address, use '*'\n" + " to signal 'use what the operating system give= s you as default', for\n" + " 'all IPv4 addresses' use '0.0.0.0', for 'all = IPv6 addresses' use '::'.\n" + " ``--local`` implies ``--bind``.\n" "--remote host [port] : Remote host name or ip address.\n" "--remote-random : If multiple --remote options specified, choose one = randomly.\n" "--remote-random-hostname : Add a random string to remote DNS name.\n" @@ -988,8 +998,9 @@ const int i) { setenv_str_i(es, "proto", proto2ascii(e->proto, e->af, false), i); - setenv_str_i(es, "local", e->local, i); - setenv_str_i(es, "local_port", e->local_port, i); + /* expected to be for single socket contexts only */ + setenv_str_i(es, "local", e->local_list->array[0]->local, i); + setenv_str_i(es, "local_port", e->local_list->array[0]->port, i); setenv_str_i(es, "remote", e->remote, i); setenv_str_i(es, "remote_port", e->remote_port, i); @@ -1713,8 +1724,12 @@ show_connection_entry(const struct connection_entry *o) { msg(D_SHOW_PARMS, " proto =3D %s", proto2ascii(o->proto, o->af, false= )); - SHOW_STR(local); - SHOW_STR(local_port); + msg(D_SHOW_PARMS, " Local Sockets:"); + for (int i =3D 0; i < o->local_list->len; i++) + { + msg(D_SHOW_PARMS, " [%s]:%s", o->local_list->array[i]->local, + o->local_list->array[i]->port); + } SHOW_STR(remote); SHOW_STR(remote_port); SHOW_BOOL(remote_float); @@ -2162,6 +2177,37 @@ #endif /* ifdef ENABLE_MANAGEMENT */ +static struct local_list * +alloc_local_list_if_undef(struct connection_entry *ce, struct gc_arena *gc) +{ + if (!ce->local_list) + { + ALLOC_OBJ_CLEAR_GC(ce->local_list, struct local_list, gc); + } + return ce->local_list; +} + +static struct local_entry * +alloc_local_entry(struct connection_entry *ce, const int msglevel, + struct gc_arena *gc) +{ + struct local_list *l =3D alloc_local_list_if_undef(ce, gc); + struct local_entry *e; + + if (l->len >=3D CONNECTION_LIST_SIZE) + { + msg(msglevel, "Maximum number of 'local' options (%d) exceeded", + CONNECTION_LIST_SIZE); + + return NULL; + } + + ALLOC_OBJ_CLEAR_GC(e, struct local_entry, gc); + l->array[l->len++] =3D e; + + return e; +} + static struct connection_list * alloc_connection_list_if_undef(struct options *options) { @@ -2354,6 +2400,15 @@ "--proto tcp-server or --proto tcp-client"); } + /* + * Sanity check on Client mode + */ + + if (options->mode !=3D MODE_SERVER && ce->local_list->len > 1) + { + msg(M_USAGE, "multiple --local statements only allowed in --server= mode"); + } + if (options->lladdr && dev !=3D DEV_TYPE_TAP) { msg(M_USAGE, "--lladdr can only be used in --dev tap mode"); @@ -2379,13 +2434,6 @@ * Sanity check on --local, --remote, and --ifconfig */ - if (proto_is_net(ce->proto) - && string_defined_equal(ce->local, ce->remote) - && string_defined_equal(ce->local_port, ce->remote_port)) - { - msg(M_USAGE, "--remote and --local addresses are the same"); - } - if (string_defined_equal(ce->remote, options->ifconfig_local) || string_defined_equal(ce->remote, options->ifconfig_remote_netma= sk)) { @@ -2394,13 +2442,6 @@ "addresses"); } - if (string_defined_equal(ce->local, options->ifconfig_local) - || string_defined_equal(ce->local, options->ifconfig_remote_netmas= k)) - { - msg(M_USAGE, - "--local addresses must be distinct from --ifconfig addresses"= ); - } - if (string_defined_equal(options->ifconfig_local, options->ifconfig_remote_netmask)) { @@ -2413,12 +2454,6 @@ msg(M_USAGE, "--bind and --nobind can't be used together"); } - if (ce->local && !ce->bind_local) - { - msg(M_USAGE, - "--local and --nobind don't make sense when used together"); - } - if (ce->local_port_defined && !ce->bind_local) { msg(M_USAGE, @@ -2430,6 +2465,29 @@ msg(M_USAGE, "--nobind doesn't make sense unless used with --remot= e"); } + for (int i =3D 0; i < ce->local_list->len; i++) + { + struct local_entry *le =3D ce->local_list->array[i]; + + if (proto_is_net(ce->proto) + && string_defined_equal(le->local, ce->remote) + && string_defined_equal(le->port, ce->remote_port)) + { + msg(M_USAGE, "--remote and a --local addresses are the same"); + } + + if (string_defined_equal(le->local, options->ifconfig_local) + || string_defined_equal(le->local, options->ifconfig_remote_ne= tmask)) + { + msg(M_USAGE, "--local addresses must be distinct from --ifconf= ig addresses"); + } + + if (le->local && !ce->bind_local) + { + msg(M_USAGE, "--local and --nobind don't make sense when used = together"); + } + } + /* * Check for consistency of management options */ @@ -3128,7 +3186,7 @@ } /* an option is present that requires local bind to enabled */ - bool need_bind =3D ce->local || ce->local_port_defined || ce->bind_def= ined; + bool need_bind =3D ce->local_port_defined || ce->bind_defined || ce->l= ocal_list; /* socks proxy is enabled */ bool uses_socks =3D ce->proto =3D=3D PROTO_UDP && ce->socks_proxy_serv= er; @@ -3264,6 +3322,16 @@ } } +static void +options_postprocess_mutate_le(struct connection_entry ce, struct local_ent= ry *le) +{ + /* use the global port if none is specified */ + if (!le->port) + { + le->port =3D ce.local_port; + } +} + #ifdef _WIN32 /* If iservice is in use, we need def1 method for redirect-gateway */ static void @@ -3738,6 +3806,28 @@ options_postprocess_mutate_ce(o, o->connection_list->array[i]); } + if (o->ce.local_list) + { + for (i =3D 0; i < o->ce.local_list->len; i++) + { + options_postprocess_mutate_le(o->ce, o->ce.local_list->array[i= ]); + } + } + else + { + /* if no 'local' directive was specified, convert the global port + * setting to a listen entry */ + struct local_entry *e =3D alloc_local_entry(&o->ce, M_USAGE, &o->g= c); + ASSERT(e); + e->port =3D o->ce.local_port; + } + + /* use the same listen list for every outgoing connection */ + for (i =3D 0; i < o->connection_list->len; ++i) + { + o->connection_list->array[i]->local_list =3D o->ce.local_list; + } + if (o->tls_server) { /* Check that DH file is specified, or explicitly disabled */ @@ -6133,10 +6223,27 @@ VERIFY_PERMISSION(OPT_P_UP); options->ifconfig_nowarn =3D true; } - else if (streq(p[0], "local") && p[1] && !p[2]) + else if (streq(p[0], "local") && p[1] && !p[3]) { + struct local_entry *e; + VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION); - options->ce.local =3D p[1]; + + e =3D alloc_local_entry(&options->ce, M_USAGE, &options->gc); + ASSERT(e); + + /* '*' is treated as 'ask the system to get some socket', + * therefore force binding on a particular address only when + * actually specified. */ + if (strcmp(p[1], "*") !=3D 0) + { + e->local =3D p[1]; + } + + if (p[2]) + { + e->port =3D p[2]; + } } else if (streq(p[0], "remote-random") && !p[1]) { diff --git a/src/openvpn/options.h b/src/openvpn/options.h index 6ab92e2..008f7cf 100644 --- a/src/openvpn/options.h +++ b/src/openvpn/options.h @@ -94,14 +94,20 @@ #error "At least one of OpenSSL or mbed TLS needs to be defined." #endif +struct local_entry +{ + const char *local; + const char *port; +}; + struct connection_entry { + struct local_list *local_list; int proto; sa_family_t af; const char *local_port; bool local_port_defined; const char *remote_port; - const char *local; const char *remote; bool remote_float; bool bind_defined; @@ -179,6 +185,12 @@ #define CONNECTION_LIST_SIZE 64 +struct local_list +{ + int len; + struct local_entry *array[CONNECTION_LIST_SIZE]; +}; + struct connection_list { int capacity; diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 0e9e4f8..6112332 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -342,7 +342,6 @@ void do_preresolve(struct context *c) { - int i; struct connection_list *l =3D c->options.connection_list; const unsigned int preresolve_flags =3D GETADDR_RESOLVE |GETADDR_UPDATE_MANAGEMENT_STATE @@ -350,13 +349,13 @@ |GETADDR_FATAL; - for (i =3D 0; i < l->len; ++i) + for (int i =3D 0; i < l->len; ++i) { int status; const char *remote; int flags =3D preresolve_flags; - struct connection_entry *ce =3D c->options.connection_list->array[= i]; + struct connection_entry *ce =3D l->array[i]; if (proto_is_dgram(ce->proto)) { @@ -416,12 +415,19 @@ } } - if (ce->bind_local) + flags |=3D GETADDR_PASSIVE; + flags &=3D ~GETADDR_RANDOMIZE; + + for (int j =3D 0; j < ce->local_list->len; j++) { - flags |=3D GETADDR_PASSIVE; - flags &=3D ~GETADDR_RANDOMIZE; - status =3D do_preresolve_host(c, ce->local, ce->local_port, - ce->af, flags); + struct local_entry *le =3D ce->local_list->array[j]; + + if (!le->local) + { + continue; + } + + status =3D do_preresolve_host(c, le->local, le->port, ce->af, = flags); if (status !=3D 0) { goto err; @@ -1882,8 +1888,8 @@ const char *remote_host =3D o->ce.remote; const char *remote_port =3D o->ce.remote_port; - sock->local_host =3D o->ce.local; - sock->local_port =3D o->ce.local_port; + sock->local_host =3D o->ce.local_list->array[sock_index]->local; + sock->local_port =3D o->ce.local_list->array[sock_index]->port; sock->remote_host =3D remote_host; sock->remote_port =3D remote_port; sock->dns_cache =3D c->c1.dns_cache; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/436?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: I4d1c96662c5a8c750d883e3b20adde09529e2764 Gerrit-Change-Number: 436 Gerrit-PatchSet: 10 Gerrit-Owner: ordex Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: its_Giaan Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: cron2 Gerrit-Attention: flichtenheld Gerrit-Attention: ordex Gerrit-MessageType: newpatchset --u+GpWyKx0zc= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: cron2, flichtenheld, ordex, plais= thos.

its_Giaan uploaded patch set #10 to the change original= ly created by ordex.

View Change

allow user to specify 'local' mul=
tiple times in config files

It is now possible to specify 'local= ' multiple times in a server
config to let it listen on multiple soc= kets (address:port) of
the same protocol.

Change-Id: I4d1c96662c5= a8c750d883e3b20adde09529e2764
Signed-off-by: Antonio Quartulli <a@...493...= 2181...>
Signed-off-by: Gianmarco De Gregori <gianmarco@...2726...= >
---
M doc/man-sections/link-options.rst
M src/openvpn/init.c<= br>M src/openvpn/options.c
M src/openvpn/options.h
M src/openvpn/sock= et.c
5 files changed, 183 insertions(+), 45 deletions(-)

git p= ull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/36/436/10d= iff --git a/doc/man-sections/link-options.rst b/doc/man-sections/link-optio= ns.rst
index ca192c3..8eb2b65 100644
--- a/= doc/man-sections/link-options.rst
+++ b/doc/man-sections/li= nk-options.rst
@@ -106,13 +106,25 @@
is = not reliable. It is recommended to set tun-mtu with enough headroom<= br> instead.

---local host=
- Local host name or IP addr= ess for bind. If specified, OpenVPN will bind
- to this address only. If unspecified, OpenVPN wil= l bind to all
- interf= aces.
+--local host|*= [port]
+ Local host= name or IP address and port for bind. If specified, OpenVPN will bind
+ to this address. If un= specified, OpenVPN will bind to all interfaces.
+ '*' can be used as hostname and means= 'any host' (OpenVPN will listen on what
+ is returned by the OS).
+ On a client, or in point-to-point mod= e, this can only be specified once (1 socket).
+ On an OpenVPN setup running as ``--server``, t= his can be specified multiple times
+ to open multiple listening sockets on different addresses= and/or different ports.
+ In order to specify multiple listen ports without specifying an addr= ess, use '*'
= + to signal "use what the operating system gives you as default"= , for
+ "all IP= v4 addresses" use "0.0.0.0", for "all IPv6 addresses&qu= ot; use '::'.
+ ``--local`` implies ``--bind``.
+
+<= /span>
+ Implies --bind, 0.= 0.0.0 or :: can be used to specifically
+ open a socket.

--l= port port
- Set local = TCP/UDP port number or name. Cannot be used together with
+ Set default TCP/UDP port number. Ca= nnot be used together with
``--nobind`` option.
--mark value
diff --git a/src/op= envpn/init.c b/src/openvpn/init.c
index 850cd12..86378f7 10= 0644
--- a/src/openvpn/init.c
+++ b/src/ope= nvpn/init.c
@@ -751,7 +751,7 @@
init_connection_list(c);

- c->c1.link_sockets_num =3D 1;
+ c->c1.link_sockets= _num =3D c->options.ce.local_list->len;

<= span> do_link_socket_addr_new(c);


@@ = -4981,6 +4981,7 @@
if (dest->mode =3D=3D CM_CHILD_U= DP)
{
ASSERT(!dest->c2.lin= k_sockets);
+ = ASSERT(dest->options.ce.local_list);

= /* inherit buffers */
dest->c2.buffers = =3D src->c2.buffers;
diff --git a/src/openvpn/options.c = b/src/openvpn/options.c
index 20e8d55..09d23d7 100644
--- a/src/openvpn/options.c
+++ b/src/openvpn/op= tions.c
@@ -124,7 +124,17 @@
"--v= ersion : Show copyright and version information.\n"
"\n"

"Tunnel Options:\n"<= /span>
- "--local host= : Local host name or ip address. Implies --bind.\n"
+ "--local host|* [port]: L= ocal host name or IP address and port for bind.\n"

+ " If sp= ecified, OpenVPN will bindto this address. If unspecified,\n"+ " = OpenVPN will bind to all interfaces. '*' can be used as hostn= ame\n"
+ &quo= t; and means 'any host' (OpenVPN will listen= on what is returned by the OS).\n"
+ " On a client, or in p= oint-to-point mode, this can only be specified once (1 socket).\n"
+ " = On an OpenVPN setup running as ``--server``, this can be specifi= ed multiple times\n"
+ " to open multiple listening sockets = on different addresses and/or different ports.\n"
+ " In ord= er to specify multiple listen ports without specifying an address, use '= ;*'\n"
+ = " to signal 'use what the operating system = gives you as default', for\n"
+ " 'all IPv4 addresse= s' use '0.0.0.0', for 'all IPv6 addresses' use '::&= #39;.\n"
+ &q= uot; ``--local`` implies ``--bind``.\n"<= br> "--remote host [port] : Remote host name or ip address.\= n"
"--remote-random : If multiple --remote o= ptions specified, choose one randomly.\n"
"-= -remote-random-hostname : Add a random string to remote DNS name.\n"
@@ -988,8 +998,9 @@
= const int i)
{
setenv_str_i(es, &quo= t;proto", proto2ascii(e->proto, e->af, false), i);
- setenv_str_i(es, "local&qu= ot;, e->local, i);

-= setenv_str_i(es, "local_port", e->local_port, i);+ /* expected to be for si= ngle socket contexts only */
+ setenv_str_i(es, "local", e->local_list->array= [0]->local, i);
+ = setenv_str_i(es, "local_port", e->local_list->array[0]-&= gt;port, i);
setenv_str_i(es, "remote", e-&g= t;remote, i);
setenv_str_i(es, "remote_port"= , e->remote_port, i);

@@ -1713,8 +1724= ,12 @@
show_connection_entry(const struct connection_entry= *o)
{
msg(D_SHOW_PARMS, " prot= o =3D %s", proto2ascii(o->proto, o->af, false));
- SHOW_STR(local);

- SHOW_STR(local_port);
+ msg(D_SHOW_PARMS, " Lo= cal Sockets:");

= + for (int i =3D 0; i < o->local_list->len; i++)
+ {

+ msg(D_SHOW_PARMS, " [%s]:%s"= ;, o->local_list->array[i]->local,
+ o->local_list->array[i]->port)= ;
+ }
SHOW_STR(remote);
SHOW_STR(remote_port);
SHOW_BOOL(remote_float);
@@ -2162,6 +2177,= 37 @@

#endif /* ifdef ENABLE_MANAGEMENT = */

= +static struct local_list *
+alloc_local_list_if_undef(struct connection_entry *ce, struct gc_ar= ena *gc)
+{+ if (!ce->local_list)
+ {
+ ALLOC_OBJ_CLEAR_GC(ce->l= ocal_list, struct local_list, gc);

+ }
= + return ce->local_list;
+}
+=
+static struct local_entry = *
+alloc_local_entry(= struct connection_entry *ce, const int msglevel,
+ struct gc_arena *gc)<= br>+{
+ struct local_list *l =3D alloc_local_list= _if_undef(ce, gc);
+ = struct local_entry *e;
+
+ if (l->= ;len >=3D CONNECTION_LIST_SIZE)
+ {
= + msg(msglevel, "Maximum number of 'local' options (%d)= exceeded",
+ = CONNECTION_LIST_SIZE);
+
+ = return NULL;
+ }<= /span>
+
+ ALLOC_OBJ_CLEAR_GC(e, struct local_= entry, gc);
+ l-&g= t;array[l->len++] =3D e;
+
+ return = e;
+}
+

static struct con= nection_list *
alloc_connection_list_if_undef(struct optio= ns *options)
{
@@ -2354,6 +2400,15 @@
"--proto tcp-server or --proto tcp-client&quo= t;);
}

+ /*
+ * Sanity check on Client mode
+ */
+
+ = if (options->mode !=3D MODE_SERVER && ce->local_list->len= > 1)
+ {
+ msg(M_USAGE, &quo= t;multiple --local statements only allowed in --server mode");<= br>+ }
+
if (options->lla= ddr && dev !=3D DEV_TYPE_TAP)
{
msg(M_USAGE, "--lladdr can only be used in --dev tap mode&q= uot;);

@@ -2379,13 +2434,6 @@
* Sanit= y check on --local, --remote, and --ifconfig
*/

- if (p= roto_is_net(ce->proto)
- && string_defined_equal(ce->local, ce->remote)

- && strin= g_defined_equal(ce->local_port, ce->remote_port))
- {
- msg(M_USAGE, "--remote and --local addresses= are the same");
-= }
-
if (string_defined_equal(ce->remote, options->ifconfig_local)
|| string_defined_equal(ce->remote, options->= ifconfig_remote_netmask))
{
@@ -2394,1= 3 +2442,6 @@
"addresses"); }

- if (string_defined_equal(ce->local, options->ifconfig_l= ocal)
- || strin= g_defined_equal(ce->local, options->ifconfig_remote_netmask))<= br>- {
- msg(M_USAGE,
- "--local addresses must be= distinct from --ifconfig addresses");
- }
-
if (string_defined_equal(options->ifconfig_l= ocal,
options->ifconfig_re= mote_netmask))
{
@@ -2413,12 +2454,6 @= @
msg(M_USAGE, "--bind and --nobind can't= be used together");
}
- if (ce->local &&= ; !ce->bind_local)
-= {
- msg(M_US= AGE,
- "= ;--local and --nobind don't make sense when used together");

- }
-
if (ce->local_port= _defined && !ce->bind_local)
{
msg(M_USAGE,
@@ -2430,6 +2465,29 @@
= msg(M_USAGE, "--nobind doesn't make sense unless us= ed with --remote");
}
+ for (int i =3D 0; i < = ce->local_list->len; i++)
+ {
+ = struct local_entry *le =3D ce->local_list->array[i];
= +
+ if (proto_is_net(ce->proto)
= + && string_= defined_equal(le->local, ce->remote)
+ && string_defined_equal(le->p= ort, ce->remote_port))
+ {
+ = msg(M_USAGE, "--remote and a --local addresses are the same&qu= ot;);
+ }
+
+ if (string_defined_equal(le->l= ocal, options->ifconfig_local)
+ || string_defined_equal(le->local, options->= ;ifconfig_remote_netmask))
+ {
+ = msg(M_USAGE, "--local addresses must be distinct from --ifcon= fig addresses");
+ }
+<= br>+ if (le->local &a= mp;& !ce->bind_local)
+ {
+ = msg(M_USAGE, "--local and --nobind don't make sense whe= n used together");
+ }
+ }<= /span>
+
= /*
* Check for consistency of management options

*/
@@ -3128,7 +3186,7 @@
}

/* an option is present tha= t requires local bind to enabled */
- bool need_bind =3D ce->local || ce->local_port_defi= ned || ce->bind_defined;
+ bool need_bind =3D ce->local_port_defined || ce->bind_def= ined || ce->local_list;

/* socks = proxy is enabled */
bool uses_socks =3D ce->proto = =3D=3D PROTO_UDP && ce->socks_proxy_server;
@@ -= 3264,6 +3322,16 @@
}
}

+static void

+options_postprocess_muta= te_le(struct connection_entry ce, struct local_entry *le)
+{
+ /* use the global port if none is specified */

+ if (!le->port)
+ {
+ le->port =3D ce.local_por= t;
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+}

+
#ifdef _WIN32
= /* If iservice is in use, we need def1 method for redirect-gateway */
static void
@@ -3738,6 +3806,28 @@
options_postprocess_mutate_ce(o, o->connection_list->arr= ay[i]);

}

+ if (o->ce.local_list)
+ {
+ for (i =3D 0; i < o->ce.local_list-&g= t;len; i++)
+ = {
+ option= s_postprocess_mutate_le(o->ce, o->ce.local_list->array[i]);=
+ }
+ }
+ else
+ {
+= /* if no 'local' directive was specified, convert the globa= l port
+ * se= tting to a listen entry */
+ struct local_entry *e =3D alloc_local_entry(&o->ce, M= _USAGE, &o->gc);
+ ASSERT(e);
+ }
+
+ /*= use the same listen list for every outgoing connection */
+ for (i =3D 0; i < o->conne= ction_list->len; ++i)
+ {
+ o= ->connection_list->array[i]->local_list =3D o->ce.local_list;
+ }
+

if (o->tls= _server)
{
/* Check that DH f= ile is specified, or explicitly disabled */
@@ -6133,10 +62= 23,27 @@
VERIFY_PERMISSION(OPT_P_UP);
options->ifconfig_nowarn =3D true;
}
- else if (streq(p[0]= , "local") && p[1] && !p[2])
+ else if (streq(p[0], "local&quo= t;) && p[1] && !p[3])
{
+ struct local_entry *e;
+
= VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
- options->ce.local =3D p[1];

+
+ e =3D alloc_local_entry(&opti= ons->ce, M_USAGE, &options->gc);
+ ASSERT(e);
+
+ *= therefore force binding on a particular address only when
+ * actually specified. */

+ if (strcmp(p[1= ], "*") !=3D 0)
+ {
+ = e->local =3D p[1];
+ }
+=
+ if (p[2])
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ e->port =3D p[2]= ;

+ } }
else if (streq(p[0], "remote-rando= m") && !p[1])
{
diff --gi= t a/src/openvpn/options.h b/src/openvpn/options.h
index 6ab= 92e2..008f7cf 100644
--- a/src/openvpn/options.h
= +++ b/src/openvpn/options.h
@@ -94,14 +94,20 @@
#error "At least one of OpenSSL or mbed TLS needs to be de= fined."
#endif

+struct local_entry
+{
+ const char *local;
+ const char *port;
+};
+
struct connection_entry
{
= + struct local_list *local_l= ist;
int proto;
sa_family_t af;
const char *local_port;
bool local= _port_defined;
const char *remote_port;
- const char *local;

const char *remote;

bool remote_float;<= br> bool bind_defined;
@@ -179,6 +185,12 @@

#define CONNECTION_LIST_SIZE 64

+struct local_li= st
+{
+ int len;
+ struct local_entry *array[CONNECTION_= LIST_SIZE];
+};

+
struct = connection_list
{
int capacity;
diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c<= br>index 0e9e4f8..6112332 100644
--- a/src/openvpn/so= cket.c
+++ b/src/openvpn/socket.c
@@ -342,7= +342,6 @@
void
do_preresolve(struct cont= ext *c)
{
- int i;
struct connection_list *l =3D c->op= tions.connection_list;
const unsigned int preresolve_f= lags =3D GETADDR_RESOLVE
= |GETADDR_UPDATE_MANAGEMENT_STATE
@@ -350,13 +349,1= 3 @@
|GETADDR_FA= TAL;


- for (i =3D 0; i < l->len; ++i)
+ for (int i =3D 0; i < l->= len; ++i)

{
int status;
const char *remote;
int flags = =3D preresolve_flags;

- struct connection_entry *ce =3D c->options.con= nection_list->array[i];
+ struct connection_entry *ce =3D l->array[i];

if (proto_is_dgram(ce->proto))
= {
@@ -416,12 +415,19 @@
= }
}

- if (ce->bind_local)
= + flags |=3D GETADDR_PAS= SIVE;
+ flags = &=3D ~GETADDR_RANDOMIZE;
+
+ fo= r (int j =3D 0; j < ce->local_list->len; j++)
= {
- fla= gs |=3D GETADDR_PASSIVE;
- status =3D do_preresolve_host(c, ce= ->local, ce->local_port,
- ce->af, flags);<= br>+ struct local_en= try *le =3D ce->local_list->array[j];
+
+ if (!le->local)
+ {
+ continue;
+ }
+
+ = status =3D do_preresolve_host(c, le->local, le->port, ce->af, f= lags);
if (status !=3D 0)
= {
goto err;
@@ -1= 882,8 +1888,8 @@
const char *remote_host =3D o->ce.= remote;
const char *remote_port =3D o->ce.remote_po= rt;

-= sock->local_host =3D o->ce.local;
- sock->local_port =3D o->ce.local_port;
+ sock->local_host = =3D o->ce.local_list->array[sock_index]->local;
+ sock->local_port =3D o->ce.l= ocal_list->array[sock_index]->port;
sock->rem= ote_host =3D remote_host;
sock->remote_port =3D rem= ote_port;
sock->dns_cache =3D c->c1.dns_cache;

To view, visit change 436. To unsubscribe, or= for help writing mail filters, visit settings.

<= /div>
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I4d1c96662c5a8c750d883e3b20a= dde09529e2764
Gerrit-Change-Number: 436
Gerrit-PatchSet: 10
Gerrit-Owner: ordex <a@...2181...>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: its_Giaan <gianmarco@...1664...= 26...>
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: cron2 <gert@...1296...>= ;
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-Attention: ordex <a@...2181...> <= /div>
Gerrit-MessageType: newpatchset
--u+GpWyKx0zc=--