25 #define LOG_PREFIX "proto_dhcpv4_udp"
28 #include <freeradius-devel/server/protocol.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>
151 address = *address_p;
178 packet_len = data_size;
185 memcpy(&ipaddr, &packet->
giaddr, 4);
186 if ((packet->
opcode == 2) && (ipaddr != address->
socket.inet.dst_ipaddr.addr.v4.s_addr)) {
187 DEBUG2(
"Ignoring server reply which was not meant for us (was for %pV).",
200 (
int) packet_len, thread->
name);
239 #ifdef WITH_IFINDEX_IPADDR_RESOLUTION
248 WARN(
"Silently discarding reply due to invalid or missing message type");
262 socket.inet.src_ipaddr =
inst->src_ipaddr;
264 socket.inet.src_ipaddr =
inst->ipaddr;
265 #ifdef WITH_IFINDEX_IPADDR_RESOLUTION
266 }
else if ((address->socket.inet.ifindex > 0) &&
267 (fr_ipaddr_from_ifindex(&primary, thread->
sockfd, &socket.inet.dst_ipaddr.
af,
268 &socket.inet.ifindex) == 0)) {
269 socket.inet.src_ipaddr = primary;
274 memcpy(&socket.inet.src_ipaddr.addr.v4.s_addr, sid + 2, 4);
283 WARN(
"Silently discarding client request, as we do not know where to send it");
301 if (packet->
giaddr != INADDR_ANY) {
302 DEBUG(
"Reply will be sent to giaddr.");
303 socket.inet.dst_ipaddr.addr.v4.s_addr = packet->
giaddr;
304 socket.inet.dst_port =
inst->port;
305 socket.inet.src_port =
inst->port;
332 if (
inst->client_port) socket.inet.dst_port =
inst->client_port;
343 DEBUG(
"Reply will be broadcast due to NAK.");
359 if (request->
ciaddr != INADDR_ANY) {
360 DEBUG(
"Reply will be unicast to CIADDR from original packet.");
361 memcpy(&socket.inet.dst_ipaddr.addr.v4.s_addr, &request->
ciaddr, 4);
377 DEBUG(
"Reply will be broadcast due to client request.");
389 if ((packet->
yiaddr == htonl(INADDR_ANY)) &&
391 DEBUG(
"Reply will be unicast to source IP from original packet.");
424 if (memcmp(&socket.inet.dst_ipaddr.addr.v4.s_addr, &packet->
yiaddr, 4) == 0) {
425 DEBUG(
"Reply will be unicast to YIADDR.");
428 }
else if (
inst->broadcast &&
inst->interface) {
432 memcpy(&ipaddr, &packet->
yiaddr, 4);
433 memcpy(&macaddr, &packet->
chaddr, 6);
446 DEBUG(
"Reply will be unicast to YIADDR, done ARP table updates.");
447 memcpy(&socket.inet.dst_ipaddr.addr.v4.s_addr, &packet->
yiaddr, 4);
449 DEBUG(
"Failed adding ARP entry. Reply will be broadcast.");
455 DEBUG(
"Reply will be broadcast as we do not create raw UDP sockets.");
464 DEBUG(
"Reply will be unicast to YIADDR.");
465 memcpy(&socket.inet.dst_ipaddr.addr.v4.s_addr, &packet->
yiaddr, 4);
469 WARN(
"Silently discarding reply due to unimplemented message type %d", code[2]);
476 fr_box_ipaddr(socket.inet.dst_ipaddr), socket.inet.dst_port);
487 if (data_size <= 0)
return data_size;
507 *dynamic_clients =
inst->dynamic_clients;
526 PERROR(
"Failed opening UDP socket");
540 if (setsockopt(
sockfd, SOL_SOCKET, SO_REUSEPORT, &on,
sizeof(on)) < 0) {
548 if (
inst->recv_buff_is_set) {
551 opt =
inst->recv_buff;
552 if (setsockopt(
sockfd, SOL_SOCKET, SO_RCVBUF, &opt,
sizeof(
int)) < 0) {
558 if (
inst->broadcast) {
561 if (setsockopt(
sockfd, SOL_SOCKET, SO_BROADCAST, &on,
sizeof(on)) < 0) {
571 PERROR(
"Failed binding socket");
616 if (!option || (option[1] == 0)) {
624 memcpy(&t->
xid, &dhcp->
xid,
sizeof(t->
xid));
648 void const *one,
void const *two)
658 ret = memcmp(&a->
xid, &b->
xid,
sizeof(a->
xid));
659 if (ret != 0)
return ret;
665 if (ret != 0)
return ret;
671 if (ret != 0)
return ret;
698 if (
inst->ipaddr.af == AF_UNSPEC) {
699 cf_log_err(
conf,
"No 'ipaddr' was specified in the 'udp' section");
703 if (
inst->ipaddr.af != AF_INET) {
704 cf_log_err(
conf,
"DHCPv4 transport cannot use IPv6 for 'ipaddr'");
711 if ((
inst->src_ipaddr.af != AF_UNSPEC) &&
712 (
inst->src_ipaddr.af !=
inst->ipaddr.af)) {
713 cf_log_err(
conf,
"Both 'ipaddr' and 'src_ipaddr' must be from the same address family");
720 if (
inst->src_ipaddr.af == AF_UNSPEC) {
721 memset(&
inst->src_ipaddr, 0,
sizeof(
inst->src_ipaddr));
722 inst->src_ipaddr.af = AF_INET;
725 if (
inst->recv_buff_is_set) {
736 if (!
inst->port_name) {
741 s = getservbyname(
inst->port_name,
"udp");
747 inst->port = ntohl(s->s_port);
754 if (
inst->broadcast && !
inst->interface) {
755 cf_log_warn(
conf,
"You SHOULD set 'interface' if you have set 'broadcast = yes'.");
756 cf_log_warn(
conf,
"All replies will be broadcast, as ARP updates require 'interface' to be set.");
764 num = talloc_array_length(
inst->allow);
766 if (
inst->dynamic_clients) {
767 cf_log_err(
conf,
"The 'allow' subsection MUST contain at least one 'network' entry when 'dynamic_clients = true'.");
793 if (!
inst->clients) {
803 if (!
inst->default_client)
return 0;
806 client->
ipaddr.
addr.v4.s_addr = htonl(INADDR_NONE);
810 client->
nas_type = talloc_strdup(client,
"other");
826 if (client)
return client;
829 return inst->default_client;
835 .name =
"dhcpv4_udp",
841 .default_message_size = 4096,
842 .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 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,...
@ 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...
#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::@130 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.
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_client_t * mod_client_find(fr_listen_t *li, fr_ipaddr_t const *ipaddr, int ipproto)
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 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)
fr_ipaddr_t ipaddr
IP address to listen on.
static char const * mod_name(fr_listen_t *li)
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 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
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)
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)
CONF_SECTION * conf
Module's instance configuration.
void * data
Module's instance data.
module_instance_t const * parent
Parent module's instance (if any).
fr_uint_t total_responses
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
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)