Maurice Massar <massar at unix-ag.uni-kl.de> writes:
> I've written a start of an c32-module to do basic substition:
Intesting :) I've just finished patching the linux.c32 module for much
the same purpose. dhcp.h is stolen from the ISC DHCP implementation.
I'd really like to see something like this included in syslinux. I'm
not quite sure how to handle the unknown options, especially with
backward compatibility in mind.
diff -Nur syslinux-3.50-pre2/com32/modules/dhcp.h
syslinux-3.50-pre2.parse/com32/modules/dhcp.h
--- syslinux-3.50-pre2/com32/modules/dhcp.h 1970-01-01 01:00:00.000000000 +0100
+++ syslinux-3.50-pre2.parse/com32/modules/dhcp.h 2007-04-10 20:15:28.311218380
+0200
@@ -0,0 +1,185 @@
+/* dhcp.h
+
+ Protocol structures... */
+
+/*
+ * Copyright (c) 2004-2005 by Internet Systems Consortium, Inc.
("ISC")
+ * Copyright (c) 1995-2003 by Internet Software Consortium
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Internet Systems Consortium, Inc.
+ * 950 Charter Street
+ * Redwood City, CA 94063
+ * <info at isc.org>
+ * http://www.isc.org/
+ *
+ * This software has been written for Internet Systems Consortium
+ * by Ted Lemon in cooperation with Vixie Enterprises. To learn more
+ * about Internet Systems Consortium, see ``http://www.isc.org''.
+ * To learn more about Vixie Enterprises, see ``http://www.vix.com''.
+ */
+
+#define DHCP_UDP_OVERHEAD (20 + /* IP header */ \
+ 8) /* UDP header */
+#define DHCP_SNAME_LEN 64
+#define DHCP_FILE_LEN 128
+#define DHCP_FIXED_NON_UDP 236
+#define DHCP_FIXED_LEN (DHCP_FIXED_NON_UDP + DHCP_UDP_OVERHEAD)
+ /* Everything but options. */
+#define DHCP_MTU_MAX 1500
+#define DHCP_OPTION_LEN (DHCP_MTU_MAX - DHCP_FIXED_LEN)
+
+#define BOOTP_MIN_LEN 300
+#define DHCP_MIN_LEN 548
+
+struct dhcp_packet {
+ u_int8_t op; /* 0: Message opcode/type */
+ u_int8_t htype; /* 1: Hardware addr type (net/if_types.h) */
+ u_int8_t hlen; /* 2: Hardware addr length */
+ u_int8_t hops; /* 3: Number of relay agent hops from client */
+ u_int32_t xid; /* 4: Transaction ID */
+ u_int16_t secs; /* 8: Seconds since client started looking */
+ u_int16_t flags; /* 10: Flag bits */
+ struct in_addr ciaddr; /* 12: Client IP address (if already in use) */
+ struct in_addr yiaddr; /* 16: Client IP address */
+ struct in_addr siaddr; /* 18: IP address of next server to talk to */
+ struct in_addr giaddr; /* 20: DHCP relay agent IP address */
+ unsigned char chaddr [16]; /* 24: Client hardware address */
+ char sname [DHCP_SNAME_LEN]; /* 40: Server name */
+ char file [DHCP_FILE_LEN]; /* 104: Boot filename */
+ unsigned char options [DHCP_OPTION_LEN];
+ /* 212: Optional parameters
+ (actual length dependent on MTU). */
+};
+
+/* BOOTP (rfc951) message types */
+#define BOOTREQUEST 1
+#define BOOTREPLY 2
+
+/* Possible values for flags field... */
+#define BOOTP_BROADCAST 32768L
+
+/* Possible values for hardware type (htype) field... */
+#define HTYPE_ETHER 1 /* Ethernet 10Mbps */
+#define HTYPE_IEEE802 6 /* IEEE 802.2 Token Ring... */
+#define HTYPE_FDDI 8 /* FDDI... */
+
+/* Magic cookie validating dhcp options field (and bootp vendor
+ extensions field). */
+#define DHCP_OPTIONS_COOKIE "\143\202\123\143"
+
+/* DHCP Option codes: */
+
+#define DHO_PAD 0
+#define DHO_SUBNET_MASK 1
+#define DHO_TIME_OFFSET 2
+#define DHO_ROUTERS 3
+#define DHO_TIME_SERVERS 4
+#define DHO_NAME_SERVERS 5
+#define DHO_DOMAIN_NAME_SERVERS 6
+#define DHO_LOG_SERVERS 7
+#define DHO_COOKIE_SERVERS 8
+#define DHO_LPR_SERVERS 9
+#define DHO_IMPRESS_SERVERS 10
+#define DHO_RESOURCE_LOCATION_SERVERS 11
+#define DHO_HOST_NAME 12
+#define DHO_BOOT_SIZE 13
+#define DHO_MERIT_DUMP 14
+#define DHO_DOMAIN_NAME 15
+#define DHO_SWAP_SERVER 16
+#define DHO_ROOT_PATH 17
+#define DHO_EXTENSIONS_PATH 18
+#define DHO_IP_FORWARDING 19
+#define DHO_NON_LOCAL_SOURCE_ROUTING 20
+#define DHO_POLICY_FILTER 21
+#define DHO_MAX_DGRAM_REASSEMBLY 22
+#define DHO_DEFAULT_IP_TTL 23
+#define DHO_PATH_MTU_AGING_TIMEOUT 24
+#define DHO_PATH_MTU_PLATEAU_TABLE 25
+#define DHO_INTERFACE_MTU 26
+#define DHO_ALL_SUBNETS_LOCAL 27
+#define DHO_BROADCAST_ADDRESS 28
+#define DHO_PERFORM_MASK_DISCOVERY 29
+#define DHO_MASK_SUPPLIER 30
+#define DHO_ROUTER_DISCOVERY 31
+#define DHO_ROUTER_SOLICITATION_ADDRESS 32
+#define DHO_STATIC_ROUTES 33
+#define DHO_TRAILER_ENCAPSULATION 34
+#define DHO_ARP_CACHE_TIMEOUT 35
+#define DHO_IEEE802_3_ENCAPSULATION 36
+#define DHO_DEFAULT_TCP_TTL 37
+#define DHO_TCP_KEEPALIVE_INTERVAL 38
+#define DHO_TCP_KEEPALIVE_GARBAGE 39
+#define DHO_NIS_DOMAIN 40
+#define DHO_NIS_SERVERS 41
+#define DHO_NTP_SERVERS 42
+#define DHO_VENDOR_ENCAPSULATED_OPTIONS 43
+#define DHO_NETBIOS_NAME_SERVERS 44
+#define DHO_NETBIOS_DD_SERVER 45
+#define DHO_NETBIOS_NODE_TYPE 46
+#define DHO_NETBIOS_SCOPE 47
+#define DHO_FONT_SERVERS 48
+#define DHO_X_DISPLAY_MANAGER 49
+#define DHO_DHCP_REQUESTED_ADDRESS 50
+#define DHO_DHCP_LEASE_TIME 51
+#define DHO_DHCP_OPTION_OVERLOAD 52
+#define DHO_DHCP_MESSAGE_TYPE 53
+#define DHO_DHCP_SERVER_IDENTIFIER 54
+#define DHO_DHCP_PARAMETER_REQUEST_LIST 55
+#define DHO_DHCP_MESSAGE 56
+#define DHO_DHCP_MAX_MESSAGE_SIZE 57
+#define DHO_DHCP_RENEWAL_TIME 58
+#define DHO_DHCP_REBINDING_TIME 59
+#define DHO_VENDOR_CLASS_IDENTIFIER 60
+#define DHO_DHCP_CLIENT_IDENTIFIER 61
+#define DHO_NWIP_DOMAIN_NAME 62
+#define DHO_NWIP_SUBOPTIONS 63
+#define DHO_USER_CLASS 77
+#define DHO_FQDN 81
+#define DHO_DHCP_AGENT_OPTIONS 82
+#define DHO_SUBNET_SELECTION 118 /* RFC3011! */
+/* The DHO_AUTHENTICATE option is not a standard yet, so I've
+ allocated an option out of the "local" option space for it on a
+ temporary basis. Once an option code number is assigned, I will
+ immediately and shamelessly break this, so don't count on it
+ continuing to work. */
+#define DHO_AUTHENTICATE 210
+
+#define DHO_END 255
+
+/* DHCP message types. */
+#define DHCPDISCOVER 1
+#define DHCPOFFER 2
+#define DHCPREQUEST 3
+#define DHCPDECLINE 4
+#define DHCPACK 5
+#define DHCPNAK 6
+#define DHCPRELEASE 7
+#define DHCPINFORM 8
+
+/* Relay Agent Information option subtypes: */
+#define RAI_CIRCUIT_ID 1
+#define RAI_REMOTE_ID 2
+#define RAI_AGENT_ID 3
+
+/* FQDN suboptions: */
+#define FQDN_NO_CLIENT_UPDATE 1
+#define FQDN_SERVER_UPDATE 2
+#define FQDN_ENCODED 3
+#define FQDN_RCODE1 4
+#define FQDN_RCODE2 5
+#define FQDN_HOSTNAME 6
+#define FQDN_DOMAINNAME 7
+#define FQDN_FQDN 8
+#define FQDN_SUBOPTION_COUNT 8
diff -Nur syslinux-3.50-pre2/com32/modules/linux.c
syslinux-3.50-pre2.parse/com32/modules/linux.c
--- syslinux-3.50-pre2/com32/modules/linux.c 2007-04-02 01:29:38.000000000 +0200
+++ syslinux-3.50-pre2.parse/com32/modules/linux.c 2007-04-10 21:01:16.373044427
+0200
@@ -45,6 +45,298 @@
#include <syslinux/linux.h>
#include <syslinux/pxe.h>
+typedef uint8_t u_int8_t; /* sys/types.h */
+typedef uint16_t u_int16_t; /* doesn't work */
+typedef uint32_t u_int32_t; /* here */
+#include <netinet/in.h> /* struct in_addr */
+#include "dhcp.h" /* from ISC DHCP */
+
+/* Type of a rendering function. These functions check the length
+ field, allocate space for themselves at BUFFER and return 1 if the
+ value is to be stored. NAME is for printing error messages, P points
+ to the option code in the DHCP packet. */
+typedef int renderer_t (char **buffer, const char *name, const u_int8_t *p);
+
+/* Desciptor of a known DHCP option */
+struct dhcp_option {
+ u_int8_t code;
+ renderer_t *render;
+ const char *name;
+};
+
+/* Substitution ready representation of parsed data */
+struct option_val {
+ const char *desc;
+ const char *value;
+};
+
+/* Print a warning and return failure */
+int
+length_error (const char *name, u_int8_t len)
+{
+ fprintf (stderr, "Invalid %s option length: %d\n",
+ name, len);
+ return 0;
+}
+
+/* Print an error and exit with failure */
+void
+alloc_error (void)
+{
+ fprintf (stderr, "Allocation error!\n");
+ exit (1);
+}
+
+/* Rendering functions */
+int
+render_u8 (char **buffer, const char *name, const u_int8_t *p)
+{
+ if (p[1] != 1) return length_error (name, p[1]);
+ if (asprintf (buffer, "%u", p[2]) == -1) alloc_error ();
+ return 1;
+}
+
+int
+render_u32 (char **buffer, const char *name, const u_int8_t *p)
+{
+ if (p[1] != 4) return length_error (name, p[1]);
+ if (asprintf (buffer, "%u", ntohl (*(unsigned *)(p+2))) == -1)
alloc_error ();
+ return 1;
+}
+
+int
+render_s32 (char **buffer, const char *name, const u_int8_t *p)
+{
+ if (p[1] != 4) return length_error (name, p[1]);
+ if (asprintf (buffer, "%d", ntohl (*(unsigned *)(p+2))) == -1)
alloc_error ();
+ return 1;
+}
+
+int
+render_ip (char **buffer, const char *name, const u_int8_t *p)
+{
+ if (p[1] != 4) return length_error (name, p[1]);
+ if (asprintf (buffer, "%d.%d.%d.%d", p[2], p[3], p[4], p[5]) ==
-1) alloc_error ();
+ return 1;
+}
+
+int
+render_ips (char **buffer, const char *name, const u_int8_t *p)
+{
+ int i, next = 0;
+
+ if (p[1] < 4 || p[1] % 4 != 0) return length_error (name, p[1]);
+ *buffer = malloc (4*p[1]+1); /* 16 chars per dotted quad (including
separator) */
+ if (!buffer) alloc_error ();
+ for (i = 0; i < p[1]; i += 4) {
+ next += snprintf (*buffer + next, 17, "%d.%d.%d.%d,",
+ p[i+2], p[i+3], p[i+4], p[i+5]);
+ }
+ (*buffer)[next-1] = 0; /* overwrite the last separator */
+ return 1;
+}
+
+int
+render_str (char **buffer, const char *name, const u_int8_t *p)
+{
+ if (p[1] < 1) return length_error (name, p[1]);
+ if (asprintf (buffer, "%.*s", p[1], p+2) == -1) alloc_error ();
+ return 1;
+}
+
+/* Renderer for unknown options: encodes into hexadecimal */
+int
+render_url (char **buffer, const char *name, const u_int8_t *p)
+{
+ int i, next = 0;
+
+ *buffer = malloc (3*p[1] + 1); /* 3 chars per octet */
+ if (!buffer) alloc_error ();
+ for (i = 0; i < p[1]; i++) {
+ int ret = snprintf (*buffer + next, 4, "%%%02x", p[i+2]);
+ next += ret;
+ }
+ name = NULL; /* silence the warning */
+ return 1;
+}
+
+/* Not a real renderer, prints a warning and returns failure */
+int
+render_ovl (char **buffer, const char *name, const u_int8_t *p)
+{
+ if (p[1] != 1) return length_error (name, p[1]);
+ fprintf (stderr, "Option overload is not handled yet!\n");
+ buffer = NULL; /* silence the warning */
+ return 0;
+}
+
+/* Known option descriptions */
+const struct dhcp_option options[] = {
+ {DHO_DHCP_MESSAGE_TYPE , render_u8 , "message type"},
+ {DHO_DHCP_SERVER_IDENTIFIER, render_ip , "SERVER"},
+ {DHO_DHCP_LEASE_TIME , render_u32, "LEASE"},
+ {DHO_SUBNET_MASK , render_ip , "MASK"},
+ {DHO_TIME_OFFSET , render_s32, "TIME_OFFSET"},
+ {DHO_ROUTERS , render_ips, "ROUTERS"},
+ {DHO_DOMAIN_NAME_SERVERS , render_ips, "DNS"},
+ {DHO_HOST_NAME , render_str, "HOST"},
+ {DHO_DOMAIN_NAME , render_str, "DOMAIN"},
+ {DHO_DHCP_OPTION_OVERLOAD , render_ovl, "option overload"}
+};
+
+/* Allocate space and render a dotted quad IP address */
+char *
+dotted_quad (struct in_addr a)
+{
+ char *s;
+ u_int8_t *p = (u_int8_t *)&a.s_addr;
+
+ if (asprintf (&s, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]) ==
-1) alloc_error ();
+ return s;
+}
+
+/* A simple dynamic array */
+struct store {
+ unsigned allocated; /* space allocated for this many items */
+ unsigned length; /* actual number of items in the array */
+ unsigned size; /* size of an item in bytes */
+ unsigned blocksize; /* allocate this many bytes at once */
+ void *buffer; /* pointer to the array */
+};
+
+/* Store N objects starting from OBJ into S */
+void
+store (const void *obj, size_t n, struct store *s)
+{
+ if (s->length + n > s->allocated) {
+ void *new;
+ size_t buflen = (s->length + n) * s->size;
+ int blknum = buflen / s->blocksize;
+ int rest = buflen % s->blocksize;
+
+ if (rest != 0) blknum++;
+ buflen = blknum * s->blocksize;
+ new = realloc (s->buffer, buflen);
+ if (!new) alloc_error ();
+ s->buffer = new;
+ s->allocated = buflen / s->size;
+ }
+ memcpy (s->buffer + s->length * s->size, obj, n * s->size);
+ s->length += n;
+}
+
+/* Helper to store an option name/value pair */
+void
+store_option (const char *desc, const char *value, struct store *s)
+{
+ struct option_val o = {desc, value};
+ store (&o, 1, s);
+}
+
+/* Parse a DHCP packet of BYTES length into S */
+void
+parse_packet (const struct dhcp_packet *packet, size_t bytes,
+ struct store *s)
+{
+ const size_t csize = sizeof DHCP_OPTIONS_COOKIE - 1; /* without closing NUL
*/
+
+ if (bytes < DHCP_FIXED_NON_UDP) {
+ fprintf (stderr, "Packet too small!\n");
+ exit (1);
+ }
+
+ store_option ("CIADDR", dotted_quad (packet->ciaddr), s);
+ store_option ("YIADDR", dotted_quad (packet->yiaddr), s);
+ store_option ("SIADDR", dotted_quad (packet->siaddr), s);
+ store_option ("GIADDR", dotted_quad (packet->giaddr), s);
+ store_option ("SNAME", packet->sname, s);
+ store_option ("FILENAME", packet->file, s);
+
+ if (bytes > DHCP_FIXED_NON_UDP + csize &&
+ memcmp (packet->options, DHCP_OPTIONS_COOKIE, csize) == 0) {
+ const u_int8_t *p = packet->options + csize; /* first
possible option code */
+ const u_int8_t *last = (u_int8_t *)packet + bytes - 1; /* last byte of
the packet */
+
+ while (p <= last) {
+ char *value;
+ unsigned i;
+
+ if (*p == DHO_PAD) continue;
+ if (*p == DHO_END) break;
+ if (p == last) {
+ fprintf (stderr, "Invalid option %d: length not
included!\n", *p);
+ exit (1);
+ }
+ if (p + 1 + p[1] > last) {
+ fprintf (stderr, "Invalid option %d: value
truncated!\n", *p);
+ exit (1);
+ }
+
+ value = NULL;
+ for (i = 0; i < sizeof options / sizeof *options; i++)
+ if (options[i].code == *p && options[i].render
(&value, options[i].name, p))
+ store_option (options[i].name, value, s);
+ if (!value) { /* Unknown option, hex encode it */
+ char *digital;
+ if (asprintf (&digital, "%u", *p) == -1)
alloc_error ();
+ if (render_url (&value, digital, p))
+ store_option (digital, value, s);
+ }
+ p += p[1] + 2;
+ }
+ }
+}
+
+/* Copy from C to S substituting $ delimited substrings present in OPT_P. */
+void
+strsub (const char *c, const struct store *opt_p, struct store *s)
+{
+ static const char esc = '$';
+
+ while (*c) {
+ if (*c == esc) {
+ if (c[1] == esc) { /* substitue $ for $$ */
+ c += 2;
+ store (&esc, 1, s);
+ } else { /* look for an option name */
+ const unsigned opts = opt_p->length;
+ unsigned i;
+
+ for (i = 0; i < opts; i++) {
+ const struct option_val *opt = opt_p->buffer;
+ const char *desc = opt[i].desc;
+ unsigned desclen = strlen (desc);
+
+ if (strncasecmp (c+1, desc, desclen) == 0
+ && c[desclen+1] == esc) {
+ store (opt[i].value, strlen (opt[i].value), s);
+ c += desclen+2;
+ break;
+ }
+ }
+ if (i == opts) store (c++, 1, s); /* not found */
+ }
+ } else store (c++, 1, s); /* just copy everything else */
+ }
+}
+
+/* Stitch together the command line from a set of argv's,
+ substituting values at opt_p. */
+static char *make_cmdline (const struct store *opt_p, char **argv)
+{
+ struct store c = {0, 0, 1, 20, NULL};
+
+ while (*argv) {
+ strsub (*argv, opt_p, &c);
+ store (" ", 1, &c);
+ argv++;
+ }
+ if (c.buffer) {
+ ((char *)c.buffer)[c.length-1] = 0; /* Change trailing space into NUL
*/
+ return c.buffer;
+ } else return NULL;
+}
+
const char *progname = "linux.c32";
/* Find the last instance of a particular command line argument
@@ -99,36 +391,6 @@
return v;
}
-/* Stitch together the command line from a set of argv's */
-static char *make_cmdline(char **argv)
-{
- char **arg;
- size_t bytes;
- char *cmdline, *p;
-
- bytes = 1; /* Just in case we have a zero-entry cmdline */
- for (arg = argv; *arg; arg++) {
- bytes += strlen(*arg)+1;
- }
-
- p = cmdline = malloc(bytes);
- if (!cmdline)
- return NULL;
-
- for (arg = argv; *arg; arg++) {
- int len = strlen(*arg);
- memcpy(p, *arg, len);
- p[len] = ' ';
- p += len+1;
- }
-
- if (p > cmdline)
- p--; /* Remove the last space */
- *p = '\0';
-
- return cmdline;
-}
-
int main(int argc, char *argv[])
{
uint32_t mem_limit = 0;
@@ -140,9 +402,11 @@
void *kernel_data;
size_t kernel_len;
int opt_dhcpinfo = 0;
+ int opt_dhcpdump = 0;
void *dhcpdata;
size_t dhcplen;
char **argp, *arg, *p;
+ struct store dhcp_params = {0, 0, sizeof (struct option_val), 20, NULL};
openconsole(&dev_null_r, &dev_stdcon_w);
@@ -152,10 +416,30 @@
while ((arg = *argp) && arg[0] == '-') {
if (!strcmp("-dhcpinfo", arg)) {
opt_dhcpinfo = 1;
+ } else if (!strcmp("-dump", arg)) {
+ opt_dhcpdump = 1;
} else {
fprintf(stderr, "%s: unknown option: %s\n", progname, arg);
return 1;
}
+ argp++;
+ }
+
+ if (!pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK, &dhcpdata,
&dhcplen)) {
+ parse_packet(dhcpdata, dhcplen, &dhcp_params);
+ } else {
+ if (opt_dhcpinfo || opt_dhcpdump) {
+ fprintf(stderr, "Can't get DHCP packet\n");
+ return 1;
+ }
+ }
+
+ if (opt_dhcpdump) {
+ unsigned i;
+ for (i = 0; i < dhcp_params.length; i++) {
+ const struct option_val *o = dhcp_params.buffer;
+ printf ("%s: %s\n", o[i].desc, o[i].value);
+ }
}
if (!arg) {
@@ -184,7 +468,7 @@
we have the final argument. */
*argp = boot_image;
- cmdline = make_cmdline(argp);
+ cmdline = make_cmdline(&dhcp_params, argp);
if (!cmdline)
goto bail;
@@ -219,8 +503,7 @@
}
/* Append the DHCP info */
- if (opt_dhcpinfo &&
- !pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK, &dhcpdata,
&dhcplen)) {
+ if (opt_dhcpinfo) {
if (initramfs_add_file(initramfs, dhcpdata, dhcplen, dhcplen,
"/dhcpinfo.dat", 0, 0755))
goto bail;