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