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

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