]>
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
));
39 static void usage(void)
41 fprintf(stderr
, "Usage: ip xfrm monitor [all-nsid] [ all | OBJECTS | help ]\n");
42 fprintf(stderr
, "OBJECTS := { acquire | expire | SA | aevent | policy | report }\n");
46 static int xfrm_acquire_print(const struct sockaddr_nl
*who
,
47 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(const struct sockaddr_nl
*who
,
109 struct nlmsghdr
*n
, void *arg
)
111 FILE *fp
= (FILE *)arg
;
112 struct xfrm_usersa_flush
*xsf
= NLMSG_DATA(n
);
113 int len
= n
->nlmsg_len
;
116 len
-= NLMSG_SPACE(sizeof(*xsf
));
118 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
122 fprintf(fp
, "Flushed state ");
124 str
= strxf_xfrmproto(xsf
->proto
);
126 fprintf(fp
, "proto %s", str
);
128 fprintf(fp
, "proto %u", xsf
->proto
);
129 fprintf(fp
, "%s", _SL_
);
138 static int xfrm_policy_flush_print(const struct sockaddr_nl
*who
,
139 struct nlmsghdr
*n
, void *arg
)
141 struct rtattr
*tb
[XFRMA_MAX
+1];
142 FILE *fp
= (FILE *)arg
;
143 int len
= n
->nlmsg_len
;
145 len
-= NLMSG_SPACE(0);
147 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
151 fprintf(fp
, "Flushed policy ");
153 parse_rtattr(tb
, XFRMA_MAX
, NLMSG_DATA(n
), len
);
155 if (tb
[XFRMA_POLICY_TYPE
]) {
156 struct xfrm_userpolicy_type
*upt
;
158 fprintf(fp
, "ptype ");
160 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
161 fprintf(fp
, "(ERROR truncated)");
163 upt
= RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
164 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
167 fprintf(fp
, "%s", _SL_
);
176 static int xfrm_report_print(const struct sockaddr_nl
*who
,
177 struct nlmsghdr
*n
, void *arg
)
179 FILE *fp
= (FILE *)arg
;
180 struct xfrm_user_report
*xrep
= NLMSG_DATA(n
);
181 int len
= n
->nlmsg_len
;
182 struct rtattr
*tb
[XFRMA_MAX
+1];
185 len
-= NLMSG_LENGTH(sizeof(*xrep
));
187 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
191 family
= xrep
->sel
.family
;
192 if (family
== AF_UNSPEC
)
193 family
= preferred_family
;
195 fprintf(fp
, "report ");
197 fprintf(fp
, "proto %s ", strxf_xfrmproto(xrep
->proto
));
198 fprintf(fp
, "%s", _SL_
);
200 xfrm_selector_print(&xrep
->sel
, family
, fp
, " sel ");
202 parse_rtattr(tb
, XFRMA_MAX
, XFRMREP_RTA(xrep
), len
);
204 xfrm_xfrma_print(tb
, family
, fp
, " ");
212 static void xfrm_ae_flags_print(__u32 flags
, void *arg
)
214 FILE *fp
= (FILE *)arg
;
216 fprintf(fp
, " (0x%x) ", flags
);
219 if (flags
& XFRM_AE_CR
)
220 fprintf(fp
, " replay update ");
221 if (flags
& XFRM_AE_CE
)
222 fprintf(fp
, " timer expired ");
223 if (flags
& XFRM_AE_CU
)
224 fprintf(fp
, " policy updated ");
228 static void xfrm_usersa_print(const struct xfrm_usersa_id
*sa_id
, __u32 reqid
, FILE *fp
)
230 fprintf(fp
, "dst %s ",
231 rt_addr_n2a(sa_id
->family
, sizeof(sa_id
->daddr
), &sa_id
->daddr
));
233 fprintf(fp
, " reqid 0x%x", reqid
);
235 fprintf(fp
, " protocol %s ", strxf_proto(sa_id
->proto
));
236 fprintf(fp
, " SPI 0x%x", ntohl(sa_id
->spi
));
239 static int xfrm_ae_print(const struct sockaddr_nl
*who
,
240 struct nlmsghdr
*n
, void *arg
)
242 FILE *fp
= (FILE *)arg
;
243 struct xfrm_aevent_id
*id
= NLMSG_DATA(n
);
245 fprintf(fp
, "Async event ");
246 xfrm_ae_flags_print(id
->flags
, arg
);
248 fprintf(fp
, "src %s ", rt_addr_n2a(id
->sa_id
.family
,
249 sizeof(id
->saddr
), &id
->saddr
));
251 xfrm_usersa_print(&id
->sa_id
, id
->reqid
, fp
);
259 static void xfrm_print_addr(FILE *fp
, int family
, xfrm_address_t
*a
)
261 fprintf(fp
, "%s", rt_addr_n2a(family
, sizeof(*a
), a
));
264 static int xfrm_mapping_print(const struct sockaddr_nl
*who
,
265 struct nlmsghdr
*n
, void *arg
)
267 FILE *fp
= (FILE *)arg
;
268 struct xfrm_user_mapping
*map
= NLMSG_DATA(n
);
270 fprintf(fp
, "Mapping change ");
271 xfrm_print_addr(fp
, map
->id
.family
, &map
->old_saddr
);
273 fprintf(fp
, ":%d -> ", ntohs(map
->old_sport
));
274 xfrm_print_addr(fp
, map
->id
.family
, &map
->new_saddr
);
275 fprintf(fp
, ":%d\n\t", ntohs(map
->new_sport
));
277 xfrm_usersa_print(&map
->id
, map
->reqid
, fp
);
284 static int xfrm_accept_msg(const struct sockaddr_nl
*who
,
285 struct rtnl_ctrl_data
*ctrl
,
286 struct nlmsghdr
*n
, void *arg
)
288 FILE *fp
= (FILE *)arg
;
293 if (listen_all_nsid
) {
294 if (ctrl
== NULL
|| ctrl
->nsid
< 0)
295 fprintf(fp
, "[nsid current]");
297 fprintf(fp
, "[nsid %d]", ctrl
->nsid
);
300 switch (n
->nlmsg_type
) {
304 case XFRM_MSG_EXPIRE
:
305 xfrm_state_print(who
, n
, arg
);
307 case XFRM_MSG_NEWPOLICY
:
308 case XFRM_MSG_DELPOLICY
:
309 case XFRM_MSG_UPDPOLICY
:
310 case XFRM_MSG_POLEXPIRE
:
311 xfrm_policy_print(who
, n
, arg
);
313 case XFRM_MSG_ACQUIRE
:
314 xfrm_acquire_print(who
, n
, arg
);
316 case XFRM_MSG_FLUSHSA
:
317 xfrm_state_flush_print(who
, n
, arg
);
319 case XFRM_MSG_FLUSHPOLICY
:
320 xfrm_policy_flush_print(who
, n
, arg
);
322 case XFRM_MSG_REPORT
:
323 xfrm_report_print(who
, n
, arg
);
326 xfrm_ae_print(who
, n
, arg
);
328 case XFRM_MSG_MAPPING
:
329 xfrm_mapping_print(who
, n
, arg
);
335 if (n
->nlmsg_type
!= NLMSG_ERROR
&& n
->nlmsg_type
!= NLMSG_NOOP
&&
336 n
->nlmsg_type
!= NLMSG_DONE
) {
337 fprintf(fp
, "Unknown message: %08d 0x%08x 0x%08x\n",
338 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
343 extern struct rtnl_handle rth
;
345 int do_xfrm_monitor(int argc
, char **argv
)
348 unsigned int groups
= ~((unsigned)0); /* XXX */
359 if (matches(*argv
, "file") == 0) {
362 } else if (strcmp(*argv
, "all") == 0) {
364 } else if (matches(*argv
, "all-nsid") == 0) {
366 } else if (matches(*argv
, "acquire") == 0) {
369 } else if (matches(*argv
, "expire") == 0) {
372 } else if (matches(*argv
, "SA") == 0) {
375 } else if (matches(*argv
, "aevent") == 0) {
378 } else if (matches(*argv
, "policy") == 0) {
381 } else if (matches(*argv
, "report") == 0) {
384 } else if (matches(*argv
, "help") == 0) {
387 fprintf(stderr
, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv
);
394 groups
|= nl_mgrp(XFRMNLGRP_ACQUIRE
);
396 groups
|= nl_mgrp(XFRMNLGRP_EXPIRE
);
398 groups
|= nl_mgrp(XFRMNLGRP_SA
);
400 groups
|= nl_mgrp(XFRMNLGRP_POLICY
);
402 groups
|= nl_mgrp(XFRMNLGRP_AEVENTS
);
404 groups
|= nl_mgrp(XFRMNLGRP_REPORT
);
410 fp
= fopen(file
, "r");
412 perror("Cannot fopen");
415 err
= rtnl_from_file(fp
, xfrm_accept_msg
, stdout
);
420 if (rtnl_open_byproto(&rth
, groups
, NETLINK_XFRM
) < 0)
422 if (listen_all_nsid
&& rtnl_listen_all_nsid(&rth
) < 0)
425 if (rtnl_listen(&rth
, xfrm_accept_msg
, (void *)stdout
) < 0)