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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
/**
* Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License v2 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <netinet/in.h>
#define _unused __attribute__((unused))
#define _packed __attribute__((packed))
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#ifndef SOL_NETLINK
#define SOL_NETLINK 270
#endif
#define ND_OPT_RECURSIVE_DNS 25
#define ND_OPT_DNSSL 31
enum dhcvp6_opt {
DHCPV6_OPT_CLIENTID = 1,
DHCPV6_OPT_SERVERID = 2,
DHCPV6_OPT_IA_NA = 3,
DHCPV6_OPT_IA_ADDR = 5,
DHCPV6_OPT_ORO = 6,
DHCPV6_OPT_PREF = 7,
DHCPV6_OPT_ELAPSED = 8,
DHCPV6_OPT_RELAY_MSG = 9,
DHCPV6_OPT_AUTH = 11,
DHCPV6_OPT_STATUS = 13,
DHCPV6_OPT_RAPID_COMMIT = 14,
DHCPV6_OPT_RECONF_MESSAGE = 19,
DHCPV6_OPT_RECONF_ACCEPT = 20,
DHCPV6_OPT_DNS_SERVERS = 23,
DHCPV6_OPT_DNS_DOMAIN = 24,
DHCPV6_OPT_IA_PD = 25,
DHCPV6_OPT_IA_PREFIX = 26,
DHCPV6_OPT_INFO_REFRESH = 32,
DHCPV6_OPT_FQDN = 39,
DHCPV6_OPT_NTP_SERVER = 56,
DHCPV6_OPT_SIP_SERVER_D = 21,
DHCPV6_OPT_SIP_SERVER_A = 22,
DHCPV6_OPT_AFTR_NAME = 64,
DHCPV6_OPT_PD_EXCLUDE = 67,
};
enum dhcpv6_opt_npt {
NTP_SRV_ADDR = 1,
NTP_MC_ADDR = 2,
NTP_SRV_FQDN = 3
};
enum dhcpv6_msg {
DHCPV6_MSG_UNKNOWN = 0,
DHCPV6_MSG_SOLICIT = 1,
DHCPV6_MSG_ADVERT = 2,
DHCPV6_MSG_REQUEST = 3,
DHCPV6_MSG_RENEW = 5,
DHCPV6_MSG_REBIND = 6,
DHCPV6_MSG_REPLY = 7,
DHCPV6_MSG_RELEASE = 8,
DHCPV6_MSG_DECLINE = 9,
DHCPV6_MSG_RECONF = 10,
DHCPV6_MSG_INFO_REQ = 11,
_DHCPV6_MSG_MAX
};
enum dhcpv6_status {
DHCPV6_NoAddrsAvail = 2,
DHCPV6_NoPrefixAvail = 6,
};
typedef int(reply_handler)(enum dhcpv6_msg orig,
const void *opt, const void *end);
// retransmission strategy
struct dhcpv6_retx {
bool delay;
uint8_t init_timeo;
uint16_t max_timeo;
char name[8];
reply_handler *handler_reply;
int(*handler_finish)(void);
};
// DHCPv6 Protocol Headers
struct dhcpv6_header {
uint8_t msg_type;
uint8_t tr_id[3];
} __attribute__((packed));
struct dhcpv6_ia_hdr {
uint16_t type;
uint16_t len;
uint32_t iaid;
uint32_t t1;
uint32_t t2;
} _packed;
struct dhcpv6_ia_addr {
uint16_t type;
uint16_t len;
struct in6_addr addr;
uint32_t preferred;
uint32_t valid;
} _packed;
struct dhcpv6_ia_prefix {
uint16_t type;
uint16_t len;
uint32_t preferred;
uint32_t valid;
uint8_t prefix;
struct in6_addr addr;
} _packed;
struct dhcpv6_duid {
uint16_t type;
uint16_t len;
uint16_t duid_type;
uint8_t data[128];
} _packed;
struct dhcpv6_auth_reconfigure {
uint16_t type;
uint16_t len;
uint8_t protocol;
uint8_t algorithm;
uint8_t rdm;
uint64_t replay;
uint8_t reconf_type;
uint8_t key[16];
} _packed;
#define dhcpv6_for_each_option(start, end, otype, olen, odata)\
for (uint8_t *_o = (uint8_t*)(start); _o + 4 <= (uint8_t*)(end) &&\
((otype) = _o[0] << 8 | _o[1]) && ((odata) = (void*)&_o[4]) &&\
((olen) = _o[2] << 8 | _o[3]) + (odata) <= (uint8_t*)(end); \
_o += 4 + (_o[2] << 8 | _o[3]))
struct dhcpv6_server_cand {
bool has_noaddravail;
bool wants_reconfigure;
int16_t preference;
uint8_t duid_len;
uint8_t duid[130];
void *ia_na;
void *ia_pd;
size_t ia_na_len;
size_t ia_pd_len;
};
enum odhcp6c_state {
STATE_CLIENT_ID,
STATE_SERVER_ID,
STATE_SERVER_CAND,
STATE_ORO,
STATE_DNS,
STATE_SEARCH,
STATE_IA_NA,
STATE_IA_PD,
STATE_CUSTOM_OPTS,
STATE_SNTP_IP,
STATE_SNTP_FQDN,
STATE_SIP_IP,
STATE_SIP_FQDN,
STATE_RA_ROUTE,
STATE_RA_PREFIX,
STATE_RA_DNS,
STATE_AFTR_NAME,
_STATE_MAX
};
struct icmp6_opt {
uint8_t type;
uint8_t len;
uint8_t data[6];
};
enum dhcpv6_mode {
DHCPV6_UNKNOWN,
DHCPV6_STATELESS,
DHCPV6_STATEFUL
};
enum odhcp6c_ia_mode {
IA_MODE_NONE,
IA_MODE_TRY,
IA_MODE_FORCE,
};
struct odhcp6c_entry {
struct in6_addr router;
uint16_t length;
int16_t priority;
struct in6_addr target;
uint32_t valid;
uint32_t preferred;
};
int init_dhcpv6(const char *ifname, int request_pd);
void dhcpv6_set_ia_na_mode(enum odhcp6c_ia_mode mode);
int dhcpv6_request(enum dhcpv6_msg type);
int dhcpv6_poll_reconfigure(void);
int init_rtnetlink(void);
int set_rtnetlink_addr(int ifindex, const struct in6_addr *addr,
uint32_t pref, uint32_t valid);
int script_init(const char *path, const char *ifname);
ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src);
void script_call(const char *status);
void script_delay_call(const char *status, int timeout);
bool odhcp6c_signal_process(void);
uint64_t odhcp6c_get_milli_time(void);
void odhcp6c_random(void *buf, size_t len);
// State manipulation
void odhcp6c_clear_state(enum odhcp6c_state state);
void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len);
size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len);
void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len);
void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len);
// Entry manipulation
struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new);
void odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new);
void odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe);
void odhcp6c_expire(void);
uint32_t odhcp6c_elapsed(void);
|