]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/xfrm_monitor.c
4 * Copyright (C)2005 USAGI/WIDE Project
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses>.
20 * based on ipmonitor.c
24 * Masahide NAKAMURA @USAGI
30 #include <netinet/in.h>
34 #include "ip_common.h"
36 static void usage(void) __attribute__((noreturn
));
37 static int listen_all_nsid
;
40 static void usage(void)
42 fprintf(stderr
, "Usage: ip xfrm monitor [ nokeys ] [ all-nsid ] [ all | OBJECTS | help ]\n");
43 fprintf(stderr
, "OBJECTS := { acquire | expire | SA | aevent | policy | report }\n");
47 static int xfrm_acquire_print(struct nlmsghdr
*n
, void *arg
)
49 FILE *fp
= (FILE *)arg
;
50 struct xfrm_user_acquire
*xacq
= NLMSG_DATA(n
);
51 int len
= n
->nlmsg_len
;
52 struct rtattr
*tb
[XFRMA_MAX
+1];
55 len
-= NLMSG_LENGTH(sizeof(*xacq
));
57 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
61 parse_rtattr(tb
, XFRMA_MAX
, XFRMACQ_RTA(xacq
), len
);
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
;
69 fprintf(fp
, "acquire ");
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
);
75 fprintf(fp
, "spi 0x%08x", spi
);
77 fprintf(fp
, "(%u)", spi
);
80 fprintf(fp
, "%s", _SL_
);
82 xfrm_selector_print(&xacq
->sel
, family
, fp
, " sel ");
84 xfrm_policy_info_print(&xacq
->policy
, tb
, fp
, " ", " policy ");
87 fprintf(fp
, " seq 0x%08u ", xacq
->seq
);
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
));
99 fprintf(fp
, "%s", _SL_
);
108 static int xfrm_state_flush_print(struct nlmsghdr
*n
, void *arg
)
110 FILE *fp
= (FILE *)arg
;
111 struct xfrm_usersa_flush
*xsf
= NLMSG_DATA(n
);
112 int len
= n
->nlmsg_len
;
115 len
-= NLMSG_SPACE(sizeof(*xsf
));
117 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
121 fprintf(fp
, "Flushed state ");
123 str
= strxf_xfrmproto(xsf
->proto
);
125 fprintf(fp
, "proto %s", str
);
127 fprintf(fp
, "proto %u", xsf
->proto
);
128 fprintf(fp
, "%s", _SL_
);
137 static int xfrm_policy_flush_print(struct nlmsghdr
*n
, void *arg
)
139 struct rtattr
*tb
[XFRMA_MAX
+1];
140 FILE *fp
= (FILE *)arg
;
141 int len
= n
->nlmsg_len
;
143 len
-= NLMSG_SPACE(0);
145 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
149 fprintf(fp
, "Flushed policy ");
151 parse_rtattr(tb
, XFRMA_MAX
, NLMSG_DATA(n
), len
);
153 if (tb
[XFRMA_POLICY_TYPE
]) {
154 struct xfrm_userpolicy_type
*upt
;
156 fprintf(fp
, "ptype ");
158 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
159 fprintf(fp
, "(ERROR truncated)");
161 upt
= RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
162 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
165 fprintf(fp
, "%s", _SL_
);
174 static int xfrm_report_print(struct nlmsghdr
*n
, void *arg
)
176 FILE *fp
= (FILE *)arg
;
177 struct xfrm_user_report
*xrep
= NLMSG_DATA(n
);
178 int len
= n
->nlmsg_len
;
179 struct rtattr
*tb
[XFRMA_MAX
+1];
182 len
-= NLMSG_LENGTH(sizeof(*xrep
));
184 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
188 family
= xrep
->sel
.family
;
189 if (family
== AF_UNSPEC
)
190 family
= preferred_family
;
192 fprintf(fp
, "report ");
194 fprintf(fp
, "proto %s ", strxf_xfrmproto(xrep
->proto
));
195 fprintf(fp
, "%s", _SL_
);
197 xfrm_selector_print(&xrep
->sel
, family
, fp
, " sel ");
199 parse_rtattr(tb
, XFRMA_MAX
, XFRMREP_RTA(xrep
), len
);
201 xfrm_xfrma_print(tb
, family
, fp
, " ", nokeys
);
209 static void xfrm_ae_flags_print(__u32 flags
, void *arg
)
211 FILE *fp
= (FILE *)arg
;
213 fprintf(fp
, " (0x%x) ", flags
);
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 ");
225 static void xfrm_usersa_print(const struct xfrm_usersa_id
*sa_id
, __u32 reqid
, FILE *fp
)
227 fprintf(fp
, "dst %s ",
228 rt_addr_n2a(sa_id
->family
, sizeof(sa_id
->daddr
), &sa_id
->daddr
));
230 fprintf(fp
, " reqid 0x%x", reqid
);
232 fprintf(fp
, " protocol %s ", strxf_proto(sa_id
->proto
));
233 fprintf(fp
, " SPI 0x%x", ntohl(sa_id
->spi
));
236 static int xfrm_ae_print(struct nlmsghdr
*n
, void *arg
)
238 FILE *fp
= (FILE *)arg
;
239 struct xfrm_aevent_id
*id
= NLMSG_DATA(n
);
241 fprintf(fp
, "Async event ");
242 xfrm_ae_flags_print(id
->flags
, arg
);
244 fprintf(fp
, "src %s ", rt_addr_n2a(id
->sa_id
.family
,
245 sizeof(id
->saddr
), &id
->saddr
));
247 xfrm_usersa_print(&id
->sa_id
, id
->reqid
, fp
);
255 static void xfrm_print_addr(FILE *fp
, int family
, xfrm_address_t
*a
)
257 fprintf(fp
, "%s", rt_addr_n2a(family
, sizeof(*a
), a
));
260 static int xfrm_mapping_print(struct nlmsghdr
*n
, void *arg
)
262 FILE *fp
= (FILE *)arg
;
263 struct xfrm_user_mapping
*map
= NLMSG_DATA(n
);
265 fprintf(fp
, "Mapping change ");
266 xfrm_print_addr(fp
, map
->id
.family
, &map
->old_saddr
);
268 fprintf(fp
, ":%d -> ", ntohs(map
->old_sport
));
269 xfrm_print_addr(fp
, map
->id
.family
, &map
->new_saddr
);
270 fprintf(fp
, ":%d\n\t", ntohs(map
->new_sport
));
272 xfrm_usersa_print(&map
->id
, map
->reqid
, fp
);
279 static int xfrm_accept_msg(struct rtnl_ctrl_data
*ctrl
,
280 struct nlmsghdr
*n
, void *arg
)
282 FILE *fp
= (FILE *)arg
;
287 if (listen_all_nsid
) {
288 if (ctrl
== NULL
|| ctrl
->nsid
< 0)
289 fprintf(fp
, "[nsid current]");
291 fprintf(fp
, "[nsid %d]", ctrl
->nsid
);
294 switch (n
->nlmsg_type
) {
298 case XFRM_MSG_EXPIRE
:
299 xfrm_state_print(n
, arg
);
301 case XFRM_MSG_NEWPOLICY
:
302 case XFRM_MSG_DELPOLICY
:
303 case XFRM_MSG_UPDPOLICY
:
304 case XFRM_MSG_POLEXPIRE
:
305 xfrm_policy_print(n
, arg
);
307 case XFRM_MSG_ACQUIRE
:
308 xfrm_acquire_print(n
, arg
);
310 case XFRM_MSG_FLUSHSA
:
311 xfrm_state_flush_print(n
, arg
);
313 case XFRM_MSG_FLUSHPOLICY
:
314 xfrm_policy_flush_print(n
, arg
);
316 case XFRM_MSG_REPORT
:
317 xfrm_report_print(n
, arg
);
320 xfrm_ae_print(n
, arg
);
322 case XFRM_MSG_MAPPING
:
323 xfrm_mapping_print(n
, arg
);
329 if (n
->nlmsg_type
!= NLMSG_ERROR
&& n
->nlmsg_type
!= NLMSG_NOOP
&&
330 n
->nlmsg_type
!= NLMSG_DONE
) {
331 fprintf(fp
, "Unknown message: %08d 0x%08x 0x%08x\n",
332 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
337 extern struct rtnl_handle rth
;
339 int do_xfrm_monitor(int argc
, char **argv
)
342 unsigned int groups
= ~((unsigned)0); /* XXX */
353 if (matches(*argv
, "file") == 0) {
356 } else if (strcmp(*argv
, "nokeys") == 0) {
358 } else if (strcmp(*argv
, "all") == 0) {
360 } else if (matches(*argv
, "all-nsid") == 0) {
362 } else if (matches(*argv
, "acquire") == 0) {
365 } else if (matches(*argv
, "expire") == 0) {
368 } else if (matches(*argv
, "SA") == 0) {
371 } else if (matches(*argv
, "aevent") == 0) {
374 } else if (matches(*argv
, "policy") == 0) {
377 } else if (matches(*argv
, "report") == 0) {
380 } else if (matches(*argv
, "help") == 0) {
383 fprintf(stderr
, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv
);
390 groups
|= nl_mgrp(XFRMNLGRP_ACQUIRE
);
392 groups
|= nl_mgrp(XFRMNLGRP_EXPIRE
);
394 groups
|= nl_mgrp(XFRMNLGRP_SA
);
396 groups
|= nl_mgrp(XFRMNLGRP_POLICY
);
398 groups
|= nl_mgrp(XFRMNLGRP_AEVENTS
);
400 groups
|= nl_mgrp(XFRMNLGRP_REPORT
);
406 fp
= fopen(file
, "r");
408 perror("Cannot fopen");
411 err
= rtnl_from_file(fp
, xfrm_accept_msg
, stdout
);
416 if (rtnl_open_byproto(&rth
, groups
, NETLINK_XFRM
) < 0)
418 if (listen_all_nsid
&& rtnl_listen_all_nsid(&rth
) < 0)
421 if (rtnl_listen(&rth
, xfrm_accept_msg
, (void *)stdout
) < 0)