This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dhcp.h
228 lines (194 loc) · 6.76 KB
/
dhcp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <netinet/in.h>
#include "array.h"
#ifndef DHCPD_DHCP_H_
#define DHCPD_DHCP_H_
/* A DHCP message consists of a fixed-length header and a variable-length
* option part:
*
* 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
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | op (1) | htype (1) | hlen (1) | hops (1) |
* +---------------+---------------+---------------+---------------+
* | xid (4) |
* +-------------------------------+-------------------------------+
* | secs (2) | flags (2) |
* +-------------------------------+-------------------------------+
* | ciaddr (4) |
* +---------------------------------------------------------------+
* | yiaddr (4) |
* +---------------------------------------------------------------+
* | siaddr (4) |
* +---------------------------------------------------------------+
* | giaddr (4) |
* +---------------------------------------------------------------+
* | |
* | chaddr (16) |
* | |
* | |
* +---------------------------------------------------------------+
* | |
* | sname (64) |
* +---------------------------------------------------------------+
* | |
* | file (128) |
* +---------------------------------------------------------------+
* | |
* | options (variable) |
* +---------------------------------------------------------------+
*/
#define DHCP_MSG_F_OP(m) ((uint8_t*)((m)+0))
#define DHCP_MSG_F_HTYPE(m) ((uint8_t*)((m)+1))
#define DHCP_MSG_F_HLEN(m) ((uint8_t*)((m)+2))
#define DHCP_MSG_F_HOPS(m) ((uint8_t*)((m)+3))
#define DHCP_MSG_F_XID(m) ((uint32_t*)((m)+4))
#define DHCP_MSG_F_SECS(m) ((uint16_t*)((m)+8))
#define DHCP_MSG_F_FLAGS(m) ((uint16_t*)((m)+10))
#define DHCP_MSG_F_CIADDR(m) ((uint32_t*)((m)+12))
#define DHCP_MSG_F_YIADDR(m) ((uint32_t*)((m)+16))
#define DHCP_MSG_F_SIADDR(m) ((uint32_t*)((m)+20))
#define DHCP_MSG_F_GIADDR(m) ((uint32_t*)((m)+24))
#define DHCP_MSG_F_CHADDR(m) ((char*)((m)+28))
#define DHCP_MSG_F_MAGIC(m) ((uint8_t*)((m)+236))
#define DHCP_MSG_F_OPTIONS(m) ((uint8_t*)((m)+240))
#define DHCP_MSG_LEN (576)
#define DHCP_MSG_HDRLEN (240)
#define DHCP_MSG_MAGIC ((uint8_t[]){ 99, 130, 83, 99 })
#define DHCP_MSG_MAGIC_CHECK(m) (m[0] == 99 && m[1] == 130 && m[2] == 83 && m[3] == 99)
#define DHCP_OPT_F_CODE(o) ((uint8_t*)(o))
#define DHCP_OPT_F_LEN(o) ((uint8_t*)((o)+1))
#define DHCP_OPT_F_DATA(o) ((char*)((o)+2))
#define DHCP_OPT_LEN(o) (((uint8_t*)(o))[0] != 255 && ((uint8_t*)(o))[0] != 0 ? ((uint8_t*)(o))[1] + 2 : 1)
#define DHCP_OPT_NEXT(o) (((uint8_t*)(o))+DHCP_OPT_LEN(o))
#define DHCP_OPT_CONT(o, l) {\
(l) += DHCP_OPT_LEN(o);\
(o) = DHCP_OPT_NEXT(o);\
}
#define DHCP_OPT(m) {\
.code = *DHCP_OPT_F_CODE(m),\
.len = *DHCP_OPT_F_LEN(m),\
.data = ((*DHCP_OPT_F_LEN(m)) > 0 ? DHCP_OPT_F_DATA(m) : NULL)\
}
enum dhcp_opt_type
{
DHCP_OPT_STUB = 0,
DHCP_OPT_NETMASK = 1,
DHCP_OPT_ROUTER = 3,
DHCP_OPT_DNS = 6,
DHCP_OPT_REQIPADDR = 50,
DHCP_OPT_LEASETIME = 51,
DHCP_OPT_MSGTYPE = 53,
DHCP_OPT_SERVERID = 54,
DHCP_OPT_END = 255
};
enum dhcp_msg_type
{
DHCPDISCOVER = 1,
DHCPOFFER = 2,
DHCPREQUEST = 3,
DHCPDECLINE = 4,
DHCPACK = 5,
DHCPNAK = 6,
DHCPRELEASE = 7,
DHCPINFORM = 8
};
struct dhcp_opt
{
uint8_t code;
uint8_t len;
char *data;
};
struct dhcp_msg
{
uint8_t *data;
uint8_t *end;
size_t length;
enum dhcp_msg_type type;
char *ciaddr;
char *yiaddr;
char *siaddr;
char *giaddr;
char *chaddr;
char *srcaddr;
struct sockaddr *source;
struct sockaddr_in *sid;
};
struct dhcp_lease
{
struct in_addr address;
struct in_addr *routers;
size_t routers_cnt;
struct in_addr *nameservers;
size_t nameservers_cnt;
time_t leasetime;
uint8_t prefixlen;
};
#define DHCP_LEASE_EMPTY {\
.address = {INADDR_ANY},\
.routers = NULL,\
.routers_cnt = 0,\
.nameservers = NULL,\
.nameservers_cnt = 0,\
.leasetime = 0,\
.prefixlen = 0\
}
/**
* Prepare new DHCP message from a specified DHCP message
*
* @param[out] reply Buffer which holds the new DHCP message
* @param[in] original Buffer which holds the old DHCP message
*/
static inline void dhcp_msg_prepare(uint8_t *reply, uint8_t *original)
{
*DHCP_MSG_F_XID(reply) = *DHCP_MSG_F_XID(original);
*DHCP_MSG_F_HTYPE(reply) = *DHCP_MSG_F_HTYPE(original);
*DHCP_MSG_F_HLEN(reply) = *DHCP_MSG_F_HLEN(original);
*DHCP_MSG_F_OP(reply) = (*DHCP_MSG_F_OP(reply) == 2 ? 1 : 2);
ARRAY_COPY(DHCP_MSG_F_MAGIC(reply), DHCP_MSG_MAGIC, 4);
ARRAY_COPY(DHCP_MSG_F_CHADDR(reply), DHCP_MSG_F_CHADDR(original), 16);
}
/**
* Generate full response from a specified DHCP message
*
* @param[out] reply Buffer which holds the new DHCP message
* @param[out] options Pointer which will point to next message option
* @param[out] send_len Size of the message after filling information
* @param[in] msg DHCP message
* @param[in] type Type of the new DHCP message
*/
static inline void dhcp_msg_reply(uint8_t *reply, uint8_t **options,
size_t *send_len, struct dhcp_msg *msg, enum dhcp_msg_type type)
{
*send_len = DHCP_MSG_HDRLEN;
memset(reply, 0, DHCP_MSG_LEN);
dhcp_msg_prepare(reply, msg->data);
ARRAY_COPY(DHCP_MSG_F_SIADDR(reply), &msg->sid->sin_addr, 4);
*options = DHCP_MSG_F_OPTIONS(reply);
(*options)[0] = DHCP_OPT_MSGTYPE;
(*options)[1] = 1;
(*options)[2] = type;
DHCP_OPT_CONT(*options, *send_len);
}
static inline bool dhcp_opt_next(uint8_t **cur, struct dhcp_opt *opt, uint8_t *end)
{
if (*DHCP_OPT_F_CODE(*cur) == DHCP_OPT_END)
return false;
if (opt != NULL)
*opt = (struct dhcp_opt)DHCP_OPT(*cur);
/* Overflow detection */
if ((*cur) + DHCP_OPT_LEN(*cur) >= end)
return false;
*cur = DHCP_OPT_NEXT(*cur);
return true;
}
extern void dhcp_msg_dump(FILE *stream, struct dhcp_msg *msg);
extern uint8_t *dhcp_opt_add_lease(uint8_t *options,
size_t *send_len,
struct dhcp_lease *lease);
#endif