195a78e
diff --git a/config.h.in b/config.h.in
195a78e
index 103ad9f..0bb29d9 100644
195a78e
--- a/config.h.in
195a78e
+++ b/config.h.in
195a78e
@@ -847,6 +847,14 @@
195a78e
 /* Define if you enable libevent */
195a78e
 #undef USE_LIBEVENT
195a78e
 
195a78e
+/* WARNING! This is only for the libunbound on Linux and does not affect
195a78e
+   unbound resolving daemon itself. This may severely limit the number of
195a78e
+   available outgoing ports and thus decrease randomness. Define this only
195a78e
+   when the target system restricts (e.g. some of SELinux enabled
195a78e
+   distributions) the use of non-ephemeral ports. Define this to enable use of
195a78e
+   /proc/sys/net/ipv4/ip_local_port_range as a default outgoing port range. */
195a78e
+#undef USE_LINUX_IP_LOCAL_PORT_RANGE
195a78e
+
195a78e
 /* Define if you want to use internal select based events */
195a78e
 #undef USE_MINI_EVENT
195a78e
 
195a78e
diff --git a/configure b/configure
195a78e
index c91e8a3..826dce9 100755
195a78e
--- a/configure
195a78e
+++ b/configure
195a78e
@@ -898,6 +898,7 @@ enable_ipsecmod
195a78e
 enable_ipset
195a78e
 with_libmnl
195a78e
 enable_explicit_port_randomisation
195a78e
+enable_linux_ip_local_port_range
195a78e
 with_libunbound_only
195a78e
 '
195a78e
       ac_precious_vars='build_alias
195a78e
@@ -1590,6 +1591,16 @@ Optional Features:
195a78e
   --disable-explicit-port-randomisation
195a78e
                           disable explicit source port randomisation and rely
195a78e
                           on the kernel to provide random source ports
195a78e
+  --enable-linux-ip-local-port-range
195a78e
+                          WARNING! This is only for the libunbound on Linux
195a78e
+                          and does not affect unbound resolving daemon itself.
195a78e
+                          This may severely limit the number of available
195a78e
+                          outgoing ports and thus decrease randomness. Use
195a78e
+                          this option only when the target system restricts
195a78e
+                          the use of non-ephemeral ports. (e.g. some of
195a78e
+                          SELinux enabled distributions) Enable this option to
195a78e
+                          use /proc/sys/net/ipv4/ip_local_port_range as a
195a78e
+                          default outgoing port range
195a78e
 
195a78e
 Optional Packages:
195a78e
   --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
195a78e
@@ -4202,6 +4213,13 @@ else
195a78e
 	else on_mingw="no"; fi
195a78e
 fi
195a78e
 
195a78e
+# are we on Linux?
195a78e
+if uname -s 2>&1 | grep -i linux >/dev/null; then on_linux="yes"
195a78e
+else
195a78e
+	if echo $host $target | grep linux >/dev/null; then on_linux="yes"
195a78e
+	else on_linux="no"; fi
195a78e
+fi
195a78e
+
195a78e
 #
195a78e
 # Determine configuration file
195a78e
 # the eval is to evaluate shell expansion twice
195a78e
@@ -21588,6 +21606,23 @@ $as_echo "#define DISABLE_EXPLICIT_PORT_RANDOMISATION 1" >>confdefs.h
195a78e
 		;;
195a78e
 esac
195a78e
 
195a78e
+if test $on_linux = "yes"; then
195a78e
+	# Check whether --enable-linux-ip-local-port-range was given.
195a78e
+if test "${enable_linux_ip_local_port_range+set}" = set; then :
195a78e
+  enableval=$enable_linux_ip_local_port_range;
195a78e
+fi
195a78e
+
195a78e
+	case "$enable_linux_ip_local_port_range" in
195a78e
+		yes)
195a78e
+
195a78e
+$as_echo "#define USE_LINUX_IP_LOCAL_PORT_RANGE 1" >>confdefs.h
195a78e
+
195a78e
+			;;
195a78e
+		no|*)
195a78e
+			;;
195a78e
+	esac
195a78e
+fi
195a78e
+
195a78e
 
195a78e
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking if ${MAKE:-make} supports $< with implicit rule in scope" >&5
195a78e
 $as_echo_n "checking if ${MAKE:-make} supports $< with implicit rule in scope... " >&6; }
195a78e
diff --git a/configure.ac b/configure.ac
195a78e
index 2d88048..1207047 100644
195a78e
--- a/configure.ac
195a78e
+++ b/configure.ac
195a78e
@@ -152,6 +152,13 @@ else
195a78e
 	else on_mingw="no"; fi
195a78e
 fi
195a78e
 
195a78e
+# are we on Linux?
195a78e
+if uname -s 2>&1 | grep -i linux >/dev/null; then on_linux="yes"
195a78e
+else
195a78e
+	if echo $host $target | grep linux >/dev/null; then on_linux="yes"
195a78e
+	else on_linux="no"; fi
195a78e
+fi
195a78e
+
195a78e
 #
195a78e
 # Determine configuration file
195a78e
 # the eval is to evaluate shell expansion twice
195a78e
@@ -1847,6 +1854,17 @@ case "$enable_explicit_port_randomisation" in
195a78e
 		;;
195a78e
 esac
195a78e
 
195a78e
+if test $on_linux = "yes"; then
195a78e
+	AC_ARG_ENABLE(linux-ip-local-port-range, AC_HELP_STRING([--enable-linux-ip-local-port-range], [WARNING! This is only for the libunbound on Linux and does not affect unbound resolving daemon itself. This may severely limit the number of available outgoing ports and thus decrease randomness. Use this option only when the target system restricts the use of non-ephemeral ports. (e.g. some of SELinux enabled distributions) Enable this option to use /proc/sys/net/ipv4/ip_local_port_range as a default outgoing port range]))
195a78e
+	case "$enable_linux_ip_local_port_range" in
195a78e
+		yes)
195a78e
+			AC_DEFINE([USE_LINUX_IP_LOCAL_PORT_RANGE], [1], [WARNING! This is only for the libunbound on Linux and does not affect unbound resolving daemon itself. This may severely limit the number of available outgoing ports and thus decrease randomness. Define this only when the target system restricts (e.g. some of SELinux enabled distributions) the use of non-ephemeral ports. Define this to enable use of /proc/sys/net/ipv4/ip_local_port_range as a default outgoing port range.])
195a78e
+			;;
195a78e
+		no|*)
195a78e
+			;;
195a78e
+	esac
195a78e
+fi
195a78e
+
195a78e
 
195a78e
 AC_MSG_CHECKING([if ${MAKE:-make} supports $< with implicit rule in scope])
195a78e
 # on openBSD, the implicit rule make $< work.
195a78e
diff --git a/libunbound/context.c b/libunbound/context.c
195a78e
index cff2831..48d76d9 100644
195a78e
--- a/libunbound/context.c
195a78e
+++ b/libunbound/context.c
195a78e
@@ -69,6 +69,7 @@ context_finalize(struct ub_ctx* ctx)
195a78e
 	} else {
195a78e
 		log_init(cfg->logfile, cfg->use_syslog, NULL);
195a78e
 	}
195a78e
+	cfg_apply_local_port_policy(cfg, 65536);
195a78e
 	config_apply(cfg);
195a78e
 	if(!modstack_setup(&ctx->mods, cfg->module_conf, ctx->env))
195a78e
 		return UB_INITFAIL;
195a78e
diff --git a/util/config_file.c b/util/config_file.c
195a78e
index 4d87dee..6b90e48 100644
195a78e
--- a/util/config_file.c
195a78e
+++ b/util/config_file.c
195a78e
@@ -1681,6 +1681,37 @@ int cfg_condense_ports(struct config_file* cfg, int** avail)
195a78e
 	return num;
195a78e
 }
195a78e
 
195a78e
+void cfg_apply_local_port_policy(struct config_file* cfg, int num) {
195a78e
+(void)cfg;
195a78e
+(void)num;
195a78e
+#ifdef USE_LINUX_IP_LOCAL_PORT_RANGE
195a78e
+	{
195a78e
+		int i = 0;
195a78e
+		FILE* range_fd;
195a78e
+		if ((range_fd = fopen(LINUX_IP_LOCAL_PORT_RANGE_PATH, "r")) != NULL) {
195a78e
+			int min_port = 0;
195a78e
+			int max_port = num - 1;
195a78e
+			if (fscanf(range_fd, "%d %d", &min_port, &max_port) == 2) {
195a78e
+				for(i=0; i
195a78e
+					cfg->outgoing_avail_ports[i] = 0;
195a78e
+				}
195a78e
+				for(i=max_port+1; i
195a78e
+					cfg->outgoing_avail_ports[i] = 0;
195a78e
+				}
195a78e
+			} else {
195a78e
+				log_err("unexpected port range in %s",
195a78e
+						LINUX_IP_LOCAL_PORT_RANGE_PATH);
195a78e
+			}
195a78e
+			fclose(range_fd);
195a78e
+		} else {
195a78e
+			log_warn("failed to read from file: %s (%s)",
195a78e
+					LINUX_IP_LOCAL_PORT_RANGE_PATH,
195a78e
+					strerror(errno));
195a78e
+		}
195a78e
+	}
195a78e
+#endif
195a78e
+}
195a78e
+
195a78e
 /** print error with file and line number */
195a78e
 static void ub_c_error_va_list(const char *fmt, va_list args)
195a78e
 {
195a78e
diff --git a/util/config_file.h b/util/config_file.h
195a78e
index 7cf27cc..d091ef7 100644
195a78e
--- a/util/config_file.h
195a78e
+++ b/util/config_file.h
195a78e
@@ -1172,6 +1172,13 @@ int cfg_mark_ports(const char* str, int allow, int* avail, int num);
195a78e
  */
195a78e
 int cfg_condense_ports(struct config_file* cfg, int** avail);
195a78e
 
195a78e
+/**
195a78e
+ * Apply system specific port range policy.
195a78e
+ * @param cfg: config file.
195a78e
+ * @param num: size of the array (65536).
195a78e
+ */
195a78e
+void cfg_apply_local_port_policy(struct config_file* cfg, int num);
195a78e
+
195a78e
 /**
195a78e
  * Scan ports available
195a78e
  * @param avail: the array from cfg.
195a78e
@@ -1301,5 +1308,9 @@ void w_config_adjust_directory(struct config_file* cfg);
195a78e
 /** debug option for unit tests. */
195a78e
 extern int fake_dsa, fake_sha1;
195a78e
 
195a78e
+#ifdef USE_LINUX_IP_LOCAL_PORT_RANGE
195a78e
+#define LINUX_IP_LOCAL_PORT_RANGE_PATH "/proc/sys/net/ipv4/ip_local_port_range"
195a78e
+#endif
195a78e
+
195a78e
 #endif /* UTIL_CONFIG_FILE_H */
195a78e