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