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