4 * Copyright (C)2005 USAGI/WIDE Project
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.
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.
16 * 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
21 * based on ipmonitor.c
25 * Masahide NAKAMURA @USAGI
31 #include <linux/xfrm.h>
34 #include "ip_common.h"
36 static void usage(void) __attribute__((noreturn));
38 static void usage(void)
40 fprintf(stderr, "Usage: ip xfrm monitor [ all | LISTofOBJECTS ]\n");
44 static int xfrm_acquire_print(const struct sockaddr_nl *who,
45 struct nlmsghdr *n, void *arg)
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];
53 len -= NLMSG_LENGTH(sizeof(*xacq));
55 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
59 parse_rtattr(tb, XFRMA_MAX, XFRMACQ_RTA(xacq), len);
61 family = xacq->sel.family;
62 if (family == AF_UNSPEC)
63 family = xacq->policy.sel.family;
64 if (family == AF_UNSPEC)
65 family = preferred_family;
67 fprintf(fp, "acquire ");
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);
74 fprintf(fp, "(%u)", spi);
77 fprintf(fp, "%s", _SL_);
79 xfrm_selector_print(&xacq->sel, family, fp, " sel ");
81 xfrm_policy_info_print(&xacq->policy, tb, fp, " ", " policy ");
84 fprintf(fp, " seq 0x%08u ", xacq->seq);
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));
96 fprintf(fp, "%s", _SL_);
105 static int xfrm_state_flush_print(const struct sockaddr_nl *who,
106 struct nlmsghdr *n, void *arg)
108 FILE *fp = (FILE*)arg;
109 struct xfrm_usersa_flush *xsf = NLMSG_DATA(n);
110 int len = n->nlmsg_len;
113 len -= NLMSG_SPACE(sizeof(*xsf));
115 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
119 fprintf(fp, "Flushed state ");
121 str = strxf_xfrmproto(xsf->proto);
123 fprintf(fp, "proto %s", str);
125 fprintf(fp, "proto %u", xsf->proto);
126 fprintf(fp, "%s", _SL_);
135 static int xfrm_policy_flush_print(const struct sockaddr_nl *who,
136 struct nlmsghdr *n, void *arg)
138 struct rtattr * tb[XFRMA_MAX+1];
139 FILE *fp = (FILE*)arg;
140 int len = n->nlmsg_len;
142 len -= NLMSG_SPACE(0);
144 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
148 fprintf(fp, "Flushed policy ");
150 parse_rtattr(tb, XFRMA_MAX, NLMSG_DATA(n), len);
152 if (tb[XFRMA_POLICY_TYPE]) {
153 struct xfrm_userpolicy_type *upt;
155 fprintf(fp, "ptype ");
157 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
158 fprintf(fp, "(ERROR truncated)");
160 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
161 fprintf(fp, "%s ", strxf_ptype(upt->type));
164 fprintf(fp, "%s", _SL_);
173 static int xfrm_report_print(const struct sockaddr_nl *who,
174 struct nlmsghdr *n, void *arg)
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];
182 len -= NLMSG_LENGTH(sizeof(*xrep));
184 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
188 family = xrep->sel.family;
189 if (family == AF_UNSPEC)
190 family = preferred_family;
192 fprintf(fp, "report ");
194 fprintf(fp, "proto %s ", strxf_xfrmproto(xrep->proto));
195 fprintf(fp, "%s", _SL_);
197 xfrm_selector_print(&xrep->sel, family, fp, " sel ");
199 parse_rtattr(tb, XFRMA_MAX, XFRMREP_RTA(xrep), len);
201 xfrm_xfrma_print(tb, family, fp, " ");
209 void xfrm_ae_flags_print(__u32 flags, void *arg)
211 FILE *fp = (FILE*)arg;
212 fprintf(fp, " (0x%x) ", flags);
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 ");
224 static int xfrm_ae_print(const struct sockaddr_nl *who,
225 struct nlmsghdr *n, void *arg)
227 FILE *fp = (FILE*)arg;
228 struct xfrm_aevent_id *id = NLMSG_DATA(n);
231 fprintf(fp, "Async event ");
232 xfrm_ae_flags_print(id->flags, arg);
234 memset(abuf, '\0', sizeof(abuf));
235 fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family,
236 sizeof(id->saddr), &id->saddr,
237 abuf, sizeof(abuf)));
238 memset(abuf, '\0', sizeof(abuf));
239 fprintf(fp, "dst %s ", rt_addr_n2a(id->sa_id.family,
240 sizeof(id->sa_id.daddr), &id->sa_id.daddr,
241 abuf, sizeof(abuf)));
242 fprintf(fp, " reqid 0x%x", id->reqid);
243 fprintf(fp, " protocol %s ", strxf_proto(id->sa_id.proto));
244 fprintf(fp, " SPI 0x%x", ntohl(id->sa_id.spi));
252 static int xfrm_accept_msg(const struct sockaddr_nl *who,
253 struct nlmsghdr *n, void *arg)
255 FILE *fp = (FILE*)arg;
260 switch (n->nlmsg_type) {
264 case XFRM_MSG_EXPIRE:
265 xfrm_state_print(who, n, arg);
267 case XFRM_MSG_NEWPOLICY:
268 case XFRM_MSG_DELPOLICY:
269 case XFRM_MSG_UPDPOLICY:
270 case XFRM_MSG_POLEXPIRE:
271 xfrm_policy_print(who, n, arg);
273 case XFRM_MSG_ACQUIRE:
274 xfrm_acquire_print(who, n, arg);
276 case XFRM_MSG_FLUSHSA:
277 xfrm_state_flush_print(who, n, arg);
279 case XFRM_MSG_FLUSHPOLICY:
280 xfrm_policy_flush_print(who, n, arg);
282 case XFRM_MSG_REPORT:
283 xfrm_report_print(who, n, arg);
286 xfrm_ae_print(who, n, arg);
292 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
293 n->nlmsg_type != NLMSG_DONE) {
294 fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n",
295 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
300 extern struct rtnl_handle rth;
302 int do_xfrm_monitor(int argc, char **argv)
305 unsigned groups = ~((unsigned)0); /* XXX */
316 if (matches(*argv, "file") == 0) {
319 } else if (matches(*argv, "acquire") == 0) {
322 } else if (matches(*argv, "expire") == 0) {
325 } else if (matches(*argv, "SA") == 0) {
328 } else if (matches(*argv, "aevent") == 0) {
331 } else if (matches(*argv, "policy") == 0) {
334 } else if (matches(*argv, "report") == 0) {
337 } else if (matches(*argv, "help") == 0) {
340 fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv);
347 groups |= nl_mgrp(XFRMNLGRP_ACQUIRE);
349 groups |= nl_mgrp(XFRMNLGRP_EXPIRE);
351 groups |= nl_mgrp(XFRMNLGRP_SA);
353 groups |= nl_mgrp(XFRMNLGRP_POLICY);
355 groups |= nl_mgrp(XFRMNLGRP_AEVENTS);
357 groups |= nl_mgrp(XFRMNLGRP_REPORT);
361 fp = fopen(file, "r");
363 perror("Cannot fopen");
366 return rtnl_from_file(fp, xfrm_accept_msg, (void*)stdout);
371 if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0)
374 if (rtnl_listen(&rth, xfrm_accept_msg, (void*)stdout) < 0)