xref: /freebsd/sbin/dumpon/dumpon.c (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  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  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 static char sccsid[] = "From: @(#)swapon.c	8.1 (Berkeley) 6/5/93";
41 #endif /* not lint */
42 #endif
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #include <sys/param.h>
47 #include <sys/capsicum.h>
48 #include <sys/disk.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 #include <sys/wait.h>
52 
53 #include <assert.h>
54 #include <capsicum_helpers.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <ifaddrs.h>
59 #include <netdb.h>
60 #include <paths.h>
61 #include <stdbool.h>
62 #include <stdint.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <sysexits.h>
67 #include <unistd.h>
68 
69 #include <arpa/inet.h>
70 
71 #include <net/if.h>
72 #include <net/if_dl.h>
73 #include <net/route.h>
74 
75 #include <netinet/in.h>
76 #include <netinet/netdump/netdump.h>
77 
78 #ifdef HAVE_CRYPTO
79 #include <openssl/err.h>
80 #include <openssl/pem.h>
81 #include <openssl/rand.h>
82 #include <openssl/rsa.h>
83 #endif
84 
85 static int	verbose;
86 
87 static void _Noreturn
88 usage(void)
89 {
90 	fprintf(stderr,
91     "usage: dumpon [-i index] [-r] [-v] [-k <pubkey>] [-Zz] <device>\n"
92     "       dumpon [-i index] [-r] [-v] [-k <pubkey>] [-Zz]\n"
93     "              [-g <gateway>] -s <server> -c <client> <iface>\n"
94     "       dumpon [-v] off\n"
95     "       dumpon [-v] -l\n");
96 	exit(EX_USAGE);
97 }
98 
99 /*
100  * Look for a default route on the specified interface.
101  */
102 static char *
103 find_gateway(const char *ifname)
104 {
105 	struct ifaddrs *ifa, *ifap;
106 	struct rt_msghdr *rtm;
107 	struct sockaddr *sa;
108 	struct sockaddr_dl *sdl;
109 	struct sockaddr_in *dst, *mask, *gw;
110 	char *buf, *next, *ret;
111 	size_t sz;
112 	int error, i, ifindex, mib[7];
113 
114 	/* First look up the interface index. */
115 	if (getifaddrs(&ifap) != 0)
116 		err(EX_OSERR, "getifaddrs");
117 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
118 		if (ifa->ifa_addr->sa_family != AF_LINK)
119 			continue;
120 		if (strcmp(ifa->ifa_name, ifname) == 0) {
121 			sdl = (struct sockaddr_dl *)(void *)ifa->ifa_addr;
122 			ifindex = sdl->sdl_index;
123 			break;
124 		}
125 	}
126 	if (ifa == NULL)
127 		errx(1, "couldn't find interface index for '%s'", ifname);
128 	freeifaddrs(ifap);
129 
130 	/* Now get the IPv4 routing table. */
131 	mib[0] = CTL_NET;
132 	mib[1] = PF_ROUTE;
133 	mib[2] = 0;
134 	mib[3] = AF_INET;
135 	mib[4] = NET_RT_DUMP;
136 	mib[5] = 0;
137 	mib[6] = -1; /* FIB */
138 
139 	for (;;) {
140 		if (sysctl(mib, nitems(mib), NULL, &sz, NULL, 0) != 0)
141 			err(EX_OSERR, "sysctl(NET_RT_DUMP)");
142 		buf = malloc(sz);
143 		error = sysctl(mib, nitems(mib), buf, &sz, NULL, 0);
144 		if (error == 0)
145 			break;
146 		if (errno != ENOMEM)
147 			err(EX_OSERR, "sysctl(NET_RT_DUMP)");
148 		free(buf);
149 	}
150 
151 	ret = NULL;
152 	for (next = buf; next < buf + sz; next += rtm->rtm_msglen) {
153 		rtm = (struct rt_msghdr *)(void *)next;
154 		if (rtm->rtm_version != RTM_VERSION)
155 			continue;
156 		if ((rtm->rtm_flags & RTF_GATEWAY) == 0 ||
157 		    rtm->rtm_index != ifindex)
158 			continue;
159 
160 		dst = gw = mask = NULL;
161 		sa = (struct sockaddr *)(rtm + 1);
162 		for (i = 0; i < RTAX_MAX; i++) {
163 			if ((rtm->rtm_addrs & (1 << i)) != 0) {
164 				switch (i) {
165 				case RTAX_DST:
166 					dst = (void *)sa;
167 					break;
168 				case RTAX_GATEWAY:
169 					gw = (void *)sa;
170 					break;
171 				case RTAX_NETMASK:
172 					mask = (void *)sa;
173 					break;
174 				}
175 			}
176 			sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
177 		}
178 
179 		if (dst->sin_addr.s_addr == INADDR_ANY &&
180 		    mask->sin_addr.s_addr == 0) {
181 			ret = inet_ntoa(gw->sin_addr);
182 			break;
183 		}
184 	}
185 	free(buf);
186 	return (ret);
187 }
188 
189 static void
190 check_size(int fd, const char *fn)
191 {
192 	int name[] = { CTL_HW, HW_PHYSMEM };
193 	size_t namelen = nitems(name);
194 	unsigned long physmem;
195 	size_t len;
196 	off_t mediasize;
197 	int minidump;
198 
199 	len = sizeof(minidump);
200 	if (sysctlbyname("debug.minidump", &minidump, &len, NULL, 0) == 0 &&
201 	    minidump == 1)
202 		return;
203 	len = sizeof(physmem);
204 	if (sysctl(name, namelen, &physmem, &len, NULL, 0) != 0)
205 		err(EX_OSERR, "can't get memory size");
206 	if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) != 0)
207 		err(EX_OSERR, "%s: can't get size", fn);
208 	if ((uintmax_t)mediasize < (uintmax_t)physmem)
209 		errx(EX_IOERR, "%s is smaller than physical memory", fn);
210 }
211 
212 #ifdef HAVE_CRYPTO
213 static void
214 _genkey(const char *pubkeyfile, struct diocskerneldump_arg *kdap)
215 {
216 	FILE *fp;
217 	RSA *pubkey;
218 
219 	assert(pubkeyfile != NULL);
220 	assert(kdap != NULL);
221 
222 	fp = NULL;
223 	pubkey = NULL;
224 
225 	fp = fopen(pubkeyfile, "r");
226 	if (fp == NULL)
227 		err(1, "Unable to open %s", pubkeyfile);
228 
229 	/*
230 	 * Obsolescent OpenSSL only knows about /dev/random, and needs to
231 	 * pre-seed before entering cap mode.  For whatever reason,
232 	 * RSA_pub_encrypt uses the internal PRNG.
233 	 */
234 #if OPENSSL_VERSION_NUMBER < 0x10100000L
235 	{
236 		unsigned char c[1];
237 		RAND_bytes(c, 1);
238 	}
239 #endif
240 
241 	if (caph_enter() < 0)
242 		err(1, "Unable to enter capability mode");
243 
244 	pubkey = RSA_new();
245 	if (pubkey == NULL) {
246 		errx(1, "Unable to allocate an RSA structure: %s",
247 		    ERR_error_string(ERR_get_error(), NULL));
248 	}
249 
250 	pubkey = PEM_read_RSA_PUBKEY(fp, &pubkey, NULL, NULL);
251 	fclose(fp);
252 	fp = NULL;
253 	if (pubkey == NULL)
254 		errx(1, "Unable to read data from %s.", pubkeyfile);
255 
256 	/*
257 	 * RSA keys under ~1024 bits are trivially factorable (2018).  OpenSSL
258 	 * provides an API for RSA keys to estimate the symmetric-cipher
259 	 * "equivalent" bits of security (defined in NIST SP800-57), which as
260 	 * of this writing equates a 2048-bit RSA key to 112 symmetric cipher
261 	 * bits.
262 	 *
263 	 * Use this API as a seatbelt to avoid suggesting to users that their
264 	 * privacy is protected by encryption when the key size is insufficient
265 	 * to prevent compromise via factoring.
266 	 *
267 	 * Future work: Sanity check for weak 'e', and sanity check for absence
268 	 * of 'd' (i.e., the supplied key is a public key rather than a full
269 	 * keypair).
270 	 */
271 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
272 	if (RSA_security_bits(pubkey) < 112)
273 #else
274 	if (RSA_size(pubkey) * 8 < 2048)
275 #endif
276 		errx(1, "Small RSA keys (you provided: %db) can be "
277 		    "factored cheaply.  Please generate a larger key.",
278 		    RSA_size(pubkey) * 8);
279 
280 	kdap->kda_encryptedkeysize = RSA_size(pubkey);
281 	if (kdap->kda_encryptedkeysize > KERNELDUMP_ENCKEY_MAX_SIZE) {
282 		errx(1, "Public key has to be at most %db long.",
283 		    8 * KERNELDUMP_ENCKEY_MAX_SIZE);
284 	}
285 
286 	kdap->kda_encryptedkey = calloc(1, kdap->kda_encryptedkeysize);
287 	if (kdap->kda_encryptedkey == NULL)
288 		err(1, "Unable to allocate encrypted key");
289 
290 	/*
291 	 * If no cipher was specified, choose a reasonable default.
292 	 */
293 	if (kdap->kda_encryption == KERNELDUMP_ENC_NONE)
294 		kdap->kda_encryption = KERNELDUMP_ENC_CHACHA20;
295 	else if (kdap->kda_encryption == KERNELDUMP_ENC_AES_256_CBC &&
296 	    kdap->kda_compression != KERNELDUMP_COMP_NONE)
297 		errx(EX_USAGE, "Unpadded AES256-CBC mode cannot be used "
298 		    "with compression.");
299 
300 	arc4random_buf(kdap->kda_key, sizeof(kdap->kda_key));
301 	if (RSA_public_encrypt(sizeof(kdap->kda_key), kdap->kda_key,
302 	    kdap->kda_encryptedkey, pubkey,
303 	    RSA_PKCS1_OAEP_PADDING) != (int)kdap->kda_encryptedkeysize) {
304 		errx(1, "Unable to encrypt the one-time key: %s",
305 		    ERR_error_string(ERR_get_error(), NULL));
306 	}
307 	RSA_free(pubkey);
308 }
309 
310 /*
311  * Run genkey() in a child so it can use capability mode without affecting
312  * the rest of the runtime.
313  */
314 static void
315 genkey(const char *pubkeyfile, struct diocskerneldump_arg *kdap)
316 {
317 	pid_t pid;
318 	int error, filedes[2], status;
319 	ssize_t bytes;
320 
321 	if (pipe2(filedes, O_CLOEXEC) != 0)
322 		err(1, "pipe");
323 	pid = fork();
324 	switch (pid) {
325 	case -1:
326 		err(1, "fork");
327 		break;
328 	case 0:
329 		close(filedes[0]);
330 		_genkey(pubkeyfile, kdap);
331 		/* Write the new kdap back to the parent. */
332 		bytes = write(filedes[1], kdap, sizeof(*kdap));
333 		if (bytes != sizeof(*kdap))
334 			err(1, "genkey pipe write");
335 		bytes = write(filedes[1], kdap->kda_encryptedkey,
336 		    kdap->kda_encryptedkeysize);
337 		if (bytes != (ssize_t)kdap->kda_encryptedkeysize)
338 			err(1, "genkey pipe write kda_encryptedkey");
339 		_exit(0);
340 	}
341 	close(filedes[1]);
342 	/* Read in the child's genkey() result into kdap. */
343 	bytes = read(filedes[0], kdap, sizeof(*kdap));
344 	if (bytes != sizeof(*kdap))
345 		errx(1, "genkey pipe read");
346 	if (kdap->kda_encryptedkeysize > KERNELDUMP_ENCKEY_MAX_SIZE)
347 		errx(1, "Public key has to be at most %db long.",
348 		    8 * KERNELDUMP_ENCKEY_MAX_SIZE);
349 	kdap->kda_encryptedkey = calloc(1, kdap->kda_encryptedkeysize);
350 	if (kdap->kda_encryptedkey == NULL)
351 		err(1, "Unable to allocate encrypted key");
352 	bytes = read(filedes[0], kdap->kda_encryptedkey,
353 	    kdap->kda_encryptedkeysize);
354 	if (bytes != (ssize_t)kdap->kda_encryptedkeysize)
355 		errx(1, "genkey pipe read kda_encryptedkey");
356 	error = waitpid(pid, &status, WEXITED);
357 	if (error == -1)
358 		err(1, "waitpid");
359 	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
360 		errx(1, "genkey child exited with status %d",
361 		    WEXITSTATUS(status));
362 	else if (WIFSIGNALED(status))
363 		errx(1, "genkey child exited with signal %d",
364 		    WTERMSIG(status));
365 	close(filedes[0]);
366 }
367 #endif
368 
369 static void
370 listdumpdev(void)
371 {
372 	static char ip[200];
373 
374 	char dumpdev[PATH_MAX];
375 	struct diocskerneldump_arg ndconf;
376 	size_t len;
377 	const char *sysctlname = "kern.shutdown.dumpdevname";
378 	int fd;
379 
380 	len = sizeof(dumpdev);
381 	if (sysctlbyname(sysctlname, &dumpdev, &len, NULL, 0) != 0) {
382 		if (errno == ENOMEM) {
383 			err(EX_OSERR, "Kernel returned too large of a buffer for '%s'\n",
384 				sysctlname);
385 		} else {
386 			err(EX_OSERR, "Sysctl get '%s'\n", sysctlname);
387 		}
388 	}
389 	if (strlen(dumpdev) == 0)
390 		(void)strlcpy(dumpdev, _PATH_DEVNULL, sizeof(dumpdev));
391 
392 	if (verbose) {
393 		char *ctx, *dd;
394 		unsigned idx;
395 
396 		printf("kernel dumps on priority: device\n");
397 		idx = 0;
398 		ctx = dumpdev;
399 		while ((dd = strsep(&ctx, ",")) != NULL)
400 			printf("%u: %s\n", idx++, dd);
401 	} else
402 		printf("%s\n", dumpdev);
403 
404 	/* If netdump is enabled, print the configuration parameters. */
405 	if (verbose) {
406 		fd = open(_PATH_NETDUMP, O_RDONLY);
407 		if (fd < 0) {
408 			if (errno != ENOENT)
409 				err(EX_OSERR, "opening %s", _PATH_NETDUMP);
410 			return;
411 		}
412 		if (ioctl(fd, DIOCGKERNELDUMP, &ndconf) != 0) {
413 			if (errno != ENXIO)
414 				err(EX_OSERR, "ioctl(DIOCGKERNELDUMP)");
415 			(void)close(fd);
416 			return;
417 		}
418 
419 		printf("server address: %s\n",
420 		    inet_ntop(ndconf.kda_af, &ndconf.kda_server, ip,
421 			sizeof(ip)));
422 		printf("client address: %s\n",
423 		    inet_ntop(ndconf.kda_af, &ndconf.kda_client, ip,
424 			sizeof(ip)));
425 		printf("gateway address: %s\n",
426 		    inet_ntop(ndconf.kda_af, &ndconf.kda_gateway, ip,
427 			sizeof(ip)));
428 		(void)close(fd);
429 	}
430 }
431 
432 static int
433 opendumpdev(const char *arg, char *dumpdev)
434 {
435 	int fd, i;
436 
437 	if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
438 		strlcpy(dumpdev, arg, PATH_MAX);
439 	else {
440 		i = snprintf(dumpdev, PATH_MAX, "%s%s", _PATH_DEV, arg);
441 		if (i < 0)
442 			err(EX_OSERR, "%s", arg);
443 		if (i >= PATH_MAX)
444 			errc(EX_DATAERR, EINVAL, "%s", arg);
445 	}
446 
447 	fd = open(dumpdev, O_RDONLY);
448 	if (fd < 0)
449 		err(EX_OSFILE, "%s", dumpdev);
450 	return (fd);
451 }
452 
453 int
454 main(int argc, char *argv[])
455 {
456 	char dumpdev[PATH_MAX];
457 	struct diocskerneldump_arg ndconf, *kdap;
458 	struct addrinfo hints, *res;
459 	const char *dev, *pubkeyfile, *server, *client, *gateway;
460 	int ch, error, fd, cipher;
461 	bool gzip, list, netdump, zstd, insert, rflag;
462 	uint8_t ins_idx;
463 
464 	gzip = list = netdump = zstd = insert = rflag = false;
465 	kdap = NULL;
466 	pubkeyfile = NULL;
467 	server = client = gateway = NULL;
468 	ins_idx = KDA_APPEND;
469 	cipher = KERNELDUMP_ENC_NONE;
470 
471 	while ((ch = getopt(argc, argv, "C:c:g:i:k:lrs:vZz")) != -1)
472 		switch ((char)ch) {
473 		case 'C':
474 			if (strcasecmp(optarg, "chacha") == 0 ||
475 			    strcasecmp(optarg, "chacha20") == 0)
476 				cipher = KERNELDUMP_ENC_CHACHA20;
477 			else if (strcasecmp(optarg, "aes-cbc") == 0 ||
478 			    strcasecmp(optarg, "aes256-cbc") == 0)
479 				cipher = KERNELDUMP_ENC_AES_256_CBC;
480 			else
481 				errx(EX_USAGE, "Unrecognized cipher algorithm "
482 				    "'%s'", optarg);
483 			break;
484 		case 'c':
485 			client = optarg;
486 			break;
487 		case 'g':
488 			gateway = optarg;
489 			break;
490 		case 'i':
491 			{
492 			int i;
493 
494 			i = atoi(optarg);
495 			if (i < 0 || i >= KDA_APPEND - 1)
496 				errx(EX_USAGE,
497 				    "-i index must be between zero and %d.",
498 				    (int)KDA_APPEND - 2);
499 			insert = true;
500 			ins_idx = i;
501 			}
502 			break;
503 		case 'k':
504 			pubkeyfile = optarg;
505 			break;
506 		case 'l':
507 			list = true;
508 			break;
509 		case 'r':
510 			rflag = true;
511 			break;
512 		case 's':
513 			server = optarg;
514 			break;
515 		case 'v':
516 			verbose = 1;
517 			break;
518 		case 'Z':
519 			zstd = true;
520 			break;
521 		case 'z':
522 			gzip = true;
523 			break;
524 		default:
525 			usage();
526 		}
527 
528 	if (gzip && zstd)
529 		errx(EX_USAGE, "The -z and -Z options are mutually exclusive.");
530 
531 	if (insert && rflag)
532 		errx(EX_USAGE, "The -i and -r options are mutually exclusive.");
533 
534 	argc -= optind;
535 	argv += optind;
536 
537 	if (list) {
538 		listdumpdev();
539 		exit(EX_OK);
540 	}
541 
542 	if (argc != 1)
543 		usage();
544 
545 #ifdef HAVE_CRYPTO
546 	if (cipher != KERNELDUMP_ENC_NONE && pubkeyfile == NULL) {
547 		errx(EX_USAGE, "-C option requires a public key file.");
548 	} else if (pubkeyfile != NULL) {
549 		ERR_load_crypto_strings();
550 	}
551 #else
552 	if (pubkeyfile != NULL)
553 		errx(EX_UNAVAILABLE,"Unable to use the public key."
554 				    " Recompile dumpon with OpenSSL support.");
555 #endif
556 
557 	if (server != NULL && client != NULL) {
558 		dev = _PATH_NETDUMP;
559 		netdump = true;
560 	} else if (server == NULL && client == NULL && argc > 0) {
561 		if (strcmp(argv[0], "off") == 0) {
562 			rflag = true;
563 			dev = _PATH_DEVNULL;
564 		} else
565 			dev = argv[0];
566 		netdump = false;
567 
568 		if (strcmp(dev, _PATH_DEVNULL) == 0) {
569 			/*
570 			 * Netdump has its own configuration tracking that
571 			 * is not removed when using /dev/null.
572 			 */
573 			fd = open(_PATH_NETDUMP, O_RDONLY);
574 			if (fd != -1) {
575 				bzero(&ndconf, sizeof(ndconf));
576 				ndconf.kda_index = KDA_REMOVE_ALL;
577 				ndconf.kda_af = AF_INET;
578 				error = ioctl(fd, DIOCSKERNELDUMP, &ndconf);
579 				if (error != 0)
580 					err(1, "ioctl(%s, DIOCSKERNELDUMP)",
581 					    _PATH_NETDUMP);
582 				close(fd);
583 			}
584 		}
585 	} else
586 		usage();
587 
588 	fd = opendumpdev(dev, dumpdev);
589 	if (!netdump && !gzip && !zstd && !rflag)
590 		check_size(fd, dumpdev);
591 
592 	kdap = &ndconf;
593 	bzero(kdap, sizeof(*kdap));
594 
595 	if (rflag)
596 		kdap->kda_index = KDA_REMOVE;
597 	else
598 		kdap->kda_index = ins_idx;
599 
600 	kdap->kda_compression = KERNELDUMP_COMP_NONE;
601 	if (zstd)
602 		kdap->kda_compression = KERNELDUMP_COMP_ZSTD;
603 	else if (gzip)
604 		kdap->kda_compression = KERNELDUMP_COMP_GZIP;
605 
606 	if (netdump) {
607 		memset(&hints, 0, sizeof(hints));
608 		hints.ai_family = AF_INET;
609 		hints.ai_protocol = IPPROTO_UDP;
610 		res = NULL;
611 		error = getaddrinfo(server, NULL, &hints, &res);
612 		if (error != 0) {
613 			if (error == EAI_SYSTEM)
614 				err(EX_OSERR, "%s", gai_strerror(error));
615 			errx(EX_NOHOST, "%s", gai_strerror(error));
616 		}
617 		server = inet_ntoa(
618 		    ((struct sockaddr_in *)(void *)res->ai_addr)->sin_addr);
619 		freeaddrinfo(res);
620 
621 		if (strlcpy(ndconf.kda_iface, argv[0],
622 		    sizeof(ndconf.kda_iface)) >= sizeof(ndconf.kda_iface))
623 			errx(EX_USAGE, "invalid interface name '%s'", argv[0]);
624 		if (inet_aton(server, &ndconf.kda_server.in4) == 0)
625 			errx(EX_USAGE, "invalid server address '%s'", server);
626 		if (inet_aton(client, &ndconf.kda_client.in4) == 0)
627 			errx(EX_USAGE, "invalid client address '%s'", client);
628 
629 		if (gateway == NULL) {
630 			gateway = find_gateway(argv[0]);
631 			if (gateway == NULL) {
632 				if (verbose)
633 					printf(
634 				    "failed to look up gateway for %s\n",
635 					    server);
636 				gateway = server;
637 			}
638 		}
639 		if (inet_aton(gateway, &ndconf.kda_gateway.in4) == 0)
640 			errx(EX_USAGE, "invalid gateway address '%s'", gateway);
641 		ndconf.kda_af = AF_INET;
642 	}
643 
644 #ifdef HAVE_CRYPTO
645 	if (pubkeyfile != NULL) {
646 		kdap->kda_encryption = cipher;
647 		genkey(pubkeyfile, kdap);
648 	}
649 #endif
650 	error = ioctl(fd, DIOCSKERNELDUMP, kdap);
651 	if (error != 0)
652 		error = errno;
653 	if (error == EINVAL && (gzip || zstd)) {
654 		/* Retry without compression in case kernel lacks support. */
655 		kdap->kda_compression = KERNELDUMP_COMP_NONE;
656 		error = ioctl(fd, DIOCSKERNELDUMP, kdap);
657 		if (error == 0)
658 			warnx("Compression disabled; kernel may lack gzip or zstd support.");
659 		else
660 			error = errno;
661 	}
662 	explicit_bzero(kdap->kda_encryptedkey, kdap->kda_encryptedkeysize);
663 	free(kdap->kda_encryptedkey);
664 	explicit_bzero(kdap, sizeof(*kdap));
665 	if (error != 0) {
666 		if (netdump) {
667 			/*
668 			 * Be slightly less user-hostile for some common
669 			 * errors, especially as users don't have any great
670 			 * discoverability into which NICs support netdump.
671 			 */
672 			if (error == ENXIO)
673 				errx(EX_OSERR, "Unable to configure netdump "
674 				    "because the interface's link is down.");
675 			else if (error == ENODEV)
676 				errx(EX_OSERR, "Unable to configure netdump "
677 				    "because the interface driver does not yet "
678 				    "support netdump.");
679 		}
680 		errc(EX_OSERR, error, "ioctl(DIOCSKERNELDUMP)");
681 	}
682 
683 	if (verbose)
684 		listdumpdev();
685 
686 	exit(EX_OK);
687 }
688