Blame SOURCES/0012-RFC-3442-Classless-Static-Route-Option-for-DHCPv4-51.patch

df4638
From 01b1dcfef129a4eccfaf0f63a216774019f82dca Mon Sep 17 00:00:00 2001
df4638
From: Pavel Zhukov <pzhukov@redhat.com>
df4638
Date: Thu, 21 Feb 2019 10:32:35 +0100
df4638
Subject: [PATCH 12/26] RFC 3442 - Classless Static Route Option for DHCPv4
df4638
 (#516325)
df4638
Cc: pzhukov@redhat.com
df4638
df4638
(Submitted to dhcp-bugs@isc.org - [ISC-Bugs #24572])
df4638
---
df4638
 client/clparse.c      | 13 ++++++++++--
df4638
 common/dhcp-options.5 | 43 +++++++++++++++++++++++++++++++++++++++
df4638
 common/inet.c         | 54 +++++++++++++++++++++++++++++++++++++++++++++++++
df4638
 common/options.c      | 49 +++++++++++++++++++++++++++++++++++++++++++-
df4638
 common/parse.c        | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++-
df4638
 common/tables.c       |  2 ++
df4638
 includes/dhcp.h       |  1 +
df4638
 includes/dhcpd.h      |  2 ++
df4638
 includes/dhctoken.h   |  5 +++--
df4638
 9 files changed, 219 insertions(+), 6 deletions(-)
df4638
df4638
diff --git a/client/clparse.c b/client/clparse.c
df4638
index 44387ed..862e4f9 100644
df4638
--- a/client/clparse.c
df4638
+++ b/client/clparse.c
df4638
@@ -31,7 +31,7 @@
df4638
 
df4638
 struct client_config top_level_config;
df4638
 
df4638
-#define NUM_DEFAULT_REQUESTED_OPTS	14
df4638
+#define NUM_DEFAULT_REQUESTED_OPTS	15
df4638
 /* There can be 2 extra requested options for DHCPv4-over-DHCPv6. */
df4638
 struct option *default_requested_options[NUM_DEFAULT_REQUESTED_OPTS + 2 + 1];
df4638
 
df4638
@@ -87,7 +87,11 @@ isc_result_t read_client_conf ()
df4638
 				dhcp_universe.code_hash, &code, 0, MDL);
df4638
 
df4638
 	/* 4 */
df4638
-	code = DHO_ROUTERS;
df4638
+	/* The Classless Static Routes option code MUST appear in the parameter
df4638
+     * request list prior to both the Router option code and the Static
df4638
+     * Routes option code, if present. (RFC3442)
df4638
+	 */
df4638
+	code = DHO_CLASSLESS_STATIC_ROUTES;
df4638
 	option_code_hash_lookup(&default_requested_options[3],
df4638
 				dhcp_universe.code_hash, &code, 0, MDL);
df4638
 
df4638
@@ -141,6 +145,11 @@ isc_result_t read_client_conf ()
df4638
 	option_code_hash_lookup(&default_requested_options[13],
df4638
 				dhcp_universe.code_hash, &code, 0, MDL);
df4638
 
df4638
+	/* 15 */
df4638
+	code = DHO_ROUTERS;
df4638
+	option_code_hash_lookup(&default_requested_options[14],
df4638
+				dhcp_universe.code_hash, &code, 0, MDL);
df4638
+
df4638
 	for (code = 0 ; code < NUM_DEFAULT_REQUESTED_OPTS ; code++) {
df4638
 		if (default_requested_options[code] == NULL)
df4638
 			log_fatal("Unable to find option definition for "
df4638
diff --git a/common/dhcp-options.5 b/common/dhcp-options.5
df4638
index d9e1197..2343b19 100644
df4638
--- a/common/dhcp-options.5
df4638
+++ b/common/dhcp-options.5
df4638
@@ -110,6 +110,26 @@ hexadecimal, separated by colons.  For example:
df4638
 or
df4638
   option dhcp-client-identifier 43:4c:49:45:54:2d:46:4f:4f;
df4638
 .fi
df4638
+.PP
df4638
+The
df4638
+.B destination-descriptor
df4638
+describe the IP subnet number and subnet mask
df4638
+of a particular destination using a compact encoding. This encoding
df4638
+consists of one octet describing the width of the subnet mask,
df4638
+followed by all the significant octets of the subnet number.
df4638
+The following table contains some examples of how various subnet
df4638
+number/mask combinations can be encoded:
df4638
+.nf
df4638
+.sp 1
df4638
+Subnet number   Subnet mask      Destination descriptor
df4638
+0               0                0
df4638
+10.0.0.0        255.0.0.0        8.10
df4638
+10.0.0.0        255.255.255.0    24.10.0.0
df4638
+10.17.0.0       255.255.0.0      16.10.17
df4638
+10.27.129.0     255.255.255.0    24.10.27.129
df4638
+10.229.0.128    255.255.255.128  25.10.229.0.128
df4638
+10.198.122.47   255.255.255.255  32.10.198.122.47
df4638
+.fi
df4638
 .SH SETTING OPTION VALUES USING EXPRESSIONS
df4638
 Sometimes it's helpful to be able to set the value of a DHCP option
df4638
 based on some value that the client has sent.  To do this, you can
df4638
@@ -1086,6 +1106,29 @@ dhclient-script will create routes:
df4638
 .RE
df4638
 .PP
df4638
 .nf
df4638
+.B option \fBclassless-static-routes\fR \fIdestination-descriptor ip-address\fR
df4638
+                            [\fB,\fR \fIdestination-descriptor ip-address\fR...]\fB;\fR
df4638
+.fi
df4638
+.RS 0.25i
df4638
+.PP
df4638
+This option (see RFC3442) specifies a list of classless static routes
df4638
+that the client should install in its routing cache.
df4638
+.PP
df4638
+This option can contain one or more static routes, each of which
df4638
+consists of a destination descriptor and the IP address of the router
df4638
+that should be used to reach that destination.
df4638
+.PP
df4638
+Many clients may not implement the Classless Static Routes option.
df4638
+DHCP server administrators should therefore configure their DHCP
df4638
+servers to send both a Router option and a Classless Static Routes
df4638
+option, and should specify the default router(s) both in the Router
df4638
+option and in the Classless Static Routes option.
df4638
+.PP
df4638
+If the DHCP server returns both a Classless Static Routes option and
df4638
+a Router option, the DHCP client ignores the Router option.
df4638
+.RE
df4638
+.PP
df4638
+.nf
df4638
 .B option \fBstreettalk-directory-assistance-server\fR \fIip-address\fR
df4638
                                            [\fB,\fR \fIip-address\fR...]\fB;\fR
df4638
 .fi
df4638
diff --git a/common/inet.c b/common/inet.c
df4638
index c4da73c..981fb92 100644
df4638
--- a/common/inet.c
df4638
+++ b/common/inet.c
df4638
@@ -519,6 +519,60 @@ free_iaddrcidrnetlist(struct iaddrcidrnetlist **result) {
df4638
 	return ISC_R_SUCCESS;
df4638
 }
df4638
 
df4638
+static const char *
df4638
+inet_ntopdd(const unsigned char *src, unsigned srclen, char *dst, size_t size)
df4638
+{
df4638
+	char tmp[sizeof("32.255.255.255.255")];
df4638
+	int len;
df4638
+
df4638
+	switch (srclen) {
df4638
+		case 2:
df4638
+			len = sprintf (tmp, "%u.%u", src[0], src[1]);
df4638
+			break;
df4638
+		case 3:
df4638
+			len = sprintf (tmp, "%u.%u.%u", src[0], src[1], src[2]);
df4638
+			break;
df4638
+		case 4:
df4638
+			len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
df4638
+			break;
df4638
+		case 5:
df4638
+			len = sprintf (tmp, "%u.%u.%u.%u.%u", src[0], src[1], src[2], src[3], src[4]);
df4638
+			break;
df4638
+		default:
df4638
+			return NULL;
df4638
+	}
df4638
+	if (len < 0)
df4638
+		return NULL;
df4638
+
df4638
+	if (len > size) {
df4638
+		errno = ENOSPC;
df4638
+		return NULL;
df4638
+	}
df4638
+
df4638
+	return strcpy (dst, tmp);
df4638
+}
df4638
+
df4638
+/* pdestdesc() turns an iaddr structure into a printable dest. descriptor */
df4638
+const char *
df4638
+pdestdesc(const struct iaddr addr) {
df4638
+	static char pbuf[sizeof("255.255.255.255.255")];
df4638
+
df4638
+	if (addr.len == 0) {
df4638
+		return "<null destination descriptor>";
df4638
+	}
df4638
+	if (addr.len == 1) {
df4638
+		return "0";
df4638
+	}
df4638
+	if ((addr.len >= 2) && (addr.len <= 5)) {
df4638
+		return inet_ntopdd(addr.iabuf, addr.len, pbuf, sizeof(pbuf));
df4638
+	}
df4638
+
df4638
+	log_fatal("pdestdesc():%s:%d: Invalid destination descriptor length %d.",
df4638
+		  MDL, addr.len);
df4638
+	/* quell compiler warnings */
df4638
+	return NULL;
df4638
+}
df4638
+
df4638
 /* piaddr() turns an iaddr structure into a printable address. */
df4638
 /* XXX: should use a const pointer rather than passing the structure */
df4638
 const char *
df4638
diff --git a/common/options.c b/common/options.c
df4638
index fc0e088..3034cf0 100644
df4638
--- a/common/options.c
df4638
+++ b/common/options.c
df4638
@@ -729,7 +729,11 @@ cons_options(struct packet *inpacket, struct dhcp_packet *outpacket,
df4638
 		 * packet.
df4638
 		 */
df4638
 		priority_list[priority_len++] = DHO_SUBNET_MASK;
df4638
-		priority_list[priority_len++] = DHO_ROUTERS;
df4638
+		if (lookup_option(&dhcp_universe, cfg_options,
df4638
+							DHO_CLASSLESS_STATIC_ROUTES))
df4638
+			priority_list[priority_len++] = DHO_CLASSLESS_STATIC_ROUTES;
df4638
+		else
df4638
+			priority_list[priority_len++] = DHO_ROUTERS;
df4638
 		priority_list[priority_len++] = DHO_DOMAIN_NAME_SERVERS;
df4638
 		priority_list[priority_len++] = DHO_HOST_NAME;
df4638
 		priority_list[priority_len++] = DHO_FQDN;
df4638
@@ -1804,6 +1808,7 @@ const char *pretty_print_option (option, data, len, emit_commas, emit_quotes)
df4638
 	unsigned long tval;
df4638
 	isc_boolean_t a_array = ISC_FALSE;
df4638
 	int len_used;
df4638
+	unsigned int octets = 0;
df4638
 
df4638
 	if (emit_commas)
df4638
 		comma = ',';
df4638
@@ -1812,6 +1817,7 @@ const char *pretty_print_option (option, data, len, emit_commas, emit_quotes)
df4638
 
df4638
 	memset (enumbuf, 0, sizeof enumbuf);
df4638
 
df4638
+	if (option->format[0] != 'R') { /* see explanation lower */
df4638
 	/* Figure out the size of the data. */
df4638
 	for (l = i = 0; option -> format [i]; i++, l++) {
df4638
 		if (l >= sizeof(fmtbuf) - 1)
df4638
@@ -2004,6 +2010,33 @@ const char *pretty_print_option (option, data, len, emit_commas, emit_quotes)
df4638
 	if (numhunk < 0)
df4638
 		numhunk = 1;
df4638
 
df4638
+	} else { /* option->format[i] == 'R') */
df4638
+		/* R (destination descriptor) has variable length.
df4638
+		 * We can find it only in classless static route option,
df4638
+		 * so we are for sure parsing classless static route option now.
df4638
+		 * We go through whole the option to check whether there are no
df4638
+		 * missing/extra bytes.
df4638
+		 * I didn't find out how to improve the existing code and that's the
df4638
+		 * reason for this separate 'else' where I do my own checkings.
df4638
+		 * I know it's little bit unsystematic, but it works.
df4638
+		 */
df4638
+		numhunk = 0;
df4638
+		numelem = 2; /* RI */
df4638
+		fmtbuf[0]='R'; fmtbuf[1]='I'; fmtbuf[2]=0;
df4638
+		for (i =0; i < len; i = i + octets + 5) {
df4638
+			if (data[i] > 32) { /* subnet mask width */
df4638
+				log_error ("wrong subnet mask width in destination descriptor");
df4638
+				break;
df4638
+			}
df4638
+			numhunk++;
df4638
+			octets = ((data[i]+7) / 8);
df4638
+		}
df4638
+		if (i != len) {
df4638
+			log_error ("classless static routes option has wrong size or "
df4638
+					   "there's some garbage in format");
df4638
+		}
df4638
+	}
df4638
+
df4638
 	/* Cycle through the array (or hunk) printing the data. */
df4638
 	for (i = 0; i < numhunk; i++) {
df4638
 		if ((a_array == ISC_TRUE) && (i != 0) && (numelem > 0)) {
df4638
@@ -2159,6 +2192,20 @@ const char *pretty_print_option (option, data, len, emit_commas, emit_quotes)
df4638
 				strcpy(op, piaddr(iaddr));
df4638
 				dp += 4;
df4638
 				break;
df4638
+
df4638
+			      case 'R':
df4638
+				if (dp[0] <= 32)
df4638
+					iaddr.len = (((dp[0]+7)/8)+1);
df4638
+				else {
df4638
+					log_error ("wrong subnet mask width in destination descriptor");
df4638
+					return "<error>";
df4638
+				}
df4638
+
df4638
+				memcpy(iaddr.iabuf, dp, iaddr.len);
df4638
+				strcpy(op, pdestdesc(iaddr));
df4638
+				dp += iaddr.len;
df4638
+				break;
df4638
+
df4638
 			      case '6':
df4638
 				iaddr.len = 16;
df4638
 				memcpy(iaddr.iabuf, dp, 16);
df4638
diff --git a/common/parse.c b/common/parse.c
df4638
index 3ac4ebf..f17bc0b 100644
df4638
--- a/common/parse.c
df4638
+++ b/common/parse.c
df4638
@@ -344,6 +344,39 @@ int parse_ip_addr (cfile, addr)
df4638
 	return 0;
df4638
 }	
df4638
 
df4638
+/*
df4638
+ * destination-descriptor :== NUMBER DOT NUMBER |
df4638
+ *                            NUMBER DOT NUMBER DOT NUMBER |
df4638
+ *                            NUMBER DOT NUMBER DOT NUMBER DOT NUMBER |
df4638
+ *                            NUMBER DOT NUMBER DOT NUMBER DOT NUMBER DOT NUMBER
df4638
+ */
df4638
+
df4638
+int parse_destination_descriptor (cfile, addr)
df4638
+	struct parse *cfile;
df4638
+	struct iaddr *addr;
df4638
+{
df4638
+		unsigned int mask_width, dest_dest_len;
df4638
+		addr -> len = 0;
df4638
+		if (parse_numeric_aggregate (cfile, addr -> iabuf,
df4638
+									 &addr -> len, DOT, 10, 8)) {
df4638
+			mask_width = (unsigned int)addr->iabuf[0];
df4638
+			dest_dest_len = (((mask_width+7)/8)+1);
df4638
+			if (mask_width > 32) {
df4638
+				parse_warn (cfile,
df4638
+				"subnet mask width (%u) greater than 32.", mask_width);
df4638
+			}
df4638
+			else if (dest_dest_len != addr->len) {
df4638
+				parse_warn (cfile,
df4638
+				"destination descriptor with subnet mask width %u "
df4638
+				"should have %u octets, but has %u octets.",
df4638
+				mask_width, dest_dest_len, addr->len);
df4638
+			}
df4638
+
df4638
+			return 1;
df4638
+		}
df4638
+		return 0;
df4638
+}
df4638
+
df4638
 /*
df4638
  * Return true if every character in the string is hexadecimal.
df4638
  */
df4638
@@ -724,8 +757,10 @@ unsigned char *parse_numeric_aggregate (cfile, buf,
df4638
 		if (count) {
df4638
 			token = peek_token (&val, (unsigned *)0, cfile);
df4638
 			if (token != separator) {
df4638
-				if (!*max)
df4638
+				if (!*max) {
df4638
+					*max = count;
df4638
 					break;
df4638
+				}
df4638
 				if (token != RBRACE && token != LBRACE)
df4638
 					token = next_token (&val,
df4638
 							    (unsigned *)0,
df4638
@@ -1672,6 +1707,9 @@ int parse_option_code_definition (cfile, option)
df4638
 	      case IP_ADDRESS:
df4638
 		type = 'I';
df4638
 		break;
df4638
+	      case DESTINATION_DESCRIPTOR:
df4638
+		type = 'R';
df4638
+		break;
df4638
 	      case IP6_ADDRESS:
df4638
 		type = '6';
df4638
 		break;
df4638
@@ -5101,6 +5139,15 @@ int parse_option_token (rv, cfile, fmt, expr, uniform, lookups)
df4638
 		}
df4638
 		break;
df4638
 
df4638
+	      case 'R': /* destination descriptor */
df4638
+		if (!parse_destination_descriptor (cfile, &addr)) {
df4638
+			return 0;
df4638
+		}
df4638
+		if (!make_const_data (&t, addr.iabuf, addr.len, 0, 1, MDL)) {
df4638
+			return 0;
df4638
+		}
df4638
+		break;
df4638
+
df4638
 	      case '6': /* IPv6 address. */
df4638
 		if (!parse_ip6_addr(cfile, &addr)) {
df4638
 			return 0;
df4638
@@ -5378,6 +5425,13 @@ int parse_option_decl (oc, cfile)
df4638
 					goto exit;
df4638
 				len = ip_addr.len;
df4638
 				dp = ip_addr.iabuf;
df4638
+				goto alloc;
df4638
+
df4638
+			      case 'R': /* destination descriptor */
df4638
+				if (!parse_destination_descriptor (cfile, &ip_addr))
df4638
+					goto exit;
df4638
+				len = ip_addr.len;
df4638
+				dp = ip_addr.iabuf;
df4638
 
df4638
 			      alloc:
df4638
 				if (hunkix + len > sizeof hunkbuf) {
df4638
diff --git a/common/tables.c b/common/tables.c
df4638
index d2294c0..f1be07d 100644
df4638
--- a/common/tables.c
df4638
+++ b/common/tables.c
df4638
@@ -45,6 +45,7 @@ HASH_FUNCTIONS (option_code, const unsigned *, struct option,
df4638
    Format codes:
df4638
 
df4638
    I - IPv4 address
df4638
+   R - destination descriptor (RFC3442)
df4638
    6 - IPv6 address
df4638
    l - 32-bit signed integer
df4638
    L - 32-bit unsigned integer
df4638
@@ -216,6 +217,7 @@ static struct option dhcp_options[] = {
df4638
 #endif
df4638
 	{ "subnet-selection", "I",		&dhcp_universe, 118, 1 },
df4638
 	{ "domain-search", "D",			&dhcp_universe, 119, 1 },
df4638
+	{ "classless-static-routes", "RIA",	&dhcp_universe, 121, 1 },
df4638
 	{ "vivco", "Evendor-class.",		&dhcp_universe, 124, 1 },
df4638
 	{ "vivso", "Evendor.",			&dhcp_universe, 125, 1 },
df4638
 #if 0
df4638
diff --git a/includes/dhcp.h b/includes/dhcp.h
df4638
index 0a74137..95bf539 100644
df4638
--- a/includes/dhcp.h
df4638
+++ b/includes/dhcp.h
df4638
@@ -158,6 +158,7 @@ struct dhcp_packet {
df4638
 #define DHO_ASSOCIATED_IP			92
df4638
 #define DHO_SUBNET_SELECTION			118 /* RFC3011! */
df4638
 #define DHO_DOMAIN_SEARCH			119 /* RFC3397 */
df4638
+#define DHO_CLASSLESS_STATIC_ROUTES		121 /* RFC3442 */
df4638
 #define DHO_VIVCO_SUBOPTIONS			124
df4638
 #define DHO_VIVSO_SUBOPTIONS			125
df4638
 
df4638
diff --git a/includes/dhcpd.h b/includes/dhcpd.h
df4638
index 3632a6b..2ac39ae 100644
df4638
--- a/includes/dhcpd.h
df4638
+++ b/includes/dhcpd.h
df4638
@@ -2951,6 +2951,7 @@ isc_result_t range2cidr(struct iaddrcidrnetlist **result,
df4638
 			const struct iaddr *lo, const struct iaddr *hi);
df4638
 isc_result_t free_iaddrcidrnetlist(struct iaddrcidrnetlist **result);
df4638
 const char *piaddr (struct iaddr);
df4638
+const char *pdestdesc (struct iaddr);
df4638
 char *piaddrmask(struct iaddr *, struct iaddr *);
df4638
 char *piaddrcidr(const struct iaddr *, unsigned int);
df4638
 u_int16_t validate_port(char *);
df4638
@@ -3169,6 +3170,7 @@ void parse_client_lease_declaration (struct parse *,
df4638
 int parse_option_decl (struct option_cache **, struct parse *);
df4638
 void parse_string_list (struct parse *, struct string_list **, int);
df4638
 int parse_ip_addr (struct parse *, struct iaddr *);
df4638
+int parse_destination_descriptor (struct parse *, struct iaddr *);
df4638
 int parse_ip_addr_with_subnet(struct parse *, struct iaddrmatch *);
df4638
 void parse_reject_statement (struct parse *, struct client_config *);
df4638
 
df4638
diff --git a/includes/dhctoken.h b/includes/dhctoken.h
df4638
index 7e7215a..b4d93ba 100644
df4638
--- a/includes/dhctoken.h
df4638
+++ b/includes/dhctoken.h
df4638
@@ -376,8 +376,9 @@ enum dhcp_token {
df4638
 	LEASE_ID_FORMAT = 676,
df4638
 	TOKEN_HEX = 677,
df4638
 	TOKEN_OCTAL = 678,
df4638
-	KEY_ALGORITHM = 679
df4638
-        BOOTP_BROADCAST_ALWAYS = 680
df4638
+	KEY_ALGORITHM = 679,
df4638
+        BOOTP_BROADCAST_ALWAYS = 680,
df4638
+	DESTINATION_DESCRIPTOR = 681
df4638
 };
df4638
 
df4638
 #define is_identifier(x)	((x) >= FIRST_TOKEN &&	\
df4638
-- 
df4638
2.14.5
df4638