busybox/networking/udhcp/packet.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Packet ops
   4 *
   5 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
   6 *
   7 * Licensed under GPLv2, see file LICENSE in this source tree.
   8 */
   9#include "common.h"
  10#include "dhcpd.h"
  11#include <netinet/in.h>
  12#include <netinet/if_ether.h>
  13#include <netpacket/packet.h>
  14
  15#if ENABLE_UDHCPC || ENABLE_UDHCPD
  16void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type)
  17{
  18        memset(packet, 0, sizeof(*packet));
  19        packet->op = BOOTREQUEST; /* if client to a server */
  20        switch (type) {
  21        case DHCPOFFER:
  22        case DHCPACK:
  23        case DHCPNAK:
  24                packet->op = BOOTREPLY; /* if server to client */
  25        }
  26        packet->htype = 1; /* ethernet */
  27        packet->hlen = 6;
  28        packet->cookie = htonl(DHCP_MAGIC);
  29        if (DHCP_END != 0)
  30                packet->options[0] = DHCP_END;
  31        udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
  32}
  33#endif
  34
  35#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
  36void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
  37{
  38        char buf[sizeof(packet->chaddr)*2 + 1];
  39
  40        if (dhcp_verbose < 2)
  41                return;
  42
  43        bb_info_msg(
  44                //" op %x"
  45                //" htype %x"
  46                " hlen %x"
  47                //" hops %x"
  48                " xid %x"
  49                //" secs %x"
  50                //" flags %x"
  51                " ciaddr %x"
  52                " yiaddr %x"
  53                " siaddr %x"
  54                " giaddr %x"
  55                //" sname %s"
  56                //" file %s"
  57                //" cookie %x"
  58                //" options %s"
  59                //, packet->op
  60                //, packet->htype
  61                , packet->hlen
  62                //, packet->hops
  63                , packet->xid
  64                //, packet->secs
  65                //, packet->flags
  66                , packet->ciaddr
  67                , packet->yiaddr
  68                , packet->siaddr_nip
  69                , packet->gateway_nip
  70                //, packet->sname[64]
  71                //, packet->file[128]
  72                //, packet->cookie
  73                //, packet->options[]
  74        );
  75        *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
  76        bb_info_msg(" chaddr %s", buf);
  77}
  78#endif
  79
  80/* Read a packet from socket fd, return -1 on read error, -2 on packet error */
  81int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd)
  82{
  83        int bytes;
  84
  85        memset(packet, 0, sizeof(*packet));
  86        bytes = safe_read(fd, packet, sizeof(*packet));
  87        if (bytes < 0) {
  88                log1s("packet read error, ignoring");
  89                return bytes; /* returns -1 */
  90        }
  91
  92        if (bytes < offsetof(struct dhcp_packet, options)
  93         || packet->cookie != htonl(DHCP_MAGIC)
  94        ) {
  95                bb_simple_info_msg("packet with bad magic, ignoring");
  96                return -2;
  97        }
  98        log2("received %s", "a packet");
  99        /* log2 because more informative msg for valid packets is printed later at log1 level */
 100        udhcp_dump_packet(packet);
 101
 102        return bytes;
 103}
 104
 105/* Construct a ip/udp header for a packet, send packet */
 106int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
 107                uint32_t source_nip, int source_port,
 108                uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
 109                int ifindex)
 110{
 111        struct sockaddr_ll dest_sll;
 112        struct ip_udp_dhcp_packet packet;
 113        unsigned padding;
 114        int fd;
 115        int result = -1;
 116        const char *msg;
 117
 118        fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
 119        if (fd < 0) {
 120                msg = "socket(%s)";
 121                goto ret_msg;
 122        }
 123
 124        memset(&dest_sll, 0, sizeof(dest_sll));
 125        memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
 126        packet.data = *dhcp_pkt; /* struct copy */
 127
 128        dest_sll.sll_family = AF_PACKET;
 129        dest_sll.sll_protocol = htons(ETH_P_IP);
 130        dest_sll.sll_ifindex = ifindex;
 131        /*dest_sll.sll_hatype = ARPHRD_???;*/
 132        /*dest_sll.sll_pkttype = PACKET_???;*/
 133        dest_sll.sll_halen = 6;
 134        memcpy(dest_sll.sll_addr, dest_arp, 6);
 135
 136        if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
 137                msg = "bind(%s)";
 138                goto ret_close;
 139        }
 140
 141        /* We were sending full-sized DHCP packets (zero padded),
 142         * but some badly configured servers were seen dropping them.
 143         * Apparently they drop all DHCP packets >576 *ethernet* octets big,
 144         * whereas they may only drop packets >576 *IP* octets big
 145         * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
 146         *
 147         * In order to work with those buggy servers,
 148         * we truncate packets after end option byte.
 149         *
 150         * However, RFC 1542 says "The IP Total Length and UDP Length
 151         * must be large enough to contain the minimal BOOTP header of 300 octets".
 152         * Thus, we retain enough padding to not go below 300 BOOTP bytes.
 153         * Some devices have filters which drop DHCP packets shorter than that.
 154         */
 155        padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
 156        if (padding > DHCP_SIZE - 300)
 157                padding = DHCP_SIZE - 300;
 158
 159        packet.ip.protocol = IPPROTO_UDP;
 160        packet.ip.saddr = source_nip;
 161        packet.ip.daddr = dest_nip;
 162        packet.udp.source = htons(source_port);
 163        packet.udp.dest = htons(dest_port);
 164        /* size, excluding IP header: */
 165        packet.udp.len = htons(UDP_DHCP_SIZE - padding);
 166        /* for UDP checksumming, ip.len is set to UDP packet len */
 167        packet.ip.tot_len = packet.udp.len;
 168        packet.udp.check = inet_cksum(&packet,
 169                        IP_UDP_DHCP_SIZE - padding);
 170        /* but for sending, it is set to IP packet len */
 171        packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
 172        packet.ip.ihl = sizeof(packet.ip) >> 2;
 173        packet.ip.version = IPVERSION;
 174        packet.ip.ttl = IPDEFTTL;
 175        packet.ip.check = inet_cksum(&packet.ip, sizeof(packet.ip));
 176
 177        udhcp_dump_packet(dhcp_pkt);
 178        result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
 179                        (struct sockaddr *) &dest_sll, sizeof(dest_sll));
 180        msg = "sendto";
 181 ret_close:
 182        close(fd);
 183        if (result < 0) {
 184 ret_msg:
 185                bb_perror_msg(msg, "PACKET");
 186        }
 187        return result;
 188}
 189
 190/* Let the kernel do all the work for packet generation */
 191int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
 192                uint32_t source_nip, int source_port,
 193                uint32_t dest_nip, int dest_port,
 194                const char *ifname)
 195{
 196        struct sockaddr_in sa;
 197        unsigned padding;
 198        int fd;
 199        int result = -1;
 200        const char *msg;
 201
 202        fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
 203        if (fd < 0) {
 204                msg = "socket(%s)";
 205                goto ret_msg;
 206        }
 207        setsockopt_reuseaddr(fd);
 208
 209        /* If interface carrier goes down, unless we
 210         * bind socket to a particular netdev, the packet
 211         * can go out through another interface, eg. via
 212         * default route despite being bound to a specific
 213         * source IP. As such, bind to device hard and fail
 214         * otherwise. Sending renewal packets on foreign
 215         * interfaces makes no sense.
 216         */
 217        if (ifname) {
 218                if (setsockopt_bindtodevice(fd, ifname) < 0) {
 219                        msg = "bindtodevice";
 220                        goto ret_close;
 221                }
 222        }
 223
 224        memset(&sa, 0, sizeof(sa));
 225        sa.sin_family = AF_INET;
 226        sa.sin_port = htons(source_port);
 227        sa.sin_addr.s_addr = source_nip;
 228        if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
 229                msg = "bind(%s)";
 230                goto ret_close;
 231        }
 232
 233        memset(&sa, 0, sizeof(sa));
 234        sa.sin_family = AF_INET;
 235        sa.sin_port = htons(dest_port);
 236        sa.sin_addr.s_addr = dest_nip;
 237        if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
 238                msg = "connect";
 239                goto ret_close;
 240        }
 241
 242        udhcp_dump_packet(dhcp_pkt);
 243        padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
 244        if (padding > DHCP_SIZE - 300)
 245                padding = DHCP_SIZE - 300;
 246        result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
 247        msg = "write";
 248 ret_close:
 249        close(fd);
 250        if (result < 0) {
 251 ret_msg:
 252                bb_perror_msg(msg, "UDP");
 253        }
 254        return result;
 255}
 256