sitnl follow-up
cover sitnl follow-up
From: Antonio Quartulli <[email protected]...>
Hi all,
this small patchset aims at tackling most of the concerns raised by Gert while merging the sitnl introduction patchset.
All patches are fairly small, but they are a bit intertwined (they change nearby lines), therefore it would be better to merge them in the provided order to avoid ugly merge conflicts.
Cheers,
Antonio Quartulli (6):
sitnl: harden strncpy() by forcing arguments to have the same length networking/best_gw: remove useless prefixlen parameter
networking: extend API for better memory management tun.c: undo_ifconfig_ipv4/6 remove useless gc argument networking_sitnl.c: uncrustify file
route.c: simplify ifdef logic
src/openvpn/networking.h | 33 +++++++++-- src/openvpn/networking_iproute2.c | 78 ++++++++++--------------- src/openvpn/networking_iproute2.h | 1 + src/openvpn/networking_sitnl.c | 96 +++++++++++++++++++++---------- src/openvpn/openvpn.c | 1 + src/openvpn/route.c | 32 ++++++----- src/openvpn/route.h | 6 +- src/openvpn/tun.c | 28 ++++----- 8 files changed, 165 insertions(+), 110 deletions(-)
Signature · 1 line
2.22.0
PATCH 1/6 sitnl: harden strncpy() by forcing arguments to have the same length
From: Antonio Quartulli <[email protected]...>
At the moment a strcpy() (without length check!) is performed between a string long IFNAMSIZ bytes and one of 16 bytes. This is ok right now because IFNAMSIZ is defined as 16, however this bit is not under our control and may change in he future without us being warned.
For this reason, force both strings to use IFNAMSIZ as size and, since this constant may not exist on every platform, ensure it is always defined.
--- src/openvpn/networking_sitnl.c | 2 +- src/openvpn/route.h | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-)
src/openvpn/networking_sitnl.c +1 -1
@@ -494,7 +494,7 @@ sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst,/* save result in output variables */ memcpy (best_gw, &res.gw, res.addr_size); -strcpy (best_iface, res.iface); +strncpy (best_iface, res.iface,IFNAMSIZ ); err:return ret;
src/openvpn/route.h +5 -1
@@ -184,7 +184,11 @@ struct route_ipv6_gateway_info {#ifdef _WIN32DWORD adapter_index;/* interface or ~0 if undefined */ #else -char iface[16 ];/* interface name (null terminated), may be empty */ +/* non linux platform don't have this constant defined */ +#ifndef IFNAMSIZ +#define IFNAMSIZ 16 +#endif +char iface[IFNAMSIZ ];/* interface name (null terminated), may be empty */ #endif /* gateway interface hardware address */
Signature · 1 line
2.22.0
PATCH 2/6 networking/best_gw: remove useless prefixlen parameter
In reply to Antonio QuartulliFrom: Antonio Quartulli <[email protected]...>
The prefixlen parameter is not used at all while retrieving a route therefore it can safely be removed.
--- src/openvpn/networking.h | 7 ++----- src/openvpn/networking_iproute2.c | 4 ++-- src/openvpn/networking_sitnl.c | 20 +++++++++----------- src/openvpn/route.c | 4 ++-- 4 files changed, 15 insertions(+), 20 deletions(-)
src/openvpn/networking.h +2 -5
@@ -241,15 +241,13 @@ int net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,* * @param ctx the implementation specific context * @param dst The destination to lookup -* @param prefixlen The length of the prefix of the destination * @param best_gw Location where the retrieved GW has to be stored * @param best_iface Location where the retrieved interface has to be stored * * @return 0 on success, a negative error code otherwise */ int net_route_v4_best_gw (openvpn_net_ctx_t *ctx,const in_addr_t *dst, -int prefixlen,in_addr_t *best_gw, -openvpn_net_iface_t *best_iface); +in_addr_t *best_gw,openvpn_net_iface_t *best_iface);/** * Retrieve the gateway and outgoing interface for the specified IPv6
@@ -257,14 +255,13 @@ int net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst,* * @param ctx the implementation specific context * @param dst The destination to lookup -* @param prefixlen The length of the prefix of the destination * @param best_gw Location where the retrieved GW has to be stored * @param best_iface Location where the retrieved interface has to be stored * * @return 0 on success, a negative error code otherwise */ int net_route_v6_best_gw (openvpn_net_ctx_t *ctx,const struct in6_addr *dst, -int prefixlen,struct in6_addr *best_gw, +struct in6_addr *best_gw,openvpn_net_iface_t *best_iface);#endif /* ENABLE_SITNL || ENABLE_IPROUTE */
src/openvpn/networking_iproute2.c +2 -2
@@ -325,7 +325,7 @@ net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,int net_route_v4_best_gw (openvpn_net_ctx_t *ctx,const in_addr_t *dst, -int prefixlen,in_addr_t *best_gw,char *best_iface) +in_addr_t *best_gw,char *best_iface) { best_iface[0 ] ='\0' ;
@@ -378,7 +378,7 @@ net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst,* uses the sitnl implementation from networking_sitnl.c. * * int -* net_route_v6_best_gw(const struct in6_addr *dst, int prefixlen, +* net_route_v6_best_gw(const struct in6_addr *dst, * struct in6_addr *best_gw, char *best_iface) */
src/openvpn/networking_sitnl.c +9 -11
@@ -451,7 +451,7 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)static int sitnl_route_best_gw (sa_family_t af_family,const inet_address_t *dst, -int prefixlen,void *best_gw,char *best_iface) +void *best_gw,char *best_iface) {struct sitnl_route_req req;route_res_t res;
@@ -468,7 +468,6 @@ sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst, req.n.nlmsg_flags =NLM_F_REQUEST ; req.r.rtm_family = af_family; - req.r.rtm_dst_len = prefixlen;switch (af_family) {
@@ -503,7 +502,7 @@ err:/* used by iproute2 implementation too */ int net_route_v6_best_gw (openvpn_net_ctx_t *ctx,const struct in6_addr *dst, -int prefixlen,struct in6_addr *best_gw,char *best_iface) +struct in6_addr *best_gw,char *best_iface) {inet_address_t dst_v6 = {0 };char buf[INET6_ADDRSTRLEN ];
@@ -514,11 +513,10 @@ net_route_v6_best_gw(openvpn_net_ctx_t *ctx, const struct in6_addr *dst, dst_v6.ipv6 = *dst; } -msg (D_ROUTE ,"%s query: dst %s/%d" , __func__, -inet_ntop (AF_INET6 , &dst_v6.ipv6, buf,sizeof (buf)), prefixlen); +msg (D_ROUTE ,"%s query: dst %s" , __func__, +inet_ntop (AF_INET6 , &dst_v6.ipv6, buf,sizeof (buf))); - ret =sitnl_route_best_gw (AF_INET6 , &dst_v6, prefixlen, best_gw, - best_iface); + ret =sitnl_route_best_gw (AF_INET6 , &dst_v6, best_gw, best_iface);if (ret <0 ) {return ret;
@@ -544,7 +542,7 @@ net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx)int net_route_v4_best_gw (openvpn_net_ctx_t *ctx,const in_addr_t *dst, -int prefixlen,in_addr_t *best_gw,char *best_iface) +in_addr_t *best_gw,char *best_iface) {inet_address_t dst_v4 = {0 };char buf[INET_ADDRSTRLEN ];
@@ -555,10 +553,10 @@ net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst, dst_v4.ipv4 =htonl (*dst); } -msg (D_ROUTE ,"%s query: dst %s/%d" , __func__, -inet_ntop (AF_INET , &dst_v4.ipv4, buf,sizeof (buf)), prefixlen); +msg (D_ROUTE ,"%s query: dst %s" , __func__, +inet_ntop (AF_INET , &dst_v4.ipv4, buf,sizeof (buf))); - ret =sitnl_route_best_gw (AF_INET , &dst_v4, prefixlen, best_gw, best_iface); + ret =sitnl_route_best_gw (AF_INET , &dst_v4, best_gw, best_iface);if (ret <0 ) {return ret;
src/openvpn/route.c +2 -2
@@ -3172,7 +3172,7 @@ get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)#ifndef TARGET_ANDROID /* get default gateway IP addr */ -if (net_route_v4_best_gw (ctx,NULL ,0 , &rgi->gateway.addr, best_name) ==0 ) +if (net_route_v4_best_gw (ctx,NULL , &rgi->gateway.addr, best_name) ==0 ) { rgi->flags |=RGI_ADDR_DEFINED ;if (!rgi->gateway.addr && best_name[0 ])
@@ -3330,7 +3330,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,CLEAR (*rgi6); -if (net_route_v6_best_gw (ctx, dest,0 , &rgi6->gateway.addr_ipv6, +if (net_route_v6_best_gw (ctx, dest, &rgi6->gateway.addr_ipv6, rgi6->iface) ==0 ) {if (!IN6_IS_ADDR_UNSPECIFIED (rgi6->gateway.addr_ipv6.s6_addr))
Signature · 1 line
2.22.0
PATCH 3/6 networking: extend API for better memory management
In reply to Antonio QuartulliFrom: Antonio Quartulli <[email protected]...>
Networking backend implementations may need to allocate dynamic resources that require an explicit free/release.
Since these cleanup are perfomed not very often, and only at specific times, it makes sense to have the upper layer signal when it's the right time to do so, by means of a new API call.
For this purpose two news APIs have been implemented:
- net_ctx_free() to release all backend specific resources. Expected to be called at application cleanup time;
- net_ctx_reset() to let backends release temporary resources (i.e. reset garbage collectors). To be invoked after routines that are expected to allocate memory (i.e. tun setup or shutdown).
In this patch related implementations for iproute2 and sitnl are also provided.
--- src/openvpn/networking.h | 26 +++++++++++ src/openvpn/networking_iproute2.c | 74 ++++++++++++------------------- src/openvpn/networking_iproute2.h | 1 + src/openvpn/networking_sitnl.c | 12 +++++ src/openvpn/openvpn.c | 1 + src/openvpn/route.c | 8 ++++ src/openvpn/tun.c | 5 +++ 7 files changed, 82 insertions(+), 45 deletions(-)
src/openvpn/networking.h +26 -0
@@ -39,6 +39,18 @@ net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx) {return 0 ; } + +static inline void +net_ctx_reset (openvpn_net_ctx_t *ctx) +{ + (void )ctx; +} + +static inline void +net_ctx_free (openvpn_net_ctx_t *ctx) +{ + (void )ctx; +}#endif #if defined(ENABLE_SITNL ) || defined(ENABLE_IPROUTE )
@@ -53,6 +65,20 @@ net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx)*/ int net_ctx_init (struct context *c,openvpn_net_ctx_t *ctx); +/** +* Release resources allocated by the internal garbage collector +* +* @param ctx the implementation specific context +*/ +void net_ctx_reset (openvpn_net_ctx_t *ctx); + +/** +* Release all resources allocated within the platform specific context object +* +* @param ctx the implementation specific context to release +*/ +void net_ctx_free (openvpn_net_ctx_t *ctx); +/** * Bring interface up or down. *
src/openvpn/networking_iproute2.c +29 -45
227 lines not on this page · raw message
src/openvpn/networking_iproute2.h +1 -0
@@ -29,6 +29,7 @@ typedef char openvpn_net_iface_t;struct openvpn_net_ctx {struct env_set *es; +struct gc_arena gc; };typedef struct openvpn_net_ctx openvpn_net_ctx_t ;
src/openvpn/networking_sitnl.c +12 -0
@@ -540,6 +540,18 @@ net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx)return 0 ; } +void +net_ctx_reset (openvpn_net_ctx_t *ctx) +{ + (void )ctx; +} + +void +net_ctx_free (openvpn_net_ctx_t *ctx) +{ + (void )ctx; +} +int net_route_v4_best_gw (openvpn_net_ctx_t *ctx,const in_addr_t *dst,in_addr_t *best_gw,char *best_iface)
src/openvpn/openvpn.c +1 -0
@@ -332,6 +332,7 @@ openvpn_main(int argc, char *argv[])env_set_destroy (c.es);uninit_options (&c.options);gc_reset (&c.gc); +net_ctx_free (&c.net_ctx); }while (c.sig->signal_received ==SIGHUP ); }
src/openvpn/route.c +8 -0
@@ -1826,6 +1826,8 @@ done: }argv_reset (&argv);gc_free (&gc); +/* release resources potentially allocated during route setup */ +net_ctx_reset (ctx); }
@@ -2130,6 +2132,8 @@ done: }argv_reset (&argv);gc_free (&gc); +/* release resources potentially allocated during route setup */ +net_ctx_reset (ctx); } static void
@@ -2322,6 +2326,8 @@ done: r->flags &= ~RT_ADDED ;argv_reset (&argv);gc_free (&gc); +/* release resources potentially allocated during route cleanup */ +net_ctx_reset (ctx); }void
@@ -2548,6 +2554,8 @@ delete_route_ipv6(const struct route_ipv6 *r6, const struct tuntap *tt,argv_reset (&argv);gc_free (&gc); +/* release resources potentially allocated during route cleanup */ +net_ctx_reset (ctx); }/*
src/openvpn/tun.c +5 -0
@@ -1435,6 +1435,9 @@ do_ifconfig(struct tuntap *tt, const char *ifname, int tun_mtu, {do_ifconfig_ipv6 (tt, ifname, tun_mtu, es, ctx); } + +/* release resources potentially allocated during interface setup */ +net_ctx_free (ctx); } static void
@@ -2027,6 +2030,8 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) }gc_free (&gc); +/* release resources potentially allocated during undo */ +net_ctx_reset (ctx); }close_tun_generic (tt);
Signature · 1 line
2.22.0
PATCH 4/6 tun.c: undo_ifconfig_ipv4/6 remove useless gc argument
In reply to Antonio QuartulliFrom: Antonio Quartulli <[email protected]...>
With the new networking APIs, each implementation handles garbage collection internally and therefore does not require a gc object to be provided by the outer layer.
However, there are a few cases where a garbage collector is still required. In close_tun() move the declaration and cleanup of gc to the area where it is used and simplify the surrounding code a bit.
While at it, fix a typo in a nearby ifdef comment.
--- src/openvpn/tun.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
src/openvpn/tun.c +10 -13
@@ -1950,9 +1950,8 @@ tuncfg(const char *dev, const char *dev_type, const char *dev_node,#endif /* ENABLE_FEATURE_TUN_PERSIST */ -void -undo_ifconfig_ipv4 (struct tuntap *tt,struct gc_arena *gc, -openvpn_net_ctx_t *ctx) +static void +undo_ifconfig_ipv4 (struct tuntap *tt,openvpn_net_ctx_t *ctx) {#if defined(TARGET_LINUX )int netbits =netmask_to_netbits2 (tt->remote_netmask);
@@ -1974,7 +1973,7 @@ undo_ifconfig_ipv4(struct tuntap *tt, struct gc_arena *gc, tt->actual_name); } } -#else /* ifdef TARGET_LINUX */ +#else /* ifndef TARGET_LINUX */ struct argv argv =argv_new ();argv_printf (&argv,"%s %s 0.0.0.0" ,IFCONFIG_PATH , tt->actual_name);
@@ -1986,9 +1985,8 @@ undo_ifconfig_ipv4(struct tuntap *tt, struct gc_arena *gc,#endif /* ifdef TARGET_LINUX */ } -void -undo_ifconfig_ipv6 (struct tuntap *tt,struct gc_arena *gc, -openvpn_net_ctx_t *ctx) +static void +undo_ifconfig_ipv6 (struct tuntap *tt,openvpn_net_ctx_t *ctx) {#if defined(TARGET_LINUX ) if (net_addr_v6_del(ctx, tt->actual_name, &tt->local_ipv6,
@@ -1996,7 +1994,8 @@ undo_ifconfig_ipv6(struct tuntap *tt, struct gc_arena *gc, {msg (M_WARN ,"Linux can't del IPv6 from iface %s" , tt->actual_name); } -#else /* ifdef TARGET_LINUX */ +#else /* ifndef TARGET_LINUX */ +struct gc_arena gc =gc_new ();const char *ifconfig_ipv6_local =print_in6_addr (tt->local_ipv6,0 , gc);struct argv argv =argv_new ();
@@ -2007,6 +2006,7 @@ undo_ifconfig_ipv6(struct tuntap *tt, struct gc_arena *gc,openvpn_execve_check (&argv,NULL ,0 ,"Linux ip -6 addr del failed" );argv_reset (&argv); +gc_free (&gc);#endif /* ifdef TARGET_LINUX */ }
@@ -2017,19 +2017,16 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)if (tt->type !=DEV_TYPE_NULL ) { -struct gc_arena gc =gc_new (); -if (tt->did_ifconfig_setup) { -undo_ifconfig_ipv4 (tt, &gc, ctx); +undo_ifconfig_ipv4 (tt, ctx); }if (tt->did_ifconfig_ipv6_setup) { -undo_ifconfig_ipv6 (tt, &gc, ctx); +undo_ifconfig_ipv6 (tt, ctx); } -gc_free (&gc);/* release resources potentially allocated during undo */ net_ctx_reset (ctx); }
Signature · 1 line
2.22.0
PATCH 5/6 networking_sitnl.c: uncrustify file
In reply to Antonio QuartulliGive this file a run under uncrustify to fix a few style glitches here and there.
--- src/openvpn/networking_sitnl.c | 62 +++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 19 deletions(-)
src/openvpn/networking_sitnl.c +43 -19
@@ -45,7 +45,7 @@#define SITNL_ADDATTR (_msg, _max_size, _attr, _data, _size) \ { \ - if (sitnl_addattr(_msg, _max_size, _attr, _data, _size) < 0)\ + if (sitnl_addattr(_msg, _max_size, _attr, _data, _size) < 0) \ { \ goto err; \ } \
@@ -335,16 +335,16 @@ sitnl_send(struct nlmsghdr *payload, pid_t peer, unsigned int groups, }/* if (((int)nladdr.nl_pid != peer) || (h->nlmsg_pid != nladdr.nl_pid) -|| (h->nlmsg_seq != seq)) -{ -rcv_len -= NLMSG_ALIGN(len); -h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); -msg(M_DEBUG, "%s: skipping unrelated message. nl_pid:%d (peer:%d) nl_msg_pid:%d nl_seq:%d seq:%d", -__func__, (int)nladdr.nl_pid, peer, h->nlmsg_pid, -h->nlmsg_seq, seq); -continue; -} -*/ +* || (h->nlmsg_seq != seq)) +* { +* rcv_len -= NLMSG_ALIGN(len); +* h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); +* msg(M_DEBUG, "%s: skipping unrelated message. nl_pid:%d (peer:%d) nl_msg_pid:%d nl_seq:%d seq:%d", +* __func__, (int)nladdr.nl_pid, peer, h->nlmsg_pid, +* h->nlmsg_seq, seq); +* continue; +* } +*/ if (h->nlmsg_type ==NLMSG_ERROR ) { err = (struct nlmsgerr *)NLMSG_DATA (h);
@@ -359,7 +359,9 @@ sitnl_send(struct nlmsghdr *payload, pid_t peer, unsigned int groups, { ret =0 ;if (cb) + { ret =cb (h, arg_cb); + } } else {
@@ -427,9 +429,11 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)case RTA_OIF : ifindex = *(unsigned int *)RTA_DATA (rta);break ; +/* route prefix */ case RTA_DST :break ; +/* GW for the route */ case RTA_GATEWAY :memcpy (&res->gw,RTA_DATA (rta), res->addr_size);
@@ -475,9 +479,11 @@ sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst, res.addr_size =sizeof (in_addr_t); req.n.nlmsg_flags |=NLM_F_DUMP ;break ; +case AF_INET6 : res.addr_size =sizeof (struct in6_addr );break ; +default :/* unsupported */ return -EINVAL ;
@@ -598,7 +604,8 @@ net_iface_up(openvpn_net_ctx_t *ctx, const char *iface, bool up) } ifindex =if_nametoindex (iface); -if (ifindex ==0 ) { +if (ifindex ==0 ) + {msg (M_WARN ,"%s: rtnl: cannot get ifindex for %s: %s" , __func__, iface,strerror (errno));return -ENOENT ;
@@ -612,9 +619,13 @@ net_iface_up(openvpn_net_ctx_t *ctx, const char *iface, bool up) req.i.ifi_index = ifindex; req.i.ifi_change |=IFF_UP ;if (up) + { req.i.ifi_flags |=IFF_UP ; + }else + { req.i.ifi_flags &= ~IFF_UP ; + }msg (M_INFO ,"%s: set %s %s" , __func__, iface, up ?"up" :"down" );
@@ -631,7 +642,8 @@ net_iface_mtu_set(openvpn_net_ctx_t *ctx, const char *iface,CLEAR (req); ifindex =if_nametoindex (iface); -if (ifindex ==0 ) { +if (ifindex ==0 ) + {msg (M_WARN |M_ERRNO ,"%s: rtnl: cannot get ifindex for %s" , __func__, iface);return -1 ;
@@ -676,9 +688,11 @@ sitnl_addr_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family,case AF_INET : size =sizeof (struct in_addr );break ; +case AF_INET6 : size =sizeof (struct in6_addr );break ; +default :msg (M_WARN ,"%s: rtnl: unknown address family %d" , __func__, af_family);
@@ -723,10 +737,12 @@ sitnl_addr_ptp_add(sa_family_t af_family, const char *iface, {int ifindex; -switch (af_family) { +switch (af_family) + {case AF_INET :case AF_INET6 :break ; +default :return -EINVAL ; }
@@ -755,10 +771,12 @@ sitnl_addr_ptp_del(sa_family_t af_family, const char *iface, {int ifindex; -switch (af_family) { +switch (af_family) + {case AF_INET :case AF_INET6 :break ; +default :return -EINVAL ; }
@@ -796,9 +814,11 @@ sitnl_route_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family,case AF_INET : size =sizeof (in_addr_t);break ; +case AF_INET6 : size =sizeof (struct in6_addr );break ; +default :return -EINVAL ; }
@@ -859,12 +879,14 @@ sitnl_addr_add(sa_family_t af_family, const char *iface, {int ifindex; -switch (af_family) { +switch (af_family) + {case AF_INET :case AF_INET6 :break ; +default : -return -EINVAL ;; +return -EINVAL ; }if (!iface)
@@ -891,12 +913,14 @@ sitnl_addr_del(sa_family_t af_family, const char *iface, inet_address_t *addr, {int ifindex; -switch (af_family) { +switch (af_family) + {case AF_INET :case AF_INET6 :break ; +default : -return -EINVAL ;; +return -EINVAL ; }if (!iface)
Signature · 1 line
2.22.0
PATCH 6/6 route.c: simplify ifdef logic
In reply to Antonio QuartulliWith the introduction of the new networking API layer on linux, some ifdefs can be pruned or simplified.
While at it move some variable to improve readability.
--- src/openvpn/route.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-)
src/openvpn/route.c +9 -11
@@ -1554,13 +1554,10 @@ add_route(struct route_ipv4 *r,struct argv argv =argv_new ();#if !defined(TARGET_LINUX )const char *network; -#if !defined(ENABLE_IPROUTE ) && !defined(TARGET_AIX ) +#if !defined(TARGET_AIX )const char *netmask;#endif const char *gateway; -#else -const char *iface; -int metric;#endif bool status = false;int is_local_route;
@@ -1574,7 +1571,7 @@ add_route(struct route_ipv4 *r,#if !defined(TARGET_LINUX ) network =print_in_addr_t (r->network,0 , &gc); -#if !defined(ENABLE_IPROUTE ) && !defined(TARGET_AIX ) +#if !defined(TARGET_AIX ) netmask =print_in_addr_t (r->netmask,0 , &gc);#endif gateway =print_in_addr_t (r->gateway,0 , &gc);
@@ -1587,13 +1584,14 @@ add_route(struct route_ipv4 *r, }#if defined(TARGET_LINUX ) - iface =NULL ; +const char *iface =NULL ; +int metric =-1 ; +if (is_on_link (is_local_route, flags, rgi)) { iface = rgi->iface; } - metric =-1 ;if (r->flags &RT_METRIC_DEFINED ) { metric = r->metric;
@@ -2148,10 +2146,10 @@ delete_route(struct route_ipv4 *r,struct argv argv =argv_new ();#if !defined(TARGET_LINUX )const char *network; -#if !defined(ENABLE_IPROUTE ) && !defined(TARGET_AIX ) +#if !defined(TARGET_AIX )const char *netmask;#endif -#if !defined(TARGET_LINUX ) && !defined(TARGET_ANDROID ) +#if !defined(TARGET_ANDROID )const char *gateway;#endif #else
@@ -2168,10 +2166,10 @@ delete_route(struct route_ipv4 *r,#if !defined(TARGET_LINUX ) network =print_in_addr_t (r->network,0 , &gc); -#if !defined(ENABLE_IPROUTE ) && !defined(TARGET_AIX ) +#if !defined(TARGET_AIX ) netmask =print_in_addr_t (r->netmask,0 , &gc);#endif -#if !defined(TARGET_LINUX ) && !defined(TARGET_ANDROID ) +#if !defined(TARGET_ANDROID ) gateway =print_in_addr_t (r->gateway,0 , &gc);#endif #endif
Signature · 1 line
2.22.0
Re: [Openvpn-devel] [PATCH 1/6] sitnl: harden strncpy() by forcing arguments to have the same length
In reply to Antonio QuartulliQuoted 5 lines
For this reason, force both strings to use IFNAMSIZ as size and, since this constant may not exist on every platform, ensure it is always defined.
A problem with this patch misght be that strncpy() does NOT NUL terminates the copied string. (It writes EXACTLY IFNAMSIZ bytes but only when the string is shorter, it will writes NUL bytes in the reminder of the char array[].)
You could use strlcpy() but I'm not sure if that is available in Windows.
Quoted 3 lines
/* save result in output variables */
memcpy(best_gw, &res.gw, res.addr_size);- strcpy(best_iface, res.iface);
Quoted 1 line
+
strncpy(best_iface, res.iface, IFNAMSIZ);
Casper
Re: [Openvpn-devel] [PATCH 1/6] sitnl: harden strncpy() by forcing arguments to have the same length
Hi and thanks for your review,
Quoted 9 lines · On 05/08/2019 11:33, [email protected]... wrote:
Quoted 5 lines
For this reason, force both strings to use IFNAMSIZ as size and, since this constant may not exist on every platform, ensure it is always defined.
A problem with this patch misght be that
strncpy()does NOT NUL terminates the copied string. (It writes EXACTLY IFNAMSIZ bytes but only when the string is shorter, it will writes NUL bytes in the reminder of the char array[].)
the assumption behind this patch is that now both source and destination buffer have exactly the same size (IFNAMSIZ) and the source is NULL terminated. Therefore the problem you mentioned should not appear here, I think.
Quoted 2 lines
You could use
strlcpy()but I'm not sure if that is available in Windows.
This code is currently used only on Linux - it is not expected to ever run on Windows.
Cheers,
Quoted 10 lines
Quoted 5 lines
/* save result in output variables */ memcpy(best_gw, &res.gw, res.addr_size);-
strcpy(best_iface, res.iface);Quoted 1 line
+
strncpy(best_iface, res.iface, IFNAMSIZ);Casper
Signature · 5 lines
Antonio Quartulli OpenVPN Inc. -- Antonio Quartulli
FYI,
this patchset has undergone buildbot, travis-ci and gitlab-ci testing and nothing out of the ordinary came out.
Regards,
Quoted 33 lines · Antonio Quartulli
From: Antonio Quartulli <[email protected]...>
Hi all,
this small patchset aims at tackling most of the concerns raised by Gert while merging the sitnl introduction patchset.
All patches are fairly small, but they are a bit intertwined (they change nearby lines), therefore it would be better to merge them in the provided order to avoid ugly merge conflicts.
Cheers,
Antonio Quartulli (6):
sitnl: hardenstrncpy()by forcing arguments to have the same length networking/best_gw: remove useless prefixlen parameter
networking: extend API for better memory managementtun.c: undo_ifconfig_ipv4/6 remove useless gc argumentnetworking_sitnl.c: uncrustify file
route.c: simplify ifdef logicsrc/openvpn/networking.h | 33 +++++++++-- src/openvpn/networking_iproute2.c | 78 ++++++++++--------------- src/openvpn/networking_iproute2.h | 1 + src/openvpn/networking_sitnl.c | 96 +++++++++++++++++++++---------- src/openvpn/openvpn.c | 1 + src/openvpn/route.c | 32 ++++++----- src/openvpn/route.h | 6 +- src/openvpn/tun.c | 28 ++++----- 8 files changed, 165 insertions(+), 110 deletions(-)
Signature · 1 line
Antonio Quartulli
[Openvpn-devel] [PATCH applied] Re: networking/best_gw: remove useless prefixlen parameter
In reply to Antonio QuartulliNice, trivial, and to the point :-) - passing "make check" for iproute2 and sitnl builds.
Your patch has been applied to the master branch.
commit 4490b5a1eae96904f8973d44ff0e12d819cb9cbc
Author: Antonio Quartulli Date: Mon Aug 5 11:25:25 2019 +0200
networking/best_gw: remove useless prefixlen parameter
Signed-off-by: Antonio Quartulli <antonio2openvpn.net>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg18725.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: sitnl: harden strncpy() by forcing arguments to have the same length
In reply to Antonio QuartulliInteresting remark from Casper Dik - strncpy() actually is defined to *always* write all IFNAMSIZ bytes (so says the linux man page), unlike strcpy() which stops after the '\0' byte. But in this particular case, "not overrunning" is more important than "save a few cycles on a short interface name". But it was certainly news to me.
Now all strings involved are guaranteed to always have the same length.
Thanks.
Your patch has been applied to the master branch.
commit 8a05f860af386cd135ee1aacb0d5eccc49c466bb
Author: Antonio Quartulli Date: Mon Aug 5 11:25:24 2019 +0200
sitnl: harden strncpy() by forcing arguments to have the same length
Signed-off-by: Antonio Quartulli <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg18722.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Re: [Openvpn-devel] [PATCH 3/6] networking: extend API for better memory management
In reply to Antonio QuartulliHi,
Quoted 7 lines · Antonio Quartulli
From: Antonio Quartulli <[email protected]...>
Networking backend implementations may need to allocate dynamic resources that require an explicit free/release.
Since these cleanup are perfomed not very often, and only at specific times, it makes sense to have the upper layer signal when it's the right time to do so, by means of a new API call.
While I generally like this patch ("0.9 ACKs"), there is one thing that rubs me totally the wrong way and that must go:
src/openvpn/networking_iproute2.c net_addr_v4_add(openvpn_net_ctx_t *ctx, const char *iface, · line 99
Quoted 2 lines
src/openvpn/networking_iproute2.c +2
-0+char *addr_str = (char *)print_in_addr_t (*addr,0 , &ctx->gc); +char *brd_str = (char *)print_in_addr_t (*broadcast,0 , &ctx->gc);
These casts - print_in_addr_t() returns a "const char *", it gets assigned to a temporary variable of type "char *", and handed over to a printf() variant. Such code should never ever have a cast.
If the compiler warns about loss of const (in which case I would ask the question why the return value of print_in_addr_t() is declared const at all, if it's a gc_malloc()ed string which *could* be written to just fine...), then let's make "addr_str" and "brd_str" a const as well.
But no casts in for "basic types assigned to single-use variables", ever.
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...PATCH v2 1/1 networking: extend API for better memory management
From: Antonio Quartulli <[email protected]...>
Networking backend implementations may need to allocate dynamic resources that require an explicit free/release.
Since these cleanup are perfomed not very often, and only at specific times, it makes sense to have the upper layer signal when it's the right time to do so, by means of a new API call.
For this purpose two news APIs have been implemented:
- net_ctx_free() to release all backend specific resources. Expected to be called at application cleanup time;
- net_ctx_reset() to let backends release temporary resources (i.e. reset garbage collectors). To be invoked after routines that are expected to allocate memory (i.e. tun setup or shutdown).
In this patch related implementations for iproute2 and sitnl are also provided.
---
Changes from v1:
- got rid of (now) useless casts in front of print_in_addr_t()
src/openvpn/networking.h | 26 +++++++++++ src/openvpn/networking_iproute2.c | 74 ++++++++++++------------------- src/openvpn/networking_iproute2.h | 1 + src/openvpn/networking_sitnl.c | 12 +++++ src/openvpn/openvpn.c | 1 + src/openvpn/route.c | 8 ++++ src/openvpn/tun.c | 5 +++ 7 files changed, 82 insertions(+), 45 deletions(-)
src/openvpn/networking.h +26 -0
@@ -39,6 +39,18 @@ net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx) {return 0 ; } + +static inline void +net_ctx_reset (openvpn_net_ctx_t *ctx) +{ + (void )ctx; +} + +static inline void +net_ctx_free (openvpn_net_ctx_t *ctx) +{ + (void )ctx; +}#endif #if defined(ENABLE_SITNL ) || defined(ENABLE_IPROUTE )
@@ -53,6 +65,20 @@ net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx)*/ int net_ctx_init (struct context *c,openvpn_net_ctx_t *ctx); +/** +* Release resources allocated by the internal garbage collector +* +* @param ctx the implementation specific context +*/ +void net_ctx_reset (openvpn_net_ctx_t *ctx); + +/** +* Release all resources allocated within the platform specific context object +* +* @param ctx the implementation specific context to release +*/ +void net_ctx_free (openvpn_net_ctx_t *ctx); +/** * Bring interface up or down. *
src/openvpn/networking_iproute2.c +29 -45
227 lines not on this page · raw message
src/openvpn/networking_iproute2.h +1 -0
@@ -29,6 +29,7 @@ typedef char openvpn_net_iface_t;struct openvpn_net_ctx {struct env_set *es; +struct gc_arena gc; };typedef struct openvpn_net_ctx openvpn_net_ctx_t ;
src/openvpn/networking_sitnl.c +12 -0
@@ -540,6 +540,18 @@ net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx)return 0 ; } +void +net_ctx_reset (openvpn_net_ctx_t *ctx) +{ + (void )ctx; +} + +void +net_ctx_free (openvpn_net_ctx_t *ctx) +{ + (void )ctx; +} +int net_route_v4_best_gw (openvpn_net_ctx_t *ctx,const in_addr_t *dst,in_addr_t *best_gw,char *best_iface)
src/openvpn/openvpn.c +1 -0
@@ -332,6 +332,7 @@ openvpn_main(int argc, char *argv[])env_set_destroy (c.es);uninit_options (&c.options);gc_reset (&c.gc); +net_ctx_free (&c.net_ctx); }while (c.sig->signal_received ==SIGHUP ); }
src/openvpn/route.c +8 -0
@@ -1826,6 +1826,8 @@ done: }argv_reset (&argv);gc_free (&gc); +/* release resources potentially allocated during route setup */ +net_ctx_reset (ctx); }
@@ -2130,6 +2132,8 @@ done: }argv_reset (&argv);gc_free (&gc); +/* release resources potentially allocated during route setup */ +net_ctx_reset (ctx); } static void
@@ -2322,6 +2326,8 @@ done: r->flags &= ~RT_ADDED ;argv_reset (&argv);gc_free (&gc); +/* release resources potentially allocated during route cleanup */ +net_ctx_reset (ctx); }void
@@ -2548,6 +2554,8 @@ delete_route_ipv6(const struct route_ipv6 *r6, const struct tuntap *tt,argv_reset (&argv);gc_free (&gc); +/* release resources potentially allocated during route cleanup */ +net_ctx_reset (ctx); }/*
src/openvpn/tun.c +5 -0
@@ -1423,6 +1423,9 @@ do_ifconfig(struct tuntap *tt, const char *ifname, int tun_mtu, {do_ifconfig_ipv6 (tt, ifname, tun_mtu, es, ctx); } + +/* release resources potentially allocated during interface setup */ +net_ctx_free (ctx); } static void
@@ -2015,6 +2018,8 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) }gc_free (&gc); +/* release resources potentially allocated during undo */ +net_ctx_reset (ctx); }close_tun_generic (tt);
Signature · 1 line
2.22.1
[Openvpn-devel] [PATCH applied] Re: networking: extend API for better memory management
Things look much nicer now - less casts, less free() calls :-) - thanks.
Tested on Linux/iproute2 and Linux/sitnl (t_client tests).
Your patch has been applied to the master branch.
commit 59fde742a113a450fe65a4d6b2cb9122425c1920
Author: Antonio Quartulli Date: Fri Aug 16 22:26:54 2019 +0200
networking: extend API for better memory management
Signed-off-by: Antonio Quartulli <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg18780.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: tun.c: undo_ifconfig_ipv4/6 remove useless gc argument
In reply to Antonio QuartulliLooks like a reasonable change, tested on Linux/iproute2 and FreeBSD (to cover the "#ifndef TARGET_LINUX" part).
Your patch has been applied to the master branch.
commit e64d9c47d580427ed5e3d176dbcb7aa2ea55c4ba
Author: Antonio Quartulli Date: Mon Aug 5 11:25:27 2019 +0200
tun.c: undo_ifconfig_ipv4/6 remove useless gc argument
Signed-off-by: Antonio Quartulli <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg18726.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: networking_sitnl.c: uncrustify file
In reply to Antonio QuartulliVerified that this does, indeed, only whitespace / bracket changes, and there are no ambiguous brackets in the parts that are touched.
It messes up your nice formatting of the backspaces for SITNL_ADDATTR(), though :-)
Lightly tested on FreeBSD and Linux/sitnl.
Your patch has been applied to the master branch.
commit 2c45d268ca65c522fbabb7c4dab5e721296b4623
Author: Antonio Quartulli Date: Mon Aug 5 11:25:28 2019 +0200
networking_sitnl.c: uncrustify file
Signed-off-by: Antonio Quartulli <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg18727.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: route.c: simplify ifdef logic
In reply to Antonio QuartulliThese look like reasonable simplifications - if we're not on linux, we'll never have ENABLE_IPROUTE2, so no good testing for it. And so on.
Compile tested on Linux/sitnl, Linux/iproute2, FreeBSD. Buildbot will see to the others (if it breaks AIX, I'll find out next time I play around with that port, and complain to the dimwit who ACKed this :) ).
Your patch has been applied to the master branch.
commit d42f070c507592a80425fa45801bc2d4cb573789
Author: Antonio Quartulli Date: Mon Aug 5 11:25:29 2019 +0200
route.c: simplify ifdef logic
Signed-off-by: Antonio Quartulli <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg18724.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering