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_error_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_error_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                log1("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_error_msg("packet with bad magic, ignoring");
  96                return -2;
  97        }
  98        log1("received %s", "a packet");
  99        udhcp_dump_packet(packet);
 100
 101        return bytes;
 102}
 103
 104/* Construct a ip/udp header for a packet, send packet */
 105int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
 106                uint32_t source_nip, int source_port,
 107                uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
 108                int ifindex)
 109{
 110        struct sockaddr_ll dest_sll;
 111        struct ip_udp_dhcp_packet packet;
 112        unsigned padding;
 113        int fd;
 114        int result = -1;
 115        const char *msg;
 116
 117        fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
 118        if (fd < 0) {
 119                msg = "socket(%s)";
 120                goto ret_msg;
 121        }
 122
 123        memset(&dest_sll, 0, sizeof(dest_sll));
 124        memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
 125        packet.data = *dhcp_pkt; /* struct copy */
 126
 127        dest_sll.sll_family = AF_PACKET;
 128        dest_sll.sll_protocol = htons(ETH_P_IP);
 129        dest_sll.sll_ifindex = ifindex;
 130        /*dest_sll.sll_hatype = ARPHRD_???;*/
 131        /*dest_sll.sll_pkttype = PACKET_???;*/
 132        dest_sll.sll_halen = 6;
 133        memcpy(dest_sll.sll_addr, dest_arp, 6);
 134
 135        if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
 136                msg = "bind(%s)";
 137                goto ret_close;
 138        }
 139
 140        /* We were sending full-sized DHCP packets (zero padded),
 141         * but some badly configured servers were seen dropping them.
 142         * Apparently they drop all DHCP packets >576 *ethernet* octets big,
 143         * whereas they may only drop packets >576 *IP* octets big
 144         * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
 145         *
 146         * In order to work with those buggy servers,
 147         * we truncate packets after end option byte.
 148         *
 149         * However, RFC 1542 says "The IP Total Length and UDP Length
 150         * must be large enough to contain the minimal BOOTP header of 300 octets".
 151         * Thus, we retain enough padding to not go below 300 BOOTP bytes.
 152         * Some devices have filters which drop DHCP packets shorter than that.
 153         */
 154        padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
 155        if (padding > DHCP_SIZE - 300)
 156                padding = DHCP_SIZE - 300;
 157
 158        packet.ip.protocol = IPPROTO_UDP;
 159        packet.ip.saddr = source_nip;
 160        packet.ip.daddr = dest_nip;
 161        packet.udp.source = htons(source_port);
 162        packet.udp.dest = htons(dest_port);
 163        /* size, excluding IP header: */
 164        packet.udp.len = htons(UDP_DHCP_SIZE - padding);
 165        /* for UDP checksumming, ip.len is set to UDP packet len */
 166        packet.ip.tot_len = packet.udp.len;
 167        packet.udp.check = inet_cksum((uint16_t *)&packet,
 168                        IP_UDP_DHCP_SIZE - padding);
 169        /* but for sending, it is set to IP packet len */
 170        packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
 171        packet.ip.ihl = sizeof(packet.ip) >> 2;
 172        packet.ip.version = IPVERSION;
 173        packet.ip.ttl = IPDEFTTL;
 174        packet.ip.check = inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip));
 175
 176        udhcp_dump_packet(dhcp_pkt);
 177        result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
 178                        (struct sockaddr *) &dest_sll, sizeof(dest_sll));
 179        msg = "sendto";
 180 ret_close:
 181        close(fd);
 182        if (result < 0) {
 183 ret_msg:
 184                bb_perror_msg(msg, "PACKET");
 185        }
 186        return result;
 187}
 188
 189/* Let the kernel do all the work for packet generation */
 190int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
 191                uint32_t source_nip, int source_port,
 192                uint32_t dest_nip, int dest_port)
 193{
 194        struct sockaddr_in sa;
 195        unsigned padding;
 196        int fd;
 197        int result = -1;
 198        const char *msg;
 199
 200        fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
 201        if (fd < 0) {
 202                msg = "socket(%s)";
 203                goto ret_msg;
 204        }
 205        setsockopt_reuseaddr(fd);
 206
 207        memset(&sa, 0, sizeof(sa));
 208        sa.sin_family = AF_INET;
 209        sa.sin_port = htons(source_port);
 210        sa.sin_addr.s_addr = source_nip;
 211        if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
 212                msg = "bind(%s)";
 213                goto ret_close;
 214        }
 215
 216        memset(&sa, 0, sizeof(sa));
 217        sa.sin_family = AF_INET;
 218        sa.sin_port = htons(dest_port);
 219        sa.sin_addr.s_addr = dest_nip;
 220        if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
 221                msg = "connect";
 222                goto ret_close;
 223        }
 224
 225        udhcp_dump_packet(dhcp_pkt);
 226        padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
 227        if (padding > DHCP_SIZE - 300)
 228                padding = DHCP_SIZE - 300;
 229        result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
 230        msg = "write";
 231 ret_close:
 232        close(fd);
 233        if (result < 0) {
 234 ret_msg:
 235                bb_perror_msg(msg, "UDP");
 236        }
 237        return result;
 238}
 239