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