From: "ralf_lici (Code Review)" Date: Tue, 10 Sep 2024 07:43:38 +0000 In-Reply-To: References: Reply-To: ralf@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <5a0b1d5dba6fb56417372c94075842fe0c698b4f-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="ezz0ITQCaPo="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: Support CIDR on options and extend netbits usage List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --ezz0ITQCaPo= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/739?usp=3Demail to look at the new patch set (#2). Change subject: Support CIDR on options and extend netbits usage ...................................................................... Support CIDR on options and extend netbits usage Add support for CIDR notation on all suitable options (client-nat, ifconfig, ifconfig-pull, ifconfig-push, ifconfig-push-constraint, iroute, route, server, server-bridge). Anyway push of ifconfig is done without CIDR in order to support older peers. Also netmask env vars have been preserved while also introducing the corresponding netbits vars. Some of the internal logic has also been modified to use netbits instead of netmasks. Change-Id: Iae04ad8715e40dfc76475c2c5b9a766c9604efc9 Signed-off-by: Ralf Lici --- M src/openvpn/basic.h M src/openvpn/clinat.c M src/openvpn/clinat.h M src/openvpn/helper.c M src/openvpn/init.c M src/openvpn/manage.c M src/openvpn/multi.c M src/openvpn/options.c M src/openvpn/options.h M src/openvpn/pool.c M src/openvpn/push.c M src/openvpn/route.c M src/openvpn/route.h M src/openvpn/socket.c M src/openvpn/socket.h M src/openvpn/tun.c M src/openvpn/tun.h 17 files changed, 520 insertions(+), 284 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/739/2 diff --git a/src/openvpn/basic.h b/src/openvpn/basic.h index d1075b2..6658008 100644 --- a/src/openvpn/basic.h +++ b/src/openvpn/basic.h @@ -33,5 +33,6 @@ #define CLEAR(x) memset(&(x), 0, sizeof(x)) #define IPV4_NETMASK_HOST 0xffffffffU +#define IPV4_NETBITS_HOST 32U #endif diff --git a/src/openvpn/clinat.c b/src/openvpn/clinat.c index 2d3b359..f565989 100644 --- a/src/openvpn/clinat.c +++ b/src/openvpn/clinat.c @@ -31,6 +31,7 @@ #include "proto.h" #include "socket.h" #include "memdbg.h" +#include "route.h" static bool add_entry(struct client_nat_option_list *dest, @@ -60,11 +61,11 @@ for (i =3D 0; i < list->n; ++i) { const struct client_nat_entry *e =3D &list->entries[i]; - msg(msglevel, " CNAT[%d] t=3D%d %s/%s/%s", + msg(msglevel, " CNAT[%d] t=3D%d %s/%d %s", i, e->type, print_in_addr_t(e->network, IA_NET_ORDER, &gc), - print_in_addr_t(e->netmask, IA_NET_ORDER, &gc), + e->netbits, print_in_addr_t(e->foreign_network, IA_NET_ORDER, &gc)); } } @@ -111,6 +112,7 @@ int msglevel) { struct client_nat_entry e; + unsigned int netbits =3D 0; bool ok; if (!strcmp(type, "snat")) @@ -127,19 +129,34 @@ return; } - e.network =3D getaddr(0, network, 0, &ok, NULL); + e.network =3D getaddr(0, network, &netbits, 0, &ok, NULL); if (!ok) { msg(msglevel, "client-nat: bad network: %s", network); return; } - e.netmask =3D getaddr(0, netmask, 0, &ok, NULL); - if (!ok) + if (netmask) { - msg(msglevel, "client-nat: bad netmask: %s", netmask); + const in_addr_t mask =3D getaddr(0, netmask, NULL, 0, &ok, NULL); + const int bits =3D netmask_to_netbits2(mask); + + if (!ok || bits <=3D 0) + { + msg(msglevel, "client-nat: bad netmask: %s", netmask); + return; + } + e.netbits =3D bits; + } + else if (netbits > 0) + { + e.netbits =3D netbits; + } + else + { + msg(msglevel, "client-nat: netmask or bits must be specified"); return; } - e.foreign_network =3D getaddr(0, foreign_network, 0, &ok, NULL); + e.foreign_network =3D getaddr(0, foreign_network, NULL, 0, &ok, NULL); if (!ok) { msg(msglevel, "client-nat: bad foreign network: %s", foreign_netwo= rk); @@ -210,6 +227,7 @@ for (i =3D 0; i < list->n; ++i) { const struct client_nat_entry *e =3D &list->entries[i]; /* current= NAT rule */ + const in_addr_t netmask =3D netbits_to_netmask((int)e->netbits); if (e->type ^ direction) { addr =3D *(addr_ptr =3D &h->ip.daddr); @@ -231,13 +249,13 @@ to =3D &e->foreign_network; } - if (((addr & e->netmask) =3D=3D *from) && !(amask & alog)) + if (((addr & netmask) =3D=3D *from) && !(amask & alog)) { /* pre-adjust IP checksum */ ADD_CHECKSUM_32(accumulate, addr); /* do NAT transform */ - addr =3D (addr & ~e->netmask) | *to; + addr =3D (addr & ~netmask) | *to; /* post-adjust IP checksum */ SUB_CHECKSUM_32(accumulate, addr); diff --git a/src/openvpn/clinat.h b/src/openvpn/clinat.h index 94141f5..bebd012 100644 --- a/src/openvpn/clinat.h +++ b/src/openvpn/clinat.h @@ -36,7 +36,7 @@ #define CN_DNAT 1 int type; in_addr_t network; - in_addr_t netmask; + unsigned int netbits; in_addr_t foreign_network; }; diff --git a/src/openvpn/helper.c b/src/openvpn/helper.c index bbdbc04..30f3539 100644 --- a/src/openvpn/helper.c +++ b/src/openvpn/helper.c @@ -64,22 +64,21 @@ } static const char * -print_opt_route(const in_addr_t network, const in_addr_t netmask, struct g= c_arena *gc) +print_opt_route(const in_addr_t network, const int netbits, struct gc_aren= a *gc) { struct buffer out =3D alloc_buf_gc(128, gc); ASSERT(network); - if (netmask) - { - buf_printf(&out, "route %s %s", - print_in_addr_t(network, 0, gc), - print_in_addr_t(netmask, 0, gc)); - } - else + if (netbits < 0) { buf_printf(&out, "route %s", print_in_addr_t(network, 0, gc)); } + else + { + buf_printf(&out, "route %s/%d", + print_in_addr_t(network, 0, gc), netbits); + } return BSTR(&out); } @@ -111,27 +110,28 @@ } static void -helper_add_route(const in_addr_t network, const in_addr_t netmask, struct = options *o) +helper_add_route(const in_addr_t network, const int netbits, struct option= s *o) { rol_check_alloc(o); add_route_to_option_list(o->routes, print_in_addr_t(network, 0, &o->gc), - print_in_addr_t(netmask, 0, &o->gc), + print_in_addr_t(netbits_to_netmask(netbits), = 0, &o->gc), NULL, NULL); } static void -verify_common_subnet(const char *opt, const in_addr_t a, const in_addr_t b= , const in_addr_t subnet) +verify_common_subnet(const char *opt, const in_addr_t a, const in_addr_t b= , const unsigned int netbits) { struct gc_arena gc =3D gc_new(); - if ((a & subnet) !=3D (b & subnet)) + const unsigned int var =3D 32 - netbits; + if ((a >> var) !=3D (b >> var)) { - msg(M_USAGE, "%s IP addresses %s and %s are not in the same %s sub= net", + msg(M_USAGE, "%s IP addresses %s and %s are not in the same /%d su= bnet", opt, print_in_addr_t(a, 0, &gc), print_in_addr_t(b, 0, &gc), - print_in_addr_t(subnet, 0, &gc)); + netbits); } gc_free(&gc); } @@ -268,9 +268,6 @@ if (o->server_defined) { - int netbits =3D -2; - bool status =3D false; - if (o->client) { msg(M_USAGE, "--server and --client cannot be used together"); @@ -296,34 +293,24 @@ msg(M_USAGE, "--server directive only makes sense with --dev t= un or --dev tap"); } - status =3D netmask_to_netbits(o->server_network, o->server_netmask= , &netbits); - if (!status) - { - msg(M_USAGE, "--server directive network/netmask combination i= s invalid"); - } - - if (netbits < 0) - { - msg(M_USAGE, "--server directive netmask is invalid"); - } - - if (netbits < IFCONFIG_POOL_MIN_NETBITS) + if (o->server_netbits < IFCONFIG_POOL_MIN_NETBITS) { msg(M_USAGE, "--server directive netmask allows for too many h= ost addresses (subnet must be %s or higher)", print_netmask(IFCONFIG_POOL_MIN_NETBITS, &gc)); } + const in_addr_t server_netmask =3D netbits_to_netmask((int)o->serv= er_netbits); if (dev =3D=3D DEV_TYPE_TUN) { int pool_end_reserve =3D 4; - if (netbits > 29) + if (o->server_netbits > 29) { msg(M_USAGE, "--server directive when used with --dev tun = must define a subnet of %s or lower", print_netmask(29, &gc)); } - if (netbits =3D=3D 29) + if (o->server_netbits =3D=3D 29) { pool_end_reserve =3D 0; } @@ -342,14 +329,14 @@ { o->ifconfig_pool_defined =3D true; o->ifconfig_pool_start =3D o->server_network + 4; - o->ifconfig_pool_end =3D (o->server_network | ~o->serv= er_netmask) - pool_end_reserve; + o->ifconfig_pool_end =3D (o->server_network | ~server_= netmask) - pool_end_reserve; ifconfig_pool_verify_range(M_USAGE, o->ifconfig_pool_s= tart, o->ifconfig_pool_end); } - helper_add_route(o->server_network, o->server_netmask, o); + helper_add_route(o->server_network, o->server_netbits, o); if (o->enable_c2c) { - push_option(o, print_opt_route(o->server_network, o->s= erver_netmask, &o->gc), M_USAGE); + push_option(o, print_opt_route(o->server_network, o->s= erver_netbits, &o->gc), M_USAGE); } else if (o->topology =3D=3D TOP_NET30) { @@ -359,16 +346,16 @@ else if (o->topology =3D=3D TOP_SUBNET) { o->ifconfig_local =3D print_in_addr_t(o->server_network + = 1, 0, &o->gc); - o->ifconfig_remote_netmask =3D print_in_addr_t(o->server_n= etmask, 0, &o->gc); + o->ifconfig_remote_netmask =3D print_in_addr_t(server_netm= ask, 0, &o->gc); if (!(o->server_flags & SF_NOPOOL)) { o->ifconfig_pool_defined =3D true; o->ifconfig_pool_start =3D o->server_network + 2; - o->ifconfig_pool_end =3D (o->server_network | ~o->serv= er_netmask) - 1; + o->ifconfig_pool_end =3D (o->server_network | ~server_= netmask) - 1; ifconfig_pool_verify_range(M_USAGE, o->ifconfig_pool_s= tart, o->ifconfig_pool_end); } - o->ifconfig_pool_netmask =3D o->server_netmask; + o->ifconfig_pool_netbits =3D o->server_netbits; push_option(o, print_opt_route_gateway(o->server_network += 1, &o->gc), M_USAGE); if (!o->route_default_gateway) @@ -393,7 +380,7 @@ } else if (dev =3D=3D DEV_TYPE_TAP) { - if (netbits > 30) + if (o->server_netbits > 30) { msg(M_USAGE, "--server directive when used with --dev tap = must define a subnet of %s or lower", print_netmask(30, &gc)); @@ -402,16 +389,16 @@ o->mode =3D MODE_SERVER; o->tls_server =3D true; o->ifconfig_local =3D print_in_addr_t(o->server_network + 1, 0= , &o->gc); - o->ifconfig_remote_netmask =3D print_in_addr_t(o->server_netma= sk, 0, &o->gc); + o->ifconfig_remote_netmask =3D print_in_addr_t(server_netmask,= 0, &o->gc); if (!(o->server_flags & SF_NOPOOL)) { o->ifconfig_pool_defined =3D true; o->ifconfig_pool_start =3D o->server_network + 2; - o->ifconfig_pool_end =3D (o->server_network | ~o->server_n= etmask) - 1; + o->ifconfig_pool_end =3D (o->server_network | ~server_netm= ask) - 1; ifconfig_pool_verify_range(M_USAGE, o->ifconfig_pool_start= , o->ifconfig_pool_end); } - o->ifconfig_pool_netmask =3D o->server_netmask; + o->ifconfig_pool_netbits =3D o->server_netbits; push_option(o, print_opt_route_gateway(o->server_network + 1, = &o->gc), M_USAGE); } @@ -425,7 +412,7 @@ { o->push_ifconfig_constraint_defined =3D true; o->push_ifconfig_constraint_network =3D o->server_network; - o->push_ifconfig_constraint_netmask =3D o->server_netmask; + o->push_ifconfig_constraint_netbits =3D o->server_netbits; } } @@ -478,9 +465,9 @@ if (o->server_bridge_defined) { - verify_common_subnet("--server-bridge", o->server_bridge_ip, o= ->server_bridge_pool_start, o->server_bridge_netmask); - verify_common_subnet("--server-bridge", o->server_bridge_pool_= start, o->server_bridge_pool_end, o->server_bridge_netmask); - verify_common_subnet("--server-bridge", o->server_bridge_ip, o= ->server_bridge_pool_end, o->server_bridge_netmask); + verify_common_subnet("--server-bridge", o->server_bridge_ip, o= ->server_bridge_pool_start, o->server_bridge_netbits); + verify_common_subnet("--server-bridge", o->server_bridge_pool_= start, o->server_bridge_pool_end, o->server_bridge_netbits); + verify_common_subnet("--server-bridge", o->server_bridge_ip, o= ->server_bridge_pool_end, o->server_bridge_netbits); } o->mode =3D MODE_SERVER; @@ -492,7 +479,7 @@ o->ifconfig_pool_start =3D o->server_bridge_pool_start; o->ifconfig_pool_end =3D o->server_bridge_pool_end; ifconfig_pool_verify_range(M_USAGE, o->ifconfig_pool_start, o-= >ifconfig_pool_end); - o->ifconfig_pool_netmask =3D o->server_bridge_netmask; + o->ifconfig_pool_netbits =3D o->server_bridge_netbits; push_option(o, print_opt_route_gateway(o->server_bridge_ip, &o= ->gc), M_USAGE); } else if (o->server_bridge_proxy_dhcp && !(o->server_flags & SF_NO_= PUSH_ROUTE_GATEWAY)) diff --git a/src/openvpn/init.c b/src/openvpn/init.c index dd56961..7f72f8d 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -115,6 +115,7 @@ const char *dev_type, int tun_mtu, const char *ifconfig_local, + const int ifconfig_netbits, const char *ifconfig_remote, const char *context, const char *signal_text, @@ -144,7 +145,14 @@ } if (!ifconfig_remote) { - ifconfig_remote =3D ""; + if (ifconfig_netbits > 0) + { + ifconfig_remote =3D print_in_addr_t(netbits_to_netmask(ifconfi= g_netbits), 0, &gc); + } + else + { + ifconfig_remote =3D ""; + } } if (!context) { @@ -1948,7 +1956,8 @@ dev_type_string(c->options.dev, c->options.dev_type), c->c2.frame.tun_mtu, print_in_addr_t(c->c1.tuntap->local, IA_EMPTY_IF_UNDEF= , &gc), - print_in_addr_t(c->c1.tuntap->remote_netmask, IA_EMPTY= _IF_UNDEF, &gc), + c->c1.tuntap->netbits, + print_in_addr_t(c->c1.tuntap->remote, IA_EMPTY_IF_UNDE= F, &gc), "init", NULL, "up", @@ -1988,7 +1997,8 @@ dev_type_string(c->options.dev, c->options.dev_typ= e), c->c2.frame.tun_mtu, print_in_addr_t(c->c1.tuntap->local, IA_EMPTY_IF_U= NDEF, &gc), - print_in_addr_t(c->c1.tuntap->remote_netmask, IA_E= MPTY_IF_UNDEF, &gc), + c->c1.tuntap->netbits, + print_in_addr_t(c->c1.tuntap->remote, IA_EMPTY_IF_= UNDEF, &gc), "restart", NULL, "up", @@ -2045,7 +2055,8 @@ struct gc_arena gc =3D gc_new(); const char *tuntap_actual =3D string_alloc(c->c1.tuntap->actual_name, = &gc); const in_addr_t local =3D c->c1.tuntap->local; - const in_addr_t remote_netmask =3D c->c1.tuntap->remote_netmask; + const int netbits =3D c->c1.tuntap->netbits; + const in_addr_t remote =3D c->c1.tuntap->remote; unsigned long adapter_index =3D 0; #ifdef _WIN32 adapter_index =3D c->c1.tuntap->adapter_index; @@ -2077,7 +2088,8 @@ NULL, c->c2.frame.tun_mtu, print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc), - print_in_addr_t(remote_netmask, IA_EMPTY_IF_UNDEF,= &gc), + netbits, + print_in_addr_t(remote, IA_EMPTY_IF_UNDEF, &gc), "init", signal_description(c->sig->signal_received, c->sig->signal_text), @@ -2107,7 +2119,8 @@ NULL, c->c2.frame.tun_mtu, print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc), - print_in_addr_t(remote_netmask, IA_EMPTY_IF_UNDEF, &gc= ), + netbits, + print_in_addr_t(remote, IA_EMPTY_IF_UNDEF, &gc), "init", signal_description(c->sig->signal_received, c->sig->signal_text), @@ -2137,7 +2150,8 @@ NULL, c->c2.frame.tun_mtu, print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc), - print_in_addr_t(remote_netmask, IA_EMPTY_IF_UNDEF,= &gc), + netbits, + print_in_addr_t(remote, IA_EMPTY_IF_UNDEF, &gc), "restart", signal_description(c->sig->signal_received, c->sig->signal_text), diff --git a/src/openvpn/manage.c b/src/openvpn/manage.c index 05b5a1a..a88ee5f 100644 --- a/src/openvpn/manage.c +++ b/src/openvpn/manage.c @@ -554,7 +554,7 @@ { /* IP:port specified */ bool status; - const in_addr_t addr =3D getaddr(GETADDR_HOST_ORDER|GETADDR_MS= G_VIRT_OUT, p1, 0, &status, NULL); + const in_addr_t addr =3D getaddr(GETADDR_HOST_ORDER|GETADDR_MS= G_VIRT_OUT, p1, NULL, 0, &status, NULL); if (status) { const int port =3D atoi(p2); @@ -2803,6 +2803,7 @@ "untrusted_ip=3D", "ifconfig_local=3D", "ifconfig_netmask=3D", + "ifconfig_netbits=3D", "daemon_start_time=3D", "daemon_pid=3D", "dev=3D", diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 0509911..aba1a0f 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -1433,7 +1433,8 @@ const struct options *o =3D &c->options; if (o->push_ifconfig_constraint_defined && c->c2.push_ifconfig_defined) { - return (o->push_ifconfig_constraint_netmask & c->c2.push_ifconfig_= local) =3D=3D o->push_ifconfig_constraint_network; + const unsigned int var =3D 32 - o->push_ifconfig_constraint_netbit= s; + return (c->c2.push_ifconfig_local >> var) =3D=3D (o->push_ifconfig= _constraint_network >> var); } else { @@ -1512,10 +1513,10 @@ mi->context.c2.push_ifconfig_local =3D remote; if (tunnel_type =3D=3D DEV_TYPE_TAP || (tunnel_type =3D=3D= DEV_TYPE_TUN && tunnel_topology =3D=3D TOP_SUBNET)) { - mi->context.c2.push_ifconfig_remote_netmask =3D mi->co= ntext.options.ifconfig_pool_netmask; + mi->context.c2.push_ifconfig_remote_netmask =3D netbit= s_to_netmask((int)mi->context.options.ifconfig_pool_netbits); if (!mi->context.c2.push_ifconfig_remote_netmask) { - mi->context.c2.push_ifconfig_remote_netmask =3D mi= ->context.c1.tuntap->remote_netmask; + mi->context.c2.push_ifconfig_remote_netmask =3D ne= tbits_to_netmask((int)mi->context.c1.tuntap->netbits); } } else if (tunnel_type =3D=3D DEV_TYPE_TUN) @@ -2425,15 +2426,14 @@ { const char *ifconfig_constraint_network =3D print_in_addr_t(mi->context.options.push_ifconfig_constraint_n= etwork, 0, &gc); - const char *ifconfig_constraint_netmask =3D - print_in_addr_t(mi->context.options.push_ifconfig_constraint_n= etmask, 0, &gc); /* JYFIXME -- this should cause the connection to fail */ msg(D_MULTI_ERRORS, "MULTI ERROR: primary virtual IP for %s (%s) " - "violates tunnel network/netmask constraint (%s/%s)", + "violates tunnel network/subnet constraint (%s/%d)", multi_instance_string(mi, false, &gc), print_in_addr_t(mi->context.c2.push_ifconfig_local, 0, &gc), - ifconfig_constraint_network, ifconfig_constraint_netmask); + ifconfig_constraint_network, + mi->context.options.push_ifconfig_constraint_netbits); } /* diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 649f48b..b0fabe1 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -1075,7 +1075,7 @@ #endif /* ifndef _WIN32 */ static in_addr_t -get_ip_addr(const char *ip_string, int msglevel, bool *error) +get_ip_addr(const char *ip_string, unsigned int *netbits, int msglevel, bo= ol *error) { unsigned int flags =3D GETADDR_HOST_ORDER; bool succeeded =3D false; @@ -1086,7 +1086,7 @@ flags |=3D GETADDR_FATAL; } - ret =3D getaddr(flags, ip_string, 0, &succeeded, NULL); + ret =3D getaddr(flags, ip_string, netbits, 0, &succeeded, NULL); if (!succeeded && error) { *error =3D true; @@ -1333,7 +1333,7 @@ if (ip_addr_dotted_quad_safe(parm)) /* FQDN -- IP address only */ { bool error =3D false; - const in_addr_t addr =3D get_ip_addr(parm, msglevel, &error); + const in_addr_t addr =3D get_ip_addr(parm, NULL, msglevel, &er= ror); if (!error) { array[(*len)++] =3D addr; @@ -1513,12 +1513,10 @@ { struct gc_arena gc =3D gc_new(); - msg(D_SHOW_PARMS, " server_network =3D %s", print_in_addr_t(o->server= _network, 0, &gc)); - msg(D_SHOW_PARMS, " server_netmask =3D %s", print_in_addr_t(o->server= _netmask, 0, &gc)); + msg(D_SHOW_PARMS, " server_network =3D %s/%d", print_in_addr_t(o->ser= ver_network, 0, &gc), o->server_netbits); msg(D_SHOW_PARMS, " server_network_ipv6 =3D %s", print_in6_addr(o->se= rver_network_ipv6, 0, &gc) ); SHOW_INT(server_netbits_ipv6); - msg(D_SHOW_PARMS, " server_bridge_ip =3D %s", print_in_addr_t(o->serv= er_bridge_ip, 0, &gc)); - msg(D_SHOW_PARMS, " server_bridge_netmask =3D %s", print_in_addr_t(o-= >server_bridge_netmask, 0, &gc)); + msg(D_SHOW_PARMS, " server_bridge_ip =3D %s/%d", print_in_addr_t(o->s= erver_bridge_ip, 0, &gc), o->server_bridge_netbits); msg(D_SHOW_PARMS, " server_bridge_pool_start =3D %s", print_in_addr_t= (o->server_bridge_pool_start, 0, &gc)); msg(D_SHOW_PARMS, " server_bridge_pool_end =3D %s", print_in_addr_t(o= ->server_bridge_pool_end, 0, &gc)); if (o->push_list.head) @@ -1534,9 +1532,8 @@ } } SHOW_BOOL(ifconfig_pool_defined); - msg(D_SHOW_PARMS, " ifconfig_pool_start =3D %s", print_in_addr_t(o->i= fconfig_pool_start, 0, &gc)); + msg(D_SHOW_PARMS, " ifconfig_pool_start =3D %s/%d", print_in_addr_t(o= ->ifconfig_pool_start, 0, &gc), o->ifconfig_pool_netbits); msg(D_SHOW_PARMS, " ifconfig_pool_end =3D %s", print_in_addr_t(o->ifc= onfig_pool_end, 0, &gc)); - msg(D_SHOW_PARMS, " ifconfig_pool_netmask =3D %s", print_in_addr_t(o-= >ifconfig_pool_netmask, 0, &gc)); SHOW_STR(ifconfig_pool_persist_filename); SHOW_INT(ifconfig_pool_persist_refresh_freq); SHOW_BOOL(ifconfig_ipv6_pool_defined); @@ -1597,14 +1594,15 @@ int msglevel) { struct iroute *ir; + unsigned int netbits =3D 0; ALLOC_OBJ_GC(ir, struct iroute, &o->gc); - ir->network =3D getaddr(GETADDR_HOST_ORDER, network_str, 0, NULL, NULL= ); - ir->netbits =3D 32; /* host route if no netmask given */ + ir->network =3D getaddr(GETADDR_HOST_ORDER, network_str, &netbits, 0, = NULL, NULL); + ir->netbits =3D 32; /* host route if no netmask or netbits g= iven */ if (netmask_str) { - const in_addr_t netmask =3D getaddr(GETADDR_HOST_ORDER, netmask_st= r, 0, NULL, NULL); + const in_addr_t netmask =3D getaddr(GETADDR_HOST_ORDER, netmask_st= r, NULL, 0, NULL, NULL); ir->netbits =3D netmask_to_netbits2(netmask); if (ir->netbits < 0) @@ -1615,6 +1613,10 @@ return; } } + else if (netbits) + { + ir->netbits =3D (int)netbits; + } ir->next =3D o->iroutes; o->iroutes =3D ir; @@ -2631,9 +2633,9 @@ { msg(M_USAGE, "--connect-freq only works with --mode server --p= roto udp. Try --max-clients instead."); } - if (!(dev =3D=3D DEV_TYPE_TAP || (dev =3D=3D DEV_TYPE_TUN && optio= ns->topology =3D=3D TOP_SUBNET)) && options->ifconfig_pool_netmask) + if (!(dev =3D=3D DEV_TYPE_TAP || (dev =3D=3D DEV_TYPE_TUN && optio= ns->topology =3D=3D TOP_SUBNET)) && options->ifconfig_pool_netbits > 0) { - msg(M_USAGE, "The third parameter to --ifconfig-pool (netmask)= is only valid in --dev tap mode"); + msg(M_USAGE, "The netbits parameter to --ifconfig-pool is only= valid in --dev tap mode"); } if (options->routes && (options->routes->flags & RG_ENABLE)) { @@ -6092,11 +6094,29 @@ iproute_path =3D p[1]; } #endif - else if (streq(p[0], "ifconfig") && p[1] && p[2] && !p[3]) + else if (streq(p[0], "ifconfig") && p[1] && !p[3]) { + bool error =3D false; + VERIFY_PERMISSION(OPT_P_UP); - if (ip_or_dns_addr_safe(p[1], options->allow_pull_fqdn) && ip_or_d= ns_addr_safe(p[2], options->allow_pull_fqdn)) /* FQDN -- may be DNS name */ + if (!p[2]) /* p[1] must be in CIDR format */ { + unsigned int netbits =3D 0; + get_ip_addr(p[1], &netbits, msglevel, &error); + if (error || !netbits) + { + msg(msglevel, "ifconfig parm '%s' must be a valid address/= bits", p[1]); + goto err; + } + options->ifconfig_local =3D strtok(p[1], "/"); + /* convert netbits to netmask to unify the storage as string + * representation of in_addr_t variables and simplify parsing = */ + options->ifconfig_remote_netmask =3D print_in_addr_t(netbits_t= o_netmask((int)netbits), 0, &options->gc); + } + else if (ip_or_dns_addr_safe(p[1], options->allow_pull_fqdn) && ip= _or_dns_addr_safe(p[2], options->allow_pull_fqdn)) /* FQDN -- may be DNS na= me */ + { + /* don't suggest CIDR usage here since we don't know if it's a + * remote address or a netmask */ options->ifconfig_local =3D p[1]; options->ifconfig_remote_netmask =3D p[2]; } @@ -6999,35 +7019,62 @@ VERIFY_PERMISSION(OPT_P_PERSIST_IP); options->persist_remote_ip =3D true; } - else if (streq(p[0], "client-nat") && p[1] && p[2] && p[3] && p[4] && = !p[5]) + else if (streq(p[0], "client-nat") && p[1] && p[2] && p[3] && !p[5]) { + size_t i =3D 1; + const char *type =3D p[i++]; + const char *network =3D p[i++]; + const char *netmask =3D p[i+1] ? p[i++] : NULL; + const char *foreign_network =3D p[i]; + VERIFY_PERMISSION(OPT_P_ROUTE); cnol_check_alloc(options); - add_client_nat_to_option_list(options->client_nat, p[1], p[2], p[3= ], p[4], msglevel); + add_client_nat_to_option_list(options->client_nat, type, network, = netmask, foreign_network, msglevel); } else if (streq(p[0], "route") && p[1] && !p[5]) { + size_t i =3D 1; + char *cidr =3D strchr(p[i], '/'); + if (cidr && !no_more_than_n_args(msglevel, p, 4, NM_QUOTE_HINT)) + { + goto err; + } + VERIFY_PERMISSION(OPT_P_ROUTE); rol_check_alloc(options); if (pull_mode) { - if (!ip_or_dns_addr_safe(p[1], options->allow_pull_fqdn) && !i= s_special_addr(p[1])) /* FQDN -- may be DNS name */ + + const char *network =3D strtok(p[i++], "/"); /* this modifies = p[1] */ + if (!ip_or_dns_addr_safe(network, options->allow_pull_fqdn) &&= !is_special_addr(p[1])) /* FQDN -- may be DNS name */ { - msg(msglevel, "route parameter network/IP '%s' must be a v= alid address", p[1]); + msg(msglevel, "route parameter network/IP '%s' must be a v= alid address or address/bits", p[1]); goto err; } - if (p[2] && !ip_addr_dotted_quad_safe(p[2])) /* FQDN -- must b= e IP address */ + if (!cidr && p[i]) { - msg(msglevel, "route parameter netmask '%s' must be an IP = address", p[2]); - goto err; + if (!ip_addr_dotted_quad_safe(p[i++])) /* FQDN -- must be = IP address */ + { + msg(msglevel, "route parameter netmask '%s' must be an= IP address", p[2]); + goto err; + } } - if (p[3] && !ip_or_dns_addr_safe(p[3], options->allow_pull_fqd= n) && !is_special_addr(p[3])) /* FQDN -- may be DNS name */ + if (p[i] && !ip_or_dns_addr_safe(p[i], options->allow_pull_fqd= n) && !is_special_addr(p[i])) /* FQDN -- may be DNS name */ { - msg(msglevel, "route parameter gateway '%s' must be a vali= d address", p[3]); + msg(msglevel, "route parameter gateway '%s' must be a vali= d address", p[i]); goto err; } } - add_route_to_option_list(options->routes, p[1], p[2], p[3], p[4]); + + if (cidr) + { + *cidr =3D (char)('/'); /* restore the original CIDR notation f= or p[1] */ + add_route_to_option_list(options->routes, p[1], NULL, p[2], p[= 3]); + } + else + { + add_route_to_option_list(options->routes, p[1], p[2], p[3], p[= 4]); + } } else if (streq(p[0], "route-ipv6") && p[1] && !p[4]) { @@ -7348,33 +7395,56 @@ VERIFY_PERMISSION(OPT_P_GENERAL); options->occ =3D false; } - else if (streq(p[0], "server") && p[1] && p[2] && !p[4]) + else if (streq(p[0], "server") && p[1] && !p[4]) { const int lev =3D M_WARN; bool error =3D false; - in_addr_t network, netmask; + in_addr_t network; + unsigned int netbits =3D 0; + size_t i =3D 1; VERIFY_PERMISSION(OPT_P_GENERAL); - network =3D get_ip_addr(p[1], lev, &error); - netmask =3D get_ip_addr(p[2], lev, &error); - if (error || !network || !netmask) + network =3D get_ip_addr(p[i++], &netbits, lev, &error); + if (netbits) + { + if (!no_more_than_n_args(msglevel, p, 3, NM_QUOTE_HINT)) + { + goto err; + } + } + else + { + const in_addr_t netmask =3D get_ip_addr(p[i++], NULL, lev, &er= ror); + const int nb =3D netmask_to_netbits2(netmask); + if (netmask && nb > 0) + { + netbits =3D nb; + } + else + { + msg(M_USAGE, "--server directive network/netmask combinati= on is invalid"); + goto err; + } + } + + if (error || !network || !netbits) { msg(msglevel, "error parsing --server parameters"); goto err; } options->server_defined =3D true; options->server_network =3D network; - options->server_netmask =3D netmask; + options->server_netbits =3D netbits; - if (p[3]) + if (p[i]) { - if (streq(p[3], "nopool")) + if (streq(p[i], "nopool")) { options->server_flags |=3D SF_NOPOOL; } else { - msg(msglevel, "error parsing --server: %s is not a recogni= zed flag", p[3]); + msg(msglevel, "error parsing --server: %s is not a recogni= zed flag", p[i]); goto err; } } @@ -7403,25 +7473,42 @@ options->server_network_ipv6 =3D network; options->server_netbits_ipv6 =3D netbits; } - else if (streq(p[0], "server-bridge") && p[1] && p[2] && p[3] && p[4] = && !p[5]) + else if (streq(p[0], "server-bridge") && p[1] && p[2] && p[3] && !p[5]) { const int lev =3D M_WARN; bool error =3D false; - in_addr_t ip, netmask, pool_start, pool_end; + in_addr_t ip, pool_start, pool_end; + unsigned int netbits =3D 0; + size_t i =3D 1; VERIFY_PERMISSION(OPT_P_GENERAL); - ip =3D get_ip_addr(p[1], lev, &error); - netmask =3D get_ip_addr(p[2], lev, &error); - pool_start =3D get_ip_addr(p[3], lev, &error); - pool_end =3D get_ip_addr(p[4], lev, &error); - if (error || !ip || !netmask || !pool_start || !pool_end) + ip =3D get_ip_addr(p[i++], &netbits, lev, &error); + if (netbits) + { + if (!no_more_than_n_args(msglevel, p, 4, NM_QUOTE_HINT)) + { + goto err; + } + } + else + { + const in_addr_t netmask =3D get_ip_addr(p[i++], NULL, lev, &er= ror); + const int nb =3D netmask_to_netbits2(netmask); + if (netmask && nb > 0) + { + netbits =3D nb; + } + } + pool_start =3D get_ip_addr(p[i++], NULL, lev, &error); + pool_end =3D get_ip_addr(p[i], NULL, lev, &error); + if (error || !ip || !netbits || !pool_start || !pool_end) { msg(msglevel, "error parsing --server-bridge parameters"); goto err; } options->server_bridge_defined =3D true; options->server_bridge_ip =3D ip; - options->server_bridge_netmask =3D netmask; + options->server_bridge_netbits =3D netbits; options->server_bridge_pool_start =3D pool_start; options->server_bridge_pool_end =3D pool_end; } @@ -7456,15 +7543,26 @@ { const int lev =3D M_WARN; bool error =3D false; - in_addr_t start, end, netmask =3D 0; + in_addr_t start, end; + unsigned int netbits =3D 0; VERIFY_PERMISSION(OPT_P_GENERAL); - start =3D get_ip_addr(p[1], lev, &error); - end =3D get_ip_addr(p[2], lev, &error); - if (p[3]) + start =3D get_ip_addr(p[1], &netbits, lev, &error); + end =3D get_ip_addr(p[2], NULL, lev, &error); + if (!netbits && p[3]) { - netmask =3D get_ip_addr(p[3], lev, &error); + const in_addr_t netmask =3D get_ip_addr(p[3], NULL, lev, NULL); + const int nb =3D netmask_to_netbits2(netmask); + if (netmask && nb >=3D 0) + { + netbits =3D nb; + } } + else if (netbits && !no_more_than_n_args(msglevel, p, 3, NM_QUOTE_= HINT)) + { + goto err; + } + if (error) { msg(msglevel, "error parsing --ifconfig-pool parameters"); @@ -7478,10 +7576,7 @@ options->ifconfig_pool_defined =3D true; options->ifconfig_pool_start =3D start; options->ifconfig_pool_end =3D end; - if (netmask) - { - options->ifconfig_pool_netmask =3D netmask; - } + options->ifconfig_pool_netbits =3D netbits; } else if (streq(p[0], "ifconfig-pool-persist") && p[1] && !p[3]) { @@ -7802,45 +7897,76 @@ VERIFY_PERMISSION(OPT_P_INSTANCE); option_iroute_ipv6(options, p[1], msglevel); } - else if (streq(p[0], "ifconfig-push") && p[1] && p[2] && !p[4]) + else if (streq(p[0], "ifconfig-push") && p[1] && !p[4]) { - in_addr_t local, remote_netmask; + in_addr_t local, remote_netmask =3D 0; + unsigned int netbits; + size_t i =3D 1; VERIFY_PERMISSION(OPT_P_INSTANCE); - local =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[1], 0, NUL= L, NULL); - remote_netmask =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[2= ], 0, NULL, NULL); + local =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[i++], &net= bits, 0, NULL, NULL); + if (netbits) /* convert netbits to netmask */ + { + if (!no_more_than_n_args(msglevel, p, 3, NM_QUOTE_HINT)) + { + goto err; + } + remote_netmask =3D netbits_to_netmask((int)netbits); + } + else if (p[i]) + { + /* don't suggest CIDR usage here since we don't know if it's a + * remote address or a netmask */ + remote_netmask =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE,= p[i++], NULL, 0, NULL, NULL); + } + if (local && remote_netmask) { options->push_ifconfig_defined =3D true; options->push_ifconfig_local =3D local; options->push_ifconfig_remote_netmask =3D remote_netmask; - if (p[3]) + if (p[i]) { - options->push_ifconfig_local_alias =3D getaddr(GETADDR_HOS= T_ORDER|GETADDR_RESOLVE, p[3], 0, NULL, NULL); + options->push_ifconfig_local_alias =3D getaddr(GETADDR_HOS= T_ORDER|GETADDR_RESOLVE, p[i], NULL, 0, NULL, NULL); } } else { - msg(msglevel, "cannot parse --ifconfig-push addresses"); + msg(msglevel, "cannot parse --ifconfig-push parameters"); goto err; } } - else if (streq(p[0], "ifconfig-push-constraint") && p[1] && p[2] && !p= [3]) + else if (streq(p[0], "ifconfig-push-constraint") && p[1] && !p[3]) { - in_addr_t network, netmask; + in_addr_t network; + unsigned int netbits =3D 0; + size_t i =3D 1; VERIFY_PERMISSION(OPT_P_GENERAL); - network =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[1], 0, N= ULL, NULL); - netmask =3D getaddr(GETADDR_HOST_ORDER, p[2], 0, NULL, NULL); - if (network && netmask) + network =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[i++], &n= etbits, 0, NULL, NULL); + if (!netbits && p[i]) + { + const in_addr_t netmask =3D getaddr(GETADDR_HOST_ORDER, p[2], = NULL, 0, NULL, NULL); + const int nb =3D netmask_to_netbits2(netmask); + if (netmask && nb > 0) + { + netbits =3D nb; + } + } + else if (netbits && !no_more_than_n_args(msglevel, p, 2, NM_QUOTE_= HINT)) + { + goto err; + } + + if (network && netbits) { options->push_ifconfig_constraint_defined =3D true; options->push_ifconfig_constraint_network =3D network; - options->push_ifconfig_constraint_netmask =3D netmask; + options->push_ifconfig_constraint_netbits =3D netbits; } else { - msg(msglevel, "cannot parse --ifconfig-push-constraint address= es"); + msg(msglevel, "cannot parse --ifconfig-push-constraint paramet= ers"); goto err; } } diff --git a/src/openvpn/options.h b/src/openvpn/options.h index f608cb8..88de24c 100644 --- a/src/openvpn/options.h +++ b/src/openvpn/options.h @@ -450,7 +450,7 @@ const char *tmp_dir; bool server_defined; in_addr_t server_network; - in_addr_t server_netmask; + unsigned int server_netbits; bool server_ipv6_defined; /* IPv6 */ struct in6_addr server_network_ipv6; /* IPv6 */ unsigned int server_netbits_ipv6; /* IPv6 */ @@ -464,7 +464,7 @@ bool server_bridge_defined; in_addr_t server_bridge_ip; - in_addr_t server_bridge_netmask; + unsigned int server_bridge_netbits; in_addr_t server_bridge_pool_start; in_addr_t server_bridge_pool_end; @@ -472,7 +472,7 @@ bool ifconfig_pool_defined; in_addr_t ifconfig_pool_start; in_addr_t ifconfig_pool_end; - in_addr_t ifconfig_pool_netmask; + unsigned int ifconfig_pool_netbits; const char *ifconfig_pool_persist_filename; int ifconfig_pool_persist_refresh_freq; @@ -499,7 +499,7 @@ in_addr_t push_ifconfig_local_alias; bool push_ifconfig_constraint_defined; in_addr_t push_ifconfig_constraint_network; - in_addr_t push_ifconfig_constraint_netmask; + unsigned int push_ifconfig_constraint_netbits; bool push_ifconfig_ipv4_blocked; /* IPv4 */ bool push_ifconfig_ipv6_defined; /* IPv6 */ struct in6_addr push_ifconfig_ipv6_local; /* IPv6 */ diff --git a/src/openvpn/pool.c b/src/openvpn/pool.c index e3c3708..84227b6 100644 --- a/src/openvpn/pool.c +++ b/src/openvpn/pool.c @@ -652,8 +652,8 @@ if (strlen(ip_buf) > 0) { bool v4_ok =3D true; - in_addr_t addr =3D getaddr(GETADDR_HOST_ORDER, ip_buf, 0, = &v4_ok, - NULL); + in_addr_t addr =3D getaddr(GETADDR_HOST_ORDER, ip_buf, NUL= L, 0, + &v4_ok, NULL); if (!v4_ok) { diff --git a/src/openvpn/push.c b/src/openvpn/push.c index 6c06374..b8ab470 100644 --- a/src/openvpn/push.c +++ b/src/openvpn/push.c @@ -1136,9 +1136,20 @@ if (p[0] && !strcmp(p[0], "route") && !p[3] && o->iroutes) { /* get route parameters */ - bool status1, status2; - const in_addr_t network =3D getaddr(GETADDR_HOST_ORDER= , p[1], 0, &status1, NULL); - const in_addr_t netmask =3D getaddr(GETADDR_HOST_ORDER= , p[2] ? p[2] : "255.255.255.255", 0, &status2, NULL); + bool status1, status2 =3D true; + int netbits =3D -1; + + /* get netbits from CIDR or convert netmask to netbits= */ + const in_addr_t network =3D getaddr(GETADDR_HOST_ORDER= , p[1], (unsigned int *)&netbits, 0, &status1, NULL); + if (p[2]) + { + const in_addr_t netmask =3D getaddr(GETADDR_HOST_O= RDER, p[2], NULL, 0, &status2, NULL); + netbits =3D netmask_to_netbits2(netmask); + } + if (netbits <=3D 0) + { + netbits =3D 32; + } /* did route parameters parse correctly? */ if (status1 && status2) @@ -1148,7 +1159,7 @@ /* does route match an iroute? */ for (ir =3D o->iroutes; ir !=3D NULL; ir =3D ir->n= ext) { - if (network =3D=3D ir->network && netmask =3D= =3D netbits_to_netmask(ir->netbits >=3D 0 ? ir->netbits : 32)) + if (network =3D=3D ir->network && netbits =3D= =3D (ir->netbits >=3D 0 ? ir->netbits : 32)) { enable =3D false; break; diff --git a/src/openvpn/route.c b/src/openvpn/route.c index 91f2032..ee77875 100644 --- a/src/openvpn/route.c +++ b/src/openvpn/route.c @@ -189,9 +189,9 @@ route_string(const struct route_ipv4 *r, struct gc_arena *gc) { struct buffer out =3D alloc_buf_gc(256, gc); - buf_printf(&out, "ROUTE network %s netmask %s gateway %s", + buf_printf(&out, "ROUTE network %s/%d gateway %s", print_in_addr_t(r->network, 0, gc), - print_in_addr_t(r->netmask, 0, gc), + r->netbits, print_in_addr_t(r->gateway, 0, gc) ); if (r->flags & RT_METRIC_DEFINED) @@ -321,11 +321,17 @@ const struct route_option *ro, const struct route_list *rl) { - const in_addr_t default_netmask =3D IPV4_NETMASK_HOST; + const in_addr_t default_netbits =3D IPV4_NETBITS_HOST; bool status; int ret; struct in_addr special =3D {0}; + /* separate address and prefix if present */ + char *network =3D strdup(ro->network); + const char *cidr =3D strchr(network, '/'); + const char *addr =3D strtok(network, "/"); + const char *prefix =3D strtok(NULL, "/"); + CLEAR(*r); r->option =3D ro; @@ -355,7 +361,7 @@ else { ret =3D openvpn_getaddrinfo(GETADDR_RESOLVE | GETADDR_WARN_ON_SIGN= AL, - ro->network, NULL, 0, NULL, AF_INET, net= work_list); + addr, NULL, 0, NULL, AF_INET, network_li= st); } status =3D (ret =3D=3D 0); @@ -365,25 +371,40 @@ goto fail; } - /* netmask */ + /* netbits */ + r->netbits =3D default_netbits; if (is_route_parm_defined(ro->netmask)) { - r->netmask =3D getaddr( + const in_addr_t netmask =3D getaddr( GETADDR_HOST_ORDER | GETADDR_WARN_ON_SIGNAL, ro->netmask, + NULL, 0, &status, NULL); - if (!status) + const int netbits =3D netmask_to_netbits2(netmask); + + if (!status || netbits < 0) { goto fail; } + r->netbits =3D netbits; } - else + else if (cidr) { - r->netmask =3D default_netmask; + if (!prefix) /* handle 'aa.bb.cc.dd/' case */ + { + goto fail; + } + char *endp =3D NULL; + const unsigned long netbits =3D strtoul(prefix, &endp, 10); + if ((*endp !=3D '\0') || (netbits > sizeof(in_addr_t) * 8)) + { + goto fail; + } + r->netbits =3D netbits; } /* gateway */ @@ -397,6 +418,7 @@ | GETADDR_HOST_ORDER | GETADDR_WARN_ON_SIGNAL, ro->gateway, + NULL, 0, &status, NULL); @@ -428,7 +450,7 @@ if (r->metric < 0) { msg(M_WARN, PACKAGE_NAME " ROUTE: route metric for network %s = (%s) must be >=3D 0", - ro->network, + addr, ro->metric); goto fail; } @@ -442,11 +464,13 @@ r->flags |=3D RT_DEFINED; + free(network); return true; fail: msg(M_WARN, PACKAGE_NAME " ROUTE: failed to parse/resolve route for ho= st/network: %s", ro->network); + free(network); return false; } @@ -516,7 +540,7 @@ struct route_option *ro; ALLOC_OBJ_GC(ro, struct route_option, l->gc); ro->network =3D network; - ro->netmask =3D netmask; + ro->netmask =3D netmask; /* NULL if network has CIDR notation */ ro->gateway =3D gateway; ro->metric =3D metric; ro->next =3D l->routes; @@ -582,7 +606,7 @@ r1->flags =3D RT_DEFINED; r1->gateway =3D target; r1->network =3D gateway->addr & gateway->netmask; - r1->netmask =3D ~(l2-1); + r1->netbits =3D netmask_to_netbits2(gateway->netmask) + 1; r1->next =3D rl->routes; rl->routes =3D r1; @@ -681,6 +705,7 @@ | GETADDR_HOST_ORDER | GETADDR_WARN_ON_SIGNAL, remote_endpoint, + NULL, 0, &defined, NULL); @@ -921,7 +946,7 @@ static bool add_route3(in_addr_t network, - in_addr_t netmask, + unsigned int netbits, in_addr_t gateway, const struct tuntap *tt, unsigned int flags, @@ -933,14 +958,14 @@ CLEAR(r); r.flags =3D RT_DEFINED; r.network =3D network; - r.netmask =3D netmask; + r.netbits =3D netbits; r.gateway =3D gateway; return add_route(&r, tt, flags, rgi, es, ctx); } static void del_route3(in_addr_t network, - in_addr_t netmask, + unsigned int netbits, in_addr_t gateway, const struct tuntap *tt, unsigned int flags, @@ -952,7 +977,7 @@ CLEAR(r); r.flags =3D RT_DEFINED|RT_ADDED; r.network =3D network; - r.netmask =3D netmask; + r.netbits =3D netbits; r.gateway =3D gateway; delete_route(&r, tt, flags, rgi, es, ctx); } @@ -971,7 +996,7 @@ { if (rb->bypass[i]) { - ret =3D add_route3(rb->bypass[i], IPV4_NETMASK_HOST, gateway, = tt, + ret =3D add_route3(rb->bypass[i], IPV4_NETBITS_HOST, gateway, = tt, flags | ROUTE_REF_GW, rgi, es, ctx) && ret; } } @@ -993,7 +1018,7 @@ if (rb->bypass[i]) { del_route3(rb->bypass[i], - IPV4_NETMASK_HOST, + IPV4_NETBITS_HOST, gateway, tt, flags | ROUTE_REF_GW, @@ -1057,7 +1082,7 @@ if ((rl->spec.flags & RTSA_REMOTE_HOST) && rl->spec.remote_host !=3D IPV4_INVALID_ADDR) { - ret =3D add_route3(rl->spec.remote_host, IPV4_NETMASK_= HOST, + ret =3D add_route3(rl->spec.remote_host, IPV4_NETBITS_= HOST, rl->rgi.gateway.addr, tt, flags | ROU= TE_REF_GW, &rl->rgi, es, ctx); rl->iflags |=3D RL_DID_LOCAL; @@ -1078,11 +1103,11 @@ if (rl->flags & RG_DEF1) { /* add new default route (1st component) */ - ret =3D add_route3(0x00000000, 0x80000000, rl->spec.re= mote_endpoint, + ret =3D add_route3(0x00000000, 1, rl->spec.remote_endp= oint, tt, flags, &rl->rgi, es, ctx) && ret; /* add new default route (2nd component) */ - ret =3D add_route3(0x80000000, 0x80000000, rl->spec.re= mote_endpoint, + ret =3D add_route3(0x80000000, 1, rl->spec.remote_endp= oint, tt, flags, &rl->rgi, es, ctx) && ret; } else @@ -1120,7 +1145,7 @@ if (rl->iflags & RL_DID_LOCAL) { del_route3(rl->spec.remote_host, - IPV4_NETMASK_HOST, + IPV4_NETBITS_HOST, rl->rgi.gateway.addr, tt, flags | ROUTE_REF_GW, @@ -1140,7 +1165,7 @@ { /* delete default route (1st component) */ del_route3(0x00000000, - 0x80000000, + 1, rl->spec.remote_endpoint, tt, flags, @@ -1150,7 +1175,7 @@ /* delete default route (2nd component) */ del_route3(0x80000000, - 0x80000000, + 1, rl->spec.remote_endpoint, tt, flags, @@ -1215,7 +1240,7 @@ for (r =3D rl->routes; r; r =3D r->next) { - check_subnet_conflict(r->network, r->netmask, "route"); + check_subnet_conflict(r->network, netbits_to_netmask((int)r->n= etbits), "route"); if (flags & ROUTE_DELETE_FIRST) { delete_route(r, tt, flags, &rl->rgi, es, ctx); @@ -1432,14 +1457,18 @@ if (r->flags & RT_DEFINED) { setenv_route_addr(es, "network", r->network, i); - setenv_route_addr(es, "netmask", r->netmask, i); + setenv_route_addr(es, "netmask", netbits_to_netmask(r->netbits), i= ); setenv_route_addr(es, "gateway", r->gateway, i); + struct buffer name1 =3D alloc_buf_gc(256, &gc); + buf_printf(&name1, "route_netbits_%d", i); + setenv_int(es, BSTR(&name1), (int)r->netbits); + if (r->flags & RT_METRIC_DEFINED) { - struct buffer name =3D alloc_buf_gc(256, &gc); - buf_printf(&name, "route_metric_%d", i); - setenv_int(es, BSTR(&name), r->metric); + struct buffer name2 =3D alloc_buf_gc(256, &gc); + buf_printf(&name2, "route_metric_%d", i); + setenv_int(es, BSTR(&name2), r->metric); } } gc_free(&gc); @@ -1519,7 +1548,7 @@ static int local_route(in_addr_t network, - in_addr_t netmask, + unsigned int netbits, in_addr_t gateway, const struct route_gateway_info *rgi) { @@ -1528,7 +1557,7 @@ if (rgi && (rgi->flags & rgi_needed) =3D=3D rgi_needed && gateway =3D=3D rgi->gateway.addr - && netmask =3D=3D 0xFFFFFFFF) + && netbits =3D=3D IPV4_NETBITS_HOST) { if (((network ^ rgi->gateway.addr) & rgi->gateway.netmask) =3D=3D= 0) { @@ -1583,12 +1612,12 @@ #if !defined(TARGET_LINUX) const char *network =3D print_in_addr_t(r->network, 0, &gc); #if !defined(TARGET_AIX) - const char *netmask =3D print_in_addr_t(r->netmask, 0, &gc); + const char *netmask =3D print_in_addr_t(netbits_to_netmask((int)r->net= bits), 0, &gc); #endif const char *gateway =3D print_in_addr_t(r->gateway, 0, &gc); #endif - is_local_route =3D local_route(r->network, r->netmask, r->gateway, rgi= ); + is_local_route =3D local_route(r->network, r->netbits, r->gateway, rgi= ); if (is_local_route =3D=3D LR_ERROR) { goto done; @@ -1609,7 +1638,7 @@ } status =3D RTA_SUCCESS; - int ret =3D net_route_v4_add(ctx, &r->network, netmask_to_netbits2(r->= netmask), + int ret =3D net_route_v4_add(ctx, &r->network, (int)r->netbits, &r->gateway, iface, 0, metric); if (ret =3D=3D -EEXIST) { @@ -1840,10 +1869,9 @@ #elif defined(TARGET_AIX) { - int netbits =3D netmask_to_netbits2(r->netmask); argv_printf(&argv, "%s add -net %s/%d %s", ROUTE_PATH, - network, netbits, gateway); + network, r->netbits, gateway); argv_msg(D_ROUTE, &argv); bool ret =3D openvpn_execve_check(&argv, es, 0, "ERROR: AIX route add command fail= ed"); @@ -2176,14 +2204,14 @@ #if !defined(TARGET_LINUX) network =3D print_in_addr_t(r->network, 0, &gc); #if !defined(TARGET_AIX) - netmask =3D print_in_addr_t(r->netmask, 0, &gc); + netmask =3D print_in_addr_t(netbits_to_netmask((int)r->netbits), 0, &g= c); #endif #if !defined(TARGET_ANDROID) gateway =3D print_in_addr_t(r->gateway, 0, &gc); #endif #endif - is_local_route =3D local_route(r->network, r->netmask, r->gateway, rgi= ); + is_local_route =3D local_route(r->network, r->netbits, r->gateway, rgi= ); if (is_local_route =3D=3D LR_ERROR) { goto done; @@ -2196,7 +2224,7 @@ metric =3D r->metric; } - if (net_route_v4_del(ctx, &r->network, netmask_to_netbits2(r->netmask), + if (net_route_v4_del(ctx, &r->network, (int)r->netbits, &r->gateway, NULL, 0, metric) < 0) { msg(M_WARN, "ERROR: Linux route delete command failed"); @@ -2318,10 +2346,9 @@ #elif defined(TARGET_AIX) { - int netbits =3D netmask_to_netbits2(r->netmask); argv_printf(&argv, "%s delete -net %s/%d %s", ROUTE_PATH, - network, netbits, gateway); + network, r->netbits, gateway); argv_msg(D_ROUTE, &argv); openvpn_execve_check(&argv, es, 0, "ERROR: AIX route delete comman= d failed"); } @@ -2840,13 +2867,14 @@ int ret =3D RTA_ERROR; DWORD status; const DWORD if_index =3D (adapter_index =3D=3D TUN_ADAPTER_INDEX_INVAL= ID) ? windows_route_find_if_index(r, tt) : adapter_index; + const in_addr_t netmask =3D netbits_to_netmask((int)r->netbits); if (if_index !=3D TUN_ADAPTER_INDEX_INVALID) { MIB_IPFORWARDROW fr; CLEAR(fr); fr.dwForwardDest =3D htonl(r->network); - fr.dwForwardMask =3D htonl(r->netmask); + fr.dwForwardMask =3D htonl(netmask); fr.dwForwardPolicy =3D 0; fr.dwForwardNextHop =3D htonl(r->gateway); fr.dwForwardIfIndex =3D if_index; @@ -2860,11 +2888,11 @@ fr.dwForwardMetric4 =3D METRIC_NOT_USED; fr.dwForwardMetric5 =3D METRIC_NOT_USED; - if ((r->network & r->netmask) !=3D r->network) + if ((r->network & netmask) !=3D r->network) { msg(M_WARN, "Warning: address %s is not a network address in r= elation to netmask %s", print_in_addr_t(r->network, 0, &gc), - print_in_addr_t(r->netmask, 0, &gc)); + print_in_addr_t(netmask, 0, &gc)); } status =3D CreateIpForwardEntry(&fr); @@ -2932,6 +2960,7 @@ bool ret =3D false; DWORD status; const DWORD if_index =3D windows_route_find_if_index(r, tt); + const in_addr_t netmask =3D netbits_to_netmask((int)r->netbits); if (if_index !=3D TUN_ADAPTER_INDEX_INVALID) { @@ -2939,7 +2968,7 @@ CLEAR(fr); fr.dwForwardDest =3D htonl(r->network); - fr.dwForwardMask =3D htonl(r->netmask); + fr.dwForwardMask =3D htonl(netmask); fr.dwForwardPolicy =3D 0; fr.dwForwardNextHop =3D htonl(r->gateway); fr.dwForwardIfIndex =3D if_index; @@ -3016,7 +3045,7 @@ .metric =3D (r->flags & RT_METRIC_DEFINED ? r->metric : -1) }; - netmask_to_netbits(r->network, r->netmask, &msg.prefix_len); + msg.prefix_len =3D r->netbits; if (msg.prefix_len =3D=3D -1) { msg.prefix_len =3D 32; @@ -3996,7 +4025,7 @@ while (iplist) { bool succeed =3D false; - const in_addr_t ip =3D getaddr(GETADDR_HOST_ORDER, iplist->IpAddre= ss.String, 0, &succeed, NULL); + const in_addr_t ip =3D getaddr(GETADDR_HOST_ORDER, iplist->IpAddre= ss.String, NULL, 0, &succeed, NULL); if (succeed) { add_host_route_if_nonlocal(rb, ip); @@ -4096,7 +4125,7 @@ { if (rgi) { - if (local_route(addr, 0xFFFFFFFF, rgi->gateway.addr, rgi)) + if (local_route(addr, IPV4_NETBITS_HOST, rgi->gateway.addr, rgi)) { return TLA_LOCAL; } diff --git a/src/openvpn/route.h b/src/openvpn/route.h index 421e7d2..a117584 100644 --- a/src/openvpn/route.h +++ b/src/openvpn/route.h @@ -117,7 +117,7 @@ unsigned int flags; const struct route_option *option; in_addr_t network; - in_addr_t netmask; + unsigned int netbits; in_addr_t gateway; int metric; }; diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 17c5e76..767036f 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -179,6 +179,7 @@ in_addr_t getaddr(unsigned int flags, const char *hostname, + unsigned int *netbits, int resolve_retry_seconds, bool *succeeded, struct signal_info *sig_info) @@ -186,7 +187,7 @@ in_addr_t addr; int status; - status =3D get_addr_generic(AF_INET, flags, hostname, &addr, NULL, + status =3D get_addr_generic(AF_INET, flags, hostname, &addr, netbits, resolve_retry_seconds, sig_info, M_WARN); if (status=3D=3D0) diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h index 47083ad..f6165b2 100644 --- a/src/openvpn/socket.h +++ b/src/openvpn/socket.h @@ -524,6 +524,7 @@ */ in_addr_t getaddr(unsigned int flags, const char *hostname, + unsigned int *netbits, int resolve_retry_seconds, bool *succeeded, struct signal_info *sig_info); diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index 739e008..52e2464 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -113,7 +113,7 @@ if (addr.family =3D=3D AF_INET) { addr.address.ipv4.s_addr =3D htonl(tt->local); - addr.prefix_len =3D netmask_to_netbits2(tt->adapter_netmask); + addr.prefix_len =3D tt->netbits; msg(D_IFCONFIG, "INET address service: %s %s/%d", add ? "add" : "remove", print_in_addr_t(tt->local, 0, &gc), addr.prefix_len); @@ -499,17 +499,17 @@ static const char ifconfig_warn_how_to_silence[] =3D "(silence this warnin= g with --ifconfig-nowarn)"; /* - * If !tun_p2p, make sure ifconfig_remote_netmask looks - * like a netmask. + * If !tun_p2p, make sure either ifconfig_netbits is + * set or ifconfig_remote looks like a netmask. * - * If tun_p2p, make sure ifconfig_remote_netmask looks + * If tun_p2p, make sure ifconfig_remote looks * like an IPv4 address. */ static void -ifconfig_sanity_check(bool tun_p2p, in_addr_t addr) +ifconfig_sanity_check(bool tun_p2p, in_addr_t addr, int netbits) { struct gc_arena gc =3D gc_new(); - const bool looks_like_netmask =3D ((addr & 0xFF000000) =3D=3D 0xFF0000= 00); + const bool looks_like_netmask =3D addr && ((addr & 0xFF000000) =3D=3D = 0xFF000000); if (tun_p2p) { if (looks_like_netmask) @@ -521,9 +521,11 @@ } else { - if (!looks_like_netmask) + if (netbits < 0 || !looks_like_netmask) { - msg(M_WARN, "WARNING: Since you are using subnet topology, the= second argument to --ifconfig must be a netmask, for example something lik= e 255.255.255.0. %s", + msg(M_WARN, "WARNING: Since you are using topology subnet, you= must either " + "provide a valid prefix length in the first argument of --= ifconfig (eg. 10.8.0.0/24) " + "or a valid netmask as the second argument (eg. 255.255.25= 5.0). %s", ifconfig_warn_how_to_silence); } } @@ -539,34 +541,36 @@ int type, in_addr_t public, in_addr_t local, - in_addr_t remote_netmask) + in_addr_t remote, + int netbits) { struct gc_arena gc =3D gc_new(); #if 0 - msg(M_INFO, "CHECK_ADDR_CLASH type=3D%d public=3D%s local=3D%s, remote= _netmask=3D%s", + msg(M_INFO, "CHECK_ADDR_CLASH type=3D%d public=3D%s local=3D%s, netbit= s=3D%d, remote=3D%s", type, print_in_addr_t(public, 0, &gc), print_in_addr_t(local, 0, &gc), - print_in_addr_t(remote_netmask, 0, &gc)); + netbits, + print_in_addr_t(remote, 0, &gc)); #endif if (public) { if (type =3D=3D DEV_TYPE_TUN) { - const in_addr_t test_netmask =3D 0xFFFFFF00; - const in_addr_t public_net =3D public &test_netmask; - const in_addr_t local_net =3D local & test_netmask; - const in_addr_t remote_net =3D remote_netmask & test_netmask; + const in_addr_t netmask =3D netbits < 0 ? 0xFFFFFF00 : netbits= _to_netmask(netbits); + const in_addr_t public_net =3D public &netmask; + const in_addr_t local_net =3D local & netmask; + const in_addr_t remote_net =3D remote & netmask; - if (public =3D=3D local || public =3D=3D remote_netmask) + if (public =3D=3D local || public =3D=3D remote) { msg(M_WARN, "WARNING: --%s address [%s] conflicts with --ifconfig = address pair [%s, %s]. %s", name, print_in_addr_t(public, 0, &gc), print_in_addr_t(local, 0, &gc), - print_in_addr_t(remote_netmask, 0, &gc), + print_in_addr_t(remote, 0, &gc), ifconfig_warn_how_to_silence); } @@ -577,22 +581,23 @@ name, print_in_addr_t(public, 0, &gc), print_in_addr_t(local, 0, &gc), - print_in_addr_t(remote_netmask, 0, &gc), + print_in_addr_t(remote ? remote : netmask, 0, &gc), ifconfig_warn_how_to_silence); } } else if (type =3D=3D DEV_TYPE_TAP) { - const in_addr_t public_network =3D public &remote_netmask; - const in_addr_t virtual_network =3D local & remote_netmask; - if (public_network =3D=3D virtual_network) + const in_addr_t netmask =3D netbits_to_netmask(netbits); + const in_addr_t public_network =3D public &netmask; + const in_addr_t virtual_network =3D local & netmask; + if (netmask && public_network =3D=3D virtual_network) { msg(M_WARN, - "WARNING: --%s address [%s] conflicts with --ifconfig = subnet [%s, %s] -- local and remote addresses cannot be inside of the --ifc= onfig subnet. %s", + "WARNING: --%s address [%s] conflicts with --ifconfig = subnet [%s/%d] -- local and remote addresses cannot be inside of the --ifco= nfig subnet. %s", name, print_in_addr_t(public, 0, &gc), print_in_addr_t(local, 0, &gc), - print_in_addr_t(remote_netmask, 0, &gc), + netbits, ifconfig_warn_how_to_silence); } } @@ -669,9 +674,10 @@ { if (!is_tun_p2p(tt)) { + const in_addr_t netmask =3D netbits_to_netmask(tt->netbits); buf_printf(&out, "%s %s", - print_in_addr_t(tt->local & tt->remote_netmask, 0, = gc), - print_in_addr_t(tt->remote_netmask, 0, gc)); + print_in_addr_t(tt->local & netmask, 0, gc), + print_in_addr_t(netmask, 0, gc)); } else if (tt->type =3D=3D DEV_TYPE_TUN) /* tun p2p topology */ { @@ -679,12 +685,12 @@ if (remote) { r =3D print_in_addr_t(tt->local, 0, gc); - l =3D print_in_addr_t(tt->remote_netmask, 0, gc); + l =3D print_in_addr_t(tt->remote, 0, gc); } else { l =3D print_in_addr_t(tt->local, 0, gc); - r =3D print_in_addr_t(tt->remote_netmask, 0, gc); + r =3D print_in_addr_t(tt->remote, 0, gc); } buf_printf(&out, "%s %s", r, l); } @@ -765,7 +771,8 @@ { struct gc_arena gc =3D gc_new(); const char *ifconfig_local =3D print_in_addr_t(tt->local, 0, &gc); - const char *ifconfig_remote_netmask =3D print_in_addr_t(tt->remote_net= mask, 0, &gc); + const char *ifconfig_remote =3D tt->remote ? print_in_addr_t(tt->remot= e, 0, &gc) : NULL; + const char *ifconfig_netmask =3D tt->netbits >=3D 0 ? print_in_addr_t(= netbits_to_netmask(tt->netbits), 0, &gc) : NULL; /* * Set environmental variables with ifconfig parameters. @@ -777,11 +784,12 @@ setenv_str(es, "ifconfig_local", ifconfig_local); if (tun) { - setenv_str(es, "ifconfig_remote", ifconfig_remote_netmask); + setenv_str(es, "ifconfig_remote", ifconfig_remote); } else { - setenv_str(es, "ifconfig_netmask", ifconfig_remote_netmask); + setenv_int(es, "ifconfig_netbits", tt->netbits); + setenv_str(es, "ifconfig_netmask", ifconfig_netmask); } } @@ -837,7 +845,7 @@ bool tun_p2p =3D is_tun_p2p(tt); /* - * Convert arguments to binary IPv4 addresses. + * Convert arguments to binary IPv4 addresses and netbits. */ tt->local =3D getaddr( @@ -846,27 +854,48 @@ | GETADDR_FATAL_ON_SIGNAL | GETADDR_FATAL, ifconfig_local_parm, + NULL, 0, NULL, NULL); - tt->remote_netmask =3D getaddr( + tt->remote =3D getaddr( (tun_p2p ? GETADDR_RESOLVE : 0) | GETADDR_HOST_ORDER | GETADDR_FATAL_ON_SIGNAL | GETADDR_FATAL, ifconfig_remote_netmask_parm, + NULL, 0, NULL, NULL); + /* in p2p mode discard the netbits otherwise discard the remote */ + if (!tun_p2p) + { + if (tt->remote) + { + tt->netbits =3D netmask_to_netbits2(tt->remote); + tt->remote =3D 0; + } + else + { + msg(M_FATAL, "init_tun: can not figure out the subnet from= the remote parameter (%s)", ifconfig_remote_netmask_parm); + } + tt->remote =3D 0; + } + else + { + tt->netbits =3D -1; + } + /* * Look for common errors in --ifconfig parms */ if (strict_warn) { struct addrinfo *curele; - ifconfig_sanity_check(tun_p2p, tt->remote_netmask); + ifconfig_sanity_check(tun_p2p, tt->remote, tt->netbits); /* * If local_public or remote_public addresses are defined, @@ -877,11 +906,13 @@ { if (curele->ai_family =3D=3D AF_INET) { + const in_addr_t local =3D ntohl(((struct sockaddr_in *= )curele->ai_addr)->sin_addr.s_addr); check_addr_clash("local", tt->type, - ((struct sockaddr_in *)curele->ai_add= r)->sin_addr.s_addr, + local, tt->local, - tt->remote_netmask); + tt->remote, + tt->netbits); } } @@ -889,17 +920,19 @@ { if (curele->ai_family =3D=3D AF_INET) { + const in_addr_t remote =3D ntohl(((struct sockaddr_in = *)curele->ai_addr)->sin_addr.s_addr); check_addr_clash("remote", tt->type, - ((struct sockaddr_in *)curele->ai_add= r)->sin_addr.s_addr, + remote, tt->local, - tt->remote_netmask); + tt->remote, + tt->netbits); } } if (!tun_p2p) { - check_subnet_conflict(tt->local, tt->remote_netmask, "TUN/= TAP adapter"); + check_subnet_conflict(tt->local, netbits_to_netmask(tt->ne= tbits), "TUN/TAP adapter"); } else { @@ -914,12 +947,12 @@ */ if (tun_p2p) { - verify_255_255_255_252(tt->local, tt->remote_netmask); + verify_255_255_255_252(tt->local, tt->remote); tt->adapter_netmask =3D ~3; } else { - tt->adapter_netmask =3D tt->remote_netmask; + tt->adapter_netmask =3D netbits_to_netmask(tt->netbits); } #endif @@ -1062,8 +1095,9 @@ create_arbitrary_remote( struct tuntap *tt ) { in_addr_t remote; + const in_addr_t netmask =3D netbits_to_netmask(tt->netbits); - remote =3D (tt->local & tt->remote_netmask) +1; + remote =3D (tt->local & netmask) +1; if (remote =3D=3D tt->local) { @@ -1300,7 +1334,13 @@ #if !defined(TARGET_LINUX) const char *ifconfig_local =3D NULL; - const char *ifconfig_remote_netmask =3D NULL; +#if !defined(_WIN32) + const char *ifconfig_remote =3D NULL; +#endif +#if !defined(TARGET_FREEBSD) && !defined(TARGET_DRAGONFLY) + const char *ifconfig_netmask =3D NULL; + const in_addr_t netmask =3D netbits_to_netmask(tt->netbits); +#endif struct argv argv =3D argv_new(); struct gc_arena gc =3D gc_new(); @@ -1308,7 +1348,12 @@ * Set ifconfig parameters */ ifconfig_local =3D print_in_addr_t(tt->local, 0, &gc); - ifconfig_remote_netmask =3D print_in_addr_t(tt->remote_netmask, 0, &gc= ); +#if !defined(_WIN32) + ifconfig_remote =3D print_in_addr_t(tt->remote, 0, &gc); +#endif +#if !defined(TARGET_FREEBSD) && !defined(TARGET_DRAGONFLY) + ifconfig_netmask =3D print_in_addr_t(netmask, 0, &gc); +#endif #endif #if defined(TARGET_LINUX) @@ -1324,16 +1369,14 @@ if (tun_p2p) { - if (net_addr_ptp_v4_add(ctx, ifname, &tt->local, - &tt->remote_netmask) < 0) + if (net_addr_ptp_v4_add(ctx, ifname, &tt->local, &tt->remote) < 0) { msg(M_FATAL, "Linux can't add IP to interface %s", ifname); } } else { - if (net_addr_v4_add(ctx, ifname, &tt->local, - netmask_to_netbits2(tt->remote_netmask)) < 0) + if (net_addr_v4_add(ctx, ifname, &tt->local, tt->netbits) < 0) { msg(M_FATAL, "Linux can't add IP to interface %s", ifname); } @@ -1341,9 +1384,14 @@ #elif defined(TARGET_ANDROID) char out[64]; - snprintf(out, sizeof(out), "%s %s %d %s", ifconfig_local, - ifconfig_remote_netmask, tun_mtu, print_topology(tt->topology= )); + snprintf(out, sizeof(out), "%s %s %d %s", + ifconfig_local, + ifconfig_remote ? ifconfig_remote : ifconfig_netmask, + tun_mtu, + print_topology(tt->topology)); management_android_control(management, "IFCONFIG", out); + (void)ifconfig_netmask; + (void)netmask; #elif defined(TARGET_SOLARIS) /* Solaris 2.6 (and 7?) cannot set all parameters in one go... @@ -1354,7 +1402,7 @@ if (tun_p2p) { argv_printf(&argv, "%s %s %s %s mtu %d up", IFCONFIG_PATH, ifname, - ifconfig_local, ifconfig_remote_netmask, tun_mtu); + ifconfig_local, ifconfig_remote, tun_mtu); argv_msg(M_INFO, &argv); if (!openvpn_execve_check(&argv, es, 0, "Solaris ifconfig phase-1 = failed")) @@ -1369,13 +1417,13 @@ { argv_printf(&argv, "%s %s %s %s netmask %s mtu %d up", IFCONFIG_PA= TH, ifname, ifconfig_local, ifconfig_local, - ifconfig_remote_netmask, tun_mtu); + ifconfig_netmask, tun_mtu); } else /* tap */ { argv_printf(&argv, "%s %s %s netmask %s up", IFCONFIG_PATH, ifname, ifconfig_local, - ifconfig_remote_netmask); + ifconfig_netmask); } argv_msg(M_INFO, &argv); @@ -1390,8 +1438,8 @@ struct route_ipv4 r; CLEAR(r); r.flags =3D RT_DEFINED | RT_METRIC_DEFINED; - r.network =3D tt->local & tt->remote_netmask; - r.netmask =3D tt->remote_netmask; + r.network =3D tt->local & netmask; + r.netbits =3D tt->netbits; r.gateway =3D tt->local; r.metric =3D 0; add_route(&r, tt, 0, NULL, es, NULL); @@ -1413,7 +1461,7 @@ argv_printf(&argv, "%s %s %s %s mtu %d netmask 255.255.255.255 up -link0", IFCONFIG_PATH, ifname, ifconfig_local, - ifconfig_remote_netmask, tun_mtu); + ifconfig_remote, tun_mtu); } else if (tt->type =3D=3D DEV_TYPE_TUN) { @@ -1421,13 +1469,13 @@ argv_printf(&argv, "%s %s %s %s mtu %d netmask %s up -link0", IFCONFIG_PATH, ifname, ifconfig_local, print_in_addr_t(remote_end, 0, &gc), tun_mtu, - ifconfig_remote_netmask); + ifconfig_netmask); } else /* tap */ { argv_printf(&argv, "%s %s %s netmask %s mtu %d link0", IFCONFIG_PATH, ifname, ifconfig_local, - ifconfig_remote_netmask, tun_mtu); + ifconfig_netmask, tun_mtu); } argv_msg(M_INFO, &argv); openvpn_execve_check(&argv, es, S_FATAL, "OpenBSD ifconfig failed"); @@ -1438,8 +1486,8 @@ struct route_ipv4 r; CLEAR(r); r.flags =3D RT_DEFINED; - r.network =3D tt->local & tt->remote_netmask; - r.netmask =3D tt->remote_netmask; + r.network =3D tt->local & netmask; + r.netbits =3D tt->netbits; r.gateway =3D remote_end; add_route(&r, tt, 0, NULL, es, NULL); } @@ -1451,14 +1499,14 @@ { argv_printf(&argv, "%s %s %s %s mtu %d netmask 255.255.255.255 up", IFCONFIG_PATH, ifname, ifconfig_local, - ifconfig_remote_netmask, tun_mtu); + ifconfig_remote, tun_mtu); } else if (tt->type =3D=3D DEV_TYPE_TUN) { remote_end =3D create_arbitrary_remote(tt); argv_printf(&argv, "%s %s %s %s mtu %d netmask %s up", IFCONFIG_PA= TH, ifname, ifconfig_local, print_in_addr_t(remote_end, 0,= &gc), - tun_mtu, ifconfig_remote_netmask); + tun_mtu, ifconfig_netmask); } else /* tap */ { @@ -1469,7 +1517,7 @@ */ argv_printf(&argv, "%s %s %s netmask %s mtu %d", IFCONFIG_PATH, ifname, ifconfig_local, - ifconfig_remote_netmask, tun_mtu); + ifconfig_netmask, tun_mtu); } argv_msg(M_INFO, &argv); openvpn_execve_check(&argv, es, S_FATAL, "NetBSD ifconfig failed"); @@ -1480,8 +1528,8 @@ struct route_ipv4 r; CLEAR(r); r.flags =3D RT_DEFINED; - r.network =3D tt->local & tt->remote_netmask; - r.netmask =3D tt->remote_netmask; + r.network =3D tt->local & netmask; + r.netbits =3D tt->netbits; r.gateway =3D remote_end; add_route(&r, tt, 0, NULL, es, NULL); } @@ -1503,18 +1551,18 @@ { argv_printf(&argv, "%s %s %s %s mtu %d netmask 255.255.255.255 up", IFCONFIG_PATH, ifname, ifconfig_local, - ifconfig_remote_netmask, tun_mtu); + ifconfig_remote, tun_mtu); } else if (tt->type =3D=3D DEV_TYPE_TUN) { argv_printf(&argv, "%s %s %s %s netmask %s mtu %d up", IFCONFIG_PATH, ifname, ifconfig_local, ifconfig_local, - ifconfig_remote_netmask, tun_mtu); + ifconfig_netmask, tun_mtu); } else /* tap */ { argv_printf(&argv, "%s %s %s netmask %s mtu %d up", IFCONFIG_PATH, - ifname, ifconfig_local, ifconfig_remote_netmask, + ifname, ifconfig_local, ifconfig_netmask, tun_mtu); } @@ -1527,8 +1575,8 @@ struct route_ipv4 r; CLEAR(r); r.flags =3D RT_DEFINED; - r.network =3D tt->local & tt->remote_netmask; - r.netmask =3D tt->remote_netmask; + r.network =3D tt->local & netmask; + r.netbits =3D tt->netbits; r.gateway =3D tt->local; add_route(&r, tt, 0, NULL, es, NULL); } @@ -1540,13 +1588,12 @@ { argv_printf(&argv, "%s %s %s %s mtu %d netmask 255.255.255.255 up", IFCONFIG_PATH, ifname, ifconfig_local, - ifconfig_remote_netmask, tun_mtu); + ifconfig_remote, tun_mtu); } else /* tun with topology subnet and tap mode (always subne= t) */ { - int netbits =3D netmask_to_netbits2(tt->remote_netmask); argv_printf(&argv, "%s %s %s/%d mtu %d up", IFCONFIG_PATH, - ifname, ifconfig_local, netbits, tun_mtu ); + ifname, ifconfig_local, tt->netbits, tun_mtu ); } argv_msg(M_INFO, &argv); @@ -1565,7 +1612,7 @@ /* example: ifconfig tap0 172.30.1.1 netmask 255.255.254.0 up */ argv_printf(&argv, "%s %s %s netmask %s mtu %d up", IFCONFIG_PATH, - ifname, ifconfig_local, ifconfig_remote_netmask, tun_m= tu); + ifname, ifconfig_local, ifconfig_netmask, tun_mtu); argv_msg(M_INFO, &argv); openvpn_execve_check(&argv, aix_es, S_FATAL, "AIX ifconfig failed"= ); @@ -1578,7 +1625,7 @@ msg(M_INFO, "******** NOTE: Please manually set the IP/netmask of '%s' to= %s/%s (if it is not already set)", ifname, ifconfig_local, - ifconfig_remote_netmask); + ifconfig_netmask); } else if (tt->options.ip_win32_type =3D=3D IPW32_SET_DHCP_MASQ || tt->o= ptions.ip_win32_type =3D=3D IPW32_SET_ADAPTIVE) { @@ -1660,12 +1707,10 @@ undo_ifconfig_ipv4(struct tuntap *tt, openvpn_net_ctx_t *ctx) { #if defined(TARGET_LINUX) - int netbits =3D netmask_to_netbits2(tt->remote_netmask); - if (is_tun_p2p(tt)) { if (net_addr_ptp_v4_del(ctx, tt->actual_name, &tt->local, - &tt->remote_netmask) < 0) + &tt->remote) < 0) { msg(M_WARN, "Linux can't del IP from iface %s", tt->actual_name); @@ -1673,7 +1718,7 @@ } else { - if (net_addr_v4_del(ctx, tt->actual_name, &tt->local, netbits) < 0) + if (net_addr_v4_del(ctx, tt->actual_name, &tt->local, tt->netbits)= < 0) { msg(M_WARN, "Linux can't del IP from iface %s", tt->actual_name); @@ -4683,8 +4728,8 @@ if (ip_str && netmask_str && strlen(ip_str) && strlen(netmask_= str)) { - *ip =3D getaddr(getaddr_flags, ip_str, 0, &succeed1, NULL); - *netmask =3D getaddr(getaddr_flags, netmask_str, 0, &succe= ed2, NULL); + *ip =3D getaddr(getaddr_flags, ip_str, NULL, 0, &succeed1,= NULL); + *netmask =3D getaddr(getaddr_flags, netmask_str, NULL, 0, = &succeed2, NULL); ret =3D (succeed1 =3D=3D true && succeed2 =3D=3D true); } } @@ -5370,7 +5415,7 @@ break; } - ip =3D getaddr(getaddr_flags, ip_str, 0, &succeed, NULL); + ip =3D getaddr(getaddr_flags, ip_str, NULL, 0, &succeed, NULL); if (!succeed) { break; @@ -6428,11 +6473,12 @@ if (!tt->did_ifconfig_setup || tt->topology =3D=3D TOP_SUBNET) { in_addr_t ep[3]; + const in_addr_t netmask =3D netbits_to_netmask(tt->netbits); BOOL status; ep[0] =3D htonl(tt->local); - ep[1] =3D htonl(tt->local & tt->remote_netmask); - ep[2] =3D htonl(tt->remote_netmask); + ep[1] =3D htonl(tt->local & netmask); + ep[2] =3D htonl(netmask); status =3D DeviceIoControl(tt->hand, TAP_WIN_IOCTL_CONFIG_TUN, ep, sizeof(ep), @@ -6456,7 +6502,7 @@ { in_addr_t ep[2]; ep[0] =3D htonl(tt->local); - ep[1] =3D htonl(tt->remote_netmask); + ep[1] =3D htonl(tt->remote); if (!DeviceIoControl(tt->hand, TAP_WIN_IOCTL_CONFIG_POINT_TO_POINT, ep, sizeof(ep), @@ -6485,11 +6531,11 @@ { if (tt->topology =3D=3D TOP_SUBNET) { - ep[2] =3D dhcp_masq_addr(tt->local, tt->remote_netmask, tt->op= tions.dhcp_masq_custom_offset ? tt->options.dhcp_masq_offset : 0); + ep[2] =3D dhcp_masq_addr(tt->local, netbits_to_netmask(tt->net= bits), tt->options.dhcp_masq_custom_offset ? tt->options.dhcp_masq_offset := 0); } else { - ep[2] =3D htonl(tt->remote_netmask); + ep[2] =3D htonl(tt->remote); } } else diff --git a/src/openvpn/tun.h b/src/openvpn/tun.h index 33b9552..68aa3b8 100644 --- a/src/openvpn/tun.h +++ b/src/openvpn/tun.h @@ -188,7 +188,8 @@ /* ifconfig parameters */ in_addr_t local; - in_addr_t remote_netmask; + in_addr_t remote; + int netbits; struct in6_addr local_ipv6; struct in6_addr remote_ipv6; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/739?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: Iae04ad8715e40dfc76475c2c5b9a766c9604efc9 Gerrit-Change-Number: 739 Gerrit-PatchSet: 2 Gerrit-Owner: ralf_lici Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --ezz0ITQCaPo= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

ralf_lici uploaded patch set #2 to this change.

<= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/739?usp=3Demail">View Chang= e

Support CIDR on options and extend netbits usage

Add support f= or CIDR notation on all suitable options (client-nat,
ifconfig, ifconfig= -pull, ifconfig-push, ifconfig-push-constraint,
iroute, route, server, s= erver-bridge). Anyway push of ifconfig is done
without CIDR in order to = support older peers. Also netmask env vars have
been preserved while als= o introducing the corresponding netbits vars.

Some of the internal l= ogic has also been modified to use netbits instead
of netmasks.

C= hange-Id: Iae04ad8715e40dfc76475c2c5b9a766c9604efc9
Signed-off-by: Ralf = Lici <ralf@...2726...>
---
M src/openvpn/basic.h
M src/openv= pn/clinat.c
M src/openvpn/clinat.h
M src/openvpn/helper.c
M src/op= envpn/init.c
M src/openvpn/manage.c
M src/openvpn/multi.c
M src/op= envpn/options.c
M src/openvpn/options.h
M src/openvpn/pool.c
M src= /openvpn/push.c
M src/openvpn/route.c
M src/openvpn/route.h
M src/= openvpn/socket.c
M src/openvpn/socket.h
M src/openvpn/tun.c
M src/= openvpn/tun.h
17 files changed, 520 insertions(+), 284 deletions(-)
<= br>
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/739/2=
diff --git a/src/openvpn/basic.h b/src/openvpn/basic.h
<= span>index d1075b2..6658008 100644
--- a/src/openvpn/basic.= h
+++ b/src/openvpn/basic.h
@@ -33,5 +33,6 = @@
#define CLEAR(x) memset(&(x), 0, sizeof(x))<= br>
#define IPV4_NETMASK_HOST 0xffffffffU+#define IPV4_NETBITS_HOST 32U=

#endif
diff --git a/src= /openvpn/clinat.c b/src/openvpn/clinat.c
index 2d3b359..f56= 5989 100644
--- a/src/openvpn/clinat.c
+++ = b/src/openvpn/clinat.c
@@ -31,6 +31,7 @@
#= include "proto.h"
#include "socket.h"<= /span>
#include "memdbg.h"
+#include "route.h"

static bool
add_entry(struct client_nat_op= tion_list *dest,
@@ -60,11 +61,11 @@
= for (i =3D 0; i < list->n; ++i)
{<= br> const struct client_nat_entry *e =3D &list->en= tries[i];
- = msg(msglevel, " CNAT[%d] t=3D%d %s/%s/%s",
+ msg(msglevel, " CNAT[%d= ] t=3D%d %s/%d %s",
i,
e->type,

print_in_ad= dr_t(e->network, IA_NET_ORDER, &gc),
- print_in_addr_t(e->netmask, IA_NET= _ORDER, &gc),
+ = e->netbits,
print_in_addr= _t(e->foreign_network, IA_NET_ORDER, &gc));
= }
}
@@ -111,6 +112,7 @@
int msglevel)

{ struct client_nat_entry e;
+ unsigned int netbits =3D 0;
bo= ol ok;

if (!strcmp(type, "snat&= quot;))
@@ -127,19 +129,34 @@
retu= rn;
}

- e.network =3D getaddr(0, network, 0, &ok, NU= LL);
+ e.network = =3D getaddr(0, network, &netbits, 0, &ok, NULL);
= if (!ok)
{
msg(msglevel, &= quot;client-nat: bad network: %s", network);
= return;
}
- e.netmask =3D getaddr(0, netmask, 0, &ok, NULL);- if (!ok)
+ if (netmask)
= {
- msg(msglevel= , "client-nat: bad netmask: %s", netmask);
+ const in_addr_t mask =3D getaddr(= 0, netmask, NULL, 0, &ok, NULL);
+ const int bits =3D netmask_to_netbits2(mask);
+
+ if (!ok || bits <=3D 0)=
+ {
+ msg(msglevel, "clie= nt-nat: bad netmask: %s", netmask);
+ return;
+ }
+ e.netbits =3D bits;
+ }
+ else if (netbits > 0)
+ {
+= e.netbits =3D netbits;
+ }
+ = else
+ {+ msg(msglevel, "= client-nat: netmask or bits must be specified");
= return;
}
- e.foreign_network =3D getaddr(0, foreign_network, 0, &= ok, NULL);
+ e.for= eign_network =3D getaddr(0, foreign_network, NULL, 0, &ok, NULL);
if (!ok)
{
ms= g(msglevel, "client-nat: bad foreign network: %s", foreign_networ= k);
@@ -210,6 +227,7 @@
for (i =3D 0; = i < list->n; ++i)
{
con= st struct client_nat_entry *e =3D &list->entries[i]; /* current NAT = rule */
+ cons= t in_addr_t netmask =3D netbits_to_netmask((int)e->netbits);
<= span> if (e->type ^ direction)

{=
addr =3D *(addr_ptr =3D &h->ip.daddr);=
@@ -231,13 +249,13 @@
to =3D &e-= >foreign_network;
}
- if (((addr & e->= netmask) =3D=3D *from) && !(amask & alog))
+ if (((addr & netmask) =3D=3D= *from) && !(amask & alog))
{ /* pre-adjust IP checksum */
= ADD_CHECKSUM_32(accumulate, addr);

= /* do NAT transform */
- addr =3D (addr & ~e->netmask) | *to;<= br>+ addr =3D (addr = & ~netmask) | *to;

/* po= st-adjust IP checksum */
SUB_CHECKSUM_32(accum= ulate, addr);
diff --git a/src/openvpn/clinat.h b/src/openv= pn/clinat.h
index 94141f5..bebd012 100644
-= -- a/src/openvpn/clinat.h
+++ b/src/openvpn/clinat.h=
@@ -36,7 +36,7 @@
#define CN_DNAT 1
int type;

in_addr_t network;
- in_addr_t netmask;
+ unsigned int netbits; in_addr_t foreign_network;
};
=
diff --git a/src/openvpn/helper.c b/src/openvpn/helper.c<= /span>
index bbdbc04..30f3539 100644
--- a/src/ope= nvpn/helper.c
+++ b/src/openvpn/helper.c
@@= -64,22 +64,21 @@
}

sta= tic const char *
-print= _opt_route(const in_addr_t network, const in_addr_t netmask, struct gc_aren= a *gc)
+print_opt_rou= te(const in_addr_t network, const int netbits, struct gc_arena *gc)<= br> {
struct buffer out =3D alloc_buf_gc(128, gc= );
ASSERT(network);

- if (netmask)
- {
- buf_printf(&out, "route %s %s",

- print_in= _addr_t(network, 0, gc),
- }
- else
+ if (netbits < 0)
{
= buf_printf(&out, "route %s",
= print_in_addr_t(network, 0, gc));
}
= + else
+ {
+ buf_printf(&out, "route %s/%d",
+ pr= int_in_addr_t(network, 0, gc), netbits);
+ }

return BS= TR(&out);
}
@@ -111,27 +110,28 @@
}

static void
-helper_add_route(const in_addr_t ne= twork, const in_addr_t netmask, struct options *o)

+helper_add_route(const in_addr_t network, co= nst int netbits, struct options *o)
{
= rol_check_alloc(o);
add_route_to_option_list(o->ro= utes,
print_in_addr_t(network= , 0, &o->gc),
- = print_in_addr_t(netmask, 0, &o->gc),
+ = print_in_addr_t(netbits_to_netmask(netbits), 0, &o->gc),
NULL,
= NULL);
}

static void

-verify= _common_subnet(const char *opt, const in_addr_t a, const in_addr_t b, const= in_addr_t subnet)
+v= erify_common_subnet(const char *opt, const in_addr_t a, const in_addr_t b, = const unsigned int netbits)
{
struct = gc_arena gc =3D gc_new();
- if ((a & subnet) !=3D (b & subnet))
+ const unsigned int var =3D 32 - netbi= ts;
+ if ((a >&= gt; var) !=3D (b >> var))
{
- msg(M_USAGE, "%s IP addresses= %s and %s are not in the same %s subnet",
+ msg(M_USAGE, "%s IP addresses %s a= nd %s are not in the same /%d subnet",
op= t,
print_in_addr_t(a, 0, &gc),
<= span> print_in_addr_t(b, 0, &gc),

- print_in_addr_t(subnet, 0, &gc)= );
+ netbi= ts);
}
gc_free(&gc); }
@@ -268,9 +268,6 @@

= if (o->server_defined)
{
- int netbits =3D -2;
- bool status =3D false;<= /span>
-
= if (o->client)
{
= msg(M_USAGE, "--server and --client cannot be used together");=
@@ -296,34 +293,24 @@
msg(M_U= SAGE, "--server directive only makes sense with --dev tun or --dev tap= ");
}

- status =3D netmask_to_netbits(o->= ;server_network, o->server_netmask, &netbits);
- if (!status)
- {
- msg(M_USAGE, "--server directive netwo= rk/netmask combination is invalid");
- }
-
- if (n= etbits < 0)
- = {
- msg(M_U= SAGE, "--server directive netmask is invalid");
- }
-
+ if (o->server_netbits < IF= CONFIG_POOL_MIN_NETBITS)
{
= msg(M_USAGE, "--server directive netmask allows for too many hos= t addresses (subnet must be %s or higher)",
= print_netmask(IFCONFIG_POOL_MIN_NETBITS, &gc));
= }

+ const in_addr_t server_netmask =3D netbits_to_netmask((int= )o->server_netbits);
if (dev =3D=3D DEV_TYPE_TU= N)
{
int pool_end_res= erve =3D 4;

- if (netbits > 29)
+ if (o->server_netbits > 29) {
msg(M_USAGE, "= --server directive when used with --dev tun must define a subnet of %s or l= ower",
print_netmask(29, &gc)= );
}

- if (netbits =3D=3D 29)+ if (o->server= _netbits =3D=3D 29)
{
= pool_end_reserve =3D 0;
}
<= span>@@ -342,14 +329,14 @@

{
o->ifconfig_pool_defined =3D true;
o->ifconfig_pool_start =3D o->server_network = + 4;
- = o->ifconfig_pool_end =3D (o->server_network | ~o->server_netmas= k) - pool_end_reserve;
- helper_add_rout= e(o->server_network, o->server_netmask, o);
+ helper_add_route(o->server= _network, o->server_netbits, o);
if (o-= >enable_c2c)
{
- push_option(o, print_opt_r= oute(o->server_network, o->server_netmask, &o->gc), M_USAGE);<= /span>
+ = push_option(o, print_opt_route(o->server_network, o->server_netbits, = &o->gc), M_USAGE);
}
else if (o->topology =3D=3D TOP_NET30)

{
@@ -359,16 +346,16 @@
= else if (o->topology =3D=3D TOP_SUBNET)
= {
o->ifconfig_local =3D print= _in_addr_t(o->server_network + 1, 0, &o->gc);
- o->ifconfig_remote_net= mask =3D print_in_addr_t(o->server_netmask, 0, &o->gc);+ o->ifconfi= g_remote_netmask =3D print_in_addr_t(server_netmask, 0, &o->gc);


if (!(o->server_flags &a= mp; SF_NOPOOL))
{
= o->ifconfig_pool_defined =3D true;
= o->ifconfig_pool_start =3D o->server_network + 2;=
- o->if= config_pool_end =3D (o->server_network | ~o->server_netmask) - 1;

+ o-&= gt;ifconfig_pool_end =3D (o->server_network | ~server_netmask) - 1;
ifconfig_pool_verify_range(M_USAGE, o->= ifconfig_pool_start, o->ifconfig_pool_end);
= }
- = o->ifconfig_pool_netmask =3D o->server_netmask;
+ o->ifconfig_pool_netb= its =3D o->server_netbits;

= push_option(o, print_opt_route_gateway(o->server_network + 1, &= ;o->gc), M_USAGE);
if (!o->route_def= ault_gateway)
@@ -393,7 +380,7 @@
= }
else if (dev =3D=3D DEV_TYPE_TAP)
{

- = if (netbits > 30)
+ if (o->server_netbits > 30)
= {
msg(M_USAGE, "--server directiv= e when used with --dev tap must define a subnet of %s or lower",
print_netmask(30, &gc));
@@ -402,16 +389,16 @@

o->mode =3D MODE_SE= RVER;
o->tls_server =3D true;
o->ifconfig_local =3D print_in_addr_t(o->server_netwo= rk + 1, 0, &o->gc);
- o->ifconfig_remote_netmask =3D print_in_addr_t(o->se= rver_netmask, 0, &o->gc);
+ o->ifconfig_remote_netmask =3D print_in_addr_t(= server_netmask, 0, &o->gc);

= if (!(o->server_flags & SF_NOPOOL))
= {
o->ifconfig_pool_defined =3D true= ;
o->ifconfig_pool_start =3D o->serv= er_network + 2;
- = o->ifconfig_pool_end =3D (o->server_network | ~o->server= _netmask) - 1;
+ = o->ifconfig_pool_end =3D (o->server_network | ~server_netm= ask) - 1;
ifconfig_pool_verify_range(M_USA= GE, o->ifconfig_pool_start, o->ifconfig_pool_end);
= }
- = o->ifconfig_pool_netmask =3D o->server_netmask;
+ o->ifconfig_pool_netbit= s =3D o->server_netbits;

= push_option(o, print_opt_route_gateway(o->server_network + 1, &o->= ;gc), M_USAGE);
}
@@ -425,7 +412,7= @@
{
o->push_ifco= nfig_constraint_defined =3D true;
o->push_i= fconfig_constraint_network =3D o->server_network;
- o->push_ifconfig_constraint_= netmask =3D o->server_netmask;
+ o->push_ifconfig_constraint_netbits =3D o->= server_netbits;
}
}
@@ -478,9 +465,9 @@

if (o->server_bridge_defined)
{

- verify_commo= n_subnet("--server-bridge", o->server_bridge_ip, o->server_= bridge_pool_start, o->server_bridge_netmask);
- verify_common_subnet("--server-= bridge", o->server_bridge_pool_start, o->server_bridge_pool_end,= o->server_bridge_netmask);
- verify_common_subnet("--server-bridge", o-&g= t;server_bridge_ip, o->server_bridge_pool_end, o->server_bridge_netma= sk);
+ ver= ify_common_subnet("--server-bridge", o->server_bridge_ip, o-&g= t;server_bridge_pool_start, o->server_bridge_netbits);
+ verify_common_subnet(&quo= t;--server-bridge", o->server_bridge_pool_start, o->server_bridg= e_pool_end, o->server_bridge_netbits);
+ verify_common_subnet("--server-bridg= e", o->server_bridge_ip, o->server_bridge_pool_end, o->server= _bridge_netbits);
}

o->mode =3D MODE_SERVER;
@@ -492,7 +479,7 @= @
o->ifconfig_pool_start =3D o->server_b= ridge_pool_start;
o->ifconfig_pool_end =3D = o->server_bridge_pool_end;
ifconfig_pool_ve= rify_range(M_USAGE, o->ifconfig_pool_start, o->ifconfig_pool_end);

- o->ifconf= ig_pool_netmask =3D o->server_bridge_netmask;
+ o->ifconfig_pool_netbits =3D o-= >server_bridge_netbits;
push_option(o, prin= t_opt_route_gateway(o->server_bridge_ip, &o->gc), M_USAGE);
}
else if (o->server_bridge= _proxy_dhcp && !(o->server_flags & SF_NO_PUSH_ROUTE_GATEWAY)= )
diff --git a/src/openvpn/init.c b/src/openvpn/init.c
index dd56961..7f72f8d 100644
--- a/src/openvpn= /init.c
+++ b/src/openvpn/init.c
@@ -115,6 = +115,7 @@
const char *dev_type,
int tun_mtu,

const char *ifconf= ig_local,
+ = const int ifconfig_netbits,
const char *ifco= nfig_remote,
const char *context,
const char *signal_text,

@@ -144,7 +145,14= @@
}
if (!ifconfig_remote)
{
- = ifconfig_remote =3D "";
+ if (ifconfig_netbits > 0)
+ {
+ ifconfig_remote =3D print_in_addr_t(ne= tbits_to_netmask(ifconfig_netbits), 0, &gc);
+ }
+ else
+ {
+ ifconfig_remote =3D "";
+ }
}
if (!context)

{
@@ -1948,7 +195= 6,8 @@
dev_type_string(c->options.d= ev, c->options.dev_type),
c->c2.= frame.tun_mtu,
print_in_addr_t(c->c= 1.tuntap->local, IA_EMPTY_IF_UNDEF, &gc),
- print_in_addr_t(c->c1.tun= tap->remote_netmask, IA_EMPTY_IF_UNDEF, &gc),
+ c->c1.tuntap->n= etbits,
+ = print_in_addr_t(c->c1.tuntap->remote, IA_EMPTY_IF_UNDEF, &= ;gc),
"init",
NULL,
"up&= quot;,
@@ -1988,7 +1997,8 @@
= dev_type_string(c->options.dev, c->options.dev_type),

c->c2.frame.tun_mtu,
print_in_addr_t(c->c1.tuntap->local, IA_EM= PTY_IF_UNDEF, &gc),
- print_in_addr_t(c->c1.tuntap->remote_netmas= k, IA_EMPTY_IF_UNDEF, &gc),
+ c->c1.tuntap->netbits,
= + print_= in_addr_t(c->c1.tuntap->remote, IA_EMPTY_IF_UNDEF, &gc), "restart",
= NULL,
"up= ",
@@ -2045,7 +2055,8 @@
struct g= c_arena gc =3D gc_new();
const char *tuntap_actual =3D= string_alloc(c->c1.tuntap->actual_name, &gc);
= const in_addr_t local =3D c->c1.tuntap->local;
- const in_addr_t remote_netmask =3D c-= >c1.tuntap->remote_netmask;
+ const int netbits =3D c->c1.tuntap->netbits;=
+ const in_addr_t remote= =3D c->c1.tuntap->remote;
unsigned long adapter= _index =3D 0;
#ifdef _WIN32
adapter_i= ndex =3D c->c1.tuntap->adapter_index;
@@ -2077,7 +208= 8,8 @@
NULL,
= c->c2.frame.tun_mtu,
= print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
- print_in_ad= dr_t(remote_netmask, IA_EMPTY_IF_UNDEF, &gc),
+ netbits,
+ print_in= _addr_t(remote, IA_EMPTY_IF_UNDEF, &gc),
= "init",
signa= l_description(c->sig->signal_received,
= c->sig->signal_text),
@= @ -2107,7 +2119,8 @@
NULL,
<= span> c->c2.frame.tun_mtu,
= print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
<= span style=3D"color: hsl(0, 100%, 40%);">- print_in_addr= _t(remote_netmask, IA_EMPTY_IF_UNDEF, &gc),
+ netbits,
+ print_in_addr_t(re= mote, IA_EMPTY_IF_UNDEF, &gc),
&qu= ot;init",
signal_description(c-&g= t;sig->signal_received,
= c->sig->signal_text),
@@ -2137,7 +2150,8 @@
NULL,
= c->c2.frame.tun_mtu,
= print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
- print_in_addr_t(rem= ote_netmask, IA_EMPTY_IF_UNDEF, &gc),
+ netbits,
+ print_in_addr_t(= remote, IA_EMPTY_IF_UNDEF, &gc),
= "restart",
signal_des= cription(c->sig->signal_received,
= c->sig->signal_text),
diff -= -git a/src/openvpn/manage.c b/src/openvpn/manage.c
index 05= b5a1a..a88ee5f 100644
--- a/src/openvpn/manage.c
= +++ b/src/openvpn/manage.c
@@ -554,7 +554,7 @@=
{
/* IP:port specified */
bool status;
- const in_addr_t addr =3D getaddr(GETADDR_HOS= T_ORDER|GETADDR_MSG_VIRT_OUT, p1, 0, &status, NULL);
+ const in_addr_t addr =3D g= etaddr(GETADDR_HOST_ORDER|GETADDR_MSG_VIRT_OUT, p1, NULL, 0, &status, N= ULL);
if (status)
= {
const int port =3D atoi(p2);
= @@ -2803,6 +2803,7 @@
"untrusted_ip=3D&= quot;,
"ifconfig_local=3D",
"ifconfig_netmask=3D",

+ "ifconfig_netbits=3D", "daemon_start_time=3D",
= "daemon_pid=3D",
"dev=3D",

diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c=
index 0509911..aba1a0f 100644
--- a/src/openvpn/m= ulti.c
+++ b/src/openvpn/multi.c
@@ -1433,7= +1433,8 @@
const struct options *o =3D &c->opt= ions;
if (o->push_ifconfig_constraint_defined &= & c->c2.push_ifconfig_defined)
{
- return (o->push_ifconfig_= constraint_netmask & c->c2.push_ifconfig_local) =3D=3D o->push_if= config_constraint_network;
+ const unsigned int var =3D 32 - o->push_ifconfig_constrai= nt_netbits;
+ = return (c->c2.push_ifconfig_local >> var) =3D=3D (o->push_ifcon= fig_constraint_network >> var);
}
else
{
@@ -1512,10 +1513,10 @@<= /span>
mi->context.c2.push_ifconfig_local =3D = remote;
if (tunnel_type =3D=3D DEV_TYPE_TA= P || (tunnel_type =3D=3D DEV_TYPE_TUN && tunnel_topology =3D=3D TOP= _SUBNET))
{
- mi->context.c2.push_ifconfig_= remote_netmask =3D mi->context.options.ifconfig_pool_netmask;
= + mi->con= text.c2.push_ifconfig_remote_netmask =3D netbits_to_netmask((int)mi->con= text.options.ifconfig_pool_netbits);
i= f (!mi->context.c2.push_ifconfig_remote_netmask)
= {
- = mi->context.c2.push_ifconfig_remote_netmask =3D mi->= ;context.c1.tuntap->remote_netmask;
+ mi->context.c2.push_ifconfig_= remote_netmask =3D netbits_to_netmask((int)mi->context.c1.tuntap->net= bits);
}
= }
else if (tunnel_type =3D=3D DEV_TYPE= _TUN)
@@ -2425,15 +2426,14 @@
{=
const char *ifconfig_constraint_network =3D
print_in_addr_t(mi->context.options.push_ifconfig_const= raint_network, 0, &gc);

- const char *ifconfig_constraint_netmask =3D
- print_in_addr_t(mi->cont= ext.options.push_ifconfig_constraint_netmask, 0, &gc);
=
/* JYFIXME -- this should cause the connection t= o fail */
msg(D_MULTI_ERRORS, "MULTI ERROR: p= rimary virtual IP for %s (%s) "
- "violates tunnel network/netmask constraint= (%s/%s)",
+ = "violates tunnel network/subnet constraint (%s/%d)",

multi_instance_string(mi, false, &gc),<= br> print_in_addr_t(mi->context.c2.push_ifconfig_local= , 0, &gc),
- = ifconfig_constraint_network, ifconfig_constraint_netmask);
<= span style=3D"color: hsl(120, 100%, 40%);">+ ifconfig_constraint= _network,
+ = mi->context.options.push_ifconfig_constraint_netbits);
}

/*
diff --gi= t a/src/openvpn/options.c b/src/openvpn/options.c
index 649= f48b..b0fabe1 100644
--- a/src/openvpn/options.c
= +++ b/src/openvpn/options.c
@@ -1075,7 +1075,7 @@

#endif /* ifndef _WIN32 */

= static in_addr_t
-get_= ip_addr(const char *ip_string, int msglevel, bool *error)
+get_ip_addr(const char *ip_string, un= signed int *netbits, int msglevel, bool *error)
{ unsigned int flags =3D GETADDR_HOST_ORDER;
= bool succeeded =3D false;
@@ -1086,7 +1086,7 @@ flags |=3D GETADDR_FATAL;
}
=
- ret =3D ge= taddr(flags, ip_string, 0, &succeeded, NULL);
+ ret =3D getaddr(flags, ip_string, netbits= , 0, &succeeded, NULL);
if (!succeeded && = error)
{
*error =3D true;

@@ -1333,7 +1333,7 @@
if (ip_addr_dot= ted_quad_safe(parm)) /* FQDN -- IP address only */
= {
bool error =3D false;
- const in_addr_t addr =3D get_ip= _addr(parm, msglevel, &error);
+ const in_addr_t addr =3D get_ip_addr(parm, NULL,= msglevel, &error);
if (!error)
= {
array[(*len)++] =3D a= ddr;
@@ -1513,12 +1513,10 @@
{
<= span> struct gc_arena gc =3D gc_new();

- msg(D_SHOW_PARMS, " server= _network =3D %s", print_in_addr_t(o->server_network, 0, &gc));<= /span>
- msg(D_SHOW_PARMS, = " server_netmask =3D %s", print_in_addr_t(o->server_netmask, = 0, &gc));
+ ms= g(D_SHOW_PARMS, " server_network =3D %s/%d", print_in_addr_t(o-&= gt;server_network, 0, &gc), o->server_netbits);
= msg(D_SHOW_PARMS, " server_network_ipv6 =3D %s", print_in6_addr= (o->server_network_ipv6, 0, &gc) );
SHOW_INT(se= rver_netbits_ipv6);
- = msg(D_SHOW_PARMS, " server_bridge_ip =3D %s", print_in_addr_t(= o->server_bridge_ip, 0, &gc));
- msg(D_SHOW_PARMS, " server_bridge_netmask =3D %s&= quot;, print_in_addr_t(o->server_bridge_netmask, 0, &gc));+ msg(D_SHOW_PARMS, " = server_bridge_ip =3D %s/%d", print_in_addr_t(o->server_bridge_ip, = 0, &gc), o->server_bridge_netbits);
msg(D_SHOW_= PARMS, " server_bridge_pool_start =3D %s", print_in_addr_t(o->= ;server_bridge_pool_start, 0, &gc));
msg(D_SHOW_PA= RMS, " server_bridge_pool_end =3D %s", print_in_addr_t(o->ser= ver_bridge_pool_end, 0, &gc));
if (o->push_list= .head)
@@ -1534,9 +1532,8 @@
}
}
SHOW_BOOL(ifconfig_pool_defined);<= /span>
- msg(D_SHOW_PARMS, = " ifconfig_pool_start =3D %s", print_in_addr_t(o->ifconfig_po= ol_start, 0, &gc));
+ msg(D_SHOW_PARMS, " ifconfig_pool_start =3D %s/%d", prin= t_in_addr_t(o->ifconfig_pool_start, 0, &gc), o->ifconfig_pool_net= bits);
msg(D_SHOW_PARMS, " ifconfig_pool_end =3D= %s", print_in_addr_t(o->ifconfig_pool_end, 0, &gc));- msg(D_SHOW_PARMS, " i= fconfig_pool_netmask =3D %s", print_in_addr_t(o->ifconfig_pool_netm= ask, 0, &gc));
SHOW_STR(ifconfig_pool_persist_file= name);
SHOW_INT(ifconfig_pool_persist_refresh_freq);
SHOW_BOOL(ifconfig_ipv6_pool_defined);
= @@ -1597,14 +1594,15 @@
int msglevel)=
{
struct iroute *ir;
+ unsigned int netbits =3D 0;
ALLOC_OBJ_GC(ir, struct iroute, &o->gc= );
- ir->network = =3D getaddr(GETADDR_HOST_ORDER, network_str, 0, NULL, NULL);
- ir->netbits =3D 32; = /* host route if no netmask given */

+ ir->network =3D getaddr(GETADDR_HOST_ORDER, networ= k_str, &netbits, 0, NULL, NULL);
+ ir->netbits =3D 32; /* host route if no n= etmask or netbits given */

if (netma= sk_str)
{
- const in_addr_t netmask =3D getaddr(GETADDR_HOST_ORDER, ne= tmask_str, 0, NULL, NULL);
+ const in_addr_t netmask =3D getaddr(GETADDR_HOST_ORDER, netm= ask_str, NULL, 0, NULL, NULL);
ir->netbits =3D = netmask_to_netbits2(netmask);

if= (ir->netbits < 0)
@@ -1615,6 +1613,10 @@
<= span> return;

}
}=
+ else if (netbit= s)
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ ir->netbits =3D (int= )netbits;

+ }


ir->next =3D o->iroutes; o->iroutes =3D ir;
@@ -2631,9 +2633,9 @@
{
msg(M_USAGE, "--c= onnect-freq only works with --mode server --proto udp. Try --max-clients i= nstead.");
}
- if (!(dev =3D=3D DEV_TYPE_TAP || (dev =3D=3D D= EV_TYPE_TUN && options->topology =3D=3D TOP_SUBNET)) && = options->ifconfig_pool_netmask)
+ if (!(dev =3D=3D DEV_TYPE_TAP || (dev =3D=3D DEV_TYP= E_TUN && options->topology =3D=3D TOP_SUBNET)) && option= s->ifconfig_pool_netbits > 0)
{
- msg(M_USAGE, "The= third parameter to --ifconfig-pool (netmask) is only valid in --dev tap mo= de");

+ = msg(M_USAGE, "The netbits parameter to --ifconfig-pool is only vali= d in --dev tap mode");
}
= if (options->routes && (options->routes->flags & = RG_ENABLE))
{
@@ -6092,11 +6094,29= @@
iproute_path =3D p[1];
}<= /span>
#endif
- else if (streq(p[0], "ifconfig") && p[1] &&= p[2] && !p[3])
+ else if (streq(p[0], "ifconfig") && p[1] &&am= p; !p[3])
{
+ bool error =3D false;
+
VERIFY_PERMISSION(OPT_P_UP= );
- if (ip_or_d= ns_addr_safe(p[1], options->allow_pull_fqdn) && ip_or_dns_addr_s= afe(p[2], options->allow_pull_fqdn)) /* FQDN -- may be DNS name */
+ if (!p[2]) /* p[1= ] must be in CIDR format */
{
+ unsigned int netbits =3D 0;<= /span>
+ get_ip_a= ddr(p[1], &netbits, msglevel, &error);
+ if (error || !netbits)
+ {

+ msg(msglevel, "ifc= onfig parm '%s' must be a valid address/bits", p[1]);+ goto err;
+ }+ options->ifco= nfig_local =3D strtok(p[1], "/");
+ /* convert netbits to netmask to unify = the storage as string
+ * representation of in_addr_t variables and simplify parsing= */
+ opti= ons->ifconfig_remote_netmask =3D print_in_addr_t(netbits_to_netmask((int= )netbits), 0, &options->gc);
+ }
+ else if (ip_or_dns_addr_safe(p[1], options->allow_pull_fqd= n) && ip_or_dns_addr_safe(p[2], options->allow_pull_fqdn)) /* FQ= DN -- may be DNS name */
+ {
+ = /* don't suggest CIDR usage here since we don't know if it&#= 39;s a
+ = * remote address or a netmask */
options->i= fconfig_local =3D p[1];
options->ifconfig_r= emote_netmask =3D p[2];
}
@@ -6999= ,35 +7019,62 @@
VERIFY_PERMISSION(OPT_P_PERSIST_IP= );
options->persist_remote_ip =3D true;<= br> }
- el= se if (streq(p[0], "client-nat") && p[1] && p[2] = && p[3] && p[4] && !p[5])
+ else if (streq(p[0], "client-nat&quo= t;) && p[1] && p[2] && p[3] && !p[5])
{
+ = size_t i =3D 1;
+ const char *type =3D p[i++];
+ const char *network =3D p[i++];
+ const char *netmask =3D p[i+= 1] ? p[i++] : NULL;
+= const char *foreign_network =3D p[i];
+
VERIFY_PERMISSION(OPT_P= _ROUTE);
cnol_check_alloc(options);
- add_client_nat_to_option_lis= t(options->client_nat, p[1], p[2], p[3], p[4], msglevel);

+ add_client_nat_to_option_l= ist(options->client_nat, type, network, netmask, foreign_network, msglev= el);

}
else if (streq(p[0], "= ;route") && p[1] && !p[5])
{
+ size_t i =3D 1;<= /span>
+ char *cidr = =3D strchr(p[i], '/');
+ if (cidr && !no_more_than_n_args(msglevel, p, 4,= NM_QUOTE_HINT))
+ = {
+ g= oto err;
+ }
+
= VERIFY_PERMISSION(OPT_P_ROUTE);
rol_check_all= oc(options);
if (pull_mode)
= {
- if (!= ip_or_dns_addr_safe(p[1], options->allow_pull_fqdn) && !is_speci= al_addr(p[1])) /* FQDN -- may be DNS name */
+
+ const char *network =3D strtok(p[i++], "/"); /*= this modifies p[1] */
- msg(msglevel, "route parameter network/IP '%s= 9; must be a valid address", p[1]);
+ msg(msglevel, "route parameter ne= twork/IP '%s' must be a valid address or address/bits", p[1]);=
goto err;
}<= /span>
- if (p[2] &= amp;& !ip_addr_dotted_quad_safe(p[2])) /* FQDN -- must be IP address */=
+ if (!ci= dr && p[i])
{
- msg(msglevel, "route para= meter netmask '%s' must be an IP address", p[2]);
- goto err;
+ if (!ip_addr= _dotted_quad_safe(p[i++])) /* FQDN -- must be IP address */
+ {

+ msg(msglevel, &qu= ot;route parameter netmask '%s' must be an IP address", p[2]);=
+ = goto err;
+ = }
}
- if (p[3] && !ip_or_dns_addr_safe(p[3]= , options->allow_pull_fqdn) && !is_special_addr(p[3])) /* FQDN -= - may be DNS name */
= + if (p[i] && !ip_or_dns_addr_safe(p[i], options->all= ow_pull_fqdn) && !is_special_addr(p[i])) /* FQDN -- may be DNS name= */
{
- msg(msglevel, "route parameter gateway &#= 39;%s' must be a valid address", p[3]);
+ msg(msglevel, "route para= meter gateway '%s' must be a valid address", p[i]);
= goto err;
}
<= span> }

- = add_route_to_option_list(options->routes, p[1], p[2], p[3], p[4]);
+
+ if (cidr)
+ {
+ *cidr =3D (char)('/'); /* resto= re the original CIDR notation for p[1] */
+ add_route_to_option_list(options->rout= es, p[1], NULL, p[2], p[3]);
+ }
+ = else
+ = {
+ add_ro= ute_to_option_list(options->routes, p[1], p[2], p[3], p[4]);
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

= }
else if (streq(p[0], "route-ipv6") &&a= mp; p[1] && !p[4])
{
@@ -7348,= 33 +7395,56 @@
VERIFY_PERMISSION(OPT_P_GENERAL);
options->occ =3D false;
}
- else if (streq(p[0]= , "server") && p[1] && p[2] && !p[4])
+ else if (streq(p[0]= , "server") && p[1] && !p[4])
= {
const int lev =3D M_WARN;
= bool error =3D false;
- in_addr_t network, netmask;
+ in_addr_t network;
+ unsigned int netbits =3D 0;
<= span style=3D"color: hsl(120, 100%, 40%);">+ size_t i =3D 1;
<= br>
VERIFY_PERMISSION(OPT_P_GENERAL);
- network =3D get_ip_= addr(p[1], lev, &error);
- netmask =3D get_ip_addr(p[2], lev, &error);
- if (error || !network || != netmask)

+ net= work =3D get_ip_addr(p[i++], &netbits, lev, &error);
+ if (netbits)

+ {

+ if (!no_more_than_n_args(msgl= evel, p, 3, NM_QUOTE_HINT))
+ {
+ goto err;
+ }
+ }
+ = else
+ {
+ const in_addr= _t netmask =3D get_ip_addr(p[i++], NULL, lev, &error);
+ const int nb =3D netmask= _to_netbits2(netmask);
+ {
+ netbits =3D nb;
+ }
+ else
+ {
+ msg(M_USAGE, "--server directive n= etwork/netmask combination is invalid");
+ goto err;
+ }
+ }
+
+ = if (error || !network || !netbits)
{=
msg(msglevel, "error parsing --server parameter= s");
goto err;
}=
options->server_defined =3D true;
options->server_network =3D network;

- options->server_netmask =3D netm= ask;
+ options= ->server_netbits =3D netbits;

- if (p[3])
+ if (p[i])
{
- if (streq(p[3= ], "nopool"))
+ if (streq(p[i], "nopool"))
= {
options->server_flags |=3D SF_= NOPOOL;
}
else
{
- msg(msglevel, "error parsing --server: %s is no= t a recognized flag", p[3]);
+ msg(msglevel, "error parsing --server: %= s is not a recognized flag", p[i]);
g= oto err;
}
}@@ -7403,25 +7473,42 @@
options->server= _network_ipv6 =3D network;
options->server_netb= its_ipv6 =3D netbits;
}
- else if (streq(p[0], "server-bridge") = && p[1] && p[2] && p[3] && p[4] && = !p[5])
+ else if (= streq(p[0], "server-bridge") && p[1] && p[2] &= ;& p[3] && !p[5])
{
= const int lev =3D M_WARN;
bool error =3D false;=
- in_addr_t ip,= netmask, pool_start, pool_end;
+ in_addr_t ip, pool_start, pool_end;
+ unsigned int netbits =3D 0;
+ size_t i =3D 1;=

VERIFY_PERMISSION(OPT_P_GENERAL= );
- ip =3D get_= ip_addr(p[1], lev, &error);
- netmask =3D get_ip_addr(p[2], lev, &error);- pool_start =3D get_ip_a= ddr(p[3], lev, &error);
- pool_end =3D get_ip_addr(p[4], lev, &error);
- if (error || !ip || !netma= sk || !pool_start || !pool_end)

+ ip =3D get_ip_addr(p[i++], &netbits, lev, &erro= r);
+ if (netb= its)
+ {
+ if (!no_more_= than_n_args(msglevel, p, 4, NM_QUOTE_HINT))
+ {
+ goto err;
+ }
+ }
+ else
= + {
+ = const in_addr_t netmask =3D get_ip_addr(p[i++], NULL, lev, &error);<= /span>
+ const in= t nb =3D netmask_to_netbits2(netmask);
+ if (netmask && nb > 0)
= + {
+ netbits =3D nb;
+ }
= + }
+ pool_start =3D get_ip_addr(p[i++= ], NULL, lev, &error);
+ pool_end =3D get_ip_addr(p[i], NULL, lev, &error);
+ if (error || !ip = || !netbits || !pool_start || !pool_end)
{<= br> msg(msglevel, "error parsing --server-bridge par= ameters");
goto err;
= }
options->server_bridge_defined =3D true;=
options->server_bridge_ip =3D ip;
- options->server_bridge_= netmask =3D netmask;

= + options->server_bridge_netbits =3D netbits;
= options->server_bridge_pool_start =3D pool_start;
= options->server_bridge_pool_end =3D pool_end;
= }
@@ -7456,15 +7543,26 @@
{=
const int lev =3D M_WARN;
bool = error =3D false;
- = in_addr_t start, end, netmask =3D 0;
+ in_addr_t start, end;
+ unsigned int netbits =3D 0;
VERIFY_PERMISSION(OPT_P_GENERAL);<= br>- start =3D get_ip_addr= (p[1], lev, &error);
- if (p[3])
+ start =3D get_ip_addr(p[1], &net= bits, lev, &error);
+ end =3D get_ip_addr(p[2], NULL, lev, &error);
+ if (!netbits && p= [3])

{
- netmask =3D get_ip_addr(p[3], lev, &error);
+ const in_addr= _t netmask =3D get_ip_addr(p[3], NULL, lev, NULL);
+ const int nb =3D netmask_to_netb= its2(netmask);
+ = if (netmask && nb >=3D 0)
+ {
+ netbits =3D nb;
+ }
}
+ else if (netbits= && !no_more_than_n_args(msglevel, p, 3, NM_QUOTE_HINT))
= + {
+ goto err;
+ }
+
if (error)
<= span> {

msg(msglevel, "error pars= ing --ifconfig-pool parameters");
@@ -7478,10 +7576,7 = @@
options->ifconfig_pool_defined =3D true;
options->ifconfig_pool_start =3D start;
= options->ifconfig_pool_end =3D end;
- if (netmask)
- {
- options->ifconfig_pool_netmask =3D netma= sk;
- }+ options->ifconfig= _pool_netbits =3D netbits;
}
else= if (streq(p[0], "ifconfig-pool-persist") && p[1] &&a= mp; !p[3])
{
@@ -7802,45 +7897,76 @@
VERIFY_PERMISSION(OPT_P_INSTANCE);
= option_iroute_ipv6(options, p[1], msglevel);
= }
- else if (streq(p= [0], "ifconfig-push") && p[1] && p[2] && = !p[4])
+ else if (= streq(p[0], "ifconfig-push") && p[1] && !p[4])
{
- = in_addr_t local, remote_netmask;
+ in_addr_t local, remote_netmask =3D 0;+ unsigned int netbits= ;
+ size_t i = =3D 1;

VERIFY_PERMISSION(OPT_P_I= NSTANCE);
- loca= l =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[1], 0, NULL, NULL);
- remote_netmask = =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[2], 0, NULL, NULL);
+ local =3D getaddr= (GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[i++], &netbits, 0, NULL, NULL);<= /span>
+ if (netbits)= /* convert netbits to netmask */
+ {
+ if (!no_more_than_n_args(msglevel, p, 3, NM_QUOTE_HINT))
+ {+ goto err;
+ }+ remote_netmask = =3D netbits_to_netmask((int)netbits);
+ }
+ else if (p[i])
+ {
= + /* don't suggest CIDR usage here since we don't know i= f it's a
+ = * remote address or a netmask */
+ remote_netmask =3D getaddr(GETADDR_HOST_ORDE= R|GETADDR_RESOLVE, p[i++], NULL, 0, NULL, NULL);
+ }
+
if (local && remote_ne= tmask)
{
options->= push_ifconfig_defined =3D true;
options->pu= sh_ifconfig_local =3D local;
options->push_= ifconfig_remote_netmask =3D remote_netmask;
- if (p[3])
+ if (p[i])
= {
- opti= ons->push_ifconfig_local_alias =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RE= SOLVE, p[3], 0, NULL, NULL);
+ options->push_ifconfig_local_alias =3D getaddr(= GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[i], NULL, 0, NULL, NULL);
<= span> }

}
els= e
{
- msg(msglevel, "cannot parse --ifconfig-push address= es");
+ = msg(msglevel, "cannot parse --ifconfig-push parameters");
goto err;
}
}

- else if = (streq(p[0], "ifconfig-push-constraint") && p[1] &&am= p; p[2] && !p[3])
+ else if (streq(p[0], "ifconfig-push-constraint") &&= amp; p[1] && !p[3])
{
- in_addr_t network, netmask;
= + in_addr_t network;
+ unsigned int ne= tbits =3D 0;
+ = size_t i =3D 1;

VERIFY_PERMISSI= ON(OPT_P_GENERAL);
- = network =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[1], 0, NULL,= NULL);
- netmas= k =3D getaddr(GETADDR_HOST_ORDER, p[2], 0, NULL, NULL);
- if (network && netmask)
+ network =3D g= etaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[i++], &netbits, 0, NULL, N= ULL);
+ if (!n= etbits && p[i])
+ {
+ = const in_addr_t netmask =3D getaddr(GETADDR_HOST_ORDER, p[2], NULL, 0= , NULL, NULL);
+ = const int nb =3D netmask_to_netbits2(netmask);
+ if (netmask && nb >= ; 0)
+ {
+ netbi= ts =3D nb;
+ = }
+ }
+ else if (netbits = && !no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HINT))
<= span style=3D"color: hsl(120, 100%, 40%);">+ {

+ goto err;
+ }
+
+ if (network && netbits)
{
options->push_ifconfig_constraint_defined = =3D true;
options->push_ifconfig_constraint= _network =3D network;
-= options->push_ifconfig_constraint_netmask =3D netmask;
+ options->p= ush_ifconfig_constraint_netbits =3D netbits;
}
else
{
- msg(msglevel, "cannot pars= e --ifconfig-push-constraint addresses");
+ msg(msglevel, "cannot parse --i= fconfig-push-constraint parameters");
got= o err;
}
}
di= ff --git a/src/openvpn/options.h b/src/openvpn/options.h
in= dex f608cb8..88de24c 100644
--- a/src/openvpn/options.h
+++ b/src/openvpn/options.h
@@ -450,7 +450,7 @= @
const char *tmp_dir;
bool serve= r_defined;
in_addr_t server_network;
- in_addr_t server_netmask;+ unsigned int server_netbi= ts;
bool server_ipv6_defined; = /* IPv6 */
struct in6_addr server_network_ipv6; = /* IPv6 */
unsigned int server_netbits_ipv6= ; /* IPv6 */
@@ -464,7 +464,7 @@
bool server_bridge_defined;
= in_addr_t server_bridge_ip;
- in_addr_t server_bridge_netmask;
+ unsigned int server_bridge_netbits;<= br> in_addr_t server_bridge_pool_start;
in_a= ddr_t server_bridge_pool_end;

@@ -472,7 += 472,7 @@
bool ifconfig_pool_defined;
= in_addr_t ifconfig_pool_start;
in_addr_t ifconfig_= pool_end;
- in_addr_= t ifconfig_pool_netmask;
+ unsigned int ifconfig_pool_netbits;
const cha= r *ifconfig_pool_persist_filename;
int ifconfig_pool_p= ersist_refresh_freq;

@@ -499,7 +499,7 @@<= /span>
in_addr_t push_ifconfig_local_alias;
= bool push_ifconfig_constraint_defined;
in_addr_t p= ush_ifconfig_constraint_network;
- in_addr_t push_ifconfig_constraint_netmask;
+ unsigned int push_ifconfig_const= raint_netbits;
bool push_ifconfig_ipv4_blocked; = /* IPv4 */
bool push_ifconfig_ipv6_define= d; /* IPv6 */
struct in6_addr push_= ifconfig_ipv6_local; /* IPv6 */
diff --git a/src/= openvpn/pool.c b/src/openvpn/pool.c
index e3c3708..84227b6 = 100644
--- a/src/openvpn/pool.c
+++ b/src/o= penvpn/pool.c
@@ -652,8 +652,8 @@
= if (strlen(ip_buf) > 0)
{
bool v4_ok =3D true;

- in_addr_t addr =3D getaddr(GETADDR_HOST_= ORDER, ip_buf, 0, &v4_ok,
- NULL);
+ in_addr_t addr =3D geta= ddr(GETADDR_HOST_ORDER, ip_buf, NULL, 0,
+ &v4_ok, N= ULL);

if (!v4_ok)=
{
diff --git a/src/openvpn/push.= c b/src/openvpn/push.c
index 6c06374..b8ab470 100644=
--- a/src/openvpn/push.c
+++ b/src/openvpn/push.c=
@@ -1136,9 +1136,20 @@
if= (p[0] && !strcmp(p[0], "route") && !p[3] &&a= mp; o->iroutes)
{
= /* get route parameters */
- bool status1, status2;
<= span style=3D"color: hsl(0, 100%, 40%);">- const in_addr= _t network =3D getaddr(GETADDR_HOST_ORDER, p[1], 0, &status1, NULL);
- cons= t in_addr_t netmask =3D getaddr(GETADDR_HOST_ORDER, p[2] ? p[2] : "255= .255.255.255", 0, &status2, NULL);
+ bool status1, status2 =3D true;=
+ = int netbits =3D -1;
= +
+ = /* get netbits from CIDR or convert netmask to netbits */
+ const in_addr_t= network =3D getaddr(GETADDR_HOST_ORDER, p[1], (unsigned int *)&netbits= , 0, &status1, NULL);

+ if (p[2])
+ {
+ const in_addr_t netmask =3D getad= dr(GETADDR_HOST_ORDER, p[2], NULL, 0, &status2, NULL);
+ netbits =3D = netmask_to_netbits2(netmask);
+ }
+ if (netbits <=3D 0)
+ {
+ netbits =3D 32= ;
+ = }

/* did route par= ameters parse correctly? */
if (status= 1 && status2)
@@ -1148,7 +1159,7 @@
/* does route match an iroute? */

for (ir =3D o->iroutes; ir !=3D NULL; ir =3D i= r->next)

{
- if (network = =3D=3D ir->network && netmask =3D=3D netbits_to_netmask(ir->n= etbits >=3D 0 ? ir->netbits : 32))
+ if (network =3D=3D ir->= network && netbits =3D=3D (ir->netbits >=3D 0 ? ir->netbit= s : 32))
{
= enable =3D false;
= break;
diff --git a/src/openvpn/route= .c b/src/openvpn/route.c
index 91f2032..ee77875 100644
--- a/src/openvpn/route.c
+++ b/src/openvpn/rou= te.c
@@ -189,9 +189,9 @@
route_string(cons= t struct route_ipv4 *r, struct gc_arena *gc)
{
<= span> struct buffer out =3D alloc_buf_gc(256, gc);

- buf_printf(&out, "ROUTE netwo= rk %s netmask %s gateway %s",
+ buf_printf(&out, "ROUTE network %s/%d gateway %= s",
print_in_addr_t(r->network, 0, = gc),
- pr= int_in_addr_t(r->netmask, 0, gc),
+ r->netbits,
= print_in_addr_t(r->gateway, 0, gc)
)= ;
if (r->flags & RT_METRIC_DEFINED)
<= span>@@ -321,11 +321,17 @@

const struct route_o= ption *ro,
const struct route_list *rl)<= br> {
- const = in_addr_t default_netmask =3D IPV4_NETMASK_HOST;
+ const in_addr_t default_netbits =3D IPV4_N= ETBITS_HOST;
bool status;
int ret= ;
struct in_addr special =3D {0};

+ /* separate addres= s and prefix if present */
+ char *network =3D strdup(ro->network);
+ const char *cidr =3D strchr(network, = '/');
+ co= nst char *addr =3D strtok(network, "/");
+ const char *prefix =3D strtok(NULL, &quo= t;/");
+<= br> CLEAR(*r);
r->option =3D ro;
@@ -355,7 +361,7 @@
else
{
ret =3D openvpn_getaddrinfo(GE= TADDR_RESOLVE | GETADDR_WARN_ON_SIGNAL,
- ro->network, NULL, 0= , NULL, AF_INET, network_list);
+ addr, NULL, 0, NULL, AF_INET,= network_list);
}

= status =3D (ret =3D=3D 0);
@@ -365,25 +371,40 @@<= br> goto fail;
}

- /* netmask */+ /* netbits */
<= span>

+ r->net= bits =3D default_netbits;
if (is_route_parm_defined(ro= ->netmask))
{
- r->netmask =3D getaddr(
+ const in_addr_t netmask =3D getaddr(=
GETADDR_HOST_ORDER
= | GETADDR_WARN_ON_SIGNAL,
ro->netmask,
+ NULL,
0,
&status,
NULL);
- if (!status)
+ const int netbits =3D netmask_to_netbits2(netmask);
+
+ if (!status || netbits < 0)
{
goto fail;
= }
+ = r->netbits =3D netbits;
}
- else
+ else if (cidr)
{
<= span style=3D"color: hsl(0, 100%, 40%);">- r->netmask =3D default= _netmask;

+ if= (!prefix) /* handle 'aa.bb.cc.dd/' case */
+ {
+ goto fail;
+ }
+ char *endp =3D NULL;
+ const unsigned long netbits =3D strtoul(p= refix, &endp, 10);
+ {
+ goto fail;<= /span>
+ }
= + r->netbits =3D netb= its;
}

/* gatew= ay */
@@ -397,6 +418,7 @@
= | GETADDR_HOST_ORDER
| GETADDR_WARN_ON_SIG= NAL,
ro->gateway,
+ NULL,
= 0,
&status,
NULL);

@@ -428,7 +450,7 @@
<= span> if (r->metric < 0)

{ msg(M_WARN, PACKAGE_NAME " ROUTE: route metric for= network %s (%s) must be >=3D 0",
- ro->network,
+ addr,
= ro->metric);
goto fail;
}
@@ -442,11 +464,13 @@

r->flags |=3D RT_DEFINED;

+ free(network);
return true;

fail:
msg(M_WARN, PACKAGE_NAME " ROUTE: failed to parse/reso= lve route for host/network: %s",
ro->netwo= rk);
+ free(networ= k);
return false;
}
=
@@ -516,7 +540,7 @@
struct route_opti= on *ro;
ALLOC_OBJ_GC(ro, struct route_option, l->gc= );
ro->network =3D network;
- ro->netmask =3D netmask;
<= span style=3D"color: hsl(120, 100%, 40%);">+ ro->netmask =3D netmask;= /* NULL if network has CIDR notation */

ro->gatewa= y =3D gateway;
ro->metric =3D metric;
ro->next =3D l->routes;

@@ -582,7 +606,7 @@
r1->flags =3D RT_DEFINED;
= r1->gateway =3D target;
r1->network =3D g= ateway->addr & gateway->netmask;
- r1->netmask =3D ~(l2-1);
+ r1->netbits =3D netmask_to_= netbits2(gateway->netmask) + 1;
r1->next =3D= rl->routes;
rl->routes =3D r1;

@@ -681,6 +705,7 @@
| GE= TADDR_HOST_ORDER
| GETADDR_WARN_ON_SIGNAL,
remote_endpoint,
+ NULL,
0,
&defined,
NULL)= ;
@@ -921,7 +946,7 @@

st= atic bool
add_route3(in_addr_t network,
- in_addr_t netmask,
= + unsigned int netbit= s,
in_addr_t gateway,
= const struct tuntap *tt,
unsigned int flags,=
@@ -933,14 +958,14 @@
CLEAR(r);
r.flags =3D RT_DEFINED;
r.network =3D = network;
- r.netmask= =3D netmask;
+ r.= netbits =3D netbits;
r.gateway =3D gateway;
= return add_route(&r, tt, flags, rgi, es, ctx);
}


static void
del_r= oute3(in_addr_t network,
+ unsigned int netbits,
i= n_addr_t gateway,
const struct tuntap *tt,
unsigned int flags,
@@ -952,7 +977,= 7 @@
CLEAR(r);
r.flags =3D RT_DEF= INED|RT_ADDED;
r.network =3D network;
- r.netmask =3D netmask;
<= span style=3D"color: hsl(120, 100%, 40%);">+ r.netbits =3D netbits;
r.gateway =3D gateway;
delete_route(&= amp;r, tt, flags, rgi, es, ctx);
}
@@ -971= ,7 +996,7 @@
{
if (rb->byp= ass[i])
{
- ret =3D add_route3(rb->bypass[i], IPV4_NETMASK_= HOST, gateway, tt,
+ = ret =3D add_route3(rb->bypass[i], IPV4_NETBITS_HOST, gateway,= tt,
flags | ROUTE_REF_GW, rg= i, es, ctx) && ret;
}
= }
@@ -993,7 +1018,7 @@
if (rb->= ;bypass[i])
{
del_rou= te3(rb->bypass[i],
-= IPV4_NETMASK_HOST,
+ IPV4_NETBITS_HOST,
<= span> gateway,

= tt,
flags | ROUTE_REF_GW,=
@@ -1057,7 +1082,7 @@
if ((rl-&g= t;spec.flags & RTSA_REMOTE_HOST)
&= amp;& rl->spec.remote_host !=3D IPV4_INVALID_ADDR)
= {
- = ret =3D add_route3(rl->spec.remote_host, IPV4_NETMASK_HO= ST,
+ = ret =3D add_route3(rl->spec.remote_host, IPV4_NETBITS_HOST, rl->rgi.gateway.addr, tt, = flags | ROUTE_REF_GW,
= &rl->rgi, es, ctx);
rl->ifl= ags |=3D RL_DID_LOCAL;
@@ -1078,11 +1103,11 @@
if (rl->flags & RG_DEF1)

= {
/* add new default route = (1st component) */
- = ret =3D add_route3(0x00000000, 0x80000000, rl->spec.rem= ote_endpoint,
+ = ret =3D add_route3(0x00000000, 1, rl->spec.remote_endpoint= ,
tt, flags, &rl-= >rgi, es, ctx) && ret;

= /* add new default route (2nd component) */
- ret =3D add_route3(= 0x80000000, 0x80000000, rl->spec.remote_endpoint,
+ ret =3D add_route3(0x= 80000000, 1, rl->spec.remote_endpoint,
= tt, flags, &rl->rgi, es, ctx) && ret;
}
else
@@ -1120,7 +1145,7 @@
if (rl->ifla= gs & RL_DID_LOCAL)
{
= del_route3(rl->spec.remote_host,
- IPV4_NETMASK_HOST,
+ IPV4_NETBITS= _HOST,

rl->rgi.gateway.addr,
tt,
= flags | ROUTE_REF_GW,
@@ -1140,7 +1165,7 @@ {
/* delete default ro= ute (1st component) */
del_route3(0x000000= 00,
- = 0x80000000,
= + 1,
= rl->spec.remote_endpoint,
tt= ,
flags,
@@ -11= 50,7 +1175,7 @@

/* delet= e default route (2nd component) */
del_rou= te3(0x80000000,
- = 0x80000000,
+ 1,
= rl->spec.remote_endpoint,
= tt,
flags,
= @@ -1215,7 +1240,7 @@

for = (r =3D rl->routes; r; r =3D r->next)
{
- check_subnet_co= nflict(r->network, r->netmask, "route");
+ check_subnet_conflict(r-&g= t;network, netbits_to_netmask((int)r->netbits), "route");
if (flags & ROUTE_DELETE_FIRST)
{

delete_route(r, tt, flags= , &rl->rgi, es, ctx);
@@ -1432,14 +1457,18 @@=
if (r->flags & RT_DEFINED)
{
setenv_route_addr(es, "network", r->netw= ork, i);
- seten= v_route_addr(es, "netmask", r->netmask, i);
+ setenv_route_addr(es, "ne= tmask", netbits_to_netmask(r->netbits), i);
= setenv_route_addr(es, "gateway", r->gateway, i);
<= span>

+ struc= t buffer name1 =3D alloc_buf_gc(256, &gc);
+ buf_printf(&name1, "route_netbi= ts_%d", i);
+ = setenv_int(es, BSTR(&name1), (int)r->netbits);
+
if (r->= flags & RT_METRIC_DEFINED)
{
- struct buffer name =3D allo= c_buf_gc(256, &gc);
- buf_printf(&name, "route_metric_%d", i);=
- setenv_int(es, B= STR(&name), r->metric);
+ struct buffer name2 =3D alloc_buf_gc(256, &gc);<= /span>
+ buf_prin= tf(&name2, "route_metric_%d", i);
+ setenv_int(es, BSTR(&name2), r-= >metric);
}
}
gc_free(&gc);

@@ -1519,7 +1548,7 @@
=
static int
local_route(in_addr_t = network,
- i= n_addr_t netmask,
+ = unsigned int netbits,
in_addr_t gate= way,
const struct route_gateway_info *rgi)
{
@@ -1528,7 +1557,7 @@
= if (rgi
&& (rgi->flags & rgi_needed= ) =3D=3D rgi_needed
&& gateway =3D=3D rgi-= >gateway.addr
- = && netmask =3D=3D 0xFFFFFFFF)
+ && netbits =3D=3D IPV4_NETBITS_HOST)
{
if (((network ^ rgi->gat= eway.addr) & rgi->gateway.netmask) =3D=3D 0)
= {
@@ -1583,12 +1612,12 @@
#if !defined(T= ARGET_LINUX)
const char *network =3D print_in_addr_t(r= ->network, 0, &gc);
#if !defined(TARGET_AIX)=
- const char *netmask =3D = print_in_addr_t(r->netmask, 0, &gc);
+ const char *netmask =3D print_in_addr_t(netbits= _to_netmask((int)r->netbits), 0, &gc);
#endif
const char *gateway =3D print_in_addr_t(r->gateway, 0, &= amp;gc);
#endif

- is_local_route =3D local_route(r->netwo= rk, r->netmask, r->gateway, rgi);
+ is_local_route =3D local_route(r->network, r->= ;netbits, r->gateway, rgi);
if (is_local_route =3D= =3D LR_ERROR)
{
goto done;
@@ -1609,7 +1638,7 @@
}
=
status =3D RTA_SUCCESS;
- int ret =3D net_route_v4_add(ctx, &r->n= etwork, netmask_to_netbits2(r->netmask),
+ int ret =3D net_route_v4_add(ctx, &r->ne= twork, (int)r->netbits,
= &r->gateway, iface, 0, metric);
if (ret =3D=3D = -EEXIST)
{
@@ -1840,10 +1869,9 @@
#elif defined(TARGET_AIX)

= {
- int netbi= ts =3D netmask_to_netbits2(r->netmask);
argv_pr= intf(&argv, "%s add -net %s/%d %s",
= ROUTE_PATH,
- network, netbits, gateway);
+ network, r->netbits, g= ateway);
argv_msg(D_ROUTE, &argv);
<= span> bool ret =3D openvpn_execve_check(&argv, es, 0,
"ERROR: AIX route add = command failed");
@@ -2176,14 +2204,14 @@
#if !defined(TARGET_LINUX)

network =3D print_in_a= ddr_t(r->network, 0, &gc);
#if !defined(TARGET_AIX)=
- netmask =3D print= _in_addr_t(r->netmask, 0, &gc);
+ netmask =3D print_in_addr_t(netbits_to_netmask((int)= r->netbits), 0, &gc);
#endif
#if != defined(TARGET_ANDROID)
gateway =3D print_in_addr_t(r-= >gateway, 0, &gc);
#endif
#endif

- i= s_local_route =3D local_route(r->network, r->netmask, r->gateway, = rgi);
+ is_local_r= oute =3D local_route(r->network, r->netbits, r->gateway, rgi);
if (is_local_route =3D=3D LR_ERROR)
= {
goto done;
@@ -2196,7 +2224,7 @@=
metric =3D r->metric;
}

- i= f (net_route_v4_del(ctx, &r->network, netmask_to_netbits2(r->netm= ask),
+ if (net_ro= ute_v4_del(ctx, &r->network, (int)r->netbits,
= &r->gateway, NULL, 0, metric) < 0)<= br> {
msg(M_WARN, "ERROR: Linux rou= te delete command failed");
@@ -2318,10 +2346,9 @@
#elif defined(TARGET_AIX)

= {
- int netb= its =3D netmask_to_netbits2(r->netmask);
argv_p= rintf(&argv, "%s delete -net %s/%d %s",
= ROUTE_PATH,
- network, netbits, gateway);
+ network, r->netbit= s, gateway);
argv_msg(D_ROUTE, &argv);<= br> openvpn_execve_check(&argv, es, 0, "ERROR: AIX r= oute delete command failed");
}
@= @ -2840,13 +2867,14 @@
int ret =3D RTA_ERROR; DWORD status;
const DWORD if_index =3D (a= dapter_index =3D=3D TUN_ADAPTER_INDEX_INVALID) ? windows_route_find_if_inde= x(r, tt) : adapter_index;
+ const in_addr_t netmask =3D netbits_to_netmask((int)r->netbits= );

if (if_index !=3D TUN_ADAPTER_IND= EX_INVALID)
{
MIB_IPFORWARDRO= W fr;
CLEAR(fr);
fr.dwFor= wardDest =3D htonl(r->network);
- fr.dwForwardMask =3D htonl(r->netmask);
<= span style=3D"color: hsl(120, 100%, 40%);">+ fr.dwForwardMask =3D ht= onl(netmask);

fr.dwForwardPolicy =3D 0;
= fr.dwForwardNextHop =3D htonl(r->gateway);
fr.dwForwardIfIndex =3D if_index;

@@ -2860,11 +2= 888,11 @@
fr.dwForwardMetric4 =3D METRIC_NOT_USED;=
fr.dwForwardMetric5 =3D METRIC_NOT_USED;
- if (= (r->network & r->netmask) !=3D r->network)
+ if ((r->network & netmas= k) !=3D r->network)
{
= msg(M_WARN, "Warning: address %s is not a network address in relat= ion to netmask %s",
print_in_addr_t(r= ->network, 0, &gc),
- print_in_addr_t(r->netmask, 0, &gc));+ print_in_addr= _t(netmask, 0, &gc));
}

status =3D CreateIpForwardEntry(&fr);
@@ -2932,6 +2960,7 @@

bool ret =3D false; DWORD status;
const DWORD if_index =3D wi= ndows_route_find_if_index(r, tt);
+ const in_addr_t netmask =3D netbits_to_netmask((int)r->= ;netbits);

if (if_index !=3D TUN_ADA= PTER_INDEX_INVALID)
{
@@ -2939,7 +2968= ,7 @@
CLEAR(fr);

fr.dwForwardDest =3D htonl(r->network);

- fr.dwForwardMask =3D htonl(r->ne= tmask);
+ fr.d= wForwardMask =3D htonl(netmask);
fr.dwForwardPolic= y =3D 0;
fr.dwForwardNextHop =3D htonl(r->gatew= ay);
fr.dwForwardIfIndex =3D if_index;
<= span>@@ -3016,7 +3045,7 @@

.metric =3D (r->flag= s & RT_METRIC_DEFINED ? r->metric : -1)
};

- netm= ask_to_netbits(r->network, r->netmask, &msg.prefix_len);+ msg.prefix_len =3D r->= ;netbits;
if (msg.prefix_len =3D=3D -1)
{

msg.prefix_len =3D 32;
@@= -3996,7 +4025,7 @@
while (iplist)
= {
bool succeed =3D false;
- const in_addr_t ip =3D getaddr(GETA= DDR_HOST_ORDER, iplist->IpAddress.String, 0, &succeed, NULL);=
+ const in_addr_t ip= =3D getaddr(GETADDR_HOST_ORDER, iplist->IpAddress.String, NULL, 0, &= ;succeed, NULL);
if (succeed)
= {
add_host_route_if_nonlocal(rb, ip);
@@ -4096,7 +4125,7 @@
{
= if (rgi)
{
- if (local_route(addr, 0xFFFFFFFF, rgi->gateway.addr, r= gi))
+ if (loc= al_route(addr, IPV4_NETBITS_HOST, rgi->gateway.addr, rgi))
{

return TLA_LOCAL;
}

diff --git a/src/openvpn/route.h b/src/openvpn= /route.h
index 421e7d2..a117584 100644
--- = a/src/openvpn/route.h
+++ b/src/openvpn/route.h
<= span>@@ -117,7 +117,7 @@

unsigned int flags; const struct route_option *option;
in_addr= _t network;
- in_add= r_t netmask;
+ uns= igned int netbits;
in_addr_t gateway;
= int metric;
};
diff --git a/src/openv= pn/socket.c b/src/openvpn/socket.c
index 17c5e76..767036f 1= 00644
--- a/src/openvpn/socket.c
+++ b/src/= openvpn/socket.c
@@ -179,6 +179,7 @@
in_ad= dr_t
getaddr(unsigned int flags,
= const char *hostname,
+ unsigned int *netbits,
int resolve_retry= _seconds,
bool *succeeded,
= struct signal_info *sig_info)
@@ -186,7 +187,7 @@<= br> in_addr_t addr;
int status;

- status =3D g= et_addr_generic(AF_INET, flags, hostname, &addr, NULL,
+ status =3D get_addr_generic(AF_I= NET, flags, hostname, &addr, netbits,
= resolve_retry_seconds, sig_info,
= M_WARN);
if (status=3D=3D0)<= br>diff --git a/src/openvpn/socket.h b/src/openvpn/socket.hindex 47083ad..f6165b2 100644
--- a/src/openvpn/sock= et.h
+++ b/src/openvpn/socket.h
@@ -524,6 += 524,7 @@
*/
in_addr_t getaddr(unsigned i= nt flags,
const char *hostname,+ unsigned i= nt *netbits,
int resolve_retry_seconds,<= /span>
bool *succeeded,
= struct signal_info *sig_info);
diff --git a/src= /openvpn/tun.c b/src/openvpn/tun.c
index 739e008..52e2464 1= 00644
--- a/src/openvpn/tun.c
+++ b/src/ope= nvpn/tun.c
@@ -113,7 +113,7 @@
if (add= r.family =3D=3D AF_INET)
{
ad= dr.address.ipv4.s_addr =3D htonl(tt->local);
- addr.prefix_len =3D netmask_to_netbits2(t= t->adapter_netmask);
+ addr.prefix_len =3D tt->netbits;
ms= g(D_IFCONFIG, "INET address service: %s %s/%d",
= add ? "add" : "remove",
= print_in_addr_t(tt->local, 0, &gc), addr.prefix_len);
@@ -499,17 +499,17 @@
static const char ifconf= ig_warn_how_to_silence[] =3D "(silence this warning with --ifconfig-no= warn)";

/*
- * If !tun_p2p, make sure ifconfig_remote_netm= ask looks
- * like a n= etmask.
+ * If !tun_p= 2p, make sure either ifconfig_netbits is
+ * set or ifconfig_remote looks like a netmask.=
*
- * If tu= n_p2p, make sure ifconfig_remote_netmask looks
+ * If tun_p2p, make sure ifconfig_remote looks
* like an IPv4 address.
*/
static void

-ifcon= fig_sanity_check(bool tun_p2p, in_addr_t addr)
+ifconfig_sanity_check(bool tun_p2p, in_addr_t ad= dr, int netbits)
{
struct gc_arena gc= =3D gc_new();
- con= st bool looks_like_netmask =3D ((addr & 0xFF000000) =3D=3D 0xFF000000);=
+ const bool look= s_like_netmask =3D addr && ((addr & 0xFF000000) =3D=3D 0xFF0000= 00);
if (tun_p2p)
{
if (looks_like_netmask)

@@ -521,9 +521,11 @@
}
else
{
- if (!looks_like_net= mask)
+ if (ne= tbits < 0 || !looks_like_netmask)
{
<= span style=3D"color: hsl(0, 100%, 40%);">- msg(M_WARN, "WAR= NING: Since you are using subnet topology, the second argument to --ifconfi= g must be a netmask, for example something like 255.255.255.0. %s",
+ msg(M_WARN= , "WARNING: Since you are using topology subnet, you must either "= ;
+ &q= uot;provide a valid prefix length in the first argument of --ifconfig (eg. = 10.8.0.0/24) "
+= "or a valid netmask as the second argument (eg. 255.25= 5.255.0). %s",
ifconfig_warn_how_to_s= ilence);
}
}
= @@ -539,34 +541,36 @@
int type, in_addr_t public,
= in_addr_t local,
- = in_addr_t remote_netmask)
+ in_addr_t remote,
+ int netbits)
= {
struct gc_arena gc =3D gc_new();
#if 0

- msg(M_I= NFO, "CHECK_ADDR_CLASH type=3D%d public=3D%s local=3D%s, remote_netmas= k=3D%s",
+ ms= g(M_INFO, "CHECK_ADDR_CLASH type=3D%d public=3D%s local=3D%s, netbits= =3D%d, remote=3D%s",
type,
= print_in_addr_t(public, 0, &gc),
print_= in_addr_t(local, 0, &gc),
- print_in_addr_t(remote_netmask, 0, &gc));
+ netbits,

+ print_in_addr_t(remote, 0, &= amp;gc));
#endif

if= (public)
{
if (type =3D=3D D= EV_TYPE_TUN)
{
- const in_addr_t test_netmask =3D 0xFFFFFF00;<= /span>
- const in_a= ddr_t public_net =3D public &test_netmask;
- const in_addr_t local_net =3D local &a= mp; test_netmask;
- = const in_addr_t remote_net =3D remote_netmask & test_netmask;
+ const in_= addr_t netmask =3D netbits < 0 ? 0xFFFFFF00 : netbits_to_netmask(netbits= );
+ const= in_addr_t public_net =3D public &netmask;
+ const in_addr_t local_net =3D local = & netmask;
+ = const in_addr_t remote_net =3D remote & netmask;

- if (pub= lic =3D=3D local || public =3D=3D remote_netmask)
+ if (public =3D=3D local || public= =3D=3D remote)
{
= msg(M_WARN,
"WARNING: --%s a= ddress [%s] conflicts with --ifconfig address pair [%s, %s]. %s",
name,
= print_in_addr_t(public, 0, &gc),
= print_in_addr_t(local, 0, &gc),
- print_in_addr_t(remote_netmask, 0, &g= c),
+ = print_in_addr_t(remote, 0, &gc),
= ifconfig_warn_how_to_silence);
}
=
@@ -577,22 +581,23 @@
= name,
print_in_addr_t(public, = 0, &gc),
print_in_addr_t(local, 0,= &gc),
- = print_in_addr_t(remote_netmask, 0, &gc),
+ print_in_addr_t(remot= e ? remote : netmask, 0, &gc),
ifc= onfig_warn_how_to_silence);
}
= }
else if (type =3D=3D DEV_TYPE_TAP)
{
= - const in_addr_t public_network =3D public &remote_netmask;=
- const in_= addr_t virtual_network =3D local & remote_netmask;
- if (public_network =3D=3D virt= ual_network)
+ = const in_addr_t netmask =3D netbits_to_netmask(netbits);
+ const in_addr_t publi= c_network =3D public &netmask;

+ const in_addr_t virtual_network =3D local & = netmask;
+ = if (netmask && public_network =3D=3D virtual_network)
{

msg(M_WARN,
<= span style=3D"color: hsl(0, 100%, 40%);">- "WARNING= : --%s address [%s] conflicts with --ifconfig subnet [%s, %s] -- local and = remote addresses cannot be inside of the --ifconfig subnet. %s",
+ "= ;WARNING: --%s address [%s] conflicts with --ifconfig subnet [%s/%d] -- loc= al and remote addresses cannot be inside of the --ifconfig subnet. %s"= ,
name,
= print_in_addr_t(public, 0, &gc),
= print_in_addr_t(local, 0, &gc),
- print_in_addr_t(remote_netmask, 0, = &gc),
+ = netbits,
ifconfig_warn_how_t= o_silence);
}
}
@@ -669,9 +674,10 @@
{
= if (!is_tun_p2p(tt))
{
+ const in_addr_t netmask =3D n= etbits_to_netmask(tt->netbits);
buf_printf(= &out, "%s %s",
- print_in_addr_t(tt->local & tt->rem= ote_netmask, 0, gc),
- = print_in_addr_t(tt->remote_netmask, 0, gc));
+ pr= int_in_addr_t(tt->local & netmask, 0, gc),
+ print_in_addr_t(netmas= k, 0, gc));
}
else if (tt= ->type =3D=3D DEV_TYPE_TUN) /* tun p2p topology */
= {
@@ -679,12 +685,12 @@
if= (remote)
{
r= =3D print_in_addr_t(tt->local, 0, gc);
- l =3D print_in_addr_t(tt->remote_ne= tmask, 0, gc);
+ = l =3D print_in_addr_t(tt->remote, 0, gc);
= }
else
= {
l =3D print_in_addr_t(tt->local, 0, g= c);
- r = =3D print_in_addr_t(tt->remote_netmask, 0, gc);
+ r =3D print_in_addr_t(tt->= ;remote, 0, gc);
}
= buf_printf(&out, "%s %s", r, l);
}=
@@ -765,7 +771,8 @@
{
= struct gc_arena gc =3D gc_new();
const char *ifconfi= g_local =3D print_in_addr_t(tt->local, 0, &gc);
- const char *ifconfig_remote_netmask = =3D print_in_addr_t(tt->remote_netmask, 0, &gc);
+ const char *ifconfig_remote =3D tt-= >remote ? print_in_addr_t(tt->remote, 0, &gc) : NULL;
<= span style=3D"color: hsl(120, 100%, 40%);">+ const char *ifconfig_netmas= k =3D tt->netbits >=3D 0 ? print_in_addr_t(netbits_to_netmask(tt->= netbits), 0, &gc) : NULL;


/*
* Set environmental variables with ifconfig parameters.<= /span>
@@ -777,11 +784,12 @@
setenv_str(e= s, "ifconfig_local", ifconfig_local);
if= (tun)
{
- setenv_str(es, "ifconfig_remote", ifconfi= g_remote_netmask);
+ = setenv_str(es, "ifconfig_remote", ifconfig_remote);
}
else
= {
- set= env_str(es, "ifconfig_netmask", ifconfig_remote_netmask);<= br>+ setenv_int(es, = "ifconfig_netbits", tt->netbits);
+ setenv_str(es, "ifconfig_netmas= k", ifconfig_netmask);
}
= }

@@ -837,7 +845,7 @@
= bool tun_p2p =3D is_tun_p2p(tt);

= /*
- * = Convert arguments to binary IPv4 addresses.
+ * Convert arguments to binary IPv4 address= es and netbits.
*/

tt->local =3D getaddr(

@@ -846,27 +854,48 @= @
| GETADDR_FATAL_ON_SIGNAL
= | GETADDR_FATAL,
ifconfig_local_par= m,
+ NULL,=
0,
NULL,<= br> NULL);

- tt->remote_netmask =3D getaddr(<= br>+ tt->remote =3D g= etaddr(
(tun_p2p ? GETADDR_RESOLVE : 0)=
| GETADDR_HOST_ORDER
| = GETADDR_FATAL_ON_SIGNAL
| GETADDR_FATAL,
ifconfig_remote_netmask_parm,
+ NULL,
= 0,
NULL,
NU= LL);

+ if (!tun_= p2p)
+ {
+ if (tt->re= mote)
+ {<= /span>
+ tt-&= gt;netbits =3D netmask_to_netbits2(tt->remote);
+ tt->remote =3D 0;<= br>+ }
+ else

+ {
+ msg(M_FATAL, "init_t= un: can not figure out the subnet from the remote parameter (%s)", ifc= onfig_remote_netmask_parm);
+ }
+ tt->remote =3D 0;
+ }
+ else
+ = {
+ = tt->netbits =3D -1;
+=
/*
* Look for common errors in= --ifconfig parms
*/
if = (strict_warn)
{
struc= t addrinfo *curele;
- = ifconfig_sanity_check(tun_p2p, tt->remote_netmask);
= + ifconfig_sanity_ch= eck(tun_p2p, tt->remote, tt->netbits);

/*

* If local_public or remo= te_public addresses are defined,
@@ -877,11 +906,13 @@
{
if (curele->= ai_family =3D=3D AF_INET)
{
+ const in_addr_= t local =3D ntohl(((struct sockaddr_in *)curele->ai_addr)->sin_addr.s= _addr);

check_addr_clash("local&q= uot;,
tt->type,
- = ((struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr= ,
+ = local,
= tt->local,
- = tt->remote_netmask);
+ = tt->remote,
+ = tt->netbits);
= }
}

@= @ -889,17 +920,19 @@
{
= if (curele->ai_family =3D=3D AF_INET)
= {
+ = const in_addr_t remote =3D ntohl(((struct sockaddr_in *)curele-&= gt;ai_addr)->sin_addr.s_addr);
chec= k_addr_clash("remote",
= tt->type,
+ remote,
= tt->local,
- tt->remote= _netmask);
+ = tt->remote,
+ tt->netbits)= ;
}
}<= br>
if (!tun_p2p)
= {
- = check_subnet_conflict(tt->local, tt->remote_netmask, "TUN/TA= P adapter");
+ = check_subnet_conflict(tt->local, netbits_to_netmask(tt->= ;netbits), "TUN/TAP adapter");
}
else
{
@@ -914,12 +947,12 @@

*/
= if (tun_p2p)
{
- verify_255_255_255_252(tt->local, tt-&= gt;remote_netmask);
+= verify_255_255_255_252(tt->local, tt->remote);
= tt->adapter_netmask =3D ~3;
= }
else
{
- tt->adapter_netmask =3D= tt->remote_netmask;
+ tt->adapter_netmask =3D netbits_to_netmask(tt->netbit= s);
}
#endif

@@ -1062,8 +1095,9 @@
create_arbitrary_remot= e( struct tuntap *tt )
{
in_addr_t re= mote;
+ const in_a= ddr_t netmask =3D netbits_to_netmask(tt->netbits);

- remote =3D (tt->lo= cal & tt->remote_netmask) +1;
+ remote =3D (tt->local & netmask) +1;
=
if (remote =3D=3D tt->local)
{

@@ -1300,7 +1334,13 @@
#if !defined(TARGET_LINUX)
const char *ifconfi= g_local =3D NULL;
- = const char *ifconfig_remote_netmask =3D NULL;
+#if !defined(_WIN32)
+ const char *ifconfig_remote =3D NULL;=
+#endif
+#if !defined(TARGET_FREEBSD) &&= !defined(TARGET_DRAGONFLY)
+ const char *ifconfig_netmask =3D NULL;
+ const in_addr_t netmask =3D netbits_to_n= etmask(tt->netbits);
+#endif
struct argv argv =3D argv_new();
= struct gc_arena gc =3D gc_new();

@@ -1308,7 +1348,12 @@

* Set ifconfig parameters
*/
ifconfig_local =3D print_in_ad= dr_t(tt->local, 0, &gc);
- ifconfig_remote_netmask =3D print_in_addr_t(tt->remote_net= mask, 0, &gc);
+#= if !defined(_WIN32)
+= ifconfig_remote =3D print_in_addr_t(tt->remote, 0, &gc);<= br>+#endif
+#if !defined(TARGET_FREEBSD) && = !defined(TARGET_DRAGONFLY)
+ ifconfig_netmask =3D print_in_addr_t(netmask, 0, &gc);
+#endif
#= endif

#if defined(TARGET_LINUX)@@ -1324,16 +1369,14 @@

if (= tun_p2p)
{
- if (net_addr_ptp_v4_add(ctx, ifname, &tt->local,
- = &tt->remote_netmask) < 0)
+ if (net_addr_ptp_v4_add(ctx, ifname, &= tt->local, &tt->remote) < 0)
{=
msg(M_FATAL, "Linux can't add IP to interfa= ce %s", ifname);
}
}
else
{
- if (net_addr_v4_add(ctx, ifname, &tt-&g= t;local,
- = netmask_to_netbits2(tt->remote_netmask)) < 0)+ if (net_addr_v4_add(= ctx, ifname, &tt->local, tt->netbits) < 0)
= {
msg(M_FATAL, "Linux can't add= IP to interface %s", ifname);
}
@@ -1341,9 +1384,14 @@

#elif defined(TARGET_ANDROID)
char out[64];

- snprintf(out, sizeof(out), "%s %s = %d %s", ifconfig_local,
- ifconfig_remote_netmask, tun_mtu, print_topology(tt->= ;topology));
+ snp= rintf(out, sizeof(out), "%s %s %d %s",
+ ifconfig_local,
+ ifconfig_remote ? ifconf= ig_remote : ifconfig_netmask,
+ tun_mtu,
+ print_topology(tt->topology));
= management_android_control(management, "IFCONFIG", out);
+ (void)ifconfig_netm= ask;
+ (void)netma= sk;

#elif defined(TARGET_SOLARIS)=
/* Solaris 2.6 (and 7?) cannot set all parameters in one go.= ..
@@ -1354,7 +1402,7 @@
if (tun_p2p)<= /span>
{
argv_printf(&argv, &quo= t;%s %s %s %s mtu %d up", IFCONFIG_PATH, ifname,
- ifconfig_local, ifconfi= g_remote_netmask, tun_mtu);
+ ifconfig_local, ifconfig_remote, tun_mtu);

argv_msg(M_INFO, &argv);<= br> if (!openvpn_execve_check(&argv, es, 0, "Solaris= ifconfig phase-1 failed"))
@@ -1369,13 +1417,13 @@
{
argv_printf(&argv, "= %s %s %s %s netmask %s mtu %d up", IFCONFIG_PATH,
= ifname, ifconfig_local, ifconfig_local,
- ifconfig_remote_netm= ask, tun_mtu);
+ = ifconfig_netmask, tun_mtu);
} else /* tap */
{
= argv_printf(&argv, "%s %s %s netmask %s up",
= IFCONFIG_PATH, ifname, ifconfig_local,
- ifconfig_remote_n= etmask);

+ = ifconfig_netmask);
}
=
argv_msg(M_INFO, &argv);
@@ -1390,8 +143= 8,8 @@
struct route_ipv4 r;
= CLEAR(r);
r.flags =3D RT_DEFINED | RT_METRIC_DE= FINED;
- r.netwo= rk =3D tt->local & tt->remote_netmask;
- r.netmask =3D tt->remote_netmask;
+ r.network =3D t= t->local & netmask;
+ r.netbits =3D tt->netbits;
r.gat= eway =3D tt->local;
r.metric =3D 0;
<= span> add_route(&r, tt, 0, NULL, es, NULL);

@@ = -1413,7 +1461,7 @@
argv_printf(&argv, "%s %s %s %s mtu %d netmask 255.255.255.2= 55 up -link0",
IFCONFIG_PATH, ifn= ame, ifconfig_local,
- = ifconfig_remote_netmask, tun_mtu);
+ ifconfig_remote, tun_= mtu);
}
else if (tt->type =3D= =3D DEV_TYPE_TUN)
{
@@ -1421,13 +1469,= 13 @@
argv_printf(&argv, "%s %s %s %s mtu= %d netmask %s up -link0",
IFCONF= IG_PATH, ifname, ifconfig_local,
print= _in_addr_t(remote_end, 0, &gc), tun_mtu,
- ifconfig_remote_netmask);=
+ ifconf= ig_netmask);
}
else /* tap */
{
argv_printf(&argv, "%= s %s %s netmask %s mtu %d link0",
= IFCONFIG_PATH, ifname, ifconfig_local,
- ifconfig_remote_netmask, tun_mtu);
+ if= config_netmask, tun_mtu);
}
argv_= msg(M_INFO, &argv);
openvpn_execve_check(&argv= , es, S_FATAL, "OpenBSD ifconfig failed");
@@ -14= 38,8 +1486,8 @@
struct route_ipv4 r;
CLEAR(r);

r.flags =3D RT_DEFINED;
- r.network =3D tt-&= gt;local & tt->remote_netmask;
- r.netmask =3D tt->remote_netmask;
+ r.network =3D tt->local= & netmask;

+ = r.netbits =3D tt->netbits;
r.gateway =3D re= mote_end;
add_route(&r, tt, 0, NULL, es, NULL)= ;
}
@@ -1451,14 +1499,14 @@
= {
argv_printf(&argv, "%s %s %s= %s mtu %d netmask 255.255.255.255 up",
= IFCONFIG_PATH, ifname, ifconfig_local,
- ifconfig_remote_netmask, tun_mt= u);
+ = ifconfig_remote, tun_mtu);
}
= else if (tt->type =3D=3D DEV_TYPE_TUN)
{
= remote_end =3D create_arbitrary_remote(tt);
= argv_printf(&argv, "%s %s %s %s mtu %d netmask %s up"= ;, IFCONFIG_PATH,
ifname, ifconfig_loc= al, print_in_addr_t(remote_end, 0, &gc),
- tun_mtu, ifconfig_remote_netmask= );
+ = tun_mtu, ifconfig_netmask);
}
= else /* tap */
{
@@ -1469,7 +1517,7 @@=
*/
argv_printf(&arg= v, "%s %s %s netmask %s mtu %d",
= IFCONFIG_PATH, ifname, ifconfig_local,
- ifconfig_remote_netmask, tun_mtu)= ;
+ = ifconfig_netmask, tun_mtu);
}
a= rgv_msg(M_INFO, &argv);
openvpn_execve_check(&= argv, es, S_FATAL, "NetBSD ifconfig failed");
@@ = -1480,8 +1528,8 @@
struct route_ipv4 r;
= CLEAR(r);
r.flags =3D RT_DEFINED;
- r.network =3D t= t->local & tt->remote_netmask;
- r.netmask =3D tt->remote_netmask;
<= span style=3D"color: hsl(120, 100%, 40%);">+ r.network =3D tt->lo= cal & netmask;

+ = r.netbits =3D tt->netbits;
r.gateway =3D= remote_end;
add_route(&r, tt, 0, NULL, es, NU= LL);
}
@@ -1503,18 +1551,18 @@<= br> {
argv_printf(&argv, "%s %s= %s %s mtu %d netmask 255.255.255.255 up",
= IFCONFIG_PATH, ifname, ifconfig_local,
- ifconfig_remote_netmask, tun= _mtu);
+ = ifconfig_remote, tun_mtu);
}
= else if (tt->type =3D=3D DEV_TYPE_TUN)
{<= br> argv_printf(&argv, "%s %s %s %s netmask %s mtu %= d up",
IFCONFIG_PATH, ifname, ifc= onfig_local, ifconfig_local,
- ifconfig_remote_netmask, tun_mtu);
+ ifconfig_netm= ask, tun_mtu);

}
else /* tap */
{
argv_printf(&argv, "= ;%s %s %s netmask %s mtu %d up", IFCONFIG_PATH,
- ifname, ifconfig_local,= ifconfig_remote_netmask,
+ ifname, ifconfig_local, ifconfig_netmask,<= br> tun_mtu);
}

@@ -1527,8 +1575,8 @@
struct= route_ipv4 r;
CLEAR(r);
= r.flags =3D RT_DEFINED;
- r.network =3D tt->local & tt->remote_netmask;- r.netmask =3D tt->re= mote_netmask;
+ = r.network =3D tt->local & netmask;
+ r.netbits =3D tt->netbits;
r.gateway =3D tt->local;

add_route(= &r, tt, 0, NULL, es, NULL);
}
@@ -= 1540,13 +1588,12 @@
{
argv_pr= intf(&argv, "%s %s %s %s mtu %d netmask 255.255.255.255 up",<= /span>
IFCONFIG_PATH, ifname, ifconfig_local,=
- i= fconfig_remote_netmask, tun_mtu);
+ ifconfig_remote, tun_mtu);
}

else /* tun with topology subnet a= nd tap mode (always subnet) */
{
- int netbits =3D netmask_to_netbits2= (tt->remote_netmask);
argv_printf(&argv, &q= uot;%s %s %s/%d mtu %d up", IFCONFIG_PATH,
- ifname, ifconfig_local, netbi= ts, tun_mtu );
+ = ifname, ifconfig_local, tt->netbits, tun_mtu ); }

argv_msg(M_INFO, &= argv);
@@ -1565,7 +1612,7 @@

/* example: ifconfig tap0 172.30.1.1 netmask 255.255.254.0 up = */

argv_printf(&argv, "%s %s %s netmask %= s mtu %d up", IFCONFIG_PATH,
- ifname, ifconfig_local, ifconfig_remote_net= mask, tun_mtu);
+ = ifname, ifconfig_local, ifconfig_netmask, tun_mtu);<= br>
argv_msg(M_INFO, &argv);
= openvpn_execve_check(&argv, aix_es, S_FATAL, "AIX i= fconfig failed");
@@ -1578,7 +1625,7 @@
msg(M_INFO,

"******** NOTE: P= lease manually set the IP/netmask of '%s' to %s/%s (if it is not al= ready set)",
ifname, ifconfig_local,
- ifconfig_remot= e_netmask);
+ = ifconfig_netmask);
}
else if = (tt->options.ip_win32_type =3D=3D IPW32_SET_DHCP_MASQ || tt->options.= ip_win32_type =3D=3D IPW32_SET_ADAPTIVE)
{
<= span>@@ -1660,12 +1707,10 @@

undo_ifconfig_ipv4(struct tun= tap *tt, openvpn_net_ctx_t *ctx)
{
#if de= fined(TARGET_LINUX)
- = int netbits =3D netmask_to_netbits2(tt->remote_netmask);
-

if (is_tun_p2= p(tt))
{
if (net_addr_ptp_v4_= del(ctx, tt->actual_name, &tt->local,
- &tt->remot= e_netmask) < 0)
+ = &tt->remote) < 0)
= {
msg(M_WARN, "Linux can't d= el IP from iface %s",
tt->actual_n= ame);
@@ -1673,7 +1718,7 @@
} else
{
- if (net_addr_v4_del(ctx, tt->actual_name, &am= p;tt->local, netbits) < 0)
+ if (net_addr_v4_del(ctx, tt->actual_name, &tt-&= gt;local, tt->netbits) < 0)
{
msg(M_WARN, "Linux can't del IP from iface %s"= ,

tt->actual_name);
@@ = -4683,8 +4728,8 @@

if (ip_st= r && netmask_str && strlen(ip_str) && strlen(netmas= k_str))
{
- *ip =3D getaddr(getaddr_flags, ip_str, 0, = &succeed1, NULL);
-= *netmask =3D getaddr(getaddr_flags, netmask_str, 0, &su= cceed2, NULL);
+ = *ip =3D getaddr(getaddr_flags, ip_str, NULL, 0, &succeed1, N= ULL);
+ = *netmask =3D getaddr(getaddr_flags, netmask_str, NULL, 0, &succeed2, = NULL);
ret =3D (succeed1 =3D=3D true &= & succeed2 =3D=3D true);
}
}

@@ -5370,7 +5415,7 @@
= break;
}

- ip =3D getaddr(getaddr_flags, ip_= str, 0, &succeed, NULL);
+ ip =3D getaddr(getaddr_flags, ip_str, NULL, 0, &succee= d, NULL);
if (!succeed)
{=
break;
@@ -6428,11 +6473,12 @= @
if (!tt->did_ifconfig_setup || tt->topology = =3D=3D TOP_SUBNET)
{
in_addr_= t ep[3];
+ con= st in_addr_t netmask =3D netbits_to_netmask(tt->netbits);
BOOL status;


ep[0] = =3D htonl(tt->local);
- ep[2] =3D htonl(tt-= >remote_netmask);
= + ep[1] =3D htonl(tt->local & netmask);
+ ep[2] =3D htonl(netmask);<= br>
status =3D DeviceIoControl(tt->hand,= TAP_WIN_IOCTL_CONFIG_TUN,
= ep, sizeof(ep),
@@ -6456,7 +6502,7 @@
= {
in_addr_t ep[2];
ep[0= ] =3D htonl(tt->local);
- ep[1] =3D htonl(tt->remote_netmask);
+ ep[1] =3D htonl(tt->remote);

if (!DeviceIoControl(tt->hand,= TAP_WIN_IOCTL_CONFIG_POINT_TO_POINT,
= ep, sizeof(ep),
@@ -6485,11 +6531,11 @@
= {
if (tt->topology =3D=3D TOP_SUBNET= )
{
- ep[2] =3D dhcp_masq_addr(tt->local, tt->remote_net= mask, tt->options.dhcp_masq_custom_offset ? tt->options.dhcp_masq_off= set : 0);
+ = ep[2] =3D dhcp_masq_addr(tt->local, netbits_to_netmask(tt->netbits)= , tt->options.dhcp_masq_custom_offset ? tt->options.dhcp_masq_offset = : 0);
}
else
{

- = ep[2] =3D htonl(tt->remote_netmask);
+ ep[2] =3D htonl(tt->remote);=
}
}
else
diff --git a/src/openvpn/tun.h b/src/openvpn/tun.h
index 33b9552..68aa3b8 100644

--- a/src/openvpn/tun.h
+++ b/src/openvpn/tun.h
@@ -188,7 +188,8 @@

/* ifconfig parameters */
<= span> in_addr_t local;

- in_addr_t remote_netmask;
+ in_addr_t remote;
+ int netbits;

st= ruct in6_addr local_ipv6;
struct in6_addr remote_ipv6;=

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

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Iae04ad8715e40dfc76475c2c5b9= a766c9604efc9
Gerrit-Change-Number: 739
Gerrit-PatchSet: 2
Gerrit-Owner: ralf_lici <ralf@...2726...>= ;
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-MessageType: newpatchset
--ezz0ITQCaPo=--