]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/xfrm_monitor.c
libnetlink: introduce rtnl_listen_filter_t
[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>
f9cb3a2f 31#include <linux/xfrm.h>
93390779 32
f9cb3a2f 33#include "utils.h"
34#include "xfrm.h"
35#include "ip_common.h"
36
37static void usage(void) __attribute__((noreturn));
38
39static void usage(void)
40{
c16298be
VK
41 fprintf(stderr, "Usage: ip xfrm monitor [ all | OBJECTS | help ]\n");
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{
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
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);
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
efe69c1b
MN
107static int xfrm_state_flush_print(const struct sockaddr_nl *who,
108 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
137static int xfrm_policy_flush_print(const struct sockaddr_nl *who,
138 struct nlmsghdr *n, void *arg)
139{
140 struct rtattr * tb[XFRMA_MAX+1];
141 FILE *fp = (FILE*)arg;
142 int len = n->nlmsg_len;
143
144 len -= NLMSG_SPACE(0);
145 if (len < 0) {
146 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
147 return -1;
148 }
149
150 fprintf(fp, "Flushed policy ");
151
152 parse_rtattr(tb, XFRMA_MAX, NLMSG_DATA(n), len);
153
154 if (tb[XFRMA_POLICY_TYPE]) {
155 struct xfrm_userpolicy_type *upt;
156
157 fprintf(fp, "ptype ");
158
159 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
160 fprintf(fp, "(ERROR truncated)");
161
162 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
163 fprintf(fp, "%s ", strxf_ptype(upt->type));
164 }
165
166 fprintf(fp, "%s", _SL_);
167
168 if (oneline)
169 fprintf(fp, "\n");
170 fflush(fp);
171
172 return 0;
173}
174
c54f31ee
MN
175static int xfrm_report_print(const struct sockaddr_nl *who,
176 struct nlmsghdr *n, void *arg)
177{
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];
182 __u16 family;
183
c54f31ee
MN
184 len -= NLMSG_LENGTH(sizeof(*xrep));
185 if (len < 0) {
186 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
187 return -1;
188 }
189
190 family = xrep->sel.family;
191 if (family == AF_UNSPEC)
192 family = preferred_family;
193
194 fprintf(fp, "report ");
195
196 fprintf(fp, "proto %s ", strxf_xfrmproto(xrep->proto));
197 fprintf(fp, "%s", _SL_);
198
199 xfrm_selector_print(&xrep->sel, family, fp, " sel ");
200
201 parse_rtattr(tb, XFRMA_MAX, XFRMREP_RTA(xrep), len);
202
203 xfrm_xfrma_print(tb, family, fp, " ");
204
205 if (oneline)
206 fprintf(fp, "\n");
207
208 return 0;
209}
210
d1f28cf1 211static void xfrm_ae_flags_print(__u32 flags, void *arg)
c9fd974b 212{
213 FILE *fp = (FILE*)arg;
214 fprintf(fp, " (0x%x) ", flags);
215 if (!flags)
216 return;
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 ");
223
224}
225
598a42c0
FW
226static void xfrm_usersa_print(const struct xfrm_usersa_id *sa_id, __u32 reqid, FILE *fp)
227{
228 char buf[256];
229
230 buf[0] = 0;
656111b2 231 fprintf(fp, "dst %s ",
26dcdf3a
EB
232 rt_addr_n2a(sa_id->family, sizeof(sa_id->daddr), &sa_id->daddr,
233 buf, sizeof(buf)));
598a42c0
FW
234
235 fprintf(fp, " reqid 0x%x", reqid);
236
237 fprintf(fp, " protocol %s ", strxf_proto(sa_id->proto));
238 fprintf(fp, " SPI 0x%x", ntohl(sa_id->spi));
239}
240
c9fd974b 241static int xfrm_ae_print(const struct sockaddr_nl *who,
242 struct nlmsghdr *n, void *arg)
243{
244 FILE *fp = (FILE*)arg;
245 struct xfrm_aevent_id *id = NLMSG_DATA(n);
246 char abuf[256];
247
248 fprintf(fp, "Async event ");
249 xfrm_ae_flags_print(id->flags, arg);
250 fprintf(fp,"\n\t");
251 memset(abuf, '\0', sizeof(abuf));
26dcdf3a
EB
252 fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family,
253 sizeof(id->saddr), &id->saddr,
656111b2 254 abuf, sizeof(abuf)));
598a42c0
FW
255
256 xfrm_usersa_print(&id->sa_id, id->reqid, fp);
c9fd974b 257
258 fprintf(fp, "\n");
259 fflush(fp);
260
261 return 0;
262}
263
656111b2 264static void xfrm_print_addr(FILE *fp, int family, xfrm_address_t *a)
598a42c0
FW
265{
266 char buf[256];
267
268 buf[0] = 0;
26dcdf3a 269 fprintf(fp, "%s", rt_addr_n2a(family, sizeof(*a), a, buf, sizeof(buf)));
598a42c0
FW
270}
271
272static int xfrm_mapping_print(const struct sockaddr_nl *who,
273 struct nlmsghdr *n, void *arg)
274{
275 FILE *fp = (FILE*)arg;
276 struct xfrm_user_mapping *map = NLMSG_DATA(n);
277
278 fprintf(fp, "Mapping change ");
656111b2 279 xfrm_print_addr(fp, map->id.family, &map->old_saddr);
598a42c0
FW
280
281 fprintf(fp, ":%d -> ", ntohs(map->old_sport));
656111b2 282 xfrm_print_addr(fp, map->id.family, &map->new_saddr);
598a42c0
FW
283 fprintf(fp, ":%d\n\t", ntohs(map->new_sport));
284
285 xfrm_usersa_print(&map->id, map->reqid, fp);
286
287 fprintf(fp, "\n");
288 fflush(fp);
289 return 0;
290}
291
f9cb3a2f 292static int xfrm_accept_msg(const struct sockaddr_nl *who,
0628cddd 293 struct rtnl_ctrl_data *ctrl,
f9cb3a2f 294 struct nlmsghdr *n, void *arg)
295{
296 FILE *fp = (FILE*)arg;
297
90f93024
SH
298 if (timestamp)
299 print_timestamp(fp);
300
efe69c1b
MN
301 switch (n->nlmsg_type) {
302 case XFRM_MSG_NEWSA:
303 case XFRM_MSG_DELSA:
304 case XFRM_MSG_UPDSA:
305 case XFRM_MSG_EXPIRE:
c595c790
SH
306 xfrm_state_print(who, n, arg);
307 return 0;
efe69c1b
MN
308 case XFRM_MSG_NEWPOLICY:
309 case XFRM_MSG_DELPOLICY:
310 case XFRM_MSG_UPDPOLICY:
311 case XFRM_MSG_POLEXPIRE:
c595c790
SH
312 xfrm_policy_print(who, n, arg);
313 return 0;
efe69c1b 314 case XFRM_MSG_ACQUIRE:
f9cb3a2f 315 xfrm_acquire_print(who, n, arg);
316 return 0;
efe69c1b
MN
317 case XFRM_MSG_FLUSHSA:
318 xfrm_state_flush_print(who, n, arg);
f9cb3a2f 319 return 0;
efe69c1b
MN
320 case XFRM_MSG_FLUSHPOLICY:
321 xfrm_policy_flush_print(who, n, arg);
f9cb3a2f 322 return 0;
efe69c1b 323 case XFRM_MSG_REPORT:
c54f31ee
MN
324 xfrm_report_print(who, n, arg);
325 return 0;
efe69c1b 326 case XFRM_MSG_NEWAE:
c9fd974b 327 xfrm_ae_print(who, n, arg);
328 return 0;
598a42c0
FW
329 case XFRM_MSG_MAPPING:
330 xfrm_mapping_print(who, n, arg);
331 return 0;
efe69c1b
MN
332 default:
333 break;
c9fd974b 334 }
efe69c1b 335
f9cb3a2f 336 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
337 n->nlmsg_type != NLMSG_DONE) {
c595c790 338 fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n",
f9cb3a2f 339 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
340 }
341 return 0;
342}
343
77219712
SH
344extern struct rtnl_handle rth;
345
f9cb3a2f 346int do_xfrm_monitor(int argc, char **argv)
347{
f9cb3a2f 348 char *file = NULL;
349 unsigned groups = ~((unsigned)0); /* XXX */
350 int lacquire=0;
351 int lexpire=0;
c9fd974b 352 int laevent=0;
c595c790
SH
353 int lpolicy=0;
354 int lsa=0;
c54f31ee 355 int lreport=0;
f9cb3a2f 356
77219712
SH
357 rtnl_close(&rth);
358
f9cb3a2f 359 while (argc > 0) {
360 if (matches(*argv, "file") == 0) {
361 NEXT_ARG();
362 file = *argv;
363 } else if (matches(*argv, "acquire") == 0) {
364 lacquire=1;
365 groups = 0;
366 } else if (matches(*argv, "expire") == 0) {
367 lexpire=1;
368 groups = 0;
c595c790
SH
369 } else if (matches(*argv, "SA") == 0) {
370 lsa=1;
371 groups = 0;
c9fd974b 372 } else if (matches(*argv, "aevent") == 0) {
373 laevent=1;
374 groups = 0;
c595c790
SH
375 } else if (matches(*argv, "policy") == 0) {
376 lpolicy=1;
377 groups = 0;
c54f31ee
MN
378 } else if (matches(*argv, "report") == 0) {
379 lreport=1;
380 groups = 0;
f9cb3a2f 381 } else if (matches(*argv, "help") == 0) {
382 usage();
5bf9f5c5 383 } else if (strcmp(*argv, "all")) {
f9cb3a2f 384 fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv);
385 exit(-1);
386 }
387 argc--; argv++;
388 }
389
390 if (lacquire)
7b822512 391 groups |= nl_mgrp(XFRMNLGRP_ACQUIRE);
f9cb3a2f 392 if (lexpire)
7b822512 393 groups |= nl_mgrp(XFRMNLGRP_EXPIRE);
c595c790 394 if (lsa)
7b822512 395 groups |= nl_mgrp(XFRMNLGRP_SA);
c595c790 396 if (lpolicy)
7b822512 397 groups |= nl_mgrp(XFRMNLGRP_POLICY);
c9fd974b 398 if (laevent)
7b822512 399 groups |= nl_mgrp(XFRMNLGRP_AEVENTS);
c54f31ee 400 if (lreport)
7b822512 401 groups |= nl_mgrp(XFRMNLGRP_REPORT);
f9cb3a2f 402
403 if (file) {
404 FILE *fp;
405 fp = fopen(file, "r");
406 if (fp == NULL) {
407 perror("Cannot fopen");
408 exit(-1);
409 }
410 return rtnl_from_file(fp, xfrm_accept_msg, (void*)stdout);
411 }
412
c595c790
SH
413 if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0)
414 exit(1);
415
f9cb3a2f 416 if (rtnl_listen(&rth, xfrm_accept_msg, (void*)stdout) < 0)
417 exit(2);
418
351efcde 419 return 0;
f9cb3a2f 420}