• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
usage(void)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 
xfrm_acquire_print(const struct sockaddr_nl * who,struct nlmsghdr * n,void * arg)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 
xfrm_state_flush_print(const struct sockaddr_nl * who,struct nlmsghdr * n,void * arg)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 
xfrm_policy_flush_print(const struct sockaddr_nl * who,struct nlmsghdr * n,void * arg)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 
xfrm_report_print(const struct sockaddr_nl * who,struct nlmsghdr * n,void * arg)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 
xfrm_ae_flags_print(__u32 flags,void * arg)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 
xfrm_usersa_print(const struct xfrm_usersa_id * sa_id,__u32 reqid,FILE * fp)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 
xfrm_ae_print(const struct sockaddr_nl * who,struct nlmsghdr * n,void * arg)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 
xfrm_print_addr(FILE * fp,int family,xfrm_address_t * a)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 
xfrm_mapping_print(const struct sockaddr_nl * who,struct nlmsghdr * n,void * arg)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 
xfrm_accept_msg(const struct sockaddr_nl * who,struct rtnl_ctrl_data * ctrl,struct nlmsghdr * n,void * arg)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 
do_xfrm_monitor(int argc,char ** argv)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 (matches(*argv, "all-nsid") == 0) {
363 			listen_all_nsid = 1;
364 		} else if (matches(*argv, "acquire") == 0) {
365 			lacquire = 1;
366 			groups = 0;
367 		} else if (matches(*argv, "expire") == 0) {
368 			lexpire = 1;
369 			groups = 0;
370 		} else if (matches(*argv, "SA") == 0) {
371 			lsa = 1;
372 			groups = 0;
373 		} else if (matches(*argv, "aevent") == 0) {
374 			laevent = 1;
375 			groups = 0;
376 		} else if (matches(*argv, "policy") == 0) {
377 			lpolicy = 1;
378 			groups = 0;
379 		} else if (matches(*argv, "report") == 0) {
380 			lreport = 1;
381 			groups = 0;
382 		} else if (matches(*argv, "help") == 0) {
383 			usage();
384 		} else if (strcmp(*argv, "all")) {
385 			fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv);
386 			exit(-1);
387 		}
388 		argc--;	argv++;
389 	}
390 
391 	if (lacquire)
392 		groups |= nl_mgrp(XFRMNLGRP_ACQUIRE);
393 	if (lexpire)
394 		groups |= nl_mgrp(XFRMNLGRP_EXPIRE);
395 	if (lsa)
396 		groups |= nl_mgrp(XFRMNLGRP_SA);
397 	if (lpolicy)
398 		groups |= nl_mgrp(XFRMNLGRP_POLICY);
399 	if (laevent)
400 		groups |= nl_mgrp(XFRMNLGRP_AEVENTS);
401 	if (lreport)
402 		groups |= nl_mgrp(XFRMNLGRP_REPORT);
403 
404 	if (file) {
405 		FILE *fp;
406 		int err;
407 
408 		fp = fopen(file, "r");
409 		if (fp == NULL) {
410 			perror("Cannot fopen");
411 			exit(-1);
412 		}
413 		err = rtnl_from_file(fp, xfrm_accept_msg, stdout);
414 		fclose(fp);
415 		return err;
416 	}
417 
418 	if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0)
419 		exit(1);
420 	if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0)
421 		exit(1);
422 
423 	if (rtnl_listen(&rth, xfrm_accept_msg, (void *)stdout) < 0)
424 		exit(2);
425 
426 	return 0;
427 }
428