]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/xfrm_monitor.c
Merge branch 'master' into iproute2-next
[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 <netinet/in.h>
31
32 #include "utils.h"
33 #include "xfrm.h"
34 #include "ip_common.h"
35
36 static void usage(void) __attribute__((noreturn));
37 int listen_all_nsid;
38
39 static void usage(void)
40 {
41 fprintf(stderr, "Usage: ip xfrm monitor [all-nsid] [ all | OBJECTS | help ]\n");
42 fprintf(stderr, "OBJECTS := { acquire | expire | SA | aevent | policy | report }\n");
43 exit(-1);
44 }
45
46 static int xfrm_acquire_print(const struct sockaddr_nl *who,
47 struct nlmsghdr *n, void *arg)
48 {
49 FILE *fp = (FILE *)arg;
50 struct xfrm_user_acquire *xacq = NLMSG_DATA(n);
51 int len = n->nlmsg_len;
52 struct rtattr *tb[XFRMA_MAX+1];
53 __u16 family;
54
55 len -= NLMSG_LENGTH(sizeof(*xacq));
56 if (len < 0) {
57 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
58 return -1;
59 }
60
61 parse_rtattr(tb, XFRMA_MAX, XFRMACQ_RTA(xacq), len);
62
63 family = xacq->sel.family;
64 if (family == AF_UNSPEC)
65 family = xacq->policy.sel.family;
66 if (family == AF_UNSPEC)
67 family = preferred_family;
68
69 fprintf(fp, "acquire ");
70
71 fprintf(fp, "proto %s ", strxf_xfrmproto(xacq->id.proto));
72 if (show_stats > 0 || xacq->id.spi) {
73 __u32 spi = ntohl(xacq->id.spi);
74
75 fprintf(fp, "spi 0x%08x", spi);
76 if (show_stats > 0)
77 fprintf(fp, "(%u)", spi);
78 fprintf(fp, " ");
79 }
80 fprintf(fp, "%s", _SL_);
81
82 xfrm_selector_print(&xacq->sel, family, fp, " sel ");
83
84 xfrm_policy_info_print(&xacq->policy, tb, fp, " ", " policy ");
85
86 if (show_stats > 0)
87 fprintf(fp, " seq 0x%08u ", xacq->seq);
88 if (show_stats > 0) {
89 fprintf(fp, "%s-mask %s ",
90 strxf_algotype(XFRMA_ALG_CRYPT),
91 strxf_mask32(xacq->ealgos));
92 fprintf(fp, "%s-mask %s ",
93 strxf_algotype(XFRMA_ALG_AUTH),
94 strxf_mask32(xacq->aalgos));
95 fprintf(fp, "%s-mask %s",
96 strxf_algotype(XFRMA_ALG_COMP),
97 strxf_mask32(xacq->calgos));
98 }
99 fprintf(fp, "%s", _SL_);
100
101 if (oneline)
102 fprintf(fp, "\n");
103 fflush(fp);
104
105 return 0;
106 }
107
108 static int xfrm_state_flush_print(const struct sockaddr_nl *who,
109 struct nlmsghdr *n, void *arg)
110 {
111 FILE *fp = (FILE *)arg;
112 struct xfrm_usersa_flush *xsf = NLMSG_DATA(n);
113 int len = n->nlmsg_len;
114 const char *str;
115
116 len -= NLMSG_SPACE(sizeof(*xsf));
117 if (len < 0) {
118 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
119 return -1;
120 }
121
122 fprintf(fp, "Flushed state ");
123
124 str = strxf_xfrmproto(xsf->proto);
125 if (str)
126 fprintf(fp, "proto %s", str);
127 else
128 fprintf(fp, "proto %u", xsf->proto);
129 fprintf(fp, "%s", _SL_);
130
131 if (oneline)
132 fprintf(fp, "\n");
133 fflush(fp);
134
135 return 0;
136 }
137
138 static int xfrm_policy_flush_print(const struct sockaddr_nl *who,
139 struct nlmsghdr *n, void *arg)
140 {
141 struct rtattr *tb[XFRMA_MAX+1];
142 FILE *fp = (FILE *)arg;
143 int len = n->nlmsg_len;
144
145 len -= NLMSG_SPACE(0);
146 if (len < 0) {
147 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
148 return -1;
149 }
150
151 fprintf(fp, "Flushed policy ");
152
153 parse_rtattr(tb, XFRMA_MAX, NLMSG_DATA(n), len);
154
155 if (tb[XFRMA_POLICY_TYPE]) {
156 struct xfrm_userpolicy_type *upt;
157
158 fprintf(fp, "ptype ");
159
160 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
161 fprintf(fp, "(ERROR truncated)");
162
163 upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]);
164 fprintf(fp, "%s ", strxf_ptype(upt->type));
165 }
166
167 fprintf(fp, "%s", _SL_);
168
169 if (oneline)
170 fprintf(fp, "\n");
171 fflush(fp);
172
173 return 0;
174 }
175
176 static int xfrm_report_print(const struct sockaddr_nl *who,
177 struct nlmsghdr *n, void *arg)
178 {
179 FILE *fp = (FILE *)arg;
180 struct xfrm_user_report *xrep = NLMSG_DATA(n);
181 int len = n->nlmsg_len;
182 struct rtattr *tb[XFRMA_MAX+1];
183 __u16 family;
184
185 len -= NLMSG_LENGTH(sizeof(*xrep));
186 if (len < 0) {
187 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
188 return -1;
189 }
190
191 family = xrep->sel.family;
192 if (family == AF_UNSPEC)
193 family = preferred_family;
194
195 fprintf(fp, "report ");
196
197 fprintf(fp, "proto %s ", strxf_xfrmproto(xrep->proto));
198 fprintf(fp, "%s", _SL_);
199
200 xfrm_selector_print(&xrep->sel, family, fp, " sel ");
201
202 parse_rtattr(tb, XFRMA_MAX, XFRMREP_RTA(xrep), len);
203
204 xfrm_xfrma_print(tb, family, fp, " ");
205
206 if (oneline)
207 fprintf(fp, "\n");
208
209 return 0;
210 }
211
212 static void xfrm_ae_flags_print(__u32 flags, void *arg)
213 {
214 FILE *fp = (FILE *)arg;
215
216 fprintf(fp, " (0x%x) ", flags);
217 if (!flags)
218 return;
219 if (flags & XFRM_AE_CR)
220 fprintf(fp, " replay update ");
221 if (flags & XFRM_AE_CE)
222 fprintf(fp, " timer expired ");
223 if (flags & XFRM_AE_CU)
224 fprintf(fp, " policy updated ");
225
226 }
227
228 static void xfrm_usersa_print(const struct xfrm_usersa_id *sa_id, __u32 reqid, FILE *fp)
229 {
230 fprintf(fp, "dst %s ",
231 rt_addr_n2a(sa_id->family, sizeof(sa_id->daddr), &sa_id->daddr));
232
233 fprintf(fp, " reqid 0x%x", reqid);
234
235 fprintf(fp, " protocol %s ", strxf_proto(sa_id->proto));
236 fprintf(fp, " SPI 0x%x", ntohl(sa_id->spi));
237 }
238
239 static int xfrm_ae_print(const struct sockaddr_nl *who,
240 struct nlmsghdr *n, void *arg)
241 {
242 FILE *fp = (FILE *)arg;
243 struct xfrm_aevent_id *id = NLMSG_DATA(n);
244
245 fprintf(fp, "Async event ");
246 xfrm_ae_flags_print(id->flags, arg);
247 fprintf(fp, "\n\t");
248 fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family,
249 sizeof(id->saddr), &id->saddr));
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 fprintf(fp, "%s", rt_addr_n2a(family, sizeof(*a), a));
262 }
263
264 static int xfrm_mapping_print(const struct sockaddr_nl *who,
265 struct nlmsghdr *n, void *arg)
266 {
267 FILE *fp = (FILE *)arg;
268 struct xfrm_user_mapping *map = NLMSG_DATA(n);
269
270 fprintf(fp, "Mapping change ");
271 xfrm_print_addr(fp, map->id.family, &map->old_saddr);
272
273 fprintf(fp, ":%d -> ", ntohs(map->old_sport));
274 xfrm_print_addr(fp, map->id.family, &map->new_saddr);
275 fprintf(fp, ":%d\n\t", ntohs(map->new_sport));
276
277 xfrm_usersa_print(&map->id, map->reqid, fp);
278
279 fprintf(fp, "\n");
280 fflush(fp);
281 return 0;
282 }
283
284 static int xfrm_accept_msg(const struct sockaddr_nl *who,
285 struct rtnl_ctrl_data *ctrl,
286 struct nlmsghdr *n, void *arg)
287 {
288 FILE *fp = (FILE *)arg;
289
290 if (timestamp)
291 print_timestamp(fp);
292
293 if (listen_all_nsid) {
294 if (ctrl == NULL || ctrl->nsid < 0)
295 fprintf(fp, "[nsid current]");
296 else
297 fprintf(fp, "[nsid %d]", ctrl->nsid);
298 }
299
300 switch (n->nlmsg_type) {
301 case XFRM_MSG_NEWSA:
302 case XFRM_MSG_DELSA:
303 case XFRM_MSG_UPDSA:
304 case XFRM_MSG_EXPIRE:
305 xfrm_state_print(who, n, arg);
306 return 0;
307 case XFRM_MSG_NEWPOLICY:
308 case XFRM_MSG_DELPOLICY:
309 case XFRM_MSG_UPDPOLICY:
310 case XFRM_MSG_POLEXPIRE:
311 xfrm_policy_print(who, n, arg);
312 return 0;
313 case XFRM_MSG_ACQUIRE:
314 xfrm_acquire_print(who, n, arg);
315 return 0;
316 case XFRM_MSG_FLUSHSA:
317 xfrm_state_flush_print(who, n, arg);
318 return 0;
319 case XFRM_MSG_FLUSHPOLICY:
320 xfrm_policy_flush_print(who, n, arg);
321 return 0;
322 case XFRM_MSG_REPORT:
323 xfrm_report_print(who, n, arg);
324 return 0;
325 case XFRM_MSG_NEWAE:
326 xfrm_ae_print(who, n, arg);
327 return 0;
328 case XFRM_MSG_MAPPING:
329 xfrm_mapping_print(who, n, arg);
330 return 0;
331 default:
332 break;
333 }
334
335 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
336 n->nlmsg_type != NLMSG_DONE) {
337 fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n",
338 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
339 }
340 return 0;
341 }
342
343 extern struct rtnl_handle rth;
344
345 int do_xfrm_monitor(int argc, char **argv)
346 {
347 char *file = NULL;
348 unsigned int groups = ~((unsigned)0); /* XXX */
349 int lacquire = 0;
350 int lexpire = 0;
351 int laevent = 0;
352 int lpolicy = 0;
353 int lsa = 0;
354 int lreport = 0;
355
356 rtnl_close(&rth);
357
358 while (argc > 0) {
359 if (matches(*argv, "file") == 0) {
360 NEXT_ARG();
361 file = *argv;
362 } else if (strcmp(*argv, "all") == 0) {
363 /* fall out */
364 } else if (matches(*argv, "all-nsid") == 0) {
365 listen_all_nsid = 1;
366 } else if (matches(*argv, "acquire") == 0) {
367 lacquire = 1;
368 groups = 0;
369 } else if (matches(*argv, "expire") == 0) {
370 lexpire = 1;
371 groups = 0;
372 } else if (matches(*argv, "SA") == 0) {
373 lsa = 1;
374 groups = 0;
375 } else if (matches(*argv, "aevent") == 0) {
376 laevent = 1;
377 groups = 0;
378 } else if (matches(*argv, "policy") == 0) {
379 lpolicy = 1;
380 groups = 0;
381 } else if (matches(*argv, "report") == 0) {
382 lreport = 1;
383 groups = 0;
384 } else if (matches(*argv, "help") == 0) {
385 usage();
386 } else {
387 fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv);
388 exit(-1);
389 }
390 argc--; argv++;
391 }
392
393 if (lacquire)
394 groups |= nl_mgrp(XFRMNLGRP_ACQUIRE);
395 if (lexpire)
396 groups |= nl_mgrp(XFRMNLGRP_EXPIRE);
397 if (lsa)
398 groups |= nl_mgrp(XFRMNLGRP_SA);
399 if (lpolicy)
400 groups |= nl_mgrp(XFRMNLGRP_POLICY);
401 if (laevent)
402 groups |= nl_mgrp(XFRMNLGRP_AEVENTS);
403 if (lreport)
404 groups |= nl_mgrp(XFRMNLGRP_REPORT);
405
406 if (file) {
407 FILE *fp;
408 int err;
409
410 fp = fopen(file, "r");
411 if (fp == NULL) {
412 perror("Cannot fopen");
413 exit(-1);
414 }
415 err = rtnl_from_file(fp, xfrm_accept_msg, stdout);
416 fclose(fp);
417 return err;
418 }
419
420 if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0)
421 exit(1);
422 if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0)
423 exit(1);
424
425 if (rtnl_listen(&rth, xfrm_accept_msg, (void *)stdout) < 0)
426 exit(2);
427
428 return 0;
429 }