-
Notifications
You must be signed in to change notification settings - Fork 2
/
wpad_dhcp_posix.c
274 lines (226 loc) · 8.08 KB
/
wpad_dhcp_posix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>
#include <errno.h>
#ifdef _WIN32
# include <winsock2.h>
# include <ws2tcpip.h>
# include <windows.h>
#else
# include <arpa/inet.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <netdb.h>
# include <unistd.h>
#endif
#include "log.h"
#include "net_adapter.h"
#include "util.h"
#ifdef _WIN32
# define socketerr WSAGetLastError()
# define ssize_t int
#else
# define socketerr errno
# define SOCKET int
# define closesocket close
#endif
#define DHCP_SERVER_PORT (67)
#define DHCP_CLIENT_PORT (68)
#define DHCP_MAGIC ("\x63\x82\x53\x63")
#define DHCP_MAGIC_LEN (4)
#define DHCP_ACK (5)
#define DHCP_INFORM (8)
#define DHCP_BOOT_REQUEST (1)
#define DHCP_BOOT_REPLY (2)
#define DHCP_OPT_PAD (0)
#define DHCP_OPT_MSGTYPE (0x35)
#define DHCP_OPT_PARAMREQ (0x37)
#define DHCP_OPT_WPAD (0xfc)
#define DHCP_OPT_END (0xff)
#define DHCP_OPT_MIN_LENGTH (312)
#define ETHERNET_TYPE (1)
#define ETHERNET_LENGTH (6)
typedef struct dhcp_msg {
uint8_t op; /* operation */
uint8_t htype; /* hardware address type */
uint8_t hlen; /* hardware address len */
uint8_t hops; /* message hops */
uint32_t xid; /* transaction id */
uint16_t secs; /* seconds since protocol start */
uint16_t flags; /* 0 = unicast, 1 = broadcast */
uint32_t ciaddr; /* client IP */
uint32_t yiaddr; /* your IP */
uint32_t siaddr; /* server IP */
uint32_t giaddr; /* gateway IP */
uint8_t chaddr[16]; /* client hardware address */
uint8_t sname[64]; /* server name */
uint8_t file[128]; /* bootstrap file */
uint8_t options[DHCP_OPT_MIN_LENGTH];
} dhcp_msg;
typedef struct dhcp_option {
uint8_t type;
uint8_t length;
uint8_t value[1];
} dhcp_option;
static inline bool dhcp_check_magic(uint8_t *options) {
return memcmp(options, DHCP_MAGIC, DHCP_MAGIC_LEN) == 0;
}
static inline uint8_t *dhcp_copy_magic(uint8_t *options) {
memcpy(options, DHCP_MAGIC, DHCP_MAGIC_LEN);
return options + DHCP_MAGIC_LEN;
}
static inline uint8_t *dhcp_copy_option(uint8_t *options, dhcp_option *option) {
memcpy(options, &option->type, sizeof(option->type));
options += sizeof(option->type);
memcpy(options, &option->length, sizeof(option->length));
options += sizeof(option->length);
if (option->length) {
memcpy(options, option->value, option->length);
options += option->length;
}
return options;
}
static uint8_t *dhcp_get_option(dhcp_msg *reply, uint8_t type, uint8_t *length) {
uint8_t *opts = reply->options + DHCP_MAGIC_LEN;
uint8_t *opts_end = reply->options + sizeof(reply->options);
// Enumerate DHCP options
while (opts < opts_end && *opts != DHCP_OPT_END) {
if (*opts == DHCP_OPT_PAD) {
opts++;
continue;
}
// Parse option type and length
uint8_t opt_type = *opts++;
uint8_t opt_length = *opts++;
// Check if option type matches
if (opt_type == type) {
// Allocate buffer to return option value
uint8_t *value = calloc(opt_length + 1, sizeof(char));
if (value)
memcpy(value, opts, opt_length);
// Optionally return option length
if (length)
*length = opt_length;
return value;
}
opts += opt_length;
}
return NULL;
}
static bool dhcp_send_inform(SOCKET sfd, uint32_t xid, net_adapter_s *adapter) {
struct sockaddr_in address = {0};
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_NONE;
address.sin_port = htons(DHCP_SERVER_PORT);
// Construct request
struct dhcp_msg request = {0};
request.op = DHCP_BOOT_REQUEST;
request.htype = ETHERNET_TYPE;
request.hlen = adapter->mac_length;
if (request.hlen > sizeof(request.chaddr))
request.hlen = sizeof(request.chaddr);
memcpy(request.chaddr, adapter->mac, request.hlen);
request.xid = xid;
request.ciaddr = *(uint32_t *)adapter->ip;
request.yiaddr = *(uint32_t *)adapter->ip;
request.siaddr = *(uint32_t *)adapter->dhcp;
uint8_t *opts = request.options;
// Construct request signature
opts = dhcp_copy_magic(opts);
// Construct request options
dhcp_option opt_msg_type = {DHCP_OPT_MSGTYPE, 1, {DHCP_INFORM}};
opts = dhcp_copy_option(opts, &opt_msg_type);
dhcp_option opt_param_req = {DHCP_OPT_PARAMREQ, 1, {DHCP_OPT_WPAD}};
opts = dhcp_copy_option(opts, &opt_param_req);
dhcp_option opt_end = {DHCP_OPT_END, 0, {0}};
opts = dhcp_copy_option(opts, &opt_end);
// Broadcast DHCP request
const ssize_t request_len = (ssize_t)(opts - (uint8_t *)&request);
const ssize_t sent =
sendto(sfd, (const char *)&request, request_len, 0, (struct sockaddr *)&address, sizeof(address));
return sent == request_len;
}
static bool dhcp_read_reply(SOCKET sfd, uint32_t request_xid, dhcp_msg *reply) {
const ssize_t response_len = recvfrom(sfd, (char *)reply, sizeof(dhcp_msg), 0, NULL, NULL);
if (response_len <= (ssize_t)(sizeof(dhcp_msg) - DHCP_OPT_MIN_LENGTH)) {
LOG_DEBUG("Unable to read DHCP reply (%d:%d)\n", (int32_t)response_len, socketerr);
return false;
}
if (reply->op != DHCP_BOOT_REPLY) {
LOG_DEBUG("Invalid DHCP reply operation (%" PRId32 ")\n", (int32_t)reply->op);
return false;
}
if (reply->xid != request_xid) {
LOG_ERROR("Invalid DHCP reply transaction id (%" PRIx32 ")\n", reply->xid);
return false;
}
if (!dhcp_check_magic(reply->options)) {
LOG_ERROR("Invalid DHCP reply magic (%" PRIx32 ")\n", *(uint32_t *)reply->options);
return false;
}
return true;
}
char *wpad_dhcp_adapter_posix(uint8_t bind_ip[4], net_adapter_s *adapter, int32_t timeout_sec) {
SOCKET sfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if ((int)sfd == -1) {
LOG_ERROR("Unable to create udp socket\n");
return NULL;
}
int broadcast = 1;
setsockopt(sfd, SOL_SOCKET, SO_BROADCAST, (const char *)&broadcast, sizeof(broadcast));
int reuseaddr = 1;
setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuseaddr, sizeof(reuseaddr));
struct timeval tv = {timeout_sec, 0};
setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof(tv));
struct sockaddr_in address = {0};
address.sin_family = AF_INET;
address.sin_addr.s_addr = *(uint32_t *)bind_ip;
address.sin_port = htons(DHCP_CLIENT_PORT);
int err = bind(sfd, (struct sockaddr *)&address, sizeof(address));
if (err == -1) {
// Likely can't bind to protected port, try again with random port
if (socketerr == EACCES) {
address.sin_port = 0;
err = bind(sfd, (struct sockaddr *)&address, sizeof(address));
}
if (err == -1) {
LOG_DEBUG("Unable to bind udp socket (%d)\n", socketerr);
closesocket(sfd);
return NULL;
}
}
// Generate random transaction id
srand((int)time(NULL));
uint32_t request_xid = rand();
// Send DHCPINFORM request to DHCP server
if (!dhcp_send_inform(sfd, request_xid, adapter)) {
LOG_ERROR("Unable to send DHCP inform\n");
closesocket(sfd);
return NULL;
}
// Read reply from DHCP server
dhcp_msg reply = {0};
bool is_ok = dhcp_read_reply(sfd, request_xid, &reply);
closesocket(sfd);
if (!is_ok)
return NULL;
// Parse options in DHCP reply
uint8_t opt_length = 0;
uint8_t *opt = NULL;
opt = dhcp_get_option(&reply, DHCP_OPT_MSGTYPE, &opt_length);
if (opt_length != 1 || *opt != DHCP_ACK) {
LOG_ERROR("Invalid DHCP reply (msgtype=%d)\n", *opt);
return NULL;
}
free(opt);
opt = dhcp_get_option(&reply, DHCP_OPT_WPAD, &opt_length);
if (opt_length <= 0) {
LOG_ERROR("Invalid DHCP reply (optlen=%d)\n", opt_length);
return NULL;
}
return (char *)opt;
}