The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
dhcpv4.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /**
19  * $Id: 59e4080fe806988ec13a8ec726c8c096d84f01c0 $
20  *
21  * @file protocols/dhcpv4/dhcpv4.h
22  * @brief Implementation of the DHCPv4 protocol.
23  *
24  * @copyright 2008 The FreeRADIUS server project
25  * @copyright 2008 Alan DeKok (aland@deployingradius.com)
26  */
27 RCSIDH(dhcp_h, "$Id: 59e4080fe806988ec13a8ec726c8c096d84f01c0 $")
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include <freeradius-devel/util/pcap.h>
34 #include <freeradius-devel/util/packet.h>
35 #include <freeradius-devel/protocol/dhcpv4/rfc2131.h>
36 
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)
42 
43 typedef enum {
63 
64 #define FR_DHCP_PACKET_CODE_VALID(_code) (((_code) > 0) && ((_code) < FR_DHCP_CODE_MAX))
65 
66 typedef struct {
71  uint32_t xid; /* 4 */
72  uint16_t secs; /* 8 */
74  uint32_t ciaddr; /* 12 */
75  uint32_t yiaddr; /* 16 */
76  uint32_t siaddr; /* 20 */
77  uint32_t giaddr; /* 24 */
78  uint8_t chaddr[DHCP_CHADDR_LEN]; /* 28 */
79  uint8_t sname[DHCP_SNAME_LEN]; /* 44 */
84 
85 /*
86  * Some clients silently ignore responses less than 300 bytes.
87  */
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
92 
93 #define DHCP_OPTION_FIELD (0)
94 #define DHCP_FILE_FIELD (1)
95 #define DHCP_SNAME_FIELD (2)
96 
97 #define DHCP_PACK_OPTION1(x,y) ((x) | ((y) << 8))
98 #define DHCP_UNPACK_OPTION1(x) (((x) & 0xff00) >> 8)
99 
100 #ifndef INADDR_BROADCAST
101 # define INADDR_BROADCAST INADDR_NONE
102 #endif
103 #define ETH_ADDR_LEN 6
104 
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
109 #endif
110 
111 extern fr_dict_attr_t const **dhcp_header_attrs[];
112 extern size_t dhcp_header_attrs_len;
113 extern char const *dhcp_message_types[];
114 extern int dhcp_header_sizes[];
116 
117 #ifdef HAVE_LINUX_IF_PACKET_H
118 # define ETH_HDR_SIZE 14
119 /* Discard raw packets which we are not interested in. Allow to trace why we discard. */
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"); \
125  } \
126  fr_packet_free(&packet); \
127  return NULL; \
128 }
129 #endif
130 
131 /** Used as the decoder ctx
132  *
133  */
134 typedef struct {
136  TALLOC_CTX *tmp_ctx; //!< for temporary things cleaned up during decoding
138 
139 typedef enum {
145 
146 typedef struct {
147  bool exists;
148  bool dns_label;
151 
153 {
155 }
156 
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)
159 
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)
163 
164 /*
165  * base.c
166  */
167 int8_t fr_dhcpv4_attr_cmp(void const *a, void const *b);
168 
169 bool fr_dhcpv4_ok(uint8_t const *data, ssize_t data_len, uint8_t *message_type, uint32_t *xid);
171 bool fr_dhcpv4_is_encodable(void const *item, void const *uctx);
172 void *fr_dhcpv4_next_encodable(fr_dlist_head_t *list, void *to_eval, void *uctx);
173 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);
174 ssize_t fr_dhcpv4_encode_dbuff(fr_dbuff_t *dbuff, dhcp_packet_t *original, int code, uint32_t xid, fr_pair_list_t *vps);
176 int fr_dhcpv4_global_init(void);
177 void fr_dhcpv4_global_free(void);
178 void fr_dhcpv4_print_hex(FILE *fp, uint8_t const *packet, size_t packet_len);
179 
180 /*
181  * decode.c
182  */
184  uint8_t const *data, size_t len, void *decode_ctx) CC_HINT(nonnull);
185 
187  uint8_t const *data, size_t data_len) CC_HINT(nonnull);
188 
189 /*
190  * encode.c
191  */
193 
194 /*
195  * packet.c
196  */
197 uint8_t const *fr_dhcpv4_packet_get_option(dhcp_packet_t const *packet, size_t packet_size, fr_dict_attr_t const *da);
198 
199 int fr_dhcpv4_decode(TALLOC_CTX *ctx, fr_pair_list_t *out,
200  uint8_t const *data, size_t data_len, unsigned int *code);
201 
203 
204 #ifdef HAVE_LINUX_IF_PACKET_H
205 /*
206  * raw.c
207  */
208 #include <linux/if_packet.h>
209 int fr_dhcpv4_raw_socket_open(struct sockaddr_ll *p_ll, int iface_index);
210 
211 int fr_dhcpv4_raw_packet_send(int sockfd, struct sockaddr_ll *p_ll,
212  fr_packet_t *packet, fr_pair_list_t *list);
213 
214 fr_packet_t *fr_dhcpv4_raw_packet_recv(int sockfd, struct sockaddr_ll *p_ll,
215  fr_packet_t *request, fr_pair_list_t *list);
216 #endif
217 
218 /*
219  * pcap.c
220  */
221 #ifdef HAVE_LIBPCAP
222 /*
223  * Use fr_pcap_init and fr_pcap_open to create/open handles.
224  */
225 fr_packet_t *fr_dhcpv4_pcap_recv(fr_pcap_t *pcap);
226 
227 int fr_dhcpv4_pcap_send(fr_pcap_t *pcap, uint8_t *dst_ether_addr, fr_packet_t *packet);
228 #endif
229 
230 /*
231  * udp.c
232  */
235 
236 #ifdef __cplusplus
237 }
238 #endif
static int const char char buffer[256]
Definition: acutest.h:574
int const char * file
Definition: acutest.h:702
#define RCSIDH(h, id)
Definition: build.h:482
fr_dcursor_eval_t void const * uctx
Definition: dcursor.h:546
return item
Definition: dcursor.h:553
static int sockfd
Definition: dhcpclient.c:56
TALLOC_CTX * tmp_ctx
for temporary things cleaned up during decoding
Definition: dhcpv4.h:136
char const * dhcp_message_types[]
Definition: base.c:126
uint8_t eth_bcast[ETH_ADDR_LEN]
Definition: base.c:164
#define DHCP_CHADDR_LEN
Definition: dhcpv4.h:37
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:240
size_t dhcp_header_attrs_len
Definition: base.c:124
uint32_t yiaddr
Definition: dhcpv4.h:75
int fr_dhcpv4_global_init(void)
Resolve/cache attributes in the DHCP dictionary.
Definition: base.c:584
fr_packet_t * fr_dhcpv4_udp_packet_recv(int sockfd)
Receive DHCP packet using a connectionless UDP socket.
Definition: udp.c:74
ssize_t fr_dhcpv4_encode_foreign(fr_dbuff_t *dbuff, fr_pair_list_t const *list)
Definition: encode.c:774
int fr_dhcpv4_udp_packet_send(fr_packet_t *packet)
Send DHCP packet using a connectionless UDP socket.
Definition: udp.c:41
#define DHCP_SNAME_LEN
Definition: dhcpv4.h:38
#define DHCP_FILE_LEN
Definition: dhcpv4.h:39
fr_dhcpv4_attr_flags_prefix_t prefix
Definition: dhcpv4.h:149
int8_t fr_dhcpv4_attr_cmp(void const *a, void const *b)
Definition: base.c:196
fr_dhcpv4_attr_flags_prefix_t
Definition: dhcpv4.h:139
@ DHCPV4_FLAG_PREFIX_INVALID
Definition: dhcpv4.h:140
@ DHCPV4_FLAG_PREFIX_NONE
Definition: dhcpv4.h:141
@ DHCPV4_FLAG_PREFIX_SPLIT
Definition: dhcpv4.h:143
@ DHCPV4_FLAG_PREFIX_BITS
Definition: dhcpv4.h:142
uint32_t giaddr
Definition: dhcpv4.h:77
#define DHCP_VEND_LEN
Definition: dhcpv4.h:40
static fr_dhcpv4_attr_flags_t const * fr_dhcpv4_attr_flags(fr_dict_attr_t const *da)
Definition: dhcpv4.h:152
#define ETH_ADDR_LEN
Definition: dhcpv4.h:103
ssize_t fr_dhcpv4_encode_dbuff(fr_dbuff_t *dbuff, dhcp_packet_t *original, int code, uint32_t xid, fr_pair_list_t *vps)
Definition: base.c:341
fr_dhcpv4_packet_code_t
Definition: dhcpv4.h:43
@ FR_DHCP_REQUEST
Definition: dhcpv4.h:47
@ FR_DHCP_LEASE_UNASSIGNED
Definition: dhcpv4.h:55
@ FR_DHCP_DECLINE
Definition: dhcpv4.h:48
@ FR_DHCP_OFFER
Definition: dhcpv4.h:46
@ FR_DHCP_DISCOVER
Definition: dhcpv4.h:45
@ FR_DHCP_CODE_MAX
Definition: dhcpv4.h:60
@ FR_DHCP_LEASE_QUERY_DONE
Definition: dhcpv4.h:59
@ FR_DHCP_LEASE_ACTIVE
Definition: dhcpv4.h:57
@ FR_DHCP_LEASE_UNKNOWN
Definition: dhcpv4.h:56
@ FR_DHCP_BULK_LEASE_QUERY
Definition: dhcpv4.h:58
@ FR_DHCP_LEASE_QUERY
Definition: dhcpv4.h:54
@ FR_DHCP_NAK
Definition: dhcpv4.h:50
@ FR_DHCP_RELEASE
Definition: dhcpv4.h:51
@ FR_DHCP_FORCE_RENEW
Definition: dhcpv4.h:53
@ FR_DHCP_DO_NOT_RESPOND
Definition: dhcpv4.h:61
@ FR_DHCP_INFORM
Definition: dhcpv4.h:52
@ FR_DHCP_ACK
Definition: dhcpv4.h:49
@ FR_DHCP_INVALID
Definition: dhcpv4.h:44
uint16_t secs
Definition: dhcpv4.h:72
uint32_t option_format
Definition: dhcpv4.h:81
uint8_t htype
Definition: dhcpv4.h:68
void fr_dhcpv4_global_free(void)
Definition: base.c:628
fr_packet_t * fr_dhcpv4_packet_alloc(uint8_t const *data, ssize_t data_len)
Definition: packet.c:395
uint8_t opcode
Definition: dhcpv4.h:67
uint8_t hlen
Definition: dhcpv4.h:69
void * fr_dhcpv4_next_encodable(fr_dlist_head_t *list, void *to_eval, void *uctx)
DHCPV4-specific iterator.
Definition: base.c:319
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.
Definition: decode.c:538
uint8_t hops
Definition: dhcpv4.h:70
int dhcp_header_sizes[]
Definition: base.c:147
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
uint16_t flags
Definition: dhcpv4.h:73
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)
Definition: base.c:336
bool fr_dhcpv4_is_encodable(void const *item, void const *uctx)
fr_dict_attr_t const ** dhcp_header_attrs[]
Definition: base.c:108
int fr_dhcpv4_packet_encode(fr_packet_t *packet, fr_pair_list_t *list)
Definition: packet.c:367
fr_dict_attr_t const * root
Definition: dhcpv4.h:135
uint32_t xid
Definition: dhcpv4.h:71
uint32_t siaddr
Definition: dhcpv4.h:76
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.
Definition: encode.c:726
uint32_t ciaddr
Definition: dhcpv4.h:74
ssize_t fr_dhcpv4_decode_foreign(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len)
Definition: decode.c:648
int fr_dhcpv4_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len, unsigned int *code)
Definition: packet.c:100
void fr_dhcpv4_print_hex(FILE *fp, uint8_t const *packet, size_t packet_len)
Print a raw DHCP packet as hex.
Definition: base.c:672
Used as the decoder ctx.
Definition: dhcpv4.h:134
@ FR_DICT_ATTR_EXT_PROTOCOL_SPECIFIC
Protocol specific extensions.
Definition: dict.h:170
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Definition: dict_ext.h:140
Head of a doubly linked list.
Definition: dlist.h:51
unsigned short uint16_t
Definition: merged_model.c:31
unsigned int uint32_t
Definition: merged_model.c:33
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
static fr_internal_encode_ctx_t encode_ctx
static fr_slen_t data
Definition: value.h:1265
int nonnull(2, 5))
static size_t char ** out
Definition: value.h:997