27 RCSIDH(dhcp_h,
"$Id: 59e4080fe806988ec13a8ec726c8c096d84f01c0 $")
33 #include <freeradius-devel/util/pcap.h>
34 #include <freeradius-devel/util/packet.h>
35 #include <freeradius-devel/protocol/dhcpv4/rfc2131.h>
37 #define DHCP_CHADDR_LEN (16)
38 #define DHCP_SNAME_LEN (64)
39 #define DHCP_FILE_LEN (128)
40 #define DHCP_VEND_LEN (308)
41 #define DHCP_OPTION_MAGIC_NUMBER (0x63825363)
64 #define FR_DHCP_PACKET_CODE_VALID(_code) (((_code) > 0) && ((_code) < FR_DHCP_CODE_MAX))
88 #define MIN_PACKET_SIZE (244)
89 #define DEFAULT_PACKET_SIZE (300)
90 #define MAX_PACKET_SIZE (1500 - 40)
91 #define DHCPV4_MAX_ATTRIBUTES 255
93 #define DHCP_OPTION_FIELD (0)
94 #define DHCP_FILE_FIELD (1)
95 #define DHCP_SNAME_FIELD (2)
97 #define DHCP_PACK_OPTION1(x,y) ((x) | ((y) << 8))
98 #define DHCP_UNPACK_OPTION1(x) (((x) & 0xff00) >> 8)
100 #ifndef INADDR_BROADCAST
101 # define INADDR_BROADCAST INADDR_NONE
103 #define ETH_ADDR_LEN 6
105 #if defined(HAVE_LIBPCAP) || defined(HAVE_LINUX_IF_PACKET_H)
106 # define ETH_TYPE_IP 0x0800
107 # define IP_HDR_SIZE 20
108 # define UDP_HDR_SIZE 8
117 #ifdef HAVE_LINUX_IF_PACKET_H
118 # define ETH_HDR_SIZE 14
120 # define DISCARD_RP(...) { \
121 if (fr_debug_lvl > 2) { \
122 fprintf(stdout, "dhcpclient: discarding received packet: "); \
123 fprintf(stdout, ## __VA_ARGS__); \
124 fprintf(stdout, "\n"); \
126 fr_packet_free(&packet); \
157 #define fr_dhcpv4_flag_dns_label(_da) (fr_dhcpv4_attr_flags(_da)->dns_label)
158 #define fr_dhcpv4_flag_exists(_da) (fr_dhcpv4_attr_flags(_da)->exists)
160 #define fr_dhcpv4_flag_prefix(_da) fr_dhcpv4_attr_flags(_da)->prefix
161 #define fr_dhcpv4_flag_prefix_bits(_da) (fr_dhcpv4_attr_flags(_da)->prefix == DHCPV4_FLAG_PREFIX_BITS)
162 #define fr_dhcpv4_flag_prefix_split(_da) (fr_dhcpv4_attr_flags(_da)->prefix == DHCPV4_FLAG_PREFIX_SPLIT)
200 uint8_t const *
data,
size_t data_len,
unsigned int *code);
204 #ifdef HAVE_LINUX_IF_PACKET_H
208 #include <linux/if_packet.h>
209 int fr_dhcpv4_raw_socket_open(
struct sockaddr_ll *p_ll,
int iface_index);
211 int fr_dhcpv4_raw_packet_send(
int sockfd,
struct sockaddr_ll *p_ll,
227 int fr_dhcpv4_pcap_send(fr_pcap_t *pcap,
uint8_t *dst_ether_addr,
fr_packet_t *packet);
static int const char char buffer[256]
fr_dcursor_eval_t void const * uctx
TALLOC_CTX * tmp_ctx
for temporary things cleaned up during decoding
char const * dhcp_message_types[]
uint8_t eth_bcast[ETH_ADDR_LEN]
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.
size_t dhcp_header_attrs_len
int fr_dhcpv4_global_init(void)
Resolve/cache attributes in the DHCP dictionary.
fr_packet_t * fr_dhcpv4_udp_packet_recv(int sockfd)
Receive DHCP packet using a connectionless UDP socket.
ssize_t fr_dhcpv4_encode_foreign(fr_dbuff_t *dbuff, fr_pair_list_t const *list)
int fr_dhcpv4_udp_packet_send(fr_packet_t *packet)
Send DHCP packet using a connectionless UDP socket.
fr_dhcpv4_attr_flags_prefix_t prefix
int8_t fr_dhcpv4_attr_cmp(void const *a, void const *b)
fr_dhcpv4_attr_flags_prefix_t
@ DHCPV4_FLAG_PREFIX_INVALID
@ DHCPV4_FLAG_PREFIX_NONE
@ DHCPV4_FLAG_PREFIX_SPLIT
@ DHCPV4_FLAG_PREFIX_BITS
static fr_dhcpv4_attr_flags_t const * fr_dhcpv4_attr_flags(fr_dict_attr_t const *da)
ssize_t fr_dhcpv4_encode_dbuff(fr_dbuff_t *dbuff, dhcp_packet_t *original, int code, uint32_t xid, fr_pair_list_t *vps)
@ FR_DHCP_LEASE_UNASSIGNED
@ FR_DHCP_LEASE_QUERY_DONE
@ FR_DHCP_BULK_LEASE_QUERY
void fr_dhcpv4_global_free(void)
fr_packet_t * fr_dhcpv4_packet_alloc(uint8_t const *data, ssize_t data_len)
void * fr_dhcpv4_next_encodable(fr_dlist_head_t *list, void *to_eval, void *uctx)
DHCPV4-specific iterator.
ssize_t fr_dhcpv4_decode_option(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t len, void *decode_ctx)
Decode DHCP option.
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.
ssize_t fr_dhcpv4_encode(uint8_t *buffer, size_t buflen, dhcp_packet_t *original, int code, uint32_t xid, fr_pair_list_t *vps)
bool fr_dhcpv4_is_encodable(void const *item, void const *uctx)
fr_dict_attr_t const ** dhcp_header_attrs[]
int fr_dhcpv4_packet_encode(fr_packet_t *packet, fr_pair_list_t *list)
fr_dict_attr_t const * root
ssize_t fr_dhcpv4_encode_option(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, void *encode_ctx)
Encode a DHCP option and any sub-options.
ssize_t fr_dhcpv4_decode_foreign(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len)
int fr_dhcpv4_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len, unsigned int *code)
void fr_dhcpv4_print_hex(FILE *fp, uint8_t const *packet, size_t packet_len)
Print a raw DHCP packet as hex.
@ FR_DICT_ATTR_EXT_PROTOCOL_SPECIFIC
Protocol specific extensions.
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Head of a doubly linked list.
static fr_internal_encode_ctx_t encode_ctx
static size_t char ** out