1
2
3
4
5
6
7
8#ifndef UDHCP_COMMON_H
9#define UDHCP_COMMON_H 1
10
11#include "libbb.h"
12#include "common_bufsiz.h"
13#include <netinet/udp.h>
14#include <netinet/ip.h>
15
16PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
17
18extern const uint8_t MAC_BCAST_ADDR[6] ALIGN2;
19
20
21
22
23
24#define DHCP_MAGIC 0x63825363
25#define DHCP_OPTIONS_BUFSIZE 308
26#define BOOTREQUEST 1
27#define BOOTREPLY 2
28
29
30struct dhcp_packet {
31 uint8_t op;
32 uint8_t htype;
33 uint8_t hlen;
34 uint8_t hops;
35 uint32_t xid;
36 uint16_t secs;
37 uint16_t flags;
38#define BROADCAST_FLAG 0x8000
39 uint32_t ciaddr;
40 uint32_t yiaddr;
41
42
43 uint32_t siaddr_nip;
44
45
46
47
48
49 uint32_t gateway_nip;
50 uint8_t chaddr[16];
51 uint8_t sname[64];
52
53
54
55 uint8_t file[128];
56
57
58
59
60
61 uint32_t cookie;
62 uint8_t options[DHCP_OPTIONS_BUFSIZE + CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS];
63};
64#define DHCP_PKT_SNAME_LEN 64
65#define DHCP_PKT_FILE_LEN 128
66#define DHCP_PKT_SNAME_LEN_STR "64"
67#define DHCP_PKT_FILE_LEN_STR "128"
68
69struct ip_udp_dhcp_packet {
70 struct iphdr ip;
71 struct udphdr udp;
72 struct dhcp_packet data;
73};
74
75struct udp_dhcp_packet {
76 struct udphdr udp;
77 struct dhcp_packet data;
78};
79
80enum {
81 IP_UDP_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
82 UDP_DHCP_SIZE = sizeof(struct udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
83 DHCP_SIZE = sizeof(struct dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
84};
85
86
87struct BUG_bad_sizeof_struct_ip_udp_dhcp_packet {
88 char c[IP_UDP_DHCP_SIZE == 576 ? 1 : -1];
89};
90
91
92
93
94enum {
95 OPTION_IP = 0,
96 OPTION_IP_PAIR,
97 OPTION_STRING,
98
99
100 OPTION_STRING_HOST,
101
102 OPTION_U8,
103 OPTION_U16,
104
105 OPTION_U32,
106 OPTION_S32,
107 OPTION_BIN,
108 OPTION_STATIC_ROUTES,
109 OPTION_6RD,
110#if ENABLE_FEATURE_UDHCP_RFC3397 || ENABLE_FEATURE_UDHCPC6_RFC3646 || ENABLE_FEATURE_UDHCPC6_RFC4704
111 OPTION_DNS_STRING,
112#endif
113#if ENABLE_FEATURE_UDHCP_RFC3397
114 OPTION_SIP_SERVERS,
115#endif
116
117 OPTION_TYPE_MASK = 0x0f,
118
119 OPTION_REQ = 0x10,
120
121 OPTION_LIST = 0x20,
122};
123
124struct dhcp_scan_state {
125 int overload;
126 int rem;
127 uint8_t *optionptr;
128};
129
130
131
132
133
134
135#define DHCP_PADDING 0x00
136#define DHCP_SUBNET 0x01
137
138
139
140
141
142
143
144
145#define DHCP_HOST_NAME 0x0c
146
147
148
149
150
151
152
153
154
155
156
157
158#define DHCP_REQUESTED_IP 0x32
159#define DHCP_LEASE_TIME 0x33
160#define DHCP_OPTION_OVERLOAD 0x34
161#define DHCP_MESSAGE_TYPE 0x35
162#define DHCP_SERVER_ID 0x36
163#define DHCP_PARAM_REQ 0x37
164
165#define DHCP_MAX_SIZE 0x39
166
167
168#define DHCP_VENDOR 0x3c
169#define DHCP_CLIENT_ID 0x3d
170
171
172
173
174#define DHCP_FQDN 0x51
175
176
177
178
179
180
181
182
183
184
185
186
187#define DHCP_END 0xff
188
189
190#define OPT_CODE 0
191#define OPT_LEN 1
192#define OPT_DATA 2
193
194#define D6_OPT_CODE 0
195#define D6_OPT_LEN 2
196#define D6_OPT_DATA 4
197
198#define OPTION_FIELD 0
199#define FILE_FIELD 1
200#define SNAME_FIELD 2
201
202
203#define DHCPDISCOVER 1
204#define DHCPOFFER 2
205#define DHCPREQUEST 3
206#define DHCPDECLINE 4
207#define DHCPACK 5
208#define DHCPNAK 6
209#define DHCPRELEASE 7
210#define DHCPINFORM 8
211#define DHCP_MINTYPE DHCPDISCOVER
212#define DHCP_MAXTYPE DHCPINFORM
213
214struct dhcp_optflag {
215 uint8_t flags;
216 uint8_t code;
217};
218
219struct option_set {
220 uint8_t *data;
221 struct option_set *next;
222};
223
224#if ENABLE_UDHCPC || ENABLE_UDHCPD
225extern const struct dhcp_optflag dhcp_optflags[];
226extern const char dhcp_option_strings[] ALIGN1;
227#endif
228extern const uint8_t dhcp_option_lengths[] ALIGN1;
229
230unsigned FAST_FUNC udhcp_option_idx(const char *name, const char *option_strings);
231
232void init_scan_state(struct dhcp_packet *packet, struct dhcp_scan_state *scan_state) FAST_FUNC;
233uint8_t *udhcp_scan_options(struct dhcp_packet *packet, struct dhcp_scan_state *scan_state) FAST_FUNC;
234uint8_t *udhcp_get_option(struct dhcp_packet *packet, int code) FAST_FUNC;
235
236
237
238uint8_t *udhcp_get_option32(struct dhcp_packet *packet, int code) FAST_FUNC;
239int udhcp_end_option(uint8_t *optionptr) FAST_FUNC;
240void udhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt) FAST_FUNC;
241#if ENABLE_UDHCPC || ENABLE_UDHCPD
242void udhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, uint32_t data) FAST_FUNC;
243#endif
244#if ENABLE_FEATURE_UDHCP_RFC3397 || ENABLE_FEATURE_UDHCPC6_RFC3646 || ENABLE_FEATURE_UDHCPC6_RFC4704
245char *dname_dec(const uint8_t *cstr, int clen, const char *pre) FAST_FUNC;
246uint8_t *dname_enc( const char *src, int *retlen) FAST_FUNC;
247#endif
248#if !ENABLE_UDHCPC6
249#define udhcp_find_option(opt_list, code, dhcpv6) \
250 udhcp_find_option(opt_list, code)
251#endif
252struct option_set *udhcp_find_option(struct option_set *opt_list, uint8_t code, bool dhcpv6) FAST_FUNC;
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
296# define IF_UDHCP_VERBOSE(...) __VA_ARGS__
297extern unsigned dhcp_verbose;
298# define log1(...) do { if (dhcp_verbose >= 1) bb_info_msg(__VA_ARGS__); } while (0)
299
300void log1s(const char *msg) FAST_FUNC;
301# if CONFIG_UDHCP_DEBUG >= 2
302void udhcp_dump_packet(struct dhcp_packet *packet) FAST_FUNC;
303# define log2(...) do { if (dhcp_verbose >= 2) bb_info_msg(__VA_ARGS__); } while (0)
304# define log2s(msg) do { if (dhcp_verbose >= 2) bb_simple_info_msg(msg); } while (0)
305# else
306# define udhcp_dump_packet(...) ((void)0)
307# define log2(...) ((void)0)
308# define log2s(msg) ((void)0)
309# endif
310# if CONFIG_UDHCP_DEBUG >= 3
311# define log3(...) do { if (dhcp_verbose >= 3) bb_info_msg(__VA_ARGS__); } while (0)
312# define log3s(msg) do { if (dhcp_verbose >= 3) bb_simple_info_msg(msg); } while (0)
313# else
314# define log3(...) ((void)0)
315# define log3s(msg) ((void)0)
316# endif
317#else
318# define IF_UDHCP_VERBOSE(...)
319# define udhcp_dump_packet(...) ((void)0)
320# define log1(...) ((void)0)
321# define log1s(msg) ((void)0)
322# define log2(...) ((void)0)
323# define log2s(msg) ((void)0)
324# define log3(...) ((void)0)
325# define log3s(msg) ((void)0)
326#endif
327
328
329
330
331int FAST_FUNC udhcp_str2nip(const char *str, void *arg);
332
333#if !ENABLE_UDHCPC6
334#define udhcp_insert_new_option(opt_list, code, length, dhcpv6) \
335 udhcp_insert_new_option(opt_list, code, length)
336#endif
337void* FAST_FUNC udhcp_insert_new_option(struct option_set **opt_list,
338 unsigned code,
339 unsigned length,
340 bool dhcpv6);
341
342
343#if !ENABLE_UDHCPC6
344#define udhcp_str2optset(str, arg, optflags, option_strings, dhcpv6) \
345 udhcp_str2optset(str, arg, optflags, option_strings)
346#endif
347int FAST_FUNC udhcp_str2optset(const char *str,
348 void *arg,
349 const struct dhcp_optflag *optflags,
350 const char *option_strings,
351 bool dhcpv6);
352
353#if ENABLE_UDHCPC || ENABLE_UDHCPD
354void udhcp_init_header(struct dhcp_packet *packet, char type) FAST_FUNC;
355#endif
356
357int udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd) FAST_FUNC;
358
359int udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
360 uint32_t source_nip, int source_port,
361 uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
362 int ifindex) FAST_FUNC;
363
364int udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
365 uint32_t source_nip, int source_port,
366 uint32_t dest_nip, int dest_port,
367 const char *ifname) FAST_FUNC;
368
369void udhcp_sp_setup(void) FAST_FUNC;
370void udhcp_sp_fd_set(struct pollfd *pfds, int extra_fd) FAST_FUNC;
371int udhcp_sp_read(void) FAST_FUNC;
372
373int udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac) FAST_FUNC;
374
375int udhcp_listen_socket( int port, const char *inf) FAST_FUNC;
376
377
378int arpping(uint32_t test_nip,
379 const uint8_t *safe_mac,
380 uint32_t from_ip,
381 uint8_t *from_mac,
382 const char *interface,
383 unsigned timeo) FAST_FUNC;
384
385
386int sprint_nip6(char *dest, const uint8_t *ip) FAST_FUNC;
387
388POP_SAVED_FUNCTION_VISIBILITY
389
390#endif
391