dhcpv6-ia: move function definitions to odhcpd.h
[project/odhcpd.git] / src / odhcpd.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
15 #pragma once
16 #include <netinet/in.h>
17 #include <netinet/icmp6.h>
18 #include <netinet/ether.h>
19 #include <stdbool.h>
20 #include <syslog.h>
21
22 #include <libubox/blobmsg.h>
23 #include <libubox/list.h>
24 #include <libubox/uloop.h>
25 #include <libubox/avl.h>
26 #include <libubox/ustream.h>
27
28 // RFC 6106 defines this router advertisement option
29 #define ND_OPT_ROUTE_INFO 24
30 #define ND_OPT_RECURSIVE_DNS 25
31 #define ND_OPT_DNS_SEARCH 31
32
33 #define INFINITE_VALID(x) ((x) == 0)
34
35 #define _unused __attribute__((unused))
36 #define _packed __attribute__((packed))
37
38 #define ALL_IPV6_NODES "ff02::1"
39 #define ALL_IPV6_ROUTERS "ff02::2"
40
41 #define IN6_IS_ADDR_ULA(a) (((a)->s6_addr32[0] & htonl(0xfe000000)) == htonl(0xfc000000))
42
43 struct interface;
44 struct nl_sock;
45 extern struct list_head leases;
46
47 struct odhcpd_event {
48 struct uloop_fd uloop;
49 void (*handle_dgram)(void *addr, void *data, size_t len,
50 struct interface *iface, void *dest_addr);
51 void (*handle_error)(struct odhcpd_event *e, int error);
52 void (*recv_msgs)(struct odhcpd_event *e);
53 };
54
55 typedef void (*dhcpv6_binding_cb_handler_t)(struct in6_addr *addr, int prefix,
56 uint32_t pref, uint32_t valid,
57 void *arg);
58
59 union if_addr {
60 struct in_addr in;
61 struct in6_addr in6;
62 };
63
64 struct netevent_handler_info {
65 struct interface *iface;
66 union {
67 struct {
68 union if_addr dst;
69 uint8_t dst_len;
70 union if_addr gateway;
71 } rt;
72 struct {
73 union if_addr dst;
74 uint16_t state;
75 uint8_t flags;
76 } neigh;
77 struct {
78 struct odhcpd_ipaddr *addrs;
79 size_t len;
80 } addrs_old;
81 union if_addr addr;
82 };
83 };
84
85 enum netevents {
86 NETEV_IFINDEX_CHANGE,
87 NETEV_ADDR_ADD,
88 NETEV_ADDR_DEL,
89 NETEV_ADDRLIST_CHANGE,
90 NETEV_ADDR6_ADD,
91 NETEV_ADDR6_DEL,
92 NETEV_ADDR6LIST_CHANGE,
93 NETEV_ROUTE6_ADD,
94 NETEV_ROUTE6_DEL,
95 NETEV_NEIGH6_ADD,
96 NETEV_NEIGH6_DEL,
97 };
98
99 struct netevent_handler {
100 struct list_head head;
101 void (*cb) (unsigned long event, struct netevent_handler_info *info);
102 };
103
104 struct odhcpd_ipaddr {
105 union if_addr addr;
106 uint8_t prefix;
107 uint32_t preferred;
108 uint32_t valid;
109
110 /* ipv6 only */
111 uint8_t dprefix;
112
113 /* ipv4 only */
114 struct in_addr broadcast;
115 };
116
117 enum odhcpd_mode {
118 MODE_DISABLED,
119 MODE_SERVER,
120 MODE_RELAY,
121 MODE_HYBRID
122 };
123
124
125 enum odhcpd_assignment_flags {
126 OAF_TENTATIVE = (1 << 0),
127 OAF_BOUND = (1 << 1),
128 OAF_STATIC = (1 << 2),
129 OAF_BROKEN_HOSTNAME = (1 << 3),
130 };
131
132 struct config {
133 bool legacy;
134 bool main_dhcpv4;
135 char *dhcp_cb;
136 char *dhcp_statefile;
137 int log_level;
138 } config;
139
140
141 struct lease {
142 struct list_head head;
143 struct in_addr ipaddr;
144 uint32_t hostid;
145 struct ether_addr mac;
146 uint16_t duid_len;
147 uint8_t *duid;
148 uint32_t dhcpv4_leasetime;
149 char hostname[];
150 };
151
152
153 struct odhcpd_ref_ip;
154
155 struct dhcp_assignment {
156 struct list_head head;
157 struct interface *iface;
158
159 struct sockaddr_in6 peer;
160 time_t valid_until;
161
162 #define fr_timer reconf_timer
163 struct uloop_timeout reconf_timer;
164 #define accept_fr_nonce accept_reconf
165 bool accept_reconf;
166 #define fr_cnt reconf_cnt
167 int reconf_cnt;
168 uint8_t key[16];
169 struct odhcpd_ref_ip *fr_ip;
170
171 uint32_t addr;
172 uint32_t assigned;
173 uint32_t iaid;
174 uint8_t length; // length == 128 -> IA_NA, length <= 64 -> IA_PD
175
176 struct odhcpd_ipaddr *managed;
177 ssize_t managed_size;
178 struct ustream_fd managed_sock;
179
180 unsigned int flags;
181 uint32_t leasetime;
182 char *hostname;
183 #define hwaddr mac
184 uint8_t mac[6];
185
186 uint8_t clid_len;
187 uint8_t clid_data[];
188 };
189
190
191 struct interface {
192 struct avl_node avl;
193
194 int ifindex;
195 char *ifname;
196 const char *name;
197
198 // IPv6 runtime data
199 struct odhcpd_ipaddr *addr6;
200 size_t addr6_len;
201
202 // RA runtime data
203 struct uloop_timeout timer_rs;
204
205 // DHCPv6 runtime data
206 struct odhcpd_event dhcpv6_event;
207 struct list_head ia_assignments;
208
209 // NDP runtime data
210 struct odhcpd_event ndp_event;
211
212 // IPv4 runtime data
213 struct odhcpd_ipaddr *addr4;
214 size_t addr4_len;
215
216 // DHCPv4 runtime data
217 struct odhcpd_event dhcpv4_event;
218 struct list_head dhcpv4_assignments;
219 struct list_head dhcpv4_fr_ips;
220
221 // Managed PD
222 char dhcpv6_pd_manager[128];
223 struct in6_addr dhcpv6_pd_cer;
224
225 // Services
226 enum odhcpd_mode ra;
227 enum odhcpd_mode dhcpv6;
228 enum odhcpd_mode ndp;
229 enum odhcpd_mode dhcpv4;
230
231 // Config
232 bool inuse;
233 bool external;
234 bool master;
235 bool ignore;
236 bool always_rewrite_dns;
237 bool ra_not_onlink;
238 bool ra_advrouter;
239 bool ra_useleasetime;
240 bool ra_dns;
241 bool no_dynamic_dhcp;
242 uint8_t pio_filter_length;
243 struct in6_addr pio_filter_addr;
244
245 // RA
246 int learn_routes;
247 int default_router;
248 int ra_managed;
249 int route_preference;
250 int ra_maxinterval;
251 int ra_mininterval;
252 int ra_lifetime;
253 uint32_t ra_reachabletime;
254 uint32_t ra_retranstime;
255 uint32_t ra_hoplimit;
256 int ra_mtu;
257
258 // DHCPv4
259 struct in_addr dhcpv4_start;
260 struct in_addr dhcpv4_end;
261 struct in_addr dhcpv4_start_ip;
262 struct in_addr dhcpv4_end_ip;
263 struct in_addr dhcpv4_local;
264 struct in_addr dhcpv4_bcast;
265 struct in_addr dhcpv4_mask;
266 struct in_addr *dhcpv4_router;
267 size_t dhcpv4_router_cnt;
268 struct in_addr *dhcpv4_dns;
269 size_t dhcpv4_dns_cnt;
270 uint32_t dhcpv4_leasetime;
271 bool dhcpv4_forcereconf;
272
273 // DNS
274 struct in6_addr *dns;
275 size_t dns_cnt;
276 uint8_t *search;
277 size_t search_len;
278
279 // DHCPV6
280 void *dhcpv6_raw;
281 size_t dhcpv6_raw_len;
282 bool dhcpv6_assignall;
283 bool dhcpv6_pd;
284 bool dhcpv6_na;
285
286 char *upstream;
287 size_t upstream_len;
288
289 char *filter_class;
290 };
291
292 extern struct avl_tree interfaces;
293
294 #define RA_MANAGED_NO_MFLAG 0
295 #define RA_MANAGED_MFLAG 1
296 #define RA_MANAGED_NO_AFLAG 2
297
298
299 // Exported main functions
300 int odhcpd_register(struct odhcpd_event *event);
301 int odhcpd_deregister(struct odhcpd_event *event);
302 void odhcpd_process(struct odhcpd_event *event);
303
304 ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
305 struct iovec *iov, size_t iov_len,
306 const struct interface *iface);
307 int odhcpd_get_interface_dns_addr(const struct interface *iface,
308 struct in6_addr *addr);
309 int odhcpd_get_interface_config(const char *ifname, const char *what);
310 int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6]);
311 struct interface* odhcpd_get_interface_by_index(int ifindex);
312 struct interface* odhcpd_get_master_interface(void);
313 int odhcpd_urandom(void *data, size_t len);
314
315 void odhcpd_run(void);
316 time_t odhcpd_time(void);
317 ssize_t odhcpd_unhexlify(uint8_t *dst, size_t len, const char *src);
318 void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len);
319 const char *odhcpd_print_mac(const uint8_t *mac, const size_t len);
320
321 int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits);
322 void odhcpd_bmemcpy(void *av, const void *bv, size_t bits);
323
324 int odhcpd_netmask2bitlen(bool v6, void *mask);
325 bool odhcpd_bitlen2netmask(bool v6, unsigned int bits, void *mask);
326 bool odhcpd_valid_hostname(const char *name);
327
328 int config_parse_interface(void *data, size_t len, const char *iname, bool overwrite);
329
330 #ifdef WITH_UBUS
331 int ubus_init(void);
332 const char* ubus_get_ifname(const char *name);
333 void ubus_apply_network(void);
334 bool ubus_has_prefix(const char *name, const char *ifname);
335 void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac, const size_t mac_len,
336 const struct in_addr *addr, const char *name, const char *interface);
337 #endif
338
339 ssize_t dhcpv6_ia_handle_IAs(uint8_t *buf, size_t buflen, struct interface *iface,
340 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end);
341 int dhcpv6_ia_init(void);
342 int dhcpv6_ia_setup_interface(struct interface *iface, bool enable);
343 void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcp_assignment *c, time_t now,
344 dhcpv6_binding_cb_handler_t func, void *arg);
345 void dhcpv6_ia_write_statefile(void);
346
347 int netlink_add_netevent_handler(struct netevent_handler *hdlr);
348 ssize_t netlink_get_interface_addrs(const int ifindex, bool v6,
349 struct odhcpd_ipaddr **addrs);
350 int netlink_setup_route(const struct in6_addr *addr, const int prefixlen,
351 const int ifindex, const struct in6_addr *gw,
352 const uint32_t metric, const bool add);
353 int netlink_setup_proxy_neigh(const struct in6_addr *addr,
354 const int ifindex, const bool add);
355 int netlink_setup_addr(struct odhcpd_ipaddr *addr,
356 const int ifindex, const bool v6, const bool add);
357 void netlink_dump_neigh_table(const bool proxy);
358 void netlink_dump_addr_table(const bool v6);
359
360 // Exported module initializers
361 int netlink_init(void);
362 int router_init(void);
363 int dhcpv6_init(void);
364 int ndp_init(void);
365 #ifdef DHCPV4_SUPPORT
366 int dhcpv4_init(void);
367
368 int dhcpv4_setup_interface(struct interface *iface, bool enable);
369 #endif
370 int router_setup_interface(struct interface *iface, bool enable);
371 int dhcpv6_setup_interface(struct interface *iface, bool enable);
372 int ndp_setup_interface(struct interface *iface, bool enable);
373
374 void odhcpd_reload(void);