router: improve error checking
[project/odhcpd.git] / src / odhcpd.h
1 /**
2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
3 *
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.
7 *
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.
12 *
13 */
14
15 #pragma once
16 #include <netinet/in.h>
17 #include <netinet/icmp6.h>
18 #include <netinet/ether.h>
19 #include <stdbool.h>
20 #include <syslog.h>
21
22 #include <libubox/blobmsg.h>
23
24 #ifndef typeof
25 #define typeof __typeof
26 #endif
27
28 #ifndef container_of
29 #define container_of(ptr, type, member) ( \
30 (type *)( (char *)ptr - offsetof(type,member) ))
31 #endif
32
33 #include <libubox/list.h>
34 #include <libubox/uloop.h>
35
36 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
37
38 // RFC 6106 defines this router advertisement option
39 #define ND_OPT_ROUTE_INFO 24
40 #define ND_OPT_RECURSIVE_DNS 25
41 #define ND_OPT_DNS_SEARCH 31
42
43 #define INFINITE_VALID(x) ((x) == 0)
44
45 #define _unused __attribute__((unused))
46 #define _packed __attribute__((packed))
47
48 #define ALL_IPV6_NODES "ff02::1"
49 #define ALL_IPV6_ROUTERS "ff02::2"
50
51 #define IN6_IS_ADDR_ULA(a) (((a)->s6_addr32[0] & htonl(0xfe000000)) == htonl(0xfc000000))
52
53 struct interface;
54 struct nl_sock;
55 extern struct list_head leases;
56
57 struct odhcpd_event {
58 struct uloop_fd uloop;
59 void (*handle_dgram)(void *addr, void *data, size_t len,
60 struct interface *iface, void *dest_addr);
61 void (*handle_error)(struct odhcpd_event *e, int error);
62 void (*recv_msgs)(struct odhcpd_event *e);
63 };
64
65 union if_addr {
66 struct in_addr in;
67 struct in6_addr in6;
68 };
69
70 struct netevent_handler_info {
71 struct interface *iface;
72 union {
73 struct {
74 union if_addr dst;
75 uint8_t dst_len;
76 union if_addr gateway;
77 } rt;
78 struct {
79 union if_addr dst;
80 uint16_t state;
81 uint8_t flags;
82 } neigh;
83 struct {
84 struct odhcpd_ipaddr *addrs;
85 size_t len;
86 } addrs_old;
87 union if_addr addr;
88 };
89 };
90
91 enum netevents {
92 NETEV_IFINDEX_CHANGE,
93 NETEV_ADDR_ADD,
94 NETEV_ADDR_DEL,
95 NETEV_ADDRLIST_CHANGE,
96 NETEV_ADDR6_ADD,
97 NETEV_ADDR6_DEL,
98 NETEV_ADDR6LIST_CHANGE,
99 NETEV_ROUTE6_ADD,
100 NETEV_ROUTE6_DEL,
101 NETEV_NEIGH6_ADD,
102 NETEV_NEIGH6_DEL,
103 };
104
105 struct netevent_handler {
106 struct list_head head;
107 void (*cb) (unsigned long event, struct netevent_handler_info *info);
108 };
109
110 struct odhcpd_ipaddr {
111 union if_addr addr;
112 uint8_t prefix;
113 uint32_t preferred;
114 uint32_t valid;
115
116 /* ipv6 only */
117 uint8_t dprefix;
118
119 /* ipv4 only */
120 struct in_addr broadcast;
121 };
122
123 enum odhcpd_mode {
124 MODE_DISABLED,
125 MODE_SERVER,
126 MODE_RELAY,
127 MODE_HYBRID
128 };
129
130
131 enum odhcpd_assignment_flags {
132 OAF_TENTATIVE = (1 << 0),
133 OAF_BOUND = (1 << 1),
134 OAF_STATIC = (1 << 2),
135 };
136
137 struct config {
138 bool legacy;
139 bool main_dhcpv4;
140 char *dhcp_cb;
141 char *dhcp_statefile;
142 int log_level;
143 } config;
144
145
146 struct lease {
147 struct list_head head;
148 struct in_addr ipaddr;
149 uint32_t hostid;
150 struct ether_addr mac;
151 uint16_t duid_len;
152 uint8_t *duid;
153 uint32_t dhcpv4_leasetime;
154 char hostname[];
155 };
156
157
158 struct interface {
159 struct list_head head;
160
161 int ifindex;
162 char *ifname;
163 const char *name;
164
165 // IPv6 runtime data
166 struct odhcpd_ipaddr *addr6;
167 size_t addr6_len;
168
169 // RA runtime data
170 struct uloop_timeout timer_rs;
171
172 // DHCPv6 runtime data
173 struct odhcpd_event dhcpv6_event;
174 struct list_head ia_assignments;
175
176 // NDP runtime data
177 struct odhcpd_event ndp_event;
178
179 // IPv4 runtime data
180 struct odhcpd_ipaddr *addr4;
181 size_t addr4_len;
182
183 // DHCPv4 runtime data
184 struct odhcpd_event dhcpv4_event;
185 struct list_head dhcpv4_assignments;
186 struct list_head dhcpv4_fr_ips;
187
188 // Managed PD
189 char dhcpv6_pd_manager[128];
190 struct in6_addr dhcpv6_pd_cer;
191
192 // Services
193 enum odhcpd_mode ra;
194 enum odhcpd_mode dhcpv6;
195 enum odhcpd_mode ndp;
196 enum odhcpd_mode dhcpv4;
197
198 // Config
199 bool inuse;
200 bool external;
201 bool master;
202 bool ignore;
203 bool always_rewrite_dns;
204 bool ra_not_onlink;
205 bool ra_advrouter;
206 bool ra_useleasetime;
207 bool no_dynamic_dhcp;
208 uint8_t pio_filter_length;
209 struct in6_addr pio_filter_addr;
210
211 // RA
212 int learn_routes;
213 int default_router;
214 int ra_managed;
215 int route_preference;
216 int ra_maxinterval;
217 int ra_mininterval;
218 int ra_lifetime;
219 uint32_t ra_reachabletime;
220 uint32_t ra_retranstime;
221 uint32_t ra_hoplimit;
222 int ra_mtu;
223
224 // DHCPv4
225 struct in_addr dhcpv4_start;
226 struct in_addr dhcpv4_end;
227 struct in_addr dhcpv4_start_ip;
228 struct in_addr dhcpv4_end_ip;
229 struct in_addr dhcpv4_local;
230 struct in_addr dhcpv4_bcast;
231 struct in_addr dhcpv4_mask;
232 struct in_addr *dhcpv4_router;
233 size_t dhcpv4_router_cnt;
234 struct in_addr *dhcpv4_dns;
235 size_t dhcpv4_dns_cnt;
236 uint32_t dhcpv4_leasetime;
237 bool dhcpv4_forcereconf;
238
239 // DNS
240 struct in6_addr *dns;
241 size_t dns_cnt;
242 uint8_t *search;
243 size_t search_len;
244
245 // DHCPV6
246 void *dhcpv6_raw;
247 size_t dhcpv6_raw_len;
248 bool dhcpv6_assignall;
249
250 char *upstream;
251 size_t upstream_len;
252
253 char *filter_class;
254 };
255
256 extern struct list_head interfaces;
257
258 #define RA_MANAGED_NO_MFLAG 0
259 #define RA_MANAGED_MFLAG 1
260 #define RA_MANAGED_NO_AFLAG 2
261
262
263 // Exported main functions
264 int odhcpd_register(struct odhcpd_event *event);
265 int odhcpd_deregister(struct odhcpd_event *event);
266 void odhcpd_process(struct odhcpd_event *event);
267
268 ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
269 struct iovec *iov, size_t iov_len,
270 const struct interface *iface);
271 int odhcpd_get_interface_dns_addr(const struct interface *iface,
272 struct in6_addr *addr);
273 struct interface* odhcpd_get_interface_by_name(const char *name);
274 int odhcpd_get_interface_config(const char *ifname, const char *what);
275 int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6]);
276 struct interface* odhcpd_get_interface_by_index(int ifindex);
277 struct interface* odhcpd_get_master_interface(void);
278 int odhcpd_urandom(void *data, size_t len);
279
280 void odhcpd_run(void);
281 time_t odhcpd_time(void);
282 ssize_t odhcpd_unhexlify(uint8_t *dst, size_t len, const char *src);
283 void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len);
284 const char *odhcpd_print_mac(const uint8_t *mac, const size_t len);
285
286 int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits);
287 void odhcpd_bmemcpy(void *av, const void *bv, size_t bits);
288
289 int odhcpd_netmask2bitlen(bool v6, void *mask);
290 bool odhcpd_bitlen2netmask(bool v6, unsigned int bits, void *mask);
291
292 int config_parse_interface(void *data, size_t len, const char *iname, bool overwrite);
293
294 #ifdef WITH_UBUS
295 int ubus_init(void);
296 const char* ubus_get_ifname(const char *name);
297 void ubus_apply_network(void);
298 bool ubus_has_prefix(const char *name, const char *ifname);
299 void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac, const size_t mac_len,
300 const struct in_addr *addr, const char *name, const char *interface);
301 #endif
302
303 int netlink_add_netevent_handler(struct netevent_handler *hdlr);
304 ssize_t netlink_get_interface_addrs(const int ifindex, bool v6,
305 struct odhcpd_ipaddr **addrs);
306 int netlink_setup_route(const struct in6_addr *addr, const int prefixlen,
307 const int ifindex, const struct in6_addr *gw,
308 const uint32_t metric, const bool add);
309 int netlink_setup_proxy_neigh(const struct in6_addr *addr,
310 const int ifindex, const bool add);
311 int netlink_setup_addr(struct odhcpd_ipaddr *addr,
312 const int ifindex, const bool v6, const bool add);
313 void netlink_dump_neigh_table(const bool proxy);
314 void netlink_dump_addr_table(const bool v6);
315
316 // Exported module initializers
317 int netlink_init(void);
318 int router_init(void);
319 int dhcpv6_init(void);
320 int ndp_init(void);
321 #ifdef DHCPV4_SUPPORT
322 int dhcpv4_init(void);
323
324 int dhcpv4_setup_interface(struct interface *iface, bool enable);
325 #endif
326 int router_setup_interface(struct interface *iface, bool enable);
327 int dhcpv6_setup_interface(struct interface *iface, bool enable);
328 int ndp_setup_interface(struct interface *iface, bool enable);
329
330 void odhcpd_reload(void);