The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
proto_dhcpv4_udp.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: d7de34a63fe74bcada0e9c9abcd9c32580ebdd85 $
19 * @file proto_dhcpv4_udp.c
20 * @brief DHCPv4 handler for UDP.
21 *
22 * @copyright 2018 The FreeRADIUS server project.
23 * @copyright 2018 Alan DeKok (aland@deployingradius.com)
24 */
25#define LOG_PREFIX "proto_dhcpv4_udp"
26
27#include <netdb.h>
28#include <freeradius-devel/arp/arp.h>
29#include <freeradius-devel/util/udp.h>
30#include <freeradius-devel/util/trie.h>
31#include <freeradius-devel/io/application.h>
32#include <freeradius-devel/io/listen.h>
33#include <freeradius-devel/io/schedule.h>
34#include <freeradius-devel/protocol/dhcpv4/freeradius.internal.h>
35#include <freeradius-devel/util/pcap.h>
36#include "proto_dhcpv4.h"
37
39
40#ifdef HAVE_LIBPCAP
41typedef struct {
42 fr_rb_node_t node; //!< in tree of handles.
43 char const *interface; //!< name for the handle.
44 fr_pcap_t *pcap; //!< pcap handle.
45} proto_dhcpv4_pcap_t;
46#endif
47
48typedef struct {
49 char const *name; //!< socket name
50 int sockfd;
51
52 fr_io_address_t *connection; //!< for connected sockets.
53
54 fr_stats_t stats; //!< statistics for this socket
55
56#ifdef HAVE_LIBPCAP
57 fr_rb_tree_t pcaps; //!< Tree of available pcap handles
58#endif
60
61typedef struct {
62 CONF_SECTION *cs; //!< our configuration
63
64 fr_ipaddr_t ipaddr; //!< IP address to listen on.
65
66 fr_ipaddr_t src_ipaddr; //!< IP address to source replies
67
68 char const *interface; //!< Interface to bind to.
69 char const *port_name; //!< Name of the port for getservent().
70
71 uint32_t recv_buff; //!< How big the kernel's receive buffer should be.
72
73 uint32_t max_packet_size; //!< for message ring buffer.
74 uint32_t max_attributes; //!< Limit maximum decodable attributes.
75
76 uint16_t port; //!< Port to listen on.
77 uint16_t client_port; //!< Client port to reply to.
78
79 bool broadcast; //!< whether we listen for broadcast packets
80
81 bool recv_buff_is_set; //!< Whether we were provided with a receive
82 //!< buffer value.
83 bool dynamic_clients; //!< whether we have dynamic clients
84
85 fr_client_list_t *clients; //!< local clients
86 fr_client_t *default_client; //!< default 0/0 client
87
88 fr_trie_t *trie; //!< for parsed networks
89 fr_ipaddr_t *allow; //!< allowed networks for dynamic clients
90 fr_ipaddr_t *deny; //!< denied networks for dynamic clients
91
92#ifdef HAVE_LIBPCAP
93 bool use_pcap; //!< use libpcap for unicast replies to broadcast requests.
94#endif
96
97
104
105
109
110 { FR_CONF_OFFSET_TYPE_FLAGS("src_ipaddr", FR_TYPE_COMBO_IP_ADDR, 0, proto_dhcpv4_udp_t, src_ipaddr) },
111
112 { FR_CONF_OFFSET("interface", proto_dhcpv4_udp_t, interface) },
113 { FR_CONF_OFFSET("port_name", proto_dhcpv4_udp_t, port_name) },
114
115 { FR_CONF_OFFSET("port", proto_dhcpv4_udp_t, port) },
116 { FR_CONF_OFFSET("client_port", proto_dhcpv4_udp_t, client_port) },
117 { FR_CONF_OFFSET_IS_SET("recv_buff", FR_TYPE_UINT32, 0, proto_dhcpv4_udp_t, recv_buff) },
118
119 { FR_CONF_OFFSET("broadcast", proto_dhcpv4_udp_t, broadcast) } ,
120
121 { FR_CONF_OFFSET("dynamic_clients", proto_dhcpv4_udp_t, dynamic_clients) } ,
122 { FR_CONF_POINTER("networks", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) networks_config },
123
124 { FR_CONF_OFFSET("max_packet_size", proto_dhcpv4_udp_t, max_packet_size), .dflt = "4096" } ,
125 { FR_CONF_OFFSET("max_attributes", proto_dhcpv4_udp_t, max_attributes), .dflt = STRINGIFY(DHCPV4_MAX_ATTRIBUTES) } ,
126
127#ifdef HAVE_LIBPCAP
128 { FR_CONF_OFFSET_FLAGS("use_pcap", CONF_FLAG_HIDDEN, proto_dhcpv4_udp_t, use_pcap) },
129#endif
131};
132
133static fr_dict_t const *dict_dhcpv4;
134
140
143
146 { .out = &attr_message_type, .name = "Message-Type", .type = FR_TYPE_UINT8, .dict = &dict_dhcpv4},
147 { .out = &attr_dhcp_server_identifier, .name = "Server-Identifier", .type = FR_TYPE_IPV4_ADDR, .dict = &dict_dhcpv4},
149};
150
151#ifdef HAVE_LIBPCAP
152static fr_cmp_ret_t dhcpv4_pcap_cmp(void const *a, void const *b)
153{
154 proto_dhcpv4_pcap_t const *one = a, *two = b;
155 return CMP(strcmp(one->interface, two->interface), 0);
156}
157
158static proto_dhcpv4_pcap_t *dhcpv4_pcap_find(proto_dhcpv4_udp_thread_t *thread, char const *interface)
159{
160 proto_dhcpv4_pcap_t find, *pcap;
161
162 find = (proto_dhcpv4_pcap_t) {
163 .interface = interface
164 };
165
166 fr_rb_find((void **)&pcap, &thread->pcaps, &find);
167 if (pcap) return pcap;
168
169 MEM(pcap = talloc_zero(thread, proto_dhcpv4_pcap_t));
170
171 pcap->pcap = fr_pcap_init(pcap, interface, PCAP_INTERFACE_OUT);
172
173 if (!pcap->pcap) {
174 ERROR("Failed creating pcap for interface %s", interface);
175 return NULL;
176 }
177
178 if (fr_pcap_open(pcap->pcap) < 0) {
179 ERROR("Failed opening pcap on interface %s", interface);
180 error:
181 talloc_free(pcap);
182 return NULL;
183 }
184
185 /*
186 * Use a filter which will match nothing - so we don't capture any packets
187 */
188 if (fr_pcap_apply_filter(pcap->pcap, "tcp and udp") < 0) {
189 ERROR("Failed adding filter to pcap on interface %s", interface);
190 goto error;
191 }
192
193 pcap->interface = pcap->pcap->name;
194 fr_rb_insert(&thread->pcaps, pcap);
195
196 return pcap;
197}
198
199static void dhcpv4_pcap_free(proto_dhcpv4_udp_thread_t *thread, proto_dhcpv4_pcap_t *pcap)
200{
201 fr_rb_remove(NULL, &thread->pcaps, pcap);
202 talloc_free(pcap);
203}
204#endif
205
206static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time_p, uint8_t *buffer, size_t buffer_len,
207 size_t *leftover)
208{
209 proto_dhcpv4_udp_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_dhcpv4_udp_thread_t);
210 fr_io_address_t *address, **address_p;
211
212 int flags;
213 ssize_t data_size;
214 size_t packet_len;
215 uint8_t message_type;
216 uint32_t xid, ipaddr;
217 dhcp_packet_t *packet;
218
219 *leftover = 0; /* always for UDP */
220
221 /*
222 * Where the addresses should go. This is a special case
223 * for proto_dhcpv4.
224 */
225 address_p = (fr_io_address_t **) packet_ctx;
226 address = *address_p;
227
228 /*
229 * Tell udp_recv if we're connected or not.
230 */
231 flags = UDP_FLAGS_CONNECTED * (thread->connection != NULL);
232
233 data_size = udp_recv(thread->sockfd, flags, &address->socket, buffer, buffer_len, recv_time_p);
234 if (data_size < 0) {
235 RATE_LIMIT_GLOBAL(PERROR, "Read error (%zd)", data_size);
236 return data_size;
237 }
238
239 if (!data_size) {
240 RATE_LIMIT_GLOBAL(WARN, "Got no data - ignoring");
241 return 0;
242 }
243
244 /*
245 * @todo - make this take "&packet_len", as the DHCPv4
246 * packet may be smaller than the parent UDP packet.
247 */
248 if (!fr_dhcpv4_ok(buffer, data_size, &message_type, &xid)) {
249 RATE_LIMIT_GLOBAL(PWARN, "Invalid packet - ignoring");
250 return 0;
251 }
252
253 packet_len = data_size;
254
255 /*
256 * We've seen a server reply to this port, but the giaddr
257 * is *not* our address. Drop it.
258 */
259 packet = (dhcp_packet_t *) buffer;
260 memcpy(&ipaddr, &packet->giaddr, 4);
261 if ((packet->opcode == 2) && (ipaddr != address->socket.inet.dst_ipaddr.addr.v4.s_addr)) {
262 DEBUG2("Ignoring server reply which was not meant for us (was for %pV).",
263 fr_box_ipaddr(address->socket.inet.dst_ipaddr));
264 return 0;
265 }
266
267 /*
268 * proto_dhcpv4 sets the priority
269 */
270
271 /*
272 * Print out what we received.
273 */
274 DEBUG2("Received %s XID %08x length %d %s", dhcp_message_types[message_type], xid,
275 (int) packet_len, thread->name);
276
277 return packet_len;
278}
279
280
281static ssize_t mod_write(fr_listen_t *li, void *packet_ctx, UNUSED fr_time_t request_time,
282 uint8_t *buffer, size_t buffer_len, UNUSED size_t written)
283{
285 proto_dhcpv4_udp_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_dhcpv4_udp_thread_t);
286
287 fr_io_track_t *track = talloc_get_type_abort(packet_ctx, fr_io_track_t);
288 proto_dhcpv4_track_t *request = talloc_get_type_abort(track->packet, proto_dhcpv4_track_t);
289 fr_socket_t socket;
290
291 int flags;
292 ssize_t data_size;
293
294 /*
295 * @todo - share a stats interface with the parent? or
296 * put the stats in the listener, so that proto_dhcpv4
297 * can update them, too.. <sigh>
298 */
299 thread->stats.total_responses++;
300
301 flags = UDP_FLAGS_CONNECTED * (thread->connection != NULL);
302
303 /*
304 * Swap src/dst IP/port
305 */
306 fr_socket_addr_swap(&socket, &track->address->socket);
307
308 /*
309 * Figure out which kind of packet we're sending.
310 */
311 if (!thread->connection) {
312 uint8_t const *code, *sid;
313 dhcp_packet_t *packet = (dhcp_packet_t *) buffer;
314#ifdef WITH_IFINDEX_IPADDR_RESOLUTION
315 fr_ipaddr_t primary;
316#endif
317
318 /*
319 * This isn't available in the packet header.
320 */
321 code = fr_dhcpv4_packet_get_option(packet, buffer_len, attr_message_type);
322 if (!code || (code[1] < 1) || (code[2] == 0) || (code[2] > FR_DHCP_LEASE_ACTIVE)) {
323 WARN("Silently discarding reply due to invalid or missing message type");
324 return 0;
325 }
326
327 /*
328 * Set the source IP we'll use for sending the packets.
329 *
330 * - if src_ipaddr is unicast, use that
331 * - else if socket wasn't bound to *, then use that
332 * - else if we have ifindex, get main IP from that interface and use that.
333 * - else for offer/ack, look at option 54, for Server Identification and use that
334 * - else leave source IP as whatever is already in "socket.inet.src_ipaddr".
335 */
336 if (!fr_ipaddr_is_inaddr_any(&inst->src_ipaddr)) {
337 socket.inet.src_ipaddr = inst->src_ipaddr;
338 } else if (!fr_ipaddr_is_inaddr_any(&inst->ipaddr)) {
339 socket.inet.src_ipaddr = inst->ipaddr;
340#ifdef WITH_IFINDEX_IPADDR_RESOLUTION
341 } else if ((address->socket.inet.ifindex > 0) &&
342 (fr_ipaddr_from_ifindex(&primary, thread->sockfd, &socket.inet.dst_ipaddr.af,
343 &socket.inet.ifindex) == 0)) {
344 socket.inet.src_ipaddr = primary;
345#endif
346 } else if (((code[2] == FR_DHCP_OFFER) || (code[2] == FR_DHCP_ACK)) &&
347 ((sid = fr_dhcpv4_packet_get_option(packet, buffer_len, attr_dhcp_server_identifier)) != NULL) &&
348 (sid[1] == 4)) {
349 memcpy(&socket.inet.src_ipaddr.addr.v4.s_addr, sid + 2, 4);
350 }
351
352 /*
353 * If we're forwarding the Discover or Request,
354 * then we need to figure out where to forward it
355 * to?
356 */
357 if ((code[2] == FR_DHCP_DISCOVER) || (code[2] == FR_DHCP_REQUEST)) {
358 WARN("Silently discarding client request, as we do not know where to send it");
359 return 0;
360 }
361
362 /*
363 * We have GIADDR in the packet, so send it
364 * there. The packet is FROM our IP address and
365 * port, TO the destination IP address, at the
366 * same (i.e. server) port.
367 *
368 * RFC 2131 page 23
369 *
370 * "If the 'giaddr' field in a DHCP message from
371 * a client is non-zero, the server sends any
372 * return messages to the 'DHCP server' port on
373 * the BOOTP relay agent whose address appears in
374 * 'giaddr'.
375 */
376 if (packet->giaddr != INADDR_ANY) {
377 DEBUG("Reply will be sent to giaddr.");
378 socket.inet.dst_ipaddr.addr.v4.s_addr = packet->giaddr;
379 socket.inet.dst_port = inst->port;
380 socket.inet.src_port = inst->port;
381
382 /*
383 * Increase the hop count for client
384 * packets sent to the next gateway.
385 */
386 if ((code[2] == FR_DHCP_DISCOVER) ||
387 (code[2] == FR_DHCP_REQUEST)) {
388 packet->opcode = 1; /* client message */
389 packet->hops = request->hops + 1;
390 } else {
391 packet->opcode = 2; /* server message */
392 }
393
394 goto send_reply;
395 }
396
397 packet->opcode = 2; /* server message */
398
399 /*
400 * If the client port is specified, use it.
401 *
402 * RFC 2131 page 23.
403 *
404 * "DHCP messages from a server to a client are sent
405 * to the 'DHCP client' port (68)"
406 */
407 if (inst->client_port) socket.inet.dst_port = inst->client_port;
408
409 /*
410 * NAKs are broadcast when there's no giaddr.
411 *
412 * RFC 2131 page 23.
413 *
414 * "In all cases, when 'giaddr' is zero, the server
415 * broadcasts any DHCPNAK messages to 0xffffffff."
416 */
417 if (code[2] == FR_DHCP_NAK) {
418 DEBUG("Reply will be broadcast due to NAK.");
419 socket.inet.dst_ipaddr.addr.v4.s_addr = INADDR_BROADCAST;
420 goto send_reply;
421 }
422
423 /*
424 * The original packet has CIADDR, so we unicast
425 * the reply there.
426 *
427 * RFC 2131 page 23.
428 *
429 * "If the 'giaddr' field is zero and the
430 * 'ciaddr' field is nonzero, then the server
431 * unicasts DHCPOFFER and DHCPACK messages to the
432 * address in 'ciaddr'."
433 */
434 if (request->ciaddr != INADDR_ANY) {
435 DEBUG("Reply will be unicast to CIADDR from original packet.");
436 memcpy(&socket.inet.dst_ipaddr.addr.v4.s_addr, &request->ciaddr, 4);
437 goto send_reply;
438 }
439
440 /*
441 * The original packet requested a broadcast
442 * reply, so we broadcast the reply.
443 *
444 * RFC 2131 page 23.
445 *
446 * "If 'giaddr' is zero and 'ciaddr' is zero, and
447 * the broadcast bit is set, then the server
448 * broadcasts DHCPOFFER and DHCPACK messages to
449 * 0xffffffff."
450 */
451 if (request->broadcast) {
452 DEBUG("Reply will be broadcast due to client request.");
453 socket.inet.dst_ipaddr.addr.v4.s_addr = INADDR_BROADCAST;
454 goto send_reply;
455 }
456
457 /*
458 * The original packet was unicast to us, such as
459 * via a relay. We have a unicast destination
460 * address, so we just use that.
461 *
462 * This extension isn't in the RFC, but we find it useful.
463 */
464 if ((packet->yiaddr == htonl(INADDR_ANY)) &&
465 (socket.inet.dst_ipaddr.addr.v4.s_addr != htonl(INADDR_BROADCAST))) {
466 DEBUG("Reply will be unicast to source IP from original packet.");
467 goto send_reply;
468 }
469
470 /*
471 * RFC 2131 page 23.
472 *
473 * "If the broadcast bit is not set and 'giaddr'
474 * is zero and 'ciaddr' is zero, then the server
475 * unicasts DHCPOFFER and DHCPACK messages to the
476 * client's hardware address and 'yiaddr'
477 * address."
478 */
479 switch (code[2]) {
480 /*
481 * OFFERs are sent to YIADDR if we
482 * received a unicast packet from YIADDR.
483 * Otherwise, they are unicast to YIADDR
484 * (if we can update ARP), otherwise they
485 * are broadcast.
486 */
487 case FR_DHCP_OFFER:
488 {
489 char if_name[IFNAMSIZ];
490#ifdef HAVE_LIBPCAP
491 offer:
492#endif
493 if_name[0] = '\0';
494#ifdef WITH_IFINDEX_NAME_RESOLUTION
495 if (!inst->interface && socket.inet.ifindex) fr_ifname_from_ifindex(if_name, socket.inet.ifindex);
496#endif
497 /*
498 * If the packet was unicast from the
499 * client, unicast it back without
500 * updating the ARP table. We presume
501 * that the ARP table has been updated by
502 * the OS, since we received a unicast
503 * packet.
504 *
505 * This check simply makes sure that we
506 * don't needlessly update the ARP table.
507 */
508 if (memcmp(&socket.inet.dst_ipaddr.addr.v4.s_addr, &packet->yiaddr, 4) == 0) {
509 DEBUG("Reply will be unicast to YIADDR.");
510
511#ifdef SIOCSARP
512 } else if (inst->broadcast &&
513#ifdef HAVE_LIBPCAP
514 !inst->use_pcap &&
515#endif
516 (inst->interface || if_name[0])) {
517 uint8_t macaddr[6];
518 uint8_t ipaddr[4];
519
520 memcpy(&ipaddr, &packet->yiaddr, 4);
521 memcpy(&macaddr, &packet->chaddr, 6);
522
523 /*
524 * Else the OFFER was broadcast.
525 * This socket is listening for
526 * broadcast packets on a
527 * particular interface. We're
528 * too lazy to write raw UDP
529 * packets, so we update our
530 * local ARP table and then
531 * unicast the reply.
532 */
533 if (fr_arp_entry_add(thread->sockfd, inst->interface ? inst->interface : if_name, ipaddr, macaddr) == 0) {
534 DEBUG("Reply will be unicast to YIADDR, done ARP table updates.");
535 memcpy(&socket.inet.dst_ipaddr.addr.v4.s_addr, &packet->yiaddr, 4);
536 } else {
537 DEBUG("Failed adding ARP entry. Reply will be broadcast.");
538 socket.inet.dst_ipaddr.addr.v4.s_addr = INADDR_BROADCAST;
539 }
540#endif
541#ifdef __FreeBSD__
542 } else if (inst->broadcast &&
543#ifdef HAVE_LIBPCAP
544 !inst->use_pcap &&
545#endif
546 (inst->interface || if_name[0])) {
547 uint8_t macaddr[6];
548 uint8_t ipaddr[4];
549
550 memcpy(&ipaddr, &packet->yiaddr, 4);
551 memcpy(&macaddr, &packet->chaddr, 6);
552
553 /*
554 * FreeBSD version of above using netlink API
555 */
556 if (fr_bsd_arp_entry_add(socket.inet.ifindex, ipaddr, macaddr) == 0) {
557 DEBUG("Reply will be unicast to YADDR, done ARP table updates.");
558 memcpy(&socket.inet.dst_ipaddr.addr.v4.s_addr, &packet->yiaddr, 4);
559 } else {
560 DEBUG("Failed adding ARP table entry. Reply will be broadcast.");
561 socket.inet.dst_ipaddr.addr.v4.s_addr = INADDR_BROADCAST;
562 }
563#endif
564#ifdef HAVE_LIBPCAP
565 } else if (inst->use_pcap && inst->broadcast && (inst->interface || if_name[0])) {
566 proto_dhcpv4_pcap_t *pcap;
567 uint8_t macaddr[6];
568 fr_packet_t tosend;
569
570 pcap = dhcpv4_pcap_find(thread, inst->interface ? inst->interface : if_name);
571 if (!pcap) return -1;
572
573 DEBUG("Reply will be unicast to YIADDR.");
574 memcpy(&macaddr, &packet->chaddr, 6);
575 memcpy(&socket.inet.dst_ipaddr.addr.v4.s_addr, &packet->yiaddr, 4);
576 tosend = (fr_packet_t) {
577 .socket = socket,
578 .data = buffer,
579 .data_len = buffer_len
580 };
581
582 DEBUG("Sending %s XID %08x from %pV:%d to %pV:%d", dhcp_message_types[code[2]], packet->xid,
583 fr_box_ipaddr(socket.inet.src_ipaddr), socket.inet.src_port,
584 fr_box_ipaddr(socket.inet.dst_ipaddr), socket.inet.dst_port);
585 if (fr_dhcpv4_pcap_send(pcap->pcap, macaddr, &tosend) < 0) {
586 ERROR("Failed sending packet");
587 dhcpv4_pcap_free(thread, pcap);
588 return -1;
589 }
590
591 return buffer_len;
592#endif
593 } else {
594 DEBUG("Reply will be broadcast as we do not create raw UDP sockets.");
595 socket.inet.dst_ipaddr.addr.v4.s_addr = INADDR_BROADCAST;
596 }
597 }
598 break;
599
600 /*
601 * ACKs are unicast to YIADDR
602 */
603 case FR_DHCP_ACK:
604#ifdef HAVE_LIBPCAP
605 /*
606 * Are we replying on a system using pcap - if so use the same logic as OFFER
607 */
608 if (inst->use_pcap) goto offer;
609#endif
610 DEBUG("Reply will be unicast to YIADDR.");
611 memcpy(&socket.inet.dst_ipaddr.addr.v4.s_addr, &packet->yiaddr, 4);
612 break;
613
614 default:
615 WARN("Silently discarding reply due to unimplemented message type %d", code[2]);
616 return 0;
617 }
618
620 DEBUG("Sending %s XID %08x from %pV:%d to %pV:%d", dhcp_message_types[code[2]], packet->xid,
621 fr_box_ipaddr(socket.inet.src_ipaddr), socket.inet.src_port,
622 fr_box_ipaddr(socket.inet.dst_ipaddr), socket.inet.dst_port);
623 }
624
625 /*
626 * proto_dhcpv4 takes care of suppressing do-not-respond, etc.
627 */
628 data_size = udp_send(&socket, flags, buffer, buffer_len);
629
630 /*
631 * This socket is dead. That's an error...
632 */
633 if (data_size <= 0) return data_size;
634
635 return data_size;
636}
637
638
640{
641 proto_dhcpv4_udp_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_dhcpv4_udp_thread_t);
642
643 thread->connection = connection;
644 return 0;
645}
646
647
648static void mod_network_get(int *ipproto, bool *dynamic_clients, fr_trie_t const **trie, void *instance)
649{
650 proto_dhcpv4_udp_t *inst = talloc_get_type_abort(instance, proto_dhcpv4_udp_t);
651
652 *ipproto = IPPROTO_UDP;
653 *dynamic_clients = inst->dynamic_clients;
654 *trie = inst->trie;
655}
656
657
658/** Open a UDP listener for DHCPV4
659 *
660 */
661static int mod_open(fr_listen_t *li)
662{
664 proto_dhcpv4_udp_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_dhcpv4_udp_thread_t);
665
666 int sockfd, rcode;
667 fr_ipaddr_t ipaddr = inst->ipaddr;
668 uint16_t port = inst->port;
669
670 li->fd = sockfd = fr_socket_server_udp(&inst->ipaddr, &port, inst->port_name, true);
671 if (sockfd < 0) {
672 cf_log_err(li->cs, "Failed opening UDP socket - %s", fr_strerror());
673 error:
674 return -1;
675 }
676
677 li->app_io_addr = fr_socket_addr_alloc_inet_src(li, IPPROTO_UDP, 0, &inst->ipaddr, port);
678
679 /*
680 * Set SO_REUSEPORT before bind, so that all packets can
681 * listen on the same destination IP address.
682 */
683 {
684 int on = 1;
685
686 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on)) < 0) {
687 cf_log_err(li->cs, "Failed to set socket 'reuseport' - %s", fr_syserror(errno));
688 close(sockfd);
689 return -1;
690 }
691 }
692
693#ifdef SO_RCVBUF
694 if (inst->recv_buff_is_set) {
695 int opt;
696
697 opt = inst->recv_buff;
698 if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(int)) < 0) {
699 cf_log_warn(li->cs, "Failed setting 'recv_buf' - %s", fr_syserror(errno));
700 }
701 }
702#endif
703
704 if (inst->broadcast) {
705 int on = 1;
706
707 if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
708 cf_log_err(li->cs, "Failed to set 'broadcast' - %s", fr_syserror(errno));
709 close(sockfd);
710 return -1;
711 }
712 }
713
714 rad_suid_up();
715 rcode = fr_socket_bind(sockfd, inst->interface, &ipaddr, &port);
717 if (rcode < 0) {
718 close(sockfd);
719 cf_log_err(li->cs, "Failed binding to socket - %s", fr_strerror());
720 cf_log_err(li->cs, DOC_ROOT_REF(troubleshooting/network/bind));
721 goto error;
722 }
723 if (inst->interface) li->app_io_addr->inet.src_ipaddr.scope_id = ipaddr.scope_id;
724
725 thread->sockfd = sockfd;
726
727 fr_assert((cf_parent(inst->cs) != NULL) && (cf_parent(cf_parent(inst->cs)) != NULL)); /* listen { ... } */
728
730 NULL, 0,
731 &inst->ipaddr, inst->port,
732 inst->interface);
733
734#ifdef HAVE_LIBPCAP
735 fr_rb_inline_init(&thread->pcaps, proto_dhcpv4_pcap_t, node, dhcpv4_pcap_cmp, NULL);
736#endif
737 return 0;
738}
739
740#ifdef HAVE_LIBPCAP
741static int mod_close(fr_listen_t *li)
742{
743 proto_dhcpv4_udp_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_dhcpv4_udp_thread_t);
744 void **pcap_to_free;
745 int i;
746
747 if (fr_rb_flatten_inorder(NULL, &pcap_to_free, &thread->pcaps) < 0) return -1;
748
749 for (i = talloc_array_length(pcap_to_free) - 1; i >= 0; i--) talloc_free(pcap_to_free[i]);
750 talloc_free(pcap_to_free);
751
752 return 0;
753}
754#endif
755
756/** Set the file descriptor for this socket.
757 *
758 */
759static int mod_fd_set(fr_listen_t *li, int fd)
760{
762 proto_dhcpv4_udp_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_dhcpv4_udp_thread_t);
763
764 thread->sockfd = fd;
765
767 &thread->connection->socket.inet.src_ipaddr,
768 thread->connection->socket.inet.src_port,
769 &inst->ipaddr, inst->port,
770 inst->interface);
771
772 return 0;
773}
774
775
776static void *mod_track_create(UNUSED void const *instance, UNUSED void *thread_instance, UNUSED fr_client_t *client,
777 fr_io_track_t *track, uint8_t const *packet, size_t packet_len)
778{
780 dhcp_packet_t const *dhcp = (dhcp_packet_t const *) packet;
781 uint8_t const *option;
782
783 option = fr_dhcpv4_packet_get_option(dhcp, packet_len, attr_message_type);
784 if (!option || (option[1] == 0)) {
785 DEBUG("No %s in the packet - ignoring", attr_message_type->name);
786 return NULL;
787 }
788
789 t = talloc_zero(track, proto_dhcpv4_track_t);
790 if (!t) return NULL;
791
792 memcpy(&t->xid, &dhcp->xid, sizeof(t->xid));
793
794 t->message_type = option[2];
795
796 /*
797 * Track most packets by chaddr. For lease queries, that
798 * field can be the client address being queried, not the
799 * address of the system which sent the packet. So
800 * instead for lease queries, we use giaddr, which MUST
801 * exist according to RFC 4388 Section 6.3
802 */
803 if (option[2] != FR_DHCP_LEASE_QUERY) {
804 if (dhcp->hlen == 6) memcpy(&t->chaddr, &dhcp->chaddr, 6);
805 }
806
807 t->broadcast = ((dhcp->flags & FR_FLAGS_VALUE_BROADCAST) != 0);
808 t->hops = dhcp->hops;
809 memcpy(&t->ciaddr, &dhcp->ciaddr, sizeof(t->ciaddr));
810 memcpy(&t->giaddr, &dhcp->giaddr, sizeof(t->giaddr));
811
812 return t;
813}
814
815static int mod_track_compare(UNUSED void const *instance, UNUSED void *thread_instance, UNUSED fr_client_t *client,
816 void const *one, void const *two)
817{
818 int ret;
819 proto_dhcpv4_track_t const *a = one;
820 proto_dhcpv4_track_t const *b = two;
821
822 /*
823 * The tree is ordered by XIDs, which are (hopefully)
824 * pseudo-randomly distributed.
825 */
826 ret = memcmp(&a->xid, &b->xid, sizeof(a->xid));
827 if (ret != 0) return ret;
828
829 /*
830 * Hardware addresses should also be randomly distributed.
831 */
832 ret = memcmp(&a->chaddr, &b->chaddr, sizeof(a->chaddr));
833 if (ret != 0) return ret;
834
835 /*
836 * Compare giaddr, but not ciaddr
837 */
838 ret = memcmp(&a->giaddr, &b->giaddr, sizeof(a->giaddr));
839 if (ret != 0) return ret;
840
841 return (a->message_type < b->message_type) - (a->message_type > b->message_type);
842}
843
844static char const *mod_name(fr_listen_t *li)
845{
846 proto_dhcpv4_udp_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_dhcpv4_udp_thread_t);
847
848 return thread->name;
849}
850
851
852static int mod_instantiate(module_inst_ctx_t const *mctx)
853{
854 proto_dhcpv4_udp_t *inst = talloc_get_type_abort(mctx->mi->data, proto_dhcpv4_udp_t);
855 CONF_SECTION *conf = mctx->mi->conf;
856 size_t num;
857 CONF_ITEM *ci;
858 CONF_SECTION *server_cs;
859 fr_client_t *client;
860
861 inst->cs = conf;
862
863 /*
864 * Complain if no "ipaddr" is set.
865 */
866 if (inst->ipaddr.af == AF_UNSPEC) {
867 cf_log_err(conf, "No 'ipaddr' was specified in the 'udp' section");
868 return -1;
869 }
870
871 if (inst->ipaddr.af != AF_INET) {
872 cf_log_err(conf, "DHCPv4 transport cannot use IPv6 for 'ipaddr'");
873 return -1;
874 }
875
876 /*
877 * If src_ipaddr is defined, it must be of the same address family as "ipaddr"
878 */
879 if ((inst->src_ipaddr.af != AF_UNSPEC) &&
880 (inst->src_ipaddr.af != inst->ipaddr.af)) {
881 cf_log_err(conf, "Both 'ipaddr' and 'src_ipaddr' must be from the same address family");
882 return -1;
883 }
884
885 /*
886 * Set src_ipaddr to INADDR_NONE if not otherwise specified
887 */
888 if (inst->src_ipaddr.af == AF_UNSPEC) {
889 memset(&inst->src_ipaddr, 0, sizeof(inst->src_ipaddr));
890 inst->src_ipaddr.af = AF_INET;
891 }
892
893 if (inst->recv_buff_is_set) {
894 FR_INTEGER_BOUND_CHECK("recv_buff", inst->recv_buff, >=, 32);
895 FR_INTEGER_BOUND_CHECK("recv_buff", inst->recv_buff, <=, INT_MAX);
896 }
897
898 FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, >=, MIN_PACKET_SIZE);
899 FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, <=, 65536);
900
901 if (!inst->port) {
902 struct servent *s;
903
904 if (!inst->port_name) {
905 cf_log_err(conf, "No 'port' was specified in the 'udp' section");
906 return -1;
907 }
908
909 s = getservbyname(inst->port_name, "udp");
910 if (!s) {
911 cf_log_err(conf, "Unknown value for 'port_name = %s", inst->port_name);
912 return -1;
913 }
914
915 inst->port = ntohs(s->s_port);
916 }
917
918#if defined(SIOCSARP) || defined(__FreeBSD__)
919 /*
920 * If we're listening for broadcast requests, we MUST
921 */
922 if (inst->broadcast && !inst->interface) {
923 cf_log_warn(conf, "You SHOULD set 'interface' if you have set 'broadcast = yes'.");
924 cf_log_warn(conf, "All replies will be broadcast, as ARP updates require 'interface' to be set.");
925 }
926#elif defined HAVE_LIBPCAP
927 INFO("libpcap will be used for unicast replies to broadcast requests.");
928 inst->use_pcap = true;
929#endif
930
931 /*
932 * Parse and create the trie for dynamic clients, even if
933 * there's no dynamic clients.
934 */
935 num = talloc_array_length(inst->allow);
936 if (!num) {
937 if (inst->dynamic_clients) {
938 cf_log_err(conf, "The 'allow' subsection MUST contain at least one 'network' entry when 'dynamic_clients = true'.");
939 return -1;
940 }
941 } else {
942 inst->trie = fr_master_io_network(inst, inst->ipaddr.af, inst->allow, inst->deny);
943 if (!inst->trie) {
944 cf_log_perr(conf, "Failed creating list of networks");
945 return -1;
946 }
947 }
948
949 ci = cf_section_to_item(mctx->mi->parent->conf); /* listen { ... } */
950 fr_assert(ci != NULL);
951 ci = cf_parent(ci);
952 fr_assert(ci != NULL);
953
954 server_cs = cf_item_to_section(ci);
955
956 /*
957 * Look up local clients, if they exist.
958 *
959 * @todo - ensure that we only parse clients which are
960 * for IPPROTO_UDP, and don't require a "secret".
961 */
962 if (cf_section_find_next(server_cs, NULL, "client", CF_IDENT_ANY)) {
963 inst->clients = client_list_parse_section(server_cs, IPPROTO_UDP, false);
964 if (!inst->clients) {
965 cf_log_err(conf, "Failed creating local clients");
966 return -1;
967 }
968 }
969
970 /*
971 * Create a fake client.
972 */
973 client = inst->default_client = talloc_zero(inst, fr_client_t);
974 if (!inst->default_client) return -1;
975
976 client->ipaddr.af = AF_INET;
977 client->ipaddr.addr.v4.s_addr = htonl(INADDR_NONE);
978 client->src_ipaddr = client->ipaddr;
979
980 client->longname = client->shortname = client->secret = talloc_strdup(client, "default");
981 client->nas_type = talloc_strdup(client, "other");
982
983 return 0;
984}
985
987{
989
990 /*
991 * Prefer local clients.
992 */
993 if (inst->clients) {
994 return client_find(inst->clients, ipaddr, ipproto);
995 }
996
997 return inst->default_client;
998}
999
1001 .common = {
1002 .magic = MODULE_MAGIC_INIT,
1003 .name = "dhcpv4_udp",
1005 .inst_size = sizeof(proto_dhcpv4_udp_t),
1006 .thread_inst_size = sizeof(proto_dhcpv4_udp_thread_t),
1007 .instantiate = mod_instantiate,
1008 },
1009 .default_message_size = 4096,
1010 .track_duplicates = true,
1011
1012 .open = mod_open,
1013 .read = mod_read,
1014 .write = mod_write,
1015#ifdef HAVE_LIBPCAP
1016 .close = mod_close,
1017#endif
1018 .fd_set = mod_fd_set,
1019 .track_create = mod_track_create,
1020 .track_compare = mod_track_compare,
1021 .connection_set = mod_connection_set,
1022 .network_get = mod_network_get,
1023 .client_find = mod_client_find,
1024 .get_name = mod_name,
1025};
static int const char char buffer[256]
Definition acutest.h:576
char const * fr_app_io_socket_name(TALLOC_CTX *ctx, fr_app_io_t const *app_io, fr_ipaddr_t const *src_ipaddr, int src_port, fr_ipaddr_t const *dst_ipaddr, int dst_port, char const *interface)
Definition app_io.c:32
module_t common
Common fields to all loadable modules.
Definition app_io.h:34
Public structure describing an I/O path for a protocol.
Definition app_io.h:33
int fr_arp_entry_add(int fd, char const *interface, uint8_t ipaddr[static 4], uint8_t macaddr[static 6])
#define STRINGIFY(x)
Definition build.h:216
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:113
#define UNUSED
Definition build.h:384
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:669
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:529
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
#define FR_CONF_POINTER(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result
Definition cf_parse.h:334
#define FR_CONF_OFFSET_IS_SET(_name, _type, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct,...
Definition cf_parse.h:294
#define FR_CONF_OFFSET_FLAGS(_name, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:268
@ CONF_FLAG_MULTI
CONF_PAIR can have multiple copies.
Definition cf_parse.h:446
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition cf_parse.h:423
@ CONF_FLAG_HIDDEN
Used by scripts to omit items from the generated documentation.
Definition cf_parse.h:455
#define FR_CONF_OFFSET_TYPE_FLAGS(_name, _type, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:238
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:606
Common header for all CONF_* types.
Definition cf_priv.h:54
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:749
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:695
CONF_SECTION * cf_section_find_next(CONF_SECTION const *cs, CONF_SECTION const *prev, char const *name1, char const *name2)
Return the next matching section.
Definition cf_util.c:1225
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:345
#define cf_parent(_cf)
Definition cf_util.h:118
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:352
#define cf_log_warn(_cf, _fmt,...)
Definition cf_util.h:346
#define CF_IDENT_ANY
Definition cf_util.h:80
#define MEM(x)
Definition debug.h:36
#define ERROR(fmt,...)
Definition dhcpclient.c:40
static int sockfd
Definition dhcpclient.c:55
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
#define MIN_PACKET_SIZE
Definition dhcpv4.h:88
uint32_t yiaddr
Definition dhcpv4.h:75
uint8_t const * fr_dhcpv4_packet_get_option(dhcp_packet_t const *packet, size_t packet_size, fr_dict_attr_t const *da)
Retrieve a DHCP option from a raw packet buffer.
Definition packet.c:37
#define INADDR_BROADCAST
Definition dhcpv4.h:101
uint32_t giaddr
Definition dhcpv4.h:77
@ FR_DHCP_REQUEST
Definition dhcpv4.h:47
@ 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_QUERY
Definition dhcpv4.h:54
@ FR_DHCP_NAK
Definition dhcpv4.h:50
@ FR_DHCP_ACK
Definition dhcpv4.h:49
uint8_t opcode
Definition dhcpv4.h:67
uint8_t hlen
Definition dhcpv4.h:69
uint8_t hops
Definition dhcpv4.h:70
uint16_t flags
Definition dhcpv4.h:73
uint32_t xid
Definition dhcpv4.h:71
uint32_t ciaddr
Definition dhcpv4.h:74
#define DHCPV4_MAX_ATTRIBUTES
Definition dhcpv4.h:91
uint8_t chaddr[DHCP_CHADDR_LEN]
Definition dhcpv4.h:78
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:292
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:305
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:311
Specifies an attribute which must be present for the module to function.
Definition dict.h:291
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:304
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
talloc_free(hp)
int fr_ipaddr_is_inaddr_any(fr_ipaddr_t const *ipaddr)
Determine if an address is the INADDR_ANY address for its address family.
Definition inet.c:62
int af
Address family.
Definition inet.h:64
uint32_t scope_id
A host may have multiple link-local interfaces the scope ID allows the application to specify which o...
Definition inet.h:70
union fr_ipaddr_t::@143 addr
IPv4/6 prefix.
fr_socket_t socket
src/dst ip and port.
Definition base.h:336
fr_ipaddr_t ipaddr
IPv4/IPv6 address of the host.
Definition client.h:83
char const * secret
Secret PSK.
Definition client.h:90
fr_ipaddr_t src_ipaddr
IPv4/IPv6 address to send responses from (family must match ipaddr).
Definition client.h:84
char const * nas_type
Type of client (arbitrary).
Definition client.h:131
char const * longname
Client identifier.
Definition client.h:87
char const * shortname
Client nickname.
Definition client.h:88
Describes a host allowed to send packets to the server.
Definition client.h:80
#define PERROR(_fmt,...)
Definition log.h:233
#define PWARN(_fmt,...)
Definition log.h:232
#define RATE_LIMIT_GLOBAL(_log, _fmt,...)
Rate limit messages using a global limiting entry.
Definition log.h:658
uint64_t total_responses
Definition stats.h:38
void rad_suid_up(void)
Definition util.c:767
void rad_suid_down(void)
Definition util.c:771
ssize_t udp_recv(int sockfd, int flags, fr_socket_t *socket_out, void *data, size_t data_len, fr_time_t *when)
Read a UDP packet.
Definition udp.c:144
int udp_send(fr_socket_t const *sock, int flags, void *data, size_t data_len)
Send a packet via a UDP socket.
Definition udp.c:42
CONF_SECTION * cs
of this listener
Definition listen.h:41
fr_socket_t * app_io_addr
for tracking duplicate sockets
Definition listen.h:36
void const * app_io_instance
I/O path configuration context.
Definition listen.h:33
void * thread_instance
thread / socket context
Definition listen.h:34
int fd
file descriptor for this socket - set by open
Definition listen.h:28
fr_trie_t * fr_master_io_network(TALLOC_CTX *ctx, int af, fr_ipaddr_t *allow, fr_ipaddr_t *deny)
Create a trie from arrays of allow / deny IP addresses.
Definition master.c:3210
static int mod_close(fr_listen_t *li)
Close the socket.
Definition master.c:3059
fr_io_address_t const * address
of this packet.. shared between multiple packets
Definition master.h:55
uint8_t * packet
really a tracking structure, not a packet
Definition master.h:57
unsigned short uint16_t
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_COMBO_IP_PREFIX
IPv4 or IPv6 address prefix depending on length.
@ FR_TYPE_UINT8
8 Bit unsigned integer.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
fr_cmp_ret_t
Result of an ordering comparison.
Definition misc.h:50
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
fr_ethernet_t chaddr
fr_client_t * default_client
default 0/0 client
static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time_p, uint8_t *buffer, size_t buffer_len, size_t *leftover)
static fr_dict_attr_t const * attr_dhcp_server_identifier
fr_ipaddr_t * allow
allowed networks for dynamic clients
uint32_t max_attributes
Limit maximum decodable attributes.
bool dynamic_clients
whether we have dynamic clients
fr_dict_autoload_t proto_dhcpv4_udp_dict[]
fr_trie_t * trie
for parsed networks
uint16_t port
Port to listen on.
fr_ipaddr_t src_ipaddr
IP address to source replies.
fr_io_address_t * connection
for connected sockets.
CONF_SECTION * cs
our configuration
char const * port_name
Name of the port for getservent().
static fr_client_t * mod_client_find(fr_listen_t *li, fr_ipaddr_t const *ipaddr, int ipproto)
static int mod_open(fr_listen_t *li)
Open a UDP listener for DHCPV4.
static fr_dict_t const * dict_dhcpv4
static const conf_parser_t udp_listen_config[]
static void mod_network_get(int *ipproto, bool *dynamic_clients, fr_trie_t const **trie, void *instance)
static void * mod_track_create(UNUSED void const *instance, UNUSED void *thread_instance, UNUSED fr_client_t *client, fr_io_track_t *track, uint8_t const *packet, size_t packet_len)
fr_ipaddr_t ipaddr
IP address to listen on.
static const conf_parser_t networks_config[]
fr_app_io_t proto_dhcpv4_udp
bool recv_buff_is_set
Whether we were provided with a receive buffer value.
fr_ipaddr_t * deny
denied networks for dynamic clients
fr_stats_t stats
statistics for this socket
fr_dict_attr_autoload_t proto_dhcpv4_udp_dict_attr[]
uint16_t client_port
Client port to reply to.
bool broadcast
whether we listen for broadcast packets
uint32_t recv_buff
How big the kernel's receive buffer should be.
static int mod_track_compare(UNUSED void const *instance, UNUSED void *thread_instance, UNUSED fr_client_t *client, void const *one, void const *two)
static int mod_connection_set(fr_listen_t *li, fr_io_address_t *connection)
static int mod_fd_set(fr_listen_t *li, int fd)
Set the file descriptor for this socket.
static char const * mod_name(fr_listen_t *li)
static ssize_t mod_write(fr_listen_t *li, void *packet_ctx, UNUSED fr_time_t request_time, uint8_t *buffer, size_t buffer_len, UNUSED size_t written)
static int mod_instantiate(module_inst_ctx_t const *mctx)
fr_client_list_t * clients
local clients
uint32_t max_packet_size
for message ring buffer.
char const * name
socket name
char const * interface
Interface to bind to.
static fr_dict_attr_t const * attr_message_type
char const * dhcp_message_types[]
Definition base.c:129
bool fr_dhcpv4_ok(uint8_t const *data, ssize_t data_len, uint8_t *message_type, uint32_t *xid)
Check received DHCP request is valid and build fr_packet_t structure if it is.
Definition base.c:248
#define fr_assert(_expr)
Definition rad_assert.h:37
static int ipproto
#define DEBUG2(fmt,...)
#define WARN(fmt,...)
static uint16_t client_port
Definition radclient.c:85
#define INFO(fmt,...)
Definition radict.c:63
static void send_reply(int sockfd, fr_channel_data_t *reply)
static rs_t * conf
Definition radsniff.c:52
int fr_rb_remove(void **removed, fr_rb_tree_t *tree, void const *data)
Remove an entry from the tree, without freeing the data.
Definition rb.c:718
int fr_rb_find(void **found, fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:586
int fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:637
#define fr_rb_inline_init(_tree, _type, _field, _data_cmp, _data_free)
Initialises a red black tree.
Definition rb.h:178
int fr_rb_flatten_inorder(TALLOC_CTX *ctx, void **out[], fr_rb_tree_t *tree)
The main red black tree structure.
Definition rb.h:71
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:351
void * data
Module's instance data.
Definition module.h:293
module_instance_t const * parent
Parent module's instance (if any).
Definition module.h:359
conf_parser_t const * config
How to convert a CONF_SECTION to a module instance.
Definition module.h:206
int fr_socket_server_udp(fr_ipaddr_t const *src_ipaddr, uint16_t *src_port, char const *port_name, bool async)
Open an IPv4/IPv6 unconnected UDP socket.
Definition socket.c:843
int fr_socket_bind(int sockfd, char const *ifname, fr_ipaddr_t *src_ipaddr, uint16_t *src_port)
Bind a UDP/TCP v4/v6 socket to a given ipaddr src port, and interface.
Definition socket.c:200
fr_client_t * client_find(fr_client_list_t const *clients, fr_ipaddr_t const *ipaddr, int proto)
Definition client.c:370
fr_client_list_t * client_list_parse_section(CONF_SECTION *section, int proto, TLS_UNUSED bool tls_required)
Definition client.c:475
Group of clients.
Definition client.c:50
eap_aka_sim_process_conf_t * inst
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define talloc_get_type_abort_const
Definition talloc.h:117
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
"server local" time.
Definition time.h:69
#define UDP_FLAGS_CONNECTED
Definition udp.h:38
fr_socket_t socket
This packet was received on.
Definition packet.h:57
static fr_socket_t * fr_socket_addr_alloc_inet_src(TALLOC_CTX *ctx, int proto, int ifindex, fr_ipaddr_t const *ipaddr, int port)
A variant of fr_socket_addr_init_inet_src will also allocates a fr_socket_t.
Definition socket.h:249
int af
AF_INET, AF_INET6, or AF_UNIX.
Definition socket.h:83
static void fr_socket_addr_swap(fr_socket_t *dst, fr_socket_t const *src)
Swap src/dst information of a fr_socket_t.
Definition socket.h:126
Holds information necessary for binding or connecting to a socket.
Definition socket.h:60
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:558
#define DOC_ROOT_REF(_x)
Definition version.h:90
#define fr_box_ipaddr(_val)
Definition value.h:317