]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/xfrm_monitor.c
xfrm: add option to hide keys in state output
[mirror_iproute2.git] / ip / xfrm_monitor.c
1 /* $USAGI: $ */
2
3 /*
4 * Copyright (C)2005 USAGI/WIDE Project
5 *
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.
10 *
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.
15 *
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>.
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>
30 #include <netinet/in.h>
31
32 #include "utils.h"
33 #include "xfrm.h"
34 #include "ip_common.h"
35
36 static void usage(void) __attribute__((noreturn));
37 static int listen_all_nsid;
38 static bool nokeys;
39
40 static void usage(void)
41 {
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");
44 exit(-1);
45 }
46
47 static int xfrm_acquire_print(struct nlmsghdr *n, void *arg)
48 {
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];
53 __u16 family;
54
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);
62
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);
74
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");
103 fflush(fp);
104
105 return 0;
106 }
107
108 static int xfrm_state_flush_print(struct nlmsghdr *n, void *arg)
109 {
110 FILE *fp = (FILE *)arg;
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
137 static int xfrm_policy_flush_print(struct nlmsghdr *n, void *arg)
138 {
139 struct rtattr *tb[XFRMA_MAX+1];
140 FILE *fp = (FILE *)arg;
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
161 upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]);
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
174 static int xfrm_report_print(struct nlmsghdr *n, void *arg)
175 {
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];
180 __u16 family;
181
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
201 xfrm_xfrma_print(tb, family, fp, " ", nokeys);
202
203 if (oneline)
204 fprintf(fp, "\n");
205
206 return 0;
207 }
208
209 static void xfrm_ae_flags_print(__u32 flags, void *arg)
210 {
211 FILE *fp = (FILE *)arg;
212
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
225 static void xfrm_usersa_print(const struct xfrm_usersa_id *sa_id, __u32 reqid, FILE *fp)
226 {
227 fprintf(fp, "dst %s ",
228 rt_addr_n2a(sa_id->family, sizeof(sa_id->daddr), &sa_id->daddr));
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
236 static int xfrm_ae_print(struct nlmsghdr *n, void *arg)
237 {
238 FILE *fp = (FILE *)arg;
239 struct xfrm_aevent_id *id = NLMSG_DATA(n);
240
241 fprintf(fp, "Async event ");
242 xfrm_ae_flags_print(id->flags, arg);
243 fprintf(fp, "\n\t");
244 fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family,
245 sizeof(id->saddr), &id->saddr));
246
247 xfrm_usersa_print(&id->sa_id, id->reqid, fp);
248
249 fprintf(fp, "\n");
250 fflush(fp);
251
252 return 0;
253 }
254
255 static void xfrm_print_addr(FILE *fp, int family, xfrm_address_t *a)
256 {
257 fprintf(fp, "%s", rt_addr_n2a(family, sizeof(*a), a));
258 }
259
260 static int xfrm_mapping_print(struct nlmsghdr *n, void *arg)
261 {
262 FILE *fp = (FILE *)arg;
263 struct xfrm_user_mapping *map = NLMSG_DATA(n);
264
265 fprintf(fp, "Mapping change ");
266 xfrm_print_addr(fp, map->id.family, &map->old_saddr);
267
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));
271
272 xfrm_usersa_print(&map->id, map->reqid, fp);
273
274 fprintf(fp, "\n");
275 fflush(fp);
276 return 0;
277 }
278
279 static int xfrm_accept_msg(struct rtnl_ctrl_data *ctrl,
280 struct nlmsghdr *n, void *arg)
281 {
282 FILE *fp = (FILE *)arg;
283
284 if (timestamp)
285 print_timestamp(fp);
286
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
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:
299 xfrm_state_print(n, arg);
300 return 0;
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);
306 return 0;
307 case XFRM_MSG_ACQUIRE:
308 xfrm_acquire_print(n, arg);
309 return 0;
310 case XFRM_MSG_FLUSHSA:
311 xfrm_state_flush_print(n, arg);
312 return 0;
313 case XFRM_MSG_FLUSHPOLICY:
314 xfrm_policy_flush_print(n, arg);
315 return 0;
316 case XFRM_MSG_REPORT:
317 xfrm_report_print(n, arg);
318 return 0;
319 case XFRM_MSG_NEWAE:
320 xfrm_ae_print(n, arg);
321 return 0;
322 case XFRM_MSG_MAPPING:
323 xfrm_mapping_print(n, arg);
324 return 0;
325 default:
326 break;
327 }
328
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);
333 }
334 return 0;
335 }
336
337 extern struct rtnl_handle rth;
338
339 int do_xfrm_monitor(int argc, char **argv)
340 {
341 char *file = NULL;
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;
349
350 rtnl_close(&rth);
351
352 while (argc > 0) {
353 if (matches(*argv, "file") == 0) {
354 NEXT_ARG();
355 file = *argv;
356 } else if (strcmp(*argv, "nokeys") == 0) {
357 nokeys = true;
358 } else if (strcmp(*argv, "all") == 0) {
359 /* fall out */
360 } else if (matches(*argv, "all-nsid") == 0) {
361 listen_all_nsid = 1;
362 } else if (matches(*argv, "acquire") == 0) {
363 lacquire = 1;
364 groups = 0;
365 } else if (matches(*argv, "expire") == 0) {
366 lexpire = 1;
367 groups = 0;
368 } else if (matches(*argv, "SA") == 0) {
369 lsa = 1;
370 groups = 0;
371 } else if (matches(*argv, "aevent") == 0) {
372 laevent = 1;
373 groups = 0;
374 } else if (matches(*argv, "policy") == 0) {
375 lpolicy = 1;
376 groups = 0;
377 } else if (matches(*argv, "report") == 0) {
378 lreport = 1;
379 groups = 0;
380 } else if (matches(*argv, "help") == 0) {
381 usage();
382 } else {
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)
390 groups |= nl_mgrp(XFRMNLGRP_ACQUIRE);
391 if (lexpire)
392 groups |= nl_mgrp(XFRMNLGRP_EXPIRE);
393 if (lsa)
394 groups |= nl_mgrp(XFRMNLGRP_SA);
395 if (lpolicy)
396 groups |= nl_mgrp(XFRMNLGRP_POLICY);
397 if (laevent)
398 groups |= nl_mgrp(XFRMNLGRP_AEVENTS);
399 if (lreport)
400 groups |= nl_mgrp(XFRMNLGRP_REPORT);
401
402 if (file) {
403 FILE *fp;
404 int err;
405
406 fp = fopen(file, "r");
407 if (fp == NULL) {
408 perror("Cannot fopen");
409 exit(-1);
410 }
411 err = rtnl_from_file(fp, xfrm_accept_msg, stdout);
412 fclose(fp);
413 return err;
414 }
415
416 if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0)
417 exit(1);
418 if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0)
419 exit(1);
420
421 if (rtnl_listen(&rth, xfrm_accept_msg, (void *)stdout) < 0)
422 exit(2);
423
424 return 0;
425 }