forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintsadbmsg.c
297 lines (279 loc) · 7.83 KB
/
printsadbmsg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "unp.h"
#include <net/pfkeyv2.h>
const char *
get_sadb_msg_type(int type)
{
static char buf[100];
switch (type) {
case SADB_RESERVED: return "Reserved";
case SADB_GETSPI: return "Get SPI";
case SADB_UPDATE: return "Update";
case SADB_ADD: return "Add";
case SADB_DELETE: return "Delete";
case SADB_GET: return "Get";
case SADB_ACQUIRE: return "Acquire";
case SADB_REGISTER: return "Register";
case SADB_EXPIRE: return "Expire";
case SADB_FLUSH: return "Flush";
case SADB_DUMP: return "Dump";
default: sprintf(buf, "[Unknown type %d]", type);
return buf;
}
}
const char *
get_sadb_satype(int type)
{
static char buf[100];
switch (type) {
case SADB_SATYPE_UNSPEC: return "Unspecified";
case SADB_SATYPE_AH: return "IPsec AH";
case SADB_SATYPE_ESP: return "IPsec ESP";
case SADB_SATYPE_RSVP: return "RSVP";
case SADB_SATYPE_OSPFV2: return "OSPFv2";
case SADB_SATYPE_RIPV2: return "RIPv2";
case SADB_SATYPE_MIP: return "Mobile IP";
default: sprintf(buf, "[Unknown satype %d]", type);
return buf;
}
}
const char *
get_auth_alg(int alg)
{
static char buf[100];
switch (alg) {
case SADB_AALG_NONE: return "None";
case SADB_AALG_MD5HMAC: return "HMAC-MD5";
case SADB_AALG_SHA1HMAC: return "HMAC-SHA-1";
#ifdef SADB_X_AALG_MD5
case SADB_X_AALG_MD5: return "Keyed MD5";
#endif
#ifdef SADB_X_AALG_SHA
case SADB_X_AALG_SHA: return "Keyed SHA-1";
#endif
#ifdef SADB_X_AALG_NULL
case SADB_X_AALG_NULL: return "Null";
#endif
#ifdef SADB_X_AALG_SHA2_256
case SADB_X_AALG_SHA2_256: return "SHA2-256";
#endif
#ifdef SADB_X_AALG_SHA2_384
case SADB_X_AALG_SHA2_384: return "SHA2-384";
#endif
#ifdef SADB_X_AALG_SHA2_512
case SADB_X_AALG_SHA2_512: return "SHA2-512";
#endif
default: sprintf(buf, "[Unknown authentication algorithm %d]", alg);
return buf;
}
}
const char *
get_encrypt_alg(int alg)
{
static char buf[100];
switch (alg) {
case SADB_EALG_NONE: return "None";
case SADB_EALG_DESCBC: return "DES-CBC";
case SADB_EALG_3DESCBC: return "3DES-CBC";
case SADB_EALG_NULL: return "Null";
#ifdef SADB_X_EALG_CAST128CBC
case SADB_X_EALG_CAST128CBC: return "CAST128-CBC";
#endif
#ifdef SADB_X_EALG_BLOWFISHCBC
case SADB_X_EALG_BLOWFISHCBC: return "Blowfish-CBC";
#endif
#ifdef SADB_X_EALG_AES
case SADB_X_EALG_AES: return "AES";
#endif
default: sprintf(buf, "[Unknown encryption algorithm %d]", alg);
return buf;
}
}
const char *
get_sa_state(int state)
{
static char buf[100];
switch (state) {
case SADB_SASTATE_LARVAL: return "Larval";
case SADB_SASTATE_MATURE: return "Mature";
case SADB_SASTATE_DYING: return "Dying";
case SADB_SASTATE_DEAD: return "Dead";
default: sprintf(buf, "[Unknown SA state %d]", state);
return buf;
}
}
const char *
get_sadb_alg_type(int alg, int authenc)
{
if (authenc == SADB_EXT_SUPPORTED_AUTH) {
return get_auth_alg(alg);
} else {
return get_encrypt_alg(alg);
}
}
void
sa_print(struct sadb_ext *ext)
{
struct sadb_sa *sa = (struct sadb_sa *)ext;
printf(" SA: SPI=%d Replay Window=%d State=%s\n",
sa->sadb_sa_spi, sa->sadb_sa_replay,
get_sa_state(sa->sadb_sa_state));
printf(" Authentication Algorithm: %s\n",
get_auth_alg(sa->sadb_sa_auth));
printf(" Encryption Algorithm: %s\n",
get_encrypt_alg(sa->sadb_sa_encrypt));
if (sa->sadb_sa_flags & SADB_SAFLAGS_PFS)
printf(" Perfect Forward Secrecy\n");
}
void
supported_print(struct sadb_ext *ext)
{
struct sadb_supported *sup = (struct sadb_supported *)ext;
struct sadb_alg *alg;
int len;
printf(" Supported %s algorithms:\n",
sup->sadb_supported_exttype == SADB_EXT_SUPPORTED_AUTH ?
"authentication" :
"encryption");
len = sup->sadb_supported_len * 8;
len -= sizeof(*sup);
if (len == 0) {
printf(" None\n");
return;
}
for (alg = (struct sadb_alg *)(sup + 1); len>0; len -= sizeof(*alg), alg++) {
printf(" %s ivlen %d bits %d-%d\n",
get_sadb_alg_type(alg->sadb_alg_id, sup->sadb_supported_exttype),
alg->sadb_alg_ivlen, alg->sadb_alg_minbits, alg->sadb_alg_maxbits);
}
}
void
lifetime_print(struct sadb_ext *ext)
{
struct sadb_lifetime *life = (struct sadb_lifetime *)ext;
printf(" %s lifetime:\n",
life->sadb_lifetime_exttype == SADB_EXT_LIFETIME_CURRENT ?
"Current" :
life->sadb_lifetime_exttype == SADB_EXT_LIFETIME_HARD ?
"Hard" :
"Soft");
printf(" %d allocations, %d bytes", life->sadb_lifetime_allocations,
life->sadb_lifetime_bytes);
if (life->sadb_lifetime_exttype == SADB_EXT_LIFETIME_CURRENT) {
time_t t;
struct tmp *tm;
char buf[100];
/* absolute times */
t = life->sadb_lifetime_addtime;
tm = localtime(&t);
strftime(buf, sizeof(buf), "%c", tm);
printf("\n added at %s, ", buf);
if (life->sadb_lifetime_usetime == 0) {
printf("never used\n");
} else {
t = life->sadb_lifetime_usetime;
tm = localtime(&t);
strftime(buf, sizeof(buf), "%c", tm);
printf("first used at %s\n", buf);
}
} else {
printf("%d addtime, %d usetime\n", life->sadb_lifetime_addtime,
life->sadb_lifetime_usetime);
}
}
void
address_print(struct sadb_ext *ext)
{
struct sadb_address *addr = (struct sadb_address *)ext;
struct sockaddr *sa;
printf(" %s address: ",
addr->sadb_address_exttype == SADB_EXT_ADDRESS_SRC ?
"Source" :
addr->sadb_address_exttype == SADB_EXT_ADDRESS_DST ?
"Dest" :
"Proxy");
sa = (struct sockaddr *)(addr + 1);
printf(" %s", sock_ntop(sa, addr->sadb_address_len * 8 - sizeof(*addr)));
if (addr->sadb_address_prefixlen == 0)
printf(" ");
else
printf("/%d ", addr->sadb_address_prefixlen);
switch (addr->sadb_address_proto) {
case IPPROTO_UDP: printf("(UDP)"); break;
case IPPROTO_TCP: printf("(TCP)"); break;
case 0: break;
default: printf("(IP proto %d)", addr->sadb_address_proto);
break;
}
printf("\n");
}
void
key_print(struct sadb_ext *ext)
{
struct sadb_key *key = (struct sadb_key *)ext;
int bits;
unsigned char *p;
printf(" %s key, %d bits: 0x",
key->sadb_key_exttype == SADB_EXT_KEY_AUTH ?
"Authentication" : "Encryption",
key->sadb_key_bits);
for (p = (unsigned char *)(key + 1), bits = key->sadb_key_bits;
bits > 0; p++, bits -= 8)
printf("%02x", *p);
printf("\n");
}
void
print_sadb_msg(struct sadb_msg *msg, int msglen)
{
struct sadb_ext *ext;
if (msglen != msg->sadb_msg_len * 8) {
err_msg("SADB Message length (%d) doesn't match msglen (%d)\n",
msg->sadb_msg_len * 8, msglen);
return;
}
if (msg->sadb_msg_version != PF_KEY_V2) {
err_msg("SADB Message version not PF_KEY_V2\n");
return;
}
printf("SADB Message %s, errno %d, satype %s, seq %d, pid %d\n",
get_sadb_msg_type(msg->sadb_msg_type), msg->sadb_msg_errno,
get_sadb_satype(msg->sadb_msg_satype), msg->sadb_msg_seq,
msg->sadb_msg_pid);
if (msg->sadb_msg_errno != 0)
printf(" errno %s\n", strerror(msg->sadb_msg_errno));
if (msglen == sizeof(struct sadb_msg))
return; /* no extensions */
msglen -= sizeof(struct sadb_msg);
ext = (struct sadb_ext *)(msg + 1);
while (msglen > 0) {
switch (ext->sadb_ext_type) {
case SADB_EXT_RESERVED: printf(" Reserved Extension\n"); break;
case SADB_EXT_SA: sa_print(ext); break;
case SADB_EXT_LIFETIME_CURRENT:
case SADB_EXT_LIFETIME_HARD:
case SADB_EXT_LIFETIME_SOFT:
lifetime_print(ext); break;
case SADB_EXT_ADDRESS_SRC:
case SADB_EXT_ADDRESS_DST:
case SADB_EXT_ADDRESS_PROXY:
address_print(ext); break;
case SADB_EXT_KEY_AUTH:
case SADB_EXT_KEY_ENCRYPT:
key_print(ext); break;
case SADB_EXT_IDENTITY_SRC:
case SADB_EXT_IDENTITY_DST:
printf(" [identity...]\n"); break;
case SADB_EXT_SENSITIVITY:
printf(" [sensitivity...]\n"); break;
case SADB_EXT_PROPOSAL:
printf(" [proposal...]\n"); break;
case SADB_EXT_SUPPORTED_AUTH:
case SADB_EXT_SUPPORTED_ENCRYPT:
supported_print(ext); break;
case SADB_EXT_SPIRANGE:
printf(" [spirange...]\n"); break;
default: printf(" [unknown extension %d]\n", ext->sadb_ext_type);
}
msglen -= ext->sadb_ext_len << 3;
ext = (char *)ext + (ext->sadb_ext_len << 3);
}
}