tweak Config texts and some broken English elsewhere
[oweals/busybox.git] / networking / udhcp / clientpacket.c
index 86faec925d446c6896a4978f60d81daf07460d4d..21c1a7bd56a15987ee70aa17f66cd051797eba4d 100644 (file)
@@ -1,28 +1,15 @@
+/* vi: set sw=4 ts=4: */
 /* clientpacket.c
  *
  * Packet generation and dispatching functions for the DHCP client.
  *
  * Russ Dill <Russ.Dill@asu.edu> July 2001
  *
- * 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; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
-#include <string.h>
-#include <sys/socket.h>
+
 #include <features.h>
-#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
+#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
 #include <netpacket/packet.h>
 #include <net/ethernet.h>
 #else
 #include <linux/if_packet.h>
 #include <linux/if_ether.h>
 #endif
-#include <stdlib.h>
-#include <time.h>
-#include <unistd.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
 
+#include "common.h"
 #include "dhcpd.h"
-#include "packet.h"
-#include "options.h"
 #include "dhcpc.h"
-#include "debug.h"
+#include "options.h"
 
 
 /* Create a random xid */
-unsigned long random_xid(void)
+uint32_t FAST_FUNC random_xid(void)
 {
-       static int initialized;
+       static smallint initialized;
+
        if (!initialized) {
-               int fd;
-               unsigned long seed;
-
-               fd = open("/dev/urandom", 0);
-               if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) {
-                       LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %s",
-                               strerror(errno));
-                       seed = time(0);
-               }
-               if (fd >= 0) close(fd);
-               srand(seed);
-               initialized++;
+               srand(monotonic_us());
+               initialized = 1;
        }
        return rand();
 }
 
 
-/* initialize a packet with the proper defaults */
-static void init_packet(struct dhcpMessage *packet, char type)
+/* Initialize the packet with the proper defaults */
+static void init_packet(struct dhcp_packet *packet, char type)
 {
-       struct vendor  {
-               char vendor, length;
-               char str[sizeof("udhcp "VERSION)];
-       } vendor_id = { DHCP_VENDOR,  sizeof("udhcp "VERSION) - 1, "udhcp "VERSION};
-       
-       init_header(packet, type);
-       memcpy(packet->chaddr, client_config.arp, 6);
-       add_option_string(packet->options, client_config.clientid);
-       if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
-       add_option_string(packet->options, (unsigned char *) &vendor_id);
+       udhcp_init_header(packet, type);
+       memcpy(packet->chaddr, client_config.client_mac, 6);
+       if (client_config.clientid)
+               add_option_string(packet->options, client_config.clientid);
+       if (client_config.hostname)
+               add_option_string(packet->options, client_config.hostname);
+       if (client_config.fqdn)
+               add_option_string(packet->options, client_config.fqdn);
+       if ((type != DHCPDECLINE) && (type != DHCPRELEASE))
+               add_option_string(packet->options, client_config.vendorclass);
 }
 
 
-/* Add a paramater request list for stubborn DHCP servers. Pull the data
+/* Add a parameter request list for stubborn DHCP servers. Pull the data
  * from the struct in options.c. Don't do bounds checking here because it
  * goes towards the head of the packet. */
-static void add_requests(struct dhcpMessage *packet)
+static void add_param_req_option(struct dhcp_packet *packet)
 {
+       uint8_t c;
        int end = end_option(packet->options);
        int i, len = 0;
 
-       packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
-       for (i = 0; options[i].code; i++)
-               if (options[i].flags & OPTION_REQ)
-                       packet->options[end + OPT_DATA + len++] = options[i].code;
-       packet->options[end + OPT_LEN] = len;
-       packet->options[end + OPT_DATA + len] = DHCP_END;
+       for (i = 0; (c = dhcp_options[i].code) != 0; i++) {
+               if (((dhcp_options[i].flags & OPTION_REQ)
+                    && !client_config.no_default_options)
+                || (client_config.opt_mask[c >> 3] & (1 << (c & 7)))
+               ) {
+                       packet->options[end + OPT_DATA + len] = c;
+                       len++;
+               }
+       }
+       if (len) {
+               packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
+               packet->options[end + OPT_LEN] = len;
+               packet->options[end + OPT_DATA + len] = DHCP_END;
+       }
+}
 
+/* RFC 2131
+ * 4.4.4 Use of broadcast and unicast
+ *
+ * The DHCP client broadcasts DHCPDISCOVER, DHCPREQUEST and DHCPINFORM
+ * messages, unless the client knows the address of a DHCP server.
+ * The client unicasts DHCPRELEASE messages to the server. Because
+ * the client is declining the use of the IP address supplied by the server,
+ * the client broadcasts DHCPDECLINE messages.
+ *
+ * When the DHCP client knows the address of a DHCP server, in either
+ * INIT or REBOOTING state, the client may use that address
+ * in the DHCPDISCOVER or DHCPREQUEST rather than the IP broadcast address.
+ * The client may also use unicast to send DHCPINFORM messages
+ * to a known DHCP server. If the client receives no response to DHCP
+ * messages sent to the IP address of a known DHCP server, the DHCP
+ * client reverts to using the IP broadcast address.
+ */
+
+static int raw_bcast_from_client_config_ifindex(struct dhcp_packet *packet)
+{
+       return udhcp_send_raw_packet(packet,
+               /*src*/ INADDR_ANY, CLIENT_PORT,
+               /*dst*/ INADDR_BROADCAST, SERVER_PORT, MAC_BCAST_ADDR,
+               client_config.ifindex);
 }
 
 
+#if ENABLE_FEATURE_UDHCPC_ARPING
+/* Broadcast a DHCP decline message */
+int FAST_FUNC send_decline(uint32_t xid, uint32_t server, uint32_t requested)
+{
+       struct dhcp_packet packet;
+
+       init_packet(&packet, DHCPDECLINE);
+       packet.xid = xid;
+       add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
+       add_simple_option(packet.options, DHCP_SERVER_ID, server);
+
+       bb_info_msg("Sending decline...");
+
+       return raw_bcast_from_client_config_ifindex(&packet);
+}
+#endif
+
+
 /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
-int send_discover(unsigned long xid, unsigned long requested)
+int FAST_FUNC send_discover(uint32_t xid, uint32_t requested)
 {
-       struct dhcpMessage packet;
+       struct dhcp_packet packet;
 
        init_packet(&packet, DHCPDISCOVER);
        packet.xid = xid;
        if (requested)
                add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
 
-       add_requests(&packet);
-       LOG(LOG_DEBUG, "Sending discover...");
-       return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, 
-                               SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
+       /* Explicitly saying that we want RFC-compliant packets helps
+        * some buggy DHCP servers to NOT send bigger packets */
+       add_simple_option(packet.options, DHCP_MAX_SIZE, htons(576));
+
+       add_param_req_option(&packet);
+
+       bb_info_msg("Sending discover...");
+       return raw_bcast_from_client_config_ifindex(&packet);
 }
 
 
-/* Broadcasts a DHCP request message */
-int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
+/* Broadcast a DHCP request message */
+/* RFC 2131 3.1 paragraph 3:
+ * "The client _broadcasts_ a DHCPREQUEST message..."
+ */
+int FAST_FUNC send_select(uint32_t xid, uint32_t server, uint32_t requested)
 {
-       struct dhcpMessage packet;
+       struct dhcp_packet packet;
        struct in_addr addr;
 
        init_packet(&packet, DHCPREQUEST);
@@ -132,122 +158,116 @@ int send_selecting(unsigned long xid, unsigned long server, unsigned long reques
 
        add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
        add_simple_option(packet.options, DHCP_SERVER_ID, server);
-       
-       add_requests(&packet);
+       add_param_req_option(&packet);
+
        addr.s_addr = requested;
-       LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr));
-       return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, 
-                               SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
+       bb_info_msg("Sending select for %s...", inet_ntoa(addr));
+       return raw_bcast_from_client_config_ifindex(&packet);
 }
 
 
 /* Unicasts or broadcasts a DHCP renew message */
-int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
+int FAST_FUNC send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr)
 {
-       struct dhcpMessage packet;
-       int ret = 0;
+       struct dhcp_packet packet;
 
        init_packet(&packet, DHCPREQUEST);
        packet.xid = xid;
        packet.ciaddr = ciaddr;
 
-       add_requests(&packet);
-       LOG(LOG_DEBUG, "Sending renew...");
-       if (server) 
-               ret = kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
-       else ret = raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
-                               SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
-       return ret;
-}      
+       add_param_req_option(&packet);
+       bb_info_msg("Sending renew...");
+       if (server)
+               return udhcp_send_kernel_packet(&packet,
+                       ciaddr, CLIENT_PORT,
+                       server, SERVER_PORT);
+
+       return raw_bcast_from_client_config_ifindex(&packet);
+}
 
 
 /* Unicasts a DHCP release message */
-int send_release(unsigned long server, unsigned long ciaddr)
+int FAST_FUNC send_release(uint32_t server, uint32_t ciaddr)
 {
-       struct dhcpMessage packet;
+       struct dhcp_packet packet;
 
        init_packet(&packet, DHCPRELEASE);
        packet.xid = random_xid();
        packet.ciaddr = ciaddr;
-       
-       add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
+
        add_simple_option(packet.options, DHCP_SERVER_ID, server);
 
-       LOG(LOG_DEBUG, "Sending release...");
-       return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
+       bb_info_msg("Sending release...");
+       return udhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
 }
 
 
-/* return -1 on errors that are fatal for the socket, -2 for those that aren't */
-int get_raw_packet(struct dhcpMessage *payload, int fd)
+/* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */
+int FAST_FUNC udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd)
 {
        int bytes;
-       struct udp_dhcp_packet packet;
-       u_int32_t source, dest;
-       u_int16_t check;
+       struct ip_udp_dhcp_packet packet;
+       uint16_t check;
 
-       memset(&packet, 0, sizeof(struct udp_dhcp_packet));
-       bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
+       memset(&packet, 0, sizeof(packet));
+       bytes = safe_read(fd, &packet, sizeof(packet));
        if (bytes < 0) {
-               DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring");
-               usleep(500000); /* possible down interface, looping condition */
-               return -1;
+               log1("Packet read error, ignoring");
+               /* NB: possible down interface, etc. Caller should pause. */
+               return bytes; /* returns -1 */
        }
-       
-       if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
-               DEBUG(LOG_INFO, "message too short, ignoring");
+
+       if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp))) {
+               log1("Packet is too short, ignoring");
                return -2;
        }
-       
+
        if (bytes < ntohs(packet.ip.tot_len)) {
-               DEBUG(LOG_INFO, "Truncated packet");
+               /* packet is bigger than sizeof(packet), we did partial read */
+               log1("Oversized packet, ignoring");
                return -2;
        }
-       
+
        /* ignore any extra garbage bytes */
        bytes = ntohs(packet.ip.tot_len);
-       
-       /* Make sure its the right packet for us, and that it passes sanity checks */
-       if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
-           packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
-           bytes > (int) sizeof(struct udp_dhcp_packet) ||
-           ntohs(packet.udp.len) != (short) (bytes - sizeof(packet.ip))) {
-               DEBUG(LOG_INFO, "unrelated/bogus packet");
-               return -2;
+
+       /* make sure its the right packet for us, and that it passes sanity checks */
+       if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION
+        || packet.ip.ihl != (sizeof(packet.ip) >> 2)
+        || packet.udp.dest != htons(CLIENT_PORT)
+       /* || bytes > (int) sizeof(packet) - can't happen */
+        || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip))
+       ) {
+               log1("Unrelated/bogus packet, ignoring");
+               return -2;
        }
 
-       /* check IP checksum */
+       /* verify IP checksum */
        check = packet.ip.check;
        packet.ip.check = 0;
-       if (check != checksum(&(packet.ip), sizeof(packet.ip))) {
-               DEBUG(LOG_INFO, "bad IP header checksum, ignoring");
-               return -1;
+       if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) {
+               log1("Bad IP header checksum, ignoring");
+               return -2;
        }
-       
-       /* verify the UDP checksum by replacing the header with a psuedo header */
-       source = packet.ip.saddr;
-       dest = packet.ip.daddr;
+
+       /* verify UDP checksum. IP header has to be modified for this */
+       memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
+       /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
+       packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
        check = packet.udp.check;
        packet.udp.check = 0;
-       memset(&packet.ip, 0, sizeof(packet.ip));
-
-       packet.ip.protocol = IPPROTO_UDP;
-       packet.ip.saddr = source;
-       packet.ip.daddr = dest;
-       packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
-       if (check && check != checksum(&packet, bytes)) {
-               DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring");
+       if (check && check != udhcp_checksum(&packet, bytes)) {
+               log1("Packet with bad UDP checksum received, ignoring");
                return -2;
        }
-       
-       memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
-       
-       if (ntohl(payload->cookie) != DHCP_MAGIC) {
-               LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring");
+
+       memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
+
+       if (dhcp_pkt->cookie != htonl(DHCP_MAGIC)) {
+               bb_info_msg("Packet with bad magic, ignoring");
                return -2;
        }
-       DEBUG(LOG_INFO, "oooooh!!! got some!");
+       log1("Got valid DHCP packet");
+       udhcp_dump_packet(dhcp_pkt);
        return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
-       
 }
-