]>
Commit | Line | Data |
---|---|---|
52b7d2ce A |
1 | /* $KAME: pfkey_dump.c,v 1.45 2003/09/08 10:14:56 itojun Exp $ */ |
2 | ||
3 | /* | |
4 | * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. | |
5 | * All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * 3. Neither the name of the project nor the names of its contributors | |
16 | * may be used to endorse or promote products derived from this software | |
17 | * without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | |
30 | */ | |
31 | ||
32 | #ifdef HAVE_CONFIG_H | |
33 | #include "config.h" | |
34 | #endif | |
35 | ||
36 | #include <sys/types.h> | |
37 | #include <sys/param.h> | |
38 | #include <sys/socket.h> | |
39 | #ifdef HAVE_NETINET6_IPSEC | |
40 | # include <netinet6/ipsec.h> | |
41 | #else | |
42 | # include <netinet/ipsec.h> | |
43 | #endif | |
44 | ||
52b7d2ce | 45 | #include <System/net/pfkeyv2.h> |
52b7d2ce A |
46 | |
47 | #include <netinet/in.h> | |
48 | #include <arpa/inet.h> | |
49 | ||
50 | #include <stdlib.h> | |
51 | #include <unistd.h> | |
52 | #include <stdio.h> | |
53 | #include <string.h> | |
54 | #include <time.h> | |
55 | #include <netdb.h> | |
56 | ||
57 | #include "ipsec_strerror.h" | |
58 | #include "libpfkey.h" | |
59 | ||
60 | /* cope with old kame headers - ugly */ | |
61 | #ifndef SADB_X_AALG_MD5 | |
62 | #define SADB_X_AALG_MD5 SADB_AALG_MD5 | |
63 | #endif | |
64 | #ifndef SADB_X_AALG_SHA | |
65 | #define SADB_X_AALG_SHA SADB_AALG_SHA | |
66 | #endif | |
67 | #ifndef SADB_X_AALG_NULL | |
68 | #define SADB_X_AALG_NULL SADB_AALG_NULL | |
69 | #endif | |
70 | ||
71 | #ifndef SADB_X_EALG_BLOWFISHCBC | |
72 | #define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC | |
73 | #endif | |
74 | #ifndef SADB_X_EALG_CAST128CBC | |
75 | #define SADB_X_EALG_CAST128CBC SADB_EALG_CAST128CBC | |
76 | #endif | |
77 | #ifndef SADB_X_EALG_RC5CBC | |
78 | #ifdef SADB_EALG_RC5CBC | |
79 | #define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC | |
80 | #endif | |
81 | #endif | |
82 | ||
83 | #define GETMSGSTR(str, num) \ | |
84 | do { \ | |
85 | /*CONSTCOND*/ \ | |
86 | if (sizeof((str)[0]) == 0 \ | |
87 | || num >= sizeof(str)/sizeof((str)[0])) \ | |
88 | printf("%u ", (num)); \ | |
89 | else if (strlen((str)[(num)]) == 0) \ | |
90 | printf("%u ", (num)); \ | |
91 | else \ | |
92 | printf("%s ", (str)[(num)]); \ | |
93 | } while (/*CONSTCOND*/0) | |
94 | ||
95 | #define GETMSGV2S(v2s, num) \ | |
96 | do { \ | |
97 | struct val2str *p; \ | |
98 | for (p = (v2s); p && p->str; p++) { \ | |
99 | if (p->val == (num)) \ | |
100 | break; \ | |
101 | } \ | |
102 | if (p && p->str) \ | |
103 | printf("%s ", p->str); \ | |
104 | else \ | |
105 | printf("%u ", (num)); \ | |
106 | } while (/*CONSTCOND*/0) | |
107 | ||
108 | static char *str_ipaddr __P((struct sockaddr *)); | |
109 | static char *str_ipport __P((struct sockaddr *)); | |
110 | static char *str_prefport __P((u_int, u_int, u_int, u_int)); | |
111 | static void str_upperspec __P((u_int, u_int, u_int)); | |
112 | static char *str_time __P((time_t)); | |
113 | static void str_lifetime_byte __P((struct sadb_lifetime *, char *)); | |
114 | static void pfkey_sadump1(struct sadb_msg *, int); | |
115 | static void pfkey_spdump1(struct sadb_msg *, int); | |
116 | ||
117 | struct val2str { | |
118 | int val; | |
119 | const char *str; | |
120 | }; | |
121 | ||
122 | /* | |
123 | * Must to be re-written about following strings. | |
124 | */ | |
125 | static char *str_satype[] = { | |
126 | "unspec", | |
127 | "unknown", | |
128 | "ah", | |
129 | "esp", | |
130 | "unknown", | |
131 | "rsvp", | |
132 | "ospfv2", | |
133 | "ripv2", | |
134 | "mip", | |
135 | "ipcomp", | |
136 | "policy", | |
137 | "tcp", | |
138 | }; | |
139 | ||
140 | static char *str_mode[] = { | |
141 | "any", | |
142 | "transport", | |
143 | "tunnel", | |
144 | }; | |
145 | ||
146 | static char *str_state[] = { | |
147 | "larval", | |
148 | "mature", | |
149 | "dying", | |
150 | "dead", | |
151 | }; | |
152 | ||
153 | static struct val2str str_alg_auth[] = { | |
154 | { SADB_AALG_NONE, "none", }, | |
155 | { SADB_AALG_MD5HMAC, "hmac-md5", }, | |
156 | { SADB_AALG_SHA1HMAC, "hmac-sha1", }, | |
157 | { SADB_X_AALG_MD5, "md5", }, | |
158 | { SADB_X_AALG_SHA, "sha", }, | |
159 | { SADB_X_AALG_NULL, "null", }, | |
160 | #ifdef SADB_X_AALG_TCP_MD5 | |
161 | { SADB_X_AALG_TCP_MD5, "tcp-md5", }, | |
162 | #endif | |
163 | #ifdef SADB_X_AALG_SHA2_256 | |
164 | { SADB_X_AALG_SHA2_256, "hmac-sha256", }, | |
165 | #endif | |
166 | #ifdef SADB_X_AALG_SHA2_384 | |
167 | { SADB_X_AALG_SHA2_384, "hmac-sha384", }, | |
168 | #endif | |
169 | #ifdef SADB_X_AALG_SHA2_512 | |
170 | { SADB_X_AALG_SHA2_512, "hmac-sha512", }, | |
171 | #endif | |
172 | #ifdef SADB_X_AALG_RIPEMD160HMAC | |
173 | { SADB_X_AALG_RIPEMD160HMAC, "hmac-ripemd160", }, | |
174 | #endif | |
175 | #ifdef SADB_X_AALG_AES_XCBC_MAC | |
176 | { SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", }, | |
177 | #endif | |
178 | { -1, NULL, }, | |
179 | }; | |
180 | ||
181 | static struct val2str str_alg_enc[] = { | |
182 | { SADB_EALG_NONE, "none", }, | |
183 | { SADB_EALG_DESCBC, "des-cbc", }, | |
184 | { SADB_EALG_3DESCBC, "3des-cbc", }, | |
185 | { SADB_EALG_NULL, "null", }, | |
186 | #ifdef SADB_X_EALG_RC5CBC | |
187 | { SADB_X_EALG_RC5CBC, "rc5-cbc", }, | |
188 | #endif | |
189 | { SADB_X_EALG_CAST128CBC, "cast128-cbc", }, | |
190 | { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", }, | |
191 | #ifdef SADB_X_EALG_AESCBC | |
192 | { SADB_X_EALG_AESCBC, "aes-cbc", }, | |
193 | #endif | |
194 | #ifdef SADB_X_EALG_TWOFISHCBC | |
195 | { SADB_X_EALG_TWOFISHCBC, "twofish-cbc", }, | |
196 | #endif | |
197 | #ifdef SADB_X_EALG_AESCTR | |
198 | { SADB_X_EALG_AESCTR, "aes-ctr", }, | |
199 | #endif | |
200 | { -1, NULL, }, | |
201 | }; | |
202 | ||
203 | static struct val2str str_alg_comp[] = { | |
204 | { SADB_X_CALG_NONE, "none", }, | |
205 | { SADB_X_CALG_OUI, "oui", }, | |
206 | { SADB_X_CALG_DEFLATE, "deflate", }, | |
207 | { SADB_X_CALG_LZS, "lzs", }, | |
208 | { -1, NULL, }, | |
209 | }; | |
210 | ||
211 | /* | |
212 | * dump SADB_MSG formated. For debugging, you should use kdebug_sadb(). | |
213 | */ | |
214 | ||
215 | void | |
216 | pfkey_sadump(m) | |
217 | struct sadb_msg *m; | |
218 | { | |
219 | pfkey_sadump1(m, 0); | |
220 | } | |
221 | ||
222 | void | |
223 | pfkey_sadump_withports(m) | |
224 | struct sadb_msg *m; | |
225 | { | |
226 | pfkey_sadump1(m, 1); | |
227 | } | |
228 | ||
229 | void | |
230 | pfkey_sadump1(m, withports) | |
231 | struct sadb_msg *m; | |
232 | int withports; | |
233 | { | |
234 | caddr_t mhp[SADB_EXT_MAX + 1]; | |
235 | struct sadb_sa *m_sa; | |
236 | struct sadb_x_sa2 *m_sa2; | |
237 | struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts; | |
238 | struct sadb_address *m_saddr, *m_daddr; | |
239 | #ifdef notdef | |
240 | struct sadb_address *m_paddr; | |
241 | #endif | |
242 | struct sadb_key *m_auth, *m_enc; | |
243 | #ifdef notdef | |
244 | struct sadb_ident *m_sid, *m_did; | |
245 | struct sadb_sens *m_sens; | |
246 | #endif | |
247 | #ifdef SADB_X_EXT_NAT_T_TYPE | |
248 | struct sadb_x_nat_t_type *natt_type; | |
249 | struct sadb_x_nat_t_port *natt_sport, *natt_dport; | |
250 | struct sadb_address *natt_oa; | |
251 | ||
252 | int use_natt = 0; | |
253 | #endif | |
254 | struct sockaddr *sa; | |
255 | ||
256 | /* check pfkey message. */ | |
257 | if (pfkey_align(m, mhp)) { | |
258 | printf("%s\n", ipsec_strerror()); | |
259 | return; | |
260 | } | |
261 | if (pfkey_check(mhp)) { | |
262 | printf("%s\n", ipsec_strerror()); | |
263 | return; | |
264 | } | |
265 | ||
266 | m_sa = (void *)mhp[SADB_EXT_SA]; | |
267 | m_sa2 = (void *)mhp[SADB_X_EXT_SA2]; | |
268 | m_lftc = (void *)mhp[SADB_EXT_LIFETIME_CURRENT]; | |
269 | m_lfth = (void *)mhp[SADB_EXT_LIFETIME_HARD]; | |
270 | m_lfts = (void *)mhp[SADB_EXT_LIFETIME_SOFT]; | |
271 | m_saddr = (void *)mhp[SADB_EXT_ADDRESS_SRC]; | |
272 | m_daddr = (void *)mhp[SADB_EXT_ADDRESS_DST]; | |
273 | #ifdef notdef | |
274 | m_paddr = (void *)mhp[SADB_EXT_ADDRESS_PROXY]; | |
275 | #endif | |
276 | m_auth = (void *)mhp[SADB_EXT_KEY_AUTH]; | |
277 | m_enc = (void *)mhp[SADB_EXT_KEY_ENCRYPT]; | |
278 | #ifdef notdef | |
279 | m_sid = (void *)mhp[SADB_EXT_IDENTITY_SRC]; | |
280 | m_did = (void *)mhp[SADB_EXT_IDENTITY_DST]; | |
281 | m_sens = (void *)mhp[SADB_EXT_SENSITIVITY]; | |
282 | #endif | |
283 | #ifdef SADB_X_EXT_NAT_T_TYPE | |
284 | natt_type = (void *)mhp[SADB_X_EXT_NAT_T_TYPE]; | |
285 | natt_sport = (void *)mhp[SADB_X_EXT_NAT_T_SPORT]; | |
286 | natt_dport = (void *)mhp[SADB_X_EXT_NAT_T_DPORT]; | |
287 | natt_oa = (void *)mhp[SADB_X_EXT_NAT_T_OA]; | |
288 | ||
289 | if (natt_type && natt_type->sadb_x_nat_t_type_type) | |
290 | use_natt = 1; | |
291 | #endif | |
292 | /* source address */ | |
293 | if (m_saddr == NULL) { | |
294 | printf("no ADDRESS_SRC extension.\n"); | |
295 | return; | |
296 | } | |
297 | sa = (void *)(m_saddr + 1); | |
298 | if (withports) | |
299 | printf("%s[%s]", str_ipaddr(sa), str_ipport(sa)); | |
300 | else | |
301 | printf("%s", str_ipaddr(sa)); | |
302 | #ifdef SADB_X_EXT_NAT_T_TYPE | |
303 | if (use_natt && natt_sport) | |
304 | printf("[%u]", ntohs(natt_sport->sadb_x_nat_t_port_port)); | |
305 | #endif | |
306 | printf(" "); | |
307 | ||
308 | /* destination address */ | |
309 | if (m_daddr == NULL) { | |
310 | printf(" no ADDRESS_DST extension.\n"); | |
311 | return; | |
312 | } | |
313 | sa = (void *)(m_daddr + 1); | |
314 | if (withports) | |
315 | printf("%s[%s]", str_ipaddr(sa), str_ipport(sa)); | |
316 | else | |
317 | printf("%s", str_ipaddr(sa)); | |
318 | #ifdef SADB_X_EXT_NAT_T_TYPE | |
319 | if (use_natt && natt_dport) | |
320 | printf("[%u]", ntohs(natt_dport->sadb_x_nat_t_port_port)); | |
321 | #endif | |
322 | printf(" "); | |
323 | ||
324 | /* SA type */ | |
325 | if (m_sa == NULL) { | |
326 | printf("no SA extension.\n"); | |
327 | return; | |
328 | } | |
329 | if (m_sa2 == NULL) { | |
330 | printf("no SA2 extension.\n"); | |
331 | return; | |
332 | } | |
333 | printf("\n\t"); | |
334 | ||
335 | #ifdef SADB_X_EXT_NAT_T_TYPE | |
336 | if (use_natt && m->sadb_msg_satype == SADB_SATYPE_ESP) | |
337 | printf("esp-udp "); | |
338 | else if (use_natt) | |
339 | printf("natt+"); | |
340 | ||
341 | if (!use_natt || m->sadb_msg_satype != SADB_SATYPE_ESP) | |
342 | #endif | |
343 | GETMSGSTR(str_satype, m->sadb_msg_satype); | |
344 | ||
345 | printf("mode="); | |
346 | GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode); | |
347 | ||
348 | printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n", | |
349 | (u_int32_t)ntohl(m_sa->sadb_sa_spi), | |
350 | (u_int32_t)ntohl(m_sa->sadb_sa_spi), | |
351 | (u_int32_t)m_sa2->sadb_x_sa2_reqid, | |
352 | (u_int32_t)m_sa2->sadb_x_sa2_reqid); | |
353 | ||
354 | #ifdef SADB_X_EXT_NAT_T_TYPE | |
355 | /* other NAT-T information */ | |
356 | if (use_natt && natt_oa) | |
357 | printf("\tNAT OA=%s\n", | |
358 | str_ipaddr((void *)(natt_oa + 1))); | |
359 | #endif | |
360 | ||
361 | /* encryption key */ | |
362 | if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) { | |
363 | printf("\tC: "); | |
364 | GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt); | |
365 | } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) { | |
366 | if (m_enc != NULL) { | |
367 | printf("\tE: "); | |
368 | GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt); | |
369 | ipsec_hexdump((caddr_t)(void *)m_enc + sizeof(*m_enc), | |
370 | m_enc->sadb_key_bits / 8); | |
371 | printf("\n"); | |
372 | } | |
373 | } | |
374 | ||
375 | /* authentication key */ | |
376 | if (m_auth != NULL) { | |
377 | printf("\tA: "); | |
378 | GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth); | |
379 | ipsec_hexdump((caddr_t)(void *)m_auth + sizeof(*m_auth), | |
380 | m_auth->sadb_key_bits / 8); | |
381 | printf("\n"); | |
382 | } | |
383 | ||
384 | /* replay windoe size & flags */ | |
385 | printf("\tseq=0x%08x replay=%u flags=0x%08x ", | |
386 | m_sa2->sadb_x_sa2_sequence, | |
387 | m_sa->sadb_sa_replay, | |
388 | m_sa->sadb_sa_flags); | |
389 | ||
390 | /* state */ | |
391 | printf("state="); | |
392 | GETMSGSTR(str_state, m_sa->sadb_sa_state); | |
393 | printf("\n"); | |
394 | ||
395 | /* lifetime */ | |
396 | if (m_lftc != NULL) { | |
397 | time_t tmp_time = time(0); | |
398 | ||
399 | printf("\tcreated: %s", | |
400 | str_time((long)m_lftc->sadb_lifetime_addtime)); | |
401 | printf("\tcurrent: %s\n", str_time(tmp_time)); | |
402 | printf("\tdiff: %lu(s)", | |
403 | (u_long)(m_lftc->sadb_lifetime_addtime == 0 ? | |
404 | 0 : (tmp_time - m_lftc->sadb_lifetime_addtime))); | |
405 | ||
406 | printf("\thard: %lu(s)", | |
407 | (u_long)(m_lfth == NULL ? | |
408 | 0 : m_lfth->sadb_lifetime_addtime)); | |
409 | printf("\tsoft: %lu(s)\n", | |
410 | (u_long)(m_lfts == NULL ? | |
411 | 0 : m_lfts->sadb_lifetime_addtime)); | |
412 | ||
413 | printf("\tlast: %s", | |
414 | str_time((long)m_lftc->sadb_lifetime_usetime)); | |
415 | printf("\thard: %lu(s)", | |
416 | (u_long)(m_lfth == NULL ? | |
417 | 0 : m_lfth->sadb_lifetime_usetime)); | |
418 | printf("\tsoft: %lu(s)\n", | |
419 | (u_long)(m_lfts == NULL ? | |
420 | 0 : m_lfts->sadb_lifetime_usetime)); | |
421 | ||
422 | str_lifetime_byte(m_lftc, "current"); | |
423 | str_lifetime_byte(m_lfth, "hard"); | |
424 | str_lifetime_byte(m_lfts, "soft"); | |
425 | printf("\n"); | |
426 | ||
427 | printf("\tallocated: %lu", | |
428 | (unsigned long)m_lftc->sadb_lifetime_allocations); | |
429 | printf("\thard: %lu", | |
430 | (u_long)(m_lfth == NULL ? | |
431 | 0 : m_lfth->sadb_lifetime_allocations)); | |
432 | printf("\tsoft: %lu\n", | |
433 | (u_long)(m_lfts == NULL ? | |
434 | 0 : m_lfts->sadb_lifetime_allocations)); | |
435 | } | |
436 | ||
437 | printf("\tsadb_seq=%lu pid=%lu ", | |
438 | (u_long)m->sadb_msg_seq, | |
439 | (u_long)m->sadb_msg_pid); | |
440 | ||
441 | /* XXX DEBUG */ | |
442 | printf("refcnt=%u\n", m->sadb_msg_reserved); | |
443 | ||
444 | return; | |
445 | } | |
446 | ||
447 | void | |
448 | pfkey_spdump(m) | |
449 | struct sadb_msg *m; | |
450 | { | |
451 | pfkey_spdump1(m, 0); | |
452 | } | |
453 | ||
454 | void | |
455 | pfkey_spdump_withports(m) | |
456 | struct sadb_msg *m; | |
457 | { | |
458 | pfkey_spdump1(m, 1); | |
459 | } | |
460 | ||
461 | static void | |
462 | pfkey_spdump1(m, withports) | |
463 | struct sadb_msg *m; | |
464 | int withports; | |
465 | { | |
466 | char pbuf[NI_MAXSERV]; | |
467 | caddr_t mhp[SADB_EXT_MAX + 1]; | |
468 | struct sadb_address *m_saddr, *m_daddr; | |
469 | #ifdef SADB_X_EXT_TAG | |
470 | struct sadb_x_tag *m_tag; | |
471 | #endif | |
472 | struct sadb_x_policy *m_xpl; | |
473 | struct sadb_lifetime *m_lftc = NULL, *m_lfth = NULL; | |
474 | struct sockaddr *sa; | |
475 | u_int16_t sport = 0, dport = 0; | |
476 | ||
477 | /* check pfkey message. */ | |
478 | if (pfkey_align(m, mhp)) { | |
479 | printf("%s\n", ipsec_strerror()); | |
480 | return; | |
481 | } | |
482 | if (pfkey_check(mhp)) { | |
483 | printf("%s\n", ipsec_strerror()); | |
484 | return; | |
485 | } | |
486 | ||
487 | m_saddr = (void *)mhp[SADB_EXT_ADDRESS_SRC]; | |
488 | m_daddr = (void *)mhp[SADB_EXT_ADDRESS_DST]; | |
489 | #ifdef SADB_X_EXT_TAG | |
490 | m_tag = (void *)mhp[SADB_X_EXT_TAG]; | |
491 | #endif | |
492 | m_xpl = (void *)mhp[SADB_X_EXT_POLICY]; | |
493 | m_lftc = (void *)mhp[SADB_EXT_LIFETIME_CURRENT]; | |
494 | m_lfth = (void *)mhp[SADB_EXT_LIFETIME_HARD]; | |
495 | ||
52b7d2ce A |
496 | if (m_saddr && m_daddr) { |
497 | /* source address */ | |
498 | sa = (void *)(m_saddr + 1); | |
499 | switch (sa->sa_family) { | |
500 | case AF_INET: | |
501 | case AF_INET6: | |
502 | if (getnameinfo(sa, (socklen_t)sysdep_sa_len(sa), NULL, | |
503 | 0, pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0) | |
504 | sport = 0; /*XXX*/ | |
505 | else | |
506 | sport = atoi(pbuf); | |
507 | printf("%s%s ", str_ipaddr(sa), | |
508 | str_prefport((u_int)sa->sa_family, | |
509 | (u_int)m_saddr->sadb_address_prefixlen, | |
510 | (u_int)sport, | |
511 | (u_int)m_saddr->sadb_address_proto)); | |
512 | break; | |
513 | default: | |
514 | printf("unknown-af "); | |
515 | break; | |
516 | } | |
517 | ||
518 | /* destination address */ | |
519 | sa = (void *)(m_daddr + 1); | |
520 | switch (sa->sa_family) { | |
521 | case AF_INET: | |
522 | case AF_INET6: | |
523 | if (getnameinfo(sa, (socklen_t)sysdep_sa_len(sa), NULL, | |
524 | 0, pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0) | |
525 | dport = 0; /*XXX*/ | |
526 | else | |
527 | dport = atoi(pbuf); | |
528 | printf("%s%s ", str_ipaddr(sa), | |
529 | str_prefport((u_int)sa->sa_family, | |
530 | (u_int)m_daddr->sadb_address_prefixlen, | |
531 | (u_int)dport, | |
532 | (u_int)m_saddr->sadb_address_proto)); | |
533 | break; | |
534 | default: | |
535 | printf("unknown-af "); | |
536 | break; | |
537 | } | |
538 | ||
539 | /* upper layer protocol */ | |
540 | if (m_saddr->sadb_address_proto != | |
541 | m_daddr->sadb_address_proto) { | |
542 | printf("upper layer protocol mismatched.\n"); | |
543 | return; | |
544 | } | |
545 | str_upperspec((u_int)m_saddr->sadb_address_proto, (u_int)sport, | |
546 | (u_int)dport); | |
547 | } | |
548 | #ifdef SADB_X_EXT_TAG | |
549 | else if (m_tag) | |
550 | printf("tagged \"%s\" ", m_tag->sadb_x_tag_name); | |
551 | #endif | |
552 | else | |
553 | printf("(no selector, probably per-socket policy) "); | |
554 | ||
555 | /* policy */ | |
556 | { | |
557 | char *d_xpl; | |
558 | ||
559 | if (m_xpl == NULL) { | |
560 | printf("no X_POLICY extension.\n"); | |
561 | return; | |
562 | } | |
563 | if (withports) | |
564 | d_xpl = ipsec_dump_policy_withports(m_xpl, "\n\t"); | |
565 | else | |
566 | d_xpl = ipsec_dump_policy((ipsec_policy_t)m_xpl, "\n\t"); | |
567 | ||
568 | if (!d_xpl) | |
569 | printf("\n\tPolicy:[%s]\n", ipsec_strerror()); | |
570 | else { | |
571 | /* dump SPD */ | |
572 | printf("\n\t%s\n", d_xpl); | |
573 | free(d_xpl); | |
574 | } | |
575 | } | |
576 | ||
577 | /* lifetime */ | |
578 | if (m_lftc) { | |
579 | printf("\tcreated: %s ", | |
580 | str_time((long)m_lftc->sadb_lifetime_addtime)); | |
581 | printf("lastused: %s\n", | |
582 | str_time((long)m_lftc->sadb_lifetime_usetime)); | |
583 | } | |
584 | if (m_lfth) { | |
585 | printf("\tlifetime: %lu(s) ", | |
586 | (u_long)m_lfth->sadb_lifetime_addtime); | |
587 | printf("validtime: %lu(s)\n", | |
588 | (u_long)m_lfth->sadb_lifetime_usetime); | |
589 | } | |
590 | ||
591 | ||
592 | printf("\tspid=%ld seq=%ld pid=%ld\n", | |
593 | (u_long)m_xpl->sadb_x_policy_id, | |
594 | (u_long)m->sadb_msg_seq, | |
595 | (u_long)m->sadb_msg_pid); | |
596 | ||
597 | /* XXX TEST */ | |
598 | printf("\trefcnt=%u\n", m->sadb_msg_reserved); | |
599 | ||
600 | return; | |
601 | } | |
602 | ||
603 | /* | |
604 | * set "ipaddress" to buffer. | |
605 | */ | |
606 | static char * | |
607 | str_ipaddr(sa) | |
608 | struct sockaddr *sa; | |
609 | { | |
610 | static char buf[NI_MAXHOST]; | |
611 | const int niflag = NI_NUMERICHOST; | |
612 | ||
613 | if (sa == NULL) | |
614 | return ""; | |
615 | ||
616 | if (getnameinfo(sa, (socklen_t)sysdep_sa_len(sa), buf, sizeof(buf), | |
617 | NULL, 0, niflag) == 0) | |
618 | return buf; | |
619 | return NULL; | |
620 | } | |
621 | ||
622 | /* | |
623 | * set "port" to buffer. | |
624 | */ | |
625 | static char * | |
626 | str_ipport(sa) | |
627 | struct sockaddr *sa; | |
628 | { | |
629 | static char buf[NI_MAXHOST]; | |
630 | const int niflag = NI_NUMERICSERV; | |
631 | ||
632 | if (sa == NULL) | |
633 | return ""; | |
634 | ||
635 | if (getnameinfo(sa, (socklen_t)sysdep_sa_len(sa), NULL, 0, | |
636 | buf, sizeof(buf), niflag) == 0) | |
637 | return buf; | |
638 | return NULL; | |
639 | } | |
640 | ||
641 | ||
642 | /* | |
643 | * set "/prefix[port number]" to buffer. | |
644 | */ | |
645 | static char * | |
646 | str_prefport(family, pref, port, ulp) | |
647 | u_int family, pref, port, ulp; | |
648 | { | |
649 | static char buf[128]; | |
650 | char prefbuf[128]; | |
651 | char portbuf[128]; | |
652 | int plen; | |
653 | ||
654 | switch (family) { | |
655 | case AF_INET: | |
656 | plen = sizeof(struct in_addr) << 3; | |
657 | break; | |
658 | case AF_INET6: | |
659 | plen = sizeof(struct in6_addr) << 3; | |
660 | break; | |
661 | default: | |
662 | return "?"; | |
663 | } | |
664 | ||
665 | if (pref == plen) | |
666 | prefbuf[0] = '\0'; | |
667 | else | |
668 | snprintf(prefbuf, sizeof(prefbuf), "/%u", pref); | |
669 | ||
670 | if (ulp == IPPROTO_ICMPV6) | |
671 | memset(portbuf, 0, sizeof(portbuf)); | |
672 | else { | |
673 | if (port == IPSEC_PORT_ANY) | |
674 | snprintf(portbuf, sizeof(portbuf), "[%s]", "any"); | |
675 | else | |
676 | snprintf(portbuf, sizeof(portbuf), "[%u]", port); | |
677 | } | |
678 | ||
679 | snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf); | |
680 | ||
681 | return buf; | |
682 | } | |
683 | ||
684 | static void | |
685 | str_upperspec(ulp, p1, p2) | |
686 | u_int ulp, p1, p2; | |
687 | { | |
688 | if (ulp == IPSEC_ULPROTO_ANY) | |
689 | printf("any"); | |
690 | else if (ulp == IPPROTO_ICMPV6) { | |
691 | printf("icmp6"); | |
692 | if (!(p1 == IPSEC_PORT_ANY && p2 == IPSEC_PORT_ANY)) | |
693 | printf(" %u,%u", p1, p2); | |
694 | } else { | |
695 | struct protoent *ent; | |
696 | ||
697 | switch (ulp) { | |
698 | case IPPROTO_IPV4: | |
699 | printf("ip4"); | |
700 | break; | |
701 | default: | |
702 | ent = getprotobynumber((int)ulp); | |
703 | if (ent) | |
704 | printf("%s", ent->p_name); | |
705 | else | |
706 | printf("%u", ulp); | |
707 | ||
708 | endprotoent(); | |
709 | break; | |
710 | } | |
711 | } | |
712 | } | |
713 | ||
714 | /* | |
715 | * set "Mon Day Time Year" to buffer | |
716 | */ | |
717 | static char * | |
718 | str_time(t) | |
719 | time_t t; | |
720 | { | |
721 | static char buf[128]; | |
722 | ||
723 | if (t == 0) { | |
724 | int i = 0; | |
725 | for (;i < 20;) buf[i++] = ' '; | |
726 | } else { | |
727 | char *t0; | |
728 | t0 = ctime(&t); | |
729 | memcpy(buf, t0 + 4, 20); | |
730 | } | |
731 | ||
732 | buf[20] = '\0'; | |
733 | ||
734 | return(buf); | |
735 | } | |
736 | ||
737 | static void | |
738 | str_lifetime_byte(x, str) | |
739 | struct sadb_lifetime *x; | |
740 | char *str; | |
741 | { | |
742 | double y; | |
743 | char *unit; | |
744 | int w; | |
745 | ||
746 | if (x == NULL) { | |
747 | printf("\t%s: 0(bytes)", str); | |
748 | return; | |
749 | } | |
750 | ||
751 | #if 0 | |
752 | if ((x->sadb_lifetime_bytes) / 1024 / 1024) { | |
753 | y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024; | |
754 | unit = "M"; | |
755 | w = 1; | |
756 | } else if ((x->sadb_lifetime_bytes) / 1024) { | |
757 | y = (x->sadb_lifetime_bytes) * 1.0 / 1024; | |
758 | unit = "K"; | |
759 | w = 1; | |
760 | } else { | |
761 | y = (x->sadb_lifetime_bytes) * 1.0; | |
762 | unit = ""; | |
763 | w = 0; | |
764 | } | |
765 | #else | |
766 | y = (x->sadb_lifetime_bytes) * 1.0; | |
767 | unit = ""; | |
768 | w = 0; | |
769 | #endif | |
770 | printf("\t%s: %.*f(%sbytes)", str, w, y, unit); | |
771 | } |