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>
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
33 #define INFINITE_VALID(x) ((x) == 0)
35 #define _unused __attribute__((unused))
36 #define _packed __attribute__((packed))
38 #define ALL_IPV6_NODES "ff02::1"
39 #define ALL_IPV6_ROUTERS "ff02::2"
41 #define IN6_IS_ADDR_ULA(a) (((a)->s6_addr32[0] & htonl(0xfe000000)) == htonl(0xfc000000))
45 extern struct list_head leases
;
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
);
60 struct netevent_handler_info
{
61 struct interface
*iface
;
66 union if_addr gateway
;
74 struct odhcpd_ipaddr
*addrs
;
85 NETEV_ADDRLIST_CHANGE
,
88 NETEV_ADDR6LIST_CHANGE
,
95 struct netevent_handler
{
96 struct list_head head
;
97 void (*cb
) (unsigned long event
, struct netevent_handler_info
*info
);
100 struct odhcpd_ipaddr
{
110 struct in_addr broadcast
;
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),
132 char *dhcp_statefile
;
138 struct list_head head
;
139 struct in_addr ipaddr
;
141 struct ether_addr mac
;
144 uint32_t dhcpv4_leasetime
;
149 struct odhcpd_ref_ip
;
151 struct dhcp_assignment
{
152 struct list_head head
;
153 struct interface
*iface
;
155 struct sockaddr_in6 peer
;
158 #define fr_timer reconf_timer
159 struct uloop_timeout reconf_timer
;
160 #define accept_fr_nonce accept_reconf
162 #define fr_cnt reconf_cnt
165 struct odhcpd_ref_ip
*fr_ip
;
170 uint8_t length
; // length == 128 -> IA_NA, length <= 64 -> IA_PD
172 struct odhcpd_ipaddr
*managed
;
173 ssize_t managed_size
;
174 struct ustream_fd managed_sock
;
195 struct odhcpd_ipaddr
*addr6
;
199 struct uloop_timeout timer_rs
;
201 // DHCPv6 runtime data
202 struct odhcpd_event dhcpv6_event
;
203 struct list_head ia_assignments
;
206 struct odhcpd_event ndp_event
;
209 struct odhcpd_ipaddr
*addr4
;
212 // DHCPv4 runtime data
213 struct odhcpd_event dhcpv4_event
;
214 struct list_head dhcpv4_assignments
;
215 struct list_head dhcpv4_fr_ips
;
218 char dhcpv6_pd_manager
[128];
219 struct in6_addr dhcpv6_pd_cer
;
223 enum odhcpd_mode dhcpv6
;
224 enum odhcpd_mode ndp
;
225 enum odhcpd_mode dhcpv4
;
232 bool always_rewrite_dns
;
235 bool ra_useleasetime
;
237 bool no_dynamic_dhcp
;
238 uint8_t pio_filter_length
;
239 struct in6_addr pio_filter_addr
;
245 int route_preference
;
249 uint32_t ra_reachabletime
;
250 uint32_t ra_retranstime
;
251 uint32_t ra_hoplimit
;
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
;
270 struct in6_addr
*dns
;
277 size_t dhcpv6_raw_len
;
278 bool dhcpv6_assignall
;
288 extern struct avl_tree interfaces
;
290 #define RA_MANAGED_NO_MFLAG 0
291 #define RA_MANAGED_MFLAG 1
292 #define RA_MANAGED_NO_AFLAG 2
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
);
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
);
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
);
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
);
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
);
324 int config_parse_interface(void *data
, size_t len
, const char *iname
, bool overwrite
);
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
);
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
);
348 // Exported module initializers
349 int netlink_init(void);
350 int router_init(void);
351 int dhcpv6_init(void);
353 #ifdef DHCPV4_SUPPORT
354 int dhcpv4_init(void);
356 int dhcpv4_setup_interface(struct interface
*iface
, bool enable
);
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
);
362 void odhcpd_reload(void);