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