forked from sem-hub/dhcprelya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option82_plugin.c
333 lines (304 loc) · 9.89 KB
/
option82_plugin.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* Copyright (c) 2007-2012 Sergey Matveychuk Yandex, LLC. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. 2.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution. 4. Neither the name
* of the company nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. */
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "dhcprelya.h"
static char rid[255];
static int rid_len, drop_untrusted = 1, never_strip_answer = 0, always_strip_answer = 0;
STAILQ_HEAD(thead, trusted_circuits) trusted_head;
struct trusted_circuits {
uint8_t *id;
int len;
STAILQ_ENTRY(trusted_circuits) next;
};
/* returns: NULL if optin82 was not found. *optins point to End-Of-Options
* mark (255) or pointer to option82 */
uint8_t *
find_option82(uint8_t *options)
{
uint8_t *p = NULL;
do {
switch (*options) {
case 0:
options++;
break;
default:
options += options[1] + 2;
}
} while (*options != 255 && *options != 82);
if (*options == 82)
p = options;
return p;
}
int
option82_plugin_init(plugin_options_head_t *options_head)
{
struct plugin_options *opts, *opts_tmp;
int i, n, rid_set = 0;
char *p, *p1;
struct trusted_circuits *tc_entry;
STAILQ_INIT(&trusted_head);
SLIST_FOREACH_SAFE(opts, options_head, next, opts_tmp) {
if ((p = strchr(opts->option_line, '=')) == NULL) {
logd(LOG_ERR, "option82_plugin: Syntax error at line: %s", opts->option_line);
return 0;
}
*p = '\0';
p++;
if (strcasecmp(opts->option_line, "drop_untrusted") == 0) {
if ((drop_untrusted = get_bool_value(p)) == -1) {
logd(LOG_ERR, "option82_plugin: Syntex error in option value at line: %s", opts->option_line);
return 0;
}
} else if (strcasecmp(opts->option_line, "remote_id") == 0) {
rid_set = 1;
rid_len = 0;
/* is a string */
if (*p == '"') {
p++;
for(rid_len=0; *p != '"' && *p != '\0' && rid_len<sizeof(rid); p++,rid_len++)
rid[rid_len] = *p;
if (*p != '"') {
logd(LOG_ERR, "option82_plugin: Syntex error in option value at line: %s", opts->option_line);
return 0;
}
rid[rid_len] = '\0';
} else if (strcasecmp(p, "0x") == 0) {
p += 2;
logd(LOG_ERR, "option82_plugin: hexadecimal is not supported yet at line: %s", opts->option_line);
return 0;
} else {
logd(LOG_ERR, "option82_plugin: Syntex error in option value at line: %s", opts->option_line);
return 0;
}
} else if (strcasecmp(opts->option_line, "never_strip_answer") == 0) {
if ((never_strip_answer = get_bool_value(p)) == -1) {
logd(LOG_ERR, "option82_plugin: Syntex error in option value at line: %s", opts->option_line);
return 0;
}
} else if (strcasecmp(opts->option_line, "always_strip_answer") == 0) {
if ((always_strip_answer = get_bool_value(p)) == -1) {
logd(LOG_ERR, "option82_plugin: Syntex error in option value at line: %s", opts->option_line);
return 0;
}
if (never_strip_answer && always_strip_answer) {
logd(LOG_ERR, "option82_plugin: options never_strip_answer and always_strip_answer are mutually exclusive");
return 0;
}
} else if (strcasecmp(opts->option_line, "trusted_circuits") == 0) {
n = 1;
while ((p1 = strsep(&p, " \t")) != NULL) {
if (*p1 == '"') {
p1++;
tc_entry = malloc(sizeof(struct trusted_circuits));
if (tc_entry == NULL) {
logd(LOG_ERR, "option82_plugin: malloc error");
return 0;
}
tc_entry->id = malloc(strlen(p1));
if (tc_entry->id == NULL) {
logd(LOG_ERR, "option82_plugin: malloc error");
return 0;
}
for (i = 0; *p1 != '\0'; i++, p1++)
tc_entry->id[i] = *p1;
i--;
if (tc_entry->id[i] != '"') {
logd(LOG_ERR, "option82_plugin: value syntax error at line %d", opts->option_line);
return 0;
}
tc_entry->id[i] = '\0';
logd(LOG_DEBUG, "trusted circuit #%d: %s", n, tc_entry->id);
tc_entry->len = strlen((char *)tc_entry->id);
STAILQ_INSERT_TAIL(&trusted_head, tc_entry, next);
n++;
} else {
if (strncasecmp(p1, "0x", 2) == 0)
logd(LOG_ERR, "option82_plugin: hexadecial is not supported yet at line: %d", opts->option_line);
else
logd(LOG_ERR, "option82_plugin: value syntax error at line %d", opts->option_line);
return 0;
}
}
} else {
logd(LOG_ERR, "option82_plugin: Unknown option at line: %s", opts->option_line);
return 0;
}
free(opts->option_line);
SLIST_REMOVE(options_head, opts, plugin_options, next);
free(opts);
}
if (!rid_set) {
if (gethostname(rid, sizeof(rid)) == -1) {
logd(LOG_ERR, "option82_plugin: Can't get a hostname");
return 0;
}
rid_len = strlen(rid);
}
logd(LOG_DEBUG, "option82_plugin: Agent Remote ID: %s", rid);
return 1;
}
int
option82_plugin_client_request(const struct interface *intf,
uint8_t **packet, size_t *psize)
{
struct dhcp_packet *dhcp;
uint8_t *buf, *p, *opt;
int intf_name_len, match;
struct trusted_circuits *tc_entry;
dhcp = (struct dhcp_packet *)(*packet + ETHER_HDR_LEN + DHCP_UDP_OVERHEAD);
p = dhcp->options;
p += DHCP_COOKIE_LEN;
opt = find_option82(p);
/* XXX discard if GIADDR spoofing (our address) */
if (*((ip_addr_t *)&dhcp->giaddr) == 0 && opt != NULL) {
logd(LOG_ERR, "option82_plugin: got a packet from an agent but GIADDR == 0. Dropped.");
return 0;
}
/* if we already have option82, check for trusted circuits. we'll not
* add own option82 if it's already there. */
if (opt) {
match = 0;
STAILQ_FOREACH(tc_entry, &trusted_head, next) {
if (tc_entry->len == rid_len && memcmp(tc_entry->id, rid, rid_len) == 0)
match = 1;
}
if (!match) {
logd(LOG_DEBUG, "option82_plugin: got a packet with option82 but from unknown circuit. Dropped.");
return 0;
}
} else {
/* Go to end of options mark */
while (*p != 255 && p - *packet <= *psize) {
p++;
}
/* Not found. Bad packet. */
if (p - *packet >= *psize) {
logd(LOG_ERR, "option82_plugin: Bad options format");
return 0;
}
intf_name_len = strlen(intf->name);
/* RFC3046 requires this check */
if (*psize + 4 + intf_name_len + rid_len > max_packet_size) {
logd(LOG_ERR, "option82_plugin: a packet will oversided after adding options82. Passed without changes.");
return 1;
}
buf = malloc(*psize + intf_name_len + rid_len + 6);
if (buf == NULL) {
logd(LOG_ERR, "option82_plugin: malloc error");
return 0;
}
memset(buf, 0, *psize + intf_name_len + rid_len + 6);
memcpy(buf, *packet, *psize);
p = buf + (p - *packet);
*(p++) = 82;
*(p++) = 4 + intf_name_len + rid_len;
*(p++) = 1;
*(p++) = intf_name_len;
memcpy(p, intf->name, intf_name_len);
p += intf_name_len;
*(p++) = 2;
*(p++) = rid_len;
memcpy(p, rid, rid_len);
p += rid_len;
/* End of options */
*p = 255;
p = *packet;
*packet = buf;
free(p);
*psize = *psize + intf_name_len + rid_len + 6;
}
return 1;
}
int
option82_plugin_send_to_client(const struct sockaddr_in *server,
const struct interface *intf, uint8_t **packet,
size_t *psize)
{
struct dhcp_packet *dhcp;
struct ip *ip;
struct udphdr *udp;
uint8_t *p, *opt;
int rlen, opt_size = 0, match, need_strip = 0;
struct trusted_circuits *tc_entry;
dhcp = (struct dhcp_packet *)(*packet + ETHER_HDR_LEN + DHCP_UDP_OVERHEAD);
ip = (struct ip *)(*packet + ETHER_HDR_LEN);
udp = (struct udphdr *)(*packet + ETHER_HDR_LEN + sizeof(struct ip));
opt = find_option82(dhcp->options + DHCP_COOKIE_LEN);
/* We don't find option82, drop the packet */
if (opt == NULL)
return 0;
/* Find Remote ID sub-option */
p = opt + 2;
if (*p != 1 && *p != 2) {
logd(LOG_ERR, "option82_plugin: bad sub-option. The packet dropped.");
return 0;
}
if (*p == 1)
p += *(p + 1) + 2;
p++;
rlen = *p++;
/* Check if Remote ID is our one */
match = 0;
if (rlen == rid_len && memcmp(rid, p, rid_len) == 0)
match = 1;
/* it's not our. check for trusted */
if (!match) {
match = 0;
STAILQ_FOREACH(tc_entry, &trusted_head, next) {
if (tc_entry->len == rlen && memcmp(tc_entry->id, p, rlen) == 0)
match = 1;
}
if (!match) {
*(p + rlen) = '\0';
logd(LOG_DEBUG, "option82_plugin: an answer from untrusted circuit: %s. Ignored", p);
return 0;
}
need_strip = 0;
} else if (!never_strip_answer)
need_strip = 1;
/* strip option82 */
if (need_strip || always_strip_answer) {
opt_size = opt[1] + 2;
memmove(opt, opt + opt_size, *psize - (opt + opt_size - *packet));
*psize -= opt_size;
udp->uh_ulen = htons(ntohs(udp->uh_ulen) - opt_size);
ip->ip_len = htons(ntohs(ip->ip_len) - opt_size);
ip->ip_sum = 0;
ip->ip_sum = htons(ip_checksum((const char *)ip, sizeof(struct ip)));
}
return 1;
}
struct plugin_data option82_plugin = {
"option82",
option82_plugin_init,
NULL,
option82_plugin_client_request,
NULL,
NULL,
option82_plugin_send_to_client
};