e20e41
From 333856b1c1b032f937dd24d604f98cdb6dfe3d91 Mon Sep 17 00:00:00 2001
e20e41
From: Simon Kelley <simon@thekelleys.org.uk>
e20e41
Date: Mon, 29 Jan 2018 22:49:27 +0000
e20e41
Subject: [PATCH] Default min-port to 1024 to avoid reserved ports.
e20e41
e20e41
(cherry picked from commit baf553db0cdb50707ddab464fb3eff7786ea576c)
e20e41
---
e20e41
 man/dnsmasq.8      | 3 ++-
e20e41
 src/dns-protocol.h | 1 +
e20e41
 src/dnsmasq.c      | 3 ---
e20e41
 src/network.c      | 5 +----
e20e41
 src/option.c       | 1 +
e20e41
 5 files changed, 5 insertions(+), 8 deletions(-)
e20e41
e20e41
diff --git a/man/dnsmasq.8 b/man/dnsmasq.8
e20e41
index 1f1b048..9b7adde 100644
e20e41
--- a/man/dnsmasq.8
e20e41
+++ b/man/dnsmasq.8
e20e41
@@ -182,7 +182,8 @@ OS: this was the default behaviour in versions prior to 2.43.
e20e41
 Do not use ports less than that given as source for outbound DNS
e20e41
 queries. Dnsmasq picks random ports as source for outbound queries:
e20e41
 when this option is given, the ports used will always to larger
e20e41
-than that specified. Useful for systems behind firewalls. 
e20e41
+than that specified. Useful for systems behind firewalls. If not specified,
e20e41
+defaults to 1024.
e20e41
 .TP
e20e41
 .B --max-port=<port>
e20e41
 Use ports lower than that given as source for outbound DNS queries.
e20e41
diff --git a/src/dns-protocol.h b/src/dns-protocol.h
e20e41
index 75d8ffb..dd69b28 100644
e20e41
--- a/src/dns-protocol.h
e20e41
+++ b/src/dns-protocol.h
e20e41
@@ -16,6 +16,7 @@
e20e41
 
e20e41
 #define NAMESERVER_PORT 53
e20e41
 #define TFTP_PORT       69
e20e41
+#define MIN_PORT        1024           /* first non-reserved port */
e20e41
 #define MAX_PORT        65535u
e20e41
 
e20e41
 #define IN6ADDRSZ       16
e20e41
diff --git a/src/dnsmasq.c b/src/dnsmasq.c
e20e41
index 83631ef..ae1aa96 100644
e20e41
--- a/src/dnsmasq.c
e20e41
+++ b/src/dnsmasq.c
e20e41
@@ -220,9 +220,6 @@ int main (int argc, char **argv)
e20e41
     die(_("loop detection not available: set HAVE_LOOP in src/config.h"), NULL, EC_BADCONF);
e20e41
 #endif
e20e41
 
e20e41
-  if (daemon->max_port != MAX_PORT && daemon->min_port == 0)
e20e41
-    daemon->min_port = 1024u;
e20e41
-
e20e41
   if (daemon->max_port < daemon->min_port)
e20e41
     die(_("max_port cannot be smaller than min_port"), NULL, EC_BADCONF);
e20e41
 
e20e41
diff --git a/src/network.c b/src/network.c
e20e41
index fcd9d8d..d75f560 100644
e20e41
--- a/src/network.c
e20e41
+++ b/src/network.c
e20e41
@@ -1149,10 +1149,7 @@ int random_sock(int family)
e20e41
       if (fix_fd(fd))
e20e41
 	while(tries--)
e20e41
 	  {
e20e41
-	    unsigned short port = rand16();
e20e41
-	    
e20e41
-            if (daemon->min_port != 0 || daemon->max_port != MAX_PORT)
e20e41
-              port = htons(daemon->min_port + (port % ((unsigned short)ports_avail)));
e20e41
+	    unsigned short port = htons(daemon->min_port + (rand16() % ((unsigned short)ports_avail)));
e20e41
 	    
e20e41
 	    if (family == AF_INET) 
e20e41
 	      {
e20e41
diff --git a/src/option.c b/src/option.c
e20e41
index 3469f53..22846f6 100644
e20e41
--- a/src/option.c
e20e41
+++ b/src/option.c
e20e41
@@ -4521,6 +4521,7 @@ void read_opts(int argc, char **argv, char *compile_opts)
e20e41
   daemon->soa_retry = SOA_RETRY;
e20e41
   daemon->soa_expiry = SOA_EXPIRY;
e20e41
   daemon->max_port = MAX_PORT;
e20e41
+  daemon->min_port = MIN_PORT;
e20e41
 
e20e41
   add_txt("version.bind", "dnsmasq-" VERSION, 0 );
e20e41
   add_txt("authors.bind", "Simon Kelley", 0);
e20e41
-- 
e20e41
2.20.1
e20e41