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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt