024ebea5476b49b907e59895899c3a3388a6125e
[project/odhcp6c.git] / src / odhcp6c.h
1 /**
2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
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 */
14 #pragma once
15 #include <stdint.h>
16 #include <stdbool.h>
17 #include <netinet/in.h>
18
19 #define _unused __attribute__((unused))
20 #define _packed __attribute__((packed))
21
22 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
23
24 #define ND_OPT_RECURSIVE_DNS 25
25 #define ND_OPT_DNSSL 31
26
27 #define DHCPV6_SOL_MAX_RT 3600
28 #define DHCPV6_REQ_MAX_RT 30
29 #define DHCPV6_CNF_MAX_RT 4
30 #define DHCPV6_REN_MAX_RT 600
31 #define DHCPV6_REB_MAX_RT 600
32 #define DHCPV6_INF_MAX_RT 3600
33
34 enum dhcvp6_opt {
35 DHCPV6_OPT_CLIENTID = 1,
36 DHCPV6_OPT_SERVERID = 2,
37 DHCPV6_OPT_IA_NA = 3,
38 DHCPV6_OPT_IA_ADDR = 5,
39 DHCPV6_OPT_ORO = 6,
40 DHCPV6_OPT_PREF = 7,
41 DHCPV6_OPT_ELAPSED = 8,
42 DHCPV6_OPT_RELAY_MSG = 9,
43 DHCPV6_OPT_AUTH = 11,
44 DHCPV6_OPT_STATUS = 13,
45 DHCPV6_OPT_RAPID_COMMIT = 14,
46 DHCPV6_OPT_RECONF_MESSAGE = 19,
47 DHCPV6_OPT_RECONF_ACCEPT = 20,
48 DHCPV6_OPT_DNS_SERVERS = 23,
49 DHCPV6_OPT_DNS_DOMAIN = 24,
50 DHCPV6_OPT_IA_PD = 25,
51 DHCPV6_OPT_IA_PREFIX = 26,
52 DHCPV6_OPT_SNTP_SERVERS = 31,
53 DHCPV6_OPT_INFO_REFRESH = 32,
54 DHCPV6_OPT_FQDN = 39,
55 DHCPV6_OPT_NTP_SERVER = 56,
56 DHCPV6_OPT_SIP_SERVER_D = 21,
57 DHCPV6_OPT_SIP_SERVER_A = 22,
58 DHCPV6_OPT_AFTR_NAME = 64,
59 DHCPV6_OPT_PD_EXCLUDE = 67,
60 DHCPV6_OPT_SOL_MAX_RT = 82,
61 DHCPV6_OPT_INF_MAX_RT = 83,
62 #ifdef EXT_PREFIX_CLASS
63 /* draft-bhandari-dhc-class-based-prefix, not yet standardized */
64 DHCPV6_OPT_PREFIX_CLASS = EXT_PREFIX_CLASS,
65 #endif
66 };
67
68 enum dhcpv6_opt_npt {
69 NTP_SRV_ADDR = 1,
70 NTP_MC_ADDR = 2,
71 NTP_SRV_FQDN = 3
72 };
73
74 enum dhcpv6_msg {
75 DHCPV6_MSG_UNKNOWN = 0,
76 DHCPV6_MSG_SOLICIT = 1,
77 DHCPV6_MSG_ADVERT = 2,
78 DHCPV6_MSG_REQUEST = 3,
79 DHCPV6_MSG_RENEW = 5,
80 DHCPV6_MSG_REBIND = 6,
81 DHCPV6_MSG_REPLY = 7,
82 DHCPV6_MSG_RELEASE = 8,
83 DHCPV6_MSG_DECLINE = 9,
84 DHCPV6_MSG_RECONF = 10,
85 DHCPV6_MSG_INFO_REQ = 11,
86 _DHCPV6_MSG_MAX
87 };
88
89 enum dhcpv6_status {
90 DHCPV6_Success = 0,
91 DHCPV6_UnspecFail = 1,
92 DHCPV6_NoAddrsAvail = 2,
93 DHCPV6_NoBinding = 3,
94 DHCPV6_NotOnLink = 4,
95 DHCPV6_UseMulticast = 5,
96 DHCPV6_NoPrefixAvail = 6,
97 _DHCPV6_Status_Max
98 };
99
100 enum dhcpv6_config {
101 DHCPV6_STRICT_OPTIONS = 1,
102 DHCPV6_CLIENT_FQDN = 2,
103 DHCPV6_ACCEPT_RECONFIGURE = 4,
104 };
105
106 typedef int(reply_handler)(enum dhcpv6_msg orig, const int rc,
107 const void *opt, const void *end);
108
109 // retransmission strategy
110 struct dhcpv6_retx {
111 bool delay;
112 uint8_t init_timeo;
113 uint16_t max_timeo;
114 uint8_t max_rc;
115 char name[8];
116 reply_handler *handler_reply;
117 int(*handler_finish)(void);
118 };
119
120 // DHCPv6 Protocol Headers
121 struct dhcpv6_header {
122 uint8_t msg_type;
123 uint8_t tr_id[3];
124 } __attribute__((packed));
125
126 struct dhcpv6_ia_hdr {
127 uint16_t type;
128 uint16_t len;
129 uint32_t iaid;
130 uint32_t t1;
131 uint32_t t2;
132 } _packed;
133
134 struct dhcpv6_ia_addr {
135 uint16_t type;
136 uint16_t len;
137 struct in6_addr addr;
138 uint32_t preferred;
139 uint32_t valid;
140 } _packed;
141
142 struct dhcpv6_ia_prefix {
143 uint16_t type;
144 uint16_t len;
145 uint32_t preferred;
146 uint32_t valid;
147 uint8_t prefix;
148 struct in6_addr addr;
149 } _packed;
150
151 struct dhcpv6_duid {
152 uint16_t type;
153 uint16_t len;
154 uint16_t duid_type;
155 uint8_t data[128];
156 } _packed;
157
158 struct dhcpv6_auth_reconfigure {
159 uint16_t type;
160 uint16_t len;
161 uint8_t protocol;
162 uint8_t algorithm;
163 uint8_t rdm;
164 uint64_t replay;
165 uint8_t reconf_type;
166 uint8_t key[16];
167 } _packed;
168
169
170 #define dhcpv6_for_each_option(start, end, otype, olen, odata)\
171 for (uint8_t *_o = (uint8_t*)(start); _o + 4 <= (uint8_t*)(end) &&\
172 ((otype) = _o[0] << 8 | _o[1]) && ((odata) = (void*)&_o[4]) &&\
173 ((olen) = _o[2] << 8 | _o[3]) + (odata) <= (uint8_t*)(end); \
174 _o += 4 + (_o[2] << 8 | _o[3]))
175
176
177 struct dhcpv6_server_cand {
178 bool has_noaddravail;
179 bool wants_reconfigure;
180 int16_t preference;
181 uint8_t duid_len;
182 uint8_t duid[130];
183 uint32_t sol_max_rt;
184 uint32_t inf_max_rt;
185 void *ia_na;
186 void *ia_pd;
187 size_t ia_na_len;
188 size_t ia_pd_len;
189 };
190
191
192 enum odhcp6c_state {
193 STATE_CLIENT_ID,
194 STATE_SERVER_ID,
195 STATE_SERVER_CAND,
196 STATE_ORO,
197 STATE_DNS,
198 STATE_SEARCH,
199 STATE_IA_NA,
200 STATE_IA_PD,
201 STATE_IA_PD_INIT,
202 STATE_CUSTOM_OPTS,
203 STATE_SNTP_IP,
204 STATE_NTP_IP,
205 STATE_NTP_FQDN,
206 STATE_SIP_IP,
207 STATE_SIP_FQDN,
208 STATE_RA_ROUTE,
209 STATE_RA_PREFIX,
210 STATE_RA_DNS,
211 STATE_AFTR_NAME,
212 _STATE_MAX
213 };
214
215
216 struct icmp6_opt {
217 uint8_t type;
218 uint8_t len;
219 uint8_t data[6];
220 };
221
222
223 enum dhcpv6_mode {
224 DHCPV6_UNKNOWN = -1,
225 DHCPV6_STATELESS,
226 DHCPV6_STATEFUL
227 };
228
229 enum odhcp6c_ia_mode {
230 IA_MODE_NONE,
231 IA_MODE_TRY,
232 IA_MODE_FORCE,
233 };
234
235
236 struct odhcp6c_entry {
237 struct in6_addr router;
238 uint16_t length;
239 int16_t priority;
240 struct in6_addr target;
241 uint32_t valid;
242 uint32_t preferred;
243 uint32_t t1;
244 uint32_t t2;
245 uint16_t class;
246 uint32_t iaid;
247 };
248
249 struct odhcp6c_request_prefix {
250 uint32_t iaid;
251 uint16_t length;
252 };
253
254 int init_dhcpv6(const char *ifname, unsigned int client_options, int sol_timeout);
255 void dhcpv6_set_ia_mode(enum odhcp6c_ia_mode na, enum odhcp6c_ia_mode pd);
256 int dhcpv6_request(enum dhcpv6_msg type);
257 int dhcpv6_poll_reconfigure(void);
258 int dhcpv6_promote_server_cand(void);
259
260 int init_rtnetlink(void);
261 int set_rtnetlink_addr(int ifindex, const struct in6_addr *addr,
262 uint32_t pref, uint32_t valid);
263
264 int script_init(const char *path, const char *ifname);
265 ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src);
266 void script_call(const char *status);
267 void script_delay_call(const char *status, int timeout);
268
269 bool odhcp6c_signal_process(void);
270 uint64_t odhcp6c_get_milli_time(void);
271 void odhcp6c_random(void *buf, size_t len);
272 bool odhcp6c_is_bound(void);
273
274 // State manipulation
275 void odhcp6c_clear_state(enum odhcp6c_state state);
276 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len);
277 void odhcp6c_append_state(enum odhcp6c_state state, const void *data, size_t len);
278 void odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len);
279 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len);
280 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len);
281 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len);
282
283 // Entry manipulation
284 struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new);
285 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new);
286 bool odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe);
287
288 void odhcp6c_expire(void);
289 uint32_t odhcp6c_elapsed(void);