1 /* $OpenBSD: sync.c,v 1.15 2013/10/18 15:19:40 krw Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Bob Beck <beck@openbsd.org> 5 * Copyright (c) 2006, 2007 Reyk Floeter <reyk@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/param.h> 21 #include <sys/stdint.h> 22 #include <sys/file.h> 23 #include <sys/wait.h> 24 #include <sys/socket.h> 25 #include <sys/resource.h> 26 #include <sys/uio.h> 27 #include <sys/ioctl.h> 28 #include <sys/queue.h> 29 30 31 #include <net/if.h> 32 #include <netinet/in.h> 33 #include <arpa/inet.h> 34 35 #include <err.h> 36 #include <errno.h> 37 #include <pwd.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <unistd.h> 42 #include <sha1.h> 43 44 #include <netdb.h> 45 46 #include <openssl/hmac.h> 47 48 #include "dhcpd.h" 49 #include "sync.h" 50 51 int sync_debug; 52 53 u_int32_t sync_counter; 54 int syncfd = -1; 55 int sendmcast; 56 57 struct sockaddr_in sync_in; 58 struct sockaddr_in sync_out; 59 static char *sync_key; 60 61 struct sync_host { 62 LIST_ENTRY(sync_host) h_entry; 63 64 char *h_name; 65 struct sockaddr_in sh_addr; 66 }; 67 LIST_HEAD(synchosts, sync_host) sync_hosts = LIST_HEAD_INITIALIZER(sync_hosts); 68 69 void sync_send(struct iovec *, int); 70 71 int 72 sync_addhost(const char *name, u_short port) 73 { 74 struct addrinfo hints, *res, *res0; 75 struct sync_host *shost; 76 struct sockaddr_in *addr = NULL; 77 78 bzero(&hints, sizeof(hints)); 79 hints.ai_family = PF_UNSPEC; 80 hints.ai_socktype = SOCK_STREAM; 81 if (getaddrinfo(name, NULL, &hints, &res0) != 0) 82 return (EINVAL); 83 for (res = res0; res != NULL; res = res->ai_next) { 84 if (addr == NULL && res->ai_family == AF_INET) { 85 addr = (struct sockaddr_in *)res->ai_addr; 86 break; 87 } 88 } 89 if (addr == NULL) { 90 freeaddrinfo(res0); 91 return (EINVAL); 92 } 93 if ((shost = (struct sync_host *) 94 calloc(1, sizeof(struct sync_host))) == NULL) { 95 freeaddrinfo(res0); 96 return (ENOMEM); 97 } 98 shost->h_name = strdup(name); 99 if (shost->h_name == NULL) { 100 free(shost); 101 freeaddrinfo(res0); 102 return (ENOMEM); 103 } 104 105 shost->sh_addr.sin_family = AF_INET; 106 shost->sh_addr.sin_port = htons(port); 107 shost->sh_addr.sin_addr.s_addr = addr->sin_addr.s_addr; 108 freeaddrinfo(res0); 109 110 LIST_INSERT_HEAD(&sync_hosts, shost, h_entry); 111 112 if (sync_debug) 113 note("added dhcp sync host %s (address %s, port %d)\n", 114 shost->h_name, inet_ntoa(shost->sh_addr.sin_addr), port); 115 116 return (0); 117 } 118 119 int 120 sync_init(const char *iface, const char *baddr, u_short port) 121 { 122 int one = 1; 123 u_int8_t ttl; 124 struct ifreq ifr; 125 struct ip_mreq mreq; 126 struct sockaddr_in *addr; 127 char ifnam[IFNAMSIZ], *ttlstr; 128 const char *errstr; 129 struct in_addr ina; 130 131 if (iface != NULL) 132 sendmcast++; 133 134 bzero(&ina, sizeof(ina)); 135 if (baddr != NULL) { 136 if (inet_pton(AF_INET, baddr, &ina) != 1) { 137 ina.s_addr = htonl(INADDR_ANY); 138 if (iface == NULL) 139 iface = baddr; 140 else if (iface != NULL && strcmp(baddr, iface) != 0) { 141 fprintf(stderr, "multicast interface does " 142 "not match"); 143 return (-1); 144 } 145 } 146 } 147 148 sync_key = SHA1File(DHCP_SYNC_KEY, NULL); 149 if (sync_key == NULL) { 150 if (errno != ENOENT) { 151 fprintf(stderr, "failed to open sync key: %s\n", 152 strerror(errno)); 153 return (-1); 154 } 155 /* Use empty key by default */ 156 sync_key = ""; 157 } 158 159 syncfd = socket(AF_INET, SOCK_DGRAM, 0); 160 if (syncfd == -1) 161 return (-1); 162 163 if (setsockopt(syncfd, SOL_SOCKET, SO_REUSEADDR, &one, 164 sizeof(one)) == -1) 165 goto fail; 166 167 bzero(&sync_out, sizeof(sync_out)); 168 sync_out.sin_family = AF_INET; 169 sync_out.sin_len = sizeof(sync_out); 170 sync_out.sin_addr.s_addr = ina.s_addr; 171 if (baddr == NULL && iface == NULL) 172 sync_out.sin_port = 0; 173 else 174 sync_out.sin_port = htons(port); 175 176 if (bind(syncfd, (struct sockaddr *)&sync_out, sizeof(sync_out)) == -1) 177 goto fail; 178 179 /* Don't use multicast messages */ 180 if (iface == NULL) 181 return (syncfd); 182 183 strlcpy(ifnam, iface, sizeof(ifnam)); 184 ttl = DHCP_SYNC_MCASTTTL; 185 if ((ttlstr = strchr(ifnam, ':')) != NULL) { 186 *ttlstr++ = '\0'; 187 ttl = (u_int8_t)strtonum(ttlstr, 1, UINT8_MAX, &errstr); 188 if (errstr) { 189 fprintf(stderr, "invalid multicast ttl %s: %s", 190 ttlstr, errstr); 191 goto fail; 192 } 193 } 194 195 bzero(&ifr, sizeof(ifr)); 196 strlcpy(ifr.ifr_name, ifnam, sizeof(ifr.ifr_name)); 197 if (ioctl(syncfd, SIOCGIFADDR, &ifr) == -1) 198 goto fail; 199 200 bzero(&sync_in, sizeof(sync_in)); 201 addr = (struct sockaddr_in *)&ifr.ifr_addr; 202 sync_in.sin_family = AF_INET; 203 sync_in.sin_len = sizeof(sync_in); 204 sync_in.sin_addr.s_addr = addr->sin_addr.s_addr; 205 sync_in.sin_port = htons(port); 206 207 bzero(&mreq, sizeof(mreq)); 208 sync_out.sin_addr.s_addr = inet_addr(DHCP_SYNC_MCASTADDR); 209 mreq.imr_multiaddr.s_addr = inet_addr(DHCP_SYNC_MCASTADDR); 210 mreq.imr_interface.s_addr = sync_in.sin_addr.s_addr; 211 212 if (setsockopt(syncfd, IPPROTO_IP, 213 IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) { 214 fprintf(stderr, "failed to add multicast membership to %s: %s", 215 DHCP_SYNC_MCASTADDR, strerror(errno)); 216 goto fail; 217 } 218 if (setsockopt(syncfd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 219 sizeof(ttl)) == -1) { 220 fprintf(stderr, "failed to set multicast ttl to " 221 "%u: %s\n", ttl, strerror(errno)); 222 setsockopt(syncfd, IPPROTO_IP, 223 IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); 224 goto fail; 225 } 226 227 if (sync_debug) 228 syslog_r(LOG_DEBUG, &sdata, "using multicast dhcp sync %smode " 229 "(ttl %u, group %s, port %d)\n", 230 sendmcast ? "" : "receive ", 231 ttl, inet_ntoa(sync_out.sin_addr), port); 232 233 return (syncfd); 234 235 fail: 236 close(syncfd); 237 return (-1); 238 } 239 240 void 241 sync_recv(void) 242 { 243 struct dhcp_synchdr *hdr; 244 struct sockaddr_in addr; 245 struct dhcp_synctlv_hdr *tlv; 246 struct dhcp_synctlv_lease *lv; 247 struct lease *lease; 248 u_int8_t buf[DHCP_SYNC_MAXSIZE]; 249 u_int8_t hmac[2][DHCP_SYNC_HMAC_LEN]; 250 struct lease l, *lp; 251 u_int8_t *p; 252 socklen_t addr_len; 253 ssize_t len; 254 u_int hmac_len; 255 256 bzero(&addr, sizeof(addr)); 257 bzero(buf, sizeof(buf)); 258 259 addr_len = sizeof(addr); 260 if ((len = recvfrom(syncfd, buf, sizeof(buf), 0, 261 (struct sockaddr *)&addr, &addr_len)) < 1) 262 return; 263 if (addr.sin_addr.s_addr != htonl(INADDR_ANY) && 264 bcmp(&sync_in.sin_addr, &addr.sin_addr, 265 sizeof(addr.sin_addr)) == 0) 266 return; 267 268 /* Ignore invalid or truncated packets */ 269 hdr = (struct dhcp_synchdr *)buf; 270 if (len < sizeof(struct dhcp_synchdr) || 271 hdr->sh_version != DHCP_SYNC_VERSION || 272 hdr->sh_af != AF_INET || 273 len < ntohs(hdr->sh_length)) 274 goto trunc; 275 len = ntohs(hdr->sh_length); 276 277 /* Compute and validate HMAC */ 278 memcpy(hmac[0], hdr->sh_hmac, DHCP_SYNC_HMAC_LEN); 279 bzero(hdr->sh_hmac, DHCP_SYNC_HMAC_LEN); 280 HMAC(EVP_sha1(), sync_key, strlen(sync_key), buf, len, 281 hmac[1], &hmac_len); 282 if (bcmp(hmac[0], hmac[1], DHCP_SYNC_HMAC_LEN) != 0) 283 goto trunc; 284 285 if (sync_debug) 286 note("%s(sync): received packet of %d bytes\n", 287 inet_ntoa(addr.sin_addr), (int)len); 288 289 p = (u_int8_t *)(hdr + 1); 290 while (len) { 291 tlv = (struct dhcp_synctlv_hdr *)p; 292 293 if (len < sizeof(struct dhcp_synctlv_hdr) || 294 len < ntohs(tlv->st_length)) 295 goto trunc; 296 297 switch (ntohs(tlv->st_type)) { 298 case DHCP_SYNC_LEASE: 299 lv = (struct dhcp_synctlv_lease *)tlv; 300 if (sizeof(*lv) > ntohs(tlv->st_length)) 301 goto trunc; 302 lease = find_lease_by_hw_addr( 303 lv->lv_hardware_addr.haddr, 304 lv->lv_hardware_addr.hlen); 305 if (lease == NULL) 306 lease = find_lease_by_ip_addr(lv->lv_ip_addr); 307 308 lp = &l; 309 memset(lp, 0, sizeof(*lp)); 310 lp->timestamp = ntohl(lv->lv_timestamp); 311 lp->starts = ntohl(lv->lv_starts); 312 lp->ends = ntohl(lv->lv_ends); 313 memcpy(&lp->ip_addr, &lv->lv_ip_addr, 314 sizeof(lp->ip_addr)); 315 memcpy(&lp->hardware_addr, &lv->lv_hardware_addr, 316 sizeof(lp->hardware_addr)); 317 note("DHCP_SYNC_LEASE from %s for hw %s -> ip %s, " 318 "start %lld, end %lld", 319 inet_ntoa(addr.sin_addr), 320 print_hw_addr(lp->hardware_addr.htype, 321 lp->hardware_addr.hlen, lp->hardware_addr.haddr), 322 piaddr(lp->ip_addr), 323 (long long)lp->starts, (long long)lp->ends); 324 /* now whack the lease in there */ 325 if (lease == NULL) { 326 enter_lease(lp); 327 write_leases(); 328 } 329 else if (lease->ends < lp->ends) 330 supersede_lease(lease, lp, 1); 331 else if (lease->ends > lp->ends) 332 /* 333 * our partner sent us a lease 334 * that is older than what we have, 335 * so re-educate them with what we 336 * know is newer. 337 */ 338 sync_lease(lease); 339 break; 340 case DHCP_SYNC_END: 341 goto done; 342 default: 343 printf("invalid type: %d\n", ntohs(tlv->st_type)); 344 goto trunc; 345 } 346 len -= ntohs(tlv->st_length); 347 p = ((u_int8_t *)tlv) + ntohs(tlv->st_length); 348 } 349 350 done: 351 return; 352 353 trunc: 354 if (sync_debug) 355 note("%s(sync): truncated or invalid packet\n", 356 inet_ntoa(addr.sin_addr)); 357 } 358 359 void 360 sync_send(struct iovec *iov, int iovlen) 361 { 362 struct sync_host *shost; 363 struct msghdr msg; 364 365 if (syncfd == -1) 366 return; 367 368 /* setup buffer */ 369 bzero(&msg, sizeof(msg)); 370 msg.msg_iov = iov; 371 msg.msg_iovlen = iovlen; 372 373 if (sendmcast) { 374 if (sync_debug) 375 note("sending multicast sync message\n"); 376 msg.msg_name = &sync_out; 377 msg.msg_namelen = sizeof(sync_out); 378 if (sendmsg(syncfd, &msg, 0) == -1) 379 warning("sending multicast sync message failed: %m"); 380 } 381 382 LIST_FOREACH(shost, &sync_hosts, h_entry) { 383 if (sync_debug) 384 note("sending sync message to %s (%s)\n", 385 shost->h_name, inet_ntoa(shost->sh_addr.sin_addr)); 386 msg.msg_name = &shost->sh_addr; 387 msg.msg_namelen = sizeof(shost->sh_addr); 388 if (sendmsg(syncfd, &msg, 0) == -1) 389 warning("sending sync message failed: %m"); 390 } 391 } 392 393 void 394 sync_lease(struct lease *lease) 395 { 396 struct iovec iov[4]; 397 struct dhcp_synchdr hdr; 398 struct dhcp_synctlv_lease lv; 399 struct dhcp_synctlv_hdr end; 400 char pad[DHCP_ALIGNBYTES]; 401 u_int16_t leaselen, padlen; 402 int i = 0; 403 HMAC_CTX ctx; 404 u_int hmac_len; 405 406 if (sync_key == NULL) 407 return; 408 409 bzero(&hdr, sizeof(hdr)); 410 bzero(&lv, sizeof(lv)); 411 bzero(&pad, sizeof(pad)); 412 413 HMAC_CTX_init(&ctx); 414 HMAC_Init(&ctx, sync_key, strlen(sync_key), EVP_sha1()); 415 416 leaselen = sizeof(lv); 417 padlen = DHCP_ALIGN(leaselen) - leaselen; 418 419 /* Add DHCP sync packet header */ 420 hdr.sh_version = DHCP_SYNC_VERSION; 421 hdr.sh_af = AF_INET; 422 hdr.sh_counter = sync_counter++; 423 hdr.sh_length = htons(sizeof(hdr) + sizeof(lv) + padlen + sizeof(end)); 424 iov[i].iov_base = &hdr; 425 iov[i].iov_len = sizeof(hdr); 426 HMAC_Update(&ctx, iov[i].iov_base, iov[i].iov_len); 427 i++; 428 429 /* Add single DHCP sync address entry */ 430 lv.lv_type = htons(DHCP_SYNC_LEASE); 431 lv.lv_length = htons(leaselen + padlen); 432 lv.lv_timestamp = htonl(lease->timestamp); 433 lv.lv_starts = htonl(lease->starts); 434 lv.lv_ends = htonl(lease->ends); 435 memcpy(&lv.lv_ip_addr, &lease->ip_addr, sizeof(lv.lv_ip_addr)); 436 memcpy(&lv.lv_hardware_addr, &lease->hardware_addr, 437 sizeof(lv.lv_hardware_addr)); 438 note("sending DHCP_SYNC_LEASE for hw %s -> ip %s, start %d, end %d", 439 print_hw_addr(lv.lv_hardware_addr.htype, lv.lv_hardware_addr.hlen, 440 lv.lv_hardware_addr.haddr), piaddr(lease->ip_addr), 441 ntohl(lv.lv_starts), ntohl(lv.lv_ends)); 442 iov[i].iov_base = &lv; 443 iov[i].iov_len = sizeof(lv); 444 HMAC_Update(&ctx, iov[i].iov_base, iov[i].iov_len); 445 i++; 446 447 iov[i].iov_base = pad; 448 iov[i].iov_len = padlen; 449 HMAC_Update(&ctx, iov[i].iov_base, iov[i].iov_len); 450 i++; 451 452 /* Add end marker */ 453 end.st_type = htons(DHCP_SYNC_END); 454 end.st_length = htons(sizeof(end)); 455 iov[i].iov_base = &end; 456 iov[i].iov_len = sizeof(end); 457 HMAC_Update(&ctx, iov[i].iov_base, iov[i].iov_len); 458 i++; 459 460 HMAC_Final(&ctx, hdr.sh_hmac, &hmac_len); 461 462 /* Send message to the target hosts */ 463 sync_send(iov, i); 464 HMAC_CTX_cleanup(&ctx); 465 } 466