]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/xfrm_monitor.c
man: tc-taprio.8: fix syntax error
[mirror_iproute2.git] / ip / xfrm_monitor.c
CommitLineData
f9cb3a2f 1/* $USAGI: $ */
2
3/*
4 * Copyright (C)2005 USAGI/WIDE Project
ae665a52 5 *
f9cb3a2f 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.
ae665a52 10 *
f9cb3a2f 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.
ae665a52 15 *
f9cb3a2f 16 * You should have received a copy of the GNU General Public License
4d98ab00 17 * along with this program; if not, see <http://www.gnu.org/licenses>.
f9cb3a2f 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>
93390779 30#include <netinet/in.h>
93390779 31
f9cb3a2f 32#include "utils.h"
33#include "xfrm.h"
34#include "ip_common.h"
35
36static void usage(void) __attribute__((noreturn));
fa92d8cb 37static int listen_all_nsid;
f9cb3a2f 38
39static void usage(void)
40{
b6ec53e3 41 fprintf(stderr, "Usage: ip xfrm monitor [all-nsid] [ all | OBJECTS | help ]\n");
c16298be 42 fprintf(stderr, "OBJECTS := { acquire | expire | SA | aevent | policy | report }\n");
f9cb3a2f 43 exit(-1);
44}
45
cd554f2c 46static int xfrm_acquire_print(struct nlmsghdr *n, void *arg)
f9cb3a2f 47{
56f5daac 48 FILE *fp = (FILE *)arg;
f9cb3a2f 49 struct xfrm_user_acquire *xacq = NLMSG_DATA(n);
50 int len = n->nlmsg_len;
56f5daac 51 struct rtattr *tb[XFRMA_MAX+1];
f9cb3a2f 52 __u16 family;
53
f9cb3a2f 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);
90f93024 61
f9cb3a2f 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);
56f5daac 73
f9cb3a2f 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");
669ae748 102 fflush(fp);
f9cb3a2f 103
104 return 0;
105}
106
cd554f2c 107static int xfrm_state_flush_print(struct nlmsghdr *n, void *arg)
efe69c1b 108{
56f5daac 109 FILE *fp = (FILE *)arg;
efe69c1b
MN
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
cd554f2c 136static int xfrm_policy_flush_print(struct nlmsghdr *n, void *arg)
efe69c1b 137{
56f5daac
SH
138 struct rtattr *tb[XFRMA_MAX+1];
139 FILE *fp = (FILE *)arg;
efe69c1b
MN
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
9f1370c0 160 upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]);
efe69c1b
MN
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
cd554f2c 173static int xfrm_report_print(struct nlmsghdr *n, void *arg)
c54f31ee 174{
56f5daac 175 FILE *fp = (FILE *)arg;
c54f31ee
MN
176 struct xfrm_user_report *xrep = NLMSG_DATA(n);
177 int len = n->nlmsg_len;
56f5daac 178 struct rtattr *tb[XFRMA_MAX+1];
c54f31ee
MN
179 __u16 family;
180
c54f31ee
MN
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
d1f28cf1 208static void xfrm_ae_flags_print(__u32 flags, void *arg)
c9fd974b 209{
56f5daac
SH
210 FILE *fp = (FILE *)arg;
211
c9fd974b 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
598a42c0
FW
224static void xfrm_usersa_print(const struct xfrm_usersa_id *sa_id, __u32 reqid, FILE *fp)
225{
656111b2 226 fprintf(fp, "dst %s ",
2e96d2cc 227 rt_addr_n2a(sa_id->family, sizeof(sa_id->daddr), &sa_id->daddr));
598a42c0
FW
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
cd554f2c 235static int xfrm_ae_print(struct nlmsghdr *n, void *arg)
c9fd974b 236{
56f5daac 237 FILE *fp = (FILE *)arg;
c9fd974b 238 struct xfrm_aevent_id *id = NLMSG_DATA(n);
c9fd974b 239
240 fprintf(fp, "Async event ");
241 xfrm_ae_flags_print(id->flags, arg);
56f5daac 242 fprintf(fp, "\n\t");
26dcdf3a 243 fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family,
2e96d2cc 244 sizeof(id->saddr), &id->saddr));
598a42c0
FW
245
246 xfrm_usersa_print(&id->sa_id, id->reqid, fp);
c9fd974b 247
248 fprintf(fp, "\n");
249 fflush(fp);
250
251 return 0;
252}
253
656111b2 254static void xfrm_print_addr(FILE *fp, int family, xfrm_address_t *a)
598a42c0 255{
2e96d2cc 256 fprintf(fp, "%s", rt_addr_n2a(family, sizeof(*a), a));
598a42c0
FW
257}
258
cd554f2c 259static int xfrm_mapping_print(struct nlmsghdr *n, void *arg)
598a42c0 260{
56f5daac 261 FILE *fp = (FILE *)arg;
598a42c0
FW
262 struct xfrm_user_mapping *map = NLMSG_DATA(n);
263
264 fprintf(fp, "Mapping change ");
656111b2 265 xfrm_print_addr(fp, map->id.family, &map->old_saddr);
598a42c0
FW
266
267 fprintf(fp, ":%d -> ", ntohs(map->old_sport));
656111b2 268 xfrm_print_addr(fp, map->id.family, &map->new_saddr);
598a42c0
FW
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
cd554f2c 278static int xfrm_accept_msg(struct rtnl_ctrl_data *ctrl,
f9cb3a2f 279 struct nlmsghdr *n, void *arg)
280{
56f5daac 281 FILE *fp = (FILE *)arg;
f9cb3a2f 282
90f93024
SH
283 if (timestamp)
284 print_timestamp(fp);
285
b6ec53e3
ND
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
efe69c1b
MN
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:
cd554f2c 298 xfrm_state_print(n, arg);
c595c790 299 return 0;
efe69c1b
MN
300 case XFRM_MSG_NEWPOLICY:
301 case XFRM_MSG_DELPOLICY:
302 case XFRM_MSG_UPDPOLICY:
303 case XFRM_MSG_POLEXPIRE:
cd554f2c 304 xfrm_policy_print(n, arg);
c595c790 305 return 0;
efe69c1b 306 case XFRM_MSG_ACQUIRE:
cd554f2c 307 xfrm_acquire_print(n, arg);
f9cb3a2f 308 return 0;
efe69c1b 309 case XFRM_MSG_FLUSHSA:
cd554f2c 310 xfrm_state_flush_print(n, arg);
f9cb3a2f 311 return 0;
efe69c1b 312 case XFRM_MSG_FLUSHPOLICY:
cd554f2c 313 xfrm_policy_flush_print(n, arg);
f9cb3a2f 314 return 0;
efe69c1b 315 case XFRM_MSG_REPORT:
cd554f2c 316 xfrm_report_print(n, arg);
c54f31ee 317 return 0;
efe69c1b 318 case XFRM_MSG_NEWAE:
cd554f2c 319 xfrm_ae_print(n, arg);
c9fd974b 320 return 0;
598a42c0 321 case XFRM_MSG_MAPPING:
cd554f2c 322 xfrm_mapping_print(n, arg);
598a42c0 323 return 0;
efe69c1b
MN
324 default:
325 break;
c9fd974b 326 }
efe69c1b 327
f9cb3a2f 328 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
329 n->nlmsg_type != NLMSG_DONE) {
c595c790 330 fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n",
f9cb3a2f 331 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
332 }
333 return 0;
334}
335
77219712
SH
336extern struct rtnl_handle rth;
337
f9cb3a2f 338int do_xfrm_monitor(int argc, char **argv)
339{
f9cb3a2f 340 char *file = NULL;
56f5daac
SH
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;
f9cb3a2f 348
77219712
SH
349 rtnl_close(&rth);
350
f9cb3a2f 351 while (argc > 0) {
352 if (matches(*argv, "file") == 0) {
353 NEXT_ARG();
354 file = *argv;
b8e77990
NH
355 } else if (strcmp(*argv, "all") == 0) {
356 /* fall out */
b6ec53e3
ND
357 } else if (matches(*argv, "all-nsid") == 0) {
358 listen_all_nsid = 1;
f9cb3a2f 359 } else if (matches(*argv, "acquire") == 0) {
56f5daac 360 lacquire = 1;
f9cb3a2f 361 groups = 0;
362 } else if (matches(*argv, "expire") == 0) {
56f5daac 363 lexpire = 1;
f9cb3a2f 364 groups = 0;
c595c790 365 } else if (matches(*argv, "SA") == 0) {
56f5daac 366 lsa = 1;
c595c790 367 groups = 0;
c9fd974b 368 } else if (matches(*argv, "aevent") == 0) {
56f5daac 369 laevent = 1;
c9fd974b 370 groups = 0;
c595c790 371 } else if (matches(*argv, "policy") == 0) {
56f5daac 372 lpolicy = 1;
c595c790 373 groups = 0;
c54f31ee 374 } else if (matches(*argv, "report") == 0) {
56f5daac 375 lreport = 1;
c54f31ee 376 groups = 0;
f9cb3a2f 377 } else if (matches(*argv, "help") == 0) {
378 usage();
b8e77990 379 } else {
f9cb3a2f 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)
7b822512 387 groups |= nl_mgrp(XFRMNLGRP_ACQUIRE);
f9cb3a2f 388 if (lexpire)
7b822512 389 groups |= nl_mgrp(XFRMNLGRP_EXPIRE);
c595c790 390 if (lsa)
7b822512 391 groups |= nl_mgrp(XFRMNLGRP_SA);
c595c790 392 if (lpolicy)
7b822512 393 groups |= nl_mgrp(XFRMNLGRP_POLICY);
c9fd974b 394 if (laevent)
7b822512 395 groups |= nl_mgrp(XFRMNLGRP_AEVENTS);
c54f31ee 396 if (lreport)
7b822512 397 groups |= nl_mgrp(XFRMNLGRP_REPORT);
f9cb3a2f 398
399 if (file) {
400 FILE *fp;
e49b51d6
SH
401 int err;
402
f9cb3a2f 403 fp = fopen(file, "r");
404 if (fp == NULL) {
405 perror("Cannot fopen");
406 exit(-1);
407 }
e49b51d6
SH
408 err = rtnl_from_file(fp, xfrm_accept_msg, stdout);
409 fclose(fp);
410 return err;
f9cb3a2f 411 }
412
c595c790
SH
413 if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0)
414 exit(1);
b6ec53e3
ND
415 if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0)
416 exit(1);
c595c790 417
56f5daac 418 if (rtnl_listen(&rth, xfrm_accept_msg, (void *)stdout) < 0)
f9cb3a2f 419 exit(2);
420
351efcde 421 return 0;
f9cb3a2f 422}