Mailing List Archive

[PATCH] Add support for DHCP-Options
hi,

I've written a start of an c32-module to do basic substition:

add subst.c32

this is a start of a generic substition module

---
commit 9a1f41a7e6599fe6a162197cd9ddc6610185e780
tree cfb799a0be9844926afe44e21b2eb96630666c44
parent 89478bdbfa7167bc1b627a478d042c99e46f06b7
author Maurice Massar <massar@unix-ag.uni-kl.de> Tue, 10 Apr 2007 20:13:39 +0200
committer Maurice Massar <massar@unix-ag.uni-kl.de> Tue, 10 Apr 2007 20:13:39 +0200

com32/modules/Makefile | 3 -
com32/modules/subst.c | 229 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 231 insertions(+), 1 deletions(-)

diff --git a/com32/modules/Makefile b/com32/modules/Makefile
index bef45a8..a7c2de9 100644
--- a/com32/modules/Makefile
+++ b/com32/modules/Makefile
@@ -45,7 +45,8 @@ INCDIR = /usr/include
COM32DIR = $(AUXDIR)/com32

MODULES = chain.c32 menu.c32 vesamenu.c32 ethersel.c32 mboot.c32 \
- dmitest.c32 cpuidtest.c32 pcitest.c32 elf.c32 linux.c32
+ dmitest.c32 cpuidtest.c32 pcitest.c32 elf.c32 linux.c32 \
+ subst.c32
TESTFILES =

all: $(MODULES) $(TESTFILES)
diff --git a/com32/modules/subst.c b/com32/modules/subst.c
new file mode 100644
index 0000000..60241c9
--- /dev/null
+++ b/com32/modules/subst.c
@@ -0,0 +1,229 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 2007 Maurice Massar <massar@unix-ag.uni-kl.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
+ * Boston MA 02111-1307, USA; either version 2 of the License, or
+ * (at your option) any later version; incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * subst.c
+ *
+ * this is a start of a generic substitution module
+ *
+ * Currently only DHCP string options are supported.
+ *
+ * To use, set up a syslinux config file like this:
+ *
+ * LABEL something
+ * KERNEL subst.c32
+ * APPEND vmlinuz-${dhcp/option-123} initrd=initrd.img-${dhcp/option-123}.gz
+ *
+ * further: ".localboot ###" is supported (like menu.c32 does)
+ *
+ * TODO:
+ * - add support for concatenating options (current len limit: 255 bytes)
+ * - add support for options overflowing into filename and server-name area
+ * - add support to recode binary data to text
+ * - add support for non-option dhcp data
+ * - support to declare variables inside config-file
+ *
+ * - if the comboot-api gets a methode to pass a configfile
+ * (contents, not filename) back to syslinux, use it!
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <console.h>
+#include <netinet/in.h>
+#include <syslinux/boot.h>
+#include <syslinux/pxe.h>
+
+typedef struct {
+ uint8_t opcode;
+ uint8_t hardware;
+ uint8_t hardware_len;
+ uint8_t gatehops;
+ uint32_t ident;
+ uint16_t seconds;
+ uint16_t flags;
+
+ in_addr_t cip;
+ in_addr_t yip;
+ in_addr_t sip;
+ in_addr_t gip;
+ uint8_t caddr[16];
+ uint8_t sname[64];
+ uint8_t bootfile[128];
+ uint8_t options[];
+} __packed bootp_t;
+
+bootp_t *dhcpack;
+size_t dhcpack_len;
+
+int find_dhcp_option(uint8_t search, const uint8_t **str, int *len)
+{
+ uint8_t *p;
+ int l;
+
+ p = dhcpack->options;
+ l = dhcpack_len - 236;
+
+ p += 4; l -= 4; /* skip magic */
+
+ while (l > 0) {
+ if (*p == 0) {
+ p++; l--;
+ continue;
+ }
+ if (*p == 255)
+ return 0;
+ if (*p == search)
+ break;
+ l -= 2 + p[1];
+ p += 2 + p[1];
+ }
+
+ l -= 2;
+ l = (p[1] <= l) ? p[1] : l;
+ p += 2;
+
+ *str = p;
+ *len = l;
+ return 1;
+}
+
+char *getvar_dhcp(const char *str, size_t len)
+{
+ const uint8_t *opt_str;
+ char *out = NULL;
+ int num, opt_len;
+
+ if (len >= 7 && memcmp(str, "option-", 7) == 0) {
+ len -= 7; str += 7;
+ num = 0;
+ while (len-- > 0)
+ num = num*10 + *str++ - '0';
+ if (find_dhcp_option(num, &opt_str, &opt_len)) {
+ out = malloc(opt_len + 1);
+ memcpy(out, opt_str, opt_len);
+ out[opt_len] = '\0';
+ }
+ }
+
+ return out;
+}
+
+char *getvar(const char *str, size_t len)
+{
+ char *out = NULL;
+ const char test[] = "UNKNOWN";
+
+ if (len >= 5 && memcmp(str, "dhcp/", 5) == 0) {
+ out = getvar_dhcp(str+5, len-5);
+ }
+
+ if (out == NULL) {
+ out = malloc(sizeof(test));
+ strcpy(out, test);
+ }
+
+ return out;
+}
+
+void append_output(char **out, size_t *outlen, const char *str, size_t len)
+{
+ size_t used;
+
+ used = (*out != NULL) ? strlen(*out) : 0;
+ if (used + len + 1 > *outlen) {
+ *outlen += 256 + (len & ~255);
+ *out = realloc(*out, *outlen);
+ }
+ memcpy(*out + used, str, len);
+ (*out)[used+len] = '\0';
+}
+
+char *do_subst(const char *str)
+{
+ int i;
+ enum {NORMAL, LITERAL, QUOTE} state = NORMAL;
+ char *out = NULL, *replacement;
+ const char *start, *stop;
+ size_t outlen = 0;
+
+ for (i = 0; str[i] != '\0'; i++) {
+ switch (state) {
+ case LITERAL:
+ append_output(&out, &outlen, str+i, 1);
+ state = NORMAL;
+ break;
+ case QUOTE:
+ if (str[i] == '\'') {
+ state = NORMAL;
+ break;
+ }
+ append_output(&out, &outlen, str+i, 1);
+ break;
+ case NORMAL:
+ if (str[i] == '\\') {
+ state = LITERAL;
+ break;
+ } else if (str[i] == '\'') {
+ state = QUOTE;
+ break;
+ } else if (str[i] != '$') {
+ append_output(&out, &outlen, str+i, 1);
+ break;
+ }
+ start = (str[i+1] == '{') ? str+i+1 : NULL;
+ stop = strchr(str+i+2, '}');
+ if (start == NULL || stop == NULL) {
+ append_output(&out, &outlen, str+i, 1);
+ break;
+ }
+ replacement = getvar(start + 1, stop-start-1);
+ append_output(&out, &outlen, replacement, strlen(replacement));
+ free(replacement);
+ i += stop-start+1;
+ break;
+ }
+ }
+
+ return out;
+}
+
+int main(int argc, char *argv[])
+{
+ int i;
+ char *out, *cmdline = NULL;
+ void *buf;
+ size_t cmd_len = 0;
+
+ openconsole(&dev_stdcon_r, &dev_stdcon_w);
+ if (pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK, &buf, &dhcpack_len) != 0) {
+ fprintf(stderr, "failed to get dhcp infos\n");
+ return 1;
+ }
+ dhcpack = (bootp_t *)buf;
+
+ for (i = 1; i < argc; i++) {
+ out = do_subst(argv[i]);
+ append_output(&cmdline, &cmd_len, out, strlen(out));
+ free(out);
+ if (i+1 < argc)
+ append_output(&cmdline, &cmd_len, " ", 1);
+ }
+ printf("subst: cmdline=%s\n", cmdline);
+ if (memcmp(cmdline, ".localboot ", 11) == 0) {
+ syslinux_local_boot(strtol(cmdline+11, NULL, 0));
+ }
+ syslinux_run_command(cmdline);
+
+ return 0;
+}

_______________________________________________
SYSLINUX mailing list
Submissions to SYSLINUX@zytor.com
Unsubscribe or set options at:
http://www.zytor.com/mailman/listinfo/syslinux
Please do not send private replies to mailing list traffic.
Re: [PATCH] Add support for DHCP-Options [ In reply to ]
Maurice Massar <massar@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@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;

_______________________________________________
SYSLINUX mailing list
Submissions to SYSLINUX@zytor.com
Unsubscribe or set options at:
http://www.zytor.com/mailman/listinfo/syslinux
Please do not send private replies to mailing list traffic.
Re: [PATCH] Add support for DHCP-Options [ In reply to ]
Ferenc Wagner <wferi@niif.hu> writes:

> Maurice Massar <massar@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.

Whoops, usage examples were left off. Here we go:

kernel dhcp.c32
append vmlinuz initrd=initrd.img root=/dev/mapper/$HOST$-root

or

kernel dhcp.c32
append -dump vmlinuz initrd=initrd.img root=/dev/mapper/$HOST$-root

or even

kernel dhcp.c32
append -dhcpinfo vmlinuz initrd=$SNAME$.img

Basically, known DHCP data is rendered as appropriate, unknown options
are sort of URL encoded, which isn't really usable, but at least shows
them in the dump. $ can be produced as $$. Currently implemented
options:

DHCP_SERVER_IDENTIFIER ---> SERVER
DHCP_LEASE_TIME ---> LEASE
SUBNET_MASK ---> MASK
TIME_OFFSET ---> TIME_OFFSET
ROUTERS ---> ROUTERS
DOMAIN_NAME_SERVERS ---> DNS
HOST_NAME ---> HOST
DOMAIN_NAME ---> DOMAIN

CIADDR, YIADDR, SIADDR, GIADDR, SNAME and FILENAME stand for
themselves. The matching is case insensitive.

-dump prints the parsed data, -dhcpinfo adds the packet to the initrd
(original linux.c function)
--
Regards,
Feri.

_______________________________________________
SYSLINUX mailing list
Submissions to SYSLINUX@zytor.com
Unsubscribe or set options at:
http://www.zytor.com/mailman/listinfo/syslinux
Please do not send private replies to mailing list traffic.
Re: [PATCH] Add support for DHCP-Options [ In reply to ]
Ferenc Wagner <wferi@niif.hu> writes:

> Whoops, usage examples were left off. Here we go:
>
> kernel dhcp.c32

Er, I mean linux.c32, but you must have guessed it.
--
Feri.

_______________________________________________
SYSLINUX mailing list
Submissions to SYSLINUX@zytor.com
Unsubscribe or set options at:
http://www.zytor.com/mailman/listinfo/syslinux
Please do not send private replies to mailing list traffic.
Re: [PATCH] Add support for DHCP-Options [ In reply to ]
Ferenc Wagner <wferi@niif.hu> writes:

> Maurice Massar <massar@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.

Hi Peter,

did you consider adding this to the syslinux distribution?
--
Regards,
Feri.

_______________________________________________
SYSLINUX mailing list
Submissions to SYSLINUX@zytor.com
Unsubscribe or set options at:
http://www.zytor.com/mailman/listinfo/syslinux
Please do not send private replies to mailing list traffic.
Re: [PATCH] Add support for DHCP-Options [ In reply to ]
Ferenc Wagner wrote:
> Ferenc Wagner <wferi@niif.hu> writes:
>
>> Maurice Massar <massar@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.
>
> Hi Peter,
>
> did you consider adding this to the syslinux distribution?

Not really. If I'm going to have a macro system it probably needs to be
a bit more generic. I'm currently thinking over how I want to do that,
or to what extent it's practical.

I've gotten some other request in that area, too, which also need to be
considered.

-hpa

_______________________________________________
SYSLINUX mailing list
Submissions to SYSLINUX@zytor.com
Unsubscribe or set options at:
http://www.zytor.com/mailman/listinfo/syslinux
Please do not send private replies to mailing list traffic.
Re: [PATCH] Add support for DHCP-Options [ In reply to ]
"H. Peter Anvin" <hpa@zytor.com> writes:

> Ferenc Wagner wrote:
>> Ferenc Wagner <wferi@niif.hu> writes:
>>
>>> Maurice Massar <massar@unix-ag.uni-kl.de> writes:
>>>
>>>> I've written a start of an c32-module to do basic substition:
>>> Interesting :) I've just finished patching the linux.c32 module for
>>> much the same purpose.
>>
>> did you consider adding this to the syslinux distribution?
>
> Not really. If I'm going to have a macro system it probably needs to be
> a bit more generic. I'm currently thinking over how I want to do that,
> or to what extent it's practical.
>
> I've gotten some other request in that area, too, which also need to be
> considered.

Ok. Let me know if I can lend a hand.
--
Feri.

_______________________________________________
SYSLINUX mailing list
Submissions to SYSLINUX@zytor.com
Unsubscribe or set options at:
http://www.zytor.com/mailman/listinfo/syslinux
Please do not send private replies to mailing list traffic.
Re: [PATCH] Add support for DHCP-Options [ In reply to ]
hi,

On Fri, Apr 27, 2007 at 08:55:02AM -0700, H. Peter Anvin wrote:
> Ferenc Wagner wrote:
> > Ferenc Wagner <wferi@niif.hu> writes:
> >> Maurice Massar <massar@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.
> >
> > did you consider adding this to the syslinux distribution?
>
> Not really. If I'm going to have a macro system it probably needs to be
> a bit more generic. I'm currently thinking over how I want to do that,
> or to what extent it's practical.

If you add a way to return a configfile (instead of configfile name)
from a c32-module to syslinux, then anything should be possible to be
implemented as c32. What do you think about this way? Or do you want
to integrate it more in syslinux core?

cu
maurice

_______________________________________________
SYSLINUX mailing list
Submissions to SYSLINUX@zytor.com
Unsubscribe or set options at:
http://www.zytor.com/mailman/listinfo/syslinux
Please do not send private replies to mailing list traffic.
Re: [PATCH] Add support for DHCP-Options [ In reply to ]
Maurice Massar wrote:
>
> If you add a way to return a configfile (instead of configfile name)
> from a c32-module to syslinux, then anything should be possible to be
> implemented as c32. What do you think about this way? Or do you want
> to integrate it more in syslinux core?
>

No, that is highly unlikely to be implemented. It would be a highly
nontrivial change.

-hpa

_______________________________________________
SYSLINUX mailing list
Submissions to SYSLINUX@zytor.com
Unsubscribe or set options at:
http://www.zytor.com/mailman/listinfo/syslinux
Please do not send private replies to mailing list traffic.