2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
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.
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.
17 #include <netinet/in.h>
19 #define _unused __attribute__((unused))
20 #define _packed __attribute__((packed))
22 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
24 #define ND_OPT_RECURSIVE_DNS 25
25 #define ND_OPT_DNSSL 31
28 DHCPV6_OPT_CLIENTID = 1,
29 DHCPV6_OPT_SERVERID = 2,
31 DHCPV6_OPT_IA_ADDR = 5,
34 DHCPV6_OPT_ELAPSED = 8,
35 DHCPV6_OPT_RELAY_MSG = 9,
37 DHCPV6_OPT_STATUS = 13,
38 DHCPV6_OPT_RAPID_COMMIT = 14,
39 DHCPV6_OPT_RECONF_MESSAGE = 19,
40 DHCPV6_OPT_RECONF_ACCEPT = 20,
41 DHCPV6_OPT_DNS_SERVERS = 23,
42 DHCPV6_OPT_DNS_DOMAIN = 24,
43 DHCPV6_OPT_IA_PD = 25,
44 DHCPV6_OPT_IA_PREFIX = 26,
45 DHCPV6_OPT_INFO_REFRESH = 32,
47 DHCPV6_OPT_NTP_SERVER = 56,
48 DHCPV6_OPT_SIP_SERVER_D = 21,
49 DHCPV6_OPT_SIP_SERVER_A = 22,
50 DHCPV6_OPT_AFTR_NAME = 64,
51 DHCPV6_OPT_PD_EXCLUDE = 67,
52 #ifdef EXT_PREFIX_CLASS
53 /* draft-bhandari-dhc-class-based-prefix, not yet standardized */
54 DHCPV6_OPT_PREFIX_CLASS = EXT_PREFIX_CLASS,
65 DHCPV6_MSG_UNKNOWN = 0,
66 DHCPV6_MSG_SOLICIT = 1,
67 DHCPV6_MSG_ADVERT = 2,
68 DHCPV6_MSG_REQUEST = 3,
70 DHCPV6_MSG_REBIND = 6,
72 DHCPV6_MSG_RELEASE = 8,
73 DHCPV6_MSG_DECLINE = 9,
74 DHCPV6_MSG_RECONF = 10,
75 DHCPV6_MSG_INFO_REQ = 11,
81 DHCPV6_UnspecFail = 1,
82 DHCPV6_NoAddrsAvail = 2,
85 DHCPV6_UseMulticast = 5,
86 DHCPV6_NoPrefixAvail = 6,
90 typedef int(reply_handler)(enum dhcpv6_msg orig, const int rc,
91 const void *opt, const void *end);
93 // retransmission strategy
100 reply_handler *handler_reply;
101 int(*handler_finish)(void);
104 // DHCPv6 Protocol Headers
105 struct dhcpv6_header {
108 } __attribute__((packed));
110 struct dhcpv6_ia_hdr {
118 struct dhcpv6_ia_addr {
121 struct in6_addr addr;
126 struct dhcpv6_ia_prefix {
132 struct in6_addr addr;
142 struct dhcpv6_auth_reconfigure {
154 #define dhcpv6_for_each_option(start, end, otype, olen, odata)\
155 for (uint8_t *_o = (uint8_t*)(start); _o + 4 <= (uint8_t*)(end) &&\
156 ((otype) = _o[0] << 8 | _o[1]) && ((odata) = (void*)&_o[4]) &&\
157 ((olen) = _o[2] << 8 | _o[3]) + (odata) <= (uint8_t*)(end); \
158 _o += 4 + (_o[2] << 8 | _o[3]))
161 struct dhcpv6_server_cand {
162 bool has_noaddravail;
163 bool wants_reconfigure;
210 enum odhcp6c_ia_mode {
217 struct odhcp6c_entry {
218 struct in6_addr router;
221 struct in6_addr target;
230 int init_dhcpv6(const char *ifname, int request_pd, int sol_timeout);
231 void dhcpv6_set_ia_mode(enum odhcp6c_ia_mode na, enum odhcp6c_ia_mode pd);
232 int dhcpv6_request(enum dhcpv6_msg type);
233 int dhcpv6_poll_reconfigure(void);
235 int init_rtnetlink(void);
236 int set_rtnetlink_addr(int ifindex, const struct in6_addr *addr,
237 uint32_t pref, uint32_t valid);
239 int script_init(const char *path, const char *ifname);
240 ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src);
241 void script_call(const char *status);
242 void script_delay_call(const char *status, int timeout);
244 bool odhcp6c_signal_process(void);
245 uint64_t odhcp6c_get_milli_time(void);
246 void odhcp6c_random(void *buf, size_t len);
247 bool odhcp6c_is_bound(void);
249 // State manipulation
250 void odhcp6c_clear_state(enum odhcp6c_state state);
251 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len);
252 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len);
253 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len);
254 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len);
256 // Entry manipulation
257 struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new);
258 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new);
259 bool odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe);
261 void odhcp6c_expire(void);
262 uint32_t odhcp6c_elapsed(void);