blob: ea17987d6849f9ff8f19dc1501acc0144d1e77db [file] [log] [blame]
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +00001/* $USAGI: $ */
2
3/*
4 * Copyright (C)2005 USAGI/WIDE Project
Stephen Hemmingerae665a52006-12-05 10:10:22 -08005 *
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +00006 * 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.
Stephen Hemmingerae665a52006-12-05 10:10:22 -080010 *
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +000011 * 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.
Stephen Hemmingerae665a52006-12-05 10:10:22 -080015 *
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +000016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20/*
21 * based on ipmonitor.c
22 */
23/*
24 * Authors:
25 * Masahide NAKAMURA @USAGI
26 */
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <linux/xfrm.h>
32#include "utils.h"
33#include "xfrm.h"
34#include "ip_common.h"
35
36static void usage(void) __attribute__((noreturn));
37
38static void usage(void)
39{
David Wardcbec0212011-06-11 16:13:30 +000040 fprintf(stderr, "Usage: ip xfrm monitor [ all | LISTofXFRM-OBJECTS ]\n");
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +000041 exit(-1);
42}
43
44static int xfrm_acquire_print(const struct sockaddr_nl *who,
45 struct nlmsghdr *n, void *arg)
46{
47 FILE *fp = (FILE*)arg;
48 struct xfrm_user_acquire *xacq = NLMSG_DATA(n);
49 int len = n->nlmsg_len;
50 struct rtattr * tb[XFRMA_MAX+1];
51 __u16 family;
52
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +000053 len -= NLMSG_LENGTH(sizeof(*xacq));
54 if (len < 0) {
55 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
56 return -1;
57 }
58
59 parse_rtattr(tb, XFRMA_MAX, XFRMACQ_RTA(xacq), len);
shemminger90f93022005-06-07 21:55:55 +000060
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +000061 family = xacq->sel.family;
62 if (family == AF_UNSPEC)
63 family = xacq->policy.sel.family;
64 if (family == AF_UNSPEC)
65 family = preferred_family;
66
67 fprintf(fp, "acquire ");
68
69 fprintf(fp, "proto %s ", strxf_xfrmproto(xacq->id.proto));
70 if (show_stats > 0 || xacq->id.spi) {
71 __u32 spi = ntohl(xacq->id.spi);
72 fprintf(fp, "spi 0x%08x", spi);
73 if (show_stats > 0)
74 fprintf(fp, "(%u)", spi);
75 fprintf(fp, " ");
76 }
77 fprintf(fp, "%s", _SL_);
78
79 xfrm_selector_print(&xacq->sel, family, fp, " sel ");
80
81 xfrm_policy_info_print(&xacq->policy, tb, fp, " ", " policy ");
82
83 if (show_stats > 0)
84 fprintf(fp, " seq 0x%08u ", xacq->seq);
85 if (show_stats > 0) {
86 fprintf(fp, "%s-mask %s ",
87 strxf_algotype(XFRMA_ALG_CRYPT),
88 strxf_mask32(xacq->ealgos));
89 fprintf(fp, "%s-mask %s ",
90 strxf_algotype(XFRMA_ALG_AUTH),
91 strxf_mask32(xacq->aalgos));
92 fprintf(fp, "%s-mask %s",
93 strxf_algotype(XFRMA_ALG_COMP),
94 strxf_mask32(xacq->calgos));
95 }
96 fprintf(fp, "%s", _SL_);
97
98 if (oneline)
99 fprintf(fp, "\n");
shemminger669ae742005-11-07 18:39:30 +0000100 fflush(fp);
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000101
102 return 0;
103}
104
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900105static int xfrm_state_flush_print(const struct sockaddr_nl *who,
106 struct nlmsghdr *n, void *arg)
107{
108 FILE *fp = (FILE*)arg;
109 struct xfrm_usersa_flush *xsf = NLMSG_DATA(n);
110 int len = n->nlmsg_len;
111 const char *str;
112
113 len -= NLMSG_SPACE(sizeof(*xsf));
114 if (len < 0) {
115 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
116 return -1;
117 }
118
119 fprintf(fp, "Flushed state ");
120
121 str = strxf_xfrmproto(xsf->proto);
122 if (str)
123 fprintf(fp, "proto %s", str);
124 else
125 fprintf(fp, "proto %u", xsf->proto);
126 fprintf(fp, "%s", _SL_);
127
128 if (oneline)
129 fprintf(fp, "\n");
130 fflush(fp);
131
132 return 0;
133}
134
135static int xfrm_policy_flush_print(const struct sockaddr_nl *who,
136 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 = (struct xfrm_userpolicy_type *)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
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900173static int xfrm_report_print(const struct sockaddr_nl *who,
174 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
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900182 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, " ");
202
203 if (oneline)
204 fprintf(fp, "\n");
205
206 return 0;
207}
208
jamalc9fd9742006-12-07 20:31:14 -0500209void xfrm_ae_flags_print(__u32 flags, void *arg)
210{
211 FILE *fp = (FILE*)arg;
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
Florian Westphal598a42c2012-03-13 12:35:25 +0000224static void xfrm_usersa_print(const struct xfrm_usersa_id *sa_id, __u32 reqid, FILE *fp)
225{
226 char buf[256];
227
228 buf[0] = 0;
229 fprintf(fp, "dst %s ", rt_addr_n2a(sa_id->family,
230 sizeof(sa_id->daddr), &sa_id->daddr, buf, sizeof(buf)));
231
232 fprintf(fp, " reqid 0x%x", reqid);
233
234 fprintf(fp, " protocol %s ", strxf_proto(sa_id->proto));
235 fprintf(fp, " SPI 0x%x", ntohl(sa_id->spi));
236}
237
jamalc9fd9742006-12-07 20:31:14 -0500238static int xfrm_ae_print(const struct sockaddr_nl *who,
239 struct nlmsghdr *n, void *arg)
240{
241 FILE *fp = (FILE*)arg;
242 struct xfrm_aevent_id *id = NLMSG_DATA(n);
243 char abuf[256];
244
245 fprintf(fp, "Async event ");
246 xfrm_ae_flags_print(id->flags, arg);
247 fprintf(fp,"\n\t");
248 memset(abuf, '\0', sizeof(abuf));
Stephen Hemminger81c61792006-12-13 17:05:50 -0800249 fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family,
250 sizeof(id->saddr), &id->saddr,
jamalc9fd9742006-12-07 20:31:14 -0500251 abuf, sizeof(abuf)));
Florian Westphal598a42c2012-03-13 12:35:25 +0000252
253 xfrm_usersa_print(&id->sa_id, id->reqid, fp);
jamalc9fd9742006-12-07 20:31:14 -0500254
255 fprintf(fp, "\n");
256 fflush(fp);
257
258 return 0;
259}
260
Florian Westphal598a42c2012-03-13 12:35:25 +0000261static void xfrm_print_addr(FILE *fp, int family, xfrm_address_t *a, size_t s)
262{
263 char buf[256];
264
265 buf[0] = 0;
266 fprintf(fp, "%s", rt_addr_n2a(family, s, a, buf, sizeof(buf)));
267}
268
269static int xfrm_mapping_print(const struct sockaddr_nl *who,
270 struct nlmsghdr *n, void *arg)
271{
272 FILE *fp = (FILE*)arg;
273 struct xfrm_user_mapping *map = NLMSG_DATA(n);
274
275 fprintf(fp, "Mapping change ");
276 xfrm_print_addr(fp, map->id.family, &map->old_saddr,
277 sizeof(map->old_saddr));
278
279 fprintf(fp, ":%d -> ", ntohs(map->old_sport));
280 xfrm_print_addr(fp, map->id.family, &map->new_saddr,
281 sizeof(map->new_saddr));
282 fprintf(fp, ":%d\n\t", ntohs(map->new_sport));
283
284 xfrm_usersa_print(&map->id, map->reqid, fp);
285
286 fprintf(fp, "\n");
287 fflush(fp);
288 return 0;
289}
290
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000291static int xfrm_accept_msg(const struct sockaddr_nl *who,
292 struct nlmsghdr *n, void *arg)
293{
294 FILE *fp = (FILE*)arg;
295
shemminger90f93022005-06-07 21:55:55 +0000296 if (timestamp)
297 print_timestamp(fp);
298
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900299 switch (n->nlmsg_type) {
300 case XFRM_MSG_NEWSA:
301 case XFRM_MSG_DELSA:
302 case XFRM_MSG_UPDSA:
303 case XFRM_MSG_EXPIRE:
shemmingerc595c792005-11-01 23:03:03 +0000304 xfrm_state_print(who, n, arg);
305 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900306 case XFRM_MSG_NEWPOLICY:
307 case XFRM_MSG_DELPOLICY:
308 case XFRM_MSG_UPDPOLICY:
309 case XFRM_MSG_POLEXPIRE:
shemmingerc595c792005-11-01 23:03:03 +0000310 xfrm_policy_print(who, n, arg);
311 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900312 case XFRM_MSG_ACQUIRE:
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000313 xfrm_acquire_print(who, n, arg);
314 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900315 case XFRM_MSG_FLUSHSA:
316 xfrm_state_flush_print(who, n, arg);
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000317 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900318 case XFRM_MSG_FLUSHPOLICY:
319 xfrm_policy_flush_print(who, n, arg);
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000320 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900321 case XFRM_MSG_REPORT:
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900322 xfrm_report_print(who, n, arg);
323 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900324 case XFRM_MSG_NEWAE:
jamalc9fd9742006-12-07 20:31:14 -0500325 xfrm_ae_print(who, n, arg);
326 return 0;
Florian Westphal598a42c2012-03-13 12:35:25 +0000327 case XFRM_MSG_MAPPING:
328 xfrm_mapping_print(who, n, arg);
329 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900330 default:
331 break;
jamalc9fd9742006-12-07 20:31:14 -0500332 }
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900333
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000334 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
335 n->nlmsg_type != NLMSG_DONE) {
shemmingerc595c792005-11-01 23:03:03 +0000336 fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n",
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000337 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
338 }
339 return 0;
340}
341
Stephen Hemminger77219712006-09-25 17:27:37 -0700342extern struct rtnl_handle rth;
343
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000344int do_xfrm_monitor(int argc, char **argv)
345{
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000346 char *file = NULL;
347 unsigned groups = ~((unsigned)0); /* XXX */
348 int lacquire=0;
349 int lexpire=0;
jamalc9fd9742006-12-07 20:31:14 -0500350 int laevent=0;
shemmingerc595c792005-11-01 23:03:03 +0000351 int lpolicy=0;
352 int lsa=0;
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900353 int lreport=0;
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000354
Stephen Hemminger77219712006-09-25 17:27:37 -0700355 rtnl_close(&rth);
356
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000357 while (argc > 0) {
358 if (matches(*argv, "file") == 0) {
359 NEXT_ARG();
360 file = *argv;
361 } else if (matches(*argv, "acquire") == 0) {
362 lacquire=1;
363 groups = 0;
364 } else if (matches(*argv, "expire") == 0) {
365 lexpire=1;
366 groups = 0;
shemmingerc595c792005-11-01 23:03:03 +0000367 } else if (matches(*argv, "SA") == 0) {
368 lsa=1;
369 groups = 0;
jamalc9fd9742006-12-07 20:31:14 -0500370 } else if (matches(*argv, "aevent") == 0) {
371 laevent=1;
372 groups = 0;
shemmingerc595c792005-11-01 23:03:03 +0000373 } else if (matches(*argv, "policy") == 0) {
374 lpolicy=1;
375 groups = 0;
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900376 } else if (matches(*argv, "report") == 0) {
377 lreport=1;
378 groups = 0;
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000379 } else if (matches(*argv, "help") == 0) {
380 usage();
381 } else {
382 fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv);
383 exit(-1);
384 }
385 argc--; argv++;
386 }
387
388 if (lacquire)
jamal7b822512006-12-07 20:58:23 -0500389 groups |= nl_mgrp(XFRMNLGRP_ACQUIRE);
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000390 if (lexpire)
jamal7b822512006-12-07 20:58:23 -0500391 groups |= nl_mgrp(XFRMNLGRP_EXPIRE);
shemmingerc595c792005-11-01 23:03:03 +0000392 if (lsa)
jamal7b822512006-12-07 20:58:23 -0500393 groups |= nl_mgrp(XFRMNLGRP_SA);
shemmingerc595c792005-11-01 23:03:03 +0000394 if (lpolicy)
jamal7b822512006-12-07 20:58:23 -0500395 groups |= nl_mgrp(XFRMNLGRP_POLICY);
jamalc9fd9742006-12-07 20:31:14 -0500396 if (laevent)
jamal7b822512006-12-07 20:58:23 -0500397 groups |= nl_mgrp(XFRMNLGRP_AEVENTS);
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900398 if (lreport)
jamal7b822512006-12-07 20:58:23 -0500399 groups |= nl_mgrp(XFRMNLGRP_REPORT);
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000400
401 if (file) {
402 FILE *fp;
403 fp = fopen(file, "r");
404 if (fp == NULL) {
405 perror("Cannot fopen");
406 exit(-1);
407 }
408 return rtnl_from_file(fp, xfrm_accept_msg, (void*)stdout);
409 }
410
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000411 //ll_init_map(&rth);
412
shemmingerc595c792005-11-01 23:03:03 +0000413 if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0)
414 exit(1);
415
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000416 if (rtnl_listen(&rth, xfrm_accept_msg, (void*)stdout) < 0)
417 exit(2);
418
shemminger351efcd2005-09-01 19:21:50 +0000419 return 0;
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000420}