/
rlm_dhcpv4.c
232 lines (190 loc) · 5.99 KB
/
rlm_dhcpv4.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
/*
* This program is is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* $Id$
* @file rlm_dhcp.c
* @brief Will contain dhcp listener code.
*
* @copyright 2012 The FreeRADIUS server project
*/
RCSID("$Id$")
#include <freeradius-devel/libradius.h>
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/modules.h>
#include <freeradius-devel/dhcpv4/dhcpv4.h>
#include <ctype.h>
#define FR_DHCPV4_PARAMETER_REQUEST_LIST 55
/*
* Define a structure for our module configuration.
*
* These variables do not need to be in a structure, but it's
* a lot cleaner to do so, and a pointer to the structure can
* be used as the instance handle.
*/
typedef struct rlm_dhcp_t {
int nothing;
} rlm_dhcp_t;
/*
* Allow single attribute values to be retrieved from the dhcp.
*/
static ssize_t dhcp_options_xlat(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen,
UNUSED void const *mod_inst, UNUSED void const *xlat_inst,
REQUEST *request, char const *fmt)
{
vp_cursor_t cursor, src_cursor;
vp_tmpl_t *src;
VALUE_PAIR *vp, *head = NULL;
int decoded = 0;
ssize_t slen;
while (isspace((int) *fmt)) fmt++;
slen = tmpl_afrom_attr_str(request, &src, fmt, REQUEST_CURRENT, PAIR_LIST_REQUEST, false, false);
if (slen <= 0) {
REMARKER(fmt, slen, fr_strerror());
error:
talloc_free(src);
return -1;
}
if (src->type != TMPL_TYPE_ATTR) {
RPEDEBUG("dhcp_options cannot operate on a %s", fr_int2str(tmpl_names, src->type, "<INVALID>"));
goto error;
}
if (src->tmpl_da->type != FR_TYPE_OCTETS) {
REDEBUG("dhcp_options got a %s attribute needed octets",
fr_int2str(dict_attr_types, src->tmpl_da->type, "<INVALID>"));
goto error;
}
fr_pair_cursor_init(&cursor, &head);
for (vp = tmpl_cursor_init(NULL, &src_cursor, request, src);
vp;
vp = tmpl_cursor_next(&src_cursor, src)) {
uint8_t const *p = vp->vp_octets, *end = p + vp->vp_length;
ssize_t len;
VALUE_PAIR *vps = NULL;
vp_cursor_t options_cursor;
fr_pair_cursor_init(&options_cursor, &vps);
/*
* Loop over all the options data
*/
while (p < end) {
len = fr_dhcpv4_decode_option(request->packet, &options_cursor,
fr_dict_root(fr_dict_internal), p, end - p, NULL);
if (len <= 0) {
RWDEBUG("DHCP option decoding failed: %s", fr_strerror());
fr_pair_list_free(&head);
goto error;
}
p += len;
}
fr_pair_cursor_merge(&cursor, vps);
}
for (vp = fr_pair_cursor_first(&cursor);
vp;
vp = fr_pair_cursor_next(&cursor)) {
rdebug_pair(L_DBG_LVL_2, request, vp, "dhcp_options: ");
decoded++;
}
fr_pair_list_move(request->packet, &(request->packet->vps), &head);
/* Free any unmoved pairs */
fr_pair_list_free(&head);
snprintf(*out, outlen, "%i", decoded);
talloc_free(src);
return strlen(*out);
}
static ssize_t dhcp_xlat(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen,
UNUSED void const *mod_inst, UNUSED void const *xlat_inst,
REQUEST *request, char const *fmt)
{
vp_cursor_t cursor;
VALUE_PAIR *vp;
uint8_t binbuf[255];
ssize_t len;
while (isspace((int) *fmt)) fmt++;
if ((radius_copy_vp(request, &vp, request, fmt) < 0) || !vp) return 0;
fr_pair_cursor_init(&cursor, &vp);
len = fr_dhcpv4_encode_option(binbuf, sizeof(binbuf), &cursor, NULL);
talloc_free(vp);
if (len <= 0) {
RPEDEBUG("DHCP option encoding failed");
return -1;
}
if ((size_t)((len * 2) + 1) > outlen) {
REDEBUG("DHCP option encoding failed: Output buffer exhausted, needed %zd bytes, have %zd bytes",
(len * 2) + 1, outlen);
return -1;
}
return fr_bin2hex(*out, binbuf, len);
}
/*
* Instantiate the module.
*/
static int mod_bootstrap(void *instance, UNUSED CONF_SECTION *conf)
{
rlm_dhcp_t *inst = instance;
fr_dict_attr_t const *da;
xlat_register(inst, "dhcp_options", dhcp_options_xlat, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN);
xlat_register(inst, "dhcp", dhcp_xlat, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN);
/*
* Fixup dictionary entry for DHCP-Paramter-Request-List adding all the options
*/
da = fr_dict_attr_by_num(NULL, DHCP_MAGIC_VENDOR, FR_DHCPV4_PARAMETER_REQUEST_LIST);
if (da) {
fr_value_box_t value = { .type = FR_TYPE_UINT8 };
uint8_t i;
/* No padding or termination options */
DEBUG3("Adding values for %s", da->name);
for (i = 1; i < 255; i++) {
fr_dict_attr_t const *attr;
attr = fr_dict_attr_by_num(NULL, DHCP_MAGIC_VENDOR, i);
if (!attr) {
DEBUG3("No DHCP RFC space attribute at %i", i);
continue;
}
value.vb_uint8 = i;
DEBUG3("Adding %s value %i %s", da->name, i, attr->name);
if (fr_dict_enum_add_alias(da, attr->name, &value, true, false) < 0) {
DEBUG3("Failed adding value: %s", fr_strerror());
}
}
}
return 0;
}
static int dhcp_load(void)
{
int ret;
ret = fr_dict_read(main_config.dict, main_config.dictionary_dir, "dictionary.dhcp");
if (fr_dhcpv4_init() < 0) {
ERROR("%s", fr_strerror());
return -1;
}
return ret;
}
/*
* The module name should be the only globally exported symbol.
* That is, everything else should be 'static'.
*
* If the module needs to temporarily modify it's instantiation
* data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
* The server will then take care of ensuring that the module
* is single-threaded.
*/
extern rad_module_t rlm_dhcp;
rad_module_t rlm_dhcp = {
.magic = RLM_MODULE_INIT,
.name = "dhcp",
.inst_size = sizeof(rlm_dhcp_t),
.load = dhcp_load,
.bootstrap = mod_bootstrap,
};