]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/xfrm_monitor.c
76905ed3f1e14c56ae338e0d73998ac99c763389
[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
39 static void usage(void)
40 {
41 fprintf(stderr, "Usage: ip xfrm monitor [all-nsid] [ all | OBJECTS | help ]\n");
42 fprintf(stderr, "OBJECTS := { acquire | expire | SA | aevent | policy | report }\n");
43 exit(-1);
44 }
45
46 static int xfrm_acquire_print(struct nlmsghdr *n, void *arg)
47 {
48 FILE *fp = (FILE *)arg;
49 struct xfrm_user_acquire *xacq = NLMSG_DATA(n);
50 int len = n->nlmsg_len;
51 struct rtattr *tb[XFRMA_MAX+1];
52 __u16 family;
53
54 len -= NLMSG_LENGTH(sizeof(*xacq));
55 if (len < 0) {
56 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
57 return -1;
58 }
59
60 parse_rtattr(tb, XFRMA_MAX, XFRMACQ_RTA(xacq), len);
61
62 family = xacq->sel.family;
63 if (family == AF_UNSPEC)
64 family = xacq->policy.sel.family;
65 if (family == AF_UNSPEC)
66 family = preferred_family;
67
68 fprintf(fp, "acquire ");
69
70 fprintf(fp, "proto %s ", strxf_xfrmproto(xacq->id.proto));
71 if (show_stats > 0 || xacq->id.spi) {
72 __u32 spi = ntohl(xacq->id.spi);
73
74 fprintf(fp, "spi 0x%08x", spi);
75 if (show_stats > 0)
76 fprintf(fp, "(%u)", spi);
77 fprintf(fp, " ");
78 }
79 fprintf(fp, "%s", _SL_);
80
81 xfrm_selector_print(&xacq->sel, family, fp, " sel ");
82
83 xfrm_policy_info_print(&xacq->policy, tb, fp, " ", " policy ");
84
85 if (show_stats > 0)
86 fprintf(fp, " seq 0x%08u ", xacq->seq);
87 if (show_stats > 0) {
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));
97 }
98 fprintf(fp, "%s", _SL_);
99
100 if (oneline)
101 fprintf(fp, "\n");
102 fflush(fp);
103
104 return 0;
105 }
106
107 static int xfrm_state_flush_print(struct nlmsghdr *n, void *arg)
108 {
109 FILE *fp = (FILE *)arg;
110 struct xfrm_usersa_flush *xsf = NLMSG_DATA(n);
111 int len = n->nlmsg_len;
112 const char *str;
113
114 len -= NLMSG_SPACE(sizeof(*xsf));
115 if (len < 0) {
116 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
117 return -1;
118 }
119
120 fprintf(fp, "Flushed state ");
121
122 str = strxf_xfrmproto(xsf->proto);
123 if (str)
124 fprintf(fp, "proto %s", str);
125 else
126 fprintf(fp, "proto %u", xsf->proto);
127 fprintf(fp, "%s", _SL_);
128
129 if (oneline)
130 fprintf(fp, "\n");
131 fflush(fp);
132
133 return 0;
134 }
135
136 static int xfrm_policy_flush_print(struct nlmsghdr *n, void *arg)
137 {
138 struct rtattr *tb[XFRMA_MAX+1];
139 FILE *fp = (FILE *)arg;
140 int len = n->nlmsg_len;
141
142 len -= NLMSG_SPACE(0);
143 if (len < 0) {
144 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
145 return -1;
146 }
147
148 fprintf(fp, "Flushed policy ");
149
150 parse_rtattr(tb, XFRMA_MAX, NLMSG_DATA(n), len);
151
152 if (tb[XFRMA_POLICY_TYPE]) {
153 struct xfrm_userpolicy_type *upt;
154
155 fprintf(fp, "ptype ");
156
157 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
158 fprintf(fp, "(ERROR truncated)");
159
160 upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]);
161 fprintf(fp, "%s ", strxf_ptype(upt->type));
162 }
163
164 fprintf(fp, "%s", _SL_);
165
166 if (oneline)
167 fprintf(fp, "\n");
168 fflush(fp);
169
170 return 0;
171 }
172
173 static int xfrm_report_print(struct nlmsghdr *n, void *arg)
174 {
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];
179 __u16 family;
180
181 len -= NLMSG_LENGTH(sizeof(*xrep));
182 if (len < 0) {
183 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
184 return -1;
185 }
186
187 family = xrep->sel.family;
188 if (family == AF_UNSPEC)
189 family = preferred_family;
190
191 fprintf(fp, "report ");
192
193 fprintf(fp, "proto %s ", strxf_xfrmproto(xrep->proto));
194 fprintf(fp, "%s", _SL_);
195
196 xfrm_selector_print(&xrep->sel, family, fp, " sel ");
197
198 parse_rtattr(tb, XFRMA_MAX, XFRMREP_RTA(xrep), len);
199
200 xfrm_xfrma_print(tb, family, fp, " ");
201
202 if (oneline)
203 fprintf(fp, "\n");
204
205 return 0;
206 }
207
208 static void xfrm_ae_flags_print(__u32 flags, void *arg)
209 {
210 FILE *fp = (FILE *)arg;
211
212 fprintf(fp, " (0x%x) ", flags);
213 if (!flags)
214 return;
215 if (flags & XFRM_AE_CR)
216 fprintf(fp, " replay update ");
217 if (flags & XFRM_AE_CE)
218 fprintf(fp, " timer expired ");
219 if (flags & XFRM_AE_CU)
220 fprintf(fp, " policy updated ");
221
222 }
223
224 static void xfrm_usersa_print(const struct xfrm_usersa_id *sa_id, __u32 reqid, FILE *fp)
225 {
226 fprintf(fp, "dst %s ",
227 rt_addr_n2a(sa_id->family, sizeof(sa_id->daddr), &sa_id->daddr));
228
229 fprintf(fp, " reqid 0x%x", reqid);
230
231 fprintf(fp, " protocol %s ", strxf_proto(sa_id->proto));
232 fprintf(fp, " SPI 0x%x", ntohl(sa_id->spi));
233 }
234
235 static int xfrm_ae_print(struct nlmsghdr *n, void *arg)
236 {
237 FILE *fp = (FILE *)arg;
238 struct xfrm_aevent_id *id = NLMSG_DATA(n);
239
240 fprintf(fp, "Async event ");
241 xfrm_ae_flags_print(id->flags, arg);
242 fprintf(fp, "\n\t");
243 fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family,
244 sizeof(id->saddr), &id->saddr));
245
246 xfrm_usersa_print(&id->sa_id, id->reqid, fp);
247
248 fprintf(fp, "\n");
249 fflush(fp);
250
251 return 0;
252 }
253
254 static void xfrm_print_addr(FILE *fp, int family, xfrm_address_t *a)
255 {
256 fprintf(fp, "%s", rt_addr_n2a(family, sizeof(*a), a));
257 }
258
259 static int xfrm_mapping_print(struct nlmsghdr *n, void *arg)
260 {
261 FILE *fp = (FILE *)arg;
262 struct xfrm_user_mapping *map = NLMSG_DATA(n);
263
264 fprintf(fp, "Mapping change ");
265 xfrm_print_addr(fp, map->id.family, &map->old_saddr);
266
267 fprintf(fp, ":%d -> ", ntohs(map->old_sport));
268 xfrm_print_addr(fp, map->id.family, &map->new_saddr);
269 fprintf(fp, ":%d\n\t", ntohs(map->new_sport));
270
271 xfrm_usersa_print(&map->id, map->reqid, fp);
272
273 fprintf(fp, "\n");
274 fflush(fp);
275 return 0;
276 }
277
278 static int xfrm_accept_msg(struct rtnl_ctrl_data *ctrl,
279 struct nlmsghdr *n, void *arg)
280 {
281 FILE *fp = (FILE *)arg;
282
283 if (timestamp)
284 print_timestamp(fp);
285
286 if (listen_all_nsid) {
287 if (ctrl == NULL || ctrl->nsid < 0)
288 fprintf(fp, "[nsid current]");
289 else
290 fprintf(fp, "[nsid %d]", ctrl->nsid);
291 }
292
293 switch (n->nlmsg_type) {
294 case XFRM_MSG_NEWSA:
295 case XFRM_MSG_DELSA:
296 case XFRM_MSG_UPDSA:
297 case XFRM_MSG_EXPIRE:
298 xfrm_state_print(n, arg);
299 return 0;
300 case XFRM_MSG_NEWPOLICY:
301 case XFRM_MSG_DELPOLICY:
302 case XFRM_MSG_UPDPOLICY:
303 case XFRM_MSG_POLEXPIRE:
304 xfrm_policy_print(n, arg);
305 return 0;
306 case XFRM_MSG_ACQUIRE:
307 xfrm_acquire_print(n, arg);
308 return 0;
309 case XFRM_MSG_FLUSHSA:
310 xfrm_state_flush_print(n, arg);
311 return 0;
312 case XFRM_MSG_FLUSHPOLICY:
313 xfrm_policy_flush_print(n, arg);
314 return 0;
315 case XFRM_MSG_REPORT:
316 xfrm_report_print(n, arg);
317 return 0;
318 case XFRM_MSG_NEWAE:
319 xfrm_ae_print(n, arg);
320 return 0;
321 case XFRM_MSG_MAPPING:
322 xfrm_mapping_print(n, arg);
323 return 0;
324 default:
325 break;
326 }
327
328 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
329 n->nlmsg_type != NLMSG_DONE) {
330 fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n",
331 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
332 }
333 return 0;
334 }
335
336 extern struct rtnl_handle rth;
337
338 int do_xfrm_monitor(int argc, char **argv)
339 {
340 char *file = NULL;
341 unsigned int groups = ~((unsigned)0); /* XXX */
342 int lacquire = 0;
343 int lexpire = 0;
344 int laevent = 0;
345 int lpolicy = 0;
346 int lsa = 0;
347 int lreport = 0;
348
349 rtnl_close(&rth);
350
351 while (argc > 0) {
352 if (matches(*argv, "file") == 0) {
353 NEXT_ARG();
354 file = *argv;
355 } else if (strcmp(*argv, "all") == 0) {
356 /* fall out */
357 } else if (matches(*argv, "all-nsid") == 0) {
358 listen_all_nsid = 1;
359 } else if (matches(*argv, "acquire") == 0) {
360 lacquire = 1;
361 groups = 0;
362 } else if (matches(*argv, "expire") == 0) {
363 lexpire = 1;
364 groups = 0;
365 } else if (matches(*argv, "SA") == 0) {
366 lsa = 1;
367 groups = 0;
368 } else if (matches(*argv, "aevent") == 0) {
369 laevent = 1;
370 groups = 0;
371 } else if (matches(*argv, "policy") == 0) {
372 lpolicy = 1;
373 groups = 0;
374 } else if (matches(*argv, "report") == 0) {
375 lreport = 1;
376 groups = 0;
377 } else if (matches(*argv, "help") == 0) {
378 usage();
379 } else {
380 fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv);
381 exit(-1);
382 }
383 argc--; argv++;
384 }
385
386 if (lacquire)
387 groups |= nl_mgrp(XFRMNLGRP_ACQUIRE);
388 if (lexpire)
389 groups |= nl_mgrp(XFRMNLGRP_EXPIRE);
390 if (lsa)
391 groups |= nl_mgrp(XFRMNLGRP_SA);
392 if (lpolicy)
393 groups |= nl_mgrp(XFRMNLGRP_POLICY);
394 if (laevent)
395 groups |= nl_mgrp(XFRMNLGRP_AEVENTS);
396 if (lreport)
397 groups |= nl_mgrp(XFRMNLGRP_REPORT);
398
399 if (file) {
400 FILE *fp;
401 int err;
402
403 fp = fopen(file, "r");
404 if (fp == NULL) {
405 perror("Cannot fopen");
406 exit(-1);
407 }
408 err = rtnl_from_file(fp, xfrm_accept_msg, stdout);
409 fclose(fp);
410 return err;
411 }
412
413 if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0)
414 exit(1);
415 if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0)
416 exit(1);
417
418 if (rtnl_listen(&rth, xfrm_accept_msg, (void *)stdout) < 0)
419 exit(2);
420
421 return 0;
422 }