Print this page
First attempt at further IPsec cluster cleanup
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/lib/libipsecutil/common/ipsec_util.c
+++ new/usr/src/lib/libipsecutil/common/ipsec_util.c
1 1 /*
2 2 *
3 3 * CDDL HEADER START
4 4 *
5 5 * The contents of this file are subject to the terms of the
6 6 * Common Development and Distribution License (the "License").
7 7 * You may not use this file except in compliance with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 * If applicable, add the following below this CDDL HEADER, with the
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
21 21 */
22 22 /*
23 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 * Copyright 2012 Milan Juri. All rights reserved.
26 26 */
27 27
28 28 #include <unistd.h>
29 29 #include <stdio.h>
30 30 #include <stdlib.h>
31 31 #include <stdarg.h>
32 32 #include <sys/types.h>
33 33 #include <sys/stat.h>
34 34 #include <fcntl.h>
35 35 #include <sys/sysconf.h>
36 36 #include <strings.h>
37 37 #include <ctype.h>
38 38 #include <errno.h>
39 39 #include <sys/socket.h>
40 40 #include <netdb.h>
41 41 #include <netinet/in.h>
42 42 #include <arpa/inet.h>
43 43 #include <net/pfkeyv2.h>
44 44 #include <net/pfpolicy.h>
45 45 #include <libintl.h>
46 46 #include <setjmp.h>
47 47 #include <libgen.h>
48 48 #include <libscf.h>
49 49
50 50 #include "ipsec_util.h"
51 51 #include "ikedoor.h"
52 52
53 53 /*
54 54 * This file contains support functions that are shared by the ipsec
55 55 * utilities and daemons including ipseckey(1m), ikeadm(1m) and in.iked(1m).
56 56 */
57 57
58 58
59 59 #define EFD(file) (((file) == stdout) ? stderr : (file))
60 60
61 61 /* Limits for interactive mode. */
62 62 #define MAX_LINE_LEN IBUF_SIZE
63 63 #define MAX_CMD_HIST 64000 /* in bytes */
64 64
65 65 /* Set standard default/initial values for globals... */
66 66 boolean_t pflag = B_FALSE; /* paranoid w.r.t. printing keying material */
67 67 boolean_t nflag = B_FALSE; /* avoid nameservice? */
68 68 boolean_t interactive = B_FALSE; /* util not running on cmdline */
69 69 boolean_t readfile = B_FALSE; /* cmds are being read from a file */
70 70 uint_t lineno = 0; /* track location if reading cmds from file */
71 71 uint_t lines_added = 0;
72 72 uint_t lines_parsed = 0;
73 73 jmp_buf env; /* for error recovery in interactive/readfile modes */
74 74 char *my_fmri = NULL;
75 75 FILE *debugfile = stderr;
76 76 static GetLine *gl = NULL; /* for interactive mode */
77 77
78 78 /*
79 79 * Print errno and exit if cmdline or readfile, reset state if interactive
80 80 * The error string *what should be dgettext()'d before calling bail().
81 81 */
82 82 void
83 83 bail(char *what)
84 84 {
85 85 if (errno != 0)
86 86 warn(what);
87 87 else
88 88 warnx(dgettext(TEXT_DOMAIN, "Error: %s"), what);
89 89 if (readfile) {
90 90 return;
91 91 }
92 92 if (interactive && !readfile)
93 93 longjmp(env, 2);
94 94 EXIT_FATAL(NULL);
95 95 }
96 96
97 97 /*
98 98 * Print caller-supplied variable-arg error msg, then exit if cmdline or
99 99 * readfile, or reset state if interactive.
100 100 */
101 101 /*PRINTFLIKE1*/
102 102 void
103 103 bail_msg(char *fmt, ...)
104 104 {
105 105 va_list ap;
106 106 char msgbuf[BUFSIZ];
107 107
108 108 va_start(ap, fmt);
109 109 (void) vsnprintf(msgbuf, BUFSIZ, fmt, ap);
110 110 va_end(ap);
111 111 if (readfile)
112 112 warnx(dgettext(TEXT_DOMAIN,
113 113 "ERROR on line %u:\n%s\n"), lineno, msgbuf);
114 114 else
115 115 warnx(dgettext(TEXT_DOMAIN, "ERROR: %s\n"), msgbuf);
116 116
117 117 if (interactive && !readfile)
118 118 longjmp(env, 1);
119 119
120 120 EXIT_FATAL(NULL);
121 121 }
122 122
123 123 /*
124 124 * bytecnt2str() wrapper. Zeroes out the input buffer and if the number
125 125 * of bytes to be converted is more than 1K, it will produce readable string
126 126 * in parentheses, store it in the original buffer and return the pointer to it.
127 127 * Maximum length of the returned string is 14 characters (not including
128 128 * the terminating zero).
129 129 */
130 130 char *
131 131 bytecnt2out(uint64_t num, char *buf, size_t bufsiz, int flags)
132 132 {
133 133 char *str;
134 134
135 135 (void) memset(buf, '\0', bufsiz);
136 136
137 137 if (num > 1024) {
138 138 /* Return empty string in case of out-of-memory. */
139 139 if ((str = malloc(bufsiz)) == NULL)
140 140 return (buf);
141 141
142 142 (void) bytecnt2str(num, str, bufsiz);
143 143 /* Detect overflow. */
144 144 if (strlen(str) == 0) {
145 145 free(str);
146 146 return (buf);
147 147 }
148 148
149 149 /* Emit nothing in case of overflow. */
150 150 if (snprintf(buf, bufsiz, "%s(%sB)%s",
151 151 flags & SPC_BEGIN ? " " : "", str,
152 152 flags & SPC_END ? " " : "") >= bufsiz)
153 153 (void) memset(buf, '\0', bufsiz);
154 154
155 155 free(str);
156 156 }
157 157
158 158 return (buf);
159 159 }
160 160
161 161 /*
162 162 * Convert 64-bit number to human readable string. Useful mainly for the
163 163 * byte lifetime counters. Returns pointer to the user supplied buffer.
164 164 * Able to convert up to Exabytes. Maximum length of the string produced
165 165 * is 9 characters (not counting the terminating zero).
166 166 */
167 167 char *
168 168 bytecnt2str(uint64_t num, char *buf, size_t buflen)
169 169 {
170 170 uint64_t n = num;
171 171 char u;
172 172 int index = 0;
173 173
174 174 while (n >= 1024) {
175 175 n /= 1024;
176 176 index++;
177 177 }
178 178
179 179 /* The field has all units this function can represent. */
180 180 u = " KMGTPE"[index];
181 181
182 182 if (index == 0) {
183 183 /* Less than 1K */
184 184 if (snprintf(buf, buflen, "%llu ", num) >= buflen)
185 185 (void) memset(buf, '\0', buflen);
186 186 } else {
187 187 /* Otherwise display 2 precision digits. */
188 188 if (snprintf(buf, buflen, "%.2f %c",
189 189 (double)num / (1ULL << index * 10), u) >= buflen)
190 190 (void) memset(buf, '\0', buflen);
191 191 }
192 192
193 193 return (buf);
194 194 }
195 195
196 196 /*
197 197 * secs2str() wrapper. Zeroes out the input buffer and if the number of
198 198 * seconds to be converted is more than minute, it will produce readable
199 199 * string in parentheses, store it in the original buffer and return the
200 200 * pointer to it.
201 201 */
202 202 char *
203 203 secs2out(unsigned int secs, char *buf, int bufsiz, int flags)
204 204 {
205 205 char *str;
206 206
207 207 (void) memset(buf, '\0', bufsiz);
208 208
209 209 if (secs > 60) {
210 210 /* Return empty string in case of out-of-memory. */
211 211 if ((str = malloc(bufsiz)) == NULL)
212 212 return (buf);
213 213
214 214 (void) secs2str(secs, str, bufsiz);
215 215 /* Detect overflow. */
216 216 if (strlen(str) == 0) {
217 217 free(str);
218 218 return (buf);
219 219 }
220 220
221 221 /* Emit nothing in case of overflow. */
222 222 if (snprintf(buf, bufsiz, "%s(%s)%s",
223 223 flags & SPC_BEGIN ? " " : "", str,
224 224 flags & SPC_END ? " " : "") >= bufsiz)
225 225 (void) memset(buf, '\0', bufsiz);
226 226
227 227 free(str);
228 228 }
229 229
230 230 return (buf);
231 231 }
232 232
233 233 /*
234 234 * Convert number of seconds to human readable string. Useful mainly for
235 235 * the lifetime counters. Returns pointer to the user supplied buffer.
236 236 * Able to convert up to days.
237 237 */
238 238 char *
239 239 secs2str(unsigned int secs, char *buf, int bufsiz)
240 240 {
241 241 double val = secs;
242 242 char *unit = "second";
243 243
244 244 if (val >= 24*60*60) {
245 245 val /= 86400;
246 246 unit = "day";
247 247 } else if (val >= 60*60) {
248 248 val /= 60*60;
249 249 unit = "hour";
250 250 } else if (val >= 60) {
251 251 val /= 60;
252 252 unit = "minute";
253 253 }
254 254
255 255 /* Emit nothing in case of overflow. */
256 256 if (snprintf(buf, bufsiz, "%.2f %s%s", val, unit,
257 257 val >= 2 ? "s" : "") >= bufsiz)
258 258 (void) memset(buf, '\0', bufsiz);
259 259
260 260 return (buf);
261 261 }
262 262
263 263 /*
264 264 * dump_XXX functions produce ASCII output from various structures.
265 265 *
266 266 * Because certain errors need to do this to stderr, dump_XXX functions
267 267 * take a FILE pointer.
268 268 *
269 269 * If an error occured while writing to the specified file, these
270 270 * functions return -1, zero otherwise.
271 271 */
272 272
273 273 int
274 274 dump_sockaddr(struct sockaddr *sa, uint8_t prefixlen, boolean_t addr_only,
275 275 FILE *where, boolean_t ignore_nss)
276 276 {
277 277 struct sockaddr_in *sin;
278 278 struct sockaddr_in6 *sin6;
279 279 char *printable_addr, *protocol;
280 280 uint8_t *addrptr;
281 281 /* Add 4 chars to hold '/nnn' for prefixes. */
282 282 char storage[INET6_ADDRSTRLEN + 4];
283 283 uint16_t port;
284 284 boolean_t unspec;
285 285 struct hostent *hp;
286 286 int getipnode_errno, addrlen;
287 287
288 288 switch (sa->sa_family) {
289 289 case AF_INET:
290 290 /* LINTED E_BAD_PTR_CAST_ALIGN */
291 291 sin = (struct sockaddr_in *)sa;
292 292 addrptr = (uint8_t *)&sin->sin_addr;
293 293 port = sin->sin_port;
294 294 protocol = "AF_INET";
295 295 unspec = (sin->sin_addr.s_addr == 0);
296 296 addrlen = sizeof (sin->sin_addr);
297 297 break;
298 298 case AF_INET6:
299 299 /* LINTED E_BAD_PTR_CAST_ALIGN */
300 300 sin6 = (struct sockaddr_in6 *)sa;
301 301 addrptr = (uint8_t *)&sin6->sin6_addr;
302 302 port = sin6->sin6_port;
303 303 protocol = "AF_INET6";
304 304 unspec = IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr);
305 305 addrlen = sizeof (sin6->sin6_addr);
306 306 break;
307 307 default:
308 308 return (0);
309 309 }
310 310
311 311 if (inet_ntop(sa->sa_family, addrptr, storage, INET6_ADDRSTRLEN) ==
312 312 NULL) {
313 313 printable_addr = dgettext(TEXT_DOMAIN, "Invalid IP address.");
314 314 } else {
315 315 char prefix[5]; /* "/nnn" with terminator. */
316 316
317 317 (void) snprintf(prefix, sizeof (prefix), "/%d", prefixlen);
318 318 printable_addr = storage;
319 319 if (prefixlen != 0) {
320 320 (void) strlcat(printable_addr, prefix,
321 321 sizeof (storage));
322 322 }
323 323 }
324 324 if (addr_only) {
325 325 if (fprintf(where, "%s", printable_addr) < 0)
326 326 return (-1);
327 327 } else {
328 328 if (fprintf(where, dgettext(TEXT_DOMAIN,
329 329 "%s: port %d, %s"), protocol,
330 330 ntohs(port), printable_addr) < 0)
331 331 return (-1);
332 332 if (ignore_nss == B_FALSE) {
333 333 /*
334 334 * Do AF_independent reverse hostname lookup here.
335 335 */
336 336 if (unspec) {
337 337 if (fprintf(where,
338 338 dgettext(TEXT_DOMAIN,
339 339 " <unspecified>")) < 0)
340 340 return (-1);
341 341 } else {
342 342 hp = getipnodebyaddr((char *)addrptr, addrlen,
343 343 sa->sa_family, &getipnode_errno);
344 344 if (hp != NULL) {
345 345 if (fprintf(where,
346 346 " (%s)", hp->h_name) < 0)
347 347 return (-1);
348 348 freehostent(hp);
349 349 } else {
350 350 if (fprintf(where,
351 351 dgettext(TEXT_DOMAIN,
352 352 " <unknown>")) < 0)
353 353 return (-1);
354 354 }
355 355 }
356 356 }
357 357 if (fputs(".\n", where) == EOF)
358 358 return (-1);
359 359 }
360 360 return (0);
361 361 }
362 362
363 363 /*
364 364 * Dump a key, any salt and bitlen.
365 365 * The key is made up of a stream of bits. If the algorithm requires a salt
366 366 * value, this will also be part of the dumped key. The last "saltbits" of the
367 367 * key string, reading left to right will be the salt value. To make it easier
368 368 * to see which bits make up the key, the salt value is enclosed in []'s.
369 369 * This function can also be called when ipseckey(1m) -s is run, this "saves"
370 370 * the SAs, including the key to a file. When this is the case, the []'s are
371 371 * not printed.
372 372 *
373 373 * The implementation allows the kernel to be told about the length of the salt
374 374 * in whole bytes only. If this changes, this function will need to be updated.
375 375 */
376 376 int
377 377 dump_key(uint8_t *keyp, uint_t bitlen, uint_t saltbits, FILE *where,
378 378 boolean_t separate_salt)
379 379 {
380 380 int numbytes, saltbytes;
381 381
382 382 numbytes = SADB_1TO8(bitlen);
383 383 saltbytes = SADB_1TO8(saltbits);
384 384 numbytes += saltbytes;
385 385
386 386 /* The & 0x7 is to check for leftover bits. */
387 387 if ((bitlen & 0x7) != 0)
388 388 numbytes++;
389 389
390 390 while (numbytes-- != 0) {
391 391 if (pflag) {
392 392 /* Print no keys if paranoid */
393 393 if (fprintf(where, "XX") < 0)
394 394 return (-1);
395 395 } else {
396 396 if (fprintf(where, "%02x", *keyp++) < 0)
397 397 return (-1);
398 398 }
399 399 if (separate_salt && saltbytes != 0 &&
400 400 numbytes == saltbytes) {
401 401 if (fprintf(where, "[") < 0)
402 402 return (-1);
403 403 }
404 404 }
405 405
406 406 if (separate_salt && saltbits != 0) {
407 407 if (fprintf(where, "]/%u+%u", bitlen, saltbits) < 0)
408 408 return (-1);
409 409 } else {
410 410 if (fprintf(where, "/%u", bitlen + saltbits) < 0)
411 411 return (-1);
412 412 }
413 413
414 414 return (0);
415 415 }
416 416
417 417 /*
418 418 * Print an authentication or encryption algorithm
419 419 */
420 420 static int
421 421 dump_generic_alg(uint8_t alg_num, int proto_num, FILE *where)
422 422 {
423 423 struct ipsecalgent *alg;
424 424
425 425 alg = getipsecalgbynum(alg_num, proto_num, NULL);
426 426 if (alg == NULL) {
427 427 if (fprintf(where, dgettext(TEXT_DOMAIN,
428 428 "<unknown %u>"), alg_num) < 0)
429 429 return (-1);
430 430 return (0);
431 431 }
432 432
433 433 /*
434 434 * Special-case <none> for backward output compat.
435 435 * Assume that SADB_AALG_NONE == SADB_EALG_NONE.
436 436 */
437 437 if (alg_num == SADB_AALG_NONE) {
438 438 if (fputs(dgettext(TEXT_DOMAIN,
439 439 "<none>"), where) == EOF)
440 440 return (-1);
441 441 } else {
442 442 if (fputs(alg->a_names[0], where) == EOF)
443 443 return (-1);
444 444 }
445 445
446 446 freeipsecalgent(alg);
447 447 return (0);
448 448 }
449 449
450 450 int
451 451 dump_aalg(uint8_t aalg, FILE *where)
452 452 {
453 453 return (dump_generic_alg(aalg, IPSEC_PROTO_AH, where));
454 454 }
455 455
456 456 int
457 457 dump_ealg(uint8_t ealg, FILE *where)
458 458 {
459 459 return (dump_generic_alg(ealg, IPSEC_PROTO_ESP, where));
460 460 }
461 461
462 462 /*
463 463 * Print an SADB_IDENTTYPE string
464 464 *
465 465 * Also return TRUE if the actual ident may be printed, FALSE if not.
466 466 *
467 467 * If rc is not NULL, set its value to -1 if an error occured while writing
468 468 * to the specified file, zero otherwise.
469 469 */
470 470 boolean_t
471 471 dump_sadb_idtype(uint8_t idtype, FILE *where, int *rc)
472 472 {
473 473 boolean_t canprint = B_TRUE;
474 474 int rc_val = 0;
475 475
476 476 switch (idtype) {
477 477 case SADB_IDENTTYPE_PREFIX:
478 478 if (fputs(dgettext(TEXT_DOMAIN, "prefix"), where) == EOF)
479 479 rc_val = -1;
480 480 break;
481 481 case SADB_IDENTTYPE_FQDN:
482 482 if (fputs(dgettext(TEXT_DOMAIN, "FQDN"), where) == EOF)
483 483 rc_val = -1;
484 484 break;
485 485 case SADB_IDENTTYPE_USER_FQDN:
486 486 if (fputs(dgettext(TEXT_DOMAIN,
487 487 "user-FQDN (mbox)"), where) == EOF)
488 488 rc_val = -1;
489 489 break;
490 490 case SADB_X_IDENTTYPE_DN:
491 491 if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Distinguished Name"),
492 492 where) == EOF)
493 493 rc_val = -1;
494 494 canprint = B_FALSE;
495 495 break;
496 496 case SADB_X_IDENTTYPE_GN:
497 497 if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Generic Name"),
498 498 where) == EOF)
499 499 rc_val = -1;
500 500 canprint = B_FALSE;
501 501 break;
502 502 case SADB_X_IDENTTYPE_KEY_ID:
503 503 if (fputs(dgettext(TEXT_DOMAIN, "Generic key id"),
504 504 where) == EOF)
505 505 rc_val = -1;
506 506 break;
507 507 case SADB_X_IDENTTYPE_ADDR_RANGE:
508 508 if (fputs(dgettext(TEXT_DOMAIN, "Address range"), where) == EOF)
509 509 rc_val = -1;
510 510 break;
511 511 default:
512 512 if (fprintf(where, dgettext(TEXT_DOMAIN,
513 513 "<unknown %u>"), idtype) < 0)
514 514 rc_val = -1;
515 515 break;
516 516 }
517 517
518 518 if (rc != NULL)
519 519 *rc = rc_val;
520 520
521 521 return (canprint);
522 522 }
523 523
524 524 /*
525 525 * Slice an argv/argc vector from an interactive line or a read-file line.
526 526 */
527 527 static int
528 528 create_argv(char *ibuf, int *newargc, char ***thisargv)
529 529 {
530 530 unsigned int argvlen = START_ARG;
531 531 char **current;
532 532 boolean_t firstchar = B_TRUE;
533 533 boolean_t inquotes = B_FALSE;
534 534
535 535 *thisargv = malloc(sizeof (char *) * argvlen);
536 536 if ((*thisargv) == NULL)
537 537 return (MEMORY_ALLOCATION);
538 538 current = *thisargv;
539 539 *current = NULL;
540 540
541 541 for (; *ibuf != '\0'; ibuf++) {
542 542 if (isspace(*ibuf)) {
543 543 if (inquotes) {
544 544 continue;
545 545 }
546 546 if (*current != NULL) {
547 547 *ibuf = '\0';
548 548 current++;
549 549 if (*thisargv + argvlen == current) {
550 550 /* Regrow ***thisargv. */
551 551 if (argvlen == TOO_MANY_ARGS) {
552 552 free(*thisargv);
553 553 return (TOO_MANY_TOKENS);
554 554 }
555 555 /* Double the allocation. */
556 556 current = realloc(*thisargv,
557 557 sizeof (char *) * (argvlen << 1));
558 558 if (current == NULL) {
559 559 free(*thisargv);
560 560 return (MEMORY_ALLOCATION);
561 561 }
562 562 *thisargv = current;
563 563 current += argvlen;
564 564 argvlen <<= 1; /* Double the size. */
565 565 }
566 566 *current = NULL;
567 567 }
568 568 } else {
569 569 if (firstchar) {
570 570 firstchar = B_FALSE;
571 571 if (*ibuf == COMMENT_CHAR || *ibuf == '\n') {
572 572 free(*thisargv);
573 573 return (COMMENT_LINE);
574 574 }
575 575 }
576 576 if (*ibuf == QUOTE_CHAR) {
577 577 if (inquotes) {
578 578 inquotes = B_FALSE;
579 579 *ibuf = '\0';
580 580 } else {
581 581 inquotes = B_TRUE;
582 582 }
583 583 continue;
584 584 }
585 585 if (*current == NULL) {
586 586 *current = ibuf;
587 587 (*newargc)++;
588 588 }
589 589 }
590 590 }
591 591
592 592 /*
593 593 * Tricky corner case...
594 594 * I've parsed _exactly_ the amount of args as I have space. It
595 595 * won't return NULL-terminated, and bad things will happen to
596 596 * the caller.
597 597 */
598 598 if (argvlen == *newargc) {
599 599 current = realloc(*thisargv, sizeof (char *) * (argvlen + 1));
600 600 if (current == NULL) {
601 601 free(*thisargv);
602 602 return (MEMORY_ALLOCATION);
603 603 }
604 604 *thisargv = current;
605 605 current[argvlen] = NULL;
606 606 }
607 607
608 608 return (SUCCESS);
609 609 }
610 610
611 611 /*
612 612 * init interactive mode if needed and not yet initialized
613 613 */
614 614 static void
615 615 init_interactive(FILE *infile, CplMatchFn *match_fn)
616 616 {
617 617 if (infile == stdin) {
618 618 if (gl == NULL) {
619 619 if ((gl = new_GetLine(MAX_LINE_LEN,
620 620 MAX_CMD_HIST)) == NULL)
621 621 errx(1, dgettext(TEXT_DOMAIN,
622 622 "tecla initialization failed"));
623 623
624 624 if (gl_customize_completion(gl, NULL,
625 625 match_fn) != 0) {
626 626 (void) del_GetLine(gl);
627 627 errx(1, dgettext(TEXT_DOMAIN,
628 628 "tab completion failed to initialize"));
629 629 }
630 630
631 631 /*
632 632 * In interactive mode we only want to terminate
633 633 * when explicitly requested (e.g. by a command).
634 634 */
635 635 (void) sigset(SIGINT, SIG_IGN);
636 636 }
637 637 } else {
638 638 readfile = B_TRUE;
639 639 }
640 640 }
641 641
642 642 /*
643 643 * free tecla data structure
644 644 */
645 645 static void
646 646 fini_interactive(void)
647 647 {
648 648 if (gl != NULL)
649 649 (void) del_GetLine(gl);
650 650 }
651 651
652 652 /*
653 653 * Get single input line, wrapping around interactive and non-interactive
654 654 * mode.
655 655 */
656 656 static char *
657 657 do_getstr(FILE *infile, char *prompt, char *ibuf, size_t ibuf_size)
658 658 {
659 659 char *line;
660 660
661 661 if (infile != stdin)
662 662 return (fgets(ibuf, ibuf_size, infile));
663 663
664 664 /*
665 665 * If the user hits ^C then we want to catch it and
666 666 * start over. If the user hits EOF then we want to
667 667 * bail out.
668 668 */
669 669 once_again:
670 670 line = gl_get_line(gl, prompt, NULL, -1);
671 671 if (gl_return_status(gl) == GLR_SIGNAL) {
672 672 gl_abandon_line(gl);
673 673 goto once_again;
674 674 } else if (gl_return_status(gl) == GLR_ERROR) {
675 675 gl_abandon_line(gl);
676 676 errx(1, dgettext(TEXT_DOMAIN, "Error reading terminal: %s\n"),
677 677 gl_error_message(gl, NULL, 0));
678 678 } else {
679 679 if (line != NULL) {
680 680 if (strlcpy(ibuf, line, ibuf_size) >= ibuf_size)
681 681 warnx(dgettext(TEXT_DOMAIN,
682 682 "Line too long (max=%d chars)"),
683 683 ibuf_size);
684 684 line = ibuf;
685 685 }
686 686 }
687 687
688 688 return (line);
689 689 }
690 690
691 691 /*
692 692 * Enter a mode where commands are read from a file. Treat stdin special.
693 693 */
694 694 void
695 695 do_interactive(FILE *infile, char *configfile, char *promptstring,
696 696 char *my_fmri, parse_cmdln_fn parseit, CplMatchFn *match_fn)
697 697 {
698 698 char ibuf[IBUF_SIZE], holder[IBUF_SIZE];
699 699 char *hptr, **thisargv, *ebuf;
700 700 int thisargc;
701 701 boolean_t continue_in_progress = B_FALSE;
702 702 char *s;
703 703
704 704 (void) setjmp(env);
705 705
706 706 ebuf = NULL;
707 707 interactive = B_TRUE;
708 708 bzero(ibuf, IBUF_SIZE);
709 709
710 710 /* panics for us */
711 711 init_interactive(infile, match_fn);
712 712
713 713 while ((s = do_getstr(infile, promptstring, ibuf, IBUF_SIZE)) != NULL) {
714 714 if (readfile)
715 715 lineno++;
716 716 thisargc = 0;
717 717 thisargv = NULL;
718 718
719 719 /*
720 720 * Check byte IBUF_SIZE - 2, because byte IBUF_SIZE - 1 will
721 721 * be null-terminated because of fgets().
722 722 */
723 723 if (ibuf[IBUF_SIZE - 2] != '\0') {
724 724 if (infile == stdin) {
725 725 /* do_getstr() issued a warning already */
726 726 bzero(ibuf, IBUF_SIZE);
727 727 continue;
728 728 } else {
729 729 ipsecutil_exit(SERVICE_FATAL, my_fmri,
730 730 debugfile, dgettext(TEXT_DOMAIN,
731 731 "Line %d too big."), lineno);
732 732 }
733 733 }
734 734
735 735 if (!continue_in_progress) {
736 736 /* Use -2 because of \n from fgets. */
737 737 if (ibuf[strlen(ibuf) - 2] == CONT_CHAR) {
738 738 /*
739 739 * Can use strcpy here, I've checked the
740 740 * length already.
741 741 */
742 742 (void) strcpy(holder, ibuf);
743 743 hptr = &(holder[strlen(holder)]);
744 744
745 745 /* Remove the CONT_CHAR from the string. */
746 746 hptr[-2] = ' ';
747 747
748 748 continue_in_progress = B_TRUE;
749 749 bzero(ibuf, IBUF_SIZE);
750 750 continue;
751 751 }
752 752 } else {
753 753 /* Handle continuations... */
754 754 (void) strncpy(hptr, ibuf,
755 755 (size_t)(&(holder[IBUF_SIZE]) - hptr));
756 756 if (holder[IBUF_SIZE - 1] != '\0') {
757 757 ipsecutil_exit(SERVICE_FATAL, my_fmri,
758 758 debugfile, dgettext(TEXT_DOMAIN,
759 759 "Command buffer overrun."));
760 760 }
761 761 /* Use - 2 because of \n from fgets. */
762 762 if (hptr[strlen(hptr) - 2] == CONT_CHAR) {
763 763 bzero(ibuf, IBUF_SIZE);
764 764 hptr += strlen(hptr);
765 765
766 766 /* Remove the CONT_CHAR from the string. */
767 767 hptr[-2] = ' ';
768 768
769 769 continue;
770 770 } else {
771 771 continue_in_progress = B_FALSE;
772 772 /*
773 773 * I've already checked the length...
774 774 */
775 775 (void) strcpy(ibuf, holder);
776 776 }
777 777 }
778 778
779 779 /*
780 780 * Just in case the command fails keep a copy of the
781 781 * command buffer for diagnostic output.
782 782 */
783 783 if (readfile) {
784 784 /*
785 785 * The error buffer needs to be big enough to
786 786 * hold the longest command string, plus
787 787 * some extra text, see below.
788 788 */
789 789 ebuf = calloc((IBUF_SIZE * 2), sizeof (char));
790 790 if (ebuf == NULL) {
791 791 ipsecutil_exit(SERVICE_FATAL, my_fmri,
792 792 debugfile, dgettext(TEXT_DOMAIN,
793 793 "Memory allocation error."));
794 794 } else {
795 795 (void) snprintf(ebuf, (IBUF_SIZE * 2),
796 796 dgettext(TEXT_DOMAIN,
797 797 "Config file entry near line %u "
798 798 "caused error(s) or warnings:\n\n%s\n\n"),
799 799 lineno, ibuf);
800 800 }
801 801 }
802 802
803 803 switch (create_argv(ibuf, &thisargc, &thisargv)) {
804 804 case TOO_MANY_TOKENS:
805 805 ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
806 806 dgettext(TEXT_DOMAIN, "Too many input tokens."));
807 807 break;
808 808 case MEMORY_ALLOCATION:
809 809 ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
810 810 dgettext(TEXT_DOMAIN, "Memory allocation error."));
811 811 break;
812 812 case COMMENT_LINE:
813 813 /* Comment line. */
814 814 free(ebuf);
815 815 break;
816 816 default:
817 817 if (thisargc != 0) {
818 818 lines_parsed++;
819 819 /* ebuf consumed */
820 820 parseit(thisargc, thisargv, ebuf, readfile);
821 821 } else {
822 822 free(ebuf);
823 823 }
824 824 free(thisargv);
825 825 if (infile == stdin) {
826 826 (void) printf("%s", promptstring);
827 827 (void) fflush(stdout);
828 828 }
829 829 break;
830 830 }
831 831 bzero(ibuf, IBUF_SIZE);
832 832 }
833 833
834 834 /*
835 835 * The following code is ipseckey specific. This should never be
836 836 * used by ikeadm which also calls this function because ikeadm
837 837 * only runs interactively. If this ever changes this code block
838 838 * sould be revisited.
839 839 */
840 840 if (readfile) {
841 841 if (lines_parsed != 0 && lines_added == 0) {
842 842 ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
843 843 dgettext(TEXT_DOMAIN, "Configuration file did not "
844 844 "contain any valid SAs"));
845 845 }
846 846
847 847 /*
848 848 * There were errors. Putting the service in maintenance mode.
849 849 * When svc.startd(1M) allows services to degrade themselves,
850 850 * this should be revisited.
851 851 *
852 852 * If this function was called from a program running as a
853 853 * smf_method(5), print a warning message. Don't spew out the
854 854 * errors as these will end up in the smf(5) log file which is
855 855 * publically readable, the errors may contain sensitive
856 856 * information.
857 857 */
858 858 if ((lines_added < lines_parsed) && (configfile != NULL)) {
859 859 if (my_fmri != NULL) {
860 860 ipsecutil_exit(SERVICE_BADCONF, my_fmri,
861 861 debugfile, dgettext(TEXT_DOMAIN,
862 862 "The configuration file contained %d "
863 863 "errors.\n"
864 864 "Manually check the configuration with:\n"
865 865 "ipseckey -c %s\n"
866 866 "Use svcadm(1M) to clear maintenance "
867 867 "condition when errors are resolved.\n"),
868 868 lines_parsed - lines_added, configfile);
869 869 } else {
870 870 EXIT_BADCONFIG(NULL);
871 871 }
872 872 } else {
873 873 if (my_fmri != NULL)
874 874 ipsecutil_exit(SERVICE_EXIT_OK, my_fmri,
875 875 debugfile, dgettext(TEXT_DOMAIN,
876 876 "%d actions successfully processed."),
877 877 lines_added);
878 878 }
879 879 } else {
880 880 /* no newline upon Ctrl-D */
881 881 if (s != NULL)
882 882 (void) putchar('\n');
883 883 (void) fflush(stdout);
884 884 }
885 885
886 886 fini_interactive();
887 887
888 888 EXIT_OK(NULL);
889 889 }
890 890
891 891 /*
892 892 * Functions to parse strings that represent a debug or privilege level.
893 893 * These functions are copied from main.c and door.c in usr.lib/in.iked/common.
894 894 * If this file evolves into a common library that may be used by in.iked
895 895 * as well as the usr.sbin utilities, those duplicate functions should be
896 896 * deleted.
897 897 *
898 898 * A privilege level may be represented by a simple keyword, corresponding
899 899 * to one of the possible levels. A debug level may be represented by a
900 900 * series of keywords, separated by '+' or '-', indicating categories to
901 901 * be added or removed from the set of categories in the debug level.
902 902 * For example, +all-op corresponds to level 0xfffffffb (all flags except
903 903 * for D_OP set); while p1+p2+pfkey corresponds to level 0x38. Note that
904 904 * the leading '+' is implicit; the first keyword in the list must be for
905 905 * a category that is to be added.
906 906 *
907 907 * These parsing functions make use of a local version of strtok, strtok_d,
908 908 * which includes an additional parameter, char *delim. This param is filled
909 909 * in with the character which ends the returned token. In other words,
910 910 * this version of strtok, in addition to returning the token, also returns
911 911 * the single character delimiter from the original string which marked the
912 912 * end of the token.
913 913 */
914 914 static char *
915 915 strtok_d(char *string, const char *sepset, char *delim)
916 916 {
917 917 static char *lasts;
918 918 char *q, *r;
919 919
920 920 /* first or subsequent call */
921 921 if (string == NULL)
922 922 string = lasts;
923 923
924 924 if (string == 0) /* return if no tokens remaining */
925 925 return (NULL);
926 926
927 927 q = string + strspn(string, sepset); /* skip leading separators */
928 928
929 929 if (*q == '\0') /* return if no tokens remaining */
930 930 return (NULL);
931 931
932 932 if ((r = strpbrk(q, sepset)) == NULL) { /* move past token */
933 933 lasts = 0; /* indicate that this is last token */
934 934 } else {
935 935 *delim = *r; /* save delimitor */
936 936 *r = '\0';
937 937 lasts = r + 1;
938 938 }
939 939 return (q);
940 940 }
941 941
942 942 static keywdtab_t privtab[] = {
943 943 { IKE_PRIV_MINIMUM, "base" },
944 944 { IKE_PRIV_MODKEYS, "modkeys" },
945 945 { IKE_PRIV_KEYMAT, "keymat" },
946 946 { IKE_PRIV_MINIMUM, "0" },
947 947 };
948 948
949 949 int
950 950 privstr2num(char *str)
951 951 {
952 952 keywdtab_t *pp;
953 953 char *endp;
954 954 int priv;
955 955
956 956 for (pp = privtab; pp < A_END(privtab); pp++) {
957 957 if (strcasecmp(str, pp->kw_str) == 0)
958 958 return (pp->kw_tag);
959 959 }
960 960
961 961 priv = strtol(str, &endp, 0);
962 962 if (*endp == '\0')
963 963 return (priv);
964 964
965 965 return (-1);
966 966 }
967 967
968 968 static keywdtab_t dbgtab[] = {
969 969 { D_CERT, "cert" },
970 970 { D_KEY, "key" },
971 971 { D_OP, "op" },
972 972 { D_P1, "p1" },
973 973 { D_P1, "phase1" },
974 974 { D_P2, "p2" },
975 975 { D_P2, "phase2" },
976 976 { D_PFKEY, "pfkey" },
977 977 { D_POL, "pol" },
978 978 { D_POL, "policy" },
979 979 { D_PROP, "prop" },
980 980 { D_DOOR, "door" },
981 981 { D_CONFIG, "config" },
982 982 { D_LABEL, "label" },
983 983 { D_ALL, "all" },
984 984 { 0, "0" },
985 985 };
986 986
987 987 int
988 988 dbgstr2num(char *str)
989 989 {
990 990 keywdtab_t *dp;
991 991
992 992 for (dp = dbgtab; dp < A_END(dbgtab); dp++) {
993 993 if (strcasecmp(str, dp->kw_str) == 0)
994 994 return (dp->kw_tag);
995 995 }
996 996 return (D_INVALID);
997 997 }
998 998
999 999 int
1000 1000 parsedbgopts(char *optarg)
1001 1001 {
1002 1002 char *argp, *endp, op, nextop;
1003 1003 int mask = 0, new;
1004 1004
1005 1005 mask = strtol(optarg, &endp, 0);
1006 1006 if (*endp == '\0')
1007 1007 return (mask);
1008 1008
1009 1009 op = optarg[0];
1010 1010 if (op != '-')
1011 1011 op = '+';
1012 1012 argp = strtok_d(optarg, "+-", &nextop);
1013 1013 do {
1014 1014 new = dbgstr2num(argp);
1015 1015 if (new == D_INVALID) {
1016 1016 /* we encountered an invalid keywd */
1017 1017 return (new);
1018 1018 }
1019 1019 if (op == '+') {
1020 1020 mask |= new;
1021 1021 } else {
1022 1022 mask &= ~new;
1023 1023 }
1024 1024 op = nextop;
1025 1025 } while ((argp = strtok_d(NULL, "+-", &nextop)) != NULL);
1026 1026
1027 1027 return (mask);
1028 1028 }
1029 1029
1030 1030
1031 1031 /*
1032 1032 * functions to manipulate the kmcookie-label mapping file
1033 1033 */
1034 1034
1035 1035 /*
1036 1036 * Open, lockf, fdopen the given file, returning a FILE * on success,
1037 1037 * or NULL on failure.
1038 1038 */
1039 1039 FILE *
1040 1040 kmc_open_and_lock(char *name)
1041 1041 {
1042 1042 int fd, rtnerr;
1043 1043 FILE *fp;
1044 1044
1045 1045 if ((fd = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
1046 1046 return (NULL);
1047 1047 }
1048 1048 if (lockf(fd, F_LOCK, 0) < 0) {
1049 1049 return (NULL);
1050 1050 }
1051 1051 if ((fp = fdopen(fd, "a+")) == NULL) {
1052 1052 return (NULL);
1053 1053 }
1054 1054 if (fseek(fp, 0, SEEK_SET) < 0) {
1055 1055 /* save errno in case fclose changes it */
1056 1056 rtnerr = errno;
1057 1057 (void) fclose(fp);
1058 1058 errno = rtnerr;
1059 1059 return (NULL);
1060 1060 }
1061 1061 return (fp);
1062 1062 }
1063 1063
1064 1064 /*
1065 1065 * Extract an integer cookie and string label from a line from the
1066 1066 * kmcookie-label file. Return -1 on failure, 0 on success.
1067 1067 */
1068 1068 int
1069 1069 kmc_parse_line(char *line, int *cookie, char **label)
1070 1070 {
1071 1071 char *cookiestr;
1072 1072
1073 1073 *cookie = 0;
1074 1074 *label = NULL;
1075 1075
1076 1076 cookiestr = strtok(line, " \t\n");
1077 1077 if (cookiestr == NULL) {
1078 1078 return (-1);
1079 1079 }
1080 1080
1081 1081 /* Everything that follows, up to the newline, is the label. */
1082 1082 *label = strtok(NULL, "\n");
1083 1083 if (*label == NULL) {
1084 1084 return (-1);
1085 1085 }
1086 1086
1087 1087 *cookie = atoi(cookiestr);
1088 1088 return (0);
1089 1089 }
1090 1090
1091 1091 /*
1092 1092 * Insert a mapping into the file (if it's not already there), given the
1093 1093 * new label. Return the assigned cookie, or -1 on error.
1094 1094 */
1095 1095 int
1096 1096 kmc_insert_mapping(char *label)
1097 1097 {
1098 1098 FILE *map;
1099 1099 char linebuf[IBUF_SIZE];
1100 1100 char *cur_label;
1101 1101 int max_cookie = 0, cur_cookie, rtn_cookie;
1102 1102 int rtnerr = 0;
1103 1103 boolean_t found = B_FALSE;
1104 1104
1105 1105 /* open and lock the file; will sleep until lock is available */
1106 1106 if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
1107 1107 /* kmc_open_and_lock() sets errno appropriately */
1108 1108 return (-1);
1109 1109 }
1110 1110
1111 1111 while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
1112 1112
1113 1113 /* Skip blank lines, which often come near EOF. */
1114 1114 if (strlen(linebuf) == 0)
1115 1115 continue;
1116 1116
1117 1117 if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
1118 1118 rtnerr = EINVAL;
1119 1119 goto error;
1120 1120 }
1121 1121
1122 1122 if (cur_cookie > max_cookie)
1123 1123 max_cookie = cur_cookie;
1124 1124
1125 1125 if ((!found) && (strcmp(cur_label, label) == 0)) {
1126 1126 found = B_TRUE;
1127 1127 rtn_cookie = cur_cookie;
1128 1128 }
1129 1129 }
1130 1130
1131 1131 if (!found) {
1132 1132 rtn_cookie = ++max_cookie;
1133 1133 if ((fprintf(map, "%u\t%s\n", rtn_cookie, label) < 0) ||
1134 1134 (fflush(map) < 0)) {
1135 1135 rtnerr = errno;
1136 1136 goto error;
1137 1137 }
1138 1138 }
1139 1139 (void) fclose(map);
1140 1140
1141 1141 return (rtn_cookie);
1142 1142
1143 1143 error:
1144 1144 (void) fclose(map);
1145 1145 errno = rtnerr;
1146 1146 return (-1);
1147 1147 }
1148 1148
1149 1149 /*
1150 1150 * Lookup the given cookie and return its corresponding label. Return
1151 1151 * a pointer to the label on success, NULL on error (or if the label is
1152 1152 * not found). Note that the returned label pointer points to a static
1153 1153 * string, so the label will be overwritten by a subsequent call to the
1154 1154 * function; the function is also not thread-safe as a result.
1155 1155 */
1156 1156 char *
1157 1157 kmc_lookup_by_cookie(int cookie)
1158 1158 {
1159 1159 FILE *map;
1160 1160 static char linebuf[IBUF_SIZE];
1161 1161 char *cur_label;
1162 1162 int cur_cookie;
1163 1163
1164 1164 if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
1165 1165 return (NULL);
1166 1166 }
1167 1167
1168 1168 while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
1169 1169
1170 1170 if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
1171 1171 (void) fclose(map);
1172 1172 return (NULL);
1173 1173 }
1174 1174
1175 1175 if (cookie == cur_cookie) {
1176 1176 (void) fclose(map);
1177 1177 return (cur_label);
1178 1178 }
1179 1179 }
1180 1180 (void) fclose(map);
1181 1181
1182 1182 return (NULL);
1183 1183 }
1184 1184
1185 1185 /*
1186 1186 * Parse basic extension headers and return in the passed-in pointer vector.
1187 1187 * Return values include:
1188 1188 *
1189 1189 * KGE_OK Everything's nice and parsed out.
1190 1190 * If there are no extensions, place NULL in extv[0].
1191 1191 * KGE_DUP There is a duplicate extension.
1192 1192 * First instance in appropriate bin. First duplicate in
1193 1193 * extv[0].
1194 1194 * KGE_UNK Unknown extension type encountered. extv[0] contains
1195 1195 * unknown header.
1196 1196 * KGE_LEN Extension length error.
1197 1197 * KGE_CHK High-level reality check failed on specific extension.
1198 1198 *
1199 1199 * My apologies for some of the pointer arithmetic in here. I'm thinking
1200 1200 * like an assembly programmer, yet trying to make the compiler happy.
1201 1201 */
1202 1202 int
1203 1203 spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize,
1204 1204 char *diag_buf, uint_t diag_buf_len)
1205 1205 {
1206 1206 int i;
1207 1207
1208 1208 if (diag_buf != NULL)
1209 1209 diag_buf[0] = '\0';
1210 1210
1211 1211 for (i = 1; i <= SPD_EXT_MAX; i++)
1212 1212 extv[i] = NULL;
1213 1213
1214 1214 i = 0;
1215 1215 /* Use extv[0] as the "current working pointer". */
1216 1216
1217 1217 extv[0] = (spd_ext_t *)(basehdr + 1);
1218 1218 msgsize = SPD_64TO8(msgsize);
1219 1219
1220 1220 while ((char *)extv[0] < ((char *)basehdr + msgsize)) {
1221 1221 /* Check for unknown headers. */
1222 1222 i++;
1223 1223 if (extv[0]->spd_ext_type == 0 ||
1224 1224 extv[0]->spd_ext_type > SPD_EXT_MAX) {
1225 1225 if (diag_buf != NULL) {
1226 1226 (void) snprintf(diag_buf, diag_buf_len,
1227 1227 "spdsock ext 0x%X unknown: 0x%X",
1228 1228 i, extv[0]->spd_ext_type);
1229 1229 }
1230 1230 return (KGE_UNK);
1231 1231 }
1232 1232
1233 1233 /*
1234 1234 * Check length. Use uint64_t because extlen is in units
1235 1235 * of 64-bit words. If length goes beyond the msgsize,
1236 1236 * return an error. (Zero length also qualifies here.)
1237 1237 */
1238 1238 if (extv[0]->spd_ext_len == 0 ||
1239 1239 (uint8_t *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
1240 1240 (uint8_t *)((uint8_t *)basehdr + msgsize))
1241 1241 return (KGE_LEN);
1242 1242
1243 1243 /* Check for redundant headers. */
1244 1244 if (extv[extv[0]->spd_ext_type] != NULL)
1245 1245 return (KGE_DUP);
1246 1246
1247 1247 /* If I make it here, assign the appropriate bin. */
1248 1248 extv[extv[0]->spd_ext_type] = extv[0];
1249 1249
1250 1250 /* Advance pointer (See above for uint64_t ptr reasoning.) */
1251 1251 extv[0] = (spd_ext_t *)
1252 1252 ((uint64_t *)extv[0] + extv[0]->spd_ext_len);
1253 1253 }
1254 1254
1255 1255 /* Everything's cool. */
1256 1256
1257 1257 /*
1258 1258 * If extv[0] == NULL, then there are no extension headers in this
1259 1259 * message. Ensure that this is the case.
1260 1260 */
1261 1261 if (extv[0] == (spd_ext_t *)(basehdr + 1))
1262 1262 extv[0] = NULL;
1263 1263
1264 1264 return (KGE_OK);
1265 1265 }
1266 1266
1267 1267 const char *
1268 1268 spdsock_diag(int diagnostic)
1269 1269 {
1270 1270 switch (diagnostic) {
1271 1271 case SPD_DIAGNOSTIC_NONE:
1272 1272 return (dgettext(TEXT_DOMAIN, "no error"));
1273 1273 case SPD_DIAGNOSTIC_UNKNOWN_EXT:
1274 1274 return (dgettext(TEXT_DOMAIN, "unknown extension"));
1275 1275 case SPD_DIAGNOSTIC_BAD_EXTLEN:
1276 1276 return (dgettext(TEXT_DOMAIN, "bad extension length"));
1277 1277 case SPD_DIAGNOSTIC_NO_RULE_EXT:
1278 1278 return (dgettext(TEXT_DOMAIN, "no rule extension"));
1279 1279 case SPD_DIAGNOSTIC_BAD_ADDR_LEN:
1280 1280 return (dgettext(TEXT_DOMAIN, "bad address len"));
1281 1281 case SPD_DIAGNOSTIC_MIXED_AF:
1282 1282 return (dgettext(TEXT_DOMAIN, "mixed address family"));
1283 1283 case SPD_DIAGNOSTIC_ADD_NO_MEM:
1284 1284 return (dgettext(TEXT_DOMAIN, "add: no memory"));
1285 1285 case SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT:
1286 1286 return (dgettext(TEXT_DOMAIN, "add: wrong action count"));
1287 1287 case SPD_DIAGNOSTIC_ADD_BAD_TYPE:
1288 1288 return (dgettext(TEXT_DOMAIN, "add: bad type"));
1289 1289 case SPD_DIAGNOSTIC_ADD_BAD_FLAGS:
1290 1290 return (dgettext(TEXT_DOMAIN, "add: bad flags"));
1291 1291 case SPD_DIAGNOSTIC_ADD_INCON_FLAGS:
1292 1292 return (dgettext(TEXT_DOMAIN, "add: inconsistent flags"));
1293 1293 case SPD_DIAGNOSTIC_MALFORMED_LCLPORT:
1294 1294 return (dgettext(TEXT_DOMAIN, "malformed local port"));
1295 1295 case SPD_DIAGNOSTIC_DUPLICATE_LCLPORT:
1296 1296 return (dgettext(TEXT_DOMAIN, "duplicate local port"));
1297 1297 case SPD_DIAGNOSTIC_MALFORMED_REMPORT:
1298 1298 return (dgettext(TEXT_DOMAIN, "malformed remote port"));
1299 1299 case SPD_DIAGNOSTIC_DUPLICATE_REMPORT:
1300 1300 return (dgettext(TEXT_DOMAIN, "duplicate remote port"));
1301 1301 case SPD_DIAGNOSTIC_MALFORMED_PROTO:
1302 1302 return (dgettext(TEXT_DOMAIN, "malformed proto"));
1303 1303 case SPD_DIAGNOSTIC_DUPLICATE_PROTO:
1304 1304 return (dgettext(TEXT_DOMAIN, "duplicate proto"));
1305 1305 case SPD_DIAGNOSTIC_MALFORMED_LCLADDR:
1306 1306 return (dgettext(TEXT_DOMAIN, "malformed local address"));
1307 1307 case SPD_DIAGNOSTIC_DUPLICATE_LCLADDR:
1308 1308 return (dgettext(TEXT_DOMAIN, "duplicate local address"));
1309 1309 case SPD_DIAGNOSTIC_MALFORMED_REMADDR:
1310 1310 return (dgettext(TEXT_DOMAIN, "malformed remote address"));
1311 1311 case SPD_DIAGNOSTIC_DUPLICATE_REMADDR:
1312 1312 return (dgettext(TEXT_DOMAIN, "duplicate remote address"));
1313 1313 case SPD_DIAGNOSTIC_MALFORMED_ACTION:
1314 1314 return (dgettext(TEXT_DOMAIN, "malformed action"));
1315 1315 case SPD_DIAGNOSTIC_DUPLICATE_ACTION:
1316 1316 return (dgettext(TEXT_DOMAIN, "duplicate action"));
1317 1317 case SPD_DIAGNOSTIC_MALFORMED_RULE:
1318 1318 return (dgettext(TEXT_DOMAIN, "malformed rule"));
1319 1319 case SPD_DIAGNOSTIC_DUPLICATE_RULE:
1320 1320 return (dgettext(TEXT_DOMAIN, "duplicate rule"));
1321 1321 case SPD_DIAGNOSTIC_MALFORMED_RULESET:
1322 1322 return (dgettext(TEXT_DOMAIN, "malformed ruleset"));
1323 1323 case SPD_DIAGNOSTIC_DUPLICATE_RULESET:
1324 1324 return (dgettext(TEXT_DOMAIN, "duplicate ruleset"));
1325 1325 case SPD_DIAGNOSTIC_INVALID_RULE_INDEX:
1326 1326 return (dgettext(TEXT_DOMAIN, "invalid rule index"));
1327 1327 case SPD_DIAGNOSTIC_BAD_SPDID:
1328 1328 return (dgettext(TEXT_DOMAIN, "bad spdid"));
1329 1329 case SPD_DIAGNOSTIC_BAD_MSG_TYPE:
1330 1330 return (dgettext(TEXT_DOMAIN, "bad message type"));
1331 1331 case SPD_DIAGNOSTIC_UNSUPP_AH_ALG:
1332 1332 return (dgettext(TEXT_DOMAIN, "unsupported AH algorithm"));
1333 1333 case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG:
1334 1334 return (dgettext(TEXT_DOMAIN,
1335 1335 "unsupported ESP encryption algorithm"));
1336 1336 case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG:
1337 1337 return (dgettext(TEXT_DOMAIN,
1338 1338 "unsupported ESP authentication algorithm"));
1339 1339 case SPD_DIAGNOSTIC_UNSUPP_AH_KEYSIZE:
1340 1340 return (dgettext(TEXT_DOMAIN, "unsupported AH key size"));
1341 1341 case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_KEYSIZE:
1342 1342 return (dgettext(TEXT_DOMAIN,
1343 1343 "unsupported ESP encryption key size"));
1344 1344 case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_KEYSIZE:
1345 1345 return (dgettext(TEXT_DOMAIN,
1346 1346 "unsupported ESP authentication key size"));
1347 1347 case SPD_DIAGNOSTIC_NO_ACTION_EXT:
1348 1348 return (dgettext(TEXT_DOMAIN, "No ACTION extension"));
1349 1349 case SPD_DIAGNOSTIC_ALG_ID_RANGE:
1350 1350 return (dgettext(TEXT_DOMAIN, "invalid algorithm identifer"));
1351 1351 case SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES:
1352 1352 return (dgettext(TEXT_DOMAIN,
1353 1353 "number of key sizes inconsistent"));
1354 1354 case SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES:
1355 1355 return (dgettext(TEXT_DOMAIN,
1356 1356 "number of block sizes inconsistent"));
1357 1357 case SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN:
1358 1358 return (dgettext(TEXT_DOMAIN, "invalid mechanism name length"));
1359 1359 case SPD_DIAGNOSTIC_NOT_GLOBAL_OP:
1360 1360 return (dgettext(TEXT_DOMAIN,
1361 1361 "operation not applicable to all policies"));
1362 1362 case SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS:
1363 1363 return (dgettext(TEXT_DOMAIN,
1364 1364 "using selectors on a transport-mode tunnel"));
1365 1365 default:
1366 1366 return (dgettext(TEXT_DOMAIN, "unknown diagnostic"));
1367 1367 }
1368 1368 }
1369 1369
1370 1370 /*
1371 1371 * PF_KEY Diagnostic table.
1372 1372 *
1373 1373 * PF_KEY NOTE: If you change pfkeyv2.h's SADB_X_DIAGNOSTIC_* space, this is
1374 1374 * where you need to add new messages.
1375 1375 */
1376 1376
1377 1377 const char *
1378 1378 keysock_diag(int diagnostic)
1379 1379 {
1380 1380 switch (diagnostic) {
1381 1381 case SADB_X_DIAGNOSTIC_NONE:
1382 1382 return (dgettext(TEXT_DOMAIN, "No diagnostic"));
1383 1383 case SADB_X_DIAGNOSTIC_UNKNOWN_MSG:
1384 1384 return (dgettext(TEXT_DOMAIN, "Unknown message type"));
1385 1385 case SADB_X_DIAGNOSTIC_UNKNOWN_EXT:
1386 1386 return (dgettext(TEXT_DOMAIN, "Unknown extension type"));
1387 1387 case SADB_X_DIAGNOSTIC_BAD_EXTLEN:
1388 1388 return (dgettext(TEXT_DOMAIN, "Bad extension length"));
1389 1389 case SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE:
1390 1390 return (dgettext(TEXT_DOMAIN,
1391 1391 "Unknown Security Association type"));
1392 1392 case SADB_X_DIAGNOSTIC_SATYPE_NEEDED:
1393 1393 return (dgettext(TEXT_DOMAIN,
1394 1394 "Specific Security Association type needed"));
1395 1395 case SADB_X_DIAGNOSTIC_NO_SADBS:
1396 1396 return (dgettext(TEXT_DOMAIN,
1397 1397 "No Security Association Databases present"));
1398 1398 case SADB_X_DIAGNOSTIC_NO_EXT:
1399 1399 return (dgettext(TEXT_DOMAIN,
1400 1400 "No extensions needed for message"));
1401 1401 case SADB_X_DIAGNOSTIC_BAD_SRC_AF:
1402 1402 return (dgettext(TEXT_DOMAIN, "Bad source address family"));
1403 1403 case SADB_X_DIAGNOSTIC_BAD_DST_AF:
1404 1404 return (dgettext(TEXT_DOMAIN,
1405 1405 "Bad destination address family"));
1406 1406 case SADB_X_DIAGNOSTIC_BAD_PROXY_AF:
1407 1407 return (dgettext(TEXT_DOMAIN,
1408 1408 "Bad inner-source address family"));
1409 1409 case SADB_X_DIAGNOSTIC_AF_MISMATCH:
1410 1410 return (dgettext(TEXT_DOMAIN,
1411 1411 "Source/destination address family mismatch"));
1412 1412 case SADB_X_DIAGNOSTIC_BAD_SRC:
1413 1413 return (dgettext(TEXT_DOMAIN, "Bad source address value"));
1414 1414 case SADB_X_DIAGNOSTIC_BAD_DST:
1415 1415 return (dgettext(TEXT_DOMAIN, "Bad destination address value"));
1416 1416 case SADB_X_DIAGNOSTIC_ALLOC_HSERR:
1417 1417 return (dgettext(TEXT_DOMAIN,
1418 1418 "Soft allocations limit more than hard limit"));
1419 1419 case SADB_X_DIAGNOSTIC_BYTES_HSERR:
1420 1420 return (dgettext(TEXT_DOMAIN,
1421 1421 "Soft bytes limit more than hard limit"));
1422 1422 case SADB_X_DIAGNOSTIC_ADDTIME_HSERR:
1423 1423 return (dgettext(TEXT_DOMAIN, "Soft add expiration time later "
1424 1424 "than hard expiration time"));
1425 1425 case SADB_X_DIAGNOSTIC_USETIME_HSERR:
1426 1426 return (dgettext(TEXT_DOMAIN, "Soft use expiration time later "
1427 1427 "than hard expiration time"));
1428 1428 case SADB_X_DIAGNOSTIC_MISSING_SRC:
1429 1429 return (dgettext(TEXT_DOMAIN, "Missing source address"));
1430 1430 case SADB_X_DIAGNOSTIC_MISSING_DST:
1431 1431 return (dgettext(TEXT_DOMAIN, "Missing destination address"));
1432 1432 case SADB_X_DIAGNOSTIC_MISSING_SA:
1433 1433 return (dgettext(TEXT_DOMAIN, "Missing SA extension"));
1434 1434 case SADB_X_DIAGNOSTIC_MISSING_EKEY:
1435 1435 return (dgettext(TEXT_DOMAIN, "Missing encryption key"));
1436 1436 case SADB_X_DIAGNOSTIC_MISSING_AKEY:
1437 1437 return (dgettext(TEXT_DOMAIN, "Missing authentication key"));
1438 1438 case SADB_X_DIAGNOSTIC_MISSING_RANGE:
1439 1439 return (dgettext(TEXT_DOMAIN, "Missing SPI range"));
1440 1440 case SADB_X_DIAGNOSTIC_DUPLICATE_SRC:
1441 1441 return (dgettext(TEXT_DOMAIN, "Duplicate source address"));
1442 1442 case SADB_X_DIAGNOSTIC_DUPLICATE_DST:
1443 1443 return (dgettext(TEXT_DOMAIN, "Duplicate destination address"));
1444 1444 case SADB_X_DIAGNOSTIC_DUPLICATE_SA:
1445 1445 return (dgettext(TEXT_DOMAIN, "Duplicate SA extension"));
1446 1446 case SADB_X_DIAGNOSTIC_DUPLICATE_EKEY:
1447 1447 return (dgettext(TEXT_DOMAIN, "Duplicate encryption key"));
1448 1448 case SADB_X_DIAGNOSTIC_DUPLICATE_AKEY:
1449 1449 return (dgettext(TEXT_DOMAIN, "Duplicate authentication key"));
1450 1450 case SADB_X_DIAGNOSTIC_DUPLICATE_RANGE:
1451 1451 return (dgettext(TEXT_DOMAIN, "Duplicate SPI range"));
1452 1452 case SADB_X_DIAGNOSTIC_MALFORMED_SRC:
1453 1453 return (dgettext(TEXT_DOMAIN, "Malformed source address"));
1454 1454 case SADB_X_DIAGNOSTIC_MALFORMED_DST:
1455 1455 return (dgettext(TEXT_DOMAIN, "Malformed destination address"));
1456 1456 case SADB_X_DIAGNOSTIC_MALFORMED_SA:
1457 1457 return (dgettext(TEXT_DOMAIN, "Malformed SA extension"));
1458 1458 case SADB_X_DIAGNOSTIC_MALFORMED_EKEY:
1459 1459 return (dgettext(TEXT_DOMAIN, "Malformed encryption key"));
1460 1460 case SADB_X_DIAGNOSTIC_MALFORMED_AKEY:
1461 1461 return (dgettext(TEXT_DOMAIN, "Malformed authentication key"));
1462 1462 case SADB_X_DIAGNOSTIC_MALFORMED_RANGE:
1463 1463 return (dgettext(TEXT_DOMAIN, "Malformed SPI range"));
1464 1464 case SADB_X_DIAGNOSTIC_AKEY_PRESENT:
1465 1465 return (dgettext(TEXT_DOMAIN, "Authentication key not needed"));
1466 1466 case SADB_X_DIAGNOSTIC_EKEY_PRESENT:
1467 1467 return (dgettext(TEXT_DOMAIN, "Encryption key not needed"));
1468 1468 case SADB_X_DIAGNOSTIC_PROP_PRESENT:
1469 1469 return (dgettext(TEXT_DOMAIN, "Proposal extension not needed"));
1470 1470 case SADB_X_DIAGNOSTIC_SUPP_PRESENT:
1471 1471 return (dgettext(TEXT_DOMAIN,
1472 1472 "Supported algorithms extension not needed"));
1473 1473 case SADB_X_DIAGNOSTIC_BAD_AALG:
1474 1474 return (dgettext(TEXT_DOMAIN,
1475 1475 "Unsupported authentication algorithm"));
1476 1476 case SADB_X_DIAGNOSTIC_BAD_EALG:
1477 1477 return (dgettext(TEXT_DOMAIN,
1478 1478 "Unsupported encryption algorithm"));
1479 1479 case SADB_X_DIAGNOSTIC_BAD_SAFLAGS:
1480 1480 return (dgettext(TEXT_DOMAIN, "Invalid SA flags"));
1481 1481 case SADB_X_DIAGNOSTIC_BAD_SASTATE:
1482 1482 return (dgettext(TEXT_DOMAIN, "Invalid SA state"));
1483 1483 case SADB_X_DIAGNOSTIC_BAD_AKEYBITS:
1484 1484 return (dgettext(TEXT_DOMAIN,
1485 1485 "Bad number of authentication bits"));
1486 1486 case SADB_X_DIAGNOSTIC_BAD_EKEYBITS:
1487 1487 return (dgettext(TEXT_DOMAIN,
1488 1488 "Bad number of encryption bits"));
1489 1489 case SADB_X_DIAGNOSTIC_ENCR_NOTSUPP:
1490 1490 return (dgettext(TEXT_DOMAIN,
1491 1491 "Encryption not supported for this SA type"));
1492 1492 case SADB_X_DIAGNOSTIC_WEAK_EKEY:
1493 1493 return (dgettext(TEXT_DOMAIN, "Weak encryption key"));
1494 1494 case SADB_X_DIAGNOSTIC_WEAK_AKEY:
1495 1495 return (dgettext(TEXT_DOMAIN, "Weak authentication key"));
1496 1496 case SADB_X_DIAGNOSTIC_DUPLICATE_KMP:
1497 1497 return (dgettext(TEXT_DOMAIN,
1498 1498 "Duplicate key management protocol"));
1499 1499 case SADB_X_DIAGNOSTIC_DUPLICATE_KMC:
1500 1500 return (dgettext(TEXT_DOMAIN,
1501 1501 "Duplicate key management cookie"));
1502 1502 case SADB_X_DIAGNOSTIC_MISSING_NATT_LOC:
1503 1503 return (dgettext(TEXT_DOMAIN, "Missing NAT-T local address"));
1504 1504 case SADB_X_DIAGNOSTIC_MISSING_NATT_REM:
1505 1505 return (dgettext(TEXT_DOMAIN, "Missing NAT-T remote address"));
1506 1506 case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_LOC:
1507 1507 return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T local address"));
1508 1508 case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_REM:
1509 1509 return (dgettext(TEXT_DOMAIN,
1510 1510 "Duplicate NAT-T remote address"));
1511 1511 case SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC:
1512 1512 return (dgettext(TEXT_DOMAIN, "Malformed NAT-T local address"));
1513 1513 case SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM:
1514 1514 return (dgettext(TEXT_DOMAIN,
1515 1515 "Malformed NAT-T remote address"));
1516 1516 case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_PORTS:
1517 1517 return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T ports"));
1518 1518 case SADB_X_DIAGNOSTIC_MISSING_INNER_SRC:
1519 1519 return (dgettext(TEXT_DOMAIN, "Missing inner source address"));
1520 1520 case SADB_X_DIAGNOSTIC_MISSING_INNER_DST:
1521 1521 return (dgettext(TEXT_DOMAIN,
1522 1522 "Missing inner destination address"));
1523 1523 case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC:
1524 1524 return (dgettext(TEXT_DOMAIN,
1525 1525 "Duplicate inner source address"));
1526 1526 case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST:
1527 1527 return (dgettext(TEXT_DOMAIN,
1528 1528 "Duplicate inner destination address"));
1529 1529 case SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC:
1530 1530 return (dgettext(TEXT_DOMAIN,
1531 1531 "Malformed inner source address"));
1532 1532 case SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST:
1533 1533 return (dgettext(TEXT_DOMAIN,
1534 1534 "Malformed inner destination address"));
1535 1535 case SADB_X_DIAGNOSTIC_PREFIX_INNER_SRC:
1536 1536 return (dgettext(TEXT_DOMAIN,
1537 1537 "Invalid inner-source prefix length "));
1538 1538 case SADB_X_DIAGNOSTIC_PREFIX_INNER_DST:
1539 1539 return (dgettext(TEXT_DOMAIN,
1540 1540 "Invalid inner-destination prefix length"));
1541 1541 case SADB_X_DIAGNOSTIC_BAD_INNER_DST_AF:
1542 1542 return (dgettext(TEXT_DOMAIN,
1543 1543 "Bad inner-destination address family"));
1544 1544 case SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH:
1545 1545 return (dgettext(TEXT_DOMAIN,
1546 1546 "Inner source/destination address family mismatch"));
1547 1547 case SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF:
1548 1548 return (dgettext(TEXT_DOMAIN,
1549 1549 "Bad NAT-T remote address family"));
1550 1550 case SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF:
1551 1551 return (dgettext(TEXT_DOMAIN,
1552 1552 "Bad NAT-T local address family"));
1553 1553 case SADB_X_DIAGNOSTIC_PROTO_MISMATCH:
1554 1554 return (dgettext(TEXT_DOMAIN,
1555 1555 "Source/desination protocol mismatch"));
1556 1556 case SADB_X_DIAGNOSTIC_INNER_PROTO_MISMATCH:
1557 1557 return (dgettext(TEXT_DOMAIN,
1558 1558 "Inner source/desination protocol mismatch"));
1559 1559 case SADB_X_DIAGNOSTIC_DUAL_PORT_SETS:
1560 1560 return (dgettext(TEXT_DOMAIN,
1561 1561 "Both inner ports and outer ports are set"));
1562 1562 case SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE:
1563 1563 return (dgettext(TEXT_DOMAIN,
1564 1564 "Pairing failed, target SA unsuitable for pairing"));
1565 1565 case SADB_X_DIAGNOSTIC_PAIR_ADD_MISMATCH:
1566 1566 return (dgettext(TEXT_DOMAIN,
1567 1567 "Source/destination address differs from pair SA"));
1568 1568 case SADB_X_DIAGNOSTIC_PAIR_ALREADY:
1569 1569 return (dgettext(TEXT_DOMAIN,
1570 1570 "Already paired with another security association"));
1571 1571 case SADB_X_DIAGNOSTIC_PAIR_SA_NOTFOUND:
1572 1572 return (dgettext(TEXT_DOMAIN,
1573 1573 "Command failed, pair security association not found"));
1574 1574 case SADB_X_DIAGNOSTIC_BAD_SA_DIRECTION:
1575 1575 return (dgettext(TEXT_DOMAIN,
1576 1576 "Inappropriate SA direction"));
1577 1577 case SADB_X_DIAGNOSTIC_SA_NOTFOUND:
1578 1578 return (dgettext(TEXT_DOMAIN,
1579 1579 "Security association not found"));
1580 1580 case SADB_X_DIAGNOSTIC_SA_EXPIRED:
1581 1581 return (dgettext(TEXT_DOMAIN,
1582 1582 "Security association is not valid"));
1583 1583 case SADB_X_DIAGNOSTIC_BAD_CTX:
1584 1584 return (dgettext(TEXT_DOMAIN,
1585 1585 "Algorithm invalid or not supported by Crypto Framework"));
1586 1586 case SADB_X_DIAGNOSTIC_INVALID_REPLAY:
1587 1587 return (dgettext(TEXT_DOMAIN,
1588 1588 "Invalid Replay counter"));
1589 1589 case SADB_X_DIAGNOSTIC_MISSING_LIFETIME:
1590 1590 return (dgettext(TEXT_DOMAIN,
1591 1591 "Inappropriate lifetimes"));
1592 1592 default:
1593 1593 return (dgettext(TEXT_DOMAIN, "Unknown diagnostic code"));
1594 1594 }
1595 1595 }
1596 1596
1597 1597 /*
1598 1598 * Convert an IPv6 mask to a prefix len. I assume all IPv6 masks are
1599 1599 * contiguous, so I stop at the first zero bit!
1600 1600 */
1601 1601 int
1602 1602 in_masktoprefix(uint8_t *mask, boolean_t is_v4mapped)
1603 1603 {
1604 1604 int rc = 0;
1605 1605 uint8_t last;
1606 1606 int limit = IPV6_ABITS;
1607 1607
1608 1608 if (is_v4mapped) {
1609 1609 mask += ((IPV6_ABITS - IP_ABITS)/8);
1610 1610 limit = IP_ABITS;
1611 1611 }
1612 1612
1613 1613 while (*mask == 0xff) {
1614 1614 rc += 8;
1615 1615 if (rc == limit)
1616 1616 return (limit);
1617 1617 mask++;
1618 1618 }
1619 1619
1620 1620 last = *mask;
1621 1621 while (last != 0) {
1622 1622 rc++;
1623 1623 last = (last << 1) & 0xff;
1624 1624 }
1625 1625
1626 1626 return (rc);
1627 1627 }
1628 1628
1629 1629 /*
1630 1630 * Expand the diagnostic code into a message.
1631 1631 */
1632 1632 void
1633 1633 print_diagnostic(FILE *file, uint16_t diagnostic)
1634 1634 {
1635 1635 /* Use two spaces so above strings can fit on the line. */
1636 1636 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1637 1637 " Diagnostic code %u: %s.\n"),
1638 1638 diagnostic, keysock_diag(diagnostic));
1639 1639 }
1640 1640
1641 1641 /*
1642 1642 * Prints the base PF_KEY message.
1643 1643 */
1644 1644 void
1645 1645 print_sadb_msg(FILE *file, struct sadb_msg *samsg, time_t wallclock,
1646 1646 boolean_t vflag)
1647 1647 {
1648 1648 if (wallclock != 0)
1649 1649 printsatime(file, wallclock, dgettext(TEXT_DOMAIN,
1650 1650 "%sTimestamp: %s\n"), "", NULL,
1651 1651 vflag);
1652 1652
1653 1653 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1654 1654 "Base message (version %u) type "),
1655 1655 samsg->sadb_msg_version);
1656 1656 switch (samsg->sadb_msg_type) {
1657 1657 case SADB_RESERVED:
1658 1658 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1659 1659 "RESERVED (warning: set to 0)"));
1660 1660 break;
1661 1661 case SADB_GETSPI:
1662 1662 (void) fprintf(file, "GETSPI");
1663 1663 break;
1664 1664 case SADB_UPDATE:
1665 1665 (void) fprintf(file, "UPDATE");
1666 1666 break;
1667 1667 case SADB_X_UPDATEPAIR:
1668 1668 (void) fprintf(file, "UPDATE PAIR");
1669 1669 break;
1670 1670 case SADB_ADD:
1671 1671 (void) fprintf(file, "ADD");
1672 1672 break;
1673 1673 case SADB_DELETE:
1674 1674 (void) fprintf(file, "DELETE");
1675 1675 break;
1676 1676 case SADB_X_DELPAIR:
1677 1677 (void) fprintf(file, "DELETE PAIR");
1678 1678 break;
1679 1679 case SADB_GET:
1680 1680 (void) fprintf(file, "GET");
1681 1681 break;
1682 1682 case SADB_ACQUIRE:
1683 1683 (void) fprintf(file, "ACQUIRE");
1684 1684 break;
1685 1685 case SADB_REGISTER:
1686 1686 (void) fprintf(file, "REGISTER");
1687 1687 break;
1688 1688 case SADB_EXPIRE:
1689 1689 (void) fprintf(file, "EXPIRE");
1690 1690 break;
1691 1691 case SADB_FLUSH:
1692 1692 (void) fprintf(file, "FLUSH");
1693 1693 break;
1694 1694 case SADB_DUMP:
1695 1695 (void) fprintf(file, "DUMP");
1696 1696 break;
1697 1697 case SADB_X_PROMISC:
1698 1698 (void) fprintf(file, "X_PROMISC");
1699 1699 break;
1700 1700 case SADB_X_INVERSE_ACQUIRE:
1701 1701 (void) fprintf(file, "X_INVERSE_ACQUIRE");
1702 1702 break;
1703 1703 default:
1704 1704 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1705 1705 "Unknown (%u)"), samsg->sadb_msg_type);
1706 1706 break;
1707 1707 }
1708 1708 (void) fprintf(file, dgettext(TEXT_DOMAIN, ", SA type "));
1709 1709
1710 1710 switch (samsg->sadb_msg_satype) {
1711 1711 case SADB_SATYPE_UNSPEC:
1712 1712 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1713 1713 "<unspecified/all>"));
1714 1714 break;
1715 1715 case SADB_SATYPE_AH:
1716 1716 (void) fprintf(file, "AH");
1717 1717 break;
1718 1718 case SADB_SATYPE_ESP:
1719 1719 (void) fprintf(file, "ESP");
1720 1720 break;
1721 1721 case SADB_SATYPE_RSVP:
1722 1722 (void) fprintf(file, "RSVP");
1723 1723 break;
1724 1724 case SADB_SATYPE_OSPFV2:
1725 1725 (void) fprintf(file, "OSPFv2");
1726 1726 break;
1727 1727 case SADB_SATYPE_RIPV2:
1728 1728 (void) fprintf(file, "RIPv2");
1729 1729 break;
1730 1730 case SADB_SATYPE_MIP:
1731 1731 (void) fprintf(file, dgettext(TEXT_DOMAIN, "Mobile IP"));
1732 1732 break;
1733 1733 default:
1734 1734 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1735 1735 "<unknown %u>"), samsg->sadb_msg_satype);
1736 1736 break;
1737 1737 }
1738 1738
1739 1739 (void) fprintf(file, ".\n");
1740 1740
1741 1741 if (samsg->sadb_msg_errno != 0) {
1742 1742 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1743 1743 "Error %s from PF_KEY.\n"),
1744 1744 strerror(samsg->sadb_msg_errno));
1745 1745 print_diagnostic(file, samsg->sadb_x_msg_diagnostic);
1746 1746 }
1747 1747
1748 1748 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1749 1749 "Message length %u bytes, seq=%u, pid=%u.\n"),
1750 1750 SADB_64TO8(samsg->sadb_msg_len), samsg->sadb_msg_seq,
1751 1751 samsg->sadb_msg_pid);
1752 1752 }
1753 1753
1754 1754 /*
1755 1755 * Print the SA extension for PF_KEY.
1756 1756 */
1757 1757 void
1758 1758 print_sa(FILE *file, char *prefix, struct sadb_sa *assoc)
1759 1759 {
1760 1760 if (assoc->sadb_sa_len != SADB_8TO64(sizeof (*assoc))) {
1761 1761 warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
1762 1762 "WARNING: SA info extension length (%u) is bad."),
1763 1763 SADB_64TO8(assoc->sadb_sa_len));
1764 1764 }
1765 1765
1766 1766 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1767 1767 "%sSADB_ASSOC spi=0x%x, replay window size=%u, state="),
1768 1768 prefix, ntohl(assoc->sadb_sa_spi), assoc->sadb_sa_replay);
1769 1769 switch (assoc->sadb_sa_state) {
1770 1770 case SADB_SASTATE_LARVAL:
1771 1771 (void) fprintf(file, dgettext(TEXT_DOMAIN, "LARVAL"));
|
↓ open down ↓ |
1771 lines elided |
↑ open up ↑ |
1772 1772 break;
1773 1773 case SADB_SASTATE_MATURE:
1774 1774 (void) fprintf(file, dgettext(TEXT_DOMAIN, "MATURE"));
1775 1775 break;
1776 1776 case SADB_SASTATE_DYING:
1777 1777 (void) fprintf(file, dgettext(TEXT_DOMAIN, "DYING"));
1778 1778 break;
1779 1779 case SADB_SASTATE_DEAD:
1780 1780 (void) fprintf(file, dgettext(TEXT_DOMAIN, "DEAD"));
1781 1781 break;
1782 +#if 0 /* DEPRECATED */
1782 1783 case SADB_X_SASTATE_ACTIVE_ELSEWHERE:
1783 1784 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1784 1785 "ACTIVE_ELSEWHERE"));
1785 1786 break;
1786 1787 case SADB_X_SASTATE_IDLE:
1787 1788 (void) fprintf(file, dgettext(TEXT_DOMAIN, "IDLE"));
1788 1789 break;
1790 +#endif
1789 1791 default:
1790 1792 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1791 1793 "<unknown %u>"), assoc->sadb_sa_state);
1792 1794 }
1793 1795
1794 1796 if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
1795 1797 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1796 1798 "\n%sAuthentication algorithm = "),
1797 1799 prefix);
1798 1800 (void) dump_aalg(assoc->sadb_sa_auth, file);
1799 1801 }
1800 1802
1801 1803 if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
1802 1804 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1803 1805 "\n%sEncryption algorithm = "), prefix);
1804 1806 (void) dump_ealg(assoc->sadb_sa_encrypt, file);
1805 1807 }
1806 1808
1807 1809 (void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%sflags=0x%x < "), prefix,
1808 1810 assoc->sadb_sa_flags);
1809 1811 if (assoc->sadb_sa_flags & SADB_SAFLAGS_PFS)
1810 1812 (void) fprintf(file, "PFS ");
1811 1813 if (assoc->sadb_sa_flags & SADB_SAFLAGS_NOREPLAY)
1812 1814 (void) fprintf(file, "NOREPLAY ");
1813 1815
1814 1816 /* BEGIN Solaris-specific flags. */
1815 1817 if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_USED)
1816 1818 (void) fprintf(file, "X_USED ");
1817 1819 if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_PAIRED)
1818 1820 (void) fprintf(file, "X_PAIRED ");
1819 1821 if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_OUTBOUND)
1820 1822 (void) fprintf(file, "X_OUTBOUND ");
1821 1823 if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_INBOUND)
1822 1824 (void) fprintf(file, "X_INBOUND ");
1823 1825 if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_UNIQUE)
1824 1826 (void) fprintf(file, "X_UNIQUE ");
1825 1827 if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG1)
1826 1828 (void) fprintf(file, "X_AALG1 ");
1827 1829 if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG2)
1828 1830 (void) fprintf(file, "X_AALG2 ");
1829 1831 if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG1)
1830 1832 (void) fprintf(file, "X_EALG1 ");
1831 1833 if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG2)
1832 1834 (void) fprintf(file, "X_EALG2 ");
1833 1835 if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC)
1834 1836 (void) fprintf(file, "X_NATT_LOC ");
1835 1837 if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM)
1836 1838 (void) fprintf(file, "X_NATT_REM ");
1837 1839 if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL)
1838 1840 (void) fprintf(file, "X_TUNNEL ");
1839 1841 if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATTED)
1840 1842 (void) fprintf(file, "X_NATTED ");
1841 1843 /* END Solaris-specific flags. */
1842 1844
1843 1845 (void) fprintf(file, ">\n");
1844 1846 }
1845 1847
1846 1848 void
1847 1849 printsatime(FILE *file, int64_t lt, const char *msg, const char *pfx,
1848 1850 const char *pfx2, boolean_t vflag)
1849 1851 {
1850 1852 char tbuf[TBUF_SIZE]; /* For strftime() call. */
1851 1853 const char *tp = tbuf;
1852 1854 time_t t = lt;
1853 1855 struct tm res;
1854 1856
1855 1857 if (t != lt) {
1856 1858 if (lt > 0)
1857 1859 t = LONG_MAX;
1858 1860 else
1859 1861 t = LONG_MIN;
1860 1862 }
1861 1863
1862 1864 if (strftime(tbuf, TBUF_SIZE, NULL, localtime_r(&t, &res)) == 0)
1863 1865 tp = dgettext(TEXT_DOMAIN, "<time conversion failed>");
1864 1866 (void) fprintf(file, msg, pfx, tp);
1865 1867 if (vflag && (pfx2 != NULL))
1866 1868 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1867 1869 "%s\t(raw time value %" PRIu64 ")\n"), pfx2, lt);
1868 1870 }
1869 1871
1870 1872 /*
1871 1873 * Print the SA lifetime information. (An SADB_EXT_LIFETIME_* extension.)
1872 1874 */
1873 1875 void
1874 1876 print_lifetimes(FILE *file, time_t wallclock, struct sadb_lifetime *current,
1875 1877 struct sadb_lifetime *hard, struct sadb_lifetime *soft,
1876 1878 struct sadb_lifetime *idle, boolean_t vflag)
1877 1879 {
1878 1880 int64_t scratch;
1879 1881 char *soft_prefix = dgettext(TEXT_DOMAIN, "SLT: ");
1880 1882 char *hard_prefix = dgettext(TEXT_DOMAIN, "HLT: ");
1881 1883 char *current_prefix = dgettext(TEXT_DOMAIN, "CLT: ");
1882 1884 char *idle_prefix = dgettext(TEXT_DOMAIN, "ILT: ");
1883 1885 char byte_str[BYTE_STR_SIZE]; /* byte lifetime string representation */
1884 1886 char secs_str[SECS_STR_SIZE]; /* buffer for seconds representation */
1885 1887
1886 1888 if (current != NULL &&
1887 1889 current->sadb_lifetime_len != SADB_8TO64(sizeof (*current))) {
1888 1890 warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
1889 1891 "WARNING: CURRENT lifetime extension length (%u) is bad."),
1890 1892 SADB_64TO8(current->sadb_lifetime_len));
1891 1893 }
1892 1894
1893 1895 if (hard != NULL &&
1894 1896 hard->sadb_lifetime_len != SADB_8TO64(sizeof (*hard))) {
1895 1897 warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
1896 1898 "WARNING: HARD lifetime extension length (%u) is bad."),
1897 1899 SADB_64TO8(hard->sadb_lifetime_len));
1898 1900 }
1899 1901
1900 1902 if (soft != NULL &&
1901 1903 soft->sadb_lifetime_len != SADB_8TO64(sizeof (*soft))) {
1902 1904 warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
1903 1905 "WARNING: SOFT lifetime extension length (%u) is bad."),
1904 1906 SADB_64TO8(soft->sadb_lifetime_len));
1905 1907 }
1906 1908
1907 1909 if (idle != NULL &&
1908 1910 idle->sadb_lifetime_len != SADB_8TO64(sizeof (*idle))) {
1909 1911 warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
1910 1912 "WARNING: IDLE lifetime extension length (%u) is bad."),
1911 1913 SADB_64TO8(idle->sadb_lifetime_len));
1912 1914 }
1913 1915
1914 1916 (void) fprintf(file, " LT: Lifetime information\n");
1915 1917 if (current != NULL) {
1916 1918 /* Express values as current values. */
1917 1919 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1918 1920 "%sCurrent lifetime information:\n"),
1919 1921 current_prefix);
1920 1922 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1921 1923 "%s%" PRIu64 " bytes %sprotected, %u allocations "
1922 1924 "used.\n"), current_prefix,
1923 1925 current->sadb_lifetime_bytes,
1924 1926 bytecnt2out(current->sadb_lifetime_bytes, byte_str,
1925 1927 sizeof (byte_str), SPC_END),
1926 1928 current->sadb_lifetime_allocations);
1927 1929 printsatime(file, current->sadb_lifetime_addtime,
1928 1930 dgettext(TEXT_DOMAIN, "%sSA added at time: %s\n"),
1929 1931 current_prefix, current_prefix, vflag);
1930 1932 if (current->sadb_lifetime_usetime != 0) {
1931 1933 printsatime(file, current->sadb_lifetime_usetime,
1932 1934 dgettext(TEXT_DOMAIN,
1933 1935 "%sSA first used at time %s\n"),
1934 1936 current_prefix, current_prefix, vflag);
1935 1937 }
1936 1938 printsatime(file, wallclock, dgettext(TEXT_DOMAIN,
1937 1939 "%sTime now is %s\n"), current_prefix, current_prefix,
1938 1940 vflag);
1939 1941 }
1940 1942
1941 1943 if (soft != NULL) {
1942 1944 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1943 1945 "%sSoft lifetime information:\n"),
1944 1946 soft_prefix);
1945 1947 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1946 1948 "%s%" PRIu64 " bytes %sof lifetime, %u allocations.\n"),
1947 1949 soft_prefix,
1948 1950 soft->sadb_lifetime_bytes,
1949 1951 bytecnt2out(soft->sadb_lifetime_bytes, byte_str,
1950 1952 sizeof (byte_str), SPC_END),
1951 1953 soft->sadb_lifetime_allocations);
1952 1954 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1953 1955 "%s%" PRIu64 " seconds %sof post-add lifetime.\n"),
1954 1956 soft_prefix, soft->sadb_lifetime_addtime,
1955 1957 secs2out(soft->sadb_lifetime_addtime, secs_str,
1956 1958 sizeof (secs_str), SPC_END));
1957 1959 (void) fprintf(file, dgettext(TEXT_DOMAIN,
1958 1960 "%s%" PRIu64 " seconds %sof post-use lifetime.\n"),
1959 1961 soft_prefix, soft->sadb_lifetime_usetime,
1960 1962 secs2out(soft->sadb_lifetime_usetime, secs_str,
1961 1963 sizeof (secs_str), SPC_END));
1962 1964 /* If possible, express values as time remaining. */
1963 1965 if (current != NULL) {
1964 1966 if (soft->sadb_lifetime_bytes != 0)
1965 1967 (void) fprintf(file, dgettext(TEXT_DOMAIN, "%s"
1966 1968 "%" PRIu64 " bytes %smore can be "
1967 1969 "protected.\n"), soft_prefix,
1968 1970 (soft->sadb_lifetime_bytes >
1969 1971 current->sadb_lifetime_bytes) ?
1970 1972 soft->sadb_lifetime_bytes -
1971 1973 current->sadb_lifetime_bytes : 0,
1972 1974 (soft->sadb_lifetime_bytes >
1973 1975 current->sadb_lifetime_bytes) ?
1974 1976 bytecnt2out(soft->sadb_lifetime_bytes -
1975 1977 current->sadb_lifetime_bytes, byte_str,
1976 1978 sizeof (byte_str), SPC_END) : "");
1977 1979 if (soft->sadb_lifetime_addtime != 0 ||
1978 1980 (soft->sadb_lifetime_usetime != 0 &&
1979 1981 current->sadb_lifetime_usetime != 0)) {
1980 1982 int64_t adddelta, usedelta;
1981 1983
1982 1984 if (soft->sadb_lifetime_addtime != 0) {
1983 1985 adddelta =
1984 1986 current->sadb_lifetime_addtime +
1985 1987 soft->sadb_lifetime_addtime -
1986 1988 wallclock;
1987 1989 } else {
1988 1990 adddelta = TIME_MAX;
1989 1991 }
1990 1992
1991 1993 if (soft->sadb_lifetime_usetime != 0 &&
1992 1994 current->sadb_lifetime_usetime != 0) {
1993 1995 usedelta =
1994 1996 current->sadb_lifetime_usetime +
1995 1997 soft->sadb_lifetime_usetime -
1996 1998 wallclock;
1997 1999 } else {
1998 2000 usedelta = TIME_MAX;
1999 2001 }
2000 2002 (void) fprintf(file, "%s", soft_prefix);
2001 2003 scratch = MIN(adddelta, usedelta);
2002 2004 if (scratch >= 0) {
2003 2005 (void) fprintf(file,
2004 2006 dgettext(TEXT_DOMAIN,
2005 2007 "Soft expiration occurs in %"
2006 2008 PRId64 " seconds%s\n"), scratch,
2007 2009 secs2out(scratch, secs_str,
2008 2010 sizeof (secs_str), SPC_BEGIN));
2009 2011 } else {
2010 2012 (void) fprintf(file,
2011 2013 dgettext(TEXT_DOMAIN,
2012 2014 "Soft expiration occurred\n"));
2013 2015 }
2014 2016 scratch += wallclock;
2015 2017 printsatime(file, scratch, dgettext(TEXT_DOMAIN,
2016 2018 "%sTime of expiration: %s.\n"),
2017 2019 soft_prefix, soft_prefix, vflag);
2018 2020 }
2019 2021 }
2020 2022 }
2021 2023
2022 2024 if (hard != NULL) {
2023 2025 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2024 2026 "%sHard lifetime information:\n"), hard_prefix);
2025 2027 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2026 2028 "%s%" PRIu64 " bytes %sof lifetime, %u allocations.\n"),
2027 2029 hard_prefix,
2028 2030 hard->sadb_lifetime_bytes,
2029 2031 bytecnt2out(hard->sadb_lifetime_bytes, byte_str,
2030 2032 sizeof (byte_str), SPC_END),
2031 2033 hard->sadb_lifetime_allocations);
2032 2034 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2033 2035 "%s%" PRIu64 " seconds %sof post-add lifetime.\n"),
2034 2036 hard_prefix, hard->sadb_lifetime_addtime,
2035 2037 secs2out(hard->sadb_lifetime_addtime, secs_str,
2036 2038 sizeof (secs_str), SPC_END));
2037 2039 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2038 2040 "%s%" PRIu64 " seconds %sof post-use lifetime.\n"),
2039 2041 hard_prefix, hard->sadb_lifetime_usetime,
2040 2042 secs2out(hard->sadb_lifetime_usetime, secs_str,
2041 2043 sizeof (secs_str), SPC_END));
2042 2044 /* If possible, express values as time remaining. */
2043 2045 if (current != NULL) {
2044 2046 if (hard->sadb_lifetime_bytes != 0)
2045 2047 (void) fprintf(file, dgettext(TEXT_DOMAIN, "%s"
2046 2048 "%" PRIu64 " bytes %smore can be "
2047 2049 "protected.\n"), hard_prefix,
2048 2050 (hard->sadb_lifetime_bytes >
2049 2051 current->sadb_lifetime_bytes) ?
2050 2052 hard->sadb_lifetime_bytes -
2051 2053 current->sadb_lifetime_bytes : 0,
2052 2054 (hard->sadb_lifetime_bytes >
2053 2055 current->sadb_lifetime_bytes) ?
2054 2056 bytecnt2out(hard->sadb_lifetime_bytes -
2055 2057 current->sadb_lifetime_bytes, byte_str,
2056 2058 sizeof (byte_str), SPC_END) : "");
2057 2059 if (hard->sadb_lifetime_addtime != 0 ||
2058 2060 (hard->sadb_lifetime_usetime != 0 &&
2059 2061 current->sadb_lifetime_usetime != 0)) {
2060 2062 int64_t adddelta, usedelta;
2061 2063
2062 2064 if (hard->sadb_lifetime_addtime != 0) {
2063 2065 adddelta =
2064 2066 current->sadb_lifetime_addtime +
2065 2067 hard->sadb_lifetime_addtime -
2066 2068 wallclock;
2067 2069 } else {
2068 2070 adddelta = TIME_MAX;
2069 2071 }
2070 2072
2071 2073 if (hard->sadb_lifetime_usetime != 0 &&
2072 2074 current->sadb_lifetime_usetime != 0) {
2073 2075 usedelta =
2074 2076 current->sadb_lifetime_usetime +
2075 2077 hard->sadb_lifetime_usetime -
2076 2078 wallclock;
2077 2079 } else {
2078 2080 usedelta = TIME_MAX;
2079 2081 }
2080 2082 (void) fprintf(file, "%s", hard_prefix);
2081 2083 scratch = MIN(adddelta, usedelta);
2082 2084 if (scratch >= 0) {
2083 2085 (void) fprintf(file,
2084 2086 dgettext(TEXT_DOMAIN,
2085 2087 "Hard expiration occurs in %"
2086 2088 PRId64 " seconds%s\n"), scratch,
2087 2089 secs2out(scratch, secs_str,
2088 2090 sizeof (secs_str), SPC_BEGIN));
2089 2091 } else {
2090 2092 (void) fprintf(file,
2091 2093 dgettext(TEXT_DOMAIN,
2092 2094 "Hard expiration occurred\n"));
2093 2095 }
2094 2096 scratch += wallclock;
2095 2097 printsatime(file, scratch, dgettext(TEXT_DOMAIN,
2096 2098 "%sTime of expiration: %s.\n"),
2097 2099 hard_prefix, hard_prefix, vflag);
2098 2100 }
2099 2101 }
2100 2102 }
2101 2103 if (idle != NULL) {
2102 2104 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2103 2105 "%sIdle lifetime information:\n"), idle_prefix);
2104 2106 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2105 2107 "%s%" PRIu64 " seconds %sof post-add lifetime.\n"),
2106 2108 idle_prefix, idle->sadb_lifetime_addtime,
2107 2109 secs2out(idle->sadb_lifetime_addtime, secs_str,
2108 2110 sizeof (secs_str), SPC_END));
2109 2111 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2110 2112 "%s%" PRIu64 " seconds %sof post-use lifetime.\n"),
2111 2113 idle_prefix, idle->sadb_lifetime_usetime,
2112 2114 secs2out(idle->sadb_lifetime_usetime, secs_str,
2113 2115 sizeof (secs_str), SPC_END));
2114 2116 }
2115 2117 }
2116 2118
2117 2119 /*
2118 2120 * Print an SADB_EXT_ADDRESS_* extension.
2119 2121 */
2120 2122 void
2121 2123 print_address(FILE *file, char *prefix, struct sadb_address *addr,
2122 2124 boolean_t ignore_nss)
2123 2125 {
2124 2126 struct protoent *pe;
2125 2127
2126 2128 (void) fprintf(file, "%s", prefix);
2127 2129 switch (addr->sadb_address_exttype) {
2128 2130 case SADB_EXT_ADDRESS_SRC:
2129 2131 (void) fprintf(file, dgettext(TEXT_DOMAIN, "Source address "));
2130 2132 break;
2131 2133 case SADB_X_EXT_ADDRESS_INNER_SRC:
2132 2134 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2133 2135 "Inner source address "));
2134 2136 break;
2135 2137 case SADB_EXT_ADDRESS_DST:
2136 2138 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2137 2139 "Destination address "));
2138 2140 break;
2139 2141 case SADB_X_EXT_ADDRESS_INNER_DST:
2140 2142 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2141 2143 "Inner destination address "));
2142 2144 break;
2143 2145 case SADB_X_EXT_ADDRESS_NATT_LOC:
2144 2146 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2145 2147 "NAT-T local address "));
2146 2148 break;
2147 2149 case SADB_X_EXT_ADDRESS_NATT_REM:
2148 2150 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2149 2151 "NAT-T remote address "));
2150 2152 break;
2151 2153 }
2152 2154
2153 2155 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2154 2156 "(proto=%d"), addr->sadb_address_proto);
2155 2157 if (ignore_nss == B_FALSE) {
2156 2158 if (addr->sadb_address_proto == 0) {
2157 2159 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2158 2160 "/<unspecified>"));
2159 2161 } else if ((pe = getprotobynumber(addr->sadb_address_proto))
2160 2162 != NULL) {
2161 2163 (void) fprintf(file, "/%s", pe->p_name);
2162 2164 } else {
2163 2165 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2164 2166 "/<unknown>"));
2165 2167 }
2166 2168 }
2167 2169 (void) fprintf(file, dgettext(TEXT_DOMAIN, ")\n%s"), prefix);
2168 2170 (void) dump_sockaddr((struct sockaddr *)(addr + 1),
2169 2171 addr->sadb_address_prefixlen, B_FALSE, file, ignore_nss);
2170 2172 }
2171 2173
2172 2174 /*
2173 2175 * Print an SADB_EXT_KEY extension.
2174 2176 */
2175 2177 void
2176 2178 print_key(FILE *file, char *prefix, struct sadb_key *key)
2177 2179 {
2178 2180 (void) fprintf(file, "%s", prefix);
2179 2181
2180 2182 switch (key->sadb_key_exttype) {
2181 2183 case SADB_EXT_KEY_AUTH:
2182 2184 (void) fprintf(file, dgettext(TEXT_DOMAIN, "Authentication"));
2183 2185 break;
2184 2186 case SADB_EXT_KEY_ENCRYPT:
2185 2187 (void) fprintf(file, dgettext(TEXT_DOMAIN, "Encryption"));
2186 2188 break;
2187 2189 }
2188 2190
2189 2191 (void) fprintf(file, dgettext(TEXT_DOMAIN, " key.\n%s"), prefix);
2190 2192 (void) dump_key((uint8_t *)(key + 1), key->sadb_key_bits,
2191 2193 key->sadb_key_reserved, file, B_TRUE);
2192 2194 (void) fprintf(file, "\n");
2193 2195 }
2194 2196
2195 2197 /*
2196 2198 * Print an SADB_EXT_IDENTITY_* extension.
2197 2199 */
2198 2200 void
2199 2201 print_ident(FILE *file, char *prefix, struct sadb_ident *id)
2200 2202 {
2201 2203 boolean_t canprint = B_TRUE;
2202 2204
2203 2205 (void) fprintf(file, "%s", prefix);
2204 2206 switch (id->sadb_ident_exttype) {
2205 2207 case SADB_EXT_IDENTITY_SRC:
2206 2208 (void) fprintf(file, dgettext(TEXT_DOMAIN, "Source"));
2207 2209 break;
2208 2210 case SADB_EXT_IDENTITY_DST:
2209 2211 (void) fprintf(file, dgettext(TEXT_DOMAIN, "Destination"));
2210 2212 break;
2211 2213 }
2212 2214
2213 2215 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2214 2216 " identity, uid=%d, type "), id->sadb_ident_id);
2215 2217 canprint = dump_sadb_idtype(id->sadb_ident_type, file, NULL);
2216 2218 (void) fprintf(file, "\n%s", prefix);
2217 2219 if (canprint) {
2218 2220 (void) fprintf(file, "%s\n", (char *)(id + 1));
2219 2221 } else {
2220 2222 print_asn1_name(file, (const unsigned char *)(id + 1),
2221 2223 SADB_64TO8(id->sadb_ident_len) - sizeof (sadb_ident_t));
2222 2224 }
2223 2225 }
2224 2226
2225 2227 /*
2226 2228 * Convert sadb_sens extension into binary security label.
2227 2229 */
2228 2230
2229 2231 #include <tsol/label.h>
2230 2232 #include <sys/tsol/tndb.h>
2231 2233 #include <sys/tsol/label_macro.h>
2232 2234
2233 2235 void
2234 2236 ipsec_convert_sens_to_bslabel(const struct sadb_sens *sens, bslabel_t *sl)
2235 2237 {
2236 2238 uint64_t *bitmap = (uint64_t *)(sens + 1);
2237 2239 int bitmap_len = SADB_64TO8(sens->sadb_sens_sens_len);
2238 2240
2239 2241 bsllow(sl);
2240 2242 LCLASS_SET((_bslabel_impl_t *)sl, sens->sadb_sens_sens_level);
2241 2243 bcopy(bitmap, &((_bslabel_impl_t *)sl)->compartments,
2242 2244 bitmap_len);
2243 2245 }
2244 2246
2245 2247 void
2246 2248 ipsec_convert_bslabel_to_string(bslabel_t *sl, char **plabel)
2247 2249 {
2248 2250 if (label_to_str(sl, plabel, M_LABEL, DEF_NAMES) != 0) {
2249 2251 *plabel = strdup(dgettext(TEXT_DOMAIN,
2250 2252 "** Label conversion failed **"));
2251 2253 }
2252 2254 }
2253 2255
2254 2256 void
2255 2257 ipsec_convert_bslabel_to_hex(bslabel_t *sl, char **plabel)
2256 2258 {
2257 2259 if (label_to_str(sl, plabel, M_INTERNAL, DEF_NAMES) != 0) {
2258 2260 *plabel = strdup(dgettext(TEXT_DOMAIN,
2259 2261 "** Label conversion failed **"));
2260 2262 }
2261 2263 }
2262 2264
2263 2265 int
2264 2266 ipsec_convert_sl_to_sens(int doi, bslabel_t *sl, sadb_sens_t *sens)
2265 2267 {
2266 2268 uint8_t *bitmap;
2267 2269 int sens_len = sizeof (sadb_sens_t) + _C_LEN * 4;
2268 2270
2269 2271
2270 2272 if (sens == NULL)
2271 2273 return (sens_len);
2272 2274
2273 2275
2274 2276 (void) memset(sens, 0, sens_len);
2275 2277
2276 2278 sens->sadb_sens_exttype = SADB_EXT_SENSITIVITY;
2277 2279 sens->sadb_sens_len = SADB_8TO64(sens_len);
2278 2280 sens->sadb_sens_dpd = doi;
2279 2281
2280 2282 sens->sadb_sens_sens_level = LCLASS(sl);
2281 2283 sens->sadb_sens_integ_level = 0;
2282 2284 sens->sadb_sens_sens_len = _C_LEN >> 1;
2283 2285 sens->sadb_sens_integ_len = 0;
2284 2286
2285 2287 sens->sadb_x_sens_flags = 0;
2286 2288
2287 2289 bitmap = (uint8_t *)(sens + 1);
2288 2290 bcopy(&(((_bslabel_impl_t *)sl)->compartments), bitmap, _C_LEN * 4);
2289 2291
2290 2292 return (sens_len);
2291 2293 }
2292 2294
2293 2295
2294 2296 /*
2295 2297 * Print an SADB_SENSITIVITY extension.
2296 2298 */
2297 2299 void
2298 2300 print_sens(FILE *file, char *prefix, const struct sadb_sens *sens,
2299 2301 boolean_t ignore_nss)
2300 2302 {
2301 2303 char *plabel;
2302 2304 char *hlabel;
2303 2305 uint64_t *bitmap = (uint64_t *)(sens + 1);
2304 2306 bslabel_t sl;
2305 2307 int i;
2306 2308 int sens_len = sens->sadb_sens_sens_len;
2307 2309 int integ_len = sens->sadb_sens_integ_len;
2308 2310 boolean_t inner = (sens->sadb_sens_exttype == SADB_EXT_SENSITIVITY);
2309 2311 const char *sensname = inner ?
2310 2312 dgettext(TEXT_DOMAIN, "Plaintext Sensitivity") :
2311 2313 dgettext(TEXT_DOMAIN, "Ciphertext Sensitivity");
2312 2314
2313 2315 ipsec_convert_sens_to_bslabel(sens, &sl);
2314 2316
2315 2317 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2316 2318 "%s%s DPD %d, sens level=%d, integ level=%d, flags=%x\n"),
2317 2319 prefix, sensname, sens->sadb_sens_dpd, sens->sadb_sens_sens_level,
2318 2320 sens->sadb_sens_integ_level, sens->sadb_x_sens_flags);
2319 2321
2320 2322 ipsec_convert_bslabel_to_hex(&sl, &hlabel);
2321 2323
2322 2324 if (ignore_nss) {
2323 2325 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2324 2326 "%s %s Label: %s\n"), prefix, sensname, hlabel);
2325 2327
2326 2328 for (i = 0; i < sens_len; i++, bitmap++)
2327 2329 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2328 2330 "%s %s BM extended word %d 0x%" PRIx64 "\n"),
2329 2331 prefix, sensname, i, *bitmap);
2330 2332
2331 2333 } else {
2332 2334 ipsec_convert_bslabel_to_string(&sl, &plabel);
2333 2335
2334 2336 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2335 2337 "%s %s Label: %s (%s)\n"),
2336 2338 prefix, sensname, plabel, hlabel);
2337 2339 free(plabel);
2338 2340
2339 2341 }
2340 2342 free(hlabel);
2341 2343
2342 2344 bitmap = (uint64_t *)(sens + 1 + sens_len);
2343 2345
2344 2346 for (i = 0; i < integ_len; i++, bitmap++)
2345 2347 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2346 2348 "%s Integrity BM extended word %d 0x%" PRIx64 "\n"),
2347 2349 prefix, i, *bitmap);
2348 2350 }
2349 2351
2350 2352 /*
2351 2353 * Print an SADB_EXT_PROPOSAL extension.
2352 2354 */
2353 2355 void
2354 2356 print_prop(FILE *file, char *prefix, struct sadb_prop *prop)
2355 2357 {
2356 2358 struct sadb_comb *combs;
2357 2359 int i, numcombs;
2358 2360
2359 2361 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2360 2362 "%sProposal, replay counter = %u.\n"), prefix,
2361 2363 prop->sadb_prop_replay);
2362 2364
2363 2365 numcombs = prop->sadb_prop_len - SADB_8TO64(sizeof (*prop));
2364 2366 numcombs /= SADB_8TO64(sizeof (*combs));
2365 2367
2366 2368 combs = (struct sadb_comb *)(prop + 1);
2367 2369
2368 2370 for (i = 0; i < numcombs; i++) {
2369 2371 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2370 2372 "%s Combination #%u "), prefix, i + 1);
2371 2373 if (combs[i].sadb_comb_auth != SADB_AALG_NONE) {
2372 2374 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2373 2375 "Authentication = "));
2374 2376 (void) dump_aalg(combs[i].sadb_comb_auth, file);
2375 2377 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2376 2378 " minbits=%u, maxbits=%u.\n%s "),
2377 2379 combs[i].sadb_comb_auth_minbits,
2378 2380 combs[i].sadb_comb_auth_maxbits, prefix);
2379 2381 }
2380 2382
2381 2383 if (combs[i].sadb_comb_encrypt != SADB_EALG_NONE) {
2382 2384 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2383 2385 "Encryption = "));
2384 2386 (void) dump_ealg(combs[i].sadb_comb_encrypt, file);
2385 2387 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2386 2388 " minbits=%u, maxbits=%u.\n%s "),
2387 2389 combs[i].sadb_comb_encrypt_minbits,
2388 2390 combs[i].sadb_comb_encrypt_maxbits, prefix);
2389 2391 }
2390 2392
2391 2393 (void) fprintf(file, dgettext(TEXT_DOMAIN, "HARD: "));
2392 2394 if (combs[i].sadb_comb_hard_allocations)
2393 2395 (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "),
2394 2396 combs[i].sadb_comb_hard_allocations);
2395 2397 if (combs[i].sadb_comb_hard_bytes)
2396 2398 (void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%"
2397 2399 PRIu64 " "), combs[i].sadb_comb_hard_bytes);
2398 2400 if (combs[i].sadb_comb_hard_addtime)
2399 2401 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2400 2402 "post-add secs=%" PRIu64 " "),
2401 2403 combs[i].sadb_comb_hard_addtime);
2402 2404 if (combs[i].sadb_comb_hard_usetime)
2403 2405 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2404 2406 "post-use secs=%" PRIu64 ""),
2405 2407 combs[i].sadb_comb_hard_usetime);
2406 2408
2407 2409 (void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%s SOFT: "),
2408 2410 prefix);
2409 2411 if (combs[i].sadb_comb_soft_allocations)
2410 2412 (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "),
2411 2413 combs[i].sadb_comb_soft_allocations);
2412 2414 if (combs[i].sadb_comb_soft_bytes)
2413 2415 (void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%"
2414 2416 PRIu64 " "), combs[i].sadb_comb_soft_bytes);
2415 2417 if (combs[i].sadb_comb_soft_addtime)
2416 2418 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2417 2419 "post-add secs=%" PRIu64 " "),
2418 2420 combs[i].sadb_comb_soft_addtime);
2419 2421 if (combs[i].sadb_comb_soft_usetime)
2420 2422 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2421 2423 "post-use secs=%" PRIu64 ""),
2422 2424 combs[i].sadb_comb_soft_usetime);
2423 2425 (void) fprintf(file, "\n");
2424 2426 }
2425 2427 }
2426 2428
2427 2429 /*
2428 2430 * Print an extended proposal (SADB_X_EXT_EPROP).
2429 2431 */
2430 2432 void
2431 2433 print_eprop(FILE *file, char *prefix, struct sadb_prop *eprop)
2432 2434 {
2433 2435 uint64_t *sofar;
2434 2436 struct sadb_x_ecomb *ecomb;
2435 2437 struct sadb_x_algdesc *algdesc;
2436 2438 int i, j;
2437 2439
2438 2440 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2439 2441 "%sExtended Proposal, replay counter = %u, "), prefix,
2440 2442 eprop->sadb_prop_replay);
2441 2443 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2442 2444 "number of combinations = %u.\n"), eprop->sadb_x_prop_numecombs);
2443 2445
2444 2446 sofar = (uint64_t *)(eprop + 1);
2445 2447 ecomb = (struct sadb_x_ecomb *)sofar;
2446 2448
2447 2449 for (i = 0; i < eprop->sadb_x_prop_numecombs; ) {
2448 2450 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2449 2451 "%s Extended combination #%u:\n"), prefix, ++i);
2450 2452
2451 2453 (void) fprintf(file, dgettext(TEXT_DOMAIN, "%s HARD: "),
2452 2454 prefix);
2453 2455 (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "),
2454 2456 ecomb->sadb_x_ecomb_hard_allocations);
2455 2457 (void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%" PRIu64
2456 2458 ", "), ecomb->sadb_x_ecomb_hard_bytes);
2457 2459 (void) fprintf(file, dgettext(TEXT_DOMAIN, "post-add secs=%"
2458 2460 PRIu64 ", "), ecomb->sadb_x_ecomb_hard_addtime);
2459 2461 (void) fprintf(file, dgettext(TEXT_DOMAIN, "post-use secs=%"
2460 2462 PRIu64 "\n"), ecomb->sadb_x_ecomb_hard_usetime);
2461 2463
2462 2464 (void) fprintf(file, dgettext(TEXT_DOMAIN, "%s SOFT: "),
2463 2465 prefix);
2464 2466 (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "),
2465 2467 ecomb->sadb_x_ecomb_soft_allocations);
2466 2468 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2467 2469 "bytes=%" PRIu64 ", "), ecomb->sadb_x_ecomb_soft_bytes);
2468 2470 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2469 2471 "post-add secs=%" PRIu64 ", "),
2470 2472 ecomb->sadb_x_ecomb_soft_addtime);
2471 2473 (void) fprintf(file, dgettext(TEXT_DOMAIN, "post-use secs=%"
2472 2474 PRIu64 "\n"), ecomb->sadb_x_ecomb_soft_usetime);
2473 2475
2474 2476 sofar = (uint64_t *)(ecomb + 1);
2475 2477 algdesc = (struct sadb_x_algdesc *)sofar;
2476 2478
2477 2479 for (j = 0; j < ecomb->sadb_x_ecomb_numalgs; ) {
2478 2480 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2479 2481 "%s Alg #%u "), prefix, ++j);
2480 2482 switch (algdesc->sadb_x_algdesc_satype) {
2481 2483 case SADB_SATYPE_ESP:
2482 2484 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2483 2485 "for ESP "));
2484 2486 break;
2485 2487 case SADB_SATYPE_AH:
2486 2488 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2487 2489 "for AH "));
2488 2490 break;
2489 2491 default:
2490 2492 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2491 2493 "for satype=%d "),
2492 2494 algdesc->sadb_x_algdesc_satype);
2493 2495 }
2494 2496 switch (algdesc->sadb_x_algdesc_algtype) {
2495 2497 case SADB_X_ALGTYPE_CRYPT:
2496 2498 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2497 2499 "Encryption = "));
2498 2500 (void) dump_ealg(algdesc->sadb_x_algdesc_alg,
2499 2501 file);
2500 2502 break;
2501 2503 case SADB_X_ALGTYPE_AUTH:
2502 2504 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2503 2505 "Authentication = "));
2504 2506 (void) dump_aalg(algdesc->sadb_x_algdesc_alg,
2505 2507 file);
2506 2508 break;
2507 2509 default:
2508 2510 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2509 2511 "algtype(%d) = alg(%d)"),
2510 2512 algdesc->sadb_x_algdesc_algtype,
2511 2513 algdesc->sadb_x_algdesc_alg);
2512 2514 break;
2513 2515 }
2514 2516
2515 2517 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2516 2518 " minbits=%u, maxbits=%u, saltbits=%u\n"),
2517 2519 algdesc->sadb_x_algdesc_minbits,
2518 2520 algdesc->sadb_x_algdesc_maxbits,
2519 2521 algdesc->sadb_x_algdesc_reserved);
2520 2522
2521 2523 sofar = (uint64_t *)(++algdesc);
2522 2524 }
2523 2525 ecomb = (struct sadb_x_ecomb *)sofar;
2524 2526 }
2525 2527 }
2526 2528
2527 2529 /*
2528 2530 * Print an SADB_EXT_SUPPORTED extension.
2529 2531 */
2530 2532 void
2531 2533 print_supp(FILE *file, char *prefix, struct sadb_supported *supp)
2532 2534 {
2533 2535 struct sadb_alg *algs;
2534 2536 int i, numalgs;
2535 2537
2536 2538 (void) fprintf(file, dgettext(TEXT_DOMAIN, "%sSupported "), prefix);
2537 2539 switch (supp->sadb_supported_exttype) {
2538 2540 case SADB_EXT_SUPPORTED_AUTH:
2539 2541 (void) fprintf(file, dgettext(TEXT_DOMAIN, "authentication"));
2540 2542 break;
2541 2543 case SADB_EXT_SUPPORTED_ENCRYPT:
2542 2544 (void) fprintf(file, dgettext(TEXT_DOMAIN, "encryption"));
2543 2545 break;
2544 2546 }
2545 2547 (void) fprintf(file, dgettext(TEXT_DOMAIN, " algorithms.\n"));
2546 2548
2547 2549 algs = (struct sadb_alg *)(supp + 1);
2548 2550 numalgs = supp->sadb_supported_len - SADB_8TO64(sizeof (*supp));
2549 2551 numalgs /= SADB_8TO64(sizeof (*algs));
2550 2552 for (i = 0; i < numalgs; i++) {
2551 2553 uint16_t exttype = supp->sadb_supported_exttype;
2552 2554
2553 2555 (void) fprintf(file, "%s", prefix);
2554 2556 switch (exttype) {
2555 2557 case SADB_EXT_SUPPORTED_AUTH:
2556 2558 (void) dump_aalg(algs[i].sadb_alg_id, file);
2557 2559 break;
2558 2560 case SADB_EXT_SUPPORTED_ENCRYPT:
2559 2561 (void) dump_ealg(algs[i].sadb_alg_id, file);
2560 2562 break;
2561 2563 }
2562 2564 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2563 2565 " minbits=%u, maxbits=%u, ivlen=%u, saltbits=%u"),
2564 2566 algs[i].sadb_alg_minbits, algs[i].sadb_alg_maxbits,
2565 2567 algs[i].sadb_alg_ivlen, algs[i].sadb_x_alg_saltbits);
2566 2568 if (exttype == SADB_EXT_SUPPORTED_ENCRYPT)
2567 2569 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2568 2570 ", increment=%u"), algs[i].sadb_x_alg_increment);
2569 2571 (void) fprintf(file, dgettext(TEXT_DOMAIN, ".\n"));
2570 2572 }
2571 2573 }
2572 2574
2573 2575 /*
2574 2576 * Print an SADB_EXT_SPIRANGE extension.
2575 2577 */
2576 2578 void
2577 2579 print_spirange(FILE *file, char *prefix, struct sadb_spirange *range)
2578 2580 {
2579 2581 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2580 2582 "%sSPI Range, min=0x%x, max=0x%x\n"), prefix,
2581 2583 htonl(range->sadb_spirange_min),
2582 2584 htonl(range->sadb_spirange_max));
2583 2585 }
2584 2586
2585 2587 /*
2586 2588 * Print an SADB_X_EXT_KM_COOKIE extension.
2587 2589 */
2588 2590
2589 2591 void
2590 2592 print_kmc(FILE *file, char *prefix, struct sadb_x_kmc *kmc)
2591 2593 {
2592 2594 char *cookie_label;
2593 2595
2594 2596 if ((cookie_label = kmc_lookup_by_cookie(kmc->sadb_x_kmc_cookie)) ==
2595 2597 NULL)
2596 2598 cookie_label = dgettext(TEXT_DOMAIN, "<Label not found.>");
2597 2599
2598 2600 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2599 2601 "%sProtocol %u, cookie=\"%s\" (%u)\n"), prefix,
2600 2602 kmc->sadb_x_kmc_proto, cookie_label, kmc->sadb_x_kmc_cookie);
2601 2603 }
2602 2604
2603 2605 /*
2604 2606 * Print an SADB_X_EXT_REPLAY_CTR extension.
2605 2607 */
2606 2608
2607 2609 void
2608 2610 print_replay(FILE *file, char *prefix, sadb_x_replay_ctr_t *repl)
2609 2611 {
2610 2612 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2611 2613 "%sReplay Value "), prefix);
2612 2614 if ((repl->sadb_x_rc_replay32 == 0) &&
2613 2615 (repl->sadb_x_rc_replay64 == 0)) {
2614 2616 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2615 2617 "<Value not found.>"));
2616 2618 }
2617 2619 /*
2618 2620 * We currently do not support a 64-bit replay value.
2619 2621 * RFC 4301 will require one, however, and we have a field
2620 2622 * in place when 4301 is built.
2621 2623 */
2622 2624 (void) fprintf(file, "% " PRIu64 "\n",
2623 2625 ((repl->sadb_x_rc_replay32 == 0) ?
2624 2626 repl->sadb_x_rc_replay64 : repl->sadb_x_rc_replay32));
2625 2627 }
2626 2628 /*
2627 2629 * Print an SADB_X_EXT_PAIR extension.
2628 2630 */
2629 2631 static void
2630 2632 print_pair(FILE *file, char *prefix, struct sadb_x_pair *pair)
2631 2633 {
2632 2634 (void) fprintf(file, dgettext(TEXT_DOMAIN, "%sPaired with spi=0x%x\n"),
2633 2635 prefix, ntohl(pair->sadb_x_pair_spi));
2634 2636 }
2635 2637
2636 2638 /*
2637 2639 * Take a PF_KEY message pointed to buffer and print it. Useful for DUMP
2638 2640 * and GET.
2639 2641 */
2640 2642 void
2641 2643 print_samsg(FILE *file, uint64_t *buffer, boolean_t want_timestamp,
2642 2644 boolean_t vflag, boolean_t ignore_nss)
2643 2645 {
2644 2646 uint64_t *current;
2645 2647 struct sadb_msg *samsg = (struct sadb_msg *)buffer;
2646 2648 struct sadb_ext *ext;
2647 2649 struct sadb_lifetime *currentlt = NULL, *hardlt = NULL, *softlt = NULL;
2648 2650 struct sadb_lifetime *idlelt = NULL;
2649 2651 int i;
2650 2652 time_t wallclock;
2651 2653
2652 2654 (void) time(&wallclock);
2653 2655
2654 2656 print_sadb_msg(file, samsg, want_timestamp ? wallclock : 0, vflag);
2655 2657 current = (uint64_t *)(samsg + 1);
2656 2658 while (current - buffer < samsg->sadb_msg_len) {
2657 2659 int lenbytes;
2658 2660
2659 2661 ext = (struct sadb_ext *)current;
2660 2662 lenbytes = SADB_64TO8(ext->sadb_ext_len);
2661 2663 switch (ext->sadb_ext_type) {
2662 2664 case SADB_EXT_SA:
2663 2665 print_sa(file, dgettext(TEXT_DOMAIN,
2664 2666 "SA: "), (struct sadb_sa *)current);
2665 2667 break;
2666 2668 /*
2667 2669 * Pluck out lifetimes and print them at the end. This is
2668 2670 * to show relative lifetimes.
2669 2671 */
2670 2672 case SADB_EXT_LIFETIME_CURRENT:
2671 2673 currentlt = (struct sadb_lifetime *)current;
2672 2674 break;
2673 2675 case SADB_EXT_LIFETIME_HARD:
2674 2676 hardlt = (struct sadb_lifetime *)current;
2675 2677 break;
2676 2678 case SADB_EXT_LIFETIME_SOFT:
2677 2679 softlt = (struct sadb_lifetime *)current;
2678 2680 break;
2679 2681 case SADB_X_EXT_LIFETIME_IDLE:
2680 2682 idlelt = (struct sadb_lifetime *)current;
2681 2683 break;
2682 2684
2683 2685 case SADB_EXT_ADDRESS_SRC:
2684 2686 print_address(file, dgettext(TEXT_DOMAIN, "SRC: "),
2685 2687 (struct sadb_address *)current, ignore_nss);
2686 2688 break;
2687 2689 case SADB_X_EXT_ADDRESS_INNER_SRC:
2688 2690 print_address(file, dgettext(TEXT_DOMAIN, "INS: "),
2689 2691 (struct sadb_address *)current, ignore_nss);
2690 2692 break;
2691 2693 case SADB_EXT_ADDRESS_DST:
2692 2694 print_address(file, dgettext(TEXT_DOMAIN, "DST: "),
2693 2695 (struct sadb_address *)current, ignore_nss);
2694 2696 break;
2695 2697 case SADB_X_EXT_ADDRESS_INNER_DST:
2696 2698 print_address(file, dgettext(TEXT_DOMAIN, "IND: "),
2697 2699 (struct sadb_address *)current, ignore_nss);
2698 2700 break;
2699 2701 case SADB_EXT_KEY_AUTH:
2700 2702 print_key(file, dgettext(TEXT_DOMAIN,
2701 2703 "AKY: "), (struct sadb_key *)current);
2702 2704 break;
2703 2705 case SADB_EXT_KEY_ENCRYPT:
2704 2706 print_key(file, dgettext(TEXT_DOMAIN,
2705 2707 "EKY: "), (struct sadb_key *)current);
2706 2708 break;
2707 2709 case SADB_EXT_IDENTITY_SRC:
2708 2710 print_ident(file, dgettext(TEXT_DOMAIN, "SID: "),
2709 2711 (struct sadb_ident *)current);
2710 2712 break;
2711 2713 case SADB_EXT_IDENTITY_DST:
2712 2714 print_ident(file, dgettext(TEXT_DOMAIN, "DID: "),
2713 2715 (struct sadb_ident *)current);
2714 2716 break;
2715 2717 case SADB_EXT_SENSITIVITY:
2716 2718 print_sens(file, dgettext(TEXT_DOMAIN, "SNS: "),
2717 2719 (struct sadb_sens *)current, ignore_nss);
2718 2720 break;
2719 2721 case SADB_EXT_PROPOSAL:
2720 2722 print_prop(file, dgettext(TEXT_DOMAIN, "PRP: "),
2721 2723 (struct sadb_prop *)current);
2722 2724 break;
2723 2725 case SADB_EXT_SUPPORTED_AUTH:
2724 2726 print_supp(file, dgettext(TEXT_DOMAIN, "SUA: "),
2725 2727 (struct sadb_supported *)current);
2726 2728 break;
2727 2729 case SADB_EXT_SUPPORTED_ENCRYPT:
2728 2730 print_supp(file, dgettext(TEXT_DOMAIN, "SUE: "),
2729 2731 (struct sadb_supported *)current);
2730 2732 break;
2731 2733 case SADB_EXT_SPIRANGE:
2732 2734 print_spirange(file, dgettext(TEXT_DOMAIN, "SPR: "),
2733 2735 (struct sadb_spirange *)current);
2734 2736 break;
2735 2737 case SADB_X_EXT_EPROP:
2736 2738 print_eprop(file, dgettext(TEXT_DOMAIN, "EPR: "),
2737 2739 (struct sadb_prop *)current);
2738 2740 break;
2739 2741 case SADB_X_EXT_KM_COOKIE:
2740 2742 print_kmc(file, dgettext(TEXT_DOMAIN, "KMC: "),
2741 2743 (struct sadb_x_kmc *)current);
2742 2744 break;
2743 2745 case SADB_X_EXT_ADDRESS_NATT_REM:
2744 2746 print_address(file, dgettext(TEXT_DOMAIN, "NRM: "),
2745 2747 (struct sadb_address *)current, ignore_nss);
2746 2748 break;
2747 2749 case SADB_X_EXT_ADDRESS_NATT_LOC:
2748 2750 print_address(file, dgettext(TEXT_DOMAIN, "NLC: "),
2749 2751 (struct sadb_address *)current, ignore_nss);
2750 2752 break;
2751 2753 case SADB_X_EXT_PAIR:
2752 2754 print_pair(file, dgettext(TEXT_DOMAIN, "OTH: "),
2753 2755 (struct sadb_x_pair *)current);
2754 2756 break;
2755 2757 case SADB_X_EXT_OUTER_SENS:
2756 2758 print_sens(file, dgettext(TEXT_DOMAIN, "OSN: "),
2757 2759 (struct sadb_sens *)current, ignore_nss);
2758 2760 break;
2759 2761 case SADB_X_EXT_REPLAY_VALUE:
2760 2762 (void) print_replay(file, dgettext(TEXT_DOMAIN,
2761 2763 "RPL: "), (sadb_x_replay_ctr_t *)current);
2762 2764 break;
2763 2765 default:
2764 2766 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2765 2767 "UNK: Unknown ext. %d, len %d.\n"),
2766 2768 ext->sadb_ext_type, lenbytes);
2767 2769 for (i = 0; i < ext->sadb_ext_len; i++)
2768 2770 (void) fprintf(file, dgettext(TEXT_DOMAIN,
2769 2771 "UNK: 0x%" PRIx64 "\n"),
2770 2772 ((uint64_t *)ext)[i]);
2771 2773 break;
2772 2774 }
2773 2775 current += (lenbytes == 0) ?
2774 2776 SADB_8TO64(sizeof (struct sadb_ext)) : ext->sadb_ext_len;
2775 2777 }
2776 2778 /*
2777 2779 * Print lifetimes NOW.
2778 2780 */
2779 2781 if (currentlt != NULL || hardlt != NULL || softlt != NULL ||
2780 2782 idlelt != NULL)
2781 2783 print_lifetimes(file, wallclock, currentlt, hardlt,
2782 2784 softlt, idlelt, vflag);
2783 2785
2784 2786 if (current - buffer != samsg->sadb_msg_len) {
2785 2787 warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
2786 2788 "WARNING: insufficient buffer space or corrupt message."));
2787 2789 }
2788 2790
2789 2791 (void) fflush(file); /* Make sure our message is out there. */
2790 2792 }
2791 2793
2792 2794 /*
2793 2795 * save_XXX functions are used when "saving" the SA tables to either a
2794 2796 * file or standard output. They use the dump_XXX functions where needed,
2795 2797 * but mostly they use the rparseXXX functions.
2796 2798 */
2797 2799
2798 2800 /*
2799 2801 * Print save information for a lifetime extension.
2800 2802 *
2801 2803 * NOTE : It saves the lifetime in absolute terms. For example, if you
2802 2804 * had a hard_usetime of 60 seconds, you'll save it as 60 seconds, even though
2803 2805 * there may have been 59 seconds burned off the clock.
2804 2806 */
2805 2807 boolean_t
2806 2808 save_lifetime(struct sadb_lifetime *lifetime, FILE *ofile)
2807 2809 {
2808 2810 char *prefix;
2809 2811
2810 2812 switch (lifetime->sadb_lifetime_exttype) {
2811 2813 case SADB_EXT_LIFETIME_HARD:
2812 2814 prefix = "hard";
2813 2815 break;
2814 2816 case SADB_EXT_LIFETIME_SOFT:
2815 2817 prefix = "soft";
2816 2818 break;
2817 2819 case SADB_X_EXT_LIFETIME_IDLE:
2818 2820 prefix = "idle";
2819 2821 break;
2820 2822 }
2821 2823
2822 2824 if (putc('\t', ofile) == EOF)
2823 2825 return (B_FALSE);
2824 2826
2825 2827 if (lifetime->sadb_lifetime_allocations != 0 && fprintf(ofile,
2826 2828 "%s_alloc %u ", prefix, lifetime->sadb_lifetime_allocations) < 0)
2827 2829 return (B_FALSE);
2828 2830
2829 2831 if (lifetime->sadb_lifetime_bytes != 0 && fprintf(ofile,
2830 2832 "%s_bytes %" PRIu64 " ", prefix, lifetime->sadb_lifetime_bytes) < 0)
2831 2833 return (B_FALSE);
2832 2834
2833 2835 if (lifetime->sadb_lifetime_addtime != 0 && fprintf(ofile,
2834 2836 "%s_addtime %" PRIu64 " ", prefix,
2835 2837 lifetime->sadb_lifetime_addtime) < 0)
2836 2838 return (B_FALSE);
2837 2839
2838 2840 if (lifetime->sadb_lifetime_usetime != 0 && fprintf(ofile,
2839 2841 "%s_usetime %" PRIu64 " ", prefix,
2840 2842 lifetime->sadb_lifetime_usetime) < 0)
2841 2843 return (B_FALSE);
2842 2844
2843 2845 return (B_TRUE);
2844 2846 }
2845 2847
2846 2848 /*
2847 2849 * Print save information for an address extension.
2848 2850 */
2849 2851 boolean_t
2850 2852 save_address(struct sadb_address *addr, FILE *ofile)
2851 2853 {
2852 2854 char *printable_addr, buf[INET6_ADDRSTRLEN];
2853 2855 const char *prefix, *pprefix;
2854 2856 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(addr + 1);
2855 2857 struct sockaddr_in *sin = (struct sockaddr_in *)sin6;
2856 2858 int af = sin->sin_family;
2857 2859
2858 2860 /*
2859 2861 * Address-family reality check.
2860 2862 */
2861 2863 if (af != AF_INET6 && af != AF_INET)
2862 2864 return (B_FALSE);
2863 2865
2864 2866 switch (addr->sadb_address_exttype) {
2865 2867 case SADB_EXT_ADDRESS_SRC:
2866 2868 prefix = "src";
2867 2869 pprefix = "sport";
2868 2870 break;
2869 2871 case SADB_X_EXT_ADDRESS_INNER_SRC:
2870 2872 prefix = "isrc";
2871 2873 pprefix = "isport";
2872 2874 break;
2873 2875 case SADB_EXT_ADDRESS_DST:
2874 2876 prefix = "dst";
2875 2877 pprefix = "dport";
2876 2878 break;
2877 2879 case SADB_X_EXT_ADDRESS_INNER_DST:
2878 2880 prefix = "idst";
2879 2881 pprefix = "idport";
2880 2882 break;
2881 2883 case SADB_X_EXT_ADDRESS_NATT_LOC:
2882 2884 prefix = "nat_loc ";
2883 2885 pprefix = "nat_lport";
2884 2886 break;
2885 2887 case SADB_X_EXT_ADDRESS_NATT_REM:
2886 2888 prefix = "nat_rem ";
2887 2889 pprefix = "nat_rport";
2888 2890 break;
2889 2891 }
2890 2892
2891 2893 if (fprintf(ofile, " %s ", prefix) < 0)
2892 2894 return (B_FALSE);
2893 2895
2894 2896 /*
2895 2897 * Do not do address-to-name translation, given that we live in
2896 2898 * an age of names that explode into many addresses.
2897 2899 */
2898 2900 printable_addr = (char *)inet_ntop(af,
2899 2901 (af == AF_INET) ? (char *)&sin->sin_addr : (char *)&sin6->sin6_addr,
2900 2902 buf, sizeof (buf));
2901 2903 if (printable_addr == NULL)
2902 2904 printable_addr = "Invalid IP address.";
2903 2905 if (fprintf(ofile, "%s", printable_addr) < 0)
2904 2906 return (B_FALSE);
2905 2907 if (addr->sadb_address_prefixlen != 0 &&
2906 2908 !((addr->sadb_address_prefixlen == 32 && af == AF_INET) ||
2907 2909 (addr->sadb_address_prefixlen == 128 && af == AF_INET6))) {
2908 2910 if (fprintf(ofile, "/%d", addr->sadb_address_prefixlen) < 0)
2909 2911 return (B_FALSE);
2910 2912 }
2911 2913
2912 2914 /*
2913 2915 * The port is in the same position for struct sockaddr_in and
2914 2916 * struct sockaddr_in6. We exploit that property here.
2915 2917 */
2916 2918 if ((pprefix != NULL) && (sin->sin_port != 0))
2917 2919 (void) fprintf(ofile, " %s %d", pprefix, ntohs(sin->sin_port));
2918 2920
2919 2921 return (B_TRUE);
2920 2922 }
2921 2923
2922 2924 /*
2923 2925 * Print save information for a key extension. Returns whether writing
2924 2926 * to the specified output file was successful or not.
2925 2927 */
2926 2928 boolean_t
2927 2929 save_key(struct sadb_key *key, FILE *ofile)
2928 2930 {
2929 2931 char *prefix;
2930 2932
2931 2933 if (putc('\t', ofile) == EOF)
2932 2934 return (B_FALSE);
2933 2935
2934 2936 prefix = (key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ? "auth" : "encr";
2935 2937
2936 2938 if (fprintf(ofile, "%skey ", prefix) < 0)
2937 2939 return (B_FALSE);
2938 2940
2939 2941 if (dump_key((uint8_t *)(key + 1), key->sadb_key_bits,
2940 2942 key->sadb_key_reserved, ofile, B_FALSE) == -1)
2941 2943 return (B_FALSE);
2942 2944
2943 2945 return (B_TRUE);
2944 2946 }
2945 2947
2946 2948 /*
2947 2949 * Print save information for an identity extension.
2948 2950 */
2949 2951 boolean_t
2950 2952 save_ident(struct sadb_ident *ident, FILE *ofile)
2951 2953 {
2952 2954 char *prefix;
2953 2955
2954 2956 if (putc('\t', ofile) == EOF)
2955 2957 return (B_FALSE);
2956 2958
2957 2959 prefix = (ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ? "src" :
2958 2960 "dst";
2959 2961
2960 2962 if (fprintf(ofile, "%sidtype %s ", prefix,
2961 2963 rparseidtype(ident->sadb_ident_type)) < 0)
2962 2964 return (B_FALSE);
2963 2965
2964 2966 if (ident->sadb_ident_type == SADB_X_IDENTTYPE_DN ||
2965 2967 ident->sadb_ident_type == SADB_X_IDENTTYPE_GN) {
2966 2968 if (fprintf(ofile, dgettext(TEXT_DOMAIN,
2967 2969 "<can-not-print>")) < 0)
2968 2970 return (B_FALSE);
2969 2971 } else {
2970 2972 if (fprintf(ofile, "%s", (char *)(ident + 1)) < 0)
2971 2973 return (B_FALSE);
2972 2974 }
2973 2975
2974 2976 return (B_TRUE);
2975 2977 }
2976 2978
2977 2979 boolean_t
2978 2980 save_sens(struct sadb_sens *sens, FILE *ofile)
2979 2981 {
2980 2982 char *prefix;
2981 2983 char *hlabel;
2982 2984 bslabel_t sl;
2983 2985
2984 2986 if (putc('\t', ofile) == EOF)
2985 2987 return (B_FALSE);
2986 2988
2987 2989 if (sens->sadb_sens_exttype == SADB_EXT_SENSITIVITY)
2988 2990 prefix = "label";
2989 2991 else if ((sens->sadb_x_sens_flags & SADB_X_SENS_IMPLICIT) == 0)
2990 2992 prefix = "outer-label";
2991 2993 else
2992 2994 prefix = "implicit-label";
2993 2995
2994 2996 ipsec_convert_sens_to_bslabel(sens, &sl);
2995 2997 ipsec_convert_bslabel_to_hex(&sl, &hlabel);
2996 2998
2997 2999 if (fprintf(ofile, "%s %s ", prefix, hlabel) < 0) {
2998 3000 free(hlabel);
2999 3001 return (B_FALSE);
3000 3002 }
3001 3003 free(hlabel);
3002 3004
3003 3005 return (B_TRUE);
3004 3006 }
3005 3007
3006 3008 /*
3007 3009 * "Save" a security association to an output file.
3008 3010 *
3009 3011 * NOTE the lack of calls to dgettext() because I'm outputting parseable stuff.
3010 3012 * ALSO NOTE that if you change keywords (see parsecmd()), you'll have to
3011 3013 * change them here as well.
3012 3014 */
3013 3015 void
3014 3016 save_assoc(uint64_t *buffer, FILE *ofile)
3015 3017 {
3016 3018 int terrno;
3017 3019 boolean_t seen_proto = B_FALSE, seen_iproto = B_FALSE;
3018 3020 uint64_t *current;
3019 3021 struct sadb_address *addr;
3020 3022 struct sadb_x_replay_ctr *repl;
3021 3023 struct sadb_msg *samsg = (struct sadb_msg *)buffer;
3022 3024 struct sadb_ext *ext;
3023 3025
3024 3026 #define tidyup() \
3025 3027 terrno = errno; (void) fclose(ofile); errno = terrno; \
3026 3028 interactive = B_FALSE
3027 3029
3028 3030 #define savenl() if (fputs(" \\\n", ofile) == EOF) \
3029 3031 { bail(dgettext(TEXT_DOMAIN, "savenl")); }
3030 3032
3031 3033 if (fputs("# begin assoc\n", ofile) == EOF)
3032 3034 bail(dgettext(TEXT_DOMAIN,
3033 3035 "save_assoc: Opening comment of SA"));
3034 3036 if (fprintf(ofile, "add %s ", rparsesatype(samsg->sadb_msg_satype)) < 0)
3035 3037 bail(dgettext(TEXT_DOMAIN, "save_assoc: First line of SA"));
3036 3038 savenl();
3037 3039
3038 3040 current = (uint64_t *)(samsg + 1);
3039 3041 while (current - buffer < samsg->sadb_msg_len) {
3040 3042 struct sadb_sa *assoc;
3041 3043
3042 3044 ext = (struct sadb_ext *)current;
3043 3045 addr = (struct sadb_address *)ext; /* Just in case... */
3044 3046 switch (ext->sadb_ext_type) {
3045 3047 case SADB_EXT_SA:
3046 3048 assoc = (struct sadb_sa *)ext;
3047 3049 if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) {
3048 3050 if (fprintf(ofile, "# WARNING: SA was dying "
3049 3051 "or dead.\n") < 0) {
3050 3052 tidyup();
3051 3053 bail(dgettext(TEXT_DOMAIN,
3052 3054 "save_assoc: fprintf not mature"));
3053 3055 }
3054 3056 }
3055 3057 if (fprintf(ofile, " spi 0x%x ",
3056 3058 ntohl(assoc->sadb_sa_spi)) < 0) {
3057 3059 tidyup();
3058 3060 bail(dgettext(TEXT_DOMAIN,
3059 3061 "save_assoc: fprintf spi"));
3060 3062 }
3061 3063 if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
3062 3064 if (fprintf(ofile, "encr_alg %s ",
3063 3065 rparsealg(assoc->sadb_sa_encrypt,
3064 3066 IPSEC_PROTO_ESP)) < 0) {
3065 3067 tidyup();
3066 3068 bail(dgettext(TEXT_DOMAIN,
3067 3069 "save_assoc: fprintf encrypt"));
3068 3070 }
3069 3071 }
3070 3072 if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
3071 3073 if (fprintf(ofile, "auth_alg %s ",
3072 3074 rparsealg(assoc->sadb_sa_auth,
3073 3075 IPSEC_PROTO_AH)) < 0) {
3074 3076 tidyup();
3075 3077 bail(dgettext(TEXT_DOMAIN,
3076 3078 "save_assoc: fprintf auth"));
3077 3079 }
3078 3080 }
3079 3081 if (fprintf(ofile, "replay %d ",
3080 3082 assoc->sadb_sa_replay) < 0) {
3081 3083 tidyup();
3082 3084 bail(dgettext(TEXT_DOMAIN,
3083 3085 "save_assoc: fprintf replay"));
3084 3086 }
3085 3087 if (assoc->sadb_sa_flags & (SADB_X_SAFLAGS_NATT_LOC |
3086 3088 SADB_X_SAFLAGS_NATT_REM)) {
3087 3089 if (fprintf(ofile, "encap udp") < 0) {
3088 3090 tidyup();
3089 3091 bail(dgettext(TEXT_DOMAIN,
3090 3092 "save_assoc: fprintf encap"));
3091 3093 }
3092 3094 }
3093 3095 savenl();
3094 3096 break;
3095 3097 case SADB_EXT_LIFETIME_HARD:
3096 3098 case SADB_EXT_LIFETIME_SOFT:
3097 3099 case SADB_X_EXT_LIFETIME_IDLE:
3098 3100 if (!save_lifetime((struct sadb_lifetime *)ext,
3099 3101 ofile)) {
3100 3102 tidyup();
3101 3103 bail(dgettext(TEXT_DOMAIN, "save_lifetime"));
3102 3104 }
3103 3105 savenl();
3104 3106 break;
3105 3107 case SADB_X_EXT_ADDRESS_INNER_SRC:
3106 3108 case SADB_X_EXT_ADDRESS_INNER_DST:
3107 3109 if (!seen_iproto && addr->sadb_address_proto) {
3108 3110 (void) fprintf(ofile, " iproto %d",
3109 3111 addr->sadb_address_proto);
3110 3112 savenl();
3111 3113 seen_iproto = B_TRUE;
3112 3114 }
3113 3115 goto skip_srcdst; /* Hack to avoid cases below... */
3114 3116 /* FALLTHRU */
3115 3117 case SADB_EXT_ADDRESS_SRC:
3116 3118 case SADB_EXT_ADDRESS_DST:
3117 3119 if (!seen_proto && addr->sadb_address_proto) {
3118 3120 (void) fprintf(ofile, " proto %d",
3119 3121 addr->sadb_address_proto);
3120 3122 savenl();
3121 3123 seen_proto = B_TRUE;
3122 3124 }
3123 3125 /* FALLTHRU */
3124 3126 case SADB_X_EXT_ADDRESS_NATT_REM:
3125 3127 case SADB_X_EXT_ADDRESS_NATT_LOC:
3126 3128 skip_srcdst:
3127 3129 if (!save_address(addr, ofile)) {
3128 3130 tidyup();
3129 3131 bail(dgettext(TEXT_DOMAIN, "save_address"));
3130 3132 }
3131 3133 savenl();
3132 3134 break;
3133 3135 case SADB_EXT_KEY_AUTH:
3134 3136 case SADB_EXT_KEY_ENCRYPT:
3135 3137 if (!save_key((struct sadb_key *)ext, ofile)) {
3136 3138 tidyup();
3137 3139 bail(dgettext(TEXT_DOMAIN, "save_address"));
3138 3140 }
3139 3141 savenl();
3140 3142 break;
3141 3143 case SADB_EXT_IDENTITY_SRC:
3142 3144 case SADB_EXT_IDENTITY_DST:
3143 3145 if (!save_ident((struct sadb_ident *)ext, ofile)) {
3144 3146 tidyup();
3145 3147 bail(dgettext(TEXT_DOMAIN, "save_address"));
3146 3148 }
3147 3149 savenl();
3148 3150 break;
3149 3151 case SADB_X_EXT_REPLAY_VALUE:
3150 3152 repl = (sadb_x_replay_ctr_t *)ext;
3151 3153 if ((repl->sadb_x_rc_replay32 == 0) &&
3152 3154 (repl->sadb_x_rc_replay64 == 0)) {
3153 3155 tidyup();
3154 3156 bail(dgettext(TEXT_DOMAIN, "Replay Value"));
3155 3157 }
3156 3158 if (fprintf(ofile, "replay_value %" PRIu64 "",
3157 3159 (repl->sadb_x_rc_replay32 == 0 ?
3158 3160 repl->sadb_x_rc_replay64 :
3159 3161 repl->sadb_x_rc_replay32)) < 0) {
3160 3162 tidyup();
3161 3163 bail(dgettext(TEXT_DOMAIN,
3162 3164 "save_assoc: fprintf replay value"));
3163 3165 }
3164 3166 savenl();
3165 3167 break;
3166 3168 case SADB_EXT_SENSITIVITY:
3167 3169 case SADB_X_EXT_OUTER_SENS:
3168 3170 if (!save_sens((struct sadb_sens *)ext, ofile)) {
3169 3171 tidyup();
3170 3172 bail(dgettext(TEXT_DOMAIN, "save_sens"));
3171 3173 }
3172 3174 savenl();
3173 3175 break;
3174 3176 default:
3175 3177 /* Skip over irrelevant extensions. */
3176 3178 break;
3177 3179 }
3178 3180 current += ext->sadb_ext_len;
3179 3181 }
3180 3182
3181 3183 if (fputs(dgettext(TEXT_DOMAIN, "\n# end assoc\n\n"), ofile) == EOF) {
3182 3184 tidyup();
3183 3185 bail(dgettext(TEXT_DOMAIN, "save_assoc: last fputs"));
3184 3186 }
3185 3187 }
3186 3188
3187 3189 /*
3188 3190 * Open the output file for the "save" command.
3189 3191 */
3190 3192 FILE *
3191 3193 opensavefile(char *filename)
3192 3194 {
3193 3195 int fd;
3194 3196 FILE *retval;
3195 3197 struct stat buf;
3196 3198
3197 3199 /*
3198 3200 * If the user specifies "-" or doesn't give a filename, then
3199 3201 * dump to stdout. Make sure to document the dangers of files
3200 3202 * that are NFS, directing your output to strange places, etc.
3201 3203 */
3202 3204 if (filename == NULL || strcmp("-", filename) == 0)
3203 3205 return (stdout);
3204 3206
3205 3207 /*
3206 3208 * open the file with the create bits set. Since I check for
3207 3209 * real UID == root in main(), I won't worry about the ownership
3208 3210 * problem.
3209 3211 */
3210 3212 fd = open(filename, O_WRONLY | O_EXCL | O_CREAT | O_TRUNC, S_IRUSR);
3211 3213 if (fd == -1) {
3212 3214 if (errno != EEXIST)
3213 3215 bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
3214 3216 "open error"),
3215 3217 strerror(errno));
3216 3218 fd = open(filename, O_WRONLY | O_TRUNC, 0);
3217 3219 if (fd == -1)
3218 3220 bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
3219 3221 "open error"), strerror(errno));
3220 3222 if (fstat(fd, &buf) == -1) {
3221 3223 (void) close(fd);
3222 3224 bail_msg("%s fstat: %s", filename, strerror(errno));
3223 3225 }
3224 3226 if (S_ISREG(buf.st_mode) &&
3225 3227 ((buf.st_mode & S_IAMB) != S_IRUSR)) {
3226 3228 warnx(dgettext(TEXT_DOMAIN,
3227 3229 "WARNING: Save file already exists with "
3228 3230 "permission %o."), buf.st_mode & S_IAMB);
3229 3231 warnx(dgettext(TEXT_DOMAIN,
3230 3232 "Normal users may be able to read IPsec "
3231 3233 "keying material."));
3232 3234 }
3233 3235 }
3234 3236
3235 3237 /* Okay, we have an FD. Assign it to a stdio FILE pointer. */
3236 3238 retval = fdopen(fd, "w");
3237 3239 if (retval == NULL) {
3238 3240 (void) close(fd);
3239 3241 bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
3240 3242 "fdopen error"), strerror(errno));
3241 3243 }
3242 3244 return (retval);
3243 3245 }
3244 3246
3245 3247 const char *
3246 3248 do_inet_ntop(const void *addr, char *cp, size_t size)
3247 3249 {
3248 3250 boolean_t isv4;
3249 3251 struct in6_addr *inaddr6 = (struct in6_addr *)addr;
3250 3252 struct in_addr inaddr;
3251 3253
3252 3254 if ((isv4 = IN6_IS_ADDR_V4MAPPED(inaddr6)) == B_TRUE) {
3253 3255 IN6_V4MAPPED_TO_INADDR(inaddr6, &inaddr);
3254 3256 }
3255 3257
3256 3258 return (inet_ntop(isv4 ? AF_INET : AF_INET6,
3257 3259 isv4 ? (void *)&inaddr : inaddr6, cp, size));
3258 3260 }
3259 3261
3260 3262 char numprint[NBUF_SIZE];
3261 3263
3262 3264 /*
3263 3265 * Parse and reverse parse a specific SA type (AH, ESP, etc.).
3264 3266 */
3265 3267 static struct typetable {
3266 3268 char *type;
3267 3269 int token;
3268 3270 } type_table[] = {
3269 3271 {"all", SADB_SATYPE_UNSPEC},
3270 3272 {"ah", SADB_SATYPE_AH},
3271 3273 {"esp", SADB_SATYPE_ESP},
3272 3274 /* PF_KEY NOTE: More to come if net/pfkeyv2.h gets updated. */
3273 3275 {NULL, 0} /* Token value is irrelevant for this entry. */
3274 3276 };
3275 3277
3276 3278 char *
3277 3279 rparsesatype(int type)
3278 3280 {
3279 3281 struct typetable *tt = type_table;
3280 3282
3281 3283 while (tt->type != NULL && type != tt->token)
3282 3284 tt++;
3283 3285
3284 3286 if (tt->type == NULL) {
3285 3287 (void) snprintf(numprint, NBUF_SIZE, "%d", type);
3286 3288 } else {
3287 3289 return (tt->type);
3288 3290 }
3289 3291
3290 3292 return (numprint);
3291 3293 }
3292 3294
3293 3295
3294 3296 /*
3295 3297 * Return a string containing the name of the specified numerical algorithm
3296 3298 * identifier.
3297 3299 */
3298 3300 char *
3299 3301 rparsealg(uint8_t alg, int proto_num)
3300 3302 {
3301 3303 static struct ipsecalgent *holder = NULL; /* we're single-threaded */
3302 3304
3303 3305 if (holder != NULL)
3304 3306 freeipsecalgent(holder);
3305 3307
3306 3308 holder = getipsecalgbynum(alg, proto_num, NULL);
3307 3309 if (holder == NULL) {
3308 3310 (void) snprintf(numprint, NBUF_SIZE, "%d", alg);
3309 3311 return (numprint);
3310 3312 }
3311 3313
3312 3314 return (*(holder->a_names));
3313 3315 }
3314 3316
3315 3317 /*
3316 3318 * Parse and reverse parse out a source/destination ID type.
3317 3319 */
3318 3320 static struct idtypes {
3319 3321 char *idtype;
3320 3322 uint8_t retval;
3321 3323 } idtypes[] = {
3322 3324 {"prefix", SADB_IDENTTYPE_PREFIX},
3323 3325 {"fqdn", SADB_IDENTTYPE_FQDN},
3324 3326 {"domain", SADB_IDENTTYPE_FQDN},
3325 3327 {"domainname", SADB_IDENTTYPE_FQDN},
3326 3328 {"user_fqdn", SADB_IDENTTYPE_USER_FQDN},
3327 3329 {"mailbox", SADB_IDENTTYPE_USER_FQDN},
3328 3330 {"der_dn", SADB_X_IDENTTYPE_DN},
3329 3331 {"der_gn", SADB_X_IDENTTYPE_GN},
3330 3332 {NULL, 0}
3331 3333 };
3332 3334
3333 3335 char *
3334 3336 rparseidtype(uint16_t type)
3335 3337 {
3336 3338 struct idtypes *idp;
3337 3339
3338 3340 for (idp = idtypes; idp->idtype != NULL; idp++) {
3339 3341 if (type == idp->retval)
3340 3342 return (idp->idtype);
3341 3343 }
3342 3344
3343 3345 (void) snprintf(numprint, NBUF_SIZE, "%d", type);
3344 3346 return (numprint);
3345 3347 }
3346 3348
3347 3349 /*
3348 3350 * This is a general purpose exit function, calling functions can specify an
3349 3351 * error type. If the command calling this function was started by smf(5) the
3350 3352 * error type could be used as a hint to the restarter. In the future this
3351 3353 * function could be used to do something more intelligent with a process that
3352 3354 * encounters an error. If exit() is called with an error code other than those
3353 3355 * defined by smf(5), the program will just get restarted. Unless restarting
3354 3356 * is likely to resolve the error condition, its probably sensible to just
3355 3357 * log the error and keep running.
3356 3358 *
3357 3359 * The SERVICE_* exit_types mean nothing if the command was run from the
3358 3360 * command line, just exit(). There are two special cases:
3359 3361 *
3360 3362 * SERVICE_DEGRADE - Not implemented in smf(5), one day it could hint that
3361 3363 * the service is not running as well is it could. For
3362 3364 * now, don't do anything, just record the error.
3363 3365 * DEBUG_FATAL - Something happened, if the command was being run in debug
3364 3366 * mode, exit() as you really want to know something happened,
3365 3367 * otherwise just keep running. This is ignored when running
3366 3368 * under smf(5).
3367 3369 *
3368 3370 * The function will handle an optional variable args error message, this
3369 3371 * will be written to the error stream, typically a log file or stderr.
3370 3372 */
3371 3373 void
3372 3374 ipsecutil_exit(exit_type_t type, char *fmri, FILE *fp, const char *fmt, ...)
3373 3375 {
3374 3376 int exit_status;
3375 3377 va_list args;
3376 3378
3377 3379 if (fp == NULL)
3378 3380 fp = stderr;
3379 3381 if (fmt != NULL) {
3380 3382 va_start(args, fmt);
3381 3383 vwarnxfp(fp, fmt, args);
3382 3384 va_end(args);
3383 3385 }
3384 3386
3385 3387 if (fmri == NULL) {
3386 3388 /* Command being run directly from a shell. */
3387 3389 switch (type) {
3388 3390 case SERVICE_EXIT_OK:
3389 3391 exit_status = 0;
3390 3392 break;
3391 3393 case SERVICE_DEGRADE:
3392 3394 return;
3393 3395 case SERVICE_BADPERM:
3394 3396 case SERVICE_BADCONF:
3395 3397 case SERVICE_MAINTAIN:
3396 3398 case SERVICE_DISABLE:
3397 3399 case SERVICE_FATAL:
3398 3400 case SERVICE_RESTART:
3399 3401 case DEBUG_FATAL:
3400 3402 warnxfp(fp, "Fatal error - exiting.");
3401 3403 exit_status = 1;
3402 3404 break;
3403 3405 }
3404 3406 } else {
3405 3407 /* Command being run as a smf(5) method. */
3406 3408 switch (type) {
3407 3409 case SERVICE_EXIT_OK:
3408 3410 exit_status = SMF_EXIT_OK;
3409 3411 break;
3410 3412 case SERVICE_DEGRADE: /* Not implemented yet. */
3411 3413 case DEBUG_FATAL:
3412 3414 /* Keep running, don't exit(). */
3413 3415 return;
3414 3416 case SERVICE_BADPERM:
3415 3417 warnxfp(fp, dgettext(TEXT_DOMAIN,
3416 3418 "Permission error with %s."), fmri);
3417 3419 exit_status = SMF_EXIT_ERR_PERM;
3418 3420 break;
3419 3421 case SERVICE_BADCONF:
3420 3422 warnxfp(fp, dgettext(TEXT_DOMAIN,
3421 3423 "Bad configuration of service %s."), fmri);
3422 3424 exit_status = SMF_EXIT_ERR_FATAL;
3423 3425 break;
3424 3426 case SERVICE_MAINTAIN:
3425 3427 warnxfp(fp, dgettext(TEXT_DOMAIN,
3426 3428 "Service %s needs maintenance."), fmri);
3427 3429 exit_status = SMF_EXIT_ERR_FATAL;
3428 3430 break;
3429 3431 case SERVICE_DISABLE:
3430 3432 exit_status = SMF_EXIT_ERR_FATAL;
3431 3433 break;
3432 3434 case SERVICE_FATAL:
3433 3435 warnxfp(fp, dgettext(TEXT_DOMAIN,
3434 3436 "Service %s fatal error."), fmri);
3435 3437 exit_status = SMF_EXIT_ERR_FATAL;
3436 3438 break;
3437 3439 case SERVICE_RESTART:
3438 3440 exit_status = 1;
3439 3441 break;
3440 3442 }
3441 3443 }
3442 3444 (void) fflush(fp);
3443 3445 (void) fclose(fp);
3444 3446 exit(exit_status);
3445 3447 }
|
↓ open down ↓ |
1647 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX