From de345ffbbabe1de0707f0fdc22bfd10c7a7a62f1 Mon Sep 17 00:00:00 2001 From: Sergey Ryazanov Date: Tue, 21 Jun 2016 12:07:31 +0300 Subject: [PATCH] Another one fix for control buf handling in udp_xmit To be able to put data to control buffer, it should be allocated and assigned to message header. Do that unconditionally before message build. Clear the control buffer fields just before message sending if control buffer empty. This change should finally fix 4830a2008917f4882a2b293e3b22dce2f9af570e (use address from the last received UDP packet in UDP messages response) and bug introduced in 90368df06394a26639341a50fac17a87e188c638 (Fix control buffer handling in udp_xmit) --- network.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/network.c b/network.c index 8596e623..66109e97 100644 --- a/network.c +++ b/network.c @@ -286,6 +286,8 @@ void udp_xmit (struct buffer *buf, struct tunnel *t) * OKAY, now send a packet with the right SAref values. */ memset(&msgh, 0, sizeof(struct msghdr)); + msgh.msg_control = cbuf; + msgh.msg_controllen = sizeof(cbuf); if (gconfig.ipsecsaref && t->refhim != IPSEC_SAREF_NULL) { cmsg = CMSG_FIRSTHDR(&msgh); @@ -303,9 +305,6 @@ void udp_xmit (struct buffer *buf, struct tunnel *t) } #ifdef LINUX - msgh.msg_control = cbuf; - msgh.msg_controllen = sizeof(cbuf); - if (t->my_addr.ipi_addr.s_addr){ struct in_pktinfo *pktinfo; @@ -325,20 +324,17 @@ void udp_xmit (struct buffer *buf, struct tunnel *t) finallen += cmsg->cmsg_len; } - - msgh.msg_controllen = finallen; #endif /* * Some OS don't like assigned buffer with zero length (e.g. OpenBSD), * some OS don't like empty buffer with non-zero length (e.g. Linux). * So make them all happy by assigning control buffer only if we really - * have something there. + * have something there and zero both fields otherwise. */ - if (finallen) { - msgh.msg_control = cbuf; - msgh.msg_controllen = finallen; - } + msgh.msg_controllen = finallen; + if (!finallen) + msgh.msg_control = NULL; iov.iov_base = buf->start; iov.iov_len = buf->len;