The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
base.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 104d1181c94ac0a171e4b14f6587df38c8ecd62b $
19 * @file src/process/dhcpv4/base.c
20 * @brief Base DORA, etc. DHCPV4 processing.
21 *
22 * @copyright 2018 The Freeradius server project.
23 * @copyright 2018 Alan DeKok (aland@deployingradius.com)
24 */
25#define LOG_PREFIX "process_dhcpv4"
26
27#include <freeradius-devel/io/application.h>
28#include <freeradius-devel/server/protocol.h>
29#include <freeradius-devel/server/module_method.h>
30#include <freeradius-devel/util/dict.h>
31#include <freeradius-devel/util/debug.h>
32#include <freeradius-devel/dhcpv4/dhcpv4.h>
33
34static fr_dict_t const *dict_dhcpv4;
35
38 { .out = &dict_dhcpv4, .proto = "dhcpv4" },
39 { NULL }
40};
41
45
48 { .out = &attr_message_type, .name = "Message-Type", .type = FR_TYPE_UINT8, .dict = &dict_dhcpv4},
49 { .out = &attr_yiaddr, .name = "Your-IP-Address", .type = FR_TYPE_IPV4_ADDR, .dict = &dict_dhcpv4},
50 { .out = &attr_packet_type, .name = "Packet-Type", .type = FR_TYPE_UINT32, .dict = &dict_dhcpv4},
51 { NULL }
52};
53
54/*
55 * Debug the packet if requested.
56 */
57static void dhcpv4_packet_debug(request_t *request, fr_packet_t *packet, fr_pair_list_t *list, bool received)
58{
59#ifdef WITH_IFINDEX_NAME_RESOLUTION
60 char if_name[IFNAMSIZ];
61#endif
62
63 if (!packet) return;
64 if (!RDEBUG_ENABLED) return;
65
66 log_request(L_DBG, L_DBG_LVL_1, request, __FILE__, __LINE__, "%s %s XID %08x from %s%pV%s:%i to %s%pV%s:%i "
67#ifdef WITH_IFINDEX_NAME_RESOLUTION
68 "%s%s%s"
69#endif
70 "",
71 received ? "Received" : "Sending",
72 dhcp_message_types[packet->code],
73 packet->id,
74 packet->socket.inet.src_ipaddr.af == AF_INET6 ? "[" : "",
75 fr_box_ipaddr(packet->socket.inet.src_ipaddr),
76 packet->socket.inet.src_ipaddr.af == AF_INET6 ? "]" : "",
77 packet->socket.inet.src_port,
78 packet->socket.inet.dst_ipaddr.af == AF_INET6 ? "[" : "",
79 fr_box_ipaddr(packet->socket.inet.dst_ipaddr),
80 packet->socket.inet.dst_ipaddr.af == AF_INET6 ? "]" : "",
81 packet->socket.inet.dst_port
82#ifdef WITH_IFINDEX_NAME_RESOLUTION
83 , packet->socket.inet.ifindex ? "via " : "",
84 packet->socket.inet.ifindex ? fr_ifname_from_ifindex(if_name, packet->socket.inet.ifindex) : "",
85 packet->socket.inet.ifindex ? " " : ""
86#endif
87 );
88
89 if (received || request->parent) {
90 log_request_pair_list(L_DBG_LVL_1, request, NULL, list, NULL);
91 } else {
92 log_request_proto_pair_list(L_DBG_LVL_1, request, NULL, list, NULL);
93 }
94}
95
118
122
123#define FR_DHCP_PROCESS_CODE_VALID(_x) (FR_DHCP_PACKET_CODE_VALID(_x) || (_x == FR_DHCP_DO_NOT_RESPOND))
124
125#define PROCESS_PACKET_TYPE fr_dhcpv4_packet_code_t
126#define PROCESS_CODE_MAX FR_DHCP_CODE_MAX
127#define PROCESS_CODE_DO_NOT_RESPOND FR_DHCP_DO_NOT_RESPOND
128#define PROCESS_PACKET_CODE_VALID FR_DHCP_PROCESS_CODE_VALID
129#define PROCESS_INST process_dhcpv4_t
130#define PROCESS_CODE_DYNAMIC_CLIENT FR_DHCP_ACK
131#include <freeradius-devel/server/process.h>
132
133RESUME(check_yiaddr)
134{
135 fr_pair_t *vp;
136
137 vp = fr_pair_find_by_da(&request->reply_pairs, NULL, attr_yiaddr);
138 if (!vp) {
139 REDEBUG("%s packet does not have YIADDR. The client will not receive an IP address.",
140 dhcp_message_types[request->reply->code]);
141 }
142
143 return CALL_RESUME(send_generic);
144}
145
146static fr_process_state_t const process_state[] = {
147 [FR_DHCP_DISCOVER] = {
148 .packet_type = {
152
158 },
159 .rcode = RLM_MODULE_NOOP,
160 .default_reply = FR_DHCP_DO_NOT_RESPOND,
161 .recv = recv_generic,
162 .resume = resume_recv_generic,
163 .section_offset = PROCESS_CONF_OFFSET(discover),
164 },
165 [FR_DHCP_OFFER] = {
166 .packet_type = {
170
176 },
177 .rcode = RLM_MODULE_NOOP,
178 .default_reply = FR_DHCP_DO_NOT_RESPOND,
179 .send = send_generic,
180 .resume = resume_check_yiaddr,
181 .section_offset = PROCESS_CONF_OFFSET(offer),
182 },
183
184 [FR_DHCP_REQUEST] = {
185 .packet_type = {
189
195 },
196 .rcode = RLM_MODULE_NOOP,
197 .default_reply = FR_DHCP_DO_NOT_RESPOND,
198 .recv = recv_generic,
199 .resume = resume_recv_generic,
200 .section_offset = PROCESS_CONF_OFFSET(request),
201 },
202 [FR_DHCP_DECLINE] = {
203 .packet_type = {
207
213 },
214 .rcode = RLM_MODULE_NOOP,
215 .default_reply = FR_DHCP_DO_NOT_RESPOND,
216 .recv = recv_generic,
217 .resume = resume_recv_generic,
218 .section_offset = PROCESS_CONF_OFFSET(request),
219 },
220
221 [FR_DHCP_ACK] = {
222 .packet_type = {
226
232 },
233 .rcode = RLM_MODULE_NOOP,
234 .default_reply = FR_DHCP_DO_NOT_RESPOND,
235 .send = send_generic,
236 .resume = resume_check_yiaddr,
237 .section_offset = PROCESS_CONF_OFFSET(ack),
238 },
239 [FR_DHCP_NAK] = {
240 .packet_type = {
244
250 },
251 .rcode = RLM_MODULE_NOOP,
252 .default_reply = FR_DHCP_DO_NOT_RESPOND,
253 .send = send_generic,
254 .resume = resume_send_generic,
255 .section_offset = PROCESS_CONF_OFFSET(nak),
256 },
257
258 [FR_DHCP_INFORM] = {
259 .packet_type = {
263
269 },
270 .rcode = RLM_MODULE_NOOP,
271 .default_reply = FR_DHCP_DO_NOT_RESPOND,
272 .recv = recv_generic,
273 .resume = resume_recv_generic,
274 .section_offset = PROCESS_CONF_OFFSET(inform),
275 },
276
277 [FR_DHCP_RELEASE] = { /* releases are not responded to */
278 .packet_type = {
282
288 },
289 .rcode = RLM_MODULE_NOOP,
290 .default_reply = FR_DHCP_DO_NOT_RESPOND,
291 .recv = recv_generic,
292 .resume = resume_recv_generic,
293 .section_offset = PROCESS_CONF_OFFSET(release),
294 },
295
297 .packet_type = {
301
307 },
308 .rcode = RLM_MODULE_NOOP,
309 .default_reply = FR_DHCP_DO_NOT_RESPOND,
310 .recv = recv_generic,
311 .resume = resume_recv_generic,
312 .section_offset = PROCESS_CONF_OFFSET(lease_query),
313 },
314
316 .packet_type = {
320
326 },
327 .rcode = RLM_MODULE_NOOP,
328 .default_reply = FR_DHCP_DO_NOT_RESPOND,
329 .send = send_generic,
330 .resume = resume_send_generic,
331 .section_offset = PROCESS_CONF_OFFSET(lease_unassigned),
332 },
333
335 .packet_type = {
339
345 },
346 .rcode = RLM_MODULE_NOOP,
347 .default_reply = FR_DHCP_DO_NOT_RESPOND,
348 .send = send_generic,
349 .resume = resume_send_generic,
350 .section_offset = PROCESS_CONF_OFFSET(lease_unknown),
351 },
352
354 .packet_type = {
358
364 },
365 .rcode = RLM_MODULE_NOOP,
366 .default_reply = FR_DHCP_DO_NOT_RESPOND,
367 .send = send_generic,
368 .resume = resume_send_generic,
369 .section_offset = PROCESS_CONF_OFFSET(lease_active),
370 },
371
373 .packet_type = {
377
383 },
384 .rcode = RLM_MODULE_NOOP,
385 .default_reply = FR_DHCP_DO_NOT_RESPOND,
386 .send = send_generic,
387 .resume = resume_send_generic,
388 .section_offset = PROCESS_CONF_OFFSET(do_not_respond),
389 },
390};
391
392static unlang_action_t mod_process(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
393{
394 fr_process_state_t const *state;
395
397
399 fr_assert(PROCESS_PACKET_CODE_VALID(request->packet->code));
400
401 request->component = "dhcpv4";
402 request->module = NULL;
403 fr_assert(request->dict == dict_dhcpv4);
404
405 UPDATE_STATE(packet);
406
407 if (!state->recv) {
408 REDEBUG("Invalid packet type (%u)", request->packet->code);
410 }
411
412 dhcpv4_packet_debug(request, request->packet, &request->request_pairs, true);
413
414 if (unlikely(request_is_dynamic_client(request))) {
415 return new_client(p_result, mctx, request);
416 }
417
418 return state->recv(p_result, mctx, request);
419}
420
422 {
423 .section = SECTION_NAME("recv", "Discover"),
424 .actions = &mod_actions_postauth,
425
426 .methods = (const section_name_t *[]) {
428 NULL
429 },
430 .offset = PROCESS_CONF_OFFSET(discover),
431 },
432 {
433 .section = SECTION_NAME("send", "Offer"),
434 .actions = &mod_actions_postauth,
435 .offset = PROCESS_CONF_OFFSET(offer),
436 },
437 {
438 .section = SECTION_NAME("recv", "Request"),
440
441 .methods = (const section_name_t *[]) {
443 NULL
444 },
445 .offset = PROCESS_CONF_OFFSET(request),
446 },
447
448 {
449 .section = SECTION_NAME("send", "Ack"),
450 .actions = &mod_actions_postauth,
451 .offset = PROCESS_CONF_OFFSET(ack),
452 },
453 {
454 .section = SECTION_NAME("send", "NAK"),
456 .offset = PROCESS_CONF_OFFSET(nak),
457 },
458 {
459 .section = SECTION_NAME("recv", "Decline"),
461
462 .methods = (const section_name_t *[]) {
464 NULL
465 },
466 .offset = PROCESS_CONF_OFFSET(decline),
467 },
468
469 {
470 .section = SECTION_NAME("recv", "Release"),
471 .actions = &mod_actions_postauth,
472
473 .methods = (const section_name_t *[]) {
475 NULL
476 },
477 .offset = PROCESS_CONF_OFFSET(release),
478 },
479 {
480 .section = SECTION_NAME("recv", "Inform"),
481 .actions = &mod_actions_postauth,
482 .offset = PROCESS_CONF_OFFSET(inform),
483 },
484
485 {
486 .section = SECTION_NAME("recv", "Lease-Query"),
488 .offset = PROCESS_CONF_OFFSET(lease_query),
489 },
490 {
491 .section = SECTION_NAME("send", "Lease-Unassigned"),
493 .offset = PROCESS_CONF_OFFSET(lease_unassigned),
494 },
495 {
496 .section = SECTION_NAME("send", "Lease-Unknown"),
498 .offset = PROCESS_CONF_OFFSET(lease_unknown),
499 },
500 {
501 .section = SECTION_NAME("send", "Lease-Active"),
503 .offset = PROCESS_CONF_OFFSET(lease_active),
504 },
505
506 {
507 .section = SECTION_NAME("send", "Do-Not-Respond"),
509 .offset = PROCESS_CONF_OFFSET(do_not_respond),
510 },
511
512 DYNAMIC_CLIENT_SECTIONS,
513
515};
516
517
520 .common = {
521 .magic = MODULE_MAGIC_INIT,
522 .name = "dhcpv4",
523 .inst_size = sizeof(process_dhcpv4_t)
524 },
525 .process = mod_process,
526 .compile_list = compile_list,
527 .dict = &dict_dhcpv4
528};
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
#define unlikely(_x)
Definition build.h:381
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
@ FR_DHCP_REQUEST
Definition dhcpv4.h:47
@ FR_DHCP_LEASE_UNASSIGNED
Definition dhcpv4.h:55
@ FR_DHCP_DECLINE
Definition dhcpv4.h:48
@ FR_DHCP_OFFER
Definition dhcpv4.h:46
@ FR_DHCP_DISCOVER
Definition dhcpv4.h:45
@ FR_DHCP_LEASE_ACTIVE
Definition dhcpv4.h:57
@ FR_DHCP_LEASE_UNKNOWN
Definition dhcpv4.h:56
@ FR_DHCP_LEASE_QUERY
Definition dhcpv4.h:54
@ FR_DHCP_NAK
Definition dhcpv4.h:50
@ FR_DHCP_RELEASE
Definition dhcpv4.h:51
@ FR_DHCP_DO_NOT_RESPOND
Definition dhcpv4.h:61
@ FR_DHCP_INFORM
Definition dhcpv4.h:52
@ FR_DHCP_ACK
Definition dhcpv4.h:49
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:268
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:281
Specifies an attribute which must be present for the module to function.
Definition dict.h:267
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:280
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
fr_dict_attr_t const * attr_packet_type
Definition base.c:93
void log_request_proto_pair_list(fr_log_lvl_t lvl, request_t *request, fr_pair_t const *parent, fr_pair_list_t const *vps, char const *prefix)
Print a list of protocol fr_pair_ts.
Definition log.c:854
void log_request(fr_log_type_t type, fr_log_lvl_t lvl, request_t *request, char const *file, int line, char const *fmt,...)
Marshal variadic log arguments into a va_list and pass to normal logging functions.
Definition log.c:612
void log_request_pair_list(fr_log_lvl_t lvl, request_t *request, fr_pair_t const *parent, fr_pair_list_t const *vps, char const *prefix)
Print a fr_pair_list_t.
Definition log.c:830
@ L_DBG_LVL_1
Highest priority debug messages (-x).
Definition log.h:70
@ L_DBG
Only displayed when debugging is enabled.
Definition log.h:59
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_UINT8
8 Bit unsigned integer.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
unlang_mod_actions_t const mod_actions_postauth
Definition mod_action.c:88
unlang_mod_action_t actions[RLM_MODULE_NUMCODES]
Definition mod_action.h:62
module_instance_t const * mi
Instance of the module being instantiated.
Definition module_ctx.h:42
Temporary structure to hold arguments for module calls.
Definition module_ctx.h:41
section_name_t module_method_ippool_mark
section_name_t module_method_ippool_extend
section_name_t module_method_ippool_release
section_name_t module_method_ippool_allocate
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:693
static unlang_action_t mod_process(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Definition base.c:179
static const virtual_server_compile_t compile_list[]
Definition base.c:205
static fr_process_state_t const process_state[]
Definition base.c:68
#define PROCESS_PACKET_CODE_VALID
Definition base.c:64
CONF_SECTION * request
Definition base.c:101
CONF_SECTION * lease_query
Definition base.c:108
CONF_SECTION * ack
Definition base.c:103
CONF_SECTION * offer
Definition base.c:100
CONF_SECTION * lease_unassigned
Definition base.c:109
static void dhcpv4_packet_debug(request_t *request, fr_packet_t *packet, fr_pair_list_t *list, bool received)
Definition base.c:57
CONF_SECTION * do_not_respond
Definition base.c:112
CONF_SECTION * nak
Definition base.c:104
fr_process_module_t process_dhcpv4
Definition base.c:519
static fr_dict_attr_t const * attr_yiaddr
Definition base.c:43
CONF_SECTION * force_renew
Definition base.c:107
CONF_SECTION * decline
Definition base.c:102
CONF_SECTION * new_client
Definition base.c:114
fr_dict_attr_autoload_t process_dhcpv4_dict_attr[]
Definition base.c:47
static fr_dict_t const * dict_dhcpv4
Definition base.c:34
fr_dict_autoload_t process_dhcpv4_dict[]
Definition base.c:37
CONF_SECTION * add_client
Definition base.c:115
CONF_SECTION * inform
Definition base.c:106
CONF_SECTION * lease_active
Definition base.c:111
process_dhcpv4_sections_t sections
Definition base.c:120
CONF_SECTION * release
Definition base.c:105
CONF_SECTION * discover
Definition base.c:99
CONF_SECTION * deny_client
Definition base.c:116
CONF_SECTION * lease_unknown
Definition base.c:110
static fr_dict_attr_t const * attr_message_type
Definition base.c:42
#define PROCESS_TRACE
Trace each state function as it's entered.
Definition process.h:65
module_t common
Common fields for all loadable modules.
Definition process.h:55
Common public symbol definition for all process modules.
Definition process.h:54
char const * dhcp_message_types[]
Definition base.c:126
#define fr_assert(_expr)
Definition rad_assert.h:38
#define REDEBUG(fmt,...)
Definition radclient.h:52
#define RDEBUG_ENABLED()
Definition radclient.h:49
#define RETURN_MODULE_FAIL
Definition rcode.h:56
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:40
@ RLM_MODULE_INVALID
The module considers the request invalid.
Definition rcode.h:45
@ RLM_MODULE_OK
The module is OK, continue.
Definition rcode.h:43
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:42
@ RLM_MODULE_DISALLOW
Reject the request (user is locked out).
Definition rcode.h:46
@ RLM_MODULE_REJECT
Immediately reject the request.
Definition rcode.h:41
@ RLM_MODULE_NOTFOUND
User not found.
Definition rcode.h:47
@ RLM_MODULE_UPDATED
OK (pairs modified).
Definition rcode.h:49
@ RLM_MODULE_NOOP
Module succeeded without doing anything.
Definition rcode.h:48
#define request_is_dynamic_client(_x)
Definition request.h:162
#define SECTION_NAME(_name1, _name2)
Define a section name consisting of a verb and a noun.
Definition section.h:40
Section name identifier.
Definition section.h:44
size_t inst_size
Size of the module's instance data.
Definition module.h:203
void * data
Module's instance data.
Definition module.h:271
#define RESUME(_x)
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
#define talloc_get_type_abort_const
Definition talloc.h:282
unsigned int code
Packet code (type).
Definition packet.h:61
fr_socket_t socket
This packet was received on.
Definition packet.h:57
int id
Packet ID (used to link requests/responses).
Definition packet.h:60
int af
AF_INET, AF_INET6, or AF_UNIX.
Definition socket.h:78
#define fr_box_ipaddr(_val)
Definition value.h:294
#define COMPILE_TERMINATOR
section_name_t const * section
Identifier for the section.
Processing sections which are allowed in this virtual server.