From: "its_Giaan (Code Review)" Date: Thu, 12 Dec 2024 09:23:10 +0000 In-Reply-To: References: Reply-To: gianmarco@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296..., a@...2181... Message-ID: <5914b83e2e196aecdd5beefc2b26a3d87fa61733-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="+4ahEaWRcVY="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: Adapt socket handling to support listening on multiple sockets List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , cron2 , flichtenheld Cc: openvpn-devel --+4ahEaWRcVY= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, ordex, plaisthos. its_Giaan has uploaded a new patch set (#9) to the change originally create= d by ordex. ( http://gerrit.openvpn.net/c/openvpn/+/434?usp=3Demail ) Change subject: Adapt socket handling to support listening on multiple sock= ets ...................................................................... Adapt socket handling to support listening on multiple sockets Introduce internal changes preparing the server to handle multiple sockets concurrently for both TCP and UDP protocols. While no user-visible features are implemented yet, these modifications are essential for enabling future functionality such as listening on multiple ports. Key changes are: converting link_socket from a single pointer to an array in various contexts, in order to be able to store multiple sockets at once. Change-Id: Ia0a889e800f0b36aed770ee36e31afeec5df6084 Signed-off-by: Antonio Quartulli Signed-off-by: Gianmarco De Gregori --- M src/openvpn/dco.c M src/openvpn/forward.c M src/openvpn/forward.h M src/openvpn/init.c M src/openvpn/init.h M src/openvpn/mss.c M src/openvpn/mtcp.c M src/openvpn/mudp.c M src/openvpn/mudp.h M src/openvpn/multi.c M src/openvpn/multi.h M src/openvpn/openvpn.c M src/openvpn/openvpn.h M src/openvpn/ping.h M src/openvpn/socket.c M src/openvpn/socket.h 16 files changed, 327 insertions(+), 186 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/34/434/9 diff --git a/src/openvpn/dco.c b/src/openvpn/dco.c index dcc80d3..8f2adb5 100644 --- a/src/openvpn/dco.c +++ b/src/openvpn/dco.c @@ -478,11 +478,11 @@ return 0; } - struct link_socket *ls =3D c->c2.link_socket; + struct link_socket *sock =3D c->c2.link_sockets[0]; - ASSERT(ls->info.connection_established); + ASSERT(sock->info.connection_established); - struct sockaddr *remoteaddr =3D &ls->info.lsa->actual.dest.addr.sa; + struct sockaddr *remoteaddr =3D &sock->info.lsa->actual.dest.addr.sa; struct tls_multi *multi =3D c->c2.tls_multi; #ifdef TARGET_FREEBSD /* In Linux in P2P mode the kernel automatically removes an existing p= eer @@ -494,7 +494,7 @@ } #endif int ret =3D dco_new_peer(&c->c1.tuntap->dco, multi->peer_id, - c->c2.link_socket->sd, NULL, remoteaddr, NULL, = NULL); + c->c2.link_sockets[0]->sd, NULL, remoteaddr, NU= LL, NULL); if (ret < 0) { return ret; @@ -527,12 +527,12 @@ #if ENABLE_IP_PKTINFO struct context *c =3D &mi->context; - if (!proto_is_udp(c->c2.link_socket->info.proto) || !(c->options.sockf= lags & SF_USE_IP_PKTINFO)) + if (!proto_is_udp(c->c2.link_sockets[0]->info.proto) || !(c->options.s= ockflags & SF_USE_IP_PKTINFO)) { return false; } - struct link_socket_actual *actual =3D &c->c2.link_socket_info->lsa->ac= tual; + struct link_socket_actual *actual =3D &c->c2.link_socket_infos[0]->lsa= ->actual; switch (actual->dest.addr.sa.sa_family) { @@ -577,7 +577,7 @@ int peer_id =3D c->c2.tls_multi->peer_id; struct sockaddr *remoteaddr, *localaddr =3D NULL; struct sockaddr_storage local =3D { 0 }; - int sd =3D c->c2.link_socket->sd; + int sd =3D c->c2.link_sockets[0]->sd; if (c->mode =3D=3D CM_CHILD_TCP) @@ -587,8 +587,8 @@ } else { - ASSERT(c->c2.link_socket_info->connection_established); - remoteaddr =3D &c->c2.link_socket_info->lsa->actual.dest.addr.sa; + ASSERT(c->c2.link_socket_infos[0]->connection_established); + remoteaddr =3D &c->c2.link_socket_infos[0]->lsa->actual.dest.addr.= sa; } /* In server mode we need to fetch the remote addresses from the push = config */ diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c index 2c72001..f3f3503 100644 --- a/src/openvpn/forward.c +++ b/src/openvpn/forward.c @@ -58,12 +58,17 @@ wait_status_string(struct context *c, struct gc_arena *gc) { struct buffer out =3D alloc_buf_gc(64, gc); - buf_printf(&out, "I/O WAIT %s|%s|%s|%s %s", + + buf_printf(&out, "I/O WAIT %s|%s| %s", tun_stat(c->c1.tuntap, EVENT_READ, gc), tun_stat(c->c1.tuntap, EVENT_WRITE, gc), - socket_stat(c->c2.link_socket, EVENT_READ, gc), - socket_stat(c->c2.link_socket, EVENT_WRITE, gc), tv_string(&c->c2.timeval, gc)); + for (int i =3D 0; i < c->c1.link_sockets_num; i++) + { + buf_printf(&out, "\n %s|%s", + socket_stat(c->c2.link_sockets[i], EVENT_READ, gc), + socket_stat(c->c2.link_sockets[i], EVENT_WRITE, gc)); + } return BSTR(&out); } @@ -99,7 +104,7 @@ { if (c->c2.tls_multi && c->c2.tls_exit_signal) { - if (link_socket_connection_oriented(c->c2.link_socket)) + if (link_socket_connection_oriented(c->c2.link_sockets[0])) { if (c->c2.tls_multi->n_soft_errors) { @@ -567,6 +572,7 @@ #ifdef ENABLE_FRAGMENT /* * Should we deliver a datagram fragment to remote? + * c is expected to be a single-link context (p2p or child) */ static void check_fragment(struct context *c) @@ -1123,7 +1129,9 @@ decrypt_status =3D openvpn_decrypt(&c->c2.buf, c->c2.buffers->decr= ypt_buf, co, &c->c2.frame, ad_start); - if (!decrypt_status && link_socket_connection_oriented(c->c2.link_= socket)) + if (!decrypt_status + /* on the instance context we have only one socket, so just ch= eck the first one */ + && link_socket_connection_oriented(c->c2.link_sockets[0])) { /* decryption errors are fatal in TCP mode */ register_signal(c->sig, SIGUSR1, "decryption-error"); /* SOFT-= SIGUSR1 -- decryption error in TCP mode */ @@ -1451,7 +1459,7 @@ */ void -process_incoming_tun(struct context *c) +process_incoming_tun(struct context *c, struct link_socket *out_sock) { struct gc_arena gc =3D gc_new(); @@ -1484,7 +1492,7 @@ */ unsigned int flags =3D PIPV4_PASSTOS | PIP_MSSFIX | PIPV4_CLIENT_N= AT | PIPV6_ICMP_NOHOST_CLIENT; - process_ip_header(c, flags, &c->c2.buf); + process_ip_header(c, flags, &c->c2.buf, out_sock); #ifdef PACKET_TRUNCATION_CHECK /* if (c->c2.buf.len > 1) --c->c2.buf.len; */ @@ -1645,7 +1653,8 @@ } void -process_ip_header(struct context *c, unsigned int flags, struct buffer *bu= f) +process_ip_header(struct context *c, unsigned int flags, struct buffer *bu= f, + struct link_socket *sock) { if (!c->options.ce.mssfix) { @@ -1679,7 +1688,7 @@ /* extract TOS from IP header */ if (flags & PIPV4_PASSTOS) { - link_socket_extract_tos(c->c2.link_socket, &ipbuf); + link_socket_extract_tos(sock, &ipbuf); } #endif @@ -1887,7 +1896,7 @@ */ void -process_outgoing_tun(struct context *c) +process_outgoing_tun(struct context *c, struct link_socket *in_sock) { /* * Set up for write() call to TUN/TAP @@ -1904,9 +1913,8 @@ * The --mssfix option requires * us to examine the IP header (IPv4 or IPv6). */ - process_ip_header(c, - PIP_MSSFIX | PIPV4_EXTRACT_DHCP_ROUTER | PIPV4_CLIEN= T_NAT | PIP_OUTGOING, - &c->c2.to_tun); + process_ip_header(c, PIP_MSSFIX|PIPV4_EXTRACT_DHCP_ROUTER|PIPV4_CLIENT= _NAT|PIP_OUTGOING, &c->c2.to_tun, + in_sock); if (c->c2.to_tun.len <=3D c->c2.frame.buf.payload_size) { @@ -2180,8 +2188,11 @@ /* * Configure event wait based on socket, tuntap flags. */ - socket_set(c->c2.link_socket, c->c2.event_set, socket, - &c->c2.link_socket->ev_arg, NULL); + for (int i =3D 0; i < c->c1.link_sockets_num; i++) + { + socket_set(c->c2.link_sockets[i], c->c2.event_set, socket, + &c->c2.link_sockets[i]->ev_arg, NULL); + } tun_set(c->c1.tuntap, c->c2.event_set, tuntap, (void *)tun_shift, NULL= ); #if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) if (socket & EVENT_READ && c->c2.did_open_tun) @@ -2219,7 +2230,7 @@ if (!c->sig->signal_received) { - if (!(flags & IOW_CHECK_RESIDUAL) || !socket_read_residual(c->c2.l= ink_socket)) + if (!(flags & IOW_CHECK_RESIDUAL) || !sockets_read_residual(c)) { int status; @@ -2311,7 +2322,7 @@ /* TUN device ready to accept write */ else if (status & TUN_WRITE) { - process_outgoing_tun(c); + process_outgoing_tun(c, sock); } /* Incoming data on TCP/UDP port */ else if (status & SOCKET_READ) @@ -2328,7 +2339,7 @@ read_incoming_tun(c); if (!IS_SIG(c)) { - process_incoming_tun(c); + process_incoming_tun(c, sock); } } else if (status & DCO_READ) diff --git a/src/openvpn/forward.h b/src/openvpn/forward.h index 8ef19b4..5c2fdb8 100644 --- a/src/openvpn/forward.h +++ b/src/openvpn/forward.h @@ -231,10 +231,12 @@ * * If an error occurs, it is logged and the packet is dropped. * - * @param c - The context structure of the VPN tunnel associated with the - * packet. + * @param c The context structure of the VPN tunnel associated with + * the packet. + * @param out_sock Socket that will be used to send out the packet. + * */ -void process_incoming_tun(struct context *c); +void process_incoming_tun(struct context *c, struct link_socket *out_sock); /** @@ -246,10 +248,11 @@ * * If an error occurs, it is logged and the packet is dropped. * - * @param c - The context structure of the VPN tunnel associated with - * the packet. + * @param c The context structure of the VPN tunnel associated + * with the packet. + * @param in_sock Socket where the packet was received. */ -void process_outgoing_tun(struct context *c); +void process_outgoing_tun(struct context *c, struct link_socket *in_sock); /*************************************************************************= */ @@ -304,20 +307,21 @@ #define PIPV6_ICMP_NOHOST_SERVER (1<<6) -void process_ip_header(struct context *c, unsigned int flags, struct buffe= r *buf); +void process_ip_header(struct context *c, unsigned int flags, struct buffe= r *buf, + struct link_socket *sock); bool schedule_exit(struct context *c); static inline struct link_socket_info * get_link_socket_info(struct context *c) { - if (c->c2.link_socket_info) + if (c->c2.link_socket_infos) { - return c->c2.link_socket_info; + return c->c2.link_socket_infos[0]; } else { - return &c->c2.link_socket->info; + return &c->c2.link_sockets[0]->info; } } diff --git a/src/openvpn/init.c b/src/openvpn/init.c index 9371024..2cc37df 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -528,12 +528,12 @@ { /* Check if there is another resolved address to try for * the current connection */ - if (c->c1.link_socket_addr.current_remote - && c->c1.link_socket_addr.current_remote->ai_next + if (c->c1.link_socket_addrs[0].current_remote + && c->c1.link_socket_addrs[0].current_remote->ai_next && !c->options.advance_next_remote) { - c->c1.link_socket_addr.current_remote =3D - c->c1.link_socket_addr.current_remote->ai_next; + c->c1.link_socket_addrs[0].current_remote =3D + c->c1.link_socket_addrs[0].current_remote->ai_next; } else { @@ -549,20 +549,20 @@ * skipped by management on the previous loop. * If so, clear the addrinfo objects as close_instance= does */ - if (c->c1.link_socket_addr.remote_list) + if (c->c1.link_socket_addrs[0].remote_list) { - clear_remote_addrlist(&c->c1.link_socket_addr, + clear_remote_addrlist(&c->c1.link_socket_addrs[0], !c->options.resolve_in_advan= ce); } /* close_instance should have cleared the addrinfo obj= ects */ - ASSERT(c->c1.link_socket_addr.current_remote =3D=3D NU= LL); - ASSERT(c->c1.link_socket_addr.remote_list =3D=3D NULL); + ASSERT(c->c1.link_socket_addrs[0].current_remote =3D= =3D NULL); + ASSERT(c->c1.link_socket_addrs[0].remote_list =3D=3D N= ULL); } else { - c->c1.link_socket_addr.current_remote =3D - c->c1.link_socket_addr.remote_list; + c->c1.link_socket_addrs[0].current_remote =3D + c->c1.link_socket_addrs[0].remote_list; } int advance_count =3D 1; @@ -727,6 +727,13 @@ uninit_proxy_dowork(c); } +static void +do_link_socket_addr_new(struct context *c) +{ + ALLOC_ARRAY_CLEAR_GC(c->c1.link_socket_addrs, struct link_socket_addr, + c->c1.link_sockets_num, &c->gc); +} + void context_init_1(struct context *c) { @@ -736,6 +743,10 @@ init_connection_list(c); + c->c1.link_sockets_num =3D 1; + + do_link_socket_addr_new(c); + #if defined(ENABLE_PKCS11) if (c->first_time) { @@ -1636,7 +1647,7 @@ CLEAR(local); actual =3D &get_link_socket_info(c)->lsa->actual; remote =3D actual->dest; - getsockname(c->c2.link_socket->sd, &local.addr.sa, &sa_len); + getsockname(c->c2.link_sockets[0]->sd, &local.addr.sa, &sa_len); #if ENABLE_IP_PKTINFO if (!addr_defined(&local)) { @@ -1762,8 +1773,8 @@ c->options.ifconfig_ipv6_local, c->options.ifconfig_ipv6_netbits, c->options.ifconfig_ipv6_remote, - c->c1.link_socket_addr.bind_local, - c->c1.link_socket_addr.remote_list, + c->c1.link_socket_addrs[0].bind_local, + c->c1.link_socket_addrs[0].remote_list, !c->options.ifconfig_nowarn, c->c2.es, &c->net_ctx, @@ -1946,17 +1957,16 @@ do_alloc_route_list(c); /* parse and resolve the route option list */ - ASSERT(c->c2.link_socket); + ASSERT(c->c2.link_sockets[0]); if (c->options.routes && c->c1.route_list) { do_init_route_list(&c->options, c->c1.route_list, - &c->c2.link_socket->info, c->c2.es, &c->net= _ctx); + &c->c2.link_sockets[0]->info, c->c2.es, &c-= >net_ctx); } if (c->options.routes_ipv6 && c->c1.route_ipv6_list) { do_init_route_ipv6_list(&c->options, c->c1.route_ipv6_list, - &c->c2.link_socket->info, c->c2.es, - &c->net_ctx); + &c->c2.link_sockets[0]->info, c->c2.es= , &c->net_ctx); } /* do ifconfig */ @@ -1969,8 +1979,7 @@ c->options.dev_type, c->options.dev_node, &gc); - do_ifconfig(c->c1.tuntap, guess, c->c2.frame.tun_mtu, c->c2.es, - &c->net_ctx); + do_ifconfig(c->c1.tuntap, guess, c->c2.frame.tun_mtu, c->c2.es= , &c->net_ctx); } /* possibly add routes */ @@ -1985,6 +1994,7 @@ /* Store the old fd inside the fd so open_tun can use it */ c->c1.tuntap->fd =3D oldtunfd; #endif + if (dco_enabled(&c->options)) { ovpn_dco_init(c->mode, &c->c1.tuntap->dco); @@ -1996,8 +2006,7 @@ /* set the hardware address */ if (c->options.lladdr) { - set_lladdr(&c->net_ctx, c->c1.tuntap->actual_name, c->options.= lladdr, - c->c2.es); + set_lladdr(&c->net_ctx, c->c1.tuntap->actual_name, c->options.= lladdr, c->c2.es); } /* do ifconfig */ @@ -2478,7 +2487,7 @@ { msg(M_NONFATAL, "dco-win doesn't yet support reopening= TUN device"); /* prevent link_socket_close() from closing handle wit= h WinSock API */ - c->c2.link_socket->sd =3D SOCKET_UNDEFINED; + c->c2.link_sockets[0]->sd =3D SOCKET_UNDEFINED; return false; } else @@ -2701,13 +2710,23 @@ if (found & OPT_P_SOCKBUF) { msg(D_PUSH, "OPTIONS IMPORT: --sndbuf/--rcvbuf options modified"); - link_socket_update_buffer_sizes(c->c2.link_socket, c->options.rcvb= uf, c->options.sndbuf); + + for (int i =3D 0; i < c->c1.link_sockets_num; i++) + { + link_socket_update_buffer_sizes(c->c2.link_sockets[i], + c->options.rcvbuf, + c->options.sndbuf); + } } if (found & OPT_P_SOCKFLAGS) { msg(D_PUSH, "OPTIONS IMPORT: --socket-flags option modified"); - link_socket_update_flags(c->c2.link_socket, c->options.sockflags); + for (int i =3D 0; i < c->c1.link_sockets_num; i++) + { + link_socket_update_flags(c->c2.link_sockets[i], + c->options.sockflags); + } } if (found & OPT_P_PERSIST) @@ -3783,12 +3802,63 @@ static void do_link_socket_new(struct context *c) { - ASSERT(!c->c2.link_socket); - c->c2.link_socket =3D link_socket_new(); + ASSERT(!c->c2.link_sockets); + + ALLOC_ARRAY_GC(c->c2.link_sockets, struct link_socket *, + c->c1.link_sockets_num, &c->c2.gc); + + for (int i =3D 0; i < c->c1.link_sockets_num; i++) + { + c->c2.link_sockets[i] =3D link_socket_new(); + } c->c2.link_socket_owned =3D true; } /* + * bind TCP/UDP sockets + */ +static void +do_init_socket_phase1(struct context *c) +{ + for (int i =3D 0; i < c->c1.link_sockets_num; i++) + { + int mode =3D LS_MODE_DEFAULT; + + /* mode allows CM_CHILD_TCP + * instances to inherit acceptable fds + * from a top-level parent */ + if (c->options.mode =3D=3D MODE_SERVER) + { + /* initializing listening socket */ + if (c->mode =3D=3D CM_TOP) + { + mode =3D LS_MODE_TCP_LISTEN; + } + /* initializing socket to client */ + else if (c->mode =3D=3D CM_CHILD_TCP) + { + mode =3D LS_MODE_TCP_ACCEPT_FROM; + } + } + + /* init each socket with its specific args */ + link_socket_init_phase1(c, i, mode); + } +} + +/* + * finalize TCP/UDP sockets + */ +static void +do_init_socket_phase2(struct context *c) +{ + for (int i =3D 0; i < c->c1.link_sockets_num; i++) + { + link_socket_init_phase2(c, c->c2.link_sockets[i]); + } +} + +/* * Print MTU INFO */ static void @@ -3943,15 +4013,21 @@ /* in dco-win case, link socket is a tun handle which is * closed in do_close_tun(). Set it to UNDEFINED so * we won't use WinSock API to close it. */ - if (tuntap_is_dco_win(c->c1.tuntap) && c->c2.link_socket) + if (tuntap_is_dco_win(c->c1.tuntap) && c->c2.link_sockets) { - c->c2.link_socket->sd =3D SOCKET_UNDEFINED; + for (int i =3D 0; i < c->c1.link_sockets_num; i++) + { + c->c2.link_sockets[i]->sd =3D SOCKET_UNDEFINED; + } } - if (c->c2.link_socket && c->c2.link_socket_owned) + if (c->c2.link_sockets && c->c2.link_socket_owned) { - link_socket_close(c->c2.link_socket); - c->c2.link_socket =3D NULL; + for (int i =3D 0; i < c->c1.link_sockets_num; i++) + { + link_socket_close(c->c2.link_sockets[i]); + } + c->c2.link_sockets =3D NULL; } @@ -3962,28 +4038,36 @@ && ( (c->options.persist_remote_ip) || ( c->sig->source !=3D SIG_SOURCE_HARD - && ((c->c1.link_socket_addr.current_remote - && c->c1.link_socket_addr.current_remote->ai_next) + && ((c->c1.link_socket_addrs[0].current_remote + && c->c1.link_socket_addrs[0].current_remote->ai_nex= t) || c->options.no_advance)) ))) { - clear_remote_addrlist(&c->c1.link_socket_addr, !c->options.resolve= _in_advance); + clear_remote_addrlist(&c->c1.link_socket_addrs[0], + !c->options.resolve_in_advance); } /* Clear the remote actual address when persist_remote_ip is not in us= e */ - if (!(c->sig->signal_received =3D=3D SIGUSR1 && c->options.persist_rem= ote_ip)) + for (int i =3D 0; i < c->c1.link_sockets_num; i++) { - CLEAR(c->c1.link_socket_addr.actual); + if (!(c->sig->signal_received =3D=3D SIGUSR1 && c->options.persist= _remote_ip)) + { + CLEAR(c->c1.link_socket_addrs[i].actual); + } } - if (!(c->sig->signal_received =3D=3D SIGUSR1 && c->options.persist_loc= al_ip)) + for (int i =3D 0; i < c->c1.link_sockets_num; i++) { - if (c->c1.link_socket_addr.bind_local && !c->options.resolve_in_ad= vance) + if (!(c->sig->signal_received =3D=3D SIGUSR1 && c->options.persist= _local_ip)) { - freeaddrinfo(c->c1.link_socket_addr.bind_local); - } + if (c->c1.link_socket_addrs[i].bind_local + && !c->options.resolve_in_advance) + { + freeaddrinfo(c->c1.link_socket_addrs[i].bind_local); + } - c->c1.link_socket_addr.bind_local =3D NULL; + c->c1.link_socket_addrs[i].bind_local =3D NULL; + } } } @@ -4466,7 +4550,6 @@ { const struct options *options =3D &c->options; const bool child =3D (c->mode =3D=3D CM_CHILD_TCP || c->mode =3D=3D CM= _CHILD_UDP); - int link_socket_mode =3D LS_MODE_DEFAULT; /* init garbage collection level */ gc_init(&c->c2.gc); @@ -4507,21 +4590,6 @@ /* map in current connection entry */ next_connection_entry(c); - /* link_socket_mode allows CM_CHILD_TCP - * instances to inherit acceptable fds - * from a top-level parent */ - if (c->options.ce.proto =3D=3D PROTO_TCP_SERVER) - { - if (c->mode =3D=3D CM_TOP) - { - link_socket_mode =3D LS_MODE_TCP_LISTEN; - } - else if (c->mode =3D=3D CM_CHILD_TCP) - { - link_socket_mode =3D LS_MODE_TCP_ACCEPT_FROM; - } - } - /* should we disable paging? */ if (c->first_time && options->mlock) { @@ -4664,7 +4732,7 @@ /* bind the TCP/UDP socket */ if (c->mode =3D=3D CM_P2P || c->mode =3D=3D CM_TOP || c->mode =3D=3D C= M_CHILD_TCP) { - link_socket_init_phase1(c, link_socket_mode); + do_init_socket_phase1(c); } /* initialize tun/tap device object, @@ -4707,7 +4775,7 @@ /* finalize the TCP/UDP socket */ if (c->mode =3D=3D CM_P2P || c->mode =3D=3D CM_TOP || c->mode =3D=3D C= M_CHILD_TCP) { - link_socket_init_phase2(c); + do_init_socket_phase2(c); /* Update dynamic frame calculation as exact transport socket info= rmation @@ -4843,7 +4911,8 @@ void inherit_context_child(struct context *dest, - const struct context *src) + const struct context *src, + struct link_socket *ls) { CLEAR(*dest); @@ -4856,6 +4925,8 @@ /* c1 init */ packet_id_persist_init(&dest->c1.pid_persist); + dest->c1.link_sockets_num =3D 1; + do_link_socket_addr_new(dest); dest->c1.ks.key_type =3D src->c1.ks.key_type; /* inherit SSL context */ @@ -4880,7 +4951,7 @@ * The CM_TOP context does the socket listen(), * and the CM_CHILD_TCP context does the accept(). */ - dest->c2.accept_from =3D src->c2.link_socket; + dest->c2.accept_from =3D ls; } #ifdef ENABLE_PLUGIN @@ -4904,18 +4975,25 @@ /* UDP inherits some extra things which TCP does not */ if (dest->mode =3D=3D CM_CHILD_UDP) { + ASSERT(!dest->c2.link_sockets); + /* inherit buffers */ dest->c2.buffers =3D src->c2.buffers; - /* inherit parent link_socket and tuntap */ - dest->c2.link_socket =3D src->c2.link_socket; + ALLOC_ARRAY_GC(dest->c2.link_sockets, struct link_socket *, 1, &de= st->gc); - ALLOC_OBJ_GC(dest->c2.link_socket_info, struct link_socket_info, &= dest->gc); - *dest->c2.link_socket_info =3D src->c2.link_socket->info; + /* inherit parent link_socket and tuntap */ + dest->c2.link_sockets[0] =3D ls; + + ALLOC_ARRAY_GC(dest->c2.link_socket_infos, struct link_socket_info= *, 1, + &dest->gc); + ALLOC_OBJ_GC(dest->c2.link_socket_infos[0], struct link_socket_inf= o, + &dest->gc); + *dest->c2.link_socket_infos[0] =3D ls->info; /* locally override some link_socket_info fields */ - dest->c2.link_socket_info->lsa =3D &dest->c1.link_socket_addr; - dest->c2.link_socket_info->connection_established =3D false; + dest->c2.link_socket_infos[0]->lsa =3D &dest->c1.link_socket_addrs= [0]; + dest->c2.link_socket_infos[0]->connection_established =3D false; } } diff --git a/src/openvpn/init.h b/src/openvpn/init.h index ea7eb30..11c32ac 100644 --- a/src/openvpn/init.h +++ b/src/openvpn/init.h @@ -95,7 +95,8 @@ bool do_deferred_options(struct context *c, const unsigned int found); void inherit_context_child(struct context *dest, - const struct context *src); + const struct context *src, + struct link_socket *ls); void inherit_context_top(struct context *dest, const struct context *src); diff --git a/src/openvpn/mss.c b/src/openvpn/mss.c index ebdec25..89dee7d 100644 --- a/src/openvpn/mss.c +++ b/src/openvpn/mss.c @@ -357,7 +357,7 @@ struct link_socket_info *lsi =3D get_link_socket_info(c); struct options *o =3D &c->options; - int pmtu =3D c->c2.link_socket->mtu; + int pmtu =3D c->c2.link_sockets[0]->mtu; sa_family_t af =3D lsi->lsa->actual.dest.addr.sa.sa_family; int proto =3D lsi->proto; diff --git a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c index a2841f0..b5bbf13 100644 --- a/src/openvpn/mtcp.c +++ b/src/openvpn/mtcp.c @@ -114,13 +114,13 @@ #endif /* ENABLE_DEBUG */ static struct multi_instance * -multi_create_instance_tcp(struct multi_context *m) +multi_create_instance_tcp(struct multi_context *m, struct link_socket *ls) { struct gc_arena gc =3D gc_new(); struct multi_instance *mi =3D NULL; struct hash *hash =3D m->hash; - mi =3D multi_create_instance(m, NULL); + mi =3D multi_create_instance(m, NULL, ls); if (mi) { struct hash_element *he; @@ -170,13 +170,16 @@ /* buffer for queued TCP socket output packets */ mi->tcp_link_out_deferred =3D mbuf_init(m->top.options.n_bcast_buf); - ASSERT(mi->context.c2.link_socket); - ASSERT(mi->context.c2.link_socket->info.lsa); - ASSERT(mi->context.c2.link_socket->mode =3D=3D LS_MODE_TCP_ACCEPT_FROM= ); - ASSERT(mi->context.c2.link_socket->info.lsa->actual.dest.addr.sa.sa_fa= mily =3D=3D AF_INET - || mi->context.c2.link_socket->info.lsa->actual.dest.addr.sa.sa= _family =3D=3D AF_INET6 + ASSERT(mi->context.c2.link_sockets); + ASSERT(mi->context.c2.link_sockets[0]); + ASSERT(mi->context.c2.link_sockets[0]->info.lsa); + ASSERT(mi->context.c2.link_sockets[0]->mode =3D=3D LS_MODE_TCP_ACCEPT_= FROM); + ASSERT(mi->context.c2.link_sockets[0]->info.lsa->actual.dest.addr.sa.s= a_family =3D=3D AF_INET + || mi->context.c2.link_sockets[0]->info.lsa->actual.dest.addr.s= a.sa_family =3D=3D AF_INET6 ); - if (!mroute_extract_openvpn_sockaddr(&mi->real, &mi->context.c2.link_s= ocket->info.lsa->actual.dest, true)) + if (!mroute_extract_openvpn_sockaddr(&mi->real, + &mi->context.c2.link_sockets[0]->= info.lsa->actual.dest, + true)) { msg(D_MULTI_ERRORS, "MULTI TCP: TCP client address is undefined"); return false; @@ -232,7 +235,7 @@ void multi_tcp_dereference_instance(struct multi_tcp *mtcp, struct multi_instan= ce *mi) { - struct link_socket *ls =3D mi->context.c2.link_socket; + struct link_socket *ls =3D mi->context.c2.link_sockets[0]; if (ls && mi->socket_set_called) { event_del(mtcp->es, socket_event_handle(ls)); @@ -247,7 +250,7 @@ if (mi) { mi->socket_set_called =3D true; - socket_set(mi->context.c2.link_socket, + socket_set(mi->context.c2.link_sockets[0], m->mtcp->es, mbuf_defined(mi->tcp_link_out_deferred) ? EVENT_WRITE := EVENT_READ, &mi->ev_arg, @@ -261,8 +264,12 @@ { int status; unsigned int *persistent =3D &mtcp->tun_rwflags; - socket_set_listen_persistent(c->c2.link_socket, mtcp->es, - &c->c2.link_socket->ev_arg); + + for (int i =3D 0; i < c->c1.link_sockets_num; i++) + { + socket_set_listen_persistent(c->c2.link_sockets[i], mtcp->es, + &c->c2.link_sockets[i]->ev_arg); + } #ifdef _WIN32 if (tuntap_is_wintun(c->c1.tuntap)) @@ -480,16 +487,18 @@ case TA_SOCKET_READ: case TA_SOCKET_READ_RESIDUAL: ASSERT(mi); - ASSERT(mi->context.c2.link_socket); + ASSERT(mi->context.c2.link_sockets); + ASSERT(mi->context.c2.link_sockets[0]); set_prefix(mi); - read_incoming_link(&mi->context, mi->context.c2.link_socket); + read_incoming_link(&mi->context, mi->context.c2.link_sockets[0= ]); clear_prefix(); if (!IS_SIG(&mi->context)) { - multi_process_incoming_link(m, mi, mpp_flags); + multi_process_incoming_link(m, mi, mpp_flags, + mi->context.c2.link_sockets[0]= ); if (!IS_SIG(&mi->context)) { - stream_buf_read_setup(mi->context.c2.link_socket); + stream_buf_read_setup(mi->context.c2.link_sockets[0]); } } break; @@ -565,7 +574,7 @@ break; case MTP_NONE: - if (mi && socket_read_residual(c->c2.link_socket)) + if (mi && sockets_read_residual(c)) { newaction =3D TA_SOCKET_READ_RESIDUAL; } @@ -720,9 +729,14 @@ /* new incoming TCP client attempting to connect? */ case EVENT_ARG_LINK_SOCKET: - ASSERT(m->top.c2.link_socket); - socket_reset_listen_persistent(m->top.c2.link_socket); - mi =3D multi_create_instance_tcp(m); + if (!ev_arg->u.sock) + { + msg(D_MULTI_ERRORS, "MULTI: mtcp_proc_io: null soc= ket"); + break; + } + + socket_reset_listen_persistent(ev_arg->u.sock); + mi =3D multi_create_instance_tcp(m, ev_arg->u.sock); if (mi) { multi_tcp_action(m, mi, TA_INITIAL, false); diff --git a/src/openvpn/mudp.c b/src/openvpn/mudp.c index 5fbd7b0..3af5af6 100644 --- a/src/openvpn/mudp.c +++ b/src/openvpn/mudp.c @@ -185,7 +185,8 @@ */ struct multi_instance * -multi_get_create_instance_udp(struct multi_context *m, bool *floated) +multi_get_create_instance_udp(struct multi_context *m, bool *floated, + struct link_socket *ls) { struct gc_arena gc =3D gc_new(); struct mroute_addr real =3D {0}; @@ -256,7 +257,7 @@ * connect-freq but not against connect-freq-initial */ reflect_filter_rate_limit_decrease(m->initial_rate_lim= iter); - mi =3D multi_create_instance(m, &real); + mi =3D multi_create_instance(m, &real, ls); if (mi) { hash_add_fast(hash, bucket, &mi->real, hv, mi); @@ -317,7 +318,7 @@ msg_set_prefix("Connection Attempt"); m->top.c2.to_link =3D m->hmac_reply; m->top.c2.to_link_addr =3D m->hmac_reply_dest; - process_outgoing_link(&m->top, m->top.c2.link_socket); + process_outgoing_link(&m->top, m->top.c2.link_sockets[0]); m->hmac_reply_dest =3D NULL; } } @@ -326,7 +327,7 @@ * Process an I/O event. */ static void -multi_process_io_udp(struct multi_context *m) +multi_process_io_udp(struct multi_context *m, struct link_socket *sock) { const unsigned int status =3D m->top.c2.event_set_status; const unsigned int mpp_flags =3D m->top.c2.fast_io @@ -380,10 +381,11 @@ /* Incoming data on UDP port */ else if (status & SOCKET_READ) { - read_incoming_link(&m->top, m->top.c2.link_socket); + read_incoming_link(&m->top, sock); if (!IS_SIG(&m->top)) { - multi_process_incoming_link(m, NULL, mpp_flags); + multi_process_incoming_link(m, NULL, mpp_flags, + sock); } } /* Incoming data on TUN device */ @@ -514,7 +516,11 @@ else { /* process I/O */ - multi_process_io_udp(&multi); + + /* Since there's only one link_socket just use the first, in a= n upcoming + * patch this will be changed by using the link_socket returne= d by the + * event set */ + multi_process_io_udp(&multi, top->c2.link_sockets[0]); MULTI_CHECK_SIG(&multi); } diff --git a/src/openvpn/mudp.h b/src/openvpn/mudp.h index 14e90d0..b378754 100644 --- a/src/openvpn/mudp.h +++ b/src/openvpn/mudp.h @@ -55,12 +55,14 @@ * it. If no entry exists, this function handles its creation, and if * successful, returns the newly created instance. * - * @param m - The single multi_context structure. + * @param m - The single multi_context structure. + * @param ls - Listening socket where this instance is connecting= to * * @return A pointer to a multi_instance if one already existed for the * packet's source address or if one was a newly created successfully. * NULL if one did not yet exist and a new one was not created. */ -struct multi_instance *multi_get_create_instance_udp(struct multi_context = *m, bool *floated); +struct multi_instance *multi_get_create_instance_udp(struct multi_context = *m, bool *floated, + struct link_socket *l= s); #endif /* ifndef MUDP_H */ diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 45b3cfa..d9e6b1f 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -750,7 +750,8 @@ * Create a client instance object for a newly connected client. */ struct multi_instance * -multi_create_instance(struct multi_context *m, const struct mroute_addr *r= eal) +multi_create_instance(struct multi_context *m, const struct mroute_addr *r= eal, + struct link_socket *ls) { struct gc_arena gc =3D gc_new(); struct multi_instance *mi; @@ -773,7 +774,7 @@ generate_prefix(mi); } - inherit_context_child(&mi->context, &m->top); + inherit_context_child(&mi->context, &m->top, ls); if (IS_SIG(&mi->context)) { goto err; @@ -3124,7 +3125,8 @@ } void -multi_process_float(struct multi_context *m, struct multi_instance *mi) +multi_process_float(struct multi_context *m, struct multi_instance *mi, + struct link_socket *ls) { struct mroute_addr real =3D {0}; struct hash *hash =3D m->hash; @@ -3180,8 +3182,8 @@ mi->context.c2.to_link_addr =3D &mi->context.c2.from; /* inherit parent link_socket and link_socket_info */ - mi->context.c2.link_socket =3D m->top.c2.link_socket; - mi->context.c2.link_socket_info->lsa->actual =3D m->top.c2.from; + mi->context.c2.link_sockets[0] =3D ls; + mi->context.c2.link_socket_infos[0]->lsa->actual =3D m->top.c2.from; tls_update_remote_addr(mi->context.c2.tls_multi, &mi->context.c2.from); @@ -3329,7 +3331,8 @@ * i.e. client -> server direction. */ bool -multi_process_incoming_link(struct multi_context *m, struct multi_instance= *instance, const unsigned int mpp_flags) +multi_process_incoming_link(struct multi_context *m, struct multi_instance= *instance, const unsigned int mpp_flags, + struct link_socket *ls) { struct gc_arena gc =3D gc_new(); @@ -3350,7 +3353,7 @@ #ifdef MULTI_DEBUG_EVENT_LOOP printf("TCP/UDP -> TUN [%d]\n", BLEN(&m->top.c2.buf)); #endif - multi_set_pending(m, multi_get_create_instance_udp(m, &floated)); + multi_set_pending(m, multi_get_create_instance_udp(m, &floated, ls= )); } else { @@ -3391,7 +3394,7 @@ /* nonzero length means that we have a valid, decrypted pa= cked */ if (floated && c->c2.buf.len > 0) { - multi_process_float(m, m->pending); + multi_process_float(m, m->pending, ls); } process_incoming_link_part2(c, lsi, orig_buf); @@ -3610,7 +3613,7 @@ } /* encrypt in instance context */ - process_incoming_tun(c); + process_incoming_tun(c, c->c2.link_sockets[0]); /* postprocess and set wakeup */ ret =3D multi_process_post(m, m->pending, mpp_flags); @@ -3642,7 +3645,8 @@ { pip_flags |=3D PIP_MSSFIX; } - process_ip_header(&item.instance->context, pip_flags, &item.instan= ce->context.c2.buf); + process_ip_header(&item.instance->context, pip_flags, &item.instan= ce->context.c2.buf, + item.instance->context.c2.link_sockets[0]); encrypt_sign(&item.instance->context, true); mbuf_free_buf(item.buffer); diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index f386a3b..a4cea79 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -274,7 +274,8 @@ void multi_top_free(struct multi_context *m); -struct multi_instance *multi_create_instance(struct multi_context *m, cons= t struct mroute_addr *real); +struct multi_instance *multi_create_instance(struct multi_context *m, cons= t struct mroute_addr *real, + struct link_socket *ls); void multi_close_instance(struct multi_context *m, struct multi_instance *= mi, bool shutdown); @@ -288,7 +289,8 @@ * existing peer. Updates multi_instance with new address, * updates hashtables in multi_context. */ -void multi_process_float(struct multi_context *m, struct multi_instance *m= i); +void multi_process_float(struct multi_context *m, struct multi_instance *m= i, + struct link_socket *ls); #define MPP_PRE_SELECT (1<<0) #define MPP_CONDITIONAL_PRE_SELECT (1<<1) @@ -353,8 +355,10 @@ * when using TCP transport. Otherwise NULL, as is * the case when using UDP transport. * @param mpp_flags - Fast I/O optimization flags. + * @param ls - Socket where the packet was received. */ -bool multi_process_incoming_link(struct multi_context *m, struct multi_ins= tance *instance, const unsigned int mpp_flags); +bool multi_process_incoming_link(struct multi_context *m, struct multi_ins= tance *instance, const unsigned int mpp_flags, + struct link_socket *ls); /** @@ -668,7 +672,7 @@ #endif set_prefix(mi); vlan_process_outgoing_tun(m, mi); - process_outgoing_tun(&mi->context); + process_outgoing_tun(&mi->context, mi->context.c2.link_sockets[0]); ret =3D multi_process_post(m, mi, mpp_flags); clear_prefix(); return ret; @@ -683,7 +687,7 @@ { bool ret =3D true; set_prefix(mi); - process_outgoing_link(&mi->context, mi->context.c2.link_socket); + process_outgoing_link(&mi->context, mi->context.c2.link_sockets[0]); ret =3D multi_process_post(m, mi, mpp_flags); clear_prefix(); return ret; diff --git a/src/openvpn/openvpn.c b/src/openvpn/openvpn.c index c52540c..977d02d 100644 --- a/src/openvpn/openvpn.c +++ b/src/openvpn/openvpn.c @@ -91,7 +91,7 @@ } /* process the I/O which triggered select */ - process_io(c, c->c2.link_socket); + process_io(c, c->c2.link_sockets[0]); P2P_CHECK_SIG(); perf_pop(); diff --git a/src/openvpn/openvpn.h b/src/openvpn/openvpn.h index 8dfcab4..df43bba 100644 --- a/src/openvpn/openvpn.h +++ b/src/openvpn/openvpn.h @@ -154,7 +154,8 @@ */ struct context_1 { - struct link_socket_addr link_socket_addr; + int link_sockets_num; + struct link_socket_addr *link_socket_addrs; /**< Local and remote addresses on the * external network. */ @@ -233,11 +234,11 @@ /* bitmask for event status. Check event.h for possible values */ unsigned int event_set_status; - struct link_socket *link_socket; /* socket used for TCP/UDP connec= tion to remote */ + struct link_socket **link_sockets; + struct link_socket_info **link_socket_infos; + bool link_socket_owned; - /** This variable is used instead link_socket->info for P2MP UDP child= s */ - struct link_socket_info *link_socket_info; const struct link_socket *accept_from; /* possibly do accept() on a pa= rent link_socket */ struct link_socket_actual *to_link_addr; /* IP address of remote */ diff --git a/src/openvpn/ping.h b/src/openvpn/ping.h index 887172c..4a87895 100644 --- a/src/openvpn/ping.h +++ b/src/openvpn/ping.h @@ -63,7 +63,7 @@ && event_timeout_trigger(&c->c2.ping_rec_interval, &c->c2.timeval, (!c->options.ping_timer_remote - || link_socket_actual_defined(&c->c1.lin= k_socket_addr.actual)) + || link_socket_actual_defined(&c->c1.lin= k_socket_addrs[0].actual)) ? ETT_DEFAULT : 15)) { trigger_ping_timeout_signal(c); diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 01874fe..fead016 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -42,6 +42,21 @@ #include "memdbg.h" +bool +sockets_read_residual(const struct context *c) +{ + int i; + + for (i =3D 0; i < c->c1.link_sockets_num; i++) + { + if (c->c2.link_sockets[i]->stream_buf.residual_fully_formed) + { + return true; + } + } + return false; +} + /* * Convert sockflags/getaddr_flags into getaddr_flags */ @@ -1002,7 +1017,6 @@ create_socket_tcp(struct addrinfo *addrinfo) { socket_descriptor_t sd; - ASSERT(addrinfo); ASSERT(addrinfo->ai_socktype =3D=3D SOCK_STREAM); @@ -1034,7 +1048,6 @@ create_socket_udp(struct addrinfo *addrinfo, const unsigned int flags) { socket_descriptor_t sd; - ASSERT(addrinfo); ASSERT(addrinfo->ai_socktype =3D=3D SOCK_DGRAM); @@ -1444,7 +1457,6 @@ #ifdef TARGET_ANDROID protect_fd_nonlocal(sd, remote); #endif - set_nonblock(sd); status =3D connect(sd, remote, af_addr_size(remote->sa_family)); if (status) @@ -1837,19 +1849,18 @@ } void -link_socket_init_phase1(struct context *c, int mode) +link_socket_init_phase1(struct context *c, + int sock_index, + int mode) { - struct link_socket *sock =3D c->c2.link_socket; + struct link_socket *sock =3D c->c2.link_sockets[sock_index]; struct options *o =3D &c->options; ASSERT(sock); - const char *remote_host =3D o->ce.remote; - const char *remote_port =3D o->ce.remote_port; - sock->local_host =3D o->ce.local; sock->local_port =3D o->ce.local_port; - sock->remote_host =3D remote_host; - sock->remote_port =3D remote_port; + sock->remote_host =3D o->ce.remote; + sock->remote_port =3D o->ce.remote_port; sock->dns_cache =3D c->c1.dns_cache; sock->http_proxy =3D c->c1.http_proxy; sock->socks_proxy =3D c->c1.socks_proxy; @@ -1865,19 +1876,20 @@ sock->socket_buffer_sizes.sndbuf =3D o->sndbuf; sock->sockflags =3D o->sockflags; + #if PORT_SHARE if (o->port_share_host && o->port_share_port) { sock->sockflags |=3D SF_PORT_SHARE; } #endif + sock->mark =3D o->mark; - sock->bind_dev =3D o->bind_dev; sock->info.proto =3D o->ce.proto; sock->info.af =3D o->ce.af; sock->info.remote_float =3D o->ce.remote_float; - sock->info.lsa =3D &c->c1.link_socket_addr; + sock->info.lsa =3D &c->c1.link_socket_addrs[sock_index]; sock->info.bind_ipv6_only =3D o->ce.bind_ipv6_only; sock->info.ipchange_command =3D o->ipchange; sock->info.plugins =3D c->plugins; @@ -1903,24 +1915,25 @@ sock->remote_port =3D c->c1.http_proxy->options.port; /* the OpenVPN server we will use the proxy to connect to */ - sock->proxy_dest_host =3D remote_host; - sock->proxy_dest_port =3D remote_port; + sock->proxy_dest_host =3D o->ce.remote; + sock->proxy_dest_port =3D o->ce.remote_port; } /* or in Socks proxy mode? */ else if (sock->socks_proxy) { + /* the proxy server */ sock->remote_host =3D c->c1.socks_proxy->server; sock->remote_port =3D c->c1.socks_proxy->port; /* the OpenVPN server we will use the proxy to connect to */ - sock->proxy_dest_host =3D remote_host; - sock->proxy_dest_port =3D remote_port; + sock->proxy_dest_host =3D o->ce.remote; + sock->proxy_dest_port =3D o->ce.remote_port; } else { - sock->remote_host =3D remote_host; - sock->remote_port =3D remote_port; + sock->remote_host =3D o->ce.remote; + sock->remote_port =3D o->ce.remote_port; } /* bind behavior for TCP server vs. client */ @@ -2172,9 +2185,9 @@ /* finalize socket initialization */ void -link_socket_init_phase2(struct context *c) +link_socket_init_phase2(struct context *c, + struct link_socket *sock) { - struct link_socket *sock =3D c->c2.link_socket; const struct frame *frame =3D &c->c2.frame; struct signal_info *sig_info =3D c->sig; @@ -2220,7 +2233,6 @@ { create_socket(sock, sock->info.lsa->current_remote); } - } /* If socket has not already been created create it now */ @@ -2239,7 +2251,6 @@ addr_family_name(sock->info.lsa->bind_local->ai_family= )); sock->info.af =3D sock->info.lsa->bind_local->ai_family; } - create_socket(sock, sock->info.lsa->bind_local); } } @@ -2287,7 +2298,7 @@ done: if (sig_save.signal_received) { - /* Always restore the saved signal -- register/throw_signal will h= andle priority */ + /* Always restore the saved signal --register/throw_signal will ha= ndle priority */ if (sig_save.source =3D=3D SIG_SOURCE_HARD && sig_info =3D=3D &sig= info_static) { throw_signal(sig_save.signal_received); diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h index 465d92b..16106d4 100644 --- a/src/openvpn/socket.h +++ b/src/openvpn/socket.h @@ -344,9 +344,13 @@ /* * Initialize link_socket object. */ -void link_socket_init_phase1(struct context *c, int mode); +void +link_socket_init_phase1(struct context *c, + int sock_index, + int mode); -void link_socket_init_phase2(struct context *c); +void link_socket_init_phase2(struct context *c, + struct link_socket *sock); void do_preresolve(struct context *c); @@ -1228,11 +1232,12 @@ * Socket I/O wait functions */ -static inline bool -socket_read_residual(const struct link_socket *sock) -{ - return sock && sock->stream_buf.residual_fully_formed; -} +/* + * Extends the pre-existing read residual logic + * to all initialized sockets, ensuring the complete + * packet is read. + */ +bool sockets_read_residual(const struct context *c); static inline event_t socket_event_handle(const struct link_socket *sock) -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/434?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: Ia0a889e800f0b36aed770ee36e31afeec5df6084 Gerrit-Change-Number: 434 Gerrit-PatchSet: 9 Gerrit-Owner: ordex Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: its_Giaan Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: cron2 Gerrit-Attention: ordex Gerrit-MessageType: newpatchset --+4ahEaWRcVY= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: cron2, ordex, plaisthos.

its_Giaan uploaded patch set #9 to the change originall= y created by ordex.

View Change

Adapt socket handling to support listening=
 on multiple sockets

Introduce internal changes preparing the se= rver to
handle multiple sockets concurrently for both
TCP and= UDP protocols. While no user-visible
features are implemented yet, = these modifications
are essential for enabling future functionality<= br> such as listening on multiple ports.

Key changes are: con= verting link_socket from a
single pointer to an array in various con= texts,
in order to be able to store multiple sockets
at once.=

Change-Id: Ia0a889e800f0b36aed770ee36e31afeec5df6084
Signed-off-= by: Antonio Quartulli <a@...2181...>
Signed-off-by: Gianmarco De G= regori <gianmarco@...2726...>
---
M src/openvpn/dco.c
M src/= openvpn/forward.c
M src/openvpn/forward.h
M src/openvpn/init.c
M s= rc/openvpn/init.h
M src/openvpn/mss.c
M src/openvpn/mtcp.c
M src/o= penvpn/mudp.c
M src/openvpn/mudp.h
M src/openvpn/multi.c
M src/ope= nvpn/multi.h
M src/openvpn/openvpn.c
M src/openvpn/openvpn.h
M src= /openvpn/ping.h
M src/openvpn/socket.c
M src/openvpn/socket.h
16 f= iles changed, 327 insertions(+), 186 deletions(-)

git pull ssh:=
//gerrit.openvpn.net:29418/openvpn refs/changes/34/434/9
diff --git=
 a/src/openvpn/dco.c b/src/openvpn/dco.c
index dcc80d3..8f2= adb5 100644
--- a/src/openvpn/dco.c
+++ b/s= rc/openvpn/dco.c
@@ -478,11 +478,11 @@
= return 0;
}

- struct link_socket *ls =3D c->c2.l= ink_socket;
+ stru= ct link_socket *sock =3D c->c2.link_sockets[0];
=
- ASSERT(ls->info.conne= ction_established);
+= ASSERT(sock->info.connection_established);
<= br>- struct sockaddr *remotead= dr =3D &ls->info.lsa->actual.dest.addr.sa;
+ struct sockaddr *remoteaddr =3D &= sock->info.lsa->actual.dest.addr.sa;
struct tls_= multi *multi =3D c->c2.tls_multi;
#ifdef TARGET_FREEBSD=
/* In Linux in P2P mode the kernel automatically remo= ves an existing peer
@@ -494,7 +494,7 @@
= }
#endif
int ret =3D dco_new_peer(= &c->c1.tuntap->dco, multi->peer_id,
- c->c2.link_socket-= >sd, NULL, remoteaddr, NULL, NULL);
+ c->c2.link_sockets[0]->= sd, NULL, remoteaddr, NULL, NULL);
if (ret < 0)
{
return ret;
@@= -527,12 +527,12 @@
#if ENABLE_IP_PKTINFO
= struct context *c =3D &mi->context;
- if (!proto_is_udp(c->c2.= link_socket->info.proto) || !(c->options.sockflags & SF_USE_IP_PK= TINFO))
+ if (!pro= to_is_udp(c->c2.link_sockets[0]->info.proto) || !(c->options.sockf= lags & SF_USE_IP_PKTINFO))
{
= return false;
}

- struct link_socket_actual *actual = =3D &c->c2.link_socket_info->lsa->actual;
+ struct link_socket_actual *actual = =3D &c->c2.link_socket_infos[0]->lsa->actual;
=
switch (actual->dest.addr.sa.sa_family) {
@@ -577,7 +577,7 @@
int = peer_id =3D c->c2.tls_multi->peer_id;
struct soc= kaddr *remoteaddr, *localaddr =3D NULL;
struct sockadd= r_storage local =3D { 0 };
- int sd =3D c->c2.link_socket->sd;
+ int sd =3D c->c2.link_sockets[0]->sd= ;


if (c->mode = =3D=3D CM_CHILD_TCP)
@@ -587,8 +587,8 @@
= }
else
{
- ASSERT(c->c2.link_socket_info-&g= t;connection_established);
- remoteaddr =3D &c->c2.link_socket_info->lsa->actu= al.dest.addr.sa;
+ = ASSERT(c->c2.link_socket_infos[0]->connection_established);
+ remoteaddr =3D &= amp;c->c2.link_socket_infos[0]->lsa->actual.dest.addr.sa; }

/* In server mode we = need to fetch the remote addresses from the push config */
= diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c
= index 2c72001..f3f3503 100644
--- a/src/openvpn/forward.c
+++ b/src/openvpn/forward.c
@@ -58,12 +58,17= @@
wait_status_string(struct context *c, struct gc_arena = *gc)
{
struct buffer out =3D alloc_bu= f_gc(64, gc);
- buf_= printf(&out, "I/O WAIT %s|%s|%s|%s %s",
+
+ buf_printf(&out, "I/O WAIT %s|%s| %s",
tun_stat(c->c1.tuntap, EVENT_READ, gc),
tun_stat(c->c1.tuntap, EVENT_WRITE, gc),
- socket_st= at(c->c2.link_socket, EVENT_READ, gc),
- socket_stat(c->c2.link_socket, EVENT_= WRITE, gc),
tv_string(&c->c2.timeval= , gc));
+ for (int= i =3D 0; i < c->c1.link_sockets_num; i++)
+ {
+ buf_printf(&out, "\n %s|%s",+ socket_sta= t(c->c2.link_sockets[i], EVENT_READ, gc),
+ socket_stat(c->c2.link_socke= ts[i], EVENT_WRITE, gc));
+ }
return BSTR(&out);
}<= /span>

@@ -99,7 +104,7 @@
{
if (c->c2.tls_multi && c->c2.tls_exit_signa= l)
{
- if (link_socket_connection_oriented(c->c2.link_socket))
+ if (link_socket_= connection_oriented(c->c2.link_sockets[0]))
{
if (c->c2.tls_multi->n_soft_errors)
{
@@ -567,6 +572,7 @@
#ifdef ENABLE_FRAGMENT

/*
* Should w= e deliver a datagram fragment to remote?
+ * c is expected to be a single-link context (p2p or c= hild)
*/
static void
ch= eck_fragment(struct context *c)
@@ -1123,7 +1129,9 @@
decrypt_status =3D openvpn_decrypt(&c->c2.buf, c= ->c2.buffers->decrypt_buf,
= co, &c->c2.frame, ad_start);

- if (!decrypt_status= && link_socket_connection_oriented(c->c2.link_socket))+ if (!decrypt_status<= /span>
+ /* on th= e instance context we have only one socket, so just check the first one */<= /span>
+ &&am= p; link_socket_connection_oriented(c->c2.link_sockets[0]))
{

/* decryption errors are fatal i= n TCP mode */
register_signal(c->sig, SIGUS= R1, "decryption-error"); /* SOFT-SIGUSR1 -- decryption error in T= CP mode */
@@ -1451,7 +1459,7 @@
*/

void
-process_incoming_tun(struct context *c)
+process_incoming_tun(struct context *c, s= truct link_socket *out_sock)
{
struct= gc_arena gc =3D gc_new();

@@ -1484,7 +14= 92,7 @@
*/
unsigned int = flags =3D PIPV4_PASSTOS | PIP_MSSFIX | PIPV4_CLIENT_NAT
= | PIPV6_ICMP_NOHOST_CLIENT;
- process_ip_header(c, flags, &c= ->c2.buf);
+ = process_ip_header(c, flags, &c->c2.buf, out_sock);

#ifdef PACKET_TRUNCATION_CHECK
= /* if (c->c2.buf.len > 1) --c->c2.buf.len; */
@@ = -1645,7 +1653,8 @@
}

vo= id
-process_ip_header(s= truct context *c, unsigned int flags, struct buffer *buf)
+process_ip_header(struct context *c, = unsigned int flags, struct buffer *buf,
+ struct link_socket *sock)
<= span> {

if (!c->options.ce.mssfix)
= {
@@ -1679,7 +1688,7 @@
/= * extract TOS from IP header */
if (flags &= ; PIPV4_PASSTOS)
{
- link_socket_extract_tos(c->c2.= link_socket, &ipbuf);
+ link_socket_extract_tos(sock, &ipbuf);
= }
#endif

@@ -1887,7 +1896,7 @@

*/

= void
-process_ou= tgoing_tun(struct context *c)
+process_outgoing_tun(struct context *c, struct link_socket *in_so= ck)
{
/*
* Set = up for write() call to TUN/TAP
@@ -1904,9 +1913,8 @@=
* The --mssfix option requires
* us t= o examine the IP header (IPv4 or IPv6).
*/
= - process_ip_header(c,<= br>- PIP_MSS= FIX | PIPV4_EXTRACT_DHCP_ROUTER | PIPV4_CLIENT_NAT | PIP_OUTGOING,- &c-&= gt;c2.to_tun);
+ p= rocess_ip_header(c, PIP_MSSFIX|PIPV4_EXTRACT_DHCP_ROUTER|PIPV4_CLIENT_NAT|P= IP_OUTGOING, &c->c2.to_tun,
+ in_sock);

= if (c->c2.to_tun.len <=3D c->c2.frame.buf.payload_size)=
{
@@ -2180,8 +2188,11 @@
/*

* Configure event wait based on socket, t= untap flags.
*/
- socket_set(c->c2.link_socket, c->c2.event_set, so= cket,
- &= amp;c->c2.link_socket->ev_arg, NULL);
+ for (int i =3D 0; i < c->c1.link_sockets_= num; i++)
+ {
+ socket_set(c->= ;c2.link_sockets[i], c->c2.event_set, socket,
+ &c->c2.link_sockets[= i]->ev_arg, NULL);
+ }
tun_set(c->c1.tuntap, c->c2.event_set, t= untap, (void *)tun_shift, NULL);
#if defined(TARGET_LINUX)= || defined(TARGET_FREEBSD)
if (socket & EVENT_REA= D && c->c2.did_open_tun)
@@ -2219,7 +2230,7 @@

if (!c->sig->signal_received)
{
-= if (!(flags & IOW_CHECK_RESIDUAL) || !socket_read_residual(c-&g= t;c2.link_socket))
+ = if (!(flags & IOW_CHECK_RESIDUAL) || !sockets_read_residual(c))<= /span>
{
int status;<= br>
@@ -2311,7 +2322,7 @@
/* TU= N device ready to accept write */
else if (status &= ; TUN_WRITE)
{
- process_outgoing_tun(c);
+ process_outgoing_tun(c, sock); }
/* Incoming data on TCP/UDP port */
else if (status & SOCKET_READ)
@@ -232= 8,7 +2339,7 @@
read_incoming_tun(c);
if (!IS_SIG(c))

{
- process_incoming_tun(c);
+ process_incom= ing_tun(c, sock);
}
}<= br> else if (status & DCO_READ)
diff --git a/= src/openvpn/forward.h b/src/openvpn/forward.h
index 8ef19b4= ..5c2fdb8 100644
--- a/src/openvpn/forward.h
+++ b/src/openvpn/forward.h

@@ -231,10 +231,12 @@<= br> *
* If an error occurs, it is logged and the p= acket is dropped.
*
- * @param c - The context structure of the VPN tunnel assoc= iated with the
- * = packet.
+ * @param c = The context structure of the VPN tunnel associated with
+ * the packet.
+ * @param out_sock Soc= ket that will be used to send out the packet.
+ *
*/
-void process_incoming_tun(struct context *c);
+void process_incoming= _tun(struct context *c, struct link_socket *out_sock);


/**
@@ -246,10 +248,11 @@<= /span>
*
* If an error occurs, it is logged an= d the packet is dropped.
*
- * @param c - The context structure of the VPN tunne= l associated with
- * = the packet.
+ * @p= aram c The context structure of the VPN tunnel associated
+ * with the packet= .

+ * @param in_sock = Socket where the packet was received.
*/
-void process_outgoing_tun(struct cont= ext *c);

+void proces= s_outgoing_tun(struct context *c, struct link_socket *in_sock);
<= span>


/********************************= ******************************************/
@@ -304,20 +307= ,21 @@
#define PIPV6_ICMP_NOHOST_SERVER (1<<6= )


-void process_ip_header(struct context *c, unsigned int flag= s, struct buffer *buf);
+void process_ip_header(struct context *c, unsigned int flags, struct bu= ffer *buf,
+ = struct link_socket *sock);

= bool schedule_exit(struct context *c);

= static inline struct link_socket_info *
get_link_socket_in= fo(struct context *c)
{
- if (c->c2.link_socket_info)
+ if (c->c2.link_socket_infos)
{
- = return c->c2.link_socket_info;
+ return c->c2.link_socket_infos[0];
<= span> }

else
{
- return &c->c2.link_so= cket->info;

+ = return &c->c2.link_sockets[0]->info;
}
}

diff --git a/src/openvpn= /init.c b/src/openvpn/init.c
index 9371024..2cc37df 100644<= /span>
--- a/src/openvpn/init.c
+++ b/src/openvpn/= init.c
@@ -528,12 +528,12 @@
{
/* Check if there is another resolved address to = try for
* the current connection */- if (c->c1.link_s= ocket_addr.current_remote
- && c->c1.link_socket_addr.current_remote->= ;ai_next
+ = if (c->c1.link_socket_addrs[0].current_remote
+ && c->c1.link_sock= et_addrs[0].current_remote->ai_next
&am= p;& !c->options.advance_next_remote)
{<= /span>
- c->= c1.link_socket_addr.current_remote =3D
- c->c1.link_socket_addr.current_remo= te->ai_next;
+ = c->c1.link_socket_addrs[0].current_remote =3D
+ c->c1.link_= socket_addrs[0].current_remote->ai_next;

}<= /span>
else
{
= @@ -549,20 +549,20 @@
* skipped= by management on the previous loop.
= * If so, clear the addrinfo objects as close_instance does
= */
+ if (c->= ;c1.link_socket_addrs[0].remote_list)
= {
- = clear_remote_addrlist(&c->c1.link_socket_addr,
+ clear_remote_= addrlist(&c->c1.link_socket_addrs[0],
= !c->options.resolve_in_advance);=
}

= /* close_instance should have cleared the addrinfo objects */<= /span>
- AS= SERT(c->c1.link_socket_addr.current_remote =3D=3D NULL);
- ASSERT(c->c1.l= ink_socket_addr.remote_list =3D=3D NULL);

+ ASSERT(c->c1.link_socket_addrs= [0].current_remote =3D=3D NULL);
+ ASSERT(c->c1.link_socket_addrs[0].remot= e_list =3D=3D NULL);
}
= else
{
- c->c1.link_socket_ad= dr.current_remote =3D
-= c->c1.link_socket_addr.remote_list;
+ c->c1.lin= k_socket_addrs[0].current_remote =3D

+ c->c1.link_socket_addrs[0].remo= te_list;
}

int advance_count =3D 1;

@@ -727,6 +727= ,13 @@
uninit_proxy_dowork(c);
}

+static= void
+do_link_socket= _addr_new(struct context *c)
+{
+ ALLOC= _ARRAY_CLEAR_GC(c->c1.link_socket_addrs, struct link_socket_addr,=
+ c= ->c1.link_sockets_num, &c->gc);
+}
+
void
context_init_1(struct context *= c)
{
@@ -736,6 +743,10 @@
=
init_connection_list(c);
+ c->c1.link_sockets_num= =3D 1;
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ do_link_socket_addr_new(c);=

+
#= if defined(ENABLE_PKCS11)
if (c->first_time)=
{
@@ -1636,7 +1647,7 @@
= CLEAR(local);
actual =3D &get_link_socket= _info(c)->lsa->actual;
remote =3D actual->= ;dest;
- getsock= name(c->c2.link_socket->sd, &local.addr.sa, &sa_len);<= br>+ getsockname(c->c= 2.link_sockets[0]->sd, &local.addr.sa, &sa_len);
#if ENABLE_IP_PKTINFO

if (!addr_defined(&loc= al))
{
@@ -1762,8 +1773,8 @@
c->options.ifconfig_ipv6_local,<= /span>
c->options.ifconfig_ipv6_ne= tbits,
c->options.ifconfig_= ipv6_remote,
- = c->c1.link_socket_addr.bind_local,
- c->c1.lin= k_socket_addr.remote_list,
+ c->c1.link_socket_addrs[0].bind_local= ,
+ = c->c1.link_socket_addrs[0].remote_list,
= !c->options.ifconfig_nowarn,
= c->c2.es,
= &c->net_ctx,
@@ -1946,17 +1957,16 @@<= br> do_alloc_route_list(c);

/* parse and resolve the route option list */

- ASSERT(c->c2.link_socket);
+ ASSERT(c->c2= .link_sockets[0]);
if (c->options.routes &&= amp; c->c1.route_list)
{
= do_init_route_list(&c->options, c->c1.route_list,- = &c->c2.link_socket->info, c->c2.es, &c->net_ctx);
+ = &c->c2.link_sockets[0]->info, c->c2.es, &c->net_= ctx);
}
if (c->options= .routes_ipv6 && c->c1.route_ipv6_list)
= {
do_init_route_ipv6_list(&c->options, = c->c1.route_ipv6_list,
- &c->c2.link_socket->info,= c->c2.es,
- = &c->net_ctx);
+ &c->= ;c2.link_sockets[0]->info, c->c2.es, &c->net_ctx);
<= span> }


/* do ifconfig *= /
@@ -1969,8 +1979,7 @@
= c->options.dev_type,
= c->options.dev_node,
&gc);
- do_ifconfig(c-= >c1.tuntap, guess, c->c2.frame.tun_mtu, c->c2.es,
- &c->net= _ctx);
+ d= o_ifconfig(c->c1.tuntap, guess, c->c2.frame.tun_mtu, c->c2.es, &am= p;c->net_ctx);
}

/* possibly add routes */

@@ -1985,6 +1994,7 @= @
/* Store the old fd inside the fd so open_tun ca= n use it */
c->c1.tuntap->fd =3D oldtunfd;
#endif
- set_lladdr(&c->net_ctx, c-&= gt;c1.tuntap->actual_name, c->options.lladdr,
- c->c2.es);=
+ set_lladdr(&am= p;c->net_ctx, c->c1.tuntap->actual_name, c->options.lladdr, c-&= gt;c2.es);
}

= /* do ifconfig */
@@ -2478,7 +2487,7 @@
{

msg(M_NONFATAL, = "dco-win doesn't yet support reopening TUN device"); /* prevent link_socket_close() from closing ha= ndle with WinSock API */
+ c-= >c2.link_sockets[0]->sd =3D SOCKET_UNDEFINED;
= return false;
}
else

@@ -2701,13 +2710,23 @@ if (found & OPT_P_SOCKBUF)
{ msg(D_PUSH, "OPTIONS IMPORT: --sndbuf/--rcvbuf options= modified");
- = link_socket_update_buffer_sizes(c->c2.link_socket, c->options.rcv= buf, c->options.sndbuf);
+
+ for= (int i =3D 0; i < c->c1.link_sockets_num; i++)
+ {
+ link_socket_update_buffer_sizes(c->c= 2.link_sockets[i],
+ = c->options.rcvbuf,
= + = c->options.sndbuf);
+ }
}
<= br> if (found & OPT_P_SOCKFLAGS)
{
msg(D_PUSH, "OPTIONS IMPORT: --socket-flags option= modified");
- = link_socket_update_flags(c->c2.link_socket, c->options.sockflags)= ;
+ for (int i= =3D 0; i < c->c1.link_sockets_num; i++)
+ {
+ link_socket_update_flags(c->c2.link_sockets= [i],
+ = c->options.sockflags);
+ }
}

if (found & OPT_P_PERSIST)
@@ = -3783,12 +3802,63 @@
static void
do_link_= socket_new(struct context *c)
{
- ASSERT(!c->c2.link_socket);
- c->c2.link_socket =3D link_s= ocket_new();

+ ASS= ERT(!c->c2.link_sockets);
+
+ ALLOC_= ARRAY_GC(c->c2.link_sockets, struct link_socket *,
+ c->c1.link_sockets_= num, &c->c2.gc);
+
+ for (int i = =3D 0; i < c->c1.link_sockets_num; i++)
+ {
+ c->c2.link_sockets[i] =3D link_socket_new();<= br>+ }
= c->c2.link_socket_owned =3D true;
}

/*
+ = * bind TCP/UDP sockets
+static void
+do_init_socket_phase1= (struct context *c)
+= {
+ for (int i =3D= 0; i < c->c1.link_sockets_num; i++)
+ {
+ int mode =3D LS_MODE_DEFAULT;
+
+ /* mode allows CM_CHILD_TCP
+ * instances to inherit acceptable fds
+ * from a top-le= vel parent */
+ = if (c->options.mode =3D=3D MODE_SERVER)
+ {
+ /* initializing listening socket */
+ if (c->mode =3D= =3D CM_TOP)

+ = {
+ = mode =3D LS_MODE_TCP_LISTEN;
+ }
+ /* initializing socket to client */
+ else if (c->mode =3D=3D CM= _CHILD_TCP)
+ = {
+ = mode =3D LS_MODE_TCP_ACCEPT_FROM;
+ }
+ }
+=
+ /* init eac= h socket with its specific args */
+ link_socket_init_phase1(c, i, mode);
+ }

+}
+
+/*+ * finalize TCP/UDP sockets<= /span>
+ */
+static void
+do_init_socket_phase2(struct context *c)
+{
+ for (int i =3D 0; i < c->c1.lin= k_sockets_num; i++)
+= {
+ link_s= ocket_init_phase2(c, c->c2.link_sockets[i]);
+ }
+}
+
+/*
* = Print MTU INFO
*/
static void
= @@ -3943,15 +4013,21 @@
/* in dco-win case, link= socket is a tun handle which is
* closed in do_close= _tun(). Set it to UNDEFINED so
* we won't use Win= Sock API to close it. */
+ if (tuntap_i= s_dco_win(c->c1.tuntap) && c->c2.link_sockets)
{

- c->= ;c2.link_socket->sd =3D SOCKET_UNDEFINED;
+ for (int i =3D 0; i < c->c1.link_soc= kets_num; i++)
+ = {
+ c-&= gt;c2.link_sockets[i]->sd =3D SOCKET_UNDEFINED;
+ }
}
<= span>

- if (c->c= 2.link_socket && c->c2.link_socket_owned)
+ if (c->c2.link_sockets && = c->c2.link_socket_owned)
{
- link_socket_close(c->c2.link_socket= );
- c->c2.li= nk_socket =3D NULL;
+= for (int i =3D 0; i < c->c1.link_sockets_num; i++)
= + {
+ link_socket_close(c->c2.l= ink_sockets[i]);
+ = }
+ c->= ;c2.link_sockets =3D NULL;
}
<= br>
@@ -3962,28 +4038,36 @@
= && ( (c->options.persist_remote_ip)
= ||
( c->sig->source !=3D SIG_S= OURCE_HARD
- = && ((c->c1.link_socket_addr.current_remote
- && c-&g= t;c1.link_socket_addr.current_remote->ai_next)

+ && ((c->c1.link_s= ocket_addrs[0].current_remote
+ && c->c1.link_socket_addrs[0].cu= rrent_remote->ai_next)
|| c->op= tions.no_advance))
)))
= {
- clear_remot= e_addrlist(&c->c1.link_socket_addr, !c->options.resolve_in_advanc= e);
+ clear_re= mote_addrlist(&c->c1.link_socket_addrs[0],
+ !c->options.= resolve_in_advance);
}

/* Clear the remote actual address when persist_remote_ip is not in= use */

- if (!(c-&g= t;sig->signal_received =3D=3D SIGUSR1 && c->options.persist_r= emote_ip))
+ for (= int i =3D 0; i < c->c1.link_sockets_num; i++)
{<= /span>
- CLEAR(c->c1= .link_socket_addr.actual);
+ if (!(c->sig->signal_received =3D=3D SIGUSR1 &&= ; c->options.persist_remote_ip))
+ {
+ CLEAR(c->c1.link_socket_addrs[i].actual);
+ }

}=

- = if (!(c->sig->signal_received =3D=3D SIGUSR1 && c->option= s.persist_local_ip))
= + for (int i =3D 0; i < c->c1.link_sockets_num; i++)
{

- if (= c->c1.link_socket_addr.bind_local && !c->options.resolve_in_a= dvance)
+ if (= !(c->sig->signal_received =3D=3D SIGUSR1 && c->options.per= sist_local_ip))
{
- freeaddrinfo(c->c1.link_socket_addr.bin= d_local);
- }
+ if (c->= c1.link_socket_addrs[i].bind_local
+ && !c->options.resolve_in_advance= )
+ {
+ freeaddr= info(c->c1.link_socket_addrs[i].bind_local);
+ }

- c->c1.link_socket_addr.bin= d_local =3D NULL;

+ = c->c1.link_socket_addrs[i].bind_local =3D NULL;
+ }

}
}

@@ -4466,7 +4550,6 @@
{
const struct options *options =3D &a= mp;c->options;
const bool child =3D (c->mode =3D= =3D CM_CHILD_TCP || c->mode =3D=3D CM_CHILD_UDP);
- int link_socket_mode =3D LS_MODE_DEFAUL= T;

/* init garbage collection level = */
gc_init(&c->c2.gc);
@@ -4507= ,21 +4590,6 @@
/* map in current connection entry */
next_connection_entry(c);

<= span style=3D"color: hsl(0, 100%, 40%);">- /* link_socket_mode allows CM= _CHILD_TCP

- * inst= ances to inherit acceptable fds
- * from a top-level parent */
- if (c->options.ce.proto =3D=3D PROTO_TCP_SERVE= R)
- {
- if (c->mode =3D=3D CM_TO= P)

- {- link_socket_mode = =3D LS_MODE_TCP_LISTEN;
- }
- el= se if (c->mode =3D=3D CM_CHILD_TCP)
- {
- link_socket_mode =3D LS_MODE_TCP_ACCEPT_FROM;
- }

- }
-
/* should we disable paging? */ if (c->first_time && options->mlock)
<= span> {

@@ -4664,7 +4732,7 @@
/* b= ind the TCP/UDP socket */
if (c->mode =3D=3D CM_P2P= || c->mode =3D=3D CM_TOP || c->mode =3D=3D CM_CHILD_TCP)
<= span> {

- li= nk_socket_init_phase1(c, link_socket_mode);
+ do_init_socket_phase1(c);
= }

/* initialize tun/tap device o= bject,
@@ -4707,7 +4775,7 @@
/* finali= ze the TCP/UDP socket */
if (c->mode =3D=3D CM_P2P = || c->mode =3D=3D CM_TOP || c->mode =3D=3D CM_CHILD_TCP)
{

- lin= k_socket_init_phase2(c);
+ do_init_socket_phase2(c);

<= /span>
/* Update dynamic frame calculation as exact trans= port socket information
@@ -4843,7 +4911,8 @@

void
inherit_context_child(struct co= ntext *dest,
- = const struct context *src)
+ const struct context *src,=
+ stru= ct link_socket *ls)
{
CLEAR(*dest);

@@ -4856,6 +4925,8 @@

/* c1 init */
packet_id_persist_ini= t(&dest->c1.pid_persist);
+ dest->c1.link_sockets_num =3D 1;
+ do_link_socket_addr_new(dest);=

dest->c1.ks.key_type =3D src->c1.ks.= key_type;
/* inherit SSL context */
@@= -4880,7 +4951,7 @@
* The CM_TOP context does the= socket listen(),
* and the CM_CHILD_TCP context = does the accept().
*/
- dest->c2.accept_from =3D src->c2.li= nk_socket;
+ d= est->c2.accept_from =3D ls;
}

#ifdef ENABLE_PLUGIN
@@ -4904,18 +4975,25 @@<= /span>
/* UDP inherits some extra things which TCP does not *= /
if (dest->mode =3D=3D CM_CHILD_UDP)
{

+ AS= SERT(!dest->c2.link_sockets);
+
/* inherit buffers */
dest->c2.buffers =3D src->c2.buffers;


- /* inherit paren= t link_socket and tuntap */
- dest->c2.link_socket =3D src->c2.link_socket;+ ALLOC_ARRAY_GC(dest-&= gt;c2.link_sockets, struct link_socket *, 1, &dest->gc);
<= span>

- ALLOC_O= BJ_GC(dest->c2.link_socket_info, struct link_socket_info, &dest->= gc);
- *dest->= ;c2.link_socket_info =3D src->c2.link_socket->info;
+ /* inherit parent link_socket= and tuntap */
+ = dest->c2.link_sockets[0] =3D ls;
+
+= ALLOC_ARRAY_GC(dest->c2.link_socket_infos, struct link_socket_in= fo *, 1,
+ = &dest->gc);
+ ALLOC_OBJ_GC(dest->c2.link_socket_infos[0], struct li= nk_socket_info,
+ = &dest->gc);
+ *dest->c2.link_socket_infos[0] =3D ls->info;<= /span>

/* locally override some link_so= cket_info fields */
- = dest->c2.link_socket_info->lsa =3D &dest->c1.link_socket= _addr;
- dest-&g= t;c2.link_socket_info->connection_established =3D false;
+ dest->c2.link_socket_inf= os[0]->lsa =3D &dest->c1.link_socket_addrs[0];

+ dest->c2.link_socket_infos[= 0]->connection_established =3D false;
}
<= span> }


diff --git a/src/openvpn/init.h b= /src/openvpn/init.h
index ea7eb30..11c32ac 100644--- a/src/openvpn/init.h
+++ b/src/openvpn/init.h
@@ -95,7 +95,8 @@
bool do_deferred_options(s= truct context *c, const unsigned int found);

void inherit_context_child(struct context *dest,

- const struct con= text *src);
+ = const struct context *src,
+ struct link_socket *ls= );

void inherit_context_top(struct conte= xt *dest,
const struct context *s= rc);
diff --git a/src/openvpn/mss.c b/src/openvpn/mss.c
index ebdec25..89dee7d 100644
--- a/src/openvp= n/mss.c
+++ b/src/openvpn/mss.c
@@ -357,7 += 357,7 @@
struct link_socket_info *lsi =3D get_link_soc= ket_info(c);
struct options *o =3D &c->options;=

- = int pmtu =3D c->c2.link_socket->mtu;
+ int pmtu =3D c->c2.link_sockets[0]->mtu;<= /span>
sa_family_t af =3D lsi->lsa->actual.dest.addr.sa= .sa_family;
int proto =3D lsi->proto;

diff --git a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c
index a2841f0..b5bbf13 100644
--- a/src/open= vpn/mtcp.c
+++ b/src/openvpn/mtcp.c
@@ -114= ,13 +114,13 @@
#endif /* ENABLE_DEBUG */
=
static struct multi_instance *
-multi_create_instance_tcp(struct multi_context *= m)
+multi_create_inst= ance_tcp(struct multi_context *m, struct link_socket *ls)
= {
struct gc_arena gc =3D gc_new();
= struct multi_instance *mi =3D NULL;
struct hash *has= h =3D m->hash;

- mi =3D multi_create_instance(m, NULL);
+ mi =3D multi_create_instance(m, NU= LL, ls);
if (mi)
{
struct hash_element *he;

@@ -170,13 +170,16 @@
/* buffer for queued TCP socket output packets */
mi->tcp_link_out_deferred =3D mbuf_init(m->top.option= s.n_bcast_buf);

- ASSERT(mi->context.c2.link_socket);
- ASSERT(mi->context.c2.link_socket-&= gt;info.lsa);
- ASSE= RT(mi->context.c2.link_socket->mode =3D=3D LS_MODE_TCP_ACCEPT_FROM);<= /span>
- ASSERT(mi->cont= ext.c2.link_socket->info.lsa->actual.dest.addr.sa.sa_family =3D=3D AF= _INET
- || mi= ->context.c2.link_socket->info.lsa->actual.dest.addr.sa.sa_family = =3D=3D AF_INET6
+ = ASSERT(mi->context.c2.link_sockets);
+ ASSERT(mi->context.c2.link_sockets[0]);+ ASSERT(mi->context.c2= .link_sockets[0]->info.lsa);
+ ASSERT(mi->context.c2.link_sockets[0]->mode =3D=3D LS= _MODE_TCP_ACCEPT_FROM);
+ ASSERT(mi->context.c2.link_sockets[0]->info.lsa->actual.de= st.addr.sa.sa_family =3D=3D AF_INET
+ || mi->context.c2.link_sockets[0]->info.ls= a->actual.dest.addr.sa.sa_family =3D=3D AF_INET6
= );
- if (!mrout= e_extract_openvpn_sockaddr(&mi->real, &mi->context.c2.link_so= cket->info.lsa->actual.dest, true))
+ if (!mroute_extract_openvpn_sockaddr(&mi->= real,
+ = &mi->context.c2.link_sockets[0]->info.= lsa->actual.dest,
= + true))
{
msg(D_MULTI_ERRORS, "MULTI TCP: TCP client addre= ss is undefined");
return false;
@@ -232,7 +235,7 @@

void
multi_tcp_de= reference_instance(struct multi_tcp *mtcp, struct multi_instance *mi)
{
- stru= ct link_socket *ls =3D mi->context.c2.link_socket;
+ struct link_socket *ls =3D mi->con= text.c2.link_sockets[0];
if (ls && mi->sock= et_set_called)
{
event_del(mt= cp->es, socket_event_handle(ls));
@@ -247,7 +250,7 @@
if (mi)
{
= mi->socket_set_called =3D true;
- socket_set(mi->context.c2.link_socket,
+ socket_set(mi->contex= t.c2.link_sockets[0],

m->mtcp->es= ,
mbuf_defined(mi->tcp_link_out_defe= rred) ? EVENT_WRITE : EVENT_READ,
&= mi->ev_arg,
@@ -261,8 +264,12 @@
{
int status;
unsigned int *persistent = =3D &mtcp->tun_rwflags;
- socket_set_listen_persistent(c->c2.link_socket, mtcp->es= ,
- = &c->c2.link_socket->ev_arg);
+
+ for (int i =3D 0; i < c->c1.link_sockets_num; i++)=
+ {
+ socket_set_listen_persist= ent(c->c2.link_sockets[i], mtcp->es,

+ &c->c2.l= ink_sockets[i]->ev_arg);
+ }

#ifdef _WIN32
if (tuntap_is_wintun(c->c1.tuntap))

@@ -480,16 = +487,18 @@
case TA_SOCKET_READ:
= case TA_SOCKET_READ_RESIDUAL:
ASSERT(mi= );
- ASSERT(= mi->context.c2.link_socket);
+ ASSERT(mi->context.c2.link_sockets);
<= span style=3D"color: hsl(120, 100%, 40%);">+ ASSERT(mi->conte= xt.c2.link_sockets[0]);

set_prefix(mi);=
- read_incoming_li= nk(&mi->context, mi->context.c2.link_socket);
+ read_incoming_link(&mi-= >context, mi->context.c2.link_sockets[0]);
= clear_prefix();
if (!IS_SIG(&mi->con= text))
{
- multi_process_incoming_link(m, mi, mpp_flag= s);
+ = multi_process_incoming_link(m, mi, mpp_flags,
+ mi-&g= t;context.c2.link_sockets[0]);
if (!IS_SIG= (&mi->context))
{
- stream_buf_read_set= up(mi->context.c2.link_socket);
+ stream_buf_read_setup(mi->context.c2.= link_sockets[0]);
}
= }
break;
@@ -565,7 +574= ,7 @@
break;

case MTP_NONE:

+ if (mi= && sockets_read_residual(c))
{=
newaction =3D TA_SOCKET_READ_RESIDUAL; }
@@ -720,9 +729,14 @@
=
/* new incoming TCP client attempting to= connect? */
case EVENT_ARG_LINK_SOCKET:
- ASS= ERT(m->top.c2.link_socket);
- socket_reset_listen_persistent(m->top.c2.li= nk_socket);
- = mi =3D multi_create_instance_tcp(m);
+ if (!ev_arg->u.sock)
+ {
+ = msg(D_MULTI_ERRORS, "MULTI: mtcp_proc_io: null socket");=
+ br= eak;
+ = }
+
+ socket_reset= _listen_persistent(ev_arg->u.sock);

+ mi =3D multi_create_instance_tcp(m, = ev_arg->u.sock);
if (mi)
= {
multi= _tcp_action(m, mi, TA_INITIAL, false);
diff --git a/src/ope= nvpn/mudp.c b/src/openvpn/mudp.c
index 5fbd7b0..3af5af6 100= 644
--- a/src/openvpn/mudp.c
+++ b/src/open= vpn/mudp.c
@@ -185,7 +185,8 @@
*/<= br>
struct multi_instance *
-multi_get_create_instance_udp(struct multi_= context *m, bool *floated)
+multi_get_create_instance_udp(struct multi_context *m, bool *floated= ,
+ = struct link_socket *ls)
{
= struct gc_arena gc =3D gc_new();
struct mroute_addr r= eal =3D {0};
@@ -256,7 +257,7 @@
= * connect-freq but not against connect-freq-initial */ reflect_filter_rate_limit_decrease(m->initi= al_rate_limiter);

- mi =3D multi_create_instance(m, &real= );
+ = mi =3D multi_create_instance(m, &real, ls);
= if (mi)
{
hash_add_fast(hash, bucket, &mi->real, hv,= mi);

@@ -317,7 +318,7 @@
msg_set_= prefix("Connection Attempt");
m->top.= c2.to_link =3D m->hmac_reply;
m->top.c2.to_l= ink_addr =3D m->hmac_reply_dest;
- process_outgoing_link(&m->top, m->top.c2.li= nk_socket);
+ = process_outgoing_link(&m->top, m->top.c2.link_sockets[0]);=
m->hmac_reply_dest =3D NULL;
}
}
@@ -326,7 +327,7 @@
* P= rocess an I/O event.
*/
static void
-multi_process_io_udp(struc= t multi_context *m)
+= multi_process_io_udp(struct multi_context *m, struct link_socket *sock)
{
const unsigned int status =3D m->to= p.c2.event_set_status;
const unsigned int mpp_flags = =3D m->top.c2.fast_io
@@ -380,10 +381,11 @@
/* Incoming data on UDP port */

else if (stat= us & SOCKET_READ)
{
- read_incoming_link(&m->top, m->top= .c2.link_socket);
+ = read_incoming_link(&m->top, sock);
if= (!IS_SIG(&m->top))
{
- multi_process_incoming_link(m, = NULL, mpp_flags);
+ = multi_process_incoming_link(m, NULL, mpp_flags,
+ = sock);
}
}
/* Incoming data on TUN device */

@@ -514,7 +516,11 @= @
else
{
= /* process I/O */
- multi_process_io_udp(&multi);
+
+ /* Since there's only one link_socket just = use the first, in an upcoming
+ * patch this will be changed by using the link_socke= t returned by the
+ = * event set */
+ multi_process_io_udp(&multi, top->c2.link_sockets[= 0]);
MULTI_CHECK_SIG(&multi);
}


diff --git a/src/openvpn/m= udp.h b/src/openvpn/mudp.h
index 14e90d0..b378754 100644
--- a/src/openvpn/mudp.h
+++ b/src/openvpn/mu= dp.h
@@ -55,12 +55,14 @@
* it. If no ent= ry exists, this function handles its creation, and if
* s= uccessful, returns the newly created instance.
*- * @param m - The s= ingle multi_context structure.
+ * @param m - The single multi_context structure.
+ * @param ls - = Listening socket where this instance is connecting to
*
* @return A pointer to a multi_instance if one already exi= sted for the
* packet's source address or if one = was a newly created successfully.
* NULL if one did = not yet exist and a new one was not created.
*/-struct multi_instance *multi_ge= t_create_instance_udp(struct multi_context *m, bool *floated);
+struct multi_instance *multi_get= _create_instance_udp(struct multi_context *m, bool *floated,

+ = struct link_socket *ls);


#endif /* ifndef MUDP_H */

diff --git a/src/openvpn/mul= ti.c b/src/openvpn/multi.c
index 45b3cfa..d9e6b1f 100644
--- a/src/openvpn/multi.c
+++ b/src/openvpn/m= ulti.c
@@ -750,7 +750,8 @@
* Create a cli= ent instance object for a newly connected client.
*/
struct multi_instance *
-multi_create_instance(struct multi_context *m, const struct = mroute_addr *real)
+m= ulti_create_instance(struct multi_context *m, const struct mroute_addr *rea= l,
+ = struct link_socket *ls)
{
struct= gc_arena gc =3D gc_new();
struct multi_instance *mi;<= /span>
@@ -773,7 +774,7 @@
generate_prefi= x(mi);
}

- inherit_context_child(&mi->context, &a= mp;m->top);
+ i= nherit_context_child(&mi->context, &m->top, ls);
if (IS_SIG(&mi->context))

{
= goto err;
@@ -3124,7 +3125,8 @@
}


void
-multi_process_float(struct multi_context *m, struc= t multi_instance *mi)
+multi_process_float(struct multi_context *m, struct multi_instance *mi,
+ s= truct link_socket *ls)
{
struct mrout= e_addr real =3D {0};
struct hash *hash =3D m->hash;=
@@ -3180,8 +3182,8 @@
mi->context.= c2.to_link_addr =3D &mi->context.c2.from;
/* inherit parent link_socket and link_socket_info */- mi->context.c2.link_soc= ket =3D m->top.c2.link_socket;
- mi->context.c2.link_socket_info->lsa->actual =3D m= ->top.c2.from;
+ = mi->context.c2.link_sockets[0] =3D ls;
+ mi->context.c2.link_socket_infos[0]->lsa-= >actual =3D m->top.c2.from;

tl= s_update_remote_addr(mi->context.c2.tls_multi, &mi->context.c2.fr= om);

@@ -3329,7 +3331,8 @@
* i.e. client -> server direction.

*/
bool

-multi_proces= s_incoming_link(struct multi_context *m, struct multi_instance *instance, c= onst unsigned int mpp_flags)
+multi_process_incoming_link(struct multi_context *m, struct multi_= instance *instance, const unsigned int mpp_flags,
+ struct link_socke= t *ls)
{
struct gc_arena gc =3D gc_ne= w();

@@ -3350,7 +3353,7 @@
#ifdef MULTI_DEBUG_EVENT_LOOP

printf("TCP/= UDP -> TUN [%d]\n", BLEN(&m->top.c2.buf));
= #endif
- multi_s= et_pending(m, multi_get_create_instance_udp(m, &floated));
+ multi_set_pending(m, mul= ti_get_create_instance_udp(m, &floated, ls));

}
else
{
@@ -3391,7 += 3394,7 @@
/* nonzero length means that we = have a valid, decrypted packed */
if (floa= ted && c->c2.buf.len > 0)
{<= /span>
- mu= lti_process_float(m, m->pending);
+ multi_process_float(m, m->pending, = ls);
}

= process_incoming_link_part2(c, lsi, orig_buf);
@@ -3610,7 +3613,7 @@

}
=
/* encrypt in instance context= */
- = process_incoming_tun(c);
+ process_incoming_tun(c, c->c2.link_sockets[0]= );

/* postprocess an= d set wakeup */
ret =3D multi_process_= post(m, m->pending, mpp_flags);
@@ -3642,7 +3645,8 @@
{
pip_flags |=3D PIP_MS= SFIX;
}
- process_ip_header(&item.instance->context, pip_fl= ags, &item.instance->context.c2.buf);
+ process_ip_header(&item.instance->c= ontext, pip_flags, &item.instance->context.c2.buf,
+ item.instan= ce->context.c2.link_sockets[0]);
encrypt_sign(&= amp;item.instance->context, true);
mbuf_free_bu= f(item.buffer);

diff --git a/src/openvpn/= multi.h b/src/openvpn/multi.h
index f386a3b..a4cea79 100644=
--- a/src/openvpn/multi.h
+++ b/src/openvp= n/multi.h
@@ -274,7 +274,8 @@

<= span> void multi_top_free(struct multi_context *m);


-struct multi_instance *mult= i_create_instance(struct multi_context *m, const struct mroute_addr *real);=
+struct multi_instan= ce *multi_create_instance(struct multi_context *m, const struct mroute_addr= *real,
+ = struct link_socket *ls);
=
void multi_close_instance(struct multi_context *m, struct= multi_instance *mi, bool shutdown);

@@ -= 288,7 +289,8 @@
* existing peer. Updates multi_instance w= ith new address,
* updates hashtables in multi_context.
*/
-vo= id multi_process_float(struct multi_context *m, struct multi_instance *mi);=
+void multi_process_= float(struct multi_context *m, struct multi_instance *mi,
+ struct link_= socket *ls);

#define MPP_PRE_SELECT = (1<<0)
#define MPP_CONDITIONAL_PRE_SELECT (1= <<1)
@@ -353,8 +355,10 @@
* = when using TCP transport. Otherwise NULL, as is
* the case when using UDP transport.

= * @param mpp_flags - Fast I/O optimization flags.
+ * @param ls - Socket wh= ere the packet was received.

*/
-bool multi_process_incoming_link(struct multi_c= ontext *m, struct multi_instance *instance, const unsigned int mpp_flags);<= /span>
+bool multi_process_i= ncoming_link(struct multi_context *m, struct multi_instance *instance, cons= t unsigned int mpp_flags,
+ struct link_socket *ls);
<= span>


/**
@@ -668,7 +67= 2,7 @@
#endif
set_prefix(mi);<= br> vlan_process_outgoing_tun(m, mi);
- process_outgoing_tun(&mi->context);
+ process_outgoing_t= un(&mi->context, mi->context.c2.link_sockets[0]);
ret =3D multi_process_post(m, mi, mpp_flags);

cl= ear_prefix();
return ret;
@@ -683,7 +6= 87,7 @@
{
bool ret =3D true; set_prefix(mi);
- process_outgoing_link(&mi->context, mi->context.c2.lin= k_socket);
+ proce= ss_outgoing_link(&mi->context, mi->context.c2.link_sockets[0]);
ret =3D multi_process_post(m, mi, mpp_flags); clear_prefix();
return ret;
diff --git a/src/openvpn/openvpn.c b/src/openvpn/openvpn.c

index c52540c..977d02d 100644

--- a/src/openvpn/openvpn.c=
+++ b/src/openvpn/openvpn.c
@@ -91,7 +91,7= @@
}

/= * process the I/O which triggered select */
- process_io(c, c->c2.link_socket);+ process_io(c, c->= c2.link_sockets[0]);
P2P_CHECK_SIG();

perf_pop();
diff --git a/src= /openvpn/openvpn.h b/src/openvpn/openvpn.h
index 8dfcab4..d= f43bba 100644
--- a/src/openvpn/openvpn.h
+= ++ b/src/openvpn/openvpn.h
@@ -154,7 +154,8 @@
*/

struct context_1
{
- struct link_socket_addr link_so= cket_addr;

+ int l= ink_sockets_num;
+ = struct link_socket_addr *link_socket_addrs;
/**< L= ocal and remote addresses on the
* external network= . */

@@ -233,11 +234,11 @@
/* bitmask for event status. Check event.h for possible values */
unsigned int event_set_status;
<= br>- struct link_socket *link_= socket; /* socket used for TCP/UDP connection to remote */
+ struct link_socket **link_so= ckets;

+ struct li= nk_socket_info **link_socket_infos;
+
bool link_socket_owned;

- /** This vari= able is used instead link_socket->info for P2MP UDP childs */
= - struct link_socket_info *lin= k_socket_info;
const struct link_socket *accept_from; = /* possibly do accept() on a parent link_socket */
=
struct link_socket_actual *to_link_addr; /* IP address of= remote */
diff --git a/src/openvpn/ping.h b/src/openvpn/pi= ng.h
index 887172c..4a87895 100644
--- a/sr= c/openvpn/ping.h
+++ b/src/openvpn/ping.h
@= @ -63,7 +63,7 @@
&& event_timeout_trigger(= &c->c2.ping_rec_interval,
= &c->c2.timeval,
= (!c->options.ping_timer_remote
- || link_socket_actual_d= efined(&c->c1.link_socket_addr.actual))
+ || link_socket= _actual_defined(&c->c1.link_socket_addrs[0].actual))
? ETT_DEFAULT : 15))

= {
trigger_ping_timeout_signal(c);
diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c

= index 01874fe..fead016 100644
--- a/src/openvpn/socket.c
+++ b/src/openvpn/socket.c
@@ -42,6 +42,21 @@=

#include "memdbg.h"
+bool=
+sockets_read_residual(cons= t struct context *c)
= +{
+ int i;=
+
+ for (i =3D 0; i < c->c1.link_socket= s_num; i++)
+ {
+ if (c->c2.l= ink_sockets[i]->stream_buf.residual_fully_formed)
+ {
+ return true;
+ }
+ }
+ return false;
+
/*

* Convert sockflags/getaddr_flags into getaddr_flag= s
*/
@@ -1002,7 +1017,6 @@
create_socket_tcp(struct addrinfo *addrinfo)

{ socket_descriptor_t sd;
-
ASSERT(addrinfo);
= ASSERT(addrinfo->ai_socktype =3D=3D SOCK_STREAM);

@@ -1034,7 +1048,6 @@
create_socket_udp(struc= t addrinfo *addrinfo, const unsigned int flags)
{ socket_descriptor_t sd;
-
ASSERT(addrinfo);
= ASSERT(addrinfo->ai_socktype =3D=3D SOCK_DGRAM);

@@ -1444,7 +1457,6 @@
#ifdef TARGET_ANDROID
protect_fd_nonlocal(sd, remote);
#endif=
-
= set_nonblock(sd);
status =3D connect(sd, remote, af_a= ddr_size(remote->sa_family));
if (status)@@ -1837,19 +1849,18 @@
}
<= br> void
-link_so= cket_init_phase1(struct context *c, int mode)
+link_socket_init_phase1(struct context *c,=
+ in= t sock_index,
+ = int mode)
{
- struct link_socket *sock =3D c->c2.link_soc= ket;
+ struct link= _socket *sock =3D c->c2.link_sockets[sock_index];
s= truct options *o =3D &c->options;
ASSERT(sock);=

- = const char *remote_host =3D o->ce.remote;
- const char *remote_port =3D o->ce.remote_por= t;
-
= sock->local_host =3D o->ce.local;
sock->lo= cal_port =3D o->ce.local_port;
- sock->remote_host =3D remote_host;
- sock->remote_port =3D remote_port;
+ sock->remote_h= ost =3D o->ce.remote;
+ sock->remote_port =3D o->ce.remote_port;
= sock->dns_cache =3D c->c1.dns_cache;
sock->= http_proxy =3D c->c1.http_proxy;
sock->socks_pro= xy =3D c->c1.socks_proxy;
@@ -1865,19 +1876,20 @@=
sock->socket_buffer_sizes.sndbuf =3D o->sndbuf;=

sock->sockflags =3D o->sockflags;
+
#if = PORT_SHARE
if (o->port_share_host && o->= port_share_port)
{
sock->s= ockflags |=3D SF_PORT_SHARE;
}
#endif=
+
= sock->mark =3D o->mark;
- sock->bind_dev =3D o->bind_dev;

sock->info.proto =3D o->ce.proto;
= sock->info.af =3D o->ce.af;
sock->info.rem= ote_float =3D o->ce.remote_float;
- sock->info.lsa =3D &c->c1.link_socket_addr;
+ sock->info.lsa = =3D &c->c1.link_socket_addrs[sock_index];
sock-= >info.bind_ipv6_only =3D o->ce.bind_ipv6_only;
s= ock->info.ipchange_command =3D o->ipchange;
sock= ->info.plugins =3D c->plugins;
@@ -1903,24 +1915,25 @= @
sock->remote_port =3D c->c1.http_proxy->= ;options.port;

/* the OpenVPN se= rver we will use the proxy to connect to */
- sock->proxy_dest_host =3D remote_host;
- sock->proxy_de= st_port =3D remote_port;
+ sock->proxy_dest_host =3D o->ce.remote;
+ sock->proxy_dest_port =3D= o->ce.remote_port;
}
/* or in= Socks proxy mode? */
else if (sock->socks_proxy)
{
+
/* the proxy server */
= sock->remote_host =3D c->c1.socks_proxy->server;
= sock->remote_port =3D c->c1.socks_proxy->port;
=
/* the OpenVPN server we will use the prox= y to connect to */
- = sock->proxy_dest_host =3D remote_host;
- sock->proxy_dest_port =3D remote_port;<= /span>
+ sock->pro= xy_dest_host =3D o->ce.remote;
+ sock->proxy_dest_port =3D o->ce.remote_port;
}
else
{
- sock->remote_hos= t =3D remote_host;
- = sock->remote_port =3D remote_port;
+ sock->remote_host =3D o->ce.remote;
+ sock->remot= e_port =3D o->ce.remote_port;
}

/* bind behavior for TCP server vs. client */@@ -2172,9 +2185,9 @@

/* finalize= socket initialization */
void
-link_socket_init_phase2(struct context *c)=
+link_socket_init_phase2(st= ruct context *c,
+ = struct link_socket *sock)
{- struct link_socket *sock = =3D c->c2.link_socket;
const struct frame *frame = =3D &c->c2.frame;
struct signal_info *sig_info = =3D c->sig;

@@ -2220,7 +2233,6 @@
{
create_socket(sock, soc= k->info.lsa->current_remote);
}
-

}
=
/* If socket has not already been created crea= te it now */
@@ -2239,7 +2251,6 @@
= addr_family_name(sock->info.lsa->bind_local->ai_famil= y));
sock->info.af =3D sock->info.ls= a->bind_local->ai_family;
}
-

crea= te_socket(sock, sock->info.lsa->bind_local);
= }
}
@@ -2287,7 +2298,7 @@
<= span> done:

if (sig_save.signal_received)
{

- /* = Always restore the saved signal -- register/throw_signal will handle priori= ty */
+ /* Alw= ays restore the saved signal --register/throw_signal will handle priority *= /
if (sig_save.source =3D=3D SIG_SOURCE_HARD &= & sig_info =3D=3D &siginfo_static)
{
throw_signal(sig_save.signal_received);
<= span>diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h

index 465d92b..16106d4 100644

--- a/src/openvpn/socket.h=
+++ b/src/openvpn/socket.h
@@ -344,9 +344,= 13 @@
/*
* Initialize link_socket object= .
*/
= -void link_socket_init_phase1(struct context *c, int mode);
+void

+link_socket_init_phase1(struct context *c,<= br>+ int= sock_index,
+ = int mode);

-void link_socket_init_phase2(struct context *c);
+void link_socket_init_= phase2(struct context *c,
+ struct link_socket *sock);

void do_preresolve(struct context *c);

@@ -1228,11 +1232,12 @@
* Socket I/O = wait functions
*/

-static inline bool
-socket_read_residual(const struct link_socket = *sock)
-{
- return sock && sock->= ;stream_buf.residual_fully_formed;

-}
+/*
+ * Extends the pre-exi= sting read residual logic
+ * to all initialized sockets, ensuring the complete
+ * packet is read.
+ */
+bool sockets_read_residual(const struct context *c);<= /span>

static inline event_t
s= ocket_event_handle(const struct link_socket *sock)
<= br>

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

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia0a889e800f0b36aed770ee36e3= 1afeec5df6084
Gerrit-Change-Number: 434
Gerrit-PatchSet: 9
Gerrit-Owner: ordex <a@...2181...>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: its_Giaan <gianmarco@...1664...= 26...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: cron2 <gert@...1296...>= ;
Gerrit-Attention: ordex <a@...2181...> <= /div>
Gerrit-MessageType: newpatchset
--+4ahEaWRcVY=--