]>
Commit | Line | Data |
---|---|---|
f9cb3a2f | 1 | /* $USAGI: $ */ |
2 | ||
3 | /* | |
4 | * Copyright (C)2005 USAGI/WIDE Project | |
ae665a52 | 5 | * |
f9cb3a2f | 6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
ae665a52 | 10 | * |
f9cb3a2f | 11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
ae665a52 | 15 | * |
f9cb3a2f | 16 | * You should have received a copy of the GNU General Public License |
4d98ab00 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses>. |
f9cb3a2f | 18 | */ |
19 | /* | |
20 | * based on ipmonitor.c | |
21 | */ | |
22 | /* | |
23 | * Authors: | |
24 | * Masahide NAKAMURA @USAGI | |
25 | */ | |
26 | ||
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
93390779 | 30 | #include <netinet/in.h> |
93390779 | 31 | |
f9cb3a2f | 32 | #include "utils.h" |
33 | #include "xfrm.h" | |
34 | #include "ip_common.h" | |
35 | ||
36 | static void usage(void) __attribute__((noreturn)); | |
fa92d8cb | 37 | static int listen_all_nsid; |
a6af9f2e | 38 | static bool nokeys; |
f9cb3a2f | 39 | |
40 | static void usage(void) | |
41 | { | |
a6af9f2e | 42 | fprintf(stderr, "Usage: ip xfrm monitor [ nokeys ] [ all-nsid ] [ all | OBJECTS | help ]\n"); |
c16298be | 43 | fprintf(stderr, "OBJECTS := { acquire | expire | SA | aevent | policy | report }\n"); |
f9cb3a2f | 44 | exit(-1); |
45 | } | |
46 | ||
cd554f2c | 47 | static int xfrm_acquire_print(struct nlmsghdr *n, void *arg) |
f9cb3a2f | 48 | { |
56f5daac | 49 | FILE *fp = (FILE *)arg; |
f9cb3a2f | 50 | struct xfrm_user_acquire *xacq = NLMSG_DATA(n); |
51 | int len = n->nlmsg_len; | |
56f5daac | 52 | struct rtattr *tb[XFRMA_MAX+1]; |
f9cb3a2f | 53 | __u16 family; |
54 | ||
f9cb3a2f | 55 | len -= NLMSG_LENGTH(sizeof(*xacq)); |
56 | if (len < 0) { | |
57 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
58 | return -1; | |
59 | } | |
60 | ||
61 | parse_rtattr(tb, XFRMA_MAX, XFRMACQ_RTA(xacq), len); | |
90f93024 | 62 | |
f9cb3a2f | 63 | family = xacq->sel.family; |
64 | if (family == AF_UNSPEC) | |
65 | family = xacq->policy.sel.family; | |
66 | if (family == AF_UNSPEC) | |
67 | family = preferred_family; | |
68 | ||
69 | fprintf(fp, "acquire "); | |
70 | ||
71 | fprintf(fp, "proto %s ", strxf_xfrmproto(xacq->id.proto)); | |
72 | if (show_stats > 0 || xacq->id.spi) { | |
73 | __u32 spi = ntohl(xacq->id.spi); | |
56f5daac | 74 | |
f9cb3a2f | 75 | fprintf(fp, "spi 0x%08x", spi); |
76 | if (show_stats > 0) | |
77 | fprintf(fp, "(%u)", spi); | |
78 | fprintf(fp, " "); | |
79 | } | |
80 | fprintf(fp, "%s", _SL_); | |
81 | ||
82 | xfrm_selector_print(&xacq->sel, family, fp, " sel "); | |
83 | ||
84 | xfrm_policy_info_print(&xacq->policy, tb, fp, " ", " policy "); | |
85 | ||
86 | if (show_stats > 0) | |
87 | fprintf(fp, " seq 0x%08u ", xacq->seq); | |
88 | if (show_stats > 0) { | |
89 | fprintf(fp, "%s-mask %s ", | |
90 | strxf_algotype(XFRMA_ALG_CRYPT), | |
91 | strxf_mask32(xacq->ealgos)); | |
92 | fprintf(fp, "%s-mask %s ", | |
93 | strxf_algotype(XFRMA_ALG_AUTH), | |
94 | strxf_mask32(xacq->aalgos)); | |
95 | fprintf(fp, "%s-mask %s", | |
96 | strxf_algotype(XFRMA_ALG_COMP), | |
97 | strxf_mask32(xacq->calgos)); | |
98 | } | |
99 | fprintf(fp, "%s", _SL_); | |
100 | ||
101 | if (oneline) | |
102 | fprintf(fp, "\n"); | |
669ae748 | 103 | fflush(fp); |
f9cb3a2f | 104 | |
105 | return 0; | |
106 | } | |
107 | ||
cd554f2c | 108 | static int xfrm_state_flush_print(struct nlmsghdr *n, void *arg) |
efe69c1b | 109 | { |
56f5daac | 110 | FILE *fp = (FILE *)arg; |
efe69c1b MN |
111 | struct xfrm_usersa_flush *xsf = NLMSG_DATA(n); |
112 | int len = n->nlmsg_len; | |
113 | const char *str; | |
114 | ||
115 | len -= NLMSG_SPACE(sizeof(*xsf)); | |
116 | if (len < 0) { | |
117 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
118 | return -1; | |
119 | } | |
120 | ||
121 | fprintf(fp, "Flushed state "); | |
122 | ||
123 | str = strxf_xfrmproto(xsf->proto); | |
124 | if (str) | |
125 | fprintf(fp, "proto %s", str); | |
126 | else | |
127 | fprintf(fp, "proto %u", xsf->proto); | |
128 | fprintf(fp, "%s", _SL_); | |
129 | ||
130 | if (oneline) | |
131 | fprintf(fp, "\n"); | |
132 | fflush(fp); | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
cd554f2c | 137 | static int xfrm_policy_flush_print(struct nlmsghdr *n, void *arg) |
efe69c1b | 138 | { |
56f5daac SH |
139 | struct rtattr *tb[XFRMA_MAX+1]; |
140 | FILE *fp = (FILE *)arg; | |
efe69c1b MN |
141 | int len = n->nlmsg_len; |
142 | ||
143 | len -= NLMSG_SPACE(0); | |
144 | if (len < 0) { | |
145 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
146 | return -1; | |
147 | } | |
148 | ||
149 | fprintf(fp, "Flushed policy "); | |
150 | ||
151 | parse_rtattr(tb, XFRMA_MAX, NLMSG_DATA(n), len); | |
152 | ||
153 | if (tb[XFRMA_POLICY_TYPE]) { | |
154 | struct xfrm_userpolicy_type *upt; | |
155 | ||
156 | fprintf(fp, "ptype "); | |
157 | ||
158 | if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) | |
159 | fprintf(fp, "(ERROR truncated)"); | |
160 | ||
9f1370c0 | 161 | upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]); |
efe69c1b MN |
162 | fprintf(fp, "%s ", strxf_ptype(upt->type)); |
163 | } | |
164 | ||
165 | fprintf(fp, "%s", _SL_); | |
166 | ||
167 | if (oneline) | |
168 | fprintf(fp, "\n"); | |
169 | fflush(fp); | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
cd554f2c | 174 | static int xfrm_report_print(struct nlmsghdr *n, void *arg) |
c54f31ee | 175 | { |
56f5daac | 176 | FILE *fp = (FILE *)arg; |
c54f31ee MN |
177 | struct xfrm_user_report *xrep = NLMSG_DATA(n); |
178 | int len = n->nlmsg_len; | |
56f5daac | 179 | struct rtattr *tb[XFRMA_MAX+1]; |
c54f31ee MN |
180 | __u16 family; |
181 | ||
c54f31ee MN |
182 | len -= NLMSG_LENGTH(sizeof(*xrep)); |
183 | if (len < 0) { | |
184 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
185 | return -1; | |
186 | } | |
187 | ||
188 | family = xrep->sel.family; | |
189 | if (family == AF_UNSPEC) | |
190 | family = preferred_family; | |
191 | ||
192 | fprintf(fp, "report "); | |
193 | ||
194 | fprintf(fp, "proto %s ", strxf_xfrmproto(xrep->proto)); | |
195 | fprintf(fp, "%s", _SL_); | |
196 | ||
197 | xfrm_selector_print(&xrep->sel, family, fp, " sel "); | |
198 | ||
199 | parse_rtattr(tb, XFRMA_MAX, XFRMREP_RTA(xrep), len); | |
200 | ||
a6af9f2e | 201 | xfrm_xfrma_print(tb, family, fp, " ", nokeys); |
c54f31ee MN |
202 | |
203 | if (oneline) | |
204 | fprintf(fp, "\n"); | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
d1f28cf1 | 209 | static void xfrm_ae_flags_print(__u32 flags, void *arg) |
c9fd974b | 210 | { |
56f5daac SH |
211 | FILE *fp = (FILE *)arg; |
212 | ||
c9fd974b | 213 | fprintf(fp, " (0x%x) ", flags); |
214 | if (!flags) | |
215 | return; | |
216 | if (flags & XFRM_AE_CR) | |
217 | fprintf(fp, " replay update "); | |
218 | if (flags & XFRM_AE_CE) | |
219 | fprintf(fp, " timer expired "); | |
220 | if (flags & XFRM_AE_CU) | |
221 | fprintf(fp, " policy updated "); | |
222 | ||
223 | } | |
224 | ||
598a42c0 FW |
225 | static void xfrm_usersa_print(const struct xfrm_usersa_id *sa_id, __u32 reqid, FILE *fp) |
226 | { | |
656111b2 | 227 | fprintf(fp, "dst %s ", |
2e96d2cc | 228 | rt_addr_n2a(sa_id->family, sizeof(sa_id->daddr), &sa_id->daddr)); |
598a42c0 FW |
229 | |
230 | fprintf(fp, " reqid 0x%x", reqid); | |
231 | ||
232 | fprintf(fp, " protocol %s ", strxf_proto(sa_id->proto)); | |
233 | fprintf(fp, " SPI 0x%x", ntohl(sa_id->spi)); | |
234 | } | |
235 | ||
cd554f2c | 236 | static int xfrm_ae_print(struct nlmsghdr *n, void *arg) |
c9fd974b | 237 | { |
56f5daac | 238 | FILE *fp = (FILE *)arg; |
c9fd974b | 239 | struct xfrm_aevent_id *id = NLMSG_DATA(n); |
c9fd974b | 240 | |
241 | fprintf(fp, "Async event "); | |
242 | xfrm_ae_flags_print(id->flags, arg); | |
56f5daac | 243 | fprintf(fp, "\n\t"); |
26dcdf3a | 244 | fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family, |
2e96d2cc | 245 | sizeof(id->saddr), &id->saddr)); |
598a42c0 FW |
246 | |
247 | xfrm_usersa_print(&id->sa_id, id->reqid, fp); | |
c9fd974b | 248 | |
249 | fprintf(fp, "\n"); | |
250 | fflush(fp); | |
251 | ||
252 | return 0; | |
253 | } | |
254 | ||
656111b2 | 255 | static void xfrm_print_addr(FILE *fp, int family, xfrm_address_t *a) |
598a42c0 | 256 | { |
2e96d2cc | 257 | fprintf(fp, "%s", rt_addr_n2a(family, sizeof(*a), a)); |
598a42c0 FW |
258 | } |
259 | ||
cd554f2c | 260 | static int xfrm_mapping_print(struct nlmsghdr *n, void *arg) |
598a42c0 | 261 | { |
56f5daac | 262 | FILE *fp = (FILE *)arg; |
598a42c0 FW |
263 | struct xfrm_user_mapping *map = NLMSG_DATA(n); |
264 | ||
265 | fprintf(fp, "Mapping change "); | |
656111b2 | 266 | xfrm_print_addr(fp, map->id.family, &map->old_saddr); |
598a42c0 FW |
267 | |
268 | fprintf(fp, ":%d -> ", ntohs(map->old_sport)); | |
656111b2 | 269 | xfrm_print_addr(fp, map->id.family, &map->new_saddr); |
598a42c0 FW |
270 | fprintf(fp, ":%d\n\t", ntohs(map->new_sport)); |
271 | ||
272 | xfrm_usersa_print(&map->id, map->reqid, fp); | |
273 | ||
274 | fprintf(fp, "\n"); | |
275 | fflush(fp); | |
276 | return 0; | |
277 | } | |
278 | ||
cd554f2c | 279 | static int xfrm_accept_msg(struct rtnl_ctrl_data *ctrl, |
f9cb3a2f | 280 | struct nlmsghdr *n, void *arg) |
281 | { | |
56f5daac | 282 | FILE *fp = (FILE *)arg; |
f9cb3a2f | 283 | |
90f93024 SH |
284 | if (timestamp) |
285 | print_timestamp(fp); | |
286 | ||
b6ec53e3 ND |
287 | if (listen_all_nsid) { |
288 | if (ctrl == NULL || ctrl->nsid < 0) | |
289 | fprintf(fp, "[nsid current]"); | |
290 | else | |
291 | fprintf(fp, "[nsid %d]", ctrl->nsid); | |
292 | } | |
293 | ||
efe69c1b MN |
294 | switch (n->nlmsg_type) { |
295 | case XFRM_MSG_NEWSA: | |
296 | case XFRM_MSG_DELSA: | |
297 | case XFRM_MSG_UPDSA: | |
298 | case XFRM_MSG_EXPIRE: | |
cd554f2c | 299 | xfrm_state_print(n, arg); |
c595c790 | 300 | return 0; |
efe69c1b MN |
301 | case XFRM_MSG_NEWPOLICY: |
302 | case XFRM_MSG_DELPOLICY: | |
303 | case XFRM_MSG_UPDPOLICY: | |
304 | case XFRM_MSG_POLEXPIRE: | |
cd554f2c | 305 | xfrm_policy_print(n, arg); |
c595c790 | 306 | return 0; |
efe69c1b | 307 | case XFRM_MSG_ACQUIRE: |
cd554f2c | 308 | xfrm_acquire_print(n, arg); |
f9cb3a2f | 309 | return 0; |
efe69c1b | 310 | case XFRM_MSG_FLUSHSA: |
cd554f2c | 311 | xfrm_state_flush_print(n, arg); |
f9cb3a2f | 312 | return 0; |
efe69c1b | 313 | case XFRM_MSG_FLUSHPOLICY: |
cd554f2c | 314 | xfrm_policy_flush_print(n, arg); |
f9cb3a2f | 315 | return 0; |
efe69c1b | 316 | case XFRM_MSG_REPORT: |
cd554f2c | 317 | xfrm_report_print(n, arg); |
c54f31ee | 318 | return 0; |
efe69c1b | 319 | case XFRM_MSG_NEWAE: |
cd554f2c | 320 | xfrm_ae_print(n, arg); |
c9fd974b | 321 | return 0; |
598a42c0 | 322 | case XFRM_MSG_MAPPING: |
cd554f2c | 323 | xfrm_mapping_print(n, arg); |
598a42c0 | 324 | return 0; |
efe69c1b MN |
325 | default: |
326 | break; | |
c9fd974b | 327 | } |
efe69c1b | 328 | |
f9cb3a2f | 329 | if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP && |
330 | n->nlmsg_type != NLMSG_DONE) { | |
c595c790 | 331 | fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n", |
f9cb3a2f | 332 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
333 | } | |
334 | return 0; | |
335 | } | |
336 | ||
77219712 SH |
337 | extern struct rtnl_handle rth; |
338 | ||
f9cb3a2f | 339 | int do_xfrm_monitor(int argc, char **argv) |
340 | { | |
f9cb3a2f | 341 | char *file = NULL; |
56f5daac SH |
342 | unsigned int groups = ~((unsigned)0); /* XXX */ |
343 | int lacquire = 0; | |
344 | int lexpire = 0; | |
345 | int laevent = 0; | |
346 | int lpolicy = 0; | |
347 | int lsa = 0; | |
348 | int lreport = 0; | |
f9cb3a2f | 349 | |
77219712 SH |
350 | rtnl_close(&rth); |
351 | ||
f9cb3a2f | 352 | while (argc > 0) { |
353 | if (matches(*argv, "file") == 0) { | |
354 | NEXT_ARG(); | |
355 | file = *argv; | |
a6af9f2e BW |
356 | } else if (strcmp(*argv, "nokeys") == 0) { |
357 | nokeys = true; | |
b8e77990 NH |
358 | } else if (strcmp(*argv, "all") == 0) { |
359 | /* fall out */ | |
b6ec53e3 ND |
360 | } else if (matches(*argv, "all-nsid") == 0) { |
361 | listen_all_nsid = 1; | |
f9cb3a2f | 362 | } else if (matches(*argv, "acquire") == 0) { |
56f5daac | 363 | lacquire = 1; |
f9cb3a2f | 364 | groups = 0; |
365 | } else if (matches(*argv, "expire") == 0) { | |
56f5daac | 366 | lexpire = 1; |
f9cb3a2f | 367 | groups = 0; |
c595c790 | 368 | } else if (matches(*argv, "SA") == 0) { |
56f5daac | 369 | lsa = 1; |
c595c790 | 370 | groups = 0; |
c9fd974b | 371 | } else if (matches(*argv, "aevent") == 0) { |
56f5daac | 372 | laevent = 1; |
c9fd974b | 373 | groups = 0; |
c595c790 | 374 | } else if (matches(*argv, "policy") == 0) { |
56f5daac | 375 | lpolicy = 1; |
c595c790 | 376 | groups = 0; |
c54f31ee | 377 | } else if (matches(*argv, "report") == 0) { |
56f5daac | 378 | lreport = 1; |
c54f31ee | 379 | groups = 0; |
f9cb3a2f | 380 | } else if (matches(*argv, "help") == 0) { |
381 | usage(); | |
b8e77990 | 382 | } else { |
f9cb3a2f | 383 | fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv); |
384 | exit(-1); | |
385 | } | |
386 | argc--; argv++; | |
387 | } | |
388 | ||
389 | if (lacquire) | |
7b822512 | 390 | groups |= nl_mgrp(XFRMNLGRP_ACQUIRE); |
f9cb3a2f | 391 | if (lexpire) |
7b822512 | 392 | groups |= nl_mgrp(XFRMNLGRP_EXPIRE); |
c595c790 | 393 | if (lsa) |
7b822512 | 394 | groups |= nl_mgrp(XFRMNLGRP_SA); |
c595c790 | 395 | if (lpolicy) |
7b822512 | 396 | groups |= nl_mgrp(XFRMNLGRP_POLICY); |
c9fd974b | 397 | if (laevent) |
7b822512 | 398 | groups |= nl_mgrp(XFRMNLGRP_AEVENTS); |
c54f31ee | 399 | if (lreport) |
7b822512 | 400 | groups |= nl_mgrp(XFRMNLGRP_REPORT); |
f9cb3a2f | 401 | |
402 | if (file) { | |
403 | FILE *fp; | |
e49b51d6 SH |
404 | int err; |
405 | ||
f9cb3a2f | 406 | fp = fopen(file, "r"); |
407 | if (fp == NULL) { | |
408 | perror("Cannot fopen"); | |
409 | exit(-1); | |
410 | } | |
e49b51d6 SH |
411 | err = rtnl_from_file(fp, xfrm_accept_msg, stdout); |
412 | fclose(fp); | |
413 | return err; | |
f9cb3a2f | 414 | } |
415 | ||
c595c790 SH |
416 | if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0) |
417 | exit(1); | |
b6ec53e3 ND |
418 | if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0) |
419 | exit(1); | |
c595c790 | 420 | |
56f5daac | 421 | if (rtnl_listen(&rth, xfrm_accept_msg, (void *)stdout) < 0) |
f9cb3a2f | 422 | exit(2); |
423 | ||
351efcde | 424 | return 0; |
f9cb3a2f | 425 | } |