X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fdhcpv6.c;h=58b3bd764fd9d89aea7a40d0104b954a2705d3b9;hb=f21a0a7bbc31ff8b519e566087e5b425c39480c6;hp=f0a4938788a96ca7163c2c640dba6413990086aa;hpb=1bbf34a1df3e16865b3b9330b338952076bc26f9;p=oweals%2Fodhcpd.git diff --git a/src/dhcpv6.c b/src/dhcpv6.c index f0a4938..58b3bd7 100644 --- a/src/dhcpv6.c +++ b/src/dhcpv6.c @@ -1,5 +1,6 @@ /** * Copyright (C) 2012-2013 Steven Barth + * Copyright (C) 2018 Hans Dedecker * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License v2 as published by @@ -18,6 +19,9 @@ #include #include #include +#include + +#include #include "odhcpd.h" #include "dhcpv6.h" @@ -33,68 +37,125 @@ static void handle_client_request(void *addr, void *data, size_t len, struct interface *iface, void *dest_addr); - -// Create socket and register events -int init_dhcpv6(void) +/* Create socket and register events */ +int dhcpv6_init(void) { - dhcpv6_ia_init(); - return 0; + return dhcpv6_ia_init(); } - -int setup_dhcpv6_interface(struct interface *iface, bool enable) +int dhcpv6_setup_interface(struct interface *iface, bool enable) { + int ret = 0; + if (iface->dhcpv6_event.uloop.fd > 0) { uloop_fd_delete(&iface->dhcpv6_event.uloop); close(iface->dhcpv6_event.uloop.fd); iface->dhcpv6_event.uloop.fd = -1; } - // Configure multicast settings - if (enable && iface->dhcpv6 && !iface->master) { - int sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP); - if (sock < 0) { - syslog(LOG_ERR, "Failed to create DHCPv6 server socket: %s", - strerror(errno)); - return -1; + /* Configure multicast settings */ + if (enable && iface->dhcpv6) { + struct sockaddr_in6 bind_addr = {AF_INET6, htons(DHCPV6_SERVER_PORT), + 0, IN6ADDR_ANY_INIT, 0}; + struct ipv6_mreq mreq; + int val = 1; + + iface->dhcpv6_event.uloop.fd = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP); + if (iface->dhcpv6_event.uloop.fd < 0) { + syslog(LOG_ERR, "socket(AF_INET6): %m"); + ret = -1; + goto out; } - // Basic IPv6 configuration - setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, iface->ifname, strlen(iface->ifname)); + /* Basic IPv6 configuration */ + if (setsockopt(iface->dhcpv6_event.uloop.fd, SOL_SOCKET, SO_BINDTODEVICE, + iface->ifname, strlen(iface->ifname)) < 0) { + syslog(LOG_ERR, "setsockopt(SO_BINDTODEVICE): %m"); + ret = -1; + goto out; + } - int val = 1; - setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val)); - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); - setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val, sizeof(val)); + if (setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6, IPV6_V6ONLY, + &val, sizeof(val)) < 0) { + syslog(LOG_ERR, "setsockopt(IPV6_V6ONLY): %m"); + ret = -1; + goto out; + } + + if (setsockopt(iface->dhcpv6_event.uloop.fd, SOL_SOCKET, SO_REUSEADDR, + &val, sizeof(val)) < 0) { + syslog(LOG_ERR, "setsockopt(SO_REUSEADDR): %m"); + ret = -1; + goto out; + } + + if (setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, + &val, sizeof(val)) < 0) { + syslog(LOG_ERR, "setsockopt(IPV6_RECVPKTINFO): %m"); + ret = -1; + goto out; + } val = DHCPV6_HOP_COUNT_LIMIT; - setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)); + if (setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, + &val, sizeof(val)) < 0) { + syslog(LOG_ERR, "setsockopt(IPV6_MULTICAST_HOPS): %m"); + ret = -1; + goto out; + } val = 0; - setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, sizeof(val)); + if (setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, + &val, sizeof(val)) < 0) { + syslog(LOG_ERR, "setsockopt(IPV6_MULTICAST_LOOP): %m"); + ret = -1; + goto out; + } - struct sockaddr_in6 bind_addr = {AF_INET6, htons(DHCPV6_SERVER_PORT), - 0, IN6ADDR_ANY_INIT, 0}; + if (bind(iface->dhcpv6_event.uloop.fd, (struct sockaddr*)&bind_addr, + sizeof(bind_addr)) < 0) { + syslog(LOG_ERR, "bind(): %m"); + ret = -1; + goto out; + } + + memset(&mreq, 0, sizeof(mreq)); + inet_pton(AF_INET6, ALL_DHCPV6_RELAYS, &mreq.ipv6mr_multiaddr); + mreq.ipv6mr_interface = iface->ifindex; - if (bind(sock, (struct sockaddr*)&bind_addr, sizeof(bind_addr))) { - syslog(LOG_ERR, "Failed to open DHCPv6 server socket: %s", - strerror(errno)); - return -1; + if (setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, + &mreq, sizeof(mreq)) < 0) { + syslog(LOG_ERR, "setsockopt(IPV6_ADD_MEMBERSHIP): %m"); + ret = -1; + goto out; } - struct ipv6_mreq relay = {ALL_DHCPV6_RELAYS, iface->ifindex}; - struct ipv6_mreq server = {ALL_DHCPV6_SERVERS, iface->ifindex}; - setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &relay, sizeof(relay)); + if (iface->dhcpv6 == MODE_SERVER) { + memset(&mreq, 0, sizeof(mreq)); + inet_pton(AF_INET6, ALL_DHCPV6_SERVERS, &mreq.ipv6mr_multiaddr); + mreq.ipv6mr_interface = iface->ifindex; - if (iface->dhcpv6 == RELAYD_SERVER) - setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &server, sizeof(server)); + if (setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, + &mreq, sizeof(mreq)) < 0) { + syslog(LOG_ERR, "setsockopt(IPV6_ADD_MEMBERSHIP): %m"); + ret = -1; + goto out; + } + } - iface->dhcpv6_event.uloop.fd = sock; iface->dhcpv6_event.handle_dgram = handle_dhcpv6; odhcpd_register(&iface->dhcpv6_event); } - return setup_dhcpv6_ia_interface(iface, enable); + ret = dhcpv6_setup_ia_interface(iface, enable); + +out: + if (ret < 0 && iface->dhcpv6_event.uloop.fd > 0) { + close(iface->dhcpv6_event.uloop.fd); + iface->dhcpv6_event.uloop.fd = -1; + } + + return ret; } enum { @@ -115,7 +176,7 @@ enum { }; static void handle_nested_message(uint8_t *data, size_t len, - uint8_t **opts, uint8_t **end, struct iovec iov[IOV_TOTAL - 1]) + uint8_t **opts, uint8_t **end, struct iovec iov[IOV_TOTAL]) { struct dhcpv6_relay_header *hdr = (struct dhcpv6_relay_header*)data; if (iov[IOV_NESTED].iov_base == NULL) { @@ -169,7 +230,7 @@ static void update_nested_message(uint8_t *data, size_t len, ssize_t pdiff) } } -// Simple DHCPv6-server for information requests +/* Simple DHCPv6-server for information requests */ static void handle_client_request(void *addr, void *data, size_t len, struct interface *iface, void *dest_addr) { @@ -178,9 +239,9 @@ static void handle_client_request(void *addr, void *data, size_t len, if (len < sizeof(*hdr)) return; - syslog(LOG_NOTICE, "Got DHCPv6 request"); + syslog(LOG_NOTICE, "Got DHCPv6 request on %s", iface->name); - // Construct reply message + /* Construct reply message */ struct __attribute__((packed)) { uint8_t msg_type; uint8_t tr_id[3]; @@ -224,23 +285,23 @@ static void handle_client_request(void *addr, void *data, size_t len, } refresh = {htons(DHCPV6_OPT_INFO_REFRESH), htons(sizeof(uint32_t)), htonl(600)}; - struct odhcpd_ipaddr ipaddr; - struct in6_addr *dns_addr = iface->dns; + struct in6_addr dns_addr, *dns_addr_ptr = iface->dns; size_t dns_cnt = iface->dns_cnt; - if (dns_cnt == 0 && odhcpd_get_interface_addresses(iface->ifindex, &ipaddr, 1) == 1) { - dns_addr = &ipaddr.addr; + if ((dns_cnt == 0) && + !odhcpd_get_interface_dns_addr(iface, &dns_addr)) { + dns_addr_ptr = &dns_addr; dns_cnt = 1; } struct { uint16_t type; uint16_t len; - } dns = {htons(DHCPV6_OPT_DNS_SERVERS), htons(dns_cnt * sizeof(*dns_addr))}; + } dns = {htons(DHCPV6_OPT_DNS_SERVERS), htons(dns_cnt * sizeof(*dns_addr_ptr))}; - // DNS Search options + /* DNS Search options */ uint8_t search_buf[256], *search_domain = iface->search; size_t search_len = iface->search_len; @@ -274,7 +335,7 @@ static void handle_client_request(void *addr, void *data, size_t len, [IOV_DEST] = {&dest, (uint8_t*)&dest.clientid_type - (uint8_t*)&dest}, [IOV_MAXRT] = {&maxrt, sizeof(maxrt)}, [IOV_DNS] = {&dns, (dns_cnt) ? sizeof(dns) : 0}, - [IOV_DNS_ADDR] = {dns_addr, dns_cnt * sizeof(*dns_addr)}, + [IOV_DNS_ADDR] = {dns_addr_ptr, dns_cnt * sizeof(*dns_addr_ptr)}, [IOV_SEARCH] = {&search, (search_len) ? sizeof(search) : 0}, [IOV_SEARCH_DOMAIN] = {search_domain, search_len}, [IOV_PDBUF] = {pdbuf, 0}, @@ -302,9 +363,12 @@ static void handle_client_request(void *addr, void *data, size_t len, } else if (opts[-4] == DHCPV6_MSG_INFORMATION_REQUEST) { iov[IOV_REFRESH].iov_base = &refresh; iov[IOV_REFRESH].iov_len = sizeof(refresh); + + /* Return inf max rt option in reply to information request */ + maxrt.type = htons(DHCPV6_OPT_INF_MAX_RT); } - // Go through options and find what we need + /* Go through options and find what we need */ uint16_t otype, olen; uint8_t *odata; dhcpv6_for_each_option(opts, opts_end, otype, olen, odata) { @@ -315,27 +379,28 @@ static void handle_client_request(void *addr, void *data, size_t len, } else if (otype == DHCPV6_OPT_SERVERID) { if (olen != ntohs(dest.serverid_length) || memcmp(odata, &dest.duid_type, olen)) - return; // Not for us + return; /* Not for us */ } else if (iface->filter_class && otype == DHCPV6_OPT_USER_CLASS) { uint8_t *c = odata, *cend = &odata[olen]; for (; &c[2] <= cend && &c[2 + (c[0] << 8) + c[1]] <= cend; c = &c[2 + (c[0] << 8) + c[1]]) { size_t elen = strlen(iface->filter_class); if (((((size_t)c[0]) << 8) | c[1]) == elen && !memcmp(&c[2], iface->filter_class, elen)) - return; // Ignore from homenet + return; /* Ignore from homenet */ } } else if (otype == DHCPV6_OPT_IA_PD) { #ifdef EXT_CER_ID iov[IOV_CERID].iov_len = sizeof(cerid); if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr)) { - struct odhcpd_ipaddr addrs[32]; - ssize_t len = odhcpd_get_interface_addresses(0, addrs, - sizeof(addrs) / sizeof(*addrs)); + struct odhcpd_ipaddr *addrs; + ssize_t len = netlink_get_interface_addrs(0, true, &addrs); for (ssize_t i = 0; i < len; ++i) if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr) || memcmp(&addrs[i].addr, &cerid.addr, sizeof(cerid.addr)) < 0) - cerid.addr = addrs[i].addr; + cerid.addr = addrs[i].addr.in6; + + free(addrs); } #endif } @@ -361,7 +426,7 @@ static void handle_client_request(void *addr, void *data, size_t len, return; } - if (iov[IOV_NESTED].iov_len > 0) // Update length + if (iov[IOV_NESTED].iov_len > 0) /* Update length */ update_nested_message(data, len, iov[IOV_DEST].iov_len + iov[IOV_MAXRT].iov_len + iov[IOV_DNS].iov_len + iov[IOV_DNS_ADDR].iov_len + iov[IOV_SEARCH].iov_len + iov[IOV_SEARCH_DOMAIN].iov_len + @@ -372,13 +437,13 @@ static void handle_client_request(void *addr, void *data, size_t len, } -// Central DHCPv6-relay handler +/* Central DHCPv6-relay handler */ static void handle_dhcpv6(void *addr, void *data, size_t len, struct interface *iface, void *dest_addr) { - if (iface->dhcpv6 == RELAYD_SERVER) { + if (iface->dhcpv6 == MODE_SERVER) { handle_client_request(addr, data, len, iface, dest_addr); - } else if (iface->dhcpv6 == RELAYD_RELAY) { + } else if (iface->dhcpv6 == MODE_RELAY) { if (iface->master) relay_server_response(data, len); else @@ -387,10 +452,10 @@ static void handle_dhcpv6(void *addr, void *data, size_t len, } -// Relay server response (regular relay server handling) +/* Relay server response (regular relay server handling) */ static void relay_server_response(uint8_t *data, size_t len) { - // Information we need to gather + /* Information we need to gather */ uint8_t *payload_data = NULL; size_t payload_len = 0; int32_t ifaceidx = 0; @@ -402,7 +467,7 @@ static void relay_server_response(uint8_t *data, size_t len) int otype, olen; uint8_t *odata, *end = data + len; - // Relay DHCPv6 reply from server to client + /* Relay DHCPv6 reply from server to client */ struct dhcpv6_relay_header *h = (void*)data; if (len < sizeof(*h) || h->msg_type != DHCPV6_MSG_RELAY_REPL) return; @@ -410,7 +475,7 @@ static void relay_server_response(uint8_t *data, size_t len) memcpy(&target.sin6_addr, &h->peer_address, sizeof(struct in6_addr)); - // Go through options and find what we need + /* Go through options and find what we need */ dhcpv6_for_each_option(h->options, end, otype, olen, odata) { if (otype == DHCPV6_OPT_INTERFACE_ID && olen == sizeof(ifaceidx)) { @@ -421,7 +486,7 @@ static void relay_server_response(uint8_t *data, size_t len) } } - // Invalid interface-id or basic payload + /* Invalid interface-id or basic payload */ struct interface *iface = odhcpd_get_interface_by_index(ifaceidx); if (!iface || iface->master || !payload_data || payload_len < 4) return; @@ -430,10 +495,10 @@ static void relay_server_response(uint8_t *data, size_t len) struct in6_addr *dns_ptr = NULL; size_t dns_count = 0; - // If the payload is relay-reply we have to send to the server port + /* If the payload is relay-reply we have to send to the server port */ if (payload_data[0] == DHCPV6_MSG_RELAY_REPL) { target.sin6_port = htons(DHCPV6_SERVER_PORT); - } else { // Go through the payload data + } else { /* Go through the payload data */ struct dhcpv6_client_header *h = (void*)payload_data; end = payload_data + payload_len; @@ -447,24 +512,24 @@ static void relay_server_response(uint8_t *data, size_t len) } } - // Rewrite DNS servers if requested + /* Rewrite DNS servers if requested */ if (iface->always_rewrite_dns && dns_ptr && dns_count > 0) { if (is_authenticated) - return; // Impossible to rewrite + return; /* Impossible to rewrite */ - struct odhcpd_ipaddr ip; const struct in6_addr *rewrite = iface->dns; + struct in6_addr addr; size_t rewrite_cnt = iface->dns_cnt; if (rewrite_cnt == 0) { - if (odhcpd_get_interface_addresses(iface->ifindex, &ip, 1) < 1) + if (odhcpd_get_interface_dns_addr(iface, &addr)) return; // Unable to get interface address - rewrite = &ip.addr; + rewrite = &addr; rewrite_cnt = 1; } - // Copy over any other addresses + /* Copy over any other addresses */ for (size_t i = 0; i < dns_count; ++i) { size_t j = (i < rewrite_cnt) ? i : rewrite_cnt - 1; memcpy(&dns_ptr[i], &rewrite[j], sizeof(*rewrite)); @@ -475,23 +540,45 @@ static void relay_server_response(uint8_t *data, size_t len) odhcpd_send(iface->dhcpv6_event.uloop.fd, &target, &iov, 1, iface); } +static struct odhcpd_ipaddr *relay_link_address(struct interface *iface) +{ + struct odhcpd_ipaddr *addr = NULL; + time_t now = odhcpd_time(); + + for (size_t i = 0; i < iface->addr6_len; i++) { + if (iface->addr6[i].valid <= (uint32_t)now) + continue; + + if (iface->addr6[i].preferred > (uint32_t)now) { + addr = &iface->addr6[i]; + break; + } + + if (!addr || (iface->addr6[i].valid > addr->valid)) + addr = &iface->addr6[i]; + } + + return addr; +} -// Relay client request (regular DHCPv6-relay) +/* Relay client request (regular DHCPv6-relay) */ static void relay_client_request(struct sockaddr_in6 *source, const void *data, size_t len, struct interface *iface) { struct interface *master = odhcpd_get_master_interface(); const struct dhcpv6_relay_header *h = data; - if (!master || master->dhcpv6 != RELAYD_RELAY || + struct sockaddr_in6 s; + + if (!master || master->dhcpv6 != MODE_RELAY || h->msg_type == DHCPV6_MSG_RELAY_REPL || h->msg_type == DHCPV6_MSG_RECONFIGURE || h->msg_type == DHCPV6_MSG_REPLY || h->msg_type == DHCPV6_MSG_ADVERTISE) - return; // Invalid message types for client + return; /* Invalid message types for client */ syslog(LOG_NOTICE, "Got a DHCPv6-request"); - // Construct our forwarding envelope + /* Construct our forwarding envelope */ struct dhcpv6_relay_forward_envelope hdr = { .msg_type = DHCPV6_MSG_RELAY_FORW, .hop_count = 0, @@ -501,33 +588,38 @@ static void relay_client_request(struct sockaddr_in6 *source, .relay_message_len = htons(len), }; - if (h->msg_type == DHCPV6_MSG_RELAY_FORW) { // handle relay-forward + if (h->msg_type == DHCPV6_MSG_RELAY_FORW) { /* handle relay-forward */ if (h->hop_count >= DHCPV6_HOP_COUNT_LIMIT) return; // Invalid hop count else hdr.hop_count = h->hop_count + 1; } - // use memcpy here as the destination fields are unaligned + /* use memcpy here as the destination fields are unaligned */ uint32_t ifindex = iface->ifindex; memcpy(&hdr.peer_address, &source->sin6_addr, sizeof(struct in6_addr)); memcpy(&hdr.interface_id_data, &ifindex, sizeof(ifindex)); - // Detect public IP of slave interface to use as link-address - struct odhcpd_ipaddr ip; - if (odhcpd_get_interface_addresses(iface->ifindex, &ip, 1) < 1) { - // No suitable address! Is the slave not configured yet? - // Detect public IP of master interface and use it instead - // This is WRONG and probably violates the RFC. However - // otherwise we have a hen and egg problem because the - // slave-interface cannot be auto-configured. - if (odhcpd_get_interface_addresses(master->ifindex, &ip, 1) < 1) - return; // Could not obtain a suitable address + /* Detect public IP of slave interface to use as link-address */ + struct odhcpd_ipaddr *ip = relay_link_address(iface); + if (!ip) { + /* No suitable address! Is the slave not configured yet? + * Detect public IP of master interface and use it instead + * This is WRONG and probably violates the RFC. However + * otherwise we have a hen and egg problem because the + * slave-interface cannot be auto-configured. */ + ip = relay_link_address(master); + if (!ip) + return; /* Could not obtain a suitable address */ } - memcpy(&hdr.link_address, &ip.addr, sizeof(hdr.link_address)); - struct sockaddr_in6 dhcpv6_servers = {AF_INET6, - htons(DHCPV6_SERVER_PORT), 0, ALL_DHCPV6_SERVERS, 0}; + memcpy(&hdr.link_address, &ip->addr.in6, sizeof(hdr.link_address)); + + memset(&s, 0, sizeof(s)); + s.sin6_family = AF_INET6; + s.sin6_port = htons(DHCPV6_SERVER_PORT); + inet_pton(AF_INET6, ALL_DHCPV6_SERVERS, &s.sin6_addr); + struct iovec iov[2] = {{&hdr, sizeof(hdr)}, {(void*)data, len}}; - odhcpd_send(iface->dhcpv6_event.uloop.fd, &dhcpv6_servers, iov, 2, master); + odhcpd_send(master->dhcpv6_event.uloop.fd, &s, iov, 2, master); }