Blame SOURCES/dnsmasq-2.81-tag-filtering-of-dhcp-host-directives.patch

8e0267
From dd04a0d90d2fca66b5f91952ae7286c5de1714f1 Mon Sep 17 00:00:00 2001
8e0267
From: Simon Kelley <simon@thekelleys.org.uk>
8e0267
Date: Fri, 7 Feb 2020 21:05:54 +0000
8e0267
Subject: [PATCH] Add tag filtering of dhcp-host directives.
8e0267
8e0267
(cherry picked from commit 52ec7836139e7a11374971905e5ac0d2d02e32c0)
8e0267
8e0267
Conflicts:
8e0267
	CHANGELOG
8e0267
	src/rfc3315.c
8e0267
---
8e0267
 man/dnsmasq.8     |  5 ++++-
8e0267
 src/dhcp-common.c | 42 ++++++++++++++++++++++++++++++++----------
8e0267
 src/dnsmasq.h     |  4 +++-
8e0267
 src/lease.c       |  2 +-
8e0267
 src/option.c      | 14 ++++++--------
8e0267
 src/rfc2131.c     |  6 +++---
8e0267
 src/rfc3315.c     | 49 ++++++++++++++++++++++---------------------------
8e0267
 7 files changed, 71 insertions(+), 51 deletions(-)
8e0267
8e0267
diff --git a/man/dnsmasq.8 b/man/dnsmasq.8
8e0267
index 2c9d9f6..a59b06f 100644
8e0267
--- a/man/dnsmasq.8
8e0267
+++ b/man/dnsmasq.8
8e0267
@@ -953,7 +953,7 @@ is also included, as described in RFC-3775 section 7.3.
8e0267
 tells dnsmasq to advertise the prefix without the on-link (aka L) bit set.
8e0267
 
8e0267
 .TP
8e0267
-.B \-G, --dhcp-host=[<hwaddr>][,id:<client_id>|*][,set:<tag>][,<ipaddr>][,<hostname>][,<lease_time>][,ignore]
8e0267
+.B \-G, --dhcp-host=[<hwaddr>][,id:<client_id>|*][,set:<tag>][tag:<tag>][,<ipaddr>][,<hostname>][,<lease_time>][,ignore]
8e0267
 Specify per host parameters for the DHCP server. This allows a machine
8e0267
 with a particular hardware address to be always allocated the same
8e0267
 hostname, IP address and lease time. A hostname specified like this
8e0267
@@ -1038,6 +1038,9 @@ ignore requests from unknown machines using
8e0267
 .B --dhcp-ignore=tag:!known
8e0267
 If the host matches only a dhcp-host directive which cannot
8e0267
 be used because it specifies an address on different subnet, the tag "known-othernet" is set.
8e0267
+
8e0267
+The tag:<tag> construct filters which dhcp-host directives are used. Tagged directives are used in preference to untagged ones.
8e0267
+
8e0267
 Ethernet addresses (but not client-ids) may have
8e0267
 wildcard bytes, so for example 
8e0267
 .B --dhcp-host=00:20:e0:3b:13:*,ignore 
8e0267
diff --git a/src/dhcp-common.c b/src/dhcp-common.c
8e0267
index 5d437dd..71e9e5b 100644
8e0267
--- a/src/dhcp-common.c
8e0267
+++ b/src/dhcp-common.c
8e0267
@@ -304,11 +304,12 @@ static int is_config_in_context(struct dhcp_context *context, struct dhcp_config
8e0267
   return 0;
8e0267
 }
8e0267
 
8e0267
-struct dhcp_config *find_config(struct dhcp_config *configs,
8e0267
-				struct dhcp_context *context,
8e0267
-				unsigned char *clid, int clid_len,
8e0267
-				unsigned char *hwaddr, int hw_len, 
8e0267
-				int hw_type, char *hostname)
8e0267
+static struct dhcp_config *find_config_match(struct dhcp_config *configs,
8e0267
+					     struct dhcp_context *context,
8e0267
+					     unsigned char *clid, int clid_len,
8e0267
+					     unsigned char *hwaddr, int hw_len, 
8e0267
+					     int hw_type, char *hostname,
8e0267
+					     struct dhcp_netid *tags, int tag_not_needed)
8e0267
 {
8e0267
   int count, new;
8e0267
   struct dhcp_config *config, *candidate; 
8e0267
@@ -320,7 +321,9 @@ struct dhcp_config *find_config(struct dhcp_config *configs,
8e0267
 	{
8e0267
 	  if (config->clid_len == clid_len && 
8e0267
 	      memcmp(config->clid, clid, clid_len) == 0 &&
8e0267
-	      is_config_in_context(context, config))
8e0267
+	      is_config_in_context(context, config) &&
8e0267
+	      match_netid(config->filter, tags, tag_not_needed))
8e0267
+	    
8e0267
 	    return config;
8e0267
 	  
8e0267
 	  /* dhcpcd prefixes ASCII client IDs by zero which is wrong, but we try and
8e0267
@@ -328,7 +331,8 @@ struct dhcp_config *find_config(struct dhcp_config *configs,
8e0267
 	     see lease_update_from_configs() */
8e0267
 	  if ((!context || !(context->flags & CONTEXT_V6)) && *clid == 0 && config->clid_len == clid_len-1  &&
8e0267
 	      memcmp(config->clid, clid+1, clid_len-1) == 0 &&
8e0267
-	      is_config_in_context(context, config))
8e0267
+	      is_config_in_context(context, config) &&
8e0267
+	      match_netid(config->filter, tags, tag_not_needed))
8e0267
 	    return config;
8e0267
 	}
8e0267
   
8e0267
@@ -336,14 +340,16 @@ struct dhcp_config *find_config(struct dhcp_config *configs,
8e0267
   if (hwaddr)
8e0267
     for (config = configs; config; config = config->next)
8e0267
       if (config_has_mac(config, hwaddr, hw_len, hw_type) &&
8e0267
-	  is_config_in_context(context, config))
8e0267
+	  is_config_in_context(context, config) &&
8e0267
+	  match_netid(config->filter, tags, tag_not_needed))
8e0267
 	return config;
8e0267
   
8e0267
   if (hostname && context)
8e0267
     for (config = configs; config; config = config->next)
8e0267
       if ((config->flags & CONFIG_NAME) && 
8e0267
 	  hostname_isequal(config->hostname, hostname) &&
8e0267
-	  is_config_in_context(context, config))
8e0267
+	  is_config_in_context(context, config) &&
8e0267
+	  match_netid(config->filter, tags, tag_not_needed))
8e0267
 	return config;
8e0267
 
8e0267
   
8e0267
@@ -352,7 +358,8 @@ struct dhcp_config *find_config(struct dhcp_config *configs,
8e0267
 
8e0267
   /* use match with fewest wildcard octets */
8e0267
   for (candidate = NULL, count = 0, config = configs; config; config = config->next)
8e0267
-    if (is_config_in_context(context, config))
8e0267
+    if (is_config_in_context(context, config) &&
8e0267
+	match_netid(config->filter, tags, tag_not_needed))
8e0267
       for (conf_addr = config->hwaddr; conf_addr; conf_addr = conf_addr->next)
8e0267
 	if (conf_addr->wildcard_mask != 0 &&
8e0267
 	    conf_addr->hwaddr_len == hw_len &&	
8e0267
@@ -366,6 +373,21 @@ struct dhcp_config *find_config(struct dhcp_config *configs,
8e0267
   return candidate;
8e0267
 }
8e0267
 
8e0267
+/* Find tagged configs first. */
8e0267
+struct dhcp_config *find_config(struct dhcp_config *configs,
8e0267
+				struct dhcp_context *context,
8e0267
+				unsigned char *clid, int clid_len,
8e0267
+				unsigned char *hwaddr, int hw_len, 
8e0267
+				int hw_type, char *hostname, struct dhcp_netid *tags)
8e0267
+{
8e0267
+  struct dhcp_config *ret = find_config_match(configs, context, clid, clid_len, hwaddr, hw_len, hw_type, hostname, tags, 0);
8e0267
+
8e0267
+  if (!ret)
8e0267
+    ret = find_config_match(configs, context, clid, clid_len, hwaddr, hw_len, hw_type, hostname, tags, 1);
8e0267
+
8e0267
+  return ret;
8e0267
+}
8e0267
+
8e0267
 void dhcp_update_configs(struct dhcp_config *configs)
8e0267
 {
8e0267
   /* Some people like to keep all static IP addresses in /etc/hosts.
8e0267
diff --git a/src/dnsmasq.h b/src/dnsmasq.h
8e0267
index 9437226..055a0d1 100644
8e0267
--- a/src/dnsmasq.h
8e0267
+++ b/src/dnsmasq.h
8e0267
@@ -749,6 +749,7 @@ struct dhcp_config {
8e0267
   unsigned char *clid;   /* clientid */
8e0267
   char *hostname, *domain;
8e0267
   struct dhcp_netid_list *netid;
8e0267
+  struct dhcp_netid *filter;
8e0267
 #ifdef HAVE_DHCP6
8e0267
   struct addrlist *addr6;
8e0267
 #endif
8e0267
@@ -1514,7 +1515,8 @@ struct dhcp_config *find_config(struct dhcp_config *configs,
8e0267
 				struct dhcp_context *context,
8e0267
 				unsigned char *clid, int clid_len,
8e0267
 				unsigned char *hwaddr, int hw_len, 
8e0267
-				int hw_type, char *hostname);
8e0267
+				int hw_type, char *hostname,
8e0267
+				struct dhcp_netid *filter);
8e0267
 int config_has_mac(struct dhcp_config *config, unsigned char *hwaddr, int len, int type);
8e0267
 #ifdef HAVE_LINUX_NETWORK
8e0267
 char *whichdevice(void);
8e0267
diff --git a/src/lease.c b/src/lease.c
8e0267
index 5c33df7..00c82f6 100644
8e0267
--- a/src/lease.c
8e0267
+++ b/src/lease.c
8e0267
@@ -222,7 +222,7 @@ void lease_update_from_configs(void)
8e0267
     if (lease->flags & (LEASE_TA | LEASE_NA))
8e0267
       continue;
8e0267
     else if ((config = find_config(daemon->dhcp_conf, NULL, lease->clid, lease->clid_len, 
8e0267
-				   lease->hwaddr, lease->hwaddr_len, lease->hwaddr_type, NULL)) && 
8e0267
+				   lease->hwaddr, lease->hwaddr_len, lease->hwaddr_type, NULL, NULL)) && 
8e0267
 	     (config->flags & CONFIG_NAME) &&
8e0267
 	     (!(config->flags & CONFIG_ADDR) || config->addr.s_addr == lease->addr.s_addr))
8e0267
       lease_set_hostname(lease, config->hostname, 1, get_domain(lease->addr), NULL);
8e0267
diff --git a/src/option.c b/src/option.c
8e0267
index ea70ee3..88cd2ab 100644
8e0267
--- a/src/option.c
8e0267
+++ b/src/option.c
8e0267
@@ -953,8 +953,7 @@ static char *set_prefix(char *arg)
8e0267
    return arg;
8e0267
 }
8e0267
 
8e0267
-static struct dhcp_netid *
8e0267
-dhcp_netid_create(const char *net, struct dhcp_netid *next)
8e0267
+static struct dhcp_netid *dhcp_netid_create(const char *net, struct dhcp_netid *next)
8e0267
 {
8e0267
   struct dhcp_netid *tt;
8e0267
   tt = opt_malloc(sizeof (struct dhcp_netid));
8e0267
@@ -1019,7 +1018,8 @@ static void dhcp_config_free(struct dhcp_config *config)
8e0267
         }
8e0267
       
8e0267
       dhcp_netid_list_free(config->netid);
8e0267
-
8e0267
+      dhcp_netid_free(config->filter);
8e0267
+      
8e0267
       if (config->flags & CONFIG_CLID)
8e0267
         free(config->clid);
8e0267
 
8e0267
@@ -3167,6 +3167,7 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
8e0267
 	new->flags = (option == LOPT_BANK) ? CONFIG_BANK : 0;
8e0267
 	new->hwaddr = NULL;
8e0267
 	new->netid = NULL;
8e0267
+	new->filter = NULL;
8e0267
 	new->clid = NULL;
8e0267
 	new->addr6 = NULL;
8e0267
 
8e0267
@@ -3215,11 +3216,8 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
8e0267
 		    newlist->list = dhcp_netid_create(arg+4, NULL);
8e0267
 		  }
8e0267
 		else if (strstr(arg, "tag:") == arg)
8e0267
-		  {
8e0267
-		    
8e0267
-		    dhcp_config_free(new);
8e0267
-		    ret_err(_("cannot match tags in --dhcp-host"));
8e0267
-		  }
8e0267
+		  new->filter = dhcp_netid_create(arg+4, new->filter);
8e0267
+		  
8e0267
 #ifdef HAVE_DHCP6
8e0267
 		else if (arg[0] == '[' && arg[strlen(arg)-1] == ']')
8e0267
 		  {
8e0267
diff --git a/src/rfc2131.c b/src/rfc2131.c
8e0267
index 997575a..a741f9f 100644
8e0267
--- a/src/rfc2131.c
8e0267
+++ b/src/rfc2131.c
8e0267
@@ -479,7 +479,7 @@ size_t dhcp_reply(struct dhcp_context *context, char *iface_name, int int_index,
8e0267
   mess->op = BOOTREPLY;
8e0267
   
8e0267
   config = find_config(daemon->dhcp_conf, context, clid, clid_len, 
8e0267
-		       mess->chaddr, mess->hlen, mess->htype, NULL);
8e0267
+		       mess->chaddr, mess->hlen, mess->htype, NULL, run_tag_if(netid));
8e0267
 
8e0267
   /* set "known" tag for known hosts */
8e0267
   if (config)
8e0267
@@ -489,7 +489,7 @@ size_t dhcp_reply(struct dhcp_context *context, char *iface_name, int int_index,
8e0267
       netid = &known_id;
8e0267
     }
8e0267
   else if (find_config(daemon->dhcp_conf, NULL, clid, clid_len, 
8e0267
-		       mess->chaddr, mess->hlen, mess->htype, NULL))
8e0267
+		       mess->chaddr, mess->hlen, mess->htype, NULL, run_tag_if(netid)))
8e0267
     {
8e0267
       known_id.net = "known-othernet";
8e0267
       known_id.next = netid;
8e0267
@@ -725,7 +725,7 @@ size_t dhcp_reply(struct dhcp_context *context, char *iface_name, int int_index,
8e0267
 		 to avoid impersonation by name. */
8e0267
 	      struct dhcp_config *new = find_config(daemon->dhcp_conf, context, NULL, 0,
8e0267
 						    mess->chaddr, mess->hlen, 
8e0267
-						    mess->htype, hostname);
8e0267
+						    mess->htype, hostname, run_tag_if(netid));
8e0267
 	      if (new && !have_config(new, CONFIG_CLID) && !new->hwaddr)
8e0267
 		{
8e0267
 		  config = new;
8e0267
diff --git a/src/rfc3315.c b/src/rfc3315.c
8e0267
index ee58b57..a0067e9 100644
8e0267
--- a/src/rfc3315.c
8e0267
+++ b/src/rfc3315.c
8e0267
@@ -486,35 +486,29 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
8e0267
 	 }
8e0267
     }	 
8e0267
   
8e0267
-  if (state->clid)
8e0267
+  if (state->clid &&
8e0267
+      (config = find_config(daemon->dhcp_conf, state->context, state->clid, state->clid_len,
8e0267
+			    state->mac, state->mac_len, state->mac_type, NULL, run_tag_if(state->tags))) &&
8e0267
+      have_config(config, CONFIG_NAME))
8e0267
     {
8e0267
-      config = find_config(daemon->dhcp_conf, state->context, state->clid, state->clid_len, state->mac, state->mac_len, state->mac_type, NULL);
8e0267
-      
8e0267
-      if (have_config(config, CONFIG_NAME))
8e0267
-	{
8e0267
-	  state->hostname = config->hostname;
8e0267
-	  state->domain = config->domain;
8e0267
-	  state->hostname_auth = 1;
8e0267
-	}
8e0267
-      else if (state->client_hostname)
8e0267
-	{
8e0267
-	  state->domain = strip_hostname(state->client_hostname);
8e0267
+      state->hostname = config->hostname;
8e0267
+      state->domain = config->domain;
8e0267
+      state->hostname_auth = 1;
8e0267
+    }
8e0267
+  else if (state->client_hostname)
8e0267
+    {
8e0267
+      state->domain = strip_hostname(state->client_hostname);
8e0267
 	  
8e0267
-	  if (strlen(state->client_hostname) != 0)
8e0267
-	    {
8e0267
-	      state->hostname = state->client_hostname;
8e0267
-	      if (!config)
8e0267
-		{
8e0267
-		  /* Search again now we have a hostname. 
8e0267
-		     Only accept configs without CLID here, (it won't match)
8e0267
-		     to avoid impersonation by name. */
8e0267
-		  struct dhcp_config *new = find_config(daemon->dhcp_conf, state->context, NULL, 0, NULL, 0, 0, state->hostname);
8e0267
-		  if (new && !have_config(new, CONFIG_CLID) && !new->hwaddr)
8e0267
-		    config = new;
8e0267
-		}
8e0267
-	    }
8e0267
+      if (strlen(state->client_hostname) != 0)
8e0267
+	{
8e0267
+	  /* Search again now we have a hostname. 
8e0267
+	     Only accept configs without CLID here, (it won't match)
8e0267
+	     to avoid impersonation by name. */
8e0267
+	  struct dhcp_config *new = find_config(daemon->dhcp_conf, state->context, NULL, 0, NULL, 0, 0, state->hostname, run_tag_if(state->tags));
8e0267
+	  if (new && !have_config(new, CONFIG_CLID) && !new->hwaddr)
8e0267
+	    config = new;
8e0267
 	}
8e0267
-    }
8e0267
+     }
8e0267
 
8e0267
   if (config)
8e0267
     {
8e0267
@@ -535,7 +529,8 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
8e0267
 	ignore = 1;
8e0267
     }
8e0267
   else if (state->clid &&
8e0267
-	   find_config(daemon->dhcp_conf, NULL, state->clid, state->clid_len, state->mac, state->mac_len, state->mac_type, NULL))
8e0267
+	   find_config(daemon->dhcp_conf, NULL, state->clid, state->clid_len,
8e0267
+		       state->mac, state->mac_len, state->mac_type, NULL, run_tag_if(state->tags)))
8e0267
     {
8e0267
       known_id.net = "known-othernet";
8e0267
       known_id.next = state->tags;
8e0267
-- 
8e0267
1.8.3.1
8e0267