25#define LOG_PREFIX "proto_dhcpv4_udp"
28#include <freeradius-devel/arp/arp.h>
29#include <freeradius-devel/server/protocol.h>
30#include <freeradius-devel/util/udp.h>
31#include <freeradius-devel/util/trie.h>
32#include <freeradius-devel/io/application.h>
33#include <freeradius-devel/io/listen.h>
34#include <freeradius-devel/io/schedule.h>
35#include <freeradius-devel/protocol/dhcpv4/freeradius.internal.h>
36#include <freeradius-devel/util/pcap.h>
44 char const *interface;
153static int8_t dhcpv4_pcap_cmp(
void const *a,
void const *b)
155 proto_dhcpv4_pcap_t
const *one = a, *two = b;
156 return CMP(strcmp(one->interface, two->interface), 0);
161 proto_dhcpv4_pcap_t find, *pcap;
163 find = (proto_dhcpv4_pcap_t) {
164 .interface = interface
168 if (pcap)
return pcap;
170 MEM(pcap = talloc_zero(thread, proto_dhcpv4_pcap_t));
172 pcap->pcap = fr_pcap_init(pcap, interface, PCAP_INTERFACE_OUT);
175 ERROR(
"Failed creating pcap for interface %s", interface);
179 if (fr_pcap_open(pcap->pcap) < 0) {
180 ERROR(
"Failed opening pcap on interface %s", interface);
189 if (fr_pcap_apply_filter(pcap->pcap,
"tcp and udp") < 0) {
190 ERROR(
"Failed adding filter to pcap on interface %s", interface);
194 pcap->interface = pcap->pcap->name;
227 address = *address_p;
254 packet_len = data_size;
261 memcpy(&ipaddr, &packet->
giaddr, 4);
262 if ((packet->
opcode == 2) && (ipaddr != address->
socket.inet.dst_ipaddr.addr.v4.s_addr)) {
263 DEBUG2(
"Ignoring server reply which was not meant for us (was for %pV).",
276 (
int) packet_len, thread->
name);
315#ifdef WITH_IFINDEX_IPADDR_RESOLUTION
324 WARN(
"Silently discarding reply due to invalid or missing message type");
338 socket.inet.src_ipaddr =
inst->src_ipaddr;
340 socket.inet.src_ipaddr =
inst->ipaddr;
341#ifdef WITH_IFINDEX_IPADDR_RESOLUTION
342 }
else if ((address->socket.inet.ifindex > 0) &&
343 (fr_ipaddr_from_ifindex(&primary, thread->
sockfd, &socket.inet.dst_ipaddr.
af,
344 &socket.inet.ifindex) == 0)) {
345 socket.inet.src_ipaddr = primary;
350 memcpy(&socket.inet.src_ipaddr.addr.v4.s_addr, sid + 2, 4);
359 WARN(
"Silently discarding client request, as we do not know where to send it");
377 if (packet->
giaddr != INADDR_ANY) {
378 DEBUG(
"Reply will be sent to giaddr.");
379 socket.inet.dst_ipaddr.addr.v4.s_addr = packet->
giaddr;
380 socket.inet.dst_port =
inst->port;
381 socket.inet.src_port =
inst->port;
408 if (
inst->client_port) socket.inet.dst_port =
inst->client_port;
419 DEBUG(
"Reply will be broadcast due to NAK.");
435 if (request->
ciaddr != INADDR_ANY) {
436 DEBUG(
"Reply will be unicast to CIADDR from original packet.");
437 memcpy(&socket.inet.dst_ipaddr.addr.v4.s_addr, &request->
ciaddr, 4);
453 DEBUG(
"Reply will be broadcast due to client request.");
465 if ((packet->
yiaddr == htonl(INADDR_ANY)) &&
467 DEBUG(
"Reply will be unicast to source IP from original packet.");
490 char if_name[IFNAMSIZ];
495#ifdef WITH_IFINDEX_NAME_RESOLUTION
496 if (!
inst->interface && socket.inet.ifindex) fr_ifname_from_ifindex(if_name, socket.inet.ifindex);
509 if (memcmp(&socket.inet.dst_ipaddr.addr.v4.s_addr, &packet->
yiaddr, 4) == 0) {
510 DEBUG(
"Reply will be unicast to YIADDR.");
513 }
else if (
inst->broadcast &&
517 (
inst->interface || if_name[0])) {
521 memcpy(&ipaddr, &packet->
yiaddr, 4);
522 memcpy(&macaddr, &packet->
chaddr, 6);
535 DEBUG(
"Reply will be unicast to YIADDR, done ARP table updates.");
536 memcpy(&socket.inet.dst_ipaddr.addr.v4.s_addr, &packet->
yiaddr, 4);
538 DEBUG(
"Failed adding ARP entry. Reply will be broadcast.");
543 }
else if (
inst->broadcast &&
547 (
inst->interface || if_name[0])) {
551 memcpy(&ipaddr, &packet->
yiaddr, 4);
552 memcpy(&macaddr, &packet->
chaddr, 6);
557 if (fr_bsd_arp_entry_add(socket.inet.ifindex, ipaddr, macaddr) == 0) {
558 DEBUG(
"Reply will be unicast to YADDR, done ARP table updates.");
559 memcpy(&socket.inet.dst_ipaddr.addr.v4.s_addr, &packet->
yiaddr, 4);
561 DEBUG(
"Failed adding ARP table entry. Reply will be broadcast.");
566 }
else if (
inst->use_pcap &&
inst->broadcast && (
inst->interface || if_name[0])) {
567 proto_dhcpv4_pcap_t *pcap;
571 pcap = dhcpv4_pcap_find(thread,
inst->interface ?
inst->interface : if_name);
572 if (!pcap)
return -1;
574 DEBUG(
"Reply will be unicast to YIADDR.");
575 memcpy(&macaddr, &packet->
chaddr, 6);
576 memcpy(&socket.inet.dst_ipaddr.addr.v4.s_addr, &packet->
yiaddr, 4);
580 .data_len = buffer_len
585 fr_box_ipaddr(socket.inet.dst_ipaddr), socket.inet.dst_port);
586 if (fr_dhcpv4_pcap_send(pcap->pcap, macaddr, &tosend) < 0) {
587 ERROR(
"Failed sending packet");
588 dhcpv4_pcap_free(thread, pcap);
595 DEBUG(
"Reply will be broadcast as we do not create raw UDP sockets.");
609 if (
inst->use_pcap)
goto offer;
611 DEBUG(
"Reply will be unicast to YIADDR.");
612 memcpy(&socket.inet.dst_ipaddr.addr.v4.s_addr, &packet->
yiaddr, 4);
616 WARN(
"Silently discarding reply due to unimplemented message type %d", code[2]);
623 fr_box_ipaddr(socket.inet.dst_ipaddr), socket.inet.dst_port);
634 if (data_size <= 0)
return data_size;
654 *dynamic_clients =
inst->dynamic_clients;
673 PERROR(
"Failed opening UDP socket");
687 if (setsockopt(
sockfd, SOL_SOCKET, SO_REUSEPORT, &on,
sizeof(on)) < 0) {
695 if (
inst->recv_buff_is_set) {
698 opt =
inst->recv_buff;
699 if (setsockopt(
sockfd, SOL_SOCKET, SO_RCVBUF, &opt,
sizeof(
int)) < 0) {
705 if (
inst->broadcast) {
708 if (setsockopt(
sockfd, SOL_SOCKET, SO_BROADCAST, &on,
sizeof(on)) < 0) {
718 PERROR(
"Failed binding socket");
732 fr_rb_inline_init(&thread->pcaps, proto_dhcpv4_pcap_t, node, dhcpv4_pcap_cmp, NULL);
746 for (i = talloc_array_length(pcap_to_free) - 1; i >= 0; i--)
talloc_free(pcap_to_free[i]);
781 if (!option || (option[1] == 0)) {
789 memcpy(&t->
xid, &dhcp->
xid,
sizeof(t->
xid));
813 void const *one,
void const *two)
823 ret = memcmp(&a->
xid, &b->
xid,
sizeof(a->
xid));
824 if (ret != 0)
return ret;
830 if (ret != 0)
return ret;
836 if (ret != 0)
return ret;
863 if (
inst->ipaddr.af == AF_UNSPEC) {
864 cf_log_err(
conf,
"No 'ipaddr' was specified in the 'udp' section");
868 if (
inst->ipaddr.af != AF_INET) {
869 cf_log_err(
conf,
"DHCPv4 transport cannot use IPv6 for 'ipaddr'");
876 if ((
inst->src_ipaddr.af != AF_UNSPEC) &&
877 (
inst->src_ipaddr.af !=
inst->ipaddr.af)) {
878 cf_log_err(
conf,
"Both 'ipaddr' and 'src_ipaddr' must be from the same address family");
885 if (
inst->src_ipaddr.af == AF_UNSPEC) {
886 memset(&
inst->src_ipaddr, 0,
sizeof(
inst->src_ipaddr));
887 inst->src_ipaddr.af = AF_INET;
890 if (
inst->recv_buff_is_set) {
901 if (!
inst->port_name) {
906 s = getservbyname(
inst->port_name,
"udp");
912 inst->port = ntohl(s->s_port);
915#if defined(SIOCSARP) || defined(__FreeBSD__)
919 if (
inst->broadcast && !
inst->interface) {
920 cf_log_warn(
conf,
"You SHOULD set 'interface' if you have set 'broadcast = yes'.");
921 cf_log_warn(
conf,
"All replies will be broadcast, as ARP updates require 'interface' to be set.");
923#elif defined HAVE_LIBPCAP
924 INFO(
"libpcap will be used for unicast replies to broadcast requests.");
925 inst->use_pcap =
true;
932 num = talloc_array_length(
inst->allow);
934 if (
inst->dynamic_clients) {
935 cf_log_err(
conf,
"The 'allow' subsection MUST contain at least one 'network' entry when 'dynamic_clients = true'.");
961 if (!
inst->clients) {
971 if (!
inst->default_client)
return 0;
974 client->
ipaddr.
addr.v4.s_addr = htonl(INADDR_NONE);
978 client->
nas_type = talloc_strdup(client,
"other");
994 if (client)
return client;
997 return inst->default_client;
1003 .name =
"dhcpv4_udp",
1009 .default_message_size = 4096,
1010 .track_duplicates =
true,
static int const char char buffer[256]
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)
module_t common
Common fields to all loadable modules.
Public structure describing an I/O path for a protocol.
int fr_arp_entry_add(int fd, char const *interface, uint8_t ipaddr[static 4], uint8_t macaddr[static 6])
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
#define CONF_PARSER_TERMINATOR
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
#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
#define FR_CONF_POINTER(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result
#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,...
#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
@ CONF_FLAG_MULTI
CONF_PAIR can have multiple copies.
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
@ CONF_FLAG_HIDDEN
Used by scripts to omit items from the generated documentation.
#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
Defines a CONF_PAIR to C data type mapping.
Common header for all CONF_* types.
A section grouping multiple CONF_PAIR.
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
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.
#define cf_log_err(_cf, _fmt,...)
#define cf_log_perr(_cf, _fmt,...)
#define cf_log_warn(_cf, _fmt,...)
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.
#define DHCPV4_MAX_ATTRIBUTES
uint8_t chaddr[DHCP_CHADDR_LEN]
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Specifies an attribute which must be present for the module to function.
Specifies a dictionary which must be loaded/loadable for the module to function.
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
int fr_ipaddr_is_inaddr_any(fr_ipaddr_t const *ipaddr)
Determine if an address is the INADDR_ANY address for its address family.
union fr_ipaddr_t::@136 addr
fr_socket_t socket
src/dst ip and port.
fr_socket_t * app_io_addr
for tracking duplicate sockets
void const * app_io_instance
I/O path configuration context.
void * thread_instance
thread / socket context
int fd
file descriptor for this socket - set by open
fr_ipaddr_t ipaddr
IPv4/IPv6 address of the host.
char const * secret
Secret PSK.
fr_ipaddr_t src_ipaddr
IPv4/IPv6 address to send responses from (family must match ipaddr).
char const * nas_type
Type of client (arbitrary).
char const * longname
Client identifier.
char const * shortname
Client nickname.
Describes a host allowed to send packets to the server.
#define RATE_LIMIT_GLOBAL(_log, _fmt,...)
Rate limit messages using a global limiting entry.
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.
int udp_send(fr_socket_t const *sock, int flags, void *data, size_t data_len)
Send a packet via a UDP socket.
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.
static int mod_close(fr_listen_t *li)
Close the socket.
fr_io_address_t const * address
of this packet.. shared between multiple packets
uint8_t * packet
really a tracking structure, not a packet
@ 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.
module_instance_t * mi
Instance of the module being instantiated.
Temporary structure to hold arguments for instantiation calls.
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[]
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.
static uint16_t client_port
static void send_reply(int sockfd, fr_channel_data_t *reply)
void * fr_rb_remove(fr_rb_tree_t *tree, void const *data)
Remove an entry from the tree, without freeing the data.
void * fr_rb_find(fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
bool fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
#define fr_rb_inline_init(_tree, _type, _field, _data_cmp, _data_free)
Initialises a red black tree.
int fr_rb_flatten_inorder(TALLOC_CTX *ctx, void **out[], fr_rb_tree_t *tree)
The main red black tree structure.
CONF_SECTION * conf
Module's instance configuration.
void * data
Module's instance data.
module_instance_t const * parent
Parent module's instance (if any).
conf_parser_t const * config
How to convert a CONF_SECTION to a module instance.
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.
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.
fr_client_t * client_find(fr_client_list_t const *clients, fr_ipaddr_t const *ipaddr, int proto)
fr_client_list_t * client_list_parse_section(CONF_SECTION *section, int proto, TLS_UNUSED bool tls_required)
eap_aka_sim_process_conf_t * inst
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
#define talloc_get_type_abort_const
#define UDP_FLAGS_CONNECTED
fr_socket_t socket
This packet was received on.
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.
int af
AF_INET, AF_INET6, or AF_UNIX.
static void fr_socket_addr_swap(fr_socket_t *dst, fr_socket_t const *src)
Swap src/dst information of a fr_socket_t.
Holds information necessary for binding or connecting to a socket.
#define fr_box_ipaddr(_val)