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: b85f54de10b69d1949d22767693d1b026f648a6c $
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
159 },
160 .rcode = RLM_MODULE_NOOP,
161 .default_reply = FR_DHCP_DO_NOT_RESPOND,
162 .recv = recv_generic,
163 .resume = resume_recv_generic,
164 .section_offset = PROCESS_CONF_OFFSET(discover),
165 },
166 [FR_DHCP_OFFER] = {
167 .packet_type = {
171
178 },
179 .rcode = RLM_MODULE_NOOP,
180 .default_reply = FR_DHCP_DO_NOT_RESPOND,
181 .send = send_generic,
182 .resume = resume_check_yiaddr,
183 .section_offset = PROCESS_CONF_OFFSET(offer),
184 },
185
186 [FR_DHCP_REQUEST] = {
187 .packet_type = {
191
198 },
199 .rcode = RLM_MODULE_NOOP,
200 .default_reply = FR_DHCP_DO_NOT_RESPOND,
201 .recv = recv_generic,
202 .resume = resume_recv_generic,
203 .section_offset = PROCESS_CONF_OFFSET(request),
204 },
205 [FR_DHCP_DECLINE] = {
206 .packet_type = {
210
217 },
218 .rcode = RLM_MODULE_NOOP,
219 .default_reply = FR_DHCP_DO_NOT_RESPOND,
220 .recv = recv_generic,
221 .resume = resume_recv_generic,
222 .section_offset = PROCESS_CONF_OFFSET(request),
223 },
224
225 [FR_DHCP_ACK] = {
226 .packet_type = {
230
237 },
238 .rcode = RLM_MODULE_NOOP,
239 .default_reply = FR_DHCP_DO_NOT_RESPOND,
240 .send = send_generic,
241 .resume = resume_check_yiaddr,
242 .section_offset = PROCESS_CONF_OFFSET(ack),
243 },
244 [FR_DHCP_NAK] = {
245 .packet_type = {
249
256 },
257 .rcode = RLM_MODULE_NOOP,
258 .default_reply = FR_DHCP_DO_NOT_RESPOND,
259 .send = send_generic,
260 .resume = resume_send_generic,
261 .section_offset = PROCESS_CONF_OFFSET(nak),
262 },
263
264 [FR_DHCP_INFORM] = {
265 .packet_type = {
269
276 },
277 .rcode = RLM_MODULE_NOOP,
278 .default_reply = FR_DHCP_DO_NOT_RESPOND,
279 .recv = recv_generic,
280 .resume = resume_recv_generic,
281 .section_offset = PROCESS_CONF_OFFSET(inform),
282 },
283
284 [FR_DHCP_RELEASE] = { /* releases are not responded to */
285 .packet_type = {
289
296 },
297 .rcode = RLM_MODULE_NOOP,
298 .default_reply = FR_DHCP_DO_NOT_RESPOND,
299 .recv = recv_generic,
300 .resume = resume_recv_generic,
301 .section_offset = PROCESS_CONF_OFFSET(release),
302 },
303
305 .packet_type = {
309
316 },
317 .rcode = RLM_MODULE_NOOP,
318 .default_reply = FR_DHCP_DO_NOT_RESPOND,
319 .recv = recv_generic,
320 .resume = resume_recv_generic,
321 .section_offset = PROCESS_CONF_OFFSET(lease_query),
322 },
323
325 .packet_type = {
329
336 },
337 .rcode = RLM_MODULE_NOOP,
338 .default_reply = FR_DHCP_DO_NOT_RESPOND,
339 .send = send_generic,
340 .resume = resume_send_generic,
341 .section_offset = PROCESS_CONF_OFFSET(lease_unassigned),
342 },
343
345 .packet_type = {
349
356 },
357 .rcode = RLM_MODULE_NOOP,
358 .default_reply = FR_DHCP_DO_NOT_RESPOND,
359 .send = send_generic,
360 .resume = resume_send_generic,
361 .section_offset = PROCESS_CONF_OFFSET(lease_unknown),
362 },
363
365 .packet_type = {
369
376 },
377 .rcode = RLM_MODULE_NOOP,
378 .default_reply = FR_DHCP_DO_NOT_RESPOND,
379 .send = send_generic,
380 .resume = resume_send_generic,
381 .section_offset = PROCESS_CONF_OFFSET(lease_active),
382 },
383
385 .packet_type = {
389
396 },
397 .rcode = RLM_MODULE_NOOP,
398 .default_reply = FR_DHCP_DO_NOT_RESPOND,
399 .send = send_generic,
400 .resume = resume_send_generic,
401 .section_offset = PROCESS_CONF_OFFSET(do_not_respond),
402 },
403};
404
405static unlang_action_t mod_process(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
406{
407 fr_process_state_t const *state;
408
410
412 fr_assert(PROCESS_PACKET_CODE_VALID(request->packet->code));
413
414 request->component = "dhcpv4";
415 request->module = NULL;
416 fr_assert(request->proto_dict == dict_dhcpv4);
417
418 UPDATE_STATE(packet);
419
420 if (!state->recv) {
421 REDEBUG("Invalid packet type (%u)", request->packet->code);
423 }
424
425 dhcpv4_packet_debug(request, request->packet, &request->request_pairs, true);
426
427 if (unlikely(request_is_dynamic_client(request))) {
428 return new_client(p_result, mctx, request);
429 }
430
431 return state->recv(p_result, mctx, request);
432}
433
435 {
436 .section = SECTION_NAME("recv", "Discover"),
437 .actions = &mod_actions_postauth,
438
439 .methods = (const section_name_t *[]) {
441 NULL
442 },
443 .offset = PROCESS_CONF_OFFSET(discover),
444 },
445 {
446 .section = SECTION_NAME("send", "Offer"),
447 .actions = &mod_actions_postauth,
448 .offset = PROCESS_CONF_OFFSET(offer),
449 },
450 {
451 .section = SECTION_NAME("recv", "Request"),
453
454 .methods = (const section_name_t *[]) {
456 NULL
457 },
458 .offset = PROCESS_CONF_OFFSET(request),
459 },
460
461 {
462 .section = SECTION_NAME("send", "Ack"),
463 .actions = &mod_actions_postauth,
464 .offset = PROCESS_CONF_OFFSET(ack),
465 },
466 {
467 .section = SECTION_NAME("send", "NAK"),
469 .offset = PROCESS_CONF_OFFSET(nak),
470 },
471 {
472 .section = SECTION_NAME("recv", "Decline"),
474
475 .methods = (const section_name_t *[]) {
477 NULL
478 },
479 .offset = PROCESS_CONF_OFFSET(decline),
480 },
481
482 {
483 .section = SECTION_NAME("recv", "Release"),
484 .actions = &mod_actions_postauth,
485
486 .methods = (const section_name_t *[]) {
488 NULL
489 },
490 .offset = PROCESS_CONF_OFFSET(release),
491 },
492 {
493 .section = SECTION_NAME("recv", "Inform"),
494 .actions = &mod_actions_postauth,
495 .offset = PROCESS_CONF_OFFSET(inform),
496 },
497
498 {
499 .section = SECTION_NAME("recv", "Lease-Query"),
501 .offset = PROCESS_CONF_OFFSET(lease_query),
502 },
503 {
504 .section = SECTION_NAME("send", "Lease-Unassigned"),
506 .offset = PROCESS_CONF_OFFSET(lease_unassigned),
507 },
508 {
509 .section = SECTION_NAME("send", "Lease-Unknown"),
511 .offset = PROCESS_CONF_OFFSET(lease_unknown),
512 },
513 {
514 .section = SECTION_NAME("send", "Lease-Active"),
516 .offset = PROCESS_CONF_OFFSET(lease_active),
517 },
518
519 {
520 .section = SECTION_NAME("send", "Do-Not-Respond"),
522 .offset = PROCESS_CONF_OFFSET(do_not_respond),
523 },
524
525 DYNAMIC_CLIENT_SECTIONS,
526
528};
529
530
533 .common = {
534 .magic = MODULE_MAGIC_INIT,
535 .name = "dhcpv4",
536 .inst_size = sizeof(process_dhcpv4_t)
537 },
538 .process = mod_process,
539 .compile_list = compile_list,
540 .dict = &dict_dhcpv4,
541 .packet_type = &attr_packet_type
542};
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:383
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:273
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:286
Specifies an attribute which must be present for the module to function.
Definition dict.h:272
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:285
#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:852
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:610
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:828
@ 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:92
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:697
static unlang_action_t mod_process(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Definition base.c:184
static const virtual_server_compile_t compile_list[]
Definition base.c:210
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:532
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:66
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:57
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_TIMEOUT
Module (or section) timed out.
Definition rcode.h:50
@ 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:188
#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:204
void * data
Module's instance data.
Definition module.h:272
#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:287
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:313
section_name_t const * section
Identifier for the section.
#define COMPILE_TERMINATOR
Processing sections which are allowed in this virtual server.