]> git.decadent.org.uk Git - odhcp6c.git/blob - src/odhcp6c.h
Initial commit (expect bugs!)
[odhcp6c.git] / src / odhcp6c.h
1 /**
2  * Copyright (C) 2012 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 };
52
53 enum dhcpv6_msg {
54         DHCPV6_MSG_UNKNOWN = 0,
55         DHCPV6_MSG_SOLICIT = 1,
56         DHCPV6_MSG_ADVERT = 2,
57         DHCPV6_MSG_REQUEST = 3,
58         DHCPV6_MSG_RENEW = 5,
59         DHCPV6_MSG_REBIND = 6,
60         DHCPV6_MSG_REPLY = 7,
61         DHCPV6_MSG_RELEASE = 8,
62         DHCPV6_MSG_DECLINE = 9,
63         DHCPV6_MSG_RECONF = 10,
64         DHCPV6_MSG_INFO_REQ = 11,
65         _DHCPV6_MSG_MAX
66 };
67
68 enum dhcpv6_status {
69         DHCPV6_NoAddrsAvail = 2,
70         DHCPV6_NoPrefixAvail = 6,
71 };
72
73 typedef int(reply_handler)(enum dhcpv6_msg orig,
74                 const void *opt, const void *end, uint32_t elapsed);
75
76 // retransmission strategy
77 struct dhcpv6_retx {
78         bool delay;
79         uint8_t init_timeo;
80         uint16_t max_timeo;
81         char name[8];
82         reply_handler *handler_reply;
83         int(*handler_finish)(uint32_t elapsed);
84 };
85
86
87 // DHCPv6 Protocol Headers
88 struct dhcpv6_header {
89         uint8_t msg_type;
90         uint8_t tr_id[3];
91 } __attribute__((packed));
92
93 struct dhcpv6_ia_hdr {
94         uint16_t type;
95         uint16_t len;
96         uint32_t iaid;
97         uint32_t t1;
98         uint32_t t2;
99 } _packed;
100
101 struct dhcpv6_ia_addr {
102         uint16_t type;
103         uint16_t len;
104         struct in6_addr addr;
105         uint32_t preferred;
106         uint32_t valid;
107 } _packed;
108
109 struct dhcpv6_ia_prefix {
110         uint16_t type;
111         uint16_t len;
112         uint32_t preferred;
113         uint32_t valid;
114         uint8_t prefix;
115         struct in6_addr addr;
116 } _packed;
117
118 struct dhcpv6_duid {
119         uint16_t type;
120         uint16_t len;
121         uint16_t duid_type;
122         uint8_t data[128];
123 } _packed;
124
125
126 #define dhcpv6_for_each_option(start, end, otype, olen, odata)\
127         for (uint8_t *_o = (uint8_t*)(start); _o + 4 <= (uint8_t*)(end) &&\
128                 ((otype) = _o[0] << 8 | _o[1]) && ((odata) = (void*)&_o[4]) &&\
129                 ((olen) = _o[2] << 8 | _o[3]) + (odata) <= (uint8_t*)(end); \
130                 _o += 4 + (_o[2] << 8 | _o[3]))
131
132
133 struct dhcpv6_server_cand {
134         bool has_noaddravail;
135         bool wants_reconfigure;
136         int16_t preference;
137         uint8_t duid_len;
138         uint8_t duid[130];
139 };
140
141
142 enum odhcp6c_state {
143         STATE_CLIENT_ID,
144         STATE_SERVER_ID,
145         STATE_SERVER_CAND,
146         STATE_ORO,
147         STATE_DNS,
148         STATE_SEARCH,
149         STATE_IA_NA,
150         STATE_IA_PD,
151         STATE_IA_PD_LOST,
152         STATE_CUSTOM_OPTS,
153         _STATE_MAX
154 };
155
156
157 struct icmp6_opt {
158         uint8_t type;
159         uint8_t len;
160         uint8_t data[6];
161 };
162
163
164 enum dhcpv6_mode {
165         DHCPV6_UNKNOWN,
166         DHCPV6_STATELESS,
167         DHCPV6_STATEFUL
168 };
169
170
171 enum odhcp6c_ia_mode {
172         IA_MODE_NONE,
173         IA_MODE_TRY,
174         IA_MODE_FORCE,
175 };
176
177
178 int init_dhcpv6(const char *ifname, int request_pd);
179 void dhcpv6_set_ia_na_mode(enum odhcp6c_ia_mode mode);
180 int dhcpv6_request(enum dhcpv6_msg type);
181 int dhcpv6_poll_reconfigure(void);
182 void dhcpv6_remove_addrs(void);
183
184 int init_rtnetlink(void);
185 int set_rtnetlink_addr(int ifindex, const struct in6_addr *addr,
186                 time_t pref, time_t valid);
187
188 int script_init(const char *path, const char *ifname);
189 ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src);
190 void script_call(const char *status);
191
192
193 // State manipulation
194 bool odhcp6c_signal_is_pending(void);
195 uint64_t adhc6c_get_milli_time(void);
196 void odhcp6c_clear_state(enum odhcp6c_state state);
197 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len);
198 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len);
199 bool odhcp6c_commit_state(enum odhcp6c_state state, size_t old_len);
200 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len);
201