1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * dhcpcd - DHCP client daemon 4 * Copyright (c) 2006-2020 Roy Marples <roy@marples.name> 5 * All rights reserved 6 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #define UUID_LEN 36 30 #define DUID_TIME_EPOCH 946684800 31 32 #include <sys/param.h> 33 #include <sys/socket.h> 34 #include <sys/types.h> 35 #ifdef BSD 36 # include <sys/sysctl.h> 37 #endif 38 39 #include <arpa/inet.h> 40 41 #include <net/if.h> 42 #include <net/if_arp.h> 43 44 #include <errno.h> 45 #include <stdint.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <time.h> 50 #include <unistd.h> 51 52 #include "common.h" 53 #include "dhcpcd.h" 54 #include "duid.h" 55 #include "logerr.h" 56 57 static size_t 58 duid_machineuuid(char *uuid, size_t uuid_len) 59 { 60 int r; 61 size_t len = uuid_len; 62 63 #if defined(HW_UUID) /* OpenBSD */ 64 int mib[] = { CTL_HW, HW_UUID }; 65 66 r = sysctl(mib, sizeof(mib)/sizeof(mib[0]), uuid, &len, NULL, 0); 67 #elif defined(KERN_HOSTUUID) /* FreeBSD */ 68 int mib[] = { CTL_KERN, KERN_HOSTUUID }; 69 70 r = sysctl(mib, sizeof(mib)/sizeof(mib[0]), uuid, &len, NULL, 0); 71 #elif defined(__NetBSD__) 72 r = sysctlbyname("machdep.dmi.system-uuid", uuid, &len, NULL, 0); 73 #elif defined(__linux__) 74 FILE *fp; 75 76 fp = fopen("/sys/class/dmi/id/product_uuid", "r"); 77 if (fp == NULL) 78 return 0; 79 if (fgets(uuid, (int)uuid_len, fp) == NULL) { 80 fclose(fp); 81 return 0; 82 } 83 len = strlen(uuid) + 1; 84 fclose(fp); 85 r = len == 1 ? -1 : 0; 86 #else 87 UNUSED(uuid); 88 r = -1; 89 errno = ENOSYS; 90 #endif 91 92 if (r == -1) 93 return 0; 94 return len; 95 } 96 97 static size_t 98 duid_make_uuid(uint8_t *d) 99 { 100 uint16_t type = htons(DUID_UUID); 101 char uuid[UUID_LEN + 1]; 102 size_t l; 103 104 if (duid_machineuuid(uuid, sizeof(uuid)) != sizeof(uuid)) 105 return 0; 106 107 /* All zeros UUID is not valid */ 108 if (strcmp("00000000-0000-0000-0000-000000000000", uuid) == 0) 109 return 0; 110 111 memcpy(d, &type, sizeof(type)); 112 l = sizeof(type); 113 d += sizeof(type); 114 l += hwaddr_aton(d, uuid); 115 return l; 116 } 117 118 size_t 119 duid_make(void *d, const struct interface *ifp, uint16_t type) 120 { 121 uint8_t *p; 122 uint16_t u16; 123 time_t t; 124 uint32_t u32; 125 126 if (ifp->hwlen == 0) 127 return 0; 128 129 p = d; 130 u16 = htons(type); 131 memcpy(p, &u16, sizeof(u16)); 132 p += sizeof(u16); 133 u16 = htons(ifp->hwtype); 134 memcpy(p, &u16, sizeof(u16)); 135 p += sizeof(u16); 136 if (type == DUID_LLT) { 137 /* time returns seconds from jan 1 1970, but DUID-LLT is 138 * seconds from jan 1 2000 modulo 2^32 */ 139 t = time(NULL) - DUID_TIME_EPOCH; 140 u32 = htonl((uint32_t)t & 0xffffffff); 141 memcpy(p, &u32, sizeof(u32)); 142 p += sizeof(u32); 143 } 144 /* Finally, add the MAC address of the interface */ 145 memcpy(p, ifp->hwaddr, ifp->hwlen); 146 p += ifp->hwlen; 147 return (size_t)(p - (uint8_t *)d); 148 } 149 150 #define DUID_STRLEN DUID_LEN * 3 151 static size_t 152 duid_get(struct dhcpcd_ctx *ctx, const struct interface *ifp) 153 { 154 uint8_t *data; 155 size_t len, slen; 156 char line[DUID_STRLEN]; 157 const struct interface *ifp2; 158 159 /* If we already have a DUID then use it as it's never supposed 160 * to change once we have one even if the interfaces do */ 161 if ((len = dhcp_read_hwaddr_aton(ctx, &data, DUID)) != 0) { 162 if (len <= DUID_LEN) { 163 ctx->duid = data; 164 return len; 165 } 166 logerrx("DUID too big (max %u): %s", DUID_LEN, DUID); 167 /* Keep the buffer, will assign below. */ 168 } else { 169 if (errno != ENOENT) 170 logerr("%s", DUID); 171 if ((data = malloc(DUID_LEN)) == NULL) { 172 logerr(__func__); 173 return 0; 174 } 175 } 176 177 /* No file? OK, lets make one based the machines UUID */ 178 if (ifp == NULL) { 179 if (ctx->duid_type != DUID_DEFAULT && 180 ctx->duid_type != DUID_UUID) 181 len = 0; 182 else 183 len = duid_make_uuid(data); 184 if (len == 0) 185 free(data); 186 else 187 ctx->duid = data; 188 return len; 189 } 190 191 /* Regardless of what happens we will create a DUID to use. */ 192 ctx->duid = data; 193 194 /* No UUID? OK, lets make one based on our interface */ 195 if (ifp->hwlen == 0) { 196 logwarnx("%s: does not have hardware address", ifp->name); 197 TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) { 198 if (ifp2->hwlen != 0) 199 break; 200 } 201 if (ifp2) { 202 ifp = ifp2; 203 logwarnx("picked interface %s to generate a DUID", 204 ifp->name); 205 } else { 206 if (ctx->duid_type != DUID_LL) 207 logwarnx("no interfaces have a fixed hardware " 208 "address"); 209 return duid_make(data, ifp, DUID_LL); 210 } 211 } 212 213 len = duid_make(data, ifp, 214 ctx->duid_type == DUID_LL ? DUID_LL : DUID_LLT); 215 hwaddr_ntoa(data, len, line, sizeof(line)); 216 slen = strlen(line); 217 if (slen < sizeof(line) - 2) { 218 line[slen++] = '\n'; 219 line[slen] = '\0'; 220 } 221 if (dhcp_writefile(ctx, DUID, 0640, line, slen) == -1) { 222 logerr("%s: cannot write duid", __func__); 223 if (ctx->duid_type != DUID_LL) 224 return duid_make(data, ifp, DUID_LL); 225 } 226 return len; 227 } 228 229 size_t 230 duid_init(struct dhcpcd_ctx *ctx, const struct interface *ifp) 231 { 232 233 if (ctx->duid == NULL) 234 ctx->duid_len = duid_get(ctx, ifp); 235 return ctx->duid_len; 236 } 237