-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwpad_dhcp_posix.c
More file actions
236 lines (196 loc) · 6.99 KB
/
wpad_dhcp_posix.c
File metadata and controls
236 lines (196 loc) · 6.99 KB
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
#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 "testing.h"
#include "util.h"
#include "wpad_dhcp_posix.h"
#include "wpad_dhcp_posix_p.h"
#ifdef _WIN32
# define socketerr WSAGetLastError()
# define ssize_t int
#else
# define socketerr errno
# define SOCKET int
# define closesocket close
#endif
PROXYRES_TESTABLE bool dhcp_check_magic(uint8_t *options) {
return memcmp(options, DHCP_MAGIC, DHCP_MAGIC_LEN) == 0;
}
PROXYRES_TESTABLE uint8_t *dhcp_copy_magic(uint8_t *options) {
memcpy(options, DHCP_MAGIC, DHCP_MAGIC_LEN);
return options + DHCP_MAGIC_LEN;
}
PROXYRES_TESTABLE 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;
}
PROXYRES_TESTABLE 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++;
if (opts >= opts_end)
break;
uint8_t opt_length = *opts++;
// Validate option data fits within buffer
if (opt_length > opts_end - opts)
break;
// Check if option type matches
if (opt_type == type) {
// Allocate buffer to return option value
uint8_t *value = (uint8_t *)calloc(opt_length + 1, sizeof(uint8_t));
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)", (int32_t)response_len, socketerr);
return false;
}
if (reply->op != DHCP_BOOT_REPLY) {
log_debug("Invalid DHCP reply operation (%" PRId32 ")", (int32_t)reply->op);
return false;
}
if (reply->xid != request_xid) {
log_error("Invalid DHCP reply transaction id (%" PRIx32 ")", reply->xid);
return false;
}
if (!dhcp_check_magic(reply->options)) {
log_error("Invalid DHCP reply magic (%" PRIx32 ")", *(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");
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)", 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");
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)", *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)", opt_length);
return NULL;
}
return (char *)opt;
}