2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char rcsid
[] _U_
=
33 "@(#) $Header: /tcpdump/master/tcpdump/print-isakmp.c,v 1.44 2004-03-12 01:13:53 guy Exp $ (LBL)";
40 #include <tcpdump-stdinc.h>
47 #include "ipsec_doi.h"
49 #include "interface.h"
50 #include "addrtoname.h"
51 #include "extract.h" /* must come after interface.h */
58 #ifndef HAVE_SOCKADDR_STORAGE
59 #define sockaddr_storage sockaddr
62 static const u_char
*isakmp_sa_print(const struct isakmp_gen
*,
63 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
64 static const u_char
*isakmp_p_print(const struct isakmp_gen
*,
65 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
66 static const u_char
*isakmp_t_print(const struct isakmp_gen
*,
67 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
68 static const u_char
*isakmp_ke_print(const struct isakmp_gen
*,
69 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
70 static const u_char
*isakmp_id_print(const struct isakmp_gen
*,
71 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
72 static const u_char
*isakmp_cert_print(const struct isakmp_gen
*,
73 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
74 static const u_char
*isakmp_cr_print(const struct isakmp_gen
*,
75 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
76 static const u_char
*isakmp_sig_print(const struct isakmp_gen
*,
77 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
78 static const u_char
*isakmp_hash_print(const struct isakmp_gen
*,
79 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
80 static const u_char
*isakmp_nonce_print(const struct isakmp_gen
*,
81 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
82 static const u_char
*isakmp_n_print(const struct isakmp_gen
*,
83 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
84 static const u_char
*isakmp_d_print(const struct isakmp_gen
*,
85 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
86 static const u_char
*isakmp_vid_print(const struct isakmp_gen
*,
87 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
88 static const u_char
*isakmp_sub0_print(u_char
, const struct isakmp_gen
*,
89 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
90 static const u_char
*isakmp_sub_print(u_char
, const struct isakmp_gen
*,
91 const u_char
*, u_int32_t
, u_int32_t
, u_int32_t
, int);
92 static char *numstr(int);
93 static void safememcpy(void *, const void *, size_t);
95 #define MAXINITIATORS 20
99 struct sockaddr_storage iaddr
;
100 struct sockaddr_storage raddr
;
101 } cookiecache
[MAXINITIATORS
];
104 static const char *protoidstr
[] = {
105 NULL
, "isakmp", "ipsec-ah", "ipsec-esp", "ipcomp",
109 static const char *npstr
[] = {
110 "none", "sa", "p", "t", "ke", "id", "cert", "cr", "hash",
111 "sig", "nonce", "n", "d", "vid"
115 static const u_char
*(*npfunc
[])(const struct isakmp_gen
*, const u_char
*,
116 u_int32_t
, u_int32_t
, u_int32_t
, int) = {
134 static const char *etypestr
[] = {
135 "none", "base", "ident", "auth", "agg", "inf", NULL
, NULL
,
136 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
137 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
138 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
139 "oakley-quick", "oakley-newgroup",
142 #define STR_OR_ID(x, tab) \
143 (((x) < sizeof(tab)/sizeof(tab[0]) && tab[(x)]) ? tab[(x)] : numstr(x))
144 #define PROTOIDSTR(x) STR_OR_ID(x, protoidstr)
145 #define NPSTR(x) STR_OR_ID(x, npstr)
146 #define ETYPESTR(x) STR_OR_ID(x, etypestr)
149 (((x) < sizeof(npfunc)/sizeof(npfunc[0]) && npfunc[(x)]) \
150 ? npfunc[(x)] : NULL)
153 iszero(u_char
*p
, size_t l
)
162 /* find cookie from initiator cache */
164 cookie_find(cookie_t
*in
)
168 for (i
= 0; i
< MAXINITIATORS
; i
++) {
169 if (memcmp(in
, &cookiecache
[i
].initiator
, sizeof(*in
)) == 0)
176 /* record initiator */
178 cookie_record(cookie_t
*in
, const u_char
*bp2
)
182 struct sockaddr_in
*sin
;
185 struct sockaddr_in6
*sin6
;
190 ninitiator
= (i
+ 1) % MAXINITIATORS
;
194 ip
= (struct ip
*)bp2
;
197 memset(&cookiecache
[ninitiator
].iaddr
, 0,
198 sizeof(cookiecache
[ninitiator
].iaddr
));
199 memset(&cookiecache
[ninitiator
].raddr
, 0,
200 sizeof(cookiecache
[ninitiator
].raddr
));
202 sin
= (struct sockaddr_in
*)&cookiecache
[ninitiator
].iaddr
;
203 #ifdef HAVE_SOCKADDR_SA_LEN
204 sin
->sin_len
= sizeof(struct sockaddr_in
);
206 sin
->sin_family
= AF_INET
;
207 memcpy(&sin
->sin_addr
, &ip
->ip_src
, sizeof(ip
->ip_src
));
208 sin
= (struct sockaddr_in
*)&cookiecache
[ninitiator
].raddr
;
209 #ifdef HAVE_SOCKADDR_SA_LEN
210 sin
->sin_len
= sizeof(struct sockaddr_in
);
212 sin
->sin_family
= AF_INET
;
213 memcpy(&sin
->sin_addr
, &ip
->ip_dst
, sizeof(ip
->ip_dst
));
217 memset(&cookiecache
[ninitiator
].iaddr
, 0,
218 sizeof(cookiecache
[ninitiator
].iaddr
));
219 memset(&cookiecache
[ninitiator
].raddr
, 0,
220 sizeof(cookiecache
[ninitiator
].raddr
));
222 ip6
= (struct ip6_hdr
*)bp2
;
223 sin6
= (struct sockaddr_in6
*)&cookiecache
[ninitiator
].iaddr
;
224 #ifdef HAVE_SOCKADDR_SA_LEN
225 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
227 sin6
->sin6_family
= AF_INET6
;
228 memcpy(&sin6
->sin6_addr
, &ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
229 sin6
= (struct sockaddr_in6
*)&cookiecache
[ninitiator
].raddr
;
230 #ifdef HAVE_SOCKADDR_SA_LEN
231 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
233 sin6
->sin6_family
= AF_INET6
;
234 memcpy(&sin6
->sin6_addr
, &ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
240 memcpy(&cookiecache
[ninitiator
].initiator
, in
, sizeof(*in
));
241 ninitiator
= (ninitiator
+ 1) % MAXINITIATORS
;
244 #define cookie_isinitiator(x, y) cookie_sidecheck((x), (y), 1)
245 #define cookie_isresponder(x, y) cookie_sidecheck((x), (y), 0)
247 cookie_sidecheck(int i
, const u_char
*bp2
, int initiator
)
249 struct sockaddr_storage ss
;
252 struct sockaddr_in
*sin
;
255 struct sockaddr_in6
*sin6
;
259 memset(&ss
, 0, sizeof(ss
));
260 ip
= (struct ip
*)bp2
;
263 sin
= (struct sockaddr_in
*)&ss
;
264 #ifdef HAVE_SOCKADDR_SA_LEN
265 sin
->sin_len
= sizeof(struct sockaddr_in
);
267 sin
->sin_family
= AF_INET
;
268 memcpy(&sin
->sin_addr
, &ip
->ip_src
, sizeof(ip
->ip_src
));
272 ip6
= (struct ip6_hdr
*)bp2
;
273 sin6
= (struct sockaddr_in6
*)&ss
;
274 #ifdef HAVE_SOCKADDR_SA_LEN
275 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
277 sin6
->sin6_family
= AF_INET6
;
278 memcpy(&sin6
->sin6_addr
, &ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
285 sa
= (struct sockaddr
*)&ss
;
287 if (sa
->sa_family
!= ((struct sockaddr
*)&cookiecache
[i
].iaddr
)->sa_family
)
289 #ifdef HAVE_SOCKADDR_SA_LEN
293 if (sa
->sa_family
== AF_INET6
)
294 salen
= sizeof(struct sockaddr_in6
);
296 salen
= sizeof(struct sockaddr
);
298 salen
= sizeof(struct sockaddr
);
301 if (memcmp(&ss
, &cookiecache
[i
].iaddr
, salen
) == 0)
304 if (sa
->sa_family
!= ((struct sockaddr
*)&cookiecache
[i
].raddr
)->sa_family
)
306 #ifdef HAVE_SOCKADDR_SA_LEN
310 if (sa
->sa_family
== AF_INET6
)
311 salen
= sizeof(struct sockaddr_in6
);
313 salen
= sizeof(struct sockaddr
);
315 salen
= sizeof(struct sockaddr
);
318 if (memcmp(&ss
, &cookiecache
[i
].raddr
, salen
) == 0)
325 rawprint(caddr_t loc
, size_t len
)
333 for (i
= 0; i
< len
; i
++)
334 printf("%02x", p
[i
] & 0xff);
342 const char *value
[30]; /*XXX*/
345 static const u_char
*
346 isakmp_attrmap_print(const u_char
*p
, const u_char
*ep
,
347 const struct attrmap
*map
, size_t nmap
)
357 totlen
= 4 + EXTRACT_16BITS(&q
[1]);
358 if (ep
< p
+ totlen
) {
364 t
= EXTRACT_16BITS(&q
[0]) & 0x7fff;
365 if (map
&& t
< nmap
&& map
[t
].type
)
366 printf("type=%s ", map
[t
].type
);
368 printf("type=#%d ", t
);
371 v
= EXTRACT_16BITS(&q
[1]);
372 if (map
&& t
< nmap
&& v
< map
[t
].nvalue
&& map
[t
].value
[v
])
373 printf("%s", map
[t
].value
[v
]);
375 rawprint((caddr_t
)&q
[1], 2);
377 printf("len=%d value=", EXTRACT_16BITS(&q
[1]));
378 rawprint((caddr_t
)&p
[4], EXTRACT_16BITS(&q
[1]));
384 static const u_char
*
385 isakmp_attr_print(const u_char
*p
, const u_char
*ep
)
395 totlen
= 4 + EXTRACT_16BITS(&q
[1]);
396 if (ep
< p
+ totlen
) {
402 t
= EXTRACT_16BITS(&q
[0]) & 0x7fff;
403 printf("type=#%d ", t
);
407 rawprint((caddr_t
)&q
[1], 2);
409 printf("len=%d value=", EXTRACT_16BITS(&q
[1]));
410 rawprint((caddr_t
)&p
[2], EXTRACT_16BITS(&q
[1]));
416 static const u_char
*
417 isakmp_sa_print(const struct isakmp_gen
*ext
, const u_char
*ep
, u_int32_t phase
,
418 u_int32_t doi0 _U_
, u_int32_t proto0
, int depth
)
420 const struct isakmp_pl_sa
*p
;
421 struct isakmp_pl_sa sa
;
423 u_int32_t doi
, sit
, ident
;
424 const u_char
*cp
, *np
;
427 printf("%s:", NPSTR(ISAKMP_NPTYPE_SA
));
429 p
= (struct isakmp_pl_sa
*)ext
;
430 safememcpy(&sa
, ext
, sizeof(sa
));
434 printf(" doi=%d", doi
);
435 printf(" situation=%u", (u_int32_t
)ntohl(sa
.sit
));
436 return (u_char
*)(p
+ 1);
439 printf(" doi=ipsec");
440 q
= (u_int32_t
*)&sa
.sit
;
441 printf(" situation=");
448 printf("%ssecrecy", t
? "+" : "");
452 printf("%sintegrity", t
? "+" : "");
454 np
= (u_char
*)ext
+ sizeof(sa
);
456 safememcpy(&ident
, ext
+ 1, sizeof(ident
));
457 printf(" ident=%u", (u_int32_t
)ntohl(ident
));
461 ext
= (struct isakmp_gen
*)np
;
463 cp
= isakmp_sub_print(ISAKMP_NPTYPE_P
, ext
, ep
, phase
, doi
, proto0
,
469 static const u_char
*
470 isakmp_p_print(const struct isakmp_gen
*ext
, const u_char
*ep
, u_int32_t phase
,
471 u_int32_t doi0
, u_int32_t proto0 _U_
, int depth
)
473 const struct isakmp_pl_p
*p
;
474 struct isakmp_pl_p prop
;
477 printf("%s:", NPSTR(ISAKMP_NPTYPE_P
));
479 p
= (struct isakmp_pl_p
*)ext
;
480 safememcpy(&prop
, ext
, sizeof(prop
));
481 printf(" #%d protoid=%s transform=%d",
482 prop
.p_no
, PROTOIDSTR(prop
.prot_id
), prop
.num_t
);
485 rawprint((caddr_t
)(p
+ 1), prop
.spi_size
);
488 ext
= (struct isakmp_gen
*)((u_char
*)(p
+ 1) + prop
.spi_size
);
490 cp
= isakmp_sub_print(ISAKMP_NPTYPE_T
, ext
, ep
, phase
, doi0
,
491 prop
.prot_id
, depth
);
496 static const char *isakmp_p_map
[] = {
500 static const char *ah_p_map
[] = {
501 NULL
, "(reserved)", "md5", "sha", "1des",
502 "sha2-256", "sha2-384", "sha2-512",
505 static const char *esp_p_map
[] = {
506 NULL
, "1des-iv64", "1des", "3des", "rc5", "idea", "cast",
507 "blowfish", "3idea", "1des-iv32", "rc4", "null", "aes"
510 static const char *ipcomp_p_map
[] = {
511 NULL
, "oui", "deflate", "lzs",
514 const struct attrmap ipsec_t_map
[] = {
515 { NULL
, 0, { NULL
} },
516 { "lifetype", 3, { NULL
, "sec", "kb", }, },
517 { "life", 0, { NULL
} },
518 { "group desc", 5, { NULL
, "modp768", "modp1024", "EC2N 2^155",
520 { "enc mode", 3, { NULL
, "tunnel", "transport", }, },
521 { "auth", 5, { NULL
, "hmac-md5", "hmac-sha1", "1des-mac", "keyed", }, },
522 { "keylen", 0, { NULL
} },
523 { "rounds", 0, { NULL
} },
524 { "dictsize", 0, { NULL
} },
525 { "privalg", 0, { NULL
} },
528 const struct attrmap oakley_t_map
[] = {
529 { NULL
, 0, { NULL
} },
530 { "enc", 8, { NULL
, "1des", "idea", "blowfish", "rc5",
531 "3des", "cast", "aes", }, },
532 { "hash", 7, { NULL
, "md5", "sha1", "tiger",
533 "sha2-256", "sha2-384", "sha2-512", }, },
534 { "auth", 6, { NULL
, "preshared", "dss", "rsa sig", "rsa enc",
535 "rsa enc revised", }, },
536 { "group desc", 5, { NULL
, "modp768", "modp1024", "EC2N 2^155",
538 { "group type", 4, { NULL
, "MODP", "ECP", "EC2N", }, },
539 { "group prime", 0, { NULL
} },
540 { "group gen1", 0, { NULL
} },
541 { "group gen2", 0, { NULL
} },
542 { "group curve A", 0, { NULL
} },
543 { "group curve B", 0, { NULL
} },
544 { "lifetype", 3, { NULL
, "sec", "kb", }, },
545 { "lifeduration", 0, { NULL
} },
546 { "prf", 0, { NULL
} },
547 { "keylen", 0, { NULL
} },
548 { "field", 0, { NULL
} },
549 { "order", 0, { NULL
} },
552 static const u_char
*
553 isakmp_t_print(const struct isakmp_gen
*ext
, const u_char
*ep
,
554 u_int32_t phase _U_
, u_int32_t doi _U_
, u_int32_t proto
,
557 const struct isakmp_pl_t
*p
;
558 struct isakmp_pl_t t
;
561 const struct attrmap
*map
;
565 printf("%s:", NPSTR(ISAKMP_NPTYPE_T
));
567 p
= (struct isakmp_pl_t
*)ext
;
568 safememcpy(&t
, ext
, sizeof(t
));
572 idstr
= STR_OR_ID(t
.t_id
, isakmp_p_map
);
574 nmap
= sizeof(oakley_t_map
)/sizeof(oakley_t_map
[0]);
577 idstr
= STR_OR_ID(t
.t_id
, ah_p_map
);
579 nmap
= sizeof(ipsec_t_map
)/sizeof(ipsec_t_map
[0]);
582 idstr
= STR_OR_ID(t
.t_id
, esp_p_map
);
584 nmap
= sizeof(ipsec_t_map
)/sizeof(ipsec_t_map
[0]);
587 idstr
= STR_OR_ID(t
.t_id
, ipcomp_p_map
);
589 nmap
= sizeof(ipsec_t_map
)/sizeof(ipsec_t_map
[0]);
599 printf(" #%d id=%s ", t
.t_no
, idstr
);
601 printf(" #%d id=%d ", t
.t_no
, t
.t_id
);
602 cp
= (u_char
*)(p
+ 1);
603 ep2
= (u_char
*)p
+ ntohs(t
.h
.len
);
604 while (cp
< ep
&& cp
< ep2
) {
606 cp
= isakmp_attrmap_print(cp
, (ep
< ep2
) ? ep
: ep2
,
609 cp
= isakmp_attr_print(cp
, (ep
< ep2
) ? ep
: ep2
);
616 static const u_char
*
617 isakmp_ke_print(const struct isakmp_gen
*ext
, const u_char
*ep _U_
,
618 u_int32_t phase _U_
, u_int32_t doi _U_
, u_int32_t proto _U_
,
623 printf("%s:", NPSTR(ISAKMP_NPTYPE_KE
));
625 safememcpy(&e
, ext
, sizeof(e
));
626 printf(" key len=%d", ntohs(e
.len
) - 4);
627 if (2 < vflag
&& 4 < ntohs(e
.len
)) {
629 rawprint((caddr_t
)(ext
+ 1), ntohs(e
.len
) - 4);
631 return (u_char
*)ext
+ ntohs(e
.len
);
634 static const u_char
*
635 isakmp_id_print(const struct isakmp_gen
*ext
, const u_char
*ep _U_
,
636 u_int32_t phase
, u_int32_t doi _U_
, u_int32_t proto _U_
,
639 #define USE_IPSECDOI_IN_PHASE1 1
640 const struct isakmp_pl_id
*p
;
641 struct isakmp_pl_id id
;
642 static const char *idtypestr
[] = {
643 "IPv4", "IPv4net", "IPv6", "IPv6net",
645 static const char *ipsecidtypestr
[] = {
646 NULL
, "IPv4", "FQDN", "user FQDN", "IPv4net", "IPv6",
647 "IPv6net", "IPv4range", "IPv6range", "ASN1 DN", "ASN1 GN",
653 printf("%s:", NPSTR(ISAKMP_NPTYPE_ID
));
655 p
= (struct isakmp_pl_id
*)ext
;
656 safememcpy(&id
, ext
, sizeof(id
));
657 if (sizeof(*p
) < id
.h
.len
)
658 data
= (u_char
*)(p
+ 1);
661 len
= ntohs(id
.h
.len
) - sizeof(*p
);
664 printf(" [phase=%d doi=%d proto=%d]", phase
, doi
, proto
);
667 #ifndef USE_IPSECDOI_IN_PHASE1
671 printf(" idtype=%s", STR_OR_ID(id
.d
.id_type
, idtypestr
));
672 printf(" doi_data=%u",
673 (u_int32_t
)(ntohl(id
.d
.doi_data
) & 0xffffff));
676 #ifdef USE_IPSECDOI_IN_PHASE1
681 const struct ipsecdoi_id
*p
;
682 struct ipsecdoi_id id
;
685 p
= (struct ipsecdoi_id
*)ext
;
686 safememcpy(&id
, ext
, sizeof(id
));
687 printf(" idtype=%s", STR_OR_ID(id
.type
, ipsecidtypestr
));
692 pe
= getprotobynumber(id
.proto_id
);
694 printf(" protoid=%s", pe
->p_name
);
699 /* it DOES NOT mean IPPROTO_IP! */
700 printf(" protoid=%s", "0");
702 printf(" port=%d", ntohs(id
.port
));
706 case IPSECDOI_ID_IPV4_ADDR
:
707 printf(" len=%d %s", len
, ipaddr_string(data
));
710 case IPSECDOI_ID_FQDN
:
711 case IPSECDOI_ID_USER_FQDN
:
714 printf(" len=%d ", len
);
715 for (i
= 0; i
< len
; i
++)
716 safeputchar(data
[i
]);
720 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
723 mask
= data
+ sizeof(struct in_addr
);
724 printf(" len=%d %s/%u.%u.%u.%u", len
,
726 mask
[0], mask
[1], mask
[2], mask
[3]);
731 case IPSECDOI_ID_IPV6_ADDR
:
732 printf(" len=%d %s", len
, ip6addr_string(data
));
735 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
737 const u_int32_t
*mask
;
738 mask
= (u_int32_t
*)(data
+ sizeof(struct in6_addr
));
740 printf(" len=%d %s/0x%08x%08x%08x%08x", len
,
741 ip6addr_string(data
),
742 mask
[0], mask
[1], mask
[2], mask
[3]);
747 case IPSECDOI_ID_IPV4_ADDR_RANGE
:
748 printf(" len=%d %s-%s", len
, ipaddr_string(data
),
749 ipaddr_string(data
+ sizeof(struct in_addr
)));
753 case IPSECDOI_ID_IPV6_ADDR_RANGE
:
754 printf(" len=%d %s-%s", len
, ip6addr_string(data
),
755 ip6addr_string(data
+ sizeof(struct in6_addr
)));
759 case IPSECDOI_ID_DER_ASN1_DN
:
760 case IPSECDOI_ID_DER_ASN1_GN
:
761 case IPSECDOI_ID_KEY_ID
:
768 printf(" len=%d", len
);
771 rawprint((caddr_t
)data
, len
);
774 return (u_char
*)ext
+ ntohs(id
.h
.len
);
777 static const u_char
*
778 isakmp_cert_print(const struct isakmp_gen
*ext
, const u_char
*ep _U_
,
779 u_int32_t phase _U_
, u_int32_t doi0 _U_
, u_int32_t proto0 _U_
,
782 const struct isakmp_pl_cert
*p
;
783 struct isakmp_pl_cert cert
;
784 static const char *certstr
[] = {
785 "none", "pkcs7", "pgp", "dns",
786 "x509sign", "x509ke", "kerberos", "crl",
787 "arl", "spki", "x509attr",
790 printf("%s:", NPSTR(ISAKMP_NPTYPE_CERT
));
792 p
= (struct isakmp_pl_cert
*)ext
;
793 safememcpy(&cert
, ext
, sizeof(cert
));
794 printf(" len=%d", ntohs(cert
.h
.len
) - 4);
795 printf(" type=%s", STR_OR_ID((cert
.encode
), certstr
));
796 if (2 < vflag
&& 4 < ntohs(cert
.h
.len
)) {
798 rawprint((caddr_t
)(ext
+ 1), ntohs(cert
.h
.len
) - 4);
800 return (u_char
*)ext
+ ntohs(cert
.h
.len
);
803 static const u_char
*
804 isakmp_cr_print(const struct isakmp_gen
*ext
, const u_char
*ep _U_
,
805 u_int32_t phase _U_
, u_int32_t doi0 _U_
, u_int32_t proto0 _U_
,
808 const struct isakmp_pl_cert
*p
;
809 struct isakmp_pl_cert cert
;
810 static const char *certstr
[] = {
811 "none", "pkcs7", "pgp", "dns",
812 "x509sign", "x509ke", "kerberos", "crl",
813 "arl", "spki", "x509attr",
816 printf("%s:", NPSTR(ISAKMP_NPTYPE_CR
));
818 p
= (struct isakmp_pl_cert
*)ext
;
819 safememcpy(&cert
, ext
, sizeof(cert
));
820 printf(" len=%d", ntohs(cert
.h
.len
) - 4);
821 printf(" type=%s", STR_OR_ID((cert
.encode
), certstr
));
822 if (2 < vflag
&& 4 < ntohs(cert
.h
.len
)) {
824 rawprint((caddr_t
)(ext
+ 1), ntohs(cert
.h
.len
) - 4);
826 return (u_char
*)ext
+ ntohs(cert
.h
.len
);
829 static const u_char
*
830 isakmp_hash_print(const struct isakmp_gen
*ext
, const u_char
*ep _U_
,
831 u_int32_t phase _U_
, u_int32_t doi _U_
, u_int32_t proto _U_
,
836 printf("%s:", NPSTR(ISAKMP_NPTYPE_HASH
));
838 safememcpy(&e
, ext
, sizeof(e
));
839 printf(" len=%d", ntohs(e
.len
) - 4);
840 if (2 < vflag
&& 4 < ntohs(e
.len
)) {
842 rawprint((caddr_t
)(ext
+ 1), ntohs(e
.len
) - 4);
844 return (u_char
*)ext
+ ntohs(e
.len
);
847 static const u_char
*
848 isakmp_sig_print(const struct isakmp_gen
*ext
, const u_char
*ep _U_
,
849 u_int32_t phase _U_
, u_int32_t doi _U_
, u_int32_t proto _U_
,
854 printf("%s:", NPSTR(ISAKMP_NPTYPE_SIG
));
856 safememcpy(&e
, ext
, sizeof(e
));
857 printf(" len=%d", ntohs(e
.len
) - 4);
858 if (2 < vflag
&& 4 < ntohs(e
.len
)) {
860 rawprint((caddr_t
)(ext
+ 1), ntohs(e
.len
) - 4);
862 return (u_char
*)ext
+ ntohs(e
.len
);
865 static const u_char
*
866 isakmp_nonce_print(const struct isakmp_gen
*ext
, const u_char
*ep _U_
,
867 u_int32_t phase _U_
, u_int32_t doi _U_
, u_int32_t proto _U_
,
872 printf("%s:", NPSTR(ISAKMP_NPTYPE_NONCE
));
874 safememcpy(&e
, ext
, sizeof(e
));
875 printf(" n len=%d", ntohs(e
.len
) - 4);
876 if (2 < vflag
&& 4 < ntohs(e
.len
)) {
878 rawprint((caddr_t
)(ext
+ 1), ntohs(e
.len
) - 4);
880 return (u_char
*)ext
+ ntohs(e
.len
);
883 static const u_char
*
884 isakmp_n_print(const struct isakmp_gen
*ext
, const u_char
*ep
, u_int32_t phase
,
885 u_int32_t doi0 _U_
, u_int32_t proto0 _U_
, int depth
)
887 struct isakmp_pl_n
*p
, n
;
892 static const char *notify_error_str
[] = {
893 NULL
, "INVALID-PAYLOAD-TYPE",
894 "DOI-NOT-SUPPORTED", "SITUATION-NOT-SUPPORTED",
895 "INVALID-COOKIE", "INVALID-MAJOR-VERSION",
896 "INVALID-MINOR-VERSION", "INVALID-EXCHANGE-TYPE",
897 "INVALID-FLAGS", "INVALID-MESSAGE-ID",
898 "INVALID-PROTOCOL-ID", "INVALID-SPI",
899 "INVALID-TRANSFORM-ID", "ATTRIBUTES-NOT-SUPPORTED",
900 "NO-PROPOSAL-CHOSEN", "BAD-PROPOSAL-SYNTAX",
901 "PAYLOAD-MALFORMED", "INVALID-KEY-INFORMATION",
902 "INVALID-ID-INFORMATION", "INVALID-CERT-ENCODING",
903 "INVALID-CERTIFICATE", "CERT-TYPE-UNSUPPORTED",
904 "INVALID-CERT-AUTHORITY", "INVALID-HASH-INFORMATION",
905 "AUTHENTICATION-FAILED", "INVALID-SIGNATURE",
906 "ADDRESS-NOTIFICATION", "NOTIFY-SA-LIFETIME",
907 "CERTIFICATE-UNAVAILABLE", "UNSUPPORTED-EXCHANGE-TYPE",
908 "UNEQUAL-PAYLOAD-LENGTHS",
910 static const char *ipsec_notify_error_str
[] = {
913 static const char *notify_status_str
[] = {
916 static const char *ipsec_notify_status_str
[] = {
917 "RESPONDER-LIFETIME", "REPLAY-STATUS",
920 /* NOTE: these macro must be called with x in proper range */
923 #define NOTIFY_ERROR_STR(x) \
924 STR_OR_ID((x), notify_error_str)
927 #define IPSEC_NOTIFY_ERROR_STR(x) \
928 STR_OR_ID((u_int)((x) - 8192), ipsec_notify_error_str)
931 #define NOTIFY_STATUS_STR(x) \
932 STR_OR_ID((u_int)((x) - 16384), notify_status_str)
935 #define IPSEC_NOTIFY_STATUS_STR(x) \
936 STR_OR_ID((u_int)((x) - 24576), ipsec_notify_status_str)
938 printf("%s:", NPSTR(ISAKMP_NPTYPE_N
));
940 p
= (struct isakmp_pl_n
*)ext
;
941 safememcpy(&n
, ext
, sizeof(n
));
945 printf(" doi=%d", doi
);
946 printf(" proto=%d", proto
);
947 if (ntohs(n
.type
) < 8192)
948 printf(" type=%s", NOTIFY_ERROR_STR(ntohs(n
.type
)));
949 else if (ntohs(n
.type
) < 16384)
950 printf(" type=%s", numstr(ntohs(n
.type
)));
951 else if (ntohs(n
.type
) < 24576)
952 printf(" type=%s", NOTIFY_STATUS_STR(ntohs(n
.type
)));
954 printf(" type=%s", numstr(ntohs(n
.type
)));
957 rawprint((caddr_t
)(p
+ 1), n
.spi_size
);
959 return (u_char
*)(p
+ 1) + n
.spi_size
;
962 printf(" doi=ipsec");
963 printf(" proto=%s", PROTOIDSTR(proto
));
964 if (ntohs(n
.type
) < 8192)
965 printf(" type=%s", NOTIFY_ERROR_STR(ntohs(n
.type
)));
966 else if (ntohs(n
.type
) < 16384)
967 printf(" type=%s", IPSEC_NOTIFY_ERROR_STR(ntohs(n
.type
)));
968 else if (ntohs(n
.type
) < 24576)
969 printf(" type=%s", NOTIFY_STATUS_STR(ntohs(n
.type
)));
970 else if (ntohs(n
.type
) < 32768)
971 printf(" type=%s", IPSEC_NOTIFY_STATUS_STR(ntohs(n
.type
)));
973 printf(" type=%s", numstr(ntohs(n
.type
)));
976 rawprint((caddr_t
)(p
+ 1), n
.spi_size
);
979 cp
= (u_char
*)(p
+ 1) + n
.spi_size
;
980 ep2
= (u_char
*)p
+ ntohs(n
.h
.len
);
984 switch (ntohs(n
.type
)) {
985 case IPSECDOI_NTYPE_RESPONDER_LIFETIME
:
987 const struct attrmap
*map
= oakley_t_map
;
988 size_t nmap
= sizeof(oakley_t_map
)/sizeof(oakley_t_map
[0]);
989 while (cp
< ep
&& cp
< ep2
) {
990 cp
= isakmp_attrmap_print(cp
,
991 (ep
< ep2
) ? ep
: ep2
, map
, nmap
);
995 case IPSECDOI_NTYPE_REPLAY_STATUS
:
996 printf("replay detection %sabled",
997 (*(u_int32_t
*)cp
) ? "en" : "dis");
999 case ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
:
1000 if (isakmp_sub_print(ISAKMP_NPTYPE_SA
,
1001 (struct isakmp_gen
*)cp
, ep
, phase
, doi
, proto
,
1008 ntohs(n
.h
.len
) - sizeof(*p
) - n
.spi_size
,
1013 return (u_char
*)ext
+ ntohs(n
.h
.len
);
1016 static const u_char
*
1017 isakmp_d_print(const struct isakmp_gen
*ext
, const u_char
*ep _U_
,
1018 u_int32_t phase _U_
, u_int32_t doi0 _U_
, u_int32_t proto0 _U_
,
1021 const struct isakmp_pl_d
*p
;
1022 struct isakmp_pl_d d
;
1028 printf("%s:", NPSTR(ISAKMP_NPTYPE_D
));
1030 p
= (struct isakmp_pl_d
*)ext
;
1031 safememcpy(&d
, ext
, sizeof(d
));
1035 printf(" doi=%u", doi
);
1036 printf(" proto=%u", proto
);
1038 printf(" doi=ipsec");
1039 printf(" proto=%s", PROTOIDSTR(proto
));
1041 printf(" spilen=%u", d
.spi_size
);
1042 printf(" nspi=%u", ntohs(d
.num_spi
));
1044 q
= (u_int8_t
*)(p
+ 1);
1045 for (i
= 0; i
< ntohs(d
.num_spi
); i
++) {
1048 rawprint((caddr_t
)q
, d
.spi_size
);
1054 static const u_char
*
1055 isakmp_vid_print(const struct isakmp_gen
*ext
, const u_char
*ep _U_
,
1056 u_int32_t phase _U_
, u_int32_t doi _U_
, u_int32_t proto _U_
,
1059 struct isakmp_gen e
;
1061 printf("%s:", NPSTR(ISAKMP_NPTYPE_VID
));
1063 safememcpy(&e
, ext
, sizeof(e
));
1064 printf(" len=%d", ntohs(e
.len
) - 4);
1065 if (2 < vflag
&& 4 < ntohs(e
.len
)) {
1067 rawprint((caddr_t
)(ext
+ 1), ntohs(e
.len
) - 4);
1069 return (u_char
*)ext
+ ntohs(e
.len
);
1072 static const u_char
*
1073 isakmp_sub0_print(u_char np
, const struct isakmp_gen
*ext
, const u_char
*ep
,
1074 u_int32_t phase
, u_int32_t doi
, u_int32_t proto
, int depth
)
1077 struct isakmp_gen e
;
1081 safememcpy(&e
, ext
, sizeof(e
));
1084 * Since we can't have a payload length of less than 4 bytes,
1085 * we need to bail out here if the generic header is nonsensical
1086 * or truncated, otherwise we could loop forever processing
1087 * zero-length items or otherwise misdissect the packet.
1089 item_len
= ntohs(e
.len
);
1095 * XXX - what if item_len is too short, or too long,
1096 * for this payload type?
1098 cp
= (*NPFUNC(np
))(ext
, ep
, phase
, doi
, proto
, depth
);
1100 printf("%s", NPSTR(np
));
1107 static const u_char
*
1108 isakmp_sub_print(u_char np
, const struct isakmp_gen
*ext
, const u_char
*ep
,
1109 u_int32_t phase
, u_int32_t doi
, u_int32_t proto
, int depth
)
1113 struct isakmp_gen e
;
1115 cp
= (const u_char
*)ext
;
1118 TCHECK2(*ext
, sizeof(e
));
1120 safememcpy(&e
, ext
, sizeof(e
));
1122 if (ep
< (u_char
*)ext
+ ntohs(e
.len
)) {
1123 printf(" [|%s]", NPSTR(np
));
1129 for (i
= 0; i
< depth
; i
++)
1132 cp
= isakmp_sub0_print(np
, ext
, ep
, phase
, doi
, proto
, depth
);
1137 /* Zero-length subitem */
1142 ext
= (struct isakmp_gen
*)cp
;
1152 static char buf
[20];
1153 snprintf(buf
, sizeof(buf
), "#%d", x
);
1158 * some compiler tries to optimize memcpy(), using the alignment constraint
1159 * on the argument pointer type. by using this function, we try to avoid the
1163 safememcpy(void *p
, const void *q
, size_t l
)
1169 isakmp_print(const u_char
*bp
, u_int length
, const u_char
*bp2
)
1171 const struct isakmp
*p
;
1179 p
= (const struct isakmp
*)bp
;
1182 if ((struct isakmp
*)ep
< p
+ 1) {
1183 printf("[|isakmp]");
1187 safememcpy(&base
, p
, sizeof(base
));
1191 major
= (base
.vers
& ISAKMP_VERS_MAJOR
)
1192 >> ISAKMP_VERS_MAJOR_SHIFT
;
1193 minor
= (base
.vers
& ISAKMP_VERS_MINOR
)
1194 >> ISAKMP_VERS_MINOR_SHIFT
;
1195 printf(" %d.%d", major
, minor
);
1200 rawprint((caddr_t
)&base
.msgid
, sizeof(base
.msgid
));
1205 rawprint((caddr_t
)&base
.i_ck
, sizeof(base
.i_ck
));
1207 rawprint((caddr_t
)&base
.r_ck
, sizeof(base
.r_ck
));
1211 phase
= (*(u_int32_t
*)base
.msgid
== 0) ? 1 : 2;
1213 printf(" phase %d", phase
);
1215 printf(" phase %d/others", phase
);
1217 i
= cookie_find(&base
.i_ck
);
1219 if (iszero((u_char
*)&base
.r_ck
, sizeof(base
.r_ck
))) {
1220 /* the first packet */
1223 cookie_record(&base
.i_ck
, bp2
);
1227 if (bp2
&& cookie_isinitiator(i
, bp2
))
1229 else if (bp2
&& cookie_isresponder(i
, bp2
))
1235 printf(" %s", ETYPESTR(base
.etype
));
1237 printf("[%s%s]", base
.flags
& ISAKMP_FLAG_E
? "E" : "",
1238 base
.flags
& ISAKMP_FLAG_C
? "C" : "");
1242 const struct isakmp_gen
*ext
;
1245 #define CHECKLEN(p, np) \
1246 if (ep < (u_char *)(p)) { \
1247 printf(" [|%s]", NPSTR(np)); \
1253 /* regardless of phase... */
1254 if (base
.flags
& ISAKMP_FLAG_E
) {
1256 * encrypted, nothing we can do right now.
1257 * we hope to decrypt the packet in the future...
1259 printf(" [encrypted %s]", NPSTR(base
.np
));
1264 CHECKLEN(p
+ 1, base
.np
)
1267 ext
= (struct isakmp_gen
*)(p
+ 1);
1268 isakmp_sub_print(np
, ext
, ep
, phase
, 0, 0, 0);
1273 if (ntohl(base
.len
) != length
) {
1274 printf(" (len mismatch: isakmp %u/ip %u)",
1275 (u_int32_t
)ntohl(base
.len
), length
);