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 #define min(a, b) (((a) < (b)) ? (a) : (b))
30 #define max(a, b) (((a) > (b)) ? (a) : (b))
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
37 // RFC 8781 defines PREF64 option
38 #define ND_OPT_PREF64 38
40 #define INFINITE_VALID(x) ((x) == 0)
42 #define _unused __attribute__((unused))
43 #define _packed __attribute__((packed))
45 #define ALL_IPV6_NODES "ff02::1"
46 #define ALL_IPV6_ROUTERS "ff02::2"
48 #define NTP_SUBOPTION_SRV_ADDR 1
49 #define NTP_SUBOPTION_MC_ADDR 2
50 #define NTP_SUBOPTION_SRV_FQDN 3
51 #define IPV6_ADDR_LEN 16
53 #define IN6_IS_ADDR_ULA(a) (((a)->s6_addr32[0] & htonl(0xfe000000)) == htonl(0xfc000000))
55 #define ADDR_MATCH_PIO_FILTER(_addr, iface) (odhcpd_bmemcmp(&(_addr)->addr, \
56 &(iface)->pio_filter_addr, \
57 (iface)->pio_filter_length) != 0 || \
58 (_addr)->prefix < (iface)->pio_filter_length)
62 extern struct vlist_tree leases
;
63 extern struct config config
;
66 struct uloop_fd uloop
;
67 void (*handle_dgram
)(void *addr
, void *data
, size_t len
,
68 struct interface
*iface
, void *dest_addr
);
69 void (*handle_error
)(struct odhcpd_event
*e
, int error
);
70 void (*recv_msgs
)(struct odhcpd_event
*e
);
73 typedef int (*send_reply_cb_t
)(const void *buf
, size_t len
,
74 const struct sockaddr
*dest
, socklen_t dest_len
,
77 typedef void (*dhcpv6_binding_cb_handler_t
)(struct in6_addr
*addr
, int prefix
,
78 uint32_t pref
, uint32_t valid
,
86 struct netevent_handler_info
{
87 struct interface
*iface
;
92 union if_addr gateway
;
100 struct odhcpd_ipaddr
*addrs
;
108 NETEV_IFINDEX_CHANGE
,
111 NETEV_ADDRLIST_CHANGE
,
114 NETEV_ADDR6LIST_CHANGE
,
121 struct netevent_handler
{
122 struct list_head head
;
123 void (*cb
) (unsigned long event
, struct netevent_handler_info
*info
);
126 struct odhcpd_ipaddr
{
136 uint8_t invalid_advertisements
;
141 struct in_addr broadcast
;
153 enum odhcpd_assignment_flags
{
154 OAF_TENTATIVE
= (1 << 0),
155 OAF_BOUND
= (1 << 1),
156 OAF_STATIC
= (1 << 2),
157 OAF_BROKEN_HOSTNAME
= (1 << 3),
158 OAF_DHCPV4
= (1 << 4),
159 OAF_DHCPV6_NA
= (1 << 5),
160 OAF_DHCPV6_PD
= (1 << 6),
167 char *dhcp_statefile
;
173 struct vlist_node node
;
174 struct list_head assignments
;
177 struct ether_addr mac
;
189 LEASE_ATTR_LEASETIME
,
194 struct odhcpd_ref_ip
;
196 struct dhcp_assignment
{
197 struct list_head head
;
198 struct list_head lease_list
;
200 void (*dhcp_free_cb
)(struct dhcp_assignment
*a
);
202 struct interface
*iface
;
205 struct sockaddr_in6 peer
;
207 time_t preferred_until
;
209 #define fr_timer reconf_timer
210 struct uloop_timeout reconf_timer
;
211 #define accept_fr_nonce accept_reconf
213 #define fr_cnt reconf_cnt
216 struct odhcpd_ref_ip
*fr_ip
;
220 uint64_t assigned_host_id
;
221 uint32_t assigned_subnet_id
;
224 uint8_t length
; // length == 128 -> IA_NA, length <= 64 -> IA_PD
226 struct odhcpd_ipaddr
*managed
;
227 ssize_t managed_size
;
228 struct ustream_fd managed_sock
;
251 struct odhcpd_ipaddr
*addr6
;
253 struct odhcpd_ipaddr
*invalid_addr6
;
254 size_t invalid_addr6_len
;
257 struct odhcpd_event router_event
;
258 struct uloop_timeout timer_rs
;
261 // DHCPv6 runtime data
262 struct odhcpd_event dhcpv6_event
;
263 struct list_head ia_assignments
;
266 struct odhcpd_event ndp_event
;
270 struct odhcpd_ipaddr
*addr4
;
273 // DHCPv4 runtime data
274 struct odhcpd_event dhcpv4_event
;
275 struct list_head dhcpv4_assignments
;
276 struct list_head dhcpv4_fr_ips
;
279 char dhcpv6_pd_manager
[128];
280 struct in6_addr dhcpv6_pd_cer
;
284 enum odhcpd_mode dhcpv6
;
285 enum odhcpd_mode ndp
;
286 enum odhcpd_mode dhcpv4
;
293 bool always_rewrite_dns
;
304 bool ra_useleasetime
;
306 uint8_t pref64_length
;
307 struct in6_addr pref64_addr
;
308 bool no_dynamic_dhcp
;
309 bool have_link_local
;
310 uint8_t pio_filter_length
;
311 struct in6_addr pio_filter_addr
;
313 int route_preference
;
317 uint32_t ra_reachabletime
;
318 uint32_t ra_retranstime
;
319 uint32_t ra_hoplimit
;
321 uint32_t preferred_lifetime
;
324 uint32_t dhcp_leasetime
;
327 struct in_addr dhcpv4_start
;
328 struct in_addr dhcpv4_end
;
329 struct in_addr dhcpv4_start_ip
;
330 struct in_addr dhcpv4_end_ip
;
331 struct in_addr dhcpv4_local
;
332 struct in_addr dhcpv4_bcast
;
333 struct in_addr dhcpv4_mask
;
334 struct in_addr
*dhcpv4_router
;
335 size_t dhcpv4_router_cnt
;
336 struct in_addr
*dhcpv4_dns
;
337 size_t dhcpv4_dns_cnt
;
338 bool dhcpv4_forcereconf
;
341 struct in6_addr
*dns
;
348 size_t dhcpv6_raw_len
;
349 bool dhcpv6_assignall
;
352 uint32_t dhcpv6_hostid_len
;
360 struct in_addr
*dhcpv4_ntp
;
361 size_t dhcpv4_ntp_cnt
;
363 uint16_t dhcpv6_ntp_len
;
364 size_t dhcpv6_ntp_cnt
;
367 struct in6_addr
*dhcpv6_sntp
;
368 size_t dhcpv6_sntp_cnt
;
371 extern struct avl_tree interfaces
;
372 extern const struct blobmsg_policy lease_attrs
[LEASE_ATTR_MAX
];
374 inline static void free_assignment(struct dhcp_assignment
*a
)
377 list_del(&a
->lease_list
);
387 inline static struct dhcp_assignment
*alloc_assignment(size_t extra_len
)
389 struct dhcp_assignment
*a
= calloc(1, sizeof(*a
) + extra_len
);
394 INIT_LIST_HEAD(&a
->head
);
395 INIT_LIST_HEAD(&a
->lease_list
);
400 // Exported main functions
401 int odhcpd_register(struct odhcpd_event
*event
);
402 int odhcpd_deregister(struct odhcpd_event
*event
);
403 void odhcpd_process(struct odhcpd_event
*event
);
405 ssize_t
odhcpd_send(int socket
, struct sockaddr_in6
*dest
,
406 struct iovec
*iov
, size_t iov_len
,
407 const struct interface
*iface
);
408 int odhcpd_get_interface_dns_addr(const struct interface
*iface
,
409 struct in6_addr
*addr
);
410 int odhcpd_get_interface_config(const char *ifname
, const char *what
);
411 int odhcpd_get_mac(const struct interface
*iface
, uint8_t mac
[6]);
412 int odhcpd_get_flags(const struct interface
*iface
);
413 struct interface
* odhcpd_get_interface_by_index(int ifindex
);
414 int odhcpd_urandom(void *data
, size_t len
);
416 void odhcpd_run(void);
417 time_t odhcpd_time(void);
418 ssize_t
odhcpd_unhexlify(uint8_t *dst
, size_t len
, const char *src
);
419 void odhcpd_hexlify(char *dst
, const uint8_t *src
, size_t len
);
420 const char *odhcpd_print_mac(const uint8_t *mac
, const size_t len
);
422 int odhcpd_bmemcmp(const void *av
, const void *bv
, size_t bits
);
423 void odhcpd_bmemcpy(void *av
, const void *bv
, size_t bits
);
425 int odhcpd_netmask2bitlen(bool v6
, void *mask
);
426 bool odhcpd_bitlen2netmask(bool v6
, unsigned int bits
, void *mask
);
427 bool odhcpd_valid_hostname(const char *name
);
429 int config_parse_interface(void *data
, size_t len
, const char *iname
, bool overwrite
);
430 struct lease
*config_find_lease_by_duid(const uint8_t *duid
, const uint16_t len
);
431 struct lease
*config_find_lease_by_mac(const uint8_t *mac
);
432 struct lease
*config_find_lease_by_hostid(const uint64_t hostid
);
433 struct lease
*config_find_lease_by_ipaddr(const uint32_t ipaddr
);
434 int set_lease_from_blobmsg(struct blob_attr
*ba
);
438 const char* ubus_get_ifname(const char *name
);
439 void ubus_apply_network(void);
440 bool ubus_has_prefix(const char *name
, const char *ifname
);
441 void ubus_bcast_dhcp_event(const char *type
, const uint8_t *mac
, const size_t mac_len
,
442 const struct in_addr
*addr
, const char *name
, const char *interface
);
445 ssize_t
dhcpv6_ia_handle_IAs(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
446 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
);
447 int dhcpv6_ia_init(void);
448 int dhcpv6_ia_setup_interface(struct interface
*iface
, bool enable
);
449 void dhcpv6_ia_enum_addrs(struct interface
*iface
, struct dhcp_assignment
*c
, time_t now
,
450 dhcpv6_binding_cb_handler_t func
, void *arg
);
451 void dhcpv6_ia_write_statefile(void);
453 int netlink_add_netevent_handler(struct netevent_handler
*hdlr
);
454 ssize_t
netlink_get_interface_addrs(const int ifindex
, bool v6
,
455 struct odhcpd_ipaddr
**addrs
);
456 ssize_t
netlink_get_interface_linklocal(int ifindex
, struct odhcpd_ipaddr
**addrs
);
457 int netlink_get_interface_proxy_neigh(int ifindex
, const struct in6_addr
*addr
);
458 int netlink_setup_route(const struct in6_addr
*addr
, const int prefixlen
,
459 const int ifindex
, const struct in6_addr
*gw
,
460 const uint32_t metric
, const bool add
);
461 int netlink_setup_proxy_neigh(const struct in6_addr
*addr
,
462 const int ifindex
, const bool add
);
463 int netlink_setup_addr(struct odhcpd_ipaddr
*addr
,
464 const int ifindex
, const bool v6
, const bool add
);
465 void netlink_dump_neigh_table(const bool proxy
);
466 void netlink_dump_addr_table(const bool v6
);
468 // Exported module initializers
469 int netlink_init(void);
470 int router_init(void);
471 int dhcpv6_init(void);
473 #ifdef DHCPV4_SUPPORT
474 int dhcpv4_init(void);
476 int dhcpv4_setup_interface(struct interface
*iface
, bool enable
);
477 void dhcpv4_handle_msg(void *addr
, void *data
, size_t len
,
478 struct interface
*iface
, _unused
void *dest_addr
,
479 send_reply_cb_t send_reply
, void *opaque
);
481 int router_setup_interface(struct interface
*iface
, bool enable
);
482 int dhcpv6_setup_interface(struct interface
*iface
, bool enable
);
483 int ndp_setup_interface(struct interface
*iface
, bool enable
);
484 void reload_services(struct interface
*iface
);
486 void odhcpd_reload(void);