]>
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
);
74 fprintf(fp
, "spi 0x%08x", spi
);
76 fprintf(fp
, "(%u)", spi
);
79 fprintf(fp
, "%s", _SL_
);
81 xfrm_selector_print(&xacq
->sel
, family
, fp
, " sel ");
83 xfrm_policy_info_print(&xacq
->policy
, tb
, fp
, " ", " policy ");
86 fprintf(fp
, " seq 0x%08u ", xacq
->seq
);
88 fprintf(fp
, "%s-mask %s ",
89 strxf_algotype(XFRMA_ALG_CRYPT
),
90 strxf_mask32(xacq
->ealgos
));
91 fprintf(fp
, "%s-mask %s ",
92 strxf_algotype(XFRMA_ALG_AUTH
),
93 strxf_mask32(xacq
->aalgos
));
94 fprintf(fp
, "%s-mask %s",
95 strxf_algotype(XFRMA_ALG_COMP
),
96 strxf_mask32(xacq
->calgos
));
98 fprintf(fp
, "%s", _SL_
);
107 static int xfrm_state_flush_print(const struct sockaddr_nl
*who
,
108 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(const struct sockaddr_nl
*who
,
138 struct nlmsghdr
*n
, void *arg
)
140 struct rtattr
* tb
[XFRMA_MAX
+1];
141 FILE *fp
= (FILE*)arg
;
142 int len
= n
->nlmsg_len
;
144 len
-= NLMSG_SPACE(0);
146 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
150 fprintf(fp
, "Flushed policy ");
152 parse_rtattr(tb
, XFRMA_MAX
, NLMSG_DATA(n
), len
);
154 if (tb
[XFRMA_POLICY_TYPE
]) {
155 struct xfrm_userpolicy_type
*upt
;
157 fprintf(fp
, "ptype ");
159 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
160 fprintf(fp
, "(ERROR truncated)");
162 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
163 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
166 fprintf(fp
, "%s", _SL_
);
175 static int xfrm_report_print(const struct sockaddr_nl
*who
,
176 struct nlmsghdr
*n
, void *arg
)
178 FILE *fp
= (FILE*)arg
;
179 struct xfrm_user_report
*xrep
= NLMSG_DATA(n
);
180 int len
= n
->nlmsg_len
;
181 struct rtattr
* tb
[XFRMA_MAX
+1];
184 len
-= NLMSG_LENGTH(sizeof(*xrep
));
186 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
190 family
= xrep
->sel
.family
;
191 if (family
== AF_UNSPEC
)
192 family
= preferred_family
;
194 fprintf(fp
, "report ");
196 fprintf(fp
, "proto %s ", strxf_xfrmproto(xrep
->proto
));
197 fprintf(fp
, "%s", _SL_
);
199 xfrm_selector_print(&xrep
->sel
, family
, fp
, " sel ");
201 parse_rtattr(tb
, XFRMA_MAX
, XFRMREP_RTA(xrep
), len
);
203 xfrm_xfrma_print(tb
, family
, fp
, " ");
211 static void xfrm_ae_flags_print(__u32 flags
, void *arg
)
213 FILE *fp
= (FILE*)arg
;
214 fprintf(fp
, " (0x%x) ", flags
);
217 if (flags
& XFRM_AE_CR
)
218 fprintf(fp
, " replay update ");
219 if (flags
& XFRM_AE_CE
)
220 fprintf(fp
, " timer expired ");
221 if (flags
& XFRM_AE_CU
)
222 fprintf(fp
, " policy updated ");
226 static void xfrm_usersa_print(const struct xfrm_usersa_id
*sa_id
, __u32 reqid
, FILE *fp
)
231 fprintf(fp
, "dst %s ",
232 rt_addr_n2a(sa_id
->family
, sizeof(sa_id
->daddr
), &sa_id
->daddr
,
235 fprintf(fp
, " reqid 0x%x", reqid
);
237 fprintf(fp
, " protocol %s ", strxf_proto(sa_id
->proto
));
238 fprintf(fp
, " SPI 0x%x", ntohl(sa_id
->spi
));
241 static int xfrm_ae_print(const struct sockaddr_nl
*who
,
242 struct nlmsghdr
*n
, void *arg
)
244 FILE *fp
= (FILE*)arg
;
245 struct xfrm_aevent_id
*id
= NLMSG_DATA(n
);
248 fprintf(fp
, "Async event ");
249 xfrm_ae_flags_print(id
->flags
, arg
);
251 memset(abuf
, '\0', sizeof(abuf
));
252 fprintf(fp
, "src %s ", rt_addr_n2a(id
->sa_id
.family
,
253 sizeof(id
->saddr
), &id
->saddr
,
254 abuf
, sizeof(abuf
)));
256 xfrm_usersa_print(&id
->sa_id
, id
->reqid
, fp
);
264 static void xfrm_print_addr(FILE *fp
, int family
, xfrm_address_t
*a
)
269 fprintf(fp
, "%s", rt_addr_n2a(family
, sizeof(*a
), a
, buf
, sizeof(buf
)));
272 static int xfrm_mapping_print(const struct sockaddr_nl
*who
,
273 struct nlmsghdr
*n
, void *arg
)
275 FILE *fp
= (FILE*)arg
;
276 struct xfrm_user_mapping
*map
= NLMSG_DATA(n
);
278 fprintf(fp
, "Mapping change ");
279 xfrm_print_addr(fp
, map
->id
.family
, &map
->old_saddr
);
281 fprintf(fp
, ":%d -> ", ntohs(map
->old_sport
));
282 xfrm_print_addr(fp
, map
->id
.family
, &map
->new_saddr
);
283 fprintf(fp
, ":%d\n\t", ntohs(map
->new_sport
));
285 xfrm_usersa_print(&map
->id
, map
->reqid
, fp
);
292 static int xfrm_accept_msg(const struct sockaddr_nl
*who
,
293 struct rtnl_ctrl_data
*ctrl
,
294 struct nlmsghdr
*n
, void *arg
)
296 FILE *fp
= (FILE*)arg
;
301 if (listen_all_nsid
) {
302 if (ctrl
== NULL
|| ctrl
->nsid
< 0)
303 fprintf(fp
, "[nsid current]");
305 fprintf(fp
, "[nsid %d]", ctrl
->nsid
);
308 switch (n
->nlmsg_type
) {
312 case XFRM_MSG_EXPIRE
:
313 xfrm_state_print(who
, n
, arg
);
315 case XFRM_MSG_NEWPOLICY
:
316 case XFRM_MSG_DELPOLICY
:
317 case XFRM_MSG_UPDPOLICY
:
318 case XFRM_MSG_POLEXPIRE
:
319 xfrm_policy_print(who
, n
, arg
);
321 case XFRM_MSG_ACQUIRE
:
322 xfrm_acquire_print(who
, n
, arg
);
324 case XFRM_MSG_FLUSHSA
:
325 xfrm_state_flush_print(who
, n
, arg
);
327 case XFRM_MSG_FLUSHPOLICY
:
328 xfrm_policy_flush_print(who
, n
, arg
);
330 case XFRM_MSG_REPORT
:
331 xfrm_report_print(who
, n
, arg
);
334 xfrm_ae_print(who
, n
, arg
);
336 case XFRM_MSG_MAPPING
:
337 xfrm_mapping_print(who
, n
, arg
);
343 if (n
->nlmsg_type
!= NLMSG_ERROR
&& n
->nlmsg_type
!= NLMSG_NOOP
&&
344 n
->nlmsg_type
!= NLMSG_DONE
) {
345 fprintf(fp
, "Unknown message: %08d 0x%08x 0x%08x\n",
346 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
351 extern struct rtnl_handle rth
;
353 int do_xfrm_monitor(int argc
, char **argv
)
356 unsigned groups
= ~((unsigned)0); /* XXX */
367 if (matches(*argv
, "file") == 0) {
370 } else if (matches(*argv
, "all-nsid") == 0) {
372 } else if (matches(*argv
, "acquire") == 0) {
375 } else if (matches(*argv
, "expire") == 0) {
378 } else if (matches(*argv
, "SA") == 0) {
381 } else if (matches(*argv
, "aevent") == 0) {
384 } else if (matches(*argv
, "policy") == 0) {
387 } else if (matches(*argv
, "report") == 0) {
390 } else if (matches(*argv
, "help") == 0) {
392 } else if (strcmp(*argv
, "all")) {
393 fprintf(stderr
, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv
);
400 groups
|= nl_mgrp(XFRMNLGRP_ACQUIRE
);
402 groups
|= nl_mgrp(XFRMNLGRP_EXPIRE
);
404 groups
|= nl_mgrp(XFRMNLGRP_SA
);
406 groups
|= nl_mgrp(XFRMNLGRP_POLICY
);
408 groups
|= nl_mgrp(XFRMNLGRP_AEVENTS
);
410 groups
|= nl_mgrp(XFRMNLGRP_REPORT
);
414 fp
= fopen(file
, "r");
416 perror("Cannot fopen");
419 return rtnl_from_file(fp
, xfrm_accept_msg
, (void*)stdout
);
422 if (rtnl_open_byproto(&rth
, groups
, NETLINK_XFRM
) < 0)
424 if (listen_all_nsid
&& rtnl_listen_all_nsid(&rth
) < 0)
427 if (rtnl_listen(&rth
, xfrm_accept_msg
, (void*)stdout
) < 0)