Blame SOURCES/dhcp-rfc3442-classless-static-routes.patch

26a25c
diff -up dhcp-4.3.4/client/clparse.c.rfc3442 dhcp-4.3.4/client/clparse.c
26a25c
--- dhcp-4.3.4/client/clparse.c.rfc3442	2016-04-29 12:23:34.192032714 +0200
26a25c
+++ dhcp-4.3.4/client/clparse.c	2016-04-29 12:24:37.531016317 +0200
26a25c
@@ -31,7 +31,7 @@
26a25c
 
26a25c
 struct client_config top_level_config;
26a25c
 
26a25c
-#define NUM_DEFAULT_REQUESTED_OPTS	14
26a25c
+#define NUM_DEFAULT_REQUESTED_OPTS	15
26a25c
 /* There can be 2 extra requested options for DHCPv4-over-DHCPv6. */
26a25c
 struct option *default_requested_options[NUM_DEFAULT_REQUESTED_OPTS + 2 + 1];
26a25c
 
26a25c
@@ -87,7 +87,11 @@ isc_result_t read_client_conf ()
26a25c
 				dhcp_universe.code_hash, &code, 0, MDL);
26a25c
 
26a25c
 	/* 4 */
26a25c
-	code = DHO_ROUTERS;
26a25c
+	/* The Classless Static Routes option code MUST appear in the parameter
26a25c
+     * request list prior to both the Router option code and the Static
26a25c
+     * Routes option code, if present. (RFC3442)
26a25c
+	 */
26a25c
+	code = DHO_CLASSLESS_STATIC_ROUTES;
26a25c
 	option_code_hash_lookup(&default_requested_options[3],
26a25c
 				dhcp_universe.code_hash, &code, 0, MDL);
26a25c
 
26a25c
@@ -141,6 +145,11 @@ isc_result_t read_client_conf ()
26a25c
 	option_code_hash_lookup(&default_requested_options[13],
26a25c
 				dhcp_universe.code_hash, &code, 0, MDL);
26a25c
 
26a25c
+	/* 15 */
26a25c
+	code = DHO_ROUTERS;
26a25c
+	option_code_hash_lookup(&default_requested_options[14],
26a25c
+				dhcp_universe.code_hash, &code, 0, MDL);
26a25c
+
26a25c
 	for (code = 0 ; code < NUM_DEFAULT_REQUESTED_OPTS ; code++) {
26a25c
 		if (default_requested_options[code] == NULL)
26a25c
 			log_fatal("Unable to find option definition for "
26a25c
diff -up dhcp-4.3.4/common/dhcp-options.5.rfc3442 dhcp-4.3.4/common/dhcp-options.5
26a25c
--- dhcp-4.3.4/common/dhcp-options.5.rfc3442	2016-04-29 12:23:34.183032716 +0200
26a25c
+++ dhcp-4.3.4/common/dhcp-options.5	2016-04-29 12:23:34.237032703 +0200
26a25c
@@ -111,6 +111,26 @@ hexadecimal, separated by colons.  For e
26a25c
 or
26a25c
   option dhcp-client-identifier 43:4c:49:45:54:2d:46:4f:4f;
26a25c
 .fi
26a25c
+.PP
26a25c
+The
26a25c
+.B destination-descriptor
26a25c
+describe the IP subnet number and subnet mask
26a25c
+of a particular destination using a compact encoding. This encoding
26a25c
+consists of one octet describing the width of the subnet mask,
26a25c
+followed by all the significant octets of the subnet number.
26a25c
+The following table contains some examples of how various subnet
26a25c
+number/mask combinations can be encoded:
26a25c
+.nf
26a25c
+.sp 1
26a25c
+Subnet number   Subnet mask      Destination descriptor
26a25c
+0               0                0
26a25c
+10.0.0.0        255.0.0.0        8.10
26a25c
+10.0.0.0        255.255.255.0    24.10.0.0
26a25c
+10.17.0.0       255.255.0.0      16.10.17
26a25c
+10.27.129.0     255.255.255.0    24.10.27.129
26a25c
+10.229.0.128    255.255.255.128  25.10.229.0.128
26a25c
+10.198.122.47   255.255.255.255  32.10.198.122.47
26a25c
+.fi
26a25c
 .SH SETTING OPTION VALUES USING EXPRESSIONS
26a25c
 Sometimes it's helpful to be able to set the value of a DHCP option
26a25c
 based on some value that the client has sent.  To do this, you can
26a25c
@@ -1031,6 +1051,29 @@ dhclient-script will create routes:
26a25c
 .RE
26a25c
 .PP
26a25c
 .nf
26a25c
+.B option \fBclassless-static-routes\fR \fIdestination-descriptor ip-address\fR
26a25c
+                            [\fB,\fR \fIdestination-descriptor ip-address\fR...]\fB;\fR
26a25c
+.fi
26a25c
+.RS 0.25i
26a25c
+.PP
26a25c
+This option (see RFC3442) specifies a list of classless static routes
26a25c
+that the client should install in its routing cache.
26a25c
+.PP
26a25c
+This option can contain one or more static routes, each of which
26a25c
+consists of a destination descriptor and the IP address of the router
26a25c
+that should be used to reach that destination.
26a25c
+.PP
26a25c
+Many clients may not implement the Classless Static Routes option.
26a25c
+DHCP server administrators should therefore configure their DHCP
26a25c
+servers to send both a Router option and a Classless Static Routes
26a25c
+option, and should specify the default router(s) both in the Router
26a25c
+option and in the Classless Static Routes option.
26a25c
+.PP
26a25c
+If the DHCP server returns both a Classless Static Routes option and
26a25c
+a Router option, the DHCP client ignores the Router option.
26a25c
+.RE
26a25c
+.PP
26a25c
+.nf
26a25c
 .B option \fBstreettalk-directory-assistance-server\fR \fIip-address\fR
26a25c
                                            [\fB,\fR \fIip-address\fR...]\fB;\fR
26a25c
 .fi
26a25c
diff -up dhcp-4.3.4/common/inet.c.rfc3442 dhcp-4.3.4/common/inet.c
26a25c
--- dhcp-4.3.4/common/inet.c.rfc3442	2016-03-22 14:16:51.000000000 +0100
26a25c
+++ dhcp-4.3.4/common/inet.c	2016-04-29 12:23:34.237032703 +0200
26a25c
@@ -519,6 +519,60 @@ free_iaddrcidrnetlist(struct iaddrcidrne
26a25c
 	return ISC_R_SUCCESS;
26a25c
 }
26a25c
 
26a25c
+static const char *
26a25c
+inet_ntopdd(const unsigned char *src, unsigned srclen, char *dst, size_t size)
26a25c
+{
26a25c
+	char tmp[sizeof("32.255.255.255.255")];
26a25c
+	int len;
26a25c
+
26a25c
+	switch (srclen) {
26a25c
+		case 2:
26a25c
+			len = sprintf (tmp, "%u.%u", src[0], src[1]);
26a25c
+			break;
26a25c
+		case 3:
26a25c
+			len = sprintf (tmp, "%u.%u.%u", src[0], src[1], src[2]);
26a25c
+			break;
26a25c
+		case 4:
26a25c
+			len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
26a25c
+			break;
26a25c
+		case 5:
26a25c
+			len = sprintf (tmp, "%u.%u.%u.%u.%u", src[0], src[1], src[2], src[3], src[4]);
26a25c
+			break;
26a25c
+		default:
26a25c
+			return NULL;
26a25c
+	}
26a25c
+	if (len < 0)
26a25c
+		return NULL;
26a25c
+
26a25c
+	if (len > size) {
26a25c
+		errno = ENOSPC;
26a25c
+		return NULL;
26a25c
+	}
26a25c
+
26a25c
+	return strcpy (dst, tmp);
26a25c
+}
26a25c
+
26a25c
+/* pdestdesc() turns an iaddr structure into a printable dest. descriptor */
26a25c
+const char *
26a25c
+pdestdesc(const struct iaddr addr) {
26a25c
+	static char pbuf[sizeof("255.255.255.255.255")];
26a25c
+
26a25c
+	if (addr.len == 0) {
26a25c
+		return "<null destination descriptor>";
26a25c
+	}
26a25c
+	if (addr.len == 1) {
26a25c
+		return "0";
26a25c
+	}
26a25c
+	if ((addr.len >= 2) && (addr.len <= 5)) {
26a25c
+		return inet_ntopdd(addr.iabuf, addr.len, pbuf, sizeof(pbuf));
26a25c
+	}
26a25c
+
26a25c
+	log_fatal("pdestdesc():%s:%d: Invalid destination descriptor length %d.",
26a25c
+		  MDL, addr.len);
26a25c
+	/* quell compiler warnings */
26a25c
+	return NULL;
26a25c
+}
26a25c
+
26a25c
 /* piaddr() turns an iaddr structure into a printable address. */
26a25c
 /* XXX: should use a const pointer rather than passing the structure */
26a25c
 const char *
26a25c
diff -up dhcp-4.3.4/common/options.c.rfc3442 dhcp-4.3.4/common/options.c
26a25c
--- dhcp-4.3.4/common/options.c.rfc3442	2016-03-22 14:16:51.000000000 +0100
26a25c
+++ dhcp-4.3.4/common/options.c	2016-04-29 12:23:34.237032703 +0200
26a25c
@@ -713,7 +713,11 @@ cons_options(struct packet *inpacket, st
26a25c
 		 * packet.
26a25c
 		 */
26a25c
 		priority_list[priority_len++] = DHO_SUBNET_MASK;
26a25c
-		priority_list[priority_len++] = DHO_ROUTERS;
26a25c
+		if (lookup_option(&dhcp_universe, cfg_options,
26a25c
+							DHO_CLASSLESS_STATIC_ROUTES))
26a25c
+			priority_list[priority_len++] = DHO_CLASSLESS_STATIC_ROUTES;
26a25c
+		else
26a25c
+			priority_list[priority_len++] = DHO_ROUTERS;
26a25c
 		priority_list[priority_len++] = DHO_DOMAIN_NAME_SERVERS;
26a25c
 		priority_list[priority_len++] = DHO_HOST_NAME;
26a25c
 		priority_list[priority_len++] = DHO_FQDN;
26a25c
@@ -1694,6 +1698,7 @@ const char *pretty_print_option (option,
26a25c
 	unsigned long tval;
26a25c
 	isc_boolean_t a_array = ISC_FALSE;
26a25c
 	int len_used;
26a25c
+	unsigned int octets = 0;
26a25c
 
26a25c
 	if (emit_commas)
26a25c
 		comma = ',';
26a25c
@@ -1702,6 +1707,7 @@ const char *pretty_print_option (option,
26a25c
 
26a25c
 	memset (enumbuf, 0, sizeof enumbuf);
26a25c
 
26a25c
+	if (option->format[0] != 'R') { /* see explanation lower */
26a25c
 	/* Figure out the size of the data. */
26a25c
 	for (l = i = 0; option -> format [i]; i++, l++) {
26a25c
 		if (l >= sizeof(fmtbuf) - 1)
26a25c
@@ -1894,6 +1900,33 @@ const char *pretty_print_option (option,
26a25c
 	if (numhunk < 0)
26a25c
 		numhunk = 1;
26a25c
 
26a25c
+	} else { /* option->format[i] == 'R') */
26a25c
+		/* R (destination descriptor) has variable length.
26a25c
+		 * We can find it only in classless static route option,
26a25c
+		 * so we are for sure parsing classless static route option now.
26a25c
+		 * We go through whole the option to check whether there are no
26a25c
+		 * missing/extra bytes.
26a25c
+		 * I didn't find out how to improve the existing code and that's the
26a25c
+		 * reason for this separate 'else' where I do my own checkings.
26a25c
+		 * I know it's little bit unsystematic, but it works.
26a25c
+		 */
26a25c
+		numhunk = 0;
26a25c
+		numelem = 2; /* RI */
26a25c
+		fmtbuf[0]='R'; fmtbuf[1]='I'; fmtbuf[2]=0;
26a25c
+		for (i =0; i < len; i = i + octets + 5) {
26a25c
+			if (data[i] > 32) { /* subnet mask width */
26a25c
+				log_error ("wrong subnet mask width in destination descriptor");
26a25c
+				break;
26a25c
+			}
26a25c
+			numhunk++;
26a25c
+			octets = ((data[i]+7) / 8);
26a25c
+		}
26a25c
+		if (i != len) {
26a25c
+			log_error ("classless static routes option has wrong size or "
26a25c
+					   "there's some garbage in format");
26a25c
+		}
26a25c
+	}
26a25c
+
26a25c
 	/* Cycle through the array (or hunk) printing the data. */
26a25c
 	for (i = 0; i < numhunk; i++) {
26a25c
 		if ((a_array == ISC_TRUE) && (i != 0) && (numelem > 0)) {
26a25c
@@ -2049,6 +2082,20 @@ const char *pretty_print_option (option,
26a25c
 				strcpy(op, piaddr(iaddr));
26a25c
 				dp += 4;
26a25c
 				break;
26a25c
+
26a25c
+			      case 'R':
26a25c
+				if (dp[0] <= 32)
26a25c
+					iaddr.len = (((dp[0]+7)/8)+1);
26a25c
+				else {
26a25c
+					log_error ("wrong subnet mask width in destination descriptor");
26a25c
+					return "<error>";
26a25c
+				}
26a25c
+
26a25c
+				memcpy(iaddr.iabuf, dp, iaddr.len);
26a25c
+				strcpy(op, pdestdesc(iaddr));
26a25c
+				dp += iaddr.len;
26a25c
+				break;
26a25c
+
26a25c
 			      case '6':
26a25c
 				iaddr.len = 16;
26a25c
 				memcpy(iaddr.iabuf, dp, 16);
26a25c
diff -up dhcp-4.3.4/common/parse.c.rfc3442 dhcp-4.3.4/common/parse.c
26a25c
--- dhcp-4.3.4/common/parse.c.rfc3442	2016-04-29 12:23:34.220032707 +0200
26a25c
+++ dhcp-4.3.4/common/parse.c	2016-04-29 12:23:34.238032702 +0200
26a25c
@@ -341,6 +341,39 @@ int parse_ip_addr (cfile, addr)
26a25c
 }	
26a25c
 
26a25c
 /*
26a25c
+ * destination-descriptor :== NUMBER DOT NUMBER |
26a25c
+ *                            NUMBER DOT NUMBER DOT NUMBER |
26a25c
+ *                            NUMBER DOT NUMBER DOT NUMBER DOT NUMBER |
26a25c
+ *                            NUMBER DOT NUMBER DOT NUMBER DOT NUMBER DOT NUMBER
26a25c
+ */
26a25c
+
26a25c
+int parse_destination_descriptor (cfile, addr)
26a25c
+	struct parse *cfile;
26a25c
+	struct iaddr *addr;
26a25c
+{
26a25c
+		unsigned int mask_width, dest_dest_len;
26a25c
+		addr -> len = 0;
26a25c
+		if (parse_numeric_aggregate (cfile, addr -> iabuf,
26a25c
+									 &addr -> len, DOT, 10, 8)) {
26a25c
+			mask_width = (unsigned int)addr->iabuf[0];
26a25c
+			dest_dest_len = (((mask_width+7)/8)+1);
26a25c
+			if (mask_width > 32) {
26a25c
+				parse_warn (cfile,
26a25c
+				"subnet mask width (%u) greater than 32.", mask_width);
26a25c
+			}
26a25c
+			else if (dest_dest_len != addr->len) {
26a25c
+				parse_warn (cfile,
26a25c
+				"destination descriptor with subnet mask width %u "
26a25c
+				"should have %u octets, but has %u octets.",
26a25c
+				mask_width, dest_dest_len, addr->len);
26a25c
+			}
26a25c
+
26a25c
+			return 1;
26a25c
+		}
26a25c
+		return 0;
26a25c
+}
26a25c
+
26a25c
+/*
26a25c
  * Return true if every character in the string is hexadecimal.
26a25c
  */
26a25c
 static int
26a25c
@@ -720,8 +753,10 @@ unsigned char *parse_numeric_aggregate (
26a25c
 		if (count) {
26a25c
 			token = peek_token (&val, (unsigned *)0, cfile);
26a25c
 			if (token != separator) {
26a25c
-				if (!*max)
26a25c
+				if (!*max) {
26a25c
+					*max = count;
26a25c
 					break;
26a25c
+				}
26a25c
 				if (token != RBRACE && token != LBRACE)
26a25c
 					token = next_token (&val,
26a25c
 							    (unsigned *)0,
26a25c
@@ -1668,6 +1703,9 @@ int parse_option_code_definition (cfile,
26a25c
 	      case IP_ADDRESS:
26a25c
 		type = 'I';
26a25c
 		break;
26a25c
+	      case DESTINATION_DESCRIPTOR:
26a25c
+		type = 'R';
26a25c
+		break;
26a25c
 	      case IP6_ADDRESS:
26a25c
 		type = '6';
26a25c
 		break;
26a25c
@@ -5097,6 +5135,15 @@ int parse_option_token (rv, cfile, fmt,
26a25c
 		}
26a25c
 		break;
26a25c
 
26a25c
+	      case 'R': /* destination descriptor */
26a25c
+		if (!parse_destination_descriptor (cfile, &addr)) {
26a25c
+			return 0;
26a25c
+		}
26a25c
+		if (!make_const_data (&t, addr.iabuf, addr.len, 0, 1, MDL)) {
26a25c
+			return 0;
26a25c
+		}
26a25c
+		break;
26a25c
+
26a25c
 	      case '6': /* IPv6 address. */
26a25c
 		if (!parse_ip6_addr(cfile, &addr)) {
26a25c
 			return 0;
26a25c
@@ -5374,6 +5421,13 @@ int parse_option_decl (oc, cfile)
26a25c
 					goto exit;
26a25c
 				len = ip_addr.len;
26a25c
 				dp = ip_addr.iabuf;
26a25c
+				goto alloc;
26a25c
+
26a25c
+			      case 'R': /* destination descriptor */
26a25c
+				if (!parse_destination_descriptor (cfile, &ip_addr))
26a25c
+					goto exit;
26a25c
+				len = ip_addr.len;
26a25c
+				dp = ip_addr.iabuf;
26a25c
 
26a25c
 			      alloc:
26a25c
 				if (hunkix + len > sizeof hunkbuf) {
26a25c
diff -up dhcp-4.3.4/common/tables.c.rfc3442 dhcp-4.3.4/common/tables.c
26a25c
--- dhcp-4.3.4/common/tables.c.rfc3442	2016-04-29 12:23:34.209032710 +0200
26a25c
+++ dhcp-4.3.4/common/tables.c	2016-04-29 12:23:34.238032702 +0200
26a25c
@@ -45,6 +45,7 @@ HASH_FUNCTIONS (option_code, const unsig
26a25c
    Format codes:
26a25c
 
26a25c
    I - IPv4 address
26a25c
+   R - destination descriptor (RFC3442)
26a25c
    6 - IPv6 address
26a25c
    l - 32-bit signed integer
26a25c
    L - 32-bit unsigned integer
26a25c
@@ -216,6 +217,7 @@ static struct option dhcp_options[] = {
26a25c
 #endif
26a25c
 	{ "subnet-selection", "I",		&dhcp_universe, 118, 1 },
26a25c
 	{ "domain-search", "D",			&dhcp_universe, 119, 1 },
26a25c
+	{ "classless-static-routes", "RIA",	&dhcp_universe, 121, 1 },
26a25c
 	{ "vivco", "Evendor-class.",		&dhcp_universe, 124, 1 },
26a25c
 	{ "vivso", "Evendor.",			&dhcp_universe, 125, 1 },
26a25c
 #if 0
26a25c
diff -up dhcp-4.3.4/includes/dhcpd.h.rfc3442 dhcp-4.3.4/includes/dhcpd.h
26a25c
--- dhcp-4.3.4/includes/dhcpd.h.rfc3442	2016-04-29 12:23:34.186032716 +0200
26a25c
+++ dhcp-4.3.4/includes/dhcpd.h	2016-04-29 12:23:34.239032702 +0200
26a25c
@@ -2894,6 +2894,7 @@ isc_result_t range2cidr(struct iaddrcidr
26a25c
 			const struct iaddr *lo, const struct iaddr *hi);
26a25c
 isc_result_t free_iaddrcidrnetlist(struct iaddrcidrnetlist **result);
26a25c
 const char *piaddr (struct iaddr);
26a25c
+const char *pdestdesc (struct iaddr);
26a25c
 char *piaddrmask(struct iaddr *, struct iaddr *);
26a25c
 char *piaddrcidr(const struct iaddr *, unsigned int);
26a25c
 u_int16_t validate_port(char *);
26a25c
@@ -3108,6 +3109,7 @@ void parse_client_lease_declaration (str
26a25c
 int parse_option_decl (struct option_cache **, struct parse *);
26a25c
 void parse_string_list (struct parse *, struct string_list **, int);
26a25c
 int parse_ip_addr (struct parse *, struct iaddr *);
26a25c
+int parse_destination_descriptor (struct parse *, struct iaddr *);
26a25c
 int parse_ip_addr_with_subnet(struct parse *, struct iaddrmatch *);
26a25c
 void parse_reject_statement (struct parse *, struct client_config *);
26a25c
 
26a25c
diff -up dhcp-4.3.4/includes/dhcp.h.rfc3442 dhcp-4.3.4/includes/dhcp.h
26a25c
--- dhcp-4.3.4/includes/dhcp.h.rfc3442	2016-03-22 14:16:51.000000000 +0100
26a25c
+++ dhcp-4.3.4/includes/dhcp.h	2016-04-29 12:23:34.239032702 +0200
26a25c
@@ -159,6 +159,7 @@ struct dhcp_packet {
26a25c
 #define DHO_ASSOCIATED_IP			92
26a25c
 #define DHO_SUBNET_SELECTION			118 /* RFC3011! */
26a25c
 #define DHO_DOMAIN_SEARCH			119 /* RFC3397 */
26a25c
+#define DHO_CLASSLESS_STATIC_ROUTES		121 /* RFC3442 */
26a25c
 #define DHO_VIVCO_SUBOPTIONS			124
26a25c
 #define DHO_VIVSO_SUBOPTIONS			125
26a25c
 
26a25c
diff -up dhcp-4.3.4/includes/dhctoken.h.rfc3442 dhcp-4.3.4/includes/dhctoken.h
26a25c
--- dhcp-4.3.4/includes/dhctoken.h.rfc3442	2016-04-29 12:23:34.239032702 +0200
26a25c
+++ dhcp-4.3.4/includes/dhctoken.h	2016-04-29 12:25:07.236008628 +0200
26a25c
@@ -374,7 +374,8 @@ enum dhcp_token {
26a25c
 	LEASE_ID_FORMAT = 676,
26a25c
 	TOKEN_HEX = 677,
26a25c
 	TOKEN_OCTAL = 678,
26a25c
-	BOOTP_BROADCAST_ALWAYS = 679
26a25c
+	BOOTP_BROADCAST_ALWAYS = 679,
26a25c
+	DESTINATION_DESCRIPTOR = 680
26a25c
 };
26a25c
 
26a25c
 #define is_identifier(x)	((x) >= FIRST_TOKEN &&	\