From 035aeb82e4b9c41b0a34e0f77ec95463f8e7e1b7 Mon Sep 17 00:00:00 2001
From: "Kevin P. Fleming" <kpfleming@digium.com>
Date: Thu, 12 Oct 2006 18:43:52 +0000
Subject: [PATCH] Merged revisions 44956 via svnmerge from
 https://origsvn.digium.com/svn/asterisk/branches/1.4

................
r44956 | kpfleming | 2006-10-12 13:38:51 -0500 (Thu, 12 Oct 2006) | 10 lines

Merged revisions 44955 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.2

........
r44955 | kpfleming | 2006-10-12 13:31:26 -0500 (Thu, 12 Oct 2006) | 2 lines

ensure that IAX2 and SIP sockets allow UDP fragmentation when running on Linux (thanks to Brian Candler on the asterisk-dev list for the tip)

........

................


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@44957 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 channels/chan_sip.c      |  3 +++
 include/asterisk/utils.h | 15 +++++++++++++++
 main/netsock.c           |  2 ++
 main/utils.c             | 10 ++++++++++
 4 files changed, 30 insertions(+)

diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 636efc456a..5a18342fd0 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -16041,10 +16041,13 @@ static int reload_config(enum channelreloadreason reason)
 		} else {
 			/* Allow SIP clients on the same host to access us: */
 			const int reuseFlag = 1;
+
 			setsockopt(sipsock, SOL_SOCKET, SO_REUSEADDR,
 				   (const char*)&reuseFlag,
 				   sizeof reuseFlag);
 
+			ast_enable_packet_fragmentation(sipsock);
+
 			if (bind(sipsock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
 				ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
 				ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h
index 9ae2e4595a..30e9523e29 100644
--- a/include/asterisk/utils.h
+++ b/include/asterisk/utils.h
@@ -515,4 +515,19 @@ int _ast_vasprintf(char **ret, const char *file, int lineno, const char *func, c
 	}))
 #endif
 
+/*!
+  \brief Disable PMTU discovery on a socket
+  \param sock The socket to manipulate
+  \return Nothing
+
+  On Linux, UDP sockets default to sending packets with the Dont Fragment (DF)
+  bit set. This is supposedly done to allow the application to do PMTU
+  discovery, but Asterisk does not do this.
+
+  Because of this, UDP packets sent by Asterisk that are larger than the MTU
+  of any hop in the path will be lost. This function can be called on a socket
+  to ensure that the DF bit will not be set.
+ */
+void ast_enable_packet_fragmentation(int sock);
+
 #endif /* _ASTERISK_UTILS_H */
diff --git a/main/netsock.c b/main/netsock.c
index e19433bbbe..46b3060747 100644
--- a/main/netsock.c
+++ b/main/netsock.c
@@ -144,6 +144,8 @@ struct ast_netsock *ast_netsock_bindaddr(struct ast_netsock_list *list, struct i
 	if (setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))) 
 		ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
 
+	ast_enable_packet_fragmentation(netsocket);
+
 	if (!(ns = ast_calloc(1, sizeof(struct ast_netsock)))) {
 		close(netsocket);
 		return NULL;
diff --git a/main/utils.c b/main/utils.c
index 6ba136ed67..9ff2fd48ac 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -984,3 +984,13 @@ int ast_dynamic_str_thread_build_va(struct ast_dynamic_str **buf, size_t max_len
 
 	return res;
 }
+
+void ast_enable_packet_fragmentation(int sock)
+{
+#ifdef __linux__
+	int val = IP_PMTUDISC_DONT;
+	
+	if (setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val)))
+		ast_log(LOG_WARNING, "Unable to disable PMTU discovery. Large UDP packets may fail to be delivered when sent from this socket.\n");
+#endif
+}
-- 
GitLab