]>
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 <linux/xfrm.h>
33 #include "ip_common.h"
35 static void usage(void) __attribute__((noreturn
));
37 static void usage(void)
39 fprintf(stderr
, "Usage: ip xfrm monitor [ all | LISTofXFRM-OBJECTS ]\n");
43 static int xfrm_acquire_print(const struct sockaddr_nl
*who
,
44 struct nlmsghdr
*n
, void *arg
)
46 FILE *fp
= (FILE*)arg
;
47 struct xfrm_user_acquire
*xacq
= NLMSG_DATA(n
);
48 int len
= n
->nlmsg_len
;
49 struct rtattr
* tb
[XFRMA_MAX
+1];
52 len
-= NLMSG_LENGTH(sizeof(*xacq
));
54 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
58 parse_rtattr(tb
, XFRMA_MAX
, XFRMACQ_RTA(xacq
), len
);
60 family
= xacq
->sel
.family
;
61 if (family
== AF_UNSPEC
)
62 family
= xacq
->policy
.sel
.family
;
63 if (family
== AF_UNSPEC
)
64 family
= preferred_family
;
66 fprintf(fp
, "acquire ");
68 fprintf(fp
, "proto %s ", strxf_xfrmproto(xacq
->id
.proto
));
69 if (show_stats
> 0 || xacq
->id
.spi
) {
70 __u32 spi
= ntohl(xacq
->id
.spi
);
71 fprintf(fp
, "spi 0x%08x", spi
);
73 fprintf(fp
, "(%u)", spi
);
76 fprintf(fp
, "%s", _SL_
);
78 xfrm_selector_print(&xacq
->sel
, family
, fp
, " sel ");
80 xfrm_policy_info_print(&xacq
->policy
, tb
, fp
, " ", " policy ");
83 fprintf(fp
, " seq 0x%08u ", xacq
->seq
);
85 fprintf(fp
, "%s-mask %s ",
86 strxf_algotype(XFRMA_ALG_CRYPT
),
87 strxf_mask32(xacq
->ealgos
));
88 fprintf(fp
, "%s-mask %s ",
89 strxf_algotype(XFRMA_ALG_AUTH
),
90 strxf_mask32(xacq
->aalgos
));
91 fprintf(fp
, "%s-mask %s",
92 strxf_algotype(XFRMA_ALG_COMP
),
93 strxf_mask32(xacq
->calgos
));
95 fprintf(fp
, "%s", _SL_
);
104 static int xfrm_state_flush_print(const struct sockaddr_nl
*who
,
105 struct nlmsghdr
*n
, void *arg
)
107 FILE *fp
= (FILE*)arg
;
108 struct xfrm_usersa_flush
*xsf
= NLMSG_DATA(n
);
109 int len
= n
->nlmsg_len
;
112 len
-= NLMSG_SPACE(sizeof(*xsf
));
114 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
118 fprintf(fp
, "Flushed state ");
120 str
= strxf_xfrmproto(xsf
->proto
);
122 fprintf(fp
, "proto %s", str
);
124 fprintf(fp
, "proto %u", xsf
->proto
);
125 fprintf(fp
, "%s", _SL_
);
134 static int xfrm_policy_flush_print(const struct sockaddr_nl
*who
,
135 struct nlmsghdr
*n
, void *arg
)
137 struct rtattr
* tb
[XFRMA_MAX
+1];
138 FILE *fp
= (FILE*)arg
;
139 int len
= n
->nlmsg_len
;
141 len
-= NLMSG_SPACE(0);
143 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
147 fprintf(fp
, "Flushed policy ");
149 parse_rtattr(tb
, XFRMA_MAX
, NLMSG_DATA(n
), len
);
151 if (tb
[XFRMA_POLICY_TYPE
]) {
152 struct xfrm_userpolicy_type
*upt
;
154 fprintf(fp
, "ptype ");
156 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
157 fprintf(fp
, "(ERROR truncated)");
159 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
160 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
163 fprintf(fp
, "%s", _SL_
);
172 static int xfrm_report_print(const struct sockaddr_nl
*who
,
173 struct nlmsghdr
*n
, void *arg
)
175 FILE *fp
= (FILE*)arg
;
176 struct xfrm_user_report
*xrep
= NLMSG_DATA(n
);
177 int len
= n
->nlmsg_len
;
178 struct rtattr
* tb
[XFRMA_MAX
+1];
181 len
-= NLMSG_LENGTH(sizeof(*xrep
));
183 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
187 family
= xrep
->sel
.family
;
188 if (family
== AF_UNSPEC
)
189 family
= preferred_family
;
191 fprintf(fp
, "report ");
193 fprintf(fp
, "proto %s ", strxf_xfrmproto(xrep
->proto
));
194 fprintf(fp
, "%s", _SL_
);
196 xfrm_selector_print(&xrep
->sel
, family
, fp
, " sel ");
198 parse_rtattr(tb
, XFRMA_MAX
, XFRMREP_RTA(xrep
), len
);
200 xfrm_xfrma_print(tb
, family
, fp
, " ");
208 static void xfrm_ae_flags_print(__u32 flags
, void *arg
)
210 FILE *fp
= (FILE*)arg
;
211 fprintf(fp
, " (0x%x) ", flags
);
214 if (flags
& XFRM_AE_CR
)
215 fprintf(fp
, " replay update ");
216 if (flags
& XFRM_AE_CE
)
217 fprintf(fp
, " timer expired ");
218 if (flags
& XFRM_AE_CU
)
219 fprintf(fp
, " policy updated ");
223 static void xfrm_usersa_print(const struct xfrm_usersa_id
*sa_id
, __u32 reqid
, FILE *fp
)
228 fprintf(fp
, "dst %s ", rt_addr_n2a(sa_id
->family
,
229 sizeof(sa_id
->daddr
), &sa_id
->daddr
, buf
, sizeof(buf
)));
231 fprintf(fp
, " reqid 0x%x", reqid
);
233 fprintf(fp
, " protocol %s ", strxf_proto(sa_id
->proto
));
234 fprintf(fp
, " SPI 0x%x", ntohl(sa_id
->spi
));
237 static int xfrm_ae_print(const struct sockaddr_nl
*who
,
238 struct nlmsghdr
*n
, void *arg
)
240 FILE *fp
= (FILE*)arg
;
241 struct xfrm_aevent_id
*id
= NLMSG_DATA(n
);
244 fprintf(fp
, "Async event ");
245 xfrm_ae_flags_print(id
->flags
, arg
);
247 memset(abuf
, '\0', sizeof(abuf
));
248 fprintf(fp
, "src %s ", rt_addr_n2a(id
->sa_id
.family
,
249 sizeof(id
->saddr
), &id
->saddr
,
250 abuf
, sizeof(abuf
)));
252 xfrm_usersa_print(&id
->sa_id
, id
->reqid
, fp
);
260 static void xfrm_print_addr(FILE *fp
, int family
, xfrm_address_t
*a
, size_t s
)
265 fprintf(fp
, "%s", rt_addr_n2a(family
, s
, a
, buf
, sizeof(buf
)));
268 static int xfrm_mapping_print(const struct sockaddr_nl
*who
,
269 struct nlmsghdr
*n
, void *arg
)
271 FILE *fp
= (FILE*)arg
;
272 struct xfrm_user_mapping
*map
= NLMSG_DATA(n
);
274 fprintf(fp
, "Mapping change ");
275 xfrm_print_addr(fp
, map
->id
.family
, &map
->old_saddr
,
276 sizeof(map
->old_saddr
));
278 fprintf(fp
, ":%d -> ", ntohs(map
->old_sport
));
279 xfrm_print_addr(fp
, map
->id
.family
, &map
->new_saddr
,
280 sizeof(map
->new_saddr
));
281 fprintf(fp
, ":%d\n\t", ntohs(map
->new_sport
));
283 xfrm_usersa_print(&map
->id
, map
->reqid
, fp
);
290 static int xfrm_accept_msg(const struct sockaddr_nl
*who
,
291 struct nlmsghdr
*n
, void *arg
)
293 FILE *fp
= (FILE*)arg
;
298 switch (n
->nlmsg_type
) {
302 case XFRM_MSG_EXPIRE
:
303 xfrm_state_print(who
, n
, arg
);
305 case XFRM_MSG_NEWPOLICY
:
306 case XFRM_MSG_DELPOLICY
:
307 case XFRM_MSG_UPDPOLICY
:
308 case XFRM_MSG_POLEXPIRE
:
309 xfrm_policy_print(who
, n
, arg
);
311 case XFRM_MSG_ACQUIRE
:
312 xfrm_acquire_print(who
, n
, arg
);
314 case XFRM_MSG_FLUSHSA
:
315 xfrm_state_flush_print(who
, n
, arg
);
317 case XFRM_MSG_FLUSHPOLICY
:
318 xfrm_policy_flush_print(who
, n
, arg
);
320 case XFRM_MSG_REPORT
:
321 xfrm_report_print(who
, n
, arg
);
324 xfrm_ae_print(who
, n
, arg
);
326 case XFRM_MSG_MAPPING
:
327 xfrm_mapping_print(who
, n
, arg
);
333 if (n
->nlmsg_type
!= NLMSG_ERROR
&& n
->nlmsg_type
!= NLMSG_NOOP
&&
334 n
->nlmsg_type
!= NLMSG_DONE
) {
335 fprintf(fp
, "Unknown message: %08d 0x%08x 0x%08x\n",
336 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
341 extern struct rtnl_handle rth
;
343 int do_xfrm_monitor(int argc
, char **argv
)
346 unsigned groups
= ~((unsigned)0); /* XXX */
357 if (matches(*argv
, "file") == 0) {
360 } else if (matches(*argv
, "acquire") == 0) {
363 } else if (matches(*argv
, "expire") == 0) {
366 } else if (matches(*argv
, "SA") == 0) {
369 } else if (matches(*argv
, "aevent") == 0) {
372 } else if (matches(*argv
, "policy") == 0) {
375 } else if (matches(*argv
, "report") == 0) {
378 } else if (matches(*argv
, "help") == 0) {
381 fprintf(stderr
, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv
);
388 groups
|= nl_mgrp(XFRMNLGRP_ACQUIRE
);
390 groups
|= nl_mgrp(XFRMNLGRP_EXPIRE
);
392 groups
|= nl_mgrp(XFRMNLGRP_SA
);
394 groups
|= nl_mgrp(XFRMNLGRP_POLICY
);
396 groups
|= nl_mgrp(XFRMNLGRP_AEVENTS
);
398 groups
|= nl_mgrp(XFRMNLGRP_REPORT
);
402 fp
= fopen(file
, "r");
404 perror("Cannot fopen");
407 return rtnl_from_file(fp
, xfrm_accept_msg
, (void*)stdout
);
410 if (rtnl_open_byproto(&rth
, groups
, NETLINK_XFRM
) < 0)
413 if (rtnl_listen(&rth
, xfrm_accept_msg
, (void*)stdout
) < 0)