1 /* $OpenBSD: dhcpleasectl.c,v 1.13 2024/11/21 13:38:14 claudio Exp $ */ 2 3 /* 4 * Copyright (c) 2021 Florian Obser <florian@openbsd.org> 5 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> 6 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org> 7 * Copyright (c) 2003 Henning Brauer <henning@openbsd.org> 8 * 9 * Permission to use, copy, modify, and distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 */ 21 22 #include <sys/types.h> 23 #include <sys/ioctl.h> 24 #include <sys/queue.h> 25 #include <sys/socket.h> 26 #include <sys/time.h> 27 #include <sys/un.h> 28 29 #include <arpa/inet.h> 30 31 #include <net/if.h> 32 33 #include <netinet/in.h> 34 #include <netinet/if_ether.h> 35 36 #include <err.h> 37 #include <errno.h> 38 #include <event.h> 39 #include <imsg.h> 40 #include <limits.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <time.h> 45 #include <unistd.h> 46 47 #include "dhcpleased.h" 48 49 __dead void usage(void); 50 void show_interface_msg(struct ctl_engine_info *); 51 52 struct imsgbuf *ibuf; 53 54 __dead void 55 usage(void) 56 { 57 extern char *__progname; 58 59 fprintf(stderr, "usage: %s [-l] [-s socket] [-w maxwait] interface\n", 60 __progname); 61 exit(1); 62 } 63 64 int 65 main(int argc, char *argv[]) 66 { 67 struct sockaddr_un sun; 68 struct imsg imsg; 69 struct ctl_engine_info *cei; 70 int ctl_sock; 71 int n, lFlag = 0, maxwait_set = 0, didot = 0; 72 int ch, if_index = 0, maxwait = 10, bound = 0; 73 char *sockname; 74 const char *errstr; 75 76 sockname = _PATH_DHCPLEASED_SOCKET; 77 while ((ch = getopt(argc, argv, "ls:w:")) != -1) { 78 switch (ch) { 79 case 'l': 80 lFlag = 1; 81 break; 82 case 's': 83 sockname = optarg; 84 break; 85 case 'w': 86 maxwait_set = 1; 87 maxwait = strtonum(optarg, 1, INT_MAX, &errstr); 88 if (errstr) 89 errx(1, "maxwait value is %s: %s", 90 errstr, optarg); 91 break; 92 93 default: 94 usage(); 95 } 96 } 97 argc -= optind; 98 argv += optind; 99 100 if (argc != 1) 101 usage(); 102 103 if ((if_index = if_nametoindex(argv[0])) == 0) 104 errx(1, "unknown interface"); 105 106 if (!lFlag) { 107 struct ifreq ifr, ifr_x; 108 int ioctl_sock; 109 110 if ((ioctl_sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 111 err(1, NULL); 112 113 strlcpy(ifr.ifr_name, argv[0], sizeof(ifr.ifr_name)); 114 strlcpy(ifr_x.ifr_name, argv[0], sizeof(ifr.ifr_name)); 115 116 if (ioctl(ioctl_sock, SIOCGIFFLAGS, &ifr) == -1) 117 err(1, "SIOCGIFFLAGS"); 118 119 if (ioctl(ioctl_sock, SIOCGIFXFLAGS, &ifr_x) == -1) 120 err(1, "SIOCGIFFLAGS"); 121 122 if (!(ifr.ifr_flags & IFF_UP) || 123 !(ifr_x.ifr_flags & IFXF_AUTOCONF4)) { 124 if (geteuid()) 125 errx(1, "need root privileges"); 126 } 127 128 if (!(ifr.ifr_flags & IFF_UP)) { 129 ifr.ifr_flags |= IFF_UP; 130 if (ioctl(ioctl_sock, SIOCSIFFLAGS, &ifr) == -1) 131 err(1, "SIOCSIFFLAGS"); 132 } 133 if (!(ifr_x.ifr_flags & IFXF_AUTOCONF4)) { 134 ifr_x.ifr_flags |= IFXF_AUTOCONF4; 135 if (ioctl(ioctl_sock, SIOCSIFXFLAGS, &ifr_x) == -1) 136 err(1, "SIOCSIFFLAGS"); 137 } 138 } 139 140 if (lFlag && !maxwait_set) 141 maxwait = 0; 142 143 /* Connect to control socket. */ 144 if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 145 err(1, "socket"); 146 147 memset(&sun, 0, sizeof(sun)); 148 sun.sun_family = AF_UNIX; 149 strlcpy(sun.sun_path, sockname, sizeof(sun.sun_path)); 150 151 if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) 152 err(1, "connect: %s", sockname); 153 154 if (pledge("stdio", NULL) == -1) 155 err(1, "pledge"); 156 157 if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL) 158 err(1, NULL); 159 if (imsgbuf_init(ibuf, ctl_sock) == -1) 160 err(1, NULL); 161 162 if (!lFlag) { 163 imsg_compose(ibuf, IMSG_CTL_SEND_REQUEST, 0, 0, -1, 164 &if_index, sizeof(if_index)); 165 if (imsgbuf_flush(ibuf) == -1) 166 err(1, "write error"); 167 168 } 169 170 for(;;) { 171 imsg_compose(ibuf, IMSG_CTL_SHOW_INTERFACE_INFO, 0, 0, -1, 172 &if_index, sizeof(if_index)); 173 174 if (imsgbuf_flush(ibuf) == -1) 175 err(1, "write error"); 176 177 if ((n = imsgbuf_read(ibuf)) == -1) 178 err(1, "read error"); 179 if (n == 0) 180 errx(1, "pipe closed"); 181 182 if ((n = imsg_get(ibuf, &imsg)) == -1) 183 errx(1, "imsg_get error"); 184 if (n == 0) 185 break; 186 187 if (imsg.hdr.type == IMSG_CTL_END) { 188 if (lFlag) 189 errx(1, "non-autoconf interface %s", argv[0]); 190 else if (--maxwait < 0) 191 break; 192 else 193 continue; 194 } 195 196 cei = imsg.data; 197 if (strcmp(cei->state, "Bound") == 0) 198 bound = 1; 199 200 if (bound || --maxwait < 0) { 201 if (didot) 202 putchar('\n'); 203 show_interface_msg(cei); 204 break; 205 } else { 206 didot = 1; 207 putchar('.'); 208 fflush(stdout); 209 } 210 imsg_free(&imsg); 211 sleep(1); 212 } 213 close(ctl_sock); 214 free(ibuf); 215 216 return (0); 217 } 218 219 void 220 show_interface_msg(struct ctl_engine_info *cei) 221 { 222 struct timespec now, diff; 223 time_t d, h, m, s; 224 int i; 225 char buf[IF_NAMESIZE], *bufp; 226 char ipbuf[INET_ADDRSTRLEN]; 227 char maskbuf[INET_ADDRSTRLEN]; 228 char gwbuf[INET_ADDRSTRLEN]; 229 230 bufp = if_indextoname(cei->if_index, buf); 231 printf("%s [%s]\n", bufp != NULL ? bufp : "unknown", cei->state); 232 memset(ipbuf, 0, sizeof(ipbuf)); 233 if (cei->requested_ip.s_addr != INADDR_ANY) { 234 clock_gettime(CLOCK_MONOTONIC, &now); 235 timespecsub(&now, &cei->request_time, &diff); 236 memset(ipbuf, 0, sizeof(ipbuf)); 237 memset(maskbuf, 0, sizeof(maskbuf)); 238 memset(gwbuf, 0, sizeof(gwbuf)); 239 if (inet_ntop(AF_INET, &cei->requested_ip, ipbuf, 240 sizeof(ipbuf)) == NULL) 241 ipbuf[0] = '\0'; 242 if (inet_ntop(AF_INET, &cei->mask, maskbuf, sizeof(maskbuf)) 243 == NULL) 244 maskbuf[0] = '\0'; 245 printf("\tinet %s netmask %s\n", ipbuf, maskbuf); 246 for (i = 0; i < cei->routes_len; i++) { 247 if (inet_ntop(AF_INET, &cei->routes[i].dst, ipbuf, 248 sizeof(ipbuf)) == NULL) 249 ipbuf[0] = '\0'; 250 if (inet_ntop(AF_INET, &cei->routes[i].mask, maskbuf, 251 sizeof(maskbuf)) == NULL) 252 maskbuf[0] = '\0'; 253 if (inet_ntop(AF_INET, &cei->routes[i].gw, 254 gwbuf, sizeof(gwbuf)) == NULL) 255 gwbuf[0] = '\0'; 256 257 if (cei->routes[i].dst.s_addr == INADDR_ANY 258 && cei->routes[i].mask.s_addr == INADDR_ANY) 259 printf("\tdefault gateway %s\n", gwbuf); 260 else 261 printf("\troute %s/%d gateway %s\n", 262 ipbuf, 33 - 263 ffs(ntohl(cei->routes[i].mask.s_addr)), 264 gwbuf); 265 } 266 if (cei->nameservers[0].s_addr != INADDR_ANY) { 267 printf("\tnameservers"); 268 for (i = 0; i < MAX_RDNS_COUNT && 269 cei->nameservers[i].s_addr != INADDR_ANY; 270 i++) { 271 if (inet_ntop(AF_INET, &cei->nameservers[i], 272 ipbuf, sizeof(ipbuf)) == NULL) 273 continue; 274 printf(" %s", ipbuf); 275 } 276 printf("\n"); 277 } 278 s = cei->lease_time - diff.tv_sec; 279 if (s < 0) 280 s = 0; 281 282 if ( s > 86400 ) { 283 d = s / 86400; 284 285 /* round up */ 286 if (s - d * 86400 > 43200) 287 d++; 288 printf("\tlease %lld day%s\n", d, d > 1 ? "s" : ""); 289 } else if (s > 3600) { 290 h = s / 3600; 291 292 /* round up */ 293 if (s - h * 3600 > 1800) 294 h++; 295 printf("\tlease %lld hour%s\n", h, h > 1 ? "s" : ""); 296 } else if (s > 60) { 297 m = s / 60; 298 299 /* round up */ 300 if (s - m * 60 > 30) 301 m++; 302 printf("\tlease %lld minute%s\n", m, m > 1 ? "s" : ""); 303 } else 304 printf("\tlease %lld second%s\n", s, s > 1 ? "s" : ""); 305 } 306 if (cei->server_identifier.s_addr != INADDR_ANY) { 307 if (inet_ntop(AF_INET, &cei->server_identifier, ipbuf, 308 sizeof(ipbuf)) == NULL) 309 ipbuf[0] = '\0'; 310 } else if (cei->dhcp_server.s_addr != INADDR_ANY) { 311 if (inet_ntop(AF_INET, &cei->dhcp_server, ipbuf, sizeof(ipbuf)) 312 == NULL) 313 ipbuf[0] = '\0'; 314 } 315 if (ipbuf[0] != '\0') 316 printf("\tdhcp server %s\n", ipbuf); 317 } 318