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