dhcpv6-ia: allow up to 64 bit wide hostid
[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 #include <libubox/vlist.h>
28
29 // RFC 6106 defines this router advertisement option
30 #define ND_OPT_ROUTE_INFO 24
31 #define ND_OPT_RECURSIVE_DNS 25
32 #define ND_OPT_DNS_SEARCH 31
33
34 #define INFINITE_VALID(x) ((x) == 0)
35
36 #define _unused __attribute__((unused))
37 #define _packed __attribute__((packed))
38
39 #define ALL_IPV6_NODES "ff02::1"
40 #define ALL_IPV6_ROUTERS "ff02::2"
41
42 #define IN6_IS_ADDR_ULA(a) (((a)->s6_addr32[0] & htonl(0xfe000000)) == htonl(0xfc000000))
43
44 #define ADDR_MATCH_PIO_FILTER(_addr, iface) (odhcpd_bmemcmp(&(_addr)->addr, \
45 &(iface)->pio_filter_addr, \
46 (iface)->pio_filter_length) != 0 || \
47 (_addr)->prefix < (iface)->pio_filter_length)
48
49 struct interface;
50 struct nl_sock;
51 extern struct vlist_tree leases;
52 extern struct config config;
53
54 struct odhcpd_event {
55 struct uloop_fd uloop;
56 void (*handle_dgram)(void *addr, void *data, size_t len,
57 struct interface *iface, void *dest_addr);
58 void (*handle_error)(struct odhcpd_event *e, int error);
59 void (*recv_msgs)(struct odhcpd_event *e);
60 };
61
62 typedef int (*send_reply_cb_t)(const void *buf, size_t len,
63 const struct sockaddr *dest, socklen_t dest_len,
64 void *opaque);
65
66 typedef void (*dhcpv6_binding_cb_handler_t)(struct in6_addr *addr, int prefix,
67 uint32_t pref, uint32_t valid,
68 void *arg);
69
70 union if_addr {
71 struct in_addr in;
72 struct in6_addr in6;
73 };
74
75 struct netevent_handler_info {
76 struct interface *iface;
77 union {
78 struct {
79 union if_addr dst;
80 uint8_t dst_len;
81 union if_addr gateway;
82 } rt;
83 struct {
84 union if_addr dst;
85 uint16_t state;
86 uint8_t flags;
87 } neigh;
88 struct {
89 struct odhcpd_ipaddr *addrs;
90 size_t len;
91 } addrs_old;
92 union if_addr addr;
93 };
94 };
95
96 enum netevents {
97 NETEV_IFINDEX_CHANGE,
98 NETEV_ADDR_ADD,
99 NETEV_ADDR_DEL,
100 NETEV_ADDRLIST_CHANGE,
101 NETEV_ADDR6_ADD,
102 NETEV_ADDR6_DEL,
103 NETEV_ADDR6LIST_CHANGE,
104 NETEV_ROUTE6_ADD,
105 NETEV_ROUTE6_DEL,
106 NETEV_NEIGH6_ADD,
107 NETEV_NEIGH6_DEL,
108 };
109
110 struct netevent_handler {
111 struct list_head head;
112 void (*cb) (unsigned long event, struct netevent_handler_info *info);
113 };
114
115 struct odhcpd_ipaddr {
116 union if_addr addr;
117 uint8_t prefix;
118 uint32_t preferred;
119 uint32_t valid;
120
121 /* ipv6 only */
122 uint8_t dprefix;
123
124 /* ipv4 only */
125 struct in_addr broadcast;
126 };
127
128 enum odhcpd_mode {
129 MODE_DISABLED,
130 MODE_SERVER,
131 MODE_RELAY,
132 MODE_HYBRID
133 };
134
135
136 enum odhcpd_assignment_flags {
137 OAF_TENTATIVE = (1 << 0),
138 OAF_BOUND = (1 << 1),
139 OAF_STATIC = (1 << 2),
140 OAF_BROKEN_HOSTNAME = (1 << 3),
141 OAF_DHCPV4 = (1 << 4),
142 OAF_DHCPV6_NA = (1 << 5),
143 OAF_DHCPV6_PD = (1 << 6),
144 };
145
146 struct config {
147 bool legacy;
148 bool main_dhcpv4;
149 char *dhcp_cb;
150 char *dhcp_statefile;
151 int log_level;
152 };
153
154
155 struct lease {
156 struct vlist_node node;
157 struct list_head assignments;
158 uint32_t ipaddr;
159 uint64_t hostid;
160 struct ether_addr mac;
161 uint16_t duid_len;
162 uint8_t *duid;
163 uint32_t leasetime;
164 char *hostname;
165 };
166
167 enum {
168 LEASE_ATTR_IP,
169 LEASE_ATTR_MAC,
170 LEASE_ATTR_DUID,
171 LEASE_ATTR_HOSTID,
172 LEASE_ATTR_LEASETIME,
173 LEASE_ATTR_NAME,
174 LEASE_ATTR_MAX
175 };
176
177 struct odhcpd_ref_ip;
178
179 struct dhcp_assignment {
180 struct list_head head;
181 struct list_head lease_list;
182
183 void (*dhcp_free_cb)(struct dhcp_assignment *a);
184
185 struct interface *iface;
186 struct lease *lease;
187
188 struct sockaddr_in6 peer;
189 time_t valid_until;
190 time_t preferred_until;
191
192 #define fr_timer reconf_timer
193 struct uloop_timeout reconf_timer;
194 #define accept_fr_nonce accept_reconf
195 bool accept_reconf;
196 #define fr_cnt reconf_cnt
197 int reconf_cnt;
198 uint8_t key[16];
199 struct odhcpd_ref_ip *fr_ip;
200
201 uint32_t addr;
202 union {
203 uint64_t assigned_host_id;
204 uint32_t assigned_subnet_id;
205 };
206 uint32_t iaid;
207 uint8_t length; // length == 128 -> IA_NA, length <= 64 -> IA_PD
208
209 struct odhcpd_ipaddr *managed;
210 ssize_t managed_size;
211 struct ustream_fd managed_sock;
212
213 unsigned int flags;
214 uint32_t leasetime;
215 char *hostname;
216 char *reqopts;
217 #define hwaddr mac
218 uint8_t mac[6];
219
220 uint16_t clid_len;
221 uint8_t clid_data[];
222 };
223
224
225 struct interface {
226 struct avl_node avl;
227
228 int ifindex;
229 char *ifname;
230 const char *name;
231
232 // IPv6 runtime data
233 struct odhcpd_ipaddr *addr6;
234 size_t addr6_len;
235
236 // RA runtime data
237 struct odhcpd_event router_event;
238 struct uloop_timeout timer_rs;
239 uint32_t ra_sent;
240
241 // DHCPv6 runtime data
242 struct odhcpd_event dhcpv6_event;
243 struct list_head ia_assignments;
244
245 // NDP runtime data
246 struct odhcpd_event ndp_event;
247 int ndp_ping_fd;
248
249 // IPv4 runtime data
250 struct odhcpd_ipaddr *addr4;
251 size_t addr4_len;
252
253 // DHCPv4 runtime data
254 struct odhcpd_event dhcpv4_event;
255 struct list_head dhcpv4_assignments;
256 struct list_head dhcpv4_fr_ips;
257
258 // Managed PD
259 char dhcpv6_pd_manager[128];
260 struct in6_addr dhcpv6_pd_cer;
261
262 // Services
263 enum odhcpd_mode ra;
264 enum odhcpd_mode dhcpv6;
265 enum odhcpd_mode ndp;
266 enum odhcpd_mode dhcpv4;
267
268 // Config
269 bool inuse;
270 bool external;
271 bool master;
272 bool ignore;
273 bool always_rewrite_dns;
274 bool dns_service;
275
276 // NDP
277 int learn_routes;
278
279 // RA
280 uint8_t ra_flags;
281 bool ra_slaac;
282 bool ra_not_onlink;
283 bool ra_advrouter;
284 bool ra_useleasetime;
285 bool ra_dns;
286 bool no_dynamic_dhcp;
287 uint8_t pio_filter_length;
288 struct in6_addr pio_filter_addr;
289 int default_router;
290 int route_preference;
291 int ra_maxinterval;
292 int ra_mininterval;
293 int ra_lifetime;
294 uint32_t ra_reachabletime;
295 uint32_t ra_retranstime;
296 uint32_t ra_hoplimit;
297 int ra_mtu;
298 uint32_t preferred_lifetime;
299
300 // DHCP
301 uint32_t dhcp_leasetime;
302
303 // DHCPv4
304 struct in_addr dhcpv4_start;
305 struct in_addr dhcpv4_end;
306 struct in_addr dhcpv4_start_ip;
307 struct in_addr dhcpv4_end_ip;
308 struct in_addr dhcpv4_local;
309 struct in_addr dhcpv4_bcast;
310 struct in_addr dhcpv4_mask;
311 struct in_addr *dhcpv4_router;
312 size_t dhcpv4_router_cnt;
313 struct in_addr *dhcpv4_dns;
314 size_t dhcpv4_dns_cnt;
315 bool dhcpv4_forcereconf;
316
317 // DNS
318 struct in6_addr *dns;
319 size_t dns_cnt;
320 uint8_t *search;
321 size_t search_len;
322
323 // DHCPV6
324 void *dhcpv6_raw;
325 size_t dhcpv6_raw_len;
326 bool dhcpv6_assignall;
327 bool dhcpv6_pd;
328 bool dhcpv6_na;
329 uint32_t dhcpv6_hostid_len;
330
331 char *upstream;
332 size_t upstream_len;
333
334 char *filter_class;
335 };
336
337 extern struct avl_tree interfaces;
338 extern const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX];
339
340 inline static void free_assignment(struct dhcp_assignment *a)
341 {
342 list_del(&a->head);
343 list_del(&a->lease_list);
344
345 if (a->dhcp_free_cb)
346 a->dhcp_free_cb(a);
347
348 free(a->hostname);
349 free(a->reqopts);
350 free(a);
351 }
352
353 inline static struct dhcp_assignment *alloc_assignment(size_t extra_len)
354 {
355 struct dhcp_assignment *a = calloc(1, sizeof(*a) + extra_len);
356
357 if (!a)
358 return NULL;
359
360 INIT_LIST_HEAD(&a->head);
361 INIT_LIST_HEAD(&a->lease_list);
362
363 return a;
364 }
365
366 // Exported main functions
367 int odhcpd_register(struct odhcpd_event *event);
368 int odhcpd_deregister(struct odhcpd_event *event);
369 void odhcpd_process(struct odhcpd_event *event);
370
371 ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
372 struct iovec *iov, size_t iov_len,
373 const struct interface *iface);
374 int odhcpd_get_interface_dns_addr(const struct interface *iface,
375 struct in6_addr *addr);
376 int odhcpd_get_interface_config(const char *ifname, const char *what);
377 int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6]);
378 struct interface* odhcpd_get_interface_by_index(int ifindex);
379 int odhcpd_urandom(void *data, size_t len);
380
381 void odhcpd_run(void);
382 time_t odhcpd_time(void);
383 ssize_t odhcpd_unhexlify(uint8_t *dst, size_t len, const char *src);
384 void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len);
385 const char *odhcpd_print_mac(const uint8_t *mac, const size_t len);
386
387 int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits);
388 void odhcpd_bmemcpy(void *av, const void *bv, size_t bits);
389
390 int odhcpd_netmask2bitlen(bool v6, void *mask);
391 bool odhcpd_bitlen2netmask(bool v6, unsigned int bits, void *mask);
392 bool odhcpd_valid_hostname(const char *name);
393
394 int config_parse_interface(void *data, size_t len, const char *iname, bool overwrite);
395 struct lease *config_find_lease_by_duid(const uint8_t *duid, const uint16_t len);
396 struct lease *config_find_lease_by_mac(const uint8_t *mac);
397 struct lease *config_find_lease_by_hostid(const uint64_t hostid);
398 struct lease *config_find_lease_by_ipaddr(const uint32_t ipaddr);
399 int set_lease_from_blobmsg(struct blob_attr *ba);
400
401 #ifdef WITH_UBUS
402 int ubus_init(void);
403 const char* ubus_get_ifname(const char *name);
404 void ubus_apply_network(void);
405 bool ubus_has_prefix(const char *name, const char *ifname);
406 void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac, const size_t mac_len,
407 const struct in_addr *addr, const char *name, const char *interface);
408 #endif
409
410 ssize_t dhcpv6_ia_handle_IAs(uint8_t *buf, size_t buflen, struct interface *iface,
411 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end);
412 int dhcpv6_ia_init(void);
413 int dhcpv6_ia_setup_interface(struct interface *iface, bool enable);
414 void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcp_assignment *c, time_t now,
415 dhcpv6_binding_cb_handler_t func, void *arg);
416 void dhcpv6_ia_write_statefile(void);
417
418 int netlink_add_netevent_handler(struct netevent_handler *hdlr);
419 ssize_t netlink_get_interface_addrs(const int ifindex, bool v6,
420 struct odhcpd_ipaddr **addrs);
421 int netlink_get_interface_proxy_neigh(int ifindex, const struct in6_addr *addr);
422 int netlink_setup_route(const struct in6_addr *addr, const int prefixlen,
423 const int ifindex, const struct in6_addr *gw,
424 const uint32_t metric, const bool add);
425 int netlink_setup_proxy_neigh(const struct in6_addr *addr,
426 const int ifindex, const bool add);
427 int netlink_setup_addr(struct odhcpd_ipaddr *addr,
428 const int ifindex, const bool v6, const bool add);
429 void netlink_dump_neigh_table(const bool proxy);
430 void netlink_dump_addr_table(const bool v6);
431
432 // Exported module initializers
433 int netlink_init(void);
434 int router_init(void);
435 int dhcpv6_init(void);
436 int ndp_init(void);
437 #ifdef DHCPV4_SUPPORT
438 int dhcpv4_init(void);
439
440 int dhcpv4_setup_interface(struct interface *iface, bool enable);
441 void dhcpv4_handle_msg(void *addr, void *data, size_t len,
442 struct interface *iface, _unused void *dest_addr,
443 send_reply_cb_t send_reply, void *opaque);
444 #endif
445 int router_setup_interface(struct interface *iface, bool enable);
446 int dhcpv6_setup_interface(struct interface *iface, bool enable);
447 int ndp_setup_interface(struct interface *iface, bool enable);
448
449 void odhcpd_reload(void);