All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Typedefs | Functions | Variables
dhcp.c File Reference
#include <freeradius-devel/libradius.h>
#include <freeradius-devel/udpfromto.h>
#include <freeradius-devel/dhcp.h>
#include <freeradius-devel/net.h>
#include <freeradius-devel/pcap.h>
#include <sys/ioctl.h>
#include <net/if_arp.h>
+ Include dependency graph for dhcp.c:

Go to the source code of this file.

Data Structures

struct  dhcp_option_t
 
struct  dhcp_packet_t
 

Macros

#define DEBUG   if (fr_debug_lvl && fr_log_fp) fr_printf_log
 
#define DEFAULT_PACKET_SIZE   (300)
 
#define DHCP_CHADDR_LEN   (16)
 
#define DHCP_FILE_FIELD   (1)
 
#define DHCP_FILE_LEN   (128)
 
#define DHCP_MAX_MESSAGE_TYPE   (sizeof(dhcp_message_types) / sizeof(dhcp_message_types[0]))
 
#define DHCP_OPTION_FIELD   (0)
 
#define DHCP_OPTION_MAGIC_NUMBER   (0x63825363)
 
#define DHCP_SNAME_FIELD   (2)
 
#define DHCP_SNAME_LEN   (64)
 
#define DHCP_VEND_LEN   (308)
 
#define INADDR_BROADCAST   INADDR_NONE
 
#define MAX_PACKET_SIZE   (1500 - 40)
 
#define MIN_PACKET_SIZE   (244)
 

Typedefs

typedef struct dhcp_option_t dhcp_option_t
 
typedef struct dhcp_packet_t dhcp_packet_t
 

Functions

static ssize_t decode_tlv (TALLOC_CTX *ctx, vp_cursor_t *cursor, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len)
 RFC 4243 Vendor Specific Suboptions. More...
 
static ssize_t decode_value (TALLOC_CTX *ctx, vp_cursor_t *cursor, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len)
 
static ssize_t decode_value_internal (TALLOC_CTX *ctx, vp_cursor_t *cursor, fr_dict_attr_t const *da, uint8_t const *data, size_t data_len)
 
static uint8_t const * dhcp_get_option (dhcp_packet_t const *packet, size_t packet_size, unsigned int option)
 
static ssize_t encode_rfc_hdr (uint8_t *out, ssize_t outlen, fr_dict_attr_t const **tlv_stack, unsigned int depth, vp_cursor_t *cursor)
 Write out an RFC option header and option data. More...
 
static ssize_t encode_tlv_hdr (uint8_t *out, ssize_t outlen, fr_dict_attr_t const **tlv_stack, unsigned int depth, vp_cursor_t *cursor)
 Write out a TLV header (and any sub TLVs or values) More...
 
static ssize_t encode_value (uint8_t *out, size_t outlen, fr_dict_attr_t const **tlv_stack, unsigned int depth, vp_cursor_t *cursor)
 Write DHCP option value into buffer. More...
 
int fr_dhcp_add_arp_entry (UNUSED int fd, UNUSED char const *interface, UNUSED VALUE_PAIR *macaddr, UNUSED VALUE_PAIR *ip)
 
static int fr_dhcp_array_members (size_t *out, size_t len, fr_dict_attr_t const *da)
 Returns the number of array members for arrays with fixed element sizes. More...
 
int8_t fr_dhcp_attr_cmp (void const *a, void const *b)
 
int fr_dhcp_decode (RADIUS_PACKET *packet)
 
ssize_t fr_dhcp_decode_option (TALLOC_CTX *ctx, vp_cursor_t *cursor, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, UNUSED void *decoder_ctx)
 Decode DHCP option. More...
 
int fr_dhcp_encode (RADIUS_PACKET *packet)
 
ssize_t fr_dhcp_encode_option (uint8_t *out, size_t outlen, vp_cursor_t *cursor, UNUSED void *encoder_ctx)
 Encode a DHCP option and any sub-options. More...
 
static RADIUS_PACKETfr_dhcp_packet_ok (uint8_t const *data, ssize_t data_len, fr_ipaddr_t src_ipaddr, uint16_t src_port, fr_ipaddr_t dst_ipaddr, uint16_t dst_port)
 Check reveived DHCP request is valid and build RADIUS_PACKET structure if it is. More...
 
RADIUS_PACKETfr_dhcp_recv_socket (int sockfd)
 Receive DHCP packet using socket. More...
 
int fr_dhcp_send_socket (RADIUS_PACKET *packet)
 Send DHCP packet using socket. More...
 

Variables

char const * dhcp_header_names []
 
static int dhcp_header_sizes []
 
char const * dhcp_message_types []
 

Data Structure Documentation

struct dhcp_option_t

Definition at line 110 of file dhcp.c.

Data Fields
uint8_t code
uint8_t length
struct dhcp_packet_t

Definition at line 91 of file dhcp.c.

Data Fields
uint8_t chaddr[DHCP_CHADDR_LEN]
uint32_t ciaddr
uint8_t file[DHCP_FILE_LEN]
uint16_t flags
uint32_t giaddr
uint8_t hlen
uint8_t hops
uint8_t htype
uint8_t opcode
uint32_t option_format
uint8_t options[DHCP_VEND_LEN]
uint16_t secs
uint32_t siaddr
uint8_t sname[DHCP_SNAME_LEN]
uint32_t xid
uint32_t yiaddr

Macro Definition Documentation

#define DEBUG   if (fr_debug_lvl && fr_log_fp) fr_printf_log

Definition at line 63 of file dhcp.c.

#define DEFAULT_PACKET_SIZE   (300)

Definition at line 175 of file dhcp.c.

#define DHCP_CHADDR_LEN   (16)

Definition at line 52 of file dhcp.c.

#define DHCP_FILE_FIELD   (1)

Definition at line 179 of file dhcp.c.

#define DHCP_FILE_LEN   (128)

Definition at line 54 of file dhcp.c.

#define DHCP_MAX_MESSAGE_TYPE   (sizeof(dhcp_message_types) / sizeof(dhcp_message_types[0]))

Definition at line 159 of file dhcp.c.

#define DHCP_OPTION_FIELD   (0)

Definition at line 178 of file dhcp.c.

#define DHCP_OPTION_MAGIC_NUMBER   (0x63825363)

Definition at line 56 of file dhcp.c.

#define DHCP_SNAME_FIELD   (2)

Definition at line 180 of file dhcp.c.

#define DHCP_SNAME_LEN   (64)

Definition at line 53 of file dhcp.c.

#define DHCP_VEND_LEN   (308)

Definition at line 55 of file dhcp.c.

#define INADDR_BROADCAST   INADDR_NONE

Definition at line 59 of file dhcp.c.

#define MAX_PACKET_SIZE   (1500 - 40)

Definition at line 176 of file dhcp.c.

#define MIN_PACKET_SIZE   (244)

Definition at line 174 of file dhcp.c.

Typedef Documentation

typedef struct dhcp_option_t dhcp_option_t
typedef struct dhcp_packet_t dhcp_packet_t

Function Documentation

static ssize_t decode_tlv ( TALLOC_CTX *  ctx,
vp_cursor_t cursor,
fr_dict_attr_t const *  parent,
uint8_t const *  data,
size_t  data_len 
)
static

RFC 4243 Vendor Specific Suboptions.

Vendor specific suboptions are in the format.

     0                   1                   2                   3
     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                     Enterprise Number 0                       |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |    Len 0      |                                               /
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    /                      Suboption Data 0                         /
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                     Enterprise Number n                       |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |    Len n      |                                               /
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    /                      Suboption Data n                         /
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

So although the vendor is identified, the format of the data isn't specified so we can't actually resolve the suboption to an attribute. For now, we just convert it to an attribute of DHCP-Vendor-Specific-Information with raw octets contents. Decode DHCP suboptions

Parameters
[in]ctxcontext to alloc new attributes in.
[in,out]cursorWhere to write the decoded options.
[in]parentof sub TLVs.
[in]datato parse.
[in]data_lenof data parsed.

Definition at line 895 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ssize_t decode_value ( TALLOC_CTX *  ctx,
vp_cursor_t cursor,
fr_dict_attr_t const *  parent,
uint8_t const *  data,
size_t  data_len 
)
static

Definition at line 975 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ssize_t decode_value_internal ( TALLOC_CTX *  ctx,
vp_cursor_t cursor,
fr_dict_attr_t const *  da,
uint8_t const *  data,
size_t  data_len 
)
static

Definition at line 724 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static uint8_t const* dhcp_get_option ( dhcp_packet_t const *  packet,
size_t  packet_size,
unsigned int  option 
)
static

Definition at line 182 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ssize_t encode_rfc_hdr ( uint8_t *  out,
ssize_t  outlen,
fr_dict_attr_t const **  tlv_stack,
unsigned int  depth,
vp_cursor_t cursor 
)
static

Write out an RFC option header and option data.

Note
May coalesce options with fixed width values
Parameters
[in,out]outbuffer to write the TLV to.
[out]outlenlength of the output buffer.
[in]tlv_stackDescribing nesting of options.
[in]depthin the tlv_stack.
[in,out]cursorCurrent attribute we're encoding.
Returns
  • >0 length of data encoded.
  • 0 if we ran out of space.
  • < 0 on error.

Definition at line 1431 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ssize_t encode_tlv_hdr ( uint8_t *  out,
ssize_t  outlen,
fr_dict_attr_t const **  tlv_stack,
unsigned int  depth,
vp_cursor_t cursor 
)
static

Write out a TLV header (and any sub TLVs or values)

Parameters
[in,out]outbuffer to write the TLV to.
[out]outlenlength of the output buffer.
[in]tlv_stackDescribing nesting of options.
[in]depthin the tlv_stack.
[in,out]cursorCurrent attribute we're encoding.
Returns
  • >0 length of data encoded.
  • 0 if we ran out of space.
  • < 0 on error.

Definition at line 1503 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ssize_t encode_value ( uint8_t *  out,
size_t  outlen,
fr_dict_attr_t const **  tlv_stack,
unsigned int  depth,
vp_cursor_t cursor 
)
static

Write DHCP option value into buffer.

Does not include DHCP option length or number.

Parameters
[in,out]outbuffer to write the option to.
[out]outlenlength of the output buffer.
[in]tlv_stackDescribing nesting of options.
[in]depthin tlv_stack.
[in,out]cursorCurrent attribute we're encoding.
Returns
  • The length of data writen.
  • -1 if out of buffer.
  • -2 if unsupported type.

Definition at line 1346 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_dhcp_add_arp_entry ( UNUSED int  fd,
UNUSED char const *  interface,
UNUSED VALUE_PAIR macaddr,
UNUSED VALUE_PAIR ip 
)

Definition at line 1908 of file dhcp.c.

+ Here is the call graph for this function:

static int fr_dhcp_array_members ( size_t *  out,
size_t  len,
fr_dict_attr_t const *  da 
)
static

Returns the number of array members for arrays with fixed element sizes.

Definition at line 682 of file dhcp.c.

+ Here is the caller graph for this function:

int8_t fr_dhcp_attr_cmp ( void const *  a,
void const *  b 
)

Definition at line 1309 of file dhcp.c.

+ Here is the caller graph for this function:

int fr_dhcp_decode ( RADIUS_PACKET packet)

Definition at line 1103 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ssize_t fr_dhcp_decode_option ( TALLOC_CTX *  ctx,
vp_cursor_t cursor,
fr_dict_attr_t const *  parent,
uint8_t const *  data,
size_t  data_len,
UNUSED void *  decoder_ctx 
)

Decode DHCP option.

Parameters
[in]ctxcontext to alloc new attributes in.
[in,out]cursorWhere to write the decoded options.
[in]parentThe root of the protocol dictionary used to decode DHCP attributes.
[in]datato parse.
[in]data_lenof data to parse.
[in]decoder_ctxUnused.

Definition at line 1031 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_dhcp_encode ( RADIUS_PACKET packet)

Definition at line 1629 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ssize_t fr_dhcp_encode_option ( uint8_t *  out,
size_t  outlen,
vp_cursor_t cursor,
UNUSED void *  encoder_ctx 
)

Encode a DHCP option and any sub-options.

Parameters
outWhere to write encoded DHCP attributes.
outlenLength of out buffer.
cursorwith current VP set to the option to be encoded. Will be advanced to the next option to encode.
encoder_ctxUnused.
Returns
  • > 0 length of data written.
  • < 0 error.
  • 0 not valid option for DHCP (skipping).

Definition at line 1578 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RADIUS_PACKET * fr_dhcp_packet_ok ( uint8_t const *  data,
ssize_t  data_len,
fr_ipaddr_t  src_ipaddr,
uint16_t  src_port,
fr_ipaddr_t  dst_ipaddr,
uint16_t  dst_port 
)
static

Check reveived DHCP request is valid and build RADIUS_PACKET structure if it is.

Parameters
datapointer to received packet.
data_lenlength of received data.
src_ipaddrsource ip address.
src_portsource port address.
dst_ipaddrdestination ip address.
dst_portdestination port address.
Returns
  • RADIUS_PACKET pointer if valid
  • NULL if invalid

Definition at line 338 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RADIUS_PACKET* fr_dhcp_recv_socket ( int  sockfd)

Receive DHCP packet using socket.

Parameters
sockfdhandle.
Returns
  • pointer to RADIUS_PACKET if successful.
  • NULL if failed.

Definition at line 255 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_dhcp_send_socket ( RADIUS_PACKET packet)

Send DHCP packet using socket.

Parameters
packetto send
Returns
  • >= 0 if successful.
  • < 0 if failed.

Definition at line 561 of file dhcp.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

char const* dhcp_header_names[]
Initial value:
= {
"DHCP-Opcode",
"DHCP-Hardware-Type",
"DHCP-Hardware-Address-Length",
"DHCP-Hop-Count",
"DHCP-Transaction-Id",
"DHCP-Number-of-Seconds",
"DHCP-Flags",
"DHCP-Client-IP-Address",
"DHCP-Your-IP-Address",
"DHCP-Server-IP-Address",
"DHCP-Gateway-IP-Address",
"DHCP-Client-Hardware-Address",
"DHCP-Server-Host-Name",
"DHCP-Boot-Filename",
NULL
}

Definition at line 121 of file dhcp.c.

int dhcp_header_sizes[]
static
Initial value:
= {
1, 1, 1, 1,
4, 2, 2, 4,
4, 4, 4,
}
#define DHCP_SNAME_LEN
Definition: dhcp.c:53
#define DHCP_FILE_LEN
Definition: dhcp.c:54
#define DHCP_CHADDR_LEN
Definition: dhcp.c:52

Definition at line 161 of file dhcp.c.

char const* dhcp_message_types[]
Initial value:
= {
"invalid",
"DHCP-Discover",
"DHCP-Offer",
"DHCP-Request",
"DHCP-Decline",
"DHCP-Ack",
"DHCP-NAK",
"DHCP-Release",
"DHCP-Inform",
"DHCP-Force-Renew",
"DHCP-Lease-Query",
"DHCP-Lease-Unassigned",
"DHCP-Lease-Unknown",
"DHCP-Lease-Active",
"DHCP-Bulk-Lease-Query",
"DHCP-Lease-Query-Done"
}

Definition at line 140 of file dhcp.c.