• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/odhcp6c/src/odhcp6c.h

  1 /**
  2  * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
  3  * Copyright (C) 2018 Hans Dedecker <dedeckeh@gmail.com>
  4  *
  5  * This program is free software; you can redistribute it and/or modify
  6  * it under the terms of the GNU General Public License v2 as published by
  7  * the Free Software Foundation.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  *
 14  */
 15 #pragma once
 16 #include <stdint.h>
 17 #include <stdbool.h>
 18 #include <netinet/in.h>
 19 
 20 #define _unused __attribute__((unused))
 21 #define _packed __attribute__((packed))
 22 #define _aligned(n) __attribute__((aligned(n)))
 23 
 24 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 25 
 26 #define ND_OPT_RECURSIVE_DNS 25
 27 #define ND_OPT_DNSSL 31
 28 
 29 #define DHCPV6_SOL_MAX_RT 120
 30 #define DHCPV6_REQ_MAX_RT 30
 31 #define DHCPV6_CNF_MAX_RT 4
 32 #define DHCPV6_REN_MAX_RT 600
 33 #define DHCPV6_REB_MAX_RT 600
 34 #define DHCPV6_INF_MAX_RT 120
 35 
 36 #define RA_MIN_ADV_INTERVAL 3   /* RFC 4861 paragraph 6.2.1 */
 37 
 38 enum dhcvp6_opt {
 39         DHCPV6_OPT_CLIENTID = 1,
 40         DHCPV6_OPT_SERVERID = 2,
 41         DHCPV6_OPT_IA_NA = 3,
 42         DHCPV6_OPT_IA_TA = 4,
 43         DHCPV6_OPT_IA_ADDR = 5,
 44         DHCPV6_OPT_ORO = 6,
 45         DHCPV6_OPT_PREF = 7,
 46         DHCPV6_OPT_ELAPSED = 8,
 47         DHCPV6_OPT_RELAY_MSG = 9,
 48         DHCPV6_OPT_AUTH = 11,
 49         DHCPV6_OPT_UNICAST = 12,
 50         DHCPV6_OPT_STATUS = 13,
 51         DHCPV6_OPT_RAPID_COMMIT = 14,
 52         DHCPV6_OPT_USER_CLASS = 15,
 53         DHCPV6_OPT_VENDOR_CLASS = 16,
 54         DHCPV6_OPT_INTERFACE_ID = 18,
 55         DHCPV6_OPT_RECONF_MESSAGE = 19,
 56         DHCPV6_OPT_RECONF_ACCEPT = 20,
 57         DHCPV6_OPT_SIP_SERVER_D = 21,
 58         DHCPV6_OPT_SIP_SERVER_A = 22,
 59         DHCPV6_OPT_DNS_SERVERS = 23,
 60         DHCPV6_OPT_DNS_DOMAIN = 24,
 61         DHCPV6_OPT_IA_PD = 25,
 62         DHCPV6_OPT_IA_PREFIX = 26,
 63         DHCPV6_OPT_SNTP_SERVERS = 31,
 64         DHCPV6_OPT_INFO_REFRESH = 32,
 65         DHCPV6_OPT_REMOTE_ID = 37,
 66         DHCPV6_OPT_SUBSCRIBER_ID = 38,
 67         DHCPV6_OPT_FQDN = 39,
 68         DHCPV6_OPT_ERO = 43,
 69         DHCPV6_OPT_LQ_QUERY = 44,
 70         DHCPV6_OPT_CLIENT_DATA = 45,
 71         DHCPV6_OPT_CLT_TIME = 46,
 72         DHCPV6_OPT_LQ_RELAY_DATA = 47,
 73         DHCPV6_OPT_LQ_CLIENT_LINK = 48,
 74         DHCPV6_OPT_RELAY_ID = 53,
 75         DHCPV6_OPT_NTP_SERVER = 56,
 76         DHCPV6_OPT_CLIENT_ARCH_TYPE = 61,
 77         DHCPV6_OPT_AFTR_NAME = 64,
 78         DHCPV6_OPT_RSOO = 66,
 79         DHCPV6_OPT_PD_EXCLUDE = 67,
 80         DHCPV6_OPT_VSS = 68,
 81         DHCPV6_OPT_LINK_LAYER_ADDRESS = 79,
 82         DHCPV6_OPT_LINK_ADDRESS = 80,
 83         DHCPV6_OPT_RADIUS = 81,
 84         DHCPV6_OPT_SOL_MAX_RT = 82,
 85         DHCPV6_OPT_INF_MAX_RT = 83,
 86 #ifdef EXT_CER_ID
 87         /* draft-donley-dhc-cer-id-option-03 */
 88         DHCPV6_OPT_CER_ID = EXT_CER_ID,
 89 #endif
 90         DHCPV6_OPT_DHCPV4_MSG = 87,
 91         /* draft-ietf-softwire-map-dhcp-08 */
 92         DHCPV6_OPT_S46_RULE = 89,
 93         DHCPV6_OPT_S46_BR = 90,
 94         DHCPV6_OPT_S46_DMR = 91,
 95         DHCPV6_OPT_S46_V4V6BIND = 92,
 96         DHCPV6_OPT_S46_PORTPARAMS = 93,
 97         DHCPV6_OPT_S46_CONT_MAPE = 94,
 98         DHCPV6_OPT_S46_CONT_MAPT = 95,
 99         DHCPV6_OPT_S46_CONT_LW = 96,
100         DHCPV6_OPT_LQ_BASE_TIME = 100,
101         DHCPV6_OPT_LQ_START_TIME = 101,
102         DHCPV6_OPT_LQ_END_TIME = 102,
103         DHCPV6_OPT_ANI_ATT = 105,
104         DHCPV6_OPT_ANI_NETWORK_NAME = 106,
105         DHCPV6_OPT_ANI_AP_NAME = 107,
106         DHCPV6_OPT_ANI_AP_BSSID = 108,
107         DHCPV6_OPT_ANI_OPERATOR_ID = 109,
108         DHCPV6_OPT_ANI_OPERATOR_REALM = 110,
109         DHCPV6_OPT_MUD_URL_V6 = 112,
110         DHCPV6_OPT_F_BINDING_STATUS = 114,
111         DHCPV6_OPT_F_CONNECT_FLAGS = 115,
112         DHCPV6_OPT_F_DNS_REMOVAL_INFO = 116,
113         DHCPV6_OPT_F_DNS_HOST_NAME = 117,
114         DHCPV6_OPT_F_DNS_ZONE_NAME = 118,
115         DHCPV6_OPT_F_DNS_FLAGS = 119,
116         DHCPV6_OPT_F_EXPIRATION_TIME = 120,
117         DHCPV6_OPT_F_MAX_UNACKED_BNDUPD = 121,
118         DHCPV6_OPT_F_MCLT = 122,
119         DHCPV6_OPT_F_PARTNER_LIFETIME = 123,
120         DHCPV6_OPT_F_PARTNER_LIFETIME_SENT = 124,
121         DHCPV6_OPT_F_PARTNER_DOWN_TIME = 125,
122         DHCPV6_OPT_F_PARTNER_RAW_CLT_TIME = 126,
123         DHCPV6_OPT_F_PROTOCOL_VERSION = 127,
124         DHCPV6_OPT_F_KEEPALIVE_TIME = 128,
125         DHCPV6_OPT_F_RECONFIGURE_DATA = 129,
126         DHCPV6_OPT_F_RELATIONSHIP_NAME = 130,
127         DHCPV6_OPT_F_SERVER_FLAGS = 131,
128         DHCPV6_OPT_F_SERVER_STATE = 132,
129         DHCPV6_OPT_F_START_TIME_OF_STATE = 133,
130         DHCPV6_OPT_F_STATE_EXPIRATION_TIME  = 134,
131         DHCPV6_OPT_RELAY_PORT = 135,
132 };
133 
134 enum dhcpv6_opt_npt {
135         NTP_SRV_ADDR = 1,
136         NTP_MC_ADDR = 2,
137         NTP_SRV_FQDN = 3
138 };
139 
140 enum dhcpv6_msg {
141         DHCPV6_MSG_UNKNOWN = 0,
142         DHCPV6_MSG_SOLICIT = 1,
143         DHCPV6_MSG_ADVERT = 2,
144         DHCPV6_MSG_REQUEST = 3,
145         DHCPV6_MSG_RENEW = 5,
146         DHCPV6_MSG_REBIND = 6,
147         DHCPV6_MSG_REPLY = 7,
148         DHCPV6_MSG_RELEASE = 8,
149         DHCPV6_MSG_DECLINE = 9,
150         DHCPV6_MSG_RECONF = 10,
151         DHCPV6_MSG_INFO_REQ = 11,
152         _DHCPV6_MSG_MAX
153 };
154 
155 enum dhcpv6_status {
156         DHCPV6_Success = 0,
157         DHCPV6_UnspecFail = 1,
158         DHCPV6_NoAddrsAvail = 2,
159         DHCPV6_NoBinding = 3,
160         DHCPV6_NotOnLink = 4,
161         DHCPV6_UseMulticast = 5,
162         DHCPV6_NoPrefixAvail = 6,
163         _DHCPV6_Status_Max
164 };
165 
166 enum dhcpv6_config {
167         DHCPV6_STRICT_OPTIONS = 1,
168         DHCPV6_CLIENT_FQDN = 2,
169         DHCPV6_ACCEPT_RECONFIGURE = 4,
170         DHCPV6_IGNORE_OPT_UNICAST = 8,
171 };
172 
173 typedef int(reply_handler)(enum dhcpv6_msg orig, const int rc,
174                 const void *opt, const void *end, const struct sockaddr_in6 *from);
175 
176 // retransmission strategy
177 struct dhcpv6_retx {
178         bool delay;
179         uint8_t init_timeo;
180         uint16_t max_timeo;
181         uint8_t max_rc;
182         char name[8];
183         reply_handler *handler_reply;
184         int(*handler_finish)(void);
185 };
186 
187 // DHCPv6 Protocol Headers
188 struct dhcpv6_header {
189         uint8_t msg_type;
190         uint8_t tr_id[3];
191 } __attribute__((packed));
192 
193 struct dhcpv6_ia_hdr {
194         uint16_t type;
195         uint16_t len;
196         uint32_t iaid;
197         uint32_t t1;
198         uint32_t t2;
199 } _packed;
200 
201 struct dhcpv6_ia_addr {
202         uint16_t type;
203         uint16_t len;
204         struct in6_addr addr;
205         uint32_t preferred;
206         uint32_t valid;
207 } _packed;
208 
209 struct dhcpv6_ia_prefix {
210         uint16_t type;
211         uint16_t len;
212         uint32_t preferred;
213         uint32_t valid;
214         uint8_t prefix;
215         struct in6_addr addr;
216 } _packed;
217 
218 struct dhcpv6_duid {
219         uint16_t type;
220         uint16_t len;
221         uint16_t duid_type;
222         uint8_t data[128];
223 } _packed;
224 
225 struct dhcpv6_auth_reconfigure {
226         uint16_t type;
227         uint16_t len;
228         uint8_t protocol;
229         uint8_t algorithm;
230         uint8_t rdm;
231         uint64_t replay;
232         uint8_t reconf_type;
233         uint8_t key[16];
234 } _packed;
235 
236 struct dhcpv6_cer_id {
237         uint16_t type;
238         uint16_t len;
239         struct in6_addr addr;
240 } _packed;
241 
242 struct dhcpv6_s46_portparams {
243         uint8_t offset;
244         uint8_t psid_len;
245         uint16_t psid;
246 } _packed;
247 
248 struct dhcpv6_s46_v4v6bind {
249         struct in_addr ipv4_address;
250         uint8_t bindprefix6_len;
251         uint8_t bind_ipv6_prefix[];
252 } _packed;
253 
254 struct dhcpv6_s46_dmr {
255         uint8_t dmr_prefix6_len;
256         uint8_t dmr_ipv6_prefix[];
257 } _packed;
258 
259 struct dhcpv6_s46_rule {
260         uint8_t flags;
261         uint8_t ea_len;
262         uint8_t prefix4_len;
263         struct in_addr ipv4_prefix;
264         uint8_t prefix6_len;
265         uint8_t ipv6_prefix[];
266 } _packed;
267 
268 #define dhcpv6_for_each_option(start, end, otype, olen, odata)\
269         for (uint8_t *_o = (uint8_t*)(start); _o + 4 <= (uint8_t*)(end) &&\
270                 ((otype) = _o[0] << 8 | _o[1]) && ((odata) = (void*)&_o[4]) &&\
271                 ((olen) = _o[2] << 8 | _o[3]) + (odata) <= (uint8_t*)(end); \
272                 _o += 4 + (_o[2] << 8 | _o[3]))
273 
274 
275 struct dhcpv6_server_cand {
276         bool has_noaddravail;
277         bool wants_reconfigure;
278         int16_t preference;
279         uint8_t duid_len;
280         uint8_t duid[130];
281         struct in6_addr server_addr;
282         uint32_t sol_max_rt;
283         uint32_t inf_max_rt;
284         void *ia_na;
285         void *ia_pd;
286         size_t ia_na_len;
287         size_t ia_pd_len;
288 };
289 
290 
291 enum odhcp6c_state {
292         STATE_CLIENT_ID,
293         STATE_SERVER_ID,
294         STATE_SERVER_CAND,
295         STATE_SERVER_ADDR,
296         STATE_ORO,
297         STATE_DNS,
298         STATE_SEARCH,
299         STATE_IA_NA,
300         STATE_IA_PD,
301         STATE_IA_PD_INIT,
302         STATE_CUSTOM_OPTS,
303         STATE_SNTP_IP,
304         STATE_NTP_IP,
305         STATE_NTP_FQDN,
306         STATE_SIP_IP,
307         STATE_SIP_FQDN,
308         STATE_RA_ROUTE,
309         STATE_RA_PREFIX,
310         STATE_RA_DNS,
311         STATE_RA_SEARCH,
312         STATE_AFTR_NAME,
313         STATE_OPTS,
314         STATE_CER,
315         STATE_S46_MAPT,
316         STATE_S46_MAPE,
317         STATE_S46_LW,
318         STATE_PASSTHRU,
319         _STATE_MAX
320 };
321 
322 
323 struct icmp6_opt {
324         uint8_t type;
325         uint8_t len;
326         uint8_t data[6];
327 };
328 
329 
330 enum dhcpv6_mode {
331         DHCPV6_UNKNOWN = -1,
332         DHCPV6_STATELESS,
333         DHCPV6_STATEFUL
334 };
335 
336 enum ra_config {
337         RA_RDNSS_DEFAULT_LIFETIME = 1,
338 };
339 
340 enum odhcp6c_ia_mode {
341         IA_MODE_NONE,
342         IA_MODE_TRY,
343         IA_MODE_FORCE,
344 };
345 
346 
347 struct odhcp6c_entry {
348         struct in6_addr router;
349         uint8_t auxlen;
350         uint8_t length;
351         int16_t priority;
352         struct in6_addr target;
353         uint32_t valid;
354         uint32_t preferred;
355         uint32_t t1;
356         uint32_t t2;
357         uint32_t iaid;
358         uint8_t auxtarget[];
359 };
360 
361 // Include padding after auxtarget to align the next entry
362 #define odhcp6c_entry_size(entry) \
363         (sizeof(struct odhcp6c_entry) + (((entry)->auxlen + 3) & ~3))
364 
365 #define odhcp6c_next_entry(entry) \
366         ((struct odhcp6c_entry *)((uint8_t *)(entry) + odhcp6c_entry_size(entry)))
367 
368 
369 struct odhcp6c_request_prefix {
370         uint32_t iaid;
371         uint16_t length;
372 };
373 
374 enum odhcp6c_opt_flags {
375         OPT_U8 = 0,
376         OPT_IP6,
377         OPT_STR,
378         OPT_DNS_STR,
379         OPT_USER_CLASS,
380         OPT_MASK_SIZE = 0x0F,
381         OPT_ARRAY = 0x10,
382         OPT_INTERNAL = 0x20,
383         OPT_NO_PASSTHRU = 0x40,
384         OPT_ORO = 0x80,
385         OPT_ORO_STATEFUL = 0x100,
386         OPT_ORO_STATELESS = 0x200,
387         OPT_ORO_SOLICIT = 0x400
388 };
389 
390 struct odhcp6c_opt {
391         uint16_t code;
392         uint16_t flags;
393         const char *str;
394 };
395 
396 int init_dhcpv6(const char *ifname, unsigned int client_options, int sol_timeout);
397 int dhcpv6_set_ia_mode(enum odhcp6c_ia_mode na, enum odhcp6c_ia_mode pd);
398 int dhcpv6_request(enum dhcpv6_msg type);
399 int dhcpv6_poll_reconfigure(void);
400 int dhcpv6_promote_server_cand(void);
401 
402 int init_rtnetlink(void);
403 int set_rtnetlink_addr(int ifindex, const struct in6_addr *addr,
404                 uint32_t pref, uint32_t valid);
405 
406 int ra_get_hoplimit(void);
407 int ra_get_mtu(void);
408 int ra_get_reachable(void);
409 int ra_get_retransmit(void);
410 
411 int script_init(const char *path, const char *ifname);
412 ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src);
413 void script_call(const char *status, int delay, bool resume);
414 
415 bool odhcp6c_signal_process(void);
416 uint64_t odhcp6c_get_milli_time(void);
417 int odhcp6c_random(void *buf, size_t len);
418 bool odhcp6c_is_bound(void);
419 bool odhcp6c_addr_in_scope(const struct in6_addr *addr);
420 
421 // State manipulation
422 void odhcp6c_clear_state(enum odhcp6c_state state);
423 int odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len);
424 void odhcp6c_append_state(enum odhcp6c_state state, const void *data, size_t len);
425 int odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len);
426 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len);
427 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len);
428 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len);
429 
430 // Entry manipulation
431 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new,
432                                 uint32_t safe, unsigned int holdoff_interval);
433 
434 void odhcp6c_expire(void);
435 uint32_t odhcp6c_elapsed(void);
436 struct odhcp6c_opt *odhcp6c_find_opt(const uint16_t code);
437 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt