From: "ralf_lici (Code Review)" Date: Mon, 9 Sep 2024 15:12:55 +0000 References: Reply-To: ralf@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <36ae7618c415a2084827364fe4cf304b42c7987f-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="jpuP3aF4tkQ="; 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 --jpuP3aF4tkQ= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello plaisthos, flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/739?usp=3Demail to review the following change. 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, 521 insertions(+), 284 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/739/1 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..eaf5abb 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,35 @@ 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; + } + msg(M_WARN, "client-nat network parameter can also be specified as= a CIDR address instead of using the separate netmask parameter"); + 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 +228,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 +250,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..84e9db4 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) @@ -1614,6 +1612,11 @@ netmask_str); return; } + msg(M_WARN, "iroute network parameter can also be specified as a C= IDR address instead of using the separate netmask parameter"); + } + else if (netbits) + { + ir->netbits =3D (int)netbits; } ir->next =3D o->iroutes; @@ -2631,9 +2634,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 +6095,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 +7020,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 +7396,57 @@ 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) + { + msg(M_WARN, "server network parameter can also be specifie= d as a CIDR address instead of using the separate netmask parameter"); + 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 +7475,43 @@ 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) + { + msg(M_WARN, "server-bridge gateway parameter can also be s= pecified as a CIDR address instead of using the separate netmask parameter"= ); + 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 +7546,27 @@ { 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) + { + msg(M_WARN, "ifconfig-pool start-IP parameter can also be = specified as a CIDR address instead of using the separate netmask parameter= "); + 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 +7580,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 +7901,77 @@ 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; + msg(M_WARN, "ifconfig-push-constraint network parameter ca= n also be specified as a CIDR address instead of using the separate netmask= parameter"); + } + } + 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..b743433 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,41 @@ 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; } + msg(M_WARN, "route parameter network/IP can also be specified as a= CIDR address instead of using the separate netmask parameter"); + 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 +419,7 @@ | GETADDR_HOST_ORDER | GETADDR_WARN_ON_SIGNAL, ro->gateway, + NULL, 0, &status, NULL); @@ -428,7 +451,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 +465,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 +541,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 +607,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 +706,7 @@ | GETADDR_HOST_ORDER | GETADDR_WARN_ON_SIGNAL, remote_endpoint, + NULL, 0, &defined, NULL); @@ -921,7 +947,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 +959,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 +978,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 +997,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 +1019,7 @@ if (rb->bypass[i]) { del_route3(rb->bypass[i], - IPV4_NETMASK_HOST, + IPV4_NETBITS_HOST, gateway, tt, flags | ROUTE_REF_GW, @@ -1057,7 +1083,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 +1104,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 +1146,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 +1166,7 @@ { /* delete default route (1st component) */ del_route3(0x00000000, - 0x80000000, + 1, rl->spec.remote_endpoint, tt, flags, @@ -1150,7 +1176,7 @@ /* delete default route (2nd component) */ del_route3(0x80000000, - 0x80000000, + 1, rl->spec.remote_endpoint, tt, flags, @@ -1215,7 +1241,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 +1458,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 +1549,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 +1558,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 +1613,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 +1639,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 +1870,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 +2205,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 +2225,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 +2347,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 +2868,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 +2889,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 +2961,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 +2969,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 +3046,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 +4026,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 +4126,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..92c5e2b 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,11 @@ #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 + const char *ifconfig_netmask =3D NULL; + const in_addr_t netmask =3D netbits_to_netmask(tt->netbits); struct argv argv =3D argv_new(); struct gc_arena gc =3D gc_new(); @@ -1308,7 +1346,10 @@ * 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 + ifconfig_netmask =3D print_in_addr_t(netmask, 0, &gc); #endif =20 #if defined(TARGET_LINUX) @@ -1324,16 +1365,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,8 +1380,11 @@ #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); #elif defined(TARGET_SOLARIS) @@ -1354,7 +1396,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 +1411,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 +1432,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 +1455,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 +1463,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 +1480,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 +1493,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 +1511,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 +1522,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 +1545,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 +1569,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 +1582,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 +1606,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 +1619,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 +1701,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 +1712,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 +4722,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 +5409,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 +6467,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 +6496,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 +6525,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: 1 Gerrit-Owner: ralf_lici Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --jpuP3aF4tkQ= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

ralf_lici would like plaisthos and flichtenheld to review this change.

Vi= ew Change

Support CIDR on options and extend netbits usage

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

Some of the in= ternal logic has also been modified to use netbits instead
of netmasks.<= br>
Change-Id: Iae04ad8715e40dfc76475c2c5b9a766c9604efc9
Signed-off-b= y: Ralf Lici <ralf@...2726...>
---
M src/openvpn/basic.h
M s= rc/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<= br>M src/openvpn/push.c
M src/openvpn/route.c
M src/openvpn/route.hM src/openvpn/socket.c
M src/openvpn/socket.h
M src/openvpn/tun.cM src/openvpn/tun.h
17 files changed, 521 insertions(+), 284 deletions= (-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/=
39/739/1
diff --git a/src/openvpn/basic.h b/src/openvpn/basic.h
index d1075b2..6658008 100644
--- a/src/openvp= n/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 --g= it a/src/openvpn/clinat.c b/src/openvpn/clinat.c
index 2d3b= 359..eaf5abb 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"
<= span>

static bool
add_entry(struct clien= t_nat_option_list *dest,
@@ -60,11 +61,11 @@
for (i =3D 0; i < list->n; ++i)
{=
const struct client_nat_entry *e =3D &lis= t->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,
pri= nt_in_addr_t(e->network, IA_NET_ORDER, &gc),
- print_in_addr_t(e->netma= sk, IA_NET_ORDER, &gc),
+ e->netbits,
pri= nt_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, &= quot;snat"))
@@ -127,19 +129,35 @@
= return;
}

- e.network =3D getaddr(0, network, 0, &= amp;ok, NULL);
+ e= .network =3D getaddr(0, network, &netbits, 0, &ok, NULL); if (!ok)
{
msg(ms= glevel, "client-nat: bad network: %s", network);
= return;
}
- e.netmask =3D getaddr(0, netmask, 0, &ok, NULL);<= /span>
- 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(mas= k);
+
+ if (!ok || bits <=3D 0)<= /span>
+ {
= + msg(msglevel, &quo= t;client-nat: bad netmask: %s", netmask);
+ return;
+ }
+ msg(M_WARN, "client-nat network parameter can= also be specified as a CIDR address instead of using the separate netmask = parameter");
+ = e.netbits =3D bits;
+ }
+ else= if (netbits > 0)
= + {
+ e.net= bits =3D netbits;
+ = }
+ else<= br>+ {
+ msg(msglevel, "client-nat: n= etmask 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, &= quot;client-nat: bad foreign network: %s", foreign_network);@@ -210,6 +228,7 @@
for (i =3D 0; i < list-&= gt;n; ++i)
{
const struct cli= ent_nat_entry *e =3D &list->entries[i]; /* current NAT rule */

+ const in_addr_t n= etmask =3D netbits_to_netmask((int)e->netbits);
= if (e->type ^ direction)
{
= addr =3D *(addr_ptr =3D &h->ip.daddr);
@@ = -231,13 +250,13 @@
to =3D &e->foreign_n= etwork;
}

- if (((addr & e->netmask) =3D= =3D *from) && !(amask & alog))
+ if (((addr & netmask) =3D=3D *from) &= ;& !(amask & alog))
{
= /* pre-adjust IP checksum */
ADD_CHEC= KSUM_32(accumulate, addr);

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

/* post-adjust IP= checksum */
SUB_CHECKSUM_32(accumulate, addr)= ;
diff --git a/src/openvpn/clinat.h b/src/openvpn/clinat.h<= /span>
index 94141f5..bebd012 100644
--- a/src/ope= nvpn/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 ch= ar *
-print_opt_route(c= onst in_addr_t network, const in_addr_t netmask, struct gc_arena *gc)
+print_opt_route(const in_= addr_t network, const int netbits, struct gc_arena *gc)
{<= /span>
struct buffer out =3D alloc_buf_gc(128, gc); ASSERT(network);

- if (netmask)
- {
- buf_printf(&out, "route %s %s",
- print_in_addr_t(netw= ork, 0, gc),
- = print_in_addr_t(netmask, 0, gc));
- }
- else
+ i= f (netbits < 0)
{
buf_prin= tf(&out, "route %s",
prin= t_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, cons= t in_addr_t netmask, struct options *o)
+helper_add_route(const in_addr_t network, const int net= bits, struct options *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_sub= net(const char *opt, const in_addr_t a, const in_addr_t b, const in_addr_t = subnet)
+verify_commo= n_subnet(const char *opt, const in_addr_t a, const in_addr_t b, const unsig= ned 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 ar= e not in the same %s subnet",
+ msg(M_USAGE, "%s IP addresses %s and %s are not= in the same /%d subnet",
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-&g= t;client)
{
msg(M_USA= GE, "--server and --client cannot be used together");
<= span>@@ -296,34 +293,24 @@
msg(M_USAGE, "= --server directive only makes sense with --dev tun or --dev tap");

}

- status =3D netmask_to_netbits(o->server_netw= ork, o->server_netmask, &netbits);
- if (!status)
- {
- msg(M_USAGE, "--server directive network/netmask co= mbination is invalid");
- }
-

- if (netbits < 0= )
- {
= - msg(M_USAGE, "-= -server directive netmask is invalid");
- }
-
- if= (netbits < IFCONFIG_POOL_MIN_NETBITS)
+ if (o->server_netbits < IFCONFIG_POOL_M= IN_NETBITS)
{
msg(M_U= SAGE, "--server directive netmask allows for too many host 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_TUN)
= {
int pool_end_reserve =3D 4;

- = if (netbits > 29)
+ if (o->server_netbits > 29)
= {
msg(M_USAGE, "--server dire= ctive 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)
{
poo= l_end_reserve =3D 0;
}
@@ -342= ,14 +329,14 @@
{
= o->ifconfig_pool_defined =3D true;
= o->ifconfig_pool_start =3D o->server_network + 4;<= br>- o->ifc= onfig_pool_end =3D (o->server_network | ~o->server_netmask) - pool_en= d_reserve;
+ = o->ifconfig_pool_end =3D (o->server_network | ~server_netm= ask) - pool_end_reserve;
ifconfig_pool= _verify_range(M_USAGE, o->ifconfig_pool_start, o->ifconfig_pool_end);=
}

- helper_add_route(o->serv= er_network, o->server_netmask, o);
+ helper_add_route(o->server_network, o-= >server_netbits, o);
if (o->enable_c= 2c)
{
- push_option(o, print_opt_route(o->s= erver_network, o->server_netmask, &o->gc), M_USAGE);
+ push_option(= o, print_opt_route(o->server_network, o->server_netbits, &o->g= c), M_USAGE);

}
= else if (o->topology =3D=3D TOP_NET30)
= {
@@ -359,16 +346,16 @@
= else if (o->topology =3D=3D TOP_SUBNET)
{<= /span>
o->ifconfig_local =3D print_in_addr_t(o= ->server_network + 1, 0, &o->gc);
- o->ifconfig_remote_netmask =3D pri= nt_in_addr_t(o->server_netmask, 0, &o->gc);
+ o->ifconfig_remote_net= mask =3D print_in_addr_t(server_netmask, 0, &o->gc);

if (!(o->server_flags & SF_NOPOO= L))
{
= 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_netmask) - 1;
+ o->ifconfig_= pool_end =3D (o->server_network | ~server_netmask) - 1;

= ifconfig_pool_verify_range(M_USAGE, o->ifconfig_poo= l_start, o->ifconfig_pool_end);
}
- o->ifcon= fig_pool_netmask =3D o->server_netmask;
+ o->ifconfig_pool_netbits =3D o-&g= t;server_netbits;

push_o= ption(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 (netbi= ts > 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, &= amp;o->gc);
- = o->ifconfig_remote_netmask =3D print_in_addr_t(o->server_netmask= , 0, &o->gc);
= + o->ifconfig_remote_netmask =3D print_in_addr_t(server_netma= sk, 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_netmask) - = 1;
+ o= ->ifconfig_pool_end =3D (o->server_network | ~server_netmask) - 1;
ifconfig_pool_verify_range(M_USAGE, o->if= config_pool_start, o->ifconfig_pool_end);
}=
- o->ifc= onfig_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_USAG= E);
}
@@ -425,7 +412,7 @@ {
o->push_ifconfig_constra= int_defined =3D true;
o->push_ifconfig_cons= traint_network =3D o->server_network;
- o->push_ifconfig_constraint_netmask =3D o= ->server_netmask;
= + o->push_ifconfig_constraint_netbits =3D o->server_netbit= s;
}
}

@@ -478,9 +465,9 @@

= if (o->server_bridge_defined)
{
- verify_common_subnet(&quo= t;--server-bridge", o->server_bridge_ip, o->server_bridge_pool_s= tart, 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_brid= ge_ip, o->server_bridge_pool_end, o->server_bridge_netmask);+ verify_common_su= bnet("--server-bridge", o->server_bridge_ip, o->server_brid= ge_pool_start, o->server_bridge_netbits);
+ verify_common_subnet("--server-br= idge", o->server_bridge_pool_start, o->server_bridge_pool_end, o= ->server_bridge_netbits);
+ verify_common_subnet("--server-bridge", o-&g= t;server_bridge_ip, o->server_bridge_pool_end, o->server_bridge_netbi= ts);
}

= o->mode =3D MODE_SERVER;
@@ -492,7 +479,7 @@
<= span> o->ifconfig_pool_start =3D o->server_bridge_pool_st= art;

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_netma= sk =3D o->server_bridge_netmask;

+ o->ifconfig_pool_netbits =3D o->server_br= idge_netbits;
push_option(o, print_opt_route_g= ateway(o->server_bridge_ip, &o->gc), M_USAGE);
= }
else if (o->server_bridge_proxy_dhcp &= amp;& !(o->server_flags & SF_NO_PUSH_ROUTE_GATEWAY))
<= span>diff --git a/src/openvpn/init.c b/src/openvpn/init.c

i= ndex 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 i= fconfig_netbits,
const char *ifconfig_remote,<= /span>
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_netm= ask(ifconfig_netbits), 0, &gc);
+ }
+ else
+ = {
+ = ifconfig_remote =3D "";
+ }
}
if (!c= ontext)
{
@@ -1948,7 +1956,8 @@=
dev_type_string(c->options.dev, c->opt= ions.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->remot= e_netmask, IA_EMPTY_IF_UNDEF, &gc),
+ c->c1.tuntap->netbits,=
+ print_= in_addr_t(c->c1.tuntap->remote, IA_EMPTY_IF_UNDEF, &gc), "init",
= NULL,
"up",<= br>@@ -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_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-&g= t;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_ad= dr_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;<= /span>
#ifdef _WIN32
adapter_index =3D c->= ;c1.tuntap->adapter_index;
@@ -2077,7 +2088,8 @@<= br> NULL,
= c->c2.frame.tun_mtu,
prin= t_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
- print_in_addr_t(remote_ne= tmask, IA_EMPTY_IF_UNDEF, &gc),
+ netbits,
+ print_in_addr_t(remote= , IA_EMPTY_IF_UNDEF, &gc),
&qu= ot;init",
signal_description(= c->sig->signal_received,
= c->sig->signal_text),
@@ -2107,7 +211= 9,8 @@
NULL,
= c->c2.frame.tun_mtu,
pr= int_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
- print_in_addr_t(remote_netm= ask, IA_EMPTY_IF_UNDEF, &gc),
+ netbits,
+ print_in_addr_t(remote, IA_EMPTY= _IF_UNDEF, &gc),
"init",=
signal_description(c->sig->sign= al_received,
c->= sig->signal_text),
@@ -2137,7 +2150,8 @@
NULL,

c-= >c2.frame.tun_mtu,
print_in_add= r_t(local, IA_EMPTY_IF_UNDEF, &gc),
- print_in_addr_t(remote_netmask, I= A_EMPTY_IF_UNDEF, &gc),
+ netbits,
+ print_in_addr_t(remote, IA_EMP= TY_IF_UNDEF, &gc),
"resta= rt",
signal_description(c->= ;sig->signal_received,
= c->sig->signal_text),
diff --git a/src/ope= nvpn/manage.c b/src/openvpn/manage.c
index 05b5a1a..a88ee5f= 100644
--- a/src/openvpn/manage.c
+++ b/sr= c/openvpn/manage.c
@@ -554,7 +554,7 @@
= {
/* IP:port specified */
bool status;

- const in_addr_t addr =3D getaddr(GETADDR_HOST_ORDER|GETADD= R_MSG_VIRT_OUT, p1, 0, &status, NULL);
+ const in_addr_t addr =3D getaddr(GETADDR= _HOST_ORDER|GETADDR_MSG_VIRT_OUT, p1, NULL, 0, &status, NULL); if (status)
{
<= span> const int port =3D atoi(p2);

@@ -2803= ,6 +2803,7 @@
"untrusted_ip=3D",<= br> "ifconfig_local=3D",
&= quot;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
inde= x 0509911..aba1a0f 100644
--- a/src/openvpn/multi.c<= br>+++ 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_net= mask & c->c2.push_ifconfig_local) =3D=3D o->push_ifconfig_constra= int_network;
+ = const unsigned int var =3D 32 - o->push_ifconfig_constraint_netbits;
+ 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_t= ype =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->context.c2.push_i= fconfig_remote_netmask =3D netbits_to_netmask((int)mi->context.options.i= fconfig_pool_netbits);
if (!mi->con= text.c2.push_ifconfig_remote_netmask)
= {
- = mi->context.c2.push_ifconfig_remote_netmask =3D mi->context.c1.tu= ntap->remote_netmask;
+ mi->context.c2.push_ifconfig_remote_netmask= =3D netbits_to_netmask((int)mi->context.c1.tuntap->netbits);<= br> }
} 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_network,= 0, &gc);
- = const char *ifconfig_constraint_netmask =3D
- print_in_addr_t(mi->context.options.pu= sh_ifconfig_constraint_netmask, 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->conte= xt.options.push_ifconfig_constraint_netbits);
}=

/*
diff --git a/src/openvp= n/options.c b/src/openvpn/options.c
index 649f48b..84e9db4 = 100644
--- a/src/openvpn/options.c
+++ b/sr= c/openvpn/options.c
@@ -1075,7 +1075,7 @@
= #endif /* ifndef _WIN32 */

static in_add= r_t
-get_ip_addr(const = char *ip_string, int msglevel, bool *error)
+get_ip_addr(const char *ip_string, unsigned int *ne= tbits, int msglevel, bool *error)
{
u= nsigned int flags =3D GETADDR_HOST_ORDER;
bool succeed= ed =3D false;
@@ -1086,7 +1086,7 @@
= flags |=3D GETADDR_FATAL;
}
=
- ret =3D getaddr(flags, i= p_string, 0, &succeeded, NULL);
+ ret =3D getaddr(flags, ip_string, netbits, 0, &succ= eeded, NULL);
if (!succeeded && error)<= br> {
*error =3D true;
@= @ -1333,7 +1333,7 @@
if (ip_addr_dotted_quad_safe(= parm)) /* FQDN -- IP address only */
{
<= span> bool error =3D false;

- const in_addr_t addr =3D get_ip_addr(parm, ms= glevel, &error);
= + const in_addr_t addr =3D get_ip_addr(parm, NULL, msglevel, &am= p;error);
if (!error)
= {
array[(*len)++] =3D addr;@@ -1513,12 +1513,10 @@
{
stru= ct 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));<= /span>
+ msg(D_SHOW_PARMS= , " server_network =3D %s/%d", print_in_addr_t(o->server_netw= ork, 0, &gc), o->server_netbits);
msg(D_SHOW_PA= RMS, " server_network_ipv6 =3D %s", print_in6_addr(o->server_= network_ipv6, 0, &gc) );
SHOW_INT(server_netbits_i= pv6);
- msg(D_SHOW_P= ARMS, " server_bridge_ip =3D %s", print_in_addr_t(o->server_b= ridge_ip, 0, &gc));
- msg(D_SHOW_PARMS, " server_bridge_netmask =3D %s", print_i= n_addr_t(o->server_bridge_netmask, 0, &gc));
+ msg(D_SHOW_PARMS, " server_brid= ge_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_brid= ge_pool_start, 0, &gc));
msg(D_SHOW_PARMS, " = server_bridge_pool_end =3D %s", print_in_addr_t(o->server_bridge_p= ool_end, 0, &gc));
if (o->push_list.head)
@@ -1534,9 +1532,8 @@
}
}

SHOW_BOOL(ifconfig_pool_defined);
- msg(D_SHOW_PARMS, " ifco= nfig_pool_start =3D %s", print_in_addr_t(o->ifconfig_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", p= rint_in_addr_t(o->ifconfig_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(G= ETADDR_HOST_ORDER, network_str, 0, NULL, NULL);
- ir->netbits =3D 32; /* host rout= e if no netmask given */
+ ir->network =3D getaddr(GETADDR_HOST_ORDER, network_str, &n= etbits, 0, NULL, NULL);
+ ir->netbits =3D 32; /* host route if no netmask or net= bits given */

if (netmask_str)
{
- = const in_addr_t netmask =3D getaddr(GETADDR_HOST_ORDER, netmask_str, 0,= NULL, NULL);
+ = const in_addr_t netmask =3D getaddr(GETADDR_HOST_ORDER, netmask_str, NULL= , 0, NULL, NULL);
ir->netbits =3D netmask_to_ne= tbits2(netmask);

if (ir->netb= its < 0)
@@ -1614,6 +1612,11 @@
= netmask_str);
return;
}

+ = msg(M_WARN, "iroute network parameter can also be specified as a CIDR = address instead of using the separate netmask parameter");
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+ else if (netbits)
+ {
+ ir->netbits =3D (int)netbits;
}


ir->next =3D o->irout= es;
@@ -2631,9 +2634,9 @@
{=
msg(M_USAGE, "--connect-freq only works with --= mode server --proto udp. Try --max-clients instead.");
}

- i= f (!(dev =3D=3D DEV_TYPE_TAP || (dev =3D=3D DEV_TYPE_TUN && options= ->topology =3D=3D TOP_SUBNET)) && options->ifconfig_pool_netm= ask)
+ if (!(d= ev =3D=3D DEV_TYPE_TAP || (dev =3D=3D DEV_TYPE_TUN && options->t= opology =3D=3D TOP_SUBNET)) && options->ifconfig_pool_netbits &g= t; 0)
{
- msg(M_USAGE, "The third parameter to --ifconfig= -pool (netmask) is only valid in --dev tap mode");
+ msg(M_USAGE, "The netb= its parameter to --ifconfig-pool is only valid in --dev tap mode");
}
if (options->routes &a= mp;& (options->routes->flags & RG_ENABLE))
= {
@@ -6092,11 +6095,29 @@
i= proute_path =3D p[1];
}
#endif=
- else if (streq(p[0], &qu= ot;ifconfig") && p[1] && p[2] && !p[3])=
+ else if (streq(p[0], &= quot;ifconfig") && p[1] && !p[3])
= {
+ bool erro= r =3D false;
+=
VERIFY_PERMISSION(OPT_P_UP);
- if (ip_or_dns_addr_safe(p[1], options->= ;allow_pull_fqdn) && ip_or_dns_addr_safe(p[2], options->allow_pu= ll_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, msglev= el, &error);
+ = if (error || !netbits)
+ {
+ msg(msglevel, "ifconfig parm '%s' must b= e a valid address/bits", p[1]);
+ goto err;
+ }
+ options->ifconfig_local =3D strtok(p[1], &q= uot;/");
+ = /* convert netbits to netmask to unify the storage as string+ * representatio= n of in_addr_t variables and simplify parsing */
+ options->ifconfig_remote_netmas= k =3D print_in_addr_t(netbits_to_netmask((int)netbits), 0, &options->= ;gc);
+ }
+ else 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 */
+ {
+ /* don't suggest CI= DR usage here since we don't know if it's a

+ * remote address or a netmas= k */
options->ifconfig_local =3D p[1];
options->ifconfig_remote_netmask =3D p[2];
}
@@ -6999,35 +7020,62 @@
VERIFY_PERMISSION(OPT_P_PERSIST_IP);

= options->persist_remote_ip =3D true;
}
- else if (streq(p[0], "cli= ent-nat") && p[1] && p[2] && p[3] && p= [4] && !p[5])

+ else if (streq(p[0], "client-nat") && p[1] &&am= p; 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;<= br>+ const char *foreign= _network =3D p[i];
+<= /span>
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))
<= span style=3D"color: hsl(120, 100%, 40%);">+ {

+ 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) && !is_special_addr(p[1])) /* FQDN -- = may be DNS name */
+<= /span>
+ const ch= ar *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(msgl= evel, "route parameter network/IP '%s' must be a valid address= ", p[1]);
+ = msg(msglevel, "route parameter network/IP '%s' must= be a valid address or address/bits", p[1]);
= goto err;
}
- if (p[2] && !ip_addr_dotted_= quad_safe(p[2])) /* FQDN -- must be IP address */
+ if (!cidr && p[i])=
{
- 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_f= qdn) && !is_special_addr(p[3])) /* FQDN -- may be DNS name */
+ if (p[i] &= ;& !ip_or_dns_addr_safe(p[i], options->allow_pull_fqdn) && != is_special_addr(p[i])) /* FQDN -- may be DNS name */
= {
- = msg(msglevel, "route parameter gateway '%s' must be a valid= address", p[3]);
- 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= for p[1] */
+ = add_route_to_option_list(options->routes, p[1], NULL, p[2], p[3]);<= /span>
+ }
= + else
+ {
+ add_route_to_option_list(options-&g= t;routes, p[1], p[2], p[3], p[4]);
+ }
}
else i= f (streq(p[0], "route-ipv6") && p[1] && !p[4])
{
@@ -7348,33 +7396,57 @@
VERIFY_PERMISSION(OPT_P_GENERAL);

optio= ns->occ =3D false;
}
- else if (streq(p[0], "server") &&a= mp; p[1] && p[2] && !p[4])
+ else if (streq(p[0], "server") &&a= mp; p[1] && !p[4])
{
= const int lev =3D M_WARN;
bool error =3D false;
- in_addr_t networ= k, 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);<= /span>
- netmask =3D ge= t_ip_addr(p[2], lev, &error);
- if (error || !network || !netmask)
+ network =3D get_ip_addr(p[i++], = &netbits, lev, &error);
+ if (netbits)
+ {
+ {+ 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);+ if (netmask &= ;& nb > 0)
+ = {
+ = msg(M_WARN, "server network parameter can also be specified as= a CIDR address instead of using the separate netmask parameter");
+ netbits= =3D nb;
+ = }
+ else<= /span>
+ {=
+ msg(M_USAG= E, "--server directive network/netmask combination is invalid");<= /span>
+ goto= err;
+ }<= /span>
+ }
= +
+ if (error || !network || !netbits)=
{
msg(msglevel, "error= parsing --server parameters");
goto err;=
}
options->server_def= ined =3D true;
options->server_network =3D netw= ork;
- options-&= gt;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 recognized flag", p[3]);
+ msg(msglevel, "= ;error parsing --server: %s is not a recognized flag", p[i]);
goto err;
} }
@@ -7403,25 +7475,43 @@
= options->server_network_ipv6 =3D network;
= options->server_netbits_ipv6 =3D netbits;
}
- else if (streq(p[0], &= quot;server-bridge") && p[1] && p[2] && p[3] &= amp;& 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, poo= l_end;
+ unsig= ned int netbits =3D 0;
- ip =3D get_ip_addr(p[1], lev, &error);
- netmask =3D get_ip_addr(p[2], l= ev, &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++], &a= mp;netbits, lev, &error);
+ if (netbits)
+ {
= + if (!no_more_than_n_args(msglevel, p, 4, NM_QUOTE_HINT))
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ goto err;
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+ else
+ {
+ const in_addr_t netmask =3D get_ip_addr(p[i++]= , NULL, lev, &error);
+ const int nb =3D netmask_to_netbits2(netmask);
= + if (netmask &&= amp; nb > 0)
+ = {
+ = msg(M_WARN, "server-bridge gateway parameter can also be specifi= ed as a CIDR address instead of using the separate netmask parameter")= ;
+ ne= tbits =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;
- option= s->server_bridge_netmask =3D netmask;
+ options->server_bridge_netbits =3D netbits;=
options->server_bridge_pool_start =3D pool_sta= rt;
options->server_bridge_pool_end =3D pool_en= d;
}
@@ -7456,15 +7546,27 @@ {
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 netb= its =3D 0;

VERIFY_PERMISSION(OPT= _P_GENERAL);
- s= tart =3D get_ip_addr(p[1], lev, &error);
- end =3D get_ip_addr(p[2], lev, &error);<= /span>
- if (p[3])
+ start =3D get_ip= _addr(p[1], &netbits, lev, &error);
+ end =3D get_ip_addr(p[2], NULL, lev, &e= rror);
+ if (!= netbits && p[3])
{
- netmask =3D get_ip_addr(p[3], l= ev, &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)+ {
+ msg(M_WARN, "i= fconfig-pool start-IP parameter can also be specified as a CIDR address ins= tead of using the separate netmask parameter");

+ netbits =3D nb;+ }
}

+ = else if (netbits && !no_more_than_n_args(msglevel, p, 3, NM_QUOTE_H= INT))
+ {
+ goto err;
+ }
+

if (er= ror)
{
msg(msglevel, = "error parsing --ifconfig-pool parameters");
@@ -= 7478,10 +7580,7 @@
options->ifconfig_pool_defin= ed =3D true;
options->ifconfig_pool_start =3D s= tart;
options->ifconfig_pool_end =3D end;
- if (netmask)=
- {
- options->ifconfig_pool_ne= tmask =3D netmask;
- = }
+ optio= ns->ifconfig_pool_netbits =3D netbits;
}
= else if (streq(p[0], "ifconfig-pool-persist") &&am= p; p[1] && !p[3])
{
@@ -7802,4= 5 +7901,77 @@
VERIFY_PERMISSION(OPT_P_INSTANCE);
option_iroute_ipv6(options, p[1], msglevel);=
}
- e= lse 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;
+ unsig= ned int netbits;
+ = size_t i =3D 1;

VERIFY_PERM= ISSION(OPT_P_INSTANCE);
- local =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[1], 0, NU= LL, NULL);
- rem= ote_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, N= ULL, NULL);
+ = if (netbits) /* convert netbits to netmask */
+ {
+ if (!no_more_than_n_args(msglevel, p, 3, NM_QUO= TE_HINT))
+ = {
+ = goto err;
+ = }
+ remo= te_netmask =3D netbits_to_netmask((int)netbits);
+ }
+ else if (p[i])
+ {
+ /* don't suggest CIDR usage here since we don&#= 39;t know if it's a
+ * remote address or a netmask */
+ remote_netmask =3D getaddr(GETADD= R_HOST_ORDER|GETADDR_RESOLVE, p[i++], NULL, 0, NULL, NULL);
+ }

+
if (local &&= ; remote_netmask)
{
o= ptions->push_ifconfig_defined =3D true;
opt= ions->push_ifconfig_local =3D local;
option= s->push_ifconfig_remote_netmask =3D remote_netmask;
- if (p[3])
+ if (p[i])
= {
- = options->push_ifconfig_local_alias =3D getaddr(GETADDR_HOST_ORDER= |GETADDR_RESOLVE, p[3], 0, NULL, NULL);
+ options->push_ifconfig_local_alias = =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[i], NULL, 0, NULL, NULL);=
}
}
= else
{
- msg(msglevel, "cannot parse --ifconfig-= push addresses");
-= else if (streq(p[0], "ifconfig-push-constraint") && p= [1] && p[2] && !p[3])
+ else if (streq(p[0], "ifconfig-push-constraint&q= uot;) && p[1] && !p[3])
{
- in_addr_t network, netmask= ;

+ in_addr_t = network;
+ uns= igned int netbits =3D 0;
+ size_t i =3D 1;

VER= IFY_PERMISSION(OPT_P_GENERAL);
- network =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[= 1], 0, NULL, NULL);
- = netmask =3D getaddr(GETADDR_HOST_ORDER, p[2], 0, NULL, NULL);<= br>- if (network &&= ; netmask)
+ n= etwork =3D getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[i++], &netbits= , 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;
+ }
+= }
+ el= se if (netbits && !no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HIN= T))
+ {=
+ goto err;
+ }
+

+ if (network && netbits)
{

options->push_ifconfig_constr= aint_defined =3D true;
options->push_ifconf= ig_constraint_network =3D network;
- options->push_ifconfig_constraint_netmask =3D n= etmask;
+ = options->push_ifconfig_constraint_netbits =3D netbits;
= }
else
{- msg(msglevel, &quo= t;cannot parse --ifconfig-push-constraint addresses");
+ msg(msglevel, "can= not parse --ifconfig-push-constraint parameters");

= 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
@@ -4= 50,7 +450,7 @@
const char *tmp_dir;
= bool server_defined;
in_addr_t server_network;
- in_addr_t server_netma= sk;
+ unsigned int= server_netbits;
bool server_ipv6_defined; = /* IPv6 */
struct in6_addr server_netwo= rk_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_net= bits;

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_add= r_t ifconfig_pool_end;
= - in_addr_t ifconfig_pool_netmask;
+ unsigned int ifconfig_pool_netbits;
= const char *ifconfig_pool_persist_filename;
int if= config_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_i= fconfig_constraint_netbits;
bool push_ifconfig_ipv4_bl= ocked; /* IPv4 */
bool push_ifconfi= g_ipv6_defined; /* IPv6 */
struct i= n6_addr push_ifconfig_ipv6_local; /* IPv6 */
diff= --git a/src/openvpn/pool.c b/src/openvpn/pool.c
index e3c3= 708..84227b6 100644
--- a/src/openvpn/pool.c
+++ b/src/openvpn/pool.c

@@ -652,8 +652,8 @@
if (strlen(ip_buf) > 0)

{<= /span>
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, NULL, 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/o= penvpn/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, &statu= s1, NULL);
- = const in_addr_t netmask =3D getaddr(GETADDR_HOST_ORDER, p[2] ? p[2= ] : "255.255.255.255", 0, &status2, NULL);
+ bool status1, stat= us2 =3D true;
+ = int netbits =3D -1;
+
+ = /* get netbits from CIDR or convert netmask to netbits */
+ co= nst in_addr_t network =3D getaddr(GETADDR_HOST_ORDER, p[1], (unsigned int *= )&netbits, 0, &status1, NULL);
+ if (p[2])
+ {
+ const in_addr_t netm= ask =3D getaddr(GETADDR_HOST_ORDER, p[2], NULL, 0, &status2, NULL);
+ = netbits =3D netmask_to_netbits2(netmask);
+ }
+ if (netbits <=3D 0)<= br>+ {
+ n= etbits =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 N= ULL; ir =3D ir->next)
{<= br>- i= f (network =3D=3D ir->network && netmask =3D=3D netbits_to_netma= sk(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..b7434= 33 100644
--- a/src/openvpn/route.c
+++ b/s= rc/openvpn/route.c
@@ -189,9 +189,9 @@
rou= te_string(const struct route_ipv4 *r, struct gc_arena *gc)
= {
struct buffer out =3D alloc_buf_gc(256, gc);=
- buf_printf(&out, &qu= ot;ROUTE network %s netmask %s gateway %s",
+ buf_printf(&out, "ROUTE network %= s/%d gateway %s",
print_in_addr_t(r-&g= t;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_DEFINE= D)
@@ -321,11 +321,17 @@
const = struct route_option *ro,
const struct route_lis= t *rl)
{
- const in_addr_t default_netmask =3D IPV4_NETMASK_HOST;
+ const in_addr_t default_netb= its =3D IPV4_NETBITS_HOST;

bool status;
int ret;

struct in_addr special =3D {0};=

+ /* s= eparate address and prefix if present */
+ char *network =3D strdup(ro->network);+ const char *cidr =3D str= chr(network, '/');
+ const char *addr =3D strtok(network, "/");
<= span style=3D"color: hsl(120, 100%, 40%);">+ const char *prefix =3D strt= ok(NULL, "/");

+
CLEAR(*r);
r->option =3D= ro;

@@ -355,7 +361,7 @@
= else
{
ret =3D openvpn_g= etaddrinfo(GETADDR_RESOLVE | GETADDR_WARN_ON_SIGNAL,
- ro->ne= twork, NULL, 0, NULL, AF_INET, network_list);
+ addr, NULL, 0, = NULL, AF_INET, network_list);
}

status =3D (ret =3D=3D 0);
@@ -365,25 +371= ,41 @@
goto fail;
}
- /* netmas= k */
+ /* netbits = */

= + r->netbits =3D default_netbits;
if (is_route_p= arm_defined(ro->netmask))
{
- r->netmask =3D getaddr(+ const in_addr_t netm= ask =3D getaddr(
GETADDR_HOST_ORDER
= | GETADDR_WARN_ON_SIGNAL,
r= o->netmask,
+ = NULL,
0,
&= amp;status,
NULL);
- if (!status)
+ const int netbits =3D netmask_to_netbits= 2(netmask);
+<= br>+ if (!status || netb= its < 0)
{
goto fa= il;
}
+ msg(M_WARN, "route parameter network/IP can also be= specified as a CIDR address instead of using the separate netmask paramete= r");
+ r-= >netbits =3D netbits;
}
- else
+ else if (cidr)
{
- r->netmask =3D default_netmas= k;
+ if (!pref= ix) /* handle 'aa.bb.cc.dd/' case */
+ {
+ goto fail;
+ }
+ char *endp =3D NULL;
+ const unsigned long netbits =3D strtoul(prefix, &a= mp;endp, 10);
+ = if ((*endp !=3D '\0') || (netbits > sizeof(in_addr_t) * 8))
+ {
+ goto fail;
+ }
+ r->netbits =3D netbits;
}

/* gateway */
@@ -397,6 +419,7 @@
| GETADDR= _HOST_ORDER
| GETADDR_WARN_ON_SIGNAL,
ro->gateway,
+ NULL,
= 0,
&status,
= NULL);
@@ -428,7 +451,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 +465,13 @@
r->flags |=3D RT_DEFINED;

+ free(network);

= return true;

fail:
= msg(M_WARN, PACKAGE_NAME " ROUTE: failed to parse/resolve route f= or host/network: %s",
ro->network);=
+ free(network);<= br> return false;
}

= @@ -516,7 +541,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 n= etwork has CIDR notation */
ro->gateway =3D gateway= ;
ro->metric =3D metric;
ro-&g= t;next =3D l->routes;
@@ -582,7 +607,7 @@
r1->flags =3D RT_DEFINED;

r1->gat= eway =3D target;
r1->network =3D gateway->ad= dr & gateway->netmask;
- r1->netmask =3D ~(l2-1);
+ r1->netbits =3D netmask_to_netbits2(gate= way->netmask) + 1;
r1->next =3D rl->route= s;
rl->routes =3D r1;
<= br>@@ -681,6 +706,7 @@
| GETADDR_HOST_OR= DER
| GETADDR_WARN_ON_SIGNAL,
= remote_endpoint,
+ NULL,
0,
&defined,

NULL);
<= span>@@ -921,7 +947,7 @@


static bool
add_route3(in_addr_t network,
- in_addr_t netmask,
+ unsigned int netbits, in_addr_t gateway,
const st= ruct tuntap *tt,
unsigned int flags,
= @@ -933,14 +959,14 @@
CLEAR(r);
= r.flags =3D RT_DEFINED;
r.network =3D network;
- r.netmask =3D netmas= k;
+ r.netbits =3D= netbits;
r.gateway =3D gateway;
= return add_route(&r, tt, flags, rgi, es, ctx);
}

static void
del_route3(in_ad= dr_t network,
- = in_addr_t netmask,
+ unsigned int netbits,
in_addr_t ga= teway,
const struct tuntap *tt,
unsigned int flags,

@@ -952,7 +978,7 @@=
CLEAR(r);
r.flags =3D RT_DEFINED|RT_ADD= ED;
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 +997,7 = @@
{
if (rb->bypass[i])
{
+ = ret =3D add_route3(rb->bypass[i], IPV4_NETBITS_HOST, gateway, tt,
flags | ROUTE_REF_GW, rgi, es, ctx= ) && ret;
}
}<= br>@@ -993,7 +1019,7 @@
if (rb->bypass[i]= )
{
del_route3(rb->= ;bypass[i],
- = IPV4_NETMASK_HOST,
+ IPV4_NETBITS_HOST,
= gateway,
tt,
flags | ROUTE_REF_GW,
= @@ -1057,7 +1083,7 @@
if ((rl->spec.fla= gs & 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 | = ROUTE_REF_GW,
&rl= ->rgi, es, ctx);
rl->iflags |=3D= RL_DID_LOCAL;
@@ -1078,11 +1104,11 @@
= if (rl->flags & RG_DEF1)
= {
/* add new default route (1st com= ponent) */
- = ret =3D add_route3(0x00000000, 0x80000000, rl->spec.remote_endp= oint,
+ = 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(0x800000= 00, 0x80000000, rl->spec.remote_endpoint,
+ ret =3D add_route3(0x80000000,= 1, rl->spec.remote_endpoint,
= tt, flags, &rl->rgi, es, ctx) && ret;
= }
else
@@ -1120,7 +1146,7 @@

if (rl->iflags & = RL_DID_LOCAL)
{
del_r= oute3(rl->spec.remote_host,
- IPV4_NETMASK_HOST,
+ IPV4_NETBITS_HOST,
rl->rgi.gateway.addr,
tt,

fla= gs | ROUTE_REF_GW,
@@ -1140,7 +1166,7 @@
= {
/* delete default route (1st = component) */
del_route3(0x00000000,
- = 0x80000000,
+ = 1,
rl->sp= ec.remote_endpoint,
tt,<= br> flags,
@@ -1150,7 +117= 6,7 @@

/* delete default= route (2nd component) */
del_route3(0x800= 00000,
- = 0x80000000,
+ 1,
= rl->spec.remote_endpoint,
= tt,
flags,
@@ = -1215,7 +1241,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->netbits), "route");
if (flags & ROUTE_DELETE_FIRST)

= {
delete_route(r, tt, flags, &rl= ->rgi, es, ctx);
@@ -1432,14 +1458,18 @@
if (r->flags & RT_DEFINED)

{
setenv_route_addr(es, "network", r->network, i);<= /span>
- setenv_route_a= ddr(es, "netmask", r->netmask, i);
+ setenv_route_addr(es, "netmask&quo= t;, 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&quo= t;, i);
+ sete= nv_int(es, BSTR(&name1), (int)r->netbits);
+
if (r->flags &am= p; 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(&n= ame2, "route_metric_%d", i);
+ setenv_int(es, BSTR(&name2), r->metri= c);
}
}
= gc_free(&gc);
@@ -1519,7 +1549,7 @@

static int
local_route(in_addr_t network,<= /span>
- in_addr_t = netmask,
+ = unsigned int netbits,
in_addr_t gateway,
const struct route_gateway_info *rgi)
{

@@ -1528,7 +1558,7 @@
if (rgi
&& (rgi->flags & rgi_needed) =3D=3D = rgi_needed
&& gateway =3D=3D rgi->gatew= ay.addr
- &&= amp; netmask =3D=3D 0xFFFFFFFF)
+ && netbits =3D=3D IPV4_NETBITS_HOST)
= {
if (((network ^ rgi->gateway.addr= ) & rgi->gateway.netmask) =3D=3D 0)
{
@@ -1583,12 +1613,12 @@
#if !defined(TARGET_LIN= UX)
const char *network =3D print_in_addr_t(r->netw= ork, 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_netma= sk((int)r->netbits), 0, &gc);
#endif
const char *gateway =3D print_in_addr_t(r->gateway, 0, &gc);<= /span>
#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_ERR= OR)
{
goto done;
@@ -1609,7 +1639,7 @@

}
status =3D RTA_SUCCESS;
- int ret =3D net_route_v4_add(ctx, &r->network, ne= tmask_to_netbits2(r->netmask),
+ int ret =3D net_route_v4_add(ctx, &r->network, (in= t)r->netbits,
&r->= ;gateway, iface, 0, metric);
if (ret =3D=3D -EEXIST)
{
@@ -1840,10 +1870,9 @@
#elif defined(TARGET_AIX)


{
- int netbits =3D net= mask_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 fa= iled");
@@ -2176,14 +2205,14 @@
#if != defined(TARGET_LINUX)
network =3D print_in_addr_t(r-&g= t;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->netb= its), 0, &gc);
#endif
#if !defined(TA= RGET_ANDROID)
gateway =3D print_in_addr_t(r->gatewa= y, 0, &gc);
#endif
#endif
<= span>

- is_local_ro= ute =3D local_route(r->network, r->netmask, r->gateway, rgi);
+ is_local_route =3D l= ocal_route(r->network, r->netbits, r->gateway, rgi);
if (is_local_route =3D=3D LR_ERROR)

{ goto done;
@@ -2196,7 +2225,7 @@ metric =3D r->metric;
}
<= span>

- if (net_rou= te_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 +2347,9 @@
#elif defined(TARGET_AIX)


{
- int netbits =3D ne= tmask_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 delet= e command failed");
}
@@ -2840,13= +2868,14 @@
int ret =3D RTA_ERROR;
= DWORD status;
const DWORD if_index =3D (adapter_ind= ex =3D=3D TUN_ADAPTER_INDEX_INVALID) ? windows_route_find_if_index(r, tt) := adapter_index;
+ = const in_addr_t netmask =3D netbits_to_netmask((int)r->netbits);<= br>
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(netma= sk);
fr.dwForwardPolicy =3D 0;
= fr.dwForwardNextHop =3D htonl(r->gateway);
= fr.dwForwardIfIndex =3D if_index;
@@ -2860,11 +2889,11 @@=
fr.dwForwardMetric4 =3D METRIC_NOT_USED; fr.dwForwardMetric5 =3D METRIC_NOT_USED;
=
- if ((r->ne= twork & r->netmask) !=3D r->network)
+ if ((r->network & netmask) !=3D r= ->network)
{
msg(M= _WARN, "Warning: address %s is not a network address in relation to ne= tmask %s",
print_in_addr_t(r->netw= ork, 0, &gc),
- = print_in_addr_t(r->netmask, 0, &gc));
+ print_in_addr_t(netmas= k, 0, &gc));
}

status =3D CreateIpForwardEntry(&fr);

@@ -2= 932,6 +2961,7 @@
bool ret =3D false;
= DWORD status;
const DWORD if_index =3D windows_rou= te_find_if_index(r, tt);
+ const in_addr_t netmask =3D netbits_to_netmask((int)r->netbits)= ;

if (if_index !=3D TUN_ADAPTER_INDE= X_INVALID)
{
@@ -2939,7 +2969,7 @@
CLEAR(fr);

= fr.dwForwardDest =3D htonl(r->network);
- fr.dwForwardMask =3D htonl(r->netmask);
+ fr.dwForwardMa= sk =3D htonl(netmask);
fr.dwForwardPolicy =3D 0;
fr.dwForwardNextHop =3D htonl(r->gateway);
fr.dwForwardIfIndex =3D if_index;
@@ -3= 016,7 +3046,7 @@
.metric =3D (r->flags & RT= _METRIC_DEFINED ? r->metric : -1)
};

- netmask_to_net= bits(r->network, r->netmask, &msg.prefix_len);
+ msg.prefix_len =3D r->netbits;<= /span>
if (msg.prefix_len =3D=3D -1)
{
msg.prefix_len =3D 32;
@@ -3996,7 += 4026,7 @@
while (iplist)
{=
bool succeed =3D false;
- const in_addr_t ip =3D getaddr(GETADDR_HOST_OR= DER, iplist->IpAddress.String, 0, &succeed, NULL);
+ const in_addr_t ip =3D getadd= r(GETADDR_HOST_ORDER, iplist->IpAddress.String, NULL, 0, &succeed, N= ULL);
if (succeed)
{
add_host_route_if_nonlocal(rb, ip);
@@ -4096,7 +4126,7 @@

{
if (rgi)
{
- = if (local_route(addr, 0xFFFFFFFF, rgi->gateway.addr, rgi))=
+ if (local_route(ad= dr, 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/openv= pn/route.h
+++ b/src/openvpn/route.h
@@ -11= 7,7 +117,7 @@
unsigned int flags;
= const struct route_option *option;
in_addr_t network;=
- in_addr_t netmask= ;
+ unsigned int n= etbits;
in_addr_t gateway;
int me= tric;
};
diff --git a/src/openvpn/socket.c= b/src/openvpn/socket.c
index 17c5e76..767036f 100644
--- a/src/openvpn/socket.c
+++ b/src/openvpn/soc= ket.c
@@ -179,6 +179,7 @@
in_addr_t=
getaddr(unsigned int flags,
const char = *hostname,
+ u= nsigned int *netbits,
int resolve_retry_seconds,
bool *succeeded,
struct si= gnal_info *sig_info)
@@ -186,7 +187,7 @@
= in_addr_t addr;
int status;

- status =3D get_addr_gen= eric(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)
di= ff --git a/src/openvpn/socket.h b/src/openvpn/socket.h
inde= x 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,
<= span> bool *succeeded,

= struct signal_info *sig_info);
diff --git a/src/openvpn/tu= n.c b/src/openvpn/tun.c
index 739e008..92c5e2b 100644
--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c<= /span>
@@ -113,7 +113,7 @@
if (addr.family = =3D=3D AF_INET)
{
addr.addres= s.ipv4.s_addr =3D htonl(tt->local);
- addr.prefix_len =3D netmask_to_netbits2(tt->ada= pter_netmask);
+ = addr.prefix_len =3D tt->netbits;
msg(D_IFCON= FIG, "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_h= ow_to_silence[] =3D "(silence this warning with --ifconfig-nowarn)&quo= t;;

/*
- * 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, ma= ke sure ifconfig_remote_netmask looks
+ * If tun_p2p, make sure ifconfig_remote looks
= * like an IPv4 address.
*/
stat= ic void
-ifconfig_sanit= y_check(bool tun_p2p, in_addr_t addr)
+ifconfig_sanity_check(bool tun_p2p, in_addr_t addr, int n= etbits)
{
struct gc_arena gc =3D gc_n= ew();
- const bool l= ooks_like_netmask =3D ((addr & 0xFF000000) =3D=3D 0xFF000000);+ const bool looks_like_ne= tmask =3D addr && ((addr & 0xFF000000) =3D=3D 0xFF000000);
if (tun_p2p)
{
= if (looks_like_netmask)
@@ -521,9 +521,11 @@
<= span> }

else
{
- if (!looks_like_netmask)
+ if (netbits <= ; 0 || !looks_like_netmask)
{
- msg(M_WARN, "WARNING: Sin= ce you are using subnet topology, the second argument to --ifconfig must be= a netmask, for example something like 255.255.255.0. %s",
<= span style=3D"color: hsl(120, 100%, 40%);">+ msg(M_WARN, "W= ARNING: Since you are using topology subnet, you must either "
<= br>+ "provi= de 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.255.0).= %s",
ifconfig_warn_how_to_silence);<= /span>
}
}
@@ -539,3= 4 +541,36 @@
int type,
= in_addr_t public,
in_addr= _t local,
- = in_addr_t remote_netmask)
+ in_addr_t remote,
+ int netbits)
{<= /span>
struct gc_arena gc =3D gc_new();
#if = 0
- msg(M_INFO, &quo= t;CHECK_ADDR_CLASH type=3D%d public=3D%s local=3D%s, remote_netmask=3D%s&qu= ot;,
+ msg(M_INFO,= "CHECK_ADDR_CLASH type=3D%d public=3D%s local=3D%s, netbits=3D%d, rem= ote=3D%s",
type,
pri= nt_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 pu= blic_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 n= etmask =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 & ne= tmask;
+ c= onst 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)
{
ms= g(M_WARN,
"WARNING: --%s address = [%s] conflicts with --ifconfig address pair [%s, %s]. %s",
<= span> name,

print_= in_addr_t(public, 0, &gc),
print_i= n_addr_t(local, 0, &gc),
- print_in_addr_t(remote_netmask, 0, &gc),
+ pri= nt_in_addr_t(remote, 0, &gc),
ifco= nfig_warn_how_to_silence);
}
=
@@ -577,22 +581,23 @@
= name,
print_in_addr_t(public, 0, &= ;gc),
print_in_addr_t(local, 0, &g= c),
- = print_in_addr_t(remote_netmask, 0, &gc),
+ print_in_addr_t(remote ? rem= ote : netmask, 0, &gc),
ifconfig_w= arn_how_to_silence);
}
= }
else if (type =3D=3D DEV_TYPE_TAP)
<= span> {

- = 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_net= work)
+ co= nst in_addr_t netmask =3D netbits_to_netmask(netbits);
+ const in_addr_t public_netwo= rk =3D public &netmask;
+ const in_addr_t virtual_network =3D local & netmask= ;
+ if (ne= tmask && 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 --ifconfig subnet. %s",
+ "WARNIN= G: --%s address [%s] conflicts with --ifconfig subnet [%s/%d] -- local and = remote addresses cannot be inside of the --ifconfig subnet. %s",
name,
= print_in_addr_t(public, 0, &gc),
p= rint_in_addr_t(local, 0, &gc),
- print_in_addr_t(remote_netmask, 0, &gc= ),
+ = netbits,
ifconfig_warn_how_to_silen= ce);
}
}
@@ -669,9 +674,10 @@

{
if= (!is_tun_p2p(tt))
{
+ const in_addr_t netmask =3D netbits_t= o_netmask(tt->netbits);
buf_printf(&out= , "%s %s",
- = print_in_addr_t(tt->local & tt->remote_netm= ask, 0, gc),
- = print_in_addr_t(tt->remote_netmask, 0, gc));
+ print_in_a= ddr_t(tt->local & netmask, 0, gc),

+ print_in_addr_t(netmask, 0, gc= ));
}
else if (tt->typ= e =3D=3D DEV_TYPE_TUN) /* tun p2p topology */
{
@@ -679,12 +685,12 @@
if (remote= )
{
r =3D pri= nt_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 prin= t_in_addr_t(tt->remote_netmask, 0, gc);
+ r =3D print_in_addr_t(tt->remote,= 0, gc);
}
buf_pr= intf(&out, "%s %s", r, l);
}<= br>@@ -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_netmask, 0, &gc);
+ const char *ifconfig_remote =3D tt->remote= ? print_in_addr_t(tt->remote, 0, &gc) : NULL;
+ const char *ifconfig_netmask =3D tt-&= gt;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, "i= fconfig_local", ifconfig_local);
if (tun)
{
- setenv_str(es, "ifconfig_remote", ifconfig_remote_n= etmask);
+ = setenv_str(es, "ifconfig_remote", ifconfig_remote);
}

else
{
- setenv_str(es= , "ifconfig_netmask", ifconfig_remote_netmask);
+ setenv_int(es, "ifco= nfig_netbits", tt->netbits);
+ setenv_str(es, "ifconfig_netmask", i= fconfig_netmask);
}
}<= br>
@@ -837,7 +845,7 @@
boo= l tun_p2p =3D is_tun_p2p(tt);

/*=
- * Convert ar= guments to binary IPv4 addresses.
+ * Convert arguments to binary IPv4 addresses and net= bits.
*/

= tt->local =3D getaddr(
@@ -846,27 +854,48 @@ | GETADDR_FATAL_ON_SIGNAL
= | GETADDR_FATAL,
ifconfig_local_parm,<= br>+ NULL, 0,
NULL,
= NULL);

- tt->remote_netmask =3D getaddr(
+ tt->remote =3D getaddr(
(tun_p2p ? GETADDR_RESOLVE : 0)
= | GETADDR_HOST_ORDER
| GETADDR_FA= TAL_ON_SIGNAL
| GETADDR_FATAL,
ifconfig_remote_netmask_parm,

+ NULL,
0,
NULL,
NULL);

+ = /* in p2p mode discard the netbits otherwise discard the remote */<= br>+ 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 n= ot figure out the subnet from the remote parameter (%s)", ifconfig_rem= ote_netmask_parm);
+ = }
+ = tt->remote =3D 0;
+ }
+ = else
+ {
+ tt->ne= tbits =3D -1;
+ = }
+
/*

* Look for common errors in --ifconf= ig parms
*/
if (strict_w= arn)
{
struct addrinf= o *curele;
- = ifconfig_sanity_check(tun_p2p, tt->remote_netmask);
+ ifconfig_sanity_check(tun_p= 2p, 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_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->ai_a= ddr)->sin_addr.s_addr);
check_addr_= clash("remote",
= tt->type,
- = ((struct sockaddr_in *)curele->ai_addr)= ->sin_addr.s_addr,
+ remote,
= tt->local,
- tt->remote_netmas= k);
+ = tt->remote,
+ tt->netbits);
}
}

if (!tun_p2p)
= {
- che= ck_subnet_conflict(tt->local, tt->remote_netmask, "TUN/TAP adapt= er");
+ = check_subnet_conflict(tt->local, netbits_to_netmask(tt->netbit= s), "TUN/TAP adapter");
}
= else
{
@@ -= 914,12 +947,12 @@
*/
if = (tun_p2p)
{
- verify_255_255_255_252(tt->local, tt->remo= te_netmask);
+ = verify_255_255_255_252(tt->local, tt->remote);
= tt->adapter_netmask =3D ~3;
}
else
{
- tt->adapter_netmask =3D tt-&= gt;remote_netmask;
+ = tt->adapter_netmask =3D netbits_to_netmask(tt->netbits);
}
#endif
<= br>@@ -1062,8 +1095,9 @@
create_arbitrary_remote( st= ruct tuntap *tt )
{
in_addr_t remote;=
+ const in_addr_t= netmask =3D netbits_to_netmask(tt->netbits);
- remote =3D (tt->local &= amp; tt->remote_netmask) +1;
+ remote =3D (tt->local & netmask) +1;

if (remote =3D=3D tt->local)
= {
@@ -1300,7 +1334,11 @@

#if !defined(TARGET_LINUX)

const char *ifconfig_loc= al =3D NULL;
- const= char *ifconfig_remote_netmask =3D NULL;
+#if !defined(_WIN32)
+ const char *ifconfig_remote =3D NULL;
<= span style=3D"color: hsl(120, 100%, 40%);">+#endif

+ const char *ifconfig_netmask =3D NULL;
+ const in_addr_t n= etmask =3D netbits_to_netmask(tt->netbits);
struct = argv argv =3D argv_new();
struct gc_arena gc =3D gc_ne= w();

@@ -1308,7 +1346,10 @@
* Set ifconfig parameters

*/
ifconfig_local =3D print_in_addr_t(tt->local, 0, &gc);
<= br>- 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
+ = ifconfig_netmask =3D print_in_addr_t(netmask, 0, &gc);
#endif


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

i= f (tun_p2p)
{
- if (net_addr_ptp_v4_add(ctx, ifname, &tt->local= ,
- = &tt->remote_netmask) < 0)
+ if (net_addr_ptp_v4_add(ctx, ifname, &a= mp;tt->local, &tt->remote) < 0)
{
msg(M_FATAL, "Linux can't add IP to inte= rface %s", ifname);
}
}<= /span>
else
{
- if (net_addr_v4_add(ctx, ifname, &tt= ->local,
- = netmask_to_netbits2(tt->remote_netmask)) < 0)
+ if (net_addr_v4_a= dd(ctx, ifname, &tt->local, tt->netbits) < 0)
= {
msg(M_FATAL, "Linux can't = add IP to interface %s", ifname);
}@@ -1341,8 +1380,11 @@
#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-&g= t;topology));
+ sn= printf(out, sizeof(out), "%s %s %d %s",
+ ifconfig_local,
+ ifconfig_remote ? ifcon= fig_remote : ifconfig_netmask,
+ tun_mtu,
+ print_topology(tt->topology));
management_android_control(management, "IFCONFIG", out);

#elif defined(TARGET_SOLARIS)
@@ -1354,7 +1396,7 @@

if (tun_p2p)
{

argv_printf(&argv, "%s %s %s %s m= tu %d up", IFCONFIG_PATH, ifname,
- ifconfig_local, ifconfig_remote_netmas= k, 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 +1411,13 @@
= {
argv_printf(&argv, "%s %s %s %s net= mask %s mtu %d up", IFCONFIG_PATH,
= ifname, ifconfig_local, ifconfig_local,
- ifconfig_remote_netmask, tun_mtu);<= /span>
+ = ifconfig_netmask, tun_mtu);
}
els= e /* tap */
{
argv_printf(&am= p;argv, "%s %s %s netmask %s up",
= IFCONFIG_PATH, ifname, ifconfig_local,
- ifconfig_remote_netmask);=
+ ifconf= ig_netmask);
}

= argv_msg(M_INFO, &argv);
@@ -1390,8 +1432,8 @@ struct route_ipv4 r;
CLEAR(r);
r.flags =3D RT_DEFINED | RT_METRIC_DEFINED;- r.network =3D tt->l= ocal & tt->remote_netmask;
- r.netmask =3D tt->remote_netmask;
+ r.network =3D tt->local &am= p; netmask;
+ = r.netbits =3D tt->netbits;
r.gateway =3D tt->= ;local;
r.metric =3D 0;
a= dd_route(&r, tt, 0, NULL, es, NULL);
@@ -1413,7 +1455,7= @@
argv_printf(&argv,
= "%s %s %s %s mtu %d netmask 255.255.255.255 up -link0&qu= ot;,
IFCONFIG_PATH, ifname, ifconfig_l= ocal,
- = ifconfig_remote_netmask, tun_mtu);
+ ifconfig_remote, tun_mtu); }
else if (tt->type =3D=3D DEV_TYPE_TUN= )
{
@@ -1421,13 +1463,13 @@
= argv_printf(&argv, "%s %s %s %s mtu %d netmask %s u= p -link0",
IFCONFIG_PATH, ifname,= ifconfig_local,
print_in_addr_t(remot= e_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_netmask,= tun_mtu);
}
argv_msg(M_INFO, &am= p;argv);
openvpn_execve_check(&argv, es, S_FATAL, = "OpenBSD ifconfig failed");
@@ -1438,8 +1480,8 @@=
struct route_ipv4 r;
CLE= AR(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 +1493,14 @@
{
argv_printf(&argv, "%s %s %s %s mtu %d net= mask 255.255.255.255 up",
IFCONFI= G_PATH, ifname, ifconfig_local,
- ifconfig_remote_netmask, tun_mtu);
= + ifconfig_r= emote, tun_mtu);
}
else if (tt-&g= t;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_a= ddr_t(remote_end, 0, &gc),
- tun_mtu, ifconfig_remote_netmask);
<= span style=3D"color: hsl(120, 100%, 40%);">+ tun_mtu, if= config_netmask);

}
else /* tap */=
{
@@ -1469,7 +1511,7 @@
*/

argv_printf(&argv, "%s %s= %s netmask %s mtu %d",
IFCONFIG_= PATH, ifname, ifconfig_local,
- ifconfig_remote_netmask, tun_mtu);
+ ifconfig_net= mask, tun_mtu);

}
argv_msg(M_INFO= , &argv);
openvpn_execve_check(&argv, es, S_FA= TAL, "NetBSD ifconfig failed");
@@ -1480,8 +1522,= 8 @@
struct route_ipv4 r;
= CLEAR(r);
r.flags =3D RT_DEFINED;
- r.network =3D tt->local &a= mp; tt->remote_netmask;

- r.netmask =3D tt->remote_netmask;
+ r.network =3D tt->local & netm= ask;
+ r.netbi= ts =3D tt->netbits;
r.gateway =3D remote_end;
add_route(&r, tt, 0, NULL, es, NULL); }
@@ -1503,18 +1545,18 @@
= {
argv_printf(&argv, "%s %s %s %s mtu %d = netmask 255.255.255.255 up",
IFCO= NFIG_PATH, ifname, ifconfig_local,
- ifconfig_remote_netmask, tun_mtu);<= br>+ ifconfi= g_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, i= fconfig_local,
- = ifconfig_remote_netmask, tun_mtu);
+ ifconfig_netmask, tun_mtu);=
}
else /* tap */
{

argv_printf(&argv, "%s %s %s netm= ask %s mtu %d up", IFCONFIG_PATH,
- ifname, ifconfig_local, ifconfig_remot= e_netmask,
+ = ifname, ifconfig_local, ifconfig_netmask,
= tun_mtu);
}
@@ -1527,8 +1569,8 @@
struct route_ipv4 r;<= /span>
CLEAR(r);
r.flags =3D RT_= DEFINED;
- r.net= work =3D tt->local & tt->remote_netmask;
- r.netmask =3D tt->remote_netmask;
+ r.network =3D= tt->local & netmask;
+ r.netbits =3D tt->netbits;
r.g= ateway =3D tt->local;
add_route(&r, tt, 0, = NULL, es, NULL);
}
@@ -1540,13 +1582,1= 2 @@
{
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 (al= ways subnet) */
{
- 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 );<= /span>
+ = ifname, ifconfig_local, tt->netbits, tun_mtu );
}

argv_msg(M_INFO, &argv);@@ -1565,7 +1606,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&quo= t;, IFCONFIG_PATH,

- = ifname, ifconfig_local, ifconfig_remote_netmask, tun_mtu);=
+ = ifname, ifconfig_local, ifconfig_netmask, tun_mtu);

argv_msg(M_INFO, &argv);
= openvpn_execve_check(&argv, aix_es, S_FATAL, "AIX ifconfig failed&= quot;);
@@ -1578,7 +1619,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_ne= tmask);
}
else if (tt->options= .ip_win32_type =3D=3D IPW32_SET_DHCP_MASQ || tt->options.ip_win32_type = =3D=3D IPW32_SET_ADAPTIVE)
{
@@ -1660,= 12 +1701,10 @@
undo_ifconfig_ipv4(struct tuntap *tt, openv= pn_net_ctx_t *ctx)
{
#if defined(TARGET_L= INUX)
- int netbits = =3D netmask_to_netbits2(tt->remote_netmask);
-
if (is_tun_p2p(tt))<= br> {
if (net_addr_ptp_v4_del(ctx, tt-&g= t;actual_name, &tt->local,
- &tt->remote_netmask) <= ; 0)
+ = &tt->remote) < 0)
{
msg(M_WARN, "Linux can't del IP from ifa= ce %s",
tt->actual_name);@@ -1673,7 +1712,7 @@
}
e= lse
{
- 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-&= gt;netbits) < 0)
{
= msg(M_WARN, "Linux can't del IP from iface %s",
tt->actual_name);

@@ -4683,8 +4722,= 8 @@

if (ip_str && n= etmask_str && strlen(ip_str) && strlen(netmask_str))=
{
- = *netmask =3D getaddr(getaddr_flags, netmask_str, 0, &succeed2, NULL);=
+ *ip= =3D getaddr(getaddr_flags, ip_str, NULL, 0, &succeed1, NULL);+ *netmask =3D= getaddr(getaddr_flags, netmask_str, NULL, 0, &succeed2, NULL);<= br> ret =3D (succeed1 =3D=3D true && succeed2= =3D=3D true);
}
}
@@ -5370,7 +5409,7 @@
break;
}

- ip =3D getaddr(getaddr_flags, ip_str, 0, &s= ucceed, NULL);
+ = ip =3D getaddr(getaddr_flags, ip_str, NULL, 0, &succeed, NULL);
if (!succeed)
{
break;

@@ -6428,11 +6467,12 @@
if (!tt->did_ifconfig_setup || tt->topology =3D=3D TOP_SUBNE= T)

{
in_addr_t ep[3];<= br>+ const in_addr_t net= mask =3D netbits_to_netmask(tt->netbits);
BOOL = status;

ep[0] =3D htonl(tt->l= ocal);
- ep[1] = =3D htonl(tt->local & tt->remote_netmask);
- ep[2] =3D htonl(tt->remote_netma= sk);
+ 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 +6496,7 @@
{
<= span> 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_C= ONFIG_POINT_TO_POINT,
ep, siz= eof(ep),
@@ -6485,11 +6525,11 @@
{
if (tt->topology =3D=3D TOP_SUBNET)
{

- = ep[2] =3D dhcp_masq_addr(tt->local, tt->remote_netmask, tt->opt= ions.dhcp_masq_custom_offset ? tt->options.dhcp_masq_offset : 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 h= tonl(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_add= r_t local;
- in_addr= _t remote_netmask;
+ = in_addr_t remote;
= + int netbits;

struct in6_addr lo= cal_ipv6;
struct in6_addr remote_ipv6;

To view, visit change 739. To unsubscribe, or for help writ= ing mail filters, visit sett= ings.

<= link itemprop=3D"url" href=3D"http://gerrit.openvpn.net/c/openvpn/+/739?usp= =3Demail"/>
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Iae04ad8715e40dfc76475c2c5b9= a766c9604efc9
Gerrit-Change-Number: 739
Gerrit-PatchSet: 1
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: newchange
--jpuP3aF4tkQ=--