blob: 154bbee01bb020eee5d37048d8315950fa1a08a7 [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{
40 fprintf(stderr, "Usage: ip xfrm monitor [ all | LISTofOBJECTS ]\n");
41 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
53 if (n->nlmsg_type != XFRM_MSG_ACQUIRE) {
54 fprintf(stderr, "Not an acquire: %08x %08x %08x\n",
55 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
56 return 0;
57 }
58
59 len -= NLMSG_LENGTH(sizeof(*xacq));
60 if (len < 0) {
61 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
62 return -1;
63 }
64
65 parse_rtattr(tb, XFRMA_MAX, XFRMACQ_RTA(xacq), len);
shemminger90f93022005-06-07 21:55:55 +000066
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +000067 family = xacq->sel.family;
68 if (family == AF_UNSPEC)
69 family = xacq->policy.sel.family;
70 if (family == AF_UNSPEC)
71 family = preferred_family;
72
73 fprintf(fp, "acquire ");
74
75 fprintf(fp, "proto %s ", strxf_xfrmproto(xacq->id.proto));
76 if (show_stats > 0 || xacq->id.spi) {
77 __u32 spi = ntohl(xacq->id.spi);
78 fprintf(fp, "spi 0x%08x", spi);
79 if (show_stats > 0)
80 fprintf(fp, "(%u)", spi);
81 fprintf(fp, " ");
82 }
83 fprintf(fp, "%s", _SL_);
84
85 xfrm_selector_print(&xacq->sel, family, fp, " sel ");
86
87 xfrm_policy_info_print(&xacq->policy, tb, fp, " ", " policy ");
88
89 if (show_stats > 0)
90 fprintf(fp, " seq 0x%08u ", xacq->seq);
91 if (show_stats > 0) {
92 fprintf(fp, "%s-mask %s ",
93 strxf_algotype(XFRMA_ALG_CRYPT),
94 strxf_mask32(xacq->ealgos));
95 fprintf(fp, "%s-mask %s ",
96 strxf_algotype(XFRMA_ALG_AUTH),
97 strxf_mask32(xacq->aalgos));
98 fprintf(fp, "%s-mask %s",
99 strxf_algotype(XFRMA_ALG_COMP),
100 strxf_mask32(xacq->calgos));
101 }
102 fprintf(fp, "%s", _SL_);
103
104 if (oneline)
105 fprintf(fp, "\n");
shemminger669ae742005-11-07 18:39:30 +0000106 fflush(fp);
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000107
108 return 0;
109}
110
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900111static int xfrm_report_print(const struct sockaddr_nl *who,
112 struct nlmsghdr *n, void *arg)
113{
114 FILE *fp = (FILE*)arg;
115 struct xfrm_user_report *xrep = NLMSG_DATA(n);
116 int len = n->nlmsg_len;
117 struct rtattr * tb[XFRMA_MAX+1];
118 __u16 family;
119
120 if (n->nlmsg_type != XFRM_MSG_REPORT) {
121 fprintf(stderr, "Not a report: %08x %08x %08x\n",
122 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
123 return 0;
124 }
125
126 len -= NLMSG_LENGTH(sizeof(*xrep));
127 if (len < 0) {
128 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
129 return -1;
130 }
131
132 family = xrep->sel.family;
133 if (family == AF_UNSPEC)
134 family = preferred_family;
135
136 fprintf(fp, "report ");
137
138 fprintf(fp, "proto %s ", strxf_xfrmproto(xrep->proto));
139 fprintf(fp, "%s", _SL_);
140
141 xfrm_selector_print(&xrep->sel, family, fp, " sel ");
142
143 parse_rtattr(tb, XFRMA_MAX, XFRMREP_RTA(xrep), len);
144
145 xfrm_xfrma_print(tb, family, fp, " ");
146
147 if (oneline)
148 fprintf(fp, "\n");
149
150 return 0;
151}
152
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000153static int xfrm_accept_msg(const struct sockaddr_nl *who,
154 struct nlmsghdr *n, void *arg)
155{
156 FILE *fp = (FILE*)arg;
157
shemminger90f93022005-06-07 21:55:55 +0000158 if (timestamp)
159 print_timestamp(fp);
160
shemmingerc595c792005-11-01 23:03:03 +0000161 if (n->nlmsg_type == XFRM_MSG_NEWSA ||
shemminger669ae742005-11-07 18:39:30 +0000162 n->nlmsg_type == XFRM_MSG_DELSA ||
163 n->nlmsg_type == XFRM_MSG_UPDSA ||
164 n->nlmsg_type == XFRM_MSG_EXPIRE) {
shemmingerc595c792005-11-01 23:03:03 +0000165 xfrm_state_print(who, n, arg);
166 return 0;
167 }
168 if (n->nlmsg_type == XFRM_MSG_NEWPOLICY ||
shemminger669ae742005-11-07 18:39:30 +0000169 n->nlmsg_type == XFRM_MSG_DELPOLICY ||
170 n->nlmsg_type == XFRM_MSG_UPDPOLICY ||
171 n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
shemmingerc595c792005-11-01 23:03:03 +0000172 xfrm_policy_print(who, n, arg);
173 return 0;
174 }
175
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000176 if (n->nlmsg_type == XFRM_MSG_ACQUIRE) {
177 xfrm_acquire_print(who, n, arg);
178 return 0;
179 }
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000180 if (n->nlmsg_type == XFRM_MSG_FLUSHSA) {
181 /* XXX: Todo: show proto in xfrm_usersa_flush */
182 fprintf(fp, "Flushed state\n");
183 return 0;
184 }
185 if (n->nlmsg_type == XFRM_MSG_FLUSHPOLICY) {
186 fprintf(fp, "Flushed policy\n");
187 return 0;
188 }
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900189 if (n->nlmsg_type == XFRM_MSG_REPORT) {
190 xfrm_report_print(who, n, arg);
191 return 0;
192 }
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000193 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
194 n->nlmsg_type != NLMSG_DONE) {
shemmingerc595c792005-11-01 23:03:03 +0000195 fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n",
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000196 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
197 }
198 return 0;
199}
200
Stephen Hemminger77219712006-09-25 17:27:37 -0700201extern struct rtnl_handle rth;
202
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000203int do_xfrm_monitor(int argc, char **argv)
204{
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000205 char *file = NULL;
206 unsigned groups = ~((unsigned)0); /* XXX */
207 int lacquire=0;
208 int lexpire=0;
shemmingerc595c792005-11-01 23:03:03 +0000209 int lpolicy=0;
210 int lsa=0;
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900211 int lreport=0;
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000212
Stephen Hemminger77219712006-09-25 17:27:37 -0700213 rtnl_close(&rth);
214
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000215 while (argc > 0) {
216 if (matches(*argv, "file") == 0) {
217 NEXT_ARG();
218 file = *argv;
219 } else if (matches(*argv, "acquire") == 0) {
220 lacquire=1;
221 groups = 0;
222 } else if (matches(*argv, "expire") == 0) {
223 lexpire=1;
224 groups = 0;
shemmingerc595c792005-11-01 23:03:03 +0000225 } else if (matches(*argv, "SA") == 0) {
226 lsa=1;
227 groups = 0;
228 } else if (matches(*argv, "policy") == 0) {
229 lpolicy=1;
230 groups = 0;
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900231 } else if (matches(*argv, "report") == 0) {
232 lreport=1;
233 groups = 0;
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000234 } else if (matches(*argv, "help") == 0) {
235 usage();
236 } else {
237 fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv);
238 exit(-1);
239 }
240 argc--; argv++;
241 }
242
243 if (lacquire)
244 groups |= XFRMGRP_ACQUIRE;
245 if (lexpire)
246 groups |= XFRMGRP_EXPIRE;
shemmingerc595c792005-11-01 23:03:03 +0000247 if (lsa)
248 groups |= XFRMGRP_SA;
249 if (lpolicy)
250 groups |= XFRMGRP_POLICY;
Masahide NAKAMURAc54f31e2006-12-05 19:16:05 +0900251 if (lreport)
252 groups |= XFRMGRP_REPORT;
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000253
254 if (file) {
255 FILE *fp;
256 fp = fopen(file, "r");
257 if (fp == NULL) {
258 perror("Cannot fopen");
259 exit(-1);
260 }
261 return rtnl_from_file(fp, xfrm_accept_msg, (void*)stdout);
262 }
263
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000264 //ll_init_map(&rth);
265
shemmingerc595c792005-11-01 23:03:03 +0000266 if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0)
267 exit(1);
268
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000269 if (rtnl_listen(&rth, xfrm_accept_msg, (void*)stdout) < 0)
270 exit(2);
271
shemminger351efcd2005-09-01 19:21:50 +0000272 return 0;
linux-ipv6.org!nakamf9cb3a22005-03-22 16:13:21 +0000273}