philipp / rpms / dhcp

Forked from rpms/dhcp 4 years ago
Clone

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

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