]> git.decadent.org.uk Git - odhcp6c.git/blob - src/odhcp6c.h
104213681c8b18001bf5a131ceb98c9a12c61c26
[odhcp6c.git] / src / odhcp6c.h
1 /**
2  * Copyright (C) 2012-2014 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 #define DEFAULT_MIN_UPDATE_INTERVAL 30
35
36 enum dhcvp6_opt {
37         DHCPV6_OPT_CLIENTID = 1,
38         DHCPV6_OPT_SERVERID = 2,
39         DHCPV6_OPT_IA_NA = 3,
40         DHCPV6_OPT_IA_TA = 4,
41         DHCPV6_OPT_IA_ADDR = 5,
42         DHCPV6_OPT_ORO = 6,
43         DHCPV6_OPT_PREF = 7,
44         DHCPV6_OPT_ELAPSED = 8,
45         DHCPV6_OPT_RELAY_MSG = 9,
46         DHCPV6_OPT_AUTH = 11,
47         DHCPV6_OPT_UNICAST = 12,
48         DHCPV6_OPT_STATUS = 13,
49         DHCPV6_OPT_RAPID_COMMIT = 14,
50         DHCPV6_OPT_USER_CLASS = 15,
51         DHCPV6_OPT_VENDOR_CLASS = 16,
52         DHCPV6_OPT_RECONF_MESSAGE = 19,
53         DHCPV6_OPT_RECONF_ACCEPT = 20,
54         DHCPV6_OPT_DNS_SERVERS = 23,
55         DHCPV6_OPT_DNS_DOMAIN = 24,
56         DHCPV6_OPT_IA_PD = 25,
57         DHCPV6_OPT_IA_PREFIX = 26,
58         DHCPV6_OPT_SNTP_SERVERS = 31,
59         DHCPV6_OPT_INFO_REFRESH = 32,
60         DHCPV6_OPT_FQDN = 39,
61         DHCPV6_OPT_NTP_SERVER = 56,
62         DHCPV6_OPT_SIP_SERVER_D = 21,
63         DHCPV6_OPT_SIP_SERVER_A = 22,
64         DHCPV6_OPT_AFTR_NAME = 64,
65         DHCPV6_OPT_PD_EXCLUDE = 67,
66         DHCPV6_OPT_SOL_MAX_RT = 82,
67         DHCPV6_OPT_INF_MAX_RT = 83,
68 #ifdef EXT_CER_ID
69         /* draft-donley-dhc-cer-id-option-03 */
70         DHCPV6_OPT_CER_ID = EXT_CER_ID,
71 #endif
72         /* draft-ietf-softwire-map-dhcp-08 */
73         DHCPV6_OPT_S46_RULE = 89,
74         DHCPV6_OPT_S46_BR = 90,
75         DHCPV6_OPT_S46_DMR = 91,
76         DHCPV6_OPT_S46_V4V6BIND = 92,
77         DHCPV6_OPT_S46_PORTPARAMS = 93,
78         DHCPV6_OPT_S46_CONT_MAPE = 94,
79         DHCPV6_OPT_S46_CONT_MAPT = 95,
80         DHCPV6_OPT_S46_CONT_LW = 96,
81 };
82
83 enum dhcpv6_opt_npt {
84         NTP_SRV_ADDR = 1,
85         NTP_MC_ADDR = 2,
86         NTP_SRV_FQDN = 3
87 };
88
89 enum dhcpv6_msg {
90         DHCPV6_MSG_UNKNOWN = 0,
91         DHCPV6_MSG_SOLICIT = 1,
92         DHCPV6_MSG_ADVERT = 2,
93         DHCPV6_MSG_REQUEST = 3,
94         DHCPV6_MSG_RENEW = 5,
95         DHCPV6_MSG_REBIND = 6,
96         DHCPV6_MSG_REPLY = 7,
97         DHCPV6_MSG_RELEASE = 8,
98         DHCPV6_MSG_DECLINE = 9,
99         DHCPV6_MSG_RECONF = 10,
100         DHCPV6_MSG_INFO_REQ = 11,
101         _DHCPV6_MSG_MAX
102 };
103
104 enum dhcpv6_status {
105         DHCPV6_Success = 0,
106         DHCPV6_UnspecFail = 1,
107         DHCPV6_NoAddrsAvail = 2,
108         DHCPV6_NoBinding = 3,
109         DHCPV6_NotOnLink = 4,
110         DHCPV6_UseMulticast = 5,
111         DHCPV6_NoPrefixAvail = 6,
112         _DHCPV6_Status_Max
113 };
114
115 enum dhcpv6_config {
116         DHCPV6_STRICT_OPTIONS = 1,
117         DHCPV6_CLIENT_FQDN = 2,
118         DHCPV6_ACCEPT_RECONFIGURE = 4,
119 };
120
121 typedef int(reply_handler)(enum dhcpv6_msg orig, const int rc,
122                 const void *opt, const void *end, const struct sockaddr_in6 *from);
123
124 // retransmission strategy
125 struct dhcpv6_retx {
126         bool delay;
127         uint8_t init_timeo;
128         uint16_t max_timeo;
129         uint8_t max_rc;
130         char name[8];
131         reply_handler *handler_reply;
132         int(*handler_finish)(void);
133 };
134
135 // DHCPv6 Protocol Headers
136 struct dhcpv6_header {
137         uint8_t msg_type;
138         uint8_t tr_id[3];
139 } __attribute__((packed));
140
141 struct dhcpv6_ia_hdr {
142         uint16_t type;
143         uint16_t len;
144         uint32_t iaid;
145         uint32_t t1;
146         uint32_t t2;
147 } _packed;
148
149 struct dhcpv6_ia_addr {
150         uint16_t type;
151         uint16_t len;
152         struct in6_addr addr;
153         uint32_t preferred;
154         uint32_t valid;
155 } _packed;
156
157 struct dhcpv6_ia_prefix {
158         uint16_t type;
159         uint16_t len;
160         uint32_t preferred;
161         uint32_t valid;
162         uint8_t prefix;
163         struct in6_addr addr;
164 } _packed;
165
166 struct dhcpv6_duid {
167         uint16_t type;
168         uint16_t len;
169         uint16_t duid_type;
170         uint8_t data[128];
171 } _packed;
172
173 struct dhcpv6_auth_reconfigure {
174         uint16_t type;
175         uint16_t len;
176         uint8_t protocol;
177         uint8_t algorithm;
178         uint8_t rdm;
179         uint64_t replay;
180         uint8_t reconf_type;
181         uint8_t key[16];
182 } _packed;
183
184 struct dhcpv6_cer_id {
185         uint16_t type;
186         uint16_t len;
187         struct in6_addr addr;
188 } _packed;
189
190 struct dhcpv6_s46_portparams {
191         uint8_t offset;
192         uint8_t psid_len;
193         uint16_t psid;
194 } _packed;
195
196 struct dhcpv6_s46_v4v6bind {
197         struct in_addr ipv4_address;
198         uint8_t bindprefix6_len;
199         uint8_t bind_ipv6_prefix[];
200 } _packed;
201
202 struct dhcpv6_s46_dmr {
203         uint8_t dmr_prefix6_len;
204         uint8_t dmr_ipv6_prefix[];
205 } _packed;
206
207 struct dhcpv6_s46_rule {
208         uint8_t flags;
209         uint8_t ea_len;
210         uint8_t prefix4_len;
211         struct in_addr ipv4_prefix;
212         uint8_t prefix6_len;
213         uint8_t ipv6_prefix[];
214 } _packed;
215
216 #define dhcpv6_for_each_option(start, end, otype, olen, odata)\
217         for (uint8_t *_o = (uint8_t*)(start); _o + 4 <= (uint8_t*)(end) &&\
218                 ((otype) = _o[0] << 8 | _o[1]) && ((odata) = (void*)&_o[4]) &&\
219                 ((olen) = _o[2] << 8 | _o[3]) + (odata) <= (uint8_t*)(end); \
220                 _o += 4 + (_o[2] << 8 | _o[3]))
221
222
223 struct dhcpv6_server_cand {
224         bool has_noaddravail;
225         bool wants_reconfigure;
226         int16_t preference;
227         uint8_t duid_len;
228         uint8_t duid[130];
229         uint32_t sol_max_rt;
230         uint32_t inf_max_rt;
231         void *ia_na;
232         void *ia_pd;
233         size_t ia_na_len;
234         size_t ia_pd_len;
235 };
236
237
238 enum odhcp6c_state {
239         STATE_CLIENT_ID,
240         STATE_SERVER_ID,
241         STATE_SERVER_CAND,
242         STATE_SERVER_ADDR,
243         STATE_ORO,
244         STATE_DNS,
245         STATE_SEARCH,
246         STATE_IA_NA,
247         STATE_IA_PD,
248         STATE_IA_PD_INIT,
249         STATE_CUSTOM_OPTS,
250         STATE_SNTP_IP,
251         STATE_NTP_IP,
252         STATE_NTP_FQDN,
253         STATE_SIP_IP,
254         STATE_SIP_FQDN,
255         STATE_RA_ROUTE,
256         STATE_RA_PREFIX,
257         STATE_RA_DNS,
258         STATE_AFTR_NAME,
259         STATE_VENDORCLASS,
260         STATE_USERCLASS,
261         STATE_CER,
262         STATE_S46_MAPT,
263         STATE_S46_MAPE,
264         STATE_S46_LW,
265         STATE_PASSTHRU,
266         _STATE_MAX
267 };
268
269
270 struct icmp6_opt {
271         uint8_t type;
272         uint8_t len;
273         uint8_t data[6];
274 };
275
276
277 enum dhcpv6_mode {
278         DHCPV6_UNKNOWN = -1,
279         DHCPV6_STATELESS,
280         DHCPV6_STATEFUL
281 };
282
283 enum odhcp6c_ia_mode {
284         IA_MODE_NONE,
285         IA_MODE_TRY,
286         IA_MODE_FORCE,
287 };
288
289
290 struct odhcp6c_entry {
291         struct in6_addr router;
292         uint16_t length;
293         int16_t priority;
294         struct in6_addr target;
295         uint32_t valid;
296         uint32_t preferred;
297         uint32_t t1;
298         uint32_t t2;
299         uint32_t iaid;
300 };
301
302 struct odhcp6c_request_prefix {
303         uint32_t iaid;
304         uint16_t length;
305 };
306
307 int init_dhcpv6(const char *ifname, unsigned int client_options, int sol_timeout);
308 void dhcpv6_set_ia_mode(enum odhcp6c_ia_mode na, enum odhcp6c_ia_mode pd);
309 int dhcpv6_request(enum dhcpv6_msg type);
310 int dhcpv6_poll_reconfigure(void);
311 int dhcpv6_promote_server_cand(void);
312
313 int init_rtnetlink(void);
314 int set_rtnetlink_addr(int ifindex, const struct in6_addr *addr,
315                 uint32_t pref, uint32_t valid);
316
317 int ra_conf_hoplimit(int newvalue);
318 int ra_conf_mtu(int newvalue);
319 int ra_conf_reachable(int newvalue);
320 int ra_conf_retransmit(int newvalue);
321
322 int script_init(const char *path, const char *ifname);
323 ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src);
324 void script_call(const char *status);
325
326 bool odhcp6c_signal_process(void);
327 uint64_t odhcp6c_get_milli_time(void);
328 int odhcp6c_random(void *buf, size_t len);
329 bool odhcp6c_is_bound(void);
330
331 // State manipulation
332 void odhcp6c_clear_state(enum odhcp6c_state state);
333 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len);
334 void odhcp6c_append_state(enum odhcp6c_state state, const void *data, size_t len);
335 void odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len);
336 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len);
337 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len);
338 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len);
339
340 // Entry manipulation
341 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe, bool filterexcess);
342
343 void odhcp6c_expire(void);
344 uint32_t odhcp6c_elapsed(void);