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