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