blob: 79453e41d726ced6ef5ff1f1dfdee76778631788 [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
Stephen Hemminger4d98ab02013-12-06 15:05:07 -080017 * along with this program; if not, see <http://www.gnu.org/licenses>.
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +000018 */
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 <linux/xfrm.h>
31#include "utils.h"
32#include "xfrm.h"
33#include "ip_common.h"
34
35static void usage(void) __attribute__((noreturn));
36
37static void usage(void)
38{
David Wardcbec0212011-06-11 16:13:30 +000039 fprintf(stderr, "Usage: ip xfrm monitor [ all | LISTofXFRM-OBJECTS ]\n");
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +000040 exit(-1);
41}
42
43static int xfrm_acquire_print(const struct sockaddr_nl *who,
44 struct nlmsghdr *n, void *arg)
45{
46 FILE *fp = (FILE*)arg;
47 struct xfrm_user_acquire *xacq = NLMSG_DATA(n);
48 int len = n->nlmsg_len;
49 struct rtattr * tb[XFRMA_MAX+1];
50 __u16 family;
51
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +000052 len -= NLMSG_LENGTH(sizeof(*xacq));
53 if (len < 0) {
54 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
55 return -1;
56 }
57
58 parse_rtattr(tb, XFRMA_MAX, XFRMACQ_RTA(xacq), len);
shemminger90f93022005-06-07 21:55:55 +000059
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +000060 family = xacq->sel.family;
61 if (family == AF_UNSPEC)
62 family = xacq->policy.sel.family;
63 if (family == AF_UNSPEC)
64 family = preferred_family;
65
66 fprintf(fp, "acquire ");
67
68 fprintf(fp, "proto %s ", strxf_xfrmproto(xacq->id.proto));
69 if (show_stats > 0 || xacq->id.spi) {
70 __u32 spi = ntohl(xacq->id.spi);
71 fprintf(fp, "spi 0x%08x", spi);
72 if (show_stats > 0)
73 fprintf(fp, "(%u)", spi);
74 fprintf(fp, " ");
75 }
76 fprintf(fp, "%s", _SL_);
77
78 xfrm_selector_print(&xacq->sel, family, fp, " sel ");
79
80 xfrm_policy_info_print(&xacq->policy, tb, fp, " ", " policy ");
81
82 if (show_stats > 0)
83 fprintf(fp, " seq 0x%08u ", xacq->seq);
84 if (show_stats > 0) {
85 fprintf(fp, "%s-mask %s ",
86 strxf_algotype(XFRMA_ALG_CRYPT),
87 strxf_mask32(xacq->ealgos));
88 fprintf(fp, "%s-mask %s ",
89 strxf_algotype(XFRMA_ALG_AUTH),
90 strxf_mask32(xacq->aalgos));
91 fprintf(fp, "%s-mask %s",
92 strxf_algotype(XFRMA_ALG_COMP),
93 strxf_mask32(xacq->calgos));
94 }
95 fprintf(fp, "%s", _SL_);
96
97 if (oneline)
98 fprintf(fp, "\n");
shemminger669ae742005-11-07 18:39:30 +000099 fflush(fp);
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000100
101 return 0;
102}
103
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900104static int xfrm_state_flush_print(const struct sockaddr_nl *who,
105 struct nlmsghdr *n, void *arg)
106{
107 FILE *fp = (FILE*)arg;
108 struct xfrm_usersa_flush *xsf = NLMSG_DATA(n);
109 int len = n->nlmsg_len;
110 const char *str;
111
112 len -= NLMSG_SPACE(sizeof(*xsf));
113 if (len < 0) {
114 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
115 return -1;
116 }
117
118 fprintf(fp, "Flushed state ");
119
120 str = strxf_xfrmproto(xsf->proto);
121 if (str)
122 fprintf(fp, "proto %s", str);
123 else
124 fprintf(fp, "proto %u", xsf->proto);
125 fprintf(fp, "%s", _SL_);
126
127 if (oneline)
128 fprintf(fp, "\n");
129 fflush(fp);
130
131 return 0;
132}
133
134static int xfrm_policy_flush_print(const struct sockaddr_nl *who,
135 struct nlmsghdr *n, void *arg)
136{
137 struct rtattr * tb[XFRMA_MAX+1];
138 FILE *fp = (FILE*)arg;
139 int len = n->nlmsg_len;
140
141 len -= NLMSG_SPACE(0);
142 if (len < 0) {
143 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
144 return -1;
145 }
146
147 fprintf(fp, "Flushed policy ");
148
149 parse_rtattr(tb, XFRMA_MAX, NLMSG_DATA(n), len);
150
151 if (tb[XFRMA_POLICY_TYPE]) {
152 struct xfrm_userpolicy_type *upt;
153
154 fprintf(fp, "ptype ");
155
156 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
157 fprintf(fp, "(ERROR truncated)");
158
159 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
160 fprintf(fp, "%s ", strxf_ptype(upt->type));
161 }
162
163 fprintf(fp, "%s", _SL_);
164
165 if (oneline)
166 fprintf(fp, "\n");
167 fflush(fp);
168
169 return 0;
170}
171
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900172static int xfrm_report_print(const struct sockaddr_nl *who,
173 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
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900181 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
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800208static void xfrm_ae_flags_print(__u32 flags, void *arg)
jamalc9fd9742006-12-07 20:31:14 -0500209{
210 FILE *fp = (FILE*)arg;
211 fprintf(fp, " (0x%x) ", flags);
212 if (!flags)
213 return;
214 if (flags & XFRM_AE_CR)
215 fprintf(fp, " replay update ");
216 if (flags & XFRM_AE_CE)
217 fprintf(fp, " timer expired ");
218 if (flags & XFRM_AE_CU)
219 fprintf(fp, " policy updated ");
220
221}
222
Florian Westphal598a42c2012-03-13 12:35:25 +0000223static void xfrm_usersa_print(const struct xfrm_usersa_id *sa_id, __u32 reqid, FILE *fp)
224{
225 char buf[256];
226
227 buf[0] = 0;
Stephen Hemminger656111b2014-08-04 10:30:35 -0700228 fprintf(fp, "dst %s ",
229 rt_addr_n2a(sa_id->family, &sa_id->daddr, buf, sizeof(buf)));
Florian Westphal598a42c2012-03-13 12:35:25 +0000230
231 fprintf(fp, " reqid 0x%x", reqid);
232
233 fprintf(fp, " protocol %s ", strxf_proto(sa_id->proto));
234 fprintf(fp, " SPI 0x%x", ntohl(sa_id->spi));
235}
236
jamalc9fd9742006-12-07 20:31:14 -0500237static int xfrm_ae_print(const struct sockaddr_nl *who,
238 struct nlmsghdr *n, void *arg)
239{
240 FILE *fp = (FILE*)arg;
241 struct xfrm_aevent_id *id = NLMSG_DATA(n);
242 char abuf[256];
243
244 fprintf(fp, "Async event ");
245 xfrm_ae_flags_print(id->flags, arg);
246 fprintf(fp,"\n\t");
247 memset(abuf, '\0', sizeof(abuf));
Stephen Hemminger656111b2014-08-04 10:30:35 -0700248 fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family, &id->saddr,
249 abuf, sizeof(abuf)));
Florian Westphal598a42c2012-03-13 12:35:25 +0000250
251 xfrm_usersa_print(&id->sa_id, id->reqid, fp);
jamalc9fd9742006-12-07 20:31:14 -0500252
253 fprintf(fp, "\n");
254 fflush(fp);
255
256 return 0;
257}
258
Stephen Hemminger656111b2014-08-04 10:30:35 -0700259static void xfrm_print_addr(FILE *fp, int family, xfrm_address_t *a)
Florian Westphal598a42c2012-03-13 12:35:25 +0000260{
261 char buf[256];
262
263 buf[0] = 0;
Stephen Hemminger656111b2014-08-04 10:30:35 -0700264 fprintf(fp, "%s", rt_addr_n2a(family, a, buf, sizeof(buf)));
Florian Westphal598a42c2012-03-13 12:35:25 +0000265}
266
267static int xfrm_mapping_print(const struct sockaddr_nl *who,
268 struct nlmsghdr *n, void *arg)
269{
270 FILE *fp = (FILE*)arg;
271 struct xfrm_user_mapping *map = NLMSG_DATA(n);
272
273 fprintf(fp, "Mapping change ");
Stephen Hemminger656111b2014-08-04 10:30:35 -0700274 xfrm_print_addr(fp, map->id.family, &map->old_saddr);
Florian Westphal598a42c2012-03-13 12:35:25 +0000275
276 fprintf(fp, ":%d -> ", ntohs(map->old_sport));
Stephen Hemminger656111b2014-08-04 10:30:35 -0700277 xfrm_print_addr(fp, map->id.family, &map->new_saddr);
Florian Westphal598a42c2012-03-13 12:35:25 +0000278 fprintf(fp, ":%d\n\t", ntohs(map->new_sport));
279
280 xfrm_usersa_print(&map->id, map->reqid, fp);
281
282 fprintf(fp, "\n");
283 fflush(fp);
284 return 0;
285}
286
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000287static int xfrm_accept_msg(const struct sockaddr_nl *who,
288 struct nlmsghdr *n, void *arg)
289{
290 FILE *fp = (FILE*)arg;
291
shemminger90f93022005-06-07 21:55:55 +0000292 if (timestamp)
293 print_timestamp(fp);
294
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900295 switch (n->nlmsg_type) {
296 case XFRM_MSG_NEWSA:
297 case XFRM_MSG_DELSA:
298 case XFRM_MSG_UPDSA:
299 case XFRM_MSG_EXPIRE:
shemmingerc595c792005-11-01 23:03:03 +0000300 xfrm_state_print(who, n, arg);
301 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900302 case XFRM_MSG_NEWPOLICY:
303 case XFRM_MSG_DELPOLICY:
304 case XFRM_MSG_UPDPOLICY:
305 case XFRM_MSG_POLEXPIRE:
shemmingerc595c792005-11-01 23:03:03 +0000306 xfrm_policy_print(who, n, arg);
307 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900308 case XFRM_MSG_ACQUIRE:
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000309 xfrm_acquire_print(who, n, arg);
310 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900311 case XFRM_MSG_FLUSHSA:
312 xfrm_state_flush_print(who, n, arg);
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000313 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900314 case XFRM_MSG_FLUSHPOLICY:
315 xfrm_policy_flush_print(who, n, arg);
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000316 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900317 case XFRM_MSG_REPORT:
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900318 xfrm_report_print(who, n, arg);
319 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900320 case XFRM_MSG_NEWAE:
jamalc9fd9742006-12-07 20:31:14 -0500321 xfrm_ae_print(who, n, arg);
322 return 0;
Florian Westphal598a42c2012-03-13 12:35:25 +0000323 case XFRM_MSG_MAPPING:
324 xfrm_mapping_print(who, n, arg);
325 return 0;
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900326 default:
327 break;
jamalc9fd9742006-12-07 20:31:14 -0500328 }
Masahide NAKAMURAefe69c12007-08-24 11:05:24 +0900329
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000330 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
331 n->nlmsg_type != NLMSG_DONE) {
shemmingerc595c792005-11-01 23:03:03 +0000332 fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n",
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000333 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
334 }
335 return 0;
336}
337
Stephen Hemminger77219712006-09-25 17:27:37 -0700338extern struct rtnl_handle rth;
339
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000340int do_xfrm_monitor(int argc, char **argv)
341{
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000342 char *file = NULL;
343 unsigned groups = ~((unsigned)0); /* XXX */
344 int lacquire=0;
345 int lexpire=0;
jamalc9fd9742006-12-07 20:31:14 -0500346 int laevent=0;
shemmingerc595c792005-11-01 23:03:03 +0000347 int lpolicy=0;
348 int lsa=0;
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900349 int lreport=0;
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000350
Stephen Hemminger77219712006-09-25 17:27:37 -0700351 rtnl_close(&rth);
352
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000353 while (argc > 0) {
354 if (matches(*argv, "file") == 0) {
355 NEXT_ARG();
356 file = *argv;
357 } else if (matches(*argv, "acquire") == 0) {
358 lacquire=1;
359 groups = 0;
360 } else if (matches(*argv, "expire") == 0) {
361 lexpire=1;
362 groups = 0;
shemmingerc595c792005-11-01 23:03:03 +0000363 } else if (matches(*argv, "SA") == 0) {
364 lsa=1;
365 groups = 0;
jamalc9fd9742006-12-07 20:31:14 -0500366 } else if (matches(*argv, "aevent") == 0) {
367 laevent=1;
368 groups = 0;
shemmingerc595c792005-11-01 23:03:03 +0000369 } else if (matches(*argv, "policy") == 0) {
370 lpolicy=1;
371 groups = 0;
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900372 } else if (matches(*argv, "report") == 0) {
373 lreport=1;
374 groups = 0;
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000375 } else if (matches(*argv, "help") == 0) {
376 usage();
377 } else {
378 fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv);
379 exit(-1);
380 }
381 argc--; argv++;
382 }
383
384 if (lacquire)
jamal7b822512006-12-07 20:58:23 -0500385 groups |= nl_mgrp(XFRMNLGRP_ACQUIRE);
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000386 if (lexpire)
jamal7b822512006-12-07 20:58:23 -0500387 groups |= nl_mgrp(XFRMNLGRP_EXPIRE);
shemmingerc595c792005-11-01 23:03:03 +0000388 if (lsa)
jamal7b822512006-12-07 20:58:23 -0500389 groups |= nl_mgrp(XFRMNLGRP_SA);
shemmingerc595c792005-11-01 23:03:03 +0000390 if (lpolicy)
jamal7b822512006-12-07 20:58:23 -0500391 groups |= nl_mgrp(XFRMNLGRP_POLICY);
jamalc9fd9742006-12-07 20:31:14 -0500392 if (laevent)
jamal7b822512006-12-07 20:58:23 -0500393 groups |= nl_mgrp(XFRMNLGRP_AEVENTS);
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900394 if (lreport)
jamal7b822512006-12-07 20:58:23 -0500395 groups |= nl_mgrp(XFRMNLGRP_REPORT);
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000396
397 if (file) {
398 FILE *fp;
399 fp = fopen(file, "r");
400 if (fp == NULL) {
401 perror("Cannot fopen");
402 exit(-1);
403 }
404 return rtnl_from_file(fp, xfrm_accept_msg, (void*)stdout);
405 }
406
shemmingerc595c792005-11-01 23:03:03 +0000407 if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0)
408 exit(1);
409
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000410 if (rtnl_listen(&rth, xfrm_accept_msg, (void*)stdout) < 0)
411 exit(2);
412
shemminger351efcd2005-09-01 19:21:50 +0000413 return 0;
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000414}