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);
55 typedef void (*dhcpv6_binding_cb_handler_t)(struct in6_addr *addr, int prefix,
56 uint32_t pref, uint32_t valid,
64 struct netevent_handler_info {
65 struct interface *iface;
70 union if_addr gateway;
78 struct odhcpd_ipaddr *addrs;
89 NETEV_ADDRLIST_CHANGE,
92 NETEV_ADDR6LIST_CHANGE,
99 struct netevent_handler {
100 struct list_head head;
101 void (*cb) (unsigned long event, struct netevent_handler_info *info);
104 struct odhcpd_ipaddr {
114 struct in_addr broadcast;
125 enum odhcpd_assignment_flags {
126 OAF_TENTATIVE = (1 << 0),
127 OAF_BOUND = (1 << 1),
128 OAF_STATIC = (1 << 2),
129 OAF_BROKEN_HOSTNAME = (1 << 3),
136 char *dhcp_statefile;
142 struct list_head head;
143 struct in_addr ipaddr;
145 struct ether_addr mac;
148 uint32_t dhcpv4_leasetime;
153 struct odhcpd_ref_ip;
155 struct dhcp_assignment {
156 struct list_head head;
157 struct interface *iface;
159 struct sockaddr_in6 peer;
162 #define fr_timer reconf_timer
163 struct uloop_timeout reconf_timer;
164 #define accept_fr_nonce accept_reconf
166 #define fr_cnt reconf_cnt
169 struct odhcpd_ref_ip *fr_ip;
174 uint8_t length; // length == 128 -> IA_NA, length <= 64 -> IA_PD
176 struct odhcpd_ipaddr *managed;
177 ssize_t managed_size;
178 struct ustream_fd managed_sock;
199 struct odhcpd_ipaddr *addr6;
203 struct uloop_timeout timer_rs;
205 // DHCPv6 runtime data
206 struct odhcpd_event dhcpv6_event;
207 struct list_head ia_assignments;
210 struct odhcpd_event ndp_event;
213 struct odhcpd_ipaddr *addr4;
216 // DHCPv4 runtime data
217 struct odhcpd_event dhcpv4_event;
218 struct list_head dhcpv4_assignments;
219 struct list_head dhcpv4_fr_ips;
222 char dhcpv6_pd_manager[128];
223 struct in6_addr dhcpv6_pd_cer;
227 enum odhcpd_mode dhcpv6;
228 enum odhcpd_mode ndp;
229 enum odhcpd_mode dhcpv4;
236 bool always_rewrite_dns;
239 bool ra_useleasetime;
241 bool no_dynamic_dhcp;
242 uint8_t pio_filter_length;
243 struct in6_addr pio_filter_addr;
249 int route_preference;
253 uint32_t ra_reachabletime;
254 uint32_t ra_retranstime;
255 uint32_t ra_hoplimit;
259 struct in_addr dhcpv4_start;
260 struct in_addr dhcpv4_end;
261 struct in_addr dhcpv4_start_ip;
262 struct in_addr dhcpv4_end_ip;
263 struct in_addr dhcpv4_local;
264 struct in_addr dhcpv4_bcast;
265 struct in_addr dhcpv4_mask;
266 struct in_addr *dhcpv4_router;
267 size_t dhcpv4_router_cnt;
268 struct in_addr *dhcpv4_dns;
269 size_t dhcpv4_dns_cnt;
270 uint32_t dhcpv4_leasetime;
271 bool dhcpv4_forcereconf;
274 struct in6_addr *dns;
281 size_t dhcpv6_raw_len;
282 bool dhcpv6_assignall;
292 extern struct avl_tree interfaces;
294 #define RA_MANAGED_NO_MFLAG 0
295 #define RA_MANAGED_MFLAG 1
296 #define RA_MANAGED_NO_AFLAG 2
299 // Exported main functions
300 int odhcpd_register(struct odhcpd_event *event);
301 int odhcpd_deregister(struct odhcpd_event *event);
302 void odhcpd_process(struct odhcpd_event *event);
304 ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
305 struct iovec *iov, size_t iov_len,
306 const struct interface *iface);
307 int odhcpd_get_interface_dns_addr(const struct interface *iface,
308 struct in6_addr *addr);
309 int odhcpd_get_interface_config(const char *ifname, const char *what);
310 int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6]);
311 struct interface* odhcpd_get_interface_by_index(int ifindex);
312 struct interface* odhcpd_get_master_interface(void);
313 int odhcpd_urandom(void *data, size_t len);
315 void odhcpd_run(void);
316 time_t odhcpd_time(void);
317 ssize_t odhcpd_unhexlify(uint8_t *dst, size_t len, const char *src);
318 void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len);
319 const char *odhcpd_print_mac(const uint8_t *mac, const size_t len);
321 int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits);
322 void odhcpd_bmemcpy(void *av, const void *bv, size_t bits);
324 int odhcpd_netmask2bitlen(bool v6, void *mask);
325 bool odhcpd_bitlen2netmask(bool v6, unsigned int bits, void *mask);
326 bool odhcpd_valid_hostname(const char *name);
328 int config_parse_interface(void *data, size_t len, const char *iname, bool overwrite);
332 const char* ubus_get_ifname(const char *name);
333 void ubus_apply_network(void);
334 bool ubus_has_prefix(const char *name, const char *ifname);
335 void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac, const size_t mac_len,
336 const struct in_addr *addr, const char *name, const char *interface);
339 ssize_t dhcpv6_ia_handle_IAs(uint8_t *buf, size_t buflen, struct interface *iface,
340 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end);
341 int dhcpv6_ia_init(void);
342 int dhcpv6_ia_setup_interface(struct interface *iface, bool enable);
343 void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcp_assignment *c, time_t now,
344 dhcpv6_binding_cb_handler_t func, void *arg);
345 void dhcpv6_ia_write_statefile(void);
347 int netlink_add_netevent_handler(struct netevent_handler *hdlr);
348 ssize_t netlink_get_interface_addrs(const int ifindex, bool v6,
349 struct odhcpd_ipaddr **addrs);
350 int netlink_setup_route(const struct in6_addr *addr, const int prefixlen,
351 const int ifindex, const struct in6_addr *gw,
352 const uint32_t metric, const bool add);
353 int netlink_setup_proxy_neigh(const struct in6_addr *addr,
354 const int ifindex, const bool add);
355 int netlink_setup_addr(struct odhcpd_ipaddr *addr,
356 const int ifindex, const bool v6, const bool add);
357 void netlink_dump_neigh_table(const bool proxy);
358 void netlink_dump_addr_table(const bool v6);
360 // Exported module initializers
361 int netlink_init(void);
362 int router_init(void);
363 int dhcpv6_init(void);
365 #ifdef DHCPV4_SUPPORT
366 int dhcpv4_init(void);
368 int dhcpv4_setup_interface(struct interface *iface, bool enable);
370 int router_setup_interface(struct interface *iface, bool enable);
371 int dhcpv6_setup_interface(struct interface *iface, bool enable);
372 int ndp_setup_interface(struct interface *iface, bool enable);
374 void odhcpd_reload(void);