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))
44 #define ADDR_MATCH_PIO_FILTER(_addr, iface) (odhcpd_bmemcmp(&(_addr)->addr, \
45 &(iface)->pio_filter_addr, \
46 (iface)->pio_filter_length) != 0 || \
47 (_addr)->prefix < (iface)->pio_filter_length)
51 extern struct vlist_tree leases
;
52 extern struct config config
;
55 struct uloop_fd uloop
;
56 void (*handle_dgram
)(void *addr
, void *data
, size_t len
,
57 struct interface
*iface
, void *dest_addr
);
58 void (*handle_error
)(struct odhcpd_event
*e
, int error
);
59 void (*recv_msgs
)(struct odhcpd_event
*e
);
62 typedef int (*send_reply_cb_t
)(const void *buf
, size_t len
,
63 const struct sockaddr
*dest
, socklen_t dest_len
,
66 typedef void (*dhcpv6_binding_cb_handler_t
)(struct in6_addr
*addr
, int prefix
,
67 uint32_t pref
, uint32_t valid
,
75 struct netevent_handler_info
{
76 struct interface
*iface
;
81 union if_addr gateway
;
89 struct odhcpd_ipaddr
*addrs
;
100 NETEV_ADDRLIST_CHANGE
,
103 NETEV_ADDR6LIST_CHANGE
,
110 struct netevent_handler
{
111 struct list_head head
;
112 void (*cb
) (unsigned long event
, struct netevent_handler_info
*info
);
115 struct odhcpd_ipaddr
{
125 struct in_addr broadcast
;
136 enum odhcpd_assignment_flags
{
137 OAF_TENTATIVE
= (1 << 0),
138 OAF_BOUND
= (1 << 1),
139 OAF_STATIC
= (1 << 2),
140 OAF_BROKEN_HOSTNAME
= (1 << 3),
141 OAF_DHCPV4
= (1 << 4),
142 OAF_DHCPV6_NA
= (1 << 5),
143 OAF_DHCPV6_PD
= (1 << 6),
150 char *dhcp_statefile
;
156 struct vlist_node node
;
157 struct list_head assignments
;
160 struct ether_addr mac
;
172 LEASE_ATTR_LEASETIME
,
177 struct odhcpd_ref_ip
;
179 struct dhcp_assignment
{
180 struct list_head head
;
181 struct list_head lease_list
;
183 void (*dhcp_free_cb
)(struct dhcp_assignment
*a
);
185 struct interface
*iface
;
188 struct sockaddr_in6 peer
;
190 time_t preferred_until
;
192 #define fr_timer reconf_timer
193 struct uloop_timeout reconf_timer
;
194 #define accept_fr_nonce accept_reconf
196 #define fr_cnt reconf_cnt
199 struct odhcpd_ref_ip
*fr_ip
;
204 uint8_t length
; // length == 128 -> IA_NA, length <= 64 -> IA_PD
206 struct odhcpd_ipaddr
*managed
;
207 ssize_t managed_size
;
208 struct ustream_fd managed_sock
;
230 struct odhcpd_ipaddr
*addr6
;
234 struct odhcpd_event router_event
;
235 struct uloop_timeout timer_rs
;
238 // DHCPv6 runtime data
239 struct odhcpd_event dhcpv6_event
;
240 struct list_head ia_assignments
;
243 struct odhcpd_event ndp_event
;
247 struct odhcpd_ipaddr
*addr4
;
250 // DHCPv4 runtime data
251 struct odhcpd_event dhcpv4_event
;
252 struct list_head dhcpv4_assignments
;
253 struct list_head dhcpv4_fr_ips
;
256 char dhcpv6_pd_manager
[128];
257 struct in6_addr dhcpv6_pd_cer
;
261 enum odhcpd_mode dhcpv6
;
262 enum odhcpd_mode ndp
;
263 enum odhcpd_mode dhcpv4
;
270 bool always_rewrite_dns
;
281 bool ra_useleasetime
;
283 bool no_dynamic_dhcp
;
284 uint8_t pio_filter_length
;
285 struct in6_addr pio_filter_addr
;
287 int route_preference
;
291 uint32_t ra_reachabletime
;
292 uint32_t ra_retranstime
;
293 uint32_t ra_hoplimit
;
295 uint32_t preferred_lifetime
;
298 uint32_t dhcp_leasetime
;
301 struct in_addr dhcpv4_start
;
302 struct in_addr dhcpv4_end
;
303 struct in_addr dhcpv4_start_ip
;
304 struct in_addr dhcpv4_end_ip
;
305 struct in_addr dhcpv4_local
;
306 struct in_addr dhcpv4_bcast
;
307 struct in_addr dhcpv4_mask
;
308 struct in_addr
*dhcpv4_router
;
309 size_t dhcpv4_router_cnt
;
310 struct in_addr
*dhcpv4_dns
;
311 size_t dhcpv4_dns_cnt
;
312 bool dhcpv4_forcereconf
;
315 struct in6_addr
*dns
;
322 size_t dhcpv6_raw_len
;
323 bool dhcpv6_assignall
;
333 extern struct avl_tree interfaces
;
334 extern const struct blobmsg_policy lease_attrs
[LEASE_ATTR_MAX
];
336 inline static void free_assignment(struct dhcp_assignment
*a
)
339 list_del(&a
->lease_list
);
349 inline static struct dhcp_assignment
*alloc_assignment(size_t extra_len
)
351 struct dhcp_assignment
*a
= calloc(1, sizeof(*a
) + extra_len
);
356 INIT_LIST_HEAD(&a
->head
);
357 INIT_LIST_HEAD(&a
->lease_list
);
362 // Exported main functions
363 int odhcpd_register(struct odhcpd_event
*event
);
364 int odhcpd_deregister(struct odhcpd_event
*event
);
365 void odhcpd_process(struct odhcpd_event
*event
);
367 ssize_t
odhcpd_send(int socket
, struct sockaddr_in6
*dest
,
368 struct iovec
*iov
, size_t iov_len
,
369 const struct interface
*iface
);
370 int odhcpd_get_interface_dns_addr(const struct interface
*iface
,
371 struct in6_addr
*addr
);
372 int odhcpd_get_interface_config(const char *ifname
, const char *what
);
373 int odhcpd_get_mac(const struct interface
*iface
, uint8_t mac
[6]);
374 struct interface
* odhcpd_get_interface_by_index(int ifindex
);
375 int odhcpd_urandom(void *data
, size_t len
);
377 void odhcpd_run(void);
378 time_t odhcpd_time(void);
379 ssize_t
odhcpd_unhexlify(uint8_t *dst
, size_t len
, const char *src
);
380 void odhcpd_hexlify(char *dst
, const uint8_t *src
, size_t len
);
381 const char *odhcpd_print_mac(const uint8_t *mac
, const size_t len
);
383 int odhcpd_bmemcmp(const void *av
, const void *bv
, size_t bits
);
384 void odhcpd_bmemcpy(void *av
, const void *bv
, size_t bits
);
386 int odhcpd_netmask2bitlen(bool v6
, void *mask
);
387 bool odhcpd_bitlen2netmask(bool v6
, unsigned int bits
, void *mask
);
388 bool odhcpd_valid_hostname(const char *name
);
390 int config_parse_interface(void *data
, size_t len
, const char *iname
, bool overwrite
);
391 struct lease
*config_find_lease_by_duid(const uint8_t *duid
, const uint16_t len
);
392 struct lease
*config_find_lease_by_mac(const uint8_t *mac
);
393 struct lease
*config_find_lease_by_hostid(const uint32_t hostid
);
394 struct lease
*config_find_lease_by_ipaddr(const uint32_t ipaddr
);
395 int set_lease_from_blobmsg(struct blob_attr
*ba
);
399 const char* ubus_get_ifname(const char *name
);
400 void ubus_apply_network(void);
401 bool ubus_has_prefix(const char *name
, const char *ifname
);
402 void ubus_bcast_dhcp_event(const char *type
, const uint8_t *mac
, const size_t mac_len
,
403 const struct in_addr
*addr
, const char *name
, const char *interface
);
406 ssize_t
dhcpv6_ia_handle_IAs(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
407 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
);
408 int dhcpv6_ia_init(void);
409 int dhcpv6_ia_setup_interface(struct interface
*iface
, bool enable
);
410 void dhcpv6_ia_enum_addrs(struct interface
*iface
, struct dhcp_assignment
*c
, time_t now
,
411 dhcpv6_binding_cb_handler_t func
, void *arg
);
412 void dhcpv6_ia_write_statefile(void);
414 int netlink_add_netevent_handler(struct netevent_handler
*hdlr
);
415 ssize_t
netlink_get_interface_addrs(const int ifindex
, bool v6
,
416 struct odhcpd_ipaddr
**addrs
);
417 int netlink_get_interface_proxy_neigh(int ifindex
, const struct in6_addr
*addr
);
418 int netlink_setup_route(const struct in6_addr
*addr
, const int prefixlen
,
419 const int ifindex
, const struct in6_addr
*gw
,
420 const uint32_t metric
, const bool add
);
421 int netlink_setup_proxy_neigh(const struct in6_addr
*addr
,
422 const int ifindex
, const bool add
);
423 int netlink_setup_addr(struct odhcpd_ipaddr
*addr
,
424 const int ifindex
, const bool v6
, const bool add
);
425 void netlink_dump_neigh_table(const bool proxy
);
426 void netlink_dump_addr_table(const bool v6
);
428 // Exported module initializers
429 int netlink_init(void);
430 int router_init(void);
431 int dhcpv6_init(void);
433 #ifdef DHCPV4_SUPPORT
434 int dhcpv4_init(void);
436 int dhcpv4_setup_interface(struct interface
*iface
, bool enable
);
437 void dhcpv4_handle_msg(void *addr
, void *data
, size_t len
,
438 struct interface
*iface
, _unused
void *dest_addr
,
439 send_reply_cb_t send_reply
, void *opaque
);
441 int router_setup_interface(struct interface
*iface
, bool enable
);
442 int dhcpv6_setup_interface(struct interface
*iface
, bool enable
);
443 int ndp_setup_interface(struct interface
*iface
, bool enable
);
445 void odhcpd_reload(void);