2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
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.
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.
16 #include <netinet/in.h>
17 #include <netinet/icmp6.h>
18 #include <netinet/ether.h>
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>
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
34 #define INFINITE_VALID(x) ((x) == 0)
36 #define _unused __attribute__((unused))
37 #define _packed __attribute__((packed))
39 #define ALL_IPV6_NODES "ff02::1"
40 #define ALL_IPV6_ROUTERS "ff02::2"
42 #define IN6_IS_ADDR_ULA(a) (((a)->s6_addr32[0] & htonl(0xfe000000)) == htonl(0xfc000000))
46 extern struct vlist_tree leases
;
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
);
56 typedef void (*dhcpv6_binding_cb_handler_t
)(struct in6_addr
*addr
, int prefix
,
57 uint32_t pref
, uint32_t valid
,
65 struct netevent_handler_info
{
66 struct interface
*iface
;
71 union if_addr gateway
;
79 struct odhcpd_ipaddr
*addrs
;
90 NETEV_ADDRLIST_CHANGE
,
93 NETEV_ADDR6LIST_CHANGE
,
100 struct netevent_handler
{
101 struct list_head head
;
102 void (*cb
) (unsigned long event
, struct netevent_handler_info
*info
);
105 struct odhcpd_ipaddr
{
115 struct in_addr broadcast
;
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),
140 char *dhcp_statefile
;
146 struct vlist_node node
;
147 struct list_head assignments
;
150 struct ether_addr mac
;
158 struct odhcpd_ref_ip
;
160 struct dhcp_assignment
{
161 struct list_head head
;
162 struct list_head lease_list
;
164 void (*dhcp_free_cb
)(struct dhcp_assignment
*a
);
166 struct interface
*iface
;
169 struct sockaddr_in6 peer
;
172 #define fr_timer reconf_timer
173 struct uloop_timeout reconf_timer
;
174 #define accept_fr_nonce accept_reconf
176 #define fr_cnt reconf_cnt
179 struct odhcpd_ref_ip
*fr_ip
;
184 uint8_t length
; // length == 128 -> IA_NA, length <= 64 -> IA_PD
186 struct odhcpd_ipaddr
*managed
;
187 ssize_t managed_size
;
188 struct ustream_fd managed_sock
;
210 struct odhcpd_ipaddr
*addr6
;
214 struct odhcpd_event router_event
;
215 struct uloop_timeout timer_rs
;
218 // DHCPv6 runtime data
219 struct odhcpd_event dhcpv6_event
;
220 struct list_head ia_assignments
;
223 struct odhcpd_event ndp_event
;
227 struct odhcpd_ipaddr
*addr4
;
230 // DHCPv4 runtime data
231 struct odhcpd_event dhcpv4_event
;
232 struct list_head dhcpv4_assignments
;
233 struct list_head dhcpv4_fr_ips
;
236 char dhcpv6_pd_manager
[128];
237 struct in6_addr dhcpv6_pd_cer
;
241 enum odhcpd_mode dhcpv6
;
242 enum odhcpd_mode ndp
;
243 enum odhcpd_mode dhcpv4
;
250 bool always_rewrite_dns
;
260 bool ra_useleasetime
;
262 bool no_dynamic_dhcp
;
263 uint8_t pio_filter_length
;
264 struct in6_addr pio_filter_addr
;
266 int route_preference
;
270 uint32_t ra_reachabletime
;
271 uint32_t ra_retranstime
;
272 uint32_t ra_hoplimit
;
276 struct in_addr dhcpv4_start
;
277 struct in_addr dhcpv4_end
;
278 struct in_addr dhcpv4_start_ip
;
279 struct in_addr dhcpv4_end_ip
;
280 struct in_addr dhcpv4_local
;
281 struct in_addr dhcpv4_bcast
;
282 struct in_addr dhcpv4_mask
;
283 struct in_addr
*dhcpv4_router
;
284 size_t dhcpv4_router_cnt
;
285 struct in_addr
*dhcpv4_dns
;
286 size_t dhcpv4_dns_cnt
;
287 uint32_t dhcpv4_leasetime
;
288 bool dhcpv4_forcereconf
;
291 struct in6_addr
*dns
;
298 size_t dhcpv6_raw_len
;
299 bool dhcpv6_assignall
;
309 extern struct avl_tree interfaces
;
311 inline static void free_assignment(struct dhcp_assignment
*a
)
314 list_del(&a
->lease_list
);
324 inline static struct dhcp_assignment
*alloc_assignment(size_t extra_len
)
326 struct dhcp_assignment
*a
= calloc(1, sizeof(*a
) + extra_len
);
331 INIT_LIST_HEAD(&a
->head
);
332 INIT_LIST_HEAD(&a
->lease_list
);
337 // Exported main functions
338 int odhcpd_register(struct odhcpd_event
*event
);
339 int odhcpd_deregister(struct odhcpd_event
*event
);
340 void odhcpd_process(struct odhcpd_event
*event
);
342 ssize_t
odhcpd_send(int socket
, struct sockaddr_in6
*dest
,
343 struct iovec
*iov
, size_t iov_len
,
344 const struct interface
*iface
);
345 int odhcpd_get_interface_dns_addr(const struct interface
*iface
,
346 struct in6_addr
*addr
);
347 int odhcpd_get_interface_config(const char *ifname
, const char *what
);
348 int odhcpd_get_mac(const struct interface
*iface
, uint8_t mac
[6]);
349 struct interface
* odhcpd_get_interface_by_index(int ifindex
);
350 int odhcpd_urandom(void *data
, size_t len
);
352 void odhcpd_run(void);
353 time_t odhcpd_time(void);
354 ssize_t
odhcpd_unhexlify(uint8_t *dst
, size_t len
, const char *src
);
355 void odhcpd_hexlify(char *dst
, const uint8_t *src
, size_t len
);
356 const char *odhcpd_print_mac(const uint8_t *mac
, const size_t len
);
358 int odhcpd_bmemcmp(const void *av
, const void *bv
, size_t bits
);
359 void odhcpd_bmemcpy(void *av
, const void *bv
, size_t bits
);
361 int odhcpd_netmask2bitlen(bool v6
, void *mask
);
362 bool odhcpd_bitlen2netmask(bool v6
, unsigned int bits
, void *mask
);
363 bool odhcpd_valid_hostname(const char *name
);
365 int config_parse_interface(void *data
, size_t len
, const char *iname
, bool overwrite
);
366 struct lease
*config_find_lease_by_duid(const uint8_t *duid
, const uint16_t len
);
367 struct lease
*config_find_lease_by_mac(const uint8_t *mac
);
368 struct lease
*config_find_lease_by_hostid(const uint32_t hostid
);
369 struct lease
*config_find_lease_by_ipaddr(const uint32_t ipaddr
);
373 const char* ubus_get_ifname(const char *name
);
374 void ubus_apply_network(void);
375 bool ubus_has_prefix(const char *name
, const char *ifname
);
376 void ubus_bcast_dhcp_event(const char *type
, const uint8_t *mac
, const size_t mac_len
,
377 const struct in_addr
*addr
, const char *name
, const char *interface
);
380 ssize_t
dhcpv6_ia_handle_IAs(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
381 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
);
382 int dhcpv6_ia_init(void);
383 int dhcpv6_ia_setup_interface(struct interface
*iface
, bool enable
);
384 void dhcpv6_ia_enum_addrs(struct interface
*iface
, struct dhcp_assignment
*c
, time_t now
,
385 dhcpv6_binding_cb_handler_t func
, void *arg
);
386 void dhcpv6_ia_write_statefile(void);
388 int netlink_add_netevent_handler(struct netevent_handler
*hdlr
);
389 ssize_t
netlink_get_interface_addrs(const int ifindex
, bool v6
,
390 struct odhcpd_ipaddr
**addrs
);
391 int netlink_get_interface_proxy_neigh(int ifindex
, const struct in6_addr
*addr
);
392 int netlink_setup_route(const struct in6_addr
*addr
, const int prefixlen
,
393 const int ifindex
, const struct in6_addr
*gw
,
394 const uint32_t metric
, const bool add
);
395 int netlink_setup_proxy_neigh(const struct in6_addr
*addr
,
396 const int ifindex
, const bool add
);
397 int netlink_setup_addr(struct odhcpd_ipaddr
*addr
,
398 const int ifindex
, const bool v6
, const bool add
);
399 void netlink_dump_neigh_table(const bool proxy
);
400 void netlink_dump_addr_table(const bool v6
);
402 // Exported module initializers
403 int netlink_init(void);
404 int router_init(void);
405 int dhcpv6_init(void);
407 #ifdef DHCPV4_SUPPORT
408 int dhcpv4_init(void);
410 int dhcpv4_setup_interface(struct interface
*iface
, bool enable
);
412 int router_setup_interface(struct interface
*iface
, bool enable
);
413 int dhcpv6_setup_interface(struct interface
*iface
, bool enable
);
414 int ndp_setup_interface(struct interface
*iface
, bool enable
);
416 void odhcpd_reload(void);