]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/xfrm_monitor.c
cleanup warnings
[mirror_iproute2.git] / ip / xfrm_monitor.c
1 /* $USAGI: $ */
2
3 /*
4 * Copyright (C)2005 USAGI/WIDE Project
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses>.
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>
30 #include <linux/xfrm.h>
31 #include "utils.h"
32 #include "xfrm.h"
33 #include "ip_common.h"
34
35 static void usage(void) __attribute__((noreturn));
36
37 static void usage(void)
38 {
39 fprintf(stderr, "Usage: ip xfrm monitor [ all | LISTofXFRM-OBJECTS ]\n");
40 exit(-1);
41 }
42
43 static 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
52 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);
59
60 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");
99 fflush(fp);
100
101 return 0;
102 }
103
104 static 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
134 static 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
172 static 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
181 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
208 static void xfrm_ae_flags_print(__u32 flags, void *arg)
209 {
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
223 static 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;
228 fprintf(fp, "dst %s ",
229 rt_addr_n2a(sa_id->family, &sa_id->daddr, buf, sizeof(buf)));
230
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
237 static 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));
248 fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family, &id->saddr,
249 abuf, sizeof(abuf)));
250
251 xfrm_usersa_print(&id->sa_id, id->reqid, fp);
252
253 fprintf(fp, "\n");
254 fflush(fp);
255
256 return 0;
257 }
258
259 static void xfrm_print_addr(FILE *fp, int family, xfrm_address_t *a)
260 {
261 char buf[256];
262
263 buf[0] = 0;
264 fprintf(fp, "%s", rt_addr_n2a(family, a, buf, sizeof(buf)));
265 }
266
267 static 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 ");
274 xfrm_print_addr(fp, map->id.family, &map->old_saddr);
275
276 fprintf(fp, ":%d -> ", ntohs(map->old_sport));
277 xfrm_print_addr(fp, map->id.family, &map->new_saddr);
278 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
287 static int xfrm_accept_msg(const struct sockaddr_nl *who,
288 struct nlmsghdr *n, void *arg)
289 {
290 FILE *fp = (FILE*)arg;
291
292 if (timestamp)
293 print_timestamp(fp);
294
295 switch (n->nlmsg_type) {
296 case XFRM_MSG_NEWSA:
297 case XFRM_MSG_DELSA:
298 case XFRM_MSG_UPDSA:
299 case XFRM_MSG_EXPIRE:
300 xfrm_state_print(who, n, arg);
301 return 0;
302 case XFRM_MSG_NEWPOLICY:
303 case XFRM_MSG_DELPOLICY:
304 case XFRM_MSG_UPDPOLICY:
305 case XFRM_MSG_POLEXPIRE:
306 xfrm_policy_print(who, n, arg);
307 return 0;
308 case XFRM_MSG_ACQUIRE:
309 xfrm_acquire_print(who, n, arg);
310 return 0;
311 case XFRM_MSG_FLUSHSA:
312 xfrm_state_flush_print(who, n, arg);
313 return 0;
314 case XFRM_MSG_FLUSHPOLICY:
315 xfrm_policy_flush_print(who, n, arg);
316 return 0;
317 case XFRM_MSG_REPORT:
318 xfrm_report_print(who, n, arg);
319 return 0;
320 case XFRM_MSG_NEWAE:
321 xfrm_ae_print(who, n, arg);
322 return 0;
323 case XFRM_MSG_MAPPING:
324 xfrm_mapping_print(who, n, arg);
325 return 0;
326 default:
327 break;
328 }
329
330 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
331 n->nlmsg_type != NLMSG_DONE) {
332 fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n",
333 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
334 }
335 return 0;
336 }
337
338 extern struct rtnl_handle rth;
339
340 int do_xfrm_monitor(int argc, char **argv)
341 {
342 char *file = NULL;
343 unsigned groups = ~((unsigned)0); /* XXX */
344 int lacquire=0;
345 int lexpire=0;
346 int laevent=0;
347 int lpolicy=0;
348 int lsa=0;
349 int lreport=0;
350
351 rtnl_close(&rth);
352
353 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;
363 } else if (matches(*argv, "SA") == 0) {
364 lsa=1;
365 groups = 0;
366 } else if (matches(*argv, "aevent") == 0) {
367 laevent=1;
368 groups = 0;
369 } else if (matches(*argv, "policy") == 0) {
370 lpolicy=1;
371 groups = 0;
372 } else if (matches(*argv, "report") == 0) {
373 lreport=1;
374 groups = 0;
375 } 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)
385 groups |= nl_mgrp(XFRMNLGRP_ACQUIRE);
386 if (lexpire)
387 groups |= nl_mgrp(XFRMNLGRP_EXPIRE);
388 if (lsa)
389 groups |= nl_mgrp(XFRMNLGRP_SA);
390 if (lpolicy)
391 groups |= nl_mgrp(XFRMNLGRP_POLICY);
392 if (laevent)
393 groups |= nl_mgrp(XFRMNLGRP_AEVENTS);
394 if (lreport)
395 groups |= nl_mgrp(XFRMNLGRP_REPORT);
396
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
407 if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0)
408 exit(1);
409
410 if (rtnl_listen(&rth, xfrm_accept_msg, (void*)stdout) < 0)
411 exit(2);
412
413 return 0;
414 }