2 * Copyright (C) 2012-2014 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
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
34 #define DEFAULT_MIN_UPDATE_INTERVAL 30
37 DHCPV6_OPT_CLIENTID = 1,
38 DHCPV6_OPT_SERVERID = 2,
41 DHCPV6_OPT_IA_ADDR = 5,
44 DHCPV6_OPT_ELAPSED = 8,
45 DHCPV6_OPT_RELAY_MSG = 9,
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,
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_PREFIX_CLASS
69 /* draft-bhandari-dhc-class-based-prefix, not yet standardized */
70 DHCPV6_OPT_PREFIX_CLASS = EXT_PREFIX_CLASS,
73 /* draft-donley-dhc-cer-id-option-03 */
74 DHCPV6_OPT_CER_ID = EXT_CER_ID,
76 /* draft-ietf-softwire-map-dhcp-08 */
77 DHCPV6_OPT_S46_RULE = 89,
78 DHCPV6_OPT_S46_BR = 90,
79 DHCPV6_OPT_S46_DMR = 91,
80 DHCPV6_OPT_S46_V4V6BIND = 92,
81 DHCPV6_OPT_S46_PORTPARAMS = 93,
82 DHCPV6_OPT_S46_CONT_MAPE = 94,
83 DHCPV6_OPT_S46_CONT_MAPT = 95,
84 DHCPV6_OPT_S46_CONT_LW = 96,
94 DHCPV6_MSG_UNKNOWN = 0,
95 DHCPV6_MSG_SOLICIT = 1,
96 DHCPV6_MSG_ADVERT = 2,
97 DHCPV6_MSG_REQUEST = 3,
99 DHCPV6_MSG_REBIND = 6,
100 DHCPV6_MSG_REPLY = 7,
101 DHCPV6_MSG_RELEASE = 8,
102 DHCPV6_MSG_DECLINE = 9,
103 DHCPV6_MSG_RECONF = 10,
104 DHCPV6_MSG_INFO_REQ = 11,
110 DHCPV6_UnspecFail = 1,
111 DHCPV6_NoAddrsAvail = 2,
112 DHCPV6_NoBinding = 3,
113 DHCPV6_NotOnLink = 4,
114 DHCPV6_UseMulticast = 5,
115 DHCPV6_NoPrefixAvail = 6,
120 DHCPV6_STRICT_OPTIONS = 1,
121 DHCPV6_CLIENT_FQDN = 2,
122 DHCPV6_ACCEPT_RECONFIGURE = 4,
125 typedef int(reply_handler)(enum dhcpv6_msg orig, const int rc,
126 const void *opt, const void *end, const struct sockaddr_in6 *from);
128 // retransmission strategy
135 reply_handler *handler_reply;
136 int(*handler_finish)(void);
139 // DHCPv6 Protocol Headers
140 struct dhcpv6_header {
143 } __attribute__((packed));
145 struct dhcpv6_ia_hdr {
153 struct dhcpv6_ia_addr {
156 struct in6_addr addr;
161 struct dhcpv6_ia_prefix {
167 struct in6_addr addr;
177 struct dhcpv6_auth_reconfigure {
188 struct dhcpv6_cer_id {
191 struct in6_addr addr;
194 struct dhcpv6_s46_portparams {
200 struct dhcpv6_s46_v4v6bind {
201 struct in_addr ipv4_address;
202 uint8_t bindprefix6_len;
203 uint8_t bind_ipv6_prefix[];
206 struct dhcpv6_s46_dmr {
207 uint8_t dmr_prefix6_len;
208 uint8_t dmr_ipv6_prefix[];
211 struct dhcpv6_s46_rule {
215 struct in_addr ipv4_prefix;
217 uint8_t ipv6_prefix[];
220 #define dhcpv6_for_each_option(start, end, otype, olen, odata)\
221 for (uint8_t *_o = (uint8_t*)(start); _o + 4 <= (uint8_t*)(end) &&\
222 ((otype) = _o[0] << 8 | _o[1]) && ((odata) = (void*)&_o[4]) &&\
223 ((olen) = _o[2] << 8 | _o[3]) + (odata) <= (uint8_t*)(end); \
224 _o += 4 + (_o[2] << 8 | _o[3]))
227 struct dhcpv6_server_cand {
228 bool has_noaddravail;
229 bool wants_reconfigure;
287 enum odhcp6c_ia_mode {
294 struct odhcp6c_entry {
295 struct in6_addr router;
298 struct in6_addr target;
307 struct odhcp6c_request_prefix {
312 int init_dhcpv6(const char *ifname, unsigned int client_options, int sol_timeout);
313 void dhcpv6_set_ia_mode(enum odhcp6c_ia_mode na, enum odhcp6c_ia_mode pd);
314 int dhcpv6_request(enum dhcpv6_msg type);
315 int dhcpv6_poll_reconfigure(void);
316 int dhcpv6_promote_server_cand(void);
318 int init_rtnetlink(void);
319 int set_rtnetlink_addr(int ifindex, const struct in6_addr *addr,
320 uint32_t pref, uint32_t valid);
322 int ra_conf_hoplimit(int newvalue);
323 int ra_conf_mtu(int newvalue);
324 int ra_conf_reachable(int newvalue);
325 int ra_conf_retransmit(int newvalue);
327 int script_init(const char *path, const char *ifname);
328 ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src);
329 void script_call(const char *status);
331 bool odhcp6c_signal_process(void);
332 uint64_t odhcp6c_get_milli_time(void);
333 int odhcp6c_random(void *buf, size_t len);
334 bool odhcp6c_is_bound(void);
336 // State manipulation
337 void odhcp6c_clear_state(enum odhcp6c_state state);
338 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len);
339 void odhcp6c_append_state(enum odhcp6c_state state, const void *data, size_t len);
340 void odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len);
341 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len);
342 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len);
343 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len);
345 // Entry manipulation
346 struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new);
347 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe, bool filterexcess);
349 void odhcp6c_expire(void);
350 uint32_t odhcp6c_elapsed(void);