• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/odhcpd/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 #include <libubox/list.h>
 24 #include <libubox/uloop.h>
 25 #include <libubox/avl.h>
 26 #include <libubox/ustream.h>
 27 #include <libubox/vlist.h>
 28 
 29 #define min(a, b) (((a) < (b)) ? (a) : (b))
 30 #define max(a, b) (((a) > (b)) ? (a) : (b))
 31 
 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
 36 
 37 #define INFINITE_VALID(x) ((x) == 0)
 38 
 39 #define _unused __attribute__((unused))
 40 #define _packed __attribute__((packed))
 41 
 42 #define ALL_IPV6_NODES "ff02::1"
 43 #define ALL_IPV6_ROUTERS "ff02::2"
 44 
 45 #define NTP_SUBOPTION_SRV_ADDR 1
 46 #define NTP_SUBOPTION_MC_ADDR 2
 47 #define NTP_SUBOPTION_SRV_FQDN 3
 48 #define IPV6_ADDR_LEN 16
 49 
 50 #define IN6_IS_ADDR_ULA(a) (((a)->s6_addr32[0] & htonl(0xfe000000)) == htonl(0xfc000000))
 51 
 52 #define ADDR_MATCH_PIO_FILTER(_addr, iface) (odhcpd_bmemcmp(&(_addr)->addr, \
 53                                                             &(iface)->pio_filter_addr, \
 54                                                             (iface)->pio_filter_length) != 0 || \
 55                                              (_addr)->prefix < (iface)->pio_filter_length)
 56 
 57 struct interface;
 58 struct nl_sock;
 59 extern struct vlist_tree leases;
 60 extern struct config config;
 61 
 62 struct odhcpd_event {
 63         struct uloop_fd uloop;
 64         void (*handle_dgram)(void *addr, void *data, size_t len,
 65                         struct interface *iface, void *dest_addr);
 66         void (*handle_error)(struct odhcpd_event *e, int error);
 67         void (*recv_msgs)(struct odhcpd_event *e);
 68 };
 69 
 70 typedef int (*send_reply_cb_t)(const void *buf, size_t len,
 71                                const struct sockaddr *dest, socklen_t dest_len,
 72                                void *opaque);
 73 
 74 typedef void (*dhcpv6_binding_cb_handler_t)(struct in6_addr *addr, int prefix,
 75                                             uint32_t pref, uint32_t valid,
 76                                             void *arg);
 77 
 78 union if_addr {
 79         struct in_addr in;
 80         struct in6_addr in6;
 81 };
 82 
 83 struct netevent_handler_info {
 84         struct interface *iface;
 85         union {
 86                 struct {
 87                         union if_addr dst;
 88                         uint8_t dst_len;
 89                         union if_addr gateway;
 90                 } rt;
 91                 struct {
 92                         union if_addr dst;
 93                         uint16_t state;
 94                         uint8_t flags;
 95                 } neigh;
 96                 struct {
 97                         struct odhcpd_ipaddr *addrs;
 98                         size_t len;
 99                 } addrs_old;
100                 union if_addr addr;
101         };
102 };
103 
104 enum netevents {
105         NETEV_IFINDEX_CHANGE,
106         NETEV_ADDR_ADD,
107         NETEV_ADDR_DEL,
108         NETEV_ADDRLIST_CHANGE,
109         NETEV_ADDR6_ADD,
110         NETEV_ADDR6_DEL,
111         NETEV_ADDR6LIST_CHANGE,
112         NETEV_ROUTE6_ADD,
113         NETEV_ROUTE6_DEL,
114         NETEV_NEIGH6_ADD,
115         NETEV_NEIGH6_DEL,
116 };
117 
118 struct netevent_handler {
119         struct list_head head;
120         void (*cb) (unsigned long event, struct netevent_handler_info *info);
121 };
122 
123 struct odhcpd_ipaddr {
124         union if_addr addr;
125         uint8_t prefix;
126         uint32_t preferred;
127         uint32_t valid;
128 
129         union {
130                 /* ipv6 only */
131                 struct {
132                         uint8_t dprefix;
133                         uint8_t invalid_advertisements;
134                 };
135 
136                 /* ipv4 only */
137                 struct in_addr broadcast;
138         };
139 };
140 
141 enum odhcpd_mode {
142         MODE_DISABLED,
143         MODE_SERVER,
144         MODE_RELAY,
145         MODE_HYBRID
146 };
147 
148 
149 enum odhcpd_assignment_flags {
150         OAF_TENTATIVE           = (1 << 0),
151         OAF_BOUND               = (1 << 1),
152         OAF_STATIC              = (1 << 2),
153         OAF_BROKEN_HOSTNAME     = (1 << 3),
154         OAF_DHCPV4              = (1 << 4),
155         OAF_DHCPV6_NA           = (1 << 5),
156         OAF_DHCPV6_PD           = (1 << 6),
157 };
158 
159 struct config {
160         bool legacy;
161         bool main_dhcpv4;
162         char *dhcp_cb;
163         char *dhcp_statefile;
164         int log_level;
165 };
166 
167 
168 struct lease {
169         struct vlist_node node;
170         struct list_head assignments;
171         uint32_t ipaddr;
172         uint64_t hostid;
173         struct ether_addr mac;
174         uint16_t duid_len;
175         uint8_t *duid;
176         uint32_t leasetime;
177         char *hostname;
178 };
179 
180 enum {
181         LEASE_ATTR_IP,
182         LEASE_ATTR_MAC,
183         LEASE_ATTR_DUID,
184         LEASE_ATTR_HOSTID,
185         LEASE_ATTR_LEASETIME,
186         LEASE_ATTR_NAME,
187         LEASE_ATTR_MAX
188 };
189 
190 struct odhcpd_ref_ip;
191 
192 struct dhcp_assignment {
193         struct list_head head;
194         struct list_head lease_list;
195 
196         void (*dhcp_free_cb)(struct dhcp_assignment *a);
197 
198         struct interface *iface;
199         struct lease *lease;
200 
201         struct sockaddr_in6 peer;
202         time_t valid_until;
203         time_t preferred_until;
204 
205 #define fr_timer        reconf_timer
206         struct uloop_timeout reconf_timer;
207 #define accept_fr_nonce accept_reconf
208         bool accept_reconf;
209 #define fr_cnt          reconf_cnt
210         int reconf_cnt;
211         uint8_t key[16];
212         struct odhcpd_ref_ip *fr_ip;
213 
214         uint32_t addr;
215         union {
216                 uint64_t assigned_host_id;
217                 uint32_t assigned_subnet_id;
218         };
219         uint32_t iaid;
220         uint8_t length; // length == 128 -> IA_NA, length <= 64 -> IA_PD
221 
222         struct odhcpd_ipaddr *managed;
223         ssize_t managed_size;
224         struct ustream_fd managed_sock;
225 
226         unsigned int flags;
227         uint32_t leasetime;
228         char *hostname;
229         char *reqopts;
230 #define hwaddr          mac
231         uint8_t mac[6];
232 
233         uint16_t clid_len;
234         uint8_t clid_data[];
235 };
236 
237 
238 struct interface {
239         struct avl_node avl;
240 
241         int ifindex;
242         char *ifname;
243         const char *name;
244 
245         // IPv6 runtime data
246         struct odhcpd_ipaddr *addr6;
247         size_t addr6_len;
248         struct odhcpd_ipaddr *invalid_addr6;
249         size_t invalid_addr6_len;
250 
251         // RA runtime data
252         struct odhcpd_event router_event;
253         struct uloop_timeout timer_rs;
254         uint32_t ra_sent;
255 
256         // DHCPv6 runtime data
257         struct odhcpd_event dhcpv6_event;
258         struct list_head ia_assignments;
259 
260         // NDP runtime data
261         struct odhcpd_event ndp_event;
262         int ndp_ping_fd;
263 
264         // IPv4 runtime data
265         struct odhcpd_ipaddr *addr4;
266         size_t addr4_len;
267 
268         // DHCPv4 runtime data
269         struct odhcpd_event dhcpv4_event;
270         struct list_head dhcpv4_assignments;
271         struct list_head dhcpv4_fr_ips;
272 
273         // Managed PD
274         char dhcpv6_pd_manager[128];
275         struct in6_addr dhcpv6_pd_cer;
276 
277         // Services
278         enum odhcpd_mode ra;
279         enum odhcpd_mode dhcpv6;
280         enum odhcpd_mode ndp;
281         enum odhcpd_mode dhcpv4;
282 
283         // Config
284         bool inuse;
285         bool external;
286         bool master;
287         bool ignore;
288         bool always_rewrite_dns;
289         bool dns_service;
290 
291         // NDP
292         int learn_routes;
293 
294         // RA
295         uint8_t ra_flags;
296         bool ra_slaac;
297         bool ra_not_onlink;
298         bool ra_advrouter;
299         bool ra_useleasetime;
300         bool ra_dns;
301         bool no_dynamic_dhcp;
302         uint8_t pio_filter_length;
303         struct in6_addr pio_filter_addr;
304         int default_router;
305         int route_preference;
306         int ra_maxinterval;
307         int ra_mininterval;
308         int ra_lifetime;
309         uint32_t ra_reachabletime;
310         uint32_t ra_retranstime;
311         uint32_t ra_hoplimit;
312         int ra_mtu;
313         uint32_t preferred_lifetime;
314 
315         // DHCP
316         uint32_t dhcp_leasetime;
317 
318         // DHCPv4
319         struct in_addr dhcpv4_start;
320         struct in_addr dhcpv4_end;
321         struct in_addr dhcpv4_start_ip;
322         struct in_addr dhcpv4_end_ip;
323         struct in_addr dhcpv4_local;
324         struct in_addr dhcpv4_bcast;
325         struct in_addr dhcpv4_mask;
326         struct in_addr *dhcpv4_router;
327         size_t dhcpv4_router_cnt;
328         struct in_addr *dhcpv4_dns;
329         size_t dhcpv4_dns_cnt;
330         bool dhcpv4_forcereconf;
331 
332         // DNS
333         struct in6_addr *dns;
334         size_t dns_cnt;
335         uint8_t *search;
336         size_t search_len;
337 
338         // DHCPV6
339         void *dhcpv6_raw;
340         size_t dhcpv6_raw_len;
341         bool dhcpv6_assignall;
342         bool dhcpv6_pd;
343         bool dhcpv6_na;
344         uint32_t dhcpv6_hostid_len;
345 
346         char *upstream;
347         size_t upstream_len;
348 
349         char *filter_class;
350 
351         // NTP
352         struct in_addr *dhcpv4_ntp;
353         size_t dhcpv4_ntp_cnt;
354         uint8_t *dhcpv6_ntp;
355         uint16_t dhcpv6_ntp_len;
356         size_t dhcpv6_ntp_cnt;
357 
358         // SNTP
359         struct in6_addr *dhcpv6_sntp;
360         size_t dhcpv6_sntp_cnt;
361 };
362 
363 extern struct avl_tree interfaces;
364 extern const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX];
365 
366 inline static void free_assignment(struct dhcp_assignment *a)
367 {
368         list_del(&a->head);
369         list_del(&a->lease_list);
370 
371         if (a->dhcp_free_cb)
372                 a->dhcp_free_cb(a);
373 
374         free(a->hostname);
375         free(a->reqopts);
376         free(a);
377 }
378 
379 inline static struct dhcp_assignment *alloc_assignment(size_t extra_len)
380 {
381         struct dhcp_assignment *a = calloc(1, sizeof(*a) + extra_len);
382 
383         if (!a)
384                 return NULL;
385 
386         INIT_LIST_HEAD(&a->head);
387         INIT_LIST_HEAD(&a->lease_list);
388 
389         return a;
390 }
391 
392 // Exported main functions
393 int odhcpd_register(struct odhcpd_event *event);
394 int odhcpd_deregister(struct odhcpd_event *event);
395 void odhcpd_process(struct odhcpd_event *event);
396 
397 ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
398                 struct iovec *iov, size_t iov_len,
399                 const struct interface *iface);
400 int odhcpd_get_interface_dns_addr(const struct interface *iface,
401                 struct in6_addr *addr);
402 int odhcpd_get_interface_config(const char *ifname, const char *what);
403 int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6]);
404 struct interface* odhcpd_get_interface_by_index(int ifindex);
405 int odhcpd_urandom(void *data, size_t len);
406 
407 void odhcpd_run(void);
408 time_t odhcpd_time(void);
409 ssize_t odhcpd_unhexlify(uint8_t *dst, size_t len, const char *src);
410 void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len);
411 const char *odhcpd_print_mac(const uint8_t *mac, const size_t len);
412 
413 int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits);
414 void odhcpd_bmemcpy(void *av, const void *bv, size_t bits);
415 
416 int odhcpd_netmask2bitlen(bool v6, void *mask);
417 bool odhcpd_bitlen2netmask(bool v6, unsigned int bits, void *mask);
418 bool odhcpd_valid_hostname(const char *name);
419 
420 int config_parse_interface(void *data, size_t len, const char *iname, bool overwrite);
421 struct lease *config_find_lease_by_duid(const uint8_t *duid, const uint16_t len);
422 struct lease *config_find_lease_by_mac(const uint8_t *mac);
423 struct lease *config_find_lease_by_hostid(const uint64_t hostid);
424 struct lease *config_find_lease_by_ipaddr(const uint32_t ipaddr);
425 int set_lease_from_blobmsg(struct blob_attr *ba);
426 
427 #ifdef WITH_UBUS
428 int ubus_init(void);
429 const char* ubus_get_ifname(const char *name);
430 void ubus_apply_network(void);
431 bool ubus_has_prefix(const char *name, const char *ifname);
432 void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac, const size_t mac_len,
433                 const struct in_addr *addr, const char *name, const char *interface);
434 #endif
435 
436 ssize_t dhcpv6_ia_handle_IAs(uint8_t *buf, size_t buflen, struct interface *iface,
437                 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end);
438 int dhcpv6_ia_init(void);
439 int dhcpv6_ia_setup_interface(struct interface *iface, bool enable);
440 void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcp_assignment *c, time_t now,
441                                 dhcpv6_binding_cb_handler_t func, void *arg);
442 void dhcpv6_ia_write_statefile(void);
443 
444 int netlink_add_netevent_handler(struct netevent_handler *hdlr);
445 ssize_t netlink_get_interface_addrs(const int ifindex, bool v6,
446                 struct odhcpd_ipaddr **addrs);
447 int netlink_get_interface_proxy_neigh(int ifindex, const struct in6_addr *addr);
448 int netlink_setup_route(const struct in6_addr *addr, const int prefixlen,
449                 const int ifindex, const struct in6_addr *gw,
450                 const uint32_t metric, const bool add);
451 int netlink_setup_proxy_neigh(const struct in6_addr *addr,
452                 const int ifindex, const bool add);
453 int netlink_setup_addr(struct odhcpd_ipaddr *addr,
454                 const int ifindex, const bool v6, const bool add);
455 void netlink_dump_neigh_table(const bool proxy);
456 void netlink_dump_addr_table(const bool v6);
457 
458 // Exported module initializers
459 int netlink_init(void);
460 int router_init(void);
461 int dhcpv6_init(void);
462 int ndp_init(void);
463 #ifdef DHCPV4_SUPPORT
464 int dhcpv4_init(void);
465 
466 int dhcpv4_setup_interface(struct interface *iface, bool enable);
467 void dhcpv4_handle_msg(void *addr, void *data, size_t len,
468                        struct interface *iface, _unused void *dest_addr,
469                        send_reply_cb_t send_reply, void *opaque);
470 #endif
471 int router_setup_interface(struct interface *iface, bool enable);
472 int dhcpv6_setup_interface(struct interface *iface, bool enable);
473 int ndp_setup_interface(struct interface *iface, bool enable);
474 
475 void odhcpd_reload(void);
476 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt