"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 3 | * Packet ops |
| 4 | * |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 5 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 6 | * |
| 7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 8 | */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 9 | #include <netinet/in.h> |
Denis Vlasenko | 83e5d6f | 2006-12-18 21:49:06 +0000 | [diff] [blame] | 10 | #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 11 | # include <netpacket/packet.h> |
| 12 | # include <net/ethernet.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 13 | #else |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 14 | # include <asm/types.h> |
| 15 | # include <linux/if_packet.h> |
| 16 | # include <linux/if_ether.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 17 | #endif |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 18 | |
| 19 | #include "common.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 20 | #include "dhcpd.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 21 | |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 22 | void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 23 | { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 24 | memset(packet, 0, sizeof(*packet)); |
Denis Vlasenko | 739e30f | 2008-09-26 23:45:20 +0000 | [diff] [blame] | 25 | packet->op = BOOTREQUEST; /* if client to a server */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 26 | switch (type) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 27 | case DHCPOFFER: |
| 28 | case DHCPACK: |
| 29 | case DHCPNAK: |
Denis Vlasenko | 739e30f | 2008-09-26 23:45:20 +0000 | [diff] [blame] | 30 | packet->op = BOOTREPLY; /* if server to client */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 31 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 32 | packet->htype = 1; /* ethernet */ |
| 33 | packet->hlen = 6; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 34 | packet->cookie = htonl(DHCP_MAGIC); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 35 | if (DHCP_END != 0) |
| 36 | packet->options[0] = DHCP_END; |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 37 | udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Denys Vlasenko | 7a76eba | 2009-06-19 13:51:29 +0200 | [diff] [blame] | 40 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2 |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 41 | void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet) |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 42 | { |
| 43 | char buf[sizeof(packet->chaddr)*2 + 1]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 44 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 45 | if (dhcp_verbose < 2) |
| 46 | return; |
| 47 | |
| 48 | bb_info_msg( |
| 49 | //" op %x" |
| 50 | //" htype %x" |
| 51 | " hlen %x" |
| 52 | //" hops %x" |
| 53 | " xid %x" |
| 54 | //" secs %x" |
| 55 | //" flags %x" |
| 56 | " ciaddr %x" |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 57 | " yiaddr %x" |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 58 | " siaddr %x" |
| 59 | " giaddr %x" |
| 60 | //" chaddr %s" |
| 61 | //" sname %s" |
| 62 | //" file %s" |
| 63 | //" cookie %x" |
| 64 | //" options %s" |
| 65 | //, packet->op |
| 66 | //, packet->htype |
| 67 | , packet->hlen |
| 68 | //, packet->hops |
| 69 | , packet->xid |
| 70 | //, packet->secs |
| 71 | //, packet->flags |
| 72 | , packet->ciaddr |
| 73 | , packet->yiaddr |
| 74 | , packet->siaddr_nip |
| 75 | , packet->gateway_nip |
| 76 | //, packet->chaddr[16] |
| 77 | //, packet->sname[64] |
| 78 | //, packet->file[128] |
| 79 | //, packet->cookie |
| 80 | //, packet->options[] |
| 81 | ); |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 82 | *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0'; |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 83 | bb_info_msg(" chaddr %s", buf); |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | /* Read a packet from socket fd, return -1 on read error, -2 on packet error */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 88 | int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 89 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 90 | int bytes; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 91 | unsigned char *vendor; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 93 | memset(packet, 0, sizeof(*packet)); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 94 | bytes = safe_read(fd, packet, sizeof(*packet)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 95 | if (bytes < 0) { |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 96 | log1("Packet read error, ignoring"); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 97 | return bytes; /* returns -1 */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Denis Vlasenko | 6884f66 | 2007-11-23 00:08:54 +0000 | [diff] [blame] | 100 | if (packet->cookie != htonl(DHCP_MAGIC)) { |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 101 | bb_info_msg("Packet with bad magic, ignoring"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 102 | return -2; |
| 103 | } |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 104 | log1("Received a packet"); |
| 105 | udhcp_dump_packet(packet); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 106 | |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 107 | if (packet->op == BOOTREQUEST) { |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 108 | vendor = udhcp_get_option(packet, DHCP_VENDOR); |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 109 | if (vendor) { |
| 110 | #if 0 |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 111 | static const char broken_vendors[][8] = { |
| 112 | "MSFT 98", |
| 113 | "" |
| 114 | }; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 115 | int i; |
| 116 | for (i = 0; broken_vendors[i][0]; i++) { |
Denys Vlasenko | 37a658c | 2010-03-23 15:43:08 +0100 | [diff] [blame] | 117 | if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)strlen(broken_vendors[i]) |
| 118 | && strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - OPT_DATA]) == 0 |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 119 | ) { |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 120 | log1("Broken client (%s), forcing broadcast replies", |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 121 | broken_vendors[i]); |
| 122 | packet->flags |= htons(BROADCAST_FLAG); |
| 123 | } |
| 124 | } |
| 125 | #else |
Denys Vlasenko | 37a658c | 2010-03-23 15:43:08 +0100 | [diff] [blame] | 126 | if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)(sizeof("MSFT 98")-1) |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 127 | && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0 |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 128 | ) { |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 129 | log1("Broken client (%s), forcing broadcast replies", "MSFT 98"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 130 | packet->flags |= htons(BROADCAST_FLAG); |
| 131 | } |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 132 | #endif |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | return bytes; |
| 137 | } |
| 138 | |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 139 | uint16_t FAST_FUNC udhcp_checksum(void *addr, int count) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 140 | { |
| 141 | /* Compute Internet Checksum for "count" bytes |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 142 | * beginning at location "addr". |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 143 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 144 | int32_t sum = 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 145 | uint16_t *source = (uint16_t *) addr; |
| 146 | |
| 147 | while (count > 1) { |
| 148 | /* This is the inner loop */ |
| 149 | sum += *source++; |
| 150 | count -= 2; |
| 151 | } |
| 152 | |
| 153 | /* Add left-over byte, if any */ |
| 154 | if (count > 0) { |
| 155 | /* Make sure that the left-over byte is added correctly both |
| 156 | * with little and big endian hosts */ |
| 157 | uint16_t tmp = 0; |
Denis Vlasenko | fff145d | 2007-12-20 21:11:38 +0000 | [diff] [blame] | 158 | *(uint8_t*)&tmp = *(uint8_t*)source; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 159 | sum += tmp; |
| 160 | } |
| 161 | /* Fold 32-bit sum to 16 bits */ |
| 162 | while (sum >> 16) |
| 163 | sum = (sum & 0xffff) + (sum >> 16); |
| 164 | |
| 165 | return ~sum; |
| 166 | } |
| 167 | |
Denis Vlasenko | 6198792 | 2007-12-21 16:32:30 +0000 | [diff] [blame] | 168 | /* Construct a ip/udp header for a packet, send packet */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 169 | int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt, |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 170 | uint32_t source_nip, int source_port, |
| 171 | uint32_t dest_nip, int dest_port, const uint8_t *dest_arp, |
Denis Vlasenko | c321b51 | 2008-09-26 16:29:12 +0000 | [diff] [blame] | 172 | int ifindex) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 173 | { |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 174 | struct sockaddr_ll dest_sll; |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 175 | struct ip_udp_dhcp_packet packet; |
Denys Vlasenko | b8b72f0 | 2010-05-31 00:45:09 +0200 | [diff] [blame] | 176 | unsigned padding; |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 177 | int fd; |
| 178 | int result = -1; |
| 179 | const char *msg; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 180 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 181 | fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); |
| 182 | if (fd < 0) { |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 183 | msg = "socket(%s)"; |
| 184 | goto ret_msg; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 187 | memset(&dest_sll, 0, sizeof(dest_sll)); |
| 188 | memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data)); |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 189 | packet.data = *dhcp_pkt; /* struct copy */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 190 | |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 191 | dest_sll.sll_family = AF_PACKET; |
| 192 | dest_sll.sll_protocol = htons(ETH_P_IP); |
| 193 | dest_sll.sll_ifindex = ifindex; |
| 194 | dest_sll.sll_halen = 6; |
| 195 | memcpy(dest_sll.sll_addr, dest_arp, 6); |
| 196 | |
| 197 | if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) { |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 198 | msg = "bind(%s)"; |
| 199 | goto ret_close; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Denys Vlasenko | b8b72f0 | 2010-05-31 00:45:09 +0200 | [diff] [blame] | 202 | /* We were sending full-sized DHCP packets (zero padded), |
| 203 | * but some badly configured servers were seen dropping them. |
| 204 | * Apparently they drop all DHCP packets >576 *ethernet* octets big, |
| 205 | * whereas they may only drop packets >576 *IP* octets big |
| 206 | * (which for typical Ethernet II means 590 octets: 6+6+2 + 576). |
| 207 | * |
| 208 | * In order to work with those buggy servers, |
| 209 | * we truncate packets after end option byte. |
| 210 | */ |
| 211 | padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options); |
| 212 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 213 | packet.ip.protocol = IPPROTO_UDP; |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 214 | packet.ip.saddr = source_nip; |
| 215 | packet.ip.daddr = dest_nip; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 216 | packet.udp.source = htons(source_port); |
| 217 | packet.udp.dest = htons(dest_port); |
Denis Vlasenko | fff145d | 2007-12-20 21:11:38 +0000 | [diff] [blame] | 218 | /* size, excluding IP header: */ |
Denys Vlasenko | b8b72f0 | 2010-05-31 00:45:09 +0200 | [diff] [blame] | 219 | packet.udp.len = htons(UPD_DHCP_SIZE - padding); |
Denis Vlasenko | fff145d | 2007-12-20 21:11:38 +0000 | [diff] [blame] | 220 | /* for UDP checksumming, ip.len is set to UDP packet len */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 221 | packet.ip.tot_len = packet.udp.len; |
Denys Vlasenko | b8b72f0 | 2010-05-31 00:45:09 +0200 | [diff] [blame] | 222 | packet.udp.check = udhcp_checksum(&packet, IP_UPD_DHCP_SIZE - padding); |
Denis Vlasenko | fff145d | 2007-12-20 21:11:38 +0000 | [diff] [blame] | 223 | /* but for sending, it is set to IP packet len */ |
Denys Vlasenko | b8b72f0 | 2010-05-31 00:45:09 +0200 | [diff] [blame] | 224 | packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE - padding); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 225 | packet.ip.ihl = sizeof(packet.ip) >> 2; |
| 226 | packet.ip.version = IPVERSION; |
| 227 | packet.ip.ttl = IPDEFTTL; |
Denis Vlasenko | fff145d | 2007-12-20 21:11:38 +0000 | [diff] [blame] | 228 | packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 229 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 230 | udhcp_dump_packet(dhcp_pkt); |
Denys Vlasenko | b8b72f0 | 2010-05-31 00:45:09 +0200 | [diff] [blame] | 231 | result = sendto(fd, &packet, IP_UPD_DHCP_SIZE - padding, /*flags:*/ 0, |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 232 | (struct sockaddr *) &dest_sll, sizeof(dest_sll)); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 233 | msg = "sendto"; |
| 234 | ret_close: |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 235 | close(fd); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 236 | if (result < 0) { |
| 237 | ret_msg: |
| 238 | bb_perror_msg(msg, "PACKET"); |
| 239 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 240 | return result; |
| 241 | } |
| 242 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 243 | /* Let the kernel do all the work for packet generation */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 244 | int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt, |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 245 | uint32_t source_nip, int source_port, |
| 246 | uint32_t dest_nip, int dest_port) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 247 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 248 | struct sockaddr_in client; |
Denys Vlasenko | b8b72f0 | 2010-05-31 00:45:09 +0200 | [diff] [blame] | 249 | unsigned padding; |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 250 | int fd; |
| 251 | int result = -1; |
| 252 | const char *msg; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 253 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 254 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 255 | if (fd < 0) { |
| 256 | msg = "socket(%s)"; |
| 257 | goto ret_msg; |
| 258 | } |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 259 | setsockopt_reuseaddr(fd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 260 | |
| 261 | memset(&client, 0, sizeof(client)); |
| 262 | client.sin_family = AF_INET; |
| 263 | client.sin_port = htons(source_port); |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 264 | client.sin_addr.s_addr = source_nip; |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 265 | if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) { |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 266 | msg = "bind(%s)"; |
| 267 | goto ret_close; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | memset(&client, 0, sizeof(client)); |
| 271 | client.sin_family = AF_INET; |
| 272 | client.sin_port = htons(dest_port); |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 273 | client.sin_addr.s_addr = dest_nip; |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 274 | if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) { |
| 275 | msg = "connect"; |
| 276 | goto ret_close; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 279 | udhcp_dump_packet(dhcp_pkt); |
Denys Vlasenko | b8b72f0 | 2010-05-31 00:45:09 +0200 | [diff] [blame] | 280 | |
| 281 | padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options); |
| 282 | result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 283 | msg = "write"; |
| 284 | ret_close: |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 285 | close(fd); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 286 | if (result < 0) { |
| 287 | ret_msg: |
| 288 | bb_perror_msg(msg, "UDP"); |
| 289 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 290 | return result; |
| 291 | } |